-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
35 commits
Select commit
Hold shift + click to select a range
e79fc17
first subticket: defining boundaries
yh-cyber 5e18683
starting on second subticket
yh-cyber 3d64f9b
starting on second subticket
yh-cyber 89ba124
working on window_postions function
yh-cyber beb3931
adding to sampler.py
yh-cyber fb3d757
fixed problem in test_sampler
yh-cyber cb3be37
adding to #33
yh-cyber 10897e9
adding to tests/sampler
yh-cyber 7dba7ee
finished test class for #33
yh-cyber fc3fe36
minor fix
yh-cyber beb9bca
Merge branch 'main' into yh-cyber-windowed-blending-sampler
yh-cyber 4036106
completed #33
yh-cyber afb2ea1
starting #34
yh-cyber c8765cb
finished tests for #34
yh-cyber 94f3a6f
finished #34
yh-cyber 347bbae
setting up for #35
yh-cyber 6d8af62
layout structure for produce_region
yh-cyber fac8f56
done produce_region, working on tests
yh-cyber 5f739e2
working on fakestore
yh-cyber 82d9964
Merge branch 'main' into yh-cyber-windowed-blending-sampler
yh-cyber 13a135e
finished subissue 35
yh-cyber e6641cc
fixing script quality check errors
yh-cyber 45f33c5
working on requested changes
yh-cyber bc5b1e1
sampler.py fixed, working on test_sampler.py
yh-cyber 6d314f7
fixing script errors
yh-cyber e10439f
fixed all issues except 1, one more test to be added for window. adde…
yh-cyber 90e96e7
Merge branch 'main' of https://github.com/cssu/terrain-diffusion into…
yh-cyber aaf1dac
added assertion for edge_len >1 in weight_grid
yh-cyber fa6f1b6
added last rrequested change
yh-cyber ee0696c
added comment to clarify
yh-cyber d8ace93
implemented changes
yh-cyber 59abe52
Merge branch 'main' into yh-cyber-windowed-blending-sampler
yh-cyber e75a75c
fixed window/step assertion
yh-cyber f073cfc
Merge branch 'yh-cyber-windowed-blending-sampler' of https://github.c…
yh-cyber 0a3f225
fixed test_deterministic_region
yh-cyber File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,6 +12,7 @@ dist/ | |
| # tool caches | ||
| .pytest_cache/ | ||
| .ruff_cache/ | ||
| .coverage | ||
|
|
||
| node_modules/ | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,220 @@ | ||
| """ | ||
| 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 = 2 | ||
| 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) | ||
|
KurbyDoo marked this conversation as resolved.
|
||
|
|
||
|
|
||
| # the requested test for determinitic grid has been added at line 205 | ||
| 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) | ||
|
|
||
| def test_deterministic_region(self, pipeline): | ||
| """Assert the output matches the expected grid.""" | ||
| seed = 123 | ||
| height = 9 | ||
| width = 9 | ||
| window_size = 3 | ||
| step = 2 | ||
| pipeline.generate.side_effect = lambda patch: np.ones(patch.shape) | ||
|
KurbyDoo marked this conversation as resolved.
|
||
|
|
||
| weighted_sum, weight_sum = produce_region(seed, height, width, window_size, step, pipeline) | ||
| result = weighted_sum / weight_sum | ||
|
|
||
| expected = np.ones((9, 9)) | ||
|
|
||
| assert np.allclose(result, expected) | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.