Beispiel #1
0
def setup_viz(state):
    """Setup visualization."""

    import matplotlib
    import matplotlib.figure
    import matplotlib.backends.backend_tkagg
    import Tkinter
    import threading

    viz = {}

    # Initialise Tk ...
    figure = matplotlib.figure.Figure()
    figure.set_size_inches((8, 6))

    viz['axes1'] = figure.add_subplot(311)
    viz['axes2'] = figure.add_subplot(312)
    viz['axes3'] = figure.add_subplot(313)

    viz['tk'] = Tkinter.Tk()
    viz['canvas'] = matplotlib.backends.backend_tkagg.FigureCanvasTkAgg(
        figure, master=viz['tk'])
    viz['canvas'].get_tk_widget().pack(expand=True, fill=Tkinter.BOTH)

    return viz
    def plotPixels(self, xVals, images, title):
        figure = matplotlib.figure.Figure((6, 4), dpi = 100, 
                facecolor = (1, 1, 1))
        axes = figure.add_subplot(1, 1, 1)
        axes.set_axis_bgcolor('white')
        axes.set_title(title)
        axes.set_ylabel('Individual pixel value')
        axes.set_xlabel('Exposure time (ms)')

        # Plot pixels from the centers of each quadrant as well as from
        # the center of the image and the average of the image as a whole.
        lines = []
        labels = []
        for cam in range(images.shape[1]):
            camImages = images[:, cam]
            height, width = camImages[0].shape
            cy = height / 2
            cx = width / 2
            for color, xOffset, yOffset in [('r', -1, -1), ('g', -1, 1), 
                    ('b', 1, 1), ('c', 1, -1), ('m', 0, 0)]:
                y = cy + yOffset * height / 4
                x = cx + xOffset * width / 4
                lines.append(axes.plot(xVals, camImages[:, y, x], color))
                labels.append("Pixel at %d, %d for cam %d" % (x, y, cam))
            lines.append(axes.plot(xVals, map(numpy.mean, camImages), 'k'))
            labels.append("Average for cam %d" % cam)
        figure.legend(lines, labels, loc = 'upper left')
        frame = wx.Frame(None, title = "Linearity plot")
        canvas = matplotlib.backends.backend_wxagg.FigureCanvasWxAgg(
                frame, -1, figure)
        canvas.draw()
        # Big enough to be visible, but unlikely to run off the edge of 
        # the screen.
        frame.SetSize((640, 480))
        frame.Show()
def showList(xList, yList):
	#show
	fig = plt.figure()
	ax = fig.add_subplot(111, projection='3d')
	
	cateList = [[],[],[],[],[],[],[],[],[],[]]
	for i in range(len(yList)):
		y = yList[i]
		x = xList[i]
		cateList[y].append(x)
	
	color = ["red", "black", "yellow", "blue", "green", "pink", "magenta", "cyan", '#eeefff', "#ffefff"]
	
	i = 0
	for list in cateList:
	
		xList = [i[0] for i in list]
		yList = [i[1] for i in list]
		zList = [0 for i in list]

		ax.scatter(xList, yList, zList, color=color[i], marker = '.', alpha=0.8, depthshade=True)
		#plt.scatter(xList, yList, color=color[i], marker = '^', alpha=0.5)
		i+=1
	ax.set_xlabel('X Label')
	ax.set_ylabel('Y Label')
	ax.set_zlabel('Z Label')	
	plt.show()	
 def create_protocol_tab(self, channel):
     """
     Creates a widget displaying a stored D/A signal.
     """
     widget = QtWidgets.QWidget(self)
     # Create figure
     figure = matplotlib.figure.Figure()
     figure.suptitle(self._abf.filename())
     canvas = backend.FigureCanvasQTAgg(figure)
     canvas.setParent(widget)
     axes = figure.add_subplot(1, 1, 1)
     toolbar = backend.NavigationToolbar2QT(canvas, widget)
     # Draw lines
     name = 'DA(' + str(channel) + ')'  # Default if no data is present
     times = None
     for i, sweep in enumerate(self._abf.protocol()):
         if times is None:
             name = 'DA' + str(sweep[channel].number()) + ': ' \
                 + sweep[channel].name()
             times = sweep[channel].times()
         axes.plot(times, sweep[channel].values())
     # Create a layout
     vbox = QtWidgets.QVBoxLayout()
     vbox.addWidget(canvas)
     vbox.addWidget(toolbar)
     widget.setLayout(vbox)
     self._figures.append(figure)
     self._axes.append(axes)
     return widget, name
    def create_graph_tab(self, time, key):
        """
        Creates a widget displaying a graph.
        """
        widget = QtWidgets.QWidget(self)

        # Create figure
        figure = matplotlib.figure.Figure()
        figure.suptitle(self._atf.filename())
        canvas = backend.FigureCanvasQTAgg(figure)
        canvas.setParent(widget)
        axes = figure.add_subplot(1, 1, 1)
        toolbar = backend.NavigationToolbar2QT(canvas, widget)

        # Draw lines
        axes.plot(self._atf[time], self._atf[key])

        # Create a layout
        vbox = QtWidgets.QVBoxLayout()
        vbox.addWidget(canvas)
        vbox.addWidget(toolbar)
        widget.setLayout(vbox)
        self._figures.append(figure)
        self._axes.append(axes)

        # Return widget
        return widget
 def create_graph_tab(self, time, data):
     """
     Creates a widget displaying a time series.
     """
     widget = QtWidgets.QWidget(self)
     # Create figure
     figure = matplotlib.figure.Figure()
     figure.suptitle(self._filename)
     canvas = backend.FigureCanvasQTAgg(figure)
     canvas.setParent(widget)
     axes = figure.add_subplot(1, 1, 1)
     toolbar = backend.NavigationToolbar2QT(canvas, widget)
     # Draw line
     if time is None:
         axes.plot(data)
     else:
         axes.plot(time, data)
     # Create a layout
     vbox = QtWidgets.QVBoxLayout()
     vbox.addWidget(canvas)
     vbox.addWidget(toolbar)
     widget.setLayout(vbox)
     self._figures.append(figure)
     self._axes.append(axes)
     return widget
 def create_graph_tab(self, record):
     """
     Creates a widget displaying the data in record i
     """
     widget = QtWidgets.QWidget(self)
     # Create figure
     figure = matplotlib.figure.Figure()
     figure.suptitle(self._wcp.filename())
     canvas = backend.FigureCanvasQTAgg(figure)
     canvas.setParent(widget)
     axes = figure.add_subplot(1, 1, 1)
     toolbar = backend.NavigationToolbar2QT(canvas, widget)
     # Draw lines
     for i in range(self._wcp.channels()):
         axes.plot(
             np.array(self._wcp.times(), copy=True),
             np.array(self._wcp.values(record, i), copy=True),
         )
     # Create a layout
     vbox = QtWidgets.QVBoxLayout()
     vbox.addWidget(canvas)
     vbox.addWidget(toolbar)
     widget.setLayout(vbox)
     self._figures.append(figure)
     self._axes.append(axes)
     return widget
Beispiel #8
0
    def __init__(self, parent):
        figure = matplotlib.figure.Figure()
        self.axes = figure.add_subplot(111)

        FigureCanvas.__init__(self, figure)
        self.parent = parent        
        self.setParent(parent)
        
        prefix, suffix = self.parent.market_proxy.getPrefixSuffixCounter()
        precision = self.parent.market_proxy.getPrecisionCounter()
        self.axes.set_xlabel(QtCore.QCoreApplication.translate("DepthChartDialog", "Price"))
        format_str = "{}%1.{}f{}".format(prefix, precision, suffix)
        formatter = matplotlib.ticker.FormatStrFormatter(format_str)
        self.axes.xaxis.set_major_formatter(formatter)

        prefix, suffix = self.parent.market_proxy.getPrefixSuffixBase()
        precision = self.parent.market_proxy.getPrecisionBase()
        self.axes.set_ylabel(QtCore.QCoreApplication.translate("DepthChartDialog", "Volume"))
        format_str = "{}%1.{}f{}".format(prefix, precision, suffix)
        formatter = matplotlib.ticker.FormatStrFormatter(format_str)
        self.axes.yaxis.set_major_formatter(formatter)

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

        #self.axes.xaxis_date()

        self.cid = self.mpl_connect('button_press_event', self.onclick)
Beispiel #9
0
    def __init__(self, parent):
        figure = matplotlib.figure.Figure()
        self.axes = figure.add_subplot(111)

        FigureCanvas.__init__(self, figure)
        self.parent = parent
        self.setParent(parent)

        prefix, suffix = self.parent.market_proxy.getPrefixSuffixCounter()
        precision = self.parent.market_proxy.getPrecisionCounter()
        self.axes.set_xlabel(
            QtCore.QCoreApplication.translate("DepthChartDialog", "Price"))
        format_str = "{}%1.{}f{}".format(prefix, precision, suffix)
        formatter = matplotlib.ticker.FormatStrFormatter(format_str)
        self.axes.xaxis.set_major_formatter(formatter)

        prefix, suffix = self.parent.market_proxy.getPrefixSuffixBase()
        precision = self.parent.market_proxy.getPrecisionBase()
        self.axes.set_ylabel(
            QtCore.QCoreApplication.translate("DepthChartDialog", "Volume"))
        format_str = "{}%1.{}f{}".format(prefix, precision, suffix)
        formatter = matplotlib.ticker.FormatStrFormatter(format_str)
        self.axes.yaxis.set_major_formatter(formatter)

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

        #self.axes.xaxis_date()

        self.cid = self.mpl_connect('button_press_event', self.onclick)
Beispiel #10
0
        def redisplay(event):
            figure.clf()
            axes = figure.add_subplot(1, 1, 1)

            if (event is not None) or (gridding[0] is None):
                do_gridding(first_x.Value, first_y.Value, second_x.Value, second_y.Value)
            self.display_grid(background_image, gridding[0], image_set_number, axes)
            canvas.draw()
Beispiel #11
0
    def __init__(self,
                 title="Osciloscope",
                 width=780,
                 height=360,
                 nr_samples_on_screen=250,
                 graph_type='-',
                 max_value=500,
                 plot_color="lightgreen",
                 bg_color="darkgreen",
                 facecolor="white",
                 ylabel="Current",
                 picker=None):

        gtk.Frame.__init__(self, title)

        self.font = {
            'fontname': 'Liberation Sans',
            'color': 'b',
            'fontweight': 'bold',
            'fontsize': 10
        }

        self.max_value = max_value
        self.nr_samples_on_screen = nr_samples_on_screen
        self.ind = numpy.arange(nr_samples_on_screen)
        self.samples = [0.0] * nr_samples_on_screen

        figure = matplotlib.figure.Figure(figsize=(10, 4),
                                          dpi=100,
                                          facecolor=facecolor)
        ax = figure.add_subplot(111)
        self.ax = ax
        ax.set_axis_bgcolor(bg_color)

        self.on_screen_samples = ax.plot(self.ind,
                                         self.samples,
                                         graph_type,
                                         color=plot_color,
                                         picker=picker)

        ax.set_ylim(0, max_value)
        ax.set_ylabel(ylabel, self.font)
        ax.set_xlabel("%d samples" % nr_samples_on_screen, self.font)
        ax.set_xticks(range(0, nr_samples_on_screen + 1, 20))
        #ax.set_xticklabels(range(0, nr_samples_on_screen + 1, 20))
        ax.grid(True)

        for label in ax.get_yticklabels() + ax.get_xticklabels():
            label.set(fontsize=8)

        self.canvas = figure_canvas(figure)  # a gtk.DrawingArea
        self.canvas.set_size_request(width, height)

        self.add(self.canvas)
        self.modify_bg(gtk.STATE_NORMAL, gtk.gdk.color_parse(facecolor))
        self.nr_samples = 0
Beispiel #12
0
        def redisplay(event):
            figure.clf()
            axes = figure.add_subplot(1, 1, 1)

            if (event is not None) or (gridding[0] is None):
                do_gridding(first_x.Value, first_y.Value, second_x.Value,
                            second_y.Value)
            self.display_grid(background_image, gridding[0], image_set_number,
                              axes)
            canvas.draw()
Beispiel #13
0
def drawFigureForRange(figure, data, frame, frame_range):
    global last_index
    sub = figure.add_subplot(
        212)  # creates a subplot (lower of the 2 veritcal rows)

    for limbKey in data.keys():  # iterate over each limb
        limb = data[limbKey]
        bottom = max(
            0, frame - frame_range
        )  # `frame_range` frames before the current frame, or 0th frame
        top = min(
            len(limb["magnitude"]), frame + frame_range
        )  # `frame_range` frames after the current frame, or the last frame
        sub.plot(np.arange(bottom, top),
                 limb["butter"][bottom:top],
                 '-',
                 color=limb["color"])  # plot the butterworth processed data

        transparent_color = limb["color"] + (0.15, )

        for spike in limb["start_spikes"]:  # draw start of each activity red
            if spike < frame - frame_range:
                continue
            if spike > frame + frame_range:
                break
            sub.axvline(x=spike, color=limb["color"])

        for i in range(
                last_index, limb["mid_spikes_count"]
        ):  # draw duration of each activity as mostly transparent
            spike = limb["mid_spikes"][i]
            if spike < frame - frame_range:
                last_index = i
                continue
            if spike > frame + frame_range:
                break
            sub.axvline(x=spike, color=transparent_color)

        for spike in limb["end_spikes"]:  # draw end of each activity red
            if spike < frame - frame_range:
                continue
            if spike > frame + frame_range:
                break
            sub.axvline(x=spike, color=limb["color"])

    sub.axvline(
        x=frame, color="red"
    )  #really a blue line b/c of OpenCV - Matplotlib changes in color formatting

    #sets axes labels and limits for the subplot
    sub.set_xlabel("Frame #")
    sub.set_ylabel("Magnitude")
    sub.set_xlim(frame - frame_range, frame + frame_range)
    sub.set_ylim(0, 2.25)
Beispiel #14
0
def visualize_map(occupancy_map, particles):
    x = particles[:, 0] / 10
    y = particles[:, 1] / 10

    fig = plt.figure(1)
    ax = fig.add_subplot(111)
    ax.imshow(occupancy_map, cmap='Greys')
    ax.scatter(x, y, color='r', marker='.', s=10)
    fig.canvas.draw()
    plt.show(block=False)
    ax.clear()
    #time.sleep(1)
    """
Beispiel #15
0
def parseAndDrawHDF5(figure, data, frameIndex):
    sub = figure.add_subplot(211)
    for limbKey in data.keys():
        limb = data[limbKey]
        sub.arrow(
            limb["pos"][0],
            limb["pos"][1],
            limb["dx"][frameIndex],
            limb["dy"][frameIndex],
            color=limb["color"])  # draw the vector for each limbs' motion

    #Sets axes labels and limits for the subplot
    sub.set_xlabel('x')
    sub.set_ylabel('y')
    sub.set_xlim(-5, 5)
    sub.set_ylim(-5, 5)
Beispiel #16
0
 def __init__(self, name, figure, plot=111):
     gobject.GObject.__init__(self)
     self.name = name
     self.figure = figure
     self.axes = figure.add_subplot(plot)
     self.properties = {'xmin': "",
                        'xmax': "",
                        'ymin': "",
                        'ymax': "",
                        'ylog': "0",
                        'xlog': "0",
                        'xlabel': "",
                        'ylabel': "",
                        'title': "",
                        'grid': "1",
                        'legend': "0"}
     self.figure.canvas.connect('scroll_event', self.event_scroll)
Beispiel #17
0
    def __all__(self):
        A = self.A
        print '\t A', A

        print 'K POCATKU SOURADNEHO SYSTEMU'
        self.local = False
        S_y, S_z = self.S
        c_y, c_z = self.c
        I_y, I_z = self.I
        D_yz = self.Dyz
        I_p = self.Ip

        print '\t S_y', S_y
        print '\t S_z', S_z
        print '\t cy', c_y
        print '\t cz', c_z
        print '\t Iy', I_y
        print '\t Iz', I_z
        print '\t Dyz', D_yz
        print '\t I_p', I_p

        print 'K TEZISTI'
        self.local = True
        S_y, S_z = self.S
        I_y, I_z = self.I
        D_yz = self.Dyz
        I_p = self.Ip
        PrincipalValues = self.PrincipalValues

        print '\t Iy', I_y
        print '\t Iz', I_z
        print '\t Dyz', D_yz
        print '\t I_p', I_p
        print '\t PrincipalValues', PrincipalValues
        I1, I2, alpha = PrincipalValues
        i1 = sqrt(I1 / A)
        i2 = sqrt(I2 / A)
        print i1, i2

        fig = p.figure()
        ax = fig.add_subplot(111)
        e = Ellipse((c_y, c_z), width = i1 * 2, height = i2 * 2, angle = alpha * 180 / pi + 90, fill = False)
        ax.plot(self.pol[:, 0], self.pol[:, 1])
        ax.plot(self.c[0], self.c[1], 'ro')
        ax.add_patch(e)
        p.show()
Beispiel #18
0
def plotSectors(figure, data):
    """
    Dessine le graphs des secteurs économiques
    :param figure: La figure sur laquelle le graph est crée
    :param data: Les données à afficher
    :return:
    """
    # Crée le graphique
    sectorsGraph = figure.add_subplot(211)

    # Définit la largeur des barres
    width = 0.2

    x = np.arange(len(data))

    # Définit les trois bares de secteur
    sectorsGraph.bar(x,
                     data[:, 0],
                     width,
                     color='#3498db',
                     label='Capital constant (C)')
    sectorsGraph.bar(x + width,
                     data[:, 1],
                     width,
                     color='#e74c3c',
                     label='Capital variable (V)')
    sectorsGraph.bar(x + (2 * width),
                     data[:, 2],
                     width,
                     color='#f1c40f',
                     label='Surplus (S)')

    # Définit les légendes en X
    sectorsGraph.set_xticks(x + width + width / 2)
    sectorsGraph.set_xticklabels(['Secteur 1', 'Secteur 2', 'Total'])

    # Définit la légende en y
    sectorsGraph.set_ylabel('Milliards de CHF')

    # titre
    sectorsGraph.set_title('Année ' + str(current_year))

    # Définit la grille
    sectorsGraph.grid(True, 'major', 'y', ls='--', lw=.5, c='k', alpha=.3)
Beispiel #19
0
	def __init__(self, title = "Osciloscope", width = 780, height = 360,
		     nr_samples_on_screen = 250, graph_type = '-',
		     max_value = 500, plot_color = "lightgreen",
		     bg_color = "darkgreen", facecolor = "white",
		     ylabel = "Current", picker = None):

		gtk.Frame.__init__(self, title)

		self.font = { 'fontname'   : 'Liberation Sans',
			      'color'      : 'b',
			      'fontweight' : 'bold',
			      'fontsize'   : 10 }

		self.max_value = max_value
		self.nr_samples_on_screen = nr_samples_on_screen
		self.ind = numpy.arange(nr_samples_on_screen)
		self.samples = [ 0.0 ] * nr_samples_on_screen

		figure = matplotlib.figure.Figure(figsize = (10, 4), dpi = 100,
						  facecolor = facecolor)
		ax = figure.add_subplot(111)
		self.ax = ax
		ax.set_axis_bgcolor(bg_color)

		self.on_screen_samples = ax.plot(self.ind, self.samples, graph_type,
						 color = plot_color,
						 picker = picker)

		ax.set_ylim(0, max_value)
		ax.set_ylabel(ylabel, self.font)
		ax.set_xlabel("%d samples" % nr_samples_on_screen, self.font)
		ax.set_xticks(range(0, nr_samples_on_screen + 1, 20))
		#ax.set_xticklabels(range(0, nr_samples_on_screen + 1, 20))
		ax.grid(True)

		for label in ax.get_yticklabels() + ax.get_xticklabels():
			label.set(fontsize = 8)

		self.canvas = figure_canvas(figure)  # a gtk.DrawingArea
		self.canvas.set_size_request(width, height)

		self.add(self.canvas)
		self.modify_bg(gtk.STATE_NORMAL, gtk.gdk.color_parse(facecolor))
		self.nr_samples = 0
Beispiel #20
0
def visualize_map(occupancy_map, particles, imageNumber):
    x = particles[:, 0] / 10
    y = particles[:, 1] / 10

    fig = plt.figure(1)
    # mng = plt.get_current_fig_manager();
    # mng.resize(*mng.window.maxsize())
    ax = fig.add_subplot(111)

    ax.scatter(x, y, color='r', marker='.', s=10)
    ax.imshow(occupancy_map, cmap='Greys')
    fig.canvas.draw()
    plt.show(block=False)

    fileName = 'Renders/working%04d.png' % imageNumber
    plt.savefig(fileName)
    ax.clear()

    #time.sleep(2)
    """
 def create_graph_tab(self, key, data):
     """
     Creates a widget displaying the ``data`` stored under ``key``.
     """
     widget = QtWidgets.QWidget(self)
     # Create figure
     figure = matplotlib.figure.Figure()
     figure.suptitle(self._filename)
     canvas = backend.FigureCanvasQTAgg(figure)
     canvas.setParent(widget)
     axes = figure.add_subplot(1, 1, 1)
     axes.set_title(key)
     toolbar = backend.NavigationToolbar2QT(canvas, widget)
     # Draw lines
     axes.plot(self._time, data)
     # Create a layout
     vbox = QtWidgets.QVBoxLayout()
     vbox.addWidget(canvas)
     vbox.addWidget(toolbar)
     widget.setLayout(vbox)
     self._figures.append(figure)
     self._axes.append(axes)
     return widget
def showList(xList, yList):
    #show
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')

    cateList = [[], [], [], [], [], [], [], [], [], []]
    for i in range(len(yList)):
        y = yList[i]
        x = xList[i]
        cateList[y].append(x)

    color = [
        "red", "black", "yellow", "blue", "green", "pink", "magenta", "cyan",
        '#eeefff', "#ffefff"
    ]

    i = 0
    for list in cateList:

        xList = [i[0] for i in list]
        yList = [i[1] for i in list]
        zList = [0 for i in list]

        ax.scatter(xList,
                   yList,
                   zList,
                   color=color[i],
                   marker='.',
                   alpha=0.8,
                   depthshade=True)
        #plt.scatter(xList, yList, color=color[i], marker = '^', alpha=0.5)
        i += 1
    ax.set_xlabel('X Label')
    ax.set_ylabel('Y Label')
    ax.set_zlabel('Z Label')
    plt.show()
Beispiel #23
0
def main():
    """
    Plot the ISMIP-HOM test results.
    """
    
    if args.all_ps:
         nonFSmodelType='All Partial Stokes'
    else:
         nonFSmodelType='First Order'
    print 'NOTE: The category being used for models approximating Full Stokes is: '+nonFSmodelType
    print 'For more information, see details of option -a by invoking:   python plotISMIP_HOM.py --help \n'


    # =========================================================
    #  First generate the standard ascii files defined by ISMIP-HOM
    #  (This used to be in the run script, but is more useful and natural here.)
    #  Note: It is not strictly necessary to generate these files
    #  just to plot the results, but this approach is left in 
    #  to use existing code, and on the off-chance it is ever
    #  necessary to generate these standard ascii ISMIP-HOM files...
    # =========================================================

    pattern = get_file_pattern() 

    experiments, sizes = get_experiments_and_sizes(pattern)

    if args.sizes and (set(args.sizes) <= set(map(int,sizes))):
        sizes = args.sizes
    elif args.sizes:
        print("ERROR: No output files found for that size!")
        print("   Run `ls "+pattern.replace('-?','-[a-e]')+"`\non the command line to see the set of output files. (Note, test f's size is ignored.)")
        sys.exit(1)

    # sort experiments and sizes
    sizes.sort()
    experiments.sort()

    # Loop over the experiments
    for experiment in experiments:

        # Loop over the sizes
        for size in map(int,sizes):
            try:
                # Extract the output data for comparison to the other models

                # NOTE: The script now assumes that uvel_extend and vvel_extend are ALWAYS present.
                # Those fields contain the ice velocity computed at the upper right corner of each grid cell.
                # They appear to be on the x1,y1 grid in their metadata but are actually on the x0,y0 grid.
                # The additional row/column include the first halo value past ewn/nsn.
                # That value is valid at both 0.0 and 1.0 on the non-dimensional coordinate system.
                # Matrix manipulations for each test case below are done to create a larger matrix that goes from 0.0 to 1.0, inclusive.
                # NOTE: The cases below are only writing [x,y,u,v] to the text file.  This is the minimum needed to compare to other models.
                # In the future, the other additional fields specified in section 4 of http://homepages.ulb.ac.be/~fpattyn/ismip/ismiphom.pdf
                # can be added.  wvel and the stresses are on the x1,y1 grid, so they would need to be interpolated to the x0,y0 grid
                # since we are using that as the coordinate system in the text files.

                # Open the netCDF file that was written by CISM

                # Standard filename format used by both scripts
                if experiment == 'f': 
                    size = 100
                
                out_file = pattern.replace('-?','-'+experiment).replace('????',str(size).zfill(4))
                netCDFfile = NetCDFFile(out_file,'r')
                if netCDF_module == 'Scientific.IO.NetCDF':
                    velscale = netCDFfile.variables['uvel_extend'].scale_factor
                else:
                    velscale = 1.0


                if experiment in ['f',]:
                    # Convert CISM output data to the rotated coord system used by the problem setup
                    alpha = -3.0 * pi/180  # defined in run script
                    if netCDF_module == 'Scientific.IO.NetCDF':
                        thkscale = netCDFfile.variables['thk'].scale_factor
                        wvelscale = netCDFfile.variables['wvel_ho'].scale_factor
                    else:
                        thkscale = 1.0
                        wvelscale = 1.0
                    
                    usurf = netCDFfile.variables['usurf'][-1,:,:] * thkscale  # get last time level
                    usurfStag = (usurf[1:,1:] + usurf[1:,:-1] + usurf[:-1,:-1] + usurf[:-1, :-1]) / 4.0
                    uvelS = netCDFfile.variables['uvel'][-1,0,:,:] * velscale  # top level of last time
                    vvelS = netCDFfile.variables['vvel'][-1,0,:,:] * velscale  # top level of last time
                    wvelS = netCDFfile.variables['wvel_ho'][-1,0,:,:] * wvelscale  # top level of last time
                    wvelStag = (wvelS[1:,1:] + wvelS[1:,:-1] + wvelS[:-1,:-1] + wvelS[:-1, :-1]) / 4.0
                    x0 = netCDFfile.variables['x0'][:]
                    y0 = netCDFfile.variables['y0'][:]
                    # calculate rotated xprime coordinates along the surface - xx, yy are used by code below to write the output
                    xx = x0 * cos(alpha) + (usurfStag[20,:]-7000.0) * sin(alpha)
                    xx = xx/1000.0 - 50.0
                    yy = y0/1000.0 - 50.0
                    # calculate rotated uvel/vvel at surface
                    uvelSprime =  uvelS[:,:] * cos(alpha) + wvelStag[:,:] * sin(alpha)
                    wvelSprime = -uvelS[:,:] * sin(alpha) + wvelStag[:,:] * cos(alpha)

                    nan = np.ones(uvelSprime.shape)*-999.0  # create a dummy matrix for uncalculated values.

                    # ===========================================
                    # optional bit of code to plot out vertical velocity on the rotated grid.
                    # This can be compared to Fig. 14b in the tc-2007-0019-sp3.pdf document
                    figure = pyplot.figure(subplotpars=matplotlib.figure.SubplotParams(top=.85,bottom=.15))
                    axes = figure.add_subplot(111)
                    pc = axes.pcolor(wvelSprime)
                    pyplot.colorbar(pc)
                    cntr=axes.contour(wvelSprime, [-0.5, -0.4, -0.3, -0.2, -0.1, 0.0, 0.1, 0.2, 0.3, 0.4],colors='k')
                    axes.clabel(cntr)
                    pyplot.title('Compare to Fig. 14b of tc-2007-0019-sp3.pdf')
                    pyplot.draw()
                    pyplot.show()
                    # ===========================================

                else:  # all other tests
                    # Make x,y position arrays that can be used by all test cases.
                    #   Want x/y positions to include the periodic edge at both the beginning and end
                    xx = netCDFfile.variables['x0'][:]/(1000.0*float(size))
                    xx = np.concatenate(([0.0],xx,[1.0]))
                    yy = netCDFfile.variables['y0'][:]/(1000.0*float(size))
                    yy = np.concatenate(([0.0],yy,[1.0]))
                    if experiment in ('b','d'):
                        yy = (yy[len(yy)/2],)  # for the 2-d experiments, just use the middle y-index

                    # Figure out u,v since all experiments needs at least one of them (avoids duplicate code in each case below
                    #   Want to use last time level.  Most experiments should only have a single time level, but F may have many in the file.
                    #   Apparently some older versions of netCDF4 give an error when using the -1 dimension if the size is 1, hence this bit of seemingly unnecessary logic...
                    if netCDFfile.variables['uvel_extend'][:].shape[0] == 1:
                        t = 0
                    else:
                        t = -1
                    us = netCDFfile.variables['uvel_extend'][t,0,:,:] * velscale  # top level of last time
                    us = np.concatenate( (us[:,-1:], us), axis=1)  # copy the column at x=1.0 to x=0.0
                    us = np.concatenate( (us[-1:,:], us), axis=0)  # copy the row at y=1.0 to y=0.0
                    vs = netCDFfile.variables['vvel_extend'][t,0,:,:] * velscale  # top level of last time
                    vs = np.concatenate( (vs[:,-1:], vs), axis=1)  # copy the column at x=1.0 to x=0.0
                    vs = np.concatenate( (vs[-1:,:], vs), axis=0)  # copy the row at y=1.0 to y=0.0
                    ub = netCDFfile.variables['uvel_extend'][t,-1,:,:] * velscale  # bottom level of last time
                    ub = np.concatenate( (ub[:,-1:], ub), axis=1)  # copy the column at x=1.0 to x=0.0
                    ub = np.concatenate( (ub[-1:,:], ub), axis=0)  # copy the row at y=1.0 to y=0.0
                    vb = netCDFfile.variables['vvel_extend'][t,-1,:,:] * velscale  # bottom level of last time
                    vb = np.concatenate( (vb[:,-1:], vb), axis=1)  # copy the column at x=1.0 to x=0.0
                    vb = np.concatenate( (vb[-1:,:], vb), axis=0)  # copy the row at y=1.0 to y=0.0
                    #nan = ub*np.NaN  # create a dummy matrix for uncalculated values.
                    nan = np.ones(ub.shape)*-999.0  # create a dummy matrix for uncalculated values.

                # make arrays of the variables needed for each experiment
                # the extended-grid velocities have the periodic edge in the last x-position.  We also want it in the first x-position.
                # After building the 2-d array as needed for each variable from the raw file data, then build a list called 'data'.
                if experiment == 'a':
                    #  This is supposed to be: [('uvel',0),('vvel',0),('wvel',0),('tau_xz',-1),('tau_yz',-1),[deltap]]
                    data = (us, vs, nan, nan, nan, nan)
                elif experiment == 'b':
                    #  This is supposed to be: uvel(0), wvel(0), tau_xz(-1), deltaP
                    data = (us, nan, nan, nan)
                elif experiment == 'c':
                    #  This is supposed to be: [uvel',0),('vvel',0),('wvel',0),('uvel',-1),('vvel',-1),('tau_xz',-1),('tau_yz',-1), deltap]
                    data = (us, vs, nan, ub, vb, nan, nan, nan)
                elif experiment == 'd':
                    #  This is supposed to be:  [('uvel',0),('wvel',0),('uvel',-1),('tau_xz',-1), deltap]
                    data = (us, nan, nan, nan, nan)
                elif experiment == 'f':  # should be: x, y, zs, vx, vy, vz
                    #  This is supposed to be: [('usurf',None),('uvel',0),('vvel',0),('wvel',0)]
                    data = (nan, uvelSprime, vvelS, nan)

                # Write a "standard" ISMIP-HOM file (example file name: "cis1a020.txt") in the "output" subdirectory 
                ISMIP_HOMfilename = out_file.replace('.out.nc','.txt')
                ISMIP_HOMfile = open(ISMIP_HOMfilename,'w')
                for i, x in enumerate(xx):
                    for j, y in enumerate(yy):
                        if experiment in ('a','c','f'):  # include x and y positions
                            ISMIP_HOMfile.write('\t'.join(map(str,[x,y]+[v[j,i] for (v) in data]))+'\n')
                        else:  # only include x position
                            ISMIP_HOMfile.write('\t'.join(map(str,[x]+[v[j,i] for (v) in data]))+'\n')
                ISMIP_HOMfile.close()
                netCDFfile.close()
            except:
                print 'Warning: The CISM output file for experiment '+experiment+' at size '+str(size)+' could NOT be read successfully!'
                raise

    # =========================================================
    #  Now actually analyze the results.
    # =========================================================
    
    # Loop over the experiments requested on the command line
    for experiment in experiments:
        print 'ISMIP-HOM', experiment.upper()

        # Create the figure on which the plot axes will be placed
        figure = pyplot.figure(subplotpars=matplotlib.figure.SubplotParams(top=.85,bottom=.15))
        figure.text(0.5,0.92,'ISMIP-HOM Experiment '+experiment.upper(),horizontalalignment='center',size='large')
        if args.subtitle:
            figure.text(0.5,0.89,args.subtitle,horizontalalignment='center',size='small')
        figure.text(0.5,0.1,'Normalized X coordinate',horizontalalignment='center',size='small')
        figure.text(0.06,0.5,'Ice Speed (m/a)',rotation='vertical',verticalalignment='center')
        # Create the (three column) legend
        prop = matplotlib.font_manager.FontProperties(size='x-small')
        Line2D = matplotlib.lines.Line2D([],[],color=(0,0,0))
        figure.legend([Line2D],['Model Output'],loc=(0.1,0.05),prop=prop).draw_frame(False)
        Line2D.set_linestyle(':')
        Line2D.set_color((1,0,0))
        Patch = matplotlib.patches.Patch(edgecolor=None,facecolor=(1,0,0),alpha=0.25)
        figure.legend([Line2D,Patch],['Full Stokes Mean','Full Stokes Std. Dev.'],loc=(0.3,0.02),prop=prop).draw_frame(False)
        Line2D.set_color((0,0,1))
        Patch.set_facecolor((0,0,1))
        figure.legend([Line2D,Patch],[nonFSmodelType+' Mean',nonFSmodelType+' Std. Dev.'],loc=(0.55,0.02),prop=prop).draw_frame(False)

        # Loop over the sizes requested on the command line
        for i, size in enumerate(map(int,sizes)):
            try:
                if experiment == 'f': 
                    if size != 100 or len(sizes) > 1:
                        print 'NOTE: Experiment f uses a domain size of 100 km only'
                    size = 100
                
                out_file = pattern.replace('-?','-'+experiment).replace('????',str(size).zfill(4))

                # Create the plot axes for this domain size
                if len(sizes) == 1:
                    axes = figure.add_subplot(111)
                else:
                    axes = figure.add_subplot(2,3,i+1)
                    for tick in axes.xaxis.get_major_ticks():
                        tick.label1.set_fontsize('xx-small')
                    for tick in axes.yaxis.get_major_ticks():
                        tick.label1.set_fontsize('xx-small')
                axes.set_title('%d km' % size, size='medium')

                # Get the Glimmer output data
                glimmerData = read(out_file.replace('.out.nc','.txt'),experiment)
                # The Glimmer data is on a staggered grid;
                # Interpolate to obtain the value at x=0 and x=1
                # using periodic boundary conditions
                #v = (glimmerData[0][1] + glimmerData[-1][1])/2
                #glimmerData = [(0.0,v)]+glimmerData+[(1.0,v)]

                # Plot the Glimmer data
                axes.plot([row[0] for row in glimmerData],
                          [row[1] for row in glimmerData],color='black')

                # Get the data from other models for comparison
                firstOrder = 0
                fullStokes = 1
                count = [0,0]
                sumV  = [[0.0 for v in glimmerData],[0.0 for v in glimmerData]]
                sumV2 = [[0.0 for v in glimmerData],[0.0 for v in glimmerData]]
                for (path,directories,filenames) in os.walk('ismip_all'):
                    for filename in filenames:
                        modelName = filename[0:4]
                        modelExperiment = filename[4]
                        modelSize = filename[5:8]
                        #print 'name, exp, size', modelName, modelExperiment, modelSize
                        if modelName == 'aas1':
                            # Skip the 'aas1' model because its output files in the tc-2007-0019-sp2.zip file do not follow the proper naming convention.  MJH 11/5/13
                            continue
                        if (modelExperiment != experiment) or (modelExperiment != 'f' and int(modelSize) != size) \
                        or (not args.all_ps and not modelName in lmlaModels + fullStokesModels):
                            continue # continue next loop iteration if not the size or not the experiment desired or if we just want FO comparison and this model is not FO or FS.
                        elif (modelExperiment == 'f'):
                            if (modelSize == '001'):
                                  continue # ignore the sliding version for now
                            if modelName == 'cma1':
                                  continue  # the cma1 'f' experiments made the x,y coords dimensionless instead of dimensional - ignore for convenience
                        print 'Using data from file:',os.path.join(path,filename)
                        data = read(os.path.join(path,filename),experiment)
                        if modelName in fullStokesModels:
                            index = fullStokes
                        else:
                            index = firstOrder
                        count[index] += 1

                        #axes.plot([row[0] for row in data], [row[1] for row in data] )   ## OPTIONAL: print out every individual model in its native x-coordinates.

                        # Interpolate onto the x values from the Glimmer model run
                        for (i,target) in enumerate([row[0] for row in glimmerData]):
                            below = -99999.0
                            above =  99999.0
                            for (j,x) in enumerate([row[0] for row in data]):
                                if  below <  x <= target: b,below = j,x
                                if target <= x <  above:  a,above = j,x
                            if above == below:
                                v = data[a][1]
                            else:
                                if below == -99999.0: # Use the periodic boundary condition at x = 0
                                    xBelow = data[-1][0] - 1
                                    vBelow = data[-1][1]
                                else:
                                    xBelow,vBelow = data[b]
                                
                                if above ==  99999.0: # Use the periodic boundary condition at x = 1
                                    xAbove = data[0][0] + 1
                                    vAbove = data[0][1]
                                else:
                                    xAbove,vAbove = data[a]
                                
                                if xAbove == xBelow:
                                    print 'Surprise!',above,below,xAbove,xBelow,vAbove,vBelow
                                    v = (vAbove+vBelow)/2
                                else:
                                    alpha = (target-xBelow)/(xAbove-xBelow)
                                    v = alpha*vAbove + (1-alpha)*vBelow
                            sumV [index][i] += v
                            sumV2[index][i] += v*v

                # Calculate statistics of the other model results
                if sum(count) == 0:
                    print 'To compare with other models you need to download the ISMIP-HOM results from: http://www.the-cryosphere.net/2/95/2008/tc-2-95-2008-supplement.zip and unzip the contained file tc-2007-0019-sp2.zip into a directory named ismip_all.  The ismip_all directory must be in the directory from which you are running this script.'
                else:
                    # Find the mean and standard deviation of the velocities at each x
                    for index in (firstOrder,fullStokes):
                        if count[index] == 0:
                            continue
                        mean = list()
                        standardDeviation = list()
                        for i in range(len(glimmerData)):
                            mean.append(sumV[index][i]/count[index])
                            standardDeviation.append(sqrt(sumV2[index][i]/count[index]-mean[-1]**2))

                        # Plot the mean using a dotted line
                        color = (index,0,1-index) # blue for first order (index=0); red for full Stokes (index=1)
                        x = [row[0] for row in glimmerData]
                        axes.plot(x,mean,':',color=color)

                        # Plot a filled polygon showing the mean plus and minus one standard deviation
                        meanMinusSD = [m-sd for (m,sd) in zip(mean,standardDeviation)]
                        meanPlusSD  = [m+sd for (m,sd) in zip(mean,standardDeviation)]
                        x = x + list(reversed(x))
                        y = meanPlusSD + list(reversed(meanMinusSD))
                        axes.fill(x,y,facecolor=color,edgecolor=color,alpha=0.25)

                        if index == firstOrder:
                            # Calculate some statistics comparing the Glimmer data with the other models
                            pcterror = [100.0*abs(glimmer-others)/others for (glimmer,others) in zip([row[1] for row in glimmerData],mean)]
                            abserror = [abs(glimmer-others)        for (glimmer,others) in zip([row[1] for row in glimmerData],mean)]
                            maximum = max(pcterror)
                            position = glimmerData[pcterror.index(maximum)][0]
                            total   = sum([e for e in pcterror])
                            compare = sum([(s/m) for (s,m) in zip(standardDeviation,mean)])
                            n = len(glimmerData)
                            #print '\t'.join([str(size)+' km',str(total/n),str(compare/n),str(position)])
                            print 'Size='+str(size)+' km' 
                            print '  Mean percent error along flowline of CISM relative to mean of first-order models='+str(total/float(n))+'%'
                            print '  Mean COD (stdev/mean) along flowline of mean of first-order models (excluding CISM)='+str(compare/float(n)*100.0)+'%'
                            print '  Max. CISM percent error='+str(maximum)+'% at x-position '+str(position)
                            print '  Max. CISM absolute error='+str(max(abserror))+' m/yr at x-position '+str(glimmerData[abserror.index(max(abserror))][0])

            except:
                print "Error in analyzing/plotting experiment ",experiment," at size ",size," km"

        if savePlotInFile:
            plot_file = pattern.replace('-?','-'+experiment).replace('.????','').replace('.out.nc',plotType)
            print 'Writing:', plot_file
            pyplot.savefig(plot_file)

        # Experiment f can also have a surface profile plotted
        if experiment == 'f':
            # rather than getting the data from the text file, we are going to read it directly.
            # this is because the velocities and usrf are on different grids, so it is difficult to include them
            # both in the standard ISMIP-HOM text file format that has a single x,y coord. system
            size = 100
            out_file = pattern.replace('-?','-'+experiment).replace('????',str(size).zfill(4))
            netCDFfile = NetCDFFile(out_file,'r')
            if netCDF_module == 'Scientific.IO.NetCDF':
                thkscale = netCDFfile.variables['thk'].scale_factor
            else:
                thkscale = 1.0
            usurf = netCDFfile.variables['usurf'][-1,:,:] * thkscale  # get last time level
            x1 = netCDFfile.variables['x1'][:]
            y1 = netCDFfile.variables['y1'][:]

            #  Create the usurf figure
            ufigure = pyplot.figure(subplotpars=matplotlib.figure.SubplotParams(top=.85,bottom=.15))
            ufigure.text(0.5,0.92,'ISMIP-HOM Experiment F: Surface elevation',horizontalalignment='center',size='large')
            if args.subtitle:
                ufigure.text(0.5,0.89,args.subtitle,horizontalalignment='center',size='small')
            ufigure.text(0.5,0.1,'X coordinate',horizontalalignment='center',size='small')
            ufigure.text(0.06,0.5,'upper surface (m)',rotation='vertical',verticalalignment='center')
            # Create the (three column) legend
            prop = matplotlib.font_manager.FontProperties(size='x-small')
            Line2D = matplotlib.lines.Line2D([],[],color=(0,0,0))
            ufigure.legend([Line2D],['Model Output'],loc=(0.1,0.05),prop=prop).draw_frame(False)
            Line2D.set_linestyle(':')
            Line2D.set_color((1,0,0))
            Patch = matplotlib.patches.Patch(edgecolor=None,facecolor=(1,0,0),alpha=0.25)
            ufigure.legend([Line2D,Patch],['Full Stokes Mean','Full Stokes Std. Dev.'],loc=(0.3,0.02),prop=prop).draw_frame(False)
            Line2D.set_color((0,0,1))
            Patch.set_facecolor((0,0,1))
            ufigure.legend([Line2D,Patch],[nonFSmodelType+' Mean',nonFSmodelType+' Std. Dev.'],loc=(0.55,0.02),prop=prop).draw_frame(False)
            # Create the plot axes 
            axes2 = ufigure.add_subplot(111)
            axes2.set_title('%d km' % size, size='medium')

            # Convert CISM output data to the rotated coord system used by the problem setup
            alpha = -3.0 * pi/180  # defined in run script
            # use integer floor division operator to get an index close to the center  TODO should be interpolating if needed...
            yp = len(y1)//2
            # calculate rotated xprime, zprime coordinates along the surface (this is the coord. sys. used for this test case)
            xprime =  x1 * cos(alpha) + (usurf[yp,:]-7000.0) * sin(alpha)
            xprime = xprime/1000.0 - 50.0
            zprime = -x1 * sin(alpha) + (usurf[yp,:]-7000.0) * cos(alpha)
            # Plot CISM output
            axes2.plot(xprime, zprime, color='black')

            # create glimmerData so we can re-use the code from above
            glimmerData = list()
            for i in range(len(xprime)):
                glimmerData.append(tuple([xprime[i], zprime[i]]))

            # Now plot the other models - yucky code copied from above
            # Get the data from other models for comparison
            firstOrder = 0
            fullStokes = 1
            count = [0,0]
            sumV  = [[0.0 for v in glimmerData],[0.0 for v in glimmerData]]
            sumV2 = [[0.0 for v in glimmerData],[0.0 for v in glimmerData]]
            for (path,directories,filenames) in os.walk('ismip_all'):
                for filename in filenames:
                    modelName = filename[0:4]
                    modelExperiment = filename[4]
                    modelSize = filename[5:8]
                    #print 'name, exp, size', modelName, modelExperiment, modelSize
                    if modelName == 'aas1':
                        # Skip the 'aas1' model because its output files in the tc-2007-0019-sp2.zip file do not follow the proper naming convention.  MJH 11/5/13
                        continue
                    if (modelExperiment != experiment) or (modelExperiment != 'f' and int(modelSize) != size) \
                    or (not args.all_ps and not modelName in lmlaModels + fullStokesModels):
                        continue # continue next loop iteration if not the size or not the experiment desired or if we just want FO comparison and this model is not FO or FS.
                    elif (modelExperiment == 'f'):
                        if (modelSize == '001'):
                            continue # ignore the sliding version for now
                        if modelName == 'cma1':
                            continue  # the cma1 'f' experiments made the x,y coords dimensionless instead of dimensional - ignore for convenience
                    print 'Using data from file:',os.path.join(path,filename)
                    data = read(os.path.join(path,filename), experiment='f-elevation')
                    if modelName in fullStokesModels:
                        index = fullStokes
                    else:
                        index = firstOrder
                    count[index] += 1

                    #axes2.plot([row[0] for row in data], [row[1] for row in data] )   ## OPTIONAL: print out every individual model in its native x-coordinates.

                    # Interpolate onto the x values from the Glimmer model run
                    for (i,target) in enumerate([row[0] for row in glimmerData]):
                        below = -99999.0
                        above =  99999.0
                        for (j,x) in enumerate([row[0] for row in data]):
                            if  below <  x <= target: b,below = j,x
                            if target <= x <  above:  a,above = j,x
                        if above == below:
                            v = data[a][1]
                        else:
                            if below == -99999.0: # Use the periodic boundary condition at x = 0
                                xBelow = data[-1][0] - 1
                                vBelow = data[-1][1]
                            else:
                                xBelow,vBelow = data[b]
                            if above ==  99999.0: # Use the periodic boundary condition at x = 1
                                xAbove = data[0][0] + 1
                                vAbove = data[0][1]
                            else:
                                xAbove,vAbove = data[a]
                            if xAbove == xBelow:
                                print 'Surprise!',above,below,xAbove,xBelow,vAbove,vBelow
                                v = (vAbove+vBelow)/2
                            else:
                                alpha = (target-xBelow)/(xAbove-xBelow)
                                v = alpha*vAbove + (1-alpha)*vBelow
                        sumV [index][i] += v
                        sumV2[index][i] += v*v

                    # Calculate statistics of the other model results
                    if sum(count) == 0:
                        print 'To compare with other models you need to download the ISMIP-HOM results from: http://www.the-cryosphere.net/2/95/2008/tc-2-95-2008-supplement.zip and unzip the contained file tc-2007-0019-sp2.zip into a directory named ismip_all.  The ismip_all directory must be in the directory from which you are running this script.'
                    else:
                        # Find the mean and standard deviation of the velocities at each x
                        for index in (firstOrder,fullStokes):
                            if count[index] == 0:
                                continue
                            mean = list()
                            standardDeviation = list()
                            for i in range(len(glimmerData)):
                                mean.append(sumV[index][i]/count[index])
                                standardDeviation.append(sqrt(sumV2[index][i]/count[index]-mean[-1]**2))

                            # Plot the mean using a dotted line
                            color = (index,0,1-index) # blue for first order (index=0); red for full Stokes (index=1)
                            x = [row[0] for row in glimmerData]
                            axes2.plot(x,mean,':',color=color)

                            # Plot a filled polygon showing the mean plus and minus one standard deviation
                            meanMinusSD = [m-sd for (m,sd) in zip(mean,standardDeviation)]
                            meanPlusSD  = [m+sd for (m,sd) in zip(mean,standardDeviation)]
                            x = x + list(reversed(x))
                            y = meanPlusSD + list(reversed(meanMinusSD))
                            axes2.fill(x,y,facecolor=color,edgecolor=color,alpha=0.25)

            if savePlotInFile:
                plot_file = pattern.replace('-?','-'+experiment+'-SurfaceElevation').replace('.????','').replace('.out.nc',plotType)
                print 'Writing:', plot_file
                pyplot.savefig(plot_file)

        # Experiment f should be run for one size (100 km) only
        if experiment == 'f': 
              break

    if not savePlotInFile:
        pyplot.show()
Beispiel #24
0
    def handle_interaction(self, current_shape, orig_image):
        from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg
        import wx
        """Show the cropping user interface"""
        pixel_data = stretch(orig_image)
        #
        # Create the UI - a dialog with a figure inside
        #
        style = wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER
        dialog_box = wx.Dialog(
            wx.GetApp().TopWindow,
            -1,
            "Select the cropping region",
            size=(640, 480),
            style=style,
        )
        sizer = wx.BoxSizer(wx.VERTICAL)
        figure = matplotlib.figure.Figure()
        panel = FigureCanvasWxAgg(dialog_box, -1, figure)
        sizer.Add(panel, 1, wx.EXPAND)
        btn_sizer = wx.StdDialogButtonSizer()
        btn_sizer.AddButton(wx.Button(dialog_box, wx.ID_OK))
        btn_sizer.AddButton(wx.Button(dialog_box, wx.ID_CANCEL))
        btn_sizer.Realize()
        sizer.Add(btn_sizer, 0, wx.ALIGN_CENTER_HORIZONTAL | wx.ALL, 5)
        dialog_box.SetSizer(sizer)
        dialog_box.Size = dialog_box.BestSize
        dialog_box.Layout()

        axes = figure.add_subplot(1, 1, 1)
        assert isinstance(axes, matplotlib.axes.Axes)
        if pixel_data.ndim == 2:
            axes.imshow(pixel_data, matplotlib.cm.Greys_r, origin="upper")
        else:
            axes.imshow(pixel_data, origin="upper")
        # t = axes.transData.inverted()
        current_handle = [None]

        def data_xy(mouse_event):
            """Return the mouse event's x & y converted into data-relative coords"""
            x = mouse_event.xdata
            y = mouse_event.ydata
            return x, y

        class Handle(matplotlib.patches.Rectangle):
            dm = max((10, min(pixel_data.shape) / 50))
            height, width = (dm, dm)

            def __init__(self, x, y, on_move):
                x = max(0, min(x, pixel_data.shape[1]))
                y = max(0, min(y, pixel_data.shape[0]))
                self.__selected = False
                self.__color = cellprofiler_core.preferences.get_primary_outline_color(
                )
                self.__color = numpy.hstack(
                    (self.__color, [255])).astype(float) / 255.0
                self.__on_move = on_move
                super(Handle, self).__init__(
                    (x - self.width / 2, y - self.height / 2),
                    self.width,
                    self.height,
                    edgecolor=self.__color,
                    facecolor="none",
                )
                self.set_picker(True)

            def move(self, x, y):
                self.set_xy((x - self.width / 2, y - self.height / 2))
                self.__on_move(x, y)

            def select(self, on):
                self.__selected = on
                if on:
                    current_handle[0] = self
                    self.set_facecolor(self.__color)

                else:
                    self.set_facecolor("none")
                    if current_handle[0] == self:
                        current_handle[0] = None
                figure.canvas.draw()
                dialog_box.Update()

            @property
            def is_selected(self):
                return self.__selected

            @property
            def center_x(self):
                """The handle's notion of its x coordinate"""
                return self.get_x() + self.get_width() / 2

            @property
            def center_y(self):
                """The handle's notion of its y coordinate"""
                return self.get_y() + self.get_height() / 2

            def handle_pick(self, event):
                mouse_event = event.mouseevent
                x, y = data_xy(mouse_event)
                if mouse_event.button == 1:
                    self.select(True)
                    self.orig_x = self.center_x
                    self.orig_y = self.center_y
                    self.first_x = x
                    self.first_y = y

            def handle_mouse_move_event(self, event):
                x, y = data_xy(event)
                if x is None or y is None:
                    return
                x = x - self.first_x + self.orig_x
                y = y - self.first_y + self.orig_y
                if x < 0:
                    x = 0
                if x >= pixel_data.shape[1]:
                    x = pixel_data.shape[1] - 1
                if y < 0:
                    y = 0
                if y >= pixel_data.shape[0]:
                    y = pixel_data.shape[0] - 1
                self.move(x, y)

        class CropRectangle(object):
            def __init__(self, top_left, bottom_right):
                self.__left, self.__top = top_left
                self.__right, self.__bottom = bottom_right
                color = cellprofiler_core.preferences.get_primary_outline_color(
                )
                color = numpy.hstack((color, [255])).astype(float) / 255.0
                self.rectangle = matplotlib.patches.Rectangle(
                    (min(self.__left,
                         self.__right), min(self.__bottom, self.__top)),
                    abs(self.__right - self.__left),
                    abs(self.__top - self.__bottom),
                    edgecolor=color,
                    facecolor="none",
                )
                self.top_left_handle = Handle(top_left[0], top_left[1],
                                              self.handle_top_left)
                self.bottom_right_handle = Handle(bottom_right[0],
                                                  bottom_right[1],
                                                  self.handle_bottom_right)

            def handle_top_left(self, x, y):
                self.__left = x
                self.__top = y
                self.__reshape()

            def handle_bottom_right(self, x, y):
                self.__right = x
                self.__bottom = y
                self.__reshape()

            def __reshape(self):
                self.rectangle.set_xy(
                    (min(self.__left,
                         self.__right), min(self.__bottom, self.__top)))
                self.rectangle.set_width(abs(self.__right - self.__left))
                self.rectangle.set_height(abs(self.__bottom - self.__top))
                self.rectangle.figure.canvas.draw()
                dialog_box.Update()

            @property
            def patches(self):
                return [
                    self.rectangle, self.top_left_handle,
                    self.bottom_right_handle
                ]

            @property
            def handles(self):
                return [self.top_left_handle, self.bottom_right_handle]

            @property
            def left(self):
                return min(self.__left, self.__right)

            @property
            def right(self):
                return max(self.__left, self.__right)

            @property
            def top(self):
                return min(self.__top, self.__bottom)

            @property
            def bottom(self):
                return max(self.__top, self.__bottom)

        class CropEllipse(object):
            def __init__(self, center, radius):
                """Draw an ellipse with control points at the ellipse center and
                a given x and y radius"""
                self.center_x, self.center_y = center
                self.radius_x = self.center_x + radius[0] / 2
                self.radius_y = self.center_y + radius[1] / 2
                color = cellprofiler_core.preferences.get_primary_outline_color(
                )
                color = numpy.hstack((color, [255])).astype(float) / 255.0
                self.ellipse = matplotlib.patches.Ellipse(center,
                                                          self.width,
                                                          self.height,
                                                          edgecolor=color,
                                                          facecolor="none")
                self.center_handle = Handle(self.center_x, self.center_y,
                                            self.move_center)
                self.radius_handle = Handle(self.radius_x, self.radius_y,
                                            self.move_radius)

            def move_center(self, x, y):
                self.center_x = x
                self.center_y = y
                self.redraw()

            def move_radius(self, x, y):
                self.radius_x = x
                self.radius_y = y
                self.redraw()

            @property
            def width(self):
                return abs(self.center_x - self.radius_x) * 4

            @property
            def height(self):
                return abs(self.center_y - self.radius_y) * 4

            def redraw(self):
                self.ellipse.center = (self.center_x, self.center_y)
                self.ellipse.width = self.width
                self.ellipse.height = self.height
                self.ellipse.figure.canvas.draw()
                dialog_box.Update()

            @property
            def patches(self):
                return [self.ellipse, self.center_handle, self.radius_handle]

            @property
            def handles(self):
                return [self.center_handle, self.radius_handle]

        if self.shape == SH_ELLIPSE:
            if current_shape is None:
                current_shape = {
                    EL_XCENTER: pixel_data.shape[1] / 2,
                    EL_YCENTER: pixel_data.shape[0] / 2,
                    EL_XRADIUS: pixel_data.shape[1] / 2,
                    EL_YRADIUS: pixel_data.shape[0] / 2,
                }
            ellipse = current_shape
            shape = CropEllipse(
                (ellipse[EL_XCENTER], ellipse[EL_YCENTER]),
                (ellipse[EL_XRADIUS], ellipse[EL_YRADIUS]),
            )
        else:
            if current_shape is None:
                current_shape = {
                    RE_LEFT: pixel_data.shape[1] / 4,
                    RE_TOP: pixel_data.shape[0] / 4,
                    RE_RIGHT: pixel_data.shape[1] * 3 / 4,
                    RE_BOTTOM: pixel_data.shape[0] * 3 / 4,
                }
            rectangle = current_shape
            shape = CropRectangle(
                (rectangle[RE_LEFT], rectangle[RE_TOP]),
                (rectangle[RE_RIGHT], rectangle[RE_BOTTOM]),
            )
        for patch in shape.patches:
            axes.add_artist(patch)

        def on_mouse_down_event(event):
            axes.pick(event)

        def on_mouse_move_event(event):
            if current_handle[0] is not None:
                current_handle[0].handle_mouse_move_event(event)

        def on_mouse_up_event(event):
            if current_handle[0] is not None:
                current_handle[0].select(False)

        def on_pick_event(event):
            for h in shape.handles:
                if id(h) == id(event.artist):
                    h.handle_pick(event)

        figure.canvas.mpl_connect("button_press_event", on_mouse_down_event)
        figure.canvas.mpl_connect("button_release_event", on_mouse_up_event)
        figure.canvas.mpl_connect("motion_notify_event", on_mouse_move_event)
        figure.canvas.mpl_connect("pick_event", on_pick_event)

        try:
            if dialog_box.ShowModal() != wx.ID_OK:
                raise ValueError("Cancelled by user")
        finally:
            dialog_box.Destroy()
        if self.shape == SH_RECTANGLE:
            return {
                RE_LEFT: shape.left,
                RE_TOP: shape.top,
                RE_RIGHT: shape.right,
                RE_BOTTOM: shape.bottom,
            }
        else:
            return {
                EL_XCENTER: shape.center_x,
                EL_YCENTER: shape.center_y,
                EL_XRADIUS: shape.width / 2,
                EL_YRADIUS: shape.height / 2,
            }
Beispiel #25
0
    def run(self, workspace):
        """Run the module

        workspace    - The workspace contains
            pipeline     - instance of cpp for this run
            image_set    - the images in the image set being processed
            object_set   - the objects (labeled masks) in this image set
            measurements - the measurements for this run
            frame        - the parent frame to whatever frame is created. None means don't draw.
        """
        background_image = self.get_background_image(workspace, None)

        if (self.each_or_once == EO_ONCE and
                    self.get_good_gridding(workspace) is not None):
            gridding = self.get_good_gridding(workspace)
        if self.auto_or_manual == AM_AUTOMATIC:
            gridding = self.run_automatic(workspace)
        elif self.manual_choice == MAN_COORDINATES:
            gridding = self.run_coordinates(workspace)
        elif self.manual_choice == MAN_MOUSE:
            gridding = workspace.interaction_request(self, background_image,
                                                     workspace.measurements.image_set_number)
        self.set_good_gridding(workspace, gridding)
        workspace.set_grid(self.grid_image.value, gridding)
        #
        # Save measurements
        #
        self.add_measurement(workspace, F_X_LOCATION_OF_LOWEST_X_SPOT,
                             gridding.x_location_of_lowest_x_spot)
        self.add_measurement(workspace, F_Y_LOCATION_OF_LOWEST_Y_SPOT,
                             gridding.y_location_of_lowest_y_spot)
        self.add_measurement(workspace, F_ROWS, gridding.rows)
        self.add_measurement(workspace, F_COLUMNS, gridding.columns)
        self.add_measurement(workspace, F_X_SPACING, gridding.x_spacing)
        self.add_measurement(workspace, F_Y_SPACING, gridding.y_spacing)

        # update background image
        background_image = self.get_background_image(workspace, gridding)

        workspace.display_data.gridding = gridding.serialize()
        workspace.display_data.background_image = background_image
        workspace.display_data.image_set_number = workspace.measurements.image_set_number

        if self.wants_image:
            import matplotlib.transforms
            import matplotlib.figure
            import matplotlib.backends.backend_agg
            from cellprofiler.gui.tools import figure_to_image
            figure = matplotlib.figure.Figure()
            canvas = matplotlib.backends.backend_agg.FigureCanvasAgg(figure)
            ax = figure.add_subplot(1, 1, 1)
            self.display_grid(background_image, gridding,
                              workspace.measurements.image_set_number, ax)
            #
            # This is the recipe for just showing the axis
            #
            figure.set_frameon(False)
            ax.set_axis_off()
            figure.subplots_adjust(0, 0, 1, 1, 0, 0)
            ai = ax.images[0]
            shape = ai.get_size()
            dpi = figure.dpi
            width = float(shape[1]) / dpi
            height = float(shape[0]) / dpi
            figure.set_figheight(height)
            figure.set_figwidth(width)
            bbox = matplotlib.transforms.Bbox(
                    np.array([[0.0, 0.0], [width, height]]))
            transform = matplotlib.transforms.Affine2D(
                    np.array([[dpi, 0, 0],
                              [0, dpi, 0],
                              [0, 0, 1]]))
            figure.bbox = matplotlib.transforms.TransformedBbox(bbox, transform)
            image_pixels = figure_to_image(figure, dpi=dpi)
            image = cpi.Image(image_pixels)

            workspace.image_set.add(self.save_image_name.value, image)
Beispiel #26
0
def _create_line(plots, labels, plot_info):
    """\
    Given all the data for the metrics, create a line plot.

    plots: list of dicts containing the plot data. Each dict contains:
            x: list of x-values for the plot
            y: list of corresponding y-values
            errors: errors for each data point, or None if no error information
                    available
            label: plot title
    labels: list of x-tick labels
    plot_info: a MetricsPlot
    """
    # when we're doing any kind of normalization, all series get put into a
    # single plot
    single = bool(plot_info.normalize_to)

    area_data = []
    lines = []
    if single:
        plot_height = _SINGLE_PLOT_HEIGHT
    else:
        plot_height = _MULTIPLE_PLOT_HEIGHT_PER_PLOT * len(plots)
    figure, height = _create_figure(plot_height)

    if single:
        subplot = figure.add_subplot(1, 1, 1)

    # Plot all the data
    for plot_index, (plot, color) in enumerate(zip(plots,
                                                   _colors(len(plots)))):
        needs_invert = (plot['label'] in plot_info.inverted_series)

        # Add a new subplot, if user wants multiple subplots
        # Also handle axis inversion for subplots here
        if not single:
            subplot = figure.add_subplot(len(plots), 1, plot_index + 1)
            subplot.set_title(plot['label'])
            if needs_invert:
                # for separate plots, just invert the y-axis
                subplot.set_ylim(1, 0)
        elif needs_invert:
            # for a shared plot (normalized data), need to invert the y values
            # manually, since all plots share a y-axis
            plot['y'] = [-y for y in plot['y']]

        # Plot the series
        subplot.set_xticks(range(0, len(labels)))
        subplot.set_xlim(-1, len(labels))
        if single:
            lines += subplot.plot(plot['x'],
                                  plot['y'],
                                  label=plot['label'],
                                  marker=_MULTIPLE_PLOT_MARKER_TYPE,
                                  markersize=_MULTIPLE_PLOT_MARKER_SIZE)
            error_bar_color = lines[-1].get_color()
        else:
            lines += subplot.plot(plot['x'],
                                  plot['y'],
                                  _SINGLE_PLOT_STYLE,
                                  label=plot['label'])
            error_bar_color = _SINGLE_PLOT_ERROR_BAR_COLOR
        if plot['errors']:
            subplot.errorbar(plot['x'],
                             plot['y'],
                             linestyle='None',
                             yerr=plot['errors'],
                             color=error_bar_color)
        subplot.set_xticklabels([])

    # Construct the information for the drilldowns.
    # We need to do this in a separate loop so that all the data is in
    # matplotlib before we start calling transform(); otherwise, it will return
    # incorrect data because it hasn't finished adjusting axis limits.
    for line in lines:

        # Get the pixel coordinates of each point on the figure
        x = line.get_xdata()
        y = line.get_ydata()
        label = line.get_label()
        icoords = line.get_transform().transform(zip(x, y))

        # Get the appropriate drilldown query
        drill = plot_info.query_dict['__' + label + '__']

        # Set the title attributes (hover-over tool-tips)
        x_labels = [labels[x_val] for x_val in x]
        titles = [
            '%s - %s: %f' % (label, x_label, y_val)
            for x_label, y_val in zip(x_labels, y)
        ]

        # Get the appropriate parameters for the drilldown query
        params = [
            dict(query=drill, series=line.get_label(), param=x_label)
            for x_label in x_labels
        ]

        area_data += [
            dict(left=ix - 5,
                 top=height - iy - 5,
                 right=ix + 5,
                 bottom=height - iy + 5,
                 title=title,
                 callback=plot_info.drilldown_callback,
                 callback_arguments=param_dict)
            for (ix, iy), title, param_dict in zip(icoords, titles, params)
        ]

    subplot.set_xticklabels(labels, rotation=90, size=_LINE_XTICK_LABELS_SIZE)

    # Show the legend if there are not multiple subplots
    if single:
        font_properties = matplotlib.font_manager.FontProperties(
            size=_LEGEND_FONT_SIZE)
        legend = figure.legend(lines, [plot['label'] for plot in plots],
                               prop=font_properties,
                               handlelen=_LEGEND_HANDLE_LENGTH,
                               numpoints=_LEGEND_NUM_POINTS)
        # Workaround for matplotlib not keeping all line markers in the legend -
        # it seems if we don't do this, matplotlib won't keep all the line
        # markers in the legend.
        for line in legend.get_lines():
            line.set_marker(_LEGEND_MARKER_TYPE)

    return (figure, area_data)
Beispiel #27
0
    def run(self, workspace):
        """Run the module

        workspace    - The workspace contains
            pipeline     - instance of cpp for this run
            image_set    - the images in the image set being processed
            object_set   - the objects (labeled masks) in this image set
            measurements - the measurements for this run
            frame        - the parent frame to whatever frame is created. None means don't draw.
        """
        background_image = self.get_background_image(workspace, None)

        if (self.each_or_once == EO_ONCE
                and self.get_good_gridding(workspace) is not None):
            gridding = self.get_good_gridding(workspace)
        if self.auto_or_manual == AM_AUTOMATIC:
            gridding = self.run_automatic(workspace)
        elif self.manual_choice == MAN_COORDINATES:
            gridding = self.run_coordinates(workspace)
        elif self.manual_choice == MAN_MOUSE:
            gridding = workspace.interaction_request(
                self, background_image,
                workspace.measurements.image_set_number)
        self.set_good_gridding(workspace, gridding)
        workspace.set_grid(self.grid_image.value, gridding)
        #
        # Save measurements
        #
        self.add_measurement(
            workspace,
            F_X_LOCATION_OF_LOWEST_X_SPOT,
            gridding.x_location_of_lowest_x_spot,
        )
        self.add_measurement(
            workspace,
            F_Y_LOCATION_OF_LOWEST_Y_SPOT,
            gridding.y_location_of_lowest_y_spot,
        )
        self.add_measurement(workspace, F_ROWS, gridding.rows)
        self.add_measurement(workspace, F_COLUMNS, gridding.columns)
        self.add_measurement(workspace, F_X_SPACING, gridding.x_spacing)
        self.add_measurement(workspace, F_Y_SPACING, gridding.y_spacing)

        # update background image
        background_image = self.get_background_image(workspace, gridding)

        workspace.display_data.gridding = gridding.serialize()
        workspace.display_data.background_image = background_image
        workspace.display_data.image_set_number = (
            workspace.measurements.image_set_number)

        if self.wants_image:
            import matplotlib.transforms
            import matplotlib.figure
            import matplotlib.backends.backend_agg
            from cellprofiler.gui.tools import figure_to_image

            figure = matplotlib.figure.Figure()
            canvas = matplotlib.backends.backend_agg.FigureCanvasAgg(figure)
            ax = figure.add_subplot(1, 1, 1)
            self.display_grid(background_image, gridding,
                              workspace.measurements.image_set_number, ax)
            #
            # This is the recipe for just showing the axis
            #
            figure.set_frameon(False)
            ax.set_axis_off()
            figure.subplots_adjust(0, 0, 1, 1, 0, 0)
            ai = ax.images[0]
            shape = ai.get_size()
            dpi = figure.dpi
            width = float(shape[1]) / dpi
            height = float(shape[0]) / dpi
            figure.set_figheight(height)
            figure.set_figwidth(width)
            bbox = matplotlib.transforms.Bbox(
                np.array([[0.0, 0.0], [width, height]]))
            transform = matplotlib.transforms.Affine2D(
                np.array([[dpi, 0, 0], [0, dpi, 0], [0, 0, 1]]))
            figure.bbox = matplotlib.transforms.TransformedBbox(
                bbox, transform)
            image_pixels = figure_to_image(figure, dpi=dpi)
            image = cpi.Image(image_pixels)

            workspace.image_set.add(self.save_image_name.value, image)
Beispiel #28
0
                nsoil[i][j]=1 #maxima calidad 
                dead_counter=dead_counter+1
                    
    forest=nforest
    soil=nsoil
    
    
final_dist=distribution(forest,dif_sizes,ntrees,max_size)
    

#%% single plot. method 1
    
fig = plt.figure(figsize=(5,5), dpi=100 )


ax1 = fig.add_subplot(2,2,1)
ax2 = fig.add_subplot(2,2,4) 
ax1.set_title('Forest simulation')
ax2.set_title('Relative size frequency')
ax2.set_xlabel('Size intervals')
ax2.set_ylabel('Frequency')
ax2.set_ylim((0.0,1.0))
a = np.zeros((ntrees,ntrees))
x=np.linspace(0,1,num=dif_sizes+1)

# cuenta las divisiones que hay y las caracteriza auqnque el valor
# no tiene ningun significado, simplemente tenemos dif_sizes valores. Sirve, pero, para espaciar 
#el eje y 
array = ax1.imshow(forest , animated=False, cmap='plasma', vmin=0,vmax=max_size,  
                   extent=[0,ntrees,ntrees,0], aspect=1, origin= 'upper')
Beispiel #29
0
 def __init__(self):
     """Create an empty matplotlib figure with two subplots."""
     self.figure = figure = matplotlib.figure.Figure()
     self.axx = axx = figure.add_subplot(211)
     self.axy       = figure.add_subplot(212, sharex=axx)
Beispiel #30
0
def main():

    src_path_map = '../data/map/wean.dat'
    src_path_log = '../data/log/robotdata5.log'

    logfile = open(src_path_log, 'r')

    first_time_idx = True
    for time_idx, line in enumerate(logfile):
        # time_idx is just a counter
        # line is the text from a line in the file.

        # Read a single 'line' from the log file (can be either odometry or laser measurement)
        meas_type = line[
            0]  # L : laser scan measurement, O : odometry measurement
        meas_vals = np.fromstring(
            line[2:], dtype=np.float64,
            sep=' ')  # convert measurement values from string to double

        state = meas_vals[
            0:3]  # odometry reading [x, y, theta] in odometry frame
        time_stamp = meas_vals[-1]

        if (meas_type == "L"):  # Laser data
            odometry_laser = meas_vals[
                3:6]  # [x, y, theta] coordinates of laser in odometry frame
            ranges = meas_vals[
                6:-1]  # 180 range measurement values from single laser scan
            ranges[ranges > 2500] = 0
        else:
            #print("Skipping this record because it is an odometry record.")
            continue

        # convert the ranges to [X,Y]
        X = np.zeros(180)
        Y = np.zeros(180)
        for I in range(180):
            #lidarPoints[I,:] = ranges[I] * np.array[math.cos(float(I)/180), math.sin(float(I/180)) ]
            angleInRadians = (float(I) / 180) * math.pi
            X[I] = ranges[I] * math.cos(angleInRadians)
            Y[I] = ranges[I] * math.sin(angleInRadians)

        # plot the points

        print("Time: %f" % time_stamp)
        # plt.scatter(X,Y,color='r',marker='.', s = 10);
        # plt.axis('equal')
        # plt.show(block=False)

        # #plt.show()
        # time.sleep(1)
        # plt.close()

        fig = plt.figure(1)
        ax = fig.add_subplot(111)

        ax.scatter(X, Y, color='r', marker='.', s=10)
        ax.axis('equal')
        fig.canvas.draw()
        plt.show(block=False)
        time.sleep(.1)
        ax.clear()
Beispiel #31
0
def main():
    """
    Plot the ISMIP-HOM test results.
    """

    if args.all_ps:
        nonFSmodelType = 'All Partial Stokes'
    else:
        nonFSmodelType = 'First Order'
    print 'NOTE: The category being used for models approximating Full Stokes is: ' + nonFSmodelType
    print 'For more information, see details of option -a by invoking:   python plotISMIP_HOM.py --help \n'

    # =========================================================
    #  First generate the standard ascii files defined by ISMIP-HOM
    #  (This used to be in the run script, but is more useful and natural here.)
    #  Note: It is not strictly necessary to generate these files
    #  just to plot the results, but this approach is left in
    #  to use existing code, and on the off-chance it is ever
    #  necessary to generate these standard ascii ISMIP-HOM files...
    # =========================================================

    pattern = get_file_pattern()

    experiments, sizes = get_experiments_and_sizes(pattern)

    if args.sizes and (set(args.sizes) <= set(map(int, sizes))):
        sizes = args.sizes
    elif args.sizes:
        print("ERROR: No output files found for that size!")
        print(
            "   Run `ls " + pattern.replace('-?', '-[a-e]') +
            "`\non the command line to see the set of output files. (Note, test f's size is ignored.)"
        )
        sys.exit(1)

    # sort experiments and sizes
    sizes.sort()
    experiments.sort()

    # Loop over the experiments
    for experiment in experiments:

        # Loop over the sizes
        for size in map(int, sizes):
            try:
                # Extract the output data for comparison to the other models

                # NOTE: The script now assumes that uvel_extend and vvel_extend are ALWAYS present.
                # Those fields contain the ice velocity computed at the upper right corner of each grid cell.
                # They appear to be on the x1,y1 grid in their metadata but are actually on the x0,y0 grid.
                # The additional row/column include the first halo value past ewn/nsn.
                # That value is valid at both 0.0 and 1.0 on the non-dimensional coordinate system.
                # Matrix manipulations for each test case below are done to create a larger matrix that goes from 0.0 to 1.0, inclusive.
                # NOTE: The cases below are only writing [x,y,u,v] to the text file.  This is the minimum needed to compare to other models.
                # In the future, the other additional fields specified in section 4 of http://homepages.ulb.ac.be/~fpattyn/ismip/ismiphom.pdf
                # can be added.  wvel and the stresses are on the x1,y1 grid, so they would need to be interpolated to the x0,y0 grid
                # since we are using that as the coordinate system in the text files.

                # Open the netCDF file that was written by CISM

                # Standard filename format used by both scripts
                if experiment == 'f':
                    size = 100

                out_file = pattern.replace('-?', '-' + experiment).replace(
                    '????',
                    str(size).zfill(4))
                netCDFfile = NetCDFFile(out_file, 'r')
                if netCDF_module == 'Scientific.IO.NetCDF':
                    velscale = netCDFfile.variables['uvel_extend'].scale_factor
                else:
                    velscale = 1.0

                if experiment in [
                        'f',
                ]:
                    # Convert CISM output data to the rotated coord system used by the problem setup
                    alpha = -3.0 * pi / 180  # defined in run script
                    if netCDF_module == 'Scientific.IO.NetCDF':
                        thkscale = netCDFfile.variables['thk'].scale_factor
                        wvelscale = netCDFfile.variables[
                            'wvel_ho'].scale_factor
                    else:
                        thkscale = 1.0
                        wvelscale = 1.0

                    usurf = netCDFfile.variables['usurf'][
                        -1, :, :] * thkscale  # get last time level
                    usurfStag = (usurf[1:, 1:] + usurf[1:, :-1] +
                                 usurf[:-1, :-1] + usurf[:-1, :-1]) / 4.0
                    uvelS = netCDFfile.variables['uvel'][
                        -1, 0, :, :] * velscale  # top level of last time
                    vvelS = netCDFfile.variables['vvel'][
                        -1, 0, :, :] * velscale  # top level of last time
                    wvelS = netCDFfile.variables['wvel_ho'][
                        -1, 0, :, :] * wvelscale  # top level of last time
                    wvelStag = (wvelS[1:, 1:] + wvelS[1:, :-1] +
                                wvelS[:-1, :-1] + wvelS[:-1, :-1]) / 4.0
                    x0 = netCDFfile.variables['x0'][:]
                    y0 = netCDFfile.variables['y0'][:]
                    # calculate rotated xprime coordinates along the surface - xx, yy are used by code below to write the output
                    xx = x0 * cos(alpha) + (usurfStag[20, :] -
                                            7000.0) * sin(alpha)
                    xx = xx / 1000.0 - 50.0
                    yy = y0 / 1000.0 - 50.0
                    # calculate rotated uvel/vvel at surface
                    uvelSprime = uvelS[:, :] * cos(
                        alpha) + wvelStag[:, :] * sin(alpha)
                    wvelSprime = -uvelS[:, :] * sin(
                        alpha) + wvelStag[:, :] * cos(alpha)

                    nan = np.ones(
                        uvelSprime.shape
                    ) * -999.0  # create a dummy matrix for uncalculated values.

                    # ===========================================
                    # optional bit of code to plot out vertical velocity on the rotated grid.
                    # This can be compared to Fig. 14b in the tc-2007-0019-sp3.pdf document
                    figure = pyplot.figure(subplotpars=matplotlib.figure.
                                           SubplotParams(top=.85, bottom=.15))
                    axes = figure.add_subplot(111)
                    pc = axes.pcolor(wvelSprime)
                    pyplot.colorbar(pc)
                    cntr = axes.contour(wvelSprime, [
                        -0.5, -0.4, -0.3, -0.2, -0.1, 0.0, 0.1, 0.2, 0.3, 0.4
                    ],
                                        colors='k')
                    axes.clabel(cntr)
                    pyplot.title('Compare to Fig. 14b of tc-2007-0019-sp3.pdf')
                    pyplot.draw()
                    pyplot.show()
                    # ===========================================

                else:  # all other tests
                    # Make x,y position arrays that can be used by all test cases.
                    #   Want x/y positions to include the periodic edge at both the beginning and end
                    xx = netCDFfile.variables['x0'][:] / (1000.0 * float(size))
                    xx = np.concatenate(([0.0], xx, [1.0]))
                    yy = netCDFfile.variables['y0'][:] / (1000.0 * float(size))
                    yy = np.concatenate(([0.0], yy, [1.0]))
                    if experiment in ('b', 'd'):
                        yy = (
                            yy[len(yy) / 2],
                        )  # for the 2-d experiments, just use the middle y-index

                    # Figure out u,v since all experiments needs at least one of them (avoids duplicate code in each case below
                    #   Want to use last time level.  Most experiments should only have a single time level, but F may have many in the file.
                    #   Apparently some older versions of netCDF4 give an error when using the -1 dimension if the size is 1, hence this bit of seemingly unnecessary logic...
                    if netCDFfile.variables['uvel_extend'][:].shape[0] == 1:
                        t = 0
                    else:
                        t = -1
                    us = netCDFfile.variables['uvel_extend'][
                        t, 0, :, :] * velscale  # top level of last time
                    us = np.concatenate(
                        (us[:, -1:], us),
                        axis=1)  # copy the column at x=1.0 to x=0.0
                    us = np.concatenate(
                        (us[-1:, :], us),
                        axis=0)  # copy the row at y=1.0 to y=0.0
                    vs = netCDFfile.variables['vvel_extend'][
                        t, 0, :, :] * velscale  # top level of last time
                    vs = np.concatenate(
                        (vs[:, -1:], vs),
                        axis=1)  # copy the column at x=1.0 to x=0.0
                    vs = np.concatenate(
                        (vs[-1:, :], vs),
                        axis=0)  # copy the row at y=1.0 to y=0.0
                    ub = netCDFfile.variables['uvel_extend'][
                        t, -1, :, :] * velscale  # bottom level of last time
                    ub = np.concatenate(
                        (ub[:, -1:], ub),
                        axis=1)  # copy the column at x=1.0 to x=0.0
                    ub = np.concatenate(
                        (ub[-1:, :], ub),
                        axis=0)  # copy the row at y=1.0 to y=0.0
                    vb = netCDFfile.variables['vvel_extend'][
                        t, -1, :, :] * velscale  # bottom level of last time
                    vb = np.concatenate(
                        (vb[:, -1:], vb),
                        axis=1)  # copy the column at x=1.0 to x=0.0
                    vb = np.concatenate(
                        (vb[-1:, :], vb),
                        axis=0)  # copy the row at y=1.0 to y=0.0
                    #nan = ub*np.NaN  # create a dummy matrix for uncalculated values.
                    nan = np.ones(
                        ub.shape
                    ) * -999.0  # create a dummy matrix for uncalculated values.

                # make arrays of the variables needed for each experiment
                # the extended-grid velocities have the periodic edge in the last x-position.  We also want it in the first x-position.
                # After building the 2-d array as needed for each variable from the raw file data, then build a list called 'data'.
                if experiment == 'a':
                    #  This is supposed to be: [('uvel',0),('vvel',0),('wvel',0),('tau_xz',-1),('tau_yz',-1),[deltap]]
                    data = (us, vs, nan, nan, nan, nan)
                elif experiment == 'b':
                    #  This is supposed to be: uvel(0), wvel(0), tau_xz(-1), deltaP
                    data = (us, nan, nan, nan)
                elif experiment == 'c':
                    #  This is supposed to be: [uvel',0),('vvel',0),('wvel',0),('uvel',-1),('vvel',-1),('tau_xz',-1),('tau_yz',-1), deltap]
                    data = (us, vs, nan, ub, vb, nan, nan, nan)
                elif experiment == 'd':
                    #  This is supposed to be:  [('uvel',0),('wvel',0),('uvel',-1),('tau_xz',-1), deltap]
                    data = (us, nan, nan, nan, nan)
                elif experiment == 'f':  # should be: x, y, zs, vx, vy, vz
                    #  This is supposed to be: [('usurf',None),('uvel',0),('vvel',0),('wvel',0)]
                    data = (nan, uvelSprime, vvelS, nan)

                # Write a "standard" ISMIP-HOM file (example file name: "cis1a020.txt") in the "output" subdirectory
                ISMIP_HOMfilename = out_file.replace('.out.nc', '.txt')
                ISMIP_HOMfile = open(ISMIP_HOMfilename, 'w')
                for i, x in enumerate(xx):
                    for j, y in enumerate(yy):
                        if experiment in ('a', 'c',
                                          'f'):  # include x and y positions
                            ISMIP_HOMfile.write('\t'.join(
                                map(str, [x, y] + [v[j, i]
                                                   for (v) in data])) + '\n')
                        else:  # only include x position
                            ISMIP_HOMfile.write('\t'.join(
                                map(str, [x] + [v[j, i]
                                                for (v) in data])) + '\n')
                ISMIP_HOMfile.close()
                netCDFfile.close()
            except:
                print 'Warning: The CISM output file for experiment ' + experiment + ' at size ' + str(
                    size) + ' could NOT be read successfully!'
                raise

    # =========================================================
    #  Now actually analyze the results.
    # =========================================================

    # Loop over the experiments requested on the command line
    for experiment in experiments:
        print 'ISMIP-HOM', experiment.upper()

        # Create the figure on which the plot axes will be placed
        figure = pyplot.figure(
            subplotpars=matplotlib.figure.SubplotParams(top=.85, bottom=.15))
        figure.text(0.5,
                    0.92,
                    'ISMIP-HOM Experiment ' + experiment.upper(),
                    horizontalalignment='center',
                    size='large')
        if args.subtitle:
            figure.text(0.5,
                        0.89,
                        args.subtitle,
                        horizontalalignment='center',
                        size='small')
        figure.text(0.5,
                    0.1,
                    'Normalized X coordinate',
                    horizontalalignment='center',
                    size='small')
        figure.text(0.06,
                    0.5,
                    'Ice Speed (m/a)',
                    rotation='vertical',
                    verticalalignment='center')
        # Create the (three column) legend
        prop = matplotlib.font_manager.FontProperties(size='x-small')
        Line2D = matplotlib.lines.Line2D([], [], color=(0, 0, 0))
        figure.legend([Line2D], ['Model Output'], loc=(0.1, 0.05),
                      prop=prop).draw_frame(False)
        Line2D.set_linestyle(':')
        Line2D.set_color((1, 0, 0))
        Patch = matplotlib.patches.Patch(edgecolor=None,
                                         facecolor=(1, 0, 0),
                                         alpha=0.25)
        figure.legend([Line2D, Patch],
                      ['Full Stokes Mean', 'Full Stokes Std. Dev.'],
                      loc=(0.3, 0.02),
                      prop=prop).draw_frame(False)
        Line2D.set_color((0, 0, 1))
        Patch.set_facecolor((0, 0, 1))
        figure.legend(
            [Line2D, Patch],
            [nonFSmodelType + ' Mean', nonFSmodelType + ' Std. Dev.'],
            loc=(0.55, 0.02),
            prop=prop).draw_frame(False)

        # Loop over the sizes requested on the command line
        for i, size in enumerate(map(int, sizes)):
            try:
                if experiment == 'f':
                    if size != 100 or len(sizes) > 1:
                        print 'NOTE: Experiment f uses a domain size of 100 km only'
                    size = 100

                out_file = pattern.replace('-?', '-' + experiment).replace(
                    '????',
                    str(size).zfill(4))

                # Create the plot axes for this domain size
                if len(sizes) == 1:
                    axes = figure.add_subplot(111)
                else:
                    axes = figure.add_subplot(2, 3, i + 1)
                    for tick in axes.xaxis.get_major_ticks():
                        tick.label1.set_fontsize('xx-small')
                    for tick in axes.yaxis.get_major_ticks():
                        tick.label1.set_fontsize('xx-small')
                axes.set_title('%d km' % size, size='medium')

                # Get the Glimmer output data
                glimmerData = read(out_file.replace('.out.nc', '.txt'),
                                   experiment)
                # The Glimmer data is on a staggered grid;
                # Interpolate to obtain the value at x=0 and x=1
                # using periodic boundary conditions
                #v = (glimmerData[0][1] + glimmerData[-1][1])/2
                #glimmerData = [(0.0,v)]+glimmerData+[(1.0,v)]

                # Plot the Glimmer data
                axes.plot([row[0] for row in glimmerData],
                          [row[1] for row in glimmerData],
                          color='black')

                # Get the data from other models for comparison
                firstOrder = 0
                fullStokes = 1
                count = [0, 0]
                sumV = [[0.0 for v in glimmerData], [0.0 for v in glimmerData]]
                sumV2 = [[0.0 for v in glimmerData],
                         [0.0 for v in glimmerData]]
                for (path, directories, filenames) in os.walk('ismip_all'):
                    for filename in filenames:
                        modelName = filename[0:4]
                        modelExperiment = filename[4]
                        modelSize = filename[5:8]
                        #print 'name, exp, size', modelName, modelExperiment, modelSize
                        if modelName == 'aas1':
                            # Skip the 'aas1' model because its output files in the tc-2007-0019-sp2.zip file do not follow the proper naming convention.  MJH 11/5/13
                            continue
                        if (modelExperiment != experiment) or (modelExperiment != 'f' and int(modelSize) != size) \
                        or (not args.all_ps and not modelName in lmlaModels + fullStokesModels):
                            continue  # continue next loop iteration if not the size or not the experiment desired or if we just want FO comparison and this model is not FO or FS.
                        elif (modelExperiment == 'f'):
                            if (modelSize == '001'):
                                continue  # ignore the sliding version for now
                            if modelName == 'cma1':
                                continue  # the cma1 'f' experiments made the x,y coords dimensionless instead of dimensional - ignore for convenience
                        print 'Using data from file:', os.path.join(
                            path, filename)
                        data = read(os.path.join(path, filename), experiment)
                        if modelName in fullStokesModels:
                            index = fullStokes
                        else:
                            index = firstOrder
                        count[index] += 1

                        #axes.plot([row[0] for row in data], [row[1] for row in data] )   ## OPTIONAL: print out every individual model in its native x-coordinates.

                        # Interpolate onto the x values from the Glimmer model run
                        for (i, target) in enumerate(
                            [row[0] for row in glimmerData]):
                            below = -99999.0
                            above = 99999.0
                            for (j, x) in enumerate([row[0] for row in data]):
                                if below < x <= target: b, below = j, x
                                if target <= x < above: a, above = j, x
                            if above == below:
                                v = data[a][1]
                            else:
                                if below == -99999.0:  # Use the periodic boundary condition at x = 0
                                    xBelow = data[-1][0] - 1
                                    vBelow = data[-1][1]
                                else:
                                    xBelow, vBelow = data[b]

                                if above == 99999.0:  # Use the periodic boundary condition at x = 1
                                    xAbove = data[0][0] + 1
                                    vAbove = data[0][1]
                                else:
                                    xAbove, vAbove = data[a]

                                if xAbove == xBelow:
                                    print 'Surprise!', above, below, xAbove, xBelow, vAbove, vBelow
                                    v = (vAbove + vBelow) / 2
                                else:
                                    alpha = (target - xBelow) / (xAbove -
                                                                 xBelow)
                                    v = alpha * vAbove + (1 - alpha) * vBelow
                            sumV[index][i] += v
                            sumV2[index][i] += v * v

                # Calculate statistics of the other model results
                if sum(count) == 0:
                    print 'To compare with other models you need to download the ISMIP-HOM results from: http://www.the-cryosphere.net/2/95/2008/tc-2-95-2008-supplement.zip and unzip the contained file tc-2007-0019-sp2.zip into a directory named ismip_all.  The ismip_all directory must be in the directory from which you are running this script.'
                else:
                    # Find the mean and standard deviation of the velocities at each x
                    for index in (firstOrder, fullStokes):
                        if count[index] == 0:
                            continue
                        mean = list()
                        standardDeviation = list()
                        for i in range(len(glimmerData)):
                            mean.append(sumV[index][i] / count[index])
                            standardDeviation.append(
                                sqrt(sumV2[index][i] / count[index] -
                                     mean[-1]**2))

                        # Plot the mean using a dotted line
                        color = (
                            index, 0, 1 - index
                        )  # blue for first order (index=0); red for full Stokes (index=1)
                        x = [row[0] for row in glimmerData]
                        axes.plot(x, mean, ':', color=color)

                        # Plot a filled polygon showing the mean plus and minus one standard deviation
                        meanMinusSD = [
                            m - sd for (m, sd) in zip(mean, standardDeviation)
                        ]
                        meanPlusSD = [
                            m + sd for (m, sd) in zip(mean, standardDeviation)
                        ]
                        x = x + list(reversed(x))
                        y = meanPlusSD + list(reversed(meanMinusSD))
                        axes.fill(x,
                                  y,
                                  facecolor=color,
                                  edgecolor=color,
                                  alpha=0.25)

                        if index == firstOrder:
                            # Calculate some statistics comparing the Glimmer data with the other models
                            pcterror = [
                                100.0 * abs(glimmer - others) / others
                                for (glimmer, others
                                     ) in zip([row[1]
                                               for row in glimmerData], mean)
                            ]
                            abserror = [
                                abs(glimmer - others)
                                for (glimmer, others
                                     ) in zip([row[1]
                                               for row in glimmerData], mean)
                            ]
                            maximum = max(pcterror)
                            position = glimmerData[pcterror.index(maximum)][0]
                            total = sum([e for e in pcterror])
                            compare = sum([
                                (s / m)
                                for (s, m) in zip(standardDeviation, mean)
                            ])
                            n = len(glimmerData)
                            #print '\t'.join([str(size)+' km',str(total/n),str(compare/n),str(position)])
                            print 'Size=' + str(size) + ' km'
                            print '  Mean percent error along flowline of CISM relative to mean of first-order models=' + str(
                                total / float(n)) + '%'
                            print '  Mean COD (stdev/mean) along flowline of mean of first-order models (excluding CISM)=' + str(
                                compare / float(n) * 100.0) + '%'
                            print '  Max. CISM percent error=' + str(
                                maximum) + '% at x-position ' + str(position)
                            print '  Max. CISM absolute error=' + str(
                                max(abserror)) + ' m/yr at x-position ' + str(
                                    glimmerData[abserror.index(
                                        max(abserror))][0])

            except:
                print "Error in analyzing/plotting experiment ", experiment, " at size ", size, " km"

        if savePlotInFile:
            plot_file = pattern.replace('-?', '-' + experiment).replace(
                '.????', '').replace('.out.nc', plotType)
            print 'Writing:', plot_file
            pyplot.savefig(plot_file)

        # Experiment f can also have a surface profile plotted
        if experiment == 'f':
            # rather than getting the data from the text file, we are going to read it directly.
            # this is because the velocities and usrf are on different grids, so it is difficult to include them
            # both in the standard ISMIP-HOM text file format that has a single x,y coord. system
            size = 100
            out_file = pattern.replace('-?', '-' + experiment).replace(
                '????',
                str(size).zfill(4))
            netCDFfile = NetCDFFile(out_file, 'r')
            if netCDF_module == 'Scientific.IO.NetCDF':
                thkscale = netCDFfile.variables['thk'].scale_factor
            else:
                thkscale = 1.0
            usurf = netCDFfile.variables['usurf'][
                -1, :, :] * thkscale  # get last time level
            x1 = netCDFfile.variables['x1'][:]
            y1 = netCDFfile.variables['y1'][:]

            #  Create the usurf figure
            ufigure = pyplot.figure(
                subplotpars=matplotlib.figure.SubplotParams(top=.85,
                                                            bottom=.15))
            ufigure.text(0.5,
                         0.92,
                         'ISMIP-HOM Experiment F: Surface elevation',
                         horizontalalignment='center',
                         size='large')
            if args.subtitle:
                ufigure.text(0.5,
                             0.89,
                             args.subtitle,
                             horizontalalignment='center',
                             size='small')
            ufigure.text(0.5,
                         0.1,
                         'X coordinate',
                         horizontalalignment='center',
                         size='small')
            ufigure.text(0.06,
                         0.5,
                         'upper surface (m)',
                         rotation='vertical',
                         verticalalignment='center')
            # Create the (three column) legend
            prop = matplotlib.font_manager.FontProperties(size='x-small')
            Line2D = matplotlib.lines.Line2D([], [], color=(0, 0, 0))
            ufigure.legend([Line2D], ['Model Output'],
                           loc=(0.1, 0.05),
                           prop=prop).draw_frame(False)
            Line2D.set_linestyle(':')
            Line2D.set_color((1, 0, 0))
            Patch = matplotlib.patches.Patch(edgecolor=None,
                                             facecolor=(1, 0, 0),
                                             alpha=0.25)
            ufigure.legend([Line2D, Patch],
                           ['Full Stokes Mean', 'Full Stokes Std. Dev.'],
                           loc=(0.3, 0.02),
                           prop=prop).draw_frame(False)
            Line2D.set_color((0, 0, 1))
            Patch.set_facecolor((0, 0, 1))
            ufigure.legend(
                [Line2D, Patch],
                [nonFSmodelType + ' Mean', nonFSmodelType + ' Std. Dev.'],
                loc=(0.55, 0.02),
                prop=prop).draw_frame(False)
            # Create the plot axes
            axes2 = ufigure.add_subplot(111)
            axes2.set_title('%d km' % size, size='medium')

            # Convert CISM output data to the rotated coord system used by the problem setup
            alpha = -3.0 * pi / 180  # defined in run script
            # use integer floor division operator to get an index close to the center  TODO should be interpolating if needed...
            yp = len(y1) // 2
            # calculate rotated xprime, zprime coordinates along the surface (this is the coord. sys. used for this test case)
            xprime = x1 * cos(alpha) + (usurf[yp, :] - 7000.0) * sin(alpha)
            xprime = xprime / 1000.0 - 50.0
            zprime = -x1 * sin(alpha) + (usurf[yp, :] - 7000.0) * cos(alpha)
            # Plot CISM output
            axes2.plot(xprime, zprime, color='black')

            # create glimmerData so we can re-use the code from above
            glimmerData = list()
            for i in range(len(xprime)):
                glimmerData.append(tuple([xprime[i], zprime[i]]))

            # Now plot the other models - yucky code copied from above
            # Get the data from other models for comparison
            firstOrder = 0
            fullStokes = 1
            count = [0, 0]
            sumV = [[0.0 for v in glimmerData], [0.0 for v in glimmerData]]
            sumV2 = [[0.0 for v in glimmerData], [0.0 for v in glimmerData]]
            for (path, directories, filenames) in os.walk('ismip_all'):
                for filename in filenames:
                    modelName = filename[0:4]
                    modelExperiment = filename[4]
                    modelSize = filename[5:8]
                    #print 'name, exp, size', modelName, modelExperiment, modelSize
                    if modelName == 'aas1':
                        # Skip the 'aas1' model because its output files in the tc-2007-0019-sp2.zip file do not follow the proper naming convention.  MJH 11/5/13
                        continue
                    if (modelExperiment != experiment) or (modelExperiment != 'f' and int(modelSize) != size) \
                    or (not args.all_ps and not modelName in lmlaModels + fullStokesModels):
                        continue  # continue next loop iteration if not the size or not the experiment desired or if we just want FO comparison and this model is not FO or FS.
                    elif (modelExperiment == 'f'):
                        if (modelSize == '001'):
                            continue  # ignore the sliding version for now
                        if modelName == 'cma1':
                            continue  # the cma1 'f' experiments made the x,y coords dimensionless instead of dimensional - ignore for convenience
                    print 'Using data from file:', os.path.join(path, filename)
                    data = read(os.path.join(path, filename),
                                experiment='f-elevation')
                    if modelName in fullStokesModels:
                        index = fullStokes
                    else:
                        index = firstOrder
                    count[index] += 1

                    #axes2.plot([row[0] for row in data], [row[1] for row in data] )   ## OPTIONAL: print out every individual model in its native x-coordinates.

                    # Interpolate onto the x values from the Glimmer model run
                    for (i,
                         target) in enumerate([row[0] for row in glimmerData]):
                        below = -99999.0
                        above = 99999.0
                        for (j, x) in enumerate([row[0] for row in data]):
                            if below < x <= target: b, below = j, x
                            if target <= x < above: a, above = j, x
                        if above == below:
                            v = data[a][1]
                        else:
                            if below == -99999.0:  # Use the periodic boundary condition at x = 0
                                xBelow = data[-1][0] - 1
                                vBelow = data[-1][1]
                            else:
                                xBelow, vBelow = data[b]
                            if above == 99999.0:  # Use the periodic boundary condition at x = 1
                                xAbove = data[0][0] + 1
                                vAbove = data[0][1]
                            else:
                                xAbove, vAbove = data[a]
                            if xAbove == xBelow:
                                print 'Surprise!', above, below, xAbove, xBelow, vAbove, vBelow
                                v = (vAbove + vBelow) / 2
                            else:
                                alpha = (target - xBelow) / (xAbove - xBelow)
                                v = alpha * vAbove + (1 - alpha) * vBelow
                        sumV[index][i] += v
                        sumV2[index][i] += v * v

                    # Calculate statistics of the other model results
                    if sum(count) == 0:
                        print 'To compare with other models you need to download the ISMIP-HOM results from: http://www.the-cryosphere.net/2/95/2008/tc-2-95-2008-supplement.zip and unzip the contained file tc-2007-0019-sp2.zip into a directory named ismip_all.  The ismip_all directory must be in the directory from which you are running this script.'
                    else:
                        # Find the mean and standard deviation of the velocities at each x
                        for index in (firstOrder, fullStokes):
                            if count[index] == 0:
                                continue
                            mean = list()
                            standardDeviation = list()
                            for i in range(len(glimmerData)):
                                mean.append(sumV[index][i] / count[index])
                                standardDeviation.append(
                                    sqrt(sumV2[index][i] / count[index] -
                                         mean[-1]**2))

                            # Plot the mean using a dotted line
                            color = (
                                index, 0, 1 - index
                            )  # blue for first order (index=0); red for full Stokes (index=1)
                            x = [row[0] for row in glimmerData]
                            axes2.plot(x, mean, ':', color=color)

                            # Plot a filled polygon showing the mean plus and minus one standard deviation
                            meanMinusSD = [
                                m - sd
                                for (m, sd) in zip(mean, standardDeviation)
                            ]
                            meanPlusSD = [
                                m + sd
                                for (m, sd) in zip(mean, standardDeviation)
                            ]
                            x = x + list(reversed(x))
                            y = meanPlusSD + list(reversed(meanMinusSD))
                            axes2.fill(x,
                                       y,
                                       facecolor=color,
                                       edgecolor=color,
                                       alpha=0.25)

            if savePlotInFile:
                plot_file = pattern.replace(
                    '-?', '-' + experiment + '-SurfaceElevation').replace(
                        '.????', '').replace('.out.nc', plotType)
                print 'Writing:', plot_file
                pyplot.savefig(plot_file)

        # Experiment f should be run for one size (100 km) only
        if experiment == 'f':
            break

    if not savePlotInFile:
        pyplot.show()
