Exemplo n.º 1
0
	def extract_words(self, content_word = True, epoch_type = 'epoch', dirty = False):
		if not hasattr(self,'raw'): 
			if dirty: self.load_eeg_data(apply_ica = False)
			else: self.load_eeg_data()
		if not hasattr(self,'raw') or self.raw == 0: 
			print('could not load raw, doing nothing')
			return
		if not hasattr(self,'data'): 
			if epoch_type in ['epochn400','n400','baseline_n400']: 
				keep_channels = utils.n400_channel_set()
			elif epoch_type in ['epochpmn','pmn','epoch']:
				keep_channels = utils.pmn_channel_set()
			else: raise ValueError('unk epoch type:',epoch_type)
			self.data,self.ch,self.rm_ch= utils.raw2np(self.raw,keep_channels= keep_channels)
		self.extracted_words,self.extracted_eeg, self.word_indices = [], [], []
		self.epoch_type = epoch_type
		for i,w in enumerate(self.words):
			if not dirty:
				if not w.usable or not hasattr(w,'pos') or not hasattr(w,'ppl'): continue
				if content_word and not w.pos.content_word: continue
			wd = utils.extract_word_eeg_data(self.data,w, epoch_type = epoch_type)
			if type(wd) == np.ndarray:
				self.extracted_words.append(w)
				self.extracted_eeg.append(wd)
				self.word_indices.append(i)
Exemplo n.º 2
0
def plot(n_dict, v_dict, gate='190', identifier='cross_entropy1'):
    '''plots pmn epochs per channel, not yet tested (reconstructed).
	'''
    co, cod = cap_order()
    values = {}
    nd, vd = load_vn_dicts(gate, identifier)
    x = np.arange(-300, 1000)

    for k in vd.keys():
        values[k] = vd[k] / nd[k]

    pmn_ch = utils.pmn_channel_set()
    for ch in pmn_ch:
        rows, cols, index = cod[ch]
        plt.subplot(rows, cols, index)
        plt.plot(x,
                 values['low_' + ch] - np.mean(values['low_' + ch][:300]),
                 color='blue')
        plt.plot(x,
                 values['mid_' + ch] - np.mean(values['mid_' + ch][:300]),
                 color='orange')
        plt.plot(x,
                 values['high_' + ch] - np.mean(values['high_' + ch][:300]),
                 color='red')
        plt.ylim(0.4, -0.4)
        plt.grid()
        plt.axis('off')
        plt.legend(('low-' + ch, 'mid-' + ch, 'high-' + ch),
                   fontsize='x-small')
        plt.axhline(linewidth=1, color='black')
        plt.axvline(linewidth=1, linestyle='-', color='black')
        plt.axvline(150, color='tomato', linewidth=1, linestyle='--')
        plt.axvline(350, color='tomato', linewidth=1, linestyle='--')
Exemplo n.º 3
0
def pmn_dataword2eeg(f, w2e=None):
    '''matches eeg data to the pmn dataword and extracts eeg averages for baseline and pmn.
	f 		filename of the pmn_dataword
	function is called from make_dataset_gate
	'''
    if w2e == None: w2e = make_word2eeg_dict()
    pmn_ch = utils.pmn_channel_set()
    pmnd = open(f).read().rstrip('\n').split('\t')
    word_number = f2word_number(f)
    failed = []
    ch_name2d = {}
    output = []
    header = load_header()
    header = ['pp'] + header
    for ch in pmn_ch:
        header.extend([ch + '_baseline', ch + '_pmn'])
    for i in range(1, 49):
        k = str(i) + '_' + word_number
        if k not in w2e.keys():
            failed.append(k)
            continue
        fnp = w2e[k]
        if not check_match(fnp, pmnd): continue
        temp = ['pp' + str(i)] + pmnd[:]
        for ch in pmn_ch:
            chd = load_eeg_ch(fnp, channel=ch)
            if type(chd) == str: temp.extend(['NA', 'NA'])
            else:
                temp.append(average(chd, 'baseline'))
                temp.append(average(chd, 'pmn'))
        output.append('\t'.join(map(str, temp)))
    return output, '\t'.join(header), failed
