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:
path2execyou pass toPyLCModel,- the community binary for your OS/architecture from schorschinho/LCModel,
- 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),
- the container image
ghcr.io/julianmer/lcmodel, if Docker or podman is running, - 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
- LCModel source code: created and made available free of charge (Stephen Provencher).
- LCModel binaries: schorschinho/LCModel (Georg Oeltzschner and contributors)
- Basis conversion reference: MRS Basis Set Conversion Toolbox (Kay Igwe)
- NIfTI-MRS: spec2nii, NIfTI-MRS Python tools (Will Clarke)
- Example data: ISMRM 2016 MRS Fitting Challenge (Małgorzata Marjańska, Dinesh Deelchand, Roland Kreis), mirrored via wtclarke/mrs_fitting_challenge
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file lcmodel_wrapper-0.3.1.tar.gz.
File metadata
- Download URL: lcmodel_wrapper-0.3.1.tar.gz
- Upload date:
- Size: 47.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ab852a2eb8bfdd1e08a757c2b118df3825c93468c9b28a16f32396525aee9e2f
|
|
| MD5 |
b8e68ddde17cf6b0ad3876117e33665f
|
|
| BLAKE2b-256 |
74822d6d97c9af8f32e59b833d96e2f548569d0beb034d6d61b4bebd0ac0edf5
|
File details
Details for the file lcmodel_wrapper-0.3.1-py3-none-any.whl.
File metadata
- Download URL: lcmodel_wrapper-0.3.1-py3-none-any.whl
- Upload date:
- Size: 43.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
75021a62d1e94e79072f78e2dff366be5175ec72fe1b5332cafb2d2480f6cc17
|
|
| MD5 |
4d9593a7b0ecb57ec4db621d876de450
|
|
| BLAKE2b-256 |
d3b814084395f47643f30c13102c736a3f8814aaea69c4164cd7d080f7887d4f
|