Example #1
0
    def extractSpanningTree(self, nodes):
        assert len(nodes) > 1
        nodeIds = [self.nameToId[name] for name in nodes]
        paths = [dijkstra_path(self.nxDg.to_undirected(), source=nodeIds[0], target=x) for x in nodeIds[1:]]
        nodesToInclude = set()
        for path in paths:
            for node in path:
                nodesToInclude.add(node)

        cpy = self.nxDg.subgraph(nodesToInclude).copy()
        # Get rid of nodes that have only 1 children
        graphWasModified = True
        while graphWasModified:
            graphWasModified = False
            for node in nx.nodes(cpy):
                if cpy.out_degree(node) == 1 and cpy.in_degree(node) == 1:
                    if node not in nodeIds:
                        # This is a spurious node in the species tree,
                        # we can and should remove
                        childEdge = list(cpy.out_edges(node, data=True))[0]
                        parentEdge = list(cpy.in_edges(node, data=True))[0]
                        child = childEdge[1]
                        childDist = childEdge[2]['weight']
                        parent = parentEdge[0]
                        assert parent != node
                        parentDist = parentEdge[2]['weight']
                        cpy.remove_node(node)
                        cpy.add_edge(parent, child, weight = childDist + parentDist)
                        graphWasModified = True
                        break

        mcCpy = MultiCactusTree(cpy, 2)
        mcCpy.nameUnlabeledInternalNodes(prefix="thisPrefixShouldNeverAppear")
        mcCpy.computeSubtreeRoots()
        return mcCpy
Example #2
0
def get_best_route(DG, my_df, stops, start_coord, end_coord, num_stops):
    '''
	Find optimal commuter path between start_coord and end_coord.
	Considers beginning travel at a num_stops number of transit 
	stops that are closest to the starting coordinate. 
	'''
    WAIT_TIME = 7 * 60

    start_stop_ids = get_closest_stops(my_df, start_coord, num_stops)
    end_stop_ids = get_closest_stops(my_df, end_coord, num_stops)
    to_remove = add_temp_stops(DG, stops, start_stop_ids, end_stop_ids,
                               start_coord, end_coord)
    result = algos.dijkstra_path(DG, 'temp start', 'temp end', weight='weight')
    assert (isinstance(result[0], str) and isinstance(result[-1], str)
            and result[1][1] == -1 and result[-2][1] == -1)
    path = Path(DG, result, WAIT_TIME)
    remove_temp_stops(DG, to_remove)
    return path
Example #3
0
    def extractSpanningTree(self, nodes):
        assert len(nodes) > 1
        nodeIds = [self.nameToId[name] for name in nodes]
        paths = [
            dijkstra_path(self.nxDg.to_undirected(),
                          source=nodeIds[0],
                          target=x) for x in nodeIds[1:]
        ]
        nodesToInclude = set()
        for path in paths:
            for node in path:
                nodesToInclude.add(node)

        cpy = self.nxDg.subgraph(nodesToInclude).copy()
        # Get rid of nodes that have only 1 children
        graphWasModified = True
        while graphWasModified:
            graphWasModified = False
            for node in nx.nodes(cpy):
                if cpy.out_degree(node) == 1 and cpy.in_degree(node) == 1:
                    if node not in nodeIds:
                        # This is a spurious node in the species tree,
                        # we can and should remove
                        childEdge = list(cpy.out_edges(node, data=True))[0]
                        parentEdge = list(cpy.in_edges(node, data=True))[0]
                        child = childEdge[1]
                        childDist = childEdge[2]['weight']
                        parent = parentEdge[0]
                        assert parent != node
                        parentDist = parentEdge[2]['weight']
                        cpy.remove_node(node)
                        cpy.add_edge(parent,
                                     child,
                                     weight=childDist + parentDist)
                        graphWasModified = True
                        break

        mcCpy = MultiCactusTree(cpy, 2)
        mcCpy.nameUnlabeledInternalNodes(prefix="thisPrefixShouldNeverAppear")
        mcCpy.computeSubtreeRoots()
        return mcCpy
Example #4
0
    def init_choices(self, init_graph):
        paths = {
            snknode: [
                dijkstra_path(init_graph, srcnode, snknode, weight='length')
                for srcnode in self.sources
            ]
            for snknode in self.sinks
        }

        def dtchoice_init():
            choices = np.zeros((self.T, self.ntypes))
            choices[np.random.choice(self.Tm, 4), :] = 0.25
            return choices

        def srate_init(nodeid):
            out_degree = init_graph.out_degree(nodeid)
            choices = np.zeros((self.T, out_degree, self.ntypes))
            for i in np.arange(self.ntypes):
                for j, srcnodeid in enumerate(self.sources):
                    if nodeid in paths[self.sinks[i]][j]:
                        nxtnodeid = paths[self.sinks[i]][j][
                            paths[self.sinks[i]][j].index(nodeid) + 1]
                        try:
                            idx = list(init_graph.out_edges(nodeid)).index(
                                (nodeid, nxtnodeid))
                            choices[:, idx, i] = 1
                        except ValueError:
                            pass
                        break
                if np.any(~np.isclose(1, np.sum(choices[:, :, i], axis=1))):
                    choices[:, np.random.choice(out_degree, 1), i] = 1
            return choices

        if self.dtchoices == None:
            self.dtchoices = dict()
            for nodeid in self.sources:
                self.dtchoices[nodeid] = dtchoice_init()
        if self.srates == None:
            self.srates = dict()
            for nodeid in self.diverges:
                self.srates[nodeid] = srate_init(nodeid)
    def generate(self, config):
        """Generates a single sensor event time series for a given `WalkGeneratorConfig`.
        """
        # path = [1,2,3,4,5,6,7,8,9,10,13,14,15,16,17,18,3,4,5,6,7,8,9,10,13,14,15,16,17,18,3,2,1]
        self.__reset()
        if config.custom_path is None:
            path = weighted.dijkstra_path(self.G, config.start,
                                          config.destination)
        else:
            path = config.custom_path

        time = 0

        last_node = path[0]
        for node in path[1:]:
            self.__move(node, time)
            distance = self.G.edges[last_node, node]['weight']
            jit = self._rng.uniform(config.jitter * -1, config.jitter)
            time += distance / (config.walking_speed + jit)
            last_node = node
        return self.__telegram_sequence