Skip to content

Commit 986ec09

Browse files
committed
RF: move all the tests from reproin.py to its test_
To avoid pytest freaking out, as in https://travis-ci.org/nipy/heudiconv/jobs/453993426
1 parent 2b083a9 commit 986ec09

File tree

3 files changed

+167
-158
lines changed

3 files changed

+167
-158
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ install:
3737
- git config --global user.name "Travis Almighty"
3838

3939
script:
40-
- coverage run `which py.test` -s -v heudiconv heudiconv/heuristics/*.py
40+
- coverage run `which py.test` -s -v heudiconv
4141

4242
after_success:
4343
- codecov

heudiconv/heuristics/reproin.py

Lines changed: 0 additions & 156 deletions
Original file line numberDiff line numberDiff line change
@@ -922,159 +922,3 @@ def fixup_subjectid(subjectid):
922922
# just filter out possible _- in it
923923
return re.sub('[-_]', '', subjectid)
924924
return "sid%06d" % int(reg.groups()[0])
925-
926-
927-
def test_filter_files():
928-
# Filtering is currently disabled -- any sequence directory is Ok
929-
assert(filter_files('/home/mvdoc/dbic/09-run_func_meh/0123432432.dcm'))
930-
assert(filter_files('/home/mvdoc/dbic/run_func_meh/012343143.dcm'))
931-
932-
933-
def test_md5sum():
934-
assert md5sum('cryptonomicon') == '1cd52edfa41af887e14ae71d1db96ad1'
935-
assert md5sum('mysecretmessage') == '07989808231a0c6f522f9d8e34695794'
936-
937-
938-
def test_fix_canceled_runs():
939-
from collections import namedtuple
940-
FakeSeqInfo = namedtuple('FakeSeqInfo',
941-
['accession_number', 'series_id',
942-
'protocol_name', 'series_description'])
943-
944-
seqinfo = []
945-
runname = 'func_run+'
946-
for i in range(1, 6):
947-
seqinfo.append(
948-
FakeSeqInfo('accession1',
949-
'{0:02d}-'.format(i) + runname,
950-
runname, runname)
951-
)
952-
953-
fake_accession2run = {
954-
'accession1': ['^01-', '^03-']
955-
}
956-
957-
seqinfo_ = fix_canceled_runs(seqinfo, fake_accession2run)
958-
959-
for i, s in enumerate(seqinfo_, 1):
960-
output = runname
961-
if i == 1 or i == 3:
962-
output = 'cancelme_' + output
963-
for key in ['series_description', 'protocol_name']:
964-
value = getattr(s, key)
965-
assert(value == output)
966-
# check we didn't touch series_id
967-
assert(s.series_id == '{0:02d}-'.format(i) + runname)
968-
969-
970-
def test_fix_dbic_protocol():
971-
from collections import namedtuple
972-
FakeSeqInfo = namedtuple('FakeSeqInfo',
973-
['accession_number', 'study_description',
974-
'field1', 'field2'])
975-
accession_number = 'A003'
976-
seq1 = FakeSeqInfo(accession_number,
977-
'mystudy',
978-
'02-anat-scout_run+_MPR_sag',
979-
'11-func_run-life2_acq-2mm692')
980-
seq2 = FakeSeqInfo(accession_number,
981-
'mystudy',
982-
'nochangeplease',
983-
'nochangeeither')
984-
985-
986-
seqinfos = [seq1, seq2]
987-
keys = ['field1']
988-
subsdict = {
989-
md5sum('mystudy'):
990-
[('scout_run\+', 'scout'),
991-
('run-life[0-9]', 'run+_task-life')],
992-
}
993-
994-
seqinfos_ = fix_dbic_protocol(seqinfos, keys=keys, subsdict=subsdict)
995-
assert(seqinfos[1] == seqinfos_[1])
996-
# field2 shouldn't have changed since I didn't pass it
997-
assert(seqinfos_[0] == FakeSeqInfo(accession_number,
998-
'mystudy',
999-
'02-anat-scout_MPR_sag',
1000-
seq1.field2))
1001-
1002-
# change also field2 please
1003-
keys = ['field1', 'field2']
1004-
seqinfos_ = fix_dbic_protocol(seqinfos, keys=keys, subsdict=subsdict)
1005-
assert(seqinfos[1] == seqinfos_[1])
1006-
# now everything should have changed
1007-
assert(seqinfos_[0] == FakeSeqInfo(accession_number,
1008-
'mystudy',
1009-
'02-anat-scout_MPR_sag',
1010-
'11-func_run+_task-life_acq-2mm692'))
1011-
1012-
1013-
def test_sanitize_str():
1014-
assert sanitize_str('[email protected]') == 'superduperfaster'
1015-
assert sanitize_str('perfect') == 'perfect'
1016-
assert sanitize_str('never:use:colon:!') == 'neverusecolon'
1017-
1018-
1019-
def test_fixupsubjectid():
1020-
assert fixup_subjectid("abra") == "abra"
1021-
assert fixup_subjectid("sub") == "sub"
1022-
assert fixup_subjectid("sid") == "sid"
1023-
assert fixup_subjectid("sid000030") == "sid000030"
1024-
assert fixup_subjectid("sid0000030") == "sid000030"
1025-
assert fixup_subjectid("sid00030") == "sid000030"
1026-
assert fixup_subjectid("sid30") == "sid000030"
1027-
assert fixup_subjectid("SID30") == "sid000030"
1028-
1029-
1030-
def test_parse_series_spec():
1031-
pdpn = parse_series_spec
1032-
1033-
assert pdpn("nondbic_func-bold") == {}
1034-
assert pdpn("cancelme_func-bold") == {}
1035-
1036-
assert pdpn("bids_func-bold") == \
1037-
pdpn("func-bold") == \
1038-
{'seqtype': 'func', 'seqtype_label': 'bold'}
1039-
1040-
# pdpn("bids_func_ses+_task-boo_run+") == \
1041-
# order and PREFIX: should not matter, as well as trailing spaces
1042-
assert \
1043-
pdpn(" PREFIX:bids_func_ses+_task-boo_run+ ") == \
1044-
pdpn("PREFIX:bids_func_ses+_task-boo_run+") == \
1045-
pdpn("bids_func_ses+_run+_task-boo") == \
1046-
{
1047-
'seqtype': 'func',
1048-
# 'seqtype_label': 'bold',
1049-
'session': '+',
1050-
'run': '+',
1051-
'task': 'boo',
1052-
}
1053-
1054-
# TODO: fix for that
1055-
assert pdpn("bids_func-pace_ses-1_task-boo_acq-bu_bids-please_run-2__therest") == \
1056-
pdpn("bids_func-pace_ses-1_run-2_task-boo_acq-bu_bids-please__therest") == \
1057-
pdpn("func-pace_ses-1_task-boo_acq-bu_bids-please_run-2") == \
1058-
{
1059-
'seqtype': 'func', 'seqtype_label': 'pace',
1060-
'session': '1',
1061-
'run': '2',
1062-
'task': 'boo',
1063-
'acq': 'bu',
1064-
'bids': 'bids-please'
1065-
}
1066-
1067-
assert pdpn("bids_anat-scout_ses+") == \
1068-
{
1069-
'seqtype': 'anat',
1070-
'seqtype_label': 'scout',
1071-
'session': '+',
1072-
}
1073-
1074-
assert pdpn("anat_T1w_acq-MPRAGE_run+") == \
1075-
{
1076-
'seqtype': 'anat',
1077-
'run': '+',
1078-
'acq': 'MPRAGE',
1079-
'seqtype_label': 'T1w'
1080-
}

heudiconv/heuristics/test_reproin.py

Lines changed: 166 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,16 @@
22
# Tests for reproin.py
33
#
44
from collections import OrderedDict
5-
from .reproin import get_dups_marked
5+
from .reproin import (
6+
filter_files,
7+
fix_canceled_runs,
8+
fix_dbic_protocol,
9+
fixup_subjectid,
10+
get_dups_marked,
11+
md5sum,
12+
parse_series_spec,
13+
sanitize_str,
14+
)
615

716

817
def test_get_dups_marked():
@@ -37,3 +46,159 @@ def test_get_dups_marked():
3746
('smth2',): ['c']
3847
}
3948

49+
50+
51+
def test_filter_files():
52+
# Filtering is currently disabled -- any sequence directory is Ok
53+
assert(filter_files('/home/mvdoc/dbic/09-run_func_meh/0123432432.dcm'))
54+
assert(filter_files('/home/mvdoc/dbic/run_func_meh/012343143.dcm'))
55+
56+
57+
def test_md5sum():
58+
assert md5sum('cryptonomicon') == '1cd52edfa41af887e14ae71d1db96ad1'
59+
assert md5sum('mysecretmessage') == '07989808231a0c6f522f9d8e34695794'
60+
61+
62+
def test_fix_canceled_runs():
63+
from collections import namedtuple
64+
FakeSeqInfo = namedtuple('FakeSeqInfo',
65+
['accession_number', 'series_id',
66+
'protocol_name', 'series_description'])
67+
68+
seqinfo = []
69+
runname = 'func_run+'
70+
for i in range(1, 6):
71+
seqinfo.append(
72+
FakeSeqInfo('accession1',
73+
'{0:02d}-'.format(i) + runname,
74+
runname, runname)
75+
)
76+
77+
fake_accession2run = {
78+
'accession1': ['^01-', '^03-']
79+
}
80+
81+
seqinfo_ = fix_canceled_runs(seqinfo, fake_accession2run)
82+
83+
for i, s in enumerate(seqinfo_, 1):
84+
output = runname
85+
if i == 1 or i == 3:
86+
output = 'cancelme_' + output
87+
for key in ['series_description', 'protocol_name']:
88+
value = getattr(s, key)
89+
assert(value == output)
90+
# check we didn't touch series_id
91+
assert(s.series_id == '{0:02d}-'.format(i) + runname)
92+
93+
94+
def test_fix_dbic_protocol():
95+
from collections import namedtuple
96+
FakeSeqInfo = namedtuple('FakeSeqInfo',
97+
['accession_number', 'study_description',
98+
'field1', 'field2'])
99+
accession_number = 'A003'
100+
seq1 = FakeSeqInfo(accession_number,
101+
'mystudy',
102+
'02-anat-scout_run+_MPR_sag',
103+
'11-func_run-life2_acq-2mm692')
104+
seq2 = FakeSeqInfo(accession_number,
105+
'mystudy',
106+
'nochangeplease',
107+
'nochangeeither')
108+
109+
110+
seqinfos = [seq1, seq2]
111+
keys = ['field1']
112+
subsdict = {
113+
md5sum('mystudy'):
114+
[('scout_run\+', 'scout'),
115+
('run-life[0-9]', 'run+_task-life')],
116+
}
117+
118+
seqinfos_ = fix_dbic_protocol(seqinfos, keys=keys, subsdict=subsdict)
119+
assert(seqinfos[1] == seqinfos_[1])
120+
# field2 shouldn't have changed since I didn't pass it
121+
assert(seqinfos_[0] == FakeSeqInfo(accession_number,
122+
'mystudy',
123+
'02-anat-scout_MPR_sag',
124+
seq1.field2))
125+
126+
# change also field2 please
127+
keys = ['field1', 'field2']
128+
seqinfos_ = fix_dbic_protocol(seqinfos, keys=keys, subsdict=subsdict)
129+
assert(seqinfos[1] == seqinfos_[1])
130+
# now everything should have changed
131+
assert(seqinfos_[0] == FakeSeqInfo(accession_number,
132+
'mystudy',
133+
'02-anat-scout_MPR_sag',
134+
'11-func_run+_task-life_acq-2mm692'))
135+
136+
137+
def test_sanitize_str():
138+
assert sanitize_str('[email protected]') == 'superduperfaster'
139+
assert sanitize_str('perfect') == 'perfect'
140+
assert sanitize_str('never:use:colon:!') == 'neverusecolon'
141+
142+
143+
def test_fixupsubjectid():
144+
assert fixup_subjectid("abra") == "abra"
145+
assert fixup_subjectid("sub") == "sub"
146+
assert fixup_subjectid("sid") == "sid"
147+
assert fixup_subjectid("sid000030") == "sid000030"
148+
assert fixup_subjectid("sid0000030") == "sid000030"
149+
assert fixup_subjectid("sid00030") == "sid000030"
150+
assert fixup_subjectid("sid30") == "sid000030"
151+
assert fixup_subjectid("SID30") == "sid000030"
152+
153+
154+
def test_parse_series_spec():
155+
pdpn = parse_series_spec
156+
157+
assert pdpn("nondbic_func-bold") == {}
158+
assert pdpn("cancelme_func-bold") == {}
159+
160+
assert pdpn("bids_func-bold") == \
161+
pdpn("func-bold") == \
162+
{'seqtype': 'func', 'seqtype_label': 'bold'}
163+
164+
# pdpn("bids_func_ses+_task-boo_run+") == \
165+
# order and PREFIX: should not matter, as well as trailing spaces
166+
assert \
167+
pdpn(" PREFIX:bids_func_ses+_task-boo_run+ ") == \
168+
pdpn("PREFIX:bids_func_ses+_task-boo_run+") == \
169+
pdpn("bids_func_ses+_run+_task-boo") == \
170+
{
171+
'seqtype': 'func',
172+
# 'seqtype_label': 'bold',
173+
'session': '+',
174+
'run': '+',
175+
'task': 'boo',
176+
}
177+
178+
# TODO: fix for that
179+
assert pdpn("bids_func-pace_ses-1_task-boo_acq-bu_bids-please_run-2__therest") == \
180+
pdpn("bids_func-pace_ses-1_run-2_task-boo_acq-bu_bids-please__therest") == \
181+
pdpn("func-pace_ses-1_task-boo_acq-bu_bids-please_run-2") == \
182+
{
183+
'seqtype': 'func', 'seqtype_label': 'pace',
184+
'session': '1',
185+
'run': '2',
186+
'task': 'boo',
187+
'acq': 'bu',
188+
'bids': 'bids-please'
189+
}
190+
191+
assert pdpn("bids_anat-scout_ses+") == \
192+
{
193+
'seqtype': 'anat',
194+
'seqtype_label': 'scout',
195+
'session': '+',
196+
}
197+
198+
assert pdpn("anat_T1w_acq-MPRAGE_run+") == \
199+
{
200+
'seqtype': 'anat',
201+
'run': '+',
202+
'acq': 'MPRAGE',
203+
'seqtype_label': 'T1w'
204+
}

0 commit comments

Comments
 (0)