Example #1
0
 def test_mark_greaterthan(self):
     y = [ 0, -1, 0, 0, 0, 1, 1, -1, -1, -1, 
           1,  1, 0, 0, 0, 0, 1,  1,  0,  0]
     klust = Cluster(y,0)
     assert_equal(klust.starts, [ 0,  1,  2,  5,  7, 10, 12, 16, 18])
     assert_equal(klust.mark_greaterthan(3),
                  [1,1,0,0,0,1,1,0,0,0,1,1,0,0,0,0,1,1,1,1])
 def test_mark_greaterthan(self):
     y = [0, -1, 0, 0, 0, 1, 1, -1, -1, -1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0]
     klust = Cluster(y, 0)
     assert_equal(klust.starts, [0, 1, 2, 5, 7, 10, 12, 16, 18])
     assert_equal(
         klust.mark_greaterthan(3),
         [1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1])
 def test_grouped_slices(self):
     "Make sure we're not massing w/ .uniques ..."
     y = [0, -1, 0, 0, 0, 1, 1, -1, -1, -1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0]
     klust = Cluster(y, 0)
     uniques_before = klust.uniques
     klust.grouped_slices()
     uniques_after = klust.uniques
     assert_equal(uniques_before, uniques_after)
Example #4
0
 def test_grouped_slices(self):
     "Make sure we're not massing w/ .uniques ..."
     y = [ 0, -1, 0, 0, 0, 1, 1, -1, -1, -1, 
           1,  1, 0, 0, 0, 0, 1,  1,  0,  0]
     klust = Cluster(y,0)
     uniques_before = klust.uniques
     klust.grouped_slices()
     uniques_after = klust.uniques
     assert_equal(uniques_before, uniques_after)
 def test_cluster(self):
     sequence = [0, 0, 1, 2, 2, 2, 3, 4, 3, 4, 4, 4]
     klust = Cluster(sequence, 0)
     assert_equal(klust.clustered, [[
         0,
         0,
     ], [
         1,
     ], [
         2,
         2,
         2,
     ], [
         3,
     ], [
         4,
     ], [
         3,
     ], [
         4,
         4,
         4,
     ]])
     assert_equal(klust.uniques, [0, 1, 2, 3, 4, 3, 4])
     assert_equal(len(klust.uniques), len(klust.clustered))
     assert_equal(klust.slices, [
         slice(0, 2),
         slice(2, 3),
         slice(3, 6),
         slice(6, 7),
         slice(7, 8),
         slice(8, 9),
         slice(9, 12)
     ])
     #
     x = [
         1.8, 1.3, 2.4, 1.2, 2.5, 3.9, 1., 3.8, 4.2, 3.3, 1.2, 0.2, 0.9,
         2.7, 2.4, 2.8, 2.7, 4.7, 4.2, 0.4
     ]
     klust = Cluster(x, 1)
     assert_equal(klust.starts, [0, 2, 3, 4, 5, 6, 7, 10, 13, 17, 19])
     assert_equal(Cluster(x, 1.5).starts, [0, 6, 7, 10, 13, 17, 19])
     assert_equal(Cluster(x, 2.5).starts, [0, 6, 7, 19])
     assert_equal(
         Cluster(x, 2.5, np.greater).starts,
         [0, 1, 2, 3, 4, 5, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18])
Example #6
0
    def plot_enso_background(self, ensoindices=None, lag=0, **optinfo):
        """
    Plots colored stripes in the background of plot to represent ENSO phases.

    Parameters
    ----------
    ensophases: {array-like}, optional
        Array of ENSO indices (``+1`` for El Niño, ``0`` for Neutral and
        ``-1`` for La Niña episodes).
        If None, the ENSO indices of the underlying series are used instead.
    
        """
        if ensoindices is None:
            series = self._series
            if series is None or not hasattr(series, 'ensoindices') or \
                series.ensoindices is None:
                raise ValueError("Undefined ENSO indices!")
            ensoindices = series.ensoindices
        if self.xdata is None:
            errmsg = "Unable to retrieve the dates of the current plot!"
            raise ValueError(errmsg)
        #
        dates = self.xdata
        clust_indices = Cluster(ensoindices.filled(0), 0)
        _dates = np.empty(len(dates) + 1, int)
        _dates[:-1] = dates
        _dates[-1] = dates[-2]
        episodes = dict([(k, zip(_dates[v[:, 0]], _dates[v[:, 1]]))
                         for (k, v) in clust_indices.grouped_limits().items()])
        #
        colors = ENSOcolors['polygons']
        for (key, idx) in {'C': -1, 'N': 0, 'W': +1}.iteritems():
            colors[idx] = colors[key]
        #
        trans = blended_transform_factory(self.transData, self.transAxes)
        for (k, lim) in episodes.iteritems():
            _bbc = BrokenBarHCollection(
                [(x + lag, y - x) for (x, y) in lim],
                (0, 1),
                facecolors=colors[k],
                edgecolors=colors[k],
            )
            _bbc.set_alpha(0.2)
            _bbc.set_transform(trans)
            self.add_collection(_bbc)
Example #7
0
    def plot_enso_background(self, ensoindices=None, lag=0, **optinfo):
        """
    Plots colored stripes in the background of plot to represent ENSO phases.

    Parameters
    ----------
    ensophases: {array-like}, optional
        Array of ENSO indices (``+1`` for El Niño, ``0`` for Neutral and
        ``-1`` for La Niña episodes).
        If None, the ENSO indices of the underlying series are used instead.
    
        """
        if ensoindices is None:
            series = self._series
            if series is None or not hasattr(series, 'ensoindices') or \
                series.ensoindices is None:
                raise ValueError("Undefined ENSO indices!")
            ensoindices = series.ensoindices
        if self.xdata is None:
            errmsg = "Unable to retrieve the dates of the current plot!"
            raise ValueError(errmsg)
        #
        dates = self.xdata
        clust_indices = Cluster(ensoindices.filled(0), 0)
        _dates = np.empty(len(dates) + 1, int)
        _dates[:-1] = dates
        _dates[-1] = dates[-2]
        episodes = dict([(k, zip(_dates[v[:, 0]], _dates[v[:, 1]]))
                         for (k, v) in clust_indices.grouped_limits().items()])
        #
        colors = ENSOcolors['polygons']
        for (key, idx) in {'C':-1, 'N':0, 'W':+1}.iteritems():
            colors[idx] = colors[key]
        #
        trans = blended_transform_factory(self.transData, self.transAxes)
        for (k, lim) in episodes.iteritems():
            _bbc = BrokenBarHCollection([(x + lag, y - x) for (x, y) in lim],
                                         (0, 1),
                                         facecolors=colors[k],
                                         edgecolors=colors[k],)
            _bbc.set_alpha(0.2)
            _bbc.set_transform(trans)
            self.add_collection(_bbc)