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

Optimisation in excprob function #424

Merged
merged 1 commit into from
Aug 20, 2024
Merged

Optimisation in excprob function #424

merged 1 commit into from
Aug 20, 2024

Conversation

viktor40
Copy link
Contributor

@viktor40 viktor40 commented Aug 19, 2024

Currently pysteps.postprocesing.ensemblestats.excprob copies the input array only to replace all values afterwards by either a 0or a 1. It is much more efficient to just initialize an array of the same shape using np.zeros(). By doing this, the code can also be simplified by removing the part of the code setting values to 0.

Below, is example code showing the difference in performance between the two methods. This is for a 700x700 grid with 12 timesteps and 24 ensemble members. This shows a significant difference. The specific part of the code is sped up by four orders of magnitude, going from taking 153 ms to 20.5 µs.

import numpy as np
s = (24, 12, 700, 700)
test_arr = np.random.randint(1., 5., size=s)

%timeit test_arr.copy()
>>> 153 ms ± 4.87 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)

%timeit np.zeros(s)
>>> 20.5 µs ± 656 ns per loop (mean ± std. dev. of 7 runs, 10,000 loops each)

@viktor40 viktor40 requested a review from ladc August 19, 2024 10:44
Copy link

codecov bot commented Aug 19, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 83.87%. Comparing base (953f799) to head (30faaf2).
Report is 10 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #424      +/-   ##
==========================================
+ Coverage   83.52%   83.87%   +0.35%     
==========================================
  Files         159      160       +1     
  Lines       12575    12779     +204     
==========================================
+ Hits        10503    10719     +216     
+ Misses       2072     2060      -12     
Flag Coverage Δ
unit_tests 83.87% <100.00%> (+0.35%) ⬆️

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.

Copy link
Member

@dnerini dnerini left a comment

Choose a reason for hiding this comment

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

Nice work, thanks!

Copy link
Contributor

@ladc ladc left a comment

Choose a reason for hiding this comment

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

Looks good, Viktor. Every (micro-)second matters in nowcasting!

@dnerini dnerini merged commit 9803d54 into master Aug 20, 2024
10 checks passed
@dnerini dnerini deleted the vis-performance branch August 20, 2024 08:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants