Skip to main content

Python bindings for Monte Carlo eXtreme photon transport simulator

Project description

PMCX - Python bindings for Monte Carlo eXtreme photon transport simulator

  • Copyright: (C) Matin Raayai Ardakani (2022-2023) <raayaiardakani.m at northeastern.edu>, Qianqian Fang (2019-2024) <q.fang at neu.edu>, Fan-Yu Yen (2023-2024) <yen.f at northeastern.edu>
  • License: GNU Public License V3 or later
  • Version: 0.3.3
  • URL: https://pypi.org/project/pmcx/
  • Github: https://github.com/fangq/mcx

Linux Python Module
MacOS Python Module
Windows Python Module

This module provides a Python binding for Monte Carlo eXtreme (MCX). For other binaries, including the standalone executable and the MATLAB bindings, see our website.

Monte Carlo eXtreme (MCX) is a fast photon transport simulation software for 3D heterogeneous turbid media. By taking advantage of the massively parallel threads and extremely low memory latency in a modern graphics processing unit (GPU), MCX is capable of performing Monte Carlo (MC) photon simulations at a blazing speed, typically hundreds to a thousand times faster than a fully optimized CPU-based MC implementation.

How to Install

Runtime Dependencies

  • NVIDIA GPU Driver: A CUDA-capable NVIDIA GPU and driver is required to run MCX. An up-to-date driver is recommended. The binary wheel distributed over pip runs on NVIDIA drivers with CUDA 10.1 support on Windows, CUDA 9.2 support on Linux, and CUDA 10.2 support on macOS, respectively. For more details on driver versions and their CUDA support, see the CUDA Release Notes. To download the latest driver for your system, see the NVIDIA Driver Download Page. You shouldn't need to have CUDA toolkit installed. MCX is built with the static CUDA runtime library.
  • Python: Python 3.6 and newer is required. Python 2 is not supported.
  • numpy: Used to pass/receive volumetric information to/from pmcx. To install, use either conda or pip package managers: pip install numpy or conda install numpy
  • (optional) jdata: Only needed to read/write JNIfTI output files. To install, use pip: pip install jdata on all operating systems; For Debian-based Linux distributions, you can also install to the system interpreter using apt-get: sudo apt-get install python3-jdata. See https://pypi.org/project/jdata/ for more details.
  • (optional) bjdata: Only needed to read/write BJData/UBJSON files. To install, run pip install bjdata on all operating systems; For Debian-based Linux distributions, you can also install to the system interpreter using apt-get: sudo apt-get install python3-bjdata. See https://pypi.org/project/bjdata/ for more details.
  • (optional) matplotlib: For plotting the results. To install, run either pip install matplotlib or conda install matplotlib

Build Instructions

Build Dependencies

  • Operating System: Windows and Linux are fully supported; For building MCX on macOS, OSX 10.13 (High Sierra) and older are highly recommended since 10.13 was the last version of macOS with NVIDIA CUDA support, and matching the CUDA compiler version with the C/C++ compiler shipped with Xcode is easier. Newer macOS versions can be used for building MCX, but need to have System Integrity Protection disabled prior to installing the CUDA toolkit due to the NVIDIA installer copying its payload under the /Developer directory under root.

  • NVIDIA CUDA Toolkit: CUDA 7.5 or newer is required. On macOS, 10.2 is the last available CUDA version. For details on how to install CUDA, see the CUDA Download Page. The NVIDIA GPU driver of the target system must support the selected CUDA toolkit.

  • Python Interpreter: Python 3.6 or above. The pip Python package manager and the wheel package (available via pip) are not required but recommended.

  • C/C++ Compiler: CUDA Toolkit supports only the following compilers:

    • GNU GCC for Linux-based distributions.
    • Microsoft Visual Studio C/C++ Compiler for Windows.
    • Apple Clang for macOS, available via Xcode. The last Xcode version supported by CUDA 10.2 is 10.3. If using an OSX version higher than 10.15 it can be downloaded and installed from Apple's Developer Website with an Apple ID. After installation, select the proper Xcode version from the commandline, and set the SDKROOT environment variable:
      sudo xcode-select -s /Applications/Xcode_10.3.app/Contents/Developer/
      export SDKROOT=/Applications/Xcode_10.3.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk
      

    Refer to each OS's online documentations for more in-depth information on how to install these compilers. Note that the version of the C/C++ compiler used must be supported by the CUDA toolkit version. If not, compilation will fail with an error notifying you of this problem. See the CUDA Installation Guides for more details.

  • OpenMP: The installed C/C++ Compiler should have support for OpenMP. GCC and Microsoft Visual Studio compiler support OpenMP out of the box. Apple Clang, however, requires manual installation of OpenMP libraries for Apple Clang. The easiest way to do this is via the Brew package manager, preferably after selecting the correct Xcode version:

      brew install libomp
      brew link --force libomp
    
  • CMake: CMake version 3.15 and later is required. Refer to the CMake website for more information on how to download. CMake is also widely available on package managers across all operating systems. Additionally, on Windows, make sure Visual Studio's C++ CMake tools for Windows is also installed by selecting its option during installation.

