5

I use numpy.mgrid to generate "coordinate index arrays"

y, x = np.mgrid[0:3, 0:2]
print x
array([[0, 1],
       [0, 1],
       [0, 1]])

In many situations, I take some slice through these arrays (e.g. x[0, :]) and discard the rest of the data. Sometimes, these slices are much smaller than the original arrays, which are expensive to compute (i.e. np.mgrid[0:512, 0:512, 0:512]). Does numpy provide an equivalent to [coord[view] for coord in np.mgrid[0:512, 0:512, 0:512] that doesn't generate large intermediate arrays?

I realize the solution is trivial for the slice [0,:], but I'm looking for a general solution that handles any valid way to index numpy arrays

Edit

Some have asked for specific examples for what view might look like. Ideally, I'm hoping for a general solution that handles any valid way to index a ndarray. Here are a few specific examples for the 3x2 array above:

1) view = (1, slice(None, None, 2))

2) view = (np.array([0,1]), np.array([0, 1]))

3) view = np.array([[False, False], [False, True], [False, False]])

And I'm looking for a function like

def mgrid_with_view(array_shape, view)
    ...

That returns the equivalent of [o[view] for o in np.indices(array_shape)] without unnecessary computation or memory.

ChrisB
  • 4,628
  • 7
  • 29
  • 41
  • What is "view" in your final question? – Carl F. Aug 20 '12 at 00:57
  • "view" is, ideally, anything that can be used to index a numpy array. (a scalar, slice, tuples thereof, boolean or integer array, etc). For the example I provided, it would be `(0, slice(None, None, None)` – ChrisB Aug 20 '12 at 01:01
  • 1
    Can you give an example? I usually use np.ogrid to generate the cooridinate index arrays, and by using broadcasting, it can get the same result as np.mgrid. – HYRY Aug 20 '12 at 01:45

1 Answers1

1

As HYRY mentioned, I believe what you want to avoid is creating the full arrays. mgrid creates a full array, however if you use:

x, y = np.broadcast_arrays(*np.ogrid[0:2,0:3])

x and y take up no more memory then np.arange(0,2) (and np.arange(0,3)), while acting as if each was a full array. If you require a single large result array, you should probably slice these arrays individually and then concatenate them. (np.broadcast_arrays returns a tuple of arrays instead of an array)

seberg
  • 8,785
  • 2
  • 31
  • 30
  • 1
    Great, thank you. I guess the point here is that array broadcasting doesn't actually allocate memory to create full arrays, but rather constructs a view into another (potentially smaller) array. This lets you cheaply make large temporary arrays – ChrisB Aug 20 '12 at 15:14