Example #1
0
    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})
Example #2
0
 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"])
Example #3
0
 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"])
Example #4
0
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}.")
Example #5
0
 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),
     )
Example #6
0
 def clone(self) -> "Metric":
     """Create a copy of this Metric."""
     cls = type(self)
     return cls(**serialize_init_args(self), )
Example #7
0
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
Example #8
0
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
Example #9
0
 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)
Example #10
0
 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)