def get_scanpos_given_circles(XY_roi, XY, Circs, stm_r_roi, stm_r, ax=None, max_pipette_xovershoot=300.): """ Parameters ---------- XY_roi - center positions of circular regions of interest XY - Circs - stm_r_roi - radius of circular regions of interest, in microns stm_r - radius of light stimulus, in microns ax - matplotlib axes object on which IM is plotted. max_pipette_xovershoot - In microns. Max x distance to move to the right of pipette tip. Go to far and the objective will touch the pipette and destroy the patch. Assumes that the pipette enters from the right. In microns. Returns ------- """ # If no axes was set, plot on the currently active. if ax is None: ax = plt.gca() step_len = np.floor(sqrt(2*stm_r**2)) xlim, ylim = np.sort(ax.get_xlim()), np.sort(ax.get_ylim()) # Draw a grid with spacing 'step_len' covering the entire image y, x = np.ogrid[ylim[0]:ylim[1]:step_len,xlim[0]:xlim[1]:step_len] mask = np.zeros((y.shape[0],x.shape[1])) for xcntr,ycntr in XY_roi: mask = logical_or(mask, ((y - ycntr)**2 + (x - xcntr)**2) <= stm_r_roi**2) labeled_mask, n_labels = label( mask ) mask_labels = np.unique( labeled_mask )[1:] ix0 = XY.shape[0] XY.resize(ix0+mask.sum(), refcheck=False) for ix, ml in enumerate(mask_labels): y_ix, x_ix = nonzero(labeled_mask == ml) ix1 = x_ix.shape[0] + ix0 XY[ix0:ix1] = zip(x[0,x_ix],y[y_ix,0]) ix0 = ix1 # Remove positions outside of max_pipette_xovershoot if max_pipette_xovershoot > 0: XY = XY[XY['X'] <= max_pipette_xovershoot] elif max_pipette_xovershoot < 0: XY = XY[XY['X'] >= max_pipette_xovershoot] for x,y in XY: c = Circle((x,y), stm_r, ec=prestm_col, ls='dotted', fill=False) Circs.append(ax.add_patch(c)) plt.title(ax.get_title()+r', $N_{'+str(int(round(stm_r*2)))+'}='+str(len(XY))+'$') plt.show() plt.draw() sleep(1e-3) return XY, Circs
def get_scanpos_given_4_sides(stm_r, corners, ax=None, max_pipette_xovershoot=300.): """ """ # If no axes was set, plot on the currently active. if ax is None: ax = plt.gca() step_len = np.floor(np.sqrt(2*stm_r**2)) flk = step_len/2.0 lo_l, up_l = corners["lo_left"], corners["up_left"] lo_r, up_r = corners["lo_right"], corners["up_right"] # Clean out x and ys outside outer border of ROI. lbord_m = (lo_l[1] - up_l[1]) / (lo_l[0] - up_l[0]) lbord_b = lo_l[1] - lbord_m*lo_l[0] rbord_m = (lo_r[1] - up_r[1]) / (lo_r[0] - up_r[0]) rbord_b = lo_r[1] - rbord_m*lo_r[0] upbord_m = (up_l[1] - up_r[1]) / (up_l[0] - up_r[0]) upbord_b = up_l[1] - upbord_m*up_l[0] lobord_m = (lo_l[1] - lo_r[1]) / (lo_l[0] - lo_r[0]) lobord_b = lo_l[1] - lobord_m*lo_l[0] lbord = lambda x, y: x > (y - lbord_b) / lbord_m - flk rbord = lambda x, y: x < (y - rbord_b) / rbord_m + flk lobord = lambda x, y: y > lobord_m*x + lobord_b - flk upbord = lambda x, y: y < upbord_m*x + upbord_b + flk X_tmp = np.arange(min(lo_l[0],up_l[0]), max(lo_r[0]+100,up_r[0]+100), step_len) Y_tmp = np.arange(min(lo_l[1],lo_r[1]), max(up_l[1]+100,up_r[1]+100), step_len) # Remove positions outside of max_pipette_xovershoot if max_pipette_xovershoot > 0: X_tmp = X_tmp[X_tmp <= max_pipette_xovershoot] elif max_pipette_xovershoot < 0: X_tmp = X_tmp[X_tmp >= max_pipette_xovershoot] X,Y = [],[] Circs = [];pos_num = 0 for x in X_tmp: for y in Y_tmp: if lbord(x,y) and rbord(x,y) and lobord(x,y) and upbord(x,y) : c = Circle((x, y), stm_r,ec=prestm_col, ls='dotted', fill=False) Circs.append(ax.add_patch(c)) X.append(x) Y.append(y) plt.text(x, y, str(pos_num)) pos_num += 1 nstmloc = len(X) title(r'Stm pos: $N_{'+str(int(round(stm_r*2)))+'}='+str(nstmloc)+'$') plt.show() plt.draw() sleep(1e-3) XY = np.recarray(len(X), dtype=[('X',float),('Y',float)]) XY['X'], XY['Y'] = X, Y return XY, Circs
def get_ROI_4_sides(ax = None): """ Returns four sides of an Region Of Interest """ # Assumes scale bar length 500 microns scalebar_len_um = 500. # microns #prestm_col = [.3,.7,.3] # If no axes was set, plot on the currently active. if ax is None: ax = plt.gca() print 'Click on the top and the bottom of the scalebar.\n' sclbar_xy_px = np.array(plt.ginput(n=2)) print 'Click on pipette tip/cell.\n' cell_xy_px = np.array(plt.ginput(n=1, timeout=60))[0] sclbar_len_px = abs(diff(sclbar_xy_px[:,1]))[0] um_to_px = sclbar_len_px/scalebar_len_um xlim_px, ylim_px = ax.get_xlim(), ax.get_ylim() xlim_um = ((xlim_px[0]-cell_xy_px[0])/um_to_px, (xlim_px[1]-cell_xy_px[0])/um_to_px) ylim_um = ((ylim_px[0]-cell_xy_px[1])/um_to_px, (ylim_px[1]-cell_xy_px[1])/um_to_px) IM = ax.get_images()[0] IM.set_extent((xlim_um[0], xlim_um[1], ylim_um[0], ylim_um[1])) xlabel('micron') ylabel('micron') plt.draw() sleep(1e-3) # Get and plot the outer borders of the area to stimulate print 'Click on 4 corners making up the border of region to stimulate.\n' region_border_xy = np.array(ginput(n=4)) lol_bix = np.ones(4, dtype=bool) up_ix = np.argsort(region_border_xy[:,1])[2:] r_ix = np.argsort(region_border_xy[:,0])[2:] lol_bix[up_ix] = False lor_bix = lol_bix.copy() lol_bix[r_ix] = False lor_bix[lol_bix] = False upl_bix = ~(lor_bix | lol_bix) upl_bix[r_ix] = False upr_bix = ~(lor_bix | lol_bix | upl_bix) lo_left = region_border_xy[lol_bix, :].flatten() up_right = region_border_xy[upr_bix, :].flatten() lo_right = region_border_xy[lor_bix, :].flatten() up_left = region_border_xy[upl_bix, :].flatten() plt.plot([lo_left[0], up_left[0], up_right[0], lo_right[0], lo_left[0]], [lo_left[1], up_left[1], up_right[1], lo_right[1], lo_left[1]], c=prestm_col) ax.set_xlim(xlim_um) ax.set_ylim(ylim_um) corners = {'lo_left':lo_left, 'up_left':up_left, 'up_right':up_right, 'lo_right':lo_right} return corners