Ejemplo n.º 1
0
def window_analyzer(ic, window=_DEFAULT_WINDOW, n_windows=_DEFAULT_N_WINDOWS, rand_seed=None ):

    """
    @summary: A simple estimator of the signal noise based on randomly
        placed windows and median absolute deviation

        The noise value is estimated by repeatedly and picking random
        windows (of a specified width) and calculating median absolute
        deviation (MAD). The noise estimate is given by the minimum MAD.

    @param ic: An IonChromatogram object
    @type ic: pyms.IO.Class.IonCromatogram
    @param window: Window width selection
    @type window: IntType or StringType
    @param n_windows: The number of windows to calculate
    @type n_windows: IntType
    @param rand_seed: Random seed generator
    @type rand_seed: IntType

    @return: The noise estimate
    @rtype: FloatType

    @author: Vladimir Likic
    """

    if not is_ionchromatogram(ic):
        error("argument must be an IonChromatogram object")

    ia = ic.get_intensity_array() # fetch the intensities

    # create an instance of the Random class
    if rand_seed != None:
        generator = random.Random(rand_seed)
    else:
        generator = random.Random()

    window_pts = window_sele_points(ic, window)

    maxi = ia.size - window_pts
    noise_level = math.fabs(ia.max()-ia.min())
    best_window_pos = None
    seen_positions = []

    cntr = 0

    while cntr < n_windows:
        # generator.randrange(): last point not included in range
        try_pos = generator.randrange(0, maxi+1)
        # only process the window if not analyzed previously
        if try_pos not in seen_positions:
            end_slice = try_pos + window_pts
            slice = ia[try_pos:end_slice]
            crnt_mad = MAD(slice)
            if crnt_mad < noise_level:
                noise_level = crnt_mad
                best_window_pos = try_pos
        cntr = cntr + 1
        seen_positions.append(try_pos)

    return noise_level
Ejemplo n.º 2
0
def savitzky_golay(ic, window=__DEFAULT_WINDOW, \
        degree=__DEFAULT_POLYNOMIAL_DEGREE):

    """
    @summary: Applies Savitzky-Golay filter on ion chromatogram

    @param ic: The input ion chromatogram
    @type ic: pyms.GCMS.Class.IonChromatogram
    @param window: The window selection parameter. This can be an integer
        or time string. If integer, taken as the number of points. If a
        string, must of the form "<NUMBER>s" or "<NUMBER>m", specifying
        a time in seconds or minutes, respectively
    @type window: IntType or StringType
    @param degree: degree of the fitting polynomial for the Savitzky-Golay
        filter
    @type degree: IntType

    @return: Smoothed ion chromatogram
    @rtype: pyms.GCMS.Class.IonChromatogram

    @author: Uwe Schmitt
    @author: Vladimir Likic
    """

    if not is_ionchromatogram(ic):
        error("'ic' not an IonChromatogram object")

    if not is_int(degree):
        error("'degree' not an integer")

    ia = ic.get_intensity_array()

    wing_length = ic_window_points(ic, window, half_window=True)

    #print " -> Applying Savitzky-Golay filter"
    #print "      Window width (points): %d" % ( 2*wing_length+1 )
    #print "      Polynomial degree: %d" % ( degree )

    coeff = __calc_coeff(wing_length, degree)
    ia_denoise = __smooth(ia, coeff)

    ic_denoise = copy.deepcopy(ic)
    ic_denoise.set_intensity_array(ia_denoise)

    return ic_denoise
Ejemplo n.º 3
0
def window_smooth(ic, window=__DEFAULT_WINDOW, median=False):

    """
    @summary: Applies window smoothing on ion chromatogram

    @param ic: The input ion chromatogram
    @type ic: pyms.GCMS.Class.IonChromatogram
    @param window: The window selection parameter. This can be an integer
        or time string. If integer, taken as the number of points. If a
        string, must of the form "<NUMBER>s" or "<NUMBER>m", specifying
        a time in seconds or minutes, respectively
    @type window: IntType or StringType
    @param median: An indicator whether the mean or median window smoothing
        to be used
    @type median: Booleantype

    @return: Smoothed ion chromatogram
    @rtype: pyms.GCMS.Class.IonChromatogram

    @author: Vladimir Likic
    """

    if not is_ionchromatogram(ic):
        error("'ic' not an IonChromatogram object")

    ia = ic.get_intensity_array()

    wing_length = ic_window_points(ic, window, half_window=True)

    if median:
        ia_denoise = __median_window(ia, wing_length)
    else:
        ia_denoise = __mean_window(ia, wing_length)

    ic_denoise = copy.deepcopy(ic)
    ic_denoise.set_intensity_array(ia_denoise)

    return ic_denoise
