Skip to content

Histogram compatibility with numpy 7364 #7856

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

Conversation

chelseatroy
Copy link
Contributor

The hist function should now work with either 'normed' or 'density' defined.

If neither are defined, default is 'False.'
If both are defined as different things, the function raises an error.

Some tests added, and comment documentation updated, to reflect the changes.

@tacaswell tacaswell added this to the 2.1 (next point release) milestone Jan 17, 2017
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="utf-8" standalone="no"?>
Copy link
Member

Choose a reason for hiding this comment

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

Please rebase and remove this file entirely.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi @QuLogic!

Thanks for the quick response. I rebased, but my changes are up to date with the current remote. The contents of determinism_tex.svg appear to be tested in test_backend_svg.py (line 204). Would you still like me to remove it?

Copy link
Member

Choose a reason for hiding this comment

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

This was added in your first commit. You'll need to rebase and edit the first commit to remove the file.

@@ -5875,10 +5875,10 @@ def table(self, **kwargs):
#### Data analysis

@_preprocess_data(replace_names=["x", 'weights'], label_namer="x")
def hist(self, x, bins=None, range=None, normed=False, weights=None,
def hist(self, x, bins=None, range=None, density=None, weights=None,
Copy link
Member

Choose a reason for hiding this comment

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

Changing the order is probably not a good idea, for backwards compatibility's sake, even though people really shouldn't be expecting order-only arguments to work this far in.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi @QuLogic!

My understanding is that if a user is expecting order-only arguments to work this far in, then their argument will just take the name density instead of normed now. And since the name density is meant, in this change, to actually replace normed, the code will still be backward compatible. If the developer specifically named it normed, then the fact that it's named allows it to work even though normed is at the end now.

Please let me know if this is inaccurate.

Copy link
Member

Choose a reason for hiding this comment

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

I think this is safe to do. If people are using positional it just works, if they are using keyword it still just works. By putting this here we make it clearer which the preferred name is.

@@ -5923,7 +5923,10 @@ def hist(self, x, bins=None, range=None, normed=False, weights=None,

Default is ``None``

normed : boolean, optional
normed or density : boolean, optional
Copy link
Member

Choose a reason for hiding this comment

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

normed, density

Copy link
Member

Choose a reason for hiding this comment

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

suggest putting 'density' first as that will (eventually) be the preferred name.


weights : (n, ) array_like or None, optional
An array of weights, of the same shape as `x`. Each value in `x`
only contributes its associated weight towards the bin count
(instead of 1). If `normed` is True, the weights are normalized,
(instead of 1). If `normed` and/or 'density' is True, the weights are normalized,
Copy link
Member

Choose a reason for hiding this comment

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

Probably too long; wrap at 79 characters.

so that the integral of the density over the range remains 1.

Default is ``None``

cumulative : boolean, optional
If `True`, then a histogram is computed where each bin gives the
counts in that bin plus all bins for smaller values. The last bin
gives the total number of datapoints. If `normed` is also `True`
gives the total number of datapoints. If `normed` and/or 'density' is also `True`
Copy link
Member

Choose a reason for hiding this comment

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

Probably too long; wrap at 79 characters.

@@ -6068,6 +6076,15 @@ def hist(self, x, bins=None, range=None, normed=False, weights=None,
.. plot:: mpl_examples/statistics/histogram_demo_features.py

"""

# This sets the density variable, if necessary, to its predecessor, 'normed.'
if density != None and normed != None and density != normed:
Copy link
Member

Choose a reason for hiding this comment

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

Use is not None.

# This sets the density variable, if necessary, to its predecessor, 'normed.'
if density != None and normed != None and density != normed:
raise ValueError('The density and normed arguments represent the same concept. Please set only one of them, or set them to the same value.')
elif normed != None and density == None:
Copy link
Member

Choose a reason for hiding this comment

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

Use is None.

@@ -2541,6 +2541,18 @@ def test_hist_stacked_normed():
ax = fig.add_subplot(111)
ax.hist((d1, d2), stacked=True, normed=True)

@image_comparison(baseline_images=['hist_stacked_normed'])
Copy link
Member

Choose a reason for hiding this comment

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

Two blank lines above.

@@ -3262,10 +3274,6 @@ def test_specgram_angle_freqs():
noverlap=noverlap, pad_to=pad_to, sides='onesided',
mode='phase', scale='dB')

with pytest.raises(ValueError):
Copy link
Member

Choose a reason for hiding this comment

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

Why is this removed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@QuLogic it shouldn't have been. My mistake—good catch!

d1 = np.linspace(1, 3, 20)
d2 = np.linspace(0, 10, 50)
fig = plt.figure()
ax = fig.add_subplot(111)
Copy link
Member

Choose a reason for hiding this comment

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

fig, ax = plt.subplots()

@QuLogic
Copy link
Member

QuLogic commented Jan 17, 2017

Mostly minor formatting things; it seems to make sense otherwise.

@NelleV
Copy link
Member

NelleV commented Jan 17, 2017

If that makes your life easier, we are using the tool pep8 to check for coherent styles in matplotlib. You can install it and run it locally on your computer, and maybe even configure your text editor to display the pep8 violations.

set, then that value will be used. If neither are set, then the args
will be treated as 'False.'

If both are set to different things, the hist function raises an
Copy link
Member

Choose a reason for hiding this comment

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

I suggest 'if both are set, raise' even if they are the same.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Change made and pushed.

Copy link
Member

Choose a reason for hiding this comment

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

The docs are out of sync with the code.

@chelseatroy
Copy link
Contributor Author

Pushed several changes as per @QuLogic's observations and suggestions!

@chelseatroy chelseatroy force-pushed the histogram_compatibility_with_numpy_7364 branch from bdcaab5 to 932e4c8 Compare January 18, 2017 18:29
@chelseatroy
Copy link
Contributor Author

Force pushed to remove determinism_tex.svg from 3 commits back.

@dstansby
Copy link
Member

dstansby commented Feb 1, 2017

There are still a few PEP 8 violations that are causing the tests to fail (https://travis-ci.org/matplotlib/matplotlib/jobs/193148586#L1777), would it be okay to fix them?

They look like they are all lines that are longer than 79 characters, which is the maximum allowed line length.


weights : (n, ) array_like or None, optional
An array of weights, of the same shape as `x`. Each value in `x`
only contributes its associated weight towards the bin count
(instead of 1). If `normed` is True, the weights are normalized,
so that the integral of the density over the range remains 1.
(instead of 1). If `normed` and/or 'density' is True,
Copy link
Member

Choose a reason for hiding this comment

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

should be just 'or'

If `cumulative` evaluates to less than 0 (e.g., -1), the direction
of accumulation is reversed. In this case, if `normed` is also
`True`, then the histogram is normalized such that the first bin
gives the total number of datapoints. If `normed` and/or 'density'
Copy link
Member

Choose a reason for hiding this comment

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

only 'or'

@@ -6032,7 +6042,7 @@ def hist(self, x, bins=None, range=None, normed=False, weights=None,
Returns
-------
n : array or list of arrays
The values of the histogram bins. See **normed** and **weights**
The values of the histogram bins. See **normed or density** and **weights**
Copy link
Member

Choose a reason for hiding this comment

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

This line is likely too long and needs to be wrapped.

@@ -2542,6 +2542,18 @@ def test_hist_stacked_normed():
ax.hist((d1, d2), stacked=True, normed=True)


@image_comparison(baseline_images=['hist_stacked_normed'])
Copy link
Member

@tacaswell tacaswell Feb 3, 2017

Choose a reason for hiding this comment

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

If you are going to add an image test you also need to commit the baseline images.

If this adds coverage we don't have other wise, the images (or at least the png) should be added; if not, just drop the decorator.

@tacaswell
Copy link
Member

@chelseatroy ping on this.

@chelseatroy
Copy link
Contributor Author

Hi @tacaswell. Just saw this. I guess for some reason I didn't receive a notification about your ping on this PR. If this still hasn't been done, I can make the changes this week...though I suspect someone has beat me to it.

@chelseatroy
Copy link
Contributor Author

@tacaswell I guess I'm pretty lost as to what to do from here. All the build checks are failing, I don't really understand why, I basically just changed the character width of some comments.

@efiring
Copy link
Member

efiring commented Jun 27, 2017

Presumably because of other changes since the last time you added a commit, there are conflicts so that your changes no longer merge cleanly. The "Resolve conflicts" button shows them nicely, and it looks like they will be easy to resolve.

@tacaswell
Copy link
Member

=================================== FAILURES ===================================
 PEP8-check(ignoring E111 E114 E115 E116 E121 E122 E123 E124 E125 E126 E127 E128 E129 E131 E226 E240 E241 E242 E243 E244 E245 E246 E247 E248 E249 E265 E266 E704 W503) 
[gw0] linux -- Python 3.6.1 /home/travis/build/matplotlib/matplotlib/venv/bin/python
/home/travis/build/matplotlib/matplotlib/lib/matplotlib/axes/_axes.py:5874:80: E501 line too long (80 > 79 characters)
            set, then that value will be used. If neither are set, then the args
                                                                               ^
/home/travis/build/matplotlib/matplotlib/lib/matplotlib/axes/_axes.py:6013:1: W293 blank line contains whitespace
^
/home/travis/build/matplotlib/matplotlib/lib/matplotlib/axes/_axes.py:6014:80: E501 line too long (80 > 79 characters)
        # Sets the density variable, if necessary, to its predecessor, 'normed.'
                                                                               ^
!!!!!!!!!!!! xdist.dsession.Interrupted: stopping after 1 failures !!!!!!!!!!!!!

Looks like there are 2 formatting issues.

elif normed is None and density is None:
density = False

def _normalize_input(inp, ename='input'):
Copy link
Member

Choose a reason for hiding this comment

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

I am confused by this? Did this come from the merge?

@dstansby
Copy link
Member

dstansby commented Aug 5, 2017

Hi @chelseatroy, thanks for the PR! Since there were two PRs I have merged them together with a few fixes and put them in #8993 - you will still be attributed to the work when it gets merged though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants