forked from THE-Port/ThePortHackathon-Pier15
-
Notifications
You must be signed in to change notification settings - Fork 0
/
baseline.py
135 lines (99 loc) · 2.92 KB
/
baseline.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
# from array import array
#FINDS BOTH BASELINE AND MEAN LINE
import numpy
from numpy import NaN, Inf, arange, isscalar, asarray, array, mean
import peakutils
from peakutils.plot import plot as pplot
from matplotlib import pyplot
# def calc_baseline(x,y):
# zeroed_y=[]
# for n in range(len(y)):
# line_y=array(y[n][0:1]+y[n][-41:-1])
# line_x=array(x[n][0:1]+x[n][-41:-1])
# p=scipy.polyfit(baseline_x,baseline_y,1)
# baseline_y=array(x[n])*p[0]+p[1]
# zeroed_y.append(baseline_y)
# return zeroed_y
#f= open('input.txt', 'r')
#f = open('export_elpho_drug_ISZ.txt','r')
f = open('export_elpho_drug_ISZ+RIF.txt','r')
pairs = f.readlines()
x_axis = []
y_axis = []
for pair in pairs:
values = pair.split()
x_axis.append(float(values[0]))
y_axis.append(float(values[1]))
two = numpy.column_stack((x_axis,y_axis))
variance = numpy.var(asarray(two),)
# calc_baseline(x_axis, y_axis)
# print zeroed_y
def peakdet(v, delta, x = None):
maxtab = []
mintab = []
if x is None:
x = arange(len(v))
v = asarray(v)
if len(v) != len(x):
sys.exit('Input vectors v and x must have same length')
if not isscalar(delta):
sys.exit('Input argument delta must be a scalar')
if delta <= 0:
sys.exit('Input argument delta must be positive')
mn, mx = Inf, -Inf
mnpos, mxpos = NaN, NaN
lookformax = True
for i in arange(len(v)):
this = v[i]
if this > mx:
mx = this
mxpos = x[i]
if this < mn:
mn = this
mnpos = x[i]
if lookformax:
if this < mx-delta:
maxtab.append((mxpos, mx))
mn = this
mnpos = x[i]
lookformax = False
else:
if this > mn+delta:
mintab.append((mnpos, mn))
mx = this
mxpos = x[i]
lookformax = True
return array(maxtab), array(mintab)
def findMean(ys):
basel = mean(ys)
return basel
series = y_axis
maxtab, mintab = peakdet(series,0.1)
basel = findMean(series)
x_axis = asarray(x_axis)
y_axis = asarray(y_axis)
mean = []
cut = []
cutx = []
cuty=[]
for y in y_axis:
mean.append(float(basel))
highcut = basel + ((variance-basel)/15)
lowcut = basel - ((variance-basel)/15)
for a in range (0,len(two)):
if two[a][1] > lowcut and two[a][1] < highcut:
cut.append(two[a])
for m in range (0, len(cut)):
cutx.append(cut[m][0])
cuty.append(cut[m][1])
base = peakutils.baseline(y_axis, 2)
cutbase = peakutils.baseline(asarray(cuty), 2)
pyplot.figure(figsize=(10,6))
pyplot.plot(x_axis, y_axis, label="data")
pyplot.plot(x_axis, base, label = "baseline")
pyplot.plot(x_axis, mean, label= "mean")
pyplot.plot(cutx, cuty, label= "cut")
pyplot.plot(cutx, asarray(cutbase), label = "cut baseline")
pyplot.plot()
pyplot.legend()
pyplot.show()