<s> import sys <EOL> import signal <EOL> from time import time , strftime <EOL> from msct_parser import Parser <EOL> import sct_utils as sct <EOL> import os <EOL> import copy_reg <EOL> import types <EOL> import pandas as pd <EOL> path_script = os . path . dirname ( __file__ ) <EOL> path_sct = os . path . dirname ( path_script ) <EOL> sys . path . append ( path_sct + '<STR_LIT>' ) <EOL> sys . path . append ( path_sct + '<STR_LIT>' ) <EOL> def _pickle_method ( method ) : <EOL> """<STR_LIT>""" <EOL> func_name = method . im_func . __name__ <EOL> obj = method . im_self <EOL> cls = method . im_class <EOL> cls_name = '<STR_LIT>' <EOL> if func_name . startswith ( '<STR_LIT>' ) and not func_name . endswith ( '<STR_LIT>' ) : <EOL> cls_name = cls . __name__ . lstrip ( '<STR_LIT:_>' ) <EOL> if cls_name : <EOL> func_name = '<STR_LIT:_>' + cls_name + func_name <EOL> return _unpickle_method , ( func_name , obj , cls ) <EOL> def _unpickle_method ( func_name , obj , cls ) : <EOL> """<STR_LIT>""" <EOL> for cls in cls . mro ( ) : <EOL> try : <EOL> func = cls . __dict__ [ func_name ] <EOL> except KeyError : <EOL> pass <EOL> else : <EOL> break <EOL> return func . __get__ ( obj , cls ) <EOL> copy_reg . pickle ( types . MethodType , _pickle_method , _unpickle_method ) <EOL> def generate_data_list ( folder_dataset , verbose = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> data_subjects , subjects_dir = [ ] , [ ] <EOL> for subject_dir in os . listdir ( folder_dataset ) : <EOL> if not subject_dir . startswith ( '<STR_LIT:.>' ) and os . path . isdir ( folder_dataset + subject_dir ) : <EOL> data_subjects . append ( folder_dataset + subject_dir + '<STR_LIT:/>' ) <EOL> subjects_dir . append ( subject_dir ) <EOL> if not data_subjects : <EOL> sct . printv ( '<STR_LIT>' + folder_dataset + '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> verbose = verbose , type = '<STR_LIT:error>' ) <EOL> return data_subjects , subjects_dir <EOL> def process_results ( results , subjects_name , function , folder_dataset , parameters ) : <EOL> try : <EOL> results_dataframe = pd . concat ( [ result [ <NUM_LIT:2> ] for result in results ] ) <EOL> results_dataframe . loc [ : , '<STR_LIT>' ] = pd . Series ( subjects_name , index = results_dataframe . index ) <EOL> results_dataframe . loc [ : , '<STR_LIT>' ] = pd . Series ( [ function ] * len ( subjects_name ) , index = results_dataframe . index ) <EOL> results_dataframe . loc [ : , '<STR_LIT>' ] = pd . Series ( [ folder_dataset ] * len ( subjects_name ) , index = results_dataframe . index ) <EOL> results_dataframe . loc [ : , '<STR_LIT>' ] = pd . Series ( [ parameters ] * len ( subjects_name ) , index = results_dataframe . index ) <EOL> return results_dataframe <EOL> except KeyboardInterrupt : <EOL> return '<STR_LIT>' <EOL> except Exception as e : <EOL> sct . printv ( '<STR_LIT>' . format ( sys . exc_info ( ) [ - <NUM_LIT:1> ] . tb_lineno ) , <NUM_LIT:1> , '<STR_LIT>' ) <EOL> sct . printv ( str ( e ) , <NUM_LIT:1> , '<STR_LIT>' ) <EOL> sys . exit ( <NUM_LIT:2> ) <EOL> def function_launcher ( args ) : <EOL> import importlib <EOL> script_to_be_run = importlib . import_module ( '<STR_LIT>' + args [ <NUM_LIT:0> ] ) <EOL> return script_to_be_run . test ( * args [ <NUM_LIT:1> : ] ) <EOL> def init_worker ( ) : <EOL> signal . signal ( signal . SIGINT , signal . SIG_IGN ) <EOL> def test_function ( function , folder_dataset , parameters = '<STR_LIT>' , nb_cpu = None , verbose = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> data_subjects , subjects_name = generate_data_list ( folder_dataset ) <EOL> os . environ [ "<STR_LIT>" ] = "<STR_LIT:1>" <EOL> from multiprocessing import Pool <EOL> import itertools <EOL> data_and_params = itertools . izip ( itertools . repeat ( function ) , data_subjects , itertools . repeat ( parameters ) ) <EOL> pool = Pool ( processes = nb_cpu , initializer = init_worker ) <EOL> try : <EOL> async_results = pool . map_async ( function_launcher , data_and_params ) . get ( <NUM_LIT> ) <EOL> pool . close ( ) <EOL> pool . join ( ) <EOL> results = process_results ( async_results , subjects_name , function , folder_dataset , parameters ) <EOL> except KeyboardInterrupt : <EOL> print "<STR_LIT>" <EOL> pool . terminate ( ) <EOL> pool . join ( ) <EOL> sys . exit ( <NUM_LIT:2> ) <EOL> except Exception as e : <EOL> sct . printv ( '<STR_LIT>' . format ( sys . exc_info ( ) [ - <NUM_LIT:1> ] . tb_lineno ) , <NUM_LIT:1> , '<STR_LIT>' ) <EOL> sct . printv ( str ( e ) , <NUM_LIT:1> , '<STR_LIT>' ) <EOL> pool . terminate ( ) <EOL> pool . join ( ) <EOL> sys . exit ( <NUM_LIT:2> ) <EOL> return results <EOL> def get_parser ( ) : <EOL> parser = Parser ( __file__ ) <EOL> parser . usage . set_description ( "<STR_LIT>" ) <EOL> parser . add_option ( name = "<STR_LIT>" , <EOL> type_value = "<STR_LIT:str>" , <EOL> description = "<STR_LIT>" , <EOL> mandatory = True , <EOL> example = "<STR_LIT>" ) <EOL> parser . add_option ( name = "<STR_LIT>" , <EOL> type_value = "<STR_LIT>" , <EOL> description = "<STR_LIT>" , <EOL> mandatory = True , <EOL> example = "<STR_LIT>" ) <EOL> parser . add_option ( name = "<STR_LIT>" , <EOL> type_value = "<STR_LIT:str>" , <EOL> description = "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> mandatory = False ) <EOL> parser . add_option ( name = "<STR_LIT>" , <EOL> type_value = "<STR_LIT:int>" , <EOL> description = "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> mandatory = False , <EOL> default_value = <NUM_LIT:0> , <EOL> example = '<STR_LIT>' ) <EOL> parser . add_option ( name = "<STR_LIT>" , <EOL> type_value = "<STR_LIT>" , <EOL> description = "<STR_LIT>" , <EOL> mandatory = False , <EOL> example = [ '<STR_LIT:0>' , '<STR_LIT:1>' , '<STR_LIT:2>' ] , <EOL> default_value = '<STR_LIT:1>' ) <EOL> return parser <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> parser = get_parser ( ) <EOL> arguments = parser . parse ( sys . argv [ <NUM_LIT:1> : ] ) <EOL> function_to_test = arguments [ "<STR_LIT>" ] <EOL> dataset = arguments [ "<STR_LIT>" ] <EOL> dataset = sct . slash_at_the_end ( dataset , slash = <NUM_LIT:1> ) <EOL> parameters = '<STR_LIT>' <EOL> if "<STR_LIT>" in arguments : <EOL> parameters = arguments [ "<STR_LIT>" ] <EOL> nb_cpu = None <EOL> if "<STR_LIT>" in arguments : <EOL> nb_cpu = arguments [ "<STR_LIT>" ] <EOL> verbose = arguments [ "<STR_LIT>" ] <EOL> start_time = time ( ) <EOL> print '<STR_LIT>' + strftime ( "<STR_LIT>" ) + '<STR_LIT:)>' <EOL> results = test_function ( function_to_test , dataset , parameters , nb_cpu , verbose ) <EOL> pd . set_option ( '<STR_LIT>' , <NUM_LIT> ) <EOL> pd . set_option ( '<STR_LIT>' , <NUM_LIT> ) <EOL> pd . set_option ( '<STR_LIT>' , <NUM_LIT:1000> ) <EOL> results_subset = results . drop ( '<STR_LIT>' , <NUM_LIT:1> ) . drop ( '<STR_LIT>' , <NUM_LIT:1> ) . drop ( '<STR_LIT>' , <NUM_LIT:1> ) . drop ( '<STR_LIT>' , <NUM_LIT:1> ) <EOL> results_display = results_subset <EOL> results_mean = results_subset [ results_subset . status != <NUM_LIT:200> ] . mean ( numeric_only = True ) <EOL> results_mean [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> results_display = results_display . append ( results_mean , ignore_index = True ) <EOL> results_std = results_subset [ results_subset . status != <NUM_LIT:200> ] . std ( numeric_only = True ) <EOL> results_std [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> results_display = results_display . append ( results_std , ignore_index = True ) <EOL> count_passed = results_subset . status [ results_subset . status == <NUM_LIT:0> ] . count ( ) <EOL> results_display = results_display . set_index ( '<STR_LIT>' ) . reset_index ( ) <EOL> print '<STR_LIT>' + function_to_test + '<STR_LIT:U+0020>' + parameters + '<STR_LIT>' <EOL> print '<STR_LIT>' + dataset <EOL> print results_display . to_string ( ) <EOL> print '<STR_LIT>' + str ( count_passed ) + '<STR_LIT:/>' + str ( len ( results_subset ) ) <EOL> elapsed_time = time ( ) - start_time <EOL> print '<STR_LIT>' + str ( int ( round ( elapsed_time ) ) ) + '<STR_LIT:s>' <EOL> print '<STR_LIT>' </s>
