def _get_widgets(self, idx): prior_idx = self._get_prior_idx(self.params[idx]) ptype = self.rundata.get("PSP_byname%i_type" % prior_idx, "N") image = self.rundata.get("PSP_byname%i_image" % prior_idx, "") mean = self.rundata.get("PSP_byname%i_mean" % prior_idx, "") prec = self.rundata.get("PSP_byname%i_prec" % prior_idx, "") trans = self.rundata.get("PSP_byname%i_transform" % prior_idx, "") type_combo = QtGui.QComboBox() type_combo.addItem("Normal", "N") type_combo.addItem("Image Prior", "I") type_combo.addItem("Spatial prior, type M", "M") type_combo.addItem("Spatial prior , type P", "P") type_combo.addItem("Spatial prior, type m", "m") type_combo.addItem("Spatial prior , type p", "p") type_combo.addItem("ARD prior", "A") type_combo.setSizeAdjustPolicy(QtGui.QComboBox.AdjustToContents) type_combo.setCurrentIndex(type_combo.findData(ptype)) type_combo.currentIndexChanged.connect(self._changed) image_combo = OverlayCombo(self.ivm, static_only=True) image_combo.setSizeAdjustPolicy(QtGui.QComboBox.AdjustToContents) if image != "": image_combo.setCurrentIndex(image_combo.findText(image)) image_combo.currentIndexChanged.connect(self._changed) mean_cb = QtGui.QCheckBox() mean_cb.setChecked(ptype != "I" and mean != "") mean_cb.stateChanged.connect(self._changed) mean_edit = QtGui.QLineEdit(mean) mean_edit.editingFinished.connect(self._changed) prec_cb = QtGui.QCheckBox() prec_cb.setChecked(prec != "") prec_cb.stateChanged.connect(self._changed) prec_edit = QtGui.QLineEdit(prec) prec_edit.editingFinished.connect(self._changed) trans_combo = QtGui.QComboBox() trans_combo.addItem("Default transformation", "") trans_combo.addItem("No transformation", "I") trans_combo.addItem("Log transformation", "L") trans_combo.addItem("Soft-plus transformation", "S") trans_combo.addItem("Fractional transformation", "F") trans_combo.setSizeAdjustPolicy(QtGui.QComboBox.AdjustToContents) trans_combo.setCurrentIndex(trans_combo.findData(trans)) trans_combo.currentIndexChanged.connect(self._changed) return (type_combo, QtGui.QLabel("Image: "), image_combo, mean_cb, QtGui.QLabel("Custom mean: "), mean_edit, prec_cb, QtGui.QLabel("Custom precision: "), prec_edit, trans_combo)
class ImageOptionWidget(OptionWidget): """ OptionWidget subclass which allows image options to be chosen from the current list of overlays """ def __init__(self, opt, **kwargs): OptionWidget.__init__(self, opt, **kwargs) self.combo = OverlayCombo(self.ivm, static_only=True) self.widgets.append(self.combo) def get_value(self): return self.combo.currentText() def set_value(self, value): idx = self.combo.findText(value) self.combo.setCurrentIndex(idx) def add(self, grid, row): OptionWidget.add(self, grid, row) grid.addWidget(self.combo, row, 1)
class AslImageWidget(QtGui.QWidget, LogSource): """ QWidget which allows an ASL data set to be described This is intended to be embedded into a QpWidget which supports processing of ASL data """ # Signal emitted when the data or metadata is changed sig_changed = QtCore.Signal() def __init__(self, ivm, parent=None, **kwargs): LogSource.__init__(self) QtGui.QWidget.__init__(self, parent) self.ivm = ivm self.data = None self.default_md = kwargs.get("default_metadata", DEFAULT_METADATA) self.md = dict(self.default_md) self.aslimage = None self.valid = True vbox = QtGui.QVBoxLayout() self.setLayout(vbox) grid = QtGui.QGridLayout() grid.addWidget(QtGui.QLabel("ASL data"), 0, 0) self.data_combo = OverlayCombo(self.ivm) self.data_combo.currentIndexChanged.connect(self._data_changed) grid.addWidget(self.data_combo, 0, 1) view_classes = [ LabelType, RepeatsChoice, NumPhases, NumEncodings, BlockFormat, DataStructure, SignalView, Labelling, Readout, SliceTime, Multiband, Times, BolusDurations, VariableRepeats ] self.views = [] for idx, view_class in enumerate(view_classes): if view_class in kwargs.get("ignore_views", ()): continue view = view_class(grid, ypos=idx + 2) view.ivm = self.ivm view.sig_md_changed.connect(self._metadata_changed) self.views.append(view) #grid.addWidget(QtGui.QLabel("Data order preview"), 5, 0) # Code below is for specific multiple phases #self.phases_lbl = QtGui.QLabel("Phases (\N{DEGREE SIGN})") #grid.addWidget(self.phases_lbl, 3, 0) #self.phases_lbl.setVisible(False) #self.phases = NumberList([float(x)*360/8 for x in range(8)]) #grid.addWidget(self.phases, 3, 1) #self.phases.setVisible(False) grid.setColumnStretch(0, 0) grid.setColumnStretch(1, 1) grid.setColumnStretch(2, 0) grid.setRowStretch(len(view_classes) + 2, 1) self.grid = grid vbox.addLayout(grid) self.warn_label = WarningBox() vbox.addWidget(self.warn_label) self._update_ui() self._data_changed() def set_data_name(self, name): """ Set the name of the data item being displayed """ if name not in self.ivm.data: raise QpException("Data not found: %s" % name) else: idx = self.data_combo.findText(name) self.data_combo.setCurrentIndex(idx) def _data_changed(self): """ New data selected - load any previously defined metadata, and validate it """ self.data = self.ivm.data.get(self.data_combo.currentText(), None) for idx in range(self.grid.count()): if idx > 1: w = self.grid.itemAt(idx).widget() if w is not None: w.setEnabled(self.data is not None) if self.data is not None: self.md = self.data.metadata.get("AslData", None) if self.md is None: self.md = dict(self.default_md) if self.data.nvols % 2 == 0: # Even number of volumes - guess TC pairs # FIXME don't need block format for single TI self.md["iaf"] = "tc" self.md["ibf"] = "tis" for view in self.views: view.set_data(self.data, self.md) self._validate_metadata() self.sig_changed.emit() def _metadata_changed(self, sender): """ Called when a view has changed the metadata """ self.debug("Metadata changed %s", sender) if self.data is not None: self.debug("Current metadata: %s", self.md) self.debug("New metadata: %s", sender.md) self.md = sender.md self._validate_metadata() self._save_metadata() self.sig_changed.emit() def _save_metadata(self): """ Set the metadata on the dataset """ if self.data is not None: current_md = self.data.metadata.get("AslData", {}) self.debug("Save: Current metadata: %s", current_md) if self.md != current_md: self.debug("Different!") self.data.metadata["AslData"] = dict(self.md) self.debug("Saved: %s ", self.md) self._update_ui() def _update_ui(self, ignore=()): """ Update user interface from the current metadata """ try: for view in self.views: if view not in ignore: view.set_data(self.data, self.md) finally: self.updating_ui = False def _validate_metadata(self): """ Validate data against specified TIs, etc """ try: if self.md and self.data is not None: self.debug("Validating metadata: %s", str(self.md)) self.aslimage, _ = qpdata_to_aslimage(self.data, metadata=self.md) self.warn_label.clear() self.valid = True except ValueError as e: self.debug("Failed: %s", str(e)) self.aslimage = None self.warn_label.warn(str(e)) self.valid = False def get_options(self): """ Get batch options """ options = {} if self.data is not None: options["data"] = self.data.name options.update(self.md) return options
class ClusteringWidget(QpWidget): """ Widget for doing K-means clustering on 3D or 4D data """ def __init__(self, **kwargs): super(ClusteringWidget, self).__init__(name="KMeans Clustering", icon="clustering", desc="Generate clusters from 3D or 4D data", group="Clustering", position=2, **kwargs) self.curves = {} def init_ui(self): self.process = KMeansProcess(self.ivm) layout = QtGui.QVBoxLayout() self.setLayout(layout) title = TitleWidget(self, help="cluster") layout.addWidget(title) DESC = """ <i>Performs clustering of 3D or 4D data using the K-means algorithm. PCA reduction is used on 4D data to extract representative curves """ desc = QtGui.QLabel(DESC) desc.setWordWrap(True) layout.addWidget(desc) gbox = QtGui.QGroupBox() gbox.setTitle('Clustering options') grid = QtGui.QGridLayout() gbox.setLayout(grid) # Data to cluster grid.addWidget(QtGui.QLabel("Data"), 0, 0) self.data_combo = OverlayCombo(self.ivm) self.data_combo.currentIndexChanged.connect(self._data_changed) grid.addWidget(self.data_combo, 0, 1) grid.addWidget(QtGui.QLabel("ROI"), 1, 0) self.roi_combo = RoiCombo(self.ivm, none_option=True) grid.addWidget(self.roi_combo, 1, 1) # Number of clusters inside the ROI self.n_clusters = NumericOption("Number of clusters", grid, xpos=2, ypos=0, minval=2, maxval=20, default=4, intonly=True) self.n_clusters.spin.setToolTip("") # Number of PCA modes self.n_pca = NumericOption("Number of PCA modes", grid, xpos=2, ypos=1, minval=1, maxval=10, default=3, intonly=True) self.n_pca.spin.setToolTip("") # Output ROI name grid.addWidget(QtGui.QLabel("Output name"), 2, 0) self.output_name = QtGui.QLineEdit("clusters") grid.addWidget(self.output_name, 2, 1) layout.addWidget(gbox) # Run clustering button hbox = QtGui.QHBoxLayout() self.run_btn = QtGui.QPushButton('Run', self) self.run_btn.clicked.connect(self.run_clustering) hbox.addWidget(self.run_btn) hbox.addStretch(1) layout.addLayout(hbox) # Plot window, showing representative curves for 4D data self.show_curves_btn = QtGui.QPushButton('Show representative curves', self) self.show_curves_btn.clicked.connect(self._show_curves) layout.addWidget(self.show_curves_btn) self.plotwin = pg.GraphicsLayoutWidget() self.plotwin.setBackground(background=None) self.plot = self.plotwin.addPlot(title="Cluster representative curves") self.plot.setLabel('left', "Signal Enhancement") self.plotwin.setVisible(False) layout.addWidget(self.plotwin) # Statistics self.show_count_btn = QtGui.QPushButton('Show voxel counts', self) self.show_count_btn.clicked.connect(self._show_counts) layout.addWidget(self.show_count_btn) self.stats_gbox = QtGui.QGroupBox() self.stats_gbox.setTitle('Voxel count') self.count_table = QtGui.QStandardItemModel() self.count_view = QtGui.QTableView() self.count_view.resizeColumnsToContents() self.count_view.setModel(self.count_table) hbox = QtGui.QHBoxLayout() hbox.addWidget(self.count_view) self.stats_gbox.setLayout(hbox) self.stats_gbox.setVisible(False) layout.addWidget(self.stats_gbox) # Merge regions self.show_merge_btn = QtGui.QPushButton('Show merge options', self) self.show_merge_btn.clicked.connect(self._show_merge) layout.addWidget(self.show_merge_btn) self.merge_gbox = QtGui.QGroupBox() self.merge_gbox.setTitle('Merge regions') vbox = QtGui.QVBoxLayout() self.merge_gbox.setLayout(vbox) hbox = QtGui.QHBoxLayout() self.merge_btn = QtGui.QPushButton('Merge', self) self.merge_btn.clicked.connect(self._run_merge) hbox.addWidget(self.merge_btn) hbox.addWidget(QtGui.QLabel('Merge region ')) self.merge_region1 = QtGui.QLineEdit('1', self) hbox.addWidget(self.merge_region1) hbox.addWidget(QtGui.QLabel(' with ')) self.merge_region2 = QtGui.QLineEdit('2', self) hbox.addWidget(self.merge_region2) vbox.addLayout(hbox) hbox = QtGui.QHBoxLayout() self.auto_merge_btn = QtGui.QPushButton('AutoMerge', self) self.auto_merge_btn.clicked.connect(self._run_automerge) hbox.addWidget(self.auto_merge_btn) hbox.addStretch(1) vbox.addLayout(hbox) self.merge_gbox.setVisible(False) layout.addWidget(self.merge_gbox) layout.addStretch(1) def activate(self): self.ivl.sig_focus_changed.connect(self._focus_changed) self.ivm.sig_current_roi.connect(self._current_roi_changed) self.ivm.sig_main_data.connect(self._main_data_changed) self._data_changed() def deactivate(self): self.ivl.sig_focus_changed.disconnect(self._focus_changed) self.ivm.sig_current_roi.disconnect(self._current_roi_changed) self.ivm.sig_main_data.disconnect(self._main_data_changed) def _current_roi_changed(self, roi): if roi is not None and roi.name != self.output_name.text(): self.roi_combo.setCurrentIndex(self.roi_combo.findText(roi.name)) def _main_data_changed(self, data): if data is not None: idx = self.data_combo.findText(data.name) else: idx = 0 self.data_combo.setCurrentIndex(idx) def _data_changed(self): data = self.ivm.data.get(self.data_combo.currentText(), None) if data is not None: is4d = data.nvols > 1 self.debug("Number of vols: %i, 4d=%s", data.nvols, is4d) self.n_pca.label.setVisible(is4d) self.n_pca.spin.setVisible(is4d) self.show_curves_btn.setVisible(is4d) self.plotwin.setVisible(is4d and self.plotwin.isVisible()) self.auto_merge_btn.setEnabled(is4d) self.run_btn.setEnabled(data is not None) def _focus_changed(self): self._update_voxel_count() def _show_curves(self): if self.plotwin.isVisible(): self.plotwin.setVisible(False) self.show_curves_btn.setText("Show representative curves") else: self.plotwin.setVisible(True) self.show_curves_btn.setText("Hide representative curves") def _show_counts(self): if self.stats_gbox.isVisible(): self.stats_gbox.setVisible(False) self.show_count_btn.setText("Show voxel counts") else: self.stats_gbox.setVisible(True) self.show_count_btn.setText("Hide voxel counts") def _show_merge(self): if self.merge_gbox.isVisible(): self.merge_gbox.setVisible(False) self.show_merge_btn.setText("Show merge_options") else: self.merge_gbox.setVisible(True) self.show_merge_btn.setText("Hide merge options") def batch_options(self): options = { "data": self.data_combo.currentText(), "roi": self.roi_combo.currentText(), "n-clusters": self.n_clusters.spin.value(), "output-name": self.output_name.text(), "invert-roi": False, } if options["roi"] == "<none>": del options["roi"] if self.n_pca.label.isVisible(): # 4D PCA options options["n-pca"] = self.n_pca.spin.value() options["reduction"] = "pca" options["norm-data"] = True return "KMeans", options def run_clustering(self): """ Run kmeans clustering using normalised PCA modes """ options = self.batch_options()[1] self.process.run(options) self._update_voxel_count() if self.n_pca.label.isVisible(): self.update_plot() def _update_voxel_count(self): self.count_table.clear() self.count_table.setVerticalHeaderItem( 0, QtGui.QStandardItem("Voxel count")) roi = self.ivm.rois.get(self.output_name.text(), None) if roi is not None: col_idx = 0 for region, name in roi.regions.items(): self.count_table.setHorizontalHeaderItem( col_idx, QtGui.QStandardItem(name)) # Volume count voxel_count = np.sum(roi.raw() == region) self.count_table.setItem( 0, col_idx, QtGui.QStandardItem(str(np.around(voxel_count)))) col_idx += 1 def update_plot(self): """ Plot the cluster curves :return: """ # Clear graph self.plot.clear() self.plot.setLabel('bottom', "Volume", "") if self.plot.legend is not None: # Work around pyqtgraph legend bug - the legend is recreated multiple times! # https://stackoverflow.com/questions/42792858/pyqtgraph-delete-persisting-legend-in-pyqt4-gui self.plot.legend.scene().removeItem(self.plot.legend) data = self.ivm.data.get(self.data_combo.currentText(), None) roi = self.ivm.rois.get(self.output_name.text(), None) if roi is not None and data is not None and data.nvols > 1: # Generate mean curve for each cluster self._generate_cluster_means(roi, data) self.plot.addLegend() # Plotting using single or multiple plots for region, name in roi.regions.items(): if np.sum(self.curves[region]) == 0: continue pencol = get_pencol(roi, region) curve = self.plot.plot(pen=pencol, width=8.0, name=name) xx = np.arange(len(self.curves[region])) curve.setData(xx, self.curves[region]) def _run_merge(self): m1 = int(self.merge_region1.text()) m2 = int(self.merge_region2.text()) self._merge(m1, m2) def _run_automerge(self): # Use PCA features or true curves? # Mean features from each cluster # Distance matrix between features if len(self.curves) < 2: return curvemat = np.zeros((len(self.curves), self.ivm.main.nvols)) row_region = {} idx = 0 for region, curve in self.curves.items(): row_region[idx] = region curvemat[idx, :] = curve idx += 1 distmat = pairwise.euclidean_distances(curvemat) distmat[distmat == 0] = np.inf loc1 = np.where(distmat == distmat.min())[0] self._merge(row_region[loc1[0]], row_region[loc1[1]]) def _generate_cluster_means(self, roi, data): """ Generate the mean curves for each cluster Returns: """ self.curves = {} for region in roi.regions: roi_data = data.mask(roi, region=region, output_flat=True) mean = np.median(roi_data, axis=0) self.curves[region] = mean def _merge(self, m1, m2): roi = self.ivm.rois.get(self.output_name.text(), None) if roi is not None: roi_data = roi.raw() roi_data[roi_data == m1] = m2 # signal the change self.ivm.add(NumpyData(roi_data, grid=roi.grid, name=self.output_name.text(), roi=True), make_current=True) # replot self.update_plot() self._update_voxel_count()
class OverlayViewWidget(QtGui.QGroupBox): """ Change view options for ROI """ def __init__(self, ivl, view): QtGui.QGroupBox.__init__(self) self.ivl = ivl self.ivm = ivl.ivm self.view = view grid = QtGui.QGridLayout() grid.setVerticalSpacing(2) grid.setContentsMargins(5, 5, 5, 5) self.setLayout(grid) grid.addWidget(QtGui.QLabel("Overlay"), 0, 0) self.overlay_combo = OverlayCombo(self.ivm, none_option=True, set_first=False) grid.addWidget(self.overlay_combo, 0, 1) grid.addWidget(QtGui.QLabel("View"), 1, 0) self.ov_view_combo = QtGui.QComboBox() self.ov_view_combo.addItem("All") self.ov_view_combo.addItem("Only in ROI") self.ov_view_combo.addItem("None") grid.addWidget(self.ov_view_combo, 1, 1) grid.addWidget(QtGui.QLabel("Color map"), 2, 0) hbox = QtGui.QHBoxLayout() self.ov_cmap_combo = QtGui.QComboBox() self.ov_cmap_combo.addItem("jet") self.ov_cmap_combo.addItem("hot") self.ov_cmap_combo.addItem("gist_heat") self.ov_cmap_combo.addItem("flame") self.ov_cmap_combo.addItem("bipolar") self.ov_cmap_combo.addItem("spectrum") hbox.addWidget(self.ov_cmap_combo) self.ov_levels_btn = QtGui.QPushButton() self.ov_levels_btn.setIcon(QtGui.QIcon(get_icon("levels.png"))) self.ov_levels_btn.setFixedSize(16, 16) self.ov_levels_btn.setToolTip("Adjust colour map levels") self.ov_levels_btn.clicked.connect(self._show_ov_levels) self.ov_levels_btn.setEnabled(False) hbox.addWidget(self.ov_levels_btn) grid.addLayout(hbox, 2, 1) grid.addWidget(QtGui.QLabel("Alpha"), 3, 0) self.ov_alpha_sld = QtGui.QSlider(QtCore.Qt.Horizontal, self) self.ov_alpha_sld.setFocusPolicy(QtCore.Qt.NoFocus) self.ov_alpha_sld.setRange(0, 255) self.ov_alpha_sld.setValue(255) grid.addWidget(self.ov_alpha_sld, 3, 1) grid.setRowStretch(4, 1) self.overlay_combo.currentIndexChanged.connect(self._combo_changed) self.ov_view_combo.currentIndexChanged.connect(self._view_changed) self.ov_cmap_combo.currentIndexChanged.connect(self._cmap_changed) self.ov_alpha_sld.valueChanged.connect(self._alpha_changed) self.view.sig_view_changed.connect(self._update) def _update(self, view): widgets = [ self.ov_view_combo, self.ov_cmap_combo, self.ov_alpha_sld, self.overlay_combo ] try: for widget in widgets: widget.blockSignals(True) if not view.get("visible"): self.ov_view_combo.setCurrentIndex(2) elif view.get("roi_only"): self.ov_view_combo.setCurrentIndex(1) else: self.ov_view_combo.setCurrentIndex(0) # 'Custom' only appears as a flag to indicate the user has messed with the # LUT using the histogram widget. Otherwise is is hidden cmap = view.get("cmap") if cmap == "custom": idx = self.ov_cmap_combo.findText("custom") if idx >= 0: self.ov_cmap_combo.setCurrentIndex(idx) else: self.ov_cmap_combo.addItem("custom") idx = self.ov_cmap_combo.findText("custom") self.ov_cmap_combo.setCurrentIndex(idx) else: idx = self.ov_cmap_combo.findText("custom") if idx >= 0: self.ov_cmap_combo.removeItem(idx) idx = self.ov_cmap_combo.findText(view.get("cmap")) self.ov_cmap_combo.setCurrentIndex(idx) self.ov_alpha_sld.setValue(view.get("alpha")) self.ov_levels_btn.setEnabled(view.data is not None) if view.data is not None: idx = self.overlay_combo.findText(view.data.name) self.overlay_combo.setCurrentIndex(idx) else: self.overlay_combo.setCurrentIndex(-1) except: import traceback traceback.print_exc() finally: for widget in widgets: widget.blockSignals(False) def _combo_changed(self, idx): if idx > 0: data_name = self.overlay_combo.itemText(idx) self.ivm.set_current_data(data_name) else: self.ivl.ivm.set_current_data(None) def _cmap_changed(self, idx): cmap = self.ov_cmap_combo.itemText(idx) self.view.set("cmap", cmap) def _view_changed(self, idx): """ Viewing style (all or within ROI only) changed """ self.view.set("visible", idx in (0, 1)) self.view.set("roi_only", (idx == 1)) def _alpha_changed(self, alpha): """ Set the data transparency """ self.view.set("alpha", alpha) def _show_ov_levels(self): dlg = LevelsDialog(self, self.ivl, self.ivm, self.view) dlg.exec_()