def test_invalid_mean_with_no_entry_sized_timeseries(self): """ Sized Timeseries should throw when calling mean and len == 0. """ for i, ts_class in SIZED_CONCRETE_CLASSES: ts = TimeSeries([], []) with self.assertRaises(Exception): ts.mean() scores.append(('#ts', '%s throws on mean() when len == 0' % i, 1))
def test_unary(): a = TimeSeries([1, 1.5, 2, 2.5, 10], [0, 2, -1, 0.5, 0]) assert abs(a) == 2.29128784747792 assert bool(a) == True assert -a == TimeSeries([1.0, 1.5, 2.0, 2.5, 10.0], [-0.0, -2.0, 1.0, -0.5, -0.0]) assert +a == TimeSeries([1, 1.5, 2, 2.5, 10], [0, 2, -1, 0.5, 0])
def test_iteritems(): data = [0, 1, 2, 3, 4] time = [5, 6, 7, 8, 9] ts = TimeSeries(data, time) l = ts.iteritems() next(l) assert next(l) == (6, 1)
def test_itertimes(): data = [0, 1, 2, 3, 4] #range(0,5) time = [5, 6, 7, 8, 9] #range(5,10) ts = TimeSeries(data, time) l = ts.itertimes() next(l) assert next(l) == 6
def test_invalid_input_eq(): ts_1 = TimeSeries([1, 2], [3, 4]) ts_2 = TimeSeries([1, 2, 3], [3, 4, 5]) with raises(TypeError): ts_1 == ([3, 1], [4, 2]) with raises(ValueError): ts_1 == ts_2
def test_setitem(): data = [4, 5, 6] time = [1, 2, 3] ts = TimeSeries(data, time) index = 0 val = 0 ts[index] = val assert ts == TimeSeries([0, 5, 6], [1, 2, 3])
def test_interpolation(): # Simple cases c = TimeSeries([1.], [1.2]) d = ts3.interpolate([1.]) assert c == d assert ts3.interpolate(ts4.times()) == TimeSeries([2.5, 7.5], [1.5, 2.5]) # Boundary conditions assert ts3.interpolate([-100, 100]) == TimeSeries([-100, 100], [1, 3])
def test_interpolation_against_boundarys(self): for i, ts_class in SIZED_CONCRETE_CLASSES: ts = TimeSeries([0, 5, 10], [1, 2, 3]) # Boundary conditions ares = ts.interpolate([-100, 100]) self.assertEqual(list(ares.values()), [1, 3]) self.assertEqual(list(ares.times()), [-100, 100]) scores.append(('#ts', '%s interpolate against boundaaryts' % i, 1))
def test_itervalues(): ten = TimeSeries(range(0, 11), range(0, 11)) i = ten.itervalues() n = next(i) assert n == 0 assert n.dtype == np.int64 n = next(i) assert n == 1 assert n.dtype == np.int64
def test_contains(): a = TimeSeries([1, 1.5, 2, 2.5, 10], [0, 2, -1, 0.5, 0]) assert a.__contains__(1.5) == True e3 = '' try: a.__contains__('monkeys') except Exception as e: e3 = e assert type(e3).__name__ == 'str'
def test_itervalues(): ten = TimeSeries(range(0,11), range(0,11)) i = ten.itervalues() n = next(i) assert n == 0 assert n.dtype == np.int64 n = next(i) assert n == 1 assert n.dtype == np.int64
def test_sub(): a = TimeSeries([1, 1.5, 2, 2.5, 10], [0, 2, -1, 0.5, 0]) b = TimeSeries([1, 1.5, 2, 2.5, 10], [1, 2, 3, 4, 5]) assert a - b == TimeSeries([1.0, 1.5, 2.0, 2.5, 10.0], [-1.0, 0.0, -4.0, -3.5, -5.0]) assert 3 - a == TimeSeries([1.0, 1.5, 2.0, 2.5, 10.0], [3.0, 1.0, 4.0, 2.5, 3.0]) assert a - 3 == TimeSeries([1.0, 1.5, 2.0, 2.5, 10.0], [-3.0, -1.0, -4.0, -2.5, -3.0])
def test_sub(): a = TimeSeries([0, 5, 10], [1, 2, 3]) b = TimeSeries([0, 5, 10], [10, 20, 30]) c = 100 d = TimeSeries([0, 1, 2], [1, 2, 3]) assert b - a == TimeSeries([0, 5, 10], [9, 18, 27]) assert a - c == TimeSeries([0, 5, 10], [-99, -98, -97]) with raises(ValueError): a - d
def test_bool_operator_with_nonempty_sized_timeseries(self): """ Sized Timeseries with any length > 0 should return True when l2 norm is non-zero. """ for i, ts_class in SIZED_CONCRETE_CLASSES: tsa = TimeSeries([1, 2, 3, 4, 5], [0.1, 0.2, 0.3, 0.4, 0.5]) tsb = TimeSeries([1], [0.1]) self.assertTrue(bool(tsa)) self.assertTrue(bool(tsb)) scores.append( ('#ts', '%s bool should return true for lengths > 0' % i, 1))
def test_interpolation_with_simple_input(self): for i, ts_class in SIZED_CONCRETE_CLASSES: ts = TimeSeries([0, 5, 10], [1, 2, 3]) ares = ts.interpolate([1]) self.assertEqual(ares.values()[0], 1.2) self.assertEqual(ares.times()[0], 1) ares = ts.interpolate([2.5, 7.5]) self.assertEqual(list(ares.values()), [1.5, 2.5]) self.assertEqual(list(ares.times()), [2.5, 7.5]) scores.append(('#ts', 'test interpol base', 3))
def test_iteritems(): ten = TimeSeries(range(0, 11), range(0, 11)) i = ten.iteritems() n = next(i) assert n == (0, 0) assert len(n) == 2 assert n[0] == 0 assert n[1] == 0 n = next(i) assert n == (1, 1)
def test_iteritems(): ten = TimeSeries(range(0,11), range(0,11)) i = ten.iteritems() n = next(i) assert n == (0, 0) assert len(n) == 2 assert n[0] == 0 assert n[1] == 0 n = next(i) assert n == (1, 1)
def smoke_test(): threes = TimeSeries(range(0, 1000, 3)) fives = TimeSeries(range(0, 1000, 5)) s = 0 for i in range(0, 1000): if i in threes or i in fives: s += i print("sum", s)
def test_invalid_input(): with raises(ValueError): TimeSeries([1, 2], [3, 4, 5]) with raises(TypeError): TimeSeries(1, 2) with raises(TypeError): TimeSeries(1, [1]) with raises(TypeError): TimeSeries([1, 2], 3) with raises(ValueError): TimeSeries([1, 2], [2, 2])
def test_invalid_input_add(): ts_1 = TimeSeries([1, 2], [3, 4]) ts_2 = TimeSeries([1, 2, 3], [3, 4, 5]) ts_3 = TimeSeries([2, 3], [5, 6]) arry = [1, 2] with raises(ValueError): ts_sum = ts_1 + ts_2 with raises(TypeError): ts_sum = ts_1 + arry with raises(ValueError): ts_sum = ts_1 + ts_3
def test_add(): c = 100 d = TimeSeries([0, 1, 2], [1, 2, 3]) assert ts1 + ts2 == TimeSeries(range(0, 4), [11, 22, 33, 44, 55]) assert ts1 + c == TimeSeries(range(0, 4), [101, 102, 103, 104, 105]) with raises(ValueError): ts1 + d assert ts1 + ts2 == ts2 + ts1 assert c + ts1 == ts1 + c with raises(ValueError): d + ts1
def test_mul(): a = TimeSeries([0, 5, 10], [1, 2, 3]) b = TimeSeries([0, 5, 10], [10, 20, 30]) c = 100 d = TimeSeries([0, 1, 2], [1, 2, 3]) assert a * b == TimeSeries([0, 5, 10], [10, 40, 90]) assert a * c == TimeSeries([0, 5, 10], [100, 200, 300]) with raises(ValueError): a * d assert a * b == b * a assert c * a == a * c with raises(ValueError): d * a
def test_pype4(): time = [] values = [] for x in range(100): time.append(x) values.append(x - 50) a = TimeSeries(time, values) ast = parser.parse(input1, lexer=lexer) # Semantic analysis ast.walk(CheckSingleAssignment()) ast.walk(CheckSingleIOExpression()) syms = ast.walk(SymbolTableVisitor()) ast.walk(CheckUndefinedVariables(syms)) # Translation ir = ast.mod_walk(LoweringVisitor(syms)) ir.flowgraph_pass(AssignmentEllision()) ir.flowgraph_pass(DeadCodeElimination()) ir.topological_flowgraph_pass(InlineComponents()) # PCode Generation pcodegen = PCodeGenerator() ir.flowgraph_pass(pcodegen) pcodes = pcodegen.pcodes standardized_TS = pcodes['standardize'].run(a) assert (round(standardized_TS.mean(), 7) == 0) assert (round(standardized_TS.std() - 1, 7) == 0)
def test_invalid_multiplication_with_inconsistent_sizes(self): """ Sized Timeseries should throw when multiplying a differently-sized time-series. """ for i, ts_class in SIZED_CONCRETE_CLASSES: tsa = TimeSeries([1, 2, 3, 4, 5], [0.1, 0.2, 0.3, 0.4, 0.5]) tsb = TimeSeries([1, 2, 3], [0.5, 0.2, 0.9]) tsc = TimeSeries([], []) with self.assertRaises(ValueError): tsa * tsb with self.assertRaises(ValueError): tsa * tsc with self.assertRaises(ValueError): tsb * tsc scores.append(( '#ts', '%s class should throw when multiplying two differently-sized timeseries' % i, 1))
def test_setitem_index_error(): data = [4, 5, 6] time = [1, 2, 3] ts = TimeSeries(data, time) index = 5 val = 0 with raises(IndexError): ts[index] = val
def test_invalid_equality_check_with_different_length_sized_timeseries( self): """ Sized Timeseries with the different sizes should throw error. """ for i, ts_class in SIZED_CONCRETE_CLASSES: tsa = TimeSeries([1, 2, 3, 4], [0.1, 0.2, 0.3, 0.4]) tsb = TimeSeries([1, 2, 3], [0.5, 0.2, 0.9]) tsc = TimeSeries([], []) with self.assertRaises(ValueError): tsa == tsb with self.assertRaises(ValueError): tsa == tsc with self.assertRaises(ValueError): tsb == tsc scores.append(( '#ts', '%s class equality check should throw when ts classes have different sizes' % i, 1))
def test_bool_operator_with_nonempty_sized_timeseries(self): """ Sized Timeseries with any length > 0 should return False when l2 norm is zero. """ for i, ts_class in SIZED_CONCRETE_CLASSES: ts = TimeSeries([0, 1, 2, 3], [0, 0, 0, 0]) self.assertFalse(bool(ts)) scores.append( ('#ts', '%s bool should return false when l2 norm is 0' % i, 1))
def test_valid_init(self): """ All the sized timeseries __init__ constructors should take any sequence-like thing. Unsized should take a generator. """ # NOTE: Depending on intepretation, this might be safe ts = TimeSeries([0], [0]) ats = ArrayTimeSeries([0], [0]) sts = SimulatedTimeSeries(zero_generator) scores.append(('#ts', 'init valid ts, ats, sts', 3))
def tsmaker(m, s, j): "returns metadata and a time series in the shape of a jittered normal" meta = {} meta['order'] = int( np.random.choice([-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5])) meta['blarg'] = int(np.random.choice([1, 2])) t = np.arange(0.0, 1.0, 0.01) v = norm.pdf(t, m, s) + j * np.random.randn(100) return meta, TimeSeries(t, v)
def test_divide(): a = TimeSeries([1, 2, 3, 10], [5, 10, 15, 20]) b = TimeSeries([1, 2, 3, 10], [15, 100, 150, 200]) c = TimeSeries([1, 2, 3], [15, 100, 150]) assert (a / 5) == TimeSeries([1, 2, 3, 10], [1.0, 2.0, 3.0, 4.0]) assert (b / a) == TimeSeries([1, 2, 3, 10], [3.0, 10.0, 10.0, 10.0]) e3 = '' try: test = c / a except Exception as e: e3 = e assert type(e3).__name__ == 'ValueError' e3 = '' try: test = c / complex(1) except Exception as e: e3 = e assert type(e3).__name__ == 'AttributeError'
def test_valid_nonempty_str_and_repr(self): """ All concrete classes should have a __str__() and __repr__(). Repeats are acceptable. """ ts = TimeSeries([0], [0]) ats = ArrayTimeSeries([0], [0]) sts = SimulatedTimeSeries(zero_generator) for ts_class in [ts, ats, sts]: dunder_str = str(ts_class) dunder_repr = repr(ts_class) self.assertNotEqual(dunder_str, '') self.assertNotEqual(dunder_repr, '') scores.append( ('#ts', 'all concrete classes should have non-empty str and repr', 1))
def test_valid_mean_with_two_entry_sized_timeseries(self): """ Sized Timeseries should return mean when len > 1. """ for i, ts_class in SIZED_CONCRETE_CLASSES: tsa = TimeSeries([1, 2], [1, 2]) tsb = TimeSeries([1, 2, 3, 4], [1, 2, 3, 4]) self.assertEqual(tsa.mean(), 1.5) self.assertEqual(tsb.mean(), np.array([1, 2, 3, 4]).mean()) scores.append(('#ts', '%s valid mean when len > 1' % i, 1))
def test_interpolate(): a = TimeSeries([1, 1.5, 2, 2.5, 10], [0, 2, -1, 0.5, 0]) assert a.interpolate([2]) == TimeSeries([2],[-1])
def test_iter(): ten = TimeSeries(range(0,11), range(0,11)) i = ten.__iter__() n = next(i) assert n == 0 assert n.dtype == np.int64
def test_median(): median = TimeSeries(range(0,3), range(0,3)) assert median.median() == 1
def test_std(): ten = TimeSeries([1, 2, 3, 4, 5], [600, 470, 170, 430, 300]) print(ten.std()) assert int(ten.std()) == 147
def test_mean(): ten = TimeSeries(range(0,11), range(0,11)) assert ten.mean() == 5
def test_items(): a = TimeSeries([1, 1.5, 2, 2.5, 10, 11], [0, 2, -1, 0.5, 0, 0]) test = a.items() assert test == [(1.0, 0.0), (1.5, 2.0), (2.0, -1.0), (2.5, 0.5), (10.0, 0.0), (11.0, 0.0)]
def test_calling(): a = TimeSeries([1, 1.5, 2, 2.5, 10], [0, 2, -1, 0.5, 0]) assert a.times() == [1, 1.5, 2, 2.5, 10] assert a.values() == [0, 2, -1, 0.5, 0]
def test_interpolation(): a = TimeSeries([0,5,10], [1,2,3]) b = TimeSeries([2.5,7.5], [100, -100]) assert (a.interpolate([1]) == TimeSeries([1],[1.2])) assert (a.interpolate(b.times()) == TimeSeries([2.5,7.5], [1.5, 2.5])) assert (a.interpolate([-100,100]) == TimeSeries([-100,100], [1,3]))