SciPy 2024

mrfmsim: a modular simulation platform for magnetic resonance force microscopy experiments
07-10, 14:35–15:05 (US/Pacific), Room 316

We present mrfmsim, an open-source framework that facilitates the design, simulation, and signal validation of magnetic resonance force microscopy experiments. The mrfmsim framework uses directed acyclic graphs (DAGs) to model experiments and employs a plugin system that adds custom experiments and functionalities. Differing from common DAG-powered workflow packages, mrfmsim allows flexible customizations of experiments post-definition without rewriting the internal model, such as optimized looping. In the talk, we present the challenges in building simulation packages for experiments undergoing continuous development in a graduate research setting. We discuss the current one-off approach that led to error-prone code and how modularity, extendibility, and readability can speed up the development cycle.


Magnetic resonance force microscopy (MRFM) is a scanning probe technique that can, in principle, determine spins' orientation and three-dimensional location with angstrom precision [1, 2]. The method opens up myriad possibilities, such as determining the three-dimensional structure of biological complexes. The experiments are inherently complex and have a low signal-to-noise ratio. As a result, simulations are needed to aid the experiment design, signal recovery, and signal-to-noise analysis and as a starting point for spin-density reconstruction in imaging experiments.

The complexity of MRFM experiments and their constantly evolving nature presents a unique challenge for simulations:

  1. The experiments share standard components, such as a cantilever, a radiofrequency or microwave source, a magnet, and a sample. Yet, they vary in component parameters, detection algorithms, and signal type. Therefore, the simulation needs a model-building workflow that can easily reuse shared components while maintaining the flexibility to implement different experiments and updates.
  2. For a scientific technique under constant development, collaboration within the research group or among the community is crucial. Therefore, the code needs to be extendable to add additional functionalities, readable for communication, and testable for peer review.
  3. The simulation is used for graduate students with varying programming backgrounds. Hence, the platform should provide a shallower learning curve for executing the simulation with some modifications.

The conventional one-off approach often simulates each experiment of interest. The code is unreadable and lacks tests, which led to incorrect publication results in many scientific fields. In this talk, we present mrfmsim [3], an open-source Python platform for simulating MRFM experiments that is modular, extendable, and readable. For modularity and readability, the platform uses directed acyclic graphs through NetworkX [4] to model each experiment, using a node as a functional step and an edge as the data flow. However, unlike popular workflow packages such as Dask and Airflow [5, 6], we developed mmodel [7] as the backend that allows flexibility to modify the model's post-definition. For example, MRFM experiments often require simulating the spectra of signals by looping the external magnetic field or microwave frequency. Due to the expensive matrix computations, we would like to optimize the code to only loop functions that require the field or frequency parameter. Using mrfmsim, we can quickly generate these loops without duplicating the code or changing the internal model. For extendibility, mrfmsim uses a simple plugin system that allows different groups to develop and add additional experiments and functionalities. In addition, mrfmsim can define models using YAML configuration files and provide a command-line interface. Using mrfmsim, we have dramatically reduced our development cycles, leading to discoveries in new physics [8].

We want to show the SciPy community the challenges we face in developing simulation platforms for a constantly evolving scientific technique in a graduate research setting. We hope our discoveries and efforts can help other research groups tackle similar problems.

[1] D. Rugar, et al., Single spin detection by magnetic resonance force microscopy. Nature, 430(6997):329-332, July 2004.
[2] E. Moore, et al., Scanned-probe detection of electron spin resonance from a nitroxide spin probe. Proceedings of the National Academy of Sciences, 106(52):22251-22256, December 2009.
[3] P. Sun, et al., mrfmsim, https://github.com/Marohn-Group/mrfmsim.
[4] A. A. Hagberg, et al., Exploring network structure, dynamics, and function using NetworkX. Proceedings of the 7th Python in Science Conference, pp. 11–15, 2008.
[5] M. Rocklin, Dask: Parallel computation with blocked algorithms and task scheduling, Python in Science Conference, pp.126-132, 2015.
[6] Apache Airflow, The Apache Software Foundation, 2024.
[7] P. Sun, et al., mmodel: A workflow framework to accelerate the development of experimental simulations. The Journal of Chemical Physics, 159(4):044801, July 2023.
[8] M. Boucher, et al., The Landau-Zener-Stuckelberg-Majorana transition in the T2<<T1 limit. Journal of Magnetic Resonance, 354:107523, September 2023.

Dr. Peter Sun is a Physical Chemistry Postdoc in the John Marohn Group at Cornell University. His research focuses on developing simulation and reconstruction of 3D images and fabricating nanoscale detection devices for magnetic resonance force microscopy experiments.