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

Implement area calculation for footprint of 3D objects #378

Merged
merged 49 commits into from
Mar 15, 2024

Conversation

w-k-jones
Copy link
Member

@w-k-jones w-k-jones commented Dec 4, 2023

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 new collapse_dim keyword has been added to get_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 than calculate_area are identical except for updates to imports

  • Have you followed our guidelines in CONTRIBUTING.md?
  • Have you self-reviewed your code and corrected any misspellings?
  • Have you written documentation that is easy to understand?
  • Have you written descriptive commit messages?
  • Have you added NumPy docstrings for newly added functions?
  • Have you formatted your code using black?
  • If you have introduced a new functionality, have you added adequate unit tests?
  • Have all tests passed in your local clone?
  • If you have introduced a new functionality, have you added an example notebook?
  • Have you kept your pull request small and limited so that it is easy to review?
  • Have the newest changes from this branch been merged?

@w-k-jones w-k-jones self-assigned this Dec 4, 2023
@w-k-jones w-k-jones added this to the Version 1.5.3 milestone Dec 4, 2023
@w-k-jones w-k-jones linked an issue Dec 4, 2023 that may be closed by this pull request
@w-k-jones w-k-jones added bug Code that is failing or producing the wrong result enhancement Addition of new features, or improved functionality of existing features labels Dec 4, 2023
Copy link

codecov bot commented Dec 4, 2023

Codecov Report

Attention: Patch coverage is 52.94118% with 168 lines in your changes are missing coverage. Please review.

Project coverage is 60.44%. Comparing base (df86e7f) to head (a5f126e).
Report is 9 commits behind head on RC_v1.5.x.

Files Patch % Lines
tobac/analysis/cell_analysis.py 12.19% 144 Missing ⚠️
tobac/analysis/feature_analysis.py 22.58% 24 Missing ⚠️
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     
Flag Coverage Δ
unittests 60.44% <52.94%> (+3.44%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@w-k-jones w-k-jones mentioned this pull request Dec 4, 2023
11 tasks
@w-k-jones w-k-jones force-pushed the 3d_area_calculation branch from 55e54f2 to 929483f Compare December 4, 2023 23:08
@fsenf
Copy link
Member

fsenf commented Mar 4, 2024

@w-k-jones : Unfortunately, the notebook is still failing to run through after your fix :-(

@w-k-jones
Copy link
Member Author

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

@fsenf
Copy link
Member

fsenf commented Mar 6, 2024

after #411 is done, this PR could be the next one. @w-k-jones : Any idea for a quickfix for the notebook bug?

@w-k-jones
Copy link
Member Author

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

@w-k-jones
Copy link
Member Author

w-k-jones commented Mar 13, 2024

I have added a fix to the issues with iris->xarray conversion (see convert_cube_to_dataarray in utils.decorators) which handles the situation in which a masked, integer cube would cause an error when converting to xarray. The notebooks now seem to all run correctly, even with older versions of xarray. I have also included updates from RC_v1.5.x and the fixes in #416 , so will wait for that to be merged before this.

@fsenf / @fziegner would you be able to review?

@fsenf
Copy link
Member

fsenf commented Mar 14, 2024

Yes, great job, @w-k-jones ! CI looks good! We will allocate more time for review tomorrow.

Copy link
Member

@fsenf fsenf left a 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
Copy link
Member

Choose a reason for hiding this comment

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

good catch

Comment on lines 347 to 365
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"},
),
},
)
Copy link
Member

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.

Copy link
Member Author

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

Comment on lines +14 to +36
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)
Copy link
Member

Choose a reason for hiding this comment

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

great hack!

@w-k-jones
Copy link
Member Author

Thanks @fsenf , I will update the case you highlighted to make my intentions clearer. Will wait until after #416 to merge

@freemansw1
Copy link
Member

Thanks @fsenf , I will update the case you highlighted to make my intentions clearer. Will wait until after #416 to merge

I've just merged #416

@w-k-jones w-k-jones merged commit 3a8ffba into tobac-project:RC_v1.5.x Mar 15, 2024
20 of 21 checks passed
@w-k-jones w-k-jones mentioned this pull request Mar 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Code that is failing or producing the wrong result enhancement Addition of new features, or improved functionality of existing features
Projects
None yet
Development

Successfully merging this pull request may close these issues.

bug in calculate_area
4 participants