From 1dd05b6b4ef1fd42324ea5279060822c67669a99 Mon Sep 17 00:00:00 2001
From: "Olivier J.N. Bertrand" <olivier.bertrand@uni-bielefeld.de>
Date: Tue, 21 Aug 2018 14:19:46 +0200
Subject: [PATCH] Add compute from partial velocity

---
 navipy/moving/agent.py | 29 ++++++++++++++---------------
 1 file changed, 14 insertions(+), 15 deletions(-)

diff --git a/navipy/moving/agent.py b/navipy/moving/agent.py
index 188a7aa..196dddf 100644
--- a/navipy/moving/agent.py
+++ b/navipy/moving/agent.py
@@ -366,6 +366,15 @@ the agent motion, or
             self._graph.add_node(row_id,
                                  posorient=posor)
         self.mode_of_motion = mode_of_motion
+        # Create a dataframe to store the velocities
+        convention = self._brain.renderer.rotation_convention
+        tuples_posvel = posorient_columns(convention)
+        tuples_posvel.extend(velocities_columns(convention))
+        index_posvel = pd.MultiIndex.from_tuples(tuples_posvel,
+                                                 names=['position',
+                                                        'orientation'])
+        self.velocities = pd.DataFrame(columns=index_posvel,
+                                       index=list(self._graph.nodes()))
 
     @property
     def graph(self):
@@ -383,26 +392,16 @@ the agent motion, or
                            timeout=1,
                            filename=None,
                            blocksize=100):
-        # Create a dataframe to store the velocities
-        convention = self._brain.renderer.rotation_convention
-        tuples_posvel = posorient_columns(convention)
-        tuples_posvel.extend(velocities_columns(convention))
-        index_posvel = pd.MultiIndex.from_tuples(tuples_posvel,
-                                                 names=['position',
-                                                        'orientation'])
-        self.velocities = pd.DataFrame(columns=index_posvel,
-                                       index=list(self._graph.nodes()))
+        nodes_tocompute = self.velocities.isna().any(axis=1)
+        nodes_tocompute = nodes_tocompute[nodes_tocompute].index
         # Build a list of nodes
         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:
+        for node in nodes_tocompute:
             posorients_queue.put(self._graph.nodes[node]['posorient'])
 
         # Start ndatabase loader
+        convention = self._brain.renderer.rotation_convention
         num_agents = ncpu
         agents = [CyberBeeAgent(copy.copy(self._brain),
                                 convention=convention,
@@ -421,7 +420,7 @@ the agent motion, or
         nline = 0
         prev_nline = nline
         t_start = time.time()
-        nbnodes = nx.number_of_nodes(self._graph)
+        nbnodes = nodes_tocompute.shape[0]
         for _ in range(nbnodes):
             res = results_queue.get(timeout=timeout)
             self.velocities.loc[res.name, res.index] = res
-- 
GitLab