Perl scripts analyze Jawbone UP data

Graphing Activity

With all this data available, you can now indulge in all kinds of neat experiments. For attractive charts, you just need a graphics package such as Chart::Clicker [4] to generate aesthetically pleasing results without much manual work. Figure 6 shows two graphs in a chart with the distance covered during the past 24 hours in purple and the steps measured dynamically at fixed intervals in yellow.

Figure 6: Distance traveled in the past 24 hours (purple) and steps counted in dynamically selected measurement intervals (yellow).

To save space on the device, the UP saves data sets in measurement intervals of varying length: If nothing happens for a long time, while the wearer is sleeping or watching television, the time window with the movement count can be one hour or longer. At full speed, however, the bracelet writes an entry to memory every minute to record the number of steps. The UP server estimates the distance covered by the UP wearer, the average speed, the calories burned during the activity, and the number of seconds in which the user was active in the time interval.

All of this information is tapped by the script in Listing 2 and rendered in graphical form using the chart package. The Chart::Clicker package from CPAN draws beautiful curves and is so flexible that it can even bundle two types of graphs in a single chart, as shown in Figure 6.

Listing 2

bandchart

 

A rendering engine called Chart::Clicker::Renderer::Area plots the number of meters covered over a timeline as a filled area (purple in Figure 6), and the graph shows the spikes of step count values as thin bars in yellow, also over the time axis.

On the Move

Left to its own devices, Chart::Clicker represents data as xy coordinates. If the x-values take the form of dates in Unix seconds, the Chart::Clicker::Axis::DateTime module ensures that they appear on the x-axis in a neat day or time data format.

The script uses the band() method in line 31 to fetch the raw data for the past 24 hours from the bracelet. Optionally, an arbitrary time window could be defined. The results are an array of entries, each containing a reference to a hash  – with steps for the number of steps per period of time, distance for the distance traveled, and speed for the average speed during the measurement period. The time method fetches the timestamp, active_time the portion of the time window in which the user was active, and calories the number of calories burned.

The script then fills three arrays with the retrieved values: @time with the timestamps in Unix format, @steps with the step count at these times, and @distance with the total distance covered up to the point in time. Lines 56 and 70 generate one data series each of the type Data::Series from the time-step or time-distance combinations.

A record of the Data::DataSet type for a graph can store multiple data series in Chart::Clicker, but lines 56 and 64 respectively generate one data set with only one series each for the two graphs. The script wants to render these differently – one as an area graph and the other as a bar chart  – but in the same diagram.

Rendering in Context

Chart::Clicker works in contexts to render the graphs; default is the preset, and line 53 picks up an object of the same name. To render the second data set, line 80 defines an additional context named steps; line 83 adds it to the system. Lines 88 and 89 then call add_to_datasets() to add both series to the Clicker, and the domain_axis() method assigns the two to the Axis::DateTime time axis via the context. The time axis then interprets the x values of data series as date values and renders them accordingly.

The %H:%M specification tells the formatter to plot only the hours and minutes of the Unix date in the axis. The time zone America/Los_Angeles is for the San Francisco area and may need to be adapted to your location. The time zone name matches the Olson database on which the DateTime module is based; the Wikipedia page [5] lists the values for Europe and the rest of the world.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Data Visualization in Python

    Python's powerful Matplotlib, Bokeh, PyQtGraph, and Pandas libraries lend programmers a helping hand when visualizing complex data and their relationships.

  • Google Chart

    The Google Chart API lets you draw custom graphs, charts, maps, and barcodes through a simple web interface.

  • Perl: Google Chart Instructions

    A CPAN module passes drawing instructions in object-oriented Perl to Google Chart, which draws visually attractive diagrams.

  • yEd

    Build complex data structures into simple but illuminating diagrams using the yEd graph editor.

  • ActivityDiary

    ActivityDiary creates a handy digital diary by mapping your workout and route GPS data.

comments powered by Disqus

Direct Download

Read full article as PDF:

Price $2.95

News