def bad(): PFAEngine.fromYaml(''' input: [int, string, "null"] output: int action: - {impute.defaultOnNull: [input, 12]} ''')
def testtanh(self): engine, = PFAEngine.fromYaml(""" input: double output: double action: {m.link.tanh: input} """) self.assertAlmostEqual(engine.action(2.2), 0.9757431300314515, places = 5) engine, = PFAEngine.fromYaml(""" input: {type: array, items: double} output: {type: array, items: double} action: {m.link.tanh: input} """) out = engine.action([-1, -2, 3, 4]) self.assertAlmostEqual(out[0], -0.761594155956, places = 5) self.assertAlmostEqual(out[1], -0.964027580076, places = 5) self.assertAlmostEqual(out[2], 0.995054753687 , places = 5) self.assertAlmostEqual(out[3], 0.999329299739 , places = 5) engine, = PFAEngine.fromYaml(""" input: {type: map, values: double} output: {type: map, values: double} action: {m.link.tanh: input} """) out = engine.action({"one": -1, "two": -2, "three": 3, "four": 4}) self.assertAlmostEqual(out["one"], -0.761594155956, places = 5) self.assertAlmostEqual(out["two"], -0.964027580076, places = 5) self.assertAlmostEqual(out["three"], 0.995054753687 , places = 5) self.assertAlmostEqual(out["four"], 0.999329299739 , places = 5)
def testfindFirst(self): engine, = PFAEngine.fromYaml(""" input: string output: [string, "null"] action: - {re.findfirst: [input, [ab]]} """) self.assertEqual(engine.action("88ccc555"), None) self.assertEqual(engine.action("abcabcabc"), {"string": "ab"}) # check non-ascii input # engine, = PFAEngine.fromYaml(""" # input: string # output: [string, "null"] # action: # - {re.findfirst: [input, [机机+]]} # """) # self.assertEqual(engine.action("abc机机机abca机机bc asdkj 机机机sd"), "机机机") # self.assertEqual(engine.action("abdefg"), None) # check byte input engine, = PFAEngine.fromYaml(""" input: bytes output: [bytes, "null"] action: - re.findfirst: [input, {bytes.encodeUtf8: {string: "对讲机(讲|机)*"}}] """) self.assertEqual( engine.action( "abcde\xe5\xaf\xb9\xe8\xae\xb2\xe6\x9c\xba\xe8\xae\xb2fgg\xe5\xaf\xb9\xe8\xae\xb2\xe6\x9c\xba\xe8\xae\xb2h" ), {"bytes": "\xe5\xaf\xb9\xe8\xae\xb2\xe6\x9c\xba\xe8\xae\xb2"}) self.assertEqual(engine.action("abcdefghijk"), None)
def testfindFirst(self): engine, = PFAEngine.fromYaml(""" input: string output: [string, "null"] action: - {re.findfirst: [input, [ab]]} """) self.assertEqual(engine.action("88ccc555"), None) self.assertEqual(engine.action("abcabcabc"), "ab") # check non-ascii input # engine, = PFAEngine.fromYaml(""" #input: string #output: [string, "null"] #action: # - {re.findfirst: [input, [机机+]]} #""") # self.assertEqual(engine.action("abc机机机abca机机bc asdkj 机机机sd"), "机机机") # self.assertEqual(engine.action("abdefg"), None) # check byte input engine, = PFAEngine.fromYaml(""" input: bytes output: [bytes, "null"] action: - re.findfirst: [input, {bytes.encodeUtf8: {string: "对讲机(讲|机)*"}}] """) self.assertEqual(engine.action("abcde对讲机讲fgg对讲机讲h"), "对讲机讲") self.assertEqual(engine.action("abcdefghijk"), None)
def testBin(self): engine1, = PFAEngine.fromYaml(''' input: double output: int action: interp.bin: [input, 5, 100, 110] ''') self.assertEqual(engine1.action(100.0), 0) self.assertEqual(engine1.action(101.0), 0) self.assertEqual(engine1.action(101.999), 0) self.assertEqual(engine1.action(102.0), 1) self.assertEqual(engine1.action(109.999), 4) engine2, = PFAEngine.fromYaml(''' input: double output: int action: interp.bin: [input, 100, 2] ''') self.assertEqual(engine2.action(99.0), -1) self.assertEqual(engine2.action(99.999), -1) self.assertEqual(engine2.action(100.0), 0) self.assertEqual(engine2.action(101.0), 0) self.assertEqual(engine2.action(101.999), 0) self.assertEqual(engine2.action(102.0), 1) self.assertEqual(engine2.action(109.999), 4) self.assertEqual(engine2.action(110.0), 5) self.assertEqual(engine2.action(0.0), -50)
def testDoMin(self): engine, = PFAEngine.fromYaml(''' input: double output: double action: - {min: [input, 3.2]} ''') self.assertEqual(engine.action(2.2), 2.2) self.assertEqual(engine.action(4.2), 3.2) engine, = PFAEngine.fromYaml(''' input: int output: int action: - {min: [input, 3]} ''') self.assertEqual(engine.action(2), 2) self.assertEqual(engine.action(4), 3) engine, = PFAEngine.fromYaml(''' input: string output: string action: - {min: [input, [HAL]]} ''') self.assertEqual(engine.action("GZK"), "GZK") self.assertEqual(engine.action("IBM"), "HAL")
def testBytes(self): engine1, = PFAEngine.fromYaml(''' input: "null" output: bytes randseed: 12345 action: {rand.bytes: [10]} ''') self.assertEqual(engine1.action(None), "j\x02\xd3L^1\x90)\x1fn") self.assertEqual(engine1.action(None), "\x8f,\x8dZ\xf5\x17\xfai\x81%") self.assertEqual(engine1.action(None), "\xb80W\x06V\xf7\xfa\xbe\x00\xf0") engine2, = PFAEngine.fromYaml(''' input: "null" output: bytes randseed: 12345 action: {rand.bytes: [10, {base64: "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXowMTIzNDU2Nzg5"}]} ''') self.assertEqual(engine2.action(None), "oa3kngufep") self.assertEqual(engine2.action(None), "ugtm8d9osf") self.assertEqual(engine2.action(None), "zgmam890a7") engine3, = PFAEngine.fromYaml(''' input: "null" output: bytes randseed: 12345 action: {rand.bytes: [10, 33, 127]} ''') self.assertEqual(engine3.action(None), "H!n=C3V0,I") self.assertEqual(engine3.action(None), "U1UB{)|GP.") self.assertEqual(engine3.action(None), "d2A#@{}f!y")
def bad(): PFAEngine.fromYaml(''' input: ["null", "null"] output: "null" action: - {impute.defaultOnNull: [input, "null"]} ''')
def testString(self): engine1, = PFAEngine.fromYaml(''' input: "null" output: string randseed: 12345 action: {rand.string: [10]} ''') self.assertEqual(engine1.action(None), u"姾ȳ눿䂂侔⧕穂⋭嶄") self.assertEqual(engine1.action(None), u"祩▩睿䲩컲Ꮉ퍣夅泚 ") self.assertEqual(engine1.action(None), u"魍⤉䧇ԕ䥖탺퍬ꃒÀ쬘") engine2, = PFAEngine.fromYaml(''' input: "null" output: string randseed: 12345 action: {rand.string: [10, {string: "abcdefghijklmnopqrstuvwxyz0123456789"}]} ''') self.assertEqual(engine2.action(None), "oa3kngufep") self.assertEqual(engine2.action(None), "ugtm8d9osf") self.assertEqual(engine2.action(None), "zgmam890a7") engine3, = PFAEngine.fromYaml(''' input: "null" output: string randseed: 12345 action: {rand.string: [10, 33, 127]} ''') self.assertEqual(engine3.action(None), "H!n=C3V0,I") self.assertEqual(engine3.action(None), "U1UB{)|GP.") self.assertEqual(engine3.action(None), "d2A#@{}f!y")
def testDoAbs(self): engine, = PFAEngine.fromYaml(''' input: double output: double action: - {m.abs: input} ''') self.assertAlmostEqual(engine.action(-3.14), 3.14, places=2) engine, = PFAEngine.fromYaml(''' input: int output: int action: - {m.abs: input} ''') self.assertEqual(engine.action(2147483647), 2147483647) self.assertEqual(engine.action(-2147483647), 2147483647) self.assertRaises(PFARuntimeException, lambda: engine.action(-2147483648)) engine, = PFAEngine.fromYaml(''' input: long output: long action: - {m.abs: input} ''') self.assertEqual(engine.action(9223372036854775807), 9223372036854775807) self.assertEqual(engine.action(-9223372036854775807), 9223372036854775807) self.assertRaises(PFARuntimeException, lambda: engine.action(-9223372036854775808))
def testProbit(self): engine, = PFAEngine.fromYaml(""" input: double output: double action: {m.link.probit: input} """) self.assertAlmostEqual(engine.action(2.2), 0.9860965524865013, places = 5) engine, = PFAEngine.fromYaml(""" input: {type: array, items: double} output: {type: array, items: double} action: {m.link.probit: input} """) out = engine.action([1, 2, 3, 4]) self.assertAlmostEqual(out[0], 0.841344746068543 , places = 5) self.assertAlmostEqual(out[1], 0.9772498680518207, places = 5) self.assertAlmostEqual(out[2], 0.9986501019683699, places = 5) self.assertAlmostEqual(out[3], 0.9999683287581669, places = 5) engine, = PFAEngine.fromYaml(""" input: {type: map, values: double} output: {type: map, values: double} action: {m.link.probit: input} """) out = engine.action({"one": 1, "two": 2, "three": 3, "four": 4}) self.assertAlmostEqual(out["one"], 0.841344746068543 , places = 5) self.assertAlmostEqual(out["two"], 0.9772498680518207, places = 5) self.assertAlmostEqual(out["three"], 0.9986501019683699, places = 5) self.assertAlmostEqual(out["four"], 0.9999683287581669, places = 5)
def testLogit(self): engine, = PFAEngine.fromYaml(""" input: double output: double action: {m.link.logit: input} """) self.assertAlmostEqual(engine.action(2.2), 0.9002495108803148, places = 5) engine, = PFAEngine.fromYaml(""" input: {type: array, items: double} output: {type: array, items: double} action: {m.link.logit: input} """) out = engine.action([1, 2, 3, 4]) self.assertAlmostEqual(out[0], 0.7310585786300049, places = 5) self.assertAlmostEqual(out[1], 0.8807970779778823, places = 5) self.assertAlmostEqual(out[2], 0.9525741268224334, places = 5) self.assertAlmostEqual(out[3], 0.9820137900379085, places = 5) engine, = PFAEngine.fromYaml(""" input: {type: map, values: double} output: {type: map, values: double} action: {m.link.logit: input} """) out = engine.action({"one": 1, "two": 2, "three": 3, "four": 4}) self.assertAlmostEqual(out["one"], 0.7310585786300049, places = 5) self.assertAlmostEqual(out["two"], 0.8807970779778823, places = 5) self.assertAlmostEqual(out["three"],0.9525741268224334, places = 5) self.assertAlmostEqual(out["four"], 0.9820137900379085, places = 5)
def testDoExponentiation(self): engine, = PFAEngine.fromYaml(''' input: double output: double action: - {"**": [input, 30]} ''') self.assertAlmostEqual(engine.action(2.5), 867361737988.4036, places=2) engine, = PFAEngine.fromYaml(''' input: int output: int action: - {"**": [input, 30]} ''') self.assertEqual(engine.action(2), 1073741824) engine, = PFAEngine.fromYaml(''' input: int output: int action: - {"**": [input, 30]} ''') self.assertRaises(PFARuntimeException, lambda: engine.action(3)) engine, = PFAEngine.fromYaml(''' input: long output: long action: - {"**": [input, 30]} ''') self.assertEqual(engine.action(3), 205891132094649)
def testMakeTimestamp(self): engine, = PFAEngine.fromYaml(""" input: int output: double action: - {time.makeTimestamp: [input, 3, 11, 14, 50, 6, 245, {string: ""}]} """) self.assertEqual(engine.action(2015), 1426085406.245) engine2, = PFAEngine.fromYaml(""" input: int output: double action: - {time.makeTimestamp: [input, 1, 1, 0, 0, 0, 0, {string: ""}]} """) self.assertEqual(engine2.action(1970), 0) engine3, = PFAEngine.fromYaml(""" input: string output: double action: - {time.makeTimestamp: [1970, 1, 1, 0, 0, 0, 0, input]} """) self.assertEqual(engine3.action("Europe/Paris"), -3600) self.assertEqual(engine3.action("Etc/UTC"), 0) self.assertEqual(engine3.action("Atlantic/Azores"), 3600)
def testDoCmp(self): engine, = PFAEngine.fromYaml(''' input: int output: int action: - {cmp: [input, 5]} ''') self.assertEqual(engine.action(3), -1) self.assertEqual(engine.action(5), 0) self.assertEqual(engine.action(7), 1) engine, = PFAEngine.fromYaml(''' input: double output: int action: - {cmp: [input, 5.3]} ''') self.assertEqual(engine.action(5.2), -1) self.assertEqual(engine.action(5.3), 0) self.assertEqual(engine.action(5.4), 1) engine, = PFAEngine.fromYaml(''' input: string output: int action: - {cmp: [input, [HAL]]} ''') self.assertEqual(engine.action("GZK"), -1) self.assertEqual(engine.action("HAL"), 0) self.assertEqual(engine.action("IBM"), 1)
def testEliminateOnlyCertainKeys(self): engine, = PFAEngine.fromYaml(''' input: {type: array, items: string} output: {type: map, values: int} action: map.except: - {value: {"a": 1, "b": 2, "c": 3, "d": 4, "e": 5}, type: {type: map, values: int}} - input ''') self.assertEqual(engine.action(["b", "c", "e"]), {"a": 1, "d": 4}) self.assertEqual(engine.action(["b", "c", "e", "z"]), {"a": 1, "d": 4}) self.assertEqual(engine.action([]), { "a": 1, "b": 2, "c": 3, "d": 4, "e": 5 }) engine, = PFAEngine.fromYaml(''' input: {type: array, items: string} output: {type: map, values: int} action: map.except: - {value: {}, type: {type: map, values: int}} - input ''') self.assertEqual(engine.action(["b", "c", "e"]), {}) self.assertEqual(engine.action([]), {})
def testRejectComparisonsBetweenDifferentTypesEvenIfTheyHaveTheSameStructure( self): engine, = PFAEngine.fromYaml(''' input: {type: enum, name: Category1, symbols: [z, y, x, w]} output: boolean action: - {"==": [input, {type: Category1, value: x}]} ''') self.assertFalse(engine.action("z")) self.assertFalse(engine.action("y")) self.assertTrue(engine.action("x")) self.assertFalse(engine.action("w")) self.assertRaises( PFASemanticException, lambda: PFAEngine.fromYaml(''' input: {type: enum, name: Category1, symbols: [z, y, x, w]} output: boolean action: - {"==": [input, {type: {type: enum, name: Category2, symbols: [w, x, y, z]}, value: x}]} ''')) self.assertRaises( PFASemanticException, lambda: PFAEngine.fromYaml(''' input: {type: enum, name: Category1, symbols: [z, y, x, w]} output: boolean action: - {"==": [input, {type: {type: enum, name: Category2, symbols: [w, x, y, whatever]}, value: x}]} '''))
def testBitwiseOperators(self): engine, = PFAEngine.fromYaml(''' input: "null" output: int action: - {"&": [85, 15]} ''') self.assertEqual(engine.action(None), 5) engine, = PFAEngine.fromYaml(''' input: "null" output: int action: - {"|": [85, 15]} ''') self.assertEqual(engine.action(None), 95) engine, = PFAEngine.fromYaml(''' input: "null" output: int action: - {"^": [85, 15]} ''') self.assertEqual(engine.action(None), 90) engine, = PFAEngine.fromYaml(''' input: "null" output: int action: - {"~": [85]} ''') self.assertEqual(engine.action(None), -86)
def testreplaceLast(self): engine, = PFAEngine.fromYaml(""" input: string output: string action: - {re.replacelast: [input, ["ab(c|d)*"], ["person"]]} """) self.assertEqual(engine.action("abcccdcPPPPabcccdc"), "abcccdcPPPPperson") self.assertEqual(engine.action("abcccdcPPPPabcccdcPPPP"), "abcccdcPPPPpersonPPPP") # check non-ascii input engine, = PFAEngine.fromYaml(""" input: string output: string action: - {re.replacelast: [input, [对讲机+], ["walkie talkie"]]} """) self.assertEqual(engine.action("This 对讲机 works better than that 对讲机."), "This 对讲机 works better than that walkie talkie.") # check byte input engine, = PFAEngine.fromYaml(""" input: bytes output: bytes action: - {re.replacelast: [input, {bytes.encodeUtf8: {string: "对讲机+"}}, {bytes.encodeUtf8: {string: "walkie talkie"}}]} """) self.assertEqual( engine.action( 'This \xe5\xaf\xb9\xe8\xae\xb2\xe6\x9c\xba works better than that \xe5\xaf\xb9\xe8\xae\xb2\xe6\x9c\xba.' ), 'This \xe5\xaf\xb9\xe8\xae\xb2\xe6\x9c\xba works better than that walkie talkie.' )
def testreplaceFirst(self): engine, = PFAEngine.fromYaml(""" input: string output: string action: - {re.replacefirst: [input, ["ab(c|d)*"], ["person"]]} """) self.assertEqual(engine.action("abcccdcPPPP"), "personPPPP") self.assertEqual(engine.action("PPPPabcccdcPPPP"), "PPPPpersonPPPP") self.assertEqual(engine.action("PPPPPPPP"), "PPPPPPPP") engine, = PFAEngine.fromYaml(""" input: string output: string action: - {re.replacefirst: [input, ["ab(c|d)*"], ["walkie talkie"]]} """) self.assertEqual(engine.action("This abcccdc works better than that abcccdc."), "This walkie talkie works better than that abcccdc.") # check non-ascii input # engine, = PFAEngine.fromYaml(""" #input: string #output: string #action: # - {re.replacefirst: [input, [对讲机+], ["walkie talkie"]]} #""") # self.assertEqual(engine.action("This 对讲机 works better than that 对讲机."), "This walkie talkie works better than that 对讲机.") # check byte input engine, = PFAEngine.fromYaml(""" input: bytes output: bytes action: - {re.replacefirst: [input, {bytes.encodeUtf8: {string: "对讲机+"}}, {bytes.encodeUtf8: {string: "walkie talkie"}}]} """) self.assertEqual(engine.action("This 对讲机 works better than that 对讲机."), "This walkie talkie works better than that 对讲机.")
def testreplaceLast(self): engine, = PFAEngine.fromYaml(""" input: string output: string action: - {re.replacelast: [input, ["ab(c|d)*"], ["person"]]} """) self.assertEqual(engine.action("abcccdcPPPPabcccdc"), "abcccdcPPPPperson") self.assertEqual(engine.action("abcccdcPPPPabcccdcPPPP"), "abcccdcPPPPpersonPPPP") # check non-ascii input # engine, = PFAEngine.fromYaml(""" #input: string #output: string #action: # - {re.replacelast: [input, [对讲机+], ["walkie talkie"]]} #""") # self.assertEqual(engine.action("This 对讲机 works better than that 对讲机."), "This 对讲机 works better than that walkie talkie.") # check byte input engine, = PFAEngine.fromYaml(""" input: bytes output: bytes action: - {re.replacelast: [input, {bytes.encodeUtf8: {string: "对讲机+"}}, {bytes.encodeUtf8: {string: "walkie talkie"}}]} """) self.assertEqual(engine.action("This 对讲机 works better than that 对讲机."), "This 对讲机 works better than that walkie talkie.")
def testComputeZValues(self): engine, = PFAEngine.fromYaml(''' input: double output: double action: - stat.change.zValue: - input - value: {count: 21, mean: 10, variance: 4.0} type: {type: record, name: R, namespace: what.ever, fields: [{name: count, type: double}, {name: mean, type: double}, {name: variance, type: double}]} - false ''') self.assertAlmostEqual(engine.action(10.0), 0.00, places=2) self.assertAlmostEqual(engine.action(12.0), 1.00, places=2) self.assertAlmostEqual(engine.action(0.0), -5.00, places=2) self.assertAlmostEqual(engine.action(15.0), 2.50, places=2) self.assertAlmostEqual(engine.action(8.0), -1.00, places=2) engine, = PFAEngine.fromYaml(''' input: double output: double action: - stat.change.zValue: - input - value: {count: 21, mean: 10, variance: 4.0} type: {type: record, name: R, namespace: what.ever, fields: [{name: count, type: double}, {name: mean, type: double}, {name: variance, type: double}]} - true ''') self.assertAlmostEqual(engine.action(10.0), 0.00, places=2) self.assertAlmostEqual(engine.action(12.0), 1.02, places=2) self.assertAlmostEqual(engine.action(0.0), -5.12, places=2) self.assertAlmostEqual(engine.action(15.0), 2.56, places=2) self.assertAlmostEqual(engine.action(8.0), -1.02, places=2)
def testUUID(self): engine1, = PFAEngine.fromYaml(''' input: "null" output: string randseed: 12345 action: {rand.uuid4: []} ''') self.assertEqual(engine1.action(None), "6aa79987-bb91-4029-8d1f-cd8778e7d340bbcd") self.assertEqual(engine1.action(None), "4c73a942-daea-45e5-8ee8-452ec40a3193ca54") self.assertEqual(engine1.action(None), "90e5e945-6fac-4296-85f8-dfc9e3b11fcff454") engine2, = PFAEngine.fromYaml(''' input: "null" output: string action: {s.substr: [{rand.uuid4: []}, 14, 15]} ''') for i in range(1000): self.assertEqual(engine2.action(None), "4") engine3, = PFAEngine.fromYaml(''' input: "null" output: string action: {s.substr: [{rand.uuid4: []}, 19, 20]} ''') for i in range(1000): self.assertEqual(engine3.action(None), "8")
def testNChooseKFcn(self): engine, = PFAEngine.fromYaml(''' input: int output: int action: - m.special.nChooseK: [input, 2] ''') self.assertEqual(engine.action(20), 190) self.assertEqual(engine.action(10), 45) self.assertEqual(engine.action(3), 3) ### raise the right exceptions ### engine, = PFAEngine.fromYaml(''' input: int output: int action: - m.special.nChooseK: [input, 4] ''') self.assertRaises(PFARuntimeException, lambda: engine.action(1)) self.assertRaises(PFARuntimeException, lambda: engine.action(0)) engine, = PFAEngine.fromYaml(''' input: int output: int action: - m.special.nChooseK: [input, -4] #[input, lambda] ''') self.assertRaises(PFARuntimeException, lambda: engine.action(-2))
def testRegMahalanobis(self): engine, = PFAEngine.fromYaml(""" input: {type: array, items: double} output: double action: stat.test.mahalanobis: - input - type: {type: array, items: double} value: [2.5, 2.5, 2.5] - type: {type: array, items: {type: array, items: double}} value: [[2.0, 0.0, 0.0], [0.0, 4.0, 0.0], [0.0, 0.0, 1.0]] """) self.assertAlmostEqual(engine.action([1, 2, 3]), 1.19895788083, places = 5) engine, = PFAEngine.fromYaml(""" input: {type: map, values: double} output: double action: stat.test.mahalanobis: - input - type: {type: map, values: double} value: {one: 2.5, two: 2.5, three: 2.5} - type: {type: map, values: {type: map, values: double}} value: {one: {one: 2.0, two: 0.0, three: 0.0}, two: {one: 0.0, two: 4.0, three: 0.0}, three: {one: 0.0, two: 0.0, three: 1.0}} """) self.assertAlmostEqual(engine.action({"one": 1, "two": 2, "three": 3}), 1.19895788083, places = 5)
def testUUID(self): engine1, = PFAEngine.fromYaml(''' input: "null" output: string randseed: 12345 action: {rand.uuid4: []} ''') self.assertEqual(engine1.action(None), "6aa79987-bb91-4029-8d1f-cd8778e7d340bbcd") self.assertEqual(engine1.action(None), "4c73a942-daea-45e5-8ee8-452ec40a3193ca54") self.assertEqual(engine1.action(None), "90e5e945-6fac-4296-85f8-dfc9e3b11fcff454") engine2, = PFAEngine.fromYaml(''' input: "null" output: string action: {s.substr: [{rand.uuid4: []}, 14, 15]} ''') for i in xrange(1000): self.assertEqual(engine2.action(None), "4") engine3, = PFAEngine.fromYaml(''' input: "null" output: string action: {s.substr: [{rand.uuid4: []}, 19, 20]} ''') for i in xrange(1000): self.assertEqual(engine3.action(None), "8")
def testCloglog(self): engine, = PFAEngine.fromYaml(""" input: double output: double action: {m.link.cloglog: input} """) self.assertAlmostEqual(engine.action(2.2), 0.9998796388196516, places = 5) engine, = PFAEngine.fromYaml(""" input: {type: array, items: double} output: {type: array, items: double} action: {m.link.cloglog: input} """) out = engine.action([1, 2, 3, 4]) self.assertAlmostEqual(out[0], 0.9340119641546875, places = 5) self.assertAlmostEqual(out[1], 0.9993820210106689, places = 5) self.assertAlmostEqual(out[2], 0.9999999981078213, places = 5) self.assertAlmostEqual(out[3], 1.0 , places = 5) engine, = PFAEngine.fromYaml(""" input: {type: map, values: double} output: {type: map, values: double} action: {m.link.cloglog: input} """) out = engine.action({"one": 1, "two": 2, "three": 3, "four": 4}) self.assertAlmostEqual(out["one"], 0.9340119641546875, places = 5) self.assertAlmostEqual(out["two"], 0.9993820210106689, places = 5) self.assertAlmostEqual(out["three"], 0.9999999981078213, places = 5) self.assertAlmostEqual(out["four"], 1.0 , places = 5)
def testErrorOnNonNum(self): floatEngine, = PFAEngine.fromYaml(''' input: float output: float action: {impute.errorOnNonNum: input} ''') self.assertEqual(floatEngine.action(123.4), 123.4) self.assertRaises(PFARuntimeException, lambda: floatEngine.action(float("nan"))) self.assertRaises(PFARuntimeException, lambda: floatEngine.action(float("inf"))) self.assertRaises(PFARuntimeException, lambda: floatEngine.action(float("-inf"))) doubleEngine, = PFAEngine.fromYaml(''' input: double output: double action: {impute.errorOnNonNum: input} ''') self.assertEqual(doubleEngine.action(123.4), 123.4) self.assertRaises(PFARuntimeException, lambda: doubleEngine.action(float("nan"))) self.assertRaises(PFARuntimeException, lambda: doubleEngine.action(float("inf"))) self.assertRaises(PFARuntimeException, lambda: doubleEngine.action(float("-inf")))
def testLoglog(self): engine, = PFAEngine.fromYaml(""" input: double output: double action: {m.link.loglog: input} """) self.assertAlmostEqual(engine.action(2.2), 1.203611803484212E-4, places = 5) engine, = PFAEngine.fromYaml(""" input: {type: array, items: double} output: {type: array, items: double} action: {m.link.loglog: input} """) out = engine.action([1, 2, 3, 4]) self.assertAlmostEqual(out[0], 0.06598803584531254 , places = 5) self.assertAlmostEqual(out[1], 6.179789893310934E-4 , places = 5) self.assertAlmostEqual(out[2], 1.8921786948382924E-9 , places = 5) self.assertAlmostEqual(out[3], 1.9423376049564073E-24, places = 5) engine, = PFAEngine.fromYaml(""" input: {type: map, values: double} output: {type: map, values: double} action: {m.link.loglog: input} """) out = engine.action({"one": 1, "two": 2, "three": 3, "four": 4}) self.assertAlmostEqual(out["one"], 0.06598803584531254 , places = 5) self.assertAlmostEqual(out["two"], 6.179789893310934E-4 , places = 5) self.assertAlmostEqual(out["three"], 1.8921786948382924E-9 , places = 5) self.assertAlmostEqual(out["four"], 1.9423376049564073E-24, places = 5)
def testCauchit(self): engine, = PFAEngine.fromYaml(""" input: double output: double action: {m.link.cauchit: input} """) self.assertAlmostEqual(engine.action(2.2), 0.8642002512199081, places = 5) engine, = PFAEngine.fromYaml(""" input: {type: array, items: double} output: {type: array, items: double} action: {m.link.cauchit: input} """) out = engine.action([1, 2, 3, 4]) self.assertAlmostEqual(out[0], 0.75 , places = 5) self.assertAlmostEqual(out[1], 0.8524163823495667, places = 5) self.assertAlmostEqual(out[2], 0.8975836176504333, places = 5) self.assertAlmostEqual(out[3], 0.9220208696226307, places = 5) engine, = PFAEngine.fromYaml(""" input: {type: map, values: double} output: {type: map, values: double} action: {m.link.cauchit: input} """) out = engine.action({"one": 1, "two": 2, "three": 3, "four": 4}) self.assertAlmostEqual(out["one"], 0.75 , places = 5) self.assertAlmostEqual(out["two"], 0.8524163823495667, places = 5) self.assertAlmostEqual(out["three"], 0.8975836176504333, places = 5) self.assertAlmostEqual(out["four"], 0.9220208696226307, places = 5)
def testFromSet(self): engine, = PFAEngine.fromYaml(''' input: {type: map, values: int} output: {type: array, items: int} action: - {map.fromset: [input]} ''') self.assertEqual( set( engine.action({ "BA==": 2, "Ag==": 1, "Bg==": 3, "Cg==": 5, "CA==": 4 })), set([1, 2, 3, 4, 5])) engine, = PFAEngine.fromYaml(''' input: {type: map, values: string} output: {type: array, items: string} action: - {map.fromset: [input]} ''') self.assertEqual( set( engine.action({ "BA==": "two", "Ag==": "one", "Bg==": "three", "Cg==": "five", "CA==": "four" })), set(["one", "two", "three", "four", "five"]))
def testSoftPlus(self): engine, = PFAEngine.fromYaml(""" input: double output: double action: {m.link.softplus: input} """) self.assertAlmostEqual(engine.action(2.2), 2.305083319768696, places = 5) engine, = PFAEngine.fromYaml(""" input: {type: array, items: double} output: {type: array, items: double} action: {m.link.softplus: input} """) out = engine.action([1, 2, 3, 4]) self.assertAlmostEqual(out[0], 1.31326168752 , places = 5) self.assertAlmostEqual(out[1], 2.12692801104 , places = 5) self.assertAlmostEqual(out[2], 3.04858735157 , places = 5) self.assertAlmostEqual(out[3], 4.01814992792 , places = 5) engine, = PFAEngine.fromYaml(""" input: {type: map, values: double} output: {type: map, values: double} action: {m.link.softplus: input} """) out = engine.action({"one": 1, "two": 2, "three": 3, "four": 4}) self.assertAlmostEqual(out["one"], 1.31326168752, places = 5) self.assertAlmostEqual(out["two"], 2.12692801104, places = 5) self.assertAlmostEqual(out["three"], 3.04858735157, places = 5) self.assertAlmostEqual(out["four"], 4.01814992792, places = 5)
def testReLu(self): engine, = PFAEngine.fromYaml(""" input: double output: double action: {m.link.relu: input} """) self.assertAlmostEqual(engine.action(2.2), 2.2, places = 1) engine, = PFAEngine.fromYaml(""" input: {type: array, items: double} output: {type: array, items: double} action: {m.link.relu: input} """) out = engine.action([-1, -2, 3, 4]) self.assertAlmostEqual(out[0], 0.0, places = 5) self.assertAlmostEqual(out[1], 0.0, places = 5) self.assertAlmostEqual(out[2], 3.0, places = 5) self.assertAlmostEqual(out[3], 4.0, places = 5) engine, = PFAEngine.fromYaml(""" input: {type: map, values: double} output: {type: map, values: double} action: {m.link.relu: input} """) out = engine.action({"one": -1, "two": -2, "three": 3, "four": 4}) self.assertAlmostEqual(out["one"], 0.0, places = 5) self.assertAlmostEqual(out["two"], 0.0, places = 5) self.assertAlmostEqual(out["three"], 3.0, places = 5) self.assertAlmostEqual(out["four"], 4.0, places = 5)
def testRejectComparisonsBetweenDifferentTypesEvenIfTheyHaveTheSameStructure(self): engine, = PFAEngine.fromYaml(''' input: {type: enum, name: Category1, symbols: [z, y, x, w]} output: boolean action: - {"==": [input, {type: Category1, value: x}]} ''') self.assertFalse(engine.action("z")) self.assertFalse(engine.action("y")) self.assertTrue(engine.action("x")) self.assertFalse(engine.action("w")) self.assertRaises(PFASemanticException, lambda: PFAEngine.fromYaml(''' input: {type: enum, name: Category1, symbols: [z, y, x, w]} output: boolean action: - {"==": [input, {type: {type: enum, name: Category2, symbols: [w, x, y, z]}, value: x}]} ''')) self.assertRaises(PFASemanticException, lambda: PFAEngine.fromYaml(''' input: {type: enum, name: Category1, symbols: [z, y, x, w]} output: boolean action: - {"==": [input, {type: {type: enum, name: Category2, symbols: [w, x, y, whatever]}, value: x}]} '''))
def testfindAll(self): engine, = PFAEngine.fromYaml(""" input: string output: {type: array, items: string} action: - {re.findall: [input, [ab]]} """) self.assertEqual(engine.action("abcabcabc"), ["ab", "ab", "ab"]) self.assertEqual(engine.action("88cabcc"), ["ab"]) # check non-ascii string input engine, = PFAEngine.fromYaml(""" input: string output: {type: array, items: string} action: - {re.findall: [input, [猫机+猫]]} """) self.assertEqual(engine.action("猫机猫oooo猫机机猫ppp猫机机机猫bbbb猫机aaaa猫机机"), ["猫机猫", "猫机机猫", "猫机机机猫"]) # check byte input engine, = PFAEngine.fromYaml(""" input: bytes output: {type: array, items: bytes} action: - re.findall: [input, {bytes.encodeUtf8: {string: "ab+"}}] """) self.assertEqual(engine.action("xyz"), []) self.assertEqual(engine.action("abc\xe6\x9c\xba\xe6\x9c\xbaabcabc"), ["ab", "ab", "ab"])
def testgroupsAll(self): engine, = PFAEngine.fromYaml(""" input: string output: {type: array, items: {type: array, items: {type: array, items: int}}} action: - {re.groupsall: [input, [()(a)bc(def)ghijk]]} """) self.assertEqual(engine.action("abcdefghijkMMMMMabcdefghijkMMMM"), [[[0, 11], [0, 0], [0, 1], [3, 6]], [[16, 27], [16, 16], [16, 17], [19, 22]]]) ## check non-ascii input # engine, = PFAEngine.fromYaml(""" # input: string # output: {type: array, items: {type: array, items: {type: array, items: int}}} # action: # - {re.groupsall: [input, [(机)机]]} # """) # self.assertEqual(engine.action("abc机机abca机机bc"), [[[3,5], [3,4]], [[9,11], [9,10]]]) # check byte input engine, = PFAEngine.fromYaml(""" input: bytes output: {type: array, items: {type: array, items: {type: array, items: int}}} action: - re.groupsall: [input, {bytes.encodeUtf8: {string: "(机)机"}}] """) self.assertEqual( engine.action( 'abc\xe6\x9c\xba\xe6\x9c\xbaabca\xe6\x9c\xba\xe6\x9c\xbabc'), [[[3, 9], [3, 6]], [[13, 19], [13, 16]]])
def testScaleMaps(self): engine, = PFAEngine.fromYaml(''' input: "null" output: {type: map, values: double} action: la.scale: - type: {type: map, values: double} value: {one: 1, two: 2, three: 3, four: 4, five: 5} - 10 ''') self.assertAlmostEqual(self.chi2Vector(engine.action(None), {"one": 10.0, "two": 20.0, "three": 30.0, "four": 40.0, "five": 50.0}), 0.0, places=2) engine, = PFAEngine.fromYaml(''' input: "null" output: {type: map, values: {type: map, values: double}} action: la.scale: - type: {type: map, values: {type: map, values: double}} value: {uno: {one: 1, two: 2, three: 3}, dos: {four: 4, five: 5, six: 6}, tres: {seven: 7, eight: 8, nine: 9}} - 10 ''') self.assertAlmostEqual(self.chi2(engine.action(None), {"uno": {"one": 10.0, "two": 20.0, "three": 30.0}, "dos": {"four": 40.0, "five": 50.0, "six": 60.0}, "tres": {"seven": 70.0, "eight": 80.0, "nine": 90.0}}), 0.0, places=2)
def testfindAll(self): engine, = PFAEngine.fromYaml(""" input: string output: {type: array, items: string} action: - {re.findall: [input, [ab]]} """) self.assertEqual(engine.action("abcabcabc"), ["ab","ab", "ab"]) self.assertEqual(engine.action("88cabcc"), ["ab"]) # check non-ascii string input # engine, = PFAEngine.fromYaml(""" #input: string #output: {type: array, items: string} #action: # - {re.findall: [input, [猫机+猫]]} #""") # self.assertEqual(engine.action("猫机猫oooo猫机机猫ppp猫机机机猫bbbb猫机aaaa猫机机"), ["猫机猫" ,"猫机机猫","猫机机机猫"]) # check byte input engine, = PFAEngine.fromYaml(""" input: bytes output: {type: array, items: bytes} action: - re.findall: [input, {bytes.encodeUtf8: {string: "ab+"}}] """) self.assertEqual(engine.action("xyz"), []) self.assertEqual(engine.action("abc机机abcabc"), ["ab", "ab","ab"] )
def testSubMaps(self): engine, = PFAEngine.fromYaml(''' input: "null" output: {type: map, values: double} action: la.sub: - type: {type: map, values: double} value: {one: 1, two: 2, three: 3, four: 4, five: 5} - type: {type: map, values: double} value: {one: 101, two: 102, three: 103, four: 104, five: 105, six: 999} ''') self.assertAlmostEqual(self.chi2Vector(engine.action(None), {"one": -100.0, "two": -100.0, "three": -100.0, "four": -100.0, "five": -100.0, "six": -999.0}), 0.0, places=2) engine, = PFAEngine.fromYaml(''' input: "null" output: {type: map, values: {type: map, values: double}} action: la.sub: - type: {type: map, values: {type: map, values: double}} value: {uno: {one: 1, two: 2, three: 3}, dos: {one: 4, two: 5, three: 6}, tres: {one: 7, two: 8, three: 9}} - type: {type: map, values: {type: map, values: double}} value: {uno: {one: 101, two: 102, three: 103}, dos: {one: 104, two: 105, three: 106}, tres: {one: 107, two: 108, three: 109, four: 999.0}} ''') self.assertAlmostEqual(self.chi2(engine.action(None), {"uno": {"one": -100.0, "two": -100.0, "three": -100.0, "four": 0.0}, "dos": {"one": -100.0, "two": -100.0, "three": -100.0, "four": 0.0}, "tres": {"one": -100.0, "two": -100.0, "three": -100.0, "four": -999.0}}), 0.0, places=2)
def testgroupsAll(self): engine, = PFAEngine.fromYaml(""" input: string output: {type: array, items: {type: array, items: {type: array, items: int}}} action: - {re.groupsall: [input, [()(a)bc(def)ghijk]]} """) self.assertEqual(engine.action("abcdefghijkMMMMMabcdefghijkMMMM"), [[[0,11], [0,0], [0,1], [3,6]], [[16, 27],[16,16],[16,17], [19,22]]]) ## check non-ascii input # engine, = PFAEngine.fromYaml(""" #input: string #output: {type: array, items: {type: array, items: {type: array, items: int}}} #action: # - {re.groupsall: [input, [(机)机]]} #""") # self.assertEqual(engine.action("abc机机abca机机bc"), [[[3,5], [3,4]], [[9,11], [9,10]]]) # check byte input engine, = PFAEngine.fromYaml(""" input: bytes output: {type: array, items: {type: array, items: {type: array, items: int}}} action: - re.groupsall: [input, {bytes.encodeUtf8: {string: "(机)机"}}] """) self.assertEqual(engine.action("abc机机abca机机bc"), [[[3,9], [3,6]], [[13,19], [13,16]]])
def testScaleArrays(self): engine, = PFAEngine.fromYaml(''' input: "null" output: {type: array, items: double} action: la.scale: - type: {type: array, items: double} value: [1, 2, 3, 4, 5, 6, 7, 8, 9] - 10 ''') self.assertAlmostEqual(self.chi2Vector( engine.action(None), [10.0, 20.0, 30.0, 40.0, 50.0, 60.0, 70.0, 80.0, 90.0]), 0.0, places=2) engine, = PFAEngine.fromYaml(''' input: "null" output: {type: array, items: {type: array, items: double}} action: la.scale: - type: {type: array, items: {type: array, items: double}} value: [[1, 2, 3], [4, 5, 6], [7, 8, 9]] - 10 ''') self.assertAlmostEqual(self.chi2( engine.action(None), [[10.0, 20.0, 30.0], [40.0, 50.0, 60.0], [70.0, 80.0, 90.0]]), 0.0, places=2)
def testSubArrays(self): engine, = PFAEngine.fromYaml(''' input: "null" output: {type: array, items: double} action: la.sub: - type: {type: array, items: double} value: [1, 2, 3, 4, 5, 6, 7, 8, 9] - type: {type: array, items: double} value: [101, 102, 103, 104, 105, 106, 107, 108, 109] ''') self.assertAlmostEqual(self.chi2Vector(engine.action(None), [ -100.0, -100.0, -100.0, -100.0, -100.0, -100.0, -100.0, -100.0, -100.0 ]), 0.0, places=2) engine, = PFAEngine.fromYaml(''' input: "null" output: {type: array, items: {type: array, items: double}} action: la.sub: - type: {type: array, items: {type: array, items: double}} value: [[1, 2, 3], [4, 5, 6], [7, 8, 9]] - type: {type: array, items: {type: array, items: double}} value: [[101, 102, 103], [104, 105, 106], [107, 108, 109]] ''') self.assertAlmostEqual( self.chi2(engine.action(None), [[-100.0, -100.0, -100.0], [-100.0, -100.0, -100.0], [-100.0, -100.0, -100.0]]), 0.0, places=2)
def testSubMaps(self): engine, = PFAEngine.fromYaml(''' input: "null" output: {type: map, values: double} action: la.sub: - type: {type: map, values: double} value: {one: 1, two: 2, three: 3, four: 4, five: 5} - type: {type: map, values: double} value: {one: 101, two: 102, three: 103, four: 104, five: 105, six: 999} ''') self.assertAlmostEqual(self.chi2Vector( engine.action(None), { "one": -100.0, "two": -100.0, "three": -100.0, "four": -100.0, "five": -100.0, "six": -999.0 }), 0.0, places=2) engine, = PFAEngine.fromYaml(''' input: "null" output: {type: map, values: {type: map, values: double}} action: la.sub: - type: {type: map, values: {type: map, values: double}} value: {uno: {one: 1, two: 2, three: 3}, dos: {one: 4, two: 5, three: 6}, tres: {one: 7, two: 8, three: 9}} - type: {type: map, values: {type: map, values: double}} value: {uno: {one: 101, two: 102, three: 103}, dos: {one: 104, two: 105, three: 106}, tres: {one: 107, two: 108, three: 109, four: 999.0}} ''') self.assertAlmostEqual(self.chi2( engine.action(None), { "uno": { "one": -100.0, "two": -100.0, "three": -100.0, "four": 0.0 }, "dos": { "one": -100.0, "two": -100.0, "three": -100.0, "four": 0.0 }, "tres": { "one": -100.0, "two": -100.0, "three": -100.0, "four": -999.0 } }), 0.0, places=2)