6
6
import numpy as np
7
7
8
8
from nipype .utils .filemanip import loadpkl
9
- from nipype import logging
9
+ from nipype import logging , Function
10
10
11
11
from . import state
12
12
from . import auxiliary as aux
@@ -164,23 +164,14 @@ def get_input_el(self, ind):
164
164
])
165
165
if not from_node .mapper :
166
166
dir_nm_el_from = ""
167
-
168
- if is_node (from_node ) and is_current_interface ( from_node . interface ) :
169
- file_from = self ._reading_ci_output (
167
+ # TODO: do I need this if, what if this is wf?
168
+ if is_node (from_node ):
169
+ out_from = self ._reading_ci_output (
170
170
node = from_node , dir_nm_el = dir_nm_el_from , out_nm = from_socket )
171
- if file_from and os . path . exists ( file_from ) :
172
- inputs_dict ["{}.{}" .format (self .name , to_socket )] = file_from
171
+ if out_from :
172
+ inputs_dict ["{}.{}" .format (self .name , to_socket )] = out_from
173
173
else :
174
- raise Exception ("{} doesnt exist" .format (file_from ))
175
- else : # assuming here that I want to read the file (will not be used with the current interfaces)
176
- file_from = os .path .join (from_node .workingdir , dir_nm_el_from ,
177
- from_socket + ".txt" )
178
- with open (file_from ) as f :
179
- content = f .readline ()
180
- try :
181
- inputs_dict ["{}.{}" .format (self .name , to_socket )] = eval (content )
182
- except NameError :
183
- inputs_dict ["{}.{}" .format (self .name , to_socket )] = content
174
+ raise Exception ("output from {} doesnt exist" .format (from_node ))
184
175
185
176
return state_dict , inputs_dict
186
177
@@ -191,11 +182,10 @@ def _reading_ci_output(self, dir_nm_el, out_nm, node=None):
191
182
result_pklfile = os .path .join (os .getcwd (), node .workingdir , dir_nm_el ,
192
183
node .interface .nn .name , "result_{}.pklz" .format (
193
184
node .interface .nn .name ))
194
- if os .path .exists (result_pklfile ):
195
- out_file = getattr (loadpkl (result_pklfile ).outputs , out_nm )
196
- if os .path .exists (out_file ):
197
- return out_file
198
-
185
+ if os .path .exists (result_pklfile ) and os .stat (result_pklfile ).st_size > 0 :
186
+ out = getattr (loadpkl (result_pklfile ).outputs , out_nm )
187
+ if out :
188
+ return out
199
189
return False
200
190
201
191
# checking if all outputs are saved
@@ -253,15 +243,8 @@ def __init__(self,
253
243
self .workingdir = workingdir
254
244
self .interface = interface
255
245
256
- if is_function_interface (self .interface ):
257
- # adding node name to the interface's name mapping
258
- self .interface .input_map = dict ((key , "{}.{}" .format (self .name , value ))
259
- for (key , value ) in self .interface .input_map .items ())
260
- # list of output names taken from interface output name
261
- self .output_names = self .interface ._output_nm
262
- elif is_current_interface (self .interface ):
263
- # list of interf_key_out
264
- self .output_names = output_names
246
+ # list of interf_key_out
247
+ self .output_names = output_names
265
248
if not self .output_names :
266
249
self .output_names = []
267
250
@@ -293,19 +276,12 @@ def run_interface_el(self, i, ind):
293
276
print ("Run interface el, dict={}" .format (state_dict ))
294
277
logger .debug ("Run interface el, name={}, inputs_dict={}, state_dict={}" .format (
295
278
self .name , inputs_dict , state_dict ))
296
- if is_function_interface (self .interface ):
297
- res = self .interface .run (inputs_dict )
298
- output = self .interface .output
299
- print ("Run fun interface el, output={}" .format (output ))
300
- logger .debug ("Run fun interface el, output={}" .format (output ))
301
- self ._writting_results_tmp (state_dict , dir_nm_el , output )
302
- elif is_current_interface (self .interface ):
303
- if not self .mapper :
304
- dir_nm_el = ""
305
- res = self .interface .run (
306
- inputs = inputs_dict ,
307
- base_dir = os .path .join (os .getcwd (), self .workingdir ),
308
- dir_nm_el = dir_nm_el )
279
+ if not self .mapper :
280
+ dir_nm_el = ""
281
+ res = self .interface .run (
282
+ inputs = inputs_dict ,
283
+ base_dir = os .path .join (os .getcwd (), self .workingdir ),
284
+ dir_nm_el = dir_nm_el )
309
285
310
286
# TODO when join
311
287
#if self._joinByKey:
@@ -317,14 +293,6 @@ def run_interface_el(self, i, ind):
317
293
# dir_nm_el = os.path.join(dir_join, dir_nm_el)
318
294
return res
319
295
320
- def _writting_results_tmp (self , state_dict , dir_nm_el , output ):
321
- """temporary method to write the results in the files (this is usually part of a interface)"""
322
- if not self .mapper :
323
- dir_nm_el = ''
324
- os .makedirs (os .path .join (self .workingdir , dir_nm_el ), exist_ok = True )
325
- for key_out , val_out in output .items ():
326
- with open (os .path .join (self .workingdir , dir_nm_el , key_out + ".txt" ), "w" ) as fout :
327
- fout .write (str (val_out ))
328
296
329
297
def get_output (self ):
330
298
"""collecting all outputs and updating self._output"""
@@ -337,32 +305,11 @@ def get_output(self):
337
305
state_dict = self .state .state_ind (ind )
338
306
dir_nm_el = "_" .join (["{}:{}" .format (i , j ) for i , j in list (state_dict .items ())])
339
307
if self .mapper :
340
- if is_function_interface (self .interface ):
341
- output = os .path .join (self .workingdir , dir_nm_el , key_out + ".txt" )
342
- if self .interface .out_read :
343
- with open (output ) as fout :
344
- content = fout .readline ()
345
- try :
346
- output = eval (content )
347
- except NameError :
348
- output = content
349
- self ._output [key_out ][dir_nm_el ] = (state_dict , output )
350
- elif is_current_interface (self .interface ):
351
- self ._output [key_out ][dir_nm_el ] = \
352
- (state_dict , (state_dict , self ._reading_ci_output (dir_nm_el = dir_nm_el , out_nm = key_out )))
308
+ self ._output [key_out ][dir_nm_el ] = \
309
+ (state_dict , self ._reading_ci_output (dir_nm_el = dir_nm_el , out_nm = key_out ))
353
310
else :
354
- if is_function_interface (self .interface ):
355
- output = os .path .join (self .workingdir , key_out + ".txt" )
356
- if self .interface .out_read :
357
- with open (output ) as fout :
358
- try :
359
- output = eval (fout .readline ())
360
- except Workflow :
361
- output = fout .readline ()
362
- self ._output [key_out ] = (state_dict , output )
363
- elif is_current_interface (self .interface ):
364
- self ._output [key_out ] = \
365
- (state_dict , self ._reading_ci_output (dir_nm_el = "" , out_nm = key_out ))
311
+ self ._output [key_out ] = \
312
+ (state_dict , self ._reading_ci_output (dir_nm_el = "" , out_nm = key_out ))
366
313
return self ._output
367
314
368
315
# dj: version without join
@@ -378,13 +325,8 @@ def _check_all_results(self):
378
325
dir_nm_el = ""
379
326
380
327
for key_out in self .output_names :
381
- if is_function_interface (self .interface ):
382
- if not os .path .isfile (
383
- os .path .join (self .workingdir , dir_nm_el , key_out + ".txt" )):
384
- return False
385
- elif is_current_interface (self .interface ):
386
- if not self ._reading_ci_output (dir_nm_el , key_out ):
387
- return False
328
+ if not self ._reading_ci_output (dir_nm_el , key_out ):
329
+ return False
388
330
self ._is_complete = True
389
331
return True
390
332
@@ -394,18 +336,15 @@ def _reading_results(self):
394
336
"""
395
337
for key_out in self .output_names :
396
338
self ._result [key_out ] = []
397
- #pdb.set_trace()
398
339
if self ._state_inputs :
399
340
val_l = self ._dict_tuple2list (self ._output [key_out ])
400
- for (st_dict , filename ) in val_l :
401
- with open (filename ) as fout :
402
- self ._result [key_out ].append ((st_dict , eval (fout .readline ())))
341
+ for (st_dict , out ) in val_l :
342
+ self ._result [key_out ].append ((st_dict , out ))
403
343
else :
404
344
# st_dict should be {}
405
345
# not sure if this is used (not tested)
406
- (st_dict , filename ) = self ._output [key_out ][None ]
407
- with open (filename ) as fout :
408
- self ._result [key_out ].append (({}, eval (fout .readline ())))
346
+ (st_dict , out ) = self ._output [key_out ][None ]
347
+ self ._result [key_out ].append (({}, out ))
409
348
410
349
# dj: removing temp. from Node class
411
350
# def run(self, plugin="serial"):
@@ -559,22 +498,13 @@ def _reading_results(self):
559
498
res_l = []
560
499
val_l = self ._dict_tuple2list (self .output [key_out ][dir_nm_el ])
561
500
for val in val_l :
562
- with open (val [1 ]) as fout :
563
- logger .debug ('Reading Results: file={}, st_dict={}' .format (
564
- val [1 ], val [0 ]))
565
- res_l .append ((val [0 ], eval (fout .readline ())))
501
+ res_l .append (val )
566
502
self ._result [key_out ].append ((wf_inputs_dict , res_l ))
567
503
else :
568
504
val_l = self ._dict_tuple2list (self .output [key_out ])
569
505
for val in val_l :
570
- #TODO: I think that val shouldn't be dict here...
571
- # TMP solution
572
- if type (val ) is dict :
573
- val = [v for k , v in val .items ()][0 ]
574
- with open (val [1 ]) as fout :
575
- logger .debug ('Reading Results: file={}, st_dict={}' .format (
576
- val [1 ], val [0 ]))
577
- self ._result [key_out ].append ((val [0 ], eval (fout .readline ())))
506
+ self ._result [key_out ].append (val )
507
+
578
508
579
509
def add_nodes (self , nodes ):
580
510
"""adding nodes without defining connections
@@ -594,6 +524,7 @@ def add(self,
594
524
name = None ,
595
525
workingdir = None ,
596
526
inputs = None ,
527
+ input_names = None ,
597
528
output_names = None ,
598
529
mapper = None ,
599
530
write_state = True ,
@@ -602,10 +533,13 @@ def add(self,
602
533
if is_function (runnable ):
603
534
if not output_names :
604
535
output_names = ["out" ]
605
- interface = aux . FunctionInterface (
606
- function = runnable , output_nm = output_names , out_read = out_read )
536
+ if input_names is None :
537
+ raise Exception ( "you need to specify input_names" )
607
538
if not name :
608
539
raise Exception ("you have to specify name for the node" )
540
+ nipype1_interf = Function (function = runnable , input_names = input_names ,
541
+ output_names = output_names )
542
+ interface = aux .CurrentInterface (interface = nipype1_interf , name = "addtwo" )
609
543
if not workingdir :
610
544
workingdir = name
611
545
node = Node (
@@ -615,8 +549,9 @@ def add(self,
615
549
inputs = inputs ,
616
550
mapper = mapper ,
617
551
other_mappers = self ._node_mappers ,
618
- write_state = write_state )
619
- elif is_function_interface (runnable ) or is_current_interface (runnable ):
552
+ write_state = write_state ,
553
+ output_names = output_names )
554
+ elif is_current_interface (runnable ):
620
555
if not name :
621
556
raise Exception ("you have to specify name for the node" )
622
557
if not workingdir :
@@ -735,10 +670,6 @@ def is_function(obj):
735
670
return hasattr (obj , '__call__' )
736
671
737
672
738
- def is_function_interface (obj ):
739
- return type (obj ) is aux .FunctionInterface
740
-
741
-
742
673
def is_current_interface (obj ):
743
674
return type (obj ) is aux .CurrentInterface
744
675
0 commit comments