def generate_state_timeseries( projection_row, intervention, input_dir ) -> CovidActNowStateTimeseries: state = US_STATE_ABBREV[projection_row[rc.STATE_FULL_NAME]] fips = projection_row[rc.FIPS] raw_dataseries = get_can_projection.get_can_raw_data( input_dir, state, fips, AggregationLevel.STATE, intervention ) # join in state testing data onto the timeseries # left join '%m/%d/%y', so the left join gracefully handles # missing state testing data (i.e. NE) testing_df = get_testing_timeseries_by_state(state) new_df = pd.DataFrame(raw_dataseries).merge(testing_df, on="date", how="left") can_dataseries = new_df.to_dict(orient="records") timeseries = [] for data_series in can_dataseries: timeseries.append(_generate_state_timeseries_row(data_series)) projections = _generate_api_for_projections(projection_row) if len(timeseries) < 1: raise Exception(f"State time series empty for {intervention.name}") state_intervention = get_can_projection.get_intervention_for_state(state) actuals_ts = combined_datasets.build_us_timeseries_with_all_fields() actual_latest = combined_datasets.build_us_latest_with_all_fields() state_latest = actual_latest.get_record_for_state(state) return CovidActNowStateTimeseries( population=state_latest[CommonFields.POPULATION], lat=projection_row[rc.LATITUDE], long=projection_row[rc.LONGITUDE], actuals=_generate_actuals( state_latest, state_intervention ), stateName=projection_row[rc.STATE_FULL_NAME], fips=projection_row[rc.FIPS], lastUpdatedDate=_format_date(projection_row[rc.LAST_UPDATED]), projections=projections, timeseries=timeseries, actuals_timeseries=_generate_actuals_timeseries( actuals_ts.get_records_for_state(state), state_intervention ), )
def generate_county_timeseries(projection_row, intervention, input_dir): state_abbrev = US_STATE_ABBREV[projection_row[rc.STATE_FULL_NAME]] fips = projection_row[rc.FIPS] raw_dataseries = get_can_projection.get_can_raw_data( input_dir, state_abbrev, fips, AggregationLevel.COUNTY, intervention ) testing_df = get_testing_timeseries_by_fips(fips) new_df = pd.DataFrame(raw_dataseries).merge(testing_df, on="date", how="left") can_dataseries = new_df.to_dict(orient="records") timeseries = [] for data_series in can_dataseries: timeseries.append(_generate_county_timeseries_row(data_series)) if len(timeseries) < 1: raise Exception(f"County time series empty for {intervention.name}") projections = _generate_api_for_projections(projection_row) state_intervention = get_can_projection.get_intervention_for_state(state_abbrev) actuals_ts = combined_datasets.build_us_timeseries_with_all_fields() actual_latest = combined_datasets.build_us_latest_with_all_fields() fips_latest = actual_latest.get_record_for_fips(fips) return CovidActNowCountyTimeseries( population=fips_latest[CommonFields.POPULATION], lat=projection_row[rc.LATITUDE], long=projection_row[rc.LONGITUDE], actuals=_generate_actuals( fips_latest, state_intervention ), stateName=projection_row[rc.STATE_FULL_NAME], countyName=projection_row[rc.COUNTY], fips=projection_row[rc.FIPS], lastUpdatedDate=_format_date(projection_row[rc.LAST_UPDATED]), projections=projections, timeseries=timeseries, actuals_timeseries=_generate_actuals_timeseries( actuals_ts.get_records_for_fips(fips), state_intervention ), )