def bird_flocks( n_birds: int = 50, lang: str = 'py', dim: str = '2d', n_frames: int = 700, saves: str = None ): if lang not in ['py', 'nim']: print("Programming Language Not Supported") return if dim not in ['2d', '3d']: print("Dimension Not Supported") return border = 2000 frames = [] start = time.time() if lang == 'py': flock = Flock(n_birds, dim, border) for i in range(n_frames): flock.update() frames.append(flock.get_frame(dim=dim)) elif lang == 'nim': frames = boids.simulation(n_birds, dim, border, n_frames) print(f"Emulating {n_frames} frames on {round(time.time() - start, 2)} seconds") if dim == "2d": animate_2d_frames(frames, border, saves) elif dim == "3d": animate_3d_frames(np.array(frames), border, saves)
def test_gen_animation(mock_funcanim): f = Flock() f.gen_animation() mock_funcanim.assert_called_with(f.figure, f.animate, frames=50, interval=50)
def test_bounds_min_lessthan_max(): with open( os.path.join(os.path.dirname(__file__), 'fixtures', 'flock_samples_minmax.yaml')) as fixtures_file: fixtures = yaml.load(fixtures_file) for fixture in fixtures: instance = Flock(**fixture) with assert_raises(ValueError) as exception: instance.init_cond_matrix()
def test_num_boids_positive_int(): with open( os.path.join(os.path.dirname(__file__), 'fixtures', 'flock_samples.yaml')) as fixtures_file: fixtures = yaml.load(fixtures_file) for fixture in fixtures: instance = Flock(**fixture) with assert_raises(TypeError) as exception: instance.init_cond_matrix()
def test_conf_loader(): default_conf = yaml.load( open(os.path.join(os.path.dirname(__file__), '..', 'config.yml'), 'r')) flock = Flock() for key, item in default_conf.items(): for sub_key in item.keys(): assert getattr(flock, sub_key) == item[sub_key] non_default_conf = yaml.load( open(os.path.join(os.path.dirname(__file__), 'Fixtures', 'config.yml'), 'r')) flock = Flock(conf=non_default_conf) for key, item in non_default_conf.items(): for sub_key in item.keys(): assert getattr(flock, sub_key) == item[sub_key]
def test_random_gen(mock_uniform): f = Flock() assert mock_uniform.call_count == 4 mock_uniform.assert_any_call(-450., 50., 50.) mock_uniform.assert_any_call(300., 600., 50.) mock_uniform.assert_any_call(0., 10., 50.) mock_uniform.assert_any_call(-20., 20., 50.)
def test_from_data(): data = yaml.load( open( os.path.join(os.path.dirname(__file__), 'Fixtures', 'fixture.yml'), 'r')) flock = Flock.from_data(data["before"]) imported = flock.data for real, got in zip(data["before"], imported): for real_value, got_value in zip(real, got): assert_equal(real_value, got_value)
def test_match_speed_regression(): regression_data = yaml.load( open( os.path.join(os.path.dirname(__file__), 'Fixtures', 'fixture.yml'), 'r')) flock = Flock.from_data(regression_data['before']) flock.match_speed_to_nearby_birds() res = flock.data for after, calculated in zip(regression_data["match"], res): for after_value, calculated_value in zip(after, calculated): assert_almost_equal(after_value, calculated_value, delta=0.01)
def test_bad_boids_regression(): regression_data = yaml.load( open( os.path.join(os.path.dirname(__file__), 'Fixtures', 'fixture.yml'), 'r')) boid_data = regression_data["before"] flock = Flock.from_data(boid_data) flock.update_boids() res = flock.data for after, calculated in zip(regression_data["after"], res): for after_value, calculated_value in zip(after, calculated): assert_almost_equal(after_value, calculated_value, delta=0.01)
def test_properties(): data = yaml.load( open(os.path.join(os.path.dirname(__file__), 'Fixtures', 'fixture.yml'))) flock = Flock.from_data(data["after"]) offset = flock.offset_tuple true_offset = list(zip(data['after'][0], data['after'][1])) #Caused python3 test to fail got_data = flock.data for after, got in zip(data["after"], got_data): for after_value, got_value in zip(after, got): assert_almost_equal(after_value, got_value, delta=0.01) for n, item in enumerate(offset): assert item[0] == true_offset[n][0] assert item[1] == true_offset[n][1]
def process(): _ROOT = os.path.abspath(os.path.dirname(__file__)) with open(os.path.join(_ROOT, 'config/config.yaml')) as config_file: config = yaml.load(config_file) num_boids = config["number_of_boids"] initial_params = np.array([config["x_bounds"], config["y_bounds"]]) interaction_params = np.array([ config["centre_attraction"], config["retreat_distance"], config["attraction_distance"], config["drag_strength"] ]) #make a flock of boids and make them fly flock_of_boids = Flock(num_boids, initial_params).init_cond_matrix() flying_flock = Flight(flock_of_boids, interaction_params) #display the boids frame_x_min = config["x_bounds"][0] - config["x_margin"] frame_x_max = config["x_bounds"][1] + config["x_margin"] frame_y_min = config["y_bounds"][0] - config["y_margin"] frame_y_max = config["y_bounds"][1] + config["y_margin"] figure = plt.figure() axes = plt.axes(xlim=(frame_x_min, frame_x_max), ylim=(frame_y_min, frame_y_max)) scatter = axes.scatter(flying_flock.get_x(), flying_flock.get_y()) def animate(frame): scatter.set_offsets( zip(flying_flock.update_boids().get_x(), flying_flock.update_boids().get_y())) anim = animation.FuncAnimation(figure, animate, frames=config["frame_number"], interval=config["frame_interval"]) plt.show()
def test_animate(mock_update, mock_scatter): f = Flock() f.animate(Mock()) assert mock_update.called mock_scatter.assert_called_with(f.offset_tuple)