Skip to content
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

update functions.py #13

Closed
wants to merge 3 commits into from
Closed

Conversation

yucongalicechen
Copy link
Collaborator

  • Update functions.py: modify the algorithm to select the correct grid point by comparing the two y-coordinates of points on the circle instead of comparing them with the y-coordinate of the grid point.

@yucongalicechen
Copy link
Collaborator Author

  • Updated _get_grid_points in functions.py where we prefer to have the grid as a set instead of an array.
  • Included test_functions.py with tests functions for get_grid_points and set_distances_at_angle.

Copy link
Contributor

@sbillinge sbillinge left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this looks great, nice job.

  • I wonder if we should check intermediate points in the gridding, so go from 3 to 5 on the grid as right now we are only testing points at the center and on the circumference.
  • Also for the angle, do we want to test points coming from different quadrants to be sure about the results?
  • Please also see my inline comment

The PR is failing CI. Did you sync your main and then merge main into this branch? If you did, then there is something else wrong. You can see by clicking on "details" and see where it failed.

@sbillinge
Copy link
Contributor

I see that you are failing a bunch of pre-commit tests too. This should not happen if the pre-commit is passing on your local. Please run pre-commit install in the top level directory to get this working.

@yucongalicechen
Copy link
Collaborator Author

  • For test_get_grid_points: checked grid_size = 3 and 8 (so both odd and even numbers - cases where grid points fall on the circle and fall inside circle).
  • For test_set_distances_at_angle: checked distances for acute/right/obtuse angles for grid_size = 3 and 6 and radius = 1 and 0.3. I was going to include some extreme cases like angle = 89.9, but I thought that we're probably not going to calculate distances for these angles, so would this be necessary? Also, although the values of the distances are correct, the order of the distances is a bit weird, I will check about this.
  • For test_set_muls_at_angle: values are correct. This probably won't be an issue when we use the function because we will always call set_distances function first, but it seems that I need to add the set_distances function before using set_muls, where there is a set_distances function inside set_muls (i.e., not sure if set_distances inside set_muls is working probably).
  • There are two other functions get_path_length and get_entry_exit_coordinates in Class Gridded_circle. They are included in set_distances_at_angle, so I didn't write other tests for them.

Copy link
Contributor

@sbillinge sbillinge left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please see inline comments.

ideally we would have one test per branch and it would be easier to merge these. Just decide if you want to close this PR and do that, or just continue with this PR.

Also, quick question, did you manage to merge upstream main?


params1 = [
[[1, 3, 1, {(0.0, -1.0), (0.0, 0.0), (-1.0, 0.0), (1.0, 0.0), (0.0, 1.0)}, 5]],
[[0.2, 8, 1, {(-0.08571428571428572, -0.14285714285714285), (-0.02857142857142858, -0.14285714285714285), (0.02857142857142858, -0.14285714285714285), (0.08571428571428574, -0.14285714285714285), (-0.14285714285714285, -0.08571428571428572), (-0.08571428571428572, -0.08571428571428572), (-0.02857142857142858, -0.08571428571428572), (0.02857142857142858, -0.08571428571428572), (0.08571428571428574, -0.08571428571428572), (0.14285714285714285, -0.08571428571428572), (-0.14285714285714285, -0.02857142857142858), (-0.08571428571428572, -0.02857142857142858), (-0.02857142857142858, -0.02857142857142858), (0.02857142857142858, -0.02857142857142858), (0.08571428571428574, -0.02857142857142858), (0.14285714285714285, -0.02857142857142858), (-0.14285714285714285, 0.02857142857142858), (-0.08571428571428572, 0.02857142857142858), (-0.02857142857142858, 0.02857142857142858), (0.02857142857142858, 0.02857142857142858), (0.08571428571428574, 0.02857142857142858), (0.14285714285714285, 0.02857142857142858), (-0.14285714285714285, 0.08571428571428574), (-0.08571428571428572, 0.08571428571428574), (-0.02857142857142858, 0.08571428571428574), (0.02857142857142858, 0.08571428571428574), (0.08571428571428574, 0.08571428571428574), (0.14285714285714285, 0.08571428571428574), (-0.08571428571428572, 0.14285714285714285), (-0.02857142857142858, 0.14285714285714285), (0.02857142857142858, 0.14285714285714285), (0.08571428571428574, 0.14285714285714285)}, 32]]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this set of values seems to be very specific. How did you generate them and how do you know they are correct? A better test is one where you know the right answer and generate without using the function. For an odd number of points use 0.5.

The even number of points specified is harder to test, so do it with a very small number of points, say 2 on each axis and then the points shold presumably be something like 1/3, 2/3.

from diffpy.labpdfproc.functions import Gridded_circle

params1 = [
[[1, 3, 1, {(0.0, -1.0), (0.0, 0.0), (-1.0, 0.0), (1.0, 0.0), (0.0, 1.0)}, 5]],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these are easier to read if they are a list of tuples, one for each test, then this tuple has two lists which are for the inputs and outputs. So in this case it would be

params1 = [
    ( [1, 3, 1], [{(...), (...), (...)}, 9] ),
    (...)
]
@pytest.mark.paramaterize("inputs, expected", params1)
def test_...():
    circle = Griddedcircle(inputs[0], inputs[1])

and so on. This is much easier to read.

I may not have the syntax from pytest.mark.paramatrize() quite right so please look it up, but we would like the intent of hte tests to be easier to read in all your tests.


@pytest.mark.parametrize('params1', params1)
def test_get_grid_points(params1):
for param_set in params1:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not how paramatrize works, you don't need this.




# values of distances are correct but im a bit confused about the order of distances
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Decide in your test what behavior you want. Do you care about order? If not, you could use a set, or stick with a list but sort it by value before doing the assert, something like that. If you care about order in the function then test this.


# values of distances are correct but im a bit confused about the order of distances
params2 = [
[[1, 3, 1, [25, 90, 140],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you need to run black on everything to get the layout right. But when you install pre-commit it will do it for you when you commit. You will have to run the commit multiple times as when it runs black the commit fails, so just rerun it (if flake8 fails you have to fix the errors manually then rerun the commit).

[[1, 3, 1, [25, 90, 140],
[[0, 2, 1.81261557406, 2, 0.845236523481399],
[0, 2, 0, 2, 2],
[0, 2, 0, 3.5320888862379562, 1.2855752193730785]]]],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

many of these numbers seem magical and too high in precision (is it important for your function that it gets a number correct to 11 significant figures?). Can you manually compute a few special cases and use those as the test? It may be harder here than for the grid points, so just find a reasonable balance, but don't use the function to compute these.


@pytest.mark.parametrize('params2', params2)
def test_set_distances_at_angle(params2):
for param_set in params2:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove this for loop.

radius, n_points_on_diameter, mu, angles, expected_muls = params3
actual_gs = Gridded_circle(radius=radius, n_points_on_diameter=n_points_on_diameter, mu=mu)
for angle, expected_mul in zip(angles, expected_muls):
# here it seems like I need to set distances first (can't assign distances only using set muls)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remember, figure out what you want your function to do then write the test to do it. What does this function do and how is it used, and so what behavior do you want in different circumstances, then write the test to check that. Don't be distracted by what the function actually does at the moment. Let's just write the tests first, then we can rewrite any functions to make them have that behavior.

@sbillinge
Copy link
Contributor

I htink this PR can be closed as it is replaced with other PRs. If you agree, please can you close it?

@yucongalicechen yucongalicechen deleted the package branch April 29, 2024 01:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants