Skip to content

Commit

Permalink
Move CalculateScoresBetweenAllIonmodes from plotting_wrapper_function…
Browse files Browse the repository at this point in the history
…s.py to training_wrapper_functions.py and use fingerprint settings
  • Loading branch information
niekdejonge committed Oct 30, 2024
1 parent 6af69ee commit caf0804
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 22 deletions.
15 changes: 3 additions & 12 deletions ms2deepscore/wrapper_functions/plotting_wrapper_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,23 @@
from ms2deepscore.benchmarking.plot_loss_per_bin import plot_loss_per_bin_multiple_benchmarks


def create_plots_between_ionmodes(positive_validation_spectra,
negative_validation_spectra,
model_file_name: str,
def create_plots_between_ionmodes(scores_between_all_ionmodes: CalculateScoresBetweenAllIonmodes,
results_folder: str,
nr_of_bins=50):
"""
Creates comparison plots between different ion modes.
Parameters
----------
positive_validation_spectra:
The validation spectra in positive ionisation mode
negative_validation_spectra:
The validation spectra in negative ionistation mode
model_file_name:
MS2Deepscore model file name
scores_between_all_ionmodes:
CalculateScoresBetweenAllIonmodes object containing predictions and tanimoto scores between ionmodes.
results_folder:
Directory where the plots will be saved.
nr_of_bins:
Nr of tanimoto score bins to use for plotting.
"""
os.makedirs(results_folder, exist_ok=True)

scores_between_all_ionmodes = CalculateScoresBetweenAllIonmodes(model_file_name, positive_validation_spectra,
negative_validation_spectra)

plot_loss_per_bin_multiple_benchmarks(scores_between_all_ionmodes.list_of_predictions_and_tanimoto_scores(),
nr_of_bins=nr_of_bins,
loss_type="RMSE")
Expand Down
22 changes: 12 additions & 10 deletions ms2deepscore/wrapper_functions/training_wrapper_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,16 @@ def train_ms2deepscore_wrapper(spectra_file_path,
ms2ds_history_plot_file_name = os.path.join(results_folder, settings.history_plot_file_name)
plot_history(history["losses"], history["val_losses"], ms2ds_history_plot_file_name)

scores_between_all_ionmodes = CalculateScoresBetweenAllIonmodes(
os.path.join(stored_training_data.trained_models_folder, model_directory_name, settings.model_file_name),
stored_training_data.load_training_data("positive", "validation"),
stored_training_data.load_training_data("negative", "validation"),
settings.fingerprint_type, settings.fingerprint_nbits)

# Create performance plots for validation spectra
create_plots_between_ionmodes(positive_validation_spectra=stored_training_data.load_training_data("positive", "validation"),
negative_validation_spectra=stored_training_data.load_training_data("negative", "validation"),
create_plots_between_ionmodes(scores_between_all_ionmodes,
results_folder=os.path.join(stored_training_data.trained_models_folder,
model_directory_name, "benchmarking_results"),
model_file_name=os.path.join(stored_training_data.trained_models_folder,
model_directory_name,
settings.model_file_name),
nr_of_bins=50)
return model_directory_name

Expand Down Expand Up @@ -179,8 +181,8 @@ def parameter_search(

scores_between_all_ionmodes = CalculateScoresBetweenAllIonmodes(
model_file_name=os.path.join(stored_training_data.trained_models_folder,
model_directory_name,
settings.model_file_name),
model_directory_name,
settings.model_file_name),
positive_validation_spectra=positive_validation_spectra,
negative_validation_spectra=negative_validation_spectra)

Expand All @@ -192,9 +194,9 @@ def parameter_search(
for loss_type in loss_types:
losses_per_ionmode = {}
for predictions_and_tanimoto_scores in scores_between_all_ionmodes.list_of_predictions_and_tanimoto_scores():
_, losses = predictions_and_tanimoto_scores.get_average_loss_per_bin_per_inchikey_pair(loss_type,
settings.same_prob_bins)
losses_per_ionmode[predictions_and_tanimoto_scores.label] = losses
_, losses = predictions_and_tanimoto_scores.get_average_loss_per_bin_per_inchikey_pair(loss_type,
settings.same_prob_bins)
losses_per_ionmode[predictions_and_tanimoto_scores.label] = losses
combination_results["losses"][loss_type] = losses_per_ionmode

# Store results
Expand Down

0 comments on commit caf0804

Please sign in to comment.