Skip to content

Commit dcd77a4

Browse files
committed
prevent overflow in resample_skeleton
1 parent fe544a3 commit dcd77a4

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

navis/sampling/resampling.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ def resample_skeleton(x: 'core.NeuronObject',
5050
within a neuron, but you may encounter duplicates across neurons.
5151
- Any non-standard node table columns (e.g. "labels") will be lost.
5252
- Soma(s) will be pinned to the closest node in the resampled neuron.
53-
53+
- We may end up upcasting the data type for node and parent IDs to
54+
accommodate the new node IDs.
5455
5556
Also: be aware that high-resolution neurons will use A LOT of memory.
5657
@@ -253,13 +254,26 @@ def resample_skeleton(x: 'core.NeuronObject',
253254
data=new_nodes, columns=["node_id", "parent_id"] + num_cols + non_num_cols
254255
)
255256

256-
# Convert columns to appropriate dtypes
257+
# At this point node and parent IDs will be 64 bit integers and x/y/z columns will
258+
# be float 64. We will convert them back to the original dtypes but we have to
259+
# be careful with node & parent IDs to avoid overflows if the original datatype
260+
# can't accommodate the new IDs.
261+
262+
# Gather the original dtypes
257263
dtypes = {
258264
k: x.nodes[k].dtype for k in ["node_id", "parent_id"] + num_cols + non_num_cols
259265
}
260266

261-
for cols in new_nodes.columns:
262-
new_nodes = new_nodes.astype(dtypes, errors="ignore")
267+
# Check for overflow
268+
for col in ("node_id", "parent_id"):
269+
# If there is an overflow downcast to smallest possible dtype
270+
# N.B. we could also check for underflow but that's less likely
271+
if new_nodes[col].max() >= np.iinfo(np.int32).max:
272+
new_nodes[col] = pd.to_nunmeric(new_nodes[col], downcast="integer")
273+
dtypes[col] = new_nodes[col].dtype # Update dtype
274+
275+
# Now cast the rest
276+
new_nodes = new_nodes.astype(dtypes, errors="ignore")
263277

264278
# Remove duplicate nodes (branch points)
265279
new_nodes = new_nodes[~new_nodes.node_id.duplicated()]

0 commit comments

Comments
 (0)