<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Table of Contents
 Detailed panel 
 Weekly incidence curve with typical intensity and thresholds 
 Function for incidence plot
Step1:  In this example, we show the current year incidence up to given week.<br>
Along with the current incidence, we present the following intensity thresholds
Step2:  UF
Step3:  Entries with dfthresholds['se típica do inicio do surto'] = NaN have activity too low for proper epidemic threshold definition
Step4:  Displaying data for user selected week w<a name="_historical data display"></a>
For each week w selected by the user, the notification curve will always be that which is found on df_inci, while the estimates will be that stored in df_hist. Data df_inci only has the most recent estimates, which are based on the most recent week with data. The estimates obtained at each week is stored at df_hist.
So, first of all, we will slice the historical data to week w, and limit current data to week <= w.
If w=23, the historical dataset is already correctly sliced in df_hist2017, so we just have to limit the current for the proper plot
<ASSISTANT_TASK:>
Python Code:
# local
from fludashboard.libs.flu_data import prepare_keys_name
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
Explanation: Table of Contents
 Detailed panel 
 Weekly incidence curve with typical intensity and thresholds 
 Function for incidence plot: 
 State example 
 Regional example 
  Example with state where estimates are not available: 
 Obtaining the most probable activity level at selected week 
 Age distribution 
 Function for age distribution plot: 
 Incidence table information 
 Summary panel 
 Season level categorization: 
 Function to calculate seasonal level 
 Example applying to a given entry 
 Applying to the whole dataset 
 Seasonal age distribution 
 Incidence table information 
 Displaying data for user selected week
Detailed panel<a name="_detailed panel"></a>
Weekly incidence curve with typical intensity and thresholds<a name="_weekly incidence curve with typical intensity and thresholds"></a>
End of explanation
df_hist = pd.read_csv('../data/historical_estimated_values.csv', encoding='utf-8')
df_inci = pd.read_csv('../data/current_estimated_values.csv', encoding='utf-8')
df_typi = pd.read_csv('../data/mem-typical.csv', encoding='utf-8')
df_thre = pd.read_csv('../data/mem-report.csv', encoding='utf-8')
prepare_keys_name(df_hist)
prepare_keys_name(df_inci)
prepare_keys_name(df_typi)
prepare_keys_name(df_thre)
level_dict = {
    'L0': 'Baixa', 
    'L1': 'Epidêmica',
    'L2': 'Alta', 
    'L3': 'Muito alta'
}
df_inci.columns
Explanation: In this example, we show the current year incidence up to given week.<br>
Along with the current incidence, we present the following intensity thresholds:<br>
Low activity threshold: estimated epidemic threshold based on historical levels. Minimum: incidence equivalent to 5 cases.
High activity threshold: incidence considered high based on historical levels. Minimum: incidence equivalent to 10 cases.
Very high activity threshold: incidence considered very high based on historical levels. Minimum: incidence equivalent to 20 cases.
End of explanation
df_inci.head(5)
df_typi.head(5)
df_thre.tail(5)
Explanation: UF: locality code (includes UFs, Regions and Country)
Tipo: locality type (Estado, Regional or País)
mean: estimated mean incidence
50%: estimated median
2.5%: estimation lower 95% confidence interval
97.5%: estimation upper 95% confidence interval
L0: probability of being below epi. threshold (low level)
L1: probability of being above epi. threshold and below high activity (epidemic level)
L2: prob. of being above high activity and below very high (high level)
L3: prob. of being above very high activity threshold (very high level)
Situation:
stable: might suffer minor changes in the future. Reliable as is;
estimated: data estimated based on opportunity (i.e. notification delay) profile. Reliable within confidence interval;
unknown: might suffer significant changes in the coming weeks. This is the case for locations where estimation is not possible and data is still "fresh". Unreliable.
End of explanation
k = ['epiyear', 'epiweek', 'base_epiyear', 'base_epiweek']
df_inci2017 = df_inci[
    (df_inci.epiyear == 2017) &
    # (df_inci.epiweek >= 15) &
    (df_inci.dado == 'srag') &
    (df_inci.escala == 'incidência') & 
    (df_inci.uf == 'BR')
].copy()
df_inci2017.sort_values(['epiyear', 'epiweek'], inplace=True)
df_inci_chart = df_inci2017.copy()
df_inci_chart.index = df_inci_chart.epiweek
k = ['epiyear', 'epiweek', 'base_epiyear', 'base_epiweek']
df_hist2017 = df_hist[
    (df_hist.base_epiyear == 2017) &
    (df_hist.base_epiweek == 23) &
    (df_hist.dado == 'srag') &
    (df_hist.escala == 'incidência') & 
    (df_hist.uf == 'BR')
].copy()
df_hist2017.sort_values(['epiyear', 'epiweek'], inplace=True)
df_hist_chart = df_hist2017.copy()
df_hist_chart.index = df_hist_chart.epiweek
# 50% estimated cases
df_inci_chart[['srag', '50%', '2.5%', '97.5%']].plot()
plt.title('Incidence')
plt.grid(True)
plt.show()
df_hist_chart[['srag', '50%', '2.5%', '97.5%']].plot()
plt.title('Historial')
plt.grid(True)
plt.show()
df_hist2017['estimated_cases'] = df_hist2017['50%']
df = pd.merge(
    df_inci2017[['epiweek', 'srag', '2.5%', '97.5%']], 
    df_hist2017[['epiweek', 'estimated_cases']], 
    on='epiweek', how='outer'
)
df.set_index('epiweek', inplace=True)
df.plot()
plt.grid(True)
plt.title('Incidence X Historial')
plt.show()
Explanation: Entries with dfthresholds['se típica do inicio do surto'] = NaN have activity too low for proper epidemic threshold definition
End of explanation
df_hist[
        (df_hist.base_epiyear == 2017) &
        (df_hist.dado == 'srag') &
        (df_hist.escala == 'incidência') & 
        (df_hist.uf == 'BR')
        ].base_epiweek.unique()
