Analyzing Online Activity and Sleep Patterns

Well, this is fascinating as well as alarming at the same time and makes us realize how much such a small piece of information can reveal about our daily life.

That’s all about insights from analyzing the online activity of Facebook friends .

If you are interested in learning how you can create the presented graphs in this section using Python, you can continue reading the the next section.

Otherwise, you might be interested to read [Coming Soon] Analyzing Social Network Graph.

Visualizing online activity of Facebook Friends using PythonPrerequisites:Python 3.

5PendulumMatplotlibNumpyFor the sake of simplicity and privacy I won’t be presenting actual data of my friends here.

For this tutorial, we will be using a sample dummy data with the same structure.

Let’s take a look at the structure of the data-set.

Collected data-set is a simple list of dictionaries containing “list of online friends” and the “timestamp” when the list was fetched , stored as JSON in a file.

[ { "timestamp": "2019-05-14T08:12:52.

313921+02:00", "friends": [ "John Doe", "John Roe" ] }, { "timestamp": "2019-05-14T08:13:49.

449888+02:00", "friends": [ "Jane Roe", "Jane Doe" ] }, .


]Load the DataLet’s load the sample data and perform some initial processing:Above code would give us “timestamps”, a Python dictionary of (friend_name, times) pairs where times is a list of pendulum.

datetime objects representing logged timestamps when a particular friend was online.

Next step is to convert the dict into a nested dict with grouped timestamps for each friend by days.

Following code would do that:Above code does the following:It groups the list of pendulum.

datetime objects for each friend by unique days converting it into a dictionary of (day, times) pairs where times is a chronologically ordered list of timestamps.

Creates a nested Ordered Dictionary named “activities”, containing a nested dictionary of pairs (day, times).

Result is a nested dictionary of following format:{ "Jane Roe": { "We 15/05/19": [DateTime,.

,], "Th 16/05/19": [DateTime,.

,] }, "Jane Doe": { "We 15/05/19": [DateTime,.

,], "Th 16/05/19": [DateTime,.

,] }, .

}Given the above dictionary what we want to do is to create a binary map representing the online status of each friend over each day.

Let’s do that.

Don’t worry if it looks scary to you, I have also presented a summary of the code after the snippet.

Above code is essentially converting the list of timestamps for each friend for each day into a binary map with 1 representing online and 0 representing offline in intervals of 5 mins over 24 hours of the day.

It does the following:Creates 289 bins for a day.

Each timestamp is put into the appropriate bin (but the frequency is not counted).

Creates a Numpy array of shape (4, 2, 289) named online_maps because there are 4 friends and 2 days.

Days are chronologically ordered for creating the array.

To familiarize ourselves with the concept of bins, let’s say we divide a day into 24 parts/intervals i.

e bins, 1 part for each hour.

If user was online at any time from 12:00 midnight to 1:00 AM, we will set the value of first bin as 1, otherwise 0.

Doing this for all 24 intervals would result in a list of integers (which we call a binary map because value of each integer is either 0 or 1) representing the status of user being online or offline for every hour of the day.

We now have our data in a pretty good format i.

e activity maps for days for each friend.

Let’s plot some graphs using the formatted data.

Plot active statusWe can plot the online activity of a single friend over different hours of different days.

Following code plots the activity of the first friend in the data.

This should plot the following graph, where the presence of blue vertical bar represents being online and the absence means offline.

Figure 6: Online status of a friend across different days of the weekInteresting so far?Let’s plot another graph where we visualize the total number of online friends over different times of the day.

Plot no.

of online friendsWe can get the number of friends online over the day by calculate sum across the first dimension of online_maps array i.

e across friends:>>> online_count = np.

sum(online_maps, axis=0)>>> print(online_count.

shape)(2, 289)Following code then plots a line chart showing no.

of friends online for different days.

For visuals sake, we will also apply Gaussian smoothing to each curve.

Running above code results in the following plot:Figure 7: No.

of online friends of different hours of multiple days in the weekThere is so much more we can do with this simple set of information (some of which I will also discuss in later parts of this series) but this tutorial should help you get started with the basics.

The Jupyter Notebook with complete code from this post can be found here.

I hope this post was helpful for your work and research.

If you have any questions, feel free to write a comment below.

Read related stories:Crawling your Facebook friends’ dataAnalyzing Online Activity and Sleep Patterns[Coming Soon]: Analyzing my Social Network Graph.. More details

Leave a Reply