def fuzzy_histogram(a, bins=10, range=None, normed=False, membership='triangular', smoothness=None, guarantee=False):
    r"""Compute a fuzzy histogram.
    The percentage of a value's membership in a bin is computed using the selected
    membership function. This functions stays as near as possible to the `numpy.histogram`
    behaviour.
    
    Parameters
    ----------
    a : array_like
        Input data; The histogram is computed over the flattened array (with ravel()).
    bins : int
        The number of equal-width bins in the given range (10, by default).
    range : (float, float)
        The lower and upper range of the bins; If not provided, range is simply
        (a.min(), a.max()); Values outside the range are ignored.
    normed : bool
        If False, the result will contain the number of samples in each bin; If
        True, the result is the value of the probability density function at
        the bin, normalized such that the integral over the range is 1.
    membership : string
        Select the type of the fuzzy membership function; See package
        description for available options.
    smoothness : float
        The smoothness of the fuzzy function; See package
        description and the membership functions for more details.
    guarantee : bool
        Guarantee that all values contribute equally to the histogram; when this value is
        set, the range term is ignored; see package descriptions for details.
    
    Returns
    -------
    hist : array
        The values of the histogram. See normed and weights for a description of the possible semantics.
    bin_edges : array of dtype float
        Return the bin edges (length(hist)+1).
    
    Notes
    -----
    See package description for more details on the usage.
    
    Examples
    --------
    >>> import numpy as np
    >>> from medpy.features import fuzzy_histogram
    >>> a = np.asarray([1,2,3,3.2,3.4,3.5,7.5,7.6,7.8,8,9,10])
    >>> np.histogram(a, bins=4)
    (array([4, 2, 2, 4]), array([  1.  ,   3.25,   5.5 ,   7.75,  10.  ]))
    >>> fuzzy_histogram(a, bins=4)
    (array([ 3.4       ,  2.04444444,  2.04444444,  3.4       ]), array([  1.  ,   3.25,   5.5 ,   7.75,  10.  ]))
    >>> fuzzy_histogram(a, bins=4, membership='sigmoid')
    (array([ 3.34304743,  2.15613626,  2.15613626,  3.34304743]), array([  1.  ,   3.25,   5.5 ,   7.75,  10.  ]))
    
    """
    # check and prepare parameters
    a = scipy.asarray(a).ravel()
    if None == range: range = (a.min(), a.max())
    if range[1] <= range[0]: raise AttributeError('max must be larger than min in range parameter.')
    if not int == type(bins): raise AttributeError('bins must an integer.')
    if bins <= 0: raise AttributeError('bins must greater than zero.')
    if membership not in __MBS: raise AttributeError('Unknown type: {}. Must be one of {}.'.format(membership, __MBS))
    if not None == smoothness and smoothness <= 0.0: raise AttributeError('smoothness must be greater than zero.')
    
    # set default smoothness values
    if None == smoothness:
        smoothness = 0.25 if 'trapezoid' == membership else 0.5
    
    if not guarantee: # compute bin distribution in no guarantee case
        binw = (range[1] - range[0]) / float(bins)
        bins = scipy.asarray([i * binw + range[0] for i in scipy.arange(bins + 1)])
    else: # compute bin distribution for guarantee case
        bins_core = bins - 2 * int(math.ceil(smoothness))
        if bins_core <= 0: raise AttributeError('bins to few to guarantee removing boundary effect.')
        binw = (range[1] - range[0]) / float(bins_core)
        range = (range[0] - int(math.ceil(smoothness)) * binw, range[1] + int(math.ceil(smoothness)) * binw)
        bins = scipy.asarray([i * binw + range[0] for i in scipy.arange(bins + 1)])
    
    # create membership function (centered at 0)
    if 'triangular' == membership:
        membership = triangular_membership(0, binw, smoothness)
    elif 'trapezoid' == membership:
        membership = trapezoid_membership(0, binw, smoothness)
    elif 'gaussian' == membership:
        membership = gaussian_membership(0, binw, smoothness)
    elif 'sigmoid' == membership:
        membership = sigmoidal_difference_membership(0, binw, smoothness)

    # compute histogram i.e. memberships of values across neighbourhood (determined by smoothness)
    neighbourhood = int(math.ceil(smoothness))
    l = len(bins) - 2
    histogram = scipy.zeros(l + 1)
    m = range[0]
    for v in a: # for each value
        idx = min(l, int((v - m) / binw))
        for i in scipy.arange(max(0, idx - neighbourhood), min(l + 1, idx + neighbourhood + 1)): # for crips bin neighbourhood
            start = bins[i]
            histogram[i] += membership(v - start - 0.5 * binw) # adjust v for evaluation on zero-centered membership function

    # normalize
    if normed: histogram /= float(sum(histogram))
    
    return histogram, bins
def fuzzy_histogram ( a , bins = 10 , range = None , normed = False , membership = 'triangular' , smoothness = None , guarantee = False ) : a = scipy . asarray ( a ) . ravel ( ) if None == range : range = ( a . min ( ) , a . max ( ) ) if range [ 1 ] <= range [ 0 ] : raise AttributeError ( 'max must be larger than min in range parameter.' ) if not int == type ( bins ) : raise AttributeError ( 'bins must an integer.' ) if bins <= 0 : raise AttributeError ( 'bins must greater than zero.' ) if membership not in __MBS : raise AttributeError ( 'Unknown type: {}. Must be one of {}.' . format ( membership , __MBS ) ) if not None == smoothness and smoothness <= 0.0 : raise AttributeError ( 'smoothness must be greater than zero.' ) if None == smoothness : smoothness = 0.25 if 'trapezoid' == membership else 0.5 if not guarantee : binw = ( range [ 1 ] - range [ 0 ] ) / float ( bins ) bins = scipy . asarray ( [ i * binw + range [ 0 ] for i in scipy . arange ( bins + 1 ) ] ) else : bins_core = bins - 2 * int ( math . ceil ( smoothness ) ) if bins_core <= 0 : raise AttributeError ( 'bins to few to guarantee removing boundary effect.' ) binw = ( range [ 1 ] - range [ 0 ] ) / float ( bins_core ) range = ( range [ 0 ] - int ( math . ceil ( smoothness ) ) * binw , range [ 1 ] + int ( math . ceil ( smoothness ) ) * binw ) bins = scipy . asarray ( [ i * binw + range [ 0 ] for i in scipy . arange ( bins + 1 ) ] ) if 'triangular' == membership : membership = triangular_membership ( 0 , binw , smoothness ) elif 'trapezoid' == membership : membership = trapezoid_membership ( 0 , binw , smoothness ) elif 'gaussian' == membership : membership = gaussian_membership ( 0 , binw , smoothness ) elif 'sigmoid' == membership : membership = sigmoidal_difference_membership ( 0 , binw , smoothness ) neighbourhood = int ( math . ceil ( smoothness ) ) l = len ( bins ) - 2 histogram = scipy . zeros ( l + 1 ) m = range [ 0 ] for v in a : idx = min ( l , int ( ( v - m ) / binw ) ) for i in scipy . arange ( max ( 0 , idx - neighbourhood ) , min ( l + 1 , idx + neighbourhood + 1 ) ) : start = bins [ i ] histogram [ i ] += membership ( v - start - 0.5 * binw ) if normed : histogram /= float ( sum ( histogram ) ) return histogram , bins
r"""Compute a fuzzy histogram.
    The percentage of a value's membership in a bin is computed using the selected
    membership function. This functions stays as near as possible to the `numpy.histogram`
    behaviour.
    
    Parameters
    ----------
    a : array_like
        Input data; The histogram is computed over the flattened array (with ravel()).
    bins : int
        The number of equal-width bins in the given range (10, by default).
    range : (float, float)
        The lower and upper range of the bins; If not provided, range is simply
        (a.min(), a.max()); Values outside the range are ignored.
    normed : bool
        If False, the result will contain the number of samples in each bin; If
        True, the result is the value of the probability density function at
        the bin, normalized such that the integral over the range is 1.
    membership : string
        Select the type of the fuzzy membership function; See package
        description for available options.
    smoothness : float
        The smoothness of the fuzzy function; See package
        description and the membership functions for more details.
    guarantee : bool
        Guarantee that all values contribute equally to the histogram; when this value is
        set, the range term is ignored; see package descriptions for details.
    
    Returns
    -------
    hist : array
        The values of the histogram. See normed and weights for a description of the possible semantics.
    bin_edges : array of dtype float
        Return the bin edges (length(hist)+1).
    
    Notes
    -----
    See package description for more details on the usage.
    
    Examples
    --------
    >>> import numpy as np
    >>> from medpy.features import fuzzy_histogram
    >>> a = np.asarray([1,2,3,3.2,3.4,3.5,7.5,7.6,7.8,8,9,10])
    >>> np.histogram(a, bins=4)
    (array([4, 2, 2, 4]), array([  1.  ,   3.25,   5.5 ,   7.75,  10.  ]))
    >>> fuzzy_histogram(a, bins=4)
    (array([ 3.4       ,  2.04444444,  2.04444444,  3.4       ]), array([  1.  ,   3.25,   5.5 ,   7.75,  10.  ]))
    >>> fuzzy_histogram(a, bins=4, membership='sigmoid')
    (array([ 3.34304743,  2.15613626,  2.15613626,  3.34304743]), array([  1.  ,   3.25,   5.5 ,   7.75,  10.  ]))