Skip to content

Commit 5c65a98

Browse files
authored
Sessions label bug (#48)
* sessions existed where 'sessions_label' should have existed * update version seeking, bump pyproject.toml version * fixed logic on if 'sub-' string replacement
1 parent b27a724 commit 5c65a98

File tree

3 files changed

+260
-275
lines changed

3 files changed

+260
-275
lines changed

petdeface/petdeface.py

Lines changed: 28 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from niworkflows.utils.misc import check_valid_fs_license
2525

2626
from petutils.petutils import collect_anat_and_pet
27+
from importlib.metadata import version
2728

2829

2930
try:
@@ -42,43 +43,27 @@
4243
pathlib.Path(__file__).parent.parent.absolute(),
4344
]
4445

45-
__version__ = "unable to locate version number in pyproject.toml"
46+
__version__ = "unable to locate version number"
4647
# we use the default version at the time of this writing, but the most current version
4748
# can be found in the pyproject.toml file under the [tool.bids] section
4849
__bids_version__ = "1.8.0"
4950

5051

51-
# search for toml file
52-
for place in places_to_look:
53-
for root, folders, files in os.walk(place):
54-
for file in files:
55-
if file.endswith("pyproject.toml") and "petdeface" in os.path.join(
56-
root, file
57-
):
58-
toml_file = os.path.join(root, file)
59-
60-
with open(toml_file, "r") as f:
61-
for line in f.readlines():
62-
if (
63-
"version" in line
64-
and len(line.split("=")) > 1
65-
and "bids_version" not in line
66-
):
52+
if __version__ == "unable to locate version number":
53+
# we try to load the version using import lib
54+
try:
55+
__version__ = version(__package__)
56+
except ValueError:
57+
# if we can't load the version using importlib we try to load it from the pyproject.toml
58+
for place in places_to_look:
59+
try:
60+
with open(place / "pyproject.toml") as f:
61+
for line in f:
62+
if "version" in line and "bid" not in line.lower():
6763
__version__ = line.split("=")[1].strip().replace('"', "")
68-
if "bids_version" in line and len(line.split("=")) > 1:
69-
__bids_version__ = (
70-
line.split("=")[1].strip().replace('"', "")
71-
)
72-
# if the version number is found and is formatted with major.minor.patch formating we can break
73-
# we check the version with a regex expression to see if all of the parts are there
74-
if re.match(r"\d+\.\d+\.\d+", __version__):
75-
break
76-
77-
if __version__ != "unable to locate version number in pyproject.toml":
78-
# we try to load the version using import lib
79-
__version__ = importlib.metadata.version("petdeface")
80-
if re.match(r"\d+\.\d+\.\d+", __version__):
81-
break
64+
break
65+
except FileNotFoundError:
66+
pass
8267

8368

8469
def locate_freesurfer_license():
@@ -231,9 +216,7 @@ def deface(args: Union[dict, argparse.Namespace]) -> None:
231216
# right side of the sub- string as the subject label
232217
if any("sub-" in subject for subject in subjects):
233218
print("One or more subject contains sub- string")
234-
subjects = [
235-
subject.replace("sub-", "") for subject in subjects if "sub-" in subject
236-
]
219+
subjects = [subject.replace("sub-", "") for subject in subjects]
237220
# raise error if a supplied subject is not contained in the dataset
238221
participants = collect_participants(
239222
args.bids_dir, bids_validate=~args.skip_bids_validator
@@ -257,7 +240,9 @@ def deface(args: Union[dict, argparse.Namespace]) -> None:
257240
subject.replace("sub-", "") for subject in args.participant_label_exclude
258241
]
259242
subjects = [
260-
subject for subject in subjects if subject not in args.participant_label_exclude
243+
subject
244+
for subject in subjects
245+
if subject not in args.participant_label_exclude
261246
]
262247

263248
print(f"Subjects remaining in the defacing workflow: {subjects}")
@@ -277,7 +262,7 @@ def deface(args: Union[dict, argparse.Namespace]) -> None:
277262
args.bids_dir,
278263
preview_pics=args.preview_pics,
279264
anat_only=args.anat_only,
280-
session=args.session_label,
265+
session_label=args.session_label,
281266
session_label_exclude=args.session_label_exclude,
282267
)
283268
except FileNotFoundError:
@@ -305,7 +290,7 @@ def init_single_subject_wf(
305290
output_dir: pathlib.Path = None,
306291
preview_pics=False,
307292
anat_only=False,
308-
session=[],
293+
session_label=[],
309294
session_label_exclude=[],
310295
) -> Workflow:
311296
"""
@@ -343,10 +328,10 @@ def init_single_subject_wf(
343328

344329
# we combine the sessions to include and exclude into a single set of sessions to exclude from
345330
# the set of all sessions
346-
if session:
331+
if session_label:
347332
sessions_to_exclude = list(
348333
set(bids_data.get_sessions())
349-
- (set(bids_data.get_sessions()) & set(session))
334+
- (set(bids_data.get_sessions()) & set(session_label))
350335
| set(session_label_exclude)
351336
)
352337
else:
@@ -768,7 +753,7 @@ def __init__(
768753
placement="adjacent",
769754
preview_pics=True,
770755
participant_label_exclude=[],
771-
session=[],
756+
session_label=[],
772757
session_label_exclude=[],
773758
):
774759
self.bids_dir = bids_dir
@@ -781,7 +766,7 @@ def __init__(
781766
self.skip_bids_validator = skip_bids_validator
782767
self.preview_pics = preview_pics
783768
self.participant_label_exclude = participant_label_exclude
784-
self.session = session
769+
self.session_label = session_label
785770
self.session_label_exclude = session_label_exclude
786771

787772
# check if freesurfer license is valid
@@ -813,7 +798,7 @@ def run(self):
813798
"remove_existing": self.remove_existing,
814799
"preview_pics": self.preview_pics,
815800
"participant_label_exclude": self.participant_label_exclude,
816-
"session": self.session,
801+
"session_label": self.session_label,
817802
"session_label_exclude": self.session_label_exclude,
818803
}
819804
)
@@ -1133,7 +1118,7 @@ def main(): # noqa: max-complexity: 12
11331118
placement=args.placement,
11341119
preview_pics=args.preview_pics,
11351120
participant_label_exclude=args.participant_label_exclude,
1136-
session=args.session_label,
1121+
session_label=args.session_label,
11371122
session_label_exclude=args.session_label_exclude,
11381123
)
11391124
petdeface.run()

0 commit comments

Comments
 (0)