Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Factored out spectral analysis functions from mlab into a separate module #9757

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

Closed
wants to merge 1 commit into from
Closed

Conversation

DietBru
Copy link
Contributor

@DietBru DietBru commented Nov 12, 2017

The aim of this PR can be basically summarized by xkcd 26 - make basic spectral analysis in Matplotlib more accessible to occasional users. The main points are:

  • Since the number of parameters are quite high, an object based (like axis), instead of a function based interface was implemented. Utilizing setter and getter methods (@property) allows basic consistency checking when setting the attributes.
  • Emphasis was put on getting the scaling and the units right (see example spectrum_scalings.py)
  • Try to make the difference between spectras and spectral densities less confusing by introducing two
    y-axes (see image below).
  • Simplify the use of standard windows (see window_spectrums.py).

This is work in progress. The interface is not fully stable yet, the documentation needs more work, and tests don't exist yet. It still would be nice to get some feedback on the basic approach, especially in relation to #9151. Of course, any help is warmly welcomed.

Little Example

The following code (taken from psd_shotnoise.py)

... 
PS = Periodogram(x, f_s=1/T, window='hann', nperseg=n//8, noverlap=n//16,
                 detrend='mean', unit='V')
fig1, axx1 = PS.plot(density=True, yscale='db', xscale='log', fig_num=1)
axx1.set_xlim(PS.f[1], PS.f_s/2)

produces
psd

PR Checklist

  • Find a solution for Second y-axis does not scale correctly when using zoom tool #9616
  • Wrap mlab functions
  • Find solution for sphinx for error more than one target found for cross-reference for inherited methods and attributes.
  • Has Pytest style unit tests
  • Code is PEP 8 compliant
  • New features are documented, with examples if plot related
  • Documentation is sphinx and numpydoc compliant
  • Added an entry to doc/users/next_whats_new/ if major new feature (follow instructions in README.rst there)
  • Documented in doc/api/api_changes.rst if API changed in a backward-incompatible way

weiter im Text

Traitlets komplett rausgeschmissen

weiter im Text

Refactor out mlab's spectral functions
@NelleV
Copy link
Member

NelleV commented Nov 12, 2017

I'm concerned by the addition of a different class instead of relying on our Axes class. I don't see the advantage, and the inconvenience is that this not coherent at all with the current API. Had this been discussed elsewhere?

@efiring
Copy link
Member

efiring commented Nov 12, 2017

It's nice--but it looks to me like this is heading towards being an independent toolkit package rather than something that should be included in the main matplotlib repository. The heart of it is a specialized set of calculations; the plotting is almost incidental. We want to move matplotlib towards being a general plotting library, and delegate analysis calculations and specialized plotting to other libraries. This could be very useful as one of those other libraries.

@jklymak
Copy link
Member

jklymak commented Nov 12, 2017

Aren't lot of these calculations inscipy.signal? I agree w/ @NelleV and @efiring that the calculations and plotting should be kept as separate as possible.

We just had a conversation about these fncs on gitter. The consensus seemed to be to try and deprecate the mlab functions, and move the _axes methods to just call the scipy.signal functions. It probably needs its own issue, or further discussion in #9151

@dstansby
Copy link
Member

dstansby commented Nov 13, 2017

I think in an ideal world Matplotlib would just call scipy methods where possible. The problem is Matplotlib doesn't depend on scipy because historically installing scipy was difficult. So, the decision that I think needs to be made before we go any further with the mlab spectral code is is

Should Matplotlib expect users who want to plot spectral data to have an installation of scipy?

  • If the answer is yes, then the spectral functions need an overhaul and major API change to conform to scipy
  • If the answer is no, we can stick with the current functions and work on something like this PR.

@DietBru
Copy link
Contributor Author

DietBru commented Nov 13, 2017

I do agree that it probably would be better to utilize the scipy.signal functions though I think the worries about complexity are not really substantiated (we are talking about maybe less than fifty lines of algorithmic code). The majority of the code (minus the examples) is consistency checking and emitting meaningful error messages.
What makes spectral analysis difficult is the required attention to detail of parameters and scaling (If I counted right, there are 9 parameters for a periodogram and 18 scalings of the y-axis). So I'm not sure if the plotting is incidental as @efiring claimed. I believe automatically providing the essential information in the plot (precise label names, correct units, frequency resolution, etc) lowers the learning curve for beginners considerably. I know this contradicts a bit the goal stated by @jklymak of separating calculation and plotting as much as possible.
So besides the question of the dependency of SciPy raised by @dstansby I would add the question: Is it worthwhile for Matplotlib to provide a less antiquated, low-level and more pythonic interface for basic spectral analysis. If this PR is anywhere near the sweet spot is of course a point of further discussion.

@anntzer
Copy link
Contributor

anntzer commented Dec 23, 2017

It looks like none of @NelleV @efiring @jklymak (as well as myself) believe that Matplotlib should include such a feature, and @dstansby seems to be OK either way (depending on the scipy situation). So I'll close the PR.

I also believe that this functionality belongs to a third-party module, which can be listed in the third-party packages page (https://matplotlib.org/thirdpartypackages/index.html).

@anntzer anntzer closed this Dec 23, 2017
@tacaswell
Copy link
Member

Thank you for your work on this @vollbier please do not be discouraged that this is not merged!

I agree with @efiring @anntzer that this would be better suited as a stand alone package which depends on Matplotlib. The two technical benefits to this are being able have more liberal dependencies than Matplotlib (ex scipy, fftw, etc) and to operate on your own release schedule. It also lets you have a much more opinionated domain specific API. This may mean something like seaborn (/https://seaborn.pydata.org) / altair ( https://altair-viz.github.io/) which are tuned to particular data structures and statistical computations or something like windrose (https://github.com/python-windrose/windrose/) which aggressively customizes the Axes object.

The tight coupling between data / computation and the axis labels, ticks, and scales breaks some of the core abstraction of Matplotlib and poses some problems for composition (what happens if I want to show two of these plots with different windowing? That information is in the title so one of them will be labeled wrong).

The pattern of having a data / computation based object that you then call plot on is not a pattern we current have in the library (but both pandas and lmfit do!) and I am not sure we want to bring in.

I am open to it living at matplotlib/mpl-spectra (or something similar) and leaving it under your (@vollbier ) control. Examples of projects like this are https://matplotlib.org/mpl-probscale/ or https://matplotlib.org/cmocean/ .

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.

7 participants