Skip to content

Commit b4b7897

Browse files
committed
Restore events and spikes recording in Open Ephys format
1 parent fb245be commit b4b7897

4 files changed

Lines changed: 69 additions & 9 deletions

File tree

Source/Processors/RecordNode/OpenEphysFormat/OriginalRecording.cpp

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ String OriginalRecording::getEngineID() const
6969

7070
void OriginalRecording::addSpikeElectrode(int index, const SpikeChannel* elec)
7171
{
72-
spikeFileArray.add(nullptr);
72+
//spikeFileArray.add(nullptr); // deprecated
7373
}
7474

7575
void OriginalRecording::resetChannels()
@@ -91,7 +91,7 @@ void OriginalRecording::openFiles(File rootFolder, int experimentNumber, int rec
9191
processorArray.clear();
9292
lastProcId = 0;
9393

94-
//openFile(rootFolder, getEventChannel(0), 0);
94+
openFile(rootFolder, getEventChannel(0), 0);
9595
openMessageFile(rootFolder);
9696

9797
int nChannels = getNumRecordedChannels();
@@ -103,10 +103,15 @@ void OriginalRecording::openFiles(File rootFolder, int experimentNumber, int rec
103103
blockIndex.add(0);
104104
samplesSinceLastTimestamp.add(0);
105105
}
106-
for (int i = 0; i < spikeFileArray.size(); i++)
106+
107+
int nSpikes = getNumRecordedSpikes();
108+
109+
for (int i = 0; i < nSpikes; i++)
107110
{
111+
spikeFileArray.add(nullptr);
108112
openSpikeFile(rootFolder, getSpikeChannel(i), i);
109113
}
114+
110115
}
111116

112117
void OriginalRecording::openFile(File rootFolder, const InfoObjectCommon* ch, int channelIndex)
@@ -214,11 +219,14 @@ void OriginalRecording::openSpikeFile(File rootFolder, const SpikeChannel* elec,
214219

215220
if (!fileExists)
216221
{
222+
217223
String header = generateSpikeHeader(elec);
218224
fwrite(header.toUTF8(), 1, header.getNumBytesAsUTF8(), spFile);
225+
std::cout << "Wrote header." << std::endl;
219226
}
220227
diskWriteLock.exit();
221228
spikeFileArray.set(channelIndex, spFile);
229+
std::cout << "Added file." << std::endl;
222230

223231
}
224232

@@ -364,7 +372,7 @@ String OriginalRecording::generateSpikeHeader(const SpikeChannel* elec)
364372
header += "';\n";
365373

366374
header += "header.num_channels = ";
367-
header += String(elec->getNumChannels());
375+
header += String(c);
368376
header += ";\n";
369377

370378
header += "header.sampleRate = ";
@@ -592,6 +600,7 @@ void OriginalRecording::closeFiles()
592600
}
593601
}
594602
fileArray.clear();
603+
595604
blockIndex.clear();
596605
samplesSinceLastTimestamp.clear();
597606
for (int i = 0; i < spikeFileArray.size(); i++)
@@ -630,12 +639,18 @@ void OriginalRecording::closeFiles()
630639

631640
void OriginalRecording::writeSpike(int electrodeIndex, const SpikeEvent* spike)
632641
{
642+
//std::cout << "Electrode index: " << electrodeIndex << std::endl;
643+
633644
if (spikeFileArray[electrodeIndex] == nullptr)
634645
return;
635646

647+
//std::cout << "Got spike" << std::endl;
648+
636649
HeapBlock<char> spikeBuffer;
637650
const SpikeChannel* channel = getSpikeChannel(electrodeIndex);
638651

652+
//std::cout << "Got spike channel" << std::endl;
653+
639654
int totalSamples = channel->getTotalSamples() * channel->getNumChannels();
640655
int numChannels = channel->getNumChannels();
641656
int chanSamples = channel->getTotalSamples();
@@ -658,6 +673,8 @@ void OriginalRecording::writeSpike(int electrodeIndex, const SpikeEvent* spike)
658673
zeromem(spikeBuffer.getData() + 32, 2 * sizeof(float));
659674
*reinterpret_cast<uint16*>(spikeBuffer.getData() + 40) = channel->getSampleRate();
660675

676+
//std::cout << "Allocated memory" << std::endl;
677+
661678
int ptrIdx = 0;
662679
uint16* dataIntPtr = reinterpret_cast<uint16*>(spikeBuffer.getData() + 42);
663680
const float* spikeDataPtr = spike->getDataPointer();
@@ -683,6 +700,8 @@ void OriginalRecording::writeSpike(int electrodeIndex, const SpikeEvent* spike)
683700
ptrIdx += sizeof(int16);
684701
}
685702

703+
//std::cout << "Starting disk write" << std::endl;
704+
686705
diskWriteLock.enter();
687706

688707
fwrite(spikeBuffer, 1, totalBytes, spikeFileArray[electrodeIndex]);
@@ -693,6 +712,8 @@ void OriginalRecording::writeSpike(int electrodeIndex, const SpikeEvent* spike)
693712
spikeFileArray[electrodeIndex]); // ptr to FILE object
694713

695714
diskWriteLock.exit();
715+
716+
//std::cout << "Wrote to file" << std::endl;
696717
}
697718

698719
void OriginalRecording::writeXml()

Source/Processors/RecordNode/RecordNode.cpp

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ RecordNode::~RecordNode()
7171

7272
void RecordNode::addInputChannel(const GenericProcessor* sourceNode, int chan)
7373
{
74+
// not getting called
7475

7576
if (chan != AccessClass::getProcessorGraph()->midiChannelIndex)
7677
{
@@ -85,13 +86,12 @@ void RecordNode::addInputChannel(const GenericProcessor* sourceNode, int chan)
8586
}
8687
else
8788
{
88-
8989
for (int n = 0; n < sourceNode->getTotalEventChannels(); n++)
9090
{
9191
const EventChannel* orig = sourceNode->getEventChannel(n);
9292
//only add to the record node the events originating from this processor, to avoid duplicates
9393
if (orig->getSourceNodeID() == sourceNode->getNodeId())
94-
eventChannelArray.add(new EventChannel(*orig));
94+
nonOwnedEventChannelArray.add(new EventChannel(*orig));
9595

9696
}
9797

@@ -357,6 +357,24 @@ void RecordNode::updateSubprocessorMap()
357357
synchronizer->setSyncChannel(chan->getSourceNodeID(), chan->getSubProcessorIdx(), ch);
358358
}
359359

360+
}
361+
362+
for (int ch = 0; ch < nonOwnedEventChannelArray.size(); ch++)
363+
{
364+
365+
EventChannel* chan = nonOwnedEventChannelArray[ch];
366+
int sourceID = chan->getSourceNodeID();
367+
int subProcID = chan->getSubProcessorIdx();
368+
369+
eventMap[sourceID][subProcID] = chan->getNumChannels();
370+
371+
if (dataChannelStates[sourceID][subProcID].size() && !syncChannelMap[sourceID][subProcID])
372+
{
373+
syncOrderMap[sourceID][subProcID] = ch;
374+
syncChannelMap[sourceID][subProcID] = 0;
375+
synchronizer->setSyncChannel(chan->getSourceNodeID(), chan->getSubProcessorIdx(), ch);
376+
}
377+
360378
}
361379

362380
}

Source/Processors/RecordNode/RecordNode.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ class RecordNode : public GenericProcessor, public FilenameComponentListener
122122
std::vector<std::vector<int>> subProcessorMap;
123123
std::vector<int> startRecChannels;
124124

125+
Array<EventChannel*> nonOwnedEventChannelArray;
126+
125127
bool isSyncReady;
126128

127129
//TODO: Need to validate these new methods

Source/Processors/RecordNode/RecordThread.cpp

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -208,14 +208,24 @@ void RecordThread::writeSynchronizedData(const AudioSampleBuffer& dataBuffer, co
208208
std::vector<SpikeMessagePtr> spikes;
209209
int nSpikes = m_spikeQueue->getEvents(spikes, maxSpikes);
210210

211+
int total_null = 0;
212+
int total_real = 0;
213+
211214
for (int sp = 0; sp < nSpikes; ++sp)
212215
{
213216
if (spikes[sp] == NULL)
214-
std::cout << "Got NULL" << std::endl;
215-
else
217+
{
218+
total_null++;
219+
}
220+
else{
221+
216222
m_engine->writeSpike(spikes[sp]->getExtra(), &spikes[sp]->getData());
223+
total_real++;
224+
}
217225
//m_engine->writeSpike(0, &spikes[sp]->getData());
218226
}
227+
if (total_null > 0)
228+
std::cout << "Total null: " << total_null << ", total real: " << total_real << std::endl;
219229
}
220230

221231
void RecordThread::writeData(const AudioSampleBuffer& dataBuffer, int maxSamples, int maxEvents, int maxSpikes, bool lastBlock)
@@ -277,10 +287,19 @@ void RecordThread::writeData(const AudioSampleBuffer& dataBuffer, int maxSamples
277287
std::vector<SpikeMessagePtr> spikes;
278288
int nSpikes = m_spikeQueue->getEvents(spikes, maxSpikes);
279289

290+
//int total_null = 0;
291+
//int total_real = 0;
292+
280293
for (int sp = 0; sp < nSpikes; ++sp)
281294
{
282-
m_engine->writeSpike(spikes[sp]->getExtra(), &spikes[sp]->getData());
295+
if (spikes[sp] != NULL)
296+
{
297+
m_engine->writeSpike(spikes[sp]->getExtra(), &spikes[sp]->getData());
298+
}
283299
}
300+
301+
//if (total_null > 0)
302+
// std::cout << "Total null: " << total_null << ", total real: " << total_real << std::endl;
284303

285304
}
286305

0 commit comments

Comments
 (0)