Ejemplo n.º 1
0
    def test_gen_cities_avg(self):
        # test for just one city
        climate = ps5.Climate('data.csv')
        test_years = pylab.array(ps5.TESTING_INTERVAL)
        result = ps5.gen_cities_avg(climate, ['SEATTLE'], test_years)
        correct = [11.514383561643836, 10.586849315068493, 11.28319672, 12.10643836, 12.82917808, 13.13178082]
        self.assertTrue(len(correct) == len(result), "Expected length %s, was length %s" % (len(correct), len(result)))

        for index in range(len(correct)):
            good_enough = math.isclose(correct[index], result[index])
            self.assertTrue(good_enough, "City averages do not match expected results")

        # multiple cities
        result = ps5.gen_cities_avg(climate, ps5.CITIES, test_years)
        correct = [16.75950424, 16.85749511, 17.56180068, 16.65717547, 16.84499022, 17.54460535]
        self.assertTrue(len(correct) == len(result), "Expected length %s, was length %s" % (len(correct), len(result)))

        for index in range(len(correct)):
            good_enough = math.isclose(correct[index], result[index])
            self.assertTrue(good_enough, "City averages do not match expected results")

        # years range
        # multiple cities
        result = ps5.gen_cities_avg(climate, ['TAMPA', 'DALLAS'], test_years)
        correct = [20.8040411, 22.03910959, 22.27206284, 21.31136986, 20.88123288, 22.07794521]
        self.assertTrue(len(correct) == len(result), "Expected length %s, was length %s" % (len(correct), len(result)))

        for index in range(len(correct)):
            good_enough = math.isclose(correct[index], result[index])
            self.assertTrue(good_enough, "City averages do not match expected results")
Ejemplo n.º 2
0
    def test_gen_cities_avg(self):
        # test for just one city
        climate = ps5.Dataset('data.csv')
        test_years = np.array(ps5.TESTING_INTERVAL)
        result = ps5.gen_cities_avg(climate, ['SEATTLE'], test_years)
        correct = [
            10.58684932, 11.28319672, 12.10643836, 12.82917808, 13.13178082,
            12.50054645
        ]
        self.assertTrue(
            len(correct) == len(result),
            "Expected length %s, was length %s" % (len(correct), len(result)))

        for index in range(len(correct)):
            good_enough = math.isclose(correct[index], result[index])
            self.assertTrue(good_enough,
                            "City averages do not match expected results")

        # national avg check (all cities)
        result = ps5.gen_cities_avg(climate, ps5.CITIES, test_years)
        correct = [
            16.46957462, 17.17387834, 16.25620043, 16.47222062, 17.17817592,
            17.19825999
        ]
        self.assertTrue(
            len(correct) == len(result),
            "Expected length %s, was length %s" % (len(correct), len(result)))

        for index in range(len(correct)):
            good_enough = math.isclose(correct[index], result[index])
            self.assertTrue(good_enough,
                            "City averages do not match expected results")

        # two-city check
        result = ps5.gen_cities_avg(climate, ['TAMPA', 'DALLAS'], test_years)
        correct = [
            22.03910959, 22.27206284, 21.31136986, 20.88123288, 22.07794521,
            22.18155738
        ]
        self.assertTrue(
            len(correct) == len(result),
            "Expected length %s, was length %s" % (len(correct), len(result)))

        for index in range(len(correct)):
            good_enough = math.isclose(correct[index], result[index])
            self.assertTrue(good_enough,
                            "City averages do not match expected results")
Ejemplo n.º 3
0
    def test_find_interval(self):
        # Test 1: Existing positive and negative slope intervals on city data
        temp = ps5.Dataset('data.csv')
        test_years = np.array(range(1961, 2016))
        yearly_temps = ps5.gen_cities_avg(temp, ['PORTLAND'], test_years)

        result_neg = ps5.find_interval(test_years, yearly_temps, 20, False)
        correct_start = 31
        correct_end = 51
        self.assertIsNotNone(result_neg,
                             "Returned None, but valid interval exists.")
        self.assertEqual(correct_start, result_neg[0], "Start year incorrect")
        self.assertEqual(correct_end, result_neg[1], "End year incorrect")

        result_pos = ps5.find_interval(test_years, yearly_temps, 20, True)
        correct_start = 15
        correct_end = 35
        self.assertIsNotNone(result_pos,
                             "Returned None, but valid interval exists.")
        self.assertEqual(correct_start, result_pos[0], "Start year incorrect")
        self.assertEqual(correct_end, result_pos[1], "End year incorrect")

        # Test 2: y = 2x
        x = np.array(range(50))
        y = np.array(range(0, 100, 2))
        result_pos = ps5.find_interval(x, y, len(x) // 2, True)
        result_neg = ps5.find_interval(x, y, len(x) // 2, False)
        pos_correct_start = len(x) // 2
        pos_correct_end = len(x)

        self.assertIsNone(result_neg,
                          "Returned an interval, but should be None.")

        self.assertIsNotNone(result_pos,
                             "Returned None, but valid interval exists.")
        self.assertEqual(pos_correct_start, result_pos[0],
                         "Start year incorrect")
        self.assertEqual(pos_correct_end, result_pos[1], "End year incorrect")

        # Test 3: y = -2x
        x = np.array(range(50))
        y = np.array(range(100, 0, -2))
        result_pos = ps5.find_interval(x, y, len(x) // 2, True)
        result_neg = ps5.find_interval(x, y, len(x) // 2, False)
        neg_correct_start = len(x) // 2
        neg_correct_end = len(x)

        self.assertIsNone(result_pos,
                          "Returned an interval, but should be None.")

        self.assertIsNotNone(result_neg,
                             "Returned None, but valid interval exists.")
        self.assertEqual(neg_correct_start, result_neg[0],
                         "Start year incorrect")
        self.assertEqual(neg_correct_end, result_neg[1], "End year incorrect")