-
Notifications
You must be signed in to change notification settings - Fork 1
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
Expose NeXus transformation chains in workflow steps #114
Merged
Merged
Changes from 15 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
81145a0
Do not auto-compute component position
SimonHeybrock 596cf26
Add example of extrating position via separate TransformationChain
SimonHeybrock 7c634ac
Remove unnecessary added code
SimonHeybrock 61d9e96
Move source/sample position assign to separate later step
SimonHeybrock 33a2b5f
Try to unify component handling code
SimonHeybrock bb3837c
Unify some more code
SimonHeybrock ab4ffd9
More unification
SimonHeybrock 4ab8a23
Drop selection also for monitor
SimonHeybrock 99bde33
Unify even more
SimonHeybrock c7181c5
Cleanup
SimonHeybrock 0f68b0c
Rename TypeVar ComponentType
SimonHeybrock 3518616
Rename monitor comp names
SimonHeybrock 54d39c4
Rename some more
SimonHeybrock 1aa06c9
Update tests
SimonHeybrock f71405a
docstring
SimonHeybrock 3b80bc2
Cleanup unused subclass
SimonHeybrock f58b51f
Document reason for function split
SimonHeybrock e161dc9
Comment on offset
SimonHeybrock 1618d6c
Bump to new minimum scippnexus
SimonHeybrock b202320
Merge branch 'main' into transformations
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
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 |
---|---|---|
|
@@ -22,8 +22,6 @@ | |
NeXusGroup = NewType('NeXusGroup', snx.Group) | ||
"""A ScippNexus group in an open file.""" | ||
|
||
NeXusDetectorName = NewType('NeXusDetectorName', str) | ||
"""Name of a detector (bank) in a NeXus file.""" | ||
NeXusEntryName = NewType('NeXusEntryName', str) | ||
"""Name of an entry in a NeXus file.""" | ||
NeXusSourceName = NewType('NeXusSourceName', str) | ||
|
@@ -33,8 +31,6 @@ | |
|
||
GravityVector = NewType('GravityVector', sc.Variable) | ||
|
||
Component = TypeVar('Component', bound=snx.NXobject) | ||
|
||
PreopenNeXusFile = NewType('PreopenNeXusFile', bool) | ||
"""Whether to preopen NeXus files before passing them to the rest of the workflow.""" | ||
|
||
|
@@ -95,9 +91,9 @@ class TransmissionRun(Generic[ScatteringRunType]): | |
"""Identifier for an arbitrary monitor""" | ||
Monitor5 = NewType('Monitor5', int) | ||
"""Identifier for an arbitrary monitor""" | ||
Incident = NewType('Incident', int) | ||
IncidentMonitor = NewType('IncidentMonitor', int) | ||
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. Added the monitor suffix here since these now show up as |
||
"""Incident monitor""" | ||
Transmission = NewType('Transmission', int) | ||
TransmissionMonitor = NewType('TransmissionMonitor', int) | ||
"""Transmission monitor""" | ||
MonitorType = TypeVar( | ||
'MonitorType', | ||
|
@@ -106,50 +102,56 @@ class TransmissionRun(Generic[ScatteringRunType]): | |
Monitor3, | ||
Monitor4, | ||
Monitor5, | ||
Incident, | ||
Transmission, | ||
IncidentMonitor, | ||
TransmissionMonitor, | ||
) | ||
"""TypeVar used for specifying the monitor type such as Incident or Transmission""" | ||
|
||
|
||
class NeXusMonitorName(sciline.Scope[MonitorType, str], str): | ||
"""Name of a monitor in a NeXus file.""" | ||
|
||
|
||
class NeXusDetector(sciline.Scope[RunType, sc.DataGroup], sc.DataGroup): | ||
"""Full raw data from a NeXus detector.""" | ||
|
||
|
||
class NeXusMonitor( | ||
sciline.ScopeTwoParams[RunType, MonitorType, sc.DataGroup], sc.DataGroup | ||
): | ||
"""Full raw data from a NeXus monitor.""" | ||
Component = TypeVar( | ||
'Component', | ||
snx.NXdetector, | ||
snx.NXsample, | ||
snx.NXsource, | ||
Monitor1, | ||
Monitor2, | ||
Monitor3, | ||
Monitor4, | ||
Monitor5, | ||
IncidentMonitor, | ||
TransmissionMonitor, | ||
) | ||
UniqueComponent = TypeVar('UniqueComponent', snx.NXsample, snx.NXsource) | ||
"""Components that can be identified by their type as there will only be one.""" | ||
|
||
|
||
class NeXusSample(sciline.Scope[RunType, sc.DataGroup], sc.DataGroup): | ||
"""Raw data from a NeXus sample.""" | ||
class NeXusName(sciline.Scope[Component, str], str): | ||
"""Name of a component in a NeXus file.""" | ||
|
||
|
||
class NeXusSource(sciline.Scope[RunType, sc.DataGroup], sc.DataGroup): | ||
"""Raw data from a NeXus source.""" | ||
class NeXusClass(sciline.Scope[Component, str], str): | ||
"""NX_class of a component in a NeXus file.""" | ||
|
||
|
||
class NeXusDetectorData(sciline.Scope[RunType, sc.DataArray], sc.DataArray): | ||
"""Data array loaded from an NXevent_data or NXdata group within an NXdetector.""" | ||
NeXusDetectorName = NeXusName[snx.NXdetector] | ||
"""Name of a detector (bank) in a NeXus file.""" | ||
|
||
|
||
class NeXusMonitorData( | ||
sciline.ScopeTwoParams[RunType, MonitorType, sc.DataArray], sc.DataArray | ||
class NeXusComponent( | ||
sciline.ScopeTwoParams[Component, RunType, sc.DataGroup], sc.DataGroup | ||
): | ||
"""Data array loaded from an NXevent_data or NXdata group within an NXmonitor.""" | ||
"""Raw data from a NeXus component.""" | ||
|
||
|
||
class SourcePosition(sciline.Scope[RunType, sc.Variable], sc.Variable): | ||
"""Position of the neutron source.""" | ||
class NeXusData(sciline.ScopeTwoParams[Component, RunType, sc.DataArray], sc.DataArray): | ||
""" | ||
Data array loaded from an NXevent_data or NXdata group. | ||
|
||
This must be contained in an NXmonitor or NXdetector group. | ||
""" | ||
|
||
|
||
class SamplePosition(sciline.Scope[RunType, sc.Variable], sc.Variable): | ||
"""Position of the sample.""" | ||
class Position(sciline.ScopeTwoParams[Component, RunType, sc.Variable], sc.Variable): | ||
"""Position of a component such as source, sample, monitor, or detector.""" | ||
|
||
|
||
class DetectorPositionOffset(sciline.Scope[RunType, sc.Variable], sc.Variable): | ||
|
@@ -166,6 +168,10 @@ class CalibratedDetector(sciline.Scope[RunType, sc.DataArray], sc.DataArray): | |
"""Calibrated data from a detector.""" | ||
|
||
|
||
class CalibratedBeamline(sciline.Scope[RunType, sc.DataArray], sc.DataArray): | ||
"""Calibrated beamline with detector and other components.""" | ||
|
||
|
||
class CalibratedMonitor( | ||
sciline.ScopeTwoParams[RunType, MonitorType, sc.DataArray], sc.DataArray | ||
): | ||
|
@@ -217,23 +223,45 @@ class NeXusComponentLocationSpec(NeXusLocationSpec, Generic[Component, RunType]) | |
|
||
|
||
@dataclass | ||
class NeXusMonitorLocationSpec( | ||
NeXusComponentLocationSpec[snx.NXmonitor, RunType], Generic[RunType, MonitorType] | ||
): | ||
""" | ||
NeXus filename and optional parameters to identify (parts of) a monitor to load. | ||
""" | ||
class NeXusDataLocationSpec(NeXusLocationSpec, Generic[Component, RunType]): | ||
"""NeXus filename and parameters to identify (parts of) detector data to load.""" | ||
|
||
|
||
T = TypeVar('T', bound='NeXusTransformationChain') | ||
|
||
|
||
@dataclass | ||
class NeXusDetectorDataLocationSpec( | ||
NeXusComponentLocationSpec[snx.NXevent_data, RunType], Generic[RunType] | ||
): | ||
"""NeXus filename and parameters to identify (parts of) detector data to load.""" | ||
class NeXusTransformationChain(snx.TransformationChain, Generic[Component, RunType]): | ||
@classmethod | ||
def from_base(cls: type[T], base: snx.TransformationChain) -> T: | ||
return cls( | ||
parent=base.parent, | ||
value=base.value, | ||
transformations=base.transformations, | ||
) | ||
|
||
def compute_position(self) -> sc.Variable | sc.DataArray: | ||
return self.compute() * sc.vector([0, 0, 0], unit='m') | ||
|
||
|
||
@dataclass | ||
class NeXusMonitorDataLocationSpec( | ||
NeXusComponentLocationSpec[snx.NXevent_data, RunType], Generic[RunType, MonitorType] | ||
): | ||
"""NeXus filename and parameters to identify (parts of) monitor data to load.""" | ||
class NeXusTransformation(Generic[Component, RunType]): | ||
value: sc.Variable | ||
|
||
@staticmethod | ||
def from_chain( | ||
chain: NeXusTransformationChain[Component, RunType], | ||
) -> 'NeXusTransformation[Component, RunType]': | ||
""" | ||
Convert a transformation chain to a single transformation. | ||
|
||
As transformation chains may be time-dependent, this method will need to select | ||
a specific time point to convert to a single transformation. This may include | ||
averaging as well as threshold checks. This is not implemented yet and we | ||
therefore currently raise an error if the transformation chain does not compute | ||
to a scalar. | ||
""" | ||
transform = chain.compute() | ||
if transform.ndim == 0: | ||
return NeXusTransformation(value=transform) | ||
raise ValueError(f"Expected scalar transformation, got {transform}") |
Oops, something went wrong.
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.
This is the heart of the change in this PR: We are no longer computing the position directly here. This is moved into an extra step.