66import numpy as np
77
88from nipype .utils .filemanip import loadpkl
9- from nipype import logging
9+ from nipype import logging , Function
1010
1111from . import state
1212from . import auxiliary as aux
@@ -164,23 +164,14 @@ def get_input_el(self, ind):
164164 ])
165165 if not from_node .mapper :
166166 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 (
170170 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
173173 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 ))
184175
185176 return state_dict , inputs_dict
186177
@@ -191,11 +182,10 @@ def _reading_ci_output(self, dir_nm_el, out_nm, node=None):
191182 result_pklfile = os .path .join (os .getcwd (), node .workingdir , dir_nm_el ,
192183 node .interface .nn .name , "result_{}.pklz" .format (
193184 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
199189 return False
200190
201191 # checking if all outputs are saved
@@ -253,15 +243,8 @@ def __init__(self,
253243 self .workingdir = workingdir
254244 self .interface = interface
255245
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
265248 if not self .output_names :
266249 self .output_names = []
267250
@@ -293,19 +276,12 @@ def run_interface_el(self, i, ind):
293276 print ("Run interface el, dict={}" .format (state_dict ))
294277 logger .debug ("Run interface el, name={}, inputs_dict={}, state_dict={}" .format (
295278 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 )
309285
310286 # TODO when join
311287 #if self._joinByKey:
@@ -317,14 +293,6 @@ def run_interface_el(self, i, ind):
317293 # dir_nm_el = os.path.join(dir_join, dir_nm_el)
318294 return res
319295
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 ))
328296
329297 def get_output (self ):
330298 """collecting all outputs and updating self._output"""
@@ -337,32 +305,11 @@ def get_output(self):
337305 state_dict = self .state .state_ind (ind )
338306 dir_nm_el = "_" .join (["{}:{}" .format (i , j ) for i , j in list (state_dict .items ())])
339307 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 ))
353310 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 ))
366313 return self ._output
367314
368315 # dj: version without join
@@ -378,13 +325,8 @@ def _check_all_results(self):
378325 dir_nm_el = ""
379326
380327 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
388330 self ._is_complete = True
389331 return True
390332
@@ -394,18 +336,15 @@ def _reading_results(self):
394336 """
395337 for key_out in self .output_names :
396338 self ._result [key_out ] = []
397- #pdb.set_trace()
398339 if self ._state_inputs :
399340 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 ))
403343 else :
404344 # st_dict should be {}
405345 # 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 ))
409348
410349 # dj: removing temp. from Node class
411350 # def run(self, plugin="serial"):
@@ -559,22 +498,13 @@ def _reading_results(self):
559498 res_l = []
560499 val_l = self ._dict_tuple2list (self .output [key_out ][dir_nm_el ])
561500 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 )
566502 self ._result [key_out ].append ((wf_inputs_dict , res_l ))
567503 else :
568504 val_l = self ._dict_tuple2list (self .output [key_out ])
569505 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+
578508
579509 def add_nodes (self , nodes ):
580510 """adding nodes without defining connections
@@ -594,6 +524,7 @@ def add(self,
594524 name = None ,
595525 workingdir = None ,
596526 inputs = None ,
527+ input_names = None ,
597528 output_names = None ,
598529 mapper = None ,
599530 write_state = True ,
@@ -602,10 +533,13 @@ def add(self,
602533 if is_function (runnable ):
603534 if not output_names :
604535 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" )
607538 if not name :
608539 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" )
609543 if not workingdir :
610544 workingdir = name
611545 node = Node (
@@ -615,8 +549,9 @@ def add(self,
615549 inputs = inputs ,
616550 mapper = mapper ,
617551 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 ):
620555 if not name :
621556 raise Exception ("you have to specify name for the node" )
622557 if not workingdir :
@@ -735,10 +670,6 @@ def is_function(obj):
735670 return hasattr (obj , '__call__' )
736671
737672
738- def is_function_interface (obj ):
739- return type (obj ) is aux .FunctionInterface
740-
741-
742673def is_current_interface (obj ):
743674 return type (obj ) is aux .CurrentInterface
744675
0 commit comments