Skip to content
Snippets Groups Projects
Commit 51cc937c authored by Chandrasekhar Ramakrishnan's avatar Chandrasekhar Ramakrishnan
Browse files

refactor: moved code into helper.py

parent 8c02a0db
No related branches found
No related tags found
No related merge requests found
Showing
with 857 additions and 379 deletions
source diff could not be displayed: it is too large. Options to address this: view the blob.
......@@ -15,6 +15,7 @@ Click==7.0
colorama==0.4.3
conda==4.7.12
conda-package-handling==1.6.0
-e src/covid-19/covid_19_dashboard
cryptography==2.8
decorator==4.4.1
defusedxml==0.6.0
......
# http://editorconfig.org
root = true
[*]
indent_style = space
indent_size = 4
trim_trailing_whitespace = true
insert_final_newline = true
charset = utf-8
end_of_line = lf
[*.bat]
indent_style = tab
end_of_line = crlf
[LICENSE]
insert_final_newline = false
[Makefile]
indent_style = tab
* COVID-19 Dashboard version:
* Python version:
* Operating System:
### Description
Describe what you were trying to get done.
Tell us what happened, what went wrong, and what you expected to happen.
### What I Did
```
Paste the command(s) you ran and the output.
If there was a crash, please include the traceback here.
```
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
# C extensions
*.so
# Distribution / packaging
.Python
env/
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
*.egg-info/
.installed.cfg
*.egg
# 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/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
.hypothesis/
.pytest_cache/
# Translations
*.mo
*.pot
# Django stuff:
*.log
local_settings.py
# Flask stuff:
instance/
.webassets-cache
# Scrapy stuff:
.scrapy
# Sphinx documentation
docs/_build/
# PyBuilder
target/
# Jupyter Notebook
.ipynb_checkpoints
# pyenv
.python-version
# celery beat schedule file
celerybeat-schedule
# SageMath parsed files
*.sage.py
# dotenv
.env
# virtualenv
.venv
venv/
ENV/
# Spyder project settings
.spyderproject
.spyproject
# Rope project settings
.ropeproject
# mkdocs documentation
/site
# mypy
.mypy_cache/
# IDE settings
.vscode/
\ No newline at end of file
# Config file for automatic testing at travis-ci.com
language: python
python:
- 3.8
- 3.7
- 3.6
- 3.5
# Command to install dependencies, e.g. pip install -r requirements.txt --use-mirrors
install: pip install -U tox-travis
# Command to run tests, e.g. python setup.py test
script: tox
.. highlight:: shell
============
Contributing
============
Contributions are welcome, and they are greatly appreciated! Every little bit
helps, and credit will always be given.
You can contribute in many ways:
Types of Contributions
----------------------
Report Bugs
~~~~~~~~~~~
Report bugs at https://github.com/ciyer/covid_19_dashboard/issues.
If you are reporting a bug, please include:
* Your operating system name and version.
* Any details about your local setup that might be helpful in troubleshooting.
* Detailed steps to reproduce the bug.
Fix Bugs
~~~~~~~~
Look through the GitHub issues for bugs. Anything tagged with "bug" and "help
wanted" is open to whoever wants to implement it.
Implement Features
~~~~~~~~~~~~~~~~~~
Look through the GitHub issues for features. Anything tagged with "enhancement"
and "help wanted" is open to whoever wants to implement it.
Write Documentation
~~~~~~~~~~~~~~~~~~~
COVID-19 Dashboard could always use more documentation, whether as part of the
official COVID-19 Dashboard docs, in docstrings, or even on the web in blog posts,
articles, and such.
Submit Feedback
~~~~~~~~~~~~~~~
The best way to send feedback is to file an issue at https://github.com/ciyer/covid_19_dashboard/issues.
If you are proposing a feature:
* Explain in detail how it would work.
* Keep the scope as narrow as possible, to make it easier to implement.
* Remember that this is a volunteer-driven project, and that contributions
are welcome :)
Get Started!
------------
Ready to contribute? Here's how to set up `covid_19_dashboard` for local development.
1. Fork the `covid_19_dashboard` repo on GitHub.
2. Clone your fork locally::
$ git clone git@github.com:your_name_here/covid_19_dashboard.git
3. Install your local copy into a virtualenv. Assuming you have virtualenvwrapper installed, this is how you set up your fork for local development::
$ mkvirtualenv covid_19_dashboard
$ cd covid_19_dashboard/
$ python setup.py develop
4. Create a branch for local development::
$ git checkout -b name-of-your-bugfix-or-feature
Now you can make your changes locally.
5. When you're done making changes, check that your changes pass flake8 and the
tests, including testing other Python versions with tox::
$ flake8 covid_19_dashboard tests
$ python setup.py test or pytest
$ tox
To get flake8 and tox, just pip install them into your virtualenv.
6. Commit your changes and push your branch to GitHub::
$ git add .
$ git commit -m "Your detailed description of your changes."
$ git push origin name-of-your-bugfix-or-feature
7. Submit a pull request through the GitHub website.
Pull Request Guidelines
-----------------------
Before you submit a pull request, check that it meets these guidelines:
1. The pull request should include tests.
2. If the pull request adds functionality, the docs should be updated. Put
your new functionality into a function with a docstring, and add the
feature to the list in README.rst.
3. The pull request should work for Python 3.5, 3.6, 3.7 and 3.8, and for PyPy. Check
https://travis-ci.com/ciyer/covid_19_dashboard/pull_requests
and make sure that the tests pass for all supported Python versions.
Tips
----
To run a subset of tests::
$ python -m unittest tests.test_covid_19_dashboard
Deploying
---------
A reminder for the maintainers on how to deploy.
Make sure all your changes are committed (including an entry in HISTORY.rst).
Then run::
$ bump2version patch # possible: major / minor / patch
$ git push
$ git push --tags
Travis will then deploy to PyPI if tests pass.
=======
History
=======
0.1.0 (2020-03-15)
------------------
* First release on PyPI.
Apache Software License 2.0
Copyright (c) 2020, Chandrasekhar Ramakrishnan
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
include CONTRIBUTING.rst
include HISTORY.rst
include LICENSE
include README.rst
recursive-include tests *
recursive-exclude * __pycache__
recursive-exclude * *.py[co]
recursive-include docs *.rst conf.py Makefile make.bat *.jpg *.png *.gif
.PHONY: clean clean-test clean-pyc clean-build docs help
.DEFAULT_GOAL := help
define BROWSER_PYSCRIPT
import os, webbrowser, sys
from urllib.request import pathname2url
webbrowser.open("file://" + pathname2url(os.path.abspath(sys.argv[1])))
endef
export BROWSER_PYSCRIPT
define PRINT_HELP_PYSCRIPT
import re, sys
for line in sys.stdin:
match = re.match(r'^([a-zA-Z_-]+):.*?## (.*)$$', line)
if match:
target, help = match.groups()
print("%-20s %s" % (target, help))
endef
export PRINT_HELP_PYSCRIPT
BROWSER := python -c "$$BROWSER_PYSCRIPT"
help:
@python -c "$$PRINT_HELP_PYSCRIPT" < $(MAKEFILE_LIST)
clean: clean-build clean-pyc clean-test ## remove all build, test, coverage and Python artifacts
clean-build: ## remove build artifacts
rm -fr build/
rm -fr dist/
rm -fr .eggs/
find . -name '*.egg-info' -exec rm -fr {} +
find . -name '*.egg' -exec rm -f {} +
clean-pyc: ## remove Python file artifacts
find . -name '*.pyc' -exec rm -f {} +
find . -name '*.pyo' -exec rm -f {} +
find . -name '*~' -exec rm -f {} +
find . -name '__pycache__' -exec rm -fr {} +
clean-test: ## remove test and coverage artifacts
rm -fr .tox/
rm -f .coverage
rm -fr htmlcov/
rm -fr .pytest_cache
lint: ## check style with flake8
flake8 covid_19_dashboard tests
test: ## run tests quickly with the default Python
python setup.py test
test-all: ## run tests on every Python version with tox
tox
coverage: ## check code coverage quickly with the default Python
coverage run --source covid_19_dashboard setup.py test
coverage report -m
coverage html
$(BROWSER) htmlcov/index.html
docs: ## generate Sphinx HTML documentation, including API docs
rm -f docs/covid_19_dashboard.rst
rm -f docs/modules.rst
sphinx-apidoc -o docs/ covid_19_dashboard
$(MAKE) -C docs clean
$(MAKE) -C docs html
$(BROWSER) docs/_build/html/index.html
servedocs: docs ## compile the docs watching for changes
watchmedo shell-command -p '*.rst' -c '$(MAKE) -C docs html' -R -D .
release: dist ## package and upload a release
twine upload dist/*
dist: clean ## builds source and wheel package
python setup.py sdist
python setup.py bdist_wheel
ls -l dist
install: clean ## install the package to the active Python's site-packages
python setup.py install
==================
COVID-19 Dashboard
==================
.. image:: https://img.shields.io/pypi/v/covid_19_dashboard.svg
:target: https://pypi.python.org/pypi/covid_19_dashboard
.. image:: https://img.shields.io/travis/ciyer/covid_19_dashboard.svg
:target: https://travis-ci.com/ciyer/covid_19_dashboard
.. image:: https://readthedocs.org/projects/covid-19-dashboard/badge/?version=latest
:target: https://covid-19-dashboard.readthedocs.io/en/latest/?badge=latest
:alt: Documentation Status
Helper functions for plotting covid-19 data
* Free software: Apache Software License 2.0
* Documentation: https://covid-19-dashboard.readthedocs.io.
Features
--------
* TODO
Credits
-------
This package was created with Cookiecutter_ and the `audreyr/cookiecutter-pypackage`_ project template.
.. _Cookiecutter: https://github.com/audreyr/cookiecutter
.. _`audreyr/cookiecutter-pypackage`: https://github.com/audreyr/cookiecutter-pypackage
"""Top-level package for COVID-19 Dashboard."""
__author__ = """Chandrasekhar Ramakrishnan"""
__email__ = 'cramakri@ethz.ch'
__version__ = '0.1.0'
from .helper import *
\ No newline at end of file
"""Main module.
Helper functions for the dashboard"""
import altair as alt
from vega_datasets import data
import pandas as pd
import os
def read_jhu_covid_df(ts_folder, name):
filename = os.path.join(ts_folder, f"time_series_19-covid-{name}.csv")
df = pd.read_csv(filename)
df = df.set_index(['Province/State', 'Country/Region', 'Lat', 'Long'])
df.columns = pd.to_datetime(df.columns)
return df
def read_jhu_frames_map(ts_folder):
jhu_frames_map = {
"confirmed": read_jhu_covid_df(ts_folder, "Confirmed"),
"deaths": read_jhu_covid_df(ts_folder, "Deaths"),
"recovered": read_jhu_covid_df(ts_folder, "Recovered")
}
return jhu_frames_map
def read_rates_covid_df(rates_folder, name):
filename = os.path.join(rates_folder, f"ts_rates_19-covid-{name}.csv")
df = pd.read_csv(filename).drop("Unnamed: 0", axis=1)
df = df.set_index(['Country/Region'])
df.columns = pd.to_datetime(df.columns)
return df
def read_rates_frames_map(rates_folder):
rates_frames_map = {
"confirmed": read_rates_covid_df(rates_folder, "confirmed"),
"deaths": read_rates_covid_df(rates_folder, "deaths"),
"recovered": read_rates_covid_df(rates_folder, "recovered")
}
return rates_frames_map
def read_geodata(geodata_path):
geodata_df = pd.read_csv(geodata_path)
geodata_df = geodata_df.drop('Unnamed: 0', axis=1)
geodata_df = geodata_df.rename({'name_jhu':'Country/Region'}, axis=1)
geodata_df = geodata_df.set_index('Country/Region')
return geodata_df
def latest_jhu_country_ser(jhu_frames_map, name):
return jhu_frames_map[name].iloc[:,-1].groupby(level='Country/Region').sum()
def countries_with_number_of_cases(jhu_frames_map, name, count):
case_count_ser = latest_jhu_country_ser(jhu_frames_map, 'confirmed')
countries_over_thresh = case_count_ser[case_count_ser > count - 1].index
return countries_over_thresh
def latest_rates_ser(rates_frames_map, name):
return rates_frames_map[name].iloc[:,-1]
def compute_map_df(rates_frames_map, jhu_frames_map, geodata_df, countries_over_thresh):
map_df = pd.concat([
latest_rates_ser(rates_frames_map, 'confirmed'),
latest_rates_ser(rates_frames_map, 'deaths'),
latest_rates_ser(rates_frames_map, 'recovered')], axis=1)
nominal_df = pd.concat([
latest_jhu_country_ser(jhu_frames_map, 'confirmed'),
latest_jhu_country_ser(jhu_frames_map, 'deaths'),
latest_jhu_country_ser(jhu_frames_map, 'recovered')], axis=1)
map_df = pd.concat([map_df, nominal_df, geodata_df[['Longitude', 'Latitude']]], axis=1)
# Restrict to countries with 100 or more cases
map_df = map_df.loc[countries_over_thresh].dropna()
map_df = map_df.reset_index()
map_df.columns = ['Country/Region',
'Confirmed/100k', 'Deaths/100k', 'Recovered/100k',
'Confirmed', 'Deaths', 'Recovered',
'Long', 'Lat']
return map_df
def map_of_variable(map_df, variable, title):
# Data generators for the background
sphere = alt.sphere()
graticule = alt.graticule()
# Source of land data
source = alt.topo_feature(data.world_110m.url, 'countries')
# Layering and configuring the components
p = alt.layer(
alt.Chart(sphere).mark_geoshape(fill='#cae6ef'),
alt.Chart(graticule).mark_geoshape(stroke='white', strokeWidth=0.5),
alt.Chart(source).mark_geoshape(fill='#dddddd', stroke='#aaaaaa'),
alt.Chart(map_df).mark_circle(opacity=0.6).encode(
longitude='Long:Q',
latitude='Lat:Q',
size=alt.Size(f'{variable}:Q', title="Cases"),
color=alt.value('steelblue'),
tooltip=["Country/Region:N",
"Confirmed:Q", "Deaths:Q", "Recovered:Q",
"Confirmed/100k:Q", "Deaths/100k:Q", "Recovered/100k:Q"]
)
).project(
'naturalEarth1'
).properties(width=600, height=400, title=f"{title} cases per 100k inhabitants"
).configure_view(stroke=None)
return p
def growth_df(rates_frames_map, geodata_df, name, countries_over_thresh):
latest_confirmed_ser = rates_frames_map['confirmed'].iloc[:,-1]
countries_over_1 = latest_confirmed_ser[latest_confirmed_ser >= 2].reset_index()['Country/Region']
confirmed_rate_df = rates_frames_map['confirmed']
confirmed_rate_df = confirmed_rate_df.loc[
confirmed_rate_df.index.isin(countries_over_1) &
confirmed_rate_df.index.isin(countries_over_thresh)]
confirmed_rate_df = confirmed_rate_df.join(
geodata_df[['Longitude', 'Latitude', 'region_un']]).set_index(
['Longitude', 'Latitude', 'region_un'], append=True)
confirmed_rate_df = confirmed_rate_df.stack().reset_index()
confirmed_rate_df = confirmed_rate_df.rename(
{'region_un': 'Geo Region', 'level_4': 'Date', 0: 'Confirmed/100k'}, axis=1)
return confirmed_rate_df
# Minimal makefile for Sphinx documentation
#
# You can set these variables from the command line.
SPHINXOPTS =
SPHINXBUILD = python -msphinx
SPHINXPROJ = covid_19_dashboard
SOURCEDIR = .
BUILDDIR = _build
# Put it first so that "make" without argument is like "make help".
help:
@$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
.PHONY: help Makefile
# Catch-all target: route all unknown targets to Sphinx using the new
# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS).
%: Makefile
@$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
#!/usr/bin/env python
#
# covid_19_dashboard documentation build configuration file, created by
# sphinx-quickstart on Fri Jun 9 13:47:02 2017.
#
# This file is execfile()d with the current directory set to its
# containing dir.
#
# Note that not all possible configuration values are present in this
# autogenerated file.
#
# All configuration values have a default; values that are commented out
# serve to show the default.
# If extensions (or modules to document with autodoc) are in another
# directory, add these directories to sys.path here. If the directory is
# relative to the documentation root, use os.path.abspath to make it
# absolute, like shown here.
#
import os
import sys
sys.path.insert(0, os.path.abspath('..'))
import covid_19_dashboard
# -- General configuration ---------------------------------------------
# If your documentation needs a minimal Sphinx version, state it here.
#
# needs_sphinx = '1.0'
# Add any Sphinx extension module names here, as strings. They can be
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom ones.
extensions = ['sphinx.ext.autodoc', 'sphinx.ext.viewcode']
# Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates']
# The suffix(es) of source filenames.
# You can specify multiple suffix as a list of string:
#
# source_suffix = ['.rst', '.md']
source_suffix = '.rst'
# The master toctree document.
master_doc = 'index'
# General information about the project.
project = 'COVID-19 Dashboard'
copyright = "2020, Chandrasekhar Ramakrishnan"
author = "Chandrasekhar Ramakrishnan"
# The version info for the project you're documenting, acts as replacement
# for |version| and |release|, also used in various other places throughout
# the built documents.
#
# The short X.Y version.
version = covid_19_dashboard.__version__
# The full version, including alpha/beta/rc tags.
release = covid_19_dashboard.__version__
# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
#
# This is also used if you do content translation via gettext catalogs.
# Usually you set "language" from the command line for these cases.
language = None
# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
# This patterns also effect to html_static_path and html_extra_path
exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store']
# The name of the Pygments (syntax highlighting) style to use.
pygments_style = 'sphinx'
# If true, `todo` and `todoList` produce output, else they produce nothing.
todo_include_todos = False
# -- Options for HTML output -------------------------------------------
# The theme to use for HTML and HTML Help pages. See the documentation for
# a list of builtin themes.
#
html_theme = 'alabaster'
# Theme options are theme-specific and customize the look and feel of a
# theme further. For a list of options available for each theme, see the
# documentation.
#
# html_theme_options = {}
# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
html_static_path = ['_static']
# -- Options for HTMLHelp output ---------------------------------------
# Output file base name for HTML help builder.
htmlhelp_basename = 'covid_19_dashboarddoc'
# -- Options for LaTeX output ------------------------------------------
latex_elements = {
# The paper size ('letterpaper' or 'a4paper').
#
# 'papersize': 'letterpaper',
# The font size ('10pt', '11pt' or '12pt').
#
# 'pointsize': '10pt',
# Additional stuff for the LaTeX preamble.
#
# 'preamble': '',
# Latex figure (float) alignment
#
# 'figure_align': 'htbp',
}
# Grouping the document tree into LaTeX files. List of tuples
# (source start file, target name, title, author, documentclass
# [howto, manual, or own class]).
latex_documents = [
(master_doc, 'covid_19_dashboard.tex',
'COVID-19 Dashboard Documentation',
'Chandrasekhar Ramakrishnan', 'manual'),
]
# -- Options for manual page output ------------------------------------
# One entry per manual page. List of tuples
# (source start file, name, description, authors, manual section).
man_pages = [
(master_doc, 'covid_19_dashboard',
'COVID-19 Dashboard Documentation',
[author], 1)
]
# -- Options for Texinfo output ----------------------------------------
# Grouping the document tree into Texinfo files. List of tuples
# (source start file, target name, title, author,
# dir menu entry, description, category)
texinfo_documents = [
(master_doc, 'covid_19_dashboard',
'COVID-19 Dashboard Documentation',
author,
'covid_19_dashboard',
'One line description of project.',
'Miscellaneous'),
]
.. include:: ../CONTRIBUTING.rst
.. include:: ../HISTORY.rst
Welcome to COVID-19 Dashboard's documentation!
======================================
.. toctree::
:maxdepth: 2
:caption: Contents:
readme
installation
usage
modules
contributing
history
Indices and tables
==================
* :ref:`genindex`
* :ref:`modindex`
* :ref:`search`
.. highlight:: shell
============
Installation
============
Stable release
--------------
To install COVID-19 Dashboard, run this command in your terminal:
.. code-block:: console
$ pip install covid_19_dashboard
This is the preferred method to install COVID-19 Dashboard, as it will always install the most recent stable release.
If you don't have `pip`_ installed, this `Python installation guide`_ can guide
you through the process.
.. _pip: https://pip.pypa.io
.. _Python installation guide: http://docs.python-guide.org/en/latest/starting/installation/
From sources
------------
The sources for COVID-19 Dashboard can be downloaded from the `Github repo`_.
You can either clone the public repository:
.. code-block:: console
$ git clone git://github.com/ciyer/covid_19_dashboard
Or download the `tarball`_:
.. code-block:: console
$ curl -OJL https://github.com/ciyer/covid_19_dashboard/tarball/master
Once you have a copy of the source, you can install it with:
.. code-block:: console
$ python setup.py install
.. _Github repo: https://github.com/ciyer/covid_19_dashboard
.. _tarball: https://github.com/ciyer/covid_19_dashboard/tarball/master
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment