Skip to content

Adapting BlackrockIO for new features of version 2.3 - #523

Merged
JuliaSprenger merged 51 commits into
NeuralEnsemble:masterfrom
muellerbjoern:timing_discard_empty
Jul 12, 2018
Merged

JuliaSprenger merged 51 commits into
NeuralEnsemble:masterfrom
muellerbjoern:timing_discard_empty

Conversation

@muellerbjoern

Copy link
Copy Markdown
Contributor

Blackrock file version 2.3 enables features like a reset of the global timestamp clock or pauses during recording. This creates multiple segments in nsX data whereas in the nev files there is usually nothing that shows the beginning of a new segment. Thus, nev segments need to be mapped from the nsX or, if this is not available, segments can be distinguished, when a reset occurred and the timestamps start from 0 again. If the recording was paused and thus the timestamps were not reset, this can only be done using the nsX file.
This pull request enables the following:

  • Mapping segments found in nsX to nev:
    • If the recording was paused, a new segment border is entered using the timestamp of the nsX segment
    • If a reset occurred, the nev segment can be detected by a negative difference of timestamps.
      If this is not possible, the segments cannot be distinguished and a ValueError is raised
  • Splitting nev into segments even when no nsX is available. A warning is issued, that 'paused' segments cannot be detected.
  • Empty segments (0 or 1 sample) that are contained in the nsX file are discarded

I will now create test files for this and write tests.

JuliaSprenger and others added 27 commits March 7, 2018 15:49
- time stamps of nsx data pakets are recorded in timestamp resolution and not in sampling rate
* Tries to extract segments in nev (2.3) files via comment events
* Adds additional segments if spike times are reset to 0
* Tries to match segments of nsx files with those of nev files
  -> to be improved
This does not create detectable segments in nev files, so these need not be checked
Enabled loading multi segment nsX files without nev
Now nev files with 'Pause' can be loaded with the nev correctly cut into segments.
Only situation that is not covered are 'Reset' segments that are not shown in the nev
This is faster now and also segment/spiketrain t_start and t_stops are
correct
This makes spikes within one period (i.e. time between two samples) of a
segment belong to the corresponding segment.
Now Blackrock files with pause and/or reset should load completely fine,
even nev without nsX. If something cannot be determined, a warning is
shown or an error is raised, depending on how severe it is
All timestamps seemed to be before the first segment, because with the
tolerance around segments, the start timestamp was negative which lead
to an integer overflow causing the start timestamp to be ~2^32.
could continue until then.
For example ns6 is shifted by 82 samples to ns2, so it starts
recording at 82 and stops about 82 samples after ns2 (as far as one can
tell from the low resolution of ns2). Then spike extraction COULD (not
sure) be from 0 samples to end of ns2 + 82 samples.
So whenever anything is recorded, spike extraction MIGHT happen, thus
the most liberal implementation is used here, in order to get all
spikes.
When no reset was detected in nev although there was one in nsX, then so
far it raised an erros. But if the first segment of the relevant two can
be determined to be empty, then it is clear all spikes belong to the
next segment
@pep8speaks

pep8speaks commented Apr 19, 2018 •

Copy link
Copy Markdown

Hello @bjoern1001001! Thanks for updating the PR.

Line 347:30: E127 continuation line over-indented for visual indent
Line 889:25: E127 continuation line over-indented for visual indent
Line 1699:28: E127 continuation line over-indented for visual indent
Line 1700:28: E127 continuation line over-indented for visual indent

Line 300:21: E128 continuation line under-indented for visual indent

Comment last updated on July 02, 2018 at 13:25 Hours UTC

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage decreased (-0.2%) to 48.727% when pulling dcbafa5 on bjoern1001001:timing_discard_empty into db9357c on NeuralEnsemble:master.

@coveralls

coveralls commented Apr 19, 2018 •

Copy link
Copy Markdown

Coverage Status

Coverage decreased (-0.4%) to 49.936% when pulling 4be6176 on bjoern1001001:timing_discard_empty into 12d7330 on NeuralEnsemble:master.

@muellerbjoern

Copy link
Copy Markdown
Contributor Author

I just added tests that make sure segments are detected correctly and their start and stop times are correct. There are multiple tests with different files in order to check all situations:

  • Failing, because segments could not be separated in nev
  • Warning because of spikes outside of segment
  • Warning because reset was not explicitly documented in nev file
  • Correctly running through with no problems
    I will make a pull request to upload those files to gin now.

In my opinion this PR is thus finished.

Comment thread neo/rawio/blackrockrawio.py Outdated
import os
import re
import warnings
import time

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this import is necessary any more...

@samuelgarcia

Copy link
Copy Markdown
Contributor

Sharp eye.
It was to benchmark internal performence. No longer needed.

I had used this for testing the performance
Those mainly contained now unused print statements I used for debugging
@muellerbjoern

Copy link
Copy Markdown
Contributor Author

Oops, I'm sorry, I forgot this there.
I just fixed it and also removed a few print statements I had used for debugging.

@samuelgarcia What you mentioned some time ago about try: ... except ... not being explicit should be fixed alreadly as well.

These files were duplicate, so I did not upload them to gin but never
removed them from the files that should be downloaded by the tests. I
never noticed, because tests were not failing for me. On Python3, they
are skipped, but on Python2 you would never notice anything because of
how urlretrieve works. Instead of failing with Error 404, it would just
download the HTML page showing an image with 'Error 404' or something.
This makes the files available and thus causes the test not to be
skipped. If one tried accessing them, the test would fail, because the
file is an HTML file.
Comment thread neo/rawio/blackrockrawio.py Outdated
else:
ts = np.math.ceil(t_start * self.__nev_basic_header['timestamp_resolution'])
ind_start = np.searchsorted(timestamp, ts)
# SearchSorted does not apply. timestamp is not sorted!

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this comment still up to date? I guess it's not, but it's not obvious to me.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't remember who write this comment.
Is it me ?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, this stuff is introduced in this PR. I probably entered it in an early version of this PR.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, as far as I remember this came from you. But as it only refers to single segments, searchsorted should work anyway because spikes are sorted for each segment. So I just removed the comment.

Comment thread neo/rawio/blackrockrawio.py Outdated
return waveforms

def _event_count(self, block_index, seg_index, event_channel_index):
# TODO: Should this only return NonNeural events or also Comment events ?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

currently only NonNeural events are counted. Is there a reason to exclude Comment events here?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't remember.
I guess here both nonneural + comments should count in different event channel.

events_data = self.nev_data['NonNeural']
events_data, event_segment_ids = self.nev_data['NonNeural']
ev_dict = self.__nonneural_evtypes[self.__nev_spec](events_data)[name]
mask = ev_dict['mask'] & (event_segment_ids == seg_index)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mask is never used -> can be removed here

Comment thread neo/rawio/blackrockrawio.py Outdated
mask = ev_dict['mask'] & (event_segment_ids == seg_index)

timestamp = events_data[ev_dict['mask']]['timestamp']
labels = events_data[ev_dict['mask']][ev_dict['field']]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mask should be used here instead of ev_dict['mask'] for the 2 previsous lines

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you are right. I fixed this.

Comment thread neo/rawio/blackrockrawio.py Outdated

# Only needs to be done for nev version 2.3
if self.__nev_spec == '2.3':
# TODO: Set earlier for 'self' and find out values!!!

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bjoern1001001: please update these todos and comments

Until now it just returned all event timestamps instead of only the ones
belonging to the correct segment
I had rearranged nev segments for events and spiketrains seperately
causing the events to have a segment_id that is too low, so they were
matched tothe wrong segment. This is now fixed by only rearranging if
nsX segments have to be rearranged
AnalogSignal.t_start must not be set to SpikeTrain.t_start if there were
spikes before the AnalogSignal recording started. This would shift all
values.
Comment thread neo/rawio/blackrockrawio.py Outdated
if length < 2:
nb_empty_segments += 1
self.nsx_data.pop(data_bl)
# TODO: CHECK WHAT HAPPENS HERE AND IF -1 IS CORRECT

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this comment still valid?
I think subtracting 1 of data_bl is not the right solution since it is only providing the reset of the counter in the current run of the loop, but in the next run the index is continued as usual. Maybe it would be better to use while here instead of a for loop.

It was not necessary to reduce the loop index. The supposed fix did not
even do this.
This makes the code a lot more manageable
# Path to nev that has spikes that don't fit nsX segment
filename_nev_outside_seg = self.get_filename_path(
'segment/PauseSpikesOutside/pause_spikes_outside_seg')
# Path to nsX and nev that are correct

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what exactly is 'correct' here? non-corrupted?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants