Exemplo n.º 1
0
 def toProto(self) -> waypoint_pb2.Waypoint:
     return waypoint_pb2.Waypoint(
         id=wrappers_pb2.UInt32Value(value=self.id, ) if self.id else None,
         lat=self.lat,
         lng=self.lng,
         angle=self.angle,
         delay=self.delay.toProto() if self.delay else None,
         radius=wrappers_pb2.DoubleValue(value=self.radius, )
         if self.radius else None,
     )
Exemplo n.º 2
0
 def wrap_value(value):
     if isinstance(value, bool):
         message = wrapper.BoolValue()
         message.value = value
     elif isinstance(value, int):
         message = wrapper.Int64Value()
         message.value = value
     elif isinstance(value, float):
         message = wrapper.DoubleValue()
         message.value = value
     else:
         raise Exception("Unsupported type {}".format(type(value)))
     return message
Exemplo n.º 3
0
def move_arm(robot_state, is_admittance, world_T_goals, arm_surface_contact_client, velocity,
             allow_walking, world_T_admittance, press_force_percentage, api_send_frame,
             use_xy_to_z_cross_term, bias_force_x):

    traj = move_along_trajectory(api_send_frame, velocity, world_T_goals)
    press_force = geometry_pb2.Vec3(x=0, y=0, z=press_force_percentage)

    max_vel = wrappers_pb2.DoubleValue(value=velocity)

    cmd = arm_surface_contact_pb2.ArmSurfaceContact.Request(
        pose_trajectory_in_task=traj, root_frame_name=api_send_frame,
        root_tform_task=world_T_admittance, press_force_percentage=press_force,
        x_axis=arm_surface_contact_pb2.ArmSurfaceContact.Request.AXIS_MODE_POSITION,
        y_axis=arm_surface_contact_pb2.ArmSurfaceContact.Request.AXIS_MODE_POSITION,
        z_axis=arm_surface_contact_pb2.ArmSurfaceContact.Request.AXIS_MODE_POSITION,
        max_linear_velocity=max_vel)

    if is_admittance:
        # Add admittance options
        cmd.z_axis = arm_surface_contact_pb2.ArmSurfaceContact.Request.AXIS_MODE_FORCE
        cmd.press_force_percentage.z = press_force_percentage

        #if admittance_frame is not None:

        # Set the robot to be really stiff in x/y and really sensitive to admittance in z.
        cmd.xy_admittance = arm_surface_contact_pb2.ArmSurfaceContact.Request.ADMITTANCE_SETTING_OFF
        cmd.z_admittance = arm_surface_contact_pb2.ArmSurfaceContact.Request.ADMITTANCE_SETTING_LOOSE

        if use_xy_to_z_cross_term:
            cmd.xy_to_z_cross_term_admittance = arm_surface_contact_pb2.ArmSurfaceContact.Request.ADMITTANCE_SETTING_VERY_STIFF
        else:
            cmd.xy_to_z_cross_term_admittance = arm_surface_contact_pb2.ArmSurfaceContact.Request.ADMITTANCE_SETTING_OFF

        # Set a bias force
        cmd.bias_force_ewrt_body.CopyFrom(geometry_pb2.Vec3(x=bias_force_x, y=0, z=0))
    else:
        cmd.bias_force_ewrt_body.CopyFrom(geometry_pb2.Vec3(x=0, y=0, z=0))

    gripper_cmd_packed = RobotCommandBuilder.claw_gripper_open_fraction_command(0)
    cmd.gripper_command.CopyFrom(
        gripper_cmd_packed.synchronized_command.gripper_command.claw_gripper_command)

    cmd.is_robot_following_hand = allow_walking

    # Build the request proto
    proto = arm_surface_contact_service_pb2.ArmSurfaceContactCommand(request=cmd)

    # Send the request
    arm_surface_contact_client.arm_surface_contact_command(proto)
Exemplo n.º 4
0
def _unpack_values(values):
  """Unpack protobuf values."""
  unpacked = {}
  for key, packed_value in six.iteritems(values):
    if packed_value.Is(wrappers_pb2.DoubleValue.DESCRIPTOR):
      value = wrappers_pb2.DoubleValue()
    elif packed_value.Is(wrappers_pb2.Int64Value.DESCRIPTOR):
      value = wrappers_pb2.Int64Value()
    elif packed_value.Is(wrappers_pb2.StringValue.DESCRIPTOR):
      value = wrappers_pb2.StringValue()
    else:
      raise ValueError('Unknown stat type for ' + key)

    packed_value.Unpack(value)
    unpacked[key] = value.value

  return unpacked
