Skip to content

Commit

Permalink
Merge pull request open-ephys#77 from malfatti/FixOpenEphysPy
Browse files Browse the repository at this point in the history
Add dtype argument to load() function
  • Loading branch information
jsiegle authored Jul 29, 2019
2 parents d73ec0b + 8a13c71 commit 7903bcc
Showing 1 changed file with 21 additions and 21 deletions.
42 changes: 21 additions & 21 deletions Python3/OpenEphys.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@
MAX_NUMBER_OF_RECORDS = int(1e6)
MAX_NUMBER_OF_EVENTS = int(1e6)

def load(filepath):
def load(filepath, dtype = float):

# redirects to code for individual file types
if 'continuous' in filepath:
data = loadContinuous(filepath)
data = loadContinuous(filepath, dtype)
elif 'spikes' in filepath:
data = loadSpikes(filepath)
elif 'events' in filepath:
Expand Down Expand Up @@ -71,20 +71,20 @@ def loadFolder(folderpath, dtype = float, **kwargs):

return data

def loadFolderToArray(folderpath, channels = 'all', chprefix = 'CH',
def loadFolderToArray(folderpath, channels = 'all', chprefix = 'CH',
dtype = float, session = '0', source = '100'):
'''Load continuous files in specified folder to a single numpy array. By default all
CH continous files are loaded in numerical order, ordering can be specified with
optional channels argument which should be a list of channel numbers.'''

if channels == 'all':
channels = _get_sorted_channels(folderpath, chprefix, session, source)

if session == '0':
filelist = [source + '_'+chprefix + x + '.continuous' for x in map(str,channels)]
else:
filelist = [source + '_'+chprefix + x + '_' + session + '.continuous' for x in map(str,channels)]

t0 = time.time()
numFiles = 1

Expand Down Expand Up @@ -403,33 +403,33 @@ def __str__(self):
return str(self.prog_bar)
#*************************************************************

def pack_2(folderpath, filename = '', channels = 'all', chprefix = 'CH',
def pack_2(folderpath, filename = '', channels = 'all', chprefix = 'CH',
dref = None, session = '0', source = '100'):

'''Alternative version of pack which uses numpy's tofile function to write data.
pack_2 is much faster than pack and avoids quantization noise incurred in pack due
to conversion of data to float voltages during loadContinous followed by rounding
back to integers for packing.
filename: Name of the output file. By default, it follows the same layout of continuous files,
but without the channel number, for example, '100_CHs_3.dat' or '100_ADCs.dat'.
channels: List of channel numbers specifying order in which channels are packed. By default
all CH continous files are packed in numerical order.
chprefix: String name that defines if channels from headstage, auxiliary or ADC inputs
will be loaded.
dref: Digital referencing - either supply a channel number or 'ave' to reference to the
average of packed channels.
source: String name of the source that openephys uses as the prefix. It is usually 100,
if the headstage is the first source added, but can specify something different.
'''

data_array = loadFolderToArray(folderpath, channels, chprefix, np.int16, session, source)

if dref:
if dref == 'ave':
print('Digital referencing to average of all channels.')
Expand All @@ -441,27 +441,27 @@ def pack_2(folderpath, filename = '', channels = 'all', chprefix = 'CH',
reference = deepcopy(data_array[:,channels.index(dref)])
for i in range(data_array.shape[1]):
data_array[:,i] = data_array[:,i] - reference

if session == '0': session = ''
else: session = '_'+session

if not filename: filename = source + '_' + chprefix + 's' + session + '.dat'
print('Packing data to file: ' + filename)
data_array.tofile(os.path.join(folderpath,filename))


def _get_sorted_channels(folderpath, chprefix='CH', session='0', source='100'):
Files = [f for f in os.listdir(folderpath) if '.continuous' in f
and '_'+chprefix in f
Files = [f for f in os.listdir(folderpath) if '.continuous' in f
and '_'+chprefix in f
and source in f]

if session == '0':
Files = [f for f in Files if len(f.split('_')) == 2]
Chs = sorted([int(f.split('_'+chprefix)[1].split('.')[0]) for f in Files])
else:
Files = [f for f in Files if len(f.split('_')) == 3
Files = [f for f in Files if len(f.split('_')) == 3
and f.split('.')[0].split('_')[2] == session]

Chs = sorted([int(f.split('_'+chprefix)[1].split('_')[0]) for f in Files])

return(Chs)

0 comments on commit 7903bcc

Please sign in to comment.