Skip to main content

parse

Function parse 

pub fn parse(spec: &str) -> Result<Distribution>
Expand description

Parses a DSL spec into a Distribution.

See the module documentation for the full grammar. The parse is total: it never panics on any input and always returns either a validated distribution or a descriptive error.

use atune_core::space::{dsl, Distribution};

assert_eq!(dsl::parse("loguniform(1e-5, 1e-2)")?, Distribution::float_log(1e-5, 1e-2)?);
assert_eq!(dsl::parse("int(2, 512, log)")?, Distribution::int_log(2, 512)?);
assert_eq!(dsl::parse("int(0, 10, step=2)")?, Distribution::int_step(0, 10, 2)?);
assert_eq!(dsl::parse("choice(adam, sgd)")?, Distribution::cat_labels(["adam", "sgd"])?);
assert_eq!(dsl::parse("bool")?, Distribution::boolean());
assert_eq!(dsl::parse("42")?, Distribution::int(42, 42)?);            // fixed int
assert_eq!(dsl::parse("0.001")?, Distribution::float(0.001, 0.001)?); // fixed float
assert!(dsl::parse("loguniform(-1, 1)").is_err());

§Errors

Error::InvalidSpace with a message naming what was expected and where, for empty input, an unknown function, the wrong argument count, a malformed or non-finite numeric literal, unbalanced parentheses, or any distribution the underlying constructors reject (low > high, a non-positive loguniform bound, a duplicate categorical label, …).