Exemplo n.º 1
0
def preprocess(index):
    # load data
    t, stim, rec = load_frame_group(path, index=index, stacked=False)
    # apply filter
    rec_filt = []
    for rec_ in rec:
        rec_ = butter_lpf(rec_, lo_cutoff, fs)
        rec_filt.append(rec_)
    # mean
    rec = np.stack(rec, axis=0).mean(axis=0)
    rec = subtract_baseline(t, rec)
    # filter
    rec_filt = np.stack(rec_filt, axis=0).mean(axis=0)
    rec_filt = subtract_baseline(t, rec_filt)

    # split data by stimuli
    ts1 = np.argmax(stim)
    # mask
    stim[ts1:ts1 + 100] = 0
    ts2 = np.argmax(stim)

    ts1, ts2 = t[ts1], t[ts2]
    logger.debug("stimuli timestamp: {}, {}".format(ts1, ts2))

    # split stimuli
    t_, rec1 = t_crop(t, rec, (ts1, ts2))
    _, rec_filt1 = t_crop(t, rec_filt, (ts1, ts2))

    _, rec2 = t_crop(t, rec, (ts2, 2 * ts2 - ts1))
    _, rec_filt2 = t_crop(t, rec_filt, (ts2, 2 * ts2 - ts1))

    # offset t
    t_ -= t_[0]
    return t_, [rec1, rec2], [rec_filt1, rec_filt2]
Exemplo n.º 2
0
def preprocess(index, crop=(0.1, 0.15)):
    """
    Preprocess data directly from file.

    Args:
        index (ndarray): Frame range to extract, must be contiguous.
        crop (tuple): Timestamp range for coarse croping.
    
    Returns:
        (tuple): tuple containing:
            t (ndarray): Cropped timestamp.
            rec (ndarray): Raw recordings.
            rec_filt (ndarray): Filtered recordings.
    """
    # load data
    t, stim, rec = load_frame_group(path, index=index, stacked=False)

    t_ = None
    rec_tmp, rec_filt = [], []
    for rec_ in rec:
        rec_lpf = butter_lpf(rec_, lo_cutoff, fs)
        rec_lpf = subtract_baseline(t, rec_lpf)
        t_, rec_lpf = t_crop(t, rec_lpf, crop)
        rec_filt.append(rec_lpf)

        rec_ori = subtract_baseline(t, rec_)
        _, rec_ori = t_crop(t, rec_ori, crop)
        rec_tmp.append(rec_ori)
    rec = rec_tmp

    # offset t
    t_ -= t_[0]

    return t_, rec, rec_filt
Exemplo n.º 3
0
def preprocess(index):
    # load data
    t, stim, rec = load_frame_group(path, index=index, stacked=False)

    # determine stimuli split point
    ts1 = np.argmax(stim)
    # mask first stimuli
    stim[ts1:ts1 + 100] = 0
    ts2 = np.argmax(stim)
    ts1, ts2 = t[ts1], t[ts2]
    logger.debug("stimuli timestamp: {}, {}".format(ts1, ts2))

    logger.info("applying LPF and background subtraction")
    # apply filter and subtract baseline
    rec_tmp, rec_filt = [], []
    for rec_ in rec:
        rec_lpf = butter_lpf(rec_, lo_cutoff, fs)
        rec_lpf = subtract_baseline(t, rec_lpf)
        rec_filt.append(rec_lpf)

        rec_ori = subtract_baseline(t, rec_)
        rec_tmp.append(rec_ori)
    rec = rec_tmp

    logger.info("cropping")
    # split stimuli
    t_ = None
    rec1, rec2 = [], []
    rec_filt1, rec_filt2 = [], []
    for rec_, rec_filt_ in zip(rec, rec_filt):
        t_, rec1_ = t_crop(t, rec_, (ts1, ts2))
        rec1.append(rec1_)
        _, rec_filt1_ = t_crop(t, rec_filt_, (ts1, ts2))
        rec_filt1.append(rec_filt1_)

        _, rec2_ = t_crop(t, rec_, (ts2, 2 * ts2 - ts1))
        rec2.append(rec2_)
        _, rec_filt2_ = t_crop(t, rec_filt_, (ts2, 2 * ts2 - ts1))
        rec_filt2.append(rec_filt2_)

    # offset t
    t_ -= t_[0]
    return (
        t_,
        [np.stack(rec1, axis=0),
         np.stack(rec2, axis=0)],
        [np.stack(rec_filt1, axis=0),
         np.stack(rec_filt2, axis=0)],
    )
