forked from astronomerdamo/pydcf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dcf.py
387 lines (287 loc) · 10.6 KB
/
dcf.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
'''
A simple implementation of the discrete correlation function (DCF)
Author: Damien Robertson - [email protected]
Usage:
$ python dcf.py -h for help and basic instruction
'''
from __future__ import print_function, division
print(__doc__)
try:
import numpy as np
except ImportError:
print("Numpy not installed, try - pip install numpy")
import sys
sys.exit()
#
# Subroutines
#
def tsdtrnd(ts, vrbs, plyft):
'''
Subroutine - tsdtrnd
Time series detrend using the user chosen polynomial order. Subroutine
fits a ploynomial to the time series data and subtracts.
Requires scipy.optimize (scipy) to be installed.
'''
if plyft == 0:
ts_mean = np.mean(ts[:,1])
ts[:,1] = ts[:,1] - ts_mean
if vrbs:
print("Mean subtraction: %.4e" %ts_mean)
elif plyft == 1:
try:
from scipy.optimize import curve_fit
except ImportError:
print("Scipy not installed, try - pip install scipy")
import sys
sys.exit()
lnfnc = lambda x, a, b: a*x + b
p0, c0 = curve_fit(lnfnc, ts[:,0], ts[:,1], sigma=ts[:,2])
ts[:,1] = ts[:,1] - lnfnc(ts[:,0], p0[0], p0[1])
if vrbs:
print("Linear De-trend Coefficients [a*x + b]")
print("a:", p0[0])
print("b:", p0[1])
else:
try:
from scipy.optimize import curve_fit
except ImportError:
print("Scipy not installed, try - pip install scipy")
import sys
sys.exit()
lnfnc = lambda x, a, b, c: a*x**2.0 + b*x + c
p0, c0 = curve_fit(lnfnc, ts[:,0], ts[:,1], sigma=ts[:,2])
ts[:,1] = ts[:,1] - lnfnc(ts[:,0], p0[0], p0[1], p0[2])
if vrbs:
print("Quadratic De-trend Coefficients [a*x**2 + b*x + c]")
print("a:", p0[0])
print("b:", p0[1])
print("c:", p0[2])
return ts
def set_unitytime(ts1, ts2):
'''
Subroutine - set_unitytime
Simply shifts both time series so that one starts at zero.
'''
unitytime = min(np.min(ts1[:,0]), np.min(ts2[:,0]))
ts1[:,0] = ts1[:,0] - unitytime
ts2[:,0] = ts2[:,0] - unitytime
return ts1, ts2
def chck_tserr(ts):
'''
Subroutine - chck_tserr
Makes sure user has entered a properly formatted ts file.
Checks to see if input time series has a measurement error column - third
column of input file.
'''
assert ((ts.shape[1] == 2) or (ts.shape[1] == 3)), "TS SHAPE ERROR"
if ts.shape[1] == 2:
ts_fill = np.zeros((ts.shape[0], 3))
ts_fill[:,0:2] = ts[:,0:2]
return ts_fill
else:
return ts
def get_timeseries(infile1, infile2, vrbs, plyft):
'''
Subroutine - get_timeseries
Takes the user specified filenames and runs tsdtrnd and set_unitytime.
Returns the prepared time series for DCF.
'''
ts1_in = np.loadtxt(infile1, comments='#', delimiter=',')
ts2_in = np.loadtxt(infile2, comments='#', delimiter=',')
ts1 = chck_tserr(ts1_in)
ts2 = chck_tserr(ts2_in)
ts1, ts2 = set_unitytime(ts1, ts2)
ts1 = tsdtrnd(ts1, vrbs, plyft)
ts2 = tsdtrnd(ts2, vrbs, plyft)
return ts1, ts2
def sdcf(ts1, ts2, t, dt):
'''
Subroutine - sdcf
DCF algorithm with slot weighting
'''
dcf = np.zeros(t.shape[0])
dcferr = np.zeros(t.shape[0])
n = np.zeros(t.shape[0])
dst = np.empty((ts1.shape[0], ts2.shape[0]))
for i in range(ts1.shape[0]):
for j in range(ts2.shape[0]):
dst[i,j] = ts2[j,0] - ts1[i,0]
for k in range(t.shape[0]):
tlo = t[k] - dt/2.0
thi = t[k] + dt/2.0
ts1idx, ts2idx = np.where((dst < thi) & (dst > tlo))
mts2 = np.mean(ts2[ts2idx,1])
mts1 = np.mean(ts1[ts1idx,1])
n[k] = ts1idx.shape[0]
dcfdnm = np.sqrt((np.var(ts1[ts1idx,1]) - np.mean(ts1[ts1idx,2])**2) \
* (np.var(ts2[ts2idx,1]) - np.mean(ts2[ts2idx,2])**2))
dcfs = (ts2[ts2idx,1] - mts2) * (ts1[ts1idx,1] - mts1) / dcfdnm
dcf[k] = np.sum(dcfs) / float(n[k])
dcferr[k] = np.sqrt(np.sum((dcfs - dcf[k])**2)) / float(n[k] - 1)
return dcf, dcferr
def gdcf(ts1, ts2, t, dt):
'''
Subroutine - gdcf
DCF algorithm with gaussian weighting
'''
h = dt/4.0
gkrn = lambda x: np.exp(-1.0 * np.abs(x)**2 / (2.0 * h**2)) \
/ np.sqrt(2.0 * np.pi * h)
cntrbt = gkrn(3.290527*h)
dcf = np.zeros(t.shape[0])
dcferr = np.zeros(t.shape[0])
n = np.zeros(t.shape[0])
dst = np.empty((ts1.shape[0], ts2.shape[0]))
for i in range(ts1.shape[0]):
for j in range(ts2.shape[0]):
dst[i,j] = ts2[j,0] - ts1[i,0]
for k in range(t.shape[0]):
gdst = gkrn(dst - t[k])
ts1idx, ts2idx = np.where(gdst >= cntrbt)
mts2 = np.mean(ts2[ts2idx,1])
mts1 = np.mean(ts1[ts1idx,1])
n[k] = ts1idx.shape[0]
dcfdnm = np.sqrt((np.var(ts1[ts1idx,1]) - np.mean(ts1[ts1idx,2])**2) \
* (np.var(ts2[ts2idx,1]) - np.mean(ts2[ts2idx,2])**2))
dcfs = (ts2[ts2idx,1] - mts2) * (ts1[ts1idx,1] - mts1) / dcfdnm
dcf[k] = np.sum(dcfs) / float(n[k])
dcferr[k] = np.sqrt(np.sum((dcfs - dcf[k])**2)) / float(n[k] - 1)
return dcf, dcferr
#
# MAIN PROGRAM
#
import argparse
INPUT = argparse.ArgumentParser(description='DCF USER PARAMETERS')
#
# USER PARAMETER INPUT
# STANDARD PARAMETERS (REQUIRED):
# time_series1.dat - path/filename
# time_series2.dat - path/filename
# lag_range_low - float
# lag_range_high - float
# lag_bin_width - float
#
INPUT.add_argument('infile1', metavar='time_series1', type=open, nargs=1,
help='Time Series 1')
INPUT.add_argument('infile2', metavar='time_series2', type=open, nargs=1,
help='Time Series 2')
INPUT.add_argument('lgl', metavar='lag_range_low', type=float, nargs=1,
help='Lag range low')
INPUT.add_argument('lgh', metavar='lag_range_high', type=float, nargs=1,
help='Lag range high')
INPUT.add_argument('dt', metavar='lag_bin_width', type=float, nargs=1,
help='Width of lag bin, dt')
#
# USER PARAMETER INPUT
# OPTIONAL PARAMETERS:
# weight = 'slot' or 'gauss'
# polyfit = 0, 1, 2
# plot = True or False
# verbose = True or False
# output = True of False
#
INPUT.add_argument('-w', dest='weight', type=str, nargs=1,
default=['slot'], choices=['slot', 'gauss'],
required=False, help='Lag bin weighting scheme')
INPUT.add_argument('-p', dest='polyfit', type=int, nargs=1,
default=[0], choices=[0, 1, 2],
required=False, help='Polynomial fit subtraction')
INPUT.add_argument('-np', '--no-plot', dest='noplot', action='store_false',
help='Do not produce plot')
INPUT.add_argument('-o', '--output', dest='output', action='store_true',
help='Write output file')
INPUT.add_argument('-v', '--verbose', dest='verbose', action='store_true',
help='Show all steps')
OPTS = INPUT.parse_args()
#
# USER PARAMETER CHECK AND READOUT
# This section will fail if:
# Parameters 'lag_range_low' and 'lag_range_high' are not symmetric
# about zero, ie: |lag_range_low| == |lag_range_high|
# Parameter 'lag_range_low' is greater than 'lag_range_high'.
#
# **PITFALL**
# There is no check to make sure the user enters a sensible
# lag bin width. See README for more details.
#
assert OPTS.lgl[0] < OPTS.lgh[0], "INPUT ERROR - LAG RANGE"
if OPTS.verbose:
print("\nPYTHON SCRIPT: dcf3")
print()
print("INPUT TIMESERIES 1:", OPTS.infile1[0].name)
print("INPUT TIMESERIES 2:", OPTS.infile2[0].name)
print("LAG RANGE PROBED :", OPTS.lgl[0], " : ", OPTS.lgh[0])
print("LAG BIN WIDTH :", OPTS.dt[0])
#
# TIME SERIES PREPARATION
# This section subtracts a n'th order polynomial from both input time
# series prior to the DCF. The user may choose:
# 0'th order polynomial - subtracting the mean or zeroing the data.
# 1'st order polynomial - subtracting a linear fit
# 2'nd order polynomial - subtracting a quadratic fit
# The default setting is subtracting a 0'th order polynomial (the mean).
# This simply zeros the data and doesn't change any intrinsic qualities.
#
# **PITFALL**
# Just because you can subtract a n'th order polynomial doesn't mean you
# should. The program doesn't monitor or tell you a subtraction is
# harmful or unnecessary.
#
# **PITFALL 2**
# If you have subtracted your own fits from the time series, leave the
# default setting, 0, as is. It won't change your data.
#
if OPTS.verbose:
print("\nTime series preparation")
TS1, TS2 = get_timeseries(OPTS.infile1[0], OPTS.infile2[0], OPTS.verbose, \
OPTS.polyfit[0])
#
# DCF
# This section earns the paycheck for the entire program - runs the DCF
# algorithm. The user main choose the rectangular 'slot' weighting or
# the gaussian 'gauss' weighting. See README for details on pair weighting.
#
# The regular weighting scheme is 'slot' and also default. The 'gauss'
# weighting assigns higher importance to data found at the centre of
# the lag bin.
#
DT = OPTS.dt[0]
N = np.around((OPTS.lgh[0] - OPTS.lgl[0]) / float(DT))
T = np.linspace(OPTS.lgl[0]+(DT/2.0), OPTS.lgh[0]-(DT/2.0), N)
if OPTS.weight[0] == 'slot':
if OPTS.verbose:
print("\nDCF INITIATED USING SLOT WEIGHTING")
DCF, DCFERR = sdcf(TS1, TS2, T, DT)
else:
if OPTS.verbose:
print("\nDCF INITIATED USING GAUSSIAN WEIGHTING")
DCF, DCFERR = gdcf(TS1, TS2, T, DT)
if OPTS.verbose:
print("DCF COMPLETE")
#
# CHECK IF OUTPUT WRITE IS TRUE
#
if OPTS.output:
print("Writing DCF output file to: dcf_output.csv")
np.savetxt('dcf_output.csv', np.transpose((T, DCF, DCFERR)), fmt="%.6f", \
header="LAG,DCF,DCF_ERROR", delimiter=',')
#
# PLOT RESULTS
# If the user wishes to suppress the plot
# one should use the -np or --no-plot flag on the command line.
#
# Requires python module matplotlib.
#
if OPTS.noplot:
try:
import matplotlib.pyplot as plt
except ImportError:
print("Matplotlib not installed, try - pip install matplotlib")
import sys
sys.exit()
plt.figure(0)
plt.errorbar(T, DCF, DCFERR, color='k', ls='-', capsize=0)
plt.xlabel("Lag")
plt.ylabel("Correlation Coefficient")
plt.xlim(OPTS.lgl[0], OPTS.lgh[0])
plt.show()