-
Notifications
You must be signed in to change notification settings - Fork 2
feat: pulse shaping #211
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
Open
jokasimr
wants to merge
14
commits into
main
Choose a base branch
from
beer-pulse-shaping
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
feat: pulse shaping #211
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
187f299
feat: pulse shaping
jokasimr ed8ba37
fix
jokasimr 5aed46f
fix: load chopper settings in all modes - truncate time to limits of …
jokasimr 3cd12af
data: add updated simulation files to pooch, and add provider for par…
jokasimr 402382e
docs
jokasimr 7e7329d
fix: make functions public
jokasimr 2ed9687
docs
jokasimr a405435
refactor: extract mode param mapping
jokasimr ae279d4
Update src/ess/beer/conversions.py
jokasimr 96467a7
fix: unnecessary mod
jokasimr a799d97
Merge branch 'beer-pulse-shaping' of github.com:scipp/essdiffraction …
jokasimr 36544a5
docs
jokasimr d94cc37
refactor: change name to signal it makes a graph
jokasimr f68032a
docs: change quotes to double
jokasimr 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 hidden or 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 hidden or 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 hidden or 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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -18,7 +18,7 @@ def compute_tof_in_each_cluster( | |||||
| da: StreakClusteredData[RunType], | ||||||
| mod_period: ModulationPeriod, | ||||||
| ) -> TofDetector[RunType]: | ||||||
| '''Fits a line through each cluster, the intercept of the line is t0. | ||||||
| """Fits a line through each cluster, the intercept of the line is t0. | ||||||
| The line is fitted using linear regression with an outlier removal procedure. | ||||||
|
|
||||||
| The algorithm is: | ||||||
|
|
@@ -30,15 +30,18 @@ def compute_tof_in_each_cluster( | |||||
| of the points in the cluster, and probably should belong to another cluster or | ||||||
| are part of the background. | ||||||
| 3. Go back to 1) and iterate until convergence. A few iterations should be enough. | ||||||
| ''' | ||||||
| """ | ||||||
| if isinstance(da, sc.DataGroup): | ||||||
| return sc.DataGroup( | ||||||
| {k: compute_tof_in_each_cluster(v, mod_period) for k, v in da.items()} | ||||||
| ) | ||||||
|
|
||||||
| max_distance_from_streak_line = mod_period / 3 | ||||||
| sin_theta_L = sc.sin(da.bins.coords['two_theta'] / 2) * da.bins.coords['Ltotal'] | ||||||
| t = da.bins.coords['event_time_offset'] | ||||||
| t = time_of_arrival( | ||||||
| da.bins.coords['event_time_offset'], | ||||||
| da.coords['tc'].to(unit=da.bins.coords['event_time_offset'].unit), | ||||||
| ) | ||||||
| for _ in range(15): | ||||||
| s, t0 = _linear_regression_by_bin(sin_theta_L, t, da.data) | ||||||
|
|
||||||
|
|
@@ -57,10 +60,10 @@ def compute_tof_in_each_cluster( | |||||
| def _linear_regression_by_bin( | ||||||
| x: sc.Variable, y: sc.Variable, w: sc.Variable | ||||||
| ) -> tuple[sc.Variable, sc.Variable]: | ||||||
| '''Performs a weighted linear regression of the points | ||||||
| """Performs a weighted linear regression of the points | ||||||
| in the binned variables ``x`` and ``y`` weighted by ``w``. | ||||||
| Returns ``b1`` and ``b0`` such that ``y = b1 * x + b0``. | ||||||
| ''' | ||||||
| """ | ||||||
| w = sc.values(w) | ||||||
| tot_w = w.bins.sum() | ||||||
|
|
||||||
|
|
@@ -77,7 +80,7 @@ def _linear_regression_by_bin( | |||||
|
|
||||||
|
|
||||||
| def _compute_d( | ||||||
| event_time_offset: sc.Variable, | ||||||
| time_of_arrival: sc.Variable, | ||||||
| theta: sc.Variable, | ||||||
| dhkl_list: sc.Variable, | ||||||
| pulse_length: sc.Variable, | ||||||
|
|
@@ -87,15 +90,15 @@ def _compute_d( | |||||
| given a list of known peaks.""" | ||||||
| # Source: https://www2.mcstas.org/download/components/3.4/contrib/NPI_tof_dhkl_detector.comp | ||||||
| sinth = sc.sin(theta) | ||||||
| t = event_time_offset | ||||||
| t = time_of_arrival | ||||||
|
|
||||||
| d = sc.empty(dims=sinth.dims, shape=sinth.shape, unit=dhkl_list[0].unit) | ||||||
| d[:] = sc.scalar(float('nan'), unit=dhkl_list[0].unit) | ||||||
| dtfound = sc.empty(dims=sinth.dims, shape=sinth.shape, dtype='float64', unit=t.unit) | ||||||
| dtfound[:] = sc.scalar(float('nan'), unit=t.unit) | ||||||
|
|
||||||
| const = (2 * sinth * L0 / (scipp.constants.h / scipp.constants.m_n)).to( | ||||||
| unit=f'{event_time_offset.unit}/angstrom' | ||||||
| unit=f'{time_of_arrival.unit}/angstrom' | ||||||
| ) | ||||||
|
|
||||||
| for dhkl in dhkl_list: | ||||||
|
|
@@ -112,36 +115,49 @@ def _compute_d( | |||||
| return d | ||||||
|
|
||||||
|
|
||||||
| def _tof_from_dhkl( | ||||||
| def time_of_arrival( | ||||||
| event_time_offset: sc.Variable, | ||||||
| tc: sc.Variable, | ||||||
| ): | ||||||
| """Does frame unwrapping for pulse shaping chopper modes. | ||||||
|
|
||||||
| Events before the "cutoff time" `tc` are assumed to come from the previous pulse.""" | ||||||
| _eto = event_time_offset | ||||||
|
Member
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.
Suggested change
It's unusual to use a protected local name like this.
Contributor
Author
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. I agree, but spellcheck complained about |
||||||
| T = sc.scalar(1 / 14, unit='s').to(unit=_eto.unit) | ||||||
| tc = tc.to(unit=_eto.unit) | ||||||
| return sc.where(_eto >= tc, _eto, _eto + T) | ||||||
|
|
||||||
|
|
||||||
| def _tof_from_dhkl( | ||||||
| time_of_arrival: sc.Variable, | ||||||
| theta: sc.Variable, | ||||||
| coarse_dhkl: sc.Variable, | ||||||
| Ltotal: sc.Variable, | ||||||
| mod_period: sc.Variable, | ||||||
| time0: sc.Variable, | ||||||
| ) -> sc.Variable: | ||||||
| '''Computes tof for BEER given the dhkl peak that the event belongs to''' | ||||||
| """Computes tof for BEER given the dhkl peak that the event belongs to""" | ||||||
| # Source: https://www2.mcstas.org/download/components/3.4/contrib/NPI_tof_dhkl_detector.comp | ||||||
| # tref = 2 * d_hkl * sin(theta) / hm * Ltotal | ||||||
| # tc = event_time_zero - time0 - tref | ||||||
| # tc = time_of_arrival - time0 - tref | ||||||
| # dt = floor(tc / mod_period + 0.5) * mod_period + time0 | ||||||
| # tof = event_time_offset - dt | ||||||
| # tof = time_of_arrival - dt | ||||||
| c = (-2 * 1.0 / (scipp.constants.h / scipp.constants.m_n)).to( | ||||||
| unit=f'{event_time_offset.unit}/m/angstrom' | ||||||
| unit=f'{time_of_arrival.unit}/m/angstrom' | ||||||
| ) | ||||||
| out = sc.sin(theta) | ||||||
| out *= c | ||||||
| out *= coarse_dhkl | ||||||
| out *= Ltotal | ||||||
| out += event_time_offset | ||||||
| out += time_of_arrival | ||||||
| out -= time0 | ||||||
| out /= mod_period | ||||||
| out += 0.5 | ||||||
| sc.floor(out, out=out) | ||||||
| out *= mod_period | ||||||
| out += time0 | ||||||
| out *= -1 | ||||||
| out += event_time_offset | ||||||
| out += time_of_arrival | ||||||
| return out | ||||||
|
|
||||||
|
|
||||||
|
|
@@ -151,20 +167,23 @@ def tof_from_known_dhkl_graph( | |||||
| time0: WavelengthDefinitionChopperDelay, | ||||||
| dhkl_list: DHKLList, | ||||||
| ) -> TofCoordTransformGraph: | ||||||
| """Graph computing ``tof`` in modulation chopper modes using | ||||||
| list of peak positions.""" | ||||||
|
|
||||||
| def _compute_coarse_dspacing( | ||||||
| event_time_offset, | ||||||
| time_of_arrival: sc.Variable, | ||||||
| theta: sc.Variable, | ||||||
| pulse_length: sc.Variable, | ||||||
| L0, | ||||||
| L0: sc.Variable, | ||||||
| ): | ||||||
| '''To capture dhkl_list, otherwise it causes an error when | ||||||
| """To capture dhkl_list, otherwise it causes an error when | ||||||
| ``.transform_coords`` is called unless it is called with | ||||||
| ``keep_indermediates=False``. | ||||||
| The error happens because data arrays do not allow coordinates | ||||||
| with dimensions not present on the data. | ||||||
| ''' | ||||||
| """ | ||||||
| return _compute_d( | ||||||
| event_time_offset=event_time_offset, | ||||||
| time_of_arrival=time_of_arrival, | ||||||
| theta=theta, | ||||||
| pulse_length=pulse_length, | ||||||
| L0=L0, | ||||||
|
|
@@ -176,19 +195,56 @@ def _compute_coarse_dspacing( | |||||
| 'mod_period': lambda: mod_period, | ||||||
| 'time0': lambda: time0, | ||||||
| 'tof': _tof_from_dhkl, | ||||||
| 'time_of_arrival': time_of_arrival, | ||||||
| 'coarse_dhkl': _compute_coarse_dspacing, | ||||||
| 'theta': lambda two_theta: two_theta / 2, | ||||||
| } | ||||||
|
|
||||||
|
|
||||||
| def compute_tof_from_known_peaks( | ||||||
| def t0_estimate( | ||||||
| wavelength_estimate: sc.Variable, | ||||||
| L0: sc.Variable, | ||||||
| Ltotal: sc.Variable, | ||||||
| ) -> sc.Variable: | ||||||
| """Estimates the time-at-chopper by assuming the wavelength.""" | ||||||
| return ( | ||||||
| sc.constants.m_n | ||||||
| / sc.constants.h | ||||||
| * wavelength_estimate | ||||||
| * (L0 - Ltotal).to(unit=wavelength_estimate.unit) | ||||||
| ).to(unit='s') | ||||||
|
|
||||||
|
|
||||||
| def _tof_from_t0( | ||||||
| time_of_arrival: sc.Variable, | ||||||
| t0: sc.Variable, | ||||||
| ) -> sc.Variable: | ||||||
| """Computes time-of-flight by subtracting a start time.""" | ||||||
| return time_of_arrival - t0 | ||||||
|
|
||||||
|
|
||||||
| def tof_from_t0_estimate_graph() -> TofCoordTransformGraph: | ||||||
| """Graph for computing ``tof`` in pulse shaping chopper modes.""" | ||||||
| return { | ||||||
| 't0': t0_estimate, | ||||||
| 'tof': _tof_from_t0, | ||||||
| 'time_of_arrival': time_of_arrival, | ||||||
| } | ||||||
|
|
||||||
|
|
||||||
| def compute_tof( | ||||||
| da: RawDetector[RunType], graph: TofCoordTransformGraph | ||||||
| ) -> TofDetector[RunType]: | ||||||
| """Uses the transformation graph to compute ``tof``.""" | ||||||
| return da.transform_coords(('tof',), graph=graph) | ||||||
|
|
||||||
|
|
||||||
| convert_from_known_peaks_providers = ( | ||||||
| tof_from_known_dhkl_graph, | ||||||
| compute_tof_from_known_peaks, | ||||||
| compute_tof, | ||||||
| ) | ||||||
| convert_pulse_shaping = ( | ||||||
| tof_from_t0_estimate_graph, | ||||||
| compute_tof, | ||||||
| ) | ||||||
| providers = (compute_tof_in_each_cluster,) | ||||||
This file contains hidden or 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.
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.
Uh oh!
There was an error while loading. Please reload this page.