<jupyter_start><jupyter_text>Define classes and how many trials per class<jupyter_code>C_OVR  = [0,1] #MI classes, [0,1,2,3] for left hand, right hand, feet, tongue
_classes  = C_OVR*10 #*trials per MI class
random.shuffle(_classes) #randomize sequence of MI classes


fileroot = 'E:\\Diamond\\own_expo\\'
filewrite = open(fileroot + 'record.txt','w')
filewrite.write('')
filewrite.close()

file_cross = open(fileroot + 'cross_sign.txt','w')
file_cross.write('0')
file_cross.close()

endgame = open(fileroot + 'endgame.txt','w')
endgame.write('0')
endgame.close()


filewrite = open(fileroot + 'record.txt','a')

plt.ioff()

end = 0
%matplotlib auto
plt.ioff()

n = 0
reso = 0.001
reso1 = 0.2
pltpause = 0.05

#########################  Connect to Openvibe aquisition server (AS) ########################################################
# host and port of tcp tagging server
HOST = '127.0.0.1' #local machine address
PORT = 15361 #port to connect to AS

# transform a value into an array of byte values in little-endian order.
def to_byte(value, length):
    for x in range(length):
        yield value%256
        value//=256
        
# connect 
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))

#padding of zeros to keep length of tag consistant
padding=[0]*8

#############################################################################################################################

fig = plt.figure()
ax = fig.add_subplot(1,1,1)

fig.canvas.draw()
img = mpimg.imread('E:\\Diamond\\cues\\black.png')
ax.imshow(img)
fig.canvas.draw()
plt.pause(pltpause)


t0 = datetime.datetime.now()
filewrite.write('rest,' + str(t0) + '\n')

cross = t0 + datetime.timedelta(0,randint(0,5)/10 + 6)
cue = cross + datetime.timedelta(0,1)
rest = cue + datetime.timedelta(0,4)

cross_exed = 0
cue_exed = 0
rest_exed = 0


while n < len(_classes):

    d_cross = (datetime.datetime.now() - cross).total_seconds()
    if cross_exed == 0 and (abs(d_cross) <= reso or d_cross>=reso):
    #if cross_exed == 0 and np.abs(cross - datetime.datetime.now()).total_seconds() < reso:
        
        img = mpimg.imread('E:\\Diamond\\cues\\fixation.png')
        ax.clear()
        ax.imshow(img)
        fig.canvas.draw()
        plt.pause(pltpause)
        filewrite.write('cross,' + str(datetime.datetime.now()) + '\n')
        
        file_cross = open(fileroot + 'cross_sign.txt','w')
        file_cross.write('1')
        file_cross.close()
        
        print ('cross', datetime.datetime.now())
        print ('_classes', _classes[n])
        
        cross_exed = 1
        
    """
    elif cross_exed == 0 and (datetime.datetime.now()-cross).total_seconds() < 0.2 and (datetime.datetime.now()-cross).total_seconds() > reso:
        ax.clear()
        img = mpimg.imread('E:\\Diamond\\cues\\fixation.png')
        ax.imshow(img)
        fig.canvas.draw()
        plt.pause(pltpause)
        filewrite.write('cross,' + str(datetime.datetime.now()) + '\n')
        print ('cross1', datetime.datetime.now())
        print ('_classes', _classes[n])
        cross_exed = 1
    
        file_cross = open(fileroot + 'cross_sign.txt','w')
        file_cross.write('1')
        file_cross.close()
    """
    d_cue = (datetime.datetime.now() - cue).total_seconds()
    #if cue_exed == 0 and np.abs(cue - datetime.datetime.now()).total_seconds() < reso:
    if  cue_exed == 0 and (abs(d_cue) <= reso or d_cue>=reso):
        if _classes[n] == 0:
            img = mpimg.imread('E:\\Diamond\\cues\\left_hand.png')
            EVENT_ID = 0x441 #LEFT HAND (1089) EVENT_IDs are used to tag eeg streams in openvibe, IDs are pre-defined at http://openvibe.inria.fr/stimulation-codes/
        elif _classes[n] == 1:
            img = mpimg.imread('E:\\Diamond\\cues\\right_hand.png')
            EVENT_ID = 0x442 #RIGHT HAND (1090)
        elif _classes[n] == 2:
            img = mpimg.imread('E:\\Diamond\\cues\\feet.png')
            EVENT_ID = 0x303 #FOOT (FEET) (771)
        elif _classes[n] == 3:
            img = mpimg.imread('E:\\Diamond\\cues\\tongue.jpg')
            EVENT_ID = 0x304 #TONGUE (772)
        
        
        
        ax.clear()
        ax.imshow(img)
        fig.canvas.draw()
        plt.pause(pltpause)
        
        #same time tag for record.txt and openvibe eeg streams
        time_datetime = datetime.datetime.now()
        time_unix = time.time()
        
        filewrite.write('cue,' + str(_classes[n]) + ',' + str(time_datetime) + '\n')
        
        #event_id to byte format
        event_id=list(to_byte(EVENT_ID, 8))
        # timestamp can be either the posix time in ms, or 0 to let the acquisition server timestamp the tag itself.
        timestamp=list(to_byte(int(time_unix*1000), 8))
        #send tag to openvibe, tag is padding + event_id + timestamp in unix time, in uint64 format
        s.sendall(bytearray(padding+event_id+timestamp))
        
        print ('cue', datetime.datetime.now())
        print ('_classes', _classes[n])
        
        
        cue_exed = 1   

    """
    elif cue_exed == 0 and (datetime.datetime.now()-cue).total_seconds() < 0.2 and (datetime.datetime.now()-cue).total_seconds() > reso:
        if _classes[n] == 0:
            img = mpimg.imread('E:\\Diamond\\cues\\left_hand.png')
        elif _classes[n] == 1:
            img = mpimg.imread('E:\\Diamond\\cues\\right_hand.png')
        elif _classes[n] == 2:
            img = mpimg.imread('E:\\Diamond\\cues\\feet.png')
        elif _classes[n] == 3:
            img = mpimg.imread('E:\\Diamond\\cues\\tongue.jpg')
        ax.clear()
        ax.imshow(img)
        fig.canvas.draw()
        plt.pause(pltpause)        
        filewrite.write('cue,' + str(_classes[n]) + ',' + str(datetime.datetime.now()) + '\n')

        print ('cue1', datetime.datetime.now())
        print ('_classes', _classes[n])
        cue_exed = 1
    """
    
    d_rest = (datetime.datetime.now() - rest).total_seconds()
    #if rest_exed == 0 and np.abs(rest - datetime.datetime.now()).total_seconds() < reso:
    if rest_exed == 0 and (abs(d_rest) <= reso or d_rest>=reso):
        img = mpimg.imread('E:\\Diamond\\cues\\black.png')
        ax.clear()
        ax.imshow(img)
        fig.canvas.draw()
        plt.pause(pltpause)
        filewrite.write('rest,' + str(datetime.datetime.now()) + '\n')
   
        print ('rest', datetime.datetime.now())
        print ('_classes', _classes[n])
        rest_exed = 1
        
    """
    elif rest_exed == 0 and (datetime.datetime.now()-rest).total_seconds() < 0.2 and (datetime.datetime.now()-rest).total_seconds() > reso:
        img = mpimg.imread('E:\\Diamond\\cues\\black.png')
        ax.clear()
        ax.imshow(img)
        fig.canvas.draw()
        plt.pause(pltpause)
        filewrite.write('rest,' + str(datetime.datetime.now()) + '\n')
        
        print ('rest1', datetime.datetime.now())
        print ('_classes', _classes[n])
        rest_exed = 1
    """    
    if cross_exed == 1 and cue_exed==1 and rest_exed == 1:
        cross = rest + datetime.timedelta(0,randint(0,5)/10 + 6)
        cue = cross + datetime.timedelta(0,1)
        rest = cue + datetime.timedelta(0,4)
        
        cross_exed = 0
        cue_exed = 0
        rest_exed = 0
        
       
        n = n +1
        print (n)
        
        
filewrite.close()
s.close()

end = 1
if end == 1:
    endgame = open(fileroot + 'endgame.txt','w')
    endgame.write('1')
    endgame.close()
plt.ion()
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
fig.canvas.draw()
t0 = datetime.datetime.now()
img = mpimg.imread('E:\\Diamond\\cues\\feet.png')
ax.imshow(img)
fig.show()

plotted = 0
while (datetime.datetime.now()-t0).total_seconds() < 5:
    if plotted == 0 and (datetime.datetime.now()-t0).total_seconds() > 2:
        ax.clear()
        img = mpimg.imread('E:\\Diamond\\cues\\feet.png')
        print('shit')
        ax.imshow(img)
        plotted = 1
        fig.canvas.draw()


#plt.ion()

fig = plt.figure()
ax = fig.add_subplot(1,1,1)
fig.canvas.draw()


img1 = mpimg.imread('E:\\Diamond\\cues\\feet.png')
img2 = mpimg.imread('E:\\Diamond\\cues\\black.png')


ax.imshow(img1)
fig.canvas.draw()

plt.pause(0.1)

ax.clear()
ax.imshow(img2)
fig.canvas.draw()
%matplotlib auto
%matplotlib
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
img = mpimg.imread('E:\\Diamond\\cues\\feet.png')
ax.imshow(img)
fig.show()
img
_classes
import matplotlib.image as mpimg
img = mpimg.imread('E:\\Diamond\\cues\\fixation.png')
plt.figure(1)

plt.imshow(img)

plt.clf()
img = mpimg.imread('E:\\Diamond\\cues\\feet.png')
plt.imshow(img)


rest
timer = threading.Timer(10000, print('cue'))
timer.start()
cue
t0
np.abs((datetime.datetime.now() - (datetime.datetime.now()+datetime.timedelta(0,4))).total_seconds())
x=datetime.datetime.today()
y=x.replace(day=x.day+1, hour=1, minute=0, second=0, microsecond=0)
x
y
t0 + datetime.timedelta(0,60)
t0
t0 = datetime.datetime.now()
dt = (datetime.datetime.now() - t0)
while dt.total_seconds()  <= 5:
    dt = (datetime.datetime.now() - t0)
print (dt.total_seconds(), datetime.datetime.now())
t0
t0.<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.18408941485864563, 'fraction_numerical': 0.0220249835634451, 'mean_word_length': 2.675795408779702, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 1, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '2987', 'license': 'BSD-2-Clause', 'n_tokens_mistral': 3408, 'n_tokens_neox': 3230, 'n_words': 632, 'path': 'data_collect/eeg_data_collect_tcp_tag_openvibe.ipynb', 'repo_name': 's151202/EEG_data_streaming', 'size': '9126'}