Beispiel #1
0
class Main(QMainWindow, Ui_MainWindow):
	def __init__(self, ):
		super(Main, self).__init__()
		self.setupUi(self)
		self.fig_dict = {}

		self.mplfigs.itemClicked.connect(self.change_fig)


	def change_fig(self, item):
		text = item.text()
		self.rm_mpl()
		self.add_mpl(self.fig_dict[text])


	def add_mpl(self, fig):
		self.canvas = FigureCanvas(fig)
		self.mplvl.addWidget(self.canvas)
		self.canvas.draw()

		self.toolbar = NavigationToolbar(self.canvas, self, coordinates=True)
		self.addToolBar(self.toolbar)


	def rm_mpl(self, ):
		self.mplvl.removeWidget(self.canvas)
		self.canvas.close()
		self.mplvl.removeWidget(self.toolbar)
		self.toolbar.close()


	def add_fig(self, name, fig):
		self.fig_dict[name] = fig
		self.mplfigs.addItem(name)
Beispiel #2
0
def plot_raw_dataset_um(
        points_um,
        savedir='',
        title='images',
        grid=(3, 3),
        figsize=(8, 8),
        size=0.1,
        dpi=300,
        invert_yaxis=True,
):
    '''
    Plot the mRNA spots for each gene.
    '''

    print('Plotting mRNA spots of selected genes...')
    if not os.path.exists(savedir):
        os.makedirs(savedir)

    genes_per_plot = grid[0] * grid[1]
    num_plots, remain = divmod(len(points_um), genes_per_plot)

    if remain != 0:
        num_plots += 1

    gene_list = list(points_um.keys())
    gene_idx = 0

    for plot_num in range(num_plots):
        fig = Figure(figsize=figsize, dpi=dpi)
        canvas = FigCanvas(fig)
        fig.set_canvas(canvas)

        for gridpos in range(genes_per_plot):
            if gene_idx == len(points_um):
                break
            ax = fig.add_subplot(grid[0], grid[1], gridpos + 1)

            ax.scatter(
                points_um[gene_list[gene_idx]][:, 1],
                points_um[gene_list[gene_idx]][:, 0],
                s=size,
            )
            ax.set_aspect('equal')
            ax.set_title(gene_list[gene_idx])

            if invert_yaxis:
                ax.invert_yaxis()
            gene_idx += 1

        fig.suptitle(title + f'\n {plot_num + 1} of {num_plots}')
        fig.tight_layout(rect=(0, 0, 1, .94))

        savename = (
            f'raw_image_{plot_num+1}_{datetime.now().strftime("%Y%m%d_%H%M%S")}.png'
        )
        fig.savefig(os.path.join(savedir, savename), dpi=dpi)

        canvas.close()
        fig.clear()
        plt.close()
Beispiel #3
0
class Main(Ui_MainWindow, QMainWindow):
    def __init__(self, ):
        super(Main, self).__init__()
        self.setupUi(self)
        self.fig_dict = {}

        self.L1.itemClicked.connect(self.changefig)

        # self.chk1.setVisible(False)
        # self.chk1.setChecked(True)
        self.setWindowIcon(QtGui.QIcon('ipco-logo.png'))

    def changefig(self, item):
        text = item.text()
        self.rmmpl()
        self.addmpl(self.fig_dict[text])

    def addmpl(self, fig):
        self.canvas = FigureCanvas(fig)
        self.mplvl.addWidget(self.canvas)
        self.canvas.draw()
        self.toolbar = NavigationToolbar(self.canvas, self, coordinates=True)
        self.mplvl.addWidget(self.toolbar)
        self.rmmpl()

    def rmmpl(self, ):
        self.mplvl.removeWidget(self.canvas)
        self.canvas.close()
        self.mplvl.removeWidget(self.toolbar)
        self.toolbar.close()

    def addfig(self, name, fig):
        self.fig_dict[name] = fig
        self.L1.addItem(name)
class EDXmain(QtWidgets.QWidget):
    ''' Interface to get a list of points from an existing plot '''

    # http://blog.rcnelson.com/building-a-matplotlib-gui-with-qt-designer-part-1/
    def __init__(self, ):
        super(EDXmain, self).__init__()  # gets inherited methods of this class
        self.initUI()
        self.fig_dict = {}  # list of active figures
        # click on item in Qlistwidget triggers change of figures
        self.mplfigs.itemClicked.connect(self.changefig)
        # add select data subset button here

    def initUI(self):
        self.setGeometry(600, 300, 400, 200)
        self.setWindowTitle('EDX main window')
        self.show()

    def addmpl(self, fig):
        ''' adds matplotlib plot to qt container'''
        self.canvas = FigureCanvas(fig)  # fig canvas widget
        # mplvl is a QVBoxLayout instance with addWidget method
        self.mplvl.addWidget(
            self.canvas)  # mplvl is layout name within container in .ui window
        # could also be directly added to mplwindow container but then scales differently
        self.canvas.draw()
        self.toolbar = NavigationToolbar(self.canvas,
                                         self.mplwindow,
                                         coordinates=True)
        self.mplvl.addWidget(self.toolbar)  # toolbar added below passed plot
        self.canvas.mpl_connect('key_press_event', self.on_key_press)

    def rmmpl(self, ):
        ''' remove existing plots  '''
        self.mplvl.removeWidget(self.canvas)
        self.canvas.close()
        self.mplvl.removeWidget(self.toolbar)
        self.toolbar.close()

    def addfig(self, name, fig):
        ''' Adds name, underlying fig to dictionary and displays in window '''
        self.fig_dict[name] = fig
        self.mplfigs.addItem(name)  # adds name to QlistWidget

    def changefig(self, item):
        text = item.text()
        self.rmmpl()  # removes existing
        self.addmpl(self.fig_dict[text])

    def on_key_press(self, event):
        ''' get coords on plot '''
        ix, iy = event.xdata, event.ydata
        print('Coords are: ', str(ix), str(iy))
class Main(QtWidgets.QMainWindow, Ui_MainWindow):
    def __init__(self):
        QtWidgets.QMainWindow.__init__(self)
        Ui_MainWindow.__init__(self)
        self.setupUi(self)
        self.plotField.clicked.connect(self.plotfield)

        #Notice that an empty figure instance was added to our plotting window in the initialization method.
        #This is necessary because our first call to changefig will try to remove a previously displayed figure,
        #which will throw an error if one is not displayed.
        #The empty figure serves as a placeholder so the changefig method functions properly.
        fig = Figure()
        self.addmpl(fig)

    def plotfield(self):
        wavelen = float(self.wavelen.toPlainText())
        modenum = int(self.modeNum.toPlainText())
        Zin = float(self.z.toPlainText())

        x, ET = genAM.doubleSlit(wavelen, modenum, Zin)

        figx = Figure()
        ax1f1 = figx.add_subplot(111)
        ax1f1.set_title('Beam Pattern')

        ax1f1.plot(x, np.abs(ET)**2, label='Beam Pattern at %s m' % Zin)
        ax1f1.set_xlabel('x [m]')
        ax1f1.set_ylabel('Intensity')
        ax1f1.legend(loc='best')
        ax1f1.grid()

        self.rmmpl()
        self.addmpl(figx)

    def addmpl(self, fig):
        self.canvas = FigureCanvas(fig)
        self.mplvl_0.addWidget(self.canvas)
        self.canvas.draw()
        self.toolbar = NavigationToolbar(self.canvas,
                                         self.mplwindow,
                                         coordinates=True)
        self.mplvl_0.addWidget(self.toolbar)

    def rmmpl(self):
        self.mplvl_0.removeWidget(self.canvas)
        self.canvas.close()
        self.mplvl_0.removeWidget(self.toolbar)
        self.toolbar.close()
Beispiel #6
0
class Main(QMainWindow, Ui_MainWindow):
    def __init__(self, ):
        super(Main, self).__init__()
        self.setupUi(self)

    def addmpl(self, fig):
        self.canvas = FigureCanvas(fig)
        self.mplvl.addWidget(self.canvas)
        self.canvas.draw()
        self.toolbar = NavigationToolbar(self.canvas,
                                         self.mplwindow,
                                         coordinates=True)
        self.mplvl.addWidget(self.toolbar)

    def rmmpl(self, ):
        self.mplvl.removeWidget(self.canvas)
        self.canvas.close()
        self.mplvl.removeWidget(self.toolbar)
        self.toolbar.close()
Beispiel #7
0
class MplWidget(QWidget):
    '''
    This class creates figures in the UI
    '''
    def __init__(self, parent=None):
        QWidget.__init__(self, parent)
        self.canvas = FigureCanvas(Figure())
        self.vertical_layout = QVBoxLayout()
        self.vertical_layout.addWidget(self.canvas)
        self.toolbar = NavigationToolbar(self.canvas, self)
        self.vertical_layout.addWidget(self.toolbar)
        self.setLayout(self.vertical_layout)
        self.canvas.draw()

    def removePlot(self, ):
        self.vertical_layout.removeWidget(self.canvas)
        self.canvas.close()
        self.vertical_layout.removeWidget(self.toolbar)
        self.toolbar.close()
        self.setLayout(self.vertical_layout)

    def addPlot(self, fig):
        self.canvas = FigureCanvas(fig)
        self.vertical_layout.addWidget(self.canvas)
        self.toolbar = NavigationToolbar(self.canvas, self, coordinates=True)
        self.vertical_layout.addWidget(self.toolbar)
        self.canvas.draw()
        self.setLayout(self.vertical_layout)

    def addDynamicPlot(self, fig):
        self.canvas = FigureCanvas(fig)
        self.vertical_layout.addWidget(self.canvas)
        self.toolbar = NavigationToolbar(self.canvas, self, coordinates=True)
        self.vertical_layout.addWidget(self.toolbar)
        self.setLayout(self.vertical_layout)
        self.cursor = SnaptoCursor(fig)
        self.cid = self.canvas.mpl_connect('motion_notify_event',
                                           self.cursor.mouse_move)

    def selectPointPlot(self, fig, QListWidget):
        self.cursor = SnaptoCursor(fig, QListWidget)
        self.canvas.mpl_connect('button_press_event', self.cursor.onclick)
        self.canvas.mpl_connect('motion_notify_event', self.cursor.mouse_move)
Beispiel #8
0
class MplCanvas(object):
    """Ultimately, this is a QWidget (as well as a FigureCanvasAgg, etc.)."""
    def __init__(self, parent, width, height, dpi):
        from PyQt5 import QtWidgets
        from matplotlib.figure import Figure
        from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg
        self.fig = Figure(figsize=(width, height), dpi=dpi)
        self.canvas = FigureCanvasQTAgg(self.fig)
        self.axes = self.fig.add_subplot(111)
        self.axes.set(xlabel='Time (sec)', ylabel='Activation (AU)')
        self.canvas.setParent(parent)
        FigureCanvasQTAgg.setSizePolicy(self.canvas,
                                        QtWidgets.QSizePolicy.Expanding,
                                        QtWidgets.QSizePolicy.Expanding)
        FigureCanvasQTAgg.updateGeometry(self.canvas)
        # XXX eventually this should be called in the window resize callback
        tight_layout(fig=self.axes.figure)

    def plot(self, x, y, label, **kwargs):
        """Plot a curve."""
        line, = self.axes.plot(x, y, label=label, **kwargs)
        self.update_plot()
        return line

    def update_plot(self):
        """Update the plot."""
        self.axes.legend(prop={
            'family': 'monospace',
            'size': 'small'
        },
                         framealpha=0.5,
                         handlelength=1.)
        self.canvas.draw()

    def show(self):
        """Show the canvas."""
        self.canvas.show()

    def close(self):
        """Close the canvas."""
        self.canvas.close()
Beispiel #9
0
class MainWindow(QMainWindow, Ui_MainWindow):
    def __init__(self, ):
        super(MainWindow, self).__init__()
        self.setupUi(self)
        self.cboxCultura.clear()
        self.cboxEstado.clear()
        root, dirs, files = next(os.walk(dirculturassimuladas))
        self.cboxCultura.addItems(dirs)
        self.cboxCultura.currentIndexChanged.connect(self.carregaEstados)
        self.cboxEstado.currentIndexChanged.connect(self.carregaShape)

    def carregaShape(self):
        subprocess.call(
            ('/home/daniel/Desktop/tcc/shapeSP/recorteSP.R',
             self.cboxEstado.currentText(), self.cboxCultura.currentText()))
        MainWindow.rmmpl()
        drawstates()
        MainWindow.addmpl(fig1)

    def carregaEstados(self):
        self.cboxEstado.clear()
        direstado = dirculturassimuladas + "/" + self.cboxCultura.currentText()
        root, dirs, files = os.walk(direstado).next()
        self.cboxEstado.addItems(dirs)

    def addmpl(self, fig):
        self.canvas = FigureCanvas(fig)
        self.mplvl.addWidget(self.canvas)
        self.canvas.draw()
        self.show()
        #self.toolbar = NavigationToolbar(self.canvas)#, self.mplwindow
        #self.mplvl.addWidget(self.toolbar)

        #self.canvas.mpl_connect('pick_event', self.on_pick)

    def rmmpl(self):
        self.mplvl.removeWidget(self.canvas)
        self.canvas.close()
        fig1.clf()
Beispiel #10
0
class Main(QMainWindow, Ui_MainWindow):
    """The class's docstring"""

    def __init__(self, ):
        super(Main, self).__init__()
        self.setupUi(self)
        self.menubar = self.menuBar()
        self.menubar.setNativeMenuBar(False)

        # define some callback functions here
        from classes.pytecpiv_dialog_conf_class import dialog_conf

        self.actionConfiguration.triggered.connect(self.show_conf_fn)  # menu settings
        self.dialog_conf = dialog_conf(self)

        from classes.pytecpiv_dialog_fig_class import dialog_img
        self.dialog_img = dialog_img(self)

        self.new_project_menu.triggered.connect(self.create_new_project)  # menu new project
        self.import_calib_menu.triggered.connect(self.import_calib_img)  # menu data import calib image

        self.Dataset_comboBox.currentIndexChanged.connect(self.dataset_combobox_fn)

        self.Img_pushButton.clicked.connect(self.show_dialog_img)

    def show_dialog_img(self):
        self.dialog_img.show()

    def addmpl(self, fig):
        """The method's docstring"""
        self.canvas = FigureCanvas(fig)
        self.mplvl.addWidget(self.canvas)
        self.canvas.draw()
        self.toolbar = NavigationToolbar(self.canvas, self.mplfig, coordinates=True)
        self.mplvl.addWidget(self.toolbar)

    def rmmpl(self, ):
        """The method's docstring"""
        self.mplvl.removeWidget(self.canvas)
        self.canvas.close()
        self.mplvl.removeWidget(self.toolbar)
        self.toolbar.close()

    def show_conf_fn(self):
        """This function makes visible the dialogue box for the configuration"""
        import os
        from pytecpiv_conf import pytecpiv_get_pref

        current_directory = os.getcwd()
        (file_exist, sources_path, projects_path) = pytecpiv_get_pref()
        self.dialog_conf.code_label.setText(current_directory)
        self.dialog_conf.sources_label.setText(sources_path)
        self.dialog_conf.projects_label.setText(projects_path)
        self.dialog_conf.show()

    def create_new_project(self):
        from PyQt5.QtWidgets import QFileDialog
        from pytecpiv_conf import pytecpiv_get_pref
        from datetime import datetime
        from pytecpiv_util import dprint
        import json

        file_exist, sources_path, projects_path = pytecpiv_get_pref()
        this_project_path = QFileDialog.getExistingDirectory(self, 'Open directory', projects_path)

        drive, path_and_file = os.path.splitdrive(this_project_path)
        path_project, project_name = os.path.split(path_and_file)

        # create new project
        project_create_time = str(datetime.now())
        dprint(' ')
        dprint(project_create_time)
        dprint('creating new project')

        # create new project_metadata file
        t = os.path.isfile('project_metadata.json')

        if t:
            os.remove('project_metadata.json')

        metadata = {'project': []}
        metadata['project'].append({'project_path': this_project_path})
        metadata['project'].append({'path': path_project})
        metadata['project'].append({'name': project_name})
        metadata['project'].append({'create_date': project_create_time})

        with open('project_metadata.json', 'w') as outfile:
            json.dump(metadata, outfile)

    def import_calib_img(self):
        from PyQt5.QtWidgets import QFileDialog
        from pytecpiv_conf import pytecpiv_get_pref, pytecpiv_set_cores
        from pytecpiv_util import dprint
        from pytecpiv_import import import_img

        import json
        import os

        global dataset_index, current_dataset, time_step

        #  get the preferences for where sources are located
        file_exist, sources_path, projects_path = pytecpiv_get_pref()

        #  pick the directory with the raw calib images; open the sources directory
        source_path_calib_img = QFileDialog.getExistingDirectory(self, 'Open directory', sources_path)

        #  get the fraction core for parallel processing from conf dialog
        fraction_cores = self.dialog_conf.SliderCores.value()
        fraction_cores = fraction_cores / 100

        n_cores, use_cores = pytecpiv_set_cores(fraction_cores)

        dprint(str(n_cores) + ' cores available')
        dprint('using ' + str(use_cores) + ' cores when parallel')

        #  open the project_metadata to get the name and path of the new project
        with open('project_metadata.json') as f:
            project_data = json.load(f)

            project = project_data["project"]
            project = project[0]
            project_path = project["project_path"]

        #  create a directory named CALIB inside the new project directory to store the imported calibration images
        project_path_calib_img = os.path.join(project_path, 'CALIB')

        t = os.path.exists(project_path_calib_img)

        if t:
            dprint('saving in directory: ' + project_path_calib_img)
        else:
            os.mkdir(project_path_calib_img)
            dprint('saving in directory: ' + project_path_calib_img)

        n_img = import_img(source_path_calib_img, project_path_calib_img, use_cores)

        project_data['project'].append({'source_calibration': source_path_calib_img,
                                        'number_calibration_images': n_img})

        # save metadata
        with open('project_metadata.json', 'w') as outfile:
            json.dump(project_data, outfile)

        # create new dataset entry and select
        dataset_index = dataset_index + 1

        current_dataset[0] = 'calibration'
        current_dataset[1] = 1
        current_dataset[2] = 1
        current_dataset[3] = 0
        current_dataset[4] = 0
        current_dataset[5] = project_path_calib_img
        current_dataset[6] = 0
        current_dataset[7] = 1
        current_dataset[8] = 'Greys'

        #  change the frame number and time in gui
        self.frame_text.setText(str(1))
        self.time_text.setText(str((1 - 1) * time_step))

        # change status of chow image checkbox
        self.Img_checkBox.setCheckState(2)

        # save dataset in json file
        table_dataset = {dataset_index: []}
        table_dataset[dataset_index].append({
            'name': 'calibration',
            'frame': 1,
            'plot_image': 1,
            'plot_vector': 0,
            'plot_scalar': 0,
            'image_path': project_path_calib_img,
            'img_value_min': 0,
            'img_value_max': 1,
            'img_colormap': 'Greys'
        })

        # save data in json file in sources
        with open('table_dataset.json', 'w') as outfile:
            json.dump(table_dataset, outfile)

        # change the selected combobox
        self.Dataset_comboBox.insertItem(int(dataset_index), 'calibration images')
        self.Dataset_comboBox.setCurrentIndex(int(dataset_index))

    def dataset_combobox_fn(self):
        """

        :param self:
        :return:
        """
        import json
        from pytecpiv_util import create_fig

        global current_dataset, fig1

        index = self.Dataset_comboBox.currentIndex()

        self.rmmpl()  # clear the mpl for a replot

        if index == 0:  # this is a special index with the credits & license

            fig1 = Figure()
            ax1f1 = fig1.add_subplot(111)
            s1 = 'pyTecPIV v0.1-alpha'
            s2 = 'build on Python 3.7 with the following packages:'
            s3 = 'numpy, scikit-image, rawpy, json, hdf5, matplotlib'
            s4 = 'GUI build with Qt5'
            s5 = 'D. Boutelier, 2020'
            ax1f1.margins(0, 0, tight=True)
            ax1f1.set_ylim([0, 1])
            ax1f1.set_xlim([0, 1])
            ax1f1.text(0.01, 0.95, s1, fontsize=12)
            ax1f1.text(0.01, 0.9, s2, fontsize=10)
            ax1f1.text(0.01, 0.85, s3, fontsize=10)
            ax1f1.text(0.01, 0.775, s4, fontsize=10)
            ax1f1.text(0.01, 0.675, s5, fontsize=8)
            ax1f1.set_aspect('equal')
            ax1f1.set_axis_off()

        else:
            #  open the table_dataset file
            with open('table_dataset.json') as f:
                table_dataset = json.load(f)
                selected_dataset = table_dataset[str(index)]
                selected_dataset = selected_dataset[0]
                current_dataset[0] = selected_dataset['name']
                current_dataset[1] = selected_dataset['frame']
                current_dataset[2] = selected_dataset['plot_image']
                current_dataset[3] = selected_dataset['plot_vector']
                current_dataset[4] = selected_dataset['plot_scalar']
                current_dataset[5] = selected_dataset['image_path']
                current_dataset[6] = selected_dataset['img_value_min']
                current_dataset[7] = selected_dataset['img_value_max']
                current_dataset[8] = selected_dataset['img_colormap']

            fig1 = Figure()
            create_fig(fig1, current_dataset)

        self.addmpl(fig1)
Beispiel #11
0
class univariategroupDlg(QtWidgets.QDialog, Ui_univariategroup):
    def __init__(self, parent=None):
        super(univariategroupDlg, self).__init__(parent)
        self.setupUi(self)
        self.setWindowFlags(QtCore.Qt.Window | QtCore.Qt.CustomizeWindowHint
                            | QtCore.Qt.WindowTitleHint
                            | QtCore.Qt.WindowCloseButtonHint
                            | QtCore.Qt.WindowMaximizeButtonHint)
        self.YcomboBox.addItems(DS.Lc[DS.Ic])
        self.YcomboBox.setCurrentIndex(0)
        self.TcheckBox.setChecked(False)
        self.XGcheckBox.setChecked(True)
        self.YGcheckBox.setChecked(True)
        self.XMcheckBox.setChecked(True)
        self.YMcheckBox.setChecked(True)
        self.XcheckBox.setChecked(True)
        self.YcheckBox.setChecked(True)
        self.ApplyButton.clicked.connect(self.redraw)
        self.ResetButton.clicked.connect(self.reset)
        fig = Figure()
        ax = fig.add_subplot(111)
        ax.plot(np.array(0))
        ax.set_xlim([0, 1])
        ax.set_ylim([0, 1])
        self.addmpl(fig)

    def redraw(self):
        groups = pd.Series(DS.Gr[DS.Ir], dtype='int')
        gr_type = [isinstance(e, (int, np.integer)) for e in groups]
        if sum(gr_type) != len(groups):
            QtWidgets.QMessageBox.critical(self,'Error',"Groups must be all present \n and must be integers",\
                                           QtWidgets.QMessageBox.Ok)
            return ()
        data = DS.Raw[DS.Ir]
        data = data[self.YcomboBox.currentText()]
        if data.shape[1] != 1:
            QtWidgets.QMessageBox.critical(self,'Error',"More columns have the same name",\
                                           QtWidgets.QMessageBox.Ok)
            return ()
        YG = pd.DataFrame({
            'G': groups.values,
            'Y': data.values,
            'Cr': DS.Cr[DS.Ir],
            'Lr': DS.Lr[DS.Ir]
        })
        YG = YG[~data.isnull().values]
        YGg = YG.groupby(['G'])
        ngr = len(YGg)
        color = 'blue'
        fig = Figure()
        ax = fig.add_subplot(111)
        if self.CcheckBox.isChecked():
            color = DS.Cc[self.YcomboBox.currentIndex() - 1]
        if (self.BoxradioButton.isChecked()):
            plt.hold = True
            boxes = []
            nbox = []
            medianprops = dict(marker='D',
                               markeredgecolor='black',
                               markerfacecolor=color)
            for key, grp in YGg:
                nbox.append(key)
                boxes.append(grp['Y'])
            ax.boxplot(boxes, vert=1, medianprops=medianprops)
            ax.set_xticklabels(nbox, rotation='vertical')
            if self.YcheckBox.isChecked():
                if self.YlineEdit.text():
                    ax.set_ylabel(self.YlineEdit.text())
            if self.XcheckBox.isChecked():
                if self.XlineEdit.text():
                    ax.set_xlabel(self.YlineEdit.text())
        if (self.ConditioningradioButton.isChecked()):
            for key, grp in YGg:
                if self.CcheckBox.isChecked():
                    vcol = grp['Cr']
                else:
                    vcol = cm.magma.colors[int(
                        (len(cm.magma.colors) - 1) / ngr * key)]
                ax.scatter(range(1,
                                 len(grp['Y']) + 1),
                           grp['Y'],
                           color=vcol,
                           marker='o',
                           label=key)
            ax.legend(loc='best')
        elif (self.histogramradioButton.isChecked()):
            iqr = np.percentile(YG['Y'], [75, 25])
            iqr = iqr[0] - iqr[1]
            n = YG['Y'].shape[0]
            dy = abs(float(YG['Y'].max()) - float(YG['Y'].min()))
            nbins = np.floor(dy / (2 * iqr) * n**(1 / 3)) + 1
            nbins = 2 * nbins
            bins = np.linspace(float(YG['Y'].min()), float(YG['Y'].max()),
                               nbins)
            for key, grp in YGg:
                if self.CcheckBox.isChecked():
                    vcol = grp['Cr']
                    vcol = np.unique(vcol)[0]
                else:
                    vcol = cm.magma.colors[int(
                        (len(cm.magma.colors) - 1) / ngr * key)]
                ax.hist(grp['Y'],
                        bins=bins,
                        histtype='bar',
                        color=vcol,
                        alpha=0.5,
                        orientation='vertical',
                        label=str(key))
            ax.legend(loc='best')
        if self.TcheckBox.isChecked():
            if self.TlineEdit.text():
                ax.set_title(self.TlineEdit.text())
        else:
            ax.set_title('')
        if self.XcheckBox.isChecked():
            if self.XlineEdit.text():
                ax.set_xlabel(self.XlineEdit.text())
        else:
            ax.set_xlabel('')
        if self.YcheckBox.isChecked():
            ax.set_ylabel(self.YlineEdit.text())
        else:
            ax.set_ylabel('')
        if self.XGcheckBox.isChecked():
            ax.xaxis.grid(True)
        else:
            ax.xaxis.grid(False)
        if self.YGcheckBox.isChecked():
            ax.yaxis.grid(True)
        else:
            ax.yaxis.grid(False)
        if not self.XMcheckBox.isChecked():
            ax.tick_params(axis='x',
                           which='both',
                           bottom='off',
                           top='off',
                           labelbottom='off')
        if not self.YMcheckBox.isChecked():
            ax.tick_params(axis='y',
                           which='both',
                           left='off',
                           right='off',
                           labelleft='off')
        self.rmmpl()
        self.addmpl(fig)

    def reset(self):
        self.YcomboBox.setCurrentIndex(0)
        self.XGcheckBox.setChecked(True)
        self.YGcheckBox.setChecked(True)
        self.XMcheckBox.setChecked(True)
        self.YMcheckBox.setChecked(True)
        self.XcheckBox.setChecked(True)
        self.YcheckBox.setChecked(True)
        self.XlineEdit.setText('')
        self.YlineEdit.setText('')
        self.TlineEdit.setText('')
        self.update()

    def addmpl(self, fig):
        self.canvas = FigureCanvas(fig)
        self.mplvl.addWidget(self.canvas)
        self.canvas.draw()
        self.toolbar = NavigationToolbar(self.canvas,
                                         self.mplwindow,
                                         coordinates=True)
        self.mplvl.addWidget(self.toolbar)

    def rmmpl(self, ):
        self.mplvl.removeWidget(self.canvas)
        self.canvas.close()
        self.mplvl.removeWidget(self.toolbar)
        self.toolbar.close()
Beispiel #12
0
class doeplotDlg(QtWidgets.QDialog,Ui_doeplotDialog):
    def __init__(self,parent=None):
        super(doeplotDlg,self).__init__(parent)
        self.setupUi(self)
        self.setWindowFlags(QtCore.Qt.Window |QtCore.Qt.CustomizeWindowHint |
            QtCore.Qt.WindowTitleHint | QtCore.Qt.WindowCloseButtonHint |
            QtCore.Qt.WindowMaximizeButtonHint)
        Lc=DS.Lc[DS.Ic]
        Gc=DS.Gc[DS.Ic]
        Lcx=Lc[-Gc]
        Lcy=Lc[Gc]
        for x in Lcy:
            self.responcecomboBox.addItem(str(x))
        for x in Lcx:
            self.factorcomboBox.addItem(str(x))
        self.XGcheckBox.setChecked(False)
        self.YGcheckBox.setChecked(False)
        self.XMcheckBox.setChecked(True)
        self.YMcheckBox.setChecked(True)
        self.XcheckBox.setChecked(True)
        self.YcheckBox.setChecked(True)
        self.ApplyButton.clicked.connect(self.redraw)
        self.ResetButton.clicked.connect(self.reset)
        fig=Figure()
        ax=fig.add_subplot(111)
        ax.plot(np.array(0))
        ax.set_xlim([0,1])
        ax.set_ylim([0,1])
        self.addmpl(fig)
    def redraw(self):
        if DOE.lfac is None: 
            doemodel(self)
        fig=Figure()
        ny=self.responcecomboBox.currentIndex()
        nf=self.factorcomboBox.currentIndex()
        Lc=DS.Lc[DS.Ic]
        Gc=DS.Gc[DS.Ic]
        Lcy=Lc[Gc]
        Lcx=Lc[-Gc]
        nX=len(Lcx)
        data=DS.Raw.loc[DS.Ir,DS.Ic]
        Y=data[Lcy]
        X=data[Lcx]
        nr=X.shape[0]
        w_text=''
        if nr>2**nX:
            Xu=X[-X.duplicated().values]
            Yu=[]
            for i in range(Xu.shape[0]):
                Yu.append(Y[(X==Xu.ix[i,:]).product(axis=1).astype('bool').values].mean().values)
            nr=2**nX
            X=Xu
            Y=pd.DataFrame(Yu,index=X.index,columns=Y.columns)
            w_text='Duplicated tests are averaged'
        ncx=len(Lcx)
        Y=Y.values.astype('float')
        X=X.values.astype('float')
        if self.orderedradioButton.isChecked():
            ax=fig.add_subplot(111)
            Y=Y[:,ny]
            order=Y.argsort()
            ind=np.arange(nr)
            X=X[order,:]
            if((X.max()==1)&(X.min()==-1)):
                X=X.astype('str')
                X[X=='-1.0']='-'
                X[X=='1.0']='+'
                X[X=='0.0']='O'
            else:
                X=X.astype('str')            
            label=nr*['']
            for i in range(nr):
                label[i]=' '.join(X[i,:].tolist())
            ax.scatter(ind,Y[order],color='red',alpha=0.3,marker='o')
            ax.set_xticks(ind)
            ax.set_xticklabels(ind[order]+1, rotation='vertical')
            ax.set_xlabel('Experiment Sequence')
            ax.set_ylabel('Responce')
            ax.xaxis.grid()
            ax.yaxis.grid()
            ax.set_title('Ordered Plot for '+self.responcecomboBox.currentText())
            for i in range(nr):
                ax.annotate(label[i],(ind[i],Y[order][i]),rotation=-90,size=14)
        elif self.meanradioButton.isChecked():
            ax=fig.add_subplot(111)
            Y=Y[:,ny]
            vmain=np.zeros(2*ncx)
            Ym=np.mean(Y)
            labels=[]
            for i in range(ncx):
                labels=labels+['-']+[Lcx[i]]+['+']+[' ']
                vmain[2*i]=np.mean(Y[X[:,i]==-1])
                vmain[2*i+1]=np.mean(Y[X[:,i]==1])
            ind=np.arange(2*ncx)
            ax.scatter(ind,vmain,color='red',alpha=0.3,marker='o')
            ax.set_xlabel('Factors')
            ax.set_ylabel('Responce')
            ax.set_xticks(np.arange(0,2*ncx,0.5))
            ax.set_xticklabels(labels,rotation='vertical')
            ax.set_title('Main Effects Plot for Responce: '+self.responcecomboBox.currentText())
            ax.add_line(Line2D([-1,2*ncx],[Ym,Ym],color='green'))
            for i in range(ncx):
                ax.add_line(Line2D([ind[2*i],ind[2*i+1]],[vmain[2*i],vmain[2*i+1]],color='red')) 
        elif self.mainradioButton.isChecked():
            ax=fig.add_subplot(111)
            Y=Y[:,ny]
            Ym=np.mean(Y)
            labels=[]
            ind=[]
            vy=[]
            for i in range(ncx):
                labels=labels+['-']+[Lcx[i]]+['+']+[' ']
                vy=vy+[Y[X[:,i]==-1].T.tolist()]+[Y[X[:,i]==1].T.tolist()]
                ind=ind+len(Y[X[:,i]==-1])*[2*i]+len(Y[X[:,i]==1])*[2*i+1]
            ax.scatter(ind,vy,color='red',alpha=0.3,marker='o')
            ax.set_xlabel('Factors')
            ax.set_ylabel('Responce')
            ax.set_xticks(np.arange(0,2*ncx,0.5))
            ax.set_xticklabels(labels, rotation='vertical')
            ax.set_title('Scatter Plot for Responce :'+self.responcecomboBox.currentText())
            ax.add_line(Line2D([-1,2*ncx],[Ym,Ym],color='green'))
            for i in range(2*ncx):
                ax.add_line(Line2D([i,i],[min(vy[i]),max(vy[i])],color='red')) 
        elif self.interactionradioButton.isChecked():
            def int_plot(ax,X,Y,Ym,i,j):
                x=X[:,i]
                if(i!=j):
                    x=x*X[:,j]
                y_m=np.mean(Y[x==-1])
                y_p=np.mean(Y[x==+1])
                ax.set_ylim([min(Y),max(Y)])
                ax.scatter([0,1],[y_m,y_p],color='red',alpha=0.3,marker='o')
                ax.add_line(Line2D([-1,2],[Ym,Ym],color='green'))
                ax.add_line(Line2D([0,1],[y_m,y_p],color='red'))
            Y=Y[:,ny]
            Ym=np.mean(Y)
            gs=GridSpec(ncx,ncx)
            for i in range(ncx):
                for j in range(ncx):
                    if(j>=i):
                        ax=fig.add_subplot(gs[i,j])
                        int_plot(ax,X,Y,Ym,i,j)
                        if(i==j):
                            ax.set_ylabel('Responce')
                            ax.set_xticks([0,1], minor=False)
                            ax.set_xticklabels(['-','+'],minor=False)
                            ax.set_xlabel(Lcx[i])
                        else:
                            ax.xaxis.set_visible(False)
        elif self.blockradioButton.isChecked():
            ax=fig.add_subplot(111)
            for i in range(nr):
                for j in range(ncx):
                   if((X[i,j]!=1)&(X[i,j]!=-1)):
                        QtWidgets.QMessageBox.critical(self,'Output Limit',
                        'There are more then 2 levels\n I am not able to plot',QtWidgets.QMessageBox.Ok)
                        return()
            labels=np.array(range(1,ncx+1))
            labels=np.delete(labels,nf)
            x=X[:,nf]
            X=np.delete(X,nf,1)
            Y=Y[:,ny]
            if(min(Y)>0):
                ymin=0.99*min(Y)
            else:
                ymin=1.01*min(Y)
            if(max(Y)>0):
                ymax=1.01*max(Y)
            else:
                ymax=0.99*max(Y)
            n_factor=math.factorial(ncx)/math.factorial(ncx-2)/2
            if(n_factor>10):
                QtWidgets.QMessageBox.critical(self,'Output Limit',
                'There are more then 10 interactions. \n Too many to be plotted!',QtWidgets.QMessageBox.Ok)
                return()
            if(n_factor==1):
                QtWidgets.QMessageBox.critical(self,'Output Limit',
                'There are no interaction left. \n Block Plot does not apply',QtWidgets.QMessageBox.Ok)
                return()
            for i in range(ncx-1):
                for j in range(i,(ncx-1)):
                    if(j>i):
                        vm=np.zeros(4)
                        vp=np.zeros(4)
                        vm[0]=np.mean(Y[(x==-1)&(X[:,j]==-1)&(X[:,i]==-1)])
                        vm[1]=np.mean(Y[(x==-1)&(X[:,j]==-1)&(X[:,i]==+1)])
                        vm[2]=np.mean(Y[(x==-1)&(X[:,j]==+1)&(X[:,i]==-1)])
                        vm[3]=np.mean(Y[(x==-1)&(X[:,j]==+1)&(X[:,i]==+1)])
                        vp[0]=np.mean(Y[(x==+1)&(X[:,j]==-1)&(X[:,i]==-1)])
                        vp[1]=np.mean(Y[(x==+1)&(X[:,j]==-1)&(X[:,i]==+1)])
                        vp[2]=np.mean(Y[(x==+1)&(X[:,j]==+1)&(X[:,i]==-1)])
                        vp[3]=np.mean(Y[(x==+1)&(X[:,j]==+1)&(X[:,i]==+1)])
                        ind=[0,1,2,3]
                        ax.set_xlabel('Factors Combinations')
                        ax.set_ylabel('Responce')
                        ax.set_title('Block Plot: Responce '+self.responcecomboBox.currentText()+', Factor '+
                        self.factorcomboBox.currentText()+', interaction '+Lcx[labels[i]-1]+'-'+Lcx[labels[j]-1])
                        ax.plot(ind,vm,'_',ind,vp,'+')
                        ax.set_xticks(ind)
                        ax.set_xticklabels(['(- -)','(-,+)','(+,-)','(+,+)'], rotation='horizontal')
                        ax.set_xlim([-1,4])
                        ax.set_ylim([ymin,ymax])
                        currentAxis = plt.gca()
                        for k in range(4):
                            someX=k
                            someY=np.mean([vm[k],vp[k]])
                            d=abs(vm[k]-vp[k])*1.1
                            currentAxis.add_patch(Rectangle((someX-.1,someY-d/2),0.2,d,fill=False))
        elif self.vsfactoradioButton.isChecked():
            ax=fig.add_subplot(111)
            for i in range(nr):
                for j in range(ncx):
                   if((X[i,j]!=1)&(X[i,j]!=-1)):
                        QtWidgets.QMessageBox.critical(self,'Output Limit',
                        'There are more then 2 levels\n I am not able to plot',QtWidgets.QMessageBox.Ok)
                        return()
            x=X[:,nf]
            Y=Y[:,ny]
            ym=Y[x==-1]
            yp=Y[x==+1]
            ind=range(1,3)
            ax.set_ylabel('Responce')
            ax.set_title('Block Plot: Responce '+self.responcecomboBox.currentText()
            +' vs. Factor '+self.factorcomboBox.currentText())
            ax.boxplot([ym,yp])
            ax.set_xticks(ind)
            ax.set_xticklabels(['(-)','(+)'], rotation='horizontal')
        elif self.youdenradioButton.isChecked():
            ax=fig.add_subplot(111)
            Y=Y[:,ny]
            nfac=len(DOE.lfac)
            sfac=np.array([str(i) for i in DOE.lfac])
            x=np.zeros(nfac)
            y=np.zeros(nfac)
            for i in range(nfac):
                Xp=X[:,(DOE.lfac[i][0]-1)]
                for j in range(1,len(DOE.lfac[i])):
                    Xp=Xp*X[:,(DOE.lfac[i][j]-1)]
                x[i]=Y[Xp==-1].mean()
                y[i]=Y[Xp==+1].mean()
            ax.set_ylabel('Average Responce for +1')
            ax.set_xlabel('Average Responce for -1')
            ax.set_title('Youden Plot: Responce :'+self.responcecomboBox.currentText())
            ax.scatter(x,y,color='red',alpha=0.3,marker='o')
            ax.scatter(Y.mean(),Y.mean(),color='blue',s=60,marker='+')
            ax.axhline(y=Y.mean(),color='red',linewidth=1.5,zorder=0)
            ax.axvline(x=Y.mean(),color='red',linewidth=1.5,zorder=0)
            for i in range(nfac):
                ax.annotate(sfac[i],(x[i],y[i]),rotation=0,size=12)
        elif self.effectradioButton.isChecked():
            ax=fig.add_subplot(111)
            Y=Y[:,ny]
            nfac=len(DOE.lfac)
            sfac=np.array([str(i) for i in DOE.lfac])
            x=np.zeros(nfac)
            y=np.zeros(nfac)
            for i in range(nfac):
                Xp=X[:,(DOE.lfac[i][0]-1)]
                for j in range(1,len(DOE.lfac[i])):
                    Xp=Xp*X[:,(DOE.lfac[i][j]-1)]
                x[i]=Y[Xp==-1].mean()
                y[i]=Y[Xp==+1].mean()
            eff=abs(x-y)
            order=eff.argsort()
            width=0.35 
            ind=np.arange(1,nfac+1)
            ax.bar(ind,eff[order],width,color='blue')
            ax.set_xticks(ind+width/2)
            ax.set_xticklabels(sfac[order], rotation='vertical')
            ax.set_xlabel('Factors') 
            ax.set_ylabel('|Effects|') 
            ax.set_title('Effect Plot for '+self.responcecomboBox.currentText())
        elif self.halfradioButton.isChecked():
            ax=fig.add_subplot(111)
            Y=Y[:,ny]
            nfac=len(DOE.lfac)
            sfac=np.array([str(i) for i in DOE.lfac])
            x=np.zeros(nfac)
            y=np.zeros(nfac)
            for i in range(nfac):
                Xp=X[:,(DOE.lfac[i][0]-1)]
                for j in range(1,len(DOE.lfac[i])):
                    Xp=Xp*X[:,(DOE.lfac[i][j]-1)]
                x[i]=Y[Xp==-1].mean()
                y[i]=Y[Xp==+1].mean()
            eff=abs(x-y)
            order=eff.argsort()
            ind=[sp.stats.halfnorm.ppf(i/(nfac+1)) for i in range(1,nfac+1)]
            ax.scatter(ind,eff[order],color='blue',s=60,marker='+')
            for i in range(nfac):
                ax.annotate(sfac[order][i],(ind[i],eff[order][i]),rotation=-90,size=12)
            ax.set_xlabel('Half Normal Distribution Order') 
            ax.set_ylabel('Ordered |Effects|') 
            ax.set_title('Half Normal Distribution Plot for '+self.responcecomboBox.currentText())
        if self.XcheckBox.isChecked():
            if self.XlineEdit.text():
                ax.set_xlabel(self.XlineEdit.text())
        else:
            ax.set_xlabel('')
        if self.YcheckBox.isChecked():
            if self.YlineEdit.text():
                ax.set_ylabel(self.YlineEdit.text())
        else:
            ax.set_ylabel('')
        if self.XGcheckBox.isChecked():
            ax.xaxis.grid(True)
        else:
            ax.xaxis.grid(False)
        if self.YGcheckBox.isChecked():
            ax.yaxis.grid(True)
        else:
            ax.yaxis.grid(False)
        if not self.XMcheckBox.isChecked():    
            ax.tick_params(axis='x',which='both',bottom='off',top='off',labelbottom='off')
        if not self.YMcheckBox.isChecked():
            ax.tick_params(axis='y',which='both',left='off',right='off',labelleft='off')
        ax.annotate(w_text,xy=(0.75,0.95),xycoords='figure fraction',fontsize=8)
        self.rmmpl()
        self.addmpl(fig)        
    def reset(self):
        self.XGcheckBox.setChecked(False)
        self.YGcheckBox.setChecked(False)
        self.XMcheckBox.setChecked(False)
        self.YMcheckBox.setChecked(True)
        self.XcheckBox.setChecked(True)
        self.YcheckBox.setChecked(True)
        self.XlineEdit.setText('')
        self.YlineEdit.setText('')
        self.update()        
    def addmpl(self, fig):
        self.canvas=FigureCanvas(fig)
        self.mplvl.addWidget(self.canvas)
        self.canvas.draw()
        self.toolbar=NavigationToolbar(self.canvas, 
                self.mplwindow, coordinates=True)
        self.mplvl.addWidget(self.toolbar)
    def rmmpl(self,):
        self.mplvl.removeWidget(self.canvas)
        self.canvas.close()
        self.mplvl.removeWidget(self.toolbar)
        self.toolbar.close()
