Ejemplo n.º 1
0
 def test_encode_decode_numpy(self):
     arr = np.array([[1, 2, 3], [4, 5, 6]])
     self.assertTrue(np.array_equal(arr, object_from_json(object_to_json(arr))))
Ejemplo n.º 2
0
    def generator_run_to_sqa(
        self, generator_run: GeneratorRun, weight: Optional[float] = None
    ) -> SQAGeneratorRun:
        """Convert Ax GeneratorRun to SQLAlchemy and compile a list of (object,
        sqa_counterpart) tuples to set `db_id` on user-facing classes after
        the conversion is complete and the SQL session is flushed (SQLAlchemy
        classes receive their `id` attributes during `session.flush()`).

        In addition to creating and storing a new GeneratorRun object, we need to
        create and store copies of the Arms, Metrics, Parameters, and
        ParameterConstraints owned by this GeneratorRun.
        """
        arms = []
        for arm, arm_weight in generator_run.arm_weights.items():
            arms.append(self.arm_to_sqa(arm=arm, weight=arm_weight))

        metrics = self.optimization_config_to_sqa(generator_run.optimization_config)
        parameters, parameter_constraints = self.search_space_to_sqa(
            generator_run.search_space
        )

        best_arm_name = None
        best_arm_parameters = None
        best_arm_predictions = None
        if generator_run.best_arm_predictions is not None:
            # pyre-fixme[16]: `Optional` has no attribute `__getitem__`.
            best_arm = generator_run.best_arm_predictions[0]
            best_arm_predictions = list(generator_run.best_arm_predictions[1])
            best_arm_name = best_arm._name
            best_arm_parameters = best_arm.parameters
        model_predictions = (
            # pyre-fixme[6]: Expected `Iterable[Variable[_T]]` for 1st param but got
            #  `Optional[typing.Tuple[Dict[str, List[float]], Dict[str, Dict[str,
            #  List[float]]]]]`.
            list(generator_run.model_predictions)
            if generator_run.model_predictions is not None
            else None
        )

        generator_run_type = self.get_enum_value(
            value=generator_run.generator_run_type,
            enum=self.config.generator_run_type_enum,
        )

        # pyre-fixme: Expected `Base` for 1st...ing.Type[GeneratorRun]`.
        generator_run_class: SQAGeneratorRun = self.config.class_to_sqa_class[
            GeneratorRun
        ]
        gr_sqa = generator_run_class(  # pyre-ignore[29]: `SQAGeneratorRun` not a func.
            id=generator_run.db_id,
            arms=arms,
            metrics=metrics,
            parameters=parameters,
            parameter_constraints=parameter_constraints,
            time_created=generator_run.time_created,
            generator_run_type=generator_run_type,
            weight=weight,
            index=generator_run.index,
            fit_time=generator_run.fit_time,
            gen_time=generator_run.gen_time,
            best_arm_name=best_arm_name,
            best_arm_parameters=best_arm_parameters,
            best_arm_predictions=best_arm_predictions,
            model_predictions=model_predictions,
            model_key=generator_run._model_key,
            model_kwargs=object_to_json(generator_run._model_kwargs),
            bridge_kwargs=object_to_json(generator_run._bridge_kwargs),
            gen_metadata=object_to_json(generator_run._gen_metadata),
            model_state_after_gen=object_to_json(generator_run._model_state_after_gen),
            generation_step_index=generator_run._generation_step_index,
            candidate_metadata_by_arm_signature=object_to_json(
                generator_run._candidate_metadata_by_arm_signature
            ),
        )
        return gr_sqa
Ejemplo n.º 3
0
    def generator_run_to_sqa(
            self,
            generator_run: GeneratorRun,
            weight: Optional[float] = None) -> SQAGeneratorRun:
        """Convert Ax GeneratorRun to SQLAlchemy.

        In addition to creating and storing a new GeneratorRun object, we need to
        create and store copies of the Arms, Metrics, Parameters, and
        ParameterConstraints owned by this GeneratorRun.
        """
        arms = [
            self.arm_to_sqa(arm=arm, weight=weight)
            for (arm, weight) in generator_run.arm_weights.items()
        ]

        metrics = self.optimization_config_to_sqa(
            generator_run.optimization_config)
        parameters, parameter_constraints = self.search_space_to_sqa(
            generator_run.search_space)

        best_arm_name = None
        best_arm_parameters = None
        best_arm_predictions = None
        if generator_run.best_arm_predictions is not None:
            # pyre-fixme[16]: `Optional` has no attribute `__getitem__`.
            best_arm = generator_run.best_arm_predictions[0]
            best_arm_predictions = list(generator_run.best_arm_predictions[1])
            best_arm_name = best_arm._name
            best_arm_parameters = best_arm.parameters
        model_predictions = (
            # pyre-fixme[6]: Expected `Iterable[Variable[_T]]` for 1st param but got
            #  `Optional[typing.Tuple[Dict[str, List[float]], Dict[str, Dict[str,
            #  List[float]]]]]`.
            list(generator_run.model_predictions)
            if generator_run.model_predictions is not None else None)

        generator_run_type = self.get_enum_value(
            value=generator_run.generator_run_type,
            enum=self.config.generator_run_type_enum,
        )

        # pyre-fixme: Expected `Base` for 1st...ing.Type[GeneratorRun]`.
        generator_run_class: SQAGeneratorRun = self.config.class_to_sqa_class[
            GeneratorRun]
        # pyre-fixme[29]: `SQAGeneratorRun` is not a function.
        return generator_run_class(
            arms=arms,
            metrics=metrics,
            parameters=parameters,
            parameter_constraints=parameter_constraints,
            time_created=generator_run.time_created,
            generator_run_type=generator_run_type,
            weight=weight,
            index=generator_run.index,
            fit_time=generator_run.fit_time,
            gen_time=generator_run.gen_time,
            best_arm_name=best_arm_name,
            best_arm_parameters=best_arm_parameters,
            best_arm_predictions=best_arm_predictions,
            model_predictions=model_predictions,
            model_key=generator_run._model_key,
            model_kwargs=object_to_json(generator_run._model_kwargs),
            bridge_kwargs=object_to_json(generator_run._bridge_kwargs),
            gen_metadata=object_to_json(generator_run._gen_metadata),
            model_state_after_gen=object_to_json(
                generator_run._model_state_after_gen),
            generation_step_index=generator_run._generation_step_index,
            candidate_metadata_by_arm_signature=object_to_json(
                generator_run._candidate_metadata_by_arm_signature),
        )
Ejemplo n.º 4
0
    def generator_run_to_sqa(
        self,
        generator_run: GeneratorRun,
        weight: Optional[float] = None,
        reduced_state: bool = False,
    ) -> SQAGeneratorRun:
        """Convert Ax GeneratorRun to SQLAlchemy.

        In addition to creating and storing a new GeneratorRun object, we need to
        create and store copies of the Arms, Metrics, Parameters, and
        ParameterConstraints owned by this GeneratorRun.
        """

        arms = []
        for arm, arm_weight in generator_run.arm_weights.items():
            arms.append(self.arm_to_sqa(arm=arm, weight=arm_weight))

        metrics = self.optimization_config_to_sqa(
            generator_run.optimization_config)
        parameters, parameter_constraints = self.search_space_to_sqa(
            generator_run.search_space)

        best_arm_name = None
        best_arm_parameters = None
        best_arm_predictions = None
        if generator_run.best_arm_predictions is not None:
            best_arm = generator_run.best_arm_predictions[0]
            # pyre-fixme[16]: `Optional` has no attribute `__getitem__`.
            best_arm_predictions = list(generator_run.best_arm_predictions[1])
            best_arm_name = best_arm._name
            best_arm_parameters = best_arm.parameters
        model_predictions = (list(generator_run.model_predictions)
                             if generator_run.model_predictions is not None
                             else None)

        generator_run_type = self.get_enum_value(
            value=generator_run.generator_run_type,
            enum=self.config.generator_run_type_enum,
        )

        # pyre-fixme: Expected `Base` for 1st...ing.Type[GeneratorRun]`.
        generator_run_class: SQAGeneratorRun = self.config.class_to_sqa_class[
            GeneratorRun]
        gr_sqa = generator_run_class(  # pyre-ignore[29]: `SQAGeneratorRun` not a func.
            id=generator_run.db_id,
            arms=arms,
            metrics=metrics,
            parameters=parameters,
            parameter_constraints=parameter_constraints,
            time_created=generator_run.time_created,
            generator_run_type=generator_run_type,
            weight=weight,
            index=generator_run.index,
            fit_time=generator_run.fit_time,
            gen_time=generator_run.gen_time,
            best_arm_name=best_arm_name,
            best_arm_parameters=best_arm_parameters,
            best_arm_predictions=best_arm_predictions,
            model_predictions=model_predictions,
            model_key=generator_run._model_key,
            model_kwargs=object_to_json(generator_run._model_kwargs)
            if not reduced_state else None,
            bridge_kwargs=object_to_json(generator_run._bridge_kwargs)
            if not reduced_state else None,
            gen_metadata=object_to_json(generator_run._gen_metadata)
            if not reduced_state else None,
            model_state_after_gen=object_to_json(
                generator_run._model_state_after_gen)
            if not reduced_state else None,
            generation_step_index=generator_run._generation_step_index,
            candidate_metadata_by_arm_signature=object_to_json(
                generator_run._candidate_metadata_by_arm_signature),
        )
        return gr_sqa