Esempio n. 1
0
def plot_one_slice_of_ninja_star_samples_2(samples, output_image_path_generator, dpi=100):

    import ninja_star_distribution

    pylab.hold(True)

    print "Computing the original pdf values."
    M = 4.0
    mesh_x,mesh_y = np.mgrid[-M:M:.01, -M:M:.01]
    z = ninja_star_distribution.mesh_pdf(mesh_x, mesh_y)

    print "Generating the nice plots."
    model_pdf_values_plot_handle = plt.pcolor(mesh_x, mesh_y, z)
    #plt.winter()
    plt.pink()
    #d = plt.colorbar(model_pdf_value_plot_handle, orientation='horizontal')

    for c in np.arange(samples.shape[0]):
        x = samples[c,:,0]
        y = samples[c,:,1]
        scatter_handle = pylab.scatter(x, y)

        pylab.draw()
        pylab.savefig(output_image_path_generator(c), dpi=dpi)
        print "Wrote " + output_image_path_generator(c)                                             
        del(scatter_handle)

    pylab.close()
Esempio n. 2
0
def plot_one_slice_of_ninja_star_samples(samples_slice, output_image_path, dpi=100, omit_ninja_star_from_plots = False):
    """
        Samples should be of size (M, d).
        You would generally pick either one chain alone
        or one time slice from a set of chains.

        This plotting function doesn't pretend to be
        a very general method. It assumes that the samples
        are 2-dimensional and that [-4.0, 4.0]^2 is the
        best choice of window to plot the samples.
    """

    import ninja_star_distribution

    pylab.hold(True)

    x = samples_slice[:,0]
    y = samples_slice[:,1]

    # TODO : pick better color for the sample dots
    pylab.scatter(x, y)

    # TODO : stamp the KL divergence on the plots

    M = 4.0
    if not omit_ninja_star_from_plots:
        print "Computing the original pdf values."

        mesh_x,mesh_y = np.mgrid[-M:M:.01, -M:M:.01]
        z = ninja_star_distribution.mesh_pdf(mesh_x, mesh_y)

        print "Generating the nice plots."
        model_pdf_values_plot_handle = plt.pcolor(mesh_x, mesh_y, z)
        #plt.winter()
        plt.pink()
        #d = plt.colorbar(model_pdf_value_plot_handle, orientation='horizontal')

    pylab.axes([-M, M, -M, M])
    count_of_points_outside = np.count_nonzero( (x < M) + (M < x) + (y < M) + (M < y) )
    pylab.text(0.0, 0.0,"%d points outside" % count_of_points_outside,fontsize=12, transform = pylab.gca().transAxes)

    pylab.draw()
    #pylab.savefig(output_image_path)
    pylab.savefig(output_image_path, dpi=dpi)
    pylab.close()
Esempio n. 3
0
x1 = np.linspace(-2, 2, xn)
#9*9行列を作成
y = np.zeros((len(x0),len(x1)))
for i0 in range(xn):
    for i1 in range(xn):
        y[i1,i0] = f3(x0[i0],x1[i1])

#行列yを出力
print(y)

#行列yを小数点n桁に四捨五入して表示
print(np.round(y,1))

#数値を色で表現する
plot.figure(figsize=(3.5,3))
#グラデーションパターン。他にも.jet(),.pink(),.bone()などある
plot.pink()
#色による行列の表示
plot.pcolor(y)
#カラーバー表示
plot.colorbar()
#plot.show()


xx0,xx1 = np.meshgrid(x0,x1)
plot.figure(figsize=(5,3.5))
ax = plot.subplot(1,1,1,projection='3d')
ax.plot_surface(xx0,xx1,y,rstride=1,cstride=1,alpha=0.3,color='blue',edgecolor='black')
ax.set_zticks((0,0.2))
ax.view_init(75,-95)
plot.show()
Esempio n. 4
0
def run_platformqc(platform, data_path, output_path, *, suffix=None, n_channel = 512, n_process=15):
    THRESHOLD_INACTIVE = 0.0025

    ld = os.path.join(output_path, "log")
    pd = os.path.join(output_path, "fig")
    if not os.path.isdir(ld):
        os.makedirs(ld, exist_ok=True)

    if not os.path.isdir(pd):
        os.makedirs(pd, exist_ok=True)
    if not suffix:
        suffix = ""
    else:
        suffix = "_" + suffix
    log_path  = os.path.join(ld, "log_ont_platform" +  suffix + ".txt")
    plot_path = os.path.join(pd, "fig_ont_platform" +  suffix + ".png")
    json_path = os.path.join(output_path, "QC_vals_" + platform + suffix + ".json")
    # json
    tobe_json = {}

    ### logging conf ###
    logger = logging.getLogger(__name__)
    logger.setLevel(logging.INFO)
    fh = logging.FileHandler(log_path, 'w')
    sh = logging.StreamHandler()

    formatter = logging.Formatter('%(module)s:%(asctime)s:%(lineno)d:%(levelname)s:%(message)s')
    fh.setFormatter(formatter)
    sh.setFormatter(formatter)

    logger.addHandler(sh)
    logger.addHandler(fh)
    #####################

    logger.info("Started %s platform QC for %s" % (platform, data_path))
    logger.info("The num of channel:%d, the num of process:%d" % (n_channel, n_process))

    l    = list_fast5_files(data_path, logger)
    ltgz = list_fast5_targz(data_path)

    if len(l) == 0 and len(ltgz) == 0:
        logger.warning("No fast5 or compressed file in the given path: %s" % data_path)
        return 1

    if len(l) > 0 and len(ltgz) > 0:
        logger.warning("The given path is a mixture of compressed and uncompressed files. Check: %s" % data_path)
        return 1

    bag  = [set() for _ in range(n_channel)]
    fcs  = set()
    kits = set()
    occ  = []

    p = Pool(processes=n_process)

    if len(l) == 0:
        logger.info("There is no fast5 file in the given dir: %s" % data_path)
        #print(l)
        for f in ltgz:
            logger.info("Process a compressed file: %s" % f)
            base_dir = os.path.dirname( os.path.abspath(f) )
            sub_dir  = os.path.basename(f).replace(".tar.gz", '')
            extract_tar(f, base_path=base_dir) # this method extract all members at the same dir of f
            _l = list_fast5_files( os.path.join(base_dir, sub_dir), logger )
            process_uncompressed_multi(p, _l, bag, fcs, kits, logger)
            shutil.rmtree( os.path.join(base_dir, sub_dir) )
    else:
        process_uncompressed_multi(p, l, bag, fcs, kits, logger)

    p.close()
    logger.info("All of fast5 files were loaded.")

    tobe_json['Sequencing kit'] = str(", ".join(str(s.decode("utf-8")) for s in kits))
    tobe_json['Flowcell'] = str(", ".join(str(s.decode("utf-8")) for s in fcs))

    logger.info("Aggregating pore running time.")
    max = -1
    channel_wise_cnt = [0] * n_channel
    for i in range(len(bag)):
        s = sorted(bag[i], key=itemgetter(0,1))
        bag[i] = s
        if len(s) > 0 and s[-1][1] > max:
            max = s[-1][1]

    for i in range(1, max+1):
        cnt = 0
        for j in range(len(bag)):
            if len(bag[j]) < 1:
                continue

            if bag[j][0][0] <= i and i <= bag[j][0][1]:
                cnt += 1
                channel_wise_cnt[j] += 1

            elif bag[j][0][1] < i:
                bag[j].pop(0)

        occ.append(cnt/len(bag))

    logger.info("Aggregation finished.")

    tobe_json['Sequencing time in seconds'] = int(max)
    tobe_json['The time reached maximum active pore rate'] = int(np.argmax(occ))
    tobe_json['The maximum active pore rate'] = float(np.max(occ))

    for i in range(n_channel):
        channel_wise_cnt[i] /= max

    tobe_json['The fraction of inactive pores'] = float(np.where(np.array(channel_wise_cnt) < THRESHOLD_INACTIVE)[0].shape[0]/n_channel)

    y = np.arange(0, 33)
    x = np.arange(0, 17)
    X,Y = np.meshgrid(x,y)
    Z = np.zeros((33,17), dtype=float)
    c2cor = get_flowcell_coord()

    logger.info("Generating plot 1.")

    for (c, cor)  in enumerate(c2cor):
        if cor is None:
            continue
        Z[cor[0]][cor[1]] = channel_wise_cnt[c-1]

    #plt.figure(figsize=(5,4))
    plt.subplot(3,1,1)
    plt.plot(occ)
    plt.grid(True)

    plt.xlabel('Elapsed time in seconds')
    plt.ylabel('Active channel rate')

    for i in np.arange(1, max+1, 28800): # 8 hours
        if i == 1:
            continue
        plt.axvline(x=i, linestyle='dashed', linewidth=1, color='blue', alpha=0.8)

    logger.info("Generating plot 2.")
    plt.subplot(3,1,2)
    plt.pcolor(X, Y, Z, cmap='RdBu')
    plt.colorbar()
    plt.tight_layout()
    plt.title("Pore activity mapped on the actual layout")
    plt.contour(X, Y, Z, levels=[THRESHOLD_INACTIVE], linewidths=2, linestyles='dashed')
    plt.pink()

    logger.info("Generating plot 3.")
    plt.subplot(3,1,3)
    plt.hist(channel_wise_cnt, color='blue', bins=100)
    plt.xlabel('Channel wise activity rate')
    plt.ylabel('Frequency')

    plt.subplots_adjust(hspace=1.0)

    plt.savefig(plot_path, bbox_inches="tight")
    #plt.savefig(plot_path)
    #plt.show()
    logger.info("Plots were saved to %s." % plot_path)

    with open(json_path, "w") as f:
        logger.info("Quality measurements were written into a JSON file: %s" % json_path)
        json.dump(tobe_json, f, indent=4)