Skip to content

Commit a971390

Browse files
committed
little advances
1 parent 80fcc91 commit a971390

File tree

1 file changed

+31
-20
lines changed

1 file changed

+31
-20
lines changed

nipype/algorithms/misc.py

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1152,12 +1152,9 @@ class AddCSVRowInputSpec(TraitedSpec):
11521152
cols = traits.Int(desc='Number of columns')
11531153
field_headings = traits.List(traits.Str(), mandatory=True,
11541154
desc='Heading list of available field to be added.')
1155-
1156-
float_trait = traits.Float( 0.0, argstr='%.5f' )
1157-
int_trait = traits.Int( 0, argstr='%d' )
1158-
str_trait = traits.Str( '', argstr='"%s"')
1159-
row_trait = traits.Either( int_trait, float_trait, str_trait )
1160-
new_fields = traits.List( row_trait, mandatory=True, desc='List of new values in row')
1155+
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' )
11611158

11621159
class AddCSVRowOutputSpec(TraitedSpec):
11631160
csv_file = File(desc='Output CSV file containing rows ')
@@ -1200,36 +1197,50 @@ def _run_interface(self, runtime):
12001197

12011198
if not isdefined( self.inputs.cols ) and isdefined( self.inputs.field_headings ):
12021199
cols = len( self.inputs.field_headings )
1200+
headings = self.inputs.field_headings
12031201

12041202
if cols == 0:
12051203
iflogger.error( 'Number of cols and length of field headings must be > 0' )
12061204

12071205
if len( self.inputs.new_fields ) != cols:
12081206
iflogger.error( 'Wrong length of fields, does not match number of cols' )
12091207

1210-
with open(self.inputs.in_file, 'r+') as in_file:
1208+
col_width = self.inputs.col_width
1209+
float_dec = self.inputs.float_dec
1210+
1211+
with open(self.inputs.in_file, 'a+') as in_file:
12111212
lines = in_file.readlines()
1213+
1214+
if len(lines)>0 and lines[0]=='\n':
1215+
lines.pop()
1216+
1217+
print len(lines)
12121218

1213-
if len(headings)>0 and (len(lines) == 0 or lines[0] == ''):
1219+
if (len(headings)>0) and (len(lines)==0):
12141220
hdr = [ '"%s"' % h for h in self.inputs.field_headings ]
12151221
hdrstr = ",".join(hdr)
1216-
lines.insert( 0, hdrstr )
1222+
lines = [ hdrstr+'\n' ]
1223+
1224+
if not lines[-1] or lines[-1]=='\n':
1225+
lines.pop()
12171226

1218-
print self.inputs.new_fields.items()
1219-
1220-
metadata = dict(argstr=lambda t: t is not None)
1227+
row_data = []
1228+
metadata = dict(separator=lambda t: t is not None)
12211229
for name, spec in sorted(self.inputs.traits(**metadata).items()):
1222-
print name
1223-
value = getattr(self.inputs, name)
1224-
if not value is None:
1225-
print value
1226-
#arg = self._format_row( name, spec, value )
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 )
12271236

1237+
row_data.append( argstr.format(v) )
12281238

1239+
newrow = ",".join( row_data )
1240+
lines.append( newrow+'\n' )
12291241

1230-
#newrow = ",".join( self.inputs.new_fields )
1231-
#lines.append( newrow )
1232-
#in_file.writelines( lines )
1242+
print "".join( lines )
1243+
in_file.write( "".join(lines) )
12331244

12341245
return runtime
12351246

0 commit comments

Comments
 (0)