Processing and Analyzing Sensor Data – A DIY Approach (Part II)
Join the DZone community and get the full member experience.Join For Free
in a recent lab, we set up sensors in our office to collect our own machine-generated data (see this blog post). for the follow-up lab, our team split up into three groups. two of us set out to implement a nice real-time visualization of our data, another colleague investigated and benchmarked different document schemas for storing time series data in mongodb (see this post on the mongosoup blog), while the remaining two started analysing the data we collected.
the sensors we set up delivered two types of data. we dubbed data of the first type (temperature and sound intensity) “stream data”, while we called data of the second type (touch and motion detector data) “event data”. stream data are characterized by the fact that they are generated steadily, while event data only occur when an event (person touching the coffee machine, walking past the kitchen) triggers them.
regarding data analysis, we handled the two types of data quite differently. to understand the stream data we applied concepts from time series analysis, while we approached the event data with markov chain models. we used r for both approaches.
analysing the stream data
at first glance, we noted that the temperature data that was written to the database was quite sparse, with lots of missing values. closer inspection revealed that there are two types of missing values. the first type occured in longer stretches (typically half or full days), while the second type was spread evenly across the whole observation period. missing values of the first type were due to the raspberry pi crashing (duh), while the those of the second type were due to the way the sensors emit data. namely, if the current value is not different from the one observed 100 milliseconds before, no value is transmitted. while there was no way recovering the missing data of the first type, the values of the second type could easily be reconstructed by replacing the missing value with the last observed value.
in the next step, we coarse-grained the data, keeping only one observation per minute. this seemed reasonable since we didn’t notice significant differences between temperature fluctuations at the 100ms and 1s scales and those at the minute scale.
the following graph shows the temperature data on the minute level from may 14th until may 22nd, 2014 – a period without longer stretches of missing data of the first type. firstly, we note that during the first seven days, the temperature varies only moderately between 23° and 24° celsius. during this period, the weather outside was mild but not too hot. you can see some variation of the temperature over periods of 24 hours, but of course this is less pronounced than the variation in the temperature outdoor, since the building is insulated. during day 7 though (may 20th), a trend emerges which sees the average temperature rising. also, the temperature variation during 24 hours become more distinct. this was when the warm summer wheather kicked in in southern germany.
looking at the auto-correlation function of the time series (lower left panel), you can note distinct peaks at around 1400 and 2800 lags. since we’re dealing with one observation per minute, those lags correspond to one and two days, respectively. this is to be expected and shows that temperature patterns over 24 hours behave roughly the same for all days. also, if we hadn’t known in advance what kind of data we were dealing with, this shape of the auto-correlation functions would tell us that 24 hours seems to be a natural time scale hidden in the data.
to better understand the behaviour of the temperature data, we turned to a classical technique from time series analysis, namely an additive decomposition into trend, seasonal and random component. this decomposition is achieved by first applying a smoothing operation to the observed data (typically simple moving average) to extract the trend component. this is then subtracted fromt he observed data. to get the seasonal component, one divides the time series into blocks corresponding to one period (in this case, 24 hours), and then averages over corresponding values in each period. after the seasonal component has been extracted, it is subtracted, leaving only the random part of the time series.
the result of the decomposition is shown in the figure above. the second row clearly shows the trend, with a slight ditch at day 5 and the warm wheather kicking in from day 7.
the seasonal component in the third row shows the variation of temperature over the course of 24h. we note two things: firstly, it follows the expected pattern, rising from night to afternoon and falling afterwards. secondly, the absolute variations are quite small, spanning a range of only 0.6 degrees celsius.
the random component looks slightly weird. in general, the better the decomposition, the more we expect the random part to look like white noise, i.e. normally distributed data. this is not really the case here, since during the first seven days we see a recurring pattern of a drop followed by a period of rising values, and some bigger oscillations at days seven to nine.
a possible explanation is that due to the wheather change on day seven, the variations in the course of one day become more pronounced. this biases the estimation of the trend component, since on three of the nine days, the daily pattern is quite different from the other six. since there is only one seasonality pattern that is subtracted from the observations over all periods, this causes a model misfit that produces the quirks in the random part.
analysing the sound data
inspecting the sound intensity data as the second source of stream data we collected in our office, we found that it is characterized by patterns of short, intense bursts which seem to be distributed quite irregularly over the day, while at night everything remains quiet.
here’s an example for may 14th – may 16th:
what could be a natural time scale for seasonal effects, i.e. repeating patterns in these data? we tried decompositions of the sound intensity time series data in the fashion described above on various time scales, but could not identify any periods at which seasonality happens, apart from the rather trivial insight that it’s noisier at day time than at night.
the auto-correlation function acf (data from may 12th to 21st, aggregated to second level, frequency one hour) confirms this. although there are some smaller bumps, distinct maxima are lacking. the decorrelation length, i.e. the point at which the auto-correlation crosses zero for the first time, is roughly equal to 15 hours, and even at 24 hours the auto-correlation is negative instead of positive, indicating that even 24 hours is not a natural time scale for the data at hand.
after all, the pattern according to which people move around the office and produce noise seems to be quite irregular.
nevertheless, we noticed something interesting in the decomposition of the data from may 16th with a period of 15 minutes (see below). namely, in the night there seem to be small peaks at a regular period.
taking a closer look by isolating the data from the night between may 15th and may 16th and calculating the auto-correlation function confirmed this:
there are distinct maxima at the lags corresponding to one hour, two hours and so on. our conjecture is that these peaks are caused by a church bell ringing nearby.
analysing the event data
the event data we analyzed came from several sources: touch sensors at the handles of the coffee machine, the fridge and the toilet door, as well as a motion sensor which was placed at the corridor between offices and kitchen / toilet.
the following diagram shows the steps we had to perform in the preparation phase before we were able to analyze the event data by fitting it to a markov chain.
in the case of event data the raw data of the touch sensors caused some headache as the sensors are electrodes and influenced by the surrounding environment and natural electrical variations. this caused a lot of “flickering” and bogus events. so we needed to do various filtering steps.
the tinkerforge multitouch sensor offers the possibility to set a sensitivity and recalibrate the sensor. decreasing the sensitivity helped reducing bogus events, but the sensitivity could only be set once for all electrodes connected to the multitouch bricklet. since we have varying cable length between the electrodes and the multitouch bricklet, the sensitivity would have to be set individually. so we still had a lot of short “flickering” events in the database, which lead us to the next step
in the next step we did a pre-filtering on the database level for the multitouch sensors. these filtered events were written into a new mongodb collection keeping the original schema. this step was done once and was sufficient for our static analysis. for a real time analysis we need to think of a better solution.
specifically, events below the following durations were filtered out:
- toilet door sensor: 800ms
- coffee machine sensor: 600ms
- fridge sensor: 600ms
these thresholds were defined after comparing the duration of events during the night to the standard duration triggered by human usage which usually results in events of duration above 1000ms.
after filtering the events we had to transform the data so it could be used for a markov chain analysis of the sensor states. as we use r for this analysis, we also used r for the transformation.
we read the filtered events from mongodb (using the rmongodb package ) and extracted the states for every sensor and every second. this results in an overall state which represents the combination of single sensor states for every second. additionally we defined a zero state for the situation in which no sensor events occur.
for the analysis we were only interested in the transition probabilities between states and not in the duration of events. so we compressed succeeding identical states into one single state. this had the additional advantage that the noise introduced due to flickering events could be further reduced. here the zero state helped us to do some kind of start-stop detection of events. after a zero state has occurred it is of course possible to move into the same sensor state again.
these steps lead to the following data format, which is modeled by a data frame in r (1 = sensor on, 0 = off):
timestamp motion toilet fridge coffee state
1400659392 1 0 0 0 1000
1400659397 0 0 0 0 0000
1400659403 1 1 0 0 1100
1400659410 0 0 0 0 0000
1400660442 1 0 0 0 1000
1399505901 0 1 0 0 0100
1399506127 0 0 0 0 0000
1399506128 0 1 0 0 0100
1399506424 0 0 0 0 0000
1399506933 0 0 0 1 0001
remove overlapping events
since several persons move around in the office at the same time, a lot of events overlap. for the analysis of transition probabilities we focussed only on events which can be triggered by the same person one after the other. so we focussed now only on the states: “toilet on”, “motion on”, “fridge on”, “coffee on” and “motion on & toilet on”. the state “motion & toilet” can be triggered by one person because the motion sensor is always on for about five seconds and in this timespan the toilet door is opened, which is within reach of the motion sensor. this step is a strong simplification, but yields plausible results as shown below.
after having done the filtering and transformation we could use the r package markovchain to analyze event transition probabilities by simply calling
> markovchainfit(data = states, method="mle", name="event sensor mle")
where “states” contain our transformed sensor data which are fitted to the underlying markov chain distribution by maximum likelihood estimation (mle).
the result is a matrix with transition probabilities from one state to another. the markovchain package also provides a plot method for the transition matrix which plots the matrix as a graph.
in the graph displayed here we excluded the “toilet” states, since they were still to noisy and with “motion+toilet” we have a state which should be equal. we further removed all edges with transition probabilities below 0.03.
the result is a graph which fits quite well to the expected behaviour:
- every event is strongly related with motion
- coffee and fridge events are strongly related with each other
given that every event is related to a previous motion (as our physical setup requires) we detect 23% of the destinations of motion (given that the state sequences don’t overlap). undetected are for example people going to the printer (which is next to the motion sensor). for further analysis – and given the peculiarities of the sound intensity data discussed above – we want to define sound events based on certain sound intensities, which should enable us to detect printer events.
another idea for improvement would be to use the transition probabilities and try to detect overlapping event sequences, so we don’t have to filter out the events as described in step 5.
another interesting feature the markovchain package delivers is that we can simulate sequences of events given a start state and a (fitted) markov chain.
so if for example we start with the most common state “motion” and simulate the next 10 events:
> rmarkovchain(n=10, object=mc$estimate, t0 = "m")
we get the sequence
 "f" "c" "c" "m" "m" "m" "mt" "m" "mt" "mt"
