-
Notifications
You must be signed in to change notification settings - Fork 54
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
Implement area calculation for footprint of 3D objects #378
Implement area calculation for footprint of 3D objects #378
Conversation
…s of labels to be collapsed
…r more dims of labels to be collapsed
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## RC_v1.5.x #378 +/- ##
=============================================
+ Coverage 57.00% 60.44% +3.44%
=============================================
Files 20 23 +3
Lines 3454 3522 +68
=============================================
+ Hits 1969 2129 +160
+ Misses 1485 1393 -92
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
… when trying to collapse all axes
…into 3d_area_calculation
55e54f2
to
929483f
Compare
…ersion from idealised case notebook
@w-k-jones : Unfortunately, the notebook is still failing to run through after your fix :-( |
I haven’t addressed the issue with the notebook yet, still deciding what action to take. Pinning the xarray requirements is the easiest but I generally try to avoid doing that |
after #411 is done, this PR could be the next one. @w-k-jones : Any idea for a quickfix for the notebook bug? |
The easiest fix will be to ensure the correct xarray version, although I'm slightly against doing that |
I have added a fix to the issues with iris->xarray conversion (see |
Yes, great job, @w-k-jones ! CI looks good! We will allocate more time for review tomorrow. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Dear Will,
really great work! Esp. the expansion of testing capabilities (~1000 new lines) is exceptional!
I marked a small inconsistency, but I am also happy if you just proceed and merge you devs back to the release candidate.
Cheers, Fabian.
@@ -413,7 +413,7 @@ def calculate_area(features, mask, method_area=None, vertical_coord=None): | |||
) | |||
elif method_area == "latlon": | |||
if (mask_slice.coord("latitude").ndim == 1) and ( | |||
mask_slice.coord("latitude").ndim == 1 | |||
mask_slice.coord("longitude").ndim == 1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good catch
tobac/tests/test_analysis_spatial.py
Outdated
test_labels = xr.DataArray( | ||
test_labels.values, | ||
dims=( | ||
"time", | ||
"latitude", | ||
"x_dim", | ||
), | ||
coords={ | ||
"time": [datetime(2000, 1, 1), datetime(2000, 1, 1, 1)], | ||
"latitude": xr.DataArray( | ||
np.arange(5), dims="latitude", attrs={"units": "degrees"} | ||
), | ||
"longitude": xr.DataArray( | ||
np.stack([np.arange(5)] * 5), | ||
dims=("x_dim", "latitude"), | ||
attrs={"units": "degrees"}, | ||
), | ||
}, | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks inconsistent. I am not sure why you replace "longitude" with "x_dim", but it should be done for "latitude" in a similar way to be consistent.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this case it's meant to be inconsistent, as I was trying to test the error condition for when a dataset has 1D latitude but 2D longitude (i.e. inconsistent dimensionality on lat/lon). I will update the dimension naming to make it a bit clearer however
def convert_cube_to_dataarray(cube): | ||
""" | ||
Convert an iris cube to an xarray dataarray, averting error for integer dtype cubes in xarray<v2023.06 | ||
|
||
Parameters | ||
---------- | ||
cube : iris.cube.Cube | ||
Iris data cube | ||
|
||
Returns | ||
------- | ||
dataarray : xr.DataArray | ||
dataarray converted from cube. If the cube's core data is a masked array and has integer dtype, | ||
the returned datarray will have a numpy array with masked values filled with the minimum value for | ||
that integer dtype. Otherwise the data will be identical to that produced using xr.DataArray.from_iris | ||
""" | ||
if isinstance(cube.core_data(), ma.core.MaskedArray) and np.issubdtype( | ||
cube.core_data().dtype, np.integer | ||
): | ||
return xr.DataArray.from_iris( | ||
cube.copy(cube.core_data().filled(np.iinfo(cube.core_data().dtype).min)) | ||
) | ||
return xr.DataArray.from_iris(cube) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
great hack!
Resolves #333
Changes
calculate_area
to use bulk statistics for area calculation, and enables area calculation for both 2D and 3D tracked objects. Area is now not calculated with a time dimension when using lat/lon coords, reducing memory usage. A newcollapse_dim
keyword has been added toget_statistics_from_mask
, allowing one or more dimensions of the input labels to be collapsed. This, for example, allows calculation of the 2D area of the footprint of a 3D tracked object.I have also reorganised
analysis
into a package, with a separate file for spatial functions. Functions other thancalculate_area
are identical except for updates to imports