SR Benchmark
SRToolkit.dataset.sr_benchmark
Benchmark collection for symbolic regression datasets.
SR_benchmark
SR_benchmark(benchmark_name: str, datasets: Optional[List[Union[SR_dataset, Tuple[str, SR_dataset]]]] = None, metadata: Optional[Dict[str, Any]] = None, version: str = '1.0.0', base_dir: Optional[str] = None)
A named, persistent collection of symbolic regression datasets.
Examples:
>>> from SRToolkit.dataset import Feynman
>>> benchmark = Feynman() # Feynman is a specific instance of SR_benchmark with additional functionality
>>> len(benchmark.list_datasets(verbose=False))
100
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
benchmark_name
|
str
|
Name of this benchmark. |
required |
datasets
|
Optional[List[Union[SR_dataset, Tuple[str, SR_dataset]]]]
|
Initial datasets to add. Each element can be an
SR_dataset instance (auto-named as
|
None
|
metadata
|
Optional[Dict[str, Any]]
|
Optional dictionary of benchmark-level metadata (e.g. citation, description). |
None
|
version
|
str
|
Version string for this benchmark. Defaults to |
'1.0.0'
|
base_dir
|
Optional[str]
|
Directory where dataset files are stored or will be written. Optional — if omitted, the data cache is used exclusively. |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If any element of |
Source code in SRToolkit/dataset/sr_benchmark.py
add_dataset_instance
Adds an instance of the SR_dataset class to the benchmark.
Examples:
>>> from SRToolkit.dataset import Feynman
>>> benchmark = Feynman() # Feynman is a specific instance of SR_benchmark with additional functionality
>>> dataset = benchmark.create_dataset('I.16.6')
>>> isinstance(dataset, SR_dataset)
True
>>> bm = SR_benchmark("BM")
>>> bm.add_dataset_instance("I.16.6", dataset)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dataset_name
|
str
|
The name of the dataset. |
required |
dataset
|
SR_dataset
|
An instance of the SR_dataset class. |
required |
Raises:
| Type | Description |
|---|---|
Exception
|
If the dataset name already exists in the benchmark. |
Source code in SRToolkit/dataset/sr_benchmark.py
add_dataset
add_dataset(symbol_library: SymbolLibrary, dataset: Optional[Union[ndarray, Tuple[ndarray, ndarray]]] = None, dataset_name: Optional[str] = None, ranking_function: str = 'rmse', max_evaluations: int = -1, ground_truth: Optional[Union[List[str], Node, ndarray]] = None, original_equation: Optional[str] = None, success_threshold: Optional[float] = None, seed: Optional[int] = None, dataset_metadata: Optional[dict] = None, samplers: Optional[List[Sampler]] = None, data_source: Optional[DataSource] = None, **kwargs: Unpack[EstimationSettings])
Adds a dataset to the benchmark.
Examples:
>>> import tempfile, numpy as np
>>> from SRToolkit.utils import SymbolLibrary
>>> bm = SR_benchmark("BM")
>>> X = np.random.rand(10, 2)
>>> y = X[:, 0] + X[:, 1]
>>> bm.add_dataset(SymbolLibrary.default_symbols(2),(X, y),dataset_name="test_ds",ground_truth=["X_0", "+", "X_1"],original_equation="y = x0 + x1")
>>> len(bm.list_datasets(verbose=False))
1
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dataset
|
Optional[Union[ndarray, Tuple[ndarray, ndarray]]]
|
Direct data for the dataset: a 2-D numpy array (features) or a
|
None
|
symbol_library
|
SymbolLibrary
|
The symbol library to use. |
required |
dataset_name
|
Optional[str]
|
The name of the dataset. Auto-generated if |
None
|
ranking_function
|
str
|
|
'rmse'
|
max_evaluations
|
int
|
Maximum expressions to evaluate. |
-1
|
ground_truth
|
Optional[Union[List[str], Node, ndarray]]
|
Ground truth expression. |
None
|
original_equation
|
Optional[str]
|
Human-readable equation string. |
None
|
success_threshold
|
Optional[float]
|
Error threshold for success. |
None
|
seed
|
Optional[int]
|
Random seed. |
None
|
dataset_metadata
|
Optional[dict]
|
Optional dataset-level metadata dict. |
None
|
samplers
|
Optional[List[Sampler]]
|
Optional list of samplers (one per input variable). They define the problem's input distribution and power resample; a SampleSource draws from them. |
None
|
data_source
|
Optional[DataSource]
|
Optional DataSource
describing where the data comes from
(UrlSource or
SampleSource). When provided,
the |
None
|
**kwargs
|
Unpack[EstimationSettings]
|
Estimation settings forwarded to SR_evaluator. |
{}
|
Raises:
| Type | Description |
|---|---|
ValueError
|
Various validation errors (see below). |
Source code in SRToolkit/dataset/sr_benchmark.py
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 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 | |
add_from_samplers
add_from_samplers(ground_truth: Union[List[str], Node], samplers: List[Sampler], symbol_library: Optional[SymbolLibrary] = None, n_samples: int = 10000, seed: Optional[int] = None, ranking_function: str = 'rmse', dataset_name: Optional[str] = None, original_equation: Optional[str] = None, success_threshold: Optional[float] = None, max_evaluations: int = -1, dataset_metadata: Optional[dict] = None, **kwargs: Unpack[EstimationSettings]) -> None
Add a dataset described only by a ground-truth expression and per-variable samplers.
This is the benchmark-level counterpart to
SR_dataset.from_samplers:
it attaches a SampleSource so the data
is generated lazily from samplers (and, for RMSE, ground_truth) the first
time the dataset is materialised via
create_dataset.
Examples:
>>> from SRToolkit.dataset.sampling import UniformSampling
>>> bm = SR_benchmark("BM")
>>> bm.add_from_samplers(["X_0", "+", "X_1"],
... [UniformSampling(0, 5), UniformSampling(0, 5)], dataset_name="add",
... n_samples=100, seed=0)
>>> ds = bm.create_dataset("add")
>>> ds.X.shape
(100, 2)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ground_truth
|
Union[List[str], Node]
|
Ground-truth expression as a token list or Node. |
required |
samplers
|
List[Sampler]
|
One Sampler per input variable. |
required |
symbol_library
|
Optional[SymbolLibrary]
|
Token vocabulary. Defaults to default_symbols with one variable per sampler. |
None
|
n_samples
|
int
|
Number of input rows to generate on materialisation. Defaults to |
10000
|
seed
|
Optional[int]
|
Random seed stored on the SampleSource. |
None
|
ranking_function
|
str
|
|
'rmse'
|
dataset_name
|
Optional[str]
|
Name of the dataset. Auto-generated if |
None
|
original_equation
|
Optional[str]
|
Human-readable equation string. Auto-filled from a token-list
|
None
|
success_threshold
|
Optional[float]
|
Error threshold for success. |
None
|
max_evaluations
|
int
|
Maximum expressions to evaluate. |
-1
|
dataset_metadata
|
Optional[dict]
|
Optional dataset-level metadata dict. |
None
|
**kwargs
|
Unpack[EstimationSettings]
|
Estimation settings forwarded to SR_evaluator. |
{}
|
Source code in SRToolkit/dataset/sr_benchmark.py
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 | |
create_dataset
create_dataset(dataset_name: str, n_samples: Optional[int] = None, seed: Optional[int] = None) -> SR_dataset
Creates an instance of a dataset from the given dataset name.
When n_samples is provided the returned dataset contains freshly sampled data
instead of the pre-generated data on disk. The dataset must have samplers defined
(see samplers argument of
add_dataset).
Examples:
>>> from SRToolkit.dataset import Feynman
>>> benchmark = Feynman() # Feynman is a specific instance of SR_benchmark with additional functionality
>>> dataset = benchmark.create_dataset('I.16.6')
>>> dataset.X.shape
(10000, 3)
>>> dataset_small = benchmark.create_dataset('I.16.6', n_samples=500, seed=0)
>>> dataset_small.X.shape
(500, 3)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dataset_name
|
str
|
The name of the dataset to create. |
required |
n_samples
|
Optional[int]
|
If provided, generate a fresh dataset with this many samples using the stored samplers instead of loading pre-generated data from disk. |
None
|
seed
|
Optional[int]
|
Random seed used when |
None
|
Returns:
| Type | Description |
|---|---|
SR_dataset
|
An SR_dataset instance containing the |
SR_dataset
|
data, ground truth expression, and metadata for the given dataset. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the dataset name is not found, or if |
Source code in SRToolkit/dataset/sr_benchmark.py
list_datasets
Lists the available datasets.
Examples:
>>> from SRToolkit.dataset import Feynman
>>> benchmark = Feynman() # Feynman is a specific instance of SR_benchmark with additional functionality
>>> len(benchmark.list_datasets(num_variables=2, verbose=False))
15
>>> datasets_with_8_vars = benchmark.list_datasets(num_variables=8, verbose=False)
>>> datasets_with_8_vars[0]
'II.36.38'
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
verbose
|
bool
|
If |
True
|
num_variables
|
int
|
If not |
-1
|
Returns:
| Type | Description |
|---|---|
List[str]
|
A list of dataset names. |
Source code in SRToolkit/dataset/sr_benchmark.py
to_dict
Serialise the benchmark to a pure JSON-safe dictionary.
Dataset entries that have an sr_dataset key (added via
add_dataset_instance)
are serialised via SR_dataset.to_dict().
Returns:
| Type | Description |
|---|---|
dict
|
A JSON-safe dict representing the full benchmark configuration. |
Source code in SRToolkit/dataset/sr_benchmark.py
from_dict
classmethod
Reconstruct an SR_benchmark from a config dict or a saved JSON file.
To load a self-contained .zip archive (written by
to_archive) use
from_archive instead.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
d
|
Union[dict, str, Path]
|
A dict produced by to_dict, or a path to a JSON file. |
required |
Returns:
| Type | Description |
|---|---|
SR_benchmark
|
An SR_benchmark instance. |
Source code in SRToolkit/dataset/sr_benchmark.py
to_archive
Write the benchmark (config + data) to a .zip archive.
The archive contains:
benchmark.json: the benchmark configuration dict.data/<dataset_name>.npz: the cached data for each dataset.data/<dataset_name>_gt.npy: ground-truth behaviour array (if present).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Union[str, Path]
|
Destination path for the archive. Non- |
required |
Source code in SRToolkit/dataset/sr_benchmark.py
from_archive
classmethod
Load a benchmark from a self-contained .zip archive.
This is the counterpart to
to_archive: it reads
benchmark.json from the archive, extracts the bundled data/*.npz (and any
_gt.npy) into the data cache, and returns a benchmark whose datasets read
from that populated cache.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Union[str, Path]
|
Path to a |
required |
Returns:
| Type | Description |
|---|---|
SR_benchmark
|
An SR_benchmark instance. |
Source code in SRToolkit/dataset/sr_benchmark.py
from_url
classmethod
Download a self-contained .zip archive from a URL and load it.
This is the remote counterpart to
from_archive: the
archive is downloaded to a temporary file and then loaded exactly as
from_archive would. The url must point at an archive written by
to_archive (a
benchmark.json plus a data/ directory) — not a bare .npz/data zip
(that is what UrlSource is for).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
URL of a |
required |
Returns:
| Type | Description |
|---|---|
SR_benchmark
|
An SR_benchmark instance. |