Exemple #1
0
def main():
    args = parse_args()
    header, counts = read_counts(args.counts)
    counts = pd.DataFrame(counts, columns=header)
    counts = sort_counts(counts)

    num_genes = int(args.num_genes)-1
    out_dir = args.out_dir

    # plot pca
    print('Generating PCA plot ...')
    plot.plot_pca(out_dir, counts)

    # plot heatmap
    print('Generating Gene Expression Heatmap ...')
    plot.plot_heatmap(out_dir, counts, num_genes)

    # Plot gene expression tragectory
    print('Generating Gene Expression Trajectory ...')
    plot.plot_trajectory(out_dir, counts, num_genes)

    # Save formatted and sorted count file
    counts.to_csv('data/counts_clust.txt', index=False, sep='\t')

    # Call clust to cluster the genes
    print('Clustering Genes (this might take awhile) ...')
    subprocess.call(['mkdir', 'clust_out'])
    if args.kmeans is not None:
        subprocess.call(['clust', 'data/counts_clust.txt',
                         '-o', './clust_out', '-K', args.kmeans])
    else:
        subprocess.call(['clust', 'data/counts_clust.txt',
                        '-o', './clust_out'])

    # Make sure everythin is all set for motif enrichment
    clust_out = args.input_genes
    ref_bed = args.bedfile
    ref_fa = args.genome
    ref_motif = args.motif

    try:
        open(clust_out)
    except FileNotFoundError:
        print('Clustering failed, '
              + 'Clusters_Objects.tsv not found in the output directory')

    print('Motif Enrichment Analysis (this might take awhile) ...')
    try:
        enrichment.run_motif_enrichment(clust_out,
                                        ref_bed,
                                        ref_fa, ref_motif,
                                        args.ame,
                                        100, 100)
    except NameError:
        print('AME is not installed! Please install AME and try again')
def print_positions():
    """
	Prints positions of the devices encountered so far
	"""
    pos = []
    for device_mac in const.positions:
        print(device_mac, *const.positions[device_mac][0],
              const.positions[device_mac][1])
        pos.append([
            dist(const.positions[device_mac][0], const.ANCHORS[a])
            for a in const.ANCHORS
        ])
    plot_heatmap(pos)
Exemple #3
0
def plot_specific_feature(df, y, save):
    score_mat = mat_specificity_score(df, y)
    peak_index, peak_labels = cluster_specific(score_mat,
                                               np.unique(y),
                                               top=200)
    plot_heatmap(df.iloc[peak_index],
                 y=y,
                 row_labels=peak_labels,
                 ncol=3,
                 cmap='Reds',
                 vmax=1,
                 row_cluster=False,
                 legend_font=6,
                 cax_title='Peak Value',
                 figsize=(8, 10),
                 bbox_to_anchor=(0.4, 1.2),
                 position=(0.8, 0.76, 0.1, 0.015),
                 save=save)
Exemple #4
0
def run(train_data, test_data):
    batch_size = 10
    n_samples = np.array(train_data).shape[0]
    n_batches = int(np.ceil(float(n_samples) / batch_size))
    batch_slices = list(
        gen_even_slices(n_batches * batch_size, n_batches, n_samples))

    nodes = [50, 75, 100, 150]

    for item in nodes:
        errors = []
        model = BernoulliRBM(n_components=item,
                             learning_rate=0.1,
                             batch_size=10,
                             n_iter=1,
                             random_state=None,
                             verbose=1)
        for _ in range(20):
            for batch_slice in batch_slices:
                model.partial_fit(train_data[batch_slice])
            errors.append(percent_error(model.gibbs(test_data), test_data))
        plot.plot_points(errors)
        plot.plot_heatmap(reformat_data(test_data[0]))
        plot.plot_heatmap(reformat_data(model.gibbs(test_data)[0]))

        if item == 50 or item == 100:
            plot.plot_heatmap(model.__dict__['components_'].reshape(item, 784))
def predict(args):
    tokenizer = nltk.tokenize.word_tokenize
    tagger = nltk.tag.perceptron.PerceptronTagger()

    print(f'Loading model from `{args.model}`...')
    feature_opts = get_feature_opts(args.features)
    model = DependencyParser(feature_opts, args.decoder)
    model.load(args.model)
    print(f'Model UAS: {model.arc_accuracy["dev"]:.2f}')

    def predict_input(line):
        tokens = tokenizer(line)
        tagged = tagger.tag(tokens)
        print('> ' + ' '.join([f'{token}/{tag}' for token, tag in tagged]))
        tokens, tags = zip(*tagged)
        if args.no_tags:
            tags = len(tags) * ['_']
        tokens = make_conll_tokens(tokens, tags)
        heads, probs, _ = model.parse(tokens)
        return tokens, heads, probs

    if args.jabber:
        for i, line in enumerate(JABBERWOCKY, 1):
            tokens, heads, probs = predict_input(line)
            name = f'jab-{i}'
            if args.no_tags:
                name += '-notags'
            plot_heatmap([token.form for token in tokens],
                         probs,
                         dir=args.plot_dir,
                         name=name,
                         ext=args.ext)
            print_prediction(tokens, heads)
    if args.examples:
        for i, line in enumerate(EXAMPLES, 1):
            tokens, heads, probs = predict_input(line)
            name = f'ex-{i}'
            if args.no_tags:
                name += '-notags'
            plot_heatmap([token.form for token in tokens],
                         probs,
                         dir=args.plot_dir,
                         name=name,
                         ext=args.ext)
            print_prediction(tokens, heads)
    else:
        step = 0
        while True:
            step += 1
            line = input('Input: ')
            tokens, heads, probs = predict_input(line)
            name = f'{args.plot_name}-{step}'
            if args.no_tags:
                name += '-notags'
            plot_heatmap([token.form for token in tokens],
                         probs,
                         name=name,
                         ext=args.ext)
            print_prediction(tokens, heads)
Exemple #6
0
def plot_feature(output_dir):
    y = pd.read_csv(output_dir + '/cluster_assignments.txt',
                    sep='\t',
                    index_col=0,
                    header=None)[1]
    feature = pd.read_csv(output_dir + '/feature.txt',
                          sep='\t',
                          index_col=0,
                          header=None)
    plot_heatmap(feature.T,
                 y,
                 figsize=(8, 3),
                 cmap='RdBu_r',
                 vmax=8,
                 vmin=-8,
                 center=0,
                 ylabel='Feature dimension',
                 yticklabels=np.arange(10) + 1,
                 cax_title='Feature value',
                 legend_font=6,
                 ncol=1,
                 bbox_to_anchor=(1.1, 1.1),
                 position=(0.92, 0.15, .08, .04),
                 save=output_dir + '/feature.png')
Exemple #7
0
def sequential():
	data_array = pict_data()
	X0 = data_array[:3]
	W = hopfield.weights(X0)
	X = [data_array[9], data_array[10]]
	# for x in X0:
	# 	plot.plot_heatmap(reformat_data(x))
	# 	plot.plot_heatmap(reformat_data(hopfield.recall_until_stable(W, x)))
	for x in X:
		plot.plot_heatmap(reformat_data(x))
		plot.plot_heatmap(reformat_data(hopfield.recall_until_stable(W, x)))
		res_array = hopfield.recall_sequentially(W, x)
		for r in res_array:
			plot.plot_heatmap(reformat_data(np.array(r)))
