Pharmpy: a versatile open-source library for pharmacometrics
Rikard Nordgren (1), Stella Belin (1), Xiaomei Chen (1), Johan Borg (1), Zhe Huang (1), Osama Qutishat (1), Keqi Shi (1), Xuanlin Liu (1), Shijun Wang (1), Samia Mohamed (1), Yersultan Mirasbekov (1), Andrew C. Hooker (1), Mats O. Karlsson (1)
(1) Department of Pharmacy, Uppsala University, Sweden
The development of pharmacometric nonlinear mixed effect models is a complex, multi-step process. Generally, it begins with an informed starting model, which is subsequently improved in a stepwise manner. Throughout the process, a modeler needs to master knowledge in multiple domains, including pharmacology and statistics. The technical component, i.e. how to implement and manipulate models, is an additional burden. Furthermore, having traceable and reproducible results is crucial, therefore scripting parts of the model building rather than making manual changes can improve the quality of the overall process.
Pharmpy is an open-source software package for pharmacometric modeling. It has functionality ranging from reading and manipulating model files and datasets, to full tools where subsequent results are collected and presented. Through a plugin mechanism it is possible to support different model languages and tools for estimation and simulation. Currently, Pharmpy supports NM-TRAN control streams and subsequent NONMEM [1] execution as well as nlmixr-models and execution [2]. Pharmpy is intended to be useful to tool developers as well as pharmacometric researchers and modelers, and has different Application Programming Interface (API) layers to cater to the different needs of the groups. Pharmpy is implemented as a Python library, but can also be used in R [3] via the pharmr package or via the command line interface (CLI).
The Pharmpy model
The main design principles of Pharmpy are modularity and tool agnosticism. A modular design allows for functions and tools to be reused and combined in new ways, while tool agnosticism means that the main part of the code base is not based on a specific modeling language and thus can be extended to new languages. The abstraction for nonlinear effects models lies at the core of Pharmpy, where a model is separated into components, such as the parameters, random variables, statements, and dataset. The components have their own classes with APIs that allows for low level manipulation. A Pharmpy model can be created both from scratch and by reading in and parsing an existing model. With this abstraction, a NONMEM model can be written by a modeler, parsed into a Pharmpy model, and then translated into a nlmixr model and estimated with nlmixr.
Manipulating and exploring NLME models
The general Pharmpy model can be manipulated from the low level API, but there are also more high-level transformations. Since Pharmpy is tool agnostic, they act on the general Pharmpy model object, and after these changes have been made the model language specific code can be generated, e.g. choosing the ADVAN for a NONMEM model. The package can do advanced model manipulation of PK models, including changing the absorption rate, absorption delay, elimination rate and distribution. Further development is ongoing to support a wider scope of models, such as multiple dosing, PKPD, and TMDD models. For general models, Pharmpy can do different transformations such as adding covariate effects, set variability on parameters, manipulate the covariance structure of random effects, update initial estimates, and transform values below limit of quantification.
Estimation results and tools
Pharmpy has mutiple tools that can create and estimate different models, as well as presenting the subsequent results. These tools range from a simple estimation to more complex tools that e.g. decide the structural model [4], the IIV and IOV structure, or the residual error model. Pharmpy also has a tool for Automatic Model Development (AMD) which aims to automatically build a PK model from a given dataset [5], with current development aiming for a bigger scope of models. All of these tools are implemented for the Pharmpy model object, meaning that when new model languages are added as plugins the tools can be used directly.
Pharmpy uses the main packages from the Python scientific stack with sympy [6] for symbolic manipulations, and pandas [7], numpy [8] and scipy [9] for numeric calculations, and dask [10] for executing tools. Pharmpy and pharmr are freely available at:
https://pharmpy.github.io
https://github.com/pharmpy/pharmr
Contribution to the Pharmpy-package is welcome!
Acknowledgement: This work was supported by F. Hoffmann-La Roche Ltd., Basel, Switzerland and Bayer AG. A special thanks to Dr. Emilie Schindler, Dr. Sylvie Retout, Dr. Valerie Cosson, Dr. João Abrantes, and Zrinka Duvnjak for conducting testing and providing feedback.
References:
[1] Bauer RJ. NONMEM Tutorial Part I: Description of Commands and Options, With Simple Examples of Population Analysis. CPT: Pharmacometrics & Systems Pharmacology. 2019;8(8):525–37.
[2] Fidler M, Xiong Y, Schoemaker R, Wilkins J, Trame M, Hooijmaijers R, Post T, Wang W. (2021) nlmixr: Nonlinear Mixed Effects Models in Population Pharmacokinetics and Pharmacodynamics. R package version 2.0.6. https://CRAN.R-project.org/package=nlmixr.
[3] R Core Team (2020). R: A language and environment for statistical computing. R Foundation for Statistical Computing, Vienna, Austria. URL https://www.R-project.org/.
[4] Alzahra Hamdan, Xiaomei Chen, Stella Belin, Rikard Nordgren, Simon Buatois, João A. Abrantes, Andrew C. Hooker and Mats O. Karlsson, Automatic Development of Pharmacokinetic Structural Models – Pharmpy Model Search Tool, PAGE 2022
[5] Xiaomei Chen, Alzahra Hamdan, Shijun Wang, Tianwu Yang, Rikard Nordgren, Stella Belin, Zhe Huang, Simon J. Carter, Simon Buatois, João A. Abrantes, Andrew C. Hooker, Mats O. Karlsson, Development of a tool for fully automatic model development (AMD), PAGE 2022
[6] Meurer A, Smith CP, Paprocki M, Certík O, Kirpichev SB, Rocklin M, Kumar A, Ivanov S, Moore JK, Singh S, Rathnayake T, Vig S, Granger BE, Muller RP, Bonazzi F, Gupta H, Vats S, Johansson F, Pedregosa F, Curry MJ, Terrel AR, Roucka Š, Saboo A, Fernando I, Kulal S, Cimrman R, Scopatz A. (2017) SymPy: symbolic computing in Python. PeerJ Computer Science 3:e103 https://doi.org/10.7717/peerj-cs.103
[7] Jeff Reback, Wes McKinney, jbrockmendel, Joris Van den Bossche, Tom Augspurger, Phillip Cloud, gfyoung, Sinhrks, Adam Klein, Matthew Roeschke, Simon Hawkins, Jeff Tratner, Chang She, William Ayd, Terji Petersen, Marc Garcia, Jeremy Schendel, Andy Hayden, Mortada Mehyar et al. (2020). pandas-dev/pandas: Pandas 1.0.3 (v1.0.3). Zenodo. https://doi.org/10.5281/zenodo.3715232
[8] Harris, C.R., Millman, K.J., van der Walt, S.J. et al. Array programming with NumPy. Nature 585, 357–362 (2020). DOI: 0.1038/s41586-020-2649-2 . (Publisher link).
[9] Pauli Virtanen, Ralf Gommers, Travis E. Oliphant, Matt Haberland, Tyler Reddy, David Cournapeau, Evgeni Burovski, Pearu Peterson, Warren Weckesser, Jonathan Bright, Stéfan J. van der Walt, Matthew Brett, Joshua Wilson, K. Jarrod Millman, Nikolay Mayorov, Andrew R. J. Nelson, Eric Jones, Robert Kern, Eric Larson, CJ Carey, Ilhan Polat, Yu Feng, Eric W. Moore, Jake VanderPlas, Denis Laxalde, Josef Perktold, Robert Cimrman, Ian Henriksen, E.A. Quintero, Charles R Harris, Anne M. Archibald, Antônio H. Ribeiro, Fabian Pedregosa, Paul van Mulbregt, and SciPy 1.0 Contributors. (2020) SciPy 1.0: Fundamental Algorithms for Scientific Computing in Python. Nature Methods, 17(3), 261-272.
[10] Dask Development Team (2016). Dask: Library for dynamic task scheduling, https://dask.org