Fixed $PRIOR support and bugfix in shrinkage results handling
[PsN.git] / bin / npc
blobb5d092ecb9499ef389d037bb41eb7adc0c21d2c7
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 = ( 'stratify_on:s' => 'column',
28 'no_of_strata:i' => 'N',
29 'lst:s' => 'file',
30 'msfo:s' => 'file',
31 'dv:s' => 'variable',
32 'unstable_override_estimation:i' => '1');
34 my $res = GetOptions( \%options,
35 @common_options::get_opt_strings,
36 keys(%required_options),
37 keys(%optional_options) );
38 exit unless $res;
40 common_options::set_globals( \%options, 'npc' );
41 common_options::get_defaults( \%options, 'npc' );
42 common_options::sanity_checks( \%options, 'npc' );
44 my %help_text;
46 $help_text{Pre_help_message} = <<'EOF';
47 npc
49 Perl script for Numerical Predictive Check.
51 Usage:
52 EOF
54 $help_text{Options} = <<'EOF';
55 Options:
57 The options are given here in their long form. Any option may be
58 abbreviated to any nonconflicting prefix. The -threads option
59 may be abbreviated to -t(or even -thr) but -debug may not be
60 abbreviated to -d because it conflicts with -debug_packages and
61 -debug_subroutines.
63 The following options are valid:
64 EOF
66 $help_text{-h} = <<'EOF';
67 -h | -?
69 With -h or -? npc will print a list of options and exit.
70 EOF
72 $help_text{-help} = <<'EOF';
73 -help
75 With -help npc will print this, longer, help message.
76 EOF
78 $help_text{-samples} = <<'EOF';
79 -samples=number
81 Number of simulated datasets to generate, required.
82 Must be at least 20.
83 EOF
85 $help_text{-stratify_on} = <<'EOF';
86 -stratify_on=COLUMN_HEADER
88 Header of column to stratify on.
89 The npc will stratify the data on unique values in the specified
90 column, and perform separate analyses on each set.
91 EOF
93 $help_text{-no_of_strata} = <<'EOF';
94 -no_of_strata=number
96 Only allowed together with -stratify_on.
97 An integer requesting the number of strata.
98 The number must be equal to or less than the number of unique
99 values in the stratification column.
100 If the number is smaller than the number of unique values, the
101 observations will be grouped so that the number of observations in
102 each group/strata is approximately equal.
105 $help_text{-lst} = <<'EOF';
106 -lst=file
108 The npc will update initial parameter estimates in the model to
109 the final values in the lst-file. Optional parameter.
110 Cannot be used together with -msfo option nor with a model
111 containing a $NONPARAMETRIC record.
114 $help_text{-lst} = <<'EOF';
115 -msfo=file
117 The npc will use the NONMEM state defined in the update file.
118 Cannot be used together with -lst option.
119 If the modelfile contains a $NONPARAMETRIC record this parameter
120 is required, otherwise it is optional.
123 $help_text{-dv} = <<'EOF';
124 -dv=VARIABLE
126 Name of dependent variable.
127 Default is DV, but any variable is allowed.
128 Case sensitive, must be exactly the same case and spelling as in modelfile.
129 Maximum of 4 letters because of NONMEM limitation, unless either IPRED or IWRES.
132 #$help_text{-unstable_override_estimation} = <<'EOF';
133 # -unstable_override_estimation=1
135 # Only for advanced users.
136 # Override the safety checks of the program and refuse estimation when
137 # -dv option is used. This option has not been tested and
138 # crashes may occur.
139 #EOF
141 $help_text{Post_help_message} = <<'EOF';
142 Also see 'execute -help' for a description of common options.
147 common_options::online_help( 'npc', \%options, \%help_text, \%required_options, \%optional_options);
149 ## Check that we do have a model file
150 if ( scalar(@ARGV) < 1 ) {
151 print "A model file must be specified. Use 'npc -h' for help.\n";
152 exit;
155 if( scalar(@ARGV) > 1 ){
156 print "NPC can only handle one modelfile. Check that all options begin with".
157 " '-'.\nUse 'npc -h' for help.\n";die;
158 exit;
161 if ( defined $options{'no_of_strata'} ){
162 unless (defined $options{'stratify_on'}){
163 die "Error: The NPC does not allow option no_of_strata unless a ".
164 "stratification column has been specified.\n";
168 if (defined $options{'msfo'}){
169 if (defined $options{'extra_files'}){
170 $options{'extra_files'} .= ','.$options{'msfo'};
171 }else {
172 $options{'extra_files'} = $options{'msfo'};
176 my $eval_string = common_options::model_parameters(\%options);
178 my $model = model -> new ( eval( $eval_string ),
179 filename => $ARGV[0],
180 ignore_missing_output_files => 1 ,
181 cwres => ($options{'dv'} eq 'CWRES'));
184 my $do_estimation = ((defined $options{'dv'}) ||
185 (defined $options{'stratify_on'} &&
186 ($options{'stratify_on'} eq 'PRED')));
188 if (defined $options{'unstable_override_estimation'} &&
189 $options{'unstable_override_estimation'} == 1 &&
190 defined $options{'dv'} &&
191 !(defined $options{'stratify_on'} &&($options{'stratify_on'} eq 'PRED'))
193 $do_estimation = 0;
196 my $npc = tool::npc ->
197 new ( eval( $common_options::parameters ),
198 models => [ $model ],
199 stratify_on => $options{'stratify_on'},
200 no_of_strata => $options{'no_of_strata'},
201 samples => $options{'samples'},
202 lst_file => $options{'lst'},
203 msfo_file => $options{'msfo'},
204 dv => $options{'dv'},
205 do_estimation => $do_estimation
208 my $restart = ( -e $npc -> {'directory'}."/command.txt" ) ? 1 : 0;
209 unless ($restart){
210 open(CMD, ">", $npc -> directory . "/command.txt");
211 print CMD $cmd_line, "\n";
212 close(CMD);
215 $npc -> run;
216 $npc -> print_results;