class RandomUUID(LibFcn): name = prefix + "uuid" sig = Sig([], P.String(), Lifespan(None, PFAVersion(0, 7, 2), PFAVersion(0, 9, 0), "use rand.uuid4 instead")) errcodeBase = 34100 def __call__(self, state, scope, pos, paramTypes): return "{0:08x}-{1:04x}-4{2:03x}-8{3:03x}-{4:016x}".format( state.rand.getrandbits(32), state.rand.getrandbits(16), state.rand.getrandbits(12), state.rand.getrandbits(12), state.rand.getrandbits(64))
class ReducedChi2(LibFcn): name = prefix + "reducedChi2" sig = Sig( [{ "state_": P.WildRecord("A", { "chi2": P.Double(), "DOF": P.Int() }) }], P.Double(), Lifespan(None, PFAVersion(0, 7, 2), PFAVersion(0, 9, 0), "use test.reducedChi2 instead")) errcodeBase = 31060 def __call__(self, state, scope, pos, paramTypes, state_): return float(state_["chi2"] / state_["DOF"])
class Chi2Prob(LibFcn): name = prefix + "chi2Prob" sig = Sig( [{ "state_": P.WildRecord("A", { "chi2": P.Double(), "DOF": P.Int() }) }], P.Double(), Lifespan(None, PFAVersion(0, 7, 2), PFAVersion(0, 9, 0), "use test.chi2Prob instead")) errcodeBase = 31070 def __call__(self, state, scope, pos, paramTypes, state_): return float( Chi2Distribution(state_["DOF"], self.name, self.errcodeBase, pos).CDF(state_["chi2"]))
class Residual(LibFcn): name = prefix + "residual" sig = Sigs([ Sig([{ "observation": P.Double() }, { "prediction": P.Double() }], P.Double(), Lifespan(None, PFAVersion(0, 7, 2), PFAVersion(0, 9, 0), "use test.residual instead")), Sig([{ "observation": P.Array(P.Double()) }, { "prediction": P.Array(P.Double()) }], P.Array(P.Double()), Lifespan(None, PFAVersion(0, 7, 2), PFAVersion(0, 9, 0), "use test.residual instead")), Sig([{ "observation": P.Map(P.Double()) }, { "prediction": P.Map(P.Double()) }], P.Map(P.Double()), Lifespan(None, PFAVersion(0, 7, 2), PFAVersion(0, 9, 0), "use test.residual instead")) ]) errcodeBase = 31020 def __call__(self, state, scope, pos, paramTypes, observation, prediction): if isinstance(observation, dict): if len(observation) != len(prediction): raise PFARuntimeException("misaligned prediction", self.errcodeBase + 0, self.name, pos) result = {} for k, o in observation.items(): try: result[k] = o - prediction[k] except KeyError: raise PFARuntimeException("misaligned prediction", self.errcodeBase + 0, self.name, pos) return result elif isinstance(observation, (tuple, list)): try: result = [ float(o - p) for o, p in zip(observation, prediction) ] except: raise PFARuntimeException("misaligned prediction", self.errcodeBase + 0, self.name, pos) return result else: return float(observation - prediction)
class UpdateChi2(LibFcn): name = prefix + "updateChi2" sig = Sigs([ Sig([{ "pull": P.Double() }, { "state_": P.WildRecord("A", { "chi2": P.Double(), "DOF": P.Int() }) }], P.Wildcard("A"), Lifespan(None, PFAVersion(0, 7, 2), PFAVersion(0, 9, 0), "use test.updateChi2 instead")), Sig([{ "pull": P.Array(P.Double()) }, { "state_": P.WildRecord("A", { "chi2": P.Double(), "DOF": P.Int() }) }], P.Wildcard("A"), Lifespan(None, PFAVersion(0, 7, 2), PFAVersion(0, 9, 0), "use test.updateChi2 instead")), Sig([{ "pull": P.Map(P.Double()) }, { "state_": P.WildRecord("A", { "chi2": P.Double(), "DOF": P.Int() }) }], P.Wildcard("A"), Lifespan(None, PFAVersion(0, 7, 2), PFAVersion(0, 9, 0), "use test.updateChi2 instead")) ]) errcodeBase = 31050 def __call__(self, state, scope, pos, paramTypes, pull, state_): if isinstance(pull, float): return update(pull * pull, state_) elif isinstance(pull, (tuple, list)): return update(sum([y**2 for y in pull]), state_) else: return update(sum([y**2 for y in pull.values()]), state_)
class Mahalanobis(LibFcn): name = prefix + "mahalanobis" sig = Sigs([Sig([{"observation": P.Array(P.Double())}, {"prediction": P.Array(P.Double())}, {"covariance": P.Array(P.Array(P.Double()))}], P.Double(), Lifespan(None, PFAVersion(0, 7, 2), PFAVersion(0, 9, 0), "use test.mahalanobis instead")), Sig([{"observation": P.Map(P.Double())}, {"prediction": P.Map(P.Double())}, {"covariance": P.Map(P.Map(P.Double()))}], P.Double(), Lifespan(None, PFAVersion(0, 7, 2), PFAVersion(0, 9, 0), "use test.mahalanobis instead"))]) errcodeBase = 31040 def __call__(self, state, scope, pos, paramTypes, observation, prediction, covariance): if isinstance(observation, (tuple, list)): if (len(observation) < 1): raise PFARuntimeException("too few rows/cols", self.errcodeBase + 0, self.name, pos) if (len(observation) != len(prediction)): raise PFARuntimeException("misaligned prediction", self.errcodeBase + 1, self.name, pos) if (not all(len(i)==len(covariance[0]) for i in covariance)) and (len(covariance) != len(covariance[0])): raise PFARuntimeException("misaligned covariance", self.errcodeBase + 2, self.name, pos) x = np().array([(o - p) for o, p in zip(observation, prediction)]) C = np().array(covariance) else: if (len(observation) < 1): raise PFARuntimeException("too few rows/cols", self.errcodeBase + 0, self.name, pos) if (len(observation) != len(prediction)): raise PFARuntimeException("misaligned prediction", self.errcodeBase + 1, self.name, pos) # use observation keys throughout keys = list(observation.keys()) try: x = np().array([observation[key] - prediction[key] for key in keys]) except: raise PFARuntimeException("misaligned prediction", self.errcodeBase + 1, self.name, pos) C = np().empty((len(keys), len(keys))) try: for i,k1 in enumerate(keys): for j,k2 in enumerate(keys): C[i,j] = float(covariance[k1][k2]) except: raise PFARuntimeException("misaligned covariance", self.errcodeBase + 2, self.name, pos) return float(np().sqrt(np().linalg.solve(C, x).T.dot(x)))
class Pull(LibFcn): name = prefix + "pull" sig = Sigs([Sig([{"observation": P.Double()}, {"prediction": P.Double()}, {"uncertainty": P.Double()}], P.Double(), Lifespan(None, PFAVersion(0, 7, 2), PFAVersion(0, 9, 0), "use test.pull instead")), Sig([{"observation": P.Array(P.Double())}, {"prediction": P.Array(P.Double())}, {"uncertainty": P.Array(P.Double())}], P.Array(P.Double()), Lifespan(None, PFAVersion(0, 7, 2), PFAVersion(0, 9, 0), "use test.pull instead")), Sig([{"observation": P.Map(P.Double())}, {"prediction": P.Map(P.Double())}, {"uncertainty": P.Map(P.Double())}], P.Map(P.Double()), Lifespan(None, PFAVersion(0, 7, 2), PFAVersion(0, 9, 0), "use test.pull instead"))]) errcodeBase = 31030 def __call__(self, state, scope, pos, paramTypes, observation, prediction, uncertainty): if isinstance(observation, dict): if len(observation) != len(prediction): raise PFARuntimeException("misaligned prediction", self.errcodeBase + 0, self.name, pos) if len(observation) != len(uncertainty): raise PFARuntimeException("misaligned uncertainty", self.errcodeBase + 1, self.name, pos) result = {} for k, o in list(observation.items()): try: p = prediction[k] except KeyError: raise PFARuntimeException("misaligned prediction", self.errcodeBase + 0, self.name, pos) try: u = uncertainty[k] except KeyError: raise PFARuntimeException("misaligned uncertainty", self.errcodeBase + 1, self.name, pos) try: result[k] = (o - p) / u except ZeroDivisionError: result[k] = float("nan") return result elif isinstance(observation, (tuple, list)): if len(observation) != len(prediction): raise PFARuntimeException("misaligned prediction", self.errcodeBase + 0, self.name, pos) if len(observation) != len(uncertainty): raise PFARuntimeException("misaligned prediction", self.errcodeBase + 0, self.name, pos) return [float((o - p)/u) for o, p, u in zip(observation, prediction, uncertainty)] else: return float((observation - prediction)/uncertainty)
class Number(LibFcn): name = prefix + "number" sig = Sigs([ Sig([{ "x": P.Long() }], P.String(), Lifespan(None, PFAVersion(0, 7, 2), PFAVersion(0, 9, 0), "use s.int for integers")), Sig([{ "x": P.Long() }, { "width": P.Int() }, { "zeroPad": P.Boolean() }], P.String(), Lifespan(None, PFAVersion(0, 7, 2), PFAVersion(0, 9, 0), "use s.int for integers")), Sig([{ "x": P.Double() }, { "width": P.Union([P.Int(), P.Null()]) }, { "precision": P.Union([P.Int(), P.Null()]) }], P.String()), Sig([{ "x": P.Double() }, { "width": P.Union([P.Int(), P.Null()]) }, { "precision": P.Union([P.Int(), P.Null()]) }, { "minNoExp": P.Double() }, { "maxNoExp": P.Double() }], P.String()) ]) errcodeBase = 39120 def __call__(self, state, scope, pos, paramTypes, *args): if len(args) == 1 and paramTypes[0] in ("int", "long"): x, = args return "{0:d}".format(x) elif len(args) == 3 and paramTypes[0] in ("int", "long"): x, width, zeroPad = args if not zeroPad: if width < 0: formatStr = "{0:<" + str(-width) + "d}" elif width == 0: formatStr = "" else: formatStr = "{0:" + str(width) + "d}" else: if width < 0: raise PFARuntimeException( "negative width cannot be used with zero-padding", self.errcodeBase + 0, self.name, pos) elif width == 0: formatStr = "" else: formatStr = "{0:0" + str(width) + "d}" return formatStr.format(x) else: if len(args) == 3: x, width, precision = args minNoExp, maxNoExp = 0.0001, 100000 else: x, width, precision, minNoExp, maxNoExp = args if width is None: widthStr = "" else: if isinstance(width, dict): width = width["int"] if width < 0: widthStr = "<" + str(-width) else: widthStr = str(width) if precision is None: precisionStr = ".6" else: if isinstance(precision, dict): precision = precision["int"] if precision < 0: raise PFARuntimeException("negative precision", self.errcodeBase + 1, self.name, pos) else: precisionStr = "." + str(precision) if x == 0.0: x = 0.0 # drop sign bit from zero v = abs(x) if v == 0.0 or (v >= minNoExp and v <= maxNoExp): conv = "f" else: conv = "e" formatStr = "{0:" + widthStr + precisionStr + conv + "}" result = formatStr.format(x) if precision is None: m = re.search("\.[0-9]+?(0+) *$", result) if m is None: m = re.search("\.[0-9]+?(0+)e", result) if m is not None: start, end = m.regs[1] numChanged = end - start result = result[:start] + result[end:] if width is not None: if width < 0: actual, target = len(result), -width if actual < target: result = result + (" " * (target - actual)) elif width == 0: result = "" else: actual, target = len(result), width if actual < target: result = (" " * (target - actual)) + result return result