Skip to content
Snippets Groups Projects
Commit 94b40f80 authored by Olivier Bertrand's avatar Olivier Bertrand
Browse files

Simply build graph by using velocities

parent 1b0d75cc
No related branches found
No related tags found
No related merge requests found
...@@ -439,56 +439,39 @@ the agent motion, or ...@@ -439,56 +439,39 @@ the agent motion, or
nline += 1 nline += 1
return self.velocities.copy() return self.velocities.copy()
def build_graph(self, def build_graph(self, movemode, moveparam):
ncpu=5, """
timeout=1, Connect edges with a given velocity
filename=None, """
blocksize=100): if self.velocities.dropna().shape[0] == 0:
# Build a list of nodes raise NameError('compute_velocities should be called first')
results_edges = [] edges = pd.Series(data=np.nan, index=self.velocities.index)
posorients_queue = JoinableQueue() # Make sure that the velocity start at the correct location
results_queue = Queue() posorients = self._brain.posorients
if version < 2: myvelocities = self.velocities.copy()
graph_nodes = list(self._graph.nodes()) myvelocities = myvelocities.swaplevel(axis=1)
else: myvelocities.x = posorients.x
graph_nodes = list(self._graph.nodes) myvelocities.y = posorients.y
for node in graph_nodes: myvelocities.z = posorients.z
posorients_queue.put(self._graph.nodes[node]['posorient']) myvelocities.alpha_0 = posorients.alpha_0
myvelocities.alpha_1 = posorients.alpha_1
# Start ndatabase loader myvelocities.alpha_2 = posorients.alpha_2
num_agents = ncpu myvelocities = myvelocities.swaplevel(axis=1)
agents = [GridAgent(copy.copy(self._brain), for ii, row in myvelocities.iterrows():
posorients_queue=posorients_queue, if np.any(np.isnan(row)):
results_queue=results_queue) continue
for _ in range(num_agents)] # Move according to user mode of motion
for w in agents: nposorient = navimomath.next_pos(row, movemode, moveparam)
w.mode_of_motion = self.mode_of_motion # Snap to the closest point
w.start() nextpos_index = navimomath.closest_pos(
nposorient, myvelocities)
# Add a poison pill for each agent edges[ii] = nextpos_index.name
for _ in range(num_agents): # Format for graph
posorients_queue.put(None) validedges = edges.dropna()
results_edges = np.vstack(
# Wait for all of the tasks to finish [validedges.index,
# posorients_queue.join() validedges.values]).transpose()
nline = 0 # Add to graph
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])
self._graph.add_edges_from(results_edges) self._graph.add_edges_from(results_edges)
self.check_graph() self.check_graph()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment