Пример #1
0
class Symmetric(LibFcn):
    name = prefix + "symmetric"
    sig = Sigs([Sig([{"x": P.Array(P.Array(P.Double()))}, {"tol": P.Double()}], P.Boolean()),
                Sig([{"x": P.Map(P.Map(P.Double()))}, {"tol": P.Double()}], P.Boolean())])
    errcodeBase = 24100
    @staticmethod
    def same(x, y, tol):
        if math.isinf(x) and math.isinf(y) and ((x > 0.0 and y > 0.0) or (x < 0.0 and y < 0.0)):
            return True
        elif math.isnan(x) and math.isnan(y):
            return True
        elif not math.isinf(x) and not math.isnan(x) and not math.isinf(y) and not math.isnan(y):
            return abs(x - y) < tol
        else:
            return False
    def __call__(self, state, scope, pos, paramTypes, x, tol):
        if isinstance(x, (list, tuple)) and all(isinstance(xi, (list, tuple)) for xi in x):
            rows = len(x)
            if rows < 1:
                raise PFARuntimeException("too few rows/cols", self.errcodeBase + 0, self.name, pos)
            cols = len(x[0])
            if cols < 1:
                raise PFARuntimeException("too few rows/cols", self.errcodeBase + 0, self.name, pos)
            if raggedArray(x):
                raise PFARuntimeException("ragged columns", self.errcodeBase + 1, self.name, pos)
            if rows != cols:
                raise PFARuntimeException("non-square matrix", self.errcodeBase + 2, self.name, pos)
            return all(all(self.same(x[i][j], x[j][i], tol) for j in range(cols)) for i in range(rows))

        elif isinstance(x, dict) and all(isinstance(x[i], dict) for i in x.keys()):
            keys = list(rowKeys(x).union(colKeys(x)))
            if len(keys) < 1 or all(len(row) == 0 for row in x.values()):
                raise PFARuntimeException("too few rows/cols", self.errcodeBase + 0, self.name, pos)
            return all(all(self.same(x.get(i, {}).get(j, 0.0), x.get(j, {}).get(i, 0.0), tol) for j in keys) for i in keys)
Пример #2
0
class ContainsValue(LibFcn):
    name = prefix + "containsValue"
    sig = Sigs([
        Sig([{
            "m": P.Map(P.Wildcard("A"))
        }, {
            "value": P.Wildcard("A")
        }], P.Boolean()),
        Sig([{
            "m": P.Map(P.Wildcard("A"))
        }, {
            "fcn": P.Fcn([P.Wildcard("A")], P.Boolean())
        }], P.Boolean())
    ])
    errcodeBase = 26040

    def __call__(self, state, scope, pos, paramTypes, m, value):
        if callable(value):
            for v in m.values():
                if callfcn(state, scope, value, [v]):
                    return True
            return False
        else:
            for v in m.values():
                if v == value:
                    return True
            return False
Пример #3
0
class ZipMapWithKey(LibFcn):
    name = prefix + "zipmapWithKey"
    sig = Sigs([
        Sig([{
            "a": P.Map(P.Wildcard("A"))
        }, {
            "b": P.Map(P.Wildcard("B"))
        }, {
            "fcn":
            P.Fcn([P.String(), P.Wildcard("A"),
                   P.Wildcard("B")], P.Wildcard("Z"))
        }], P.Map(P.Wildcard("Z"))),
        Sig([{
            "a": P.Map(P.Wildcard("A"))
        }, {
            "b": P.Map(P.Wildcard("B"))
        }, {
            "c": P.Map(P.Wildcard("C"))
        }, {
            "fcn":
            P.Fcn([
                P.String(),
                P.Wildcard("A"),
                P.Wildcard("B"),
                P.Wildcard("C")
            ], P.Wildcard("Z"))
        }], P.Map(P.Wildcard("Z"))),
        Sig([{
            "a": P.Map(P.Wildcard("A"))
        }, {
            "b": P.Map(P.Wildcard("B"))
        }, {
            "c": P.Map(P.Wildcard("C"))
        }, {
            "d": P.Map(P.Wildcard("D"))
        }, {
            "fcn":
            P.Fcn([
                P.String(),
                P.Wildcard("A"),
                P.Wildcard("B"),
                P.Wildcard("C"),
                P.Wildcard("D")
            ], P.Wildcard("Z"))
        }], P.Map(P.Wildcard("Z")))
    ])
    errcodeBase = 26380

    def __call__(self, state, scope, pos, paramTypes, *args):
        fcn = args[-1]
        maps = args[:-1]
        keys = maps[0].keys()
        for m in maps:
            if keys != m.keys():
                raise PFARuntimeException("misaligned maps",
                                          self.errcodeBase + 0, self.name, pos)
        out = {}
        for k in keys:
            out[k] = callfcn(state, scope, fcn, [k] + [x[k] for x in maps])
        return out
Пример #4
0
class FindGroupsFirst(LibFcn):
    name = prefix + "findgroupsfirst"
    sig = Sigs([
        Sig([{
            "haystack": P.String()
        }, {
            "pattern": P.String()
        }], P.Array(P.String())),
        Sig([{
            "haystack": P.Bytes()
        }, {
            "pattern": P.Bytes()
        }], P.Array(P.Bytes()))
    ])
    errcodeBase = 35080

    def __call__(self, state, scope, pos, paramTypes, haystack, pattern):
        haystack, pattern, to = convert(haystack, pattern, paramTypes[0])
        re = Regexer(haystack, pattern, self.errcodeBase + 0, self.name, pos)
        start = 0
        found = re.search(start)
        out = []
        region = re.getRegion()
        if (found):
            for i in range(0, re.groupsFound()):
                out.append(to(haystack[region.beg[i]:region.end[i]]))
        else:
            out = []
        re.free()
        return out
Пример #5
0
class Bin(LibFcn):
    name = prefix + "bin"
    sig = Sigs([Sig([{"x": P.Double()}, {"numbins": P.Int()}, {"low": P.Double()}, {"high": P.Double()}], P.Int()),
                Sig([{"x": P.Double()}, {"origin": P.Double()}, {"width": P.Double()}], P.Int())])
    errcodeBase = 22000
    def __call__(self, state, scope, pos, paramTypes, x, *args):
        if len(args) == 3:
            numbins, low, high = args
            if low >= high or math.isnan(low) or math.isnan(high):
                raise PFARuntimeException("bad histogram range", self.errcodeBase + 0, self.name, pos)
            if numbins < 1:
                raise PFARuntimeException("bad histogram scale", self.errcodeBase + 1, self.name, pos)
            if math.isnan(x) or x < low or x >= high:
                raise PFARuntimeException("x out of range", self.errcodeBase + 2, self.name, pos)

            out = int(math.floor(numbins * div((x - low), (high - low))))

            if out < 0 or out >= numbins:
                raise PFARuntimeException("x out of range", self.errcodeBase + 2, self.name, pos)
            return out
        else:
            origin, width = args
            if math.isnan(origin) or math.isinf(origin):
                raise PFARuntimeException("bad histogram range", self.errcodeBase + 0, self.name, pos)
            if width <= 0.0 or math.isnan(width):
                raise PFARuntimeException("bad histogram scale", self.errcodeBase + 1, self.name, pos)
            if math.isnan(x) or math.isinf(x):
                raise PFARuntimeException("x out of range", self.errcodeBase + 2, self.name, pos)
            else:
                return int(math.floor(div((x - origin), width)))
Пример #6
0
class RIndex(LibFcn):
    name = prefix + "rindex"
    sig = Sigs([
        Sig([{
            "haystack": P.String()
        }, {
            "pattern": P.String()
        }], P.Array(P.Int())),
        Sig([{
            "haystack": P.Bytes()
        }, {
            "pattern": P.Bytes()
        }], P.Array(P.Int()))
    ])
    errcodeBase = 35030

    def __call__(self, state, scope, pos, paramTypes, haystack, pattern):
        haystack, pattern, to = convert(haystack, pattern, paramTypes[0])
        re = Regexer(haystack, pattern, self.errcodeBase + 0, self.name, pos)
        found = re.search(0)
        region = re.getRegion()
        start = 0
        if found:
            while found:
                region = re.getRegion()
                start = region.end[0]
                found = re.search(start)
            out = [region.beg[0], region.end[0]]
        else:
            out = []
        re.free()
        return out
Пример #7
0
class FindAll(LibFcn):
    name = prefix + "findall"
    sig = Sigs([
        Sig([{
            "haystack": P.String()
        }, {
            "pattern": P.String()
        }], P.Array(P.String())),
        Sig([{
            "haystack": P.Bytes()
        }, {
            "pattern": P.Bytes()
        }], P.Array(P.Bytes()))
    ])
    errcodeBase = 35060

    def __call__(self, state, scope, pos, paramTypes, haystack, pattern):
        haystack, pattern, to = convert(haystack, pattern, paramTypes[0])
        re = Regexer(haystack, pattern, self.errcodeBase + 0, self.name, pos)
        found = re.search(0)
        region = re.getRegion()
        start = region.end[0]
        out = []
        if found:
            while found:
                region = re.getRegion()
                start = region.end[0]
                out.append(to(haystack[region.beg[0]:region.end[0]]))
                found = re.search(start)
        else:
            out = []
        re.free()
        return out
Пример #8
0
class MapApply(LibFcn):
    name = prefix + "map"
    sig = Sigs([
        Sig([{
            "x": P.Array(P.Array(P.Double()))
        }, {
            "fcn": P.Fcn([P.Double()], P.Double())
        }], P.Array(P.Array(P.Double()))),
        Sig([{
            "x": P.Map(P.Map(P.Double()))
        }, {
            "fcn": P.Fcn([P.Double()], P.Double())
        }], P.Map(P.Map(P.Double())))
    ])
    errcodeBase = 24000

    def __call__(self, state, scope, pos, paramTypes, x, fcn):
        if isinstance(x, (list, tuple)) and all(
                isinstance(xi, (list, tuple)) for xi in x):
            return [[callfcn(state, scope, fcn, [xj]) for xj in xi]
                    for xi in x]

        elif isinstance(x, dict) and all(
                isinstance(x[i], dict) for i in list(x.keys())):
            return dict((i,
                         dict((j, callfcn(state, scope, fcn, [xj]))
                              for j, xj in list(xi.items())))
                        for i, xi in list(x.items()))
Пример #9
0
class Trace(LibFcn):
    name = prefix + "trace"
    sig = Sigs([
        Sig([{
            "x": P.Array(P.Array(P.Double()))
        }], P.Double()),
        Sig([{
            "x": P.Map(P.Map(P.Double()))
        }], P.Double())
    ])
    errcodeBase = 24080

    def __call__(self, state, scope, pos, paramTypes, x):
        if isinstance(x, (list, tuple)) and all(
                isinstance(xi, (list, tuple)) for xi in x):
            rows = len(x)
            if rows == 0:
                return 0.0
            else:
                cols = len(x[0])
                if raggedArray(x):
                    raise PFARuntimeException("ragged columns",
                                              self.errcodeBase + 0, self.name,
                                              pos)
                return sum(x[i][i] for i in range(min(rows, cols)))

        elif isinstance(x, dict) and all(
                isinstance(x[i], dict) for i in list(x.keys())):
            keys = rowKeys(x).intersection(colKeys(x))
            return sum(x[i][i] for i in keys)
Пример #10
0
class IsNum(LibFcn):
    name = prefix + "isnum"
    sig = Sigs([Sig([{"x": P.Float()}], P.Boolean()),
                Sig([{"x": P.Double()}], P.Boolean())])
    errcodeBase = 21040
    def __call__(self, state, scope, pos, paramTypes, x):
        return not math.isnan(x) and not math.isinf(x)
Пример #11
0
class Bernoulli(LibFcn):
    name = prefix + "bernoulli"
    sig = Sigs([
        Sig([{
            "datum": P.Array(P.String())
        }, {
            "classModel": P.Map(P.Double())
        }], P.Double()),
        Sig([{
            "datum": P.Array(P.String())
        }, {
            "classModel": P.WildRecord("C", {"values": P.Map(P.Double())})
        }], P.Double())
    ])

    errcodeBase = 10020

    def __call__(self, state, scope, pos, paramTypes, datum, classModel):
        if paramTypes[1]["type"] == "record":
            classModel = classModel["values"]

        ll = 0.0
        for v in list(classModel.values()):
            if (v <= 0.0) or (v >= 1.0):
                raise PFARuntimeException(
                    "probability in classModel cannot be less than 0 or greater than 1",
                    self.errcodeBase + 0, self.name, pos)
            ll += math.log(1.0 - v)
        for item in datum:
            p = classModel.get(item, None)
            if p is not None:
                ll += math.log(p) - math.log(1.0 - p)
        return ll
Пример #12
0
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 = 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)))
Пример #13
0
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)
Пример #14
0
class Truncate(LibFcn):
    name = prefix + "truncate"
    sig = Sigs([Sig([{"x": P.Array(P.Array(P.Double()))}, {"keep": P.Int()}], P.Array(P.Array(P.Double()))),
                Sig([{"x": P.Map(P.Map(P.Double()))}, {"keep": P.Array(P.String())}], P.Map(P.Map(P.Double())))])
    errcodeBase = 24120
    def __call__(self, state, scope, pos, paramTypes, x, keep):
        if keep < 0:
            keep = 0

        if isinstance(x, (list, tuple)) and all(isinstance(xi, (list, tuple)) for xi in x):
            rows = len(x)
            if rows < 1:
                raise PFARuntimeException("too few rows/cols", self.errcodeBase + 0, self.name, pos)
            cols = len(x[0])
            if cols < 1:
                raise PFARuntimeException("too few rows/cols", self.errcodeBase + 0, self.name, pos)
            if raggedArray(x):
                raise PFARuntimeException("ragged columns", self.errcodeBase + 1, self.name, pos)
            return x[:keep]

        elif isinstance(x, dict) and all(isinstance(x[i], dict) for i in x.keys()):
            rows = rowKeys(x)
            cols = colKeys(x)
            if len(rows) < 1 or len(cols) < 1:
                raise PFARuntimeException("too few rows/cols", self.errcodeBase + 0, self.name, pos)
            return dict((k, x[k]) for k in rows if k in keep)
Пример #15
0
class Count(LibFcn):
    name = prefix + "count"
    sig = Sigs([Sig([{"haystack": P.Array(P.Wildcard("A"))}, {"needle": P.Array(P.Wildcard("A"))}], P.Int()),
                Sig([{"haystack": P.Array(P.Wildcard("A"))}, {"needle": P.Wildcard("A")}], P.Int()),
                Sig([{"a": P.Array(P.Wildcard("A"))}, {"predicate": P.Fcn([P.Wildcard("A")], P.Boolean())}], P.Int())])
    errcodeBase = 15080
    def __call__(self, state, scope, pos, paramTypes, haystack, needle):
        if len(haystack) == 0:
            return 0
        else:
            if isinstance(needle, (list, tuple)):
                if len(needle) == 0:
                    return 0
                else:
                    count = 0
                    for start in range(len(haystack) - len(needle) + 1):
                        if needle == haystack[start:(start + len(needle))]:
                            count += 1
                    return count
            elif callable(needle):
                count = 0
                for item in haystack:
                    if callfcn(state, scope, needle, [item]):
                        count += 1
                return count
            else:
                return haystack.count(needle)
Пример #16
0
class Split(LibFcn):
    name = prefix + "split"
    sig = Sigs([Sig([{"haystack": P.String()}, {"pattern": P.String()}], P.Array(P.String())),
                Sig([{"haystack": P.Bytes()}, {"pattern": P.Bytes()}], P.Array(P.Bytes()))])
    errcodeBase = 35130
    def __call__(self, state, scope, pos, paramTypes, haystack, pattern):
        haystack, pattern, to = convert(haystack, pattern, paramTypes[0])
        re = Regexer(haystack, pattern, self.errcodeBase + 0, self.name, pos)
        out = []
        start = 0
        found = re.search(start)
        region = re.getRegion()
        beg = 0
        end = region.beg[0]
        if (end == 0):
            flag = True
        else:
            flag = False
        if found:
            while found:
                out.append(to(haystack[beg:end]))
                beg = region.end[0]
                found = re.search(beg)
                region = re.getRegion()
                end = region.beg[0]
            if beg != len(haystack):
                out.append(to(haystack[beg:]))
            if flag:
                out = out[1:]
        else:
            out = [to(haystack)]
        re.free()
        return out
Пример #17
0
class Count(LibFcn):
    name = prefix + "count"
    sig = Sigs([
        Sig([{
            "haystack": P.String()
        }, {
            "pattern": P.String()
        }], P.Int()),
        Sig([{
            "haystack": P.Bytes()
        }, {
            "pattern": P.Bytes()
        }], P.Int())
    ])
    errcodeBase = 35020

    def __call__(self, state, scope, pos, paramTypes, haystack, pattern):
        haystack, pattern, to = convert(haystack, pattern, paramTypes[0])
        re = Regexer(haystack, pattern, self.errcodeBase + 0, self.name, pos)
        total = 0
        found = re.search(0)
        region = re.getRegion()
        start = region.end[0]
        while found:
            total += 1
            found = re.search(start)
            region = re.getRegion()
            start = region.end[0]
        re.free()
        return total
Пример #18
0
class ReplaceAll(LibFcn):
    name = prefix + "replaceall"
    sig = Sigs([Sig([{"haystack": P.String()}, {"pattern": P.String()}, {"replacement": P.String()}], P.String()),
                Sig([{"haystack": P.Bytes()}, {"pattern": P.Bytes()}, {"replacement": P.Bytes()}], P.Bytes())])
    errcodeBase = 35140
    def __call__(self, state, scope, pos, paramTypes, haystack, pattern, replacement):
        original = haystack
        haystack, pattern, to = convert(haystack, pattern, paramTypes[0])
        re = Regexer(haystack, pattern, self.errcodeBase + 0, self.name, pos)
        found = re.search(0)
        region = re.getRegion()
        beg = 0
        end = region.beg[0]
        out = ""
        if found:
            while found:
                out = out + to(haystack[beg:end]) + replacement
                beg = region.end[0]
                found = re.search(beg)
                region = re.getRegion()
                end = region.beg[0]
            if beg != len(haystack):
                out = out + to(haystack[beg:])
        else:
            out = original
        re.free()
        return out
Пример #19
0
class Groups(LibFcn):
    name = prefix + "groups"
    sig = Sigs([
        Sig([{
            "haystack": P.String()
        }, {
            "pattern": P.String()
        }], P.Array(P.Array(P.Int()))),
        Sig([{
            "haystack": P.Bytes()
        }, {
            "pattern": P.Bytes()
        }], P.Array(P.Array(P.Int())))
    ])
    errcodeBase = 35040

    def __call__(self, state, scope, pos, paramTypes, haystack, pattern):
        haystack, pattern, to = convert(haystack, pattern, paramTypes[0])
        re = Regexer(haystack, pattern, self.errcodeBase + 0, self.name, pos)
        found = re.search(0)
        region = re.getRegion()
        start = region.end[0]
        out = []
        if found:
            for i in range(0, re.groupsFound()):
                out.append([region.beg[i], region.end[i]])
        else:
            out = []
        re.free()
        return out
Пример #20
0
class ToLong(LibFcn):
    name = prefix + "long"
    sig = Sigs([
        Sig([{
            "x": P.Int()
        }], P.Long()),
        Sig([{
            "x": P.Long()
        }], P.Long()),
        Sig([{
            "x": P.Float()
        }], P.Long()),
        Sig([{
            "x": P.Double()
        }], P.Long())
    ])
    errcodeBase = 17030

    def __call__(self, state, scope, pos, paramTypes, x):
        try:
            if isinstance(x, float):
                if math.isnan(x):
                    raise OverflowError
                else:
                    out = int(math.floor(x + 0.5))
            else:
                out = x
            if LONG_MIN_VALUE <= out <= LONG_MAX_VALUE:
                return out
            else:
                raise OverflowError
        except OverflowError:
            raise PFARuntimeException("long overflow", self.errcodeBase + 0,
                                      self.name, pos)
Пример #21
0
class FindFirst(LibFcn):
    name = prefix + "findfirst"
    sig = Sigs([
        Sig([{
            "haystack": P.String()
        }, {
            "pattern": P.String()
        }], P.Union([P.String(), P.Null()])),
        Sig([{
            "haystack": P.Bytes()
        }, {
            "pattern": P.Bytes()
        }], P.Union([P.Bytes(), P.Null()]))
    ])
    errcodeBase = 35070

    def __call__(self, state, scope, pos, paramTypes, haystack, pattern):
        haystack, pattern, to = convert(haystack, pattern, paramTypes[0])
        re = Regexer(haystack, pattern, self.errcodeBase + 0, self.name, pos)
        found = re.search(0)
        if found:
            region = re.getRegion()
            out = to(haystack[region.beg[0]:region.end[0]])
        else:
            out = None
        re.free()
        if out is not None:
            return {paramTypes[0]: out}
        else:
            return out
Пример #22
0
class FanoutBoolean(LibFcn):
    name = prefix + "fanoutBoolean"
    sig = Sigs([
        Sig([{
            "x": P.WildEnum("A")
        }], P.Array(P.Boolean())),
        Sig([{
            "x": P.String()
        }, {
            "dictionary": P.Array(P.String())
        }, {
            "outOfRange": P.Boolean()
        }], P.Array(P.Boolean())),
        Sig([{
            "x": P.Int()
        }, {
            "minimum": P.Int()
        }, {
            "maximum": P.Int()
        }, {
            "outOfRange": P.Boolean()
        }], P.Array(P.Boolean()))
    ])
    errcodeBase = 17060

    def __call__(self, state, scope, pos, paramTypes, x, *args):
        if len(args) == 0:
            return fanoutEnum(x, paramTypes[0]["symbols"])
        elif len(args) == 2:
            if len(args[0]) != len(set(args[0])):
                raise PFARuntimeException("non-distinct values in dictionary",
                                          self.errcodeBase + 0, self.name, pos)
            return fanoutString(x, args[0], args[1])
        elif len(args) == 3:
            return fanoutInt(x, args[0], args[1], args[2])
Пример #23
0
class ReplaceFirst(LibFcn):
    name = prefix + "replacefirst"
    sig = Sigs([
        Sig([{
            "haystack": P.String()
        }, {
            "pattern": P.String()
        }, {
            "replacement": P.String()
        }], P.String()),
        Sig([{
            "haystack": P.Bytes()
        }, {
            "pattern": P.Bytes()
        }, {
            "replacement": P.Bytes()
        }], P.Bytes())
    ])
    errcodeBase = 35110

    def __call__(self, state, scope, pos, paramTypes, haystack, pattern,
                 replacement):
        haystack, pattern, to = convert(haystack, pattern, paramTypes[0])
        re = Regexer(haystack, pattern, self.errcodeBase + 0, self.name, pos)
        found = re.search(0)
        region = re.getRegion()
        if found:
            out = to(haystack[:region.beg[0]]) + replacement + to(
                haystack[region.end[0]:])
        else:
            out = to(haystack)
        re.free()
        return out
Пример #24
0
class ErrorOnNonNum(LibFcn):
    name = prefix + "errorOnNonNum"
    sig = Sigs([
        Sig([{
            "x": P.Float()
        }], P.Float()),
        Sig([{
            "x": P.Double()
        }], P.Double())
    ])
    errcodeBase = 21050

    def __call__(self, state, scope, pos, paramTypes, x):
        if math.isnan(x):
            raise PFARuntimeException("encountered nan", self.errcodeBase + 0,
                                      self.name, pos)
        elif math.isinf(x):
            if x > 0.0:
                raise PFARuntimeException("encountered +inf",
                                          self.errcodeBase + 1, self.name, pos)
            else:
                raise PFARuntimeException("encountered -inf",
                                          self.errcodeBase + 2, self.name, pos)
        else:
            return x
Пример #25
0
class BallR(LibFcn):
    name = prefix + "ballR"
    sig = Sigs([
        Sig([{
            "r": P.Double()
        }, {
            "datum": P.Array(P.Double())
        }, {
            "codebook": P.Array(P.Array(P.Double()))
        }], P.Array(P.Array(P.Double()))),
        Sig([{
            "r": P.Double()
        }, {
            "datum": P.Wildcard("A")
        }, {
            "codebook": P.Array(P.Wildcard("B"))
        }, {
            "metric": P.Fcn([P.Wildcard("A"), P.Wildcard("B")], P.Double())
        }], P.Array(P.Wildcard("B")))
    ])
    errcodeBase = 30020

    def __call__(self, state, scope, pos, paramTypes, r, datum, codebook,
                 *args):
        if len(args) == 1:
            metric, = args
            distances = [
                callfcn(state, scope, metric, [datum, x]) for x in codebook
            ]
        else:
            distances = [
                math.sqrt(sum((di - xi)**2 for di, xi in zip(datum, x)))
                for x in codebook
            ]
        return [x for x, d in zip(codebook, distances) if d < r]
Пример #26
0
class Euclidean(MetricWithMissingValues):
    name = prefix + "euclidean"
    sig = Sigs([
        Sig([{
            "similarity":
            P.Fcn([P.Wildcard("A"), P.Wildcard("B")], P.Double())
        }, {
            "x": P.Array(P.Union([P.Null(), P.Wildcard("A")]))
        }, {
            "y": P.Array(P.Union([P.Null(), P.Wildcard("B")]))
        }], P.Double()),
        Sig([{
            "similarity":
            P.Fcn([P.Wildcard("A"), P.Wildcard("B")], P.Double())
        }, {
            "x": P.Array(P.Union([P.Null(), P.Wildcard("A")]))
        }, {
            "y": P.Array(P.Union([P.Null(), P.Wildcard("B")]))
        }, {
            "missingWeight": P.Array(P.Double())
        }], P.Double())
    ])
    errcodeBase = 28030

    def increment(self, tally, x):
        return tally + x**2

    def finalize(self, x):
        return math.sqrt(x)
Пример #27
0
class Add(LibFcn, ObjKey):
    name = prefix + "add"
    sig = Sigs([
        Sig([{
            "m": P.Map(P.Wildcard("A"))
        }, {
            "key": P.String()
        }, {
            "value": P.Wildcard("A")
        }], P.Map(P.Wildcard("A"))),
        Sig([{
            "m": P.Map(P.Wildcard("A"))
        }, {
            "item": P.Wildcard("A")
        }], P.Map(P.Wildcard("A")))
    ])
    errcodeBase = 26050

    def __call__(self, state, scope, pos, paramTypes, m, *args):
        if len(args) == 2:
            key, value = args
            return dict(m, **{key: value})
        else:
            item, = args
            key = self.toKey(item, jsonNodeToAvroType(paramTypes[1]))
            return dict(m, **{key: item})
Пример #28
0
class Taxicab(MetricWithMissingValues):
    name = prefix + "taxicab"
    sig = Sigs([
        Sig([{
            "similarity":
            P.Fcn([P.Wildcard("A"), P.Wildcard("B")], P.Double())
        }, {
            "x": P.Array(P.Union([P.Null(), P.Wildcard("A")]))
        }, {
            "y": P.Array(P.Union([P.Null(), P.Wildcard("B")]))
        }], P.Double()),
        Sig([{
            "similarity":
            P.Fcn([P.Wildcard("A"), P.Wildcard("B")], P.Double())
        }, {
            "x": P.Array(P.Union([P.Null(), P.Wildcard("A")]))
        }, {
            "y": P.Array(P.Union([P.Null(), P.Wildcard("B")]))
        }, {
            "missingWeight": P.Array(P.Double())
        }], P.Double())
    ])
    errcodeBase = 28060

    def increment(self, tally, x):
        return tally + x

    def finalize(self, x):
        return x
Пример #29
0
class SoftMax(LibFcn):
    name = prefix + "softmax"
    sig = Sigs([
        Sig([{
            "x": P.Array(P.Double())
        }], P.Array(P.Double())),
        Sig([{
            "x": P.Map(P.Double())
        }], P.Map(P.Double()))
    ])
    errcodeBase = 25000

    def __call__(self, state, scope, pos, paramTypes, x):
        if len(x) == 0:
            raise PFARuntimeException("empty input", self.errcodeBase + 0,
                                      self.name, pos)
        if paramTypes[0]["type"] == "map":
            xx = x.copy()
            tmp = map(abs, xx.values())
            if list(xx.values())[tmp.index(max(tmp))] >= 0:
                m = max(xx.values())
            else:
                m = min(xx.values())
            denom = sum([math.exp(v - m) for v in x.values()])
            for key in x.keys():
                xx[key] = float(math.exp(xx[key] - m) / denom)
            return xx
        else:
            tmp = map(abs, x)
            if x[tmp.index(max(tmp))] >= 0:
                m = max(x)
            else:
                m = min(x)
            denom = sum([math.exp(v - m) for v in x])
            return [float(math.exp(val - m) / denom) for val in x]
Пример #30
0
class Det(LibFcn):
    name = prefix + "det"
    sig = Sigs([Sig([{"x": P.Array(P.Array(P.Double()))}], P.Double()),
                Sig([{"x": P.Map(P.Map(P.Double()))}], P.Double())])
    errcodeBase = 24090
    def __call__(self, state, scope, pos, paramTypes, x):
        if isinstance(x, (list, tuple)) and all(isinstance(xi, (list, tuple)) for xi in x):
            rows = len(x)
            if rows < 1:
                raise PFARuntimeException("too few rows/cols", self.errcodeBase + 0, self.name, pos)
            cols = len(x[0])
            if cols < 1:
                raise PFARuntimeException("too few rows/cols", self.errcodeBase + 0, self.name, pos)
            if raggedArray(x):
                raise PFARuntimeException("ragged columns", self.errcodeBase + 1, self.name, pos)
            if rows != cols:
                raise PFARuntimeException("non-square matrix", self.errcodeBase + 2, self.name, pos)
            if any(any(math.isnan(z) or math.isinf(z) for z in row) for row in x):
                return float("nan")
            else:
                return float(np().linalg.det(arraysToMatrix(x)))

        elif isinstance(x, dict) and all(isinstance(x[i], dict) for i in x.keys()):
            keys = list(rowKeys(x).union(colKeys(x)))
            if len(keys) < 1 or all(len(row) == 0 for row in x.values()):
                raise PFARuntimeException("too few rows/cols", self.errcodeBase + 0, self.name, pos)
            if any(any(math.isnan(z) or math.isinf(z) for z in row.values()) for row in x.values()):
                return float("nan")
            else:
                return float(np().linalg.det(mapsToMatrix(x, keys, keys)))