Skip to content

Commit c93c6d5

Browse files
authored
♻️ DRY up output key collection for group config (#2210)
2 parents 2ffff67 + 0978ac3 commit c93c6d5

File tree

4 files changed

+29
-71
lines changed

4 files changed

+29
-71
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
5353
- Fixed empty `shell` variable in cluster run scripts.
5454
- A bug in which bandpass filters always assumed 1D regressor files have exactly 5 header rows.
5555
- Removed an erroneous connection to AFNI 3dTProject in nuisance denoising that would unnecessarily send a spike regressor as a censor. This would sometimes cause TRs to unnecessarily be dropped from the timeseries as if scrubbing were being performed.
56+
- Lingering calls to `cpac_outputs.csv` (was changed to `cpac_outputs.tsv` in v1.8.1).
5657

5758
### Removed
5859

CPAC/pipeline/cpac_group_runner.py

Lines changed: 7 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (C) 2022-2024 C-PAC Developers
1+
# Copyright (C) 2022-2025 C-PAC Developers
22

33
# This file is part of C-PAC.
44

@@ -143,31 +143,12 @@ def gather_nifti_globs(pipeline_output_folder, resource_list, pull_func=False):
143143
import glob
144144
import os
145145

146-
import pandas as pd
147-
import pkg_resources as p
146+
from CPAC.utils.outputs import group_derivatives
148147

149148
exts = ".nii"
150149
nifti_globs = []
151150

152-
keys_tsv = p.resource_filename("CPAC", "resources/cpac_outputs.tsv")
153-
try:
154-
keys = pd.read_csv(keys_tsv, delimiter="\t")
155-
except Exception as e:
156-
err = (
157-
"\n[!] Could not access or read the cpac_outputs.tsv "
158-
f"resource file:\n{keys_tsv}\n\nError details {e}\n"
159-
)
160-
raise Exception(err)
161-
162-
derivative_list = list(keys[keys["Sub-Directory"] == "func"]["Resource"])
163-
derivative_list = derivative_list + list(
164-
keys[keys["Sub-Directory"] == "anat"]["Resource"]
165-
)
166-
167-
if pull_func:
168-
derivative_list = derivative_list + list(
169-
keys[keys["Space"] == "functional"]["Resource"]
170-
)
151+
derivative_list = group_derivatives(pull_func)
171152

172153
if len(resource_list) == 0:
173154
err = "\n\n[!] No derivatives selected!\n\n"
@@ -361,33 +342,14 @@ def create_output_dict_list(
361342
"""Create a dictionary of output filepaths and their associated information."""
362343
import os
363344

364-
import pandas as pd
365-
import pkg_resources as p
366-
367345
if len(resource_list) == 0:
368346
err = "\n\n[!] No derivatives selected!\n\n"
369347
raise Exception(err)
370348

371349
if derivatives is None:
372-
keys_tsv = p.resource_filename("CPAC", "resources/cpac_outputs.tsv")
373-
try:
374-
keys = pd.read_csv(keys_tsv, delimiter="\t")
375-
except Exception as e:
376-
err = (
377-
"\n[!] Could not access or read the cpac_outputs.csv "
378-
f"resource file:\n{keys_tsv}\n\nError details {e}\n"
379-
)
380-
raise Exception(err)
350+
from CPAC.utils.outputs import group_derivatives
381351

382-
derivatives = list(keys[keys["Sub-Directory"] == "func"]["Resource"])
383-
derivatives = derivatives + list(
384-
keys[keys["Sub-Directory"] == "anat"]["Resource"]
385-
)
386-
387-
if pull_func:
388-
derivatives = derivatives + list(
389-
keys[keys["Space"] == "functional"]["Resource"]
390-
)
352+
derivatives = group_derivatives(pull_func)
391353

392354
# remove any extra /'s
393355
pipeline_output_folder = pipeline_output_folder.rstrip("/")
@@ -752,18 +714,10 @@ def prep_feat_inputs(group_config_file: str) -> dict:
752714
import os
753715

754716
import pandas as pd
755-
import pkg_resources as p
756717

757-
keys_tsv = p.resource_filename("CPAC", "resources/cpac_outputs.tsv")
758-
try:
759-
keys = pd.read_csv(keys_tsv, delimiter="\t")
760-
except Exception as e:
761-
err = (
762-
"\n[!] Could not access or read the cpac_outputs.tsv "
763-
f"resource file:\n{keys_tsv}\n\nError details {e}\n"
764-
)
765-
raise Exception(err)
718+
from CPAC.utils.outputs import Outputs
766719

720+
keys = Outputs.reference
767721
derivatives = list(
768722
keys[keys["Derivative"] == "yes"][keys["Space"] == "template"][
769723
keys["Values"] == "z-score"

CPAC/utils/create_fsl_flame_preset.py

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (C) 2018-2024 C-PAC Developers
1+
# Copyright (C) 2018-2025 C-PAC Developers
22

33
# This file is part of C-PAC.
44

@@ -1092,20 +1092,6 @@ def run(
10921092

10931093
import os
10941094

1095-
import pandas as pd
1096-
import pkg_resources as p
1097-
1098-
# make life easy
1099-
keys_csv = p.resource_filename("CPAC", "resources/cpac_outputs.csv")
1100-
try:
1101-
pd.read_csv(keys_csv)
1102-
except Exception as e:
1103-
err = (
1104-
"\n[!] Could not access or read the cpac_outputs.csv "
1105-
f"resource file:\n{keys_csv}\n\nError details {e}\n"
1106-
)
1107-
raise Exception(err)
1108-
11091095
if derivative_list == "all":
11101096
derivative_list = [
11111097
"alff",

CPAC/utils/outputs.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"""Specify the resources that C-PAC writes to the output direcotry."""
1818

1919
from importlib.resources import files
20+
from typing import ClassVar
2021

2122
import pandas as pd
2223

@@ -27,7 +28,9 @@ class Outputs:
2728
reference_csv = str(files("CPAC").joinpath("resources/cpac_outputs.tsv"))
2829

2930
try:
30-
reference = pd.read_csv(reference_csv, delimiter="\t", keep_default_na=False)
31+
reference: ClassVar[pd.DataFrame] = pd.read_csv(
32+
reference_csv, delimiter="\t", keep_default_na=False
33+
)
3134
except Exception as e:
3235
err = (
3336
"\n[!] Could not access or read the cpac_outputs.tsv "
@@ -47,8 +50,12 @@ class Outputs:
4750
reference[reference["4D Time Series"] == "Yes"]["Resource"]
4851
)
4952

50-
anat = list(reference[reference["Sub-Directory"] == "anat"]["Resource"])
51-
func = list(reference[reference["Sub-Directory"] == "func"]["Resource"])
53+
anat: ClassVar[list[str]] = list(
54+
reference[reference["Sub-Directory"] == "anat"]["Resource"]
55+
)
56+
func: ClassVar[list[str]] = list(
57+
reference[reference["Sub-Directory"] == "func"]["Resource"]
58+
)
5259

5360
# outputs to send into smoothing, if smoothing is enabled, and
5461
# outputs to write out if the user selects to write non-smoothed outputs
@@ -65,6 +72,8 @@ class Outputs:
6572
all_template_filter = _template_filter | _epitemplate_filter | _symtemplate_filter
6673
all_native_filter = _T1w_native_filter | _bold_native_filter | _long_native_filter
6774

75+
bold_native: ClassVar[list[str]] = list(reference[_bold_native_filter]["Resource"])
76+
6877
native_nonsmooth = list(
6978
reference[all_native_filter & _nonsmoothed_filter]["Resource"]
7079
)
@@ -121,3 +130,11 @@ def _is_gifti(_file_key):
121130
for gifti in giftis.itertuples()
122131
if " " in gifti.File
123132
}
133+
134+
135+
def group_derivatives(pull_func: bool = False) -> list[str]:
136+
"""Gather keys for anatomical and functional derivatives for group analysis."""
137+
derivatives: list[str] = Outputs.func + Outputs.anat
138+
if pull_func:
139+
derivatives = derivatives + Outputs.bold_native
140+
return derivatives

0 commit comments

Comments
 (0)