Skip to content

Commit

Permalink
Merge pull request #14 from climateintelligence/noLag_fix
Browse files Browse the repository at this point in the history
fixing no_lag bug
  • Loading branch information
cehbrecht authored Apr 17, 2024
2 parents d76c2ba + fd4d893 commit 0ecb635
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 3 deletions.
3 changes: 2 additions & 1 deletion hawk/analysis/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ def __init__(
self.tefs_features_lags = []
if self.tefs_use_contemporary_features:
self.tefs_features_lags.append(0)
self.tefs_features_lags.extend(list(range(1, self.tefs_max_lag_features + 1)))
if self.tefs_max_lag_features > 0:
self.tefs_features_lags.extend(list(range(1, self.tefs_max_lag_features + 1)))

self.tefs_target_lags = list(range(1, self.tefs_max_lag_target + 1))

Expand Down
7 changes: 5 additions & 2 deletions hawk/processes/wps_causal.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,12 +227,15 @@ def _handler(self, request, response):

tefs_direction = request.inputs["tefs_direction"][0].data
tefs_use_contemporary_features = request.inputs["tefs_use_contemporary_features"][0].data
tefs_max_lag_features = int(request.inputs["tefs_max_lag_features"][0].data)
if str(request.inputs["tefs_max_lag_features"][0].data) == "no_lag":
tefs_max_lag_features = 0
else:
tefs_max_lag_features = int(request.inputs["tefs_max_lag_features"][0].data)
tefs_max_lag_target = int(request.inputs["tefs_max_lag_target"][0].data)

workdir = Path(self.workdir)

if not tefs_use_contemporary_features and tefs_max_lag_features == "no_lag":
if not tefs_use_contemporary_features and tefs_max_lag_features == 0:
raise ValueError("You cannot use no lag features and not use contemporary features in TEFS.")

causal_analysis = CausalAnalysis(
Expand Down
68 changes: 68 additions & 0 deletions tests/test_causal_analysis_noLag.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#!/usr/bin/env python

"""Tests for `hawk` package."""

import os

import pandas as pd
import pytest
from click.testing import CliRunner # noqa: F401

import hawk # noqa: F401
from hawk import cli # noqa: F401
from hawk.analysis import CausalAnalysis


@pytest.fixture
def response():
"""Sample pytest fixture.
See more at: http://doc.pytest.org/en/latest/fixture.html
"""
# import requests
# return requests.get('https://github.com/audreyr/cookiecutter-pypackage')


def test_content(response):
"""Sample pytest test function with the pytest fixture as an argument."""
# from bs4 import BeautifulSoup
# assert 'GitHub' in BeautifulSoup(response.content).title.string


def test_causal_analysis_noLag():
df_train = pd.read_csv("hawk/demo/Ticino_train.csv", header=0)
df_test = pd.read_csv("hawk/demo/Ticino_test.csv", header=0)
target_column_name = "target"
pcmci_test_choice = "ParCorr"
pcmci_max_lag = 2
tefs_direction = "both"
tefs_use_contemporary_features = True
tefs_max_lag_features = "no_lag"
tefs_max_lag_target = 1
workdir = "tests/output"

if str(tefs_max_lag_features) == "no_lag":
tefs_max_lag_features = 0
else:
tefs_max_lag_features = int(tefs_max_lag_features)

if not tefs_use_contemporary_features and tefs_max_lag_features == 0:
raise ValueError("You cannot use no lag features and not use contemporary features in TEFS.")

causal_analysis = CausalAnalysis(
df_train,
df_test,
target_column_name,
pcmci_test_choice,
pcmci_max_lag,
tefs_direction,
tefs_use_contemporary_features,
tefs_max_lag_features,
tefs_max_lag_target,
workdir,
response=None,
)

causal_analysis.run()

os.system("rm -r tests/output")

0 comments on commit 0ecb635

Please sign in to comment.