Falcon
Loading...
Searching...
No Matches
spikedata.hpp
1// ---------------------------------------------------------------------
2// This file is part of falcon-core.
3//
4// Copyright (C) 2015, 2016, 2017 Neuro-Electronics Research Flanders
5//
6// Falcon-server is free software: you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10//
11// Falcon-server is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15//
16// You should have received a copy of the GNU General Public License
17// along with falcon-core. If not, see <http://www.gnu.org/licenses/>.
18// ---------------------------------------------------------------------
19
20#pragma once
21
22#include <string>
23#include <vector>
24
25#include "channelvalidity.hpp"
26#include "idata.hpp"
27#include "spikedata_common.hpp"
28#include "utilities/general.hpp"
29
30namespace nsSpikeType {
31
32using ParentType = AnyType;
33
34struct Parameters {
35 Parameters(double bufsize = 0., unsigned int nchan = 0, double rate = 0.)
36 : buffer_size(bufsize), nchannels(nchan), sample_rate(rate) {}
37
38 double buffer_size;
39 unsigned int nchannels;
40 double sample_rate;
41};
42
43class Data : public IData<Data, ParentType> {
44 public:
45 using BaseClass = IData<Data, ParentType>;
46
47 Data(unsigned int nchannels, double buffer_size, double sample_rate, size_t max_nspikes = 0);
48
49 Data(const Parameters& parameters)
50 : Data(parameters.nchannels, parameters.buffer_size, parameters.sample_rate) {}
51
52 static const std::string static_datatype() { return "spike"; }
53 static const std::string static_dataname() { return "spikes"; }
54
55 Parameters parameters() const { return Parameters(buffer_size_, n_channels_, sample_rate_); }
56
57 void ClearData() override;
58
59 unsigned int n_channels() const;
60
61 double sample_rate() const;
62 double buffer_size() const { return buffer_size_; }
63
64 void add_spike(const std::vector<double>& amplitudes,
65 uint64_t hw_timestamp); // 1st argument will change to a
66 // better interface for matrices
67
68 void add_spike(double* amplitudes, uint64_t hw_timestamp);
69
70 unsigned int n_detected_spikes() const;
71
72 std::vector<double>& amplitudes();
73
74 ChannelValidityMask& validity_mask();
75
76 const std::vector<uint64_t>& ts_detected_spikes() const;
77
78 uint64_t ts_detected_spikes(int index) const;
79
80 std::vector<double>::const_iterator spike_amplitudes(std::size_t spike_index) const;
81
82 void SerializeBinary(std::ostream& stream,
83 Serialization::Format format = Serialization::Format::FULL) const final;
84
85 void SerializeYAML(YAML::Node& node,
86 Serialization::Format format = Serialization::Format::FULL) const final;
87
88 void YAMLDescription(YAML::Node& node,
89 Serialization::Format format = Serialization::Format::FULL) const final;
90
91 void SerializeFlatBuffer(flexbuffers::Builder& flex_builder) final;
92
93 protected:
94 uint8_t n_channels_;
95 unsigned int n_detected_spikes_;
96 std::vector<double> amplitudes_;
97 // std::vector<double> widths_;
98 std::vector<uint64_t> hw_ts_detected_spikes_;
99 double buffer_size_;
100 double sample_rate_;
101 ChannelValidityMask validity_mask_;
102 ChannelValidityMask default_validity_mask_; // independent of spike detection outcome
103
104 public:
105 static constexpr unsigned int DEFAULT_MAX_NSPIKES =
106 MAX_N_SPIKES_IN_BUFFER; // max expected # of spikes in a buffer
107
108 protected:
109 // for serialization
110 const std::string N_CHANNELS = "n_channels";
111 const std::string N_DETECTED_SPIKES = "n_detected_spikes";
112 const std::string TS_DETECTED_SPIKES = "TS_detected_spikes";
113 const std::string SPIKE_AMPLITUDES = "spike_amplitudes";
114};
115
116class Capabilities {
117 public:
118 Capabilities(ChannelRange channel_range = ChannelRange(1, MAX_N_CHANNELS_SPIKE_DETECTION))
119 : channel_range_(channel_range) {}
120
121 ChannelRange channel_range() const { return channel_range_; }
122
123 void Validate(const Data& prototype) const {
124 if (!channel_range_.inrange(prototype.n_channels())) {
125 throw std::runtime_error("Number of channels cannot be zero and needs to be in range " +
126 channel_range_.to_string());
127 }
128 }
129
130 protected:
131 ChannelRange channel_range_;
132};
133
134} // namespace nsSpikeType
135
136using SpikeType = DefineType<nsSpikeType::Data, AnyType, true, nsSpikeType::Capabilities,
Definition channelvalidity.hpp:30
Convenient template class to define data types.
Definition idata.hpp:75
Base class of all data object classes.
Definition idata.hpp:95
Definition spikedata.hpp:116
Definition spikedata.hpp:43
Definition spikedata.hpp:34