-
Notifications
You must be signed in to change notification settings - Fork 0
/
cropmygraph.py
executable file
·85 lines (79 loc) · 2.42 KB
/
cropmygraph.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
#!/usr/bin/env python3
# plot a csv 2 column data
import pandas as pd
import matplotlib.pyplot as plt
# get access to stdin/out
plt.ion()
import sys
from matplotlib.backend_bases import MouseButton
from xyfromfile import xyfromfile
import sys
import os
def cropIt(x, y, xmin, xmax):
# exclude values less than xmin
y = y[x > xmin]
# exclude values greater than xmax
y = y[x < xmax]
# exclude values less than xmin
x = x[x > xmin]
# exclude values greater than xmax
x = x[x < xmax]
return x, y
# saves the clicked x, y data
class clickMe:
"""
saves the clicked co-ordinates in `clicked`
aim: to removed `clicked` from global scope
"""
def __init__(self):
self.clicked = []
def on_click(self, event):
"""
To save the x, y data from right mouse click
"""
if event.button is MouseButton.LEFT:
self.clicked.append((event.xdata, event.ydata))
print((event.xdata, event.ydata))
def cropMyGraph(data):
# create a clickMe instance to store clicks
I = clickMe()
# set up matplotlib for catching mouse clicks
plt.connect('button_press_event', I.on_click)
# read the file
x, y = xyfromfile(data)
while True:
# turn stdin on
plt.plot(x, y)
plt.show()
# wait to choose the end points
wait = input('choose crop points and press enter; \nq to quit: \n')
# breakout of the infinite loop
if wait == 'q': break
# visualy inspect graph and click suitable
# crop points
xmin = I.clicked[0][0]
xmax = I.clicked[1][0]
# clear I.clicked for further use
I.clicked = []
# call cropIt with suitable crop points
x, y = cropIt(x, y, float(xmin), float(xmax))
df = pd.DataFrame()
df['x']=x ; df['y'] = y
file_noext = os.path.basename(data).split('.')[0]
df.to_excel('trim'+ file_noext + '.xlsx', index=False)
return (x, y)
# run as a top-level file by reading
# data as commnadline argument
if __name__ == '__main__':
# help command line option
try:
datafile = sys.argv[1] or 'o1s.csv'
except Exception:
print('Usage: cropmygraph.py mydata.ext ')
print(sys.exc_info())
else:
# visualize the data and choose crop points
# the returned df will be a cropped graph
data = sys.argv[1]
# data --> o1s.csv
(x, y) = cropMyGraph(data)