Пример #1
0
                    resolution='i')

        m.drawcoastlines(linewidth=0.5)
        m.drawcountries(linewidth=0.5)
        parallels = np.arange(10., 40, 2.)
        # labels = [left,right,top,bottom]
        m.drawparallels(parallels, labels=[False, True, True, False],
                        linewidth=0.5)
        meridians = np.arange(-20., 17., 4.)
        m.drawmeridians(meridians, labels=[True, False, False, True],
                        linewidth=0.5)

        min = 0
        max = 70000

        levels = MaxNLocator(nbins=15).tick_values(min, max)

        # discrete_cmap = utilities.cmap_discretize(cm.RdYlBu_r, 10)
        cmap = cm.get_cmap('RdYlBu_r')
        norm = BoundaryNorm(levels, ncolors=cmap.N, clip=True)

        # m.pcolormesh(lons, lats, correlation_array, extent=extent,
        #             origin='lower',
        #         interpolation='none',
        #         cmap=cmap, vmin=min, vmax=max+1)

        data_array = np.ma.masked_where(np.isnan(data_array),
                                        data_array)

        m.pcolormesh(target_lons, target_lats, data_array,
                     cmap=cmap, norm=norm, vmin=min, vmax=max)
def plot_partial_dependence(gbrt, X, features, feature_names=None,
                            label=None, n_cols=3, grid_resolution=100,
                            percentiles=(0.05, 0.95), n_jobs=1,
                            verbose=0, ax=None, line_kw=None,
                            contour_kw=None, **fig_kw):
    """Partial dependence plots for ``features``.

    The ``len(features)`` plots are arranged in a grid with ``n_cols``
    columns. Two-way partial dependence plots are plotted as contour
    plots.

    Parameters
    ----------
    gbrt : BaseGradientBoosting
        A fitted gradient boosting model.
    X : array-like, shape=(n_samples, n_features)
        The data on which ``gbrt`` was trained.
    features : seq of tuples or ints
        If seq[i] is an int or a tuple with one int value, a one-way
        PDP is created; if seq[i] is a tuple of two ints, a two-way
        PDP is created.
    feature_names : seq of str
        Name of each feature; feature_names[i] holds
        the name of the feature with index i.
    label : object
        The class label for which the PDPs should be computed.
        Only if gbrt is a multi-class model. Must be in ``gbrt.classes_``.
    n_cols : int
        The number of columns in the grid plot (default: 3).
    percentiles : (low, high), default=(0.05, 0.95)
        The lower and upper percentile used create the extreme values
        for the PDP axes.
    grid_resolution : int, default=100
        The number of equally spaced points on the axes.
    n_jobs : int
        The number of CPUs to use to compute the PDs. -1 means 'all CPUs'.
        Defaults to 1.
    verbose : int
        Verbose output during PD computations. Defaults to 0.
    ax : Matplotlib axis object, default None
        An axis object onto which the plots will be drawn.
    line_kw : dict
        Dict with keywords passed to the ``pylab.plot`` call.
        For one-way partial dependence plots.
    contour_kw : dict
        Dict with keywords passed to the ``pylab.plot`` call.
        For two-way partial dependence plots.
    fig_kw : dict
        Dict with keywords passed to the figure() call.
        Note that all keywords not recognized above will be automatically
        included here.

    Returns
    -------
    fig : figure
        The Matplotlib Figure object.
    axs : seq of Axis objects
        A seq of Axis objects, one for each subplot.

    Examples
    --------
    >>> from sklearn.datasets import make_friedman1
    >>> from sklearn.ensemble import GradientBoostingRegressor
    >>> X, y = make_friedman1()
    >>> clf = GradientBoostingRegressor(n_estimators=10).fit(X, y)
    >>> fig, axs = plot_partial_dependence(clf, X, [0, (0, 1)]) #doctest: +SKIP
    ...
    """
    import matplotlib.pyplot as plt
    from matplotlib import transforms
    from matplotlib.ticker import MaxNLocator
    from matplotlib.ticker import ScalarFormatter

    if not isinstance(gbrt, BaseGradientBoosting):
        raise ValueError('gbrt has to be an instance of BaseGradientBoosting')
    if gbrt.estimators_.shape[0] == 0:
        raise ValueError('Call %s.fit before partial_dependence' %
                         gbrt.__class__.__name__)

    # set label_idx for multi-class GBRT
    if hasattr(gbrt, 'classes_') and np.size(gbrt.classes_) > 2:
        if label is None:
            raise ValueError('label is not given for multi-class PDP')
        label_idx = np.searchsorted(gbrt.classes_, label)
        if gbrt.classes_[label_idx] != label:
            raise ValueError('label %s not in ``gbrt.classes_``' % str(label))
    else:
        # regression and binary classification
        label_idx = 0

    X = array2d(X, dtype=DTYPE, order='C')
    if gbrt.n_features != X.shape[1]:
        raise ValueError('X.shape[1] does not match gbrt.n_features')

    if line_kw is None:
        line_kw = {'color': 'green'}
    if contour_kw is None:
        contour_kw = {}

    # convert feature_names to list
    if feature_names is None:
        # if not feature_names use fx indices as name
        feature_names = map(str, range(gbrt.n_features))
    elif isinstance(feature_names, np.ndarray):
        feature_names = feature_names.tolist()

    def convert_feature(fx):
        if isinstance(fx, six.string_types):
            try:
                fx = feature_names.index(fx)
            except ValueError:
                raise ValueError('Feature %s not in feature_names' % fx)
        return fx

    # convert features into a seq of int tuples
    tmp_features = []
    for fxs in features:
        if isinstance(fxs, (numbers.Integral,) + six.string_types):
            fxs = (fxs,)
        try:
            fxs = np.array([convert_feature(fx) for fx in fxs], dtype=np.int32)
        except TypeError:
            raise ValueError('features must be either int, str, or tuple '
                             'of int/str')
        if not (1 <= np.size(fxs) <= 2):
            raise ValueError('target features must be either one or two')

        tmp_features.append(fxs)

    features = tmp_features

    names = []
    try:
        for fxs in features:
            names.append([feature_names[i] for i in fxs])
    except IndexError:
        raise ValueError('features[i] must be in [0, n_features) '
                         'but was %d' % i)

    # compute PD functions
    pd_result = Parallel(n_jobs=n_jobs, verbose=verbose)(
        delayed(partial_dependence)(gbrt, fxs, X=X,
                                    grid_resolution=grid_resolution)
        for fxs in features)

    # get global min and max values of PD grouped by plot type
    pdp_lim = {}
    for pdp, axes in pd_result:
        min_pd, max_pd = pdp[label_idx].min(), pdp[label_idx].max()
        n_fx = len(axes)
        old_min_pd, old_max_pd = pdp_lim.get(n_fx, (min_pd, max_pd))
        min_pd = min(min_pd, old_min_pd)
        max_pd = max(max_pd, old_max_pd)
        pdp_lim[n_fx] = (min_pd, max_pd)

    # create contour levels for two-way plots
    if 2 in pdp_lim:
        Z_level = np.linspace(*pdp_lim[2], num=8)

    if ax is None:
        fig = plt.figure(**fig_kw)
    else:
        fig = ax.get_figure()
        fig.clear()

    n_cols = min(n_cols, len(features))
    n_rows = int(np.ceil(len(features) / float(n_cols)))
    axs = []
    for i, fx, name, (pdp, axes) in zip(count(), features, names,
                                        pd_result):
        ax = fig.add_subplot(n_rows, n_cols, i + 1)

        if len(axes) == 1:
            ax.plot(axes[0], pdp[label_idx].ravel(), **line_kw)
        else:
            # make contour plot
            assert len(axes) == 2
            XX, YY = np.meshgrid(axes[0], axes[1])
            Z = pdp[label_idx].reshape(map(np.size, axes)).T
            CS = ax.contour(XX, YY, Z, levels=Z_level, linewidths=0.5,
                            colors='k')
            ax.contourf(XX, YY, Z, levels=Z_level, vmax=Z_level[-1],
                        vmin=Z_level[0], alpha=0.75, **contour_kw)
            ax.clabel(CS, fmt='%2.2f', colors='k', fontsize=10, inline=True)

        # plot data deciles + axes labels
        deciles = mquantiles(X[:, fx[0]], prob=np.arange(0.1, 1.0, 0.1))
        trans = transforms.blended_transform_factory(ax.transData,
                                                     ax.transAxes)
        ylim = ax.get_ylim()
        ax.vlines(deciles, [0], 0.05, transform=trans, color='k')
        ax.set_xlabel(name[0])
        ax.set_ylim(ylim)

        # prevent x-axis ticks from overlapping
        ax.xaxis.set_major_locator(MaxNLocator(nbins=6, prune='lower'))
        tick_formatter = ScalarFormatter()
        tick_formatter.set_powerlimits((-3, 4))
        ax.xaxis.set_major_formatter(tick_formatter)

        if len(axes) > 1:
            # two-way PDP - y-axis deciles + labels
            deciles = mquantiles(X[:, fx[1]], prob=np.arange(0.1, 1.0, 0.1))
            trans = transforms.blended_transform_factory(ax.transAxes,
                                                         ax.transData)
            xlim = ax.get_xlim()
            ax.hlines(deciles, [0], 0.05, transform=trans, color='k')
            ax.set_ylabel(name[1])
            # hline erases xlim
            ax.set_xlim(xlim)
        else:
            ax.set_ylabel('Partial dependence')

        if len(axes) == 1:
            ax.set_ylim(pdp_lim[1])
        axs.append(ax)

    fig.subplots_adjust(bottom=0.15, top=0.7, left=0.1, right=0.95, wspace=0.4,
                        hspace=0.3)
    return fig, axs
Пример #3
0
def plot_cartopy(lons, lats, size, color, labels=None, projection='global',
                 resolution='110m', continent_fill_color='0.8',
                 water_fill_color='1.0', colormap=None, colorbar=None,
                 marker="o", title=None, colorbar_ticklabel_format=None,
                 show=True, proj_kwargs=None, **kwargs):  # @UnusedVariable
    """
    Creates a Cartopy plot with a data point scatter plot.

    :type lons: list/tuple of floats
    :param lons: Longitudes of the data points.
    :type lats: list/tuple of floats
    :param lats: Latitudes of the data points.
    :type size: float or list/tuple of floats
    :param size: Size of the individual points in the scatter plot.
    :type color: list/tuple of floats (or objects that can be
        converted to floats, like e.g.
        :class:`~obspy.core.utcdatetime.UTCDateTime`)
    :param color: Color information of the individual data points to be
        used in the specified color map (e.g. origin depths,
        origin times).
    :type labels: list/tuple of str
    :param labels: Annotations for the individual data points.
    :type projection: str, optional
    :param projection: The map projection.
        Currently supported are:

            * ``"global"`` (Will plot the whole world using
              :class:`~cartopy.crs.Mollweide`.)
            * ``"ortho"`` (Will center around the mean lat/long using
              :class:`~cartopy.crs.Orthographic`.)
            * ``"local"`` (Will plot around local events using
              :class:`~cartopy.crs.AlbersEqualArea`.)
            * Any other Cartopy :class:`~cartopy.crs.Projection`. An instance
              of this class will be created using the supplied ``proj_kwargs``.

        Defaults to "global"
    :type resolution: str, optional
    :param resolution: Resolution of the boundary database to use. Will be
        passed directly to the Cartopy module. Possible values are:

            * ``"110m"``
            * ``"50m"``
            * ``"10m"``

        Defaults to ``"110m"``. For compatibility, you may also specify any of
        the Basemap resolutions defined in :func:`plot_basemap`.
    :type continent_fill_color: Valid matplotlib color, optional
    :param continent_fill_color:  Color of the continents. Defaults to
        ``"0.9"`` which is a light gray.
    :type water_fill_color: Valid matplotlib color, optional
    :param water_fill_color: Color of all water bodies.
        Defaults to ``"white"``.
    :type colormap: str, any matplotlib colormap, optional
    :param colormap: The colormap for color-coding the events as provided
        in `color` kwarg.
        The event with the smallest `color` property will have the
        color of one end of the colormap and the event with the highest
        `color` property the color of the other end with all other events
        in between.
        Defaults to None which will use the default matplotlib colormap.
    :type colorbar: bool, optional
    :param colorbar: When left `None`, a colorbar is plotted if more than one
        object is plotted. Using `True`/`False` the colorbar can be forced
        on/off.
    :type title: str
    :param title: Title above plot.
    :type colorbar_ticklabel_format: str or function or
        subclass of :class:`matplotlib.ticker.Formatter`
    :param colorbar_ticklabel_format: Format string or Formatter used to format
        colorbar tick labels.
    :type show: bool
    :param show: Whether to show the figure after plotting or not. Can be used
        to do further customization of the plot before showing it.
    :type proj_kwargs: dict
    :param proj_kwargs: Keyword arguments to pass to the Cartopy
        :class:`~cartopy.ccrs.Projection`. In this dictionary, you may specify
        ``central_longitude='auto'`` or ``central_latitude='auto'`` to have
        this function calculate the latitude or longitude as it would for other
        projections. Some arguments may be ignored if you choose one of the
        built-in ``projection`` choices.
    """
    import matplotlib.pyplot as plt

    if isinstance(color[0], (datetime.datetime, UTCDateTime)):
        datetimeplot = True
        color = [date2num(getattr(t, 'datetime', t)) for t in color]
    else:
        datetimeplot = False

    fig = plt.figure()

    # The colorbar should only be plotted if more then one event is
    # present.
    if colorbar is not None:
        show_colorbar = colorbar
    else:
        if len(lons) > 1 and hasattr(color, "__len__") and \
                not isinstance(color, (str, native_str)):
            show_colorbar = True
        else:
            show_colorbar = False

    if projection == "local":
        ax_x0, ax_width = 0.10, 0.80
    elif projection == "global":
        ax_x0, ax_width = 0.01, 0.98
    else:
        ax_x0, ax_width = 0.05, 0.90

    proj_kwargs = proj_kwargs or {}
    if projection == 'global':
        proj_kwargs['central_longitude'] = np.mean(lons)
        proj = ccrs.Mollweide(**proj_kwargs)
    elif projection == 'ortho':
        proj_kwargs['central_latitude'] = np.mean(lats)
        proj_kwargs['central_longitude'] = mean_longitude(lons)
        proj = ccrs.Orthographic(**proj_kwargs)
    elif projection == 'local':
        if min(lons) < -150 and max(lons) > 150:
            max_lons = max(np.array(lons) % 360)
            min_lons = min(np.array(lons) % 360)
        else:
            max_lons = max(lons)
            min_lons = min(lons)
        lat_0 = max(lats) / 2. + min(lats) / 2.
        lon_0 = max_lons / 2. + min_lons / 2.
        if lon_0 > 180:
            lon_0 -= 360
        deg2m_lat = 2 * np.pi * 6371 * 1000 / 360
        deg2m_lon = deg2m_lat * np.cos(lat_0 / 180 * np.pi)
        if len(lats) > 1:
            height = (max(lats) - min(lats)) * deg2m_lat
            width = (max_lons - min_lons) * deg2m_lon
            margin = 0.2 * (width + height)
            height += margin
            width += margin
        else:
            height = 2.0 * deg2m_lat
            width = 5.0 * deg2m_lon
        # Do intelligent aspect calculation for local projection
        # adjust to figure dimensions
        w, h = fig.get_size_inches()
        aspect = w / h
        if show_colorbar:
            aspect *= 1.2
        if width / height < aspect:
            width = height * aspect
        else:
            height = width / aspect

        proj_kwargs['central_latitude'] = lat_0
        proj_kwargs['central_longitude'] = lon_0
        proj = ccrs.AlbersEqualArea(**proj_kwargs)

    # User-supplied projection.
    elif isinstance(projection, type):
        if 'central_longitude' in proj_kwargs:
            if proj_kwargs['central_longitude'] == 'auto':
                proj_kwargs['central_longitude'] = mean_longitude(lons)
        if 'central_latitude' in proj_kwargs:
            if proj_kwargs['central_latitude'] == 'auto':
                proj_kwargs['central_latitude'] = np.mean(lats)
        if 'pole_longitude' in proj_kwargs:
            if proj_kwargs['pole_longitude'] == 'auto':
                proj_kwargs['pole_longitude'] = np.mean(lons)
        if 'pole_latitude' in proj_kwargs:
            if proj_kwargs['pole_latitude'] == 'auto':
                proj_kwargs['pole_latitude'] = np.mean(lats)

        proj = projection(**proj_kwargs)

    else:
        msg = "Projection '%s' not supported." % projection
        raise ValueError(msg)

    if show_colorbar:
        map_ax = fig.add_axes([ax_x0, 0.13, ax_width, 0.77], projection=proj)
        cm_ax = fig.add_axes([ax_x0, 0.05, ax_width, 0.05])
        plt.sca(map_ax)
    else:
        ax_y0, ax_height = 0.05, 0.85
        if projection == "local":
            ax_y0 += 0.05
            ax_height -= 0.05
        map_ax = fig.add_axes([ax_x0, ax_y0, ax_width, ax_height],
                              projection=proj)

    if projection == 'local':
        x0, y0 = proj.transform_point(lon_0, lat_0, proj.as_geodetic())
        map_ax.set_xlim(x0 - width / 2, x0 + width / 2)
        map_ax.set_ylim(y0 - height / 2, y0 + height / 2)
    else:
        map_ax.set_global()

    # Pick features at specified resolution.
    resolution = _CARTOPY_RESOLUTIONS[resolution]
    try:
        borders, land, ocean = _CARTOPY_FEATURES[resolution]
    except KeyError:
        borders = cfeature.NaturalEarthFeature(cfeature.BORDERS.category,
                                               cfeature.BORDERS.name,
                                               resolution,
                                               edgecolor='none',
                                               facecolor='none')
        land = cfeature.NaturalEarthFeature(cfeature.LAND.category,
                                            cfeature.LAND.name, resolution,
                                            edgecolor='face', facecolor='none')
        ocean = cfeature.NaturalEarthFeature(cfeature.OCEAN.category,
                                             cfeature.OCEAN.name, resolution,
                                             edgecolor='face',
                                             facecolor='none')
        _CARTOPY_FEATURES[resolution] = (borders, land, ocean)

    # Draw coast lines, country boundaries, fill continents.
    if MATPLOTLIB_VERSION >= [2, 0, 0]:
        map_ax.set_facecolor(water_fill_color)
    else:
        map_ax.set_axis_bgcolor(water_fill_color)
    map_ax.add_feature(ocean, facecolor=water_fill_color)
    map_ax.add_feature(land, facecolor=continent_fill_color)
    map_ax.add_feature(borders, edgecolor='0.75')
    map_ax.coastlines(resolution=resolution, color='0.4')

    # Draw grid lines - TODO: draw_labels=True doesn't work yet.
    if projection == 'local':
        map_ax.gridlines()
    else:
        # Draw lat/lon grid lines every 30 degrees.
        map_ax.gridlines(xlocs=range(-180, 181, 30), ylocs=range(-90, 91, 30))

    # Plot labels
    if labels and len(lons) > 0:
        with map_ax.hold_limits():
            for name, xpt, ypt, _colorpt in zip(labels, lons, lats, color):
                map_ax.text(xpt, ypt, name, weight="heavy", color="k",
                            zorder=100, transform=ccrs.Geodetic(),
                            path_effects=[
                                PathEffects.withStroke(linewidth=3,
                                                       foreground="white")])

    scatter = map_ax.scatter(lons, lats, marker=marker, s=size, c=color,
                             zorder=10, cmap=colormap,
                             transform=ccrs.Geodetic())

    if title:
        plt.suptitle(title)

    # Only show the colorbar for more than one event.
    if show_colorbar:
        if colorbar_ticklabel_format is not None:
            if isinstance(colorbar_ticklabel_format, (str, native_str)):
                formatter = FormatStrFormatter(colorbar_ticklabel_format)
            elif hasattr(colorbar_ticklabel_format, '__call__'):
                formatter = FuncFormatter(colorbar_ticklabel_format)
            elif isinstance(colorbar_ticklabel_format, Formatter):
                formatter = colorbar_ticklabel_format
            locator = MaxNLocator(5)
        else:
            if datetimeplot:
                locator = AutoDateLocator()
                formatter = AutoDateFormatter(locator)
                # Compat with old matplotlib versions.
                if hasattr(formatter, "scaled"):
                    formatter.scaled[1 / (24. * 60.)] = '%H:%M:%S'
            else:
                locator = None
                formatter = None
        cb = Colorbar(cm_ax, scatter, cmap=colormap,
                      orientation='horizontal',
                      ticks=locator,
                      format=formatter)
        # Compat with old matplotlib versions.
        if hasattr(cb, "update_ticks"):
            cb.update_ticks()

    if show:
        plt.show()

    return fig
Пример #4
0
def plot_histogram(data, figsize=(7, 5), color=None, number_to_keep=None,
                   sort='asc', target_string=None,
                   legend=None, bar_labels=True, title=None, ax=None):
    """Plot a histogram of data.

    Args:
        data (list or dict): This is either a list of dictionaries or a single
            dict containing the values to represent (ex {'001': 130})
        figsize (tuple): Figure size in inches.
        color (list or str): String or list of strings for histogram bar colors.
        number_to_keep (int): The number of terms to plot and rest
            is made into a single bar called 'rest'.
        sort (string): Could be 'asc', 'desc', or 'hamming'.
        target_string (str): Target string if 'sort' is a distance measure.
        legend(list): A list of strings to use for labels of the data.
            The number of entries must match the length of data (if data is a
            list or 1 if it's a dict)
        bar_labels (bool): Label each bar in histogram with probability value.
        title (str): A string to use for the plot title
        ax (matplotlib.axes.Axes): An optional Axes object to be used for
            the visualization output. If none is specified a new matplotlib
            Figure will be created and used. Additionally, if specified there
            will be no returned Figure since it is redundant.

    Returns:
        matplotlib.Figure:
            A figure for the rendered histogram, if the ``ax``
            kwarg is not set.

    Raises:
        ImportError: Matplotlib not available.
        VisualizationError: When legend is provided and the length doesn't
            match the input data.

    Example:
        .. jupyter-execute::

           from qiskit import QuantumCircuit, BasicAer, execute
           from qiskit.visualization import plot_histogram
           %matplotlib inline

           qc = QuantumCircuit(2, 2)
           qc.h(0)
           qc.cx(0, 1)
           qc.measure([0, 1], [0, 1])

           backend = BasicAer.get_backend('qasm_simulator')
           job = execute(qc, backend)
           plot_histogram(job.result().get_counts(), color='midnightblue', title="New Histogram")
    """
    if not HAS_MATPLOTLIB:
        raise ImportError('Must have Matplotlib installed.')
    if sort not in VALID_SORTS:
        raise VisualizationError("Value of sort option, %s, isn't a "
                                 "valid choice. Must be 'asc', "
                                 "'desc', or 'hamming'")
    if sort in DIST_MEAS.keys() and target_string is None:
        err_msg = 'Must define target_string when using distance measure.'
        raise VisualizationError(err_msg)

    if isinstance(data, dict):
        data = [data]

    if legend and len(legend) != len(data):
        raise VisualizationError("Length of legendL (%s) doesn't match "
                                 "number of input executions: %s" %
                                 (len(legend), len(data)))
    if ax is None:
        fig, ax = plt.subplots(figsize=figsize)
    else:
        fig = None

    labels = list(sorted(
        functools.reduce(lambda x, y: x.union(y.keys()), data, set())))
    if number_to_keep is not None:
        labels.append('rest')

    if sort in DIST_MEAS.keys():
        dist = []
        for item in labels:
            dist.append(DIST_MEAS[sort](item, target_string))

        labels = [list(x) for x in zip(*sorted(zip(dist, labels),
                                               key=lambda pair: pair[0]))][1]

    labels_dict = OrderedDict()

    # Set bar colors
    if color is None:
        color = ['#648fff', '#dc267f', '#785ef0', '#ffb000', '#fe6100']
    elif isinstance(color, str):
        color = [color]

    all_pvalues = []
    length = len(data)
    for item, execution in enumerate(data):
        if number_to_keep is not None:
            data_temp = dict(Counter(execution).most_common(number_to_keep))
            data_temp["rest"] = sum(execution.values()) - sum(data_temp.values())
            execution = data_temp
        values = []
        for key in labels:
            if key not in execution:
                if number_to_keep is None:
                    labels_dict[key] = 1
                    values.append(0)
                else:
                    values.append(-1)
            else:
                labels_dict[key] = 1
                values.append(execution[key])
        values = np.array(values, dtype=float)
        where_idx = np.where(values >= 0)[0]
        pvalues = values[where_idx] / sum(values[where_idx])
        for value in pvalues:
            all_pvalues.append(value)
        numelem = len(values[where_idx])
        ind = np.arange(numelem)  # the x locations for the groups
        width = 1/(len(data)+1)  # the width of the bars
        rects = []
        for idx, val in enumerate(pvalues):
            label = None
            if not idx and legend:
                label = legend[item]
            if val >= 0:
                rects.append(ax.bar(idx+item*width, val, width, label=label,
                                    color=color[item % len(color)],
                                    zorder=2))
        bar_center = (width / 2) * (length - 1)
        ax.set_xticks(ind + bar_center)
        ax.set_xticklabels(labels_dict.keys(), fontsize=14, rotation=70)
        # attach some text labels
        if bar_labels:
            for rect in rects:
                for rec in rect:
                    height = rec.get_height()
                    if height >= 1e-3:
                        ax.text(rec.get_x() + rec.get_width() / 2., 1.05 * height,
                                '%.3f' % float(height),
                                ha='center', va='bottom', zorder=3)
                    else:
                        ax.text(rec.get_x() + rec.get_width() / 2., 1.05 * height,
                                '0',
                                ha='center', va='bottom', zorder=3)

    # add some text for labels, title, and axes ticks
    ax.set_ylabel('Probabilities', fontsize=14)
    ax.set_ylim([0., min([1.2, max([1.2 * val for val in all_pvalues])])])
    if sort == 'desc':
        ax.invert_xaxis()

    ax.yaxis.set_major_locator(MaxNLocator(5))
    for tick in ax.yaxis.get_major_ticks():
        tick.label.set_fontsize(14)
    ax.set_facecolor('#eeeeee')
    plt.grid(which='major', axis='y', zorder=0, linestyle='--')
    if title:
        plt.title(title)

    if legend:
        ax.legend(loc='upper left', bbox_to_anchor=(1.01, 1.0), ncol=1,
                  borderaxespad=0, frameon=True, fontsize=12)
    if fig:
        if get_backend() in ['module://ipykernel.pylab.backend_inline',
                             'nbAgg']:
            plt.close(fig)
    return fig
Пример #5
0
                      alpha=0.25,
                      color='black',
                      linewidth=0)
axarr[0].fill_between(tl,
                      NS_vector - 2 * sigma_ns,
                      NS_vector + 2 * sigma_ns,
                      alpha=0.2,
                      color='black',
                      linewidth=0)
axarr[0].errorbar(t, NS, yerr=dNS, fmt=".k")
axarr[0].set_ylabel("$\Delta$ North (mas)",
                    fontweight='normal',
                    fontsize=16,
                    labelpad=4,
                    **csfont)
axarr[0].yaxis.set_major_locator(MaxNLocator(prune='both'))
axarr[0].locator_params(axis='y', nbins=8)
axarr[0].yaxis.set_minor_locator(minorLocator1)
axarr[0].yaxis.set_tick_params(which='minor', width=1, length=3, color='k')
axarr[0].yaxis.set_tick_params(which='major', width=1, length=6, color='k')
axarr[0].xaxis.set_tick_params(which='minor', width=1, length=3, color='k')
axarr[0].xaxis.set_tick_params(which='major', width=1, length=6, color='k')
axarr[0].set_title(target, fontweight='normal', fontsize=16, **csfont)

