Skip to content

Commit

Permalink
Merge branch 'develop' into feature/xr_dataset_context_manager
Browse files Browse the repository at this point in the history
# Conflicts:
#	CHANGELOG.md
  • Loading branch information
emanuel-schmid committed Oct 3, 2024
2 parents 4c695ed + dc7d424 commit a445c92
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 8 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ Code freeze date: YYYY-MM-DD

### Fixed

- Closed some file handles left open when reading netcdf files with `climada.hazard` modules [#953](https://github.com/CLIMADA-project/climada_python/pull/953)
- File handles are being closed after reading netcdf files with `climada.hazard` modules [#953](https://github.com/CLIMADA-project/climada_python/pull/953)
- Avoids a ValueError in the impact calculation for cases with a single exposure point and MDR values of 0, by explicitly removing zeros in `climada.hazard.Hazard.get_mdr` [#933](https://github.com/CLIMADA-project/climada_python/pull/948)

### Deprecated

### Removed
Expand Down
32 changes: 30 additions & 2 deletions climada/engine/test/test_impact_calc.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@

from climada import CONFIG
from climada.entity.entity_def import Entity
from climada.entity import Exposures, ImpactFuncSet, ImpactFunc
from climada.hazard.base import Hazard
from climada.entity import Exposures, ImpactFuncSet, ImpactFunc, ImpfTropCyclone
from climada.hazard.base import Hazard, Centroids
from climada.engine import ImpactCalc, Impact
from climada.engine.impact_calc import LOGGER as ILOG
from climada.util.constants import ENT_DEMO_TODAY, DEMO_DIR
Expand Down Expand Up @@ -471,6 +471,34 @@ def test_stitch_risk_metrics(self):
np.testing.assert_array_equal(eai_exp, [2.25, 1.25, 4.5])
self.assertEqual(aai_agg, 8.0) # Sum of eai_exp

def test_single_exp_zero_mdr(self):
"""Test for case where exposure has a single value and MDR or fraction contains zeros"""
centroids = Centroids.from_lat_lon([-26.16], [28.20])
haz = Hazard(
intensity=sparse.csr_matrix(np.array([[31.5], [19.0]])),
event_id=np.arange(2),
event_name=[0,1],
frequency=np.ones(2) / 2,
fraction=sparse.csr_matrix(np.zeros((2,1))),
date=np.array([0, 1]),
centroids=centroids,
haz_type='TC'
)
exp = Exposures({'value': [1.],
'longitude': 28.22,
'latitude': -26.17,
'impf_TC': 1},
crs="EPSG:4326")
imp_evt = 0.00250988804927603
aai_agg = imp_evt/2
eai_exp = np.array([aai_agg])
at_event = np.array([imp_evt, 0])
exp.set_geometry_points()
impf_tc = ImpfTropCyclone.from_emanuel_usa()
impf_set = ImpactFuncSet([impf_tc])
impf_set.check()
imp = ImpactCalc(exp, impf_set, haz).impact(save_mat=True)
check_impact(self, imp, haz, exp, aai_agg, eai_exp, at_event, at_event)

class TestImpactMatrixCalc(unittest.TestCase):
"""Verify the computation of the impact matrix"""
Expand Down
4 changes: 3 additions & 1 deletion climada/hazard/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1103,7 +1103,9 @@ def get_mdr(self, cent_idx, impf):
impf.id)
mdr_array = impf.calc_mdr(mdr.toarray().ravel()).reshape(mdr.shape)
mdr = sparse.csr_matrix(mdr_array)
return mdr[:, indices]
mdr_out = mdr[:, indices]
mdr_out.eliminate_zeros()
return mdr_out

def get_paa(self, cent_idx, impf):
"""
Expand Down
8 changes: 8 additions & 0 deletions climada/hazard/test/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1176,6 +1176,14 @@ def test_get_mdr(self):
true_mdr = np.digitize(haz.intensity[:, idx].toarray(), [0, 1])
np.testing.assert_array_almost_equal(mdr.toarray(), true_mdr)

# #case with zeros everywhere
cent_idx = np.array([0, 0, 1])
impf.mdd=np.array([0,0,0,1])
# how many non-zeros values are expected
num_nz_values = 5
mdr = haz.get_mdr(cent_idx, impf)
self.assertEqual(mdr.nnz, num_nz_values)

def test_get_paa(self):
haz = dummy_hazard()
impf = dummy_step_impf(haz)
Expand Down
4 changes: 2 additions & 2 deletions doc/guide/install.rst
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ For advanced Python users or developers of CLIMADA, we recommed cloning the CLIM

.. code-block:: shell
mamba create -n climada_env "python=3.9.*"
mamba create -n climada_env "python=3.11.*"
.. hint::

Expand All @@ -177,7 +177,7 @@ For advanced Python users or developers of CLIMADA, we recommed cloning the CLIM
:width: 60%

* - **Supported Version**
- ``3.9``
- ``3.11``
* - Allowed Versions
- ``3.9``, ``3.10``, ``3.11``

Expand Down
2 changes: 1 addition & 1 deletion script/jenkins/install_env.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/bash -e

mamba remove --name climada_env --all
mamba create -n climada_env python=3.9
mamba create -n climada_env python=3.11
mamba env update -n climada_env -f requirements/env_climada.yml

source activate climada_env
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@

classifiers=[
'Development Status :: 4 - Beta',
'Programming Language :: Python :: 3.9',
'Programming Language :: Python :: 3',
'Topic :: Scientific/Engineering :: Atmospheric Science',
'Topic :: Scientific/Engineering :: GIS',
'Topic :: Scientific/Engineering :: Mathematics',
Expand Down

0 comments on commit a445c92

Please sign in to comment.