Skip to content

Devguru-codes/Bayesian-DSC-Precompiled

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Bayesian-DSC

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.

Features

  • Uses a pre-compiled Stan gamma-variate model through CmdStanPy
  • Exposes a backend-swappable BaseDSCFitter abstraction 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, and MTT
  • Includes a reproducible synthetic-data simulation and plot export

Installation

  1. Create a virtual environment in the project root:
python -m venv .venv
  1. Activate it.

On Windows PowerShell:

.\.venv\Scripts\Activate.ps1
  1. Install the Python dependencies:
pip install -r requirements.txt
  1. 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 -c

This downloads and builds CmdStan under the user CmdStan directory, typically C:\Users\<username>\.cmdstan.

Usage

Run the synthetic demonstration script from the project root:

python simulate_dsc.py

On your Windows setup, the easiest option is the included helper script, which configures the RTools toolchain before launching the demo:

.\run_simulation.ps1

The 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.png in the project root

If you prefer to avoid activating the environment explicitly on Windows, you can run:

.\.venv\Scripts\python simulate_dsc.py

If Stan compilation on Windows picks up the wrong C:\MinGW toolchain, prefer the helper script above because it prepends the correct RTools paths automatically.

Tests

Run the lightweight unit tests from the project root:

.\.venv\Scripts\python -m unittest discover -s tests

Project Structure

.
|-- .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

Notes

  • The first DSCBayesianFitter instantiation 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 NaN outputs instead of crashing.
  • The repository .gitignore excludes the local virtual environment, Python caches, CmdStan directories, and the generated fit_result.png.
  • On Windows systems that already have an older C:\MinGW toolchain installed, make sure the RTools paths provided by cmdstanpy.install_cmdstan -c take precedence when compiling Stan models.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors