Esempio n. 1
0
    def testGetProperties(self):
        properties = get_object_properties(Metric(name="foo"))
        self.assertEqual(properties, {"name": "foo"})

        properties = get_object_properties(
            Metric(name="foo", lower_is_better=True))
        self.assertEqual(properties, {"name": "foo", "lower_is_better": True})
Esempio n. 2
0
    def testGetProperties(self):
        # Extract default value.
        properties = get_object_properties(Metric(name="foo"))
        self.assertEqual(properties, {"name": "foo", "lower_is_better": None})

        # Extract passed value.
        properties = get_object_properties(Metric(name="foo", lower_is_better=True))
        self.assertEqual(properties, {"name": "foo", "lower_is_better": True})
Esempio n. 3
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 = get_object_properties(object=benchmark_problem)
        properties["__type"] = benchmark_problem.__class__.__name__
        return properties
    else:  # pragma: no cover
        raise ValueError(
            f"Expected benchmark problem, got: {benchmark_problem}.")
Esempio n. 4
0
    def runner_to_sqa(self, runner: Runner) -> SQARunner:
        """Convert Ax Runner to SQLAlchemy."""
        runner_type = RUNNER_REGISTRY.get(type(runner))
        if runner_type is None:
            raise SQAEncodeError(
                "Cannot encode runner to SQLAlchemy because runner's "
                "subclass is invalid.")  # pragma: no cover
        properties = get_object_properties(object=runner)

        # pyre-fixme: Expected `Base` for 1st...t `typing.Type[Runner]`.
        runner_class: SQARunner = self.config.class_to_sqa_class[Runner]
        # pyre-fixme[29]: `SQARunner` is not a function.
        return runner_class(runner_type=runner_type, properties=properties)
Esempio n. 5
0
    def get_metric_type_and_properties(
            self, metric: Metric) -> Tuple[int, Dict[str, Any]]:
        """Given an Ax Metric, convert its type into a member of MetricType enum,
        and construct a dictionary to be stored in the database `properties`
        json blob.
        """
        metric_type = METRIC_REGISTRY.get(type(metric))
        if metric_type is None:
            raise SQAEncodeError(
                "Cannot encode metric to SQLAlchemy because metric's "
                "subclass is invalid.")  # pragma: no cover

        # name and lower_is_better are stored directly on the metric,
        # so we don't need to include these in the properties blob
        properties = get_object_properties(
            object=metric, exclude_fields=["name", "lower_is_better"])

        return metric_type, properties
Esempio n. 6
0
def runner_to_dict(runner: SyntheticRunner) -> Dict[str, Any]:
    """Convert Ax synthetic runner to a dictionary."""
    properties = get_object_properties(object=runner)
    properties["__type"] = runner.__class__.__name__
    return properties
Esempio n. 7
0
def metric_to_dict(metric: Metric) -> Dict[str, Any]:
    """Convert Ax metric to a dictionary."""
    properties = get_object_properties(object=metric)
    properties["__type"] = metric.__class__.__name__
    return properties
Esempio n. 8
0
def observation_features_to_dict(obs_features: ObservationFeatures) -> Dict[str, Any]:
    """Converts Ax observation features to a dictionary"""
    properties = get_object_properties(object=obs_features)
    properties['_type'] = obs_features.__class__.__name__
    return properties