Skip to content

Commit 80ab972

Browse files
fix: support non-writable arrays when in_place is called
1 parent 1cf587f commit 80ab972

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

fastremap/fastremap.pyx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ def renumber(arr, start=1, preserve_zero=True, in_place=False):
150150
shape = arr.shape
151151
order = 'F' if arr.flags['F_CONTIGUOUS'] else 'C'
152152
in_place = in_place and (arr.flags['F_CONTIGUOUS'] or arr.flags['C_CONTIGUOUS'])
153+
in_place = in_place and arr.flags.writeable
153154

154155
if not in_place:
155156
arr = np.copy(arr, order=order)
@@ -482,7 +483,7 @@ def mask_except(arr, labels, in_place=False, value=0):
482483
else:
483484
order = 'C'
484485

485-
if not in_place:
486+
if not in_place or not arr.flags.writeable:
486487
arr = np.copy(arr, order=order)
487488

488489
arr = _reshape(arr, (arr.size,))
@@ -685,7 +686,7 @@ def remap(arr, table, preserve_missing_labels=False, in_place=False):
685686
fit_value = min_label if abs(min_label) > abs(max_label) else max_label
686687
arr = refit(arr, fit_value, increase_only=True)
687688

688-
if not in_place and original_dtype == arr.dtype:
689+
if (not in_place and original_dtype == arr.dtype) or not arr.flags.writeable:
689690
arr = np.copy(arr, order=order)
690691

691692
if all([ k == v for k,v in table.items() ]) and preserve_missing_labels:
@@ -771,7 +772,7 @@ def remap_from_array(cnp.ndarray[UINT] arr, cnp.ndarray[UINT] vals, in_place=Tru
771772
cdef size_t maxkey = vals.size - 1
772773
cdef UINT elem
773774

774-
if not in_place:
775+
if not in_place or not arr.flags.writeable:
775776
arr = np.copy(arr)
776777

777778
with nogil:
@@ -796,7 +797,7 @@ def remap_from_array_kv(cnp.ndarray[ALLINT] arr, cnp.ndarray[ALLINT] keys, cnp.n
796797
cdef size_t size = keys.size
797798
cdef ALLINT elem
798799

799-
if not in_place:
800+
if not in_place or not arr.flags.writeable:
800801
arr = np.copy(arr)
801802

802803
with nogil:

0 commit comments

Comments
 (0)