Skip to content

Commit 7b6f732

Browse files
committed
Merge remote-tracking branch 'gh-mvdoc/enh-anonym' into enh-dbic2
* gh-mvdoc/enh-anonym: Add test for add_rows_to_scans_keys_file BF: forgotten nipype Conflicts: setup.py -- both fixed for nipype
2 parents b55b892 + 97fbd33 commit 7b6f732

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

bin/heudiconv

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -990,26 +990,24 @@ def add_rows_to_scans_keys_file(fn, newrows):
990990
if lexists(fn):
991991
with open(fn, 'r') as csvfile:
992992
reader = csv.reader(csvfile, delimiter='\t')
993-
existing_rows = [row[0] for row in reader]
993+
existing_rows = [row for row in reader]
994994
# skip header
995995
fnames2info = {row[0]: row[1:] for row in existing_rows[1:]}
996996

997-
newrows_key = newrows.key()
997+
newrows_key = newrows.keys()
998998
newrows_toadd = list(set(newrows_key) - set(fnames2info.keys()))
999999
for key_toadd in newrows_toadd:
10001000
fnames2info[key_toadd] = newrows[key_toadd]
1001-
header = []
10021001
# remove
10031002
os.unlink(fn)
10041003
else:
1005-
header = ['filename', 'acq_time', 'operator', 'randstr']
10061004
fnames2info = newrows
10071005

1006+
header = ['filename', 'acq_time', 'operator', 'randstr']
10081007
# save
10091008
with open(fn, 'a') as csvfile:
10101009
writer = csv.writer(csvfile, delimiter='\t')
1011-
if header:
1012-
writer.writerow(header)
1010+
writer.writerow(header)
10131011
for key in sorted(fnames2info.keys()):
10141012
writer.writerow([key] + fnames2info[key])
10151013

tests/test_main.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import csv
12
import os
23
import pytest
34
import sys
@@ -134,5 +135,25 @@ def test_add_rows_to_scans_keys_file(tmpdir):
134135
}
135136
heudiconv.add_rows_to_scans_keys_file(fn, rows)
136137

137-
#with open(fn, 'r') as csvfile:
138+
def _check_rows(fn, rows):
139+
with open(fn, 'r') as csvfile:
140+
reader = csv.reader(csvfile, delimiter='\t')
141+
rows_loaded = []
142+
for row in reader:
143+
rows_loaded.append(row)
144+
for i, row_ in enumerate(rows_loaded):
145+
if i == 0:
146+
assert(row_ == ['filename', 'acq_time', 'operator', 'randstr'])
147+
else:
148+
assert(rows[row_[0]] == row_[1:])
149+
150+
_check_rows(fn, rows)
151+
# add a new one
152+
extra_rows = {
153+
'a_new_file.nii.gz': ['2016adsfasd23', '', 'fasadfasdf'],
154+
'my_file.nii.gz': ['2016adsfasd', '', 'fasadfasdf'],
155+
'another_file.nii.gz': ['2018xxxxx', '', 'fasadfasdf']
156+
}
157+
heudiconv.add_rows_to_scans_keys_file(fn, extra_rows)
158+
_check_rows(fn, extra_rows)
138159

0 commit comments

Comments
 (0)