def __init__(self, parent=None, width=5, height=4, dpi=100): super(QWidget, self).__init__(parent) self.setLayout(QVBoxLayout()) self.header_layout = QHBoxLayout() self.layout().addLayout(self.header_layout) self.header_layout.addWidget(QLabel("Selection group")) self.group_combobox = QComboBox(self) self.header_layout.addWidget(self.group_combobox) self.tw_checkbox = QCheckBox(self) self.tw_checkbox.setText("Tera-Wasserburg?") self.header_layout.addWidget(self.tw_checkbox) axes_button = QToolButton(self) axes_button.setText("Setup axes") axes_button.clicked.connect(self.setup_axes) self.header_layout.addWidget(axes_button) spacer = QWidget(self) spacer.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Minimum) self.header_layout.addWidget(spacer) save_button = QToolButton(self) save_button.setText("Save") save_button.clicked.connect(self.save_figure) self.header_layout.addWidget(save_button) self.plot = QLabel("Select a group above...") self.layout().addWidget(self.plot) self.group_combobox.addItems(data.selectionGroupNames()) self.group_combobox.currentTextChanged.connect(self.update_plot) self.tw_checkbox.toggled.connect(self.update_plot) self.setWindowTitle("U-Pb contour")
def calculate(): d = QDialog() d.setWindowTitle('Channel Calculator') hl = QHBoxLayout() name_le = QLineEdit(d) hl.addWidget(name_le) hl.addWidget(QLabel('=')) channel_names = [ re.sub('\W|^(?=\d)', '_', channel_name) for channel_name in data.timeSeriesNames() ] eqn_le = AutoCompleteEdit(channel_names, parent=d) hl.addWidget(eqn_le) type_cb = QComboBox(d) channel_types = { 'Input': data.Input, 'Intermediate': data.Intermediate, 'Output': data.Output } for tname, tv in channel_types.items(): type_cb.addItem(tname, tv) hl.addWidget(type_cb) bb = QDialogButtonBox(QDialogButtonBox.Cancel | QDialogButtonBox.Ok, Qt.Horizontal, d) bb.accepted.connect(d.accept) bb.rejected.connect(d.reject) hl.addWidget(bb) d.setLayout(hl) if d.exec() == QDialog.Accepted: for channel_name in data.timeSeriesNames(): exec('%s = data.timeSeries("%s").data()' % (re.sub('\W|^(?=\d)', '_', channel_name), channel_name)) new_data = eval('%s' % eqn_le.text) new_type = type_cb.currentData new_channel = data.createTimeSeries(name_le.text, new_type, None, new_data) new_channel.setProperty('Created by', 'Channel Calculator') data.dataChanged.emit()
def __init__(self, parent = None): super(QWidget, self).__init__(parent) self.setLayout(QVBoxLayout()) header_layout = QHBoxLayout() header_layout.addWidget(QLabel("Selection group:")) self.group_combobox = QComboBox() self.group_combobox.addItems(data.selectionGroupNames()) self.group_combobox.currentTextChanged.connect(self.update_dendrogram) header_layout.addWidget(self.group_combobox) self.layout().addLayout(header_layout) self.plot = QLabel("Select a group above...") self.layout().addWidget(self.plot) self.resize(600, 800) self.setWindowTitle("Dendrogram Example") self.show()
def __init__(self, parent=None, width=5, height=4, dpi=100): super(QWidget, self).__init__(parent) self.setLayout(QVBoxLayout()) self.header_layout = QHBoxLayout() self.layout().addLayout(self.header_layout) self.header_layout.addWidget(QLabel("Selection group")) self.group_combobox = QComboBox(self) self.header_layout.addWidget(self.group_combobox) self.header_layout.addWidget(QLabel("Channel")) self.channel_combobox = QComboBox(self) self.header_layout.addWidget(self.channel_combobox) axes_button = QToolButton(self) axes_button.setText("Setup axes") axes_button.clicked.connect(self.setup_axes) self.header_layout.addWidget(axes_button) spacer = QWidget(self) spacer.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Minimum) self.header_layout.addWidget(spacer) save_button = QToolButton(self) save_button.setText("Save") save_button.clicked.connect(self.save_figure) self.header_layout.addWidget(save_button) self.plot = QLabel("Select a group above...") self.layout().addWidget(self.plot) self.group_combobox.addItems(data.selectionGroupNames()) self.group_combobox.currentTextChanged.connect( lambda: self.update_plot()) self.channel_combobox.addItems(data.timeSeriesNames()) self.channel_combobox.currentTextChanged.connect( lambda: self.update_plot()) self.setWindowTitle("Density Plot")
class DensityWidget(QWidget): xrange_plot = None yrange_plot = None def __init__(self, parent=None, width=5, height=4, dpi=100): super(QWidget, self).__init__(parent) self.setLayout(QVBoxLayout()) self.header_layout = QHBoxLayout() self.layout().addLayout(self.header_layout) self.header_layout.addWidget(QLabel("Selection group")) self.group_combobox = QComboBox(self) self.header_layout.addWidget(self.group_combobox) self.header_layout.addWidget(QLabel("Channel")) self.channel_combobox = QComboBox(self) self.header_layout.addWidget(self.channel_combobox) axes_button = QToolButton(self) axes_button.setText("Setup axes") axes_button.clicked.connect(self.setup_axes) self.header_layout.addWidget(axes_button) spacer = QWidget(self) spacer.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Minimum) self.header_layout.addWidget(spacer) save_button = QToolButton(self) save_button.setText("Save") save_button.clicked.connect(self.save_figure) self.header_layout.addWidget(save_button) self.plot = QLabel("Select a group above...") self.layout().addWidget(self.plot) self.group_combobox.addItems(data.selectionGroupNames()) self.group_combobox.currentTextChanged.connect( lambda: self.update_plot()) self.channel_combobox.addItems(data.timeSeriesNames()) self.channel_combobox.currentTextChanged.connect( lambda: self.update_plot()) self.setWindowTitle("Density Plot") def update_plot(self, xrange=None, yrange=None): self.layout().removeWidget(self.plot) self.plot.deleteLater() group_name = self.group_combobox.currentText channel_name = self.channel_combobox.currentText print("density: update_plot %s %s" % (group_name, channel_name)) group = data.selectionGroup(group_name) channel = data.timeSeries(channel_name) x = np.empty(group.count) * np.nan err = np.empty(group.count) * np.nan for i, s in enumerate(group.selections()): r = data.result(s, channel) x[i] = r.value() err[i] = r.uncertaintyAs2SE() self.fig = plt.figure() self.fig.set_size_inches(8, 8) self.fig.set_dpi(120) ax = self.fig.add_subplot(111) kde = stats.gaussian_kde(x, bw_method=kde_bandwidth) ax.plot(x, np.zeros(x.shape), 'b|', ms=5) x_eval = np.linspace(0.8 * x.min(), 1.2 * x.max(), num=2000) ax.plot(x_eval, kde(x_eval), 'k-', label='kde') if xrange is None: self.xrange_plot = ax.get_xlim() else: print('xrange=%s' % (xrange)) ax.set_xlim(xrange) if yrange is None: self.yrange_plot = ax.get_ylim() else: print('yrange=%s' % (yrange)) ax.set_ylim(yrange) ax.set_xlabel(channel.name) self.fig.canvas.draw() self.plot = FigureCanvas(self.fig) self.plot.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) self.layout().addWidget(self.plot) def save_figure(self): fname = QFileDialog.getSaveFileName( self, "Save figure", QDir.homePath(), "Images (*.png, *.jpg, *.tif, *.pdf, *.svg") print("Trying to save to file %s" % (fname)) if fname: self.fig.savefig(fname) def setup_axes(self): d = QDialog(self) l = QVBoxLayout() fl = QFormLayout() d.setLayout(l) params = { 'X plot range': (self.xrange_plot, QLineEdit(), QLineEdit()), 'Y plot range': (self.yrange_plot, QLineEdit(), QLineEdit()) } for key in params: ll = QHBoxLayout() params[key][1].setText(params[key][0][0]) params[key][2].setText(params[key][0][1]) ll.addWidget(params[key][1]) ll.addWidget(params[key][2]) fl.addRow(key, ll) l.addLayout(fl) ok_button = QPushButton('Ok') cancel_button = QPushButton('Cancel') button_layout = QHBoxLayout() button_layout.addWidget(cancel_button) button_layout.addWidget(ok_button) l.addLayout(button_layout) ok_button.clicked.connect(lambda: d.accept()) cancel_button.clicked.connect(lambda: d.reject()) if (d.exec() == QDialog.Accepted): self.xrange_plot = (float(params['X plot range'][1].text), float(params['X plot range'][2].text)) self.yrange_plot = (float(params['Y plot range'][1].text), float(params['Y plot range'][2].text)) self.update_plot(xrange=self.xrange_plot, yrange=self.yrange_plot)
class DendrogramWidget(QWidget): channel_type = data.Input image_widget = None group_combobox = None def __init__(self, parent = None): super(QWidget, self).__init__(parent) self.setLayout(QVBoxLayout()) header_layout = QHBoxLayout() header_layout.addWidget(QLabel("Selection group:")) self.group_combobox = QComboBox() self.group_combobox.addItems(data.selectionGroupNames()) self.group_combobox.currentTextChanged.connect(self.update_dendrogram) header_layout.addWidget(self.group_combobox) self.layout().addLayout(header_layout) self.plot = QLabel("Select a group above...") self.layout().addWidget(self.plot) self.resize(600, 800) self.setWindowTitle("Dendrogram Example") self.show() def update_dendrogram(self): self.layout().removeWidget(self.plot) self.plot.deleteLater() group_name = self.group_combobox.currentText print("dendrogram: update_dendrogram %s"%(group_name)) plt.clf() fig = plt.gcf() fig.set_size_inches(8, 8) fig.set_dpi(120) X = np.array([]) labels = [] for channel in data.timeSeriesList(self.channel_type): if channel.name == "TotalBeam": continue labels += [channel.name] d = np.array([]) for s in data.selectionGroup(group_name).selections(): selection_data = channel.dataForSelection(s) d = np.insert(d, 0, selection_data) try: d = d/np.max(d) except: labels.pop() print('Error processing channel %s'%(channel.name)) continue if len(X) == 0: X = d else: X = np.column_stack((X, d)) try: linked = linkage(np.transpose(X), 'ward') dendrogram(linked, labels=labels) except RuntimeError as r: print(r) self.plot = QLabel('There was a problem processing the selected group.') self.layout().addWidget(self.plot) return fig.canvas.draw() self.plot = FigureCanvas(fig) self.plot.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) self.layout().addWidget(self.plot)
class UPbContourWidget(QWidget): xrange_mask = (0, 30) yrange_mask = (0, 3) zrange_mask = (5000, math.inf) xrange_plot = (0, 30) yrange_plot = (0, 3) marker_sep = 100 * 10**6 def __init__(self, parent=None, width=5, height=4, dpi=100): super(QWidget, self).__init__(parent) self.setLayout(QVBoxLayout()) self.header_layout = QHBoxLayout() self.layout().addLayout(self.header_layout) self.header_layout.addWidget(QLabel("Selection group")) self.group_combobox = QComboBox(self) self.header_layout.addWidget(self.group_combobox) self.tw_checkbox = QCheckBox(self) self.tw_checkbox.setText("Tera-Wasserburg?") self.header_layout.addWidget(self.tw_checkbox) axes_button = QToolButton(self) axes_button.setText("Setup axes") axes_button.clicked.connect(self.setup_axes) self.header_layout.addWidget(axes_button) spacer = QWidget(self) spacer.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Minimum) self.header_layout.addWidget(spacer) save_button = QToolButton(self) save_button.setText("Save") save_button.clicked.connect(self.save_figure) self.header_layout.addWidget(save_button) self.plot = QLabel("Select a group above...") self.layout().addWidget(self.plot) self.group_combobox.addItems(data.selectionGroupNames()) self.group_combobox.currentTextChanged.connect(self.update_plot) self.tw_checkbox.toggled.connect(self.update_plot) self.setWindowTitle("U-Pb contour") def update_plot(self): self.layout().removeWidget(self.plot) self.plot.deleteLater() TW = self.tw_checkbox.checked group_name = self.group_combobox.currentText print("UPb_contour: update_plot %s %s" % (group_name, TW)) channel_data = {} channel_names = () if TW: channel_names = ('Final U238/Pb206', 'Final Pb207/Pb206', 'Zr96_CPS') else: channel_names = ('Final Pb207/U235', 'Final Pb206/U238', 'Zr96_CPS') for channel_name in channel_names: channel = data.timeSeries(channel_name) channel_data[channel_name] = np.array([]) for s in data.selectionGroup(group_name).selections(): selection_data = channel.dataForSelection(s) channel_data[channel_name] = np.insert( channel_data[channel_name], 0, selection_data) masks = [self.xrange_mask, self.yrange_mask, self.zrange_mask] for i, mask in enumerate(masks): if mask is None: masks[i] = (-math.inf, math.inf) df = pd.DataFrame({ 'x': channel_data[channel_names[0]], 'y': channel_data[channel_names[1]], 'z': channel_data[channel_names[2]] }) df = df.loc[(df['x'] > masks[0][0]) & (df['x'] < masks[0][1]) & (df['y'] > masks[1][0]) & (df['y'] < masks[1][1]) & (df['z'] > masks[2][0]) & (df['z'] < masks[2][1])] # Calculate concordia and markers t = np.array(range(1, 4001 * 10**6, 1 * 10**6)) tmarkers = np.array(range(100 * 10**6, 4001 * 10**6, self.marker_sep)) Xcon, Ycon = np.array([]), np.array([]) Xmarkers, Ymarkers = np.array([]), np.array([]) if TW: (Xcon, Ycon) = (1 / (np.exp(l238U * t) - 1), (1 / U85r) * (np.exp(l235U * t) - 1) / (np.exp(l238U * t) - 1)) (Xmarkers, Ymarkers) = (1 / (np.exp(l238U * tmarkers) - 1), (1 / U85r) * (np.exp(l235U * tmarkers) - 1) / (np.exp(l238U * tmarkers) - 1)) else: (Xcon, Ycon) = (np.exp(l235U * t) - 1, np.exp(l238U * t) - 1) (Xmarkers, Ymarkers) = (np.exp(l235U * tmarkers) - 1, np.exp(l238U * tmarkers) - 1) # Do joint plot sb.set_style('ticks') self.jointgrid = sb.jointplot(df['x'], df['y'], kind="kde", height=8, space=0, n_levels=50, shade_lowest=False, xlim=self.xrange_plot, ylim=self.yrange_plot) self.jointgrid.ax_joint.plot(Xcon, Ycon, zorder=1) self.jointgrid.ax_joint.scatter(Xmarkers, Ymarkers, s=50, zorder=2) if TW: self.jointgrid.set_axis_labels( r'${^{238}\rm{U}}/{^{206}\rm{Pb}}$', r'${^{207}\rm{Pb}}/{^{206}\rm{Pb}}$', fontsize=24) else: self.jointgrid.set_axis_labels(r'${^{207}\rm{Pb}}/{^{235}\rm{U}}$', r'${^{206}\rm{Pb}}/{^{238}\rm{U}}$', fontsize=24) x_scale = 9 * 0.78 / (self.xrange_plot[1] - self.xrange_plot[0]) y_scale = 7 * 0.80 / (self.yrange_plot[1] - self.yrange_plot[0]) for i, a in enumerate(tmarkers): if (a == 0 or i < 2): continue rotation = np.degrees( np.arctan2((Ymarkers[i] - Ymarkers[i - 1]) * y_scale, (Xmarkers[i] - Xmarkers[i - 1]) * x_scale)) offset = (0, 0) self.jointgrid.ax_joint.annotate("%i" % (a / 1e6) + " Ma", xy=(Xmarkers[i], Ymarkers[i]), xytext=offset, textcoords='offset points', rotation=rotation - 90, ha='right', va='center', rotation_mode='anchor', fontsize=14) self.plot = FigureCanvas(self.jointgrid.fig) self.plot.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) self.layout().addWidget(self.plot) def save_figure(self): fname = QFileDialog.getSaveFileName( self, "Save figure", QDir.homePath(), "Images (*.png, *.jpg, *.tif, *.pdf, *.svg") print("Trying to save to file %s" % (fname)) if fname: self.jointgrid.savefig(fname) def setup_axes(self): d = QDialog(self) l = QVBoxLayout() fl = QFormLayout() d.setLayout(l) params = { 'X mask range': (self.xrange_mask, QLineEdit(), QLineEdit()), 'Y mask range': (self.yrange_mask, QLineEdit(), QLineEdit()), 'Z mask range': (self.zrange_mask, QLineEdit(), QLineEdit()), 'X plot range': (self.xrange_plot, QLineEdit(), QLineEdit()), 'Y plot range': (self.yrange_plot, QLineEdit(), QLineEdit()) } for key in params: ll = QHBoxLayout() params[key][1].setText(params[key][0][0]) params[key][2].setText(params[key][0][1]) ll.addWidget(params[key][1]) ll.addWidget(params[key][2]) fl.addRow(key, ll) l.addLayout(fl) ok_button = QPushButton('Ok') cancel_button = QPushButton('Cancel') button_layout = QHBoxLayout() button_layout.addWidget(cancel_button) button_layout.addWidget(ok_button) l.addLayout(button_layout) ok_button.clicked.connect(lambda: d.accept()) cancel_button.clicked.connect(lambda: d.reject()) if (d.exec() == QDialog.Accepted): self.xrange_mask = (float(params['X mask range'][1].text), float(params['X mask range'][2].text)) self.yrange_mask = (float(params['Y mask range'][1].text), float(params['Y mask range'][2].text)) self.zrange_mask = (float(params['Z mask range'][1].text), float(params['Z mask range'][2].text)) self.xrange_plot = (float(params['X plot range'][1].text), float(params['X plot range'][2].text)) self.yrange_plot = (float(params['Y plot range'][1].text), float(params['Y plot range'][2].text)) self.update_plot()