Skip to content

Commit 27349f3

Browse files
adapt to new defaults
1 parent 4f86e46 commit 27349f3

File tree

3 files changed

+31
-31
lines changed

3 files changed

+31
-31
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ Removed:
4040
- `Exposures.write_hdf5` pickles geometry data in WKB format, which is faster and more sustainable. [#1051](https://github.com/CLIMADA-project/climada_python/pull/1051)
4141
- The online documentation has been completely overhauled, now uses PyData theme: [#977](https://github.com/CLIMADA-project/climada_python/pull/977)
4242
- Add `climada.hazard.xarray` module with helper structures for reading Hazard objects from `xarray` data [#1063](https://github.com/CLIMADA-project/climada_python/pull/1063)
43-
- The output of the `impact_yearset` was changed to only contain attributes corresponding to the yearly impact set. The default parameters of `impact_yearset`, `sample_events`, and `impact_yearset_from_sampling_vect` functions of the `climada.util.yearsets` module have been changed to sampling with replacement and not applying the correction factor to the impact yearsets. The application of the correction factor and the frequency of the resulting yearly impact object are corrected. [#1075](https://github.com/CLIMADA-project/climada_python/pull/1075)
43+
- The output of the `impact_yearset` was changed to only contain attributes corresponding to the yearly impact set. The application of the correction factor and the frequency of the resulting yearly impact object are corrected. [#1075](https://github.com/CLIMADA-project/climada_python/pull/1075)
4444

4545
### Fixed
4646

climada/util/yearsets.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626

2727
def impact_yearset(
28-
imp, sampled_years, lam=None, correction_fac=False, with_replacement=True, seed=None
28+
imp, sampled_years, lam=None, correction_fac=True, with_replacement=False, seed=None
2929
):
3030
"""Create a yearset of impacts (yimp) containing a probabilistic impact for each year
3131
in the sampled_years list by sampling events from the impact received as input with a
@@ -45,13 +45,13 @@ def impact_yearset(
4545
events per year. If no lambda value is given, the default lam = sum(imp.frequency)
4646
is used. Default is None.
4747
correction_fac : boolean, optional
48-
If True a correction factor is applied to the resulting yimp. It is
48+
If True, a correction factor is applied to the resulting yimp. It is
4949
scaled in such a way that the expected annual impact (eai) of the yimp
50-
equals the eai of the input impact. Defaults to False.
50+
equals the eai of the input impact. Defaults to True.
5151
with_replacement : bool, optional
5252
If True, impact events are sampled with replacement. If False, events are sampled
5353
without replacement. Sampling without replacement can yield distorted samples if
54-
frequencies of different events are unqual. Defaults to True.
54+
frequencies of different events are unqual. Defaults to False.
5555
seed : Any, optional
5656
seed for the default bit generator
5757
default: None
@@ -86,7 +86,7 @@ def impact_yearset(
8686

8787

8888
def impact_yearset_from_sampling_vect(
89-
imp, sampled_years, sampling_vect, correction_fac=False
89+
imp, sampled_years, sampling_vect, correction_fac=True
9090
):
9191
"""Create a yearset of impacts (yimp) containing a probabilistic impact for each year
9292
in the sampled_years list by sampling events from the impact received as input following
@@ -112,7 +112,7 @@ def impact_yearset_from_sampling_vect(
112112
correction_fac : boolean, optional
113113
If True a correction factor is applied to the resulting yimp. It is
114114
scaled in such a way that the expected annual impact (eai) of the yimp
115-
equals the eai of the input impact. Defaults to False.
115+
equals the eai of the input impact. Defaults to True.
116116
117117
Returns
118118
-------
@@ -170,7 +170,7 @@ def sample_from_poisson(n_sampled_years, lam, seed=None):
170170
return np.round(np.random.poisson(lam=lam, size=n_sampled_years)).astype("int")
171171

172172

173-
def sample_events(events_per_year, freqs_orig, with_replacement=True, seed=None):
173+
def sample_events(events_per_year, freqs_orig, with_replacement=False, seed=None):
174174
"""Sample events uniformely from an array (indices_orig) without replacement
175175
(if sum(events_per_year) > n_input_events the input events are repeated
176176
(tot_n_events/n_input_events) times, by ensuring that the same events doens't
@@ -185,7 +185,7 @@ def sample_events(events_per_year, freqs_orig, with_replacement=True, seed=None)
185185
with_replacement : bool, optional
186186
If True, impact events are sampled with replacement. If False, events are sampled
187187
without replacement. Sampling without replacement can yield distorted samples if
188-
frequencies of different events are unqual. Defaults to True.
188+
frequencies of different events are unqual. Defaults to False.
189189
seed : Any, optional
190190
seed for the default bit generator.
191191
Default: None
@@ -209,7 +209,7 @@ def sample_events(events_per_year, freqs_orig, with_replacement=True, seed=None)
209209
LOGGER.warning(
210210
"The frequencies of the different events are not equal. This can lead to "
211211
"distorted sampling if the frequencies vary significantly. To avoid this, "
212-
"please set with_replacement=True to sample with replacement instead."
212+
"please set `with_replacement=True` to sample with replacement instead."
213213
)
214214

215215
indices = indices_orig
@@ -226,7 +226,7 @@ def sample_events(events_per_year, freqs_orig, with_replacement=True, seed=None)
226226
)
227227

228228
# if not enough events remaining, append original events
229-
if len(indices) < amount_events or len(indices) == 0:
229+
if len(np.unique(indices)) < amount_events or len(indices) == 0:
230230
indices = np.append(indices, indices_orig)
231231
freqs = np.append(freqs, freqs_orig)
232232

doc/user-guide/climada_util_yearsets.ipynb

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
"The function `impact_yearset` performs all these computational steps, taking an `imp` and the list of sampled_years (`sampled_years`) as input. The output of the function is the `yimp` object and the `sampling_vect`.\n",
1212
"Moreover, a `sampling_vect` (generated in a previous run) can be provided as optional input and the user can custom-define the Poisson parameter `lam`. Reapplying the same sampling_vect does not only allow to reproduce the generated `yimp`, but also for a physically consistent way of sampling impacts caused by different hazards. \n",
1313
"\n",
14-
"*Sampling options.* Per default, impact events are sampled with replacement. When setting `with_replacement=False`, the impact events are sampled without replacement (given that the original impact object contains enough events). Note that sampling without replacement can lead to distorted sampling if the frequencies of the different impacts (`imp.frequency`) are not equal.\n",
14+
"*Sampling options.* Per default, impact events are sampled without replacement (given that the original impact object contains enough events). When setting `with_replacement=True`, the impact events are sampled with replacement . Note that sampling without replacement can lead to distorted sampling if the frequencies of the different impacts (`imp.frequency`) are not equal.\n",
1515
"\n",
16-
"*Correction factor.* By setting `correction_fac=False`, a correction factor is applied uniformly to all yearly impacts, such that the final `yimp` object has the same average annual impact than `imp`, the original impact object.\n",
16+
"*Correction factor.* By default, a correction factor is applied uniformly to all yearly impacts, such that the final `yimp` object has the same average annual impact than `imp`, the original impact object. Applying the correction factor can be avoided by setting `correction_fac=False`.\n",
1717
"\n"
1818
]
1919
},
@@ -64,7 +64,7 @@
6464
{
6565
"data": {
6666
"text/plain": [
67-
"array([1, 2, 1, 0, 1, 0, 0, 2, 1, 2])"
67+
"array([0, 1, 1, 0, 2, 3, 3, 0, 2, 3])"
6868
]
6969
},
7070
"execution_count": 2,
@@ -90,16 +90,16 @@
9090
{
9191
"data": {
9292
"text/plain": [
93-
"[array([2]),\n",
94-
" array([1, 0]),\n",
95-
" array([2]),\n",
93+
"[array([], dtype=int64),\n",
94+
" array([0]),\n",
95+
" array([1]),\n",
9696
" array([], dtype=int64),\n",
97-
" array([2]),\n",
97+
" array([4, 2]),\n",
98+
" array([2, 0, 4]),\n",
99+
" array([5, 1, 3]),\n",
98100
" array([], dtype=int64),\n",
99-
" array([], dtype=int64),\n",
100-
" array([1, 4]),\n",
101-
" array([2]),\n",
102-
" array([4, 4])]"
101+
" array([5, 3]),\n",
102+
" array([5, 3, 1])]"
103103
]
104104
},
105105
"execution_count": 3,
@@ -121,7 +121,7 @@
121121
{
122122
"data": {
123123
"text/plain": [
124-
"array([ 4, 2, 4, 0, 4, 0, 0, 62, 4, 120])"
124+
"array([ 0, 0, 2, 0, 64, 64, 70, 0, 68, 70])"
125125
]
126126
},
127127
"execution_count": 4,
@@ -143,7 +143,7 @@
143143
{
144144
"data": {
145145
"text/plain": [
146-
"1.34"
146+
"0.7928994082840237"
147147
]
148148
},
149149
"execution_count": 5,
@@ -180,9 +180,9 @@
180180
"name": "stdout",
181181
"output_type": "stream",
182182
"text": [
183-
"yimp.at_event = [ 2 0 0 122 14 0 0 246 4 124]\n",
184-
"imp_per_year = [ 2 0 0 122 14 0 0 246 4 124]\n",
185-
"The expected annual impact 51.2 differs from the one of the original impact (26.8).\n"
183+
"yimp.at_event = [ 2 0 0 70 130 0 0 128 68 62]\n",
184+
"imp_per_year = [ 2 0 0 70 130 0 0 128 68 62]\n",
185+
"The expected annual impact 46.0 differs from the one of the original impact (26.8).\n"
186186
]
187187
}
188188
],
@@ -228,9 +228,9 @@
228228
"name": "stdout",
229229
"output_type": "stream",
230230
"text": [
231-
"2025-07-22 17:10:20,134 - climada.util.yearsets - INFO - The correction factor is 0.5234375.\n",
232-
"yimp.at_event = [ 1.05 0. 0. 63.86 7.33 0. 0. 128.77 2.09 64.91]\n",
233-
"imp_per_year = [ 1.05 0. 0. 63.86 7.33 0. 0. 128.77 2.09 64.91]\n",
231+
"2025-09-19 15:39:54,743 - climada.util.yearsets - INFO - The correction factor is 0.5826086956521739.\n",
232+
"yimp.at_event = [ 1.17 0. 0. 40.78 75.74 0. 0. 74.57 39.62 36.12]\n",
233+
"imp_per_year = [ 1.17 0. 0. 40.78 75.74 0. 0. 74.57 39.62 36.12]\n",
234234
"The expected annual impact 26.8 is equal to the one of the original impact (26.8).\n"
235235
]
236236
}
@@ -241,7 +241,7 @@
241241
" imp, sampled_years, correction_fac=True, seed=example_seed\n",
242242
")\n",
243243
"\n",
244-
"# compute correction factor\n",
244+
"# compute correction factor from imp_per_year computed above without correction factor\n",
245245
"correction_factor = yearsets.calculate_correction_fac(imp_per_year, imp)\n",
246246
"\n",
247247
"print(\"yimp.at_event = \", np.round(yimp.at_event, 2))\n",

0 commit comments

Comments
 (0)