Example #1
0
def test_LogFormatterSciNotation():
    test_cases = {
        10: (
            (-1, '${-10^{0}}$'),
            (1e-05, '${10^{-5}}$'),
            (1, '${10^{0}}$'),
            (100000, '${10^{5}}$'),
            (2e-05, '${2\\times10^{-5}}$'),
            (2, '${2\\times10^{0}}$'),
            (200000, '${2\\times10^{5}}$'),
            (5e-05, '${5\\times10^{-5}}$'),
            (5, '${5\\times10^{0}}$'),
            (500000, '${5\\times10^{5}}$'),
        ),
        2: (
            (0.03125, '${2^{-5}}$'),
            (1, '${2^{0}}$'),
            (32, '${2^{5}}$'),
            (0.0375, '${1.2\\times2^{-5}}$'),
            (1.2, '${1.2\\times2^{0}}$'),
            (38.4, '${1.2\\times2^{5}}$'),
        )
    }

    for base in test_cases.keys():
        formatter = mticker.LogFormatterSciNotation(base=base)
        formatter.sublabel = set([1, 2, 5, 1.2])
        for value, expected in test_cases[base]:
            with matplotlib.rc_context({'text.usetex': False}):
                assert formatter(value) == expected
Example #2
0
def logcontourf(xx,yy,arr,subs=(1.0,),clabelsize=14,\
vmin=None,vmax=None,\
cmap='viridis',alpha=1,extend='both',offsetTextsize=14):
    """
    Parameter
    ---------------
    xx,yy  :array_like
    arr    :array_like
            input data
    subs   :sequence of float 'all','auto',None,
           default=(1,) ex) (1.0,2.0) 
    cmap   :string colormap
    vmin   :float minimum value(default=arr.min())
    vmax   :float maximum value(default=arr.max())
    alpha  :float 0-1 (default=1)
    extend :string {'max','min','both','neither'}
    
    
    Return
    ----------------
    fig
    ax

    """
    fig = plt.figure(figsize=(10, 5), facecolor='w')
    ax = fig.add_subplot(1,
                         1,
                         1,
                         projection=ccrs.PlateCarree(central_longitude=180))
    cax = fig.add_axes([0.25, 0, 0.5, 0.05])
    ax = ckit.set_geogrid(ax)
    if vmin is None:
        vmin = arr.min()
    if vmax is None:
        vmax = arr.max()
    cf = ax.contourf(xx,
                     yy,
                     arr,
                     vmin=vmin,
                     vmax=vmax,
                     cmap=cmap,
                     extend=extend,
                     alpha=alpha,
                     locator=ticker.LogLocator(subs=subs),
                     transform=ccrs.PlateCarree())
    cbar = fig.colorbar(cf,
                        cax,
                        extend=extend,
                        ticks=ticker.LogLocator(subs=(1.0, )),
                        format=ticker.LogFormatterSciNotation(),
                        orientation='horizontal')
    cbar.ax.tick_params(labelsize=clabelsize)
    cbar.ax.xaxis.offsetText.set_fontsize(offsetTextsize)
    return fig, ax, cbar
Example #3
0
 def test_basic(self, base, value, expected):
     formatter = mticker.LogFormatterSciNotation(base=base)
     formatter.sublabel = {1, 2, 5, 1.2}
     with matplotlib.rc_context({'text.usetex': False}):
         assert formatter(value) == expected
Example #4
0
    def __init__(self, ax, cmap=None,
                 norm=None,
                 alpha=None,
                 values=None,
                 boundaries=None,
                 orientation='vertical',
                 ticklocation='auto',
                 extend='neither',
                 spacing='uniform',  # uniform or proportional
                 ticks=None,
                 format=None,
                 drawedges=False,
                 filled=True,
                 extendfrac=None,
                 extendrect=False,
                 label='',
                 ):
        #: The axes that this colorbar lives in.
        self.ax = ax
        self._patch_ax()
        if cmap is None:
            cmap = cm.get_cmap()
        if norm is None:
            norm = colors.Normalize()
        self.alpha = alpha
        cm.ScalarMappable.__init__(self, cmap=cmap, norm=norm)
        self.values = values
        self.boundaries = boundaries
        self.extend = extend
        self._inside = self._slice_dict[extend]
        self.spacing = spacing
        self.orientation = orientation
        self.drawedges = drawedges
        self.filled = filled
        self.extendfrac = extendfrac
        self.extendrect = extendrect
        self.solids = None
        self.lines = list()
        self.outline = None
        self.patch = None
        self.dividers = None

        if ticklocation == 'auto':
            ticklocation = 'bottom' if orientation == 'horizontal' else 'right'
        self.ticklocation = ticklocation

        self.set_label(label)
        if cbook.iterable(ticks):
            self.locator = ticker.FixedLocator(ticks, nbins=len(ticks))
        else:
            self.locator = ticks    # Handle default in _ticker()
        if format is None:
            if isinstance(self.norm, colors.LogNorm):
                self.formatter = ticker.LogFormatterSciNotation()
            elif isinstance(self.norm, colors.SymLogNorm):
                self.formatter = ticker.LogFormatterSciNotation(
                                        linthresh=self.norm.linthresh)
            else:
                self.formatter = ticker.ScalarFormatter()
        elif cbook.is_string_like(format):
            self.formatter = ticker.FormatStrFormatter(format)
        else:
            self.formatter = format  # Assume it is a Formatter
        # The rest is in a method so we can recalculate when clim changes.
        self.config_axis()
        self.draw_all()
Example #5
0
            bool_den_array = image > image_dict['density_threshold']
            vel_rad = vel_rad * bool_den_array  #bool_den_array*np.nan*vel_rad
            vel_rad[vel_rad == 0] = np.nan

            v_std = np.std(vel_rad / 100000)
            v_cbar_min = -1
            v_cbar_max = 1
            plot = ax.pcolormesh(X,
                                 Y,
                                 vel_rad / 100000,
                                 cmap='idl06_r',
                                 rasterized=True,
                                 vmin=v_cbar_min,
                                 vmax=v_cbar_max)
            fmt = ticker.LogFormatterSciNotation()
            fmt.create_dummy_axis()
            if args.density_threshold != 0.0:
                exp_min = np.log10(image_dict['density_threshold'])
            else:
                exp_min = np.log10(cbar_min)
            exp_max = np.log10(cbar_max)
            n_level = (exp_max - exp_min) * 2 + 1
            contour_levels = np.logspace(exp_min, exp_max, int(n_level))
            CS = ax.contour(X,
                            Y,
                            image,
                            locator=plt.LogLocator(),
                            linewidths=0.5,
                            colors='k',
                            levels=contour_levels)
Example #6
0
def main():
    parser = argparse.ArgumentParser(description='plotter')
    parser.add_argument('--fig_length',
                        type=int,
                        default=6,
                        help='matplotlib figure length (default: 6)')
    parser.add_argument('--fig_width',
                        type=int,
                        default=6,
                        help='matplotlib figure width (default: 6)')
    parser.add_argument('--style',
                        default='seaborn',
                        help='matplotlib figure style (default: seaborn)')
    parser.add_argument('--title',
                        default=None,
                        help='matplotlib figure title (default: None)')
    parser.add_argument('--xlabel',
                        default=None,
                        help='matplotlib figure xlabel')
    parser.add_argument('--xkey',
                        default='l',
                        help='x-axis key in csv file (default: l)')
    parser.add_argument('--ykey',
                        default='r',
                        help='y-axis key in csv file (default: r)')
    parser.add_argument('--ylabel',
                        default=None,
                        help='matplotlib figure ylabel')
    parser.add_argument('--smooth',
                        type=int,
                        default=10,
                        help='smooth radius of y axis (default: 10)')
    parser.add_argument(
        '--resample',
        type=int,
        default=512,
        help=
        'if not zero, size of the uniform grid in x direction to resample onto. Resampling is performed via symmetric EMA smoothing (see the docstring for symmetric_ema). Default is zero (no resampling). Note that if average_group is True, resampling is necessary; in that case, default value is 512. (default: 512)'
    )
    parser.add_argument(
        '--smooth_step',
        type=float,
        default=1.0,
        help=
        'when resampling (i.e. when resample > 0 or average_group is True), use this EMA decay parameter (in units of the new grid step). See docstrings for decay_steps in symmetric_ema or one_sided_ema functions.'
    )
    parser.add_argument(
        '--avg_group',
        action='store_true',
        help='average the curves in the same group and plot the mean.')
    parser.add_argument(
        '--shaded_std',
        action='store_true',
        help='shaded region corresponding to standard deviation of the group')
    parser.add_argument(
        '--shaded_err',
        action='store_true',
        help='shaded region corresponding to error in mean estimate')
    parser.add_argument('--legend_loc',
                        type=int,
                        default=0,
                        help='location of legend')
    parser.add_argument('--legend_outside',
                        action='store_true',
                        help='place the legend outside of the figure')
    parser.add_argument('--no_legend_group_num',
                        action='store_true',
                        help="don't show num of group in legend")

    parser.add_argument(
        '--time',
        action='store_true',
        help=
        'enable this will set x_key to t, and activate parameters about time')
    parser.add_argument(
        '--time_unit',
        default='h',
        help='parameters about time, x axis time unit (default: h)')
    parser.add_argument(
        '--time_interval',
        type=float,
        default=1,
        help='parameters about time, x axis time interval (default: 1)')

    parser.add_argument('--xformat', default='eng', help='x-axis format')
    parser.add_argument('--xlim',
                        type=int,
                        default=None,
                        help='x-axis limitation (default: None)')

    parser.add_argument('--log_dir',
                        default='./',
                        help='log dir (default: ./)')
    parser.add_argument('--filename', default='monitor', help='csv filename')
    parser.add_argument('--show', action='store_true', help='show figure')
    parser.add_argument('--save', action='store_true', help='save figure')
    parser.add_argument('--dpi',
                        type=int,
                        default=400,
                        help='figure dpi (default: 400)')
    args = parser.parse_args()

    xscale = 1
    if args.time:
        if args.xlabel is None:
            args.xlabel = 'Training time'
        args.xkey = 't'
        if args.time_unit == 'h':
            xscale = 60 * 60
            args.time_interval = 2
        elif args.time_unit == 'min':
            xscale = 60
            args.time_interval = 20
    else:
        if args.xlabel is None:
            args.xlabel = 'Timesteps'

    if args.ylabel is None:
        args.ylabel = 'Episode Reward'

    if args.filename == 'evaluator':
        args.xkey = 'total_steps'
        args.ykey = 'mean_score'

    allresults = pu.load_results(args.log_dir, filename=args.filename)
    pu.plot_results(allresults,
                    fig_length=args.fig_length,
                    fig_width=args.fig_width,
                    style=args.style,
                    title=args.title,
                    xlabel=args.xlabel,
                    ylabel=args.ylabel,
                    xkey=args.xkey,
                    ykey=args.ykey,
                    xscale=xscale,
                    smooth_radius=args.smooth,
                    resample=args.resample,
                    smooth_step=args.smooth_step,
                    average_group=args.avg_group,
                    shaded_std=args.shaded_std,
                    shaded_err=args.shaded_err,
                    legend_outside=args.legend_outside,
                    legend_loc=args.legend_loc,
                    legend_group_num=not args.no_legend_group_num,
                    filename=args.filename)

    ax = plt.gca()  # get current axis
    if args.time:
        if args.time_unit == 'h' or args.time_unit == 'min':
            ax.xaxis.set_major_locator(
                mticker.MultipleLocator(args.time_interval))
        ax.xaxis.set_major_formatter(
            mticker.FormatStrFormatter("%d" + args.time_unit))
    else:
        if args.xformat == 'eng':
            ax.xaxis.set_major_formatter(mticker.EngFormatter())
        elif args.xformat == 'log':
            ax.xaxis.set_major_formatter(mticker.LogFormatter())
        elif args.xformat == 'sci':
            ax.xaxis.set_major_formatter(mticker.LogFormatterSciNotation())

    if args.xlim is not None:
        plt.xlim((0, args.xlim))

    if args.save:
        plt.savefig(args.log_dir + 'figure', dpi=args.dpi, bbox_inches='tight')
    if args.show:
        plt.show()
