Bayesian-DSC is a small portfolio project that demonstrates Bayesian fitting of Dynamic Susceptibility Contrast (DSC) MRI concentration-time curves with CmdStanPy. The core design goal is to compile the Stan model once when the fitter is instantiated, then reuse that compiled executable for repeated curve fitting.
- Uses a pre-compiled Stan gamma-variate model through
CmdStanPy - Exposes a backend-swappable
BaseDSCFitterabstraction for future PyMC or NumPyro implementations - Fits noisy DSC concentration-time curves with Bayesian inference
- Returns posterior means and 95% HDI bounds for
A,alpha,beta,t0,sigma,CBV, andMTT - Includes a reproducible synthetic-data simulation and plot export
- Create a virtual environment in the project root:
python -m venv .venv- Activate it.
On Windows PowerShell:
.\.venv\Scripts\Activate.ps1- Install the Python dependencies:
pip install -r requirements.txt- Install the underlying CmdStan toolchain.
On Windows, the simplest option is to let cmdstanpy install the required compiler toolchain if needed:
python -m cmdstanpy.install_cmdstan -cThis downloads and builds CmdStan under the user CmdStan directory, typically C:\Users\<username>\.cmdstan.
Run the synthetic demonstration script from the project root:
python simulate_dsc.pyOn your Windows setup, the easiest option is the included helper script, which configures the RTools toolchain before launching the demo:
.\run_simulation.ps1The script will:
- generate a synthetic DSC concentration-time curve
- add Gaussian noise
- compile and sample the Stan model
- print true parameters, posterior means, and 95% HDI intervals
- save
fit_result.pngin the project root
If you prefer to avoid activating the environment explicitly on Windows, you can run:
.\.venv\Scripts\python simulate_dsc.pyIf Stan compilation on Windows picks up the wrong C:\MinGW toolchain, prefer the helper script above because it prepends the correct RTools paths automatically.
Run the lightweight unit tests from the project root:
.\.venv\Scripts\python -m unittest discover -s tests.
|-- .gitignore
|-- README.md
|-- requirements.txt
|-- run_simulation.ps1
|-- simulate_dsc.py
|-- tests
| `-- test_bayesian_fitter.py
`-- src
|-- __init__.py
|-- bayesian_fitter.py
`-- stan_models
`-- gamma_variate.stan
- The first
DSCBayesianFitterinstantiation may take longer because Stan compilation happens at that point. - Subsequent calls to
.fit_curve(...)on the same fitter instance reuse the already compiled executable. - If the Stan source is unchanged and the executable already exists, CmdStanPy can also reuse that executable on later runs.
- If compilation or sampling fails, the wrapper safely returns
NaNoutputs instead of crashing. - The repository
.gitignoreexcludes the local virtual environment, Python caches, CmdStan directories, and the generatedfit_result.png. - On Windows systems that already have an older
C:\MinGWtoolchain installed, make sure the RTools paths provided bycmdstanpy.install_cmdstan -ctake precedence when compiling Stan models.