Пример #1
0
    def test_is_valley_free_false(self):
        peerup = [0, 5, 6, 4]  # DPU
        peerpeer = [0, 5, 6, 5, 1]  # UPPD
        downpeer = [4, 6, 5, 0]  # DPD
        downup = [4, 5, 4]  # DU

        self.assertFalse(vft.is_valley_free(self.sample_graph, peerup))
        self.assertFalse(vft.is_valley_free(self.sample_graph, peerpeer))
        self.assertFalse(vft.is_valley_free(self.sample_graph, downpeer))
        self.assertFalse(vft.is_valley_free(self.sample_graph, downup))
Пример #2
0
    def test_shortest_vf_route(self):
        s, t = 0, 1  # shortest vf: N0 - N5 - N1
        shortest_vf = [0, 5, 1]
        sh = vft.get_shortest_vf_route(self.sample_graph, s, t)
        self.assertEqual(sh, shortest_vf)

        s, t = 0, 2
        shortest_vf = [0, 5, 6, 2]
        sh = vft.get_shortest_vf_route(self.sample_graph, s, t)
        self.assertEqual(sh, shortest_vf)
Пример #3
0
    def test_edge_dir_closeness_with_name(self):
        dir_u = vft.edge_dir(self.sample_graph, ('N5', 'N4'),
                             vfmode=vft.CLOSENESS)
        dir_d = vft.edge_dir(self.sample_graph, ('N6', 'N2'),
                             vfmode=vft.CLOSENESS)
        dir_p = vft.edge_dir(self.sample_graph, ('N5', 'N6'),
                             vfmode=vft.CLOSENESS)

        self.assertEqual(dir_u, vf_tools.LinkDir.U)
        self.assertEqual(dir_d, vf_tools.LinkDir.D)
        self.assertEqual(dir_p, vf_tools.LinkDir.P)
Пример #4
0
    def test_edge_dir_prelabeled_with_name(self):
        dir_u = vft.edge_dir(self.sample_graph, ('N5', 'N4'),
                             vfmode=vft.PRELABELED)
        dir_d = vft.edge_dir(self.sample_graph, ('N6', 'N2'),
                             vfmode=vft.PRELABELED)
        dir_p = vft.edge_dir(self.sample_graph, ('N5', 'N6'),
                             vfmode=vft.PRELABELED)

        self.assertEqual(dir_u, vf_tools.LinkDir.U)
        self.assertEqual(dir_d, vf_tools.LinkDir.D)
        self.assertEqual(dir_p, vf_tools.LinkDir.P)
Пример #5
0
    def test_edge_dir_rank_with_name(self):
        dir_u = vft.edge_dir(self.sample_graph, ('N5', 'N4'),
                             vfmode=vft.DEGREE)
        dir_d = vft.edge_dir(self.sample_graph, ('N6', 'N2'),
                             vfmode=vft.DEGREE)
        dir_p = vft.edge_dir(self.sample_graph, ('N5', 'N6'),
                             vfmode=vft.DEGREE)

        self.assertEqual(dir_u, vf_tools.LinkDir.U)
        self.assertEqual(dir_d, vf_tools.LinkDir.D)
        self.assertEqual(dir_p, vf_tools.LinkDir.P)
Пример #6
0
    def test_trace_conversion_error(self):
        fake_trace = [
            ['FAKE', 'FAKE1'],
        ]
        fake_trace_id = [
            [2, 4, 55],
        ]
        with self.assertRaises(ValueError):
            _ = vft.trace_in_vertex_id(self.sample_graph, fake_trace)

        with self.assertRaises(IndexError):
            _ = vft.trace_in_vertex_name(self.sample_graph, fake_trace_id)
Пример #7
0
def ba_generator(ba_graph, sh_paths, stretch, vf_g, progressbar=False):
    vf_count = 0
    trace_count = 0
    lp_count = 0
    progress = progressbar1.DummyProgressBar(end=10, width=15)

    if progressbar:
        progress = progressbar1.AnimatedProgressBar(end=len(sh_paths),
                                                    width=15)
    for (s, t), shl in sh_paths:
        progress += 1
        progress.show_progress()
        logger.debug('SH from %s to %s is %d' % (s, t, shl))
        random_route = helpers.random_route_walk(ba_graph, s, t, shl + stretch)
        logger.debug('Random route: %s' % random_route)
        real_stretch = len(random_route) - shl

        if real_stretch != stretch:
            continue

        trace_count += 1

        is_vf = vft.is_valley_free(ba_graph,
                                   random_route,
                                   vfmode=vft.CLOSENESS)
        logger.debug(
            'Trace edge dir: %s' %
            vft.trace_to_string(ba_graph, random_route, vfmode=vft.CLOSENESS))
        logger.debug('Is VF: %s' % is_vf)
        if is_vf:
            is_lp = vft.is_local_preferenced(ba_graph,
                                             random_route,
                                             first_edge=True,
                                             vfmode=vft.CLOSENESS,
                                             vf_g=vf_g)
        else:
            is_lp = 0
        logger.debug('Is LP: %s' % is_lp)

        vf_count += int(is_vf)
        lp_count += int(is_lp)

    logger.info('Stretch %d' % stretch)
    logger.info('Trace count: %d' % trace_count)
    logger.info('VF count: %d' % vf_count)
    logger.info('LP count: %d' % lp_count)

    return (stretch, trace_count, vf_count, lp_count)
Пример #8
0
def ba_calc(arguments):
    ba_graph = helpers.load_network(arguments.ba_graph)
    sh_paths = helpers.load_from_json(arguments.point_pairs)
    out = arguments.out

    min_stretch = arguments.min_stretch
    max_stretch = arguments.max_stretch

    max_c = len(sh_paths)
    arguments.lb = arguments.lb if 0 <= arguments.lb <= max_c else 0
    arguments.ub = arguments.ub if 0 <= arguments.ub <= max_c else max_c

    arguments.lb, arguments.ub = (min(arguments.lb, arguments.ub),
                                  max(arguments.lb, arguments.ub))

    sh_paths = sh_paths[arguments.lb:arguments.ub]
    vf_g_closeness = vft.convert_to_vf(ba_graph, vfmode=vft.CLOSENESS)

    results = [[] for x in xrange(min_stretch, max_stretch + 1)]

    for stretch in xrange(min_stretch, max_stretch + 1):
        logger.info('Calculate results with stretch %d' % stretch)
        result = ba_generator(ba_graph, sh_paths, stretch, vf_g_closeness,
                              arguments.progressbar)
        results[stretch - min_stretch] = result

    helpers.save_to_json(out, results)
Пример #9
0
def main():
    formatter = argparse.ArgumentDefaultsHelpFormatter
    parser = argparse.ArgumentParser(description=('Syntetic route generator'),
                                     formatter_class=formatter)

    parser.add_argument('--progressbar', action='store_true')
    parser.add_argument('--verbose', '-v', action='count', default=0)

    # for paralelization
    parser.add_argument('--lower-bound', '-lb', type=int, default=0, dest='lb')
    parser.add_argument('--upper-bound',
                        '-ub',
                        type=int,
                        default=-1,
                        dest='ub')

    parser.add_argument('network')
    parser.add_argument('meta')
    parser.add_argument('out')

    subparsers = parser.add_subparsers(help=('Sub commands to switch '
                                             'between different functions'))

    trace_dir_help = 'Analyze original route direction decisions'
    trace_dir_arg = subparsers.add_parser('trace-dir', help=trace_dir_help)
    trace_dir_arg.set_defaults(dispatch=trace_dir)

    upwalker_help = 'How many up step required unnecessary?'
    upwalker_arg = subparsers.add_parser('upwalker', help=upwalker_help)
    upwalker_arg.add_argument('--mode',
                              default='count',
                              choices=['count', 'deepness'])
    upwalker_arg.set_defaults(dispatch=upwalker)

    arguments = parser.parse_args()

    arguments.verbose = min(len(helpers.LEVELS), arguments.verbose)
    logging.getLogger('compnet').setLevel(helpers.LEVELS[arguments.verbose])

    g = helpers.load_network(arguments.network)
    g = g.simplify()

    meta = helpers.load_from_json(arguments.meta)
    meta = [
        m for m in meta
        if m[helpers.IS_VF_CLOSENESS] == 1 and len(m[helpers.TRACE]) > 1
        and m[helpers.TRACE][0] != m[helpers.TRACE][-1]
    ]

    arguments.lb = arguments.lb if 0 <= arguments.lb <= len(meta) else 0
    arguments.ub = arguments.ub if 0 <= arguments.ub <= len(meta) else len(
        meta)
    meta = meta[arguments.lb:arguments.ub]

    vf_g = vft.convert_to_vf(g, vfmode=vft.CLOSENESS)

    arguments.dispatch(g, meta, vf_g, arguments)
Пример #10
0
    def test_trace_clean(self):
        traceroutes = [['N0', 'N5', 'N4', 'N6'], ['NFAKE', 'N4', 'N5'],
                       ['N5', 'N5', 'N4', 'N5'], ['N5', 'N2', 'N4']]

        cleaned_traces, ignored = vft.trace_clean(self.sample_graph,
                                                  traceroutes)
        self.assertEqual(len(cleaned_traces), 3)
        self.assertEqual(ignored, 1)
        self.assertIn(['N0', 'N5', 'N4', 'N6'], cleaned_traces)
        self.assertIn(['N5', 'N5', 'N4', 'N5'], cleaned_traces)
        self.assertIn(['N5', 'N2', 'N4'], cleaned_traces)
Пример #11
0
    def test_convert_to_vf(self):
        vfg = vft.convert_to_vf(self.sample_graph)
        self.assertEqual(vfg.ecount(), 22)
        self.assertEqual(vfg.vcount(), 16)

        real_traces = [
            ['N0', 'N5', 'EN5', 'EN1'],
            ['N1', 'N5', 'N4', 'EN4', 'EN6', 'EN3'],  # SELF hop
            ['N5', 'EN6', 'EN2']
        ]  # PEER edge
        fake_traces = [
            ['EN4', 'EN6', 'EN6', 'EN5', 'EN4'],  # DPU
            ['N5', 'EN6', 'N5'],  # PP
            ['EN4', 'EN6', 'N5'],  # DP
            ['N0', 'N5', 'EN5', 'EN4']
        ]  # if CP and PC edge
        # not handled properly

        self.assertFalse(all([vft.trace_exists(vfg, x) for x in fake_traces]))
        self.assertTrue(all([vft.trace_exists(vfg, x) for x in real_traces]))
Пример #12
0
 def test_edge_dir_error(self):
     up = self.sample_graph.es[self.sample_graph.get_eid(4, 5)]
     error_msg = "igraph's edge type is not supported"
     with self.assertRaises(AttributeError, msg=error_msg):
         vft.edge_dir(self.sample_graph, up, vfmode=vft.PRELABELED)
     with self.assertRaises(AttributeError, msg=error_msg):
         vft.edge_dir(self.sample_graph, up, vfmode=vft.CLOSENESS)
     with self.assertRaises(AttributeError, msg=error_msg):
         vft.edge_dir(self.sample_graph, up, vfmode=vft.DEGREE)
Пример #13
0
def vf_attributes(g, trace, vfmode, get_lp_soft, get_lp_hard, vf_g=None):
    is_vf = int(vft.is_valley_free(g, trace, vfmode))
    is_lp_soft = -1
    is_lp_hard = -1
    if is_vf:
        if get_lp_soft:
            lp_soft = vft.is_local_preferenced(g, trace,
                                               vf_g=vf_g,
                                               first_edge=True,
                                               vfmode=vfmode)
            is_lp_soft = int(lp_soft)
        else:
            is_lp_prelabeled_soft = -1

        if get_lp_hard:
            lp_hard = vft.is_local_preferenced(g, trace,
                                               vf_g=vf_g,
                                               first_edge=False,
                                               vfmode=vfmode)
            is_lp_hard = int(lp_hard)
        else:
            is_lp_hard = -1

    return (is_vf, is_lp_soft, is_lp_hard)
