-
Notifications
You must be signed in to change notification settings - Fork 2
Add tests for distances, initialization and move phis #8
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
Open
EkaterinaNikolaeva
wants to merge
4
commits into
ItIsMrLaG:main
Choose a base branch
from
EkaterinaNikolaeva:additional-tests
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| import pytest | ||
| import taichi as ti | ||
| import taichi.math as tm | ||
| from catsim.tools import ( | ||
| euclidean_distance, | ||
| manhattan_distance, | ||
| chebyshev_distance, | ||
| get_distance, | ||
| ) | ||
| from catsim.constants import ( | ||
| EUCLIDEAN_DISTANCE, | ||
| CHEBYSHEV_DISTANCE, | ||
| MANHATTAN_DISTANCE, | ||
| ) | ||
|
|
||
|
|
||
| @ti.data_oriented | ||
| class TestDistanceFunctions: | ||
| @ti.kernel | ||
| def get_euclidean(self, p1: tm.vec2, p2: tm.vec2) -> ti.f32: | ||
| return euclidean_distance(p1, p2) | ||
|
|
||
| @ti.kernel | ||
| def get_manhattan(self, p1: tm.vec2, p2: tm.vec2) -> ti.f32: | ||
| return manhattan_distance(p1, p2) | ||
|
|
||
| @ti.kernel | ||
| def get_chebyshev(self, p1: tm.vec2, p2: tm.vec2) -> ti.f32: | ||
| return chebyshev_distance(p1, p2) | ||
|
|
||
| @ti.kernel | ||
| def get_distance_by_type( | ||
| self, p1: tm.vec2, p2: tm.vec2, distance_type: ti.i32 | ||
| ) -> ti.f32: | ||
| return get_distance(p1, p2, distance_type) | ||
|
|
||
| @pytest.mark.parametrize( | ||
| "p1, p2, expected", | ||
| [ | ||
| (tm.vec2(0, 0), tm.vec2(3, 4), 5.0), | ||
| (tm.vec2(1, 1), tm.vec2(4, 5), 5.0), | ||
| (tm.vec2(-2, -3), tm.vec2(1, 1), 5.0), | ||
| (tm.vec2(-1.5, 0), tm.vec2(1.0, 0), 2.5), | ||
| (tm.vec2(-1e5, -1e5), tm.vec2(-1e5, -1e5), 0.0), | ||
| (tm.vec2(1000, 2000), tm.vec2(3000, 4000), 2828.4271247), | ||
| (tm.vec2(1e5, 1e5), tm.vec2(-1e4, -1e6), 1105486.3183232), | ||
| ], | ||
| ) | ||
| def test_euclidean(self, p1, p2, expected): | ||
| result = self.get_euclidean(p1, p2) | ||
| assert pytest.approx(result, 0.001) == expected | ||
|
|
||
| @pytest.mark.parametrize( | ||
| "p1, p2, expected", | ||
| [ | ||
| (tm.vec2(0, 0), tm.vec2(3, 4), 7.0), | ||
| (tm.vec2(-2, -3), tm.vec2(1, 1), 7.0), | ||
| (tm.vec2(-5, -10), tm.vec2(-6, -3), 8.0), | ||
| (tm.vec2(-1, -2), tm.vec2(5, -7), 11.0), | ||
| ], | ||
| ) | ||
| def test_manhattan(self, p1, p2, expected): | ||
| result = self.get_manhattan(p1, p2) | ||
| assert pytest.approx(result, 0.001) == expected | ||
|
|
||
| @pytest.mark.parametrize( | ||
| "p1, p2, expected", | ||
| [ | ||
| (tm.vec2(0, 0), tm.vec2(3, 4), 4.0), | ||
| (tm.vec2(-2, -3), tm.vec2(1, -5), 3.0), | ||
| (tm.vec2(-2, -3), tm.vec2(1, -7), 4.0), | ||
| (tm.vec2(-5, -10), tm.vec2(-6, -3), 7.0), | ||
| ], | ||
| ) | ||
| def test_chebyshev(self, p1, p2, expected): | ||
| result = self.get_chebyshev(p1, p2) | ||
| assert pytest.approx(result, 0.001) == expected | ||
|
|
||
| @pytest.mark.parametrize( | ||
| "dist_type, p1, p2, expected", | ||
| [ | ||
| (EUCLIDEAN_DISTANCE, tm.vec2(0, 0), tm.vec2(3, 4), 5.0), | ||
| (MANHATTAN_DISTANCE, tm.vec2(0, 0), tm.vec2(3, 4), 7.0), | ||
| (CHEBYSHEV_DISTANCE, tm.vec2(0, 0), tm.vec2(3, 4), 4.0), | ||
| ], | ||
| ) | ||
| def test_distance(self, dist_type, p1, p2, expected): | ||
| result = self.get_distance_by_type(p1, p2, dist_type) | ||
| assert pytest.approx(result, 0.001) == expected |
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,62 @@ | ||
| import taichi as ti | ||
| import catsim.cat as cat | ||
| from catsim.constants import ( | ||
| DISABLE_PROB_INTER, | ||
| MOVE_PATTERN_RANDOM_ID, | ||
| MOVE_PATTERN_LINE_ID, | ||
| ) | ||
|
|
||
|
|
||
| def init_cat_env(move_radius, r0, r1, width, height, move_pattern, prob_inter): | ||
| cat.init_cat_env( | ||
| move_radius, | ||
| r0, | ||
| r1, | ||
| width, | ||
| height, | ||
| move_pattern, | ||
| prob_inter, | ||
| ) | ||
|
|
||
|
|
||
| def test_init_cat_env(): | ||
| cat.init_cat_env( | ||
| move_radius=10.5, | ||
| r0=2.0, | ||
| r1=8.0, | ||
| width=100, | ||
| height=200, | ||
| move_pattern=MOVE_PATTERN_RANDOM_ID, | ||
| prob_inter=DISABLE_PROB_INTER, | ||
| ) | ||
| assert cat._MOVE_RADIUS == 10.5 | ||
| assert cat._RADIUS_0 == 2.0 | ||
| assert cat._RADIUS_1 == 8.0 | ||
| assert cat._PLATE_WIDTH == 100 | ||
| assert cat._PLATE_HEIGHT == 200 | ||
| assert cat._MOVE_PATTERN == MOVE_PATTERN_RANDOM_ID | ||
| assert cat._PROB_INTER == DISABLE_PROB_INTER | ||
|
|
||
|
|
||
| @ti.kernel | ||
| def init_cat(cat_r: ti.f32) -> cat.Cat: | ||
| catInstance = cat.Cat() | ||
| catInstance.init_cat(cat_r) | ||
| return catInstance | ||
|
|
||
|
|
||
| def test_cat_initialization_with_env(): | ||
| cat.init_cat_env( | ||
| move_radius=15.0, | ||
| r0=5.0, | ||
| r1=10.0, | ||
| width=50, | ||
| height=50, | ||
| move_pattern=MOVE_PATTERN_LINE_ID, | ||
| prob_inter=DISABLE_PROB_INTER, | ||
| ) | ||
| catInstance = init_cat(3.0) | ||
| assert 0 <= catInstance.point[0] <= cat._PLATE_WIDTH | ||
| assert 0 <= catInstance.point[0] <= cat._PLATE_HEIGHT | ||
| assert catInstance.radius == 3.0 | ||
| assert catInstance.move_pattern == MOVE_PATTERN_LINE_ID |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Поведение
move_pattern_phisпри отражении довольно неочевидное для меня. Проверьте, пожалуйста, что именно там должно происходить (и лучше записать куда-нибудь в доку, как конкретно работают перемещения)