Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions src/scippnexus/_load.py
Copy link
Member

Choose a reason for hiding this comment

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

There are some issues with this approach:

  • It affects all warnings, even those that may not be related to file structure but, e.g., arise from dependencies.
  • It works by changing a global variable. So if this code is used in a multi-threaded context, e.g., in Sciline with Dask, the filter also affects warnings that don't arise from ScippNexus.

Those issues can be somewhat mitigated by filtering on the module where the warning is raised. But you can still get race conditions with multi-threading.

If you want to go with this approach, please add a clear docstring explaining the caveats.

Copy link
Member

Choose a reason for hiding this comment

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

Would it make sense to consider using logging instead of warnings?

Copy link
Member

Choose a reason for hiding this comment

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

Not if you want to treat warnings as errors in some contexts.

Copy link
Member Author

Choose a reason for hiding this comment

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

There are some issues with this approach

Yes, you make some good points. Do you have a suggestion for an alternative? If not, I guess filtering on the module would already be a big improvement.

Copy link
Member

Choose a reason for hiding this comment

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

Have we considered putting the filtering code into userland, i.e., not change ScippNexus?

Copy link
Member Author

Choose a reason for hiding this comment

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

It's an option, I just thought having a flag would be the most user friendly mechanism.

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# Copyright (c) 2023 Scipp contributors (https://github.com/scipp)
import contextlib
import io
import warnings
from os import PathLike

import h5py as h5
Expand All @@ -23,6 +24,7 @@ def load(
root: str | None = None,
select: ScippIndex = (),
definitions: Definitions | DefaultDefinitionsType = DefaultDefinitions,
quiet: bool = False,
) -> sc.DataGroup | sc.DataArray | sc.Dataset:
"""Load a NeXus file.

Expand Down Expand Up @@ -83,16 +85,20 @@ def load(
definitions:
NeXus `application definitions <../../user-guide/application-definitions.rst>`_.
Defaults to the ScippNexus base definitions.
quiet:
If ``True``, suppresses warnings while loading.

Returns
-------
:
The loaded data.
"""
with _open(filename, definitions=definitions) as group:
if root is not None:
group = group[root]
return group[select]
with warnings.catch_warnings():
warnings.simplefilter("ignore" if quiet else "default")
Copy link
Member

Choose a reason for hiding this comment

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

I think this resets user-defined filters. You need to not call simplefilter if not quiet. But I may be wrong. Did you check?

with _open(filename, definitions=definitions) as group:
if root is not None:
group = group[root]
return group[select]


def _open(
Expand Down
7 changes: 7 additions & 0 deletions tests/load_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,3 +210,10 @@ def test_load_from_snx_group_rejects_new_definitions(
with pytest.raises(TypeError, match='Cannot override application definitions'):
with snx.File(nexus_buffer, 'r') as f:
snx.load(f, definitions=definitions)


def test_load_suppress_warnings():
from scippnexus import data

filename = data.get_path('PG3_4844_event.nxs')
snx.load(filename, quiet=True)
Loading