fixed label prints
[PsN.git] / lib / model / shrinkage_module_subs.pm
blob3bb53ec448456c1c1fb598e3b9e74a477f8ff999
1 # {{{ include
3 start include statements
5 use Data::Dumper;
7 end include statements
9 # }}} include
11 # {{{ enable
13 start enable
15 $self -> {'enabled'} = 1;
17 end enable
19 # }}} enable
21 # {{{ disable
23 start disable
25 $self -> {'enabled'} = 0;
27 end disable
29 # }}} disable
31 # {{{ format_shrinkage_tables
33 start format_shrinkage_tables
35 my $omegas = $self -> {'problem'} -> nomegas;
37 my $eta_str = 'ID';
38 my $eps_str = 'ID IWRES EVID';
40 for( my $j = 1; $j <= $omegas; $j++ ) {
41 $eta_str = $eta_str.' ETA'.$j;
43 $eta_str = $eta_str.' FILE='.$self -> eta_tablename.
44 ' NOAPPEND ONEHEADER NOPRINT FIRSTONLY';
45 $eps_str = $eps_str.' FILE='.$self -> wres_tablename.
46 ' NOAPPEND ONEHEADER NOPRINT'."\n";
48 my $eta_table = model::problem::table -> new ( record_arr => [$eta_str] );
49 my $eps_table = model::problem::table -> new ( record_arr => [$eps_str] );
51 my $wrapped = ( defined $self -> {'model'} -> datas -> [$self -> problem_number-1] -> wrap_column and
52 defined $self -> {'model'} -> datas -> [$self -> problem_number-1] -> cont_column );
54 if( $wrapped ) {
55 # No good contifying the eta table. We use FIRSTONLY which will make
56 # CONT=1 on all rows and PsN does not like that.
57 $eps_table -> contify;
60 @formatted = ( @{$eta_table -> _format_record},
61 @{$eps_table -> _format_record} );
63 end format_shrinkage_tables
65 # }}} format_shrinkage_tables
67 # {{{ eta_tablename
69 start eta_tablename
71 my $probnum = $self -> problem_number;
73 $filename = 'prob'.'_'.$probnum.'.psn_etas';
75 end eta_tablename
77 # }}} eta_tablename
79 # {{{ wres_tablename
81 start wres_tablename
83 my $probnum = $self -> problem_number;
85 $filename = 'prob'.'_'.$probnum.'.psn_wres';
87 end wres_tablename
89 # }}} wres_tablename
91 # {{{ eta_table_exists
93 start eta_table_exists
95 my $directory = $self -> {'model'} -> directory;
96 my $filename = $self -> eta_tablename;
98 $exists = -e $directory.$filename ? 1 : 0;
100 end eta_table_exists
102 # }}} eta_table_exists
104 # {{{ wres_table_exists
106 start wres_table_exists
108 my $directory = $self -> {'model'} -> directory;
109 my $filename = $self -> wres_tablename;
111 $exists = -e $directory.$filename ? 1 : 0;
113 end wres_table_exists
115 # }}} wres_table_exists
117 # {{{ problem_number
119 start problem_number
121 if( defined $self -> {'model'} -> problems ) {
122 # This is the default
123 my @modprobs = @{$self -> {'model'} -> problems};
124 for( my $i = 0; $i <= $#modprobs; $i++ ) {
125 $problem_number = ($i+1) if( $modprobs[$i] eq $self -> {'problem'} );
127 } else {
128 # This happens when the problems are not yet set in the model
129 $problem_number = $self -> {'temp_problem_number'};
132 end problem_number
134 # }}} problem_number
136 # {{{ eta_shrinkage
138 start eta_shrinkage
140 # We do not handle subproblem eta shrinkage
142 if( $self -> {'enabled'} ) {
143 my $omegas = $self -> {'model'} -> outputs -> [0] -> omegas;
144 my $probnum = $self -> problem_number - 1; # start index at 0
145 my @omega_indexes;
146 if( defined $self -> {'model'} -> outputs -> [0] and
147 defined $self -> {'model'} -> outputs -> [0] -> problems -> [0] and
148 defined $self -> {'model'} -> outputs -> [0] -> problems -> [0] -> omega_indexes ) {
149 @omega_indexes = @{$self -> {'model'} ->
150 outputs -> [0] -> problems -> [0] -> omega_indexes};
152 my $defined_indexes = 0;
153 foreach my $index ( @omega_indexes ) {
154 $defined_indexes++ if defined $index;
157 if( defined $omegas and defined $omegas -> [$probnum] ) {
158 if( scalar @{$omegas -> [$probnum]} == 1 ) { # One subprob
159 if( $defined_indexes and $self -> eta_table_exists ) {
160 my $sh_table = data -> new( directory => $self -> {'model'} -> directory,
161 filename => $self -> eta_tablename,
162 ignore_missing_files => 1,
163 target => 'mem',
164 table_file => 1 );
165 my $diag_omega_idx = 0;
166 for( my $j = 0; $j < scalar @{$omegas -> [$probnum][0]}; $j++ ) {
167 next if( $omega_indexes[$j][0] != $omega_indexes[$j][1] );
168 if ( defined $omegas -> [$probnum][0][$j] and defined $sh_table and
169 $omegas -> [$probnum][0][$j] != 0 ) {
170 my $omega = sqrt($omegas -> [$probnum][0][$j]);
171 my $eta_sd = $sh_table -> sd( column => ($diag_omega_idx+2) );
172 my $shrinkage = ($omega - $eta_sd)/$omega;
173 $eta_shrinkage[0][$diag_omega_idx] = $shrinkage;
174 } else {
175 $eta_shrinkage[0][$diag_omega_idx] = undef;
177 $diag_omega_idx++;
179 } else {
180 $eta_shrinkage[0] = [];
182 } elsif( scalar @{$omegas -> [$probnum]} == 0 ) {
183 debug -> die( message => $self -> {'model'} -> full_name. " Call to output->omegas is empty. PsN can not compute shrinkage." );
184 } else {
185 debug -> die( message => $self -> {'model'} -> full_name . " Call to output->omegas indicates that results ".
186 "exists in multiple subproblems.PsN can not yet compute shrinkage".
187 " on the subproblem level" );
192 end eta_shrinkage
194 # }}} eta_shrinkage
196 # {{{ wres_shrinkage
198 start wres_shrinkage
200 # We do not handle subproblem wres shrinkage
202 if( $self -> {'enabled'} ) {
203 my $ofv = $self -> {'model'} -> outputs -> [0] -> ofv; # Use ofv to test success
205 my $probnum = $self -> problem_number - 1; # start index at 0
206 if( defined $ofv ) {
207 if( scalar @{$ofv -> [$probnum]} == 1 ) {
208 my $sh_table;
209 if( defined $ofv -> [$probnum][0] and $self -> wres_table_exists ) {
210 $sh_table = data -> new( directory => $self -> {'model'} -> directory,
211 filename => $self -> wres_tablename,
212 ignore_missing_files => 1,
213 target => 'mem',
214 table_file => 1 );
216 if( defined $sh_table ) {
217 my $wres_sd = $sh_table -> sd( column => 2,
218 subset_column => 3,
219 subset_syntax => '==0',
220 global_sd => 1 );
221 my $shrinkage = 1 - $wres_sd;
222 $wres_shrinkage[0] = $shrinkage;
223 } else {
224 $wres_shrinkage[0] = undef;
226 } elsif ( @{$ofv -> [$probnum]} < 1 ) {
227 debug -> warn( level => 1,
228 message => "There seems to be a problem with the results from ".
229 $self -> {'model'} -> filename().". Cannot compute shrinkage." );
230 } else {
231 debug -> die( message => "Call to output->omegas indicates that results ".
232 "exists in multiple subproblems.PsN can not yet compute shrinkage".
233 " on the subproblem level" );
238 end wres_shrinkage
240 # }}} wres_shrinkage