def process(self, prj, res1, res2): delta_t, velocity_pix, tol_pix = self.get_velocity_resolution( prj, res1, res2) max_bounds = max(self.bounds) if self.verbose: print "Processing:", res1.get_epoch(), res2.get_epoch( ), delta_t, velocity_pix, tol_pix if self.debug >= 2: fig, ax_check0 = self.stack.add_subplots("Segments check 1", ncols=1) fig, ax_check1 = self.stack.add_subplots("Segments check 2", ncols=1) epoch_all_mean_ncc = [] for segments1, segments2 in zip(res1, res2): if self.debug >= 2: imshow_segmented_image(ax_check0, segments1, alpha=1, projection=prj) imshow_segmented_image(ax_check1, segments2, alpha=1, projection=prj) scale = segments1.get_scale() # print "Scale %s: %s" % (scale, len(segments1)) all_ncc = [] all_ncc_shape = None for segment1 in segments1: x_dir = self.x_dir if x_dir == 'position_angle': x_dir = prj.p2s(p2i(segment1.get_coord())) elif nputils.is_callable(x_dir): x_dir = self.x_dir(prj.p2s(p2i(segment1.get_coord()))) if self.mode == 'ncc_peaks_direct': region1 = segment1.get_image_region() if min(region1.get_region().shape) <= 3: continue if self.rnd_pos_shift: shift = np.random.normal( 0, self.rnd_pos_factor * segment1.get_coord_error(min_snr=3)) region1.set_shift(shift) i1 = segments2.get_img().get_data() # i1 = nputils.shift2d(i1, np.array([4 / velocity_pix] * 2)) i1 = nputils.zoom(i1, region1.get_center(), [2 * tol_pix, 2 * tol_pix]) i2 = region1.get_region() shape = [ self.factor * (self.bounds[0] + self.bounds[1]), self.factor * (self.bounds[2] + self.bounds[3]) ] data = np.zeros(shape) # print i1.shape, i2.shape ncc = nputils.norm_xcorr2(i1, i2, mode='same') peaks = nputils.find_peaks(ncc, 4, self.ncc_threshold, fit_gaussian=False) if len(peaks) > 0: delta_pix = p2i(peaks - np.array(ncc.shape) / 2) delta = prj.pixel_scales() * np.array( delta_pix) * prj.unit v = delta / self.__get_delta_time(delta_t) if self.vel_trans: v = self.vel_trans( prj.p2s(p2i(segment1.get_coord())), v.T).T if x_dir is not None: vx, vy = nputils.vector_projection(v.T, x_dir) * v.unit else: vx, vy = v.T vx = vx.to(self.unit).value vy = vy.to(self.unit).value d = np.array([vx, vy]) if len(vx) > 0: ix = self.factor * (self.bounds[0] + vx) iy = self.factor * (self.bounds[2] + vy) widths = np.array( [[4 * self.factor * velocity_pix] * 2] * len(peaks)) # widths = np.array([[1] * 2] * len(peaks)) centers = np.array([iy, ix]).T heights = ncc[tuple( np.array([tuple(k) for k in peaks]).T)] # print widths, centers, heights data = imgutils.multiple_gaussian( shape, heights, widths, centers) ncc = data else: ncc = self.ncc_segment(segment1, segments2, 2 * tol_pix) if ncc is not None: # print ncc.shape, nputils.coord_max(ncc) ncc = zoom(ncc, velocity_pix * self.factor, order=3) # zoom will shift the center ncc = nputils.shift2d( ncc, [-(velocity_pix * self.factor) / 2.] * 2) # print ncc.shape, nputils.coord_max(ncc), velocity_pix * self.factor ncc = nputils.zoom(ncc, np.array(ncc.shape) / 2, [ 2 * max_bounds * self.factor, 2 * max_bounds * self.factor ]) # print ncc.shape, nputils.coord_max(ncc) # ncc = ncc[self.factor * (max_bounds - self.bounds[0]):self.factor * (max_bounds + self.bounds[1]), # self.factor * (max_bounds - self.bounds[2]):self.factor * (max_bounds + self.bounds[3])] # print ncc.shape if x_dir is not None: angle_rad = -np.arctan2(x_dir[1], x_dir[0]) ncc = rotate(ncc, -angle_rad / (2 * np.pi) * 360, reshape=False, order=2) if ncc is not None: if self.debug >= 3: fig, (ax0, ax1, ax2) = self.stack.add_subplots( "Segment %s" % segment1.get_segmentid(), ncols=3) r1 = segment1.get_image_region() ax0.imshow(r1.get_region()) i2 = nputils.zoom(segments2.get_img().get_data(), r1.get_center(), [2 * tol_pix, 2 * tol_pix]) ax1.imshow(i2, norm=plotutils.LogNorm(), extent=(-tol_pix, tol_pix, -tol_pix, tol_pix)) plotutils.img_axis(ax1) ax2.imshow(ncc, extent=self.global_ncc_extent) plotutils.img_axis(ax2) all_ncc.append(ncc) if all_ncc_shape is None: all_ncc_shape = ncc.shape if len(all_ncc) > 0: if scale not in self.global_ncc_scales: self.global_ncc_scales[scale] = [] self.global_ncc_scales_n[scale] = 0 mean_ncc = self.agg_fct(np.array(all_ncc), axis=0) epoch_all_mean_ncc.append(mean_ncc) if self.debug >= 1: fig, ax = self.stack.add_subplots( "Ncc epoch %s vs %s scale %s" % (res1.get_epoch(), res2.get_epoch(), scale)) ax.imshow(mean_ncc, extent=self.global_ncc_extent) plotutils.img_axis(ax) self.global_ncc_scales[scale].append(mean_ncc) self.global_ncc_scales_n[scale] += len(all_ncc) if self.debug >= 0.5 and len(epoch_all_mean_ncc) > 0: epoch_mean_ncc = self.agg_fct(np.array(epoch_all_mean_ncc), axis=0) fig, ax = self.stack.add_subplots( "Ncc epoch %s vs %s" % (res1.get_epoch(), res2.get_epoch())) ax.imshow(epoch_mean_ncc, extent=self.global_ncc_extent) plotutils.img_axis(ax)
def __init__(self): data = [ [ "unit", u.mas / u.year, "Velocity unit", nputils.validator_is(u.Unit) ], [ "bounds", [1, 1, 1, 1], "Velocity bounds", nputils.validator_list(4, (int, float)) ], [ "filter1", None, "Filter for the first image", nputils.validator_is((NoneType, nputils.AbstractFilter)) ], [ "filter2", None, "Filter for the second image", nputils.validator_is((NoneType, nputils.AbstractFilter)) ], [ "tol_pix_range", [4, 25], "Allowed range of pixel velocity resolution", nputils.validator_list(2, int) ], [ "ncc_threshold", 0.6, "Threshold for the NCC", nputils.validator_is(bool) ], [ "factor", 10, "Zoom factor of the resulting map", nputils.validator_in_range(1, 20) ], [ "method", 'ncc_peaks_direct', "Method used to compute the SCC", lambda v: v in ['ncc', 'ncc_peaks', 'ncc_peaks_direct'] ], [ "vector_direction", None, "Project the result on this direction", lambda v: v == 'position_angle' or nputils.is_callable( v) or isinstance(v, (list, np.ndarray, NoneType)) ], [ "velocity_trans", None, "Do any transform on the velocity vector, pre projection", lambda v: nputils.is_callable(v) ], [ "rnd_pos_shift", False, "Randomly shift the segments position", nputils.validator_is((bool, NoneType)) ], [ "rnd_pos_factor", 1.5, "Factor of the standart deviation of the shift", nputils.validator_in_range(0.1, 5) ], [ "img_rnd_shift", 0, "Randomly shift the images (pixel std)", nputils.validator_in_range(0, 10) ], [ "shuffle", False, "Suffle the list of images", nputils.validator_is(bool) ], ] nputils.BaseConfiguration.__init__( self, data, title="Stack cross correlation configuration")
def __init__(self): data = [ ["unit", u.mas / u.year, "Velocity unit", nputils.validator_is(u.Unit)], ["bounds", [1, 1, 1, 1], "Velocity bounds", nputils.validator_list(4, (int, float))], ["filter1", None, "Filter for the first image", nputils.validator_is((NoneType, nputils.AbstractFilter))], ["filter2", None, "Filter for the second image", nputils.validator_is((NoneType, nputils.AbstractFilter))], ["tol_pix_range", [4, 25], "Allowed range of pixel velocity resolution", nputils.validator_list(2, int)], ["ncc_threshold", 0.6, "Threshold for the NCC", nputils.validator_is(bool)], ["factor", 10, "Zoom factor of the resulting map", nputils.validator_in_range(1, 20)], ["method", 'ncc_peaks_direct', "Method used to compute the SCC", lambda v: v in ['ncc', 'ncc_peaks', 'ncc_peaks_direct']], ["vector_direction", None, "Project the result on this direction", lambda v: v == 'position_angle' or nputils.is_callable(v) or isinstance(v, (list, np.ndarray, NoneType))], ["velocity_trans", None, "Do any transform on the velocity vector, pre projection", lambda v: nputils.is_callable(v)], ["rnd_pos_shift", False, "Randomly shift the segments position", nputils.validator_is((bool, NoneType))], ["rnd_pos_factor", 1.5, "Factor of the standart deviation of the shift", nputils.validator_in_range(0.1, 5)], ["img_rnd_shift", 0, "Randomly shift the images (pixel std)", nputils.validator_in_range(0, 10)], ["shuffle", False, "Suffle the list of images", nputils.validator_is(bool)], ] nputils.BaseConfiguration.__init__(self, data, title="Stack cross correlation configuration")
def plot_links_map(ax, img, projection, links, color_style='link', mode='com', colorbar_setting=None, map_cmap='jet', vector_width=4, link_id_label=False, num_bbox=None, ang_vel_arrows=False, ang_vel_unit=u.mas / u.year, pix_per_ang_vel_unit=1, **kwargs): """Display features links on a map. Parameters ---------- ax : :class:`matplotlib.axes.Axes` img : Image An image to be used as background map. projection : :class:`libwise.imgutils.Projection` links : a list of :class:`wise.matcher.FeaturesLink` color_style : str, optional 'link': one color per link. 'date': map the features epochs to a color. any color string: use one color for each displacements vectors. function: a function that take a feature as argument and return a color. mode : str, optional Coord mode for the location of the features: 'lm', 'com' or 'cos'. colorbar_setting : ColorBar, optional Settings for the color bar if color_style is 'date'. map_cmap : :class:`matplotlib.cm.ColorMap` or str, optional vector_width : int, optional Width of the displacements vector arrows. Default is 4. link_id_label : bool, optional Annotate the links with there ids. num_bbox : dict, optional **kwargs: Additional arguments to be passed to :func:`libwise.plotutils.imshow_image`. .. _tags: plt_matching """ color_fct = None if color_style == 'date': all_epochs = matcher.get_all_epochs(links) epochs_map = plotutils.build_epochs_mappable(all_epochs, colorbar_setting.get_cmap()) if colorbar_setting is None: colorbar_setting = plotutils.ColorbarSetting(ticks_locator=mdates.AutoDateLocator(), ticks_formatter=mdates.DateFormatter('%m/%y')) colorbar_setting.add_colorbar(epochs_map, ax) color_fct = lambda f: epochs_map.to_rgba(f.get_epoch()) elif color_style is not 'link' and isinstance(color_style, str): color_fct = lambda f: color_style elif nputils.is_callable(color_style): color_fct = color_style plotutils.imshow_image(ax, img, projection=projection, title=False, cmap=plotutils.get_cmap(map_cmap), **kwargs) for link in links: delta_info = link.get_delta_info(measured_delta=False) if ang_vel_arrows: plot_velocity_vector(ax, delta_info, projection, ang_vel_unit, pix_per_ang_vel_unit, color_fct=color_fct, mode=mode, lw=0.5, fc=link.get_color(), ec='k', alpha=0.9, zorder=link.size(), width=vector_width) else: plot_displacement_vector(ax, delta_info, color_fct=color_fct, mode=mode, lw=0.5, fc=link.get_color(), ec='k', alpha=0.9, zorder=link.size(), width=vector_width) # y, x = link.get_features()[int(np.random.normal(s / 2, s / 4))].get_coord() if link_id_label: y, x = link.last().get_coord() if num_bbox is None: props = dict(boxstyle='round', facecolor='wheat', alpha=0.7, edgecolor=link.get_color(), lw=1.5) ax.text(x, y, link.get_id(), bbox=num_bbox, zorder=200, size='small')
def process(self, prj, res1, res2): delta_t, velocity_pix, tol_pix = self.get_velocity_resolution(prj, res1, res2) max_bounds = max(self.bounds) if self.verbose: print "Processing:", res1.get_epoch(), res2.get_epoch(), delta_t, velocity_pix, tol_pix if self.debug >= 2: fig, ax_check0 = self.stack.add_subplots("Segments check 1", ncols=1) fig, ax_check1 = self.stack.add_subplots("Segments check 2", ncols=1) epoch_all_mean_ncc = [] for segments1, segments2 in zip(res1, res2): if self.debug >= 2: imshow_segmented_image(ax_check0, segments1, alpha=1, projection=prj) imshow_segmented_image(ax_check1, segments2, alpha=1, projection=prj) scale = segments1.get_scale() # print "Scale %s: %s" % (scale, len(segments1)) all_ncc = [] all_ncc_shape = None for segment1 in segments1: x_dir = self.x_dir if x_dir == 'position_angle': x_dir = prj.p2s(p2i(segment1.get_coord())) elif nputils.is_callable(x_dir): x_dir = self.x_dir(prj.p2s(p2i(segment1.get_coord()))) if self.mode == 'ncc_peaks_direct': region1 = segment1.get_image_region() if min(region1.get_region().shape) <= 3: continue if self.rnd_pos_shift: shift = np.random.normal(0, self.rnd_pos_factor * segment1.get_coord_error(min_snr=3)) region1.set_shift(shift) i1 = segments2.get_img().get_data() # i1 = nputils.shift2d(i1, np.array([4 / velocity_pix] * 2)) i1 = nputils.zoom(i1, region1.get_center(), [2 * tol_pix, 2 * tol_pix]) i2 = region1.get_region() shape = [self.factor * (self.bounds[0] + self.bounds[1]), self.factor * (self.bounds[2] + self.bounds[3])] data = np.zeros(shape) # print i1.shape, i2.shape ncc = nputils.norm_xcorr2(i1, i2, mode='same') peaks = nputils.find_peaks(ncc, 4, self.ncc_threshold, fit_gaussian=False) if len(peaks) > 0: delta_pix = p2i(peaks - np.array(ncc.shape) / 2) delta = prj.pixel_scales() * np.array(delta_pix) * prj.unit v = delta / self.__get_delta_time(delta_t) if self.vel_trans: v = self.vel_trans(prj.p2s(p2i(segment1.get_coord())), v.T).T if x_dir is not None: vx, vy = nputils.vector_projection(v.T, x_dir) * v.unit else: vx, vy = v.T vx = vx.to(self.unit).value vy = vy.to(self.unit).value d = np.array([vx, vy]) if len(vx) > 0: ix = self.factor * (self.bounds[0] + vx) iy = self.factor * (self.bounds[2] + vy) widths = np.array([[4 * self.factor * velocity_pix] * 2] * len(peaks)) # widths = np.array([[1] * 2] * len(peaks)) centers = np.array([iy, ix]).T heights = ncc[tuple(np.array([tuple(k) for k in peaks]).T)] # print widths, centers, heights data = imgutils.multiple_gaussian(shape, heights, widths, centers) ncc = data else: ncc = self.ncc_segment(segment1, segments2, 2 * tol_pix) if ncc is not None: # print ncc.shape, nputils.coord_max(ncc) ncc = zoom(ncc, velocity_pix * self.factor, order=3) # zoom will shift the center ncc = nputils.shift2d(ncc, [- (velocity_pix * self.factor) / 2.] * 2) # print ncc.shape, nputils.coord_max(ncc), velocity_pix * self.factor ncc = nputils.zoom(ncc, np.array(ncc.shape) / 2, [2 * max_bounds * self.factor, 2 * max_bounds * self.factor]) # print ncc.shape, nputils.coord_max(ncc) # ncc = ncc[self.factor * (max_bounds - self.bounds[0]):self.factor * (max_bounds + self.bounds[1]), # self.factor * (max_bounds - self.bounds[2]):self.factor * (max_bounds + self.bounds[3])] # print ncc.shape if x_dir is not None: angle_rad = - np.arctan2(x_dir[1], x_dir[0]) ncc = rotate(ncc, - angle_rad / (2 * np.pi) * 360, reshape=False, order=2) if ncc is not None: if self.debug >= 3: fig, (ax0, ax1, ax2) = self.stack.add_subplots("Segment %s" % segment1.get_segmentid(), ncols=3) r1 = segment1.get_image_region() ax0.imshow(r1.get_region()) i2 = nputils.zoom(segments2.get_img().get_data(), r1.get_center(), [2 * tol_pix, 2 * tol_pix]) ax1.imshow(i2, norm=plotutils.LogNorm(), extent=(-tol_pix, tol_pix, -tol_pix, tol_pix)) plotutils.img_axis(ax1) ax2.imshow(ncc, extent=self.global_ncc_extent) plotutils.img_axis(ax2) all_ncc.append(ncc) if all_ncc_shape is None: all_ncc_shape = ncc.shape if len(all_ncc) > 0: if scale not in self.global_ncc_scales: self.global_ncc_scales[scale] = [] self.global_ncc_scales_n[scale] = 0 mean_ncc = self.agg_fct(np.array(all_ncc), axis=0) epoch_all_mean_ncc.append(mean_ncc) if self.debug >= 1: fig, ax = self.stack.add_subplots("Ncc epoch %s vs %s scale %s" % (res1.get_epoch(), res2.get_epoch(), scale)) ax.imshow(mean_ncc, extent=self.global_ncc_extent) plotutils.img_axis(ax) self.global_ncc_scales[scale].append(mean_ncc) self.global_ncc_scales_n[scale] += len(all_ncc) if self.debug >= 0.5 and len(epoch_all_mean_ncc) > 0: epoch_mean_ncc = self.agg_fct(np.array(epoch_all_mean_ncc), axis=0) fig, ax = self.stack.add_subplots("Ncc epoch %s vs %s" % (res1.get_epoch(), res2.get_epoch())) ax.imshow(epoch_mean_ncc, extent=self.global_ncc_extent) plotutils.img_axis(ax)