2017-01-20 19:51:20 +04:00
|
|
|
import time
|
|
|
|
import matplotlib.pyplot as plt
|
2017-02-08 19:23:41 +04:00
|
|
|
import dill
|
2017-02-24 20:29:55 +04:00
|
|
|
import numpy as np
|
2017-01-20 19:51:20 +04:00
|
|
|
|
|
|
|
|
|
|
|
current_milli_time = lambda: int(round(time.time() * 1000))
|
|
|
|
|
|
|
|
|
|
|
|
def uniquefilename(name):
|
|
|
|
if '.' in name:
|
|
|
|
tmp = name.split('.')
|
|
|
|
return tmp[0] + str(current_milli_time()) + '.' + tmp[1]
|
|
|
|
else:
|
|
|
|
return name + str(current_milli_time())
|
|
|
|
|
|
|
|
|
2017-01-27 14:26:47 +04:00
|
|
|
def showAndSaveImage(fig,file,flag,lgd=None):
|
2017-05-09 17:27:47 +04:00
|
|
|
"""
|
|
|
|
Show and image and save on file
|
|
|
|
:param fig: Matplotlib Figure object
|
|
|
|
:param file: filename to save the picture
|
|
|
|
:param flag: if True the image will be saved
|
|
|
|
:param lgd: legend
|
|
|
|
"""
|
2017-01-20 19:51:20 +04:00
|
|
|
if flag:
|
|
|
|
plt.show()
|
2017-01-27 14:26:47 +04:00
|
|
|
if lgd is not None:
|
|
|
|
fig.savefig(uniquefilename(file), additional_artists=lgd,bbox_inches='tight') #bbox_extra_artists=(lgd,), )
|
|
|
|
else:
|
|
|
|
fig.savefig(uniquefilename(file))
|
2017-01-30 03:59:50 +04:00
|
|
|
plt.close(fig)
|
|
|
|
|
|
|
|
|
|
|
|
def enumerate2(xs, start=0, step=1):
|
|
|
|
for x in xs:
|
|
|
|
yield (start, x)
|
2017-02-08 19:23:41 +04:00
|
|
|
start += step
|
|
|
|
|
2017-05-09 17:27:47 +04:00
|
|
|
|
2017-02-27 22:53:29 +04:00
|
|
|
def sliding_window(data, windowsize, train=0.8, inc=0.1):
|
2017-05-09 17:27:47 +04:00
|
|
|
"""
|
|
|
|
Sliding window method of cross validation for time series
|
|
|
|
:param data: the entire dataset
|
|
|
|
:param windowsize: window size
|
|
|
|
:param train: percentual of the window size will be used for training the models
|
|
|
|
:param inc: percentual of data used for slide the window
|
|
|
|
:return: window count, training set, test set
|
|
|
|
"""
|
2017-02-24 20:29:55 +04:00
|
|
|
l = len(data)
|
|
|
|
ttrain = int(round(windowsize * train, 0))
|
2017-02-27 22:53:29 +04:00
|
|
|
ic = int(round(windowsize * inc, 0))
|
|
|
|
for count in np.arange(0,l-windowsize+ic,ic):
|
|
|
|
if count + windowsize > l:
|
|
|
|
_end = l
|
|
|
|
else:
|
|
|
|
_end = count + windowsize
|
|
|
|
yield (count, data[count : count + ttrain], data[count + ttrain : _end] )
|
2017-02-24 20:29:55 +04:00
|
|
|
|
2017-02-08 19:23:41 +04:00
|
|
|
|
|
|
|
def persist_obj(obj, file):
|
2017-05-09 17:27:47 +04:00
|
|
|
"""
|
|
|
|
Persist an object on filesystem. This function depends on Dill package
|
|
|
|
:param obj: object on memory
|
|
|
|
:param file: file name to store the object
|
|
|
|
"""
|
2017-02-08 19:23:41 +04:00
|
|
|
with open(file, 'wb') as _file:
|
|
|
|
dill.dump(obj, _file)
|
|
|
|
|
2017-05-09 17:27:47 +04:00
|
|
|
|
2017-02-08 19:23:41 +04:00
|
|
|
def load_obj(file):
|
2017-05-09 17:27:47 +04:00
|
|
|
"""
|
|
|
|
Load to memory an object stored filesystem. This function depends on Dill package
|
|
|
|
:param file: file name where the object is stored
|
|
|
|
:return: object
|
|
|
|
"""
|
2017-02-08 19:23:41 +04:00
|
|
|
with open(file, 'rb') as _file:
|
|
|
|
obj = dill.load(_file)
|
|
|
|
return obj
|
|
|
|
|
|
|
|
def persist_env(file):
|
2017-05-09 17:27:47 +04:00
|
|
|
"""
|
|
|
|
Persist an entire environment on file. This function depends on Dill package
|
|
|
|
:param file: file name to store the environment
|
|
|
|
"""
|
2017-02-08 19:23:41 +04:00
|
|
|
dill.dump_session(file)
|
|
|
|
|
|
|
|
def load_env(file):
|
|
|
|
dill.load_session(file)
|