Skip to content

Commit

Permalink
Scripts for data extraction from mobility report
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Hodge committed Apr 4, 2020
0 parents commit 7ace388
Show file tree
Hide file tree
Showing 19 changed files with 962 additions and 0 deletions.
158 changes: 158 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
# Data/plots
*.csv
*.png
*.svg
*.pdf

# Work in progress
notebooks/

# Mac
.DS_Store

# PyCharm
.idea/

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
pip-wheel-metadata/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
.pybuilder/
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# pytype static type analyzer
.pytype/

# Cython debug symbols
cython_debug/

# static files generated from Django application using `collectstatic`
media
static
18 changes: 18 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
Copyright 2020 Office for National Statistics

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
112 changes: 112 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
# Mobility Report graph extractor

<p align="center">
<img src="/meta/logo.png" alt="Logo">
</p>

For extracting graphs from COVID-19 Community Mobility Report PDF into
comma separated value (CSV) files for each graph.

Scripts `getgraphs.py` and `createcsvs.py` are able to extract all graphs from a Scalable Vector Graphics (SVG) document (converted as a single document from the original PDF) in one go, including those with gaps in the data.

**Developed and tested with:**
- MacOS 10.15.4
- PyCharm Community Edition 2018.3.2
- Affinity Designer 1.6.5
- Python 3

This code is developed at *speed* on the COVID-19 Community Mobility Report PDF documents published on Friday 3rd of April 2020. Changes may be made.

## Installation

We provide the python `requirements.txt` file as well as a `poetry` setup for
dependency management.

We recommend using a virtual environment before installing dependencies.

To install with `pip`:

```shell
pip install -r requirements.txt
```

To install with `poetry`

```shell
poetry install
```

## Usage

1. Either create, or download a pre-made, SVG document file (this file is the
entire PDF document in Scalable Vector Graphics (SVG) format).

**Create**:

1. Load in PDF document to Affinity Designer.
1. Click Load all pages.
1. `File > Export > SVG (for print)`
1. Select Area: Whole Document.
1. Save the SVG file to (`./svgs`).

**Download**:

1. The Data Science Campus has created [SVGs for all G20 countries](https://drive.google.com/open?id=1n5XXYbJtf-wNWEkDPe7N8iNfjzJXwZ_d) (no PDFs available for China or Russia).
1. Save the SVG file to (`./svgs`).

2. **Run the `getgraphs.py` Python script.**

```text
Usage: getgraphs.py [OPTIONS] INPUT_LOCATION OUTPUT_FOLDER
Options:
-m, --multiple If the input location is a folder with multiple SVGs,
this has to be set to true.
-f, --folder TEXT If provided will overwrite the output folder name (can
not be used with the `--multiple` flag)
--help Show this message and exit.
```

Specify either the location of the single SVG file (e.g. `./svgs/UK`) you with to process or the folder of svg files (you will have to provide the `-m` flag in that case). You will also have to provide the output folder, e.g. `./graphs/UK`.

3. **Run the `createcsvs.py` Python script.**

```text
Usage: createcsvs.py [OPTIONS] INPUT_FOLDER OUTPUT_FOLDER DATES_FILE
Options:
-p, --plots Enables creation and saving of additional PNG plots
--help Show this message and exit.
```

Specify the input folder where the individual SVG files are (e.g. `./graphs/UK`),
the output folder where you want the CSV files to be saved to (e.g. `./output/UK`),
and the dates lookup file (e.g. `./config/dates_lookup.csv`) - used to convert
coordinates to dates.

If you want simple matplotlib PNG plots to save as well as CSV files, use the `-p` flag.

## Data format

Each CSV will be saved to (`./output/subfolder`), starting at `1.csv`. As of the **COVID-19 Community Mobility Reports** released on Friday 3rd April 2020, CSV files `1.csv` to `6.csv` relate to the country-level graphs in
the original PDF (pages one and two). Then each set of 6 CSV files (e.g., `7.csv` to `12.csv`) will relate to a regional area.

Each set of 6 files follows the order:

1. Retail & recreation
2. Grocery & pharmacy
3. Parks
4. Transit stations
5. Workplaces
6. Residential

## United Kingdom Dataset

A pre-made dataset for the United Kingdom can be found at the [Data Science Campus' Google Mobility Reports Data repository](https://github.com/datasciencecampus/google-mobility-reports-data).


## Contributing

Any suggestions or issues, please use the Issues template. We welcome
collaborators. To help us with this work, fork the repository and issue a Pull
Request when you have added a feature, or fixed a bug. Thanks!
Empty file added config/.gitkeep
Empty file.
1 change: 1 addition & 0 deletions config/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This file changes the raw vertices (0-43) to real dates. These are related to the COVID-19 Community Mobility Reports released on Friday 3rd April 2020 and should be updated for future releases.
44 changes: 44 additions & 0 deletions config/dates_lookup.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
index,date
1,2020-02-16
2,2020-02-17
3,2020-02-18
4,2020-02-19
5,2020-02-20
6,2020-02-21
7,2020-02-22
8,2020-02-23
9,2020-02-24
10,2020-02-25
11,2020-02-26
12,2020-02-27
13,2020-02-28
14,2020-02-29
15,2020-03-01
16,2020-03-02
17,2020-03-03
18,2020-03-04
19,2020-03-05
20,2020-03-06
21,2020-03-07
22,2020-03-08
23,2020-03-09
24,2020-03-10
25,2020-03-11
26,2020-03-12
27,2020-03-13
28,2020-03-14
29,2020-03-15
30,2020-03-16
31,2020-03-17
32,2020-03-18
33,2020-03-19
34,2020-03-20
35,2020-03-21
36,2020-03-22
37,2020-03-23
38,2020-03-24
39,2020-03-25
40,2020-03-26
41,2020-03-27
42,2020-03-28
43,2020-03-29
Loading

0 comments on commit 7ace388

Please sign in to comment.