Exemple #1
0
def responders(l2_dir,
               roi="DSURQEc_ctx",
               data_root="~/ni_data/ofM.dr",
               roi_root="~/ni_data/templates/roi"):

    data_regex = "(?P<subject>.+)/tstat1.nii.gz"
    data_path = "{data_root}/l2/{l2_dir}/".format(data_root=data_root,
                                                  l2_dir=l2_dir)
    data_path = path.expanduser(data_path)
    roi_path = "{roi_root}/{roi}.nii.gz".format(roi_root=roi_root, roi=roi)
    roi_path = path.expanduser(roi_path)

    data_find = DataFinder()
    data_find.inputs.root_paths = data_path
    data_find.inputs.match_regex = path.join(data_path, data_regex)
    found_data = data_find.run().outputs

    masker = NiftiMasker(mask_img=roi_path)
    voxeldf = pd.DataFrame({})
    for subject, data_file in zip(found_data.subject, found_data.out_paths):
        subject_data = {}
        print(subject, data_file)
        img = nib.load(data_file)
        img = masker.fit_transform(img)
        img = img.flatten()
        subject_data["subject"] = subject
        for i in img:
            voxel_data = deepcopy(subject_data)
            voxel_data["t"] = i
            df_ = pd.DataFrame(voxel_data, index=[None])
            voxeldf = pd.concat([voxeldf, df_])
    voxeldf.to_csv('{}/ctx_responders.csv'.format(data_path))
Exemple #2
0
def test_datafinder_unpack():
    outdir = mkdtemp()
    single_res = os.path.join(outdir, "findme.txt")
    open(single_res, 'a').close()
    open(os.path.join(outdir, "dontfindme"), 'a').close()

    from nipype.interfaces.io import DataFinder
    df = DataFinder()
    df.inputs.root_paths = outdir
    df.inputs.match_regex = '.+/(?P<basename>.+)\.txt'
    df.inputs.unpack_single = True
    result = df.run()
    print result.outputs.out_paths
    yield assert_equal, result.outputs.out_paths, single_res
Exemple #3
0
def test_datafinder_unpack():
    outdir = mkdtemp()
    single_res = os.path.join(outdir, "findme.txt")
    open(single_res, 'a').close()
    open(os.path.join(outdir, "dontfindme"), 'a').close()

    from nipype.interfaces.io import DataFinder
    df = DataFinder()
    df.inputs.root_paths = outdir
    df.inputs.match_regex = '.+/(?P<basename>.+)\.txt'
    df.inputs.unpack_single = True
    result = df.run()
    print result.outputs.out_paths
    yield assert_equal, result.outputs.out_paths, single_res
Exemple #4
0
def test_datafinder_depth():
    outdir = mkdtemp()
    os.makedirs(os.path.join(outdir, '0', '1', '2', '3'))

    from nipype.interfaces.io import DataFinder
    df = DataFinder()
    df.inputs.root_paths = os.path.join(outdir, '0')
    for min_depth in range(4):
        for max_depth in range(min_depth, 4):
            df.inputs.min_depth = min_depth
            df.inputs.max_depth = max_depth
            result = df.run()
            expected = [str(x) for x in range(min_depth, max_depth + 1)]
            for path, exp_fname in zip(result.outputs.out_paths, expected):
                _, fname = os.path.split(path)
                yield assert_equal, fname, exp_fname

    shutil.rmtree(outdir)
Exemple #5
0
def test_datafinder_depth():
    outdir = mkdtemp()
    os.makedirs(os.path.join(outdir, '0', '1', '2', '3'))

    from nipype.interfaces.io import DataFinder
    df = DataFinder()
    df.inputs.root_paths = os.path.join(outdir, '0')
    for min_depth in range(4):
        for max_depth in range(min_depth, 4):
            df.inputs.min_depth = min_depth
            df.inputs.max_depth = max_depth
            result = df.run()
            expected = [str(x) for x in range(min_depth, max_depth + 1)]
            for path, exp_fname in zip(result.outputs.out_paths, expected):
                _, fname = os.path.split(path)
                yield assert_equal, fname, exp_fname

    shutil.rmtree(outdir)
