Skip to content

Commit e343631

Browse files
adjusted raw strings; copyright notice
1 parent 3d5aced commit e343631

File tree

2 files changed

+50
-46
lines changed

2 files changed

+50
-46
lines changed

cmethods/CMethods.py

Lines changed: 45 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
__email__ = "development@b-schwertfeger.de"
1919
__link__ = "https://b-schwertfeger.de"
2020
__github__ = "https://github.com/btschwertfeger/Bias-Adjustment-Python"
21-
__description__ = " \
21+
__description__ = r" \
2222
Class / Script / Methods to adjust bias estimated in climate data \
2323
\
2424
T = Temperatures ($T$) \
@@ -104,7 +104,7 @@ def adjust_3d(
104104
n_jobs: int = 1,
105105
**kwargs,
106106
) -> xr.core.dataarray.Dataset:
107-
"""Function to adjust 3 dimensional climate data
107+
r"""Function to adjust 3 dimensional climate data
108108
109109
Note: obs, simh and simp has to be in the format (time, lat, lon)
110110
@@ -167,7 +167,9 @@ def adjust_3d(
167167
**kwargs,
168168
)
169169
else:
170-
with multiprocessing.Pool(processes=n_jobs) as pool:
170+
try:
171+
pool = multiprocessing.Pool(processes=n_jobs)
172+
# with multiprocessing.Pool(processes=n_jobs) as pool:
171173
params: List[dict] = [
172174
{
173175
"method": method,
@@ -183,6 +185,10 @@ def adjust_3d(
183185
]
184186
for lat, corrected in enumerate(pool.map(cls.pool_adjust, params)):
185187
result[lat] = corrected
188+
finally:
189+
pool.close()
190+
pool.join()
191+
186192
return result.transpose("time", "lat", "lon")
187193
raise UnknownMethodError(method, cls.METHODS)
188194

@@ -271,7 +277,7 @@ def linear_scaling(
271277
kind: str = "+",
272278
**kwargs,
273279
) -> xr.core.dataarray.DataArray:
274-
"""Method to adjust 1 dimensional climate data by the linear scaling method.
280+
r"""Method to adjust 1 dimensional climate data by the linear scaling method.
275281
276282
----- P A R A M E T E R S -----
277283
@@ -343,7 +349,7 @@ def variance_scaling(
343349
kind: str = "+",
344350
**kwargs,
345351
) -> xr.core.dataarray.DataArray:
346-
"""Method to adjust 1 dimensional climate data by variance scaling method.
352+
r"""Method to adjust 1 dimensional climate data by variance scaling method.
347353
348354
----- P A R A M E T E R S -----
349355
@@ -420,7 +426,7 @@ def delta_method(
420426
kind: str = "+",
421427
**kwargs,
422428
) -> xr.core.dataarray.DataArray:
423-
"""Method to adjust 1 dimensional climate data by delta method.
429+
r"""Method to adjust 1 dimensional climate data by delta method.
424430
425431
----- P A R A M E T E R S -----
426432
@@ -485,11 +491,11 @@ def quantile_mapping(
485491
simh: xr.core.dataarray.DataArray,
486492
simp: xr.core.dataarray.DataArray,
487493
n_quantiles: int,
488-
group: Union[str, None] = None,
494+
# group: Union[str, None] = None,
489495
kind: str = "+",
490496
**kwargs,
491497
) -> xr.core.dataarray.DataArray:
492-
"""Quantile Mapping Bias Correction
498+
r"""Quantile Mapping Bias Correction
493499
494500
----- P A R A M E T E R S -----
495501
@@ -529,18 +535,20 @@ def quantile_mapping(
529535
Alex J. Cannon and Stephen R. Sobie and Trevor Q. Murdock Bias Correction of GCM Precipitation by Quantile Mapping: How Well Do Methods Preserve Changes in Quantiles and Extremes?
530536
https://doi.org/10.1175/JCLI-D-14-00754.1)
531537
"""
532-
533-
if group is not None:
534-
return cls.grouped_correction(
535-
method="quantile_mapping",
536-
obs=obs,
537-
simh=simh,
538-
simp=simp,
539-
group=group,
540-
n_quantiles=n_quantiles,
541-
kind=kind,
542-
**kwargs,
543-
)
538+
# distribution-based adjustment on a grouped basis lead to high deviations
539+
# in the monthly transitions, if group = "time.month". This is also when the group is
540+
# day of year and so on.
541+
# if group is not None:
542+
# return cls.grouped_correction(
543+
# method="quantile_mapping",
544+
# obs=obs,
545+
# simh=simh,
546+
# simp=simp,
547+
# group=group,
548+
# n_quantiles=n_quantiles,
549+
# kind=kind,
550+
# **kwargs,
551+
# )
544552
res = simp.copy(deep=True)
545553
obs, simh, simp = np.array(obs), np.array(simh), np.array(simp)
546554

@@ -617,24 +625,12 @@ def empirical_quantile_mapping(
617625
simp: xr.core.dataarray.DataArray,
618626
n_quantiles: int = 10,
619627
extrapolate: Union[str, None] = None,
620-
group: Union[str, None] = None,
621628
**kwargs,
622629
) -> xr.core.dataarray.DataArray:
623630
"""Method to adjust 1 dimensional climate data by empirical quantile mapping"""
624631
raise ValueError(
625632
"not implemented; please have a look at: https://svn.oss.deltares.nl/repos/openearthtools/trunk/python/applications/hydrotools/hydrotools/statistics/bias_correction.py "
626633
)
627-
# if group is not None:
628-
# return cls.grouped_correction(
629-
# method = 'empirical_quantile_mapping',
630-
# obs = obs,
631-
# simh = simh,
632-
# simp = simp,
633-
# group = group,
634-
# n_quantiles = n_quantiles,
635-
# extrapolate = extrapolate
636-
# )
637-
# else: pass
638634

639635
# ? -----========= Q U A N T I L E - D E L T A - M A P P I N G =========------
640636
@classmethod
@@ -644,11 +640,11 @@ def quantile_delta_mapping(
644640
simh: xr.core.dataarray.DataArray,
645641
simp: xr.core.dataarray.DataArray,
646642
n_quantiles: int,
647-
group: Union[str, None] = None,
643+
# group: Union[str, None] = None,
648644
kind: str = "+",
649645
**kwargs,
650646
) -> xr.core.dataarray.DataArray:
651-
"""Quantile Delta Mapping bias adjustment
647+
r"""Quantile Delta Mapping bias adjustment
652648
653649
----- P A R A M E T E R S -----
654650
@@ -692,17 +688,20 @@ def quantile_delta_mapping(
692688
693689
"""
694690

695-
if group is not None:
696-
return cls.grouped_correction(
697-
method="quantile_delta_mapping",
698-
obs=obs,
699-
simh=simh,
700-
simp=simp,
701-
group=group,
702-
n_quantiles=n_quantiles,
703-
kind=kind,
704-
**kwargs,
705-
)
691+
# distribution-based adjustment on a grouped basis lead to high deviations
692+
# in the monthly transitions, if group = "time.month". This is also when the group is
693+
# day of year and so on.
694+
# if group is not None:
695+
# return cls.grouped_correction(
696+
# method="quantile_delta_mapping",
697+
# obs=obs,
698+
# simh=simh,
699+
# simp=simp,
700+
# group=group,
701+
# n_quantiles=n_quantiles,
702+
# kind=kind,
703+
# **kwargs,
704+
# )
706705
if kind in cls.ADDITIVE:
707706
res = simp.copy(deep=True)
708707
obs, simh, simp = (
@@ -769,7 +768,7 @@ def get_inverse_of_cdf(
769768
insert_cdf: Union[list, np.array],
770769
xbins: Union[list, np.array],
771770
) -> np.array:
772-
"""returns the inverse cummulative distribution function of base_cdf ($F_{base_cdf}\left[insert_cdf\right])$"""
771+
r"""returns the inverse cummulative distribution function of base_cdf ($F_{base_cdf}\left[insert_cdf\right])$"""
773772
return np.interp(insert_cdf, base_cdf, xbins)
774773

775774
@staticmethod

cmethods/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/usr/bin/env python
2+
# -*- coding:utf-8 -*-
3+
# Copyright (C) 2023 Benjamin Thomas Schwertfegerr
4+
# Github: https://github.com/btschwertfeger
5+
#

0 commit comments

Comments
 (0)