Skip to content

Commit

Permalink
Avoid use of _tile_data.keys()
Browse files Browse the repository at this point in the history
`_tile_data.keys()` returns tiles outside the scope of the imagestack if it has been subselected.  Instead, use `_iter_axes(..)`, which uses the actual xarray coordinates to determine which tiles exist.

Add a test that selects and exports an imagestack.

Depends on #1189
Fixes #1154
  • Loading branch information
Tony Tung committed Apr 18, 2019
1 parent a4645e6 commit 561135c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
6 changes: 5 additions & 1 deletion starfish/imagestack/imagestack.py
Original file line number Diff line number Diff line change
Expand Up @@ -1072,7 +1072,11 @@ def export(self,
default_tile_shape={Axes.Y: self.tile_shape[0], Axes.X: self.tile_shape[1]},
extras=self._tile_data.extras,
)
for tilekey in self._tile_data.keys():
for axis_val_map in self._iter_axes({Axes.ROUND, Axes.CH, Axes.ZPLANE}):
tilekey = TileKey(
round=axis_val_map[Axes.ROUND],
ch=axis_val_map[Axes.CH],
zplane=axis_val_map[Axes.ZPLANE])
round_, ch, zplane = tilekey.round, tilekey.ch, tilekey.z
extras: dict = self._tile_data[tilekey]

Expand Down
28 changes: 28 additions & 0 deletions starfish/imagestack/test/test_index.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import os
import tempfile
from collections import OrderedDict

import numpy as np

from starfish import ImageStack
from starfish.imagestack import indexing_utils as iu
from starfish.types import Axes, Coordinates, PhysicalCoordinateTypes
from .factories import imagestack_with_coords_factory, synthetic_stack
Expand Down Expand Up @@ -225,3 +230,26 @@ def test_nonindexed_dimensions_restored():
(Axes.ZPLANE, Coordinates.Z),
):
assert len(sel_xarray[primary_axis.value]) == len(sel_xarray[dependent_axis.value])


def test_select_and_export():
"""Tests selecting on an Imagestack with a shape (5, 5, 15, 200, 200)
1.) stack.sel(indexers)
2.) export stack
"""
stack = synthetic_stack(
num_round=5, num_ch=5, num_z=15, tile_height=200, tile_width=200)

# select on range of rounds and single ch and Z
selected = stack.sel({Axes.ROUND: (1, None), Axes.CH: (2, 3), Axes.ZPLANE: 0})

with tempfile.TemporaryDirectory() as tfd:
path = os.path.join(tfd, "stack.json")
selected.export(path)

loaded = ImageStack.from_path_or_url(path)

assert np.array_equal(selected.xarray, loaded.xarray)
for coords in (
Axes.ROUND, Axes.CH, Axes.ZPLANE, Coordinates.X, Coordinates.Y, Coordinates.Z):
assert np.allclose(selected.xarray[coords.value], loaded.xarray[coords.value])

0 comments on commit 561135c

Please sign in to comment.