예제 #1
0
 def test_graph_stacked(self):
     date = datetime.datetime(2020, 1, 1)
     reg1 = 1
     reg2 = 3
     d = {
         'dates': [],
         'region1': [],
         'region2': [],
     }
     for i in range(15):
         date += datetime.timedelta(days=1)
         d['dates'].append(date)
         reg1 += 1
         reg2 += 2
         d['region1'].append(reg1)
         d['region2'].append(reg2)
     p = common.graph_stacked(data=d,
                              start=1,
                              plot_height=450,
                              line_width=10)
예제 #2
0
 def test_graph_stacked_date(self):
     date = datetime.datetime(2020, 1, 1)
     reg1 = 1
     reg2 = 3
     d = {
         'dates': [],
         'region1': [],
         'region2': [],
     }
     for i in range(15):
         date += datetime.timedelta(days=1)
         d['dates'].append(date)
         reg1 += 1
         reg2 += 2
         d['region1'].append(reg1)
         d['region2'].append(reg2)
     d['dates'] = [x.date() for x in d['dates']]
     p = common.graph_stacked(data=d,
                              start=1,
                              plot_height=450,
                              line_width=10,
                              colors=['Crimson', 'LightGreen', 'Salmon'])
예제 #3
0
파일: by_state.py 프로젝트: xyz9911/covid19
def make_state_graphs(verbose = False, plot_height = 400, plot_width = 400, 
        window = None):
    state_pop = get_state_pop()
    if not window:
        window = int(variables.values['by_state_window'])
    if not os.path.isdir('html_temp'):
        os.mkdir('html_temp')
    df_day = get_state_data_day()
    change_dict = change_with_sig(df_day, state_pop)
    df_deaths = get_data_deaths()
    df_cases = get_data_cases()
    df_day = get_state_data_day()
    df_poisson = get_poisson_data()
    dir_path = make_territories_dir('state')
    df_hospital = pd.read_csv('data/hospital.csv') 
    df_hospital['date'] = pd.to_datetime(df_hospital['date'])
    for state in set(df_deaths['state']):
        if verbose:
            print('working on {state}'.format(state = state))
        ps = []
        for the_info in [
                (df_deaths, 'deaths', df_day, 'deaths', 
                    'Deaths by Week'.format(w = window), 
                    'Deaths by Day ({w} day mean)'.format(w = window)), 
                (df_cases, 'cases', df_day, 'cases', 
                    'Cases by Week', 
                    'Cases by Day ({w} day mean)'.format(w = window))]:
            df = the_info[0]
            df_day_ = the_info[2]
            rt_death, rt_death2 = common.get_rt(df_day_[df_day_['state'] == state]['deaths'], 7, 7)
            rt_cases, rt_cases2 = common.get_rt(df_day_[df_day_['state'] == state]['cases'], 7, 7)
            the_dict = {'date': sorted(list(set(df['date'].tolist())))}
            for i in range(1,5):
                shape_data(df, state, i, the_dict, key = the_info[1], 
                        )
            if state in ['Northern Mariana Islands', 'Virgin Islands']:
                continue
            p_poisson = make_state_graph(df = df_poisson, df2 = df_day_, state = state) 
            p_hospital = make_hospital_graph(df_hospital, state = state)
            the_dict = _trim_data(the_dict)
            y = df_day_[df_day_['state'] == state][the_info[1]].rolling(window).mean()  
            first = _get_first_nonzero(y)
            y = y[first:]
            x = df_day_[df_day_['state'] == state]['date'] 
            x = x[first:]
            p = common.graph_stacked(data = the_dict, start = 0, 
                    plot_height = plot_height,plot_width = plot_width ,
                    line_width = 10, title = the_info[4] )
            p_day = common.incidents_over_time_bar2(
                    x = x,
                    y = y,  
                    plot_height = plot_height, 
                    line_width = 3,
                    plot_width = plot_width, title = the_info[5])
            ps.append(p)
            ps.append(p_day)
            if the_info[1] == 'cases':
                ps.append(p_poisson)
                ps.append(p_hospital)
        grid = gridplot(ps, ncols = 2)
        script, div = components(grid)
        html = get_html(territory = state, script = script, div = div,
                last_week_deaths = change_dict[state]['last_week_mean'],
                curr_death = change_dict[state]['current_week_mean'],
                p_curr_week = change_dict[state]['p_value_last_week'],
                p_last_week = change_dict[state]['p_value_last_week2'],
                cur_week_per_million = change_dict[state]['current_week_per_million'],
                last_week_per_million = change_dict[state]['last_week_per_million'],
                    )
        with open(os.path.join(dir_path, 
            '{territory}'.format(territory = slugify(state))), 'w') as write_obj:
            write_obj.write(html)
    make_territories_ref_list('state', list(set(df_day['state'])))
    make_wa()
예제 #4
0
def make_state_graphs(verbose=False, plot_height=400, plot_width=400):
    if not os.path.isdir('html_temp'):
        os.mkdir('html_temp')
    date = datetime.datetime.now()
    df_deaths = get_data_deaths()
    df_cases = get_data_cases()
    df_day = get_state_data_day()
    dir_path = make_territories_dir('state')
    for state in set(df_deaths['state']):
        if verbose:
            print('working on {state}'.format(state=state))
        ps = []
        for the_info in [(
                df_deaths,
                'death',
                df_day,
                'deaths',
                'Deaths by Week',
                'Deaths by Day',
        ), (df_cases, 'cases', df_day, 'cases', 'Cases by Week',
                'Cases by Day')]:
            df = the_info[0]
            df_day_ = the_info[2]
            rt_death, rt_death2 = common.get_rt(
                df_day_[df_day_['state'] == state]['deaths'], 7, 7)
            rt_cases, rt_cases2 = common.get_rt(
                df_day_[df_day_['state'] == state]['cases'], 7, 7)
            the_dict = {'dates': sorted(list(set(df['dates'].tolist())))}
            for i in range(1, 5):
                shape_data(
                    df,
                    state,
                    i,
                    the_dict,
                    key=the_info[1],
                )
            the_dict = _trim_data(the_dict)
            p = common.graph_stacked(data=the_dict,
                                     start=0,
                                     plot_height=plot_height,
                                     plot_width=plot_width,
                                     line_width=10,
                                     title=the_info[4])
            p_day = common.incidents_over_time_bar(
                df_day_[df_day_['state'] == state],
                key=the_info[3],
                window=3,
                plot_height=plot_height,
                plot_width=plot_width,
                title=the_info[5],
                line_width=2)
            ps.append(p)
            ps.append(p_day)
        grid = gridplot(ps, ncols=2)
        script, div = components(grid)
        html = get_html(territory=state,
                        script=script,
                        div=div,
                        date=date,
                        rt_cases=rt_cases,
                        rt_death=rt_death)
        tt = '{territory}'.format(territory=common.tidy_name(state)) + '.html'
        with open(os.path.join(dir_path, tt), 'w') as write_obj:
            write_obj.write(html)
    make_territories_ref_list('state', list(set(df_day['state'])))