def __init__(self, modelweek=None, epiweek=None): from epiweeks import Week, Year self.refew = Week(1970, 1) self.refmodelweek = 0 if modelweek is None and epiweek is None: raise self.NoData("Please enter either a model week or an epiweek") elif modelweek is not None and epiweek is not None: raise self.Toomuchdata( "Please ejust one of a model week or an epiweek") elif modelweek is None: self.epiweek = epiweek if type(epiweek) is str: self.epiWeek = Week(int(epiweek[:4]), int(epiweek[4:])) elif type(epiweek) is int: epiweek = str(epiweek) self.epiWeek = Week(int(epiweek[:4]), int(epiweek[4:])) self.fromEpiWeek2ModelWeek() elif epiweek is None: self.modelweek = modelweek self.fromModelWeek2EpiWeek() self.epiWeek = Week(int(str(self.epiweek)[:4]), int(str(self.epiweek)[4:])) self.year = self.epiWeek.year self.week = self.epiWeek.week self.toFrom40Week()
def get_newunit(value): if value[0].isdecimal(): date = pd.to_datetime(value) if unit == 'week': epiweek = str(Week.fromdate(date, system="cdc")) # get epiweeks year, week = epiweek[:4], epiweek[-2:] if weekasdate in ['start', 'end']: if weekasdate == 'start': epiweek = str(Week(int(year), int(week)).startdate()) else: epiweek = str(Week(int(year), int(week)).enddate()) else: epiweek = year + '_' + 'EW' + week if epiweek not in time_cols: time_cols.append(epiweek) return epiweek elif unit == 'month': year_month = date.strftime("%Y-%m") if year_month not in time_cols: time_cols.append(year_month) return year_month elif unit == 'year': year = date.strftime("%Y") if year not in time_cols: time_cols.append(year) return year elif unit == 'full': return 'total' else: if unit == 'full': return 'total' else: return value
def week_value_to_week(value: int) -> Week: year, week = value // 100, value % 100 if year < date.min.year: return Week(date.min.year, 1) if year > date.max.year - 1: return Week( date.max.year - 1, 1) # minus 1 since internally it does some checks with a year + 1 return Week(year=year, week=week)
def addLag(obs): EW = str(obs.EW) lag = int(obs.lag) yr,wk = int(EW[:4]),int(EW[4:]) surveillanceWeek = Week(yr,wk) + lag obs['surveillanceWeek'] = "{:d}{:d}".format(surveillanceWeek.year,surveillanceWeek.week) return obs
def get_coverage(dashboard_signal: DashboardSignal, metadata) -> List[DashboardSignalCoverage]: """Get the most recent coverage for the signal.""" count_by_geo_type_df = pd.read_csv( COVERAGE_URL.format(source=dashboard_signal.source, signal=dashboard_signal.covidcast_signal)) try: count_by_geo_type_df["time_value"] = count_by_geo_type_df[ "time_value"].apply( lambda x: pd.to_datetime(str(x), format="%Y%m%d")) except: count_by_geo_type_df["time_value"] = count_by_geo_type_df[ "time_value"].apply( lambda x: pd.to_datetime(Week(x // 100, x % 100).startdate())) signal_coverage_list = [] for _, row in count_by_geo_type_df.iterrows(): signal_coverage = DashboardSignalCoverage( signal_id=dashboard_signal.db_id, date=row['time_value'].date(), geo_type='county', count=row['count']) signal_coverage_list.append(signal_coverage) return signal_coverage_list
def parse_cities_request(self, response): cities = json.loads(response.body) today = date_utils.today() current_week = Week.fromdate(today) # We have to do different passes for 2019 and 2020, since the specific days of # the epidemiological week differs. # # The api seems to return the data from the current year as "2020", and the previous as "2019", # so we'll exploit that to extract the data only from the "2020" chart for city in cities: for year in [2020, 2019]: for weeknum in range(1, current_week.week): ep_week = Week(year, weeknum) # Cache more than 4 weeks ago should_cache = (current_week.week - weeknum) > 4 yield self.make_registral_request( city=city, ep_week=ep_week, callback=self.parse_registral_request, dont_cache=not should_cache, )
def computeTargetILIepiWeek(row): forecastWeek = int(row.surveillanceWeek) weekAhead = int(row.Target.replace(' wk ahead', '')) iliYear, iliWeek = int(str(forecastWeek)[:3 + 1]), int( str(forecastWeek)[4:]) iliEW = Week(iliYear, iliWeek) + int(weekAhead) iliEW = int("{:04d}{:02d}".format(iliEW.year, iliEW.week)) row['targetWeek'] = iliEW return row
def year_week(y, w): try: if w not in ['', np.nan, None] and y not in ['', np.nan, None]: week = Week(int(y), int(w)) date = week.startdate() + timedelta(3) return date else: return '' except: return ''
def __init__(self, record_id, country, introduction, acctrans, metadata): #phylotype): self.id = record_id self.introduction = introduction #self.phylotype = phylotype self.country = country self.acctrans = acctrans if metadata[0] == "None": self.date = "NA" else: self.date = metadata[0] self.adm2 = metadata[2] self.global_lineage = metadata[3] epiweek_prep = metadata[1] if epiweek_prep != "0" and epiweek_prep != "": self.epiweek = Week(2020, int(float(epiweek_prep))) elif epiweek_prep == "0": self.epiweek = Week(2019, 52) elif epiweek_prep == "": self.epiweek = "NA" self.unclear = False if "/" in self.date: print("ERROR DATE FORMAT INCORRECT") # self.date_dt = dateparser.parse(self.date, settings={"DATE_ORDER":'DMY'}).date() # date_bits = self.date.split("/") # date_bits = self.date.split("-") # self.date_dt = dateparser.parse(self.date).date() date_bits = self.date.split("-") if len(date_bits) == 3: self.date_dt = dt.date(int(date_bits[0]), int(date_bits[1]), int(date_bits[2])) else: self.date_dt = "NA"
def grabForecastWeeks(): from epiweeks import Week, Year thisWeek = Week.thisweek() weeks = [Week(2019, 40)] while weeks[-1] < thisWeek: weeks.append(weeks[-1] + 1) formattedWeeks = [ int("{:04d}{:02d}".format(x.year, x.week)) for x in weeks ] return formattedWeeks
def __init__(self, record_id, lineage, metadata): self.id = record_id self.lineage = lineage self.country = metadata[0] self.date = metadata[1] epiweek_prep = metadata[2] if epiweek_prep != "": if float(epiweek_prep) != 0.0: self.epiweek = Week(2020, int(float(epiweek_prep))) else: self.epiweek = Week(2019, 52) else: self.epiweek = "NA" self.get_date_loc() self.travel_history = False
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 toFrom40Week(self): """ A variable from40 is created as the number of weeks from the current year or, if epiweek's week is less than 40, the past year's epidemic week number 40 to epiweek. """ yr, wk = self.year, self.week if wk > 40: self.from40 = wk - 40 else: _ref40week = Week(yr - 1, 40) _from40 = 0 while _ref40week < self.epiWeek: _ref40week += 1 _from40 += 1 self.from40 = _from40
def __init__(self, record_id, country, lineage, metadata,pillar, sub_date): self.id = record_id self.lineage = lineage self.country = country self.pillar2 = pillar if metadata[0] == "None": self.date = "NA" else: date = metadata[0] self.adm2 = metadata[2] self.global_lineage = metadata[3] self.sequencing_centre = metadata[4] if metadata[1] != "": epiweek = int(metadata[1]) if epiweek > 0 and epiweek < 54: self.epiweek = Week(2020, epiweek) elif epiweek >= 53: self.epiweek = Week(2021, epiweek - 53) elif epiweek == 0: self.epiweek = Week(2019, 52) else: self.epiweek = "NA" if "/" in date: print("ERROR DATE FORMAT INCORRECT") try: self.date_dt = dt.datetime.strptime(date, "%Y-%m-%d").date() except: self.date_dt = "NA" try: self.sub_date = dt.datetime.strptime(sub_date, "%Y-%m-%d").date() except: self.sub_date = "NA"
def get_weekdates_range(self, year, week): firstdayofweek = Week(int(year), int(week)).startdate() lastdayofweek = firstdayofweek + timedelta(days=6.9) leap = self.get_leap(year) if leap != timedelta(0): firstdayofweek = firstdayofweek + timedelta(days=leap.days) lastdayofweek = lastdayofweek + timedelta(days=leap.days) else: firstdayofweek = firstdayofweek + timedelta(days=leap.days) lastdayofweek = lastdayofweek + timedelta(days=leap.days) return (firstdayofweek, lastdayofweek)
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 week_split_count(df): w = df[['CloseDate','ClosePrice']] w['week']=pd.to_datetime(w.CloseDate).apply(lambda x: '{}'.format(x.week)) w['year']=pd.to_datetime(w.CloseDate).apply(lambda x: '{}'.format(x.year)) ww = w[['week','year']] ww.week=ww.week.apply(int) ww.year=ww.year.apply(int) ww=ww[ww.week!=53] ww['enddate'] = ww.apply(lambda row: pd.to_datetime(Week(row.year, row.week, 'iso').enddate()),axis=1) w['ds'] = ww['enddate'] w=w[['ds','ClosePrice']] w.rename(columns={'ClosePrice':'y'},inplace=True) w=w.groupby('ds').count() w.reset_index(inplace=True) w['ds'] = w['ds'].apply(lambda x: pd.to_datetime(x)) return w
def _parse_datetimes(date_int: int, time_type: str, date_format: str = "%Y%m%d") -> Union[pd.Timestamp]: # annotating nan errors """Convert a date or epiweeks string into timestamp objects. Datetimes (length 8) are converted to their corresponding date, while epiweeks (length 6) are converted to the date of the start of the week. Returns nan otherwise Epiweeks use the CDC format. :param date_int: Int representation of date. :param date_format: String of the date format to parse. :returns: Timestamp. """ date_str = str(date_int) if time_type == "day": return pd.to_datetime(date_str, format=date_format) if time_type == "week": epiwk = Week(int(date_str[:4]), int(date_str[-2:])) return pd.to_datetime(epiwk.startdate()) return np.nan
def test__parse_datetimes(): assert covidcast._parse_datetimes("202001", "week") == pd.Timestamp( Week(2020, 1).startdate()) assert covidcast._parse_datetimes("20200205", "day") == pd.Timestamp("20200205") assert pd.isna(covidcast._parse_datetimes("2020", "test"))
def test_week_value_to_week(self): self.assertEqual(week_value_to_week(202021), Week(2020, 21)) self.assertEqual(week_value_to_week(202101), Week(2021, 1))
def _parse_date(self, week_num) -> Iterator: """parses the date from the week number.""" date = Week(2022, week_num, system="iso").enddate() return clean_date(date)
def transformFWeek2WeightWeek(forecastWeek): from epiweeks import Week year,week = int(str(forecastWeek)[:4]), int(str(forecastWeek)[4:]) week = Week(year,week)+2 return int("{:04d}{:02d}".format(week.year,week.week))
]] # checking the above assumption assert null_data_x.isna().all().all() non_null_data = data_x[~null_data] all_years_data.append(non_null_data) # combining all the years and deriving the epi week start date # and generating no cases per epi week x_data = pd.concat(all_years_data) x_data.epi_week = pd.to_numeric(x_data.epi_week, downcast='integer', errors='coerce') x_data['epi_week_start_date'] = x_data.apply( lambda x: pd.to_datetime(Week(x.year, x.epi_week).startdate()), axis=1) x_data['date'] = x_data.epi_week_start_date x_data['month'] = x_data.epi_week_start_date.dt.month x_data['day'] = x_data.epi_week_start_date.dt.day for a in ['epi_week_start_date', 'date']: x_data[a] = pd.to_datetime(x_data[a]) # and generating no cases per epi week x_data = x_data.sort_values(by=['country_or_subregion', 'year', 'epi_week']) x_data['previous_epi_week_cases'] = x_data.groupby( by=['country_or_subregion', 'year'])['total_dengue_cases'].shift(1) x_data['dengue_cases_per_epi_week'] = x_data[ 'total_dengue_cases'] - x_data.previous_epi_week_cases.fillna(0) x_data['previous_epi_week_deaths'] = x_data.groupby(
n_epochs = n_epochs n_batch = n_batch n_neurons = n_neurons data_path = data_path ind_og = pd.read_csv(data_path) # ind = data.set_index(["WEEK"]) region = "Region" ind = ind_og.loc[ind_og["REGION"] == "Region " + str(r)] ind_next = ind_og[0:300] from epiweeks import Week, Year date = [] for item, row in ind.iterrows(): week = Week(row["YEAR"], row["WEEK"]) # print(row["YEAR"], row["WEEK"], week.startdate()) date.append(week.startdate()) ind["DATE"] = date new_ind = ind[['DATE', '% WEIGHTED ILI']] # ,'WEEK']] new_ind.index = new_ind['DATE'] series = new_ind.drop(['DATE'], axis=1) all_32_forecasts = [] for r in range(1, 11): # date-time parsing function for loading the dataset def parser(x): return datetime.strptime('190' + x, '%Y-%m')
def test_week_to_time_value(self): self.assertEqual(week_to_time_value(Week(2021, 1)), 202101) self.assertEqual(week_to_time_value(Week(2020, 42)), 202042)
def computeTargetILIepiWeek(forecastWeek, weekAhead): iliYear, iliWeek = int(str(forecastWeek)[:3 + 1]), int( str(forecastWeek)[4:]) iliEW = Week(iliYear, iliWeek) + int(weekAhead) iliEW = int("{:04d}{:02d}".format(iliEW.year, iliEW.week)) return iliEW
def get_leap(self, year): date = self.dates[year] starting_day_of_year = Week(year, 1).startdate() # diff = starting_day_of_year - date diff = date - starting_day_of_year return diff