Format
timenet.format ¶source
The TimeF on-disk format contract: filenames, layout templates, and pinned Arrow schemas.
The writer and the reader both use this module. They are otherwise independent. The definitions that both sides of the round-trip must agree on live here, so neither side imports the other.
checksums ¶source
File checksums recorded in the manifest and verified on read.
The reader must hash files the same way the writer did, or :meth:~timenet.reader.TimeFReader.verify
means nothing. The algorithm and the block size live here so both sides call the same code.
stream_checksum ¶source
stream_checksum(handle: _Readable) -> str
Return an open binary stream's manifest checksum, hashing it a block at a time.
This is the stream counterpart of :func:file_checksum. A reader can use it to hash a file
opened through a pyarrow filesystem (local now, an object store later) instead of a local
Path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
handle
|
_Readable
|
An open binary stream positioned at the start. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The checksum as |
constants ¶source
On-disk filenames and layout templates for the TimeF format.
check_relative_path ¶source
Reject a path that would resolve outside the dataset root it is meant to stay under.
Callers join a manifest file entry and a registry handle's relpath onto a root directory
(or filesystem prefix) as-is. An absolute path or a .. segment would escape that root instead
of raising. This would let a crafted manifest or caller read or write outside the intended
directory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The name of the field to check. It appears in the error message. |
required |
path
|
str
|
The path to check. It must be relative and rooted inside the dataset. |
required |
Raises:
| Type | Description |
|---|---|
TimeFValidationError
|
If |
part_path ¶source
Render a numbered part/shard path, refusing an index that would overflow the fixed width.
The reader visits parts in the order the manifest lists them. The zero-padded names keep an
ls or a prefix listing in that same order. That order only holds while every number fits in
PART_INDEX_DIGITS. A ninth digit sorts before the eighth and silently breaks it. Routing
every part name through here turns that overflow into a loud error instead.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
template
|
str
|
A layout template whose part number is the |
required |
index
|
int
|
The zero-based part number to render. |
required |
fields
|
str
|
Any remaining named fields the template needs, for example |
{}
|
Returns:
| Type | Description |
|---|---|
str
|
The formatted version-relative path. |
Raises:
| Type | Description |
|---|---|
TimeFValidationError
|
If |
schemas ¶source
Arrow schemas for the TimeF on-disk files, parameterized by id storage type.
The column layout is fixed. Only the id columns vary. Every id column holds one of the six logical ids
in :data:LOGICAL_IDS. Each is stored as pa.string(), or as pa.binary(16) when every value is a
canonical UUID. pa.binary(16) uses 16 raw bytes instead of a 36-character string. The writer picks
the type per logical id and writes it into the Parquet schemas. The reader infers the types from the
records file schema. binary(16) decodes back to the canonical string, so callers always see
string ids.
LOGICAL_IDS
module-attribute
¶source
LOGICAL_IDS: tuple[str, ...] = (
"record_id",
"time_series_id",
"annotation_id",
"task_id",
"source_id",
"subject_id",
)
TASK_COMMON_NAMES
module-attribute
¶source
TASK_COMMON_NAMES: tuple[str, ...] = (
"id",
"record_ids",
"from_task_ids",
"prompt",
"scope",
"input_annotation_ids",
"target_annotation_ids",
"rationale",
)
IdCodec
dataclass
¶source
Convert ids between their in-memory strings and their on-disk form.
This is the one place that holds the logical-id-per-column mapping and the bytes <-> str
conversion. The writer and the reader each build a codec (:meth:from_uuid16 or
:meth:from_encoding). Both call it, so the two halves of the format contract cannot drift apart.
uuid16
instance-attribute
¶source
The logical ids whose columns are stored as binary(16). The rest are pa.string().
decode ¶source
Decode one required id, turning 16 raw bytes back into a canonical string for uuid16.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
logical
|
str
|
The logical id the column holds. |
required |
value
|
object
|
The raw cell value (bytes for a |
required |
Returns:
| Type | Description |
|---|---|
str
|
The canonical id string. |
decode_payload ¶source
Decode a task payload cell holding ids or spans, leaving plain payload untouched.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
refs
|
TaskRefs
|
The owning task class's reference declaration. |
required |
name
|
str
|
The payload column name. |
required |
value
|
object
|
The (already tuple-normalized) cell value. |
required |
Returns:
| Type | Description |
|---|---|
object
|
The decoded value. |
decode_span ¶source
Rebuild a span from its struct row, decoding the series ids it is scoped to.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
row
|
object
|
The struct row read from a task partition, or |
required |
Returns:
| Type | Description |
|---|---|
Span | None
|
The span, or |
Raises:
| Type | Description |
|---|---|
TimeFFormatError
|
If the frame is neither |
encode ¶source
Encode one id to 16 raw bytes for a uuid16 column, else pass it through unchanged.
A reference id outside the entity id space (for example, a ForecastingTask.target_record_id
that names no record) raises a contextual :class:TimeFValidationError naming the column and
value. This makes the writer fail legibly even if referential validation is bypassed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
logical
|
str
|
The logical id the column holds (for example, |
required |
value
|
object
|
The id string, or |
required |
Returns:
| Type | Description |
|---|---|
object
|
The 16-byte form for a |
Raises:
| Type | Description |
|---|---|
TimeFValidationError
|
If a |
encode_payload ¶source
Encode a task payload cell holding ids or spans, leaving plain payload untouched.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
refs
|
TaskRefs
|
The owning task class's reference declaration. |
required |
name
|
str
|
The payload column name. |
required |
value
|
object
|
The (already list-normalized) cell value. |
required |
Returns:
| Type | Description |
|---|---|
object
|
The encoded value. |
encode_span ¶source
Encode a span to its struct row, encoding the series ids it is scoped to.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
span
|
Span | None
|
The span, or |
required |
Returns:
| Type | Description |
|---|---|
dict | None
|
The struct row, or |
Raises:
| Type | Description |
|---|---|
TimeFValidationError
|
If |
annotations_schema ¶source
annotations_schema(id_types: IdTypes) -> Schema
Return the annotations table schema.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
id_types
|
IdTypes
|
The resolved id storage types. |
required |
Returns:
| Type | Description |
|---|---|
Schema
|
The Arrow schema. |
default_id_types ¶source
default_id_types() -> IdTypes
Return the all-string id types.
Returns:
| Type | Description |
|---|---|
IdTypes
|
A mapping from every logical id to |
id_types_from_records_schema ¶source
id_types_from_records_schema(schema: Schema) -> IdTypes
Infer every logical id's Arrow type from the records Parquet schema.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
schema
|
Schema
|
The Arrow schema of any records part file. |
required |
Returns:
| Type | Description |
|---|---|
IdTypes
|
A mapping from every logical id to its Arrow type. |
index_schema ¶source
index_schema(id_types: IdTypes) -> Schema
Return the time-series index table schema.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
id_types
|
IdTypes
|
The resolved id storage types. |
required |
Returns:
| Type | Description |
|---|---|
Schema
|
The Arrow schema. |
records_schema ¶source
records_schema(id_types: IdTypes) -> Schema
Return the records table schema.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
id_types
|
IdTypes
|
The resolved id storage types. |
required |
Returns:
| Type | Description |
|---|---|
Schema
|
The Arrow schema. |
shard_schema ¶source
shard_schema(
id_types: IdTypes,
value_type: DataType = _DEFAULT_VALUES_TYPE,
) -> Schema
Return the waveform shard schema.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
id_types
|
IdTypes
|
The resolved id storage types. |
required |
value_type
|
DataType
|
The element type of the |
_DEFAULT_VALUES_TYPE
|
Returns:
| Type | Description |
|---|---|
Schema
|
The Arrow schema. |
span_struct ¶source
span_struct(id_types: IdTypes) -> DataType
Return the struct type a :class:~timenet.types.Span is stored as.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
id_types
|
IdTypes
|
The resolved id storage types. |
required |
Returns:
| Type | Description |
|---|---|
DataType
|
The struct type used for a task's |
task_schema ¶source
Return the Arrow schema for one task partition.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
task_type
|
TaskType
|
The task type whose partition schema to build. |
required |
id_types
|
IdTypes | None
|
The resolved id storage types (defaults to all- |
None
|
Returns:
| Type | Description |
|---|---|
Schema
|
The Arrow schema (common columns plus the type's payload columns). |
Raises:
| Type | Description |
|---|---|
TimeFValidationError
|
If the task dataclass payload and its Arrow schema have drifted. |
time_series_struct ¶source
time_series_struct(id_types: IdTypes) -> DataType
Return the per-record nested time-series struct type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
id_types
|
IdTypes
|
The resolved id storage types. |
required |
Returns:
| Type | Description |
|---|---|
DataType
|
The struct type used inside |