Core: Data¶
All dataset, preprocessing, representation, and discovery machinery.
Current layout¶
datasets/- raw source adapters and dataset/source builders.discovery/- signal profiles, canonical entities, and provisional hypotheses for cross-vehicle alignment.preprocessing/- explicit representations, views, segments, materialization, PyG packing, temporal streams, scaler config, vocab config, and graph transforms.datamodule/- training-time loaders and batching policy.state.py- process-local dataset state for reuse within one Python process.
What to read next¶
graphids.core.data¶
data ¶
Data-layer public API.
The runtime datamodules are imported lazily so preprocessing and discovery can be used without importing optional training dependencies.
CANBusDataset ¶
CANBusDataset(root: str | Path, raw_dir: str | Path, *, val_fraction: float, split: str = 'train', source_dirs: list[str] | None = None, seed: int = 42, shared_vocab: dict | None = None, shared_vocab_digest: str | None = None, vocab_scope: Literal['train', 'all'] = 'train', scaler_cfg: ScalerCfg = _DEFAULT_SCALER_CFG, representation_cfg: GraphRepresentationCfg = _DEFAULT_REPRESENTATION_CFG, transform=None, pre_transform=None)
Bases: BaseGraphDataset
One graph is one sliding window of CAN messages.
Source code in graphids/core/data/datasets/_base.py
CANBusSource
dataclass
¶
CANBusSource(name: str, lake_root: str | None = None, val_fraction: float = 0.2, seed: int = 42, scaler_cfg: ScalerCfg = ZBenignScalerCfg(), representation_cfg: GraphRepresentationCfg = SnapshotRepresentationCfg(), vocab_scope: Literal['train', 'all'] = 'train')
Bases: BaseGraphSource
Catalog to train/val/test CANBusDataset builder.
DatasetState
dataclass
¶
Ready-to-serve train/val/test splits.
clear_cache ¶
get_or_build ¶
Return cached DatasetState for dataset.
Source code in graphids/core/data/state.py
datamodule ¶
DataModule primitives for graph and fusion datasets.
GraphDataModule ¶
GraphDataModule(dataset, batch_size: int = 32, num_workers: int | None = None, prefetch_factor: int = 2, dynamic_batching: bool = True, label_filter: str | None = None, difficulty: Callable[..., Tensor] | None = None, scope_label: int = 0, min_steps_per_epoch: int = 1, require_cache: bool = False)
Bases: LightningDataModule
Source code in graphids/core/data/datamodule/graph.py
train_eval_dataloader ¶
fusion ¶
Fusion data module for pre-extracted TensorDict caches.
graph ¶
Lightning data module for graph datasets.
GraphDataModule ¶
GraphDataModule(dataset, batch_size: int = 32, num_workers: int | None = None, prefetch_factor: int = 2, dynamic_batching: bool = True, label_filter: str | None = None, difficulty: Callable[..., Tensor] | None = None, scope_label: int = 0, min_steps_per_epoch: int = 1, require_cache: bool = False)
Bases: LightningDataModule
Source code in graphids/core/data/datamodule/graph.py
train_eval_dataloader ¶
sampler ¶
Offline next-fit decreasing packer for variable-size graphs.
pack_offline ¶
pack_offline(sizes: Tensor, max_num: int, *, edge_sizes: Tensor | None = None, max_edges: int | None = None) -> list[list[int]]
Pack graph indices under node and edge budgets.
The sorted next-fit strategy is intentionally linear after sorting. Exact first-fit gives slightly tighter bins, but it is quadratic on large cached graph datasets and can spend minutes on CPU before the first GPU step.
Source code in graphids/core/data/datamodule/sampler.py
datasets ¶
CANBusDataset ¶
CANBusDataset(root: str | Path, raw_dir: str | Path, *, val_fraction: float, split: str = 'train', source_dirs: list[str] | None = None, seed: int = 42, shared_vocab: dict | None = None, shared_vocab_digest: str | None = None, vocab_scope: Literal['train', 'all'] = 'train', scaler_cfg: ScalerCfg = _DEFAULT_SCALER_CFG, representation_cfg: GraphRepresentationCfg = _DEFAULT_REPRESENTATION_CFG, transform=None, pre_transform=None)
Bases: BaseGraphDataset
One graph is one sliding window of CAN messages.
Source code in graphids/core/data/datasets/_base.py
CANBusSource
dataclass
¶
CANBusSource(name: str, lake_root: str | None = None, val_fraction: float = 0.2, seed: int = 42, scaler_cfg: ScalerCfg = ZBenignScalerCfg(), representation_cfg: GraphRepresentationCfg = SnapshotRepresentationCfg(), vocab_scope: Literal['train', 'all'] = 'train')
Bases: BaseGraphSource
Catalog to train/val/test CANBusDataset builder.
can_bus ¶
CAN bus dataset adapter and schema.
CANBusDataset ¶
CANBusDataset(root: str | Path, raw_dir: str | Path, *, val_fraction: float, split: str = 'train', source_dirs: list[str] | None = None, seed: int = 42, shared_vocab: dict | None = None, shared_vocab_digest: str | None = None, vocab_scope: Literal['train', 'all'] = 'train', scaler_cfg: ScalerCfg = _DEFAULT_SCALER_CFG, representation_cfg: GraphRepresentationCfg = _DEFAULT_REPRESENTATION_CFG, transform=None, pre_transform=None)
Bases: BaseGraphDataset
One graph is one sliding window of CAN messages.
Source code in graphids/core/data/datasets/_base.py
CANBusSource
dataclass
¶
CANBusSource(name: str, lake_root: str | None = None, val_fraction: float = 0.2, seed: int = 42, scaler_cfg: ScalerCfg = ZBenignScalerCfg(), representation_cfg: GraphRepresentationCfg = SnapshotRepresentationCfg(), vocab_scope: Literal['train', 'all'] = 'train')
Bases: BaseGraphSource
Catalog to train/val/test CANBusDataset builder.
infer_attack_type ¶
Infer the attack code from filename/path substrings.
Source code in graphids/core/data/datasets/can_bus.py
load_can_rows ¶
Load, normalize, and parse raw CAN CSVs from source dirs.
Source code in graphids/core/data/datasets/can_bus.py
parse_payload ¶
Hex payload to byte_0..7 plus Shannon entropy.
Source code in graphids/core/data/datasets/can_bus.py
discovery ¶
Signal profile artifacts for CAN cache builds.
build_signal_profiles ¶
Aggregate raw CAN rows into one profile per vehicle/arbitration ID.
Source code in graphids/core/data/discovery/hypotheses.py
initialize_hypotheses ¶
Create empty editable mapping rows for profile review.
Source code in graphids/core/data/discovery/hypotheses.py
hypotheses ¶
Signal profile artifacts written beside graph caches.
build_signal_profiles ¶
Aggregate raw CAN rows into one profile per vehicle/arbitration ID.
Source code in graphids/core/data/discovery/hypotheses.py
initialize_hypotheses ¶
Create empty editable mapping rows for profile review.
Source code in graphids/core/data/discovery/hypotheses.py
extract ¶
Extract and cache fusion features as a TensorDict.
Each upstream model implements extract_features(batch, device) -> dict[str, Tensor]
returning per-graph named feature tensors. This module collects those dicts
under the model name (vgae, gat, ...), stacks across batches, and saves
the resulting nested TensorDict to disk. No flat state vector, no offsets —
the fusion side reads keys directly.
Invoked by the experiment extraction pipeline. Idempotent on output_dir.
extract_states ¶
extract_states(*, checkpoints: dict[str, str], dataset: str, output_dir: str, max_samples: int = 150000, max_val_samples: int = 30000, batch_size: int = 256, seed: int = 42, val_fraction: float = 0.2, representation_cfg: GraphRepresentationCfg) -> None
Load model checkpoints, extract and cache fusion features.
Idempotent per-file: each split's cache is checked independently so re-running after adding test splits only extracts the missing files.
Source code in graphids/core/data/extract.py
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 | |
preprocessing ¶
Core graph preprocessing.
graph_tables_to_pyg ¶
graph_tables_to_pyg(tables: GraphTables, *, node_col_order: list[str], edge_col_order: tuple[str, ...], label_exprs: list[Expr]) -> tuple[Data, dict, int, int]
Compose staged tables into pre-collated PyG tensors.
Source code in graphids/core/data/preprocessing/pyg.py
curriculum ¶
Curriculum difficulty scorers used by the graph datamodule.
score_random ¶
Uniform random per-graph difficulty for curriculum control runs.
score_vgae ¶
Per-graph reconstruction MSE from a trained VGAE checkpoint.
Higher = harder. Loads the VGAE on CPU, computes per-graph mean MSE
via torch_geometric.utils.scatter, releases the model.
Source code in graphids/core/data/preprocessing/curriculum.py
edge_policy ¶
Declarative edge construction policies for graph preprocessing.
EdgePolicy
dataclass
¶
EdgePolicy(name: str, src_col: str = 'node_id', dst_col: str = 'node_id', dst_shift: int = -1, src_alias: str = 'src', dst_alias: str = 'dst')
How to derive directed edges from windowed rows.
temporal_edge_policy ¶
temporal_edge_policy(*, src_col: str = 'node_id', dst_col: str = 'node_id', dst_shift: int = -1) -> EdgePolicy
Temporal adjacency policy: edge from row t to row t + dst_shift.
Source code in graphids/core/data/preprocessing/edge_policy.py
graph_ops ¶
Composable graph transforms over node/edge preprocessing tables.
GraphTransform
dataclass
¶
GraphTransform(name: str, requires: tuple[str, ...], produces: tuple[str, ...], fn: Callable[[DataFrame, DataFrame], tuple[DataFrame, DataFrame]])
A declarative graph transform with explicit input/output columns.
default_graph_transforms ¶
Default graph transforms used in cache builds.
Source code in graphids/core/data/preprocessing/graph_ops.py
secondary_graph_transforms ¶
Additional exploratory graph transforms used in feature tests.
Source code in graphids/core/data/preprocessing/graph_ops.py
materialization ¶
Raw CAN rows to graph tables.
metadata ¶
Cache metadata contract for dataset builds.
load_metadata ¶
Read and version-gate cache_metadata.json.
Source code in graphids/core/data/preprocessing/metadata.py
merge_split_into_metadata ¶
merge_split_into_metadata(cache_dir: Path, split_name: str, split_entry: dict[str, Any], *, invariants: dict[str, Any], dataset_name: str, num_arb_ids: int) -> dict[str, Any]
Merge one split's entry into cache_metadata.json under FileLock.
First writer seeds top-level fields; later writers must match invariants + dataset name or raise.
Source code in graphids/core/data/preprocessing/metadata.py
pyg ¶
PyG tensor packing primitives for staged graph tables.
graph_tables_to_pyg ¶
graph_tables_to_pyg(tables: GraphTables, *, node_col_order: list[str], edge_col_order: tuple[str, ...], label_exprs: list[Expr]) -> tuple[Data, dict, int, int]
Compose staged tables into pre-collated PyG tensors.
Source code in graphids/core/data/preprocessing/pyg.py
representations ¶
Graph representation configs used by preprocessing.
scaler ¶
Per-column feature scalers for tensor-based graph preprocessing.
splits ¶
Leakage-safe train/validation graph indices.
graph_touched_base_units ¶
Return base snapshot windows touched by each graph.
Source code in graphids/core/data/preprocessing/splits.py
vocab ¶
Vocabulary scan, digest, persist, and load primitives.
load_vocab ¶
Return (entries, digest) from a persisted vocab file.
persist_vocab ¶
Atomic write and return the digest.
Source code in graphids/core/data/preprocessing/vocab.py
scan_arb_ids ¶
Sorted unique arb_id across every CSV under source_dirs.
Source code in graphids/core/data/preprocessing/vocab.py
vocab_digest ¶
SHA256 over (id, index) pairs sorted by index.