Exemplo n.º 4
0
def main(index, name="filter_demo_sink"):
    plt.cla()

    # load data
    t, stim, rec = load_frame_group(path, index=index, stacked=False)
    # apply filter
    rec_filt = []
    for rec_ in rec:
        rec_ = butter_lpf(rec_, lo_cutoff, fs)
        rec_filt.append(rec_)
    # mean
    rec = np.stack(rec, axis=0).mean(axis=0)
    rec = subtract_baseline(t, rec)

    crop = (0.1, 0.15)

    t_, rec = t_crop(t, rec, crop)
    ax.plot(t_, rec, "r", label="raw", linewidth=1)

    rec_filt = np.stack(rec_filt, axis=0).mean(axis=0)
    rec_filt = subtract_baseline(t, rec_filt)

    t_, rec_filt = t_crop(t, rec_filt, crop)
    ax.plot(t_, rec_filt, "b", label="filtered", linewidth=1)

    ax.axhline(0, color="k", linestyle=":", linewidth=0.5)

    # labels
    ax.legend()
    plt.xlabel("Time (s)")
    plt.ylabel("Intensity (mV)")

    # final adjust
    ax.set_xlim(crop)

    plt.savefig("{}.png".format(name), dpi=300)

    plt.waitforbuttonpress()
Exemplo n.º 5
0
def preprocess(index, crop):
    # load data
    t, stim, rec = load_frame_group(path, index=index, stacked=False)
    # apply filter
    rec_filt = []
    for rec_ in rec:
        rec_ = butter_lpf(rec_, lo_cutoff, fs)
        rec_filt.append(rec_)
    # mean
    rec = np.stack(rec, axis=0).mean(axis=0)
    rec = subtract_baseline(t, rec)
    # filter
    rec_filt = np.stack(rec_filt, axis=0).mean(axis=0)
    rec_filt = subtract_baseline(t, rec_filt)

    # crop
    t_, rec = t_crop(t, rec, crop)
    _, rec_filt = t_crop(t, rec_filt, crop)

    # offset t
    t_ -= t_[0]

    return t_, rec, rec_filt
Exemplo n.º 6
0
def main(index, name="filter_demo_sink"):
    plt.cla()

    # load data
    t, stim, rec = load_frame_group(path, index=index, stacked=False)
    # apply filter
    rec_filt = []
    for rec_ in rec:
        rec_ = butter_lpf(rec_, lo_cutoff, fs)
        rec_filt.append(rec_)
    # mean
    rec = np.stack(rec, axis=0).mean(axis=0)
    rec = subtract_baseline(t, rec)

    # visualize raw data
    crop = (0.1, 0.15)

    t_, rec = t_crop(t, rec, crop)
    ax.plot(t_, rec, "r", label="raw", linewidth=1)

    ax.axhline(0, color="k", linestyle=":", linewidth=0.5)

    # using filtered signal to extract slope
    rec_filt = np.stack(rec_filt, axis=0).mean(axis=0)
    rec_filt = subtract_baseline(t, rec_filt)

    t_, rec_filt = t_crop(t, rec_filt, crop)

    ipk, _ = find_epsp_peak(t_, rec_filt)

    ax.scatter(
        t_[ipk],
        rec_filt[ipk],
        marker="o",
        edgecolors="b",
        facecolor="none",
        label="EPSP peak",
    )

    # slope
    slope, r, dt, dy = epsp_slope(t_, rec, ipk, yf=rec_filt, return_pos=True)

    ax.plot(dt, dy, "b:", label="EPSP slope", linewidth=1)

    # labels
    ax.legend()
    plt.xlabel("Time (s)")
    plt.ylabel("Intensity (mV)")

    # create inset
    axin = inset_axes(ax, width="30%", height="50%", loc=4, borderpad=3)

    t_in, rec_in = t_crop(t_, rec, (0.1025, 0.1100))
    axin.plot(t_in, rec_in, "r", label="raw", linewidth=1)

    axin.scatter(
        t_[ipk],
        rec_filt[ipk],
        marker="o",
        edgecolors="b",
        facecolor="none",
        label="EPSP peak",
    )

    axin.plot(dt, dy, "b:", linewidth=2)

    axin.set_xlim((0.1025, 0.1100))

    # final adjust
    ax.set_xlim(crop)

    plt.savefig("{}_slope-{:.3f}_r-{:.3f}.png".format(name, slope, r), dpi=300)

    plt.waitforbuttonpress()