-
Notifications
You must be signed in to change notification settings - Fork 19
744 Create gif from simulation results #760
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
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
9352af0
fix plotmap + log scale colormap
ee501f4
plot gif example
ffa2ab3
Rearranging gif creation and add time extraction
9525d17
Use temporary files
9b41ab4
Test
eecc68a
fix naming. TODO: add dataset
HenrZu 97d4915
Merge branch 'main' of https://github.com/DLR-SC/memilio into 744-cre…
HenrZu c03bd92
adjust init, function to own file
HenrZu 3877ace
some tests, use in 2020sim, still problems with tmp dir
HenrZu 07c431e
some final fixes
HenrZu b38be51
save memory
HenrZu a9f1503
tests now working
HenrZu 7b72dbf
Apply suggestions from code review
HenrZu 9177377
review suggestions
HenrZu 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,155 @@ | ||
| ############################################################################# | ||
| # Copyright (C) 2020-2024 MEmilio | ||
| # | ||
| # Authors: Henrik Zunker, Maximilian Betz | ||
| # | ||
| # 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. | ||
| ############################################################################# | ||
|
|
||
| import datetime as dt | ||
| import os.path | ||
| import imageio | ||
| import tempfile | ||
|
|
||
| import numpy as np | ||
| import pandas as pd | ||
| import matplotlib.pyplot as plt | ||
|
|
||
| import memilio.epidata.getPopulationData as gpd | ||
| import memilio.plot.plotMap as pm | ||
| from memilio.epidata import geoModificationGermany as geoger | ||
| import memilio.epidata.progress_indicator as progind | ||
| import warnings | ||
| warnings.simplefilter(action='ignore', category=FutureWarning) | ||
|
|
||
|
|
||
| def create_plot_map(day, filename, files_input, output_path, compartments, file_format='h5', relative=False, age_groups={0: '0-4', 1: '5-14', 2: '15-34', 3: '35-59', 4: '60-79', 5: '80+'}): | ||
| """! Plots region-specific information for a single day of the simulation. | ||
| @param[in] day Day of the simulation. | ||
| @param[in] filename Name of the file to be created. | ||
| @param[in] files_input Dictionary of input files. | ||
| @param[in] output_path Output path for the figure. | ||
| @param[in] compartments List of compartments to be plotted. | ||
| @param[in] file_format Format of the file to be created. Either 'h5' or 'json'. | ||
| @param[in] relative Defines if data should be scaled relative to population. | ||
| @param[in] age_groups Dictionary of age groups to be considered. | ||
| """ | ||
|
|
||
| if len(age_groups) == 6: | ||
| filter_age = None | ||
| else: | ||
| if file_format == 'json': | ||
| filter_age = [val for val in age_groups.values()] | ||
| else: | ||
| filter_age = ['Group' + str(key) for key in age_groups.keys()] | ||
|
|
||
| # In file_input there can be two different files. When we enter two files, | ||
| # both files are plotted side by side in the same figure. | ||
| file_index = 0 | ||
| for file in files_input.values(): | ||
|
|
||
| df = pm.extract_data( | ||
| file, region_spec=None, column=None, date=day, | ||
| filters={'Group': filter_age, 'InfectionState': compartments}, | ||
| file_format=file_format) | ||
|
|
||
| if relative: | ||
|
|
||
| try: | ||
| population = pd.read_json( | ||
| 'data/pydata/Germany/county_current_population.json') | ||
| # pandas>1.5 raise FileNotFoundError instead of ValueError | ||
| except (ValueError, FileNotFoundError): | ||
| print( | ||
| "Population data was not found. Downloading it from the internet.") | ||
| population = gpd.get_population_data( | ||
| read_data=False, file_format=file_format, | ||
| out_folder='data/pydata/Germany/', no_raw=True, merge_eisenach=True) | ||
|
|
||
| # For fitting of different age groups we need format ">X". | ||
| age_group_values = list(age_groups.values()) | ||
| age_group_values[-1] = age_group_values[-1].replace( | ||
| '80+', '>79') | ||
| # scale data | ||
| df = pm.scale_dataframe_relative( | ||
| df, age_group_values, population) | ||
|
|
||
| if file_index == 0: | ||
| dfs_all = pd.DataFrame(df.iloc[:, 0]) | ||
|
|
||
| dfs_all[df.columns[-1] + ' ' + str(file_index)] = df[df.columns[-1]] | ||
| file_index += 1 | ||
|
|
||
| dfs_all = dfs_all.apply(pd.to_numeric, errors='coerce') | ||
|
|
||
| dfs_all_sorted = dfs_all.sort_values(by='Region') | ||
| dfs_all_sorted = dfs_all_sorted.reset_index(drop=True) | ||
|
|
||
| min_val = dfs_all_sorted[dfs_all_sorted.columns[1:]].min().min() | ||
| max_val = dfs_all_sorted[dfs_all_sorted.columns[1:]].max().max() | ||
|
|
||
| pm.plot_map( | ||
| dfs_all_sorted, scale_colors=np.array([min_val, max_val]), | ||
| legend=['', ''], | ||
| title='Synthetic data (relative) day ' + f'{day:2d}', plot_colorbar=True, | ||
| output_path=output_path, | ||
| fig_name=filename, dpi=300, | ||
| outercolor=[205 / 255, 238 / 255, 251 / 255]) | ||
|
|
||
|
|
||
| def create_gif_map_plot(input_data, output_dir, compartments, filename="simulation", relative=True, age_groups={0: '0-4', 1: '5-14', 2: '15-34', | ||
| 3: '35-59', 4: '60-79', 5: '80+'}): | ||
| """! Creates a gif of the simulation results by calling create_plot_map for each day of the simulation and then | ||
| storing the single plots in a temporary directory. Currently only works for the results created by the parameter study. | ||
|
|
||
| @param[in] input_data Path to the input data. The Path should contain a file called 'Results' which contains | ||
HenrZu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| the simulation results. This is the default output folder of the parameter study. | ||
| @param[in] output_dir Path where the gif should be stored. | ||
| @param[in] filename Name of the temporary file. | ||
| @param[in] relative Defines if data should be scaled relative to population. | ||
| @param[in] age_groups Dictionary of age groups to be considered. | ||
| """ | ||
|
|
||
| files_input = {'Data set': input_data + '/Results'} | ||
| file_format = 'h5' | ||
|
|
||
| if len(age_groups) == 6: | ||
| filter_age = None | ||
| else: | ||
| filter_age = ['Group' + str(key) for key in age_groups.keys()] | ||
|
|
||
| num_days = pm.extract_time_steps( | ||
| files_input[list(files_input.keys())[0]], file_format=file_format) | ||
|
|
||
| # create gif | ||
| frames = [] | ||
| with progind.Percentage() as indicator: | ||
| with tempfile.TemporaryDirectory() as tmpdirname: | ||
| for day in range(0, num_days): | ||
| create_plot_map(day, filename, files_input, tmpdirname, | ||
| compartments, file_format, relative, age_groups) | ||
|
|
||
| image = imageio.v2.imread( | ||
| os.path.join(tmpdirname, filename + ".png")) | ||
| frames.append(image) | ||
|
|
||
| # Close the current figure to free up memory | ||
| plt.close('all') | ||
| indicator.set_progress((day+1)/num_days) | ||
|
|
||
| imageio.mimsave(os.path.join(output_dir, filename + '.gif'), | ||
| frames, # array of input frames | ||
| duration=0.2, # duration of each frame in seconds | ||
| loop=0) # optional: frames per second | ||
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.
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.