def save_graph(self):
     """ Saves a copy of the progress so far """
     r = Report(self.id_dds)
     outdir = 'out/cover-progress/%s/' % self.id_dds
     self.draw_graph(r)
     filename = os.path.join(outdir, 'graphs.html')
     logger.info('Writing to %r' % filename)
     r.to_html(filename)
Example #2
0
def job_report(context, subsets, store):
    for id_subset, which in subsets.items():
        logger.info('%s = %s' % (id_subset, which))
        subset = store.select(lambda x: x['id_distance'] in which)
        logger.info('selected %s' % len(subset))
        substats = context.comp(compute_statistics, subset, job_id='%s-s' % id_subset)
        report = context.comp(report_statistics_all, id_subset, substats)
        context.add_report(report, 'main', subset=id_subset)
Example #3
0
 def save_graph(self):
     """ Saves a copy of the progress so far """
     r = Report(self.id_dds)
     outdir = 'out/cover-progress/%s/' % self.id_dds
     self.draw_graph(r)
     filename = os.path.join(outdir, 'graphs.html')
     logger.info('Writing to %r' % filename)
     r.to_html(filename, write_pickle=True)
Example #4
0
    def log_chosen(self, node):
        all_visibility = map(self.visibility, self.G.nodes())

        logger.info('#%4d closed %4d open %4d [minvis: %.4g] pop %s' %
                    (self.iterations, len(self.closed), len(self.open_nodes),
                     np.min(all_visibility), plan_friendly(node)))
        if (self.iterations + 1) % self.debug_graph_iterations == 0:
            self.save_graph()
 def log_chosen(self, node):
     all_visibility = map(self.visibility, self.G.nodes())
     
     logger.info('#%4d closed %4d open %4d [minvis: %.4g] pop %s' % 
           (self.iterations, len(self.closed), len(self.open_nodes),
            np.min(all_visibility),
            self.node_friendly(node)))
     if (self.iterations + 1) % self.debug_graph_iterations == 0:
         self.save_graph()
Example #6
0
def results2stats_dict(results):
    """ Applies all possible statistics to the results (output of run_planning_stats). """
    res = {}
    for x in Stats.statistics:
        try:
            res[x] = Stats.statistics[x].function(results)
        except:
            logger.info('stats error')
            res[x] = -1
    return res
Example #7
0
def job_report(context, subsets, store):
    for id_subset, which in subsets.items():
        logger.info('%s = %s' % (id_subset, which))
        subset = store.select(lambda x: x['id_distance'] in which)
        logger.info('selected %s' % len(subset))
        substats = context.comp(compute_statistics,
                                subset,
                                job_id='%s-s' % id_subset)
        report = context.comp(report_statistics_all, id_subset, substats)
        context.add_report(report, 'main', subset=id_subset)
Example #8
0
def results2stats_dict(results):
    """ Applies all possible statistics to the results (output of run_planning_stats). """
    res = {}
    for x in Stats.statistics:
        try:
            res[x] = Stats.statistics[x].function(results)
        except:
            logger.info('stats error')
            res[x] = -1
    return res
Example #9
0
    def __init__(self, plan_time, plan_max_length, metric_goal):
        logger.info('initiating sampleplanner')
        print('pr: initiating sampleplanner')
        DiffeoPlanningAlgo.__init__(self)
        config = get_dp_config()

        self.metric_goal = config.distances.instance(metric_goal)
        self.plan_time = plan_time
        self.max_length = plan_max_length
        logger.info('done')
        print('pr: done')
Example #10
0
 def __init__(self, plan_time, plan_max_length, metric_goal):
     logger.info('initiating sampleplanner')
     print('pr: initiating sampleplanner')
     DiffeoPlanningAlgo.__init__(self)
     config = get_dp_config()
     
     self.metric_goal = config.distances.instance(metric_goal)
     self.plan_time = plan_time
     self.max_length = plan_max_length
     logger.info('done')
     print('pr: done')
Example #11
0
def compute_dist_stats(id_distance, id_stream, delta):
    distances_library = get_conftools_uncertain_image_distances()
    distance = distances_library.instance(id_distance)
    stream = get_conftools_streams().instance(id_stream)
    it = stream.read_all()
    results = []
    for logitem in iterate_testcases(it, delta):
        assert_allclose(len(logitem.u), delta)
        y0 = UncertainImage(logitem.y0)
        y1 = UncertainImage(logitem.y1)
        d = distance.distance(y0, y1)
        results.append(d)
        
    logger.info('%s: found %d of %d steps in %s' % 
                (id_distance, len(results), delta, id_stream))
    return results
Example #12
0
def compute_dist_stats(id_distance, id_stream, delta):
    distances_library = get_conftools_uncertain_image_distances()
    distance = distances_library.instance(id_distance)
    stream = get_conftools_streams().instance(id_stream)
    it = stream.read_all()
    results = []
    for logitem in iterate_testcases(it, delta):
        assert_allclose(len(logitem.u), delta)
        y0 = UncertainImage(logitem.y0)
        y1 = UncertainImage(logitem.y1)
        d = distance.distance(y0, y1)
        results.append(d)

    logger.info('%s: found %d of %d steps in %s' %
                (id_distance, len(results), delta, id_stream))
    return results
Example #13
0
 def save(self, outdir):
     """ Creates outdir/<id_tc>.pickle and outdir/<>.yaml """
     filename_pickle = self.id_tc + '.tc.pickle'
     filename_yaml = self.id_tc + '.tc.yaml' 
     description = {
        'id': self.id_tc,
        'desc': 'Automatically generated test case',
        'code': ['diffeoplan.library.load_pickle',
                 {'file:pickle': filename_pickle}]
     }
     
     filename_pickle = os.path.join(outdir, filename_pickle)
     filename_yaml = os.path.join(outdir, filename_yaml)
     
     logger.info('Writing to %r ' % friendly_path(filename_pickle))
     safe_pickle_dump(self, filename_pickle)
     
     write_entries([description], filename_yaml)
 def _get_flat_structure(self, shape):
     if self.fs is None:
         def make_size(x):
             x = int(np.ceil(x))
             if x % 2 == 0:
                 x += 1
             return x
         
         ax = make_size(shape[0] * self.ratios[0])
         ay = make_size(shape[1] * self.ratios[1])
         area = (ax, ay)
         if np.min(area) == 1:
             logger.warning('Area is too small. Ratios: %s shape: %s area: %s' % 
                            (self.ratios, shape, area))
         logger.info('ratios: %s shape: %s area: %s' % (self.ratios,
                                                        shape, area))
         self.fs = FlatStructure(shape=tuple(shape), neighborarea=tuple(area))
     
     return self.fs
Example #15
0
    def from_matrices(labels, commute, inverse, same):
        logger.info('Creating PlanReducer from matrices:\n' + 
                    formatm('same', same, 'oppo', inverse, 'swap', commute))
        pr = PlanReducer()
        for i, j in iterate_indices(commute.shape):
            li = labels[i]
            lj = labels[j]
            if commute[i, j]:
                assert commute[j, i]
                pr.set_commute(li, lj)
        
            if same[i, j]:
                assert same[j, i]
                pr.set_same(li, lj)

            if inverse[i, j]:
                assert inverse[j, i]
                pr.set_inverse(li, lj)
        return pr
Example #16
0
    def from_matrices(labels, commute, inverse, same):
        logger.info('Creating PlanReducer from matrices:\n' +
                    formatm('same', same, 'oppo', inverse, 'swap', commute))
        pr = PlanReducer()
        for i, j in iterate_indices(commute.shape):
            li = labels[i]
            lj = labels[j]
            if commute[i, j]:
                assert commute[j, i]
                pr.set_commute(li, lj)

            if same[i, j]:
                assert same[j, i]
                pr.set_same(li, lj)

            if inverse[i, j]:
                assert inverse[j, i]
                pr.set_inverse(li, lj)
        return pr
Example #17
0
    def _get_flat_structure(self, shape):
        if self.fs is None:

            def make_size(x):
                x = int(np.ceil(x))
                if x % 2 == 0:
                    x += 1
                return x

            ax = make_size(shape[0] * self.ratios[0])
            ay = make_size(shape[1] * self.ratios[1])
            area = (ax, ay)
            if np.min(area) == 1:
                logger.warning(
                    'Area is too small. Ratios: %s shape: %s area: %s' %
                    (self.ratios, shape, area))
            logger.info('ratios: %s shape: %s area: %s' %
                        (self.ratios, shape, area))
            self.fs = FlatStructure(shape=tuple(shape),
                                    neighborarea=tuple(area))

        return self.fs
