Example #1
0
    def graficoEnElTiempo(self, doc_count, key, filename):




            # Fixing random state for reproducibility
            np.random.seed(19680801)

            # create random data
            xdata1 = doc_count
            xdata2 = key

            # sort the data so it makes clean curves
            xdata1.sort()
            xdata2.sort()

            # create some y data points
            ##ydata1 = xdata1 ** 2
            ##ydata2 = 1 - xdata2 ** 3

            # plot the data
            fig = plt.figure()
            ax = fig.add_subplot(1, 1, 1)
            ax.plot(xdata1, color='tab:blue')
            ax.plot(xdata2, color='tab:orange')

            # create the events marking the x data points
            xevents1 = EventCollection(xdata1, color='tab:blue', linelength=0.05)
            xevents2 = EventCollection(xdata2, color='tab:orange', linelength=0.05)

            # create the events marking the y data points
            #yevents1 = EventCollection(ydata1, color='tab:blue', linelength=0.05,
                                       orientation='vertical')
            #yevents2 = EventCollection(ydata2, color='tab:orange', linelength=0.05,
                                       orientation='vertical')
Example #2
0
def event_collection_1():
    # Fixing random state for reproducibility
    np.random.seed(19680801)

    # create random data
    xdata = np.random.random([2, 10])

    # split the data into two parts
    xdata1 = xdata[0, :]
    xdata2 = xdata[1, :]

    # sort the data so it makes clean curves
    xdata1.sort()
    xdata2.sort()

    # create some y data points
    ydata1 = xdata1 ** 2
    ydata2 = 1 - xdata2 ** 3

    # plot the data
    fig = plt.figure()
    ax = fig.add_subplot(1, 1, 1)
    ax.plot(xdata1, ydata1, color='tab:blue')
    ax.plot(xdata2, ydata2, color='tab:orange')

    # create the events marking the x data points
    xevents1 = EventCollection(xdata1, color='tab:blue', linelength=0.05)
    xevents2 = EventCollection(xdata2, color='tab:orange', linelength=0.05)

    # create the events marking the y data points
    yevents1 = EventCollection(
        ydata1,
        color='tab:blue',
        linelength=0.05,
        orientation='vertical'
    )
    yevents2 = EventCollection(
        ydata2,
        color='tab:orange',
        linelength=0.05,
        orientation='vertical'
    )

    # add the events to the axis
    ax.add_collection(xevents1)
    ax.add_collection(xevents2)
    ax.add_collection(yevents1)
    ax.add_collection(yevents2)

    # set the limits
    ax.set_xlim([0, 1])
    ax.set_ylim([0, 1])

    ax.set_title('line plot with data points')

    # display the plot
    plt.show()
Example #3
0
def generate_matplotlib_pdf(uid, tag, units, digest, summary, frame_count):
    # Generate x/y points
    x = []
    y = []
    left = q_graph_min * q_graph_scalar
    right = q_graph_max * q_graph_scalar
    step = (right - left) * 0.01
    for q in range(int(left), int(right + step), int(step)):
        pct = q / q_graph_scalar
        x.append(pct)
        y.append(digest.quantile(pct))

    # Generate x event points
    xevents = []
    t = 0
    for c in digest.centroids:
        t += weight(c)
        xevents.append(t / digest.total_weight)

    # Draw the plot
    fig = plt.figure()
    ax = fig.add_subplot()

    ax.set_title('{} PDF'.format(to_pretty_name(tag)))
    ax.set_xlabel('Probability Distribution')
    ax.set_xlim([0, 1])
    ax.set_ylabel('{} - {}'.format(to_pretty_name(tag), units))

    ax.plot(x, y)

    event_length = (y[-1] - y[0]) / 20
    ax.add_collection(
        EventCollection(xevents,
                        lineoffset=y[0] - event_length,
                        linelength=event_length))

    # Use the graph to determine text placement
    if digest.quantile(.5) > get_midpoint(digest):
        x_txt, y_txt = 0.65, 0.07
    else:
        x_txt, y_txt = 0.05, 0.7

    ax.text(x_txt,
            y_txt,
            to_text_string(summary, frame_count),
            transform=ax.transAxes,
            fontsize=14,
            verticalalignment='bottom',
            horizontalalignment='left',
            bbox=dict(boxstyle='round', facecolor='aliceblue', alpha=0.5))

    fig.savefig('{}-{}-pdf.png'.format(uid, tag))
Example #4
0
def generate_matplotlib_cdf(uid, tag, units, digest, summary, frame_count):
    # Generate x/y points
    x = []
    y = []
    left = digest.quantile(q_graph_min)
    right = digest.quantile(q_graph_max)
    step = (right - left) * 0.01
    for w in range(int(left), int(right + step), int(step)):
        x.append(w)
        y.append(digest.cdf(w))

    xevents = [mean(c) for c in digest.centroids]

    # Draw the plot
    fig = plt.figure()
    ax = fig.add_subplot()

    ax.set_title('{} CDF'.format(to_pretty_name(tag)))
    ax.set_ylabel('Cumulative Density')
    ax.set_ylim([0, 1])
    ax.set_xlabel('{} - {}'.format(to_pretty_name(tag), units))

    ax.plot(x, y)

    ax.add_collection(EventCollection(xevents, linelength=0.05))

    # Use the graph to determine text placement
    if digest.quantile(.5) < get_midpoint(digest):
        x_txt, y_txt = 0.65, 0.07
    else:
        x_txt, y_txt = 0.05, 0.7

    ax.text(x_txt,
            y_txt,
            to_text_string(summary, frame_count),
            transform=ax.transAxes,
            fontsize=14,
            verticalalignment='bottom',
            horizontalalignment='left',
            bbox=dict(boxstyle='round', facecolor='aliceblue', alpha=0.5))

    fig.savefig('{}-{}-cdf.png'.format(uid, tag))
Example #5
0
def generate_EventCollection_plot():
    '''
    generate the initial collection and plot it
    '''
    positions = np.array([0., 1., 2., 3., 5., 8., 13., 21.])
    extra_positions = np.array([34., 55., 89.])
    orientation = 'horizontal'
    lineoffset = 1
    linelength = .5
    linewidth = 2
    color = [1, 0, 0, 1]
    linestyle = 'solid'
    antialiased = True

    coll = EventCollection(positions,
                           orientation=orientation,
                           lineoffset=lineoffset,
                           linelength=linelength,
                           linewidth=linewidth,
                           color=color,
                           linestyle=linestyle,
                           antialiased=antialiased)

    fig = plt.figure()
    splt = fig.add_subplot(1, 1, 1)
    splt.add_collection(coll)
    splt.set_title('EventCollection: default')
    props = {
        'positions': positions,
        'extra_positions': extra_positions,
        'orientation': orientation,
        'lineoffset': lineoffset,
        'linelength': linelength,
        'linewidth': linewidth,
        'color': color,
        'linestyle': linestyle,
        'antialiased': antialiased
    }
    splt.set_xlim(-1, 22)
    splt.set_ylim(0, 2)
    return splt, coll, props
Example #6
0
xdata1 = xdata[0, :]
xdata2 = xdata[1, :]

xdata1.sort()
xdata2.sort()

ydata1 = xdata1**2
ydata2 = 1 - xdata2**3

fig = plt.figure()
fig.suptitle("line plot with data points")

ax = fig.add_subplot(1, 1, 1)
ax.plot(xdata1, ydata2, 'r', xdata2, ydata2, 'b')

xevents1 = EventCollection(xdata1, color=[1, 0, 0], linelength=0.05)
xevents2 = EventCollection(xdata2, color=[0, 0, 1], linelength=0.05)

yevents1 = EventCollection(ydata1,
                           color=[1, 0, 0],
                           linelength=0.05,
                           orientation='vertical')
yevents2 = EventCollection(ydata2,
                           color=[0, 0, 1],
                           linelength=0.05,
                           orientation='vertical')

ax.add_collection(xevents1)
ax.add_collection(xevents2)
ax.add_collection(yevents1)
ax.add_collection(yevents2)
Example #7
0
def test_EventCollection_nosort():
    # Check that EventCollection doesn't modify input in place
    arr = np.array([3, 2, 1, 10])
    coll = EventCollection(arr)
    np.testing.assert_array_equal(arr, np.array([3, 2, 1, 10]))
Example #8
0
    def scatter(self, x, y, xfunc=None, yfunc=None, xscale=None, yscale=None,
                xlab=None, ylab=None, genes_to_highlight=None,
                label_genes=False, marginal_histograms=False,
                general_kwargs=dict(color="k", alpha=0.2, linewidths=0),
                general_hist_kwargs=None,
                offset_kwargs={}, label_kwargs=None, ax=None,
                one_to_one=None, callback=None, xlab_prefix=None,
                ylab_prefix=None, sizefunc=None, hist_size=0.3, hist_pad=0.0,
                nan_offset=0.015, pos_offset=0.99, linelength=0.01,
                neg_offset=0.005):
        """
        Do-it-all method for making annotated scatterplots.

        Parameters
        ----------

        x, y : array-like
            Variables to plot.  Must be names in self.data's DataFrame.  For
            example, "baseMeanA" and "baseMeanB"

        xfunc, yfunc : callable
            Functions to apply to `xvar` and `yvar` respectively. Default is
            log2; set to None to have no transformation.

        xlab, ylab : string
            Labels for x and y axes; default is to use function names for
            `xfunc` and `yfunc` and variable names `xvar` and `yvar`, e.g.,
            "log2(baseMeanA)"

        ax : None or Axes object
            If `ax=None`, then makes a new fig and returns the Axes object,
            otherwise, plots onto `ax`

        general_kwargs : dict
            Kwargs for matplotlib.scatter; specifies how all points look

        genes_to_highlight : list of (index, dict) tuples
            Provides lots of control to colors.  It is a list of (`ind`,
            `kwargs`) tuples, where each `ind` specifies genes to plot with
            `kwargs`.  Each dictionary updates a copy of `general_kwargs`. If
            `genes_to_highlight` has a "name" kwarg, this must be a list that't
            the same length as `ind`.  It will be used to label the genes in
            `ind` using `label_kwargs`.

        callback : callable
            Function to call upon clicking a point. Default is to print the
            gene name, but an example of another useful callback would be
            a mini-browser connected to a genomic_signal object from which the
            expression data were calculated.

        one_to_one : None or dict
            If not None, a dictionary of matplotlib.plot kwargs that will be
            used to plot a 1:1 line.

        label_kwargs : dict
            Kwargs for labeled genes (e.g., dict=(style='italic')).  Will only
            be used if an entry in `genes_to_highlight` has a `name` key.

        offset_kwargs : dict
            Kwargs to be passed to matplotlib.transforms.offset_copy, used for
            adjusting the positioning of gene labels in relation to the actual
            point.

        xlab_prefix, ylab_prefix : str
            Optional label prefix that will be added to the beginning of `xlab`
            and/or `ylab`.

        hist_size : float
            Size of marginal histograms

        hist_pad : float
            Spacing between marginal histograms

        nan_offset, pos_offset, neg_offset : float
            Offset, in units of "fraction of axes" for the NaN, +inf, and -inf
            "rug plots"

        linelength : float
            Line length for the rug plots

        """
        _x = self.data[x]
        _y = self.data[y]

        # Construct defaults---------------------------------------------------
        def identity(x):
            return x.copy()

        # Axis label setup
        if xlab_prefix is None:
            xlab_prefix = ""

        if ylab_prefix is None:
            ylab_prefix = ""

        if xlab is None:
            xlab = x

            if xfunc is not None:
                xlab = xlab_prefix + "%s(%s)" % (xfunc.__name__, str(x))
            else:
                xlab = xlab_prefix + "%s" % (str(x))

        if ylab is None:
            ylab = y
            if yfunc is not None:
                ylab = ylab_prefix + "%s(%s)" % (yfunc.__name__, str(y))
            else:
                ylab = ylab_prefix + "%s" % (str(y))

        if xfunc is None:
            xfunc = identity

        if yfunc is None:
            yfunc = identity

        if general_kwargs is None:
            general_kwargs = {}

        if general_hist_kwargs is None:
            general_hist_kwargs = {}

        if genes_to_highlight is None:
            genes_to_highlight = []

        if ax is None:
            fig = plt.figure()
            ax = fig.add_subplot(111)

        if label_kwargs is None:
            label_kwargs = dict(horizontalalignment='right',
                                verticalalignment='center', style='italic',
                                bbox=dict(facecolor='w', edgecolor='None',
                                          alpha=0.5))

        # Clean data ---------------------------------------------------------
        xi = xfunc(_x)
        yi = yfunc(_y)

        # handle inf, -inf, and NaN
        x_is_pos_inf = np.isinf(xi) & (xi > 0)
        x_is_neg_inf = np.isinf(xi) & (xi < 0)
        x_is_nan = np.isnan(xi)
        y_is_pos_inf = np.isinf(yi) & (yi > 0)
        y_is_neg_inf = np.isinf(yi) & (yi < 0)
        y_is_nan = np.isnan(yi)

        # Indexes for valid values
        x_valid = ~(x_is_pos_inf | x_is_neg_inf | x_is_nan)
        y_valid = ~(y_is_pos_inf | y_is_neg_inf | y_is_nan)

        gmin = max(xi[x_valid].min(), yi[y_valid].min())
        gmax = min(xi[x_valid].max(), yi[y_valid].max())


        # Convert any integer indexes into boolean, and create a new list of
        # genes to highlight.  This handles optional hist kwargs.
        allind = np.zeros_like(xi) == 0
        _genes_to_highlight = []
        for block in genes_to_highlight:
            ind = block[0]
            if ind.dtype != 'bool':
                new_ind = (np.zeros_like(xi) == 0)
                new_ind[ind] = True
                _genes_to_highlight.append(
                    tuple([new_ind] + list(block[1:]))
                )
            else:
                if hasattr(ind, 'values'):
                    ind = ind.values
                _genes_to_highlight.append(
                    tuple([ind] + list(block[1:]))
                )

        # Remove any genes that will be plotted by genes_to_highlight.  This
        # avoids double-plotting.
        for block in _genes_to_highlight:
            ind = block[0]
            allind[ind] = False

        # Copy over the color and alpha if they're not specified
        general_hist_kwargs = plotutils._updatecopy(
            orig=general_hist_kwargs, update_with=general_kwargs,
            keys=['color', 'alpha'])

        # Put the non-highlighted genes at the beginning of _genes_to_highlight
        # so we can just iterate over that.
        _genes_to_highlight.insert(
            0,
            (allind, general_kwargs, general_hist_kwargs)
        )

        # Set up the object that will handle the marginal histograms
        self.marginal = plotutils.MarginalHistScatter(
            ax, hist_size=hist_size, pad=hist_pad)


        # Set up kwargs for x and y rug plots
        rug_x_kwargs = dict(
            linelength=linelength,
            transform=blended_transform_factory(ax.transData, ax.transAxes),
        )
        rug_y_kwargs = dict(
            linelength=linelength,
            transform=blended_transform_factory(ax.transAxes, ax.transData),
            orientation='vertical',
        )

        # EventCollection objects need a color as a 3-tuple, so set up
        # a converter here.
        color_converter = matplotlib.colors.ColorConverter().to_rgb

        # Plot the one-to-one line, if kwargs were specified
        if one_to_one:
            ax.plot([gmin, gmax],
                    [gmin, gmax],
                    **one_to_one)

        # Plot any specially-highlighted genes, and label if specified
        for block in _genes_to_highlight:
            ind = block[0]
            kwargs = block[1]

            if len(block) == 3:
                hist_kwargs = block[2]
            else:
                hist_kwargs = {}

            names = kwargs.pop('names', None)
            _marginal_histograms = (
                kwargs.pop('marginal_histograms', False) or
                marginal_histograms)

            updated_kwargs = plotutils._updatecopy(
                orig=kwargs, update_with=general_kwargs)

            updated_hist_kwargs = plotutils._updatecopy(
                orig=hist_kwargs, update_with=general_hist_kwargs)
            updated_hist_kwargs = plotutils._updatecopy(
                orig=updated_hist_kwargs, update_with=kwargs,
                keys=['color', 'alpha'], override=True)

            xhist_kwargs = updated_kwargs.pop('xhist_kwargs', None)
            yhist_kwargs = updated_kwargs.pop('yhist_kwargs', None)

            self.marginal.append(
                xi[ind & x_valid & y_valid],
                yi[ind & x_valid & y_valid],
                scatter_kwargs=dict(picker=5, **updated_kwargs),
                hist_kwargs=updated_hist_kwargs,
                xhist_kwargs=xhist_kwargs,
                yhist_kwargs=yhist_kwargs,
                marginal_histograms=_marginal_histograms)
            coll = self.marginal.scatter_ax.collections[-1]
            coll.df = self.data
            coll.ind = ind

            color = color_converter(updated_kwargs['color'])
            rug_x_kwargs['color'] = color
            rug_y_kwargs['color'] = color

            # Note: if both x and y are not valid, then they will not be on the
            # plot.
            items = [
                # top rug, y is +inf and x is valid
                (xi, ind & x_valid & y_is_pos_inf, pos_offset, rug_x_kwargs),

                # one of the bottom rugs, where y is NaN
                (xi, ind & x_valid & y_is_nan, nan_offset, rug_x_kwargs),

                # bottom rug, y is -inf
                (xi, ind & x_valid & y_is_neg_inf, neg_offset, rug_x_kwargs),

                # right rug, x is +inf
                (yi, ind & y_valid & x_is_pos_inf, pos_offset, rug_y_kwargs),

                # one of the left rugs; x is NaN
                (yi, ind & y_valid & x_is_nan, nan_offset, rug_y_kwargs),

                # left rug, x is -inf
                (yi, ind & y_valid & x_is_neg_inf, neg_offset, rug_y_kwargs),
            ]
            for values, index, offset, kwargs in items:
                coll = EventCollection(
                    values[index], lineoffset=offset, **kwargs)
                coll.df = self.data
                coll.ind = index
                ax.add_collection(coll)

            if names:
                transOffset = matplotlib.transforms.offset_copy(
                    ax.transData, fig=ax.figure, **offset_kwargs)

                for xii, yii, name in zip(xi[ind], yi[ind], names):
                    ax.text(xii,
                            yii,
                            name,
                            transform=transOffset,
                            **label_kwargs)

        # register callback
        if callback is not None:
            def wrapped_callback(event):
                return callback(self._id_callback(event))

        else:
            def wrapped_callback(event):
                return self._default_callback(self._id_callback(event))

        ax.figure.canvas.mpl_connect('pick_event', wrapped_callback)

        ax.set_xlabel(xlab)
        ax.set_ylabel(ylab)
        ax.axis('tight')

        #ax.axis((xmin - xpad, xmax + xpad, ymin - ypad, ymax + ypad))
        return ax
Example #9
0
# sort the data so it makes clean curves
xdata1.sort()
xdata2.sort()

# create some y data points
ydata1 = xdata1**2
ydata2 = 1 - xdata2**3

# plot the data
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.plot(xdata1, ydata1, color='tab:blue')
ax.plot(xdata2, ydata2, color='tab:orange')

# create the events marking the x data points
xevents1 = EventCollection(xdata1, color='tab:blue', linelength=0.05)
xevents2 = EventCollection(xdata2, color='tab:orange', linelength=0.05)

# create the events marking the y data points
yevents1 = EventCollection(ydata1,
                           color='tab:blue',
                           linelength=0.05,
                           orientation='vertical')
yevents2 = EventCollection(ydata2,
                           color='tab:orange',
                           linelength=0.05,
                           orientation='vertical')

# add the events to the axis
ax.add_collection(xevents1)
ax.add_collection(xevents2)
Example #10
0
    def scatter(self,
                x,
                y,
                xfunc=None,
                yfunc=None,
                xscale=None,
                yscale=None,
                xlab=None,
                ylab=None,
                genes_to_highlight=None,
                label_genes=False,
                marginal_histograms=False,
                general_kwargs=dict(color="k", alpha=0.2, picker=True),
                general_hist_kwargs=None,
                offset_kwargs={},
                label_kwargs=None,
                ax=None,
                one_to_one=None,
                callback=None,
                xlab_prefix=None,
                ylab_prefix=None,
                sizefunc=None,
                hist_size=0.3,
                hist_pad=0.0,
                nan_offset=0.015,
                pos_offset=0.99,
                linelength=0.01,
                neg_offset=0.005,
                figure_kwargs=None):
        """
        Do-it-all method for making annotated scatterplots.

        Parameters
        ----------

        x, y : array-like
            Variables to plot.  Must be names in self.data's DataFrame.  For
            example, "baseMeanA" and "baseMeanB"

        xfunc, yfunc : callable
            Functions to apply to `xvar` and `yvar` respectively. Default is
            log2; set to None to have no transformation.

        xlab, ylab : string
            Labels for x and y axes; default is to use function names for
            `xfunc` and `yfunc` and variable names `xvar` and `yvar`, e.g.,
            "log2(baseMeanA)"

        ax : None or Axes object
            If `ax=None`, then makes a new fig and returns the Axes object,
            otherwise, plots onto `ax`

        general_kwargs : dict
            Kwargs for matplotlib.scatter; specifies how all points look

        genes_to_highlight : list of (index, dict) tuples
            Provides lots of control to colors.  It is a list of (`ind`,
            `kwargs`) tuples, where each `ind` specifies genes to plot with
            `kwargs`.  Each dictionary updates a copy of `general_kwargs`. If
            `genes_to_highlight` has a "name" kwarg, this must be a list that't
            the same length as `ind`.  It will be used to label the genes in
            `ind` using `label_kwargs`.

        callback : callable
            Function to call upon clicking a point. Must accept a single
            argument which is the gene ID. Default is to print the gene name,
            but an example of another useful callback would be a mini-browser
            connected to a genomic_signal object from which the expression data
            were calculated.

        one_to_one : None or dict
            If not None, a dictionary of matplotlib.plot kwargs that will be
            used to plot a 1:1 line.

        label_kwargs : dict
            Kwargs for labeled genes (e.g., dict=(style='italic')).  Will only
            be used if an entry in `genes_to_highlight` has a `name` key.

        offset_kwargs : dict
            Kwargs to be passed to matplotlib.transforms.offset_copy, used for
            adjusting the positioning of gene labels in relation to the actual
            point.

        xlab_prefix, ylab_prefix : str
            Optional label prefix that will be added to the beginning of `xlab`
            and/or `ylab`.

        hist_size : float
            Size of marginal histograms

        hist_pad : float
            Spacing between marginal histograms

        nan_offset, pos_offset, neg_offset : float
            Offset, in units of "fraction of axes" for the NaN, +inf, and -inf
            "rug plots"

        linelength : float
            Line length for the rug plots

        """
        _x = self.data[x]
        _y = self.data[y]

        # Construct defaults---------------------------------------------------
        def identity(x):
            return x.copy()

        # Axis label setup
        if xlab_prefix is None:
            xlab_prefix = ""

        if ylab_prefix is None:
            ylab_prefix = ""

        if xlab is None:
            xlab = x

            if xfunc is not None:
                xlab = xlab_prefix + "%s(%s)" % (xfunc.__name__, str(x))
            else:
                xlab = xlab_prefix + "%s" % (str(x))

        if ylab is None:
            ylab = y
            if yfunc is not None:
                ylab = ylab_prefix + "%s(%s)" % (yfunc.__name__, str(y))
            else:
                ylab = ylab_prefix + "%s" % (str(y))

        if xfunc is None:
            xfunc = identity

        if yfunc is None:
            yfunc = identity

        if general_kwargs is None:
            general_kwargs = {}

        if general_hist_kwargs is None:
            general_hist_kwargs = {}

        if genes_to_highlight is None:
            genes_to_highlight = []

        if ax is None:
            if figure_kwargs is None:
                figure_kwargs = {}
            fig = plt.figure(**figure_kwargs)
            ax = fig.add_subplot(111)

        if label_kwargs is None:
            label_kwargs = dict(horizontalalignment='right',
                                verticalalignment='center',
                                style='italic',
                                bbox=dict(facecolor='w',
                                          edgecolor='None',
                                          alpha=0.5))

        # Clean data ---------------------------------------------------------
        xi = xfunc(_x)
        yi = yfunc(_y)

        # handle inf, -inf, and NaN
        x_is_pos_inf = np.isinf(xi) & (xi > 0)
        x_is_neg_inf = np.isinf(xi) & (xi < 0)
        x_is_nan = np.isnan(xi)
        y_is_pos_inf = np.isinf(yi) & (yi > 0)
        y_is_neg_inf = np.isinf(yi) & (yi < 0)
        y_is_nan = np.isnan(yi)

        # Indexes for valid values
        x_valid = ~(x_is_pos_inf | x_is_neg_inf | x_is_nan)
        y_valid = ~(y_is_pos_inf | y_is_neg_inf | y_is_nan)

        # global min/max
        gmin = max(xi[x_valid].min(), yi[y_valid].min())
        gmax = min(xi[x_valid].max(), yi[y_valid].max())

        # Convert any integer indexes into boolean, and create a new list of
        # genes to highlight.  This handles optional hist kwargs.

        # We'll compile a new list of genes to highlight.
        _genes_to_highlight = []

        for block in genes_to_highlight:
            ind = block[0]

            # Convert to boolean
            if ind.dtype != 'bool':
                new_ind = (np.zeros_like(xi) == 0)
                new_ind[ind] = True
                _genes_to_highlight.append(tuple([new_ind] + list(block[1:])))

            # If it's a DataFrame, we only want the boolean values;
            else:
                if hasattr(ind, 'values'):
                    ind = ind.values
                _genes_to_highlight.append(tuple([ind] + list(block[1:])))

        # Now we remove any genes from in allind (which will be plotted using
        # `general_kwargs`) that will be plotted by genes_to_highlight.  This
        # avoids double-plotting.
        allind = np.zeros_like(xi) == 0
        for block in _genes_to_highlight:
            ind = block[0]
            allind[ind] = False

        # Copy over the color and alpha if they're not specified
        general_hist_kwargs = plotutils._updatecopy(orig=general_hist_kwargs,
                                                    update_with=general_kwargs,
                                                    keys=['color', 'alpha'])

        # Put the non-highlighted genes at the beginning of _genes_to_highlight
        # list so we can just iterate over one list
        _genes_to_highlight.insert(
            0, (allind, general_kwargs, general_hist_kwargs))

        # Set up the object that will handle the marginal histograms
        self.marginal = plotutils.MarginalHistScatter(ax,
                                                      hist_size=hist_size,
                                                      pad=hist_pad)

        # Set up kwargs for x and y rug plots
        rug_x_kwargs = dict(
            linelength=linelength,
            transform=blended_transform_factory(ax.transData, ax.transAxes),
        )
        rug_y_kwargs = dict(
            linelength=linelength,
            transform=blended_transform_factory(ax.transAxes, ax.transData),
            orientation='vertical',
        )

        # EventCollection objects need a color as a 3-tuple, so set up
        # a converter here.
        color_converter = matplotlib.colors.ColorConverter().to_rgb

        # Plot the one-to-one line, if kwargs were specified
        if one_to_one:
            ax.plot([gmin, gmax], [gmin, gmax], **one_to_one)

        # Plot 'em all, and label if specified

        # In order to avoid calling the callback function multiple times when
        # we have overlapping genes to highlight (e.g., a gene that is both
        # upregulated AND has a peak), keep track of everything that's been
        # added so far.
        self._seen = np.ones_like(xi) == 0
        for block in _genes_to_highlight:
            ind = block[0]
            kwargs = block[1]

            if len(block) == 3:
                hist_kwargs = block[2]
            else:
                hist_kwargs = {}

            names = kwargs.pop('names', None)
            _marginal_histograms = (kwargs.pop('marginal_histograms', False)
                                    or marginal_histograms)

            updated_kwargs = plotutils._updatecopy(orig=kwargs,
                                                   update_with=general_kwargs)

            updated_hist_kwargs = plotutils._updatecopy(
                orig=hist_kwargs, update_with=general_hist_kwargs)
            updated_hist_kwargs = plotutils._updatecopy(
                orig=updated_hist_kwargs,
                update_with=kwargs,
                keys=['color', 'alpha'],
                override=True)

            xhist_kwargs = updated_kwargs.pop('xhist_kwargs', None)
            yhist_kwargs = updated_kwargs.pop('yhist_kwargs', None)

            self.marginal.append(
                xi[ind & x_valid & y_valid],
                yi[ind & x_valid & y_valid],
                scatter_kwargs=dict(**updated_kwargs),
                hist_kwargs=updated_hist_kwargs,
                xhist_kwargs=xhist_kwargs,
                yhist_kwargs=yhist_kwargs,
                marginal_histograms=_marginal_histograms,
            )

            # This is important for callbacks: here we grab the last-created
            # collection,
            coll = self.marginal.scatter_ax.collections[-1]
            coll.df = self.data
            coll.ind = ind & x_valid & y_valid

            color = color_converter(updated_kwargs['color'])
            rug_x_kwargs['color'] = color
            rug_y_kwargs['color'] = color

            # Note: if both x and y are not valid, then they will not be on the
            # plot.
            items = [
                # top rug, y is +inf and x is valid
                (xi, ind & x_valid & y_is_pos_inf, pos_offset, rug_x_kwargs),

                # one of the bottom rugs, where y is NaN
                (xi, ind & x_valid & y_is_nan, nan_offset, rug_x_kwargs),

                # bottom rug, y is -inf
                (xi, ind & x_valid & y_is_neg_inf, neg_offset, rug_x_kwargs),

                # right rug, x is +inf
                (yi, ind & y_valid & x_is_pos_inf, pos_offset, rug_y_kwargs),

                # one of the left rugs; x is NaN
                (yi, ind & y_valid & x_is_nan, nan_offset, rug_y_kwargs),

                # left rug, x is -inf
                (yi, ind & y_valid & x_is_neg_inf, neg_offset, rug_y_kwargs),
            ]
            for values, index, offset, kwargs in items:
                coll = EventCollection(values[index],
                                       lineoffset=offset,
                                       **kwargs)
                coll.df = self.data
                coll.ind = index
                ax.add_collection(coll)

            if names:
                transOffset = matplotlib.transforms.offset_copy(
                    ax.transData, fig=ax.figure, **offset_kwargs)

                for xii, yii, name in zip(xi[ind], yi[ind], names):
                    ax.text(xii,
                            yii,
                            name,
                            transform=transOffset,
                            **label_kwargs)

        # register callback
        if callback is None:
            callback = self._default_callback

        def wrapped_callback(event):
            for _id in self._id_callback(event):
                callback(_id)

        ax.figure.canvas.mpl_connect('pick_event', wrapped_callback)

        ax.set_xlabel(xlab)
        ax.set_ylabel(ylab)
        ax.axis('tight')

        return ax
    def animate(self):
        """
        calling routine for audio, FFT, peak and partials and key finding, and plotting. Listens for events in plot
        window

        :return:
        string
            return code
        """
        _firstplot = True
        plt.ion()  # Stop matplotlib windows from blocking

        # start Recording
        self.stream.start_stream()

        while self.stream.is_active():

            _start = timeit.default_timer()
            logging.info('Started Audio Stream ...')

            time.sleep(self.record_seconds)
            self.stream.stop_stream(
            )  # stop the input stream for the time being
            logging.info('Stopped Audio Stream ...')

            # Convert the list of numpy-arrays into a 1D array (column-wise)
            amp = np.hstack(self.callback_output)
            # clear input stream
            self.callback_output = []

            # interrupt on hotkey 'ctrl-x' and resume on 'esc'
            if self.rc == 'x':
                while self.rc != 'esc':  # loop and wait until ESC ist pressed
                    time.sleep(.1)
                self.rc = None
                self.stream.start_stream()
                logging.info('Dump last audio stream ...')
                continue
            elif self.rc == 'y':
                return self.rc

            _stop = timeit.default_timer()
            logging.debug("time utilized for Audio [s]: " +
                          str(_stop - _start))
            logging.info('Analyzing ...')

            samples = len(amp)
            logging.info('Number of samples: ' + str(samples))
            t = np.arange(samples) / parameters.RATE
            resolution = parameters.RATE / samples
            logging.info('Resolution (Hz/channel): ' + str(resolution))

            # calculate FFT
            t1, yfft = fft(amp=amp)

            # peakfinding
            peaks = peak(frequency=t1, spectrum=yfft)
            peaklist = list(map(itemgetter(0), peaks))

            if peaks is not None:
                f_measured = harmonics(peaks=peaks)  # find the key

            displayed_text = ""
            color = 'none'
            if len(
                    f_measured
            ) != 0:  # if key is found print it and its offset colored red or green
                tone, displaced = self.find(f_measured=f_measured[0])
                if tone:
                    displayed_text = "{2:s} (a1={3:3.0f}Hz) {0:s} offset={1:.0f} cent"\
                        .format(tone, displaced, self.tuning, self.a1)
                    color = 'green' if displaced >= 0 else 'red'

            _start = timeit.default_timer()
            """
            https://stackoverflow.com/questions/40126176/fast-live-plotting-in-matplotlib-pyplot
            """
            if _firstplot:  # instantiate first plot and copy background
                # Setup figure, axis, lines, text and initiate plot once and copy background
                fig = plt.gcf()
                ax = fig.add_subplot(211)
                ax1 = fig.add_subplot(212)
                fig.set_size_inches(12, 8)
                ln, = ax.plot(t, amp)
                ln1, = ax1.plot(t1, yfft)
                text = ax1.text(
                    self.fmax,
                    np.max(yfft),
                    "",
                    # color='',
                    verticalalignment='top',
                    horizontalalignment='right',
                    fontsize=11,
                    fontweight='bold')
                ax.set_xlabel('Time/s')
                ax.set_ylabel('Intensity/arb. units')
                ax1.set_xlabel('Frequency/Hz')
                ax1.set_ylabel('Intensity/arb. units')
                axbackground = fig.canvas.copy_from_bbox(ax.bbox)
                ax1background = fig.canvas.copy_from_bbox(ax1.bbox)
            else:
                # upper subplot
                ln.set_xdata(t)
                ln.set_ydata(amp)
                # lower subplot
                ln1.set_xdata(t1)
                ln1.set_ydata(yfft)
            ax.set_xlim([0., np.max(t)])
            ax1.set_xlim([0., self.fmax])
            # set text attributes of lower subplot
            text.set_text(displayed_text)
            text.set_color(color)
            text.set_x(self.fmax)
            text.set_y(np.max(yfft))

            # remove all collections: beginning from last object
            while ax1.collections:
                ax1.collections.pop()
            yevents = EventCollection(positions=peaklist,
                                      color='tab:orange',
                                      linelength=0.05 * np.max(yfft),
                                      linewidth=2.)
            ax1.add_collection(yevents)
            yevents1 = EventCollection(positions=f_measured,
                                       color='tab:red',
                                       linelength=0.05 * np.max(yfft),
                                       lineoffset=-0.04 * np.max(yfft),
                                       linewidth=2.)
            ax1.add_collection(yevents1)

            # Rescale the axis so that the data can be seen in the plot
            # if you know the bounds of your data you could just set this once
            # so that the axis don't keep changing
            ax.relim()
            ax.autoscale_view()
            ax1.relim()
            ax1.autoscale_view()
            if _firstplot:
                plt.pause(0.001)
                #                fig.canvas.draw()
                _firstplot = False
            else:
                # restore background
                fig.canvas.restore_region(axbackground)
                fig.canvas.restore_region(ax1background)
                # redraw just the points
                ax.draw_artist(ln)
                ax1.draw_artist(ln1)
                ax1.draw_artist(text)
                # fill in the axes rectangle
                fig.canvas.blit(ax.bbox)
                fig.canvas.blit(ax1.bbox)
            ax.set_visible(self.visible)

            fig.canvas.flush_events()
            # resume the audio streaming, expect some retardation for the status change
            self.stream.start_stream()

            _stop = timeit.default_timer()
            logging.debug("time utilized for matplotlib [s]: " +
                          str(_stop - _start))

        return self.rc
Example #12
0
def makePDF(name="name",
            nVertices="nVertices",
            nEdges="nEdges",
            maxValency="maxValency",
            avgValency="avgValency",
            curve="curve",
            diameter="not computed"):
    print(" * Building PDF report")

    import matplotlib.pyplot as plt
    from matplotlib.collections import EventCollection
    import io
    import base64

    # plot the data
    fig = plt.figure()
    ax = fig.add_subplot(1, 1, 1)
    ax.plot(curve['x'], curve['y'], color='tab:red')

    xevents1 = EventCollection(curve['x'], color='tab:blue', linelength=0.5)
    yevents1 = EventCollection(curve['y'],
                               color='tab:blue',
                               linelength=0.5,
                               orientation='vertical')

    # add the events to the axis
    ax.add_collection(xevents1)
    ax.add_collection(yevents1)

    if not diameter:
        diameter = "not computed"

    # set the limits
    ax.set_xlim([0, maxValency + 1])
    ax.set_ylim([0, max(curve['y']) + 5])

    ax.set_title('Distribution des degrés')
    plt.xlabel('Degré')
    plt.ylabel('Frequence d\'apparition (%)')

    # display the plot
    # plt.show()

    my_stringIObytes = io.BytesIO()
    plt.savefig(my_stringIObytes, format='jpg')
    my_stringIObytes.seek(0)
    my_base64_jpgData = base64.b64encode(my_stringIObytes.read())

    with open('template/index.html') as template:

        report_html = template.read().replace('\n', '').replace(
            '\t', '').format(image=my_base64_jpgData,
                             caption='Salut',
                             width=600,
                             height=400,
                             name=name,
                             nVertices=nVertices,
                             nEdges=nEdges,
                             maxValency=maxValency,
                             avgValency=avgValency,
                             diameter=diameter)

        convert_html_to_pdf(report_html, name + '-report.pdf')

        return "report-2.pdf"
# costLabels = numpy.array(costLabels)
# xdata = costLabels[:, 0]
# ydata = costLabels[:, 1]

costLabels = numpy.array(costLabels)
xdata = costLabels[:, 0]
ydata = costLabels[:, 1]

# plot the data
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.plot(xdata, ydata, color='tab:blue')

# create the events marking the x data points
xevents = EventCollection(xdata, color='tab:blue', linelength=0.03)

# create the events marking the y data points
yevents = EventCollection(ydata, color='tab:blue', linelength=0.03,
                          orientation='vertical')

# add the events to the axis
ax.add_collection(xevents)
ax.add_collection(yevents)

# set the limits
ax.set_xlim([0, epoch_num])
ax.set_ylim([0, 5])

ax.set_title('line plot with data points')
        # Data set for the field/column.
        print("	len(simulation_results_database[current_field_name]):",
              len(simulation_results_database[current_field_name]), ".")
        #print(":	simulation_results_database[current_field_name]:",simulation_results_database[current_field_name],".")
        print("	current color:", enumerated_color, ".")
        """
			Plot currently enumerated field (i.e., V_in or V_out)
				on the y-axis against time column/field on the x-axis.
		"""
        ax.plot(simulation_results_database[temp_list_column_headers[1]],
                simulation_results_database[current_field_name],
                color=enumerated_color)
        #xevents1 = EventCollection(simulation_results_database[temp_list_column_headers[1]], color=enumerated_color, linelength=0.05)
        yevents1 = EventCollection(
            simulation_results_database[current_field_name],
            color=enumerated_color,
            linelength=0.05,
            orientation='vertical')
        #ax.add_collection(xevents1)
        ax.add_collection(yevents1)
        """
		ax.plot(temp_list_column_headers[1], ydata1, color='tab:blue')
		xevents1 = EventCollection(xdata1, color='tab:blue', linelength=0.05)
		yevents1 = EventCollection(ydata1, color='tab:blue', linelength=0.05, orientation='vertical')
		ax.add_collection(xevents1)
		ax.add_collection(yevents1)
		ax.plot(xdata2, ydata2, color='tab:orange')
		xevents2 = EventCollection(xdata2, color='tab:orange', linelength=0.05)
		yevents2 = EventCollection(ydata2, color='tab:orange', linelength=0.05, orientation='vertical')
		ax.add_collection(xevents2)
		ax.add_collection(yevents2)
Example #15
0
def generate_graph():
    '''
    Method which is used to draw graph via matplot library
    :return:
    '''
    file = open(
        'A:\\_MACIEK\\WORK\\Develop\\Metaheuristic\\sum_of_subset\\data\\output\\stats_to_graph.csv',
        'rt')
    data = csv.reader(file, delimiter=' ')
    time_brut = []
    time_climb = []
    time_simu = []
    time_tabu = []
    size_brut = []
    size_climb = []
    size_simu = []
    size_tabu = []

    for line in data:
        if str(line[0]) == 'Brut':
            size_brut.append(int(line[1]))
            time_brut.append(float(line[4]))
        elif str(line[0]) == 'Clim':
            size_climb.append(int(line[1]))
            time_climb.append(float(line[4]))
        elif str(line[0]) == 'Simu':
            size_simu.append(int(line[1]))
            time_simu.append(float(line[4]))
        elif str(line[0]) == 'Tabu':
            size_tabu.append(int(line[1]))
            time_tabu.append(float(line[4]))

    xdata1 = size_brut[:]
    ydata1 = time_brut[:]
    xdata2 = size_climb[:]
    ydata2 = time_climb[:]
    xdata3 = size_simu[:]
    ydata3 = time_simu[:]
    xdata4 = size_tabu[:]
    ydata4 = time_tabu[:]
    xdata1.sort()
    ydata1.sort()
    xdata2.sort()
    ydata2.sort()
    xdata3.sort()
    ydata3.sort()
    xdata4.sort()
    ydata4.sort()

    fig = plt.figure()
    ax = fig.add_subplot(1, 1, 1)
    ax.plot(xdata1, ydata1, color='tab:blue', label='Bruteforce')
    ax.plot(xdata2, ydata2, color='tab:green', label='Climbing')
    ax.plot(xdata3, ydata3, color='tab:orange', label='Simulated-Annealing')
    ax.plot(xdata4, ydata4, color='tab:red', label='Tabu')

    xevents1 = EventCollection(xdata1,
                               color='tab:blue',
                               linelength=0.05,
                               orientation='vertical')
    xevents2 = EventCollection(xdata2,
                               color='tab:green',
                               linelength=0.05,
                               orientation='vertical')
    xevents3 = EventCollection(xdata3,
                               color='tab:orange',
                               linelength=0.05,
                               orientation='vertical')
    xevents4 = EventCollection(xdata4,
                               color='tab:red',
                               linelength=0.05,
                               orientation='vertical')

    yevents1 = EventCollection(ydata1,
                               color='tab:blue',
                               linelength=0.05,
                               orientation='vertical')
    yevents2 = EventCollection(ydata2,
                               color='tab:green',
                               linelength=0.05,
                               orientation='vertical')
    yevents3 = EventCollection(ydata3,
                               color='tab:orange',
                               linelength=0.05,
                               orientation='vertical')
    yevents4 = EventCollection(ydata4,
                               color='tab:red',
                               linelength=0.05,
                               orientation='vertical')

    ax.add_collection(xevents1)
    ax.add_collection(xevents2)
    ax.add_collection(xevents3)
    ax.add_collection(xevents4)
    ax.add_collection(yevents1)
    ax.add_collection(yevents2)
    ax.add_collection(yevents3)
    ax.add_collection(yevents4)

    ax.set_xlim([10, 20])
    ax.set_ylim([0, 1])
    ax.legend()
    ax.set_title('Graph of estimated time of generated solution')
    plt.show()