comdas.codecs.svd¶
Truncated Singular Value Decomposition (SVD) codec for 2D arrays.
Classes
|
Truncated-Singular Value Decomposition (SVD) codec for 2D arrays. |
|
Truncated-SVD representation of a 2D array: |
- class comdas.codecs.svd.SVDPayload(original_shape, dtype, U, s, Vt)¶
Bases:
CompressedPayloadTruncated-SVD representation of a 2D array:
A ~= (U * s) @ Vt.- Variables:
U (numpy.ndarray) – Left singular vectors, shape
(m, k).s (numpy.ndarray) – Singular values, shape
(k,).Vt (numpy.ndarray) – Right singular vectors (transposed), shape
(k, n).
- property rank: int¶
The truncation rank
kused on this payload.- Return type:
int
- property compression_params: dict¶
The compression parameters used for this payload.
- Return type:
dict
- class comdas.codecs.svd.SVDCodec(rank=None, energy=None)¶
Bases:
CodecTruncated-Singular Value Decomposition (SVD) codec for 2D arrays.
This codec compresses a 2D array by computing its truncated singular value decomposition (SVD) and storing the left singular vectors, singular values, and right singular vectors (transposed) as a
SVDPayload.Truncation rank can be chosen either as a fixed integer
rankor as a retained energy fractionenergyin(0,1]. Exactly one option must be specified, either as a codec-level default or per-call parameter.SVD does not implement partial writes efficiently, so the full array is re-encoded every 10,000 pending writes (or on demand).
- Parameters:
rank (
int|None) – Default truncation rankkto use if not specified per-call.energy (
float|None) – Default retained energy fraction to use if not specified per-call.
- Raises:
ValueError – If both of
rankandenergyare specified.
- encode(array, *, rank=None, energy=None, **kwargs)¶
Compress a 2D array via truncated SVD.
- Parameters:
array (
ndarray) – The dense 2D array to compress.rank (
int|None) – Fixed truncation rank. Overridesself.default_rankfor this call.energy (
float|None) – Retained energy fraction in(0, 1]. Overridesself.default_energyfor this call.kwargs – Accepted and ignored for compatibility.
- Returns:
The truncated-SVD payload.
- Return type:
- Raises:
ValueError – If
arrayis not 2D, or if bothrankandenergyare specified.
- decode(payload)¶
Fully reconstruct the dense array as
(U * s) @ Vt.- Parameters:
payload (
SVDPayload) – A payload previously produced byencode().- Returns:
The reconstructed dense array.
- Return type:
ndarray
- decode_partial(payload, key)¶
Reconstruct only the requested rows/cols of
payload.- Parameters:
payload (
SVDPayload) – A payload previously produced byencode().key – An int/slice index in the NumPy-style.
- Returns:
The requested subset of the reconstructed array.
- Return type:
ndarray
- compressed_size_bytes(payload)¶
Get the total number of bytes used by
U,s, andVt.- Parameters:
payload (
SVDPayload) – A payload previously produced byencode().- Returns:
Combined byte size of the three stored arrays.
- Return type:
int
- payload_to_group(payload, group)¶
Write
U,s,Vt, andrankinto an openh5py.Group.- Parameters:
payload (
SVDPayload) – The payload to serialize.group (h5py.Group) – An open, writable
h5py.Groupdedicated to this one patch.
- Return type:
None
- payload_from_group(group, *, original_shape, dtype)¶
Construct an
SVDPayloadfrom an HDF5 group.- Parameters:
group (h5py.Group) – An open, readable
h5py.Grouppreviously written bypayload_to_group().original_shape (
tuple[int,...]) – The array shape, as read from the container’s own attributes.dtype (
dtype) – The array dtype, as read from the container’s own attributes.
- Returns:
The reconstructed payload.
- Return type: