.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/plot_02_parkinson_example.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_plot_02_parkinson_example.py: Parkinson's Disease Progression Modeling with Leaspy ===================================================== This example demonstrates how to use Leaspy to model the progression of Parkinson's disease using synthetic data. .. GENERATED FROM PYTHON SOURCE LINES 9-11 The following imports bring in the required modules and load the synthetic dataset from Leaspy. The dataset contains repeated measurements for multiple subjects over time. .. GENERATED FROM PYTHON SOURCE LINES 11-16 .. code-block:: Python from leaspy.datasets import load_dataset from leaspy.io.data import Data df = load_dataset("parkinson") .. GENERATED FROM PYTHON SOURCE LINES 17-18 The first few rows of the dataset provide an overview of its structure. .. GENERATED FROM PYTHON SOURCE LINES 18-20 .. code-block:: Python df.head() .. raw:: html
MDS1_total MDS2_total MDS3_off_total SCOPA_total MOCA_total REM_total PUTAMEN_R PUTAMEN_L CAUDATE_R CAUDATE_L
ID TIME
GS-001 71.354607 0.112301 0.122472 0.171078 0.160001 0.275257 0.492485 0.780210 0.676774 0.622611 0.494641
71.554604 0.140880 0.109504 0.118693 0.135852 0.380934 0.577203 0.751444 0.719796 0.618434 0.530978
72.054604 0.225499 0.270502 0.061310 0.211134 0.351172 0.835828 0.823315 0.691504 0.717099 0.576978
73.054604 0.132519 0.253548 0.258786 0.245323 0.377842 0.566496 0.813593 0.787914 0.770048 0.709486
73.554604 0.278923 0.321920 0.143350 0.223102 0.292768 0.741811 0.888792 0.852720 0.797368 0.715465


