def pairwise_entropy_comp(filenames): x = [] y = [] l = [] for filename in filenames: # open the spreadsheet try: workbook = xlrd.open_workbook(filename) quipu = workbook.sheet_by_name('Pendant Detail') except Exception: continue primary = parse_to_pendant_tree(quipu) collect = empty_collect() primary.as_raw(collect) x+=[entropy.calc(collect["ply"])] y+=[entropy.calc(collect["knot_type"])] l+=[os.path.basename(filename)[:-4]] plt.xlabel('pendant ply entropy (bits)') plt.ylabel('knot type entropy (bits)') plt.scatter(x, y) for i, txt in enumerate(l): plt.annotate(txt, (x[i],y[i]), fontsize=6) plt.show()
def calculate_entropy(filename,encode_fn): print filename # open the spreadsheet try: workbook = xlrd.open_workbook(filename) quipu = workbook.sheet_by_name('Pendant Detail') except Exception: print "problem" return False print(encode_fn(quipu)) return entropy.calc(encode_fn(quipu))
def calc_entropy(self): collect=empty_collect() self.as_raw(collect) self.entropy=0 for i in collect.values(): self.entropy += entropy.calc(i) global _min_entropy global _max_entropy if self.entropy<_min_entropy: _min_entropy=self.entropy if self.entropy>_max_entropy: _max_entropy=self.entropy for p in self.children: p.calc_entropy()
def global_entropy_sliced(filenames): collect={ "ply":[], "attach":[], "length":[], "colours":[], "knot_value":[], "knot_type":[], "knot_position":[], "knot_spin":[] } for filename in filenames: # open the spreadsheet try: workbook = xlrd.open_workbook(filename) quipu = workbook.sheet_by_name('Pendant Detail') primary = parse_to_pendant_tree(quipu) primary.as_raw(collect) except Exception: pass for key,value in collect.items(): print(key+" "+str(entropy.calc(value)))
try: workbook = xlrd.open_workbook(filename) quipu = workbook.sheet_by_name('Pendant Detail') except Exception: print "problem" return False print(encode_fn(quipu)) return entropy.calc(encode_fn(quipu)) def batch_generate_entropy(filenames): cache = {} for filename in filenames: e = calculate_entropy(filename, parse_to_raw) if e!=False: cache[filename]=e sorted_cache = sorted(cache.items(), key=operator.itemgetter(1)) for item in sorted_cache: print(item) # are we the script that's being run? if __name__ == "__main__": if sys.argv[1]=="test": print entropy.calc(sys.argv[2]) exit(1) if sys.argv[1]=="batch": batch_generate_entropy(generate_quipu_list()) else: print("entropy is: "+str(calculate_entropy(sys.argv[1], parse_to_raw)))
"""Demo.""" import argparse import entropy if __name__ == '__main__': """Run demo.""" parser = argparse.ArgumentParser(description='Set window size.') parser.add_argument('-w', '--window', type=int, default=2) parser.add_argument('-f', '--file', type=str, default="signal.dat") args = parser.parse_args() with open(args.file) as f: signal = f.readline() exact = 0.99999922015194587997155654610165791488976968896093062603 s = entropy.calc(signal, window=args.window) print('Entropy:', s) print('Error:', abs(s-exact))
def test_mixed(self): """Test signal with zero enropy.""" signal = [0, 0, 1, 1, 1] expected = -(2 * math.log2(2. / 5.) + 3 * math.log2(3. / 5.)) / 5 expected = -(2 * math.log2(2) + 3 * math.log2(3)) / 5 + math.log2(5) self.assertAlmostEqual(entropy.calc(signal, window=1), expected, 15)
def test_one2(self): """Test signal with one enropy.""" signal = [0, 0, 0, 1, 1, 0, 1, 1] self.assertEqual(entropy.calc(signal, window=2), 1)
def test_zero2(self): """Test signal with zero enropy.""" signal = [0, 1, 0, 1, 0, 1, 0, 1] self.assertEqual(entropy.calc(signal, window=2), 0)