NumSharp icon indicating copy to clipboard operation
NumSharp copied to clipboard

Mixing indices and slices in NDArray[...]

Open Oceania2018 opened this issue 6 years ago • 11 comments

Is it possible to suppor mixed index/ slice? This is what numpy doing:

image

Currently, it doesn't support:

image

Oceania2018 avatar Oct 06 '19 03:10 Oceania2018

label[i][Slice.Index(yind), Slice.Index(xind), Slice.Index(iou_mast), new Slice(0, 4)] = bbox_xywh;

Nucs avatar Oct 06 '19 06:10 Nucs

or

label[i][$"{yind}, {xind}, {iou_mast}, 0:4"] = bbox_xywh;

Btw: we could try implicitly convert from int to Slice.Index(int)

henon avatar Oct 06 '19 11:10 henon

Worth mentioning that using a string inside a loop or O(n) will affect performance. Regex parsing is not lightning fast.

Nucs avatar Oct 06 '19 12:10 Nucs

The iou_mask is NDArray. image

It means we use different ways in different dimension to select elements.

One approach my idea is: Define a IIndex interface, NDArray and Slice implement IIndex. update

NDArray this[params IIndex[] selectors]
{
   get; set;
}

Oceania2018 avatar Oct 06 '19 13:10 Oceania2018

Too messy, plus it won't solve your problem.. you can't implement implicit cast from int to IIndex. Having index nd[int, int, NDArray, Slice] doesn't make sense. Our algorithm accepts either only slices or only ints. nd[NDArray[]] is not for Indexing, it is for Masking. Right now use Slice.Index, in the future I'll implement implicit castings to Slice.

Nucs avatar Oct 06 '19 14:10 Nucs

We might have to extend the slicing algorithm to accept a mask (i.e. Slice.Mask(boolean_array)). He is porting Python code so it seems Python allows mixing of indices and masks.

henon avatar Oct 06 '19 15:10 henon

Masking has a complete separate algorithm from slicing. In fact they don't have anything in common (function-wise).

Nucs avatar Oct 06 '19 15:10 Nucs

Not only for masking, but also for any 1d array. We don’t have to have int to IIndex implicitly, we can use np.array(1) as alternative.

The goal is having people can use 1d or Slice, Scalar to select and mask elements in appropriate dimensions.

Oceania2018 avatar Oct 06 '19 16:10 Oceania2018

@henon This situation doesn't work:

iou_mask is NDArray, yind and xind are int: image

image

Oceania2018 avatar Oct 12 '19 00:10 Oceania2018

I know, @Nucs is removing the int[] indexer in favor of an object[] indexer so that implicit conversion operators don't get in the way and we can reliably forward the different use cases into the right implementatons ( slicing if no NDarrays are part of the params and index extraction/masking otherwise)

henon avatar Oct 12 '19 08:10 henon

@Oceania2018 Please verify if it works on master branch.

Nucs avatar Oct 12 '19 11:10 Nucs