-
Notifications
You must be signed in to change notification settings - Fork 25
1577 Add generic Time Series Plotting function #1589
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
kilianvolmer
wants to merge
5
commits into
main
Choose a base branch
from
1577-Create-a-standard-plot-for-timeseries-objects
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0302ba9
NEW: Code and examples and docs for the timeSeries plotting function
kilianvolmer 3dd7301
FIX docstrings
kilianvolmer 5ddb32a
Add tests for plot TimeSeries
kilianvolmer 6f72f72
FIX: Use colors suggested by style guide
kilianvolmer dfd0ff4
Apply suggestions from code review
kilianvolmer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| ############################################################################# | ||
| # Copyright (C) 2020-2026 MEmilio | ||
| # | ||
| # Authors: Kilian Volmer | ||
| # | ||
| # Contact: Martin J. Kuehn <Martin.Kuehn@DLR.de> | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| ############################################################################# | ||
| """ | ||
| Example demonstrating the standard TimeSeries plot of the MEmilio plot | ||
| package on the result of an ODE SEIR simulation with two age groups. | ||
| """ | ||
| import argparse | ||
| import os | ||
| from datetime import date | ||
|
|
||
| import matplotlib.pyplot as plt | ||
| import numpy as np | ||
|
|
||
| from memilio.plot.plotTimeSeries import (plot_time_series, | ||
| time_series_to_dataframe) | ||
| from memilio.simulation import AgeGroup, Damping | ||
| from memilio.simulation.oseir import InfectionState, Model, simulate | ||
|
|
||
| AGE_GROUPS = ['0-19', '20+'] | ||
| START_DATE = date(2020, 3, 1) | ||
|
|
||
|
|
||
| def run_ode_seir_simulation(days=100, dt=0.1): | ||
| """ Runs the ODE SEIR model with two age groups. | ||
|
|
||
| :param days: Number of days to simulate. (Default value = 100) | ||
| :param dt: Initial time step. (Default value = 0.1) | ||
| :returns: Simulation result as TimeSeries. | ||
| """ | ||
| group_populations = [15000, 68000] | ||
| num_groups = len(group_populations) | ||
| model = Model(num_groups) | ||
|
|
||
| for i, population in enumerate(group_populations): | ||
| group = AgeGroup(i) | ||
| model.parameters.TimeExposed[group] = 5.2 | ||
| model.parameters.TimeInfected[group] = 6. | ||
| model.parameters.TransmissionProbabilityOnContact[group] = 1. * (i+1) | ||
| model.populations[group, InfectionState.Exposed] = 100 | ||
| model.populations[group, InfectionState.Infected] = 50 | ||
| model.populations[group, InfectionState.Recovered] = 10 | ||
| model.populations.set_difference_from_group_total_AgeGroup( | ||
| (group, InfectionState.Susceptible), population) | ||
|
|
||
| model.parameters.ContactPatterns.cont_freq_mat[0].baseline = np.ones( | ||
| (num_groups, num_groups)) | ||
| model.parameters.ContactPatterns.cont_freq_mat[0].minimum = np.zeros( | ||
| (num_groups, num_groups)) | ||
| model.parameters.ContactPatterns.cont_freq_mat.add_damping(Damping( | ||
| coeffs=np.ones((num_groups, num_groups)) * 0.9, t=30.0, level=0, | ||
| type=0)) | ||
|
|
||
| model.check_constraints() | ||
| return simulate(0, days, dt, model) | ||
|
|
||
|
|
||
| def plot_results(result, output_path='.', show_plot=False): | ||
| """ Creates the standard plots of the simulation result. | ||
|
|
||
| :param result: TimeSeries returned by the simulation. | ||
| :param output_path: Directory the figures are written to. | ||
| (Default value = '.') | ||
| :param show_plot: Whether to show the figures interactively. | ||
| (Default value = False) | ||
| """ | ||
| # All compartments, summed over the age groups. The compartment names | ||
| # are taken from the InfectionState enum of the model. | ||
| ax = plot_time_series( | ||
| result, labels=InfectionState.values(), groups=AGE_GROUPS, | ||
| title='ODE SEIR simulation') | ||
| ax.figure.savefig( | ||
| os.path.join(output_path, 'seir_compartments.png'), dpi=150) | ||
|
|
||
| # Two panels in one figure: a selection of compartments per age group | ||
| # on a date axis, and a selection on a logarithmic axis. | ||
| fig, axes = plt.subplots(1, 2, figsize=(12, 4.5), layout='constrained') | ||
| plot_time_series( | ||
| result, labels=InfectionState.values(), groups=AGE_GROUPS, | ||
| sum_groups=False, select=['Exposed', 'Infected'], | ||
| start_date=START_DATE, ax=axes[0], | ||
| title='Exposed and infected per age group') | ||
| plot_time_series( | ||
| result, labels=InfectionState.values(), groups=AGE_GROUPS, | ||
| select=['Exposed', 'Infected', 'Recovered'], log_scale=True, | ||
| start_date=START_DATE, ax=axes[1], title='Logarithmic scale') | ||
| fig.savefig(os.path.join(output_path, 'seir_selection.pdf')) | ||
|
|
||
| # The same data as a tidy data frame, e.g. for other plotting libraries. | ||
| df = time_series_to_dataframe( | ||
| result, labels=InfectionState.values(), groups=AGE_GROUPS, | ||
| start_date=START_DATE) | ||
| print(df.head()) | ||
| print(df.pivot_table(index='Date', columns='Compartments', values='Values', | ||
| aggfunc='sum', observed=False).tail()) | ||
|
|
||
| if show_plot: | ||
| plt.show() | ||
| plt.close('all') | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| arg_parser = argparse.ArgumentParser( | ||
| 'plotSimulationResults', | ||
| description='Plots the result of an ODE SEIR simulation with the ' | ||
| 'standard TimeSeries plot of the MEmilio plot package.') | ||
| arg_parser.add_argument('-p', '--show_plot', action='store_true', | ||
| help='Show the figures interactively.') | ||
| arg_parser.add_argument('-o', '--output_path', default='.', | ||
| help='Directory the figures are written to.') | ||
| args = arg_parser.parse_args() | ||
| plot_results(run_ode_seir_simulation(), args.output_path, args.show_plot) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.