Example #7
0
    def axis_label_and_ticks(self, axis, data_array, name, dim_to_shape):
        """
        Get dimensions and label (if present) from requested axis.
        Also retun axes tick formatters and locators.
        """

        # Create some default axis tick formatter, depending on whether log
        # for that axis will be True or False
        formatter = {
            False: ticker.ScalarFormatter(),
            True: ticker.LogFormatterSciNotation()
        }
        locator = {False: ticker.AutoLocator(), True: ticker.LogLocator()}

        dim = axis
        # Convert to Dim object?
        if isinstance(dim, str):
            dim = sc.Dim(dim)

        underlying_dim = dim
        non_dimension_coord = False
        var = None

        if dim in data_array.coords:

            dim_coord_dim = data_array.coords[dim].dims[-1]
            tp = data_array.coords[dim].dtype

            if tp == sc.dtype.vector_3_float64:
                var = make_fake_coord(dim_coord_dim,
                                      dim_to_shape[dim_coord_dim],
                                      unit=data_array.coords[dim].unit)
                form = ticker.FuncFormatter(lambda val, pos: "(" + ",".join([
                    value_to_string(item, precision=2) for item in self.
                    scipp_obj_dict[name].coords[dim].values[int(val)]
                ]) + ")" if (int(val) >= 0 and int(val) < dim_to_shape[
                    dim_coord_dim]) else "")
                formatter.update({False: form, True: form})
                locator[False] = ticker.MaxNLocator(integer=True)
                if dim != dim_coord_dim:
                    underlying_dim = dim_coord_dim

            elif tp == sc.dtype.string:
                var = make_fake_coord(dim_coord_dim,
                                      dim_to_shape[dim_coord_dim],
                                      unit=data_array.coords[dim].unit)
                form = ticker.FuncFormatter(
                    lambda val, pos: self.scipp_obj_dict[name].coords[
                        dim].values[int(val)] if (int(val) >= 0 and int(
                            val) < dim_to_shape[dim_coord_dim]) else "")
                formatter.update({False: form, True: form})
                locator[False] = ticker.MaxNLocator(integer=True)
                if dim != dim_coord_dim:
                    underlying_dim = dim_coord_dim

            elif dim != dim_coord_dim:
                # non-dimension coordinate
                non_dimension_coord = True
                if dim_coord_dim in data_array.coords:
                    var = data_array.coords[dim_coord_dim]
                else:
                    var = make_fake_coord(dim_coord_dim,
                                          dim_to_shape[dim_coord_dim])
                underlying_dim = dim_coord_dim
                form = ticker.FuncFormatter(
                    lambda val, pos: value_to_string(data_array.coords[
                        dim].values[np.abs(var.values - val).argmin()]))
                formatter.update({False: form, True: form})

            else:
                var = data_array.coords[dim]

        else:
            # dim not found in data_array.coords
            var = make_fake_coord(dim, dim_to_shape[dim])

        label = var
        if non_dimension_coord:
            label = data_array.coords[dim]

        return underlying_dim, var, label, formatter, locator