def test_get_campaign_criterion_simulation(
    transport: str = 'grpc',
    request_type=campaign_criterion_simulation_service.
    GetCampaignCriterionSimulationRequest):
    client = CampaignCriterionSimulationServiceClient(
        credentials=ga_credentials.AnonymousCredentials(),
        transport=transport,
    )

    # Everything is optional in proto3 as far as the runtime is concerned,
    # and we are mocking out the actual API, so just send an empty request.
    request = request_type()

    # Mock the actual call within the gRPC stub, and fake the request.
    with mock.patch.object(
            type(client.transport.get_campaign_criterion_simulation),
            '__call__') as call:
        # Designate an appropriate return value for the call.
        call.return_value = campaign_criterion_simulation.CampaignCriterionSimulation(
            resource_name='resource_name_value',
            type_=simulation_type.SimulationTypeEnum.SimulationType.UNKNOWN,
            modification_method=simulation_modification_method.
            SimulationModificationMethodEnum.SimulationModificationMethod.
            UNKNOWN,
            bid_modifier_point_list=simulation.BidModifierSimulationPointList(
                points=[
                    simulation.BidModifierSimulationPoint(
                        bid_modifier=wrappers_pb2.DoubleValue(value=0.541))
                ]),
        )
        response = client.get_campaign_criterion_simulation(request)

        # Establish that the underlying gRPC stub method was called.
        assert len(call.mock_calls) == 1
        _, args, _ = call.mock_calls[0]
        assert args[
            0] == campaign_criterion_simulation_service.GetCampaignCriterionSimulationRequest(
            )

    # Establish that the response is the type that we expect.
    assert isinstance(
        response, campaign_criterion_simulation.CampaignCriterionSimulation)
    assert response.resource_name == 'resource_name_value'
    assert response.type_ == simulation_type.SimulationTypeEnum.SimulationType.UNKNOWN
    assert response.modification_method == simulation_modification_method.SimulationModificationMethodEnum.SimulationModificationMethod.UNKNOWN
Exemplo n.º 6
0
def engine_fuzz(engine_impl, target_name, sync_corpus_directory,
                testcase_directory):
  """Run engine fuzzer on untrusted worker."""
  request = untrusted_runner_pb2.EngineFuzzRequest(
      engine=engine_impl.name,
      target_name=target_name,
      sync_corpus_directory=file_host.rebase_to_worker_root(
          sync_corpus_directory),
      testcase_directory=file_host.rebase_to_worker_root(testcase_directory))

  response = host.stub().EngineFuzz(request)
  crashes = [
      engine.Crash(
          input_path=file_host.rebase_to_host_root(crash.input_path),
          stacktrace=utils.decode_to_unicode(crash.stacktrace),
          reproduce_args=crash.reproduce_args,
          crash_time=crash.crash_time) for crash in response.crashes
  ]

  unpacked_stats = {}
  for key, packed_value in six.iteritems(response.stats):
    if packed_value.Is(wrappers_pb2.DoubleValue.DESCRIPTOR):
      value = wrappers_pb2.DoubleValue()
    elif packed_value.Is(wrappers_pb2.Int32Value.DESCRIPTOR):
      value = wrappers_pb2.Int32Value()
    elif packed_value.Is(wrappers_pb2.StringValue.DESCRIPTOR):
      value = wrappers_pb2.StringValue()
    else:
      raise ValueError('Unknown stat type for ' + key)

    packed_value.Unpack(value)
    unpacked_stats[key] = value.value

  result = engine.FuzzResult(
      logs=utils.decode_to_unicode(response.logs),
      command=list(response.command),
      crashes=crashes,
      stats=unpacked_stats,
      time_executed=response.time_executed)

  file_host.pull_testcases_from_worker()
  return result, dict(response.fuzzer_metadata)
Exemplo n.º 7
0
def _pack_values(values):
    """Pack protobuf values."""
    packed = {}
    if values is None:
        return packed

    for key, value in six.iteritems(values):
        packed_value = Any()
        if isinstance(value, float):
            packed_value.Pack(wrappers_pb2.DoubleValue(value=value))
        elif isinstance(value, six.integer_types):
            packed_value.Pack(wrappers_pb2.Int64Value(value=value))
        elif isinstance(value, six.string_types):
            packed_value.Pack(wrappers_pb2.StringValue(value=value))
        else:
            raise ValueError('Unknown stat type for ' + key)

        packed[key] = packed_value

    return packed
Exemplo n.º 8
0
def handle_nested_double_value_params(proto_object, name, attribute_value):
    """Helper function to set a field to a DoubleValue protobuf in a protobuf message.

    Args:
        proto_object (Protobuf message): Any generic protobuf message.
        name (String): The field name within the protobuf message. This name should
            be both the field name and sub-field name separated by a period. For example,
            for the Vec3 velocity field, the name would be 'velocity.x'.
        attribute_value: A value with type matching the field type defined in the protobuf
            message definition. This will be saved in the attribute_name field.

    Returns:
        Nothing. Mutates the input proto_object to update the specified field name to the
        provided value.
    """
    subfields = name.split(".")
    current_attr = proto_object
    for field in subfields:
        current_attr = getattr(current_attr, field)
    current_attr.CopyFrom(wrappers_pb2.DoubleValue(value=attribute_value))
Exemplo n.º 9
0
def engine_fuzz(request, _):
    """Run engine fuzzer."""
    engine_impl = engine.get(request.engine)
    result, fuzzer_metadata = fuzz_task.run_engine_fuzzer(
        engine_impl,
        request.target_name,
        request.sync_corpus_directory,
        request.testcase_directory,
    )

    crashes = [
        untrusted_runner_pb2.EngineCrash(
            input_path=crash.input_path,
            stacktrace=protobuf_utils.encode_utf8_if_unicode(crash.stacktrace),
            reproduce_args=crash.reproduce_args,
            crash_time=crash.crash_time,
        ) for crash in result.crashes
    ]

    packed_stats = {}
    for key, value in six.iteritems(result.stats):
        packed_value = Any()
        if isinstance(value, float):
            packed_value.Pack(wrappers_pb2.DoubleValue(value=value))
        elif isinstance(value, int):
            packed_value.Pack(wrappers_pb2.Int32Value(value=value))
        elif isinstance(value, six.string_types):
            packed_value.Pack(wrappers_pb2.StringValue(value=value))
        else:
            raise ValueError("Unknown stat type for " + key)

        packed_stats[key] = packed_value

    return untrusted_runner_pb2.EngineFuzzResponse(
        logs=protobuf_utils.encode_utf8_if_unicode(result.logs),
        command=result.command,
        crashes=crashes,
        stats=packed_stats,
        time_executed=result.time_executed,
        fuzzer_metadata=fuzzer_metadata,
    )
Exemplo n.º 10
0
def joint_move_example(config):
    """A simple example of using the Boston Dynamics API to command Spot's arm to perform joint moves."""

    # See hello_spot.py for an explanation of these lines.
    bosdyn.client.util.setup_logging(config.verbose)

    sdk = bosdyn.client.create_standard_sdk('ArmJointMoveClient')
    robot = sdk.create_robot(config.hostname)
    robot.authenticate(config.username, config.password)
    robot.time_sync.wait_for_sync()

    assert robot.has_arm(), "Robot requires an arm to run this example."

    # Verify the robot is not estopped and that an external application has registered and holds
    # an estop endpoint.
    verify_estop(robot)

    robot_state_client = robot.ensure_client(
        RobotStateClient.default_service_name)

    lease_client = robot.ensure_client(
        bosdyn.client.lease.LeaseClient.default_service_name)
    lease = lease_client.acquire()
    try:
        with bosdyn.client.lease.LeaseKeepAlive(lease_client):
            # Now, we are ready to power on the robot. This call will block until the power
            # is on. Commands would fail if this did not happen. We can also check that the robot is
            # powered at any point.
            robot.logger.info(
                "Powering on robot... This may take a several seconds.")
            robot.power_on(timeout_sec=20)
            assert robot.is_powered_on(), "Robot power on failed."
            robot.logger.info("Robot powered on.")

            # Tell the robot to stand up. The command service is used to issue commands to a robot.
            # The set of valid commands for a robot depends on hardware configuration. See
            # SpotCommandHelper for more detailed examples on command building. The robot
            # command service requires timesync between the robot and the client.
            robot.logger.info("Commanding robot to stand...")
            command_client = robot.ensure_client(
                RobotCommandClient.default_service_name)
            blocking_stand(command_client, timeout_sec=10)
            robot.logger.info("Robot standing.")

            time.sleep(2.0)

            # Example 1: issue a single point trajectory without a time_since_reference in order to perform
            # a minimum time joint move to the goal obeying the default acceleration and velocity limits.
            sh0 = 0.0692
            sh1 = -1.882
            el0 = 1.652
            el1 = -0.0691
            wr0 = 1.622
            wr1 = 1.550

            traj_point = RobotCommandBuilder.create_arm_joint_trajectory_point(
                sh0, sh1, el0, el1, wr0, wr1)
            arm_joint_traj = arm_command_pb2.ArmJointTrajectory(
                points=[traj_point])
            # Make a RobotCommand
            command = make_robot_command(arm_joint_traj)

            # Send the request
            cmd_id = command_client.robot_command(command)
            robot.logger.info('Moving arm to position 1.')

            # Query for feedback to determine how long the goto will take.
            feedback_resp = command_client.robot_command_feedback(cmd_id)
            robot.logger.info("Feedback for Example 1: single point goto")
            time_to_goal = print_feedback(feedback_resp, robot.logger)
            time.sleep(time_to_goal)

            # Example 2: Single point trajectory with maximum acceleration/velocity constraints specified such
            # that the solver has to modify the desired points to honor the constraints
            sh0 = 0.0
            sh1 = -2.0
            el0 = 2.6
            el1 = 0.0
            wr0 = -0.6
            wr1 = 0.0
            max_vel = wrappers_pb2.DoubleValue(value=1)
            max_acc = wrappers_pb2.DoubleValue(value=5)
            traj_point = RobotCommandBuilder.create_arm_joint_trajectory_point(
                sh0, sh1, el0, el1, wr0, wr1, time_since_reference_secs=1.5)
            arm_joint_traj = arm_command_pb2.ArmJointTrajectory(
                points=[traj_point],
                maximum_velocity=max_vel,
                maximum_acceleration=max_acc)
            # Make a RobotCommand
            command = make_robot_command(arm_joint_traj)

            # Send the request
            cmd_id = command_client.robot_command(command)
            robot.logger.info(
                'Requesting a single point trajectory with unsatisfiable constraints.'
            )

            # Query for feedback
            feedback_resp = command_client.robot_command_feedback(cmd_id)
            robot.logger.info(
                "Feedback for Example 2: planner modifies trajectory")
            time_to_goal = print_feedback(feedback_resp, robot.logger)
            time.sleep(time_to_goal)

            # Example 3: Single point trajectory with default acceleration/velocity constraints and
            # time_since_reference_secs large enough such that the solver can plan a solution to the
            # points that also satisfies the constraints.
            sh0 = 0.0692
            sh1 = -1.882
            el0 = 1.652
            el1 = -0.0691
            wr0 = 1.622
            wr1 = 1.550
            traj_point = RobotCommandBuilder.create_arm_joint_trajectory_point(
                sh0, sh1, el0, el1, wr0, wr1, time_since_reference_secs=1.5)

            arm_joint_traj = arm_command_pb2.ArmJointTrajectory(
                points=[traj_point])

            # Make a RobotCommand
            command = make_robot_command(arm_joint_traj)

            # Send the request
            cmd_id = command_client.robot_command(command)
            robot.logger.info(
                'Requesting a single point trajectory with satisfiable constraints.'
            )

            # Query for feedback
            feedback_resp = command_client.robot_command_feedback(cmd_id)
            robot.logger.info("Feedback for Example 3: unmodified trajectory")
            time_to_goal = print_feedback(feedback_resp, robot.logger)
            time.sleep(time_to_goal)

            # ----- Do a two-point joint move trajectory ------

            # First stow the arm.
            # Build the stow command using RobotCommandBuilder
            stow = RobotCommandBuilder.arm_stow_command()

            # Issue the command via the RobotCommandClient
            command_client.robot_command(stow)

            robot.logger.info("Stow command issued.")
            time.sleep(2.0)

            # First point position
            sh0 = -1.5
            sh1 = -0.8
            el0 = 1.7
            el1 = 0.0
            wr0 = 0.5
            wr1 = 0.0

            # First point time (seconds)
            first_point_t = 2.0

            # Build the proto for the trajectory point.
            traj_point1 = RobotCommandBuilder.create_arm_joint_trajectory_point(
                sh0, sh1, el0, el1, wr0, wr1, first_point_t)

            # Second point position
            sh0 = 1.0
            sh1 = -0.2
            el0 = 1.3
            el1 = -1.3
            wr0 = -1.5
            wr1 = 1.5

            # Second point time (seconds)
            second_point_t = 4.0

            # Build the proto for the second trajectory point.
            traj_point2 = RobotCommandBuilder.create_arm_joint_trajectory_point(
                sh0, sh1, el0, el1, wr0, wr1, second_point_t)

            # Optionally, set the maximum allowable velocity in rad/s that a joint is allowed to
            # travel at. Also set the maximum allowable acceleration in rad/s^2 that a joint is
            # allowed to accelerate at. If these values are not set, a default will be used on
            # the robot.
            # Note that if these values are set too low and the trajectories are too aggressive
            # in terms of time, the desired joint angles will not be hit at the specified time.
            # Some other ways to help the robot actually hit the specified trajectory is to
            # increase the time between trajectory points, or to only specify joint position
            # goals without specifying velocity goals.
            max_vel = wrappers_pb2.DoubleValue(value=2.5)
            max_acc = wrappers_pb2.DoubleValue(value=15)

            # Build up a proto.
            arm_joint_traj = arm_command_pb2.ArmJointTrajectory(
                points=[traj_point1, traj_point2],
                maximum_velocity=max_vel,
                maximum_acceleration=max_acc)
            # Make a RobotCommand
            command = make_robot_command(arm_joint_traj)

            # Send the request
            cmd_id = command_client.robot_command(command)
            robot.logger.info('Moving arm along 2-point joint trajectory.')

            # Query for feedback to determine exactly what the planned trajectory is.
            feedback_resp = command_client.robot_command_feedback(cmd_id)
            robot.logger.info("Feedback for 2-point joint trajectory")
            print_feedback(feedback_resp, robot.logger)

            # Wait until the move completes before powering off.
            block_until_arm_arrives(command_client, cmd_id,
                                    second_point_t + 3.0)

            # Power the robot off. By specifying "cut_immediately=False", a safe power off command
            # is issued to the robot. This will attempt to sit the robot before powering off.
            robot.power_off(cut_immediately=False, timeout_sec=20)
            assert not robot.is_powered_on(), "Robot power off failed."
            robot.logger.info("Robot safely powered off.")
    finally:
        # If we successfully acquired a lease, return it.
        lease_client.return_lease(lease)
Exemplo n.º 11
0
def test_update_profile(db):
    user, token = generate_user()

    with api_session(token) as api:
        with pytest.raises(grpc.RpcError) as e:
            api.UpdateProfile(
                api_pb2.UpdateProfileReq(name=wrappers_pb2.StringValue(
                    value="  ")))
        assert e.value.code() == grpc.StatusCode.INVALID_ARGUMENT
        assert e.value.details() == errors.INVALID_NAME

        with pytest.raises(grpc.RpcError) as e:
            api.UpdateProfile(
                api_pb2.UpdateProfileReq(
                    lat=wrappers_pb2.DoubleValue(value=0),
                    lng=wrappers_pb2.DoubleValue(value=0)))
        assert e.value.code() == grpc.StatusCode.INVALID_ARGUMENT
        assert e.value.details() == errors.INVALID_COORDINATE

        api.UpdateProfile(
            api_pb2.UpdateProfileReq(
                name=wrappers_pb2.StringValue(value="New name"),
                city=wrappers_pb2.StringValue(value="Timbuktu"),
                hometown=api_pb2.NullableStringValue(value="Walla Walla"),
                lat=wrappers_pb2.DoubleValue(value=0.01),
                lng=wrappers_pb2.DoubleValue(value=-2),
                radius=wrappers_pb2.DoubleValue(value=321),
                pronouns=api_pb2.NullableStringValue(value="Ro, Robo, Robots"),
                occupation=api_pb2.NullableStringValue(value="Testing"),
                education=api_pb2.NullableStringValue(value="Couchers U"),
                about_me=api_pb2.NullableStringValue(value="I rule"),
                my_travels=api_pb2.NullableStringValue(
                    value="Oh the places you'll go!"),
                things_i_like=api_pb2.NullableStringValue(value="Couchers"),
                about_place=api_pb2.NullableStringValue(value="My place"),
                hosting_status=api_pb2.HOSTING_STATUS_CAN_HOST,
                meetup_status=api_pb2.MEETUP_STATUS_WANTS_TO_MEETUP,
                languages=api_pb2.RepeatedStringValue(
                    exists=True, value=["Binary", "English"]),
                countries_visited=api_pb2.RepeatedStringValue(
                    exists=True, value=["UK", "Aus"]),
                countries_lived=api_pb2.RepeatedStringValue(
                    exists=True, value=["UK", "Aus"]),
                additional_information=api_pb2.NullableStringValue(
                    value="I <3 Couchers"),
            ))

        user_details = api.GetUser(api_pb2.GetUserReq(user=user.username))
        assert user_details.name == "New name"
        assert user_details.city == "Timbuktu"
        assert user_details.hometown == "Walla Walla"
        assert user_details.pronouns == "Ro, Robo, Robots"
        assert user_details.education == "Couchers U"
        assert user_details.my_travels == "Oh the places you'll go!"
        assert user_details.things_i_like == "Couchers"
        assert user_details.lat == 0.01
        assert user_details.lng == -2
        assert user_details.radius == 321
        assert user_details.occupation == "Testing"
        assert user_details.about_me == "I rule"
        assert user_details.about_place == "My place"
        assert user_details.hosting_status == api_pb2.HOSTING_STATUS_CAN_HOST
        assert user_details.meetup_status == api_pb2.MEETUP_STATUS_WANTS_TO_MEETUP
        assert "Binary" in user_details.languages
        assert "English" in user_details.languages
        assert user_details.additional_information == "I <3 Couchers"
        assert "UK" in user_details.countries_visited
        assert "Aus" in user_details.countries_visited
        assert "UK" in user_details.countries_lived
        assert "Aus" in user_details.countries_lived

        # Test unset values
        api.UpdateProfile(
            api_pb2.UpdateProfileReq(
                hometown=api_pb2.NullableStringValue(is_null=True),
                radius=wrappers_pb2.DoubleValue(value=0),
                pronouns=api_pb2.NullableStringValue(is_null=True),
                occupation=api_pb2.NullableStringValue(is_null=True),
                education=api_pb2.NullableStringValue(is_null=True),
                about_me=api_pb2.NullableStringValue(is_null=True),
                my_travels=api_pb2.NullableStringValue(is_null=True),
                things_i_like=api_pb2.NullableStringValue(is_null=True),
                about_place=api_pb2.NullableStringValue(is_null=True),
                hosting_status=api_pb2.HOSTING_STATUS_UNKNOWN,
                meetup_status=api_pb2.MEETUP_STATUS_UNKNOWN,
                languages=api_pb2.RepeatedStringValue(exists=True, value=[]),
                countries_visited=api_pb2.RepeatedStringValue(exists=True,
                                                              value=[]),
                countries_lived=api_pb2.RepeatedStringValue(exists=True,
                                                            value=[]),
                additional_information=api_pb2.NullableStringValue(
                    is_null=True),
            ))

        user_details = api.GetUser(api_pb2.GetUserReq(user=user.username))
        assert not user_details.hometown
        assert not user_details.radius
        assert not user_details.pronouns
        assert not user_details.occupation
        assert not user_details.education
        assert not user_details.about_me
        assert not user_details.my_travels
        assert not user_details.things_i_like
        assert not user_details.about_place
        assert user_details.hosting_status == api_pb2.HOSTING_STATUS_UNKNOWN
        assert user_details.meetup_status == api_pb2.MEETUP_STATUS_UNKNOWN
        assert not user_details.languages
        assert not user_details.countries_visited
        assert not user_details.countries_lived
        assert not user_details.additional_information
