Commit bc25a287 authored by Vincent POLLET's avatar Vincent POLLET

[DOC] Documentation of stream.py, move select and astype filters to...

[DOC] Documentation of stream.py, move select and astype filters to stream_filters.py, doc fix in utils.
parent 8358ea0a
Pipeline #43985 passed with stage
in 15 minutes and 2 seconds
......@@ -2,10 +2,12 @@ from bob.io.stream.stream_file import StreamFile
from bob.io.stream.stream import Stream, stream_filter, StreamFilter
# import filters defined in stream.py
from bob.io.stream.stream import StreamAsType, StreamSelect, StreamView, StreamFilter, StreamSave
from bob.io.stream.stream import StreamView, StreamSave
# import filters defined in stream_filters
from bob.io.stream.stream_filters import (
StreamAsType,
StreamSelect,
StreamColorMap,
StreamNormalize,
StreamClean,
......
This diff is collapsed.
......@@ -8,10 +8,12 @@ from .config import load_data_config
class StreamFile:
"""File class to read from HDF5 streams files.
"""File class to read and write from HDF5 files.
Exposes methods to read a stream's data and meta-data. The class is initialized via
:meth:`~bob.io.stream.StreamFile.set_source`.
Exposes methods to read a stream's data and meta-data. The format of the data in the hdf5 file is defined through a
configuration dictionary.
The class can also be used to write a HDF5 file, through the :meth:`~bob.io.stream.StreamFile.put_frame` method.
Attributes
----------
......@@ -232,6 +234,14 @@ class StreamFile:
# TODO rotate if requested
return data
# write frame data
def put_frame(self, name, data):
"""Appends `data` (a frame of a stream) to the hdf5 file.
Parameters
----------
name : str
Path to the dataset to append to.
data : obj:`numpy.ndarray`
Data frame to append.
"""
self.hdf5_file.append(name, data)
......@@ -10,6 +10,107 @@ from .utils import convert_cv_to_bob, StreamArray, get_index_list
from .stream import stream_filter, StreamFilter
@stream_filter("astype")
class StreamAsType(StreamFilter):
"""Filter to cast the data to a different numpy dtype.
Attributes
----------
dtype : :obj:`numpy.dtype`
The dtype to which to cast the data.
"""
def __init__(self, name, parent, dtype):
"""Set `dtype` and initializes super().
Parameters
----------
name : str
"astyype": identifier name to use this filter from the :obj:`~bob.io.stream.Stream` class.
parent : :obj:`~bob.io.stream.Stream` or :obj:`~bob.io.stream.StreamFilter`
Parent Stream(Filter).
dtype : :obj:`numpy.dtype`
dtype to cast to.
"""
super().__init__(name=name, parent=parent)
self.dtype = dtype
def process(self, data, indices):
"""Cast `data` to `dtype`.
Parameters
----------
data : :obj:`numpy.ndarray`
Data to cast.
indices : int or :obj:`list` of int
Not used. Present for compatibility with other filters.
Returns
-------
:obj:`numpy.ndarray`
`data` casted to `dtype`.
"""
return data.astype(self.dtype)
@stream_filter("select")
class StreamSelect(StreamFilter):
"""Filter to select a channel in a color stream (in bob's format).
This could also be performed by slicing the channel in the parent.
Attributes
----------
channel : int
Index of the channel to keep.
"""
def __init__(self, name, parent, channel):
"""Set `channel` and initializes super() name and parent.
Parameters
----------
name : str
"select": identifier name to use this filter from the :obj:`~bob.io.stream.Stream` class.
parent : :obj:`~bob.io.stream.Stream` or :obj:`~bob.io.stream.StreamFilter`
Parent Stream(Filter).
channel : int
The channel to select in the color stream.
"""
super().__init__(name=name, parent=parent)
self.channel = channel
@property
def shape(self):
"""Shape of the stream's data.
Because 1 channel is selected, the dimension is 1 on the channel axis.
Returns
-------
:obj:`tuple` of int
Shape of the stream's data.
"""
return (self.parent.shape[0], 1, self.parent.shape[2], self.parent.shape[3])
def process(self, data, indices):
"""Select the required channel in `data`.
Parameters
----------
data : :obj:`numpy.ndarray`
Color data, from which a channel is selected.
indices : int
Not used. Present for compatibility with other filters.
Returns
-------
:obj:`numpy.ndarray`
Selected channel in `data`.
"""
return np.expand_dims(data[:, self.channel, :, :], axis=1)
@stream_filter("colormap")
class StreamColorMap(StreamFilter):
"""Filter to map a 1 channel images to RGB images, usefull for visualization, eg of depth maps.
......
......@@ -13,7 +13,7 @@ from bob.io.stream.utils import get_axis_size, get_index_list, rotate_data
def test_rotate_data():
"""Unit tests for `~bob.io.stream.utils.rotate_data`."""
"""Unit tests for :func:`~bob.io.stream.utils.rotate_data`."""
# 2D
test_array = np.array([[1, 2], [3, 4]])
......@@ -35,7 +35,7 @@ def test_rotate_data():
def test_get_axis_size():
"""Unit tests for `~bob.io.stream.utils.get_axis_size`."""
"""Unit tests for :func:`~bob.io.stream.utils.get_axis_size`."""
test_array = np.arange(15000).reshape(10, 3, 50, 10)
......@@ -58,7 +58,7 @@ def test_get_axis_size():
def test_get_index_list():
"""Unit tests for `~bob.io.stream.utils.get_index_list`."""
"""Unit tests for :func:`~bob.io.stream.utils.get_index_list`."""
test_array = np.arange(15000).reshape(10, 3, 50, 10)
......
......@@ -122,7 +122,6 @@ def _crop(video, mask, crop):
return video, mask
# TODO: check all boundary cases
def get_index_list(index, size):
"""From an indexing value of type int, slice, list or None, generates the equivalent list of indices in a 1d array.
......@@ -235,7 +234,7 @@ def get_axis_size(shape, axis, indices=None):
class StreamArray:
"""Class to associate data to a `~bob.io.stream.Stream`, for instance bounding boxes to a video stream.
"""Class to associate data to a :class:`~bob.io.stream.Stream`, for instance bounding boxes to a video stream.
This class allows to set the value of the data array (eg the bounding box at some or each frame of a stream) without
having to care about the shape of the stream.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment