Skip to content

Commit

Permalink
Multiscale: Support project files containing an integer working_scale
Browse files Browse the repository at this point in the history
All project files created with ilastik1.4.1b13 break otherwise
  • Loading branch information
btbest committed Feb 19, 2024
1 parent efaee7e commit 1e6c376
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
24 changes: 18 additions & 6 deletions ilastik/applets/dataSelection/opDataSelection.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,20 @@ def from_h5_group(cls, data: h5py.Group, params: Dict = None):
if "display_mode" in data:
params["display_mode"] = data["display_mode"][()].decode("utf-8")
if "working_scale" in data:
# This raises if working_scale is an int,
# which would be the case if anyone actually created a Precomputed project with ilastik1.4.1b13
params["working_scale"] = data["working_scale"][()].decode("utf-8")
try:
params["working_scale"] = data["working_scale"][()].decode("utf-8")
except AttributeError:
# Support for projects created with ilastik 1.4.1b13
saved_scale = int(data["working_scale"][()])
if saved_scale == 0: # 0 by default (in all project files).
params["working_scale"] = DEFAULT_LOWEST_SCALE_KEY
elif saved_scale > 0: # Precomputed dataset with non-default scale selected
from lazyflow.utility.io_util.RESTfulPrecomputedChunkedVolume import (
RESTfulPrecomputedChunkedVolume as Precomputed,
)

url = params["url"] # Should have url in this case
params["working_scale"] = Precomputed.fetch_scale_key(url, saved_scale)
if "scale_locked" in data:
params["scale_locked"] = bool(data["scale_locked"][()])
return cls(**params)
Expand Down Expand Up @@ -587,12 +598,13 @@ def from_h5_group(cls, group: h5py.Group, params: Dict = None):
"""
Returns a deserialized DatasetInfo dict equivalent to a dict from a MultiscaleUrlDatasetInfo
"""
from lazyflow.utility.io_util.RESTfulPrecomputedChunkedVolume import RESTfulPrecomputedChunkedVolume
from lazyflow.utility.io_util.RESTfulPrecomputedChunkedVolume import (
RESTfulPrecomputedChunkedVolume as Precomputed,
)

deserialized = super().from_h5_group(group)
remote_source = RESTfulPrecomputedChunkedVolume(deserialized.url.lstrip("precomputed://"))
deserialized.nickname = cls._nickname_from_url(deserialized.nickname)
deserialized.working_scale = remote_source.highest_resolution_key
deserialized.working_scale = Precomputed.fetch_scale_key(deserialized.url, "highest")
deserialized.scale_locked = True
return deserialized

Expand Down
14 changes: 14 additions & 0 deletions lazyflow/utility/io_util/RESTfulPrecomputedChunkedVolume.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
###############################################################################
import json
import logging
from typing import Union, Literal

import jsonschema
import numpy
Expand Down Expand Up @@ -227,6 +228,19 @@ def generate_url(self, block_coordinates, scale=DEFAULT_LOWEST_SCALE_KEY):
url = f"{base_url}/{scale}/{min_x}-{max_x}_{min_y}-{max_y}_{min_z}-{max_z}"
return url, downloaded_block_shape

@staticmethod
def fetch_scale_key(url: str, scale_index: Union[int, Literal["highest"]]):
"""Support for legacy projects with integer scale indexing.
Requests the info file and returns the key of the scale that would have corresponded
to the index in legacy versions (i.e. index in the reversed list)."""
remote_source = RESTfulPrecomputedChunkedVolume(url.lstrip("precomputed://"))
if scale_index == "highest":
return remote_source.highest_resolution_key
else:
assert isinstance(scale_index, int)
scales = [s for s in reversed(remote_source._json_info["scales"])]
return scales[scale_index]["key"]


if __name__ == "__main__":
logging.basicConfig(level=logging.DEBUG)
Expand Down

0 comments on commit 1e6c376

Please sign in to comment.