Skip to content

Commit

Permalink
Merge pull request #21 from BBVA/develop
Browse files Browse the repository at this point in the history
Release 1.4
  • Loading branch information
SIPVZ authored Jan 9, 2019
2 parents 9b6f4ce + bebbc1f commit 9d41670
Show file tree
Hide file tree
Showing 59 changed files with 1,303 additions and 47 deletions.
Binary file modified Timecop_models.db
Binary file not shown.
Binary file added Timecop_modelsv1.db
Binary file not shown.
8 changes: 8 additions & 0 deletions config/app.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
VARIABLE_NAME = "variable value"
PORT = 80
DEBUG = True
DEVELOPMENT = True
SECRET_KEY = 'do-i-really-need-this'
SQLALCHEMY_DATABASE_URI= "sqlite:///Timecop_modelsv1.db"
DB_NAME= "sqlite:///Timecop_modelsv1.db"
ENV="development"
16 changes: 0 additions & 16 deletions config/config.toml

This file was deleted.

22 changes: 13 additions & 9 deletions engines/BBDD.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class Model(Base):
__tablename__ = 'models'
TS_name = Column(String(250), nullable=False,primary_key=True)
TS_winner_name = Column(String(250), nullable=False)
TS_model = Column(LargeBinary)
TS_model = Column(LargeBinary())
TS_model_params = Column(String(250))
TS_metric = Column(Numeric)
TS_update = Column('TS_update', DATETIME, index=False, nullable=False,primary_key=True,default=datetime.datetime.utcnow)
Expand All @@ -50,15 +50,15 @@ class TS(Base):
TS_update = Column('TS_update', DATETIME, index=False, nullable=False,primary_key=True,default=datetime.datetime.utcnow)


DB_NAME = 'sqlite:///Timecop_models.db'
DB_NAME = 'sqlite:///Timecop_modelsv1.db'
engine = create_engine(DB_NAME)
#self.__db.echo = True
Base.metadata.create_all(engine)


def get_ts(name):

DB_NAME = 'sqlite:///Timecop_models.db'
DB_NAME = 'sqlite:///Timecop_modelsv1.db'
engine = create_engine(DB_NAME)
DBSession = sessionmaker(bind=engine)
session = DBSession()
Expand All @@ -69,7 +69,7 @@ def get_ts(name):

def set_ts(name, data):

DB_NAME = 'sqlite:///Timecop_models.db'
DB_NAME = 'sqlite:///Timecop_modelsv1.db'
engine = create_engine(DB_NAME)

DBSession = sessionmaker(bind=engine)
Expand All @@ -83,19 +83,20 @@ def set_ts(name, data):

def new_model(name, winner, model,params,metric):

DB_NAME = 'sqlite:///Timecop_models.db'
DB_NAME = 'sqlite:///Timecop_modelsv1.db'
engine = create_engine(DB_NAME)

DBSession = sessionmaker(bind=engine)
session = DBSession()
print ("Model saved")
print(model)


new_model = Model(TS_name=name, TS_winner_name = winner, TS_model=model,TS_model_params= params,TS_metric=metric)
new_model = Model(TS_name=name, TS_winner_name = winner, TS_model=bytearray(model),TS_model_params= params,TS_metric=metric)
session.add(new_model)
session.commit()

def get_best_model(name):
DB_NAME = 'sqlite:///Timecop_models.db'
DB_NAME = 'sqlite:///Timecop_modelsv1.db'
engine = create_engine(DB_NAME)

DBSession = sessionmaker(bind=engine)
Expand All @@ -107,5 +108,8 @@ def get_best_model(name):
# print (mt.TS_name)
# print (mt.TS_winner_name)
# print (mt.TS_update)
salida = session.query(Model).filter(Model.TS_name == name).order_by(desc('TS_update')).first()
#filter(Note.message.like("%somestr%")
winner_model_type = session.query(Model).filter(Model.TS_name.like('winner%')).order_by(desc('TS_update')).first()

salida = session.query(Model).filter(Model.TS_name == name).filter(Model.TS_winner_name == winner_model_type.TS_winner_name).order_by(desc('TS_update')).first()
return ( salida.TS_winner_name,salida.TS_model,salida.TS_model_params)
Binary file modified engines/__pycache__/functions_timeseries.cpython-36.pyc
Binary file not shown.
Binary file modified engines/__pycache__/lstm.cpython-36.pyc
Binary file not shown.
16 changes: 8 additions & 8 deletions engines/functions_timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ def model_univariate(lista_datos,num_fut,desv_mse,train,name):


if not train:
filename = './models_temp/'+name
with open(filename,'r') as f:
winner = f.read()
f.close()
# filename = './models_temp/'+name
# with open(filename,'r') as f:
# winner = f.read()
# f.close()

(model_name,model,params)=get_best_model('winner_'+name)
# print ("recupero el motor " )
Expand Down Expand Up @@ -97,10 +97,10 @@ def model_univariate(lista_datos,num_fut,desv_mse,train,name):
winner=key
print(winner)

filename = './models_temp/'+name
with open(filename,'w') as f:
f.write(winner)
f.close()
# filename = './models_temp/'+name
# with open(filename,'w') as f:
# f.write(winner)
# f.close()
new_model('winner_'+name, winner, pack('N', 365),'',0)


Expand Down
11 changes: 9 additions & 2 deletions engines/lstm.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,16 +214,23 @@ def anomaly_uni_LSTM(lista_datos,num_forecast=10,desv_mse=2,train='True',name='t
models_dict[best_model].save('./models_temp/lstm.model'+name)
print ("insertando modelo LSTM")
with open('./models_temp/lstm.model'+name,'rb') as f:
mymodel = bytearray(f.read())
new_model(name, 'LSTM', mymodel,'',dict_mse_models[best_model])
mymodel = f.read()

new_model(name, 'LSTM', bytearray(mymodel),'',dict_mse_models[best_model])
f.close()
actual_model= models_dict[best_model]

else:

print ("Adquiring best LSTM model")
(model_name,mymodel,params)=get_best_model(name)
print("el modelo es")
print(model_name)
print (mymodel)
with open('./models_temp/lstm.model'+name, "wb") as newFile:
newFile.write(mymodel)
newFile.close()


actual_model= load_model('./models_temp/lstm.model'+name)

Expand Down
2 changes: 1 addition & 1 deletion models_temp/learned_model_holt_winterstest
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3
10,add
2 changes: 1 addition & 1 deletion models_temp/learned_model_holt_winterstest1
Original file line number Diff line number Diff line change
@@ -1 +1 @@
15
12,add
1 change: 1 addition & 0 deletions models_temp/learned_model_holt_winterstest12
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
12,add
1 change: 1 addition & 0 deletions models_temp/learned_model_holt_winterstest13
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
12,add
1 change: 1 addition & 0 deletions models_temp/learned_model_holt_winterstest22
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
12,add
1 change: 1 addition & 0 deletions models_temp/learned_model_holt_winterstest40
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
12,add
1 change: 1 addition & 0 deletions models_temp/learned_model_holt_winterstest44
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
12,add
2 changes: 1 addition & 1 deletion models_temp/learned_model_vartest
Original file line number Diff line number Diff line change
@@ -1 +1 @@
324
0
2 changes: 1 addition & 1 deletion models_temp/learned_model_vartest1
Original file line number Diff line number Diff line change
@@ -1 +1 @@
108
23
1 change: 1 addition & 0 deletions models_temp/learned_model_vartest12
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
23
1 change: 1 addition & 0 deletions models_temp/learned_model_vartest13
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
23
1 change: 1 addition & 0 deletions models_temp/learned_model_vartest14
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
23
1 change: 1 addition & 0 deletions models_temp/learned_model_vartest15
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
36
1 change: 1 addition & 0 deletions models_temp/learned_model_vartest16
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
23
1 change: 1 addition & 0 deletions models_temp/learned_model_vartest17
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
36
1 change: 1 addition & 0 deletions models_temp/learned_model_vartest18
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
23
1 change: 1 addition & 0 deletions models_temp/learned_model_vartest19
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
23
1 change: 1 addition & 0 deletions models_temp/learned_model_vartest20
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
23
1 change: 1 addition & 0 deletions models_temp/learned_model_vartest22
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
23
1 change: 1 addition & 0 deletions models_temp/learned_model_vartest33
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
23
1 change: 1 addition & 0 deletions models_temp/learned_model_vartest40
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
23
1 change: 1 addition & 0 deletions models_temp/learned_model_vartest44
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
23
Binary file modified models_temp/lstm.modeltest
Binary file not shown.
Binary file added models_temp/lstm.modeltest1
Binary file not shown.
Binary file added models_temp/lstm.modeltest12
Binary file not shown.
Binary file added models_temp/lstm.modeltest13
Binary file not shown.
Binary file added models_temp/lstm.modeltest14
Binary file not shown.
Binary file added models_temp/lstm.modeltest15
Binary file not shown.
Binary file added models_temp/lstm.modeltest16
Binary file not shown.
Binary file added models_temp/lstm.modeltest17
Binary file not shown.
Binary file added models_temp/lstm.modeltest18
Binary file not shown.
Binary file added models_temp/lstm.modeltest19
Binary file not shown.
Binary file added models_temp/lstm.modeltest20
Binary file not shown.
Binary file added models_temp/lstm.modeltest22
Binary file not shown.
Binary file added models_temp/lstm.modeltest33
Binary file not shown.
Binary file added models_temp/lstm.modeltest40
Binary file not shown.
Binary file added models_temp/lstm.modeltest44
Binary file not shown.
2 changes: 1 addition & 1 deletion models_temp/test
Original file line number Diff line number Diff line change
@@ -1 +1 @@
VAR
Holtwinters
21 changes: 14 additions & 7 deletions server.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,24 @@

import engines.functions_timeseries as ft
import engines.BBDD as db

import os


app = Flask(__name__)
CORS(app)


#db.init_database()
app.config.from_pyfile(os.path.join(".", "config/app.cfg"), silent=False)

db.init_database()

DB_NAME= app.config.get("DB_NAME")
PORT = app.config.get("PORT")

@app.route('/univariate', methods=['POST'])
def univariate_engine():
db.init_database()

if not request.json:
abort(400)

Expand All @@ -37,8 +44,8 @@ def univariate_engine():
if(name != 'NA'):
filename= './lst/'+name+'.lst'
try:
with open(filename, 'r') as filehandle:
previousList = json.load(filehandle)
# with open(filename, 'r') as filehandle:
# previousList = json.load(filehandle)
previousList=db.get_ts(name).split(',')
previousList = list(map(int, previousList))
except Exception:
Expand All @@ -48,8 +55,8 @@ def univariate_engine():
if not restart :
print ("Lista append")
lista = previousList + lista
with open(filename, 'w') as filehandle:
json.dump(lista,filehandle)
# with open(filename, 'w') as filehandle:
# json.dump(lista,filehandle)
str_lista= ",".join(str(v) for v in lista)
db.set_ts(name,str_lista)

Expand Down Expand Up @@ -124,4 +131,4 @@ def index():
return "Timecop ready to play"

if __name__ == '__main__':
app.run(debug=True,host = '0.0.0.0',port=80)
app.run(host = '0.0.0.0',port=PORT)
1 change: 1 addition & 0 deletions static/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!DOCTYPE html><html><head><meta charset=utf-8><meta name=viewport content="width=device-width,initial-scale=1"><title>timecop</title><link href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700|Material+Icons" rel=stylesheet><link href=./static/css/app.14daf9aed06f69ef52ff8971689f65e3.css rel=stylesheet></head><body><div id=app></div><script type=text/javascript src=./static/js/manifest.3ad1d5771e9b13dbdad2.js></script><script type=text/javascript src=./static/js/vendor.c2a07eda06ceb06f9226.js></script><script type=text/javascript src=./static/js/app.1ad26df5b1044be50976.js></script></body></html>
7 changes: 7 additions & 0 deletions static/static/css/app.14daf9aed06f69ef52ff8971689f65e3.css

Large diffs are not rendered by default.

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions static/static/img/logo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions static/static/img/logo_dark.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions static/static/js/app.1ad26df5b1044be50976.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions static/static/js/app.1ad26df5b1044be50976.js.map

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions static/static/js/manifest.3ad1d5771e9b13dbdad2.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions static/static/js/manifest.3ad1d5771e9b13dbdad2.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 9d41670

Please sign in to comment.