def testExpFunction(self): ma5 = MovingAverage(5, 'close') holder = Exp(MovingAverage(5, 'close')) holder2 = MovingAverage(5, 'close') >> Exp for i, close in enumerate(self.sampleClose): data = {'close': close} ma5.push(data) holder.push(data) holder2.push(data) expected = math.exp(ma5.result()) calculated = holder.result() self.assertAlmostEqual(calculated, expected, 12, "at index {0:d}\n" "expected: {1:f}\n" "calculated: {2:f}".format(i, expected, calculated)) calculated = holder2.result() self.assertAlmostEqual(calculated, expected, 12, "at index {0:d}\n" "expected: {1:f}\n" "calculated: {2:f}".format(i, expected, calculated))
def testArithmeticFunctionsPickle(self): data = {'x': 1} test = Exp('x') test.push(data) with tempfile.NamedTemporaryFile('w+b', delete=False) as f: pickle.dump(test, f) with open(f.name, 'rb') as f2: pickled = pickle.load(f2) self.assertAlmostEqual(test.value, pickled.value) os.unlink(f.name) test = Log('x') test.push(data) with tempfile.NamedTemporaryFile('w+b', delete=False) as f: pickle.dump(test, f) with open(f.name, 'rb') as f2: pickled = pickle.load(f2) self.assertAlmostEqual(test.value, pickled.value) os.unlink(f.name) test = Sqrt('x') test.push(data) with tempfile.NamedTemporaryFile('w+b', delete=False) as f: pickle.dump(test, f) with open(f.name, 'rb') as f2: pickled = pickle.load(f2) self.assertAlmostEqual(test.value, pickled.value) os.unlink(f.name) data['x'] = -1. test = Pow('x', 2) test.push(data) with tempfile.NamedTemporaryFile('w+b', delete=False) as f: pickle.dump(test, f) with open(f.name, 'rb') as f2: pickled = pickle.load(f2) self.assertAlmostEqual(test.value, pickled.value) os.unlink(f.name) test = Abs('x') test.push(data) with tempfile.NamedTemporaryFile('w+b', delete=False) as f: pickle.dump(test, f) with open(f.name, 'rb') as f2: pickled = pickle.load(f2) self.assertAlmostEqual(test.value, pickled.value) os.unlink(f.name) test = Sign('x') test.push(data) with tempfile.NamedTemporaryFile('w+b', delete=False) as f: pickle.dump(test, f) with open(f.name, 'rb') as f2: pickled = pickle.load(f2) self.assertAlmostEqual(test.value, pickled.value) os.unlink(f.name) data['x'] = 1. test = Acos('x') test.push(data) with tempfile.NamedTemporaryFile('w+b', delete=False) as f: pickle.dump(test, f) with open(f.name, 'rb') as f2: pickled = pickle.load(f2) self.assertAlmostEqual(test.value, pickled.value) os.unlink(f.name) test = Asin('x') test.push(data) with tempfile.NamedTemporaryFile('w+b', delete=False) as f: pickle.dump(test, f) with open(f.name, 'rb') as f2: pickled = pickle.load(f2) self.assertAlmostEqual(test.value, pickled.value) os.unlink(f.name) test = Acosh('x') test.push(data) with tempfile.NamedTemporaryFile('w+b', delete=False) as f: pickle.dump(test, f) with open(f.name, 'rb') as f2: pickled = pickle.load(f2) self.assertAlmostEqual(test.value, pickled.value) os.unlink(f.name) test = Asinh('x') test.push(data) with tempfile.NamedTemporaryFile('w+b', delete=False) as f: pickle.dump(test, f) with open(f.name, 'rb') as f2: pickled = pickle.load(f2) self.assertAlmostEqual(test.value, pickled.value) os.unlink(f.name)
def testArithmeticFunctionsDeepcopy(self): data = {'x': 1} test = Exp('x') test.push(data) copied = copy.deepcopy(test) self.assertAlmostEqual(math.exp(data['x']), copied.value) test = Log('x') test.push(data) copied = copy.deepcopy(test) self.assertAlmostEqual(math.log(data['x']), copied.value) test = Sqrt('x') test.push(data) copied = copy.deepcopy(test) self.assertAlmostEqual(math.sqrt(data['x']), copied.value) data['x'] = -1. test = Pow('x', 2) test.push(data) copied = copy.deepcopy(test) self.assertAlmostEqual(data['x']**2, copied.value) test = Abs('x') test.push(data) copied = copy.deepcopy(test) self.assertAlmostEqual(abs(data['x']), copied.value) test = Sign('x') test.push(data) copied = copy.deepcopy(test) self.assertAlmostEqual(-1., copied.value) data['x'] = 1. test = Acos('x') test.push(data) copied = copy.deepcopy(test) self.assertAlmostEqual(math.acos(data['x']), copied.value) test = Asin('x') test.push(data) copied = copy.deepcopy(test) self.assertAlmostEqual(math.asin(data['x']), copied.value) test = Acosh('x') test.push(data) copied = copy.deepcopy(test) self.assertAlmostEqual(math.acosh(data['x']), copied.value) test = Asinh('x') test.push(data) copied = copy.deepcopy(test) self.assertAlmostEqual(math.asinh(data['x']), copied.value)