def weeks_in_range(week_range: Tuple[int, int]) -> int: start = week_value_to_week(week_range[0]) end = week_value_to_week(week_range[1]) acc = end.week - start.week # accumulate the number of weeks in the years between for y in range(start.year, end.year): year = Year(y) acc += year.totalweeks() return acc + 1 # same week should lead to 1 week that will be queried
def fromEpiWeek2ModelWeek(self): """ Convert epiweek to model week, defined as the number of epidemic weeks from 197001 """ week = self.epiWeek numWeeks = 0 w = self.refew while True: if w.year < week.year: numWeeks += Year(w.year).totalweeks w = Week(w.year + 1, 1) else: break while w < week: numWeeks += 1 w += 1 self.modelweek = numWeeks
def fromEpiWeek2ModelWeek(self, week): from epiweeks import Week, Year if type(week) is Week: pass elif type(week) is str: week = Week(int(week[:4]), int(week[4:])) elif type(week) is int: week = str(week) week = Week(int(week[:4]), int(week[4:])) numWeeks = 0 w = Week(1970, 1) while True: if w.year < week.year: numWeeks += Year(w.year).totalweeks w = Week(w.year + 1, 1) else: break while w < week: numWeeks += 1 w += 1 return numWeeks
def date_string_to_epi_week(date_string): """ parse a date string in YYYY-MM-DD format and return cumulative epi week which is cumulative total epidemiological weeks since 2019-12-22. Week beginning 2019-12-22 is week 0 """ try: date = datetime.strptime(date_string, '%Y-%m-%d').date() except: return "" # this is epi-week: week = Week.fromdate(date) if week.year < 2019 or (week.year == 2019 and week.week < 52): return "" elif week.year == 2019: return ("0") else: cum_epi_week = week.week + len( list( chain(*[[x for x in Year(y).iterweeks()] for y in range(2020, week.year)]))) return str(cum_epi_week)
import numpy as np import geopandas as gpd import pandas as pd import seaborn as sns from epiweeks import Week, Year file_mask = 'D:/Utilisateurs/guillaume/Documents/GitHub/InSIGHT-PHAC/Mask/' pr_in = 'K:/DATA/REANALYSES/ERA5/PR/' data = pr_in + 'ERA5_PR_*.nc4' ds = xr.open_mfdataset(data) ds = ds * 1000 # convert from meter to mm year_to_study = 2016 # compute epiweek for 2015 and 2016 df_epi = [] for week in Year(year_to_study).iterweeks(): day = week.week day_ini = week.startdate().strftime("%Y-%m-%d") day_end = week.enddate().strftime("%Y-%m-%d") data = {'Week': [day], 'datei': [day_ini], 'datef': [day_end]} df = pd.DataFrame(data) df_epi.append(pd.DataFrame(data).set_index('Week')) df_epi = pd.concat(df_epi, axis=0).sort_index() lat_bnd = [62, -70] lon_bnd = [230, 340] Imp_Lats = ds.sel( longitude=slice(*lon_bnd), latitude=slice(*lat_bnd),
def make_epiweeks(): weeks = list(Year(2020).iterweeks()) return weeks
def get_weeks(self, year): year = Year(year) return year.totalweeks()
# -*- coding: utf-8 -*- """ Created on Fri Sep 20 12:12:33 2019 @author: guillaume """ import pandas as pd from epiweeks import Week, Year df_epi = [] for week in Year(2015).iterweeks(): day = week.week day_ini = week.startdate().strftime("%Y-%m-%d") day_end = week.enddate().strftime("%Y-%m-%d") data = {'Week': [day], 'datei': [day_ini], 'datef': [day_end]} df = pd.DataFrame(data) df_epi.append(pd.DataFrame(data).set_index('Week')) df_epi = pd.concat(df_epi, axis=0).sort_index()
def make_epiweeks(): weeks = list(Year(2019).iterweeks()) weeks.extend(list(Year(2020).iterweeks())) return weeks