In [1]:
%pylab
import csv
In [41]:
class clicker_class(object):
def __init__(self, ax, pix_err=1):
self.canvas = ax.get_figure().canvas
self.cid = None
self.pt_lst = []
self.pt_plot = ax.plot([], [], marker='o',
linestyle='none', zorder=5)[0]
self.pix_err = pix_err
self.connect_sf()
def set_visible(self, visible):
'''sets if the curves are visible '''
self.pt_plot.set_visible(visible)
def clear(self):
'''Clears the points'''
self.pt_lst = []
self.redraw()
def connect_sf(self):
if self.cid is None:
self.cid = self.canvas.mpl_connect('button_press_event',
self.click_event)
def disconnect_sf(self):
if self.cid is not None:
self.canvas.mpl_disconnect(self.cid)
self.cid = None
def click_event(self, event):
''' Extracts locations from the user'''
if event.key == 'shift':
self.pt_lst = []
return
if event.xdata is None or event.ydata is None:
return
if event.button == 1:
self.pt_lst.append((event.xdata, event.ydata))
elif event.button == 3:
self.remove_pt((event.xdata, event.ydata))
self.redraw()
def remove_pt(self, loc):
if len(self.pt_lst) > 0:
self.pt_lst.pop(np.argmin(map(lambda x:
np.sqrt((x[0] - loc[0]) ** 2 +
(x[1] - loc[1]) ** 2),
self.pt_lst)))
def redraw(self):
if len(self.pt_lst) > 0:
x, y = zip(*self.pt_lst)
else:
x, y = [], []
self.pt_plot.set_xdata(x)
self.pt_plot.set_ydata(y)
self.canvas.draw()
def return_points(self):
'''Returns the clicked points in the format the rest of the
code expects'''
return np.vstack(self.pt_lst).T
figure(figsize=(20/2.54,20/2.54))
subplots_adjust(left=0, right=1, top=1, bottom=0)
ax = gca()
axis('equal')
ax.set_xlim(-5,5)
ax.set_ylim(-5,5)
ax.grid(True)
cc = clicker_class(ax)
In [42]:
x = cc.return_points()[0,:]
y = cc.return_points()[1,:]
xDraw = append(x,x[0])
yDraw = append(y,y[0])
plot(xDraw,yDraw)
Out[42]:
In [43]:
x_list = list()
y_list = list()
with open('../data/custom_cutout_x.csv', 'r') as x_csvfile:
with open('../data/custom_cutout_y.csv', 'r') as y_csvfile:
x_reader = csv.reader(x_csvfile, delimiter=',', lineterminator='\n')
y_reader = csv.reader(y_csvfile, delimiter=',', lineterminator='\n')
for row in x_reader:
x_list += [row]
for row in y_reader:
y_list += [row]
num_cutouts = len(x_list)
x_array = [0,]*(num_cutouts+1)
y_array = [0,]*(num_cutouts+1)
for i in range(num_cutouts):
x_array[i] = array(x_list[i], dtype='float')
y_array[i] = array(y_list[i], dtype='float')
In [44]:
x_array[-1] = x
y_array[-1] = y
In [45]:
with open('../data/custom_cutout_x.csv', 'w') as x_csvfile:
with open('../data/custom_cutout_y.csv', 'w') as y_csvfile:
x_writer = csv.writer(x_csvfile, delimiter=',', lineterminator='\n')
y_writer = csv.writer(y_csvfile, delimiter=',', lineterminator='\n')
for i in range(len(x_array)):
x_store = x_array[i]
y_store = y_array[i]
x_list = around(x_store,decimals=4).tolist()
y_list = around(y_store,decimals=4).tolist()
x_writer.writerow(x_list)
y_writer.writerow(y_list)
In [ ]: