def testGetProperties(self): # Extract default value. properties = serialize_init_args(Metric(name="foo")) self.assertEqual(properties, {"name": "foo", "lower_is_better": None}) # Extract passed value. properties = serialize_init_args( Metric(name="foo", lower_is_better=True)) self.assertEqual(properties, {"name": "foo", "lower_is_better": True})
def serialize_init_args(cls, metric: "Metric") -> Dict[str, Any]: """Serialize the properties needed to initialize the metric. Used for storage. """ return serialize_init_args( object=metric, exclude_fields=["name", "lower_is_better", "precomp_config"])
def serialize_init_args(cls, data: Data) -> Dict[str, Any]: """Serialize the class-dependent properties needed to initialize this Data. Used for storage and to help construct new similar Data. All kwargs other than "dataframe" and "description" are considered structural. """ return serialize_init_args(object=data, exclude_fields=["df", "description"])
def benchmark_problem_to_dict( benchmark_problem: BenchmarkProblem) -> Dict[str, Any]: """Converts an Ax benchmark problem to a serializable dictionary.""" if isinstance(benchmark_problem, SimpleBenchmarkProblem): if benchmark_problem.uses_synthetic_function: function_name = benchmark_problem.f.name # pyre-ignore[16] f = None else: function_name = benchmark_problem.f.__name__ # pyre-ignore[16] f = pickle.dumps(benchmark_problem.f, 0).decode() return { "__type": benchmark_problem.__class__.__name__, "uses_synthetic_function": benchmark_problem.uses_synthetic_function, "function_name": function_name, # If the benchamrk problem uses a custom callable, pickle it. "f": f, "name": benchmark_problem.name, "domain": benchmark_problem.domain, "minimize": benchmark_problem.minimize, "noise_sd": benchmark_problem.noise_sd, "evaluate_suggested": benchmark_problem.evaluate_suggested, "optimal_value": benchmark_problem.optimal_value, } elif isinstance(benchmark_problem, BenchmarkProblem): properties = serialize_init_args(object=benchmark_problem) properties["__type"] = benchmark_problem.__class__.__name__ return properties else: # pragma: no cover raise ValueError( f"Expected benchmark problem, got: {benchmark_problem}.")
def clone(self) -> "Runner": """Create a copy of this Runner.""" cls = type(self) # pyre-ignore[45]: Cannot instantiate abstract class `Runner`. return cls( **serialize_init_args(self), )
def clone(self) -> "Metric": """Create a copy of this Metric.""" cls = type(self) return cls(**serialize_init_args(self), )
def runner_to_dict(runner: SyntheticRunner) -> Dict[str, Any]: """Convert Ax synthetic runner to a dictionary.""" properties = serialize_init_args(object=runner) properties["__type"] = runner.__class__.__name__ return properties
def metric_to_dict(metric: Metric) -> Dict[str, Any]: """Convert Ax metric to a dictionary.""" properties = serialize_init_args(object=metric) properties["__type"] = metric.__class__.__name__ return properties
def serialize_init_args(cls, runner: "Runner") -> Dict[str, Any]: """Serialize the properties needed to initialize the runner. Used for storage. """ return serialize_init_args(object=runner)
def serialize_init_args(cls, metric: Metric) -> Dict[str, Any]: """Serialize the properties needed to initialize the metric. Used for storage. """ return serialize_init_args(object=metric)