Beispiel #32
0
def _setup_axes(figure, region):
    def get_path(r):
        return [] if not r else get_path(r.parent) + [r]

    region_path = get_path(region)
    (a0_region, ) = region_path[1:2] or [None]
    (a1_region, ) = region_path[2:3] or [None]

    a0_region_shapes = [
        s for s in _admin_0_shapes
        if a0_region and s.attributes["ISO_A2"] == a0_region.iso_code
    ]
    a0_region_a1_shapes = [
        s for s in _admin_1_shapes
        if a0_region and s.attributes["iso_a2"] == a0_region.iso_code
    ]
    a1_region_shapes = [
        s for s in _admin_1_shapes
        if a1_region and s.attributes["iso_a2"] == a0_region.iso_code and
        (s.attributes["fips"] == "US{a1_region.fips_code:02}"
         or s.attributes["name"] == a1_region.name or s.attributes["name"] ==
         a1_region.short_name or s.attributes["abbrev"].strip(".") == a1_region
         .short_name or s.attributes["postal"] == a1_region.short_name)
    ]

    if region.name == "World":
        # Special projection for the whole world
        axes = figure.add_subplot(projection=cartopy.crs.Robinson())
        axes.set_box_aspect(0.505)
        axes.set_extent((-179, 179, -89, 89), _lat_lon_crs)
    else:
        m = lambda g: isinstance(g, BaseMultipartGeometry)
        split = lambda g: (p for s in g for p in split(s)) if m(g) else (g, )
        area = lambda g: _area_crs.project_geometry(g, _lat_lon_crs).area
        region_shapes = a1_region_shapes or a0_region_shapes
        if not region_shapes:
            raise LookupError(f"No shapes for a0={a0_region.name} "
                              f"a1={a1_region.name if a1_region else 'N/A'}")

        region_shape_parts = ((s.geometry for s in region_shapes)
                              if region.fips_code == 15  # Hawaii
                              else (p for s in region_shapes
                                    for p in split(s.geometry)))

        main_area, main = max((area(p), p) for p in region_shape_parts)
        ((center_lon, center_lat), ) = main.centroid.coords
        axes = figure.add_subplot(projection=cartopy.crs.Orthographic(
            central_longitude=center_lon, central_latitude=center_lat))

        main_projected = axes.projection.project_geometry(main, _lat_lon_crs)
        x1, y1, x2, y2 = main_projected.bounds
        xp, yp = (x2 - x1) / 10, (y2 - y1) / 10
        axes.set_extent((x1 - xp, x2 + xp, y1 - yp, y2 + yp), axes.projection)
        axes.set_box_aspect(0.618)

    def add_shapes(shapes, **kwargs):
        axes.add_geometries(
            (s.geometry for s in shapes),
            **{
                "crs": _lat_lon_crs,
                "ec": "black",
                "fc": "none",
                **kwargs
            },
        )

    add_shapes(_water_shapes, ec="none", fc="0.9")
    add_shapes(_admin_0_shapes, lw=0.5)
    add_shapes(a0_region_a1_shapes, lw=0.5)
    add_shapes(a1_region_shapes or a0_region_shapes, lw=1)

    L2D = matplotlib.lines.Line2D
    axes.legend(
        loc="lower left",
        handles=[
            L2D(
                [],
                [],
                color=(0.0, 0.0, 0.0, 0.1),
                ls="none",
                marker="o",
                ms=12,
                label="population",
            ),
            L2D(
                [],
                [],
                mfc=(0.0, 0.0, 1.0, 0.2),
                mec=(0.0, 0.0, 1.0, 0.6),
                mew=2,
                ls="none",
                marker="o",
                ms=11,
                label="pos x2K (incr.)",
            ),
            L2D(
                [],
                [],
                mfc=(0.0, 0.0, 1.0, 0.2),
                mec=(0.0, 1.0, 0.0, 0.6),
                mew=2,
                ls="none",
                marker="o",
                ms=11,
                label="pos x2K (decr.)",
            ),
            L2D(
                [],
                [],
                color=(1.0, 0.0, 0.0, 0.2),
                mec=(1.0, 0.0, 0.0, 0.6),
                mew=2,
                ls="none",
                marker="o",
                ms=11,
                label="deaths x200K",
            ),
        ],
    )

    return axes
Beispiel #33
0
def _create_qual_histogram_helper(plot_info, extra_text=None):
    """\
    Create a machine qualification histogram of the given data.

    plot_info: a QualificationHistogram
    extra_text: text to show at the upper-left of the graph

    TODO(showard): move much or all of this into methods on
    QualificationHistogram
    """
    cursor = readonly_connection.cursor()
    cursor.execute(plot_info.query)

    if not cursor.rowcount:
        raise NoDataError('query did not return any data')

    # Lists to store the plot data.
    # hist_data store tuples of (hostname, pass_rate) for machines that have
    #     pass rates between 0 and 100%, exclusive.
    # no_tests is a list of machines that have run none of the selected tests
    # no_pass is a list of machines with 0% pass rate
    # perfect is a list of machines with a 100% pass rate
    hist_data = []
    no_tests = []
    no_pass = []
    perfect = []

    # Construct the lists of data to plot
    for hostname, total, good in cursor.fetchall():
        if total == 0:
            no_tests.append(hostname)
            continue

        if good == 0:
            no_pass.append(hostname)
        elif good == total:
            perfect.append(hostname)
        else:
            percentage = 100.0 * good / total
            hist_data.append((hostname, percentage))

    interval = plot_info.interval
    bins = range(0, 100, interval)
    if bins[-1] != 100:
        bins.append(bins[-1] + interval)

    figure, height = _create_figure(_SINGLE_PLOT_HEIGHT)
    subplot = figure.add_subplot(1, 1, 1)

    # Plot the data and get all the bars plotted
    _, _, bars = subplot.hist([data[1] for data in hist_data],
                              bins=bins,
                              align='left')
    bars += subplot.bar([-interval],
                        len(no_pass),
                        width=interval,
                        align='center')
    bars += subplot.bar([bins[-1]],
                        len(perfect),
                        width=interval,
                        align='center')
    bars += subplot.bar([-3 * interval],
                        len(no_tests),
                        width=interval,
                        align='center')

    buckets = [(bin, min(bin + interval, 100)) for bin in bins[:-1]]
    # set the x-axis range to cover all the normal bins plus the three "special"
    # ones - N/A (3 intervals left), 0% (1 interval left) ,and 100% (far right)
    subplot.set_xlim(-4 * interval, bins[-1] + interval)
    subplot.set_xticks([-3 * interval, -interval] + bins + [100 + interval])
    subplot.set_xticklabels(['N/A', '0%'] +
                            ['%d%% - <%d%%' % bucket
                             for bucket in buckets] + ['100%'],
                            rotation=90,
                            size='small')

    # Find the coordinates on the image for each bar
    x = []
    y = []
    for bar in bars:
        x.append(bar.get_x())
        y.append(bar.get_height())
    f = subplot.plot(x, y, linestyle='None')[0]
    upper_left_coords = f.get_transform().transform(zip(x, y))
    bottom_right_coords = f.get_transform().transform([(x_val + interval, 0)
                                                       for x_val in x])

    # Set the title attributes
    titles = [
        '%d%% - <%d%%: %d machines' % (bucket[0], bucket[1], y_val)
        for bucket, y_val in zip(buckets, y)
    ]
    titles.append('0%%: %d machines' % len(no_pass))
    titles.append('100%%: %d machines' % len(perfect))
    titles.append('N/A: %d machines' % len(no_tests))

    # Get the hostnames for each bucket in the histogram
    names_list = [
        _get_hostnames_in_bucket(hist_data, bucket) for bucket in buckets
    ]
    names_list += [no_pass, perfect]

    if plot_info.filter_string:
        plot_info.filter_string += ' AND '

    # Construct the list of drilldown parameters to be passed when the user
    # clicks on the bar.
    params = []
    for names in names_list:
        if names:
            hostnames = ','.join(_quote(hostname) for hostname in names)
            hostname_filter = 'hostname IN (%s)' % hostnames
            full_filter = plot_info.filter_string + hostname_filter
            params.append({'type': 'normal', 'filterString': full_filter})
        else:
            params.append({'type': 'empty'})

    params.append({'type': 'not_applicable', 'hosts': '<br />'.join(no_tests)})

    area_data = [
        dict(left=ulx,
             top=height - uly,
             right=brx,
             bottom=height - bry,
             title=title,
             callback=plot_info.drilldown_callback,
             callback_arguments=param_dict)
        for (ulx, uly), (brx, bry), title, param_dict in zip(
            upper_left_coords, bottom_right_coords, titles, params)
    ]

    # TODO(showard): extract these magic numbers to named constants
    if extra_text:
        figure.text(.1, .95, extra_text, size='xx-small')

    return (figure, area_data)
Beispiel #34
0
    def handle_interaction(self, current_shape, orig_image):
        from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg
        import wx
        """Show the cropping user interface"""
        pixel_data = stretch(orig_image)
        #
        # Create the UI - a dialog with a figure inside
        #
        style = wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER
        dialog_box = wx.Dialog(wx.GetApp().TopWindow, -1,
                               "Select the cropping region",
                               size=(640, 480),
                               style=style)
        sizer = wx.BoxSizer(wx.VERTICAL)
        figure = matplotlib.figure.Figure()
        panel = FigureCanvasWxAgg(dialog_box, -1, figure)
        sizer.Add(panel, 1, wx.EXPAND)
        btn_sizer = wx.StdDialogButtonSizer()
        btn_sizer.AddButton(wx.Button(dialog_box, wx.ID_OK))
        btn_sizer.AddButton(wx.Button(dialog_box, wx.ID_CANCEL))
        btn_sizer.Realize()
        sizer.Add(btn_sizer, 0, wx.ALIGN_CENTER_HORIZONTAL | wx.ALL, 5)
        dialog_box.SetSizer(sizer)
        dialog_box.Size = dialog_box.BestSize
        dialog_box.Layout()

        axes = figure.add_subplot(1, 1, 1)
        assert isinstance(axes, matplotlib.axes.Axes)
        if pixel_data.ndim == 2:
            axes.imshow(pixel_data, matplotlib.cm.Greys_r, origin="upper")
        else:
            axes.imshow(pixel_data, origin="upper")
        # t = axes.transData.inverted()
        current_handle = [None]

        def data_xy(mouse_event):
            """Return the mouse event's x & y converted into data-relative coords"""
            x = mouse_event.xdata
            y = mouse_event.ydata
            return x, y

        class Handle(matplotlib.patches.Rectangle):
            dm = max((10, min(pixel_data.shape) / 50))
            height, width = (dm, dm)

            def __init__(self, x, y, on_move):
                x = max(0, min(x, pixel_data.shape[1]))
                y = max(0, min(y, pixel_data.shape[0]))
                self.__selected = False
                self.__color = cellprofiler.preferences.get_primary_outline_color()
                self.__color = numpy.hstack((self.__color, [255])).astype(float) / 255.0
                self.__on_move = on_move
                super(Handle, self).__init__((x - self.width / 2, y - self.height / 2),
                                             self.width, self.height,
                                             edgecolor=self.__color,
                                             facecolor="none")
                self.set_picker(True)

            def move(self, x, y):
                self.set_xy((x - self.width / 2, y - self.height / 2))
                self.__on_move(x, y)

            def select(self, on):
                self.__selected = on
                if on:
                    current_handle[0] = self
                    self.set_facecolor(self.__color)

                else:
                    self.set_facecolor("none")
                    if current_handle[0] == self:
                        current_handle[0] = None
                figure.canvas.draw()
                dialog_box.Update()

            @property
            def is_selected(self):
                return self.__selected

            @property
            def center_x(self):
                """The handle's notion of its x coordinate"""
                return self.get_x() + self.get_width() / 2

            @property
            def center_y(self):
                """The handle's notion of its y coordinate"""
                return self.get_y() + self.get_height() / 2

            def handle_pick(self, event):
                mouse_event = event.mouseevent
                x, y = data_xy(mouse_event)
                if mouse_event.button == 1:
                    self.select(True)
                    self.orig_x = self.center_x
                    self.orig_y = self.center_y
                    self.first_x = x
                    self.first_y = y

            def handle_mouse_move_event(self, event):
                x, y = data_xy(event)
                if x is None or y is None:
                    return
                x = x - self.first_x + self.orig_x
                y = y - self.first_y + self.orig_y
                if x < 0:
                    x = 0
                if x >= pixel_data.shape[1]:
                    x = pixel_data.shape[1] - 1
                if y < 0:
                    y = 0
                if y >= pixel_data.shape[0]:
                    y = pixel_data.shape[0] - 1
                self.move(x, y)

        class CropRectangle(object):
            def __init__(self, top_left, bottom_right):
                self.__left, self.__top = top_left
                self.__right, self.__bottom = bottom_right
                color = cellprofiler.preferences.get_primary_outline_color()
                color = numpy.hstack((color, [255])).astype(float) / 255.0
                self.rectangle = matplotlib.patches.Rectangle(
                        (min(self.__left, self.__right),
                         min(self.__bottom, self.__top)),
                        abs(self.__right - self.__left),
                        abs(self.__top - self.__bottom),
                        edgecolor=color,
                        facecolor="none"
                )
                self.top_left_handle = Handle(top_left[0], top_left[1],
                                              self.handle_top_left)
                self.bottom_right_handle = Handle(bottom_right[0],
                                                  bottom_right[1],
                                                  self.handle_bottom_right)

            def handle_top_left(self, x, y):
                self.__left = x
                self.__top = y
                self.__reshape()

            def handle_bottom_right(self, x, y):
                self.__right = x
                self.__bottom = y
                self.__reshape()

            def __reshape(self):
                self.rectangle.set_xy((min(self.__left, self.__right),
                                       min(self.__bottom, self.__top)))
                self.rectangle.set_width(abs(self.__right - self.__left))
                self.rectangle.set_height(abs(self.__bottom - self.__top))
                self.rectangle.figure.canvas.draw()
                dialog_box.Update()

            @property
            def patches(self):
                return [self.rectangle, self.top_left_handle,
                        self.bottom_right_handle]

            @property
            def handles(self):
                return [self.top_left_handle, self.bottom_right_handle]

            @property
            def left(self):
                return min(self.__left, self.__right)

            @property
            def right(self):
                return max(self.__left, self.__right)

            @property
            def top(self):
                return min(self.__top, self.__bottom)

            @property
            def bottom(self):
                return max(self.__top, self.__bottom)

        class CropEllipse(object):
            def __init__(self, center, radius):
                """Draw an ellipse with control points at the ellipse center and
                a given x and y radius"""
                self.center_x, self.center_y = center
                self.radius_x = self.center_x + radius[0] / 2
                self.radius_y = self.center_y + radius[1] / 2
                color = cellprofiler.preferences.get_primary_outline_color()
                color = numpy.hstack((color, [255])).astype(float) / 255.0
                self.ellipse = matplotlib.patches.Ellipse(center, self.width, self.height,
                                                          edgecolor=color,
                                                          facecolor="none")
                self.center_handle = Handle(self.center_x, self.center_y,
                                            self.move_center)
                self.radius_handle = Handle(self.radius_x, self.radius_y,
                                            self.move_radius)

            def move_center(self, x, y):
                self.center_x = x
                self.center_y = y
                self.redraw()

            def move_radius(self, x, y):
                self.radius_x = x
                self.radius_y = y
                self.redraw()

            @property
            def width(self):
                return abs(self.center_x - self.radius_x) * 4

            @property
            def height(self):
                return abs(self.center_y - self.radius_y) * 4

            def redraw(self):
                self.ellipse.center = (self.center_x, self.center_y)
                self.ellipse.width = self.width
                self.ellipse.height = self.height
                self.ellipse.figure.canvas.draw()
                dialog_box.Update()

            @property
            def patches(self):
                return [self.ellipse, self.center_handle, self.radius_handle]

            @property
            def handles(self):
                return [self.center_handle, self.radius_handle]

        if self.shape == SH_ELLIPSE:
            if current_shape is None:
                current_shape = {
                    EL_XCENTER: pixel_data.shape[1] / 2,
                    EL_YCENTER: pixel_data.shape[0] / 2,
                    EL_XRADIUS: pixel_data.shape[1] / 2,
                    EL_YRADIUS: pixel_data.shape[0] / 2
                }
            ellipse = current_shape
            shape = CropEllipse((ellipse[EL_XCENTER], ellipse[EL_YCENTER]),
                                (ellipse[EL_XRADIUS], ellipse[EL_YRADIUS]))
        else:
            if current_shape is None:
                current_shape = {
                    RE_LEFT: pixel_data.shape[1] / 4,
                    RE_TOP: pixel_data.shape[0] / 4,
                    RE_RIGHT: pixel_data.shape[1] * 3 / 4,
                    RE_BOTTOM: pixel_data.shape[0] * 3 / 4
                }
            rectangle = current_shape
            shape = CropRectangle((rectangle[RE_LEFT], rectangle[RE_TOP]),
                                  (rectangle[RE_RIGHT], rectangle[RE_BOTTOM]))
        for patch in shape.patches:
            axes.add_artist(patch)

        def on_mouse_down_event(event):
            axes.pick(event)

        def on_mouse_move_event(event):
            if current_handle[0] is not None:
                current_handle[0].handle_mouse_move_event(event)

        def on_mouse_up_event(event):
            if current_handle[0] is not None:
                current_handle[0].select(False)

        def on_pick_event(event):
            for h in shape.handles:
                if id(h) == id(event.artist):
                    h.handle_pick(event)

        figure.canvas.mpl_connect('button_press_event', on_mouse_down_event)
        figure.canvas.mpl_connect('button_release_event', on_mouse_up_event)
        figure.canvas.mpl_connect('motion_notify_event', on_mouse_move_event)
        figure.canvas.mpl_connect('pick_event', on_pick_event)

        try:
            if dialog_box.ShowModal() != wx.ID_OK:
                raise ValueError("Cancelled by user")
        finally:
            dialog_box.Destroy()
        if self.shape == SH_RECTANGLE:
            return {
                RE_LEFT: shape.left,
                RE_TOP: shape.top,
                RE_RIGHT: shape.right,
                RE_BOTTOM: shape.bottom
            }
        else:
            return {
                EL_XCENTER: shape.center_x,
                EL_YCENTER: shape.center_y,
                EL_XRADIUS: shape.width / 2,
                EL_YRADIUS: shape.height / 2
            }
Beispiel #35
0
def _create_line(plots, labels, plot_info):
    """
    Given all the data for the metrics, create a line plot.

    plots: list of dicts containing the plot data. Each dict contains:
            x: list of x-values for the plot
            y: list of corresponding y-values
            errors: errors for each data point, or None if no error information
                    available
            label: plot title
    labels: list of x-tick labels
    plot_info: a MetricsPlot
    """
    # when we're doing any kind of normalization, all series get put into a
    # single plot
    single = bool(plot_info.normalize_to)

    area_data = []
    lines = []
    if single:
        plot_height = _SINGLE_PLOT_HEIGHT
    else:
        plot_height = _MULTIPLE_PLOT_HEIGHT_PER_PLOT * len(plots)
    figure, height = _create_figure(plot_height)

    if single:
        subplot = figure.add_subplot(1, 1, 1)

    # Plot all the data
    for plot_index, (plot, color) in enumerate(zip(plots, _colors(len(plots)))):
        needs_invert = (plot['label'] in plot_info.inverted_series)

        # Add a new subplot, if user wants multiple subplots
        # Also handle axis inversion for subplots here
        if not single:
            subplot = figure.add_subplot(len(plots), 1, plot_index + 1)
            subplot.set_title(plot['label'])
            if needs_invert:
                # for separate plots, just invert the y-axis
                subplot.set_ylim(1, 0)
        elif needs_invert:
            # for a shared plot (normalized data), need to invert the y values
            # manually, since all plots share a y-axis
            plot['y'] = [-y for y in plot['y']]

        # Plot the series
        subplot.set_xticks(range(0, len(labels)))
        subplot.set_xlim(-1, len(labels))
        if single:
            lines += subplot.plot(plot['x'], plot['y'], label=plot['label'],
                                  marker=_MULTIPLE_PLOT_MARKER_TYPE,
                                  markersize=_MULTIPLE_PLOT_MARKER_SIZE)
            error_bar_color = lines[-1].get_color()
        else:
            lines += subplot.plot(plot['x'], plot['y'], _SINGLE_PLOT_STYLE,
                                  label=plot['label'])
            error_bar_color = _SINGLE_PLOT_ERROR_BAR_COLOR
        if plot['errors']:
            subplot.errorbar(plot['x'], plot['y'], linestyle='None',
                             yerr=plot['errors'], color=error_bar_color)
        subplot.set_xticklabels([])

    # Construct the information for the drilldowns.
    # We need to do this in a separate loop so that all the data is in
    # matplotlib before we start calling transform(); otherwise, it will return
    # incorrect data because it hasn't finished adjusting axis limits.
    for line in lines:

        # Get the pixel coordinates of each point on the figure
        x = line.get_xdata()
        y = line.get_ydata()
        label = line.get_label()
        icoords = line.get_transform().transform(zip(x, y))

        # Get the appropriate drilldown query
        drill = plot_info.query_dict['__' + label + '__']

        # Set the title attributes (hover-over tool-tips)
        x_labels = [labels[x_val] for x_val in x]
        titles = ['%s - %s: %f' % (label, x_label, y_val)
                  for x_label, y_val in zip(x_labels, y)]

        # Get the appropriate parameters for the drilldown query
        params = [dict(query=drill, series=line.get_label(), param=x_label)
                  for x_label in x_labels]

        area_data += [dict(left=ix - 5, top=height - iy - 5,
                           right=ix + 5, bottom=height - iy + 5,
                           title=title,
                           callback=plot_info.drilldown_callback,
                           callback_arguments=param_dict)
                      for (ix, iy), title, param_dict
                      in zip(icoords, titles, params)]

    subplot.set_xticklabels(labels, rotation=90, size=_LINE_XTICK_LABELS_SIZE)

    # Show the legend if there are not multiple subplots
    if single:
        font_properties = matplotlib.font_manager.FontProperties(
            size=_LEGEND_FONT_SIZE)
        legend = figure.legend(lines, [plot['label'] for plot in plots],
                               prop=font_properties,
                               handlelen=_LEGEND_HANDLE_LENGTH,
                               numpoints=_LEGEND_NUM_POINTS)
        # Workaround for matplotlib not keeping all line markers in the legend -
        # it seems if we don't do this, matplotlib won't keep all the line
        # markers in the legend.
        for line in legend.get_lines():
            line.set_marker(_LEGEND_MARKER_TYPE)

    return (figure, area_data)
