Skip to content

Commit

Permalink
avoid root logger
Browse files Browse the repository at this point in the history
  • Loading branch information
ziw-liu committed Feb 9, 2024
1 parent 8799d84 commit fab9d93
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 30 deletions.
5 changes: 4 additions & 1 deletion iohub/_deprecated/zarrfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
from iohub._deprecated.reader_base import ReaderBase


_logger = logging.getLogger(__name__)


class ZarrReader(ReaderBase):
"""
.. deprecated:: 0.0.1
Expand All @@ -29,7 +32,7 @@ def __init__(
):
super().__init__()

logging.warning(
_logger.warning(
DeprecationWarning(
"`iohub.zarrfile.ZarrReader` is deprecated "
"and will be removed in the future. "
Expand Down
32 changes: 17 additions & 15 deletions iohub/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
from iohub.reader import MMStack, NDTiffDataset, read_images

__all__ = ["TIFFConverter"]
_logger = logging.getLogger(__name__)

MAX_CHUNK_SIZE = 500e6 # in bytes


Expand Down Expand Up @@ -102,12 +104,12 @@ def __init__(
chunks: tuple[int] | Literal["XY", "XYZ"] = None,
hcs_plate: bool = None,
):
logging.debug("Checking output.")
_logger.debug("Checking output.")
output_dir = Path(output_dir)
if "zarr" in output_dir.suffixes:
raise ValueError("Please specify .zarr at the end of your output")
self.output_dir = output_dir
logging.info("Initializing data.")
_logger.info("Initializing data.")
self.reader = read_images(input_dir)
if reader_type := type(self.reader) not in (
MMStack,
Expand All @@ -116,12 +118,12 @@ def __init__(
raise TypeError(
f"Reader type {reader_type} not supported for conversion."
)
logging.debug("Finished initializing data.")
_logger.debug("Finished initializing data.")
self.summary_metadata = (
self.reader.mm_meta["Summary"] if self.reader.mm_meta else None
)
self.save_name = os.path.basename(output_dir)
logging.debug("Getting dataset summary information.")
_logger.debug("Getting dataset summary information.")
self.coord_map = dict()
self.p = len(self.reader)
self.t = self.reader.frames
Expand All @@ -134,7 +136,7 @@ def __init__(
self.hcs_plate = hcs_plate
self._check_hcs_sites()
self._get_pos_names()
logging.info(
_logger.info(
f"Found Dataset {self.save_name} with "
f"dimensions (P, T, C, Z, Y, X): {self.dim}"
)
Expand All @@ -146,7 +148,7 @@ def __init__(
raise ValueError(
"grid_layout and hcs_plate must not be both true"
)
logging.info("Generating HCS plate level grid.")
_logger.info("Generating HCS plate level grid.")
try:
self.position_grid = _create_grid_from_coordinates(
*self._get_position_coords()
Expand All @@ -166,7 +168,7 @@ def _check_hcs_sites(self):
self.hcs_sites = self.reader.hcs_position_labels
self.hcs_plate = True
except ValueError:
logging.debug(
_logger.debug(
"HCS sites not detected, "
"dumping all position into a single row."
)
Expand Down Expand Up @@ -216,7 +218,7 @@ def _get_pos_names(self):

def _gen_chunks(self, input_chunks):
if not input_chunks:
logging.debug("No chunk size specified, using ZYX.")
_logger.debug("No chunk size specified, using ZYX.")
chunks = [1, 1, self.z, self.y, self.x]
elif isinstance(input_chunks, tuple):
chunks = list(input_chunks)
Expand All @@ -242,14 +244,14 @@ def _gen_chunks(self, input_chunks):
):
chunks[-3] = np.ceil(chunks[-3] / 2).astype(int)

logging.debug(f"Zarr store chunk size will be set to {chunks}.")
_logger.debug(f"Zarr store chunk size will be set to {chunks}.")

return tuple(chunks)

def _get_channel_names(self):
cns = self.reader.channel_names
if not cns:
logging.warning(
_logger.warning(
"Cannot find channel names, using indices instead."
)
cns = [str(i) for i in range(self.c)]
Expand Down Expand Up @@ -292,7 +294,7 @@ def _init_zarr_arrays(self):
def _init_hcs_arrays(self, arr_kwargs):
for row, col, fov in self.hcs_sites:
self._create_zeros_array(row, col, fov, arr_kwargs)
logging.info(
_logger.info(
"Created HCS NGFF layout from Micro-Manager HCS position labels."
)
self.writer.print_tree()
Expand All @@ -314,7 +316,7 @@ def _create_zeros_array(
pos.dump_meta()

def _convert_image_plane_metadata(self, fov, zarr_name: str):
logging.info("Writing image plane metadata...")
_logger.info("Writing image plane metadata...")
bar_format_time_channel = (
"Timepoints/Channels: |{bar:16}|{n_fmt}/{total_fmt} "
"(Time Remaining: {remaining}), {rate_fmt}{postfix}]"
Expand All @@ -337,7 +339,7 @@ def _convert_image_plane_metadata(self, fov, zarr_name: str):
for z_idx in range(self.z):
metadata = fov.frame_metadata(t=t_idx, c=c_key, z=z_idx)
if metadata is None:
logging.warning(
_logger.warning(
f"Cannot load data at timepoint {t_idx}, channel "
f"{c_idx}, filling with zeros. Raw data may be "
"incomplete."
Expand Down Expand Up @@ -365,14 +367,14 @@ def __call__(self) -> None:
>>> converter = TIFFConverter("input/path/", "output/path/")
>>> converter()
"""
logging.debug("Setting up Zarr store.")
_logger.debug("Setting up Zarr store.")
self._init_zarr_arrays()
bar_format_images = (
"Converting Positions: |{bar:16}|{n_fmt}/{total_fmt} "
"(Time Remaining: {remaining}), {rate_fmt}{postfix}]"
)
# Run through every coordinate and convert in acquisition order
logging.info("Converting Images...")
_logger.info("Converting Images...")
for p_idx, (_, fov) in tqdm(
enumerate(self.reader), bar_format=bar_format_images
):
Expand Down
11 changes: 6 additions & 5 deletions iohub/mmstack.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@


