Skip to content

Commit 1e40613

Browse files
authored
Merge pull request #46 from yibeichan/redo-split
Redo split
2 parents 61cf3bb + 2030f18 commit 1e40613

File tree

7 files changed

+89
-86
lines changed

7 files changed

+89
-86
lines changed

.gitignore

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
11
.DS_Store
2-
*/.DS_Store
3-
*/*/.DS_Store
4-
*/*/*/.DS_Store
5-
*/.DS_Store
6-
*/*/*/*/.DS_Store
7-
*/*/*/*/*/.DS_Store
8-
92
# Byte-compiled / optimized / DLL files
103
__pycache__/
114
*.py[cod]

pydra/tasks/fsl/_version.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# file generated by setuptools_scm
2+
# don't change, don't track in version control
3+
__version__ = version = '0.1.dev123+gd6b46a1.d20230502'
4+
__version_tuple__ = version_tuple = (0, 1, 'dev123', 'gd6b46a1.d20230502')

pydra/tasks/fsl/utils/split.py

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22
from pydra import ShellCommandTask
33
import typing as ty
44

5+
6+
def Split_output(inputs):
7+
import os, glob
8+
9+
output_dir = os.getcwd()
10+
return sorted(glob.glob(os.path.join(output_dir, f"{inputs.output_basename}*.*")))
11+
12+
513
input_fields = [
614
(
715
"in_file",
@@ -14,9 +22,9 @@
1422
},
1523
),
1624
(
17-
"out_base_name",
25+
"output_basename",
1826
str,
19-
{"help_string": "outputs prefix", "argstr": "{out_base_name}", "position": 1},
27+
{"help_string": "outputs prefix", "argstr": "{output_basename}", "position": 1},
2028
),
2129
(
2230
"dimension",
@@ -31,13 +39,34 @@
3139
]
3240
Split_input_spec = specs.SpecInfo(name="Input", fields=input_fields, bases=(specs.ShellSpec,))
3341

34-
output_fields = []
42+
output_fields = [
43+
(
44+
"out_files",
45+
specs.MultiOutputFile,
46+
{
47+
"help_string": "output files",
48+
"requires": ["in_file", "output_basename", "dimension"],
49+
"callable": Split_output,
50+
},
51+
)
52+
]
3553
Split_output_spec = specs.SpecInfo(
3654
name="Output", fields=output_fields, bases=(specs.ShellOutSpec,)
3755
)
3856

3957

4058
class Split(ShellCommandTask):
59+
"""
60+
Example
61+
-------
62+
>>> task = Split()
63+
>>> task.inputs.in_file = "test.nii.gz"
64+
>>> task.inputs.output_basename = "test_split"
65+
>>> task.inputs.dimension = "t"
66+
>>> task.cmdline
67+
'fslsplit test.nii.gz test_split -t'
68+
"""
69+
4170
input_spec = Split_input_spec
4271
output_spec = Split_output_spec
4372
executable = "fslsplit"

pydra/tasks/fsl/utils/tests/test_run_split.py

Lines changed: 13 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,19 @@
44

55

66
@pytest.mark.xfail("FSLDIR" not in os.environ, reason="no FSL found", raises=FileNotFoundError)
7-
@pytest.mark.parametrize("inputs, outputs", [])
7+
@pytest.mark.parametrize(
8+
"inputs, outputs",
9+
[
10+
(
11+
{
12+
"in_file": "test.nii.gz",
13+
"output_basename": "test_split",
14+
"dimension": "t",
15+
},
16+
["out_files"],
17+
)
18+
],
19+
)
820
def test_Split(test_data, inputs, outputs):
921
if inputs is None:
1022
in_file = Path(test_data) / "test.nii.gz"
@@ -39,34 +51,3 @@ def test_Split(test_data, inputs, outputs):
3951
assert [os.path.exists(x) for x in getattr(res.output, out_nm)]
4052
else:
4153
assert os.path.exists(getattr(res.output, out_nm))
42-
43-
44-
@pytest.mark.parametrize("inputs, error", [(None, "AttributeError")])
45-
def test_Split_exception(test_data, inputs, error):
46-
if inputs is None:
47-
in_file = Path(test_data) / "test.nii.gz"
48-
task = Split(in_file=in_file)
49-
else:
50-
for key, val in inputs.items():
51-
try:
52-
pattern = r"\.[a-zA-Z]*"
53-
if isinstance(val, str):
54-
if re.findall(pattern, val) != []:
55-
inputs[key] = Path(test_data) / val
56-
elif "_dir" in key:
57-
dirpath = Path(test_data) / val
58-
if dirpath.exists() and dirpath.is_dir():
59-
shutil.rmtree(dirpath)
60-
inputs[key] = Path(test_data) / val
61-
else:
62-
inputs[key] = eval(val)
63-
elif isinstance(val, list):
64-
if all(re.findall(pattern, _) != [] for _ in val):
65-
inputs[key] = [Path(test_data) / _ for _ in val]
66-
else:
67-
inputs[key] = eval(val)
68-
except:
69-
pass
70-
task = Split(**inputs)
71-
with pytest.raises(eval(error)):
72-
task.generated_output_names

pydra/tasks/fsl/utils/tests/test_spec_split.py

Lines changed: 13 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,19 @@
33
from ..split import Split
44

55

6-
@pytest.mark.parametrize("inputs, outputs", [])
6+
@pytest.mark.parametrize(
7+
"inputs, outputs",
8+
[
9+
(
10+
{
11+
"in_file": "test.nii.gz",
12+
"output_basename": "test_split",
13+
"dimension": "t",
14+
},
15+
["out_files"],
16+
)
17+
],
18+
)
719
def test_Split(test_data, inputs, outputs):
820
if inputs is None:
921
in_file = Path(test_data) / "test.nii.gz"
@@ -31,34 +43,3 @@ def test_Split(test_data, inputs, outputs):
3143
pass
3244
task = Split(**inputs)
3345
assert set(task.generated_output_names) == set(["return_code", "stdout", "stderr"] + outputs)
34-
35-
36-
@pytest.mark.parametrize("inputs, error", [(None, "AttributeError")])
37-
def test_Split_exception(test_data, inputs, error):
38-
if inputs is None:
39-
in_file = Path(test_data) / "test.nii.gz"
40-
task = Split(in_file=in_file)
41-
else:
42-
for key, val in inputs.items():
43-
try:
44-
pattern = r"\.[a-zA-Z]*"
45-
if isinstance(val, str):
46-
if re.findall(pattern, val) != []:
47-
inputs[key] = Path(test_data) / val
48-
elif "_dir" in key:
49-
dirpath = Path(test_data) / val
50-
if dirpath.exists() and dirpath.is_dir():
51-
shutil.rmtree(dirpath)
52-
inputs[key] = Path(test_data) / val
53-
else:
54-
inputs[key] = eval(val)
55-
elif isinstance(val, list):
56-
if all(re.findall(pattern, _) != [] for _ in val):
57-
inputs[key] = [Path(test_data) / _ for _ in val]
58-
else:
59-
inputs[key] = eval(val)
60-
except:
61-
pass
62-
task = Split(**inputs)
63-
with pytest.raises(eval(error)):
64-
task.generated_output_names

specs/callables.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -392,13 +392,20 @@ def MELODIC_output(field, inputs):
392392
return outputs
393393

394394

395-
def SLICE_output(inputs):
396-
import glob
395+
# def SLICE_output(inputs):
396+
# import glob
397397

398-
suffix = "slice_*"
399-
if inputs.out_base_name:
400-
fname_template = f"{inputs.out_base_name}_{suffix}"
401-
else:
402-
fname_template = f"{inputs.in_file}_{suffix}"
398+
# suffix = "slice_*"
399+
# if inputs.out_base_name:
400+
# fname_template = f"{inputs.out_base_name}_{suffix}"
401+
# else:
402+
# fname_template = f"{inputs.in_file}_{suffix}"
403+
404+
# return sorted(glob(fname_template))
405+
406+
407+
def Split_output(inputs):
408+
import os, glob
403409

404-
return sorted(glob(fname_template))
410+
output_dir = os.getcwd()
411+
return sorted(glob.glob(os.path.join(output_dir, f"{inputs.output_basename}*.*")))

specs/fsl_utils_param.yml

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -332,13 +332,21 @@ Smooth:
332332

333333
Split:
334334
output_requirements:
335-
out_file: [in_file, dimension]
336-
output_templates:
335+
out_files: [in_file, output_basename, dimension]
336+
output_callables:
337+
out_files: Split_output
337338
doctest:
339+
in_file: test.nii.gz
340+
output_basename: test_split
341+
dimension: t
342+
cmdline: fslsplit test.nii.gz test_split -t
343+
338344
tests_inputs:
339-
-
345+
- in_file: test.nii.gz
346+
output_basename: test_split
347+
dimension: t
340348
tests_outputs:
341-
- AttributeError
349+
- out_files
342350

343351
SwapDimensions:
344352
output_requirements:

0 commit comments

Comments
 (0)