webknossos.dataset.view
A View
is essentially a bounding box to a region of a specific StorageBackend
that also provides functionality.
Write-operations are restricted to the bounding box.
View
s are designed to be easily passed around as parameters.
A View
, in its most basic form, does not have a reference to its StorageBackend
.
Do not use this constructor manually. Instead use View.get_view()
(also available on a MagView
) to get a View
.
⚠️ Deprecated, use view.bounding_box.in_mag(view.mag).topleft
instead.
Writes the data
at the specified relative_offset
or absolute_offset
, both specified in Mag(1).
⚠️ The offset
parameter is deprecated.
This parameter used to be relative for View
and absolute for MagView
,
and specified in the mag of the respective view.
Writing data to a segmentation layer manually does not automatically update the largest_segment_id. To set
the largest segment id properly run the refresh_largest_segment_id
method on your layer or set the
largest_segment_id
property manually..
Example:
ds = Dataset(DS_PATH, voxel_size=(1, 1, 1))
segmentation_layer = cast(
SegmentationLayer,
ds.add_layer("segmentation", SEGMENTATION_CATEGORY),
)
mag = segmentation_layer.add_mag(Mag(1))
mag.write(data=MY_NP_ARRAY)
segmentation_layer.refresh_largest_segment_id()
Note that writing compressed data which is not aligned with the blocks on disk may result in diminished performance, as full blocks will automatically be read to pad the write actions.
The user can specify which data should be read. The default is to read all data of the view's bounding box. Alternatively, one can supply one of the following keyword argument combinations:
relative_offset
andsize
, both in Mag(1)absolute_offset
andsize
, both in Mag(1)relative_bounding_box
in Mag(1)absolute_bounding_box
in Mag(1)- ⚠️ deprecated:
offset
andsize
, both in the current Mag.offset
used to be relative forView
and absolute forMagView
If the specified bounding box exceeds the data on disk, the rest is padded with 0
.
Returns the specified data as a np.array
.
Example:
import numpy as np
# ...
# let mag1 be a MagView
view = mag1.get_view(absolute_offset=(10, 20, 30), size=(100, 200, 300))
assert np.array_equal(
view.read(absolute_offset=(0, 0, 0), size=(100, 200, 300)),
view.read(),
)
⚠️ Deprecated. Please use read()
with relative_bounding_box
or absolute_bounding_box
in Mag(1) instead.
The user can specify the bounding_box
in the current mag of the requested data.
See read()
for more details.
Returns a view that is limited to the specified bounding box.
The new view may exceed the bounding box of the current view only if read_only
is set to True
.
The default is to return the same view as the current bounding box,
in case of a MagView
that's the layer bounding box.
One can supply one of the following keyword argument combinations:
relative_offset
andsize
, both in Mag(1)absolute_offset
andsize
, both in Mag(1)- ⚠️ deprecated:
offset
andsize
, both in the current Mag.offset
used to be relative forView
and absolute forMagView
Example:
# ...
# let mag1 be a MagView
view = mag1.get_view(absolute_offset=(10, 20, 30), size=(100, 200, 300))
# works because the specified sub-view is completely in the bounding box of the view
sub_view = view.get_view(relative_offset=(50, 60, 70), size=(10, 120, 230))
# fails because the specified sub-view is not completely in the bounding box of the view
invalid_sub_view = view.get_view(relative_offset=(50, 60, 70), size=(999, 120, 230))
# works because `read_only=True`
valid_sub_view = view.get_view(relative_offset=(50, 60, 70), size=(999, 120, 230), read_only=True)
The returned writer buffers multiple slices before they are written to disk. As soon as the buffer is full, the data gets written to disk.
Arguments:
- The user can specify where the writer should start:
buffer_size
: amount of slices that get buffereddimension
: dimension along which the data is sliced (x:0
, y:1
, z:2
; default is2
)).
The writer must be used as context manager using the with
syntax (see example below),
which results in a generator consuming np.ndarray-slices via writer.send(slice)
.
Exiting the context will automatically flush any remaining buffered data to disk.
Usage:
data_cube = ...
view = ...
with view.get_buffered_slice_writer() as writer:
for data_slice in data_cube:
writer.send(data_slice)
The returned reader yields slices of data along a specified axis. Internally, it reads multiple slices from disk at once and buffers the data.
Arguments:
- The user can specify where the writer should start:
buffer_size
: amount of slices that get buffereddimension
: dimension along which the data is sliced (x:0
, y:1
, z:2
; default is2
)).
The reader must be used as a context manager using the with
syntax (see example below).
Entering the context returns an iterator yielding slices (np.ndarray).
Usage:
view = ...
with view.get_buffered_slice_reader() as reader:
for slice_data in reader:
...
The view is chunked into multiple sub-views of size chunk_shape
(in Mag(1)),
by default one chunk per file.
Then, func_per_chunk
is performed on each sub-view.
Besides the view, the counter i
is passed to the func_per_chunk
,
which can be used for logging.
Additional parameters for func_per_chunk
can be specified using functools.partial
.
The computation of each chunk has to be independent of each other.
Therefore, the work can be parallelized with executor
.
If the View
is of type MagView
only the bounding box from the properties is chunked.
Example:
from webknossos.utils import named_partial
def some_work(args: Tuple[View, int], some_parameter: int) -> None:
view_of_single_chunk, i = args
# perform operations on the view
...
# ...
# let 'mag1' be a [`MagView`](../../webknossos/dataset/mag_view.html#MagView)
func = named_partial(some_work, some_parameter=42)
mag1.for_each_chunk(
func,
)
The view is chunked into multiple sub-views of size chunk_shape
(in Mag(1)),
by default one chunk per file.
Then, func_per_chunk
is performed on each sub-view and the results are collected
in a list.
Additional parameters for func_per_chunk
can be specified using functools.partial
.
The computation of each chunk has to be independent of each other.
Therefore, the work can be parallelized with executor
.
If the View
is of type MagView
only the bounding box from the properties is chunked.
Example:
from webknossos.utils import named_partial
def some_work(view: View, some_parameter: int) -> None:
# perform operations on the view
...
# ...
# let 'mag1' be a [`MagView`](../../webknossos/dataset/mag_view.html#MagView)
func = named_partial(some_work, some_parameter=42)
results = mag1.map_chunk(
func,
)
This method is similar to for_each_chunk
in the sense that it delegates work to smaller chunks,
given by source_chunk_shape
and target_chunk_shape
(both in Mag(1),
by default using the larger of the source_views and the target_views file-sizes).
However, this method also takes another view as a parameter. Both views are chunked simultaneously
and a matching pair of chunks is then passed to the function that shall be executed.
This is useful if data from one view should be (transformed and) written to a different view,
assuming that the transformation of the data can be handled on chunk-level.
Additionally to the two views, the counter i
is passed to the func_per_chunk
, which can be used for logging.
The mapping of chunks from the source view to the target is bijective.
The ratio between the size of the source_view
(self
) and the source_chunk_shape
must be equal to
the ratio between the target_view
and the target_chunk_shape
. This guarantees that the number of chunks
in the source_view
is equal to the number of chunks in the target_view
.
The target_chunk_shape
must be a multiple of the file size on disk to avoid concurrent writes.
Example use case: downsampling from Mag(1) to Mag(2)
- size of the views:
16384³
(8192³
in Mag(2) fortarget_view
) - automatic chunk sizes:
2048³
, assuming default file-lengths (1024³
in Mag(2), which fits the default file-length of 32*32)