Program Listing for File meta.hpp#

Return to documentation for file (python/morpheus/morpheus/_lib/include/morpheus/messages/meta.hpp)

/*
 * SPDX-FileCopyrightText: Copyright (c) 2021-2025, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
 * SPDX-License-Identifier: Apache-2.0
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#pragma once

#include "morpheus/export.h"
#include "morpheus/objects/data_table.hpp"  // for IDataTable
#include "morpheus/objects/table_info.hpp"
#include "morpheus/objects/tensor_object.hpp"
#include "morpheus/types.hpp"  // for TensorIndex

#include <cudf/io/types.hpp>
#include <pybind11/pytypes.h>

#include <memory>
#include <optional>
#include <string>
#include <vector>

namespace morpheus {

/****** Component public implementations ******************/
/****** MessageMeta****************************************/

class MORPHEUS_EXPORT MutableTableCtxMgr;

class MORPHEUS_EXPORT MessageMeta
{
  public:
    virtual TensorIndex count() const;

    virtual TableInfo get_info() const;

    virtual TableInfo get_info(const std::string& col_name) const;

    virtual TableInfo get_info(const std::vector<std::string>& column_names) const;

    virtual void set_data(const std::string& col_name, TensorObject tensor);

    virtual void set_data(const std::vector<std::string>& column_names, const std::vector<TensorObject>& tensors);

    virtual MutableTableInfo get_mutable_info() const;

    std::vector<std::string> get_column_names() const;

    bool has_sliceable_index() const;

    virtual std::optional<std::string> ensure_sliceable_index();

    virtual std::shared_ptr<MessageMeta> copy_ranges(const std::vector<RangeType>& ranges) const;

    virtual std::shared_ptr<MessageMeta> get_slice(TensorIndex start, TensorIndex stop) const;

    static std::shared_ptr<MessageMeta> create_from_python(pybind11::object&& data_table);

    static std::shared_ptr<MessageMeta> create_from_cpp(cudf::io::table_with_metadata&& data_table,
                                                        int index_col_count = 0);

  protected:
    MessageMeta(std::shared_ptr<IDataTable> data);

    static pybind11::object cpp_to_py(cudf::io::table_with_metadata&& table, int index_col_count = 0);

    std::shared_ptr<IDataTable> m_data;
};

class MORPHEUS_EXPORT SlicedMessageMeta : public MessageMeta
{
  public:
    SlicedMessageMeta(std::shared_ptr<MessageMeta> other,
                      TensorIndex start                = 0,
                      TensorIndex stop                 = -1,
                      std::vector<std::string> columns = {});

    TensorIndex count() const override;

    TableInfo get_info() const override;

    TableInfo get_info(const std::string& col_name) const override;

    TableInfo get_info(const std::vector<std::string>& column_names) const override;

    MutableTableInfo get_mutable_info() const override;

    std::optional<std::string> ensure_sliceable_index() override;

  private:
    TensorIndex m_start{0};
    TensorIndex m_stop{-1};
    std::vector<std::string> m_column_names;
};

/****** Python Interface **************************/
/****** MessageMetaInterfaceProxy**************************/
struct MORPHEUS_EXPORT MessageMetaInterfaceProxy
{
    static std::shared_ptr<MessageMeta> init_cpp(const std::string& filename);

    static std::shared_ptr<MessageMeta> init_python(pybind11::object&& data_frame);

    static std::shared_ptr<MessageMeta> init_python_meta(const pybind11::object& meta);

    static TensorIndex count(MessageMeta& self);

    static pybind11::object get_data(MessageMeta& self);

    static pybind11::object get_data(MessageMeta& self, std::string col_name);

    static pybind11::object get_data(MessageMeta& self, std::vector<std::string> columns);

    static pybind11::object get_data(MessageMeta& self, pybind11::none none_obj);

    static void set_data(MessageMeta& self, pybind11::object columns, pybind11::object value);

    static std::vector<std::string> get_column_names(MessageMeta& self);

    static pybind11::object get_data_frame(MessageMeta& self);

    static pybind11::object df_property(MessageMeta& self);

    static MutableTableCtxMgr mutable_dataframe(MessageMeta& self);

    static bool has_sliceable_index(MessageMeta& self);

    static std::optional<std::string> ensure_sliceable_index(MessageMeta& self);

    static std::shared_ptr<MessageMeta> copy_ranges(MessageMeta& self, const std::vector<RangeType>& ranges);

    static std::shared_ptr<MessageMeta> get_slice(MessageMeta& self, TensorIndex start, TensorIndex stop);
};  // end of group
}  // namespace morpheus