Example #8
0
    def axis_label_and_ticks(self, axis, data_array, name):
        """
        Get dimensions and label (if present) from requested axis.
        Also retun axes tick formatters and locators.
        """

        # Create some default axis tick formatter, depending on whether log
        # for that axis will be True or False
        formatter = {
            False: ticker.ScalarFormatter(),
            True: ticker.LogFormatterSciNotation()
        }
        locator = {False: ticker.AutoLocator(), True: ticker.LogLocator()}

        dim = axis
        # # Convert to Dim object?
        # if isinstance(dim, str):
        #     dim = Dim(dim)
        # print(dim, data_array["coords"])

        if dim in data_array["coords"]:

            dim_coord_dim = data_array["coords"][dim]["dims"][0]
            tp = data_array["coords"][dim]["dtype"]

            if tp == "vector_3_float64":
                var = make_fake_coord(dim,
                                      self.shapes[name][dim],
                                      unit=data_array["coords"][dim]["unit"])
                form = ticker.FuncFormatter(lambda val, pos: "(" + ",".join([
                    value_to_string(item, precision=2) for item in self.
                    scipp_obj_dict[name]["coords"][dim]["values"][int(val)]
                ]) + ")" if (int(val) >= 0 and int(val) < self.shapes[name][
                    dim]) else "")
                formatter.update({False: form, True: form})
                locator[False] = ticker.MaxNLocator(integer=True)

            elif tp == "str":
                var = make_fake_coord(dim,
                                      self.shapes[name][dim],
                                      unit=data_array["coords"][dim]["unit"])
                form = ticker.FuncFormatter(
                    lambda val, pos: self.scipp_obj_dict[name]["coords"][dim][
                        "values"][int(val)] if (int(val) >= 0 and int(
                            val) < self.shapes[name][dim]) else "")
                formatter.update({False: form, True: form})
                locator[False] = ticker.MaxNLocator(integer=True)

            elif dim != dim_coord_dim:  # non-dimension coordinate
                var = data_array["coords"][dim_coord_dim]
                form = ticker.FuncFormatter(lambda val, pos: value_to_string(
                    data_array["coords"][dim]["values"][np.abs(data_array[
                        "coords"][dim_coord_dim]["values"] - val).argmin()]))
                formatter.update({False: form, True: form})
            else:
                var = data_array["coords"][dim]

        else:
            # dim not found in data_array["coords"]
            var = make_fake_coord(dim, self.shapes[name][dim])

        return dim, var, formatter, locator
Example #9
0
def main():
    parser = argparse.ArgumentParser(description='plotter')
    parser.add_argument('--fig_length',
                        type=int,
                        default=6,
                        help='matplotlib figure length (default: 6)')
    parser.add_argument('--fig_width',
                        type=int,
                        default=6,
                        help='matplotlib figure width (default: 6)')
    parser.add_argument('--style',
                        default='seaborn',
                        help='matplotlib figure style (default: seaborn)')
    parser.add_argument('--title',
                        default=None,
                        help='matplotlib figure title (default: None)')
    parser.add_argument('--xlabel',
                        default=None,
                        help='matplotlib figure xlabel')
    parser.add_argument('--xkey',
                        default='l',
                        help='x-axis key in csv file (default: l)')
    parser.add_argument('--ykey',
                        default='r',
                        help='y-axis key in csv file (default: r)')
    parser.add_argument('--smooth',
                        type=int,
                        default=10,
                        help='smooth radius of y axis (default: 10)')
    parser.add_argument('--ylabel',
                        default=None,
                        help='matplotlib figure ylabel')
    parser.add_argument(
        '--avg_group',
        action='store_true',
        help='average the curves in the same group and plot the mean.')
    parser.add_argument(
        '--shaded_std',
        action='store_true',
        help='shaded region corresponding to standard deviation of the group')
    parser.add_argument(
        '--shaded_err',
        action='store_true',
        help='shaded region corresponding to error in mean estimate')
    parser.add_argument('--legend_outside',
                        action='store_true',
                        default=False,
                        help='place the legend outside of the figure')

    parser.add_argument(
        '--time',
        action='store_true',
        help=
        'enable this will set x_key to t, and activate parameters about time')
    parser.add_argument(
        '--time_unit',
        default='h',
        help='parameters about time, x axis time unit (default: h)')
    parser.add_argument(
        '--time_interval',
        type=float,
        default=1,
        help='parameters about time, x axis time interval (default: 1)')

    parser.add_argument('--xformat', default='eng', help='x-axis format')
    parser.add_argument('--xlim',
                        type=int,
                        default=None,
                        help='x-axis limitation (default: None)')

    parser.add_argument('--log_dir',
                        default='./',
                        help='log dir (default: ./)')
    parser.add_argument('--filename', default='monitor', help='csv filename')
    parser.add_argument('--show', action='store_true', help='show figure')
    parser.add_argument('--save', action='store_true', help='save figure')
    parser.add_argument('--dpi',
                        type=int,
                        default=400,
                        help='figure dpi (default: 400)')
    args = parser.parse_args()

    xscale = 1
    if args.time:
        if args.xlabel is None:
            args.xlabel = 'Training time'
        args.xkey = 't'
        if args.time_unit == 'h':
            xscale = 60 * 60
            args.time_interval = 2
        elif args.time_unit == 'min':
            xscale = 60
            args.time_interval = 20
    else:
        if args.xlabel is None:
            args.xlabel = 'Timesteps'

    if args.ylabel is None:
        args.ylabel = 'Episode Reward'

    allresults = pu.load_results(args.log_dir, filename=args.filename)
    pu.plot_results(allresults,
                    fig_length=args.fig_length,
                    fig_width=args.fig_width,
                    style=args.style,
                    title=args.title,
                    xlabel=args.xlabel,
                    ylabel=args.ylabel,
                    xkey=args.xkey,
                    ykey=args.ykey,
                    xscale=xscale,
                    smooth_radius=args.smooth,
                    average_group=args.avg_group,
                    shaded_std=args.shaded_std,
                    shaded_err=args.shaded_err,
                    legend_outside=args.legend_outside)

    ax = plt.gca()  # get current axis
    if args.time:
        if args.time_unit == 'h' or args.time_unit == 'min':
            ax.xaxis.set_major_locator(
                mticker.MultipleLocator(args.time_interval))
        ax.xaxis.set_major_formatter(
            mticker.FormatStrFormatter("%d" + args.time_unit))
    else:
        if args.xformat == 'eng':
            ax.xaxis.set_major_formatter(mticker.EngFormatter())
        elif args.xformat == 'log':
            ax.xaxis.set_major_formatter(mticker.LogFormatter())
        elif args.xformat == 'sci':
            ax.xaxis.set_major_formatter(mticker.LogFormatterSciNotation())

    if args.xlim is not None:
        plt.xlim((0, args.xlim))

    if args.save:
        plt.savefig(args.log_dir + 'figure', dpi=args.dpi, bbox_inches='tight')
    if args.show:
        plt.show()
Example #10
0
 def FormatAxis(self, ax2fmt, scale):
     if ax2fmt == 'x':
         setScale = self.ax.set_xscale
         curAxis = self.ax.xaxis
         curData = self.xData
     elif ax2fmt == 'y':
         setScale = self.ax.set_yscale
         curAxis = self.ax.yaxis
         curData = self.yData
     setScale(scale)
     if scale == 'linear':
         self.ax.ticklabel_format(style='sci',
                                  axis=ax2fmt,
                                  scilimits=(0, 0),
                                  useMathText=True)
         curAxis.set_minor_locator(tck.AutoMinorLocator())
         self.ax.relim()
         self.ax.autoscale(True, ax2fmt, None)
     elif scale == 'log':
         curAxis.set_minor_locator(tck.LogLocator(subs=numpy.arange(2, 10)))
         logFmt = tck.LogFormatterSciNotation(base=10,
                                              labelOnlyBase=False,
                                              minor_thresholds=(4, 1))
         curAxis.set_minor_formatter(logFmt)
         self.ax.relim()
         self.ax.autoscale(True, ax2fmt, None)
     elif scale == 'symlog':
         axMin = min(abs(curData[curData != 0]))
         axMax = max(abs(curData))
         axRange = numpy.log10(axMax / axMin)
         if ax2fmt == 'x':
             setScale('symlog',
                      basex=10,
                      subsx=numpy.arange(2, 10),
                      linthreshx=axMin * 10**(axRange / 2))
         elif ax2fmt == 'y':
             setScale('symlog',
                      basey=10,
                      subsy=numpy.arange(2, 10),
                      linthreshy=axMin * 10**(axRange / 2))
         # Thomas Duvernay, 06/01/19
         # There seems to be a bug with the labelling of the 0 tick
         # when a 'symlog' is used as an axis scale. It looks like
         # it is considered as a minor tick.
         symLogLoc = tck.SymmetricalLogLocator(subs=numpy.arange(2, 10),
                                               linthresh=axMin *
                                               10**(axRange / 2),
                                               base=10)
         curAxis.set_minor_locator(symLogLoc)
         logFmt = tck.LogFormatterSciNotation(base=10,
                                              labelOnlyBase=False,
                                              minor_thresholds=(4, 1),
                                              linthresh=axMin *
                                              10**(axRange / 2))
         curAxis.set_minor_formatter(logFmt)
     self.ax.set_xlabel(self.xCombo.get_child().get_text(),
                        fontweight='bold',
                        fontsize=20)
     self.ax.set_ylabel(self.yCombo.get_child().get_text(),
                        fontweight='bold',
                        fontsize=20)
     self.ax.tick_params(which='major', length=7, labelsize=16, width=2)
     self.ax.tick_params(which='minor',
                         length=4,
                         labelsize=10,
                         width=2,
                         colors='xkcd:scarlet',
                         labelrotation=45)
     self.ax.xaxis.get_offset_text().set(fontsize=13,
                                         fontweight='bold',
                                         color='xkcd:black')
     self.ax.yaxis.get_offset_text().set(fontsize=13,
                                         fontweight='bold',
                                         color='xkcd:black')
     self.fig.set_tight_layout(True)
Example #11
0
def dist_stitch(df, bins, filepath, cfg):
    all_columns = list(df.index.names)
    columns_noproc = [c for c in all_columns if c != "process"]
    columns_nobins = [c for c in all_columns if "bin" not in c]
    columns_nobins_noproc = [c for c in columns_nobins if c != "process"]

    # Remove under and overflow bins (add overflow into final bin)
    bins = np.array(bins[0][1:-1])

    df_pivot_proc = df.pivot_table(
        values='yield',
        index=columns_noproc,
        columns='process',
        aggfunc=np.sum,
    )

    # Ratio
    y = np.log10(df_pivot_proc.sum(axis=1))
    df_ratio = 0.5*(np.roll(y, 1) + np.roll(y, -1))/y
    df_ratio.iloc[0] = 1.
    df_ratio.iloc[-1] = 1.

    # Ratio uncertainty
    df_pivot_proc_var = df.pivot_table(
        values = 'variance',
        index = columns_noproc,
        columns = 'process',
        aggfunc = np.sum,
    )
    df_ratio_err = np.sqrt(df_pivot_proc_var.sum(axis=1))/df_pivot_proc.sum(axis=1)

    # Split axis into top and bottom with ratio 3:1
    # Share the x axis, not the y axis
    # Figure size is 4.8 by 6.4 inches
    fig, (axtop, axbot) = plt.subplots(
        nrows=2, ncols=1, sharex='col', sharey=False,
        gridspec_kw={'height_ratios': [3, 1]},
        figsize = (4.8, 6.4),
    )
    if cfg.log:
        locmin = ticker.LogLocator(
            base = 10.0,
            subs = (0.1,0.2,0.4,0.6,0.8,1,2,4,6,8,10),
        )
        axtop.yaxis.set_minor_locator(locmin)
        axtop.yaxis.set_minor_formatter(ticker.NullFormatter())
        axtop.yaxis.set_major_formatter(ticker.LogFormatterSciNotation())
        axtop.set_yscale('log')

    # Get the global bins
    xlow = df_pivot_proc.index.get_level_values("bin0_low").values
    xupp = df_pivot_proc.index.get_level_values("bin0_upp").values
    xcenters = (xupp + xlow)/2

    # Stacked plot
    sorted_processes = list(df_pivot_proc.sum().sort_values(ascending=False).index)
    df_pivot_proc = df_pivot_proc.fillna(0)
    axtop.hist(
        [xcenters]*len(sorted_processes),
        bins = bins,
        weights = [df_pivot_proc[proc].values for proc in sorted_processes],
        histtype='step',
        stacked = True,
        color = [cfg.sample_colours.get(proc, "blue")
                 for proc in sorted_processes],
        label = sorted_processes,
    )
    axtop.set_xlim(bins[0], bins[-1])

    # Add CMS text to top + energy + lumi
    axtop.text(0, 1, r'$\mathbf{CMS}\ \mathit{Preliminary}$',
               horizontalalignment='left',
               verticalalignment='bottom',
               transform=axtop.transAxes,
               fontsize='large')
    axtop.text(1, 1, r'$35.9\ \mathrm{fb}^{-1}(13\ \mathrm{TeV})$',
               horizontalalignment='right',
               verticalalignment='bottom',
               transform=axtop.transAxes,
               fontsize='large')

    # Legend - reverse the labels
    handles, labels = axtop.get_legend_handles_labels()
    handles = handles[::-1]
    labels = labels[::-1]
    labels = [cfg.sample_names.get(l, l) for l in labels]
    axtop.legend(handles, labels)

    # Ratio plot
    if df_ratio.shape[0] > 2:
        df_ratio.iloc[-2] = 1.
    axbot.hist(
        xcenters,
        bins = bins,
        weights = df_ratio,
        color = "black",
        histtype = 'step',
    )

    axbot.fill_between(
        xlow,
        1. - df_ratio_err.values,
        1. + df_ratio_err.values,
        step = 'post',
        color = "#aaaaaa",
        label = "MC stat. unc.",
    )
    ylim_ratio = max(1.-df_ratio.min(), df_ratio.max()-1.)*1.1
    ylim_ratio_err = df_ratio_err.max()*1.1
    ylim = max(ylim_ratio, ylim_ratio_err)
    if not (np.isinf(ylim) or np.isnan(ylim)):
        axbot.set_ylim((1.-ylim, 1.+ylim))
    axbot.axhline(1., ls=':', color='black')

    # Add labels
    name = cfg.name
    axbot.set_xlabel(cfg.axis_label.get(name, name),
                     fontsize='large')
    axbot.set_ylabel("Closure", fontsize='large')

    # Legend
    handles, labels = axbot.get_legend_handles_labels()
    axbot.legend(handles, labels)

    # Report
    print("Creating {}.pdf".format(filepath))

    # Actually save the figure
    plt.tight_layout()
    fig.savefig(filepath+".pdf", format="pdf", bbox_inches="tight")
    plt.close(fig)

    return df
Example #12
0
def zonal_pcolormesh(yy,zz,arr,cnum=20,clabelsize=14,extend='both',\
            cmap='viridis',alpha=1,subs=(1,),
            dlat=15.0,labelsize=14,scale='normal',vmin=None,vmax=None,
            na_values=-999,replace_nan=False,
            timestep=0,xsel='mean',cyclic=False):
    """
    plot zonal pcolormesh

    Parameters
    ----------
    yy,zz  :array_like
    xsel   :int or string, deafult 'mean'
        index of logitude
    arr    :array_like
        input data
    cnum    :int or float
        the number of contour
    powerlimits:tuple
        exponent range
    subs   :sequence of float or {'all','auto',None}
        default=(1,) ex) (1.0,2.0) 

    Returns
    -------
    fig :matplotlib.Figure
        figure object for plotting
    ax :matplotlib.Axes
        axes object for plotting
    cbar :matplotlib.Colorbar
        colorbar object for contour
    """
    sformat.set_powerlimits((-1, 3))
    fig = plt.figure(figsize=(9, 6), facecolor='w')
    ax = fig.add_subplot(1, 1, 1)
    cax = fig.add_axes([0.25, 0, 0.5, 0.05])
    if isinstance(arr, Gtool3d):
        dat = arr.getarr(
            cyclic=cyclic,
            timestep=timestep,
        )[:, :, :]
    else:
        dat = arr[:, :, :]
    if xsel == 'mean':
        dat = np.nanmean(dat[:, :, :], axis=2)
    else:
        dat = dat[:, :, xsel]
    set_latticks(ax=ax, dlat=dlat, labelsize=labelsize)
    if vmin is None:
        vmin = np.nanmin(dat)
    if vmax is None:
        vmax = np.nanmax(dat)
    norm = {'normal': None, 'log': LogNorm(vmin=vmin, vmax=vmax)}
    cf = ax.pcolormesh(yy, zz, dat, cmap=cmap, norm=norm[scale], alpha=alpha)
    if scale == 'log':
        cbar = fig.colorbar(cf,
                            cax,
                            ticks=ticker.LogLocator(subs=subs),
                            extend=extend,
                            format=ticker.LogFormatterSciNotation(),
                            orientation='horizontal')
    else:
        cbar = fig.colorbar(cf, cax, extend=extend, orientation='horizontal')
    cbar.ax.tick_params(labelsize=clabelsize)
    cbar.ax.xaxis.offsetText.set_fontsize(14)
    ax.set_ylim(1, 0)
    ax.grid()
    return fig, ax, cbar
Example #13
0
def pcolormesh(xx,yy,arr,subs=(1.0,),clabelsize=14,\
    vmin=None,vmax=None,scale='normal',\
    cmap='viridis',alpha=1,extend='both',offsetTextsize=14,\
    na_values=-999,replace_nan=False,
    timestep=0,cyclic=False,zsel=0):
    """
    pcolormesh for geopositional data

    Parameters
    -----------
    xx,yy  :array_like
    arr    :Gtool2d or Gtool3d or numpy.ndarray
        model data
    subs   :{sequence of float, 'all','auto',None}
        default=(1,) ex) (1.0,2.0) 
    cmap   :string
        colormap
    vmin   :float
        minimum value,default arr.min()
    vmax   :float
        maximum value,default arr.max()
    alpha  :float, default 1
    extend :string
        {'max','min','both','neither'}
    scale  :string
        {'normal','log'}

    Returns
    ---------
    fig :matplotlib.Figure
        Figure object for plot
    ax  :matplotlib.axes
        Axes object for plot
    cbar:matplotlib.colorbar
        colorbar object for contour
    """

    fig = plt.figure(figsize=(10, 6), facecolor='w')
    ax = fig.add_subplot(1,
                         1,
                         1,
                         projection=ccrs.PlateCarree(central_longitude=180))
    cax = fig.add_axes([0.25, 0, 0.5, 0.05])
    ax = ckit.set_geogrid(ax)
    dat = isgtoolinstance(arr,
                          timestep=timestep,
                          cyclic=cyclic,
                          zsel=zsel,
                          replace_nan=replace_nan,
                          na_values=na_values)
    if vmin is None:
        vmin = np.nanmin(dat)
    if vmax is None:
        vmax = np.nanmax(dat)
    norm = {'normal': None, 'log': LogNorm(vmin=vmin, vmax=vmax)}
    cf = ax.pcolormesh(xx,
                       yy,
                       dat,
                       cmap=cmap,
                       alpha=alpha,
                       norm=norm[scale],
                       transform=ccrs.PlateCarree())
    if scale == 'log':
        cbar = fig.colorbar(cf,
                            cax,
                            extend=extend,
                            ticks=ticker.LogLocator(subs=subs),
                            format=ticker.LogFormatterSciNotation(),
                            orientation='horizontal')
    else:
        cbar = fig.colorbar(cf, cax, extend=extend, orientation='horizontal')
    cbar.ax.tick_params(labelsize=clabelsize)
    cbar.ax.xaxis.offsetText.set_fontsize(offsetTextsize)
    return fig, ax, cbar
