示例#1
0
    def importCsvFile(self):
        self.data_unit = str(self.lineEdit_time_unit.text())
        self.input_data = ExtractSignalFromCSV(str(self.file_path),
                                               unit=self.data_unit)

        self.displaySignalPreview()

        self.label_signal_name.setVisible(True)
        self.lineEdit_signal_name.setVisible(True)
        self.pushButton_visualize.setVisible(True)
        self.pushButton_save_signal.setVisible(True)
        """ DEBUG """
        self.lineEdit_signal_name.setText("mySignal")
示例#2
0
    def importCsvFile(self):
        self.data_unit = str(self.lineEdit_time_unit.text())
        self.input_data = ExtractSignalFromCSV(str(self.file_path), unit = self.data_unit)
        
        self.displaySignalPreview()

        self.label_signal_name.setVisible(True)
        self.lineEdit_signal_name.setVisible(True)
        self.pushButton_visualize.setVisible(True)
        self.pushButton_save_signal.setVisible(True)
        
        """ DEBUG """ 
        self.lineEdit_signal_name.setText("mySignal")
示例#3
0
'''
""" Define signals in pd.dataFrame format """
# preparing the input time series
N = 20             # number of samples
f = 1.0             # sinewave frequency (Hz)
Fs = 200            # sampling frequency (Hz)
n = np.arange(0,N)  # number of samples
# input time series
x = pd.DataFrame({'X':np.sin(2*3.14*f*n/Fs)})
y = pd.DataFrame({'Y':np.sin(2*3.14*2*f*n/Fs)})
'''

"""OR"""
""" Import signal from a .csv file """
filename = 'data_examples/2Persons_Multivariate_Continous_data.csv'
x = ExtractSignalFromCSV(filename, columns = ['Upper body mq'])
y = ExtractSignalFromCSV(filename, columns = ['Upper body mq.1'])
z = ExtractSignalFromCSV(filename, columns = ['Left Hand mq'])
a = ExtractSignalFromCSV(filename, columns = ['Left Hand mq.1'])

'''
"""OR"""
""" Import signal from a .mat file """
filename = 'data_examples/data_example_MAT.mat'
x = ExtractSignalFromMAT(filename, columns_index =[0,2], columns_wanted_names=['Time', 'GlobalBodyActivity0'])
y = ExtractSignalFromMAT(filename, columns_index =[10], columns_wanted_names=['GlobalBodyActivity1'])
'''

signals = [x,y,z,a]

N = signals[0].shape[0]
示例#4
0
)
print(
    "This script computes the Granger Causality test in the spectral domain between two monovariate signals \n"
    + "expressed as Python Pandas DataFrame.")
print(
    "*************************************************************************************"
)
""" Import Utils modules """
from utils.ExtractSignal import ExtractSignalFromCSV
from utils.ResampleAndInterpolate import ResampleAndInterpolate
""" Import wanted module with every parent packages """
import DataFrom2Persons.Monovariate.Continuous.Linear.SpectralGrangerCausality as SGC
""" Import signal from a .csv file """
filename = 'data_examples/data_jouet_4.csv'
print "\nLoading signals from csv files : ", filename, "\n"
x1 = ExtractSignalFromCSV(filename, columns=['x1'])
x2 = ExtractSignalFromCSV(filename, columns=['x2'])
""" Define class attributes """
max_lag = 10  # Define the maximum lag acceptable to estimate autoregressive models
criterion = 'bic'  # Define the criterion to estimate the optimal number of lags to estimate autoregressive models
plot = True  # Authorize the plot of the results
""" Instanciate the class with its attributes """
print("\n")

try:
    sgc = SGC.SpectralGrangerCausality(max_lag=max_lag,
                                       criterion=criterion,
                                       plot=plot)
except TypeError, err:
    print("TypeError in SpectralGrangerCausality constructor : \n" + str(err))
    sys.exit(-1)
示例#5
0
'''
""" Define signals in pd.dataFrame format """
# preparing the input signals
N = 20             # number of samples
f = 1.0             # sinewave frequency (Hz)
Fs = 200            # sampling frequency (Hz)
n = np.arange(0,N)  # number of samples
# input signals
x = pd.DataFrame({'X':np.sin(2*3.14*f*n/Fs)})
y = pd.DataFrame({'Y':np.sin(2*3.14*2*f*n/Fs)})
'''

"""OR"""
""" Import signals from a .csv file """
filename = 'data_examples/2Persons_Multivariate_Continous_data.csv'
x = ExtractSignalFromCSV(filename, columns = ['Upper body mq'])
y = ExtractSignalFromCSV(filename, columns = ['Upper body mq.1'])

'''
"""OR"""
""" Import signals from a .mat file """
filename = 'data_examples/data_example_MAT.mat'
x = ExtractSignalFromMAT(filename, columns_index =[0,2], columns_wanted_names=['Time', 'GlobalBodyActivity0'])
y = ExtractSignalFromMAT(filename, columns_index =[10], columns_wanted_names=['GlobalBodyActivity1'])
'''

signals = [x,y]

N = signals[0].shape[0]
n = np.arange(0,N) 
示例#6
0
from utils.ResampleAndInterpolate import ResampleAndInterpolate
'''
""" Define signals in pd.dataFrame format """
# preparing the input time series
N = 20             # number of samples
f = 1.0             # sinewave frequency (Hz)
Fs = 200            # sampling frequency (Hz)
n = np.arange(0,N)  # number of samples
# input time series
x = pd.DataFrame({'X':np.sin(2*3.14*f*n/Fs)})
y = pd.DataFrame({'Y':np.sin(2*3.14*2*f*n/Fs)})
'''
"""OR"""
""" Import signal from a .csv file """
filename = 'data_examples/2Persons_Monovariate_Continuous_data_2.csv'
x = ExtractSignalFromCSV(filename, columns=['x'], unit='s')
y = ExtractSignalFromCSV(filename, columns=['y'], unit='s')

# Resample and Interpolate data to have constant frequency
x = ResampleAndInterpolate(x, rule='500ms', limit=5)
y = ResampleAndInterpolate(y, rule='500ms', limit=5)
'''
"""OR"""
""" Import signal from a .mat file """
filename = 'data_examples/data_example_MAT.mat'
x = ExtractSignalFromMAT(filename, columns_index =[0,2], columns_wanted_names=['Time', 'GlobalBodyActivity0'])
y = ExtractSignalFromMAT(filename, columns_index =[10], columns_wanted_names=['GlobalBodyActivity1'])
'''
""" Plot input signals """
n = [float(i) / 2 for i in range(x.size)]  # create x axis values
plt.ion()
示例#7
0
import pandas as pd  # Time serie package
import matplotlib.pyplot as plt  # Plotting package
sys.path.insert(0, '../src/')  # To be able to import from parent directory

print("=================================================================")
print("== Testing for Conditional Granger Causality between 5 signals ==")
print("=================================================================")
""" Import Utils modules """
from utils.ExtractSignal import ExtractSignalFromCSV
from utils.ResampleAndInterpolate import ResampleAndInterpolate
""" Import wanted module with every parent packages """
import DataFromManyPersons.Univariate.Continuous.Linear.ConditionalGrangerCausality as CGC
""" Import signal from a .csv file """
filename = 'data_examples/data_jouet_3.csv'
print "\nLoading signals from csv files : ", filename, "\n"
x1 = ExtractSignalFromCSV(filename, columns=['x1'])
x2 = ExtractSignalFromCSV(filename, columns=['x2'])
x3 = ExtractSignalFromCSV(filename, columns=['x3'])
x4 = ExtractSignalFromCSV(filename, columns=['x4'])
x5 = ExtractSignalFromCSV(filename, columns=['x5'])

# Resample and Interpolate data to have constant frequency
x1 = ResampleAndInterpolate(x1, rule='200ms', limit=5)
x2 = ResampleAndInterpolate(x2, rule='200ms', limit=5)
x3 = ResampleAndInterpolate(x3, rule='200ms', limit=5)
x4 = ResampleAndInterpolate(x4, rule='200ms', limit=5)
x5 = ResampleAndInterpolate(x5, rule='200ms', limit=5)
""" Define class attributes """
max_lag = 10  # Define the maximum lag acceptable to estimate autoregressive models
criterion = 'bic'  # Define the criterion to estimate the optimal number of lags to estimate autoregressive models
plot = True  # Authorize the plot of the results
示例#8
0
class AddSignalWindow(QtGui.QMainWindow, Ui_AddSignalWindow):
    
    signalSaved = QtCore.pyqtSignal() #signal emitted when the signal is ready to send
    
    def __init__(self, signals_dict):
        self.signals_dict = signals_dict
        
        super(AddSignalWindow, self).__init__()
        self.setupUi(self)
        self.initUI()
        
        
    def initUI(self):
        #Hide unvisible widgets
        self.radioButton_csv_ELAN_file.setVisible(False)
        self.radioButton_csv_file.setVisible(False)
        self.radioButton_mat_file.setVisible(False)
        self.label_time_unit.setVisible(False)
        self.lineEdit_time_unit.setVisible(False)
        self.pushButton_import_file.setVisible(False)
        self.label_signal_preview.setVisible(False)
        self.tableWidget_signal_preview.setVisible(False)
        self.label_select_variables.setVisible(False)
        self.radioButton_select_all_variables.setVisible(False)
        self.label_signal_name.setVisible(False)
        self.lineEdit_signal_name.setVisible(False)
        self.pushButton_visualize.setVisible(False)
        self.pushButton_save_signal.setVisible(False)
        
        #Define slots
        self.pushButton_browse.clicked.connect(self.browseFiles)
        self.pushButton_import_file.clicked.connect(self.importFile)
        self.lineEdit_file_path.textChanged.connect(self.resetImport)
        self.radioButton_csv_ELAN_file.clicked.connect(self.defineTimeUnit)
        self.radioButton_csv_file.clicked.connect(self.defineTimeUnit)
        self.radioButton_mat_file.clicked.connect(self.defineTimeUnit) 
        self.radioButton_select_all_variables.toggled.connect(self.selectAllVariables)
        self.tableWidget_signal_preview.itemSelectionChanged.connect(self.updateVariableSelection)
        self.pushButton_visualize.clicked.connect(self.visualizeSignal)
        self.pushButton_save_signal.clicked.connect(self.saveSignal)
        
        # Display the window
        self.show()
    
    def browseFiles(self):
        self.file_path = QtGui.QFileDialog.getOpenFileName(None, 'Select a file.', "*.csv;;*.mat")
        self.lineEdit_file_path.setText(self.file_path)

        self.radioButton_csv_file.setVisible(True)
        self.radioButton_csv_ELAN_file.setVisible(True)
        self.radioButton_mat_file.setVisible(True)
        self.pushButton_import_file.setVisible(True)
        
        self.radioButton_csv_ELAN_file.setChecked(False)
        
        
        """ DEBUG """ 
        self.lineEdit_time_unit.setText("ms")
        
        if str(self.lineEdit_file_path.text()).find(".mat") != -1 :
            self.radioButton_csv_file.setChecked(False)
            self.radioButton_mat_file.setChecked(True)
            self.label_time_unit.setVisible(False)
            self.lineEdit_time_unit.setVisible(False)
        else :
            self.radioButton_csv_file.setChecked(True)
            self.radioButton_mat_file.setChecked(False)
            self.label_time_unit.setVisible(True)
            self.lineEdit_time_unit.setVisible(True)
              
        
    def importFile(self):
        if self.lineEdit_file_path.text() == "" :
            mess = QtGui.QMessageBox.warning(self,
                        'Warning Message',
                        "You must enter a file complete path")
            return
        if (os.path.isfile(self.lineEdit_file_path.text()) == False):
            mess = QtGui.QMessageBox.warning(self,
                        'Warning Message',
                        "You must enter an existing file path")
            return
    
        if self.radioButton_csv_file.isChecked() :
            if self.lineEdit_time_unit.text() == "" :
                mess = QtGui.QMessageBox.warning(self,
                            'Warning Message',
                            "You must define a time unit.")
                return
            self.importCsvFile()
            
        elif self.radioButton_csv_ELAN_file.isChecked() :
            self.importFileFromELAN()
            
        elif self.radioButton_mat_file.isChecked() :
            self.importFileFromMat()    
        
            
                
    def defineTimeUnit(self, checked):
        sender = self.sender() #get the current sender of the slot
        if sender == self.radioButton_csv_file:
            if self.radioButton_csv_file.isChecked() :
                self.label_time_unit.setVisible(True)
                self.lineEdit_time_unit.setVisible(True)
            else :
                self.label_time_unit.setVisible(False)
                self.lineEdit_time_unit.setVisible(False)
            self.radioButton_csv_ELAN_file.setChecked(not(checked))
            self.radioButton_mat_file.setChecked(not(checked))
            
        if sender == self.radioButton_csv_ELAN_file:
            if self.radioButton_csv_ELAN_file.isChecked() :
                self.label_time_unit.setVisible(False)
                self.lineEdit_time_unit.setVisible(False)
            else:
                self.label_time_unit.setVisible(True)
                self.lineEdit_time_unit.setVisible(True)
            self.radioButton_csv_file.setChecked(not(checked))    
            self.radioButton_mat_file.setChecked(not(checked))
            
        if sender == self.radioButton_mat_file:
            if self.radioButton_mat_file.isChecked() :
                self.label_time_unit.setVisible(False)
                self.lineEdit_time_unit.setVisible(False)
            else:
                self.label_time_unit.setVisible(True)
                self.lineEdit_time_unit.setVisible(True)
            self.radioButton_csv_file.setChecked(not(checked))
            self.radioButton_csv_ELAN_file.setChecked(not(checked))
        
        self.resetImport()
        
            
    def importCsvFile(self):
        self.data_unit = str(self.lineEdit_time_unit.text())
        self.input_data = ExtractSignalFromCSV(str(self.file_path), unit = self.data_unit)
        
        self.displaySignalPreview()

        self.label_signal_name.setVisible(True)
        self.lineEdit_signal_name.setVisible(True)
        self.pushButton_visualize.setVisible(True)
        self.pushButton_save_signal.setVisible(True)
        
        """ DEBUG """ 
        self.lineEdit_signal_name.setText("mySignal")
        
    
    def importFileFromELAN(self):
        self.importELAN = ImportSignalFromELANWindow(self.lineEdit_file_path.text())
        self.importELAN.ELANimported.connect(self.saveELANInputData)              

    def importFileFromMat(self):
        self.importMAT = ImportSignalFromMATWindow(self.lineEdit_file_path.text())
        self.importMAT.MATimported.connect(self.saveMATInputData)    
    

    def resetImport(self):
        self.label_signal_preview.setVisible(False)
        self.tableWidget_signal_preview.setVisible(False)
        self.label_select_variables.setVisible(False)
        self.radioButton_select_all_variables.setVisible(False)
        self.label_signal_name.setVisible(False)
        self.lineEdit_signal_name.setVisible(False)
        self.pushButton_visualize.setVisible(False)
        self.pushButton_save_signal.setVisible(False)
        
        
    def saveELANInputData(self):
        self.input_data = self.importELAN.input_signal
        self.data_unit = "ms"
        
        self.displaySignalPreview()

        self.label_signal_name.setVisible(True)
        self.lineEdit_signal_name.setVisible(True)
        self.pushButton_visualize.setVisible(True)
        self.pushButton_save_signal.setVisible(True)
        
        
    def saveMATInputData(self):
        self.input_data = self.importMAT.input_signal
        self.data_unit = self.importMAT.my_unit
        
        self.displaySignalPreview()

        self.label_signal_name.setVisible(True)
        self.lineEdit_signal_name.setVisible(True)
        self.pushButton_visualize.setVisible(True)
        self.pushButton_save_signal.setVisible(True)    
    
        
    def displaySignalPreview(self):
        self.tableWidget_signal_preview.setSelectionBehavior(QtGui.QAbstractItemView.SelectColumns)
        
        #Display a signal preview in table widget
        self.tableWidget_signal_preview.setColumnCount(1 + len(self.input_data.columns));
        self.tableWidget_signal_preview.setRowCount(10);
        
        #Display signal columns names
        Qcol_list = QtCore.QStringList()
        Qcol_list.append('Time ('+str(self.data_unit)+')')
        for col_name in self.input_data.columns.values :
            Qcol_list.append(col_name)
        self.tableWidget_signal_preview.setHorizontalHeaderLabels(Qcol_list)
        
        # Display the 10 first lines of the signal
        for row_idx in range(10):
            item=QtGui.QTableWidgetItem()
            item.setText(QtCore.QString(str(self.input_data.index[row_idx])))
            self.tableWidget_signal_preview.setItem(row_idx, 0, item)
            col_idx = 1
            for value in self.input_data.iloc[row_idx,:] :
                item=QtGui.QTableWidgetItem()
                item.setText(QtCore.QString.number(value))
                self.tableWidget_signal_preview.setItem(row_idx, col_idx, item)
                col_idx += 1      
        self.tableWidget_signal_preview.resizeColumnsToContents()
        
        self.label_signal_preview.setVisible(True)
        self.tableWidget_signal_preview.setVisible(True)
        self.label_select_variables.setVisible(True)
        self.radioButton_select_all_variables.setVisible(True)
        
        self.updateVariableSelection()

    
    def selectAllVariables(self, checked):
        if checked:
            self.tableWidget_signal_preview.selectAll()
        else:
            self.tableWidget_signal_preview.clearSelection()
    
    
    def updateVariableSelection(self):
        # Get selected variable in preview Tab widget
        selected_var_idx = [self.tableWidget_signal_preview.selectedIndexes()[i].column()
                            for i in range(len(self.tableWidget_signal_preview.selectedIndexes()))]
        selected_var_idx = sorted(set(selected_var_idx))

        self.selected_variables = []
        for i in selected_var_idx:
            if i != 0: #ignore if TimeIndex is selected
                self.selected_variables.append(self.input_data.columns.values[i-1])

        # Update data_type
        is_categorical = True
        col_idx = 0
        while is_categorical and col_idx < len(selected_var_idx) :
            number_val = len(sorted(set(self.input_data.iloc[:,selected_var_idx[col_idx]-1])))
            if number_val > 2:
                is_categorical = False
            col_idx += 1;
         
        if(is_categorical) : self.my_data_type = 1
        else : self.my_data_type = 0
        
        
    def createMySignal(self):
        self.updateVariableSelection()
        if len(self.tableWidget_signal_preview.selectedIndexes()) == 0:
            mess = QtGui.QMessageBox.warning(self,
                                            'Warning Message',
                                            "You must select at least one variable for the signal.")
            return False
        if str(self.lineEdit_signal_name.text()) == "":
            mess = QtGui.QMessageBox.warning(self,
                                            'Warning Message',
                                            "You must define a signal name.")
            return False
        
        if str(self.lineEdit_signal_name.text()) in self.signals_dict.keys():
            mess = QtGui.QMessageBox.warning(self,
                                            'Warning Message',
                                            "This signal's name already exist.")
            return False
        
        # Keep input data in case of modifications
        self.my_signal_data = self.input_data.copy() 

        # Modify signal to corresponds to selected type
        self.my_signal_data = self.my_signal_data.loc[:,self.selected_variables]
        self.my_signal_data = pd.DataFrame(self.my_signal_data.values, self.my_signal_data.index, self.selected_variables)

        # get signal's type
        if(len(self.my_signal_data.columns) == 1):
            self.my_signal_type = 0 # Monovariate signal
        else:
            self.my_signal_type = 1 # Multivariate signal
        
        # Get signal's name
        self.my_signal_name = str(self.lineEdit_signal_name.text()).strip()
        
        #Create a new Signal instance
        self.my_signal = Signal(self.my_signal_name, self.my_signal_data, self.my_signal_type, self.my_data_type)
        
        return True
                
                
    def visualizeSignal(self):
        # Keep input data in case of modifications
        viz_signal_data = self.input_data.copy()
        if len(self.selected_variables) > 0 : 
            viz_signal_data = viz_signal_data.loc[:,self.selected_variables]
            viz_signal_data = pd.DataFrame(viz_signal_data.values, viz_signal_data.index, self.selected_variables)
            
            # get signal's type
            viz_signal_type = 0 if(len(viz_signal_data.columns) == 1) else 1
            
            viz_signal = Signal(str(self.lineEdit_signal_name.text()), viz_signal_data, viz_signal_type, self.my_data_type)
            viz_signal.plot_signal()
        else :
            mess = QtGui.QMessageBox.warning(self,
                            'Warning Message',
                            "You must define at least one variable to plot.")
            return

        
    def saveSignal(self):
        created = self.createMySignal()
        
        if created : 
            # Open message box to present signals informations
            mess = QtGui.QMessageBox()
            mess.setWindowTitle('New Signal created')
            mess.setText("The signal "+ self.my_signal_name + " has been created")
            str_my_signal_type = "Monovariate" if(self.my_signal_type==0) else "Multivariate"
            str_my_data_type = "Continuous" if(self.my_data_type==0) else "Categorical"
            str_my_variables = self.selected_variables[0]
            for x in range(len(self.selected_variables)-1):
                str_my_variables += ", " + self.selected_variables[x+1]
            mess.setInformativeText("Variables : " +str_my_variables + "\n" +
                                    "Signal type : " + str_my_signal_type + "\n" +
                                    "Data type : " + str_my_data_type+ "\n" +
                                    "\n"+
                                    "Do you want to add the signal ?")
            yesButton = mess.addButton(QtGui.QMessageBox.Yes)
            noButton = mess.addButton(QtGui.QMessageBox.No)
            mess.exec_()
        
            if mess.clickedButton() == yesButton:
                self.signalSaved.emit()  
                
    
    def closeEvent(self, event):
        reply = QtGui.QMessageBox.question(self, 'Message',
            "Do you want to quit?",
            QtGui.QMessageBox.Yes | QtGui.QMessageBox.No, QtGui.QMessageBox.Yes)

        if reply == QtGui.QMessageBox.Yes:
            event.accept() # let the window close
        else:
            event.ignore()
示例#9
0
class AddSignalWindow(QtGui.QMainWindow, Ui_AddSignalWindow):

    signalSaved = QtCore.pyqtSignal(
    )  #signal emitted when the signal is ready to send

    def __init__(self, signals_dict):
        self.signals_dict = signals_dict

        super(AddSignalWindow, self).__init__()
        self.setupUi(self)
        self.initUI()

    def initUI(self):
        #Hide unvisible widgets
        self.radioButton_csv_ELAN_file.setVisible(False)
        self.radioButton_csv_file.setVisible(False)
        self.radioButton_mat_file.setVisible(False)
        self.label_time_unit.setVisible(False)
        self.lineEdit_time_unit.setVisible(False)
        self.pushButton_import_file.setVisible(False)
        self.label_signal_preview.setVisible(False)
        self.tableWidget_signal_preview.setVisible(False)
        self.label_select_variables.setVisible(False)
        self.radioButton_select_all_variables.setVisible(False)
        self.label_signal_name.setVisible(False)
        self.lineEdit_signal_name.setVisible(False)
        self.pushButton_visualize.setVisible(False)
        self.pushButton_save_signal.setVisible(False)

        #Define slots
        self.pushButton_browse.clicked.connect(self.browseFiles)
        self.pushButton_import_file.clicked.connect(self.importFile)
        self.lineEdit_file_path.textChanged.connect(self.resetImport)
        self.radioButton_csv_ELAN_file.clicked.connect(self.defineTimeUnit)
        self.radioButton_csv_file.clicked.connect(self.defineTimeUnit)
        self.radioButton_mat_file.clicked.connect(self.defineTimeUnit)
        self.radioButton_select_all_variables.toggled.connect(
            self.selectAllVariables)
        self.tableWidget_signal_preview.itemSelectionChanged.connect(
            self.updateVariableSelection)
        self.pushButton_visualize.clicked.connect(self.visualizeSignal)
        self.pushButton_save_signal.clicked.connect(self.saveSignal)

        # Display the window
        self.show()

    def browseFiles(self):
        self.file_path = QtGui.QFileDialog.getOpenFileName(
            None, 'Select a file.', "*.csv;;*.mat")
        self.lineEdit_file_path.setText(self.file_path)

        self.radioButton_csv_file.setVisible(True)
        self.radioButton_csv_ELAN_file.setVisible(True)
        self.radioButton_mat_file.setVisible(True)
        self.pushButton_import_file.setVisible(True)

        self.radioButton_csv_ELAN_file.setChecked(False)
        """ DEBUG """
        self.lineEdit_time_unit.setText("ms")

        if str(self.lineEdit_file_path.text()).find(".mat") != -1:
            self.radioButton_csv_file.setChecked(False)
            self.radioButton_mat_file.setChecked(True)
            self.label_time_unit.setVisible(False)
            self.lineEdit_time_unit.setVisible(False)
        else:
            self.radioButton_csv_file.setChecked(True)
            self.radioButton_mat_file.setChecked(False)
            self.label_time_unit.setVisible(True)
            self.lineEdit_time_unit.setVisible(True)

    def importFile(self):
        if self.lineEdit_file_path.text() == "":
            mess = QtGui.QMessageBox.warning(
                self, 'Warning Message', "You must enter a file complete path")
            return
        if (os.path.isfile(self.lineEdit_file_path.text()) == False):
            mess = QtGui.QMessageBox.warning(
                self, 'Warning Message',
                "You must enter an existing file path")
            return

        if self.radioButton_csv_file.isChecked():
            if self.lineEdit_time_unit.text() == "":
                mess = QtGui.QMessageBox.warning(
                    self, 'Warning Message', "You must define a time unit.")
                return
            self.importCsvFile()

        elif self.radioButton_csv_ELAN_file.isChecked():
            self.importFileFromELAN()

        elif self.radioButton_mat_file.isChecked():
            self.importFileFromMat()

    def defineTimeUnit(self, checked):
        sender = self.sender()  #get the current sender of the slot
        if sender == self.radioButton_csv_file:
            if self.radioButton_csv_file.isChecked():
                self.label_time_unit.setVisible(True)
                self.lineEdit_time_unit.setVisible(True)
            else:
                self.label_time_unit.setVisible(False)
                self.lineEdit_time_unit.setVisible(False)
            self.radioButton_csv_ELAN_file.setChecked(not (checked))
            self.radioButton_mat_file.setChecked(not (checked))

        if sender == self.radioButton_csv_ELAN_file:
            if self.radioButton_csv_ELAN_file.isChecked():
                self.label_time_unit.setVisible(False)
                self.lineEdit_time_unit.setVisible(False)
            else:
                self.label_time_unit.setVisible(True)
                self.lineEdit_time_unit.setVisible(True)
            self.radioButton_csv_file.setChecked(not (checked))
            self.radioButton_mat_file.setChecked(not (checked))

        if sender == self.radioButton_mat_file:
            if self.radioButton_mat_file.isChecked():
                self.label_time_unit.setVisible(False)
                self.lineEdit_time_unit.setVisible(False)
            else:
                self.label_time_unit.setVisible(True)
                self.lineEdit_time_unit.setVisible(True)
            self.radioButton_csv_file.setChecked(not (checked))
            self.radioButton_csv_ELAN_file.setChecked(not (checked))

        self.resetImport()

    def importCsvFile(self):
        self.data_unit = str(self.lineEdit_time_unit.text())
        self.input_data = ExtractSignalFromCSV(str(self.file_path),
                                               unit=self.data_unit)

        self.displaySignalPreview()

        self.label_signal_name.setVisible(True)
        self.lineEdit_signal_name.setVisible(True)
        self.pushButton_visualize.setVisible(True)
        self.pushButton_save_signal.setVisible(True)
        """ DEBUG """
        self.lineEdit_signal_name.setText("mySignal")

    def importFileFromELAN(self):
        self.importELAN = ImportSignalFromELANWindow(
            self.lineEdit_file_path.text())
        self.importELAN.ELANimported.connect(self.saveELANInputData)

    def importFileFromMat(self):
        self.importMAT = ImportSignalFromMATWindow(
            self.lineEdit_file_path.text())
        self.importMAT.MATimported.connect(self.saveMATInputData)

    def resetImport(self):
        self.label_signal_preview.setVisible(False)
        self.tableWidget_signal_preview.setVisible(False)
        self.label_select_variables.setVisible(False)
        self.radioButton_select_all_variables.setVisible(False)
        self.label_signal_name.setVisible(False)
        self.lineEdit_signal_name.setVisible(False)
        self.pushButton_visualize.setVisible(False)
        self.pushButton_save_signal.setVisible(False)

    def saveELANInputData(self):
        self.input_data = self.importELAN.input_signal
        self.data_unit = "ms"

        self.displaySignalPreview()

        self.label_signal_name.setVisible(True)
        self.lineEdit_signal_name.setVisible(True)
        self.pushButton_visualize.setVisible(True)
        self.pushButton_save_signal.setVisible(True)

    def saveMATInputData(self):
        self.input_data = self.importMAT.input_signal
        self.data_unit = self.importMAT.my_unit

        self.displaySignalPreview()

        self.label_signal_name.setVisible(True)
        self.lineEdit_signal_name.setVisible(True)
        self.pushButton_visualize.setVisible(True)
        self.pushButton_save_signal.setVisible(True)

    def displaySignalPreview(self):
        self.tableWidget_signal_preview.setSelectionBehavior(
            QtGui.QAbstractItemView.SelectColumns)

        #Display a signal preview in table widget
        self.tableWidget_signal_preview.setColumnCount(
            1 + len(self.input_data.columns))
        self.tableWidget_signal_preview.setRowCount(10)

        #Display signal columns names
        Qcol_list = QtCore.QStringList()
        Qcol_list.append('Time (' + str(self.data_unit) + ')')
        for col_name in self.input_data.columns.values:
            Qcol_list.append(col_name)
        self.tableWidget_signal_preview.setHorizontalHeaderLabels(Qcol_list)

        # Display the 10 first lines of the signal
        for row_idx in range(10):
            item = QtGui.QTableWidgetItem()
            item.setText(QtCore.QString(str(self.input_data.index[row_idx])))
            self.tableWidget_signal_preview.setItem(row_idx, 0, item)
            col_idx = 1
            for value in self.input_data.iloc[row_idx, :]:
                item = QtGui.QTableWidgetItem()
                item.setText(QtCore.QString.number(value))
                self.tableWidget_signal_preview.setItem(row_idx, col_idx, item)
                col_idx += 1
        self.tableWidget_signal_preview.resizeColumnsToContents()

        self.label_signal_preview.setVisible(True)
        self.tableWidget_signal_preview.setVisible(True)
        self.label_select_variables.setVisible(True)
        self.radioButton_select_all_variables.setVisible(True)

        self.updateVariableSelection()

    def selectAllVariables(self, checked):
        if checked:
            self.tableWidget_signal_preview.selectAll()
        else:
            self.tableWidget_signal_preview.clearSelection()

    def updateVariableSelection(self):
        # Get selected variable in preview Tab widget
        selected_var_idx = [
            self.tableWidget_signal_preview.selectedIndexes()[i].column()
            for i in range(
                len(self.tableWidget_signal_preview.selectedIndexes()))
        ]
        selected_var_idx = sorted(set(selected_var_idx))

        self.selected_variables = []
        for i in selected_var_idx:
            if i != 0:  #ignore if TimeIndex is selected
                self.selected_variables.append(
                    self.input_data.columns.values[i - 1])

        # Update data_type
        is_categorical = True
        col_idx = 0
        while is_categorical and col_idx < len(selected_var_idx):
            number_val = len(
                sorted(
                    set(self.input_data.iloc[:,
                                             selected_var_idx[col_idx] - 1])))
            if number_val > 2:
                is_categorical = False
            col_idx += 1

        if (is_categorical): self.my_data_type = 1
        else: self.my_data_type = 0

    def createMySignal(self):
        self.updateVariableSelection()
        if len(self.tableWidget_signal_preview.selectedIndexes()) == 0:
            mess = QtGui.QMessageBox.warning(
                self, 'Warning Message',
                "You must select at least one variable for the signal.")
            return False
        if str(self.lineEdit_signal_name.text()) == "":
            mess = QtGui.QMessageBox.warning(self, 'Warning Message',
                                             "You must define a signal name.")
            return False

        if str(self.lineEdit_signal_name.text()) in self.signals_dict.keys():
            mess = QtGui.QMessageBox.warning(
                self, 'Warning Message', "This signal's name already exist.")
            return False

        # Keep input data in case of modifications
        self.my_signal_data = self.input_data.copy()

        # Modify signal to corresponds to selected type
        self.my_signal_data = self.my_signal_data.loc[:,
                                                      self.selected_variables]
        self.my_signal_data = pd.DataFrame(self.my_signal_data.values,
                                           self.my_signal_data.index,
                                           self.selected_variables)

        # get signal's type
        if (len(self.my_signal_data.columns) == 1):
            self.my_signal_type = 0  # Monovariate signal
        else:
            self.my_signal_type = 1  # Multivariate signal

        # Get signal's name
        self.my_signal_name = str(self.lineEdit_signal_name.text()).strip()

        #Create a new Signal instance
        self.my_signal = Signal(self.my_signal_name, self.my_signal_data,
                                self.my_signal_type, self.my_data_type)

        return True

    def visualizeSignal(self):
        # Keep input data in case of modifications
        viz_signal_data = self.input_data.copy()
        if len(self.selected_variables) > 0:
            viz_signal_data = viz_signal_data.loc[:, self.selected_variables]
            viz_signal_data = pd.DataFrame(viz_signal_data.values,
                                           viz_signal_data.index,
                                           self.selected_variables)

            # get signal's type
            viz_signal_type = 0 if (len(viz_signal_data.columns) == 1) else 1

            viz_signal = Signal(str(self.lineEdit_signal_name.text()),
                                viz_signal_data, viz_signal_type,
                                self.my_data_type)
            viz_signal.plot_signal()
        else:
            mess = QtGui.QMessageBox.warning(
                self, 'Warning Message',
                "You must define at least one variable to plot.")
            return

    def saveSignal(self):
        created = self.createMySignal()

        if created:
            # Open message box to present signals informations
            mess = QtGui.QMessageBox()
            mess.setWindowTitle('New Signal created')
            mess.setText("The signal " + self.my_signal_name +
                         " has been created")
            str_my_signal_type = "Monovariate" if (self.my_signal_type
                                                   == 0) else "Multivariate"
            str_my_data_type = "Continuous" if (self.my_data_type
                                                == 0) else "Categorical"
            str_my_variables = self.selected_variables[0]
            for x in range(len(self.selected_variables) - 1):
                str_my_variables += ", " + self.selected_variables[x + 1]
            mess.setInformativeText("Variables : " + str_my_variables + "\n" +
                                    "Signal type : " + str_my_signal_type +
                                    "\n" + "Data type : " + str_my_data_type +
                                    "\n" + "\n" +
                                    "Do you want to add the signal ?")
            yesButton = mess.addButton(QtGui.QMessageBox.Yes)
            noButton = mess.addButton(QtGui.QMessageBox.No)
            mess.exec_()

            if mess.clickedButton() == yesButton:
                self.signalSaved.emit()

    def closeEvent(self, event):
        reply = QtGui.QMessageBox.question(
            self, 'Message', "Do you want to quit?",
            QtGui.QMessageBox.Yes | QtGui.QMessageBox.No,
            QtGui.QMessageBox.Yes)

        if reply == QtGui.QMessageBox.Yes:
            event.accept()  # let the window close
        else:
            event.ignore()
示例#10
0
#input signals
print("\n")
print("Generating the input signals...")

N=26

x = pd.DataFrame([0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,1,0,0],np.arange(0,N))
y = pd.DataFrame([0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0],np.arange(0,N))

'''
"""OR"""
""" Import signals from a .csv file """
#Data from files
filename = 'data_examples/2Persons_Monovariate_Categorical_data.csv'

x = ExtractSignalFromCSV(filename, columns=['0'])
y = ExtractSignalFromCSV(filename, columns=['1'])

s = np.arange(0, x.shape[0])

plt.ion()
f, axarr = plt.subplots(2, sharex=True)
axarr[0].set_title('Input signals')
axarr[0].set_xlabel('Samples')
axarr[1].set_xlabel('Samples')
axarr[0].stem(s, x, label="x")
axarr[1].stem(s, y, label="y")
axarr[0].legend(loc='best')
axarr[1].legend(loc='best')
""" Define class attributes of the wanted method """
atype = 'tot'  # algorithm to be used to compute Q and q
示例#11
0
print(
    "*********************************************************************************************************************************"
)
print(
    "This scripts computes CCA between two multivariate signals and study it\'s maximal correlation, i.e. the maximal synchrony between them.\n"
    "The two multivariate signals contain features extracted from an audiovisual recording in which a person pronounces the voyels \'a\', \'o\', and \'e\'\n"
    "For the face we extracted the following action unit (AU): AU25 and AU26"
    "For the voice we extracted RMS and roughness\n"
    "Synchrony is expected to be maximised in a neighborhood of 0\n")
print(
    "**********************************************************************************************************************************"
)
"""Import wanted module with every parent packages"""
import DataFrom2Persons.Multivariate.Continuous.Linear.CCA as CCA

x = ExtractSignalFromCSV('../src/samples/CCAVoicedata.csv')
y = ExtractSignalFromCSV('../src/samples/CCAFilteredAU.csv')

"removing headers"
x = x.values
y = y.values
x0 = x[:, 0]
x1 = x[:, 1]
y0 = y[:, 0]
y1 = y[:, 1]

t = np.arange(0, x.shape[0])
""" Plot input signals """
plt.ion()
f, axarr = plt.subplots(4, sharex=True)
axarr[0].set_title('Input signals')