Skip to content

Commit

Permalink
Merge branch 'dataprod'
Browse files Browse the repository at this point in the history
* dataprod:
  More yhat stuff
  Add annual mean pollution example
  Start playing with yhat
  • Loading branch information
rdpeng committed Apr 25, 2014
2 parents 11d7470 + 51d8496 commit 0597b45
Show file tree
Hide file tree
Showing 8 changed files with 69,990 additions and 0 deletions.
99 changes: 99 additions & 0 deletions 09_DevelopingDataProducts/yhat/AP_example.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
## Create dataset of PM and O3 for all US taking year 2013 (annual
## data from EPA)

## This uses data from
## http://aqsdr1.epa.gov/aqsweb/aqstmp/airdata/download_files.html

## Read in the 2013 Annual Data

d <- read.csv("annual_all_2013.csv", nrow = 68210)

sub <- subset(d, Parameter.Name %in% c("PM2.5 - Local Conditions", "Ozone")
& Pullutant.Standard %in% c("Ozone 8-Hour 2008", "PM25 Annual 2006"),
c(State.Code, County.Code, Site.Num, Longitude, Latitude, Parameter.Name, Arithmetic.Mean, County.Name, City.Name, Pullutant.Standard))

pollavg <- aggregate(sub[, "Arithmetic.Mean"],
sub[, c("Longitude", "Latitude", "Parameter.Name")],
mean, na.rm = TRUE)
pollavg$Parameter.Name <- factor(pollavg$Parameter.Name, labels = c("ozone", "pm25"))
names(pollavg)[4] <- "level"

## Remove unneeded objects
rm(d, sub)

## library(fields)
rdist <- function (x1, x2, miles = TRUE, R = NULL) {
if (is.null(R)) {
if (miles)
R <- 3963.34
else R <- 6378.388
}
coslat1 <- cos((x1[, 2] * pi)/180)
sinlat1 <- sin((x1[, 2] * pi)/180)
coslon1 <- cos((x1[, 1] * pi)/180)
sinlon1 <- sin((x1[, 1] * pi)/180)
if (missing(x2)) {
pp <- cbind(coslat1 * coslon1, coslat1 * sinlon1, sinlat1) %*%
t(cbind(coslat1 * coslon1, coslat1 * sinlon1, sinlat1))
return(R * acos(ifelse(abs(pp) > 1, 1 * sign(pp), pp)))
}
else {
coslat2 <- cos((x2[, 2] * pi)/180)
sinlat2 <- sin((x2[, 2] * pi)/180)
coslon2 <- cos((x2[, 1] * pi)/180)
sinlon2 <- sin((x2[, 1] * pi)/180)
pp <- cbind(coslat1 * coslon1, coslat1 * sinlon1, sinlat1) %*%
t(cbind(coslat2 * coslon2, coslat2 * sinlon2, sinlat2))
return(R * acos(ifelse(abs(pp) > 1, 1 * sign(pp), pp)))
}
}

monitors <- data.matrix(pollavg[, c("Longitude", "Latitude")])


pollutant <- function(df) {
x <- data.matrix(df[, c("lon", "lat")])
r <- df$radius
d <- rdist(monitors, x)
use <- lapply(seq_len(ncol(d)), function(i) {
which(d[, i] < r[i])
})
levels <- sapply(use, function(idx) {
with(pollavg[idx, ], tapply(level, Parameter.Name, mean))
})
dlevel <- as.data.frame(t(levels))
data.frame(df, dlevel)
}

library(yhatr)

model.transform <- function(df) {
df
}

model.predict <- function(df) {
pollutant(df)
}

yhat.config <- c(
username="[email protected]",
apikey="90d2a80bb532cabb2387aa51ac4553cc",
env="http://sandbox.yhathq.com/"
)

yhat.deploy("pollutant")



################################################################################
## Client side

library(yhatr)
yhat.config <- c(
username="[email protected]",
apikey="90d2a80bb532cabb2387aa51ac4553cc",
env="http://sandbox.yhathq.com/"
)
df <- data.frame(lon = c(-76.6167, -118.25), lat = c(39.2833, 34.05),
radius = 20)
yhat.predict("pollutant", df)
68,202 changes: 68,202 additions & 0 deletions 09_DevelopingDataProducts/yhat/annual_all_2013.csv

Large diffs are not rendered by default.

22 changes: 22 additions & 0 deletions 09_DevelopingDataProducts/yhat/environment.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
library(yhatr)

model.require <- function() {

}

model.transform <- function(df) {
df
}

model.predict <- function(df) {
data.frame(version = R.version.string, stringsAsFactors = FALSE)
}

yhat.config <- c(
username="[email protected]",
apikey="90d2a80bb532cabb2387aa51ac4553cc",
env="http://sandbox.yhathq.com/"
)


yhat.deploy("environment")
30 changes: 30 additions & 0 deletions 09_DevelopingDataProducts/yhat/front.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<!DOCTYPE HTML>
<html>
<head>
<meta name="viewport" content="width=device-width">
<title>What Am I Breathing?</title>
<link href="/static/css/main.css" rel="stylesheet" type="text/css"></link>
</head>
<body>
<div class="container">
<form id="rec">
Latitude <input id="lat" type="text" /><br />
Longitude <input id="lon" type="text" /><br />
Radius (mi) <input id="radius" type="text" /><br />
<button class="btn" type="submit">Go!</button>
</form>
<section class="results">
<p class="lead">Top Ten Recommended Beers</p>
<figure class="visualization row">
<div class="col-sm-6 col-1"></div>
<div class="col-sm-6 col-2"></div>
</figure>
</section>
<pre id="result" class="language-javascript"><code class="language-javascript"></code></pre>
</div>
<script src="/static/js/jquery-1.10.2.min.js"></script>
<script src="/static/js/code.js"></script>
<script src="/static/js/beers.js"></script>
<script src="/static/js/main.js"></script>
</body>
</html>
24 changes: 24 additions & 0 deletions 09_DevelopingDataProducts/yhat/ozone.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
library(yhatr)

model.require <- function() {

}

model.transform <- function(df) {
df
}

model.predict <- function(df) {
data.frame(Ozone = predict(fit, newdata = df))
}

fit <- lm(Ozone ~ Wind + Temp, data = airquality)

yhat.config <- c(
username="[email protected]",
apikey="90d2a80bb532cabb2387aa51ac4553cc",
env="http://sandbox.yhathq.com/"
)


yhat.deploy("ozone")
4 changes: 4 additions & 0 deletions 09_DevelopingDataProducts/yhat/pollutant.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash

curl -X POST -H "Content-Type: application/json" --user [email protected]:90d2a80bb532cabb2387aa51ac4553cc --data '{"lon": $1, "lat": $2, "radius": $3 }' http://sandbox.yhathq.com/[email protected]/models/pollutant/

9 changes: 9 additions & 0 deletions 09_DevelopingDataProducts/yhat/predictozone.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash

echo $1
echo $2

curl -X POST -H "Content-Type: application/json" \
--user [email protected]:90d2a80bb532cabb2387aa51ac4553cc \
--data '{"Wind": $1, "Temp": $2 }' \
http://sandbox.yhathq.com/[email protected]/models/ozone/
Loading

0 comments on commit 0597b45

Please sign in to comment.