コード例 #1
0
ファイル: results.py プロジェクト: toparvia/reina-model
def render_result_graphs(df):
    hc_cols = (
        ('available_hospital_beds', _('Hospital beds')),
        ('available_icu_units', _('ICU units')),
    )
    traces = []
    for col, name in hc_cols:
        t = dict(
            type='scatter', name=name, x=df.index, y=df[col], mode='lines',
            hovertemplate='%{y:d}', hoverlabel=dict(namelength=-1),
        )
        traces.append(t)

    card = GraphCard('healthcare', graph=dict(config=dict(responsive=False)))
    layout = make_layout(
        title=_('Free capacity in the healthcare system'), height=250, showlegend=True,
        margin=dict(r=250)
    )
    fig = dict(data=traces, layout=layout)
    card.set_figure(fig)
    c2 = card.render()

    df['ifr'] = df.dead.divide(df.all_infected.replace(0, np.inf)) * 100
    df['cfr'] = df.dead.divide(df.all_detected.replace(0, np.inf)) * 100
    df['r'] = df['r'].rolling(window=7).mean()

    param_cols = (
        ('r', _('Reproductive number (Rₜ)')),
        ('ifr', _('Infection fatality ratio (IFR, %')),
        ('cfr', _('Case fatality ratio (CFR, %')),
    )
    card = GraphCard('params', graph=dict(config=dict(responsive=False)))
    traces = []
    for col, name in param_cols:
        t = dict(
            type='scatter', name=name, x=df.index, y=df[col], mode='lines',
            hovertemplate='%{y:.2f}', hoverlabel=dict(namelength=-1),
        )
        traces.append(t)
    layout = make_layout(
        title=_('Epidemic parameters'), height=250, showlegend=True,
        margin=dict(r=250)
    )
    fig = dict(data=traces, layout=layout)
    card.set_figure(fig)
    c3 = card.render()

    return dbc.Row([
        dbc.Col(render_population_card(df), md=12),
        dbc.Col(c2, md=12),
        dbc.Col(c3, md=12),
        dbc.Col(render_validation_card(df), md=12),
    ])
コード例 #2
0
ファイル: population.py プロジェクト: jtuomist/ghgdash
def generate_population_forecast_graph(pop_df):
    hist_df = pop_df.query('~Forecast')
    hovertemplate = '%{x}: %{y:.0f} 000'
    hist = go.Scatter(x=hist_df.index,
                      y=hist_df.Population / 1000,
                      hovertemplate=hovertemplate,
                      mode='lines',
                      name='Väkiluku',
                      line=dict(color='#9fc9eb', ))

    forecast_df = pop_df.query('Forecast')
    forecast = go.Scatter(x=forecast_df.index,
                          y=forecast_df.Population / 1000,
                          hovertemplate=hovertemplate,
                          mode='lines',
                          name='Väkiluku (enn.)',
                          line=dict(color='#9fc9eb', dash='dash'))
    layout = make_layout(yaxis=dict(
        title='1 000 asukasta',
        zeroline=True,
    ),
                         showlegend=False,
                         title="Helsingin asukasmäärä")

    fig = go.Figure(data=[hist, forecast], layout=layout)
    return fig
コード例 #3
0
def generate_population_forecast_graph(pop_df):
    hist_df = pop_df.query('~Forecast')
    hovertemplate = '%{x}: %{y:.0f} 000'
    hist = go.Scatter(x=hist_df.index,
                      y=hist_df.Population / 1000,
                      hovertemplate=hovertemplate,
                      mode='lines',
                      name=gettext('Population'),
                      line=dict(color='#9fc9eb', ))

    forecast_df = pop_df.query('Forecast')
    forecast = dict(type='scatter',
                    x=forecast_df.index,
                    y=forecast_df.Population / 1000,
                    hovertemplate=hovertemplate,
                    mode='lines',
                    name=gettext('Population (pred.)'),
                    line=dict(color='#9fc9eb', dash='dash'))
    layout = make_layout(yaxis=dict(
        title=gettext('1,000 residents'),
        zeroline=True,
    ),
                         showlegend=False,
                         title=gettext('Population of Helsinki'))

    fig = go.Figure(data=[hist, forecast], layout=layout)
    return fig
コード例 #4
0
def building_selector_callback(
    selected_building_id,
    n_clicks,
    price_of_initial_installation,
    marginal_price_of_peak_power,
    price_of_purchased_electricity,
    grid_output_percentage,
    investment_years
):
    print('callback')
    datasets = dict(
        yearly_solar_radiation_ratio=calculate_percentage_of_yearly_radiation(),
        electricity_supply_emission_factor=calculate_electricity_supply_emission_factor()['EmissionFactor'],
        electricity_spot_price=fingrid_price.purchase_price_of_production_imbalance_power,
    )

    variables = dict(
        price_of_initial_installation=price_of_initial_installation,
        marginal_price_of_peak_power=marginal_price_of_peak_power,
        price_of_purchased_electricity=price_of_purchased_electricity / 100,
        grid_output_percentage=grid_output_percentage / 100,
        investment_years=investment_years,
    )

    if selected_building_id is None:
        perc_sol = datasets['yearly_solar_radiation_ratio']
        trace = go.Scatter(y=perc_sol, x=perc_sol.index, mode='lines')
        layout = make_layout(title='Aurinkosäteily Kumpulassa')
        fig = go.Figure(data=[trace], layout=layout)

        card = GraphCard(id='solar_radiation')
        card.set_figure(fig)

        return html.Div([
            dbc.Row([
                dbc.Col([card.render()]),
            ]),
        ])

    building = buildings_with_pv.loc[selected_building_id]

    df = pd.read_parquet('data/nuuka/%s.parquet' % selected_building_id)
    el_samples = df[df.sensor_id.isin(electricity_sensors.index)]
    el_samples = el_samples.groupby(['building_id', 'time']).value.sum().reset_index()

    sim = analyze_building(building, el_samples, None, variables, datasets)
    if sim is not None:
        out = html.Div([
            visualize_building_pv_summary(building, sim, variables)
        ])
    else:
        out = html.Div()

    return out
コード例 #5
0
ファイル: results.py プロジェクト: Jaoeya/reina-model
def render_validation_card(df):
    det = get_detected_cases()
    det = det[det['confirmed'] > 0]

    det.index = pd.DatetimeIndex(det.index)
    max_date = det.index.max()
    df = df[df.index <= max_date]

    traces = []
    for col_name in ('all_detected', 'hospitalized', 'in_icu', 'dead'):
        col = [x for x in POP_COLS if x[0] == col_name][0]
        traces.append(
            dict(
                type='scatter',
                mode='lines',
                line=dict(color=COLUMN_COLORS[col_name]),
                name=col[2] + ' ' + _('(simulated)'),
                x=df.index,
                y=df[col_name],
                hovertemplate='%{y:d}',
                hoverlabel=dict(namelength=-1),
            ))
        col_name_map = {
            'all_detected': 'confirmed',
        }
        det_col_name = col_name_map.get(col_name, col_name)
        traces.append(
            dict(
                type='scatter',
                mode='markers',
                line=dict(color=COLUMN_COLORS[col_name]),
                name=col[2] + ' ' + _('(real)'),
                x=det.index,
                y=det[det_col_name],
                hovertemplate='%{y:d}',
                hoverlabel=dict(namelength=-1),
            ))

    card = GraphCard('validation',
                     graph=dict(config=dict(
                         responsive=False,
                         displayModeBar=True,
                     )))
    layout = make_layout(title=_('Validation'),
                         height=250,
                         showlegend=True,
                         margin=dict(r=250),
                         xaxis=dict(fixedrange=False))
    fig = dict(data=traces, layout=layout)
    card.set_figure(fig)
    return card.render()
コード例 #6
0
ファイル: results.py プロジェクト: Jaoeya/reina-model
def render_population_card(df):
    traces = generate_population_traces(df)
    card = GraphCard('population', graph=dict(config=dict(responsive=False)))
    shapes, annotations, bar_count = make_intervention_shapes(df)

    layout = make_layout(title=_('Population'),
                         height=250 + bar_count * IV_BAR_PIXELS,
                         showlegend=True,
                         margin=dict(r=250, b=50 + bar_count * IV_BAR_PIXELS),
                         shapes=shapes,
                         annotations=annotations)
    fig = dict(data=traces, layout=layout)
    card.set_figure(fig)
    return card.render()