.. GENERATED FROM PYTHON SOURCE LINES 21-22 The total number of unique subjects present in the dataset is shown below. .. GENERATED FROM PYTHON SOURCE LINES 22-25 .. code-block:: Python n_subjects = df.index.get_level_values("ID").unique().shape[0] print(f"{n_subjects} subjects in the dataset.") .. rst-class:: sphx-glr-script-out .. code-block:: none 200 subjects in the dataset. .. GENERATED FROM PYTHON SOURCE LINES 26-28 The dataset is separated into a training set and a test set. The first portion of the data is used for training and the remaining portion for testing. .. GENERATED FROM PYTHON SOURCE LINES 28-31 .. code-block:: Python df_train = df.loc[:"GS-160"][["MDS1_total", "MDS2_total", "MDS3_off_total"]] df_test = df.loc["GS-161":][["MDS1_total", "MDS2_total", "MDS3_off_total"]] .. GENERATED FROM PYTHON SOURCE LINES 32-33 The pandas DataFrames are converted to Leaspy `Data` objects for further modeling. .. GENERATED FROM PYTHON SOURCE LINES 33-37 .. code-block:: Python data_train = Data.from_dataframe(df_train) data_test = Data.from_dataframe(df_test) .. GENERATED FROM PYTHON SOURCE LINES 38-40 The logistic model is imported and initialized. A two-dimensional source space is chosen to represent disease progression trajectories. .. GENERATED FROM PYTHON SOURCE LINES 40-44 .. code-block:: Python from leaspy.models import LogisticModel model = LogisticModel(name="test-model", source_dimension=2) .. GENERATED FROM PYTHON SOURCE LINES 45-46 Visualization utilities from Leaspy and Matplotlib are imported. .. GENERATED FROM PYTHON SOURCE LINES 46-52 .. code-block:: Python import matplotlib.pyplot as plt from leaspy.io.logs.visualization.plotting import Plotting leaspy_plotting = Plotting(model) .. rst-class:: sphx-glr-script-out .. code-block:: none /home/docs/checkouts/readthedocs.org/user_builds/leaspy/checkouts/476/src/leaspy/io/logs/visualization/plotting.py:42: FutureWarning: Plotting will soon be removed from Leaspy, please use Plotter instead. warnings.warn( .. GENERATED FROM PYTHON SOURCE LINES 53-54 Data that will be used to fit the model can be illustrated as follows: .. GENERATED FROM PYTHON SOURCE LINES 54-60 .. code-block:: Python ax = leaspy_plotting.patient_observations(data_train, alpha=0.7, figsize=(14, 6)) ax.set_ylim(0, 0.8) # The y-axis is adjusted for better visibility. plt.show() .. image-sg:: /auto_examples/images/sphx_glr_plot_02_parkinson_example_001.png :alt: Observations :srcset: /auto_examples/images/sphx_glr_plot_02_parkinson_example_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 61-63 The model is fitted to the training data using the MCMC-SAEM algorithm. A fixed seed is used for reproducibility and 100 iterations are performed. .. GENERATED FROM PYTHON SOURCE LINES 63-72 .. code-block:: Python model.fit( data_train, "mcmc_saem", seed=0, n_iter=100, progress_bar=False, ) .. rst-class:: sphx-glr-script-out .. code-block:: none ==> Setting seed to 0 Fit with `AlgorithmName.FIT_MCMC_SAEM` took: 1s .. GENERATED FROM PYTHON SOURCE LINES 73-75 The average trajectory estimated by the model is displayed. This figure shows the mean disease progression curves for all features. .. GENERATED FROM PYTHON SOURCE LINES 75-80 .. code-block:: Python ax = leaspy_plotting.average_trajectory( alpha=1, figsize=(14, 6), n_std_left=2, n_std_right=8 ) plt.show() .. image-sg:: /auto_examples/images/sphx_glr_plot_02_parkinson_example_002.png :alt: Average trajectories :srcset: /auto_examples/images/sphx_glr_plot_02_parkinson_example_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 81-82 Individual parameters are obtained for the test data using the personalization step. .. GENERATED FROM PYTHON SOURCE LINES 82-84 .. code-block:: Python ip = model.personalize(data_test, "scipy_minimize", seed=0, progress_bar=False) .. rst-class:: sphx-glr-script-out .. code-block:: none ==> Setting seed to 0 /home/docs/checkouts/readthedocs.org/user_builds/leaspy/checkouts/476/src/leaspy/algo/personalize/scipy_minimize.py:632: UserWarning: In `scipy_minimize` you requested `use_jacobian=True` but it is not implemented in your model test-model. Falling back to `use_jacobian=False`... warnings.warn( Personalize with `AlgorithmName.PERSONALIZE_SCIPY_MINIMIZE` took: 7s .. GENERATED FROM PYTHON SOURCE LINES 85-86 The test data with individually re-parametrized ages is plotted below. .. GENERATED FROM PYTHON SOURCE LINES 86-91 .. code-block:: Python ax = leaspy_plotting.patient_observations_reparametrized( data_test, ip, alpha=0.7, linestyle="-", figsize=(14, 6) ) plt.show() .. image-sg:: /auto_examples/images/sphx_glr_plot_02_parkinson_example_003.png :alt: Observations :srcset: /auto_examples/images/sphx_glr_plot_02_parkinson_example_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 92-93 The test data with the true ages (without re-parametrization) is plotted below. .. GENERATED FROM PYTHON SOURCE LINES 93-101 .. code-block:: Python ax = leaspy_plotting.patient_observations( data_test, alpha=0.7, linestyle="-", figsize=(14, 6), ) plt.show() .. image-sg:: /auto_examples/images/sphx_glr_plot_02_parkinson_example_004.png :alt: Observations :srcset: /auto_examples/images/sphx_glr_plot_02_parkinson_example_004.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 102-103 Observations for a specific subject are extracted to demonstrate reconstruction. .. GENERATED FROM PYTHON SOURCE LINES 103-112 .. code-block:: Python import numpy as np observations = df_test.loc["GS-187"] print(f"Seen ages: {observations.index.values}") print("Individual Parameters : ", ip["GS-187"]) timepoints = np.linspace(60, 100, 100) reconstruction = model.estimate({"GS-187": timepoints}, ip) .. rst-class:: sphx-glr-script-out .. code-block:: none Seen ages: [61.34811783 62.34811783 63.84811783 64.34812164 67.84812164 68.34812164 69.34812164 69.84812164 70.84812164 71.34812164 71.84812164 72.34812164 72.84812164 73.34812164] Individual Parameters : {'sources': [1.3120700120925903, 0.7220780253410339], 'tau': [73.46656799316406], 'xi': [0.06860218942165375]} .. GENERATED FROM PYTHON SOURCE LINES 113-114 The reconstructed trajectory along with the actual observations for selected subjects is displayed. .. GENERATED FROM PYTHON SOURCE LINES 114-130 .. code-block:: Python ax = leaspy_plotting.patient_trajectories( data_test, ip, patients_idx=["GS-187"], labels=["MDS1", "MDS2", "MDS3 (off)"], alpha=1, linestyle="-", linewidth=2, markersize=8, obs_alpha=0.5, figsize=(16, 6), factor_past=0.5, factor_future=5, ) ax.set_xlim(45, 120) plt.show() .. image-sg:: /auto_examples/images/sphx_glr_plot_02_parkinson_example_005.png :alt: Observations and individual trajectories :srcset: /auto_examples/images/sphx_glr_plot_02_parkinson_example_005.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 9.661 seconds) .. _sphx_glr_download_auto_examples_plot_02_parkinson_example.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_02_parkinson_example.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_02_parkinson_example.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_02_parkinson_example.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_