def plot_predictions(eval_labels, predictions, norm_flag, correlations, mses,
                     eval_names, output_directory, num_classes, cell_names,
                     file_txt):
    for idx in range(len(eval_labels)):
        y_samples_eval = eval_labels[idx]
        predicted_classes = predictions[idx]
        sample_names = eval_names[idx]

        x_axis = np.arange(num_classes)
        xticks = cell_names

        plt.figure()
        width = 0.35

        plt.figure(figsize=(16, 9))
        #        plt.subplot(mum_plt_row, mum_plt_col, i + 1)
        plt.bar(x_axis,
                y_samples_eval,
                width,
                color='#f99fa1',
                edgecolor='none',
                label='true activity')
        if norm_flag:
            plt.bar(x_axis + width,
                    utils.minmax_scale(predicted_classes, y_samples_eval),
                    width,
                    color='#014ead',
                    edgecolor='none',
                    label='prediction')
            ylabel_text = 'Normalized activity'
        else:
            plt.bar(x_axis + width,
                    predicted_classes,
                    width,
                    color='#014ead',
                    edgecolor='none',
                    label='prediction')
            ylabel_text = 'Activity'
        plt.xticks(x_axis + width, xticks, rotation='vertical', fontsize=9)
        plt.title('{0}, correlation = {1:.4f}, mse = {2:.4f}'.format(
            sample_names, correlations[idx], mses[idx]))
        plt.xlabel('Cell type', fontsize=12)
        plt.ylabel(ylabel_text, fontsize=12)
        plt.legend()

        fig = plt.gcf()
        fig.tight_layout()
        plt.savefig(output_directory + sample_names + file_txt + '.svg',
                    bbox_inches='tight')
        plt.close()
def plot_predictions_subplot(eval_labels, predictions, correlations, mses,
                             eval_names, output_directory, num_classes,
                             cell_names, file_txt):
    for idx in range(len(eval_labels)):
        y_samples_eval = eval_labels[idx]
        predicted_classes = predictions[idx]
        sample_names = eval_names[idx]

        # Plot
        x_axis = np.arange(num_classes)
        xticks = cell_names

        plt.figure()
        width = 0.5

        fig, axs = plt.subplots(2, figsize=(24, 12))
        #        fig.suptitle('{0}, correlation = {1:.3f}'.format(sample_names, correlations[idx]), fontsize=18)
        line_labels = ['true activity', 'prediction']
        l0 = axs[0].bar(
            x_axis, y_samples_eval, width,
            color='#f99fa1')  #, edgecolor='none', label='true activity'
        l1 = axs[1].bar(
            x_axis,
            utils.minmax_scale(predicted_classes, y_samples_eval),
            width,
            color='#014ead')  #, edgecolor='none', label='prediction'
        axs[0].set_title('{0}, correlation = {1:.4f}, mse = {2:.4f}'.format(
            sample_names, correlations[idx], mses[idx]),
                         fontsize=30)

        fig.legend(
            [l0, l1],  # The line objects
            labels=line_labels,  # The labels for each line
            loc="upper right",  # Position of legend
            borderaxespad=0.1,  # Small spacing around legend box
            fontsize=12  # Title for the legend
        )

        plt.xticks(x_axis, xticks, rotation='vertical', fontsize=15)
        plt.xlabel('Cell type', fontsize=24)
        plt.ylabel('Normalized activity', fontsize=24)

        fig = plt.gcf()
        fig.tight_layout()
        plt.savefig(output_directory + sample_names + file_txt +
                    '_subplot.svg',
                    bbox_inches='tight')
        plt.close()
Example #3
0
 def test_minmax_scaler(self):
     self.assertEqual(minmax_scale([1, 1, 1]), [0, 0, 0], 'not zero list')
     self.assertEqual(minmax_scale([0, 0, 0]), [0, 0, 0], 'not zero list')
     self.assertEqual(
         minmax_scale([1, 2, 3]).tolist(), [0., 0.5, 1.], 'wrong calculate')
     self.assertEqual(
         minmax_scale([0, 2, 2, 10]).tolist(), [0., 0.2, 0.2, 1.],
         'wrong calculate')
     self.assertIsInstance(minmax_scale([1, 2, 3]), np.ndarray,
                           'result is not np.ndarray')
     self.assertGreaterEqual(np.min(minmax_scale([1, 20, 300])), 0,
                             'min value less than zero')
     self.assertLessEqual(np.min(minmax_scale([1, 20, 300])), 1,
                          'max value more than 1')
Example #4
0
def update_output(
        rows,
        timestamp_load, timestamp_col, timestamp_row, timestamp_lab, timestanp_rng, timestamp_minmax, timestamp_normal,
        column_name, table_data, existing_columns, lab_column, rng_column, amount, minmax_column, a, b, normal_column
):
    if rows is None:
        return [{}], []
    df = pd.DataFrame(rows)

    time_load = string_to_datetime(timestamp_load)
    time_col = string_to_datetime(timestamp_col)
    time_row = string_to_datetime(timestamp_row)
    time_lab = string_to_datetime(timestamp_lab)
    time_rng = string_to_datetime(timestanp_rng)
    time_minmax = string_to_datetime(timestamp_minmax)
    time_normal = string_to_datetime(timestamp_normal)

    all_times = [time_load, time_col, time_row, time_lab, time_rng, time_minmax, time_normal]

    if time_load == max(all_times):
        columns = [
            {'id': column, 'name': column, 'deletable': True, 'renamable': True} for i, column in enumerate(df.columns)
        ]
        return df.to_dict('records'), columns
    elif time_col == max(all_times):
        if column_name:
            existing_columns.append({'id': column_name, 'name': column_name, 'deletable': True, 'renamable': True})
        return table_data, existing_columns
    elif time_row == max(all_times):
        table_data.append({c['id']: '' for c in existing_columns})
        return table_data, existing_columns
    elif time_lab == max(all_times):
        if lab_column:
            df = pd.DataFrame(table_data)
            df, new_col = text_to_labels(df, lab_column)
            table_data = df.to_dict('records')
            if new_col:
                existing_columns.append({'id': new_col, 'name': new_col, 'deletable': True, 'renamable': True})
        return table_data, existing_columns
    elif time_rng == max(all_times):
        if rng_column and amount and amount.isdigit():
            df = pd.DataFrame(table_data)
            df, new_col = make_ranges(df, rng_column, int(amount))
            table_data = df.to_dict('records')
            if new_col:
                existing_columns.append({'id': new_col, 'name': new_col, 'deletable': True, 'renamable': True})
        return table_data, existing_columns
    elif time_minmax == max(all_times):
        if minmax_column and a and b and a.isdigit() and b.isdigit():
            df = pd.DataFrame(table_data)
            df, new_col = minmax_scale(df, minmax_column, int(a), int(b))
            table_data = df.to_dict('records')
            if new_col:
                existing_columns.append({'id': new_col, 'name': new_col, 'deletable': True, 'renamable': True})
        return table_data, existing_columns
    elif time_normal == max(all_times):
        if normal_column:
            df = pd.DataFrame(table_data)
            df, new_col = normalize(df, normal_column)
            table_data = df.to_dict('records')
            if new_col:
                existing_columns.append({'id': new_col, 'name': new_col, 'deletable': True, 'renamable': True})
        return table_data, existing_columns
    else:
        return [{}], []
def plot_random_predictions(eval_labels,
                            predictions,
                            correlations,
                            ind_collection,
                            eval_names,
                            output_directory,
                            num_classes,
                            cell_names,
                            title=None,
                            scale=True):
    for n in range(3):
        mum_plt_row = 1
        mum_plt_col = 1
        num_plt = mum_plt_row * mum_plt_col
        # 3 plots for each correlation categories
        for k in range(len(ind_collection) - 1):
            if len(ind_collection[k + 1]) < num_plt: continue
            idx = random.sample(ind_collection[k + 1], num_plt)

            y_samples_eval = eval_labels[idx]
            predicted_classes = predictions[idx]
            sample_names = eval_names[idx]

            # Plot
            x_axis = np.arange(num_classes)
            if cell_names == []:
                xticks = []
            else:
                xticks = cell_names

            plt.figure(1)
            width = 0.35
            for i in range(num_plt):
                plt.figure(figsize=(16, 9))
                plt.subplot(mum_plt_row, mum_plt_col, i + 1)
                plt.bar(x_axis,
                        y_samples_eval[i],
                        width,
                        color='#f99fa1',
                        edgecolor='none',
                        label='true activity')
                if scale:
                    plt.bar(x_axis + width,
                            utils.minmax_scale(predicted_classes[i],
                                               y_samples_eval[i]),
                            width,
                            color='#014ead',
                            edgecolor='none',
                            label='prediction')
                    scale_txt = '_normalized'
                else:
                    plt.bar(x_axis + width,
                            predicted_classes[i],
                            width,
                            color='#014ead',
                            edgecolor='none',
                            label='prediction')
                    scale_txt = '_original'
                plt.xticks(x_axis + width,
                           xticks,
                           rotation='vertical',
                           fontsize=9)
                plt.title('{0}, correlation = {1:.3f}'.format(
                    sample_names[i], correlations[idx[i]]))
                plt.xlabel('cell type', fontsize=12)
                plt.ylabel(scale_txt + ' activity', fontsize=12)
                plt.legend()

            fig = plt.gcf()
            fig.tight_layout()
            if not title:
                title = ''
            plt.savefig(output_directory + title +
                        "basset_cor_q{0}{1}".format(k, n + 1) + scale_txt +
                        ".svg",
                        bbox_inches='tight')

            plt.close()
Example #6
0
        T -= 0.01
    elif command == 'scale_true':
        SCALE = True
    elif command == 'scale_false':
        SCALE = False

    # Что показываем
    for i, key in enumerate(get_best_banner(banner, top=TOP, t=T,
                                            scale=SCALE)):
        banner[key][0] += 1
        print(i, key)

    # Update stats
    ctr = get_stats(banner)
    if SCALE:
        prob = softmax(minmax_scale(ctr), t=T)
    else:
        prob = softmax(ctr, t=T)

    for i, key in enumerate(banner):
        banner[key][2] = round(ctr[i], 6)
        banner[key][3] = round(prob[i], 6)

    command = input('Your choice: ')

    # вывод статистики
    if command == 'stat':
        print('\n', '=' * 50, f'Temperature is = {T}', sep='\n')
        print('Scale is =', SCALE, end='\n' * 2)
        print("{:<6} {:<6} {:<6} {:<10} {:<10}".format('Key', 'view', 'click',
                                                       'ctr', 'prob'))