Skip to content

Commit 09853bd

Browse files
committed
♻️ DRY up output key collection for group config
1 parent 2ffff67 commit 09853bd

File tree

3 files changed

+25
-70
lines changed

3 files changed

+25
-70
lines changed

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: 17 additions & 2 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

@@ -47,8 +48,12 @@ class Outputs:
4748
reference[reference["4D Time Series"] == "Yes"]["Resource"]
4849
)
4950

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

5358
# outputs to send into smoothing, if smoothing is enabled, and
5459
# outputs to write out if the user selects to write non-smoothed outputs
@@ -65,6 +70,8 @@ class Outputs:
6570
all_template_filter = _template_filter | _epitemplate_filter | _symtemplate_filter
6671
all_native_filter = _T1w_native_filter | _bold_native_filter | _long_native_filter
6772

73+
bold_native: ClassVar[list[str]] = list(reference[_bold_native_filter]["Resource"])
74+
6875
native_nonsmooth = list(
6976
reference[all_native_filter & _nonsmoothed_filter]["Resource"]
7077
)
@@ -121,3 +128,11 @@ def _is_gifti(_file_key):
121128
for gifti in giftis.itertuples()
122129
if " " in gifti.File
123130
}
131+
132+
133+
def group_derivatives(pull_func: bool = False) -> list[str]:
134+
"""Gather keys for anatomical and functional derivatives for group analysis."""
135+
derivatives: list[str] = Outputs.func + Outputs.anat
136+
if pull_func:
137+
derivatives = derivatives + Outputs.bold_native
138+
return derivatives

0 commit comments

Comments
 (0)