3 start include statements
4 use ext
::Parallel
::ForkManager
;
15 if ( $PsN::config
-> {'_'} -> {'use_database'} ) {
21 # }}} include statements
27 # The I<retries>, I<picky>, I<nm_version>, I<seed> and I<threads>
28 # attributes can be specified as either a scalar or as an
29 # array. The scalar value will be applied to all models whereas
30 # the array holds values per model. If an array is given it must
31 # be of the same length as the number of models.
33 # The I<directory> is the folder where the tools stores
34 # temporary data and runs subtools (or in the modelfit case,
35 # runs NONMEM). If unsure of what this means, leave it undefined
36 # and a default will be used, e.g. modelfit_dir3 or something.
38 # The base_directory refers to the directory where the tool
39 # should place its own directory. Default is current directory.
41 # A more interresting attribute is I<threads> which sets how many
42 # parallel executions of NONMEM that will run. Some tips are:
43 # Setting the number of threads higher than the number of nodes in
44 # your cluster/supercomputer can make your runs slower. The
45 # biggest limiting factor is the amount of memory needed by
46 # NONMEM. With smaller runs, just set the thread number to the
47 # number of nodes available.
49 # The I<directory> is the folder where the tools stores
50 # temporary data and runs subtools (or in the modelfit case,
51 # runs NONMEM). Each NONMEM run will have its own sub directory
52 # NM_run[X] where [X] is an index running from 0 to the number of
53 # runs-1. If unsure of what this means, leave it undefined and a
54 # default will be used, e.g. modelfit_dir3 or something.
56 # Next, the I<compress> and I<remove_temp_files> attributes are good
57 # if you want to save some hard disk space. I<compress> set to 1
58 # will put all NONMEM output in to an tar/gz archive named
59 # I<problem_files.tgz> placed in the I<NM_run[X]> directory
60 # described above. If I<remove_temp_files> is set to 1, the NONMEM
61 # files: 'FCON', 'FDATA', 'FSTREAM', 'PRDERR' will be removed.
63 # I<clean> is a stronger version of I<remove_temp_files>; it will also
64 # remove I<NM_run[X]> and all that is in these.
66 # I<retries> is the number of times L</run> will alter initial
67 # values and (re)execute NONMEM when executions fail. I<retries>
68 # can either be an integer, specifying the number of retries for
69 # all models, or it can be an array with the number of retries
70 # specific for each modelfile as elements. The default value is
71 # B<5>. The algorithm for altering the initial values works
72 # roughly like this: For each each new try, a random new initial
73 # value is drawn from a uniform distribution with limits +-n*10%
74 # of the original intial estimate and where n i equal to the retry
75 # number. I.e. the first retry, the borders of the distribution
76 # are +-10%. The algorithm ensures that the new values are within
77 # specified boundaries.
81 # For a full dexcription of the algorithm, see <a
82 # href="model/problem/record/init_option.html#set_random_init">set_random_init</a>
84 # href="model/problem/record/init_option.html">init_option
91 # For a full dexcription of the algorithm, see I<set_random_init>
92 # of the I<init_option> class.
96 # If I<picky> is set to 1, the output from NONMEM will be checked
97 # more thoroughly. If any of the lines below are found in the
98 # minimization message, a rerun is initiated.
100 # ESTIMATE OF THETA IS NEAR THE BOUNDARY AND
101 # PARAMETER ESTIMATE IS NEAR ITS BOUNDARY
102 # R MATRIX ALGORITHMICALLY SINGULAR
103 # S MATRIX ALGORITHMICALLY SINGULAR
105 # I<nm_version> is a string with the version number of NONMEM that
106 # will be used. The installed versions of NONMEM must be specified
107 # in OSspecific.pm, the class responsible for system specific
110 # I<logfile> specifies the name of the logfile.
112 # If I<debug> is set to 1(true), (many!) debug messages will be
115 # I<extra_files> is an array of strings where each string is a
116 # file needed for NONMEM execution. Those file will be moved
117 # to the I<NM_run[X]> directory.
119 # I<seed> is just a way to set a seed number.
121 # If a directory is given as argument to a tool, it will extract
122 # all information about what has already been run in this
123 # directory and continue there. If nothing is left to do, it will
124 # still produce the output as a normal run would. This is useful
125 # both for resuming crashed runs as well as for extracting
126 # information form an old run.
128 $this -> {'seed'} = defined $parm{'seed'} ?
$parm{'seed'} : random_uniform_integer
(1,0,10000000);
130 #Initiate the random generator if a seed is given (which it is, see above)
131 random_set_seed_from_phrase
( $this -> {'seed'} );
133 # The base_directory refers to the directory where the tool should place its own
135 if ( defined $parm{'base_directory'} ) {
136 $this -> {'base_directory'} = $parm{'base_directory'};
138 my ($uniquePath, $file) = OSspecific
::absolute_path
( '', '' );
139 $this -> {'base_directory'} = $uniquePath;
142 my @tool_name_full = split( '::', ref $this );
143 my $tool_name = $tool_name_full[$#tool_name_full];
145 # The directory is the folder where the tools stores temporary data and
146 # runs subtools (or in the modelfit case, runs NONMEM)
147 if ( defined $parm{'directory'} ) {
149 ( $this -> {'directory'}, $dummy ) = OSspecific
::absolute_path
( $parm{'directory'}, '');
152 $this -> {'directory'} =
153 OSspecific
::unique_path
( $tool_name.'_dir' ,
154 $this -> {'base_directory'} );
158 # Create my temporary directory
160 if ( $PsN::config
-> {'_'} -> {'use_database'} ) {
161 my( $found_log, $found_tool_id ) = $this -> read_log
;
163 $this -> register_in_database
unless ( $found_tool_id );
165 $this -> log_object
unless ( $found_log and $found_tool_id );
168 debug
-> die( message
=> "No model specified!" )
169 unless ( defined $this -> {'models'} and scalar @
{$this -> {'models'}} > 0 );
170 foreach my $mod ( @
{$this -> {'models'}} ) {
171 debug
-> die( message
=> "Supplied argument model is not defined" )
174 # Make sure that the filenames are absolute and collect model_ids
176 foreach my $model ( @
{$this -> {'models'}} ) {
177 my $model_id = $model -> model_id
;
178 if( not defined $model_id ) {
179 $model_id = $model -> register_in_database
;
181 my $datas = $model -> datas
;
182 if( defined $datas ) {
183 foreach my $data ( @
{$datas} ) {
184 my $data_id = $data -> data_id
;
185 if( not defined $data_id ) {
186 $data -> register_in_database
;
191 push( @model_ids, $model -> model_id
);
192 my ($directory, $filename) = OSspecific
::absolute_path
( $model -> directory
, $model -> filename
);
193 $model -> filename
( $filename );
194 $model -> directory
( $directory );
195 if ( defined $model -> outputs
) {
196 my @outputs = @
{$model -> outputs
};
197 foreach my $output ( @outputs ) {
198 my ($directory, $filename) = OSspecific
::absolute_path
( $outputs[0] -> directory
, $outputs[0] -> filename
);
199 $output -> filename
( $filename );
200 $output -> directory
( $directory );
203 if ( defined $model -> datas
) {
204 my @datas = @
{$model -> datas
};
205 foreach my $data ( @datas ) {
206 my ($directory, $filename) = OSspecific
::absolute_path
( $datas[0] -> directory
, $datas[0] -> filename
);
207 $data -> filename
( $filename );
208 $data -> directory
( $directory );
212 $this -> {'model_ids'} = \
@model_ids;
213 if ( not -e
$this -> {'directory'}."done.database.tool_models" ) {
214 $this -> register_tm_relation
( model_ids
=> \
@model_ids,
215 prepared_models
=> 0 );
216 open( DB
, ">".$this -> {'directory'}."done.database.tool_models" );
229 open( OLOG
, '>',$self -> {'directory'}.'object.txt' );
230 $Data::Dumper
::Maxdepth
= 1;
231 print OLOG Dumper
$self;
232 $Data::Dumper
::Maxdepth
= 0;
242 if( -e
$self -> {'directory'}.'object.txt' ) {
244 open( OLOG
, '<'.$self -> {'directory'}.'object.txt' );
247 for ( my $i = 1; $i < $#olog; $i++ ) {
248 $str = $str.$olog[$i];
251 my %tmp = eval( $str );
253 if( exists $tmp{'tool_id'} ) {
254 $self -> {'tool_id'} = $tmp{'tool_id'};
263 # {{{ register_in_database
265 start register_in_database
267 if ( $PsN::config
-> {'_'} -> {'use_database'} ) {
268 my @tool_name_full = split( '::', ref $self );
269 my $tool_name = $tool_name_full[$#tool_name_full];
270 my ( $date_str, $time_str );
271 if ( $Config{osname
} eq 'MSWin32' ) {
272 $date_str = `date /T`;
273 $time_str = ' '.`time /T`;
280 my $date_time = $date_str.$time_str;
281 # Backslashes messes up the sql syntax
282 my $dir_str = $self->{'directory'};
283 $dir_str =~ s/\\/\//g
;
285 my $dbh = DBI
-> connect("DBI:mysql:host=".$PsN::config
-> {'_'} -> {'database_server'}.
286 ";databse=".$PsN::config
-> {'_'} -> {'project'},
287 $PsN::config
-> {'_'} -> {'user'},
288 $PsN::config
-> {'_'} -> {'password'},
289 {'RaiseError' => 1});
291 if ( defined $self -> {'parent_tool_id'} ) {
292 # print "INSERT INTO tool (parent_tool_id,name,date,directory) ".
293 # "VALUES (".$self -> {'parent_tool_id'}.", '".
294 # "$tool_name', '$date_time', '$dir_str' )\n";
295 $sth = $dbh -> prepare
("INSERT INTO ".$PsN::config
-> {'_'} -> {'project'}.
296 ".tool (parent_tool_id,name,date,directory) ".
297 "VALUES (".$self -> {'parent_tool_id'}.", '".
298 "$tool_name', '$date_time', '$dir_str' )");
300 $sth = $dbh -> prepare
("INSERT INTO ".$PsN::config
-> {'_'} -> {'project'}.
301 ".tool (name,date,directory) ".
302 "VALUES ('$tool_name', '$date_time', '$dir_str' )");
305 $self -> {'tool_id'} = $sth->{'mysql_insertid'};
310 end register_in_database
312 # }}} register_in_database
314 # {{{ register_tm_relation
316 start register_tm_relation
317 if ( $PsN::config
-> {'_'} -> {'use_database'} and
318 defined $self -> {'tool_id'} and $#model_ids >= 0 ) {
319 my $dbh = DBI
-> connect("DBI:mysql:host=".$PsN::config
-> {'_'} -> {'database_server'}.
320 ";databse=".$PsN::config
-> {'_'} -> {'project'},
321 $PsN::config
-> {'_'} -> {'user'},
322 $PsN::config
-> {'_'} -> {'password'},
323 {'raiseerror' => 1});
326 my $columns = "( tool_id, model_id, prepared_model )";
327 foreach my $model_id ( @model_ids ) {
328 if ( defined $model_id ) {
329 $values = $values."," if ( defined $values );
330 if( $prepared_models ) {
331 $values = $values."(".$self -> {'tool_id'}.", $model_id, 1 )";
333 $values = $values."(".$self -> {'tool_id'}.", $model_id, 0 )";
337 $sth = $dbh -> prepare
( "INSERT INTO ".$PsN::config
-> {'_'} -> {'project'}.
338 ".tool_model $columns VALUES $values" );
340 $sth -> finish
if ( defined $sth );
343 end register_tm_relation
345 # }}} register_tm_relation
351 #%{$tool} = %{$self};
352 #$tool -> {'models'} = undef;
353 #$tool -> {'tools'} = undef;
354 #@{$tool -> {'models'}} = ();
355 #@{$tool -> {'tools'}} = ();
356 #foreach my $model ( @{$self -> {'models'}} ) {
357 # push( @{$tool -> {'models'}}, $model -> copy );
359 #foreach my $subtool ( @{$self -> {'tools'}} ) {
360 # push( @{$tool -> {'tools'}}, $subtool -> copy );
362 #bless( $tool, ref( $self ) );
364 # ! NOTE ! This is not a deep copy ! NOTE !
365 # This function has now been replaced with "copying" the "reference object" in new().
367 # my $all_parameters = {};
369 # foreach my $valid_p ( keys %{ $self -> {'__valid_parameters'} } ){
370 # if( defined $self -> {$valid_p} ) {
371 # $all_parameters -> {$valid_p} = $self -> {$valid_p};
375 # %{$all_parameters} = (%{$all_parameters}, %parameters);
378 # $Data::Dumper::Maxdepth = 3;
379 # print Dumper( $all_parameters );
380 # $Data::Dumper::Maxdepth = 0;
382 # my $tool_string = ref $self;
384 # $tool = "$tool_string" -> new( %{$all_parameters} );
394 # Runs the pre_fork_setup specific for the subtool
395 my $sub_pre_fork_setup = $self -> {'subtools'} -> [0];
396 if ( defined $sub_pre_fork_setup ) {
397 $sub_pre_fork_setup = $sub_pre_fork_setup.'_pre_fork_setup';
398 if ( defined( $self -> can
( $sub_pre_fork_setup ) ) ) {
399 $self -> $sub_pre_fork_setup;
412 # Run the print_results specific for the subtool
413 my $sub_print_results = $self -> {'subtools'} -> [0];
414 if ( defined $sub_print_results ) {
418 my $size_ref = shift;
420 if ( defined $arr and ref($arr) eq 'ARRAY' ) {
421 push( @
{$size_ref}, scalar @
{$arr} );
422 ( $dim, $size_ref ) = get_dim
( $arr->[0], $dim, $size_ref );
424 return ( $dim, $size_ref );
428 if ( not defined $val or $val eq '' ) {
429 return sprintf("%10s",$PsN::output_style
).',';
432 my $nodot = /.*\..*/ ?
0 : 1;
434 if ( /.*\D+.*/ or $nodot) {
435 return sprintf("%10s",$val).',';
437 return sprintf("%10.5f",$val).',';
441 debug
-> die( message
=> "No results_file defined" )
442 unless ( defined $self -> {'results_file'} );
443 open ( RES
, ">".$self -> {'directory'}.$self -> {'results_file'} );
444 if ( defined $self -> {'results'} ) {
445 my @all_results = @
{$self -> {'results'}};
446 for ( my $i = 0; $i <= $#all_results; $i++ ) {
447 if ( defined $all_results[$i]{'own'} ) {
448 my @my_results = @
{$all_results[$i]{'own'}};
449 for ( my $j = 0; $j <= $#my_results; $j++ ) {
450 # These size estimates include the problem and sub_problem dimensions:
451 my ( $ldim, $lsize_ref ) = get_dim
( $my_results[$j]{'labels'}, -1, [] );
452 my ( $vdim, $vsize_ref ) = get_dim
( $my_results[$j]{'values'}, -1, [] );
453 print RES
$my_results[$j]{'name'},"\n" if ( $vdim > 1 );
455 if ( defined $my_results[$j]{'values'} and
456 scalar @
{$my_results[$j]{'values'}} >= 0 ) {
457 my @values = @
{$my_results[$j]{'values'}};
459 if ( defined $my_results[$j]{'labels'} and
460 scalar @
{$my_results[$j]{'labels'}} >= 0 ) {
461 @labels = @
{$my_results[$j]{'labels'}};
463 # Print Header Labels
465 my $label = \
@labels;
466 print RES
','.format_value
($label),"\n";
467 } elsif ( $ldim == 2 ) {
469 for ( my $n = 0; $n < scalar @
{$labels[1]}; $n++ ) {
470 my $label = $labels[1][$n];
471 print RES format_value
($label);
475 # print RES "\n" if ( scalar @{$labels[1]} );
479 print RES
','.format_value
(\
@values),"\n";
480 } elsif ( $vdim == 1 ) {
481 for ( my $m = 0; $m < scalar @values; $m++ ) {
482 my $label = $labels[$m];
483 print RES
','.format_value
($label);
484 my $val = $values[$m];
485 print RES
','.format_value
($val),"\n";
487 } elsif ( $vdim == 2 ) {
488 for ( my $m = 0; $m < scalar @values; $m++ ) {
491 $label = $labels[$m];
492 } elsif ( $ldim == 2 ) {
493 $label = $labels[0][$m];
495 print RES format_value
($label);
496 if( defined $values[$m] ){
497 for ( my $n = 0; $n < scalar @
{$values[$m]}; $n++ ) {
498 print RES format_value
($values[$m][$n]);
511 debug
-> warn( level
=> 2,
512 message
=> "No subtools defined".
513 ", using default printing routine" );
520 # {{{ post_fork_analyze
522 start post_fork_analyze
524 # Runs the post_fork_analyze specific for the subtool
525 my $sub_post_fork_analyze = $self -> {'subtools'} -> [0];
526 if ( defined $sub_post_fork_analyze ) {
527 $sub_post_fork_analyze = $sub_post_fork_analyze.'_post_fork_analyze';
528 if ( defined( $self -> can
( $sub_post_fork_analyze ) ) ) {
529 $self -> $sub_post_fork_analyze;
532 if ( defined $self -> {'results_file'} ) {
533 #$self -> print_results;
536 end post_fork_analyze
538 # }}} post_fork_analyze
544 $self -> _prepare_model
( model_number
=> $model_number );
546 # Run the setup specific for the subtool
547 my $sub_setup = $self -> {'subtools'} -> [0];
548 if ( defined $sub_setup ) {
549 $sub_setup = $sub_setup.'_setup';
550 $self -> $sub_setup( model_number
=> $model_number );
560 mkdir( $self -> {'directory'} ) unless ( -e
$self -> {'directory'} );
576 # | |->{own} The results from this tool on the first model
580 # | | | |->{name} e.g. 'parameter.estimates'
584 # | | | | |->[0]... e.g. ['TH1', 'TH2', 'TH3'] indexed on problem and sub problem
587 # | | | | |->[#problems]
591 # | | | |->[0] e.g. [0.21, 20.3, 3] indexed as above
594 # | | | |->[#problems]
598 # | | | |->{name} e.g. 'standard.errors'
601 # | |->{subtools} The results from the subtools on the first model
603 # | |->[0] First sub tool
605 # | | |->[0] First model of the prepared models sent to the first sub tool
607 # | | | |->{own} The first sub tools results on the first model
609 # | | | | |->[0] First result type
611 # | | | | | |->{name}
612 # | | | | | |->{labels}
613 # | | | | | |->{values}
615 # | | | | |->[1] Second result type
617 # | | | | | |->{name}
618 # | | | | | |->{labels}
619 # | | | | | |->{values}
620 # | | | |->{subtools} Another tool level
622 # | | |->[1] Second model of the prepared models sent to the first sub tool
624 # | | | |->{own} The first sub tools results on the second model
626 # | | | | |->[0] First result type
628 # | | | | | |->{name}
629 # | | | | | |->{labels}
630 # | | | | | |->{values}
632 # | | | | |->[1] Second result type
634 # | | | | | |->{name}
635 # | | | | | |->{labels}
636 # | | | | | |->{values}
637 # | | | |->{subtools} Another tool level
640 # | | |->[#prepared models] Last model of the prepared models sent to the first sub tool
642 # | | | |->{own} The first sub tools results on the last model
644 # | | | | |->[0] First result type
646 # | | | | | |->{name}
647 # | | | | | |->{labels}
648 # | | | | | |->{values}
650 # | | | | |->[1] Second result type
652 # | | | | | |->{name}
653 # | | | | | |->{labels}
654 # | | | | | |->{values}
655 # | | | |->{subtools} Another tool level
657 # | |->[1] Second sub tool
659 # | |->[#tools] Last sub tool
661 # |->[1] Second model. All above repeated for this model.
663 # |->[#models] Last model. As above.
665 # Prepared_models structure:
671 # | |->{own} The prepared models of this tool using the first model as base
673 # | | |->[0] First prep model
674 # | | |->[1] Second prep model
676 # | | |->[#prep_models] Last prep model
678 # | |->{subtools} The prepared models of the subtools on the first model. Only one sub tool per prepared model above.
680 # | |->[0] First model of the models (prepared above) sent to the first sub tool
682 # | | |->{own} The first sub tools prepared models on its first model
684 # | | | |->[0] First prep model
685 # | | | |->[1] Second prep model
687 # | | | |->[#prep_models]Last prep model
691 # | |->[1] Second model of the models (prepared above) sent to the first sub tool
693 # | | |->{own} The first sub tools prepared models on its second model
695 # | | | |->[0] First prep model
696 # | | | |->[1] Second prep model
698 # | | | |->[#prep_models]Last prep model
707 my $return_dir = getcwd
();
708 chdir( $self -> {'directory'} );
710 $self -> pre_fork_setup
;
712 my @models = @
{$self -> {'models'}};
713 # Use the thread number of this tool level:
714 my $threads = ref( $self -> {'threads'} ) eq 'ARRAY' ?
715 $self -> {'threads'} -> [0] : $self -> {'threads'};
717 # No point in using more threads than models
718 $threads = $#models + 1 if ( $threads > $#models + 1);
720 # Currently parallel execution is not supported on windows platforms
721 $threads = 1 if( $Config{osname
} eq 'MSWin32' );
723 # Create new forkmanager
724 my $pm = ext
::Parallel
::ForkManager
-> new
($threads) if ( $threads > 1 );
726 $pm -> run_on_finish
( sub { my ( $pid, $exit_code, $ident ) = @_;
728 debug
-> die( message
=> "Subtool died, exiting." );
730 } ) if ( $threads > 1 );
732 # Store some globals for single-thread mode to make each loop
733 # over the models see the same (fresh) prepared attributes as
734 # in the parallel mode.
737 # THREAD if ( $threads == 1 ) {
738 # THREAD if ( defined $self -> {'tools'} ) {
739 # THREAD @pre_fork_tools = @{$self -> {'tools'}};
743 # Loop over the models
744 for ( my $i = 1; $i <= scalar @models; $i++ ) {
745 # Spawn new processes
746 $pm -> start
and next if ( $threads > 1 );
748 # model_number is a member that tells the tool which model
749 # it is currently working on.
750 $self -> model_number
( $i );
752 # Reset some globals: (only needed for threads==1)
753 # THREAD if ( $threads == 1 && defined $self -> {'tools'}) {
754 # THREAD @{$self -> {'tools'}} = @pre_fork_tools;
757 # Make sure that each process gets a unique random sequence:
758 random_set_seed_from_phrase
(random_uniform_integer
(1,0,10000*$i));
759 # srand(rand()*10000*$i);
762 $self -> setup
( model_number
=> $i );
765 my @tool_results = ();
766 my @tool_models = ();
767 if ( defined $self -> {'tools'} ) {
768 foreach my $tool (@
{$self -> {'tools'}}){
769 # There is to date (2004-01-27 no tool that creates more than one internal
770 # tool. Hence this is a loop of one cycle. But to be general, again...
772 my( $returns, $prep_models ) = $tool -> run
;
773 # push the sub tool's return values
774 push ( @tool_results, $returns );
775 if ( defined $prep_models ) {
776 push ( @tool_models, $prep_models );
778 'debug' -> warn(level
=> 1,
779 message
=> "inside " . ref($self) . " but no prep_models defined from $tool $i");
781 $self -> post_subtool_analyze
;
785 debug
-> die( message
=> "No tool object to run from tool object." );
788 $self -> {'results'}[$i-1]{'subtools'}= \
@tool_results;
789 $self -> {'prepared_models'}[$i-1]{'subtools'} = \
@tool_models;
791 # Analyze the results
792 $self -> analyze
( model_number
=> $i );
794 Storable
::store
( $self -> {'prepared_models'},
795 $self -> {'directory'}."/m$i/prepared_models.log" );
796 if ( $threads > 1 ) {
797 Storable
::store
( $self -> {'results'},
798 $self -> {'directory'}."/m$i/results.log" );
799 # Maybe redundant to transfer back both prepared_models as well as tools
801 # Actually, by principle everything interesting for
802 # a parent should be placed in "results" or possibly
805 #Storable::store( $self -> {'tools'},
806 # $self -> {'directory'}."/m$i/tools.log" );
808 $pm -> finish
if ( $threads > 1 );
810 $pm -> wait_all_children
if ( $threads > 1 );
812 for( my $i = 1; $i <= scalar @
{$self -> {'models'}}; $i++ ) {
813 my @prepared_models = @
{Storable
::retrieve
( $self -> {'directory'}.
814 "/m$i/prepared_models.log" )};
815 unlink( $self -> {'directory'} . "/m$i/prepared_models.log" );
816 $self->{'prepared_models'}[$i-1] = $prepared_models[$i-1];
819 if ( $threads > 1 ) {
820 for( my $i = 1; $i <= scalar @
{$self -> {'models'}}; $i++ ) {
821 my @model_results = @
{Storable
::retrieve
( $self -> {'directory'}.
822 "/m$i/results.log" )};
823 # It is important to keep the number of dimensions: push the first value, not the
825 $self->{'results'}[$i-1] = $model_results[$i-1];
827 # Read comment aboud tools.log near storable above.
829 #push( @{$self -> {'tools'}},
830 # Storable::retrieve( $self -> {'directory'}.
831 # "/m$i/tools.log" ) );
836 # Perform analyses that need to be done after all models have
837 # been run and processed. Also write a result file if one is
839 $self -> post_fork_analyze
;
842 # @results = @{$self -> {'results'}};
843 # @prepared_models = @{$self -> {'prepared_models'}};
854 my ($newdir, $newfile) = OSspecific
::absolute_path
( $self -> {'directory'} . '/m'.$model_number, '' );
855 debug
-> warn( level
=> 2,
856 message
=> "Making directory\t\t" . $newdir );
867 $self -> {'raw_results'}[$model_number-1] =
868 $self -> {'tools'} -> [0] -> raw_results
;
869 my $sub_analyze = $self -> {'subtools'} -> [0];
870 if ( defined $sub_analyze ) {
871 $sub_analyze = $sub_analyze.'_analyze';
872 if( defined $self -> can
( $sub_analyze ) ){
873 $self -> $sub_analyze( model_number
=> $model_number );
881 # {{{ _modelfit_raw_results_callback
883 start _modelfit_raw_results_callback
886 OSspecific
::absolute_path
( $self -> {'directory'},
887 $self -> {'raw_results_file'}[$model_number-1] );
888 my ($dir,$nonp_file) =
889 OSspecific
::absolute_path
( $self -> {'directory'},
890 $self -> {'raw_nonp_file'}[$model_number-1] );
892 my $modelfit = shift;
893 $modelfit -> raw_results_file
( $dir.$file );
894 $modelfit -> raw_nonp_file
( $dir.$nonp_file );
898 end _modelfit_raw_results_callback
900 # }}} _modelfit_raw_results_callback
902 # {{{ read_raw_results
903 start read_raw_results
905 undef $self -> {'raw_results_header'};
906 for ( my $i = 1; $i <= scalar @
{$self->{'models'}}; $i++ ) { # All models
907 if ( -e
$self -> {'directory'}.'raw_results'.$i.'.csv' ) {
908 open( RRES
, $self -> {'directory'}.'raw_results'.$i.'.csv' );
911 map { chomp; my @tmp = split(',',$_); $_ = \
@tmp } @file ;
912 $self -> {'raw_results_header'} -> [$i-1] = shift @file;
913 $self -> {'raw_results'} -> [$i-1] = \
@file;
915 if ( -e
$self -> {'directory'}.'raw_nonp_results'.$i.'.csv' ) {
916 open( RRES
, $self -> {'directory'}.'raw_nonp_results'.$i.'.csv' );
919 map { chomp; my @tmp = split(',',$_); $_ = \
@tmp } @file ;
920 $self -> {'raw_nonp_results'} -> [$i-1] = \
@file;
925 # }}} read_raw_results
927 # {{{ create_raw_results_rows
928 start create_raw_results_rows
930 my @probs = @
{$model -> {'outputs'} -> [0] -> problem_structure
};
931 my $np = scalar @probs; # #probs
933 my @params = ( 'theta', 'omega', 'sigma', 'npomega', 'shrinkage_etas', 'shrinkage_wres','eigen' );
934 # ------------ Push model, problem and sub-problem numbers --------------
936 for( my $j = 0; $j < $np; $j++ ) {
937 my $ns = $probs[$j]; # #subprobs
938 for( my $k = 0; $k < $ns; $k++ ) {
939 my $row = $model_row++;
940 push( @
{$return_rows[$row]}, ($model_number,($j+1),($k+1)) );
944 # --------------------- Loop all result categories ----------------------
947 foreach my $category ( @
{$self -> {'raw_results_header'}},'npomega' ){
948 next if( $category eq 'model' or $category eq 'problem' or $category eq 'subproblem' );
949 my $model_row = 0; # Need to mask previous definition of model_row
950 my ( $accessor, $res );
952 # {{{ Get the values for the category
954 if ( $category eq 'theta' or $category eq 'omega' or $category eq 'sigma' or
955 $category eq 'setheta' or $category eq 'seomega' or $category eq 'sesigma' or
956 $category eq 'npomega' or $category eq 'eigen' ) {
957 $accessor = $category.'s';
958 $res = $model -> {'outputs'} -> [0] -> $accessor;
960 if( defined $res and ref $res eq 'ARRAY' and $category ne 'npomega' ){
962 foreach my $prob ( @
{$res} ){
963 if( defined $prob and ref $prob eq 'ARRAY' and $prob -> [0] and ref $prob -> [0] eq 'ARRAY' ){
964 $raw_line_structure{ $model_number+$prob_num }{ $category } = scalar @
{$return_rows[$model_number+$prob_num]}+$position
965 . ",". scalar @
{$prob -> [0]};
971 } elsif ( $category eq 'shrinkage_etas' ) {
972 # Shrinkage does not work for subproblems right now.
973 $res = $model -> eta_shrinkage
;
974 } elsif ( $category eq 'shrinkage_wres' ) {
975 # Shrinkage does not work for subproblems right now.
976 # get ofv just to get the prob-subp structure
977 $res = $model -> wres_shrinkage
;
979 $accessor = $category;
980 $res = $model -> {'outputs'} -> [0] -> $accessor;
983 # }}} Get the values for the category
985 if( $category eq 'npomega' ){
986 $results_file = 'raw_nonp_results';
989 $results_file = 'raw_results';
993 for( my $j = 0; $j < $np; $j++ ) {
994 my $ns = $probs[$j]; # #subprobs
995 if( defined $res -> [$j] ) {
996 for( my $k = 0; $k < $ns; $k++ ) {
997 my $row = $model_row++;
998 if( ref $res -> [$j] eq 'ARRAY' ){
999 if( defined $res -> [$j][$k] ) {
1000 if ( ref $res -> [$j][$k] eq 'ARRAY' ) {
1001 push( @
{$return_rows[$row]}, @
{$res -> [$j][$k]} );
1002 if( scalar @
{$res -> [$j][$k]} > $max_hash -> {$category} ){
1003 $max_hash -> {$category} = scalar @
{$res -> [$j][$k]};
1005 push( @
{$return_rows[$row]},
1006 (undef) x
($max_hash -> {$category} - scalar @
{$res -> [$j][$k]}) );
1009 push( @
{$return_rows[$row]}, $res -> [$j][$k] );
1012 push( @
{$return_rows[$row]},
1013 (undef) x
$max_hash -> {$category} );
1016 push( @
{$return_rows[$row]},
1022 # {{{ Push undefs for missing subprobs
1024 for( my $k = 0; $k < $ns; $k++ ) {
1025 my $row = $model_row++;
1026 push( @
{$return_rows[$row]},
1027 (undef) x
$max_hash -> {$category} );
1030 # }}} Push undefs for missing subprobs
1037 # {{{ Push undefs for missing probs/subprobs
1039 for( my $j = 0; $j < $np; $j++ ) {
1040 my $ns = $probs[$j]; # #subprobs
1041 for( my $k = 0; $k < $ns; $k++ ) {
1042 my $row = $model_row++;
1043 push( @
{$return_rows[$row]},
1044 (undef) x
$max_hash -> {$category} );
1048 # }}} Push undefs for missing probs/subprobs
1053 $model -> {'outputs'} -> [0] -> flush
;
1055 if( $create_header ) {
1057 # {{{ Create a header
1059 foreach my $category ( @
{$self -> {'raw_results_header'}},'npomega' ){
1061 foreach my $param ( @params ) {
1062 if ( $category eq $param ) {
1063 if( defined $max_hash -> {$param} ){
1064 for ( my $i = 1; $i <= $max_hash -> { $category }; $i++ ) {
1065 push ( @new_names, uc(substr($category,0,2)).$i );
1070 if ( $category eq 'se'.$param ) {
1071 if( defined $max_hash -> {$param} ){
1072 for ( my $i = 1; $i <= $max_hash -> { $category }; $i++ ) {
1073 push ( @new_names, uc(substr($category,2,2)).$i );
1075 map ( $_ = 'se'.$_, @new_names );
1080 if ( $#new_names >= 0 ) {
1081 push( @new_header, @new_names );
1083 push( @new_header, $category );
1093 end create_raw_results_rows
1096 # {{{ post_subtool_analyze
1098 start post_subtool_analyze
1100 my $sub_analyze = $self -> {'subtools'} -> [0];
1101 if ( defined $sub_analyze ) {
1102 $sub_analyze = $sub_analyze.'_post_subtool_analyze';
1103 if( defined $self -> can
( $sub_analyze ) ){
1104 $self -> $sub_analyze( model_number
=> $model_number );
1108 end post_subtool_analyze
1116 # # Run the results specific for the subtool
1117 # my $sub_results = $self -> {'subtools'} -> [0];
1118 # if ( defined $sub_results ) {
1119 # $sub_results = $sub_results.'_results';
1120 # @results = @{ $self -> $sub_results( accessor => $accessor, format => $format ) };
1127 # {{{ harvest_output
1129 start harvest_output
1132 # harvest_output is a complement to AUTOLOAD below. AUTOLOAD is
1133 # currently used to find the AUTOLOAD:ed accessor in any
1134 # existing subtool, model, data or outputobject. It is
1135 # inefficient in that it will have to be called for once for
1136 # each accessor. harvest_output will take a list of accessors
1137 # that it will search for in each object, saving time and
1138 # foremost; memory. Also it will take arguments such as
1139 # "search_models", "search_subtools" that will make things more
1140 # efficient if you know where to search.
1142 unless( $search_models + $search_output + $search_data <= 1 ){
1143 'debug' -> die( message
=> "This is a PsN bug: Only one of the 'search_' options can and must be specified.".
1144 "\t search_models: $search_models\n".
1145 "\t search_data: $search_data\n".
1146 "\t search_output: $search_output");
1149 if ( $search_subtools ) {
1150 'debug' -> warn( level
=> 1,
1151 message
=> "\n\nSearching subtools, which is a very untested functionality!!\n\n" );
1153 # if ( defined $self -> {'tools'} ) {
1154 # my @tools = @{$self -> {'tools'}};
1155 # foreach my $tool_ref ( @tools ) {
1156 # foreach my $tool ( @{$tool_ref} ) {
1157 # if ( $tool -> can( $accessor ) ) {
1158 # push( @result, $tool -> $accessor( %accessor_parameters ) );
1160 # 'debug' -> die(message => "Accessor $accessor is not available in the tool " . ref($tool) );
1165 # 'debug' -> warn( level => 1,
1166 # message => "Supposed to be run by the sub tools but no sub tools were defined" );
1171 sub models_traverse2
{
1172 my %parameters = @_;
1173 my @models = $parameters{'models'} ? @
{$parameters{'models'}} : ();
1174 my $search_models = $parameters{'search_models'};
1175 my $search_output = $parameters{'search_output'};
1176 my $search_data = $parameters{'search_data'};
1177 my $accessor_parameters = $parameters{'accessor_parameters'};
1178 my $accessors = $parameters{'accessors'};
1181 for( my $i = 0; $i < scalar (@models); $i++ ){
1183 foreach my $model ( @
{$models[$i]{'own'}} ) {
1185 foreach my $accessor( @
{$accessors} ) {
1187 if( $search_models and $model -> can
( $accessor ) ) {
1188 push( @
{$results{$accessor}[$i]{'own'}}, $model -> $accessor( %{$accessor_parameters} ) );
1190 } elsif( $search_data and $model -> datas
-> [0] -> can
( $accessor ) ) {
1191 push( @
{$results{$accessor}[$i]{'own'}}, $model -> datas
-> [0] -> $accessor( %{$accessor_parameters} ) );
1193 } elsif( $search_output and $model -> outputs
-> [0] -> can
( $accessor ) ) {
1194 push( @
{$results{$accessor}[$i]{'own'}}, $model -> outputs
-> [0] -> $accessor( %{$accessor_parameters} ) );
1197 'debug' -> die( message
=> "Neither model, data, output have a method for $accessor" );
1200 if ( defined $models[$i]{'subtools'} ) {
1201 push( @
{$results{$accessor}[$i]{'subtools'}}, models_traverse2
( models
=> $models[$i]{'subtools'} ) );
1206 $model -> datas
-> [0] -> flush
();
1208 if( $search_output ){
1209 $model -> outputs
-> [0] -> flush
();
1220 if ( $search_original_models ) {
1221 @models = @
{$self -> {'models'}};
1222 } elsif ( defined $self -> {'prepared_models'} ) {
1223 @models = @
{$self -> {'prepared_models'}};
1225 'debug' -> warn( level
=> 2,
1226 message
=> "Trying @accessors, but no prepared models available" );
1230 %result = %{models_traverse2
( models
=> \
@models,
1231 search_models
=> $search_models,
1232 search_output
=> $search_output,
1233 search_data
=> $search_data,
1234 accessor_parameters
=> \
%accessor_parameters,
1235 accessors
=> \
@accessors )};
1245 debug
-> warn( level
=> 2,
1246 message
=> "Caught method $AUTOLOAD" );
1247 debug
-> warn( level
=> 2,
1248 message
=> "arguments: @_" );;
1250 my $original_models = $parm{'original_models'};
1251 delete( $parm{'original_models'} );
1252 my $class = $parm{'class'};
1253 $AUTOLOAD =~ s/.*://;
1254 return if $AUTOLOAD eq 'DESTROY';
1256 # TODO: Kolla att orginalmodellen körs med submetod i run!!!!! kolla också var resultaten
1259 if ( $class =~ /tool::/ ) {
1260 delete( $parm{'mod_array'} );
1261 delete( $parm{'original_models'} );
1262 delete( $parm{'class'} );
1264 if ( defined $self -> {'tools'} ) {
1265 my @tools = @
{$self -> {'tools'}};
1266 my $accessor = $AUTOLOAD;
1267 foreach my $tool_ref ( @tools ) {
1268 foreach my $tool ( @
{$tool_ref} ) {
1269 if ( $tool -> can
( $accessor ) ) {
1270 push( @result, $tool -> $accessor( @_ ) );
1272 'debug' -> die(message
=> "Accessor $accessor is not available in the tool " . ref($tool) );
1277 print "AUTOLOAD in ",ref($self)," caught tool $AUTOLOAD. It was ",
1278 "supposed to be run by the sub tools but no sub tools were defined\n";
1283 if ( $original_models ) {
1284 @models = @
{$self -> {'models'}};
1285 } elsif ( defined $self -> {'prepared_models'} ) {
1286 'debug' -> warn(level
=> 1,
1287 message
=> "Using prepared models" );
1288 @prep_models = @
{$self -> {'prepared_models'}};
1290 print "WARNING: tool -> AUTOLOAD: Trying $AUTOLOAD, but no prepared models available\n";
1293 sub models_traverse
{
1295 my $mod_array_ref = $parm{'mod_array'};
1296 my $class = $parm{'class'};
1297 delete( $parm{'mod_array'} );
1298 delete( $parm{'class'} );
1301 @mod_array = defined $mod_array_ref ? @
{$mod_array_ref} : ();
1302 my @inner_result = ();
1304 for ( my $i = 0; $i <= $#mod_array; $i++ ) {
1305 foreach my $model ( @
{$mod_array[$i]{'own'}} ) {
1306 unless ( defined $class ) {
1307 my $mod_can = defined $model -> can
( $AUTOLOAD ) ?
1 : 0;
1308 my $out_can = (defined $model -> outputs
and
1309 defined $model -> outputs
-> [0] and
1310 defined $model -> outputs
-> [0] -> can
($AUTOLOAD))
1312 my $dat_can = (defined $model -> datas
and
1313 defined $model -> datas
-> [0] and
1314 defined $model -> datas
-> [0] -> can
($AUTOLOAD))
1316 if ( ($mod_can + $out_can + $dat_can) > 1 ) {
1318 $classes = 'model ' if $mod_can;
1319 $classes = $classes.'output ' if $out_can;
1320 $classes = $classes.'data ' if $dat_can;
1321 'debug' -> die( message
=> "Accessor $AUTOLOAD available in multiple classes: $classes" );
1323 if ( $mod_can + $out_can + $dat_can == 0 ) {
1324 'debug' -> die( message
=> "Accessor $AUTOLOAD is not available in any of the model, ".
1325 "output or data classes OR no output or data object available".
1326 " through this model" );
1329 push( @
{$inner_result[$i]{'own'}}, $model -> $AUTOLOAD( @_ ) );
1330 } elsif ( $out_can ) {
1331 push( @
{$inner_result[$i]{'own'}}, $model -> outputs
-> [0] -> $AUTOLOAD( @_ ) );
1332 } elsif ( $dat_can ) {
1333 push( @
{$inner_result[$i]{'own'}}, $model -> datas
-> [0] -> $AUTOLOAD( @_ ) );
1336 if ( $class eq 'model' ) {
1337 push( @
{$inner_result[$i]{'own'}}, $model -> $AUTOLOAD( @_ ) );
1339 my $class_accessor = $class.'s';
1340 push( @
{$inner_result[$i]{'own'}}, $model -> $class_accessor -> [0] -> $AUTOLOAD( @_ ) );
1345 if ( defined $mod_array[$i]{'subtools'} ) {
1346 push( @
{$inner_result[$i]{'subtools'}},
1347 models_traverse
( mod_array
=> $mod_array[$i]{'subtools'},
1352 return \
@inner_result;
1354 if ( $original_models ) {
1355 debug
-> warn( level
=> 2,
1356 message
=> "Traversing ".scalar $models[0]{'own'}." model(s)" );
1357 @result = @
{models_traverse
( mod_array
=> \
@models,
1360 @result = @
{models_traverse
( mod_array
=> \
@prep_models,
1369 # {{{ process_results
1371 # sub process_results {
1372 # my $res_ref = shift;
1375 # foreach my $res ( @{$res_ref} ) {
1376 # if ( ref ( $res ) eq 'ARRAY' ) {
1377 # process_results( $res, $pad );
1379 # print "HEPP ",ref($self)," $i $pad $res\n";