Beispiel #13
0
class VisualizerDialog(QtWidgets.QWidget):
    """ Defines a QDialog that is used to visualize the data in the main
    data preview area. It connects the dataChanged signal of the abstract
    data table and the sectionMoved signal of the header of the table view to
    the update_plot() method so that the method is called whenever either the
    content of the results table changes, or the columns are moved.

    The visualizer dialog has a 'Freeze visualization' checkbox. If the box
    is checked, the visualization is not updated on changes.

    FIXME: Live updates are currently disabled.
    """
    def __init__(self, parent=None):
        super(VisualizerDialog, self).__init__(parent)

        self.ui = Ui_Visualizer()
        self.ui.setupUi(self)
        self.ui.progress_bar.setRange(0, 0)
        self.ui.box_visualize.hide()
        self.ui.progress_bar.hide()
        self.ui.label.hide()

        self.setWindowIcon(options.cfg.icon)
        self.margin_dialoglog_stack = []

        self.frozen = False
        self.spinner = QtWidgets.QSpinBox()
        self.spinner.setFrame(True)
        self.spinner.setButtonSymbols(QtWidgets.QAbstractSpinBox.UpDownArrows)
        self.spinner.setMaximum(10)
        self.spinner.setMinimum(1)
        self.spinner.setSuffix(" year(s)")
        self.spinner_label = QtWidgets.QLabel("Bandwidth: ")
        self.spinner.valueChanged.connect(self.update_plot)

        self.combo_x_function = QtWidgets.QComboBox()
        self.label_x_function = QtWidgets.QLabel("Variable on &X axis:")
        self.label_x_function.setBuddy(self.combo_x_function)
        self.combo_y_function = QtWidgets.QComboBox()
        self.label_y_function = QtWidgets.QLabel("Variable on &Y axis:")
        self.label_y_function.setBuddy(self.combo_y_function)

        self.toolbar = None
        self.canvas = None

        try:
            self.resize(options.settings.value("visualizer_size"))
        except TypeError:
            pass

    def closeEvent(self, event):
        options.settings.setValue("visualizer_size", self.size())
        self.close()

    def update_plot(self):
        """
        Update the plot.

        During the update, the canvas and the navigation toolbar are
        replaced by new instances, as is the figure used by the visualizer.
        Finally, the draw() method of the visualizer is called to plot the
        visualization again.
        """
        if hasattr(self.toolbar, "margin_dialog"):
            self.toolbar.margin_dialog.hide()
            self.toolbar.margin_dialog.close()
            get_toplevel_window().widget_list.remove(self.toolbar.margin_dialog)
            del self.toolbar.margin_dialog

        if self.smooth:
            self.spinner.setEnabled(False)
            self.visualizer.update_data(bandwidth=self.spinner.value())
        else:
            self.visualizer.update_data()


        kwargs = {}
        if self.visualizer.numerical_axes == 2:
            try:
                kwargs["func_y"] = self._function_list[self.combo_y_function.currentIndex()]
                kwargs["column_y"] = None
            except IndexError:
                kwargs["func_y"] = None
                kwargs["column_y"] = utf8(self.combo_y_function.currentText())
        if self.visualizer.numerical_axes == 1:
            try:
                kwargs["func_x"] = self._function_list[self.combo_x_function.currentIndex()]
                kwargs["column_x"] = None
            except IndexError:
                kwargs["func_x"] = None
                kwargs["column_x"] = utf8(self.combo_x_function.currentText())

        self.visualizer.setup_figure()

        self.remove_matplot()
        self.add_matplot()

        self.visualizer.draw(**kwargs)

        self.visualizer.g.fig.tight_layout()
        self.visualizer.adjust_axes()
        self.visualizer.adjust_fonts()
        if self.smooth:
            self.spinner.setEnabled(True)
        self.visualizer.g.fig.tight_layout()

        if hasattr(self.toolbar, "margin_dialog"):
            self.toolbar.configure_subplots()

    def add_matplot(self):
        """ Add a matplotlib canvas and a navigation bar to the dialog. """
        if not self.canvas:
            self.canvas = FigureCanvas(self.visualizer.g.fig)
            self.ui.verticalLayout.addWidget(self.canvas)
            self.canvas.setParent(self.ui.box_visualize)
            self.canvas.setFocusPolicy(QtCore.Qt.ClickFocus)
            self.canvas.setFocus()

        if not self.toolbar:
            self.toolbar = CoqNavigationToolbar(self.canvas, self, True)
            if (self.visualizer.numerical_axes == 2 and
                self.visualizer.function_list):
                self.toolbar.addWidget(self.label_y_function)
                self.toolbar.addWidget(self.combo_y_function)
            if (self.visualizer.numerical_axes == 1 and
                self.visualizer.function_list):
                self.toolbar.addWidget(self.label_x_function)
                self.toolbar.addWidget(self.combo_x_function)
            if options.cfg.experimental:
                self.toolbar.check_freeze.stateChanged.connect(self.toggle_freeze)
            if self.smooth:
                self.toolbar.addWidget(self.spinner_label)
                self.toolbar.addWidget(self.spinner)
            self.ui.navigation_layout.addWidget(self.toolbar)
        else:
            self.toolbar.canvas = self.canvas
        self.canvas.mpl_connect('key_press_event', self.keyPressEvent)
        self.canvas.mpl_connect('button_press_event', self.onclick)

    def onclick(self, event):
        """
        Pass any click event onward to the visualizer, unless the dialog is
        in panning or zooming mode.
        """
        if not self.toolbar.isPanning() and not self.toolbar.isZooming():
            self.visualizer.onclick(event)

    def remove_matplot(self):
        """
        Remove the matplotlib canvas and the navigation bar from the
        dialog.
        """
        if self.canvas:
            self.canvas.close()
        self.ui.verticalLayout.removeWidget(self.canvas)
        self.canvas = None

    def close(self, *args):
        """ Close the visualizer widget, disconnect the signals, and remove
        the visualizer from the list of visualizers when closing."""
        try:
            self.disconnect_signals()
        except TypeError:
            # TypeErrors can be raised if there is no connected object. This
            # can be ignored:
            pass
        self.remove_matplot()
        super(VisualizerDialog, self).close()
        try:
            get_toplevel_window().widget_list.remove(self)
        except ValueError:
            pass
        try:
            del self.visualizer
        except AttributeError:
            pass

    def keyPressEvent(self, event):
        """ Catch key events so that they can be passed on to the matplotlib
        toolbar. """
        try:
            key_press_handler(event, self.canvas, self.toolbar)
        except AttributeError:
            # Attribute errors seem to occur when a key is pressed while the
            # mouse is outside of the figure area:
            #
            # AttributeError: 'QKeyEvent' object has no attribute 'inaxes'
            #
            # This exception may be safely ignored.
            pass
        except Exception as e:
            print(e)
            raise e

    def connect_signals(self):
        """ Connect the dataChanged signal of the abstract data table and the
        sectionMoved signal of the header of the table view to the
        update_plot() method so that the method is called whenever either the
        content of the results table changes, or the columns are moved."""
        if not options.cfg.experimental:
            return
        get_toplevel_window().table_model.dataChanged.connect(self.update_plot)
        get_toplevel_window().table_model.layoutChanged.connect(self.update_plot)
        self.visualizer._view.horizontalHeader().sectionMoved.connect(self.update_plot)

    def disconnect_signals(self):
        """ Disconnect the dataChanged signal of the abstract data table and
        the sectionMoved signal of the header of the table view so that the
        update_plot() method is not called anymore when the content of the
        results table changes or the columns are moved."""
        if not options.cfg.experimental:
            return
        get_toplevel_window().table_model.dataChanged.disconnect(self.update_plot)
        get_toplevel_window().table_model.layoutChanged.disconnect(self.update_plot)
        self.visualizer._view.horizontalHeader().sectionMoved.disconnect(self.update_plot)

    def toggle_freeze(self):
        """ Toggle the 'frozen' state of the visualization. This method is
        called whenever the 'Freeze visualization' box is checked or
        unchecked.

        If the box is checked, the visualization is frozen, and the plot is
        not updated if the content of the results table changes or if the
        columns of the table view are moved.

        If the box is unchecked (the default), the visualization is not
        frozen, and the plot is updated on changes to the results table. """
        if not options.cfg.experimental:
            return
        self.frozen = not self.frozen
        if self.frozen:
            self.disconnect_signals()
        else:
            self.connect_signals()

    def Plot(self, model, view, visualizer_class, parent=None, **kwargs):
        """ Use the visualization type given as 'visualizer_class' to display
        the data given in the abstract data table 'model', using the table
        view given in 'view'. """
        self.smooth = kwargs.get("smooth", False)
        self.visualizer = visualizer_class(model, view, parent=None, **kwargs)
        self._function_list = self.visualizer.function_list
        try:
            self.combo_x_function.addItems([fnc.get_name() for fnc in self._function_list])
            for x in self.visualizer._number_columns:
                if x not in [fnc(columns=self.visualizer._group_by, session=get_toplevel_window().Session).get_id() for fnc in self._function_list]:
                    self.combo_x_function.addItem(x)
            self.combo_x_function.currentIndexChanged.connect(self.update_plot)
        except AttributeError:
            pass
        try:
            self.combo_y_function.addItems([fnc.get_name() for fnc in self._function_list] + self.visualizer._number_columns)
            self.combo_y_function.currentIndexChanged.connect(self.update_plot)
        except AttributeError:
            pass

        if not self.visualizer._table.empty:
            self.setVisible(True)
            self.connect_signals()
            get_toplevel_window().widget_list.append(self)
            self.add_matplot()
            self.thread = classes.CoqThread(self.visualizer.draw,
                                            parent=self)
            self.thread.taskStarted.connect(self.startplot)
            self.thread.taskFinished.connect(self.finishplot)
            self.thread.taskException.connect(self.plotexception)

            self.visualizer.moveToThread(self.thread)
            self.thread.start()

    def plotexception(self, e):
        print(e)
        QtWidgets.QMessageBox.critical(self, "Error while plotting – Coquery",
            msg_visualization_error.format(self.exception),
            QtWidgets.QMessageBox.Ok, QtWidgets.QMessageBox.Ok)

    def startplot(self):
        self.ui.box_visualize.hide()
        self.ui.frame.setDisabled(True)
        self.ui.frame_placeholder.show()
        self.ui.progress_bar.show()
        self.ui.label.show()
        self.repaint()

    def finishplot(self):
        self.ui.frame_placeholder.hide()
        self.ui.progress_bar.hide()
        self.ui.label.hide()
        self.ui.box_visualize.show()
        self.ui.frame.setDisabled(False)
        self.repaint()

        self.visualizer.g.fig.canvas.draw()

        self.visualizer.g.fig.tight_layout()
        self.visualizer.adjust_axes()
        self.visualizer.adjust_fonts()

        self.visualizer.set_hover()
        self.visualizer.g.fig.tight_layout()
        self.show()

        # Create an alert in the system taskbar to indicate that the
        # visualization has completed:
        options.cfg.app.alert(self, 0)
    def close(self, *args, **kwargs):
        for widget in [self.toolbar, self.smartScale, self.VCursor]:
            widget.close()
            widget.deleteLater()

        FigureCanvas.close(self, *args, **kwargs)
Beispiel #15
0
class Application(Ui_QtBaseClass, Ui_MainWindow):
    def __init__(self, parent=None):
        Ui_QtBaseClass.__init__(self, parent)
        self._figure = None

        self.setupUi(self)
        self.update_parameters(self.static_default_parameters())
        self.show()

        self.on_click_submit()  # load graph based on default values

        # (figure_width, figure_height) = tuple(self._figure.figure.get_size_inches)
        # self.update_parameters(figure_size_width = figure_width, figure_size_height = figure_height, figure_size_scale = 1.0)

        # connect button to function on_click
        self.pushButton_submit.clicked.connect(self.on_click_submit)
        self.pushButton_quit.clicked.connect(self.on_click_quit)
        self.pushButton_update.clicked.connect(self.on_click_update)
        self.pushButton_save.clicked.connect(self.on_click_save)

    def on_click_submit(self):
        p = self.get_parameters()
        print("PARSED PARAMETERS ARE:")
        for i in p:
            print(i + ': ' + str(p[i]))
        time.sleep(0.1)

        self._figure = main_function(
            p['time_step'], p['time_cap'], p['gas_emissivity'], p['vent_mass_flow_rate'],
            p['specimen_exposed_area'], p['specimen_volume'], p['specimen_heat_of_combustion'], p['specimen_burning_rate'], p['specimen_density'],
            p['lining_surface_emissivity'], p['lining_surface_area'], p['lining_thickness'], p['lining_thermal_conductivity'], p['lining_density'], p['lining_specific_heat']
        )
        self.add_mpl(self._figure.figure)
        self.on_click_save()
        return self.get_parameters()

    def on_click_update(self):
        self.remove_mpl()
        self.on_click_submit()
        self.on_click_save()

    def on_click_quit(self):
        self.close()

    def on_click_save(self):
        old_size = self._figure.figure.get_size_inches().tolist()

        p = self.get_parameters()
        new_size = [p['figure_size_width']*p['figure_size_scale'], p['figure_size_height']*p['figure_size_scale']]

        self._figure.figure.set_size_inches(new_size)
        self._figure.save_figure("output")
        print("Figure saved at: " + os.getcwd())

        self._figure.figure.set_size_inches(old_size)
        self.canvas.draw()

    def add_mpl(self, fig):
        self.canvas = FigureCanvas(fig)
        self.mplvl.addWidget(self.canvas)
        self.canvas.draw()
        self.toolbar = NavigationToolbar(self.canvas,
                self.mplwindow, coordinates=True)
        self.mplvl.addWidget(self.toolbar)

    def remove_mpl(self):
        self.mplvl.removeWidget(self.canvas)
        self.canvas.close()
        self.mplvl.removeWidget(self.toolbar)
        self.toolbar.close()

    def update_parameters(self, parameters):
        for key in parameters:
            parameters[key] = str(parameters[key])

        self.lineEdit_timeStep.setText(parameters['time_step'])
        self.lineEdit_timeCap.setText(parameters['time_cap'])
        self.lineEdit_gasEmissivity.setText(parameters['gas_emissivity'])
        self.lineEdit_ventMassFlowRate.setText(parameters['vent_mass_flow_rate'])

        self.lineEdit_specimenExposedArea.setText(parameters['specimen_exposed_area'])
        self.lineEdit_specimenVolume.setText(parameters['specimen_volume'])
        self.lineEdit_specimenHeatOfCombustion.setText(parameters['specimen_heat_of_combustion'])
        self.lineEdit_specimenBurningRate.setText(parameters['specimen_burning_rate'])
        self.lineEdit_specimenDensity.setText(parameters['specimen_density'])

        self.lineEdit_liningSurfaceEmissivity.setText(parameters['lining_surface_emissivity'])
        self.lineEdit_liningSurfaceArea.setText(parameters['lining_surface_area'])
        self.lineEdit_liningThickness.setText(parameters['lining_thickness'])
        self.lineEdit_liningThermalConductivity.setText(parameters['lining_thermal_conductivity'])
        self.lineEdit_liningDensity.setText(parameters['lining_density'])
        self.lineEdit_liningSpecificHeat.setText(parameters['lining_specific_heat'])

        self.lineEdit_figure_size_width.setText(parameters['figure_size_width'])
        self.lineEdit_figure_size_height.setText(parameters['figure_size_height'])
        self.lineEdit_figure_size_scale.setText(parameters['figure_size_scale'])
        pass

    def get_parameters(self):
        # self.lineEdit_timeStep.text()
        # self.lineEdit_timeCap.text()
        # self.lineEdit_gasEmissivity.text()
        # self.lineEdit_ventMassFlowRate.text()
        #
        # self.lineEdit_specimenExposedArea.text()
        # self.lineEdit_specimenVolume.text()
        # self.lineEdit_specimenHeatOfCombustion.text()
        # self.lineEdit_specimenBurningRate.text()
        # self.lineEdit_specimenDensity.text()
        #
        # self.lineEdit_liningSurfaceEmissivity.text()
        # self.lineEdit_liningSurfaceArea.text()
        # self.lineEdit_liningThickness.text()
        # self.lineEdit_liningThermalConductivity.text()
        # self.lineEdit_liningDensity.text()
        # self.lineEdit_liningSpecificHeat.text()

        parameters={
            "time_step": self.lineEdit_timeStep.text(),
            "time_cap": self.lineEdit_timeCap.text(),
            'gas_emissivity': self.lineEdit_gasEmissivity.text(),
            'vent_mass_flow_rate': self.lineEdit_ventMassFlowRate.text(),

            'specimen_exposed_area': self.lineEdit_specimenExposedArea.text(),
            'specimen_volume': self.lineEdit_specimenVolume.text(),
            'specimen_heat_of_combustion': self.lineEdit_specimenHeatOfCombustion.text(),
            'specimen_burning_rate': self.lineEdit_specimenBurningRate.text(),
            'specimen_density': self.lineEdit_specimenDensity.text(),

            'lining_surface_emissivity': self.lineEdit_liningSurfaceEmissivity.text(),
            'lining_surface_area': self.lineEdit_liningSurfaceArea.text(),
            'lining_thickness': self.lineEdit_liningThickness.text(),
            'lining_thermal_conductivity': self.lineEdit_liningThermalConductivity.text(),
            'lining_density': self.lineEdit_liningDensity.text(),
            'lining_specific_heat': self.lineEdit_liningSpecificHeat.text(),

            'figure_size_width': self.lineEdit_figure_size_width.text(),
            'figure_size_height': self.lineEdit_figure_size_height.text(),
            'figure_size_scale': self.lineEdit_figure_size_scale.text(),
        }

        for item in parameters:
            parameters[item] = float(eval(parameters[item]))

        return parameters

    def run_simulation(self):
        d=1

    @staticmethod
    def static_default_parameters():
        parameters = {
            'time_step': 0.5,
            'time_cap': '60*60*2',
            'gas_emissivity': 0.7,
            'vent_mass_flow_rate': 0.0735,

            'specimen_exposed_area': 1,
            'specimen_volume': 0.4,
            'specimen_heat_of_combustion': '19e6',
            'specimen_burning_rate': '0.7/1000./60.',
            'specimen_density': 640,

            'lining_surface_emissivity': 0.7,
            'lining_surface_area': '(4*3+3*1.5+1.5*4)*2',
            'lining_thickness': .05,
            'lining_thermal_conductivity': 1.08997,
            'lining_density': 1700,
            'lining_specific_heat': 820,

            'figure_size_width': 10,
            'figure_size_height': 7.5,
            'figure_size_scale': 0.6,
        }
        return parameters
Beispiel #16
0
class plsplotDlg(QtWidgets.QDialog,Ui_plsplotDialog):
    def __init__(self,parent=None):
        super(plsplotDlg,self).__init__(parent)
        self.setupUi(self)
        self.setWindowFlags(QtCore.Qt.Window |QtCore.Qt.CustomizeWindowHint |
            QtCore.Qt.WindowTitleHint | QtCore.Qt.WindowCloseButtonHint |
            QtCore.Qt.WindowMaximizeButtonHint)
        for x in range(PLS.ncp):
            self.XcomboBox.addItem(str(x+1))
            self.YcomboBox.addItem(str(x+1))
        Lc=DS.Lc[DS.Ic]
        Gc=DS.Gc[DS.Ic]
        Lcy=Lc[Gc]
        for x in Lcy:
            self.responcecomboBox.addItem(str(x))
        self.XcomboBox.setCurrentIndex(0)
        self.YcomboBox.setCurrentIndex(1)
        self.VcheckBox.setChecked(False)
        self.XGcheckBox.setChecked(False)
        self.YGcheckBox.setChecked(False)
        self.XMcheckBox.setChecked(True)
        self.YMcheckBox.setChecked(True)
        self.XcheckBox.setChecked(True)
        self.YcheckBox.setChecked(True)
        self.ApplyButton.clicked.connect(self.redraw)
        self.ResetButton.clicked.connect(self.reset)
        fig=Figure()
        ax=fig.add_subplot(111)
        ax.plot(np.array(0))
        ax.set_xlim([0,1])
        ax.set_ylim([0,1])
        self.addmpl(fig)
    def redraw(self):
        fig=Figure()
        ax=fig.add_subplot(111)
        c1=self.XcomboBox.currentIndex()
        c2=self.YcomboBox.currentIndex()
        ny=self.responcecomboBox.currentIndex()
        Yname=self.responcecomboBox.currentText()
        Ts=DS.Ts[DS.Ir]
        Gc=DS.Gc[DS.Ic]
        Cr=DS.Cr[DS.Ir]
        Cr=Cr[-Ts]
        Lr=DS.Lr[DS.Ir] 
        Lr=Lr[-Ts]
        Lc=DS.Lc[DS.Ic]
        Lcx=Lc[-Gc]
        Lcy=Lc[Gc]
        Cc=DS.Cc[DS.Ic]
        Ccx=Cc[-Gc]
        ncx=len(Lcx)
        ncy=len(Lcy)
        nr=len(Lr)
        Y=DS.Raw.loc[DS.Ir,DS.Ic][Lcy]
        Y=Y[-Ts]
        if(self.fittedradioButton.isChecked()):
            Y=Y.values
            ax.scatter(Y[:,ny],PLS.Ys[:,ny],color=Cr,alpha=0.3,marker='o')
            xmin,xmax=ax.get_xlim()
            ax.set_ylim([xmin,xmax])
            ax.set_xlabel(Yname+' measured')
            ax.set_ylabel(Yname+' fitted')
            ax.set_title('Fitted vs. Measured Plot for '+Yname+ ' : Model '+str(PLS.ncp)+' components')
            ax.add_line(Line2D([xmin,xmax],[xmin,xmax],color='red'))
            if self.VcheckBox.isChecked():
                for i in range(nr):
                    ax.annotate(Lr[i],(Y[:,c1][i],PLS.Ys[:,c1][i]))
        elif(self.allfittedradioButton.isChecked()):
            Yc=pd.melt(pd.DataFrame(PLS.Yc))
            Ysc=pd.melt(pd.DataFrame(PLS.Ysc))
            df=pd.DataFrame(dict(X=Yc['value'],Y=Ysc['value'],point=Yc['variable']))
            np.random.seed(nr)
            groups=df.groupby('point')
            colors=pd.tools.plotting._get_standard_colors(len(groups),color_type='random')
            ax.set_prop_cycle(cycler('color',colors))
            ax.margins(0.05)
            for group in groups:
                ax.plot(group[1].X,group[1].Y, marker='o',linestyle='',ms=5,label=Lcy[group[0]])
            ax.set_xlabel('Measured Data')
            ax.set_ylabel('Fitted Data')
            Dmin=min([Yc.value.min(),Ysc.value.min()])
            Dmax=max([Yc.value.max(),Ysc.value.max()])
            ax.set_xlim([Dmin,Dmax])
            ax.set_ylim([Dmin,Dmax])
            ax.set_title('Measured vs. Fitted Data')
            ax.add_line(Line2D([Dmin,Dmax],[Dmin,Dmax],color='red')) 
            ax.legend(loc='best') 
        elif(self.coefficientradioButton.isChecked()):
            B=PLS.B[:,ny]
            ind=np.array(range(1,ncx+1))
            if(ncx>30):
                itick=np.linspace(0,ncx-1,20).astype(int)
                ltick=Lcx[itick]
            else:
                itick=ind
                ltick=Lcx
            if self.CcheckBox.isChecked():
                vcol=Ccx
            else:
                vcol='blue'
            ax.bar(ind,B,align='center',color=vcol)
            ax.set_xticks(itick)
            ax.set_xticklabels(ltick,rotation='vertical')
            ax.set_xlim([0,ncx+1])
            ax.set_xlabel('Variables') 
            ax.set_ylabel('Coefficients') 
            ax.set_title('Coefficients for '+Yname+' : Model '+str(PLS.ncp)+' components')
        elif self.wiradioButton.isChecked():
            WS=np.dot(PLS.WS,np.linalg.inv(PLS.C))
            txt=range(1,ncx+1)
            if self.VcheckBox.isChecked():
                txt=Lcx
            vcol=ncx*['blue']
            if self.CcheckBox.isChecked():
                vcol=Ccx
            ax.scatter(WS[:,c1],WS[:,c2],alpha=0.3,color=vcol,s=30,marker='o')
            ax.scatter(0,0,color='red',s=60,marker='+')
            for i in range(ncx):
                ax.annotate(txt[i],(WS[:,c1][i],WS[:,c2][i]))
            lim=[WS.min(),0, WS.max()]
            vlim=max(abs(np.array(lim)))*1.2
            ax.set_xlim([np.copysign(vlim,lim[0]),np.copysign(vlim,lim[2])])
            ax.set_ylim([np.copysign(vlim,lim[0]),np.copysign(vlim,lim[2])])
            ax.set_title('Weight Plot: Model with '+str(PLS.ncp)+' components')
            ax.set_xlabel('W*'+str(c1+1))
            ax.set_ylabel('W*'+str(c2+1))
        elif(self.qiradioButton.isChecked()):
            txt=range(1,ncy+1)
            if self.VcheckBox.isChecked():
                txt=Lcx
            vcol=ncy*['blue']
            if self.CcheckBox.isChecked():
                vcol=Ccx
            ax.scatter(PLS.Q[:,c1],PLS.Q[:,c2],alpha=0.3,color=vcol,s=30,marker='o')
            ax.scatter(0,0,color='red',s=60,marker='+')
            for i in range(ncy):
                ax.annotate(txt[i],(PLS.Q[:,c1][i],PLS.Q[:,c2][i]))
            lim=[PLS.Q.min(),0,PLS.Q.max()]
            vlim=max(abs(np.array(lim)))*1.2
            ax.set_xlim([np.copysign(vlim,lim[0]),np.copysign(vlim,lim[2])])
            ax.set_ylim([np.copysign(vlim,lim[0]),np.copysign(vlim,lim[2])])
            ax.set_title('Loading Plot: Model with '+str(PLS.ncp)+' components')
            ax.set_xlabel('C'+str(c1+1))
            ax.set_ylabel('C'+str(c2+1))
        elif(self.wqiradioButton.isChecked()):
            WS=np.dot(PLS.WS,np.linalg.inv(PLS.C))
            txtc=range(1,ncy+1)
            txtw=range(1,ncx+1)
            if self.VcheckBox.isChecked():
                txt=DS.Lc[DS.Ic]
                txtc=txt[DS.Gc[DS.Ic]]
                txtw=txt[-DS.Gc[DS.Ic]]
            vcolc=ncy*['red']
            vcolw=ncx*['blue']
            if self.CcheckBox.isChecked():
                vcol=DS.Cc[DS.Ic]
                vcolc=vcol[DS.Gc[DS.Ic]]
                vcolw=vcol[-DS.Gc[DS.Ic]]
            ax.scatter(PLS.Q[:,c1],PLS.Q[:,c2],alpha=0.3,color=vcolc,s=30,marker='o')
            ax.scatter(WS[:,c1],WS[:,c2],alpha=0.3,color=vcolw,s=30,marker='o')
            ax.scatter(0,0,color='red',s=60,marker='+')
            for i in range(ncy):
                ax.annotate(txtc[i],(PLS.Q[:,c1][i],PLS.Q[:,c2][i]))
            for i in range(ncx):
                ax.annotate(txtw[i],(WS[:,c1][i],WS[:,c2][i]))
            lim=[PLS.Q.min(),0,PLS.Q.max(),WS.max(),WS.min()]
            vlim=max(abs(np.array(lim)))*1.2
            ax.set_xlim([np.copysign(vlim,lim[0]),np.copysign(vlim,lim[2])])
            ax.set_ylim([np.copysign(vlim,lim[0]),np.copysign(vlim,lim[2])])
            ax.set_title('Loading Plot: Model with '+str(PLS.ncp)+' components')
            ax.set_xlabel('W* Q'+str(c1+1))
            ax.set_ylabel('W* Q'+str(c2+1))
        elif(self.ht2radioButton.isChecked()):
            ind=np.array(range(1,nr+1))
            colors=[]
            for i in range(nr):
                colors.append('blue')
                if(PLS.HT2[i]>PLS.T95):
                    colors[i]='red'
            if(nr>30):
                itick=np.linspace(0,nr-1,20).astype(int)
                ltick=Lr[itick]
            else:
                itick=ind
                ltick=Lr
            ax.bar(ind,PLS.HT2,align='center',color=colors)
            ax.set_xticks(itick)
            ax.set_xticklabels(ltick,rotation='vertical')
            ax.set_xlabel('Object') 
            ax.set_title('Hoteling T2 for '+str(PLS.ncp)+' components model')
            ax.axhline(y=PLS.T95,color='red',linewidth=1.5,zorder=0)
            ax.annotate('95%',xy=(0,PLS.T95),color='red')
            ax.axhline(y=PLS.T99,color='red',linewidth=1.5,zorder=0)
            ax.annotate('99%',xy=(0,PLS.T99),color='red')
            ax.set_ylim([0,1.2*max([PLS.T99,max(PLS.HT2)])])
            ax.set_xlim([0,nr+1])
        elif(self.scoreradioButton.isChecked()):
            st2=PLS.T.var(axis=0)
            alpha=(100-self.alphaspinBox.value())/100
            fn=np.array(range(1,(PLS.ncp+1)))
            fd=(nr+1)-fn
            fn =tuple(fn)
            fd =tuple(fd)
            falpha=sp.stats.f.ppf(alpha,fn,fd)
            colors=[]
            txt=[]
            for i in range(nr):
                txt.append('')
                colors.append('blue')
                rp=(PLS.T[:,c1][i])**2/st2[c1]+(PLS.T[:,c2][i])**2/st2[c2]
                if(rp>falpha[PLS.ncp-1]):
                    colors[i]='red'
                    txt[i]=Lr[i]
            ax.scatter(PLS.T[:,c1],PLS.T[:,c2],alpha=0.3,color=colors,s=30,marker='o')
            ax.scatter(0,0,color='red',s=60,marker='+')
            for i in range(nr):
                ax.annotate(txt[i],(PLS.T[:,c1][i],PLS.T[:,c2][i]))
            lim=[PLS.T.min(),0,PLS.T.max()]
            vlim=max(abs(np.array(lim)))*1.2
            ax.set_xlim([np.copysign(vlim,lim[0]),np.copysign(vlim,lim[2])])
            ax.set_ylim([np.copysign(vlim,lim[0]),np.copysign(vlim,lim[2])])
            ax.set_title('Score Plot: Model with '+str(PLS.ncp)+' components')
            ax.set_xlabel('Comp.'+str(c1+1)+' ('+str(round(st2[c1]/PLS.ncp*100,2))+'%)')
            ax.set_ylabel('Comp.'+str(c2+1)+' ('+str(round(st2[c2]/PLS.ncp*100,2))+'%)')
            ella=Ellipse(xy=(0,0),width=2*math.sqrt(falpha[PLS.ncp-1]*st2[c1]),height=2*math.sqrt(falpha[PLS.ncp-1]*st2[c2]),color='red')
            ella.set_facecolor('none')
            ax.add_artist(ella)
            alpha=alpha*100
            ax.annotate(str(round(alpha,0))+'%',xy=(0,-math.sqrt(falpha[PLS.ncp-1]*st2[c2])),color='red')
        elif(self.spexradioButton.isChecked()):
            T95=sp.stats.chi2.ppf(0.95,nr)/(nr-PLS.ncp)
            T99=sp.stats.chi2.ppf(0.99,nr)/(nr-PLS.ncp)
            ind=np.array(range(1,nr+1))
            if(nr>30):
                itick=np.linspace(0,nr-1,20).astype(int)
                ltick=Lr[itick]
            else:
                itick=ind
                ltick=Lr
            ax.bar(ind,PLS.SPEX,color='blue')
            ax.set_xticks(itick)
            ax.set_xticklabels(ltick,rotation='vertical')
            ax.set_xlabel('X Variables') 
            ax.set_ylabel('SPE X') 
            ax.set_title('SPE X for '+str(PLS.ncp)+' components model')
            ax.set_ylim([0,1.2*max([max(PLS.SPEX),T99])])
            ax.set_xlim([0,max(itick)])
            ax.axhline(y=T95,color='red',linewidth=1.5)
            ax.annotate('95%',xy=(0,T95),color='red')
            ax.axhline(y=T99,color='red',linewidth=1.5)
            ax.annotate('99%',xy=(0,T99),color='red')
        elif self.speyradioButton.isChecked():
            T95=sp.stats.chi2.ppf(0.95,nr)/(nr-PLS.ncp)
            T99=sp.stats.chi2.ppf(0.99,nr)/(nr-PLS.ncp)
            ind=np.array(range(1,nr+1))
            if(nr>30):
                itick=np.linspace(0,nr-1,20).astype(int)
                ltick=Lr[itick]
            else:
                itick=ind
                ltick=Lr
            ax.bar(ind,PLS.SPEY,color='blue')
            ax.set_xticks(itick)
            ax.set_xticklabels(ltick,rotation='vertical')
            ax.set_xlabel('Y Variables') 
            ax.set_ylabel('SPE Y') 
            ax.set_title('SPE Y for '+str(PLS.ncp)+' components model')
            ax.set_ylim([0,1.2*max([max(PLS.SPEY),T99])])
            ax.set_xlim([0,max(itick)])
            ax.axhline(y=T95,color='red',linewidth=1.5)
            ax.annotate('95%',xy=(0,T95),color='red')
            ax.axhline(y=T99,color='red',linewidth=1.5)
            ax.annotate('99%',xy=(0,T99),color='red')
        elif self.cvradioButton.isChecked():
            X0=pd.DataFrame(PLS.Xc)
            Y0=pd.DataFrame(PLS.Yc)
            np.random.seed(nr)
            kf=sk.model_selection.KFold(n_splits=self.segmentspinBox.value(),shuffle=True)
            Ycv=pd.DataFrame(0,index=range(nr),columns=range(ncy))
            for train, test in kf.split(X0):
                 X=X0.iloc[train,:]
                 Y=Y0.iloc[train,:]
                 Xm,Ym,Sx,Sy,Xc,Yc,SSX,SSY,P,C,U,T,Q,W,WS,B,Ysc,Ys,R2,SPEX,SPEY,HT2,VIP,T95,T99=pls_model(X,Y,PLS.ncp,False,False,False)
                 X_test=X0.iloc[test,:]
                 Ycv.iloc[test,:]=np.dot(X_test,B)
            Y0=Y0.values
            PLS.Ycv=Ycv.values
            Ycvs=PLS.Ym+PLS.Sy*Ycv.T
            PLS.Ycvs=Ycvs.T.values
            Y=PLS.Ym+PLS.Sy*Y0.T
            Y=Y.T
            if self.CcheckBox.isChecked():
                vcol=Cr
            else:
                vcol='red'
            ax.scatter(Y[:,ny],PLS.Ycvs.iloc[:,ny],color=vcol,alpha=0.3,marker='o')
            if self.VcheckBox.isChecked():       
                for i in range(nr):
                    ax.annotate(Lr[i],(Y[:,ny][i],PLS.Ycvs.iloc[:,ny][i]))
            ax.set_xlabel(Yname+' measured')
            ax.set_ylabel(Yname+' CV predicted')
            xmin,xmax=ax.get_xlim()
            ax.set_ylim([xmin,xmax])
            ax.set_title('CV Predicted vs. Measured Plot for '+Yname+' : Model '+str(PLS.ncp)+' components')
            ax.add_line(Line2D([xmin,xmax],[xmin,xmax],color='red'))
        elif self.turadioButton.isChecked():
            if self.CcheckBox.isChecked():
                vcol=Cr
            else:
                vcol='red'
            ax.scatter(PLS.T[:,c1],PLS.U[:,c1],color=vcol,alpha=0.3,marker='o')
            if self.VcheckBox.isChecked():       
                for i in range(nr):
                    ax.annotate(Lr[i],(PLS.T[:,c1][i],PLS.U[:,c1][i]))
            ax.set_xlabel('T('+str(c1+1)+')')
            ax.set_ylabel('U('+str(c1+1)+')')
            ax.set_title('T vs. U Plot')
        elif self.vipradioButton.isChecked():
            ind=np.array(range(1,ncx+1))
            if(ncx>30):
                itick=np.linspace(0,ncx-1,20).astype(int)
                ltick=Lcx[itick]
            else:
                itick=ind
                ltick=Lcx
            colors=[]
            for i in range(ncx):
                colors.append('blue')
                if(PLS.VIP[i]>1):
                    colors[i]='red'
            ax.bar(ind,PLS.VIP,align='center',color=colors)
            ax.set_xticks(itick)
            ax.set_xticklabels(ltick,rotation='vertical')
            ax.set_xlim([0,max(itick)+1])
            ax.set_xlabel('Variables') 
            ax.yaxis.grid()
            ax.set_title('Variable Importance Plot (VIP): Model '+str(PLS.ncp)+' components')
        if self.XcheckBox.isChecked():
            if self.XlineEdit.text():
                ax.set_xlabel(self.XlineEdit.text())
        else:
            ax.set_xlabel('')
        if self.YcheckBox.isChecked():
            if self.YlineEdit.text():
                ax.set_ylabel(self.YlineEdit.text())
        else:
            ax.set_ylabel('')
        if self.XGcheckBox.isChecked():
            ax.xaxis.grid(True)
        else:
            ax.xaxis.grid(False)
        if self.YGcheckBox.isChecked():
            ax.yaxis.grid(True)
        else:
            ax.yaxis.grid(False)
        if not self.XMcheckBox.isChecked():    
            ax.tick_params(axis='x',which='both',bottom='off',top='off',labelbottom='off')
        if not self.YMcheckBox.isChecked():
            ax.tick_params(axis='y',which='both',left='off',right='off',labelleft='off')
        self.rmmpl()
        self.addmpl(fig)        
    def reset(self):
        self.XcomboBox.setCurrentIndex(0)
        self.YcomboBox.setCurrentIndex(1)
        self.XGcheckBox.setChecked(False)
        self.YGcheckBox.setChecked(False)
        self.XMcheckBox.setChecked(False)
        self.YMcheckBox.setChecked(True)
        self.XcheckBox.setChecked(True)
        self.YcheckBox.setChecked(True)
        self.XlineEdit.setText('')
        self.YlineEdit.setText('')
        self.update()        
    def addmpl(self, fig):
        self.canvas=FigureCanvas(fig)
        self.mplvl.addWidget(self.canvas)
        self.canvas.draw()
        self.toolbar=NavigationToolbar(self.canvas, 
                self.mplwindow, coordinates=True)
        self.mplvl.addWidget(self.toolbar)
    def rmmpl(self,):
        self.mplvl.removeWidget(self.canvas)
        self.canvas.close()
        self.mplvl.removeWidget(self.toolbar)
        self.toolbar.close()
Beispiel #17
0
class regplotDlg(QtWidgets.QDialog, Ui_regplot):
    def __init__(self, parent=None):
        super(regplotDlg, self).__init__(parent)
        self.setupUi(self)
        self.setWindowFlags(QtCore.Qt.Window | QtCore.Qt.CustomizeWindowHint
                            | QtCore.Qt.WindowTitleHint
                            | QtCore.Qt.WindowCloseButtonHint
                            | QtCore.Qt.WindowMaximizeButtonHint)
        self.TcheckBox.setChecked(True)
        self.XGcheckBox.setChecked(False)
        self.YGcheckBox.setChecked(False)
        self.XMcheckBox.setChecked(True)
        self.YMcheckBox.setChecked(True)
        self.XcheckBox.setChecked(True)
        self.YcheckBox.setChecked(True)
        self.ApplyButton.clicked.connect(self.redraw)
        self.ResetButton.clicked.connect(self.reset)
        fig = Figure()
        ax = fig.add_subplot(111)
        ax.plot(np.array(0))
        ax.set_xlim([0, 1])
        ax.set_ylim([0, 1])
        self.addmpl(fig)

    def redraw(self):
        fig = Figure()
        ax = fig.add_subplot(111)
        data = DS.Raw.loc[DS.Ir, DS.Ic]
        Gc = DS.Gc[DS.Ic]
        Ts = DS.Ts[DS.Ir]
        X = data.loc[:, -Gc]
        Y = data.loc[:, Gc]
        Yname = Y.columns[0]
        X = X[-Ts]
        Y = Y[-Ts]
        Lr = DS.Lr[DS.Ir]
        Cr = DS.Cr[DS.Ir]
        Lr = Lr[-Ts]
        Cr = Cr[-Ts]
        nr = len(Lr)
        Lc = DS.Lc[DS.Ic]
        Lcx = Lc[-Gc]
        ncx = len(Lcx)
        Lcy = Lc[Gc]
        scoretr = REG.lr.score(X, Y)
        np.random.seed(REG.seed)
        if self.coeffradioButton.isChecked():
            B = REG.lr.coef_
            ind = np.array(range(1, ncx + 1))
            if (ncx > 30):
                itick = np.linspace(0, ncx - 1, 20).astype(int)
                ltick = Lcx[itick]
            else:
                itick = ind
                ltick = Lcx
            vcol = []
            for i in ind:
                if (REG.pS[i] < 0.05): vcol.append('red')
                else: vcol.append('blue')
            ax.bar(ind,
                   B,
                   align='center',
                   color=vcol,
                   label='Score: {:04.2f}'.format(scoretr))
            ax.errorbar(ind, B, yerr=REG.dB[1:], fmt='o', ecolor='green')
            ax.set_xticks(itick)
            ax.set_xticklabels(ltick, rotation='vertical')
            ax.set_xlim([0, ncx + 1])
            ax.set_xlabel('Variables')
            ax.set_ylabel('Coefficients')
            ax.set_title('Regression for ' + Lcy[0] +
                         ' Intercept:  {:10.4f}-{:10.4f}-{:10.4f}'.format(
                             REG.lr.intercept_ -
                             REG.dB[0], REG.lr.intercept_, REG.lr.intercept_ +
                             REG.dB[0]))
            ax.set_ylabel('Coefficients')
            ax.legend(loc='upper left')
        elif self.resradioButton.isChecked():
            Yfit = REG.lr.predict(X)
            Q = np.squeeze(Y.values) - Yfit
            ind = range(1, nr + 1)
            vcol = nr * ['blue']
            if self.CcheckBox.isChecked():
                vcol = Cr
            ax.scatter(ind, Q, alpha=0.3, color=vcol, s=30, marker='o')
            if self.VcheckBox.isChecked():
                for i in range(nr):
                    ax.annotate(Lr[i], (ind[i], Q[i]))
            lim = [Q.min(), 0, Q.max()]
            vlim = max(abs(np.array(lim))) * 1.1
            ax.set_xlim([0, nr + 2])
            ax.set_ylim([np.copysign(vlim, lim[0]), np.copysign(vlim, lim[2])])
            ax.set_title('Residual Plot: Model with for ' + Lcy[0])
            ax.set_xlabel('Point Index')
            ax.set_ylabel('Residuals')
        elif self.cvradioButton.isChecked():
            scores = sk.model_selection.cross_val_score(
                REG.lr, X, Y, cv=self.segmentspinBox.value())
            REG.Ycv = sk.model_selection.cross_val_predict(
                REG.lr, X, Y, cv=self.segmentspinBox.value())
            if self.CcheckBox.isChecked():
                vcol = Cr
            else:
                vcol = 'red'
            ax.scatter(Y, REG.Ycv, color=vcol, alpha=0.3, marker='o')
            if self.VcheckBox.isChecked():
                for i in range(nr):
                    ax.annotate(Lr[i], (Y.values[i], REG.Ycv[i]))
            ax.set_xlabel(Yname + ' measured')
            ax.set_ylabel(Yname + ' CV predicted')
            xmin, xmax = ax.get_xlim()
            ax.set_ylim([xmin, xmax])
            ax.set_title('CV Predicted Plot for ' + Lcy[0] +
                         ' : Score {:04.2f}'.format(scores.mean()))
            ax.add_line(Line2D([xmin, xmax], [xmin, xmax], color='red'))
        elif self.dispersionradioButton.isChecked():
            fig = plt.figure()
            ax = fig.add_subplot(111)
            cax = ax.matshow(REG.dism)
            fig.colorbar(cax, format='%.2E')
            ax.set_title('Trace = {:10.4E}'.format(np.trace(REG.dism)))
        elif self.leverageradioButton.isChecked():
            Ftable = surtabDlg.launch(None)
            if len(np.shape(Ftable)) == 0: return ()
            if np.argmax(Ftable['X axis'].values) == np.argmax(
                    Ftable['Y axis'].values):
                QtWidgets.QMessageBox.critical(
                    self, 'Error', "Two variables on the same axis",
                    QtWidgets.QMessageBox.Ok)
                return ()
            fig = plt.figure()
            ax = fig.add_subplot(111)
            npts = 20
            xname = Ftable[(Ftable['X axis'] == True).values].index[0]
            yname = Ftable[(Ftable['Y axis'] == True).values].index[0]
            cname = Ftable[(Ftable['Constant'] == True).values].index.tolist()
            cvalue = Ftable.loc[(Ftable['Constant'] == True).values, 'value']
            x = np.linspace(float(Ftable['min'][xname]),
                            float(Ftable['max'][xname]), npts)
            y = np.linspace(float(Ftable['min'][yname]),
                            float(Ftable['max'][yname]), npts)
            px = []
            py = []
            for i in range(npts):
                for j in range(npts):
                    px.append(x[i])
                    py.append(y[j])
            mx = pd.DataFrame({xname: px, yname: py})
            xtitle = ''
            for i in range(len(cname)):
                xtitle = xtitle + cname[i] + ' = ' + str(
                    cvalue.values.tolist()[i])
                mx[cname[i]] = np.ones(npts**2) * float(cvalue[i])
            mx = mx[Lcx]
            pz = np.diag(np.dot(np.dot(mx, REG.dism), mx.T))
            px = np.array(px)
            py = np.array(py)
            pz = np.array(pz)
            z = plt.mlab.griddata(px, py, pz, x, y, interp='linear')
            plt.contour(x, y, z, 15, linewidths=0.5, colors='k')
            plt.contourf(x, y, z, 15, cmap=plt.cm.rainbow)
            plt.colorbar()
            ax.set_xlabel(xname)
            ax.set_ylabel(yname)
            ax.set_title(xtitle)
            ax.set_xlim([px.min(), px.max()])
            ax.set_ylim([py.min(), py.max()])
        elif self.surfaceradioButton.isChecked():
            Ftable = surtabDlg.launch(None)
            if len(np.shape(Ftable)) == 0: return ()
            if np.argmax(Ftable['X axis'].values) == np.argmax(
                    Ftable['Y axis'].values):
                QtWidgets.QMessageBox.critical(
                    self, 'Error', "Two variables on the same axis",
                    QtWidgets.QMessageBox.Ok)
                return ()
            fig = plt.figure()
            ax = fig.add_subplot(111)
            npts = 100
            xname = Ftable[(Ftable['X axis'] == True).values].index[0]
            yname = Ftable[(Ftable['Y axis'] == True).values].index[0]
            cname = Ftable[(Ftable['Constant'] == True).values].index.tolist()
            cvalue = Ftable.loc[(Ftable['Constant'] == True).values, 'value']
            x = np.linspace(float(Ftable['min'][xname]),
                            float(Ftable['max'][xname]), npts)
            y = np.linspace(float(Ftable['min'][yname]),
                            float(Ftable['max'][yname]), npts)
            px = []
            py = []
            for i in range(npts):
                for j in range(npts):
                    px.append(x[i])
                    py.append(y[j])
            mx = pd.DataFrame({xname: px, yname: py})
            xtitle = ''
            for i in range(len(cname)):
                xtitle = xtitle + cname[i] + ' = ' + str(
                    cvalue.values.tolist()[i])
                mx[cname[i]] = np.ones(npts**2) * float(cvalue[i])
            mx = mx[Lcx]
            pz = REG.lr.predict(mx)
            px = np.array(px)
            py = np.array(py)
            pz = np.array(pz)
            z = plt.mlab.griddata(px, py, pz, x, y, interp='linear')
            plt.contour(x, y, z, 15, linewidths=0.5, colors='k')
            plt.contourf(x, y, z, 15, cmap=plt.cm.rainbow)
            plt.colorbar()
            ax.set_xlabel(xname)
            ax.set_ylabel(yname)
            ax.set_title(xtitle)
            ax.set_xlim([px.min(), px.max()])
            ax.set_ylim([py.min(), py.max()])
        if self.TcheckBox.isChecked():
            if self.TlineEdit.text():
                ax.set_title(self.TlineEdit.text())
        else:
            ax.set_title('')
        if self.XcheckBox.isChecked():
            if self.XlineEdit.text():
                ax.set_xlabel(self.XlineEdit.text())
        else:
            ax.set_xlabel('')
        if self.YcheckBox.isChecked():
            if self.YlineEdit.text():
                ax.set_ylabel(self.YlineEdit.text())
        else:
            ax.set_ylabel('')
        if self.XGcheckBox.isChecked():
            ax.xaxis.grid(True)
        else:
            ax.xaxis.grid(False)
        if self.YGcheckBox.isChecked():
            ax.yaxis.grid(True)
        else:
            ax.yaxis.grid(False)
        if not self.XMcheckBox.isChecked():
            ax.tick_params(axis='x',
                           which='both',
                           bottom='off',
                           top='off',
                           labelbottom='off')
        if not self.YMcheckBox.isChecked():
            ax.tick_params(axis='y',
                           which='both',
                           left='off',
                           right='off',
                           labelleft='off')
        try:
            self.rmmpl()
        except:
            pass
        self.addmpl(fig)

    def reset(self):
        self.TcheckBox.setChecked(True)
        self.XGcheckBox.setChecked(False)
        self.YGcheckBox.setChecked(False)
        self.XMcheckBox.setChecked(True)
        self.YMcheckBox.setChecked(True)
        self.XcheckBox.setChecked(True)
        self.YcheckBox.setChecked(True)
        self.update()

    def addmpl(self, fig):
        self.canvas = FigureCanvas(fig)
        self.mplvl.addWidget(self.canvas)
        self.canvas.draw()
        self.toolbar = NavigationToolbar(self.canvas,
                                         self.mplwindow,
                                         coordinates=True)
        self.mplvl.addWidget(self.toolbar)

    def rmmpl(self, ):
        self.mplvl.removeWidget(self.canvas)
        self.canvas.close()
        self.mplvl.removeWidget(self.toolbar)
        self.toolbar.close()
Beispiel #18
0
class plsweightDlg(QtWidgets.QDialog, Ui_plsweightDialog):
    def __init__(self, parent=None):
        super(plsweightDlg, self).__init__(parent)
        self.setupUi(self)
        self.setWindowFlags(QtCore.Qt.Window | QtCore.Qt.CustomizeWindowHint
                            | QtCore.Qt.WindowTitleHint
                            | QtCore.Qt.WindowCloseButtonHint
                            | QtCore.Qt.WindowMaximizeButtonHint)
        for x in range(PLS.ncp):
            self.componentcomboBox.addItem(str(x + 1))
        Lr = DS.Lr[DS.Ir]
        Ts = DS.Ts[DS.Ir]
        for x in Lr[-Ts]:
            self.firstpointcomboBox.addItem(str(x))
            self.secondpointcomboBox.addItem(str(x))
        self.firstpointcomboBox.setCurrentIndex(0)
        self.secondpointcomboBox.setCurrentIndex(1)
        Gr = DS.Gr[DS.Ir]
        Gr = Gr[-Ts]
        Gr = pd.Series(Gr)
        for x in Gr.groupby(Gr).groups.keys():
            self.firstgroupcomboBox.addItem(str(x))
            self.secondgroupcomboBox.addItem(str(x))
        self.VcheckBox.setChecked(False)
        self.XGcheckBox.setChecked(False)
        self.YGcheckBox.setChecked(False)
        self.XMcheckBox.setChecked(True)
        self.YMcheckBox.setChecked(True)
        self.XcheckBox.setChecked(True)
        self.YcheckBox.setChecked(True)
        self.ApplyButton.clicked.connect(self.redraw)
        self.ResetButton.clicked.connect(self.reset)
        fig = Figure()
        ax = fig.add_subplot(111)
        ax.plot(np.array(0))
        ax.set_xlim([0, 1])
        ax.set_ylim([0, 1])
        self.addmpl(fig)

    def redraw(self):
        nw = self.componentcomboBox.currentIndex()
        fig = Figure()
        ax = fig.add_subplot(111)
        Gc = DS.Gc[DS.Ic]
        Lc = DS.Lc[DS.Ic]
        Lcx = Lc[-Gc]
        Ccx = DS.Cc[DS.Ic]
        Ccx = Ccx[-Gc]
        ncx = len(Lcx)
        WS = np.dot(PLS.WS, np.linalg.inv(PLS.C))
        ind = np.array(range(1, ncx + 1))
        if (ncx > 30):
            itick = np.linspace(0, ncx - 1, 20).astype(int)
            ltick = Lcx[itick]
        else:
            itick = ind
            ltick = Lcx
        ax.set_xticks(itick)
        ax.set_xticklabels(ltick, rotation='vertical')
        ax.set_xlabel('Variable')
        if self.CcheckBox.isChecked():
            cl = Ccx
        else:
            cl = ncx * ['blue']
        ax.set_ylabel('Variable Contributon on Weight ' + str(nw + 1))
        ax.set_xlim([0, ncx + 1])
        if self.pointradioButton.isChecked():
            p1 = self.firstpointcomboBox.currentIndex()
            p2 = self.secondpointcomboBox.currentIndex()
            x1 = np.array(PLS.Xc[p1, :])
            x2 = np.array(PLS.Xc[p2, :])
            lx = WS[:, nw] * (x1 - x2)
            ax.bar(ind, lx, align='center', width=0.8, color=cl)
            ax.set_title('Contribution of object ' +
                         self.firstpointcomboBox.currentText() +
                         ' vs. object ' +
                         self.secondpointcomboBox.currentText() +
                         ' to Weight ' + str(nw + 1))
        elif self.averageradioButton.isChecked():
            p1 = self.firstpointcomboBox.currentIndex()
            x1 = np.array(PLS.Xc[p1, :])
            lx = WS[:, nw] * x1
            ax.bar(ind, lx, align='center', width=0.8, color=cl)
            ax.set_title('Contribution of object ' +
                         self.firstpointcomboBox.currentText() +
                         ' to Weight ' + str(nw + 1))
        elif self.groupradioButton.isChecked():
            g1 = self.firstgroupcomboBox.currentIndex()
            g2 = self.secondgroupcomboBox.currentIndex()
            Gr = DS.Gr[DS.Ir]
            Gr = Gr[-DS.Ts[DS.Ir]]
            df = pd.DataFrame(PLS.Xc).groupby(Gr).mean()
            Xa = df.values
            x1 = np.array(Xa[g1, :])
            x2 = np.array(Xa[g2, :])
            lx = WS[:, nw] * (x1 - x2)
            ax.bar(ind, lx, align='center', width=0.8, color=cl)
            ax.set_title('Contribution of Group ' +
                         self.firstgroupcomboBox.currentText() +
                         ' vs. Group ' +
                         self.secondgroupcomboBox.currentText() +
                         ' to Weight ' + str(nw + 1))
        if self.XcheckBox.isChecked():
            if self.XlineEdit.text():
                ax.set_xlabel(self.XlineEdit.text())
        else:
            ax.set_xlabel('')
        if self.YcheckBox.isChecked():
            if self.YlineEdit.text():
                ax.set_ylabel(self.YlineEdit.text())
        else:
            ax.set_ylabel('')
        if self.XGcheckBox.isChecked():
            ax.xaxis.grid(True)
        else:
            ax.xaxis.grid(False)
        if self.YGcheckBox.isChecked():
            ax.yaxis.grid(True)
        else:
            ax.yaxis.grid(False)
        if not self.XMcheckBox.isChecked():
            ax.tick_params(axis='x',
                           which='both',
                           bottom='off',
                           top='off',
                           labelbottom='off')
        if not self.YMcheckBox.isChecked():
            ax.tick_params(axis='y',
                           which='both',
                           left='off',
                           right='off',
                           labelleft='off')
        self.rmmpl()
        self.addmpl(fig)

    def reset(self):
        self.firstpointcomboBox.setCurrentIndex(0)
        self.secondpointcomboBox.setCurrentIndex(1)
        self.XGcheckBox.setChecked(False)
        self.YGcheckBox.setChecked(False)
        self.XMcheckBox.setChecked(False)
        self.YMcheckBox.setChecked(True)
        self.XcheckBox.setChecked(True)
        self.YcheckBox.setChecked(True)
        self.XlineEdit.setText('')
        self.YlineEdit.setText('')
        self.update()

    def addmpl(self, fig):
        self.canvas = FigureCanvas(fig)
        self.mplvl.addWidget(self.canvas)
        self.canvas.draw()
        self.toolbar = NavigationToolbar(self.canvas,
                                         self.mplwindow,
                                         coordinates=True)
        self.mplvl.addWidget(self.toolbar)

    def rmmpl(self, ):
        self.mplvl.removeWidget(self.canvas)
        self.canvas.close()
        self.mplvl.removeWidget(self.toolbar)
        self.toolbar.close()
Beispiel #19
0
class bivariategroupDlg(QtWidgets.QDialog, Ui_bivariategroup):
    def __init__(self, parent=None):
        super(bivariategroupDlg, self).__init__(parent)
        self.setupUi(self)
        self.setWindowFlags(QtCore.Qt.Window | QtCore.Qt.CustomizeWindowHint
                            | QtCore.Qt.WindowTitleHint
                            | QtCore.Qt.WindowCloseButtonHint
                            | QtCore.Qt.WindowMaximizeButtonHint)
        self.XcomboBox.addItem('Auto')
        self.Y2comboBox.addItem('None')
        self.XcomboBox.addItems(DS.Lc[DS.Ic])
        self.Y1comboBox.addItems(DS.Lc[DS.Ic])
        self.Y2comboBox.addItems(DS.Lc[DS.Ic])
        self.XcomboBox.setCurrentIndex(0)
        self.Y1comboBox.setCurrentIndex(0)
        self.Y2comboBox.setCurrentIndex(0)
        self.PcheckBox.setChecked(True)
        self.XGcheckBox.setChecked(True)
        self.YGcheckBox.setChecked(True)
        self.XMcheckBox.setChecked(True)
        self.YMcheckBox.setChecked(True)
        self.XcheckBox.setChecked(True)
        self.YcheckBox.setChecked(True)
        self.ApplyButton.clicked.connect(self.redraw)
        self.ResetButton.clicked.connect(self.reset)
        self.farwardButton.clicked.connect(self.farward)
        self.backwardButton.clicked.connect(self.backward)
        fig = Figure()
        ax = fig.add_subplot(111)
        ax.plot(np.array(0))
        ax.set_xlim([0, 1])
        ax.set_ylim([0, 1])
        self.addmpl(fig)
        self.id_group = 0

    def redraw(self):
        if self.XcomboBox.currentText()==self.Y1comboBox.currentText() or   \
            self.XcomboBox.currentText()==self.Y2comboBox.currentText() :
            QtWidgets.QMessageBox.critical(self, 'Error',
                                           "Variables \n must be different !",
                                           QtWidgets.QMessageBox.Ok)
            return ()
        groups = pd.Series(DS.Gr)
        if groups.isnull().all():
            QtWidgets.QMessageBox.critical(self, 'Error',
                                           "Not all groups are defined !",
                                           QtWidgets.QMessageBox.Ok)
            return ()
        groups = groups.astype('int')
        gr_type = [isinstance(e, (int, np.integer)) for e in groups]
        if sum(gr_type) != len(groups):
            QtWidgets.QMessageBox.critical(
                self, 'Error',
                "Groups must be all present \n and must be integers",
                QtWidgets.QMessageBox.Ok)
            return ()
        data = DS.Raw.iloc[DS.Ir, DS.Ic]
        Lr = DS.Lr[DS.Ir]
        Cr = DS.Cr[DS.Ir]
        fig = Figure()
        color = 'blue'
        color1 = 'blue'
        Y1 = data[self.Y1comboBox.currentText()]
        if self.CcheckBox.isChecked():
            color = DS.Cc[self.Y1comboBox.currentIndex() - 1]
        if self.Y2comboBox.currentText() == "None":
            dual = False
            Y2 = Y1
        else:
            dual = True
            Y2 = data[self.Y2comboBox.currentText()]
            if self.CcheckBox.isChecked():
                color1 = DS.Cc[self.Y2comboBox.currentIndex() - 2]
        if self.XcomboBox.currentText() == 'Auto':
            auto = True
            X = Y1
        else:
            auto = False
            X = data[self.XcomboBox.currentText()]
        df = pd.DataFrame({
            'G': groups.values,
            'X': X,
            'Y1': Y1,
            'Y2': Y2,
            'C': Cr,
            'L': Lr
        })
        dfg = df.groupby(['G'])
        ngr = len(dfg)
        if (ngr == 1):
            QtWidgets.QMessageBox.critical(
                self, 'Error',
                'Your data have just one group.\n Slide Show needs more than a single group.',
                QtWidgets.QMessageBox.Ok)
            return ()
        groups = df.G.unique()
        if (auto):
            X = []
            for gr in groups:
                grp = dfg.get_group(gr)
                X = X + list(range(len(grp['X'])))
            df['X'] = X
        if (self.slideradioButton.isChecked()):
            if dual:
                ax = fig.add_subplot(1, 2, 1)
            else:
                ax = fig.add_subplot(1, 1, 1)
            if (self.id_group > ngr - 1):
                QtWidgets.QMessageBox.critical(
                    self, 'Error', 'Highest element of list is reached',
                    QtWidgets.QMessageBox.Ok)
                self.id_group = ngr - 1
                return ()
            if (self.id_group < 0):
                QtWidgets.QMessageBox.critical(
                    self, 'Error', 'Lowest element of list is reached',
                    QtWidgets.QMessageBox.Ok)
                self.id_group = 0
                return ()
            min_X = df['X'].min()
            max_X = df['X'].max()
            min_Y1 = df['Y1'].min()
            max_Y1 = df['Y1'].max()
            min_Y2 = df['Y2'].min()
            max_Y2 = df['Y2'].max()
            M = dfg.get_group(groups[self.id_group])
            ax.set_xlim([min_X, max_X])
            ax.set_ylim([min_Y1, max_Y1])
            if self.PcheckBox.isChecked():
                ax.scatter(M['X'], M['Y1'], marker='o', color=color)
            if self.LcheckBox.isChecked():
                ax.plot(M['X'], M['Y1'], linestyle='-', color=color)
            ax.xaxis.grid()
            ax.yaxis.grid()
            ax.set_ylabel(self.Y1comboBox.currentText())
            ax.set_xlabel(self.XcomboBox.currentText())
            ax.set_title('Group: ' + str(groups[self.id_group]))
            if dual:
                ax1 = fig.add_subplot(1, 2, 2)
                ax1.set_xlim([min_X, max_X])
                ax1.set_ylim([min_Y2, max_Y2])
                if self.PcheckBox.isChecked():
                    ax1.scatter(M['X'], M['Y2'], marker='o', color=color1)
                if self.LcheckBox.isChecked():
                    ax1.plot(M['X'], M['Y2'], linestyle='-', color=color1)
                ax1.xaxis.grid()
                ax1.yaxis.grid()
                ax1.set_title('Group:' + str(groups[self.id_group]))
                ax1.set_xlabel(self.XcomboBox.currentText())
                ax1.set_ylabel(self.Y2comboBox.currentText())
        if (self.conditioningradioButton.isChecked()):
            if dual:
                ax = fig.add_subplot(1, 2, 1)
            else:
                ax = fig.add_subplot(1, 1, 1)
            for key, grp in df.groupby(['G']):
                if self.CcheckBox.isChecked():
                    vcol = grp['C']
                else:
                    vcol = cm.viridis.colors[int(
                        len(cm.viridis.colors) / ngr * key)]
                if self.PcheckBox.isChecked():
                    ax.scatter(grp['X'],
                               grp['Y1'],
                               color=vcol,
                               marker='o',
                               label=key)
                if self.LcheckBox.isChecked():
                    ax.plot(grp['X'],
                            grp['Y1'],
                            linestyle='-',
                            color=vcol,
                            label=key)
            ax.xaxis.grid()
            ax.yaxis.grid()
            ax.set_xlabel(self.XcomboBox.currentText())
            ax.set_ylabel(self.Y1comboBox.currentText())
            ax.legend(loc='best')
            if dual:
                ax1 = fig.add_subplot(1, 2, 2)
                for key, grp in df.groupby(['G']):
                    if self.CcheckBox.isChecked():
                        vcol = grp['C']
                    else:
                        vcol = cm.viridis.colors[int(
                            len(cm.viridis.colors) / ngr * key)]
                    if self.PcheckBox.isChecked():
                        ax1.scatter(grp['X'],
                                    grp['Y2'],
                                    color=vcol,
                                    marker='o',
                                    label=key)
                    if self.LcheckBox.isChecked():
                        ax1.plot(grp['X'],
                                 grp['Y2'],
                                 linestyle='-',
                                 color=vcol,
                                 label=key)
                ax1.xaxis.grid()
                ax1.yaxis.grid()
                ax1.set_xlabel(self.XcomboBox.currentText())
                ax1.set_ylabel(self.Y2comboBox.currentText())
                ax1.legend(loc='best')
        if (self.scatteradioButton.isChecked()):
            ax = fig.add_subplot(1, 1, 1)
            if dual:
                ax1 = ax.twinx()
            for key, grp in df.groupby(['G']):
                if self.CcheckBox.isChecked():
                    vcol = grp['C']
                else:
                    vcol = cm.viridis.colors[int(
                        len(cm.viridis.colors) / ngr * key)]
                if self.PcheckBox.isChecked():
                    ax.scatter(grp['X'],
                               grp['Y1'],
                               color=vcol,
                               marker='o',
                               label=key)
                    if dual:
                        ax1.scatter(grp['X'],
                                    grp['Y2'],
                                    color=vcol,
                                    marker='*',
                                    label=key)
                if self.LcheckBox.isChecked():
                    ax.plot(grp['X'],
                            grp['Y1'],
                            linestyle='-',
                            color=vcol,
                            label=key)
                    if dual:
                        ax1.plot(grp['X'],
                                 grp['Y2'],
                                 linestyle='-',
                                 color=vcol,
                                 label=key)
            ax.set_xlabel(self.XcomboBox.currentText())
            ax.set_ylabel(self.Y1comboBox.currentText())
            if dual:
                ax1.set_ylabel(self.Y2comboBox.currentText())
            ax.legend(loc='best')
        if self.XcheckBox.isChecked():
            if self.XlineEdit.text():
                ax.set_xlabel(self.XlineEdit.text())
                if dual:
                    ax1.set_xlabel(self.XlineEdit.text())
        else:
            ax.set_xlabel('')
            if dual:
                ax1.set_xlabel('')
        if self.XGcheckBox.isChecked():
            ax.xaxis.grid(True)
            if dual:
                ax1.xaxis.grid(True)
        else:
            ax.xaxis.grid(False)
            if dual:
                ax1.xaxis.grid(False)
        if self.XGcheckBox.isChecked():
            ax.xaxis.grid(True)
        if self.YGcheckBox.isChecked():
            ax.yaxis.grid(True)
            if dual:
                ax1.yaxis.grid(True)
        if not self.XMcheckBox.isChecked():
            ax.tick_params(axis='x',
                           which='both',
                           bottom='off',
                           top='off',
                           labelbottom='off')
            if dual:
                ax1.tick_params(axis='x',
                                which='both',
                                bottom='off',
                                top='off',
                                labelbottom='off')
        if not self.YMcheckBox.isChecked():
            ax.tick_params(axis='y',
                           which='both',
                           left='off',
                           right='off',
                           labelleft='off')
            if dual:
                ax1.tick_params(axis='y',
                                which='both',
                                bottom='off',
                                top='off',
                                labelbottom='off')
        self.rmmpl()
        self.addmpl(fig)

    def farward(self):
        self.id_group += 1
        self.redraw()

    def backward(self):
        self.id_group -= 1
        self.redraw()

    def reset(self):
        self.XcomboBox.setCurrentIndex(0)
        self.Y1comboBox.setCurrentIndex(0)
        self.Y2comboBox.setCurrentIndex(0)
        self.PcheckBox.setChecked(True)
        self.LcheckBox.setChecked(False)
        self.XGcheckBox.setChecked(True)
        self.YGcheckBox.setChecked(True)
        self.XMcheckBox.setChecked(True)
        self.YMcheckBox.setChecked(True)
        self.XcheckBox.setChecked(True)
        self.YcheckBox.setChecked(True)
        self.XlineEdit.setText('')
        self.YlineEdit.setText('')
        self.TlineEdit.setText('')
        self.update()

    def addmpl(self, fig):
        self.canvas = FigureCanvas(fig)
        self.mplvl.addWidget(self.canvas)
        self.canvas.draw()
        self.toolbar = NavigationToolbar(self.canvas,
                                         self.mplwindow,
                                         coordinates=True)
        self.mplvl.addWidget(self.toolbar)

    def rmmpl(self, ):
        self.mplvl.removeWidget(self.canvas)
        self.canvas.close()
        self.mplvl.removeWidget(self.toolbar)
        self.toolbar.close()
Beispiel #20
0
class bivariaterowDlg(QtWidgets.QDialog, Ui_bivariaterow):
    def __init__(self, parent=None):
        super(bivariaterowDlg, self).__init__(parent)
        self.setupUi(self)
        self.setWindowFlags(QtCore.Qt.Window | QtCore.Qt.CustomizeWindowHint
                            | QtCore.Qt.WindowTitleHint
                            | QtCore.Qt.WindowCloseButtonHint
                            | QtCore.Qt.WindowMaximizeButtonHint)
        self.XcomboBox.addItem('Auto')
        self.YcomboBox.addItem('All')
        self.XcomboBox.addItems(DS.Lr[DS.Ir])
        self.YcomboBox.addItems(DS.Lr[DS.Ir])
        self.GcomboBox.addItem('All')
        groups = pd.Series(DS.Gr[DS.Ir])
        groups = groups.dropna()
        groups = groups.values
        groups = groups.astype('str')
        groups = np.unique(groups)
        groups = np.sort(groups)
        self.GcomboBox.addItems(groups)
        self.XcomboBox.setCurrentIndex(0)
        self.YcomboBox.setCurrentIndex(0)
        self.XcomboBox.setCurrentIndex(0)
        self.YcomboBox.setCurrentIndex(0)
        self.PcheckBox.setChecked(True)
        self.XGcheckBox.setChecked(True)
        self.YGcheckBox.setChecked(True)
        self.XMcheckBox.setChecked(True)
        self.YMcheckBox.setChecked(True)
        self.XcheckBox.setChecked(True)
        self.YcheckBox.setChecked(True)
        self.ApplyButton.clicked.connect(self.redraw)
        self.ResetButton.clicked.connect(self.reset)
        fig = Figure()
        ax = fig.add_subplot(111)
        ax.plot(np.array(0))
        ax.set_xlim([0, 1])
        ax.set_ylim([0, 1])
        self.addmpl(fig)

    def redraw(self):
        if self.XcomboBox.currentText() == self.YcomboBox.currentText():
            QtWidgets.QMessageBox.critical(self, 'Error',
                                           'Variables \n must be different !',
                                           QtWidgets.QMessageBox.Ok)
            return ()
        if (self.XcomboBox.currentText() == 'Auto') and (
                self.YcomboBox.currentText()
                == 'All') and not self.scatterradioButton.isChecked():
            QtWidgets.QMessageBox.critical(
                self, 'Error',
                "You have to select two rows \n for this kind of plot!",
                QtWidgets.QMessageBox.Ok)
            return ()
        data = DS.Raw.iloc[DS.Ir, DS.Ic]
        data = data.assign(Lr=DS.Lr[DS.Ir])
        data = data.assign(Cr=DS.Cr[DS.Ir])
        data = data.assign(Gr=DS.Gr[DS.Ir])
        if (self.XcomboBox.currentText() !=
                'Auto') and (self.YcomboBox.currentText() != 'All'):
            data = data.loc[[
                self.XcomboBox.currentText(),
                self.YcomboBox.currentText()
            ]]
        elif (self.XcomboBox.currentText() !=
              'Auto') and (self.YcomboBox.currentText() == 'All'):
            QtWidgets.QMessageBox.critical(self, 'Error', "Select two rows!",
                                           QtWidgets.QMessageBox.Ok)
            return ()
        elif (self.XcomboBox.currentText()
              == 'Auto') and (self.YcomboBox.currentText() != 'All'):
            QtWidgets.QMessageBox.critical(self, 'Error',
                                           "Use Univariate plot!",
                                           QtWidgets.QMessageBox.Ok)
            return ()
        Nnan = data.isnull().isnull().all().all()
        data = data.T.dropna()
        data = data.T
        Lr = data['Lr'].values
        Cr = data['Cr'].values
        Gr = data['Gr'].values
        data = data.drop('Lr', axis=1)
        data = data.drop('Cr', axis=1)
        data = data.drop('Gr', axis=1)
        if data.dtypes.all() == 'float' and data.dtypes.all() == 'int':
            QtWidgets.QMessageBox.critical(self,'Error',"Some values are not numbers!",\
                                           QtWidgets.QMessageBox.Ok)
            return ()
        if (self.XcomboBox.currentText() !=
                'Auto') and (self.YcomboBox.currentText() != 'All'):
            if data.shape[0] != 2:
                QtWidgets.QMessageBox.critical(self,'Error',"Raw labels must be different",\
                                               QtWidgets.QMessageBox.Ok)
                return ()
            x = data.loc[self.XcomboBox.currentText()].values
            y = data.loc[self.YcomboBox.currentText()].values
        fig = Figure()
        ax = fig.add_subplot(111)
        color = 'blue'
        if self.scatterradioButton.isChecked():
            if (self.XcomboBox.currentText() !=
                    'Auto') and (self.YcomboBox.currentText() != 'All'):
                if self.PcheckBox.isChecked():
                    ax.scatter(x, y, marker='o', color=Cr)
                if self.LcheckBox.isChecked():
                    ax.plot(x, y, color='blue')
                if self.VcheckBox.isChecked():
                    for i, txt in enumerate(Lr):
                        ax.annotate(txt, (x[i], y[i]))
                ax.set_xlabel(self.XcomboBox.currentText())
                ax.set_ylabel(self.YcomboBox.currentText())
            else:
                nr, nc = data.shape
                Lc = DS.Lc[DS.Ic]
                x = range(1, nc + 1)
                color = Cr
                if self.GcheckBox.isChecked():
                    groups = Gr
                    ngr = len(np.unique(groups))
                    color = []
                    for key in groups:
                        color.append(cm.viridis.colors[int(
                            (len(cm.viridis.colors) - 1) / ngr * key)])
                for i in range(nr):
                    y = data.iloc[i, :]
                    col = color[i]
                    if self.GcomboBox.currentText() == 'All':
                        if self.PcheckBox.isChecked():
                            ax.scatter(x, y, marker='o', color=col)
                        if self.LcheckBox.isChecked():
                            ax.plot(x, y, color=col)
                    else:
                        if int(self.GcomboBox.currentText()) == groups[i]:
                            if self.PcheckBox.isChecked():
                                ax.scatter(x, y, marker='o', color=col)
                            if self.LcheckBox.isChecked():
                                ax.plot(x, y, color=col)
                if (nc > 30):
                    itick = np.linspace(0, nc - 1, 20).astype(int)
                    ltick = Lc[itick]
                else:
                    itick = x
                    ltick = Lc
                ax.set_xlim([0, nc + 2])
                ax.set_xticks(itick)
                ax.set_xticklabels(ltick, rotation='vertical')
        if self.ellipseradioButton.isChecked():

            def plot_ellipse(x, y, nstd=2, ax=None, **kwargs):
                def eigsorted(cov):
                    vals, vecs = np.linalg.eigh(cov)
                    order = vals.argsort()[::-1]
                    return vals[order], vecs[:, order]

                pos = (x.mean(), y.mean())
                cov = np.cov(x, y).tolist()
                vals, vecs = eigsorted(cov)
                theta = np.degrees(np.arctan2(*vecs[:, 0][::-1]))
                width, height = 2 * nstd * np.sqrt(vals)
                ellip = Ellipse(xy=pos,
                                width=width,
                                height=height,
                                angle=theta,
                                fill=False,
                                **kwargs)
                ax.add_artist(ellip)
                return ellip

            for j in range(1, 4):
                plot_ellipse(x, y, j, ax)
            ax.scatter(x, y)
            ax.set_xlabel(self.XcomboBox.currentText())
            ax.set_ylabel(self.YcomboBox.currentText())
            ax.set_title('Ellipse for 1,2,3 times the Standard Deviation')
        if self.boxcoxradioButton.isChecked():
            if (not (x > 0).all()) and (not (y > 0).all()):
                QtWidgets.QMessageBox.critical(self,'Error',"Values must be strictly positive",\
                                               QtWidgets.QMessageBox.Ok)
                return ()
            CBC = np.zeros(50)
            vlambda = np.linspace(-2, 2, 50)
            for i in range(50):
                trans_x = stats.boxcox(x, vlambda[i])
                CBC[i] = np.corrcoef(trans_x, y)[0, 1]
            if self.PcheckBox.isChecked():
                ax.scatter(vlambda, CBC, marker='o', color=color)
            if self.LcheckBox.isChecked():
                ax.plot(vlambda, CBC, color=color)
            ax.set_xlabel('Lambda')
            ax.set_ylabel('Correlation Coefficient')
        if self.histogramradioButton.isChecked():
            cx = 'blue'
            cy = 'red'
            xm = x.mean()
            ym = y.mean()
            xstd = x.std()
            ystd = y.std()
            dy = (ym - 3 * ystd) - (xm + 3 * xstd)
            dx = (xm - 3 * xstd) - (ym + 3 * ystd)
            if (dy > 0) | (dx > 0):
                x = sk.preprocessing.normalize(x.reshape(1, -1),
                                               norm='l2',
                                               axis=1,
                                               copy=True,
                                               return_norm=False)
                y = sk.preprocessing.normalize(y.reshape(1, -1),
                                               norm='l2',
                                               axis=1,
                                               copy=True,
                                               return_norm=False)
                x = x.ravel()
                y = y.ravel()
                ax.set_xlabel('Normalized Quantities')
            iqr = np.percentile(x, [75, 25])
            iqr = iqr[0] - iqr[1]
            n = x.size
            dx = abs(max((x.max(), y.max())) - min((x.min(), y.min())))
            nbins = int(np.floor(dx / (2 * iqr) * n**(1 / 3))) + 1
            if nbins > self.spinBox.value():
                self.spinBox.setValue(nbins)
            else:
                nbins = self.spinBox.value()
            bins = np.linspace(min((x.min(), y.min())), max(
                (x.max(), y.max())), nbins)
            ax.hist(x,
                    bins=bins,
                    histtype='bar',
                    color=cx,
                    alpha=0.5,
                    orientation='vertical',
                    label=str(self.XcomboBox.currentText()))
            ax.hist(y,
                    bins=bins,
                    histtype='bar',
                    color=cy,
                    alpha=0.5,
                    orientation='vertical',
                    label=str(self.YcomboBox.currentText()))
            box = ax.get_position()
            ax.set_position([box.x0, box.y0, box.width * 0.8, box.height])
            ax.legend(bbox_to_anchor=(1, 1),
                      loc='upper left',
                      borderaxespad=0.2)
        if Nnan:
            ax.annotate('{:04.2f} NaN'.format(Nnan),
                        xy=(0.80, 0.95),
                        xycoords='figure fraction')
        if self.XcheckBox.isChecked():
            if self.XlineEdit.text():
                ax.set_xlabel(self.XlineEdit.text())
        else:
            ax.set_xlabel('')
        if self.YcheckBox.isChecked():
            if self.YlineEdit.text():
                ax.set_ylabel(self.YlineEdit.text())
        else:
            ax.set_ylabel('')
        if self.XGcheckBox.isChecked():
            ax.xaxis.grid(True)
        if self.YGcheckBox.isChecked():
            ax.yaxis.grid(True)
        if self.TlineEdit.text():
            ax.set_title(self.TlineEdit.text())
        if not self.XMcheckBox.isChecked():
            ax.tick_params(axis='x',
                           which='both',
                           bottom='off',
                           top='off',
                           labelbottom='off')
        if not self.YMcheckBox.isChecked():
            ax.tick_params(axis='y',
                           which='both',
                           left='off',
                           right='off',
                           labelleft='off')
        self.rmmpl()
        self.addmpl(fig)

    def reset(self):
        self.XcomboBox.setCurrentIndex(0)
        self.YcomboBox.setCurrentIndex(0)
        self.YcomboBox.setCurrentIndex(0)
        self.PcheckBox.setChecked(True)
        self.LcheckBox.setChecked(False)
        self.XGcheckBox.setChecked(True)
        self.YGcheckBox.setChecked(True)
        self.XMcheckBox.setChecked(True)
        self.YMcheckBox.setChecked(True)
        self.XcheckBox.setChecked(True)
        self.YcheckBox.setChecked(True)
        self.XlineEdit.setText('')
        self.YlineEdit.setText('')
        self.TlineEdit.setText('')
        self.update()

    def addmpl(self, fig):
        self.canvas = FigureCanvas(fig)
        self.mplvl.addWidget(self.canvas)
        self.canvas.draw()
        self.toolbar = NavigationToolbar(self.canvas,
                                         self.mplwindow,
                                         coordinates=True)
        self.mplvl.addWidget(self.toolbar)

    def rmmpl(self, ):
        self.mplvl.removeWidget(self.canvas)
        self.canvas.close()
        self.mplvl.removeWidget(self.toolbar)
        self.toolbar.close()
Beispiel #21
0
class WidgetMatplot(
        QWidget, ):
    def __init__(self, ):
        super().__init__()
        self.layout = QVBoxLayout()
        self.setLayout(self.layout)
        # defualt figure
        self.figure_default = Figure()
        ax = self.figure_default.add_subplot(111)
        x = np.arange(0., 2 * np.pi, 0.1)
        ax.plot(x, np.sin(x))
        ax.text(0.5, 0.0, 'This is a plot area')
        # setup Widgets
        self.add_plot(self.figure_default)
        #self.canvas = FigureCanvas(self.figure_default)
        #self.layout.addWidget(self.canvas)
        #self.toolbar = NavigationToolbar(self.canvas,
        #        self, coordinates =True)
        #self.layout.addWidget(self.toolbar)
        self.axes = None  #this is defined when figure/plot is added

    def draw_axes(self, x, y, label=None, fmt=None):
        """
        Instead of replace whole figure,
        add a line in existing axes  
        
        Seems to be not working !! 
        """
        if fmt:
            self.axes.plot(x, y, fmt, label=label)
        else:
            self.axes.plot(x, y, label=label)

    def reset_plot(self, ):
        # show default figure
        self.rm_plot()
        self.add_plot(self.figure_default)

    def add_plot(self, fig):
        # add a figure in the canvas.
        # figure is external
        # add canvas and toolbar to layout
        # To replace it one have to remove previous one first.
        self.axes = fig.get_axes()[0]  # suppose only one axes..
        self.canvas = FigureCanvas(fig)
        self.layout.addWidget(self.canvas)  # add a canvas widget into layout
        self.canvas.draw()
        self.toolbar = NavigationToolbar(self.canvas, self, coordinates=True)
        self.layout.addWidget(self.toolbar)

    def rm_plot(self, ):
        # remove canvas and toolbar
        self.layout.removeWidget(self.canvas)
        self.canvas.close()
        #self.canvas.setParent(None)
        self.layout.removeWidget(self.toolbar)
        self.toolbar.close()
        #self.toolbar.setParent(None)

    def add_plot_by_data(self,
                         list_of_data_to_plot,
                         show_grid=False,
                         yscale='linear',
                         xlabel='c.m. angle[deg]',
                         ylabel='mb'):
        """
        Parameters
        ----------
        list_of_data_to_plot : list of dictionary to plot
            [plot1={'x': array ,'y':array,'yerr': array,
                    'label' : legend txt, 'fmt': matplotlib fmt},
             plot2={},..]


        show_grid : TYPE, optional
            DESCRIPTION. The default is False.
        yscale : TYPE, optional
            DESCRIPTION. The default is 'linear'.
        xlabel : TYPE, optional
            DESCRIPTION. The default is 'c.m. angle[deg]'.
        ylabel : TYPE, optional
            DESCRIPTION. The default is 'mb'.

        Returns
        -------
        None.

        """
        # remove previous plot
        self.rm_plot()
        fig = Figure()
        ax = fig.add_subplot(111)
        for p in list_of_data_to_plot:
            try:  # check whether a yerr exits in data
                yerr = p['yerr']
                plot_err = True
            except:
                plot_err = False
            if plot_err:  #error exist
                ax.errorbar(p['x'],
                            p['y'],
                            yerr=yerr,
                            fmt=p['fmt'],
                            label=p['label'])
            else:
                ax.plot(p['x'], p['y'], p['fmt'], label=p['label'])
        ax.legend()
        ax.set_xlabel(xlabel)
        ax.set_ylabel(ylabel)
        ax.set_yscale(yscale)
        if show_grid:
            ax.grid()
        self.add_plot(fig)
        return
    def plotALot(
            self,
            gene_list,  # list of genes to plot
            savedir="",
            title="images",
            grid=(4, 8),  # grid to plot for each figure
            figsize=(16, 9),
            dpi=300,
    ):
        """
        plot a lot of intensity maps
        from a list of genes
        """
        genes_per_plot = grid[0] * grid[1]
        num_plots, remainder = divmod(len(gene_list), (genes_per_plot))
        # add an extra plot if
        # number of genes is not perfectly divisible by number of plots
        if remainder != 0:
            num_plots += 1

        for img_num in range(len(self.img_arrays)):

            # set up index for number of genes already plotted
            # ------------------------------------------------
            reordered_idx = 0

            for plot_num in range(num_plots):

                # set up figure canvas
                # --------------------
                fig = Figure(figsize=figsize, dpi=dpi)
                canvas = FigCanvas(fig)
                fig.set_canvas(canvas)

                for gridpos in range(genes_per_plot):

                    # check if we have reached end of gene list
                    # -----------------------------------------
                    if reordered_idx == len(gene_list) - 1:
                        break

                    # create temporary axes reference
                    # -------------------------------
                    ax = fig.add_subplot(grid[0], grid[1], gridpos + 1)

                    # plot the current gene
                    # ---------------------
                    array_idx = self.gene_index_dict[gene_list[reordered_idx]]
                    ax.imshow(self.img_arrays[img_num][array_idx, ...],
                              cmap="hot")
                    ax.set_title(gene_list[reordered_idx])

                    # increment gene index
                    # --------------------
                    reordered_idx += 1

                fig.suptitle(title + f"\n{self.hdf5_file_list[img_num]}"
                             f"\n({plot_num + 1} of {num_plots})")
                fig.tight_layout(rect=(0, 0, 1, .94))

                # save the plot
                # -------------
                savename = (
                    f"image_{img_num + 1}_{title.replace(' ','_')}"
                    f"_{plot_num + 1}of{num_plots}_{self.time_str}.png")
                if not os.path.exists(savedir):
                    os.mkdir(savedir)
                fig.savefig(os.path.join(savedir, savename), dpi=dpi)

                # close the canvas
                # ----------------
                canvas.close()
                fig.clear()
Beispiel #23
0
class Main(QMainWindow, Ui_MainWindow):
    def __init__(self, ):  #provide values for attributes at runtime
        super(Main, self).__init__()
        self.setupUi(self)
        self.pushBrowse.clicked.connect(self.selectFile)
        self.pushBrowse_2.clicked.connect(self.selectmlFile)
        self.pushApply.clicked.connect(self.apply)
        self.pushRun.clicked.connect(self.run)
        self.HomeDirectory = os.getcwd()  #saves the primary working directory
        self.directory = os.listdir(self.HomeDirectory)
        self.saveBttn.clicked.connect(self.file_save)
        self.actionOpen.triggered.connect(self.file_open)
        self.actionReset.triggered.connect(self.plots_close)
        self.message = 0
        #self.UI = []
        self.reset = 0

        #check User input for correct .csv file
        self.inputFile.setValidator(validator)
        self.inputFile.textChanged.connect(self.check_state)
        self.inputFile.textChanged.emit(self.inputFile.text())
        #check User input for correct .csv file for ml
        self.mlData.setValidator(validator)
        self.mlData.textChanged.connect(self.check_state)
        self.mlData.textChanged.emit(self.mlData.text())
        #shaft speed
        self.shaftSpeed.setValidator(regexp_checkdouble)
        self.shaftSpeed.textChanged.connect(self.check_state)
        self.shaftSpeed.textChanged.emit(self.shaftSpeed.text())
        #Num of rolling elements
        self.numberofElements.setValidator(regexp_checkint)
        self.numberofElements.textChanged.connect(self.check_state)
        self.numberofElements.textChanged.emit(self.numberofElements.text())
        #diameter of rolling elements
        self.diameterofElements.setValidator(regexp_checkdouble)
        self.diameterofElements.textChanged.connect(self.check_state)
        self.diameterofElements.textChanged.emit(
            self.diameterofElements.text())
        #pitch diameter
        self.pitchDiameter.setValidator(regexp_checkdouble)
        self.pitchDiameter.textChanged.connect(self.check_state)
        self.pitchDiameter.textChanged.emit(self.pitchDiameter.text())
        #Contact angle
        self.contactAngle.setValidator(regexp_checkdouble)
        self.contactAngle.textChanged.connect(self.check_state)
        self.contactAngle.textChanged.emit(self.contactAngle.text())
        #Frequency Acoustic
        self.samFreqAcst.setValidator(regexp_checkdouble)
        self.samFreqAcst.textChanged.connect(self.check_state)
        self.samFreqAcst.textChanged.emit(self.samFreqAcst.text())
        #Frequency Accelerometer
        """self.samFreqac.setValidator(regexp_checkdouble)
        self.samFreqac.textChanged.connect(self.check_state)
        self.samFreqac.textChanged.emit(self.samFreqac.text())"""

    def check_state(self, *args, **kwargs):
        #this function is changes the color of the lineedit fields depending on state
        sender = self.sender()
        validator = sender.validator()
        state = validator.validate(sender.text(), 0)[0]
        if state == QValidator.Acceptable:
            color = '#c4df9b'  # green
        elif state == QtGui.QValidator.Intermediate:
            color = '#fff79a'  # yellow
        else:
            color = '#f6989d'  # red
        sender.setStyleSheet('QLineEdit { background-color: %s }' % color)

    #creates a dictionary from the saved CSV
    def file_open(self):
        #function called when the open file action in triggered. Creates a dictionary from a CSV file.
        filename = QFileDialog.getOpenFileName()[0]
        reader = csv.reader(open(filename, 'r'))
        d = {}
        for row in reader:
            k, v = row
            d[k] = v

        print(d)

        self.setTextInfile(d)
        return True

    #used the dicitonary created above to assign saved variables to input parameters
    def setTextInfile(self, d):
        self.inputName.setText(d['inputName'])
        self.inputApplication.setText(d['inputApplication'])
        self.inputModelnum.setText(d['inputModelnum'])
        self.inputSavingalias.setText(d['inputSavingalias'])
        self.inputFile.setText(d['inputFile'])
        self.mlData.setText(d['mlData'])
        self.horsepower.setText(d['horsepower'])
        self.voltage.setText(d['voltage'])
        self.phase.setText(d['phase'])
        self.shaftnum.setText(d['shaftnum'])
        self.shaftSpeed.setText(d['shaftSpeed'])
        self.numberofElements.setText(d['numberofElements'])
        self.diameterofElements.setText(d['diameterofElements'])
        self.pitchDiameter.setText(d['pitchDiameter'])
        self.contactAngle.setText(d['contactAngle'])
        self.samFreqAcst.setText(d['samFreq'])

    """
    Hmm i wonder if this is used to save the file?
    """

    def file_save(self, ):
        #called when the save btn is clicked. converts user input to dictionary
        #then to dataframe then to csv file.
        dict = CreateSaveDictionary(self.inputName.text(),\
                                    self.inputApplication.text(),\
                                    self.inputModelnum.text(),\
                                    self.inputSavingalias.text(),\
                                    self.inputFile.text(),\
                                    self.mlData.text(), \
                                    self.horsepower.text(), \
                                    self.voltage.text(), \
                                    self.phase.text(), \
                                    self.shaftnum.text(),\
                                    self.shaftSpeed.text(),\
                                    self.numberofElements.text(), \
                                    self.diameterofElements.text(), \
                                    self.pitchDiameter.text(), \
                                    self.contactAngle.text(), \
                                    self.samFreqAcst.text())
        CreateCSVfromDict(dict)

    """
    These functions create the figure widgets and toolbars
    """

    #Accelerometer
    def addmpl(self, fig):
        self.canvas = FigureCanvas(fig)
        #self.canvas.setParent(None)
        self.graph01UI.addWidget(self.canvas)
        #self.canvas.draw()
        self.toolbar = NavigationToolbar(self.canvas,
                                         self.mainspectrum,
                                         coordinates=True)
        #self.toolbar.setParent(None)
        self.graph01UI.addWidget(self.toolbar)

    def addmpl02(self, fig):
        self.canvas = FigureCanvas(fig)
        #self.canvas.setParent(None)
        self.graph02UI.addWidget(self.canvas)
        #self.canvas.draw()
        self.toolbar = NavigationToolbar(self.canvas,
                                         self.mainspectrum,
                                         coordinates=True)
        #self.toolbar.setParent(None)
        self.graph02UI.addWidget(self.toolbar)

    def addgraph11(self, fig):
        self.canvas1 = FigureCanvas(fig)
        self.graph11UI.addWidget(self.canvas1)
        #self.canvas1.draw()
        self.toolbar1 = NavigationToolbar(self.canvas1,
                                          self.graph11,
                                          coordinates=True)
        self.graph11UI.addWidget(self.toolbar1)

    def addgraph12(self, fig):
        self.canvas2 = FigureCanvas(fig)
        self.graph12UI.addWidget(self.canvas2)
        #self.canvas2.draw()
        self.toolbar2 = NavigationToolbar(self.canvas2,
                                          self.graph12,
                                          coordinates=True)
        self.graph12UI.addWidget(self.toolbar2)

    def addgraph21(self, fig):
        self.canvas3 = FigureCanvas(fig)
        self.graph21UI.addWidget(self.canvas3)
        #self.canvas3.draw()
        self.toolbar3 = NavigationToolbar(self.canvas3,
                                          self.graph21,
                                          coordinates=True)
        self.graph21UI.addWidget(self.toolbar3)

    def addgraph22(self, fig):
        self.canvas4 = FigureCanvas(fig)
        self.graph22UI.addWidget(self.canvas4)
        #self.canvas4.draw()
        self.toolbar4 = NavigationToolbar(self.canvas4,
                                          self.graph22,
                                          coordinates=True)
        self.graph22UI.addWidget(self.toolbar4)

    #ACOUSTIC
    def addmpl_2(self, fig):
        self.canvas = FigureCanvas(fig)
        #self.canvas.setParent(None)
        self.graph01UI_2.addWidget(self.canvas)
        #self.canvas.draw()
        self.toolbar = NavigationToolbar(self.canvas,
                                         self.mainspectrum_2,
                                         coordinates=True)
        #self.toolbar.setParent(None)
        self.graph01UI_2.addWidget(self.toolbar)

    def addmpl02_2(self, fig):
        self.canvas = FigureCanvas(fig)
        #self.canvas.setParent(None)
        self.graph02UI_2.addWidget(self.canvas)
        #self.canvas.draw()
        self.toolbar = NavigationToolbar(self.canvas,
                                         self.mainspectrum_2,
                                         coordinates=True)
        #self.toolbar.setParent(None)
        self.graph02UI_2.addWidget(self.toolbar)

    def addgraph11_2(self, fig):
        self.canvas1 = FigureCanvas(fig)
        self.graph11UI_2.addWidget(self.canvas1)
        #self.canvas1.draw()
        self.toolbar1 = NavigationToolbar(self.canvas1,
                                          self.graph11_2,
                                          coordinates=True)
        self.graph11UI_2.addWidget(self.toolbar1)

    def addgraph12_2(self, fig):
        self.canvas2 = FigureCanvas(fig)
        self.graph12UI_2.addWidget(self.canvas2)
        #self.canvas2.draw()
        self.toolbar2 = NavigationToolbar(self.canvas2,
                                          self.graph12_2,
                                          coordinates=True)
        self.graph12UI_2.addWidget(self.toolbar2)

    def addgraph21_2(self, fig):
        self.canvas3 = FigureCanvas(fig)
        self.graph21UI_2.addWidget(self.canvas3)
        #self.canvas3.draw()
        self.toolbar3 = NavigationToolbar(self.canvas3,
                                          self.graph21_2,
                                          coordinates=True)
        self.graph21UI_2.addWidget(self.toolbar3)

    def addgraph22_2(self, fig):
        self.canvas4 = FigureCanvas(fig)
        self.graph22UI_2.addWidget(self.canvas4)
        #self.canvas4.draw()
        self.toolbar4 = NavigationToolbar(self.canvas4,
                                          self.graph22_2,
                                          coordinates=True)
        self.graph22UI_2.addWidget(self.toolbar4)

    #clearly selects file
    def selectFile(self, ):
        self.inputFile.setText(QFileDialog.getOpenFileName()[0])
        self.inputfile = self.inputFile.text()

    #selects file but for a ml data
    def selectmlFile(self, ):
        self.mlData.setText(QFileDialog.getOpenFileName()[0])

    """
    Apply checks user inputs and then assigns them to function parameter variables
    In case the user doesn't supply input for a specific field, default inputs will
    be inserted.
    """

    def apply(self, ):
        if self.inputSavingalias.text() != "":
            self.savingalias = self.inputSavingalias.text() + ".csv"
            self.inputSavingalias.setText(self.savingalias)

        if self.inputFile.text() != "":
            try:
                temp = self.inputFile.text()
                self.FileOfInterest = self.inputFile.text()
            except:
                print("pu")
        else:
            print("no input file selected, using demo file.")
            self.FileOfInterest = "AccelerometerActualDataEdited.csv"
            self.inputFile.setText("/AccelerometerActualDataEdited.csv")

        if self.mlData.text() != "":
            self.TrainingDataFile = self.mlData.text()
            print(self.TrainingDataFile)
        else:
            print("no ML data selected")
            self.TrainingDataFile = "MLSynthesizedData.csv"  #default file location
            self.mlData.setText("/MLSynthesizedData.csv")

        if self.shaftSpeed.text() != "":
            self.n = float(self.shaftSpeed.text())
            print("type n =", type(self.n))
        else:
            print("no shaft speed selected")
            self.n = 2000 / 60
            self.shaftSpeed.setText(str(self.n))

        if self.numberofElements.text() != "":
            self.N = int(self.numberofElements.text())
        else:
            print("Number of elements not specified")
            self.N = 16
            self.numberofElements.setText(str(self.N))

        if self.diameterofElements.text() != "":
            self.Bd = float(self.diameterofElements.text())
        else:
            print("Diameter of elements not specified")
            self.Bd = 0.331 * 254
            self.diameterofElements.setText(str(self.Bd))

        if self.pitchDiameter.text() != "":
            self.Pd = float(self.pitchDiameter.text())
        else:
            print("no pitch diameter specified")
            self.Pd = Pd = 2.815 * 254
            self.pitchDiameter.setText(str(self.Pd))

        if self.contactAngle.text() != "":
            self.phi = float(self.contactAngle.text())
        else:
            print("Contact angle not specified")
            self.phi = (15.17 * 3.14159) / 180
            self.contactAngle.setText(str(self.phi))

        if self.samFreqAcst.text() != "":
            self.SampleFrequency = float(self.samFreqAcst.text())

        else:
            print("no sample frequency specified")
            self.SampleFrequency = 20000
            self.samFreqAcst.setText(str(self.SampleFrequency))

        self.popup = MyPopup("Applied")
        self.popup.setGeometry(QtCore.QRect(100, 100, 400, 200))
        self.popup.show()

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

    def getPlot(self, WhichPlot, plotinfo):
        X = plotinfo[0]
        Y = plotinfo[1]
        xlabel = plotinfo[2]
        ylabel = plotinfo[3]
        Title = plotinfo[4]
        if self.reset != 0:
            WhichPlot.cla()

        WhichPlot.plot(X, Y, c=np.random.rand(3, ))
        WhichPlot.set_xlabel(xlabel, fontsize=12)
        WhichPlot.set_ylabel(ylabel, fontsize=12)
        WhichPlot.set_title(Title)
        WhichPlot.grid(True)
        if self.reset != 0:
            self.canvas.draw()

        return None

    def run(self, ):  #called when run is clicked

        if self.reset == 0:
            #instantiate the figures
            self.fig0 = plt.figure()
            self.sub0 = self.fig0.subplots()

            self.fig1 = plt.figure()
            self.sub1 = self.fig1.subplots()

            self.fig2 = plt.figure()
            self.sub2 = self.fig2.subplots()

            self.fig3 = plt.figure()
            self.sub3 = self.fig3.subplots()

            self.fig4 = plt.figure()
            self.sub4 = self.fig4.subplots()

            self.fig5 = plt.figure()
            self.sub5 = self.fig5.subplots()

            self.fig6 = plt.figure()
            self.sub6 = self.fig6.subplots()

            self.fig7 = plt.figure()
            self.sub7 = self.fig7.subplots()

            self.fig8 = plt.figure()
            self.sub8 = self.fig8.subplots()

            self.fig9 = plt.figure()
            self.sub9 = self.fig9.subplots()

        ###############################################################################
        #begin calling ml functions for processing

        #General
        Name_ID = "1"
        Application = "2"
        ModelNumber = "3"
        SavingAlias = "4"
        AccelerometerDataFilename = 'AccelerometerActualDataEdited.csv'  #filename #main file
        AcousticDataFilename = 'DataOutputMic2Col.csv'  #Wave File Name
        MLDataFilename = "MLSynthesizedData.csv"

        #Motor Characteristics
        Horsepower = "6"
        RatedVoltage = "7"
        ACorDC = "DC"
        NumberOfPolePairs = "9"
        NumberofShafts = "10"

        #Bearing Information
        ShaftSpeed = 300  #Also Used for Motor Characteristics
        NumberOfRollingElements = 3
        DiameterOfRollingElements = 3
        PitchDiameter = .2
        ContactAngle = .2

        #Processing Information
        AccelerometerSamplingFrequency = 14000  #must be an non-zero int or float
        AcousticSamplingFrequency = 30000  #must be an non-zero int or float

        #Microcontroller Information
        #Receive Required Information
        A2DResolution = 16
        VoltageMax = 5
        VoltageMin = 0

        #System/Sensor Known Constants
        AccelerationMax = 50
        AccelerationMin = -50

        #Convert User Inputs into a condensed form
        OnlyUserInput = Inputs2CondensedForm(Name_ID, Application, ModelNumber, SavingAlias,\
                                             AccelerometerDataFilename, AcousticDataFilename, \
                                             MLDataFilename, Horsepower, RatedVoltage, ACorDC, \
                                             NumberOfPolePairs, NumberofShafts, \
                                             ShaftSpeed, NumberOfRollingElements, \
                                             DiameterOfRollingElements,PitchDiameter, ContactAngle, \
                                             AccelerometerSamplingFrequency, AcousticSamplingFrequency)

        SystemInputs = System2CondensedForm(A2DResolution, VoltageMax,
                                            VoltageMin, AccelerationMax,
                                            AccelerationMin)

        #Acquire Accelerometer Actual Data
        time, amp, Voltage, Acceleration = ExtractAccelerometerData(
            OnlyUserInput, SystemInputs)

        #Acquire Acoustic Actual Data
        Channel1Time, Channel1Value, Channe21Time, Channe2Value = ExtractAcousticData(
            OnlyUserInput, SystemInputs)

        #Put All Data into Working Form
        trial = 2  #Select the instance of the data ************needs future work
        AllData = AllData2WorkingForm(OnlyUserInput,SystemInputs,time[trial], \
                                      amp[trial], Voltage[trial], Acceleration[trial],\
                                     Channel1Time,Channel1Value,Channe21Time,Channe2Value)

        #Machine Learning
        TestDF = getTESTDataFrame(AllData)
        TestMatrix = getTESTMatrix(TestDF)
        Xall_train, Yall_train, dataset = GetTrainingData(AllData)
        FinalClassifier = TrainModel(Xall_train, Yall_train)

        #Predict
        prediction, prediction_string = PredictModel(FinalClassifier,
                                                     TestMatrix)
        prediction_proba = PredictProbModel(FinalClassifier, TestMatrix)

        plt.close('all')
        plot0info,plot1info,plot2info,plot3info,plot4info,\
        plot5info,plot6info,plot7info,plot8info,plot9info = getGraphs(AllData)
        #ACCELEROMETER PLOTTING
        self.getPlot(self.sub0, plot0info)  #Raw Data
        self.getPlot(self.sub1, plot1info)  #Raw Data w DC removed
        self.getPlot(self.sub2, plot2info)  #Normalized
        self.getPlot(self.sub3, plot3info)  #FFT
        self.getPlot(self.sub4, plot4info)  #PSD

        #ACOUSTIC PLOTTING
        self.getPlot(self.sub5, plot5info)
        self.getPlot(self.sub6, plot6info)
        self.getPlot(self.sub7, plot7info)
        self.getPlot(self.sub8, plot8info)
        self.getPlot(self.sub9, plot9info)

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

        if self.reset == 0:
            self.addmpl(self.fig0)
            self.addmpl02(self.fig2)
            self.addgraph11(self.fig1)
            self.addgraph12(self.fig3)
            self.addgraph21(self.fig4)
            #self.addgraph22(self.fig4)

            self.addmpl_2(self.fig5)  #Raw Data
            self.addmpl02_2(self.fig7)  #normalized
            self.addgraph11_2(self.fig6)  #Raw Data w DC removed
            self.addgraph12_2(self.fig8)  #FFT
            self.addgraph21_2(self.fig9)  #PSD

        self.BSF.setText(str(truncate(TestDF["BSF"].values[0], 2)))
        self.BPFI.setText(str(truncate(TestDF["BPFI"].values[0], 2)))
        self.BPFO.setText(str(truncate(TestDF["BPFO"].values[0], 2)))
        self.FTF.setText(str(truncate(TestDF["FTF"].values[0], 2)))

        self.earlyEdit.setText(str(truncate(prediction_proba[0, 0], 2)))
        self.suspectEdit.setText(str(truncate(prediction_proba[0, 1], 2)))
        self.normalEdit.setText(str(truncate(prediction_proba[0, 2], 2)))
        """
        self.immEdit.setText(str(Prediction[0,3]))
        self.innerEdit.setText(str(Prediction[0,4]))
        self.rollingEdit.setText(str(Prediction[0,5]))
        self.stageEdit.setText(str(Prediction[0,6]))
        """
        self.reset = 1

    def close_application(self, ):  #self explanitory
        sys.exit()


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

    def updategraphs(self, fig):
        print("made it to update graphs")
        sip.delete(self.canvas)
        sip.delete(self.canvas1)
        sip.delete(self.canvas2)
        sip.delete(self.canvas3)
        sip.delete(self.canvas4)
        #self.spectrumUI.removeWidget(self.canvas)
        self.canvas = FigureCanvas(fig[0])
        self.canvas1 = FigureCanvas(fig[1])
        self.canvas2 = FigureCanvas(fig[2])
        self.canvas3 = FigureCanvas(fig[3])
        self.canvas4 = FigureCanvas(fig[4])

    def rmmpl(self, ):
        print("in rmmpl")
        self.spectrumUI.removeWidget(self.canvas)
        self.canvas.close()
        self.spectrumUI.removeWidget(self.toolbar)
        self.toolbar.close()

        self.graph11UI.removeWidget(self.canvas)
        self.canvas.close()
        self.graph11UI.removeWidget(self.toolbar)
        self.toolbar.close()

        self.graph12UI.removeWidget(self.canvas)
        self.canvas.close()
        self.graph12UI.removeWidget(self.toolbar)
        self.toolbar.close()

        self.graph21UI.removeWidget(self.canvas)
        self.canvas.close()
        self.graph21UI.removeWidget(self.toolbar)
        self.toolbar.close()

        self.graph22UI.removeWidget(self.canvas)
        self.canvas.close()
        self.graph22UI.removeWidget(self.toolbar)
        self.toolbar.close()

    def plots_close(self, ):
        print("made it to plots_close")
        self.spectrumUI.removeWidget(self.canvas)
        sip.delete(self.canvas)
        self.canvas = None
        self.spectrumUI.removeWidget(self.toolbar)
        sip.delete(self.toolbar)
        self.toolbar = None

        self.graph11UI.removeWidget(self.canvas1)
        self.canvas1.close()
        self.graph11UI.removeWidget(self.toolbar1)
        self.toolbar1.close()

        self.graph12UI.removeWidget(self.canvas2)
        self.canvas2.close()
        self.graph12UI.removeWidget(self.toolbar2)
        self.toolbar2.close()

        self.graph21UI.removeWidget(self.canvas3)
        self.canvas3.close()
        self.graph21UI.removeWidget(self.toolbar3)
        self.toolbar3.close()

        self.graph22UI.removeWidget(self.canvas4)
        self.canvas4.close()
        self.graph22UI.removeWidget(self.toolbar4)
        self.toolbar4.close()
