]> git.parisson.com Git - timeside.git/commitdiff
AnalyzerResults: put back a render method that retrun a matplotlib figure
authorThomas Fillon <thomas@parisson.com>
Fri, 7 Mar 2014 10:53:49 +0000 (11:53 +0100)
committerThomas Fillon <thomas@parisson.com>
Fri, 7 Mar 2014 10:53:49 +0000 (11:53 +0100)
timeside/analyzer/core.py

index 095a1ef4b9b5aa770643c1f53ba40ce869476e16..4b9a032efa01886240c44aec239e8b446bd8717c 100644 (file)
@@ -21,6 +21,7 @@
 #   Guillaume Pellerin <yomguy at parisson.com>
 #   Paul Brossier <piem@piem.org>
 #   Thomas Fillon <thomas  at parisson.com>
+
 from __future__ import division
 
 from timeside.core import Processor
@@ -31,17 +32,14 @@ import h5py
 import h5tools
 
 import os
-import matplotlib
-matplotlib.use('Agg')
-
-from matplotlib.figure import Figure
-from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
 
-#if 'DISPLAY' not in os.environ:
-#    import matplotlib
-#    matplotlib.use('Agg')
+if 'DISPLAY' not in os.environ:
+    import matplotlib
+    matplotlib.use('Agg')
 
 import matplotlib.pyplot as plt
+from matplotlib.figure import Figure
+from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
 
 numpy_data_types = [
     #'float128',
@@ -629,6 +627,20 @@ class AnalyzerResult(MetadataObject):
     def _render_plot(self, ax):
         return NotImplemented
 
+    def render(self):
+        '''Render a matplotlib figure from the analyzer result
+
+           Return the figure, use fig.show() to display if neeeded
+        '''
+        # TODO : this may crash if the data array is too large
+        # possible workaround downsampled the data
+        #  and plot center, min, max values
+        # see http://stackoverflow.com/a/8881973
+
+        fig, ax = plt.subplots()
+        self._render_plot(ax)
+        return fig
+
     def _render_PIL(self, size=(1024, 256), dpi=80):
         from ..grapher.core import Image
         image_width, image_height = size
@@ -813,13 +825,13 @@ class FrameLabelResult(LabelObject, FramewiseObject, AnalyzerResult):
 class EventValueResult(ValueObject, EventObject, AnalyzerResult):
     pass
 
+
 class EventLabelResult(LabelObject, EventObject, AnalyzerResult):
     pass
 
+
 class SegmentValueResult(ValueObject, SegmentObject, AnalyzerResult):
     def _render_plot(self, ax):
-        import itertools
-        colors = itertools.cycle(['b', 'g', 'r', 'c', 'm', 'y', 'k'])
         for time, value in (self.time, self.data):
             ax.axvline(time, ymin=0, ymax=value, color='r')
             # TODO : check value shape !!!
@@ -850,7 +862,6 @@ class AnalyzerResultContainer(dict):
     >>> resContainer = timeside.analyzer.core.AnalyzerResultContainer()
     '''
 
-
     def __init__(self, analyzer_results=None):
         super(AnalyzerResultContainer, self).__init__()
         if analyzer_results is not None: