Skip to main content

A population projection engine

Project description

popro

https://img.shields.io/pypi/v/popro.svg https://github.com/aiboxlab-pne/popro/actions/workflows/python-app.yml/badge.svg Documentation Status

A population projection engine

Features

  • Calculates population projection segmented by age over the years
    • Methodology:
    • Overview:
      • Inputs:
        • Specific year census dataset (place, age, population)

        • Dataset of people born over the years (year, place, births)

        • Projected population dataset not segmented by age over the years (year, place, population)

      • Output:
        • Population projection segmented by age dataset (year, place, age, population)

        • Errors report on combination of “place, age, year” unable to forecast (year, place, age, error_msg)

Usage

First let’s generate Input CSV files to serve as a sample.

import csv

def write_csv(file_path, list_data):
   with open(file_path, 'w', encoding='UTF8', newline='') as f:
      writer = csv.writer(f)
      for line in list_data:
            writer.writerow(line)

data_birth = [['births', 'place', 'year'],
              [102,'ny',2011],
              [116,'ny',2012],
              [94,'ny',2013],
              [123,'ny',2014],
              [156,'ny',2015]]

data_census = [['age', 'population', 'place', 'year'],
               [0, 100,  'ny', 2010],
               [1, 110,  'ny', 2010],
               [2, 105,  'ny', 2010],
               [3, 102,  'ny', 2010]]

data_population = [['population', 'place', 'year'],
                   [2010, 'ny', 2010],
                   [2100, 'ny', 2011],
                   [2050, 'ny', 2012],
                   [2040, 'ny', 2013],
                   [2090, 'ny', 2014],
                   [1950, 'ny', 2015]]

write_csv(file_path='births.csv', list_data=data_birth)
write_csv(file_path='census.csv', list_data=data_census)
write_csv(file_path='population.csv', list_data=data_population)

Now let’s import the lib Popro and generate our projection engine.

from popro import popro

dict_input = {'path_census': 'census.csv', 'path_births': 'births.csv', 'path_population': 'population.csv', 'year_census': 2010}
engine = popro.Popro(dict_input)

We are ready! Let’s start by doing some punctual projections of year, age and place.

First we will try with an age and year whose birth of the group is prior to the census.

engine.project(year=2012, place='ny', age=3, verbose=True)
pop_ny_2010_age_1 * (pop_ny_2012 / pop_ny_2010)
110 * (2050 / 2010)

112.18905472636816

Now let’s find out the projection for a group born after the census.

engine.project(year=2015, place='ny', age=4, verbose=True)
birth_ny_year_2011 * (pop_ny_2015 / pop_ny_2011)
102 * (1950 / 2100)

94.71428571428572

Finally we will generate a report with all possible combinations of year, age and place.

engine.project_all()
[{'year': 2011, 'place': 'ny', 'age': 0, 'quantity': 102.0},
 {'year': 2011, 'place': 'ny', 'age': 1, 'quantity': 104.4776119402985},
 {'year': 2011, 'place': 'ny', 'age': 2, 'quantity': 114.92537313432835},
 {'year': 2011, 'place': 'ny', 'age': 3, 'quantity': 109.70149253731343},
 {'year': 2012, 'place': 'ny', 'age': 0, 'quantity': 116.0},
 {'year': 2012, 'place': 'ny', 'age': 1, 'quantity': 99.57142857142857},
 {'year': 2012, 'place': 'ny', 'age': 2, 'quantity': 101.99004975124377},
 {'year': 2012, 'place': 'ny', 'age': 3, 'quantity': 112.18905472636816},
 {'year': 2013, 'place': 'ny', 'age': 0, 'quantity': 94.0},
 {'year': 2013, 'place': 'ny', 'age': 1, 'quantity': 115.43414634146342},
 {'year': 2013, 'place': 'ny', 'age': 2, 'quantity': 99.08571428571429},
 {'year': 2013, 'place': 'ny', 'age': 3, 'quantity': 101.49253731343283},
 {'year': 2014, 'place': 'ny', 'age': 0, 'quantity': 123.0},
 {'year': 2014, 'place': 'ny', 'age': 1, 'quantity': 96.30392156862744},
 {'year': 2014, 'place': 'ny', 'age': 2, 'quantity': 118.26341463414634},
 {'year': 2014, 'place': 'ny', 'age': 3, 'quantity': 101.51428571428572},
 {'year': 2015, 'place': 'ny', 'age': 0, 'quantity': 156.0},
 {'year': 2015, 'place': 'ny', 'age': 1, 'quantity': 114.76076555023923},
 {'year': 2015, 'place': 'ny', 'age': 2, 'quantity': 89.8529411764706},
 {'year': 2015, 'place': 'ny', 'age': 3, 'quantity': 110.34146341463415}]

Cool, but it would be better to export to a CSV, wouldn’t it?

engine.project_all(output_report_projection_path='projection_report.csv')

Report generated!

CLI

It is also possible to make projections via command line. Let’s repeat the same projections:

$ popro -i path_census,census.csv -i path_births,births.csv -i path_population,population.csv -i year_census,2010 --year 2012 --place ny --age 3
112.18905472636816
$ popro -i path_census,census.csv -i path_births,births.csv -i path_population,population.csv -i year_census,2010 --year 2015 --place ny --age 4
94.71428571428572
$ popro -i path_census,census.csv -i path_births,births.csv -i path_population,population.csv -i year_census,2010 --output projection_report.csv

History

0.1.0 (2022-04-27)

  • First release on PyPI.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

popro-0.1.1.tar.gz (1.3 MB view details)

Uploaded Source

Built Distribution

popro-0.1.1-py2.py3-none-any.whl (10.2 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file popro-0.1.1.tar.gz.

File metadata

  • Download URL: popro-0.1.1.tar.gz
  • Upload date:
  • Size: 1.3 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.10.4

File hashes

Hashes for popro-0.1.1.tar.gz
Algorithm Hash digest
SHA256 09c60e93166aec5b53507da4b00951701572547c7b683c3c498896b61a46c6b4
MD5 cceacad19837f69bb4daa370313052e8
BLAKE2b-256 3e3439160806fbb13911d1c4a01f0134ac80eb528e5f354d255225e77f872486

See more details on using hashes here.

File details

Details for the file popro-0.1.1-py2.py3-none-any.whl.

File metadata

  • Download URL: popro-0.1.1-py2.py3-none-any.whl
  • Upload date:
  • Size: 10.2 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.10.4

File hashes

Hashes for popro-0.1.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 66bc49320ba528e7130e69f2826e5171e35e703e6a327f2afb7354d1ee61f27d
MD5 f82440d2c7bebb0125d084402ee3f203
BLAKE2b-256 61760a85e0b079baff32946ebcb901206b45af5cde4236304c098817ca9fb328

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page