Beispiel #1
0
def encode_matrix_fbs(matrix, row_idx=None, col_idx=None):
    """
    Given a 2D DataFrame, ndarray or sparse equivalent, create and return a Matrix flatbuffer.

    :param matrix: 2D DataFrame, ndarray or sparse equivalent
    :param row_idx: index for row dimension, Index or ndarray
    :param col_idx: index for col dimension, Index or ndarray

    NOTE: row indices are (currently) unsupported and must be None
    """

    if row_idx is not None:
        raise ValueError("row indexing not supported for FBS Matrix")
    if matrix.ndim != 2:
        raise ValueError("FBS Matrix must be 2D")

    (n_rows, n_cols) = matrix.shape

    # estimate size needed, so we don't unnecessarily realloc.
    builder = Builder(guess_at_mem_needed(matrix))

    columns = []
    for cidx in range(n_cols - 1, -1, -1):
        # serialize the typed array
        col = matrix.iloc[:, cidx] if isinstance(
            matrix, pd.DataFrame) else matrix[:, cidx]
        typed_arr = serialize_typed_array(builder, col, column_encoding)

        # serialize the Column union
        columns.append(serialize_column(builder, typed_arr))

    # Serialize Matrix.columns[]
    Matrix.MatrixStartColumnsVector(builder, n_cols)
    for c in columns:
        builder.PrependUOffsetTRelative(c)
    matrix_column_vec = builder.EndVector(n_cols)

    # serialize the colIndex if provided
    cidx = None
    if col_idx is not None:
        cidx = serialize_typed_array(builder, col_idx, index_encoding)

    # Serialize Matrix
    matrix = serialize_matrix(builder, n_rows, n_cols, matrix_column_vec, cidx)

    builder.Finish(matrix)
    return builder.Output()
Beispiel #2
0
def simple_request(request_type, client_id=0, text=None, data=None):

    builder = Builder(50)

    if text is not None:
        text = builder.CreateString(text)

    # Create Simple Request Data
    SimpleRequestData.SimpleRequestDataStart(builder)
    SimpleRequestData.SimpleRequestDataAddType(builder, request_type)

    if text is not None:
        SimpleRequestData.SimpleRequestDataAddText(builder, text)

    if data is not None:
        SimpleRequestData.SimpleRequestDataAddData(
            builder,
            data,
        )

    contentOffset = SimpleRequestData.SimpleRequestDataEnd(builder)

    # Create Message Data
    MessageData.MessageDataStart(builder)

    MessageData.MessageDataAddVersion(
        builder, CreateProtocolVersion(builder, *PROTO_VERSION))

    MessageData.MessageDataAddSenderId(builder, client_id)

    MessageData.MessageDataAddContentType(builder, Content.SimpleRequestData)

    MessageData.MessageDataAddContent(builder, contentOffset)

    end = MessageData.MessageDataEnd(builder)
    builder.Finish(end)

    if os.environ.get("DEBUG_TRANSPORT"):
        print('->',
              "SimpleRequestData",
              "%-23s" % requestlookup[request_type],
              data,
              text,
              sep='\t')

    return set_file_identifier(builder.Output())
Beispiel #3
0
 def to_flatbuffer(self, player_index: int) -> Builder:
     builder = Builder(100)
     ControllerState.ControllerStateStart(builder)
     ControllerState.ControllerStateAddSteer(builder, self.steer)
     ControllerState.ControllerStateAddThrottle(builder, self.throttle)
     ControllerState.ControllerStateAddPitch(builder, self.pitch)
     ControllerState.ControllerStateAddYaw(builder, self.yaw)
     ControllerState.ControllerStateAddRoll(builder, self.roll)
     ControllerState.ControllerStateAddJump(builder, self.jump)
     ControllerState.ControllerStateAddBoost(builder, self.boost)
     ControllerState.ControllerStateAddHandbrake(builder, self.handbrake)
     cs_offset = ControllerState.ControllerStateEnd(builder)
     PlayerInput.PlayerInputStart(builder)
     PlayerInput.PlayerInputAddPlayerIndex(builder, player_index)
     PlayerInput.PlayerInputAddControllerState(builder, cs_offset)
     pi_offset = PlayerInput.PlayerInputEnd(builder)
     builder.Finish(pi_offset)
     return builder
Beispiel #4
0
def raw_read_message(client_id, read_tag, read_id, daq_offset, daq_scaling,
                     raw):
    builder = Builder(raw.size * 2 + 200)
    read_id_offset = builder.CreateString(read_id)
    raw_offset = builder.CreateNumpyVector(raw)

    # Create the ReadBlockData object.
    ReadBlockData.ReadBlockDataStart(builder)
    ReadBlockData.ReadBlockDataAddType(
        builder, ReadBlockType.ReadBlockType.PASS_FIRST_RAW_BLOCK)
    ReadBlockData.ReadBlockDataAddReadTag(builder, read_tag)
    ReadBlockData.ReadBlockDataAddBlockIndex(builder, 0)
    ReadBlockData.ReadBlockDataAddTotalBlocks(builder, 1)
    ReadBlockData.ReadBlockDataAddTotalSamples(builder, raw.size)
    ReadBlockData.ReadBlockDataAddDaqOffset(builder, daq_offset)
    ReadBlockData.ReadBlockDataAddDaqScaling(builder, daq_scaling)
    ReadBlockData.ReadBlockDataAddReadId(builder, read_id_offset)
    ReadBlockData.ReadBlockDataAddRawData(builder, raw_offset)

    content_offset = ReadBlockData.ReadBlockDataEnd(builder)

    # Create Message Data
    MessageData.MessageDataStart(builder)

    MessageData.MessageDataAddVersion(
        builder, CreateProtocolVersion(builder, *PROTO_VERSION))

    MessageData.MessageDataAddSenderId(builder, client_id)

    MessageData.MessageDataAddContentType(builder, Content.ReadBlockData)

    MessageData.MessageDataAddContent(builder, content_offset)

    end = MessageData.MessageDataEnd(builder)
    builder.Finish(end)

    return set_file_identifier(builder.Output())
    def serialize(entity: SpacyMessageData) -> bytearray:
        builder = Builder(128)

        payload_offset = None

        if entity.type == SpacyMessageType.SpacyMessageType().SpacyResponse:
            payload_offset = SpacyResponseMapper.serialize(entity.message, builder)

        if entity.type == SpacyMessageType.SpacyMessageType().SpacyRequest:
            payload_offset = SpacyRequestMapper.serialize(entity.message, builder)

        if entity.type == SpacyMessageType.SpacyMessageType().SpacyBatchRequest:
            payload_offset = SpacyBatchRequestMapper.serialize(entity.message, builder)

        if entity.type == SpacyMessageType.SpacyMessageType().SpacyBatchResponse:
            payload_offset = SpacyBatchResponseMapper.serialize(entity.message, builder)

        SpacyMessageClass.SpacyMessageStart(builder)
        SpacyMessageClass.SpacyMessageAddMessageType(builder, entity.type)
        SpacyMessageClass.SpacyMessageAddMessage(builder, payload_offset)
        message_offset = SpacyMessageClass.SpacyMessageEnd(builder)
        builder.Finish(message_offset)

        return builder.Output()