# Plot East offset with 1 and 2 sigma errors
axarr[1].plot(tl, EW_vector, color="black", lw=1.5, alpha=1)
axarr[1].fill_between(tl,
                      EW_vector - sigma_ew,
                      EW_vector + sigma_ew,
                      alpha=0.2,
                      color='black',
Пример #6
0
def plot_split_value_histogram(
        booster,
        feature,
        bins=None,
        ax=None,
        width_coef=0.8,
        xlim=None,
        ylim=None,
        title='Split value histogram for feature with @index/name@ @feature@',
        xlabel='Feature split value',
        ylabel='Count',
        figsize=None,
        dpi=None,
        grid=True,
        **kwargs):
    """Plot split value histogram for the specified feature of the model.

    Parameters
    ----------
    booster : Booster or LGBMModel
        Booster or LGBMModel instance of which feature split value histogram should be plotted.
    feature : int or string
        The feature name or index the histogram is plotted for.
        If int, interpreted as index.
        If string, interpreted as name.
    bins : int, string or None, optional (default=None)
        The maximum number of bins.
        If None, the number of bins equals number of unique split values.
        If string, it should be one from the list of the supported values by ``numpy.histogram()`` function.
    ax : matplotlib.axes.Axes or None, optional (default=None)
        Target axes instance.
        If None, new figure and axes will be created.
    width_coef : float, optional (default=0.8)
        Coefficient for histogram bar width.
    xlim : tuple of 2 elements or None, optional (default=None)
        Tuple passed to ``ax.xlim()``.
    ylim : tuple of 2 elements or None, optional (default=None)
        Tuple passed to ``ax.ylim()``.
    title : string or None, optional (default="Split value histogram for feature with @index/name@ @feature@")
        Axes title.
        If None, title is disabled.
        @feature@ placeholder can be used, and it will be replaced with the value of ``feature`` parameter.
        @index/name@ placeholder can be used,
        and it will be replaced with ``index`` word in case of ``int`` type ``feature`` parameter
        or ``name`` word in case of ``string`` type ``feature`` parameter.
    xlabel : string or None, optional (default="Feature split value")
        X-axis title label.
        If None, title is disabled.
    ylabel : string or None, optional (default="Count")
        Y-axis title label.
        If None, title is disabled.
    figsize : tuple of 2 elements or None, optional (default=None)
        Figure size.
    dpi : int or None, optional (default=None)
        Resolution of the figure.
    grid : bool, optional (default=True)
        Whether to add a grid for axes.
    **kwargs
        Other parameters passed to ``ax.bar()``.

    Returns
    -------
    ax : matplotlib.axes.Axes
        The plot with specified model's feature split value histogram.
    """
    if MATPLOTLIB_INSTALLED:
        import matplotlib.pyplot as plt
        from matplotlib.ticker import MaxNLocator
    else:
        raise ImportError(
            'You must install matplotlib to plot split value histogram.')

    if isinstance(booster, LGBMModel):
        booster = booster.booster_
    elif not isinstance(booster, Booster):
        raise TypeError('booster must be Booster or LGBMModel.')

    hist, bins = booster.get_split_value_histogram(feature=feature,
                                                   bins=bins,
                                                   xgboost_style=False)
    if np.count_nonzero(hist) == 0:
        raise ValueError(
            'Cannot plot split value histogram, '
            'because feature {} was not used in splitting'.format(feature))
    width = width_coef * (bins[1] - bins[0])
    centred = (bins[:-1] + bins[1:]) / 2

    if ax is None:
        if figsize is not None:
            _check_not_tuple_of_2_elements(figsize, 'figsize')
        _, ax = plt.subplots(1, 1, figsize=figsize, dpi=dpi)

    ax.bar(centred, hist, align='center', width=width, **kwargs)

    if xlim is not None:
        _check_not_tuple_of_2_elements(xlim, 'xlim')
    else:
        range_result = bins[-1] - bins[0]
        xlim = (bins[0] - range_result * 0.2, bins[-1] + range_result * 0.2)
    ax.set_xlim(xlim)

    ax.yaxis.set_major_locator(MaxNLocator(integer=True))
    if ylim is not None:
        _check_not_tuple_of_2_elements(ylim, 'ylim')
    else:
        ylim = (0, max(hist) * 1.1)
    ax.set_ylim(ylim)

    if title is not None:
        title = title.replace('@feature@', str(feature))
        title = title.replace(
            '@index/name@',
            ('name' if isinstance(feature, string_type) else 'index'))
        ax.set_title(title)
    if xlabel is not None:
        ax.set_xlabel(xlabel)
    if ylabel is not None:
        ax.set_ylabel(ylabel)
    ax.grid(grid)
    return ax
Пример #7
0
def grid_fn(input_dir, dim):
    results = {}
    for man_dir in fullpath_list(input_dir, only_dirs=True):
        man_name = os.path.basename(man_dir)
        if args.manifolds and man_name not in args.manifolds:
            continue

        results[man_name] = {}
        for thresh_dir in fullpath_list(man_dir, only_dirs=True):
            thresh = os.path.basename(thresh_dir)

            results[man_name][thresh] = {}
            for quantity in args.plots:
                ret = read_quantity_results(thresh_dir, quantity)
                if ret is not None:
                    results[man_name][thresh][quantity] = ret

    quantity_labels = dict(degrees='Node Degree',
                           seccurvs='Graph Sectional Curvature')
    quantity_titles = dict(degrees='Degree Distributions',
                           seccurvs='Curvature Estimates')

    for quantity in args.plots:
        ls = ['-', '--', '-.', ':']
        ms = ['o', 'v', '*', 'd', 'x', '1']
        width = 7 if dim == 3 else 6
        fig, ax = plt.subplots(figsize=(width, 5))

        for i, man_name in enumerate(sorted(results.keys())):
            values = results[man_name]
            xs = []
            ys = []
            for k in values.keys():
                thresh_values = values[k]
                if quantity in thresh_values:
                    xs.append(float(k))
                    ys.append(thresh_values[quantity])

            xs, ys = zip(*sorted(zip(xs, ys), key=lambda e: e[0]))
            p25, p50, p75 = zip(*ys)
            label = manifold_label_for_display(man_name)
            line, = plt.plot(xs,
                             p50,
                             label=label,
                             lw=6 - i,
                             ls=ls[i % 4],
                             marker=ms[i % 6],
                             ms=10)
            ax.fill_between(xs,
                            p25,
                            p75,
                            facecolor=line.get_color(),
                            alpha=0.3)

        ax.set_ylim(bottom=args.ymin, top=args.ymax)
        ax.set_xlim(left=0.8, right=args.xmax)
        ax.xaxis.set_major_locator(MaxNLocator(integer=True))
        ax.grid(color='lightgray', lw=2, alpha=0.5)
        ax.set_xlabel('Distance Threshold')
        if dim == 3:
            ax.set_ylabel(quantity_labels[quantity])
        else:
            ax.set_yticklabels([])
        ax.set_title('{} (n={})'.format(quantity_titles[quantity], dim),
                     y=1.18)
        ax.set_axisbelow(True)

        hs, labels = ax.get_legend_handles_labels()
        hs, labels = zip(*sorted(zip(hs, labels), key=lambda t: t[1]))
        ax.legend(hs,
                  labels,
                  bbox_to_anchor=(0, 1.02, 1, 0.2),
                  loc='lower left',
                  mode='expand',
                  borderaxespad=0,
                  ncol=4)

        plt.tight_layout()
        fig_name = os.path.join(args.save_dir, f'{quantity}-{dim}.pdf')
        fig.savefig(fig_name, bbox_inches='tight')
        plt.close()
Пример #8
0
def part4():
    lamrange = [2800, 8700]

    # The chi-by-eye temperatures.
    temps = [4500, 5200, 4750, 5200, 7300]
    amplitudes = np.zeros(len(temps), dtype=float)
    distances = np.zeros(len(temps), dtype=float)

    # Set up the figure.
    fig = pl.figure(figsize=(6, 12))
    fig.subplots_adjust(left=0.04,
                        bottom=0.07,
                        right=0.96,
                        top=0.95,
                        hspace=0.0)

    for i in range(len(temps)):
        ax = fig.add_subplot(len(temps), 1, i + 1)

        # Load the data and plot it.
        loglam, flux, z0 = load_spectrum("data/part4/{0}.fits".format(i + 1))
        lam, fsmooth = plot_spectrum(loglam, flux, z=z0, ax=ax, smooth=4)

        # Compute the redshift distance.
        distances[i] = z0 * 3e5 / 70.0  # Mpc

        # Get these y limits.
        ylim = ax.get_ylim()

        # Plot the unsmoothed spectrum.
        plot_spectrum(loglam, flux, z=z0, ax=ax, alpha=0.3)

        # Plot the black body "fit".
        bb = black_body(lam, temps[i])
        amplitudes[i] = fsmooth[np.argmax(bb)] / np.max(bb)
        ax.plot(lam, bb * amplitudes[i], "k", lw=3, alpha=0.8)

        # Plot the spectral lines.
        for n, (k, l, yoff) in enumerate(emission_lines):
            ax.axvline(l, ls="dotted", color="k")
            if i == 0:
                yoff += 8
                ax.annotate("$\mathrm{{{0}}}$".format(k),
                            xy=[l, ylim[-1]],
                            xycoords="data",
                            xytext=[0, yoff],
                            textcoords="offset points",
                            ha="center",
                            size=12)

        # Label the temperature.
        ax.annotate(r"${0}$ K".format(temps[i]),
                    xy=[0, 1],
                    xycoords="axes fraction",
                    va="top",
                    xytext=[10, -10],
                    textcoords="offset points")

        # Clean up the axis.
        ax.set_ylim(ylim)
        ax.set_xlim(lamrange)
        ax.yaxis.set_major_locator(MaxNLocator(4))
        ax.set_yticklabels([])

        if i < len(temps) - 1:
            ax.set_xticklabels([])
        else:
            ax.set_xlabel(r"$\lambda \, (\mathrm{\AA})$")

    fig.savefig("part4.pdf")

    # Part 5 starts here.
    sigma = 5.6704e-5  # erg / s / cm^2 / K^4
    lamgrid = 10**np.linspace(2, 10, 50000)
    for i in range(len(temps)):
        bb = black_body(lamgrid, temps[i])
        flux = amplitudes[i] * simps(bb, x=lamgrid)
        D = distances[i]  # in Mpc
        print(D * np.sqrt(flux / sigma / temps[i]**4))
def drawMptcpInHandover(csvFile, tcpprobeCsvFile, tcpdumpCsvFile, w0HoCsvFile,
                        w1HoCsvFile, tmpDir, count):
    ###############################################################################
    print('**********第一阶段:准备数据**********')
    #####################################################
    print('读取单台车的data.csv数据')
    df = pd.read_csv(csvFile,
                     na_filter=False,
                     usecols=['curTimestamp', 'W0pingrtt', 'W1pingrtt'],
                     dtype={
                         'curTimestamp': int,
                         'W0pingrtt': int,
                         'W1pingrtt': int
                     })
    #####################################################
    #####################################################
    print('读入tcpprobeData.csv文件为dataframe')
    tcpprobeDf = pd.read_csv(tcpprobeCsvFile,
                             usecols=[
                                 'timestamp', 'src', 'srcPort', 'dst',
                                 'dstPort', 'length', 'snd_nxt', 'snd_una',
                                 'snd_cwnd', 'ssthresh', 'snd_wnd', 'srtt',
                                 'rcv_wnd', 'path_index', 'map_data_len',
                                 'map_data_seq', 'map_subseq', 'snt_isn',
                                 'rcv_isn'
                             ],
                             dtype={
                                 'timestamp': int,
                                 'src': str,
                                 'srcPort': int,
                                 'dst': str,
                                 'dstPort': int,
                                 'length': int,
                                 'snd_nxt': int,
                                 'snd_una': int,
                                 'snd_cwnd': int,
                                 'ssthresh': int,
                                 'snd_wnd': int,
                                 'srtt': int,
                                 'rcv_wnd': int,
                                 'path_index': int,
                                 'map_data_len': int,
                                 'map_data_seq': int,
                                 'map_subseq': int,
                                 'snt_isn': int,
                                 'rcv_isn': int
                             })
    #####################################################
    #####################################################
    print('读入tcpdumpData.csv文件为dataframe')
    tcpdumpDf = pd.read_csv(tcpdumpCsvFile,
                            usecols=[
                                'timestamp', 'src', 'srcPort', 'dst',
                                'dstPort', 'tcpDataLen', 'seq', 'ack',
                                'segType', 'dsn', 'dataAck', 'subSeq',
                                'mptcpDataLen', 'tsval', 'tsecr'
                            ],
                            dtype={
                                'timestamp': int,
                                'src': str,
                                'srcPort': int,
                                'dst': str,
                                'dstPort': int,
                                'tcpDataLen': int,
                                'seq': int,
                                'ack': int,
                                'segType': str,
                                'dsn': int,
                                'dataAck': int,
                                'subSeq': int,
                                'mptcpDataLen': int,
                                'tsval': int,
                                'tsecr': int
                            })
    #####################################################
    #####################################################
    print('读取单台车的WLAN0漫游时段汇总.csv文件')
    w0HoDf = pd.read_csv(w0HoCsvFile,
                         na_filter=False,
                         usecols=['start', 'end', 'duration', 'flag'],
                         dtype={
                             'start': int,
                             'end': int,
                             'duration': int,
                             'flag': int
                         })
    #####################################################
    #####################################################
    print('读取单台车的WLAN1漫游时段汇总.csv文件')
    w1HoDf = pd.read_csv(w1HoCsvFile,
                         na_filter=False,
                         usecols=['start', 'end', 'duration', 'flag'],
                         dtype={
                             'start': int,
                             'end': int,
                             'duration': int,
                             'flag': int
                         })
    #####################################################
    print('**********第一阶段结束**********')
    ###############################################################################

    ###############################################################################
    print('**********第二阶段:画漫游事件前后mptcp时序图**********')
    #####################################################
    print('按漫游时长各分类提取时段数据进行分析')
    bins = [0, 200, 1e3, 30e3, sys.maxsize]
    labels = ['<200ms', '200ms-1s', '1s-30s', '>=30s']
    w0HoDurationCategory = dict(
        list(
            w0HoDf.groupby(
                pd.cut(w0HoDf['duration'],
                       bins=bins,
                       labels=labels,
                       right=False).sort_index())))
    w1HoDurationCategory = dict(
        list(
            w1HoDf.groupby(
                pd.cut(w1HoDf['duration'],
                       bins=bins,
                       labels=labels,
                       right=False).sort_index())))
    #####################################################
    #####################################################
    print('按漫游时长各分类提取时段数据进行分析')
    w0HoList = list(
        w0HoDf.groupby(pd.cut(w0HoDf['duration'],
                              [0, 1e3]).sort_index()))[0][1]
    w1HoList = list(
        w1HoDf.groupby(pd.cut(w1HoDf['duration'],
                              [0, 1e3]).sort_index()))[0][1]
    #####################################################
    #####################################################
    for wlanStr, wlanHoList in {
            'wlan0': w0HoDurationCategory,
            'wlan1': w1HoDurationCategory
    }.items():
        for durationStr, hoList in wlanHoList.items():
            fileDir = os.path.join(os.path.join(tmpDir, wlanStr), durationStr)
            if not os.path.isdir(fileDir):
                os.makedirs(fileDir)
            innerCount = count
            for _, ho in hoList.iterrows():
                if innerCount == 0:
                    break
                innerCount -= 1
                #####################################################
                # 提取分析时段为[漫游开始时刻-5s, 漫游结束时刻+5s]
                startTime = ho['start'] - int(5e6)
                endTime = ho['end'] + int(5e6)
                tcpprobeDf['bin'] = pd.cut(tcpprobeDf['timestamp'],
                                           [startTime, endTime])
                innerTpDf = list(tcpprobeDf.groupby('bin'))[0][1]

                tcpdumpDf['bin'] = pd.cut(tcpdumpDf['timestamp'],
                                          [startTime, endTime])
                innerTdDf = list(tcpdumpDf.groupby('bin'))[0][1]
                # 过滤服务器端口非7070的tcpdump数据
                innerTdDf = innerTdDf[(innerTdDf['srcPort'] == 7070) |
                                      (innerTdDf['dstPort'] == 7070)]
                if innerTdDf.empty or innerTpDf.empty:
                    continue
                # 划分wlan0与wlan1
                innerTpDf['wlan'] = innerTpDf.apply(
                    lambda row: 'wlan{}'.format(0
                                                if '151' in row['src'] else 1),
                    axis=1)
                innerTdDf['wlan'] = innerTdDf.apply(
                    lambda row: 'wlan0'
                    if '151' in row['src'] or '151' in row['dst'] else 'wlan1',
                    axis=1)
                # C1=red, C0=blue
                innerTdDf['color'] = innerTdDf.apply(
                    lambda row: 'C0' if row['wlan'] == 'wlan0' else 'C1',
                    axis=1)
                # 提取漫游事件
                w0InnerHoList = w0HoDf[((w0HoDf['start'] > startTime) &
                                        (w0HoDf['start'] < endTime)) |
                                       ((w0HoDf['end'] > startTime) &
                                        (w0HoDf['end'] < endTime))]
                w1InnerHoList = w1HoDf[((w1HoDf['start'] > startTime) &
                                        (w1HoDf['start'] < endTime)) |
                                       ((w1HoDf['end'] > startTime) &
                                        (w1HoDf['end'] < endTime))]
                #####################################################
                #####################################################
                # 画tcp time sequence graph
                plt.xlabel('timestamp (s)')
                xticks = np.linspace(startTime, endTime, 10, dtype=np.int64)
                xlabels = list(map(lambda x: str(int(x / 1e6))[-2:], xticks))
                plt.xticks(xticks, xlabels)
                plt.ylabel('seq/ack number')
                plt.gca().yaxis.set_major_locator(MaxNLocator(integer=True))
                # 构造时序数据
                agvToServerDf = innerTdDf[(innerTdDf['dst'] == '30.113.129.7')
                                          & (innerTdDf['dsn'] != 0)]
                serverToAgvDf = innerTdDf[(innerTdDf['src'] == '30.113.129.7')
                                          & (innerTdDf['dataAck'] != 0)]
                #####################################################
                # #####################################################
                # # 标注segType
                # # 实际作图后发现,由于带SYN,MP_CAPABLE等标志的包dsn往往为0,图上显示不出来.
                # segTypeDf = innerTdDf[innerTdDf['segType'].notnull()]
                # if segTypeDf.empty:
                #     continue
                # for _, row in segTypeDf.iterrows():
                #     x = row['timestamp']
                #     if row['dst'] == '30.113.129.7':
                #         y = row['dsn'] + row['mptcpDataLen']
                #     else:
                #         y = row['dataAck']
                #     ytext = y + 500
                #     print('segType:{}, xy:({}, {}), xytext:({}, {})'.format(row['segType'], x, y, x, ytext))
                #     plt.annotate(row['segType'], xy=(x, y), xytext=(x, ytext), arrowprops={'arrowstyle': '->'})
                # #####################################################
                #####################################################
                # 画dataAck横线
                try:
                    ackHxmin = serverToAgvDf['timestamp'].tolist()[:-1]
                    ackHxmax = serverToAgvDf['timestamp'].tolist()[1:]
                    ackHy = serverToAgvDf['dataAck'].tolist()[:-1]
                    ackHColors = serverToAgvDf['color'].tolist()[:-1]
                    ackH = list(zip(ackHy, ackHxmin, ackHxmax, ackHColors))

                    plt.hlines(*zip(*ackH), linestyles='dotted')
                except Exception as e:
                    print('画dataAck横线: {}'.format(e))
                #####################################################
                #####################################################
                # 画dataAck竖线
                try:
                    ackVymin = serverToAgvDf['dataAck'].tolist()[:-1]
                    ackVymax = serverToAgvDf['dataAck'].tolist()[1:]
                    ackVx = serverToAgvDf['timestamp'].tolist()[1:]
                    ackVColors = serverToAgvDf['color'].tolist()[1:]
                    ackV = list(zip(ackVx, ackVymin, ackVymax, ackVColors))
                    plt.vlines(*zip(*ackV), linestyles='dotted')

                except Exception as e:
                    print('画dataAck竖线: {}'.format(e))
                #####################################################
                #####################################################
                # 画dsn竖线
                try:
                    seqVymin = agvToServerDf['dsn'].tolist()
                    seqVymax = (agvToServerDf['dsn'] +
                                agvToServerDf['mptcpDataLen']).tolist()
                    seqVx = agvToServerDf['timestamp'].tolist()
                    seqVColors = agvToServerDf['color'].tolist()
                    seqV = list(zip(seqVx, seqVymin, seqVymax, seqVColors))
                    plt.vlines(*zip(*seqV))

                except Exception as e:
                    print('画dsn竖线: {}'.format(e))
                #####################################################
                #####################################################
                # 画漫游竖线
                for _, row in w0InnerHoList.iterrows():
                    plt.axvspan(row['start'],
                                row['end'],
                                alpha=0.3,
                                color='C0')
                for _, row in w1InnerHoList.iterrows():
                    plt.axvspan(row['start'],
                                row['end'],
                                alpha=0.3,
                                color='C1')
                #####################################################
                #####################################################
                # 构造时延数据
                df['timestamp'] = (df['curTimestamp'] * 1e6).astype(int)
                df['bin'] = pd.cut(df['timestamp'],
                                   bins=[startTime, endTime],
                                   right=False)
                rttDf = list(df.groupby('bin'))[0][1]
                # w0PingRtt
                w0PingRttDf = rttDf[rttDf['W0pingrtt'] % 1000 != 0][[
                    'timestamp', 'W0pingrtt'
                ]]
                w0PingRttDf = w0PingRttDf.rename(columns={'W0pingrtt': 'data'})
                w0PingRttDf['data_type'] = 'pingRtt'
                w0PingRttDf['wlan'] = 'wlan0'
                # w1PingRtt
                w1PingRttDf = rttDf[rttDf['W1pingrtt'] % 1000 != 0][[
                    'timestamp', 'W1pingrtt'
                ]]
                w1PingRttDf = w1PingRttDf.rename(columns={'W1pingrtt': 'data'})
                w1PingRttDf['data_type'] = 'pingRtt'
                w1PingRttDf['wlan'] = 'wlan1'
                # srtt
                srttDf = innerTpDf[['timestamp', 'srtt', 'wlan']]
                srttDf = srttDf.rename(columns={'srtt': 'data'})
                srttDf['data_type'] = 'srtt'
                # rtt
                tsvalDf = agvToServerDf[['timestamp', 'tsval', 'wlan']]
                tsecrDf = serverToAgvDf[['timestamp', 'tsecr']]
                tsoptRttDf = tsvalDf.merge(tsecrDf,
                                           how='inner',
                                           left_on='tsval',
                                           right_on='tsecr',
                                           suffixes=('_start', '_end'))
                tsoptRttDf['rtt'] = ((tsoptRttDf['timestamp_end'] -
                                      tsoptRttDf['timestamp_start']) /
                                     1e3).astype(int)
                # # 记录状态以debug
                # tsoptRttDf.to_csv(os.path.join(fileDir, '{}:{}-{}.csv'.format(wlanStr, ho['start'], ho['end'])))
                tsoptRttDf = tsoptRttDf[['timestamp_end', 'rtt', 'wlan']] \
                    .rename(columns={'timestamp_end': 'timestamp', 'rtt': 'data'})
                tsoptRttDf['data_type'] = 'rtt'

                delayDf = pd.concat(
                    [w0PingRttDf, w1PingRttDf, srttDf, tsoptRttDf],
                    ignore_index=True)
                # 画时延
                delayAx = plt.twinx()
                sns.lineplot(data=delayDf,
                             x='timestamp',
                             y='data',
                             hue='wlan',
                             palette={
                                 'wlan0': 'C0',
                                 'wlan1': 'C1'
                             },
                             style='data_type',
                             ms=4,
                             markers={
                                 'pingRtt': 'D',
                                 'srtt': 'o',
                                 'rtt': 's'
                             },
                             ax=delayAx)
                delayAx.set_ylabel('delay (ms)')
                delayAx.get_yaxis().set_major_locator(
                    MaxNLocator(integer=True))
                #####################################################
                #####################################################
                # 设置标题
                plt.title("mptcp时序图")

                # 减小字体
                plt.rcParams.update({'font.size': 6})

                # 调整图像避免截断xlabel
                plt.tight_layout()

                plt.savefig(os.path.join(
                    fileDir, '{}:{}-{}.png'.format(wlanStr, ho['start'],
                                                   ho['end'])),
                            dpi=200)
                plt.pause(1)
                plt.close()
                plt.pause(1)
                #####################################################
    print('**********第二阶段结束**********')
Пример #10
0
		points += 1
		if not all(close(x1, x2, epsilon)
		           for x1, x2 in zip(floats(line1), floats(line2))):
			errors += 1

	# Wilson score interval for 1 sigma
	return (logn + levels, (errors + 0.5)/(points + 1.0),
	        np.sqrt(errors*(points - errors)/points + 0.25)/(points + 1))

def plotvalues(file1, file2, epsilon, fmt='.k'):
	x, y, yerr = readvalues(file1, file2, epsilon)
	plt.errorbar(x, y, yerr, fmt=fmt)

if __name__ == '__main__':
	from sys import argv

	epsilon, fmt = 1e-6, '.k'
	for name1, name2 in zip(argv[1::2], argv[2::2]):
		if name1 == '-e':
			epsilon = float(name2)
			continue
		if name1 == '-f':
			fmt = name2
			continue
		with open(name1, 'r') as file1, open(name2, 'r') as file2:
			plotvalues(file1, file2, epsilon, fmt=fmt)
	plt.gca().xaxis.set_major_locator(MaxNLocator(integer=True))
	plt.xlabel("$L$")
	plt.ylabel("Error probability")
	plt.show()
Пример #11
0
def heatmap(data,
            xticklabels,
            yticklabels,
            xlabel="",
            ylabel="",
            textcolor="red",
            fontsize=None,
            cmap='Greys',
            number_fmt='.2f',
            fontweight_max='bold',
            fontweight_min='normal',
            ax=None,
            savefig=False):
    """
    Return a basic labelled heatmap of the numberic data, with data[0,0] placed in the upper left

    :param data: Numpy array of data to plot
    :param xticklabels (yticklabels): Labels corresponding to row (column) element of data
    :param textcolor: color designation passed to ax.text()
    :param fontsize: fontsize parameter passed to ax.text()
    :param fontweight_max: fontweight parameter passed to ax.text() for the maximum element in data (to
                           highlight the max cell in the heatmap set to 'bold' or 'heavy')
    :param fontweight_min: fontweight parameter passed to ax.text() for the minimum element in data (to
                           highlight the min cell in the heatmap set to 'bold' or 'heavy')
    :param fontsize: fontsize parameter passed to ax.text()
    :param cmap: cmap designation passed to imshow()
    :param number_fmt: Format string for the numbers printed in the heatmap
    :param savefig: If not False, save the figure generated here to savefig

    :return: tuple of (matplotlib figure, matplotlib axes)
    """
    print(
        "WARNING: USING LOCAL VERSION OF PLOTTING SCRIPTS - SWITCH TO (GENERAL_UTILS.PLOTTING)"
    )
    # Labels need to be padded by 1 element, as imshow will plot data starting centered at tick (1,1) but the
    # set_xticklabels/set_yticklabels sets labels starting at tick 0
    try:
        xticklabels = ("", ) + tuple(
            [f'{x:{number_fmt}}' for x in xticklabels])
    except TypeError:
        # Failsafe if number_fmt doesn't match the labels (eg if labels are tuples)
        xticklabels = ("", ) + tuple([str(x) for x in xticklabels])
    try:
        yticklabels = ("", ) + tuple(
            [f'{x:{number_fmt}}' for x in yticklabels])
    except TypeError:
        # Failsafe if number_fmt doesn't match the labels (eg if labels are tuples)
        yticklabels = ("", ) + tuple([str(x) for x in yticklabels])

    fig, ax = get_fig_ax(ax=ax)

    # Set fontweight dict to help with formatting
    fontweight = np.chararray(data.shape, itemsize=20, unicode=True)
    fontweight[:] = 'normal'
    # Set weight for max/min cells for highlighting
    fontweight[np.unravel_index(data.argmax(), data.shape)] = fontweight_max
    fontweight[np.unravel_index(data.argmin(), data.shape)] = fontweight_min

    for i, j in itertools.product(range(data.shape[0]), range(data.shape[1])):
        ax.text(j,
                i,
                format(data[i, j], number_fmt),
                horizontalalignment="center",
                verticalalignment='center',
                color=textcolor,
                fontsize=fontsize,
                fontweight=fontweight[i, j])
    ax.imshow(data, cmap=cmap)
    # Tried to force always having ticks for each box, but didn't work...
    # ax.set_xticks(np.arange(1, len(xticklabels)-1, 1))
    # ax.set_yticks(np.arange(1, len(yticklabels)-1, 1))
    ax.xaxis.set_major_locator(MaxNLocator(integer=True))
    ax.yaxis.set_major_locator(MaxNLocator(integer=True))
    ax.set_xticklabels(xticklabels)
    ax.set_yticklabels(yticklabels)
    ax.set_xlabel(xlabel)
    ax.set_ylabel(ylabel)

    if savefig:
        fig.savefig(savefig)

    return fig, ax
Пример #12
0
    # Setze Phase = 0 bei sehr kleinen Amplituden (unterdrücke numerisches Rauschen):
    if abs(Xn[i] / max(abs(Xn))) < 1.0e-10: Xn[i] = 0
xf = fftshift(fftfreq(len(xn), d=1.0 / len(xn)))
#xf= fftshift(range(0,len(xn)))
print('xf = ', xf)
print('xn = ', xn)
print(Xn)
my_xlim = lim_eps(xf, 0.05)
#plt.xkcd() # XKCD-Style Plots (wie von Hand gezeichnet ...)
fig2 = figure(2)
plt.suptitle(r'DFT $S[k]$', fontsize=18)  # Overall title
#
ax21 = fig2.add_subplot(211)
grid(True)
ax21.xaxis.set_major_locator(
    MaxNLocator(integer=True))  #enforce integer tick labels
ml, sl, bl = ax21.stem(xf, abs(Xn))
plt.setp(ml, 'markerfacecolor', 'r', 'markersize', 8)  # marker
plt.setp(sl, 'color', 'r', 'linewidth', 2)  # stemline
plt.setp(bl, 'linewidth', 0)  # turn off baseline
ax21.set_ylabel(r'$|S[k]|$ / V $\rightarrow$')
ax21.set_xlim(my_xlim)
ax21.set_ylim(lim_eps(abs(Xn), 0.1))
ax21.axhline(y=0, xmin=0, xmax=1, linewidth=1, color='k')
ax21.axvline(x=0, ymin=0, ymax=1, linewidth=1, color='k')

#
ax22 = fig2.add_subplot(212)
grid(True)
ax22.xaxis.set_major_locator(
    MaxNLocator(integer=True))  #enforce integer tick labels
Пример #13
0
def plot_many(filenames, pdfpages):
    groups_by_time = defaultdict(list)
    files = [h5py.File(fn, 'r') for fn in filenames]
    for f in files:
        groups = myutils.getTimeSortedGroups(f['.'])
        for g in groups:
            groups_by_time[round(g.attrs['time'])].append(g)

    times = groups_by_time.keys()
    times.sort()
    times = np.asarray(times)

    radius, radius_std = averaged_global((groups_by_time, times),
                                         'geometry/radius')
    volume, volume_std = averaged_global((groups_by_time, times),
                                         'geometry/volume')
    sphere_equiv_radius, sphere_equiv_radius_std = averaged_global(
        (groups_by_time, times), 'geometry/sphere_equiv_radius')
    #sphere_equiv_radius, sphere_equiv_radius_std = averaged_global((groups_by_time, times), 'geometry/cylinder_equiv_radius')
    sphericity, sphericity_std = averaged_global((groups_by_time, times),
                                                 'geometry/sphericity')

    # estimate velocity
    data = np.asarray((times, radius))
    if len(data[0]) > 1:
        from scipy.optimize import leastsq
        func = lambda p, x, y: (x * p[0] + p[1] - y)
        p, success = leastsq(func, (1, 0), args=(data[0], data[1]))
        velocity = p[0]
        print 'estimated velocity: %f' % velocity
        print 'fit params: %s' % str(p)
    else:
        velocity = 0.

    if 1:
        #plot by time
        fig, axes = pyplot.subplots(2,
                                    2,
                                    figsize=(mastersize[0] * 0.5,
                                             mastersize[0] * 0.5))
        mpl_utils.subplots_adjust_abs(
            fig,
            left=mpl_utils.mm_to_inch * 13,
            right=-mpl_utils.mm_to_inch * 5,
            top=-mpl_utils.mm_to_inch * 5,
            bottom=mpl_utils.mm_to_inch * 10,
            hspace=mpl_utils.mm_to_inch * 30,
            wspace=mpl_utils.mm_to_inch * 40,
        )
        axes = axes.ravel()

        def plt_rad(ax):
            ax.set(ylabel='[mm]', xlabel='t [h]')
            mpl_utils.errorbar(ax,
                               times,
                               1.e-3 * radius,
                               yerr=1.e-3 * radius_std,
                               label='r',
                               lw=0.,
                               marker='x',
                               color='k',
                               markersize=5.)
            label = u'$r_0 + v_{fit} t$\n$v_{fit} = %s$ [\u03BCm/h]' % f2s(
                velocity)
            ax.plot(times,
                    1.e-3 * (p[1] + p[0] * times),
                    label=label,
                    color='r')
            ax.legend()
            #ax.text(0.6, 0.2, r'$v_{fit} = %s$' % f2s(velocity), transform = ax.transAxes)

        def plt_vol(ax):
            ax.set(ylabel='volume', xlabel='t [h]')
            ax.errorbar(times, 1.e-9 * volume, yerr=1.e-9 * volume_std)

        def plt_sprad(ax):
            ax.set(ylabel='sphere equiv. radius', xlabel='t [h]')
            ax.errorbar(times,
                        1.e-3 * sphere_equiv_radius,
                        yerr=1.e-3 * sphere_equiv_radius_std)

        def plt_sphereicity(ax):
            ax.set(ylabel='sphericity', xlabel='t [h]')
            ax.errorbar(times, sphericity, yerr=sphericity_std)

        for ax, func in zip(axes,
                            [plt_rad, plt_vol, plt_sprad, plt_sphereicity]):
            l = MaxNLocator(nbins=4)
            ax.xaxis.set_major_locator(l)
            func(ax)

        pdfpages.savefig(fig)

    times = times[0::2]
    radial = averaged_radial((groups_by_time, times), 'vs_dr', [
        'phi_tumor', 'mvd', 'radius', 'shearforce', 'flow', 'sources', 'vel',
        'oxy', 'maturation'
    ])
    bins = np.asarray(groups_by_time[times[0]][0]['radial/vs_dr/bins'])
    bins *= 1. / 1000.
    xlim = -1.5, 1.0
    mask = np.logical_and(bins < xlim[1], bins >= xlim[0])

    def plot_times(ax, name, **kwargs):
        f = kwargs.pop('value_prefactor', 1.)
        colors = 'rgbmk'
        markers = 'os<>d'
        for i, t in enumerate(times):
            avg, std = radial[name, t]
            avg, std = avg[mask], std[mask]
            #ax.errorbar(bins[mask], f*avg, yerr=f*std, label = 't = %s' % f2s(t), **kwargs)
            mpl_utils.errorbar(ax,
                               bins[mask],
                               f * avg,
                               yerr=f * std,
                               label='t = $%s$' % f2s(t),
                               marker=markers[i],
                               color=colors[i],
                               every=5,
                               **kwargs)

    def text1(ax, txt):
        ax.text(0.95, 0.9, txt, ha="right", transform=ax.transAxes)

    def text2(ax, txt):
        ax.text(0.01, 0.9, txt, ha="left", transform=ax.transAxes)

    def mkfig(nrows, ncols):
        fig, axes = mpl_utils.subplots_abs_mm(
            (mastersize[0] / mpl_utils.mm_to_inch * 0.5 * ncols,
             mastersize[0] / mpl_utils.mm_to_inch * 0.2 * nrows), nrows, ncols,
            10, 20, a4size[0] / mpl_utils.mm_to_inch * 0.38,
            a4size[0] / mpl_utils.mm_to_inch * 0.16, 15, 5)
        return fig, axes


#  fig, axes = pyplot.subplots(4, 2, figsize = (mastersize[0], mastersize[0]*0.25*4.))
#  mpl_utils.subplots_adjust_abs(fig, left = mpl_utils.mm_to_inch*20,
#                                right = -mpl_utils.mm_to_inch*10,
#                                top  = -mpl_utils.mm_to_inch*5,
#                                bottom = mpl_utils.mm_to_inch*10,
#                                hspace = mpl_utils.mm_to_inch*30,)

    def plt_mvd(ax):
        # mvd can be written as N^3 * L0/N * 3 / V = (V=L0^3) ... = N^2 / L0^2
        ax.set(ylabel=ur'$\times 10^3$ [\u03BCm$^{-2}$]', xlim=xlim)
        plot_times(ax, 'mvd', value_prefactor=1e3)
        text1(ax, r'$L/V$')
        ax.legend(loc=mpl_utils.loc.lower_left, frameon=True)

    def plt_rad(ax):
        ax.set(ylabel=ur'[\u03BCm]', xlim=xlim)
        plot_times(ax, 'radius')
        text1(ax, r'$r_v$')

    def plt_vel(ax):
        ax.set(ylabel=ur'[\u03BCm/h]', xlim=xlim)
        text1(ax, r'$v_\phi$')
        plot_times(ax, 'vel')

    def plt_tum(ax):
        ax.set(ylabel=ur'', xlim=xlim, ylim=(-0.1, 0.7))
        plot_times(ax, 'phi_tumor')
        text1(ax, r'$\phi_t$')

    def plt_oxy(ax):
        ax.set(ylabel=ur'', xlim=xlim)
        plot_times(ax, 'oxy')
        text1(ax, r'$c_o$')

    def plt_sf(ax):
        ax.set(ylabel=ur'[Pa]', xlim=xlim)
        plot_times(ax, 'shearforce', value_prefactor=1e3)
        text1(ax, r'$f_v$')

    def plt_wall(ax):
        ax.set(ylabel=ur'[\u03BCm]', xlim=xlim)
        plot_times(ax, 'maturation')
        text1(ax, r'$w_v$')

    def plt_qv(ax):
        ax.set(ylabel=ur'[\u03BCm]', xlim=xlim)
        ax.set(xlabel=ur'$\theta$ [mm]')
        plot_times(ax, 'flow')
        text1(ax, r'$q_v$')

    fig, axes = mkfig(3, 2)
    plt_mvd(axes[0, 0])
    plt_rad(axes[0, 1])
    plt_vel(axes[1, 0])
    plt_oxy(axes[1, 1])
    plt_wall(axes[2, 0])
    plt_sf(axes[2, 1])

    for ax in axes[2, :]:
        ax.set(xlabel=ur'$\theta$ [mm]')

    axes = axes.ravel()
    for i, ax in enumerate(axes):
        ax.grid(linestyle=':', linewidth=0.5, color=gridcolor)
        mpl_utils.add_crosshair(ax, (0, 0), color=gridcolor)
        if not ax.get_xlabel():
            ax.set(xticklabels=[])
        text2(ax, '(%s)' % 'abcdefghij'[i])

    pdfpages.savefig(fig)
Пример #14
0
def wrapper(i):

    year_lower = i
    year_upper = i
    month_lower = 6
    month_upper = 8
    day_lower = 1
    day_upper = 31
    hour_lower = 0
    hour_upper = 23
    minute_lower = 0
    minute_upper = 45

    time_params = np.array([year_lower, year_upper, month_lower,
                            month_upper, day_lower, day_upper,
                            hour_lower, hour_upper, minute_lower,
                            minute_upper])

    datetimes = utilities.get_datetime_objects(time_params)

    for date_i in np.arange(0, len(datetimes)):
        date = datetimes[date_i]
        print date
        year_bool = np.asarray(years == date.year)
        root = np.asarray(year_cm_roots)[year_bool][0]
        if root == root2:
            # We're using TCH's daily cloudmask files. These are values from
            #  0-2 (I think), and need selecting and regridding.
            try:
                clouddata = Dataset(root+date.strftime("%B%Y")+'/cloudmask_' + datetimes[
                        date_i].strftime("%Y%m%d") + '.nc')
                cloudmask_times = num2date(clouddata.variables['time'][:],
                                           clouddata.variables['time'].units)
                cloudmask_times = np.asarray([dt.datetime(j.year, j.month,
                                                          j.day, j.hour,
                                                          j.minute) for j
                                              in cloudmask_times])
                cloudmask_bool = cloudmask_times == date
                clouds_data = clouddata.variables['cloud_mask'][cloudmask_bool]
                clouds_now = clouds_data#[cloudmask_bool]

                clouds_now_regridded = pinkdust.regrid_data(root2_lons, root2_lats,
                                                            target_lons,
                                                            target_lats,
                                                            clouds_now, mesh=True)
                data_array[clouds_now_regridded > 1] += 1
            except:
                print 'No cloud data for', date

        else:
            # We're using Ian's original cloud mask files. These are just a
            # binary one or zero and won't be regridded.
            try:
                clouddata = Dataset(root+
                    datetimes[date_i].strftime("%B").upper(
                    ) + str(datetimes[date_i].year) + '_CLOUDS/eumetsat.cloud.'
                    + datetimes[
                        date_i].strftime("%Y%m%d%H%M") + '.nc')
                clouds_now = clouddata.variables['cmask'][:][0]
                data_array[clouds_now == 1] += 1
            except:
                print 'No cloud data for', date

        if date_i == 100:
            print data_array
            np.save('cloudcover_array', data_array)

            extent = (
            np.min(target_lons), np.max(target_lons), np.min(target_lats),
            np.max(target_lats))
            m = Basemap(projection='cyl', llcrnrlon=extent[0],
                        urcrnrlon=extent[1],
                        llcrnrlat=extent[2], urcrnrlat=extent[3],
                        resolution='i')

            m.drawcoastlines(linewidth=0.5)
            m.drawcountries(linewidth=0.5)
            parallels = np.arange(10., 40, 2.)
            # labels = [left,right,top,bottom]
            m.drawparallels(parallels, labels=[False, True, True, False],
                            linewidth=0.5)
            meridians = np.arange(-20., 17., 4.)
            m.drawmeridians(meridians, labels=[True, False, False, True],
                            linewidth=0.5)

            min = 0
            max = 70000

            levels = MaxNLocator(nbins=15).tick_values(min, max)

            # discrete_cmap = utilities.cmap_discretize(cm.RdYlBu_r, 10)
            cmap = cm.get_cmap('RdYlBu_r')
            norm = BoundaryNorm(levels, ncolors=cmap.N, clip=True)

            data_array = np.ma.masked_where(np.isnan(data_array),
                                            data_array)

            m.pcolormesh(target_lons, target_lats, data_array,
                         cmap=cmap, norm=norm, vmin=min, vmax=max)

            cbar = plt.colorbar(orientation='horizontal', fraction=0.056,
                                pad=0.06)
            cbar.ax.set_xlabel('Counts of cloud occurrence')
            plt.tight_layout()
            plt.savefig('CPO_multiyear_cloud_frequency_2004_2012.png',
                        bbox_inches='tight')

            plt.close()
Пример #15
0
def LoadGraphs(len_unique_aca, len_unique_cre, len_list_aca, len_list_cre):
    # Load Bar Graphs
    x_values_1, y_values_1, x_values_2, y_values_2 = [], [], [], []
    for token_len, freq in len_unique_aca:
        x_values_1.append(token_len)
        y_values_1.append(freq)
    for token_len, freq in len_unique_cre:
        x_values_2.append(token_len)
        y_values_2.append(freq)
    plt.figure(0)
    plt.bar(x_values_1, y_values_1)
    plt.gca().xaxis.set_major_locator(MaxNLocator(integer=True))
    plt.gca().yaxis.set_major_locator(MaxNLocator(integer=True))
    plt.xlabel("Token Length")
    plt.ylabel("Frequency")
    plt.title("Column Graph of Size of Tokens in Academic Writing Source")
    plt.figure(1)
    plt.bar(x_values_2, y_values_2)
    plt.gca().xaxis.set_major_locator(MaxNLocator(integer=True))
    plt.gca().yaxis.set_major_locator(MaxNLocator(integer=True))
    plt.xlabel("Token Length")
    plt.ylabel("Frequency")
    plt.title("Column Graph of Size of Tokens in Creative Writing Source")

    # Load Cumulative Frequency Graph
    cumulative_freq_1, cumulative_freq_2 = [], []
    for index, freq in enumerate(y_values_1):
        if index == 0:
            cumulative_freq_1.append(freq)
        else:
            cumulative_freq_1.append(cumulative_freq_1[index - 1] + freq)
    for index, freq in enumerate(y_values_2):
        if index == 0:
            cumulative_freq_2.append(freq)
        else:
            cumulative_freq_2.append(cumulative_freq_2[index - 1] + freq)
    plt.figure(2)
    plt.plot(x_values_1, cumulative_freq_1)
    plt.gca().xaxis.set_major_locator(MaxNLocator(integer=True))
    plt.gca().yaxis.set_major_locator(MaxNLocator(integer=True))
    plt.xlabel("Token Length")
    plt.ylabel("Frequency")
    plt.title(
        "Cumulative Frequency of Size of Tokens in Academic Writing Source")
    plt.figure(3)
    plt.plot(x_values_2, cumulative_freq_2)
    plt.gca().xaxis.set_major_locator(MaxNLocator(integer=True))
    plt.gca().yaxis.set_major_locator(MaxNLocator(integer=True))
    plt.xlabel("Token Length")
    plt.ylabel("Frequency")
    plt.title(
        "Cumulative Frequency Graph of Size of Tokens in Creative Writing Source"
    )

    # Load Box and Whisker Diagram
    plt.figure(4)
    plt.boxplot(len_list_aca)
    plt.gca().xaxis.set_major_locator(MaxNLocator(integer=True))
    plt.gca().yaxis.set_major_locator(MaxNLocator(integer=True))
    plt.title("The Box Plot for the Academic Writing Sample")
    plt.figure(5)
    plt.boxplot(len_list_cre)
    plt.gca().xaxis.set_major_locator(MaxNLocator(integer=True))
    plt.gca().yaxis.set_major_locator(MaxNLocator(integer=True))
    plt.title("The Box Plot for the Creative Writing Sample")

    # DEBUG USE ONLY
    # print(len_list_aca)
    # print(len_list_cre)
    # print(len_unique_aca)
    # print(len_unique_cre)
    # print(y_values_1)
    # print(y_values_2)
    # print(cumulative_freq_1)
    # print(cumulative_freq_2)
    # print(acaTokens)
    # print(creTokens)
    plt.show()

    # Calculate mean sd median Q1 Q3 IQR Skewness
    Xf_1, fsq_1 = [], []
    Xf_2, fsq_2 = [], []
    for token_len, freq in len_unique_aca:
        Xf_1.append(token_len * freq)
    mean_1 = sum(Xf_1) / sum(y_values_1)
    for token_len, freq in len_unique_aca:
        fsq_1.append(freq * (token_len - mean_1)**2)
    sd_1 = math.sqrt(sum(fsq_1) / sum(y_values_1))
    for token_len, freq in len_unique_cre:
        Xf_2.append(token_len * freq)
    mean_2 = sum(Xf_2) / sum(y_values_2)
    for token_len, freq in len_unique_aca:
        fsq_2.append(freq * (token_len - mean_2)**2)
    sd_2 = math.sqrt(sum(fsq_2) / sum(y_values_2))
    median_1 = np.percentile(len_list_aca, 50)
    median_2 = np.percentile(len_list_cre, 50)
    Q1_1 = np.percentile(len_list_aca, 25)
    Q1_2 = np.percentile(len_list_cre, 25)
    Q3_1 = np.percentile(len_list_aca, 75)
    Q3_2 = np.percentile(len_list_cre, 75)
    IQR_1 = Q3_1 - Q1_1
    IQR_2 = Q3_2 - Q1_2
    skewness_1 = (3 * (mean_1 - median_1)) / sd_1
    skewness_2 = (3 * (mean_2 - median_2)) / sd_2

    # Copy the results to the clipboard
    pyperclip.copy("Academic Source Data:" + "\nMean = " + str(mean_1) +
                   "\nStandard Deviation = " + str(sd_1) + "\nMedian = " +
                   str(median_1) + "\nQ1 = " + str(Q1_1) + "\nQ3 = " +
                   str(Q3_1) + "\nIQR = " + str(IQR_1) + "\nSkewness = " +
                   str(skewness_1) + "\n\n"
                   "Creative Writing Source Data:" + "\nMean = " +
                   str(mean_2) + "\nStandard Deviation = " + str(sd_2) +
                   "\nMedian = " + str(median_2) + "\nQ1 = " + str(Q1_2) +
                   "\nQ3 = " + str(Q3_2) + "\nIQR = " + str(IQR_2) +
                   "\nSkewness = " + str(skewness_2))

    # Create Output Window
    column1 = [[sg.Text("Academic Source Data:")],
               [
                   sg.Text("Mean = " + str(mean_1) +
                           "\nStandard Deviation = " + str(sd_1) +
                           "\nMedian = " + str(median_1) + "\nQ1 = " +
                           str(Q1_1) + "\nQ3 = " + str(Q3_1) + "\nIQR = " +
                           str(IQR_1) + "\nSkewness = " + str(skewness_1))
               ]]
    column2 = [[sg.Text("Creative Writing Source Data:")],
               [
                   sg.Text("Mean = " + str(mean_2) +
                           "\nStandard Deviation = " + str(sd_2) +
                           "\nMedian = " + str(median_2) + "\nQ1 = " +
                           str(Q1_2) + "\nQ3 = " + str(Q3_2) + "\nIQR = " +
                           str(IQR_2) + "\nSkewness = " + str(skewness_2))
               ]]
    layout = [
        [sg.Column(column1),
         sg.VSeperator(),
         sg.Column(column2)],
        [sg.Text("Result Copied to Clipboard!", text_color="yellow")],
    ]

    window2 = sg.Window("GAC Helper", layout)
    while True:
        event, values = window2.read()
        if event == sg.WIN_CLOSED:
            break
    window2.close()
Пример #16
0
print(np.array(full_sol).shape)

param_list = np.array([cutoff, lam, v, m_lam, hx, hy, c, gam, L, L2, Nx, Ny, N_k, k_IR, \
                       T_min, T_max, mu_min, mu_max, N_T, N_mu, min_values, \
                       min_values_diq, export_sol, full_sol])
dat_name = 'threeColorQMDphaseDiagramFD_g_' + str(hx) + 'gd_' + str(
    hy) + '_T_max' + str(T_max) + 'N_T' + str(N_T) + 'N_mu' + str(
        N_mu) + 'Ngrd' + str(Nx)
np.save(dat_name, param_list)

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib.ticker import MaxNLocator

fig, ax1 = plt.subplots(nrows=1)
levels = MaxNLocator(nbins=32).tick_values(min_values.min(), min_values.max())
CS = ax1.contourf(mu_ax, T_ax, min_values, levels=levels)
fig.colorbar(CS, ax=ax1)
plt.title('Phase Diagram')
####
fig, ax1 = plt.subplots(nrows=1)
levels = MaxNLocator(nbins=32).tick_values(min_values_diq.min(),
                                           min_values_diq.max())
CS = ax1.contourf(mu_ax, T_ax, min_values_diq, levels=levels)
fig.colorbar(CS, ax=ax1)
plt.title('Diquark Phase Diagram')
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.plot_wireframe(mu_ax, T_ax, min_values, color='red')
ax.plot_wireframe(mu_ax, T_ax, min_values_diq)
plt.show()
Пример #17
0
            else:
                xlims = (-xlims, +xlims)

            ylims = np.max(np.abs(ax_residual.get_ylim()))
            

        kwds = dict(c="#666666", linestyle=":", linewidth=0.5, zorder=-1)
        ax.plot([xlims[0], +xlims[1]], [xlims[0], +xlims[1]], "-", **kwds)
        ax_residual.plot([xlims[0], +xlims[1]], [0, 0], "-", **kwds)

        ax.set_xlim(xlims[0], +xlims[1])
        ax.set_ylim(xlims[0], +xlims[1])
        ax_residual.set_xlim(xlims[0], +xlims[1])
        ax_residual.set_ylim(-ylims, +ylims)

        ax_residual.yaxis.set_major_locator(MaxNLocator(3))
        ax_residual.xaxis.set_major_locator(MaxNLocator(3))
        ax.xaxis.set_major_locator(MaxNLocator(3))
        ax.yaxis.set_major_locator(MaxNLocator(3))
        ax_residual.set_xticks([])


        ax.set_xlabel(xlabels[i])
        ax.set_ylabel(ylabels[i])
        ax_residual.set_ylabel(delta_labels[i])

        #ax.set_aspect(1.0)
        #ax_residual.set_aspect(1)
        idx += 1

    # Plot the inflated specific variances.
Пример #18
0
def plot_student_results(student, scores, cohort_size):
    #  create the figure
    fig, ax1 = plt.subplots(figsize=(9, 7))
    fig.subplots_adjust(left=0.115, right=0.88)
    fig.canvas.set_window_title('Eldorado K-8 Fitness Chart')

    pos = np.arange(len(testNames)) + 0.5  # Center bars on the Y-axis ticks

    rects = ax1.barh(pos, [scores[k].percentile for k in testNames],
                     align='center',
                     height=0.5,
                     color='m',
                     tick_label=testNames)

    ax1.set_title(student.name)

    ax1.set_xlim([0, 100])
    ax1.xaxis.set_major_locator(MaxNLocator(11))
    ax1.xaxis.grid(True,
                   linestyle='--',
                   which='major',
                   color='grey',
                   alpha=.25)

    # Plot a solid vertical gridline to highlight the median position
    ax1.axvline(50, color='grey', alpha=0.25)
    # set X-axis tick marks at the deciles
    cohort_label = ax1.text(.5,
                            -.07,
                            'Cohort Size: {}'.format(cohort_size),
                            horizontalalignment='center',
                            size='small',
                            transform=ax1.transAxes)

    # Set the right-hand Y-axis ticks and labels
    ax2 = ax1.twinx()

    scoreLabels = [format_score(scores[k].score, k) for k in testNames]

    # set the tick locations
    ax2.set_yticks(pos)
    # make sure that the limits are set equally on both yaxis so the
    # ticks line up
    ax2.set_ylim(ax1.get_ylim())

    # set the tick labels
    ax2.set_yticklabels(scoreLabels)

    ax2.set_ylabel('Test Scores')

    ax2.set_xlabel(
        ('Percentile Ranking Across '
         '{grade} Grade {gender}s').format(grade=attach_ordinal(student.grade),
                                           gender=student.gender.title()))

    rect_labels = []
    # Lastly, write in the ranking inside each bar to aid in interpretation
    for rect in rects:
        # Rectangle widths are already integer-valued but are floating
        # type, so it helps to remove the trailing decimal point and 0 by
        # converting width to int type
        width = int(rect.get_width())

        rankStr = attach_ordinal(width)
        # The bars aren't wide enough to print the ranking inside
        if (width < 5):
            # Shift the text to the right side of the right edge
            xloc = width + 1
            # Black against white background
            clr = 'black'
            align = 'left'
        else:
            # Shift the text to the left side of the right edge
            xloc = 0.98 * width
            # White on magenta
            clr = 'white'
            align = 'right'

        # Center the text vertically in the bar
        yloc = rect.get_y() + rect.get_height() / 2.0
        label = ax1.text(xloc,
                         yloc,
                         rankStr,
                         horizontalalignment=align,
                         verticalalignment='center',
                         color=clr,
                         weight='bold',
                         clip_on=True)
        rect_labels.append(label)

    # make the interactive mouse over give the bar title
    ax2.fmt_ydata = format_ycursor
    # return all of the artists created
    return {
        'fig': fig,
        'ax': ax1,
        'ax_right': ax2,
        'bars': rects,
        'perc_labels': rect_labels,
        'cohort_label': cohort_label
    }
        if count < 5:
            plt.setp(ax.get_xticklabels(), visible=False)

        #if count != 0 and count != 2 and count != 4 and count != 6:
        #    plt.setp(ax.get_yticklabels(), visible=False)

        props = dict(boxstyle='round', facecolor='white', alpha=1.0,
                     ec="white")
        fig_label = "%s %s" % (next(labels), site_name)
        ax.text(0.02, 0.95, fig_label,
                transform=ax.transAxes, fontsize=12, verticalalignment='top',
                bbox=props)

        from matplotlib.ticker import MaxNLocator
<<<<<<< HEAD
        ax.yaxis.set_major_locator(MaxNLocator(3))
        ax.xaxis.set_major_locator(MaxNLocator(6))
        #ax.set_ylim(0, 15)

        if count == 0:
            ax.set_ylim(0, 5)
        elif count == 1:
            ax.set_ylim(0, 6)
        elif count == 2:
            ax.set_ylim(0, 3)
        elif count == 3:
            ax.set_ylim(0, 14)
        elif count == 4:
            ax.set_ylim(0, 2)
        elif count == 5:
            ax.set_ylim(0, 7)
Пример #20
0
ax1.set_xlim(120, 170)
ax1.set_xlabel('Half crossing angle [urad]')
ax1.set_ylabel('Time [h]')
ax1.xaxis.label.set_color('b')
ax1.tick_params(axis='x', colors='b')
ax1.grid('on')

axlumi = ax1.twiny()
axlumi.plot(0.5 * np.sum(lumi_m2s, axis=1) / 1e34 * 1e-4,
            tc(t_stamps),
            'r',
            lw=2.)
axlumi.set_xlabel('Avg lumi [1e34 cm^-2.s-1]')
axlumi.xaxis.label.set_color('r')
axlumi.tick_params(axis='x', colors='r')
axlumi.xaxis.set_major_locator(MaxNLocator(5))

# axqp = plt.subplot2grid(shape=(5, 5), loc=(0, 4), colspan=1, rowspan=4, sharey=axlt)
# if enable_QP:
#     for plane, styl in zip(['H', 'V'], ['--', '-']):
#         parname = 'LHCBEAM%d/QP'%beam + plane
#         thistrim = chromaTrims_end[parname]
#         thistrim.data.append(thistrim.data[-1])
#         thistrim.time.append(t_stop_SB)
#         axqp.step(np.array(thistrim.data)+QP_offsets[parname], tc(thistrim.time), ls=styl, lw=2, color = 'k')
# axqp.set_xlim(0, 16)
# axqp.set_xlabel('Chromaticity')
# axqp.grid('on')

# axoct = axqp.twiny()
# thisoct = data_timb['RPMBB.RR17.ROD.A12B%d:I_MEAS'%beam]
Пример #21
0
def plot_err_components(stats):
    """Plot components of the error.

  .. note::
    it was chosen to plot(ii, mean_in_time(abs(err_i))),
    and thus the corresponding spread measure is MAD.
    If one chose instead: plot(ii, std_in_time(err_i)),
    then the corresponding measure of spread would have been std.
    This choice was made in part because (wrt. subplot 2)
    the singular values (svals) correspond to rotated MADs,
    and because rms(umisf) seems to convoluted for interpretation.
  """
    fig, (ax0, ax1, ax2) = freshfig(25, figsize=(6, 6), loc='1313', nrows=3)

    chrono = stats.HMM.t
    Nx = stats.xx.shape[1]

    err = mean(abs(stats.err.a), 0)
    sprd = mean(stats.mad.a, 0)
    umsft = mean(abs(stats.umisf.a), 0)
    usprd = mean(stats.svals.a, 0)

    ax0.plot(arange(Nx), err, 'k', lw=2, label='Error')
    if Nx < 10**3:
        ax0.fill_between(arange(Nx), [0] * len(sprd),
                         sprd,
                         alpha=0.7,
                         label='Spread')
    else:
        ax0.plot(arange(Nx), sprd, alpha=0.7, label='Spread')
    #ax0.set_yscale('log')
    ax0.set_title('Element-wise error comparison')
    ax0.set_xlabel('Dimension index (i)')
    ax0.set_ylabel('Time-average (_a) magnitude')
    ax0.set_xlim(0, Nx - 1)
    ax0.get_xaxis().set_major_locator(MaxNLocator(integer=True))
    ax0.legend(loc='upper right')

    ax1.set_xlim(0, Nx - 1)
    ax1.set_xlabel('Principal component index')
    ax1.set_ylabel('Time-average (_a) magnitude')
    ax1.set_title('Spectral error comparison')
    has_been_computed = np.any(np.isfinite(umsft))
    if has_been_computed:
        L = len(umsft)
        ax1.plot(arange(L), umsft, 'k', lw=2, label='Error')
        ax1.fill_between(arange(L), [0] * L, usprd, alpha=0.7, label='Spread')
        ax1.set_yscale('log')
        ax1.get_xaxis().set_major_locator(MaxNLocator(integer=True))
    else:
        not_available_text(ax1)

    rmse = stats.rmse.a[chrono.maskObs_BI]
    ax2.hist(rmse, bins=30, density=False)
    ax2.set_ylabel('Num. of occurence (_a)')
    ax2.set_xlabel('RMSE')
    ax2.set_title('Histogram of RMSE values')
    ax2.set_xlim(left=0)

    plot_pause(0.1)
    plt.tight_layout()
Пример #22
0
    def __init__(self, *filters, **kwargs):
        """Initialise a new `BodePlot`
        """
        from ..frequencyseries import FrequencySeries

        dB = kwargs.pop('dB', True)
        frequencies = kwargs.pop('frequencies', None)

        # parse plotting arguments
        figargs = dict()
        title = kwargs.pop('title', None)
        for key in [
                'figsize', 'dpi', 'frameon', 'subplotpars', 'tight_layout'
        ]:
            if key in kwargs:
                figargs[key] = kwargs.pop(key)

        # generate figure
        super().__init__(**figargs)

        # delete the axes, and create two more
        self.add_subplot(2, 1, 1)
        self.add_subplot(2, 1, 2, sharex=self.maxes)

        # add filters
        for filter_ in filters:
            if isinstance(filter_, FrequencySeries):
                self.add_frequencyseries(filter_, dB=dB, **kwargs)
            else:
                self.add_filter(filter_,
                                frequencies=frequencies,
                                dB=dB,
                                **kwargs)

        # format plots
        if dB:
            self.maxes.set_ylabel('Magnitude [dB]')
            ylim = self.maxes.get_ylim()
            if ylim[1] == 0:
                self.maxes.set_ybound(upper=ylim[1] +
                                      (ylim[1] - ylim[0]) * 0.1)
        else:
            self.maxes.set_yscale('log')
            self.maxes.set_ylabel('Amplitude')
        self.paxes.set_xlabel('Frequency [Hz]')
        self.paxes.set_ylabel('Phase [deg]')
        self.maxes.set_xscale('log')
        self.paxes.set_xscale('log')
        self.paxes.yaxis.set_major_locator(
            MaxNLocator(nbins='auto', steps=[4.5, 9]), )
        self.paxes.yaxis.set_minor_locator(
            MaxNLocator(nbins=20, steps=[4.5, 9]))
        if title:
            self.maxes.set_title(title)

        # get xlim
        if (frequencies is None and len(filters) == 1
                and isinstance(filters[0], FrequencySeries)):
            frequencies = filters[0].frequencies.value
        if not isinstance(frequencies, (type(None), int)):
            frequencies = frequencies[frequencies > 0]
            self.maxes.set_xlim(frequencies.min(), frequencies.max())
Пример #23
0
    def plot(self, figure=None, overlays=[], colorbar=True, vmin=None, vmax=None,
             linear=True, showz=True, yres=DEFAULT_YRES,
             max_dist=None, **matplotlib_args):
        """
        Plot spectrogram onto figure.

        Parameters
        ----------
        figure : matplotlib.figure.Figure
            Figure to plot the spectrogram on. If None, new Figure is created.
        overlays : list
            List of overlays (functions that receive figure and axes and return
            new ones) to be applied after drawing.
        colorbar : bool
            Flag that determines whether or not to draw a colorbar. If existing
            figure is passed, it is attempted to overdraw old colorbar.
        vmin : float
            Clip intensities lower than vmin before drawing.
        vmax : float
            Clip intensities higher than vmax before drawing.
        linear :  bool
            If set to True, "stretch" image to make frequency axis linear.
        showz : bool
            If set to True, the value of the pixel that is hovered with the
            mouse is shown in the bottom right corner.
        yres : int or None
            To be used in combination with linear=True. If None, sample the
            image with half the minimum frequency delta. Else, sample the
            image to be at most yres pixels in vertical dimension. Defaults
            to 1080 because that's a common screen size.
        max_dist : float or None
            If not None, mask elements that are further than max_dist away
            from actual data points (ie, frequencies that actually have data
            from the receiver and are not just nearest-neighbour interpolated).
        """
        # [] as default argument is okay here because it is only read.
        # pylint: disable=W0102,R0914
        if linear:
            delt = yres
            if delt is not None:
                delt = max(
                    (self.freq_axis[0] - self.freq_axis[-1]) / (yres - 1),
                    _min_delt(self.freq_axis) / 2.
                )
                delt = float(delt)

            data = _LinearView(self.clip_values(vmin, vmax), delt)
            freqs = np.arange(
                self.freq_axis[0], self.freq_axis[-1], -data.delt
            )
        else:
            data = np.array(self.clip_values(vmin, vmax))
            freqs = self.freq_axis

        figure = plt.gcf()

        if figure.axes:
            axes = figure.axes[0]
        else:
            axes = figure.add_subplot(111)

        params = {
            'origin': 'lower',
            'aspect': 'auto',
        }
        params.update(matplotlib_args)
        if linear and max_dist is not None:
            toplot = ma.masked_array(data, mask=data.make_mask(max_dist))
            pass
        else:
            toplot = data
        im = axes.imshow(toplot, **params)

        xa = axes.get_xaxis()
        ya = axes.get_yaxis()

        xa.set_major_formatter(
            FuncFormatter(self.time_formatter)
        )

        if linear:
            # Start with a number that is divisible by 5.
            init = (self.freq_axis[0] % 5) / data.delt
            nticks = 15.
            # Calculate MHz difference between major ticks.
            dist = (self.freq_axis[0] - self.freq_axis[-1]) / nticks
            # Round to next multiple of 10, at least ten.
            dist = max(round(dist, -1), 10)
            # One pixel in image space is data.delt MHz, thus we can convert
            # our distance between the major ticks into image space by dividing
            # it by data.delt.

            ya.set_major_locator(
                IndexLocator(
                    dist / data.delt, init
                )
            )
            ya.set_minor_locator(
                IndexLocator(
                    dist / data.delt / 10, init
                )
            )
            def freq_fmt(x, pos):
                # This is necessary because matplotlib somehow tries to get
                # the mid-point of the row, which we do not need here.
                x = x + 0.5
                return self.format_freq(self.freq_axis[0] - x * data.delt)
        else:
            freq_fmt = _list_formatter(freqs, self.format_freq)
            ya.set_major_locator(MaxNLocator(integer=True, steps=[1, 5, 10]))

        ya.set_major_formatter(
            FuncFormatter(freq_fmt)
        )

        axes.set_xlabel(self.t_label)
        axes.set_ylabel(self.f_label)
        # figure.suptitle(self.content)

        figure.suptitle(
            ' '.join([
                get_day(self.start).strftime("%d %b %Y"),
                'Radio flux density',
                '(' + ', '.join(self.instruments) + ')',
            ])
        )

        for tl in xa.get_ticklabels():
            tl.set_fontsize(10)
            tl.set_rotation(30)
        figure.add_axes(axes)
        figure.subplots_adjust(bottom=0.2)
        figure.subplots_adjust(left=0.2)

        if showz:
            axes.format_coord = self._mk_format_coord(
                data, figure.gca().format_coord)

        if colorbar:
            if len(figure.axes) > 1:
                Colorbar(figure.axes[1], im).set_label("Intensity")
            else:
                figure.colorbar(im).set_label("Intensity")

        for overlay in overlays:
            figure, axes = overlay(figure, axes)

        for ax in figure.axes:
            ax.autoscale()
        if isinstance(figure, SpectroFigure):
            figure._init(self, freqs)
        return axes
Пример #24
0
    #                       overwrite=True)

    if plot_additional_period_axis:
        fig.text(0.5, 0.96, 'Period [d]', ha='center')
        for panel, base_panel in zip(axes[0], axes[-1]):
            ax2 = panel.twiny()
            ax2.set_xticks(base_panel.get_xticks())
            ax2.set_xticklabels(af_plots.convert_frequency_to_period(base_panel.get_xticks()))
            ax2.set_xlim(panel.get_xlim())
            ax2.spines['right'].set_visible(False)
            ax2.spines['left'].set_visible(False)
        for row in axes:
            for idx, ax in enumerate(row):
                ax.set_ylim(0, ylim_max*1.45)
                if idx == 0:
                    ax.yaxis.set_major_locator(MaxNLocator(nbins=3, prune='both',
                                                           min_n_ticks=3))
                else:
                    ax.set_yticklabels([])

    plt.tight_layout()
    save_string = os.path.join(savepath, f'{object_name}_{test_period}d_'
                            f'{sampling_freq}d_alias_test.pdf')
    plt.savefig(save_string,
                bbox_inches='tight', dpi=600)
    plt.show()
    print(f'\n \n Plot saved to {save_string}')

if __name__ == "__main__":
    main()
Пример #25
0
def plot_scores(max_pow,
                scores,
                _run,
                optimal_accuracy,
                std_span=1,
                plot_y_label="Accuracy",
                plot_legend=True,
                theoretical_mean=None,
                theoretical_std=None,
                start_pow=1,
                ylim_top=1.05,
                ylim_bottom=None,
                axes=None):

    configure_matplotlib()

    # plt.title('Accuracy')
    if axes is None:
        fig = plt.figure()
        axes = fig.add_subplot(1, 1, 1)
    else:
        fig = axes.figure

    mean_scores = {key: np.mean(s, axis=1) for key, s in scores.items()}
    std_scores = {key: np.std(s, axis=1) for key, s in scores.items()}

    legend_scores_optimal = 'Limit-Rule'
    legend_scores_brownian_qda = 'Brownian-QDA'
    legend_scores_lda = 'LDA'
    legend_scores_qda = 'QDA'
    legend_scores_pls_centroid = 'PLS+Centroid'
    legend_scores_pca_qda = 'PCA+QDA'
    legend_scores_rkc = 'RKC'
    legend_theoretical = 'Theoretical'

    if theoretical_mean is not None:
        std_span_theoretical = 2

        if theoretical_std is None:
            theoretical_std = theoretical_mean * 0
            std_span_theoretical = 0

        plot_with_var(axes,
                      mean=theoretical_mean,
                      std=theoretical_std,
                      std_span=std_span_theoretical,
                      label=legend_theoretical,
                      color='C4')
    plot_with_var(axes,
                  mean=mean_scores['optimal'],
                  std=std_scores['optimal'],
                  std_span=std_span,
                  label=legend_scores_optimal,
                  color='C0',
                  linestyle=':',
                  marker='o')
    if 'brownian_qda' in scores:
        plot_with_var(axes,
                      mean=mean_scores['brownian_qda'],
                      std=std_scores['brownian_qda'],
                      label=legend_scores_brownian_qda,
                      std_span=std_span,
                      color='C1',
                      linestyle='--',
                      marker='^')
    if 'qda' in scores:
        plot_with_var(axes,
                      mean=mean_scores['qda'],
                      std=std_scores['qda'],
                      label=legend_scores_qda,
                      std_span=std_span,
                      color='C2',
                      linestyle='-.',
                      marker='v')
    if 'pca_qda' in scores:
        plot_with_var(axes,
                      mean=mean_scores['pca_qda'],
                      std=std_scores['pca_qda'],
                      std_span=std_span,
                      label=legend_scores_pca_qda,
                      color='C6',
                      marker='X')
    if 'lda' in scores:
        plot_with_var(axes,
                      mean=mean_scores['lda'],
                      std=std_scores['lda'],
                      std_span=std_span,
                      label=legend_scores_lda,
                      color='C3',
                      linestyle='--',
                      marker='s')
    if 'pls_centroid' in scores:
        plot_with_var(axes,
                      mean=mean_scores['pls_centroid'],
                      std=std_scores['pls_centroid'],
                      std_span=std_span,
                      label=legend_scores_pls_centroid,
                      color='C5',
                      linestyle='-.',
                      marker='p')
    if 'rkc' in scores:
        plot_with_var(axes,
                      mean=mean_scores['rkc'],
                      std=std_scores['rkc'],
                      std_span=std_span,
                      label=legend_scores_rkc,
                      color='C7',
                      marker='*')
    axes.xaxis.set_major_locator(MaxNLocator(integer=True))
    axes.set_xticklabels([2**i for i in range(start_pow, max_pow + 1)])
    axes.set_xlabel("$N_b$")
    if plot_y_label:
        axes.set_ylabel(plot_y_label)

    if plot_legend:
        leg = axes.legend(loc="best", fontsize=12)
        leg.get_frame().set_alpha(1)

    axes.set_xlim(0, max_pow - start_pow)
    if ylim_top is not None:
        axes.set_ylim(top=ylim_top)
    if ylim_bottom is not None:
        axes.set_ylim(bottom=ylim_bottom)
    axes.axhline(optimal_accuracy, linestyle=':', color='black')

    with tempfile.NamedTemporaryFile(suffix=".pdf") as tmpfile:
        fig.savefig(tmpfile, format="pdf")
        if _run is not None:
            _run.add_artifact(tmpfile.name, name="plot.pdf")

    return fig
Пример #26
0
from matplotlib.colors import BoundaryNorm
from matplotlib.ticker import MaxNLocator
import numpy as np

# make these smaller to increase the resolution
dx, dy = 0.05, 0.05

# generate 2 2d grids for the x & y bounds
y, x = np.mgrid[slice(1, 5 + dy, dy), slice(1, 5 + dx, dx)]

z = np.sin(x)**10 + np.cos(10 + y * x) * np.cos(x)

# x and y are bounds, so z should be the value *inside* those bounds.
# Therefore, remove the last value from the z array.
z = z[:-1, :-1]
levels = MaxNLocator(nbins=15).tick_values(z.min(), z.max())

# pick the desired colormap, sensible levels, and define a normalization
# instance which takes data values and translates those into levels.
cmap = plt.get_cmap('PiYG')
norm = BoundaryNorm(levels, ncolors=cmap.N, clip=True)

plt.subplot(2, 1, 1)
im = plt.pcolormesh(x, y, z, cmap=cmap, norm=norm)
plt.colorbar()
# set the limits of the plot to the limits of the data
plt.axis([x.min(), x.max(), y.min(), y.max()])
plt.title('pcolormesh with levels')

plt.subplot(2, 1, 2)
# contours are *point* based plots, so convert our bound into point
def plot_substrate():
    global current_idx, axes_max, cbar

    # select whichever substrate index you want, e.g., for one model:
    # 4=tumor cells field, 5=blood vessel density, 6=growth substrate

    xml_file = "output%08d.xml" % current_idx
    tree = ET.parse(xml_file)
    root = tree.getroot()
    #    print('time=' + root.find(".//current_time").text)
    mins = float(root.find(".//current_time").text)
    hrs = mins / 60.
    days = hrs / 24.
    title_str = '%d days, %d hrs, %d mins' % (int(days),
                                              (hrs % 24), mins - (hrs * 60))
    #    print(title_str)

    fname = "output%08d_microenvironment0.mat" % current_idx
    output_dir_str = '.'
    fullname = output_dir_str + "/" + fname
    if not pathlib.Path(fullname).is_file():
        print("file not found", fullname)
        return

    info_dict = {}
    scipy.io.loadmat(fullname, info_dict)
    M = info_dict['multiscale_microenvironment']
    print('plot_substrate: field_idx=', field_idx)
    f = M[field_idx, :]  #

    #N = int(math.sqrt(len(M[0,:])))
    #grid2D = M[0,:].reshape(N,N)
    xgrid = M[0, :].reshape(numy, numx)
    ygrid = M[1, :].reshape(numy, numx)

    #    xvec = grid2D[0,:]
    #xvec.size
    #xvec.shape
    num_contours = 30
    num_contours = 10
    #    vmin = 30.
    #    vmax = 38.

    levels = MaxNLocator(nbins=30).tick_values(vmin, vmax)
    #    cmap = plt.get_cmap('PiYG')
    cmap = plt.get_cmap('viridis')
    norm = BoundaryNorm(levels, ncolors=cmap.N, clip=True)

    #    my_plot = plt.contourf(xvec,xvec,M[field_idx,:].reshape(N,N), num_contours, cmap='viridis') #'viridis'
    if fix_cmap > 0:
        # my_plot = plt.contourf(xvec,xvec,M[field_idx,:].reshape(N,N), levels=levels, cmap=cmap)
        my_plot = plt.contourf(xgrid,
                               ygrid,
                               M[field_idx, :].reshape(numy, numx),
                               levels=levels,
                               extend='both',
                               cmap=cmap)
    else:
        # my_plot = plt.contourf(xvec,xvec,M[field_idx,:].reshape(N,N), cmap=cmap)
        my_plot = plt.contourf(xgrid,
                               ygrid,
                               M[field_idx, :].reshape(numy, numx),
                               cmap=cmap)

    if cbar == None:
        #      cbar = plt.colorbar(my_plot, boundaries=np.arange(vmin, vmax, 1.0))
        cbar = plt.colorbar(my_plot)
    else:
        cbar = plt.colorbar(my_plot, cax=cbar.ax)

#    plt.axis('equal')
    plt.title(title_str)

    #    plt.show()

    png_file = "aaa%08d.png" % current_idx
    def profile_nqubit_speed_constant_depth(self):
        """simulation time vs the number of qubits

        where the circuit depth is fixed at 40. Also creates a pdf file
        with this module name showing a plot of the results. Compilation
        is not included in speed.
        """
        import matplotlib.pyplot as plt
        from matplotlib.ticker import MaxNLocator
        qubit_range_max = 15
        n_qubit_list = range(1, qubit_range_max + 1)
        max_depth = 40
        min_depth = 40
        n_circuits = 10
        shots = 1024
        seed = 88
        max_time = 30  # seconds; timing stops when simulation time exceeds this number
        fmt_str1 = 'profile_nqubit_speed::nqubits:{0}, backend:{1},' \
                   'elapsed_time:{2:.2f}'
        fmt_str2 = 'backend:{0}, circuit:{1}, numOps:{2}, result:{3}'
        fmt_str3 = 'minDepth={minDepth}, maxDepth={maxDepth},' \
                   'num circuits={nCircuits}, shots={shots}'
        backend_list = [
            'local_qasm_simulator_py', 'local_unitary_simulator_py'
        ]
        if shutil.which('qasm_simulator'):
            backend_list.append('local_qasm_simulator_cpp')
        else:
            self.log.info('profile_nqubit_speed::\"qasm_simulator\" executable'
                          'not in path...skipping')
        fig = plt.figure(0)
        plt.clf()
        ax = fig.add_axes((0.1, 0.2, 0.8, 0.6))
        for _, backend in enumerate(backend_list):
            elapsedTime = np.zeros(len(n_qubit_list))
            if backend == 'local_unitary_simulator_py':
                doMeasure = False
            else:
                doMeasure = True
            j, timedOut = 0, False
            while j < qubit_range_max and not timedOut:
                nQubits = n_qubit_list[j]
                randomCircuits = RandomQasmGenerator(seed,
                                                     min_qubits=nQubits,
                                                     max_qubits=nQubits,
                                                     min_depth=min_depth,
                                                     max_depth=max_depth)
                randomCircuits.add_circuits(n_circuits, do_measure=doMeasure)
                qp = randomCircuits.get_program()
                cnames = qp.get_circuit_names()
                qobj = qp.compile(cnames,
                                  backend=backend,
                                  shots=shots,
                                  seed=seed)
                start = time.perf_counter()
                results = qp.run(qobj)
                stop = time.perf_counter()
                elapsedTime[j] = stop - start
                if elapsedTime[j] > max_time:
                    timedOut = True
                self.log.info(fmt_str1.format(nQubits, backend,
                                              elapsedTime[j]))
                if backend != 'local_unitary_simulator_py':
                    for name in cnames:
                        log_str = fmt_str2.format(backend, name,
                                                  len(qp.get_circuit(name)),
                                                  results.get_data(name))
                        self.log.info(log_str)
                j += 1
            ax.xaxis.set_major_locator(MaxNLocator(integer=True))
            if backend == 'local_unitary_simulator_py':
                ax.plot(n_qubit_list[:j],
                        elapsedTime[:j],
                        label=backend,
                        marker='o')
            else:
                ax.plot(n_qubit_list[:j],
                        elapsedTime[:j] / shots,
                        label=backend,
                        marker='o')
            ax.set_yscale('log', basey=10)
            ax.set_xlabel('number of qubits')
            ax.set_ylabel('process time/shot')
            ax.set_title('profile_nqubit_speed_constant_depth')
            fig.text(
                0.1, 0.05,
                fmt_str3.format(minDepth=min_depth,
                                maxDepth=max_depth,
                                nCircuits=n_circuits,
                                shots=shots))
            ax.legend()
        self.pdf.savefig(fig)
Пример #29
0
def plot_basemap(lons, lats, size, color, labels=None, projection='global',
                 resolution='l', continent_fill_color='0.8',
                 water_fill_color='1.0', colormap=None, colorbar=None,
                 marker="o", title=None, colorbar_ticklabel_format=None,
                 show=True, fig=None, **kwargs):  # @UnusedVariable
    """
    Creates a basemap plot with a data point scatter plot.

    :type lons: list/tuple of floats
    :param lons: Longitudes of the data points.
    :type lats: list/tuple of floats
    :param lats: Latitudes of the data points.
    :type size: float or list/tuple of floats
    :param size: Size of the individual points in the scatter plot.
    :type color: list/tuple of floats (or objects that can be
        converted to floats, like e.g.
        :class:`~obspy.core.utcdatetime.UTCDateTime`)
    :param color: Color information of the individual data points to be
        used in the specified color map (e.g. origin depths,
        origin times).
    :type labels: list/tuple of str
    :param labels: Annotations for the individual data points.
    :type projection: str, optional
    :param projection: The map projection.
        Currently supported are:

            * ``"global"`` (Will plot the whole world.)
            * ``"ortho"`` (Will center around the mean lat/long.)
            * ``"local"`` (Will plot around local events)

        Defaults to "global".
    :type resolution: str, optional
    :param resolution: Resolution of the boundary database to use. Will be
        based directly to the basemap module. Possible values are:

            * ``"c"`` (crude)
            * ``"l"`` (low)
            * ``"i"`` (intermediate)
            * ``"h"`` (high)
            * ``"f"`` (full)

        Defaults to ``"l"``. For compatibility, you may also specify any of the
        Cartopy resolutions defined in :func:`plot_cartopy`.
    :type continent_fill_color: Valid matplotlib color, optional
    :param continent_fill_color:  Color of the continents. Defaults to
        ``"0.9"`` which is a light gray.
    :type water_fill_color: Valid matplotlib color, optional
    :param water_fill_color: Color of all water bodies.
        Defaults to ``"white"``.
    :type colormap: str, any matplotlib colormap, optional
    :param colormap: The colormap for color-coding the events as provided
        in `color` kwarg.
        The event with the smallest `color` property will have the
        color of one end of the colormap and the event with the highest
        `color` property the color of the other end with all other events
        in between.
        Defaults to None which will use the default matplotlib colormap.
    :type colorbar: bool, optional
    :param colorbar: When left `None`, a colorbar is plotted if more than one
        object is plotted. Using `True`/`False` the colorbar can be forced
        on/off.
    :type title: str
    :param title: Title above plot.
    :type colorbar_ticklabel_format: str or function or
        subclass of :class:`matplotlib.ticker.Formatter`
    :param colorbar_ticklabel_format: Format string or Formatter used to format
        colorbar tick labels.
    :type show: bool
    :param show: Whether to show the figure after plotting or not. Can be used
        to do further customization of the plot before showing it.
    :type fig: :class:`matplotlib.figure.Figure`
    :param fig: Figure instance to reuse, returned from a previous
        :func:`plot_basemap` call. If a previous basemap plot is reused, any
        kwargs regarding the basemap plot setup will be ignored (i.e.
        `projection`, `resolution`, `continent_fill_color`,
        `water_fill_color`). Note that multiple plots using colorbars likely
        are problematic, but e.g. one station plot (without colorbar) and one
        event plot (with colorbar) together should work well.
    """
    import matplotlib.pyplot as plt

    if any([isinstance(c, (datetime.datetime, UTCDateTime)) for c in color]):
        datetimeplot = True
        color = [
            (np.isfinite(float(t)) and
             date2num(getattr(t, 'datetime', t)) or
             np.nan)
            for t in color]
    else:
        datetimeplot = False

    # The colorbar should only be plotted if more then one event is
    # present.
    if colorbar is None:
        if len(lons) > 1 and hasattr(color, "__len__") and \
                not isinstance(color, (str, native_str)):
            colorbar = True
        else:
            colorbar = False

    if fig is None:
        fig = plt.figure()

        if projection == "local":
            ax_x0, ax_width = 0.10, 0.80
        elif projection == "global":
            ax_x0, ax_width = 0.01, 0.98
        else:
            ax_x0, ax_width = 0.05, 0.90

        if colorbar:
            map_ax = fig.add_axes([ax_x0, 0.13, ax_width, 0.77])
            cm_ax = fig.add_axes([ax_x0, 0.05, ax_width, 0.05])
        else:
            ax_y0, ax_height = 0.05, 0.85
            if projection == "local":
                ax_y0 += 0.05
                ax_height -= 0.05
            map_ax = fig.add_axes([ax_x0, ax_y0, ax_width, ax_height])
        bmap = None

    else:
        error_message_suffix = (
            ". Please provide a figure object from a previous call to the "
            ".plot() method of e.g. an Inventory or Catalog object.")
        try:
            map_ax = fig.axes[0]
        except IndexError as e:
            e.args = tuple([e.args[0] + error_message_suffix] +
                           list(e.args[1:]))
            raise
        try:
            bmap = fig.bmap
        except AttributeError as e:
            e.args = tuple([e.args[0] + error_message_suffix] +
                           list(e.args[1:]))
            raise

    # basemap plots will break with basemap 1.1.0 together with matplotlib
    # >=2.3 (see matplotlib/basemap#382) so only thing we can do is show a
    # nicer message.
    # XXX can be removed maybe a year or so after basemap
    # 1.1.1 or 1.2.0 is released
    try:
        from matplotlib.cbook import MatplotlibDeprecationWarning
    except ImportError:
        # matplotlib 1.2.0 does not have that warning class yet
        # XXX can be removed when minimum matplotlib version gets bumped to
        # XXX 1.3.0
        category = {}
    else:
        category = {'category': MatplotlibDeprecationWarning}
    try:
        with warnings.catch_warnings():
            warnings.filterwarnings(
                'ignore', message='The axesPatch function was deprecated '
                'in version 2.1. Use Axes.patch instead.',
                module='.*basemap.*', **category)
            scatter = _plot_basemap_into_axes(
                ax=map_ax, lons=lons, lats=lats, size=size, color=color,
                bmap=bmap, labels=labels, projection=projection,
                resolution=resolution,
                continent_fill_color=continent_fill_color,
                water_fill_color=water_fill_color, colormap=colormap,
                marker=marker, title="", adjust_aspect_to_colorbar=colorbar,
                **kwargs)
    except AttributeError as e:
        if 'axesPatch' not in str(e):
            raise
        msg = ('Encountered a problem doing the basemap plot due to a known '
               'issue of matplotlib >=2.3 together with basemap <=1.1.0 (see '
               'https://github.com/matplotlib/basemap/issues/382). Please '
               'update basemap to a version >1.1.0 if available or downgrade '
               'matplotlib to a version <2.3.')
        raise Exception(msg)

    if title:
        plt.suptitle(title)

    if colorbar:
        if colorbar_ticklabel_format is not None:
            if isinstance(colorbar_ticklabel_format, (str, native_str)):
                formatter = FormatStrFormatter(colorbar_ticklabel_format)
            elif hasattr(colorbar_ticklabel_format, '__call__'):
                formatter = FuncFormatter(colorbar_ticklabel_format)
            elif isinstance(colorbar_ticklabel_format, Formatter):
                formatter = colorbar_ticklabel_format
            locator = MaxNLocator(5)
        else:
            if datetimeplot:
                locator = AutoDateLocator()
                formatter = AutoDateFormatter(locator)
                # Compat with old matplotlib versions.
                if hasattr(formatter, "scaled"):
                    formatter.scaled[1 / (24. * 60.)] = '%H:%M:%S'
            else:
                locator = None
                formatter = None

        # normal case: axes for colorbar was set up in this routine
        if "cm_ax" in locals():
            cb_kwargs = {"cax": cm_ax}
        # unusual case: reusing a plot that has no colorbar set up previously
        else:
            cb_kwargs = {"ax": map_ax}

        cb = fig.colorbar(
            mappable=scatter, cmap=colormap, orientation='horizontal',
            ticks=locator, format=formatter, **cb_kwargs)
        # Compat with old matplotlib versions.
        if hasattr(cb, "update_ticks"):
            cb.update_ticks()

    if show:
        plt.show()

    return fig
Пример #30
0
    def show(self,
             var=None,
             data=None,
             min=None,
             max=None,
             reset=None,
             fixrange=None,
             filenameout=None):
        """Draw the plotting-window"""

        t0 = default_timer()

        self.update(var=var,
                    data=data,
                    min=min,
                    max=max,
                    reset=reset,
                    fixrange=fixrange,
                    filenameout=filenameout)

        self.viewXrange = self.ax.xaxis.get_view_interval()
        self.viewYrange = self.ax.yaxis.get_view_interval()
        self.figure.clf()
        self.ax = self.figure.gca()
        self.ax.set_rasterization_zorder(-9)
        self.info()

        if self.fixzoom is None:
            self.ax.set_xlim(self.xrange[0], self.xrange[1])
            self.ax.set_ylim(self.yrange[0], self.yrange[1])
        else:
            self.ax.set_xlim(self.viewXrange)
            self.ax.set_ylim(self.viewYrange)
        self.ax.set_aspect('equal')

        valueRange = self.max - self.min
        if valueRange == 0.:
            valueRange = 1

        self.valueClip = np.clip(self.value, self.min, self.max)

        # Now regrid and imshow:
        tdata0 = default_timer()
        xrange = [self.ax.get_xlim()[0], self.ax.get_xlim()[1]]
        yrange = [self.ax.get_ylim()[0], self.ax.get_ylim()[1]]

        nregrid = [self.dpi * self.fig_w, self.dpi * self.fig_h]

        if (self.swap == 0):
            CC = self.data.getCenterPoints()
        else:
            CC = self.data.getCenterPoints()[:, [1, 0]]
        tmp0 = np.complex(0, nregrid[0])
        tmp1 = np.complex(0, nregrid[1])
        grid_x, grid_y = np.mgrid[xrange[0]:xrange[1]:tmp0,
                                  yrange[0]:yrange[1]:tmp1]

        # regrid with linear interpolation and fall back to nearest neighbor at NaN, which should just be the borders.
        gridvar = griddata(CC,
                           self.valueClip, (grid_x, grid_y),
                           method='cubic',
                           fill_value=float(np.NaN))
        isnan = np.isnan(gridvar)
        gridvar[isnan] = griddata(CC,
                                  self.value, (grid_x[isnan], grid_y[isnan]),
                                  method='nearest',
                                  fill_value=float(np.NaN))

        # smooth the data slightly:
        gridvar = ndimage.gaussian_filter(gridvar, sigma=self.smooth)

        extent = xrange[0], xrange[1], yrange[0], yrange[1]
        gridvarClip = np.clip(gridvar, self.min, self.max)
        self.image = gridvarClip
        tdata1 = default_timer()

        im2 = self.ax.imshow(gridvarClip.transpose(),
                             cmap=plt.cm.get_cmap(self.cmap, self.nlevels),
                             interpolation='bicubic',
                             extent=extent,
                             origin='lower',
                             zorder=-10)
        norm = colors.Normalize(vmin=self.min, vmax=self.max)
        im2.set_norm(norm)
        #        Q=velovect(self.data.u1,self.data.u2,self.data,nvect=[20,20],scale=30,fig=self)

        if self.grid is not None:
            if (self.swap == 0):
                [myxlist, myylist] = self.data.getPointList()
            else:
                [myylist, myxlist] = self.data.getPointList()
            self.ax.fill(myxlist,
                         myylist,
                         facecolor='none',
                         edgecolor=self.edgecolor,
                         aa=True,
                         linewidth=0.2,
                         alpha=0.8)

        if self.blocks is not None:
            [myxBlockList,
             myyBlockList] = self.data.getPieces(self.blockWidth,
                                                 self.blockHeight,
                                                 self.nlevel1)
            self.ax.fill(myxBlockList,
                         myyBlockList,
                         facecolor='none',
                         edgecolor=self.edgecolor,
                         aa=True,
                         linewidth=0.4,
                         alpha=0.5)

        if self.orientation is not None:
            self.colorbar()

# ticks:
        if self.maxXticks is not None:
            self.ax.xaxis.set_major_locator(MaxNLocator(self.maxXticks - 1))
        if self.maxYticks is not None:
            self.ax.yaxis.set_major_locator(MaxNLocator(self.maxYticks - 1))

        for tick in self.ax.xaxis.get_major_ticks():
            tick.label1.set_fontsize(self.fontsize)
        for tick in self.ax.yaxis.get_major_ticks():
            tick.label1.set_fontsize(self.fontsize)
        self.ax.xaxis.get_offset_text().set_fontsize(self.fontsize - 2)
        self.ax.yaxis.get_offset_text().set_fontsize(self.fontsize - 2)

        tend = default_timer()
        print('time for arranging the data= %f sec' % (tdata1 - tdata0))
        print('Execution time = %f sec' % (tend - t0))
        print('=======================================================')
        if self.filenameout is None:
            plt.draw()

# Make the main axis active:
        plt.sca(self.ax)