Skip to content

Commit

Permalink
Permit writing of images to hierarchical on-disk representations. (#87)
Browse files Browse the repository at this point in the history
Rather than writing the basename of a referenced file, calculate the relative path from the current file and record that.  Adds a test to verify that such behavior works.

This is the last piece of slicedimage work necessary for spacetx/starfish#1116
  • Loading branch information
ttung authored Apr 3, 2019
1 parent 2b4fad2 commit 3c2e6b8
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 2 deletions.
6 changes: 4 additions & 2 deletions slicedimage/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,8 @@ def generate_partition_document(
tile_opener=tile_opener,
tile_format=tile_format,
)
json_doc[CollectionKeys.CONTENTS][partition_name] = partition_path.name
json_doc[CollectionKeys.CONTENTS][partition_name] = str(
partition_path.relative_to(path.parent))
return json_doc
elif isinstance(partition, TileSet):
json_doc[TileSetKeys.DIMENSIONS] = tuple(partition.dimensions)
Expand Down Expand Up @@ -311,7 +312,8 @@ def generate_partition_document(

buffer_fh.seek(0)
tile_fh.write(buffer_fh.read())
tiledoc[TileKeys.FILE] = tile_fh.name
tiledoc[TileKeys.FILE] = str(
pathlib.Path(tile_fh.name).relative_to(path.parent))

if tile.tile_shape is not None:
tiledoc[TileKeys.TILE_SHAPE] = \
Expand Down
72 changes: 72 additions & 0 deletions tests/io/v1_0_0/test_write.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,78 @@ def test_write_tiff(self):
self.assertEqual(tiles[0].numpy_array.all(), expected.all())
self.assertIsNotNone(tiles[0].sha256)

def test_multi_directory_write_collection(self):
"""Test that we can write collections with a directory hierarchy."""
image = slicedimage.TileSet(
["x", "y", "ch", "hyb"],
{'ch': 2, 'hyb': 2},
{'y': 120, 'x': 80},
)

for hyb in range(2):
for ch in range(2):
tile = slicedimage.Tile(
{
'x': (0.0, 0.01),
'y': (0.0, 0.01),
},
{
'hyb': hyb,
'ch': ch,
},
)
tile.numpy_array = np.zeros((120, 80))
tile.numpy_array[hyb, ch] = 1
image.add_tile(tile)
collection = slicedimage.Collection()
collection.add_partition("fov002", image)

def partition_path_generator(parent_toc_path, toc_name):
directory = parent_toc_path.parent / toc_name
directory.mkdir()
return directory / "{}.json".format(parent_toc_path.stem)

def tile_opener(tileset_path, tile, ext):
directory_path = tempfile.mkdtemp(dir=str(tileset_path.parent))
return tempfile.NamedTemporaryFile(
suffix=".{}".format(ext),
prefix="{}-".format(tileset_path.stem),
dir=directory_path,
delete=False,
)

with TemporaryDirectory() as tempdir, \
tempfile.NamedTemporaryFile(suffix=".json", dir=tempdir) as partition_file:
partition_doc = slicedimage.v0_0_0.Writer().generate_partition_document(
collection, partition_file.name,
partition_path_generator=partition_path_generator,
tile_opener=tile_opener,
)
writer = codecs.getwriter("utf-8")
json.dump(partition_doc, writer(partition_file))
partition_file.flush()

basename = os.path.basename(partition_file.name)
baseurl = "file://{}".format(os.path.dirname(partition_file.name))

loaded = slicedimage.Reader.parse_doc(basename, baseurl)

for hyb in range(2):
for ch in range(2):
tiles = [
_tile
for _tile in loaded.tiles(
lambda tile: (tile.indices['hyb'] == hyb and
tile.indices['ch'] == ch))]

self.assertEqual(len(tiles), 1)

expected = np.zeros((100, 100))
expected[hyb, ch] = 1

self.assertEqual(tiles[0].numpy_array.all(), expected.all())
self.assertIsNotNone(tiles[0].sha256)


if __name__ == "__main__":
unittest.main()

0 comments on commit 3c2e6b8

Please sign in to comment.