Skip to content

Commit 7117659

Browse files
committed
Adds populate_intended_for to process_extra_commands
It also adds the corresponding test
1 parent d5f01b9 commit 7117659

File tree

2 files changed

+41
-4
lines changed

2 files changed

+41
-4
lines changed

heudiconv/main.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import sys
44

55
from . import __version__, __packagename__
6-
from .bids import populate_bids_templates, tuneup_bids_json_files
6+
from .bids import populate_bids_templates, tuneup_bids_json_files, populate_intended_for
77
from .convert import prep_conversion
88
from .due import due, Doi
99
from .parser import get_study_sessions
@@ -47,13 +47,13 @@ def process_extra_commands(outdir, command, files, dicom_dir_template,
4747
heuristic, session, subjs, grouping):
4848
"""
4949
Perform custom command instead of regular operations. Supported commands:
50-
['treat-json', 'ls', 'populate-templates']
50+
['treat-json', 'ls', 'populate-templates', 'populate_intended_for']
5151
5252
Parameters
5353
----------
5454
outdir : str
5555
Output directory
56-
command : {'treat-json', 'ls', 'populate-templates'}
56+
command : {'treat-json', 'ls', 'populate-templates', 'populate_intended_for'}
5757
Heudiconv command to run
5858
files : list of str
5959
List of files
@@ -108,6 +108,12 @@ def process_extra_commands(outdir, command, files, dicom_dir_template,
108108
ensure_heuristic_arg(heuristic)
109109
from .utils import get_heuristic_description
110110
print(get_heuristic_description(heuristic, full=True))
111+
elif command == 'populate_intended_for':
112+
for subj in subjs:
113+
session_path = op.join(outdir, 'sub-' + subj)
114+
if session:
115+
session_path = op.join(session_path, 'ses-' + session)
116+
populate_intended_for(session_path)
111117
else:
112118
raise ValueError("Unknown command %s" % command)
113119
return

heudiconv/tests/test_main.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# TODO: break this up by modules
22

33
from heudiconv.cli.run import main as runner
4-
from heudiconv.main import workflow
4+
from heudiconv.main import (workflow,
5+
process_extra_commands)
56
from heudiconv import __version__
67
from heudiconv.utils import (create_file_if_missing,
78
set_readonly,
@@ -290,3 +291,33 @@ def test_no_etelemetry():
290291
with patch.dict('sys.modules', {'etelemetry': None}):
291292
workflow(outdir='/dev/null', command='ls',
292293
heuristic='reproin', files=[])
294+
295+
296+
# Test two scenarios:
297+
# -study without sessions
298+
# -study with sessions
299+
# The "expected_folder" is the session folder without the tmpdir
300+
@pytest.mark.parametrize(
301+
"session, expected_folder", [
302+
('', '/foo/sub-{sID}'),
303+
('pre', '/foo/sub-{sID}/ses-pre')
304+
]
305+
)
306+
def test_populate_intended_for(session, expected_folder, capfd):
307+
"""
308+
Tests for "process_extra_commands" when the command is
309+
'populate_intended_for'
310+
"""
311+
# Because the function .utils.populate_intended_for already has its own
312+
# tests, here we just test that "process_extra_commands", when 'command'
313+
# is 'populate_intended_for' does what we expect (loop through the list of
314+
# subjects and calls 'populate_intended_for'). We call it using folders
315+
# that don't exist, and check that the output is the expected.
316+
bids_folder = expected_folder.split('sub-')[0]
317+
subjects = ['1', '2']
318+
process_extra_commands(bids_folder, 'populate_intended_for', [], '',
319+
'', session, subjects, None)
320+
captured_output = capfd.readouterr().err
321+
for s in subjects:
322+
expected_info = 'Adding "IntendedFor" to the fieldmaps in ' + expected_folder.format(sID=s)
323+
assert expected_info in captured_output

0 commit comments

Comments
 (0)