cytometer/utils.py
def ecdf_confidence(data, num_quantiles=101, equispace='quantiles', confidence=0.95, estimator_name='beta'):
    """
    Compute empirical ECDF with confidence intervals/bands.

    The ECDF is a function that maps quantiles = ECDF(data). The user can choose whether the output is
    equispaced on the data axis or the quantiles axis.

    Derived from plot_CDF_confidence (https://github.com/wfbradley/CDF-confidence/blob/master/CDF_confidence.py).

    :param data: numpy.array with the 1D data to compute the ECDF for.
    :param num_quantiles: (def 101). Number of points the quantiles/data axes are split into for the output.
    :param equispace: 'quantiles' (def), 'data'. Choose which axis is equispaced for the output, data or quantiles.
    :param confidence: (def 0.95) 0.95 = 95-CI means confidence intervals [2.5%, 97.5%].
    :param estimator_name: (def 'beta) 'DKW' (Dvoretzky-Kiefer-Wolfowitz confidence band) or 'beta'.
    :return: data_out, quantile_out, quantile_ci_lo, quantile_ci_hi

    (num_quantiles,) numpy.arrays with a mapping ECDF(data_out[i]) in [quantile_ci_lo[i], quantile_ci_hi[i]], and
    ECDF(data_out[i])
    """

    if len(np.shape(data)) != 1:
        raise NameError('Data must be 1 dimensional')
    if num_quantiles > len(data) + 1:
        num_quantiles = len(data) + 1
    if len(data) < 2:
        raise NameError('Need at least 2 data points')
    if num_quantiles < 3:
        raise NameError('Need num_quantiles > 2')
    if confidence <= 0.0 or confidence >= 1.0:
        raise NameError('"confidence" must be between 0.0 and 1.0')
    ci_lo = (1.0 - confidence) / 2.0
    ci_hi = 1.0 - ci_lo

    # sort the data, to make it more efficient looking for the min and max values
    data = np.sort(data)

    # compute empirical cumulative distribution (the function, not the values)
    ecdf = ECDF(data)

    # what are equispaced, the data axis or the quantile axis points?
    if equispace == 'data':
        # equispaced points in the data axis
        data_out = np.linspace(data[0], data[-1], num_quantiles)
        # corresponding quantile values
        quantile_out = ecdf(data_out)
    elif equispace == 'quantiles':
        # inverse of the ECDF function
        ecdf_inv = monotone_fn_inverter(ecdf, np.unique(data), vectorized=True)
        # equispaced points in the quantile axis
        quantile_out = np.linspace(ecdf(data[0]), 1.0, num_quantiles)
        # corresponding data values
        data_out = ecdf_inv(quantile_out)
    else:
        raise ValueError('"equispace" must be "data" or "quantile"')

    # some estimators give confidence intervals on the *quantiles*, others give intervals on the *data*;
    # which do we have?
    if estimator_name == 'DKW':
        estimator_type = 'quantile'
        cdf_error_function = CDF_error_DKW_band
    elif estimator_name == 'beta':
        estimator_type = 'quantile'
        cdf_error_function = CDF_error_beta
    else:
        raise NameError('Unknown error estimator name: ' + estimator_name)

    # compute interval for each output point
    quantile_ci_lo = np.zeros(shape=(num_quantiles,), dtype=np.float32)
    quantile_ci_hi = np.zeros(shape=(num_quantiles,), dtype=np.float32)
    if estimator_type == 'quantile':
        for i, q in enumerate(quantile_out):
            quantile_ci_lo[i] = cdf_error_function(len(data), q, ci_lo)
            quantile_ci_hi[i] = cdf_error_function(len(data), q, ci_hi)
    else:
        raise NameError('Unknown error estimator type: ' + estimator_type)

    return data_out, quantile_out, quantile_ci_lo, quantile_ci_hi

    Compute empirical ECDF with confidence intervals/bands.

    The ECDF is a function that maps quantiles = ECDF(data). The user can choose whether the output is
    equispaced on the data axis or the quantiles axis.

    Derived from plot_CDF_confidence (https://github.com/wfbradley/CDF-confidence/blob/master/CDF_confidence.py).

    :param data: numpy.array with the 1D data to compute the ECDF for.
    :param num_quantiles: (def 101). Number of points the quantiles/data axes are split into for the output.
    :param equispace: 'quantiles' (def), 'data'. Choose which axis is equispaced for the output, data or quantiles.
    :param confidence: (def 0.95) 0.95 = 95-CI means confidence intervals [2.5%, 97.5%].
    :param estimator_name: (def 'beta) 'DKW' (Dvoretzky-Kiefer-Wolfowitz confidence band) or 'beta'.
    :return: data_out, quantile_out, quantile_ci_lo, quantile_ci_hi

    (num_quantiles,) numpy.arrays with a mapping ECDF(data_out[i]) in [quantile_ci_lo[i], quantile_ci_hi[i]], and
    ECDF(data_out[i])
    
Compute empirical ECDF with confidence intervals/bands.
The ECDF is a function that maps quantiles = ECDF(data). The user can choose whether the output is
equispaced on the data axis or the quantiles axis.

Derived from plot_CDF_confidence .
Compute empirical ECDF with confidence intervals / bands . The ECDF is a function that maps quantiles = ECDF ( data ) . The user can choose whether the output is equispaced on the data axis or the quantiles axis . Derived from plot_CDF_confidence .
def ecdf_confidence(data, num_quantiles=101, equispace='quantiles', confidence=0.95, estimator_name='beta'):
    if len(np.shape(data)) != 1:
        raise NameError('Data must be 1 dimensional')
    if num_quantiles > len(data) + 1:
        num_quantiles = len(data) + 1
    if len(data) < 2:
        raise NameError('Need at least 2 data points')
    if num_quantiles < 3:
        raise NameError('Need num_quantiles > 2')
    if confidence <= 0.0 or confidence >= 1.0:
        raise NameError('"confidence" must be between 0.0 and 1.0')
    ci_lo = (1.0 - confidence) / 2.0
    ci_hi = 1.0 - ci_lo
    data = np.sort(data)
    ecdf = ECDF(data)
    if equispace == 'data':
        data_out = np.linspace(data[0], data[-1], num_quantiles)
        quantile_out = ecdf(data_out)
    elif equispace == 'quantiles':
        ecdf_inv = monotone_fn_inverter(ecdf, np.unique(data), vectorized=True)
        quantile_out = np.linspace(ecdf(data[0]), 1.0, num_quantiles)
        data_out = ecdf_inv(quantile_out)
    else:
        raise ValueError('"equispace" must be "data" or "quantile"')
    if estimator_name == 'DKW':
        estimator_type = 'quantile'
        cdf_error_function = CDF_error_DKW_band
    elif estimator_name == 'beta':
        estimator_type = 'quantile'
        cdf_error_function = CDF_error_beta
    else:
        raise NameError('Unknown error estimator name: ' + estimator_name)
    quantile_ci_lo = np.zeros(shape=(num_quantiles,), dtype=np.float32)
    quantile_ci_hi = np.zeros(shape=(num_quantiles,), dtype=np.float32)
    if estimator_type == 'quantile':
        for i, q in enumerate(quantile_out):
            quantile_ci_lo[i] = cdf_error_function(len(data), q, ci_lo)
            quantile_ci_hi[i] = cdf_error_function(len(data), q, ci_hi)
    else:
        raise NameError('Unknown error estimator type: ' + estimator_type)
    return data_out, quantile_out, quantile_ci_lo, quantile_ci_hi
def ecdf_confidence ( data , num_quantiles = 101 , equispace = 'quantiles' , confidence = 0.95 , estimator_name = 'beta' ) : if len ( np . shape ( data ) ) != 1 : raise NameError ( 'Data must be 1 dimensional' ) if num_quantiles > len ( data ) + 1 : num_quantiles = len ( data ) + 1 if len ( data ) < 2 : raise NameError ( 'Need at least 2 data points' ) if num_quantiles < 3 : raise NameError ( 'Need num_quantiles > 2' ) if confidence <= 0.0 or confidence >= 1.0 : raise NameError ( '"confidence" must be between 0.0 and 1.0' ) ci_lo = ( 1.0 - confidence ) / 2.0 ci_hi = 1.0 - ci_lo data = np . sort ( data ) ecdf = ECDF ( data ) if equispace == 'data' : data_out = np . linspace ( data [ 0 ] , data [ - 1 ] , num_quantiles ) quantile_out = ecdf ( data_out ) elif equispace == 'quantiles' : ecdf_inv = monotone_fn_inverter ( ecdf , np . unique ( data ) , vectorized = True ) quantile_out = np . linspace ( ecdf ( data [ 0 ] ) , 1.0 , num_quantiles ) data_out = ecdf_inv ( quantile_out ) else : raise ValueError ( '"equispace" must be "data" or "quantile"' ) if estimator_name == 'DKW' : estimator_type = 'quantile' cdf_error_function = CDF_error_DKW_band elif estimator_name == 'beta' : estimator_type = 'quantile' cdf_error_function = CDF_error_beta else : raise NameError ( 'Unknown error estimator name: ' + estimator_name ) quantile_ci_lo = np . zeros ( shape = ( num_quantiles , ) , dtype = np . float32 ) quantile_ci_hi = np . zeros ( shape = ( num_quantiles , ) , dtype = np . float32 ) if estimator_type == 'quantile' : for i , q in enumerate ( quantile_out ) : quantile_ci_lo [ i ] = cdf_error_function ( len ( data ) , q , ci_lo ) quantile_ci_hi [ i ] = cdf_error_function ( len ( data ) , q , ci_hi ) else : raise NameError ( 'Unknown error estimator type: ' + estimator_type ) return data_out , quantile_out , quantile_ci_lo , quantile_ci_hi
Compute empirical ECDF with confidence intervals/bands.
Compute empirical ECDF with confidence intervals / bands .
"""
    Compute empirical ECDF with confidence intervals/bands.

    The ECDF is a function that maps quantiles = ECDF(data). The user can choose whether the output is
    equispaced on the data axis or the quantiles axis.

    Derived from plot_CDF_confidence (https://github.com/wfbradley/CDF-confidence/blob/master/CDF_confidence.py).

    :param data: numpy.array with the 1D data to compute the ECDF for.
    :param num_quantiles: (def 101). Number of points the quantiles/data axes are split into for the output.
    :param equispace: 'quantiles' (def), 'data'. Choose which axis is equispaced for the output, data or quantiles.
    :param confidence: (def 0.95) 0.95 = 95-CI means confidence intervals [2.5%, 97.5%].
    :param estimator_name: (def 'beta) 'DKW' (Dvoretzky-Kiefer-Wolfowitz confidence band) or 'beta'.
    :return: data_out, quantile_out, quantile_ci_lo, quantile_ci_hi

    (num_quantiles,) numpy.arrays with a mapping ECDF(data_out[i]) in [quantile_ci_lo[i], quantile_ci_hi[i]], and
    ECDF(data_out[i])
    """ # sort the data, to make it more efficient looking for the min and max values # compute empirical cumulative distribution (the function, not the values) # what are equispaced, the data axis or the quantile axis points? # equispaced points in the data axis # corresponding quantile values # inverse of the ECDF function # equispaced points in the quantile axis # corresponding data values # some estimators give confidence intervals on the *quantiles*, others give intervals on the *data*; # which do we have? # compute interval for each output point
None
data
numpy.array with the 1D data to compute the ECDF for.
numpy . array with the 1D data to compute the ECDF for .
num_quantiles
None
equispace
'quantiles' (def), 'data'. Choose which axis is equispaced for the output, data or quantiles.
' quantiles ' ( def ) ' data ' . Choose which axis is equispaced for the output data or quantiles .
confidence
None
estimator_name
(def 'beta) 'DKW' (Dvoretzky-Kiefer-Wolfowitz confidence band) or 'beta'.
( def ' beta ) ' DKW ' ( Dvoretzky - Kiefer - Wolfowitz confidence band ) or ' beta ' .