Skip to content

Commit

Permalink
Merge pull request #18 from gianlucamazza/develop
Browse files Browse the repository at this point in the history
cleanup
  • Loading branch information
gianlucamazza authored Aug 10, 2024
2 parents 03330bf + 9efa971 commit 2f07d0b
Show file tree
Hide file tree
Showing 38 changed files with 2,122 additions and 929 deletions.
9 changes: 9 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
root = true

[*]
indent_style = space
indent_size = 4
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
17 changes: 17 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[flake8]
max-line-length = 88
extend-ignore = E203, E266, E501, W503, W293, E302
max-complexity = 18
select = B,C,E,F,W,T4,B9
exclude =
.git,
__pycache__,
docs/source/conf.py,
old,
build,
dist,
venv

per-file-ignores =
# imported but unused
__init__.py: F401
25 changes: 25 additions & 0 deletions .github/workflows/python-code-quality.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Python Code Quality

on:
push:
branches: [main, develop]
pull_request:
branches: [main, develop]

jobs:
code-quality:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: "3.x"
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install flake8 black
- name: Run Flake8
run: flake8 src tests
- name: Run Black
run: black --check src tests
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ src/__pycache__/
src/*.egg-info/
build/

# Distribution / packaging
node_modules/

# virtualenv
venv/

Expand Down
13 changes: 13 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
repos:
- repo: https://github.com/pre-commit/mirrors-prettier
rev: "v4.0.0-alpha.8"
hooks:
- id: prettier
- repo: https://github.com/psf/black
rev: 24.8.0
hooks:
- id: black
- repo: https://github.com/pycqa/flake8
rev: 7.1.1
hooks:
- id: flake8
29 changes: 7 additions & 22 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,7 @@
"SMA_50": 50,
"SMA_200": 200,
"EMA": 20,
"MACD": [
12,
26,
9
],
"MACD": [12, 26, 9],
"RSI": 14,
"Bollinger": 20,
"ATR": 14,
Expand All @@ -30,16 +26,8 @@
"window3": 52
}
},
"targets": [
"Open",
"High",
"Low",
"Close",
"Volume"
],
"disabled_features": [
"Adj Close"
],
"targets": ["Open", "High", "Low", "Close", "Volume"],
"disabled_features": ["Adj Close"],
"all_features": [
"SMA_50",
"SMA_200",
Expand Down Expand Up @@ -71,19 +59,15 @@
"PSAR_Up",
"PSAR_Down"
],
"selected_features": [
"MACD",
"SMA_200",
"EMA"
]
"selected_features": ["MACD", "SMA_200", "EMA"]
},
"model_settings": {
"hidden_size": 391,
"num_layers": 1,
"dropout": 0.012239709228895848,
"learning_rate": 0.00020025239898961104,
"fc_output_size": 5,
"weight_decay": 8.99288758616325e-05,
"weight_decay": 8.99288758616325e-5,
"clip_value": 3.974825341474775,
"batch_size": 20,
"sequence_length": 16
Expand All @@ -93,6 +77,7 @@
"look_forward": 30,
"epochs": 150,
"model_dir": "models",
"plot_dir": "plots",
"use_time_series_split": true,
"time_series_splits": 5,
"max_lag": 5
Expand All @@ -108,4 +93,4 @@
"trade_allocation": 0.1,
"max_open_trades": 5
}
}
}
Binary file removed models/model_fold_best.onnx
Binary file not shown.
54 changes: 54 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,57 @@
[build-system]
requires = ["setuptools>=64", "wheel"]
build-backend = "setuptools.build_meta"

[tool.black]
line-length = 79
target-version = ['py38']
include = '\.pyi?$'
extend-exclude = '''
/(
# directories
\.eggs
| \.git
| \.hg
| \.mypy_cache
| \.tox
| \.venv
| build
| dist
| node_modules
| data
| docs
| logs
| models
| png
| reports
| static
)/
'''

[tool.isort]
profile = "black"
line_length = 79
multi_line_output = 3
include_trailing_comma = true
force_grid_wrap = 0
use_parentheses = true
ensure_newline_before_comments = true

[tool.pytest.ini_options]
minversion = "6.0"
addopts = "-ra -q"
testpaths = [
"tests",
]

[tool.mypy]
python_version = "3.8"
warn_return_any = true
warn_unused_configs = true
ignore_missing_imports = true

[tool.pylint.messages_control]
disable = "C0330, C0326"

[tool.pylint.format]
max-line-length = "79"
5 changes: 4 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,7 @@ optuna
onnxruntime
onnx
flask
pytest
pytest
black
flake8
pre-commit
5 changes: 3 additions & 2 deletions src/lstm_forecast/api/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
from flask import Flask
from .config import Config
from flask import Flask


def create_app(config_class=Config):
app = Flask(__name__)
app.config.from_object(config_class)

from api.routes import main

app.register_blueprint(main)

return app
return app
9 changes: 2 additions & 7 deletions src/lstm_forecast/api/app.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import os
import sys

sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))

from api import create_app

app = create_app()

if __name__ == '__main__':
app.run(debug=True)
if __name__ == "__main__":
app.run(debug=True)
7 changes: 4 additions & 3 deletions src/lstm_forecast/api/config.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os


class Config:
MODEL_PATH = os.environ.get('MODEL_PATH') or 'models/best_model.onnx'
SECRET_KEY = os.environ.get('SECRET_KEY') or 'you-will-never-guess'
# Add other configuration variables as needed
MODEL_PATH = os.environ.get("MODEL_PATH") or "models/best_model.onnx"
SECRET_KEY = os.environ.get("SECRET_KEY") or "you-will-never-guess"
# Add other configuration variables as needed
10 changes: 3 additions & 7 deletions src/lstm_forecast/api/models.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
import os
import sys
import onnxruntime as ort
import numpy as np

sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))

from api.config import Config


def load_model():
return ort.InferenceSession(Config.MODEL_PATH)


def make_prediction(model, data):
ort_inputs = {model.get_inputs()[0].name: data}
return model.run(None, ort_inputs)[0]
return model.run(None, ort_inputs)[0]
16 changes: 9 additions & 7 deletions src/lstm_forecast/api/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,25 @@
from .utils import preprocess_data, postprocess_prediction
from .models import load_model, make_prediction

main = Blueprint('main', __name__)
main = Blueprint("main", __name__)

@main.route('/predict', methods=['POST'])

@main.route("/predict", methods=["POST"])
def predict():
data = request.json
if not data:
return jsonify({'error': 'No data provided'}), 400
return jsonify({"error": "No data provided"}), 400

try:
processed_data = preprocess_data(data)
model = load_model()
prediction = make_prediction(model, processed_data)
result = postprocess_prediction(prediction)
return jsonify({'prediction': result})
return jsonify({"prediction": result})
except Exception as e:
return jsonify({'error': str(e)}), 500
return jsonify({"error": str(e)}), 500


@main.route('/health', methods=['GET'])
@main.route("/health", methods=["GET"])
def health_check():
return jsonify({'status': 'healthy'}), 200
return jsonify({"status": "healthy"}), 200
Loading

0 comments on commit 2f07d0b

Please sign in to comment.