Slice#

This module provides basic handling of slicing of sources.

The main functionality is to virtually slice an array and return its expected shape and order. Virtual slices can also be used as handles with low communication overhead in parallel processing.

Example

>>> import numpy as np
>>> import ClearMap.IO.IO as io
>>> source = io.source(np.random.rand(30,40))
>>> sliced = io.slc.Slice(source, slicing=(slice(None), slice(10,20)))
>>> sliced
Sliced-Numpy-Source(30, 10)[float64]
>>> sliced.base
Numpy-Source(30, 40)[float64]|C|
class Slice(source=None, slicing=None, name=None)[source]#

Bases: Source

A virtual slice of a source.

as_buffer()[source]#
as_real()[source]#
as_virtual()[source]#

Returns a virtual handle to this source striped of any big array data useful for parallel processing.

Returns

sourceSource class

The source class with out any cached array data.

Note

The slicing structure is kept here to be able to appropiately take slices in a source when processsing in parallel.

property array#

The sliced array.

Returns

arrayarray

An array representing the slice, if the underling source has an slicable array property, else None.

property base#

The underlying base source in case of nested slicing.

Returns

sourceSource

Returns the underlying base source of this sliced source.

property base_lower#

Returns the indices of the lower corner of this slice in the underlying base source.

Returns

positiontuple of int

The coordinates of the lower corner of this slice within the base source.

property base_position#

Returns the indices of the lower corner of this slice in the underlying base source.

Returns

positiontuple of int

The coordinates of the lower corner of this slice within the base source.

property base_shape#

The shape of the underlying base source in case of nested slicing.

Returns

shapetuple

Returns the shape for the base of this source.

property base_slicing#

The direct slicing specification of the underlying base source in case of nested slicing.

Returns

slicingtuple

Returns the slice spcification for the base of this source.

property base_upper#

Returns the indices of the upper corner of this slice in the underlying base source.

Returns

positiontuple of int

The coordinates of the upper corner of this slice within the base source.

property dtype#

The data type of the source.

Returns

dtypedtype

The data type of the source.

property element_strides#

The strides of the data source.

Returns

stridestuple of ints

The strides of the sliced source.

property location#

The location of the source’s data.

Returns

locationstr

Returns the location of the data source or None if there is none.

property lower#

Returns the indices of the lower corner of this slice in the underlying source.

Returns

positiontuple of int

The coordinates of the lower corner of this slice within the source.

property name#

The name of this source.

Returns

namestr

Name of this source.

property offset#

The offset of the memory map in the file.

Returns

offsetint

Offset of the memeory map in the file.

property order#

The order of how the data is stored in the source.

Returns

orderstr

Returns ‘C’ for C contigous and ‘F’ for fortran contigous, None otherwise.

property position#

Returns the indices of the lower corner of this slice in the underlying source.

Returns

positiontuple of int

The coordinates of the lower corner of this slice within the source.

property shape#

The shape of the source.

Returns

shapetuple

The shape of the source.

property slicing#

The slice spcification of the source.

Returns

slicingslice, list or None

Returns the slice specification of this sliced source.

property source#

The source of this sliced source.

Returns

sourceSource class

The source of this slice.

property strides#

The strides of the data source.

Returns

stridestuple of ints

The strides of the sliced source.

property unpacked_slicing#

The unpacked slicing specification.

Returns

slicingtuple

Returns the unpacked slice spcification of this sliced source.

property upper#

Returns the indices of the upper corner of this slice in the underlying source.

Returns

positiontuple of int

The coordinates of the upper corner of this slice within the source.

is_trivial(slicing)[source]#

Returns True if the slicing is not generating a real sub-slice.

Arguments

slicingobject

The slice specification.

shapetuple of ints or None.

If not None assume this shape for the array to be sliced.

Returns

is_trivialbool

True if the sliced array is changed form the original one.

is_view(slicing)[source]#

Returns True if the slicing results in a view of the original array.

Arguments

slicingobject

The slice specification.

ndimint

The dimension of the source to slice.

Returns

is_viewbool

True if the sliced array is a view.

simplify_slicing(slicing, ndim=None)[source]#

Simplifies slice specification to avoid fancy indexing if possible.

Arguments

slicingobject

The slice specification.

ndimint

The dimension of the source to slice.

Returns

slicingobject

The full slice specification.

slice_to_range(slicing, shape=None)[source]#

Transforms a slice object to a range.

Arguments

slicingslice

A sinlge slice class.

shapetupe of ints or None

The shape of the source, if None, try to determine from slice.

Returns

rangerange

The range corrresponding to the slice.

sliced_ndim(slicing, ndim, allow_index_arrays=False)[source]#

Returns the dimension of a slicing of an array with given dimension.

Arguments

slicingobject

Slice specification.

ndimint

Diemnsion of the array.

Returns

ndimint

The dimension of the sliced array.

sliced_offset(slicing, strides, shape=None, allow_index_arrays=False)[source]#

Returns the offset to the first element of the slicing into a buffer with given strides.

Arguments

slicingobject

Slice specification.

stridestuple

Strides of the array.

Returns

offsetint

Offset into the sliced array.

sliced_order(slicing, order, shape, allow_index_arrays=False)[source]#

Returns the contiguous order of a sliced array.

Arguments

slicingobject

The slice specification.

order‘C’, ‘F’ or None

The order of the source to be sliced.

shapetuple of ints

The shape of the source to be sliced.

Returns

order‘C’, ‘F’ or None

The order of the sliced source.

sliced_reduction(slicing, ndim, allow_index_arrays=False)[source]#

Returns a slicing that slices a list retaining only full dimensions in the slice.

Arguments

slicingobject

The slice specification.

ndimint

The dinension of the source.

Returns

slicingobject

Slice specification that reduces a list of length ndim to the new dimensions of the slice.

sliced_shape(slicing, shape, allow_index_arrays=False)[source]#

Returns the shape that results from slicing.

Arguments

slicingobject

Slice specification.

shapetuple

Shape of the original array.

Returns

shapetuple

The shape of the sliced array.

Note

Fancy indexing is not supported.

sliced_slicing(slicing_second, slicing_first, shape, allow_index_arrays=False)[source]#

Returns a slicing of a slicing if possible.

Arguments

slicing_secondobject

Slice specification followed by first slicing.

slicing_firstobject

First slicing.

shapetuple of ints

Shape of the original source to be sliced twice.

Returns

slicingobject

The reduced slicing.

sliced_start(slicing, shape, allow_index_arrays=False)[source]#

Returns the starting position of the slicing in the original source.

Arguments

slicingobject

Slice specification.

shapetuple

Shape of the array.

Returns

starttuple of int

Start position of the slicing in the original source.

sliced_strides(slicing, strides)[source]#

Returns the strides of the slicing of a buffer with given strides if possible.

Arguments

slicingobject

Slice specification.

stridestuple

Strides of the original array.

Returns

stridestuple

Strides into the sliced array.

unpack_slicing(slicing, ndim)[source]#

Convert slice specification to a slice specification that matches the dimension of the sliced array.

Arguments

slicingobject

The slice specification.

ndimint

The dimension of the source to slice.

Returns

slicingobject

The full slice specification.

allow_index_arrays = False#

Default value to allow index arrays in slicing.

Note

If True, allows indexing to also be integer or boolean arrays. In numpy this type of indexing triggers copying of the array and the sliced array is not a view into the original array.