Beispiel #1
0
def test_conf_fail1(baseconf, tmp_path, key):
    del baseconf[key]
    with open(tmp_path / 'conf.json', 'w+') as f:
        json.dump(baseconf, f)
        f.seek(0)
        with pytest.raises(jsonschema.ValidationError):
            conf.load_config(f)
Beispiel #2
0
def test_conf1(tmp_path, input_files_conf1, conffilename, usecli):
    infull = _make_files(tmp_path, input_files_conf1)
    conffile = Path(__file__).parent / conffilename
    if usecli:
        out = cliwrapper([tmp_path], conffile, tmp_path)
    else:
        out = get_files.get_files([tmp_path], conf.load_config(conffile))
    compare(out, infull)
    out = get_files.get_files([tmp_path],
                              conf.load_config(conffile),
                              exclude_patterns=['/sub-1/'])
    for k in list(infull.keys()):
        if k[0] == '1':
            del infull[k]
    compare(out, infull)
Beispiel #3
0
def test_derived(tmp_path):
    aT1w = np.arange(10 * 11 * 12).reshape(10, 11, 12)
    amask = np.zeros_like(aT1w)
    amask[5:8, 2:9, 3:4] = 1.0
    bT1w = np.arange(10 * 11 * 13).reshape(10, 11, 13)
    bmask = np.zeros_like(bT1w)
    bmask[5:8, 2:9, 3:10] = 1.0
    aff = np.eye(4)
    aT1wfile = tmp_path / 'a_T1w.nii'
    amaskfile = tmp_path / 'a_mask.nii'
    bT1wfile = tmp_path / 'b_T1w.nii'
    bmaskfile = tmp_path / 'b_mask.nii'
    nibabel.Nifti1Image(aT1w, aff).to_filename(str(aT1wfile))
    nibabel.Nifti1Image(amask, aff).to_filename(str(amaskfile))
    nibabel.Nifti1Image(bT1w, aff).to_filename(str(bT1wfile))
    nibabel.Nifti1Image(bmask, aff).to_filename(str(bmaskfile))
    conf = configure.load_config(Path(__file__).parent / 'testconfderived.json')
    filesdict = get_files.get_files([tmp_path], conf)
    assert filesdict[('a',)]['T1w'] == aT1wfile
    assert filesdict[('a',)]['mask'] == amaskfile
    assert filesdict[('b',)]['T1w'] == bT1wfile
    assert filesdict[('b',)]['mask'] == bmaskfile
    derived.procderived(conf, filesdict)
    assert filesdict[('a',)]['T1w'] == aT1wfile
    assert filesdict[('a',)]['mask'] == amaskfile
    assert filesdict[('b',)]['T1w'] == bT1wfile
    assert filesdict[('b',)]['mask'] == bmaskfile
    aoutmask = nibabel.load(filesdict[('a',)]['T1wmask']).get_fdata()
    assert np.all(aoutmask == aT1w * amask)
    boutmask = nibabel.load(filesdict[('b',)]['T1wmask']).get_fdata()
    assert np.all(boutmask == bT1w * bmask)
    derived.removederived(conf, filesdict)
    assert not os.path.exists(filesdict[('a',)]['T1wmask'])
    assert not os.path.exists(filesdict[('b',)]['T1wmask'])
Beispiel #4
0
def test_conf1_multfiles2(tmp_path, input_files_conf1):
    input_files_conf1[('1', None, None, None, None)]['anotherfile'] = Path(
        f'sub-1/anat/sub-1_skullstripped-true_desc-nucor_features.nii')
    _make_files(tmp_path, input_files_conf1)
    conffile = Path(__file__).parent / 'testconf1bids.json'
    with pytest.raises(get_files.MultipleFilesFoundError):
        get_files.get_files([tmp_path], conf.load_config(conffile))
Beispiel #5
0
def test_conf1_multfiles(tmp_path, input_files_conf1, conffilename):
    input_files_conf1[('1', None, None, None, None)]['nu_bet2'] = Path(
        f'sub-1/anat/sub-1_skullstripped-false_desc-nucor_T1w.nii')
    _make_files(tmp_path, input_files_conf1)
    conffile = Path(__file__).parent / conffilename
    with pytest.raises(get_files.MultipleFilesFoundError):
        get_files.get_files([tmp_path], conf.load_config(conffile))
Beispiel #6
0
def test_conf2(tmp_path, input_files_conf2, usecli):
    infull = _make_files(tmp_path, input_files_conf2)
    conffile = Path(__file__).parent / 'testconf2.json'
    if usecli:
        out = cliwrapper([tmp_path], conffile, tmp_path)
    else:
        out = get_files.get_files([tmp_path], conf.load_config(conffile))
    compare(out, infull)
Beispiel #7
0
def test_all_wf(tmp_path, input_files_conf1, filter_sub1):
    infull = _make_files(tmp_path, input_files_conf1)
    conffile = Path(__file__).parent / 'testconf1.json'
    conf = load_config(conffile)
    out = get_files.get_files([tmp_path], conf)
    if filter_sub1:
        filter_dict = {'sub': ['1']}
    else:
        filter_dict = None
    wf = workflows.all_workflow(out,
                                tmp_path,
                                conf,
                                filter_keys_dict=filter_dict)

    defaultnslices = 7

    truth = {
        'report.page_sub-1_acq-10_rec-11_run-12.compare1': {
            'name1': 'Non-uniformity corrected',
            'image1': str(infull[('1', '10', '11', '12', None)]['nu_bet']),
            'name2': 'Inormalized',
            'image2': str(infull[('1', '10', '11', '12', None)]['normalized']),
            'nslices': defaultnslices,
            'relative_dir': str(tmp_path.resolve() / 'sub-1'),
            'slice_to_image2': False,
            'max_intensity_fraction_image1': 0.99,
            'max_intensity_fraction_image2': 0.99,
            'affine_absolute_tolerance': 1e-3,
            'affine_relative_tolerance': 1e-5,
            'description': '',
        },
        'report.page_sub-1_acq-10_rec-11_run-12.compare2': {
            'name1': 'Non-uniformity corrected',
            'image1': str(infull[('1', '10', '11', '12', None)]['nu_bet']),
            'name2': 'Inormalized',
            'image2': str(infull[('1', '10', '11', '12', None)]['normalized']),
            'nslices': 3,
            'relative_dir': str(tmp_path.resolve() / 'sub-1'),
            'slice_to_image2': False,
            'max_intensity_fraction_image1': 0.91,
            'max_intensity_fraction_image2': 0.92,
            'affine_absolute_tolerance': 1e-3,
            'description': '',
            'affine_relative_tolerance': 1e-5
        },
        'report.page_sub-1_acq-10_rec-11_run-12.compare3': {
            'name1': 'Non-uniformity corrected',
            'image1': str(infull[('1', '10', '11', '12', None)]['nu_bet']),
            'name2': 'Inormalized',
            'image2': str(infull[('1', '10', '11', '12', None)]['normalized']),
            'nslices': defaultnslices,
            'relative_dir': str(tmp_path.resolve() / 'sub-1'),
            'slice_to_image2': True,
            'max_intensity_fraction_image1': 0.99,
            'max_intensity_fraction_image2': 0.99,
            'affine_absolute_tolerance': 1e-3,
            'description': '',
            'affine_relative_tolerance': 1e-5
        },
        'report.page_sub-1_acq-10_rec-11_run-12.single4': {
            'name': 'Non-uniformity corrected',
            'image': str(infull[('1', '10', '11', '12', None)]['nu_bet']),
            'nslices': defaultnslices,
            'relative_dir': str(tmp_path.resolve() / 'sub-1'),
            'max_intensity_fraction': 0.99,
            'affine_absolute_tolerance': 1e-3,
            'description': '',
            'affine_relative_tolerance': 1e-5
        },
        'report.page_sub-1_acq-10_rec-11_run-12.single5': {
            'name': 'Non-uniformity corrected',
            'image': str(infull[('1', '10', '11', '12', None)]['nu_bet']),
            'nslices': 3,
            'relative_dir': str(tmp_path.resolve() / 'sub-1'),
            'max_intensity_fraction': 0.99,
            'affine_absolute_tolerance': 1e-3,
            'description': '',
            'affine_relative_tolerance': 1e-5
        },
        'report.page_sub-1_acq-10_rec-11_run-12.single6': {
            'name': 'Non-uniformity corrected',
            'image': str(infull[('1', '10', '11', '12', None)]['nu_bet']),
            'nslices': defaultnslices,
            'relative_dir': str(tmp_path.resolve() / 'sub-1'),
            'max_intensity_fraction': 0.99,
            'affine_absolute_tolerance': 1e-3,
            'description': '',
            'affine_relative_tolerance': 1e-5
        },
        'report.page_sub-1_acq-10_rec-11_run-12.contour7': {
            'name': 'Non-uniformity corrected',
            'image': str(infull[('1', '10', '11', '12', None)]['nu_bet']),
            'labelimage': str(infull[('1', '10', '11', '12',
                                      None)]['transformed_atlas']),
            'nslices': defaultnslices,
            'relative_dir': str(tmp_path.resolve() / 'sub-1'),
            'contour_width': 5,
            'slice_to_label': True,
            'max_intensity_fraction': 0.99,
            'affine_absolute_tolerance': 1e-2,
            'description': '',
            'affine_relative_tolerance': 1e-5,
            'contour_levels': None,
            'threshold_above_zero': False
        },
        'report.page_sub-1_acq-10_rec-11_run-12.contour8': {
            'name': 'Non-uniformity corrected',
            'image': str(infull[('1', '10', '11', '12', None)]['nu_bet']),
            'labelimage': str(infull[('1', '10', '11', '12',
                                      None)]['transformed_atlas']),
            'nslices': 3,
            'relative_dir': str(tmp_path.resolve() / 'sub-1'),
            'contour_width': 5,
            'slice_to_label': False,
            'max_intensity_fraction': 0.99,
            'affine_absolute_tolerance': 1e-3,
            'description': '',
            'affine_relative_tolerance': 1e-3,
            'contour_levels': [1e-3],
            'threshold_above_zero': False
        },
        'report.page_sub-1_acq-10_rec-11_run-12.contour9': {
            'name': 'Non-uniformity corrected',
            'image': str(infull[('1', '10', '11', '12', None)]['nu_bet']),
            'labelimage': str(infull[('1', '10', '11', '12',
                                      None)]['transformed_atlas']),
            'nslices': defaultnslices,
            'relative_dir': str(tmp_path.resolve() / 'sub-1'),
            'contour_width': 2.2,
            'slice_to_label': False,
            'max_intensity_fraction': 0.95,
            'affine_absolute_tolerance': 1e-3,
            'description': '',
            'affine_relative_tolerance': 1e-5,
            'contour_levels': None,
            'threshold_above_zero': False
        },
        'report.page_sub-1_acq-10_rec-11_run-12.distributions10': {
            'name': 'features',
            'distsfile': str(infull[('1', '10', '11', '12',
                                     None)]['features']),
            'labelfile': str(infull[('1', '10', '11', '12',
                                     None)]['features_label']),
            'description': '',
            'relative_dir': str(tmp_path.resolve() / 'sub-1')
        },
        'report.page_sub-1_acq-10_rec-11_run-12.distributions11': {
            'name': 'features',
            'distsfile': str(infull[('1', '10', '11', '12',
                                     None)]['features']),
            'labelfile': str(infull[('1', '10', '11', '12',
                                     None)]['features_label']),
            'description': '',
            'relative_dir': str(tmp_path.resolve() / 'sub-1')
        },
        'report.page_sub-1_acq-10_rec-11_run-12.crash12': {
            'name': 'error',
            'crashfiles': [
                str(infull[('1', '10', '11', '12', None)]['crashfiles'])
            ],
            'relative_dir': str(tmp_path.resolve() / 'sub-1')
        },
        'report.page_sub-1_acq-10_rec-11_run-12.rating13': {
            'name': 'Rating',
            'widgets': [
                {
                 'name': 'Overall',
                 'type': 'radio',
                 'options': [{
                     "name": "Reject", "value": 1
                 }, {
                     "name": "Poor", "value": 2
                 }, {
                     "name": "Acceptable", "value": 3
                 }, {
                     "name": "Good", "value": 4
                 }, {
                     "name": "Great", "value": 5
                 }]
                },
                {
                    'name': 'Notes',
                    'type': 'checkbox',
                    'fields': ["Non-uniformity failed", "Registration failed"]
                },
                {
                    'name': 'Other',
                    'type': 'text'
                }
            ]
        },
        'report.page_sub-1_acq-10_rec-11_run-12.probmap14': {
            'name': 'Non-uniformity corrected',
            'image': str(infull[('1', '10', '11', '12', None)]['nu_bet']),
            'probmapimage': str(infull[('1', '10', '11', '12',
                                        None)]['transformed_atlas']),
            'nslices': defaultnslices,
            'relative_dir': str(tmp_path.resolve() / 'sub-1'),
            'slice_to_probmap': False,
            'max_intensity_fraction': 0.95,
            'affine_absolute_tolerance': 1e-3,
            'description': '',
            'affine_relative_tolerance': 1e-5
        },
        'report.page_sub-1_acq-10_rec-11_run-12.overlay15': {
            'name': 'Non-uniformity corrected',
            'image': str(infull[('1', '10', '11', '12', None)]['nu_bet']),
            'labelimage': str(infull[('1', '10', '11', '12',
                                      None)]['transformed_atlas']),
            'nslices': defaultnslices,
            'relative_dir': str(tmp_path.resolve() / 'sub-1'),
            'slice_to_label': False,
            'max_intensity_fraction': 0.95,
            'affine_absolute_tolerance': 1e-3,
            'description': '',
            'affine_relative_tolerance': 1e-5,
            'transparency': 0.5
        },
        'report.page_sub-2_acq-10_rec-11_run-12.compare1': {
            'name1': 'Non-uniformity corrected',
            'image1': str(infull[('2', '10', '11', '12', None)]['nu_bet']),
            'name2': 'Inormalized',
            'image2': str(infull[('2', '10', '11', '12', None)]['normalized']),
            'nslices': defaultnslices,
            'relative_dir': str(tmp_path.resolve() / 'sub-2'),
            'slice_to_image2': False,
            'max_intensity_fraction_image1': 0.99,
            'max_intensity_fraction_image2': 0.99,
            'affine_absolute_tolerance': 1e-3,
            'description': '',
            'affine_relative_tolerance': 1e-5
        },
        'report.page_sub-2_acq-10_rec-11_run-12.compare2': {
            'name1': 'Non-uniformity corrected',
            'image1': str(infull[('2', '10', '11', '12', None)]['nu_bet']),
            'name2': 'Inormalized',
            'image2': str(infull[('2', '10', '11', '12', None)]['normalized']),
            'nslices': 3,
            'relative_dir': str(tmp_path.resolve() / 'sub-2'),
            'slice_to_image2': False,
            'max_intensity_fraction_image1': 0.91,
            'max_intensity_fraction_image2': 0.92,
            'affine_absolute_tolerance': 1e-3,
            'description': '',
            'affine_relative_tolerance': 1e-5
        },
        'report.page_sub-2_acq-10_rec-11_run-12.compare3': {
            'name1': 'Non-uniformity corrected',
            'image1': str(infull[('2', '10', '11', '12', None)]['nu_bet']),
            'name2': 'Inormalized',
            'image2': str(infull[('2', '10', '11', '12', None)]['normalized']),
            'nslices': defaultnslices,
            'relative_dir': str(tmp_path.resolve() / 'sub-2'),
            'slice_to_image2': True,
            'max_intensity_fraction_image1': 0.99,
            'max_intensity_fraction_image2': 0.99,
            'affine_absolute_tolerance': 1e-3,
            'description': '',
            'affine_relative_tolerance': 1e-5
        },
        'report.page_sub-2_acq-10_rec-11_run-12.single4': {
            'name': 'Non-uniformity corrected',
            'image': str(infull[('2', '10', '11', '12', None)]['nu_bet']),
            'nslices': defaultnslices,
            'relative_dir': str(tmp_path.resolve() / 'sub-2'),
            'max_intensity_fraction': 0.99,
            'affine_absolute_tolerance': 1e-3,
            'description': '',
            'affine_relative_tolerance': 1e-5
        },
        'report.page_sub-2_acq-10_rec-11_run-12.single5': {
            'name': 'Non-uniformity corrected',
            'image': str(infull[('2', '10', '11', '12', None)]['nu_bet']),
            'nslices': 3,
            'relative_dir': str(tmp_path.resolve() / 'sub-2'),
            'max_intensity_fraction': 0.99,
            'affine_absolute_tolerance': 1e-3,
            'description': '',
            'affine_relative_tolerance': 1e-5
        },
        'report.page_sub-2_acq-10_rec-11_run-12.single6': {
            'name': 'Non-uniformity corrected',
            'image': str(infull[('2', '10', '11', '12', None)]['nu_bet']),
            'nslices': defaultnslices,
            'relative_dir': str(tmp_path.resolve() / 'sub-2'),
            'max_intensity_fraction': 0.99,
            'affine_absolute_tolerance': 1e-3,
            'description': '',
            'affine_relative_tolerance': 1e-5
        },
        'report.page_sub-2_acq-10_rec-11_run-12.contour7': {
            'name': 'Non-uniformity corrected',
            'image': str(infull[('2', '10', '11', '12', None)]['nu_bet']),
            'labelimage': str(infull[('2', '10', '11', '12',
                                      None)]['transformed_atlas']),
            'nslices': defaultnslices,
            'relative_dir': str(tmp_path.resolve() / 'sub-2'),
            'contour_width': 5,
            'slice_to_label': True,
            'max_intensity_fraction': 0.99,
            'affine_absolute_tolerance': 1e-2,
            'description': '',
            'affine_relative_tolerance': 1e-5,
            'contour_levels': None,
            'threshold_above_zero': False
        },
        'report.page_sub-2_acq-10_rec-11_run-12.contour8': {
            'name': 'Non-uniformity corrected',
            'image': str(infull[('2', '10', '11', '12', None)]['nu_bet']),
            'labelimage': str(infull[('2', '10', '11', '12',
                                      None)]['transformed_atlas']),
            'nslices': 3,
            'relative_dir': str(tmp_path.resolve() / 'sub-2'),
            'contour_width': 5,
            'slice_to_label': False,
            'max_intensity_fraction': 0.99,
            'affine_absolute_tolerance': 1e-3,
            'description': '',
            'affine_relative_tolerance': 1e-3,
            'contour_levels': [1e-3],
            'threshold_above_zero': False
        },
        'report.page_sub-2_acq-10_rec-11_run-12.contour9': {
            'name': 'Non-uniformity corrected',
            'image': str(infull[('2', '10', '11', '12', None)]['nu_bet']),
            'labelimage': str(infull[('2', '10', '11', '12',
                                      None)]['transformed_atlas']),
            'nslices': defaultnslices,
            'relative_dir': str(tmp_path.resolve() / 'sub-2'),
            'contour_width': 2.2,
            'slice_to_label': False,
            'max_intensity_fraction': 0.95,
            'affine_absolute_tolerance': 1e-3,
            'description': '',
            'affine_relative_tolerance': 1e-5,
            'contour_levels': None,
            'threshold_above_zero': False
        },
        'report.page_sub-2_acq-10_rec-11_run-12.distributions10': {
            'name': 'features',
            'distsfile': str(infull[('2', '10', '11', '12',
                                     None)]['features']),
            'labelfile': str(infull[('2', '10', '11', '12',
                                     None)]['features_label']),
            'description': '',
            'relative_dir': str(tmp_path.resolve() / 'sub-2')
        },
        'report.page_sub-2_acq-10_rec-11_run-12.distributions11': {
            'name': 'features',
            'distsfile': str(infull[('2', '10', '11', '12',
                                     None)]['features']),
            'labelfile': str(infull[('2', '10', '11', '12',
                                     None)]['features_label']),
            'description': '',
            'relative_dir': str(tmp_path.resolve() / 'sub-2')
        },
        'report.page_sub-2_acq-10_rec-11_run-12.crash12': {
            'name': 'error',
            'crashfiles': [
                str(infull[('2', '10', '11', '12', None)]['crashfiles'])
            ],
            'relative_dir': str(tmp_path.resolve() / 'sub-2')
        },
        'report.page_sub-2_acq-10_rec-11_run-12.rating13': {
            'name': 'Rating',
            'widgets': [
                {
                 'name': 'Overall',
                 'type': 'radio',
                 'options': [{
                     "name": "Reject", "value": 1
                 }, {
                     "name": "Poor", "value": 2
                 }, {
                     "name": "Acceptable", "value": 3
                 }, {
                     "name": "Good", "value": 4
                 }, {
                     "name": "Great", "value": 5
                 }]
                },
                {
                    'name': 'Notes',
                    'type': 'checkbox',
                    'fields': ["Non-uniformity failed", "Registration failed"]
                },
                {
                    'name': 'Other',
                    'type': 'text'
                }
            ]
        },
        'report.page_sub-2_acq-10_rec-11_run-12.probmap14': {
            'name': 'Non-uniformity corrected',
            'image': str(infull[('2', '10', '11', '12', None)]['nu_bet']),
            'probmapimage': str(infull[('2', '10', '11', '12',
                                        None)]['transformed_atlas']),
            'nslices': defaultnslices,
            'relative_dir': str(tmp_path.resolve() / 'sub-2'),
            'slice_to_probmap': False,
            'max_intensity_fraction': 0.95,
            'affine_absolute_tolerance': 1e-3,
            'description': '',
            'affine_relative_tolerance': 1e-5
        },
        'report.page_sub-2_acq-10_rec-11_run-12.overlay15': {
            'name': 'Non-uniformity corrected',
            'image': str(infull[('2', '10', '11', '12', None)]['nu_bet']),
            'labelimage': str(infull[('2', '10', '11', '12',
                                      None)]['transformed_atlas']),
            'nslices': defaultnslices,
            'relative_dir': str(tmp_path.resolve() / 'sub-2'),
            'slice_to_label': False,
            'max_intensity_fraction': 0.95,
            'affine_absolute_tolerance': 1e-3,
            'description': '',
            'affine_relative_tolerance': 1e-5,
            'transparency': 0.5
        },
    }

    settings = {'image_width': 2, 'image_height': 1.75}

    class Callable(object):

        def __init__(self):
            self.nodes_seen = set()
            self.nodes_checked = set()

        def __call__(self, node, graph):
            self.nodes_seen.add(node.fullname)
            if node.fullname in truth.keys():
                self.nodes_checked.add(node.fullname)
                node_traits = set(node.inputs.visible_traits())
                for k in set(truth[node.fullname].keys()):
                    assert getattr(node.inputs, k) == truth[node.fullname][k]
                for k in node_traits - set(truth[node.fullname].keys()):
                    if k in settings:
                        assert getattr(node.inputs, k) == settings[k]
                    else:
                        assert not isdefined(getattr(node.inputs, k))
            if filter_sub1:
                assert 'sub-2' not in node.fullname

    callable = Callable()
    wf.run(plugin='Debug', plugin_args={'callable': callable})
    if filter_sub1:
        assert callable.nodes_checked == set(filter(lambda x: 'sub-2' not in x, truth.keys()))
    else:
        assert callable.nodes_checked == set(truth.keys())
    wf.run(plugin='Linear')