Exemple #6
0
def responders(
    l2_dir,
    roi="dsurqec_200micron_roi-dr",
    data_root="~/ni_data/ofM.dr",
    roi_root='/usr/share/mouse-brain-atlases',
    save_inplace=True,
    save_as='',
):

    data_regex = "(?P<subject>.+)/.*?_tstat\.nii\.gz"
    data_path = "{data_root}/l2/{l2_dir}/".format(data_root=data_root,
                                                  l2_dir=l2_dir)
    data_path = path.expanduser(data_path)
    roi_path = "{roi_root}/{roi}.nii".format(roi_root=roi_root, roi=roi)
    roi_path = path.expanduser(roi_path)

    data_find = DataFinder()
    data_find.inputs.root_paths = data_path
    data_find.inputs.match_regex = path.join(data_path, data_regex)
    found_data = data_find.run().outputs
    print(found_data)

    masker = NiftiMasker(mask_img=roi_path)
    voxeldf = pd.DataFrame({})
    for subject, data_file in zip(found_data.subject, found_data.out_paths):
        subject_data = {}
        print(subject, data_file)
        img = nib.load(data_file)
        img = masker.fit_transform(img)
        img = img.flatten()
        subject_data["subject"] = subject
        for i in img:
            voxel_data = deepcopy(subject_data)
            voxel_data["t"] = i
            df_ = pd.DataFrame(voxel_data, index=[None])
            voxeldf = pd.concat([voxeldf, df_])
    if save_inplace:
        voxeldf.to_csv('{}/ctx_responders.csv'.format(data_path))
    else:
        voxeldf.to_csv(path.abspath(path.expanduser(save_as)))
Exemple #7
0
def test_datafinder_copydir():
    outdir = mkdtemp()
    open(os.path.join(outdir, "findme.txt"), 'a').close()
    open(os.path.join(outdir, "dontfindme"), 'a').close()
    open(os.path.join(outdir, "dontfindmealsotxt"), 'a').close()
    open(os.path.join(outdir, "findmetoo.txt"), 'a').close()
    open(os.path.join(outdir, "ignoreme.txt"), 'a').close()
    open(os.path.join(outdir, "alsoignore.txt"), 'a').close()

    from nipype.interfaces.io import DataFinder
    df = DataFinder()
    df.inputs.root_paths = outdir
    df.inputs.match_regex = '.+/(?P<basename>.+)\.txt'
    df.inputs.ignore_regexes = ['ignore']
    result = df.run()
    expected = ["findme.txt", "findmetoo.txt"]
    for path, expected_fname in zip(result.outputs.out_paths, expected):
        _, fname = os.path.split(path)
        yield assert_equal, fname, expected_fname

    yield assert_equal, result.outputs.basename, ["findme", "findmetoo"]

    shutil.rmtree(outdir)
Exemple #8
0
def test_datafinder_copydir():
    outdir = mkdtemp()
    open(os.path.join(outdir, "findme.txt"), 'a').close()
    open(os.path.join(outdir, "dontfindme"), 'a').close()
    open(os.path.join(outdir, "dontfindmealsotxt"), 'a').close()
    open(os.path.join(outdir, "findmetoo.txt"), 'a').close()
    open(os.path.join(outdir, "ignoreme.txt"), 'a').close()
    open(os.path.join(outdir, "alsoignore.txt"), 'a').close()

    from nipype.interfaces.io import DataFinder
    df = DataFinder()
    df.inputs.root_paths = outdir
    df.inputs.match_regex = '.+/(?P<basename>.+)\.txt'
    df.inputs.ignore_regexes = ['ignore']
    result = df.run()
    expected = ["findme.txt", "findmetoo.txt"]
    for path, expected_fname in zip(result.outputs.out_paths, expected):
        _, fname = os.path.split(path)
        yield assert_equal, fname, expected_fname

    yield assert_equal, result.outputs.basename, ["findme", "findmetoo"]

    shutil.rmtree(outdir)
Exemple #9
0
from nipype.interfaces.io import DataFinder

df = DataFinder()
df.inputs.root_paths = '/tmp/exp_01'
df.inputs.match_regex = '.+/(?P<series_dir>.+)/(?P<basename>.+)\.nii.gz'
result = df.run()
print(result.outputs.out_paths)