-
Notifications
You must be signed in to change notification settings - Fork 4
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
Support NXlog with "sublogs" such as connection_status and alarm #138
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e8d8276
Move NXlog tests to file
SimonHeybrock 5343f59
Support "sublogs" in NXlog
SimonHeybrock df17379
Test that positional indexing is disabled
SimonHeybrock 79bdea5
Small fixes
SimonHeybrock 0141b82
Fix bad squeeze of length-1 sublog
SimonHeybrock dc29cbe
Do not modify mappings that may break fallback
SimonHeybrock File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,7 +10,7 @@ | |
import numpy as np | ||
import scipp as sc | ||
|
||
from .._common import convert_time_to_datetime64, to_child_select | ||
from .._common import _to_canonical_select, convert_time_to_datetime64, to_child_select | ||
from ..typing import H5Dataset, ScippIndex | ||
from .base import ( | ||
Group, | ||
|
@@ -342,20 +342,82 @@ def _squeeze_trailing(dims: Tuple[str, ...], shape: Tuple[int, ...]) -> Tuple[in | |
|
||
|
||
class NXlog(NXdata): | ||
""" | ||
NXlog, a time-series that can be loaded as a DataArray. | ||
|
||
In some cases the NXlog may contain additional time series, such as a connection | ||
status or alarm. These cannot be handled in a standard way, since the result cannot | ||
be represented as a single DataArray. Furthermore, they prevent positional | ||
time-indexing, since the time coord of each time-series is different. We can | ||
support label-based indexing for this in the future. If additional time-series | ||
are contained within the NXlog then loading will return a DataGroup of the | ||
individual time-series (DataArrays). | ||
""" | ||
|
||
def __init__(self, attrs: Dict[str, Any], children: Dict[str, Union[Field, Group]]): | ||
self._sublogs = [] | ||
self._sublog_children = {} | ||
for name in children: | ||
if name.endswith('_time'): | ||
self._sublogs.append(name[:-5]) | ||
# Extract all fields that belong to sublogs, since they will interfere with the | ||
# setup logic in the base class (NXdata). | ||
for name in self._sublogs: | ||
for k in list(children): | ||
if k.startswith(name): | ||
field = children.pop(k) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This modifies the argument |
||
self._init_field(field) | ||
field.sizes = { | ||
'time' if i == 0 else f'dim_{i}': size | ||
for i, size in enumerate(field.dataset.shape) | ||
} | ||
self._sublog_children[k] = field | ||
|
||
super().__init__(attrs=attrs, | ||
children=children, | ||
fallback_dims=('time', ), | ||
fallback_signal_name='value') | ||
|
||
def read_children(self, sel: ScippIndex) -> sc.DataGroup: | ||
# Sublogs have distinct time axes (with a different length). Must disable | ||
# positional indexing. | ||
if self._sublogs and ('time' in _to_canonical_select(list(self.sizes), sel)): | ||
raise sc.DimensionError( | ||
"Cannot positionally select time since there are multiple " | ||
"time fields. Label-based selection is not supported yet.") | ||
dg = super().read_children(sel) | ||
for name, field in self._sublog_children.items(): | ||
dg[name] = field[sel] | ||
return dg | ||
|
||
def _time_to_datetime(self, mapping): | ||
if (time := mapping.get('time')) is not None: | ||
if time.dtype != sc.DType.datetime64 and _is_time(time): | ||
mapping['time'] = convert_time_to_datetime64( | ||
time, start=sc.epoch(unit=time.unit)) | ||
|
||
def _assemble_sublog(self, | ||
dg: sc.DataGroup, | ||
name: str, | ||
value_name: Optional[str] = None) -> sc.DataArray: | ||
value_name = name if value_name is None else f'{name}_{value_name}' | ||
da = sc.DataArray(dg.pop(value_name), coords={'time': dg.pop(f'{name}_time')}) | ||
for k in list(dg): | ||
if k.startswith(name): | ||
da.coords[k[len(name) + 1:]] = dg.pop(k) | ||
self._time_to_datetime(da.coords) | ||
return da | ||
|
||
def assemble(self, | ||
dg: sc.DataGroup) -> Union[sc.DataGroup, sc.DataArray, sc.Dataset]: | ||
if (time := dg.get('time')) is not None: | ||
if time.dtype != sc.DType.datetime64 and _is_time(time): | ||
dg['time'] = convert_time_to_datetime64(time, | ||
start=sc.epoch(unit=time.unit)) | ||
return super().assemble(dg) | ||
sublogs = sc.DataGroup() | ||
for name in self._sublogs: | ||
# Somewhat arbitrary definition of which fields is the "value" | ||
value_name = 'severity' if name == 'alarm' else None | ||
sublogs[name] = self._assemble_sublog(dg, name, value_name=value_name) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
self._time_to_datetime(dg) | ||
out = super().assemble(dg) | ||
return out if not sublogs else sc.DataGroup(value=out, **sublogs) | ||
|
||
|
||
def _find_embedded_nxevent_data( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we had some form of flexible structured dtypes, could this data be merged into a single data array by binning? (e.g. with the main log's time coord)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think so. What do you have in mind?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
da['time', i]
contains the value of the main log in this bin as well as all values of sublogs that fall into this bin.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typically the main log has many more values than the sublogs (often by orders of magnitude), so I don't see how that would be useful/feasible?