Пример #1
0
    def predict(self,
                parameter_values_pandas_frame,
                t=None,
                context_values_pandas_frame=None):  # pylint: disable=unused-argument
        # TODO: make this streaming and/or using arrow.
        #
        if context_values_pandas_frame is not None:
            raise NotImplementedError(
                "Context not currently supported on remote optimizers")
        feature_values_dict = parameter_values_pandas_frame.to_dict(
            orient='list')
        prediction_request = OptimizerService_pb2.PredictRequest(
            OptimizerHandle=self.optimizer_handle,
            Features=OptimizerService_pb2.Features(
                FeaturesJsonString=json.dumps(feature_values_dict)))
        prediction_response = self._optimizer_stub.Predict(prediction_request)

        # To be compliant with the OptimizerBase, we need to recover a single Prediction object and return it.
        #
        objective_predictions_pb2 = prediction_response.ObjectivePredictions
        assert len(objective_predictions_pb2) == 1
        only_prediction_pb2 = objective_predictions_pb2[0]
        objective_name = only_prediction_pb2.ObjectiveName
        valid_predictions_df = Prediction.dataframe_from_json(
            only_prediction_pb2.PredictionDataFrameJsonString)
        prediction = Prediction.create_prediction_from_dataframe(
            objective_name=objective_name, dataframe=valid_predictions_df)
        return prediction
Пример #2
0
 def register(self, feature_values_pandas_frame, target_values_pandas_frame):
     register_request = OptimizerService_pb2.RegisterObservationsRequest(
         OptimizerHandle=self.optimizer_handle,
         Observations=OptimizerService_pb2.Observations(
             Features=OptimizerService_pb2.Features(FeaturesJsonString=feature_values_pandas_frame.to_json(orient='index', double_precision=15)),
             ObjectiveValues=OptimizerService_pb2.ObjectiveValues(
                 ObjectiveValuesJsonString=target_values_pandas_frame.to_json(orient='index', double_precision=15)
             )
         )
     )
     self._optimizer_stub.RegisterObservations(register_request)
Пример #3
0
    def register(self, parameter_values_pandas_frame, target_values_pandas_frame, context_values_pandas_frame=None):
        if context_values_pandas_frame is not None:
            raise NotImplementedError("Context not currently supported on remote optimizers")

        feature_values_pandas_frame = parameter_values_pandas_frame
        register_request = OptimizerService_pb2.RegisterObservationsRequest(
            OptimizerHandle=self.optimizer_handle_for_optimizer_service,
            Observations=OptimizerService_pb2.Observations(
                Features=OptimizerService_pb2.Features(FeaturesJsonString=feature_values_pandas_frame.to_json(orient='index', double_precision=15)),
                ObjectiveValues=OptimizerService_pb2.ObjectiveValues(
                    ObjectiveValuesJsonString=target_values_pandas_frame.to_json(orient='index', double_precision=15)
                )
            )
        )
        self._optimizer_stub.RegisterObservations(register_request) # TODO: we should be using the optimizer_stub for this.
Пример #4
0
    def register(self, feature_values_pandas_frame,
                 target_values_pandas_frame):  # pylint: disable=unused-argument
        # TODO: implement RegisterObservations <- plural
        #
        features_dicts_per_record = feature_values_pandas_frame.to_dict(
            orient='records')
        objectives_dicts_per_record = target_values_pandas_frame.to_dict(
            orient='records')

        # TODO: Either implement streaming or arrow flight or batch.
        #
        for feature_dict, objective_dict in zip(features_dicts_per_record,
                                                objectives_dicts_per_record):
            register_request = OptimizerService_pb2.RegisterObservationRequest(
                OptimizerHandle=self.optimizer_handle,
                Observation=OptimizerService_pb2.Observation(
                    Features=OptimizerService_pb2.Features(
                        FeaturesJsonString=json.dumps(feature_dict)),
                    ObjectiveValues=OptimizerService_pb2.ObjectiveValues(
                        ObjectiveValuesJsonString=json.dumps(objective_dict))))
            self._optimizer_stub.RegisterObservation(register_request)
Пример #5
0
    def predict(self, feature_values_pandas_frame, t=None):  # pylint: disable=unused-argument
        # TODO: make this streaming and/or using arrow.
        #
        feature_values_dict = feature_values_pandas_frame.to_dict(
            orient='list')
        prediction_request = OptimizerService_pb2.PredictRequest(
            OptimizerHandle=self.optimizer_handle,
            Features=OptimizerService_pb2.Features(
                FeaturesJsonString=json.dumps(feature_values_dict)))
        prediction_response = self._optimizer_stub.Predict(prediction_request)

        # To be compliant with the OptimizerInterface, we need to recover a single Prediction object and return it.
        #
        objective_predictions_pb2 = prediction_response.ObjectivePredictions
        assert len(objective_predictions_pb2) == 1
        only_prediction_pb2 = objective_predictions_pb2[0]
        objective_name = only_prediction_pb2.ObjectiveName
        valid_predictions_df = Prediction.dataframe_from_json(
            only_prediction_pb2.PredictionDataFrameJsonString)
        prediction = Prediction.create_prediction_from_dataframe(
            objective_name=objective_name, dataframe=valid_predictions_df)
        prediction.add_invalid_rows_at_missing_indices(
            desired_index=feature_values_pandas_frame.index)
        return prediction