# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python [conda env:enron]
#     language: python
#     name: conda-env-enron-py
# ---

# +
import os, inspect
import pandas as pd
import numpy as np
import datetime as dt
import plotly
import plotly.plotly as py
import plotly.graph_objs as go

plotly.tools.set_credentials_file(username='aidinraad', api_key='<KEY>')
# -

topn = 5
CURR_DIR =  os.path.dirname(inspect.getabsfile(inspect.currentframe()))
ROOT_DIR =  os.path.dirname(CURR_DIR)

# ### Load sender data

path = os.path.join(ROOT_DIR,"enron","data", "ext")
ffname = os.path.join(path, "enron-senders.csv")
df_senders = pd.read_csv(ffname, parse_dates=["datetime"])
df_senders.head()

# ### Get sent count data frame

# get yearMonth attribute as "date"
df_senders["date"] = df_senders.datetime.dt.to_period("M")
# group by senders and  year-month dates
gpby = [df_senders.sender, df_senders.date]
df_sender_counts = df_senders.groupby(gpby)["sender"].count()
df_sender_counts = df_sender_counts.to_frame(name="sent")
df_sender_counts.head(10)

# ### Get the most prolific senders

# the most prolific senders, top-N senders
topn_senders = [
    "<NAME>",
    "<NAME>",
    "<NAME>",
    "<NAME>",
    "notes",
    "<NAME>",
    "<NAME>",
    "<NAME>",
    "<NAME>",
    "announcements" 
]

# get top-N sender data
df_topn = df_sender_counts.loc[top10_most_senders, :].reset_index()
df_topn.head(10)

colors = list(range(40))
title = 'Main Source for News'
# labels = ['Television', 'Newspaper', 'Internet', 'Radio']
colors = ['rgb(67,67,67)', 'rgb(115,115,115)', 'rgb(49,130,189)', 'rgb(189,189,189)']
mode_size = [8, 8, 12, 8]
line_size = [2, 2, 4, 2]
id = pd.date_range(start='1998-08', end="2003-01", freq='MS').to_period("M")

# +
data = []

for i in range(0, topn):
    df_sender = df_sender_counts.loc[topn_senders[i], :]
    df_sender = df_sender.reindex(id, fill_value=0)
    
    trace = go.Scatter(
        x=list(df_sender.index.astype(str).values),
        y=df_sender.sent,
        name = top10_most_senders[i],
#         line = dict(color=np.random.randn(500)),
#         colorscale='Viridis',
        opacity = 0.8,
        mode="lines+markers"
    )

    data.append(trace)

# +
layout = dict(
    title='Time Series with Rangeslider',
    xaxis=dict(
        rangeselector=dict(
            buttons=list([
                dict(count=1,
                     label='1m',
                     step='month',
                     stepmode='backward'),
                dict(count=6,
                     label='6m',
                     step='month',
                     stepmode='backward'),
                dict(step='all')
            ])
        ),
        rangeslider=dict(
            visible = True
        ),
        type='date'
    )
)

fig = dict(data=data, layout=layout)
py.iplot(fig, filename = "Time Series with Rangeslider")

# +
# annotations = []
# # Adding labels
# for y_trace, label, color in zip(y_data, labels, colors):
#     # labeling the left_side of the plot
#     annotations.append(dict(xref='paper', x=0.05, y=y_trace[0],
#                                   xanchor='right', yanchor='middle',
#                                   text=label + ' {}%'.format(y_trace[0]),
#                                   font=dict(family='Arial',
#                                             size=16),
#                                   showarrow=False))
#     # labeling the right_side of the plot
#     annotations.append(dict(xref='paper', x=0.95, y=y_trace[11],
#                                   xanchor='left', yanchor='middle',
#                                   text='{}%'.format(y_trace[11]),
#                                   font=dict(family='Arial',
#                                             size=16),
#                                   showarrow=False))
# # Title
# annotations.append(dict(xref='paper', yref='paper', x=0.0, y=1.05,
#                               xanchor='left', yanchor='bottom',
#                               text='Main Source for News',
#                               font=dict(family='Arial',
#                                         size=30,
#                                         color='rgb(37,37,37)'),
#                               showarrow=False))
# # Source
# annotations.append(dict(xref='paper', yref='paper', x=0.5, y=-0.1,
#                               xanchor='center', yanchor='top',
#                               text='Source: PewResearch Center & ' +
#                                    'Storytelling with data',
#                               font=dict(family='Arial',
#                                         size=12,
#                                         color='rgb(150,150,150)'),
#                               showarrow=False))

# layout['annotations'] = annotations

fig = go.Figure(data=traces, layout=layout)
py.iplot(fig, filename='news-source')
# -








StarcoderdataJupyterScriptsDedupFiltered