Пример #14
0
    def test_all_shortest_vf_route(self):
        self.sample_graph.add_vertex('N8')
        self.sample_graph.add_edges([['N8', 'N5'], ['N6', 'N8']])
        self.prelabeled[8] = self.prelabeled[4]
        self.sample_graph.delete_edges([
            self.sample_graph.get_eid(5, 6),
        ])

        s, t = 0, 2
        shortest_vf1 = [0, 5, 4, 6, 2]
        shortest_vf2 = [0, 5, 8, 6, 2]
        sh = vft.get_shortest_vf_route(self.sample_graph, s, t, _all=True)
        self.assertEqual(len(sh), 2)
        self.assertIn(shortest_vf1, sh)
        self.assertIn(shortest_vf2, sh)
Пример #15
0
    def test_all_shortest_lp_route(self):
        self.sample_graph.add_vertices(['N8', 'N9'])
        self.sample_graph.add_edges([['N8', 'N5'], ['N2', 'N8'], ['N9', 'N5'],
                                     ['N2', 'N9']])
        self.prelabeled[8] = 0.5
        self.prelabeled[9] = 0.5

        s, t = 1, 2
        shortest_lp = [1, 5, 8, 2]
        shortest_lp2 = [1, 5, 9, 2]
        sh = vft.get_shortest_vf_route(self.sample_graph,
                                       s,
                                       t,
                                       _all=True,
                                       mode='lp')
        self.assertEqual(len(sh), 2)
        self.assertIn(shortest_lp, sh)
        self.assertIn(shortest_lp2, sh)
Пример #16
0
    def test_lp_choose_peer_if_no_customer_cone(self):
        self.sample_graph.add_vertices(['N8', 'N9'])
        self.prelabeled[8] = self.prelabeled[7]
        self.prelabeled[9] = self.prelabeled[7] * 2
        self.closenesses[8] = self.closenesses[7]
        self.closenesses[9] = self.closenesses[7] * 2

        self.sample_graph.add_edges([['N9', 'N7'], ['N8', 'N7'], ['N6', 'N8'],
                                     ['N9', 'N3']])
        s = self.sample_graph.vs.find('N7').index
        t = self.sample_graph.vs.find('N3').index
        sh = vft.get_shortest_vf_route(self.sample_graph,
                                       s,
                                       t,
                                       _all=True,
                                       mode='lp')
        self.assertEqual(len(sh), 1)
        self.assertEqual(sh[0], [7, 8, 6, 3])
Пример #17
0
    def test_is_valley_free_true(self):
        simple_vf = [0, 5, 4, 6, 2]  # UUDD - vf
        middle_peer_vf = [0, 5, 6, 2]  # UPD - vf
        just_down = [4, 6, 3]  # DD - vf
        just_up = [1, 5, 4]  # UU - vf
        start_peer = [5, 6, 3]  # PD - vf
        just_peer = [5, 6]  # P - vf

        self.assertTrue(vft.is_valley_free(self.sample_graph, simple_vf))
        self.assertTrue(vft.is_valley_free(self.sample_graph, middle_peer_vf))
        self.assertTrue(vft.is_valley_free(self.sample_graph, just_down))
        self.assertTrue(vft.is_valley_free(self.sample_graph, just_up))
        self.assertTrue(vft.is_valley_free(self.sample_graph, start_peer))
        self.assertTrue(vft.is_valley_free(self.sample_graph, just_peer))
Пример #18
0
def random_nonvf_route(g, s, t, hop_count,
                       path=None, vfmode=None):
    if path is None:
        try:
            if isinstance(s, str):
                s = g.vs.find(s).index
            if isinstance(t, str):
                t = g.vs.find(t).index

            # check if s and t are valid inidecies
            _, _ = g.vs[s], g.vs[t]
        except (ValueError, IndexError):
            raise IndexError('Vertex index out of range or not exists')

        # some special case
        if hop_count < 1:
            # print 'HOP COUNT: %d' % hop_count
            return (False, [])

        if s == t: return (True, [s, ])
        # if s != t then length must be larger than 1
        if hop_count == 1:
            # print 'S: %s, T: %s, HC: %d' % (s, t, hop_count)
            return (False, [])

        shortest_route = g.shortest_paths(s, t, mode=i.OUT)[0][0] + 1
        if hop_count < shortest_route:
            # print 'TOO SHORT %d' % (hop_count)
            return (False, [])

        path = [s, ]
        hop_count -= 1
        if vfmode is None: vfmode = vft.CLOSENESS

    if s == t:
        return (vft.is_valley_free(g, path, vfmode=vfmode), path)

    logger.debug('Hop count remained: %d' % hop_count)
    logger.debug('Current node: %s' % s)
    neighbors = [x for x in g.neighbors(s, mode=i.OUT) if x not in path]
    distances = [x[0] for x in g.shortest_paths(neighbors, t, mode=i.OUT)]
    candidates = filter(lambda x: x[1] + 1 <= hop_count,  # +, mert az igraph
                                                          # azt mondja meg,
                                                          # s-bol hany hopp t
                        zip(neighbors, distances))

    weights = [-1 if not vft.is_valley_free(g, path + [x[0], ], vfmode=vfmode)
               else vft.edge_dir(g, [s, x[0]], vfmode=vfmode).value
               for x in candidates]
    # create a list where every columnt is neighbors, distances, weights
    # respectevly
    candidates = zip(*(zip(*candidates) + [weights, ]))
    # sort by weights
    candidates = sorted(candidates, key=itemgetter(2))

    if len(candidates) == 0:
        return (False, [])

    logger.debug('Valid candidates: %s' % candidates)
    first_route = (False, [])  # by default there was no route to T
    for next_hop in candidates:
        logger.debug('Chosen one: %s' % next_hop[0])
        isvf, r = random_nonvf_route(g, next_hop[0], t,
                                     hop_count - 1,
                                     path + [next_hop[0], ], vfmode)
        if len(r) == 0: continue
        if not isvf:
            # we are done, we found a nonVF route
            return (isvf, r)
        # our first guess a vf route. save it for later use (e.g. there is no
        # nonVF rotue) but lets try again with another candidate
        if len(first_route[1]) == 0:  # first save
            first_route = (isvf, r)

    return first_route
Пример #19
0
def random_route_walk(g, s, t, limit, named=False, weight_field=None):
    try:
        s = vft.node_to_nodeid(g, s)
        t = vft.node_to_nodeid(g, t)

        # check if s and t are valid inidecies
        _, _ = g.vs[s], g.vs[t]
    except (ValueError, IndexError):
        raise IndexError('Vertex index out of range or not exists')

    # some special case
    if limit <= 0:
        # print 'HOP COUNT: %d' % hop_count
        return []
    if s == t: return [s, ]
    # # if s != t then length must be larger than 1
    # # but only in hop count mode
    if weight_field is None and limit == 1:
        # print 'S: %s, T: %s, HC: %d' % (s, t, hop_count)
        return []

    shortest_route = g.shortest_paths(s, t, weights=weight_field, mode=i.OUT)
    shortest_route = shortest_route[0][0]

    logger.debug('Shortes: %f, Limit: %f' % (shortest_route, limit))

    if weight_field is None:
        # in hop count mode convert hop count to node count
        shortest_route += 1

    if limit < shortest_route:
        # print 'TOO SHORT %d' % (hop_count)
        return []

    def edge_weight(s, t):
        eid = g.get_eid(s, t)
        if weight_field is None:
            # +1 ha nincs suly, mert az igraph azt mondja meg,
            # s-bol hany hopp t
            w = 1
        else:
            w = g.es[eid][weight_field]
        return w

    path = [s, ]
    weights = [0, ]
    visited_node_list = [[], ]
    actual_node = s

    # In hop count mode decrement, becase s already added to the path
    if weight_field is None: limit -= 1

    while limit > 0 and actual_node != t:
        logger.debug('Limit remained: %d' % limit)
        logger.debug('Current node: %s' % actual_node)
        visited_nodes = visited_node_list[-1]
        last_weight = weights[-1]
        logger.debug('Visited nodes: %s' % visited_nodes)
        neighbors = [x for x in g.neighbors(actual_node, mode=i.OUT)
                     if x not in visited_nodes and x not in path]

        neighbor_distances = [edge_weight(actual_node, x) for x in neighbors]
        next_hop_distances = [x[0] for x in g.shortest_paths(neighbors, t,
                                                             weights=weight_field,
                                                             mode=i.OUT)]
        distances = [x[0] + x[1] for x in zip(neighbor_distances,
                                              next_hop_distances)]

        candidates = filter(lambda x: x[1] <= limit,
                            zip(neighbors, distances))

        if len(candidates) == 0:
            limit += last_weight
            path.pop()
            visited_node_list.pop()
            weights.pop()
            actual_node = path[-1]
            continue

        logger.debug('Valid candidates: %s' % candidates)

        next_hop = random.choice(candidates)[0]
        logger.debug('Chosen one: %s' % next_hop)
        visited_nodes.append(next_hop)
        path.append(next_hop)
        visited_node_list.append([])
        last_weight = edge_weight(actual_node, next_hop)
        weights.append(last_weight)

        limit -= last_weight
        actual_node = next_hop

    if named:
        path = [g.vs[x]['name'] for x in path]

    logger.debug('random path between {s} and {t}: {p}'.format(s=s,
                                                               t=t,
                                                               p=path))
    return path
