Repository: benchopt/benchopt
Pull request #761 (2024-07-19 10:26:24)

FIX default data_home+more test cases
The doc says that the default is `benchmark_dir / data` but right now default is `benchmark_dir`.
This PR fixes this to the former one and add some more tests.

codecov[bot]: ## [Codecov](https://app.codecov.io/gh/benchopt/benchopt/pull/761?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=benchopt) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 82.15%. Comparing base [(`757369c`)](https://app.codecov.io/gh/benchopt/benchopt/commit/757369c54a2dedf398a664e96466b87203d73a1d?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=benchopt) to head [(`390083f`)](https://app.codecov.io/gh/benchopt/benchopt/commit/390083fe8164d36d18eb6019563074a0c098bdd0?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=benchopt).

<details><summary>Additional details and impacted files</summary>


```diff
@@            Coverage Diff             @@
##             main     #761      +/-   ##
==========================================
+ Coverage   82.12%   82.15%   +0.03%     
==========================================
  Files          46       46              
  Lines        3139     3139              
==========================================
+ Hits         2578     2579       +1     
+ Misses        561      560       -1     
```

</details>

Patch:
diff --git a/benchopt/config.py b/benchopt/config.py
index 0812d6e..675b5f5 100644
--- a/benchopt/config.py
+++ b/benchopt/config.py
@@ -264,7 +264,7 @@ def get_data_path(key: str = None):
     data_home = benchmark.get_setting("data_home")
 
     if data_home == "":
-        data_home = benchmark.benchmark_dir
+        data_home = benchmark.benchmark_dir / "data"
 
     path = Path(data_home)
 


Test patch:
diff --git a/benchopt/tests/test_benchmark_features.py b/benchopt/tests/test_benchmark_features.py
index 7a5299a..f065dcd 100644
--- a/benchopt/tests/test_benchmark_features.py
+++ b/benchopt/tests/test_benchmark_features.py
@@ -1,3 +1,4 @@
+import re
 import pytest
 import tempfile
 
@@ -349,19 +350,46 @@ def test_run_once_callback(n_iter):
         out.check_output(rf"RUNONCE\({n_iter}\)", repetition=1)
 
 
-@pytest.mark.parametrize("test_case", ["without_data_home", "with_data_home"])
+@pytest.mark.parametrize("test_case", [
+    "no_config", "without_data_home_abs", "with_data_home_abs",
+    "without_data_home_rel", "with_data_home_rel"
+])
 def test_paths_config_key(test_case):
-    if test_case == "without_data_home":
+    if test_case == "without_data_home_abs":
         config = """
             data_paths:
-                data: /path/to/data
+                dataset: /path/to/data
         """
-    elif test_case == "with_data_home":
+        expected_path = "/path/to/data"
+        expected_home = "{bench_dir}/data"
+    elif test_case == "with_data_home_rel":
         config = """
             data_home: /path/to/home_data
             data_paths:
-                data: path/to/data
+                dataset: path/to/data
         """
+        expected_path = "/path/to/home_data/path/to/data"
+        expected_home = "/path/to/home_data"
+    elif test_case == "with_data_home_abs":
+        config = """
+            data_home: /path/to/home_data
+            data_paths:
+                dataset: /path/to/data
+        """
+        expected_path = "/path/to/data"
+        expected_home = "/path/to/home_data"
+    elif test_case == "without_data_home_rel":
+        config = """
+            data_paths:
+                dataset: path/to/data
+        """
+        expected_home = "{bench_dir}/data"
+        expected_path = f"{expected_home}/path/to/data"
+    elif test_case == "no_config":
+        config = """
+        """
+        expected_home = "{bench_dir}/data"
+        expected_path = f"{expected_home}/dataset"
     else:
         raise Exception("Invalid test case value")
 
@@ -373,13 +401,17 @@ def test_paths_config_key(test_case):
         class Dataset(BaseDataset):
             name = "custom_dataset"
             def get_data(self):
-                path = config.get_data_path(key="data")
+                home = config.get_data_path()
+                path = config.get_data_path(key="dataset")
+                print(f"HOME${home}")
                 print(f"PATH${path}")
 
                 return dict(X=np.random.rand(3, 2), y=np.ones((3,)))
     """
 
     with temp_benchmark(datasets=[custom_dataset], config=config) as benchmark:
+        expected_home = expected_home.format(bench_dir=benchmark.benchmark_dir)
+        expected_path = expected_path.format(bench_dir=benchmark.benchmark_dir)
         with CaptureRunOutput() as out:
             run([
                 str(benchmark.benchmark_dir),
@@ -388,12 +420,5 @@ def test_paths_config_key(test_case):
                 '-o dummy*[reg=0.5]'.split()
             ], standalone_mode=False)
 
-        if test_case == "without_data_home":
-            out.check_output(r"path/to/data", repetition=1)
-        elif test_case == "with_data_home":
-            out.check_output(
-                r"/path/to/home_data/path/to/data",
-                repetition=1
-            )
-        else:
-            raise Exception("Invalid test case value")
+        out.check_output(re.escape(f"HOME${expected_home}"), repetition=1)
+        out.check_output(re.escape(f"PATH${expected_path}"), repetition=1)