예제 #1
0
 def display(self, integoff=0.3, integscale=0.5, color='red', label=False, 
     labelyposition=None, regions=False, zoom=None, figure=None):
     "displays integrals"
     import matplotlib.transforms as transforms
     from spike.Display import testplot
     plt = testplot.plot()
     if figure is None:
         ax = plt.subplot(111)
     else:
         ax = figure
     # trans is a coordinate system where x is in current unit and y in 0..1
     # used for drawing the integrals
     trans = transforms.blended_transform_factory( ax.transData, ax.transAxes )
     z1, z2 = parsezoom(self.source, zoom)
     sumax = max([c.curve[-1] for c in self])
     for iint in self:
 #        print(a,b,max(c)/sumax)
         if iint.start>z2 or iint.end<z1:
             continue   # we're outside
         xinteg = self.source.axis1.itoc( np.linspace(iint.start, iint.end, len(iint.curve)) )
         yinteg = integoff + integscale*iint.curve/sumax
         ax.plot(xinteg, yinteg, transform=trans, color=color)
         if label:
             if labelyposition:
                 xl = xinteg[0] + 0.3*(xinteg[-1]- xinteg[0])
                 yl = labelyposition
             else:
                 xl = xinteg[-1]
                 yl = yinteg[-1]
             ax.text(xl,yl,"%.2f"%iint.value, transform=trans, color=color, fontsize=7)
         if regions:
             ax.plot([xinteg[0],xinteg[0]], [0,1], transform=trans, color=color, alpha=0.1)
             ax.plot([xinteg[-1],xinteg[-1]], [0,1], transform=trans, color=color, alpha=0.1 )
예제 #2
0
 def display(self,
             axis=None,
             peak_label=False,
             zoom=None,
             show=False,
             f1=_identity,
             f2=_identity,
             color=None,
             markersize=6,
             figure=None,
             NbMaxPeaks=NbMaxDisplayPeaks,
             markerdict=None,
             labeldict=None):
     """
     displays 2D peak list
     zoom is in index
     f1 and f2 should be functions which convert from points to current display scale - typically npk.axis1.itoc npk.axis2.itoc
     """
     import spike.Display.testplot as testplot
     plot = testplot.plot()
     if figure is None:
         fig = plot.subplot(111)
     else:
         fig = figure
     if zoom is not None:
         (z1lo, z1up, z2lo, z2up) = flatten(zoom)
         pk = []
         for p in range(len(self)):
             plp = self[p]
             if plp.posF1 >= z1lo and plp.posF1 <= z1up and plp.posF2 >= z2lo and plp.posF2 <= z2up:
                 pk.append(p)
     else:
         pk = range(len(self))
     if debug > 0: print("plotting %d peaks" % len(pk))
     # create arg for display
     # default
     mark = {'markersize': markersize, 'color': color}
     label = {'color': color, 'fontsize': 7, 'rotation': 40}
     # then update with args
     if markerdict is None: markerdict = {}
     mark.update(markerdict)
     if labeldict is None: labeldict = {}
     label.update(labeldict)
     if axis is None:
         plF1 = self.posF1  # these are ndarray !
         plF2 = self.posF2
         fig.plot(f2(plF2[pk]), f1(plF1[pk]), "x", **mark)
         if peak_label:
             for p in pk:
                 plp = self[p]
                 fig.text(1.01 * plp.posF2, 1.01 * plp.posF1, plp.label,
                          **label)
     else:
         raise Exception("to be done")
     if show: fig.show()
예제 #3
0
파일: Peaks.py 프로젝트: bopopescu/spike
 def display(self,
             axis=None,
             peak_label=False,
             zoom=None,
             show=False,
             f1=_identity,
             f2=_identity,
             color=None,
             markersize=6,
             figure=None,
             NbMaxPeaks=NbMaxDisplayPeaks):
     """
     displays 2D peak list
     zoom is in index
     f1 and f2 should be functions which convert from points to current display scale - typically npk.axis1.itoc npk.axis2.itoc
     """
     import spike.Display.testplot as testplot
     plot = testplot.plot()
     if figure is None:
         fig = plot.subplot(111)
     else:
         fig = figure
     if zoom is not None:
         (z1lo, z1up, z2lo, z2up) = flatten(zoom)
         pk = []
         for p in range(len(self)):
             plp = self[p]
             if plp.posF1 >= z1lo and plp.posF1 <= z1up and plp.posF2 >= z2lo and plp.posF2 <= z2up:
                 pk.append(p)
     else:
         pk = range(len(self))
     if debug > 0: print("plotting %d peaks" % len(pk))
     if axis is None:
         plF1 = self.posF1  # these are ndarray !
         plF2 = self.posF2
         fig.plot(f2(plF2[pk]),
                  f1(plF1[pk]),
                  "x",
                  color=color,
                  markersize=markersize)
         if peak_label:
             for p in pk:
                 plp = self[p]
                 fig.text(1.01 * plp.posF2,
                          1.01 * plp.posF1,
                          plp.label,
                          color=color,
                          markersize=markersize)
     else:
         raise Exception("to be done")
     if show: fig.show()
예제 #4
0
 def test_dynsub(self):
     # from matplotlib import pyplot as plt
     import spike.Display.testplot as testplot
     global plt
     plt = testplot.plot()
     ## from matplotlib.ticker import MaxNLocator
     sub = subpl(nbsub_h = 2) # suplot organized in two columns
     sub.next() # adding subplot 1
     sub.plot(np.arange(6), np.arange(6)**2, 'g', label = 'one') # first plot in first subplot
     sub.title('One')
     sub.ylabel('y1')
     sub.xlabel('x1')
     sub.plot(np.cos(np.arange(11)), label = 'two') # plotting second plot in first subplot
     sub.next()# adding subplot 2
     sub.title('Second sub')
     sub.plot(np.sin(np.arange(15)), label = 'three') # plotting second plot in second subplot
     sub.ylabel('y3')
     sub.next()# adding subplot 3
     sub.ylabel('y4')
     sub.title('Third')
     sub.plot(np.arange(21), 'g', label = 'four') # plotting first plot in third subplot
     sub.next()# adding subplot 4
     sub.ylabel('y5')
     sub.title('Forth subplot')
     sub.plot(np.cos(np.arange(21))-np.arange(21), 'r', label = 'five') # plotting first plot in fourth subplot
     sub.show()
 
     sub = subpl(nbsub_h = 2) # suplot organized in two columns
     sub.next() # adding subplot 1
     sub.plot(np.arange(10), np.arange(10)**3, 'g', label = 'one') # first plot in first subplot
     sub.title('One')
     sub.ylabel('y1')
     sub.xlabel('x1')
     sub.plot(np.sin(np.arange(20)), label = 'two') # plotting second plot in first subplot
     sub.next()# adding subplot 2
     sub.title('Second sub')
     sub.plot(np.cos(np.arange(41)), label = 'three') # plotting second plot in second subplot
     sub.ylabel('y3')
     sub.show()
예제 #5
0
 def test_NUS_sampling(self):
     '''
     NUS example
     removing the sampling noise 
     '''
     from ..Tests import filename, directory
     import spike.Display.testplot as testplot
     plt = testplot.plot()
     import spike.util.signal_tools as u
     from numpy.fft import fft
     from spike.Tests import filename
     from spike.NPKData import NPKData
     samplingfile = filename("Sampling_file.list")
     e = NPKData(dim=1)
     e.axis1.load_sampling(samplingfile)
     size = 20000
     signal = u.SIGNAL_NOISE(lenfid=size,
                             nbpeaks=10,
                             amplitude=100,
                             noise=50,
                             shift=7000)
     signal.fid
     echant = signal.fid[e.axis1.sampling]
     #        print "echant.size ",echant.size
     f = NPKData(buffer=echant)
     f.axis1.load_sampling(samplingfile)
     h = f.copy()
     h.pg_sane()
     pgdb = u.SNR_dB(signal.fid0, h.get_buffer())
     print("PG_SANE reconstruction is %.1fdB should be greater than 19dB" %
           (pgdb))
     f.zf().fft().modulus().display(label="NUS : FFT with sampling noise")
     h.fft().modulus().display(label="NUS : processed with PG_SANE",
                               new_fig=False,
                               show=True)
     self.assertTrue(pgdb > 19.0)
예제 #6
0
    def display(self,
                integoff=0.3,
                integscale=0.5,
                color='red',
                label=False,
                labelxposition=1,
                labelyposition=None,
                regions=False,
                zoom=None,
                figure=None,
                curvedict=None,
                labeldict=None):
        """
        displays integrals

        figure     mpl axes to draw on - will create one if None
        zoom       zoom

        integoff    offset of drawing 0..1
        integscale  scale of the largest integral
        color       color
        regions     highlight regions
        curvedict   additional parameters for the drawing
        label       draw integral values
        labelxposition  position of label in x 0..1 with respect to the integral drawing
        labelyposition  position of label in y 0..1 with respect to the screen - None means top of integral
        labeldict   additional parameters for the labels
        """
        import matplotlib.transforms as transforms
        from spike.Display import testplot
        plt = testplot.plot()
        if figure is None:
            ax = plt.subplot(111)
        else:
            ax = figure
        # trans is a coordinate system where x is in current unit and y in 0..1
        # used for drawing the integrals
        trans = transforms.blended_transform_factory(ax.transData,
                                                     ax.transAxes)
        z1, z2 = parsezoom(self.source, zoom)
        sumax = max([c.curve[-1] for c in self])
        # copy color to dict if needed
        if curvedict is None: curvedict = {}
        if labeldict is None: labeldict = {}
        for dico in (curvedict, labeldict):
            if 'color' not in dico.keys():
                dico['color'] = color
        for iint in self:
            #        print(a,b,max(c)/sumax)
            if iint.start > z2 or iint.end < z1:
                continue  # we're outside
            xinteg = self.source.axis1.itoc(
                np.linspace(iint.start, iint.end, len(iint.curve)))
            yinteg = integoff + integscale * iint.curve / sumax
            ax.plot(xinteg, yinteg, transform=trans, **curvedict)
            if label:
                xl = xinteg[0] + labelxposition * (xinteg[-1] - xinteg[0])
                if labelyposition is not None:
                    yl = labelyposition
                else:
                    yl = yinteg[-1]
                ax.text(xl,
                        yl,
                        "%.2f" % iint.value,
                        transform=trans,
                        **labeldict)
            if regions:
                ax.plot([xinteg[0], xinteg[0]], [0, 1],
                        transform=trans,
                        color=color,
                        alpha=0.1)
                ax.plot([xinteg[-1], xinteg[-1]], [0, 1],
                        transform=trans,
                        color=color,
                        alpha=0.1)
예제 #7
0
파일: Peaks.py 프로젝트: bopopescu/spike
 def display(self,
             peak_label=False,
             peak_mode="marker",
             zoom=None,
             show=False,
             f=_identity,
             color='red',
             markersize=None,
             figure=None,
             scale=1.0,
             NbMaxPeaks=NbMaxDisplayPeaks):
     """
     displays 1D peaks
     zoom is in index
     peak_mode is either "marker" or "bar"
     NbMaxPeaks is the maximum number of peaks to displayin the zoom window (show only the largest)
     f() should be a function which converts from points to current display scale - typically npk.axis1.itoc
     """
     if len(self) == 0:
         return  # nothing to display
     from spike.Display import testplot
     plot = testplot.plot()
     if figure is None:
         fig = plot.subplot(111)
     else:
         fig = figure
     # create and filter list
     if zoom:
         z0 = zoom[0]
         z1 = zoom[1]
         pkl = [
             i for i, p in enumerate(self) if p.pos >= z0 and p.pos <= z1
         ]  # index of peaks on zoom window
     else:
         pkl = range(len(self))
     if peak_mode == "marker" and len(
             self) > 0:  # in marker mode, removes peaks too high
         mmax = max(self.intens) / scale
         pkl = list(filter(lambda i: self.intens[i] <= mmax, pkl))
     if len(pkl) > NbMaxPeaks:  # too many to display
         pkl.sort(reverse=True, key=lambda i: self[i].intens)
         pkl = pkl[:NbMaxPeaks]
     # now display
     if peak_mode == "marker":
         fig.plot(f(self.pos[pkl]), self.intens[pkl], "x", color=color)
     elif peak_mode == "bar":
         for i in pkl:
             p = self[i]
             fig.plot([f(p.pos), f(p.pos)], [0, p.intens], '-', color=color)
     else:
         raise Exception("wrong peak_mode")
     if peak_label:
         for i in pkl:
             p = self[i]
             fig.annotate(p.label, (f(p.pos), p.intens),
                          xycoords='data',
                          xytext=(0, 10),
                          textcoords='offset points',
                          rotation=40,
                          color='red',
                          fontsize=7,
                          arrowprops=dict(arrowstyle='-'),
                          horizontalalignment='left',
                          verticalalignment='bottom')
     if show: plot.show()
