NeuralynxIO questions

181 views
Skip to first unread message

Eric Melonakos

unread,
Sep 4, 2020, 1:15:21 PM9/4/20
to Neural Ensemble
Hi,

I'm new to Neo, and I'm having a hard time applying it to my use case.

In particular, I have Neuralynx EEG data that I'd like to analyze in Python. I was previously analyzing my data in MATLAB, but other packages I want to use are in Python, so I'm making the switch. The data only includes .ncs (EEG) and .nev (event) files. When I would analyze the data in MATLAB, I used a function (provided by Neuralynx) to extract the data from a given channel's .ncs file, including the timestamps, sample frequency, an array of the number of valid samples, an array of the data samples themselves, and the file header. I would reconstruct that channel's signal as a single vector using those pieces, which vector could then be used for other analyses (e.g., FFT, etc.). That is where I am coming from. Is there a way to do that with neo.io.NeuralynxIO?

Here's what I've done with my sample data:

import neo.io
recording_directory = 'R191014_R191016_200116/'
recording = neo.io.NeuralynxIO(recording_directory)
ephysData = recording.read_block(signal_group_mode='split-all')
segmentsData = []
for seg in ephysData.segments:
    segmentsData.append(seg)

There are two problems with this:

1. Each segment's data is separated, and I don't know the best way to concatenate the signals from the different segments. (I know different segments are created by jumps in timestamps--as I did previously in MATLAB, I plan on interpolating missing data so I can have consistent timesteps for the entire recording.) I know "recording._sigs_memmap" gives similar outputs to the MATLAB importing function Neuralynx provided, but it's still segmented, and I don't want to reinvent the wheel if I don't need to. It also does not seem to have all of the elements in the number of valid samples array.

2. Even if the segments were all concatenated, it is not obvious how I access the data for things like plotting with matplotlib.pyplot. I can take the FFT of segmentsData[0].analogsignals[0], so I would expect matplotlib.pyplot.plot(segmentsData[0].analogsignals[0]) to work, but it doesn't.

tl;dr: What's the best way to go from "recording = neo.io.NeuralynxIO(recording_directory)" to plotting and calculating the FFT for the entire (non-segmented) recording session for a given channel?

I appreciate any help you can give!

Best,

Eric Melonakos

Samuel Garcia

unread,
Sep 7, 2020, 3:03:47 AM9/7/20
to neurale...@googlegroups.com
Hi Eric,
welcome to python.

First note that in neo, data are not simple numpy.array like you use to have in matalab but AnalogSignal which are numpy.array like but with metadata (samplerate, t_start...)
It is more powerful but it is a bit more difficult to then manipulate for beginners.
A simple approach would to convert then to array immediately.
The manipulation with numpy/scipy will be straightforward then.
You can use AnalogSignal.magnitude for that : it give you the underlying array without copy.
So for a big concatenation you can do

import neo
reader = neo.io.NeuralynxIO(recording_directory)
block = reader.read_block()
all_sig = []  ## this will be a list a numpy arrays
for seg in block.segments:
    anasig = seg.analogsignals[0]
    np_sig = anasig.magnitude
    all_sig.append(np_sig)
#  all sig is now a big numpy array that concatenate all chunks along time axis (0)
all_sig = np.concatenate(all_sig, axis=0)

print(all_sig.shape) will give you (n_sample, n_channel)
you will be able to use fft, plot, ... on it:

import matplot.pyplot as plt
fig, ax = plt.subplots()
ax.plot(
all_sig)
plt.show()

As you mention, when you do this, you are concatenating sigs that normally have a gap in between.
You can also pre allocate a big array with zeros and then insert all chunks at the good sample position in the loop to deal with gaps correctly.
I let you work on this :)

Cheers


Samuel
--
You received this message because you are subscribed to the Google Groups "Neural Ensemble" group.
To unsubscribe from this group and stop receiving emails from it, send an email to neuralensembl...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/neuralensemble/f380e050-8e30-400f-a839-1eed498362c8n%40googlegroups.com.

Eric Melonakos

unread,
Sep 18, 2020, 5:56:36 PM9/18/20
to Neural Ensemble
Thanks for the information, Samuel! That makes a lot of sense.

I have some more questions/clarification:

1. I noticed that blocks of the raw signal with less than 512 valid samples are discarded. Is there a reason for this? From my conversations with Neuralynx technical support, it seems that it would be better to keep all valid samples, even if they come from a chunk with fewer than 512 valid samples.

2. Does block.segments[0].analogsignals[0].t_stop correspond to the time the last sample in that segment was collected? I am guessing it does not, since np.arange(block.segments[0].analogsignals[0].times[-1].magnitude is one timestep shorter.

3. If I do reader.segment_t_start(0,0), I get 0.0 s, whereas if I do block.segments[0].analogsignals[0].t_start, I get 0.001969 s (I am sampling at 500 Hz). Does this mean that the first analog signal sample was collected 0.001969 s after the start of the block? The reason I ask is because I want to make sure I know how to align block.segments[0].events[0].times with the analog signals. For example, would an event with a timestamp of 0.001969 s have occurred at the exact same time as the first analog signal sample (as opposed to 0.001969 s after the first sample)?

4. The analogsignal data imported by Neuralynx's MATLAB function looks like it has the opposite sign as the same data imported into Python with Neo (i.e., multiplied by -1). Is there a reason for this?

5. Is there a way, using neo, to write events back to .nev files? For instance, if I want to correct typos in the event labels or add/delete events?

Thanks again,

Eric

Samuel Garcia

unread,
Sep 21, 2020, 8:17:31 AM9/21/20
to neurale...@googlegroups.com
Dear Eric,
could copy paste this question into a github issue here ?
Julia could also help me to anwser.

Best

Samuel

Eric Melonakos

unread,
Sep 21, 2020, 9:47:27 AM9/21/20
to Neural Ensemble
Done! Thanks!
Reply all
Reply to author
Forward
0 new messages