Пример #20
0
def purify(g,
           meta_original,
           out,
           count=1000,
           try_per_race=1,
           show_progress=False,
           with_lp=True):

    empty = 0
    # remove traces with already calculated random paths
    logger.warn('[r]ONLY NOT FILLED PATHS[/]')
    meta_filled = [
        x for x in meta_original if helpers.RANDOM_WALK_RUN_COUNT not in x
    ]

    # Filter if interested only in routes of stretch 1
    # meta_filled = [x for x in meta_original
    #                if x[helpers.TRACE_LEN]-x[helpers.SH_LEN] == 1]

    ## traces with a maximum stretch
    # logger.warn('[r]!!!ONLY WITH STRETCH[/]')
    # meta = [x for x in meta if x[helpers.STRETCH] > -1]

    # # shorter meta records
    # logger.warn('[r]!!!ONLY SHORT TRACES[/]')
    # meta = [x for x in meta if len(x[helpers.TRACE]) < 5]

    # meta_map = {tuple(x[helpers.TRACE]): x for x in meta_filled}

    logger.info('All trace count: %d' % len(meta_filled))
    tr_count = min(len(meta_filled), count)
    meta_random = random.sample(meta_filled, tr_count)
    logger.info('Chosen subset count: %d' % len(meta_random))

    # real_vf_degree = [x for x in meta_random if x[helpers.IS_VF_DEGREE] == 1]
    # real_nonvf_degree = [x for x in meta_random if x[helpers.IS_VF_DEGREE] == 0]
    # assert len(real_nonvf_degree) == tr_count - len(real_vf_degree)

    # real_vf_prelabeled = [x for x in meta_random if x[helpers.IS_VF_PRELABELED] == 1]
    # real_nonvf_prelabeled = [x for x in meta_random if x[helpers.IS_VF_PRELABELED] == 0]
    # assert len(real_nonvf_prelabeled) == tr_count - len(real_vf_prelabeled)

    # real_vf_closeness = [x for x in meta_random if x[helpers.IS_VF_CLOSENESS] == 1]
    # real_nonvf_closeness = [x for x in meta_random if x[helpers.IS_VF_CLOSENESS] == 0]
    # assert len(real_nonvf_closeness) == tr_count - len(real_vf_closeness)

    # logger.info('Real vf degree: %f[%d]' % ((len(real_vf_degree) / float(tr_count),
    #                                  len(real_vf_degree))))
    # logger.info('Real nonvf degree: %f[%d]' % ((len(real_nonvf_degree) / float(tr_count),
    #                                     len(real_nonvf_degree))))

    # logger.info('Real vf prelabeled: %f[%d]' % ((len(real_vf_prelabeled) / float(tr_count),
    #                                  len(real_vf_prelabeled))))
    # logger.info('Real nonvf prelabeled: %f[%d]' % ((len(real_nonvf_prelabeled) / float(tr_count),
    #                                     len(real_nonvf_prelabeled))))
    # logger.info('Real vf closeness: %f[%d]' % ((len(real_vf_closeness)/float(tr_count), len(real_vf_closeness))))
    # logger.info('Real nonvf closeness: %f[%d]' % ((len(real_nonvf_closeness)/float(tr_count), len(real_nonvf_closeness))))

    # traceroutes = [x[helpers.TRACE] for x in meta_random]
    # traceroutes = vft.trace_in_vertex_id(g, traceroutes)

    try:
        meta_random[0][helpers.TRACE]
    except Exception:
        meta_random = [{helpers.TRACE: x} for x in meta_random]

    progress = progressbar1.DummyProgressBar(end=10, width=15)
    if show_progress:
        progress = progressbar1.AnimatedProgressBar(end=len(meta_random),
                                                    width=15)

    stretch_list = []
    max_stretch = max(
        [x[helpers.TRACE_LEN] - x[helpers.SH_LEN] for x in meta_random])
    for stretch in range(0, max_stretch + 1):
        metas = [
            x for x in meta_random
            if x[helpers.TRACE_LEN] - x[helpers.SH_LEN] == stretch
        ]
        stretch_list.extend(list(repeat(stretch, len(metas))))

    # print(stretch_list)
    lenghts = random.shuffle(stretch_list)

    strx_array = []

    for idx, trace_meta in enumerate(meta_random):
        progress += 1
        progress.show_progress()
        # print(trace_meta[helpers.TRACE])
        shl = trace_meta[helpers.SH_LEN]
        trace = vft.trace_in_vertex_id(g, [
            trace_meta[helpers.TRACE],
        ])
        if len(trace) != 1:
            print 'PROBLEM'
            print trace_meta
            continue
        trace = trace[0]
        # print(trace)
        random_walk_closeness_route_vf = 0
        random_walk_closeness_route_lp_soft = 0
        random_walk_closeness_route_lp_hard = 0
        random_walk_degree_route_vf = 0
        random_walk_degree_route_lp_soft = 0
        random_walk_degree_route_lp_hard = 0
        random_walk_prelabeled_route_vf = 0
        random_walk_prelabeled_route_lp_soft = 0
        random_walk_prelabeled_route_lp_hard = 0

        s, t = trace[0], trace[-1]
        for counter in xrange(0, try_per_race):
            # random_path = helpers.random_route_walk(g, s, t, len(trace)) # Modified
            random_path = helpers.random_route_walk(
                g, s, t, shl + stretch_list[idx])  # Modified
            if len(random_path) == 0:
                empty += 1
            if vft.is_valley_free(g, random_path, vfmode=vft.CLOSENESS):
                random_walk_closeness_route_vf += 1
                if (len(random_path) == shl + 1):
                    strx_array.append(1)
                if with_lp:
                    lp_soft = vft.is_local_preferenced(g,
                                                       random_path,
                                                       first_edge=True,
                                                       vfmode=vft.CLOSENESS)
                    lp_hard = vft.is_local_preferenced(g,
                                                       random_path,
                                                       first_edge=False,
                                                       vfmode=vft.CLOSENESS)
                    if lp_soft:
                        random_walk_closeness_route_lp_soft += 1
                    if lp_hard:
                        random_walk_closeness_route_lp_hard += 1
            else:
                if (len(random_path) == shl + 1):
                    strx_array.append(0)

            # if vft.is_valley_free(g, random_path, vfmode=vft.DEGREE):
            #     random_walk_degree_route_vf += 1
            #     if with_lp:
            #         lp_soft = vft.is_local_preferenced(g, random_path,
            #                                            first_edge=True,
            #                                            vfmode=vft.DEGREE)
            #         lp_hard = vft.is_local_preferenced(g, random_path,
            #                                            first_edge=False,
            #                                            vfmode=vft.DEGREE)
            #         if lp_soft:
            #             random_walk_degree_route_lp_soft += 1
            #         if lp_hard:
            #             random_walk_degree_route_lp_hard += 1

            # if vft.is_valley_free(g, random_path, vfmode=vft.PRELABELED):
            #     random_walk_prelabeled_route_vf += 1
            #     if with_lp:
            #         lp_soft = vft.is_local_preferenced(g, random_path,
            #                                            first_edge=True,
            #                                            vfmode=vft.PRELABELED)
            #         lp_hard = vft.is_local_preferenced(g, random_path,
            #                                            first_edge=False,
            #                                            vfmode=vft.PRELABELED)
            #         if lp_soft:
            #             random_walk_prelabeled_route_lp_soft += 1
            #         if lp_hard:
            #             random_walk_prelabeled_route_lp_hard += 1

            # sanity check


#             if random_path[0] != s or random_path[-1] != t:
#                 logger.error('ALERT')

            if len(random_path) != len(set(random_path)):
                logger.error('LENGTH ERROR')

        extra_meta = {
            helpers.RANDOM_WALK_RUN_COUNT:
            try_per_race,
            helpers.RANDOM_WALK_VF_CLOSENESS_ROUTE:
            random_walk_closeness_route_vf,
            helpers.RANDOM_WALK_VF_DEGREE_ROUTE:
            random_walk_degree_route_vf,
            helpers.RANDOM_WALK_VF_PRELABELED_ROUTE:
            random_walk_prelabeled_route_vf,
        }
        if with_lp:
            extra_meta.update({
                helpers.RANDOM_WALK_LP_SOFT_CLOSENESS_ROUTE:
                random_walk_closeness_route_lp_soft,
                helpers.RANDOM_WALK_LP_HARD_CLOSENESS_ROUTE:
                random_walk_closeness_route_lp_hard,
                helpers.RANDOM_WALK_LP_SOFT_DEGREE_ROUTE:
                random_walk_degree_route_lp_soft,
                helpers.RANDOM_WALK_LP_HARD_DEGREE_ROUTE:
                random_walk_degree_route_lp_hard,
                helpers.RANDOM_WALK_LP_SOFT_PRELABELED_ROUTE:
                random_walk_prelabeled_route_lp_soft,
                helpers.RANDOM_WALK_LP_HARD_PRELABELED_ROUTE:
                random_walk_prelabeled_route_lp_hard
            })

        trace_meta.update(extra_meta)

    ## save modified meta
    # all meta_* get only references from meta_original
    helpers.save_to_json(out, meta_random)
    # meta_mod = [x for x in meta_map.itervalues()]
    # helpers.save_to_json(out, meta_mod)

    # calculate results
    # real_vf = [x[helpers.IS_VF_CLOSENESS] for x in meta_random]
    # real_vf_ratio = np.mean(real_vf)

    random_walk_vf_ratio_per_element = [
        x[helpers.RANDOM_WALK_VF_CLOSENESS_ROUTE] /
        x[helpers.RANDOM_WALK_RUN_COUNT] for x in meta_random
    ]
    random_walk_vf_ratio = np.mean(random_walk_vf_ratio_per_element)
    # print results
    logger.info('')
    logger.info('Empty: %d' % empty)
    logger.info('Tested trace count: %d' % len(meta_random))
    # logger.info('VF ratio in tested traces: %f' % real_vf_ratio)
    logger.info('VF ratio in random walks: %f' % random_walk_vf_ratio)
    logger.info('VF ratio in random walks for path stretch 1: %f' %
                np.mean(strx_array))
Пример #21
0
def purify(g, traceroutes, flags, show_progress=False):
    results = list()

    # remove traces with unknown nodes
    traceroutes = vft.trace_in_vertex_id(g, traceroutes)

    # generate valley-free graph
    if flags[FLAG_PRELABELED]:
        logger.info('Generate VF_G_PRE')
        vf_g_pre = vft.convert_to_vf(g, vfmode=vft.PRELABELED)
    else:
        logger.info('Skip prelabeled graph')
    if flags[FLAG_DEGREE]:
        logger.info('Generate VF_G_DEGREE')
        vf_g_degree = vft.convert_to_vf(g, vfmode=vft.DEGREE)
    else:
        logger.info('Skip degree graph')
    if flags[FLAG_CLOSENESS]:
        logger.info('Generate VF_G_CLOSENESS')
        vf_g_closeness = vft.convert_to_vf(g, vfmode=vft.CLOSENESS)
    else:
        logger.info('Skip closeness graph')

    progress = progressbar1.DummyProgressBar(end=10, width=15)
    if show_progress:
        progress = progressbar1.AnimatedProgressBar(end=len(traceroutes),
                                                    width=15)
    for trace in traceroutes:
        progress += 1
        progress.show_progress()

        logger.debug('Current trace: %s' % ([g.vs[x]['name'] for x in trace]))

        if len(trace) == 1: continue

        s, t = trace[0], trace[-1]

        is_vf_prelabeled = -1
        is_lp_prelabeled_hard = -1
        is_lp_prelabeled_soft = -1

        is_vf_degree = -1
        is_lp_degree_hard = -1
        is_lp_degree_soft = -1

        is_vf_closeness = -1
        is_lp_closeness_hard = -1
        is_lp_closeness_soft = -1

        trace_len = len(trace)
        sh_len = g.shortest_paths(s, t, mode=i.OUT)[0][0]
        sh_len += 1  # convert hop count to node Counter

        if flags[FLAG_PRELABELED]:
            is_vf_prelabeled = vft.is_valley_free(g, trace, vft.PRELABELED)
            is_vf_prelabeled = int(is_vf_prelabeled)
            if is_vf_prelabeled:
                if flags[FLAG_LP_SOFT]:
                    lp_soft = vft.is_local_preferenced(g,
                                                       trace,
                                                       vf_g=vf_g_pre,
                                                       first_edge=True,
                                                       vfmode=vft.PRELABELED)
                    is_lp_prelabeled_soft = 1 if lp_soft else 0
                else:
                    is_lp_prelabeled_soft = -1

                if flags[FLAG_LP_HARD]:
                    lp_hard = vft.is_local_preferenced(g,
                                                       trace,
                                                       vf_g=vf_g_pre,
                                                       first_edge=False,
                                                       vfmode=vft.PRELABELED)
                    is_lp_prelabeled_hard = 1 if lp_hard else 0
                else:
                    is_lp_prelabeled_hard = -1

        if flags[FLAG_DEGREE]:
            is_vf_degree = vft.is_valley_free(g, trace, vft.DEGREE)
            is_vf_degree = int(is_vf_degree)
            if is_vf_degree:
                if flags[FLAG_LP_SOFT]:
                    lp_soft = vft.is_local_preferenced(g,
                                                       trace,
                                                       vf_g=vf_g_degree,
                                                       first_edge=True,
                                                       vfmode=vft.DEGREE)
                    is_lp_degree_soft = 1 if lp_soft else 0
                else:
                    is_lp_degree_soft = -1

                if flags[FLAG_LP_HARD]:
                    lp_hard = vft.is_local_preferenced(g,
                                                       trace,
                                                       vf_g=vf_g_degree,
                                                       first_edge=False,
                                                       vfmode=vft.DEGREE)
                    is_lp_degree_hard = 1 if lp_hard else 0
                else:
                    is_lp_degree_hard = -1

        if flags[FLAG_CLOSENESS]:
            is_vf_closeness = vft.is_valley_free(g, trace, vft.CLOSENESS)
            is_vf_closeness = int(is_vf_closeness)
            if is_vf_closeness:
                if flags[FLAG_LP_SOFT]:
                    lp_soft = vft.is_local_preferenced(g,
                                                       trace,
                                                       vf_g=vf_g_closeness,
                                                       first_edge=True,
                                                       vfmode=vft.CLOSENESS)
                    is_lp_closeness_soft = 1 if lp_soft else 0
                else:
                    is_lp_closeness_soft = -1
                if flags[FLAG_LP_HARD]:
                    lp_hard = vft.is_local_preferenced(g,
                                                       trace,
                                                       vf_g=vf_g_closeness,
                                                       first_edge=False,
                                                       vfmode=vft.CLOSENESS)
                    is_lp_closeness_hard = 1 if lp_hard else 0
                else:
                    is_lp_closeness_hard = -1

        if False:
            sh_vf_len = vft.get_shortest_vf_route(g,
                                                  s,
                                                  t,
                                                  mode='vf',
                                                  vf_g=vf_g_pre,
                                                  _all=True,
                                                  vfmode=vft.PRELABELED)
            # ugy tunik, mintha nem mindig lenne pontos? fentartassal kezelendo
            # ez az ertek azert is kerult bele, hogy ellenorizzuk
            in_vf_prediction = 1 if sh_vf_len and trace in sh_vf_len else 0
        else:
            sh_vf_len = -1
            in_vf_prediction = -1

        sh_vf_len = len(sh_vf_len[0]) if isinstance(sh_vf_len, list) else -1
        percentage_stretch = trace_len / float(sh_len)

        named_trace = [g.vs[_id]["name"] for _id in trace]

        result = {
            helpers.TRACE: named_trace,
            helpers.TRACE_LEN: trace_len,
            helpers.SH_LEN: sh_len,
            helpers.SH_VF_LEN: sh_vf_len,
            helpers.IS_VF_PRELABELED: is_vf_prelabeled,
            helpers.IS_VF_DEGREE: is_vf_degree,
            helpers.IS_VF_CLOSENESS: is_vf_closeness,
            helpers.HOP_STRETCH: trace_len - sh_len,
            helpers.PERC_STRETCH: percentage_stretch,
            helpers.IN_VF_PRED: in_vf_prediction,
            helpers.IS_LP_SOFT_PRELABELED: is_lp_prelabeled_soft,
            helpers.IS_LP_HARD_PRELABELED: is_lp_prelabeled_hard,
            helpers.IS_LP_SOFT_DEGREE: is_lp_degree_soft,
            helpers.IS_LP_HARD_DEGREE: is_lp_degree_hard,
            helpers.IS_LP_SOFT_CLOSENESS: is_lp_closeness_soft,
            helpers.IS_LP_HARD_CLOSENESS: is_lp_closeness_hard,
        }

        results.append(result)

    # print >> sys.stderr, ('TRACE\tTRACE_LEN\tSH_LEN\tSH_VF_LEN\tIS_VF',
    #                       '\tSTRETCH\tIN_VF_PREDICTION\tIS_LP_F\tIS_LP_ALL')
    # for result in results:
    #     result = [str(r) for r in result]
    #     print >> sys.stderr, '\t'.join(result)

    # statistic = statistics.purify(g, results,
    #                               'nc+ec+tc+rt+vf+vf_closeness+pred+lp_soft_prelabeled+lp_hard_prelabeled+lp_soft_degree+lp_hard_degree+lp_soft_closeness+lp_hard_closeness'.split('+'))
    # statistics.stat_printer(statistic)

    return results
Пример #22
0
def purify(labeled_g, out, network_path, extra_hop=0):
    vs = [x.index for x in labeled_g.vs]

    ## Jus like in R

    # print '================'
    # for x in orig_vs:
    #     shp = labeled_g.get_all_shortest_paths(x, orig_vs, mode=i.ALL)
    #     res = []
    #     mes = 0
    #     for p in shp:
    #         mes += 1
    #         # print [labeled_g.vs[u]['name'] for u in p]
    #         vf_indicator = 1 if vft.is_valley_free(labeled_g, p) else 0
    #         # if vf_indicator == 0:
    #             # print [labeled_g.vs[u]['name'] for u in [p[0], p[-1]]]
    #             # print [labeled_g.degree(u) for u in p]
    #             # print vft.trace_to_string(labeled_g, p)
    #         # print vf_indicator == 1
    #         res.append(vf_indicator)
    #         # raw_input()
    #     # print mes
    #     print np.mean(res)

    # raw_input()

    # print '///////////////////////////'
    pairs = random_pairs(vs, NODE_PAIRS)
    print 'Random pairs: %d' % len(pairs)

    probed_pairs = 0

    all_vf = 0
    all_nonvf = 0
    all_vf_closeness = 0
    all_nonvf_closeness = 0
    results = []
    results2 = []
    results3 = []

    short_results = list()
    short_results2 = list()
    short_results3 = list()
    all_short_vf = 0
    all_short_nonvf = 0
    all_short_vf_closeness = 0
    all_short_nonvf_closeness = 0

    long_results = list()
    long_results2 = list()
    long_results3 = list()
    all_long_vf = 0
    all_long_nonvf = 0
    all_long_vf_closeness = 0
    all_long_nonvf_closeness = 0

    results_closeness = []
    results3_closeness = []
    progress = progressbar1.AnimatedProgressBar(end=len(pairs), width=15)
    for s, t in pairs:
        progress += 1
        progress.show_progress()
        for x in range(0, labeled_g.vcount()):
            labeled_g.vs[x]['traces'] = dict()

        # all_path = labeled_g.get_all_shortest_paths(s, t, mode=i.ALL)
        sh_len = labeled_g.shortest_paths(s, t, mode=i.ALL)[0][0]
        sh_len += 1  # convert to hop count
        all_path = helpers.dfs_mark(copy.deepcopy(labeled_g), s, t,
                                    sh_len + extra_hop)
        if all_path is None or len(all_path) < 1:
            print 'No path between %s %s' % (s, t)
            continue
        probed_pairs += 1
        vf_indicator = [
            1 if vft.is_valley_free(labeled_g, x) else 0 for x in all_path
        ]
        vf_closeness_indicator = [
            1 if vft.is_valley_free(labeled_g, x, vfmode=vft.ORDER_CLOSENESS)
            else 0 for x in all_path
        ]
        vf_count = sum(vf_indicator)
        vf_closeness_count = sum(vf_closeness_indicator)
        nonvf = len(all_path) - vf_count
        nonvf_closeness = len(all_path) - vf_closeness_count

        all_vf += vf_count
        all_nonvf += nonvf

        all_vf_closeness += vf_closeness_count
        all_nonvf_closeness += nonvf_closeness

        long_path = [x for x in all_path if len(x) == sh_len + extra_hop]
        short_path = [x for x in all_path if len(x) < sh_len + extra_hop]

        tmp = [
            1 if vft.is_valley_free(labeled_g, x, vfmode=vft.ORDER_PRELABELED)
            else 0 for x in all_path if len(x) < sh_len + extra_hop
        ]
        short_vf_count = sum(tmp)
        short_nonvf = len(tmp) - short_vf_count

        tmp = [
            1 if vft.is_valley_free(labeled_g, x, vfmode=vft.ORDER_CLOSENESS)
            else 0 for x in all_path if len(x) < sh_len + extra_hop
        ]
        short_vf_closeness_count = sum(tmp)
        short_nonvf_closeness = len(tmp) - short_vf_closeness_count

        tmp = [
            1 if vft.is_valley_free(labeled_g, x, vfmode=vft.ORDER_PRELABELED)
            else 0 for x in all_path if len(x) >= sh_len + extra_hop
        ]
        long_vf_count = sum(tmp)
        long_nonvf = len(tmp) - long_vf_count

        tmp = [
            1 if vft.is_valley_free(labeled_g, x, vfmode=vft.ORDER_CLOSENESS)
            else 0 for x in all_path if len(x) >= sh_len + extra_hop
        ]
        long_vf_closeness_count = sum(tmp)
        long_nonvf_closeness = len(tmp) - long_vf_closeness_count

        if len(all_path) > 0:
            results.append(vf_count / float(len(all_path)))
            results_closeness.append(vf_closeness_count / float(len(all_path)))
        else:
            results.append(0)
            results_closeness.append(0)

        results3.append([vf_count, nonvf])
        results3_closeness.append([vf_closeness_count, nonvf_closeness])

        if len(all_path) > 1: results2.append(vf_count / float(len(all_path)))

        all_long_vf += long_vf_count
        all_long_nonvf += long_nonvf

        all_long_vf_closeness += long_vf_closeness_count
        all_long_nonvf_closeness += long_nonvf_closeness

        all_short_vf += short_vf_count
        all_short_nonvf += short_nonvf

        all_short_vf_closeness += short_vf_closeness_count
        all_short_nonvf_closeness += short_nonvf_closeness

        if len(long_path) > 0:
            long_results.append(long_vf_count / float(len(long_path)))
            long_results3.append(long_vf_closeness_count /
                                 float(len(long_path)))
        else:
            long_results.append(0)
            long_results3.append(0)

        if len(long_path) > 1:
            long_results2.append(long_vf_count / float(len(long_path)))

        if len(short_path) > 0:
            short_results.append(short_vf_count / float(len(short_path)))
            short_results3.append(short_vf_closeness_count /
                                  float(len(short_path)))
        else:
            short_results.append(0)
            short_results3.append(0)

    print

    with open(out, 'w') as f:
        f.write('%s\n' % network_path)
        f.write('Probed pairs: %d\n' % probed_pairs)
        f.write('VF count: %d\n' % all_vf)
        f.write('Non vf count: %d\n' % all_nonvf)
        f.write('VF perc: %f\n' % (all_vf / float(all_vf + all_nonvf)))
        f.write('Mean VF prob: %f\n' % np.mean(results))
        f.write('Mean VF2 prob: %f\n' % np.mean(results2))

        f.write('\n')
        f.write('VF CLOSENESS count: %d\n' % all_vf_closeness)
        f.write('Non vf CLOSENESS count: %d\n' % all_nonvf_closeness)
        f.write(
            'VF CLOSENESS perc: %f\n' %
            (all_vf_closeness / float(all_vf_closeness + all_nonvf_closeness)))
        f.write('Mean VF CLOSENESS prob: %f\n' % np.mean(results_closeness))

        f.write('\n')
        f.write('==========\n')
        f.write('VF count: %d\n' % all_short_vf)
        f.write('VF  CLOSENESS count: %d\n' % all_short_vf_closeness)
        f.write('Non vf count: %d\n' % all_short_nonvf)
        f.write('Non vf CLOSENESS count: %d\n' % all_short_nonvf_closeness)
        if all_short_vf + all_short_nonvf > 0:
            f.write('VF perc: %f\n' %
                    (all_short_vf / float(all_short_vf + all_short_nonvf)))
        if all_short_vf_closeness + all_short_nonvf_closeness > 0:
            f.write(
                'VF CLOSENESS perc: %f\n' %
                (all_short_vf_closeness /
                 float(all_short_vf_closeness + all_short_nonvf_closeness)))
        f.write('Mean VF prob: %f\n' % np.mean(short_results))
        f.write('Mean VF CLOSENESS prob: %f\n' % np.mean(short_results3))
        f.write('Mean VF2 prob: %f\n' % np.mean(short_results2))
        f.write('=-----------------\n')
        f.write('VF count: %d\n' % all_long_vf)
        f.write('VF CLOSENESS count: %d\n' % all_long_vf_closeness)
        f.write('Non vf count: %d\n' % all_long_nonvf)
        f.write('Non vf CLOSENESS count: %d\n' % all_long_nonvf_closeness)
        f.write('VF perc: %f\n' %
                (all_long_vf / float(all_long_vf + all_long_nonvf)))
        f.write('VF CLOSENESS perc: %f\n' %
                (all_long_vf_closeness /
                 float(all_long_vf_closeness + all_long_nonvf_closeness)))
        f.write('Mean VF prob: %f\n' % np.mean(long_results))
        f.write('Mean VF CLOSENESS prob: %f\n' % np.mean(long_results3))
        f.write('Mean VF2 prob: %f\n' % np.mean(long_results2))