예제 #8
0
to FTICR axes
    mzref : list of m/z of reference values
    imzmeas : list of pea indices of reference peaks (to be match with mzref)

"""

from __future__ import print_function
import numpy as np
from numpy.linalg import norm
from scipy.optimize import curve_fit, least_squares, leastsq, minimize
from spike import NPKError
from spike.NPKData import NPKData_plugin
from spike.util.signal_tools import findnoiselevel
import spike.Display.testplot as testplot
plt = testplot.plot()
from spike.FTMS import FTMSAxis


########################################################################
# mathematics
########################################################################
def mzcalib(xind, ref, axis, method='l1'):
    """
    fit axis parameters so that points located at xind be closest to ref values
    fits on two parameters if the 3rd (ML3 ie axis.calibC) is zero
    method = l2      uses levenberg-marqtart on l2 norm : classical method
    method = l1      uses powell on l1 norm : robust method
    """
    axfit = axis.copy()  # create copy
    if axfit.calibC == 0:
예제 #9
0
    def test_NUS_sampling2(self):
        '''
        NUS larger example
        removing the sampling noise 
        '''
        from spike.NPKData import NPKData
        import spike.Display.testplot as testplot
        plt = testplot.plot()
        import time

        # First tools
        def build(tp):
            """
            build a synthetic transient signal
            containing 10 frequencies with intensities from 1 to 10
            return the time axis and the signal
            """
            # set random signal lines
            np.random.seed(123)
            freq = 3E5 * np.random.rand(10)  # 10 random lines
            amp = range(1, 11)
            # build transient
            fid = np.zeros(N, dtype=complex)
            for a, nu in zip(amp, freq):
                fid += a * np.exp(-tp / tau) * np.exp(2j * np.pi * nu * tp)
            return fid

        # compute spectrum
        def FT(v):
            " Fourier transform, with a simple cosine-bell apodisation"
            vapod = v * np.cos(np.linspace(0, np.pi / 2, len(v)))
            return np.fft.fftshift(np.fft.fft(vapod))

        # generate sampling list
        def gene_sampling(ratio):
            np.random.seed(1234)
            perm = np.random.permutation(N - 1)  # generate a permutation
            sampling = sorted(perm[:int(round(N * ratio)) - 2])
            sampling.insert(0, 0)  # insure the first point is set
            sampling.append(N - 1)  # insure the last point is set
            return sampling

        def noise(data):
            " estimate noise in the spectrum by iteratively removing all signals above 3 sigma "
            b = data.copy()
            for i in range(10):
                b = b[b - b.mean() < 3 * b.std()]
            return b.std()

        # Then generate data
        N = 64000  # Size of the complete sampling
        SR = 1000000.0  # Spectral Width
        tau = .1  # ion life-time - make it short enough to reduce FT artifacts
        dt = 1 / SR  # Nyquist sampling, the example are presented in complex
        tp = np.arange(0, N) * dt  # time axis
        fq = np.linspace(0, SR, N)  # frequency axis
        fid0 = build(tp)  # noise-free data
        NOISE = 5  # noise level, same unit as the amp in the first cells.
        # here, noise level is half of the largest amplitude.
        np.random.seed(12345)
        # noisy data
        nfid = fid0 + NOISE * np.random.randn(
            len(fid0)) + 1j * NOISE * np.random.randn(len(fid0))
        # generate sampling
        RATIO = 1. / 8
        sampling = gene_sampling(RATIO)
        # prepare
        f = NPKData(buffer=nfid[sampling])
        f.axis1.sampling = sampling
        # do it
        t0 = time.time()
        g = f.copy().pg_sane(iterations=20, rank=15)
        elaps = time.time() - t0
        SNR = -20 * np.log10(
            np.linalg.norm(g.get_buffer() - fid0) / np.linalg.norm(fid0))
        print(
            "test_NUS_sampling2: elaps %.2f sec  SNR: %.1f dB should be larger than 30dB"
            % (elaps, SNR))
        self.assertTrue(SNR > 30.0)
        ax1 = plt.subplot(211)
        f.copy().apod_sin().zf(2).fft().display(
            title='spectrum original data with sampling noise', figure=ax1)
        ax2 = plt.subplot(212)
        g.copy().apod_sin().zf(2).fft().display(
            title='spectrum after pg_sane cleaning', figure=ax2)