コード例 #7
0
ファイル: buildings.py プロジェクト: City-of-Helsinki/ghgdash
def generate_buildings_forecast_graph():
    df = generate_building_floor_area_forecast()

    cdf = pd.DataFrame(index=df.index)
    for name, attrs in BUILDING_USES.items():
        cdf[name] = df[attrs['types']].sum(axis=1) / 1000000

    # Sort columns based on the amounts in the last measured year
    last_year = cdf.loc[cdf.index.max()]
    columns = list(last_year.sort_values(ascending=False).index.values)

    traces = []
    lang = get_active_locale()
    for name in columns:
        attrs = BUILDING_USES[name]
        val = df[attrs['types']].sum(axis=1) / 1000000
        label = name
        if lang == 'en':
            label = BUILDINGS_EN[name]
        trace = go.Bar(x=df.index,
                       y=val,
                       name=label,
                       marker=dict(color=attrs['color']),
                       hoverinfo='name',
                       hovertemplate='%{x}: %{y} Mkem²')
        traces.append(trace)

    last_hist_year = df[~df.Forecast].index.max()
    forecast_divider = dict(type='line',
                            x0=last_hist_year + 0.5,
                            x1=last_hist_year + 0.5,
                            xref='x',
                            y0=0,
                            y1=1,
                            yref='paper',
                            line=dict(dash='dot', color='grey'))

    layout = make_layout(
        barmode='stack',
        yaxis=dict(title='1 000 000 kem²', ),
        xaxis=dict(title=_('Year')),
        title=_('Floor area by building purpose'),
        shapes=[forecast_divider],
        showlegend=True,
        autosize=True,
    )
    return dict(data=traces, layout=layout)
コード例 #8
0
ファイル: params.py プロジェクト: kausaltech/reina-model
def render_model_param_graphs(age):
    PERIOD_PARAMS = (
        ('incubation_period', _('Incubation period')),
        ('illness_period', _('Illness period')),
        ('hospitalization_period',
         _('Duration of regular hospital treatment')),
        ('icu_period', _('Duration of ICU treatment')),
    )

    period_cards = []
    for param, label in PERIOD_PARAMS:
        card = GraphCard(param, graph=dict(config=dict(responsive=False)))
        layout = make_layout(
            title=label,
            height=250,
            yaxis=dict(title='%'),
            xaxis=dict(title=_('days')),
        )
        traces = []
        if param == 'incubation_period':
            sample = sample_model_parameters(param, age)
            sample = sample * 100 / sample.sum()
            trace = dict(
                type='bar',
                x=sample.index,
                y=sample.values,
                hovertemplate='%{y} %',
                name='',
            )
            traces.append(trace)
        else:
            for severity in ('ASYMPTOMATIC', 'MILD', 'SEVERE', 'CRITICAL',
                             'FATAL'):
                if param == 'icu_period' and severity in ('ASYMPTOMATIC',
                                                          'MILD', 'SEVERE'):
                    continue
                if param == 'hospitalization_period' and severity in (
                        'ASYMPTOMATIC', 'MILD'):
                    continue
                sample = sample_model_parameters(param, age, severity)
                sample = sample * 100 / sample.sum()
                trace = go.Bar(type='bar',
                               x=sample.index,
                               y=sample.values,
                               hovertemplate='%{y} %',
                               name=str(SYMPTOM_MAP[severity]),
                               marker_color=SYMPTOM_COLOR_MAP[severity])
                traces.append(trace)
            layout['barmode'] = 'group'
            layout['showlegend'] = True

        fig = dict(layout=layout, data=traces)
        card.set_figure(fig)
        period_cards.append(card.render())

    sample = sample_model_parameters('symptom_severity', age)
    sample.index = sample.index.map(SYMPTOM_MAP)
    card = GraphCard('symptom-severity',
                     graph=dict(config=dict(responsive=False)))
    layout = make_layout(
        title=_('Symptom severity'),
        height=250,
        showlegend=False,
        yaxis=dict(title='%'),
    )
    sample = sample * 100 / sum(sample)
    trace = dict(type='bar', x=sample.index, y=sample.values)
    fig = dict(layout=layout, data=[trace])
    card.set_figure(fig)
    c2 = card.render()

    sample = sample_model_parameters('contacts_per_day', age)
    card = GraphCard('contacts_per_day',
                     graph=dict(config=dict(responsive=False)))
    layout = make_layout(
        title=_('Contacts per day'),
        height=250,
        showlegend=False,
        yaxis=dict(title='%', ),
        xaxis=dict(title=_('number of contacts')),
    )
    sample = sample.reindex(range(0, 100), fill_value=0)
    sample = sample * 100 / sum(sample)
    trace = dict(type='bar', x=sample.index, y=sample.values)
    fig = dict(layout=layout, data=[trace])
    card.set_figure(fig)
    c3 = card.render()

    sample = sample_model_parameters('infectiousness', age)
    sample *= 100
    card = GraphCard('infectiousness',
                     graph=dict(config=dict(responsive=False)))
    layout = make_layout(
        title=_('Infectiousness over time'),
        height=250,
        showlegend=False,
        yaxis=dict(title='%', ),
        xaxis=dict(
            title=_('Day of illness'),
            range=[-2, 14],
        ),
    )
    trace = dict(
        type='lines',
        x=sample.index,
        y=sample.values,
        line=dict(shape='spline', smoothing=0.3),
    )
    fig = dict(layout=layout, data=[trace])
    card.set_figure(fig)
    c4 = card.render()

    return html.Div([
        dbc.Row([
            dbc.Col(c2, md=6),
            dbc.Col(c3, md=6),
            dbc.Col(c4, md=6), *[dbc.Col(c, md=6) for c in period_cards]
        ],
                className='mt-4')
    ])
