From 94b40f8045085a1573b80102bdd5f5b43b9db16c Mon Sep 17 00:00:00 2001 From: "Olivier J.N. Bertrand" <olivier.bertrand@uni-bielefeld.de> Date: Thu, 30 Aug 2018 18:06:51 +0200 Subject: [PATCH] Simply build graph by using velocities --- navipy/moving/agent.py | 83 +++++++++++++++++------------------------- 1 file changed, 33 insertions(+), 50 deletions(-) diff --git a/navipy/moving/agent.py b/navipy/moving/agent.py index f7ad52a..4672b94 100644 --- a/navipy/moving/agent.py +++ b/navipy/moving/agent.py @@ -439,56 +439,39 @@ the agent motion, or nline += 1 return self.velocities.copy() - def build_graph(self, - ncpu=5, - timeout=1, - filename=None, - blocksize=100): - # Build a list of nodes - results_edges = [] - posorients_queue = JoinableQueue() - results_queue = Queue() - if version < 2: - graph_nodes = list(self._graph.nodes()) - else: - graph_nodes = list(self._graph.nodes) - for node in graph_nodes: - posorients_queue.put(self._graph.nodes[node]['posorient']) - - # Start ndatabase loader - num_agents = ncpu - agents = [GridAgent(copy.copy(self._brain), - posorients_queue=posorients_queue, - results_queue=results_queue) - for _ in range(num_agents)] - for w in agents: - w.mode_of_motion = self.mode_of_motion - w.start() - - # Add a poison pill for each agent - for _ in range(num_agents): - posorients_queue.put(None) - - # Wait for all of the tasks to finish - # posorients_queue.join() - nline = 0 - prev_nline = nline - t_start = time.time() - nbnodes = nx.number_of_nodes(self._graph) - for _ in range(nx.number_of_nodes(self._graph)): - result = results_queue.get(timeout=timeout) - results_edges.append((result[0].name, - result[1].name)) - if (nline-prev_nline) > blocksize: - t_elapse = time.time()-t_start - t_peritem = t_elapse/nline - remain = nbnodes-nline - print('Computed {} in {}'.format(nline, t_elapse)) - print('Remain {}, done in {}'.format(remain, remain*t_peritem)) - if filename is not None: - np.save(filename, np.array(results_edges)) - nline += 1 - # print(results_edges[-1]) + def build_graph(self, movemode, moveparam): + """ + Connect edges with a given velocity + """ + if self.velocities.dropna().shape[0] == 0: + raise NameError('compute_velocities should be called first') + edges = pd.Series(data=np.nan, index=self.velocities.index) + # Make sure that the velocity start at the correct location + posorients = self._brain.posorients + myvelocities = self.velocities.copy() + myvelocities = myvelocities.swaplevel(axis=1) + myvelocities.x = posorients.x + myvelocities.y = posorients.y + myvelocities.z = posorients.z + myvelocities.alpha_0 = posorients.alpha_0 + myvelocities.alpha_1 = posorients.alpha_1 + myvelocities.alpha_2 = posorients.alpha_2 + myvelocities = myvelocities.swaplevel(axis=1) + for ii, row in myvelocities.iterrows(): + if np.any(np.isnan(row)): + continue + # Move according to user mode of motion + nposorient = navimomath.next_pos(row, movemode, moveparam) + # Snap to the closest point + nextpos_index = navimomath.closest_pos( + nposorient, myvelocities) + edges[ii] = nextpos_index.name + # Format for graph + validedges = edges.dropna() + results_edges = np.vstack( + [validedges.index, + validedges.values]).transpose() + # Add to graph self._graph.add_edges_from(results_edges) self.check_graph() -- GitLab