Пример #23
0
def trace_dir(g, meta, vf_g, arguments):
    N = g.vcount()
    vf_sum_lp_w = sum([e['LPweight'] for e in vf_g.es])

    out_instead_core = 0
    core_instead_out = 0
    in_customer = 0
    in_customer_but_use_up = 0

    progress = progressbar1.DummyProgressBar(end=10, width=15)
    if arguments.progressbar:
        progress = progressbar1.AnimatedProgressBar(end=len(meta), width=15)

    for m in meta:
        progress += 1
        progress.show_progress()

        trace = m[helpers.TRACE]
        first_edge = [trace[0], trace[1]]
        first_hop_type = vft.edge_dir(g, first_edge, vfmode=vft.CLOSENESS)
        s_idx = vft.node_to_nodeid(g, trace[0])
        t_idx = vft.node_to_nodeid(g, trace[-1])

        sh_vf = vf_g.get_all_shortest_paths(s_idx + N, t_idx + N)
        if len(sh_vf) > 0:
            in_customer += 1
            if first_hop_type != LinkDir.D:
                in_customer_but_use_up += 1

        if first_hop_type == LinkDir.D:
            # 'remove' all downward path:
            for neighbor in vf_g.neighbors(s_idx + N, mode=igraph.OUT):
                down_edge = vf_g.get_eid(s_idx + N, neighbor, directed=True)
                vf_g.es[down_edge]['LPweight_old'] = vf_g.es[down_edge][
                    'LPweight']
                vf_g.es[down_edge]['LPweight'] = vf_sum_lp_w + 1

            lp_sh = vft.get_shortest_vf_route(g,
                                              s_idx,
                                              t_idx,
                                              mode='lp',
                                              vf_g=vf_g,
                                              vfmode=vft.CLOSENESS)
            pretty_plotter.pretty_plot(g, trace, lp_sh, [])
            first_new_edge = [lp_sh[0], lp_sh[1]]
            first_new_hop_type = vft.edge_dir(g,
                                              first_new_edge,
                                              vfmode=vft.CLOSENESS)
            if first_hop_type != first_new_hop_type:
                out_instead_core += 1
                print ''
                print "Original trace: %s" % trace
                print 'Original trace dir: %s' % vft.trace_to_string(g, trace)
                print 'Original closeness: %s' % [
                    g.vs.find(x)['closeness'] for x in trace
                ]
                print 'LP trace: %s' % [g.vs[x]['name'] for x in lp_sh]
                print 'LP dir: %s' % vft.trace_to_string(g, lp_sh)
                print 'LP closeness: %s' % [
                    g.vs.find(x)['closeness'] for x in lp_sh
                ]
                # raw_input()
            for neighbor in vf_g.neighbors(s_idx + N, mode=igraph.OUT):
                down_edge = vf_g.get_eid(s_idx + N, neighbor, directed=True)
                vf_g.es[down_edge]['LPweight'] = vf_g.es[down_edge][
                    'LPweight_old']

        elif first_hop_type == LinkDir.U:
            lp_sh = vft.get_shortest_vf_route(g,
                                              s_idx,
                                              t_idx,
                                              mode='lp',
                                              vf_g=vf_g,
                                              vfmode=vft.CLOSENESS)
            lp_dir = vft.trace_to_string(g, lp_sh)

            # if lp_dir[1:].startswith('U') or lp_dir[1:].startswith('D'):
            #     continue
            first_new_edge = [lp_sh[0], lp_sh[1]]
            first_new_hop_type = vft.edge_dir(g,
                                              first_new_edge,
                                              vfmode=vft.CLOSENESS)
            if first_hop_type != first_new_hop_type:
                core_instead_out += 1
                print ''
                print "Original trace: %s" % trace
                print 'Original trace dir: %s' % vft.trace_to_string(g, trace)
                print 'Original closeness: %s' % [
                    g.vs.find(x)['closeness'] for x in trace
                ]
                print 'LP trace: %s' % [g.vs[x]['name'] for x in lp_sh]
                print 'LP dir: %s' % vft.trace_to_string(g, lp_sh)
                print 'LP closeness: %s' % [
                    g.vs.find(x)['closeness'] for x in lp_sh
                ]
                pretty_plotter.pretty_plot(g, trace, lp_sh, [])
                # raw_input()

    logger.info('Core instead down: %d' % core_instead_out)
    logger.info('Down instead core: %d' % out_instead_core)
    logger.info('In customer cone: %d' % in_customer)
    logger.info('In customer but use UP: %d' % in_customer_but_use_up)
Пример #24
0
def main():
    parser = argparse.ArgumentParser(
        description=('SANDBOX mode. ', 'Write something ', 'useful here'),
        formatter_class=argparse.ArgumentDefaultsHelpFormatter)

    parser.add_argument('--progressbar', action='store_true')
    parser.add_argument('--verbose', '-v', action='count', default=0)
    parser.add_argument('--edge-drop',
                        dest='edge_drop',
                        type=float,
                        default=0.0)
    parser.add_argument('--closeness-limit',
                        dest='closeness_limit',
                        type=float,
                        default=0.0)
    parser.add_argument('network')
    parser.add_argument('traceroutes')

    arguments = parser.parse_args()

    show_progress = arguments.progressbar

    arguments.verbose = min(len(helpers.LEVELS), arguments.verbose)
    logging.getLogger('compnet').setLevel(helpers.LEVELS[arguments.verbose])

    g = helpers.load_network(arguments.network)
    traceroutes = helpers.load_from_json(arguments.traceroutes)

    logger.info('ecount: %d' % g.ecount())
    logger.info('vcount: %d' % g.vcount())
    logger.info('trace count: %d' % len(traceroutes))

    g_dummy = g.copy()
    progress = progressbar1.DummyProgressBar(end=10, width=15)
    if show_progress:
        progress = progressbar1.AnimatedProgressBar(end=len(traceroutes),
                                                    width=15)

    closeness_list = []
    for x in g_dummy.vs:
        progress += 1
        progress.show_progress()
        closeness_list.append((x.index, g_dummy.closeness(x)))

    end = int(arguments.closeness_limit * g_dummy.vcount())
    logger.debug('Top node count: %d' % end)
    top_nodes = sorted(closeness_list, key=lambda x: x[1], reverse=True)[:end]
    top_nodes_index = [x[0] for x in top_nodes]
    top_nodes_name = [g_dummy.vs[x[0]]['name'] for x in top_nodes]
    top_edges = [
        e for e in g_dummy.es
        if e.source in top_nodes_index and e.target in top_nodes_index
    ]
    logger.debug('Top edge count: %d' % len(top_edges))
    random.shuffle(top_edges)
    edge_drop = top_edges[:int(len(top_edges) * arguments.edge_drop)]
    logger.debug('Dropped edge count: %d' % len(edge_drop))
    # edges = [x.index for x in g_dummy.es]
    # random.shuffle(edges)
    # edge_drop = edges[:int(g.ecount() * arguments.edge_drop)]
    g_dummy.delete_edges(edge_drop)

    traceroutes = traceroutes[:10000]

    all_edges = []
    for trace in traceroutes:
        edges = zip(trace, trace[1:])
        edges = [tuple(sorted(e)) for e in edges]
        all_edges.extend(edges)

    all_edges = list(set(all_edges))
    top_edges = [
        e for e in all_edges
        if e[0] in top_nodes_name and e[1] in top_nodes_name
    ]
    logger.info('TOP edge count in real traceroutes: %d' % len(top_edges))

    found_top_edges = []
    increments = []
    for trace in traceroutes:
        edges = zip(trace, trace[1:])
        edges = [tuple(sorted(e)) for e in edges]
        top_edges = [
            x for x in edges
            if x[0] in top_nodes_name and x[1] in top_nodes_name
        ]
        found_top_edges.extend(top_edges)
        found_top_edges = list(set(found_top_edges))
        increments.append(len(found_top_edges))

    logger.info('Found top edge count: %d' % len(found_top_edges))

    dummy_sh_traceroutes_meta = []
    original_sh_traceroutes_meta = []
    stretches = []
    progress = progressbar1.DummyProgressBar(end=10, width=15)
    if show_progress:
        progress = progressbar1.AnimatedProgressBar(end=len(traceroutes),
                                                    width=15)
    for trace in traceroutes:
        progress += 1
        progress.show_progress()
        s, t = trace[0], trace[-1]
        # logger.debug('Get shortest paths from {s} to {t}'.format(s=s, t=t))
        sh_dummy = random.choice(g_dummy.get_shortest_paths(s, t))
        sh_original = random.choice(g.get_shortest_paths(s, t))
        stretch = len(sh_dummy) - len(sh_original)
        dummy_sh_traceroutes_meta.append((sh_dummy, stretch))
        original_sh_traceroutes_meta.append((sh_original, 0))
        stretches.append(stretch)
        # logger.debug('Stretch: %d' % stretch)
        # logger.debug('SH DUMMY: %s' % [g_dummy.vs[x]['name'] for x in sh_dummy])
        # logger.debug('SH ORIG: %s' % [g.vs[x]['name'] for x in sh_original])

    dummy_sh_meta = [(x[0], x[1],
                      vft.is_valley_free(g_dummy, x[0], vft.CLOSENESS))
                     for x in dummy_sh_traceroutes_meta]
    dummy_sh_len_hist = collections.Counter(
        [len(x[0]) for x in dummy_sh_traceroutes_meta])
    original_sh_len_hist = collections.Counter(
        [len(x[0]) for x in original_sh_traceroutes_meta])
    original_len_hist = collections.Counter([len(x) for x in traceroutes])
    stretches = [x for x in stretches if x >= 0]
    stretch_hist = collections.Counter(stretches)

    import matplotlib.pyplot as plt
    print
    print[(x, 100 * y / float(len(traceroutes)), y)
          for x, y in stretch_hist.iteritems()]
    plt.plot([x for x in stretch_hist.iterkeys()],
             [x for x in stretch_hist.itervalues()], 'g^')
    plt.ylabel('some numbers')
    # plt.show()

    logger.info('Dummy VF stat')
    max_stretch = max(dummy_sh_meta, key=lambda x: x[1])[1]
    for stretch in range(0, max_stretch + 1):
        stretched_traces = [x for x in dummy_sh_meta if x[1] == stretch]
        count = len(stretched_traces)
        vf_count = len([x for x in stretched_traces if x[2]])
        vf_perc = vf_count / float(count)
        nonvf_count = count - vf_count
        nonvf_perc = nonvf_count / float(count)
        logger.info(
            '{stretch} -- {vf_perc}[{vf_count}]\t{nonvf_perc}[{nonvf_count}]'.
            format(stretch=stretch,
                   vf_perc=vf_perc,
                   vf_count=vf_count,
                   nonvf_perc=nonvf_perc,
                   nonvf_count=nonvf_count))
    import matplotlib.pyplot as plt
    plt.plot(increments, 'g^')
    plt.ylabel('some numbers')
    plt.show()
Пример #25
0
def purify(g, meta, top_nodes, show_progress):

    progress = progressbar1.DummyProgressBar(end=10, width=15)
    if show_progress:
        progress = progressbar1.AnimatedProgressBar(end=len(meta),
                                                    width=15)

    logger.info('Purify')

    triplet_count_closeness = 0
    top_nodes_triplet_closeness = 0
    triplet_count_prelabeled = 0
    top_nodes_triplet_prelabeled = 0
    triplet_count_degree = 0
    top_nodes_triplet_degree = 0

    for row in meta:
        progress += 1
        progress.show_progress()

        trace = row[helpers.TRACE]
        trace = vft.trace_in_vertex_id(g, [trace, ])[0]

        if row[helpers.IS_VF_CLOSENESS] == 0:
            logger.debug('Closeness valley')
            vt = vft.get_valley_triplets(g, trace, vft.CLOSENESS)
            triplet_count_closeness += len(vt)
            for trip in vt:
                if any([x in top_nodes for x in trip]):
                    top_nodes_triplet_closeness += 1

        if row[helpers.IS_VF_PRELABELED] == 0:
            logger.debug('Prelabeled valley')
            vt = vft.get_valley_triplets(g, trace, vft.PRELABELED)
            triplet_count_prelabeled += len(vt)
            for trip in vt:
                if any([x in top_nodes for x in trip]):
                    top_nodes_triplet_prelabeled += 1
         
        if row[helpers.IS_VF_DEGREE] == 0:
            logger.debug('Degree valley')
            vt = vft.get_valley_triplets(g, trace, vft.DEGREE)
            triplet_count_degree += len(vt)
            for trip in vt:
                if any([x in top_nodes for x in trip]):
                    top_nodes_triplet_degree += 1

    print
    try:
        ratio_closeness = top_nodes_triplet_closeness / float(triplet_count_closeness)
    except ZeroDivisionError:
        ratio_closeness = 0.0
    try:
        ratio_prelabeled = top_nodes_triplet_prelabeled / float(triplet_count_prelabeled)
    except ZeroDivisionError:
        ratio_prelabeled = 0.0
    try:
        ratio_degree = top_nodes_triplet_degree / float(triplet_count_degree)
    except ZeroDivisionError:
        ratio_degree = 0.0
    logger.info('CLOSENESS: %6.3f[%d/%d]' % (ratio_closeness, top_nodes_triplet_closeness, triplet_count_closeness))
    logger.info('PRELABELED: %6.3f[%d/%d]' % (ratio_prelabeled, top_nodes_triplet_prelabeled, triplet_count_prelabeled))
    logger.info('DEGREE: %6.3f[%d/%d]' % (ratio_degree, top_nodes_triplet_degree, triplet_count_degree))
Пример #26
0
def purify(g, meta, flags, try_per_trace, show_progress=False):

    # generate valley-free graph
    if flags[FLAG_PRELABELED]:
        logger.info('Generate VF_G_PRE')
        vf_g_pre = vft.convert_to_vf(g, vfmode=vft.PRELABELED)
    else:
        logger.info('Skip prelabeled graph')

    if flags[FLAG_DEGREE]:
        logger.info('Generate VF_G_DEGREE')
        vf_g_degree = vft.convert_to_vf(g, vfmode=vft.DEGREE)
    else:
        logger.info('Skip degree graph')

    if flags[FLAG_CLOSENESS]:
        logger.info('Generate VF_G_CLOSENESS')
        vf_g_closeness = vft.convert_to_vf(g, vfmode=vft.CLOSENESS)
    else:
        logger.info('Skip closeness graph')

    # Randomize stretch dispersion
    stretches = [x[helpers.HOP_STRETCH] for x in meta]
    # a veletlen stretchek az eredeti stretchek veletlen, nem
    # visszateveses mintavetelezese. Mindez annyiszor, ahany
    # veletlen utat akarunk generalni minden valos trace vegpontja
    # kozott.
    stretch_list = [random.sample(stretches, len(stretches))
                    for x in xrange(0, try_per_trace)]
    # A kovetkezo ciklusban minden meta sorhoz rogton kiszamolunk
    # annyi random utat, amennyit parameterben megadtunk. Ehhez at kell
    # alakitani a stretch lista strukturat, hogy minden elem egy meta sorhoz
    # tartalmazza a random stretch ertekeket
    #
    # Pelda: elozo lepesnel kijott ez: [ [1,2,3,4], [2,4,1,3], [3,1,4,2] ]
    # vagyis elso lepesben a metaban tarolt tracek rendre 1,2,3,4 stretchet
    # kell felvegyenek, a masodikban 2,4,1,3 stb. A ciklusban viszont a meta
    # elso elemehez rogton ki akarjuk szamolni a veletlen stretchekhez tartozo
    # random utakat, vagyis [ [1,2,3], [2,4,1], [3,1,4], [4,3,2] ] formaban
    # van szukseg az ertekekre
    stretch_list = zip(*stretch_list)

    progress = progressbar1.DummyProgressBar(end=10, width=15)
    if show_progress:
        progress = progressbar1.AnimatedProgressBar(end=len(meta), width=15)

    for idx, record in enumerate(meta):
        progress += 1
        progress.show_progress()

        trace = vft.trace_in_vertex_id(g, [record[helpers.TRACE], ])
        if len(trace) != 1:
            print 'PROBLEM'
            print record
            continue
        trace = trace[0]

        if len(trace) == 1: continue

        sh_len = record[helpers.SH_LEN]
        s, t = trace[0], trace[-1]

        is_vf_prelabeled_l = []
        is_lp_prelabeled_hard_l = []
        is_lp_prelabeled_soft_l = []

        is_vf_degree_l = []
        is_lp_degree_hard_l = []
        is_lp_degree_soft_l = []

        is_vf_closeness_l = []
        is_lp_closeness_hard_l = []
        is_lp_closeness_soft_l = []

        stretch_dist = stretch_list[idx]
        real_stretch_dist = []
        for current_stretch in stretch_dist:
            random_length = sh_len + current_stretch
            random_path = helpers.random_route_walk(g, s, t, random_length)
            real_stretch_dist.append(len(random_path) - sh_len)
            if len(random_path) == 0:
                empty += 1

            if flags[FLAG_PRELABELED]:
                (is_vf_prelabeled,
                 is_lp_prelabeled_soft,
                 is_lp_prelabeled_hard) = vf_attributes(g,random_path,
                                                   vft.PRELABELED,
                                                   flags[FLAG_LP_SOFT],
                                                   flags[FLAG_LP_HARD],
                                                   vf_g_pre)
                is_vf_prelabeled_l.append(is_vf_prelabeled)
                is_lp_prelabeled_soft_l.append(is_lp_prelabeled_soft)
                is_lp_prelabeled_hard_l.append(is_lp_prelabeled_hard)

            if flags[FLAG_DEGREE]:
                (is_vf_degree,
                 is_lp_degree_soft,
                 is_lp_degree_hard) = vf_attributes(g, random_path,
                                                    vft.DEGREE,
                                                    flags[FLAG_LP_SOFT],
                                                    flags[FLAG_LP_HARD],
                                                    vf_g_degree)
                is_vf_degree_l.append(is_vf_degree)
                is_lp_degree_soft_l.append(is_lp_degree_soft)
                is_lp_degree_hard_l.append(is_lp_degree_hard)

            if flags[FLAG_CLOSENESS]:
                (is_vf_closeness,
                 is_lp_closeness_soft,
                 is_lp_closeness_hard) = vf_attributes(g, random_path,
                                                       vft.CLOSENESS,
                                                       flags[FLAG_LP_SOFT],
                                                       flags[FLAG_LP_HARD],
                                                       vf_g_closeness)
                is_vf_closeness_l.append(is_vf_closeness)
                is_lp_closeness_soft_l.append(is_lp_closeness_soft)
                is_lp_closeness_hard_l.append(is_lp_closeness_hard)

        result = {
            helpers.RANDOM_GULYAS_WALK_ROUTES_RQ_STRETCH: stretch_dist,
            helpers.RANDOM_GULYAS_WALK_ROUTES_STRETCH: real_stretch_dist,
            helpers.RANDOM_GULYAS_WALK_ROUTES_VF_PRELABELED: is_vf_prelabeled_l,
            helpers.RANDOM_GULYAS_WALK_ROUTES_VF_DEGREE: is_vf_degree_l,
            helpers.RANDOM_GULYAS_WALK_ROUTES_VF_CLOSENESS: is_vf_closeness_l,
            helpers.RANDOM_GULYAS_WALK_ROUTES_LP_SOFT_PRELABELED: is_lp_prelabeled_soft_l,
            helpers.RANDOM_GULYAS_WALK_ROUTES_LP_HARD_PRELABELED: is_lp_prelabeled_hard_l,
            helpers.RANDOM_GULYAS_WALK_ROUTES_LP_SOFT_DEGREE: is_lp_degree_soft_l,
            helpers.RANDOM_GULYAS_WALK_ROUTES_LP_HARD_DEGREE: is_lp_degree_hard_l,
            helpers.RANDOM_GULYAS_WALK_ROUTES_LP_SOFT_CLOSENESS: is_lp_closeness_soft_l,
            helpers.RANDOM_GULYAS_WALK_ROUTES_LP_HARD_CLOSENESS: is_lp_closeness_hard_l,
        }

        record.update(result)
Пример #27
0
def filter(g, traceroutes):
    results = list()

    # remove traces with unknown nodes
    traceroutes = vft.trace_in_vertex_id(g, traceroutes)

    progress = progressbar1.AnimatedProgressBar(end=len(traceroutes), width=15)
    for trace in traceroutes:
        progress += 1
        progress.show_progress()

        if not vft.trace_exists(g, trace):
            print 'BUG?'
            continue

        for x in range(0, g.vcount()):
            g.vs[x]['traces'] = dict()

        trace = tuple(trace)
        s, t = trace[0], trace[-1]

        sh_len = g.shortest_paths(s, t, mode=i.ALL)[0][0]
        sh_len += 1  # igraph's hop count to node count

        all_routes = helpers.dfs_mark(g, s, t, sh_len + 1)
        # all_routes2 = helpers.dfs_simple(g, s, t, sh_len + 1, ())

        # if set(all_routes) - set(all_routes2) != set(all_routes2) - set(all_routes):
        #     print 'AJAJAJ'
        #     print all_routes
        #     print '----------'
        #     print all_routes2

        sh_routes = [x for x in all_routes if len(x) == sh_len]

        all_vf_routes = [x for x in all_routes if vft.is_valley_free(g, x)]
        prediction_set = set(sh_routes) | set(all_vf_routes)

        result = [
            trace,
            len(trace),
            sh_len,
            len(sh_routes),
            trace in sh_routes,
            len(all_vf_routes),
            trace in all_vf_routes,
            len(all_routes),
            trace in all_routes,
            len(prediction_set),
            trace in prediction_set,
            vft.is_valley_free(g, trace),
            # sh_routes, all_vf_routes, all_routes,
            vft.trace_to_string(g, trace)
        ]

        results.append(result)

    print >> sys.stderr, (
        'TRACE\tTRACE_LEN\tSH_LEN',
        '\t#SH_ROUTE\tOK',
        '\t#ALL_VF\tOK',
        '\t#ALL_ROUTE\tOK',
        '\t#PREDICTION_SET\tOK',
        '\tIS_VF',
        # '\tSH_ROUTES\tALL_VF_ROUTES\tALL_ROUTE',
        '\tTRACE_STR')
    for result in results:
        result = [str(r) for r in result]
        print >> sys.stderr, '\t'.join(result)

    return results
Пример #28
0
def purify(g, meta, filters):

    results = dict()
    traceroutes = [x[helpers.TRACE] for x in meta]

    if 'cc' in filters:
        results['cc'] = g.transitivity_undirected(mode=igraph.TRANSITIVITY_ZERO)

    if 'ad' in filters:
        results['ad'] = g.average_path_length(directed=False, unconn=True)

    if 'nc' in filters:
        results['nc'] = g.vcount()

    if 'ec' in filters:
        results['ec'] = g.ecount()

    if 'rc' in filters:
        k = 20
        scores = g.degree()
        indices = range(g.vcount())
        indices.sort(key=scores.__getitem__)
        e_k = [x for x in g.es
               if g.degree(x.source) >= k and g.degree(x.target) >= 50]
        e_k2 = float(2 * len(e_k))
        n_k = float(len([x for x in g.vs if g.degree(x) >= k]))

        fi_k = e_k2 / (n_k * (n_k - 1))
        results['rc'] = fi_k

    # remove traces with unknown nodes
    before_caida = len(traceroutes)
    traceroutes = vft.trace_in_vertex_id(g, traceroutes)

    if 'tc' in filters:
        results['tc'] = len(traceroutes)

    if 'tl' in filters:
        results['tl'] = np.mean([len(x) for x in traceroutes])

    if 'tml' in filters:
        results['tml'] = max([len(x) for x in traceroutes])
        results['tml_sentence'] = vft.trace_in_vertex_name(g, [x for x in traceroutes if len(x) == results['tml']])[0]

    if 'tsl' in filters:
        results['tsl'] = min([len(x) for x in traceroutes])
        results['tsl_sentence'] = vft.trace_in_vertex_name(g, [x for x in traceroutes if len(x) == results['tsl']])[0]

    if 'rt' in filters:
        results['rt'] = before_caida - len(traceroutes)

    if 'vf_prelabeled' in filters:
        results['vf_prelabeled'] = len([x for x in meta if x[helpers.IS_VF_PRELABELED] == 1])

    if 'vf_degree' in filters:
        results['vf_degree'] = len([x for x in meta if x[helpers.IS_VF_DEGREE] == 1])

    if 'vf_closeness' in filters:
        results['vf_closeness'] = len([x for x in meta if x[helpers.IS_VF_CLOSENESS] == 1])

    if 'random_walk_vf_closeness' in filters:
        results['random_walk_vf_closeness'] = len([x for x in meta if x[helpers.RANDOM_WALK_VF_CLOSENESS_ROUTE] == 1])

    if 'lp_soft_prelabeled' in filters:
        results['lp_soft_prelabeled'] = len([x for x in meta if x[helpers.IS_LP_SOFT_PRELABELED] == 1])

    if 'lp_hard_prelabeled' in filters:
        results['lp_hard_prelabeled'] = len([x for x in meta if x[helpers.IS_LP_HARD_PRELABELED] == 1])

    if 'lp_soft_degree' in filters:
        results['lp_soft_degree'] = len([x for x in meta if x[helpers.IS_LP_SOFT_DEGREE] == 1])

    if 'lp_hard_degree' in filters:
        results['lp_hard_degree'] = len([x for x in meta if x[helpers.IS_LP_HARD_DEGREE] == 1])

    if 'lp_soft_closeness' in filters:
        results['lp_soft_closeness'] = len([x for x in meta if x[helpers.IS_LP_SOFT_CLOSENESS] == 1])

    if 'lp_hard_closeness' in filters:
        results['lp_hard_closeness'] = len([x for x in meta if x[helpers.IS_LP_HARD_CLOSENESS] == 1])

    if 'pred' in filters:
        # SH prediction
        sh_pred = len([x for x in meta if x[helpers.SH_LEN] == x[helpers.TRACE_LEN]])
        # only VF with 1 extra hop
        ppvf_pred = len([x for x in meta if x[helpers.TRACE_LEN] <= x[helpers.SH_LEN] + 1 and x[helpers.IS_VF_DEGREE] == 1])
        # SH or VF with one extra hop
        smart_pred = len([x for x in meta if x[helpers.SH_LEN] == x[helpers.TRACE_LEN] or (x[helpers.TRACE_LEN] <= x[helpers.SH_LEN] + 1 and x[helpers.IS_VF_DEGREE] == 1)])

        # Brute force prediction
        all_pred = len([x for x in meta if x[helpers.TRACE_LEN] <= x[helpers.SH_LEN] + 1])

        results['sh_pred'] = sh_pred
        results['ppvf_pred'] = ppvf_pred
        results['smart_pred'] = smart_pred
        results['all_pred'] = all_pred

    return results
Пример #29
0
def purify(g, meta, out, count=1000):
    results = list()
    results2 = list()
    results3 = list()
    all_vf = 0
    all_nonvf = 0
    all_vf_closeness = 0
    all_nonvf_closeness = 0

    short_results = list()
    short_results2 = list()
    short_results3 = list()
    all_short_vf = 0
    all_short_nonvf = 0
    all_short_vf_closeness = 0
    all_short_nonvf_closeness = 0

    long_results = list()
    long_results2 = list()
    long_results3 = list()
    all_long_vf = 0
    all_long_nonvf = 0
    all_long_vf_closeness = 0
    all_long_nonvf_closeness = 0

    # remove traces with already calculated all_path
    logger.warn('[r]ONLY NOT FILLED PATHS[/]')
    meta = [x for x in meta if not helpers.ALL_PATH_COUNT in x]

    # traces with a maximum stretch
    logger.warn('[r]!!!ONLY WITH LOW STRETCH[/]')
    meta = [x for x in meta if x[helpers.STRETCH] < 4]

    # shorter meta records
    logger.warn('[r]!!!ONLY SHORT TRACES[/]')
    meta = [x for x in meta if len(x[helpers.TRACE]) < 5]

    meta_map = {tuple(x[helpers.TRACE]): x for x in meta}

    # traceroutes = [x for x in meta if x[TRACE_LEN] == x[SH_LEN]]
    logger.info('All trace count: %d' % len(meta))
    tr_count = min(len(meta), count)
    meta = random.sample(meta, tr_count)
    logger.info('Chosen trace count: %d' % len(meta))

    real_vf = [x for x in meta if x[helpers.IS_VF] == 1]
    real_nonvf = [x for x in meta if x[helpers.IS_VF] == 0]

    real_vf_closeness = [x for x in meta if x[helpers.IS_VF_CLOSENESS] == 1]
    real_nonvf_closeness = [x for x in meta if x[helpers.IS_VF_CLOSENESS] == 0]

    logger.info('Real vf: %f[%d]' % ((len(real_vf)/float(len(meta)), len(real_vf))))
    logger.info('Real nonvf: %f[%d]' % ((len(real_nonvf)/float(len(meta)), len(real_nonvf))))

    logger.info('Real vf closeness: %f[%d]' % ((len(real_vf_closeness)/float(len(meta)), len(real_vf_closeness))))
    logger.info('Real nonvf closeness: %f[%d]' % ((len(real_nonvf_closeness)/float(len(meta)), len(real_nonvf_closeness))))

    logger.info('Remove unknown traces. Trace count before: %d' % len(meta))
    traceroutes = [x[helpers.TRACE] for x in meta]
    traceroutes, ignored = vft.trace_clean(g, traceroutes)
    logger.info('Traceroutes after: %d. Ignored: %d' % (len(traceroutes), ignored))

    traceroutes = vft.trace_in_vertex_id(g, traceroutes)

    progress = progressbar1.AnimatedProgressBar(end=len(traceroutes), width=15)
    for trace in traceroutes:
        progress += 1
        progress.show_progress()

        for x in range(0, g.vcount()):
            g.vs[x]['traces'] = dict()

        s, t = trace[0], trace[-1]
        sh_path = g.get_all_shortest_paths(s, t, mode=i.OUT)
        all_path = helpers.dfs_mark(copy.deepcopy(g), s, t, len(trace))

        # if len(sh_path) != len(all_path):
        #     print len(sh_path)
        #     print len(all_path)
        #     print s, t

        # sanity check
        for x in all_path:
            if x[0] != s or x[-1] != t:
                logger.error('ALERT')
        if len(set([tuple(x) for x in all_path])) != len(all_path):
            logger.error('LENGTH ALERT')
            logger.error('%s' % len(all_path))
            logger.error('%s' % len(set([tuple(x) for x in all_path])))
            logger.error('%s' % sorted(all_path))

        long_path = [x for x in all_path if len(x) == len(trace)]
        short_path = [x for x in all_path if len(x) < len(trace)]

        named_trace = [g.vs[x]['name'] for x in trace]
        extra_meta = {
            helpers.ALL_PATH_COUNT: len(all_path),
            helpers.SAME_LONG_PATH_COUNT: len(long_path),
            helpers.SHORTER_PATH_COUNT: len(short_path)
        }
        meta_map[tuple(named_trace)].update(extra_meta)

        vf_count = sum([1 if vft.is_valley_free(g, x, vfmode=vft.PRELABELED) else 0 for x in all_path])
        nonvf = len(all_path) - vf_count

        vf_closeness_count = sum([1 if vft.is_valley_free(g, x, vfmode=vft.CLOSENESS) else 0 for x in all_path])
        nonvf_closeness = len(all_path) - vf_closeness_count

        tmp = [1 if vft.is_valley_free(g, x, vfmode=vft.PRELABELED) else 0 for x in short_path]
        short_vf_count = sum(tmp)
        short_nonvf = len(tmp) - short_vf_count

        tmp = [1 if vft.is_valley_free(g, x, vfmode=vft.CLOSENESS) else 0 for x in short_path]
        short_vf_closeness_count = sum(tmp)
        short_nonvf_closeness = len(tmp) - short_vf_closeness_count

        tmp = [1 if vft.is_valley_free(g, x, vfmode=vft.PRELABELED) else 0 for x in long_path]
        long_vf_count = sum(tmp)
        long_nonvf = len(tmp) - long_vf_count

        tmp = [1 if vft.is_valley_free(g, x, vfmode=vft.CLOSENESS) else 0 for x in long_path]
        long_vf_closeness_count = sum(tmp)
        long_nonvf_closeness = len(tmp) - long_vf_closeness_count

        extra_meta = {
            helpers.ALL_PATH_VF_COUNT: vf_closeness_count,
            helpers.SAME_LONG_PATH_VF_COUNT: long_vf_closeness_count,
            helpers.SHORTER_PATH_VF_COUNT: short_vf_closeness_count
        }
        meta_map[tuple(named_trace)].update(extra_meta)

        all_vf += vf_count
        all_nonvf += nonvf

        all_vf_closeness += vf_closeness_count
        all_nonvf_closeness += nonvf_closeness

        all_long_vf += long_vf_count
        all_long_nonvf += long_nonvf

        all_long_vf_closeness += long_vf_closeness_count
        all_long_nonvf_closeness += long_nonvf_closeness

        all_short_vf += short_vf_count
        all_short_nonvf += short_nonvf

        all_short_vf_closeness += short_vf_closeness_count
        all_short_nonvf_closeness += short_nonvf_closeness

        results.append(vf_count / float(len(all_path)))
        results3.append(vf_closeness_count / float(len(all_path)))
        if len(all_path) > 1: results2.append(vf_count / float(len(all_path)))

        long_results.append(long_vf_count / float(len(long_path)))
        long_results3.append(long_vf_closeness_count / float(len(long_path)))
        if len(long_path) > 1: long_results2.append(long_vf_count / float(len(long_path)))

        if len(short_path) > 0:
            short_results.append(short_vf_count / float(len(short_path)))
            short_results3.append(short_vf_closeness_count / float(len(short_path)))
        else:
            pass
            # short_results.append(0)
            # short_results3.append(0)
        if len(short_path) > 1: short_results2.append(short_vf_count / float(len(short_path)))

    # save mofified meta
    meta_mod = [x for x in meta_map.itervalues()]
    helpers.save_to_json(out, meta_mod)

    # print results
    print 'ALL'
    print 'VF count: %d' % all_vf
    print 'VF CLOSENESS count: %d' % all_vf_closeness
    print 'Non vf count: %d' % all_nonvf
    print 'Non vf CLOSENESS count: %d' % all_nonvf_closeness
    print 'VF perc: %f' % (all_vf/float(all_vf + all_nonvf))
    print 'VF CLOSENESS perc: %f' % (all_vf_closeness/float(all_vf_closeness + all_nonvf_closeness))
    print 'Mean VF prob: %f' % np.mean(results)
    print 'Mean VF CLOSENESS prob: %f' % np.mean(results3)
    print 'Mean VF2 prob: %f' % np.mean(results2)
    print '=========='
    print 'SHORT'
    print 'VF count: %d' % all_short_vf
    print 'VF  CLOSENESS count: %d' % all_short_vf_closeness
    print 'Non vf count: %d' % all_short_nonvf
    print 'Non vf CLOSENESS count: %d' % all_short_nonvf_closeness
    if all_short_vf + all_short_nonvf > 0:
        print 'VF perc: %f' % (all_short_vf/float(all_short_vf + all_short_nonvf))
    if all_short_vf_closeness + all_short_nonvf_closeness > 0:
        print 'VF CLOSENESS perc: %f' % (all_short_vf_closeness/float(all_short_vf_closeness + all_short_nonvf_closeness))
    print 'Mean VF prob: %f' % np.mean(short_results)
    print 'Mean VF CLOSENESS prob: %f' % np.mean(short_results3)
    print 'Mean VF2 prob: %f' % np.mean(short_results2)
    print '=-----------------'
    print 'LONG'
    print 'VF count: %d' % all_long_vf
    print 'VF CLOSENESS count: %d' % all_long_vf_closeness
    print 'Non vf count: %d' % all_long_nonvf
    print 'Non vf CLOSENESS count: %d' % all_long_nonvf_closeness
    print 'VF perc: %f' % (all_long_vf/float(all_long_vf + all_long_nonvf))
    print 'VF CLOSENESS perc: %f' % (all_long_vf_closeness/float(all_long_vf_closeness + all_long_nonvf_closeness))
    print 'Mean VF prob: %f' % np.mean(long_results)
    print 'Mean VF CLOSENESS prob: %f' % np.mean(long_results3)
    print 'Mean VF2 prob: %f' % np.mean(long_results2)