コード例 #9
0
def pv_summary_graph_click(
    click_data,
    selected_building_id,
    price_of_initial_installation,
    marginal_price_of_peak_power,
    price_of_purchased_electricity,
    grid_output_percentage,
    investment_years
):
    print(click_data)
    if not click_data:
        return
    perc = click_data['points'][0]['pointNumber']

    datasets = dict(
        yearly_solar_radiation_ratio=calculate_percentage_of_yearly_radiation(),
        electricity_supply_emission_factor=calculate_electricity_supply_emission_factor()['EmissionFactor'],
        electricity_spot_price=fingrid_price.purchase_price_of_production_imbalance_power,
    )

    variables = dict(
        price_of_initial_installation=price_of_initial_installation,
        marginal_price_of_peak_power=marginal_price_of_peak_power,
        price_of_purchased_electricity=price_of_purchased_electricity / 100,
        grid_output_percentage=grid_output_percentage / 100,
        investment_years=investment_years,
    )

    building = buildings_with_pv.loc[selected_building_id]

    df = pd.read_parquet('data/nuuka/%s.parquet' % selected_building_id)
    el_samples = df[df.sensor_id.isin(electricity_sensors.index)]
    el_samples = el_samples.groupby(['building_id', 'time']).value.sum().reset_index()

    res = analyze_building(building, el_samples, perc, variables, datasets)
    summary = res['summary']

    DIV_1000_COLS = (
        'SolarEnergyProduction', 'BuildingEnergyConsumption', 'GridInputReduction',
        'GridEnergyOutput', 'EnergySalesIncome', 'EnergyCostSavings', 'InstallationPrice',
        'NetCosts',
    )
    for col in DIV_1000_COLS:
        summary[col] /= 1000

    tbl = dash_table.DataTable(
        id='pv-details-table',
        columns=[{"name": [translate_sum_col(i), translate_sum_col(i, True)], "id": i} for i in summary.index],
        data=[summary.to_dict()],
        style_table={'overflowX': 'scroll'},
    )

    def fmt_val(x):
        s = '{0:.3}'.format(x)
        if 'e' in s:
            return '%d' % int(float(s))
        else:
            return s

    tbl = dash_table.DataTable(
        id='pv-details-table',
        columns=[{'name': '', 'id': 'name'}, {'name': '', 'id': 'value'}, {'name': 'Yksikkö', 'id': 'unit'}],
        data=[dict(name=translate_sum_col(key), value=fmt_val(val), unit=translate_sum_col(key, True)) for key, val in summary.items()],
    )

    df = res['simulated']

    t1 = go.Scatter(
        x=df.index,
        y=-df.Consumption,
        mode='lines',
        line=dict(width=0),
        stackgroup='one',
        name='Kiinteistön sähkönkulutus',
    )
    t2 = go.Scatter(
        x=df.index,
        y=df.PanelProduction,
        mode='lines',
        line=dict(width=0),
        stackgroup='one',
        name='Aurinkosähköjärjestelmän tuotanto'
    )

    fig = go.Figure(data=[t1, t2], layout=make_layout(
        title='Simuloitu aurinkosähköjärjestelmä',
        yaxis=dict(
            rangemode='normal',
            title='kWh'
        ),
        margin=go.layout.Margin(
            t=30,
            r=60,
            l=60,
        ),
        showlegend=True,
        legend=dict(
            x=0,
            y=1,
            bgcolor='#E2E2E2',
        ),
    ))

    return [
        html.Div([tbl], className='mb-4'),
        html.Div(make_graph_card(dcc.Graph('simulated-pv-time-series', figure=fig))),
    ]
