Created
January 4, 2022 11:24
-
-
Save mdennehy/b1baf09d43a9dbc679fde0e8ac943cb8 to your computer and use it in GitHub Desktop.
quick n dirty hack to visualise covid19 data
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| import argparse | |
| import traceback | |
| import logging | |
| import pprint as pp | |
| import os | |
| import tqdm | |
| import time | |
| import datetime | |
| import pandas as pd | |
| import numpy as np | |
| import matplotlib | |
| import matplotlib.pyplot as plt | |
| import matplotlib.dates as mdates | |
| from mpl_toolkits.mplot3d.axes3d import Axes3D | |
| from mpl_toolkits.mplot3d import proj3d | |
| import seaborn as sns | |
| from scipy.interpolate import griddata | |
| from arcgis.gis import GIS | |
| from arcgis.features import FeatureLayer | |
| from arcgis.features import FeatureLayerCollection | |
| ################################################################################ | |
| # Main script # | |
| ################################################################################ | |
| if __name__ == "__main__": | |
| try: | |
| parser = argparse.ArgumentParser() | |
| logfilename, _ext = os.path.splitext(os.path.basename(__file__)) | |
| parser.add_argument('-l', '--logfile', help="Log file", default="%s.log" % logfilename) | |
| parser.add_argument('-L', '--loglevel', help="Log level", default="INFO") | |
| args = parser.parse_args() | |
| tqdm_disable = None | |
| logging.basicConfig(filename=args.logfile, | |
| filemode="w", | |
| datefmt="%H:%M:%S", | |
| format='[%(levelname)s] %(asctime)s (%(relativeCreated)d) {%(process)s|%(threadName)s} : %(message)s', | |
| level=args.loglevel) | |
| except Exception as e: | |
| print("Setup exception!") | |
| pp.pprint(e) | |
| traceback.print_exc() | |
| traceback.print_stack() | |
| logging.exception("Setup exception!") | |
| logging.exception(pp.pformat(e)) | |
| logging.exception(traceback.format_exc()) | |
| logging.exception(traceback.format_stack()) | |
| gis = GIS() | |
| content_cases = gis.content.search('title: CovidStatisticsProfileHPSCIrelandOpenData','Feature Layer')[0] | |
| content_countycases = gis.content.search('title: COVID-19 HPSC County Statistics Historic Data','Feature Layer')[0] | |
| content_lab= gis.content.search('title: COVID-19 Laboratory Testing Time Series','Feature Layer')[0] | |
| content_hospital= gis.content.search('title: COVID-19 SDU Acute Hospital Time Series Summary','Feature Layer')[0] | |
| fl_cases = FeatureLayerCollection(content_cases['url']).layers[0] | |
| fl_lab = FeatureLayerCollection(content_lab['url']).layers[0] | |
| fl_hospital = FeatureLayerCollection(content_hospital['url']).layers[0] | |
| query = fl_cases.query(out_fields='Date,HospitalisedAged5,HospitalisedAged5to14,HospitalisedAged15to24,HospitalisedAged25to34,HospitalisedAged35to44,HospitalisedAged45to54,HospitalisedAged55to64,HospitalisedAged65to74,HospitalisedAged75to84,HospitalisedAged85up,Aged1to4,Aged5to14,Aged15to24,Aged25to34,Aged35to44,Aged45to54,Aged55to64,Aged65to74,Aged75to84,Aged85up') | |
| data_date = [] | |
| data_cases = [] | |
| data = [] | |
| for feature in query.features: | |
| data.append([pd.to_datetime(feature.attributes['Date'], unit='ms'), | |
| # feature.attributes['HospitalisedAged5'], | |
| # feature.attributes['HospitalisedAged5to14'], | |
| # feature.attributes['HospitalisedAged15to24'], | |
| # feature.attributes['HospitalisedAged25to34'], | |
| # feature.attributes['HospitalisedAged35to44'], | |
| # feature.attributes['HospitalisedAged45to54'], | |
| # feature.attributes['HospitalisedAged55to64'], | |
| # feature.attributes['HospitalisedAged65to74'], | |
| # feature.attributes['HospitalisedAged75to84'], | |
| # feature.attributes['HospitalisedAged85up'], | |
| feature.attributes['Aged1to4'], | |
| feature.attributes['Aged5to14'], | |
| feature.attributes['Aged15to24'], | |
| feature.attributes['Aged25to34'], | |
| feature.attributes['Aged35to44'], | |
| feature.attributes['Aged45to54'], | |
| feature.attributes['Aged55to64'], | |
| feature.attributes['Aged65to74'], | |
| feature.attributes['Aged75to84'], | |
| feature.attributes['Aged85up'], | |
| ]) | |
| #data_date.append(pd.to_datetime(feature.attributes['Date'], unit='ms')) | |
| #data_cases.append(feature.attributes['SUM_number_of_new_covid_19_cases_co']) | |
| df = pd.DataFrame(data, columns=['date', | |
| # 'HospitalisedAged5', | |
| # 'HospitalisedAged5to14', | |
| # 'HospitalisedAged15to24', | |
| # 'HospitalisedAged25to34', | |
| # 'HospitalisedAged35to44', | |
| # 'HospitalisedAged45to54', | |
| # 'HospitalisedAged55to64', | |
| # 'HospitalisedAged65to74', | |
| # 'HospitalisedAged75to84', | |
| # 'HospitalisedAged85up', | |
| 'Aged1to4', | |
| 'Aged5to14', | |
| 'Aged15to24', | |
| 'Aged25to34', | |
| 'Aged35to44', | |
| 'Aged45to54', | |
| 'Aged55to64', | |
| 'Aged65to74', | |
| 'Aged75to84', | |
| 'Aged85up']) | |
| df.set_index('date', inplace=True) | |
| df = df.diff() | |
| x = np.arange(len(df.columns)) | |
| y = df.index.astype(np.int64) | |
| y = y / 1000000000 | |
| y = y / 86400 | |
| X,Y = np.meshgrid(x,y,sparse=True) | |
| Z = df | |
| fig = plt.figure(tight_layout=True) | |
| ax = fig.add_subplot(111, projection='3d') | |
| fig.set_figheight(10) | |
| fig.set_figwidth(15) | |
| x_scale=1 | |
| y_scale=2 | |
| z_scale=1 | |
| scale=np.diag([x_scale, y_scale, z_scale, 1.0]) | |
| scale=scale*(1.0/scale.max()) | |
| scale[3,3]=1.0 | |
| def short_proj(): | |
| return np.dot(Axes3D.get_proj(ax), scale) | |
| ax.get_proj=short_proj | |
| ax.set_zlabel('Cases') | |
| ax.xaxis.set_major_locator(matplotlib.ticker.MultipleLocator(1)) | |
| ax.xaxis.set_minor_locator(matplotlib.ticker.MultipleLocator(1)) | |
| rev_labels=np.flip(df.columns.values) | |
| ax.set_xticklabels(df.columns.values) | |
| ax.tick_params(axis='x', which='major', pad=15) | |
| ax.yaxis.set_major_locator(mdates.MonthLocator(bymonth=(1))) | |
| ax.yaxis.set_minor_locator(mdates.MonthLocator()) | |
| ax.yaxis.set_major_formatter(mdates.DateFormatter('%b %Y')) | |
| ax.yaxis.set_minor_formatter(mdates.DateFormatter('%b')) | |
| ax.yaxis.set_tick_params(rotation=50) | |
| #fig.colorbar(surf, shrink=1, aspect=10) | |
| fig.tight_layout(pad=0.0) | |
| fig.subplots_adjust(left=0, right=1, bottom=0, top=1) | |
| surf = ax.plot_surface(X,Y,Z, | |
| cmap=plt.cm.plasma, | |
| linewidth=1, | |
| rstride=1, | |
| cstride=1, | |
| # rcount=len(df.columns), | |
| # ccount=len(df.index), | |
| antialiased=False) | |
| ax.view_init(20, -20) | |
| ax.dist = 9 | |
| #plt.show() | |
| plt.savefig("surface.png") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment