@@ -93,6 +93,44 @@ def backward(self, data, metadata, **kwargs):
9393 return np .reshape (flat_array , newshape = array_shape , order = "C" )
9494
9595
96+ class NumpyArrayToBytes (Transformer ):
97+ """Transformer for converting generic Numpy arrays to bytes."""
98+
99+ def __init__ (self ):
100+ self .lossy = False
101+
102+ def forward (self , data : np .ndarray , ** kwargs ):
103+ """Convert a Numpy array to bytes.
104+
105+ Args:
106+ data: The Numpy array to be converted.
107+ **kwargs: Additional keyword arguments for the conversion.
108+
109+ Returns:
110+ data_bytes: The data converted to bytes.
111+ metadata: The metadata for the conversion.
112+ """
113+ array_shape = data .shape
114+ metadata = {"int_list" : list (array_shape ), "dtype" : str (data .dtype )}
115+ data_bytes = data .tobytes (order = "C" )
116+ return data_bytes , metadata
117+
118+ def backward (self , data , metadata , ** kwargs ):
119+ """Convert bytes back to a Numpy array.
120+
121+ Args:
122+ data: The data in bytes.
123+ metadata: The metadata for the conversion.
124+
125+ Returns:
126+ The data converted back to a Numpy array.
127+ """
128+ array_shape = tuple (metadata ["int_list" ])
129+ dtype = np .dtype (metadata ["dtype" ])
130+ flat_array = np .frombuffer (data , dtype = dtype )
131+ return np .reshape (flat_array , newshape = array_shape , order = "C" )
132+
133+
96134class TransformationPipeline :
97135 """Data Transformer Pipeline Class.
98136
0 commit comments