from bigbang.archive import Archive
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

"""
Explanation: This note will show you how to use BigBang to investigate a particular project participant's activity.
We will focus on Fernando Perez's role within the IPython community.
First, imports.
End of explanation
"""


url = "ipython-user"
arx = Archive(url)

"""
Explanation: Let's get all the available date from the IPython community. For now, this is just the mailing lists. One day, BigBang will also get its issue tracker data! That will be very exciting.
End of explanation
"""


fernandos = Archive(arx.data[arx.data.From.map(lambda x: 'Fernando' in x)])
fernandos.data[:3]

"""
Explanation: Now let's isolate the messages involving Fernando Perez.
This includes both messages from Fernando, and messages to Fernando.
End of explanation
"""


[x for x in fernandos.get_activity()]

"""
Explanation: Note that our way of finding Fernando Perez was not very precise. We've picked up another Fernando.
End of explanation
"""


not_fernandos = Archive(arx.data[arx.data.From.map(lambda x: 'Fernando' not in x)])
not_fernandos.data[:3]

"""
Explanation: In future iterations, we will use a more sensitive entity recognition technique to find Fernando. This will have to do for now.
We will also need the data for all the emails that were not sent by Fernando.
End of explanation
"""


not_fernandos.get_activity().sum(1).values.shape

nf = pd.DataFrame(not_fernandos.get_activity().sum(1))

f = pd.DataFrame(fernandos.get_activity().sum(1))

both = pd.merge(nf,f,how="outer",left_index=True,right_index=True,suffixes=("_nf","_f")).fillna(0)

"""
Explanation: We now have two Archives made from the original Archive, with the same range of dates, but one with and the other without Fernando. Both contain emails from many addresses. We want to get a single metric of activity.
End of explanation
"""


fig = plt.figure(figsize=(12.5, 7.5))

fa = fernandos.get_activity()

d = np.row_stack((both['0_f'],
                  both['0_nf']))

plt.stackplot(both.index.values,d,linewidth=0)

fig.axes[0].xaxis_date()

plt.show()

"""
Explanation: Let's make a stackplot of this data to see how much of the conversation on the IPython developer's list has been Fernando, over time.
End of explanation
"""


