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

Improved CF decoding #6812

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open

Improved CF decoding #6812

wants to merge 5 commits into from

Conversation

mankoff
Copy link
Contributor

@mankoff mankoff commented Jul 19, 2022

The comments above this line state, "so we just use a float64" but then it returns np.float32. I assume the comments are correct. Changing this also fixes a bug I ran into.

Note that currently, _choose_float_dtype returns float32 if the data is float16 or float32, even if the scale_factor dtype is float64.

@mankoff
Copy link
Contributor Author

mankoff commented Jul 19, 2022

Note - I also have not run the "Running the performance test suite" code in https://xarray.pydata.org/en/stable/contributing.html - I assume changing from float32 to float64 would impact performance. I can run that if suggested.

@mankoff
Copy link
Contributor Author

mankoff commented Jul 19, 2022

I'm reading more in https://github.com/pydata/xarray/blob/2a5686c6fe855502523e495e43bd381d14191c7b/xarray/coding/variables.py and I'm confused about some logic:

add_offset = pop_to(attrs, encoding, "add_offset", name=name)
dtype = _choose_float_dtype(data.dtype, "add_offset" in attrs)

pop_to does a pop operation - it removes the key/value pair. So line 1 above will remove add_offset from attrs if it exists. The second line then checks for "add_offset" in attrs which should always be False.

I think this is happening based on inspecting with the debugger.

Furthermore, the fix I implemented in this Pull Request which returns np.float64 fixes my bug, but only because this bug exists. My dataset has add_offset, so the lines I changed:

        if not has_offset:
            return np.float64

should not run, but do run because of this issue.

Line above this removes 'add_offset' from 'attrs' (if it exists), so
'"add_offset" in attrs' should always be false. It was moved into
'encoding' so let's check for it there.
Modified _choose_float_dtype
+ Returns float32 if inputs are float16 or float32
+ Returns float64 if inputs are int
Comment on lines 239 to 240
if not has_offset:
return np.float32
return np.float64
Copy link
Contributor

Choose a reason for hiding this comment

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

I think the code matches the comments. It would be clearer if written as

if has_offset:
    return np.float64
else:
    return np.float32

Without your edits, if there is an offset the condition does not trigger and we return np.float64 later

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for reviewing this pull request. FYI my original comment (later edited) said:

Also, before this is merged, I'd like to suggest a larger change, and possibly discuss architecture here a bit (if appropriate). Specifically, I'd like to change the _choose_float_dtype function, and the two calls to it, to pass in the dtype of scale_factor and add_offset, in addition to the data dtype. This function should then return the dtype of the highest precision of three.

Currently, _choose_float_dtype returns float32 if the data is float16 or float32, even if the scale_factor dtype is float64.

Based on your comment, I think my original intuition - that this function needs a large rewrite - is correct. I'll look into this and submit additional commits to this PR.

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for looking into this!

@@ -269,7 +269,7 @@ def decode(self, variable, name=None):
if "scale_factor" in attrs or "add_offset" in attrs:
scale_factor = pop_to(attrs, encoding, "scale_factor", name=name)
add_offset = pop_to(attrs, encoding, "add_offset", name=name)
dtype = _choose_float_dtype(data.dtype, "add_offset" in attrs)
dtype = _choose_float_dtype(data.dtype, "add_offset" in encoding)
Copy link
Contributor

Choose a reason for hiding this comment

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

I suspect this fixed one issue, but the original issue still remains because we still aren't looking at the dtype of scale_factor and add_offset as recommended by the conventions.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Note - I think the conventions referred to above are: https://cfconventions.org/Data/cf-conventions/cf-conventions-1.7/build/ch08.html or

If the scale_factor and add_offset attributes are of the same data type as the associated variable, the unpacked data is assumed to be of the same data type as the packed data. However, if the scale_factor and add_offset attributes are of a different data type from the variable (containing the packed data) then the unpacked data should match the type of these attributes, which must both be of type float or both be of type double. An additional restriction in this case is that the variable containing the packed data must be of type byte, short or int. It is not advised to unpack an int into a float as there is a potential precision loss.

if not has_offset:
if has_offset:
return np.float64
else:
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Reverted to original algorithm as per suggestion from dcherian.

return np.float32
# For all other types and circumstances, we just use float64.
# (safe because eg. complex numbers are not supported in NetCDF)
return np.float64


def _choose_float_dtype_decoding(dtype, scale_factor, add_offset):
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I chose to focus on decoding per the CF specification, so I split the function. Furthermore, decoding makes heavy use of np.find_common_type to select the correct datatype for the final product.

@@ -224,7 +224,7 @@ def _scale_offset_decoding(data, scale_factor, add_offset, dtype):
return data