__all__ = ["MMOmeTiffFOV", "MMStack"]
_logger = logging.getLogger(__name__)


def _normalize_mm_pos_key(key: str | int) -> int:
Expand Down Expand Up @@ -107,7 +108,7 @@ def _parse_data(self):
)
axes = ("R", "T", "C", "Z", "Y", "X")
dims = dict((ax, raw_dims.get(ax, 1)) for ax in axes)
logging.debug(f"Got dataset dimensions from tifffile: {dims}.")
_logger.debug(f"Got dataset dimensions from tifffile: {dims}.")
(
self.positions,
self.frames,
Expand All @@ -117,7 +118,7 @@ def _parse_data(self):
self.width,
) = dims.values()
self._store = series.aszarr()
logging.debug(f"Opened {self._store}.")
_logger.debug(f"Opened {self._store}.")
data = da.from_zarr(zarr.open(self._store))
self.dtype = data.dtype
img = DataArray(data, dims=raw_dims, name=self.dirname)
Expand Down Expand Up @@ -236,7 +237,7 @@ def _set_mm_meta(self, mm_meta: dict) -> None:
if self.slices == 1:
z_step_size = 1.0
else:
logging.warning(
_logger.warning(
f"Z-step size is {z_step_size} um in the metadata, "
"Using 1.0 um instead."
)
Expand Down Expand Up @@ -335,10 +336,10 @@ def _infer_image_meta(self) -> None:
if self._xy_pixel_size > 0:
return
except Exception:
logging.warning(
_logger.warning(
"Micro-Manager image plane metadata cannot be loaded."
)
logging.warning(
_logger.warning(
"XY pixel size cannot be determined, defaulting to 1.0 um."
)
self._xy_pixel_size = 1.0
Expand Down
20 changes: 11 additions & 9 deletions iohub/ngff.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
if TYPE_CHECKING:
from _typeshed import StrOrBytesPath

_logger = logging.getLogger(__name__)


def _pad_shape(shape: tuple[int], target: int = 5):
"""Pad shape tuple to a target length."""
Expand All @@ -53,7 +55,7 @@ def _open_store(
f"Dataset directory not found at {store_path}."
)
if version != "0.4":
logging.warning(
_logger.warning(
"\n".join(
"IOHub is only tested against OME-NGFF v0.4.",
f"Requested version {version} may not work properly.",
Expand Down Expand Up @@ -253,7 +255,7 @@ def iteritems(self):
try:
yield key, self[key]
except Exception:
logging.warning(
_logger.warning(
"Skipped item at {}: invalid {}.".format(
key, type(self._MEMBER_TYPE)
)
Expand Down Expand Up @@ -288,7 +290,7 @@ def _warn_invalid_meta(self):
msg = "Zarr group at {} does not have valid metadata for {}".format(
self._group.path, type(self)
)
logging.warning(msg)
_logger.warning(msg)

def _parse_meta(self):
"""Parse and set NGFF metadata from `.zattrs`."""
Expand Down Expand Up @@ -745,9 +747,9 @@ def _check_shape(self, data_shape: tuple[int]):
if data_shape[ch_axis] > num_ch:
raise ValueError(msg)
elif data_shape[ch_axis] < num_ch:
logging.warning(msg)
_logger.warning(msg)
else:
logging.info(
_logger.info(
"Dataset channel axis is not set. "
"Skipping channel shape check."
)
Expand Down Expand Up @@ -1340,7 +1342,7 @@ def __init__(

def _parse_meta(self):
if plate_meta := self.zattrs.get("plate"):
logging.debug(f"Loading HCS metadata from file: {plate_meta}")
_logger.debug(f"Loading HCS metadata from file: {plate_meta}")
self.metadata = PlateMeta(**plate_meta)
else:
self._warn_invalid_meta()
Expand All @@ -1357,13 +1359,13 @@ def _first_pos_attr(self, attr: str):
well_grp = next(row_grp.groups())[1]
pos_grp = next(well_grp.groups())[1]
except StopIteration:
logging.warning(f"{msg} No position is found in the dataset.")
_logger.warning(f"{msg} No position is found in the dataset.")
return
try:
pos = Position(pos_grp)
setattr(self, attr, getattr(pos, attr))
except AttributeError:
logging.warning(f"{msg} Invalid metadata at the first position")
_logger.warning(f"{msg} Invalid metadata at the first position")

def dump_meta(self, field_count: bool = False):
"""Dumps metadata JSON to the `.zattrs` file.
Expand Down Expand Up @@ -1641,7 +1643,7 @@ def open_ome_zarr(
raise FileExistsError(store_path)
elif mode == "w":
if os.path.exists(store_path):
logging.warning(f"Overwriting data at {store_path}")
_logger.warning(f"Overwriting data at {store_path}")
else:
raise ValueError(f"Invalid persistence mode '{mode}'.")
root = _open_store(store_path, mode, version, synchronizer)
Expand Down

0 comments on commit fab9d93

Please sign in to comment.