Example #18
0
    def save(self, outdir):
        """ Creates outdir/<id_tc>.pickle and outdir/<>.yaml """
        filename_pickle = self.id_tc + '.tc.pickle'
        filename_yaml = self.id_tc + '.tc.yaml'
        description = {
            'id':
            self.id_tc,
            'desc':
            'Automatically generated test case',
            'code': [
                'diffeoplan.library.load_pickle', {
                    'file:pickle': filename_pickle
                }
            ]
        }

        filename_pickle = os.path.join(outdir, filename_pickle)
        filename_yaml = os.path.join(outdir, filename_yaml)

        logger.info('Writing to %r ' % friendly_path(filename_pickle))
        safe_pickle_dump(self, filename_pickle)

        write_entries([description], filename_yaml)
Example #19
0
 def node_compare(self, plan1, plan2):
     plan1 = tuple(plan1)
     plan2 = tuple(plan2)
     if plan1 == plan2:
         return True
     
     dn = self.plan_distance(plan1, plan2, diffeoaction_distance_L2_infow) / self.ds.scalew
     
     match = dn < self.collapse_threshold
     if match:
         # print('%10g  %4d %4d' % (dn, len(plan1), len(plan2)))
         logger.info('Found match at %g' % dn)
         logger.info('- plan1: %s' % plan_friendly(plan1))
         logger.info('- plan1: %s' % plan_friendly(plan2))
     
     return match 
Example #20
0
    def node_compare(self, plan1, plan2):
        plan1 = tuple(plan1)
        plan2 = tuple(plan2)
        if plan1 == plan2:
            return True

        dn = self.plan_distance(
            plan1, plan2, diffeoaction_distance_L2_infow) / self.ds.scalew

        match = dn < self.collapse_threshold
        if match:
            # print('%10g  %4d %4d' % (dn, len(plan1), len(plan2)))
            logger.info('Found match at %g' % dn)
            logger.info('- plan1: %s' % plan_friendly(plan1))
            logger.info('- plan1: %s' % plan_friendly(plan2))

        return match
    def node_compare(self, plan1, plan2):
        plan1 = tuple(plan1)
        plan2 = tuple(plan2)
        if plan1 == plan2:
            return True

        if super(DiffeoCoverExp, self).node_compare(plan1, plan2):
            return True
        
        dn = self.plan_distance(plan1, plan2)
        match = dn < self.collapse_threshold
        if match:
            # print('%10g  %4d %4d' % (dn, len(plan1), len(plan2)))
            logger.info('Found match at %g' % dn)
            logger.info('- plan1: %s' % plan_friendly(plan1))
            logger.info('- plan1: %s' % plan_friendly(plan2))
        
        return match 
Example #22
0
    def node_compare(self, plan1, plan2):
        plan1 = tuple(plan1)
        plan2 = tuple(plan2)
        if plan1 == plan2:
            return True

        if super(DiffeoCoverExp, self).node_compare(plan1, plan2):
            return True

        dn = self.plan_distance(plan1, plan2)
        match = dn < self.collapse_threshold
        if match:
            # print('%10g  %4d %4d' % (dn, len(plan1), len(plan2)))
            logger.info('Found match at %g' % dn)
            logger.info('- plan1: %s' % plan_friendly(plan1))
            logger.info('- plan1: %s' % plan_friendly(plan2))

        return match
Example #23
0
    def plan(self, y0, y1, precision=None, min_visibility=None):
        """ 
            This must return an instance of PlanningResult.
        """
        time0 = time.time()
        num = len(self.composed_dds.composed_actions)
        unevaled = range(num)
        evaled = []
        min_dist = self.metric_goal.distance(y0, y1)
        best_action = None
        while (time.time() - time0 < self.plan_time):
            if len(unevaled) == 0:
                logger.info('Search done after %g evaluations' % len(evaled))
                break

            i = unevaled[np.random.randint(len(unevaled))]
            ypi = self.composed_dds.composed_actions[i].predict(y0)
            di = self.metric_goal.distance(ypi, y1)

            if min_dist > di:

                min_dist = di
                best_action = i

            unevaled.remove(i)
            evaled.append(i)
        if best_action is None:
            plan = ()
        else:
            plan = self.composed_dds.plan_reduced[best_action]
        logger.info('Plan output: ' + str(plan))
        logger.info('predicted distance for plan: ' + str(min_dist))

        return PlanningResult(success=True,
                              plan=plan,
                              status=(str(len(evaled)) + 'of' + str(num) +
                                      ' evaluated'))
Example #24
0
 def plan(self, y0, y1, precision=None, min_visibility=None):
     """ 
         This must return an instance of PlanningResult.
     """
     time0 = time.time()
     num = len(self.composed_dds.composed_actions)
     unevaled = range(num)
     evaled = []
     min_dist = self.metric_goal.distance(y0, y1)
     best_action = None
     while(time.time() - time0 < self.plan_time):
         if len(unevaled) == 0:
             logger.info('Search done after %g evaluations' % len(evaled))
             break
         
         i = unevaled[np.random.randint(len(unevaled))]
         ypi = self.composed_dds.composed_actions[i].predict(y0)
         di = self.metric_goal.distance(ypi, y1)
         
         if min_dist > di:
             
             min_dist = di
             best_action = i
         
         unevaled.remove(i)
         evaled.append(i)
     if best_action is None:
         plan = ()
     else:
         plan = self.composed_dds.plan_reduced[best_action]
     logger.info('Plan output: ' + str(plan))
     logger.info('predicted distance for plan: ' + str(min_dist))
     
     return PlanningResult(success=True, plan=plan,
                           status=(str(len(evaled)) + 'of' + str(num) + 
                                   ' evaluated'))
Example #25
0
 def log_child_discarded(self, node, action, child,
                         matches):  # @UnusedVariable
     logger.info('Discarding %s because it matches %s' %
                 (str(child), matches))
Example #26
0
def report_statistics_all(id_sub, stats, perc=10, W=0.2):
    records = stats['records']

    r = Report('statsall-%s' % id_sub)
    r.data('records', records)
    f = r.figure()
    
    id_distances = sorted(set(records['id_distance']))
        
    logger.info('%s: %s %s reo %s' % (id_sub, len(stats), id_distances,
                                      len(records)))

    colors = list(islice(cycle(['r', 'g', 'b', 'k', 'y', 'm']), 50))
    

    with f.plot('distance_order', **dp_predstats_fig) as pylab:
        ax = pylab.subplot(111)
        for i, id_d in enumerate(id_distances):
            which = records['id_distance'] == id_d
            delta = records[which]['delta']
            distance = records[which]['distance']
            order = scale_score(distance)
            order = order / float(order.size)

            
            step = float(i) / (max(len(id_distances) - 1, 1))
            xstep = W * 2 * (step - 0.5) 
            fancy_error_display(ax, delta + xstep, order,
                                colors[i], perc=perc, label=id_d)
            
        ieee_spines(pylab)    
        ticks = sorted(list(set(list(delta))))
        pylab.xlabel('plan length')
        pylab.ylabel('normalized distance')
        pylab.xticks(ticks, ticks)
        pylab.yticks((0, 1), (0, 1))
        pylab.axis((0.5, 0.5 + np.max(delta), -0.024, 1.2))
        legend_put_below(ax)

    with f.plot('distance', **dp_predstats_fig) as pylab:
        ax = pylab.subplot(111)
        for i, id_d in enumerate(id_distances):
            which = records['id_distance'] == id_d
            delta = records[which]['delta']
            distance = records[which]['distance']

            step = float(i) / max(len(id_distances) - 1, 1)
            xstep = W * 2 * (step - 0.5) 
            fancy_error_display(ax, delta + xstep, distance,
                                colors[i], perc=perc, label=id_d)
            
        ieee_spines(pylab)    
        ticks = sorted(list(set(list(delta))))
        pylab.xlabel('plan length')
        pylab.ylabel('distance')
        pylab.xticks(ticks, ticks)
#        pylab.yticks((0, 1), (0, 1))
        a = pylab.axis()
        pylab.axis((0.5, 0.5 + np.max(delta), -0.024, a[3]))
        legend_put_below(ax)

    return r
Example #27
0
 def log_child_equivalent_found(self, node, action, child,
                                match):  # @UnusedVariable
     logger.info('Equiv found: %s = %s' % (child, match))
 def log_child_discarded(self, node, action, child, matches):  # @UnusedVariable
     logger.info('Discarding %s because it matches %s' % (str(child), matches))
 def log_child_equivalent_found(self, node, action, child, match):  # @UnusedVariable
     logger.info('Equiv found: %s = %s' % (child, match))
