Skip to content

Commit eee5461

Browse files
committed
♻️ Fix cyclical imports
1 parent db27851 commit eee5461

File tree

15 files changed

+245
-205
lines changed

15 files changed

+245
-205
lines changed

CPAC/image_utils/__init__.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,22 @@
1-
from .spatial_smoothing import *
2-
from .statistical_transforms import *
1+
# Copyright (C) 2018-2022 C-PAC Developers
2+
3+
# This file is part of C-PAC.
4+
5+
# C-PAC is free software: you can redistribute it and/or modify it under
6+
# the terms of the GNU Lesser General Public License as published by the
7+
# Free Software Foundation, either version 3 of the License, or (at your
8+
# option) any later version.
9+
10+
# C-PAC is distributed in the hope that it will be useful, but WITHOUT
11+
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12+
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
13+
# License for more details.
14+
15+
# You should have received a copy of the GNU Lesser General Public
16+
# License along with C-PAC. If not, see <https://www.gnu.org/licenses/>.
17+
from .spatial_smoothing import set_gauss, spatial_smoothing
18+
from .statistical_transforms import calc_avg, fisher_z_score_standardize, \
19+
z_score_standardize
20+
21+
__all__ = ['calc_avg', 'fisher_z_score_standardize', 'set_gauss',
22+
'spatial_smoothing', 'z_score_standardize']

CPAC/image_utils/spatial_smoothing.py

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,22 @@
1-
import nipype.interfaces.fsl as fsl
1+
# Copyright (C) 2018-2022 C-PAC Developers
2+
3+
# This file is part of C-PAC.
4+
5+
# C-PAC is free software: you can redistribute it and/or modify it under
6+
# the terms of the GNU Lesser General Public License as published by the
7+
# Free Software Foundation, either version 3 of the License, or (at your
8+
# option) any later version.
9+
10+
# C-PAC is distributed in the hope that it will be useful, but WITHOUT
11+
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12+
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
13+
# License for more details.
14+
15+
# You should have received a copy of the GNU Lesser General Public
16+
# License along with C-PAC. If not, see <https://www.gnu.org/licenses/>.
17+
from nipype.interfaces import fsl, utility as util
218
from nipype.interfaces.afni import preprocess as afni
319
from CPAC.pipeline import nipype_pipeline_engine as pe
4-
import nipype.interfaces.utility as util
5-
from CPAC.utils import Outputs
620

721

822
def set_gauss(fwhm):
@@ -64,17 +78,17 @@ def spatial_smoothing(wf_name, fwhm, input_image_type='func_derivative',
6478

6579
elif opt == 'AFNI':
6680
if input_image_type == 'func_derivative_multi':
67-
output_smooth = pe.MapNode(interface= afni.BlurToFWHM(),
81+
output_smooth = pe.MapNode(interface=afni.BlurToFWHM(),
6882
name='smooth_multi',
6983
iterfield=['in_file'])
7084
else:
71-
output_smooth = pe.Node(interface= afni.BlurToFWHM(),
85+
output_smooth = pe.Node(interface=afni.BlurToFWHM(),
7286
name='smooth',
7387
iterfield=['in_file'])
7488
output_smooth.inputs.outputtype = 'NIFTI_GZ'
7589

76-
if opt =='FSL':
77-
# wire in the resource to be smoothed
90+
if opt == 'FSL':
91+
# wire in the resource to be smoothed
7892
wf.connect(inputnode, 'in_file', output_smooth, 'in_file')
7993
# get the parameters for fwhm
8094
wf.connect(inputnode_fwhm, ('fwhm', set_gauss),

CPAC/image_utils/statistical_transforms.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,22 @@
1-
from CPAC.pipeline import nipype_pipeline_engine as pe
2-
import nipype.interfaces.utility as util
3-
from nipype.interfaces.afni import preprocess
1+
# Copyright (C) 2018-2022 C-PAC Developers
2+
3+
# This file is part of C-PAC.
4+
5+
# C-PAC is free software: you can redistribute it and/or modify it under
6+
# the terms of the GNU Lesser General Public License as published by the
7+
# Free Software Foundation, either version 3 of the License, or (at your
8+
# option) any later version.
49

10+
# C-PAC is distributed in the hope that it will be useful, but WITHOUT
11+
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12+
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
13+
# License for more details.
14+
15+
# You should have received a copy of the GNU Lesser General Public
16+
# License along with C-PAC. If not, see <https://www.gnu.org/licenses/>.
17+
from nipype.interfaces import utility as util
18+
from nipype.interfaces.afni import preprocess
19+
from CPAC.pipeline import nipype_pipeline_engine as pe
520
from CPAC.utils import function
621
from CPAC.utils.utils import (
722
extract_output_mean,

CPAC/longitudinal_pipeline/longitudinal_workflow.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,12 @@
6060
subject_specific_template
6161
)
6262

63-
from CPAC.utils import Strategy, find_files, function, Outputs
64-
from CPAC.utils.utils import check_prov_for_regtool
65-
63+
from CPAC.utils import find_files, function
64+
from CPAC.utils.outputs import Outputs
65+
from CPAC.utils.strategy import Strategy
6666
from CPAC.utils.utils import (
6767
check_config_resources,
68-
check_system_deps,
69-
get_scan_params,
70-
get_tr
68+
check_prov_for_regtool
7169
)
7270

7371
logger = logging.getLogger('nipype.workflow')

CPAC/nuisance/nuisance.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
from nipype.interfaces import afni
1515
from nipype.interfaces.afni import utils as afni_utils
1616
from scipy.fftpack import fft, ifft
17-
from CPAC import utils
1817
from CPAC.utils.interfaces.function import Function
1918
from CPAC.utils.interfaces.masktool import MaskTool
2019
from CPAC.utils.interfaces.pc import PC

CPAC/pipeline/cpac_pipeline.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,6 @@
197197
)
198198

199199
from CPAC.pipeline.random_state import set_up_random_state_logger
200-
from CPAC.utils.datasource import bidsier_prefix, gather_extraction_maps
201200
from CPAC.pipeline.schema import valid_options
202201
from CPAC.utils.trimmer import the_trimmer
203202
from CPAC.utils import Configuration
@@ -248,6 +247,8 @@ def run_workflow(sub_dict, c, run, pipeline_timing_info=None, p_name=None,
248247
the prepared nipype workflow object containing the parameters
249248
specified in the config
250249
'''
250+
from CPAC.utils.datasource import bidsier_prefix
251+
251252
if plugin is not None and not isinstance(plugin, str):
252253
raise TypeError(
253254
'CPAC.pipeline.cpac_pipeline.run_workflow requires a '
@@ -1089,6 +1090,7 @@ def connect_pipeline(wf, cfg, rpool, pipeline_blocks):
10891090

10901091
def build_workflow(subject_id, sub_dict, cfg, pipeline_name=None,
10911092
num_ants_cores=1):
1093+
from CPAC.utils.datasource import gather_extraction_maps
10921094

10931095
# Workflow setup
10941096
wf = initialize_nipype_wf(cfg, sub_dict)

CPAC/pipeline/engine.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
fisher_z_score_standardize
3232
from CPAC.pipeline.check_outputs import ExpectedOutputs
3333
from CPAC.registration.registration import transform_derivative
34-
from CPAC.utils import Outputs
34+
from CPAC.utils.outputs import Outputs
3535
from CPAC.utils.datasource import (
3636
create_anat_datasource,
3737
create_func_datasource,

CPAC/pipeline/schema.py

Lines changed: 1 addition & 162 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@
2626
Match, Maybe, Optional, Range, Required, Schema
2727
from CPAC import docs_prefix
2828
from CPAC.utils.datatypes import ListFromItem
29-
from CPAC.utils.utils import delete_nested_value, lookup_nested_value, \
30-
set_nested_value
3129

3230
# 1 or more digits, optional decimal, 'e', optional '-', 1 or more digits
3331
scientific_notation_str_regex = r'^([0-9]+(\.[0-9]*)*(e)-{0,1}[0-9]+)*$'
@@ -204,166 +202,6 @@ def permutation_message(key, options):
204202
'''
205203

206204

207-
def _combine_labels(config_dict, list_to_combine, new_key):
208-
'''
209-
Helper function to combine formerly separate keys into a
210-
combined key.
211-
212-
Parameters
213-
----------
214-
config_dict: dict
215-
216-
key_sequence: iterable of lists or tuples
217-
218-
new_key: list or tuple
219-
220-
Returns
221-
-------
222-
updated_config_dict: dict
223-
'''
224-
new_value = []
225-
any_old_values = False
226-
for _to_combine in list_to_combine:
227-
try:
228-
old_value = lookup_nested_value(config_dict, _to_combine)
229-
except KeyError:
230-
old_value = None
231-
if old_value is not None:
232-
any_old_values = True
233-
if isinstance(old_value, (list, set, tuple)):
234-
for value in old_value:
235-
new_value.append(value)
236-
else:
237-
new_value.append(old_value)
238-
config_dict = delete_nested_value(config_dict, _to_combine)
239-
if any_old_values:
240-
return set_nested_value(config_dict, new_key, new_value)
241-
return config_dict
242-
243-
244-
def _now_runswitch(config_dict, key_sequence):
245-
'''
246-
Helper function to convert a formerly forkable value to a
247-
runswitch.
248-
249-
Parameters
250-
----------
251-
config_dict: dict
252-
253-
key_sequence: list or tuple
254-
255-
Returns
256-
-------
257-
updated_config_dict: dict
258-
'''
259-
try:
260-
old_forkable = lookup_nested_value(config_dict, key_sequence)
261-
except KeyError:
262-
return config_dict
263-
if isinstance(old_forkable, bool) or isinstance(old_forkable, list):
264-
return set_nested_value(
265-
config_dict, key_sequence, {'run': old_forkable})
266-
return config_dict
267-
268-
269-
def _changes_1_8_0_to_1_8_1(config_dict):
270-
'''
271-
Examples
272-
--------
273-
Starting with 1.8.0
274-
>>> zero = {'anatomical_preproc': {
275-
... 'non_local_means_filtering': True,
276-
... 'n4_bias_field_correction': True
277-
... }, 'functional_preproc': {
278-
... 'motion_estimates_and_correction': {
279-
... 'calculate_motion_first': False
280-
... }
281-
... }, 'segmentation': {
282-
... 'tissue_segmentation': {
283-
... 'ANTs_Prior_Based': {
284-
... 'CSF_label': 0,
285-
... 'left_GM_label': 1,
286-
... 'right_GM_label': 2,
287-
... 'left_WM_label': 3,
288-
... 'right_WM_label': 4}}}}
289-
>>> updated_apb = _changes_1_8_0_to_1_8_1(zero)[
290-
... 'segmentation']['tissue_segmentation']['ANTs_Prior_Based']
291-
>>> updated_apb['CSF_label']
292-
[0]
293-
>>> updated_apb['GM_label']
294-
[1, 2]
295-
>>> updated_apb['WM_label']
296-
[3, 4]
297-
298-
Starting with 1.8.1
299-
>>> one = {'anatomical_preproc': {
300-
... 'non_local_means_filtering': True,
301-
... 'n4_bias_field_correction': True
302-
... }, 'functional_preproc': {
303-
... 'motion_estimates_and_correction': {
304-
... 'calculate_motion_first': False
305-
... }
306-
... }, 'segmentation': {
307-
... 'tissue_segmentation': {
308-
... 'ANTs_Prior_Based': {
309-
... 'CSF_label': [0],
310-
... 'GM_label': [1, 2],
311-
... 'WM_label': [3, 4]}}}}
312-
>>> updated_apb = _changes_1_8_0_to_1_8_1(one)[
313-
... 'segmentation']['tissue_segmentation']['ANTs_Prior_Based']
314-
>>> updated_apb['CSF_label']
315-
[0]
316-
>>> updated_apb['GM_label']
317-
[1, 2]
318-
>>> updated_apb['WM_label']
319-
[3, 4]
320-
'''
321-
for key_sequence in {
322-
('anatomical_preproc', 'non_local_means_filtering'),
323-
('anatomical_preproc', 'n4_bias_field_correction')
324-
}:
325-
config_dict = _now_runswitch(config_dict, key_sequence)
326-
for combiners in {
327-
((
328-
('segmentation', 'tissue_segmentation', 'ANTs_Prior_Based',
329-
'CSF_label'),
330-
), ('segmentation', 'tissue_segmentation', 'ANTs_Prior_Based',
331-
'CSF_label')),
332-
((
333-
('segmentation', 'tissue_segmentation', 'ANTs_Prior_Based',
334-
'left_GM_label'),
335-
('segmentation', 'tissue_segmentation', 'ANTs_Prior_Based',
336-
'right_GM_label')
337-
), ('segmentation', 'tissue_segmentation', 'ANTs_Prior_Based',
338-
'GM_label')),
339-
((
340-
('segmentation', 'tissue_segmentation', 'ANTs_Prior_Based',
341-
'left_WM_label'),
342-
('segmentation', 'tissue_segmentation', 'ANTs_Prior_Based',
343-
'right_WM_label')
344-
), ('segmentation', 'tissue_segmentation', 'ANTs_Prior_Based',
345-
'WM_label'))
346-
}:
347-
config_dict = _combine_labels(config_dict, *combiners)
348-
try:
349-
calculate_motion_first = lookup_nested_value(
350-
config_dict,
351-
['functional_preproc', 'motion_estimates_and_correction',
352-
'calculate_motion_first']
353-
)
354-
except KeyError:
355-
calculate_motion_first = None
356-
if calculate_motion_first is not None:
357-
del config_dict['functional_preproc'][
358-
'motion_estimates_and_correction']['calculate_motion_first']
359-
config_dict = set_nested_value(config_dict, [
360-
'functional_preproc', 'motion_estimates_and_correction',
361-
'motion_estimates', 'calculate_motion_first'
362-
], calculate_motion_first)
363-
364-
return config_dict
365-
366-
367205
def sanitize(filename):
368206
'''Sanitize a filename and replace whitespaces with underscores'''
369207
return re.sub(r'\s+', '_', sanitize_filename(filename))
@@ -1111,6 +949,7 @@ def schema(config_dict):
1111949
-------
1112950
dict
1113951
'''
952+
from CPAC.utils.utils import _changes_1_8_0_to_1_8_1
1114953
partially_validated = latest_schema(_changes_1_8_0_to_1_8_1(config_dict))
1115954
try:
1116955
if (partially_validated['registration_workflows'][

CPAC/qc/tests/test_qc.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
import os
22
import pytest
3-
3+
from nipype.interfaces import utility as util
44
from CPAC.pipeline import nipype_pipeline_engine as pe
5-
import nipype.interfaces.utility as util
6-
5+
from CPAC.pipeline.cpac_group_runner import gather_outputs
76
from CPAC.qc.pipeline import create_qc_workflow
87
from CPAC.qc.utils import generate_qc_pages
9-
from CPAC.utils import Configuration, Strategy, Outputs
10-
11-
from CPAC.pipeline.cpac_group_runner import gather_outputs
8+
from CPAC.utils.configuration import Configuration
9+
from CPAC.utils.outputs import Outputs
10+
from CPAC.utils.strategy import Strategy
1211

1312

1413
def file_node(path):

CPAC/randomise/randomise.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11

22
from CPAC.pipeline import nipype_pipeline_engine as pe
3-
from CPAC.pipeline.cpac_group_runner import load_config_yml
43

54

65
def select(input_list):
@@ -122,6 +121,7 @@ def run(group_config_path):
122121
import sys
123122
import pickle
124123
import yaml
124+
from CPAC.pipeline.cpac_group_runner import load_config_yml
125125

126126
group_config_obj = load_config_yml(group_config_path)
127127
pipeline_output_folder = group_config_obj.pipeline_dir

0 commit comments

Comments
 (0)