def get_overlapping_segment(self, segment, delta=None, min_ratio=0.4): mask1 = segment.get_segmented_image().get_mask(segment) if delta is not None: mask1 = nputils.shift2d(mask1, delta) count = np.bincount((self.labels * mask1).flatten().astype(np.int)) count[0] = 0 imax = np.argmax(count) ratio = count.flatten()[imax] / float(mask1.sum()) if ratio >= min_ratio: return self.get_segment_from_id(imax) return None
def get_segments_inside2(self, segment, delta=None, ratio=None): mask = segment.get_mask() if delta is not None: mask = nputils.shift2d(mask, delta) result = FeaturesGroup() # print "Look for segments inside", segment for id, count in nputils.count((self.labels * mask).flatten()): segment_inside = self.get_segment_from_id(id) if segment_inside is not None: # print "->", segment_inside, count / float(segment_inside.get_area()), count / float(segment.get_area()) if ratio is not None: ratio_inside = max(count / float(segment_inside.get_area()), count / float(segment.get_area())) if ratio_inside < ratio: continue result.add_feature(segment_inside) return result
def get_segments_inside2(self, segment, delta=None, ratio=None): mask = segment.get_mask() if delta is not None: mask = nputils.shift2d(mask, delta) result = FeaturesGroup() # print "Look for segments inside", segment for id, count in nputils.count((self.labels * mask).flatten()): segment_inside = self.get_segment_from_id(id) if segment_inside is not None: # print "->", segment_inside, count / float(segment_inside.get_area()), count / float(segment.get_area()) if ratio is not None: ratio_inside = max( count / float(segment_inside.get_area()), count / float(segment.get_area())) if ratio_inside < ratio: continue result.add_feature(segment_inside) return result
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 bootstrap_scc(ctx, config, output_dir, n, nwise = 2, append=False, verbose=False, seperate_scales=False): """Perform Stack Cross Correlation analysis n time and store results in output_dir Parameters ---------- ctx : :class:`wise.project.AnalysisContext` config : :class:`wise.scc.SCCConfiguration` output_dir : str n : int append : bool, optional Append results seperate_scales : bool, optional .. _tags: task_scc """ random_shift = config.get("img_rnd_shift") if config.get("shuffle") == config.get("rnd_pos_shift"): print "Configuration Error: either 'shuffle' or 'rnd_pos_shift' need to be set" return all_files = list(ctx.files) prj = ctx.get_projection(ctx.open_file(all_files[0])) all_res1 = dict() all_res2 = dict() all_epochs = [] for file1 in ctx.files: img1 = ctx.open_file(file1) img1.data = nputils.shift2d(img1.data, np.random.uniform(-random_shift, random_shift, 2)) img2 = ctx.open_file(file1) img2.data = nputils.shift2d(img2.data, np.random.uniform(-random_shift, random_shift, 2)) res1 = ctx.detection(img1, filter=config.get("filter1")) print "-> Numbers of detected SSP: %s" % ", ".join([str(k.size()) for k in res1]) res2 = ctx.detection(img2, filter=config.get("filter2")) print "-> Numbers of detected SSP: %s" % ", ".join([str(k.size()) for k in res2]) all_res1[file1] = res1 all_res2[file1] = res2 all_epochs.append(img1.get_epoch()) t = time.time() # all_segments2_img = dict() # for file, segments2 in all_res2.items(): # all_segments2_img[file] = [k.get_img().data.copy() for k in segments2] if not os.path.exists(output_dir): os.mkdir(output_dir) files = os.listdir(output_dir) if append and len(files) > 0: if seperate_scales and os.path.isdir(os.path.join(output_dir, files[0])): files = os.listdir(os.path.join(output_dir, files[0])) all_i = sorted([int(os.path.splitext(file)[0].split('_')[-1]) for file in files]) if len(all_i) == 0: start = 0 else: start = all_i[-1] + 1 else: start = 0 for i in range(n): eta = "" if i > 0: remaining = (np.round((time.time() - t) / float(i) * (n - i))) eta = " (ETA: %s)" % time.strftime("%H:%M:%S", time.localtime(time.time() + remaining)) print "Run %s / %s%s" % (i + 1, n, eta) if config.get("shuffle"): # np.random.shuffle(all_files) shuffled = nputils.permutation_no_succesive(all_files) files_pair = nputils.nwise(shuffled, nwise) else: files_pair = nputils.nwise(all_files, nwise) epochs_pair = nputils.nwise(all_epochs, nwise) scc_result = scc.StackCrossCorrelation(config, verbose=verbose) for shuffled_pair, epoch_pair in zip(files_pair, epochs_pair): res1 = all_res1[shuffled_pair[0]] res2 = all_res2[shuffled_pair[-1]] # for segments2, segments2_img in zip(res2, all_segments2_img[shuffled_pair[-1]]): # segments2.get_img().data = nputils.shift2d(segments2_img, # np.random.uniform(-random_shift, random_shift, 2)) res1.epoch = epoch_pair[0] res2.epoch = epoch_pair[-1] delta_t, velocity_pix, tol_pix = scc_result.get_velocity_resolution(prj, res1, res2) if not nputils.in_range(tol_pix, config.get("tol_pix_range")): print "-> Skip: Not in the allowed range of pixel velocity resolution:", tol_pix continue scc_result.process(prj, res1, res2) if seperate_scales: for scale, gncc_map in scc_result.get_mean_ncc_scales(smooth_len=1).items(): save_dir = os.path.join(output_dir, "scale_%s" % scale) if not os.path.exists(save_dir): os.mkdir(save_dir) imgutils.Image(gncc_map).save_to_fits(os.path.join(save_dir, "gncc_map_%s.fits" % (start + i))) else: gncc_map = scc_result.get_global_ncc(smooth_len=1) imgutils.Image(gncc_map).save_to_fits(os.path.join(output_dir, "gncc_map_%s.fits" % (start + i))) print "Done"
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)