From b54bd77873ab3dc0964517d99f094730884f034b Mon Sep 17 00:00:00 2001 From: Maksim Novikov Date: Thu, 16 Jan 2020 21:20:04 +0100 Subject: [PATCH] Stub to test remote fs zarr n5 backend --- .../ioOperators/opInputDataReader.py | 7 +++- .../ioOperators/opStreamingH5N5Reader.py | 41 ++++++++++++++++++- 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/lazyflow/operators/ioOperators/opInputDataReader.py b/lazyflow/operators/ioOperators/opInputDataReader.py index 39d12cdc3..ff1b4f06a 100644 --- a/lazyflow/operators/ioOperators/opInputDataReader.py +++ b/lazyflow/operators/ioOperators/opInputDataReader.py @@ -399,7 +399,12 @@ def _attemptOpenAsH5N5(self, filePath): ) raise OpInputDataReader.DatasetReadError(msg) try: - compression_setting = h5N5File[internalPath].compression + if internalPath.startswith("/"): + internalPath = internalPath.replace("/", "", 1) + + print(h5N5File, internalPath) + compression_setting = None + #compression_setting = h5N5File[internalPath].compression except Exception as e: h5N5File.close() msg = "Error reading H5/N5 File: {}\n{}".format(externalPath, e) diff --git a/lazyflow/operators/ioOperators/opStreamingH5N5Reader.py b/lazyflow/operators/ioOperators/opStreamingH5N5Reader.py index dd3c596da..aa3ae5abb 100644 --- a/lazyflow/operators/ioOperators/opStreamingH5N5Reader.py +++ b/lazyflow/operators/ioOperators/opStreamingH5N5Reader.py @@ -26,16 +26,51 @@ import vigra import h5py import z5py +import zarr import json import os import numpy as np +import requests from lazyflow.graph import Operator, InputSlot, OutputSlot from lazyflow.utility import Timer from lazyflow.utility.helpers import get_default_axisordering +from urllib.parse import urljoin logger = logging.getLogger(__name__) +class RemoteFS: + class RemoteFile: + def __init__(self, url): + self._url = url + + def read(self): + resp = requests.get(self._url) + resp.raise_for_status() + return resp.content + + def __enter__(self): + return self + + def __exit__(self, *args, **kwargs): + pass + + def isfile(self, path): + return not path.endswith("/") + + def isdir(self, path): + return path.endswith(".n5") + + def exists(self, path): + return True + + def __init__(self, url): + self._url = url + + def open(self, path, *args, **kwargs): + url = urljoin(f"{self._url}/", path) + return self.RemoteFile(url) + class OpStreamingH5N5Reader(Operator): """ @@ -155,6 +190,10 @@ def get_h5_n5_file(filepath, mode="a"): """ name, ext = os.path.splitext(filepath) if ext in OpStreamingH5N5Reader.N5EXTS: - return z5py.N5File(filepath, mode) + fs = RemoteFS("https://web.ilastik.org/data/datasources/eec5b03babebc54b64f6b31c3a87d92e/") + store = zarr.n5.N5Store("hbp-00173_262_902_1342__s26.n5", fs=fs) + f = zarr.group(store=store, overwrite=False) + f.close = lambda: None + return f elif ext in OpStreamingH5N5Reader.H5EXTS: return h5py.File(filepath, mode)