Beispiel #36
0
    def _plot_airmass(self, figure, site, tgt_data, tz):
        """
        Plot into `figure` an airmass chart using target data from `info`
        with time plotted in timezone `tz` (a tzinfo instance).
        """
        # Urk! This seems to be necessary even though we are plotting
        # python datetime objects with timezone attached and setting
        # date formatters with the timezone
        tz_str = tz.tzname(None)
        mpl.rcParams['timezone'] = tz_str

        # set major ticks to hours
        majorTick = mpl_dt.HourLocator(tz=tz)
        majorFmt = mpl_dt.DateFormatter('%Hh')
        # set minor ticks to 15 min intervals
        minorTick = mpl_dt.MinuteLocator(range(0,59,15), tz=tz)

        figure.clf()
        ax1 = figure.add_subplot(111)

        #lstyle = 'o'
        lstyle = '-'
        lt_data = map(lambda info: info.ut.astimezone(tz),
                      tgt_data[0].history)
        # sanity check on dates in preferred timezone
        ## for dt in lt_data[:10]:
        ##     print(dt.strftime("%Y-%m-%d %H:%M:%S"))

        # plot targets airmass vs. time
        for i, info in enumerate(tgt_data):
            am_data = numpy.array(map(lambda info: info.airmass, info.history))
            am_min = numpy.argmin(am_data)
            am_data_dots = am_data
            color = self.colors[i % len(self.colors)]
            lc = color + lstyle
            # ax1.plot_date(lt_data, am_data, lc, linewidth=1.0, alpha=0.3, aa=True, tz=tz)
            ax1.plot_date(lt_data, am_data_dots, lc, linewidth=2.0,
                          aa=True, tz=tz)
            #xs, ys = mpl.mlab.poly_between(lt_data, 2.02, am_data)
            #ax1.fill(xs, ys, facecolor=self.colors[i], alpha=0.2)

            # plot object label
            targname = info.target.name
            ax1.text(mpl_dt.date2num(lt_data[am_data.argmin()]),
                     am_data.min() + 0.08, targname.upper(), color=color,
                     ha='center', va='center')

        ax1.set_ylim(2.02, 0.98)
        ax1.set_xlim(lt_data[0], lt_data[-1])
        ax1.xaxis.set_major_locator(majorTick)
        ax1.xaxis.set_minor_locator(minorTick)
        ax1.xaxis.set_major_formatter(majorFmt)
        labels = ax1.get_xticklabels()
        ax1.grid(True, color='#999999')

        # plot current hour
        lo = datetime.now(tz)
        #lo = datetime.now(tz=tz)
        hi = lo + timedelta(0, 3600.0)
        if lt_data[0] < lo < lt_data[-1]:
            ax1.axvspan(lo, hi, facecolor='#7FFFD4', alpha=0.25)

        # label axes
        localdate = lt_data[0].astimezone(tz).strftime("%Y-%m-%d")
        title = 'Airmass for the night of %s' % (localdate)
        ax1.set_title(title)
        ax1.set_xlabel(tz.tzname(None))
        ax1.set_ylabel('Airmass')

        # Plot moon altitude and degree scale
        ax2 = ax1.twinx()
        moon_data = numpy.array(map(lambda info: numpy.degrees(info.moon_alt),
                                    tgt_data[0].history))
        #moon_illum = site.moon_phase()
        ax2.plot_date(lt_data, moon_data, '#666666', linewidth=2.0,
                      alpha=0.5, aa=True, tz=tz)
        mxs, mys = mpl.mlab.poly_between(lt_data, 0, moon_data)
        # ax2.fill(mxs, mys, facecolor='#666666', alpha=moon_illum)
        ax2.set_ylabel('Moon Altitude (deg)', color='#666666')
        ax2.set_ylim(0, 90)
        ax2.set_xlim(lt_data[0], lt_data[-1])
        ax2.xaxis.set_major_locator(majorTick)
        ax2.xaxis.set_minor_locator(minorTick)
        ax2.xaxis.set_major_formatter(majorFmt)
        ax2.set_xlabel('')
        ax2.yaxis.tick_right()

        canvas = self.fig.canvas
        if canvas is not None:
            canvas.draw()
Beispiel #37
0
def _create_bar(plots, labels, plot_info):
    """
    Given all the data for the metrics, create a line plot.

    plots: list of dicts containing the plot data.
            x: list of x-values for the plot
            y: list of corresponding y-values
            errors: errors for each data point, or None if no error information
                    available
            label: plot title
    labels: list of x-tick labels
    plot_info: a MetricsPlot
    """

    area_data = []
    bars = []
    figure, height = _create_figure(_SINGLE_PLOT_HEIGHT)

    # Set up the plot
    subplot = figure.add_subplot(1, 1, 1)
    subplot.set_xticks(range(0, len(labels)))
    subplot.set_xlim(-1, len(labels))
    subplot.set_xticklabels(labels, rotation=90, size=_BAR_XTICK_LABELS_SIZE)
    # draw a bold line at y=0, making it easier to tell if bars are dipping
    # below the axis or not.
    subplot.axhline(linewidth=2, color='black')

    # width here is the width for each bar in the plot. Matplotlib default is
    # 0.8.
    width = 0.8 / len(plots)

    # Plot the data
    for plot_index, (plot, color) in enumerate(zip(plots, _colors(len(plots)))):
        # Invert the y-axis if needed
        if plot['label'] in plot_info.inverted_series:
            plot['y'] = [-y for y in plot['y']]

        adjusted_x = _get_adjusted_bar(plot['x'], width, plot_index + 1,
                                       len(plots))
        bar_data = subplot.bar(adjusted_x, plot['y'],
                               width=width, yerr=plot['errors'],
                               facecolor=color,
                               label=plot['label'])
        bars.append(bar_data[0])

    # Construct the information for the drilldowns.
    # See comment in _create_line for why we need a separate loop to do this.
    for plot_index, plot in enumerate(plots):
        adjusted_x = _get_adjusted_bar(plot['x'], width, plot_index + 1,
                                       len(plots))

        # Let matplotlib plot the data, so that we can get the data-to-image
        # coordinate transforms
        line = subplot.plot(adjusted_x, plot['y'], linestyle='None')[0]
        label = plot['label']
        upper_left_coords = line.get_transform().transform(zip(adjusted_x,
                                                               plot['y']))
        bottom_right_coords = line.get_transform().transform(
            [(x + width, 0) for x in adjusted_x])

        # Get the drilldown query
        drill = plot_info.query_dict['__' + label + '__']

        # Set the title attributes
        x_labels = [labels[x] for x in plot['x']]
        titles = ['%s - %s: %f' % (plot['label'], label, y)
                  for label, y in zip(x_labels, plot['y'])]
        params = [dict(query=drill, series=plot['label'], param=x_label)
                  for x_label in x_labels]
        area_data += [dict(left=ulx, top=height - uly,
                           right=brx, bottom=height - bry,
                           title=title,
                           callback=plot_info.drilldown_callback,
                           callback_arguments=param_dict)
                      for (ulx, uly), (brx, bry), title, param_dict
                      in zip(upper_left_coords, bottom_right_coords, titles,
                             params)]

    figure.legend(bars, [plot['label'] for plot in plots])
    return (figure, area_data)
Beispiel #38
0
def _create_bar(plots, labels, plot_info):
    """\
    Given all the data for the metrics, create a line plot.

    plots: list of dicts containing the plot data.
            x: list of x-values for the plot
            y: list of corresponding y-values
            errors: errors for each data point, or None if no error information
                    available
            label: plot title
    labels: list of x-tick labels
    plot_info: a MetricsPlot
    """

    area_data = []
    bars = []
    figure, height = _create_figure(_SINGLE_PLOT_HEIGHT)

    # Set up the plot
    subplot = figure.add_subplot(1, 1, 1)
    subplot.set_xticks(range(0, len(labels)))
    subplot.set_xlim(-1, len(labels))
    subplot.set_xticklabels(labels, rotation=90, size=_BAR_XTICK_LABELS_SIZE)
    # draw a bold line at y=0, making it easier to tell if bars are dipping
    # below the axis or not.
    subplot.axhline(linewidth=2, color='black')

    # width here is the width for each bar in the plot. Matplotlib default is
    # 0.8.
    width = 0.8 / len(plots)

    # Plot the data
    for plot_index, (plot, color) in enumerate(zip(plots,
                                                   _colors(len(plots)))):
        # Invert the y-axis if needed
        if plot['label'] in plot_info.inverted_series:
            plot['y'] = [-y for y in plot['y']]

        adjusted_x = _get_adjusted_bar(plot['x'], width, plot_index + 1,
                                       len(plots))
        bar_data = subplot.bar(adjusted_x,
                               plot['y'],
                               width=width,
                               yerr=plot['errors'],
                               facecolor=color,
                               label=plot['label'])
        bars.append(bar_data[0])

    # Construct the information for the drilldowns.
    # See comment in _create_line for why we need a separate loop to do this.
    for plot_index, plot in enumerate(plots):
        adjusted_x = _get_adjusted_bar(plot['x'], width, plot_index + 1,
                                       len(plots))

        # Let matplotlib plot the data, so that we can get the data-to-image
        # coordinate transforms
        line = subplot.plot(adjusted_x, plot['y'], linestyle='None')[0]
        label = plot['label']
        upper_left_coords = line.get_transform().transform(
            zip(adjusted_x, plot['y']))
        bottom_right_coords = line.get_transform().transform([
            (x + width, 0) for x in adjusted_x
        ])

        # Get the drilldown query
        drill = plot_info.query_dict['__' + label + '__']

        # Set the title attributes
        x_labels = [labels[x] for x in plot['x']]
        titles = [
            '%s - %s: %f' % (plot['label'], label, y)
            for label, y in zip(x_labels, plot['y'])
        ]
        params = [
            dict(query=drill, series=plot['label'], param=x_label)
            for x_label in x_labels
        ]
        area_data += [
            dict(left=ulx,
                 top=height - uly,
                 right=brx,
                 bottom=height - bry,
                 title=title,
                 callback=plot_info.drilldown_callback,
                 callback_arguments=param_dict)
            for (ulx, uly), (brx, bry), title, param_dict in zip(
                upper_left_coords, bottom_right_coords, titles, params)
        ]

    figure.legend(bars, [plot['label'] for plot in plots])
    return (figure, area_data)
Beispiel #39
0
def _create_qual_histogram_helper(plot_info, extra_text=None):
    """
    Create a machine qualification histogram of the given data.

    plot_info: a QualificationHistogram
    extra_text: text to show at the upper-left of the graph

    TODO(showard): move much or all of this into methods on
    QualificationHistogram
    """
    cursor = readonly_connection.connection().cursor()
    cursor.execute(plot_info.query)

    if not cursor.rowcount:
        raise NoDataError('query did not return any data')

    # Lists to store the plot data.
    # hist_data store tuples of (hostname, pass_rate) for machines that have
    #     pass rates between 0 and 100%, exclusive.
    # no_tests is a list of machines that have run none of the selected tests
    # no_pass is a list of machines with 0% pass rate
    # perfect is a list of machines with a 100% pass rate
    hist_data = []
    no_tests = []
    no_pass = []
    perfect = []

    # Construct the lists of data to plot
    for hostname, total, good in cursor.fetchall():
        if total == 0:
            no_tests.append(hostname)
            continue

        if good == 0:
            no_pass.append(hostname)
        elif good == total:
            perfect.append(hostname)
        else:
            percentage = 100.0 * good / total
            hist_data.append((hostname, percentage))

    interval = plot_info.interval
    bins = range(0, 100, interval)
    if bins[-1] != 100:
        bins.append(bins[-1] + interval)

    figure, height = _create_figure(_SINGLE_PLOT_HEIGHT)
    subplot = figure.add_subplot(1, 1, 1)

    # Plot the data and get all the bars plotted
    _, _, bars = subplot.hist([data[1] for data in hist_data],
                              bins=bins, align='left')
    bars += subplot.bar([-interval], len(no_pass),
                        width=interval, align='center')
    bars += subplot.bar([bins[-1]], len(perfect),
                        width=interval, align='center')
    bars += subplot.bar([-3 * interval], len(no_tests),
                        width=interval, align='center')

    buckets = [(bin, min(bin + interval, 100)) for bin in bins[:-1]]
    # set the x-axis range to cover all the normal bins plus the three "special"
    # ones - N/A (3 intervals left), 0% (1 interval left) ,and 100% (far right)
    subplot.set_xlim(-4 * interval, bins[-1] + interval)
    subplot.set_xticks([-3 * interval, -interval] + bins + [100 + interval])
    subplot.set_xticklabels(['N/A', '0%'] +
                            ['%d%% - <%d%%' % bucket for bucket in buckets] +
                            ['100%'], rotation=90, size='small')

    # Find the coordinates on the image for each bar
    x = []
    y = []
    for bar in bars:
        x.append(bar.get_x())
        y.append(bar.get_height())
    f = subplot.plot(x, y, linestyle='None')[0]
    upper_left_coords = f.get_transform().transform(zip(x, y))
    bottom_right_coords = f.get_transform().transform(
        [(x_val + interval, 0) for x_val in x])

    # Set the title attributes
    titles = ['%d%% - <%d%%: %d machines' % (bucket[0], bucket[1], y_val)
              for bucket, y_val in zip(buckets, y)]
    titles.append('0%%: %d machines' % len(no_pass))
    titles.append('100%%: %d machines' % len(perfect))
    titles.append('N/A: %d machines' % len(no_tests))

    # Get the hostnames for each bucket in the histogram
    names_list = [_get_hostnames_in_bucket(hist_data, bucket)
                  for bucket in buckets]
    names_list += [no_pass, perfect]

    if plot_info.filter_string:
        plot_info.filter_string += ' AND '

    # Construct the list of drilldown parameters to be passed when the user
    # clicks on the bar.
    params = []
    for names in names_list:
        if names:
            hostnames = ','.join(_quote(hostname) for hostname in names)
            hostname_filter = 'hostname IN (%s)' % hostnames
            full_filter = plot_info.filter_string + hostname_filter
            params.append({'type': 'normal',
                           'filterString': full_filter})
        else:
            params.append({'type': 'empty'})

    params.append({'type': 'not_applicable',
                   'hosts': '<br />'.join(no_tests)})

    area_data = [dict(left=ulx, top=height - uly,
                      right=brx, bottom=height - bry,
                      title=title, callback=plot_info.drilldown_callback,
                      callback_arguments=param_dict)
                 for (ulx, uly), (brx, bry), title, param_dict
                 in zip(upper_left_coords, bottom_right_coords, titles, params)]

    # TODO(showard): extract these magic numbers to named constants
    if extra_text:
        figure.text(.1, .95, extra_text, size='xx-small')

    return (figure, area_data)
Beispiel #40
0
from PIL import Image
import numpy as np
from matplotlib import pyplot as plt
import matplotlib.figure as fig

img = np.array(Image.open('bg1.jpg'))
"""
Normalized channels
"""
R = img[:, :, 0] / np.max(img[:, :, 0])
G = img[:, :, 1] / np.max(img[:, :, 1])
B = img[:, :, 2] / np.max(img[:, :, 2])

Hue = np.arccos(((2 * R) - G - B) / 2 *
                np.sqrt(np.square(R - G) + np.multiply((R - G), (G - B))))
Intensity = np.mean(img / 255, axis=2)
Imin = np.min(img / 255, axis=2)
Intensity[Intensity == 0] = 0.1  #avoiding zero error
Saturation = 1 - (Imin / Intensity)
fig = plt.figure()
ax = fig.add_subplot(111)
n, bins, p = ax.hist(Saturation.flatten())
ax.set_xbound(0, 1)
ax.plot(bins)
print(np.max(Intensity))
plt.show()
print(Intensity)
print(Saturation)
Beispiel #41
0
                  y0 = netCDFfile.variables['y0'][:]
                  # calculate rotated xprime coordinates along the surface - xx, yy are used by code below to write the output
                  xx = x0 * cos(alpha) + (usurfStag[20,:]-7000.0) * sin(alpha)
                  xx = xx/1000.0 - 50.0
                  yy = y0/1000.0 - 50.0
                  # calculate rotated uvel/vvel at surface
                  uvelSprime =  uvelS[:,:] * cos(alpha) + wvelStag[:,:] * sin(alpha)
                  wvelSprime = -uvelS[:,:] * sin(alpha) + wvelStag[:,:] * cos(alpha)

                  nan = np.ones(uvelSprime.shape)*-999.0  # create a dummy matrix for uncalculated values.

                  # ===========================================
                  # optional bit of code to plot out vertical velocity on the rotated grid.
                  # This can be compared to Fig. 14b in the tc-2007-0019-sp3.pdf document
                  figure = pyplot.figure(subplotpars=matplotlib.figure.SubplotParams(top=.85,bottom=.15))
                  axes = figure.add_subplot(111)
                  pc = axes.pcolor(wvelSprime)
                  pyplot.colorbar(pc)
                  cntr=axes.contour(wvelSprime, [-0.5, -0.4, -0.3, -0.2, -0.1, 0.0, 0.1, 0.2, 0.3, 0.4],colors='k')
                  axes.clabel(cntr)
                  pyplot.title('Compare to Fig. 14b of tc-2007-0019-sp3.pdf')
                  pyplot.draw()
                  pyplot.show()
                  # ===========================================

              else:  # all other tests

                  # Make x,y position arrays that can be used by all test cases.
                  #   Want x/y positions to include the periodic edge at both the beginning and end
                  xx = netCDFfile.variables['x0'][:]/(1000.0*float(size))
                  xx = np.concatenate(([0.0],xx,[1.0]))
Beispiel #42
0
                    # calculate rotated uvel/vvel at surface
                    uvelSprime = uvelS[:, :] * cos(
                        alpha) + wvelStag[:, :] * sin(alpha)
                    wvelSprime = -uvelS[:, :] * sin(
                        alpha) + wvelStag[:, :] * cos(alpha)

                    nan = np.ones(
                        uvelSprime.shape
                    ) * -999.0  # create a dummy matrix for uncalculated values.

                    # ===========================================
                    # optional bit of code to plot out vertical velocity on the rotated grid.
                    # This can be compared to Fig. 14b in the tc-2007-0019-sp3.pdf document
                    figure = pyplot.figure(subplotpars=matplotlib.figure.
                                           SubplotParams(top=.85, bottom=.15))
                    axes = figure.add_subplot(111)
                    pc = axes.pcolor(wvelSprime)
                    pyplot.colorbar(pc)
                    cntr = axes.contour(wvelSprime, [
                        -0.5, -0.4, -0.3, -0.2, -0.1, 0.0, 0.1, 0.2, 0.3, 0.4
                    ],
                                        colors='k')
                    axes.clabel(cntr)
                    pyplot.title('Compare to Fig. 14b of tc-2007-0019-sp3.pdf')
                    pyplot.draw()
                    pyplot.show()
                    # ===========================================

                else:  # all other tests

                    # Make x,y position arrays that can be used by all test cases.
Beispiel #43
0
def scaleLabel(label, scale):
    return "{0} / ($10^{{{1}}}$)".format(label, scale)

def nudge(text, x, y):
    text.set_transform(text.get_transform() + matplotlib.transforms.Affine2D().translate(x, y))

if __name__ == "__main__":
    
    # Pre-configure plot
    plot = Plot()
    figure = matplotlib.pyplot.figure()
    if plot.sig:
        figure.set_size_inches(plot.args.size, plot.args.size)
        grid = matplotlib.gridspec.GridSpec(4, 1)
        axes1 = figure.add_subplot(grid[0:-1, :])
        axes2 = figure.add_subplot(grid[-1, :])
    else:
        figure.set_size_inches(plot.args.size, SIZE_FACTOR * plot.args.size)
        axes1 = figure.gca()
    if plot.args.logx:
        axes1.semilogx()
    elif plot.args.logy:
        axes1.semilogy()
    elif plot.args.logxy:
        axes1.loglog()
    
    # Iterate runs
    driven = {}
    passive = {}
    for run in plot.runs: