Skip to content

Commit

Permalink
fix: Implemented vds initialization without writing chunks
Browse files Browse the repository at this point in the history
  • Loading branch information
micmurawski committed Jul 16, 2024
1 parent b3deee4 commit 4ff5186
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 76 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import numpy as np

from ovds_utils.metadata import MetadataTypes, MetadataValue
from ovds_utils.ovds.enums import BrickSizes, Formats
from ovds_utils.vds import VDS, Channel, Components, Formats
from ovds_utils.vds import VDS, Channel, Components, Formats, AccessModes

metadata = {
"example": MetadataValue(value="value", category="category#1", type=MetadataTypes.String)
Expand All @@ -57,6 +57,7 @@ vds = VDS(
data=zeros,
metadata_dict=metadata,
databrick_size=BrickSizes.BrickSize_64,
access_mode=AccessModes.Create,
channels=[
Channel(
name="Channel0",
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def get_dependencies():

setup(
name='ovds_utils',
version='0.2.12',
version='0.3.0',
author="Michal Murawski",
author_email="[email protected]",
description="Utilities package for Open VDS.",
Expand Down
4 changes: 3 additions & 1 deletion src/ovds_utils/logging.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import logging
import os
import sys
Expand All @@ -6,7 +8,7 @@
FORMAT = "%(asctime)s::%(name)s::%(levelname)s::%(message)s"


def get_logger(name):
def get_logger(name) -> logging.Logger:
logger = logging.getLogger(name)
console_handler = logging.StreamHandler(sys.stdout)
logger.setLevel(LOG_LEVEL)
Expand Down
2 changes: 2 additions & 0 deletions src/ovds_utils/metadata.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from typing import Any, AnyStr, Dict, Union

import openvds
Expand Down
2 changes: 2 additions & 0 deletions src/ovds_utils/ovds/copy.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from subprocess import PIPE, Popen

from ovds_utils.logging import get_logger
Expand Down
6 changes: 6 additions & 0 deletions src/ovds_utils/ovds/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,17 @@ class BrickSizes(Enum):
_2048 = openvds.core.VolumeDataLayoutDescriptor.BrickSize.BrickSize_2048
_4096 = openvds.core.VolumeDataLayoutDescriptor.BrickSize.BrickSize_4096

@classmethod
def get_from_info(cls, vds_info: dict):
return getattr(cls, "_"+vds_info["layoutDescriptor"]["brickSize"].rsplit("_")[-1])


class AccessModes(Enum):
Create = openvds.IVolumeDataAccessManager.AccessMode.AccessMode_Create
ReadOnly = openvds.IVolumeDataAccessManager.AccessMode.AccessMode_ReadOnly
ReadWrite = openvds.IVolumeDataAccessManager.AccessMode.AccessMode_ReadWrite
CreateWithoutLODGeneration = openvds.IVolumeDataAccessManager.AccessMode.AccessMode_CreateWithoutLODGeneration
ReadWriteWithoutLODGeneration = openvds.IVolumeDataAccessManager.AccessMode.AccessMode_ReadWriteWithoutLODGeneration


class Dimensions(Enum):
Expand Down
2 changes: 2 additions & 0 deletions src/ovds_utils/ovds/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import json
from pathlib import Path
from subprocess import PIPE, Popen
Expand Down
37 changes: 8 additions & 29 deletions src/ovds_utils/ovds/writing.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
from __future__ import annotations

from logging import getLogger
from typing import Any, AnyStr, Dict, List

import numpy as np
import openvds

from .enums import InitValue
from .enums import AccessModes, InitValue
from .utils import copy_ovds_metadata


logger = getLogger(__name__)

FORMAT2NPTYPE = {
Expand Down Expand Up @@ -137,7 +138,6 @@ def create_vds(
default_max_pages: int = 8,
channels_data=None,
init_value: InitValue = InitValue.zero,
# close: bool = True
):
(
layout_descriptor,
Expand All @@ -157,23 +157,6 @@ def create_vds(
full_resolution_dimension=full_resolution_dimension,
)

(
layout_descriptor,
axis_descriptors,
channel_descriptors,
metadata_container
) = create_vds_attributes(
databrick_size=databrick_size,
metadata_dict=metadata_dict,
channles=channels,
axes=axes,
lod_levels=lod,
negative_margin=negative_margin,
positive_margin=positive_margin,
options=options,
brick_size_2d_multiplier=brick_size_2d_multiplier,
full_resolution_dimension=full_resolution_dimension,
)
vds = openvds.create(
url=path,
connectionString=connection_string,
Expand All @@ -183,18 +166,20 @@ def create_vds(
metadata=metadata_container,
)
access_manager = openvds.getAccessManager(vds)

if channels_data:
for i, data in enumerate(channels_data):
channel = channels[i]
accessor = access_manager.createVolumeDataPageAccessor(
dimensionsND=channel.dimensions_nd.value,
accessMode=access_mode,
accessMode=AccessModes.Create.value,
lod=lod,
channel=i,
maxPages=default_max_pages,
)
write_pages(accessor, data, channel.format.value)
else:

if init_value != InitValue.omit_init:
for i in range(len(channels)):
channel = channels[i]
accessor = access_manager.createVolumeDataPageAccessor(
Expand All @@ -204,12 +189,6 @@ def create_vds(
channel=i,
maxPages=default_max_pages,
)
if init_value != InitValue.omit_init:
INITVALUE[init_value](accessor, channel.format.value)
else:
logger.warning(
"Values initialization was omitted. Remember to do it and close vds resource properly."
)
return vds
INITVALUE[init_value](accessor, channel.format.value)

return vds
70 changes: 37 additions & 33 deletions src/ovds_utils/vds.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

from copy import deepcopy
from typing import AnyStr, List, Sequence, Tuple, Union
from enum import Enum
from typing import List, Sequence, Tuple, Union

import numpy as np
import openvds
Expand Down Expand Up @@ -102,10 +103,10 @@ class Axis:
def __init__(
self,
samples: int,
name: AnyStr,
name: str,
coordinate_min: float,
coordinate_max: float,
unit: AnyStr = "unitless",
unit: str = "unitless",
) -> None:
self.samples = samples
self.name = name
Expand All @@ -120,9 +121,9 @@ def __repr__(self) -> str:
class Channel:
def __init__(
self,
name: AnyStr,
name: str,
format: Formats,
unit: AnyStr,
unit: str,
value_range_min: float,
value_range_max: float,
components: Components,
Expand Down Expand Up @@ -228,17 +229,11 @@ def commit(self):
self.accessor.commit()


class Mode(str, Enum):
READ = "READ"
READ_WRITE = "READ_WRITE"
WRITE = "WRITE"


class VDS:
def __init__(
self,
path: AnyStr,
connection_string: AnyStr = "",
path: str,
connection_string: str = "",
databrick_size: BrickSizes = BrickSizes._128,
metadata_dict: MetadataContainer = {},
channels_data: List[np.array] = None,
Expand All @@ -250,18 +245,18 @@ def __init__(
options: Options = Options._None,
full_resolution_dimension: int = 0,
brick_size_2d_multiplier: int = 4,
init_value: InitValue = InitValue.zero,
mode: Mode = Mode.READ
init_value: InitValue = InitValue.omit_init,
access_mode: AccessModes = AccessModes.ReadOnly
) -> None:
super().__init__()

if mode in (Mode.READ_WRITE, Mode.READ):
if access_mode in {AccessModes.ReadOnly, AccessModes.ReadWrite, AccessModes.ReadWriteWithoutLODGeneration}:
try:
self._vds_source = openvds.open(path, connection_string)
self.initialize(
path=path,
access_mode=access_mode,
connection_string=connection_string,
databrick_size=databrick_size,
)
except RuntimeError as e:
if str(e) == "Open error: File::open ":
Expand All @@ -276,7 +271,7 @@ def __init__(
axes=axes,
metadata_dict=metadata_dict,
channels_data=channels_data,
access_mode=AccessModes.Create,
access_mode=access_mode,
lod=lod,
positive_margin=positive_margin,
negagitve_margin=negative_margin,
Expand All @@ -285,17 +280,18 @@ def __init__(
brick_size_2d_multiplier=brick_size_2d_multiplier,
init_value=init_value
)

self.initialize(
path=path,
connection_string=connection_string,
databrick_size=databrick_size,
access_mode=access_mode,
connection_string=connection_string
)

def initialize(
self,
path: AnyStr,
connection_string: AnyStr = "",
databrick_size: BrickSizes = BrickSizes._128,
path: str,
access_mode: AccessModes,
connection_string: str = "",
):
self.closed = False
self.path = path
Expand All @@ -309,6 +305,7 @@ def initialize(

vds_info = get_vds_info(path, connection_string)
_info = vds_info['layoutInfo'] if 'layoutInfo' in vds_info else vds_info
databrick_size = BrickSizes.get_from_info(_info)

for i, j in enumerate(_info['axisDescriptors']):
self._axes[j['name']] = Axis(
Expand All @@ -320,6 +317,14 @@ def initialize(
)
self.chunks_count = self.count_number_of_chunks(self.shape, databrick_size)
for i, j in enumerate(_info['channelDescriptors']):

if access_mode == AccessModes.Create:
_access_mode = AccessModes.ReadWrite
elif access_mode == AccessModes.CreateWithoutLODGeneration:
_access_mode = AccessModes.ReadWriteWithoutLODGeneration
else:
_access_mode = access_mode

self._channels[j['name']] = Channel(
vds_source=self._vds_source,
shape=self.shape,
Expand All @@ -331,8 +336,8 @@ def initialize(
),
value_range_max=j['valueRange'][1],
value_range_min=j['valueRange'][0],
accessor=self._create_accessor(channel=i),
chunks_count=self.count_number_of_chunks(self.shape, databrick_size)
accessor=self._create_accessor(channel=i, access_mode=_access_mode),
chunks_count=self.chunks_count
)

def channel(self, number: int) -> Channel:
Expand All @@ -346,7 +351,7 @@ def channels(self) -> List[Channel]:
def axes(self) -> List[Axis]:
return list(self._axes.values())[::-1]

def get_channel(self, name: AnyStr) -> Channel:
def get_channel(self, name: str) -> Channel:
return self._channels[name]

def close(self, flush: bool = True):
Expand Down Expand Up @@ -401,22 +406,22 @@ def lod(self) -> LOD:
def __str__(self) -> str:
return f"<{self.__class__.__qualname__}(path={self.path})>"

@ property
@property
def axis_descriptors(self):
return tuple((
self._axes[k].name,
self._axes[k].unit,
self._axes[k].samples
) for k in self._axes)[::-1]

@ property
@property
def shape(self):
return tuple(self._axes[k].samples for k in self._axes)[::-1]

@ staticmethod
@staticmethod
def create(
path: AnyStr,
connection_string: AnyStr,
path: str,
connection_string: str,
databrick_size: BrickSizes,
access_mode: AccessModes,
channels: List[Channel],
Expand Down Expand Up @@ -447,7 +452,6 @@ def create(
brick_size_2d_multiplier=brick_size_2d_multiplier,
full_resolution_dimension=full_resolution_dimension,
init_value=init_value,
# close=True
)

@property
Expand Down
Loading

0 comments on commit 4ff5186

Please sign in to comment.