Build Steps

  1. Ensure that cmake, nvcc (NVIDIA CUDA Compiler) and the C/C++ compiler are all located over your PATH. This can be queried via echo $env:PATH on Windows or echo $PATH on Linux. If not, locate them and add their folder to the PATH.

  2. Clone the repository and switch to the pmcx/ folder:

        git clone --recursive https://github.com/fangq/mcx.git
        cd mcx/pmcx
    
  3. One can run python3 setup.py install or python3 -m pip install . to both locally build and install the module

  4. If one only wants to locally build the module, one should run python3 -m pip wheel .

  5. If the binary module is successfully built locally, you should see a binary wheel file pmcx-X.X.X-cpXX-cpXX-*.whl stored inside the mcx/pmcx folder. You can install this wheel package using python3 -m pip install --force-reinstall pmcx-*.whl to force installing this locally compiled pmcx module and overwrite any previously installed versions.

How to use

The PMCX module is easy to use. You can use the pmcx.gpuinfo() function to first verify if you have NVIDIA/CUDA compatible GPUs installed; if there are NVIDIA GPUs detected, you can then call the run() function to launch a photon simulation.

A simulation can be defined conveniently in two approaches - a one-liner and a two-liner:

  • For the one-liner, one simply pass on each MCX simulation setting as positional argument. The supported setting names are compatible to nearly all the input fields for the MATLAB version of MCX - MCXLAB)
import pmcx
import numpy as np
import matplotlib.pyplot as plt

res = pmcx.run(nphoton=1000000, vol=np.ones([60, 60, 60], dtype='uint8'), tstart=0, tend=5e-9, 
               tstep=5e-9, srcpos=[30,30,0], srcdir=[0,0,1], prop=np.array([[0, 0, 1, 1], [0.005, 1, 0.01, 1.37]]))
res['flux'].shape

plt.imshow(np.log10(res['flux'][30,:, :]))
plt.show()
  • Alternatively, one can also define a Python dict object containing each setting as a key, and pass on the dict object to pmcx.run(), or preferably, pmcx.mcxlab()
import pmcx
import numpy as np
cfg = {'nphoton': 1000000, 'vol':np.ones([60,60,60],dtype='uint8'), 'tstart':0, 'tend':5e-9, 'tstep':5e-9,
       'srcpos': [30,30,0], 'srcdir':[0,0,1], 'prop':[[0,0,1,1],[0.005,1,0.01,1.37]]}
res = pmcx.run(cfg)      # pmcx.run returns detected photon data as a concatenated 2D array res['detp'], same for res['traj']
# or alternatively/preferably
res = pmcx.mcxlab(cfg)   # pmcx.mcxlab calls pmcx.run, and postprocess res['detp'] and res['traj'] raw data into dict form

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

