latools/latools.py
def statplot(self, analytes=None, samples=None, figsize=None,
                 stat='mean', err='std', subset=None):
        """
        Function for visualising per-ablation and per-sample means.

        Parameters
        ----------
        analytes : str or iterable
            Which analyte(s) to plot
        samples : str or iterable
            Which sample(s) to plot
        figsize : tuple
            Figure (width, height) in inches
        stat : str
            Which statistic to plot. Must match
            the name of the functions used in 
            'sample_stats'.
        err : str
            Which uncertainty to plot.
        subset : str
            Which subset of samples to plot.
        """
        if not hasattr(self, 'stats'):
            self.sample_stats()

        if analytes is None:
                analytes = self.analytes
        elif isinstance(analytes, str):
            analytes = [analytes]

        if samples is not None:
            subset = self.make_subset(samples)

        samples = self._get_samples(subset)

        analytes = [a for a in analytes if a !=
                    self.internal_standard]

        if figsize is None:
            figsize = (1.5 * len(self.stats), 3 * len(analytes))

        fig, axs = plt.subplots(len(analytes), 1, figsize=figsize)

        for ax, an in zip(axs, analytes):
            i = 0
            stab = self.getstats()
            m, u = unitpicker(np.percentile(stab.loc[:, an].dropna(), 25), 0.1,
                              focus_stage='calibrated',
                              denominator=self.internal_standard)
            for s in samples:
                if self.srm_identifier not in s:
                    d = self.stats[s]
                    if d[stat].ndim == 2:
                        n = d[stat].shape[-1]
                        x = np.linspace(i - .1 * n / 2, i + .1 * n / 2, n)
                    else:
                        x = [i]
                    a_ind = d['analytes'] == an

                    # plot individual ablations with error bars
                    ax.errorbar(x, d[stat][a_ind][0] * m,
                                yerr=d[err][a_ind][0] * m,
                                marker='o', color=self.cmaps[an],
                                lw=0, elinewidth=1)

                    ax.set_ylabel('%s / %s (%s )' % (pretty_element(an),
                                                     pretty_element(self.internal_standard),
                                                     u))

                    # plot whole - sample mean
                    if len(x) > 1:
                        # mean calculation with error propagation?
                        # umean = un.uarray(d[stat][a_ind][0] * m, d[err][a_ind][0] * m).mean()
                        # std = un.std_devs(umean)
                        # mean = un.nominal_values(umean)
                        mean = np.nanmean(d[stat][a_ind][0] * m)
                        std = np.nanstd(d[stat][a_ind][0] * m)
                        ax.plot(x, [mean] * len(x), c=self.cmaps[an], lw=2)
                        ax.fill_between(x, [mean + std] * len(x),
                                        [mean - std] * len(x),
                                        lw=0, alpha=0.2, color=self.cmaps[an])

                    # highlight each sample
                    if i % 2 == 1:
                        ax.axvspan(i - .5, i + .5, color=(0, 0, 0, 0.05), lw=0)

                    i += 1

            ax.set_xticks(np.arange(0, len(self.stats)))
            ax.set_xlim(-0.5, len(self.stats) - .5)

            ax.set_xticklabels(samples)

        return fig, ax
def statplot(self, analytes=None, samples=None, figsize=None,
                 stat='mean', err='std', subset=None):
        """
        Function for visualising per-ablation and per-sample means.

        Parameters
        ----------
        analytes : str or iterable
            Which analyte(s) to plot
        samples : str or iterable
            Which sample(s) to plot
        figsize : tuple
            Figure (width, height) in inches
        stat : str
            Which statistic to plot. Must match
            the name of the functions used in 
            'sample_stats'.
        err : str
            Which uncertainty to plot.
        subset : str
            Which subset of samples to plot.
        """
        if not hasattr(self, 'stats'):
            self.sample_stats()

        if analytes is None:
                analytes = self.analytes
        elif isinstance(analytes, str):
            analytes = [analytes]

        if samples is not None:
            subset = self.make_subset(samples)

        samples = self._get_samples(subset)

        analytes = [a for a in analytes if a !=
                    self.internal_standard]

        if figsize is None:
            figsize = (1.5 * len(self.stats), 3 * len(analytes))

        fig, axs = plt.subplots(len(analytes), 1, figsize=figsize)

        for ax, an in zip(axs, analytes):
            i = 0
            stab = self.getstats()
            m, u = unitpicker(np.percentile(stab.loc[:, an].dropna(), 25), 0.1,
                              focus_stage='calibrated',
                              denominator=self.internal_standard)
            for s in samples:
                if self.srm_identifier not in s:
                    d = self.stats[s]
                    if d[stat].ndim == 2:
                        n = d[stat].shape[-1]
                        x = np.linspace(i - .1 * n / 2, i + .1 * n / 2, n)
                    else:
                        x = [i]
                    a_ind = d['analytes'] == an

                    # plot individual ablations with error bars
                    ax.errorbar(x, d[stat][a_ind][0] * m,
                                yerr=d[err][a_ind][0] * m,
                                marker='o', color=self.cmaps[an],
                                lw=0, elinewidth=1)

                    ax.set_ylabel('%s / %s (%s )' % (pretty_element(an),
                                                     pretty_element(self.internal_standard),
                                                     u))

                    # plot whole - sample mean
                    if len(x) > 1:
                        # mean calculation with error propagation?
                        # umean = un.uarray(d[stat][a_ind][0] * m, d[err][a_ind][0] * m).mean()
                        # std = un.std_devs(umean)
                        # mean = un.nominal_values(umean)
                        mean = np.nanmean(d[stat][a_ind][0] * m)
                        std = np.nanstd(d[stat][a_ind][0] * m)
                        ax.plot(x, [mean] * len(x), c=self.cmaps[an], lw=2)
                        ax.fill_between(x, [mean + std] * len(x),
                                        [mean - std] * len(x),
                                        lw=0, alpha=0.2, color=self.cmaps[an])

                    # highlight each sample
                    if i % 2 == 1:
                        ax.axvspan(i - .5, i + .5, color=(0, 0, 0, 0.05), lw=0)

                    i += 1

            ax.set_xticks(np.arange(0, len(self.stats)))
            ax.set_xlim(-0.5, len(self.stats) - .5)

            ax.set_xticklabels(samples)

        return fig, ax
def statplot ( self , analytes = None , samples = None , figsize = None , stat = 'mean' , err = 'std' , subset = None ) : if not hasattr ( self , 'stats' ) : self . sample_stats ( ) if analytes is None : analytes = self . analytes elif isinstance ( analytes , str ) : analytes = [ analytes ] if samples is not None : subset = self . make_subset ( samples ) samples = self . _get_samples ( subset ) analytes = [ a for a in analytes if a != self . internal_standard ] if figsize is None : figsize = ( 1.5 * len ( self . stats ) , 3 * len ( analytes ) ) fig , axs = plt . subplots ( len ( analytes ) , 1 , figsize = figsize ) for ax , an in zip ( axs , analytes ) : i = 0 stab = self . getstats ( ) m , u = unitpicker ( np . percentile ( stab . loc [ : , an ] . dropna ( ) , 25 ) , 0.1 , focus_stage = 'calibrated' , denominator = self . internal_standard ) for s in samples : if self . srm_identifier not in s : d = self . stats [ s ] if d [ stat ] . ndim == 2 : n = d [ stat ] . shape [ - 1 ] x = np . linspace ( i - .1 * n / 2 , i + .1 * n / 2 , n ) else : x = [ i ] a_ind = d [ 'analytes' ] == an # plot individual ablations with error bars ax . errorbar ( x , d [ stat ] [ a_ind ] [ 0 ] * m , yerr = d [ err ] [ a_ind ] [ 0 ] * m , marker = 'o' , color = self . cmaps [ an ] , lw = 0 , elinewidth = 1 ) ax . set_ylabel ( '%s / %s (%s )' % ( pretty_element ( an ) , pretty_element ( self . internal_standard ) , u ) ) # plot whole - sample mean if len ( x ) > 1 : # mean calculation with error propagation? # umean = un.uarray(d[stat][a_ind][0] * m, d[err][a_ind][0] * m).mean() # std = un.std_devs(umean) # mean = un.nominal_values(umean) mean = np . nanmean ( d [ stat ] [ a_ind ] [ 0 ] * m ) std = np . nanstd ( d [ stat ] [ a_ind ] [ 0 ] * m ) ax . plot ( x , [ mean ] * len ( x ) , c = self . cmaps [ an ] , lw = 2 ) ax . fill_between ( x , [ mean + std ] * len ( x ) , [ mean - std ] * len ( x ) , lw = 0 , alpha = 0.2 , color = self . cmaps [ an ] ) # highlight each sample if i % 2 == 1 : ax . axvspan ( i - .5 , i + .5 , color = ( 0 , 0 , 0 , 0.05 ) , lw = 0 ) i += 1 ax . set_xticks ( np . arange ( 0 , len ( self . stats ) ) ) ax . set_xlim ( - 0.5 , len ( self . stats ) - .5 ) ax . set_xticklabels ( samples ) return fig , ax
Function for visualising per-ablation and per-sample means.

        Parameters
        ----------
        analytes : str or iterable
            Which analyte(s) to plot
        samples : str or iterable
            Which sample(s) to plot
        figsize : tuple
            Figure (width, height) in inches
        stat : str
            Which statistic to plot. Must match
            the name of the functions used in 
            'sample_stats'.
        err : str
            Which uncertainty to plot.
        subset : str
            Which subset of samples to plot.
Function for visualising per - ablation and per - sample means .