You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@climate.apache.org by go...@apache.org on 2013/08/01 22:15:48 UTC
svn commit: r1509413 - in /incubator/climate/branches/RefactorInput/ocw:
plotter.py tests/test_plotter.py
Author: goodman
Date: Thu Aug 1 20:15:48 2013
New Revision: 1509413
URL: http://svn.apache.org/r1509413
Log:
CLIMATE-239 - Remove Plotter class from plotter.py
Modified:
incubator/climate/branches/RefactorInput/ocw/plotter.py
incubator/climate/branches/RefactorInput/ocw/tests/test_plotter.py
Modified: incubator/climate/branches/RefactorInput/ocw/plotter.py
URL: http://svn.apache.org/viewvc/incubator/climate/branches/RefactorInput/ocw/plotter.py?rev=1509413&r1=1509412&r2=1509413&view=diff
==============================================================================
--- incubator/climate/branches/RefactorInput/ocw/plotter.py (original)
+++ incubator/climate/branches/RefactorInput/ocw/plotter.py Thu Aug 1 20:15:48 2013
@@ -15,13 +15,6 @@
# specific language governing permissions and limitations
# under the License.
-'''
-Classes:
- Plotter - Visualizes pre-calculated metrics
-'''
-
-import os
-import logging
from tempfile import TemporaryFile
import matplotlib as mpl
import matplotlib.pyplot as plt
@@ -31,170 +24,7 @@ import scipy.stats.mstats as mstats
import numpy as np
import numpy.ma as ma
from utils.taylor import TaylorDiagram
-#from toolkit import plots
-class Plotter:
- '''
- Visualizes pre-calculated metrics
-
- This class is used for quickly plotting metrics as processed by an
- :class:`evaluation.Evaluation` object. Different plots are
- generated depending on the type of metric. For example, spatial
- correlation (2D) would be contoured over a map projection region.
- On the other hand, a time series of annual averages would be
- drawn as a line plot. Each plot is saved in a supported format
- (eg png, eps, pdf) to a user-specified directory.
-
- If the given evaluation contains multiple target datasets (eg multiple
- models), subplots will be drawn for each dataset in a single file.
- '''
-
- def __init__(self, evaluation, workdir=None, fmt='png'):
- '''
- Plotter Constructor.
-
- :param evaluation: An evaluation that has been previously ran for a
- series of datasets.
- :type evaluation: Evaluation
- :param workdir: The working directory to save generated figures.
- If set to None, it will be set to your current working directory.
- :type workdir: [optional] str or None
- :param fmt: The file type to save figures. Supported formats include
- png, eps, and pdf.
- :type fmt: [optional] str
- '''
- if workdir is None:
- workdir = os.getcwd()
-
- self.workdir = workdir
- self.fmt = fmt
- self._plot_data = []
- self.assign_plot_data(evaluation)
-
- def assign_plot_data(self, evaluation):
- '''
- Builds a list of plotting functions and corresponding arguments based
- on the shape of each result in the evaluation.
-
- Plotting function shapes are based on the length of the following
- evaluation attributes (X denotes an arbitrary value):
- Contour Map: datasets, lats, lons
- Portrait Diagram: X, subregions, datasets (not supported)
- Time Series: subregions, datasets, times
- Taylor Diagram: datasets, 2
-
- Arguments:
- :param evaluation: A previously ran Evaluation instance
- :type evaluation: Evaluation
- '''
- # Check if the evaluation was ran previously.
- try:
- results = evaluation.results
- except AttributeError:
- raise ValueError('evaluation must be ran before being processed')
-
- # Load dataset attributes
- names = [dataset.name for dataset in evaluation.target_datasets]
- lats = evaluation.ref_dataset.lats
- lons = evaluation.ref_dataset.lons
- variable = evaluation.ref_dataset.variable
- times = evaluation.ref_dataset.times
- mtimes = [t for t in times if t.day == 1][:12]
- ytimes = [t for t in times if t.month == 1 and t.day == 1]
-
- # For default plotting always assume two columns
- gridshape = len(names), 2
-
- # Process results
- for result, metric in zip(results, evaluation.metrics):
-
- # Convert result to numpy array for easier processing
- result = np.array(result)
-
- # Plot title
- ptitle = '%s_%s' %(variable, metric.__name__)
-
- # Contour Map
- if result.shape == (len(names), len(lats), len(lons)):
- plotfunc = draw_contour_map
- fname = os.path.join(self.workdir, '%s_contour_map'
- %(metric.__name__))
- args = result, lats, lons, fname
- kwargs = {'fmt': self.fmt,
- 'subtitles': names,
- 'gridshape': gridshape,
- 'ptitle': title
- }
-
- # Taylor Diagram
- elif result.shape == (len(names), 2):
- plotfunc = draw_taylor_diagram
- fname = os.path.join(self.workdir, '%s_taylor_diagram'
- %(metric.name))
- refname = self.ref_dataset.name
- args = result, names, refname
- kwargs = {'fmt': self.fmt, 'ptitle': title}
-
- # Time Series
- elif result.shape[2] == len(mtimes) or result.shape[2] == len(ytimes):
- plotfunc = draw_time_series
- fname = os.path.join(self.workdir, '%s_time_series'
- %(metric.name))
-
- # Arguments vary slightly depending on whether the time series
- # is monthly or annual
- if result.shape[2] == len(mtimes):
- ptimes = mtimes
- label_month = True
- else:
- ptimes = ytimes
- label_month = False
-
- # Number of datasets is always the first dimension in result
- # so make it the second dimension to be compatible with the
- # plotting function.
- result = result.swapaxes(1, 0)
- args = result, ptimes, names, fname
- kwargs = {'fmt': self.fmt,
- 'gridshape': gridshape,
- 'ptitle': title,
- 'label_month': label_month
- # TODO: Subplot Titles by subregion?
- }
-
- # Automatic plotting currently not supported for given result
- else:
- warning = 'No plotting function found for metric %s.' \
- 'Consider assigning one manually and rerun the' \
- 'evaluation.' %(metric.__name__)
- logging.warning(warning)
- continue
-
- self.add_plot_func(plotfunc, *args, **kwargs)
-
- def add_plot_func(self, plotfunc, *args, **kwargs):
- '''
- Add a plotting function to the plotter with given positional and
- keyword arguments.
-
- :param plotfunc: Reference to a plotting function.
- :type plotfunc: function
- '''
- self._plot_data.append((plotfunc, args, kwargs))
-
- def make_plots(self):
- '''
- Calls the stored plotting functions
- '''
- if not self._plot_data:
- raise AttributeError('No compatible plotting functions found!')
-
- for plotfunc, args, kwargs in self._plot_data:
- plotfunc(*args, **kwargs)
-
-
-# Plotting function library. Originally from a separate file, but placed here
-# for now so that the original rcmet code in this branch still works
def _nice_intervals(data, nlevs):
'''
Purpose::
Modified: incubator/climate/branches/RefactorInput/ocw/tests/test_plotter.py
URL: http://svn.apache.org/viewvc/incubator/climate/branches/RefactorInput/ocw/tests/test_plotter.py?rev=1509413&r1=1509412&r2=1509413&view=diff
==============================================================================
--- incubator/climate/branches/RefactorInput/ocw/tests/test_plotter.py (original)
+++ incubator/climate/branches/RefactorInput/ocw/tests/test_plotter.py Thu Aug 1 20:15:48 2013
@@ -18,32 +18,9 @@
'''Unit tests for the plotter.py module'''
import unittest
-import os
-import datetime
-import metrics
-import numpy as np
-from dataset import Dataset
-from evaluation import Evaluation
-from plotter import Plotter
class TestPlotter(unittest.TestCase):
- def setUp(self):
- pass
-
- def test_init(self):
- pass
-
- def test_assign_plot_data_contour_map(self):
- pass
-
- def test_assign_plot_data_time_series(self):
- pass
-
- def test_assign_plot_data_taylor_diagram(self):
- pass
-
- def test_assign_plot_data_portrait_diagram(self):
- pass
+ pass
if __name__ == '__main__':
unittest.main()
\ No newline at end of file