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

Refactor tests #251

Merged
merged 33 commits into from
May 15, 2021
Merged

Refactor tests #251

merged 33 commits into from
May 15, 2021

Conversation

ewu63
Copy link
Collaborator

@ewu63 ewu63 commented May 7, 2021

Purpose

This PR refactors the existing tests by introducing a test class that handles various shared tasks

  • run an optimization
  • check that solutions are close
  • check that the final inform is correct
  • check the history file contains expected contents
  • check hot start

I also added parameterized tests etc. for efficiency.

Not all existing tests use this class however:

  • the two SNOPT test files can probably use this if we want, but will require a bit more work
  • for some reason NSGA2 will segfault if the objfunc is defined within the class. Absolutely no idea why..

I'll keep this as a draft PR for now. The main thing I want to check is that we're not losing any coverage through this, so if reviewers can also check against the old tests and see if I missed anything. Finally, the way the tests are structured can probably improved, so any suggestions are welcome.

Type of change

What types of change is it?
Select the appropriate type(s) that describe this PR

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (non-backwards-compatible fix or feature)
  • Code style update (formatting, renaming)
  • Refactoring (no functional changes, no API changes)
  • Documentation update
  • Maintenance update
  • Other (please describe)

@ewu63 ewu63 requested review from marcomangano and sseraj May 7, 2021 15:22
@codecov
Copy link

codecov bot commented May 7, 2021

Codecov Report

Merging #251 (42b5144) into master (b955064) will decrease coverage by 0.53%.
The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #251      +/-   ##
==========================================
- Coverage   83.97%   83.44%   -0.54%     
==========================================
  Files          22       22              
  Lines        3252     3250       -2     
==========================================
- Hits         2731     2712      -19     
- Misses        521      538      +17     
Impacted Files Coverage Δ
pyoptsparse/pyoptsparse/pyOpt_optimization.py 78.83% <0.00%> (-1.50%) ⬇️
pyoptsparse/pyoptsparse/pyOpt_history.py 81.70% <0.00%> (-0.82%) ⬇️
pyoptsparse/pyoptsparse/pyOpt_optimizer.py 84.25% <0.00%> (-0.70%) ⬇️
pyoptsparse/pyoptsparse/pyALPSO/pyALPSO.py 85.18% <0.00%> (-0.36%) ⬇️
pyoptsparse/pyoptsparse/pySNOPT/pySNOPT.py 90.09% <0.00%> (-0.34%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update b955064...42b5144. Read the comment docs.

@ewu63 ewu63 mentioned this pull request May 8, 2021
12 tasks
Copy link
Contributor

@sseraj sseraj left a comment

Choose a reason for hiding this comment

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

This is a major improvement! I don't see any obvious reduction in coverage other than SNOPT versions prior to 7.7.7. Is that intentional?

assert_allclose(sol_xvars, dv["xvars"], atol=tol, rtol=tol)
# we check either optimum via try/except
try:
if optName == "SNOPT" and opt.version != "7.7.7":
Copy link
Contributor

Choose a reason for hiding this comment

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

The stable image is failing because these checks have been removed.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Good point, will add

self.optimize_with_hotstart("PSQP", 5e-12, optOptions=optOptions)
# dv = sol.getDVs()
# sol_xvars = [sol.variables["xvars"][i].value for i in range(2)]
# assert_allclose(sol_xvars, dv["xvars"], atol=tol, rtol=tol)
Copy link
Contributor

Choose a reason for hiding this comment

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

Are these commented lines supposed to be here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Ah, right these were testing sol.variables etc. I will try to generalize these and make sure the Solution object contained the right values in different attributes.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I added some tests, but not all since it's a bit harder to generalize. I've opened #256 to address this, among other things.

test/test_large_snopt.py Outdated Show resolved Hide resolved
@ewu63 ewu63 marked this pull request as ready for review May 13, 2021 17:35
@ewu63 ewu63 requested a review from a team as a code owner May 13, 2021 17:35
@ewu63 ewu63 linked an issue May 13, 2021 that may be closed by this pull request
@ewu63 ewu63 mentioned this pull request May 13, 2021
@ewu63 ewu63 requested a review from sseraj May 13, 2021 18:55
Copy link
Contributor

@marcomangano marcomangano left a comment

Choose a reason for hiding this comment

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

Altright, I think I listed all the remaining comments I had. Note that some of them are not necessarily related to your edits, but additional comments/info we could add during this refactoring.

This is a substantial PR and some commits are pretty big and hard to follow, so for future PRs I would suggest to either break them further or, more in general, add a comment to the commit message to clarify the rationale behind it?

test/test_hs071.py Show resolved Hide resolved
test/testing_utils.py Outdated Show resolved Hide resolved
test/test_hs071.py Show resolved Hide resolved
test/test_tp109.py Show resolved Hide resolved

# Check Solution
assert_allclose(sol2.objectives["obj"].value, 0.536206927538e04, atol=1e-2, rtol=1e-2)
self.assert_solution(sol2, 1e-2)
Copy link
Contributor

Choose a reason for hiding this comment

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

See above - Why is this tolerance so large?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Hmm good point, I think the idea is that this is loose because SNOPT may not be available, so then we just assert the ALPSO answer. I will fix this.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Actually ALPSO seems...kinda broken. I'll open an issue but I prefer not fixing this in this PR.



def get_dict_distance(d, d2):
"""This converts a flat 1D dict to array using sorted keys, then computes the L2 distance"""
Copy link
Contributor

Choose a reason for hiding this comment

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

took a while to understand why this was necessary, nice trick, but are we sure it is the most robust approach?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Not sure what you mean here. The code is not 100% robust because the numpy array casts aren't 100% fool proof, but the approach itself should be valid no?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah I was just thinking about cases where the L2 norm would not be the the best approach but I think for testing specifically this is not an issue. Good to go!

test/testing_utils.py Show resolved Hide resolved
Copy link
Contributor

@sseraj sseraj 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 good to me other than one thing

test/testing_utils.py Outdated Show resolved Hide resolved
@ewu63
Copy link
Collaborator Author

ewu63 commented May 14, 2021

This is a substantial PR and some commits are pretty big and hard to follow, so for future PRs I would suggest to either break them further or, more in general, add a comment to the commit message to clarify the rationale behind it?

Yeah sorry about that, I tried to break it up but as you can see, it's hard to do when I'm changing many things at once. Because I will be testing things as I develop, it's hard to keep a nice linear git history as well. I will try to make my commit messages better though, I don't think I was very good at that.

Anyways, I think I addressed most of the concerns. Please take another look and let me know.

@ewu63 ewu63 requested a review from marcomangano May 15, 2021 18:46
@marcomangano marcomangano merged commit bf140e2 into master May 15, 2021
@marcomangano marcomangano deleted the update-tests branch May 15, 2021 22:26
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.

Update testing framework
3 participants