def plot_agency_magnitude_pair(data, overlay=False, xlim=[], ylim=[],
        marker="o", figure_size=(7, 8), filetype="png", resolution=300,
        filename=None):
    """
    Plots the agency magnitude pair
    :param dict data:
        Query result for a particular joint agency-magnitude pair combination
    :param bool overlay:
        Allows another layer to be rendered on top (True) or closes the figure
        for plotting (False)
    :param list xlim:
        Lower and upper bounds for x-axis
    :param list ylim:
        Lower and upper bounds for y-axis
    """
    if not data:
        print "No pairs found - abandoning plot!"
        return
    fig = plt.figure(figsize=figure_size)
    keys = data.keys()
    plt.errorbar(data[keys[0]], data[keys[2]],
                 xerr=data[keys[1]], yerr=data[keys[3]],
                 marker=marker, mfc="b", mec="k", ls="None",
                 ecolor="r")
    plt.xlabel(utils._to_latex(keys[0]), fontsize=16)
    plt.ylabel(utils._to_latex(keys[2]), fontsize=16)
    plt.grid(True)
    if len(xlim) == 2:
        lowx = xlim[0]
        highx = xlim[1]
    else:
        lowx = np.floor(np.min(data[keys[0]]))
        highx = np.ceil(np.max(data[keys[0]]))

    if len(ylim) == 2:
        lowy = ylim[0]
        highy = ylim[1]
    else:
        lowy = np.floor(np.min(data[keys[2]]))
        highy = np.ceil(np.max(data[keys[2]]))

    if lowy < lowx:
        lowx = lowy
    if highy > highx:
        highx = highy
    plt.ylim(lowx, highx)
    plt.xlim(lowx, highx)
    # Overlay 1:1 line
    plt.plot(np.array([lowx, highx]), np.array([lowx, highx]), ls="--",
        color=[0.5, 0.5, 0.5], zorder=1)
    plt.tight_layout()
        
    if filename:
        utils._save_image(filename, filetype, resolution)
    if not overlay:
        plt.show()
    return data
Exemple #2
0
 def get_string(self, output_string, input_string):
     """
     Returns the title string
     """
     base_string = "{:s} = ".format(_to_latex(output_string))
     for iloc, param in enumerate(self.params):
         if iloc == 0:
             base_string = base_string + "{:.3f}".format(param)
         elif iloc == 1:
             base_string = base_string + " {:s}{:s}".format(
                 _set_string(param), _to_latex(input_string))
         else:
             base_string = base_string + (
                 " %s%s$^%d$" %
                 (_set_string(param), _to_latex(input_string), iloc))
     return base_string
Exemple #3
0
 def get_string(self, output_string, input_string):
     """
     Returns the title string
     """
     base_string = "%s = e$^{(%.3f %s %s)}$ %s" % (
         _to_latex(output_string), self.params[0],
         _set_string(self.params[1]), self._to_latex(input_string),
         _set_string(self.params[2]))
     return base_string
Exemple #4
0
 def get_string(self, output_string, input_string):
     """
     Returns the title string
     """
     base_string = "{:s} = ".format(_to_latex(output_string))
     # Equation 1
     upper_string = base_string +\
         "{:.3f} {:s}{:s}    for {:s} < {:.2f}".format(
             self.params[0][1],
             _set_string(self.params[0][0]),
             _to_latex(input_string),
             _to_latex(input_string),
             self.corner_magnitude)
     lower_string = base_string +\
         "{:.3f} {:s}{:s}    for {:s} $\geq$ {:.2f}".format(
             self.params[1][1],
             _set_string(self.params[1][0]),
             _to_latex(input_string),
             _to_latex(input_string),
             self.corner_magnitude)
     return "\n".join([upper_string, lower_string])
