-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalcFDiff.py
48 lines (37 loc) · 1.06 KB
/
calcFDiff.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
import numpy as np
from genTools import dataManip.aveUnqTime as aveUnqTime
def calcForDiff(time,X):
"""Calulates the finite differance
derivative of variables wrt time
using forward differenc (last point uses
backward difference). If multiple points
for each time point then the fd is calculated
using the diffence to the average value at the
nest time point.
time vector of time points
X n obs by m vars data matrix
"""
# lets do the average first
timeUnq,XUnq = aveUnqTime(time,X)
# lets no calculate the fd for each time point
n = len(time)
dXdt = np.zeros(X.shape)
for i in range(len(time)):
t = time[i]
ind = np.argmin(np.abs(t-timeUnq))
if ind < len(timeUnq)-1:
tDel = timeUnq[ind+1] - t
xDel = XUnq[ind+1] - X[i]
else:
tDel = t - timeUnq[ind-1]
xDel = X[i] - XUnq[ind-1]
dXdt[i] = xDel/tDel
return dXdt
def do4Grp(time,grp,X):
dXdt = np.zeros(X.shape)
unqGrp = np.array(list(set(grp)))
for i in range(len(unqGrp)):
g = unqGrp[i]
ind = grp==g
dXdt[ind] = calcForDiff(time[ind],X[ind])
return dXdt