class Main(QtWidgets.QMainWindow, Ui_MainWindow):
    def __init__(self):
        QtWidgets.QMainWindow.__init__(self)
        Ui_MainWindow.__init__(self)
        self.setupUi(self)
        self.simpleData.clicked.connect(self.simpleModel)
        self.realData.clicked.connect(self.realModel)
        self.BBdata.clicked.connect(self.BBspec)

        #Notice that an empty figure instance was added to our plotting window in the initialization method.
        #This is necessary because our first call to changefig will try to remove a previously displayed figure,
        #which will throw an error if one is not displayed.
        #The empty figure serves as a placeholder so the changefig method functions properly.
        fig = Figure()
        fig2 = Figure()
        fig3 = Figure()
        fig4 = Figure()
        fig5 = Figure()
        self.addmpl(fig)
        self.addmpl_2(fig2)
        self.addmpl_3(fig3)
        self.addmpl_4(fig4)
        self.addmpl_5(fig5)

    def BBspec(self):
        wavelen = np.linspace(float(self.min.toPlainText()),
                              float(self.max.toPlainText()),
                              float(self.samples.toPlainText())) * 1e-6

        path = 'C:/Users/Joe/Documents/Python Scripts/Scatter Pixel Code/Consolidated_Filter.txt'

        BBT = float(self.BBtemp.toPlainText())
        specIr = BBS.blackBodySpec(wavelen, BBT)
        FilterCoeff = FT.filterTransmission(path, wavelen)

        figx = Figure()
        ax1f1 = figx.add_subplot(111)
        ax1f1.set_title('BBspec Irradiance')
        ax1f1.plot(wavelen, baseUnits(specIr), label='Simple Model')
        ax1f1.plot(wavelen,
                   baseUnits(specIr) * FilterCoeff,
                   label='After Filter')
        ax1f1.set_xlabel('Wavelength (m)')
        ax1f1.set_ylabel('Amp (%s)' % (specIr.units))
        ax1f1.ticklabel_format(style='sci', axis='x', scilimits=(0, 0))
        ax1f1.legend(loc='best')
        ax1f1.grid()
        #print('okay')
        self.rmmpl_3()
        self.addmpl_3(figx)

    def realModel(self):
        ##Load and Store data
        wavelen = np.linspace(float(self.min.toPlainText()),
                              float(self.max.toPlainText()),
                              float(self.samples.toPlainText()))

        absEff0pol, absEff90pol = np.empty(len(wavelen)), np.empty(
            len(wavelen))
        apertureData0pol, apertureData90pol = [], []
        print(wavelen)

        step = 100 / (2 * len(wavelen))
        i = 0
        self.progressBar.setValue(i)
        for wnIdx, wn in enumerate(wavelen):
            absEff0pol[wnIdx], apertureData0poln = SPI.scatterPixelIntegrator(
                wn, 0, 0.65e-3, 0.65e-3)
            apertureData0pol.append(apertureData0poln)
            i = i + step
            self.progressBar.setValue(i)

        for wnIdx, wn in enumerate(wavelen):
            absEff90pol[
                wnIdx], apertureData90poln = SPI.scatterPixelIntegrator(
                    wn, 90, 0.65e-3, 0.65e-3)
            apertureData90pol.append(apertureData90poln)
            i = i + step
            self.progressBar.setValue(i)
        apertureData0pol, apertureData90pol = np.asarray(
            apertureData0pol), np.asarray(apertureData90pol)

        ##Plot 0 deg results averaged##
        PW = np.linspace(0, 10, 41)

        azimuthalSum0pol = []
        for wnIdx, _ in enumerate(wavelen):
            azimuthalSum0pol.append(np.sum(apertureData0pol[wnIdx], axis=0))
        azimuthalSum0pol = np.asarray(azimuthalSum0pol)

        fig0 = Figure()
        ax1f1 = fig0.add_subplot(211)
        ax1f1.set_title('Averaged Aperture Efficenty for each PW angle')
        for azimuthalSum0polN, wn in zip(azimuthalSum0pol, wavelen):
            ax1f1.plot(PW, azimuthalSum0polN / 37, label='%d 0pol' % (wn))

        ax1f1.set_xlabel('PW inclination angle (deg)')
        ax1f1.set_ylabel('Efficientcy')
        ax1f1.legend(loc='best')

        ##Plot 90 deg results azimuthally averaged##
        azimuthalSum90pol = []
        for wnIdx, _ in enumerate(wavelen):
            azimuthalSum90pol.append(np.sum(apertureData90pol[wnIdx], axis=0))
        azimuthalSum90pol = np.asarray(azimuthalSum90pol)

        ax1f2 = fig0.add_subplot(212)
        for azimuthalSum90polN, wn in zip(azimuthalSum90pol, wavelen):
            ax1f2.plot(PW, azimuthalSum90polN / 37, label='%d 90pol' % (wn))

        ax1f2.set_xlabel('PW inclination angle (deg)')
        ax1f2.set_ylabel('Efficientcy')
        ax1f2.legend(loc='best')

        ##Plot Absolute eff for 0/90deg results
        fig1 = Figure()
        ax1f3 = fig1.add_subplot(211)
        ax1f3.set_title('Aperture Efficenty for each wavelength')
        ax1f3.plot(wavelen,
                   absEff0pol,
                   marker='o',
                   linestyle='--',
                   label='Aperture Efficientcy 0pol')
        ax1f3.set_xlabel('Wavelenght (um)')
        ax1f3.set_ylabel('Aperture (%s)' % (ureg.steradian * ureg.meter**2))
        ax1f3.legend(loc='best')

        ax1f4 = fig1.add_subplot(212)
        ax1f4.plot(wavelen,
                   absEff90pol,
                   marker='o',
                   linestyle='--',
                   c='r',
                   label='Aperture Efficientcy 90pol')
        ax1f4.set_xlabel('Wavelenght (um)')
        ax1f4.set_ylabel('Aperture (%s)' % (ureg.steradian * ureg.meter**2))
        ax1f4.legend(loc='best')

        path = 'C:/Users/Joe/Documents/Python Scripts/Scatter Pixel Code/Consolidated_Filter.txt'
        FilterCoeff = FT.filterTransmission(path, wavelen * 1e-6)
        BBT = float(self.BBtemp.toPlainText())
        specIr = BBS.blackBodySpec(wavelen * 1e-6, BBT)

        specPowerAbsorbed0 = FilterCoeff * specIr * absEff0pol * ureg.steradian * ureg.mm**2
        specPowerAbsorbed90 = FilterCoeff * specIr * absEff90pol * ureg.steradian * ureg.mm**2

        fig3 = Figure()
        ax1f5 = fig3.add_subplot(211)
        ax1f5.set_title('Spectral Power Absorbed for each wavelength')
        ax1f5.plot(wavelen,
                   baseUnits(specPowerAbsorbed0),
                   marker='o',
                   linestyle='--',
                   label='Aperture Efficientcy 0pol')
        ax1f5.set_xlabel('Wavelenght (um)')
        #ax1f5.set_ylabel('Amplitude (%s)'%(specPowerAbsorbed0.units))
        ax1f5.legend(loc='best')

        ax1f6 = fig3.add_subplot(212)
        ax1f6.plot(wavelen,
                   baseUnits(specPowerAbsorbed90),
                   marker='o',
                   linestyle='--',
                   label='Aperture Efficientcy 90pol')
        ax1f6.set_xlabel('Wavelenght (um)')
        ax1f6.set_ylabel('Amplitude \n(%s)' %
                         (specPowerAbsorbed90.to_base_units().units))
        ax1f6.legend(loc='best')

        totalPowerAbsorbed0pol = simps(baseUnits(specPowerAbsorbed0),
                                       wavelen * 1e-6)  #(watts)
        totalPowerAbsorbed90pol = simps(baseUnits(specPowerAbsorbed90),
                                        wavelen * 1e-6)  #(watts)

        self.totalPowerAbsorbed0.setText(
            str(totalPowerAbsorbed0pol * ureg.watt))
        self.totalPowerAbsorbed90.setText(
            str(totalPowerAbsorbed90pol * ureg.watt))

        self.rmmpl_2()
        self.addmpl_2(fig0)
        self.rmmpl_4()
        self.addmpl_4(fig1)
        self.rmmpl_5()
        self.addmpl_5(fig3)

    def simpleModel(self):
        PW = np.linspace(0, 10, 41)
        wavelen = np.linspace(float(self.min.toPlainText()),
                              float(self.max.toPlainText()),
                              float(self.samples.toPlainText()))

        AppEff = SEAF.simulateEffectiveApertureEfficiency(
            wavelen, float(self.EOAX.toPlainText()),
            float(self.EOAM.toPlainText()), float(self.PAEX.toPlainText()),
            float(self.PAEM.toPlainText()), PW)

        figx = Figure()
        ax1f1 = figx.add_subplot(111)
        ax1f1.set_title('Simulated Effective Aperture Eff')

        for an, wn in zip(AppEff, wavelen):
            ax1f1.plot(PW, an, label='wavelength %dum' % wn)
        ax1f1.set_xlabel('Inclination angle')
        ax1f1.set_ylabel('Efficiency')
        ax1f1.legend(loc='best')
        ax1f1.grid()

        self.rmmpl()
        self.addmpl(figx)

    def addmpl(self, fig):
        self.canvas = FigureCanvas(fig)
        self.mplvl_0.addWidget(self.canvas)
        self.canvas.draw()
        self.toolbar = NavigationToolbar(self.canvas,
                                         self.mplwindow,
                                         coordinates=True)
        self.mplvl_0.addWidget(self.toolbar)

    def addmpl_2(self, fig):
        self.canvas2 = FigureCanvas(fig)
        self.mplvl_2.addWidget(self.canvas2)
        self.canvas2.draw()
        self.toolbar2 = NavigationToolbar(self.canvas2,
                                          self.mplwindow_2,
                                          coordinates=True)
        self.mplvl_2.addWidget(self.toolbar2)

    def addmpl_3(self, fig):
        self.canvas3 = FigureCanvas(fig)
        self.mplvl_3.addWidget(self.canvas3)
        self.canvas3.draw()
        self.toolbar3 = NavigationToolbar(self.canvas3,
                                          self.mplwindow_3,
                                          coordinates=True)
        self.mplvl_3.addWidget(self.toolbar3)

    def addmpl_4(self, fig):
        self.canvas4 = FigureCanvas(fig)
        self.mplvl_4.addWidget(self.canvas4)
        self.canvas4.draw()
        self.toolbar4 = NavigationToolbar(self.canvas4,
                                          self.mplwindow_4,
                                          coordinates=True)
        self.mplvl_4.addWidget(self.toolbar4)

    def addmpl_5(self, fig):
        self.canvas5 = FigureCanvas(fig)
        self.mplvl_5.addWidget(self.canvas5)
        self.canvas5.draw()
        self.toolbar5 = NavigationToolbar(self.canvas5,
                                          self.mplwindow_5,
                                          coordinates=True)
        self.mplvl_5.addWidget(self.toolbar5)

    def rmmpl(self):
        self.mplvl_0.removeWidget(self.canvas)
        self.canvas.close()
        self.mplvl_0.removeWidget(self.toolbar)
        self.toolbar.close()

    def rmmpl_2(self):
        self.mplvl_2.removeWidget(self.canvas2)
        self.canvas2.close()
        self.mplvl_2.removeWidget(self.toolbar2)
        self.toolbar2.close()

    def rmmpl_3(self):
        self.mplvl_3.removeWidget(self.canvas3)
        self.canvas3.close()
        self.mplvl_3.removeWidget(self.toolbar3)
        self.toolbar3.close()

    def rmmpl_4(self):
        self.mplvl_4.removeWidget(self.canvas4)
        self.canvas4.close()
        self.mplvl_4.removeWidget(self.toolbar4)
        self.toolbar4.close()

    def rmmpl_5(self):
        self.mplvl_5.removeWidget(self.canvas5)
        self.canvas5.close()
        self.mplvl_5.removeWidget(self.toolbar5)
        self.toolbar5.close()

    def scatterPixelIntegrator(self, wavelen, pol, width, height):
        fileMI = 'mode amplitude vs angle %dp0 um %dp0 deg.dat' % (wavelen,
                                                                   pol)
        fileMS = 'mode coefficients %dp0 um %dp0 deg-%d.dat'
        folderM = 'C:\\Users\\Joe\\Documents\\Python Scripts\\Scatter Pixel Code\\mode coefficients %dp0 um %dp0 deg' % (
            wavelen, pol)
        fileS = 'sron_rect_gap%dum_s11_abso400_%s.dat'
        folderS = 'C:\\Users\\Joe\\Documents\\Python Scripts\\Scatter Pixel Code\\Scatter Data'

        ApertureArea = width * height

        ##WORKING
        Amps, modelist, PW, Azi = LMD.loadModalData(fileMI, fileMS, folderM,
                                                    wavelen, pol)
        print('Modal Data Loaded')
        ##WORKING
        S11 = LSD.loadScatterData(fileS, folderS, wavelen)
        print('Scatter Data Loaded')
        ##WORKING
        AbosrbtionChannels = FAM.findAbsorptionModes(S11)
        #    return PW,Azi,ApertureArea,modelist,Amps,AbosrbtionChannels
        print('AbsorbtionChannels got')
        absEff, RRR = IPP.integratePixelPerformance(PW, Azi, ApertureArea,
                                                    modelist, Amps,
                                                    AbosrbtionChannels)
        print('done')
        return absEff, RRR
Beispiel #25
0
class MplCanvas(object):
    """Ultimately, this is a QWidget (as well as a FigureCanvasAgg, etc.)."""
    def __init__(self, brain, width, height, dpi):
        from PyQt5 import QtWidgets
        from matplotlib import rc_context
        from matplotlib.figure import Figure
        from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg
        if brain.separate_canvas:
            parent = None
        else:
            parent = brain.window
        # prefer constrained layout here but live with tight_layout otherwise
        context = nullcontext
        extra_events = ('resize', )
        try:
            context = rc_context({'figure.constrained_layout.use': True})
            extra_events = ()
        except KeyError:
            pass
        with context:
            self.fig = Figure(figsize=(width, height), dpi=dpi)
        self.canvas = FigureCanvasQTAgg(self.fig)
        self.axes = self.fig.add_subplot(111)
        self.axes.set(xlabel='Time (sec)', ylabel='Activation (AU)')
        self.canvas.setParent(parent)
        FigureCanvasQTAgg.setSizePolicy(self.canvas,
                                        QtWidgets.QSizePolicy.Expanding,
                                        QtWidgets.QSizePolicy.Expanding)
        FigureCanvasQTAgg.updateGeometry(self.canvas)
        self.brain = brain
        self.time_func = brain.callbacks["time"]
        for event in ('button_press', 'motion_notify') + extra_events:
            self.canvas.mpl_connect(event + '_event',
                                    getattr(self, 'on_' + event))

    def plot(self, x, y, label, **kwargs):
        """Plot a curve."""
        line, = self.axes.plot(x, y, label=label, **kwargs)
        self.update_plot()
        return line

    def plot_time_line(self, x, label, **kwargs):
        """Plot the vertical line."""
        line = self.axes.axvline(x, label=label, **kwargs)
        self.update_plot()
        return line

    def update_plot(self):
        """Update the plot."""
        leg = self.axes.legend(prop={
            'family': 'monospace',
            'size': 'small'
        },
                               framealpha=0.5,
                               handlelength=1.,
                               facecolor=self.brain._bg_color)
        for text in leg.get_texts():
            text.set_color(self.brain._fg_color)
        with warnings.catch_warnings(record=True):
            warnings.filterwarnings('ignore', 'constrained_layout')
            self.canvas.draw()

    def set_color(self, bg_color, fg_color):
        """Set the widget colors."""
        self.axes.set_facecolor(bg_color)
        self.axes.xaxis.label.set_color(fg_color)
        self.axes.yaxis.label.set_color(fg_color)
        self.axes.spines['top'].set_color(fg_color)
        self.axes.spines['bottom'].set_color(fg_color)
        self.axes.spines['left'].set_color(fg_color)
        self.axes.spines['right'].set_color(fg_color)
        self.axes.tick_params(axis='x', colors=fg_color)
        self.axes.tick_params(axis='y', colors=fg_color)
        self.fig.patch.set_facecolor(bg_color)

    def show(self):
        """Show the canvas."""
        self.canvas.show()

    def close(self):
        """Close the canvas."""
        self.canvas.close()

    def on_button_press(self, event):
        """Handle button presses."""
        # left click (and maybe drag) in progress in axes
        if (event.inaxes != self.axes or event.button != 1):
            return
        self.time_func(event.xdata, update_widget=True, time_as_index=False)

    def clear(self):
        """Clear internal variables."""
        self.close()
        self.axes.clear()
        self.fig.clear()
        self.brain = None
        self.canvas = None

    on_motion_notify = on_button_press  # for now they can be the same

    def on_resize(self, event):
        """Handle resize events."""
        tight_layout(fig=self.axes.figure)
Beispiel #26
0
class SigmaSwiperProgram(QtWidgets.QMainWindow,Ui_sigmaSwiper):
    guest_list = {"ID":[],
                "NAME":[]}
    has_guest = False
    has_graph = False
    data = {"TIME":[],
            "ID":[],
            "NAME":[]}
    graph_x = []
    graph_y = []
    today=datetime.datetime.now().strftime("%m-%d-%y")
    count = 0
    settings = {}
    settings_file=".settings.ini"
    config = configparser.ConfigParser()
    fig = plt.Figure()
    def __init__(self):
        Ui_sigmaSwiper.__init__(self)
        QtWidgets.QMainWindow.__init__(self)
        self.setupUi(self)
        self.submit_button.clicked.connect(self.read_ID)
        self.action_export_list.triggered.connect(self.export_data)
        self.action_load_guest_list.triggered.connect(self.input_guest_list)
        self.action_exit.triggered.connect(qApp.quit)
        self.config.read(self.settings_file)
        self.settings = self.config['settings']
        self.guest_list_check_label.setStyleSheet('color: black')
        self.guest_list_check_label.setText("Load a guest list")
        self.id_input.returnPressed.connect(self.read_ID)
        self.graph_layout = QtWidgets.QVBoxLayout()
        self.graph_widget.setLayout(self.graph_layout)
    def input_guest_list(self):
        fname = QFileDialog.getOpenFileName(None, 'Open Guestlist' , os.path.expanduser('~')+"/Desktop/", "Excel Files (*.xlsx)")
        if fname[0] == '':
            pass
        else:
            excel_file = pd.ExcelFile(fname[0])
            df = excel_file.parse("Sheet1")
            self.guest_list["ID"] = df["ID"].tolist()
            self.guest_list["NAME"] = df["NAME"].tolist()
            self.has_guest = True
            self.guest_list_check_label.setStyleSheet('color: black')
            self.guest_list_check_label.setText("Guest List Loaded")
    def read_ID(self):
        inp = self.id_input.text()
        if len(inp) == 6:
            if self.has_guest:
                if int(inp) not in self.guest_list["ID"]:
                    self.guest_list_check_label.setText("Not On List")
                    self.guest_list_check_label.setStyleSheet('color: red')
                    self.id_input.clear()
                else:
                    self.count +=1
                    self.graph_y.append(self.count)
                    self.data["ID"].append(inp)
                    self.data["TIME"].append(datetime.datetime.now().strftime("%H:%M"))
                    self.graph_x.append(datetime.datetime.now().strftime("%H:%M:%S"))
                    name =self.guest_list["NAME"][self.guest_list["ID"].index(int(inp))]
                    self.data["NAME"].append(name)
                    self.guest_list_check_label.setText("On List")
                    self.guest_list_check_label.setStyleSheet('color: green')
                    self.lcdNumber.display(self.count)
                    self.list_preview.addItem(name+"      -      "+inp)
                    self.plot_data()
                    #if self.count % 10 == 0:
                     #   export = pd.DataFrame(self.data)        
                      #  export.index += 1
                       # fname = os.path.expanduser('~')+'/Desktop/temp/.xlsx'
                        #export.to_excel(fname[0])
            else:
                self.guest_list_check_label.setText("No Guest List Loaded")
                self.guest_list_check_label.setStyleSheet('color: yellow')
                self.id_input.clear()

        elif len(inp) == 13:
            inp = inp[4:10]
            if self.has_guest:
                if int(inp) not in self.guest_list["ID"]:
                    self.guest_list_check_label.setText("Not On List")
                    self.guest_list_check_label.setStyleSheet('color: red')
                    self.id_input.clear()
                else:
                    self.count +=1
                    self.graph_y.append(self.count)
                    self.data["ID"].append(inp)
                    self.data["TIME"].append(datetime.datetime.now().strftime("%H:%M"))
                    self.graph_x.append(datetime.datetime.now().strftime("%H:%M:%S"))
                    name =self.guest_list["NAME"][self.guest_list["ID"].index(int(inp))]
                    self.data["NAME"].append(name)
                    self.guest_list_check_label.setText("On List")
                    self.guest_list_check_label.setStyleSheet('color: green')
                    self.lcdNumber.display(self.count)
                    self.list_preview.addItem(name+" - "+inp)
                    self.plot_data()
                    #if self.count % 10 == 0:
                     #   export = pd.DataFrame(self.data)        
                      #  export.index += 1
                       # fname =  os.path.expanduser('~')+"/Desktop/temp.xlsx"
                        #export.to_excel(fname[0])
            else:
                self.guest_list_check_label.setText("No Guest List Loaded")
                self.guest_list_check_label.setStyleSheet('color: yellow')
                self.id_input.clear()
        else:
            self.guest_list_check_label.setText("Invalid Input")
            self.guest_list_check_label.setStyleSheet('color: yellow')
        self.id_input.clear()

    def email_list(self,file_path):
        if self.settings["send_email"] == "yes":
            for x in self.settings["to_email"].strip().split(','):
                try:
                    toaddr = x.strip()
                    fromaddr = self.settings["from_email"]

                    msg = MIMEMultipart()
                    msg['From'] = fromaddr
                    msg['To'] = toaddr
                    msg['Subject'] = "Party List for "+self.today

                    body = "Hello,\n Attached is the attendance sheet for our social event on "+self.today+". If any additional information is needed, please contact <insert responsible person here>"

                    msg.attach(MIMEText(body, 'plain'))
                    filename = file_path.split("/")[-1]
                    attachment = open(file_path, "rb")
                    part = MIMEBase('application', 'octet-stream')
                    part.set_payload((attachment).read())
                    encoders.encode_base64(part)
                    part.add_header('Content-Disposition', "attachment; filename= %s" % filename)
                    msg.attach(part)
                    server = smtplib.SMTP('smtp.gmail.com', 587)
                    server.starttls()
                    server.login(fromaddr, self.settings["email_password"])
                    text = msg.as_string()
                    server.sendmail(fromaddr, toaddr, text)
                    server.quit()
                except:
                    print("email error")
                    pass
        else:
            pass
    def export_data(self):
        export = pd.DataFrame(self.data)
        export.index += 1
        fname = QFileDialog.getSaveFileName(None, 'Save Guest Log' , os.path.expanduser('~')+"/Desktop/"+self.today+"-"+self.settings["default_filename"]+".xlsx","Excel Files (*.xlsx)" )
        if fname[0] == '':
            pass
        else:
            export.to_excel(fname[0])
        self.email_list(fname[0])
    def plot_data(self):
        if not self.has_graph:
            new_x = [mdates.datestr2num(x) for x in self.graph_x]
            plt.xlabel('Time')
            plt.ylabel('Count')
            ax = self.fig.add_subplot(111)
            ax.xaxis.set_major_formatter(mdates.DateFormatter('%H:%M'))
            ax.plot(new_x, self.graph_y, 'r-')
            ax.get_xaxis().set_ticks([])
            #ax.get_yaxis().set_ticks([])
            self.canvas = FigureCanvas(self.fig)
            self.graph_layout.addWidget(self.canvas)
            self.canvas.draw()
            self.has_graph = True
        else:
            self.graph_layout.removeWidget(self.canvas)
            self.canvas.close()
            self.fig.clf()
            plt.xlabel('Time')
            plt.ylabel('Count')
            ax = self.fig.add_subplot(111)
            ax.xaxis.set_major_formatter(mdates.DateFormatter('%H:%M'))
            new_x = [mdates.datestr2num(x) for x in self.graph_x]
            ax.plot(new_x, self.graph_y, 'r-')
            ax.get_xaxis().set_ticks([])
            #ax.get_yaxis().set_ticks([])
            self.canvas = FigureCanvas(self.fig)
            self.graph_layout.addWidget(self.canvas)
            self.canvas.draw()
Beispiel #27
0
class Main(QMainWindow, Ui_MainWindow):

    def __init__(self, ):
        super(Main, self).__init__()
        self.setupUi(self)

        self.showMaximized()
        self.ave = np.array([])         # empty array for average bore
        self.auto_flag = False          # autoscale flag
        self.spinBoxval = 0             # defualt 4D data_cube dimension
        self.spinBox.hide()             # hide option unless 4D
        self.colourmap = 'viridis'      # default colourmap
        self.interpMethod = 'nearest'   # default interp method
        self.cmapmin = None             # default colourbar range, i.e let matplotlib decide
        self.cmapmax = None
        self.hres = 1                   # default res, i.e jut voxel numbers on axis
        self.vres = 1
        self.Normx = None               # normalisation method. default set to None
        self.Normy = None
        self.Normz = None

        self.XView.setChecked(True)
        self.fig = Figure()
        self.ax1 = self.fig.add_subplot(111)
        self.X = datacube()
        self.AveBoreView = 'X'

        # change view of cube
        self.XView.toggled.connect(lambda: self.btnstate(self.XView))
        self.YView.toggled.connect(lambda: self.btnstate(self.YView))
        self.ZView.toggled.connect(lambda: self.btnstate(self.ZView))
        self.Bore.toggled.connect(lambda: self.btnstate(self.Bore))
        self.AverageBore.toggled.connect(
            lambda: self.btnstate(self.AverageBore))

        # update data when slider moved
        self.Scroll_Horz.valueChanged[int].connect(self.sliderval)
        self.Scroll_Vert.valueChanged[int].connect(self.sliderval)

        self.file_open(args)
        self.Open.triggered.connect(self.file_open)
        self.Save_Avg_Bore.triggered.connect(self.saveBore)
        self.Reset.triggered.connect(self.reset_plot)
        self.AutoScale.triggered.connect(self.Auto_Scale_plot)
        # self.Bore_View.triggered.connect(self.ViewBore)
        self.action_Save_Gif.triggered.connect(self.saveGif)
        self.action_Colour_Map.triggered.connect(self.changeColourMap)
        self.action_Interpolation_Method.triggered.connect(self.changeInterpolationMethod)
        self.action_Colour_Bar_Clip.triggered.connect(self.changeclipColourBarRange)
        self.action_Save_Image.triggered.connect(self.saveImage)
        self.action_Normalisation_Method.triggered.connect(self.changeNormMethod)
        self.action_Bore_Location.triggered.connect(self.setBoreLocation)

        self.spinBox.valueChanged.connect(self.changeSpinbox)

    def setBoreLocation(self, ):

        xloc, ok = QtWidgets.QInputDialog.getInt(
            self, 'Input location', 'Enter X location:')

        yloc, ok = QtWidgets.QInputDialog.getInt(
            self, 'Input location', 'Enter Y location:')

        self.Scroll_Horz.setValue(xloc)
        self.Scroll_Vert.setValue(yloc)

        if self.Bore.isChecked():
            if self.BoreView == 'X':
                self.im.set_ydata(self.X.data[:, xloc, yloc][::])
            elif self.BoreView == 'Y':
                self.im.set_ydata(self.X.data[xloc, :, yloc][::])
            elif self.BoreView == 'Z':
                self.im.set_ydata(self.X.data[yloc, xloc, :][::])

        # try and redraw
        try:
            self.im.axes.figure.canvas.draw()
            if self.auto_flag:
                self.im.autoscale()
        except AttributeError:
            pass

    def changeNormMethod(self, ):
        # func to change Normalisation method of matshow
        method = self.getNormDialog()
        if(method == 'Log'):
            self.Normx = colors.LogNorm(vmin=0.1,
                                        vmax=self.X.data[self.ind, :, :].max())
            self.Normy = colors.LogNorm(vmin=0.1,
                                        vmax=self.X.data[:, self.ind, :].max())
            self.Normz = colors.LogNorm(vmin=0.1,
                                        vmax=self.X.data[:, :, self.ind].max())
        elif(method == 'Symmetric Log'):
            self.Normx = colors.SymLogNorm(linthresh=1.,
                                           vmin=self.X.data[self.ind, :, :].min(),
                                           vmax=self.X.data[self.ind, :, :].max())
            self.Normy = colors.SymLogNorm(linthresh=1.,
                                           vmin=self.X.data[:, self.ind, :].min(),
                                           vmax=self.X.data[:, self.ind, :].max())
            self.Normz = colors.SymLogNorm(linthresh=1.,
                                           vmin=self.X.data[:, :, self.ind].max(),
                                           vmax=self.X.data[:, :, self.ind].max())
        elif(method == 'Linear'):
            self.Normx = None
            self.Normy = None
            self.Normz = None
        self.reset_plot(False)
        self.init_plot()

    def saveImage(self, ):
        # saves data and image of current view
        name = self.showGifDialog()
        if self.XView.isChecked():
            np.savetxt(name + '.dat', self.X.data[self.Scroll_Vert.value(), :, :],
                       delimiter=' ')
        elif self.YView.isChecked():
            np.savetxt(name + '.dat', self.X.data[:, self.Scroll_Vert.value(), :],
                       delimiter=' ')
        elif self.ZView.isChecked():
            np.savetxt(name + '.dat', self.X.data[:, :, self.Scroll_Vert.value()],
                       delimiter=' ')

        self.hres, self.vres = self.showextentDialog()
        # scale x, y ticks to actual scale based upon user definition
        # thanks https://stackoverflow.com/a/17816809/6106938
        # change so that it uses extent=[xmin, xmax, ymin, ymax]
        # set default as None
        # then change here. extent added to matshow(*args, extent=[...])
        ticks = ticker.FuncFormatter(lambda x, pos: '{0:g}'.format(x * self.hres))
        self.ax1.xaxis.set_major_formatter(ticks)
        ticks = ticker.FuncFormatter(lambda y, pos: '{0:g}'.format(y * self.vres))
        self.ax1.yaxis.set_major_formatter(ticks)
        self.fig.savefig(name + '.png')

    def changeColourMap(self, ):
        # change cmap
        self.colourmap = self.showColourmapsDialog()
        self.reset_plot(False)
        self.init_plot()

    def changeclipColourBarRange(self, ):
        # change vmin, vmax for cbar

        self.cmapmin, self.cmapmax = self.showclipColourBarDialog()

        self.reset_plot(False)
        self.init_plot()

    def changeInterpolationMethod(self, ):
        # change interpolation method for image
        self.interpMethod = str(self.showInterpolationDialog())
        self.reset_plot(False)
        self.init_plot()

    def saveGif(self):
        rang = self.showGifframesDialog()  # get range of frames
        step = self.showGifstepDialog()    # get number of images
        name = self.showGifDialog()        # name of file
        tight = self.showGifExtent()       # tight or not
        # loop over range and make images
        tmpplace = self.Scroll_Vert.value()
        if rang * step > tmpplace:
            rang = tmpplace
        for i in range(rang):
            self.Scroll_Horz.setValue(self.ind)
            self.Scroll_Vert.setValue(tmpplace - (i * step))
            self.sliderval()
            if tight:
                extent = self.ax1.get_window_extent().transformed(fig.dpi_scale_trans.inverted())
                self.fig.savefig(str(i).zfill(3) + 'pic.png', bbox_inches=extent)
            else:
                self.fig.savefig(str(i).zfill(3) + 'pic.png')
        # use ffmpeg to create gif
        if tight:
            os.system("mogrify -trim *pic.png")
        os.system("ffmpeg -framerate 10 -pattern_type glob -i '*pic.png' -c:v libx264 -r 24 -pix_fmt yuv420p -vf 'pad=ceil(iw/2)*2:ceil(ih/2)*2' " + name + ".mp4")
        os.system('rm *pic.png')
        print('done')

    def changeSpinbox(self):
        # for 4d data cubes
        self.spinBoxval = int(self.spinBox.value())
        fd = open(self.name, 'rb')
        self.readslice(fd, self.ndim, np.float64, self.cubeorder)
        self.reset_plot()
        self.init_plot()

    def Auto_Scale_plot(self):
        # autoscale cbar on plot and reset clipping if any
        self.cmapmin = None
        self.cmapmax = None
        if not self.auto_flag:
            self.auto_flag = True
        else:
            self.auto_flag = False

    def sliderval(self):
        # move slider and update data
        if self.XView.isChecked():
            # fd = open(self.name, 'rb')
            self.X.readslice(self.Scroll_Horz.value())
            self.im.set_data(self.X.data[self.Scroll_Vert.value(), :, :])
            # self.Scroll_Horz.setValue(0)  # pin unsed slider
        elif self.YView.isChecked():
            self.X.readslice(self.Scroll_Horz.value())
            self.im.set_data(self.X.data[:, self.Scroll_Vert.value(), :])
            # self.Scroll_Horz.setValue(0)  # pin unsed slider
        elif self.ZView.isChecked():
            self.X.readslice(self.Scroll_Horz.value())
            self.im.set_data(self.X.data[:, :, self.Scroll_Vert.value()])
            # self.Scroll_Horz.setValue(0)  # pin unsed slider
        elif self.Bore.isChecked():
            if self.BoreView == 'X':
                self.im.set_ydata(self.X.data[:, self.Scroll_Horz.value(), self.Scroll_Vert.value()][::])
                if self.auto_flag:
                    self.ax1.relim()
                    self.ax1.autoscale_view(True, True, True)
            elif self.BoreView == 'Y':
                self.im.set_ydata(self.X.data[self.Scroll_Horz.value(), :, self.Scroll_Vert.value()][::])
                if self.auto_flag:
                    self.ax1.relim()
                    self.ax1.autoscale_view(True, True, True)
            elif self.BoreView == 'Z':
                self.im.set_ydata(self.X.data[self.Scroll_Vert.value(), self.Scroll_Horz.value(), :][::])
                if self.auto_flag:
                    self.ax1.relim()
                    self.ax1.autoscale_view(True, True, True)
        elif self.AverageBore.isChecked():
            self.Scroll_Horz.setValue(self.ind)
            self.Scroll_Vert.setValue(self.ind)

        # try and redraw
        try:
            self.im.axes.figure.canvas.draw()
            if self.auto_flag:
                self.im.autoscale()
        except AttributeError:
            pass

    def addmpl(self):
        # add plot to anvas
        self.rmmpl()
        self.canvas = FigureCanvas(self.fig)
        self.mplvl.addWidget(self.canvas)
        self.canvas.draw()
        self.toolbar = NavigationToolbar(self.canvas, self.mplwindow)
        self.mplvl.addWidget(self.toolbar)

    def rmmpl(self):
        # delete plot from canvas
        try:
            self.canvas.close()
            self.canvas.deleteLater()
            self.toolbar.close()
            self.toolbar.deleteLater()
            gc.collect()
        except:
            pass

    def saveBore(self,):
        # save bore as a list of points
        name = QtWidgets.QFileDialog.getSaveFileName(self, 'Save File')
        f = open(name, 'w')
        if len(self.ave) > 1:
            for i in range(len(self.ave)):
                f.write(str(self.ave[i]) + '\n')
            f.close()
        else:
            if self.BoreView == "X":
                tmp = self.X[:, self.Scroll_Horz.value(), self.Scroll_Vert.value()]
            elif self.BoreView == "Y":
                tmp = self.X[self.Scroll_Horz.value(), :, self.Scroll_Vert.value()]
            elif self.BoreView == "Z":
                tmp = self.X[self.Scroll_Horz.value(), self.Scroll_Vert.value(), :]

            for i in range(len(tmp)):
                f.write(str(tmp[i]) + '\n')

    def file_open(self, args):

        self.reset_plot()

        while True:
            try:
                # get file name
                if args.file is None:
                    self.X.name = QtWidgets.QFileDialog.getOpenFileName(self, 'Open File')[0]
                else:
                    self.X.name = args.file

                # get precision of data cube
                self.X.dtype, self.X.cubeorder, item = self.getPrec(args)

                # get dimensions of data cube. can be guessed
                bool4d = False
                if self.X.cubeorder == 4:
                    bool4d = True
                self.X.ndim = self.getSize(args, item, bool4d)

                try:
                    fd = open(self.X.name, 'rb')
                except FileNotFoundError:
                    self.ErrorDialog("File not Found!")
                    self.X.name = QtWidgets.QFileDialog.getOpenFileName(self, 'Open File')[0]

                self.X.readslice(0)
                self.init_plot()
                break
            except ValueError:
                size = os.path.getsize(self.X.name)
                if "Real*8" in item:
                    size /= 8
                elif "Real*4" in item:
                    size /= 4

                mssg = "Value of Ndim or precision is incorrect for this data cube.\n On disk size is: {:010d}.\n".format(int(size))

                val2 = self.X.is_perfect_n(size, 2.)
                val3 = self.X.is_perfect_n(size, 3.)

                if (val2 and val3) != 0:
                    mssg += " Try x=y={:04d}, z=1\n or x=y=z={:04d}.".format(int(val2), int(val3))
                elif val2 != 0:
                    mssg += "Try x=y={:04d}, z=1.".format(int(val2))
                elif val3 != 0:
                    mssg += "Try x=y=z={:04d}.".format(int(val3))
                self.ErrorDialog(mssg)

                args.ndim = None
                args.fpprec = None
            except UnboundLocalError:
                pass
                break

    def getSize(self, args, item, bool4d):
        if args.ndim is None and item:
            size = os.path.getsize(self.X.name)
            if "Real*8" in item:
                size /= 8
            elif "Real*4" in item:
                size /= 4
            if self.X.is_perfect_n(size, 3.) != 0:
                size = self.X.is_perfect_n(size, 3.)
                ndim = (size, size, size)
            else:
                ndim = self.showNdimDialog(bool4d)
        else:
            ndim = (args.ndim, args.ndim, args.ndim)

        return ndim

    def getPrec(self, args):
        # get precision of data cube
        item = None
        if args.fpprec is None:
            item = str(self.showDtDialog())
            if "Real*8" in item:
                dt = np.float64
            elif "Real*4" in item:
                dt = np.float32

            if "4 dim" in item:
                dim = 4
            elif "3 dim" in item:
                dim = 3
        else:
            if args.fpprec == 1:
                dt = np.float32
                dim = 4
            elif args.fpprec == 2:
                dt = np.float64
                dim = 4
            elif args.fpprec == 3:
                dt = np.float32
                dim = 3
            elif args.fpprec == 4:
                dt = np.float64
                dim = 3
        return dt, dim, item

    def btnstate(self, b):

        if b.text() == "X View":
            if b.isChecked() is True:
                self.reset_plot(False)
                self.Scroll_Vert.setMaximum(self.rows - 1)
                self.im = self.ax1.matshow(self.X.data[self.ind, :, :],
                                           vmin=self.cmapmin, vmax=self.cmapmax,
                                           cmap=str(self.colourmap), interpolation=self.interpMethod,
                                           norm=self.Normx)
                self.fig.colorbar(self.im)
                self.fig.set_tight_layout(True)
                self.ax1.set_aspect('auto')
                self.addmpl()

        if b.text() == "Y View":
            if b.isChecked() is True:
                self.reset_plot(False)
                self.Scroll_Vert.setMaximum(self.cols - 1)
                self.im = self.ax1.matshow(self.X.data[:, self.ind, :],
                                           vmin=self.cmapmin, vmax=self.cmapmax,
                                           cmap=str(self.colourmap), interpolation=self.interpMethod,
                                           norm=self.Normy)
                self.fig.colorbar(self.im)
                self.fig.set_tight_layout(True)
                self.ax1.set_aspect('auto')
                self.addmpl()

        if b.text() == "Z View":
            if b.isChecked() is True:
                self.reset_plot(False)
                self.Scroll_Vert.setMaximum(self.slices - 1)
                self.im = self.ax1.matshow(self.X.data[:, :, self.ind],
                                           vmin=self.cmapmin, vmax=self.cmapmax,
                                           cmap=str(self.colourmap), interpolation=self.interpMethod,
                                           norm=self.Normz)
                try:
                    self.fig.colorbar(self.im)
                except ZeroDivisionError:
                    self.ErrorDialog("Divison by zero, try another range")
                    self.Normz = None
                    self.Normy = None
                    self.Normz = None
                    self.cmapmin = None
                    self.cmapmax = None
                    self.btnstate(b)
                self.fig.set_tight_layout(True)
                self.ax1.set_aspect('auto')
                self.addmpl()

        if b.text() == "Draw Bore":
            if b.isChecked() is True:
                self.ViewBore()
                self.reset_plot(False)
                if self.BoreView == 'X':
                    self.im, = self.ax1.plot(self.X.data[:, self.ind, self.ind])
                elif self.BoreView == 'Y':
                    self.im, = self.ax1.plot(self.X.data[self.ind, :, self.ind])
                elif self.BoreView == 'Z':
                    self.im, = self.ax1.plot(self.X.data[self.ind, self.ind, :])
                self.fig.set_tight_layout(True)
                self.addmpl()

        if b.text() == "Avg. Bore":
            if b.isChecked() is True:
                self.AveBoreChecked()

    def ViewBore(self):
        self.BoreView = self.showBoreViewDialog()
        if self.BoreView == 'X':
            self.view = (1, 2)
        elif self.BoreView == 'Y':
            self.view = (0, 2)
        elif self.BoreView == 'Z':
            self.view = (0, 1)

    def AveBoreChecked(self):

        self.ViewBore()

        self.reset_plot(False)
        if len(self.ave) == 0:
            self.ave = np.array([])
            self.ave = np.sum(self.X.data, self.view)
            self.ave /= (len(self.X.data[self.view[0]]) * len(self.X.data[self.view[1]]))

        self.im = self.ax1.plot(self.ave[::])
        self.fig.set_tight_layout(True)
        self.addmpl()

    def reset_plot(self, *args):
        self.ave = np.array([])

        self.fig.clf()
        self.ax1.clear()
        gc.collect()  # fixes most of memory leak

        self.fig = Figure()
        self.ax1 = self.fig.add_subplot(111)
        self.rmmpl()

    def init_plot(self, ):

        self.rmmpl()

        if self.X.cubeorder == 4:
            self.rows, self.cols, self.slices, self.depth = self.X.ndim
        else:
            self.rows, self.cols, self.slices = self.X.ndim
            self.depth = 0
        self.ind = 0  # int(rows / 2)
        if self.XView.isChecked():
            view = self.XView
            self.Scroll_Vert.setMaximum(self.rows)
            self.Scroll_Horz.setMaximum(self.depth)
        elif self.YView.isChecked():
            view = self.YView
            self.Scroll_Vert.setMaximum(self.cols)
            self.Scroll_Horz.setMaximum(self.depth)
        elif self.ZView.isChecked():
            view = self.ZView
            self.Scroll_Vert.setMaximum(self.slices)
            self.Scroll_Horz.setMaximum(self.cols)
        elif self.AverageBore.isChecked():
            view = self.AverageBore
            self.Scroll_Vert.setMaximum(self.rows)
        elif self.Bore_View.isChecked():
            view = self.ViewBore
            self.Scroll_Vert.setMaximum(self.cols)
            self.Scroll_Horz.setMaximum(self.rows)
        self.btnstate(view)
        self.Scroll_Horz.setValue(self.ind)
        self.Scroll_Vert.setValue(self.ind)

    def showGifframesDialog(self, ):
        text, ok = QtWidgets.QInputDialog.getInt(
            self, '# of frames', 'Enter # of frames:')
        if ok:
            return(text)

    def showGifstepDialog(self, ):
        text, ok = QtWidgets.QInputDialog.getInt(
            self, 'Step size', 'Enter value of step:')
        if ok:
            return(text)

    def showGifExtent(self, ):
        items = ("Colour Bar", "No Colour Bar")

        text, ok = QtWidgets.QInputDialog.getItem(
            self, "Colour Bar on GIF?", " ", items, 0, False)

        if ok and text:
            if items == "Colour Bar":
                text = False
            else:
                text = True
            return text

    def showNdimDialog(self, bool4d):
        text1, ok1 = QtWidgets.QInputDialog.getInt(
            self, 'Input Ndim', 'Enter X Ndim:')
        if ok1:
            text2, ok2 = QtWidgets.QInputDialog.getInt(
                self, 'Input Ndim', 'Enter Y Ndim:')
            if ok2:
                text3, ok3 = QtWidgets.QInputDialog.getInt(
                    self, 'Input Ndim', 'Enter Z Ndim:')
                if ok3 and bool4d:
                    text4, ok4 = QtWidgets.QInputDialog.getInt(
                        self, 'Input Ndim', 'Enter T Ndim:')
                    return (text1, text2, text3, text4)
                else:
                    return (text1, text2, text3)

    def showDtDialog(self, ):
        items = ("4 dim Real*4", "4 dim Real*8",
                 "3 dim Real*4", "3 dim Real*8")

        item, ok = QtWidgets.QInputDialog.getItem(self, "Select Fortran Precision",
                                                  "Precisions", items, 0, False)

        if ok and item:
            return item

    def showBoreViewDialog(self, ):
        items = ("X", "Y", "Z")
        item, ok = QtWidgets.QInputDialog.getItem(self, "Select Average Bore Direction",
                                                  "Views", items, 0, False)
        if ok and item:
            return item

    def showGifDialog(self, ):
        text, ok = QtWidgets.QInputDialog.getText(
            self, 'Filename Dialog', 'Enter filename:')
        if ok:
            return str(text)

    def showextentDialog(self, ):
        hres, ok = QtWidgets.QInputDialog.getDouble(
            self, 'Data Extent', 'Enter horizontal resolution:', 0, -100, 100, 9,)
        if ok:
            vres, ok = QtWidgets.QInputDialog.getDouble(
                self, 'Data Extent', 'Enter vertical resolution:', 0, -100, 100, 9,)
            if ok:
                return (hres, vres)

    def getNormDialog(self, ):
        items = ("Log", "Linear", "Symmetric Log")

        item, ok = QtWidgets.QInputDialog.getItem(self, "Select cbar normalisation method",
                                                  "Method:", items, 0, False)
        if ok and item:
            return item

    def showColourmapsDialog(self, ):
        items = ('viridis', 'inferno', 'plasma', 'magma', 'Blues', 'BuGn',
                 'BuPu', 'GnBu', 'Greens', 'Greys', 'Oranges', 'OrRd', 'PuBu',
                 'PuBuGn', 'PuRd', 'Purples', 'RdPu', 'Reds', 'YlGn', 'YlGnBu',
                 'YlOrBr', 'YlOrRd', 'afmhot', 'autumn', 'bone', 'cool',
                 'copper', 'gist_heat', 'gray', 'hot', 'pink', 'spring',
                 'summer', 'winter', 'BrBG', 'bwr', 'coolwarm', 'PiYG', 'PRGn',
                 'PuOr', 'RdBu', 'RdGy', 'RdYlBu', 'RdYlGn', 'Spectral',
                 'seismic', 'Accent', 'Dark2', 'Paired', 'Pastel1', 'Pastel2',
                 'Set1', 'Set2', 'Set3', 'Vega10', 'Vega20', 'Vega20b',
                 'Vega20c', 'gist_earth', 'terrain', 'ocean', 'gist_stern',
                 'brg', 'CMRmap', 'cubehelix', 'gnuplot', 'gnuplot2',
                 'gist_ncar', 'nipy_spectral', 'jet', 'rainbow', 'gist_rainbow',
                 'hsv', 'flag', 'prism')
        item, ok = QtWidgets.QInputDialog.getItem(self, "Select Colour Map",
                                                  "Cmaps", items, 0, False)
        if ok and item:
            return item

    def showInterpolationDialog(self, ):
        items = ('none', 'nearest', 'bilinear', 'bicubic', 'spline16',
                 'spline36', 'hanning', 'hamming', 'hermite', 'kaiser',
                 'quadric', 'catrom', 'gaussian', 'bessel', 'mitchell',
                 'sinc', 'lanczos')
        item, ok = QtWidgets.QInputDialog.getItem(self, "Select Interpolation Method",
                                                  "Methods", items, 0, False)
        if ok and item:
            return item

    def showclipColourBarDialog(self, ):
        text1, ok1 = QtWidgets.QInputDialog.getDouble(
            self, 'Input cbar min', 'Enter min:', 0., np.finfo("d").min, np.finfo("d").max, 10)
        if ok1:
            text2, ok2 = QtWidgets.QInputDialog.getDouble(
                self, 'Input cbar max', 'Enter max:', 0., np.finfo("d").min, np.finfo("d").max, 10)
            if ok2:
                return (float(text1), float(text2))

    def ErrorDialog(self, ErrMsg):
        QtWidgets.QMessageBox.warning(self, "Error", ErrMsg)
Beispiel #28
0
class Main(QMainWindow, Ui_MainWindow):
    def __init__(self,specfilename,parfilename=None,wave1=None,wave2=None,numchunks=8,parent=None):
        QtWidgets.QMainWindow.__init__(self, parent)
        #super(Main,self).__init__()
        self.setupUi(self)

        ### Initialize stuff
        self.line_dict = {}
        self.fitpars = None
        self.parinfo = None
        self.linecmts = None
        self.wave1 = wave1
        self.wave2 = wave2
        self.numchunks = numchunks
        self.spls = []
        self.labeltog = 1
        self.pixtog = 0
        self.restog = 1
        self.fitconvtog = 0
        self.lastclick=1334.

        ### Read in spectrum and list of lines to fit
        self.specfilename=specfilename
        self.spectrum = readspec(specfilename)
        self.wave=self.spectrum.wavelength.value
        self.normflux=self.spectrum.flux/self.spectrum.co
        self.normsig=self.spectrum.sig/self.spectrum.co
        cfg.spectrum = self.spectrum
        cfg.wave=self.wave
        cfg.normflux=self.normflux
        cfg.filename=self.specfilename

        if not parfilename==None:
            self.initialpars(parfilename)


        ### Connect signals to slots
        self.fitButton.clicked.connect(self.fitlines)
        self.fitConvBox.clicked.connect(self.togfitconv)
        self.boxLineLabel.clicked.connect(self.toglabels)
        self.boxFitpix.clicked.connect(self.togfitpix)
        self.boxResiduals.clicked.connect(self.togresiduals)
        self.loadParsButton.clicked.connect(self.openParFileDialog)
        self.addLineButton.clicked.connect(self.addLineDialog)
        self.writeParsButton.clicked.connect(self.writeParFileDialog)
        self.writeModelButton.clicked.connect(self.writeModelFileDialog)
        self.writeModelCompButton.clicked.connect(self.writeModelCompFileDialog)
        self.quitButton.clicked.connect(self.quitGui)

        ### Initialize spectral plots
        fig=Figure()
        self.fig=fig
        self.initplot(fig)

        ### Initialize side plot
        sidefig=Figure(figsize=(5.25,2))
        self.sidefig = sidefig
        self.addsidempl(self.sidefig)
        self.sideplot(self.lastclick)  #Dummy initial cenwave setting


    def initplot(self,fig,numchunks=8):
        wlen=len(self.spectrum.wavelength)/numchunks
        self.spls=[]
        if self.wave1==None:  waveidx1=0  # Default to plotting entire spectrum for now
        else: waveidx1=jbg.closest(self.wave,self.wave1)
        if self.fitpars!=None:
                model=joebvpfit.voigtfunc(self.wave,self.datamodel.fitpars)
        sg=jbg.subplotgrid(numchunks)
        for i in range(numchunks):
            self.spls.append(fig.add_subplot(sg[i][0],sg[i][1],sg[i][2]))
            pixs=range(waveidx1+i*wlen,waveidx1+(i+1)*wlen)
            self.spls[i].plot(self.wave[pixs],self.normflux[pixs],linestyle='steps-mid')
            if self.fitpars!=None:
                self.spls[i].plot(self.wave,model,'r')
            self.spls[i].set_xlim(self.wave[pixs[0]],self.wave[pixs[-1]])
            self.spls[i].set_ylim(cfg.ylim)
            self.spls[i].set_xlabel('wavelength',labelpad=0)
            self.spls[i].set_ylabel('relative flux',labelpad=-5)
            self.spls[i].get_xaxis().get_major_formatter().set_scientific(False)
        fig.subplots_adjust(top=0.98,bottom=0.05,left=0.08,right=0.97,wspace=0.15,hspace=0.25)
        self.addmpl(fig)

    def initialpars(self,parfilename):
        ### Deal with initial parameters from line input file
        self.fitpars,self.fiterrors,self.parinfo,self.linecmts = joebvpfit.readpars(parfilename)
        cfg.fitidx=joebvpfit.fitpix(self.wave, self.fitpars) #Set pixels for fit
        cfg.wavegroups=[]
        self.datamodel = LineParTableModel(self.fitpars,self.fiterrors,self.parinfo,linecmts=self.linecmts)
        self.tableView.setModel(self.datamodel)
        self.datamodel.updatedata(self.fitpars,self.fitpars,self.parinfo,self.linecmts)

    def sideplot(self,cenwave,wavebuf=3):
        if len(self.sidefig.axes)==0:
            self.sideax=self.sidefig.add_subplot(111)
        self.sideax.clear()
        self.sideax.plot(self.wave, self.normflux, linestyle='steps-mid')
        if self.pixtog == 1:
            self.sideax.plot(self.wave[cfg.fitidx], self.normflux[cfg.fitidx], 'gs', markersize=4, mec='green')
        model = joebvpfit.voigtfunc(self.wave, self.fitpars)
        res = self.normflux - model
        self.sideax.plot(self.wave, model, 'r')
        if self.restog == 1:
            self.sideax.plot(self.wave, -res, '.', color='black', ms=2)
        self.sideax.plot(self.wave, [0] * len(self.wave), color='gray')

        ### label lines we are trying to fit
        if self.labeltog == 1:
            for j in range(len(self.fitpars[0])):
                labelloc = self.fitpars[0][j] * (1. + self.fitpars[3][j]) + self.fitpars[4][j] / c * \
                                                                            self.fitpars[0][j] * (
                                                                            1. + self.fitpars[3][j])
                label = ' {:.1f}_\nz{:.4f}'.format(self.fitpars[0][j], self.fitpars[3][j])
                self.sideax.text(labelloc, cfg.label_ypos, label, rotation=90, withdash=True, ha='center', va='bottom',
                                 clip_on=True, fontsize=cfg.label_fontsize)

        self.sideax.plot(self.wave, self.normsig, linestyle='steps-mid', color='red', lw=0.5)
        self.sideax.plot(self.wave, -self.normsig, linestyle='steps-mid', color='red', lw=0.5)
        self.sideax.get_xaxis().get_major_formatter().set_scientific(False)
        self.sideax.get_xaxis().get_major_formatter().set_useOffset(False)
        try:
            self.sideax.set_xlim(cenwave-wavebuf,cenwave+wavebuf)
            self.sideax.set_ylim(cfg.ylim)
            self.changesidefig(self.sidefig)
        except TypeError:
            pass
    def fitlines(self):
        print('VPmeasure: Fitting line profile(s)...')
        print(len(self.fitpars[0]),'lines loaded for fitting.')
        if self.fitconvtog:
            self.fitpars, self.fiterrors = joebvpfit.fit_to_convergence(self.wave, self.normflux, self.normsig,
                                                               self.datamodel.fitpars, self.datamodel.parinfo)
        else:
            self.fitpars, self.fiterrors = joebvpfit.joebvpfit(self.wave, self.normflux,self.normsig, self.datamodel.fitpars,self.datamodel.parinfo)
        self.datamodel.updatedata(self.fitpars,self.fiterrors,self.parinfo,self.linecmts)
        self.tableView.resizeColumnsToContents()
        self.updateplot()
        self.sideplot(self.lastclick)

    def togfitconv(self):
        if self.fitconvtog==1: self.fitconvtog=0
        else: self.fitconvtog=1

    def quitGui(self):
        self.deleteLater()
        self.close()

    def toglabels(self):
        if self.labeltog==1: self.labeltog=0
        else: self.labeltog=1
        self.updateplot()

    def togfitpix(self):
        if self.pixtog == 1:
            self.pixtog = 0
        else:
            self.pixtog = 1
        self.updateplot()

    def togresiduals(self):
        if self.restog == 1:
            self.restog = 0
        else:
            self.restog = 1
        self.updateplot()

    def openParFileDialog(self):
        fname = QtWidgets.QFileDialog.getOpenFileName(self, 'Open line parameter file','.')
        fname = str(fname[0])
        if fname != '':
            self.initialpars(fname)

        self.updateplot()

    def writeParFileDialog(self):
        fname = QtWidgets.QFileDialog.getSaveFileName(self, 'Save line parameter file', cfg.VPparoutfile)
        fname = str(fname[0])
        if fname != '':
            joebvpfit.writelinepars(self.datamodel.fitpars, self.datamodel.fiterrors, self.datamodel.parinfo, self.specfilename, fname, self.datamodel.linecmts)

    def writeModelFileDialog(self):
        fname = QtWidgets.QFileDialog.getSaveFileName(self, 'Save model to file', cfg.VPmodeloutfile)
        fname = str(fname[0])
        if fname != '':
            joebvpfit.writeVPmodel(fname, self.wave, self.fitpars, self.normflux, self.normsig)

    def writeModelCompFileDialog(self):
        dirDialog = QtWidgets.QFileDialog(self)
        dirDialog.setFileMode(dirDialog.Directory)
        dirDialog.setOption(dirDialog.ShowDirsOnly, True)
        defDirName = cfg.VPmodeloutfile[:-5]
        dname = dirDialog.getSaveFileName(self, 'Save model to files split by components',defDirName)
        dname = str(dname[0])
        if dname != '':
            joebvpfit.writeVPmodelByComp(dname, self.spectrum,self.fitpars)

    def addLineDialog(self):
        dlgOutput=newLineDialog.get_newline()
        if (dlgOutput != 0):
            if '' not in dlgOutput:
                newlam,newz,newcol,newb,newvel,newvel1,newvel2 = dlgOutput
                self.datamodel.addLine(self.wave,float(newlam), float(newz), float(newcol), float(newb), float(newvel), float(newvel1), float(newvel2))        #dialog=newLineDialog(parent=None)
                self.fitpars = self.datamodel.fitpars
                self.fiterrors = self.datamodel.fiterrors
                self.parinfo = self.datamodel.parinfo
                self.tableView.setModel(self.datamodel)


    def updateplot(self):
        if self.wave1==None:  waveidx1=0  # Default to plotting entire spectrum for now
        else: waveidx1=jbg.closest(self.wave,self.wave1)
        wlen=len(self.spectrum.wavelength)/self.numchunks
        for i,sp in enumerate(self.spls):
                sp.clear()
                prange=range(waveidx1+i*wlen,waveidx1+(i+1)*wlen)
    
                if ((len(self.fitpars[0])>0)):
    
                    sp.plot(self.wave,self.normflux,linestyle='steps-mid')
                    if self.pixtog==1:
                        sp.plot(self.wave[cfg.fitidx], self.normflux[cfg.fitidx], 'gs', markersize=4, mec='green')
                    model=joebvpfit.voigtfunc(self.wave,self.fitpars)
                    res=self.normflux-model
                    sp.plot(self.wave,model,'r')
                    if self.restog==1:
                        sp.plot(self.wave,-res,'.',color='black', ms=2)
                    sp.plot(self.wave,[0]*len(self.wave),color='gray')
    
                    ### label lines we are trying to fit
                    if self.labeltog==1:
                        for j in range(len(self.fitpars[0])):
                            labelloc=self.fitpars[0][j]*(1.+self.fitpars[3][j])+self.fitpars[4][j]/c*self.fitpars[0][j]*(1.+self.fitpars[3][j])
                            label = ' {:.1f}_\nz{:.4f}'.format(self.fitpars[0][j], self.fitpars[3][j])
                            sp.text(labelloc, cfg.label_ypos, label, rotation=90, withdash=True, ha='center', va='bottom', clip_on=True, fontsize=cfg.label_fontsize)
                
    
                sp.plot(self.wave,self.normsig,linestyle='steps-mid',color='red', lw=0.5)
                sp.plot(self.wave,-self.normsig,linestyle='steps-mid',color='red', lw=0.5)
                sp.set_ylim(cfg.ylim)
                sp.set_xlim(self.wave[prange[0]],self.wave[prange[-1]])
                sp.set_xlabel('wavelength (A)', fontsize=cfg.xy_fontsize, labelpad=cfg.x_labelpad)
                sp.set_ylabel('normalized flux', fontsize=cfg.xy_fontsize, labelpad=cfg.y_labelpad)
                sp.get_xaxis().get_major_formatter().set_scientific(False)
                sp.get_xaxis().get_major_formatter().set_useOffset(False)

        self.changefig(self.fig)

    def changefig(self, item):
        #text = str(item.text())
        self.rmmpl()
        self.addmpl(self.fig)

    def changesidefig(self, item):
        #text = str(item.text())
        self.rmsidempl()
        self.addsidempl(self.sidefig)
        
        
    def on_click(self, event):
        self.lastclick=event.xdata
        self.sideplot(self.lastclick)
        
    def addmpl(self, fig):
        self.canvas = FigureCanvas(fig)
        self.canvas.mpl_connect('button_press_event',self.on_click)
        self.mplvl.addWidget(self.canvas)
        self.canvas.draw()
        self.toolbar = NavigationToolbar(self.canvas,self.mplwindow,coordinates=True)
        self.mplvl.addWidget(self.toolbar)

    def addsidempl(self, sidefig):
        self.sidecanvas = FigureCanvas(sidefig)
        self.sidecanvas.setParent(self.sideMplWindow)
        if len(self.sidefig.axes) == 0:
            self.sidemplvl = QVBoxLayout()
        if len(self.sidefig.axes) != 0:
            self.sidemplvl.addWidget(self.sidecanvas)
        if len(self.sidefig.axes) == 0:
            self.sideMplWindow.setLayout(self.sidemplvl)
        self.sidecanvas.draw()

        
    def rmmpl(self,):
        self.mplvl.removeWidget(self.canvas)
        self.canvas.close()
        self.mplvl.removeWidget(self.toolbar)
        self.toolbar.close()

    def rmsidempl(self, ):
        self.sidemplvl.removeWidget(self.sidecanvas)
        self.sidecanvas.close()
Beispiel #29
0
class MplCanvas(object):
    """Ultimately, this is a QWidget (as well as a FigureCanvasAgg, etc.)."""

    def __init__(self, time_viewer, width, height, dpi):
        from PyQt5 import QtWidgets
        from matplotlib.figure import Figure
        from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg
        if time_viewer.separate_canvas:
            parent = None
        else:
            parent = time_viewer.window
        self.fig = Figure(figsize=(width, height), dpi=dpi)
        self.canvas = FigureCanvasQTAgg(self.fig)
        self.axes = self.fig.add_subplot(111)
        self.axes.set(xlabel='Time (sec)', ylabel='Activation (AU)')
        self.canvas.setParent(parent)
        FigureCanvasQTAgg.setSizePolicy(
            self.canvas,
            QtWidgets.QSizePolicy.Expanding,
            QtWidgets.QSizePolicy.Expanding
        )
        FigureCanvasQTAgg.updateGeometry(self.canvas)
        # XXX eventually this should be called in the window resize callback
        tight_layout(fig=self.axes.figure)
        self.time_viewer = time_viewer
        for event in ('button_press', 'motion_notify'):
            self.canvas.mpl_connect(
                event + '_event', getattr(self, 'on_' + event))

    def plot(self, x, y, label, **kwargs):
        """Plot a curve."""
        line, = self.axes.plot(
            x, y, label=label, **kwargs)
        self.update_plot()
        return line

    def plot_time_line(self, x, label, **kwargs):
        """Plot the vertical line."""
        line = self.axes.axvline(x, label=label, **kwargs)
        self.update_plot()
        return line

    def update_plot(self):
        """Update the plot."""
        self.axes.legend(prop={'family': 'monospace', 'size': 'small'},
                         framealpha=0.5, handlelength=1.)
        self.canvas.draw()

    def show(self):
        """Show the canvas."""
        self.canvas.show()

    def close(self):
        """Close the canvas."""
        self.canvas.close()

    def on_button_press(self, event):
        """Handle button presses."""
        # left click (and maybe drag) in progress in axes
        if (event.inaxes != self.axes or
                event.button != 1):
            return
        self.time_viewer.time_call(
            event.xdata, update_widget=True, time_as_index=False)

    on_motion_notify = on_button_press  # for now they can be the same
Beispiel #30
0
class Main(QMainWindow, Ui_MainWindow):
    def __init__(self, ):  #provide values for attributes at runtime
        super(Main, self).__init__()
        self.setupUi(self)
        self.pushBrowse.clicked.connect(self.selectFile)
        self.pushBrowse_2.clicked.connect(self.selectmlFile)
        self.pushApply.clicked.connect(self.apply)
        self.pushRun.clicked.connect(self.run)
        self.HomeDirectory = os.getcwd()  #saves the primary working directory
        self.directory = os.listdir(self.HomeDirectory)
        self.saveBttn.clicked.connect(self.file_save)
        self.actionOpen.triggered.connect(self.file_open)
        self.actionReset.triggered.connect(self.plots_close)
        self.message = 0
        self.UI = []
        self.reset = 0
        self.plotinfo = []
        self.plot2info = []
        self.plot3info = []
        self.plot4info = []
        self.plot5info = []
        self.plot6info = []

        #check User input for correct .csv file
        self.inputFile.setValidator(validator)
        self.inputFile.textChanged.connect(self.check_state)
        self.inputFile.textChanged.emit(self.inputFile.text())
        #check User input for correct .csv file for ml
        self.mlData.setValidator(validator)
        self.mlData.textChanged.connect(self.check_state)
        self.mlData.textChanged.emit(self.mlData.text())
        #shaft speed
        self.shaftSpeed.setValidator(regexp_checkdouble)
        self.shaftSpeed.textChanged.connect(self.check_state)
        self.shaftSpeed.textChanged.emit(self.shaftSpeed.text())
        #Num of rolling elements
        self.numberofElements.setValidator(regexp_checkint)
        self.numberofElements.textChanged.connect(self.check_state)
        self.numberofElements.textChanged.emit(self.numberofElements.text())
        #diameter of rolling elements
        self.diameterofElements.setValidator(regexp_checkdouble)
        self.diameterofElements.textChanged.connect(self.check_state)
        self.diameterofElements.textChanged.emit(
            self.diameterofElements.text())
        #pitch diameter
        self.pitchDiameter.setValidator(regexp_checkdouble)
        self.pitchDiameter.textChanged.connect(self.check_state)
        self.pitchDiameter.textChanged.emit(self.pitchDiameter.text())
        #Contact angle
        self.contactAngle.setValidator(regexp_checkdouble)
        self.contactAngle.textChanged.connect(self.check_state)
        self.contactAngle.textChanged.emit(self.contactAngle.text())
        #Frequency
        self.samFreq.setValidator(regexp_checkdouble)
        self.samFreq.textChanged.connect(self.check_state)
        self.samFreq.textChanged.emit(self.samFreq.text())

    def check_state(
        self, *args, **kwargs
    ):  #this function is changes the color of the lineedit fields depending on state
        sender = self.sender()
        validator = sender.validator()
        state = validator.validate(sender.text(), 0)[0]
        if state == QValidator.Acceptable:
            color = '#c4df9b'  # green
        elif state == QtGui.QValidator.Intermediate:
            color = '#fff79a'  # yellow
        else:
            color = '#f6989d'  # red
        sender.setStyleSheet('QLineEdit { background-color: %s }' % color)

    #creates a dictionary from the saved CSV
    def file_open(
        self
    ):  #function called when the open file action in triggered. Creates a dictionary from a CSV file.
        filename = QFileDialog.getOpenFileName()[0]
        reader = csv.reader(open(filename, 'r'))
        d = {}
        for row in reader:
            k, v = row
            d[k] = v

        print(d)

        self.setTextInfile(d)
        return True

    #used the dicitonary created above to assign saved variables to input parameters
    def setTextInfile(self, d):
        self.inputName.setText(d['inputName'])
        self.inputApplication.setText(d['inputApplication'])
        self.inputModelnum.setText(d['inputModelnum'])
        self.inputSavingalias.setText(d['inputSavingalias'])
        self.inputFile.setText(d['inputFile'])
        self.mlData.setText(d['mlData'])
        self.horsepower.setText(d['horsepower'])
        self.voltage.setText(d['voltage'])
        self.phase.setText(d['phase'])
        self.shaftnum.setText(d['shaftnum'])
        self.shaftSpeed.setText(d['shaftSpeed'])
        self.numberofElements.setText(d['numberofElements'])
        self.diameterofElements.setText(d['diameterofElements'])
        self.pitchDiameter.setText(d['pitchDiameter'])
        self.contactAngle.setText(d['contactAngle'])
        self.samFreq.setText(d['samFreq'])

    """
    Hmm i wonder if this is used to save the file?
    """

    def file_save(
        self,
    ):  #called when the save btn is clicked. converts user input to dictionary then to dataframe then to csv file.
        dict = CreateSaveDictionary(self.inputName.text(),
                                    self.inputApplication.text(),
                                    self.inputModelnum.text(),
                                    self.inputSavingalias.text(),
                                    self.inputFile.text(), self.mlData.text(),
                                    self.horsepower.text(),
                                    self.voltage.text(), self.phase.text(),
                                    self.shaftnum.text(),
                                    self.shaftSpeed.text(),
                                    self.numberofElements.text(),
                                    self.diameterofElements.text(),
                                    self.pitchDiameter.text(),
                                    self.contactAngle.text(),
                                    self.samFreq.text())
        CreateCSVfromDict(dict)

    """
    These functions create the figure widgets and toolbars
    """

    def addmpl(self, fig):
        self.canvas = FigureCanvas(fig)
        #self.canvas.setParent(None)
        self.spectrumUI.addWidget(self.canvas)
        #self.canvas.draw()
        #self.toolbar = NavigationToolbar(self.canvas,self.mainspectrum, coordinates=True)
        #self.toolbar.setParent(None)
        #self.spectrumUI.addWidget(self.toolbar)

    def addgraph11(self, fig):
        self.canvas1 = FigureCanvas(fig)
        self.graph11UI.addWidget(self.canvas1)
        #self.canvas1.draw()
        self.toolbar1 = NavigationToolbar(self.canvas1,
                                          self.graph11,
                                          coordinates=True)
        self.graph11UI.addWidget(self.toolbar1)

    def addgraph12(self, fig):
        self.canvas2 = FigureCanvas(fig)
        self.graph12UI.addWidget(self.canvas2)
        #self.canvas2.draw()
        self.toolbar2 = NavigationToolbar(self.canvas2,
                                          self.graph12,
                                          coordinates=True)
        self.graph12UI.addWidget(self.toolbar2)

    def addgraph13(self, fig):
        self.canvas5 = FigureCanvas(fig)
        self.graph13UI.addWidget(self.canvas5)
        #self.canvas2.draw()
        self.toolbar5 = NavigationToolbar(self.canvas5,
                                          self.graph13,
                                          coordinates=True)
        self.graph13UI.addWidget(self.toolbar5)

    def addgraph21(self, fig):
        self.canvas3 = FigureCanvas(fig)
        self.graph21UI.addWidget(self.canvas3)
        #self.canvas3.draw()
        self.toolbar3 = NavigationToolbar(self.canvas3,
                                          self.graph21,
                                          coordinates=True)
        self.graph21UI.addWidget(self.toolbar3)

    def addgraph22(self, fig):
        self.canvas4 = FigureCanvas(fig)
        self.graph22UI.addWidget(self.canvas4)
        #self.canvas4.draw()
        self.toolbar4 = NavigationToolbar(self.canvas4,
                                          self.graph22,
                                          coordinates=True)
        self.graph22UI.addWidget(self.toolbar4)

    #clearly selects file
    def selectFile(self, ):
        self.inputFile.setText(QFileDialog.getOpenFileName()[0])
        self.inputfile = self.inputFile.text()

    #selects file but for a ml data
    def selectmlFile(self, ):
        self.mlData.setText(QFileDialog.getOpenFileName()[0])

    """
    Apply checks user inputs and then assigns them to function parameter variables
    In case the user doesn't supply input for a specific field, default inputs will
    be inserted.
    """

    def apply(self, ):
        if self.inputSavingalias.text() != "":
            self.savingalias = self.inputSavingalias.text() + ".csv"
            self.inputSavingalias.setText(self.savingalias)

        if self.inputFile.text() != "":
            try:
                temp = self.inputFile.text()
                self.FileOfInterest = self.inputFile.text()
            except:
                print("pu")
        else:
            print("no input file selected, using demo file.")
            self.FileOfInterest = "AccelerometerActualData.csv"
            self.inputFile.setText("/AccelerometerActualData.csv")

        if self.mlData.text() != "":
            self.TrainingDataFile = self.mlData.text()
            print(self.TrainingDataFile)
        else:
            print("no ML data selected")
            self.TrainingDataFile = "NoNegatives.csv"  #default file location
            self.mlData.setText("/NoNegatives.csv")

        if self.shaftSpeed.text() != "":
            self.n = float(self.shaftSpeed.text())
            print("type n =", type(self.n))
        else:
            print("no shaft speed selected")
            self.n = 2000 / 60
            self.shaftSpeed.setText(str(self.n))

        if self.numberofElements.text() != "":
            self.N = int(self.numberofElements.text())
        else:
            print("Number of elements not specified")
            self.N = 16
            self.numberofElements.setText(str(self.N))

        if self.diameterofElements.text() != "":
            self.Bd = float(self.diameterofElements.text())
        else:
            print("Diameter of elements not specified")
            self.Bd = 0.331 * 254
            self.diameterofElements.setText(str(self.Bd))

        if self.pitchDiameter.text() != "":
            self.Pd = float(self.pitchDiameter.text())
        else:
            print("no pitch diameter specified")
            self.Pd = Pd = 2.815 * 254
            self.pitchDiameter.setText(str(self.Pd))

        if self.contactAngle.text() != "":
            self.phi = float(self.contactAngle.text())
        else:
            print("Contact angle not specified")
            self.phi = (15.17 * 3.14159) / 180
            self.contactAngle.setText(str(self.phi))

        if self.samFreq.text() != "":
            self.SampleFrequency = float(self.samFreq.text())

        else:
            print("no sample frequency specified")
            self.SampleFrequency = 20000
            self.samFreq.setText(str(self.SampleFrequency))

        self.popup = MyPopup("Applied")
        self.popup.setGeometry(QtCore.QRect(100, 100, 400, 200))
        self.popup.show()

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

    def getPlot(self, X, Y, xlabel, ylabel, Title):
        if self.reset != 0:
            self.sub0.cla()
        self.sub0.plot(X, Y, c=np.random.rand(3, ))
        self.sub0.set_xlabel(xlabel, fontsize=12)
        self.sub0.set_ylabel(ylabel, fontsize=12)
        self.sub0.set_title(Title)
        self.sub0.grid(True)
        if self.reset != 0:
            self.canvas.draw()

        return True

    def getPlot1(self, X, Y, xlabel, ylabel, Title):
        if self.reset != 0:
            self.sub1.cla()
        self.sub1.plot(X, Y, c=np.random.rand(3, ))
        self.sub1.set_xlabel(xlabel, fontsize=12)
        self.sub1.set_ylabel(ylabel, fontsize=12)
        self.sub1.set_title(Title)
        self.sub1.grid(True)
        if self.reset != 0:
            self.canvas1.draw()

        return True

    def getPlot2(self, X, Y, xlabel, ylabel, Title):
        if self.reset != 0:
            self.sub2.cla()
        self.sub2.plot(X, Y, c=np.random.rand(3, ))
        self.sub2.set_xlabel(xlabel, fontsize=12)
        self.sub2.set_ylabel(ylabel, fontsize=12)
        self.sub2.set_title(Title)
        self.sub2.grid(True)
        if self.reset != 0:
            self.canvas2.draw()

        return True

    def getPlot3(self, X, Y, xlabel, ylabel, Title):
        if self.reset != 0:
            self.sub3.cla()
        self.sub3.plot(X, Y, c=np.random.rand(3, ))
        self.sub3.set_xlabel(xlabel, fontsize=12)
        self.sub3.set_ylabel(ylabel, fontsize=12)
        self.sub3.set_title(Title)
        self.sub3.grid(True)
        if self.reset != 0:
            self.canvas3.draw()

        return True

    def getPlot4(self, X, Y, xlabel, ylabel, Title):
        if self.reset != 0:
            self.sub4.cla()
        self.sub4.plot(X, Y, c=np.random.rand(3, ))
        self.sub4.set_xlabel(xlabel, fontsize=12)
        self.sub4.set_ylabel(ylabel, fontsize=12)
        self.sub4.set_title(Title)
        self.sub4.grid(True)
        if self.reset != 0:
            self.canvas4.draw()

        return True

    def getPlot5(self, X, Y, xlabel, ylabel, Title):
        if self.reset != 0:
            self.sub4.cla()
        self.sub5.plot(X, Y, c=np.random.rand(3, ))
        self.sub5.set_xlabel(xlabel, fontsize=12)
        self.sub5.set_ylabel(ylabel, fontsize=12)
        self.sub5.set_title(Title)
        self.sub5.grid(True)
        if self.reset != 0:
            self.canvas5.draw()

        return True

    def run(self, ):  #called when run is clicked

        if self.reset == 0:
            #instantiate the figures
            self.fig0 = plt.figure()
            self.sub0 = self.fig0.add_subplot()

            self.fig1 = plt.figure()
            self.sub1 = self.fig1.add_subplot()

            self.fig2 = plt.figure()
            self.sub2 = self.fig2.add_subplot()

            self.fig3 = plt.figure()
            self.sub3 = self.fig3.add_subplot()

            self.fig4 = plt.figure()
            self.sub4 = self.fig4.add_subplot()

            self.fig5 = plt.figure()
            self.sub5 = self.fig5.add_subplot()

        #begin calling ml functions for processing
        Data = getValuesFromRawData(self.FileOfInterest)
        UserInput = UserInputs2WorkingForm(
            self.n, self.N, self.Bd, self.Pd, self.phi, self.SampleFrequency,
            Data, self.HomeDirectory, self.directory, self.TrainingDataFile,
            self.inputSavingalias, self.inputName, self.inputModelnum)
        self.UI = UserInput
        BearingInfo = BearingInfomation(UserInput)
        X_train, X_test, Y_train, Y_test = GetSplitTrainingData(UserInput)
        classifier = TrainModel(X_train, Y_train)
        Y_test_pred = PredictModel(classifier, X_test)
        ##
        #Create time series array
        t = np.arange(0, UserInput['Time of Sampling'],
                      1 / UserInput['Sampling Frequency'])

        #Perform FFT, PSD, Correlation, DC Offset
        UserInput1 = RemoveDCOffset(UserInput)
        x1 = FourierTransform(UserInput1)
        x2 = get_psd_values(UserInput1)
        x3 = get_autocorr_values(UserInput1)

        TEST = getTESTDataFrame(UserInput)
        TEST1 = TEST.values[:, 0:(TEST.shape[1] - 1)]
        OUTCOME = PredictModel(classifier, TEST1)
        plt.close('all')
        figs = []
        #figs = getGraphs(UserInput)
        self.plotinfo, self.plot2info, self.plot3info, self.plot4info, self.plot5info, self.plot6info = getGraphs(
            UserInput)

        self.getPlot(self.plotinfo[0], self.plotinfo[1], self.plotinfo[2],
                     self.plotinfo[3], self.plotinfo[4])
        self.getPlot1(self.plot2info[0], self.plot2info[1], self.plot2info[2],
                      self.plot2info[3], self.plot2info[4])
        self.getPlot2(self.plot3info[0], self.plot3info[1], self.plot3info[2],
                      self.plot3info[3], self.plot3info[4])
        self.getPlot3(self.plot4info[0], self.plot4info[1], self.plot4info[2],
                      self.plot4info[3], self.plot4info[4])
        self.getPlot4(self.plot5info[0], self.plot5info[1], self.plot5info[2],
                      self.plot5info[3], self.plot5info[4])
        self.getPlot5(self.plot6info[0], self.plot6info[1], self.plot6info[2],
                      self.plot6info[3], self.plot6info[4])

        Prediction = PredictProbModel(classifier, X_test)
        print("outcome:")
        print(OUTCOME)
        print("Accuracy on training set is : {}".format(
            classifier.score(X_train, Y_train)))
        print("Accuracy on test set is : {}".format(
            classifier.score(X_test, Y_test)))

        if self.reset == 0:
            self.addmpl(self.fig0)
            self.addgraph11(self.fig1)
            self.addgraph12(self.fig2)
            self.addgraph21(self.fig3)
            self.addgraph22(self.fig4)

        self.BSF.setText(str(truncate(BearingInfo['BSF'], 3)))
        self.BPFI.setText(str(truncate(BearingInfo['BPFI'], 3)))
        self.BPFO.setText(str(truncate(BearingInfo['BPFO'], 3)))
        self.FTF.setText(str(truncate(BearingInfo['FTF'], 3)))
        self.earlyEdit.setText(str(Prediction[0, 0]))
        self.suspectEdit.setText(str(Prediction[0, 1]))
        self.normalEdit.setText(str(Prediction[0, 2]))
        self.immEdit.setText(str(Prediction[0, 3]))
        self.innerEdit.setText(str(Prediction[0, 4]))
        self.rollingEdit.setText(str(Prediction[0, 5]))
        self.stageEdit.setText(str(Prediction[0, 6]))
        self.reset = 1

    def close_application(self, ):  #self explanitory
        sys.exit()


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

    def updategraphs(self, fig):
        print("made it to update graphs")
        sip.delete(self.canvas)
        sip.delete(self.canvas1)
        sip.delete(self.canvas2)
        sip.delete(self.canvas3)
        sip.delete(self.canvas4)
        #self.spectrumUI.removeWidget(self.canvas)
        self.canvas = FigureCanvas(fig[0])
        self.canvas1 = FigureCanvas(fig[1])
        self.canvas2 = FigureCanvas(fig[2])
        self.canvas3 = FigureCanvas(fig[3])
        self.canvas4 = FigureCanvas(fig[4])

    def rmmpl(self, ):
        print("in rmmpl")
        self.spectrumUI.removeWidget(self.canvas)
        self.canvas.close()
        self.spectrumUI.removeWidget(self.toolbar)
        self.toolbar.close()

        self.graph11UI.removeWidget(self.canvas)
        self.canvas.close()
        self.graph11UI.removeWidget(self.toolbar)
        self.toolbar.close()

        self.graph12UI.removeWidget(self.canvas)
        self.canvas.close()
        self.graph12UI.removeWidget(self.toolbar)
        self.toolbar.close()

        self.graph21UI.removeWidget(self.canvas)
        self.canvas.close()
        self.graph21UI.removeWidget(self.toolbar)
        self.toolbar.close()

        self.graph22UI.removeWidget(self.canvas)
        self.canvas.close()
        self.graph22UI.removeWidget(self.toolbar)
        self.toolbar.close()

    def plots_close(self, ):
        print("made it to plots_close")
        self.spectrumUI.removeWidget(self.canvas)
        sip.delete(self.canvas)
        self.canvas = None
        self.spectrumUI.removeWidget(self.toolbar)
        sip.delete(self.toolbar)
        self.toolbar = None

        self.graph11UI.removeWidget(self.canvas1)
        self.canvas1.close()
        self.graph11UI.removeWidget(self.toolbar1)
        self.toolbar1.close()

        self.graph12UI.removeWidget(self.canvas2)
        self.canvas2.close()
        self.graph12UI.removeWidget(self.toolbar2)
        self.toolbar2.close()

        self.graph21UI.removeWidget(self.canvas3)
        self.canvas3.close()
        self.graph21UI.removeWidget(self.toolbar3)
        self.toolbar3.close()

        self.graph22UI.removeWidget(self.canvas4)
        self.canvas4.close()
        self.graph22UI.removeWidget(self.toolbar4)
        self.toolbar4.close()
