From 68bb4ca837ca446532c693cbaf14aa1e0ba9cc1d Mon Sep 17 00:00:00 2001 From: Nico Kemnitz Date: Sun, 3 Sep 2023 22:07:49 +0200 Subject: [PATCH 1/2] Edges: avoid duplicate affinity/area initialization --- pychunkedgraph/graph/edges/__init__.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/pychunkedgraph/graph/edges/__init__.py b/pychunkedgraph/graph/edges/__init__.py index cfc280476..5b230ff07 100644 --- a/pychunkedgraph/graph/edges/__init__.py +++ b/pychunkedgraph/graph/edges/__init__.py @@ -33,18 +33,21 @@ def __init__( self.node_ids1 = np.array(node_ids1, dtype=basetypes.NODE_ID) self.node_ids2 = np.array(node_ids2, dtype=basetypes.NODE_ID) assert self.node_ids1.size == self.node_ids2.size - self._affinities = np.ones(len(self.node_ids1)) * DEFAULT_AFFINITY - self._areas = np.ones(len(self.node_ids1)) * DEFAULT_AREA + self._as_pairs = None self._fake_edges = fake_edges if affinities is not None and len(affinities) > 0: self._affinities = np.array(affinities, dtype=basetypes.EDGE_AFFINITY) assert self.node_ids1.size == self._affinities.size + else: + self._affinities = np.full(len(self.node_ids1), DEFAULT_AFFINITY) if areas is not None and len(areas) > 0: self._areas = np.array(areas, dtype=basetypes.EDGE_AREA) assert self.node_ids1.size == self._areas.size + else: + self._areas = np.full(len(self.node_ids1), DEFAULT_AREA) @property def affinities(self) -> np.ndarray: From 82b0e9cb069e555072d14f780b1e0266952b3377 Mon Sep 17 00:00:00 2001 From: Nico Kemnitz Date: Sun, 3 Sep 2023 22:33:11 +0200 Subject: [PATCH 2/2] Edges: avoid copies --- pychunkedgraph/graph/edges/__init__.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pychunkedgraph/graph/edges/__init__.py b/pychunkedgraph/graph/edges/__init__.py index 5b230ff07..b0e488d05 100644 --- a/pychunkedgraph/graph/edges/__init__.py +++ b/pychunkedgraph/graph/edges/__init__.py @@ -30,21 +30,21 @@ def __init__( areas: Optional[np.ndarray] = None, fake_edges=False, ): - self.node_ids1 = np.array(node_ids1, dtype=basetypes.NODE_ID) - self.node_ids2 = np.array(node_ids2, dtype=basetypes.NODE_ID) + self.node_ids1 = np.array(node_ids1, dtype=basetypes.NODE_ID, copy=False) + self.node_ids2 = np.array(node_ids2, dtype=basetypes.NODE_ID, copy=False) assert self.node_ids1.size == self.node_ids2.size self._as_pairs = None self._fake_edges = fake_edges if affinities is not None and len(affinities) > 0: - self._affinities = np.array(affinities, dtype=basetypes.EDGE_AFFINITY) + self._affinities = np.array(affinities, dtype=basetypes.EDGE_AFFINITY, copy=False) assert self.node_ids1.size == self._affinities.size else: self._affinities = np.full(len(self.node_ids1), DEFAULT_AFFINITY) if areas is not None and len(areas) > 0: - self._areas = np.array(areas, dtype=basetypes.EDGE_AREA) + self._areas = np.array(areas, dtype=basetypes.EDGE_AREA, copy=False) assert self.node_ids1.size == self._areas.size else: self._areas = np.full(len(self.node_ids1), DEFAULT_AREA)