-
Notifications
You must be signed in to change notification settings - Fork 1
/
estimate_trends_from_time_series.py
373 lines (286 loc) · 14.7 KB
/
estimate_trends_from_time_series.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
"""
Created on Fri Oct 4 09:47:45 2019
@author: moparthys
code to prepare tendencies based on Man-kendall test
"""
from datetime import datetime
import numpy as np
from scipy.stats import mstats
import mankendall_fortran_repeat_exp2 as m
from multiprocessing import Pool, Lock, current_process
import itertools
import h5py
from time import sleep
import sys, glob, os
import traceback
from timeit import default_timer as timer
import datetime
import psutil
import pathlib
if 0:
from functools import wraps
import errno
import os
import signal
class TimeoutError(Exception):
pass
def timeout(seconds=10, error_message=os.strerror(errno.ETIME)):
def decorator(func):
def _handle_timeout(signum, frame):
raise TimeoutError(error_message)
def wrapper(*args, **kwargs):
signal.signal(signal.SIGALRM, _handle_timeout)
signal.setitimer(signal.ITIMER_REAL,seconds) #used timer instead of alarm
try:
result = func(*args, **kwargs)
finally:
signal.alarm(0)
return result
return wraps(func)(wrapper)
return decorator
@timeout(2.0)
def mk_test_timeout(data_test):
return m.mk_trend(len(data_test), np.arange(len(data_test)), data_test)
def processInput_trends(subchunk, parent_iteration, child_iteration):
"""This is the main file that calculate trends"""
#print('INFO: see pid.<pid>.out to monitor trend computation progress')
#sys.stdout = open('pid.'+str(os.getpid()) + '.out', 'w')
#print('INFO: see trend.out to monitor trend computation progress')
#sys.stdout = open('trend.out', 'a')
## Debug tool to print process Ids
process = psutil.Process(os.getpid())
current = current_process()
print(process, current._identity, '{} Mo'.format(process.memory_info().rss/1024/1024))
if subchunk.input=='box':
print('### Chunk {} > subchunk {} started: COL: [{}:{}] ROW: [{}:{}]'.format(parent_iteration, child_iteration, *subchunk.get_limits('local', 'str')))
write_string0 = (param.hash+"_CHUNK" + np.str(parent_iteration)
+ "_SUBCHUNK" + np.str(child_iteration)
+ "_" + '_'.join(subchunk.get_limits('global', 'str'))
+ '.nc')
subchunk_fname = param.output_path / write_string0
## Check if cache file already exists and must be overwritten
if not param.b_delete:
if subchunk_fname.is_file():
print ('INFO: {} already exists. Use -d option to overwrite it.'.format(write_string0))
return
elif subchunk.input=='points':
print('### Chunk {} > subchunk {} started.'.format(parent_iteration, child_iteration))
print(param.input_file)
str_date_range = param.input_file.stem.replace('timeseries','')
write_string0 = 'merged_trends{}.h5'.format(str_date_range)
subchunk_fname = param.output_path / write_string0
## Result file is always overwritten in the case of point input
## Read the input time series file from main chunk, configured length of time X 500 X 500; it may vary if different chunks are used
hdf_ts = h5py.File(param.input_file, 'r')
## Create temporary storage with size of sub chunks in main chunk, currently configured 100 by 100 blocks
var_temp_output = np.empty([*subchunk.dim,4])
var_temp_output[:] = np.nan
# NaN matrix by default
## Parameters for te loop
b_deb = 0 # flag to print time profiling
t00 = timer()
t000 = timer()
t_mean = 0.
#print(current._identity, f'{process.memory_info().rss/1024/1024} Mo')
offsetx = subchunk.get_limits('local', 'tuple')[0]
offsety = subchunk.get_limits('local', 'tuple')[2]
print_freq = 20
tab_prof_valid = []
tab_prof_zero = []
hf = h5py.File(subchunk_fname, 'w')
for tsvar in hdf_ts['vars'].keys():
for jj_sub in range(subchunk.dim[0]):
#for jj_sub in range(61,80): #debug
# dimension of variable: time,x,y
# preload all the y data here to avoid overhead due to calling Dataset.variables at each iteration in the inner loop
data_test0 = hdf_ts['vars/'+tsvar][:,jj_sub+offsety,offsetx:offsetx+subchunk.dim[1]]
#data_test0 = hdf_ts.variables[tsvar][:500,sub_chunks_x[ii_sub],:]
for ii_sub in range(subchunk.dim[1]):
#for ii_sub in range(55,100):
if b_deb: print('---------------')
if b_deb: print('jj: {} - ii: {} '.format(jj_sub, ii_sub))
t0 = timer()
data_test = data_test0[:,ii_sub]
## remove tie group
data_test[1:][np.diff(data_test)==0.] = np.nan
#data_test=hdf_ts.variables[tsvar][:,sub_chunks_x[ii_sub],sub_chunks_y[jj_sub]]
slope=999.0
if b_deb:
print('t0', timer()-t0)
t0 = timer()
if b_deb: print('Data valid:', data_test.size - np.isnan(data_test).sum(), '/', data_test.size)
if 0:
print('Use mstats')
data_sen=np.ma.masked_array(data_test, mask=np.isnan(data_test))
t0 = timer()
slope, intercept, lo_slope, up_slope = mstats.theilslopes(data_sen, alpha=0.1)
print('slope, intercept, lo_slope, up_slop:')
print(slope, intercept, lo_slope, up_slope)
if b_deb: print('t02', timer()-t0)
np.savetxt('data_test.dat', data_test.T)
sys.exit()
t0 = timer()
# this mstats give correct slope and is consistent with python man-kendall score of Sn; this is fast than Fortran'''
# stats.theilslopes is giving incorrect values when NaN are inside data'''
if b_deb:
print('t2', timer()-t0)
t0 = timer()
if 1:
## orinal mann-kendall test :
bla = data_test[~np.isnan(data_test)]
if bla.size > 0:
#print('min/mean/max/nb/nb_unique', bla.min(), bla.mean(), bla.max(), len(bla), len(np.unique(bla)))
#print(bla)
if len(np.unique(bla))==1:
p,z,Sn,nx = [0,0,0,0]
else:
#data_test = data_test[-10:] # debug line to speed up
#try:
# p,z,Sn,nx = mk_test_timeout(data_test)
#except TimeoutError as e:
# print('timeout!')
# p,z,Sn,nx = [0,0,0,0]
p,z,Sn,nx = m.mk_trend(len(data_test), np.arange(len(data_test)), data_test)
else:
p,z,Sn,nx = m.mk_trend(len(data_test), np.arange(len(data_test)), data_test)
# if data_test = [], the test return (p,z,Sn,nx) = (1.0, 0.0, 0.5, 0.0)
else:
## other test
p,z,Sn,nx = [0,0,0,0]
z = data_test.mean()
if b_deb:
t4 = timer()-t0
if bla.size>0:
if bla.mean()==0.0:
tab_prof_zero.append(t4)
else:
tab_prof_valid.append(t4)
print('t4=', t4)
if 0:
import matplotlib.pyplot as plt
plt.clf()
plt.plot(bla)
plt.ylim(0,6.1)
ti1 = '{}/{} - {:.3f} s'.format(jj_sub, ii_sub, t4)
ti2 = 'min/mean/max/nb/nb_unique {:.3f} {:.3f} {:.3f} {} {}'.format(bla.min(), bla.mean(), bla.max(), len(bla), len(np.unique(bla)))
ti3 = 'slope: {}'.format(Sn)
plt.title(ti1+'\n'+ti2+'\n'+ti3)
if Sn==0.0:
plt.savefig('bla.Sn0.{}.{}.png'.format(jj_sub, ii_sub))
else:
plt.savefig('bla.{}.{}.png'.format(jj_sub, ii_sub))
t0 = timer()
if b_deb: print('p,z,slope,nx', p,z,slope,nx)
if b_deb: print('p,z,Sn,nx', p,z,Sn,nx)
var_temp_output[jj_sub,ii_sub,0] = p
var_temp_output[jj_sub,ii_sub,1] = z
var_temp_output[jj_sub,ii_sub,2] = Sn
var_temp_output[jj_sub,ii_sub,3] = nx
## Print efficiency stats
if (jj_sub+1)%print_freq==0:
elapsed = timer()-t00
data_stat = hdf_ts.variables[tsvar][:,jj_sub+1+offsety-print_freq:jj_sub+1+offsety,offsetx:offsetx+subchunk.dim[1]]
valid = 100.*(data_stat.size - np.count_nonzero(np.isnan(data_stat)))/data_stat.size
eff = 1e6*elapsed/data_stat.size
#print(subchunk.dim, data_test0.shape)
print('{} : {}.{}.block[{}-{}] : {:.3f}s elapsed : {:.3f} us/pix/date : {:.2f}% valid'.format(datetime.datetime.now(), parent_iteration, child_iteration, jj_sub+1-print_freq, jj_sub+1, elapsed, eff, valid))
t00 = timer()
sys.stdout.flush()
if 0:
t_mean += timer()-t00
#print(f't00 {ii_sub} {t_mean/(ii_sub+1)}')
#print(f't00.p{current._identity[0]}.it{ii_sub} {t_mean/(ii_sub+1):.3f}s {process.memory_info().rss/1024/1024:.2f}Mo')
print('t00.p{}.it{} {:.3f}s {:.2f}Mo'.format(current._identity[0], ii_sub, t_mean/(ii_sub+1), process.memory_info().rss/1024/1024))
v = var_temp_output[ii_sub,:,:]
for ii in range(4):
#print(f'{np.count_nonzero(np.isnan(v[:,ii]))/v[:,ii].size:.3f}', np.nanmin(v[:,ii]), np.nanmax(v[:,ii]))
print('{:.3f}'.format(np.count_nonzero(np.isnan(v[:,ii]))/v[:,ii].size), np.nanmin(v[:,ii]), np.nanmax(v[:,ii]))
print(np.nanmin(v), np.nanmax(v))
t00 = timer()
if b_deb:
#if 1:
valid = np.array(tab_prof_valid)
zero = np.array(tab_prof_zero)
#print('valid:', valid.size, valid.min(), valid.mean(), valid.max())
#print('zero:', zero.size, zero.min(), zero.mean(), zero.max())
print(valid.mean())
print(zero.mean())
#return
#sys.exit()
print('t000tot.p{} {:.3f}s {:.2f}Mo'.format(current._identity, timer()-t000, process.memory_info().rss/1024/1024))
hf.create_dataset(tsvar+'/pval', data=var_temp_output[:,:,0])
hf.create_dataset(tsvar+'/zval', data=var_temp_output[:,:,1])
hf.create_dataset(tsvar+'/slope', data=var_temp_output[:,:,2])
hf.create_dataset(tsvar+'/len', data=var_temp_output[:,:,3])
hf.close()
print ('Subchunk {} completed, save to {}'.format(child_iteration, subchunk_fname))
return None
def main():
# Calculate trend from the each time series of master chunks
nchild = 100
main_iteration = 0
for chunk in param.chunks.list:
## Check if it's a merged file or an original one
if param.product.endswith('_MERGED'):
list_of_paths = param.input_path.glob('*')
latest_path = max(list_of_paths, key=lambda p: p.stat().st_ctime)
param.input_file = latest_path
else:
param.input_file = param.input_path / (param.hash+'_timeseries_'+ '_'.join(chunk.get_limits('global','str'))+'.nc')
print('> calculating trend for the chunk:')
print(param.input_file.as_posix())
if chunk.input=='box':
print('***Row/y_SIZE***', chunk.dim[0], '***Col/x_SIZE***', chunk.dim[1])
# Subdivide each chunk into subchunks, the latter being then possibly multiprocessed
chunk.subdivide(nchild)
elif chunk.input=='points':
print('***', 'SIZE {} points'.format(chunk.dim[1]))
result_chunks = [(sub_chunk, main_iteration, sub_iteration) for sub_iteration,sub_chunk in enumerate(chunk.list)]
#### Use multiprocessing
if 0:
# Applying the multiple processing here, with process of choice, i use 4 for local and 16 for lustre
with Pool(processes=param.nproc) as pool:
print('pool started')
results = pool.starmap(processInput_trends, result_chunks)
pool.close()
pool.join()
print('pool cleaned')
#### or do it sequentially
else:
for i in result_chunks:
processInput_trends(*i)
# increase the main iteration
main_iteration += 1
def compute_trends(*args):
# Lock is the module from multiprocessing to allow the write of netcdf files one at a time to avoid conflict, first invocation here in main
global lock
lock = Lock()
class ArgsTmp:
def __init__(self, prod, chunks, np, b_delete, config):
self.hash = prod.hash
self.input = pathlib.Path(config['output_path']['extract'])
self.output = pathlib.Path(config['output_path']['trend'])
self.product = prod.name
self.nproc = np
self.chunks = chunks
self.b_delete = b_delete
self.input_path = self.input / prod.name
self.input_file = ''
self.output_path = self.output / prod.name
# Make dir if not exists
self.output_path.mkdir(parents=True, exist_ok=True)
# Erase output log file
fic = open('trend.out','w')
fic.close()
global param
param = ArgsTmp(*args)
main()
if __name__ == "__main__":
try:
# Lock is the module from multiprocessing to allow the write of netcdf files one at a time to avoid conflict, first invocation here in main
lock = Lock()
param = parse_args()
main()
except Exception:
traceback.print_exc()
sys.exit()