def rates(df_states, min_value, window = 3): states = sorted(set(df_states['state'].tolist())) l = [] for i in states: increase_deaths = common.get_rate_increase( df = df_states[(df_states['state']==i) & (df_states['deaths'] > min_value)], key = 'deaths', window =window) increase_cases = common.get_rate_increase( df = df_states[(df_states['state']==i) & (df_states['cases'] > min_value)], key = 'cases', window =window) if len(increase_deaths) == 0 or len(increase_deaths) == 1 or math.isnan(increase_deaths[-1]): last_value_deaths = None double_r_deaths = None else: last_value_deaths = round(increase_deaths[-1],2) double_r_deaths = common.get_double_rate(last_value_deaths) if len(increase_cases) == 0 or len(increase_cases) == 1 or math.isnan(increase_cases[-1]): last_value_cases = None double_r_cases = None else: last_value_cases= round(increase_cases[-1],2) double_r_cases = common.get_double_rate(last_value_cases) l.append([i, last_value_deaths, double_r_deaths, last_value_cases, double_r_cases]) l.insert(0, ['state', 'deaths_rate', 'deaths_double', 'cases_rate', 'cases_double']) with open('html_temp/rates.csv', 'w') as write_obj: writer = csv.writer(write_obj) writer.writerows(l)
def dy_dx(territory_key, territory, df, window, key, plot_height, plot_width, min_value = 0): """ Create a figure and metrics for the rate of increase or decrease """ increase = common.get_rate_increase( df = df[(df[territory_key]==territory) & (df[key] > min_value)], key = key, window =window) if len(increase) == 0 or len(increase) == 1 or math.isnan(increase[-1]): return None, None, None last_val = increase[-1] double_rate = common.get_double_rate(last_val) if last_val < 1: n = common.get_days_less_than_0(increase) msg = 'under 1 for {n} days'.format(n = n) else: if double_rate > 14: msg = 'flat' else: msg = 'doubles every {b} days'.format(b = round(double_rate)) p = figure( plot_height = plot_height, plot_width = plot_width, title = '{key}: {msg}'.format( msg = msg, key = key, ) ) p.line(x = range(len(increase)), y = increase ) p.line(x = range(len(increase)), y = [1 for x in increase], line_dash = 'dashed', color = 'black') return last_val, double_rate, p
def all_states(df_states, key, min_value, window=3, plot_height=300, plot_width=300): states = sorted(set(df_states['state'].tolist())) p_list = [] for i in states: increase = common.get_rate_increase( df=df_states[(df_states['state'] == i) & (df_states[key] > min_value)], key=key, window=window) if len(increase) == 0 or len(increase) == 1 or math.isnan( increase[-1]): continue min_val = increase[-1] if min_val < 1: n = common.get_days_less_than_0(increase) msg = 'under 1 for {n} days'.format(n=n) else: double_rate = common.get_double_rate(min_val) msg = 'doubles every {b} days'.format(b=round(double_rate)) p = figure(plot_height=plot_height, plot_width=plot_width, title='{state}: {msg}'.format(state=i, msg=msg)) p.line(x=range(len(increase)), y=increase) p.line(x=range(len(increase)), y=[1 for x in increase], line_dash='dashed', color='black') p_list.append(p) grid = gridplot(p_list, ncols=4) return grid