Skip to content

Commit

Permalink
Merge branch 'develop' into feature/split_hazard_module
Browse files Browse the repository at this point in the history
  • Loading branch information
emanuel-schmid committed Apr 16, 2024
2 parents fdef74c + 7ce9484 commit 4c84ed1
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 352 deletions.
7 changes: 6 additions & 1 deletion .readthedocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@ build:
tools:
python: "mambaforge-4.10"

# Append fixed Python version to requirements
jobs:
pre_create_environment:
- echo " - python=3.9" >> requirements/env_climada.yml

conda:
environment: requirements/env_docs.yml
environment: requirements/env_climada.yml

python:
install:
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ Code freeze date: YYYY-MM-DD

### Fixed

- Avoid an issue where a Hazard subselection would have a fraction matrix with only zeros as entries by throwing an error [#866](https://github.com/CLIMADA-project/climada_python/pull/866)

### Added

- climada.hazard.centroids.centr.Centroids.get_area_pixel
Expand Down Expand Up @@ -66,6 +68,7 @@ Code freeze date: YYYY-MM-DD
- climada.hazard.centroids.centr.Centroids.values_from_raster_files
- climada.hazard.centroids.centr.Centroids.values_from_vector_files
- climada.hazard.centroids.centr.generate_nat_earth_centroids
- `requirements/env_docs.yml`. The regular environment specs are now used to build the online documentation [#687](https://github.com/CLIMADA-project/climada_python/pull/687)

## 4.1.1

Expand Down
16 changes: 16 additions & 0 deletions climada/hazard/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,10 @@ def select(self, event_names=None, event_id=None, date=None, orig=None,
LOGGER.info('No hazard centroids within extent and region')
return None

# Sanitize fraction, because we check non-zero entries later
self.fraction.eliminate_zeros()

# Perform attribute selection
for (var_name, var_val) in self.__dict__.items():
if isinstance(var_val, np.ndarray) and var_val.ndim == 1 \
and var_val.size > 0:
Expand Down Expand Up @@ -343,6 +347,18 @@ def select(self, event_names=None, event_id=None, date=None, orig=None,
dt.datetime.fromordinal(haz.date.min()).year) + 1
haz.frequency = haz.frequency * year_span_old / year_span_new

# Check if new fraction is zero everywhere
if self._get_fraction() is not None and haz._get_fraction() is None:
raise RuntimeError(
"Your selection created a Hazard object where the fraction matrix is "
"zero everywhere. This hazard will have zero impact everywhere. "
"We are catching this condition because of an implementation detail: "
"A fraction matrix without nonzero-valued entries will be completely "
"ignored. This is surely not what you intended. If you really want to, "
"you can circumvent this error by setting your original fraction "
"matrix to zero everywhere, but there probably is no point in doing so."
)

haz.sanitize_event_ids()
return haz

Expand Down
20 changes: 20 additions & 0 deletions climada/hazard/test/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,26 @@ def test_select_tight_pass(self):
self.assertIsInstance(sel_haz.intensity, sparse.csr_matrix)
self.assertIsInstance(sel_haz.fraction, sparse.csr_matrix)

def test_select_new_fraction_zero(self):
"""Check if a new fraction of only zeros is handled correctly"""
hazard = dummy_hazard()
hazard.centroids.gdf["region_id"] = [1, 1, 2]

# Select a part of the hazard where fraction is zero only
with self.assertRaisesRegex(
RuntimeError,
"Your selection created a Hazard object where the fraction matrix is zero "
"everywhere"
):
hazard.select(event_id=[3, 4], reg_id=[2])

# Error should not be thrown if we set everything to zero
# NOTE: Setting the values of `data` to zero instead of the matrix values will
# add explicitly stored zeros. Therefore, this test explicitly checks if
# `eliminate_zeros` is called on `fraction` during `select`.
hazard.fraction.data[...] = 0
selection = hazard.select(event_id=[3, 4], reg_id=[2])
np.testing.assert_array_equal(selection.fraction.toarray(), [[0], [0]])

class TestAppend(unittest.TestCase):
"""Test append method."""
Expand Down
11 changes: 7 additions & 4 deletions doc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,18 @@ Next, execute `make` (this might take a while when executed for the first time)
make html
```

The documentation will be placed in `doc/_html`. Simply open the page `doc/_html/index.html` with your browser.
The documentation will be placed in `doc/_build/html`. Simply open the page `doc/_build/html/index.html` with your browser.

## Updating the Documentation Environment for Readthedocs.org

The online documentation is built by [`readthedocs.org`](https://readthedocs.org/).
Their servers have a limited capacity, which is typically exceeded by Anaconda when it tries to resolve all dependencies for CLIMADA.
We therefore provide a dedicated environment with *fixed* package versions in `requirements/env_docs.yml`.
Their servers have a limited capacity.
In the past, this capacity was exceeded by Anaconda when it tried to resolve all dependencies for CLIMADA.
We therefore provided a dedicated environment with *fixed* package versions in `requirements/env_docs.yml`.
As of commit `8c66d8e4a4c93225e3a337d8ad69ab09b48278e3`, this environment was removed and the online documentation environment is built using the specs in `requirements/env_climada.yml`.
If this should fail in the future, revert the changes by `8c66d8e4a4c93225e3a337d8ad69ab09b48278e3` and update the environment specs in `requirements/env_docs.yml` with the following instructions.

For re-creating this environment, we provide a Dockerfile.
For re-creating the documentation environment, we provide a Dockerfile.
You can use it to build a new environment and extract the exact versions from it.
This might be necessary when we upgrade to a new version of Python, or when dependencies are updated.
**NOTE:** Your machine must be able to run/virtualize an AMD64 OS.
Expand Down
Loading

0 comments on commit 4c84ed1

Please sign in to comment.