Last active
December 25, 2023 18:09
-
-
Save sfaleron/9791418d7023a9985bb803170c5d93d8 to your computer and use it in GitHub Desktop.
Immutable/hashable numpy Array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ISC License (ISC) | |
# | |
# Copyright 2021 Christopher Fuller | |
# | |
# Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, | |
# provided that the above copyright notice and this permission notice appear in all copies. | |
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL | |
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, | |
# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER | |
# IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR | |
# PERFORMANCE OF THIS SOFTWARE. | |
from numpy import ndarray, asarray | |
class ImmutArray(ndarray): | |
"""https://numpy.org/doc/stable/user/basics.subclassing.html | |
>>> import numpy as np | |
>>> a=np.arange(4) | |
>>> ia=ImmutArray(a) | |
>>> (ia==[0,1,2,3]).all().item() | |
True | |
>>> (ia==(0,1,2,3)).all().item() | |
True | |
>>> ((0,1,2,3)==ia).all().item() | |
True | |
>>> ([0,1,2,3]==ia).all().item() | |
True | |
>>> (ia==range(4)).all().item() | |
True | |
>>> (range(4)==ia).all().item() | |
True | |
>>> a[1]=-1 | |
>>> a | |
array([ 0, -1, 2, 3]) | |
>>> (a==ia).all().item() | |
False | |
>>> (ia==a).all().item() | |
False | |
>>> ia[1]=-1 | |
Traceback (most recent call last): | |
File "<stdin>", line 1, in <module> | |
ValueError: assignment destination is read-only | |
>>> ia | |
ImmutArray([0, 1, 2, 3]) | |
>>> (np.arange(4)==ia).all().item() | |
True | |
>>> (ia==np.arange(4)).all().item() | |
True | |
>>> ib=ia[1:3] | |
>>> ib | |
ImmutArray([1, 2]) | |
>>> ib[0]=9 | |
Traceback (most recent call last): | |
File "<stdin>", line 1, in <module> | |
ValueError: assignment destination is read-only | |
>>> ia=np.arange(4).view(ImmutArray) | |
>>> ia[0]=9 | |
Traceback (most recent call last): | |
File "<stdin>", line 1, in <module> | |
ValueError: assignment destination is read-only | |
""" | |
def __new__(cls, arr): | |
obj = asarray(arr).copy().view(cls) | |
obj.flags.writeable = False | |
obj._hash = cls._gethash(obj) | |
return obj | |
def __array_finalize__(self, obj): | |
if obj is None: | |
return | |
self._hash = getattr(obj, '_hash', None) or self._gethash(obj) | |
self.flags.writeable = False | |
@staticmethod | |
def _gethash(arr): | |
return hash((arr.shape, tuple(arr.flat))) | |
def __hash__(self): | |
return self._hash | |
__eq__ = ndarray.__eq__ | |
if __name__ == '__main__': | |
import doctest | |
results = doctest.testmod() | |
if results.attempted: | |
if results.failed: | |
print('Failure!') | |
else: | |
print('Success!') | |
else: | |
print('No tests found!') |
Dear @sfaleron
What is the license of this code?
I gravitate to APLv2, but for a single-file/snippet I may look for something less verbose. I'm a little shy about using public domain, there's some ambiguities and pitfalls there. Looks like I'll use https://opensource.org/licenses/ISC.
Copying the array doesn't work.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Previous version (rev2, only posted for about a week) implemented eq() with a scalar True/False result, rather than element-wise, as is usual for NumPy arrays.
This version delegates to the parent class, but a wrinkle arises: Some (or all) methods/ufuncs are returning scalars wrapped in the array type rather than the bare scalar, as would be expected, at least for the all() methods I use in the tests.
I'd like to clean that up, but I probably won't spend a lot of time on it.