Example python3: pop0e – simple population program

eJournal: uffmm.org,
ISSN 2567-6458, 4-6.April 2019
Email: info@uffmm.org
Author: Gerd Doeben-Henisch
Email: gerd@doeben-henisch.de

CONTEXT

This is a possible 4th step in the overall topic ‘Co-Learning python3′. After downloading WinPython and activating the integrated editor ‘spyder’ (see here),  one can edit another simple program dealing with population dynamics in a most simple way (see the source code below under the title ‘EXAMPLE: pop0e.py’). This program is a continuation of the program pop0d.py, which has been described here.

COMMENTS

In this post I comment only on the changes between the actual program and the version before.

IMPORTS

In the new version one more liibrary is used for the handling of time stamps:

import time # Lib for time

STORING DATA IN A FILE

The only extension in the new version of the small program are some lines enabling the storage of the data from the simulation in a file.

data = np.column_stack((x,pop))

This line is formating the plot-values as x and x axes written as two columns bedides each other in a file.

What comes next is a construction of a file name which includes the actual time as well as the values of the br and the dr variable:

ts = time.gmtime()
t=time.strftime(“%c”, ts) # format time data into ISO format
t=t.replace(‘ ‘,’-‘)
t=t.replace(‘:’,’-‘)
header=’br=’+str(br)+’,’+’dr=’+str(dr)+’\n’+’X-Column, Y-Column’
fname=’hxyPTL’+t+’br=’+str(br)+’-‘+’dr=’+str(dr)

After this construction a file is generated with the plotting data as well as an expressive name.

np.savetxt(fname+’.txt’, data,header=header)

SOURCE CODE

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
“””
Created on Thu 4-6 April Konga (Sweden) 2019

@author: gerd doeben-henisch
Email: gerd@doeben-henisch.de
“””

##################################
# pop0e()
###################################
#
# IDEA
#
# Simple program to compute the increase/ decrease of a population with
# the parameters population number (p), birth-rate (br) and death-rate (dr).
# In this version an extension with the following features:
# – a loop to repeat the computation for n-many cycles
# – a storage of the data in an array
# – an additional automatic storage of the plotting data in a file
# with the actual time in the file-name
# – a plot of the stored data for n-many cycles
# – the overall change of the population in %

#########################################################
# IMPORTS
# As part of the distribution of Winpython there are already many
# libraries pre-installed, which can be activated by the import command.
# Other libraries outside of the distribution have to be downloaded
# with the pip command
# Lib for plotting

import matplotlib.pyplot as plt # Lib for plotting
import numpy as np # Lib for math
import time # Lib for time

###########################################################
# DEFINITION

def pop0(p,br,dr):

p=p+(p*br)-(p*dr)

return p

###################################
# INPUT OF DATA

p = int(input(‘Population number ? ‘))

br = float(input(‘Birthrate in % ? ‘))
br = br/100

dr = float(input(‘Deathrate in % ? ‘))
dr = dr/100

n = int(input(‘How many cycles ? ‘))

baseYear = int(input(‘What is your Base Year ? ‘))

#############################################
# GLOBAL VARIABLES

pop = [] # storage for the pop-numbers for plotting
pop.append(p)

#################################################
# COMPUTE

for i in range(n):
p=pop0(p,br,dr)
pop.append(p)

##################################################
# SHOW RESULTS

for i in range(n+1):
print(‘Year %5d = Citizens. %8d \n’ %(baseYear+i, pop[i]) )

x = np.linspace(1,len(pop),len(pop))

plt.plot(x, pop, ‘bo’)
plt.show()

#####################################################
# STORE VALUES ON DISK
#
# For this see the online article
# https://www.pythonforthelab.com/blog/introduction-to-storing-data-in-files/
#
# Saves the plot data automatically in a file with a header and two columns

data = np.column_stack((x,pop))
ts = time.gmtime()
t=time.strftime(“%c”, ts) # format time data into ISO format
t=t.replace(‘ ‘,’-‘)
t=t.replace(‘:’,’-‘)
header=’br=’+str(br)+’,’+’dr=’+str(dr)+’\n’+’X-Column, Y-Column’
fname=’hxyPTL’+t+’br=’+str(br)+’-‘+’dr=’+str(dr)
np.savetxt(fname+’.txt’, data,header=header)

###########################################
# Compute Change of POP

n1=pop[0]
n2=pop[len(pop)-1]
Increase=(n2-n1)/(n1/100)

print(“From Year %5d, until Year %5d, a change of %2.2f percent \n” % (baseYear, baseYear+n,Increase) )

plt.close()

####################################################
# REAL DATA
#
# UN Demographic Yearbook 2017
# https://unstats.un.org/unsd/demographic-social/products/dyb/dybsets/2017.pdf
#
# Basic Tables UN
# https://unstats.un.org/unsd/demographic-social/products/vitstats/seratab1.pdf
#
# UN public tables
# http://data.un.org/Explorer.aspx?d=POP
#
# UN Rate of population change
# http://data.un.org/Data.aspx?d=PopDiv&f=variableID%3a47
# https://www.un.org/en/development/desa/population/index.asp
”’
Population number ? 6958169

Birthrate in % ? 1.9

Deathrate in % ? 0.77

How many cycles ? 15

What is your Base Year ? 2010
Year 2010 = Citizens. 6958169

Year 2011 = Citizens. 7036796

Year 2012 = Citizens. 7116312

Year 2013 = Citizens. 7196726

Year 2014 = Citizens. 7278049

Year 2015 = Citizens. 7360291

Year 2016 = Citizens. 7443462

Year 2017 = Citizens. 7527573

Year 2018 = Citizens. 7612635

Year 2019 = Citizens. 7698658

Year 2020 = Citizens. 7785653

Year 2021 = Citizens. 7873630

Year 2022 = Citizens. 7962602

Year 2023 = Citizens. 8052580

Year 2024 = Citizens. 8143574

Year 2025 = Citizens. 8235596

From Year 2010, until Year 2025, a change of 18.36 percent

Real UN data for 2010 – 2015
2010 2011 2012 2013 2014 2015
6 958 169 7 043 009 7 128 177 7 213 426 7 298 453 7.383.009

########################################################
# STORING DATA
#
# https://www.pythonforthelab.com/blog/introduction-to-storing-data-in-files/
#
# Example of saved file:
file name:
hxyPTLSun-Apr–7-08-55-46-2019br=0.019-dr=0.0077.txt

# br=0.019,dr=0.0077
# X-Column, Y-Column
1.000000000000000000e+00 6.958169000000000000e+06
2.000000000000000000e+00 7.036796309700000100e+06
3.000000000000000000e+00 7.116312107999609783e+06
4.000000000000000000e+00 7.196726434820005670e+06
5.000000000000000000e+00 7.278049443533471785e+06
6.000000000000000000e+00 7.360291402245399542e+06
7.000000000000000000e+00 7.443462695090772584e+06
8.000000000000000000e+00 7.527573823545298539e+06
9.000000000000000000e+00 7.612635407751359977e+06
1.000000000000000000e+01 7.698658187858950347e+06
1.100000000000000000e+01 7.785653025381756946e+06
1.200000000000000000e+01 7.873630904568570666e+06
1.300000000000000000e+01 7.962602933790194802e+06
1.400000000000000000e+01 8.052580346942024305e+06
1.500000000000000000e+01 8.143574504862469621e+06
1.600000000000000000e+01 8.235596896767416038e+06
”’