Example #14
0
def logcontourf(xx,yy,z,subs=(1.0,),clabelsize=14,\
    vmin=None,vmax=None,\
    cmap='viridis',alpha=1,extend='both',offsetTextsize=14,\
    na_values=-999,replace_nan=False,
    timestep=0,cyclic=False,zsel=0):
    """
    plot log-scaled contourf for geopositional data

    Parameters
    ----------
    xx,yy :array_like
        The coordinates of the values in z
    z :Gtool2d or Gtool3d or numpy.ndarray
        model data
    subs :sequence of float, 'all','auto',None
        default=(1,) ex) (1.0,2.0) 
    cmap   :string, default 'viridis'
        colormap
    vmin   :float
        minimum value,default=arr.min()
    vmax   :float
        maximum value,default=arr.max()
    alpha  :float, default 1
    extend :string
        {'max','min','both','neither'}
    timestep:int, default 0
        model timestep
    zsel :int, default 0
        select model layer
    cyclic:bool, default False
        Whether make logitude cyclic or not

    Returns
    --------
    fig :matplotlib.Figure
        Figure object for plot
    ax  :matplotlib.axes
        Axes object for plot
    cbar:matplotlib.colorbar
        colorbar object for contour
    """
    fig = plt.figure(figsize=(10, 6), facecolor='w')
    ax = fig.add_subplot(1,
                         1,
                         1,
                         projection=ccrs.PlateCarree(central_longitude=180))
    cax = fig.add_axes([0.25, 0, 0.5, 0.05])
    ax = ckit.set_geogrid(ax)
    dat = isgtoolinstance(arr,
                          timestep=timestep,
                          cyclic=cyclic,
                          zsel=zsel,
                          na_values=na_values,
                          replace_nan=replace_nan)
    if vmin is None:
        vmin = np.nanmin(dat)
    if vmax is None:
        vmax = np.nanmax(dat)

    cf = ax.contourf(xx,
                     yy,
                     dat,
                     vmin=vmin,
                     vmax=vmax,
                     cmap=cmap,
                     extend=extend,
                     alpha=alpha,
                     locator=ticker.LogLocator(subs=subs),
                     transform=ccrs.PlateCarree())
    cbar = fig.colorbar(cf,
                        cax,
                        extend=extend,
                        ticks=ticker.LogLocator(subs=(1.0, )),
                        format=ticker.LogFormatterSciNotation(),
                        orientation='horizontal')
    cbar.ax.tick_params(labelsize=clabelsize)
    cbar.ax.xaxis.offsetText.set_fontsize(offsetTextsize)
    return fig, ax, cbar
Example #15
0
def plot_scaling(algorithms, by_data_type_and_algorithm, output_pgf):
    data_type_means = []
    for row, data_type in enumerate(DATA_TYPES):
        means = []
        algo_means_dict = defaultdict(list)
        for algo, results_by_tunable in by_data_type_and_algorithm[
                data_type].items():
            max_tunable_results = results_by_tunable[max(
                results_by_tunable.keys())]
            if len(max_tunable_results) > 1:
                for num_threads, results in max_tunable_results.items():
                    throughput_stats = {
                        op: ThroughputStats(results, op)
                        for op in OPERATIONS
                    }
                    means.append((algo, num_threads, throughput_stats))
                    algo_means_dict[algo].append(
                        (num_threads, throughput_stats))

        means.sort(key=itemgetter(0, 1))
        for v in algo_means_dict.values():
            v.sort(key=itemgetter(0))
        algo_means = sorted(algo_means_dict.items(), key=itemgetter(0))

        print(f'({data_type})')
        print(
            tabulate([[
                f'{a} {u}', *('{:,.0f} ± {:>3,.0f} MB/s'.format(
                    t[o].mean * 1e-6, t[o].h95 * 1e-6) for o in OPERATIONS)
            ] for a, u, t in means],
                     headers=['algorithm', *OPERATIONS],
                     stralign='right',
                     disable_numparse=True))
        print()

        data_type_means.append((data_type, algo_means))

    fig, axes = plt.subplots(len(DATA_TYPES), len(OPERATIONS), figsize=(10, 6))
    fig.subplots_adjust(top=0.92,
                        bottom=0.1,
                        left=0.08,
                        right=0.88,
                        wspace=0.2,
                        hspace=0.35)
    algorithm_colors = dict(zip(sorted(algorithms), PALETTE))
    for row, (data_type, algo_means) in enumerate(data_type_means):
        for col, operation in enumerate(OPERATIONS):
            ax = axes[row, col]
            throughput_values = []
            for algo, results in algo_means:
                points = [(threads, t[operation].mean, t[operation].h95)
                          for threads, t in results]
                threads, throughputs, h95s = zip(*points)
                throughput_values += throughputs
                ax.errorbar(threads,
                            throughputs,
                            label=f'{algo} {data_type} {operation}',
                            yerr=h95s,
                            marker='o')
            ax.set_title(f'{data_type} {operation}')
            ax.set_xscale('log')
            ax.xaxis.set_major_formatter(ticker.ScalarFormatter())
            ax.xaxis.set_minor_formatter(ticker.ScalarFormatter())
            ax.set_yscale('log')
            if throughput_values:
                start, stop = min(throughput_values) / 2, max(
                    throughput_values) * 2
                ax.set_ylim(start, stop)
                ax.yaxis.set_minor_formatter(
                    ticker.LogFormatterSciNotation(minor_thresholds=(2, 0.5)))
            ax.set_xlabel('number of threads')
            ax.set_ylabel('arithmetic mean uncompressed throughput [B/s]')

    fig.legend(handles=[
        patches.Patch(color=c, label=a)
        for a, c in sorted(algorithm_colors.items(), key=itemgetter(0))
    ],
               loc='center right')

    if output_pgf:
        plt.savefig('scaling.pgf')
    else:
        plt.show()