Skip to content

Commit bd58815

Browse files
committed
first working version
1 parent a971390 commit bd58815

File tree

1 file changed

+39
-33
lines changed

1 file changed

+39
-33
lines changed

nipype/algorithms/misc.py

Lines changed: 39 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1153,8 +1153,8 @@ class AddCSVRowInputSpec(TraitedSpec):
11531153
field_headings = traits.List(traits.Str(), mandatory=True,
11541154
desc='Heading list of available field to be added.')
11551155
new_fields = traits.List( traits.Any(), mandatory=True, desc='List of new values in row', separator=',')
1156-
col_width = traits.Int( 7, mandatory=True, usedefault=True, desc='column width' )
1157-
float_dec = traits.Int( 4, mandatory=True, usedefault=True, desc='decimals' )
1156+
col_width = traits.Int( 9, mandatory=True, usedefault=True, desc='column width' )
1157+
float_dec = traits.Int( 6, mandatory=True, usedefault=True, desc='decimals' )
11581158

11591159
class AddCSVRowOutputSpec(TraitedSpec):
11601160
csv_file = File(desc='Output CSV file containing rows ')
@@ -1176,10 +1176,13 @@ class AddCSVRow(BaseInterface):
11761176
"""
11771177
input_spec = AddCSVRowInputSpec
11781178
output_spec = AddCSVRowOutputSpec
1179+
_hdrstr = None
11791180

11801181
def _run_interface(self, runtime):
11811182
cols = 0
11821183
headings = []
1184+
col_width = self.inputs.col_width
1185+
float_dec = self.inputs.float_dec
11831186

11841187
if not isdefined( self.inputs.cols ) and not isdefined( self.inputs.field_headings ):
11851188
iflogger.error( 'Either number of cols or field headings is required' )
@@ -1205,42 +1208,45 @@ def _run_interface(self, runtime):
12051208
if len( self.inputs.new_fields ) != cols:
12061209
iflogger.error( 'Wrong length of fields, does not match number of cols' )
12071210

1208-
col_width = self.inputs.col_width
1209-
float_dec = self.inputs.float_dec
1211+
if len(headings)>0:
1212+
argstr = '{:>%d}' % col_width
1213+
hdr = [ argstr.format( '"' + h + '"') for h in self.inputs.field_headings ]
1214+
self._hdrstr = ",".join(hdr) + '\n'
12101215

1211-
with open(self.inputs.in_file, 'a+') as in_file:
1212-
lines = in_file.readlines()
1213-
1214-
if len(lines)>0 and lines[0]=='\n':
1215-
lines.pop()
12161216

1217-
print len(lines)
1217+
if op.exists( self.inputs.in_file ):
1218+
with open(self.inputs.in_file, 'a+') as in_file:
1219+
lines = in_file.readlines()
12181220

1219-
if (len(headings)>0) and (len(lines)==0):
1220-
hdr = [ '"%s"' % h for h in self.inputs.field_headings ]
1221-
hdrstr = ",".join(hdr)
1222-
lines = [ hdrstr+'\n' ]
1223-
1224-
if not lines[-1] or lines[-1]=='\n':
1221+
if len(lines)>0 and lines[0]=='\n':
12251222
lines.pop()
12261223

1227-
row_data = []
1228-
metadata = dict(separator=lambda t: t is not None)
1229-
for name, spec in sorted(self.inputs.traits(**metadata).items()):
1230-
values = getattr(self.inputs, name)
1231-
1232-
for v in values:
1233-
argstr = '{:>%d}' % col_width
1234-
if type(v)=='float':
1235-
argstr = '{:>%d.%df}' % ( col_width, float_dec )
1236-
1237-
row_data.append( argstr.format(v) )
1238-
1239-
newrow = ",".join( row_data )
1240-
lines.append( newrow+'\n' )
1241-
1242-
print "".join( lines )
1243-
in_file.write( "".join(lines) )
1224+
if (len(headings)>0) and (len(lines)==0):
1225+
lines.insert(0, self._hdrstr )
1226+
in_file.write( "".join(lines) )
1227+
else:
1228+
with open(self.inputs.in_file, 'w+') as in_file:
1229+
in_file.write( self._hdrstr )
1230+
1231+
1232+
row_data = []
1233+
metadata = dict(separator=lambda t: t is not None)
1234+
for name, spec in sorted(self.inputs.traits(**metadata).items()):
1235+
values = getattr(self.inputs, name)
1236+
for v in values:
1237+
argstr = '{:>%d}' % col_width
1238+
if type(v) is float:
1239+
argstr = '{:>%d.%df}' % ( col_width, float_dec )
1240+
if type(v) is str:
1241+
v = '"' + v + '"'
1242+
row_data.append( argstr.format(v) )
1243+
newrow = ",".join( row_data ) + '\n'
1244+
1245+
with open(self.inputs.in_file, 'r+') as in_file:
1246+
in_file.seek(-2, 2)
1247+
if in_file.read(2) == '\n\n':
1248+
in_file.seek(-1, 1)
1249+
in_file.write( newrow )
12441250

12451251
return runtime
12461252

0 commit comments

Comments
 (0)