def _save_select_detect(self, *args, filename=None): """Export selected detection.""" channel, method = self._get_current_chan_type() # Read Table row_count = self._DetectLocations.rowCount() sta_ind = [channel, '', 'Time index (s)'] end_ind = [method, '', 'Time index (s)'] duration = ['', '', 'Duration (s)'] stage = ['', '', 'Sleep stage'] for row in np.arange(row_count): sta_ind.append(str(self._DetectLocations.item(row, 0).text())) end_ind.append(str(self._DetectLocations.item(row, 1).text())) duration.append(str(self._DetectLocations.item(row, 2).text())) stage.append(str(self._DetectLocations.item(row, 3).text())) # Get file name : saveas = "locinfo" + '_' + channel + '-' + method if filename is None: filename = dialog_save(self, 'Save ' + method + ' detection', saveas, "CSV file (*.csv);;Text file" " (*.txt);;All files (*.*)") if filename: file, ext = os.path.splitext(filename) file += '_' + channel + '-' + method zp = zip(sta_ind, end_ind, duration, stage) if ext.find('csv') + 1: write_csv(file + '.csv', zp) elif ext.find('txt') + 1: write_txt(file + '.txt', zp)
def _save_select_detect(self, *args, filename=None): """Export selected detection.""" channel, method = self._get_current_chan_type() # Read Table row_count = self._DetectLocations.rowCount() sta_ind = [channel, '', 'Time index (s)'] end_ind = [method, '', 'Time index (s)'] duration = ['', '', 'Duration (s)'] stage = ['', '', 'Sleep stage'] for row in np.arange(row_count): sta_ind.append(str(self._DetectLocations.item(row, 0).text())) end_ind.append(str(self._DetectLocations.item(row, 1).text())) duration.append(str(self._DetectLocations.item(row, 2).text())) stage.append(str(self._DetectLocations.item(row, 3).text())) # Get file name : saveas = "locinfo" + '_' + channel + '-' + method if filename is None: filename = dialog_save( self, 'Save ' + method + ' detection', saveas, "CSV file (*.csv);;Text file" " (*.txt);;All files (*.*)") if filename: file, ext = os.path.splitext(filename) file += '_' + channel + '-' + method zp = zip(sta_ind, end_ind, duration, stage) if ext.find('csv') + 1: write_csv(file + '.csv', zp) elif ext.find('txt') + 1: write_txt(file + '.txt', zp)
def _save_info_table(self, *args, filename=None): """Export stat info.""" # Get file name : if filename is None: filename = dialog_save(self, 'Save file', 'statsinfo', "CSV file (*.csv);;Text file (*.txt);;" "All files (*.*)") if filename: file, ext = os.path.splitext(filename) if ext.find('csv') + 1: write_csv(file + '.csv', zip(self._keysInfo, self._valInfo)) elif ext.find('txt') + 1: write_txt(file + '.txt', zip(self._keysInfo, self._valInfo))
def _save_info_table(self, *args, filename=None): """Export stat info.""" # Get file name : if filename is None: filename = dialog_save( self, 'Save file', 'statsinfo', "CSV file (*.csv);;Text file (*.txt);;" "All files (*.*)") if filename: file, ext = os.path.splitext(filename) if ext.find('csv') + 1: write_csv(file + '.csv', zip(self._keysInfo, self._valInfo)) elif ext.find('txt') + 1: write_txt(file + '.txt', zip(self._keysInfo, self._valInfo))
def cli_sleep_stats(hypno, outfile): """Compute sleep statistics from hypnogram file and export them in csv. Sleep statistics specifications: * Time in Bed (TIB) : total duration of the hypnogram. * Total Dark Time (TDT) : duration of the hypnogram from beginning to last period of sleep. * Sleep Period Time (SPT) : duration from first to last period of sleep. * Wake After Sleep Onset (WASO) : duration of wake periods within SPT * Sleep Efficiency (SE) : TST / TDT * 100 (%). * Total Sleep Time (TST) : SPT - WASO. * W, N1, N2, N3 and REM: sleep stages duration. * % (W, ... REM) : sleep stages duration expressed in percentages of TDT. * Latencies: latencies of sleep stages from the beginning of the record. (All values except SE and percentages are expressed in minutes) """ # File conversion : if hypno is not None: hypno_path = click.format_filename(hypno) if outfile is not None: outfile = click.format_filename(outfile) # Check extension ext = os.path.splitext(outfile)[1][1:].strip().lower() if ext == '': outfile = outfile + '.csv' # Load hypnogram hypno, sf_hyp = read_hypno(hypno_path) if sf_hyp < 1: mult = int(np.round(len(hypno) / sf_hyp)) hypno = oversample_hypno(hypno, mult) sf_hyp = 1 # Get sleep stats stats = sleepstats(hypno, sf_hyp=sf_hyp) stats['File'] = hypno_path print('\nSLEEP STATS\n===========') keys, val = [''] * len(stats), [''] * len(stats) # Fill table : for num, (k, v) in enumerate(stats.items()): print(k, '\t', str(v)) # Remember variables : keys[int(num)] = k val[int(num)] = str(v) if outfile is not None: write_csv(outfile, zip(keys, val)) print('===========\nCSV file saved to:', outfile)
def _save_annotation_table(self, *args, filename=None): """Export annotation table.""" # Read Table row_count = self._AnnotateTable.rowCount() sta_ind, end_ind, annot = [], [], [] for row in np.arange(row_count): sta_ind.append(str(self._AnnotateTable.item(row, 0).text())) end_ind.append(str(self._AnnotateTable.item(row, 1).text())) annot.append(str(self._AnnotateTable.item(row, 2).text())) # Get file name : if filename is None: filename = dialog_save(self, 'Save annotations', 'annotations', "CSV file (*.csv);;Text file (*.txt);;" "All files (*.*)") if filename: file, ext = os.path.splitext(filename) if ext.find('csv') + 1: write_csv(file + '.csv', zip(sta_ind, end_ind, annot)) elif ext.find('txt') + 1: write_txt(file + '.txt', zip(sta_ind, end_ind, annot))
def _save_scoring_table(self, *args, filename=None): """Export score info.""" # Read Table row_count = self._scoreTable.rowCount() sta_ind, end_ind, stage = [], [], [] for row in np.arange(row_count): sta_ind.append(str(self._scoreTable.item(row, 0).text())) end_ind.append(str(self._scoreTable.item(row, 1).text())) stage.append(str(self._scoreTable.item(row, 2).text())) # Get file name : if filename is None: filename = dialog_save(self, 'Save file', 'scoring_info', "CSV file (*.csv);;Text file (*.txt);;" "All files (*.*)") if filename: file, ext = os.path.splitext(filename) if ext.find('csv') + 1: write_csv(file + '.csv', zip(sta_ind, end_ind, stage)) elif ext.find('txt') + 1: write_txt(file + '.txt', zip(sta_ind, end_ind, stage))
def _save_annotation_table(self, *args, filename=None): """Export annotation table.""" # Read Table row_count = self._AnnotateTable.rowCount() sta_ind, end_ind, annot = [], [], [] for row in np.arange(row_count): sta_ind.append(str(self._AnnotateTable.item(row, 0).text())) end_ind.append(str(self._AnnotateTable.item(row, 1).text())) annot.append(str(self._AnnotateTable.item(row, 2).text())) # Get file name : if filename is None: filename = dialog_save( self, 'Save annotations', 'annotations', "CSV file (*.csv);;Text file (*.txt);;" "All files (*.*)") if filename: file, ext = os.path.splitext(filename) if ext.find('csv') + 1: write_csv(file + '.csv', zip(sta_ind, end_ind, annot)) elif ext.find('txt') + 1: write_txt(file + '.txt', zip(sta_ind, end_ind, annot))
def _save_scoring_table(self, *args, filename=None): """Export score info.""" # Read Table row_count = self._scoreTable.rowCount() sta_ind, end_ind, stage = [], [], [] for row in np.arange(row_count): sta_ind.append(str(self._scoreTable.item(row, 0).text())) end_ind.append(str(self._scoreTable.item(row, 1).text())) stage.append(str(self._scoreTable.item(row, 2).text())) # Get file name : if filename is None: filename = dialog_save( self, 'Save file', 'scoring_info', "CSV file (*.csv);;Text file (*.txt);;" "All files (*.*)") if filename: file, ext = os.path.splitext(filename) if ext.find('csv') + 1: write_csv(file + '.csv', zip(sta_ind, end_ind, stage)) elif ext.find('txt') + 1: write_txt(file + '.txt', zip(sta_ind, end_ind, stage))
def _fcn_save_annotations(self, *args, filename=None): """Save annotations.""" # Read Table row_count = self._annot_table.rowCount() time, amp, signal, text = [], [], [], [] for row in range(row_count): time.append(self._annot_table.item(row, 0).text()) amp.append(self._annot_table.item(row, 1).text()) signal.append(self._annot_table.item(row, 2).text()) text.append(self._annot_table.item(row, 3).text()) # Get file name : if filename is None: filename = dialog_save(self, 'Save annotations', 'annotations', "CSV file (*.csv);;Text file (*.txt);;" "All files (*.*)") if filename: kw = {'delimiter': '_'} file, ext = os.path.splitext(filename) if ext.find('csv') + 1: write_csv(file + '.csv', zip(time, amp, signal, text), **kw) elif ext.find('txt') + 1: write_txt(file + '.txt', zip(time, amp, signal, text), **kw)
def _fcn_save_annotations(self, *args, filename=None): """Save annotations.""" # Read Table row_count = self._annot_table.rowCount() time, amp, signal, text = [], [], [], [] for row in range(row_count): time.append(self._annot_table.item(row, 0).text()) amp.append(self._annot_table.item(row, 1).text()) signal.append(self._annot_table.item(row, 2).text()) text.append(self._annot_table.item(row, 3).text()) # Get file name : if filename is None: filename = dialog_save( self, 'Save annotations', 'annotations', "CSV file (*.csv);;Text file (*.txt);;" "All files (*.*)") if filename: kw = {'delimiter': '_'} file, ext = os.path.splitext(filename) if ext.find('csv') + 1: write_csv(file + '.csv', zip(time, amp, signal, text), **kw) elif ext.find('txt') + 1: write_txt(file + '.txt', zip(time, amp, signal, text), **kw)