Skip to content

Commit

Permalink
Do not mutate input argument and returned validated copies
Browse files Browse the repository at this point in the history
  • Loading branch information
sbesson committed Jan 13, 2022
1 parent 4c3b2af commit 347da42
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions ome_zarr/writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,16 @@ def _validate_axes(axes: List[str], fmt: Format = CurrentFormat()) -> None:
raise ValueError("5D data must have axes ('t', 'c', 'z', 'y', 'x')")


def _validate_well_images(images: List, fmt: Format = CurrentFormat()) -> None:
def _validate_well_images(images: List, fmt: Format = CurrentFormat()) -> List[dict]:

VALID_KEYS = [
"acquisition",
"path",
]
for index, image in enumerate(images):
validated_images = []
for image in images:
if isinstance(image, str):
images[index] = {"path": str(image)}
validated_images.append({"path": str(image)})
elif isinstance(image, dict):
if any(e not in VALID_KEYS for e in image.keys()):
LOGGER.debug("f{image} contains unspecified keys")
Expand All @@ -88,13 +89,15 @@ def _validate_well_images(images: List, fmt: Format = CurrentFormat()) -> None:
raise ValueError(f"{image} path must be of string type")
if "acquisition" in image and not isinstance(image["acquisition"], int):
raise ValueError(f"{image} acquisition must be of int type")
validated_images.append(image)
else:
raise ValueError(f"Unrecognized type for {image}")
return validated_images


def _validate_plate_acquisitions(
acquisitions: List[Dict], fmt: Format = CurrentFormat()
) -> None:
) -> List[Dict]:

VALID_KEYS = [
"id",
Expand All @@ -114,6 +117,7 @@ def _validate_plate_acquisitions(
raise ValueError(f"{acquisition} must contain an id key")
if not isinstance(acquisition["id"], int):
raise ValueError(f"{acquisition} id must be of int type")
return acquisitions


def write_multiscale(
Expand Down Expand Up @@ -238,8 +242,7 @@ def write_plate_metadata(
if field_count is not None:
plate["field_count"] = field_count
if acquisitions is not None:
_validate_plate_acquisitions(acquisitions)
plate["acquisitions"] = acquisitions
plate["acquisitions"] = _validate_plate_acquisitions(acquisitions)
group.attrs["plate"] = plate


Expand All @@ -264,9 +267,8 @@ def write_well_metadata(
Defaults to the most current.
"""

_validate_well_images(images)
well = {
"images": images,
"images": _validate_well_images(images),
"version": fmt.version,
}
group.attrs["well"] = well
Expand Down

0 comments on commit 347da42

Please sign in to comment.