Пример #1
0
def visualize(assembled,
              labels,
              namespace,
              data_names,
              gene_names=None,
              gene_expr=None,
              genes=None,
              n_iter=N_ITER,
              perplexity=PERPLEXITY,
              verbose=VERBOSE,
              learn_rate=200.,
              early_exag=12.,
              embedding=None,
              size=1):
    # Fit t-SNE.
    if embedding is None:
        tsne = TSNEApprox(n_iter=n_iter,
                          perplexity=perplexity,
                          verbose=verbose,
                          random_state=69,
                          learning_rate=learn_rate,
                          early_exaggeration=early_exag)
        tsne.fit(np.concatenate(assembled))
        embedding = tsne.embedding_

    rand_idx = range(embedding.shape[0])
    random.shuffle(rand_idx)
    embedding = embedding[rand_idx, :]
    labels = labels[rand_idx]

    # Plot clusters together.
    plot_clusters(embedding, labels, s=size)
    plt.title(('Panorama ({} iter, perplexity: {}, sigma: {}, ' +
               'knn: {}, hvg: {}, dimred: {}, approx: {})').format(
                   n_iter, perplexity, SIGMA, KNN, HVG, DIMRED, APPROX))
    plt.savefig(namespace + '.svg', dpi=500)

    # Plot clusters individually.
    for i in range(len(data_names)):
        visualize_cluster(embedding,
                          i,
                          labels,
                          cluster_name=data_names[i],
                          size=size,
                          viz_prefix=namespace)

    # Plot gene expression levels.
    if (not gene_names is None) and \
       (not gene_expr is None) and \
       (not genes is None):
        gene_expr = gene_expr[rand_idx, :]
        for gene_name in gene_names:
            visualize_expr(gene_expr,
                           embedding,
                           genes,
                           gene_name,
                           size=size,
                           viz_prefix=namespace)

    return embedding
Пример #2
0
def get_eval(lr=0.01, n_episodes=50, is_train=False, savefig=False):
    # mkdir
    print('qlearning_nn evaluating...')
    base_dir = './results/qlearning_nn'
    if not os.path.exists(base_dir):
        os.makedirs(base_dir)

    log_file = os.path.join(base_dir, 'qlearning_nn.log')
    logger = logging(log_file)
    results_file = os.path.join(base_dir, 'qlearning_nn.csv')
    if os.path.exists(results_file) and not is_train and not savefig:
        results = pd.read_csv(results_file)
        results = results.sort_values(by=['noisy', 'problem_id'])
        return results
    else:
        if os.path.exists(results_file):
            os.remove(results_file)
        if os.path.exists(log_file):
            os.remove(log_file)
        pkl_file = os.path.join(
            base_dir,
            'qlearning_nn_lr={}_episodes={}.pkl'.format(lr, n_episodes))
        if os.path.exists(pkl_file):
            q_learning_nn = pickle.load(open(pkl_file, 'rb'))
        else:
            q_learning_nn = train(lr=lr, n_episodes=n_episodes)
    # eval
    results = pd.DataFrame([],
                           columns=[
                               'problem_id', 'noisy', 'action',
                               'Total_rewards', 'avg_reward_per_action'
                           ])
    for problem_id, noisy, env in get_env():
        states, rewards, actions = implement(env,
                                             q_learning_nn,
                                             1,
                                             discount_factor=0.95)
        result = {
            'problem_id': problem_id,
            'noisy': noisy,
            'Total_rewards': sum(rewards),
            'avg_reward_per_action': sum(rewards) / len(actions)
        }
        results = results.append(pd.DataFrame(result, index=[0]),
                                 ignore_index=0)
        logger(' ' + str(result))
        logger(actions)
        if savefig:
            get_fig(states, rewards)
            pic_name = os.path.join(
                base_dir,
                'problem_id={} noisy={}.jpg'.format(problem_id, noisy))
            plt.savefig(dpi=300, fname=pic_name)
            plt.close()
        env.close()
    results = results.sort_values(by=['noisy', 'problem_id'])
    results.to_csv(results_file, index=0)
    return results
