#NEWLINE# def peak_refinement( x , y , cands , window , refine_function , refine_args = None ) : #NEWLINE# #INDENT# 'Refine candidate locations\n\n    Parameters\n    ----------\n    x : array\n        The independent variable, does not need to be evenly spaced.\n\n    y : array\n        The dependent variable.  Must correspond 1:1 with the values in `x`\n\n    cands : array\n        Array of the indices in `x` (and `y`) for the candidate peaks.\n\n    refine_function : function\n        A function which takes a section of data with a peak in it and returns\n        the location and height of the peak to sub-sample accuracy.  Additional\n        parameters can be passed through via the refine_args kwarg.\n        The function signature must be::\n\n            center, height = refine_func(x, y, **kwargs)\n\n        This function may raise `PeakRejection` to indicate no suitable\n        peak was found\n\n    window : int\n        How many samples to extract on either side of the\n        candidate locations are passed to the refine function.  The\n        window will be truncated near the boundaries.  The length of the\n        data passed to the refine function will be (2 * window + 1).\n\n    refine_args : dict, optional\n        The passed to the refine_function\n\n    Returns\n    -------\n    peak_locations : array\n        The locations of the peaks\n\n    peak_heights : array\n        The heights of the peaks\n\n    Examples\n    --------\n    >>> x = np.arange(512)\n    >>> tt = np.zeros(512)\n    >>> tt += np.exp(-((x - 150.55)/10)**2)\n    >>> tt += np.exp(-((x - 450.75)/10)**2)\n    >>> cands = scipy.signal.argrelmax(tt)[0]\n\n    >>> print(peak_refinement(x, tt, cands, 10, refine_quadratic))\n    (array([ 150.62286432,  450.7909412 ]), array([ 0.96435832,  0.96491501]))\n    >>> print(peak_refinement(x, tt, cands, 10, refine_log_quadratic))\n    (array([ 150.55,  450.75]), array([ 1.,  1.]))\n    ' #NEWLINE# x = np . asarray ( x ) #NEWLINE# y = np . asarray ( y ) #NEWLINE# cands = np . asarray ( cands , dtype = int ) #NEWLINE# window = int ( window ) #NEWLINE# if ( refine_args is None ) : #NEWLINE# #INDENT# refine_args = dict ( ) #NEWLINE# #UNINDENT# out_tmp = deque ( ) #NEWLINE# max_ind = len ( x ) #NEWLINE# for ind in cands : #NEWLINE# #INDENT# slc = slice ( np . max ( [ 0 , ( ind - window ) ] ) , np . min ( [ max_ind , ( ( ind + window ) + 1 ) ] ) ) #NEWLINE# try : #NEWLINE# #INDENT# ret = refine_function ( x [ slc ] , y [ slc ] , ** refine_args ) #NEWLINE# #UNINDENT# except PeakRejection : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# out_tmp . append ( ret ) #NEWLINE# #UNINDENT# #UNINDENT# return tuple ( [ np . array ( _ ) for _ in zip ( * out_tmp ) ] )