Source code for cowidev.testing.batch.saudi_arabia
importpandasaspdfromcowidev.utils.webimportrequest_jsonfromcowidev.utilsimportclean_date_series,clean_countfromcowidev.testingimportCountryTestBase# from cowidev.testing.utils import make_monotonic
[docs]classSaudiArabia(CountryTestBase):location:str="Saudi Arabia"units:str="tests performed"source_label:str="Ministry of Health"source_url_ref:str="https://covid19.moh.gov.sa/"source_url:str="https://services6.arcgis.com/bKYAIlQgwHslVRaK/arcgis/rest/services/DailyTestPerformance_ViewLayer/FeatureServer/0/query"rename_columns:dict={"attributes.ReportDate":"Date","attributes.DailyTest":"Daily change in cumulative total",# "attributes.CumulativeTest": "Cumulative total",}params:dict={"f":"json","where":"ReportDate>'2020-01-01 00:00:00'","returnGeometry":False,"spatialRel":"esriSpatialRelIntersects","outFields":"ReportDate,DailyTest",# CumulativeTest,"orderByFields":"ReportDate asc","resultOffset":0,"resultRecordCount":32000,"resultType":"standard",}
[docs]defread(self)->pd.DataFrame:"""Reads data from source."""data=request_json(self.source_url,params=self.params)df=pd.json_normalize(data,record_path=["features"])returndf
[docs]defpipe_date(self,df:pd.DataFrame)->pd.DataFrame:"""Cleans date column"""returndf.assign(Date=clean_date_series(df["Date"],unit="ms"))
[docs]defpipe_metrics(self,df:pd.DataFrame):"""Pipes metrics"""returndf.assign(**{"Daily change in cumulative total":df["Daily change in cumulative total"].apply(clean_count),# "Cumulative total": df["Cumulative total"].apply(clean_count),})
[docs]defpipeline(self,df:pd.DataFrame)->pd.DataFrame:"""pipeline for data"""return(df.pipe(self.pipe_rename_columns).pipe(self.pipe_date).pipe(self.pipe_metrics).pipe(self.pipe_metadata)# .pipe(make_monotonic).sort_values("Date")# .drop_duplicates(subset=["Cumulative total"], keep="first"))
[docs]defexport(self):"""Exports data to csv"""df=self.read().pipe(self.pipeline)self.export_datafile(df)