packages = ['pandas', 'jinja2'] import os import asyncio import pandas as pd import numpy as np # CHECK IF NEEDED from pandas import set_option, read_csv import io import gc import jinja2 ######## Are those form js package ?? ######## from js import document from js import Uint8Array from js import TextDecoder from js import FileReader from js import console ######## Are those form js package ?? ######## ######## Or those from pycript package (module) ?? Nope! Those are from js package above ######## #from pyscript import document #from pyscript import Uint8Array #from pyscript import TextDecoder from pyscript import Element from pyscript import display from pyscript import create_proxy #from pyscript import FileReader ## Nope! #from pyscript import console ## Nope! #### GLOBALS #### data = '' #state = 0 Token = 'Version' header_line = [] # The way it was coded, it's a list, NOT string headerline_index = 0 # -1 for detecting invalid CSV file fileToAnalyse = '' ele_id_title = Element('id_title') ele_accmin = Element('accmin') ele_content = Element('content') ele_rtables = Element('rtables') ele_tcrmax = Element('tcrmax') ## =============== PROCESS CALLBACK FOR FILE INPUT DIALOG ================ async def process_file(event): global headerline_index, fileToAnalyse, data # , state headerline_index = 0 fileList = event.target.files.to_py() #file_buf = filelist.item(0) #print(type(file_buf)) for f in fileList: fileToAnalyse = f.name ##==## print('Input CSV file :' + fileToAnalyse) data = Uint8Array.new(await f.arrayBuffer()) ##==## print('after await') data = TextDecoder.new().decode(data) # display(type(data), target='msg', append=False) i = 0 line_list = [] flag = False data1 = data[0:1000000].splitlines() ## try: for line_list in data1: if flag: break if Token in line_list: header_line = data1[i + 1] flag = True else: i += 1 ## except: if not flag: display("Seems not to be a valid IRAP text file", target='file_msg', append=False) return display("This is a valid IRAP text file", target='file_msg', append=False) headerline_index = data1.index(header_line) # CLEAR TITLE OF RESULT TABLE : ele_rtables.clear() # CLEAR MEMORY SPACES FOR THOSE GUYS (ALBEIT GARBAGE COLLECTOR) ### data = '' # NOT THIS ONE !!! data1 = [] ## f.close() ## THIS FUNCTON BOUNDS THE ACTION OF PRESSING THE FILE SELECT BUTTON TO ITS ## CALLBACK FUNCTION "process_file()" def setup_button_inp_file(): ''' # Create a Python proxy for the callback function # process_file() is your function to process events from FileReader ''' file_event = create_proxy(process_file) # Set the listener to the callback e = document.getElementById("file_input") e.addEventListener("change", file_event, False) ## =============== PROCESS CALLBACK FOR STATISTCS_1 BUTTON ================ def bt_stats1(): global fileToAnalyse global headerline_index, data # , state ##==## print('Inside bt_stats1') # CLEAR TITLE OF RESULT TABLE : #### elem = Element('rtables') ele_rtables.clear() if headerline_index == 0: display('Please, choose a CSV file first.', target='file_msg', append=False) return buff = io.StringIO(data) # <<<<============ SOLUTION !!!!!! columns_to_use = ['participantCode', 'startingRule', 'isTestBlock', 'blockPairNumber', 'currentRule', 'trialNumber', 'trialType', 'timeToFirstAnswer', 'timeToCorrectResponse', 'accuracy'] PC='participantCode'; SRL='startingRule'; ITB='isTestBlock'; BPN='blockPairNumber'; CRL='currentRule'; TN='trialNumber'; TT='trialType'; TFA='timeToFirstAnswer'; TCR='timeToCorrectResponse'; ACU='accuracy' set_option('display.max_rows', None) set_option('display.max_columns', None) ## SETS THE FOALTING FORMAT FOR TWO DECINALS, ALTERNATIVELY USE THE .round(2) ## METHOD FOR THE mean VALUE : pd.options.display.float_format = '{:,.2f}'.format pd.set_option("display.colheader_justify","right") ### ============================================== df = read_csv(buff, sep='\t', header=headerline_index, index_col=False, usecols=columns_to_use, #on_bad_lines='warn', # 'error' , 'warn', 'skip' #error_bad_lines=False, #header=None, engine='python', # 'c' , 'python' , 'pyarrow' ).rename(columns={'participantCode': 'Part Code', 'trialType': 'Trial Type', \ 'timeToCorrectResponse': 'Time Corr Resp', 'currentRule': 'Curr Rule', 'accuracy': 'Accu'}) buff.close() df = df[df[ITB] == bool(1)] try: TCRMAX = int(ele_tcrmax.element.value) #### TCRMAX = int(Element('tcrmax').element.value) except: display("Not a valid float value. Using default.", target='msg', append=False) #el = Element('tcrmax') #el.write('2000') TCRMAX = 2000 df = df.groupby(['Part Code', 'Trial Type'])[['Time Corr Resp']].mean() ### .round(2) df = df[df['Time Corr Resp'] > TCRMAX] # the means per partic per trial type that exceede TCRMAX #display(df, target='content', append = False) #### Element('content').clear() styles = [{'selector':"*", 'props':[ ("font-family" , 'Mono'), ("font-size" , 'px'), ("margin" , "15px auto"), ("border" , "4px solid #bbb")]}, {"selector": "th", "props": [("text-align", "center"), ("background-color", "#F6F8BA")]}, {"selector": "td", "props": [("text-align", "center"), ("padding", "4px 6px")]}, ] df = df.style.set_table_styles(styles) ele_content.clear() #### Element('content').write(df, append=False) ele_content.write(df, append=False) #### elem = Element('id_title') ele_id_title.write(f'Rule Violation: Means, per Participant and per Trial Type of each, considering only Test Blocks, and those that exceeds the first Time to Correct Response for TCRMAM = {TCRMAX}. Try other values.', append=False) def setup_bt_stats1(): ### print('Inside setup_bt_stats1') # Create a Python proxy for the callback function """ This function to process events from the action of pressing bt_stats1, which has an id of "stats1" """ #button1_event = create_proxy(setup_bt_stats1) button1_event = create_proxy(bt_stats1) # Set the listener to the callback e = document.getElementById("stats1") e.addEventListener("click", button1_event, False) ## =============== PROCESS CALLBACK FOR STATISTCS_2 BUTTON ================ def bt_stats2(): global fileToAnalyse global headerline_index, data, ele_id_title, ele_accmin ##==## print('Inside bt_stats2') buff = io.StringIO(data) # <<<<============ SOLUTION !!!!!! ### display(dir(buff), target='msg') if headerline_index == 0: display('Please, choose a CSV file first.', target='file_msg', append=False) return columns_to_use = ['participantCode', 'startingRule', 'isTestBlock', 'blockPairNumber', 'currentRule', 'trialNumber', 'trialType', 'timeToFirstAnswer', 'timeToCorrectResponse', 'accuracy'] PC='participantCode'; SRL='startingRule'; ITB='isTestBlock'; BPN='blockPairNumber'; CRL='currentRule'; TN='trialNumber'; TT='trialType'; TFA='timeToFirstAnswer'; TCR='timeToCorrectResponse'; ACU='accuracy' set_option('display.max_rows', None) set_option('display.max_columns', None) ## SETS THE FOALTING FORMAT FOR TWO DECINALS, ALTERNATIVELY USE THE .round(2) ## METHOD FOR THE mean VALUE : pd.options.display.float_format = '{:,.2f}'.format pd.set_option("display.colheader_justify","right") ## center" ### ============================================== df = read_csv(buff, sep='\t', header=headerline_index, index_col=False, usecols=columns_to_use, #on_bad_lines='warn', # 'error' , 'warn', 'skip' #error_bad_lines=False, #header=None, engine='python', # 'c' , 'python' , 'pyarrow' ).rename(columns={'participantCode': 'Part Code', 'trialType': 'Trial Type', \ 'timeToCorrectResponse': 'Time Corr Resp', 'currentRule': 'Curr Rule', 'accuracy': 'Accu'}) buff.close() df = df[df[ITB] == bool(1)] try: ACCMIN = float((ele_accmin).element.value) except: display("Not a valid float value. Using default.", target='msg', append=False) ACCMIN = 0.8 df = df.groupby(['Part Code', 'Trial Type'])[['Accu']].mean() ### .round(2) df = df[df['Accu'] <= ACCMIN] # the means per partic per trial type that fall bellow ACCMIN styles = [{'selector':"*", 'props':[ ("font-family" , 'Mono'), ("font-size" , 'px'), ("margin" , "15px auto"), ("border" , "4px solid #bbb")]}, {"selector": "th", "props": [("text-align", "center"), ("background-color", "#F6F8BA")]}, {"selector": "td", "props": [("text-align", "center"), ("padding", "4px 6px")]}, ] df = df.style.set_table_styles(styles) ele_content.clear() display(df, target='content', append=False) ######## ele_id_title = Element('id_title') ele_id_title.write(f'Rule Violation: Means of Accuracy, per Participant and per Trial Type of each, considering only Test Blocks, and those that fall bellow the Accuracy Threshold (ACCMIN) for ACCMIN = {ACCMIN}. Try other values.', append=False) ## =============== PROCESS CALLBACK FOR STATISTCS_3 BUTTON ================ def bt_stats3(): global fileToAnalyse global headerline_index, data, ele_id_title, ele_accmin, ele_tcrmax ##==## print('Inside bt_stats3') buff = io.StringIO(data) ### display(dir(buff), target='msg') if headerline_index == 0: display('Please, choose a CSV file first.', target='file_msg', append=False) return columns_to_use = ['participantCode', 'startingRule', 'isTestBlock', 'blockPairNumber', 'currentRule', 'trialNumber', 'trialType', 'timeToFirstAnswer', 'timeToCorrectResponse', 'accuracy'] PC='participantCode'; SRL='startingRule'; ITB='isTestBlock'; BPN='blockPairNumber'; CRL='currentRule'; TN='trialNumber'; TT='trialType'; TFA='timeToFirstAnswer'; TCR='timeToCorrectResponse'; ACU='accuracy' set_option('display.max_rows', None) set_option('display.max_columns', None) ## SETS THE FOALTING FORMAT FOR TWO DECINALS, ALTERNATIVELY USE THE .round(2) ## METHOD FOR THE mean VALUE : pd.options.display.float_format = '{:,.2f}'.format pd.set_option("display.colheader_justify","right") ## center" ### ============================================== df = read_csv(buff, sep='\t', header=headerline_index, index_col=False, usecols=columns_to_use, #on_bad_lines='warn', # 'error' , 'warn', 'skip' #error_bad_lines=False, #header=None, engine='python', # 'c' , 'python' , 'pyarrow' ).rename(columns={'participantCode': 'Part Code', 'trialType': 'Trial Type', \ 'timeToCorrectResponse': 'Time Corr Resp', 'currentRule': 'Curr Rule', 'accuracy': 'Accu'}) buff.close() ####################################################### df = df.astype({ 'Part Code': 'int32', ITB: 'bool', 'Trial Type': 'int32', 'Time Corr Resp': 'float', 'Accu': 'float' }) ####################################################### df = df[df[ITB] == bool(1)] # GRAB AND FILTER TCRMAX : try: TCRMAX = int(ele_tcrmax.element.value) except: display("Not a valid float value. Using default.", target='msg', append=False) TCRMAX = 2000 # GRAB AND FILTER ACCMIN : try: ACCMIN = float((ele_accmin).element.value) except: display("Not a valid float value. Using default.", target='msg', append=False) ACCMIN = 0.8 df = df.groupby(['Part Code', 'Trial Type'])[['Time Corr Resp', 'Accu']].mean() df = df[df['Time Corr Resp'] >= TCRMAX] df = df[df['Accu'] <= ACCMIN] styles = [{'selector':"*", 'props':[ ("font-family" , 'Mono'), ("font-size" , 'px'), ("margin" , "15px auto"), ("border" , "4px solid #bbb")]}, {"selector": "th", "props": [("text-align", "center"), ("background-color", "#F6F8BA")]}, {"selector": "td", "props": [("text-align", "center"), ("padding", "4px 6px")]}, ] df = df.style.set_table_styles(styles) ele_content.clear() display(df, target='content', append=False) ele_id_title.write(f'Rule Violation: Means of Accuracy, per Participant and per Trial Type of each, considering only Test Blocks, and those that fall bellow the Accuracy Threshold (ACCMIN) for ACCMIN = {ACCMIN}. Try other values.', append=False) ## =============== PROCESS CALLBACK FOR STATISTCS_4 BUTTON ================ def bt_stats4(): global fileToAnalyse global headerline_index, data, ele_id_title, ele_accmin, ele_tcrmax buff = io.StringIO(data) if headerline_index == 0: display('Please, choose a CSV file first.', target='file_msg', append=False) return columns_to_use = ['participantCode', 'startingRule', 'isTestBlock', 'blockPairNumber', 'currentRule', 'trialNumber', 'trialType', 'timeToFirstAnswer', 'timeToCorrectResponse', 'accuracy'] PC='participantCode'; SRL='startingRule'; ITB='isTestBlock'; BPN='blockPairNumber'; CRL='currentRule'; TN='trialNumber'; TT='trialType'; TFA='timeToFirstAnswer'; TCR='timeToCorrectResponse'; ACU='accuracy' set_option('display.max_rows', None) set_option('display.max_columns', None) ## SETS THE FOALTING FORMAT FOR TWO DECINALS, ALTERNATIVELY USE THE .round(2) ## METHOD FOR THE mean VALUE : pd.options.display.float_format = '{:,.2f}'.format pd.set_option("display.colheader_justify","right") ## center" ### ============================================== df = read_csv(buff, sep='\t', header=headerline_index, index_col=False, usecols=columns_to_use, #on_bad_lines='warn', # 'error' , 'warn', 'skip' #error_bad_lines=False, #header=None, engine='python', # 'c' , 'python' , 'pyarrow' ).rename(columns={'participantCode': 'Part Code', 'trialType': 'Trial Type', \ 'timeToCorrectResponse': 'Time Corr Resp', 'currentRule': 'Curr Rule', 'accuracy': 'Accu'}) buff.close() ####################################################### df = df.astype({ 'Part Code': 'int32', ITB: 'bool', 'Trial Type': 'int32', 'Time Corr Resp': 'float', 'Accu': 'float' }) ####################################################### # GRAB AND VALIDATE TCRMAX : try: TCRMAX = int(ele_tcrmax.element.value) except: display("Not a valid float value. Using default.", target='msg', append=False) TCRMAX = 2000 # GRAB AND VALIDATE ACCMIN : try: ACCMIN = float((ele_accmin).element.value) except: display("Not a valid float value. Using default.", target='msg', append=False) ACCMIN = 0.8 df = df[df[ITB] == bool(1)] # STEP 1 #df = df[df['Time Corr Resp'] <= 10000] # STEP 2 df = df.groupby(['Part Code', 'Curr Rule', 'Trial Type'])[['Time Corr Resp', 'Accu']].mean() df = df[df['Time Corr Resp'] >= TCRMAX] df = df[df['Accu'] <= ACCMIN] styles = [{'selector':"*", 'props':[ ("font-family" , 'Mono'), ("font-size" , 'px'), ("margin" , "15px auto"), ("border" , "4px solid #bbb")]}, {"selector": "th", "props": [("text-align", "center"), ("background-color", "#F6F8BA")]}, {"selector": "td", "props": [("text-align", "center"), ("padding", "4px 6px")]}, ] df = df.style.set_table_styles(styles) ele_content.clear() display(df, target='content', append=False) ele_id_title.write(f'Rule Violation: Means of Accuracy, per Participant and per Trial Type of each, considering only Test Blocks, and those that fall bellow the Accuracy Threshold (ACCMIN) for ACCMIN = {ACCMIN}. Try other values.', append=False) ## =============== PROCESS CALLBACK FOR STATISTCS_5 BUTTON ================ def bt_stats5(): global fileToAnalyse global headerline_index, data, ele_id_title, ele_accmin, ele_tcrmax buff = io.StringIO(data) if headerline_index == 0: display('Please, choose a CSV file first.', target='file_msg', append=False) return columns_to_use = ['participantCode', 'startingRule', 'isTestBlock', 'blockPairNumber', 'currentRule', 'trialNumber', 'trialType', 'timeToFirstAnswer', 'timeToCorrectResponse', 'accuracy'] PC='participantCode'; SRL='startingRule'; ITB='isTestBlock'; BPN='blockPairNumber'; CRL='currentRule'; TN='trialNumber'; TT='trialType'; TFA='timeToFirstAnswer'; TCR='timeToCorrectResponse'; ACU='accuracy' set_option('display.max_rows', None) set_option('display.max_columns', None) ## SETS THE FOALTING FORMAT FOR TWO DECINALS, ALTERNATIVELY USE THE .round(2) ## METHOD FOR THE FLOAT TYPE VALUES : pd.options.display.float_format = '{:,.5f}'.format pd.set_option("display.colheader_justify","right") ## center" ### ============================================== df = read_csv(buff, sep='\t', header=headerline_index, index_col=False, usecols=columns_to_use, #on_bad_lines='warn', # 'error' , 'warn', 'skip' #error_bad_lines=False, #header=None, engine='python', # 'c' , 'python' , 'pyarrow' ).rename(columns={'participantCode': 'Part Code', 'trialType': 'Trial Type'}) buff.close() ####################################################### df = df.astype({ 'Part Code': 'int64', ITB: 'bool', CRL: 'int64', 'Trial Type': 'int64', TCR: 'float', ACU: 'float' }) ####################################################### #print(df.dtypes) # GRAB AND VALIDATE TCRMAX : try: TCRMAX = int(ele_tcrmax.element.value) except: display("Not a valid float value. Using default.", target='msg', append=False) TCRMAX = 2000 # GRAB AND VALIDATE ACCMIN : try: ACCMIN = float((ele_accmin).element.value) except: display("Not a valid float value. Using default.", target='msg', append=False) ACCMIN = 0.8 df = df[df[ITB] == bool(1)] # STEP 1 (MAYBE NOT NEEDED, CAUSE THE DATA IS ALREADY FILTERED BY THE ASTYPE() ABOVE. df = df[df[TCR] <= 10000] # STEP 2 df = df.groupby(['Part Code', BPN, 'Trial Type']).apply(lambda x: pd.DataFrame({ 'MeanTCR1': [x[TCR].loc[x[CRL] == 1].mean()], 'MeanTCR2': [x[TCR].loc[x[CRL] == 2].mean()], 'Std': [x[TCR].std(ddof=1)] })) # ================================================ df['MeanTCR2_1'] = df['MeanTCR2'] - df['MeanTCR1'] # Note: some often, errors occur here. (recommesded: groupKeys=False) df['DIRAP_Score'] = df['MeanTCR2_1'] / df['Std'] df = df.groupby(['Part Code', 'Trial Type'])['DIRAP_Score'].mean().to_frame('D_Scores') df = df.pivot_table(df, index=['Part Code'], columns=['Trial Type']) styles = [{'selector':"*", 'props':[ ("font-family" , 'Mono'), ("font-size" , 'px'), ("margin" , "15px auto"), ("border" , "4px solid #bbb")]}, {"selector": "th", "props": [("text-align", "center"), ("background-color", "#F6F8BA")]}, {"selector": "td", "props": [("text-align", "center"), ("padding", "4px 6px")]}, ] df = df.style.set_table_styles(styles) ele_content.clear() display(df, target='content', append=False) ele_id_title.write(f'Overall D-Scores per Participant and per Trial Type of each, considering only Test Blocks.', append=False) # JUST IN CASE, TO CLEAN UP THE MEMORY : del(df) gc.collect() ## ======================================================== def setup_bt_stats2(): ### print('Inside setup_bt_stats2') # Create a Python proxy for the callback function ''' This function to process events from the action of pressing bt_stats2, which has an id of "stats2" ''' button2_event = create_proxy(setup_bt_stats2) #button2_event = create_proxy(bt_stats2) # Set the listener to the callback e = document.getElementById("stats2") e.addEventListener("click", button2_event, False) def setup_bt_stats3(): # Create a Python proxy for the callback function ''' This function to process events from the action of pressing bt_stats3, which has an id of "stats3" ''' #button3_event = create_proxy(bt_stats3) button3_event = create_proxy(setup_bt_stats3) # Set the listener to the callback e = document.getElementById("stats3") e.addEventListener("click", button3_event, False) def setup_bt_stats4(): # Create a Python proxy for the callback function ''' This function to process events from the action of pressing bt_stats4, which has an id of "stats4" ''' button4_event = create_proxy(bt_stats4) #button4_event = create_proxy(setup_bt_stats4) # Set the listener to the callback e = document.getElementById("stats4") e.addEventListener("click", button4_event, False) def setup_bt_stats5(): # Create a Python proxy for the callback function ''' This function to process events from the action of pressing bt_stats5, which has an id of "stats5" ''' button5_event = create_proxy(bt_stats5) #button5_event = create_proxy(setup_bt_stats5) # Set the listener to the callback e = document.getElementById("stats5") e.addEventListener("click", button5_event, False) setup_button_inp_file() setup_bt_stats1() setup_bt_stats2() setup_bt_stats3() setup_bt_stats4() setup_bt_stats5()

IRAP Trial-Type Analysis

File Messages:

You can also drag'n'drop a file to the button



Select an IRAP Analysis :
Messages:
Output: