BnpDataclass
BioNumPy extends the concept of Python’s dataclass decorator, by providing a bnpdataclass-decorator. This decorator enables dataclass-like objects with numpy-array-like fields (EncodedArray, EncodedRaggedArray, RaggedArray or pure numpy-arrays). See examples of usage in the documentation below.
API documentation
- bionumpy.bnpdataclass.bnpdataclass(base_class: type) Type[BNPDataClass] [source]
Create a bnpdataclass from a class with fields specified
A wrapper around @npdataclass that includes implicit format conversion for strings and other encoded data. @npdataclass is again a wrapper around dataclasses.dataclass but where all the fields are assumed to be objects that supports advanced numpy indexing so that any dataclass objects are also indexible like a numpy array.
bnpdataclass classes are meant to be dataclasses and so should not have any methods other than those implicitly given by npdataclass.
Parameters
- base_classtype
Base class that defines the fields of the dataclass.
Returns
- bnpdataclass
bnpdataclass object that supports numpy like indexing
Examples
>>> from bionumpy.bnpdataclass import bnpdataclass >>> @bnpdataclass ... class Person: ... name: str ... age: int ... >>> data = Person(["Knut", "Ivar", "Geir"], [35, 30, 40]) >>> print(data) Person with 3 entries name age Knut 35 Ivar 30 Geir 40
>>> print(data[[0,2]]) Person with 2 entries name age Knut 35 Geir 40
>>> print(data[[False,True, False]]) Person with 1 entries name age Ivar 30
- bionumpy.bnpdataclass.replace(obj: BNPDataClass, **kwargs) BNPDataClass [source]
Replace the values of a dataclass with new values
Parameters
- objBNPDataClass
The dataclass to be replaced
- kwargsdict
The new values to be replaced
Returns
- BNPDataClass
The new dataclass with the replaced values
Examples
>>> import bionumpy as bnp >>> entry = bnp.SequenceEntry(['seq1'], ['acgt']) >>> entry SequenceEntry with 1 entries name sequence seq1 acgt >>> bnp.replace(entry, name=['seq2']) SequenceEntry with 1 entries name sequence seq2 acgt
- bionumpy.bnpdataclass.make_dataclass(fields: List[Tuple], name: str = 'DynamicDC', bases=()) Type[BNPDataClass] [source]
Constructs a dynamic dataclass from a list of attributes
Parameters
- fields: list
a list of tuples in format (field_name, field_type) to be used to construct the dynamic bnp dataclass
- name: str
optional name of new class
Returns
new BNPDataClass