Skip to content

Commit

Permalink
Replace phi2 to phi and set std as the spread of chi2 among the repli…
Browse files Browse the repository at this point in the history
…cas of the best fit
  • Loading branch information
Cmurilochem committed Mar 8, 2024
1 parent 8679dca commit 55f0605
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
25 changes: 15 additions & 10 deletions validphys2/src/validphys/hyperoptplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ def parse_statistics(trial):
# dict_out["std"] = std
dict_out["hlosses"] = convert_string_to_numpy(results["kfold_meta"]["hyper_losses"])
dict_out["vlosses"] = convert_string_to_numpy(results["kfold_meta"]["validation_losses"])
dict_out["hlosses_phi2"] = convert_string_to_numpy(results["kfold_meta"]["hyper_losses_phi2"])
dict_out["hlosses_phi"] = convert_string_to_numpy(results["kfold_meta"]["hyper_losses_phi"])
return dict_out


Expand Down Expand Up @@ -384,9 +384,12 @@ def evaluate_trial(trial_dict, validation_multiplier, fail_threshold, loss_targe
test_loss = np.array(trial_dict["hlosses"]).max()
elif loss_target == "std":
test_loss = np.array(trial_dict["hlosses"]).std()
elif loss_target == "min_chi2_max_phi2":
elif loss_target == "min_chi2_max_phi":
test_loss = np.array(trial_dict["hlosses"]).mean()
phi2 = np.array(trial_dict["hlosses_phi2"]).mean()
phi = np.array(trial_dict["hlosses_phi"]).mean()
else:
raise ValueError(f"Loss target {loss_target} not recognized.")

loss = val_loss * validation_multiplier + test_loss * test_f

if (
Expand All @@ -400,8 +403,9 @@ def evaluate_trial(trial_dict, validation_multiplier, fail_threshold, loss_targe
loss *= 10

trial_dict["loss"] = loss
if loss_target == "min_chi2_max_phi2":
trial_dict["loss_inverse_phi2"] = np.reciprocal(phi2)
if loss_target == "min_chi2_max_phi":
trial_dict["loss_phi"] = phi
trial_dict["loss_inverse_phi"] = np.reciprocal(phi)


def generate_dictionary(
Expand Down Expand Up @@ -577,16 +581,17 @@ def hyperopt_dataframe(commandline_args):
# Now select the best one
best_idx = dataframe.loss.idxmin()

if args.loss_target == "min_chi2_max_phi2":
if args.loss_target == "min_chi2_max_phi":
minimum = dataframe.loss[best_idx]
std = np.std(dataframe.loss)
# set std to the spread of chi2 among the replicas of the best fit
std = np.std(dataframe.hlosses[best_idx])
lim_max = dataframe.loss[best_idx] + std
# select rows with chi2 losses within the best point and lim_max
selected_chi2 = dataframe[(dataframe.loss >= minimum) & (dataframe.loss <= lim_max)]
# among the selected points, select the nth lowest in 1/phi2
selected_phi2 = selected_chi2.loss_inverse_phi2.nsmallest(args.max_phi2_n_models)
# among the selected points, select the nth lowest in 1/phi
selected_phi = selected_chi2.loss_inverse_phi.nsmallest(args.max_phi_n_models)
# find the location of these points in the dataframe
indices = dataframe[dataframe['loss_inverse_phi2'].isin(selected_phi2)].index
indices = dataframe[dataframe['loss_inverse_phi'].isin(selected_phi)].index
best_trial = dataframe.loc[indices]
else:
best_trial_series = dataframe.loc[best_idx]
Expand Down
8 changes: 4 additions & 4 deletions validphys2/src/validphys/scripts/vp_hyperoptplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ def add_positional_arguments(self, parser):
"-l",
"--loss_target",
help="Choice for the definition of target loss",
choices=['average', 'best_worst', 'std', 'min_chi2_max_phi2'],
choices=['average', 'best_worst', 'std', 'min_chi2_max_phi'],
default='average',
)
parser.add_argument(
"--max_phi2_n_models",
help="If --loss_target=best_chi2_worst_phi2, outputs n models with the highest phi2.",
"--max_phi_n_models",
help="If --loss_target=best_chi2_worst_phi, outputs n models with the highest phi.",
type=int,
default=1,
)
Expand Down Expand Up @@ -129,7 +129,7 @@ def complete_mapping(self):
"autofilter": args["autofilter"],
"debug": args["debug"],
"loss_target": args["loss_target"],
"max_phi2_n_models": args["max_phi2_n_models"],
"max_phi_n_models": args["max_phi_n_models"],
}

try:
Expand Down

0 comments on commit 55f0605

Please sign in to comment.