Skip to content
This repository was archived by the owner on Sep 4, 2021. It is now read-only.

Commit 43fabb1

Browse files
committed
fix failure when node ids are large numbers
Converting incl/excl pairs to lil format was resulting in too much memory usage with large node ids. Code is now modified to keep incl/excl pairs in coo format with additional methods to delete/find points using coo components.
1 parent de8959c commit 43fabb1

File tree

1 file changed

+32
-6
lines changed

1 file changed

+32
-6
lines changed

circuitscape/compute_base.py

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -221,25 +221,51 @@ def __init__(self, filename):
221221
mode, point_ids, mat = CSIO.read_included_pairs(filename)
222222
self.is_include = (mode == "include")
223223
self.point_ids = point_ids
224-
self.mat = mat.tolil() #FIXME- causing crashes when point IDs are large (8 digits)
224+
self.mat = mat.tocoo()
225225
self.max_id = int(np.max(point_ids)) + 1
226226

227+
def has_pair(self, r, c):
228+
if not ((r < self.max_id) and (c < self.max_id)):
229+
return 0
230+
has_rows = (self.mat.row == r)
231+
if not any(has_rows):
232+
return 0
233+
has_cols = (self.mat.col[has_rows] == c)
234+
if not any(has_rows):
235+
return 0
236+
valid_data = self.mat.data[has_rows]
237+
return sum(valid_data[has_cols])
238+
239+
def has(self, r):
240+
if not (r < self.max_id):
241+
return 0
242+
has_rows = (self.mat.row == r)
243+
has_cols = (self.mat.col == r)
244+
has_any = has_rows | has_cols
245+
if not any(has_any):
246+
return 0
247+
return sum(self.mat.data[has_any])
248+
227249
def is_included_pair(self, point_id1, point_id2):
228-
return (point_id1 < self.max_id) and (point_id2 < self.max_id) and (((self.mat[point_id1, point_id2] + self.mat[point_id2, point_id1]) > 0) == self.is_include)
250+
return ((self.has_pair(point_id1, point_id2) + self.has_pair(point_id2, point_id1)) > 0) == self.is_include
229251

230252
def is_included(self, point_id):
231-
return (point_id < self.max_id) and (((self.mat[point_id,:].sum() + self.mat[:,point_id].sum()) > 0) == self.is_include)
253+
return ((self.has(point_id) > 0) == self.is_include)
232254

233255
def is_present(self, point_id):
234256
return (point_id in self.point_ids)
235257

236258
def delete_point(self, point_id):
237259
if(point_id < self.max_id):
238-
self.mat[point_id, :] = 0
239-
self.mat[:, point_id] = 0
260+
keep_idxs = (self.mat.col != point_id) & (self.mat.row != point_id)
261+
self.mat.data = self.mat.data[keep_idxs]
262+
self.mat.row = self.mat.row[keep_idxs]
263+
self.mat.col = self.mat.col[keep_idxs]
264+
self.point_ids = self.point_ids[self.point_ids != point_id]
240265

241266
def keep_only_points(self, points):
242-
for point_id in range(0, self.max_id):
267+
ptids = self.point_ids.copy()
268+
for point_id in ptids:
243269
if point_id not in points:
244270
self.delete_point(point_id)
245271

0 commit comments

Comments
 (0)