patternpythonMinor
Modified Taylor diagrams
Viewed 0 times
modifieddiagramstaylor
Problem
There is a type of diagram summarizing how well predictions from numerical models fit expectations; one obvious use case is comparing machine-learning regression models. Modified Taylor diagrams are described in this paper.
I'd like to know if my implementation follows best practices for using matplotlib, and how I might incorporate better testing.
```
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.projections import PolarAxes
import mpl_toolkits.axisartist.floating_axes as fa
import mpl_toolkits.axisartist.grid_finder as gf
class TaylorDiagramPoint(object):
"""
A single point on a Modified Taylor Diagram.
How well do the values predicted match the values expected
* do the means match
* do the standard deviations match
* are they correlated
* what is the normalized error standard deviation
* what is the bias?
Notation:
* s_ := sample standard deviation
* m_ := sample mean
* nesd := normalized error standard deviation;
> nesd2 = s_predicted2 + s_expected**2 -
2 s_predicted s_expected * corcoeff
"""
def __init__(self, expected, predicted, pred_name, point_id):
self.pred = predicted
self.expd = expected
self.s_pred = np.std(self.pred)
self.s_expd = np.std(self.expd)
self.s_normd = self.s_pred / self.s_expd
self.bias = (np.mean(self.pred) - np.mean(self.expd)) / self.s_expd
self.corrcoef = np.corrcoef(self.pred, self.expd)[0, 1]
self.corrcoef = min([self.corrcoef, 1.0])
self.nesd = np.sqrt(self.s_pred2 + self.s_expd2 -
2 self.s_pred self.s_expd * self.corrcoef)
self.name = pred_name
self.point_id = point_id
class ModTaylorDiagram(object):
"""
Given predictions and expected numerical data
plot the standard deviation of the differences and correlation between
expected and predicted in a single-quadrant polar plot, with
r=stddev
I'd like to know if my implementation follows best practices for using matplotlib, and how I might incorporate better testing.
```
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.projections import PolarAxes
import mpl_toolkits.axisartist.floating_axes as fa
import mpl_toolkits.axisartist.grid_finder as gf
class TaylorDiagramPoint(object):
"""
A single point on a Modified Taylor Diagram.
How well do the values predicted match the values expected
* do the means match
* do the standard deviations match
* are they correlated
* what is the normalized error standard deviation
* what is the bias?
Notation:
* s_ := sample standard deviation
* m_ := sample mean
* nesd := normalized error standard deviation;
> nesd2 = s_predicted2 + s_expected**2 -
2 s_predicted s_expected * corcoeff
"""
def __init__(self, expected, predicted, pred_name, point_id):
self.pred = predicted
self.expd = expected
self.s_pred = np.std(self.pred)
self.s_expd = np.std(self.expd)
self.s_normd = self.s_pred / self.s_expd
self.bias = (np.mean(self.pred) - np.mean(self.expd)) / self.s_expd
self.corrcoef = np.corrcoef(self.pred, self.expd)[0, 1]
self.corrcoef = min([self.corrcoef, 1.0])
self.nesd = np.sqrt(self.s_pred2 + self.s_expd2 -
2 self.s_pred self.s_expd * self.corrcoef)
self.name = pred_name
self.point_id = point_id
class ModTaylorDiagram(object):
"""
Given predictions and expected numerical data
plot the standard deviation of the differences and correlation between
expected and predicted in a single-quadrant polar plot, with
r=stddev
Solution
Let's start with the obvious remarks about whitespace:
Whitespace is important in Python. You got trailing whitespaces all over the place and you use an indentation of 2 spaces where 4 is prescribed by the official PEP8 Style Guide. When talking about sticking to best practices in Python, starting with PEP8 is a good idea. There's a lot more violations going on, a couple of them can be checked using the off-line tool and/or the on-line tool.
I like how you split your functions. It's straightforward and maintainable, except the large amount of magic numbers. I have a personal dislike for numerals in function names and variables (for example:
Basically, you got the usage of
Whitespace is important in Python. You got trailing whitespaces all over the place and you use an indentation of 2 spaces where 4 is prescribed by the official PEP8 Style Guide. When talking about sticking to best practices in Python, starting with PEP8 is a good idea. There's a lot more violations going on, a couple of them can be checked using the off-line tool and/or the on-line tool.
I like how you split your functions. It's straightforward and maintainable, except the large amount of magic numbers. I have a personal dislike for numerals in function names and variables (for example:
grid_locator1) and constructs like the following make me think you got naming problems:grid_locator1=grid_loc1,
tick_formatter1=tick_fmttr1Basically, you got the usage of
matplotlib down quite nicely. The rest could use some work.Code Snippets
grid_locator1=grid_loc1,
tick_formatter1=tick_fmttr1Context
StackExchange Code Review Q#82919, answer score: 5
Revisions (0)
No revisions yet.