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

isospectrum debugging / refactoring #89

Merged
merged 15 commits into from
Mar 11, 2020
Merged

isospectrum debugging / refactoring #89

merged 15 commits into from
Mar 11, 2020

Conversation

apatlpo
Copy link
Contributor

@apatlpo apatlpo commented Feb 8, 2020

This PR attemps to fix several issues related to isospectrum computations:

  • breaks with dask arrays (dask bincount does not handle mixed types arguments)

  • breaks with extra dimensions

See gist for illustration of failures

I also propose a modest refactoring which consists in adding an isotropize methods that average spectra or crossspectra azimuthaly.
This method is called by isotropic_powerspectrum and isotropic_crossspectrum

There are still some improvements that could be made:

  • isotropize could be made more robust
  • the present form is not lazy (because dask bincount is not able to predict array size)

If you believe this PR is worth anything, let me know, I can finetune, even though weeks to come are going to be busy with Ocean Sciences and all.

cheers

@rabernat
Copy link
Collaborator

rabernat commented Feb 8, 2020

Hi @apatlpo -- thanks a lot for the PR! I think this makes a lot of sense, both in terms of fixing bugs
("breaks with extra dimensions" is #9, a long standing issue) and code simplicity.

Can you just have a look at the test failure?

@apatlpo
Copy link
Contributor Author

apatlpo commented Feb 8, 2020

Sounds good, I'll try clean up the PR.

@codecov-io
Copy link

codecov-io commented Feb 9, 2020

Codecov Report

Merging #89 into master will increase coverage by 0.38%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #89      +/-   ##
==========================================
+ Coverage   97.08%   97.47%   +0.38%     
==========================================
  Files           2        2              
  Lines         755      752       -3     
  Branches      130      118      -12     
==========================================
  Hits          733      733              
+ Misses         12       10       -2     
+ Partials       10        9       -1
Impacted Files Coverage Δ
xrft/tests/test_xrft.py 99.09% <100%> (+0.07%) ⬆️
xrft/xrft.py 95.16% <100%> (+0.33%) ⬆️

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 9de9817...5ed19a6. Read the comment docs.

@apatlpo
Copy link
Contributor Author

apatlpo commented Feb 9, 2020

I'd like to propose renaming isotropic_powerspectrum and isotropic_crossspectrum.
Here are some suggestions:

  • isotropic_power_spectrum, isotropic_cross_spectrum
  • isopower_spectrum, isocross_spectrum

My preference goes to the second option.
What do people think?

@apatlpo
Copy link
Contributor Author

apatlpo commented Feb 9, 2020

Things to do:

  • improve test of isotropic_*spectrum
  • add test for isotropize
  • try to bullet proof isotropize
  • specify in isotropize doc and related methods that the code is not lazy anymore
  • investigate options to keep the code lazy (should be doable)

@rabernat
Copy link
Collaborator

I'd like to propose renaming isotropic_powerspectrum and isotropic_crossspectrum.
Here are some suggestions:

I'm ok with this, but let's do a deprecation cycle. We will keep the old function names for now but just have them call the new functions and issue a DeprecationWarning. Then in a future release we will remove them completely.

I personally prefer isotropic_power_spectrum and isotropic_cross_spectrum because they are more explicit.

@apatlpo
Copy link
Contributor Author

apatlpo commented Feb 15, 2020

Sounds good.

I won't be able to tackle before approx 1 week unfortunately.

@apatlpo
Copy link
Contributor Author

apatlpo commented Mar 2, 2020

this PR has been moving along and is close to be reviewed.

Before then, do I need to address codecov failings?

I am not sure what I'm supposed to do with these.

@apatlpo
Copy link
Contributor Author

apatlpo commented Mar 3, 2020

I was about to say that this was ready for review but got an issue with python 2.7 test.
Do I really need to install 2.7 and figure what's wrong?

ping @rabernat

@apatlpo apatlpo closed this Mar 3, 2020
@apatlpo apatlpo reopened this Mar 3, 2020
@roxyboy
Copy link
Member

roxyboy commented Mar 3, 2020

I'm in favor of ending support for py27...

@roxyboy
Copy link
Member

roxyboy commented Mar 6, 2020

If we're going to keep support py27 as for now, one way would be to check the python version on the run with if sys.version_info[0] < 3:.

@rabernat
Copy link
Collaborator

rabernat commented Mar 6, 2020

Let's just drop py2.7. We don't have enough users for a deprecation cycle.

@apatlpo - just remove the py2.7 environment from the travis config and let's see if the tests go green.

Copy link
Collaborator

@rabernat rabernat 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 looking great. Thanks for all your work @apatlpo!

@@ -652,13 +652,68 @@ def _synthetic_field(N, dL, amp, s):
theta = np.fft.ifft2(np.fft.ifftshift(F_theta))
return np.real(theta)

def _synthetic_field_xr(N, dL, amp, s,
Copy link
Collaborator

Choose a reason for hiding this comment

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

You don't need to use private functions in tests. It's not part of public API anyway. Rename to synthetic_field.

There should also probably be a fixture.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure what a fixture precisely is, but I am under the impression that because I am calling the method multiple times with different input parameters it may not be suited to this case. Correct?

spacing_tol = 1e-3
nfactor = 4
ps = xrft.power_spectrum(da, spacing_tol, dim=dims)
ps_iso = xrft.isotropize(ps, fftdim, nfactor=nfactor)
Copy link
Collaborator

Choose a reason for hiding this comment

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

This test is not actually checking anything. It's just verifying that the function does not raise an error.

Here's an idea: rather than calling power_spectrum, can you just make up a synthetic spectrum with a known isotropic spectrum, and then verify that isotropize does the right thing? You will want some assert statements in there somewhere.

xrft/xrft.py Outdated
Deprecated function. See isotropic_cross_spectrum doc
"""
import warnings
msg = "Deprecated. Use isotropic_cross_spectrum instead"
Copy link
Collaborator

Choose a reason for hiding this comment

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

What about "This function has been renamed and will disappear in the future. Please use isotropic_cross_spectrum instead"

@apatlpo
Copy link
Contributor Author

apatlpo commented Mar 6, 2020

Travis goes through but not codecov.
Let me know if I need to do anything else.

@rabernat
Copy link
Collaborator

rabernat commented Mar 7, 2020

Codecov is telling us that the test coverage has gone slightly down. The coverage tells you which percentage of the code base is covered by tests, so this means there are some new code paths that are not tested. You can use the codecov site to figure out where exactly these are. If you can add some tests

This may seem like a pain, but it's very useful to have high test coverage like we have now, and we want to try to maintain this. You can check coverage locally while developing by running this command:

- py.test xrft --cov=xrft --cov-config .coveragerc --cov-report term-missing -v

This should be a relatively simple fix, but it turns out to be complicated or annoying, we can just accept the drop in coverage and move on.

Thanks for your perseverance with this PR.

@apatlpo
Copy link
Contributor Author

apatlpo commented Mar 7, 2020

thanks for the explanation Ryan.

I had to add tests for the methods that will be soon deprecated.

@apatlpo
Copy link
Contributor Author

apatlpo commented Mar 10, 2020

Ping @rabernat @roxyboy
All lights are green

@rabernat
Copy link
Collaborator

I had to add tests for the methods that will be soon deprecated.

Ah ok, I understand. In that case, please add a # pragma: no cover comment (https://coverage.readthedocs.io/en/coverage-4.3.3/excluding.html) to the deprecated methods and then remove the redundant tests.

Then we are done.

@apatlpo
Copy link
Contributor Author

apatlpo commented Mar 10, 2020

done

@rabernat
Copy link
Collaborator

Fantastic! Thanks so much for your persistence! We really appreciate having you as a contributor to xrft.

@rabernat rabernat merged commit 0ba161f into xgcm:master Mar 11, 2020
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.

4 participants