File tree Expand file tree Collapse file tree 2 files changed +26
-7
lines changed Expand file tree Collapse file tree 2 files changed +26
-7
lines changed Original file line number Diff line number Diff line change @@ -990,26 +990,24 @@ def add_rows_to_scans_keys_file(fn, newrows):
990
990
if lexists (fn ):
991
991
with open (fn , 'r' ) as csvfile :
992
992
reader = csv .reader (csvfile , delimiter = '\t ' )
993
- existing_rows = [row [ 0 ] for row in reader ]
993
+ existing_rows = [row for row in reader ]
994
994
# skip header
995
995
fnames2info = {row [0 ]: row [1 :] for row in existing_rows [1 :]}
996
996
997
- newrows_key = newrows .key ()
997
+ newrows_key = newrows .keys ()
998
998
newrows_toadd = list (set (newrows_key ) - set (fnames2info .keys ()))
999
999
for key_toadd in newrows_toadd :
1000
1000
fnames2info [key_toadd ] = newrows [key_toadd ]
1001
- header = []
1002
1001
# remove
1003
1002
os .unlink (fn )
1004
1003
else :
1005
- header = ['filename' , 'acq_time' , 'operator' , 'randstr' ]
1006
1004
fnames2info = newrows
1007
1005
1006
+ header = ['filename' , 'acq_time' , 'operator' , 'randstr' ]
1008
1007
# save
1009
1008
with open (fn , 'a' ) as csvfile :
1010
1009
writer = csv .writer (csvfile , delimiter = '\t ' )
1011
- if header :
1012
- writer .writerow (header )
1010
+ writer .writerow (header )
1013
1011
for key in sorted (fnames2info .keys ()):
1014
1012
writer .writerow ([key ] + fnames2info [key ])
1015
1013
Original file line number Diff line number Diff line change
1
+ import csv
1
2
import os
2
3
import pytest
3
4
import sys
@@ -134,5 +135,25 @@ def test_add_rows_to_scans_keys_file(tmpdir):
134
135
}
135
136
heudiconv .add_rows_to_scans_keys_file (fn , rows )
136
137
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 )
138
159
You can’t perform that action at this time.
0 commit comments