Skip to content

Commit 262feaa

Browse files
authored
Adopting click as command line interfaces (#30)
* initial * fixing the toml * final touches on adopting click * deleting test elements * help for include_empty_properties * typo * default for convert * adopting the tests for click * creating convert_click * cleaning the converter file * adding new tests for click * final modifications in click
1 parent 7532b1b commit 262feaa

File tree

4 files changed

+71
-17
lines changed

4 files changed

+71
-17
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ __pycache__/
66
# C extensions
77
*.so
88

9+
#vscode
10+
.vscode
11+
912
# Distribution / packaging
1013
.Python
1114
build/

bids2openminds/converter.py

+32-17
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,20 @@
11
from warnings import warn
22
from bids import BIDSLayout, BIDSValidator
33
import os
4+
import click
45
from . import main
56
from . import utility
67
from . import globals
78

8-
9-
10-
def convert(bids_dir, output_filename=None):
11-
12-
if not (os.path.isdir(bids_dir)):
9+
def convert(input_path, output_path=None, multiple_files=False, include_empty_properties=False):
10+
if not (os.path.isdir(input_path)):
1311
raise NotADirectoryError(
14-
f"The input directory is not valid, you have specified {bids_dir} which is not a directory."
12+
f"The input directory is not valid, you have specified {input_path} which is not a directory."
1513
)
14+
# if not(BIDSValidator().is_bids(input_path)):
15+
# raise NotADirectoryError(f"The input directory is not valid, you have specified {input_path} which is not a BIDS directory.")
1616

17-
# if not(BIDSValidator().is_bids(bids_dir)):
18-
# raise NotADirectoryError(f"The input directory is not valid, you have specified {bids_dir} which is not a BIDS directory.")
19-
20-
bids_layout = BIDSLayout(bids_dir)
17+
bids_layout = BIDSLayout(input_path)
2118

2219
layout_df = bids_layout.to_df()
2320

@@ -32,22 +29,40 @@ def convert(bids_dir, output_filename=None):
3229

3330
[subjects_dict, subject_state_dict, subjects_list] = main.create_subjects(subjects_id, layout_df, bids_layout)
3431

35-
[files_list, file_repository] = main.create_file(layout_df, bids_dir)
32+
[files_list, file_repository] = main.create_file(layout_df, input_path)
3633

3734
dataset_version = main.create_dataset_version(
3835
bids_layout, dataset_description, layout_df, subjects_list, file_repository
3936
)
4037

4138
dataset = main.create_dataset(dataset_description, dataset_version)
42-
39+
4340
failures = globals.collection.validate(ignore=["required", "value"])
4441
assert len(failures) == 0
4542

46-
if output_filename is None:
47-
output_filename = os.path.join(bids_dir, "openminds.jsonld")
48-
globals.collection.save(output_filename)
43+
if output_path is None:
44+
if multiple_files:
45+
output_path=os.path.join(input_path, "openminds")
46+
else:
47+
output_path = os.path.join(input_path, "openminds.jsonld")
48+
49+
globals.collection.save(output_path,individual_files=multiple_files,include_empty_properties=include_empty_properties)
50+
51+
print(f"Conversion was successful, the openMINDS file is in {output_path}")
52+
53+
54+
55+
@click.command()
56+
@click.argument("input-path", type=click.Path(file_okay=False,exists=True))
57+
@click.option("-o","--output-path",default=None,type=click.Path(file_okay=True,writable=True),help="The output path or filename for OpenMINDS file/files.")
58+
@click.option("--single-file","multiple_files",flag_value=False,default=False,help="Save the entire collection into a single file (default).")
59+
@click.option("--multiple-files","multiple_files",flag_value=True,help="Each node is saved into a separate file within the specified directory. 'output-path' if specified, must be a directory.")
60+
@click.option("-e","--include-empty-properties",is_flag=True,default=False,help="Whether to include empty properties in the final file.")
61+
def convert_click(input_path, output_path, multiple_files, include_empty_properties):
62+
convert(input_path, output_path, multiple_files, include_empty_properties)
63+
4964

5065

5166
if __name__ == "__main__":
52-
bids_dir = input("Enter the BIDS directory path: ")
53-
convert(bids_dir)
67+
input_path = input("Enter the BIDS directory path: ")
68+
convert(input_path)

pyproject.toml

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ version = "0.0.0"
44
dependencies = [
55
"bids",
66
"openminds >= 0.2.2",
7+
"click>=8.1",
78
"pandas",
89
]
910
requires-python = ">=3.9"
@@ -19,6 +20,9 @@ classifiers = [
1920
"Programming Language :: Python"
2021
]
2122

23+
[project.scripts]
24+
bids2openminds="bids2openminds.converter:convert_click"
25+
2226
[build-system]
2327
requires = ["setuptools >= 61.0"]
2428
build-backend = "setuptools.build_meta"

test/test_example_datasets_click.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import os
2+
from openminds import Collection
3+
from bids2openminds.converter import convert_click
4+
from click.testing import CliRunner
5+
6+
(test_data_set,number_of_openminds_files)= ("ds003",98)
7+
8+
def test_example_datasets_click():
9+
test_dir = os.path.join("bids-examples",test_data_set)
10+
runner = CliRunner()
11+
result = runner.invoke(convert_click, [test_dir])
12+
assert result.exit_code == 0
13+
c = Collection()
14+
c.load(os.path.join(test_dir, "openminds.jsonld"))
15+
16+
#todo This section has a problem which have been solved in pull request number 32
17+
""" def test_example_datasets_click_seperate_files():
18+
test_dir = os.path.join("bids-examples", test_data_set)
19+
runner = CliRunner()
20+
result = runner.invoke(convert_click, ["--multiple-file",test_dir])
21+
assert result.exit_code == 0
22+
path_openminds=os.path.join(test_dir, "openminds")
23+
numer_of_files=len(os.listdir(path_openminds))
24+
assert numer_of_files==number_of_openminds_files """
25+
26+
def test_example_datasets_click_output_location():
27+
test_dir = os.path.join("bids-examples", test_data_set)
28+
runner = CliRunner()
29+
result = runner.invoke(convert_click, ["-o","test_openminds.jsonld",test_dir])
30+
assert result.exit_code == 0
31+
c = Collection()
32+
c.load("test_openminds.jsonld")

0 commit comments

Comments
 (0)