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

test decoding num_dates in float types #1863

Merged
merged 1 commit into from
Feb 2, 2018
Merged

test decoding num_dates in float types #1863

merged 1 commit into from
Feb 2, 2018

Conversation

j08lue
Copy link
Contributor

@j08lue j08lue commented Jan 28, 2018

@j08lue
Copy link
Contributor Author

j08lue commented Jan 28, 2018

For comparison, I added the same test to the 0.10.0 version in https://github.com/j08lue/xarray/tree/0p10-f4-time-decode, where it fails:

pytest -v xarray\tests\test_conventions.py

...

>               self.assertArrayEqual(expected, actual_cmp)
E               AssertionError:
E               Arrays are not equal
E
E               (mismatch 90.0%)
E                x: array([datetime.datetime(2000, 1, 1, 0, 0),
E                      datetime.datetime(2000, 1, 2, 0, 0),
E                      datetime.datetime(2000, 1, 3, 0, 0),...
E                y: array(['2000-01-01T00:00:00.000000', '2000-01-02T00:00:00.003211',
E                      '2000-01-03T00:00:00.006422', '2000-01-04T00:00:00.001245',
E                      '2000-01-05T00:00:00.012845', '2000-01-06T00:00:00.024444',...

@j08lue
Copy link
Contributor Author

j08lue commented Jan 28, 2018

I believe the issue originates in these lines:

flat_num_dates_ns_int = (flat_num_dates *
_NS_PER_TIME_DELTA[delta]).astype(np.int64)

where we multiply the num_dates with some float value and then cast to int64.

If num_dates is float32, numpy keeps float32 when multiplying with e.g. 1e9 and that somehow introduces an error. Here is a stripped version of the above:

flat_num_dates = np.arange(100).astype('float32')
n = 1e9
roundtripped = (flat_num_dates * n).astype(np.int64) / n
assert np.all(flat_num_dates == roundtripped)

By the way, the factor has to be large, like 1e9. E.g. 1e6 ('ms since ...') won't give the error.

The weird thing is that the corresponding code in the current master is identical:

flat_num_dates_ns_int = (flat_num_dates *
_NS_PER_TIME_DELTA[delta]).astype(np.int64)

I will look into why the result is still different from v0.10.0.

Also, if this really is the origin of the error, there are two easy ways to avoid this:

  1. Cast flat_num_dates to float64: (flat_num_dates.astype(np.float64) * n).astype(np.int64)
  2. Store _NS_PER_TIME_DELTA values as int, then numpy will do the casting.

@j08lue
Copy link
Contributor Author

j08lue commented Jan 28, 2018

Store _NS_PER_TIME_DELTA values as int, then numpy will do the casting.

Haha, OK, this is actually what you implemented in 50b0a69:

_NS_PER_TIME_DELTA = {'us': int(1e3),
'ms': int(1e6),
's': int(1e9),
'm': int(1e9) * 60,
'h': int(1e9) * 60 * 60,
'D': int(1e9) * 60 * 60 * 24}

So that is why it works now.

Case closed, I guess.

@shoyer
Copy link
Member

shoyer commented Feb 2, 2018

Nice, thank you @j08lue for tracking this down and adding tests!

@shoyer shoyer merged commit becd77c into pydata:master Feb 2, 2018
@j08lue j08lue deleted the test-coding-times-float branch February 10, 2018 12:16
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.

Time decoding has round-off error 0.10.0. Gone now.
2 participants