Skip to content

Commit

Permalink
Add default value for Hazard type
Browse files Browse the repository at this point in the history
Recover the default empty string value for `haz_type` argument of
Hazard initializer.
  • Loading branch information
peanutfun committed Oct 14, 2022
1 parent bdd10b8 commit 06c4065
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 19 deletions.
7 changes: 4 additions & 3 deletions climada/hazard/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,10 @@ class Hazard():
scalar, string, list, 1dim np.array of size num_events."""

def __init__(self,
haz_type: str,
intensity: sparse.csr_matrix,
centroids: Centroids,
date: np.ndarray,
haz_type: str = "",
units: str = "",
frequency: Optional[np.ndarray] = None,
frequency_unit: str = DEF_FREQ_UNIT,
Expand All @@ -182,14 +182,15 @@ def __init__(self,
Parameters
----------
haz_type : str
String indicating the hazard type (e.g., ``TC`` for tropical cyclone)
intensity : sparse.csr_matrix
The intensity of the hazard for each event. Shape ``(n_event, n_centroids)``
centroids : Centroids
The centroids on which this hazard is defined.
date : np.array of int
The ordinal representation of the date of each event.
haz_type : str (optional)
String indicating the hazard type (e.g., ``TC`` for tropical cyclone). This
parameter is passed to the hazard ``Tag``.
units : str (optional)
The physical units of the intensity (used for plotting)
frequency : np.array of float (optional)
Expand Down
32 changes: 16 additions & 16 deletions climada/hazard/test/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ def dummy_hazard():
[4.3, 2.1, 1.0], [5.3, 0.2, 0.0]])
units = 'm/s'

return Hazard(haz_type,
intensity,
return Hazard(intensity,
centroids,
date,
haz_type,
units,
frequency,
frequency_unit,
Expand Down Expand Up @@ -86,10 +86,10 @@ def setUp(self):
self.intensity = sparse.csr_matrix([[1, 2], [1, 2], [1, 2]])
self.fraction = sparse.csr_matrix([[1, 2], [1, 2], [1, 2]])

self.hazard = Hazard(self.haz_type,
self.intensity,
self.hazard = Hazard(self.intensity,
self.centroids,
self.date,
self.haz_type,
event_id=self.event_id,
event_name=self.event_name,
frequency=self.frequency,
Expand All @@ -101,32 +101,32 @@ def good_hazard(self):

def test_minimal_hazard(self):
"""Check if stating only the required parameters works"""
Hazard(self.haz_type,
self.intensity,
Hazard(self.intensity,
self.centroids,
self.date,
self.haz_type,
fast=False)

def test_fast_init_fail(self):
"""Check if fast init correctly disables internal check"""
frequency = np.array([1, 2])
# Not fast: Should raise exception
with self.assertRaises(ValueError) as cm:
Hazard(self.haz_type,
self.intensity,
self.centroids,
self.date,
event_id=self.event_id,
event_name=self.event_name,
frequency=frequency,
fraction=self.fraction)
Hazard(self.intensity,
self.centroids,
self.date,
self.haz_type,
event_id=self.event_id,
event_name=self.event_name,
frequency=frequency,
fraction=self.fraction)
self.assertIn("Invalid Hazard.frequency", str(cm.exception))

# Fast: Skip check
Hazard(self.haz_type,
self.intensity,
Hazard(self.intensity,
self.centroids,
self.date,
self.haz_type,
event_id=self.event_id,
event_name=self.event_name,
frequency=frequency,
Expand Down

0 comments on commit 06c4065

Please sign in to comment.