Revision 86d380144b3f85c8951923de873893583bd25edf authored by narendramukherjee on 01 October 2020, 17:54:51 UTC, committed by GitHub on 01 October 2020, 17:54:51 UTC
Change marking of trial start time
emg_make_arrays.py
# Import stuff!
import numpy as np
import tables
import easygui
import sys
import os
# Ask for the directory where the hdf5 file sits, and change to that directory
dir_name = easygui.diropenbox()
os.chdir(dir_name)
# Look for the hdf5 file in the directory
file_list = os.listdir('./')
hdf5_name = ''
for files in file_list:
if files[-2:] == 'h5':
hdf5_name = files
# Open the hdf5 file
hf5 = tables.open_file(hdf5_name, 'r+')
# Grab the names of the arrays containing digital inputs, and pull the data into a numpy array
dig_in_nodes = hf5.list_nodes('/digital_in')
dig_in = []
dig_in_pathname = []
for node in dig_in_nodes:
dig_in_pathname.append(node._v_pathname)
exec("dig_in.append(hf5.root.digital_in.%s[:])" % dig_in_pathname[-1].split('/')[-1])
dig_in = np.array(dig_in)
# Get the stimulus delivery times - take the end of the stimulus pulse as the time of delivery
dig_on = []
for i in range(len(dig_in)):
dig_on.append(np.where(dig_in[i,:] == 1)[0])
change_points = []
for on_times in dig_on:
changes = []
for j in range(len(on_times) - 1):
if np.abs(on_times[j] - on_times[j+1]) > 30:
changes.append(on_times[j])
try:
changes.append(on_times[-1]) # append the last trial which will be missed by this method
except:
pass # Continue without appending anything if this port wasn't on at all
change_points.append(changes)
# Show the user the number of trials on each digital input channel, and ask them to confirm
check = easygui.ynbox(msg = 'Digital input channels: ' + str(dig_in_pathname) + '\n' + 'No. of trials: ' + str([len(changes) for changes in change_points]), title = 'Check and confirm the number of trials detected on digital input channels')
# Go ahead only if the user approves by saying yes
if check:
pass
else:
print("Well, if you don't agree, blech_clust can't do much!")
sys.exit()
# Ask the user which digital input channels should be used for slicing out EMG arrays, and convert the channel numbers into integers for pulling stuff out of change_points
dig_in_channels = easygui.multchoicebox(msg = 'Which digital input channels should be used to slice out EMG data trial-wise?', choices = ([path for path in dig_in_pathname]))
dig_in_channel_nums = []
for i in range(len(dig_in_pathname)):
if dig_in_pathname[i] in dig_in_channels:
dig_in_channel_nums.append(i)
# Ask the user for the pre and post stimulus durations to be pulled out, and convert to integers
durations = easygui.multenterbox(msg = 'What are the signal durations pre and post stimulus that you want to pull out', fields = ['Pre stimulus (ms)', 'Post stimulus (ms)'])
for i in range(len(durations)):
durations[i] = int(durations[i])
# Grab the names of the arrays containing emg recordings
emg_nodes = hf5.list_nodes('/raw_emg')
emg_pathname = []
for node in emg_nodes:
emg_pathname.append(node._v_pathname)
# Create a numpy array to store emg data by trials
emg_data = np.ndarray((len(emg_pathname), len(dig_in_channels), len(change_points[dig_in_channel_nums[0]]), durations[0]+durations[1]))
# And pull out emg data into this array
for i in range(len(emg_pathname)):
exec("data = hf5.root.raw_emg.%s[:]" % emg_pathname[i].split('/')[-1])
for j in range(len(dig_in_channels)):
for k in range(len(change_points[dig_in_channel_nums[j]])):
raw_emg_data = data[change_points[dig_in_channel_nums[j]][k]-durations[0]*30:change_points[dig_in_channel_nums[j]][k]+durations[1]*30]
raw_emg_data = 0.195*(raw_emg_data)
# Downsample the raw data by averaging the 30 samples per millisecond, and assign to emg_data
emg_data[i, j, k, :] = np.mean(raw_emg_data.reshape((-1, 30)), axis = 1)
# Save the emg_data
np.save('emg_data.npy', emg_data)
hf5.close()
Computing file changes ...