def test_phases(self): "Test indices" ensoi = self.ensoi series = ClimateSeries(np.random.rand(len(ensoi)), dates=ensoi._dates, ensoindicator = ensoi) series.set_ensoindices(minimum_size=5, reference_season='NDJ') # Test cold/neutral cold = series.cold assert_equal(cold._mask, [ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,]) neutral = series.neutral assert_equal(neutral._mask, [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,]) warm = series.warm assert_equal(warm._mask, [ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1,])
def test_pickling(self): "Tests pickling" import cPickle ensoi = self.ensoi series = ClimateSeries(np.random.rand(len(ensoi)), dates=ensoi._dates, ensoindicator=ensoi) series.set_ensoindices(minimum_size=5, reference_season='NDJ') pickled = cPickle.loads(cPickle.dumps(series)) assert_equal(series, pickled) assert_equal(pickled.ensoindicator, pickled.ensoindicator) assert_equal(series.ensoindices, pickled.ensoindices)
def test_pickling(self): "Tests pickling" import cPickle ensoi = self.ensoi series = ClimateSeries(np.random.rand(len(ensoi)), dates=ensoi._dates, ensoindicator = ensoi) series.set_ensoindices(minimum_size=5, reference_season='NDJ') pickled = cPickle.loads(cPickle.dumps(series)) assert_equal(series, pickled) assert_equal(pickled.ensoindicator, pickled.ensoindicator) assert_equal(series.ensoindices, pickled.ensoindices)
def test_reshape(self): "Test reshape" ensoi = self.ensoi series = ClimateSeries(np.random.rand(len(ensoi)), dates=ensoi._dates, ensoindicator=ensoi) series.set_ensoindices(minimum_size=5, reference_season='NDJ') # series = series.reshape(1, -1) assert_equal(series.ensoindicator.shape, series.shape) self.failUnless(series.ensoindicator.shape != ensoi.shape) assert_equal(series.ensoindicator, ensoi.reshape(1, -1)) # series.shape = (-1, 1) assert_equal(series.ensoindicator.shape, series.shape) self.failUnless(series.ensoindicator.shape != ensoi.shape) assert_equal(series.ensoindicator, ensoi.reshape(-1, 1))
def test_apply_on_phase(self): "Test apply on phase" ensoi = self.ensoi series = ClimateSeries(np.random.rand(len(ensoi)), dates=ensoi._dates, ensoindicator=ensoi) series.set_ensoindices(minimum_size=5, reference_season='NDJ') func = ma.mean _cold = func(series[(series.ensoindices == -1).filled()]) _neut = func(series[(series.ensoindices == 0).filled()]) _warm = func(series[(series.ensoindices == +1).filled()]) _glob = func(series) control = ma.array([(_cold, _neut, _warm, _glob)], dtype=[('cold', float), ('neutral', float), ('warm', float), ('global', float)]) control['warm'] = masked result = apply_on_phase(series, func)
def test_reshape(self): "Test reshape" ensoi = self.ensoi series = ClimateSeries(np.random.rand(len(ensoi)), dates=ensoi._dates, ensoindicator = ensoi) series.set_ensoindices(minimum_size=5, reference_season='NDJ') # series = series.reshape(1, -1) assert_equal(series.ensoindicator.shape, series.shape) self.failUnless(series.ensoindicator.shape != ensoi.shape) assert_equal(series.ensoindicator, ensoi.reshape(1, -1)) # series.shape = (-1, 1) assert_equal(series.ensoindicator.shape, series.shape) self.failUnless(series.ensoindicator.shape != ensoi.shape) assert_equal(series.ensoindicator, ensoi.reshape(-1, 1))
def test_apply_on_phase(self): "Test apply on phase" ensoi = self.ensoi series = ClimateSeries(np.random.rand(len(ensoi)), dates=ensoi._dates, ensoindicator = ensoi) series.set_ensoindices(minimum_size=5, reference_season='NDJ') func = ma.mean _cold = func(series[(series.ensoindices==-1).filled()]) _neut = func(series[(series.ensoindices==0).filled()]) _warm = func(series[(series.ensoindices==+1).filled()]) _glob = func(series) control = ma.array([(_cold, _neut, _warm, _glob)], dtype=[('cold',float),('neutral',float), ('warm',float),('global',float)]) control['warm'] = masked result = apply_on_phase(series, func)
def test_indices(self): "Test indices" ensoi = self.ensoi series = ClimateSeries(np.random.rand(len(ensoi)), dates=ensoi._dates, ensoindicator = ensoi) # Try without initializiation: it should fail try: indc = series.ensoindices except ValueError: pass # So, initialize it: series.set_ensoindices(minimum_size=5, reference_season='NDJ') indc = series.ensoindices assert_equal(indc, [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,-1,-1, -1,-1,-1,-1,-1, 0, 0,-1,-1,-1,-1,-1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,+1,+1, +1,+1,+1,+1,+1,+1,+1, 0, 0, 0, 0, 0,])
def test_indices_convert(self): "Test the conversion of ensoindices from one frequency to another." ensoi = self.ensoi series = ClimateSeries(np.random.rand(len(ensoi)), dates=ensoi._dates, ensoindicator = ensoi) series.set_ensoindices(minimum_size=5, reference_season='NDJ') control = ts.time_series([ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,-1,-1, -1,-1,-1,-1,-1, 0, 0,-1,-1,-1,-1,-1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,+1,+1, +1,+1,+1,+1,+1,+1,+1, 0, 0, 0, 0, 0,], dates=ensoi.dates) assert_equal(series.ensoindices, control) # Conversion 'M' to 'D' dseries = series.convert('D') assert_equal(dseries.ensoindices, ts.lib.backward_fill(control.convert('D'))) # Conversion 'M' to 'A' aseries = series.convert('A', func=ma.mean) assert_equal(aseries.ensoindices, mode(control.convert('A'), axis=1)[0].squeeze())
def test_indices(self): "Test indices" ensoi = self.ensoi series = ClimateSeries(np.random.rand(len(ensoi)), dates=ensoi._dates, ensoindicator=ensoi) # Try without initializiation: it should fail try: indc = series.ensoindices except ValueError: pass # So, initialize it: series.set_ensoindices(minimum_size=5, reference_season='NDJ') indc = series.ensoindices assert_equal(indc, [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1, -1, -1, -1, -1, 0, 0, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, +1, +1, +1, +1, +1, +1, +1, +1, +1, 0, 0, 0, 0, 0, ])
def test_phases(self): "Test indices" ensoi = self.ensoi series = ClimateSeries(np.random.rand(len(ensoi)), dates=ensoi._dates, ensoindicator=ensoi) series.set_ensoindices(minimum_size=5, reference_season='NDJ') # Test cold/neutral cold = series.cold assert_equal(cold._mask, [ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, ]) neutral = series.neutral assert_equal(neutral._mask, [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, ]) warm = series.warm assert_equal(warm._mask, [ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, ])
def test_indices_convert(self): "Test the conversion of ensoindices from one frequency to another." ensoi = self.ensoi series = ClimateSeries(np.random.rand(len(ensoi)), dates=ensoi._dates, ensoindicator=ensoi) series.set_ensoindices(minimum_size=5, reference_season='NDJ') control = ts.time_series([ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1, -1, -1, -1, -1, 0, 0, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, +1, +1, +1, +1, +1, +1, +1, +1, +1, 0, 0, 0, 0, 0, ], dates=ensoi.dates) assert_equal(series.ensoindices, control) # Conversion 'M' to 'D' dseries = series.convert('D') assert_equal(dseries.ensoindices, ts.lib.backward_fill(control.convert('D'))) # Conversion 'M' to 'A' aseries = series.convert('A', func=ma.mean) assert_equal(aseries.ensoindices, mode(control.convert('A'), axis=1)[0].squeeze())