-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsetup.py
468 lines (419 loc) · 15.1 KB
/
setup.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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
#!/usr/bin/env python
# ~*~ coding: utf8 ~*~
"""Write a file containing cost functions.
Also derivatives, ideally.
"""
from __future__ import division, print_function
import itertools
import numpy as np
from Cython.Build import cythonize
from setuptools import Extension, setup
from correlation_function_fits import (
CorrelationPart,
PartForm,
get_full_expression,
get_full_parameter_list,
get_weighted_fit_expression,
is_valid_combination,
)
OUT_FILE_NAME = "flux_correlation_function_fits.pyx"
with open(OUT_FILE_NAME, "w") as out_file:
out_file.write(
"""# cython: embedsignature=True
# cython: language_level=3str
# cython: cdivision=True
# cython: wraparound=False
# cython: boundscheck=False
# cython: gdb_debug=False
from libc cimport math
from cython.view cimport array as cvarray
import numpy as np
cimport numpy as np
import numexpr as ne
# from numpy cimport PyArray_Where as where
ctypedef fused floating_type:
np.float32_t
np.float64_t
cdef extern from "<math.h>" nogil:
float sinf(float x)
float cosf(float x)
float expf(float x)
cdef inline floating_type cycos(floating_type x) nogil:
if floating_type is float:
return cosf(x)
elif floating_type is double:
return math.cos(x)
cdef inline floating_type cysin(floating_type x) nogil:
if floating_type is float:
return sinf(x)
elif floating_type is double:
return math.sin(x)
cdef inline floating_type cyexp(floating_type x) nogil:
if floating_type is float:
return expf(x)
elif floating_type is double:
return math.exp(x)
cdef inline floating_type where(bint cond, floating_type a, floating_type b) nogil:
if cond:
return a
return b
ctypedef floating_type (*float_fun)(floating_type) nogil
cdef float HOURS_PER_DAY = 24.
cdef float DAYS_PER_DAY = 1.
cdef float DAYS_PER_WEEK = 7.
cdef float DAYS_PER_FORTNIGHT = 14.
cdef float DAYS_PER_YEAR = 365.2425
cdef float DAYS_PER_DECADE = 10 * DAYS_PER_YEAR
cdef float HOURS_PER_YEAR = HOURS_PER_DAY * DAYS_PER_YEAR
cdef float PI_OVER_DAY = np.pi / DAYS_PER_DAY
cdef float TWO_PI_OVER_DAY = 2 * PI_OVER_DAY
cdef float FOUR_PI_OVER_DAY = 2 * TWO_PI_OVER_DAY
cdef float PI_OVER_YEAR = np.pi / DAYS_PER_YEAR
cdef float TWO_PI_OVER_YEAR = 2 * PI_OVER_YEAR
cdef float FOUR_PI_OVER_YEAR = 2 * TWO_PI_OVER_YEAR
cdef dict GLOBAL_DICT = {
"HOURS_PER_DAY": HOURS_PER_DAY,
"DAYS_PER_DAY": DAYS_PER_DAY,
"DAYS_PER_WEEK": DAYS_PER_WEEK,
"DAYS_PER_FORTNIGHT": DAYS_PER_FORTNIGHT,
"DAYS_PER_YEAR": DAYS_PER_YEAR,
"DAYS_PER_DECADE": DAYS_PER_DECADE,
"PI_OVER_DAY": PI_OVER_DAY,
"TWO_PI_OVER_DAY": TWO_PI_OVER_DAY,
"FOUR_PI_OVER_DAY": FOUR_PI_OVER_DAY,
"PI_OVER_YEAR": PI_OVER_YEAR,
"TWO_PI_OVER_YEAR": TWO_PI_OVER_YEAR,
"FOUR_PI_OVER_YEAR": FOUR_PI_OVER_YEAR,
}
"""
)
for forms in itertools.product(PartForm, PartForm, PartForm):
if not is_valid_combination(*forms):
continue
out_file.write(
"""
def {func_name:s}_fit_ne(parameters, tdata, empirical_correlogram, pair_count):
return ne.evaluate(
"{weighted_sum_expr:s}",
local_dict={{
{param_names_from_parameters:s}
"tdata": tdata,
"empirical_correlogram": empirical_correlogram,
"num_pairs": pair_count,
}},
global_dict=GLOBAL_DICT,
)
""".format(
func_name="_".join(
[
"{0:s}{1:s}".format(
part.get_short_name(),
form.get_short_name(),
)
for part, form in zip(CorrelationPart, forms)
]
),
weighted_sum_expr=get_weighted_fit_expression(*forms),
param_names_from_parameters="".join(
[
' "{param_name:s}": parameters[{i:d}],\n'.format(
i=i, param_name=param_name
)
for i, param_name in enumerate(get_full_parameter_list(*forms))
]
),
)
)
out_file.write(
"""
def {func_name:s}_curve_ne(
tdata,
{parameters:s}
):
return ne.evaluate(
"{full_expr:s}",
local_dict={{
"tdata": tdata,
{param_names_from_parameters:s}
}},
global_dict=GLOBAL_DICT,
)
""".format(
func_name="_".join(
[
"{0:s}{1:s}".format(
part.get_short_name(),
form.get_short_name(),
)
for part, form in zip(CorrelationPart, forms)
]
),
param_names_from_parameters="".join(
[
' "{param_name:s}": {param_name:s},\n'.format(
param_name=param_name
)
for param_name in get_full_parameter_list(*forms)
]
),
parameters="".join(
[
" {param:s},\n".format(param=param_name)
for param_name in get_full_parameter_list(*forms)
]
),
full_expr=get_full_expression(*forms),
)
)
out_file.write(
"""
def {function_name:s}_fit_loop(
np.float64_t[::1] parameters not None,
floating_type[::1] tdata_base not None,
floating_type[::1] empirical_correlogram not None,
floating_type[::1] pair_count not None,
):
cdef floating_type weighted_fit = 0.0
cdef floating_type deriv[{n_parameters:d}]
cdef floating_type here_deriv[{n_parameters:d}]
cdef long int n_parameters = {n_parameters:d}
{params_from_parameters:s}
cdef long int i = 0, j = 0
cdef floating_type tdata
cdef floating_type daily_corr, dm_corr
cdef floating_type ann_corr
cdef floating_type resid_corr, ec_corr
cdef floating_type here_corr
cdef floating_type deriv_common
cdef float_fun exp = cyexp
cdef float_fun cos = cycos
cdef float_fun sin = cysin
resid_timescale *= DAYS_PER_FORTNIGHT
for j in range(n_parameters):
deriv[j] = 0.0
for i in range(len(tdata_base)):
tdata = tdata_base[i]
here_corr = 0.0
daily_corr = {daily_form:s}
dm_corr = {daily_modulation_form:s}
here_corr += daily_corr * dm_corr
{accum_day_deriv:s}
{accum_dm_deriv:s}
ann_corr = {annual_form:s}
here_corr += ann_corr
{accum_ann_deriv:s}
if resid_timescale > 0:
resid_corr = resid_coef * exp(-tdata / resid_timescale)
here_corr += resid_corr
here_deriv[n_parameters - 3] = exp(-tdata / resid_timescale)
here_deriv[n_parameters - 2] = resid_corr * tdata / resid_timescale ** 2
if tdata == 0:
ec_corr = ec_coef
here_corr += ec_corr
here_deriv[n_parameters - 1] = 1
weighted_fit += pair_count[i] * (here_corr - empirical_correlogram[i]) ** 2
deriv_common = pair_count[i] * 2 * (here_corr - empirical_correlogram[i])
for j in range(n_parameters):
deriv[j] += deriv_common * here_deriv[j]
deriv[n_parameters - 2] *= DAYS_PER_FORTNIGHT
return weighted_fit, np.asarray(
<floating_type[:n_parameters]>deriv
).astype(np.float64)
""".format(
function_name="_".join(
[
"{0:s}{1:s}".format(
part.get_short_name(),
form.get_short_name(),
)
for part, form in zip(CorrelationPart, forms)
]
),
n_parameters=len(get_full_parameter_list(*forms)),
params_from_parameters="".join(
[
(
" cdef floating_type {param_name:s}"
" = parameters[{i:d}]\n"
).format(i=i, param_name=param_name)
for i, param_name in enumerate(get_full_parameter_list(*forms))
]
),
daily_form=forms[0].get_expression(CorrelationPart.DAILY),
daily_modulation_form=forms[1].get_expression(
CorrelationPart.DAILY_MODULATION
),
annual_form=forms[2].get_expression(CorrelationPart.ANNUAL),
accum_day_deriv="\n ".join(
"here_deriv[{i:d}] = {deriv_piece:s} * dm_corr".format(
i=i, deriv_piece=deriv_piece
)
for i, deriv_piece in enumerate(
forms[0].get_derivative(CorrelationPart.DAILY)
)
),
accum_dm_deriv="\n ".join(
"here_deriv[{i:d}] = daily_corr * {deriv_piece:s}".format(
i=i, deriv_piece=deriv_piece
)
for i, deriv_piece in enumerate(
forms[1].get_derivative(CorrelationPart.DAILY_MODULATION),
len(forms[0].get_parameters(CorrelationPart.DAILY)),
)
),
accum_ann_deriv="\n ".join(
"here_deriv[{i:d}] = {deriv_piece:s}".format(
i=i, deriv_piece=deriv_piece
)
for i, deriv_piece in enumerate(
forms[2].get_derivative(CorrelationPart.ANNUAL),
len(forms[0].get_parameters(CorrelationPart.DAILY))
+ len(
forms[1].get_parameters(CorrelationPart.DAILY_MODULATION)
),
)
),
)
)
out_file.write(
"""
def {function_name:s}_curve_loop(
floating_type[::1] tdata_base not None,
{parameters:s}
):
cdef floating_type weighted_fit = 0.0
cdef long int n_times = len(tdata_base)
if floating_type == np.float32_t:
typecode = "f"
elif floating_type == np.float64_t:
typecode = "d"
cdef floating_type[::1] curve = cvarray(
shape=(n_times,),
itemsize=sizeof(floating_type),
format=typecode,
)
cdef floating_type[:, ::1] deriv = cvarray(
shape=(n_times, {n_parameters:d}),
itemsize=sizeof(floating_type),
format=typecode,
)
cdef long int n_parameters = {n_parameters:d}
cdef long int i = 0, j = 0
cdef floating_type tdata
cdef floating_type daily_corr, dm_corr
cdef floating_type ann_corr
cdef floating_type resid_corr, ec_corr
cdef floating_type here_corr
cdef floating_type deriv_common
cdef float_fun exp = cyexp
cdef float_fun cos = cycos
cdef float_fun sin = cysin
resid_timescale *= DAYS_PER_FORTNIGHT
for i in range(n_times):
tdata = tdata_base[i]
here_corr = 0.0
daily_corr = {daily_form:s}
dm_corr = {daily_modulation_form:s}
here_corr += daily_corr * dm_corr
{accum_day_deriv:s}
{accum_dm_deriv:s}
ann_corr = {annual_form:s}
here_corr += ann_corr
{accum_ann_deriv:s}
if resid_timescale > 0:
resid_corr = resid_coef * exp(-tdata / resid_timescale)
here_corr += resid_corr
deriv[i, n_parameters - 3] = exp(-tdata / resid_timescale)
deriv[i, n_parameters - 2] = (
resid_corr * tdata / resid_timescale ** 2 * DAYS_PER_FORTNIGHT
)
if tdata == 0:
ec_corr = ec_coef
here_corr += ec_corr
deriv[i, n_parameters - 1] = 1
curve[i] = here_corr
return (
np.asarray(curve),
np.asarray(deriv),
)
""".format(
function_name="_".join(
[
"{0:s}{1:s}".format(
part.get_short_name(),
form.get_short_name(),
)
for part, form in zip(CorrelationPart, forms)
]
),
n_parameters=len(get_full_parameter_list(*forms)),
parameters="".join(
[
" floating_type {param_name:s},\n".format(
param_name=param_name
)
for i, param_name in enumerate(get_full_parameter_list(*forms))
]
),
daily_form=forms[0].get_expression(CorrelationPart.DAILY),
daily_modulation_form=forms[1].get_expression(
CorrelationPart.DAILY_MODULATION
),
annual_form=forms[2].get_expression(CorrelationPart.ANNUAL),
accum_day_deriv="\n ".join(
"deriv[i, {j:d}] = {deriv_piece:s} * dm_corr".format(
j=j, deriv_piece=deriv_piece
)
for j, deriv_piece in enumerate(
forms[0].get_derivative(CorrelationPart.DAILY)
)
),
accum_dm_deriv="\n ".join(
"deriv[i, {j:d}] = daily_corr * {deriv_piece:s}".format(
j=j, deriv_piece=deriv_piece
)
for j, deriv_piece in enumerate(
forms[1].get_derivative(CorrelationPart.DAILY_MODULATION),
len(forms[0].get_parameters(CorrelationPart.DAILY)),
)
),
accum_ann_deriv="\n ".join(
"deriv[i, {j:d}] = {deriv_piece:s}".format(
j=j, deriv_piece=deriv_piece
)
for j, deriv_piece in enumerate(
forms[2].get_derivative(CorrelationPart.ANNUAL),
len(forms[0].get_parameters(CorrelationPart.DAILY))
+ len(
forms[1].get_parameters(CorrelationPart.DAILY_MODULATION)
),
)
),
)
)
############################################################
# Now build the module
setup(
name="co2_flux_correlation_analysis",
author="DWesl",
version="0.0.0.rc2",
py_modules=["correlation_function_fits", "correlation_utils"],
ext_modules=cythonize(
[
Extension(
OUT_FILE_NAME.replace(".pyx", ""),
[OUT_FILE_NAME],
include_dirs=[np.get_include()],
),
],
include_path=[np.get_include()],
compiler_directives=dict(
embedsignature=True,
cdivision=True,
wraparound=True,
boundscheck=True,
),
annotate=True,
gdb_debug=True,
),
)