|
1
|
|
|
import pandas as pd |
|
2
|
|
|
import numpy as np |
|
3
|
|
|
import csv |
|
4
|
|
|
import matplotlib.pyplot as plt |
|
5
|
|
|
import warnings |
|
6
|
|
|
import matplotlib.cbook |
|
7
|
|
|
|
|
8
|
|
|
#split forward and backward sweping data, to make it easier for processing. |
|
9
|
|
|
def split(vector): |
|
10
|
|
|
""" |
|
11
|
|
|
This function takes an array and splits it into two half. |
|
12
|
|
|
""" |
|
13
|
|
|
split = int(len(vector)/2) |
|
14
|
|
|
end = int(len(vector)) |
|
15
|
|
|
vector1 = np.array(vector)[0:split] |
|
16
|
|
|
vector2 = np.array(vector)[split:end] |
|
17
|
|
|
return vector1, vector2 |
|
18
|
|
|
|
|
19
|
|
|
|
|
20
|
|
View Code Duplication |
def critical_idx(x, y): ## Finds index where data set is no longer linear |
|
|
|
|
|
|
21
|
|
|
""" |
|
22
|
|
|
This function takes x and y values callculate the derrivative of x and y, and calculate moving average of 5 and 15 points. |
|
23
|
|
|
Finds intercepts of different moving average curves and return the indexs of the first intercepts. |
|
24
|
|
|
""" |
|
25
|
|
|
k = np.diff(y)/(np.diff(x)) #calculated slops of x and y |
|
26
|
|
|
## Calculate moving average for 5 and 15 points. |
|
27
|
|
|
## This two arbitrary number can be tuned to get better fitting. |
|
28
|
|
|
ave10 = [] |
|
29
|
|
|
ave15 = [] |
|
30
|
|
|
for i in range(len(k)-10): # The reason to minus 5 is to prevent j from running out of index. |
|
31
|
|
|
a = 0 |
|
32
|
|
|
for j in range(0,5): |
|
33
|
|
|
a = a + k[i+j] |
|
34
|
|
|
ave10.append(round(a/5, 5)) # keeping 9 desimal points for more accuracy |
|
35
|
|
|
|
|
36
|
|
|
for i in range(len(k)-15): |
|
37
|
|
|
b = 0 |
|
38
|
|
|
for j in range(0,10): |
|
39
|
|
|
b = b + k[i+j] |
|
40
|
|
|
ave15.append(round(b/10, 5)) |
|
41
|
|
|
ave10i = np.asarray(ave5) |
|
|
|
|
|
|
42
|
|
|
print(ave10i) |
|
43
|
|
|
ave15i = np.asarray(ave15) |
|
44
|
|
|
print(ave15i) |
|
45
|
|
|
## Find intercepts of different moving average curves |
|
46
|
|
|
idx = np.argwhere(np.diff(np.sign(ave15i - ave10i[:len(ave15i)])!= 0)).reshape(-1)+0 #reshape into one row. |
|
47
|
|
|
return idx[1] |
|
48
|
|
|
|
|
49
|
|
|
# This is based on the method 1 where user can't choose the baseline. |
|
50
|
|
|
# If wanted to add that, choose method2. |
|
51
|
|
|
def sum_mean(vector): |
|
52
|
|
|
""" |
|
53
|
|
|
This function returns the mean values. |
|
54
|
|
|
""" |
|
55
|
|
|
a = 0 |
|
56
|
|
|
for i in vector: |
|
57
|
|
|
a = a + i |
|
58
|
|
|
return [a,a/len(vector)] |
|
59
|
|
|
|
|
60
|
|
|
|
|
61
|
|
|
def multiplica(vetor_x, vetor_y): |
|
62
|
|
|
a = 0 |
|
63
|
|
|
for x,y in zip(vetor_x, vetor_y): |
|
64
|
|
|
a = a + (x * y) |
|
65
|
|
|
return a |
|
66
|
|
|
|
|
67
|
|
|
|
|
68
|
|
|
def linear_coeff(x, y): |
|
69
|
|
|
""" |
|
70
|
|
|
This function returns the inclination coeffecient and y axis interception coeffecient m and b. |
|
71
|
|
|
""" |
|
72
|
|
|
m = (multiplica(x,y) - sum_mean(x)[0] * sum_mean(y)[1]) / (multiplica(x,x) - sum_mean(x)[0] * sum_mean(x)[1]) |
|
73
|
|
|
b = sum_mean(y)[1] - m * sum_mean(x)[1] |
|
74
|
|
|
return m, b |
|
75
|
|
|
|
|
76
|
|
|
|
|
77
|
|
|
def y_fitted_line(m, b, x): |
|
78
|
|
|
y_base = [] |
|
79
|
|
|
for i in x: |
|
80
|
|
|
y = m * i + b |
|
81
|
|
|
y_base.append(y) |
|
82
|
|
|
return y_base |
|
83
|
|
|
|
|
84
|
|
|
|
|
85
|
|
|
def linear_background(x, y): |
|
86
|
|
|
idx = critical_idx(x, y) + 5 #this is also arbitrary number we can play with. |
|
87
|
|
|
m, b = linear_coeff(x[(idx - int(0.5 * idx)) : (idx + int(0.5 * idx))], y[(idx - int(0.5 * idx)) : (idx + int(0.5 * idx))]) |
|
88
|
|
|
y_base = y_fitted_line(m, b, x) |
|
89
|
|
|
return y_base |
|
90
|
|
|
|