Ejemplo n.º 1
0
def get_amp_names(file):
    a = load_rhd.read_data(file)
    amps = [
        a['amplifier_channels'][i]['native_channel_name']
        for i in range(len(a['amplifier_channels']))
    ]
    dins = [
        a['board_dig_in_channels'][i]['native_channel_name']
        for i in range(len(a['board_dig_in_channels']))
    ]
    return sorted(amps), sorted(dins)
Ejemplo n.º 2
0
 def __init__(self, filename):
     try:
         self.filename = filename
         self.data_dict = intan.read_data(filename)
     except:
         print('Unvalid file \ path for *.rhd data')
     [self.data_vec, self.time_sync, self.trigger_vector] = self.sync()
     self.sample_rate = self.data_dict['frequency_parameters'][
         'amplifier_sample_rate']
     self.filtered_data = self.filter_data()
     self.rms_data = self.apply_rms()
     self.minimal_sample_size = 300
Ejemplo n.º 3
0
    def parse_rhd_file(self, filename, notch, bad_EMG, comb_filter):
        rhd_data = read_data(filename)
        if self.date_num < 20190701:
            self.EMG_fs = 2011.148
        else:
            self.EMG_fs = rhd_data['frequency_parameters'][
                'amplifier_sample_rate']
        # ---------- In default case, the items in EMG_names_single are obtained from the rhd file  -------- #
        EMG_single = rhd_data['amplifier_data']
        EMG_names_single = []
        for each in rhd_data['amplifier_channels']:
            EMG_names_single.append(each['custom_channel_name'])
        # ---------- Since the labels for Pop are not right, these lines change the labels -------- #
        if (self.date_num > 20200301) & (self.date_num < 20201101):
            EMG_names_single = copy.deepcopy(Pop_EMG_names_single)
        # -------- If the items in bad_EMG are numbers, these lines will find out the names -------- #
        if len(bad_EMG) > 0:
            if type(bad_EMG[0]) == int:
                bad_EMG_names = [EMG_names_single[n] for n in bad_EMG]
            elif type(bad_EMG[0]) == str:
                bad_EMG_names = bad_EMG
        else:
            bad_EMG_names = []

        # ---------- Delete paired bad channels -------- #
        bad_paired_channel, bad_EMG_post = delete_paired_bad_channel(
            EMG_names_single, bad_EMG_names)
        bad_paired_channel = sorted(bad_paired_channel, reverse=True)
        for each in bad_paired_channel:
            EMG_names_single.pop(each)
        EMG_single = np.delete(EMG_single, bad_paired_channel, axis=0)
        # ---------- To get paired EMG channels for software diffrence ---------- #
        EMG_names, EMG_index1, EMG_index2 = get_paired_EMG_index(
            EMG_names_single)

        EMG_diff = []
        for i in range(len(EMG_index1)):
            EMG_diff.append(EMG_single[EMG_index1[i], :] -
                            EMG_single[EMG_index2[i], :])

        # ---------- Based on the list in bad_EMG, substitute some channels with single end EMG ---------- #
        if bad_EMG_post:
            bad_idx, paired_idx = find_bad_EMG_index_from_list(
                EMG_names_single, bad_EMG_post)
            for (i, each) in enumerate(bad_EMG_post):
                target_idx = EMG_names.index(each[:-2])
                EMG_diff[target_idx] = EMG_single[paired_idx[i], :]
                print(
                    "For noisy channel %s, use only one single end channel." %
                    (each[:-2]))
                lost_idx = np.where(EMG_diff[target_idx] < -6300)[0]
                if lost_idx.size > 0:
                    EMG_diff[target_idx][lost_idx] = EMG_diff[target_idx][
                        lost_idx[0] - 10]

        # ---------- Apply artifacts rejection on EMG_diff ----------- #
        """
        For all dataset, artifacts rejection is necessary, must be done
        """
        EMG_diff = self.EMG_art_rej(EMG_diff)

        # ---------- Apply notch filter on EMG_diff ---------- #
        if notch == 1:
            print('Applying notch filter.')
            bnotch, anotch = signal.iirnotch(60, 30, self.EMG_fs)
            for (i, each) in enumerate(EMG_diff):
                EMG_diff[i] = signal.filtfilt(bnotch, anotch, each)
        else:
            print('No notch filter is applied.')
        # ---------- Apply comb filter on EMG_diff ----------- #
        """
        For dataset between 2020-06 and 2020-09, a comb filter is necessary
        """
        if comb_filter == 1:
            EMG_diff = self.apply_comb_filter(EMG_diff, self.EMG_fs)
            print('Comb filter has been applied, too.')

        EMG_diff = np.asarray(EMG_diff)
        # ---------- Dealing with sync ----------- #
        sync_line0 = rhd_data['board_dig_in_data'][0]
        sync_line1 = rhd_data['board_dig_in_data'][1]
        d0 = np.where(sync_line0 == True)[0]
        d1 = np.where(sync_line1 == True)[0]
        ds = int(d1[0] - int((d1[0] - d0[0]) * 0.2))
        de = int(d1[-1] + int((d0[-1] - d1[-1]) * 0.2))
        rhd_timeframe = np.arange(de - ds + 1) / self.EMG_fs
        return EMG_names, list(EMG_diff[:, ds:de]), rhd_timeframe
Ejemplo n.º 4
0
 def readInfo(self):
     res = intan.read_data(self.folderName + '/info.rhd')
     self.info = IntanInfos(res)
Ejemplo n.º 5
0
Fs = 30000
fHi = 7500
fLo = 300

## ===== ===== ===== ===== =====
## PREPROCESS DATA
## ===== ===== ===== ===== =====

# Break out into load_data function
rhd_files = glob.glob(data_fldr + "*.rhd")

fname = rhd_files[0]

start_time = time.clock()
print("Loading: " + fname)
fdata = rhd.read_data(fname)
print("Time to load and process: %.3f s" % (time.clock() - start_time))

print("Time: %.3f - %.3f s" %
      (fdata['t_amplifier'][0], fdata['t_amplifier'][-1]))

## Break out into bandpass_filtfilt function
print("Filtering data...")
if fHi >= 0.5 * Fs - 500:
    fHi = 0.5 * Fs - 1000
    print("WARNING! (bandpass): fHi >= 0.5*Fs-500")
    print("Using fHi = %.2f" % fHi)

# Implements Hanning filter
dLen = len(fdata['amplifier_data'][ch_analysis])
if dLen < 387:
Ejemplo n.º 6
0
def get_amp_din_data(file):
    a = load_rhd.read_data(file)
    full_amp_data = a['amplifier_data']
    full_din_data = a['board_dig_in_data']
    return full_amp_data, full_din_data
# -*- coding: utf-8 -*-
"""
Created on Sat Sep  1 23:02:25 2018

@author: xuanm
"""
import nexfile
from load_intan_rhd_format import read_data
filename = "Z:/data/Greyson_17L2/Cerebusdata/20180831/20180831-Greyson_PG_001.rhd"
data = read_data(filename)
EMG_1 = data['amplifier_data'][0]
#%%
w = nexfile.NexWriter(30000)
#w.fileData['FileHeader']['Comment'] = 'this is a comment'
#w.AddNeuron('neuron1', [1, 2, 3, 4])
w.AddContVarWithSingleFragment('EMG1', 0, 2010, EMG_1)
#w.WriteNexFile('C:\\Data\\testFileWrittenInPython.nex')
w.WriteNex5File('C:\\mcode\\python_for_neuroexplorer\\20180831_Greyson_PG_003.nex5', 1)