class MainWindow(QtWidgets.QMainWindow, form_class):
    def __init__(self, parent=None):
        QtWidgets.QMainWindow.__init__(self, parent)
        self.setupUi(self)
        self.cellpoints = np.array([])
        self.FindCells.clicked.connect(self.Id_cells)
        self.AddClassified.clicked.connect(self.create_csv)
        self.imageviewbutton.clicked.connect(self.openMainFig)
        self.numLayers.valueChanged.connect(self.redrawLayers)
        self.maxSigSpin.valueChanged.connect(self.Id_cells)
        self.minSigSpin.valueChanged.connect(self.Id_cells)
        self.log_overlap.valueChanged.connect(self.Id_cells)
        self.thresholdSpin.valueChanged.connect(self.Id_cells)
        self.cropsize = 25
        self.fig = Figure()
        self.THEimage = np.array([])
        self.BLUEimage = 0
        self.BLUEblobs = np.array([])
        self.REDimage = 0
        self.GREENimage = 0
        self.THEblobs = np.array([])
        self.table.setColumnCount(6)
        self.layout.addWidget(self.table, 1, 0)
        self.table.setHorizontalHeaderLabels([
            'Layer', 'Fluorescent cell count', 'Area', 'Density',
            'Nuclei count', 'Fluorescent fraction'
        ])
        for num, layer in enumerate(
            [str(x + 1) for x in range(int(self.numLayers.text()))] +
            ['Total selected reg', 'Total image']):
            self.table.insertRow(num)
            self.table.setItem(num, 0, QtWidgets.QTableWidgetItem(layer))
            self.table.setItem(num, 1, QtWidgets.QTableWidgetItem("0"))
            self.table.setItem(num, 2, QtWidgets.QTableWidgetItem("0"))
            self.table.setItem(num, 3, QtWidgets.QTableWidgetItem("0"))
            self.table.setItem(num, 4, QtWidgets.QTableWidgetItem("0"))
            self.table.setItem(num, 5, QtWidgets.QTableWidgetItem("0"))

        self.directory = 'singleCells/'
        self.guidePoints = {'TR': 0, 'TL': 0, 'BL': 0, 'BR': 0}
        self.innergridRight = [
            (self.guidePoints['TR'] * i + self.guidePoints['BR'] *
             (int(self.numLayers.text()) - i)) / int(self.numLayers.text())
            for i in range(1,
                           int(self.numLayers.text()) + 1)
        ]
        self.innergridLeft = [
            (self.guidePoints['TL'] * i + self.guidePoints['BL'] *
             (int(self.numLayers.text()) - i)) / int(self.numLayers.text())
            for i in range(1,
                           int(self.numLayers.text()) + 1)
        ]
        self.polygonList = []
        self.bigpoligon = 0
        self.figname = 0
        self.imgPolygon = 0
        #self.saveDir.setText('singleCells/')

    def openDIRwindow(self):
        dirwindow = allDirectoriesWindow(self)
        dirwindow.exec_()

    def removeCell(self, cellnumber):
        self.THEblobs[cellnumber:-1] = self.THEblobs[cellnumber + 1:]
        self.THEblobs = self.THEblobs[:-1]
        self.ImgAddPatches()

    def chooseDirectory(self):
        directory = QtWidgets.QFileDialog.getExistingDirectory(self)
        self.saveDir.setText(str(directory) + '/')
        self.DatabaseSize.setText(
            str(len(glob.glob(str(self.saveDir.text()) + '*.png'))))

    def openMainFig(self):
        if self.THEimage.any() == True:
            self.rmmpl()
            self.THEimage = np.array([])
            self.BLUEimage = 0
            while self.table.rowCount() < int(self.numLayers.text()) + 2:
                self.table.insertRow(0)
            while self.table.rowCount() > int(self.numLayers.text()) + 2:
                self.table.removeRow(0)
            for num, layer in enumerate(
                [str(x + 1) for x in range(int(self.numLayers.text()))] +
                ['Total selected reg', 'Total image']):
                self.table.setItem(num, 0, QtWidgets.QTableWidgetItem(layer))
                self.table.setItem(num, 1, QtWidgets.QTableWidgetItem("0"))
                self.table.setItem(num, 2, QtWidgets.QTableWidgetItem("0"))
                self.table.setItem(num, 3, QtWidgets.QTableWidgetItem("0"))
                self.table.setItem(num, 4, QtWidgets.QTableWidgetItem("0"))
                self.table.setItem(num, 5, QtWidgets.QTableWidgetItem("0"))
            self.directory = 'singleCells/'
            self.guidePoints = {'TR': 0, 'TL': 0, 'BL': 0, 'BR': 0}
            self.innergridRight = [
                (self.guidePoints['TR'] * i + self.guidePoints['BR'] *
                 (int(self.numLayers.text()) - i)) / int(self.numLayers.text())
                for i in range(1,
                               int(self.numLayers.text()) + 1)
            ]
            self.innergridLeft = [
                (self.guidePoints['TL'] * i + self.guidePoints['BL'] *
                 (int(self.numLayers.text()) - i)) / int(self.numLayers.text())
                for i in range(1,
                               int(self.numLayers.text()) + 1)
            ]
            self.polygonList = []
            self.bigpoligon = 0
            self.nMarkedCells.setText(str(0))
            self.THEblobs = np.array([])

        name = QtWidgets.QFileDialog.getOpenFileName(
            self, 'Single File', '~/Desktop/',
            "Image files (*.jpg *.png *.tif)")
        self.figname = str(name[0])
        image = imageio.imread(self.figname)
        #self.saveNames.setText(str(name).split("/")[-1][:-4] + 'i')
        self.THEimage = image
        self.imgPolygon = Polygon([[0, 0], [0, image.shape[1]],
                                   [image.shape[0], image.shape[1]],
                                   [image.shape[0], 0]])
        self.BLUEimage = image[:, :, 2]
        #self.BLUEblobs = blob_log(self.BLUEimage[self.cropsize:-self.cropsize,self.cropsize:-self.cropsize],  max_sigma=int(self.maxSigSpin.text()), num_sigma=10, min_sigma = int(self.minSigSpin.text()),overlap = float(self.log_overlap.text()) ,threshold=float(self.thresholdSpin.text()))
        self.REDimage = image[:, :, 0]
        self.GREENimage = image[:, :, 1]
        baseimage = self.fig.add_subplot(111)
        #baseimage.axis('off', frameon=False)
        #baseimage.grid(False)
        #baseimage.imshow(image)
        #axis('off')
        #subplots_adjust(left=0, bottom=0, right=1, top=1, wspace=0, hspace=0)

        self.fig, ax = subplots(1, 1)
        ax.imshow(self.THEimage)
        ax.axis('off')
        subplots_adjust(left=0, bottom=0, right=1, top=1, wspace=0, hspace=0)

        self.canvas = FigureCanvas(self.fig)
        self.mplvl.addWidget(self.canvas)
        self.canvas.draw()
        self.toolbar = NavigationToolbar(self.canvas,
                                         self.widget,
                                         coordinates=True)
        self.mplvl.addWidget(self.toolbar)
        cid = self.fig.canvas.mpl_connect('button_press_event', self.onclick)

    def onclick(self, event):
        print('button=%d, x=%d, y=%d, xdata=%f, ydata=%f' %
              (event.button, event.x, event.y, event.xdata, event.ydata))
        if event.button == 3:
            if str(self.rClicktype.currentText()) == 'Add cell':
                squaresize = self.cropsize
                #print(len(self.THEblobs))
                self.THEblobs = np.array(
                    self.THEblobs.tolist() +
                    [[int(event.ydata),
                      int(event.xdata), self.cropsize]])
                #print(len(self.THEblobs))
                #self.table.setHorizontalHeaderLabels(['index', 'auto class', 'manual class'])
                #rowPosition = self.table.rowCount()
                #self.table.insertRow(rowPosition)

                self.nMarkedCells.setText(
                    str(int(self.nMarkedCells.text()) + 1))
                self.ImgAddPatches()
            if str(self.rClicktype.currentText()) == 'Add 1st box corner':
                self.guidePoints['TR'] = [int(event.ydata), int(event.xdata)]
                self.rClicktype.setCurrentIndex == 'Add 2nd box corner'

                if 0 not in self.guidePoints.values():
                    self.polygonList = []
                    self.innergridRight = [
                        (array(self.guidePoints['TR']) * i +
                         array(self.guidePoints['BR']) *
                         (int(self.numLayers.text()) - i)) /
                        int(self.numLayers.text())
                        for i in range(0,
                                       int(self.numLayers.text()) + 1)
                    ]
                    self.innergridLeft = [
                        (array(self.guidePoints['TL']) * i +
                         array(self.guidePoints['BL']) *
                         (int(self.numLayers.text()) - i)) /
                        int(self.numLayers.text())
                        for i in range(0,
                                       int(self.numLayers.text()) + 1)
                    ]
                    #print(self.innergridLeft, self.innergridRight)
                    self.bigpoligon = Polygon([
                        self.guidePoints['TR'], self.guidePoints['TL'],
                        self.guidePoints['BL'], self.guidePoints['BR']
                    ])
                    for i in range(len(self.innergridLeft) - 1):
                        self.polygonList += [
                            Polygon([
                                self.innergridRight[i], self.innergridLeft[i],
                                self.innergridLeft[i + 1],
                                self.innergridRight[i + 1]
                            ])
                        ]
                self.ImgAddPatches()

            if str(self.rClicktype.currentText()) == 'Add 2nd box corner':
                self.guidePoints['TL'] = [int(event.ydata), int(event.xdata)]
                self.rClicktype.setCurrentIndex == 'Add 3rd box corner'

                if 0 not in self.guidePoints.values():
                    self.polygonList = []
                    self.innergridRight = [
                        (array(self.guidePoints['TR']) * i +
                         array(self.guidePoints['BR']) *
                         (int(self.numLayers.text()) - i)) /
                        int(self.numLayers.text())
                        for i in range(0,
                                       int(self.numLayers.text()) + 1)
                    ]
                    self.innergridLeft = [
                        (array(self.guidePoints['TL']) * i +
                         array(self.guidePoints['BL']) *
                         (int(self.numLayers.text()) - i)) /
                        int(self.numLayers.text())
                        for i in range(0,
                                       int(self.numLayers.text()) + 1)
                    ]
                    #print(self.innergridLeft, self.innergridRight)
                    self.bigpoligon = Polygon([
                        self.guidePoints['TR'], self.guidePoints['TL'],
                        self.guidePoints['BL'], self.guidePoints['BR']
                    ])
                    for i in range(len(self.innergridLeft) - 1):
                        self.polygonList += [
                            Polygon([
                                self.innergridRight[i], self.innergridLeft[i],
                                self.innergridLeft[i + 1],
                                self.innergridRight[i + 1]
                            ])
                        ]
                self.ImgAddPatches()

            if str(self.rClicktype.currentText()) == 'Add 3rd box corner':
                self.guidePoints['BL'] = [int(event.ydata), int(event.xdata)]
                self.rClicktype.setCurrentIndex == 'Add 4th box corner'

                if 0 not in self.guidePoints.values():
                    self.polygonList = []
                    self.innergridRight = [
                        (array(self.guidePoints['TR']) * i +
                         array(self.guidePoints['BR']) *
                         (int(self.numLayers.text()) - i)) /
                        int(self.numLayers.text())
                        for i in range(0,
                                       int(self.numLayers.text()) + 1)
                    ]
                    self.innergridLeft = [
                        (array(self.guidePoints['TL']) * i +
                         array(self.guidePoints['BL']) *
                         (int(self.numLayers.text()) - i)) /
                        int(self.numLayers.text())
                        for i in range(0,
                                       int(self.numLayers.text()) + 1)
                    ]
                    #print(self.innergridLeft, self.innergridRight)
                    self.bigpoligon = Polygon([
                        self.guidePoints['TR'], self.guidePoints['TL'],
                        self.guidePoints['BL'], self.guidePoints['BR']
                    ])
                    for i in range(len(self.innergridLeft) - 1):
                        self.polygonList += [
                            Polygon([
                                self.innergridRight[i], self.innergridLeft[i],
                                self.innergridLeft[i + 1],
                                self.innergridRight[i + 1]
                            ])
                        ]
                self.ImgAddPatches()

            if str(self.rClicktype.currentText()) == 'Add 4th box corner':
                self.guidePoints['BR'] = [int(event.ydata), int(event.xdata)]
                if 0 not in self.guidePoints.values():
                    self.polygonList = []
                    self.innergridRight = [
                        (array(self.guidePoints['TR']) * i +
                         array(self.guidePoints['BR']) *
                         (int(self.numLayers.text()) - i)) /
                        int(self.numLayers.text())
                        for i in range(0,
                                       int(self.numLayers.text()) + 1)
                    ]
                    self.innergridLeft = [
                        (array(self.guidePoints['TL']) * i +
                         array(self.guidePoints['BL']) *
                         (int(self.numLayers.text()) - i)) /
                        int(self.numLayers.text())
                        for i in range(0,
                                       int(self.numLayers.text()) + 1)
                    ]
                    #print(self.innergridLeft, self.innergridRight)
                    self.bigpoligon = Polygon([
                        self.guidePoints['TR'], self.guidePoints['TL'],
                        self.guidePoints['BL'], self.guidePoints['BR']
                    ])
                    #print(self.bigpoligon)
                    for i in range(len(self.innergridLeft) - 1):
                        self.polygonList += [
                            Polygon([
                                self.innergridRight[i], self.innergridLeft[i],
                                self.innergridLeft[i + 1],
                                self.innergridRight[i + 1]
                            ])
                        ]
                self.ImgAddPatches()
            if str(self.rClicktype.currentText()) == 'Remove cell':
                dist = np.sum(
                    (self.THEblobs[:, 0:2] - [event.ydata, event.xdata])**2, 1)
                if min(dist) < 800:
                    line = dist.tolist().index(min(dist))
                    #print(line)
                    self.removeCell(line)
                    self.nMarkedCells.setText(
                        str(int(self.nMarkedCells.text()) - 1))
                #self.ImgAddPatches()

        elif event.button == 2:
            #print(self.THEblobs[:,0:2])
            dist = np.sum(
                (self.THEblobs[:, 0:2] - [event.ydata, event.xdata])**2, 1)
            if min(dist) < 800:
                line = dist.tolist().index(min(dist))
                #print(line)
                self.removeCell(line)
                self.nMarkedCells.setText(
                    str(int(self.nMarkedCells.text()) - 1))
            #self.ImgAddPatches()

    def redrawLayers(self):
        if 0 not in self.guidePoints.values():
            self.polygonList = []
            self.innergridRight = [
                (array(self.guidePoints['TR']) * i +
                 array(self.guidePoints['BR']) *
                 (int(self.numLayers.text()) - i)) / int(self.numLayers.text())
                for i in range(0,
                               int(self.numLayers.text()) + 1)
            ]
            self.innergridLeft = [
                (array(self.guidePoints['TL']) * i +
                 array(self.guidePoints['BL']) *
                 (int(self.numLayers.text()) - i)) / int(self.numLayers.text())
                for i in range(0,
                               int(self.numLayers.text()) + 1)
            ]
            #print(self.innergridLeft, self.innergridRight)
            self.bigpoligon = Polygon([
                self.guidePoints['TR'], self.guidePoints['TL'],
                self.guidePoints['BL'], self.guidePoints['BR']
            ])
            #print(self.bigpoligon)
            for i in range(len(self.innergridLeft) - 1):
                self.polygonList += [
                    Polygon([
                        self.innergridRight[i], self.innergridLeft[i],
                        self.innergridLeft[i + 1], self.innergridRight[i + 1]
                    ])
                ]
        self.ImgAddPatches()

    def changeFIGURE(self, newFIG):
        self.rmmpl()
        self.canvas = FigureCanvas(newFIG)
        self.mplvl.addWidget(self.canvas)
        self.canvas.draw()
        self.toolbar = NavigationToolbar(self.canvas,
                                         self.widget,
                                         coordinates=True)
        self.mplvl.addWidget(self.toolbar)
        cid = self.fig.canvas.mpl_connect('button_press_event', self.onclick)

    def rmmpl(self, ):
        self.mplvl.removeWidget(self.canvas)
        self.canvas.close()
        self.mplvl.removeWidget(self.toolbar)
        self.toolbar.close()

    def Id_cells(self):
        if type(self.BLUEimage) == type(0): return
        while self.table.rowCount() < int(self.numLayers.text()) + 2:
            self.table.insertRow(0)
        while self.table.rowCount() > int(self.numLayers.text()) + 2:
            self.table.removeRow(0)
        for num, layer in enumerate(
            [str(x + 1) for x in range(int(self.numLayers.text()))] +
            ['Total selected reg', 'Total image']):
            self.table.setItem(num, 0, QtWidgets.QTableWidgetItem(layer))
            self.table.setItem(num, 1, QtWidgets.QTableWidgetItem("0"))
            self.table.setItem(num, 2, QtWidgets.QTableWidgetItem("0"))
            self.table.setItem(num, 3, QtWidgets.QTableWidgetItem("0"))
            self.table.setItem(num, 4, QtWidgets.QTableWidgetItem("0"))
            self.table.setItem(num, 5, QtWidgets.QTableWidgetItem("0"))

        squaresize = self.cropsize
        image_gray = self.BLUEimage

        self.BLUEblobs = blob_log(self.BLUEimage,
                                  max_sigma=int(self.maxSigSpin.text()),
                                  num_sigma=10,
                                  min_sigma=int(self.minSigSpin.text()),
                                  overlap=float(self.log_overlap.text()),
                                  threshold=float(self.thresholdSpin.text()),
                                  exclude_border=squaresize)
        self.table.setItem(
            int(self.numLayers.text()) + 1, 4,
            QtWidgets.QTableWidgetItem(str(len(self.BLUEblobs))))
        if str(self.fMarker.currentText()) == 'RFP':
            blobs = blob_log(self.REDimage,
                             max_sigma=int(self.maxSigSpin.text()),
                             num_sigma=10,
                             min_sigma=int(self.minSigSpin.text()),
                             overlap=float(self.log_overlap.text()),
                             threshold=float(self.thresholdSpin.text()),
                             exclude_border=squaresize)
            self.table.setItem(
                int(self.numLayers.text()) + 1, 4,
                QtWidgets.QTableWidgetItem(str(len(self.BLUEblobs))))
        if str(self.fMarker.currentText()) == 'GFP':
            blobs = blob_log(self.GREENimage,
                             max_sigma=int(self.maxSigSpin.text()),
                             num_sigma=10,
                             min_sigma=int(self.minSigSpin.text()),
                             overlap=float(self.log_overlap.text()),
                             threshold=float(self.thresholdSpin.text()),
                             exclude_border=squaresize)
            self.table.setItem(
                int(self.numLayers.text()) + 1, 4,
                QtWidgets.QTableWidgetItem(str(len(self.BLUEblobs))))
        if str(self.fMarker.currentText()) == 'GFP or RFP':
            blobs = blob_log(self.REDimage + self.GREENimage,
                             max_sigma=int(self.maxSigSpin.text()),
                             num_sigma=10,
                             min_sigma=int(self.minSigSpin.text()),
                             overlap=float(self.log_overlap.text()),
                             threshold=float(self.thresholdSpin.text()),
                             exclude_border=squaresize)
            self.table.setItem(
                int(self.numLayers.text()) + 1, 4,
                QtWidgets.QTableWidgetItem(str(len(self.BLUEblobs))))
        #blobsDAPI = blob_log(self.BLUEimage[squaresize:-squaresize,squaresize:-squaresize],  max_sigma=10, num_sigma=10, min_sigma = 3, threshold=.1)
        self.THEblobs = blobs
        self.nMarkedCells.setText(str(len(blobs)))
        self.table.setItem(
            int(self.numLayers.text()) + 1, 1,
            QtWidgets.QTableWidgetItem(str(len(blobs))))
        #self.table.setItem(9 , 2, QtWidgets.QTableWidgetItem(str(len(blobsDAPI))))
        if float(self.table.item(int(self.numLayers.text()) + 1,
                                 2).text()) != 0:
            self.table.setItem(
                int(self.numLayers.text()) + 1, 3,
                QtWidgets.QTableWidgetItem(
                    str(
                        float(
                            self.table.item(int(self.numLayers.text()) + 1,
                                            1).text()) /
                        float(
                            self.table.item(int(self.numLayers.text()) + 1,
                                            2).text()))))
        self.ImgAddPatches()

    def ImgAddPatches(self):
        colors = ['w', 'r', 'g', 'y', 'w', 'r', 'g', 'y', 'orange', 'w', 'r'
                  ] * 100
        squaresize = self.cropsize
        close(self.fig)
        self.fig, ax = subplots(1, 1)
        ax.imshow(self.THEimage)
        ax.axis('off')
        subplots_adjust(left=0, bottom=0, right=1, top=1, wspace=0, hspace=0)

        while self.table.rowCount() < int(self.numLayers.text()) + 2:
            self.table.insertRow(0)
        while self.table.rowCount() > int(self.numLayers.text()) + 2:
            self.table.removeRow(0)
        for num, layer in enumerate(
            [str(x + 1) for x in range(int(self.numLayers.text()))] +
            ['Total selected reg', 'Total image']):
            self.table.setItem(num, 0, QtWidgets.QTableWidgetItem(layer))
            self.table.setItem(num, 1, QtWidgets.QTableWidgetItem("0"))
            self.table.setItem(num, 2, QtWidgets.QTableWidgetItem("0"))
            self.table.setItem(num, 3, QtWidgets.QTableWidgetItem("0"))
            self.table.setItem(num, 4, QtWidgets.QTableWidgetItem("0"))
            self.table.setItem(num, 5, QtWidgets.QTableWidgetItem("0"))

        self.table.setItem(
            int(self.numLayers.text()) + 1, 1,
            QtWidgets.QTableWidgetItem(str(len(self.THEblobs))))
        self.table.setItem(
            int(self.numLayers.text()) + 1, 4,
            QtWidgets.QTableWidgetItem(str(len(self.BLUEblobs))))
        if float(self.table.item(int(self.numLayers.text()) + 1,
                                 4).text()) > 0:
            self.table.setItem(
                int(self.numLayers.text()) + 1, 5,
                QtWidgets.QTableWidgetItem(
                    str(
                        float(
                            self.table.item(int(self.numLayers.text()) + 1,
                                            1).text()) /
                        float(
                            self.table.item(int(self.numLayers.text()) + 1,
                                            4).text()))[:10]))

        if 0 not in self.guidePoints.values():
            ctr = 0
            polygonListCount = array([0 for i in self.polygonList])
            #print('pollistcount before:'+str(polygonListCount))
            for number, blob in enumerate(self.THEblobs):
                y, x, r = blob
                blobPoint = Point(y, x)
                if self.bigpoligon.contains(blobPoint):
                    ctr += 1
                    whichpolygon = [
                        1 if x.contains(blobPoint) else 0
                        for x in self.polygonList
                    ]
                    polygonListCount += array(whichpolygon)
                    #print('pollistcount:'+str(polygonListCount))
                    #c = Rectangle((x + int(squaresize/2), y + int(squaresize/2)),squaresize,squaresize, color=colors[whichpolygon.index(1)], linewidth=.5, alpha = 0.3)
                    c = Circle((x, y),
                               r + 10 if r + 10 < squaresize else squaresize,
                               color=colors[whichpolygon.index(1)],
                               linewidth=.5,
                               alpha=0.3)
                    ax.add_patch(c)
                    ax.text(x,
                            y,
                            polygonListCount[whichpolygon.index(1)],
                            color='white',
                            fontsize=10)
            self.nMarkedCells.setText(str(ctr))
            self.table.setItem(
                int(self.numLayers.text()) + 1, 2,
                QtWidgets.QTableWidgetItem(
                    str(self.imgPolygon.area / self.bigpoligon.area)[:4]))
            self.table.setItem(
                int(self.numLayers.text()), 2,
                QtWidgets.QTableWidgetItem(
                    str(int(self.bigpoligon.area / self.bigpoligon.area))))
            self.table.setItem(int(self.numLayers.text()), 1,
                               QtWidgets.QTableWidgetItem(str(ctr)))

            self.table.setItem(int(self.numLayers.text()), 3,
                               QtWidgets.QTableWidgetItem(str(ctr)))
            self.table.setItem(
                int(self.numLayers.text()) + 1, 3,
                QtWidgets.QTableWidgetItem(
                    str(
                        float(
                            self.table.item(int(self.numLayers.text()) + 1,
                                            1).text()) /
                        float(
                            self.table.item(int(self.numLayers.text()) + 1,
                                            2).text()))[:6]))

            for n, pol in enumerate(self.polygonList):
                self.table.setItem(
                    n, 2,
                    QtWidgets.QTableWidgetItem(
                        str(pol.area / self.bigpoligon.area)[:4]))
                self.table.setItem(
                    n, 3,
                    QtWidgets.QTableWidgetItem(
                        str(polygonListCount[n] /
                            (pol.area / self.bigpoligon.area))[:6]))
                self.table.setItem(
                    n, 1, QtWidgets.QTableWidgetItem(str(polygonListCount[n])))

            #### add blue cells to dapi count
            ctrDAPI = 0
            polygonListCountDAPI = array([0 for i in self.polygonList])
            for number, blob in enumerate(self.BLUEblobs):
                y, x, r = blob
                blobPoint = Point(y, x)
                if self.bigpoligon.contains(blobPoint):
                    ctrDAPI += 1
                    whichpolygonDAPI = [
                        1 if x.contains(blobPoint) else 0
                        for x in self.polygonList
                    ]
                    polygonListCountDAPI += array(whichpolygonDAPI)

            self.table.setItem(int(self.numLayers.text()), 4,
                               QtWidgets.QTableWidgetItem(str(ctrDAPI)))
            if float(self.table.item(int(self.numLayers.text()),
                                     4).text()) > 0:
                self.table.setItem(
                    int(self.numLayers.text()), 5,
                    QtWidgets.QTableWidgetItem(
                        str(
                            float(
                                self.table.item(int(self.numLayers.text()),
                                                1).text()) / ctrDAPI)))

            for n, pol in enumerate(self.polygonList):
                self.table.setItem(
                    n, 5,
                    QtWidgets.QTableWidgetItem(
                        str(polygonListCount[n] /
                            polygonListCountDAPI[n])[:6]))
                self.table.setItem(
                    n, 4,
                    QtWidgets.QTableWidgetItem(str(polygonListCountDAPI[n])))

        if 0 in self.guidePoints.values():
            for number, blob in enumerate(self.THEblobs):
                y, x, r = blob
                #c = Rectangle((x + int(squaresize/2), y + int(squaresize/2)),squaresize,squaresize, color='gray', linewidth=.5, alpha = 0.3)
                c = Circle((x, y),
                           r + 10 if r + 10 < squaresize else squaresize,
                           color='gray',
                           linewidth=.5,
                           alpha=0.3)
                ax.add_patch(c)
                ax.text(x, y, str(number), color='white', fontsize=6)
        for number, key in enumerate(self.guidePoints):
            if self.guidePoints[key] != 0:
                ax.add_patch(
                    Circle(self.guidePoints[key][::-1],
                           int(self.numLayers.text()),
                           color='w',
                           linewidth=2,
                           fill=True))

        if self.guidePoints['TR'] != 0 and self.guidePoints['TL'] != 0:
            ax.plot([self.guidePoints['TR'][1], self.guidePoints['TL'][1]],
                    [self.guidePoints['TR'][0], self.guidePoints['TL'][0]],
                    '-',
                    color='w',
                    linewidth=2)
        if self.guidePoints['TL'] != 0 and self.guidePoints['BL'] != 0:
            ax.plot([self.guidePoints['TL'][1], self.guidePoints['BL'][1]],
                    [self.guidePoints['TL'][0], self.guidePoints['BL'][0]],
                    '-',
                    color='w',
                    linewidth=2)
        if self.guidePoints['BR'] != 0 and self.guidePoints['BL'] != 0:
            ax.plot([self.guidePoints['BR'][1], self.guidePoints['BL'][1]],
                    [self.guidePoints['BR'][0], self.guidePoints['BL'][0]],
                    '-',
                    color='w',
                    linewidth=2)
        if self.guidePoints['TR'] != 0 and self.guidePoints['BR'] != 0:
            ax.plot([self.guidePoints['TR'][1], self.guidePoints['BR'][1]],
                    [self.guidePoints['TR'][0], self.guidePoints['BR'][0]],
                    '-',
                    color='w',
                    linewidth=2)

        if 0 not in self.guidePoints.values():
            for i in range(len(self.innergridLeft)):
                ax.plot([self.innergridRight[i][1], self.innergridLeft[i][1]],
                        [self.innergridRight[i][0], self.innergridLeft[i][0]],
                        '-',
                        color='w',
                        linewidth=1)
        ax.axis('off')
        subplots_adjust(left=0, bottom=0, right=1, top=1, wspace=0, hspace=0)
        for item in [self.fig, ax]:
            item.patch.set_visible(False)
        self.changeFIGURE(self.fig)

    def create_csv(self):
        layer = np.array([
            str(self.table.item(i, 0).text())
            for i in range(self.table.rowCount())
        ])
        fcells = np.array([
            str(self.table.item(i, 1).text())
            for i in range(self.table.rowCount())
        ])
        area = np.array([
            str(self.table.item(i, 2).text())
            for i in range(self.table.rowCount())
        ])
        density = np.array([
            str(self.table.item(i, 3).text())
            for i in range(self.table.rowCount())
        ])
        nucleiCount = np.array([
            str(self.table.item(i, 4).text())
            for i in range(self.table.rowCount())
        ])
        transfectedRatio = np.array([
            str(self.table.item(i, 5).text())
            for i in range(self.table.rowCount())
        ])
        classtable = DataFrame(
            np.transpose(
                np.vstack((layer, fcells, area, density, nucleiCount,
                           transfectedRatio
                           ))))  #, index=dates, columns=[nome , classe])
        print(classtable)
        saveclassification = classtable.to_csv(self.figname + '_count.csv',
                                               index=False,
                                               header=[
                                                   'layers',
                                                   'Fluorescent cells', 'Area',
                                                   'Density',
                                                   'DAPI cell count',
                                                   'Transfection efficiency'
                                               ])
Beispiel #32
0
class SWATGraph(QObject):
    """Display SWAT result data as line graphs or bar chart."""
    
    def __init__(self, csvFile):
        """Initialise class variables."""
        QObject.__init__(self)
        self._dlg = GraphDialog()
        self._dlg.setWindowFlags(self._dlg.windowFlags() & ~Qt.WindowContextHelpButtonHint)
        ## csv file of results
        self.csvFile = csvFile
        ## canvas for displaying matplotlib figure
        self.canvas = None
        ## matplotlib tool bar
        self.toolbar = None
        ## matplotlib axes
        self.ax1 = None
        
    def run(self):
        """Initialise form and run on initial csv file."""
        self._dlg.lineOrBar.addItem('Line graph')
        self._dlg.lineOrBar.addItem('Bar chart')
        self._dlg.lineOrBar.setCurrentIndex(0)
        self._dlg.newFile.clicked.connect(self.getCsv)
        self._dlg.updateButton.clicked.connect(self.updateGraph)
        self._dlg.closeForm.clicked.connect(self.closeFun)
        self.setUbuntuFont()
        self.readCsv()
        self._dlg.exec_()
        
    def addmpl(self, fig):
        """Add graph defined in fig."""
        self.canvas = FigureCanvas(fig)
        # graphvl is the QVBoxLayout instance added to the graph widget.
        # Needed to make fig expand to fill graph widget.
        self._dlg.graphvl.addWidget(self.canvas)
        self.canvas.draw()
        self.toolbar = NavigationToolbar(self.canvas, 
                self._dlg.graph, coordinates=True)
        self._dlg.graphvl.addWidget(self.toolbar)
        
    def rmmpl(self):
        """Remove current graph if any."""
        try:
            self._dlg.graphvl.removeWidget(self.canvas)
            self.canvas.close()
            self._dlg.graphvl.removeWidget(self.toolbar)
            self.toolbar.close()
            self.ax1 = None
            return
        except Exception:
            # no problem = may not have been a graph
            return
        
    @staticmethod
    def trans(msg):
        """Translate message."""
        return QApplication.translate("QSWATPlus", msg, None)
        
    @staticmethod
    def error(msg):
        """Report msg as an error."""
        msgbox = QMessageBox()
        msgbox.setWindowTitle('SWATGraph')
        msgbox.setIcon(QMessageBox.Critical)
        msgbox.setText(SWATGraph.trans(msg))
        msgbox.exec_()
        return
    
    def getCsv(self):
        """Ask user for csv file."""
        settings = QSettings()
        if settings.contains('/QSWATPlus/LastInputPath'):
            path = str(settings.value('/QSWATPlus/LastInputPath'))
        else:
            path = ''
        filtr = self.trans('CSV files (*.csv)')
        csvFile, _ = QFileDialog.getOpenFileName(None, 'Open csv file', path, filtr)
        if csvFile is not None and csvFile != '':
            settings.setValue('/QSWATPlus/LastInputPath', os.path.dirname(str(csvFile)))
            self.csvFile = csvFile
            self.readCsv()
        
    def readCsv(self):
        """Read current csv file (if any)."""
        # csvFile may be none if run from command line
        if not self.csvFile or self.csvFile == '':
            return
        if not os.path.exists(self.csvFile):
            self.error('Error: Cannot find csv file {0}'.format(self.csvFile))
            return
        """Read csv file into table; create statistics (coefficients); draw graph."""
        # clear graph
        self.rmmpl()
        # clear table
        self._dlg.table.clear()
        for i in range(self._dlg.table.columnCount()-1, -1, -1):
            self._dlg.table.removeColumn(i)
        self._dlg.table.setColumnCount(0)
        self._dlg.table.setRowCount(0)
        row = 0
        numCols = 0
        with open(self.csvFile, 'r', newline='') as csvFil:
            reader = csv.reader(csvFil)
            for line in reader:
                try:
                    # use headers in first line
                    if row == 0:
                        numCols = len(line)
                        for i in range(numCols):
                            self._dlg.table.insertColumn(i)
                        self._dlg.table.setHorizontalHeaderLabels(line)
                    else:
                        self._dlg.table.insertRow(row-1)
                        for i in range(numCols):
                            try:
                                val = line[i].strip()
                            except Exception as e:
                                self.error('Error: could not read file {0} at line {1} column {2}: {3}'.format(self.csvFile, row+1, i+1, repr(e)))
                                return
                            item = QTableWidgetItem(val)
                            self._dlg.table.setItem(row-1, i, item)
                    row = row + 1
                except Exception as e:
                    self.error('Error: could not read file {0} at line {1}: {2}'.format(self.csvFile, row+1, repr(e)))
                    return
        # columns are too narrow for headings
        self._dlg.table.resizeColumnsToContents()
        # rows are too widely spaced vertically
        self._dlg.table.resizeRowsToContents()
        self.writeStats()
        self.updateGraph()
        
    @staticmethod
    def makeFloat(s):
        """Parse string s as float and return; return nan on failure."""
        try:
            return float(s)
        except Exception:
            return float('nan')
        
    def updateGraph(self):
        """Redraw graph as line or bar chart according to lineOrBar setting."""
        style = 'bar' if self._dlg.lineOrBar.currentText() == 'Bar chart' else 'line'
        self.drawGraph(style)
        
    @staticmethod
    def shiftDates(dates, shift):
        """Add shift (number of days) to each date in dates."""
        delta = timedelta(days=shift)
        return [x + delta for x in dates]
    
    @staticmethod
    def getDateFormat(date):
        """
        Return date strptime format string from example date, plus basic width for drawing bar charts.
    
        Basic width is how matplotlib divides the date axis: number of days in the time unit
        Assumes date has one of 3 formats:
        yyyy: annual: return %Y and 12
        yyyy/m or yyyy/mm: monthly: return %Y/%m and 30
        yyyyddd: daily: return %Y%j and 24
        """
        if date.find('/') > 0:
            return '%Y/%m', 30
        length = len(date)
        if length == 4:
            return '%Y', 365
        if length == 7:
            return '%Y%j', 1
        SWATGraph.error('Cannot parse date {0}'.format(date))
        return '', 1
    
        
    def drawGraph(self, style):
        """Draw graph as line or bar chart according to style."""
        # preserve title, labels and yscale if they exist
        # in order to replace them when updating graph
        try:
            title = self.ax1.get_title()
        except Exception:
            title = ''
        try:
            xlbl = self.ax1.get_xlabel()
        except Exception:
            xlbl = ''
        try:
            ylbl = self.ax1.get_ylabel()
        except Exception:
            ylbl = ''
        try:
            yscl = self.ax1.get_yscale()
        except Exception:
            yscl = ''
        self.rmmpl()
        fig = Figure()
        # left, bottom, width, height adjusted to leave space for legend below
        self.ax1 = fig.add_axes([0.05, 0.22, 0.92, 0.68])
        numPlots = self._dlg.table.columnCount() - 1
        rng = range(self._dlg.table.rowCount())
        fmt, widthBase = self.getDateFormat(str(self._dlg.table.item(0, 0).text()).strip())
        if fmt == '':
            # could not parse
            return
        xVals = [datetime.strptime(str(self._dlg.table.item(i, 0).text()).strip(), fmt) for i in rng]
        for col in range(1, numPlots+1):
            yVals = [self.makeFloat(self._dlg.table.item(i, col).text()) for i in rng]
            h = self._dlg.table.horizontalHeaderItem(col).text()
            if style == 'line':
                self.ax1.plot(xVals, yVals, label=h)
                # reinstate yscale (only relevant for line graphs)
                if yscl == 'log':
                    self.ax1.set_yscale(yscl, nonposy='mask')
                elif yscl != '':
                    self.ax1.set_yscale(yscl)
            else:
                # width of bars in days.  
                # adding 1 to divisor gives space of size width between each date's group
                width = float(widthBase) / (numPlots+1)
                # can't imagine anyone wanting more than 7 colours
                # but just in case we'll use shades of grey for 8 upwards
                colours = ['b', 'g', 'r', 'c', 'm', 'y', 'k']
                colour = colours[col-1] if col <= 7 else str(float((col - 7)/(numPlots - 6)))
                mid = numPlots / 2
                shift = width * (col - 1 - mid)
                xValsShifted = xVals if shift == 0 else self.shiftDates(xVals, shift)
                self.ax1.bar(xValsShifted, yVals, width, color=colour, linewidth=0, label=h)
        # reinstate title and labels
        if title != '':
            self.ax1.set_title(title)
        if xlbl != '':
            self.ax1.set_xlabel(xlbl)
        else:
            self.ax1.set_xlabel('Date')
        if ylbl != '':
            self.ax1.set_ylabel(ylbl)
        self.ax1.grid(True)
        legendCols = min(4, self._dlg.table.columnCount())
        fontSize = 'x-small' if legendCols > 4 else 'small'
        self.ax1.legend(bbox_to_anchor=(1.0, -0.15), ncol=legendCols, fontsize=fontSize)
        self.addmpl(fig)
    
    def closeFun(self):
        """Close dialog."""
        self._dlg.close()
        
    def writeStats(self):
        """Write Pearson and Nash coefficients."""
        numCols = self._dlg.table.columnCount()
        numRows = self._dlg.table.rowCount()
        self._dlg.coeffs.clear()
        for i in range(1, numCols):
            for j in range(i+1, numCols):
                self.pearson(i, j, numRows)
        for i in range(1, numCols):
            for j in range(i+1, numCols):
                # only compute Nash-Sutcliffe Efficiency
                # if one plot is observed,
                # and use that as the first plot
                if str(self._dlg.table.horizontalHeaderItem(i).text()).find('observed') == 0:
                    if not str(self._dlg.table.horizontalHeaderItem(j).text()).find('observed') == 0:
                        self.nash(i, j, numRows)
                elif str(self._dlg.table.horizontalHeaderItem(j).text()).find('observed') == 0:
                    self.nash(j, i, numRows)
        
    def multiSums(self, idx1, idx2, N):
        """Return various sums for two series, only including points where both are numbers, plus count of such values."""
        s1 = 0
        s2 = 0
        s11 = 0
        s22 = 0
        s12 = 0
        count = 0
        for i in range(N):
            val1 = self.makeFloat(self._dlg.table.item(i, idx1).text())
            val2 = self.makeFloat(self._dlg.table.item(i, idx2).text())
            # ignore missing values
            if not (math.isnan(val1) or math.isnan(val2)):
                s1 += val1
                s2 += val2
                s11 += val1 * val1
                s22 += val2 * val2
                s12 += val1 * val2
                count = count + 1
        return (s1, s2, s11, s22, s12, count)
        
        
    def sum1(self, idx1, idx2, N):
        """Return sum for series1, only including points where both are numbers, plus count of such values.""" 
        s1 = 0
        count = 0
        for i in range(N):
            val1 = self.makeFloat(self._dlg.table.item(i, idx1).text())
            val2 = self.makeFloat(self._dlg.table.item(i, idx2).text())
            # ignore missing values
            if not (math.isnan(val1) or math.isnan(val2)):
                s1 += val1
                count = count + 1
        return (s1, count)
        
    def pearson(self, idx1, idx2, N):
        """Calculate and display Pearson correlation coefficients for each pair of plots."""
        s1, s2, s11, s22, s12, count = self.multiSums(idx1, idx2, N)
        if count == 0: return
        sqx = (count * s11) - (s1 * s1)
        sqy = (count * s22) - (s2 * s2)
        sxy = (count * s12) - (s1 * s2)
        deno = math.sqrt(sqx * sqy)
        if deno == 0: return
        rho = sxy / deno
        if count < N:
            extra = ' (using {0!s} of {1!s} values)'.format(count , N)
        else:
            extra = ''
        msg = 'Series1: ' + self._dlg.table.horizontalHeaderItem(idx1).text() + \
            '  Series2: ' + self._dlg.table.horizontalHeaderItem(idx2).text() + '  Pearson Correlation Coefficient = {0}{1}'.format(locale.format_string('%.2F', rho), extra)
        self._dlg.coeffs.append(SWATGraph.trans(msg))

    def nash(self, idx1, idx2, N):
        """Calculate and display Nash-Sutcliffe efficiency coefficients for each pair of plots where one is observed."""
        s1, count = self.sum1(idx1, idx2, N)
        if count == 0: return
        mean = s1 / count
        num = 0
        deno = 0
        for i in range(N):
            val1 = self.makeFloat(self._dlg.table.item(i, idx1).text())
            val2 = self.makeFloat(self._dlg.table.item(i, idx2).text())
            # ignore missing values
            if not (math.isnan(val1) or math.isnan(val2)):
                diff12 = val1 - val2
                diff1m = val1 - mean
                num += diff12 * diff12
                deno += diff1m * diff1m
        if deno == 0: return
        result = 1 - (num / deno)
        if count < N:
            extra = ' (using {0!s} of {1!s} values)'.format(count , N)
        else:
            extra = ''
        msg = 'Series1: ' + self._dlg.table.horizontalHeaderItem(idx1).text() + \
            '  Series2: ' + self._dlg.table.horizontalHeaderItem(idx2).text() + '   Nash-Sutcliffe Efficiency Coefficient = {0}{1}'.format(locale.format_string('%.2F', result), extra)
        self._dlg.coeffs.append(SWATGraph.trans(msg))
        
    def setUbuntuFont(self):
        """Set Ubuntu font size 10 as default."""
        QFontDatabase.addApplicationFont(":/fonts/Ubuntu-R.ttf")
        ufont = QFont("Ubuntu", 10, 1)
        QApplication.setFont(ufont)
Beispiel #33
0
def plotALot(
        img_array,
        gene_index_dict,  # list of genes to plot
        reordered_genes=None,
        savedir="",
        title="images",
        grid=(3, 6),  # grid to plot for each figure
        figsize=(16, 9),
        dpi=300,
):
    """
    plot a lot of images from a list of genes
    """
    genes_per_plot = grid[0] * grid[1]
    num_plots, remainder = divmod(len(gene_index_dict), (genes_per_plot))
    # add an extra plot if
    # number of genes is not perfectly divisible by number of plots
    if remainder != 0:
        num_plots += 1

    if reordered_genes is None:
        reordered_genes = [
            None,
        ] * len(gene_index_dict)
        for gene in gene_index_dict:
            reordered_genes[gene_index_dict[gene]["index"]] = gene

    # set up index for number of genes already plotted
    # ------------------------------------------------
    array_idx = 0
    for plot_num in range(num_plots):
        # set up figure canvas
        # --------------------
        fig = Figure(figsize=figsize, dpi=dpi)
        canvas = FigCanvas(fig)
        fig.set_canvas(canvas)

        for gridpos in range(genes_per_plot):
            # check if we have reached end of gene list
            # -----------------------------------------
            if array_idx == len(gene_index_dict):
                break

            # create temporary axes reference
            # -------------------------------
            ax = fig.add_subplot(grid[0], grid[1], gridpos + 1)

            # plot the current gene (array_idx)
            # ---------------------
            gene = reordered_genes[array_idx]
            ax.imshow(img_array[gene_index_dict[gene]["index"], ...],
                      cmap="hot")
            ax.set_title(gene)
            ax.grid(False)

            # increment gene index
            # --------------------
            array_idx += 1
        fig.suptitle(title + f" ({plot_num + 1} of {num_plots})")
        fig.tight_layout(rect=(0, 0, 1, .94))

        # save the plot
        #        time_now = datetime.datetime.now().strftime("%Y%m%d_%H%M")
        savename = (f"{title.replace(' ','_')}"
                    f"_{plot_num + 1}of{num_plots}.png")

        if not os.path.exists(savedir):
            os.mkdir(savedir)
        fig.savefig(os.path.join(savedir, savename), dpi=dpi)

        canvas.close()
        fig.clear()
class timeseriesplotDlg(QtWidgets.QDialog, Ui_timeseriesplotDialog):
    def __init__(self, parent=None):
        super(timeseriesplotDlg, self).__init__(parent)
        self.setupUi(self)
        self.setWindowFlags(QtCore.Qt.Window | QtCore.Qt.CustomizeWindowHint
                            | QtCore.Qt.WindowTitleHint
                            | QtCore.Qt.WindowCloseButtonHint
                            | QtCore.Qt.WindowMaximizeButtonHint)
        self.YcomboBox.addItems(DS.Lc)
        self.autocorrelationadioButton.setChecked(True)
        self.TcheckBox.setChecked(True)
        self.XGcheckBox.setChecked(False)
        self.YGcheckBox.setChecked(False)
        self.XMcheckBox.setChecked(True)
        self.YMcheckBox.setChecked(True)
        self.XcheckBox.setChecked(True)
        self.YcheckBox.setChecked(True)
        self.ApplyButton.clicked.connect(self.redraw)
        self.ResetButton.clicked.connect(self.reset)
        fig = Figure()
        ax = fig.add_subplot(111)
        ax.plot(np.array(0))
        ax.set_xlim([0, 1])
        ax.set_ylim([0, 1])
        self.addmpl(fig)

    def redraw(self):
        if self.YcomboBox.currentText() == 'None':
            QtWidgets.QMessageBox.critical(self, 'Error',
                                           "No Variable \n Selected !",
                                           QtWidgets.QMessageBox.Ok)
            return ()
        data = DS.Raw.iloc[DS.Ir, DS.Ic]
        data = data.assign(Lr=DS.Lr[DS.Ir])
        data = data.assign(Cr=DS.Cr[DS.Ir])
        data = data[[self.YcomboBox.currentText(), 'Lr', 'Cr']]
        Nnan = data[self.YcomboBox.currentText()].isnull().all()
        data = data.dropna()
        #Lr=data['Lr'].values
        data = data.drop('Lr', axis=1)
        data = data.drop('Cr', axis=1)
        Y = data.values.ravel()
        if Y.dtype == 'float' and Y.dtype == 'int':
            QtWidgets.QMessageBox.critical(self,'Error',"Some values are not numbers!",\
                                           QtWidgets.QMessageBox.Ok)
            return ()
        color = 'blue'
        fig = Figure()
        ax = fig.add_subplot(111)
        if self.CcheckBox.isChecked():
            color = DS.Cc[self.YcomboBox.currentIndex() - 1]
        if (self.autocorrelationadioButton.isChecked()):
            plot_acf(Y,
                     ax=ax,
                     color=color,
                     alpha=int(self.alfaspinBox.value()) / 100)
            ax.set_title('Autocorrelation Plot of ' +
                         self.YcomboBox.currentText())
        elif (self.spectrumradioButton.isChecked()):
            ps = abs(np.fft.fft(Y))**2
            time_step = 1 / int(self.freqSpinBox.value())
            freqs = np.fft.fftfreq(Y.size, time_step)
            idx = np.argsort(freqs)
            freqs = freqs[idx]
            ps = ps[idx]
            red = freqs > 0
            ax.plot(freqs[red], ps[red])
            ax.set_title('Spectral Plot of ' + self.YcomboBox.currentText())
            ax.set_xlabel('Frequency')
            ax.set_ylabel('Spectrum')
        if Nnan:
            ax.annotate('NaN present',
                        xy=(0.05, 0.95),
                        xycoords='axes fraction')
        if self.TcheckBox.isChecked():
            if self.TlineEdit.text():
                ax.set_title(self.TlineEdit.text())
        else:
            ax.set_title('')
        if self.XcheckBox.isChecked():
            if self.XlineEdit.text():
                ax.set_xlabel(self.XlineEdit.text())
        else:
            ax.set_xlabel('')
        if self.YcheckBox.isChecked():
            ax.set_ylabel(self.YlineEdit.text())
        else:
            ax.set_ylabel('')
        if self.XGcheckBox.isChecked():
            ax.xaxis.grid(True)
        else:
            ax.xaxis.grid(False)
        if self.YGcheckBox.isChecked():
            ax.yaxis.grid(True)
        else:
            ax.yaxis.grid(False)
        if not self.XMcheckBox.isChecked():
            ax.tick_params(axis='x',
                           which='both',
                           bottom='off',
                           top='off',
                           labelbottom='off')
        if not self.YMcheckBox.isChecked():
            ax.tick_params(axis='y',
                           which='both',
                           left='off',
                           right='off',
                           labelleft='off')
        self.rmmpl()
        self.addmpl(fig)

    def reset(self):
        self.YcomboBox.setCurrentIndex(0)
        self.XGcheckBox.setChecked(False)
        self.YGcheckBox.setChecked(False)
        self.XMcheckBox.setChecked(False)
        self.YMcheckBox.setChecked(True)
        self.XcheckBox.setChecked(True)
        self.YcheckBox.setChecked(True)
        self.XlineEdit.setText('')
        self.YlineEdit.setText('')
        self.TlineEdit.setText('')
        self.update()

    def addmpl(self, fig):
        self.canvas = FigureCanvas(fig)
        self.mplvl.addWidget(self.canvas)
        self.canvas.draw()
        self.toolbar = NavigationToolbar(self.canvas,
                                         self.mplwindow,
                                         coordinates=True)
        self.mplvl.addWidget(self.toolbar)

    def rmmpl(self, ):
        self.mplvl.removeWidget(self.canvas)
        self.canvas.close()
        self.mplvl.removeWidget(self.toolbar)
        self.toolbar.close()