def __init__(self, file, path, *args, **kwargs): super(labelPlot, self).__init__(*args, **kwargs) lbl = file[path] if not utils.is_complex_event(lbl): raise TypeError("Argument must be complex event dataset") else: self.lbl = lbl #saving the file and the path allows access to non-anonymous reference to #the dataset, which is necessary for deleting labels self.file = file self.path = path self.double_clicked = np.zeros(len(self.lbl),dtype=bool) self.installEventFilter(self) self.getViewBox().enableAutoRange(enable=False) #self.getAxis('left').hide() self.key = None self.dclickedRegion = None self.activeLabel = None if 'ms' in self.lbl.attrs['units']: self.scaling_factor = 1000. elif 'samples' in self.lbl.attrs['units']: self.scaling_factor = float(self.lbl.attrs['sampling_rate']) else: self.scaling_factor = 1. self.setMouseEnabled(y=False) self.max_plotted = 100 self.getViewBox().sigXRangeChanged.connect(self.plot_all_events) self.plot_all_events()
def plot_dataset_list(self, dataset_list, data_layout, append=False): ''' plots a list of datasets to a data layout''' QApplication.setOverrideCursor(QtCore.Qt.WaitCursor) data_layout.clear() if not append: self.subplots = [] toes = [] t = time.time() unplotable = [] for dataset in dataset_list: if 'datatype' not in dataset.attrs.keys(): unplotable.append(''.join( [dataset.file.filename, dataset.name])) continue '''sampled data''' if dataset.attrs.get('datatype') < 1000: # sampled data if (self.settings_panel.oscillogram_check.checkState() == QtCore.Qt.Checked): pl = downsamplePlot(dataset, name=str(len(self.subplots))) pl.setLabel('left', dataset.name.split('/')[-1]) data_layout.addItem(pl, row=len(self.subplots), col=0) # max_default_range = 20 # xmax = min(dataset.size/float(dataset.attrs['sampling_rate']), # maxb_default_range) # xrange = pl.dataItems[0].dataBounds(0) # yrange = pl.dataItems[0].dataBounds(1) # pl.setXRange(*xrange,padding=0) # pl.setYRange(*yrange,padding=0) self.subplots.append(pl) pl.showGrid(x=True, y=True) ''' simple events ''' elif utils.is_simple_event(dataset): if dataset.attrs.get('units') == 'ms': data = dataset.value / 1000. elif dataset.attrs.get('units') == 'samples': data = dataset.value / dataset.attrs['sampling_rate'] else: data = dataset.value if (self.settings_panel.raster_check.checkState() == QtCore.Qt.Checked or self.settings_panel.psth_check.checkState() == QtCore.Qt.Checked or self.settings_panel.isi_check.checkState() == QtCore.Qt.Checked): toes.append(data) continue ''' complex event ''' elif utils.is_complex_event(dataset): if (self.settings_panel.label_check.checkState() == QtCore.Qt.Checked): pl = labelPlot(dataset.file, dataset.name, name=str(len(self.subplots))) pl.setLabel('left', dataset.name.split('/')[-1]) data_layout.addItem(pl, row=len(self.subplots), col=0) #pl.showLabel('left', show=False) pl.sigLabelSelected.connect(self.label_selected) pl.sigNoLabelSelected.connect(self.label_unselected) self.deleteLabelAction.triggered.connect( pl.delete_selected_labels) self.subplots.append(pl) else: unplotable.append(''.join( [dataset.file.filename, dataset.name])) continue '''adding spectrograms''' if dataset.attrs.get('datatype') in (0, 1, 23): # show spectrogram if (self.settings_panel.spectrogram_check.checkState() == QtCore.Qt.Checked): pl = spectrogram.fromSettingsPanel(dataset, self.settings_panel) pl.selection_made.connect(self.spectrogramSelection) data_layout.addItem(pl, row=len(self.subplots), col=0) self.subplots.append(pl) #end for loop if toes: if self.settings_panel.raster_check.checkState( ) == QtCore.Qt.Checked: pl = rasterPlot(toes) data_layout.addItem(pl, row=len(self.subplots), col=0) pl.showLabel('left', show=False) self.subplots.append(pl) if self.settings_panel.psth_check.checkState( ) == QtCore.Qt.Checked: all_toes = np.zeros(sum(len(t) for t in toes)) k = 0 for t in toes: all_toes[k:k + len(t)] = t k += len(t) if self.settings_panel.psth_bin_size.text(): bin_size = float( self.settings_panel.psth_bin_size.text()) / 1000. else: bin_size = .01 bins = np.arange(all_toes.min(), all_toes.max() + bin_size, bin_size) y, x = np.histogram(all_toes, bins=bins) psth = pg.PlotCurveItem(x, y, stepMode=True, fillLevel=0, brush=(0, 0, 255, 80)) pl = data_layout.addPlot(row=len(self.subplots), col=0) pl.addItem(psth) pl.setMouseEnabled(y=False) self.subplots.append(pl) if self.settings_panel.isi_check.checkState() == QtCore.Qt.Checked: isis = np.zeros(sum(len(t) - 1 for t in toes)) k = 0 for t in toes: isis[k:k + len(t) - 1] = np.diff(t) k += len(t) - 1 if self.settings_panel.psth_bin_size.text(): bin_size = float( self.settings_panel.psth_bin_size.text()) / 1000. else: bin_size = .01 bins = np.arange(isis.min(), isis.max() + bin_size, bin_size) y, x = np.histogram(isis, bins=bins, normed=True) isi_hist = pg.PlotCurveItem(x, y, stepMode=True, fillLevel=0, brush=(0, 0, 255, 80)) pl = data_layout.addPlot(row=len(self.subplots), col=0) pl.addItem(isi_hist) pl.setMouseEnabled(y=False) self.subplots.append(pl) '''linking x axes''' minPlotHeight = 100 masterXLink = None for pl in self.subplots: if not masterXLink: masterXLink = pl pl.setXLink(masterXLink) pl.setMinimumHeight(minPlotHeight) if any(isinstance(pl, spectrogram) for pl in self.subplots): self.exportSelectionAction.setVisible(True) else: self.exportSelectionAction.setVisible(False) spacing = 5 self.data_layout.centralWidget.layout.setSpacing(spacing) self.data_layout.setMinimumHeight( len(self.subplots) * (minPlotHeight + spacing)) QApplication.restoreOverrideCursor() if unplotable: self.error_message.showMessage( "Could not plot the following datasets: %s" % ('\n'.join(unplotable)), "plot_error")
def plot_dataset_list(self, dataset_list, data_layout, append=False): """ plots a list of datasets to a data layout""" QApplication.setOverrideCursor(QtCore.Qt.WaitCursor) data_layout.clear() if not append: self.subplots = [] toes = [] t = time.time() unplotable = [] for dataset in dataset_list: if "datatype" not in dataset.attrs.keys(): unplotable.append("".join([dataset.file.filename, dataset.name])) continue """sampled data""" if dataset.attrs.get("datatype") < 1000: # sampled data if self.settings_panel.oscillogram_check.checkState() == QtCore.Qt.Checked: pl = downsamplePlot(dataset, name=str(len(self.subplots))) pl.setLabel("left", dataset.name.split("/")[-1]) data_layout.addItem(pl, row=len(self.subplots), col=0) # max_default_range = 20 # xmax = min(dataset.size/float(dataset.attrs['sampling_rate']), # maxb_default_range) # xrange = pl.dataItems[0].dataBounds(0) # yrange = pl.dataItems[0].dataBounds(1) # pl.setXRange(*xrange,padding=0) # pl.setYRange(*yrange,padding=0) self.subplots.append(pl) pl.showGrid(x=True, y=True) """ simple events """ elif utils.is_simple_event(dataset): if dataset.attrs.get("units") == "ms": data = dataset.value / 1000.0 elif dataset.attrs.get("units") == "samples": data = dataset.value / dataset.attrs["sampling_rate"] else: data = dataset.value if ( self.settings_panel.raster_check.checkState() == QtCore.Qt.Checked or self.settings_panel.psth_check.checkState() == QtCore.Qt.Checked or self.settings_panel.isi_check.checkState() == QtCore.Qt.Checked ): toes.append(data) continue """ complex event """ elif utils.is_complex_event(dataset): if self.settings_panel.label_check.checkState() == QtCore.Qt.Checked: pl = labelPlot(dataset.file, dataset.name, name=str(len(self.subplots))) pl.setLabel("left", dataset.name.split("/")[-1]) data_layout.addItem(pl, row=len(self.subplots), col=0) # pl.showLabel('left', show=False) pl.sigLabelSelected.connect(self.label_selected) pl.sigNoLabelSelected.connect(self.label_unselected) self.deleteLabelAction.triggered.connect(pl.delete_selected_labels) self.subplots.append(pl) else: unplotable.append("".join([dataset.file.filename, dataset.name])) continue """adding spectrograms""" if dataset.attrs.get("datatype") in (0, 1, 23): # show spectrogram if self.settings_panel.spectrogram_check.checkState() == QtCore.Qt.Checked: pl = spectrogram.fromSettingsPanel(dataset, self.settings_panel) pl.selection_made.connect(self.spectrogramSelection) data_layout.addItem(pl, row=len(self.subplots), col=0) self.subplots.append(pl) # end for loop if toes: if self.settings_panel.raster_check.checkState() == QtCore.Qt.Checked: pl = rasterPlot(toes) data_layout.addItem(pl, row=len(self.subplots), col=0) pl.showLabel("left", show=False) self.subplots.append(pl) if self.settings_panel.psth_check.checkState() == QtCore.Qt.Checked: all_toes = np.zeros(sum(len(t) for t in toes)) k = 0 for t in toes: all_toes[k : k + len(t)] = t k += len(t) if self.settings_panel.psth_bin_size.text(): bin_size = float(self.settings_panel.psth_bin_size.text()) / 1000.0 else: bin_size = 0.01 bins = np.arange(all_toes.min(), all_toes.max() + bin_size, bin_size) y, x = np.histogram(all_toes, bins=bins) psth = pg.PlotCurveItem(x, y, stepMode=True, fillLevel=0, brush=(0, 0, 255, 80)) pl = data_layout.addPlot(row=len(self.subplots), col=0) pl.addItem(psth) pl.setMouseEnabled(y=False) self.subplots.append(pl) if self.settings_panel.isi_check.checkState() == QtCore.Qt.Checked: isis = np.zeros(sum(len(t) - 1 for t in toes)) k = 0 for t in toes: isis[k : k + len(t) - 1] = np.diff(t) k += len(t) - 1 if self.settings_panel.psth_bin_size.text(): bin_size = float(self.settings_panel.psth_bin_size.text()) / 1000.0 else: bin_size = 0.01 bins = np.arange(isis.min(), isis.max() + bin_size, bin_size) y, x = np.histogram(isis, bins=bins, normed=True) isi_hist = pg.PlotCurveItem(x, y, stepMode=True, fillLevel=0, brush=(0, 0, 255, 80)) pl = data_layout.addPlot(row=len(self.subplots), col=0) pl.addItem(isi_hist) pl.setMouseEnabled(y=False) self.subplots.append(pl) """linking x axes""" minPlotHeight = 100 masterXLink = None for pl in self.subplots: if not masterXLink: masterXLink = pl pl.setXLink(masterXLink) pl.setMinimumHeight(minPlotHeight) if any(isinstance(pl, spectrogram) for pl in self.subplots): self.exportSelectionAction.setVisible(True) else: self.exportSelectionAction.setVisible(False) spacing = 5 self.data_layout.centralWidget.layout.setSpacing(spacing) self.data_layout.setMinimumHeight(len(self.subplots) * (minPlotHeight + spacing)) QApplication.restoreOverrideCursor() if unplotable: self.error_message.showMessage( "Could not plot the following datasets: %s" % ("\n".join(unplotable)), "plot_error" )