Skip to main content

UltraMS

Foundation models for MS/MS spectra. Obtain spectrum-level embeddings or fine-tune the encoder with PyTorch.

Install

Python 3.10 or newer:

python -m pip install ultrams

For a particular CPU, CUDA, ROCm, or Apple Silicon setup, select the appropriate PyTorch installation first. To install the current GitHub source instead:

python -m pip install git+https://github.com/Dsadd4/UltraMS.git

Get a spectrum embedding

from ultrams import UltraMS

model = UltraMS.from_pretrained("unsupervised")
embedding = model.encode(
    mz=[100.1, 121.1, 150.0], intensity=[20, 100, 35], precursor_mz=301.2
).embedding  # NumPy vector

Fine-tune with PyTorch

UltraMS is a torch.nn.Module. The two rows below show the required data format; replace them with your labelled spectra.

import torch
from torch.utils.data import DataLoader
from ultrams import UltraMS

dataset = [
    {"mz": [100.1, 121.1, 150.0], "intensity": [20, 100, 35], "precursor_mz": 301.2, "target": 0.5},
    {"mz": [102.1, 135.2, 167.3], "intensity": [40, 100, 25], "precursor_mz": 315.3, "target": 0.7},
]

device = "cuda" if torch.cuda.is_available() else "cpu"
model = UltraMS.from_pretrained("unsupervised", device=device).train()
head = torch.nn.Linear(model.embedding_dim, 1).to(device)
loader = DataLoader(dataset, batch_size=2, collate_fn=model.batch_converter())
optimizer = torch.optim.AdamW([*model.parameters(), *head.parameters()], lr=1e-5)

for batch in loader:
    batch = {name: value.to(device) for name, value in batch.items()}
    prediction = head(model(batch["peaks"], batch["attention_mask"], batch["precursor_mz"]))
    loss = torch.nn.functional.mse_loss(prediction.squeeze(-1), batch["target"])
    optimizer.zero_grad()
    loss.backward()
    optimizer.step()
    print(f"loss: {loss.item():.4f}")

Fine-tune in one call

For a shorter route, provide the same spectra with a label field.

import torch
from ultrams import UltraMS

labelled_spectra = [
    {"mz": [100.1, 121.1, 150.0], "intensity": [20, 100, 35], "precursor_mz": 301.2, "label": 0.5},
    {"mz": [102.1, 135.2, 167.3], "intensity": [40, 100, 25], "precursor_mz": 315.3, "label": 0.7},
]

device = "cuda" if torch.cuda.is_available() else "cpu"
model = UltraMS.from_pretrained("unsupervised", device=device)
predictor = model.finetune(labelled_spectra, task="regression", epochs=1)
prediction = predictor.predict([100.1, 121.1, 150.0], [20, 100, 35], precursor_mz=301.2)
print(prediction)

Use task="classification" for class labels. Fine-tuning saves the model, training configuration, and loss history in ultrams_finetune/.

Choose a pretrained model

Model Learned representation Use Weights
Unsupervised ("unsupervised") Encoder spectrum-level embedding, $h_{\mathrm{CLS}}$ General MS/MS representation and fine-tuning Hugging Face
MoNA contrastive ("mona") Embedding projection of $h_{\mathrm{CLS}}$ Spectrum similarity learned on MoNA Hugging Face
Search ("search") Embedding projection of $h_{\mathrm{CLS}}$ Spectrum-to-spectrum similarity used to build UltraAtlas Hugging Face

encode(...).embedding returns the representation in the table. To use a downloaded model.pt directly, call UltraMS.from_checkpoint(path).

Pretraining

The UltraMSdata pretraining code has a separate installation and requires prepared UltraMSdata.

Metadata

Release files for ultrams 0.1.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ultrams 0.1.3
File Size Uploaded
ultrams-0.1.3.tar.gz 16.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for ultrams 0.1.3
File Interpreter ABI Platform
ultrams-0.1.3-py3-none-any.whl Python 3 none any Details

Total release size: 32.6 kB

Release files / ultrams-0.1.3.tar.gz

Download URL ultrams-0.1.3.tar.gz
Size 16.1 kB
Tags Source
SHA-256 checksum
How to use checksums
64abbf5010f9b1529fc5866b645affe54e32b7b5f60461388efb85fe44c0e694
BLAKE2b-256 checksum
How to use checksums
6f5aafabd52cc5ea58933a90aa5fd9064f0a7e2761b7e51ef86634914817a574
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 24, 2026.

Transparency log

Release files / ultrams-0.1.3-py3-none-any.whl

Download URL ultrams-0.1.3-py3-none-any.whl
Size 16.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
019cede128e419242e456783943669e8296afc4f50b6588dff86dbe59f377828
BLAKE2b-256 checksum
How to use checksums
1f64faf0e8694c8c337966d8476cad3da59c14098a9ed8c17af601cfa4869b48
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 24, 2026.

Transparency log

Release history Release notifications | RSS feed

0.3.0

2 release files

0.2.0

2 release files

This release

0.1.3 This release

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page