{"id":13684098,"url":"https://github.com/gcappon/py_replay_bg","last_synced_at":"2026-01-22T19:33:20.059Z","repository":{"id":169405570,"uuid":"645321411","full_name":"gcappon/py_replay_bg","owner":"gcappon","description":"ReplayBG is a digital twin-based methodology to assess new strategies for type 1 diabetes management.","archived":false,"fork":false,"pushed_at":"2025-11-27T08:13:06.000Z","size":11680,"stargazers_count":12,"open_issues_count":6,"forks_count":7,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-11-27T19:44:46.474Z","etag":null,"topics":["digital-twin","python","simulation","type-1-diabetes"],"latest_commit_sha":null,"homepage":"https://gcappon.github.io/py_replay_bg/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/gcappon.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"COPYING.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2023-05-25T11:53:57.000Z","updated_at":"2025-11-27T08:07:48.000Z","dependencies_parsed_at":"2023-11-27T14:43:15.175Z","dependency_job_id":"bc355ee8-8596-41e5-8154-01bc2831d5c2","html_url":"https://github.com/gcappon/py_replay_bg","commit_stats":null,"previous_names":["gcappon/py-replay-bg"],"tags_count":21,"template":false,"template_full_name":null,"purl":"pkg:github/gcappon/py_replay_bg","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gcappon%2Fpy_replay_bg","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gcappon%2Fpy_replay_bg/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gcappon%2Fpy_replay_bg/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gcappon%2Fpy_replay_bg/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gcappon","download_url":"https://codeload.github.com/gcappon/py_replay_bg/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gcappon%2Fpy_replay_bg/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28669191,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-22T17:07:18.858Z","status":"ssl_error","status_checked_at":"2026-01-22T17:05:02.040Z","response_time":144,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["digital-twin","python","simulation","type-1-diabetes"],"created_at":"2024-08-02T14:00:25.804Z","updated_at":"2026-01-22T19:33:20.038Z","avatar_url":"https://github.com/gcappon.png","language":"Python","funding_links":[],"categories":["Digital Twin-Based Simulators"],"sub_categories":[],"readme":"# ReplayBG\r\n\r\n\u003cimg src=\"https://i.postimg.cc/gJn8Sy0X/replay-bg-logo.png\" width=\"250\" height=\"250\"\u003e\r\n\r\n[![License: GPL v3](https://img.shields.io/badge/License-GPLv3-blue.svg)](https://github.com/gcappon/py_replay_bg/COPYING)\r\n[![GitHub commit](https://img.shields.io/github/last-commit/gcappon/py_replay_bg)](https://github.com/gcappon/py_replay_bg/commits/master)\r\n\r\nReplayBG is a digital twin-based methodology to develop and assess new strategies for type 1 diabetes management.\r\n\r\n# Reference \r\n\r\n[G. Cappon, M. Vettoretti, G. Sparacino, S. Del Favero, A. Facchinetti, \"ReplayBG: a digital twin-based methodology to identify a personalized model from type 1 diabetes data and simulate glucose concentrations to assess alternative therapies\", IEEE Transactions on Biomedical Engineering, 2023, DOI: 10.1109/TBME.2023.3286856.](https://ieeexplore.ieee.org/document/10164140)\r\n\r\n# Get started\r\n\r\n## Installation\r\n\r\n**ReplayBG** can be installed via pypi by simply \r\n\r\n```python\r\npip install py-replay-bg\r\n```\r\n\r\n### Requirements\r\n\r\n* Python \u003e= 3.11\r\n* List of Python packages in `requirements.txt`\r\n\r\n## Preparation: imports, setup, and data loading \r\n\r\nFirst of all import the core modules:\r\n```python\r\nimport os\r\nimport numpy as np\r\nimport pandas as pd\r\n\r\nfrom multiprocessing import freeze_support\r\n```\r\n\r\nHere, `os` will be used to manage the filesystem, `numpy` and `pandas` to manipulate and manage the data to be used, and\r\n`multiprocessing.freeze_support` to enable multiprocessing functionalities and run the twinning procedure in a faster,\r\nparallelized way. \r\n\r\nThen, we will import the necessary ReplayBG modules:\r\n```python\r\nfrom py_replay_bg.py_replay_bg import ReplayBG\r\nfrom py_replay_bg.visualizer import Visualizer\r\nfrom py_replay_bg.analyzer import Analyzer\r\n```\r\n\r\nHere, `ReplayBG` is the core ReplayBG object (more information in the [The ReplayBG Object](https://gcappon.github.io/py_replay_bg/documentation/replaybg_object.html) page),\r\nwhile `Analyzer` and `Visualizer` are utility objects that will be used to\r\nrespectively analyze and visualize the results that we will produce with ReplayBG\r\n(more information in the ([Visualizing Replay Results](https://gcappon.github.io/py_replay_bg/documentation/visualizing_replay_results.html) and\r\n [Analyzing Replay Results](https://gcappon.github.io/py_replay_bg/documentation/analyzing_replay_results.html) pages).\r\n\r\nNext steps consist of setting up some variables that will be used by ReplayBG environment. \r\nFirst of all, we will run the twinning procedure in a parallelized way so let's start with:\r\n```python\r\nif __name__ == '__main__':\r\n    freeze_support()\r\n```\r\n\r\nThen, we will set the verbosity of ReplayBG:\r\n```python\r\n    verbose = True\r\n    plot_mode = False\r\n```\r\n \r\nThen, we need to decide what blueprint to use for twinning the data at hand. \r\n```python\r\n    blueprint = 'multi-meal'\r\n    save_folder = os.path.join(os.path.abspath(''),'..','..','..')\r\n    parallelize = True\r\n```\r\n\r\nFor more information on how to choose a blueprint, please refer to the [Choosing Blueprint](https://gcappon.github.io/py_replay_bg/documentation/choosing_blueprint.html) page.\r\n\r\nNow, let's load some data to play with. In this example, we will use the data stored in `example/data/data_day_1.csv` \r\nwhich contains a day of data of a patient with T1D:\r\n\r\n```python\r\ndata = pd.read_csv(os.path.join(os.path.abspath(''), '..', 'data', 'data_day_1.csv'))\r\ndata.t = pd.to_datetime(data['t'])\r\n```\r\n\r\n::: warning \r\nBe careful, data in PyReplayBG must be provided in a `.csv.` file that must follow some strict requirements. For more \r\ninformation see the [Data Requirements](https://gcappon.github.io/py_replay_bg/documentation/data_requirements.html) page.\r\n:::\r\n\r\nLet's also load the patient information (i.e., body weight and basal insulin `u2ss`) stored in the `example/data/patient_info.csv` file.\r\n\r\n```python\r\npatient_info = pd.read_csv(os.path.join(os.path.abspath(''), '..', 'data', 'patient_info.csv'))\r\np = np.where(patient_info['patient'] == 1)[0][0]\r\n# Set bw and u2ss\r\nbw = float(patient_info.bw.values[p])\r\nu2ss = float(patient_info.u2ss.values[p])\r\n```\r\n\r\nFinally, instantiate a `ReplayBG` object:\r\n\r\n```python\r\nrbg = ReplayBG(blueprint=blueprint, save_folder=save_folder,\r\n               yts=5, exercise=False,\r\n               seed=1,\r\n               verbose=verbose, plot_mode=plot_mode)\r\n\r\n```\r\n\r\n## Step 1: Creation of the digital twin\r\n\r\nTo create the digital twin, i.e., run the twinning procedure, using the MCMC method, use the `rbg.twin()` method:\r\n\r\n```python\r\nrbg.twin(data=data, bw=bw, save_name='data_day_1',\r\n         twinning_method='mcmc',\r\n         parallelize=parallelize,\r\n         n_steps=5000,\r\n         u2ss=u2ss)\r\n```\r\n\r\nFor more information on the twinning procedure see the [Twinning Procedure](https://gcappon.github.io/py_replay_bg/documentation/twinning_procedure.html) page.\r\n\r\n\r\n## Step 2: Run replay simulations\r\n\r\nNow that we have the digital twin created, it's time to replay using the `rbg.replay()` method. For more details \r\nsee the [Replaying](https://gcappon.github.io/py_replay_bg/documentation/replaying.html) page.\r\n\r\nThe possibilities are several, but for now let's just see what happens if we run a replay using the same input data used for twinning:\r\n\r\n```python\r\nreplay_results = rbg.replay(data=data, bw=bw, save_name='data_day_1',\r\n                            twinning_method='mcmc',\r\n                            save_workspace=True,\r\n                            save_suffix='_step_2a')\r\n```\r\n\r\nIt is possible to visualize the results of the simulation using:\r\n\r\n```python\r\nVisualizer.plot_replay_results(replay_results, data=data)\r\n```\r\n\r\nand analyzing the results using: \r\n\r\n```python\r\nanalysis = Analyzer.analyze_replay_results(replay_results, data=data)\r\nprint('Fit MARD: %.2f %%' % analysis['median']['twin']['mard'])\r\nprint('Mean glucose: %.2f mg/dl' % analysis['median']['glucose']['variability']['mean_glucose'])\r\n```\r\n\r\nAs a second example, we can simulate what happens with different inputs, for example when we reduce insulin by 30%.\r\nTo do that run:\r\n\r\n```python\r\ndata.bolus = data.bolus * .7\r\nreplay_results = rbg.replay(data=data, bw=bw, save_name=save_name,\r\n                            twinning_method='mcmc',\r\n                            save_workspace=True,\r\n                            save_suffix='_step_2b')\r\n\r\n# Visualize results\r\nVisualizer.plot_replay_results(replay_results)\r\n# Analyze results\r\nanalysis = Analyzer.analyze_replay_results(replay_results)\r\n\r\n# Print, for example, the average glucose\r\nprint('Mean glucose: %.2f mg/dl' % analysis['median']['glucose']['variability']['mean_glucose'])\r\n```\r\n\r\nA `.py` file with the full code of the get started example can be found in `example/code/get_started.py`.\r\n\r\n# Documentation\r\n\r\nFull documentation at [https://gcappon.github.io/py_replay_bg/](https://gcappon.github.io/py_replay_bg/).\r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgcappon%2Fpy_replay_bg","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgcappon%2Fpy_replay_bg","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgcappon%2Fpy_replay_bg/lists"}