pynapple-org / pynapple

PYthon Neural Analysis Package :pineapple:
https://pynapple-org.github.io/pynapple/
MIT License
243 stars 59 forks source link

append_NWB_LFP with TsdFrame not working #297

Closed kippfreud closed 1 month ago

kippfreud commented 1 month ago

I would like to load a continuous EEG dataset and use append_NWB_LFP to append it to a preexisting nwb file. To do this I adapted code from the wiki for loading memory mapped files. However the code breaks when using the example dataset provided with the repo:

import numpy as np
import pynapple as nap
import pandas as pd

eeg_path = "data/A2929-200711/A2929-200711.eeg"
frequency = 1250 # Hz
n_channels = 16
f = open(eeg_path, 'rb')
startoffile = f.seek(0, 0)
endoffile = f.seek(0, 2)
f.close()
bytes_size = 2
n_samples = int((endoffile-startoffile)/n_channels/bytes_size)
duration = n_samples/frequency
interval = 1/frequency

fp = np.memmap(eeg_path, np.int16, 'r', shape = (n_samples, n_channels))
timestep = np.arange(0, n_samples)/frequency

eeg = nap.TsdFrame(t=timestep, d=fp)

nap.append_NWB_LFP("data/A2929-200711/",
                   eeg) 

This is because the eeg var is a TsdFrame, so lines 285-286 of io.misc (in append_NWB_LFP function) will be triggered.

if isinstance(lfp, nap.TsdFrame):
        channels = lfp.columns.values

which will set channels to a ndarray. This will cause an error with lines 296 (append_NWB_LFP function):

all_table_region = nwbfile.create_electrode_table_region(
        region=channels, description="", name="electrodes"
    )

which expects region to be a slice or list or tuple. I suggest casting lfp.columns.values in append_NWB_LFP to a list to fix.