From 793ce5d7438e26e78753199c3586013e60cdac2f Mon Sep 17 00:00:00 2001 From: pswain <peter.swain@ed.ac.uk> Date: Fri, 20 Oct 2023 11:30:59 +0100 Subject: [PATCH] changed load_attributes to load_meta --- src/agora/abc.py | 2 +- src/agora/io/reader.py | 4 ++-- src/agora/io/writer.py | 10 ++++++---- src/aliby/io/omero.py | 8 +++----- src/extraction/core/extractor.py | 11 ++++------- 5 files changed, 16 insertions(+), 19 deletions(-) diff --git a/src/agora/abc.py b/src/agora/abc.py index 6bdb93b8..928c1709 100644 --- a/src/agora/abc.py +++ b/src/agora/abc.py @@ -190,8 +190,8 @@ class ProcessABC(ABC): """ self._parameters = parameters # convert parameters to dictionary - # and then define each parameter as an attribute for k, v in parameters.to_dict().items(): + # define each parameter as an attribute setattr(self, k, v) @property diff --git a/src/agora/io/reader.py b/src/agora/io/reader.py index b6793483..8f0410a4 100644 --- a/src/agora/io/reader.py +++ b/src/agora/io/reader.py @@ -5,7 +5,7 @@ import h5py import numpy as np from agora.io.bridge import groupsort -from agora.io.writer import load_attributes +from agora.io.writer import load_meta class DynamicReader: @@ -13,7 +13,7 @@ class DynamicReader: def __init__(self, file: str): self.file = file - self.metadata = load_attributes(file) + self.metadata = load_meta(file) class StateReader(DynamicReader): diff --git a/src/agora/io/writer.py b/src/agora/io/writer.py index 0913308f..768fcdab 100644 --- a/src/agora/io/writer.py +++ b/src/agora/io/writer.py @@ -15,9 +15,10 @@ from agora.io.bridge import BridgeH5 #################### Dynamic version ################################## -def load_attributes(file: str, group="/"): +def load_meta(file: str, group="/"): """ - Load the metadata from an h5 file and convert to a dictionary, including the "parameters" field which is stored as YAML. + Load the metadata from an h5 file and convert to a dictionary, including + the "parameters" field which is stored as YAML. Parameters ---------- @@ -26,8 +27,9 @@ def load_attributes(file: str, group="/"): group: str, optional The group in the h5 file from which to read the data """ - # load the metadata, stored as attributes, from the h5 file and return as a dictionary + # load the metadata, stored as attributes, from the h5 file with h5py.File(file, "r") as f: + # return as a dict meta = dict(f[group].attrs.items()) if "parameters" in meta: # convert from yaml format into dict @@ -51,7 +53,7 @@ class DynamicWriter: self.file = file # the metadata is stored as attributes in the h5 file if Path(file).exists(): - self.metadata = load_attributes(file) + self.metadata = load_meta(file) def _log(self, message: str, level: str = "warn"): # Log messages in the corresponding level diff --git a/src/aliby/io/omero.py b/src/aliby/io/omero.py index a56a1c0e..0ea37359 100644 --- a/src/aliby/io/omero.py +++ b/src/aliby/io/omero.py @@ -131,7 +131,6 @@ class BridgeOmero: FIXME: Add docs. """ - # metadata = load_attributes(filepath) bridge = BridgeH5(filepath) meta = safe_load(bridge.meta_h5["parameters"])["general"] server_info = {k: meta[k] for k in ("host", "username", "password")} @@ -268,7 +267,6 @@ class Dataset(BridgeOmero): FIXME: Add docs. """ - # metadata = load_attributes(filepath) bridge = BridgeH5(filepath) dataset_keys = ("omero_id", "omero_id,", "dataset_id") for k in dataset_keys: @@ -301,21 +299,21 @@ class Image(BridgeOmero): cls, filepath: t.Union[str, Path], ): - """Instatiate Image from a hdf5 file. + """ + Instantiate Image from a h5 file. Parameters ---------- cls : Image Image class filepath : t.Union[str, Path] - Location of hdf5 file. + Location of h5 file. Examples -------- FIXME: Add docs. """ - # metadata = load_attributes(filepath) bridge = BridgeH5(filepath) image_id = bridge.meta_h5["image_id"] return cls(image_id, **cls.server_info_from_h5(filepath)) diff --git a/src/extraction/core/extractor.py b/src/extraction/core/extractor.py index 6d025188..21841438 100644 --- a/src/extraction/core/extractor.py +++ b/src/extraction/core/extractor.py @@ -7,7 +7,7 @@ import pandas as pd from agora.abc import ParametersABC, StepABC from agora.io.cells import Cells -from agora.io.writer import Writer, load_attributes +from agora.io.writer import Writer, load_meta from aliby.tile.tiler import Tiler from extraction.core.functions.defaults import exparams_from_meta from extraction.core.functions.distributors import reduce_z, trap_apply @@ -89,7 +89,7 @@ class Extractor(StepABC): or leaf level. """ - # TODO Alan: Move this to a location with the SwainLab defaults + # TODO Move this to a location with the SwainLab defaults default_meta = { "pixel_size": 0.236, "z_size": 0.6, @@ -119,10 +119,11 @@ class Extractor(StepABC): self.params = parameters if store: self.local = store - self.load_meta() + self.meta = load_meta(self.local) else: # if no h5 file, use the parameters directly self.meta = {"channel": parameters.to_dict()["tree"].keys()} + if tiler: self.tiler = tiler available_channels = set((*tiler.channels, "general")) @@ -236,10 +237,6 @@ class Extractor(StepABC): # merge the two dicts self._all_funs = {**self._custom_funs, **ALL_FUNS} - def load_meta(self): - """Load metadata from h5 file.""" - self.meta = load_attributes(self.local) - def get_tiles( self, tp: int, -- GitLab