Ejemplo n.º 1
0
        def cmd():
            rbh_alignments = pd.read_csv(self.train_rbh_fn)
            rbh_alignments['q_name'] = rbh_alignments['q_name'].apply(
                fix_transdecoder_name)
            print(rbh_alignments.q_name[0])
            train_alignments = pd.concat([
                group
                for group in parsers.maf_to_df_iter(self.transcriptome_x_db_fn)
            ])
            print(train_alignments.q_name[0])
            train_alignments = train_alignments.ix[rbh_alignments.index]
            self.fit(train_alignments)

            data = self.preprocess(train_alignments)
            data = self.transform(data)
            xx, yy, Z = self.plot_grid(data.min(), data.max())
            with FigureManager(filename=self.model_fn +
                               '.training.pdf') as (fig, ax):
                ax.set_title('Boundaries and Training Data')
                ax.contourf(xx,
                            yy,
                            Z,
                            levels=np.linspace(Z.min(), 0, 7),
                            cmap=plt.cm.Blues_r)
                a = plt.contour(xx,
                                yy,
                                Z,
                                levels=[0],
                                linewidths=2,
                                colors='red')
                ax.contourf(xx, yy, Z, levels=[0, Z.max()], colors='orange')

                ax.scatter(data[:, 0], data[:, 1], c='white')
                ax.set_xlim((data.min(), data.max()))
                ax.set_ylim((data.min(), data.max()))
Ejemplo n.º 2
0
    def test_subplots_ncols(self):
        '''Test that FigureManager generates multiple subplots with ncols.
        '''

        with FigureManager(ncols=3) as (fig, ax):
            self.assertEqual(len(ax), 3)
            self.assertIsInstance(fig, figure.Figure)
            for axis in ax:
                self.assertIsInstance(axis, axes.Axes)
Ejemplo n.º 3
0
    def test_save(self):
        '''Test that FigureManager saves a plot.
        '''
        with TemporaryDirectory() as td:
            with TemporaryFile(td) as filename:
                with FigureManager(filename=filename + '.pdf') as (fig, ax):
                    ax.plot(range(10), range(10))

                self.assertTrue(os.path.isfile(filename + '.pdf'))
Ejemplo n.º 4
0
    def test_exception(self):
        '''Test that FigureManager handles internal exceptions properly.
        '''
        with TemporaryDirectory() as td:
            with TemporaryFile(td) as filename:

                # now we test the manager
                try:
                    rregexp = self.assertRaisesRegex
                except AttributeError:
                    rregexp = self.assertRaisesRegexp
                with rregexp(RuntimeError, 'TEST'):
                    with FigureManager(filename=filename) as (fig, ax):
                        raise RuntimeError('TEST')
                self.assertFalse(os.path.isfile(filename),
                                 msg='Should not have saved a file.')
Ejemplo n.º 5
0
def plot_crbh_fit(model_df,
                  hits_df,
                  model_plot_fn,
                  show=False,
                  figsize=(10, 10),
                  feature_col='E',
                  length_col='s_aln_len',
                  **fig_kwds):

    plt.style.use('seaborn-ticks')

    with FigureManager(model_plot_fn, show=show, figsize=figsize,
                       **fig_kwds) as (fig, ax):

        if not len(hits_df):
            return

        sample_size = min(len(hits_df), 5000)
        hits_df, scaled_col = scale_evalues(hits_df,
                                            name=feature_col,
                                            inplace=False)

        ax.scatter(hits_df[length_col],
                   hits_df[scaled_col],
                   s=10,
                   alpha=0.7,
                   c=sns.xkcd_rgb['ruby'],
                   marker='o',
                   label='Query Hits')

        ax.scatter(model_df['center'],
                   model_df['fit'],
                   label='CRBL Fit',
                   c=sns.xkcd_rgb['twilight blue'],
                   marker='o',
                   s=5,
                   alpha=0.7)

        leg = ax.legend(fontsize='medium', scatterpoints=3, frameon=True)
        leg.get_frame().set_linewidth(1.0)

        ax.set_xlim(model_df['center'].min(), model_df['center'].max())
        ax.set_ylim(0,
                    max(model_df['fit'].max(), hits_df[scaled_col].max()) + 50)
        ax.set_ylabel('Score ($E_{scaled}$)' if scaled_col == 'E_scaled'\
                      else 'Score ({0})'.format(scaled_col))
        ax.set_xlabel('Alignment Length')
Ejemplo n.º 6
0
        def cmd():
            results = []
            alns = []
            N = 0
            y = 0
            for group in parsers.maf_to_df_iter(self.transcriptome_x_db_fn):
                N += len(group)
                mask = self.predict(group)
                results.append(group.ix[mask])
                alns.append(group[self.features])
                y += mask.sum()
            results = pd.concat(results)
            results.to_csv(self.crbl_fn)
            aln_df = pd.concat(alns)
            print(N, y)

            data = self.preprocess(aln_df)
            data = self.transform(data)
            xx, yy, Z = self.plot_grid(data.min(), data.max())
            with FigureManager(filename=self.model_fn + '.fitted.pdf') as (fig,
                                                                           ax):
                ax.set_title('Boundary and Predict Data')
                ax.contourf(xx,
                            yy,
                            Z,
                            levels=np.linspace(Z.min(), 0, 7),
                            cmap=plt.cm.Blues_r)
                a = plt.contour(xx,
                                yy,
                                Z,
                                levels=[0],
                                linewidths=2,
                                colors='red')
                ax.contourf(xx, yy, Z, levels=[0, Z.max()], colors='orange')

                ax.scatter(data[:, 0], data[:, 1], c='white')
                ax.set_xlim((data.min(), data.max()))
                ax.set_ylim((data.min(), data.max()))