Пример #30
0
def main():
    parser = argparse.ArgumentParser(
        description=('Syntetic route generator'),
        formatter_class=argparse.ArgumentDefaultsHelpFormatter)

    parser.add_argument('--progressbar', action='store_true')
    parser.add_argument('--verbose', '-v', action='count', default=0)

    parser.add_argument('network')
    parser.add_argument('meta')
    parser.add_argument('all_trace_out', metavar='all-trace-out')
    parser.add_argument('syntetic_out', metavar='syntetic-out')
    parser.add_argument('--trace-count',
                        '-tc',
                        type=int,
                        dest='trace_count',
                        default=5000)
    parser.add_argument('--random-sample',
                        dest='random_sample',
                        action='store_true')

    parser.add_argument('--closeness-error',
                        '-ce',
                        type=float,
                        dest='closeness_error',
                        default=0.0)

    parser.add_argument('--core-limit-percentile',
                        '-cl',
                        type=int,
                        dest='core_limit',
                        default=0)

    parser.add_argument('--toggle-node-error-mode', action='store_true')

    arguments = parser.parse_args()

    arguments.verbose = min(len(helpers.LEVELS) - 1, arguments.verbose)
    logging.getLogger('compnet').setLevel(helpers.LEVELS[arguments.verbose])

    show_progress = arguments.progressbar

    g = helpers.load_network(arguments.network)
    g = g.simplify()

    meta = helpers.load_from_json(arguments.meta)

    if arguments.random_sample:
        random.shuffle(meta)

    meta = meta[:arguments.trace_count]

    N = g.vcount()

    cl = sorted([x['closeness'] for x in g.vs], reverse=True)
    logger.info('Min closeness: %s' % np.min(cl))
    logger.info('Max closeness: %s' % np.max(cl))
    logger.info('Mean closenss: %s' % np.mean(cl))
    logger.info('10%% closeness: %s' % np.percentile(cl, 10))
    logger.info('90%% closeness: %s' % np.percentile(cl, 90))

    logger.info('Core limit: [r]%d%%[/]' % arguments.core_limit)
    change_probability = 100 * arguments.closeness_error
    logger.info('Change probability: [r]%6.2f%%[/]' % change_probability)

    core_limit = np.percentile(cl, arguments.core_limit)
    logger.info('Core limit in closeness: [bb]%f[/]' % core_limit)

    if arguments.toggle_node_error_mode:
        logger.info('[r]Node error mode[/]')
        msg = (
            "If given node's closeness >= core_limit then the new ",
            "closeness in this node is ",
            #"rand(closeness_error ... old closeness)"
            "OLD_CLOSENSS * +/- closeness_error%")
        logger.info(''.join(msg))
        logger.info('Minimum node closeness: [g]%f[/]' %
                    arguments.closeness_error)
        for n in g.vs:
            if n['closeness'] < core_limit:
                continue
            # sign = -1 if random.uniform(-1, 1) < 0 else 1
            # n['closeness'] = n['closeness'] * (1 + sign * arguments.closeness_error)

            new_closeness = random.uniform(arguments.closeness_error,
                                           n['closeness'])
            n['closeness'] = new_closeness

    g_labeled = vft.label_graph_edges(g, vfmode=vft.CLOSENESS)
    peer_edge_count = len([x for x in g_labeled.es if x['dir'] == LinkDir.P])
    logger.info('Peer edge count: %d' % peer_edge_count)

    changed_edges = []

    if not arguments.toggle_node_error_mode:
        msg = ("If the closeness values of the endpoints in given edge is ",
               "larger than the core_limit and ",
               "random(0,1) < closeness_error then change the direction ",
               "for this edge")
        logger.info(''.join(msg))

        changed_u = changed_d = 0
        changed_edges = []
        changed_edgess = []
        for edge in g_labeled.es:
            s, t = edge.source, edge.target
            s_cl = g_labeled.vs[s]['closeness']
            t_cl = g_labeled.vs[t]['closeness']

            if (s_cl < core_limit or t_cl < core_limit): continue
            if random.uniform(0, 1) > arguments.closeness_error: continue
            # if abs(s_cl - t_cl) / min(s_cl, t_cl) > 0.02: continue

            new_edge_dir = LinkDir.U if random.uniform(0,
                                                       1) > 0.5 else LinkDir.D

            if new_edge_dir != edge['dir']:
                if edge['dir'] == LinkDir.U:
                    changed_u += 1
                else:
                    changed_d += 1

                edge['dir'] = new_edge_dir
                changed_edges.append(edge)
                changed_edgess.append((edge.source, edge.target))

            # if edge['dir'] == LinkDir.U:
            #     changed_u += 1
            #     changed_edgess.append((edge.source, edge.target))
            #     edge['dir'] = LinkDir.D
            #     changed_edges.append(edge)
            # elif edge['dir'] == LinkDir.D:
            #     changed_d += 1
            #     changed_edgess.append((edge.source, edge.target))
            #     edge['dir'] = LinkDir.U
            #     changed_edges.append(edge)
        logger.info('E count: %d' % g_labeled.ecount())
        logger.info('Changed U: %d' % changed_u)
        logger.info('Changed D: %d' % changed_d)
        logger.info('Changed: %d' % (changed_d + changed_u))

    changed_e = [(g_labeled.vs[e.source]['name'],
                  g_labeled.vs[e.target]['name']) for e in changed_edges]
    changed_e = changed_e + [(x[1], x[0]) for x in changed_e]

    changed_e = set(changed_e)

    vf_g_closeness = vft.convert_to_vf(g,
                                       vfmode=vft.CLOSENESS,
                                       labeled_graph=g_labeled)

    # e_colors = []
    # for e in vf_g_closeness.es:
    #     if e.source < N and e.target < N: col = 'grey'
    #     elif e.source < N and e.target >= N: col = 'blue'
    #     elif e.source >= N and e.target >= N: col = 'red'
    #     else: col = 'cyan'
    #     e_colors.append(col)
    # igraph.plot(vf_g_closeness, "/tmp/closeness.pdf",
    #             vertex_label=vf_g_closeness.vs['name'],
    #             vertex_size=0.2,
    #             edge_color=e_colors)

    pairs = [(g.vs.find(x[helpers.TRACE][0]).index,
              g.vs.find(x[helpers.TRACE][-1]).index, tuple(x[helpers.TRACE]))
             for x in meta]

    # pairs = list(set(pairs))

    # random.shuffle(pairs)

    # visited = set()
    # pairs2 = []
    # for x in pairs:
    #     k = (x[0], x[1])
    #     if k in visited: continue
    #     visited.add(k)
    #     visited.add((k[1], k[0]))
    #     pairs2.append(x)

    # pairs = pairs2

    traces = [x[2] for x in pairs]

    stretches = []
    syntetic_traces = []
    sh_traces = []
    base_traces = []
    original_traces = []
    bad = 0

    progress = progressbar1.DummyProgressBar(end=10, width=15)
    if show_progress:
        progress = progressbar1.AnimatedProgressBar(end=len(pairs), width=15)

    for s, t, trace_original in pairs:
        progress += 1
        progress.show_progress()

        trace_original_idx = [g.vs.find(x).index for x in trace_original]
        logger.debug('Original trace: %s -- %s -- %s',
                     [g.vs[x]['name'] for x in trace_original_idx],
                     vft.trace_to_string(g, trace_original_idx, vft.CLOSENESS),
                     [g.vs[x]['closeness'] for x in trace_original_idx])

        sh_routes = g.get_all_shortest_paths(s, t)
        sh_len = len(sh_routes[0])

        sh_routes_named = [[g.vs[y]['name'] for y in x] for x in sh_routes]
        sh_trace_name = random.choice(sh_routes_named)
        base_trace_name = random.choice(sh_routes_named)

        candidates = vf_g_closeness.get_all_shortest_paths(s + N, t + N)
        candidates = [vft.vf_route_converter(x, N) for x in candidates]
        # candidates = []

        if len(candidates) == 0:
            candidates = vft.get_shortest_vf_route(g_labeled,
                                                   s,
                                                   t,
                                                   mode='vf',
                                                   vf_g=vf_g_closeness,
                                                   _all=True,
                                                   vfmode=vft.CLOSENESS)

        if len(candidates) == 0:
            s_name, t_name = g.vs[s]['name'], g.vs[t]['name']
            logger.debug("!!!No syntetic route from %s to %s" %
                         (s_name, t_name))
            continue

        logger.debug('Candidates from %s to %s:' %
                     (g.vs[s]['name'], g.vs[t]['name']))

        for c in candidates:
            logger.debug('%s -- %s -- %s' %
                         ([g.vs[x]['name'] for x in c],
                          vft.trace_to_string(g_labeled, c, vft.PRELABELED),
                          [g.vs[x]['closeness'] for x in c]))

        chosen_one = random.choice(candidates)
        chosen_one_name = [g.vs[x]['name'] for x in chosen_one]

        # print chosen_one
        # print trace_original
        # pretty_plotter.pretty_plot(g, trace_original_idx,
        #                            chosen_one, changed_edgess,
        #                            spec_color=(0, 0, 0, 155))

        hop_stretch = len(chosen_one) - sh_len
        stretches.append(hop_stretch)

        trace_original_e = zip(trace_original, trace_original[1:])
        chosen_one_e = zip(chosen_one_name, chosen_one_name[1:])
        trace_affected = any([x in changed_e for x in trace_original_e])
        chosen_affected = any([x in changed_e for x in chosen_one_e])
        logger.debug('Trace affected: %s' % trace_affected)
        logger.debug('Chosen affected: %s' % chosen_affected)

        # if hop_stretch > 2:
        #     logger.debug('Base: %s' % trace_to_string(g_labeled, base_trace_name))
        #     logger.debug('SH: %s' % trace_to_string(g_labeled, sh_trace_name))
        #     logger.debug('Trace: %s' % trace_to_string(g_labeled, trace_original))
        #     logger.debug('Syntetic: %s' % trace_to_string(g_labeled, chosen_one_name))

        if trace_affected or chosen_affected or hop_stretch > 2:
            # pretty_plotter.pretty_plot_all(g, traces,
            #                                chosen_one, changed_edgess,
            #                                spec_color=(0, 0, 0, 255))
            bad += 1

        syntetic_traces.append(chosen_one_name)
        sh_traces.append(sh_trace_name)
        base_traces.append(base_trace_name)
        original_traces.append(trace_original)
        logger.debug('From %s to %s chosen one %s' %
                     (g.vs[s]['name'], g.vs[t]['name'], chosen_one_name))

    result = zip(base_traces, sh_traces, original_traces, syntetic_traces)
    helpers.save_to_json(arguments.all_trace_out, result)
    helpers.save_to_json(arguments.syntetic_out, syntetic_traces)

    print 'Bad: %d' % bad

    c = collections.Counter(stretches)
    trace_count = len(syntetic_traces)
    logger.info('Stretch dist:')
    for k in c:
        logger.info('\t%d: %5.2f%%[%d]' %
                    (k, 100 * c[k] / float(trace_count), c[k]))
    logger.info('Valid route count: %d' % trace_count)
    logger.info('Route count parameter: %d' % arguments.trace_count)
    logger.info('Generated valid pair count: %d' % len(pairs))