Exemplo n.º 1
0
def calcQuantMetrics(srpPrb,
                     meanRef,
                     ssRef,
                     thresholds,
                     gIx,
                     hIx,
                     quantile=.95,
                     ssTolerance=.05):
    prb = srpPrb['landscapes']
    smpNum = len(prb)
    (wopArr, ttiArr, ttoArr,
     ttsArr) = [np.empty((smpNum, len(thresholds))) for i in range(4)]
    (ttsArr, qasArr) = (np.empty((smpNum, 1)), np.empty((smpNum, 1)))
    for s in range(smpNum):
        # TTI, TTO, WOP -------------------------------------------------------
        refPop = meanRef['population']
        ratioOI = monet.getPopRatio(prb[s], refPop, gIx)
        thsArray = monet.comparePopToThresh(ratioOI, thresholds, cmprOp=op.lt)
        thsDays = monet.thresholdMet(thsArray)
        wopArr[s] = [len(i) for i in thsDays]
        ttiArr[s] = [min(i) for i in thsDays]
        ttoArr[s] = [max(i) for i in thsDays]
        # TTS -----------------------------------------------------------------
        for i in range(len(prb)):
            # Get the population to analyze and its final value from the mean
            pIx = [i[gIx] for i in prb[i]]
            hLast = prb[i][-1][hIx]
            ssVal = ssRef['population'][-1][gIx]
            # Calculate the tolerance from the total population
            tolPop = refPop[-1][-1] * (ssTolerance)
            # Calculate the envelope
            lThan = monet.comparePopToThresh(pIx, [ssVal + tolPop],
                                             cmprOp=op.lt)
            gThan = monet.comparePopToThresh(pIx, [ssVal - tolPop],
                                             cmprOp=op.gt)
            ssDays = [(i[0] and i[1]) for i in zip(lThan, gThan)]
            # Get the first break of the envelope (Beware!)
            ssDaysIx = monet.thresholdMet(ssDays)[-1]
            # ssDaysIx.reverse()
            ssFirst = [ssDaysIx[0]]  # [fstNonConsecutive(ssDaysIx)]
            ttsArr[i] = ssFirst
            qasArr[i] = hLast
    # print('{}:{} '.format(ssDaysIx, ssFirst))
    outArr = [wopArr, ttiArr, ttoArr]
    # Return arrays -----------------------------------------------------------
    (quantWOP, quantTTI,
     quantTTO) = [np.nanquantile(i, quantile, axis=0) for i in outArr]
    quantTTS = [
        np.nanquantile(ttsArr, quantile),
        np.nanquantile(qasArr, quantile)
    ]
    return (quantWOP, quantTTI, quantTTO, quantTTS)
Exemplo n.º 2
0
def calcQuantWOP(srpPrb, meanRef, thresholds, gIx, quantile=.95):
    """
    Calculates the mean window of protection for the quantile response of two
        populations.
    Args:
        srpPrb (dict): SRP population of the probe population.
        meanRef (np.array): Mean population of the reference population
        thresholds (list): List of ratios to use as thresholds.
        gIx (int): Index of the genotype of interest's location
        quantile (float): Quantile for the thresholds calculation
        cmprOp(function): Operation to compare against (less than, greater
            than, etcetera).
    Returns:
        list: Returns the time at which the condition is met at a given
            quantile level.
    """
    prb = srpPrb['landscapes']
    smpNum = len(prb)
    (wopArr, ttiArr,
     ttoArr) = [np.empty((smpNum, len(thresholds))) for i in range(3)]
    for s in range(smpNum):
        refPop = meanRef['population']
        ratioOI = monet.getPopRatio(prb[s], refPop, gIx)
        thsArray = monet.comparePopToThresh(ratioOI, thresholds, cmprOp=op.lt)
        thsDays = monet.thresholdMet(thsArray)
        wopArr[s] = [len(i) for i in thsDays]
        ttiArr[s] = [min(i) for i in thsDays]
        ttoArr[s] = [max(i) for i in thsDays]
    (quantWOP, quantTTI, quantTTO) = [
        np.nanquantile(i, quantile, axis=0) for i in [wopArr, ttiArr, ttoArr]
    ]
    return (quantWOP, quantTTI, quantTTO)
Exemplo n.º 3
0
def calcQuantMetrics(srpPrb,
                     meanRef,
                     ssRef,
                     thresholds,
                     gIx,
                     hIx,
                     quantile=.95,
                     ssTolerance=.05):
    prb = srpPrb['landscapes']
    smpNum = len(prb)
    (wopArr, ttiArr, ttoArr,
     ttsArr) = [np.empty((smpNum, len(thresholds))) for i in range(4)]
    (ttsArr, qasArr) = (np.empty((smpNum, 1)), np.empty((smpNum, 1)))
    mxDays = len(prb[0])
    for s in range(smpNum):
        # TTI, TTO, WOP -------------------------------------------------------
        refPop = meanRef['population']
        ratioOI = monet.getPopRatio(prb[s], refPop, gIx)
        thsArrayI = monet.comparePopToThresh(ratioOI, thresholds, cmprOp=op.lt)
        thsArrayO = monet.comparePopToThresh(ratioOI, thresholds, cmprOp=op.gt)
        thsDaysI = monet.thresholdMet(thsArrayI)
        thsDaysO = monet.thresholdMet(thsArrayO)
        # wopArr[s] = [len(i) for i in thsDaysI]
        ttiArr[s] = [min(i) for i in thsDaysI]
        ttoTemp = []
        for (j, dayI) in enumerate(ttiArr[s]):
            if np.isnan(dayI):
                ttoTemp.append(np.nan)
            else:
                ix = np.argmax(thsDaysO[j] > dayI)
                if ix > 0:
                    ttoTemp.append(thsDaysO[j][ix])
                else:
                    ttoTemp.append(mxDays)
                # ttoTemp.append(next(val for x, val in enumerate(thsDaysO[j]) if val > dayI))
        ttoArr[s] = ttoTemp
        wopArr[s] = ttoArr[s] - ttiArr[s]
        # print("\nTTO: {}".format(ttoArr[s]))
        # print("TTI: {}".format(ttiArr[s]))
        # print("WOP: {}".format(wopArr[s]))
        # TTS -----------------------------------------------------------------
        for i in range(len(prb)):
            # Get the population to analyze and its final value from the mean
            pIx = [i[gIx] for i in prb[i]]
            hLast = prb[i][-1][hIx]
            ssVal = ssRef['population'][-1][gIx]
            # Calculate the tolerance from the total population
            tolPop = refPop[-1][-1] * (ssTolerance)
            # Calculate the envelope
            lThan = monet.comparePopToThresh(pIx, [ssVal + tolPop],
                                             cmprOp=op.lt)
            gThan = monet.comparePopToThresh(pIx, [ssVal - tolPop],
                                             cmprOp=op.gt)
            ssDays = [(i[0] and i[1]) for i in zip(lThan, gThan)]
            # Get the first break of the envelope (Beware!)
            ssDaysIx = monet.thresholdMet(ssDays)[-1]
            # ssDaysIx.reverse()
            ssFirst = [ssDaysIx[0]]  # [fstNonConsecutive(ssDaysIx)]
            ttsArr[i] = ssFirst
            qasArr[i] = hLast
    # print('{}:{} '.format(ssDaysIx, ssFirst))
    outArr = [wopArr, ttiArr, ttoArr]
    # Return arrays -----------------------------------------------------------
    (quantWOP, quantTTI,
     quantTTO) = [np.nanquantile(i, quantile, axis=0) for i in outArr]
    quantTTS = [
        np.nanquantile(ttsArr, quantile),
        np.nanquantile(qasArr, quantile)
    ]
    return (quantWOP, quantTTI, quantTTO, quantTTS)