Skip to content

Commit a555820

Browse files
added max_scaling_factor for linear_scaling (*) and delta_method (*) to avoid unrealistic results in some cases
1 parent 60ab4a4 commit a555820

File tree

4 files changed

+30
-5
lines changed

4 files changed

+30
-5
lines changed

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Bias-Adjustment-Python
1+
# Bias adjustment/correction procedures for climatic reasearch
22

33
<div style="text-align: center">
44

@@ -9,7 +9,7 @@
99

1010
</div>
1111

12-
Collection of different scale- and distribution-based bias adjustment techniques for climatic research. (see `examples.ipynb` for help)
12+
Collection of different scale- and distribution-based bias adjustment techniques for climatic research (see `examples.ipynb` for help).
1313

1414
Bias adjustment procedures in Python are very slow, so they should not be used on large data sets.
1515
A C++ implementation that works way faster can be found [here](https://github.com/btschwertfeger/Bias-Adjustment-Cpp).
@@ -88,6 +88,11 @@ qdm_result = cm.adjust_3d( # 3d = 2 spatial and 1 time dimension
8888
# ratio based variables like precipitation)
8989
```
9090

91+
Notes:
92+
93+
- When using the `adjust_3d` method you have to specify the method by name.
94+
- For the multiplicative linear scaling and delta method is a maximum scaling factor of 10 set. This can be changed by the `max_scaling_factor` parameter.
95+
9196
---
9297

9398
## Examples (see repository on [GitHub](https://github.com/btschwertfeger/Bias-Adjustment-Python))

cmethods/CMethods.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ def __init__(self, method: str, available_methods: list):
5454
ADDITIVE = ['+', 'add']
5555
MULTIPLICATIVE = ['*', 'mult']
5656

57+
MAX_SCALING_FACTOR = 10
58+
5759
def __init__(self):
5860
pass
5961

@@ -289,7 +291,14 @@ def linear_scaling(cls,
289291
if group != None: return cls.grouped_correction(method='linear_scaling', obs=obs, simh=simh, simp=simp, group=group, kind=kind, **kwargs)
290292
else:
291293
if kind in cls.ADDITIVE: return np.array(simp) + (np.nanmean(obs) - np.nanmean(simh)) # Eq. 1
292-
elif kind in cls.MULTIPLICATIVE: return np.array(simp) * (np.nanmean(obs) / np.nanmean(simh)) # Eq. 2
294+
elif kind in cls.MULTIPLICATIVE:
295+
scaling_factor = (np.nanmean(obs) / np.nanmean(simh))
296+
if scaling_factor > 0 and scaling_factor > abs(kwargs.get('max_scaling_factor', cls.MAX_SCALING_FACTOR)):
297+
return np.array(simp) * abs(kwargs.get('max_scaling_factor', cls.MAX_SCALING_FACTOR))
298+
elif scaling_factor < 0 and scaling_factor < -abs(kwargs.get('max_scaling_factor', cls.MAX_SCALING_FACTOR)):
299+
return np.array(simp) * -abs(kwargs.get('max_scaling_factor', cls.MAX_SCALING_FACTOR))
300+
else:
301+
return np.array(simp) * scaling_factor # Eq. 2
293302
else: raise ValueError('Scaling type invalid. Valid options for param kind: "+" and "*"')
294303

295304
# ? -----========= V A R I A N C E - S C A L I N G =========------
@@ -404,7 +413,14 @@ def delta_method(cls,
404413
if group != None: return cls.grouped_correction(method='delta_method', obs=obs, simh=simh, simp=simp, group=group, kind=kind, **kwargs)
405414
else:
406415
if kind in cls.ADDITIVE: return np.array(obs) + (np.nanmean(simp) - np.nanmean(simh)) # Eq. 1
407-
elif kind in cls.MULTIPLICATIVE: return np.array(obs) * (np.nanmean(simp) / np.nanmean(simh)) # Eq. 2
416+
elif kind in cls.MULTIPLICATIVE:
417+
scaling_factor = (np.nanmean(simp) / np.nanmean(simh))
418+
if scaling_factor > 0 and scaling_factor > abs(kwargs.get('max_scaling_factor', cls.MAX_SCALING_FACTOR)):
419+
return np.array(obs) * abs(kwargs.get('max_scaling_factor', cls.MAX_SCALING_FACTOR))
420+
elif scaling_factor < 0 and scaling_factor < -abs(kwargs.get('max_scaling_factor', cls.MAX_SCALING_FACTOR)):
421+
return np.array(obs) * -abs(kwargs.get('max_scaling_factor', cls.MAX_SCALING_FACTOR))
422+
else:
423+
return np.array(obs) * scaling_factor # Eq. 2
408424
else: raise ValueError(f'{kind} not implemented! Use "+" or "*" instead.')
409425

410426

cmethods/__version__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
VERSION = (0,5,3)
1+
VERSION = (0,5,4)
22
__version__ = '.'.join(map(str, VERSION))

cmethods/tests/testing.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
sys.path.append('../../')
99
from cmethods.CMethods import CMethods
1010

11+
if True: # local space
12+
import cmethods
13+
print(cmethods.__file__)
14+
1115
formatter = logging.Formatter(
1216
fmt='%(asctime)s %(module)s,line: %(lineno)d %(levelname)8s | %(message)s',
1317
datefmt='%Y-%m-%d %H:%M:%S' # %I:%M:%S %p AM|PM format

0 commit comments

Comments
 (0)