Skip to content

Commit

Permalink
vpc: support model derived DVs for refcorr
Browse files Browse the repository at this point in the history
  • Loading branch information
rikardn committed Jun 11, 2024
1 parent 353be75 commit 08944ad
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 3 deletions.
74 changes: 72 additions & 2 deletions lib/tool/npc.pm
Original file line number Diff line number Diff line change
Expand Up @@ -1617,17 +1617,18 @@ sub modelfit_setup
my $path = "m1/" . $model_sims[$i]->filename;
my $destname = "${type}_simulation_refcorr.$num.mod";
my $destpath = "m1/$destname";
my $refmodel;
if (%refcorr) {
my @refstrs;
for my $k (keys %refcorr) {
push @refstrs, "$k=" . $refcorr{$k};
}
my $refstr = join(" ", @refstrs);
PsN::call_pharmpy("data reference $path -o $destpath $refstr");
my $refmodel = model->new(filename => $destpath);
$refmodel = model->new(filename => $destpath);
push @refcorr_models, $refmodel;
} elsif (defined $self->refcorr_data) {
my $refmodel = $model_sims[$i]->copy(
$refmodel = $model_sims[$i]->copy(
filename => $destname,
directory => 'm1',
copy_datafile => 0,
Expand All @@ -1642,6 +1643,51 @@ sub modelfit_setup
$refmodel->_write();
push @refcorr_models, $refmodel;
}
if (is_var_defined_in_code($refmodel, $self->dv)) {
sub prepare_pred_model {
my $self = shift;
my $base_model = shift;
my $name = shift;
my $refpredmodel = $base_model->copy(
filename => $name,
directory => 'm1',
copy_datafile => 0,
write_copy => 0,
output_same_directory => 1,
copy_output => 0);
my $neta = $refpredmodel->nomegas->[0];
my $neps = $refpredmodel->nsigmas->[0];
$refpredmodel->remove_records(type => 'omega');
$refpredmodel->remove_records(type => 'sigma');
$refpredmodel->remove_records(type => 'estimation');
$refpredmodel->remove_records(type => 'simulation');
$refpredmodel->remove_records(type => 'table');
for (my $i = 0; $i < $neta; $i++) {
$refpredmodel->add_records(type => 'omega',
record_strings => ['0 FIX'],
problem_numbers => [($self->origprobnum())]);
}
for (my $i = 0; $i < $neps; $i++) {
$refpredmodel->add_records(type => 'sigma',
record_strings => ['0 FIX'],
problem_numbers => [($self->origprobnum())]);
}
$refpredmodel->set_records(type => 'simulation',
record_strings => ['(999) ONLYSIMULATION SUBPROBLEMS=1'],
problem_numbers => [($self->origprobnum())]);
$refpredmodel->set_records(type => 'table',
record_strings => [$self->dv, 'MDV', 'NOAPPEND', 'FILE=npctab.dta'],
problem_numbers => [($self->origprobnum())]);
$refpredmodel->_write();
return $refpredmodel;
}
my $refpredmodel_name = "${type}_pred_refcorr.$num.mod";
my $refpredmodel = prepare_pred_model($self, $refmodel, $refpredmodel_name);
push @refcorr_models, $refpredmodel;
my $predmodel_name = "${type}_pred.$num.mod";
my $predmodel = prepare_pred_model($self, $model_orig, $predmodel_name);
push @refcorr_models, $predmodel;
}
}
}
$model_orig -> remove_records(type => 'simulation');
Expand Down Expand Up @@ -1689,6 +1735,30 @@ sub modelfit_setup
}
}


sub is_var_defined_in_code
{
my $model = shift;
my $var = shift;

my @line_array;
push (@line_array, @{$model->problems->[0]->errors->[0]->code})
if (defined ($model->problems->[0]->errors));
push (@line_array, @{$model->problems->[0]->preds->[0]->code})
if (defined $model->problems->[0]->preds);
push (@line_array, @{$model->problems->[0]->pks->[0]->code})
if (defined $model->problems->[0]->pks);

for my $line (@line_array) {
if ($line =~ /^\s*(\w+)\s*=/) {
if ($1 eq $var) {
return 1;
}
}
}
return 0;
}

sub _modelfit_raw_results_callback
{
my $self = shift;
Expand Down

0 comments on commit 08944ad

Please sign in to comment.