def evaluate_predictions(fname): with open(fname, 'rb') as handle: results = pickle.load(handle) tp = TowerPlanner(stability_mode='contains') # Index this as [stable][cog_stable][pw_stable] for ix, (towers, labels, preds) in enumerate(results): correct = [[[0, 0], [0, 0]], [[0, 0], [0, 0]]] total = [[[0, 0], [0, 0]], [[0, 0], [0, 0]]] # Check the tower stability type. for tower, label, pred in zip(towers, labels, preds): blocks = to_blocks(tower) cog_stable = tp.tower_is_cog_stable(blocks) pw_stable = tp.tower_is_constructible(blocks) stable = tp.tower_is_stable(blocks) if stable != label: print('WAT', stable, label) #assert stable == label total[stable][cog_stable][pw_stable] += 1 if (pred > 0.5) == label: correct[stable][cog_stable][pw_stable] += 1 print(total) print('%d Towers' % (ix + 2)) for stable in [0, 1]: for cog_stable in [0, 1]: for pw_stable in [0, 1]: if ix == 0 and pw_stable != stable: continue acc = correct[stable][cog_stable][pw_stable] / total[ stable][cog_stable][pw_stable] print( 'Stable: %d\tCOG_Stable: %d\tPW_Stable: %d\tAcc: %f' % (stable, cog_stable, pw_stable, acc))
def geometric_stable(tower): tp = TowerPlanner(stability_mode='contains') return tp.tower_is_cog_stable(tower)