def set_track_spectrum(axis: plt.axis, data: np.ndarray,
                       current_index: int) -> plt.axis:
    axis.plot(data)  # Show intensity spectrum
    axis.axvline(x=current_index, color='r')
    # axis.set_xlim([max(0, current_index - _margin), min(len(data) - 1, current_index + _margin)])
    # axis.set_ylim([max(0.001, np.min(data)), np.max(data)])
    return axis
示例#2
0
def plot_aligned(
    x: np.ndarray,
    indices: Union[np.ndarray, list],
    ax: plt.axis,
    mode: str,
    window: int = 120,
    mean_kwargs: dict = None,
    **kwargs,
):
    """
        Given a 1d array and a series of indices it plots the values of 
        the array aligned to the timestamps.
    """
    pre, aft = int(window / 2), int(window / 2)
    mean_kwargs = mean_kwargs or dict(lw=4, zorder=100, color=pink_dark)

    # get plotting params
    if mode == "pre":
        pre_c, pre_lw = kwargs.pop("color", "salmon"), kwargs.pop("lw", 2)
        aft_c, aft_lw = blue_grey, 1
        ax.axvspan(0, pre, fc=blue, alpha=0.25, zorder=-20)
    else:
        aft_c, aft_lw = kwargs.pop("color", "salmon"), kwargs.pop("lw", 2)
        pre_c, pre_lw = blue_grey, 1
        ax.axvspan(aft, window, fc=blue, alpha=0.25, zorder=-20)

    # plot each trace
    X = []  # collect data to plot mean
    for idx in indices:
        x_pre = x[idx - pre:idx]
        x_aft = x[idx - 1:idx + aft]

        if len(x_pre) != pre or len(x_aft) != aft + 1:
            logger.warning(f"Could not plot data aligned to index: {idx}")
            continue
        X.append(x[idx - pre:idx + aft])

        ax.plot(x_pre, color=pre_c, lw=pre_lw, **kwargs)
        ax.plot(np.arange(aft + 1) + aft,
                x_aft,
                color=aft_c,
                lw=aft_lw,
                **kwargs)

    # plot mean and line
    X = np.vstack(X)
    plot_mean_and_error(np.mean(X, axis=0), np.std(X, axis=0), ax,
                        **mean_kwargs)
    ax.axvline(pre, lw=2, color=blue_grey_dark, zorder=-1)

    ax.set(**get_window_ticks(window, shifted=False))
示例#3
0
def plot_raster(
    spikes: np.ndarray,
    events: Union[np.ndarray, list],
    ax: plt.axis,
    window: int = 120,
    s=5,
    color=grey_darker,
    kde: bool = True,
    bw: int = 6,
    **kwargs,
):
    """
        Plots a raster plot of spikes aligned to timestamps events

        It assumes that all event and spike times are in frames and framerate is 60
    """
    half_window = window / 2
    yticks_step = int(np.ceil(len(events) / 8)) if len(events) > 8 else 2
    X = []
    for n, event in enumerate(events):
        event_spikes = (spikes[(spikes >= event - half_window)
                               & (spikes <= event + half_window)] - event)
        X.extend(list(event_spikes))
        y = np.ones_like(event_spikes) * n
        ax.scatter(event_spikes, y, s=5, color=color, **kwargs)
    ax.axvline(0, ls=":", color="k", lw=0.75)

    # plot KDE
    if kde:
        raise NotImplementedError("KDE env setup incorrect")
        # plot_kde(
        #     ax=ax,
        #     z=-len(events) / 4,
        #     data=X,
        #     normto=len(events) / 5,
        #     color=blue_grey_dark,
        #     kde_kwargs=dict(bw=bw, cut=0),
        #     alpha=0.6,
        #     invert=False,
        # )

    # set x axis properties
    ax.set(
        yticks=np.arange(0, len(events), yticks_step),
        ylabel="event number",
        **get_window_ticks(window),
    )
def set_inset_spectrum(axis: plt.axis, data: np.ndarray, current_index: int,
                       peak_collection: PeakCollection) -> plt.axis:
    axis.plot(data)  # Show intensity spectrum
    axis = plot_peaks(axis=axis, collection=peak_collection)  # Show peaks
    y_bot, y_top = axis.get_ylim()
    text_height = y_bot + 0.6 * (y_top - y_bot)  # Data coordinates
    _margin = 50
    x_lim = [
        max(0, current_index - _margin),
        min(len(data) - 1, current_index + _margin)
    ]
    # Plot clusters
    # for cluster in peak_collection.get_clusters:
    #     bound_left, bound_right = cluster.get_value_slice
    #     if bound_right > x_lim[0] or bound_left < x_lim[1]:
    #         axis.axvspan(bound_left, bound_right, alpha=0.5, color='green')
    #     if x_lim[0] < cluster.get_avg_x < x_lim[1]:
    #         axis.text(x=cluster.get_avg_x, y=text_height, s=r'$\tilde{m}$'+f'={cluster.get_transverse_mode_id}')
    axis.axvline(x=current_index, color='r')
    axis.set_xlim(x_lim)
    return axis
示例#5
0
def add_cut_to_axis(ax: plt.axis,
                    cut_left: Optional[float] = None,
                    cut_right: Optional[float] = None,
                    cut_window: Optional[Tuple[float, float]] = None,
                    keep_window: Optional[Tuple[float, float]] = None,
                    color: str = 'white'):
    """
    Adds a "cut" to a given axis. The cut is shown as shaded area with the color
    given in the parameter color.

    :param ax: Axis to plot on.
    :param cut_left: Upper x value of the cut. If set, the area with
    x < cut_left is indicated to be cut away. Default is None
    :param cut_right: Lower x value of the cut. If set, the area with
    x > cut_right is indicated to be cut away. Default is None
    :param cut_window:
    :param keep_window:
    :param color: Color of the overlay of the area which is indicated to be
    cut away. Default is 'white'.
    """
    x_lim_low, x_lim_high = ax.get_xlim()

    if cut_left is not None:
        ax.axvspan(x_lim_low, cut_left, facecolor=color, alpha=0.7)
        ax.axvline(cut_left,
                   color='black',
                   linestyle='dashed',
                   alpha=0.7,
                   lw=1.5)
    elif cut_right is not None:
        ax.axvspan(cut_right, x_lim_high, facecolor=color, alpha=0.7)
        ax.axvline(cut_right,
                   color='black',
                   linestyle='dashed',
                   alpha=0.7,
                   lw=1.5)
    elif cut_window is not None:
        ax.axvspan(cut_window[0], cut_window[1], facecolor=color, alpha=0.7)
        ax.axvline(cut_window[0],
                   color='black',
                   linestyle='dashed',
                   alpha=0.7,
                   lw=1.5)
        ax.axvline(cut_window[1],
                   color='black',
                   linestyle='dashed',
                   alpha=0.7,
                   lw=1.5)
    elif keep_window is not None:
        ax.axvspan(x_lim_low, keep_window[0], facecolor=color, alpha=0.7)
        ax.axvline(keep_window[0],
                   color='black',
                   linestyle='dashed',
                   alpha=0.7,
                   lw=1.5)
        ax.axvspan(keep_window[1], x_lim_high, facecolor=color, alpha=0.7)
        ax.axvline(keep_window[1],
                   color='black',
                   linestyle='dashed',
                   alpha=0.7,
                   lw=1.5)