def build_tree(self): tree = MultiScaleTree() # start from coarser scale and go down for segment in self[-1]: tree.add(segment) for (parent_segments, child_segments) in nputils.pairwise(self[::-1]): for segments in child_segments: parent = parent_segments.get_segment_from_feature(segments) if parent is None: parent_node = None else: parent_node = tree.get_node(parent) tree.add(segments, parent=parent_node) # second pass to fill ms_nodes for segments in child_segments: node = tree.get_node(segments) parent_node = node.get_parent() n_parent_childs = len(parent_node.get_childs()) if n_parent_childs == 1 and not parent_node.is_root(): parent_ms_node = tree.get_ms_node(parent_node) parent_ms_node.append(node) tree.ms_nodes[node] = parent_ms_node else: tree.add_ms_node(node) return tree
def info_files_delta(ctx, delta_time_unit=u.day, angular_velocity_unit=u.mas / u.year, proper_velocity_unit=unit_c): '''Print List of selected pair of files with information on velocity resolution Parameters ---------- ctx : :class:`wise.project.AnalysisContext` delta_time_unit : :class:`astropy.units.Unit`, optional angular_velocity_unit : :class:`astropy.units.Unit`, optional proper_velocity_unit : :class:`astropy.units.Unit`, optional .. _tags: task_general ''' all_delta_time = [] all_velocity_c_px = [] all_velocity_px = [] data = [] header = ["Date 1", "Date 2", "Delta (%s)" % delta_time_unit, "Angular vel. res. (%s)" % angular_velocity_unit, "Proper vel. res. (%s)" % proper_velocity_unit] has_distance = ctx.config.data.object_distance or ctx.config.data.object_z for file1, file2 in nputils.pairwise(ctx.files): img1 = ctx.open_file(file1) img2 = ctx.open_file(file2) prj = ctx.get_projection(img1) date1 = img1.get_epoch(str_format='%Y-%m-%d') date2 = img2.get_epoch(str_format='%Y-%m-%d') if not isinstance(date1, datetime.date): print "Images have no date information" return delta_t = ((img2.get_epoch() - img1.get_epoch()).total_seconds() * u.second).to(delta_time_unit) all_delta_time.append(delta_t) velocity_px = prj.angular_velocity([0, 0], [0, 1], delta_t).to(angular_velocity_unit) all_velocity_px.append(velocity_px) if has_distance: velocity_c_px = prj.proper_velocity([0, 0], [0, 1], delta_t).to(proper_velocity_unit) all_velocity_c_px.append(velocity_c_px) if has_distance: data.append([date1, date2, delta_t.value, velocity_px.value, velocity_c_px.value]) else: data.append([date1, date2, delta_t.value, velocity_px.value, '-']) # print "{0} -> {1}: Delta time: {2}, Velocity resolution: {3:.3f}, {4:.3f}".format(*data) all_delta_time = nputils.quantities_to_array(all_delta_time) all_velocity_px = nputils.quantities_to_array(all_velocity_px) print nputils.format_table(data, header) print "Mean Delta time: %s +- %s" % (np.mean(all_delta_time), np.std(all_delta_time)) print "Mean Velocity resolution: %s +- %s" % (np.mean(all_velocity_px), np.std(all_velocity_px)) if has_distance: all_velocity_c_px = nputils.quantities_to_array(all_velocity_c_px) print "Mean Velocity resolution: %s +- %s" % (np.mean(all_velocity_c_px), np.std(all_velocity_c_px))
def build(self): for (segments1, segments2) in nputils.pairwise(self.ms_image[::-1]): dict_relation = self.relations[segments1] for feature1 in segments1: dict_relation[feature1] = [] for feature2 in segments2: seg1_f2 = segments1.get_segment_from_feature(feature2) if seg1_f2 is not None and seg1_f2 == feature1: dict_relation[feature1].append(feature2)
def bootstrap_matching(ctx, n=100, filter=None, cb_post_match=None): all_res = dict() all_epochs = [] all_imgs = dict() for i, file in enumerate(ctx.files): img = ctx.open_file(file) all_epochs.append(img.get_epoch()) res = ctx.detection(img, filter=filter) all_res[img.get_epoch()] = res all_imgs[img.get_epoch()] = img t = time.time() all_match_ratio = [] 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) shuffled = nputils.permutation_no_succesive(all_epochs) match_ratio_list = [] match_results = project.AnalysisResult(ctx.config) for epoch1, epoch2 in nputils.pairwise(shuffled): res1 = all_res[epoch1].copy() res2 = all_res[epoch2].copy() # print "Matching:", res1.get_epoch(), "vs", res2.get_epoch() res1.epoch = epoch1 for segments in res1: segments.epoch = epoch1 res2.epoch = epoch2 for segments in res2: segments.epoch = epoch2 full_match_res = ctx.match(res1, res2, verbose=False) match_results.add_match_result(full_match_res) match_results.add_detection_result(all_imgs[epoch1], res1) for match_res in full_match_res: n_segments = match_res.segments1.size() match_ratio = (match_res.get_match().size()) / (float(n_segments)) match_ratio_list.append(match_ratio) all_match_ratio.append(np.mean(match_ratio_list)) if cb_post_match is not None: cb_post_match(shuffled, match_results) # print "Match ratio stat:", nputils.stat(match_ratio_list) # print all_match_ratio print "Match ratio stat:", nputils.stat(all_match_ratio)
def plot_links_dfc(ax, projection, links, mode='com', num=False, num_bbox=None, **kwargs): """Plot features link on a distance from core vs epoch plot. Parameters ---------- ax : :class:`matplotlib.axes.Axes` projection : :class:`libwise.imgutils.Projection` links : a list of :class:`wise.matcher.FeaturesLink` mode : str, optional Coord mode for the location of the features: 'lm', 'com' or 'cos'. num : bool, optional Whatever to optionally annotate the links with there ids. num_bbox : dict, optional **kwargs : Additional arguments to be passed to :func:`matplotlib.pyplot.plot`. .. _tags: plt_matching """ dfc_fct_coord = lambda coord: nputils.l2norm(projection.p2s(p2i(coord))) dfc_fct = lambda feature: dfc_fct_coord(feature.get_coord(mode)) for link in links: delta_info = link.get_delta_info() if num is True: if num_bbox is None: num_bbox = dict(boxstyle='round', facecolor='wheat', alpha=0.7, edgecolor=link.get_color(), lw=1.5) # ax.text(link.get_first_epoch(), dfc_fct(link.first()), "%s, %s" % (link.get_id(), link.size())) ax.text(link.get_first_epoch(), dfc_fct(link.first()), link.get_id(), bbox=num_bbox, zorder=200, size='small') if 'c' not in kwargs: line_color = link.get_color() else: line_color = kwargs['c'] # plotutils.checkargs(kwargs, 'lw', 2.5) for epoch, related in link.get_relations(): link_feature = link.get(epoch) related_feature = related.get(epoch) ax.plot([epoch, epoch], [dfc_fct(link_feature), dfc_fct(related_feature)], color='k', marker='+') for feature1, feature2 in nputils.pairwise(link.get_features()): epoch1 = feature1.get_epoch() epoch2 = feature2.get_epoch() delta = delta_info.get_delta(feature1) dfc1 = dfc_fct(feature1) dfc2 = dfc_fct(feature2) # dfc2 = dfc_fct_coord(feature1.get_coord(mode) + delta) ax.plot([epoch1, epoch2], [dfc1, dfc2], color=line_color, **kwargs) ax.plot([epoch1], dfc1, ls='', marker='+', color='k')