def test_text_0_init(self): """Test vault object creation with text""" # 1. Create default donut object mydonut = donut.Donut(text=from_text(EXAMPLES[0])) # 2. Make sure it has the expected values self.assertEqual(len(mydonut.text), 19) self.assertEqual(mydonut.rows, 19) self.assertEqual(mydonut.cols, 19) self.assertEqual( mydonut.portals, { 'AA': {(9, 2)}, 'BC': {(2, 8), (9, 6)}, 'DE': {(2, 13), (6, 10)}, 'FG': {(2, 15), (11, 12)}, 'ZZ': {(13, 16)} }) self.assertEqual(mydonut.finish, donut.FINISH) self.assertEqual( mydonut.portals_at, { (2, 8): 'BC', (2, 13): 'DE', (2, 15): 'FG', (6, 10): 'DE', (9, 2): 'AA', (9, 6): 'BC', (11, 12): 'FG', (13, 16): 'ZZ' }) self.assertEqual(mydonut.part2, False) self.assertEqual(len(mydonut.locs), 47) # 3. Check methods (not used by init) self.assertEqual(mydonut.exit_dirs(9, 2), ['S']) self.assertEqual(mydonut.exit_dirs(9, 3), ['N', 'S', 'E']) self.assertEqual(mydonut.exit_dirs(9, 4), ['N', 'S']) self.assertEqual(mydonut.exit_dirs(9, 5), ['N', 'S']) self.assertEqual(mydonut.exit_dirs(9, 6), ['N', 'S']) self.assertEqual(mydonut.non_portals(9, 2), ['S']) self.assertEqual(mydonut.non_portals(9, 3), ['S', 'E']) self.assertEqual(mydonut.non_portals(9, 4), ['N', 'S']) self.assertEqual(mydonut.non_portals(9, 5), ['N']) self.assertEqual(mydonut.non_portals(9, 6), ['N', 'S']) self.assertEqual(mydonut.exit_locs(9, 2), [(9, 3)]) self.assertEqual(mydonut.exit_locs(9, 3), [(9, 2), (9, 4), (10, 3)]) self.assertEqual(mydonut.exit_locs(9, 4), [(9, 3), (9, 5)]) self.assertEqual(mydonut.exit_locs(9, 5), [(9, 4), (9, 6)]) self.assertEqual(mydonut.exit_locs(9, 6), [(9, 5)]) self.assertEqual(mydonut.inner_portal((2, 8)), False) self.assertEqual(mydonut.inner_portal((2, 13)), False) self.assertEqual(mydonut.inner_portal((2, 15)), False) self.assertEqual(mydonut.inner_portal((6, 10)), True) self.assertEqual(mydonut.inner_portal((9, 2)), False) self.assertEqual(mydonut.inner_portal((9, 6)), True) self.assertEqual(mydonut.inner_portal((11, 12)), True) self.assertEqual(mydonut.inner_portal((13, 16)), False)
def test_methods(self): "Test exploration between portal locations and other methods" # 1. Create an solver without a donut and then slap on a donut mysolver = solver.Solver() mysolver.donut = donut.Donut(text=from_text(test_donut.EXAMPLES[0])) # 2. Explore from all portal ends self.assertEqual(mysolver.explore_from((2, 8)), {(6, 10): 6}) # BC -> DE self.assertEqual(mysolver.explore_from((2, 13)), {(2, 15): 4}) # DE -> FG self.assertEqual(mysolver.explore_from((2, 15)), {(2, 13): 4}) # FG -> DE self.assertEqual(mysolver.explore_from((6, 10)), {(2, 8): 6}) # DE -> BC self.assertEqual(mysolver.explore_from((9, 2)), {(9, 6): 4, # AA -> BC (11, 12): 30, # AA -> FG (13, 16): 26}) # AA -> ZZ self.assertEqual(mysolver.explore_from((11, 12)), {(13, 16): 6, # FG -> ZZ (9, 2): 30, # FG -> AA (9, 6): 32}) # FG -> BC self.assertEqual(mysolver.explore_from((13, 16)), {(11, 12): 6, # ZZ -> FG (9, 2): 26, # ZZ -> AA (9, 6): 28}) # ZZ -> BC # 3. Compute and check direct portal paths mysolver.portal_paths = mysolver.get_direct_paths_between_portals() self.assertEqual(len(mysolver.portal_paths), 5) self.assertEqual(sorted(mysolver.portal_paths.keys()), ['AA', 'BC', 'DE', 'FG', 'ZZ']) self.assertEqual(len(mysolver.portal_paths['AA']), 3) self.assertEqual(sorted([path.steps for path in mysolver.portal_paths['AA']]), [4, 26, 30]) self.assertEqual(sorted([path.steps for path in mysolver.portal_paths['BC']]), [1, 1, 4, 6, 28, 32]) self.assertEqual(sorted([path.steps for path in mysolver.portal_paths['DE']]), [1, 1, 4, 6]) self.assertEqual(sorted([path.steps for path in mysolver.portal_paths['FG']]), [1, 1, 4, 6, 30, 32]) self.assertEqual(sorted([path.steps for path in mysolver.portal_paths['ZZ']]), [6, 26, 28]) # 4. Compute and check graph mysolver.graph = mysolver.portal_paths_to_graph() self.assertEqual(len(mysolver.graph.edges), 22) # 5. Test solving the graph mysolver.solve_donut_maze() self.assertEqual(len(mysolver.path), 8) self.assertEqual(mysolver.cost, 23) # 6. Test part2 graph generation mysolver.part2 = True mysolver.graph = mysolver.portal_paths_to_graph(depth=1) self.assertCountEqual(Counter([(_.start[0], _.end[0]) for _ in mysolver.graph.edges]), {(0, 0): 22}) mysolver.graph = mysolver.portal_paths_to_graph(depth=2) self.assertCountEqual(Counter([(_.start[0], _.end[0]) for _ in mysolver.graph.edges]), {(0, 0): 12, (1, 1): 6, (0, 1): 3, (1, 0): 3}) mysolver.graph = mysolver.portal_paths_to_graph(depth=3) self.assertCountEqual(Counter([(_.start[0], _.end[0]) for _ in mysolver.graph.edges]), {(0, 0): 12, (1, 1): 6, (2, 2): 6, (0, 1): 3, (1, 2): 3, (1, 0): 3, (2, 1): 3})
def __init__(self, text=None, verbose=False, depth=1, part2=False): # 1. Set the initial values self.donut = donut.Donut(text=text, part2=part2) self.part2 = part2 # 2. Find the paths to all of the portals self.portal_paths = self.get_direct_paths_between_portals() self.graph = self.portal_paths_to_graph(depth=depth, verbose=verbose) self.path = None self.cost = None
def test_empty_init(self): """Test default donut mazet object creation""" # 1. Create default Vault object mydonut = donut.Donut() # 2. Make sure it has the default values self.assertEqual(mydonut.text, None) self.assertEqual(mydonut.rows, 0) self.assertEqual(mydonut.cols, 0) self.assertEqual(mydonut.portals, {}) self.assertEqual(mydonut.start, donut.START) self.assertEqual(mydonut.finish, donut.FINISH) self.assertEqual(mydonut.portals_at, {}) self.assertEqual(mydonut.part2, False) self.assertEqual(mydonut.locs, {})
def main(): # read the raw data timestamp, values, labels = load_kpi(INPUT) file = os.path.basename(INPUT) # complete the timestamp, and obtain the missing point indicators timestamp, missing, (values, labels) = donut.preprocessing.complete_timestamp( timestamp, (values, labels)) # split the training and testing data test_portion = 0.3 test_n = int(len(values) * test_portion) train_values, test_values = values[:-test_n], values[-test_n:] train_labels, test_labels = labels[:-test_n], labels[-test_n:] train_missing, test_missing = missing[:-test_n], missing[-test_n:] # standardize the training and testing data train_values, mean, std = donut.preprocessing.standardize_kpi( train_values, excludes=np.logical_or(train_labels, train_missing)) test_values, _, _ = donut.preprocessing.standardize_kpi(test_values, mean=mean, std=std) # we build the entire model within the scope of `model_vs`, # it should hold exactly all the variables of `model`, including # the variables created by Keras layers with tf.variable_scope('model') as model_vs: model = donut.Donut( h_for_p_x=tf.keras.Sequential([ tf.keras.layers.Dense( 100, kernel_regularizer=tf.keras.regularizers.l2(0.001), activation=tf.nn.relu), tf.keras.layers.Dense( 100, kernel_regularizer=tf.keras.regularizers.l2(0.001), activation=tf.nn.relu), ]), h_for_q_z=tf.keras.Sequential([ tf.keras.layers.Dense( 100, kernel_regularizer=tf.keras.regularizers.l2(0.001), activation=tf.nn.relu), tf.keras.layers.Dense( 100, kernel_regularizer=tf.keras.regularizers.l2(0.001), activation=tf.nn.relu), ]), x_dims=120, z_dims=8, ) trainer = donut.DonutTrainer(model=model, model_vs=model_vs, max_epoch=EPOCHS) predictor = donut.DonutPredictor(model) with tf.Session().as_default(): trainer.fit(train_values, train_labels, train_missing, mean, std) test_scores = -predictor.get_score(test_values, test_missing) adjusted_scores = adjust_scores(test_scores, test_labels[119:]) adjusted_scores, adjusted_labels = ignore_missing( [adjusted_scores, test_labels[119:]], missing=test_missing[119:]) threshold, precision, recall, f1score = best_f1score( labels=adjusted_labels, scores=adjusted_scores) print(f'file: {file}\n' f'threshold: {threshold}\n' f'precision: {precision:.3f}\n' f'recall: {recall:.3f}\n' f'f1score: {f1score:.3f}\n') os.makedirs(OUTPUT, exist_ok=True) with open(f'{os.path.join(OUTPUT, os.path.splitext(file)[0])}.txt', 'w') as output: output.write(f'file={file}\n\n' f'threshold={threshold}\n' f'precision={precision:.3f}\n' f'recall={recall:.3f}\n' f'f1_score={f1score:.3f}\n')
def test_text_2_init(self): """Test vault object creation with text""" # 1. Create default donut object mydonut = donut.Donut(text=from_text(EXAMPLES[2]), part2=True) # 2. Make sure it has the expected values self.assertEqual(len(mydonut.text), 37) self.assertEqual(mydonut.rows, 37) self.assertEqual(mydonut.cols, 45) self.assertEqual( mydonut.portals, { 'OA': {(17, 34), (8, 13)}, 'WB': {(36, 13), (19, 2)}, 'ZH': {(42, 13), (31, 8)}, 'CJ': {(2, 15), (8, 23)}, 'CK': {(8, 17), (27, 2)}, 'IC': {(23, 8), (42, 17)}, 'XF': {(2, 21), (17, 28)}, 'RF': {(36, 21), (42, 25)}, 'NM': {(36, 23), (23, 34)}, 'RE': {(21, 8), (2, 25)}, 'ZZ': {(13, 2)}, 'FD': {(19, 34), (13, 8)}, 'LP': {(29, 28), (15, 2)}, 'AA': {(15, 34)}, 'XQ': {(17, 2), (21, 28)} }) self.assertEqual(mydonut.start, donut.START) self.assertEqual(mydonut.finish, donut.FINISH) self.assertEqual( mydonut.portals_at, { (8, 13): 'OA', (36, 13): 'WB', (42, 13): 'ZH', (2, 15): 'CJ', (8, 17): 'CK', (42, 17): 'IC', (2, 21): 'XF', (36, 21): 'RF', (8, 23): 'CJ', (36, 23): 'NM', (2, 25): 'RE', (42, 25): 'RF', (13, 2): 'ZZ', (13, 8): 'FD', (15, 2): 'LP', (15, 34): 'AA', (17, 2): 'XQ', (17, 28): 'XF', (17, 34): 'OA', (19, 2): 'WB', (19, 34): 'FD', (21, 8): 'RE', (21, 28): 'XQ', (23, 8): 'IC', (23, 34): 'NM', (27, 2): 'CK', (29, 28): 'LP', (31, 8): 'ZH' }) self.assertEqual(mydonut.part2, True) self.assertEqual(len(mydonut.locs), 376) self.assertEqual(mydonut.outer_portal((8, 13)), False) self.assertEqual(mydonut.outer_portal((36, 13)), False) self.assertEqual(mydonut.outer_portal((42, 13)), True) self.assertEqual(mydonut.outer_portal((2, 15)), True) self.assertEqual(mydonut.outer_portal((8, 17)), False) self.assertEqual(mydonut.outer_portal((42, 17)), True) self.assertEqual(mydonut.outer_portal((2, 21)), True) self.assertEqual(mydonut.outer_portal((36, 21)), False) self.assertEqual(mydonut.outer_portal((8, 23)), False) self.assertEqual(mydonut.outer_portal((36, 23)), False) self.assertEqual(mydonut.outer_portal((2, 25)), True) self.assertEqual(mydonut.outer_portal((42, 25)), True) self.assertEqual(mydonut.inner_portal((13, 2)), False) self.assertEqual(mydonut.inner_portal((13, 8)), True) self.assertEqual(mydonut.inner_portal((15, 2)), False) self.assertEqual(mydonut.inner_portal((15, 24)), True) self.assertEqual(mydonut.inner_portal((17, 2)), False) self.assertEqual(mydonut.inner_portal((17, 28)), True) self.assertEqual(mydonut.inner_portal((17, 34)), False) self.assertEqual(mydonut.inner_portal((19, 2)), False) self.assertEqual(mydonut.inner_portal((19, 34)), False) self.assertEqual(mydonut.inner_portal((21, 8)), True) self.assertEqual(mydonut.inner_portal((23, 8)), True) self.assertEqual(mydonut.inner_portal((23, 34)), False) self.assertEqual(mydonut.inner_portal((27, 2)), False) self.assertEqual(mydonut.inner_portal((29, 28)), True) self.assertEqual(mydonut.inner_portal((31, 8)), True)
def test_text_1_init(self): """Test vault object creation with text""" # 1. Create default donut object mydonut = donut.Donut(text=from_text(EXAMPLES[1])) # 2. Make sure it has the expected values self.assertEqual(len(mydonut.text), 37) self.assertEqual(mydonut.rows, 37) self.assertEqual(mydonut.cols, 35) self.assertEqual( mydonut.portals, { 'AA': {(19, 2)}, 'AS': {(32, 17), (17, 8)}, 'BU': {(26, 21), (11, 34)}, 'CP': {(19, 34), (21, 8)}, 'DI': {(2, 15), (8, 21)}, 'JO': {(13, 28), (2, 19)}, 'JP': {(21, 28), (15, 34)}, 'LF': {(15, 28), (32, 21)}, 'QG': {(32, 23), (26, 17)}, 'VT': {(26, 23), (32, 11)}, 'YN': {(2, 23), (26, 13)}, 'ZZ': {(2, 17)} }) self.assertEqual(mydonut.start, donut.START) self.assertEqual(mydonut.finish, donut.FINISH) self.assertEqual( mydonut.portals_at, { (2, 15): 'DI', (2, 17): 'ZZ', (2, 19): 'JO', (2, 23): 'YN', (8, 21): 'DI', (11, 34): 'BU', (13, 28): 'JO', (15, 28): 'LF', (15, 34): 'JP', (17, 8): 'AS', (19, 2): 'AA', (19, 34): 'CP', (21, 8): 'CP', (21, 28): 'JP', (26, 13): 'YN', (26, 17): 'QG', (26, 21): 'BU', (26, 23): 'VT', (32, 11): 'VT', (32, 17): 'AS', (32, 21): 'LF', (32, 23): 'QG' }) self.assertEqual(mydonut.part2, False) self.assertEqual(len(mydonut.locs), 313) self.assertEqual(mydonut.outer_portal((2, 15)), True) self.assertEqual(mydonut.outer_portal((2, 17)), True) self.assertEqual(mydonut.outer_portal((2, 19)), True) self.assertEqual(mydonut.outer_portal((2, 23)), True) self.assertEqual(mydonut.outer_portal((8, 21)), False) self.assertEqual(mydonut.outer_portal((11, 34)), True) self.assertEqual(mydonut.outer_portal((13, 28)), False) self.assertEqual(mydonut.outer_portal((15, 28)), False) self.assertEqual(mydonut.outer_portal((15, 34)), True) self.assertEqual(mydonut.outer_portal((17, 8)), False) self.assertEqual(mydonut.outer_portal((19, 2)), True) self.assertEqual(mydonut.outer_portal((19, 34)), True) self.assertEqual(mydonut.outer_portal((21, 8)), False) self.assertEqual(mydonut.outer_portal((21, 28)), False) self.assertEqual(mydonut.outer_portal((26, 13)), False) self.assertEqual(mydonut.outer_portal((26, 17)), False) self.assertEqual(mydonut.outer_portal((26, 21)), False) self.assertEqual(mydonut.outer_portal((26, 23)), False) self.assertEqual(mydonut.outer_portal((32, 11)), True) self.assertEqual(mydonut.outer_portal((32, 17)), True) self.assertEqual(mydonut.outer_portal((32, 21)), True) self.assertEqual(mydonut.outer_portal((32, 23)), True)
s2 = sphere.Sphere(bdgmath.Vector3(3.5, 2, 2), 2) s2.color1 = (40, 200, 40) #s2.color2 = (0, 250, 0) s2.color2 = (40, 200, 40) #scene.addObject(s2) s3 = sphere.Sphere(bdgmath.Vector3(-4, 2, 2), 2) s3.color1 = (40, 40, 200) #s3.color2 = (0, 0, 250) s3.color2 = s3.color1 #scene.addObject(s3) #cyl = cylinder.ZCylinder(bdgmath.Vector2(1, 4), 1) #scene.addObject(cyl) donut = donut.Donut(2.5, 1) #scene.addObject(donut) repeat = repeated2d.Repeated2d(9, 9, donut) #scene.addObject(repeat) box = box.Box(bdgmath.Vector3(3, 2, 1)) translatedBox = transform.Translate3d(bdgmath.Vector3(0, 5, 1), box) #scene.addObject(box) #scene.addObject(translatedBox) translatedDonut = transform.Translate3d( bdgmath.Vector3(0, 5, 5.5), transform.RotateZ(20, transform.RotateX(80, donut))) #scene.addObject(translatedDonut)
scene = scene.Scene() scene.addLight(light.DirectionalLight(bdgmath.Vector3(-1, 0.5, -4), 0.8)) scene.addLight(light.AmbientLight(0.2)) floor = plane.ZPlane(0) floor.squareSize = 2 scene.addObject(floor) cube = box.Box(bdgmath.Vector3(5, 5, 5)) cube.color = (1.0, 0.75, 0.25) negSphere = sphere.Sphere(bdgmath.Vector3(2.5, -2.5, 2.5), 4.5) cubeMinusSphere = csg.Difference(cube, negSphere) torus = donut.Donut(3.5, 1) torus.color = torus.color1 = torus.color2 = (0, 0.8, 0) rotTorus = transform.RotateZ(225, transform.RotateX(45, torus)) torusCube = csg.Union(cubeMinusSphere, rotTorus) posTorusCube = transform.Translate3d(bdgmath.Vector3(0, 0, 5), torusCube) scene.addObject(posTorusCube) #cam.renderScene(scene, 640, 320, "raytest.png", 5) cam.renderScene(scene, 100, 80, "raytest.png", 5)