Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add edges to MeshNeuron #170

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 30 additions & 5 deletions navis/core/mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class MeshNeuron(BaseNeuron):
TEMP_ATTR = ['_memory_usage', '_trimesh', '_skeleton', '_igraph', '_graph_nx']

#: Core data table(s) used to calculate hash
CORE_DATA = ['vertices', 'faces']
CORE_DATA = ['vertices', 'faces', 'edges']

def __init__(self,
x,
Expand All @@ -114,18 +114,29 @@ def __init__(self,
self._lock = 1
self._trimesh = None # this is required to avoid recursion during init


if isinstance(x, MeshNeuron):
self.__dict__.update(x.copy().__dict__)
self.vertices, self.faces, self.edges = x.vertices, x.faces, x.edges
elif hasattr(x, "faces") and hasattr(x, "vertices"):
self.vertices, self.faces = x.vertices, x.faces
elif hasattr(x, 'faces') and hasattr(x, 'vertices'):
self.vertices, self.faces = x.vertices, x.faces
if hasattr(x, "edges"):
self.edges = x.edges
else:
self.edges = np.array([[]])
elif isinstance(x, dict):
if 'faces' not in x or 'vertices' not in x:
if "faces" not in x or "vertices" not in x:
raise ValueError('Dictionary must contain "vertices" and "faces"')
self.vertices, self.faces = x['vertices'], x['faces']
self.vertices, self.faces = x["vertices"], x["faces"]
if "edges" in x:
self.edges = x["edges"]
else:
self.edges = np.array([[]], int)

elif isinstance(x, str) and os.path.isfile(x):
m = tm.load(x)
self.vertices, self.faces = m.vertices, m.faces
self.edges = np.array([[]], int)
elif isinstance(x, type(None)):
# Empty neuron
self.vertices, self.faces = np.zeros((0, 3)), np.zeros((0, 3))
Expand Down Expand Up @@ -282,6 +293,20 @@ def vertices(self, verts):
raise ValueError('Vertices must be 2-dimensional array')
self._vertices = verts
self._clear_temp_attr()

@property
def edges(self):
"""Edges making up the neuron."""
return self._edges

@edges.setter
def edges(self, edges):
if not isinstance(edges, np.ndarray):
raise TypeError(f'Edges must be numpy array, got "{type(edges)}"')
if edges.ndim != 2:
raise ValueError("Edges must be 2-dimensional array")
self._edges = edges
self._clear_temp_attr()

@property
def faces(self):
Expand Down
16 changes: 12 additions & 4 deletions navis/graph/converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,15 @@ def neuron2nx(x: "core.NeuronObject", simplify=False, epsilon=None) -> nx.DiGrap
for e, l in zip(x.trimesh.edges_unique, x.trimesh.edges_unique_length)
]
G.add_weighted_edges_from(edges)
extra_edge_lengths = np.linalg.norm(
x.vertices[x.edges[:, 0], :] - x.vertices[x.edges[:, 1], :], axis=1
)
extra_edges = [
(x.edges[i, 0], x.edges[i, 1], extra_edge_lengths[i])
for i in range(x.edges.shape[0])
]
G.add_weighted_edges_from(extra_edges)

elif isinstance(x, core.Dotprops):
if epsilon is None:
epsilon = 5 * x.sampling_resolution
Expand Down Expand Up @@ -841,14 +850,13 @@ def edges2neuron(edges, vertices=None, validate=True, **kwargs):
# (note that we assign -1 as root's parent)
parents.update({k: v[0] if v else -1 for k, v in this.items()})

nodes = pd.DataFrame(vertices, columns=['x', 'y', 'z'])
nodes.insert(0, 'node_id', nodes.index)
nodes.insert(1, 'parent_id', nodes.index.map(parents))
nodes = pd.DataFrame(vertices, columns=["x", "y", "z"])
nodes.insert(0, "node_id", nodes.index)
nodes.insert(1, "parent_id", nodes.index.map(parents))

return core.TreeNeuron(nodes, **kwargs)



def _find_all_paths(
g: nx.DiGraph, start, end, mode: str = "OUT", maxlen: Optional[int] = None
) -> list:
Expand Down
2 changes: 1 addition & 1 deletion navis/interfaces/blender.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ def _create_mesh(self, x, mat, collection=None):
ob['id'] = str(x.id)

blender_verts = verts.tolist()
me.from_pydata(list(blender_verts), [], list(x.faces))
me.from_pydata(list(blender_verts), list(x.edges), list(x.faces))
me.update()

me.polygons.foreach_set('use_smooth', [True] * len(me.polygons))
Expand Down
Loading