<s> """<STR_LIT>""" <EOL> import os . path as path <EOL> import numpy as np <EOL> def _create_generic ( folds_dic , cv_cfg , method_cfg , in_out_dir , <EOL> mapper = "<STR_LIT>" , <EOL> i_red = "<STR_LIT>" , <EOL> o_red = "<STR_LIT>" , <EOL> verbose = False ) : <EOL> """<STR_LIT>""" <EOL> cv = path . join ( in_out_dir , cv_cfg [ "<STR_LIT:src>" ] ) <EOL> folds = path . join ( in_out_dir , folds_dic [ "<STR_LIT:src>" ] ) <EOL> method = path . join ( in_out_dir , method_cfg [ "<STR_LIT:src>" ] ) <EOL> m_out = path . join ( in_out_dir , "<STR_LIT>" ) <EOL> ri_out = path . join ( in_out_dir , "<STR_LIT>" ) <EOL> ro_out = path . join ( in_out_dir , "<STR_LIT>" ) <EOL> n_o = folds_dic [ "<STR_LIT>" ] <EOL> n_i = folds_dic [ "<STR_LIT>" ] if cv_cfg [ "<STR_LIT>" ] else None <EOL> all_cmd = { } <EOL> dependancies = [ ] <EOL> name_ired = "<STR_LIT>" <EOL> name_ored = "<STR_LIT>" <EOL> for i in xrange ( n_o ) : <EOL> cmd_mapper = [ "<STR_LIT>" , mapper , cv , method , folds ] <EOL> name_cur_ired = name_ired . format ( i ) <EOL> if cv_cfg [ "<STR_LIT>" ] : <EOL> for k in xrange ( n_i ) : <EOL> cur_cmd = ( cmd_mapper + <EOL> [ m_out . format ( inner = k , outer = i ) , repr ( i ) , <EOL> "<STR_LIT>" , repr ( k ) ] ) <EOL> name = "<STR_LIT>" . format ( i , k ) <EOL> all_cmd [ name ] = cur_cmd <EOL> dependancies . append ( ( name , name_cur_ired ) ) <EOL> if verbose : <EOL> print ( "<STR_LIT:U+0020>" . join ( cur_cmd ) ) <EOL> cmd_i_red = [ "<STR_LIT>" , i_red , cv , method , folds , <EOL> ri_out . format ( outer = i ) , <EOL> m_out . format ( outer = i , inner = "<STR_LIT>" ) , <EOL> repr ( i ) ] <EOL> all_cmd [ name_cur_ired ] = cmd_i_red <EOL> dependancies . append ( ( name_cur_ired , name_ored ) ) <EOL> if verbose : <EOL> print ( "<STR_LIT>" . format ( "<STR_LIT:U+0020>" . join ( cmd_i_red ) ) ) <EOL> else : <EOL> cur_cmd = cmd_mapper + [ ri_out . format ( outer = i ) , repr ( i ) ] <EOL> name = "<STR_LIT>" . format ( i ) <EOL> all_cmd [ name ] = cur_cmd <EOL> dependancies . append ( ( name , name_ored ) ) <EOL> if verbose : <EOL> print ( "<STR_LIT:U+0020>" . join ( cur_cmd ) ) <EOL> cmd_o_red = [ "<STR_LIT>" , o_red , ro_out , ri_out ] <EOL> all_cmd [ name_ored ] = cmd_o_red <EOL> if verbose : <EOL> print ( "<STR_LIT:U+0020>" . join ( cmd_o_red ) ) <EOL> return all_cmd , dependancies <EOL> def create_wf ( folds_dic , cv_cfg , method_cfg , in_out_dir , verbose = False ) : <EOL> """<STR_LIT>""" <EOL> c_map = method_cfg [ "<STR_LIT>" ] <EOL> c_i_red = method_cfg [ "<STR_LIT>" ] <EOL> c_o_red = method_cfg [ "<STR_LIT>" ] <EOL> return _create_generic ( folds_dic , cv_cfg , method_cfg , in_out_dir , <EOL> mapper = c_map , i_red = c_i_red , o_red = c_o_red , <EOL> verbose = verbose ) <EOL> def save_wf ( wf , output_file , mode = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> cmd = wf [ <NUM_LIT:0> ] <EOL> dep_orig = wf [ <NUM_LIT:1> ] <EOL> if mode == "<STR_LIT>" : <EOL> from soma_workflow . client import Job , Workflow , Helper <EOL> for k , v in cmd . iteritems ( ) : <EOL> cmd [ k ] = Job ( command = v , name = k ) <EOL> dep = [ ( ( cmd [ a ] , cmd [ b ] ) ) for a , b in dep_orig ] <EOL> jobs = np . asarray ( cmd . values ( ) ) [ np . argsort ( cmd . keys ( ) ) ] <EOL> workflow = Workflow ( jobs = jobs . tolist ( ) , <EOL> dependencies = dep ) <EOL> Helper . serialize ( output_file , workflow ) <EOL> return workflow <EOL> elif mode == "<STR_LIT>" : <EOL> import json <EOL> for k , v in cmd . iteritems ( ) : <EOL> cmd [ k ] = "<STR_LIT:U+0020>" . join ( v ) <EOL> with open ( output_file , '<STR_LIT:w>' ) as fd : <EOL> json . dump ( dict ( cmd = cmd , dep = dep_orig ) , fd , indent = True ) <EOL> return cmd <EOL> else : <EOL> raise TypeError ( "<STR_LIT>" . format ( mode ) ) </s>
