Skip to content

Commit

Permalink
fix ruff errors in the /helper_functions and /misc folder
Browse files Browse the repository at this point in the history
  • Loading branch information
joacotome24 committed Nov 28, 2024
1 parent 9cbd590 commit 727cbc3
Show file tree
Hide file tree
Showing 7 changed files with 148 additions and 21 deletions.
2 changes: 1 addition & 1 deletion .python-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.12
3.12.7
8 changes: 2 additions & 6 deletions edge_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,17 @@
if "ENVIRONMENT" in os.environ:
app.config.from_envvar("ENVIRONMENT")

# Set optional bootswatch theme
app.config["FLASK_ADMIN_SWATCH"] = "cerulean"

app.config["SQLALCHEMY_DATABASE_URI"] = "postgresql+psycopg2://{}@/{}".format(
app.config["DBUSER"],
app.config["DBNAME"],
)

# engine = create_engine("postgresql+psycopg2://%s@/%s"%(app.config['DBUSER'], app.config['DBNAME']), echo=True)
# SessionMaker = scoped_session(sessionmaker(bind=engine))
db.init_app(app)


with app.app_context():
# Base.metadata.create_all(engine)
db.metadata.create_all(db.engine)

from alembic import command, config
Expand Down Expand Up @@ -73,7 +69,6 @@

admin = Admin(app, name="Risk Assesment", template_mode="bootstrap3")

# work with session
admin.add_view(RiskVectorModelView(db.session))
admin.add_view(TestModelView(db.session))
admin.add_view(ModelView(GpsData, db.session))
Expand All @@ -88,7 +83,8 @@
@click.command()
@click.option("--port", default=50000)
def serve(port: int | str) -> None:
app.run(host="0.0.0.0", port=port)
# Intentionally binding to all interfaces - ensure proper firewall rules are in place
app.run(host="0.0.0.0", port=port) # noqa: S104


if __name__ == "__main__":
Expand Down
4 changes: 2 additions & 2 deletions misc/vector_data_as_cloudwatch_metrics_experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ def gen_put_metric_requests(vector_id, value_timestamp_pairs):
request = None
for value, timestamp in value_timestamp_pairs:
if request is None:
request = {"Namespace": "tnc_edge_brancol_v1", "MetricData": []}
request["MetricData"].append(
request = {"Namespace": "tnc_edge_brancol_v1", "MetricData": []} # MetricData is already a list
request["MetricData"].append( # type: ignore # noqa: PGH003
{
"MetricName": f"vector_{vector_id}",
"Value": value,
Expand Down
12 changes: 3 additions & 9 deletions notebooks/helper_functions/aggregations.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,25 +36,23 @@ def aggregate_concat(dfs, freq, agg_list, agg_cols):
def compare_aggregation_correlations(dfs, intervals, agg_list, x_col, y_col):
results = {}
for x_agg in agg_list:
# print(f'x_agg: {x_agg}')
x_results = []
for i in intervals:
data = aggregate_concat(dfs, freq=i, agg_list=agg_list, agg_cols=[x_col, y_col])

for y_agg in agg_list:
y_results = {}
slope, intercept, rvalue, pvalue, stderr = stats.linregress(
_, _, rvalue, _, _ = stats.linregress(
x=data[f"{x_col}_{x_agg}"], y=data[f"{y_col}_{y_agg}"]
)
r2 = rvalue**2
r2 = float(rvalue)**2
y_results["interval"] = i
y_results["y_agg"] = y_agg
y_results["r2"] = r2

x_results.append(y_results)

df_Xresults = pd.DataFrame(x_results)
# print(df_Xresults.head())
results[x_agg] = df_Xresults

return results
Expand All @@ -65,27 +63,23 @@ def compare_aggregation_correlation_columns(dfs, intervals, agg_list, x_col, y_c

for i in intervals:
data = aggregate_concat(dfs, freq=i, agg_list=agg_list, agg_cols=[x_col] + y_cols)
# result['interval'] = i
for y_col, x_agg, y_agg in itertools.product(y_cols, agg_list, agg_list):
result = {}
result["interval"] = i
result["x_agg"] = x_agg
result["y_agg"] = y_agg
result["y_col"] = y_col

# get r2 value
x = np.array(data[f"{x_col}_{x_agg}"]).reshape((-1, 1))
y = np.array(data[f"{y_col}_{y_agg}"]).reshape((-1, 1))
model = LinearRegression()
model.fit(x, y)
r2 = model.score(x, y)

result["r2"] = r2

results.append(result)

df_results = pd.DataFrame(results)

df_results = pd.DataFrame(results)
return df_results


Expand Down
9 changes: 6 additions & 3 deletions notebooks/helper_functions/data_readers.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,12 @@ def get_bv_set_counts(bv_fish, bv_sets):

# read in catch countst
def get_ai_counts(boat, trip_info):
# Define model and validate boat input
model = "ondeck" if boat == "stpatrick" else "aifish" if boat == "brancol" else None
if model is None:
raise ValueError(f"Unsupported boat: {boat}. Must be 'stpatrick' or 'brancol'")

# Define variables based on model type
if model == "ondeck":
count_column = None
number_columns = [
Expand All @@ -106,7 +111,7 @@ def get_ai_counts(boat, trip_info):
"detection_confidence",
"count",
]
elif model == "aifish":
else: # model == "aifish"
count_column = "count"
number_columns = ["count", "detection_confidence"]

Expand Down Expand Up @@ -228,8 +233,6 @@ def get_bv_counts(ai_df, bv_fish):


def get_elog_data(vessel, trip_start_date, trip_end_date):
# elog data

sql = f"""
select
elogs.id,
Expand Down
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ dependencies = [
"schedule>=1.2.2",
"sqlalchemy>=2.0.36",
"wheel>=0.44.0",
"scipy>=1.14.1",
"scikit-learn>=1.5.2",
"awswrangler>=3.10.0",
]

[tool.uv]
Expand Down
Loading

0 comments on commit 727cbc3

Please sign in to comment.