Exemple #5
0
 def get_string(self, output_string, input_string):
     """
     Returns the title string
     """
     n_seg = len(self.params)
     full_string = []
     for iloc, params in enumerate(self.params):
         eq_string = "{:s} = {:.3f} {:s} {:s}".format(
             _to_latex(output_string), params[0], _set_string(params[1]),
             _to_latex(input_string))
         if iloc == 0:
             cond_string = eq_string + "    for {:s} < {:.3f}".format(
                 _to_latex(input_string), params[2])
         elif iloc == (n_seg - 1):
             cond_string = eq_string + "    for {:s} $\geq$ {:.3f}".format(
                 _to_latex(input_string), params[2])
         else:
             cond_string = eq_string +\
                 "    for {:.3f} $\leq$ {:s} < {:.3f}".format(
                     self.params[iloc - 1][2],
                     _to_latex(input_string),
                     params[2])
         full_string.append(cond_string)
     return "\n".join([case_string for case_string in full_string])
def plot_agency_magnitude_density(data, overlay=False, number_samples=0,
        xlim=[], ylim=[], figure_size=(7, 8), lognorm=True,
        filetype="png", resolution=300, filename=None):
    """

    """
    keys = data.keys()
    if not data:
        print "No pairs found - abandoning plot!"
        return
    
    if len(xlim) == 2:
        lowx = xlim[0]
        highx = xlim[1]
    else:
        lowx = np.floor(np.min(data[keys[0]]))
        highx = np.ceil(np.max(data[keys[0]]))

    if len(ylim) == 2:
        lowy = ylim[0]
        highy = ylim[1]
    else:
        lowy = np.floor(np.min(data[keys[2]]))
        highy = np.ceil(np.max(data[keys[2]]))
    
    if lowy < lowx:
        lowx = lowy
    if highy > highx:
        highx = highy

    xbins = np.linspace(lowx - 0.05, highx + 0.05,
                        ((highx + 0.05 - lowx - 0.05) / 0.1) + 2.0)
    ybins = np.linspace(lowx - 0.05, highx + 0.05, 
                        ((highx + 0.05 - lowx - 0.05) / 0.1) + 2.0)
    density = sample_agency_magnitude_pairs(data, xbins, ybins, number_samples)
    fig = plt.figure(figsize=figure_size)

    if lognorm:
        cmap = deepcopy(matplotlib.cm.get_cmap("jet"))
        data_norm = LogNorm(vmin=0.1, vmax=np.max(density))
    else:
        cmap = deepcopy(matplotlib.cm.get_cmap("jet"))
        cmap.set_under("w")
        data_norm = Normalize(vmin=0.1, vmax=np.max(density))
        #density[density < 1E-15] == np.nan  
    plt.pcolormesh(xbins[:-1] + 0.05, ybins[:-1] + 0.05, density.T,
                   norm=data_norm, cmap=cmap)
    cbar = plt.colorbar()
    cbar.set_label("Number Events", fontsize=16)
    plt.xlabel(utils._to_latex(keys[0]), fontsize=16)
    plt.ylabel(utils._to_latex(keys[2]), fontsize=16)
    plt.grid(True)
    plt.ylim(lowx, highx)
    plt.xlim(lowx, highx)
    # Overlay 1:1 line
    plt.plot(np.array([lowx, highx]), np.array([lowx, highx]), ls="--",
        color=[0.5, 0.5, 0.5], zorder=1)
    plt.tight_layout()
        
    if filename:
        utils._save_image(filename, filetype, resolution)
    if not overlay:
        plt.show()
    return data
Exemple #7
0
def plot_agency_magnitude_density(data,
                                  overlay=False,
                                  number_samples=0,
                                  xlim=[],
                                  ylim=[],
                                  figure_size=(7, 8),
                                  lognorm=True,
                                  filetype="png",
                                  resolution=300,
                                  filename=None):
    """

    """
    keys = data.keys()
    if not data:
        print "No pairs found - abandoning plot!"
        return

    if len(xlim) == 2:
        lowx = xlim[0]
        highx = xlim[1]
    else:
        lowx = np.floor(np.min(data[keys[0]]))
        highx = np.ceil(np.max(data[keys[0]]))

    if len(ylim) == 2:
        lowy = ylim[0]
        highy = ylim[1]
    else:
        lowy = np.floor(np.min(data[keys[2]]))
        highy = np.ceil(np.max(data[keys[2]]))

    if lowy < lowx:
        lowx = lowy
    if highy > highx:
        highx = highy

    xbins = np.linspace(lowx - 0.05, highx + 0.05,
                        ((highx + 0.05 - lowx - 0.05) / 0.1) + 2.0)
    ybins = np.linspace(lowx - 0.05, highx + 0.05,
                        ((highx + 0.05 - lowx - 0.05) / 0.1) + 2.0)
    density = sample_agency_magnitude_pairs(data, xbins, ybins, number_samples)
    fig = plt.figure(figsize=figure_size)

    if lognorm:
        cmap = deepcopy(matplotlib.cm.get_cmap("jet"))
        data_norm = LogNorm(vmin=0.1, vmax=np.max(density))
    else:
        cmap = deepcopy(matplotlib.cm.get_cmap("jet"))
        cmap.set_under("w")
        data_norm = Normalize(vmin=0.1, vmax=np.max(density))
        #density[density < 1E-15] == np.nan
    plt.pcolormesh(xbins[:-1] + 0.05,
                   ybins[:-1] + 0.05,
                   density.T,
                   norm=data_norm,
                   cmap=cmap)
    cbar = plt.colorbar()
    cbar.set_label("Number Events", fontsize=16)
    plt.xlabel(utils._to_latex(keys[0]), fontsize=16)
    plt.ylabel(utils._to_latex(keys[2]), fontsize=16)
    plt.grid(True)
    plt.ylim(lowx, highx)
    plt.xlim(lowx, highx)
    # Overlay 1:1 line
    plt.plot(np.array([lowx, highx]),
             np.array([lowx, highx]),
             ls="--",
             color=[0.5, 0.5, 0.5],
             zorder=1)
    plt.tight_layout()

    if filename:
        utils._save_image(filename, filetype, resolution)
    if not overlay:
        plt.show()
    return data
Exemple #8
0
def plot_agency_magnitude_pair(data,
                               overlay=False,
                               xlim=[],
                               ylim=[],
                               marker="o",
                               figure_size=(7, 8),
                               filetype="png",
                               resolution=300,
                               filename=None):
    """
    Plots the agency magnitude pair
    :param dict data:
        Query result for a particular joint agency-magnitude pair combination
    :param bool overlay:
        Allows another layer to be rendered on top (True) or closes the figure
        for plotting (False)
    :param list xlim:
        Lower and upper bounds for x-axis
    :param list ylim:
        Lower and upper bounds for y-axis
    """
    if not data:
        print "No pairs found - abandoning plot!"
        return
    fig = plt.figure(figsize=figure_size)
    keys = data.keys()
    plt.errorbar(data[keys[0]],
                 data[keys[2]],
                 xerr=data[keys[1]],
                 yerr=data[keys[3]],
                 marker=marker,
                 mfc="b",
                 mec="k",
                 ls="None",
                 ecolor="r")
    plt.xlabel(utils._to_latex(keys[0]), fontsize=16)
    plt.ylabel(utils._to_latex(keys[2]), fontsize=16)
    plt.grid(True)
    if len(xlim) == 2:
        lowx = xlim[0]
        highx = xlim[1]
    else:
        lowx = np.floor(np.min(data[keys[0]]))
        highx = np.ceil(np.max(data[keys[0]]))

    if len(ylim) == 2:
        lowy = ylim[0]
        highy = ylim[1]
    else:
        lowy = np.floor(np.min(data[keys[2]]))
        highy = np.ceil(np.max(data[keys[2]]))

    if lowy < lowx:
        lowx = lowy
    if highy > highx:
        highx = highy
    plt.ylim(lowx, highx)
    plt.xlim(lowx, highx)
    # Overlay 1:1 line
    plt.plot(np.array([lowx, highx]),
             np.array([lowx, highx]),
             ls="--",
             color=[0.5, 0.5, 0.5],
             zorder=1)
    plt.tight_layout()

    if filename:
        utils._save_image(filename, filetype, resolution)
    if not overlay:
        plt.show()
    return data