Fixed $PRIOR support and bugfix in shrinkage results handling
[PsN.git] / bin / vpc
blob49e0d0c279ad3640984a0e0b1fe877e7f3286d09
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 model;
11 use tool::npc;
12 use strict;
13 use debug;
14 use Getopt::Long;
15 use common_options;
16 use ui;
18 my $cmd_line = $0 . " " . join( " ", @ARGV );
20 ## Configure the command line parsing
21 Getopt::Long::config("auto_abbrev");
23 my %options;
24 ## Declare the options
26 my %required_options = ('samples:i' => 'N');
27 my %optional_options = ( 'idv:s' => 'independent variable',
28 "stratify_on:s" => 'column' ,
29 'no_of_strata:i' => 'N',
30 'lst:s' => 'file',
31 'msfo:s' => 'file',
32 'bin_by_count:i' => '1|0',
33 'no_of_bins:i' => 'N',
34 'single_bin_size:s' => 'X',
35 'overlap:i' => 'N',
36 'bin_array:s' => 'x1,x2,...',
37 'mirrors:i' => 'N',
38 'dv:s' => 'variable',
39 'unstable_override_estimation:i' => '1');
41 my $res = GetOptions( \%options,
42 @common_options::get_opt_strings,
43 keys(%required_options),
44 keys(%optional_options) );
45 exit unless $res;
47 common_options::set_globals( \%options, 'vpc' );
48 common_options::get_defaults( \%options, 'vpc' );
49 common_options::sanity_checks( \%options, 'vpc' );
51 my %help_text;
53 $help_text{Pre_help_message} = <<'EOF';
54 vpc
56 Perl script for Visual Predictive Check.
58 Usage:
59 EOF
61 $help_text{Options} = <<'EOF';
62 Options:
64 The options are given here in their long form. Any option may be
65 abbreviated to any nonconflicting prefix. The -threads option
66 may be abbreviated to -t(or even -thr) but -debug may not be
67 abbreviated to -d because it conflicts with -debug_packages and
68 -debug_subroutines.
70 The following options are valid:
71 EOF
73 $help_text{-h} = <<'EOF';
74 -h | -?
76 With -h or -? vpc will print a list of options and exit.
77 EOF
79 $help_text{-help} = <<'EOF';
80 -help
82 With -help vpc will print this, longer, help message.
83 EOF
85 $help_text{-samples} = <<'EOF';
86 -samples=number
88 Number of simulated datasets to generate, required.
89 Must be at least 20.
90 EOF
93 $help_text{-idv} = <<'EOF';
94 -idv=VARIABLE
96 Name of independent variable, optional, default TIME.
97 The vpc will bin the observations based on the independent
98 variable, either on unique values of the specified variable
99 (default), or according to selected binning options.
102 $help_text{-bin_by_count} = <<'EOF';
103 -bin_by_count= 1 | 0
105 Decide whether binning should be done based on the number of
106 observations (1) in each bin or the width of the binning interval (0).
107 Required unless only the independent variable is specified without any
108 binning options, then forbidden.
111 $help_text{-no_of_bins} = <<'EOF';
112 -no_of_bins=N
114 Number of bins of approximately equal size to create. Optional.
115 The number must be larger than 1.
116 If used, the option -bin_by_count is required, while all other
117 binning options are forbidden.
120 $help_text{-single_bin_size} = <<'EOF';
121 -single_bin_size=X
123 An scalar specifying the size of individual bins. The number
124 either specifies the number of observations in each bin or
125 the width of the binning interval, depending on whether the
126 option -bin_by_count is 1 or 0. If used, the option
127 -bin_by_count is required and -overlap is optional,
128 while all other binning options are forbidden.
131 $help_text{-overlap} = <<'EOF';
132 -overlap=Y
134 An integer specifying the percent overlap between consecutive bins.
135 If used, the options -bin_by_count and -single_bin_size are required,
136 while all other binning options are forbidden.
139 $help_text{-bin_array} = <<'EOF';
140 -bin_array=x1,x2,...
142 A comma-separated list of either the number of observations in each
143 bin or the boundaries between bins, depending on whether the option
144 -bin_by_count is 1 or 0. If binning is done by count, the list
145 must contain at least 2 numbers, otherwise 1 number is enough.
146 If used, the option -bin_by_count is required, while all other
147 binning options are forbidden.
150 $help_text{-stratify_on} = <<'EOF';
151 -stratify_on=VARIABLE
153 Variable to stratify on. The variable must be independent, i.e. be
154 the same for all simulations of a particular observation.
155 The vpc will stratify the data on unique values of the specified
156 variable, and perform separate analyses on each set.
159 $help_text{-no_of_strata} = <<'EOF';
160 -no_of_strata=number
162 Only allowed together with -stratify_on.
163 An integer requesting the number of strata.
164 The number must be equal to or less than the number of unique
165 values of the stratification variable.
166 If the number is smaller than the number of unique values, the
167 observations will be grouped so that the number of observations in
168 each group/strata is approximately equal.
171 $help_text{-lst} = <<'EOF';
172 -lst=file
174 The vpc will update initial parameter estimates in the model to
175 the final values in the lst-file. Optional parameter.
176 Cannot be used together with -msfo option nor with a model
177 containing a $NONPARAMETRIC record.
180 $help_text{-lst} = <<'EOF';
181 -msfo=file
183 The vpc will use the NONMEM state defined in the update file.
184 Cannot be used together with -lst option.
185 If the modelfile contains a $NONPARAMETRIC record this parameter
186 is required, otherwise it is optional.
189 $help_text{-mirrors} = <<'EOF';
190 -mirrors=N
192 Generate output data for N mirror plots.
195 $help_text{-dv} = <<'EOF';
196 -dv=VARIABLE
198 Name of dependent variable.
199 Default is DV, but any dependent variable is allowed.
200 Case sensitive, must be exactly the same case and spelling as in modelfile.
201 Maximum of 4 letters because of NONMEM limitation, unless either IPRED or IWRES.
204 #$help_text{-unstable_override_estimation} = <<'EOF';
205 # -unstable_override_estimation=1
207 # Only for advanced users.
208 # Override the safety checks of the program and refuse estimation when
209 # -dv option is used. This option has not been tested and
210 # crashes may occur.
211 #EOF
213 $help_text{Post_help_message} = <<'EOF';
214 Also see 'execute -help' for a description of common options.
217 common_options::online_help( 'vpc', \%options, \%help_text, \%required_options, \%optional_options);
219 ## Check that we do have a model file
220 if ( scalar(@ARGV) < 1 ) {
221 print "A model file must be specified. Use 'vpc -h' for help.\n";
222 exit;
225 if( scalar(@ARGV) > 1 ){
226 print "VPC can only handle one modelfile. Check that all options begin with".
227 " -.\nUse 'vpc -h' for help.\n";die;
228 exit;
231 my @bin_array=();
233 if ( defined $options{'bin_array'} ){
234 foreach my $v (split(/,/,$options{'bin_array'})){
235 if (length($v)>0){
236 push(@bin_array,$v);
239 unless (scalar(@bin_array)>0){
240 die "Error: Could not parse list bin_array.\n";
244 if ( defined $options{'no_of_strata'} ){
245 unless (defined $options{'stratify_on'}){
246 die "Error: The VPC does not allow option no_of_strata unless a ".
247 "stratification column has been specified.\n";
251 if (defined $options{'msfo'}){
252 if (defined $options{'extra_files'}){
253 $options{'extra_files'} .= ','.$options{'msfo'};
254 }else {
255 $options{'extra_files'} = $options{'msfo'};
259 my $eval_string = common_options::model_parameters(\%options);
261 my $model = model -> new ( eval( $eval_string ),
262 filename => $ARGV[0],
263 ignore_missing_output_files => 1,
264 cwres => ($options{'dv'} eq 'CWRES'));
266 my $do_estimation = ((defined $options{'dv'}) ||
267 ((defined $options{'idv'}) && ($options{'idv'} eq 'PRED')) ||
268 (defined $options{'stratify_on'} &&
269 ($options{'stratify_on'} eq 'PRED')));
271 if (defined $options{'unstable_override_estimation'} &&
272 $options{'unstable_override_estimation'} == 1 &&
273 defined $options{'dv'} &&
274 !((defined $options{'idv'}) && ($options{'idv'} eq 'PRED'))&&
275 !(defined $options{'stratify_on'} &&($options{'stratify_on'} eq 'PRED'))
277 $do_estimation = 0;
281 my $vpc = tool::npc ->
282 new ( eval( $common_options::parameters ),
283 models => [ $model ],
284 stratify_on => $options{'stratify_on'},
285 no_of_strata => $options{'no_of_strata'},
286 samples => $options{'samples'},
287 lst_file => $options{'lst'},
288 msfo_file => $options{'msfo'},
289 is_vpc => 1,
290 bin_on => ((defined $options{'idv'})? $options{'idv'} :'TIME'),
291 bin_by_count => $options{'bin_by_count'},
292 no_of_bins => $options{'no_of_bins'},
293 single_bin_size => $options{'single_bin_size'},
294 overlap_percent => $options{'overlap'},
295 bin_array => \@bin_array ,
296 mirrors => $options{'mirrors'},
297 dv => $options{'dv'},
298 do_estimation => $do_estimation
301 my $restart = ( -e $vpc -> {'directory'}."/command.txt" ) ? 1 : 0;
302 unless ($restart){
303 open(CMD, ">", $vpc -> directory . "/command.txt");
304 print CMD $cmd_line, "\n";
305 close(CMD);
308 $vpc -> run;
309 $vpc -> print_results;