Skip to content

Commit 17542cd

Browse files
added detrended QM (add)
1 parent a102554 commit 17542cd

File tree

6 files changed

+385
-38
lines changed

6 files changed

+385
-38
lines changed

cmethods/CMethods.py

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -433,18 +433,35 @@ def quantile_mapping(cls,
433433

434434
if group != None: return cls.grouped_correction(method='quantile_mapping', obs=obs, simh=simh, simp=simp, group=group, n_quantiles=n_quantiles, kind=kind, **kwargs)
435435
elif kind == '+':
436-
# create np.array to achieve higher accuracy (idk why)
436+
res = simp.copy(deep=True)
437437
obs, simh, simp = np.array(obs), np.array(simh), np.array(simp)
438-
438+
439439
global_max = max(np.amax(obs), np.amax(simh))
440440
global_min = min(np.amin(obs), np.amin(simh))
441441
wide = abs(global_max - global_min) / n_quantiles
442442
xbins = np.arange(global_min, global_max + wide, wide)
443-
443+
444444
cdf_obs = cls.get_cdf(obs, xbins)
445-
cdf_simh = cls.get_cdf(simh, xbins)
446-
epsilon = np.interp(simp, xbins, cdf_simh) # Eq. 1
447-
return cls.get_inverse_of_cdf(cdf_obs, epsilon, xbins) # Eq. 1
445+
cdf_simh = cls.get_cdf(simh, xbins)
446+
447+
if kwargs.get('detrended', False):
448+
'''detrended => shift mean of $T_{sim,p}$ to range of $T_{sim,h}$ to adjust extremes'''
449+
for month, idxs in res.groupby('time.month').groups.items():
450+
m_simh, m_simp = [], []
451+
for idx in idxs:
452+
m_simh.append(simh[idx])
453+
m_simp.append(simp[idx])
454+
455+
m_simh_mean = np.nanmean(m_simh)
456+
m_simp_mean = np.nanmean(m_simp)
457+
458+
epsilon = np.interp((m_simp - m_simp_mean) + m_simh_mean, xbins, cdf_simh)
459+
X = (cls.get_inverse_of_cdf(cdf_obs, epsilon, xbins) + m_simp_mean) - m_simh_mean
460+
for i, idx in enumerate(idxs): res.values[idx] = X[i]
461+
else:
462+
epsilon = np.interp(simp, xbins, cdf_simh) # Eq. 1
463+
res.values = cls.get_inverse_of_cdf(cdf_obs, epsilon, xbins) # Eq. 1
464+
return res
448465

449466
elif kind == '*':
450467
''' Inspired by Adrian Tompkins [email protected] posted here:

cmethods/__version__.py

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

cmethods/requirements.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
xarray
22
numpy
3-
tqdm
4-
matplotlib
3+
tqdm

examples.ipynb

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@
7575
},
7676
{
7777
"cell_type": "code",
78-
"execution_count": 5,
78+
"execution_count": 45,
7979
"metadata": {},
8080
"outputs": [],
8181
"source": [
@@ -116,7 +116,7 @@
116116
{
117117
"data": {
118118
"text/plain": [
119-
"<matplotlib.legend.Legend at 0x7fd5807c0af0>"
119+
"<matplotlib.legend.Legend at 0x7f8253b132e0>"
120120
]
121121
},
122122
"execution_count": 6,
@@ -178,14 +178,14 @@
178178
},
179179
{
180180
"cell_type": "code",
181-
"execution_count": 13,
181+
"execution_count": 8,
182182
"metadata": {},
183183
"outputs": [
184184
{
185185
"name": "stderr",
186186
"output_type": "stream",
187187
"text": [
188-
"100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████| 4/4 [00:00<00:00, 22.23it/s]\n"
188+
"100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 4/4 [00:00<00:00, 19.02it/s]\n"
189189
]
190190
}
191191
],
@@ -210,16 +210,16 @@
210210
},
211211
{
212212
"cell_type": "code",
213-
"execution_count": null,
213+
"execution_count": 9,
214214
"metadata": {},
215215
"outputs": [
216216
{
217217
"data": {
218218
"text/plain": [
219-
"<matplotlib.legend.Legend at 0x7fd5825231f0>"
219+
"<matplotlib.legend.Legend at 0x7f8254ac6a30>"
220220
]
221221
},
222-
"execution_count": 14,
222+
"execution_count": 9,
223223
"metadata": {},
224224
"output_type": "execute_result"
225225
},
@@ -262,9 +262,9 @@
262262
"name": "stderr",
263263
"output_type": "stream",
264264
"text": [
265-
"100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████| 4/4 [00:00<00:00, 49.56it/s]\n",
266-
"100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████| 4/4 [02:04<00:00, 31.00s/it]\n",
267-
"100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████| 4/4 [00:00<00:00, 71.26it/s]\n"
265+
"100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 4/4 [00:00<00:00, 34.63it/s]\n",
266+
"100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 4/4 [02:35<00:00, 38.96s/it]\n",
267+
"100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 4/4 [00:00<00:00, 25.12it/s]\n"
268268
]
269269
}
270270
],
@@ -282,7 +282,7 @@
282282
{
283283
"data": {
284284
"text/plain": [
285-
"<matplotlib.legend.Legend at 0x7fc918835e50>"
285+
"<matplotlib.legend.Legend at 0x7f82549d0df0>"
286286
]
287287
},
288288
"execution_count": 11,
@@ -706,14 +706,14 @@
706706
" lat int64 23\n",
707707
" lon int64 0\n",
708708
"Attributes:\n",
709-
" units: °C</pre><div class='xr-wrap' style='display:none'><div class='xr-header'><div class='xr-obj-type'>xarray.DataArray</div><div class='xr-array-name'>'tas'</div><ul class='xr-dim-list'><li><span class='xr-has-index'>time</span>: 10950</li></ul></div><ul class='xr-sections'><li class='xr-section-item'><div class='xr-array-wrap'><input id='section-0572dd97-27dc-46ec-a3ce-ef071652d211' class='xr-array-in' type='checkbox' checked><label for='section-0572dd97-27dc-46ec-a3ce-ef071652d211' title='Show/hide data repr'><svg class='icon xr-icon-database'><use xlink:href='#icon-database'></use></svg></label><div class='xr-array-preview xr-preview'><span>-23.09 -23.42 -23.18 -23.04 -22.76 ... -25.33 -25.58 -26.33 -25.64</span></div><div class='xr-array-data'><pre>array([-23.09421931, -23.41702292, -23.17541147, ..., -25.57722903,\n",
710-
" -26.33119336, -25.63539539])</pre></div></div></li><li class='xr-section-item'><input id='section-10693759-15f7-4028-bd95-abb3fa4dea5c' class='xr-section-summary-in' type='checkbox' checked><label for='section-10693759-15f7-4028-bd95-abb3fa4dea5c' class='xr-section-summary' >Coordinates: <span>(3)</span></label><div class='xr-section-inline-details'></div><div class='xr-section-details'><ul class='xr-var-list'><li class='xr-var-item'><div class='xr-var-name'><span class='xr-has-index'>time</span></div><div class='xr-var-dims'>(time)</div><div class='xr-var-dtype'>object</div><div class='xr-var-preview xr-preview'>2001-01-01 00:00:00 ... 2030-12-...</div><input id='attrs-059b5c55-8a71-42dc-89c3-3a456af1c8a8' class='xr-var-attrs-in' type='checkbox' disabled><label for='attrs-059b5c55-8a71-42dc-89c3-3a456af1c8a8' title='Show/Hide attributes'><svg class='icon xr-icon-file-text2'><use xlink:href='#icon-file-text2'></use></svg></label><input id='data-2218d843-50b5-4f62-a15d-f5639a24b5ea' class='xr-var-data-in' type='checkbox'><label for='data-2218d843-50b5-4f62-a15d-f5639a24b5ea' title='Show/Hide data repr'><svg class='icon xr-icon-database'><use xlink:href='#icon-database'></use></svg></label><div class='xr-var-attrs'><dl class='xr-attrs'></dl></div><div class='xr-var-data'><pre>array([cftime.DatetimeNoLeap(2001, 1, 1, 0, 0, 0, 0, has_year_zero=True),\n",
709+
" units: °C</pre><div class='xr-wrap' style='display:none'><div class='xr-header'><div class='xr-obj-type'>xarray.DataArray</div><div class='xr-array-name'>'tas'</div><ul class='xr-dim-list'><li><span class='xr-has-index'>time</span>: 10950</li></ul></div><ul class='xr-sections'><li class='xr-section-item'><div class='xr-array-wrap'><input id='section-25c61a00-f805-42b3-843b-cf344901210c' class='xr-array-in' type='checkbox' checked><label for='section-25c61a00-f805-42b3-843b-cf344901210c' title='Show/hide data repr'><svg class='icon xr-icon-database'><use xlink:href='#icon-database'></use></svg></label><div class='xr-array-preview xr-preview'><span>-23.09 -23.42 -23.18 -23.04 -22.76 ... -25.33 -25.58 -26.33 -25.64</span></div><div class='xr-array-data'><pre>array([-23.09421931, -23.41702292, -23.17541147, ..., -25.57722903,\n",
710+
" -26.33119336, -25.63539539])</pre></div></div></li><li class='xr-section-item'><input id='section-b452f11d-eb05-442c-ad96-79ad7d865f2c' class='xr-section-summary-in' type='checkbox' checked><label for='section-b452f11d-eb05-442c-ad96-79ad7d865f2c' class='xr-section-summary' >Coordinates: <span>(3)</span></label><div class='xr-section-inline-details'></div><div class='xr-section-details'><ul class='xr-var-list'><li class='xr-var-item'><div class='xr-var-name'><span class='xr-has-index'>time</span></div><div class='xr-var-dims'>(time)</div><div class='xr-var-dtype'>object</div><div class='xr-var-preview xr-preview'>2001-01-01 00:00:00 ... 2030-12-...</div><input id='attrs-74adbc68-6267-4aca-9bae-942e535ed2db' class='xr-var-attrs-in' type='checkbox' disabled><label for='attrs-74adbc68-6267-4aca-9bae-942e535ed2db' title='Show/Hide attributes'><svg class='icon xr-icon-file-text2'><use xlink:href='#icon-file-text2'></use></svg></label><input id='data-8d0036f4-58d0-4921-ad43-3b0a29c82a1e' class='xr-var-data-in' type='checkbox'><label for='data-8d0036f4-58d0-4921-ad43-3b0a29c82a1e' title='Show/Hide data repr'><svg class='icon xr-icon-database'><use xlink:href='#icon-database'></use></svg></label><div class='xr-var-attrs'><dl class='xr-attrs'></dl></div><div class='xr-var-data'><pre>array([cftime.DatetimeNoLeap(2001, 1, 1, 0, 0, 0, 0, has_year_zero=True),\n",
711711
" cftime.DatetimeNoLeap(2001, 1, 2, 0, 0, 0, 0, has_year_zero=True),\n",
712712
" cftime.DatetimeNoLeap(2001, 1, 3, 0, 0, 0, 0, has_year_zero=True), ...,\n",
713713
" cftime.DatetimeNoLeap(2030, 12, 29, 0, 0, 0, 0, has_year_zero=True),\n",
714714
" cftime.DatetimeNoLeap(2030, 12, 30, 0, 0, 0, 0, has_year_zero=True),\n",
715715
" cftime.DatetimeNoLeap(2030, 12, 31, 0, 0, 0, 0, has_year_zero=True)],\n",
716-
" dtype=object)</pre></div></li><li class='xr-var-item'><div class='xr-var-name'><span>lat</span></div><div class='xr-var-dims'>()</div><div class='xr-var-dtype'>int64</div><div class='xr-var-preview xr-preview'>23</div><input id='attrs-1ef61f37-fac6-4437-96bd-af484506452b' class='xr-var-attrs-in' type='checkbox' disabled><label for='attrs-1ef61f37-fac6-4437-96bd-af484506452b' title='Show/Hide attributes'><svg class='icon xr-icon-file-text2'><use xlink:href='#icon-file-text2'></use></svg></label><input id='data-527ee044-5384-4cf0-bae0-c6a7faf268c2' class='xr-var-data-in' type='checkbox'><label for='data-527ee044-5384-4cf0-bae0-c6a7faf268c2' title='Show/Hide data repr'><svg class='icon xr-icon-database'><use xlink:href='#icon-database'></use></svg></label><div class='xr-var-attrs'><dl class='xr-attrs'></dl></div><div class='xr-var-data'><pre>array(23)</pre></div></li><li class='xr-var-item'><div class='xr-var-name'><span>lon</span></div><div class='xr-var-dims'>()</div><div class='xr-var-dtype'>int64</div><div class='xr-var-preview xr-preview'>0</div><input id='attrs-2af79482-ff90-406f-84bb-4aa467287278' class='xr-var-attrs-in' type='checkbox' disabled><label for='attrs-2af79482-ff90-406f-84bb-4aa467287278' title='Show/Hide attributes'><svg class='icon xr-icon-file-text2'><use xlink:href='#icon-file-text2'></use></svg></label><input id='data-636662cc-4dd7-4a52-ada6-8d46f32f14af' class='xr-var-data-in' type='checkbox'><label for='data-636662cc-4dd7-4a52-ada6-8d46f32f14af' title='Show/Hide data repr'><svg class='icon xr-icon-database'><use xlink:href='#icon-database'></use></svg></label><div class='xr-var-attrs'><dl class='xr-attrs'></dl></div><div class='xr-var-data'><pre>array(0)</pre></div></li></ul></div></li><li class='xr-section-item'><input id='section-ffe4a215-cef7-4517-9650-12b986fce76b' class='xr-section-summary-in' type='checkbox' checked><label for='section-ffe4a215-cef7-4517-9650-12b986fce76b' class='xr-section-summary' >Attributes: <span>(1)</span></label><div class='xr-section-inline-details'></div><div class='xr-section-details'><dl class='xr-attrs'><dt><span>units :</span></dt><dd>°C</dd></dl></div></li></ul></div></div>"
716+
" dtype=object)</pre></div></li><li class='xr-var-item'><div class='xr-var-name'><span>lat</span></div><div class='xr-var-dims'>()</div><div class='xr-var-dtype'>int64</div><div class='xr-var-preview xr-preview'>23</div><input id='attrs-d9bd6002-ca54-424c-9998-380fb899c7dd' class='xr-var-attrs-in' type='checkbox' disabled><label for='attrs-d9bd6002-ca54-424c-9998-380fb899c7dd' title='Show/Hide attributes'><svg class='icon xr-icon-file-text2'><use xlink:href='#icon-file-text2'></use></svg></label><input id='data-fcba05ed-1856-4ed4-8941-6cc6ff9df52b' class='xr-var-data-in' type='checkbox'><label for='data-fcba05ed-1856-4ed4-8941-6cc6ff9df52b' title='Show/Hide data repr'><svg class='icon xr-icon-database'><use xlink:href='#icon-database'></use></svg></label><div class='xr-var-attrs'><dl class='xr-attrs'></dl></div><div class='xr-var-data'><pre>array(23)</pre></div></li><li class='xr-var-item'><div class='xr-var-name'><span>lon</span></div><div class='xr-var-dims'>()</div><div class='xr-var-dtype'>int64</div><div class='xr-var-preview xr-preview'>0</div><input id='attrs-8c59a082-8053-48de-b814-d392c32079b2' class='xr-var-attrs-in' type='checkbox' disabled><label for='attrs-8c59a082-8053-48de-b814-d392c32079b2' title='Show/Hide attributes'><svg class='icon xr-icon-file-text2'><use xlink:href='#icon-file-text2'></use></svg></label><input id='data-2cc96566-8772-4ef0-9e63-46ee4bf1fd8e' class='xr-var-data-in' type='checkbox'><label for='data-2cc96566-8772-4ef0-9e63-46ee4bf1fd8e' title='Show/Hide data repr'><svg class='icon xr-icon-database'><use xlink:href='#icon-database'></use></svg></label><div class='xr-var-attrs'><dl class='xr-attrs'></dl></div><div class='xr-var-data'><pre>array(0)</pre></div></li></ul></div></li><li class='xr-section-item'><input id='section-64a067b0-0325-4092-a018-1bc763005982' class='xr-section-summary-in' type='checkbox' checked><label for='section-64a067b0-0325-4092-a018-1bc763005982' class='xr-section-summary' >Attributes: <span>(1)</span></label><div class='xr-section-inline-details'></div><div class='xr-section-details'><dl class='xr-attrs'><dt><span>units :</span></dt><dd>°C</dd></dl></div></li></ul></div></div>"
717717
],
718718
"text/plain": [
719719
"<xarray.DataArray 'tas' (time: 10950)>\n",
@@ -736,6 +736,13 @@
736736
"ls_result"
737737
]
738738
},
739+
{
740+
"cell_type": "markdown",
741+
"metadata": {},
742+
"source": [
743+
"____"
744+
]
745+
},
739746
{
740747
"cell_type": "code",
741748
"execution_count": null,

setup.py

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
# What packages are required for this module to be executed?
1919
REQUIRED = [
20-
'xarray', 'numpy', 'tqdm' # <- always conflicts on install
20+
'xarray', 'tqdm' # <- always conflicts on install with tqdm
2121
]
2222

2323
# What packages are optional?
@@ -27,8 +27,7 @@
2727

2828
here = os.path.abspath(os.path.dirname(__file__))
2929

30-
# Import the README and use it as the long-description.
31-
# Note: this will only work if 'README.md' is present in your MANIFEST.in file!
30+
# only works if 'README.md' is present in your MANIFEST.in file!
3231
try:
3332
with io.open(os.path.join(here, 'README.md'), encoding='utf-8') as f:
3433
long_description = f'\n{f.read()}'
@@ -72,11 +71,6 @@ def run(self):
7271

7372
self.status('Uploading the package to PyPI via Twine…')
7473
os.system('twine upload dist/*')
75-
76-
# self.status('Pushing git tags…')
77-
# os.system(f'git tag v{about["__version__"]}')
78-
# os.system('git push --tags')
79-
8074
sys.exit()
8175

8276
class TestUploadCommand(Command):
@@ -106,7 +100,7 @@ def run(self):
106100
os.system(f'{sys.executable} setup.py sdist bdist_wheel --universal')
107101

108102
self.status('Uploading the package to PyPI via Twine…')
109-
os.system('twine upload -r testpypi dist/*')#--repository-url https://test.pypi.org/legacy/ dist/*')
103+
os.system('twine upload -r testpypi dist/*')
110104

111105
sys.exit()
112106

@@ -121,12 +115,6 @@ def run(self):
121115
python_requires=REQUIRES_PYTHON,
122116
url=URL,
123117
packages=find_packages(exclude=["tests", "*.tests", "*.tests.*", "tests.*"]),
124-
# If your package is a single module, use this instead of 'packages':
125-
# py_modules=['mypackage'],
126-
127-
# entry_points={
128-
# 'console_scripts': ['mycli=mymodule:cli'],
129-
# },
130118
install_requires=REQUIRED,
131119
extras_require=EXTRAS,
132120
include_package_data=True,

0 commit comments

Comments
 (0)