Skip to content

Commit

Permalink
fixing mypy types
Browse files Browse the repository at this point in the history
  • Loading branch information
pattonw committed Dec 10, 2024
1 parent 0d81ef3 commit d3e90e4
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 53 deletions.
68 changes: 17 additions & 51 deletions funlib/persistence/arrays/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def __init__(
def interleave_physical(
self, physical: Sequence[int | str], non_physical: int | str | None
) -> Sequence[int | str | None]:
interleaved = []
interleaved: list[int | str | None] = []
physical_ind = 0
for i, type in enumerate(self.types):
if type in ["space", "time"]:
Expand All @@ -43,19 +43,30 @@ def interleave_physical(

@property
def ome_scale(self) -> Sequence[int]:
return self.interleave_physical(self.voxel_size, 1)
return [
x
for x in self.interleave_physical(self.voxel_size, 1)
if isinstance(x, int)
]

@property
def ome_translate(self) -> Sequence[int]:
assert self.offset % self.voxel_size == self.voxel_size * 0, (
"funlib.persistence only supports ome-zarr with integer multiples of voxel_size as an offset."
f"offset: {self.offset}, voxel_size:{self.voxel_size}, offset % voxel_size: {self.offset % self.voxel_size}"
)
return self.interleave_physical(self.offset / self.voxel_size, 0)
return [
x
for x in self.interleave_physical(self.offset / self.voxel_size, 0)
if isinstance(x, int)
]

@property
def ome_units(self) -> list[str | None]:
return self.interleave_physical(self.units, None)
return [
str(x) if x is not None else None
for x in self.interleave_physical(self.units, None)
]

@property
def offset(self) -> Coordinate:
Expand Down Expand Up @@ -160,30 +171,7 @@ class OME_MetaDataFormat(BaseModel):
class Config:
extra = "forbid"

def fetch(self, data: dict[str | int, Any], keys: Sequence[str]):
current_key: str | int
current_key, *keys = keys
try:
current_key = int(current_key)
except ValueError:
pass
if isinstance(current_key, int):
return self.fetch(data[current_key], keys)
if len(keys) == 0:
return data.get(current_key, None)
elif isinstance(data, list):
assert current_key == "{dim}", current_key
values = []
for sub_data in data:
try:
values.append(self.fetch(sub_data, keys))
except KeyError:
values.append(None)
return values
else:
return self.fetch(data[current_key], keys)

def strip_channels(self, types: list[str], to_strip: list[Sequence]) -> None:
def strip_channels(self, types: list[str], to_strip: list[list]) -> None:
to_delete = [i for i, t in enumerate(types) if t not in ["space", "time"]][::-1]
for ll in to_strip:
if ll is not None and len(ll) == len(types):
Expand All @@ -193,35 +181,13 @@ def strip_channels(self, types: list[str], to_strip: list[Sequence]) -> None:
def parse(
self,
shape,
data: dict[str | int, Any],
offset=None,
voxel_size=None,
axis_names=None,
units=None,
types=None,
strict=False,
) -> MetaData:
offset = (
offset
if offset is not None
else self.fetch(data, self.offset_attr.split("/"))
)
voxel_size = (
voxel_size
if voxel_size is not None
else self.fetch(data, self.voxel_size_attr.split("/"))
)
axis_names = (
axis_names
if axis_names is not None
else self.fetch(data, self.axis_names_attr.split("/"))
)
units = (
units if units is not None else self.fetch(data, self.units_attr.split("/"))
)
types = (
types if types is not None else self.fetch(data, self.types_attr.split("/"))
)

if types is not None:
self.strip_channels(types, [offset, voxel_size, units])
Expand Down Expand Up @@ -278,7 +244,7 @@ def fetch(self, data: dict[str | int, Any], keys: Sequence[str]):
else:
return self.fetch(data[current_key], keys)

def strip_channels(self, types: list[str], to_strip: list[Sequence]) -> None:
def strip_channels(self, types: list[str], to_strip: list[list]) -> None:
to_delete = [i for i, t in enumerate(types) if t not in ["space", "time"]][::-1]
for ll in to_strip:
if ll is not None and len(ll) == len(types):
Expand Down
8 changes: 6 additions & 2 deletions funlib/persistence/arrays/ome_datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ def open_ome_ds(

metadata = OME_MetaDataFormat().parse(
dataset.shape,
{},
offset=list(base_translation),
voxel_size=list(base_scale),
axis_names=axis_names,
Expand Down Expand Up @@ -196,7 +195,12 @@ def prepare_ome_ds(
assert not store.exists(), "Store already exists!"

metadata = MetaData(
shape, Coordinate(offset), Coordinate(voxel_size), axis_names, units, types
Coordinate(shape),
Coordinate(offset),
Coordinate(voxel_size),
list(axis_names) if axis_names is not None else None,
list(units) if units is not None else None,
list(types) if types is not None else None,
)

axis_metadata = [
Expand Down
3 changes: 3 additions & 0 deletions mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,7 @@ ignore_missing_imports = True
ignore_missing_imports = True

[mypy-psycopg2.*]
ignore_missing_imports = True

[mypy-iohub.*]
ignore_missing_imports = True

0 comments on commit d3e90e4

Please sign in to comment.