Esempio n. 1
0
class TestCurveOther(TestCase):
    """
    Other test of Curve class
    """
    def setUp(self):
        self.c = Curve([[0, 0], [5, 5], [10, 0]])

    def test_rebinned(self):
        new_c = self.c.rebinned(step=1)
        self.assertTrue(np.array_equal(new_c.x, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]))
        new_c = self.c.rebinned(step=2, fixp=15)
        self.assertTrue(np.array_equal(new_c.x, [1, 3, 5, 7, 9]))
        new_c = self.c.rebinned(step=2, fixp=0.5)
        self.assertTrue(np.array_equal(new_c.x, [0.5, 2.5, 4.5, 6.5, 8.5]))

    def test_evaluate_at_x(self):
        # test inside and outside domain
        self.assertTrue(np.array_equal(self.c.evaluate_at_x([-1, 0, 1, 10, 11], def_val=37), [37, 0, 1, 0, 37]))
        # test between existing points
        self.assertTrue(np.array_equal(self.c.evaluate_at_x([-0.333, 0.5, 0.7, 7.3], def_val=37), [37, 0.5, 0.7, 2.7]))

    def test_subtract(self):
        # c2 has wider domain than c1
        c1 = Curve([[-1, -1], [0, 0], [1, 5], [2, 0], [3, 0]])
        c2 = Curve([[-2, 1], [-1, 1], [0, 1], [1, 1], [2, 1], [3, 1], [4, 1]])

        # should return None and self
        self.assertIsNone(c1.subtract(c2, new_obj=False))
        self.assertTrue(np.array_equal(c1, [[-1, -2], [0, -1], [1, 4], [2, -1], [3, -1]]))
        # create new object and compare
        self.assertTrue(np.array_equal(c1.subtract(c2, new_obj=True), [[-1, -3], [0, -2], [1, 3], [2, -2], [3, -2]]))
Esempio n. 2
0
class TestCurve(unittest.TestCase):
    def setUp(self):
        self.c = Curve([[0, 0], [5, 5], [10, 0]])

    def test_y_at_x(self):
        assert np.isnan(self.c.y_at_x(-12))  # outside domain, left
        self.assertEqual(self.c.y_at_x(2.5), 2.5)  # in domain
        self.assertEqual(self.c.y_at_x(6.7), 3.3)
        assert np.isnan(self.c.y_at_x(12))  # outside domain, right
        # test existing points in Curve
        self.assertEqual(self.c.y_at_x(0), 0)
        self.assertEqual(self.c.y_at_x(0.0), 0)
        self.assertEqual(self.c.y_at_x(-0.0), 0)  # 'negative zero'
        self.assertEqual(self.c.y_at_x(5), 5)
        self.assertEqual(self.c.y_at_x(10), 0)

    def test_change_domain(self):
        # one point
        assert np.array_equal(self.c.change_domain([7]).y, [3])
        # two points
        assert np.array_equal(self.c.change_domain([3, 7]).y, [3, 3])
        # more than previously
        assert np.array_equal(self.c.change_domain([1, 2, 3, 4, 5, 6, 7, 8]).y,
                              [1, 2, 3, 4, 5, 4, 3, 2]
                              )
        with self.assertRaises(ValueError):
            # outside domain
            self.c.change_domain([12])
            self.c.change_domain([-12])

    def test_rebinned(self):
        new_c = self.c.rebinned(step=1)
        assert np.array_equal(new_c.x, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
        # todo: probably something wrong with fixp, they both throw ValueError
        # new_c = self.c.rebinned(step=2, fixp=15)
        # assert np.array_equal(new_c.x, [1, 3, 5, 7, 9])
        # new_c = self.c.rebinned(step=2, fixp=-5)
        # assert np.array_equal(new_c.x, [1, 3, 5, 7, 9])

    def test_evaluate_at_x(self):
        # test inside and outside domain
        assert np.array_equal(
            self.c.evaluate_at_x([-1, 0, 1, 10, 11], def_val=37),
            [37, 0, 1, 0, 37]
        )
        # test between existing points
        assert np.array_equal(
            self.c.evaluate_at_x([-0.333, 0.5, 0.7, 7.3], def_val=37),
            [37, 0.5, 0.7, 2.7]
        )

    def test_subtract(self):
        # c2 has wider domain than c1
        c1 = Curve([[-1, -1], [0, 0], [1, 5], [2, 0], [3, 0]])
        c2 = Curve([[-2, 1], [-1, 1], [0, 1], [1, 1], [2, 1], [3, 1], [4, 1]])

        # should return None and self
        self.assertIsNone(c1.subtract(c2, new_obj=False))
        assert np.array_equal(
            c1,
            [[-1, -2], [0, -1], [1, 4], [2, -1], [3, -1]]
        )
        # create new object and compare
        assert np.array_equal(
            c1.subtract(c2, new_obj=True),
            [[-1, -3], [0, -2], [1, 3], [2, -2], [3, -2]]
        )