Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding aggregate capabilities #3

Open
wants to merge 3 commits into
base: gh-pages
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 24 additions & 8 deletions buildings/power.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ def to_yday(date):
def to_day(year, yday):
return datetime.datetime.strftime(datetime.date(int(year),1,1) + datetime.timedelta(days=yday), "%Y-%m-%d")

def day_range(start_day, end_day):
"""Returns list of string-formatted YYYY-mm-dd dates between [start, end] (inclusive)"""
st = datetime.datetime.strptime(start_day,"%Y-%m-%d")
et = datetime.datetime.strptime(end_day,"%Y-%m-%d")
dates = rrule(DAILY, dtstart=st, until=et)
return list(map(lambda x: x.strftime('%Y-%m-%d'), dates))

def high(vals):
return np.percentile(vals, 95)
def low(vals):
Expand Down Expand Up @@ -165,10 +172,7 @@ def getday(self, day):
return TimeTable.from_table(ts, 'date')

def getdays(self, start_day, end_day):
st = datetime.datetime.strptime(start_day,"%Y-%m-%d")
et = datetime.datetime.strptime(end_day,"%Y-%m-%d")
dates = rrule(DAILY, dtstart=st, until=et)
dates = list(map(str, dates))
dates = day_range(start_day, end_day)
q = {
"buildings": self.vsites,
"classes": self.vclasses,
Expand All @@ -189,7 +193,19 @@ def __init__(self, host, sites, classes):
self.vhost = host
self.vsites = sites
self.vclasses = classes


def aggregate(self, days):
"""Returns [site, units, day, count, min, p5, median, mean, p95, max] for each day"""
q = {
'buildings': self.vsites,
'dates': days,
}
r = requests.post(self.vhost + '/aggregate', json = q)
if (not r.ok):
raise Exception ('aggdays request', r.reason)
df = Table.read_table(io.StringIO(r.text))
return TimeTable.from_table(df, 'day')

def getday(self, day, cache = "./data/"):
enddate = datetime.datetime.strptime(day,"%Y-%m-%d")+datetime.timedelta(days=1)
q = {
Expand All @@ -203,7 +219,7 @@ def getday(self, day, cache = "./data/"):
dpath = cache + ("".join(self.vsites) + day + ".csv").replace(" ","_").replace("/","-")
if (cache and path.exists(dpath)) :
df = Table.read_table(dpath)
else:
else:
r = requests.post(self.vhost + '/data', json = q)
if (not r.ok):
raise Exception ('getday request', r.status_code)
Expand Down Expand Up @@ -232,7 +248,7 @@ def getdays(self, start_day, end_day, cache='./data/'):
"start": start_day,
"end": end_day
}

if (cache) :
dpath = cache + ("".join(self.vsites) + start_day + ":" + end_day + ".csv").replace(" ","_").replace("/","-")
if (cache and path.exists(dpath)) :
Expand All @@ -244,7 +260,7 @@ def getdays(self, start_day, end_day, cache='./data/'):
df = Table.read_table(io.StringIO(r.text))
if (cache) :
df.to_csv(bpath)

units = df['units'][0]
metadata = {'units' : units,
'start' : start_day,
Expand Down