Beispiel #1
0
 def tryLoad(cls, filesystem: JsonableFilesystem,
             path: PurePosixPath) -> "PrecomputedChunksInfo | Exception":
     url = filesystem.geturl(path.as_posix())
     if not filesystem.exists(path.as_posix()):
         return FileNotFoundError(f"Could not find info file at {url}")
     with filesystem.openbin(path.as_posix(), "r") as f:
         try:
             info_json = f.read().decode("utf8")
             return PrecomputedChunksInfo.from_json_value(
                 json.loads(info_json))
         except Exception:
             return ValueError(
                 f"Could not interpret json info file at {url}")
Beispiel #2
0
 def try_to_datasource(
     self,
     *,
     ilp_fs: JsonableFilesystem,
     ilp_path: PurePosixPath,
     allowed_protocols: Sequence[Protocol] = (Protocol.HTTP, Protocol.HTTPS)
 ) -> "FsDataSource | Exception":
     url = Url.parse(self.filePath)
     if url is None: # filePath was probably a path, not an URL
         path = ilp_path.parent.joinpath(self.filePath)
         url = Url.parse(ilp_fs.geturl(path.as_posix()))
     if url is None:
         return Exception(f"Could not parse {self.filePath} as URL")
     datasources_result = try_get_datasources_from_url(url=url, allowed_protocols=allowed_protocols)
     if isinstance(datasources_result, Exception):
         return Exception(f"Could not open {url} as a data source: {datasources_result}")
     if len(datasources_result) != 1:
         return Exception(f"Expected a single datasource from {url}, found {len(datasources_result)}")
     return datasources_result[0]
Beispiel #3
0
 def __init__(self,
              *,
              outer_path: PurePosixPath,
              inner_path: PurePosixPath,
              location: Point5D = Point5D.zero(),
              filesystem: JsonableFilesystem,
              spatial_resolution: Optional[Tuple[int, int, int]] = None):
     self.outer_path = outer_path
     self.inner_path = inner_path
     self.filesystem = filesystem
     binfile = filesystem.openbin(outer_path.as_posix())
     # FIXME: h5py might not like this if the filesystem isn't OSFS
     f = h5py.File(binfile, "r")  #type: ignore
     try:
         dataset = f[inner_path.as_posix()]
         if not isinstance(dataset, h5py.Dataset):
             raise ValueError(f"{inner_path} is not a Dataset")
         self.axiskeys = self.getAxisKeys(dataset)
         self._dataset = dataset
         tile_shape = Shape5D.create(raw_shape=self._dataset.chunks
                                     or self._dataset.shape,
                                     axiskeys=self.axiskeys)
         base_url = Url.parse(filesystem.geturl(outer_path.as_posix()))
         assert base_url is not None
         super().__init__(
             c_axiskeys_on_disk=self.axiskeys,
             tile_shape=tile_shape,
             interval=Shape5D.create(
                 raw_shape=self._dataset.shape,
                 axiskeys=self.axiskeys).to_interval5d(location),
             dtype=self._dataset.dtype,
             spatial_resolution=spatial_resolution or (1, 1, 1),  # FIXME
             filesystem=filesystem,
             path=self.outer_path)
     except Exception as e:
         f.close()
         raise e