Data Source
SRToolkit.dataset.data_source
Data-source descriptors for symbolic regression datasets.
A DataSource describes the origin of a
dataset's data (where the X / y arrays come from). This is a separate
concern from the dataset's input-distribution spec (its samplers): the samplers
define what the inputs of the problem look like and power
resample, and remain available no
matter where the data originated.
Two concrete sources ship with the toolkit:
- UrlSource — download a
.zipfrom a URL and extract it into the cache. - SampleSource — generate the data by
drawing
n_samplespoints from the dataset'ssamplers.
A data_source of None means the data was supplied directly (e.g. as arrays) and
already lives in the cache; nothing needs to be materialised.
Sources serialize via a "source_class" key holding the fully-qualified class path,
mirroring other custom classes, e.g., Sampler. As a result
DataSource.from_dict can reconstruct any
subclass — including user-defined ones — via importlib without a central
registry: subclass DataSource, implement
to_dict / from_dict / materialize, and it round-trips. Custom sources only
travel to other machines via the bundle mechanism (their code must be importable on the
other end). Because reconstruction imports the class named in the config, only load
configs you trust.
DataSource
Bases: ABC
Abstract base class describing the origin of a dataset's cached data.
Concrete subclasses must implement
to_dict,
from_dict, and
materialize. The dict produced
by to_dict must include a "source_class" key holding the fully-qualified class
path (e.g. "SRToolkit.dataset.data_source.UrlSource") so that
DataSource.from_dict can reconstruct it
via importlib without a central registry.
The cache layer stores a hash of every source's data_source + samplers config
and warns when it drifts from what the cache was built with — regardless of this flag —
so a changed url, changed n_samples/seed, or a switch between source kinds is
always surfaced.
Attributes:
| Name | Type | Description |
|---|---|---|
is_volatile |
bool
|
Informational hint: |
to_dict
abstractmethod
Serialize this source to a JSON-compatible dictionary.
The returned dict must include "source_class" set to the fully-qualified
class path of this source.
from_dict
classmethod
Reconstruct a source from a dict produced by to_dict.
When called on the base DataSource
class, dispatches to the concrete subclass named by the "source_class" key
using importlib — both built-in and user-defined subclasses round-trip without
a central registry. When called on a concrete subclass, that subclass must override
this method.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
d
|
Optional[dict]
|
Dictionary with a |
required |
Returns:
| Type | Description |
|---|---|
Optional['DataSource']
|
A reconstructed DataSource instance, |
Optional['DataSource']
|
or |
Raises:
| Type | Description |
|---|---|
KeyError
|
If |
ImportError
|
If the class cannot be imported (dispatch path). |
NotImplementedError
|
If called on a subclass that has not overridden this method. |
Source code in SRToolkit/dataset/data_source.py
materialize
abstractmethod
Produce the dataset's data at cache_path (an .npz file with X and,
for RMSE datasets, y).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cache_path
|
Path
|
Target |
required |
config
|
dict
|
The full serialised dataset config (see
SR_dataset.to_dict).
Sources that generate data (e.g.
SampleSource) read the
dataset's |
required |
Source code in SRToolkit/dataset/data_source.py
UrlSource
Bases: DataSource
Data downloaded as a .zip archive from a URL and extracted into the cache.
Two archive layouts are accepted transparently: a flat zip whose
<dataset_name>.npz files sit at the root (as served by the built-in benchmarks),
or a to_archive archive whose data lives under a data/ prefix (in which case
the prefix is stripped and the bundled benchmark.json / dataset.json is
ignored). Either way, the expected <dataset_name>.npz must end up in the version directory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
URL of a |
required |
to_dict
from_dict
classmethod
Deserialize a UrlSource from a dictionary.
Source code in SRToolkit/dataset/data_source.py
materialize
Download the archive and extract its data files into the version directory.
Source code in SRToolkit/dataset/data_source.py
SampleSource
Bases: DataSource
Data generated by drawing n_samples points from the dataset's samplers.
This source does not own the samplers — it carries only the generation parameters
and reads the samplers from the dataset config at materialisation time. The dataset
must therefore define samplers (one per input variable). For RMSE datasets with a
token-list ground truth, the targets y are produced by evaluating that expression
on the generated inputs.
Because the output depends on the samplers and seed, this source is volatile: the cache layer records a hash and warns when the configuration drifts (call refresh to regenerate).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_samples
|
int
|
Number of input rows to generate. Defaults to |
10000
|
seed
|
Optional[int]
|
Random seed for reproducible generation. |
None
|
Source code in SRToolkit/dataset/data_source.py
to_dict
Serialize this source to a JSON-compatible dictionary.
from_dict
classmethod
Deserialize a SampleSource from a dictionary.
Source code in SRToolkit/dataset/data_source.py
materialize
Generate X (and y for RMSE) from the dataset's samplers and save them.
Source code in SRToolkit/dataset/data_source.py
FallbackSource
Bases: DataSource
A chain of data sources tried in order until one succeeds.
materialize attempts each
source's materialize in turn; if one raises, a warning is emitted and the next is
tried. The first to succeed wins; if all fail, a RuntimeError chaining the last
error is raised.
The built-in benchmarks use this to prefer canonical downloaded data while keeping a
network-free fallback —
FallbackSource([UrlSource(archive), SampleSource(seed=...)]) downloads the
authoritative archive when reachable and regenerates from the dataset's samplers
otherwise. Because the whole chain is serialised in the config, the preference
travels: a grid recipe, an export, or
a cold worker each download the canonical data first and only sample if it is
unavailable — unlike a bare SampleSource, which would always regenerate.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sources
|
List[DataSource]
|
Ordered, non-empty list of DataSource to try. |
required |
Source code in SRToolkit/dataset/data_source.py
to_dict
Serialize this source and its whole chain to a JSON-compatible dictionary.
Source code in SRToolkit/dataset/data_source.py
from_dict
classmethod
Deserialize a FallbackSource, reconstructing each child source.
Source code in SRToolkit/dataset/data_source.py
materialize
Try each source in order; warn and continue on failure, re-raising if all fail.