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.

ENH: helper for multidimensional slicing

See original GitHub issue

In thinking about #10771, I realized that a simple helper function like the following might be handy:

def ndslice(sl):
    """ convert a slice of iterables into a tuple of slices (nd slice)"""
    start = sl.start
    stop = sl.stop
    step = sl.step
    if start is None and stop is None and step is None:
        return ()
    if start is None:
        start = itertools.repeat(None)
    if stop is None:
        stop = itertools.repeat(None)
    if step is None:
        step = itertools.repeat(None)
    return tuple(
        slice(art, op, ep)
        for art, op, ep in zip(start, stop, step)
    )

Which could be used as:

a = np.zeros((10, 10, 10))
a[ndslice(np.s_[[1,2,3]:[4,5,6]])]  # same as a[1:4, 2:5, 3:6]

Or possibly using a __getitem__ method like np.s_[], sugared to:

class ndslice_type(object):
    __getitem__ = lambda self, item: ndslice(item)
ndslice_ = ndslice_type()

a[ndslice_[[1,2,3] : [4,5,6]]]  # same as a[1:4, 2:5, 3:6]

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
shoyercommented, May 14, 2018

You can’t combine ... with a tuple. So that would need to be:

sliding_window_view(arr, shape)[(Ellipsis,) + np.ndslice_[::step_tup]]
sliding_window_view(arr, shape)[np.ndslice_[::step_tup] + (Ellipsis,)]
0reactions
pvcommented, May 14, 2018

np.ndslice_[...,::step_tup]? Not sure what the semantics for ... are in ndslice though?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Numpy multidimensional array slicing - python - Stack Overflow
Now, I can get an array containing the (0,1) element of each 3x3 subarray with x[:, 0, 1] , which returns array([ 1,...
Read more >
Chapter 4. Arrays, slices, and maps - Go in Action
Array internals and fundamentals; Managing collections of data with slices; Working with key/value pairs using maps;
Read more >
Multidimensional arrays: initial values and slicing (#15) - GitLab
The purpose of this issue is to gather a few features related to multidimensional arrays, which I would expect to be supported but...
Read more >
Indexing and Slicing of 1D, 2D and 3D Arrays Using Numpy
Generate a two-dimensional array using arange and reshape function. I made a 6×7 matrix for this video. Because it is big enough to...
Read more >
Is There any way to fix Column Widths in a Matrix - Microsoft Power ...
Specifically, I have a matrix that has two Values per Column Item: ... please let me know) or is this an enhancement that...
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