report_reader.h
1 #pragma once
2 
3 #include <map>
4 #include <string>
5 #include <utility>
6 #include <vector>
7 
8 #include <highfive/H5File.hpp>
9 
10 #include <bbp/sonata/optional.hpp>
11 #include <bbp/sonata/population.h>
12 
13 namespace bbp {
14 namespace sonata {
15 
16 // KeyType will be NodeID for somas report and CompartmentID for elements report
17 template <typename KeyType>
18 struct SONATA_API DataFrame {
19  using DataType = std::vector<KeyType>;
20  std::vector<double> times;
21  DataType ids;
22  // data[times][ids], flattened. n_cols is ids.size()
23  std::vector<float> data;
24 };
25 
26 using Spike = std::pair<NodeID, double>;
27 using Spikes = std::vector<Spike>;
28 struct SpikeTimes {
29  std::vector<NodeID> node_ids;
30  std::vector<double> timestamps;
31 };
32 
34 class SONATA_API SpikeReader
35 {
36  public:
37  class Population
38  {
39  public:
40  enum class Sorting : char {
41  none = 0,
42  by_id = 1,
43  by_time = 2,
44  };
45 
49  std::tuple<double, double> getTimes() const;
50 
54  Spikes get(const nonstd::optional<Selection>& node_ids = nonstd::nullopt,
55  const nonstd::optional<double>& tstart = nonstd::nullopt,
56  const nonstd::optional<double>& tstop = nonstd::nullopt) const;
57 
61  const SpikeTimes& getRawArrays() const;
62 
66  SpikeTimes getArrays(const nonstd::optional<Selection>& node_ids = nonstd::nullopt,
67  const nonstd::optional<double>& tstart = nonstd::nullopt,
68  const nonstd::optional<double>& tstop = nonstd::nullopt) const;
69 
73  Sorting getSorting() const;
74 
75  private:
76  Population(const std::string& filename, const std::string& populationName);
77 
78  SpikeTimes spike_times_;
79  Sorting sorting_ = Sorting::none;
80  // Use for clamping of user values
81  double tstart_, tstop_;
82 
83  void filterNode(Spikes& spikes, const Selection& node_ids) const;
84  void filterTimestamp(Spikes& spikes, double tstart, double tstop) const;
88  Spikes createSpikes() const;
89 
90  friend SpikeReader;
91  };
92 
93  explicit SpikeReader(std::string filename);
94 
98  std::vector<std::string> getPopulationNames() const;
99 
100  const Population& openPopulation(const std::string& populationName) const;
101 
102  private:
103  std::string filename_;
104 
105  // Lazy loaded population
106  mutable std::map<std::string, Population> populations_;
107 };
108 
109 template <typename KeyType>
110 class SONATA_API ReportReader
111 {
112  public:
114  {
115  public:
119  std::tuple<double, double, double> getTimes() const;
120 
124  std::string getTimeUnits() const;
125 
129  std::string getDataUnits() const;
130 
134  bool getSorted() const;
135 
139  std::vector<NodeID> getNodeIds() const;
140 
154  typename DataFrame<KeyType>::DataType getNodeIdElementIdMapping(
155  const nonstd::optional<Selection>& node_ids = nonstd::nullopt,
156  const nonstd::optional<size_t>& block_gap_limit = nonstd::nullopt) const;
157 
166  DataFrame<KeyType> get(
167  const nonstd::optional<Selection>& node_ids = nonstd::nullopt,
168  const nonstd::optional<double>& tstart = nonstd::nullopt,
169  const nonstd::optional<double>& tstop = nonstd::nullopt,
170  const nonstd::optional<size_t>& tstride = nonstd::nullopt,
171  const nonstd::optional<size_t>& block_gap_limit = nonstd::nullopt) const;
172 
173  private:
174  struct NodeIdElementLayout {
175  typename DataFrame<KeyType>::DataType ids;
176  Selection::Ranges node_ranges;
177  std::vector<uint64_t> node_offsets;
178  std::vector<uint64_t> node_index;
179  Selection::Ranges min_max_blocks;
180  };
181 
182  Population(const HighFive::File& file, const std::string& populationName);
183  std::pair<size_t, size_t> getIndex(const nonstd::optional<double>& tstart,
184  const nonstd::optional<double>& tstop) const;
194  NodeIdElementLayout getNodeIdElementLayout(
195  const nonstd::optional<Selection>& node_ids = nonstd::nullopt,
196  const nonstd::optional<size_t>& block_gap_limit = nonstd::nullopt) const;
197 
198  HighFive::Group pop_group_;
199  std::vector<NodeID> node_ids_;
200  std::vector<Selection::Range> node_ranges_;
201  std::vector<uint64_t> node_offsets_;
202  std::vector<uint64_t> node_index_;
203  double tstart_, tstop_, tstep_;
204  std::vector<std::pair<size_t, double>> times_index_;
205  std::string time_units_;
206  std::string data_units_;
207  bool is_node_ids_sorted_;
208 
209  friend ReportReader;
210  };
211 
212  explicit ReportReader(const std::string& filename);
213 
217  std::vector<std::string> getPopulationNames() const;
218 
219  const Population& openPopulation(const std::string& populationName) const;
220 
221  private:
222  HighFive::File file_;
223 
224  // Lazy loaded population
225  mutable std::map<std::string, Population> populations_;
226 };
227 
230 
231 } // namespace sonata
232 } // namespace bbp
bbp::sonata::ReportReader
Definition: report_reader.h:110
bbp::sonata::SpikeTimes
Definition: report_reader.h:28
bbp::sonata::ReportReader::Population
Definition: report_reader.h:113
bbp::sonata::Selection
Definition: selection.h:12
bbp::sonata::DataFrame
Definition: report_reader.h:18
nonstd::optional_lite::optional
class optional
Definition: optional.hpp:521
bbp::sonata::SpikeReader::Population
Definition: report_reader.h:37
bbp::sonata::SpikeReader
Used to read spike files.
Definition: report_reader.h:34