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

Add xarray support to MathematicalExpression #621

Merged
merged 26 commits into from
Nov 3, 2021

Conversation

vhirtham
Copy link
Collaborator

@vhirtham vhirtham commented Oct 29, 2021

Changes

MathematicalExpression now uses xarray internally. This can simplify multi-dimensional expressions if one uses DataArrays with correctly named dimensions. If no DataArrays are used as parameters and variables, the behavior should remain more or less the same since xarray will automatically add dimension names.

In the current version evaluate will now return a DataArray instead of a quantity. This requires some changes to the internals of the TimeSeries. However, when those changes are fully applied, the TimeSeries implementation should be much simpler than before.

Related Issues

Closes #619

Checks

  • update schema
  • updated CHANGELOG.rst
  • updated tests
  • updated doc/
  • update example/tutorial notebooks
  • update manifest file

@vhirtham vhirtham added xarray issues related to handling xarray objects core weldx core classes and functions labels Oct 29, 2021
@vhirtham vhirtham self-assigned this Oct 29, 2021
@github-actions
Copy link

github-actions bot commented Oct 29, 2021

Unit Test Results

       1 files  ±0         1 suites  ±0   2m 4s ⏱️ -38s
1 925 tests +2  1 925 ✔️ +2  0 💤 ±0  0 ±0 

Results for commit e2f0e39. ± Comparison against base commit 6e78782.

♻️ This comment has been updated with latest results.

weldx/core.py Outdated Show resolved Hide resolved
@review-notebook-app
Copy link

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

@codecov
Copy link

codecov bot commented Nov 1, 2021

Codecov Report

Merging #621 (e2f0e39) into master (6e78782) will decrease coverage by 0.06%.
The diff coverage is 94.54%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #621      +/-   ##
==========================================
- Coverage   94.62%   94.56%   -0.07%     
==========================================
  Files          93       93              
  Lines        5936     5958      +22     
==========================================
+ Hits         5617     5634      +17     
- Misses        319      324       +5     
Impacted Files Coverage Δ
weldx/time.py 97.73% <80.00%> (-0.43%) ⬇️
weldx/tags/core/mathematical_expression.py 96.66% <95.00%> (-3.34%) ⬇️
weldx/core.py 97.35% <96.55%> (-1.31%) ⬇️
weldx/measurement.py 95.57% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 6e78782...e2f0e39. Read the comment docs.

@vhirtham vhirtham marked this pull request as ready for review November 1, 2021 15:31
weldx/core.py Outdated Show resolved Hide resolved
weldx/tests/test_core.py Show resolved Hide resolved
@CagtayFabry CagtayFabry added this to the 0.5.1 milestone Nov 2, 2021
@vhirtham
Copy link
Collaborator Author

vhirtham commented Nov 2, 2021

I updated the schema and did some small refactoring. @CagtayFabry You might want to have a look at the changes. If there are no further concerns, I will merge this PR after lunch.

Comment on lines 35 to 37
anyOf:
- wx_property_tag: "asdf://weldx.bam.de/weldx/tags/core/data_array-0.1.*"
- wx_property_tag: "asdf://weldx.bam.de/weldx/tags/units/quantity-0.1.*"
Copy link
Member

Choose a reason for hiding this comment

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

I would prefer if we don't have data_array here as it is very verbose, that should still be possible right? @vhirtham

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Well, the solution would be to not use data arrays as parameters if you don't need to. But if I have a multi-dimensional equation that uses data arrays as parameters to make my life easier, I can't store it in asdf unless I sort out the dimensions myself and restore the parameters as quantities.

@CagtayFabry
Copy link
Member

I updated the schema and did some small refactoring. @CagtayFabry You might want to have a look at the changes. If there are no further concerns, I will merge this PR after lunch.

if we want to allow data_array tags in the schema we should bump the schema version (which I would like to avoid for the 0.5.x releases)

@vhirtham
Copy link
Collaborator Author

vhirtham commented Nov 2, 2021

I updated the schema and did some small refactoring. @CagtayFabry You might want to have a look at the changes. If there are no further concerns, I will merge this PR after lunch.

if we want to allow data_array tags in the schema we should bump the schema version (which I would like to avoid for the 0.5.x releases)

