Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gui/docs #41

Merged
merged 2 commits into from
Jun 12, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions docs/gui.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Graphical User Interface
========================

TARDIS uses the `PyQt4 framework <http://www.riverbankcomputing.com/software/pyqt/download>`_ for its cross-platform
interface.

The GUI runs through the `IPython Interpreter <http://ipython.org/install.html>`_ which should be started with the
command ``ipython-2.7 --pylab``, so that it has acess to pylab.

Creating an instance of the :class:`ModelViewer`-class requires that PyQt4 has already been initialized in
IPython with the command ``%gui qt``.

GUI Layout and Features
-----------------------


1 change: 1 addition & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ This is the documentation for the TARDIS package.
introduction
installation
running
gui
configuration
atomic
plasma
Expand Down
127 changes: 49 additions & 78 deletions tardis/gui.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import numpy as np
import matplotlib
import matplotlib.pylab as plt
from matplotlib.patches import Circle, Wedge
from matplotlib import colors
from matplotlib.patches import Circle, Wedge
from matplotlib.figure import *
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt4 import NavigationToolbar2QT as NavigationToolbar
from PyQt4 import QtGui, QtCore

class ModelViewer(QtGui.QWidget):
Expand All @@ -19,26 +20,30 @@ def __init__(self, parent=None):
except ImportError:
app.exec_()

QtGui.QWidget.__init__(self, parent)

self.setGeometry(100, 100, 965, 600)
self.setWindowTitle('Shell Viewer')
super(ModelViewer, self).__init__(parent)
self.setGeometry(20, 35, 1200, 500)
self.setWindowTitle('Shells Viewer')
self.tablemodel = MyTableModel(["t_rad", "Ws"])
self.tableview = QtGui.QTableView()
self.graph = MatplotlibWidget(self)
self.tableview.setMinimumWidth(200)
self.graph = MatplotlibWidget(self, 'model')
self.spectrum = MatplotlibWidget(self)
self.spectrum.ax.set_title('Spectrum')
self.spectrum.ax.set_xlabel('Wavelength (A)')
self.spectrum.ax.set_ylabel('Intensity')
self.layout = QtGui.QHBoxLayout()
self.sublayout = QtGui.QVBoxLayout()
self.model = None

def show_model(self, model=None):
"""
:param Radial1DModel: object with attribute t_rads, ws
:return: Affective method, opens window to display table
"""
if model:
self.change_model(model)
self.tableview.setModel(self.tablemodel)
self.layout.addWidget(self.graph)
self.layout.addWidget(self.tableview)
self.layout.addWidget(self.graph)
self.sublayout.addWidget(self.spectrum)
self.sublayout.addWidget(self.spectrum.toolbar)
self.layout.addLayout(self.sublayout)
self.setLayout(self.layout)
self.plot()
self.show()
Expand All @@ -63,40 +68,48 @@ def add_data(self, datain):

def plot(self):
self.graph.ax.clear()
# set title and axis labels
self.graph.ax.set_title('Model')
self.graph.ax.set_xlabel('Distance from Center (1e13 m)')
self.graph.ax.set_ylabel('Distance from Center (1e13 m)')
self.shells = []
t_rad_normalizer = colors.Normalize(vmin=self.model.t_rads.min(), vmax=self.model.t_rads.max())
t_rad_color_map = plt.cm.ScalarMappable(norm=t_rad_normalizer, cmap=plt.cm.jet)
t_rad_color_map.set_array(self.model.t_rads)
if self.graph.cb:
self.graph.cb.set_clim(vmin=self.model.t_rads.min(), vmax=self.model.t_rads.max())
self.graph.cb.update_normal(t_rad_color_map)
#self.graph.figure.delaxes(self.graph.figure.axes[1])
#self.graph.figure.subplots_adjust(right=0.90) #default right padding
else:
self.graph.cb = self.graph.figure.colorbar(t_rad_color_map)
self.graph.cb.set_label('T (K)')
#normalizing_factor = 0.2 * (self.model.r_outer[-1] - self.model.r_inner[0]) / self.model.r_inner[0]
normalizing_factor = 0.8e-15
print normalizing_factor
normalizing_factor = 8e-16
for i, t_rad in enumerate(self.model.t_rads):
r_inner = self.model.r_inner[i] * normalizing_factor
r_outer = self.model.r_outer[i] * normalizing_factor
self.shells.append(Shell(i, (0,0), r_inner, r_outer, facecolor=t_rad_color_map.to_rgba(t_rad),
picker=self.graph.shell_picker))
# shells[i].set_out_radius(self.model.r_outer[i] / 1e15)
# shells[i].set_in_radius(self.model.r_inner[i] / 1e15)
self.graph.ax.add_patch(self.shells[i])
self.graph.ax.set_xlim(0, 2)
self.graph.ax.set_ylim(0, 2)
#self.graph.ax.axis(xmin=0,xmax=20,ymin=0,ymax=20)
print self.model.r_outer[-1] / 1e15
#self.graph.ax.axis(xmin=0,xmax=self.model.r_outer[-1] / 1e15,ymin=0,ymax=self.model.r_outer[-1] / 1e15)
self.graph.ax.set_xlim(0, self.model.r_outer[-1] * 1e-15)
self.graph.ax.set_ylim(0, self.model.r_outer[-1] * 1e-15)
self.graph.draw()

