Skip to content

transform_array

Lazy transformation of array-like data.

This module provides a sequence wrapper which applies a transformation pipeline to elements on access, without materializing the transformed data upfront.

TransformArray

Bases: Sequence

A sequence which applies a processing pipeline lazily.

Elements are transformed by the configured pipeline only when they are accessed, either individually, as slices or through conversion to a numpy array.

Example
ta = transform_array.TransformArray([1, 2, 3], lambda x: x * 2)
ta[0]  # 2 - the element is transformed on access.
ta.to_numpy()  # The whole array transformed: array([2, 4, 6]).
Source code in py_misc_utils/transform_array.py
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
class TransformArray(collections.abc.Sequence):
  """A sequence which applies a processing pipeline lazily.

  Elements are transformed by the configured pipeline only when they are
  accessed, either individually, as slices or through conversion to a
  numpy array.

  Example:
    ta = transform_array.TransformArray([1, 2, 3], lambda x: x * 2)
    ta[0]  # 2 - the element is transformed on access.
    ta.to_numpy()  # The whole array transformed: array([2, 4, 6]).
  """

  def __init__(self, data, pipeline):
    """Initialize the transformed array.

    Args:
      data: Sequence of items to transform.
      pipeline: Callable (e.g. a Pipeline) applied to each accessed item.
    """
    super().__init__()
    self.data = data
    self._pipeline = pipeline
    self.shape = cu.compute_shape(data)

  def __getitem__(self, idx):
    """Return one item or a sub-sequence of transformed items.

    Args:
      idx: Integer index or slice of the items to access.

    Returns:
      The transformed item for an integer index, or a new TransformArray
      over the selected range for a slice.
    """
    if isinstance(idx, slice):
      start, end, step = idx.indices(len(self))
      slices = [self.data[i] for i in range(start, end, step)]

      return __class__(npu.maybe_stack_slices(slices), self._pipeline)

    return self._pipeline(self.data[idx])

  def __len__(self):
    """Return the number of items in the array."""
    return len(self.data)

  def to_numpy(self, dtype=None):
    """Stack all transformed items into a numpy array.

    Args:
      dtype: Optional numpy dtype for the result array.

    Returns:
      A numpy array with all the items transformed by the pipeline.
    """
    slices = [self[i] for i in range(len(self))]
    if not slices:
      return np.empty((0,))
    if not isinstance(slices[0], np.ndarray):
      slices = [np.array(x) for x in slices]

    npa = np.stack(slices, axis=0)

    return npa.astype(dtype) if dtype is not None else npa

  def __array__(self, dtype=None):
    """Support numpy conversion protocols by materializing the array."""
    return self.to_numpy(dtype=dtype)

to_numpy(dtype=None)

Stack all transformed items into a numpy array.

Parameters:

Name Type Description Default
dtype

Optional numpy dtype for the result array.

None

Returns:

Type Description

A numpy array with all the items transformed by the pipeline.

Source code in py_misc_utils/transform_array.py
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
def to_numpy(self, dtype=None):
  """Stack all transformed items into a numpy array.

  Args:
    dtype: Optional numpy dtype for the result array.

  Returns:
    A numpy array with all the items transformed by the pipeline.
  """
  slices = [self[i] for i in range(len(self))]
  if not slices:
    return np.empty((0,))
  if not isinstance(slices[0], np.ndarray):
    slices = [np.array(x) for x in slices]

  npa = np.stack(slices, axis=0)

  return npa.astype(dtype) if dtype is not None else npa