27 #ifdef ENABLE_SONATA_REPORTS
28 #include "bbp/sonata/reports.h"
29 #endif // ENABLE_SONATA_REPORTS
38 static bool all_spikes_return(std::vector<double>& spiketvec, std::vector<int>& spikegidvec) {
40 (*nrn2core_all_spike_vectors_return_)(spiketvec, spikegidvec);
54 }
catch (
const std::length_error& le) {
55 std::cerr <<
"Lenght error" << le.what() << std::endl;
68 std::vector<int>& isvecg,
69 std::vector<double>& osvect,
70 std::vector<int>& osvecg) {
71 osvect.resize(isvect.size());
72 osvecg.resize(isvecg.size());
74 std::vector<std::size_t> perm(isvect.size());
75 std::iota(perm.begin(), perm.end(), 0);
77 std::stable_sort(perm.begin(), perm.end(), [&](std::size_t
i, std::size_t j) {
78 return isvecg[i] < isvecg[j];
81 std::stable_sort(perm.begin(), perm.end(), [&](std::size_t
i, std::size_t j) {
82 return isvect[i] < isvect[j];
85 std::transform(perm.begin(), perm.end(), osvect.begin(), [&](std::size_t
i) {
88 std::transform(perm.begin(), perm.end(), osvecg.begin(), [&](std::size_t
i) {
96 double lmin_time = std::numeric_limits<double>::max();
97 double lmax_time = std::numeric_limits<double>::min();
112 bin_t = bin_t ? bin_t : 1;
115 int idx = (int) (st - min_time) / bin_t;
119 snd_dsps[
i] = snd_dsps[
i - 1] + snd_cnts[
i - 1];
126 rcv_dsps[
i] = rcv_dsps[
i - 1] + rcv_cnts[
i - 1];
128 std::size_t new_sz = 0;
129 for (
const auto& r: rcv_cnts) {
133 std::vector<double> svt_buf(new_sz, 0.0);
134 std::vector<int> svg_buf(new_sz, 0);
153 #ifdef ENABLE_SONATA_REPORTS
157 void output_spike_populations(
const SpikesInfo& spikes_info) {
159 if (spikes_info.population_info.empty()) {
160 sonata_add_spikes_population(
"All",
168 int n_populations = spikes_info.population_info.size();
169 for (
int idx = 0; idx < n_populations; idx++) {
170 const auto& curr_pop = spikes_info.population_info[idx];
171 std::string population_name = curr_pop.first;
172 int population_offset = curr_pop.second;
173 int gid_lower = population_offset;
174 int gid_upper = std::numeric_limits<int>::max();
175 if (idx != n_populations - 1) {
176 gid_upper = spikes_info.population_info[idx + 1].second - 1;
178 std::vector<double> pop_spikevec_time;
179 std::vector<int> pop_spikevec_gid;
186 sonata_add_spikes_population(population_name.data(),
188 pop_spikevec_time.data(),
189 pop_spikevec_time.size(),
190 pop_spikevec_gid.data(),
191 pop_spikevec_gid.size());
194 #endif // ENABLE_SONATA_REPORTS
200 static void output_spikes_parallel(
const char* outpath,
const SpikesInfo& spikes_info) {
201 std::stringstream ss;
202 ss << outpath <<
"/out.dat";
203 std::string fname = ss.str();
207 remove(fname.c_str());
209 #ifdef ENABLE_SONATA_REPORTS
210 sonata_create_spikefile(outpath, spikes_info.file_name.data());
211 output_spike_populations(spikes_info);
212 sonata_write_spike_populations();
213 sonata_close_spikefile();
214 #endif // ENABLE_SONATA_REPORTS
220 const int SPIKE_RECORD_LEN = 64;
222 size_t num_bytes = (
sizeof(char) * num_spikes * SPIKE_RECORD_LEN);
223 char* spike_data = (
char*) malloc(num_bytes);
225 if (spike_data ==
nullptr) {
226 printf(
"Error while writing spikes due to memory allocation\n");
231 strcpy(spike_data,
"");
234 char spike_entry[SPIKE_RECORD_LEN];
235 size_t spike_data_offset = 0;
236 for (
size_t i = 0;
i < num_spikes;
i++) {
237 int spike_entry_chars =
240 strcat_at_pos(spike_data, spike_data_offset, spike_entry, spike_entry_chars);
245 size_t num_chars = strlen(spike_data);
254 std::stringstream ss;
255 ss << outpath <<
"/out.dat";
256 std::string fname = ss.str();
259 std::vector<double> sorted_spikevec_time(
spikevec_time.size());
260 std::vector<int> sorted_spikevec_gid(
spikevec_gid.size());
264 remove(fname.c_str());
266 FILE* f = fopen(fname.c_str(),
"w");
268 std::cout <<
"WARNING: Could not open file for writing spikes." << std::endl;
272 for (std::size_t
i = 0;
i < sorted_spikevec_gid.size(); ++
i)
273 if (sorted_spikevec_gid[
i] > -1)
274 fprintf(f,
"%.8g\t%d\n", sorted_spikevec_time[
i], sorted_spikevec_gid[
i]);
287 output_spikes_parallel(outpath, spikes_info);