def computeLogScores(forecastsAndILI, forecastWeek, iliEW): if forecastsAndILI.shape[0] == 0: return pd.DataFrame() from datetime import datetime calendarEW = Week.thisweek() dayOfWeek = datetime.today().weekday if dayOfWeek in {5, 6}: # Saturday or Sunday calendarWeek = "{:d}{:d}".format( calendarEW.year, calendarEW.week + 1) # should be referenced from the next week else: calendarWeek = "{:d}{:d}".format(calendarEW.year, calendarEW.week) subsetToProbabilities = forecastsAndILI.loc[ (forecastsAndILI.bin_start_incl <= forecastsAndILI.wili) & (forecastsAndILI.bin_end_notincl > forecastsAndILI.wili), :] subsetToProbabilities['logScore'] = np.log( [float(x) for x in subsetToProbabilities.value]) logScores = subsetToProbabilities.loc[:, [ 'model', 'location', 'target', 'region', 'lag', 'releaseEW', 'releaseDate', 'wili', 'logScore' ]] logScores[ 'surveillanceWeek'] = forecastWeek # this is the most recent week of data available logScores[ 'calendarWeek'] = calendarWeek # this is the present week in real-time logScores['targetWeek'] = iliEW # this is the target week of forecasting return logScores
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 generate_week_periods(open_future_periods, page_limit, begin_period, direction, direction_change): weeks_to_display = {} # When the user first visits the period screen the begin_period variable is empty. # Therefore, use the current week as default. week = Week.thisweek("iso") + open_future_periods # If begin_period variable has a date, use it to calculate the weeks to display. if begin_period != '': week = Week.fromdate(datetime.datetime.strptime(begin_period, '%Y-%m-%d'), 'iso') # This logic is to fix week discrepancy when a user clicks + and changes the direction and press - or vice versa if direction_change: if direction == '+': week += page_limit - 1 if direction == '-': week -= page_limit - 1 # We should not open future dates for data entry. The -1 is to prevent from opening this week. if direction == '+' and week + page_limit > Week.thisweek("iso") + open_future_periods: week = Week.thisweek("iso") + open_future_periods - page_limit - 1 rng = range(page_limit, 0, -1) if direction == '+' else range(page_limit) for key, i in enumerate(rng): w = week + i if direction == '+' else week - (i + 1) weeks_to_display[str(key + 1)] = { "period": w.isoformat(), "display": "W{} - {} - {}".format(w.weektuple()[1], w.startdate(), w.enddate()) } # Take the first week to calculate the beginning period in the next screen. if direction == '+' and i == page_limit: begin_period = str(w.enddate()) # Take the final week to calculate the beginning week in the next screen. if direction == '-' and i == page_limit - 1: begin_period = str(w.startdate()) return begin_period, weeks_to_display
def addTodaysEpiWeek(self): thisweek = Week.thisweek() yr,wek = thisweek.year,thisweek.week self.todaysEW = int("{:04d}{:02d}".format(yr,wek))
def fromToday2EpiWeek(): thisweek = Week.thisweek() yr,wek = thisweek.year,thisweek.week return int("{:04d}{:02d}".format(yr,wek))
def handle_coverage(): """ similar to /signal_dashboard_coverage for a specific signal returns the coverage (number of locations for a given geo_type) """ source_signal_pairs = parse_source_signal_pairs() daily_signals, weekly_signals = count_signal_time_types(source_signal_pairs) source_signal_pairs, alias_mapper = create_source_signal_alias_mapper(source_signal_pairs) geo_type = request.args.get("geo_type", "county") if "window" in request.values: time_window, is_day = parse_day_or_week_range_arg("window") else: now_time = extract_date("latest") last = extract_integer("days") last_weeks = extract_integer("weeks") # week if latest is week like or weeks are given otherwise we don't know and guess days if (now_time is not None and not guess_time_value_is_day(now_time)) or last_weeks is not None or weekly_signals > 0: # week if last_weeks is None: last_weeks = last or 30 is_day = False now_week = Week.thisweek() if now_time is None else week_value_to_week(now_time) time_window = (week_to_time_value(now_week - last_weeks), week_to_time_value(now_week)) else: is_day = True if last is None: last = 30 now = date.today() if now_time is None else time_value_to_date(now_time) time_window = (date_to_time_value(now - timedelta(days=last)), date_to_time_value(now)) _verify_argument_time_type_matches(is_day, daily_signals, weekly_signals) q = QueryBuilder("covidcast", "c") fields_string = ["source", "signal"] fields_int = ["time_value"] q.set_fields(fields_string, fields_int) # manually append the count column because of grouping fields_int.append("count") q.fields.append(f"count({q.alias}.geo_value) as count") if geo_type == "only-county": q.where(geo_type="county") q.conditions.append('geo_value not like "%000"') else: q.where(geo_type=geo_type) q.where_source_signal_pairs("source", "signal", source_signal_pairs) q.where_time_pairs("time_type", "time_value", [TimePair("day" if is_day else "week", [time_window])]) q.group_by = "c.source, c.signal, c.time_value" q.set_order("source", "signal", "time_value") _handle_lag_issues_as_of(q, None, None, None) def transform_row(row, proxy): if not alias_mapper or "source" not in row: return row row["source"] = alias_mapper(row["source"], proxy["signal"]) return row return execute_query(q.query, q.params, fields_string, fields_int, [], transform=transform_row)