Exemple #1
0
    def test_snpp_extrapolate(self):
        # test extrapolation within range just returns the data
        years = range(self.snpp.min_year(utils.EN), self.snpp.min_year(utils.EN) + 2)
        ext = self.snpp.extrapolate(self.npp, "E06000001", years)
        act = self.snpp.filter("E06000001", years)
        self.assertTrue(ext.equals(act))

        # test extrapolagg is equivalent to extrapolate + external agg
        years = range(self.snpp.max_year(utils.EN) - 1, self.snpp.max_year(utils.EN) + 2)
        ext = utils.aggregate(self.snpp.extrapolate(self.npp, "E06000001", years), ["GENDER", "C_AGE"])
        extagg = self.snpp.extrapolagg(["GENDER", "C_AGE"], self.npp, "E06000001", years)
        self.assertTrue(ext.equals(extagg))

        # test works for multiple LADs
        extagg = self.snpp.extrapolagg(["GENDER", "C_AGE"], self.npp,
                                       ["E06000001", "E06000005", "E06000047", "S12000033", "S12000041"], years)
        self.assertTrue(np.array_equal(extagg.PROJECTED_YEAR_NAME.unique(), years))
        self.assertTrue(np.array_equal(extagg.GEOGRAPHY_CODE.unique(),
                                       ["E06000001", "E06000005", "E06000047", "S12000033", "S12000041"]))

        # check for non-contiguous extrapolation-only range
        years = [2029, 2035]
        extagg = self.snpp.extrapolagg(["GENDER", "C_AGE"], self.npp,
                                       ["E06000001", "E06000005", "E06000047", "S12000033", "S12000041"], years)
        self.assertTrue(np.array_equal(extagg.PROJECTED_YEAR_NAME.unique(), years))
        self.assertTrue(np.array_equal(extagg.GEOGRAPHY_CODE.unique(),
                                       ["E06000001", "E06000005", "E06000047", "S12000033", "S12000041"]))
Exemple #2
0
    def test_snpp_custom_projection(self):
        customdata = pd.read_csv(os.path.join(TEST_DATA_DIR, "custom_snpp.csv"))
        CustomSNPPData.register_custom_projection("test_custom_snpp", customdata, TEST_DATA_DIR)

        projs = CustomSNPPData.list_custom_projections(TEST_DATA_DIR)
        self.assertEqual(len(projs), 1)
        self.assertEqual(projs[0], "test_custom_snpp")

        custom = CustomSNPPData.CustomSNPPData("test_custom_snpp", TEST_DATA_DIR)
        geogs = np.array(
            ['E06000001', 'E06000005', 'E06000047', 'S12000033', 'S12000034', 'S12000041', 'W06000011', 'W06000016',
             'W06000018'])
        self.assertTrue(np.array_equal(sorted(custom.all_lads()), geogs))
        self.assertTrue(np.array_equal(sorted(custom.data.GEOGRAPHY_CODE.unique()), geogs))

        data = custom.filter(["E06000001", "W06000018"], range(2018, 2020))
        self.assertEqual(len(data), 728)  # 91(ages) * 2(genders) * 2(LADs) * 2(years)
        self.assertAlmostEqual(data[data.PROJECTED_YEAR_NAME == 2018].OBS_VALUE.sum(), 274097.537766, 5)

        agg = custom.aggregate(["GENDER", "C_AGE"], ["S12000033", "S12000041"], [2018], range(0, 18), 1)
        self.assertEqual(len(agg), 2)
        self.assertAlmostEqual(agg.OBS_VALUE.sum(), 30760.0, 5)  # remember this is population under 46

        # test extrapolagg is equivalent to extrapolate + external agg
        years = range(custom.max_year() - 1, custom.max_year() + 2)
        ext = utils.aggregate(custom.extrapolate(self.npp, "E06000001", years), ["GENDER", "C_AGE"])
        extagg = custom.extrapolagg(["GENDER", "C_AGE"], self.npp, "E06000001", years)
        self.assertTrue(ext.equals(extagg))
        self.assertEqual(len(ext.GEOGRAPHY_CODE.unique()), 1)
        self.assertEqual(ext.GEOGRAPHY_CODE.unique()[0], "E06000001")
        self.assertAlmostEqual(ext.OBS_VALUE.sum(), 279805.48680742574, 5)
Exemple #3
0
    def test_snpp_extrapolate(self):
        # test extrapolation within range just returns the data
        years = range(self.snpp.min_year(utils.EN),
                      self.snpp.min_year(utils.EN) + 2)
        ext = self.snpp.extrapolate(self.npp, "E06000001", years)
        act = self.snpp.filter("E06000001", years)
        self.assertTrue(ext.equals(act))

        # test extrapolagg is equivalent to extrapolate + external agg
        years = range(
            self.snpp.max_year(utils.EN) - 1,
            self.snpp.max_year(utils.EN) + 2)
        ext = utils.aggregate(
            self.snpp.extrapolate(self.npp, "E06000001", years),
            ["GENDER", "C_AGE"])
        extagg = self.snpp.extrapolagg(["GENDER", "C_AGE"], self.npp,
                                       "E06000001", years)
        self.assertTrue(ext.equals(extagg))
# initialise the population modules
npp = NPPData.NPPData()
snpp = SNPPData.SNPPData()

lad = "E08000021"  # Newcastle

start_year = 2016
end_year = snpp.max_year(lad)

# get the total projected population for ppp up to the SNPP horizon (2039)
ppp = snpp.aggregate(["GENDER", "C_AGE"], lad, range(start_year, end_year + 1))

# calculate the the variants
hhh = snpp.create_variant("hhh", npp, lad, range(start_year, end_year + 1))
lll = snpp.create_variant("lll", npp, lad, range(start_year, end_year + 1))

# aggregate the calculated variants by age and gender
hhh = utils.aggregate(hhh, ["GENDER", "C_AGE"])
lll = utils.aggregate(lll, ["GENDER", "C_AGE"])

# plot the data
plt.plot(ppp.PROJECTED_YEAR_NAME, ppp.OBS_VALUE, "bo", label="ppp")
plt.plot(hhh.PROJECTED_YEAR_NAME, hhh.OBS_VALUE, "ro", label="hhh")
plt.plot(lll.PROJECTED_YEAR_NAME, lll.OBS_VALUE, "go", label="lll")
plt.xlabel("Year")
plt.ylabel("Persons")
plt.title(lad + " Population Projection Variants")
plt.legend()
plt.show()