Skip to content

Provide a properly public find_bin_edge_dims - #2469

Merged
jl-wynen merged 34 commits into
mainfrom
public-find-bin-edges
Mar 2, 2022
Merged

jl-wynen merged 34 commits into
mainfrom
public-find-bin-edges

Conversation

@jl-wynen

Copy link
Copy Markdown
Member

Fixes #2468

There are a bunch of fixes to docstrings because those functions are now documented as part of the utils module and sphinx started complaining.

@SimonHeybrock

Copy link
Copy Markdown
Member

Is this referencing the correct issue?

Comment thread docs/reference/modules.rst Outdated

configuration
logging
utils

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Careful here: We have always considering functions in utils implementation details that can change at any time. If we now make all the functionality there "public" (documented) we would lose that ability to do so.

Have you considered other places for the added functionality? Or defining manually which functions are documented (and thus supported) publicly?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Should we rename utils to _utils to indicate that it is non public?

Or defining manually which functions are documented (and thus supported) publicly?

I tried that because I didn't like that each util module is documented separately. But autosummary is pretty inflexible and doing this is kind of awkward.

Have you considered other places for the added functionality?

We could add bin_edges at the top level. We were considering a bin-edge toolkit at some point anyway. Although, we put that on ice for now, right?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think midpoints is top-level?

If we follow my thoughts below (making this a method of the coords class) then the problem vanishes anyway?

Comment on lines +7 to +11
"""
Miscellaneous utilities.

Note that all functions from submodules are also available in ``scipp.utils`` directly.
"""

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

See above!

Comment thread src/scipp/utils/bin_edges.py Outdated
return bin_edges


def is_bin_edges(ref: VariableLike, /, *, coord: Variable) -> bool:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

  • Missing a dimension label? What if coord is 2d? The return value will be unclear.
  • Would this work better as a method of Coord, i.e., da.coords.is_edges(name, dim)?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Missing a dimension label? What if coord is 2d? The return value will be unclear.

It checks if any dim is a bin-edge. I.e. the same that the HTML output does. I don't see why we would do anything different here.

Would this work better as a method of Coord, i.e., da.coords.is_edges(name, dim)?

Then it should also be a method of attrs and meta. And it would limit it to cases where the coord is already stored in the DataArray. What if a user wants to check before adding it because they maybe don't want to add bin-edges?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It checks if any dim is a bin-edge. I.e. the same that the HTML output does. I don't see why we would do anything different here.

Because this is really important for writing code that is not broken? If we have 2d data and coord we need to know which dim has edges. For the HTML output it is maybe not critical, or rather it should be fixed to indicate which dims the edges refer to?

Then it should also be a method of attrs and meta.

Yes.

And it would limit it to cases where the coord is already stored in the DataArray. What if a user wants to check before adding it because they maybe don't want to add bin-edges?

Seems like a very special case? Maybe we should not compromise the API for this at this point?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Finding out which dim has edges is covered by find_bin_edge_dims. If we add a dim arg to is_bin_edges, it should be optional for 1d coords, I think. Otherwise, one would pretty much always write is_bin_edges(da, da.coords['x'], da.coords['x'].dim) (or the equivalent method).

Maybe we should not compromise the API for this at this point?

I don't see this as a compromise. But I don't know of any concrete use case of a free function. So I am fine with changing it to a method.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If we add a dim arg to is_bin_edges, it should be optional for 1d coords, I think.

Yes that makes sense. I believe we have done so in similar cases.

@jl-wynen

Copy link
Copy Markdown
Member Author

coords etc are currently undocumented in the reference. So it is impossible to find is_edges except for by digging through dir(da.coords). Do we want to document those classes. If so, how? They only exist as bindings to C++.

@SimonHeybrock

Copy link
Copy Markdown
Member

coords etc are currently undocumented in the reference. So it is impossible to find is_edges except for by digging through dir(da.coords). Do we want to document those classes. If so, how? They only exist as bindings to C++.

We should probably document them, yes. Can they not be listed in https://scipp.github.io/reference/classes.html?

Comment on lines +211 to +220
for idx, dim in enumerate(var.dims):
length = var.shape[idx]
if not ds.dims:
# Have a scalar slice.
# Cannot match dims, just assume length 2 attributes are bin-edge
if length == 2:
bin_edges.append(dim)
elif dim in ds.dims and ds.shape[ds.dims.index(dim)] + 1 == length:
bin_edges.append(dim)
return bin_edges

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can this now use the newly bound C++ method?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This would not be any less complex. It would have to check both meta and masks, except that Dataset does not have masks. So there needs to be quite a lot of logic.

Comment on lines +64 to +65
same_data = all(isclose(x.data, y.data, rtol=rtol, atol=atol,
equal_nan=equal_nan)).value if include_data else True

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Unrelated, but am I missing something here or is this just allclose?

@jl-wynen
jl-wynen merged commit 3d23ae0 into main Mar 2, 2022
@jl-wynen
jl-wynen deleted the public-find-bin-edges branch March 2, 2022 14:33
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.

Add to_quantity and from_quantity

2 participants