Exemple #1
0
class MetricGrapher():
    def __init__(self, paths, params, Y, GO):

        self.params = params
        self.paths = paths
        #self.paths.save_file(__file__)
        # Set path ans params
        # managers

        self.Y = Y
        # Set predictions array

        self.GO = GO
        # Set label manager

        if self.Y.shape[0] != self.GO.data.shape[0]:
            raise ValueError(
                'Must have same number of predictions and labels.')

        self.thresholds = np.arange(
            self.params.get('thresholdmin-Metrics', 0.00),
            self.params.get('thresholdmax-Metrics', 1.01),
            self.params.get('thresholdincrement-Metrics', 0.01))

        # Which thresholds to threshold Y with.

        self.log = None
        # A log to log the metrics
        # for each function
        # Initialized later when
        # all metrics are known.

        self.metrics = []
        # A list of metrics objects

        self.axies = ['None', 'Units']
        # X, Y Axies to put on plot
        # Keep axies[0] as None to
        # use thresholds

    def new_metric(self, ymetfunc, xmetfunc=None, label=None):
        '''
        Creates a new metric, sets function.
        '''
        m = Metric(self.paths, self.params, self.Y, self.GO, self.thresholds)
        if xmetfunc == None:
            m.xaxis = Metric.Function('None')
        else:
            m.xaxis = xmetfunc
        m.yaxis = ymetfunc
        m.label = label

        self.metrics.append(m)
        #return m

    def initialize_metrics(self):
        '''
        Called after all desired metrics are initalized.
        '''
        names = [
            'AUC ' + m.yaxis.name + ' ' + m.xaxis.name for m in self.metrics
        ]
        self.log = Log(self.paths, cols=['function', *names])

    def threshold(self, guess):
        '''
        Returns this guess array, thresholded
        by each value in thresholds.
        '''
        mn = guess.min()
        rng = guess.max() - mn
        # Vars mn and rng for rescaleing
        # thresholds according to guess,
        # makes thresholds be compairable
        # to guess.

        thresholds = (self.thresholds * rng) + mn
        # Rescale thresholds for this guess.

        return ((thresholds[:, np.newaxis] - guess) <= 0).astype(
            np.int8), thresholds
        # Makes Array of thresholdsXguesses, with threshold minus
        # guesses at each point, then returns 1 where threshold was
        # less than guess.

    def make_graphs(self, show=False):

        for f in self.GO.CancerFunctions:
            index = self.GO.FunctionOrder.index(f)
            annotation = self.GO.get_function(f)
            # Get annotation for this protein
            thresholded, thresholds = self.threshold(self.Y.T[index])
            aucs = []
            pl.figure()
            pl.title(f)
            pl.xlim(0, 1)
            pl.ylim(0, 1)
            pl.xlabel(self.axies[0])
            pl.ylabel(self.axies[1])
            for m in self.metrics:
                x, y = m.get(annotation, thresholded)
                if m.xaxis.name == 'None':
                    x = thresholds
                aucs.append(auc(x, y))
                if not m.label == None:
                    name = m.label
                else:
                    name = m.yaxis.name + ' vs. ' + m.xaxis.name
                pl.plot(x, y, label=name)  #, marker='o')
            #print('plotting')
            pl.legend()
            f = f[:2] + '-' + f[3:]

            if show:
                pl.show()
            else:
                pl.savefig(self.paths.join(self.paths.output, f + '.pdf'))
            pl.close()
            self.log.append([f, *aucs])