Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import altair as alt
- import pandas as pd
- class ReportCityCharts:
- @classmethod
- def create_incidence_chart(
- cls,
- df: pd.DataFrame,
- year_week: int,
- threshold_pre_epidemic: float,
- threshold_pos_epidemic: float,
- threshold_epidemic: float,
- ):
- """
- @see: https://stackoverflow.com/questions/45526734/
- hide-legend-entries-in-a-plotly-figure
- :param df:
- :param year_week:
- :param threshold_pre_epidemic: float,
- :param threshold_pos_epidemic: float
- :param threshold_epidemic: float
- :return:
- """
- df = df.reset_index()[
- ['SE', 'incidência', 'casos notif.', 'level_code']
- ]
- # 200 = 2 years
- df = df[df.SE >= year_week - 200]
- df['SE'] = df.SE.map(lambda v: '%s/%s' % (str(v)[:4], str(v)[-2:]))
- k = 'incidência'
- df['alerta verde'] = df[df.level_code == 1][k]
- df['alerta amarelo'] = df[df.level_code == 2][k]
- df['alerta laranja'] = df[df.level_code == 3][k]
- df['alerta vermelho'] = df[df.level_code == 4][k]
- df['limiar epidêmico'] = threshold_epidemic
- df['limiar pós epidêmico'] = threshold_pos_epidemic
- df['limiar pré epidêmico'] = threshold_pre_epidemic
- """
- figure_bar = df.iplot(
- asFigure=True,
- kind='bar',
- x=['SE'],
- y=[
- 'alerta verde',
- 'alerta amarelo',
- 'alerta laranja',
- 'alerta vermelho',
- ],
- legend=True,
- showlegend=True,
- yTitle='Incidência',
- xTitle='Período (Ano/Semana)',
- color=[
- 'rgb(0,255,0)',
- 'rgb(255,255,0)',
- 'rgb(255,150,0)',
- 'rgb(255,0,0)',
- ],
- hoverinfo='x+y+name',
- )
- figure_threshold = df.iplot(
- asFigure=True,
- x=['SE'],
- y=[
- 'limiar pré epidêmico',
- 'limiar pós epidêmico',
- 'limiar epidêmico',
- ],
- legend=False,
- showlegend=False,
- color=['rgb(0,255,0)', 'rgb(255,150,0)', 'rgb(255,0,0)'],
- hoverinfo='none',
- )
- figure_line = df.iplot(
- asFigure=True,
- x=['SE'],
- y=['casos notif.'],
- legend=False,
- showlegend=False,
- secondary_y=['casos notif.'],
- secondary_y_title='Casos',
- hoverinfo='x+y+name',
- color=['rgb(33,33,33)'],
- )
- figure_line['layout']['legend'].update(
- x=-0.27,
- y=0.5,
- traceorder='normal',
- font=dict(family='sans-serif', size=12, color='#000'),
- bgcolor='#FFFFFF',
- bordercolor='#E2E2E2',
- borderwidth=1,
- orientation='l',
- )
- figure_line['layout']['yaxis1'].update(title='Incidência')
- figure_line['layout'].update(
- title=(
- 'Limiares de incidência:: '
- + 'pré epidêmico=%s; '
- + 'pós epidêmico=%s; '
- + 'epidêmico=%s;'
- )
- % (
- '{:.1f}'.format(threshold_pre_epidemic),
- '{:.1f}'.format(threshold_pos_epidemic),
- '{:.1f}'.format(threshold_epidemic),
- ),
- showlegend=True,
- )
- figure_threshold.data.extend(figure_bar.data)
- figure_line.data.extend(figure_threshold.data)
- figure_line['layout']['yaxis2'].update(
- showgrid=False, range=[0, df['casos notif.'].max()]
- )
- figure_line['layout']['xaxis1'].update(
- tickangle=-60, nticks=len(df) // 4, title='Período (Ano/Semana)'
- )
- for trace in figure_line['data']:
- if trace['name'] == 'casos notif.':
- trace['visible'] = 'legendonly'
- return _plot_html(
- figure_or_data=figure_line,
- config={},
- validate=True,
- default_width='100%',
- default_height=500,
- global_requirejs='',
- )[0]
- """
- df_casos = df.rename(columns = {"casos notif.": "casosnotif"})
- base = alt.Chart(df_casos).encode(alt.X('SE:T', axis=alt.Axis(title='Período (Ano/Semana)')))
- bar = base.mark_bar().encode(alt.Y('incidência', axis=alt.Axis(title='Incidência')))
- line = base.mark_line(color="#ffd100", strokeWidth=5).encode(alt.Y('casosnotif', axis=alt.Axis(title='Casos')))
- return (bar + line).resolve_scale(y='independent').properties(width=1000)
- @classmethod
- def create_climate_chart(
- cls,
- df: pd.DataFrame,
- var_climate,
- year_week,
- climate_crit,
- climate_title,
- ):
- """
- :param df:
- :param var_climate:
- :param year_week:
- :param climate_crit:
- :param climate_title:
- :return:
- """
- k = var_climate.replace('_', '.')
- df_climate = df.reset_index()[['SE', k]]
- df_climate = df_climate[df_climate.SE >= year_week - 200]
- df_climate['SE'] = df_climate.SE.map(
- lambda v: '%s/%s' % (str(v)[:4], str(v)[-2:])
- )
- df_climate['Limiar favorável transmissão'] = climate_crit
- """
- figure = df_climate.iplot(
- asFigure=True,
- x=['SE'],
- y=[k, 'Limiar favorável transmissão'],
- showlegend=True,
- yTitle=climate_title,
- xTitle='Período (Ano/Semana)',
- )
- figure['layout']['xaxis1'].update(
- tickangle=-60, nticks=len(df_climate) // 4
- )
- figure['layout']['legend'].update(
- x=-0.1,
- y=1.2,
- traceorder='normal',
- font=dict(family='sans-serif', size=12, color='#000'),
- bgcolor='#FFFFFF',
- bordercolor='#E2E2E2',
- borderwidth=1,
- )
- return _plot_html(
- figure_or_data=figure,
- config={},
- validate=True,
- default_width='100%',
- default_height=500,
- global_requirejs='',
- )[0]
- """
- df_climate_chart = df_climate.rename(columns = {
- "temp.min":"temp_min",
- "Limiar favorável transmissão": "limiar_transmissao"})
- return alt.Chart(df_climate_chart).mark_trail().encode(
- alt.X('SE:T', axis=alt.Axis(title='Período (Ano/Semana)')),
- alt.Y('temp_min', axis=alt.Axis(title='Temperatura'))
- ).properties(width=1050)
- @classmethod
- def create_tweet_chart(cls, df: pd.DataFrame, year_week):
- """
- :param df:
- :param var_climate:
- :param year_week:
- :param climate_crit:
- :param climate_title:
- :return:
- """
- df_tweet = df.reset_index()[['SE', 'tweets']]
- df_tweet = df_tweet[df_tweet.SE >= year_week - 200]
- df_tweet['SE'] = df_tweet.SE.map(
- lambda v: '%s/%s' % (str(v)[:4], str(v)[-2:])
- )
- #df_tweet.rename(columns={'tweets': 'menções'}, inplace=True)
- """
- figure = df_tweet.iplot(
- x=['SE'],
- y=['menções'],
- asFigure=True,
- showlegend=True,
- xTitle='Período (Ano/Semana)',
- )
- figure['layout']['xaxis1'].update(
- tickangle=-60, nticks=len(df_tweet) // 4
- )
- figure['layout']['yaxis1'].update(title='Tweets')
- figure['layout']['legend'].update(
- x=-0.1,
- y=1.2,
- traceorder='normal',
- font=dict(family='sans-serif', size=12, color='#000'),
- bgcolor='#FFFFFF',
- bordercolor='#E2E2E2',
- borderwidth=1,
- )
- return _plot_html(
- figure_or_data=figure,
- config={},
- validate=True,
- default_width='100%',
- default_height=500,
- global_requirejs='',
- )[0]
- """
- return alt.Chart(df_tweet).mark_trail().encode(
- alt.X('SE:T', axis=alt.Axis(title='Período (Ano/Semana)')),
- alt.Y('tweets', axis=alt.Axis(title='Tweets'))
- ).properties(width=1050)
- class ReportStateCharts:
- @classmethod
- def create_tweet_chart(cls, df: pd.DataFrame, year_week, disease: str):
- """
- :param df:
- :param year_week:
- :param disease:
- :return:
- """
- # print(cls)
- # <class 'dados.charts.ReportStateCharts'>
- ks_cases = ['casos notif. {}'.format(disease)]
- # print(ks_cases)
- # ['casos notif. zika']
- df_tweet = df.reset_index()[['SE', 'tweets'] + ks_cases]
- df_tweet.to_csv('df_tweet01.csv')
- # filtro
- # print(year_week)
- # 201923
- df_tweet = df_tweet[df_tweet.SE >= year_week - 200]
- df_tweet.to_csv('df_tweet02.csv')
- # rename
- df_tweet.rename(columns={'tweets':'mencoes'}, inplace=True)
- df_tweet.to_csv('df_tweet03.csv')
- #df.to_csv('df2.csv')
- #print(df.index)
- """
- Int64Index([201723, 201723, 201723, 201723, 201723, 201723, 201723, 201723,
- 201723, 201723,
- ...
- 201923, 201923, 201923, 201923, 201923, 201923, 201923, 201923,
- 201923, 201923],
- dtype='int64', name='SE', length=1680)
- """
- if ks_cases :
- df_grp = (
- df_tweet.groupby(df.index)[['mencoes'] + ks_cases]
- .sum()
- .reset_index()
- )
- # df_grp.info()
- df_grp.to_csv('df_grp0001.csv')
- df_grp['SE'] = df_grp.SE.map(
- lambda v: '%s/%s' % (str(v)[:4], str(v)[-2:])
- )
- df_grp.to_csv('df_grp0002.csv')
- """
- fig_tweet = df_grp.iplot(
- x=['SE'],
- y=['menções'],
- asFigure=True,
- showlegend=True,
- xTitle='Período (Ano/Semana)',
- color=['rgb(128,128,128)'],
- )
- fig_cases = df_grp.iplot(
- x=['SE'],
- y=ks_cases,
- asFigure=True,
- secondary_y=ks_cases,
- secondary_y_title='Casos',
- showlegend=True,
- xTitle='Período (Ano/Semana)',
- color=['rgb(0,0,255)'],
- )
- fig_cases.data.extend(fig_tweet.data)
- fig_cases['layout']['xaxis1'].update(
- tickangle=-60, nticks=len(df_grp) // 24
- )
- fig_cases['layout']['yaxis1'].update(
- title='Tweets', range=[0, df_grp['menções'].max()]
- )
- fig_cases['layout']['yaxis2'].update(
- range=[0, df_grp[ks_cases].max().max()]
- )
- fig_cases['layout'].update(
- title='Casos {} / Menções mídia social'.format(disease)
- )
- fig_cases['layout']['legend'].update(
- x=-0.1,
- y=1.2,
- traceorder='normal',
- font=dict(family='sans-serif', size=12, color='#000'),
- bgcolor='#FFFFFF',
- bordercolor='#E2E2E2',
- borderwidth=1,
- )
- return _plot_html(
- figure_or_data=fig_cases,
- config={},
- validate=True,
- default_width='100%',
- default_height=300,
- global_requirejs='',
- )[0]
- """
- """
- se = alt.Chart(df_grp).encode(
- alt.X('SE:T', axis=alt.Axis(title='Período (Ano/Semana)'))
- )
- tweets = se.mark_trail().encode(
- alt.Y('mencoes', axis=alt.Axis(title='Tweets'))
- )
- casos = se.mark_trail().encode(
- alt.Y('casos notif dengue', axis=alt.Axis(title='Casos'))
- )
- return (tweets, casos).resolve_scale(y='independent')
- """
- if disease == 'dengue':
- df_grp.rename(columns={'casos notif. dengue':'casos notif dengue'}, inplace=True)
- df_grp.info()
- return alt.Chart(df_grp).mark_trail().encode(
- alt.X('SE:T', axis=alt.Axis(title='Período (Ano/Semana)')),
- alt.Y('mencoes', axis=alt.Axis(title='Tweets')),
- #alt.Y('casos notif dengue', axis=alt.Axis(title='Casos'))
- ).resolve_scale(y='independent').properties(width=1020)
- elif disease == 'chik':
- df_grp.rename(columns={'casos notif. chik':'casos notif chik'}, inplace=True)
- df_grp.info()
- return alt.Chart(df_grp).mark_trail().encode(
- alt.X('SE:T', axis=alt.Axis(title='Período (Ano/Semana)')),
- alt.Y('mencoes', axis=alt.Axis(title='Tweets')),
- #alt.Y('casos notif chik', axis=alt.Axis(title='Casos'))
- ).resolve_scale(y='independent').properties(width=1020)
- elif disease == 'zika':
- df_grp.rename(columns={'casos notif. zika':'casos notif zika'}, inplace=True)
- df_grp.info()
- return alt.Chart(df_grp).mark_trail().encode(
- alt.X('SE:T', axis=alt.Axis(title='Período (Ano/Semana)')),
- alt.Y('mencoes', axis=alt.Axis(title='Tweets')),
- #alt.Y('casos notif zika', axis=alt.Axis(title='Casos'))
- ).resolve_scale(y='independent').properties(width=1020)
- else :
- df_grp.info()
- return alt.Chart(df_grp).mark_trail().encode(
- alt.X('SE:T', axis=alt.Axis(title='Período (Ano/Semana)')),
- alt.Y('mencoes', axis=alt.Axis(title='Tweets')),
- ).resolve_scale(y='independent').properties(width=1020)
- """
- df.to_csv('df_agora.csv')
- df_grp.rename(columns={'casos notif. dengue':'casos notif dengue'}, inplace=True)
- df_grp.rename(columns={'casos notif. chik':'casos notif chik'}, inplace=True)
- df_grp.rename(columns={'casos notif. zika':'casos notif zika'}, inplace=True)
- df_grp.info()
- return alt.Chart(df_grp).mark_trail().encode(
- alt.X('SE:T', axis=alt.Axis(title='Período (Ano/Semana)')),
- alt.Y('mencoes', axis=alt.Axis(title='Tweets')),
- ).resolve_scale(y='independent').properties(width=1020)
- """
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement