question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

feature request: Consider adding a strides method to np.dtype

See original GitHub issue

See here.

It would do something like this:

def strides_from_dtype(dtype):
    shape = list(dtype.shape)

    # Make the strides for an array with an itemsize of 1 in C-order.
    tmp_strides = shape[::-1]
    tmp_strides[1:] = list(np.cumprod(tmp_strides[:-1]))
    tmp_strides[0] = 1

    # Adjust it for the real itemsize.
    tmp_strides = dtype.base.itemsize * np.array(tmp_strides)

    # Convert it to a tuple, reversing it back for proper C-order.
    return tuple(tmp_strides[::-1])

This works, but it seems inefficient:

strides = np.zeros((), dtype=dtype).strides

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:29 (29 by maintainers)

github_iconTop GitHub Comments

1reaction
eric-wiesercommented, Apr 18, 2018

@njsmith: I see where you’re coming from, but I think we should explicitly support this:

  • I think it’s in our interest to preserve strides / offsets in some_real_array[empty_slice], to allow them to be used similarly to C++ iterators / pointers
  • Given the above, I can’t see a reason to special case empty-array creation vs empty array views

Regarding my post above - here’s an implementation of some syntactic sugar to solve your problem https://gist.github.com/eric-wieser/d8790dede1c9311c5670018ee1cee83a

With this, the offset and strides for a['time']['subtime'][2] can be gotten with just

field_info(a.dtype)['time']['subtime'][2].offset
1reaction
eric-wiesercommented, Apr 18, 2018

Thanks for the inspiration, @njsmith: here’s a neat solution I came up with

def address_of(x):
    assert isinstance(x, np.ndarray)  # make sure we don't get a scalar here, which will not be a view
    return x.__array_interface__['data'][0]

dt = np.dtype([('time', [('subtime', np.int32, (30,20))], 200), ('value', np.float32, (10,2))])
a = np.empty((0,), dtype=dt)  # constant size, independent of the array size

offset = address_of(x['time'][...,100]['subtime'][...,10,1]) - address_of(a)
# 240804
Read more comments on GitHub >

github_iconTop Results From Across the Web

Support for datetime[*] numpy dtype - scikit-hep/awkward
To work around this, we'd have to detect it with array.dtype().kind() == 'M' and then get the data pointer, shape, strides, itemsize, and...
Read more >
numpy.ndarray.strides — NumPy v1.24 Manual
The strides of an array tell us how many bytes we have to skip in memory to move to the next position along...
Read more >
Advanced NumPy: Master stride tricks with 25 illustrated ...
The answer — the strides and shape to be used as parameters in numpy.lib.stride_tricks.as_strided to achieve the final NumPy view. The explanation; The...
Read more >
Supported NumPy features - Numba
NumPy arrays provide an efficient storage method for homogeneous sets of data. NumPy dtypes provide type information useful when compiling, and the regular, ......
Read more >
Typed Memoryviews — Cython 3.0.0a11 documentation
Memory views use Python slicing syntax in a similar way as NumPy. ... To use a memory view on a numpy array with...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found