Skip to main content
PyLCModel Logo

PyLCModel

A lightweight Python wrapper for LCModel spectral fitting in MR spectroscopy

PyPI version Python License

PyLCModel is a lightweight Python wrapper that streamlines the use of LCModel for least-squares spectral fitting in MRS. It automates control-file generation, handles flexible data input, manages the LCModel executable for you, and parses the output (with single- and multi-core processing).


Features

  • Zero-setup binaries — the LCModel executable is resolved automatically (download, container image, build from source, or your own path); nothing is bundled in the wheel.
  • Flexible input — NumPy arrays, NIfTI-MRS, jMRUI text, and LCModel .RAW, in time or frequency domain.
  • Automated control files — generated to match your data, or templated from an existing one.
  • Basis conversion (experimental) — jMRUI, FSL-MRS, LCModel .RAW, and Osprey/FID-A basis sets to .basis.
  • Batch fitting — single- or multi-core, with full output parsing (concentrations, CRLBs, QC, fitted series).

Installation

From PyPI

pip install lcmodel-wrapper

From Source

git clone https://github.com/julianmer/PyLCModel.git
cd PyLCModel
pip install -e .

Add --recursive to the clone (or run git submodule update --init) to also fetch the ISMRM 2016 fitting challenge example data used by the tests.


How the LCModel binary is handled

LCModel is not shipped in the wheel. On first use it is found in this order, and the first one that works is cached:

  1. path2exec you pass to PyLCModel,
  2. the community binary for your OS/architecture from schorschinho/LCModel,
  3. the binary built by this repository's CI for the installed version (releases; Linux x86_64/aarch64 and macOS arm64/x86_64, all statically linked),
  4. the container image ghcr.io/julianmer/lcmodel, if Docker or podman is running,
  5. a build from source with gfortran.

Each candidate is run once before it is accepted, so a binary that cannot run on your machine is skipped rather than cached. Useful switches: allow_download, allow_docker, allow_build on PyLCModel, and the LCMODEL_EXEC / LCMODEL_CACHE_DIR environment variables.

With the container, LCModel sees your working directory and your home directory; keep the basis set and any save_path under one of them. The image also works on its own:

docker run --rm -i -v "$PWD:$PWD" -w "$PWD" ghcr.io/julianmer/lcmodel < control.file

Getting Started

from lcmodel_wrapper import PyLCModel

# Initialize the wrapper with your basis set (the LCModel binary is resolved automatically)
lcmodel = PyLCModel(path2basis="/path/to/your/basis_set.basis")

# `data` can be a NumPy array of FIDs (time domain), a NIfTI-MRS path, etc.
concentrations, crlbs = lcmodel(data)

print("Fitted Metabolite Concentrations:", concentrations)
print("CRLBs:", crlbs)

Frequency-domain input or a custom executable:

lcmodel = PyLCModel(
    path2basis="/path/to/basis.basis",
    domain="freq",                 # pass spectra instead of FIDs
    path2exec="/path/to/lcmodel",  # optional: use your own binary
)

Experimental basis conversion (other formats -> .basis):

# Auto-detect the source format (jMRUI/AQSES/QUEST .txt folder, FSL-MRS .json folder,
# LCModel .RAW folder, or Osprey/FID-A .mat):
lcmodel = PyLCModel(path2basis="/path/to/basis_folder", convert_basis=True)

# ...or force a format and supply parameters the source does not carry:
lcmodel = PyLCModel(
    path2basis="/path/to/raw_folder",
    convert_basis=True,
    basis_format="raw",          # "jmrui" | "fsl" | "raw" | "mat"
    bandwidth=4000, central_freq=123.25,
)

# Or convert directly without fitting:
from lcmodel_wrapper import convert_to_basis
convert_to_basis("/path/to/jmrui_folder", out_path="out.basis")

Basis conversion is experimental and not validated. For a dedicated, more complete tool, see the MRS Basis Set Conversion Toolbox.


Licensing

This wrapper (the Python code) is released under the Apache License 2.0 (see LICENSE).

LCModel itself is a separate program by Dr. Stephen Provencher, distributed under the BSD 3-Clause License (see LICENSE.lcmodel). This package does not bundle LCModel; when it downloads, builds, or runs the LCModel executable, that BSD-3-Clause license and the attributions in NOTICE apply. See the LCModel home page for details.


Acknowledgements


Built with ❤️ for the MRS community

Metadata

Release files for lcmodel-wrapper 0.6.0

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

Source distribution (sdist)

Source distribution for lcmodel-wrapper 0.6.0
File Size Uploaded Provenance Actions
lcmodel_wrapper-0.6.0.tar.gz 51.9 kB Without provenance Details for lcmodel_wrapper-0.6.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for lcmodel-wrapper 0.6.0
File Interpreter ABI Platform Actions
lcmodel_wrapper-0.6.0-py3-none-any.whl Python 3 none any Details for lcmodel_wrapper-0.6.0-py3-none-any.whl

Total release size: 96.3 kB

Release files / lcmodel_wrapper-0.6.0.tar.gz

Download URL lcmodel_wrapper-0.6.0.tar.gz
Size 51.9 kB
Tags Source
SHA-256 checksum
How to use checksums
027482180f6e1e8e7d76e71ca821c00110172b28d9460f5cb88f283dc93fe587
BLAKE2b-256 checksum
How to use checksums
aebb07032d5dc51acb377ec37a84067da220eb9d1dacc91feb5c55b0972cdb6c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Release files / lcmodel_wrapper-0.6.0-py3-none-any.whl

Download URL lcmodel_wrapper-0.6.0-py3-none-any.whl
Size 44.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9525f60e55644f03bc5be1812b73cc91d3ac4a82acb9c54bb14ad8d34e604f9b
BLAKE2b-256 checksum
How to use checksums
3614e3e23ac0d75ed5dd7bb11a09af7195573361b3bb7fb332adf8368fa906e9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Security for lcmodel-wrapper 0.6.0

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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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