Exemplo n.º 12
0
def serialize_data_frame(
        df,
        project_cols: List[msg.ProjectColumn],
        chunk_size=100) -> Generator[msg.DataRecordsReply, None, None]:
    '''
    Iterate a dataframe's rows as api.DataRecordsReply
    !!! time columns must be converted to int BEFORE this method is called !!!

    Immutable on df

    :param df: DataFrame -
    :param project_cols: ProjectColumn[] - columns and type order to keep things aligned
    :param chunk_size: Int - Optional chunking size for record arrays
    '''
    string_names = [
        x.name for x in project_cols if util.is_string_column_type(x.type)
    ]
    string_dict = {
        col_name: index
        for (index, col_name) in enumerate(string_names)
    }

    number_names = [
        x.name for x in project_cols if util.is_number_column_type(x.type)
    ]
    number_dict = {
        col_name: index
        for (index, col_name) in enumerate(number_names)
    }

    time_names = [
        x.name for x in project_cols if util.is_time_column_type(x.type)
    ]
    time_dict = {
        col_name: index
        for (index, col_name) in enumerate(time_names)
    }

    num_rows = df.shape[0]
    for chunk_df in list(
            filter(lambda x: not x.empty,
                   np.array_split(df, math.ceil(num_rows / chunk_size)))):
        data_records = []
        for _, row in chunk_df.iterrows():
            strings = [
                msg.OptionalString(value=proto.StringValue(value=None))
            ] * len(string_names)
            numbers = [
                msg.OptionalNumber(value=proto.DoubleValue(value=None))
            ] * len(number_names)
            times = [msg.OptionalTime(value=proto.Int64Value(value=None))
                     ] * len(time_names)

            for col in project_cols:
                # Strings
                if util.is_string_column_type(col.type):
                    if row[col.name] == None:
                        strings[string_dict[col.name]] = msg.OptionalString(
                            value=None)
                    else:
                        strings[string_dict[col.name]] = msg.OptionalString(
                            value=proto.StringValue(value=row[col.name]))

                # Numbers
                elif util.is_number_column_type(col.type):
                    if row[col.name] == None or math.isnan(row[col.name]):
                        numbers[number_dict[col.name]] = msg.OptionalNumber(
                            value=None)
                    else:
                        numbers[number_dict[col.name]] = msg.OptionalNumber(
                            value=proto.DoubleValue(value=row[col.name]))

                # Times
                elif util.is_time_column_type(col.type):
                    if row[col.name] == None or math.isnan(
                            row[col.name].value):
                        times[time_dict[col.name]] = msg.OptionalTime(
                            value=None)
                    else:
                        times[time_dict[col.name]] = msg.OptionalTime(
                            value=proto.Int64Value(
                                value=ts_to_unix_epoch_seconds(row[col.name])))
            data_records.append(
                msg.DataRecord(strings=strings, numbers=numbers, times=times))
        yield msg.DataRecordsReply(records=data_records)
Exemplo n.º 13
0
def test_update_profile(db):
    user, token = generate_user()

    with api_session(token) as api:
        with pytest.raises(grpc.RpcError) as e:
            api.UpdateProfile(
                api_pb2.UpdateProfileReq(name=wrappers_pb2.StringValue(
                    value="  ")))
        assert e.value.code() == grpc.StatusCode.INVALID_ARGUMENT
        assert e.value.details() == errors.INVALID_NAME

        with pytest.raises(grpc.RpcError) as e:
            api.UpdateProfile(
                api_pb2.UpdateProfileReq(
                    lat=wrappers_pb2.DoubleValue(value=0),
                    lng=wrappers_pb2.DoubleValue(value=0)))
        assert e.value.code() == grpc.StatusCode.INVALID_ARGUMENT
        assert e.value.details() == errors.INVALID_COORDINATE

        # changing gender shouldn't be allowed
        with pytest.raises(grpc.RpcError) as e:
            api.UpdateProfile(
                api_pb2.UpdateProfileReq(gender=wrappers_pb2.StringValue(
                    value="newgender")))
        assert e.value.code() == grpc.StatusCode.PERMISSION_DENIED

        res = api.UpdateProfile(
            api_pb2.UpdateProfileReq(
                name=wrappers_pb2.StringValue(value="New name"),
                city=wrappers_pb2.StringValue(value="Timbuktu"),
                hometown=api_pb2.NullableStringValue(value="Walla Walla"),
                lat=wrappers_pb2.DoubleValue(value=0.01),
                lng=wrappers_pb2.DoubleValue(value=-2),
                radius=wrappers_pb2.DoubleValue(value=321),
                pronouns=api_pb2.NullableStringValue(value="Ro, Robo, Robots"),
                occupation=api_pb2.NullableStringValue(value="Testing"),
                education=api_pb2.NullableStringValue(value="Couchers U"),
                about_me=api_pb2.NullableStringValue(value="I rule"),
                my_travels=api_pb2.NullableStringValue(
                    value="Oh the places you'll go!"),
                things_i_like=api_pb2.NullableStringValue(value="Couchers"),
                about_place=api_pb2.NullableStringValue(value="My place"),
                hosting_status=api_pb2.HOSTING_STATUS_CAN_HOST,
                meetup_status=api_pb2.MEETUP_STATUS_WANTS_TO_MEETUP,
                languages=api_pb2.RepeatedStringValue(
                    exists=True, value=["Binary", "English"]),
                countries_visited=api_pb2.RepeatedStringValue(
                    exists=True, value=["UK", "Aus"]),
                countries_lived=api_pb2.RepeatedStringValue(
                    exists=True, value=["UK", "Aus"]),
                additional_information=api_pb2.NullableStringValue(
                    value="I <3 Couchers"),
            ))
        # all fields changed
        for field, value in res.ListFields():
            assert value == True

        user = api.GetUser(api_pb2.GetUserReq(user=user.username))
        assert user.name == "New name"
        assert user.city == "Timbuktu"
        assert user.hometown == "Walla Walla"
        assert user.pronouns == "Ro, Robo, Robots"
        assert user.education == "Couchers U"
        assert user.my_travels == "Oh the places you'll go!"
        assert user.things_i_like == "Couchers"
        assert user.lat == 0.01
        assert user.lng == -2
        assert user.radius == 321
        assert user.occupation == "Testing"
        assert user.about_me == "I rule"
        assert user.about_place == "My place"
        assert user.hosting_status == api_pb2.HOSTING_STATUS_CAN_HOST
        assert user.meetup_status == api_pb2.MEETUP_STATUS_WANTS_TO_MEETUP
        assert "Binary" in user.languages
        assert "English" in user.languages
        assert user.additional_information == "I <3 Couchers"
        assert "UK" in user.countries_visited
        assert "Aus" in user.countries_visited
        assert "UK" in user.countries_lived
        assert "Aus" in user.countries_lived
Exemplo n.º 14
0
def test_update_profile(db):
    user, token = generate_user()

    with api_session(token) as api:
        with pytest.raises(grpc.RpcError) as e:
            api.UpdateProfile(
                api_pb2.UpdateProfileReq(name=wrappers_pb2.StringValue(
                    value="  ")))
        assert e.value.code() == grpc.StatusCode.INVALID_ARGUMENT
        assert e.value.details() == errors.INVALID_NAME

        with pytest.raises(grpc.RpcError) as e:
            api.UpdateProfile(
                api_pb2.UpdateProfileReq(
                    lat=wrappers_pb2.DoubleValue(value=0),
                    lng=wrappers_pb2.DoubleValue(value=0)))
        assert e.value.code() == grpc.StatusCode.INVALID_ARGUMENT
        assert e.value.details() == errors.INVALID_COORDINATE

        with pytest.raises(grpc.RpcError) as e:
            api.UpdateProfile(
                api_pb2.UpdateProfileReq(
                    regions_visited=api_pb2.RepeatedStringValue(
                        value=["United States"])))
        assert e.value.code() == grpc.StatusCode.INVALID_ARGUMENT
        assert e.value.details() == errors.INVALID_REGION

        with pytest.raises(grpc.RpcError) as e:
            api.UpdateProfile(
                api_pb2.UpdateProfileReq(
                    regions_lived=api_pb2.RepeatedStringValue(
                        value=["United Kingdom"])))
        assert e.value.code() == grpc.StatusCode.INVALID_ARGUMENT
        assert e.value.details() == errors.INVALID_REGION

        api.UpdateProfile(
            api_pb2.UpdateProfileReq(
                name=wrappers_pb2.StringValue(value="New name"),
                city=wrappers_pb2.StringValue(value="Timbuktu"),
                hometown=api_pb2.NullableStringValue(value="Walla Walla"),
                lat=wrappers_pb2.DoubleValue(value=0.01),
                lng=wrappers_pb2.DoubleValue(value=-2),
                radius=wrappers_pb2.DoubleValue(value=321),
                pronouns=api_pb2.NullableStringValue(value="Ro, Robo, Robots"),
                occupation=api_pb2.NullableStringValue(value="Testing"),
                education=api_pb2.NullableStringValue(value="Couchers U"),
                about_me=api_pb2.NullableStringValue(value="I rule"),
                my_travels=api_pb2.NullableStringValue(
                    value="Oh the places you'll go!"),
                things_i_like=api_pb2.NullableStringValue(value="Couchers"),
                about_place=api_pb2.NullableStringValue(value="My place"),
                hosting_status=api_pb2.HOSTING_STATUS_CAN_HOST,
                meetup_status=api_pb2.MEETUP_STATUS_WANTS_TO_MEETUP,
                language_abilities=api_pb2.RepeatedLanguageAbilityValue(value=[
                    api_pb2.LanguageAbility(
                        code="eng",
                        fluency=api_pb2.LanguageAbility.Fluency.FLUENCY_FLUENT,
                    )
                ], ),
                regions_visited=api_pb2.RepeatedStringValue(
                    value=["CXR", "FIN"]),
                regions_lived=api_pb2.RepeatedStringValue(
                    value=["USA", "EST"]),
                additional_information=api_pb2.NullableStringValue(
                    value="I <3 Couchers"),
            ))

        user_details = api.GetUser(api_pb2.GetUserReq(user=user.username))
        assert user_details.name == "New name"
        assert user_details.city == "Timbuktu"
        assert user_details.hometown == "Walla Walla"
        assert user_details.pronouns == "Ro, Robo, Robots"
        assert user_details.education == "Couchers U"
        assert user_details.my_travels == "Oh the places you'll go!"
        assert user_details.things_i_like == "Couchers"
        assert user_details.lat == 0.01
        assert user_details.lng == -2
        assert user_details.radius == 321
        assert user_details.occupation == "Testing"
        assert user_details.about_me == "I rule"
        assert user_details.about_place == "My place"
        assert user_details.hosting_status == api_pb2.HOSTING_STATUS_CAN_HOST
        assert user_details.meetup_status == api_pb2.MEETUP_STATUS_WANTS_TO_MEETUP
        assert user_details.language_abilities[0].code == "eng"
        assert user_details.language_abilities[
            0].fluency == api_pb2.LanguageAbility.Fluency.FLUENCY_FLUENT
        assert user_details.additional_information == "I <3 Couchers"
        assert user_details.regions_visited == ["CXR", "FIN"]
        assert user_details.regions_lived == ["EST", "USA"]

        # Test unset values
        api.UpdateProfile(
            api_pb2.UpdateProfileReq(
                hometown=api_pb2.NullableStringValue(is_null=True),
                radius=wrappers_pb2.DoubleValue(value=0),
                pronouns=api_pb2.NullableStringValue(is_null=True),
                occupation=api_pb2.NullableStringValue(is_null=True),
                education=api_pb2.NullableStringValue(is_null=True),
                about_me=api_pb2.NullableStringValue(is_null=True),
                my_travels=api_pb2.NullableStringValue(is_null=True),
                things_i_like=api_pb2.NullableStringValue(is_null=True),
                about_place=api_pb2.NullableStringValue(is_null=True),
                hosting_status=api_pb2.HOSTING_STATUS_CAN_HOST,
                meetup_status=api_pb2.MEETUP_STATUS_WANTS_TO_MEETUP,
                language_abilities=api_pb2.RepeatedLanguageAbilityValue(
                    value=[]),
                regions_visited=api_pb2.RepeatedStringValue(value=[]),
                regions_lived=api_pb2.RepeatedStringValue(value=[]),
                additional_information=api_pb2.NullableStringValue(
                    is_null=True),
            ))

        user_details = api.GetUser(api_pb2.GetUserReq(user=user.username))
        assert not user_details.hometown
        assert not user_details.radius
        assert not user_details.pronouns
        assert not user_details.occupation
        assert not user_details.education
        assert not user_details.about_me
        assert not user_details.my_travels
        assert not user_details.things_i_like
        assert not user_details.about_place
        assert user_details.hosting_status == api_pb2.HOSTING_STATUS_CAN_HOST
        assert user_details.meetup_status == api_pb2.MEETUP_STATUS_WANTS_TO_MEETUP
        assert not user_details.language_abilities
        assert not user_details.regions_visited
        assert not user_details.regions_lived
        assert not user_details.additional_information
def convert_slice_metrics_to_proto(
    metrics: Tuple[slicer.SliceKeyOrCrossSliceKeyType,
                   metric_types.MetricsDict],
    add_metrics_callbacks: Optional[List[types.AddMetricsCallbackType]]
) -> metrics_for_slice_pb2.MetricsForSlice:
  """Converts the given slice metrics into serialized proto MetricsForSlice.

  Args:
    metrics: The slice metrics.
    add_metrics_callbacks: A list of metric callbacks. This should be the same
      list as the one passed to tfma.Evaluate().

  Returns:
    The MetricsForSlice proto.

  Raises:
    TypeError: If the type of the feature value in slice key cannot be
      recognized.
  """
  result = metrics_for_slice_pb2.MetricsForSlice()
  slice_key, slice_metrics = metrics

  if slicer.is_cross_slice_key(slice_key):
    result.cross_slice_key.CopyFrom(slicer.serialize_cross_slice_key(slice_key))
  else:
    result.slice_key.CopyFrom(slicer.serialize_slice_key(slice_key))

  slice_metrics = slice_metrics.copy()

  if metric_keys.ERROR_METRIC in slice_metrics:
    logging.warning('Error for slice: %s with error message: %s ', slice_key,
                    slice_metrics[metric_keys.ERROR_METRIC])
    result.metrics[metric_keys.ERROR_METRIC].debug_message = slice_metrics[
        metric_keys.ERROR_METRIC]
    return result

  # Convert the metrics from add_metrics_callbacks to the structured output if
  # defined.
  if add_metrics_callbacks and (not any(
      isinstance(k, metric_types.MetricKey) for k in slice_metrics.keys())):
    for add_metrics_callback in add_metrics_callbacks:
      if hasattr(add_metrics_callback, 'populate_stats_and_pop'):
        add_metrics_callback.populate_stats_and_pop(slice_key, slice_metrics,
                                                    result.metrics)
  for key in sorted(slice_metrics.keys()):
    value = slice_metrics[key]
    if isinstance(value, types.ValueWithTDistribution):
      unsampled_value = value.unsampled_value
      _, lower_bound, upper_bound = (
          math_util.calculate_confidence_interval(value))
      confidence_interval = metrics_for_slice_pb2.ConfidenceInterval(
          lower_bound=convert_metric_value_to_proto(lower_bound),
          upper_bound=convert_metric_value_to_proto(upper_bound),
          standard_error=convert_metric_value_to_proto(
              value.sample_standard_deviation),
          degrees_of_freedom={'value': value.sample_degrees_of_freedom})
      metric_value = convert_metric_value_to_proto(unsampled_value)
      if isinstance(key, metric_types.MetricKey):
        result.metric_keys_and_values.add(
            key=key.to_proto(),
            value=metric_value,
            confidence_interval=confidence_interval)
      else:
        # For v1 we continue to populate bounded_value for backwards
        # compatibility. If metric can be stored to double_value metrics,
        # replace it with a bounded_value.
        # TODO(b/171992041): remove the string-typed metric key branch once v1
        # code is removed.
        if metric_value.WhichOneof('type') == 'double_value':
          # setting bounded_value clears double_value in the same oneof scope.
          metric_value.bounded_value.value.value = unsampled_value
          metric_value.bounded_value.lower_bound.value = lower_bound
          metric_value.bounded_value.upper_bound.value = upper_bound
          metric_value.bounded_value.methodology = (
              metrics_for_slice_pb2.BoundedValue.POISSON_BOOTSTRAP)
        result.metrics[key].CopyFrom(metric_value)
    elif isinstance(value, metrics_for_slice_pb2.BoundedValue):
      metric_value = metrics_for_slice_pb2.MetricValue(
          double_value=wrappers_pb2.DoubleValue(value=value.value.value))
      confidence_interval = metrics_for_slice_pb2.ConfidenceInterval(
          lower_bound=metrics_for_slice_pb2.MetricValue(
              double_value=wrappers_pb2.DoubleValue(
                  value=value.lower_bound.value)),
          upper_bound=metrics_for_slice_pb2.MetricValue(
              double_value=wrappers_pb2.DoubleValue(
                  value=value.upper_bound.value)))
      result.metric_keys_and_values.add(
          key=key.to_proto(),
          value=metric_value,
          confidence_interval=confidence_interval)
    else:
      metric_value = convert_metric_value_to_proto(value)
      if isinstance(key, metric_types.MetricKey):
        result.metric_keys_and_values.add(
            key=key.to_proto(), value=metric_value)
      else:
        # TODO(b/171992041): remove the string-typed metric key branch once v1
        # code is removed.
        result.metrics[key].CopyFrom(metric_value)
  return result