Exemple #8
0
                mrna_densities[gene],
                prot_densities[gene],
                constants.dataset_config['TIMEPOINTS_NUM_MRNA'],
                constants.dataset_config['TIMEPOINTS_NUM_PROTEIN'],
                peripheral_flag,
                stripes=stripes,
                quadrants=8)
            css.append(cs)
            results[gene] = [cs, p]
            tgt_image_name = constants.analysis_config[
                'FIGURE_NAME_FORMAT_CS'].format(gene=gene)
            tgt_fp = pathlib.Path(
                constants.analysis_config['FIGURE_OUTPUT_PATH'].format(
                    root_dir=global_root_dir), tgt_image_name)
            if "original" in conf[0]:
                plot_heatmap(ranking, gene, tgt_fp)
                logger.info("Generated image at {}",
                            str(tgt_fp).split("analysis/")[1])
            else:
                plot_heatmap(ranking,
                             gene,
                             tgt_fp,
                             size=2,
                             xtickslabel=['3h', '5h'],
                             ytickslabel=['3h', '5h'])
                logger.info("Generated image at {}",
                            str(tgt_fp).split("analysis/")[1])

        tgt_image_name = constants.analysis_config[
            'FIGURE_NAME_FORMAT_CS_HISTOGRAM']
        tgt_fp = pathlib.Path(
Exemple #9
0
print("Forward pass - test features")
Z_test = model(X_test)
utils.save_loss(model.loss_dict, model_dir, "test")

# save features
utils.save_features(model_dir, "X_train", X_train, y_train)
utils.save_features(model_dir, "X_test", X_test, y_test)
utils.save_features(model_dir, "Z_train", Z_train, y_train)
utils.save_features(model_dir, "Z_test", Z_test, y_test)

# evaluation train
_, acc_svm = evaluate.svm(Z_train, y_train, Z_train, y_train)
acc_knn = evaluate.knn(Z_train, y_train, Z_train, y_train, k=5)
acc_svd = evaluate.nearsub(Z_train, y_train, Z_train, y_train, n_comp=1)
acc = {"svm": acc_svm, "knn": acc_knn, "nearsub-svd": acc_svd}
utils.save_params(model_dir, acc, name="acc_train.json")

# evaluation test
_, acc_svm = evaluate.svm(Z_train, y_train, Z_test, y_test)
acc_knn = evaluate.knn(Z_train, y_train, Z_test, y_test, k=5)
acc_svd = evaluate.nearsub(Z_train, y_train, Z_test, y_test, n_comp=1)
acc = {"svm": acc_svm, "knn": acc_knn, "nearsub-svd": acc_svd}
utils.save_params(model_dir, acc, name="acc_test.json")

# plot
plot.plot_combined_loss(model_dir)
plot.plot_heatmap(X_train, y_train, "X_train", model_dir)
plot.plot_heatmap(X_test, y_test, "X_test", model_dir)
plot.plot_heatmap(Z_train, y_train, "Z_train", model_dir)
plot.plot_heatmap(Z_test, y_test, "Z_test", model_dir)
Exemple #10
0
for item in nodes:
    encoding_dim = item

    # this is our input placeholder
    input_img = Input(shape=(784, ))
    encoded = Dense(encoding_dim, activation='relu')(input_img)
    decoded = Dense(784, activation='sigmoid')(encoded)
    autoencoder = Model(input_img, decoded)

    autoencoder.compile(optimizer='SGD',
                        loss='binary_crossentropy',
                        metrics=['binary_accuracy'])

    history = History()

    autoencoder.fit(train_data,
                    train_data,
                    epochs=20,
                    batch_size=10,
                    shuffle=True,
                    verbose=0,
                    callbacks=[history],
                    validation_data=(test_data, test_data))

    errors = [1 - x for x in history.history['val_binary_accuracy']]

    plot.plot_points(errors)
    plot.plot_heatmap(reformat_data(test_data[0]))
    plot.plot_heatmap(reformat_data(autoencoder.predict(test_data)[0]))
    if item == 50 or item == 100:
        print(plot.plot_heatmap(autoencoder.layers[1].get_weights()[0].T))
Exemple #11
0
                                loss=args.loss,
                                device=device)
    X_train, y_train, Z_train = F.to_cpu(X_train, y_train, Z_train)
    utils.save_loss(eval_dir, f'train', net.get_loss())

    print('test')
    Z_test = net.batch_forward(X_test,
                               batch_size=args.batch_size,
                               loss=args.loss,
                               device=device)
    X_test, y_test, Z_test = F.to_cpu(X_test, y_test, Z_test)
    utils.save_loss(eval_dir, f'test', net.get_loss())

## Normalize
X_train = F.normalize(X_train.flatten(1))
X_test = F.normalize(X_test.flatten(1))
Z_train = F.normalize(Z_train.flatten(1))
Z_test = F.normalize(Z_test.flatten(1))

# Evaluate
evaluate.evaluate(eval_dir, 'knn', Z_train, y_train, Z_test, y_test)
#evaluate.evaluate(eval_dir, 'nearsub', Z_train, y_train, Z_test, y_test, num_classes=num_classes, n_comp=10)

# Plot
plot.plot_loss_mcr(eval_dir, 'train')
plot.plot_loss_mcr(eval_dir, 'test')
plot.plot_heatmap(eval_dir, 'X_train', X_train, y_train, num_classes)
plot.plot_heatmap(eval_dir, 'X_test', X_test, y_test, num_classes)
plot.plot_heatmap(eval_dir, 'Z_train', Z_train, y_train, num_classes)
plot.plot_heatmap(eval_dir, 'Z_test', Z_test, y_test, num_classes)
Exemple #12
0
def correlation_matrix():
    bytes_obj = plot_heatmap()

    return send_file(bytes_obj,
                     attachment_filename='plot.png',
                     mimetype='image/png')