Exemple #1
0
def temporary_read_data(sfile):
    sfile = str(sfile)

    from LoadWriteData import LoadData
    ld = LoadData()
    adata = ld.read_data(sfile)

    return adata
Exemple #2
0
    def __init__(self):
        Qt.QObject.__init__(self)

        self._log = logging.getLogger(ConfigStatic.logger_name)
        self._dataLoader = LoadData()
        self._dict_clin_info = ClinInfoForm.dictClinInfoFullNames
        self._metainfo = ClinInfoForm.metainfofile
        self._metainfo_md5 = ClinInfoForm.metainfofile_md5sum
        self._stop = False
    def __init__(self, parent = None):
        QtGui.QWidget.__init__(self, parent)

        # musim importovat tady, jinak mam kruhovou zavislost
        from PyQwtNavigationPlot import PyQwtNavigationPlot
        from LoadWriteData import LoadData
        # from StyleManager import styleManager
        # from PyQwtPlotTools import PyQwtPlotTools

        self.setMinimumSize(800, 500)

        # self._styleManager = styleManager()
        # self._styleManager.setStyleManager(self._styleManager)

        vbox = QtGui.QVBoxLayout()

        controled_plots = list()
        self.fhrPlot = FhrPlot(self)
        self.tocoPlot = TocoPlot(self)
        # self.tocoPlot.setSizePolicy(QSizePolicy.Minimum,QSizePolicy.Minimum)

        controled_plots.append(self.fhrPlot)
        controled_plots.append(self.tocoPlot)
        self.navPlot = PyQwtNavigationPlot(self, controled_plots)

        # self.navPlot = PyQwtNavigationPlot(self,controledPlots)
        # self.plotTools = PyQwtPlotTools(self, self.fhrPlot, self._styleManager)
        # self.plot.setMargin(5)

        vbox.addWidget(self.fhrPlot)
        vbox.addWidget(self.tocoPlot)
        # vbox.addLayout(self.plotTools.ToolbarHBoxLayout)
        vbox.addWidget(self.navPlot)

        self.setLayout(vbox)

        self._dataLoader = LoadData()
        file1 = 'files/1001.dat'

        # l = self._dataLoader.readPhysionetHeader(file2)
        adata, dummy = self._dataLoader.read_physionet_signal16(file1)

        # dictClinInfo = lHeader[2]
        # print dictClinInfo['Pos_IIst']

        fhr = adata[EnumVariableName.fhr]
        toco = adata[EnumVariableName.uc]
        time_string = samples2time(len(fhr), 4)
        time_samp = adata['time_samp']
        # timeStamp_samp = aData[:,2]*4

        self.fhrPlot.plot(time_samp,fhr, time_string)
        self.tocoPlot.plot(time_samp, toco, time_string)
        self.navPlot.plot(time_samp, fhr, time_string)
Exemple #4
0
class TestReadWrite(unittest.TestCase):
    def setUp(self):
        # Init.init()
        self._dataLoader = LoadData()

    def tearDown(self):
        pass

    def test_read_header(self):

        file1 = os.path.join('files', '1001.hea')
        lheader, nrsignals, nfs, nrsamples = self._dataLoader.read_physionet_header(
            file1)

        dict1 = lheader[0]
        dict2 = lheader[1]

        self.assertEqual(nrsignals, 2)
        self.assertEqual(nfs, 4)
        self.assertEqual(nrsamples, 19200)

        self.assertEqual(dict1['format'], 16)
        self.assertEqual(dict2['format'], 16)
        self.assertEqual(dict1['gain'], 100)
        self.assertEqual(dict2['gain'], 100)

        self.assertEqual(dict1['firstvalue'], 15050)
        self.assertEqual(dict2['firstvalue'], 700)

    def test_read_signal(self):

        file1 = os.path.join('files', '1001.dat')
        adata, dummy = self._dataLoader.read_physionet_signal16(file1)

        fhr = adata[Enum.fhr]
        uc = adata[Enum.uc]

        self.assertEqual(fhr[0], 150.5)
        self.assertEqual(fhr[3], 151.25)
        self.assertEqual(fhr[18978], 94.75)

        self.assertEqual(uc[0], 7.0)
        self.assertEqual(uc[1], 8.5)
Exemple #5
0
    def __init__(self, parent=None, config_ini=None):

        QtGui.QWidget.__init__(self, parent)

        self.ui = Ui_DataBrowser()
        self.ui.setupUi(self)

        self._log = logging.getLogger(ConfigStatic.logger_name)
        self._log.info('passed')

        # self.setMinimumSize(300, 100)

        # CONFIG
        self._config_ini = config_ini or ConfigIni()

        self._dataLoader = LoadData()

        self._source_dir = ''
        self._filesExtDat = '.dat'
        self._filesExtHea = '.hea'
        self._filesExtMat = '.mat'
        self._files_ext = ''
        self._nr_files_metainfo = 0
        self._metainfo_handler = MetainfoFileConvertWorker()

        self._model = QtGui.QStandardItemModel()
        self._table = self.ui.tableView

        self.msgBoxError = QtGui.QMessageBox()
        self.msgBoxError.setDefaultButton(QtGui.QMessageBox.Close)
        self.msgBoxError.setIcon(QtGui.QMessageBox.Critical)

        self._progress_dialog = QtGui.QProgressDialog()
        self._progress_dialog.setLabelText("Creating file metainfo ...")
        self._progress_dialog.setWindowModality(QtCore.Qt.WindowModal)
        self._progress_dialog.close()

        # connections
        self._table.doubleClicked.connect(self._table_double_click)
        self.ui.btnNext.clicked.connect(self._current_row_increment)
        self.ui.btnPrev.clicked.connect(self._current_row_decrement)
        self._metainfo_handler.nr_file_processed.connect(
            self._update_metainfo_progress)
        self._progress_dialog.canceled.connect(self._stop_create_metainfo)

        self._selected_att = list()

        if self.bDebugStageIICorrect:
            import datetime
            self._metainfofile_stage2_corr = 'metainfo_corrected_' + str(
                datetime.datetime.utcnow()) + '.csv'
Exemple #6
0
    def __init__(self):
        QtGui.QWidget.__init__(self)

        self.ui = Ui_ConvertFile()
        self.ui.setupUi(self)

        pal = QtGui.QPalette()
        pal.setColor(QtGui.QPalette.Base, pal.background().color())
        self.ui.textEditInfo.setPalette(pal)
        self.ui.textEditInfo.setReadOnly(True)

        self.listAllowedFormats = ['dat', 'csv', 'mat', 'txt']
        self.listFormatTooltips = [
            'physionet format', 'comma separated value', 'matlab format',
            'BDI txt'
        ]

        self._files_to_convert = []
        self._dataLoader = LoadData()
        self._convertWorker = MetainfoFileConvertWorker()
        self._convertWorker.file_processed.connect(self._update_text_edit)

        self._dictClinInfo = ClinInfoForm.dictClinInfoFullNames

        self._setup_combo_boxes()

        self.ui.btnBrowseSource.clicked.connect(self._open_dir_source)
        self.ui.btnBrowseDest.clicked.connect(self._open_directory_dest)
        self.ui.btnConvert.clicked.connect(self._convert)
        self.ui.btnStop.clicked.connect(self._stop)

        self.msgBoxError = QtGui.QMessageBox()
        self.msgBoxError.setDefaultButton(QtGui.QMessageBox.Close)
        self.msgBoxError.setIcon(QtGui.QMessageBox.Critical)

        self._bAbort = False
        self.ui.btnStop.setEnabled(False)
Exemple #7
0
class MetainfoFileConvertWorker(Qt.QObject):
    """
    Worker for converting files.
    This worker has been extracted to be independent from GUI
    """

    file_processed = pyqtSignal(['QString'])
    nr_file_processed = pyqtSignal(['int'])

    def __init__(self):
        Qt.QObject.__init__(self)

        self._log = logging.getLogger(ConfigStatic.logger_name)
        self._dataLoader = LoadData()
        self._dict_clin_info = ClinInfoForm.dictClinInfoFullNames
        self._metainfo = ClinInfoForm.metainfofile
        self._metainfo_md5 = ClinInfoForm.metainfofile_md5sum
        self._stop = False

    @staticmethod
    def get_md5sum(files):

        md5 = hashlib.md5()
        for f in files:
            with open(f, 'r') as fr:
                md5.update(fr.read(8192))

        return md5.hexdigest()

    def metainfo_up_to_date(self, dir_dest, files):
        """
        Create md5sum from given files and compare it with metainfo md5sum

        :param dir_dest:
        :param files:
        :return: True or False
        """

        sfile_metainfo = os.path.join(dir_dest, self._metainfo)
        sfile_metainfo_md5 = os.path.join(dir_dest, self._metainfo_md5)

        if not Common.file_exists(sfile_metainfo):
            return False

        if not Common.file_exists(sfile_metainfo_md5):
            return False

        md5sum = self.get_md5sum(files)

        # read md5sum from file
        with open(sfile_metainfo_md5, 'r') as fr:
            md5expected = fr.readline()

        return md5expected == md5sum

    def metainfo_md5sum_update(self, dir_dest, files):
        """
        For the given files - write md5sum

        :param dir_dest:
        :param files:
        :return:
        """
        sfile_metainfo_md5 = os.path.join(dir_dest, self._metainfo_md5)
        md5sum = self.get_md5sum(files)

        with open(sfile_metainfo_md5, 'w+') as fw:
            fw.write(md5sum)

    def metainfo_create_header_csv(self, file_clinical_info):
        """
        Write header

        :param file_clinical_info:
        :return:
        """

        with open(file_clinical_info, 'w+') as fw:
            cnt = 0
            for s in self._dict_clin_info.keys():
                fw.write(self._dict_clin_info[s])
                cnt += 1
                if cnt == len(self._dict_clin_info):
                    fw.write('\n')
                else:
                    fw.write(',')

    def metainfo_create_csv(self, dir_dest, files):
        """
        Write information in given files into a metainfo file (ussually csv file)

        :param dir_dest:
        :param files:
        :return:
        """

        # TBME2015 classification (TEMPORARY)
        bloadclassification = False
        if bloadclassification:
            sfile = '/home/jirka/svn_working_copy/iga_brno/matlab/ENSL/barry_schifrin/TBME2015_classification_1288.json'
            dclassification = dict()
            import json
            with open(sfile, 'r') as f:
                dclassification = json.load(f)

        self._stop = False
        sfile_metainfo = os.path.join(dir_dest, self._metainfo)

        self.metainfo_create_header_csv(sfile_metainfo)
        fclin_info = open(sfile_metainfo, 'a')
        cnt_files = 0

        for f in files:
            if self._stop is True:
                self.file_processed.emit(QString('Metainfo creation stop'))
                return -1

            try:
                header = self._dataLoader.read_data(f)

                if bloadclassification:
                    name = header['name']
                    # print name
                    if name in dclassification:
                        # print dclassification[name]
                        header['Note'] = dclassification[name]

                cnt = 0
                for key in self._dict_clin_info.keys():
                    if key in header:
                        val = header[key]
                        fclin_info.write(str(val))
                    else:
                        fclin_info.write('')

                    cnt += 1
                    if cnt == len(self._dict_clin_info):
                        fclin_info.write('\n')
                    else:
                        fclin_info.write(',')

                cnt_files += 1
                self.nr_file_processed.emit(cnt_files)

            except Exception, msg:
                self._log.error('File {0} - ERROR: msg={1}'.format(
                    sfile_metainfo, msg))

        self.metainfo_md5sum_update(dir_dest, files)

        msg = 'File {0} written'.format(sfile_metainfo)
        self.file_processed.emit(QString(msg))
Exemple #8
0
class ConvertFileForm(QtGui.QWidget):

    stop_signal = pyqtSignal()

    def __init__(self):
        QtGui.QWidget.__init__(self)

        self.ui = Ui_ConvertFile()
        self.ui.setupUi(self)

        pal = QtGui.QPalette()
        pal.setColor(QtGui.QPalette.Base, pal.background().color())
        self.ui.textEditInfo.setPalette(pal)
        self.ui.textEditInfo.setReadOnly(True)

        self.listAllowedFormats = ['dat', 'csv', 'mat', 'txt']
        self.listFormatTooltips = [
            'physionet format', 'comma separated value', 'matlab format',
            'BDI txt'
        ]

        self._files_to_convert = []
        self._dataLoader = LoadData()
        self._convertWorker = MetainfoFileConvertWorker()
        self._convertWorker.file_processed.connect(self._update_text_edit)

        self._dictClinInfo = ClinInfoForm.dictClinInfoFullNames

        self._setup_combo_boxes()

        self.ui.btnBrowseSource.clicked.connect(self._open_dir_source)
        self.ui.btnBrowseDest.clicked.connect(self._open_directory_dest)
        self.ui.btnConvert.clicked.connect(self._convert)
        self.ui.btnStop.clicked.connect(self._stop)

        self.msgBoxError = QtGui.QMessageBox()
        self.msgBoxError.setDefaultButton(QtGui.QMessageBox.Close)
        self.msgBoxError.setIcon(QtGui.QMessageBox.Critical)

        self._bAbort = False
        self.ui.btnStop.setEnabled(False)

        # s = '/home/jirka/data/igabrno/CTU_UHB_physionet'
        # self.ui.lnSource.setText(s)
        # self.ui.lnDestination.setText(s)

        # self.ui.lnSource.editingFinished.connect(self.checkSourceDirForFiles)

    def _setup_combo_boxes(self):

        # cnt = 0
        self.ui.cbFrom.addItem('dat')
        self.ui.cbFrom.addItem('mat')
        self.ui.cbTo.addItem('csv')

        # for l in self.listAllowedFormats:
        #    self.ui.cbFrom.addItem(l)
        #    self.ui.cbTo.addItem(l)
        #    self.ui.cbFrom.setItemData(cnt, self.listFormatTooltips[cnt], Qt.Qt.ToolTipRole)
        #    self.ui.cbTo.setItemData(cnt, self.listFormatTooltips[cnt], Qt.Qt.ToolTipRole)
        #    cnt += 1

        self.ui.cbFrom.setCurrentIndex(0)
        self.ui.cbTo.setCurrentIndex(0)

    def _open_dir_source(self):
        open_dialog = QtGui.QFileDialog(self, 'Open directory', '')
        dir1 = open_dialog.getExistingDirectory()

        if dir == "":
            return -1
        else:
            dir1 = str(dir1)
            self.ui.lnSource.setText(dir1)
            self.check_source_dir_for_files()

        return 0

    def _open_directory_dest(self):
        open_dialog = QtGui.QFileDialog(self, 'Open directory', '')
        dir1 = open_dialog.getExistingDirectory()

        if dir == "":
            return -1
        else:
            dir1 = str(dir1)
            self.ui.lnDestination.setText(dir1)

        return 0

    def check_source_dir_for_files(self):

        dir1 = str(self.ui.lnSource.text())
        ext_mask = str(self.ui.cbFrom.itemText(self.ui.cbFrom.currentIndex()))

        check_directory_exists(dir1)

        files = Common.get_files_with_ext_mask(dir1, ext_mask)
        files.sort()

        self.ui.labFile2Convert.setText(str(len(files)))
        self._files_to_convert = files

    def _update_text_edit(self, msg):
        self.ui.textEditInfo.append(msg)
        Qt.QCoreApplication.processEvents()

    def _stop(self):
        self.ui.btnStop.setEnabled(False)
        self._convertWorker.stop()

    def _convert(self):

        self.ui.textEditInfo.clear()
        self.ui.btnConvert.setEnabled(False)
        self.ui.btnStop.setEnabled(True)

        # check source files
        ext_from = str(self.ui.cbFrom.currentText())
        ext_to = str(self.ui.cbTo.currentText())

        if ext_from == ext_to:
            self.msgBoxError.setText('Cannot convert to the same format')
            self.msgBoxError.exec_()
            return

        dir1 = str(self.ui.lnSource.text())
        dir2 = str(self.ui.lnDestination.text())

        try:
            check_directory_exists(dir1)
        except Exception as ex:
            self.msgBoxError.setText('Error in source directory')
            self.msgBoxError.setInformativeText(ex.message)
            self.msgBoxError.exec_()
            return

        try:
            check_directory_exists(dir2)
        except Exception as ex:
            self.msgBoxError.setText('Error in destination directory')
            self.msgBoxError.setInformativeText(ex.message)
            self.msgBoxError.exec_()
            return

        self.check_source_dir_for_files()

        print len(self._files_to_convert)
        if len(self._files_to_convert) == 0:
            self.msgBoxError.setText(
                'No files with format {0} in specified directory {1}'.format(
                    ext_from, dir1))
            self.msgBoxError.exec_()
            return

        if ext_from == 'dat' and ext_to == 'csv':

            # self._convertWorker.convert_files_csv(dir2, self._files_to_convert)

            files_convert_hea = list()
            for f in self._files_to_convert:
                files_convert_hea.append(
                    self._dataLoader.get_physionet_header_name_for_dat(f))

            self._convertWorker.metainfo_create_csv(dir2, files_convert_hea)
            return

        if ext_from == 'mat' and ext_to == 'csv':
            self._convertWorker.convert_files_csv(dir2, self._files_to_convert)
            self._convertWorker.metainfo_create_csv(dir2,
                                                    self._files_to_convert)
            return
Exemple #9
0
 def setUp(self):
     # Init.init()
     self._dataLoader = LoadData()
Exemple #10
0
    def __init__(self, args=sys.argv):
        QtGui.QMainWindow.__init__(self)

        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        self._win_title_default = str(self.windowTitle())

        self._log = logging.getLogger(ConfigStatic.logger_name)
        self._log.info('passed')
        self._log.info('profiling: {0}'.format(DEBUG_PROFILE))

        self._dataLoader = LoadData()

        self._config_ini = ConfigIni()
        self._convertFilesForm = ConvertFileForm()
        self._downloadDbForm = DownloadDbForm()
        self._attSelectForm = DataBrowserSelectAttrForm(self._config_ini)
        self._sent_ann_form = SentAnnotationsForm(self._config_ini)
        self._export_to_pdf_form = ExportToPdfForm()
        # self._ann_show_hide_form = AnnShowHide(self._config_ini)

        # dock widgets
        self._clinInfoWidget = ClinInfo()
        self._clinInfoWidget.clear_all()
        self.ui.dockClinInfo.setWidget(self._clinInfoWidget)

        self._dataBrowserWidget = DataBrowserForm()
        self.ui.dockDataBrowser.setWidget(self._dataBrowserWidget)
        self._dataBrowserWidget.plotFileSignal.connect(self.plot_file)

        valr = self._config_ini.get_var(EnumIniVar.annotationToolbarAlignR)
        self.ui.actionAnnToolbarAlign_right.setChecked(valr)
        self._toolbar_align()

        # self.ui.actionSent_annotations.setEnabled(False)

        self._create_connections()

        self._signal_data = dict()

        self.msgbox_unsaved = QtGui.QMessageBox()
        self.msgbox_unsaved.setText(
            "There are unsaved changes in annotations.")
        self.msgbox_unsaved.setInformativeText("Save now?")
        self.msgbox_unsaved.setStandardButtons(QtGui.QMessageBox.Save
                                               | QtGui.QMessageBox.Discard)
        self.msgbox_unsaved.setDefaultButton(QtGui.QMessageBox.Save)
        self.msgbox_unsaved.setIcon(QtGui.QMessageBox.Critical)

        self.msgbox_delete = QtGui.QMessageBox()
        self.msgbox_delete.setText("Are you sure to delele all annotations?")
        self.msgbox_delete.setInformativeText(
            "All annotations will be lost. This can't be undone.")
        self.msgbox_delete.setStandardButtons(QtGui.QMessageBox.Yes
                                              | QtGui.QMessageBox.No)
        self.msgbox_delete.setDefaultButton(QtGui.QMessageBox.Save)
        self.msgbox_delete.setIcon(QtGui.QMessageBox.Warning)

        self.msgbox_err = QtGui.QMessageBox()
        self.msgbox_err.setStandardButtons(QtGui.QMessageBox.Ok)
        self.msgbox_err.setDefaultButton(QtGui.QMessageBox.Ok)
        self.msgbox_err.setIcon(QtGui.QMessageBox.Critical)

        if self._export_to_pdf_form.reportlab_imported:
            self.ui.actionExport_to_PDF.setEnabled(True)
        else:
            self.ui.actionExport_to_PDF.setEnabled(False)
            self.ui.actionExport_to_PDF.setToolTip('Export to PDF disabled')

        parsed_args = self._process_cmd_args(args)

        # paper format
        self._set_paper_format(self._config_ini.get_var(
            EnumIniVar.paperformat))

        # self._save_to_pdf2()
        valg = QtCore.QByteArray(
            self._config_ini.get_var(EnumIniVar.windowGeometry))
        vals = QtCore.QByteArray(
            self._config_ini.get_var(EnumIniVar.windowState))
        self.restoreGeometry(valg)
        self.restoreState(vals)

        if len(vals) == 0:
            self.ui.dockDataBrowser.setVisible(False)
            self.ui.dockClinInfo.setVisible(False)
            self.ui.toolBar.setVisible(False)

        self._dock_clin_info_visibility()
        self._dock_databrowse_visibility()
        self._toolbar_ann_visibility()

        self.ui.actionCaliper.setChecked(
            self._config_ini.get_var(EnumIniVar.caliperVisible))
        self.ui.actionCaliperFHR.setChecked(
            self._config_ini.get_var(EnumIniVar.caliperFHR))
        self.ui.actionCaliperTOCO.setChecked(
            self._config_ini.get_var(EnumIniVar.caliperTOCO))
        self._caliper_set()

        self._debug_data = dict()

        if DEBUG_TOOLS is True:
            self.menuDebug = QtGui.QMenu(self.ui.menubar)
            self.menuDebug.setTitle('Debug')
            self.menuDebug.addAction(self.ui.actionDebug_CalibSignal)
            self.ui.menubar.addAction(self.menuDebug.menuAction())

        # this is a tweak for batch export
        # this is really a hack and should be solver systematically
        if parsed_args.export_to_pdf is not None:
            # batch processing
            self.plot_file(parsed_args.export_to_pdf)
            self.show()
            self.ui.PlotWidget.updatePlots()
            self._export_to_pdf(silent=True)
            sys.exit(0)
Exemple #11
0
class Main(QtGui.QMainWindow):
    def __init__(self, args=sys.argv):
        QtGui.QMainWindow.__init__(self)

        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        self._win_title_default = str(self.windowTitle())

        self._log = logging.getLogger(ConfigStatic.logger_name)
        self._log.info('passed')
        self._log.info('profiling: {0}'.format(DEBUG_PROFILE))

        self._dataLoader = LoadData()

        self._config_ini = ConfigIni()
        self._convertFilesForm = ConvertFileForm()
        self._downloadDbForm = DownloadDbForm()
        self._attSelectForm = DataBrowserSelectAttrForm(self._config_ini)
        self._sent_ann_form = SentAnnotationsForm(self._config_ini)
        self._export_to_pdf_form = ExportToPdfForm()
        # self._ann_show_hide_form = AnnShowHide(self._config_ini)

        # dock widgets
        self._clinInfoWidget = ClinInfo()
        self._clinInfoWidget.clear_all()
        self.ui.dockClinInfo.setWidget(self._clinInfoWidget)

        self._dataBrowserWidget = DataBrowserForm()
        self.ui.dockDataBrowser.setWidget(self._dataBrowserWidget)
        self._dataBrowserWidget.plotFileSignal.connect(self.plot_file)

        valr = self._config_ini.get_var(EnumIniVar.annotationToolbarAlignR)
        self.ui.actionAnnToolbarAlign_right.setChecked(valr)
        self._toolbar_align()

        # self.ui.actionSent_annotations.setEnabled(False)

        self._create_connections()

        self._signal_data = dict()

        self.msgbox_unsaved = QtGui.QMessageBox()
        self.msgbox_unsaved.setText(
            "There are unsaved changes in annotations.")
        self.msgbox_unsaved.setInformativeText("Save now?")
        self.msgbox_unsaved.setStandardButtons(QtGui.QMessageBox.Save
                                               | QtGui.QMessageBox.Discard)
        self.msgbox_unsaved.setDefaultButton(QtGui.QMessageBox.Save)
        self.msgbox_unsaved.setIcon(QtGui.QMessageBox.Critical)

        self.msgbox_delete = QtGui.QMessageBox()
        self.msgbox_delete.setText("Are you sure to delele all annotations?")
        self.msgbox_delete.setInformativeText(
            "All annotations will be lost. This can't be undone.")
        self.msgbox_delete.setStandardButtons(QtGui.QMessageBox.Yes
                                              | QtGui.QMessageBox.No)
        self.msgbox_delete.setDefaultButton(QtGui.QMessageBox.Save)
        self.msgbox_delete.setIcon(QtGui.QMessageBox.Warning)

        self.msgbox_err = QtGui.QMessageBox()
        self.msgbox_err.setStandardButtons(QtGui.QMessageBox.Ok)
        self.msgbox_err.setDefaultButton(QtGui.QMessageBox.Ok)
        self.msgbox_err.setIcon(QtGui.QMessageBox.Critical)

        if self._export_to_pdf_form.reportlab_imported:
            self.ui.actionExport_to_PDF.setEnabled(True)
        else:
            self.ui.actionExport_to_PDF.setEnabled(False)
            self.ui.actionExport_to_PDF.setToolTip('Export to PDF disabled')

        parsed_args = self._process_cmd_args(args)

        # paper format
        self._set_paper_format(self._config_ini.get_var(
            EnumIniVar.paperformat))

        # self._save_to_pdf2()
        valg = QtCore.QByteArray(
            self._config_ini.get_var(EnumIniVar.windowGeometry))
        vals = QtCore.QByteArray(
            self._config_ini.get_var(EnumIniVar.windowState))
        self.restoreGeometry(valg)
        self.restoreState(vals)

        if len(vals) == 0:
            self.ui.dockDataBrowser.setVisible(False)
            self.ui.dockClinInfo.setVisible(False)
            self.ui.toolBar.setVisible(False)

        self._dock_clin_info_visibility()
        self._dock_databrowse_visibility()
        self._toolbar_ann_visibility()

        self.ui.actionCaliper.setChecked(
            self._config_ini.get_var(EnumIniVar.caliperVisible))
        self.ui.actionCaliperFHR.setChecked(
            self._config_ini.get_var(EnumIniVar.caliperFHR))
        self.ui.actionCaliperTOCO.setChecked(
            self._config_ini.get_var(EnumIniVar.caliperTOCO))
        self._caliper_set()

        self._debug_data = dict()

        if DEBUG_TOOLS is True:
            self.menuDebug = QtGui.QMenu(self.ui.menubar)
            self.menuDebug.setTitle('Debug')
            self.menuDebug.addAction(self.ui.actionDebug_CalibSignal)
            self.ui.menubar.addAction(self.menuDebug.menuAction())

        # this is a tweak for batch export
        # this is really a hack and should be solver systematically
        if parsed_args.export_to_pdf is not None:
            # batch processing
            self.plot_file(parsed_args.export_to_pdf)
            self.show()
            self.ui.PlotWidget.updatePlots()
            self._export_to_pdf(silent=True)
            sys.exit(0)

    def _create_connections(self):
        """ Creates connections in MainWindow """

        # TODO rewrite the connection to a modern style
        # Menu: File
        self.connect(self.ui.actionQuit, QtCore.SIGNAL('triggered()'),
                     QtCore.SLOT('close()'))
        self.connect(self.ui.actionOpen, QtCore.SIGNAL('triggered()'),
                     self._open_file)
        self.connect(self.ui.actionOpen_folder, QtCore.SIGNAL('triggered()'),
                     self._open_folder_dialog)

        # Menu: TOOLS
        self.connect(self.ui.actionConvert_files, QtCore.SIGNAL('triggered()'),
                     self._show_convert_files)
        self.connect(self.ui.actionDownload_CTU_UHB_data,
                     QtCore.SIGNAL('triggered()'), self._show_download_db)
        self.connect(self.ui.actionAttribute_selection,
                     QtCore.SIGNAL('triggered()'),
                     self._show_attribute_selection)
        self.connect(self.ui.actionExport_to_PDF, QtCore.SIGNAL('triggered()'),
                     self._export_to_pdf)
        # self.connect(self.ui.actionSet_Clear_Baseline, QtCore.SIGNAL('triggered()'), self._show_set_clear_bsln)
        self.connect(self.ui.actionSent_annotations,
                     QtCore.SIGNAL('triggered()'), self._show_sent_annotations)

        group_paper = QtGui.QActionGroup(self)
        self.ui.actionEU.setActionGroup(group_paper)
        self.ui.actionUS.setActionGroup(group_paper)
        self.connect(self.ui.actionEU, QtCore.SIGNAL('triggered()'),
                     self._set_paper)
        self.connect(self.ui.actionUS, QtCore.SIGNAL('triggered()'),
                     self._set_paper)
        ''' create dictionary in the following format: action: name '''
        e = EnumAnnType
        pui = self.ui
        dactions = {
            pui.actionCursor: e.select,
            pui.actionBasal: e.basal,
            pui.actionBaseline: e.baseline,
            pui.actionRecovery: e.recovery,
            pui.actionNo_recovery: e.no_recovery,
            pui.actionExcessive_UA: e.excessive_ua,
            pui.actionEllipse: e.ellipse,
            pui.actionEllipseNote: e.ellipsenote,
            pui.actionNote: e.note,
            pui.actionEvaluationNote: e.evaluation_note
        }

        if DEBUG_FIGO_ANN:
            dactions[pui.actionFloating_Baseline] = e.floating_baseline
            dactions[pui.actionAcceleration] = e.acceleration
            dactions[pui.actionDeceleration] = e.deceleration
            dactions[pui.actionUA] = e.uterine_contraction

        group_ann = QtGui.QActionGroup(self)
        for action in dactions.iterkeys():
            if isinstance(action, QtGui.QAction):  # just to check the type
                action.setActionGroup(group_ann)

        # self.ui.actionBasal.triggered.connect(self._debug_slot)
        # self.ui.actionBaseline.triggered.connect(self._set_annotation_action)

        signal_mapper = QSignalMapper(self)

        # signal_mapper.setMapping(self.ui.actionBaseline, 'baseline')
        # self.connect(self.ui.actionBaseline, QtCore.SIGNAL('triggered()'), signal_mapper.map)
        # self.connect(signal_mapper, QtCore.SIGNAL('mapped()'), self._set_annotation_action)

        # for d in dactions.iteritems():
        for action, name in dactions.iteritems():
            signal_mapper.setMapping(action, name)

            if isinstance(action, QtGui.QAction):  # just to check the type
                action.triggered.connect(signal_mapper.map)
                # self.connect(action, QtCore.SIGNAL('triggered()'), signal_mapper.map)

        signal_mapper.mapped[QtCore.QString].connect(self._ann_set_action)
        # self.connect(self.ui.actionUS, QtCore.SIGNAL('triggered()'), self._set_paper)
        # self.connect(signal_mapper, QtCore.SIGNAL('mapped()'), self._set_annotation_action)

        # signal_mapper = QtCore.QSignalMapper(self)
        #
        # # for d in dactions.iteritems():
        # for action, name in dactions.iteritems():
        #     signal_mapper.setMapping(action, name)
        #
        #     if isinstance(action, QtGui.QAction):  # just to check the type
        #         # action.triggered.connect(signal_mapper.map)
        #         self.connect(action, QtCore.SIGNAL('triggered()'), signal_mapper.map)
        #
        # # signal_mapper.mapped[QtCore.QString].connect(self._set_annotation_action)
        # # self.connect(self.ui.actionUS, QtCore.SIGNAL('triggered()'), self._set_paper)
        # self.connect(signal_mapper, QtCore.SIGNAL('mapped()'), self._set_annotation_action)

        # self.ui.actionAnnShowHide.triggered.connect(self._show_ann_show_hide)
        self.connect(self.ui.actionSave, QtCore.SIGNAL('triggered()'),
                     self._toolbar_ann_save)
        self.connect(self.ui.actionDelete, QtCore.SIGNAL('triggered()'),
                     self._toolbar_ann_delete)

        self.ui.actionCaliper.triggered.connect(self._caliper_set)
        self.ui.actionCaliperFHR.triggered.connect(self._caliper_set)
        self.ui.actionCaliperTOCO.triggered.connect(self._caliper_set)

        self.ui.actionFIGO_acc_dec.triggered.connect(
            self._caliper_set_figo_acc_dec)
        self.ui.actionFIGO_UA.triggered.connect(self._caliper_set_figo_ua)
        self.ui.actionCaliperReset.triggered.connect(self._caliper_reset)

        # Menu: View
        self.connect(self.ui.actionClinical_information,
                     QtCore.SIGNAL('triggered()'), self._dock_clin_info_toggle)
        self.connect(self.ui.dockClinInfo,
                     QtCore.SIGNAL("visibilityChanged(bool)"),
                     self._dock_clin_info_visibility)
        self.connect(self.ui.actionData_browser, QtCore.SIGNAL('triggered()'),
                     self._dock_databrowse_toggle)
        self.connect(self.ui.dockDataBrowser,
                     QtCore.SIGNAL("visibilityChanged(bool)"),
                     self._dock_databrowse_visibility)
        self.connect(self.ui.actionAnnToolbarVisible,
                     QtCore.SIGNAL('triggered()'), self._toolbar_ann_toggle)
        self.connect(self.ui.toolBar, QtCore.SIGNAL('visibilityChanged(bool)'),
                     self._toolbar_ann_visibility)
        self.connect(self.ui.actionAnnToolbarAlign_right,
                     QtCore.SIGNAL('triggered()'), self._toolbar_align)

        # Menu: Help
        self.connect(self.ui.actionAbout, QtCore.SIGNAL('triggered()'),
                     self._show_about)

        # SIGNALS
        self.ui.PlotWidget.fhrPlot.signal_ann_changed.connect(
            self._toolbar_ann_changed)
        self.ui.PlotWidget.tocoPlot.signal_ann_changed.connect(
            self._toolbar_ann_changed)
        self._attSelectForm.signal_sel_att_changed.connect(
            self._update_data_browser)

        if DEBUG_TOOLS is True:
            self.ui.actionDebug_CalibSignal.triggered.connect(
                self.plot_calibration_signal)

            if self._dataBrowserWidget.bDebugStageIICorrect:
                self._dataBrowserWidget.debug_stageI_signal.connect(
                    self.debug_plot_stage1)

    def _dock_clin_info_visibility(self):
        """ Clinical information dock visibility has changed, set the property of checkbox"""
        self.ui.actionClinical_information.setChecked(
            self.ui.dockClinInfo.isVisible())

    def _dock_clin_info_toggle(self):
        """ Set clinical information dock widget visible or invisible  """
        if self.ui.actionClinical_information.isChecked():
            self.ui.dockClinInfo.show()
        else:
            self.ui.dockClinInfo.hide()

    def _dock_databrowse_visibility(self):
        """ Visibility of data browser has changed, set the property of checkbox. """
        self.ui.actionData_browser.setChecked(
            self.ui.dockDataBrowser.isVisible())

    def _dock_databrowse_toggle(self):
        """ Set docks widget visible or invisible  """
        if self.ui.actionData_browser.isChecked():
            self.ui.dockDataBrowser.show()
        else:
            self.ui.dockDataBrowser.hide()

    def _toolbar_ann_toggle(self):
        """ Set toolbar visible  or invisible  """
        b = True if self.ui.actionAnnToolbarVisible.isChecked() else False
        self.ui.toolBar.setVisible(b)
        self.ui.PlotWidget.ann_show(b)

    def _toolbar_ann_visibility(self):
        self.ui.actionAnnToolbarVisible.setChecked(self.ui.toolBar.isVisible())

    def _toolbar_ann_changed(self):
        """ Annotations were changed. Enable saving button. """
        self.ui.actionSave.setEnabled(True)
        # self._dataBrowserWidget.update_model()

    def _toolbar_ann_save(self):
        self.ui.PlotWidget.ann_save()
        self.ui.actionSave.setEnabled(False)

        # update data browser if annotations are selected
        if ClinInfoForm.annotation_name in self._attSelectForm.get_selected_att(
        ):
            self._dataBrowserWidget.update_model_without_sort(
                self._attSelectForm.get_selected_att())

    def _toolbar_restore_geometry(self):
        self.restoreGeometry(self._valg)

    def _toolbar_ann_delete(self):

        # print self.ui.toolBar.saveGeometry()
        ret = self.msgbox_delete.exec_()

        if ret == QtGui.QMessageBox.Yes:
            self.ui.PlotWidget.delete_annotations()

    def _toolbar_align(self):

        self.ui.toolBar.clear()

        if self.ui.actionAnnToolbarAlign_right.isChecked():
            spacer = QtGui.QWidget()
            spacer.setSizePolicy(QtGui.QSizePolicy.Expanding,
                                 QtGui.QSizePolicy.Expanding)
            self.ui.toolBar.addWidget(spacer)

        self.ui.toolBar.addAction(self.ui.actionCursor)
        self.ui.toolBar.addAction(self.ui.actionBasal)
        self.ui.toolBar.addAction(self.ui.actionBaseline)
        self.ui.toolBar.addAction(self.ui.actionRecovery)
        self.ui.toolBar.addAction(self.ui.actionNo_recovery)
        self.ui.toolBar.addAction(self.ui.actionExcessive_UA)
        # self.ui.toolBar.addWidget(toolb)
        # self.ui.toolBar.addAction(self.ui.actionEllipse)
        self.ui.toolBar.addAction(self.ui.actionEllipseNote)
        self.ui.toolBar.addAction(self.ui.actionNote)
        # self.ui.toolBar.addAction(self.ui.actionEvaluationNote)
        self.ui.toolBar.addSeparator()
        self.ui.toolBar.addAction(self.ui.actionCaliper)
        # self.ui.toolBar.addAction(self.ui.actionAnnShowHide)
        self.ui.toolBar.addAction(self.ui.actionSave)
        self.ui.toolBar.addAction(self.ui.actionDelete)

        qmenu = QtGui.QMenu()
        qmenu.addAction(self.ui.actionCaliperFHR)
        qmenu.addAction(self.ui.actionCaliperTOCO)
        qmenu.addAction(self.ui.actionFIGO_acc_dec)
        qmenu.addAction(self.ui.actionFIGO_UA)
        qmenu.addAction(self.ui.actionCaliperReset)
        self.ui.actionCaliper.setMenu(qmenu)

        if DEBUG_FIGO_ANN:
            self.ui.toolBar.addSeparator()
            self.ui.toolBar.addAction(self.ui.actionFloating_Baseline)
            self.ui.toolBar.addAction(self.ui.actionAcceleration)
            self.ui.toolBar.addAction(self.ui.actionDeceleration)
            self.ui.toolBar.addAction(self.ui.actionUA)
            # self.ui.toolBar.addAction(self.ui.actionAnnShowHide)

    def _ann_check_unsaved_changes(self):

        if self.ui.actionSave.isEnabled():
            ret = self.msgbox_unsaved.exec_()

            if ret == QtGui.QMessageBox.Save:
                self._toolbar_ann_save()
            else:
                self.ui.PlotWidget.delete_annotations()
                self.ui.actionSave.setEnabled(False)

    @pyqtSlot(QtCore.QString)
    def _ann_set_action(self, a=EnumAnnType.select):
        """
        Set what annotation action will be performed.
        The possible actions are defined in :py:class:`EnumAnnType`
        """
        self.ui.PlotWidget.set_ann_action(action=a)

    def _open_file(self):
        """ Open file in gui. Files are filtered based on extension """

        self._ann_check_unsaved_changes()

        val = self._config_ini.get_var(EnumIniVar.lastUsedDirFiles)
        file1 = QtGui.QFileDialog.getOpenFileName(
            self, 'Open File', val, 'Files (*.dat *.csv *.mat *.txt)')

        if file1 == "":
            self._log.debug("File name is empty - no file selected")
            return -1

        file1 = str(file1)
        dummy, ext = Common.get_filename_and_ext(file1)

        if ext == '.dat' or '.mat':
            self.plot_file(file1)
        elif ext == '.csv':
            self.plot_csv_file(file1)
        elif ext == '.txt':
            self.plot_bditxt_file(file1)

        self._config_ini.set_var(EnumIniVar.lastUsedDirFiles,
                                 os.path.dirname(file1))
        return 0

    def _open_folder_data_browser(self, sdir):
        """ Open folder in gui.  """

        self._ann_check_unsaved_changes()
        self.ui.dockDataBrowser.show()
        self.ui.actionData_browser.setChecked(True)

        if not os.path.exists(sdir):
            self._log.error('Chosen directory do not exists: {0}'.format(sdir))

        self._dataBrowserWidget.set_attributes_dir_and_load(
            self._attSelectForm.get_selected_att(), sdir)
        self._config_ini.set_var(EnumIniVar.lastUsedDirFiles, sdir)
        return 0

    def _open_folder_dialog(self):

        sdir = QtGui.QFileDialog.getExistingDirectory(
            self, 'Open Folder',
            self._config_ini.get_var(EnumIniVar.lastUsedDirFiles))
        if sdir == "":
            self._log.debug(
                "Data browser open directory: Name is empty - none selected")
            return -1

        sdir = str(sdir)
        self._open_folder_data_browser(sdir)

    def _get_paper_format(self):
        """
        :return: paper format
        :rtype: str
        """
        if self.ui.actionEU.isChecked():
            return EnumPaperFormat.EU
        else:
            return EnumPaperFormat.US

    def plot_file(self, sfile):
        """
        Plot a CTG file. Also plot the annotations if available.

        :param sfile: input file
        :type sfile: str
        """
        sfile = str(sfile)

        self._ann_check_unsaved_changes()
        self.ui.actionCaliper.setChecked(False)
        self._caliper_set()

        try:
            adata = self._dataLoader.read_data(sfile)
        except Exception as ex:
            self._log.error(ex)
            return

        # self._annotator.set_and_load_ann_file(sfile)

        afhr = adata[EnumVarName.fhr]
        auc = adata[EnumVarName.uc]
        atimestamp = adata[EnumVarName.timestamp]
        fs = adata[EnumVarName.fs]
        nr_samples = len(afhr)

        # TODO - samples2time and used function qtime.toString is a bottle neck!
        time_string = Common.samples2time(nr_samples, fs)
        self.ui.PlotWidget.setSamplingFreqAllPlots(fs)
        self.ui.PlotWidget.plot(atimestamp, afhr, auc, time_string)
        # self.ui.PlotWidget.plot(atimestamp, afhr, auc)

        try:
            self.ui.PlotWidget.ann_file_load_and_plot(sfile)
        except IOError as ex:
            self.msgbox_err.setText(
                'A problem occurred when loading annotations for file: {0}'.
                format(sfile))
            self.msgbox_err.setInformativeText(ex.message)
            self.msgbox_err.exec_()

        self._signal_data = adata
        self.ui.actionExport_to_PDF.setEnabled(True)

        val = adata.get('Pos_IIst')
        # print(val)
        # print(adata.get('ind_stageII'))
        if val != -1 and val is not None:
            self.ui.PlotWidget.plot_stage2_line(val)
            self.ui.PlotWidget.updatePlots()

        # val = adata.get('obsolete_ind_stageII', None)
        # val = int(val) if val is not None else val
        # if val != -1 and val is not None:
        #     self.ui.PlotWidget.plot_stage1_line(val)
        #     self.ui.PlotWidget.updatePlots()

        self._debug_data = adata

        # val = adata.get('Pos_Ist', None)
        # if val != -1 and val is not None:
        #     self.ui.PlotWidget.plot_stage1_line(val)
        #     self.ui.PlotWidget.updatePlots()

        val = adata.get('Pos_Birth')
        # print val
        if val != -1 and val is not None:
            self.ui.PlotWidget.plot_birth_line(val)
            self.ui.PlotWidget.updatePlots()

        if 'name' in adata:
            self._set_window_title_rec(adata['name'])

        # clinical information
        self._clinInfoWidget.set_all(adata)

    def plot_csv_file(self, sfile):

        try:
            adata, dummy = self._dataLoader.read_csv_file(sfile)
        except Exception as ex:
            self._log.error(ex)
            return

        afhr = adata[EnumVarName.fhr]
        uc = adata[EnumVarName.uc]
        atimestamp = adata[EnumVarName.timestamp]

        self.ui.PlotWidget.plot(atimestamp, afhr, uc)

    def plot_bditxt_file(self, sfile):

        try:
            adata, dummy = self._dataLoader.read_bdi_txtfile(sfile)
        except Exception as ex:
            self._log.error(ex)
            return

        afhr = adata[EnumVarName.fhr]
        uc = adata[EnumVarName.uc]
        atimestamp = adata[EnumVarName.timestamp]

        self.ui.PlotWidget.plot(atimestamp, afhr, uc)

    def plot_calibration_signal(self):

        if self.ui.actionEU.isChecked():
            paper_format = 'EU'
        else:
            paper_format = 'US'

        fs = self.ui.PlotWidget.fhrPlot.get_sampling_freq()
        fhr, uc, timestamp = Common.generate_calib_signal(fs, paper_format)
        self.ui.PlotWidget.plot(timestamp, fhr, uc)

    def debug_plot_stage1(self, pos_stage1):

        d = self._debug_data
        if len(d) == 0:
            return

        stage1_min = pos_stage1
        fs = d['fs']
        n = len(d['fhr'])

        pos_stage1 = n - round(60 * fs * (stage1_min))

        print pos_stage1
        self.ui.PlotWidget.plot_stage1_line(pos_stage1)
        self.ui.PlotWidget.updatePlots()

    def _show_about(self):
        win = AboutDialog()
        win.exec_()

    def _show_convert_files(self):
        self._convertFilesForm.show()

    def _show_attribute_selection(self):
        self._attSelectForm.show()

    def _show_download_db(self):
        self._downloadDbForm.show()

    def _show_sent_annotations(self):
        self._sent_ann_form.clear_and_show()

    # def _show_ann_show_hide(self):
    #
    #     # print EnumAnnType.__dict__
    #
    #     if DEBUG_FIGO_ANN is False:
    #         ps = self._ann_show_hide_form.ui
    #         lactions = [ps.cbFloatingBaseline, ps.cbAccel, ps.cbDecel, ps.cbUA]
    #         for l in lactions:
    #             l.setChecked(False)
    #             l.hide()
    #
    #     self._ann_show_hide_form.show()
    #     # self.

    def _set_paper_format(self, val):
        if val == EnumPaperFormat.EU:
            self.ui.actionEU.setChecked(True)
        else:
            self.ui.actionUS.setChecked(True)

        self._set_paper()

    def _set_paper(self):
        if self.ui.actionEU.isChecked():
            self.ui.PlotWidget.set_paper_eu()
        else:
            self.ui.PlotWidget.set_paper_us()

    @pyqtSlot()
    def _debug_slot(self):
        print 'SIGNAL CAUGHT'

    @pyqtSlot()
    def _caliper_set(self):
        bvisible = self.ui.actionCaliper.isChecked()
        bfhr = self.ui.actionCaliperFHR.isChecked()
        btoco = self.ui.actionCaliperTOCO.isChecked()
        self.ui.PlotWidget.fhrPlot.caliper_set_visible(bvisible and bfhr)
        self.ui.PlotWidget.tocoPlot.caliper_set_visible(bvisible and btoco)

    @pyqtSlot()
    def _caliper_set_figo_acc_dec(self):
        self.ui.actionCaliper.setChecked(True)
        self.ui.actionCaliperFHR.setChecked(True)
        self.ui.PlotWidget.fhrPlot.caliper_set_figo_acc_dec()

    @pyqtSlot()
    def _caliper_set_figo_ua(self):
        self.ui.actionCaliper.setChecked(True)
        self.ui.actionCaliperTOCO.setChecked(True)
        self.ui.PlotWidget.tocoPlot.caliper_set_figo_ua()

    @pyqtSlot()
    def _caliper_reset(self):
        self.ui.actionCaliper.setChecked(True)
        self.ui.actionCaliperFHR.setChecked(True)
        self.ui.actionCaliperTOCO.setChecked(True)
        self.ui.PlotWidget.fhrPlot.caliper_reset(False)
        self.ui.PlotWidget.tocoPlot.caliper_reset(False)
        self._caliper_set()

    def _set_window_title_rec(self, name=None):

        if name is None:
            self.setWindowTitle(self._win_title_default)
        else:
            # s = self._winTitleDefault + ' (' + name + ')'
            s = '{0} ({1})'.format(self._win_title_default, name)
            self.setWindowTitle(s)

    def _export_to_pdf(self, silent=False):

        if EnumVarName.fhr not in self._signal_data.keys():
            self._log.info(
                "Attempt to export to PDF without plotting any signal first")

            msgbox = QtGui.QMessageBox()
            msgbox.setText("There is no CTG to export!")
            msgbox.setStandardButtons(QtGui.QMessageBox.Close)
            msgbox.setIcon(QtGui.QMessageBox.Information)
            return msgbox.exec_()

        # save annotations and prepare signals
        self._ann_check_unsaved_changes()
        fhr = self._signal_data[EnumVarName.fhr]
        uc = self._signal_data[EnumVarName.uc]
        atimestamp = self._signal_data[EnumVarName.timestamp]
        fs = self._signal_data[EnumVarName.fs]

        if DEBUG_FIGO_ANN is False:
            pe = self._export_to_pdf_form.ui
            lactions = [pe.cbFloatingBaseline, pe.cbAccel, pe.cbDecel, pe.cbUA]
            for l in lactions:
                l.setChecked(False)
                l.hide()

        self._export_to_pdf_form.set_paper_format(self._get_paper_format())
        self._export_to_pdf_form.set_signals(fhr, uc, fs, atimestamp)
        self._export_to_pdf_form.set_record_name(self._signal_data.get("name"))

        # get annotations if any
        annotator = self.ui.PlotWidget.annotator
        self._export_to_pdf_form.set_annotations(
            annotator.get_annotations_copy_all())

        if not silent:
            self._export_to_pdf_form.show()
        else:
            path = os.path.join('/tmp/', self._signal_data.get('name'))
            self._export_to_pdf_form.ui.lnFileName.setText(path)
            self._export_to_pdf_form.save()
            print('Saved to: ', path)

    def _update_data_browser(self):
        self._dataBrowserWidget.update_model(
            self._attSelectForm.get_selected_att())

    def closeEvent(self, event):

        if DEBUG_MISC is False:
            self._ann_check_unsaved_changes()

        self._config_ini.set_var(
            EnumIniVar.annotationToolbarAlignR,
            self.ui.actionAnnToolbarAlign_right.isChecked())
        self._config_ini.set_var(EnumIniVar.paperformat,
                                 self._get_paper_format())
        self._config_ini.set_var(EnumIniVar.windowGeometry,
                                 self.saveGeometry())
        self._config_ini.set_var(EnumIniVar.windowState, self.saveState())
        self._config_ini.set_var(EnumIniVar.caliperVisible,
                                 self.ui.actionCaliper.isChecked())
        self._config_ini.set_var(EnumIniVar.caliperFHR,
                                 self.ui.actionCaliperFHR.isChecked())
        self._config_ini.set_var(EnumIniVar.caliperTOCO,
                                 self.ui.actionCaliperTOCO.isChecked())
        self._config_ini.write_config()

    def _process_cmd_args(self, args):

        parsed_args = parse_cmd_args()

        if parsed_args.physionet_file is not None:
            self.plot_file(parsed_args.physionet_file)
            # self.plot_physionet_file(parsed_args.physionet_file)

        if parsed_args.matlab_file is not None:
            self.plot_file(parsed_args.matlab_file)
            # self.plot_matlab_file(parsed_args.matlab_file)

        if parsed_args.folder is not None:
            self._open_folder_data_browser(parsed_args.folder)

        return parsed_args
