Skip to content

Commit

Permalink
Fix Ruff errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Francisco Silva authored and Francisco Silva committed Nov 12, 2024
1 parent 89cffbe commit 58f0abe
Show file tree
Hide file tree
Showing 28 changed files with 192 additions and 104 deletions.
2 changes: 1 addition & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
# *.ipynb text

# To ignore it use below
*.ipynb linguist-documentation
*.ipynb linguist-documentation
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,4 @@ log/

# cache files
*.cache
.ruff_cache/
.ruff_cache/
28 changes: 28 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.3.0
hooks:
- id: ruff
args: [--fix]
- id: ruff-format

- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
- id: check-added-large-files
- id: check-toml
- id: debug-statements
- id: check-case-conflict
- id: check-merge-conflict

- repo: local
hooks:
- id: pytest
name: pytest
entry: pytest
language: system
pass_filenames: false
always_run: true
2 changes: 1 addition & 1 deletion .streamlit/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
base="dark"

[client]
showSidebarNavigation = false
showSidebarNavigation = false
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,30 @@ The stock classifier is trained using financial ratios and growth features deriv
```bash
pip install .
```

### Development Setup

1. Install development dependencies:
```bash
pip install -e ".[dev]"
```

2. Install pre-commit hooks:
```bash
chmod +x scripts/install-hooks.sh
./scripts/install-hooks.sh
```
This setup will:
- Format your code using ruff
- Run linting checks
- Check for common issues like trailing whitespace and merge conflicts
- Validate your YAML and TOML files
- Run your pytest suite before each commit
- Verify your poetry configuration

The hooks will run automatically before each commit. If any checks fail, the commit will be blocked until you fix the issues.

To manually run all pre-commit hooks on all files:
```bash
pre-commit run --all-files
```
2 changes: 1 addition & 1 deletion notebooks/eda.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -3100,7 +3100,7 @@
"from pipeline import Scraper\n",
"\n",
"vix = Scraper(\"^GSPC\", \"yfinance\").get_market_data(\"2005-01-01\")\n",
"vix.head()\n"
"vix.head()"
]
}
],
Expand Down
27 changes: 25 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@ dependencies = [
"nbformat>=4.2.0",
"watchdog",
"ipykernel",
"shap"
"shap",
"pre-commit"
]

[project.optional-dependencies]
dev = ["pytest", "pylint", "pytest-cov", "ruff", "black", "black[jupyter]"]
dev = ["pytest", "pytest-cov", "ruff"]

[build-system]
requires = ["setuptools"]
Expand All @@ -50,6 +51,28 @@ fail_under = 80
[tool.setuptools.packages.find]
where = ["stocksense"]

[tool.ruff]
line-length = 88
target-version = "py310"

[tool.ruff.lint]
select = [
"E", # pycodestyle errors
"W", # pycodestyle warnings
"F", # pyflakes
"I", # isort
"C", # flake8-comprehensions
"B", # flake8-bugbear
]
ignore = [
]

[tool.ruff.format]
quote-style = "double"
indent-style = "space"
skip-magic-trailing-comma = false
line-ending = "auto"

[project.scripts]
stocksense-app = "app.home:main"
stocksense = "main:main"
9 changes: 9 additions & 0 deletions scripts/install-hooks.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/sh

# Install pre-commit hooks
pre-commit install

# Make sure the script is executable
chmod +x .git/hooks/pre-commit

echo "Pre-commit hooks installed successfully!"
1 change: 1 addition & 0 deletions stocksense/app/home.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import subprocess

import streamlit as st


Expand Down
22 changes: 11 additions & 11 deletions stocksense/app/pages/analytics.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
import streamlit as st
import polars as pl
import pandas as pd
import datetime as dt
from pathlib import Path

import plotly.graph_objects as go
import pandas as pd
import plotly.express as px
from plotly.subplots import make_subplots

import plotly.graph_objects as go
import polars as pl
import streamlit as st
from database_handler import DatabaseHandler
from plotly.subplots import make_subplots

pd.options.mode.chained_assignment = None # default='warn'

MILLION = 1000000
MARGIN = dict(l=0, r=10, b=10, t=25)
MARGIN = {"l": 0, "r": 10, "b": 10, "t": 25}


def list_stocks():
Expand Down Expand Up @@ -83,7 +82,8 @@ def display_stock_info(stock, info):
st.markdown(f"**Volume**: {(info.loc[0, 'volume'])} M$")
st.markdown(f"**Beta**: {(info.loc[0, 'beta']):.3f}")
st.markdown(
f"**Enterprise Value**: {(info.loc[0, 'enterprise_value'] / MILLION):.2f} M$"
"**Enterprise Value**: "
f"{(info.loc[0, 'enterprise_value'] / MILLION):.2f} M$"
)
st.divider()
st.markdown(f"**Trailing PE**: {(info.loc[0, 'fiftytwo_wc']):.2f}")
Expand Down Expand Up @@ -119,7 +119,7 @@ def plot_market_data(df, index_df):
specs=[[{"secondary_y": True}], [{}]],
)

fig.update_xaxes(rangebreaks=[dict(values=date_breaks(df))])
fig.update_xaxes(rangebreaks=[{"values": date_breaks(df)}])
fig.add_trace(
go.Scatter(
x=df["date"],
Expand Down Expand Up @@ -194,7 +194,7 @@ def plot_financial_analysis(df):
if col in df.columns:
df[col] = df[col] * 100

ratio_vars = [col for col in df.columns[15:]]
ratio_vars = list(df.columns[15:])
margins = st.multiselect(
"Select metric",
ratio_vars,
Expand All @@ -214,7 +214,7 @@ def plot_financial_analysis(df):
height=400,
template="plotly_dark",
title_text="Financial Metric Overview",
margin=dict(l=10, r=10, t=30, b=10),
margin={"l": 10, "r": 10, "b": 10, "t": 30},
)
st.plotly_chart(fig, use_container_width=True, theme=None)

Expand Down
3 changes: 1 addition & 2 deletions stocksense/app/pages/overview.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import streamlit as st
import numpy as np
import pandas as pd
import plotly.express as px

import streamlit as st
from database_handler import DatabaseHandler

pd.set_option("future.no_silent_downcasting", True)
Expand Down
1 change: 1 addition & 0 deletions stocksense/config/definitions.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os.path

import yaml

ROOT_PATH = os.path.realpath(os.path.join(os.path.dirname(__file__), ".."))
Expand Down
2 changes: 1 addition & 1 deletion stocksense/config/model_config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,4 @@
'train_start': 2007
'train_window': 12
'val_window': 2
'seed': 100
'seed': 100
2 changes: 1 addition & 1 deletion stocksense/config/path_config.yml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
"logs"
"logs"
14 changes: 7 additions & 7 deletions stocksense/database_handler/__init__.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
from database_handler.connection import DatabaseConnection
from database_handler.schema import create_tables
from database_handler.handler import DatabaseHandler
from database_handler.queries import (
insert_data,
insert_record,
update_data,
count_data,
delete_data,
delete_table,
count_data,
fetch_record,
fetch_data,
fetch_record,
insert_data,
insert_record,
update_data,
)
from database_handler.handler import DatabaseHandler
from database_handler.schema import create_tables

__all__ = [
"DatabaseConnection",
Expand Down
3 changes: 2 additions & 1 deletion stocksense/database_handler/connection.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import sqlite3
from sqlite3 import Error
from pathlib import Path
from sqlite3 import Error

from loguru import logger

PACKAGE_DIR = Path(__file__).parents[1]
Expand Down
12 changes: 6 additions & 6 deletions stocksense/database_handler/handler.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import numpy as np
import polars as pl
import sqlite3
from typing import Optional

import numpy as np
import polars as pl
from database_handler import (
DatabaseConnection,
count_data,
create_tables,
insert_data,
insert_record,
update_data,
delete_data,
delete_table,
count_data,
fetch_data,
insert_data,
insert_record,
update_data,
)

sqlite3.register_adapter(np.int32, int)
Expand Down
5 changes: 3 additions & 2 deletions stocksense/database_handler/queries.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import polars as pl
from sqlite3 import Connection, Error
from loguru import logger
from typing import Optional

import polars as pl
from loguru import logger


def insert_data(connection: Connection, table_name: str, data: pl.DataFrame) -> None:
try:
Expand Down
3 changes: 2 additions & 1 deletion stocksense/database_handler/schema.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from sqlite3 import Connection, Error

from loguru import logger


Expand Down Expand Up @@ -115,7 +116,7 @@ def create_tables(connection: Connection | None) -> None:

try:
cursor = connection.cursor()
for table_name, table_schema in tables.items():
for table_schema in tables.values():
cursor.execute(table_schema)
connection.commit()
logger.success("Tables created successfully")
Expand Down
3 changes: 1 addition & 2 deletions stocksense/main.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import click

from pipeline import ETL, Preprocess
from model import ModelHandler
from pipeline import ETL, Preprocess


@click.command()
Expand Down
2 changes: 1 addition & 1 deletion stocksense/model/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from model.xgboost_model import XGBoostModel
from model.genetic_algorithm import GeneticAlgorithm
from model.model_handler import ModelHandler
from model.xgboost_model import XGBoostModel

__all__ = ["XGBoostModel", "GeneticAlgorithm", "ModelHandler"]
20 changes: 10 additions & 10 deletions stocksense/model/genetic_algorithm.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import pygad
import polars as pl
import datetime as dt
from loguru import logger

from model import XGBoostModel
import polars as pl
import pygad
from config import get_config
from loguru import logger
from model import XGBoostModel


class GeneticAlgorithm:
Expand All @@ -28,9 +28,9 @@ def __init__(
self.init_range_high = init_range_high
self.gene_space = gene_space
self.ga_instance = None
self.no_improvement_count = 0
self.best_fitness_value = 0
self.no_improvement_limit = 5
self.no_improv_count = 0
self.no_improv_limit = 5
self.random_seed = get_config("model")["seed"]

def create_instance(self):
Expand Down Expand Up @@ -68,13 +68,13 @@ def on_generation(self, ga_instance):

if best_solution_fitness > self.best_fitness_value:
self.best_fitness_value = best_solution_fitness
self.no_improvement_count = 0
self.no_improv_count = 0
else:
self.no_improvement_count += 1
self.no_improv_count += 1

if self.no_improvement_count >= self.no_improvement_limit:
if self.no_improv_count >= self.no_improv_limit:
print(
f"no improvement for {self.no_improvement_limit} generations. stopping GA."
f"no improvement for {self.no_improv_limit} generations, stopping GA."
)
ga_instance.terminate()

Expand Down
Loading

0 comments on commit 58f0abe

Please sign in to comment.