Skip to content

Commit

Permalink
extract DataFormat into separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
normanrz committed Aug 16, 2023
1 parent 1f7d51d commit 3543df4
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 27 deletions.
2 changes: 1 addition & 1 deletion webknossos/webknossos/dataset/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
Each dataset consists of one or more layers (webknossos.dataset.layer.Layer), which themselves can comprise multiple magnifications (webknossos.dataset.mag_view.MagView).
"""

from ._array import DataFormat
from .data_format import DataFormat
from .dataset import Dataset, RemoteDataset
from .layer import Layer, SegmentationLayer
from .layer_categories import COLOR_CATEGORY, SEGMENTATION_CATEGORY, LayerCategoryType
Expand Down
14 changes: 2 additions & 12 deletions webknossos/webknossos/dataset/_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from abc import ABC, abstractmethod
from contextlib import contextmanager
from dataclasses import dataclass
from enum import Enum
from os.path import relpath
from pathlib import Path
from typing import TYPE_CHECKING, Any, Dict, Iterator, List, Optional, Type, Union
Expand All @@ -15,10 +14,10 @@
from upath import UPath
from zarr.storage import FSStore

from webknossos.dataset.defaults import WK_USE_ZARRITA

from ..geometry import BoundingBox, Vec3Int, Vec3IntLike
from ..utils import warn_deprecated
from .data_format import DataFormat
from .defaults import WK_USE_ZARRITA

if TYPE_CHECKING:
import zarrita
Expand Down Expand Up @@ -55,15 +54,6 @@ class ArrayException(Exception):
pass


class DataFormat(Enum):
WKW = "wkw"
Zarr = "zarr"
Zarr3 = "zarr3"

def __str__(self) -> str:
return self.value


@dataclass
class ArrayInfo:
data_format: DataFormat
Expand Down
10 changes: 10 additions & 0 deletions webknossos/webknossos/dataset/data_format.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from enum import Enum


class DataFormat(Enum):
WKW = "wkw"
Zarr = "zarr"
Zarr3 = "zarr3"

def __str__(self) -> str:
return self.value
3 changes: 2 additions & 1 deletion webknossos/webknossos/dataset/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
from upath import UPath

from ..geometry.vec3_int import Vec3Int, Vec3IntLike
from ._array import ArrayException, ArrayInfo, BaseArray, DataFormat
from ._array import ArrayException, ArrayInfo, BaseArray
from .data_format import DataFormat
from .defaults import (
DEFAULT_BIT_DEPTH,
DEFAULT_CHUNK_SHAPE,
Expand Down
2 changes: 1 addition & 1 deletion webknossos/webknossos/dataset/defaults.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from os import environ

from ..geometry import Vec3Int
from ._array import DataFormat
from .data_format import DataFormat

WK_USE_ZARRITA = environ.get("WK_USE_ZARRITA") is not None
DEFAULT_WKW_FILE_LEN = 32
Expand Down
3 changes: 2 additions & 1 deletion webknossos/webknossos/dataset/layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from webknossos.dataset.view import View, _copy_job
from webknossos.geometry import BoundingBox, Mag, Vec3Int, Vec3IntLike

from ._array import ArrayException, BaseArray, DataFormat
from ._array import ArrayException, BaseArray
from ._downsampling_utils import (
calculate_default_coarsest_mag,
calculate_mags_to_downsample,
Expand All @@ -27,6 +27,7 @@
parse_interpolation_mode,
)
from ._upsampling_utils import upsample_cube_job
from .data_format import DataFormat
from .layer_categories import COLOR_CATEGORY, SEGMENTATION_CATEGORY, LayerCategoryType
from .properties import (
LayerProperties,
Expand Down
26 changes: 16 additions & 10 deletions webknossos/webknossos/dataset/ome_metadata.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
import json
from typing import Any, Dict
from typing import TYPE_CHECKING, Any, Dict

import numpy as np

from ._array import DataFormat
from .dataset import Dataset
from .data_format import DataFormat
from .defaults import ZARR_JSON_FILE_NAME, ZATTRS_FILE_NAME, ZGROUP_FILE_NAME
from .layer import Layer

if TYPE_CHECKING:
from .dataset import Dataset
from .layer import Layer

def get_ome_0_4_multiscale_metadata(dataset: Dataset, layer: Layer) -> Dict[str, Any]:

def get_ome_0_4_multiscale_metadata(
dataset: "Dataset", layer: "Layer"
) -> Dict[str, Any]:
return {
"version": "0.4",
"axes": [
Expand Down Expand Up @@ -46,7 +50,9 @@ def get_ome_0_4_multiscale_metadata(dataset: Dataset, layer: Layer) -> Dict[str,
}


def get_ome_zarr3_multiscale_metadata(dataset: Dataset, layer: Layer) -> Dict[str, Any]:
def get_ome_zarr3_multiscale_metadata(
dataset: "Dataset", layer: "Layer"
) -> Dict[str, Any]:
return {
"coordinateSystems": [
{
Expand Down Expand Up @@ -79,8 +85,8 @@ def get_ome_zarr3_multiscale_metadata(dataset: Dataset, layer: Layer) -> Dict[st
"type": "scale",
"scale": [1.0]
+ (np.array(dataset.voxel_size) * mag.mag.to_np()).tolist(),
"input": mag.path.name,
"output": "example",
"input": f"/{mag.path.name}",
"output": f"/{layer.name}",
}
],
}
Expand All @@ -89,7 +95,7 @@ def get_ome_zarr3_multiscale_metadata(dataset: Dataset, layer: Layer) -> Dict[st
}


def write_ome_0_4_metadata(dataset: Dataset) -> None:
def write_ome_0_4_metadata(dataset: "Dataset") -> None:
zgroup_content = {"zarr_format": "2"}
with (dataset.path / ZGROUP_FILE_NAME).open("w", encoding="utf-8") as outfile:
json.dump(zgroup_content, outfile, indent=4)
Expand All @@ -105,7 +111,7 @@ def write_ome_0_4_metadata(dataset: Dataset) -> None:
)


def write_ome_zarr3_metadata(dataset: Dataset) -> None:
def write_ome_zarr3_metadata(dataset: "Dataset") -> None:
with (dataset.path / ZARR_JSON_FILE_NAME).open("w", encoding="utf-8") as outfile:
json.dump({"zarr_format": 3, "node_type": "group"}, outfile, indent=4)
for layer in dataset.layers.values():
Expand Down
3 changes: 2 additions & 1 deletion webknossos/webknossos/dataset/properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
from webknossos.geometry import BoundingBox, Mag, Vec3Int
from webknossos.utils import snake_to_camel_case, warn_deprecated

from ._array import ArrayException, BaseArray, DataFormat
from ._array import ArrayException, BaseArray
from .data_format import DataFormat
from .layer_categories import LayerCategoryType


Expand Down

0 comments on commit 3543df4

Please sign in to comment.