Skip to content

Commit

Permalink
👌 Improved error messages and changed dataset arg into dataset_name
Browse files Browse the repository at this point in the history
  • Loading branch information
s-weigand committed Mar 31, 2022
1 parent 8f8805d commit 9a5d038
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 13 deletions.
34 changes: 21 additions & 13 deletions glotaran/project/result.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,13 +310,13 @@ def verify(self) -> bool:

return True

def create_clp_guide_dataset(self, dataset: str, clp_label: str) -> xr.Dataset:
def create_clp_guide_dataset(self, dataset_name: str, clp_label: str) -> xr.Dataset:
"""Create dataset for clp guidance.
Parameters
----------
dataset : str
The dataset to extract the guide from.
dataset_name : str
The name of dataset to extract the guide from.
clp_label : str
The label of the clp to guide.
Expand All @@ -328,18 +328,26 @@ def create_clp_guide_dataset(self, dataset: str, clp_label: str) -> xr.Dataset:
Raises
------
ValueError
Raised if either the dataset or clp label does not exist.
If ``dataset_name`` is not in result.
ValueError
If ``clp_labels`` is not in result.
"""
if dataset not in self.data:
raise ValueError(f"Unknown dataset '{dataset}'.")
result = self.data[dataset]
if clp_label not in result.clp_label:
raise ValueError(f"Unknown clp_label '{clp_label}'.")
if dataset_name not in self.data:
raise ValueError(
f"Unknown dataset {dataset_name!r}. "
f"Known datasets are:\n {list(self.data.keys())}"
)
dataset = self.data[dataset_name]
if clp_label not in dataset.clp_label:
raise ValueError(
f"Unknown clp_label {clp_label!r}. "
f"Known clp_labels are:\n {list(dataset.clp_label.values)}"
)

return (
result.clp.sel(clp_label=[clp_label])
dataset.clp.sel(clp_label=[clp_label])
.transpose()
.rename(clp_label=result.model_dimension)
.rename(clp_label=dataset.model_dimension)
.to_dataset(name="data")
)

Expand Down Expand Up @@ -373,5 +381,5 @@ def get_dataset(self, dataset_label: str) -> xr.Dataset:
"""
try:
return self.data[dataset_label]
except KeyError:
raise ValueError(f"Unknown dataset '{dataset_label}'")
except KeyError as e:
raise ValueError(f"Unknown dataset '{dataset_label}'") from e
18 changes: 18 additions & 0 deletions glotaran/project/test/test_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,24 @@ def test_get_scheme(dummy_result: Result):
)


def test_create_clp_guide_dataset_errors(dummy_result: Result):
"""Errors thrown when dataset or clp_label are not in result."""
with pytest.raises(ValueError) as exc_info:
dummy_result.create_clp_guide_dataset("not-a-dataset", "species_1")

assert (
str(exc_info.value)
== "Unknown dataset 'not-a-dataset'. Known datasets are:\n ['dataset_1']"
)
with pytest.raises(ValueError) as exc_info:
dummy_result.create_clp_guide_dataset("dataset_1", "not-a-species")

assert (
str(exc_info.value) == "Unknown clp_label 'not-a-species'. Known clp_labels are:\n "
"['species_1', 'species_2', 'species_3']"
)


def test_create_clp_guide_dataset(dummy_result: Result):
clp_guide = dummy_result.create_clp_guide_dataset("dataset_1", "species_1")
assert clp_guide.data.shape == (1, dummy_result.data["dataset_1"].spectral.size)
Expand Down

0 comments on commit 9a5d038

Please sign in to comment.