Last active
October 4, 2016 21:39
-
-
Save dalanmiller/1cd4fc913d070170c1b9 to your computer and use it in GitHub Desktop.
Wrote these queries as example queries on sensor data - http://rethinkdb.com/blog/temperature-sensors-and-a-side-of-pi/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import rethinkdb as r | |
from datetime import datetime, timedelta | |
conn = r.connect("localhost", 28015, db="telemetry_pi") | |
## | |
#Finding the average temperature & humidity for the past 24 hours | |
## | |
day_ago = datetime.now() - timedelta(hours=24) | |
cursor = r.table("observations")\ | |
.filter(r.row("datetime") > day_ago))\ | |
"avg_humidity": r.avg(r.row("temp")), | |
.merge({ | |
"avg_temperature": r.avg(r.row("humidity")) | |
}).run(conn) | |
## | |
#Finding the hottest observations per day (or a _single_ maxima for each day) | |
## | |
cursor = r.table("observations").group( | |
lambda doc: return [ | |
doc('datetime').year(), | |
doc('datetime').month(), | |
doc('datetime').day() | |
])\ | |
.max("temp")\ | |
.ungroup()\ | |
["reduction"] | |
## | |
#Finding some basic statistics and calculating a simple linear regression (y_humidity = alpha + beta_temp * temp) | |
## | |
# https://en.wikipedia.org/wiki/Simple_linear_regression | |
# https://en.wikipedia.org/wiki/Pearson_product-moment_correlation_coefficient | |
## | |
#To dramatically shorten queries | |
obs = r.db("telemetry_pi").table("observations") | |
#Start with initial object containing calculations that don't have dependencies | |
r.object( | |
#Sums | |
"sum_humidity", obs.sum("humidity"), | |
"sum_temp", obs.sum("temp"), | |
#Averages | |
"avg_humidity", obs.avg("humidity"), | |
"avg_temp", obs.avg("temp"), | |
#Sum of Squares | |
"sumsq_humidity",obs["humidity"].map(lambda d: return d * d).sum(), | |
"sumsq_temp", obs["temp"].map(lambda d: return d * d ).sum(), | |
"sum_products_temp_humidity", r.map( | |
obs["temp"], | |
obs["humidity"], | |
lambda temp, humidity: | |
return temp * humidity | |
).sum(), | |
#n (for convenience) | |
"n", obs.count() | |
#Now calculate beta estimator and standard deviations and Pearson correlation | |
).do( lambda doc: | |
//Calculate the squared averages | |
return doc.merge({ | |
#Estimated beta for temperature | |
"beta_hat": | |
#Numerator | |
((doc("n") * doc("sum_products_temp_humidity")) - (doc("sum_humidity") * doc("sum_temp"))) / | |
#Denominator | |
(doc("n") * doc("sumsq_temp")) - (doc("sum_temp") * doc("sum_temp")) | |
, | |
#Standard Deviation calculations | |
"sd_humidity": obs["humidity"].map( lambda d: | |
return (d - doc("avg_humidity") * d - doc("avg_humidity")) | |
).avg().do(r.js('(function(x) { return Math.sqrt(x); })')), | |
"sd_temperature": obs["temp"].map( lambda d: | |
return (d - doc("avg_temp") * d - doc("avg_temp")) | |
).avg().do(r.js('(function(x) { return Math.sqrt(x); })')), | |
#Correlation calculation | |
"temp_humidity_correlation": | |
doc("sum_products_temp_humidity") / ((doc("sumsq_temp") * doc("sumsq_humidity")).do(r.js('(function(x) { return Math.sqrt(x); })'))) | |
}) | |
#Finally calculate alpha estimator | |
).do(lambda doc: | |
return doc.merge({ | |
"alpha_hat": | |
((r.expr(1) / doc("n")) * doc("sum_humidity")) - (doc("beta_hat") * (r.expr(1) / doc("n")) * doc("sum_temp")) | |
}); | |
).run(conn) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment