Skip to content

Python script to plot time series of data related to COVID19 pandemic. This repository also includes a Python-independent executable (Windows 7+) version of the script.

Notifications You must be signed in to change notification settings

matiasleoni/COVID19_plotter

Repository files navigation

Covid 19 Plotter

Author: Matías Leoni [email protected]

Table of Contents

  1. Brief description
  2. Script and executable
  3. Compile the source to executable
  4. Pyinstaller vs. Matplotlib

Brief description

This Python script allows the user to plot different time-series data related to COVID19 pandemic.

On a per country basis and for many countries at the same time one can plot:

  • Total cases
  • New cases
  • Total deaths
  • New deaths
  • Total cases per million
  • New cases per million
  • Total deaths per million
  • New deaths per million
  • Total tests
  • New tests
  • Total tests per thousand
  • New tests per thousand
  • Tests per case
  • Positivity rate
  • Test units
  • Stringency index

A moving average option can be set to smooth daily data. The plot is made by processing the current database generated by the people of "Our World in data" which is updated daily. The current data is downloaded during the first run of the script and after that one can refresh the data on each new run.

Packages used: csv, requests, datetime, os, pandas, matplotlib and PIL

Credit for the dataset: "Our World in data" Hasell, J., Mathieu, E., Beltekian, D. et al. A cross-country database of COVID-19 testing. Sci Data 7, 345 (2020). https://doi.org/10.1038/s41597-020-00688-8

Script and executable

Script

The script is mainly written in the file Covid19Plotter.py and only calls the module data.py with structural data of the csv files downloaded from "Our World in Data". It requires the python packages requests, pandas, matplotlib, numpy and pillow. The latest version of numpy is crashing with matplotlib and therefore one should run the script with numpy 1.19.3. You can find out which version of numpy your system has installed (if it exists) with the command

> python -m pip show numpy

in the command prompt. If you need to downgrade (or to fresh install) numpy to 1.19.3 version just run

> python -m pip install numpy==1.19.3

Executable

Inside the folder "dist" of this repository you will find the folder "Covid19Plotter" which is an executable version of the script which should work in Windows 7+. Downloading that folder and running the executable Covid19Plotter.exe gives you the ability to use the script without having a Python installation.

Compile the source to executable

You can reconstruct the executable version of the script taking the following steps:

1. starting files

You only need the files

  1. Covid19Plotter.py
  2. data.py
  3. requirements.txt

from this repository. Create a folder (let us call it Covid19plotter) in your system and place those files in it.

2. Create a virtual environment

Open a command line terminal and place yourself in Covid19Plotter folder. Run the command

> python -m venv .

This will create a virtual environment in this directory (you should see that some files and folders were created inside Covid19plotter).

Next, activate the environment with

> Scripts\activate.bat

You should see that your path has changed to:

(Covid19Plotter) C:\......>

This means that you are now working in the virtual environment and that any python command you run (including the packages you install) will run in this environment and not in your main python installation.

3. Install packages

Install the required packages with pip with the command

> python -m pip install -r requirements.txt

This command installs the packages listed in the file requirements.txt. This file is a pure text file which you can easily inspect with notepad. As you can see, all the packages listed in that file are at their latest version (this has been written on November 9th 2020) except matplotlib (version 3.2.2 required) and numpy (version 1.19.3) required. The necessity of numpy's old version is explained in this readme file under the title "Script and executable". The necessity, on the other hand, of running with an outdated version of matplotlib has to do with the fact that pyinstaller, the package which will allow us to construct an executable version of the script, is crashing with the latest version of matplotlib.

4. Test the script and create an executable

Still with the virtual environment activated you should test if the script is running correctly. For this run

> python Covid19Plotter.py

and go through the input/output layout of the script to plot your data of interest. If everything works smoothly, it is time to create the executable. Run

> pyinstaller Covid19Plotter.py

After this, inside a folder called "dist" you will find your executable program with it's executable .exe file and all its libraries. Deactivate the virtual environment with

> deactivate

Pyinstaller vs. Matplotlib

I have noticed all over the usual places, for example here:

https://stackoverflow.com/questions/64683076/after-i-convert-my-script-to-executable-file-it-gives-this-error-matplotlibde https://stackoverflow.com/questions/64586129/why-am-i-getting-this-error-when-running-a-python-executable

or here:

mislav/hub#2530 brentvollebregt/auto-py-to-exe#124

that many people have been having problems with combining pyinstaller with scripts using matplotlib. As suggested in many places the problem can be worked out by downgrading matplotlib to the 3.2.2 version. More recently the latest (as of 2020-11-08) version of numpy, 1.19.4 is also conflicting with matplotlib and this is why we have explained the need for numpy 1.19.3 in this script.

Moreover, even if it is true that downgrading matplotlib seems to be a good solution (at least temporarily) to have compatibility with pyinstaller, it turns out that scripts that run and print messages in the terminal will keep posting annoying WARNINGS by matplotlib such as:

The MATPLOTLIBDATA environment variable was deprecated in Matplotlib 3.1 and will be removed in 3.3. exec(bytecode, module.__dict__)

The way around this annoying warnings is to insert the following two lines:

import warnings
warnings.filterwarnings("ignore", "(?s).*MATPLOTLIBDATA.*", category=UserWarning)

before everytime you import matplotlib in your python script.

Besides its utility for plotting COVID19 data, this script can also help people work around all the version problems (and annoying warnings) related to the combination of numpy, matplotlib and pyinstaller.

About

Python script to plot time series of data related to COVID19 pandemic. This repository also includes a Python-independent executable (Windows 7+) version of the script.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published