Example #30
0
def run_planning(id_algo, id_tc, testcase, algo):
    '''
    
    :param config:
    :param id_algo:
    :param id_discdds:
    :param id_tc:
    :param algo: Already init()ed instance of DiffeoPlanningAlgo
    ''' 
    # run the planning
    y0 = testcase.y0
    y1 = testcase.y1
    
    # compute achievable precision using the model
    dds = algo.get_dds()
    metric = algo.metric_goal
    y1p = dds.predict(y0, testcase.true_plan)
    d = metric.distance(y1, y1p)
    precision = d * 1.01
    
    # compute a bound that we want on the visibility
    warnings.warn('Remember to make a parameter of the test case whether we want'
                  ' to preserve a minimum visibility ratio.')
    should_preserve = 'dcl' in id_tc
    if should_preserve:
        action = dds.plan2action(testcase.true_plan)
        min_visibility = action.get_diffeo2d_forward().get_visibility() / 1.01
    else:
        min_visibility = 0
    
    logger.info('Using model and algo metric, the reachable distance is %s. '
                'Treshold: %s' % (d, precision))
    
    
    # TODO: add computation time
    t0 = time.clock()
    # Run the planning
#    try:
    planning_result = algo.plan(y0, y1, precision=precision,
                                min_visibility=min_visibility)
#    except:
#        try:
#            logger.info(algo.start_tree.memoize_cache.summary())
#            logger.info(algo.goal_tree.memoize_cache.summary())
#            logger.info(algo.connector.memoize_cache.summary())
#        except:
#            pass
#        raise
    if hasattr(algo, 'start_tree'):
        cache_stats = {
         'start_tree': algo.start_tree.get_cache_stats(),
         'goal_tree': algo.goal_tree.get_cache_stats(),
         'connector': algo.connector.get_cache_stats(),
        }
        algo.start_tree.clear_cache()
        algo.goal_tree.clear_cache()
        algo.connector.clear_cache()
    else:
        cache_stats = {}
    
    
    
    plan_time = time.clock() - t0
    
    results = {}
    results['id_tc'] = id_tc
    results['tc'] = testcase
    results['id_discdds'] = testcase.id_discdds
    results['id_algo'] = id_algo
    results['algo'] = algo
    results['result'] = planning_result
    results['plan_time'] = plan_time
    results['cache_stats'] = cache_stats
    
    return results
Example #31
0
def report_statistics_all(id_sub, stats, perc=10, W=0.2):
    records = stats['records']

    r = Report('statsall-%s' % id_sub)
    r.data('records', records)
    f = r.figure()

    id_distances = sorted(set(records['id_distance']))

    logger.info('%s: %s %s reo %s' %
                (id_sub, len(stats), id_distances, len(records)))

    colors = list(islice(cycle(['r', 'g', 'b', 'k', 'y', 'm']), 50))

    with f.plot('distance_order', **dp_predstats_fig) as pylab:
        ax = pylab.subplot(111)
        for i, id_d in enumerate(id_distances):
            which = records['id_distance'] == id_d
            delta = records[which]['delta']
            distance = records[which]['distance']
            order = scale_score(distance)
            order = order / float(order.size)

            step = float(i) / (max(len(id_distances) - 1, 1))
            xstep = W * 2 * (step - 0.5)
            fancy_error_display(ax,
                                delta + xstep,
                                order,
                                colors[i],
                                perc=perc,
                                label=id_d)

        ieee_spines(pylab)
        ticks = sorted(list(set(list(delta))))
        pylab.xlabel('plan length')
        pylab.ylabel('normalized distance')
        pylab.xticks(ticks, ticks)
        pylab.yticks((0, 1), (0, 1))
        pylab.axis((0.5, 0.5 + np.max(delta), -0.024, 1.2))
        legend_put_below(ax)

    with f.plot('distance', **dp_predstats_fig) as pylab:
        ax = pylab.subplot(111)
        for i, id_d in enumerate(id_distances):
            which = records['id_distance'] == id_d
            delta = records[which]['delta']
            distance = records[which]['distance']

            step = float(i) / max(len(id_distances) - 1, 1)
            xstep = W * 2 * (step - 0.5)
            fancy_error_display(ax,
                                delta + xstep,
                                distance,
                                colors[i],
                                perc=perc,
                                label=id_d)

        ieee_spines(pylab)
        ticks = sorted(list(set(list(delta))))
        pylab.xlabel('plan length')
        pylab.ylabel('distance')
        pylab.xticks(ticks, ticks)
        #        pylab.yticks((0, 1), (0, 1))
        a = pylab.axis()
        pylab.axis((0.5, 0.5 + np.max(delta), -0.024, a[3]))
        legend_put_below(ax)

    return r