class ShellInfo(QtGui.QDialog):

def __init__(self, index, parent=None):
super(ShellInfo, self).__init__(parent)
self.index = index
self.setGeometry(500, 150, 650, 650)
self.setWindowTitle('Shell %d Info' % (self.index + 1))
self.graph = MatplotlibWidget(self)
self.layout = QtGui.QVBoxLayout()
self.layout.addWidget(self.graph)
self.layout.addWidget(self.graph.toolbar)
self.setLayout(self.layout)
self.show()

class MyTableModel(QtCore.QAbstractTableModel):
def __init__(self, headerdata, parent=None, *args):
QtCore.QAbstractTableModel.__init__(self, parent, *args)
super(MyTableModel, self).__init__(parent, *args)
self.arraydata = []
self.headerdata = headerdata

Expand Down Expand Up @@ -134,35 +147,30 @@ def setData(self, index, value, role=QtCore.Qt.EditRole):

class MatplotlibWidget(FigureCanvas):

def __init__(self, parent):
def __init__(self, parent, fig=None):
self.parent = parent
self.figure = Figure()
self.ax = self.figure.add_subplot(111)
self.cb = None

FigureCanvas.__init__(self, self.figure)
FigureCanvas.setSizePolicy(self, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding)
FigureCanvas.updateGeometry(self)

cid = self.figure.canvas.mpl_connect('pick_event', self.onpick)
super(MatplotlibWidget, self).__init__(self.figure)
super(MatplotlibWidget, self).setSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding)
super(MatplotlibWidget, self).updateGeometry()
if fig != 'model':
self.toolbar = NavigationToolbar(self, parent)
else:
cid = self.figure.canvas.mpl_connect('pick_event', self.onpick)

def onpick(self, event):
mouseevent = event.mouseevent
shell = event.artist
print 'Picked event:', event, mouseevent.xdata, mouseevent.ydata, shell.get_out_radius()
self.parent.tableview.selectRow(shell.get_index())
print 'Picked event:', event, event.mouseevent.xdata, event.mouseevent.ydata, event.artist.r_outer
self.parent.tableview.selectRow(event.artist.index)
shell_info = ShellInfo(event.artist.index, self.parent)

def shell_picker(self, shell, mouseevent):
"""
find the points within a certain distance from the mouseclick in
data coords and attach some extra attributes, pickx and picky
which are the data points that were picked
"""
tolerance = 0.001 # distance that click is allowed from shell, based on matplotlib data coordinates
if mouseevent.xdata is None:
return False, dict()
mouse_r2 = mouseevent.xdata ** 2 + mouseevent.ydata ** 2
if (shell.r_inner - tolerance) ** 2 < mouse_r2 < (shell.r_outer + tolerance) ** 2:
if shell.r_inner ** 2 < mouse_r2 < shell.r_outer ** 2:
return True, dict()
return False, dict()

Expand All @@ -175,40 +183,3 @@ def __init__(self, index, center, r_inner, r_outer, **kwargs):
self.r_outer = r_outer
self.r_inner = r_inner
self.width = r_outer - r_inner

def get_index(self):
return self.index

def get_out_radius(self):
return self.r_outer

def get_in_radius(self):
return self.radius - self.width

def get_center(self):
return self.center

def get_width(self):
return self.width

def set_out_radius(self, r):
if r > self.get_in_radius():
self.radius = r
return True
return False

def set_in_radius(self, r):
if 0 < r < self.get_out_radius():
self.width = self.get_out_radius() - r
return True
return False

def set_center(self, center):
self.center = center
return True

def set_width(self, width):
if width > 0:
self.width = width
return True
return False