Details for iMoodJournal_visualization.ipynb

Published by Wenqiu999


Visualize the data exported from iMoodJournal.


Tags & Data Sources

visualization mood change mood variance iMoodJournal


Please log in to comment.

Last updated 2 days, 1 hour ago

This notebook is used to analyze mood changed over time based on iMoodJournal data. It used a data file exported from iMoodJournal.Let's import packages first.

In [1]:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from datetime import datetime

Load the data.

In [2]:
Mood = pd.read_csv('mood-Jul 8, 2019.csv', index_col=False)

In order to organize and access the data using date index, we will transform all columns that are related to time into a DateIndex called DateTime.

In [3]:
date_strings = ['%d. %b %Y', '%b %d, %Y']
date_format = None
datetime_format = None

Mood['Time'] = Mood['Hour'].map(str)['Minute'].map(str), sep = ':')
Mood['Date'] = Mood['Date'].map(str)
Mood['DateTime'] = Mood['Date']['Time'], sep=' ')

while date_strings:
    date_format_test = date_strings.pop()
    datetime_string = '{} %H:%M'.format(date_format_test)
        Mood['DateTime']=pd.to_datetime(Mood['DateTime'], format=datetime_string)
        date_format = date_format_test
        datetime_format = datetime_string
    except ValueError:

if not datetime_format:
    raise Exception('Failed to parse datetime - maybe we need another datetime_string?')

mood = Mood.set_index('DateTime')
mood = mood.drop(['Time'], axis=1)

First, let's start with a line plot of the full time period to show the changes over time.

In [4]:
import matplotlib.pyplot as plt
import seaborn as sns
sns.set(rc={'figure.figsize':(11, 4)})


Now, let's take a look at how our mood changes daily. Calculate the average level of daily mood.

In [5]:
moodlevel_daily= mood['Level'].resample('D')
moodlevel_daily_mean = moodlevel_daily.mean()


Then, compare the daily average mood with the mood log.

In [6]:
fig, ax = plt.subplots(figsize=(30, 14))
marker='.', linestyle='-', linewidth=0.5, label='Mood Level')
marker='o', markersize=8, linestyle='-', label='Daily Mean')

To explore how your mood changes everyday and also compare across the whole period, let's use heat map to plot in a more colorful way. First, we need to convert the incomplete time list into a complete time list. The mood level of the moment will be recorded as NAN if there isn't any data point at that time.

In [7]:
mood.loc[mood.Minute>30,'Hour']= mood['Hour'] + 1        
hourly_mood = mood[['Date','Day of week','Hour','Level']]
In [8]:
def get_date_list(begin_date,end_date):
    date_list = [x.strftime(datetime_format) for x in list(pd.date_range(start=begin_date, end=end_date, freq='H'))]
    return date_list

# MPB note: can the dates be inferred?
Time_list = pd.DataFrame({'DateTime':get_date_list('2019-04-24','2019-07-08')})
Time_list['DateTime'] = pd.to_datetime(Time_list['DateTime'], format=datetime_format)
Time_list['Time'] = Time_list['DateTime'].apply(lambda x: x.strftime(date_format)) + ' ' + Time_list['DateTime'].apply(lambda x: x.strftime('%H'))
Time_list['Date']= Time_list['DateTime'].apply(lambda x: x.strftime(date_format) )

hourly_mood['Date'] = pd.to_datetime(hourly_mood['Date'], format=date_format)
hourly_mood['Time'] = hourly_mood['Date'].apply(lambda x: x.strftime(date_format)) + ' ' + hourly_mood['Hour'].apply(lambda x: str(x))
hourly_mood = hourly_mood.drop(['Date'],axis=1)
/opt/conda/lib/python3.6/site-packages/ SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation:
  # This is added back by InteractiveShellApp.init_path()
/opt/conda/lib/python3.6/site-packages/ SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation:
  if sys.path[0] == '':
In [9]:
Hourly_mood = pd.merge(Time_list, hourly_mood, how='left', on='Time' )
Hourly_mood_heatmap = Hourly_mood.drop(['Time','Date','Day of week','Hour'], axis=1)
Hourly_mood_heatmap = Hourly_mood_heatmap.set_index('DateTime')

Now, we have our complete hourly mood log. Let's transpose the matrix first.

In [10]:
groups = Hourly_mood_heatmap.groupby(pd.Grouper(freq='D'))
Hourly_mood_heatmap = pd.concat([pd.DataFrame(x[1].values) for x in groups], axis=1)
Hourly_mood_heatmap= pd.DataFrame(Hourly_mood_heatmap)
Hourly_mood_heatmap.columns = Hourly_mood['Date'].drop_duplicates(keep='first', inplace=False)

Let's use heatmap to provide a more intuitive, left-to-right data layout, with each row representing the hour and each column representing the day. Color Red stands for good mood, the more red the better. Color Blue stands for bad mood, while color Green means your mood was so-so at that time.

In [11]:
plt.matshow(Hourly_mood_heatmap, interpolation=None, cmap='jet', vmin=1, vmax=8)
plt.ylabel('Time of a Day',fontsize=14)
plt.xticks(np.arange(Hourly_mood_heatmap.shape[1]),Hourly_mood_heatmap.columns, rotation=90)

We can also further explore your mood changes within one day. For example, We can see there are lots of changes on 2019-5-19. Let's get your mood log of that day first.

In [12]:
Daily_mood = mood.loc['2019-6-19']

Let's try to plot your mood changes within that day.

In [13]:

To further explore the variation of the mood during this period, we will try to use Pareto chart to highlight the most representative mood levels over the whole period. First, we need to caculate the frequency of the levels of mood.

In [14]:
Levels = mood.groupby('LevelText', as_index=False)[['Date']].count()
#Levels['LevelText'] = Levels['LevelText'].apply(str)
LevelText Date
0 Bad 20
1 Good 127
2 Great 3
3 Meh 21
4 Okay 130
5 So-so 58
6 Very bad 1
7 Very good 30

Then let's plot the frequency in Bar chart.

In [15]:
Levels.plot(kind='bar', x='LevelText', y='Date', legend=None, title='Frequency of Mood levels')

Now, we can use a pareto chart to represnt both the frequency and the cumulative percentage of the mood levels.

In [16]:
def create_pareto_plot(df, x=None, y=None, title=None, show_pct_y=False, pct_format='{0:.0%}'):
    xlabel = x
    ylabel = y
    tmp = df.sort_values(y, ascending=False)
    x = tmp[x].values
    y = tmp[y].values
    weights = y.cumsum() / y.sum()
    fig, ax1 = plt.subplots(), y)

    ax2 = ax1.twinx()
    ax2.plot(x, weights, '-ro', alpha=0.5)
    ax2.set_ylabel('', color='r')
    ax2.tick_params('y', colors='r')
    vals = ax2.get_yticks()
    ax2.set_yticklabels(['{:,.2%}'.format(x) for x in vals])
    formatted_weights = [pct_format.format(x) for x in weights]
    for i, txt in enumerate(formatted_weights):
        ax2.annotate(txt, (x[i], weights[i]), fontweight='heavy')    
    if not show_pct_y:
    if title:
In [17]:
create_pareto_plot(Levels, x='LevelText', y='Date', title='Pareto Chart of Mood Level Frequency')

Events happened in someone's daily life can influence his/her mood. Let's highlight the period and add annotations to help you understand your own mood. Please input the time period and the event happened during the period below.

In [18]:
time_periods = [
        "start": "2019-07-01",
        "end": "2019-07-04",
        "label": "something happened",

Now, we can grab this period and the event.

In [19]:
period_selected = mood[time_periods[0]["start"]:time_periods[0]["end"]]
period_selected_event = time_periods[0]["label"]

Let's take a look at the peak and nadir of your mood during this period.

In [20]:
mood_period_max = period_selected['Level'].max()
mood_period_max_idx = period_selected['Level'].idxmax(axis=0, skipna=True)
print('The moment that your felt best during the period:',mood_period_max_idx)
#mood_period_max_event = input('please input the event happened when you felt best during the period:')

mood_period_min = period_selected['Level'].min()
mood_period_min_idx= period_selected['Level'].idxmin(axis=0, skipna=True)
print('The moment that your felt worse during the period:',mood_period_min_idx)
#mood_period_min_event = input('please input the event happened when you felt worse during the period:')
The moment that your felt best during the period: 2019-07-02 12:20:00
The moment that your felt worse during the period: 2019-07-04 20:09:00

According to the time points above, you can input the events happened at that moment below.

In [21]:
events = [
        "event_mood_max": "thing A",
        "event_mood_min": "thing B"

Now, we can highlight the period you selected and add your notes to the plot.

In [22]:
fig, ax = plt.subplots(figsize=(30, 14))
ax.plot(mood['Level'],marker='.', linestyle='-', linewidth=0.5, label='Mood Level')
ax.plot(moodlevel_daily_mean,marker='o', markersize=8, linestyle='-', label='Daily Mean')
ax.axvspan(time_periods[0]["start"], time_periods[0]["end"], color=sns.xkcd_rgb['grey'], alpha=0.5)
ax.set_title('Mood Changes Over Time')


ax.legend(loc='upper left', fontsize=11, frameon=True).get_frame().set_edgecolor('blue')  

bbox_props0 = dict(boxstyle='square, pad=0.6', fc='mediumvioletred', ec='r', alpha=.4, lw=.5)

ax.text(time_periods[0]["start"], 9, 'Event happened during this period:\n{}'.format(period_selected_event) , size=12,ha='left',
        family = 'serif', color='yellow', style = 'italic', weight = 'bold', bbox = bbox_props0)

bbox_props1 = dict(boxstyle='round4, pad=0.6', fc='cyan', ec='b', lw=.5)

ax.annotate('Mood Max = {}\nEvent = {}\nDate = {}'
                 .format(mood_period_max, events[0]["event_mood_max"], mood_period_max_idx.strftime('%a, %Y-%m-%d')),
            xy=(mood_period_max_idx, mood_period_max),  
            xytext=(-150, -30),      
            textcoords='offset points',
            arrowprops=dict(arrowstyle="->"), bbox=bbox_props1)    

ax.annotate('Mood Min = {}\nEvent = {}\nDate = {}'
                 .format(mood_period_min, events[0]["event_mood_min"], mood_period_min_idx.strftime('%a, %Y-%m-%d')),
            xy=(mood_period_min_idx, mood_period_min),  
            xytext=(-150, 30),      
            textcoords='offset points',
            arrowprops=dict(arrowstyle="->"), bbox=bbox_props1) 


Let's take a look at the tags you added now.

In [23]:
tags = list(mood.columns.values)[8:]
In [63]:
tag_sum = pd.DataFrame(mood[tags].apply(lambda x: x.sum()))
tag_sum['tags'] = tag_sum.index.values 
tag_sum.columns = ['frequency','tags']
In [64]:
tag_sum.plot(kind='bar',x='tags',y='frequency', legend=None, title='Frequencies of Mood Tags')
In [69]:
!pip install wordcloud
Collecting wordcloud
  Downloading (361kB)
    100% |████████████████████████████████| 368kB 1.5MB/s eta 0:00:01
Requirement already satisfied: pillow in /opt/conda/lib/python3.6/site-packages (from wordcloud)
Requirement already satisfied: numpy>=1.6.1 in /opt/conda/lib/python3.6/site-packages (from wordcloud)
Installing collected packages: wordcloud
Successfully installed wordcloud-1.5.0
You are using pip version 9.0.1, however version 19.1.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
In [72]:
from PIL import Image, ImageSequence
from wordcloud import WordCloud

def DrawWordcloud(df):
    wc = WordCloud(background_color = 'White',width=1000, height=860, margin=2)
    name = list(df.tags)
    value = df.frequency
    for i in range(len(name)):
        name[i] = str(name[i])
    dic = dict(zip(name, value))

In [ ]: