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

Wind barb plotting bug fix #1918

Closed
wants to merge 1 commit into from
Closed

Conversation

23ccozad
Copy link
Contributor

Background: The nightly build has been failing due to an error occurring in test_declarative_station_plot_fontsize(), which recently merged into the main branch. That test passed all checks when it was in PR #1899 before merging, where NumPy 1.20.3 and Pandas 1.2.4 were being used. The nightly build is running tests with NumPy 1.21.0rc2 and Pandas 1.3.0rc1, and this error is occurring with those versions.

Problem (from the MetPy perspective): The problem is related to plotting wind barbs. The last point of contact the stack trace has in MetPy is in StationPlot's plot_barb() method where we call barbs() on the matplotlib axes. I figured that might be the best place to start working on a solution. For this particular test, I found that we currently pass these variables to barbs():

  • x: A NumPy array of x-coordinates for locations of the wind barbs
  • y: A NumPy array of y-coordinates for locations of the wind barbs
  • u: A NumPy array of Pint quantity objects containing the u-components of the wind vectors
  • v: A NumPy array of Pint quantity objects containing the v-components of the wind vectors

It turns out that if we just have the magnitudes of the components u and v in the NumPy array, instead of the entire Pint object, then we avoid this error.

It is worth noting that there are 15 other plotting tests use plot_barb(), and none of them had an error because they already had NumPy arrays of floats, rather than Pint objects, for u and v. I think the reason for this is that test_declarative_station_plot_fontsize() is the only test using parse_metar_file() to get surface wind data for plotting. This parsing method provides columns eastward_wind and northward_wind that contain Pint objects, and these end up being used for u and v.

Solution: The suggested changes here will remove the units from the NumPy arrays passed to u and v, only if needed. The only thing I'm hesitant about with this is adding from ..units import units just to resolve a bug fix. Maybe there is another approach without needing that import.

Checklist

@kgoebber
Copy link
Collaborator

Hmmm, we had been getting rid of the units when going to plot with plot_barbs through the function _vector_plotting_units, which is using a call to np.array as the method to strip the units - could it be that that call is no longer stripping the units?

So instead we could modify in station_plot.py on line 331-334

        if plotting_units:
            if hasattr(u, 'units') and hasattr(v, 'units'):
                u = u.to(plotting_units).magnitude
                v = v.to(plotting_units).magnitude

Then we should also be able to remove lines 339-341 as we wouldn't need to strip any units at that point.

@23ccozad 23ccozad force-pushed the wind_barbs_bug branch 2 times, most recently from 24ea1dc to d8b4f57 Compare June 18, 2021 15:57
@23ccozad
Copy link
Contributor Author

@kgoebber I relocated some of my changes to _vector_plotting_units(), thanks for providing some clarity on that method. Also, attempted your suggestion to add .magnitude after the unit conversion. I think this didn't work because, in the failing case, u and v are arrays of Pint quantities, not Pint quantities containing an array.

The latest commit here modifies _vector_plotting_units() to check whether u and v are arrays of Pint objects. If so, we restructure u and v so they are each a singular Pint quantity, where the magnitude is an array of floats. The units that had been removed from the array are then used as the units for the new Pint quantity.

Handle arrays of Pint objects when plotting wind barbs
@23ccozad 23ccozad marked this pull request as ready for review June 18, 2021 16:50
@23ccozad 23ccozad added Area: Plots Pertains to producing plots Area: Units Pertains to unit information Type: Bug Something is not working like it should labels Jun 18, 2021
@dopplershift
Copy link
Member

So after digging in some more with @23ccozad, the root cause here really is that parse_metar_file is returning a DataFrame where some columns have an object dtype, which is not something we want. It turns out with pandas 1.3, if we stuff an array Quantity into a column, instead of dropping the units, it stuff a Quantity in each element.

@23ccozad
Copy link
Contributor Author

Resolving issue #1916 (failing nightly build) is continued in PR #1921.

@23ccozad 23ccozad closed this Jun 18, 2021
@kgoebber
Copy link
Collaborator

This is why we have tests and great that we have that nightly test build!

@23ccozad 23ccozad deleted the wind_barbs_bug branch July 16, 2021 19:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: Plots Pertains to producing plots Area: Units Pertains to unit information Type: Bug Something is not working like it should
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Nightly build is failing
3 participants