Beispiel #8
0
def test_confdoc(tmp_path, conffilename):
    indir = tmp_path / 'in'
    anat1 = indir / 'sub-1' / 'anat'
    anat1.mkdir(parents=True)
    (anat1 / 'sub-1_T1w.nii').write_text('s1t1')
    (anat1 / 'sub-1_acq-fast_T1w.nii').write_text('s1aft1')
    anat2 = indir / 'sub-2' / 'anat'
    anat2.mkdir(parents=True)
    (anat2 / 'sub-2_T1w.nii').write_text('s2t1')
    (indir / 'MNI152.nii').write_text('MNI')

    outdir = tmp_path / 'out'
    for i, d1 in enumerate(['sub-1', 'sub-2'], start=1):
        betdir = outdir / d1 / 'BET'
        betdir.mkdir(parents=True)
        (betdir / f'sub-{i}_bet.nii').write_text(f's{i}b')
        if i == 1:
            (betdir / f'sub-{i}_acq-fast_bet.nii').write_text(f's{i}afb')
        classdir = outdir / d1 / 'classify'
        classdir.mkdir(parents=True)
        (classdir / f'sub-{i}_dseg.nii').write_text(f's{i}c')
        if i == 1:
            (classdir / f'sub-{i}_acq-fast_dseg.nii').write_text(f's{i}afc')
    crashdir = outdir / 'sub-1' / 'logs' / 'sub-1_acq-fast'
    crashdir.mkdir(parents=True)
    (crashdir / 'crash.pklz').write_text('crash')

    conf = load_config(Path(__file__).parent / conffilename)
    outfiles = get_files([indir, outdir], conf)

    assert outfiles[(
        '1',
        None)]['T1'] == tmp_path / 'in' / 'sub-1' / 'anat' / 'sub-1_T1w.nii'
    assert outfiles[(
        '1', 'fast'
    )]['T1'] == tmp_path / 'in' / 'sub-1' / 'anat' / 'sub-1_acq-fast_T1w.nii'
    assert outfiles[(
        '2',
        None)]['T1'] == tmp_path / 'in' / 'sub-2' / 'anat' / 'sub-2_T1w.nii'

    assert outfiles[(
        '1',
        None)]['BET'] == tmp_path / 'out' / 'sub-1' / 'BET' / 'sub-1_bet.nii'
    assert outfiles[(
        '1', 'fast'
    )]['BET'] == tmp_path / 'out' / 'sub-1' / 'BET' / 'sub-1_acq-fast_bet.nii'
    assert outfiles[(
        '2',
        None)]['BET'] == tmp_path / 'out' / 'sub-2' / 'BET' / 'sub-2_bet.nii'

    assert outfiles[(
        '1', None
    )]['classified'] == tmp_path / 'out' / 'sub-1' / 'classify' / 'sub-1_dseg.nii'
    assert outfiles[(
        '1', 'fast'
    )]['classified'] == tmp_path / 'out' / 'sub-1' / 'classify' / 'sub-1_acq-fast_dseg.nii'
    assert outfiles[(
        '2', None
    )]['classified'] == tmp_path / 'out' / 'sub-2' / 'classify' / 'sub-2_dseg.nii'

    assert outfiles['global']['MNI'] == tmp_path / 'in' / 'MNI152.nii'
    assert outfiles[('1', 'fast')]['crashfiles'] == [
        tmp_path / 'out' / 'sub-1' / 'logs' / 'sub-1_acq-fast' / 'crash.pklz'
    ]
    assert 'crashfiles' not in outfiles[('1', None)]
    assert 'crashfiles' not in outfiles[('2', None)]

    assert len(outfiles[('1', None)]) == 3
    assert len(outfiles[('1', 'fast')]) == 4
    assert len(outfiles[('2', None)]) == 3
    assert len(outfiles['global']) == 1

    out = tmp_path / 'out'
    wf = workflows.all_workflow(outfiles, out, conf)

    truth = {
        'report.page_subject-1.compare1': {
            'name1': 'T1w input file',
            'image1': str(tmp_path / 'in' / 'sub-1' / 'anat' /
                          'sub-1_T1w.nii'),
            'name2': 'Brain extracted files',
            'image2': str(tmp_path / 'out' / 'sub-1' / 'BET' /
                          'sub-1_bet.nii'),
            'nslices': 7,
            'relative_dir': None
        },
        'report.page_subject-1.contour2': {
            'name': 'Tissue classification',
            'image': str(tmp_path / 'out' / 'sub-1' / 'BET' / 'sub-1_bet.nii'),
            'labelimage': str(tmp_path / 'out' / 'sub-1' / 'classify' /
                              'sub-1_dseg.nii'),
            'nslices': 7,
            'relative_dir': None,
            'contour_levels': None
        },
        'report.page_subject-1.crash3': {
            'name': 'Errors',
            'crashfiles': [],
            'relative_dir': None
        },
        'report.page_subject-1_acquisition-fast.compare1': {
            'name1': 'T1w input file',
            'image1': str(tmp_path / 'in' / 'sub-1' / 'anat' /
                          'sub-1_acq-fast_T1w.nii'),
            'name2': 'Brain extracted files',
            'image2': str(tmp_path / 'out' / 'sub-1' / 'BET' /
                          'sub-1_acq-fast_bet.nii'),
            'nslices': 7,
            'relative_dir': None
        },
        'report.page_subject-1_acquisition-fast.contour2': {
            'name': 'Tissue classification',
            'image': str(tmp_path / 'out' / 'sub-1' / 'BET' /
                         'sub-1_acq-fast_bet.nii'),
            'labelimage': str(tmp_path / 'out' / 'sub-1' / 'classify' /
                              'sub-1_acq-fast_dseg.nii'),
            'nslices': 7,
            'relative_dir': None,
            'contour_levels': None
        },
        'report.page_subject-1_acquisition-fast.crash3': {
            'name': 'Errors',
            'crashfiles': [
                str(tmp_path / 'out' / 'sub-1' / 'logs' / 'sub-1_acq-fast' /
                    'crash.pklz')
            ],
            'relative_dir': None
        },
        'report.page_subject-2.compare1': {
            'name1': 'T1w input file',
            'image1': str(tmp_path / 'in' / 'sub-2' / 'anat' /
                          'sub-2_T1w.nii'),
            'name2': 'Brain extracted files',
            'image2': str(tmp_path / 'out' / 'sub-2' / 'BET' /
                          'sub-2_bet.nii'),
            'nslices': 7,
            'relative_dir': None
        },
        'report.page_subject-2.contour2': {
            'name': 'Tissue classification',
            'image': str(tmp_path / 'out' / 'sub-2' / 'BET' / 'sub-2_bet.nii'),
            'labelimage': str(tmp_path / 'out' / 'sub-2' / 'classify' /
                              'sub-2_dseg.nii'),
            'nslices': 7,
            'relative_dir': None,
            'contour_levels': None
        },
        'report.page_subject-2.crash3': {
            'name': 'Errors',
            'crashfiles': [],
            'relative_dir': None
        },
        'report.page_subject-1.assemble': {
            'out_file': str(out / 'sub-1_QC.html'), 'title': 'subject-1'
        },
        'report.page_subject-1_acquisition-fast.assemble': {
            'out_file': str(out / 'sub-1_acq-fast_QC.html'),
            'title': 'subject-1_acquisition-fast'
        },
        'report.page_subject-2.assemble': {
            'out_file': str(out / 'sub-2_QC.html'), 'title': 'subject-2'
        },
        'report.index_pages': {
            'out_file': str(out / 'QC_index.html')
        },
    }

    class Callable(object):

        def __init__(self):
            self.nodes_checked = set()
            self.nodes_seen = set()

        def __call__(self, node, graph):
            self.nodes_seen.add(node.fullname)
            if node.fullname in truth.keys():
                self.nodes_checked.add(node.fullname)
                # node_traits = set(
                #     node.inputs.trait_names()) - {'trait_added', 'trait_modified'}
                for k in set(truth[node.fullname].keys()):
                    assert getattr(node.inputs, k) == truth[node.fullname][k]
                # for k in node_traits - set(truth[node.fullname].keys()):
                #     assert not isdefined(getattr(node.inputs, k))

    callable = Callable()
    wf.run(plugin='Debug', plugin_args={'callable': callable})
    assert callable.nodes_checked == set(truth.keys())

    del conf["files"]["MNI"]["global"]
    with pytest.raises(IndexError):
        outfiles = get_files([indir, outdir], conf)
Beispiel #9
0
def test_search_types2(tmp_path, input_files_conf1):
    _make_files(tmp_path, input_files_conf1)
    c = conf.load_config(Path(__file__).parent / 'testconf1bids.json')
    layouts = get_files._get_bids_layouts([tmp_path], c)
Beispiel #10
0
def test_search_types(conffile, bids_p, re_p):
    c = conf.load_config(Path(__file__).parent / conffile)
    assert get_files._search_types(c) == (bids_p, re_p)
Beispiel #11
0
def test_conf_succ(baseconf, tmp_path):
    with open(tmp_path / 'conf.json', 'w+') as f:
        json.dump(baseconf, f)
        f.seek(0)
        conf.load_config(f)