Skip to content

Optimize time index resolution in tensors.py with $O(1)$ set lookups#1693

Open
copybara-service[bot] wants to merge 1 commit into
mainfrom
test_941783042
Open

Optimize time index resolution in tensors.py with $O(1)$ set lookups#1693
copybara-service[bot] wants to merge 1 commit into
mainfrom
test_941783042

Conversation

@copybara-service

@copybara-service copybara-service Bot commented Jul 10, 2026

Copy link
Copy Markdown

Optimize time index resolution in tensors.py with $O(1)$ set lookups

  • Convert string and coordinate collections to Python sets before membership checks in _resolve_time_indices, _validate_selected_times, and build_counterfactual_inputs.
  • Reduce time index resolution runtime from $O(N \times M)$ linear scans to $O(1)$ set lookups, speeding up counterfactual evaluation across optimization grid iterations.
  • Add DataArray string type conversions to prevent DataArray unhashability TypeError in xarray coordinate matching.

Tested:
We tested this optimization, but it might not cover all test cases or usage scenarios. As this change is for open source, please review and test for your specific requirements before adoption.

@google-cla

google-cla Bot commented Jul 10, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

- Convert string and coordinate collections to Python sets before membership checks in `_resolve_time_indices`, `_validate_selected_times`, and `build_counterfactual_inputs`.
- Reduce time index resolution runtime from $O(N \times M)$ linear scans to $O(1)$ set lookups, speeding up counterfactual evaluation across optimization grid iterations.
- Add DataArray string type conversions to prevent DataArray unhashability `TypeError` in xarray coordinate matching.

Tested:
We tested this optimization, but it might not cover all test cases or usage scenarios. As this change is for open source, please review and test for your specific requirements before adoption.
PiperOrigin-RevId: 941783042
@copybara-service copybara-service Bot changed the title Optimize time index resolution in tensors.py with O(1) set lookups Optimize time index resolution in tensors.py with $O(1)$ set lookups Jul 10, 2026
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.

0 participants