Source code for signal_dataset.dataset.instructions
"""Where records live, without reading any of them.The library ships a reader, and a training pipeline may not want it: the loaderalready owns prefetch depth, worker count, shuffling and caching, and those areits decisions to make. What it cannot own is knowing where ordinal 4,821,003lives -- that is the format, and reimplementing it means parsing ``root.json``,snapshot and manifest paging, and the ArrayRecord and SafeTensors layout, whichis the whole thing this library exists to define.So the addressing is public, and separate from the fetching. A caller askswhere a range of records is, fetches those bytes however it likes, and turnsthem back into records with :func:`signal_dataset.decode`.The shape is deliberately the one TFDS and ArrayRecord already agreed on.``array_record_data_source.py`` defines a ``FileInstruction`` protocol -- thefields below, structurally checked -- specifically so that a library which*plans* can hand descriptors to a reader which *executes*, with no dependencybetween them. Emitting the same shape means those readers accept theseinstructions unchanged."""from__future__importannotationsfromdataclassesimportdataclass
[docs]@dataclass(frozen=True,slots=True)classReadInstruction:"""A contiguous run of records within one shard object. Inert by design: it names bytes and does not fetch them. Iceberg's ``FileScanTask`` and TFDS's ``FileInstruction``, the two closest analogues, are likewise plain records that the caller reads for itself. """#: Absolute URI of the shard object holding these records.filename:str#: Index of the first record to take, within that shard.skip:int#: How many records to take.take:int#: Total records the shard holds, which a reader may use to size a buffer.examples_in_shard:int#: Ordinal of the first record in the dataset's own numbering.first_ordinal:intdef__post_init__(self)->None:fornamein("skip","take","examples_in_shard","first_ordinal"):value=getattr(self,name)ifisinstance(value,bool)ornotisinstance(value,int)orvalue<0:raiseValueError(f"{name} must be a nonnegative integer")ifself.skip+self.take>self.examples_in_shard:raiseValueError("instruction runs past the end of its shard")