PyPI package: training

MolNet can train all task types (MS/MS, RT, CCS) directly from Python, without the command-line scripts. For CLI-based training, see Source code usage.

molnetpack.MolNet.train(self, task, train_data, valid_data, checkpoint_path='', resume_path='', transfer=False, precursor_type='All', use_scaler=False)

Train a model and store it on this MolNet instance.

After training the model is ready for immediate use via pred_msms, pred_rt, or pred_ccs — no checkpoint reload needed.

Parameters:
  • task (str) – One of 'msms', 'rt', 'ccs'.

  • train_data (str) – Path to training PKL file.

  • valid_data (str) – Path to validation PKL file.

  • checkpoint_path (str) – Where to save the best checkpoint. Empty string disables saving.

  • resume_path (str) – Resume from or transfer-learn from this checkpoint.

  • transfer (bool) – If True, load only encoder weights from resume_path and freeze them.

  • precursor_type (str) – Filter training data by precursor type (msms task only). One of 'All', '[M+H]+', '[M-H]-'.

  • use_scaler (bool) – Fit a StandardScaler on training targets (rt task only).

Returns:

Best validation metric achieved during training.

Return type:

float

molnetpack.MolNet.evaluate(self, test_pkl, pred_mgf, result_path='', plot_path='')

Compare predicted MS/MS spectra against ground-truth spectra.

Parameters:
  • test_pkl (str) – Path to the ground-truth PKL file (from preprocessing).

  • pred_mgf (str) – Path to the predicted spectra MGF file (from pred_msms).

  • result_path (str) – Optional path to save per-spectrum results as CSV.

  • plot_path (str) – Optional path to save a cosine similarity histogram PNG.

Returns:

DataFrame with per-spectrum cosine similarity and metadata.

Return type:

pandas.DataFrame

MS/MS model training

Fine-tune from a pretrained checkpoint:

import torch
from molnetpack import MolNet

device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
molnet_engine = MolNet(device, seed=42)

best_cosine = molnet_engine.train(
    task='msms',
    train_data='./data/qtof_etkdgv3_train.pkl',
    valid_data='./data/qtof_etkdgv3_test.pkl',
    checkpoint_path='./check_point/molnet_qtof_etkdgv3_tl.pt',
    resume_path='./check_point/molnet_pre_etkdgv3.pt',
    transfer=True,
)

# The trained model is ready for inference immediately — no reload needed
molnet_engine.load_data('./examples/input_msms.csv')
pred_df = molnet_engine.pred_msms(instrument='qtof')

Evaluate predictions against ground truth:

results_df = molnet_engine.evaluate(
    test_pkl='./data/qtof_etkdgv3_test.pkl',
    pred_mgf='./result/pred_qtof_etkdgv3_test.mgf',
    result_path='./eval_qtof_etkdgv3_test.csv',
    plot_path='./eval_qtof_etkdgv3_test.png',
)

Retention time model training

Fine-tune from a pretrained MS/MS checkpoint using transfer learning:

import torch
from molnetpack import MolNet

device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
molnet_engine = MolNet(device, seed=42)

best_mae = molnet_engine.train(
    task='rt',
    train_data='./data/metlin_etkdgv3_train.pkl',
    valid_data='./data/metlin_etkdgv3_test.pkl',
    checkpoint_path='./check_point/molnet_rt_etkdgv3_tl.pt',
    resume_path='./check_point/molnet_qtof_etkdgv3.pt',
    transfer=True,
    use_scaler=True,
)

CCS model training

Fine-tune from a pretrained MS/MS checkpoint:

import torch
from molnetpack import MolNet

device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
molnet_engine = MolNet(device, seed=42)

best_mae = molnet_engine.train(
    task='ccs',
    train_data='./data/allccs_etkdgv3_train.pkl',
    valid_data='./data/allccs_etkdgv3_test.pkl',
    checkpoint_path='./check_point/molnet_ccs_etkdgv3_tl.pt',
    resume_path='./check_point/molnet_qtof_etkdgv3.pt',
    transfer=True,
)