pmcx-0.3.3-pp310-pypy310_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (4.8 MB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ x86-64

pmcx-0.3.3-pp39-pypy39_pp73-win_amd64.whl (4.3 MB view hashes)

Uploaded PyPy Windows x86-64

pmcx-0.3.3-pp39-pypy39_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (4.8 MB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ x86-64

pmcx-0.3.3-pp39-pypy39_pp73-macosx_10_16_x86_64.whl (4.7 MB view hashes)

Uploaded PyPy macOS 10.16+ x86-64

pmcx-0.3.3-pp38-pypy38_pp73-win_amd64.whl (4.3 MB view hashes)

Uploaded PyPy Windows x86-64

pmcx-0.3.3-pp38-pypy38_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (4.8 MB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ x86-64

pmcx-0.3.3-pp38-pypy38_pp73-macosx_10_16_x86_64.whl (4.7 MB view hashes)

Uploaded PyPy macOS 10.16+ x86-64

pmcx-0.3.3-pp37-pypy37_pp73-win_amd64.whl (4.3 MB view hashes)

Uploaded PyPy Windows x86-64

pmcx-0.3.3-pp37-pypy37_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (4.8 MB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ x86-64

pmcx-0.3.3-pp37-pypy37_pp73-macosx_10_16_x86_64.whl (4.7 MB view hashes)

Uploaded PyPy macOS 10.16+ x86-64

pmcx-0.3.3-cp312-cp312-win_amd64.whl (4.3 MB view hashes)

Uploaded CPython 3.12 Windows x86-64

pmcx-0.3.3-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (4.8 MB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ x86-64

pmcx-0.3.3-cp312-cp312-macosx_10_16_universal2.whl (4.7 MB view hashes)

Uploaded CPython 3.12 macOS 10.16+ universal2 (ARM64, x86-64)

pmcx-0.3.3-cp311-cp311-win_amd64.whl (4.3 MB view hashes)

Uploaded CPython 3.11 Windows x86-64

pmcx-0.3.3-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (4.8 MB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

pmcx-0.3.3-cp311-cp311-macosx_10_16_universal2.whl (4.7 MB view hashes)

Uploaded CPython 3.11 macOS 10.16+ universal2 (ARM64, x86-64)

pmcx-0.3.3-cp310-cp310-win_amd64.whl (4.3 MB view hashes)

Uploaded CPython 3.10 Windows x86-64

pmcx-0.3.3-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (4.8 MB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

pmcx-0.3.3-cp310-cp310-macosx_11_0_x86_64.whl (4.7 MB view hashes)

Uploaded CPython 3.10 macOS 11.0+ x86-64

pmcx-0.3.3-cp39-cp39-win_amd64.whl (4.3 MB view hashes)

Uploaded CPython 3.9 Windows x86-64

pmcx-0.3.3-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (4.8 MB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

pmcx-0.3.3-cp39-cp39-macosx_11_0_x86_64.whl (4.7 MB view hashes)

Uploaded CPython 3.9 macOS 11.0+ x86-64

pmcx-0.3.3-cp38-cp38-win_amd64.whl (4.3 MB view hashes)

Uploaded CPython 3.8 Windows x86-64

pmcx-0.3.3-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (4.8 MB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

pmcx-0.3.3-cp38-cp38-macosx_11_0_x86_64.whl (4.7 MB view hashes)

Uploaded CPython 3.8 macOS 11.0+ x86-64

pmcx-0.3.3-cp37-cp37m-win_amd64.whl (4.3 MB view hashes)

Uploaded CPython 3.7m Windows x86-64

pmcx-0.3.3-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (4.8 MB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.17+ x86-64

pmcx-0.3.3-cp37-cp37m-macosx_11_0_x86_64.whl (4.7 MB view hashes)

Uploaded CPython 3.7m macOS 11.0+ x86-64

pmcx-0.3.3-cp36-cp36m-win_amd64.whl (4.3 MB view hashes)

Uploaded CPython 3.6m Windows x86-64

pmcx-0.3.3-cp36-cp36m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (4.8 MB view hashes)

Uploaded CPython 3.6m manylinux: glibc 2.17+ x86-64

pmcx-0.3.3-cp36-cp36m-macosx_10_16_x86_64.whl (4.7 MB view hashes)

Uploaded CPython 3.6m macOS 10.16+ x86-64

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page