Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add embedding evaluator to pipeline #208

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion ms2deepscore/models/load_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ def load_embedding_evaluator(filename: Union[str, Path]) -> EmbeddingEvaluationM
Filename. Expecting saved EmbeddingEvaluationModel.

"""
model_settings = torch.load(filename)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

model_settings = torch.load(filename, map_location=device)
if model_settings["version"] != __version__:
print(f"The model version ({model_settings['version']}) does not match the version of MS2Deepscore "
f"({__version__}), consider downloading a new model or changing the MS2Deepscore version")
Expand Down
29 changes: 20 additions & 9 deletions ms2deepscore/wrapper_functions/training_wrapper_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from matchms.importing import load_spectra
from ms2deepscore.benchmarking.calculate_scores_for_validation import \
calculate_true_values_and_predictions_for_validation_spectra
from ms2deepscore.SettingsMS2Deepscore import SettingsMS2Deepscore
from ms2deepscore.SettingsMS2Deepscore import SettingsMS2Deepscore, SettingsEmbeddingEvaluator
from ms2deepscore.train_new_model.split_positive_and_negative_mode import \
split_by_ionmode
from ms2deepscore.train_new_model.train_ms2deepscore import train_ms2ds_model
Expand All @@ -15,10 +15,13 @@
from ms2deepscore.utils import load_spectra_as_list
from ms2deepscore.wrapper_functions.plotting_wrapper_functions import \
create_plots_between_all_ionmodes
from ms2deepscore.models.EmbeddingEvaluatorModel import EmbeddingEvaluationModel
from ms2deepscore.models.load_model import load_model


def train_ms2deepscore_wrapper(spectra_file_path,
settings: SettingsMS2Deepscore,
settings_ms2deepscore: SettingsMS2Deepscore,
settings_embedding_evaluation: SettingsEmbeddingEvaluator = None,
validation_split_fraction=20
):
"""Splits data, trains a ms2deepscore model, and does benchmarking.
Expand All @@ -35,21 +38,29 @@ def train_ms2deepscore_wrapper(spectra_file_path,

stored_training_data = StoreTrainingData(spectra_file_path,
split_fraction=validation_split_fraction,
random_seed=settings.random_seed)
random_seed=settings_ms2deepscore.random_seed)

# Split training in pos and neg and create val and training split and select for the right ionisation mode.
training_spectra = stored_training_data.load_training_data(settings.ionisation_mode, "training")
validation_spectra = stored_training_data.load_training_data(settings.ionisation_mode, "validation")
training_spectra = stored_training_data.load_training_data(settings_ms2deepscore.ionisation_mode, "training")
validation_spectra = stored_training_data.load_training_data(settings_ms2deepscore.ionisation_mode, "validation")

model_directory_name = create_model_directory_name(settings)
model_directory_name = create_model_directory_name(settings_ms2deepscore)

# Train model
train_ms2ds_model(training_spectra, validation_spectra,
os.path.join(stored_training_data.trained_models_folder, model_directory_name), settings)
os.path.join(stored_training_data.trained_models_folder, model_directory_name), settings_ms2deepscore)
# Create performance plots for validation spectra
ms2deepsore_model_file_name = os.path.join(stored_training_data.trained_models_folder,
model_directory_name,
settings.model_file_name)
settings_ms2deepscore.model_file_name)
if settings_embedding_evaluation:
model = EmbeddingEvaluationModel(settings_embedding_evaluation)
model.train_evaluator(ms2ds_model=load_model(ms2deepsore_model_file_name),
training_spectra=training_spectra,
validation_spectra=validation_spectra)
model.save(os.path.join(stored_training_data.trained_models_folder, model_directory_name,
"embedding_evaluator.pt"))

calculate_true_values_and_predictions_for_validation_spectra(
positive_validation_spectra=stored_training_data.load_positive_train_split("validation"),
negative_validation_spectra=stored_training_data.load_negative_train_split("validation"),
Expand All @@ -59,7 +70,7 @@ def train_ms2deepscore_wrapper(spectra_file_path,

create_plots_between_all_ionmodes(model_directory=os.path.join(stored_training_data.trained_models_folder,
model_directory_name),
ref_score_bins=settings.same_prob_bins)
ref_score_bins=settings_ms2deepscore.same_prob_bins)

return model_directory_name

Expand Down
Loading