forked from jokva/labelmaker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
111 lines (86 loc) · 3.14 KB
/
main.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
#! /usr/bin/env python3
import argparse
import math
import numpy as np
import segyio
import sys
import matplotlib
matplotlib.use('Qt5Agg')
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib import patches
from matplotlib.lines import Line2D
class polybuilder(object):
def __init__(self, control, ax):
self.x = list(control.get_xdata())
self.y = list(control.get_ydata())
self.canvas = control.figure.canvas
self.control = control
self.ax = ax
self.polys = []
self.last_removed = None
self.pick = None
self.canvas.mpl_connect('button_release_event', self.onrelease)
self.canvas.mpl_connect('key_press_event', self.complete)
self.canvas.mpl_connect('pick_event', self.onpick)
self.keys = { 'escape': self.clear,
'enter': self.mkpoly,
'd': self.rmpoly,
'u': self.undo,
}
def onrelease(self, event):
if self.pick is not None:
self.pick = None
return
if self.canvas.manager.toolbar._active is not None: return
if event.inaxes != self.control.axes: return
if event.button != 1: return
self.x.append(event.xdata)
self.y.append(event.ydata)
self.control.set_data(self.x, self.y)
self.canvas.draw()
def clear(self, *_):
self.x, self.y = [], []
self.control.set_data(self.x, self.y)
def mkpoly(self, *_):
poly = patches.Polygon(list(zip(self.x, self.y)), alpha = 0.5)
self.ax.add_patch(poly)
self.polys.append(poly)
self.clear()
def rmpoly(self, event):
if event.inaxes != self.control.axes: return
for poly in self.polys:
if not poly.contains(event)[0]: continue
poly.remove()
self.last_removed = poly
self.polys.remove(poly)
def undo(self, *_ ):
# TODO: undo last added dot
if self.last_removed is None: return
if len(self.polys) > 0 and self.polys[-1] is self.last_removed: return
self.polys.append(self.last_removed)
self.ax.add_patch(self.last_removed)
def complete(self, event):
if event.key not in self.keys: return
self.keys[event.key](event)
self.canvas.draw()
def onpick(self, event):
if event.artist is not self.control: return
self.pick = 1
def main(argv):
parser = argparse.ArgumentParser(prog = argv[0],
description='Label those slices yo')
parser.add_argument('input', type=str, help='input file')
args = parser.parse_args(args = argv[1:])
with segyio.open(args.input) as f:
traces = f.trace.raw[:]
low, high = np.nanmin(traces), np.nanmax(traces)
_, ax = plt.subplots()
ax.imshow(traces, aspect='auto', cmap = plt.get_cmap('BuPu'))
line = Line2D([], [], ls='--', c='#666666',
marker='x', mew=2, mec='#204a87', picker = 5)
ax.add_line(line)
pb = polybuilder(line, ax)
plt.show()
if __name__ == '__main__':
main(sys.argv)