str2d.Str2D.struct_pad

classmethod Str2D.struct_pad(array, *args, **kwargs) Str2D[source]

Pads a structured array with fields ‘char’ and ‘alpha’. The method is a wrapper around np.pad that pads the ‘char’ and ‘alpha’ fields with the fill value specified in the ‘fill’ keyword argument.

Parameters:
  • array (np.ndarray) – A structured array with fields ‘char’ and ‘alpha’.

  • mode (str, optional) – The padding mode, by default ‘constant’. Other options are documented in np.pad but the only other one that makes sense in a character array context is ‘edge’.

  • fill (Tuple[str, int], optional) – The fill value for the ‘char’ and ‘alpha’ fields, by default (’ ‘, 0)

  • *args (tuple) – Positional arguments to np.pad.

  • **kwargs (dict) – Keyword arguments to np.pad.

Returns:

A structured array with fields ‘char’ and ‘alpha’ padded with the fill value specified in the ‘fill’ keyword argument.

Return type:

np.ndarray

Examples

Let’s use Str2D to create a structured array from a string and then pad it.

from str2d import Str2D

a = Str2D('a b c d\ne f g\nh i\nj')
a.char
array([['a', ' ', 'b', ' ', 'c', ' ', 'd'],
       ['e', ' ', 'f', ' ', 'g', ' ', ' '],
       ['h', ' ', 'i', ' ', ' ', ' ', ' '],
       ['j', ' ', ' ', ' ', ' ', ' ', ' ']], dtype='<U1')

Now let’s pad the structured array with the fill value ‘.’. The first argument is the data. The next argument is the number of padding elements to add to the beginning and end of each axis.

Str2D.struct_pad(a.data, 1, fill=('.', 0))
array([['.', '.', '.', '.', '.', '.', '.'],
       ['.', 'a', ' ', 'b', ' ', 'c', 'd'],
       ['.', 'e', ' ', 'f', ' ', 'g', ' '],
       ['.', 'h', ' ', 'i', ' ', ' ', ' '],
       ['.', 'j', ' ', ' ', ' ', ' ', ' '],
       ['.', '.', '.', '.', '.', '.', '.']], dtype='<U1')

If we want to control the padding on each side of the array, we can pass a tuple of integers to the second argument. The first integer is the number of padding elements to add to the beginning of each axis and the second integer is the number of padding elements to add to the end of each axis.

Str2D.struct_pad(a.data, (1, 2), fill=('.', 0))
array([['.', '.', '.', '.', '.', '.', '.', '.', '.', '.'],
       ['.', 'a', ' ', 'b', ' ', 'c', ' ', 'd', '.', '.'],
       ['.', 'e', ' ', 'f', ' ', 'g', ' ', ' ', '.', '.'],
       ['.', 'h', ' ', 'i', ' ', ' ', ' ', ' ', '.', '.'],
       ['.', 'j', ' ', ' ', ' ', ' ', ' ', ' ', '.', '.'],
       ['.', '.', '.', '.', '.', '.', '.', '.', '.', '.'],
       ['.', '.', '.', '.', '.', '.', '.', '.', '.', '.']], dtype='<U1')

However, you can also control the padding on each side of the array by passing the number of padding elements to add to the beginning and end of each axis as as tuple of tuples. The first tuple is the number of padding elements to add to the beginning and end of the first axis and the second tuple is the number of padding elements to add to the beginning and end of the second axis.

Str2D.struct_pad(a.data, ((1, 3), (2, 1)), fill=('.', 0))
array([['.', '.', '.', '.', '.', '.', '.', '.', '.', '.'],
       ['.', '.', 'a', ' ', 'b', ' ', 'c', ' ', 'd', '.'],
       ['.', '.', 'e', ' ', 'f', ' ', 'g', ' ', ' ', '.'],
       ['.', '.', 'h', ' ', 'i', ' ', ' ', ' ', ' ', '.'],
       ['.', '.', 'j', ' ', ' ', ' ', ' ', ' ', ' ', '.'],
       ['.', '.', '.', '.', '.', '.', '.', '.', '.', '.'],
       ['.', '.', '.', '.', '.', '.', '.', '.', '.', '.'],
       ['.', '.', '.', '.', '.', '.', '.', '.', '.', '.']], dtype='<U1')

Another thing we can do is to pass a different type of padding mode. The default is ‘constant’ which will fill the padding elements with the fill value. However, we can also use ‘edge’ which will fill the padding elements with the nearest edge value.

Str2D.struct_pad(a.data, 1, mode='edge')
array([['a', 'a', ' ', 'b', ' ', 'c', ' ', 'd', 'd'],
       ['a', 'a', ' ', 'b', ' ', 'c', ' ', 'd', 'd'],
       ['e', 'e', ' ', 'f', ' ', 'g', ' ', ' ', ' '],
       ['h', 'h', ' ', 'i', ' ', ' ', ' ', ' ', ' '],
       ['j', 'j', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
       ['j', 'j', ' ', ' ', ' ', ' ', ' ', ' ', ' ']], dtype='<U1')