Merging lars changes for PsN course
[PsN.git] / bin / sumo
blob5313e2f4b64b4c88ca52d27d441034d5375d3316
1 #!/usr/bin/perl
3 use FindBin qw($Bin);
4 use lib "$Bin/../lib";
6 # Don't edit the line below, it must look exactly like this.
7 # Everything above this line will be replaced #
9 use PsN;
10 use output;
11 use debug;
12 use strict;
13 use Getopt::Long;
15 my %options;
17 $options{'precision'} = 4;
19 my $opts = { "h|?" => $options{'help'},
20 "help" => $options{'long_help'},
21 "debug:0" => $options{'debug'},
22 "debug_package:s" => $options{'debug_package'},
23 "debug_subroutine:s" => $options{'debug_subroutine'},
24 "compact_format:1" => $options{'compact_format'},
25 "csv" => $options{'csv'},
26 "precision:4" => $options{'precision'},
27 "confidence_interval" => $options{'confidence_interval'},
28 "c_level:95" => $options{'c_level'} };
31 my $res = GetOptions( \%options, %{$opts} );
33 exit unless $res;
35 if ( scalar( @ARGV ) < 1 and !($options{'help'} or $options{'long_help'}) ){
36 print "At least one list file must be specified. Use 'sumo.pl -h' for help.\n";
37 exit;
41 if($options{'help'} or $options{'long_help'}) {
42 print <<'ENDHELP';
44 sumo.pl
46 Perl script for summarizing output data.
48 Usage:
50 sumo.pl [ -h | -? ] [ -help ]
51 [ -debug='integer' ]
52 [ -debug_package='string' ]
53 [ -debug_subroutine='string' ]
54 outputfile(s)
56 ENDHELP
58 if( $options{'long_help'} and !$options{'help'} ){
60 print <<'ENDHELP';
62 Description:
64 Sumo is short for SUmmarize Output. And it does exactly that. It
65 gathers some usefull information from NONMEM ouputfiles and prints
66 a summary to screen.
68 Examples:
70 Get summary from an output file:
72 $ sumo.pl output.lst
74 Options:
76 The following options are valid:
78 -h | -?
80 Print a list of options.
83 -help
85 Print this, longer, help message.
88 -debug='integer'
90 Default value is: 0
92 This is mainly for developers who whish to debug PsN. By default
93 'integer' is zero but you can try setting it to '1' and you might
94 get some helpfull warnings. If you run in to problems that require
95 support, you might be told to crank this number up and send the
96 output to us.
99 -debug_package='string'
101 Default value is: empty string
103 If use together with '-debug' it is possible to choose which part
104 of PsN you want to see debug messages from. Again this is mostly
105 for developers.
108 -debug_subroutine='string'
110 Default value is: empty string
112 With this option it is possible to specify, with even finer
113 granularity, which part of PsN you want to see debug messages
114 from. This is definitly only for developers.
116 ENDHELP
118 exit;
121 debug -> level( $options{'debug'} );
122 debug -> package( $options{'debug_package'} );
123 debug -> subroutine( $options{'debug_subroutine'} );
125 my $form = '%.' . $options{'precision'} . 'g';
127 foreach my $outfile ( @ARGV ) {
129 my $outobj = output -> new ('filename'=> $outfile);
131 my @output_matrix;
132 my @output_matrix_sizes;
134 my %c_levels = ( '90' => 1.6449,
135 '95' => 1.96,
136 '99' => 2.5758,
137 '99.9' => 3.2905 );
139 if( $options{'confidence_interval'} ) {
140 if( not defined $c_levels{$options{'c_level'}} ) {
141 die "Sorry, confidence intervals for level ".$options{'c_level'}.
142 " can not be output. Valid levels are: ".join(',', keys %c_levels).
143 "\n";
147 #use Data::Dumper;
148 #print Dumper( $outobj );
150 print "$outfile\n\n";
152 for( my $problems = 0; $problems <= $#{$outobj -> problems}; $problems++){
153 my $row_counter = 0;
154 for( my $sub_problems = 0; $sub_problems <= $#{$outobj -> problems -> [$problems] -> subproblems}; $sub_problems++){
156 my ( %nam, %est, %cest, %ses );
157 my @thetas = defined $outobj -> thetas -> [$problems][$sub_problems] ? @{$outobj -> thetas -> [$problems][$sub_problems]} : ();
158 my @thnam = defined $outobj -> thetanames -> [$problems] ? @{$outobj -> thetanames -> [$problems]} : ();
159 my @sethet = defined $outobj -> sethetas -> [$problems][$sub_problems] ? @{$outobj -> sethetas -> [$problems][$sub_problems]} : ();
160 $nam{'theta'} = \@thnam;
161 $est{'theta'} = \@thetas;
162 $ses{'theta'} = \@sethet;
165 my @omegas = defined $outobj -> omegas -> [$problems][$sub_problems] ? @{$outobj -> omegas -> [$problems][$sub_problems]} : ();
166 my @comegas = defined $outobj -> comegas -> [$problems][$sub_problems] ? @{$outobj -> comegas -> [$problems][$sub_problems]} : ();
167 my @omnam = defined $outobj -> omeganames -> [$problems] ? @{$outobj -> omeganames -> [$problems]} : ();
168 my @seomeg = defined $outobj -> seomegas -> [$problems][$sub_problems] ? @{$outobj -> seomegas -> [$problems][$sub_problems]} : ();
169 $nam{'omega'} = \@omnam;
170 $est{'omega'} = \@omegas;
171 $cest{'omega'} = \@comegas;
172 $ses{'omega'} = \@seomeg;
174 my @sigmas = defined $outobj -> sigmas -> [$problems][$sub_problems] ? @{$outobj -> sigmas -> [$problems][$sub_problems]} : ();
175 my @csigmas = defined $outobj -> csigmas -> [$problems][$sub_problems] ? @{$outobj -> csigmas -> [$problems][$sub_problems]} : ();
176 my @signam = defined $outobj -> sigmanames -> [$problems] ? @{$outobj -> sigmanames -> [$problems]} : ();
177 my @sesigm = defined $outobj -> sesigmas -> [$problems][$sub_problems] ? @{$outobj -> sesigmas -> [$problems][$sub_problems]} : ();
178 $nam{'sigma'} = \@signam;
179 $est{'sigma'} = \@sigmas;
180 $cest{'sigma'} = \@csigmas;
181 $ses{'sigma'} = \@sesigm;
183 my $ofv = $outobj -> ofv -> [$problems][$sub_problems];
184 my $termess= $outobj -> minimization_message -> [$problems][$sub_problems];
186 if( $termess ){
187 print join( "",@{$termess} ), "\n";
190 if ( defined $ofv ) {
191 print "Objective function value: ",$ofv,"\n\n";
192 } else {
193 print "Objective function value: UNDEFINED\n\n";
196 my @cvsethet;
197 my @cvseomeg;
198 my @cvsesigm;
200 if(@sethet) {
202 if( defined $outobj -> cvsethetas -> [$problems][$sub_problems] ) {
203 @cvsethet = @{$outobj -> cvsethetas -> [$problems][$sub_problems]};
205 if( defined $outobj -> cvseomegas -> [$problems][$sub_problems] ) {
206 @cvseomeg = @{$outobj -> cvseomegas -> [$problems][$sub_problems]};
208 if( defined $outobj -> cvsesigmas -> [$problems][$sub_problems] ) {
209 @cvsesigm = @{$outobj -> cvsesigmas -> [$problems][$sub_problems]};
214 push( @{$output_matrix[$row_counter]}, "","THETA","","","OMEGA","","","SIGMA", "" );
215 for( my $i = 0; $i <= $#{$output_matrix[$row_counter]}; $i++ ){
216 if( $output_matrix_sizes[$i] < length( $output_matrix[$row_counter][$i] ) ){
217 $output_matrix_sizes[$i] = length( $output_matrix[$row_counter][$i] );
220 $row_counter++;
222 #printf "%-4s %-29s %-29s %-18s\n"," ","THETA","OMEGA","SIGMA";
224 my $max_par = $#thetas;
225 $max_par = $#omegas if ( $#omegas > $max_par );
226 $max_par = $#sigmas if ( $#sigmas > $max_par );
228 for ( my $i = 0; $i <= $max_par; $i++ ) {
229 my ( @row, %cis );
230 if( $options{'confidence_interval'} ) {
231 foreach my $param ( 'theta', 'omega', 'sigma' ) {
232 if ( defined $est{$param}[$i] ) {
233 my $diff = $c_levels{$options{'c_level'}}*$ses{$param}[$i];
234 my ( $lo, $up );
235 if( defined $diff ) {
236 $lo = $est{$param}[$i]-$diff;
237 $up = $est{$param}[$i]+$diff;
239 my $cis = sprintf( "($form - $form)", $lo, $up );
240 push( @row, $nam{$param}[$i],
241 sprintf( $form, $est{$param}[$i] ),
242 $cis );
243 } else {
244 push( @row, '','','' );
247 } else {
248 if ( defined $thnam[$i] ) {
249 push( @row, $thnam[$i], defined $thetas[$i] ? sprintf( $form, $thetas[$i] ) : '........',
250 $cvsethet[$i] ? sprintf( "($form)", $cvsethet[$i] ) : '(........)' );
251 } else {
252 push( @row, '','','' );
254 if ( defined $omnam[$i] ) {
255 push( @row, $omnam[$i], defined $comegas[$i] ? sprintf( $form, $comegas[$i] ) : '........' ,
256 $cvseomeg[$i] ? sprintf( "($form)", $cvseomeg[$i] ) : '(........)' );
257 } else {
258 push( @row, '','','');
260 if ( defined $signam[$i] ) {
261 push( @row, $signam[$i], defined $sigmas[$i] ? sprintf( $form, $sigmas[$i] ) : '........',
262 $cvsesigm[$i] ? sprintf( "($form)", $cvsesigm[$i] ) : '(........)' );
263 } else {
264 push( @row, '','','');
268 push(@{$output_matrix[$row_counter]}, @row);
269 for( my $i = 0; $i <= $#{$output_matrix[$row_counter]}; $i++ ){
270 if( $output_matrix_sizes[$i] < length( $output_matrix[$row_counter][$i] ) ){
271 $output_matrix_sizes[$i] = length( $output_matrix[$row_counter][$i] );
274 $row_counter++;
280 #for( my $j = 0; $j <= $#output_matrix; $j++ ){
281 # for( my $i = 0; $i <= $#{$output_matrix[$j]}; $i++ ){
282 # if( $output_matrix_sizes[$i] < length( $output_matrix[$j][$i] ) ){
283 # $output_matrix_sizes[$i] = length( $output_matrix[$j][$i] );
285 # }
288 foreach my $row ( @output_matrix ){
289 for( my $i = 0; $i <= $#{$row}; $i++ ){
290 my $spaces = $output_matrix_sizes[$i] - length($row -> [$i]);
291 if( $options{'csv'} ){
292 print $row -> [$i], ",";
293 } else {
294 print " " x $spaces, $row -> [$i], " ";
297 print "\n";
302 # my $pl = 1;
303 # my @plist;
304 # while($pl) {
305 # my $f = "";
306 # $pl = 0;
308 # if(@thnam){
309 # push(@plist,shift @thnam);
310 # $pl = 1;
311 # $f.="%-4s ";
312 # } else {
313 # push(@plist," ");
314 # $f.="%-4s ";
317 # if(@thetas){
318 # push(@plist,shift @thetas);
319 # $pl = 1;
320 # $f.="%-10.4g ";
321 # } else {
322 # push(@plist," ");
323 # $f.="%-10s ";
326 # if(@cvsethet){
327 # push(@plist,"(".(sprintf "%.3g",shift @cvsethet).")");
328 # $pl = 1;
329 # $f.="%-12s ";
330 # } else {
331 # push @plist," " and $f.="%-12s ";
334 # if ( defined $omegas[0] ) {
335 # if(@omnam){
336 # # push(@om_str,shift @omnam);
337 # # $pl = 1;
338 # # $f .="%-5s ";
339 # # } else {
340 # # push(@om_str," ");
341 # # $f .="%-5s ";
342 # # }
345 # # my ( $omega, @om_str );
346 # # if(@omegas){
347 # # $omega = shift( @omegas );
348 # # unless( defined $omega and not $compact_format){
349 # # push( @om_str, "NaN" );
350 # # $f .= "%-10s ";
351 # # } elsif ( $omega != 0 or not $compact_format ) {
352 # # push(@om_str,$omega);
353 # # $pl = 1;
354 # # $f.="%-10.4g ";
355 # # }
356 # # } else {
357 # # push(@plist," ");
358 # # $f.="%-10s ";
359 # # }
361 # # if(@cvseomeg){
362 # # my $cvseomega = shift( @cvseomeg );
363 # # if ( $omega != 0 ) {
364 # # push(@plist,"(".(sprintf "%.3g",$cvseomega).")");
365 # # $pl = 1;
366 # # $f.="%-8s ";
367 # # }
368 # # } else {
369 # # push(@plist," ");
370 # # $f.="%-8s ";
371 # # }
373 # if(@signam){
374 # push(@plist,shift @signam);
375 # $pl = 1;
376 # $f .="%-4s ";
377 # } else {
378 # push(@plist," ");
379 # $f .="%-4s ";
382 # if(@sigmas){
383 # push(@plist,shift @sigmas);
384 # $pl = 1;
385 # $f.="%-10.4g ";
386 # } else {
387 # push(@plist," ");
388 # $f.="%-10s";
391 # if(@cvsesigm) {
392 # push(@plist,"(".(sprintf "%.3g",shift @cvsesigm).")");
393 # $pl = 1;
394 # $f.="%-8s ";
395 # } else {
396 # push(@plist," ");
397 # $f.="%-8s ";
400 # $f .="\n";
402 # if($pl) {
403 # printf $f,@plist;
405 # undef @plist;