def plot_batch(df, batch):

    # Plot 50uM.

    df_50uM = df[df.conc == -3]

    if batch.startswith('Ala'):
        df_dmso = df_50uM[df_50uM.comp == 'DMSO']
        for comp in [ 'K252a', 'SU11652', 'TG101209', 'RIF', 'IKK16' ]:
            df_comp = df_50uM[df_50uM.comp == comp]
            t, p_2side = ss.ttest_ind(df_comp.fluo, df_dmso.fluo)
            p_1side = p_2side / 2. if t < 0 else 1. - (p_2side / 2.)
            print('{}, one-sided t-test P = {}, n = {}'
                  .format(comp, p_1side, len(df_comp)))

    if batch == 'AlaA':
        order = [ 'K252a', 'SU11652', 'TG101209', 'RIF', 'DMSO' ]
    elif batch == 'AlaB':
        order = [ 'IKK16', 'K252a', 'RIF', 'DMSO' ]
    else:
        return

    plt.figure()
    sns.barplot(x='comp', y='fluo', data=df_50uM, ci=95, dodge=False,
                hue='control', palette=sns.color_palette("RdBu_r", 7),
                order=order, capsize=0.2, errcolor='#888888',)
    sns.swarmplot(x='comp', y='fluo', data=df_50uM, color='black',
                  order=order)
    #plt.ylim([ 10, 300000 ])
    if not batch.startswith('Ala'):
        plt.yscale('log')
    plt.savefig('figures/tb_culture_50uM_{}.svg'.format(batch))
    plt.close()

    # Plot dose-response.

    comps = sorted(set(df.comp))
    concentrations = sorted(set(df.conc))

    plt.figure(figsize=(24, 6))
    for cidx, comp in enumerate(order):
        df_subset = df[df.comp == comp]

        plt.subplot(1, 5, cidx + 1)
        sns.lineplot(x='conc', y='fluo', data=df_subset, ci=95,)
        sns.scatterplot(x='conc', y='fluo', data=df_subset,
                        color='black',)
        plt.title(comp)
        if batch.startswith('Ala'):
            plt.ylim([ 0., 1.3 ])
        else:
            plt.ylim([ 10, 1000000 ])
            plt.yscale('log')
        plt.xticks(list(range(-3, -6, -1)),
                   [ '50', '25', '10', ])#'1', '0.1' ])

    plt.savefig('figures/tb_culture_{}.svg'.format(batch))
    plt.close()
def visualize_heatmap(chem_prot, suffix=''):
    plt.figure()
    cmap = sns.diverging_palette(220, 10, as_cmap=True)
    sns.heatmap(chem_prot, cmap=cmap)
    mkdir_p('figures/')
    if suffix == '':
        plt.savefig('figures/heatmap.png', dpi=300)
    else:
        plt.savefig('figures/heatmap_{}.png'.format(suffix), dpi=300)
    plt.close()
def acquisition_scatter(y_unk_pred, var_unk_pred, acquisition, regress_type):
    y_unk_pred = y_unk_pred[:]
    y_unk_pred[y_unk_pred > 10000] = 10000

    plt.figure()
    plt.scatter(y_unk_pred, var_unk_pred, alpha=0.5, c=-acquisition,
                cmap='hot')
    plt.title(regress_type.title())
    plt.xlabel('Predicted score')
    plt.ylabel('Variance')
    plt.savefig('figures/acquisition_unknown_{}.png'
                .format(regress_type), dpi=200)
    plt.close()
Пример #6
0
def plot_values(df, score_fn):
    models = ['mlper1', 'sparsehybrid', 'gp', 'real']

    plt.figure(figsize=(10, 4))

    for midx, model in enumerate(models):
        if model == 'gp':
            color = '#3e5c71'
        elif model == 'sparsehybrid':
            color = '#2d574e'
        elif model == 'mlper1':
            color = '#a12424'
        elif model == 'real':
            color = '#A9A9A9'
        else:
            raise ValueError('Invalid model'.format(model))

        plt.subplot(1, len(models), midx + 1)
        df_subset = df[df.model == model]
        compounds = np.array(df_subset.compound_)
        if model == 'real':
            order = sorted(compounds)
        else:
            order = compounds[np.argsort(-df_subset.affinity)]
        sns.barplot(data=df_subset,
                    x='compound_',
                    y='affinity',
                    color=color,
                    order=order)
        if score_fn == 'rdock':
            plt.ylim([0, -40])
        else:
            plt.ylim([0, -12])
        plt.xticks(rotation=45)

    plt.savefig('figures/design_docking_{}.svg'.format(score_fn))
    plt.close()

    print('Score function: {}'.format(score_fn))
    print('GP vs MLP: {}'.format(
        ttest_ind(
            df[df.model == 'gp'].affinity,
            df[df.model == 'mlper1'].affinity,
        )))
    print('Hybrid vs MLP: {}'.format(
        ttest_ind(
            df[df.model == 'sparsehybrid'].affinity,
            df[df.model == 'mlper1'].affinity,
        )))
    print('')
Пример #7
0
def score_scatter(y_pred, y, var_pred, regress_type, prefix=''):
    y_pred = y_pred[:]
    y_pred[y_pred < 0] = 0
    y_pred[y_pred > 10000] = 10000

    plt.figure()
    plt.scatter(y_pred, var_pred, alpha=0.3,
                c=(y - y.min()) / (y.max() - y.min()))
    plt.viridis()
    plt.xlabel('Predicted score')
    plt.ylabel('Variance')
    plt.savefig('figures/variance_vs_pred_{}regressors{}.png'
                .format(prefix, regress_type), dpi=300)
    plt.close()
Пример #8
0
import os
os.mkdir('./output')
output_folder = 'output/'

#Area under the ROC curve
fpr, tpr, thresholds = roc_curve((y_true), y_scores)
AUC_ROC = roc_auc_score(y_true, y_scores)
print("\nArea under the ROC curve: " + str(AUC_ROC))
roc_curve = plt.figure()
plt.plot(fpr, tpr, '-', label='Area Under the Curve (AUC = %0.4f)' % AUC_ROC)
plt.title('ROC curve')
plt.xlabel("FPR (False Positive Rate)")
plt.ylabel("TPR (True Positive Rate)")
plt.legend(loc="lower right")
plt.savefig(output_folder + "ROC.png")

#Precision-recall curve
precision, recall, thresholds = precision_recall_curve(y_true, y_scores)
precision = np.fliplr([precision])[0]
recall = np.fliplr([recall])[0]
AUC_prec_rec = np.trapz(precision, recall)
print("\nArea under Precision-Recall curve: " + str(AUC_prec_rec))
prec_rec_curve = plt.figure()
plt.plot(recall,
         precision,
         '-',
         label='Area Under the Curve (AUC = %0.4f)' % AUC_prec_rec)
plt.title('Precision - Recall curve')
plt.xlabel("Recall")
plt.ylabel("Precision")
import warnings
warnings.filterwarnings('ignore')

# Define the input to be tested
test_image_index = 17
test_image = test_x[[test_image_index]]
test_image_prediction = test_y[test_image_index]

with DeepExplain(session=sess) as de:
    log = model(X)
    attributions = {
        'Gradient * Input':
        de.explain('grad*input', log * test_image_prediction, X, test_image),
        'Epsilon-LRP':
        de.explain('elrp', log * test_image_prediction, X, test_image)
    }

# Plot attributions
n_cols = len(attributions) + 1
fig, axes = plt.subplots(nrows=1, ncols=n_cols, figsize=(3 * n_cols, 3))
plot(test_image.reshape(28, 28), cmap='Greys',
     axis=axes[0]).set_title('Original')
print(test_image_prediction)
for i in range(len(test_image_prediction)):
    if test_image_prediction[i] == float(1):
        print("Test output : ", i)
for i, method_name in enumerate(sorted(attributions.keys())):
    plt.savefig(
        plot(attributions[method_name].reshape(28, 28),
             xi=test_image.reshape(28, 28),
             axis=axes[1 + i]).set_title(method_name))
Пример #10
0
    datasets, genes_list, n_cells = load_names(data_names)
    #datasets, genes = merge_datasets(datasets, genes_list)
    #datasets_dimred, genes = process_data(datasets, genes, hvg=hvg)
    datasets, genes = correct(datasets, genes_list)
    X = np.concatenate(datasets)
    X[X < 0] = 0

    cell_labels = (
        open('data/cell_labels/pancreas_cluster.txt').read().rstrip().split())
    er_idx = [i for i, cl in enumerate(cell_labels) if cl == 'beta_er']
    beta_idx = [i for i, cl in enumerate(cell_labels) if cl == 'beta']

    gadd_idx = list(genes).index('GADD45A')
    herp_idx = list(genes).index('HERPUD1')

    plt.figure()
    plt.boxplot([X[er_idx, gadd_idx], X[beta_idx, gadd_idx]], showmeans=True)
    plt.title('GADD45A (p < {})'.format(
        ttest_ind(X[er_idx, gadd_idx], X[beta_idx, gadd_idx])[1]))
    plt.xticks([1, 2], ['beta_er', 'beta'])
    plt.ylabel('Scaled gene expression')
    plt.savefig('er_stress_GADD45A.svg')

    plt.figure()
    plt.boxplot([X[er_idx, herp_idx], X[beta_idx, herp_idx]], showmeans=True)
    plt.title('HERPUD1 (p < {})'.format(
        ttest_ind(X[er_idx, herp_idx], X[beta_idx, herp_idx])[1]))
    plt.xticks([1, 2], ['beta_er', 'beta'])
    plt.ylabel('Scaled gene expression')
    plt.savefig('er_stress_HERPUD1.svg')
Пример #11
0
def parse_log(regress_type, experiment, **kwargs):
    log_fname = ('iterate_davis2011kinase_{}_{}.log'.format(
        regress_type, experiment))

    iteration = 0
    iter_to_Kds = {}
    iter_to_idxs = {}

    with open(log_fname) as f:

        while True:
            line = f.readline()
            if not line:
                break

            if not line.startswith('2019') and not line.startswith('2020'):
                continue
            if not ' | ' in line:
                continue

            line = line.split(' | ')[1]

            if line.startswith('Iteration'):
                iteration = int(line.strip().split()[-1])
                if not iteration in iter_to_Kds:
                    iter_to_Kds[iteration] = []
                if not iteration in iter_to_idxs:
                    iter_to_idxs[iteration] = []

                continue

            elif line.startswith('\tAcquire '):
                fields = line.strip().split()

                Kd = float(fields[-1])
                iter_to_Kds[iteration].append(Kd)

                chem_idx = int(fields[1].lstrip('(').rstrip(','))
                prot_idx = int(fields[2].strip().rstrip(')'))
                iter_to_idxs[iteration].append((chem_idx, prot_idx))

                continue

    assert (iter_to_Kds.keys() == iter_to_idxs.keys())
    iterations = sorted(iter_to_Kds.keys())

    # Plot Kd over iterations.

    Kd_iter, Kd_iter_max, Kd_iter_min = [], [], []
    all_Kds = []
    for iteration in iterations:
        Kd_iter.append(np.mean(iter_to_Kds[iteration]))
        Kd_iter_max.append(max(iter_to_Kds[iteration]))
        Kd_iter_min.append(min(iter_to_Kds[iteration]))
        all_Kds += list(iter_to_Kds[iteration])

        if iteration == 0:
            print('First average Kd is {}'.format(Kd_iter[0]))
        elif iteration > 4 and experiment == 'perprot':
            break

    print('Average Kd is {}'.format(np.mean(all_Kds)))

    plt.figure()
    plt.scatter(iterations, Kd_iter)
    plt.plot(iterations, Kd_iter)
    plt.fill_between(iterations, Kd_iter_min, Kd_iter_max, alpha=0.3)
    plt.viridis()
    plt.title(' '.join([regress_type, experiment]))
    plt.savefig('figures/Kd_over_iterations_{}_{}.png'.format(
        regress_type, experiment))
    plt.close()

    return

    # Plot differential entropy of acquired samples over iterations.

    chems = kwargs['chems']
    prots = kwargs['prots']
    chem2feature = kwargs['chem2feature']
    prot2feature = kwargs['prot2feature']

    d_entropies = []
    X_acquired = []
    for iteration in iterations:
        for i, j in iter_to_idxs[iteration]:
            chem = chems[i]
            prot = prots[j]
            X_acquired.append(chem2feature[chem] + prot2feature[prot])
        if len(X_acquired) <= 1:
            d_entropies.append(float('nan'))
        else:
            gaussian = GaussianMixture().fit(np.array(X_acquired))
            gaussian = multivariate_normal(gaussian.means_[0],
                                           gaussian.covariances_[0])
            d_entropies.append(gaussian.entropy())

    print('Final differential entropy is {}'.format(d_entropies[-1]))

    plt.figure()
    plt.scatter(iterations, d_entropies)
    plt.plot(iterations, d_entropies)
    plt.viridis()
    plt.title(' '.join([regress_type, experiment]))
    plt.savefig('figures/entropy_over_iterations_{}_{}.png'.format(
        regress_type, experiment))
    plt.close()
                    continue
                seen.add(zinc)
                order_list.append((order, Kd))

            order_list = [
                order for order, _ in sorted(order_list, key=lambda x: x[1])
            ]

            plt.subplot(1, 3, bidx + 1)
            sns.barplot(
                x='order',
                y='Kdpoint',
                data=df_subset,
                color=palette[bidx],
                order=order_list,
                ci=95,
                capsize=0.4,
                errcolor='#888888',
            )
            sns.swarmplot(
                x='order',
                y='Kdpoint',
                data=df_subset,
                color='black',
                order=order_list,
            )
            plt.ylim([-100, 10100])

        plt.savefig('figures/prediction_barplot_{}.svg'.format(model))
        plt.close()
def latent_scatter(var_unk_pred, y_unk_pred, acquisition, **kwargs):
    chems = kwargs['chems']
    chem2feature = kwargs['chem2feature']
    idx_obs = kwargs['idx_obs']
    idx_unk = kwargs['idx_unk']
    regress_type = kwargs['regress_type']
    prot_target = kwargs['prot_target']

    chem_idx_obs = sorted(set([i for i, _ in idx_obs]))
    chem_idx_unk = sorted(set([i for i, _ in idx_unk]))

    feature_obs = np.array([chem2feature[chems[i]] for i in chem_idx_obs])
    feature_unk = np.array([chem2feature[chems[i]] for i in chem_idx_unk])

    from sklearn.neighbors import NearestNeighbors
    nbrs = NearestNeighbors(n_neighbors=1).fit(feature_obs)
    dist = np.ravel(nbrs.kneighbors(feature_unk)[0])
    print('Distance Spearman r = {}, P = {}'.format(
        *ss.spearmanr(dist, var_unk_pred)))
    print('Distance Pearson rho = {}, P = {}'.format(
        *ss.pearsonr(dist, var_unk_pred)))

    X = np.vstack([feature_obs, feature_unk])
    labels = np.concatenate(
        [np.zeros(len(chem_idx_obs)),
         np.ones(len(chem_idx_unk))])
    sidx = np.argsort(-var_unk_pred)

    from fbpca import pca
    U, s, Vt = pca(
        X,
        k=3,
    )
    X_pca = U * s

    from umap import UMAP
    um = UMAP(
        n_neighbors=15,
        min_dist=0.5,
        n_components=2,
        metric='euclidean',
    )
    X_umap = um.fit_transform(X)

    from MulticoreTSNE import MulticoreTSNE as TSNE
    tsne = TSNE(
        n_components=2,
        n_jobs=20,
    )
    X_tsne = tsne.fit_transform(X)

    if prot_target is None:
        suffix = ''
    else:
        suffix = '_' + prot_target

    for name, coords in zip(
        ['pca', 'umap', 'tsne'],
        [X_pca, X_umap, X_tsne],
    ):
        plt.figure()
        sns.scatterplot(
            x=coords[labels == 1, 0],
            y=coords[labels == 1, 1],
            color='blue',
            alpha=0.1,
        )
        plt.scatter(
            x=coords[labels == 0, 0],
            y=coords[labels == 0, 1],
            color='orange',
            alpha=1.0,
            marker='x',
            linewidths=10,
        )
        plt.savefig('figures/latent_scatter_{}_ypred_{}{}.png'.format(
            name, regress_type, suffix),
                    dpi=300)
        plt.close()

        plt.figure()
        plt.scatter(x=coords[labels == 1, 0],
                    y=coords[labels == 1, 1],
                    c=ss.rankdata(var_unk_pred),
                    alpha=0.1,
                    cmap='coolwarm')
        plt.savefig('figures/latent_scatter_{}_var_{}{}.png'.format(
            name, regress_type, suffix),
                    dpi=300)
        plt.close()

        plt.figure()
        plt.scatter(x=coords[labels == 1, 0],
                    y=coords[labels == 1, 1],
                    c=-acquisition,
                    alpha=0.1,
                    cmap='hot')
        plt.savefig('figures/latent_scatter_{}_acq_{}{}.png'.format(
            name, regress_type, suffix),
                    dpi=300)
        plt.close()
                elif task == 2:
                    # Distance Transform
                    axes[n_class, col].imshow(dist_ref_h[:, :, n_class],
                                              cmap=cm.Greys_r)
        axes[0, 0].set_title('Patch')
        axes[0, 1].set_title('Seg Ref')
        axes[0, 2].set_title('Seg Pred')
        axes[0, 3].set_title('Bound Ref')
        axes[0, 4].set_title('Bound Pred')
        axes[0, 5].set_title('Dist Ref')
        axes[0, 6].set_title('Dist Pred')

        for n_class in range(args.num_classes):
            axes[n_class, 0].set_ylabel(f'Class {n_class}')

        plt.savefig(os.path.join(args.output_path, f'pred{i}_classes.jpg'))

        # Color
        fig2, (ax1, ax2, ax3) = plt.subplots(nrows=1, ncols=3, figsize=(10, 5))
        ax1.set_title('Original')
        ax1.imshow(img)
        ax2.set_title('Pred HSV in RGB')
        task = 3
        hsv_pred = patches_pred[task][i]
        # print(f'HSV max {i}: {hsv_patch.max()}, HSV min: {hsv_patch.min()}')
        # As long as the normalization process was just img = img / 255
        hsv_patch = (hsv_pred * np.array([179, 255, 255])).astype(np.uint8)
        rgb_patch = cv2.cvtColor(hsv_patch, cv2.COLOR_HSV2RGB)
        ax2.imshow(rgb_patch)
        # ax3.set_title('Difference between both')
        # diff = np.mean(rgb_patch - img, axis=-1)
    def plan(self, start, goal):
        self.timer = tic()
        # if self.trajectory_completed:       # return original trajectory
        #     return self.trajectory.popleft(), tic() - self.timer
        if self.plan_completed:  # return smoothed trajectory
            return self.trajectory_smoothed.popleft(), tic() - self.timer
        if len(self.tree) == 0:
            # initialization
            self.root = tuple(start)
            self.goal = tuple(goal)
            self.tree[self.root] = [0, None]  # [cost, parent]
            self.vol_free = (self.boundary[0, 3] - self.boundary[0, 0]) * \
                            (self.boundary[0, 4] - self.boundary[0, 1]) * \
                            (self.boundary[0, 5] - self.boundary[0, 2])
            for k in range(self.blocks.shape[0]):
                self.vol_free -= (self.blocks[k, 3] - self.blocks[k, 0]) * \
                                 (self.blocks[k, 4] - self.blocks[k, 1]) * \
                                 (self.blocks[k, 5] - self.blocks[k, 2])
            self.V = int(self.vol_free)
            self.r = 1.1 * 2 * (
                (1 + 1 / 3) * (self.vol_free * 3 /
                               (4 * np.pi)) * np.log(self.V) / self.V)**(1 / 3)
        if not self.map_completed:
            self.construct_tree()
            move_time = tic() - self.timer
            if self.display:
                self.display_tree()
                if self.map_completed:
                    plt.savefig(
                        os.path.join('results', self.map_name + '_tree'))
            else:
                if self.map_completed:
                    for node in self.tree.keys():
                        self.ax.plot(node[0:1],
                                     node[1:2],
                                     node[2:],
                                     'go',
                                     markersize=1)
                    plt.savefig(
                        os.path.join('results', self.map_name + '_tree'))
            return start, move_time
        if not self.trajectory_completed:
            # planning
            cur_node = self.goal
            while cur_node:
                self.trajectory.appendleft(np.array(cur_node))
                cur_node = self.tree[cur_node][1]
                if tic() - self.timer > 1.5:  # timer, exit if exceeds 1.5 s
                    self.goal = cur_node
                    return start, tic() - self.timer
            self.trajectory_completed = True
            return start, tic() - self.timer
        if not self.plan_completed:
            # smooth trajectory
            while len(self.trajectory) > 1:
                if len(self.trajectory_smoothed) == 0:
                    self.trajectory_smoothed.append(self.trajectory.popleft())
                if collision_free(self.trajectory_smoothed[-1],
                                  self.trajectory[1], self.blocks):
                    self.trajectory.popleft()
                else:
                    next_move = self.trajectory[0]
                    if dist_sq(next_move, self.trajectory_smoothed[-1]) > 1:
                        self.trajectory_smoothed.append(
                            self.trajectory_smoothed[-1] + .8 *
                            (next_move - self.trajectory_smoothed[-1]) /
                            dist(next_move, self.trajectory_smoothed[-1]))
                    else:
                        self.trajectory_smoothed.append(
                            self.trajectory.popleft())
                if tic() - self.timer > 1.5:
                    return start, tic() - self.timer

            goal = self.trajectory.popleft()
            while dist_sq(goal, self.trajectory_smoothed[-1]) > 1:
                self.trajectory_smoothed.append(
                    self.trajectory_smoothed[-1] + .8 *
                    (goal - self.trajectory_smoothed[-1]) /
                    dist(goal, self.trajectory_smoothed[-1]))
            self.trajectory_smoothed.append(goal)
            self.plan_completed = True
            return start, tic() - self.timer
Пример #16
0
            fname = 'target/log/train_davis2011kinase_{}.log'.format(model)
            data += parse_log(model, fname)

    df = pd.DataFrame(data, columns=[
        'model', 'metric', 'quadrant', 'value', 'uncertainty',
    ])

    quadrants = sorted(set(df.quadrant))
    metrics = sorted(set(df.metric))

    for quadrant in quadrants:
        for metric in metrics:

            df_subset = df[(df.metric == metric) &
                           (df.quadrant == quadrant)]

            plt.figure()
            sns.barplot(x='model', y='value', data=df_subset, ci=None,
                        order=models, hue='uncertainty', dodge=False,
                        palette=sns.color_palette("RdBu", n_colors=8))
            sns.swarmplot(x='model', y='value', data=df_subset, color='black',
                          order=models)
            if (metric == 'Pearson rho' or metric == 'Spearman r') \
               and quadrant != 'unknown_all':
                plt.ylim([ -0.05, 0.7 ])
            if metric == 'MSE' and quadrant != 'unknown_all':
                plt.ylim([ -0.01e7, 3e7 ])
            plt.savefig('figures/benchmark_cv_{}_{}.svg'
                        .format(metric, quadrant))
            plt.close()
Пример #17
0
    # Project to higher dimension.
    Z = np.absolute(np.random.randn(2, 100))
    datasets = [np.dot(s, Z) for s in samples]

    # Add batch effect "noise."
    datasets = [ds + np.random.randn(1, 100) for ds in datasets]

    # Normalize datasets.
    datasets = [normalize(ds, axis=1) for ds in datasets]

    tsne = TSNE(n_iter=400, perplexity=100, verbose=2, random_state=69)

    tsne.fit(np.concatenate(datasets[1:]))
    plot_clusters(tsne.embedding_, np.concatenate(clusters[1:]), s=500)
    plt.title('Uncorrected data')
    plt.savefig('simulation_uncorrected.svg')

    # Assemble datasets.
    assembled = assemble(datasets[1:], verbose=1, sigma=1, knn=10, approx=True)
    tsne.fit(datasets[1])
    plot_clusters(tsne.embedding_, clusters[1], s=500)
    plt.title('Dataset 1')
    plt.xlabel('t-SNE 1')
    plt.ylabel('t-SNE 2')
    plt.savefig('simulation_ds1.svg')

    tsne.fit(datasets[2])
    plot_clusters(tsne.embedding_, clusters[2], s=500)
    plt.title('Dataset 2')
    plt.xlabel('t-SNE 1')
    plt.ylabel('t-SNE 2')
