-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.py
88 lines (73 loc) · 3.14 KB
/
utils.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
import sys
import getopt
import csv
import numpy as np
import glob
import re
from math import sqrt, log
import scipy.interpolate as interpolate
import matplotlib.pyplot as plt
from scipy import signal
def smooth_sig(xn, order, cutoff):
b, a = signal.butter(order, cutoff)
zi = signal.lfilter_zi(b, a)
z, _ = signal.lfilter(b, a, xn, zi=zi*xn[0])
z2, _ = signal.lfilter(b, a, z, zi=zi*z[0])
y = signal.filtfilt(b, a, xn)
return y
class Graph:
def __init__(self, file_list, smooth=False, order=3, cutoff=0.01):
self.n = 0
self.tail_percent = 0.05
self.t = []
self.x = []
self.y = []
self.z = []
self.ke = []
self.drift = []
self.ionized = []
for file in file_list:
_t, _x, _y, _z, _ke, _drift, _ionized = np.loadtxt(file, delimiter=',', unpack=True, ndmin=2)
if smooth is True:
self.t.append(smooth_sig(_t, order, cutoff))
self.x.append(smooth_sig(_x, order, cutoff))
self.y.append(smooth_sig(_y, order, cutoff))
self.z.append(smooth_sig(_z, order, cutoff))
self.ke.append(smooth_sig(_ke, order, cutoff))
self.drift.append(smooth_sig(_drift, order, cutoff))
self.ionized.append(smooth_sig(_ionized, order, cutoff))
else:
self.t.append(_t)
self.x.append(_x)
self.y.append(_y)
self.z.append(_z)
self.ke.append(_ke)
self.drift.append(_drift)
self.ionized.append(_ionized)
self.n += 1
def M(self, recursive=False):
avg_ending_dist = sum([(self.x[i][-1]**2 + self.y[i][-1]**2 + self.z[i][-1]**2)**0.5 for i in range(self.n)]) / self.n
if (recursive):
mult_factor = sum([self.ionized[k][-1] + 1 for k in range(self.n)]) / self.n
return mult_factor
d = 10
est_ending_elecs_log = np.log(2) * np.array([k[-1] for k in self.ionized])
mu = np.mean(est_ending_elecs_log)
variance = np.var(est_ending_elecs_log)
mult_factor = np.exp((mu + 0.5 * variance) * (d / avg_ending_dist))
return mult_factor
def alpha(self, recursive=False):
avg_ending_dist = sum([(self.x[i][-1]**2 + self.y[i][-1]**2 + self.z[i][-1]**2)**0.5 for i in range(self.n)]) / self.n
if (recursive):
return np.log(self.M(recursive)) / avg_ending_distance
est_ending_elecs_log = np.log(2) * np.array([k[-1] for k in self.ionized])
mu = np.mean(est_ending_elecs_log)
variance = np.var(est_ending_elecs_log)
return (mu + 0.5 * variance) / (avg_ending_dist * 1e-4)
def drift_mean(self):
mean = sum([sum(k[-int(len(k) * self.tail_percent):]) / int(len(k) * self.tail_percent) for k in self.drift]) / self.n
return mean
def drift_std_dev(self):
mean = self.drift_mean()
mean2 = sum([sum([i**2 for i in k[-int(len(k) * self.tail_percent):]]) / int(len(k) * self.tail_percent) for k in self.drift]) / self.n
return sqrt((self.n / (self.n - 1)) * mean2 - mean**2)