which can be interpreted as follows: “someone went from his desk to the kitchen (start state ‘m’), opened the fridge, opened the pad slot of the coffee machine, closed it (and probably started the coffee machine and got his coffee) and went back to his place” and so on. this one simulation seems to be quite reasonable. another simulation run shows another result, e.g.
> rmarkovchain(n=10, object=mc$estimate, t0 = "m")  "m" "mt" "m" "m" "m" "f" "m" "m" "m" "m"
in addition to transition probabilities between two states also probabilities of longer sequences are interesting. e.g. the coffee machine handle is used two times when making coffee (opening and closing), and then the fridge is opened or vice versa. so we did a short trigram analysis (i.e. analyzing the distribution of frequencies of three successing states) with the r package “tau”. here e.g. the triple “coffee -> coffee -> fridge” was slightly leading in comparison to “fridge -> coffee -> coffee”.
all these results are only examples on a small sensor setting and can get more interesting and valuable with more complex setups.
however, we think that our analyses confronted us with many of the challenges and peculiarities that characterise real-world sensor data. and – as mentioned above – one of the most important lessons we learned is that data modelling and data preparation are such crucial ingredients to data science projects that they decide upon the fate of the whole endeavour. looking back, we think that within our setting (time- and equipment-wise), we handled them rather well.
we’ll continue this series of blog posts as the sensor projects keeps evolving…
Published at DZone with permission of Comsysto Gmbh, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.