Пример #18
0
if __name__ == '__main__':
    labels = np.array(open('data/cell_labels/all.txt').read().rstrip().split())

    # Scanorama.
    X = np.loadtxt('data/panorama_embedding.txt')
    idx = np.random.choice(X.shape[0], size=20000, replace=False)
    sil_pan = sil(X[idx, :], labels[idx])
    print(np.median(sil_pan))

    # scran MNN.
    X = np.loadtxt('data/mnn_embedding.txt')
    idx = np.random.choice(X.shape[0], size=20000, replace=False)
    sil_mnn = sil(X[idx, :], labels[idx])
    print(np.median(sil_mnn))

    # Seurat CCA.
    X = np.loadtxt('data/cca_embedding.txt')
    idx = np.random.choice(X.shape[0], size=20000, replace=False)
    sil_cca = sil(X[idx, :], labels[idx])
    print(np.median(sil_cca))

    print(ttest_ind(sil_pan, sil_mnn))
    print(ttest_ind(sil_pan, sil_cca))

    plt.figure()
    plt.boxplot([sil_pan, sil_mnn, sil_cca], showmeans=True)
    plt.title('Distributions of Silhouette Coefficients')
    plt.xticks([1, 2, 3], ['Scanorama', 'scran MNN', 'Seurat CCA'])
    plt.ylabel('Silhouette Coefficient')
    plt.savefig('silhouette.svg')
