BioDynaMo  v1.05.120-25dc9790
csv_reader.h
Go to the documentation of this file.
1 // -----------------------------------------------------------------------------
2 //
3 // Copyright (C) 2021 CERN & University of Surrey for the benefit of the
4 // BioDynaMo collaboration. All Rights Reserved.
5 //
6 // Licensed under the Apache License, Version 2.0 (the "License");
7 // you may not use this file except in compliance with the License.
8 //
9 // See the LICENSE file distributed with this work for details.
10 // See the NOTICE file distributed with this work for additional information
11 // regarding copyright ownership.
12 //
13 // -----------------------------------------------------------------------------
14 
15 #ifndef CORE_UTIL_CSV_READER_H_
16 #define CORE_UTIL_CSV_READER_H_
17 
18 #include <cmath>
19 #include <functional>
20 #include <numeric>
21 #include <string>
22 #include <vector>
23 
24 #include "csv.h"
25 
26 #include "core/util/io.h"
27 
28 namespace bdm {
29 
31 class CSVReader {
32  public:
33  explicit CSVReader(const std::string& file, int skip = 0) : filename_(file) {
34  if (!FileExists(file)) {
35  std::cout << "Error: " << file << " does not exist!" << std::endl;
36  exit(1);
37  };
38  rapidcsv::LabelParams labels(skip);
39  rapidcsv::SeparatorParams separator;
40  rapidcsv::ConverterParams converter(true);
41  doc_ = rapidcsv::Document(file, labels, separator, converter);
44  bad_entries_.resize(num_rows_, false);
45  };
46 
47  template <typename T>
48  std::vector<T> GetColumn(const std::string& column_name) {
49  return doc_.GetColumn<T>(column_name);
50  }
51 
52  template <typename T>
53  T GetCell(size_t col, size_t row) {
54  return doc_.GetCell<T>(col, row);
55  }
56 
57  std::string GetColumnName(size_t idx) { return doc_.GetColumnName(idx); }
58  std::vector<std::string> GetColumnNames() { return doc_.GetColumnNames(); }
59 
60  size_t GetColumnCount() { return doc_.GetColumnCount(); }
61 
62  size_t GetRowCount() { return doc_.GetRowCount(); }
63 
64  size_t GetNumEntries(const std::string& column_name) {
65  std::vector<std::string> ids = doc_.GetColumn<std::string>(column_name);
66  return ids.size();
67  }
68 
69  // Go over each valid entry of a column
70  template <typename T = float, typename Lambda>
71  void ForEachEntryInColumn(const std::string& column_name, Lambda lambda) {
72  for (auto val : GetColumn<T>(column_name)) {
73  if (!std::isnan(val)) {
74  lambda(val);
75  }
76  }
77  }
78 
79  void Print() {
80  std::cout << "Filename\t\t: " << filename_ << std::endl;
81  std::cout << "Row count\t\t: " << num_rows_ << std::endl;
82  std::cout << "Column count\t\t: " << num_cols_ << std::endl;
83  auto num_bad_entries =
84  std::accumulate(bad_entries_.begin(), bad_entries_.end(), 0);
85  std::cout << "Bad entries\t\t: " << num_bad_entries << std::endl;
86  }
87 
88  private:
89  std::string filename_;
90  size_t num_rows_ = 0;
91  size_t num_cols_ = 0;
92  rapidcsv::Document doc_;
93  // Flags if a row has a bad entry
94  std::vector<bool> bad_entries_;
95 };
96 
97 } // namespace bdm
98 
99 #endif // CORE_UTIL_CSV_READER_H_
bdm::CSVReader::GetRowCount
size_t GetRowCount()
Definition: csv_reader.h:62
bdm::CSVReader::GetColumn
std::vector< T > GetColumn(const std::string &column_name)
Definition: csv_reader.h:48
bdm::CSVReader
Reads in a CSV file using rapidcsv.
Definition: csv_reader.h:31
bdm::CSVReader::ForEachEntryInColumn
void ForEachEntryInColumn(const std::string &column_name, Lambda lambda)
Definition: csv_reader.h:71
bdm
Definition: agent.cc:39
bdm::CSVReader::num_rows_
size_t num_rows_
Definition: csv_reader.h:90
bdm::CSVReader::GetColumnName
std::string GetColumnName(size_t idx)
Definition: csv_reader.h:57
bdm::CSVReader::num_cols_
size_t num_cols_
Definition: csv_reader.h:91
bdm::CSVReader::GetNumEntries
size_t GetNumEntries(const std::string &column_name)
Definition: csv_reader.h:64
bdm::CSVReader::Print
void Print()
Definition: csv_reader.h:79
bdm::CSVReader::filename_
std::string filename_
Definition: csv_reader.h:89
bdm::CSVReader::GetColumnNames
std::vector< std::string > GetColumnNames()
Definition: csv_reader.h:58
bdm::CSVReader::bad_entries_
std::vector< bool > bad_entries_
Definition: csv_reader.h:94
bdm::CSVReader::doc_
rapidcsv::Document doc_
Definition: csv_reader.h:92
bdm::CSVReader::GetColumnCount
size_t GetColumnCount()
Definition: csv_reader.h:60
bdm::FileExists
bool FileExists(const std::string &file_name)
Definition: io.cc:78
io.h
bdm::CSVReader::CSVReader
CSVReader(const std::string &file, int skip=0)
Definition: csv_reader.h:33
bdm::CSVReader::GetCell
T GetCell(size_t col, size_t row)
Definition: csv_reader.h:53