bool Recordio_protobuf_reader::Decoder::decode_feature()

in src/mlio/recordio_protobuf_reader.cc [561:623]


bool Recordio_protobuf_reader::Decoder::decode_feature(const std::string &name,
                                                       const aialgs::data::Value &value)
{
    const auto &schema = state_->reader->schema();

    std::optional<std::size_t> attr_idx = schema->get_index(name);
    if (attr_idx == std::nullopt) {
        if (state_->warn_bad_instance || state_->error_bad_example) {
            auto msg = fmt::format(
                "The instance #{1:n} in the data store '{0}' has an unknown feature named '{2}'.",
                instance_->data_store().id(),
                instance_->index(),
                name);

            if (state_->warn_bad_instance) {
                logger::warn(msg);
            }

            if (state_->error_bad_example) {
                throw Invalid_instance_error{msg};
            }
        }

        return false;
    }

    attr_idx_ = *attr_idx;

    attr_ = &schema->attributes()[attr_idx_];

    switch (value.value_case()) {
    case aialgs::data::Value::ValueCase::kFloat32Tensor:
        return decode_feature<Data_type::float32>(value.float32_tensor());

    case aialgs::data::Value::ValueCase::kFloat64Tensor:
        return decode_feature<Data_type::float64>(value.float64_tensor());

    case aialgs::data::Value::ValueCase::kInt32Tensor:
        return decode_feature<Data_type::int32>(value.int32_tensor());

    case aialgs::data::Value::ValueCase::kBytes:
    case aialgs::data::Value::ValueCase::VALUE_NOT_SET:
        break;
    }

    if (state_->warn_bad_instance || state_->error_bad_example) {
        auto msg = fmt::format(
            "The feature '{2}' of the instance #{1:n} in the data store '{0}' has an unexpected data type.",
            instance_->data_store().id(),
            instance_->index(),
            attr_->name());

        if (state_->warn_bad_instance) {
            logger::warn(msg);
        }

        if (state_->error_bad_example) {
            throw Invalid_instance_error{msg};
        }
    }

    return false;
}