Since the schema is now less restrictive, no existing file will break. Furthermore, the current userbase is just us. So I think we can get away with not bumping the version.

@CagtayFabry
Copy link
Member

I updated the schema and did some small refactoring. @CagtayFabry You might want to have a look at the changes. If there are no further concerns, I will merge this PR after lunch.

if we want to allow data_array tags in the schema we should bump the schema version (which I would like to avoid for the 0.5.x releases)

Since the schema is now less restrictive, no existing file will break. Furthermore, the current userbase is just us. So I think we can get away with not bumping the version.

true, but I find the xarray schema for simple things like this borderline unreadable in the schema
Also I am not sure how far the implementation for wx_unit validation is with xarray which I figure would be very important here

can some parameters be passed as xarray and others as quantities in the current python implementation?

weldx/core.py Outdated
@@ -450,7 +467,7 @@ def _interp_time_discrete(self, time: Time) -> xr.DataArray:
"""Interpolate the time series if its data is composed of discrete values."""
return ut.xr_interp_like(
self._data,
{"time": time.as_timedelta()},
{"time": time.as_timedelta_index()},
Copy link
Member

Choose a reason for hiding this comment

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

we should pass the reference time here as well

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

well, if you use as_data_array here, you get the "non-monotonic" error I mentioned above 😉

@vhirtham
Copy link
Collaborator Author

vhirtham commented Nov 2, 2021

I updated the schema and did some small refactoring. @CagtayFabry You might want to have a look at the changes. If there are no further concerns, I will merge this PR after lunch.

if we want to allow data_array tags in the schema we should bump the schema version (which I would like to avoid for the 0.5.x releases)

Since the schema is now less restrictive, no existing file will break. Furthermore, the current userbase is just us. So I think we can get away with not bumping the version.

true, but I find the xarray schema for simple things like this borderline unreadable in the schema

Why in the schema? Did you mean the written file?
My opinion: The goal should always be to get full functionality with good readability. However, if we can't have both, then functionality is way more important to me.

Also I am not sure how far the implementation for wx_unit validation is with xarray which I figure would be very important here

If this is a problem it needs to be addressed in a follow-up issue/PR.

can some parameters be passed as xarray and others as quantities in the current python implementation?

Yes. Every parameter is stored internally (and also written to asdf) as a quantity unless it is a DataArray, which will be stored as DataArray. Only during evaluation, everything is promoted to a DataArray to perform the calculation. But that doesn't affect the internal variables

@CagtayFabry
Copy link
Member

Yes. Every parameter is stored internally (and also written to asdf) as a quantity unless it is a DataArray, which will be stored as DataArray. Only during evaluation, everything is promoted to a DataArray to perform the calculation. But that doesn't affect the internal variables

In that case I guess the schema wouldn't work for 'mixed' parameters anyway since wx_property_tag doesn't support this

I'll catch up with you later so we can discuss in detail :)

@vhirtham
Copy link
Collaborator Author

vhirtham commented Nov 2, 2021

Yes. Every parameter is stored internally (and also written to asdf) as a quantity unless it is a DataArray, which will be stored as DataArray. Only during evaluation, everything is promoted to a DataArray to perform the calculation. But that doesn't affect the internal variables

In that case I guess the schema wouldn't work for 'mixed' parameters anyway since wx_property_tag doesn't support this

I'll catch up with you later so we can discuss in detail :)

All right, I will take care of the tutorials in the meantime.

@vhirtham
Copy link
Collaborator Author

vhirtham commented Nov 2, 2021

Just implemented the matadata approach and tested it. Works like a charm. I will add the tuple construction tomorrow and add a warning if coordinates are dropped.

@vhirtham vhirtham requested a review from CagtayFabry November 3, 2021 14:01
Copy link
Member

@CagtayFabry CagtayFabry left a comment

Choose a reason for hiding this comment

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

looks really clean now 👍

@vhirtham vhirtham merged commit d8dd276 into BAMWelDX:master Nov 3, 2021
@vhirtham vhirtham deleted the me_xarray branch November 3, 2021 14:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core weldx core classes and functions xarray issues related to handling xarray objects
Projects
None yet
Development

Successfully merging this pull request may close these issues.

MathematicalExpression should support xarray.DataArrays
2 participants