Skip to content
This repository has been archived by the owner on May 31, 2023. It is now read-only.

Commit

Permalink
datasource-properties: add largestSegmentId, dataFormat & wkwResoluti…
Browse files Browse the repository at this point in the history
…ons (#64)

* datasource-properties: add largestSegmentId & dataFormat

* make datasource-properties wk backend & frontend compatible
  • Loading branch information
jstriebel authored Mar 5, 2019
1 parent a0983a9 commit 8539b56
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 9 deletions.
31 changes: 26 additions & 5 deletions wkconnect/utils/json.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from typing import Any, Optional, Union
from typing import Any, Iterable, Optional, Tuple, Union, get_type_hints

from dataclasses import InitVar

from .types import JSON, Vec3D

Expand Down Expand Up @@ -51,19 +53,38 @@ def from_json(data: JSON, cls: Optional[type]) -> Any:
return data


def yield_jsons(
obj_iter: Iterable, keys: Optional[Iterable[str]] = None
) -> Iterable[Union[JSON, Tuple[str, JSON]]]:
if keys is None:
for obj in obj_iter:
json = to_json(obj)
if json is not None:
yield json
else:
for key, obj in zip(keys, obj_iter):
json = to_json(obj)
if json is not None:
yield key, json


def to_json(obj: Any) -> JSON:
cls = type(obj)
if issubclass(cls, Vec3D):
return tuple(obj)
elif hasattr(cls, "__annotations__"):
annotations = get_type_hints(cls)
annotations = {k: v for k, v in annotations.items() if v != InitVar}
return dict(
map(lambda name: (name, to_json(getattr(obj, name))), cls.__annotations__)
yield_jsons(
(getattr(obj, name) for name in annotations), keys=annotations.keys()
)
)
elif issubclass(cls, list):
return list(map(to_json, obj))
return list(yield_jsons(obj))
elif issubclass(cls, tuple):
return tuple(map(to_json, obj))
return tuple(yield_jsons(obj))
elif issubclass(cls, dict):
return dict([(name, to_json(value)) for name, value in obj.items()])
return dict(yield_jsons(obj.values(), keys=obj.keys()))
else:
return obj
20 changes: 16 additions & 4 deletions wkconnect/webknossos/models.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
from __future__ import annotations

from typing import List, NamedTuple, Tuple
from typing import Any, List, NamedTuple, Optional, Tuple, cast

from dataclasses import dataclass
import numpy as np

from ..utils.types import Box3D, Vec3D
from dataclasses import dataclass, field

from ..utils.types import JSON, Box3D, Vec3D


class DataStoreStatus(NamedTuple):
Expand Down Expand Up @@ -34,17 +36,27 @@ class DataSourceId(NamedTuple):
name: str


@dataclass(frozen=True)
@dataclass(unsafe_hash=True)
class DataLayer:
name: str
category: str
boundingBox: BoundingBox
resolutions: List[Vec3D]
elementClass: str
dataFormat: str = field(default="wkw", init=False)
wkwResolutions: JSON = field(init=False)
largestSegmentId: Optional[int] = field(default=None, init=False)
mappings: Optional[List[Any]] = field(default=None, init=False)

def __post_init__(self) -> None:
assert self.category in ["color", "segmentation"]
assert self.elementClass in ["uint8", "uint16", "uint32", "uint64"]
if self.category == "segmentation":
self.largestSegmentId = cast(int, np.iinfo(self.elementClass).max)
self.mappings = []
self.wkwResolutions = [
{"resolution": i, "cubeLength": 1024} for i in self.resolutions
]


class DataSource(NamedTuple):
Expand Down

0 comments on commit 8539b56

Please sign in to comment.