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

Make v2 default #158

Merged
merged 7 commits into from
Aug 14, 2023
Merged
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
9 changes: 9 additions & 0 deletions docs/about/release-notes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ Release Notes
Deprecations
~~~~~~~~~~~~

v23.08.0
--------

Breaking changes
~~~~~~~~~~~~~~~~

* ``scippnexus.v2`` is now the default when running ``import scippnexus``.
Legacy interface has moved to ``scippnexus.v1``, but this will be removed in a future release `#158 <https://github.com/scipp/scippnexus/pull/158>`_.

v23.07.0
--------

Expand Down
7 changes: 4 additions & 3 deletions docs/getting-started/quick-start-guide.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@
"outputs": [],
"source": [
"# To use the legacy interface, use:\n",
"# import scippnexus as snx\n",
"import scippnexus.v2 as snx\n",
"# import scippnexus.v1 as snx\n",
"import scippnexus as snx\n",
"\n",
"with snx.File(filename) as f:\n",
" print(list(f.keys()))"
Expand Down Expand Up @@ -354,7 +354,8 @@
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3"
"pygments_lexer": "ipython3",
"version": "3.8.16"
}
},
"nbformat": 4,
Expand Down
2 changes: 2 additions & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ This is especially powerful since a number of concepts of Scipp map well to conc
News
----

- [August 2023] scippnexus-23.08.0 has been released.
The "v2" API is now the default.
- [April 2023] scippnexus-23.04.0 has been released.
This adds ``scippnexus.v2``, which provides the future API of ``scippnexus``.
The new API avoids performance bottlenecks when working with small files that contain many groups and datasets.
Expand Down
4 changes: 2 additions & 2 deletions docs/user-guide/application-definitions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
"source": [
"import h5py\n",
"import scipp as sc\n",
"import scippnexus.v2 as snx\n",
"import scippnexus as snx\n",
"\n",
"\n",
"class MyData:\n",
Expand Down Expand Up @@ -200,7 +200,7 @@
"metadata": {},
"outputs": [],
"source": [
"import scippnexus.v2 as snx\n",
"import scippnexus as snx\n",
"\n",
"\n",
"def skip(name, obj):\n",
Expand Down
2 changes: 1 addition & 1 deletion docs/user-guide/classes.rst
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.. currentmodule:: scippnexus.v2
.. currentmodule:: scippnexus

Classes
=======
Expand Down
2 changes: 1 addition & 1 deletion docs/user-guide/functions.rst
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.. currentmodule:: scippnexus.v2
.. currentmodule:: scippnexus

Functions
=========
Expand Down
5 changes: 3 additions & 2 deletions docs/user-guide/nexus-classes.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
"source": [
"from scippnexus import data\n",
"filename = data.get_path('PG3_4844_event.nxs')\n",
"import scippnexus.v2 as snx\n",
"import scippnexus as snx\n",
"f = snx.File(filename)"
]
},
Expand Down Expand Up @@ -330,7 +330,8 @@
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3"
"pygments_lexer": "ipython3",
"version": "3.8.16"
}
},
"nbformat": 4,
Expand Down
15 changes: 10 additions & 5 deletions src/scippnexus/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,15 @@
__version__ = "0.0.0"

from . import typing
from .definition import ApplicationDefinition, make_definition
from .base import (
Group,
NexusStructureError,
NXobject,
base_definitions,
create_class,
create_field,
)
from .field import Attrs, Field
from .file import File
from .nexus_classes import *
from .nxdata import NXdataStrategy
from .nxdetector import NXdetectorStrategy
from .nxlog import NXlogStrategy
from .nxobject import Attrs, Field, NexusStructureError, NXobject, NXobjectStrategy
from .nxdata import group_events_by_detector_number
9 changes: 9 additions & 0 deletions src/scippnexus/application_definitions/nxcansas/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# SPDX-License-Identifier: BSD-3-Clause
# Copyright (c) 2023 Scipp contributors (https://github.com/scipp)
# @author Simon Heybrock

# flake8: noqa

from .nxcansas import SASdata, SASentry, definitions

__all__ = ['definitions', 'SASentry', 'SASdata']
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

import scipp as sc

from ....typing import H5Group
from ...base import Group, NXobject, base_definitions, create_field
from ...field import Field
from ...nxdata import NXdata
from ...typing import H5Group


class SASentry:
Expand Down
2 changes: 1 addition & 1 deletion src/scippnexus/v2/attrs.py → src/scippnexus/attrs.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from collections.abc import Mapping
from typing import Any, Iterator

from .._hdf5_nexus import _cset_to_encoding, _ensure_str
from ._hdf5_nexus import _cset_to_encoding, _ensure_str


class Attrs(Mapping):
Expand Down
4 changes: 2 additions & 2 deletions src/scippnexus/v2/base.py → src/scippnexus/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
import numpy as np
import scipp as sc

from .._common import to_child_select
from ..typing import H5Dataset, H5Group, ScippIndex
from ._common import to_child_select
from .attrs import Attrs
from .field import Field
from .typing import H5Dataset, H5Group, ScippIndex


def asvariable(obj: Union[Any, sc.Variable]) -> sc.Variable:
Expand Down
File renamed without changes.
31 changes: 20 additions & 11 deletions src/scippnexus/file.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,33 @@
# SPDX-License-Identifier: BSD-3-Clause
# Copyright (c) 2023 Scipp contributors (https://github.com/scipp)
# @author Simon Heybrock
import warnings
from contextlib import AbstractContextManager
from typing import Mapping

import h5py
from scipp import VisibleDeprecationWarning

from .nexus_classes import NXroot
from .base import Group, base_definitions

_default_definitions = object()

class File(AbstractContextManager, NXroot):
def __init__(self, *args, definition=None, **kwargs):
warnings.warn(
"This API is deprecated and will be removed and replaced in release 23.06. "
"Switch to 'import scippnexus.v2 as snx' to prepare for this.",
VisibleDeprecationWarning,
)

class File(AbstractContextManager, Group):
def __init__(self, *args, definitions: Mapping = _default_definitions, **kwargs):
"""Context manager for NeXus files, similar to h5py.File.

Arguments other than documented are as in :py:class:`h5py.File`.

Parameters
----------
definitions:
Mapping of NX_class names to application-specific definitions.
The default is to use the base definitions as defined in the
NeXus standard.
"""
if definitions is _default_definitions:
definitions = base_definitions()
self._file = h5py.File(*args, **kwargs)
NXroot.__init__(self, self._file, definition=definition)
super().__init__(self._file, definitions=definitions)

def __enter__(self):
self._file.__enter__()
Expand Down
22 changes: 14 additions & 8 deletions src/scippnexus/nexus_classes.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
# SPDX-License-Identifier: BSD-3-Clause
# Copyright (c) 2023 Scipp contributors (https://github.com/scipp)
# @author Simon Heybrock
from .base import NXobject, NXroot # noqa F401
from .nxcylindrical_geometry import NXcylindrical_geometry # noqa F401
from .nxdata import NXdata # noqa F401
from .nxdetector import NXdetector # noqa F401
from .nxdisk_chopper import NXdisk_chopper # noqa F401
from .nxdata import NXdata, NXdetector, NXlog, NXmonitor # noqa F401
from .nxevent_data import NXevent_data # noqa F401
from .nxfermi_chopper import NXfermi_chopper # noqa F401
from .nxlog import NXlog # noqa F401
from .nxmonitor import NXmonitor # noqa F401
from .nxobject import NXobject, NXroot # noqa F401
from .nxoff_geometry import NXoff_geometry # noqa F401
from .nxsample import NXsample # noqa F401
from .nxsource import NXsource # noqa F401
from .nxtransformations import NXtransformations # noqa F401


Expand Down Expand Up @@ -72,10 +66,18 @@ class NXdetector_module(NXobject):
"""NXdetector_module"""


class NXdisk_chopper(NXobject):
"""NXdisk_chopper"""


class NXenvironment(NXobject):
"""NXenvironment"""


class NXfermi_chopper(NXobject):
"""NXfermi_chopper"""


class NXfilter(NXobject):
"""NXfilter"""

Expand Down Expand Up @@ -164,6 +166,10 @@ class NXshape(NXobject):
"""NXshape"""


class NXsource(NXobject):
"""NXsource"""


class NXslit(NXobject):
"""NXslit"""

Expand Down
28 changes: 19 additions & 9 deletions src/scippnexus/nxcylindrical_geometry.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# SPDX-License-Identifier: BSD-3-Clause
# Copyright (c) 2023 Scipp contributors (https://github.com/scipp)
# @author Simon Heybrock
from typing import Optional, Tuple, Union
from typing import Any, Dict, Optional, Union

import scipp as sc

from .nxobject import NexusStructureError, NXobject
from .base import Group, NexusStructureError, NXobject, base_definitions_dict
from .field import Field


def _parse(
Expand Down Expand Up @@ -55,15 +56,24 @@ class NXcylindrical_geometry(NXobject):
'cylinders': ('cylinder', 'vertex_index'),
}

def _get_field_dims(self, name: str) -> Union[None, Tuple[str]]:
return self._dims.get(name)

def _get_field_dtype(self, name: str) -> Union[None, sc.DType]:
if name == 'vertices':
return sc.DType.vector3
return None
def __init__(self, attrs: Dict[str, Any], children: Dict[str, Union[Field, Group]]):
super().__init__(attrs=attrs, children=children)
for name, field in children.items():
if isinstance(field, Field):
field.sizes = dict(zip(self._dims.get(name), field.dataset.shape))
if name == 'vertices':
field.dtype = sc.DType.vector3

def load_as_array(
self, detector_number: Optional[sc.Variable] = None
) -> sc.Variable:
return _parse(**self[()], parent_detector_number=detector_number)

@staticmethod
def assemble_as_child(
children: sc.DataGroup, detector_number: Optional[sc.Variable] = None
) -> sc.Variable:
return _parse(**children, parent_detector_number=detector_number)


base_definitions_dict['NXcylindrical_geometry'] = NXcylindrical_geometry
Loading