Пример #19
0
        ax1 = fig.add_subplot(p)
        ax1.set_ylabel('#Keys Moved')
        formatter = EngFormatter(places=1)
        ax1.yaxis.set_major_formatter(formatter)
        ax1.plot(d[i]['x'], d[i]['y'], 'o', label=i, markersize=6)
        ax1.text(0.025, 0.95, names[i], va='top', transform=ax1.transAxes, fontsize=textsize)
        print "ax1",i,"done"

        p = 212
        i = 'M'
        ax2 = fig.add_subplot(p,sharex=ax1)
        ax2.set_ylabel('#Keys Moved')
        formatter = EngFormatter(places=1)
        ax2.yaxis.set_major_formatter(formatter)
        ax2.plot(d[i]['x'], d[i]['y'], 'o', label=i,markersize=6)
        ax2.text(0.025, 0.95, names[i], va='top', transform=ax2.transAxes, fontsize=textsize)
        print "ax2",i,"done"

        ax2.set_xlabel('Time (Seconds)')

    else:
        fig.set_size_inches(20,2)
        axS = fig.add_subplot(111)
        axS.yaxis.set_major_formatter(formatter)
        axS.plot(s['x'], s['y'], label='Size')
        axS.set_ylabel('#Nodes')
        axS.text(0.025, 0.95, 'Network size', va='top', transform=axS.transAxes, fontsize=textsize)
        axS.set_xlabel('Time (Seconds)')

    plt.savefig("bw.pdf",bbox_inches='tight')
    if not os.path.exists('./results/RBF'):
        os.mkdir('./results/RBF')
    for i in range(10):
        id = i
        for tf in range(2):
            env = virl.Epidemic(problem_id=id, noisy=tf)
            states, rewards, actions = exec_policy(env,
                                                   rbf_func,
                                                   verbose=False)
            fig = get_fig(states, rewards)
            if tf:
                tf = 'True'
            else:
                tf = 'False'
            plt.savefig(
                dpi=300,
                fname='./results/RBF/problem_id={}_noisy={}.jpg'.format(
                    id, tf))
            print("\tproblem_id={} noisy={} Total rewards:{:.4f}".format(
                id, tf, sum(rewards)))
            plt.close()
fig, ax = plt.subplots(figsize=(8, 6))
for i in range(10):
    env = virl.Epidemic(stochastic=True)
    states, rewards, actions = exec_policy(env, rbf_func, verbose=False)
    ax.plot(np.array(states)[:, 1], label=f'draw {i}')
ax.set_xlabel('weeks since start of epidemic')
ax.set_ylabel('Number of Infectious persons')
ax.set_title('Simulation of 10 stochastic episodes with RBF policy')
ax.legend()
plt.savefig(dpi=300, fname='./results/RBF/stochastic.png')
plt.close()