diff --git a/src/ngio/__init__.py b/src/ngio/__init__.py index 67d570f..31c3992 100644 --- a/src/ngio/__init__.py +++ b/src/ngio/__init__.py @@ -1,14 +1,11 @@ """Next Generation file format IO.""" -import os from importlib.metadata import PackageNotFoundError, version from ngio.core import Image, Label, NgffImage -from ngio.utils import ngio_logger, set_logger_level -__all__ = ["Image", "Label", "NgffImage", "set_logger_level", "ngio_logger"] +__all__ = ["Image", "Label", "NgffImage"] -set_logger_level(os.getenv("NGIO_LOGGER_LEVEL", "WARNING")) try: __version__ = version("ngio") diff --git a/src/ngio/core/dimensions.py b/src/ngio/core/dimensions.py index 5a16108..b985d9a 100644 --- a/src/ngio/core/dimensions.py +++ b/src/ngio/core/dimensions.py @@ -39,6 +39,17 @@ def __init__( self._shape = [self._on_disk_shape[i] for i in axes_order] self._shape_dict = dict(zip(axes_names, self._shape, strict=True)) + def __str__(self) -> str: + """Return the string representation of the object.""" + _dimensions = ", ".join( + [f"{name}={self._shape_dict[name]}" for name in self._axes_names] + ) + return f"Dimensions({_dimensions})" + + def __repr__(self) -> str: + """Return the string representation of the object.""" + return str(self) + @property def shape(self) -> tuple[int, ...]: """Return the shape as a tuple in the canonical order.""" diff --git a/src/ngio/core/ngff_image.py b/src/ngio/core/ngff_image.py index 963c732..647ef8d 100644 --- a/src/ngio/core/ngff_image.py +++ b/src/ngio/core/ngff_image.py @@ -10,6 +10,7 @@ from ngio.ngff_meta import get_ngff_image_meta_handler from ngio.ngff_meta.fractal_image_meta import ImageMeta, PixelSize from ngio.tables.tables_group import TableGroup +from ngio.utils import ngio_logger class NgffImage: @@ -28,6 +29,8 @@ def __init__( self._metadata_cache = cache self.table = TableGroup(self.group, mode=self._mode) self.label = LabelGroup(self.group, image_ref=self.get_image(), mode=self._mode) + ngio_logger.info(f"Opened image located in store: {store}") + ngio_logger.info(f"- Image number of levels: {self.num_levels}") @property def image_meta(self) -> ImageMeta: @@ -68,7 +71,7 @@ def get_image( if path is not None or pixel_size is not None: highest_resolution = False - return Image( + image = Image( store=self.group, path=path, pixel_size=pixel_size, @@ -76,6 +79,10 @@ def get_image( label_group=LabelGroup(self.group, image_ref=None), cache=self._metadata_cache, ) + ngio_logger.info(f"Opened image at path: {image.path}") + ngio_logger.info(f"- {image.dimensions}") + ngio_logger.info(f"- {image.pixel_size}") + return image def _update_omero_window( self, min_percentile: int = 5, max_percentile: int = 95 @@ -103,7 +110,6 @@ def _update_omero_window( max_percentile, method="nearest", ).compute() - print(f"Setting window to {start} - {end}") if meta.omero is None: raise ValueError("OMERO metadata is not present in the image.") diff --git a/src/ngio/io/_zarr.py b/src/ngio/io/_zarr.py index c1c654a..beca46a 100644 --- a/src/ngio/io/_zarr.py +++ b/src/ngio/io/_zarr.py @@ -62,7 +62,6 @@ def _open_group_v2_v3( Returns: zarr.Group: The opened Zarr group. """ - print(mode) if ZARR_PYTHON_V == 3: return zarr.open_group(store=store, mode=mode, zarr_format=zarr_format) else: diff --git a/src/ngio/io/_zarr_group_utils.py b/src/ngio/io/_zarr_group_utils.py index 5f37ae7..dd2de9b 100644 --- a/src/ngio/io/_zarr_group_utils.py +++ b/src/ngio/io/_zarr_group_utils.py @@ -10,6 +10,7 @@ _open_group_v2_v3, _pass_through_group, ) +from ngio.utils import ngio_logger # Zarr v3 Imports # import zarr.store @@ -40,8 +41,14 @@ def open_group_wrapper( zarr.Group: The opened Zarr group. """ if isinstance(store, zarr.Group): - return _pass_through_group(store, mode=mode, zarr_format=zarr_format) + _group = _pass_through_group(store, mode=mode, zarr_format=zarr_format) + ngio_logger.debug( + f"Passing through group: {_group}, " + f"located in store: {_group.store.path}" + ) + return _group store = _check_store(store) - - return _open_group_v2_v3(store=store, mode=mode, zarr_format=zarr_format) + _group = _open_group_v2_v3(store=store, mode=mode, zarr_format=zarr_format) + ngio_logger.debug(f"Opened located in store: {store}") + return _group diff --git a/src/ngio/ngff_meta/fractal_image_meta.py b/src/ngio/ngff_meta/fractal_image_meta.py index 36184a2..fe43273 100644 --- a/src/ngio/ngff_meta/fractal_image_meta.py +++ b/src/ngio/ngff_meta/fractal_image_meta.py @@ -100,7 +100,7 @@ class PixelSize(BaseModel): unit: SpaceUnits = SpaceUnits.micrometer virtual: bool = False - def __repr__(self) -> str: + def __str__(self) -> str: """Return the string representation of the object.""" return f"PixelSize(x={self.x}, y={self.y}, z={self.z}, unit={self.unit.value})" diff --git a/src/ngio/tables/_utils.py b/src/ngio/tables/_utils.py index 3bb1394..7ac9a28 100644 --- a/src/ngio/tables/_utils.py +++ b/src/ngio/tables/_utils.py @@ -197,6 +197,7 @@ def table_ad_to_df( index_key: str = "label", index_type: Literal["str", "int"] = "int", validators: list[Validator] | None = None, + validate_index_name: bool = False, ) -> pd.DataFrame: """Convert a AnnData object representing a fractal table to a pandas DataFrame. @@ -207,16 +208,24 @@ def table_ad_to_df( index_type (str): The type of the index column in the DataFrame. Either 'str' or 'int'. Default is 'int'. validators (list[Validator]): A list of functions to further validate the table. + validate_index_name (bool): If True, the index name is validated. """ table_df = table_ad.to_df() - table_df[table_ad.obs_keys()] = table_ad.obs # Set the index of the DataFrame - if table_ad.obs.index.name is not None: - table_df.index = table_ad.obs.index - elif index_key in table_df.columns: + if index_key in table_df.columns: table_df = table_df.set_index(index_key) + elif table_ad.obs.index.name is not None: + if validate_index_name: + if table_ad.obs.index.name != index_key: + raise TableValidationError( + f"Index key {index_key} not found in AnnData object." + ) + table_df.index = table_ad.obs.index + elif table_ad.obs.index.name is None: + table_df.index = table_ad.obs.index + table_df.index.name = index_key else: raise TableValidationError( f"Index key {index_key} not found in AnnData object." diff --git a/src/ngio/utils/__init__.py b/src/ngio/utils/__init__.py index 1b189f4..e4c3775 100644 --- a/src/ngio/utils/__init__.py +++ b/src/ngio/utils/__init__.py @@ -1,5 +1,7 @@ """Various utilities for the ngio package.""" +import os + from ngio.utils._common_types import ArrayLike from ngio.utils._errors import ( NgioFileExistsError, @@ -10,6 +12,8 @@ from ngio.utils._logger import ngio_logger, set_logger_level from ngio.utils._pydantic_utils import BaseWithExtraFields, unique_items_validator +set_logger_level(os.getenv("NGIO_LOGGER_LEVEL", "WARNING")) + __all__ = [ "ArrayLike", # Pydantic