示例#1
1
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()
示例#2
0
    def build_feed_request(self, builder: flatbuffers.Builder) -> bytearray:

        build_market = []
        size = len(self.markets)
        for market in self.markets:
            build_market.append(builder.CreateString(market))
        req_id_string = builder.CreateString(self.req_id)

        FbsFr.FeedRequestStartMarketsVector(builder, size)
        for b in reversed(build_market):
            builder.PrependUOffsetTRelative(b)
        markets = builder.EndVector(size)

        FbsFr.FeedRequestStart(builder)
        FbsFr.FeedRequestAddReqId(builder, req_id_string)
        FbsFr.FeedRequestAddSubReqType(builder, self.sub_req_type)
        FbsFr.FeedRequestAddFeedType(builder, self.feed_type)
        FbsFr.FeedRequestAddGrouping(builder, self.grouping)
        FbsFr.FeedRequestAddDepth(builder, self.depth)
        FbsFr.FeedRequestAddFrequency(builder, self.frequency)
        FbsFr.FeedRequestAddMarkets(builder, markets)
        feed_req = FbsFr.FeedRequestEnd(builder)

        FbsMessage.MessageStart(builder)
        FbsMessage.MessageAddTts(builder, time_ns())
        FbsMessage.MessageAddBodyType(builder, FbsBody.Body.FeedRequest)
        FbsMessage.MessageAddBody(builder, feed_req)
        msg = FbsMessage.MessageEnd(builder)
        builder.Finish(msg)
        return builder.Output()
示例#3
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())
示例#4
0
    def serialize(entity: TesseractMessageEntity) -> bytearray:
        builder = Builder(1024)
        payload_offset = entity.message.serialize(builder)
        TesseractMessageStart(builder)
        TesseractMessageAddMessageType(builder, entity.type)
        TesseractMessageAddMessage(builder, payload_offset)

        message_offset = TesseractMessageEnd(builder)
        builder.Finish(message_offset)
        return builder.Output()
示例#5
0
def packmsg():
    b=Builder(0)
	#Create the field and add it directly.
    Vec2DStart(b)
    p1= CreatePoint2D(b, 3, 5)
    Vec2DAddP1(b, p1)
    p2= CreatePoint2D(b, 9, 15)
    Vec2DAddP2(b, p2)
    
    #Vec2DAddP1(b, CreatePoint2D(b, 3, 5))
    #Vec2DAddP2(b, CreatePoint2D(b, 9, 15))
    pos=Vec2DEnd(b)
    b.Finish(pos)
    return b.Output()
示例#6
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())
示例#7
0
    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()