Skip to content
Snippets Groups Projects
Commit 53589fb3 authored by Michiel Cottaar's avatar Michiel Cottaar
Browse files

Added filetree to fslpy

parent 8b55606f
No related branches found
No related tags found
No related merge requests found
{var:.0f} (int)
{var:.1f} (f1)
{var:.2f} (f2)
dir1
->eddy basename=1 (sub1)
dir2
->eddy basename=2 (sub2)
->eddy basename=1b (sub1b)
->eddy basename=0 (sub0)
->eddy basename=subvar_{subvar} (subvar)
# Sample Test passing with nose and pytest
from fsl.utils import filetree
from pathlib import PurePath
import os.path as op
import pytest
from glob import glob
def same_path(p1, p2):
assert PurePath(p1) == PurePath(p2)
def test_simple_tree():
tree = filetree.FileTree.read('eddy')
assert tree.variables['basename'] == 'eddy_output'
same_path(tree.get('basename'), './eddy_output')
same_path(tree.get('image'), './eddy_output.nii.gz')
tree = filetree.FileTree.read('eddy.tree', basename='out')
same_path(tree.get('basename'), './out')
same_path(tree.update(basename='test').get('basename'), './test')
same_path(tree.get('basename'), './out')
with pytest.raises(ValueError):
filetree.FileTree.read('non_existing')
def test_complicated_tree():
tree = filetree.FileTree.read('HCP_directory').update(subject='100307')
same_path(tree.get('T1w_acpc_dc'), '100307/T1w/T1w_acpc_dc.nii.gz')
L_white = '100307/T1w/fsaverage_LR32k/100307.L.white.32k_fs_LR.surf.gii'
same_path(tree.update(hemi='L').get('T1w_32k/white'), L_white)
same_path(tree.sub_trees['T1w_32k'].update(hemi='L').get('white'), L_white)
assert tree.exists(('T1w_32k/white', ))
assert tree.exists('T1w_32k/white')
assert not tree.exists(('T1w_32k/white_misspelled', ))
assert not tree.exists(('T1w_32k/white', 'T1w_32k/white_misspelled', ))
assert not tree.exists(('T1w_32k_err/white', ))
assert not tree.exists(('../test'))
with pytest.raises(ValueError):
assert not tree.exists(('../test'), error=True)
with pytest.raises(ValueError):
tree.exists(('T1w_32k_err/white', ), error=True)
assert tree.exists(('T1w_32k/white', ), error=True)
def test_parent_tree():
directory = op.split(__file__)[0]
tree = filetree.FileTree.read(op.join(directory, 'parent.tree'))
same_path(tree.get('sub0/basename'), '0')
same_path(tree.get('sub1/basename'), 'dir1/1')
same_path(tree.get('sub1b/basename'), 'dir1/1b')
same_path(tree.get('sub2/basename'), 'dir1/dir2/2')
same_path(tree.update(subvar='grot').get('subvar/basename'), 'subvar_grot')
with pytest.raises(KeyError):
tree.get('sub_var/basename')
def test_custom_tree():
directory = op.split(__file__)[0]
tree = filetree.FileTree.read(op.join(directory, 'custom.tree'), directory=directory)
same_path(tree.get('sub_file'), op.join(directory, 'parent/sub_file.nii.gz'))
same_path(tree.update(opt='test').get('sub_file'), op.join(directory, 'parent/opt_layer_test/sub_file.nii.gz'))
with pytest.raises(KeyError):
tree.get('opt_file')
same_path(tree.update(opt='test').get('opt_file'), op.join(directory, 'parent/opt_layer_test/opt_file_test.nii.gz'))
assert len(tree.update(opt='test').get_all('sub_file')) == 1
assert len(tree.update(opt='test').get_all_vars('sub_file')) == 1
assert len(tree.update(opt='test2').get_all('sub_file')) == 0
assert len(tree.update(opt='test2').get_all_vars('sub_file')) == 0
assert len(tree.get_all('sub_file', glob_vars=['opt'])) == 2
assert len(tree.get_all('sub_file', glob_vars='all')) == 2
assert len(tree.get_all('sub_file')) == 1
for fn, settings in zip(tree.get_all('sub_file', glob_vars='all'),
tree.get_all_vars('sub_file', glob_vars='all')):
same_path(fn, tree.update(**settings).get('sub_file'))
assert len(tree.update(opt='test2').get_all('opt_file')) == 0
assert len(tree.update(opt='test').get_all('opt_file')) == 1
with pytest.raises(KeyError):
tree.get_all('opt_file')
assert len(tree.get_all('opt_file', glob_vars=['opt'])) == 1
for vars in ({}, {'opt': 'test'}):
filename = tree.update(**vars).get('sub_file')
assert vars == tree.extract_variables('sub_file', filename)
assert tree.exists(('sub_file', 'opt_file'), error=True, on_disk=True, glob_vars=['opt'])
assert tree.exists(('sub_file', 'opt_file'), on_disk=True, glob_vars=['opt'])
assert not tree.exists(('sub_file', 'opt_file'), error=False, on_disk=True)
with pytest.raises(KeyError):
assert tree.exists(('sub_file', 'opt_file'), error=True, on_disk=True)
assert not tree.update(opt='test2').exists(('sub_file', 'opt_file'), on_disk=True)
with pytest.raises(IOError):
tree.update(opt='test2').exists(('sub_file', 'opt_file'), on_disk=True, error=True)
assert tree.template_variables() == {'opt'}
assert tree.template_variables(optional=False) == {'opt'}
assert tree.template_variables(required=False) == set()
assert tree.template_variables(required=False, optional=False) == set()
assert tree.template_variables('sub_file') == {'opt'}
assert tree.template_variables('sub_file', required=False) == {'opt'}
assert tree.template_variables('sub_file', optional=False) == set()
assert tree.template_variables('sub_file', optional=False, required=False) == set()
assert tree.template_variables('opt_file') == {'opt'}
assert tree.template_variables('opt_file', required=False) == set()
assert tree.template_variables('opt_file', optional=False) == {'opt'}
assert tree.template_variables('opt_file', optional=False, required=False) == set()
def test_format():
directory = op.split(__file__)[0]
tree = filetree.FileTree.read(op.join(directory, 'format.tree'), var=1.23)
same_path(tree.get('int'), '1')
same_path(tree.get('f1'), '1.2')
same_path(tree.get('f2'), '1.23')
def test_read_all():
for directory in filetree.tree_directories:
if directory != '.':
for filename in glob(op.join(directory, '*.tree')):
filetree.FileTree.read(filename)
def test_extract_vars_but():
"""
Reproduces a bug where the already provided variables are ignored
"""
directory = op.split(__file__)[0]
tree = filetree.FileTree.read(op.join(directory, 'extract_vars.tree'))
fn = 'parent/opt_layer_test/opt_file_test.nii.gz'
assert {'p1': 'opt_file', 'p2': 'test'} == tree.extract_variables('fn', fn)
assert {'p1': 'opt_file', 'p2': 'test'} == tree.update(p1='opt_file').extract_variables('fn', fn)
assert {'p1': 'opt', 'p2': 'file_test'} == tree.update(p1='opt').extract_variables('fn', fn)
assert {'p1': 'opt_{p3}', 'p2': 'test', 'p3': 'file'} == tree.update(p1='opt_{p3}').extract_variables('fn', fn)
from fsl.utils.filetree import register_tree, FileTree
import os.path as op
class SubFileTree(FileTree):
pass
def test_register_parent():
directory = op.split(__file__)[0]
filename = op.join(directory, 'parent.tree')
# call from sub-type
tree = SubFileTree.read(filename)
assert isinstance(tree, FileTree)
assert isinstance(tree, SubFileTree)
for child in tree.sub_trees.values():
assert isinstance(child, FileTree)
assert not isinstance(child, SubFileTree)
# call from FileTree
tree = FileTree.read(filename)
assert isinstance(tree, FileTree)
assert not isinstance(tree, SubFileTree)
for child in tree.sub_trees.values():
assert isinstance(child, FileTree)
assert not isinstance(child, SubFileTree)
# register + call from FileTree
register_tree('parent', SubFileTree)
tree = FileTree.read(filename)
assert isinstance(tree, FileTree)
assert isinstance(tree, SubFileTree)
for child in tree.sub_trees.values():
assert isinstance(child, FileTree)
assert not isinstance(child, SubFileTree)
# register + call from SubFileTree
register_tree('parent', FileTree)
tree = SubFileTree.read(filename)
assert isinstance(tree, FileTree)
assert not isinstance(tree, SubFileTree)
for child in tree.sub_trees.values():
assert isinstance(child, FileTree)
assert not isinstance(child, SubFileTree)
def test_children():
directory = op.split(__file__)[0]
filename = op.join(directory, 'parent.tree')
tree = SubFileTree.read(filename)
assert isinstance(tree, FileTree)
assert not isinstance(tree, SubFileTree)
for child in tree.sub_trees.values():
assert isinstance(child, FileTree)
assert not isinstance(child, SubFileTree)
register_tree('eddy', SubFileTree)
tree = SubFileTree.read(filename)
assert isinstance(tree, FileTree)
assert not isinstance(tree, SubFileTree)
for child in tree.sub_trees.values():
assert isinstance(child, FileTree)
assert isinstance(child, SubFileTree)
register_tree('eddy', FileTree)
tree = SubFileTree.read(filename)
assert isinstance(tree, FileTree)
assert not isinstance(tree, SubFileTree)
for child in tree.sub_trees.values():
assert isinstance(child, FileTree)
assert not isinstance(child, SubFileTree)
from fsl.utils.filetree import utils
import pytest
def test_variables():
assert ('var', 'other_var', 'var') == tuple(utils.find_variables('some{var}_{other_var}_{var}'))
assert ('var', 'other_var', 'var') == tuple(utils.find_variables('some{var}_[{other_var}_{var}]'))
assert {'other_var'} == utils.optional_variables('some{var}_[{other_var}_{var}]')
def test_get_variables():
assert {'var': 'test'} == utils.extract_variables('{var}', 'test')
assert {'var': 'test'} == utils.extract_variables('2{var}', '2test')
assert {'var': 'test'} == utils.extract_variables('{var}[_{other_var}]', 'test')
assert {'var': 'test', 'other_var': 'foo'} == utils.extract_variables('{var}[_{other_var}]', 'test_foo')
assert {'var': 'test', 'other_var': 'foo'} == utils.extract_variables('{var}[_{other_var}]_{var}', 'test_foo_test')
assert {'var': 'test'} == utils.extract_variables('{var}[_{other_var}]_{var}', 'test_test')
with pytest.raises(ValueError):
utils.extract_variables('{var}[_{other_var}]_{var}', 'test_foo')
with pytest.raises(ValueError):
utils.extract_variables('{var}[_{other_var}]_{var}', 'test_foo_bar')
with pytest.raises(ValueError):
utils.extract_variables('bar{var}[_{other_var}]_{var}', 'test')
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment