Skip to content

Commit

Permalink
Merge pull request #21 from TheCyberHead/heat
Browse files Browse the repository at this point in the history
Fix Heatmap
  • Loading branch information
Seburath authored May 15, 2020
2 parents cf5fc00 + a9ccfaa commit b5216f3
Show file tree
Hide file tree
Showing 14 changed files with 264 additions and 115 deletions.
Binary file added .DS_Store
Binary file not shown.
14 changes: 14 additions & 0 deletions cyberhead/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,21 @@ class Meta:
database = db
db_table = 'backtest_perform'

class HeatMap(peewee.Model):
title = peewee.CharField()
file_tmp_path = peewee.CharField()
file_encoded = peewee.TextField()
image_encoded = peewee.TextField()
class Meta:
database = db
db_table = 'heatmap'


if __name__ == '__main__':
if not HeatMap.table_exists():
HeatMap.create_table()
print('HM table created.')

if not DataSet.table_exists():
DataSet.create_table()
print('Data Sets table created.')
Expand Down
30 changes: 28 additions & 2 deletions cyberhead/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
from flask_cors import CORS, cross_origin
from modules.datasets.db import DataSet
from modules.strategies.db import BacktestPerform
from tasker import perform_strategy, run_loader, fetch_dataset_yahoo
from database import HeatMap
from tasker import perform_strategy, run_loader, fetch_dataset_yahoo, generate_heatmap
import os
import json
import base64

import uuid

app = Flask(__name__)
CORS(app)
Expand Down Expand Up @@ -93,6 +94,10 @@ def get_plot(strategy):
return send_from_directory('tmp', strategy)


@app.route('/get_heatmap/<heatmap>')
def get_heatmap(heatmap):
return send_from_directory('tmp/images', heatmap)

'''
Get all strategies to be available on editor.
'''
Expand All @@ -112,6 +117,27 @@ def strategy_get_edit(strategy_name):



@app.route('/heatmap', methods=["POST", "GET"])
def heatmap():
print(request.method)
if request.method == 'POST':
title = request.json['heatmap']['title']
file = request.json['heatmap']['file64'][21:]
unique_id = uuid.uuid1()
heatmap = HeatMap.create(title=title,
file_tmp_path=f"{unique_id}.csv",
file_encoded=file,
image_encoded="NA"
)
with open(f"tmp/{unique_id}.csv", "wb") as f:
f.write(base64.b64decode(file))
f.close()
generate_heatmap(unique_id, heatmap.id)
return request.json
elif request.method == 'GET':
heatmaps = HeatMap.select()
return jsonify({'heatmaps':[model_to_dict(heatmap) for heatmap in heatmaps]})

"""
This sections defines how Flask is listening, when deploying for a production instance you should set debug to False and set the host parameter to 0.0.0.0
"""
Expand Down
8 changes: 8 additions & 0 deletions cyberhead/modules/heatmap/heatmap.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import numpy as np
import seaborn as sns
import uuid

def generate_heatmap(arr: list):
unique_id = uuid.uuid1()
ax = sns.heatmap(arr, cmap='YlGnBu')
ax.figure.savefig('testingsea.png')
Binary file added cyberhead/modules/heatmap/testingsea.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
46 changes: 19 additions & 27 deletions cyberhead/modules/strategies/javascript/strategies.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,24 @@
import API_BASE from './URL'
import React from 'react';

async function getStrategies(strategy){
let response = await fetch(`${API_BASE}/get_strategies`)
let data = await response.json()
return data
}

