Repository: owkin/PyDESeq2
Pull request #83 (2023-02-20 16:28:32)

DOC fix broken RTD example gallery
#### What does your PR implement? Be specific.

This PR fixes the example gallery which causes the docs not to compile on readthedocs. This is due to a data importing issue after merging #68 (pandas won't load a csv from an url provided in the form of a `Path`).

A unit test was added to `test_utils.py` to check that `load_example_data` correctly retrieves data from github when the `datasets` directory is not accessible (as when building the docs on RTD). 


Patch:
diff --git a/README.md b/README.md
index a8e4f2a..9a12167 100644
--- a/README.md
+++ b/README.md
@@ -39,7 +39,7 @@ In case there is a feature you would particularly like to be implemented, feel f
 We recommend installing within a conda environment:
 
 ```
-conda env create -n pydeseq2
+conda create -n pydeseq2
 conda activate pydeseq2
 pip install pydeseq2
 ```
diff --git a/pydeseq2/utils.py b/pydeseq2/utils.py
index 99ec149..ace0a61 100644
--- a/pydeseq2/utils.py
+++ b/pydeseq2/utils.py
@@ -73,16 +73,18 @@ def load_example_data(
     if dataset == "synthetic":
         path_to_data = datasets_path / "synthetic"
         if Path(path_to_data).is_dir():
-            path_to_data_counts = path_to_data / "test_counts.csv"
-            path_to_data_clinical = path_to_data / "test_clinical.csv"
+            # Cast the Paths to strings to have coherent types wrt to the url case (that
+            # does not handle Paths), else mypy throws an error.
+            path_to_data_counts = str(path_to_data / "test_counts.csv")
+            path_to_data_clinical = str(path_to_data / "test_clinical.csv")
         else:
             # if the path does not exist (as is the case in RDT) load it from github
             url_to_data = (
                 "https://raw.githubusercontent.com/owkin/"
                 "PyDESeq2/main/datasets/synthetic/"
             )
-            path_to_data_counts = Path(url_to_data + "/test_counts.csv")
-            path_to_data_clinical = Path(url_to_data + "/test_clinical.csv")
+            path_to_data_counts = url_to_data + "/test_counts.csv"
+            path_to_data_clinical = url_to_data + "/test_clinical.csv"
 
         if modality == "raw_counts":
             df = pd.read_csv(


Test patch:
diff --git a/tests/test_utils.py b/tests/test_utils.py
index d61cb41..af4ef66 100644
--- a/tests/test_utils.py
+++ b/tests/test_utils.py
@@ -1,6 +1,10 @@
+import pathlib
+from unittest import mock
+
 import numpy as np
 import pytest
 
+from pydeseq2.utils import load_example_data
 from pydeseq2.utils import nb_nll
 
 
@@ -27,3 +31,25 @@ def test_nb_nll_moments(mu, alpha):
     assert np.abs(diff) < 0.2 * deviation
     error_var = np.abs(sample.var() - var_th) / var_th
     assert error_var < 1 / np.sqrt(n_montecarlo)
+
+
+# Test data loading from outside the package (e.g. on RTF)
+@pytest.mark.parametrize("modality", ["raw_counts", "clinical"])
+@pytest.mark.parametrize("mocked_dir_flag", [True, False])
+@mock.patch("pathlib.Path.is_dir")
+def test_rtd_example_data_loading(mocked_function, modality, mocked_dir_flag):
+    """
+    Test that load_example_data still works when run from a place where the ``datasets``
+    directory is not accessible, as is when the documentation is built on readthedocs.
+    """
+
+    # Mock the output of is_dir() as False to emulate not having access to the
+    # ``datasets`` directory
+    pathlib.Path.is_dir.return_value = mocked_dir_flag
+
+    # Try loading data.
+    load_example_data(
+        modality=modality,
+        dataset="synthetic",
+        debug=False,
+    )