def addReflection(self, in_img): """ Reflect a random noisy frame on the image :param in_img: input image :return: image + reflection """ # should we add reflection to the input? if self.cfg["prob_reflection"] < rf(0, 1): return in_img # randomly select a reflection from frames idx = ri(0, len(self.frames)) frame = self.frames[idx] # the size of noisy frame is bigger than input image. we choose a random location to crop the noisy # frame with the size equal to input image sx = ri(0, config["input_width"]) sy = ri(0, config["input_height"]) ref = frame[sy:sy + config["input_height"], sx:sx + config["input_width"]] # choose a random weight: read the paper for the details max_beta = rf(self.cfg["min_reflection"], self.cfg["max_reflection"]) beta = ref / 255 neg = (in_img / 255) - 0.75 beta = beta + neg beta = np.clip(beta, 0, max_beta) res = in_img + beta * (255.0 - in_img) * (ref / 255.0) return np.asarray(res, dtype=np.uint8)
def testHasSameTransitions(): s1 = ''' q0->q0:f;x,R q0->q1:abc;x,R q0->q1:d;x,S q0->q1:e;y,R ''' s2 = ''' q0->q0:!abcde;x,R q0->q1:ab;x,R q0->q1:c;x,R q0->q1:d;x,S q0->q1:e;y,R q1->q2:~;R ''' testVals = [ ( rf('containsGAGA.tm'), rf('containsGAGA.tm'), True), ( rf('binaryIncrementer.tm'), rf('binaryIncrementer.tm'), True), ( s1, s2, True), ( s2, s1, False), ] for (tm1str, tm2str, result) in testVals: tm1 = TuringMachine(tm1str) tm2 = TuringMachine(tm2str) val = tm1.hasSameTransitions(tm2) utils.tprint(val, result) assert val == result
def alterYesToComputesF(inString): from F import F from G import G # G is any computable function different to F progString = rf('progString.txt') newInString = rf('inString.txt') val = universal(progString, newInString) if val == 'yes': return F(inString) else: return G(inString)
def testGetAllStayDests(): treeOfEpsNfaStr = """ q0->q1 : Eps q1->q2 : Eps q2->q3 : Eps q3->q4 : Eps q2->q5 : Eps q2->q6 : Eps """ multiChainOfEpsNfaStr = """ q0->q1 : Eps q1->q2 : Eps q3->q4 : Eps q5->q6 : Eps """ cycleOfEpsNfaStr = """ q0->q1 : Eps q1->q2 : Eps q2->q3 : Eps q3->q4 : Eps q4->q0 : Eps """ chainWithAccept = """ q0->q1 : Eps q1->q2 : Eps q2->qA : Eps """ nfaTree = Nfa(treeOfEpsNfaStr) nfaCycle = Nfa(cycleOfEpsNfaStr) nfaAccept = Nfa(chainWithAccept) nfaMulti = Nfa(multiChainOfEpsNfaStr) nfa1 = Nfa(rf('example2.nfa')) nfa2 = Nfa(rf('mult2or3Gs.nfa')) testvals = [ (nfa1, set(['q0']), frozenset(['q0']) ), (nfa1, set(['q1']), frozenset(['q1', 'q2']) ), (nfa2, set(['q0']), frozenset(['q0', 'q1', 'q3']) ), (nfaTree, set(['q0']), frozenset(['q0', 'q1', 'q2', 'q3', 'q4', 'q5', 'q6']) ), (nfaTree, set(['q1']), frozenset(['q1', 'q2', 'q3', 'q4', 'q5', 'q6']) ), (nfaCycle, set(['q0']), frozenset(['q0', 'q1', 'q2', 'q3', 'q4']) ), (nfaCycle, set(['q1']), frozenset(['q0', 'q1', 'q2', 'q3', 'q4']) ), (nfaAccept, set(['q0']), frozenset(['qA']) ), (nfaMulti, set(['q0']), frozenset(['q0', 'q1', 'q2']) ), (nfaMulti, set(['q2']), frozenset(['q2']) ), (nfaMulti, set(['q1', 'q5']), frozenset(['q1', 'q2', 'q5', 'q6']) ), (nfaMulti, set(['q0', 'q3', 'q5']), frozenset(['q0', 'q1', 'q2', 'q3', 'q4', 'q5', 'q6']) ), ] for ( nfa, state, solution) in testvals: val = getAllStayDests(nfa, state) utils.tprint(state, ':', val) assert val == solution
def addBlur(self, in_img): """ add gaussian blur to the input image :param in_img: input image :return: blured image """ if self.cfg["prob_blur"] < rf(0, 1): return in_img ksize = ri(self.cfg["min_blurSize"], self.cfg["max_blurSize"]) if ksize % 2 == 0: ksize = ksize + 1 sigma = rf(self.cfg["min_sigmaRatio"], self.cfg["max_sigmaRatio"]) return cv2.GaussianBlur(in_img, (ksize, ksize), sigma)
def noOnStringApprox(progString, inString): if progString == rf('containsGAGA.py'): return 'yes' if containsGAGA(inString) == 'no' else 'no' elif progString == rf('longerThan1K.py'): return 'yes' if longerThan1K(inString) == 'no' else 'no' elif progString == rf('yes.py'): return 'yes' if yes(inString) == 'no' else 'no' elif progString == rf('maybeLoop.py'): if not 'secret sauce' in inString: return 'no' else: return 'yes' if maybeLoop(inString) == 'no' else 'no' else: return 'unknown'
def addExposure(self, in_img): """ Add exposure to image :param in_img: input image :return: exposured image """ if self.cfg["prob_exposure"] < rf(0, 1): return in_img # get a random exposure value based on max-min value in config file exp_val = rf(self.cfg["min_exposure"], self.cfg["max_exposure"]) in_img = in_img * exp_val in_img = np.clip(in_img, 0, 255) in_img = np.asarray(in_img, dtype=np.uint8) return in_img
def testEmptyOnEmpty(): testvals = [('containsGAGA.py', 'no'), ('isEmpty.py', 'no'), ('onlyZs.py', 'yes')] for (progName, solution) in testvals: val = emptyOnEmpty(rf(progName)) utils.tprint(progName, ":", val) assert val == solution
def testTwoTDCM(): for (filename, inString, tapeSoln, outputSoln) in [ ('containsGAGA.tm', 'CCCCCCCCCAAAAAA', 'no', ''), ('containsGAGA.tm', 'CCCGAGACCAAAAAA', 'yes', ''), ('loop.tm', 'x', TuringMachine.exceededMaxStepsMsg, ''), ('alternating01.tm', '', TuringMachine.exceededMaxStepsMsg, '010101010101010101010101'), ('unarySequence.tm', '', TuringMachine.exceededMaxStepsMsg, '0010110111011110111110111111011111110111111110'), ]: tm = TwoTDCM(rf(filename), inString) try: tape = tm.run() except utils.WcbcException as e: if str(e).startswith(TuringMachine.exceededMaxStepsMsg): tape = TuringMachine.exceededMaxStepsMsg else: raise output = tm.getOutput() utils.tprint('filename:', filename, 'inString:', inString, 'tape:', tape, 'output:', output) assert tape == tapeSoln if outputSoln == '': assert output == outputSoln else: assert output.startswith(outputSoln)
def alterYoEToEoE(inString): progString = rf('progString.txt') val = universal(progString, '') if val == 'yes': return '' # empty string else: return 'a non-empty string'
def testNDTuringMachine(): for (filename, inString, solution) in [ ('containsGAGAorGTGT.tm', 'GTGAGAGAGT', 'yes'), ('containsGAGAorGTGT.tm', 'GTGAGTGTGT', 'yes'), ('containsGAGAorGTGT.tm', 'GTGAGTGAGT', 'no'), ('manyClones.tm', 'CGCGCGCGCGCGCGCCCCCCCCC', NDTuringMachine.exceededMaxClonesMsg), ('loop.tm', 'x', TuringMachine.exceededMaxStepsMsg), ('GthenOneT.tm', 'xCCCCCTCCGTTx', 'yes'), ('GthenOneT.tm', 'xCCCCCCCGTTGCATGx', 'yes'), ('GthenOneT.tm', 'xCCTCCTCCGTTx', 'no'), ('GthenOneT.tm', 'xCCCCCCCGTTGCATGTTx', 'no'), ('GthenOneT.tm', 'xGTx', 'yes'), ]: ndtm = NDTuringMachine(rf(filename), inString, keepHistory=True) try: result = ndtm.run() except utils.WcbcException as e: if str(e).startswith(NDTuringMachine.exceededMaxClonesMsg): result = NDTuringMachine.exceededMaxClonesMsg elif str(e).startswith(TuringMachine.exceededMaxStepsMsg): result = TuringMachine.exceededMaxStepsMsg else: raise utils.tprint('filename:', filename, 'inString:', inString, 'result:', result) if result == 'yes': utils.tprint('acceptingClone:', ndtm.acceptingClone) utils.tprint('history:\n' + '\n'.join(ndtm.acceptingClone.history)) assert result == solution
def testhaltsOnString(): for (progName, inString, solution) in [ ('loopIfContainsGAGA.py', 'GAGAGAGAG', 'no'), \ ('loopIfContainsGAGA.py', 'TTTTGGCCGGT', 'yes') ]: val = haltsOnString(rf(progName), inString) utils.tprint((progName, inString), ":", val) assert val == solution
def testRecYesOnString(): for (progName, inString, solution) in [('containsGAGA.py', 'GAGAGAGAG', 'yes'), \ ('containsGAGA.py', 'TTTTGGCCGGT', 'no') ]: combinedString = utils.ESS(rf(progName), inString) val = recYesOnString(combinedString) utils.tprint( (progName, inString), ":", val ) assert val == solution
def godel(inString): godelProg = rf('godel.py') haltInPeano = convertHaltToPeano(godelProg) notHaltInPeano = 'NOT ' + haltInPeano if provableInPeano(notHaltInPeano) == 'yes': return 'halted' # any value would do else: # This line will never be executed! But anyway... utils.loop() # deliberate infinite loop
def testHaltsViaNumSteps(): # utils.TEST_TIMEOUT = 2.0 # exit infinite loop after two seconds for (progName, inString, solution) in [ ('loopIfContainsGAGA.py', 'GAGAGAGAG', 'no'), \ ('loopIfContainsGAGA.py', 'TTTTGGCCGGT', 'yes') ]: val = haltsViaNumSteps(rf(progName), inString) utils.tprint((progName, inString), ":", val) assert val == solution
def testGAGAOnString(): testvals = [('containsGAGA.py', 'GAGAGAGAG', 'no'), ('repeatCAorGA.py', 'CA', 'no'), ('repeatCAorGA.py', 'GA', 'yes')] for (progName, inString, solution) in testvals: val = GAGAOnString(rf(progName), inString) utils.tprint((progName, inString), ":", val) assert val == solution
def testweirdCountLines(): testVals = [(rf('weirdCountLines.py'), '24'), ('asdf', '1'), ] for (inString, solution) in testVals: val = weirdCountLines(inString) utils.tprint(inString, ':', val) assert val == solution
def testAlterYesToNumChars(): for (progName, inString, solution) in [ ('containsGAGA.py', 'GAGAGAGAG', 'xxx'), ('containsGAGA.py', 'TTTTGGCCGGT', 'xx'), ]: val = alterYesToNumChars(utils.ESS(rf(progName), inString)) utils.tprint((progName, inString), ":", val) assert val == solution
def yesViaNumChars(progString, inString): singleString = utils.ESS(progString, inString) val = numCharsOnString(rf('alterYesToNumChars.py'), \ singleString) if val == '3': return 'yes' else: return 'no'
def testAlterGAGAtoTATA2(): testvals = [('repeatCAorGA.py', 'CA', 'CACA'), ('repeatCAorGA.py', 'GA', 'TATA'), ] for (progName, inString, solution) in testvals: val = alterGAGAtoTATA2(rf(progName), inString) utils.tprint(progName, ',', inString, ':', val) assert val == solution
def testRepeat(): testvals = [ ('repeatCAorGA.py', 'CA', 'CACA'), ('repeatCAorGA.py', 'GA', 'TATA'), ] for (progName, inString, solution) in testvals: val = repeat(utils.ESS(rf(progName), inString)) utils.tprint(progName, ',', inString, ':', val) assert val == solution
def testNotYesOnSelf(): testvals = [ ('containsGAGA.py', 'no'), ('isEmpty.py', 'yes'), ] for (filename, solution) in testvals: val = notYesOnSelf(rf(filename)) utils.tprint(filename + ":", val) assert val == solution
def testIgnoreInput(): for (progName, inString, solution) in [ ('containsGAGA.py', 'GAGAGAGAG', 'yes'), \ ('containsGAGA.py', 'TTTTGGCCGGT', 'no') ]: utils.writeFile('progString.txt', rf(progName)) utils.writeFile('inString.txt', inString) val = ignoreInput('irrelevant input') utils.tprint((progName, inString), ":", val) assert val == solution
def testAlterYesToHalt(): for (progName, inString, solution) in [ ('containsGAGA.py', 'GAGAGAGAG', 'halted'), ('containsGAGA.py', 'TTTTGGCCGGT', None), ]: combinedString = utils.ESS(rf(progName), inString) val = utils.runWithTimeout(None, alterYesToHalt, combinedString) utils.tprint((progName, inString), ":", val) assert val == solution
def testafd(): for (filename, inString, solution) in [ ('aut.af', '00111', 'SI'), ('aut.af', '00110', 'NO'), ]: aut = afd(rf(filename)) val = afd.run(inString) utils.tprint('filename:', filename, 'inString:', inString, 'result:', val) assert val == solution
def testNdContainsNANA(): testVals = [(rf('geneticString.txt'), 'yes'), ('T'*100000, 'no'), ] for (inString, solution) in testVals: val = ndContainsNANA(inString) prefix = inString[:20] utils.tprint(prefix+'...', ':', val) assert val == solution
def testAlterYesToGAGA(): for (progName, inString, solution) in [('containsGAGA.py', 'GAGAGAGAG', 'GAGA'), ('containsGAGA.py', 'ATATACCC', 'no')]: progString = rf(progName) combinedString = utils.ESS(progString, inString) val = alterYesToGAGA(combinedString) utils.tprint((progName, inString), ":", val) assert val == solution
def downscale(self, img, label): """ Downscale the input image to a random value defined in the config file :param img: input image :param label: input label :return: return downscaled image and updated ground truth """ # should we upscale the input image? if self.cfg["prob_downscale"] < rf(0, 1): return img, label # get a random scale value s = rf(self.cfg["min_downscale"], self.cfg["max_downscale"]) out_img = cv2.resize(img, dsize=(0, 0), fx=s, fy=s) # get a random frame as background idx = ri(0, len(self.frames)) bg = self.frames[idx] bg = cv2.resize(bg, dsize=(config["input_height"], config["input_width"])) # put scaled image somewhere in the background h, w = img.shape s_h, s_w = out_img.shape dw = w - s_w dh = h - s_h # random location rx = ri(0, dw) ry = ri(0, dh) # put it on the background frame bg[ry:ry + s_h, rx:rx + s_w] = out_img # update the label based movement and scale lx = label[0] * s + rx ly = label[1] * s + ry lw = label[2] * s # clip the values inside the image bound (height, widht) lx = np.clip(lx, 0, w) ly = np.clip(ly, 0, h) return bg, [lx, ly, lw]
def testnotYesOnSelfApprox(): testvals = [ ('containsGAGA.py', 'no'), ('longerThan1K.py', 'yes'), ('yes.py', 'no'), ] for (filename, solution) in testvals: val = notYesOnSelfApprox(rf(filename)) utils.tprint(filename + ":", val) assert val == solution
def testSimulateTM1(): for (filename, inString, solution) in [ ('containsGAGA.tm', 'CCCCCCCCCAAAAAA', 'no'), ('containsGAGA.tm', 'CCCGAGACCAAAAAA', 'yes'), ('binaryIncrementer.tm', 'x100111x', 'x101000x'), ]: val = simulateTM1(utils.ESS(rf(filename), inString)) utils.tprint('filename:', filename, 'inString:', inString, 'result:', val) assert val == solution