def __call__(self, state, scope, paramTypes, data, clusters, metric, update): if len(data) == 0: raise PFARuntimeException("no data") centers = [x["center"] for x in clusters] length = len(clusters) if length == 0: raise PFARuntimeException("no clusters") matched = [[] for i in xrange(length)] for datum in data: besti = 0 bestCenter = None bestDistance = 0.0 i = 0 while i < length: thisCenter = centers[i] thisDistance = callfcn(state, scope, metric, [datum, thisCenter]) if bestCenter is None or thisDistance < bestDistance: besti = i bestCenter = thisCenter bestDistance = thisDistance i += 1 matched[besti].append(datum) out = [] for i, matchedData in enumerate(matched): if len(matchedData) == 0: out.append(clusters[i]) else: out.append(callfcn(state, scope, update, [matchedData, clusters[i]])) return out
def __call__(self, state, scope, pos, paramTypes, datum, model, kernel): const = model["const"] negClass = model["negClass"] posClass = model["posClass"] if len(negClass) == 0 and len(posClass) == 0: raise PFARuntimeException("no support vectors", self.errcodeBase + 0, self.name, pos) negClassScore = 0.0 for sv in negClass: supVec = sv["supVec"] if len(supVec) != len(datum): raise PFARuntimeException( "support vectors must have same length as datum", self.errcodeBase + 1, self.name, pos) coeff = sv["coeff"] negClassScore += callfcn(state, scope, kernel, [supVec, datum]) * coeff posClassScore = 0.0 for sv in posClass: supVec = sv["supVec"] if len(supVec) != len(datum): raise PFARuntimeException( "support vectors must have same length as datum", self.errcodeBase + 1, self.name, pos) coeff = sv["coeff"] posClassScore += callfcn(state, scope, kernel, [supVec, datum]) * coeff return negClassScore + posClassScore + const
def __call__(self, state, scope, pos, paramTypes, data, clusters, metric, update): if len(data) == 0: raise PFARuntimeException("no data", self.errcodeBase + 0, self.name, pos) centers = [x["center"] for x in clusters] length = len(clusters) if length == 0: raise PFARuntimeException("no clusters", self.errcodeBase + 1, self.name, pos) matched = [[] for i in range(length)] for datum in data: besti = 0 bestCenter = None bestDistance = 0.0 i = 0 while i < length: thisCenter = centers[i] thisDistance = callfcn(state, scope, metric, [datum, thisCenter]) if bestCenter is None or thisDistance < bestDistance: besti = i bestCenter = thisCenter bestDistance = thisDistance i += 1 matched[besti].append(datum) out = [] for i, matchedData in enumerate(matched): if len(matchedData) == 0: out.append(clusters[i]) else: out.append(callfcn(state, scope, update, [matchedData, clusters[i]])) return out
def __call__(self, state, scope, pos, paramTypes, similarity, x, y, p, missingWeight=None): length = len(x) if missingWeight is None: missingWeight = [1.0] * length if len(y) != length or len(missingWeight) != length: raise PFARuntimeException("dimensions of vectors do not match", self.errcodeBase + 0, self.name, pos) if math.isnan(p) or p <= 0: raise PFARuntimeException("Minkowski parameter p must be positive", self.errcodeBase + 1, self.name, pos) tally = 0.0 numer = 0.0 denom = 0.0 if math.isinf(p): for i in xrange(length): xi = x[i] yi = y[i] if xi is not None and yi is not None: if isinstance(paramTypes[1]["items"], (tuple, list)): xi, = xi.values() if isinstance(paramTypes[2]["items"], (tuple, list)): yi, = yi.values() z = callfcn(state, scope, similarity, [xi, yi]) if z > tally: tally = z denom += missingWeight[i] numer += missingWeight[i] if denom == 0.0: return float("nan") else: return tally * numer / denom else: for i in xrange(length): xi = x[i] yi = y[i] if xi is not None and yi is not None: if isinstance(paramTypes[1]["items"], (tuple, list)): xi, = xi.values() if isinstance(paramTypes[2]["items"], (tuple, list)): yi, = yi.values() tally += powLikeJava( callfcn(state, scope, similarity, [xi, yi]), p) denom += missingWeight[i] numer += missingWeight[i] if denom == 0.0: return float("nan") else: return powLikeJava(tally * numer / denom, 1.0 / p)
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 x.keys()): return dict((i, dict((j, callfcn(state, scope, fcn, [xj])) for j, xj in xi.items())) for i, xi in x.items())
def __call__(self, state, scope, pos, paramTypes, x, y, fcn): if isinstance(x, (list, tuple)) and all(isinstance(xi, (list, tuple)) for xi in x) and \ isinstance(y, (list, tuple)) and all(isinstance(yi, (list, tuple)) for yi in y): if len(x) != len(y) or any(len(xi) != len(yi) for xi, yi in zip(x, y)): raise PFARuntimeException("misaligned matrices", self.errcodeBase + 0, self.name, pos) return [[callfcn(state, scope, fcn, [xj, yj]) for xj, yj in zip(xi, yi)] for xi, yi in zip(x, y)] elif isinstance(x, dict) and all(isinstance(x[i], dict) for i in x.keys()) and \ isinstance(y, dict) and all(isinstance(y[i], dict) for i in y.keys()): rows = rowKeys(x).union(rowKeys(y)) cols = colKeys(x).union(colKeys(y)) return dict((i, dict((j, callfcn(state, scope, fcn, [x.get(i, {}).get(j, 0.0), y.get(i, {}).get(j, 0.0)])) for j in cols)) for i in rows)
def __call__(self, state, scope, paramTypes, datum, table, *metric): if len(metric) == 1: metric, = metric # do signature 3 one = None oned = None for item in table: d = callfcn(state, scope, metric, [datum, item["x"]]) if one is None or d < oned: one = item oned = d return one["to"] elif isinstance(paramTypes[0], dict) and paramTypes[0].get("type") == "array": # do signature 2 one = None oned = None for item in table: x = item["x"] if len(x) != len(datum): raise PFARuntimeException("inconsistent dimensionality") d = sum((x0i - xi)**2 for x0i, xi in zip(datum, x)) if one is None or d < oned: one = item oned = d return one["to"] else: # do signature 1 one = None oned = None for item in table: d = abs(datum - item["x"]) if one is None or d < oned: one = item oned = d return one["to"]
def __call__(self, state, scope, paramTypes, m, fcn): out = {} for k, v in m.items(): vv = callfcn(state, scope, fcn, [k, v]) if vv is not None: out[k] = vv return out
def __call__(self, state, scope, paramTypes, a, fcn): out = [] for i, x in enumerate(a): y = callfcn(state, scope, fcn, [i, x]) if y is not None: out.append(y) return out
def __call__(self, state, scope, pos, paramTypes, points, *args): if len(args) == 1: weight, = args else: weight = None if len(points) == 0: raise PFARuntimeException("not enough points", self.errcodeBase + 0, self.name, pos) dimensions = len(points[0]) numer = [0.0] * dimensions denom = [0.0] * dimensions for point in points: if len(point) != dimensions: raise PFARuntimeException("inconsistent dimensionality", self.errcodeBase + 1, self.name, pos) if weight is None: w = 1.0 else: w = callfcn(state, scope, weight, [point]) for i in xrange(dimensions): numer[i] += w * point[i] denom[i] += w for i in xrange(dimensions): numer[i] /= denom[i] return numer
def __call__(self, state, scope, pos, paramTypes, a, b, fcn): aset = set(a.keys()) bset = set(b.keys()) if aset != bset: return False else: return all(callfcn(state, scope, fcn, [a[k], b[k]]) for k in aset)
def __call__(self, state, scope, pos, paramTypes, a, b, fcn): aset = set(a.keys()) bset = set(b.keys()) if aset != bset: return False else: return all(callfcn(state, scope, fcn, [k, a[k], b[k]]) for k in aset)
def __call__(self, state, scope, pos, paramTypes, datum, model, activation): if len(model) == 0: raise PFARuntimeException("no layers", self.errcodeBase + 0, self.name, pos) # pass datum through first N - 1 layers, apply activation for layer in model[:-1]: bias = layer["bias"] weights = layer["weights"] if (len(bias) != len(weights)) or any( len(x) != len(datum) for x in weights): raise PFARuntimeException("weights, bias, or datum misaligned", self.errcodeBase + 1, self.name, pos) tmp = [ sum(y) for y in zip(matrix_vector_mult(weights, datum), bias) ] datum = [callfcn(state, scope, activation, [i]) for i in tmp] # pass datum through final layer, don't apply activation bias = model[-1]["bias"] weights = model[-1]["weights"] if (len(bias) != len(weights)) or any( len(x) != len(datum) for x in weights): raise PFARuntimeException("weights, bias, or datum misaligned", self.errcodeBase + 1, self.name, pos) return [sum(y) for y in zip(matrix_vector_mult(weights, datum), bias)]
def __call__(self, state, scope, pos, paramTypes, points, *args): if len(args) == 1: weight, = args else: weight = None if len(points) == 0: raise PFARuntimeException("not enough points", self.errcodeBase + 0, self.name, pos) dimensions = len(points[0]) numer = [0.0] * dimensions denom = [0.0] * dimensions for point in points: if len(point) != dimensions: raise PFARuntimeException("inconsistent dimensionality", self.errcodeBase + 1, self.name, pos) if weight is None: w = 1.0 else: w = callfcn(state, scope, weight, [point]) for i in range(dimensions): numer[i] += w * point[i] denom[i] += w for i in range(dimensions): numer[i] /= denom[i] return numer
def __call__(self, state, scope, pos, paramTypes, k, datum, codebook, *args): if k < 0: raise PFARuntimeException("k must be nonnegative", self.errcodeBase + 0, self.name, pos) if len(args) == 1: metric, = args distances = [ callfcn(state, scope, metric, [datum, x]) for x in codebook ] else: if len(codebook) == 0: return [] else: dimensions = len(datum) for x in codebook: if len(x) != dimensions: raise PFARuntimeException( "inconsistent dimensionality", self.errcodeBase + 1, self.name, pos) distances = [ sum((di - xi)**2 for di, xi in zip(datum, x)) for x in codebook ] indexes = argLowestN(distances, k, lambda a, b: a < b) return [codebook[i] for i in indexes]
def __call__(self, state, scope, pos, paramTypes, data, k, newCluster): if k <= 0: raise PFARuntimeException("k must be greater than zero", self.errcodeBase + 0, self.name, pos) uniques = [] for x in data: if x not in uniques: uniques.append(x) if len(uniques) < k: raise PFARuntimeException("not enough unique points", self.errcodeBase + 1, self.name, pos) sizes = set(len(x) for x in uniques) if len(sizes) != 1: raise PFARuntimeException("dimensions of vectors do not match", self.errcodeBase + 2, self.name, pos) state.rand.shuffle(uniques) selected = uniques[:k] return [ callfcn(state, scope, newCluster, [i, list(vec)]) for i, vec in enumerate(selected) ]
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]
def __call__(self, state, scope, paramTypes, a, fcn): out = [] for x in a: y = callfcn(state, scope, fcn, [x]) if y is not None: out.append(y) return out
def __call__(self, state, scope, pos, paramTypes, similarity, x, y, missingWeight=None): length = len(x) if missingWeight is None: missingWeight = [1.0] * length if len(y) != length or len(missingWeight) != length: raise PFARuntimeException("dimensions of vectors do not match", self.errcodeBase + 0, self.name, pos) tally = 0.0 numer = 0.0 denom = 0.0 for i in xrange(length): xi = x[i] yi = y[i] if xi is not None and yi is not None: if isinstance(paramTypes[1]["items"], (tuple, list)): xi, = xi.values() if isinstance(paramTypes[2]["items"], (tuple, list)): yi, = yi.values() tally = self.increment( tally, callfcn(state, scope, similarity, [xi, yi])) denom += missingWeight[i] numer += missingWeight[i] if denom == 0.0: return self.finalize(float("nan")) else: return self.finalize(tally * numer / denom)
def __call__(self, state, scope, pos, paramTypes, a, fcn): out = [] for x in a: if callfcn(state, scope, fcn, [x]): out.append(x) else: break return out
def __call__(self, state, scope, pos, paramTypes, m, key): if callable(key): for k in m: if callfcn(state, scope, key, [k]): return True return False else: return key in m
def __call__(self, state, scope, pos, paramTypes, a, fcn): i = 0 for ai in a: if callfcn(state, scope, fcn, [ai]): i += 1 else: break return a[i:]
def __call__(self, state, scope, pos, paramTypes, *args): if len(set(map(len, args[:-1]))) != 1: raise PFARuntimeException("misaligned arrays", self.errcodeBase + 0, self.name, pos) fcn = args[-1] out = [] for i, abcd in enumerate(zip(*args[:-1])): out.append(callfcn(state, scope, fcn, [i] + list(abcd))) return out
def __call__(self, state, scope, pos, paramTypes, a, fcn): out = {} for x in a: key = callfcn(state, scope, fcn, [x]) if key not in out: out[key] = [] out[key].append(x) return out
def __call__(self, state, scope, pos, paramTypes, similarity, x, y, p, missingWeight=None): length = len(x) if missingWeight is None: missingWeight = [1.0] * length if len(y) != length or len(missingWeight) != length: raise PFARuntimeException("dimensions of vectors do not match", self.errcodeBase + 0, self.name, pos) if math.isnan(p) or p <= 0: raise PFARuntimeException("Minkowski parameter p must be positive", self.errcodeBase + 1, self.name, pos) tally = 0.0 numer = 0.0 denom = 0.0 if math.isinf(p): for i in xrange(length): xi = x[i] yi = y[i] if xi is not None and yi is not None: if isinstance(paramTypes[1]["items"], (tuple, list)): xi, = xi.values() if isinstance(paramTypes[2]["items"], (tuple, list)): yi, = yi.values() z = callfcn(state, scope, similarity, [xi, yi]) if z > tally: tally = z denom += missingWeight[i] numer += missingWeight[i] if denom == 0.0: return float("nan") else: return tally * numer / denom else: for i in xrange(length): xi = x[i] yi = y[i] if xi is not None and yi is not None: if isinstance(paramTypes[1]["items"], (tuple, list)): xi, = xi.values() if isinstance(paramTypes[2]["items"], (tuple, list)): yi, = yi.values() tally += powLikeJava(callfcn(state, scope, similarity, [xi, yi]), p) denom += missingWeight[i] numer += missingWeight[i] if denom == 0.0: return float("nan") else: return powLikeJava(tally * numer / denom, 1.0/p)
def __call__(self, state, scope, pos, paramTypes, datum, comparisons, missingTest): for comparison in comparisons: result = callfcn(state, scope, missingTest, [datum, comparison]) if result is not None: if isinstance(result, dict) and result.keys() == ["boolean"]: return result.values()[0] else: return result raise PFARuntimeException("no successful surrogate", self.errcodeBase + 0, self.name, pos)
def __call__(self, state, scope, paramTypes, datum, clusters, *args): if len(clusters) == 0: raise PFARuntimeException("no clusters") if len(args) == 1: metric, = args distances = [callfcn(state, scope, metric, [datum, x["center"]]) for x in clusters] else: distances = [sum((di - xi)**2 for di, xi in zip(datum, x["center"])) for x in clusters] index, = argLowestN(distances, 1, lambda a, b: a < b) return clusters[index]
def __call__(self, state, scope, paramTypes, n, datum, clusters, *args): if n < 0: raise PFARuntimeException("n must be nonnegative") if len(args) == 1: metric, = args distances = [callfcn(state, scope, metric, [datum, x["center"]]) for x in clusters] else: distances = [sum((di - xi)**2 for di, xi in zip(datum, x["center"])) for x in clusters] indexes = argLowestN(distances, n, lambda a, b: a < b) return [clusters[i] for i in indexes]
def __call__(self, state, scope, pos, paramTypes, datum, operator, comparisons, test): if operator == "and": for comparison in comparisons: if callfcn(state, scope, test, [datum, comparison]) is False: return False return True elif operator == "or": for comparison in comparisons: if callfcn(state, scope, test, [datum, comparison]) is True: return True return False elif operator == "xor": numTrue = 0 for comparison in comparisons: if callfcn(state, scope, test, [datum, comparison]) is True: numTrue += 1 return numTrue % 2 == 1 else: raise PFARuntimeException("unrecognized logical operator", self.errcodeBase + 0, self.name, pos)
def __call__(self, state, scope, pos, paramTypes, n, datum, clusters, *args): if n < 0: raise PFARuntimeException("n must be nonnegative", self.errcodeBase + 0, self.name, pos) if len(args) == 1: metric, = args distances = [callfcn(state, scope, metric, [datum, x["center"]]) for x in clusters] else: distances = [sum((di - xi)**2 for di, xi in zip(datum, x["center"])) for x in clusters] indexes = argLowestN(distances, n, lambda a, b: a < b) return [clusters[i] for i in indexes]
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
def __call__(self, state, scope, pos, paramTypes, datum, comparisons, missingTest): for comparison in comparisons: result = callfcn(state, scope, missingTest, [datum, comparison]) if result is not None: if isinstance(result, dict) and result.keys() == ["boolean"]: return list(result.values())[0] else: return result raise PFARuntimeException("no successful surrogate", self.errcodeBase + 0, self.name, pos)
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
def __call__(self, state, scope, pos, paramTypes, a, fcn): typeNames = [jsonNodeToAvroType(t).name for t in paramTypes[1]["ret"] if t != "null"] out = [] for i, x in enumerate(a): y = callfcn(state, scope, fcn, [i, x]) if y is not None: if isinstance(y, dict) and len(y) == 1 and list(y.keys())[0] in typeNames: tag, value = y.items()[0] else: value = y out.append(value) return out
def __call__(self, state, scope, paramTypes, x, top, n, lessThan): index = 0 for best in top: if callfcn(state, scope, lessThan, [best, x]): break index += 1 if index == len(top): out = top + [x] else: above, below = top[:index], top[index:] out = above + [x] + below return out[:n]
def __call__(self, state, scope, pos, paramTypes, a, fcn): typeNames = [jsonNodeToAvroType(t).name for t in paramTypes[1]["ret"] if t != "null"] out = [] for i, x in enumerate(a): y = callfcn(state, scope, fcn, [i, x]) if y is not None: if isinstance(y, dict) and len(y) == 1 and y.keys()[0] in typeNames: tag, value = y.items()[0] else: value = y out.append(value) return out
def __call__(self, state, scope, pos, paramTypes, m, fcn): typeNames = [jsonNodeToAvroType(t).name for t in paramTypes[1]["ret"] if t != "null"] out = {} for k, v in m.items(): vv = callfcn(state, scope, fcn, [k, v]) if vv is not None: if isinstance(vv, dict) and len(vv) == 1 and vv.keys()[0] in typeNames: tag, value = vv.items()[0] else: value = vv out[k] = value return out
def __call__(self, state, scope, pos, paramTypes, datum, model, kernel): const = model["const"] negClass = model["negClass"] posClass = model["posClass"] if len(negClass) == 0 and len(posClass) == 0: raise PFARuntimeException("no support vectors", self.errcodeBase + 0, self.name, pos) negClassScore = 0.0 for sv in negClass: supVec = sv["supVec"] if len(supVec) != len(datum): raise PFARuntimeException("support vectors must have same length as datum", self.errcodeBase + 1, self.name, pos) coeff = sv["coeff"] negClassScore += callfcn(state, scope, kernel, [supVec, datum])*coeff posClassScore = 0.0 for sv in posClass: supVec = sv["supVec"] if len(supVec) != len(datum): raise PFARuntimeException("support vectors must have same length as datum", self.errcodeBase + 1, self.name, pos) coeff = sv["coeff"] posClassScore += callfcn(state, scope, kernel, [supVec, datum])*coeff return negClassScore + posClassScore + const
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]
def __call__(self, state, scope, pos, paramTypes, x, top, n, lessThan): if n <= 0: return [] else: index = 0 for best in top: if callfcn(state, scope, lessThan, [best, x]): break index += 1 if index == len(top): out = top + [x] else: above, below = top[:index], top[index:] out = above + [x] + below return out[:n]
def __call__(self, state, scope, paramTypes, haystack, needle): if isinstance(needle, (list, tuple)): count = 0 for start in xrange(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)
def __call__(self, state, scope, paramTypes, datum, treeNode, test): treeNodeTypeName = paramTypes[1]["name"] node = treeNode while True: if callfcn(state, scope, test, [datum, node]): union = node["pass"] else: union = node["fail"] if union is None: node = None break else: (utype, node), = union.items() if utype != treeNodeTypeName: break return node
def __call__(self, state, scope, pos, paramTypes, haystack, needle): if isinstance(needle, (list, tuple)): for start in xrange(len(haystack) - len(needle) + 1): if needle == haystack[start:(start + len(needle))]: return start return -1 elif callable(needle): for index, item in enumerate(haystack): if callfcn(state, scope, needle, [item]): return index return -1 else: try: return haystack.index(needle) except ValueError: return -1
def __call__(self, state, scope, pos, paramTypes, haystack, needle): if isinstance(needle, (list, tuple)): for start in xrange(len(haystack) - len(needle), -1, -1): if needle == haystack[start:(start + len(needle))]: return start return -1 elif callable(needle): for index, item in enumerate(reversed(haystack)): if callfcn(state, scope, needle, [item]): return len(haystack) - 1 - index return -1 else: for index in xrange(len(haystack) - 1, -1, -1): if needle == haystack[index]: return index return -1
def __call__(self, state, scope, pos, paramTypes, haystack, needle): if isinstance(needle, (list, tuple)): for start in range(len(haystack) - len(needle) + 1): if needle == haystack[start:(start + len(needle))]: return start return -1 elif callable(needle): for index, item in enumerate(haystack): if callfcn(state, scope, needle, [item]): return index return -1 else: try: return haystack.index(needle) except ValueError: return -1
def __call__(self, state, scope, pos, paramTypes, haystack, needle): if isinstance(needle, (list, tuple)): for start in range(len(haystack) - len(needle), -1, -1): if needle == haystack[start:(start + len(needle))]: return start return -1 elif callable(needle): for index, item in enumerate(reversed(haystack)): if callfcn(state, scope, needle, [item]): return len(haystack) - 1 - index return -1 else: for index in range(len(haystack) - 1, -1, -1): if needle == haystack[index]: return index return -1
def __call__(self, state, scope, pos, paramTypes, m, fcn): typeNames = [ jsonNodeToAvroType(t).name for t in paramTypes[1]["ret"] if t != "null" ] out = {} for k, v in m.items(): vv = callfcn(state, scope, fcn, [k, v]) if vv is not None: if isinstance(vv, dict) and len(vv) == 1 and list( vv.keys())[0] in typeNames: tag, value = list(vv.items())[0] else: value = vv out[k] = value return out
def __call__(self, state, scope, paramTypes, data, k, newCluster): if k <= 0: raise PFARuntimeException("k must be greater than zero") uniques = list(set(map(tuple, data))) if len(uniques) < k: raise PFARuntimeException("not enough unique points") sizes = set(len(x) for x in uniques) if len(sizes) != 1: raise PFARuntimeException("dimensions of vectors do not match") state.rand.shuffle(uniques) selected = uniques[:k] return [callfcn(state, scope, newCluster, [i, list(vec)]) for i, vec in enumerate(selected)]
def __call__(self, state, scope, pos, paramTypes, datum, treeNode, test): treeNodeTypeName = paramTypes[1]["name"] node = treeNode while True: if callfcn(state, scope, test, [datum, node]): union = node["pass"] else: union = node["fail"] if union is None: node = None break else: (utype, node), = union.items() if utype != treeNodeTypeName: break return node
def __call__(self, state, scope, pos, paramTypes, haystack, needle): if isinstance(needle, (list, tuple)): for start in xrange(len(haystack) - len(needle) + 1): if needle == haystack[start:(start + len(needle))]: return True return False elif callable(needle): for item in haystack: if callfcn(state, scope, needle, [item]): return True return False else: try: haystack.index(needle) except ValueError: return False else: return True
def __call__(self, state, scope, pos, paramTypes, datum, treeNode, test): treeNodeTypeName = paramTypes[1]["name"] node = treeNode while True: result = callfcn(state, scope, test, [datum, node]) if result is True or result == {"boolean": True}: union = node["pass"] elif result is False or result == {"boolean": False}: union = node["fail"] elif result is None: union = node["missing"] if union is None: node = None break else: (utype, node), = union.items() if utype != treeNodeTypeName: break return node