deeplc.data

Dataset classes and utilities for DeepLC.

class deeplc.data.DeepLCDataset(peptidoforms, target_retention_times=None, add_ccs_features=False, add_terminal_composition=False, padding_length=60, legacy_positional_deltas=True, include_rolling_sum=True, vectorised_encoding=True)

Bases: Dataset

Initialize the DeepLCDataset.

Parameters:
  • peptidoforms (list[Peptidoform | str]) – A list of peptidoforms, which can be either Peptidoform objects or their string representations.

  • target_retention_times (np.ndarray | None) – An array of target retention times corresponding to the peptidoforms. If None, targets will be set to NaN.

  • add_ccs_features (bool) – Whether to include CCS features in the encoded representation. Default is False.

  • add_terminal_composition (bool) – Whether to append the N- and C-terminal group composition to the global feature vector, lengthening it from 55 to 67. Required by models trained on that layout; see the feature_spec recorded in such a model. Default is False.

  • padding_length (int) – Length the per-position matrices are padded or truncated to. Must match the value the model was trained with: the fused-trunk architecture pools rather than flattens, so a mismatch changes the representation without changing any shape and would not raise. Default is 60.

  • legacy_positional_deltas (bool) –

    Whether to place modification deltas in the positional block the way versions before 4.0.1 did. That placement was wrong and 4.0.1 corrected it, but every model released against this dataset class was trained on it, so the default is True: a dataset exists to feed a model, and feeding a model an encoding it was not trained on changes its predictions on modified peptides without any error.

    Set it to False for a model trained after the correction. deeplc.core.predict() and deeplc.core.finetune() do this automatically from the feature_spec a self-describing checkpoint carries, and deeplc.core.train() does it for newly trained models. Note that deeplc._features.encode_peptidoform(), whose job is correct featurisation rather than model compatibility, defaults the other way. Affects modified peptidoforms only.

  • include_rolling_sum (bool) – Whether to build the rolling-sum matrix. A convolutional trunk reads the per-position matrix directly and ignores this one, so building it is pure cost for such a model; False puts an empty array in its place. Default is True.

  • vectorised_encoding (bool) – Whether encode_batch() may gather the residue-only part of a batch in one pass instead of encoding peptide by peptide. It produces the same values, dtype included, and falls back per peptide for anything it does not cover; False forces the per-peptide encoder for everything. Default is True.

Raises:

ValueError – If target_retention_times is provided and its length does not match the number of peptidoforms.

variant(indices, padding_length)

Return a subset of this dataset’s peptidoforms, encoded in a shorter window.

Used by the prediction path to run short peptides in a window that fits them instead of padding every one to the model’s full length. The peptidoform objects themselves are shared rather than copied, so the parsing psm_utils caches on them is not paid twice.

Parameters:
  • indices (Sequence[int]) – Positions in this dataset to include, in the order wanted.

  • padding_length (int) – Window the subset is encoded in.

Return type:

DeepLCDataset

FEATURE_KEYS = ('matrix', 'matrix_sum', 'matrix_global', 'matrix_hc')

Arrays the encoder returns, in the order the models take them.

encode_batch(indices)

Encode several peptidoforms straight into one tensor per feature.

__getitem__() builds four arrays and four tensors for a single peptide, which a DataLoader then stacks into a batch: several copies of a few hundred bytes each with a good deal of Python around them. Writing the encoder output into batch buffers instead measured 1.7x faster over the whole feature path, 0.153 against 0.089 ms per peptide, and returns the same values.

Parameters:

indices (Sequence[int]) – Positions in this dataset to encode, in the order wanted.

Return type:

tuple[Tensor, …]

classmethod from_psm_list(psm_list, add_ccs_features=False, add_terminal_composition=False, padding_length=60, legacy_positional_deltas=True, include_rolling_sum=True, vectorised_encoding=True)

Create a DeepLCDataset from a PSMList.

Parameters:
  • psm_list (PSMList) – A PSMList containing the peptidoforms and their corresponding retention times.

  • add_ccs_features (bool) – Whether to include CCS features in the encoded representation. Default is False.

  • add_terminal_composition (bool) – Whether to append the N- and C-terminal group composition to the global feature vector, lengthening it from 55 to 67. Default is False.

  • padding_length (int) – Length the per-position matrices are padded or truncated to. Must match the value the model was trained with: the fused-trunk architecture pools rather than flattens, so a mismatch changes the representation without changing any shape and would not raise. Default is 60.

  • legacy_positional_deltas (bool) –

    Whether to place modification deltas in the positional block the way versions before 4.0.1 did. That placement was wrong and 4.0.1 corrected it, but every model released against this dataset class was trained on it, so the default is True: a dataset exists to feed a model, and feeding a model an encoding it was not trained on changes its predictions on modified peptides without any error.

    Set it to False for a model trained after the correction. deeplc.core.predict() and deeplc.core.finetune() do this automatically from the feature_spec a self-describing checkpoint carries, and deeplc.core.train() does it for newly trained models. Note that deeplc._features.encode_peptidoform(), whose job is correct featurisation rather than model compatibility, defaults the other way. Affects modified peptidoforms only.

  • include_rolling_sum (bool) – Whether to build the rolling-sum matrix. Models with a convolutional trunk ignore it, and deeplc.core.predict() sets this from the model.

  • vectorised_encoding (bool) – Whether a batch may be encoded in one pass; see the class docstring.

Returns:

A DeepLCDataset instance created from the provided PSMList.

Return type:

DeepLCDataset

deeplc.data.split_datasets(train_data: _DatasetT, validation_data: _DatasetT, validation_split: float) → tuple[_DatasetT, _DatasetT]
deeplc.data.split_datasets(train_data: _DatasetT, validation_data: None, validation_split: float) → tuple[Subset[_DatasetT], Subset[_DatasetT]]

Split the dataset into training and validation sets.

Parameters:
  • train_data (Dataset) – The dataset to be split.

  • validation_data (Dataset | None) – If provided, this dataset will be used as the validation set. If None, the train_data will be split.

  • validation_split (float) – The fraction of the dataset to be used as the validation set if validation_data is None.

Returns:

A tuple containing the training and validation datasets.

Return type:

tuple[Dataset, Dataset] | tuple[Subset, Subset]

Raises:

ValueError – If validation_data is None and train_data does not implement __len__ method.