import os, tarfile, math, random, sys, getopt ## SEQUENCES AND SETS def indices(x, seq): '''List of indices in seq where x is found.''' return [pos for pos, item in enumerate(seq) if item == x] def distance2(vec1, vec2): '''Square of the euclidian distance between the vectors.''' return sum([(x - y)**2 for x,y in zip(vec1, vec2)]) def distance(vec1, vec2): '''Euclidian distance between the vectors.''' return math.sqrt(distance2(vec1, vec2)) def ran_bin_vec(length): '''A vector of len length with random 0s and 1s.''' return [random.randrange(2) for x in range(length)] def union(sets): '''The union of multiple sets in a sequence.''' return reduce(lambda x, y: x | y, sets) def intersection(sets): '''The intersection of multiple sets in a sequence.''' return reduce(lambda x, y: x & y, sets) # Get rid of this on later versions because it overrides built-in 'any' def any(pred, seq): '''True if any member of the sequence or iterator satisfies the predicate.''' for x in seq: if pred(x): return True return False def some(pred, seq): '''True if any member of the sequence or iterator satisfies the predicate.''' for x in seq: px = pred(x) if px: return px return False def every(pred, seq): '''True if every member of the sequence or iterator satisfies the predicate.''' for x in seq: if not pred(x): return False return True def reduce_lists(lists): '''Flatten a list of lists (doesn't mutate lists).''' return reduce(lambda x, y: x + y, lists) def remove_dups(seq): '''Make a copy of seq with no duplicate elements.''' copy = seq[:] for p,x in enumerate(seq): if x in seq[p+1:]: copy.remove(x) return copy ## MATH def normalize(vector): '''Make the vector (really a list) length 1.0.''' total = math.sqrt(sum([x**2 for x in vector])) for i in range(len(vector)): vector[i] /= total def dot_product(v1, v2): '''Dot product of the two vectors.''' return sum([x1 * x2 for x1, x2 in zip(v1, v2)]) def threshold(inp, thresh, min_val, max_val): '''Simple threshold function.''' if inp >= thresh: return max_val else: return min_val def sigmoid(inp, thresh, bias): '''Sigmoid function (0 < y < 1) with threshold and bias.''' return 1.0 / (1.0 + math.exp(bias * (-inp + thresh))) def sigmoid_slope(x): '''Slope of the sigmoid with output x.''' return x * (1.0 - x) ## ARGMAX def argmax(seq, func): '''Element of seq with the highest value for func.''' best = seq[0] best_score = func(best) for x in seq: x_score = func(x) if x_score > best_score: best, best_score = x, x_score return best def argmax2(seq, func): '''Element and value for seq with the highest value for func.''' best = seq[0] best_score = func(best) for x in seq: x_score = func(x) if x_score > best_score: best, best_score = x, x_score return (best, best_score) ## FILES, DIRECTORIES def tarpy(direc = '.', filename = 'files', others = ()): '''Make gzipped tar archive called filename for .py files in direc and others.''' tar = tarfile.open(filename + ".tgz", "w:gz") for fl in [f for f in os.listdir(direc) if f[-3:] == '.py']: tar.add(fl) for o in others: tar.add(o) tar.close() # To change directories, do # >>> os.chdir(path) # # To list files in a directory, do # >>> os.listdir(path) # (Use '.' for current directory.) # # For the current directory, do # >>> os.getcwd()