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
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()
......
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