-
Notifications
You must be signed in to change notification settings - Fork 0
/
substractbackground.py
84 lines (76 loc) · 2.25 KB
/
substractbackground.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
# creates and substract background
# v11 - bakup
# v12 - working code - updated __name__ == block
# import easy plotting utility and plot
from plotmeagraph import Plot
from matplotlib import pyplot as plt
# crop the graph as needed for bg fit
import sys
import os
from xyfromcsv import xyfromcsv
from xyfromexcel import xyfromexcel
import pandas as pd
class backGround:
"""
substracts background from data (x, y)
"""
def __init__(self, x, y):
self.x = x
self.y = y
def generate(self):
"""
subcalsses redefines this method
"""
def substract(self):
# generates background
yLine = self.generate()
# substracts background
y = [(y1 - y2) for (y1, y2) in zip(self.y, yLine)]
# return the updated data
return (self.x, y)
class bgLinear(backGround):
def __init__(self, x, y, x1, y1, x2, y2):
self.x1 = x1
self.x2 = x2
self.y1 = y1
self.y2 = y2
backGround.__init__(self, x, y)
def generate(self):
"""
Generates a linear background.
Line --> two point form
y - y1 = {(y2 - y1)/(x2 - x1)} * (x - x1)
"""
f = lambda x: ((self.y2 - self.y1) / (self.x2 - self.x1))\
* (x - self.x1) + self.y1
y = [f(X) for X in self.x]
return y
if __name__ == '__main__':
try:
datafile = sys.argv[1]
except Exception:
print('Usage: python3 substractbackground.py data.csv')
print(sys.exc_info())
else:
plt.ion()
datafile = os.path.basename(datafile)
ext = datafile.split('.')[1]
if ext == 'csv':
x, y = xyfromcsv(datafile)
else:
if ext == 'xls' or ext == 'xlsx':
x, y = xyfromexcel(datafile)
Plot(x, y).plot()
# endpoints (x1, y1), (x2, y2)
print('enter bg endpoints')
x1 = float(input('x1: '))
y1 = float(input('y1: '))
x2 = float(input('x2: '))
y2 = float(input('y2: '))
I = bgLinear(x, y, x1, y1, x2, y2)
x, y = I.substract()
df = pd.DataFrame()
df['x'] = x; df['y'] = y
df.to_excel('bgcut'+datafile, index=False)
Plot(x,y).plot()
input('Enter to Quit: ')