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

Faster time coordinate categorization #5999

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

bouweandela
Copy link
Member

@bouweandela bouweandela commented Jun 11, 2024

🚀 Pull Request

Description

Make time coordinate categorization faster by converting the time points to datetimes in a single call.

Example script:

In [1]: import numpy as np

In [2]: import cf_units

In [3]: from iris.cube import Cube

In [4]: from iris.coords import DimCoord

In [5]: from iris.coord_categorisation import add_year

In [6]: coord = DimCoord(np.arange(10000), standard_name='time', units=cf_units.Unit("days since epoch", "standard"))

In [7]: cube = Cube(np.empty(10000), dim_coords_and_dims=[(coord, 0)])

In [8]: %timeit add_year(cube.copy(), 'time')
9.99 ms ± 24.1 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

while with the current main branch, the timing result is

345 ms ± 2.07 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

Consult Iris pull request check list


Add any of the below labels to trigger actions on this PR:

  • benchmark_this Request that this pull request be benchmarked to check if it introduces performance shifts

Copy link

codecov bot commented Jun 11, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 89.75%. Comparing base (4cf1767) to head (0a68294).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #5999       /-   ##
==========================================
- Coverage   89.75%   89.75%   -0.01%     
==========================================
  Files          90       90              
  Lines       22929    22928       -1     
  Branches     5020     5021        1     
==========================================
- Hits        20580    20579       -1     
  Misses       1618     1618              
  Partials      731      731              

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

@bouweandela bouweandela marked this pull request as ready for review June 11, 2024 08:18
@bouweandela
Copy link
Member Author

Would it make sense to add the example code above as a benchmark?

@@ -42,7 51,11 @@ def add_categorised_coord(cube, name, from_coord, category_function, units="1"):
coordinate point-value.
units : str, default="1"
Units of the category value, typically 'no_unit' or '1'.

category_function_expects_date : bool, default=False
Copy link
Member Author

Choose a reason for hiding this comment

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

As an alternative to adding this extra argument, I could also add a new function add_categorised_time_coord. This function could then also take a simpler category_function argument, which would accept a function with only the point value argument, as the coord argument is no longer needed in any of the time categorization functions below.

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

Successfully merging this pull request may close these issues.

None yet

3 participants