|
| 1 | +"""Functions and classes for parsing Compressed List data structures.""" |
| 2 | + |
| 3 | +import numpy as np |
| 4 | + |
| 5 | +from .generics import _dispatcher |
| 6 | +from .rdsutils import get_class |
| 7 | + |
| 8 | +__author__ = "jkanche" |
| 9 | +__copyright__ = "jkanche" |
| 10 | +__license__ = "MIT" |
| 11 | + |
| 12 | + |
| 13 | +def read_partitioning_by_end(robject: dict, **kwargs): |
| 14 | + """Read an partioning by end object. |
| 15 | +
|
| 16 | + Args: |
| 17 | + robject: |
| 18 | + Dictionary containing parsed partioning by end object. |
| 19 | +
|
| 20 | + **kwargs: |
| 21 | + Additional arguments. |
| 22 | +
|
| 23 | + Returns: |
| 24 | + A vector containing the partitions. |
| 25 | + """ |
| 26 | + _cls = get_class(robject) |
| 27 | + |
| 28 | + if _cls not in ["PartitioningByEnd"]: |
| 29 | + raise RuntimeError(f"`robject` does not contain not a `PartitioningByEnd` object, contains `{_cls}`.") |
| 30 | + |
| 31 | + ends = _dispatcher(robject["attributes"]["end"], **kwargs) |
| 32 | + |
| 33 | + from compressed_lists import Partitioning |
| 34 | + |
| 35 | + return Partitioning(ends=np.asarray(ends)) |
| 36 | + |
| 37 | + |
| 38 | +def read_compressed_integer_list(robject: dict, **kwargs): |
| 39 | + """Read an R compressed list. |
| 40 | +
|
| 41 | + Args: |
| 42 | + robject: |
| 43 | + Dictionary containing parsed compressed list. |
| 44 | +
|
| 45 | + **kwargs: |
| 46 | + Additional arguments. |
| 47 | +
|
| 48 | + Returns: |
| 49 | + A `CompressedList` from the 'compressed_lists' package. |
| 50 | + """ |
| 51 | + _cls = get_class(robject) |
| 52 | + |
| 53 | + if _cls not in ["CompressedIntegerList"]: |
| 54 | + raise RuntimeError(f"`robject` does not contain not a compressed integer list object, contains `{_cls}`.") |
| 55 | + |
| 56 | + if "unlistData" not in robject["attributes"]: |
| 57 | + raise ValueError("Object does not contain unlistData, is it really a `compressedList`?") |
| 58 | + unlist_data = _dispatcher(robject["attributes"]["unlistData"], **kwargs) |
| 59 | + |
| 60 | + print("unlist_data", unlist_data) |
| 61 | + |
| 62 | + element_metadata = None |
| 63 | + if "elementMetadata" in robject["attributes"]: |
| 64 | + element_metadata = _dispatcher(robject["attributes"]["elementMetadata"], **kwargs) |
| 65 | + |
| 66 | + print("element_metadata", element_metadata) |
| 67 | + |
| 68 | + metadata = None |
| 69 | + if "metadata" in robject["attributes"]: |
| 70 | + metadata = _dispatcher(robject["attributes"]["metadata"], **kwargs) |
| 71 | + |
| 72 | + print("metadata", metadata) |
| 73 | + |
| 74 | + partition = None |
| 75 | + if "partitioning" in robject["attributes"]: |
| 76 | + partition = _dispatcher(robject["attributes"]["partitioning"], **kwargs) |
| 77 | + |
| 78 | + print("partition", partition) |
| 79 | + |
| 80 | + from compressed_lists import CompressedIntegerList |
| 81 | + |
| 82 | + return CompressedIntegerList( |
| 83 | + unlist_data=unlist_data, partitioning=partition, element_metadata=element_metadata, metadata=metadata |
| 84 | + ) |
0 commit comments