Exemplo n.º 4
0
def pmn_dataword2ploteeg(f,
                         content_word,
                         w2e=None,
                         n_dict={},
                         v_dict={},
                         failed=[],
                         only_function=False):
    '''extract eeg data for a specific pmn_dataword.
	function is called from make_plot_gate
	'''
    if w2e == None: w2e = make_word2eeg_dict()
    pmn_ch = utils.pmn_channel_set()
    pmnd = open(f).read().rstrip('\n').split('\t')
    header = load_header()
    if only_function and content_word and pmnd[header.index(
            'content_word')] == 'True':
        return n_dict, v_dict, failed
    elif content_word and pmnd[header.index('content_word')] == 'False':
        return n_dict, v_dict, failed
    value = float(pmnd[header.index('cross_entropy')])
    if value < 0.4: value_type = 'low'
    elif 2.7 > value > 0.4: value_type = 'mid'
    else: value_type = 'high'

    word_number = f2word_number(f)

    for i in range(1, 49):
        k = str(i) + '_' + word_number
        if k not in w2e.keys():
            failed.append(k)
            continue
        fnp = w2e[k]
        fch = fnp.replace('.npy', '.ch')
        d = np.load(fnp)
        channels = open(fch).read().split('\n')
        for ch in pmn_ch:
            k = value_type + '_' + ch
            if k not in n_dict:
                n_dict[k] = 0
                v_dict[k] = np.zeros(1300)
            # chd = load_eeg_ch(fnp,channel=ch)
            chd = extract_channel(d, channels, ch)
            if type(chd) == str: continue
            else:
                n_dict[k] += 1
                v_dict[k] += chd

    return n_dict, v_dict, failed
Exemplo n.º 5
0
def pmn_all(content_word=False, w2e=None, only_function=False):
    '''Create a plot dict for all gates.'''
    if w2e == None: w2e = make_word2eeg_dict()
    gates = [
        str(g) for g in range(110, 660, 20)
        if len(glob.glob(path.pmn_datasets + str(g) + '/WORDS/*')) == 49019
    ]
    values = 'ud_entropy,entropy,cross_entropy,surprisal,ud_surprisal,cow_index_word,ud_index_word'.split(
        ',')
    pmn_ch = utils.pmn_channel_set()
    pmnd = open(f).read().rstrip('\n').split('\t')
    header = load_header()
    if only_function and content_word and pmnd[header.index(
            'content_word')] == 'True':
        return n_dict, v_dict, failed
    elif content_word and pmnd[header.index('content_word')] == 'False':
        return n_dict, v_dict, failed
    value = float(pmnd[header.index('cross_entropy')])
    if value < 1: value_type = 'low'
    elif 2.8 > value > 1: value_type = 'mid'
    else: value_type = 'high'

    word_number = f2word_number(f)

    for i in range(1, 49):
        k = str(i) + '_' + word_number
        if k not in w2e.keys():
            failed.append(k)
            continue
        fnp = w2e[k]
        fch = fnp.replace('.npy', '.ch')
        d = np.load(fnp)
        channels = open(fch).read().split('\n')
        for ch in pmn_ch:
            k = value_type + '_' + ch
            if k not in n_dict:
                n_dict[k] = 0
                v_dict[k] = np.zeros(1300)
            # chd = load_eeg_ch(fnp,channel=ch)
            chd = extract_channel(d, channels, ch)
            if type(chd) == str: continue
            else:
                n_dict[k] += 1
                v_dict[k] += chd
    return n_dict, v_dict, failed
Exemplo n.º 6
0
def plot_article(gate='190', identifier='cross_entropy1'):
    '''plots pmn epochs per channel, not yet tested (reconstructed).
	'''
    co, cod = cap_order()
    values = {}
    nd, vd = load_vn_dicts(gate, identifier)
    x = np.arange(-300, 1000)

    for k in vd.keys():
        values[k] = vd[k] / nd[k]

    channels = 'F7,F8,FC5,FC6,T7,T8'.split(',')
    rows, cols = 3, 2
    pmn_ch = utils.pmn_channel_set()
    for ch in channels:
        index = channels.index(ch) + 1
        plt.subplot(rows, cols, index)
        plt.plot(x,
                 values['low_' + ch] - np.mean(values['low_' + ch][:300]),
                 color='blue')
        plt.plot(x,
                 values['mid_' + ch] - np.mean(values['mid_' + ch][:300]),
                 color='orange')
        plt.plot(x,
                 values['high_' + ch] - np.mean(values['high_' + ch][:300]),
                 color='red')
        plt.ylim(0.35, -0.35)
        plt.axis('off')
        plt.grid()
        plt.annotate(ch, xy=(-350, -0.2))
        if ch == 'T7':
            plt.annotate('-300', xy=(-350, 0.13))
            plt.annotate('1000', xy=(900, 0.13))
            plt.annotate('-0.3', xy=(-180, -0.3))
            plt.annotate(' 0.3', xy=(-180, 0.35))
            plt.legend(('low', 'mid', 'high'),
                       bbox_to_anchor=(1, 1.3),
                       fontsize='small',
                       loc=1)
        plt.axhline(linewidth=1, color='black')
        plt.axvline(linewidth=1, linestyle='-', color='black')
        plt.axvline(150, color='tomato', linewidth=1, linestyle='--')
        plt.axvline(350, color='tomato', linewidth=1, linestyle='--')