def calc_new_physical_coords_array( physical_coordinates: xr.DataArray, stack_shape: Mapping[Indices, int], indexers: Mapping[str, Union[int, slice]], ) -> xr.DataArray: """Calculates the resulting coordinates array from indexing on each dimension in indexers Parameters ---------- physical_coordinates: xarray xarray that holds the min/max values of physical coordinates per tile (Round, Ch, Z). stack_shape : Dict[str, int] The shape of the image tensor by categorical index (channels, imaging rounds, z-layers) indexers : Dict[str, (int/slice)] A dictionary of dim:index where index is the value or range to index the dimension Returns ------- A coordinates xarray indexed by R, CH, V and values recalculated according to indexing on X/Y """ new_coords = physical_coordinates.copy(deep=True) # index by R, CH, V key = { Indices.ROUND.value: indexers[Indices.ROUND.value], Indices.CH.value: indexers[Indices.CH.value], Indices.Z.value: indexers[Indices.Z.value] } new_coords = indexing_utils.index_keep_dimensions(new_coords, key) # check if X or Y dimension indexed, if so recalculate physcal coordinate min/max values if _needs_coords_recalculating(indexers[Indices.X.value], indexers[Indices.Y.value]): _recalculate_physical_coordinate_ranges(stack_shape, indexers, new_coords) return new_coords
def sel(self, indexers: Mapping[Indices, Union[int]]): """Given a dictionary mapping the index name to either a value or a slice range, return an Imagestack with each dimension indexed accordingly Parameters ---------- indexers : Dict[Indices, (int/tuple)] A dictionary of dim:index where index is the value or range to index the dimension Examples -------- Create an Imagestack using the ``synthetic_stack`` method:: >>> from starfish import ImageStack >>> from starfish.types import Indices >>> stack = ImageStack.synthetic_stack(5, 5, 15, 200, 200) >>> stack <starfish.ImageStack (r: 5, c: 5, z: 15, y: 200, x: 200)> >>> stack.sel({Indices.ROUND: (1, None), Indices.CH: 0, Indices.Z: 0}) <starfish.ImageStack (r: 4, c: 1, z: 1, y: 200, x: 200)> >>> stack.sel({Indices.ROUND: 0, Indices.CH: 0, Indices.Z: 1, ...Indices.Y: 100, Indices.X: (None, 100)}) <starfish.ImageStack (r: 1, c: 1, z: 1, y: 1, x: 100)> and the imagestack's physical coordinates xarray also indexed and recalculated according to the x,y slicing. Returns ------- ImageStack : a new image stack indexed by given value or range. """ # convert indexers to Dict[str, (int/slice)] format formatted_indexers = indexing_utils.convert_to_indexers_dict(indexers) indexed_data = indexing_utils.index_keep_dimensions( self.xarray, formatted_indexers) stack = self.from_numpy_array(indexed_data.data) # set coords on new stack stack._coordinates = physical_coordinate_calculator.calc_new_physical_coords_array( self._coordinates, self.shape, formatted_indexers) return stack
def sel(self, indexers: Mapping[Axes, Union[int, tuple]]): """Given a dictionary mapping the index name to either a value or a range represented as a tuple, return an Imagestack with each dimension indexed accordingly Parameters ---------- indexers : Dict[Axes, (int/tuple)] A dictionary of dim:index where index is the value or range to index the dimension Examples -------- Create an Imagestack using the ``synthetic_stack`` method >>> from starfish import ImageStack >>> from starfish.types import Axes >>> stack = ImageStack.synthetic_stack(5, 5, 15, 200, 200) >>> stack <starfish.ImageStack (r: 5, c: 5, z: 15, y: 200, x: 200)> >>> stack.sel({Axes.ROUND: (1, None), Axes.CH: 0, Axes.ZPLANE: 0}) <starfish.ImageStack (r: 4, c: 1, z: 1, y: 200, x: 200)> >>> stack.sel({Axes.ROUND: 0, Axes.CH: 0, Axes.ZPLANE: 1, ...Axes.Y: 100, Axes.X: (None, 100)}) <starfish.ImageStack (r: 1, c: 1, z: 1, y: 1, x: 100)> and the imagestack's physical coordinates xarray also indexed and recalculated according to the x,y slicing. Returns ------- ImageStack : a new image stack indexed by given value or range. """ # convert indexers to Dict[str, (int/slice)] format # TODO shanaxel42 check if this can be changed to xarray.copy(deep=false) stack = deepcopy(self) selector = indexing_utils.convert_to_selector(indexers) stack._data._data = indexing_utils.index_keep_dimensions( self.xarray, selector) return stack