def cli_fig_hyp(hypno, grid, color, outfile, dpi): """Create hypnogram figure from hypnogram file.""" # File conversion : if hypno is not None: hypno = click.format_filename(hypno) if outfile is not None: outfile = click.format_filename(outfile) ext = os.path.splitext(outfile)[1][1:].strip().lower() if ext == '': outfile = outfile + '.png' else: outfile = hypno + '.png' # Load hypnogram hypno, sf_hyp = read_hypno(hypno) # Bad cases (e.g. EDF files from DreamBank.net) if sf_hyp < 1: mult = int(np.round(len(hypno) / sf_hyp)) hypno = oversample_hypno(hypno, mult) sf_hyp = 1 # Create figure write_fig_hyp(outfile, hypno, sf=sf_hyp, tstartsec=0, grid=grid, ascolor=color, dpi=dpi) print('Hypnogram figure saved to:', outfile)
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 _load_hypno(self, *args, filename=None): """Load a hypnogram.""" # Define default filename for the hypnogram : if not isinstance(self._file, str): hyp_file = 'hypno' else: hyp_file = os.path.basename(self._file) + '_hypno' # Get filename : if filename is None: filename = dialog_load(self, 'Load hypnogram File', hyp_file, "Text file (*.txt);;CSV file (*.csv);;" "Elan file (*.hyp);;Excel file (*.xlsx);;" "All files (*.*)") if filename: # Load the hypnogram : self._hypno, _ = read_hypno(filename, time=self._time) self._hypno = oversample_hypno(self._hypno, self._N)[::self._dsf] self._hyp.set_data(self._sf, self._hypno, self._time) # Update info table : self._fcn_info_update() # Update scoring table : self._fcn_hypno_to_score() self._fcn_score_to_hypno()
def _load_hypno(self, *args, filename=None): """Load a hypnogram.""" # Define default filename for the hypnogram : if not isinstance(self._file, str): hyp_file = 'hypno' else: hyp_file = os.path.basename(self._file) + '_hypno' # Get filename : if filename is None: filename = dialog_load( self, 'Load hypnogram File', hyp_file, "Text file (*.txt);;CSV file (*.csv);;" "Elan file (*.hyp);;Excel file (*.xlsx);;" "All files (*.*)") if filename: # Load the hypnogram : self._hypno, _ = read_hypno(filename, time=self._time) self._hypno = oversample_hypno(self._hypno, self._N)[::self._dsf] self._hyp.set_data(self._sf, self._hypno, self._time) # Update info table : self._fcn_info_update() # Update scoring table : self._fcn_hypno_to_score() self._fcn_score_to_hypno()
""" import matplotlib.pyplot as plt from visbrain.io import write_fig_hyp, read_hypno, download_file ############################################################################### # Plotting properties ############################################################################### # Define plotting properties grid = True # display the grid ascolor = True # plt as color or in black and white file = None # Name of the file to be saved example : 'myfile.png' ############################################################################### # Hypnogram data ############################################################################### # For the illustration, a hypnogram is downloaded path_to_hypno = download_file("s101_jbe.hyp", astype='example_data') data, sf = read_hypno(path_to_hypno) ############################################################################### # Plot the hypnogram ############################################################################### # Plot the hypnogram. If file is None, the window is displayed otherwise the # figure is saved write_fig_hyp(data, sf, grid=grid, ascolor=ascolor, file=file) plt.show()