def _choose_float_dtype(dtype, has_offset):
def _choose_float_dtype_encoding(dtype, has_offset):
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Encoding per the CF spec is fairly specific. The packed variable is supposed to be type byte/short/int, not float. Most of the tests encode with a scale_factor or add_offset that require the packed data to be type float. Rather than trying to solve all this, I have just split the encode and decode dtype function.

if "add_offset" in encoding:
data -= pop_to(encoding, attrs, "add_offset", name=name)
if "scale_factor" in encoding:
data /= pop_to(encoding, attrs, "scale_factor", name=name)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The data type may change when adding or scaling, hence changing from data /= ... to data = data / ....



@pytest.mark.parametrize("scale_factor", (10, [10]))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure that scale_factor or add_offset can be an array type per the CF spec, so I changed this test.

Copy link
Contributor

Choose a reason for hiding this comment

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

These kinds of things tend to happen though. Since we have tested for it, we should just keep it around.

@mankoff mankoff changed the title Make code match comments - use 64bit float Improved CF decoding Jul 29, 2022
@dcherian
Copy link
Contributor

dcherian commented Oct 3, 2022

Sorry for dropping this @mankoff How can we move forward here?

@mankoff
Copy link
Contributor Author

mankoff commented Oct 3, 2022

Hi @dcherian - I dropped this because I went down a rabbit hole that seemed very very deep.

Xarray has written 10s (100s?) of tests that touch this decoding function that make assumptions that I believe are incorrect after a careful reading of the CF spec. I believe the path forward will take some conversation before coding, so perhaps this should be moved to an issue rather than a pull request? A big decision is if the decode option strictly follows CF guidelines. If so, then a lot of tests need to be changed (for example, to follow the simple rule of [scale_factor and add_offset] must both be of type float or both be of type double).

Enforcing this would probably break xarray backward compatibility for writing files. I assume that that may be OK and there are processes to handle this (start with 'deprecation' warnings, then eventually throw errors?). There are also likely many NetCDF files that are not standard compliant and we need to decide how to read them.

Furthermore, the CF conventions are themselves not very clear, and possibly ambiguous. I started a conversation here: cf-convention/cf-conventions#374 on this, but that is also unresolved at the moment. The CF convention mentions int and float, but not how many bytes those are. What happens when a files is written & packed on one architecture and read & unpacked on another?

@mankoff
Copy link
Contributor Author

mankoff commented Oct 6, 2022

A bit more detail about the existing tests that don't match the CF spec. Per the spec, scale_factor and add_offset should be of the same type. That causes tests throughout https://github.com/pydata/xarray/blob/main/xarray/tests/test_coding.py and https://github.com/pydata/xarray/blob/main/xarray/tests/test_backends.py to fail, because:

@pytest.mark.parametrize("scale_factor", (10, [10]))
@pytest.mark.parametrize("add_offset", (0.1, [0.1]))

There is 1 test in test_coding, and 9 tests in test_backends that use mixed types. That's a tractable number I can fix.

In addition, the expected dtype returned by many of the tests does not match (my interpretation of) the expected dtype per the CF spec.

I am concerned that this is a significant change and I'm not sure what the process is for making this change. I would like to have some idea, even if not a guarantee, that it would be welcomed and accepted before doing all the work. I note that a recent other large PR to try to fix cf decoding has also stalled, and I'm not sure why (see #2751)

@dcherian
Copy link
Contributor

A big decision is if the decode option strictly follows CF guidelines.

I think our general position is to be flexible on what we can read because there are many slightly non-compliant files out there.

Xarray has written 10s (100s?) of tests that touch this decoding function that make assumptions that I believe are incorrect after a careful reading of the CF spec.

Some of these might just be for convenience and some might be checking that we are flexible in what we can read.

This following test should be preserved so we can read those files (#4631):

 @pytest.mark.parametrize("scale_factor", (10, [10])) 
 @pytest.mark.parametrize("add_offset", (0.1, [0.1])) 

Enforcing this would probably break xarray backward compatibility for writing files.

Do we not enforce that scale_factor and add_offset are of the same dtype on write? If so, we should consider that a bug and fix it.

I am concerned that this is a significant change and I'm not sure what the process is for making this change.

I think the way to move forward would be to figure out the smallest change that would fix (or even improve) #2304 and move on. We have a 30-minute bi-weekly meeting (#4001) that you're welcomed to attend and raise specific questions. The next one is Oct 26 at 9.30am Mountain Time

@dcherian
Copy link
Contributor

dcherian commented Apr 1, 2023

We should figure out how to express some of this understanding as tests (some xfailed). That way it's easy to check when something gets fixed, and prevent regressions.

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

Successfully merging this pull request may close these issues.

float32 instead of float64 when decoding int16 with scale_factor netcdf var using xarray
2 participants