# First, last keep only stable weeksfor notification curve:
df_inci2017.loc[(df_inci2017.situation != 'stable'), 'srag'] = np.nan
# Adapt historical dataset:
df_hist.sort_values(['epiyear', 'epiweek'], inplace=True)
df_hist['estimated_cases'] = df_hist['50%']
# User selected week:
y = 2017
w = 23
def week_data(y, w):
    df_week_inci = df_inci2017[(df_inci2017.epiweek <= w)]
    df_week_hist = df_hist[
        (df_hist.base_epiyear == y) &
        (df_hist.base_epiweek == w) &
        (df_hist.dado == 'srag') &
        (df_hist.escala == 'incidência') & 
        (df_hist.uf == 'BR')
    ].copy()
    df = pd.merge(
        df_week_inci[['epiweek', 'srag']], 
        df_week_hist[['epiweek', 'estimated_cases', '2.5%', '97.5%']], 
        on='epiweek', how='outer'
    )
    df.set_index('epiweek', inplace=True)
    return df
df = week_data(y, w)
df.plot()
plt.grid(True)
plt.show()
w = 28
df = week_data(y, w)
df.plot()
plt.grid(True)
plt.show()
w = 33
df = week_data(y, w)
df.plot()
plt.grid(True)
plt.show()
Explanation: Displaying data for user selected week w<a name="_historical data display"></a>
For each week w selected by the user, the notification curve will always be that which is found on df_inci, while the estimates will be that stored in df_hist. Data df_inci only has the most recent estimates, which are based on the most recent week with data. The estimates obtained at each week is stored at df_hist.
So, first of all, we will slice the historical data to week w, and limit current data to week <= w.
If w=23, the historical dataset is already correctly sliced in df_hist2017, so we just have to limit the current for the proper plot:
End of explanation
<END_TASK>