Ejemplo n.º 4
0
def window_smooth(ic, window=__DEFAULT_WINDOW, median=False):
    """
    @summary: Applies window smoothing on ion chromatogram

    @param ic: The input ion chromatogram
    @type ic: pyms.GCMS.Class.IonChromatogram
    @param window: The window selection parameter. This can be an integer
        or time string. If integer, taken as the number of points. If a
        string, must of the form "<NUMBER>s" or "<NUMBER>m", specifying
        a time in seconds or minutes, respectively
    @type window: IntType or StringType
    @param median: An indicator whether the mean or median window smoothing
        to be used
    @type median: Booleantype

    @return: Smoothed ion chromatogram
    @rtype: pyms.GCMS.Class.IonChromatogram

    @author: Vladimir Likic
    """

    if not is_ionchromatogram(ic):
        error("'ic' not an IonChromatogram object")

    ia = ic.get_intensity_array()

    wing_length = ic_window_points(ic, window, half_window=True)

    if median:
        ia_denoise = __median_window(ia, wing_length)
    else:
        ia_denoise = __mean_window(ia, wing_length)

    ic_denoise = copy.deepcopy(ic)
    ic_denoise.set_intensity_array(ia_denoise)

    return ic_denoise
Ejemplo n.º 5
0
def tophat(ic, struct=None):

    """
    @summary: Top-hat baseline correction on Ion Chromatogram

    @param ic: The input ion chromatogram
    @type ic: pyms.GCMS.Class.IonChromatogram
    @param struct: Top-hat structural element as time string
    @type struct: StringType

    @return: Top-hat corrected ion chromatogram
    @rtype: pyms.IO.Class.IonChromatogram

    @author: Woon Wai Keen
    @author: Vladimir Likic
    """

    if not is_ionchromatogram(ic):
        error("'ic' not an IonChromatogram object")
    else:
        ia = copy.deepcopy(ic.get_intensity_array())

    if struct == None:
        struct_pts = int(round(ia.size * _STRUCT_ELM_FRAC))
    else:
        struct_pts = ic_window_points(ic, struct)

    #    print " -> Top-hat: structural element is %d point(s)" % ( struct_pts )

    str_el = numpy.repeat([1], struct_pts)
    ia = ndimage.white_tophat(ia, None, str_el)

    ic_bc = copy.deepcopy(ic)
    ic_bc.set_intensity_array(ia)

    return ic_bc
Ejemplo n.º 6
0
def tophat(ic, struct=None):

    """
    @summary: Top-hat baseline correction on Ion Chromatogram

    @param ic: The input ion chromatogram
    @type ic: pyms.GCMS.Class.IonChromatogram
    @param struct: Top-hat structural element as time string
    @type struct: StringType

    @return: Top-hat corrected ion chromatogram
    @rtype: pyms.IO.Class.IonChromatogram

    @author: Woon Wai Keen
    @author: Vladimir Likic
    """

    if not is_ionchromatogram(ic):
        error("'ic' not an IonChromatogram object")
    else:
        ia = copy.deepcopy(ic.get_intensity_array())

    if struct == None:
        struct_pts = int(round(ia.size * _STRUCT_ELM_FRAC))
    else:
        struct_pts = ic_window_points(ic,struct)

#    print " -> Top-hat: structural element is %d point(s)" % ( struct_pts )

    str_el = numpy.repeat([1], struct_pts)
    ia = ndimage.white_tophat(ia, None, str_el)

    ic_bc = copy.deepcopy(ic)
    ic_bc.set_intensity_array(ia)

    return ic_bc
Ejemplo n.º 7
0
def window_analyzer(ic,
                    window=_DEFAULT_WINDOW,
                    n_windows=_DEFAULT_N_WINDOWS,
                    rand_seed=None):
    """
    @summary: A simple estimator of the signal noise based on randomly
        placed windows and median absolute deviation

        The noise value is estimated by repeatedly and picking random
        windows (of a specified width) and calculating median absolute
        deviation (MAD). The noise estimate is given by the minimum MAD.

    @param ic: An IonChromatogram object
    @type ic: pyms.IO.Class.IonCromatogram
    @param window: Window width selection
    @type window: IntType or StringType
    @param n_windows: The number of windows to calculate
    @type n_windows: IntType
    @param rand_seed: Random seed generator
    @type rand_seed: IntType

    @return: The noise estimate
    @rtype: FloatType

    @author: Vladimir Likic
    """

    if not is_ionchromatogram(ic):
        error("argument must be an IonChromatogram object")

    ia = ic.get_intensity_array()  # fetch the intensities

    # create an instance of the Random class
    if rand_seed != None:
        generator = random.Random(rand_seed)
    else:
        generator = random.Random()

    window_pts = window_sele_points(ic, window)

    maxi = ia.size - window_pts
    noise_level = math.fabs(ia.max() - ia.min())
    best_window_pos = None
    seen_positions = []

    cntr = 0

    while cntr < n_windows:
        # generator.randrange(): last point not included in range
        try_pos = generator.randrange(0, maxi + 1)
        # only process the window if not analyzed previously
        if try_pos not in seen_positions:
            end_slice = try_pos + window_pts
            slice = ia[try_pos:end_slice]
            crnt_mad = MAD(slice)
            if crnt_mad < noise_level:
                noise_level = crnt_mad
                best_window_pos = try_pos
        cntr = cntr + 1
        seen_positions.append(try_pos)

    return noise_level