feature_output.cc 2.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/******************************************************************************
 * Copyright 2017 The Apollo Authors. All Rights Reserved.
 *
 * 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.
 *****************************************************************************/

#include "modules/prediction/common/feature_output.h"

19
#include <string>
P
panjiacheng 已提交
20
#include <vector>
21 22

#include "modules/common/util/file.h"
23
#include "modules/prediction/common/prediction_system_gflags.h"
24 25 26 27

namespace apollo {
namespace prediction {

28
Features FeatureOutput::features_;
P
panjiacheng 已提交
29
ListDataForLearning FeatureOutput::list_data_for_learning_;
30 31
std::size_t FeatureOutput::idx_feature_ = 0;
std::size_t FeatureOutput::idx_learning_ = 0;
32

33 34 35
void FeatureOutput::Close() {
  ADEBUG << "Close feature output";
  Write();
36
  WriteDataForLearning();
K
kechxu 已提交
37 38 39 40
  Clear();
}

void FeatureOutput::Clear() {
41 42
  idx_feature_ = 0;
  idx_learning_ = 0;
K
kechxu 已提交
43
  features_.Clear();
P
panjiacheng 已提交
44
  list_data_for_learning_.Clear();
45
}
46

C
Calvin Miao 已提交
47 48 49 50
bool FeatureOutput::Ready() {
  Clear();
  return true;
}
K
kechxu 已提交
51

52 53 54 55
void FeatureOutput::Insert(const Feature& feature) {
  features_.add_feature()->CopyFrom(feature);
}

P
panjiacheng 已提交
56 57 58 59 60 61 62 63 64
void FeatureOutput::InsertDataForLearning(
    const Feature& feature, const std::vector<double>& feature_values) {
  DataForLearning* data_for_learning =
      list_data_for_learning_.add_data_for_learning();
  data_for_learning->set_id(feature.id());
  data_for_learning->set_timestamp(feature.timestamp());
  for (size_t i = 0; i < feature_values.size(); ++i) {
    data_for_learning->add_features_for_learning(feature_values[i]);
  }
65 66
}

67
void FeatureOutput::Write() {
K
kechxu 已提交
68 69
  if (features_.feature_size() <= 0) {
    ADEBUG << "Skip writing empty feature.";
70 71 72
  } else {
    const std::string file_name =
        FLAGS_prediction_data_dir + "/feature." +
73
        std::to_string(idx_feature_) + ".bin";
74 75
    common::util::SetProtoToBinaryFile(features_, file_name);
    features_.Clear();
76
    ++idx_feature_;
77
  }
78
}
79

80 81
void FeatureOutput::WriteDataForLearning() {
  if (list_data_for_learning_.data_for_learning_size() <= 0) {
82 83 84 85
    ADEBUG << "Skip writing empty data_for_learning.";
  } else {
    const std::string file_name =
        FLAGS_prediction_data_dir + "/datalearn." +
86
        std::to_string(idx_learning_) + ".bin";
P
panjiacheng 已提交
87 88
    common::util::SetProtoToBinaryFile(list_data_for_learning_, file_name);
    list_data_for_learning_.Clear();
89
    ++idx_learning_;
K
kechxu 已提交
90
  }
91
}
92

93 94
int FeatureOutput::Size() { return features_.feature_size(); }

95 96
}  // namespace prediction
}  // namespace apollo