-
Notifications
You must be signed in to change notification settings - Fork 20
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
Develop minimization plots #85
Merged
Merged
Changes from 11 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
c497fb3
initial commit
kevindougherty-noaa 6342239
remove plot_features?
kevindougherty-noaa b9295bd
Merge branch 'develop' into feature/minimization_plots
kevindougherty-noaa f19f76a
updates to plot_features.py
kevindougherty-noaa 75552dd
Merge branch 'feature/LAMDA' of https://github.com/NOAA-EMC/PyGSI int…
kevindougherty-noaa 4f1780a
Merge branch 'feature/LAMDA' of https://github.com/NOAA-EMC/PyGSI int…
kevindougherty-noaa e2ac9fc
updating to merge feature/LAMDA
kevindougherty-noaa c4bb16a
minimization updates to include experiment and tm info
kevindougherty-noaa a84ff7e
pycodestyle
kevindougherty-noaa 98bd242
pycodestyle
kevindougherty-noaa 868f149
add experiment to yaml
kevindougherty-noaa 858b33c
experiment type to experiment name
kevindougherty-noaa 6402615
whoopsies
kevindougherty-noaa 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 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,138 @@ | ||
import numpy as np | ||
import pandas as pd | ||
from datetime import datetime | ||
import matplotlib.pyplot as plt | ||
from emcpy.plots.plots import LinePlot | ||
from emcpy.plots import CreatePlot | ||
|
||
__all__ = ['plot_minimization'] | ||
|
||
|
||
def _plot_cost_function(df, config, outdir): | ||
""" | ||
Use data from dataframe to plot the cost function. | ||
""" | ||
# Get cycle info | ||
cycle = df.index.get_level_values(0)[-1] | ||
cyclestr = datetime.strftime(cycle, '%Y%m%d%H') | ||
n_cycles = len(np.unique(df.index.get_level_values(0))) | ||
|
||
# Create approriate dataframes | ||
current_cycle_df = df.loc[cycle] | ||
avg_df = df.groupby(level=[1, 2]).mean() | ||
|
||
# Grab data | ||
j = current_cycle_df['J'].to_numpy() | ||
x = np.arange(len(j)) | ||
|
||
avg_j = avg_df['J'].to_numpy() | ||
avg_x = np.arange(len(avg_j)) | ||
|
||
# Create LinePlot objects | ||
cost_plot = LinePlot(x, j) | ||
cost_plot.linewidth = 2 | ||
cost_plot.label = 'Cost' | ||
|
||
avg_cost_plot = LinePlot(avg_x, avg_j) | ||
avg_cost_plot.color = 'tab:red' | ||
avg_cost_plot.linewidth = 2 | ||
avg_cost_plot.label = f'Last {n_cycles} cycles Average' | ||
|
||
# Create Plot | ||
myplot = CreatePlot() | ||
myplot.draw_data([cost_plot, avg_cost_plot]) | ||
myplot.set_yscale('log') | ||
myplot.add_grid() | ||
myplot.set_xlim(0, len(j)-1) | ||
myplot.add_xlabel('Iterations') | ||
myplot.add_ylabel('log (J)') | ||
myplot.add_legend(loc='upper right', | ||
fontsize='large') | ||
|
||
title = f"{config['experiment']} Cost - {config['tm']}" | ||
myplot.add_title(title, loc='left') | ||
myplot.add_title(cyclestr, loc='right', | ||
fontweight='semibold') | ||
|
||
fig = myplot.return_figure() | ||
|
||
savefile = (f"{cyclestr}_{config['experiment']}_" + | ||
f"tm0{config['tm']}_cost_function.png") | ||
plt.savefig(outdir + savefile, bbox_inches='tight', | ||
pad_inches=0.1) | ||
plt.close('all') | ||
|
||
|
||
def _plot_gnorm(df, config, outdir): | ||
""" | ||
Use date from dataframe to plot gnorm. | ||
""" | ||
# Get cycle info | ||
cycle = df.index.get_level_values(0)[-1] | ||
cyclestr = datetime.strftime(cycle, '%Y%m%d%H') | ||
n_cycles = len(np.unique(df.index.get_level_values(0))) | ||
|
||
# Create approriate dataframes | ||
current_cycle_df = df.loc[cycle] | ||
avg_df = df.groupby(level=[1, 2]).mean() | ||
|
||
# Grab data | ||
gJ = current_cycle_df['gJ'].to_numpy() | ||
gJ = np.log(gJ/gJ[0]) | ||
x = np.arange(len(gJ)) | ||
|
||
avg_gJ = avg_df['gJ'].to_numpy() | ||
avg_gJ = np.log(avg_gJ/avg_gJ[0]) | ||
avg_x = np.arange(len(avg_gJ)) | ||
|
||
# Create LinePlot objects | ||
gnorm = LinePlot(x, gJ) | ||
gnorm.linewidth = 2 | ||
gnorm.label = 'gnorm' | ||
|
||
avg_gnorm = LinePlot(avg_x, avg_gJ) | ||
avg_gnorm.color = 'tab:red' | ||
avg_gnorm.linewidth = 2 | ||
avg_gnorm.linestyle = '--' | ||
avg_gnorm.label = f'Last {n_cycles} cycles Average' | ||
|
||
# Create Plot | ||
myplot = CreatePlot() | ||
myplot.draw_data([gnorm, avg_gnorm]) | ||
# myplot.set_yscale('log') | ||
myplot.add_grid() | ||
myplot.set_xlim(0, len(gJ)-1) | ||
myplot.add_xlabel('Iterations') | ||
myplot.add_ylabel('log (gnorm)') | ||
myplot.add_legend(loc='upper right', | ||
fontsize='large') | ||
|
||
title = f"{config['experiment']} gnorm - {config['tm']}" | ||
myplot.add_title(title, loc='left') | ||
myplot.add_title(cyclestr, loc='right', | ||
fontweight='semibold') | ||
|
||
fig = myplot.return_figure() | ||
|
||
savefile = (f"{cyclestr}_{config['experiment']}_" + | ||
f"tm0{config['tm']}_gnorm.png") | ||
plt.savefig(outdir + savefile, bbox_inches='tight', | ||
pad_inches=0.1) | ||
plt.close('all') | ||
|
||
|
||
def plot_minimization(df, plotting_config, outdir): | ||
""" | ||
Plot minimization plots including gnorm and cost function | ||
and save them to outdir. | ||
|
||
Args: | ||
df : (pandas dataframe) dataframe with appropriate information | ||
from GSI Stat file | ||
plotting_config : (dict) dictionary with information about | ||
minimization period | ||
outdir : (str) path to output diagnostics | ||
""" | ||
|
||
_plot_gnorm(df, plotting_config, outdir) | ||
_plot_cost_function(df, plotting_config, outdir) |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only question is how to handle the
tm06
information for this plot?