コード例 #10
0
def building_base_info_callback(selected_building_id):
    datasets = dict(
        electricity_supply_emission_factor=calculate_electricity_supply_emission_factor()['EmissionFactor'],
    )

    if selected_building_id is None:
        el_s = datasets['electricity_supply_emission_factor']
        el_s = el_s.loc[el_s.index >= '2016']
        el_s = el_s.groupby(pd.Grouper(freq='d')).mean()
        trace = go.Scatter(
            y=el_s, x=el_s.index, mode='lines',
        )
        layout = make_layout(title='Sähkönhankinnan päästökerroin')
        fig = go.Figure(data=[trace], layout=layout)

        card = GraphCard(id='electricity-supply-unit-emissions')
        card.set_figure(fig)

        return html.Div([
            dbc.Row([
                dbc.Col([card.render()]),
            ]),
        ])

    building = buildings_with_pv.loc[selected_building_id]

    samples = pd.read_parquet('data/nuuka/%s.parquet' % selected_building_id)
    samples = samples.query('time < "2019-01-01T00:00:00Z"')

    el_samples = samples[samples.sensor_id.isin(electricity_sensors.index)]
    el_samples = el_samples.groupby(['building_id', 'time']).value.sum().reset_index()
    el_samples = el_samples.set_index('time')
    el_samples['emissions'] = el_samples['value'].mul(datasets['electricity_supply_emission_factor'], axis=0, fill_value=0) / 1000

    dh_samples = samples[samples.sensor_id.isin(heating_sensors.index)].query('value < 30000')
    dh_samples = dh_samples.groupby(['building_id', 'time']).value.sum().reset_index()
    dh_samples = dh_samples.set_index('time')
    dh_samples['emissions'] = dh_samples['value'] * 200 / 1000

    group_freq = 'd'
    el_emissions = el_samples.emissions.groupby(pd.Grouper(freq=group_freq)).sum()

    t1 = go.Scatter(
        x=el_emissions.index,
        y=el_emissions,
        mode='lines',
        line=dict(width=0),
        stackgroup='one',
        name='Sähkönkulutuksen päästöt',
    )
    traces = [t1]

    if not dh_samples.empty:
        dh_emissions = dh_samples.emissions.groupby(pd.Grouper(freq=group_freq)).sum()

        t2 = go.Scatter(
            x=dh_emissions.index,
            y=dh_emissions,
            mode='lines',
            line=dict(width=0),
            stackgroup='one',
            name='Lämmönkulutuksen päästöt'
        )
        traces.append(t2)

    fig = go.Figure(data=traces, layout=make_layout(
        title='Kiinteistön energiankulutuksen päästöt: %s' % ' '.join(building.description.split(' ')[1:]),
        yaxis=dict(
            rangemode='normal',
            title='kg (CO₂e.)'
        ),
        margin=go.layout.Margin(
            t=30,
            r=60,
            l=60,
        ),
        showlegend=True,
        legend=dict(
            x=0,
            y=1,
            bgcolor='#E2E2E2',
        ),
    ))

    card = GraphCard(id='building-all-emissions')
    card.set_figure(fig)
    return card.render()
コード例 #11
0
def visualize_building_pv_summary(building, df, variables):
    x = df.SolarPeakPower

    t1 = go.Scatter(
        x=x,
        y=df.InstallationPrice / variables['investment_years'] / 1000,
        mode='lines',
        name='Investointikustannukset (per vuosi, %d v)' % variables['investment_years'],
        marker=dict(color='red')
    )
    t2 = go.Scatter(
        x=x,
        y=(df.EnergyCostSavings + df.EnergySalesIncome) / 1000,
        mode='lines',
        name='Säästö (per vuosi)',
        marker=dict(color='green'),
    )
    t3 = go.Scatter(
        x=x,
        y=df.EmissionReductionCost,
        mode='lines',
        name='Päästövähennyksen hinta',
        yaxis='y2',
        marker=dict(color='grey'),
    )
    layout = make_layout(
        title='Aurinkopaneelien tuottoennuste: %s' % ' '.join(building.description.split(' ')[1:]),
        yaxis=dict(
            title='1 000 €',
            hoverformat='.3r',
            rangemode='tozero',
            separatethousands=True,
        ),
        yaxis2=dict(
            overlaying='y',
            side='right',
            title='€ / (kg / a)',
            rangemode='tozero',
            hoverformat='.2r',
            showgrid=False,
        ),
        xaxis=dict(
            title='kWp',
            hoverformat='.2r',
        ),
        separators=', ',
        margin=go.layout.Margin(
            t=30,
            r=60,
            l=60,
        ),
        showlegend=True,
        legend=dict(x=0, y=1),
    )

    fig = go.Figure(data=[t1, t2, t3], layout=layout)

    card = GraphCard(id='pv-summary')
    card.set_figure(fig)
    return html.Div([
        dbc.Row([
            dbc.Col([
                card.render()
            ], md=12),
        ]),
        dcc.Loading(id="loading-2", children=[
            html.Div(id="pv-details-placeholder")
        ], type="default")
    ])