Skip to content

BUG: Calling squeeze on a slice of a memmap raises cryptic error #25278

@WardBrian

Description

@WardBrian

Describe the issue:

Calling .squeeze() on a slice of a memmap that has shape (1,) raises

RuntimeError: ndarray subclass __array_wrap__ method returned an object which was not an instance of an ndarray subclass

Reproduce the code example:

import numpy as np
import struct

# set up file
filename = 'ex.bin'
with open(filename, 'wb') as f:
    f.write(bytearray(struct.pack('d', 3.14)))
    f.write(bytearray(struct.pack('d', 6.28)))

# mmap file
arr = np.memmap(filename, dtype=np.float64, mode='r', shape=(2,1))

print(arr)
print(arr.squeeze()) # works

print(arr[1:2,0])
print(np.array(arr)[1:2,0].squeeze()) # works
print(arr[1:2,0].squeeze()) # fails

Error message:

Traceback (most recent call last):
  File "/home/brian/Dev/cpp/ffistan/repr.py", line 18, in <module>
    print(arr[1:2,0].squeeze()) # fails
RuntimeError: ndarray subclass __array_wrap__ method returned an object which was not an instance of an ndarray subclass

Runtime information:

>>> import sys, numpy; print(numpy.__version__); print(sys.version)
1.26.2
3.9.13 | packaged by conda-forge | (main, May 27 2022, 16:56:21) 
[GCC 10.3.0]
>>> print(numpy.show_runtime())
WARNING: `threadpoolctl` not found in system! Install it by `pip install threadpoolctl`. Once installed, try `np.show_runtime` again for more detailed build information
[{'numpy_version': '1.26.2',
  'python': '3.9.13 | packaged by conda-forge | (main, May 27 2022, '
            '16:56:21) \n'
            '[GCC 10.3.0]',
  'uname': uname_result(system='Linux', node='FlatTop', release='6.2.0-37-generic', version='#38~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Thu Nov  2 18:01:13 UTC 2', machine='x86_64')},
 {'simd_extensions': {'baseline': ['SSE', 'SSE2', 'SSE3'],
                      'found': ['SSSE3',
                                'SSE41',
                                'POPCNT',
                                'SSE42',
                                'AVX',
                                'F16C',
                                'FMA3',
                                'AVX2',
                                'AVX512F',
                                'AVX512CD',
                                'AVX512_SKX',
                                'AVX512_CLX',
                                'AVX512_CNL',
                                'AVX512_ICL'],
                      'not_found': ['AVX512_KNL', 'AVX512_KNM']}}]
None

Context for the issue:

This operation seems well-defined for normal arrays but fails for memmaps. This violates the sentence The memmap object can be used anywhere an ndarray is accepted. in the documentation, and makes it difficult to write a library which relies on squeeze in a generic way which is ambivalent to whether the user provided an array or memmap

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions