-
Notifications
You must be signed in to change notification settings - Fork 0
Yh cyber windowed blending sampler #53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
e79fc17
5e18683
3d64f9b
89ba124
beb3931
fb3d757
cb3be37
10897e9
7dba7ee
fc3fe36
beb9bca
4036106
afb2ea1
c8765cb
94f3a6f
347bbae
6d8af62
fac8f56
5f739e2
82d9964
13a135e
e6641cc
45f33c5
bc5b1e1
6d314f7
e10439f
90e96e7
aaf1dac
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,203 @@ | ||
| """ | ||
| Testing for window blending sampler. | ||
| """ | ||
|
|
||
| import numpy as np | ||
| import pytest | ||
|
|
||
| from terrain_diffusion.sampler import ( | ||
| generate_noise_from_seed, | ||
| produce_region, | ||
| weight_grid, | ||
| window_positions, | ||
| ) | ||
|
|
||
|
|
||
| class TestWindowPositions: | ||
| def test_all_covered(self): | ||
| "Assert every cell in the region is covered by at least one window" | ||
| height = 4 | ||
| width = 4 | ||
| window = 2 | ||
| step = 1 | ||
| positions = window_positions(height, width, window, step) | ||
| assert all( | ||
| any( | ||
| window_r <= row < window_r + window and window_c <= column < window_c + window | ||
| for window_r, window_c in positions | ||
| ) | ||
| for row in range(height) | ||
| for column in range(width) | ||
| ) | ||
|
|
||
| def test_exceed_region(self): | ||
| "Assert no window exceeds past the region" | ||
| height = 4 | ||
| width = 4 | ||
| size = 2 | ||
| step = 1 | ||
| positions = window_positions(height, width, size, step) | ||
| assert all(x[0] + size <= height and x[1] + size <= width for x in positions) | ||
|
|
||
| def test_one_window(self): | ||
| "Assert a region exactly one window in size returns one position" | ||
| WindowRegionSize = 4 | ||
| positions = window_positions( | ||
| WindowRegionSize, WindowRegionSize, WindowRegionSize, WindowRegionSize | ||
| ) | ||
| assert len(positions) == 1 | ||
|
|
||
| # Had to modify test because added assertion to original function | ||
| def test_region_not_divisible(self): | ||
| with pytest.raises(AssertionError): | ||
| window_positions(10, 8, 4, 3) | ||
|
|
||
|
|
||
| class TestWeights: | ||
| def test_grid_equal_patch(self): | ||
| "Assert the grid is the size of a patch" | ||
| edge = 10 | ||
| weights = weight_grid(edge) | ||
| assert weights.shape == (edge, edge) | ||
|
|
||
| def test_palidrome(self): | ||
| "Assert it reads the same forwards and backwards in both directions" | ||
| edge = 10 | ||
| weights = weight_grid(edge) | ||
| assert np.array_equal(weights, weights[::-1, :]) # vertical | ||
| assert np.array_equal(weights, weights[:, ::-1]) # horizontal | ||
|
|
||
| def test_large_middle(self): | ||
| "Assert the largest value is in the middle" | ||
| edge = 11 | ||
| weights = weight_grid(edge) | ||
|
|
||
| # middle | ||
| center = edge // 2 | ||
|
|
||
| assert weights[center, center] == weights.max() | ||
|
|
||
| def test_edges_smaller(self): | ||
| "Assert values at the edges are smaller than values in the middle" | ||
| edge = 11 | ||
| weights = weight_grid(edge) | ||
|
|
||
| # middle | ||
| center = edge // 2 | ||
| middle_val = weights[center, center] | ||
|
|
||
| # R/L edges | ||
| assert all(weights[x, 0] < middle_val for x in range(edge)) | ||
| assert all(weights[x, edge - 1] < middle_val for x in range(edge)) | ||
|
|
||
| # T/B edges | ||
| assert all(weights[0, y] < middle_val for y in range(edge)) | ||
| assert all(weights[edge - 1, y] < middle_val for y in range(edge)) | ||
|
|
||
| def test_greater_zero(self): | ||
| "Assert every value is greater than zero" | ||
| weights = weight_grid(10) | ||
| assert np.all(weights > 0) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add a test with concrete values, pick a 9x9 region with 3x3 window size and assert the window matches to a deterministic 9x9 grid
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm really sorry I don't quite get this one. Is it a test for the function weight_grid, or produce_region?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes for produce region, basically I want to see a test that produces a grid with numbers and output what the grid of numbers looks like |
||
|
|
||
|
|
||
| class TestSeed: | ||
| def test_same_seed(self): | ||
| """Assert the same seed twice gives identical grids.""" | ||
| seed = 123 | ||
| height = 10 | ||
| width = 20 | ||
| noise1 = generate_noise_from_seed(seed, height, width) | ||
| noise2 = generate_noise_from_seed(seed, height, width) | ||
| assert np.array_equal(noise1, noise2) | ||
|
|
||
| def test_diff_seed(self): | ||
| """Assert two different seeds give different grids.""" | ||
| seed1 = 123 | ||
| seed2 = 456 | ||
| height = 10 | ||
| width = 20 | ||
| noise1 = generate_noise_from_seed(seed1, height, width) | ||
| noise2 = generate_noise_from_seed(seed2, height, width) | ||
| assert not np.array_equal(noise1, noise2) | ||
|
|
||
| def test_right_size(self): | ||
| """Assert the grid is the size asked for""" | ||
| seed = 123 | ||
| height = 10 | ||
| width = 20 | ||
| noise = generate_noise_from_seed(seed, height, width) | ||
| assert noise.shape == (height, width) | ||
|
|
||
|
|
||
| class TestRegionProduction: | ||
| @pytest.fixture | ||
| def pipeline(self, mocker): | ||
| pipeline = mocker.Mock() # make it a Mock object, this way can count calls. | ||
| pipeline.generate.side_effect = lambda patch: np.full( | ||
| patch.shape, 5 | ||
| ) # added side_effect to keep it a Mock object | ||
| return pipeline | ||
|
|
||
| def test_all_fives(self, pipeline): | ||
| """Assert the finished grid is all fives everywhere, including the overlaps and the corners. | ||
| If the overlaps read higher then the weights are not being divided out""" | ||
|
|
||
| seed = 123 | ||
| height = 8 | ||
| width = 8 | ||
| window_size = 4 | ||
| step = 2 | ||
|
|
||
| weighted_sum, weight_sum = produce_region(seed, height, width, window_size, step, pipeline) | ||
| result = weighted_sum / weight_sum # doing job of store | ||
| assert np.allclose( | ||
| result, 5 | ||
| ) # All close because was getting float error as some are 4.9999 due to the store | ||
|
|
||
| def test_full_size(self, pipeline): | ||
| """Assert the finished grid is the region's full resolution size""" | ||
|
|
||
| seed = 123 | ||
| height = 8 | ||
| width = 8 | ||
| window_size = 4 | ||
| step = 2 | ||
|
|
||
| weighted_sum, weight_sum = produce_region(seed, height, width, window_size, step, pipeline) | ||
| result = weighted_sum / weight_sum # doing job of store | ||
|
|
||
| assert result.shape == (height, width) | ||
|
|
||
| def test_once_per_window(self, pipeline): | ||
| """Assert the fake pipeline was called once per window position and no more""" | ||
|
|
||
| seed = 123 | ||
| height = 8 | ||
| width = 8 | ||
| window_size = 4 | ||
| step = 2 | ||
|
|
||
| positions = window_positions(height, width, window_size, step) | ||
|
|
||
| produce_region(seed, height, width, window_size, step, pipeline) | ||
|
|
||
| assert pipeline.generate.call_count == len(positions) | ||
|
|
||
| def test_same_seed_grid(self, pipeline): | ||
| """Assert the same seed and region run twice give identical grids""" | ||
| seed = 123 | ||
| height = 8 | ||
| width = 8 | ||
| window_size = 4 | ||
| step = 2 | ||
|
|
||
| weighted_sum1, weight_sum1 = produce_region( | ||
| seed, height, width, window_size, step, pipeline | ||
| ) | ||
| weighted_sum2, weight_sum2 = produce_region( | ||
| seed, height, width, window_size, step, pipeline | ||
| ) | ||
| result1 = weighted_sum1 / weight_sum1 | ||
| result2 = weighted_sum2 / weight_sum2 | ||
|
|
||
| assert np.array_equal(result1, result2) | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.