Skip to content

Commit

Permalink
smooth( ) function
Browse files Browse the repository at this point in the history
  • Loading branch information
rubae committed Sep 7, 2012
1 parent 951e34c commit e6b8435
Showing 1 changed file with 34 additions and 17 deletions.
51 changes: 34 additions & 17 deletions awesim/result.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from datetime import datetime, timedelta
import pandas
import pdb
import pickle


class Result(object):
Expand Down Expand Up @@ -54,6 +55,25 @@ def __init__(self, values, time=None, identifiers=None, **kwargs):

for k,v in kwargs.items():
setattr(self, k, v)

def save(self, filename):
"""
save(filename)
Save the Simdex object by pickling it with cPickle
To unpickle (= load) use the following command:
objectname = pickle.load(open(filename,'rb'))
# 'rb' stands for 'read, binary'
"""

f = file(filename,'wb')
# wb stands for 'write, binary'
pickle.dump(self, f)
f.close()

return filename + ' created'

def values(self):
"""
Expand Down Expand Up @@ -174,27 +194,24 @@ def smooth_by_time(signal, time, interval=300, label='left'):
This function can be used in the post-processing too.
"""

def make_datetimeindex(array_in_seconds, year):
"""
Create a pandas DateIndex from a time vector in seconds and the year.
"""

start = pandas.datetime(year, 1, 1)
datetimes = [start + pandas.datetools.timedelta(t/86400.) for t in array_in_seconds]

return pandas.DatetimeIndex(datetimes)
ratio = interval/(time[1]-time[0])

interval_string = str(interval) + 'S'
dr = make_datetimeindex(time, 2012)
df = pandas.DataFrame(data=signal, index=dr, columns=['signal'])
df5min = df.resample(interval_string, how=np.mean, closed=label, label=label)
time_index = np.arange(0, time[-1]+interval, interval)

return df5min
data = np.zeros(len(time_index))
data[0] = signal[0]
for i in range(1,len(data)):
data[i]=np.mean(signal[(i-1)*ratio:i*ratio])

return time_index, data

result = {}
value = {}
time = {}
for sid in self.simulations:
result[sid] = smooth_by_time(self.val[sid], self.time[sid], interval=interval)

value[sid], time[sid] = smooth_by_time(self.val[sid], self.time[sid], interval=interval)

result = Result(values=value, time=time)

return result

def plot(self, ylabel=None):
Expand Down

0 comments on commit e6b8435

Please sign in to comment.