class TestReadWrite(unittest.TestCase):
    def setUp(self):
        # Init.init()
        self._dataLoader = LoadData()

    def tearDown(self):
        pass

    def test_read_csv(self):

        file1 = os.path.join('files', '1001.csv')
        adata, dummy = self._dataLoader.read_csv_file(file1)

        timestamp = adata[Enum.timestamp]
        fhr = adata[Enum.fhr]
        uc = adata[Enum.uc]

        self.assertEqual(timestamp[0], 1)
        self.assertEqual(timestamp[1], 2)

        self.assertEqual(fhr[0], 150.5)
        self.assertEqual(fhr[3], 151.25)
        self.assertEqual(fhr[54], 147.5)

        self.assertEqual(uc[0], 7.0)
        self.assertEqual(uc[3], 7.5)
        self.assertEqual(uc[54], 10.5)

    def test_write_read_csv(self):

        t1 = np.zeros((2, 1), np.int)
        t2 = np.zeros((2, 1), np.float64)
        t3 = np.zeros((2, 1), np.float64)
        data_all = dict()

        t1[0, 0] = 1
        t1[1, 0] = 2
        t2[0, 0] = 155.5
        t2[1, 0] = 155
        t3[0, 0] = 7.8
        t3[1, 0] = 8

        data_all[Enum.timestamp] = t1
        data_all[Enum.fhr] = t2
        data_all[Enum.uc] = t3

        outfile = tempfile.TemporaryFile()
        self._dataLoader.write_csv_file(outfile, data_all)

        outfile.seek(0)
        content = outfile.read()

        # print content
        self.assertEqual('timestamp,fhr,uc\n1,155.5,7.8\n2,155.0,8.0\n',
                         content)

    def test_read_matlab(self):

        file1 = os.path.join('files', '1001.mat')
        adata = self._dataLoader.read_matlab_file(file1)

        timestamp = adata[Enum.timestamp]
        fhr = adata[Enum.fhr]
        uc = adata[Enum.uc]
        fs = adata[Enum.fs]

        self.assertEqual(timestamp[0], 1)
        self.assertEqual(timestamp[1], 2)

        self.assertEqual(fhr[0], 150.5)
        self.assertEqual(fhr[3], 151.25)
        self.assertEqual(fhr[18978], 94.75)

        self.assertEqual(uc[0], 7.0)
        self.assertEqual(uc[1], 8.5)

        self.assertEqual(fs, 4)

    def test_read_physionet_2017(self):

        file1 = os.path.join('files', 'spam_challenge_2017.mat')
        adata = self._dataLoader.read_matlab_file(file1)

        timestamp = adata[Enum.timestamp]
        fhr = adata[Enum.fhr]
        uc = adata[Enum.uc]
        fs = adata[Enum.fs]

        self.assertEqual(timestamp[0], 1)
        self.assertEqual(timestamp[1], 2)

        for expected, v in zip(fhr, [1, 2, 3, 4, 5]):
            self.assertEqual(v, expected)

        for expected, v in zip(uc, [5, 15, 12, 0, 1]):
            self.assertEqual(v, expected)

        self.assertEqual(fs, 4)
        self.assertEqual(adata['Pos_IIst'], 3)