From a68b1446b759a5bf310264fae8ec9e388d635546 Mon Sep 17 00:00:00 2001 From: PaoloBonettiPolimi <94172434+PaoloBonettiPolimi@users.noreply.github.com> Date: Fri, 12 Apr 2024 12:15:13 +0200 Subject: [PATCH 1/5] fixing no_lag bug --- hawk/analysis/main.py | 3 ++- hawk/processes/wps_causal.py | 7 +++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/hawk/analysis/main.py b/hawk/analysis/main.py index ac423ae..85a777d 100644 --- a/hawk/analysis/main.py +++ b/hawk/analysis/main.py @@ -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)) diff --git a/hawk/processes/wps_causal.py b/hawk/processes/wps_causal.py index c411275..e4a88e5 100644 --- a/hawk/processes/wps_causal.py +++ b/hawk/processes/wps_causal.py @@ -221,12 +221,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( From de95743737fd0ba853b94e72ef4051e52fea7c18 Mon Sep 17 00:00:00 2001 From: PaoloBonettiPolimi <94172434+PaoloBonettiPolimi@users.noreply.github.com> Date: Tue, 16 Apr 2024 15:44:08 +0200 Subject: [PATCH 2/5] Adding a test for "no_lag" option --- tests/test_causal_analysis_noLag.py | 60 +++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 tests/test_causal_analysis_noLag.py diff --git a/tests/test_causal_analysis_noLag.py b/tests/test_causal_analysis_noLag.py new file mode 100644 index 0000000..11133f7 --- /dev/null +++ b/tests/test_causal_analysis_noLag.py @@ -0,0 +1,60 @@ +#!/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(): + 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" + + 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") \ No newline at end of file From feee7fc06b1b6515ae1c3697edd90935a1893266 Mon Sep 17 00:00:00 2001 From: PaoloBonettiPolimi <94172434+PaoloBonettiPolimi@users.noreply.github.com> Date: Tue, 16 Apr 2024 17:25:51 +0200 Subject: [PATCH 3/5] fixing no_lag test --- tests/test_causal_analysis_noLag.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/tests/test_causal_analysis_noLag.py b/tests/test_causal_analysis_noLag.py index 11133f7..4cac0b8 100644 --- a/tests/test_causal_analysis_noLag.py +++ b/tests/test_causal_analysis_noLag.py @@ -29,7 +29,7 @@ def test_content(response): # assert 'GitHub' in BeautifulSoup(response.content).title.string -def test_causal_analysis(): +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" @@ -41,6 +41,14 @@ def test_causal_analysis(): 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, From e0f430bfb4c8e7ca1adb18f4f4e139d423098750 Mon Sep 17 00:00:00 2001 From: PaoloBonettiPolimi <94172434+PaoloBonettiPolimi@users.noreply.github.com> Date: Tue, 16 Apr 2024 17:29:31 +0200 Subject: [PATCH 4/5] adding new line at end of test file --- tests/test_causal_analysis_noLag.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/test_causal_analysis_noLag.py b/tests/test_causal_analysis_noLag.py index 4cac0b8..b0bf588 100644 --- a/tests/test_causal_analysis_noLag.py +++ b/tests/test_causal_analysis_noLag.py @@ -65,4 +65,5 @@ def test_causal_analysis_noLag(): causal_analysis.run() - os.system("rm -r tests/output") \ No newline at end of file + os.system("rm -r tests/output") + \ No newline at end of file From fd4d89362d92318fe7e4468bf06414c320be6d91 Mon Sep 17 00:00:00 2001 From: PaoloBonettiPolimi <94172434+PaoloBonettiPolimi@users.noreply.github.com> Date: Tue, 16 Apr 2024 17:32:37 +0200 Subject: [PATCH 5/5] removing whitespace --- tests/test_causal_analysis_noLag.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/test_causal_analysis_noLag.py b/tests/test_causal_analysis_noLag.py index b0bf588..dfc88d7 100644 --- a/tests/test_causal_analysis_noLag.py +++ b/tests/test_causal_analysis_noLag.py @@ -66,4 +66,3 @@ def test_causal_analysis_noLag(): causal_analysis.run() os.system("rm -r tests/output") - \ No newline at end of file