Skip to content

Commit

Permalink
improve logging
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzocerrone committed Nov 4, 2024
1 parent aa4dd23 commit 2248fd1
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 15 deletions.
5 changes: 1 addition & 4 deletions src/ngio/__init__.py
Original file line number Diff line number Diff line change
@@ -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")
Expand Down
11 changes: 11 additions & 0 deletions src/ngio/core/dimensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down
10 changes: 8 additions & 2 deletions src/ngio/core/ngff_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -68,14 +71,18 @@ 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,
highest_resolution=highest_resolution,
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
Expand Down Expand Up @@ -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.")
Expand Down
1 change: 0 additions & 1 deletion src/ngio/io/_zarr.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
13 changes: 10 additions & 3 deletions src/ngio/io/_zarr_group_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
_open_group_v2_v3,
_pass_through_group,
)
from ngio.utils import ngio_logger

# Zarr v3 Imports
# import zarr.store
Expand Down Expand Up @@ -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
2 changes: 1 addition & 1 deletion src/ngio/ngff_meta/fractal_image_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -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})"

Expand Down
17 changes: 13 additions & 4 deletions src/ngio/tables/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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."
Expand Down
4 changes: 4 additions & 0 deletions src/ngio/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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
Expand Down

0 comments on commit 2248fd1

Please sign in to comment.