Skip to content

Commit

Permalink
Save ndtiff image plane metadata per position (#204)
Browse files Browse the repository at this point in the history
* save ndtiff image plane metadata per position

* style
  • Loading branch information
ieivanov authored Nov 16, 2023
1 parent c0caa45 commit 37fddaf
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 15 deletions.
31 changes: 19 additions & 12 deletions iohub/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -462,8 +462,9 @@ def _convert_ndtiff(self):
"Converting Timepoints/Channels: |{bar:16}|{n_fmt}/{total_fmt} "
"(Time Remaining: {remaining}), {rate_fmt}{postfix}]"
)
all_ndtiff_metadata = {}
for p_idx in tqdm(range(self.p), bar_format=bar_format_positions):
position_image_plane_metadata = {}

# ndtiff_pos_idx, ndtiff_t_idx, and ndtiff_channel_idx
# may be None
ndtiff_pos_idx = (
Expand Down Expand Up @@ -541,19 +542,25 @@ def _convert_ndtiff(self):
ndtiff_channel_idx,
z_idx,
)
# row/well/fov/img/T/C/Z
# T/C/Z
frame_key = "/".join(
[zarr_arr.path]
+ [str(i) for i in (t_idx, c_idx, z_idx)]
[str(i) for i in (t_idx, c_idx, z_idx)]
)
all_ndtiff_metadata[frame_key] = image_metadata

logging.info("Writing ND-TIFF image plane metadata...")
with open(
os.path.join(self.output_dir, "image_plane_metadata.json"),
mode="x",
) as metadata_file:
json.dump(all_ndtiff_metadata, metadata_file, indent=4)
position_image_plane_metadata[frame_key] = image_metadata

logging.info("Writing ND-TIFF image plane metadata...")
# image plane metadata is save in
# output_dir/row/well/fov/img/image_plane_metadata.json,
# e.g. output_dir/A/1/FOV0/0/image_plane_metadata.json
with open(
os.path.join(
self.output_dir, zarr_arr.path, "image_plane_metadata.json"
),
mode="x",
) as metadata_file:
json.dump(
position_image_plane_metadata, metadata_file, indent=4
)

def run(self, check_image: bool = True):
"""Runs the conversion.
Expand Down
13 changes: 10 additions & 3 deletions tests/test_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,11 +194,18 @@ def test_converter_ndtiff(
for pos_name, pos in result.positions():
_check_scale_transform(pos, scale_voxels)
intensity += pos["0"][:].sum()
assert os.path.isfile(
os.path.join(
output, pos_name, "0", "image_plane_metadata.json"
)
)
assert intensity == raw_array.sum()
with open(os.path.join(output, "image_plane_metadata.json")) as f:
with open(
os.path.join(output, pos_name, "0", "image_plane_metadata.json")
) as f:
metadata = json.load(f)
assert len(metadata) == np.prod(raw_array.shape[:-2])
key = pos_name + "/0/0/0/0"
assert len(metadata) == np.prod(raw_array.shape[1:-2])
key = "0/0/0"
assert key in metadata
assert "ElapsedTime-ms" in metadata[key]

Expand Down

0 comments on commit 37fddaf

Please sign in to comment.