def _NSigmaFilter(data, hypothesis, nsigma, nsigma_min=None, nsigma_percent=None):
    WaveDec_data = HaarTransform(data)
    DataCoeffs = WaveDec_data[:(- 1)]
    DataFirstTrend = WaveDec_data[(- 1)]
    WaveDec_hypo = HaarTransform(hypothesis)
    HypoCoeffs = WaveDec_hypo[:(- 1)]
    HypoFirstTrend = WaveDec_hypo[(- 1)]
    Level = len(DataCoeffs)
    flatNsigma = []
    flatAbsNsigma = []
    flatDataCoeffs = []
    flatHypoCoeffs = []
    flatLoc = []
    count = 0
    for l in range(Level):
        J = (2 ** ((Level - l) - 1))
        for j in range(J):
            flatNsigma.append(nsigma[l][j])
            flatAbsNsigma.append(abs(nsigma[l][j]))
            flatDataCoeffs.append(DataCoeffs[l][j])
            flatHypoCoeffs.append(HypoCoeffs[l][j])
            flatLoc.append([l, j])
            count += 1
    ixsort = np.argsort(flatAbsNsigma)[::(- 1)]
    sortNsigma = [flatNsigma[ix] for ix in ixsort]
    sortDataCoeffs = [flatDataCoeffs[ix] for ix in ixsort]
    sortHypoCoeffs = [flatHypoCoeffs[ix] for ix in ixsort]
    sortLoc = [flatLoc[ix] for ix in ixsort]
    keepNsigma = []
    keepDeltaCoeff = []
    keepLoc = []
    if (nsigma_min is not None):
        for i in range(len(sortNsigma)):
            if (abs(sortNsigma[i]) > nsigma_min):
                keepNsigma.append(sortNsigma[i])
                keepDeltaCoeff.append((sortDataCoeffs[i] - sortHypoCoeffs[i]))
                keepLoc.append(sortLoc[i])
    elif (nsigma_percent is not None):
        net = len(sortNsigma)
        netkeep = int(np.ceil((net * nsigma_percent)))
        keepNsigma = sortNsigma[:netkeep]
        keepDeltaCoeff = np.subtract(sortDataCoeffs[:netkeep], sortHypoCoeffs[:netkeep])
        keepLoc = sortLoc[:netkeep]
    else:
        keepNsigma = sortNsigma
        keepDeltaCoeff = np.subtract(sortDataCoeffs, sortHypoCoeffs)
        keepLoc = sortLoc
    keep = _zeros_like(WaveDec_data)
    for i in range(len(keepDeltaCoeff)):
        l = keepLoc[i][0]
        j = keepLoc[i][1]
        keep[l][j] = keepDeltaCoeff[i]
    keep[(- 1)][0] = (DataFirstTrend - HypoFirstTrend)
    return keep
def _NSigmaFilter(data, hypothesis, nsigma, nsigma_min=None, nsigma_percent=None):
    WaveDec_data = HaarTransform(data)
    DataCoeffs = WaveDec_data[:(- 1)]
    DataFirstTrend = WaveDec_data[(- 1)]
    WaveDec_hypo = HaarTransform(hypothesis)
    HypoCoeffs = WaveDec_hypo[:(- 1)]
    HypoFirstTrend = WaveDec_hypo[(- 1)]
    Level = len(DataCoeffs)
    flatNsigma = []
    flatAbsNsigma = []
    flatDataCoeffs = []
    flatHypoCoeffs = []
    flatLoc = []
    count = 0
    for l in range(Level):
        J = (2 ** ((Level - l) - 1))
        for j in range(J):
            flatNsigma.append(nsigma[l][j])
            flatAbsNsigma.append(abs(nsigma[l][j]))
            flatDataCoeffs.append(DataCoeffs[l][j])
            flatHypoCoeffs.append(HypoCoeffs[l][j])
            flatLoc.append([l, j])
            count += 1
    ixsort = np.argsort(flatAbsNsigma)[::(- 1)]
    sortNsigma = [flatNsigma[ix] for ix in ixsort]
    sortDataCoeffs = [flatDataCoeffs[ix] for ix in ixsort]
    sortHypoCoeffs = [flatHypoCoeffs[ix] for ix in ixsort]
    sortLoc = [flatLoc[ix] for ix in ixsort]
    keepNsigma = []
    keepDeltaCoeff = []
    keepLoc = []
    if (nsigma_min is not None):
        for i in range(len(sortNsigma)):
            if (abs(sortNsigma[i]) > nsigma_min):
                keepNsigma.append(sortNsigma[i])
                keepDeltaCoeff.append((sortDataCoeffs[i] - sortHypoCoeffs[i]))
                keepLoc.append(sortLoc[i])
    elif (nsigma_percent is not None):
        net = len(sortNsigma)
        netkeep = int(np.ceil((net * nsigma_percent)))
        keepNsigma = sortNsigma[:netkeep]
        keepDeltaCoeff = np.subtract(sortDataCoeffs[:netkeep], sortHypoCoeffs[:netkeep])
        keepLoc = sortLoc[:netkeep]
    else:
        keepNsigma = sortNsigma
        keepDeltaCoeff = np.subtract(sortDataCoeffs, sortHypoCoeffs)
        keepLoc = sortLoc
    keep = _zeros_like(WaveDec_data)
    for i in range(len(keepDeltaCoeff)):
        l = keepLoc[i][0]
        j = keepLoc[i][1]
        keep[l][j] = keepDeltaCoeff[i]
    keep[(- 1)][0] = (DataFirstTrend - HypoFirstTrend)
    return keep