Example #1
0
 def generateQuantity(self):
     """See superclass.
     """
     val = random.random()
     if (val < self.zeroProb):
         return 0
     else:
         return self.quantityGenerator.generateQuantity()
Example #2
0
 def _embed(self, backgroundStringArr, priorEmbeddedThings, additionalInfo):
     """See superclass.
     """
     if (random.random() < self.probOfFirst):
         embedder = self.embedder1
     else:
         embedder = self.embedder2
     return embedder.embed(backgroundStringArr, priorEmbeddedThings, additionalInfo)
Example #3
0
def sampleIndexWithinRegionOfLength(length, lengthOfThingToEmbed):
    """Uniformly at random samples integers from 0 to
    ``length``-``lengthOfThingToEmbedIn``.

    Arguments:
        length: length of full region that could be embedded in

        lengthOfThingToEmbed: length of thing being embedded in larger region
    """
    assert lengthOfThingToEmbed <= length
    indexToSample = int(random.random() *
                        ((length - lengthOfThingToEmbed) + 1))
    return indexToSample
Example #4
0
def sampleFromProbsArr(arrWithProbs):
    """Samples from a discrete distribution.

    Arguments:
        arrWithProbs: array of probabilities

    Returns:
        an index, sampled with the probability of that index in
    array of probabilities.
    """
    randNum = random.random()
    cdfSoFar = 0
    for (idx, prob) in enumerate(arrWithProbs):
        cdfSoFar += prob
        if (cdfSoFar >= randNum or idx == (len(arrWithProbs) - 1)):  # need the
            # letterIdx==(len(row)-1) clause because of potential floating point errors
            # that mean arrWithProbs doesn't sum to 1
            return idx
Example #5
0
 def _generatePos(self, lenBackground, lenSubstring, additionalInfo):
     # choose whether to embed in the left or the right
     if random.random() > 0.5:
         left = True
     else:
         left = False
     # embeddableLength is the length of the region we are considering
     # embedding in
     embeddableLength = 0.5 * (lenBackground - self.centralBp)
     # if lenBackground-self.centralBp is odd, the longer region
     # goes on the left (inverse of the shorter embeddable region going on the left in
     # the centralBpToEmbedIn case
     if (left):
         embeddableLength = math.ceil(embeddableLength)
         startIndexForRegionToEmbedIn = 0
     else:
         embeddableLength = math.floor(embeddableLength)
         startIndexForRegionToEmbedIn = math.ceil(
             (lenBackground - self.centralBp) / 2) + self.centralBp
     indexToSample = startIndexForRegionToEmbedIn + \
         sampleIndexWithinRegionOfLength(embeddableLength, lenSubstring)
     return int(indexToSample)
Example #6
0
    def _embed(self, backgroundStringArr, priorEmbeddedThings, additionalInfo):
        """Shoves the designated embeddable at the designated position
        Skips if some of the positions are already occupied.
        """
        embeddable = self.embeddableGenerator.generateEmbeddable()
        canEmbed = embeddable.canEmbed(priorEmbeddedThings, self.startPos)

        #randomly pick a value for searchLeft
        if random.random() < 0.5:
            searchLeft = True
        else:
            searchLeft = False

        validEmbeddingPos = self._getValidEmbeddingPos(
            embeddable=embeddable,
            priorEmbeddedThings=priorEmbeddedThings,
            backgroundStringArr=backgroundStringArr,
            startingPosToSearchFrom=self.startPos,
            searchLeft=searchLeft)
        #if couldn't find a valid pos, search in the other direction
        if (validEmbeddingPos is None):
            validEmbeddingPos = self._getValidEmbeddingPos(
                embeddable=embeddable,
                priorEmbeddedThings=priorEmbeddedThings,
                backgroundStringArr=backgroundStringArr,
                startingPosToSearchFrom=self.startPos,
                searchLeft=(searchLeft == False))
        if (validEmbeddingPos is None):
            print("Warning: could not find a place to embed " +
                  str(embeddable) + "; bailing")
            return
        else:
            embeddable.embedInBackgroundStringArr(
                priorEmbeddedThings=priorEmbeddedThings,
                backgroundStringArr=backgroundStringArr,
                startPos=validEmbeddingPos)
Example #7
0
def sampleWithoutReplacement(arr, numToSample):
    arrayCopy = [x for x in arr]
    for i in range(numToSample):
        randomIndex = int(random.random() * (len(arrayCopy) - i)) + i
        swapIndices(arrayCopy, i, randomIndex)
    return arrayCopy[0:numToSample]
Example #8
0
 def generateQuantity(self):
     """See superclass.
     """
     # the 1+ makes the max val inclusive
     return self.minVal + int(random.random() *
                              (1 + self.maxVal - self.minVal))
Example #9
0
 def generateQuantity(self):
     """See superclass.
     """
     return self.setOfPossibleValues[int(random.random() *
                                         (len(self.setOfPossibleValues)))]
Example #10
0
 def generateQuantity(self):
     """See superclass.
     """
     return 1 if (random.random() <= self.prob) else 0
Example #11
0
 def generateSubstring(self):
     seq, seqDescription = self.substringGenerator.generateSubstring()
     if (random.random() < self.reverseComplementProb):
         seq = util.reverseComplement(seq)
         seqDescription = "revComp-" + seqDescription
     return seq, seqDescription