async function getStrategy(strategy){
let response = await fetch(`${API_BASE}/get_strategy/${strategy}`)
let data = await response.json()
return data
}
class Strategies extends React.Component {
constructor(props){
super(props);
this.state = {
load: true
}
}

async function getStrategyEdit(name){
let response = await fetch(`${API_BASE}/get_strategy_edit/${name}`)
let data = await response.json()
return data
}
componentDidMount(){
this.props.updateKey('2')
}

async function loadStrategiesEditor(){
let response = await fetch(`${API_BASE}/get_strategies_edit`)
let data = await response.json()
return data
render(){
return(
<React.Fragment>
<h1>Strategies</h1>
</React.Fragment>
)
}
}

export {
getStrategies,
getStrategy,
getStrategyEdit,
loadStrategiesEditor
};
export default Strategies;
44 changes: 37 additions & 7 deletions cyberhead/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1,45 @@
amqp==2.5.2
Backtesting==0.1.7
billiard==3.6.3.0
bokeh==2.0.2
celery==4.4.2
certifi==2019.11.28
cffi==1.14.0
chardet==3.0.4
click==7.1.2
cryptography==2.9.2
Flask==1.1.2
Flask-Cors==3.0.8
idna==2.8
importlib-metadata==1.6.0
itsdangerous==1.1.0
Jinja2==2.11.2
kombu==4.6.8
lxml==4.5.0
MarkupSafe==1.1.1
multitasking==0.0.9
numpy==1.18.1
setuptools
peewee
packaging==20.3
pandas==1.0.3
pandas-datareader==0.8.1
peewee==3.13.3
Pillow==7.1.2
pycparser==2.20
pydantic==1.4
PyMySQL==0.9.3
pyOpenSSL==19.1.0
pyparsing==2.4.7
python-dateutil==2.8.1
pytz==2020.1
PyYAML==5.3.1
requests==2.22.0
six==1.14.0
SQLAlchemy==1.3.17
starlette==0.12.9
tornado==6.0.4
typing-extensions==3.7.4.2
urllib3==1.25.8
flask
flask_cors
pyopenssl
sqlalchemy
pymysql
vine==1.3.0
Werkzeug==1.0.1
yfinance==0.1.54
zipp==3.1.0
23 changes: 22 additions & 1 deletion cyberhead/tasker.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
from celery import Celery
from backtesting.test import SMA, GOOG
from modules.strategies import SMACrossGOOG, SMACrossAPPL
from database import BacktestPerform
from database import BacktestPerform, HeatMap
from recurrent import allTimeFetch, symbolHistorical
import matplotlib as mpl
mpl.use('Agg')
import seaborn as sns
import matplotlib.pyplot as plt
import uuid
import csv

'''
Celery Initialization
Expand All @@ -14,6 +20,7 @@
'''
Define the first initialization of datasets on startup.
'''

dataset_map = {
"SMACrossGOOG": symbolHistorical('GOOG1D'),
"SMACrossAPPL": symbolHistorical('AAPL1D')
Expand Down Expand Up @@ -88,3 +95,17 @@ def run_loader():
for strategy in strategies_map.keys():
perform_strategy.delay(strategy)

def generate_heatmap(file_path, heatmap_id):
with open(f'tmp/{file_path}.csv', newline='') as f:
reader = csv.reader(f)
data = list(reader)
f.close()
headers, csv_data = data[0],data[1:]
csv_data = [[int(x[0]), int(x[1])] for x in csv_data]
unique_id = uuid.uuid1()
ax = sns.heatmap(csv_data, cmap='YlGnBu')
ax.figure.savefig(f'tmp/images/{unique_id}.png')
plt.close()
q = HeatMap.update(image_encoded=f'tmp/images/{unique_id}.png').where(HeatMap.id==heatmap_id)
q.execute()

10 changes: 10 additions & 0 deletions cyberhead/web/src/actions/getHeatMapData.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import API_BASE from './URL'

async function getHeatMapData(id){
let response = await fetch(`${API_BASE}/heatmap/${id}`)
let data = await response.json()
return data
}


export default getHeatMapData;
10 changes: 10 additions & 0 deletions cyberhead/web/src/actions/getHeatMaps.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import API_BASE from './URL'

async function getHeatMaps(){
let response = await fetch(`${API_BASE}/heatmap`)
let data = await response.json()
return data
}


export default getHeatMaps;
1 change: 0 additions & 1 deletion cyberhead/web/src/actions/submitHeatmap.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,4 @@ async function submitHeatmap(heatmap){
return data
}


export default submitHeatmap;
Loading

0 comments on commit b5216f3

Please sign in to comment.