-
Notifications
You must be signed in to change notification settings - Fork 2
Analysis of Post-mortem triggered BPMs acquistions #194
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
Draft
vellosok75
wants to merge
2
commits into
master
Choose a base branch
from
postmortem-anly
base: master
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.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,276 @@ | ||
| #!/usr/bin/env python-sirius | ||
| """.""" | ||
|
|
||
| import argparse | ||
| import os | ||
| import subprocess | ||
| from pathlib import Path | ||
|
|
||
| import numpy as np | ||
| from apsuite.commisslib.meas_bpms_signals import AcqBPMsSignals | ||
| from matplotlib import pyplot as plt | ||
| from siriuspy.clientarch import Time | ||
| from siriuspy.devices import SOFB | ||
|
|
||
|
|
||
| def acquire_data(): | ||
| """.""" | ||
| acq = AcqBPMsSignals(ispost_mortem=True) | ||
| acq.wait_for_connection() | ||
| acq.data = acq.get_data() | ||
|
|
||
| return acq, acq.data | ||
|
|
||
|
|
||
| def prepare_output_dir(data, folder=None): | ||
| """.""" | ||
| timestamp = Time(data['timestamp']) | ||
|
|
||
| day = timestamp.strftime('%Y-%m-%d') | ||
| stamp = timestamp.strftime('%Y-%m-%d-%Hh%Mm%Ss') | ||
|
|
||
| if folder is None: | ||
| base = Path.home() / 'shared' / 'screens-iocs' / 'data_by_day' | ||
|
|
||
| if not base.exists() or not os.access(base, os.W_OK): | ||
| base = Path.home() | ||
| else: | ||
| base = Path(folder).expanduser() | ||
|
|
||
| outdir = base / f'{day}-SI_postmortem_data' | ||
| outdir.mkdir(parents=True, exist_ok=True) | ||
|
|
||
| return outdir, stamp | ||
|
|
||
|
|
||
| def compute_orbit_distortion(data): | ||
| """.""" | ||
| x = data['orbx'] | ||
| y = data['orby'] | ||
|
|
||
| nsamples = min(len(x) // 2, x.shape[0]) | ||
|
|
||
| # orbit distortion (along BPMs) | ||
| orbx_dist = x[:100].mean(axis=0) - x[nsamples - 100 : nsamples].mean( | ||
| axis=0 | ||
| ) | ||
| orby_dist = y[:100].mean(axis=0) - y[nsamples - 100 : nsamples].mean( | ||
| axis=0 | ||
| ) | ||
|
|
||
| # orbit distortion (along time) | ||
| orbx_drift = x - x[:100].mean(axis=0) | ||
| orby_drift = y - y[:100].mean(axis=0) | ||
|
|
||
| tim = (-(np.arange(nsamples) / data['sampling_frequency'] * 1e3))[::-1] | ||
|
|
||
| return orbx_dist, orby_dist, orbx_drift, orby_drift, tim, nsamples | ||
|
|
||
|
|
||
| def compute_correctors(sofb, orbx_dist, orby_dist): | ||
| """.""" | ||
| corrs = sofb.invrespmat @ np.hstack([orbx_dist, orby_dist]) | ||
|
|
||
| chs = corrs[:120] | ||
| cvs = corrs[120:280] | ||
|
|
||
| outch = np.abs(chs) > 2 * chs.std() | ||
| outcv = np.abs(cvs) > 2 * cvs.std() | ||
|
|
||
| return chs, cvs, outch, outcv | ||
|
|
||
|
|
||
| def plot_results( | ||
| sofb, | ||
| stamp, | ||
| orbx_dist, | ||
| orby_dist, | ||
| orbx_drift, | ||
| orby_drift, | ||
| tim, | ||
| nsamples, | ||
| chs, | ||
| cvs, | ||
| outch, | ||
| outcv, | ||
| ): | ||
| """.""" | ||
| bpms_names = np.asarray(sofb.data.bpm_names) | ||
| ch_names = np.asarray(sofb.data.ch_names) | ||
| cv_names = np.asarray(sofb.data.cv_names) | ||
|
|
||
| idcsx = np.argsort(np.abs(orbx_dist)) | ||
| idcsy = np.argsort(np.abs(orby_dist)) | ||
|
|
||
| fig, axs = plt.subplots( | ||
| 3, | ||
| 2, | ||
| figsize=(14, 14), | ||
| ) | ||
|
|
||
| # orbit distortion | ||
| axs[0, 0].plot(orbx_dist) | ||
| axs[0, 0].set_title('horizontal orbit distortion') | ||
| axs[0, 0].set_xlabel('bpms index') | ||
| axs[0, 0].set_ylabel('orbit distortion [um]') | ||
|
|
||
| axs[0, 1].plot(orby_dist) | ||
| axs[0, 1].set_title('vertical orbit distortion') | ||
| axs[0, 1].set_xlabel('bpms index') | ||
|
|
||
| # bpms time drift for bpms w/ largest distortions | ||
| for k, alpha in zip(range(1, 6), [1, 0.8, 0.6, 0.4, 0.2]): | ||
| idx = idcsx[-k] | ||
| axs[1, 0].plot( | ||
| tim, | ||
| orbx_drift[:nsamples, idx], | ||
| alpha=alpha, | ||
| label=bpms_names[idx], | ||
| ) | ||
| axs[1, 0].set_title('horizontal orbit drift (largest BPMs drifts only)') | ||
| axs[1, 0].set_xlabel('time [ms]') | ||
| axs[1, 0].set_ylabel('orbit drift [um]') | ||
| axs[1, 0].legend() | ||
|
|
||
| for k, alpha in zip(range(1, 6), [1, 0.8, 0.6, 0.4, 0.2]): | ||
| idx = idcsy[-k] | ||
| axs[1, 1].plot( | ||
| tim, | ||
| orby_drift[:nsamples, idx], | ||
| alpha=alpha, | ||
| label=bpms_names[idx], | ||
| ) | ||
|
|
||
| axs[1, 1].legend() | ||
| axs[1, 1].set_title('vertical orbit drift (largest BPMs drifts only)') | ||
| axs[1, 1].set_xlabel('time [ms]') | ||
|
|
||
| # horizontal correctors | ||
| axs[2, 0].plot(chs) | ||
|
|
||
| idx = np.where(outch)[0] | ||
|
|
||
| axs[2, 0].plot(idx, chs[idx], 'o', color='red', mfc='none') | ||
|
|
||
| for i in idx: | ||
| axs[2, 0].annotate( | ||
| ch_names[i], | ||
| (i, chs[i]), | ||
| xytext=(3, 3), | ||
| textcoords='offset points', | ||
| fontsize=8, | ||
| color='red', | ||
| ) | ||
| axs[2, 0].set_ylabel('corrector strengths [urad]') | ||
| axs[2, 0].set_title( | ||
| 'horizontal correctors strengths explaining the distortions' | ||
| ) | ||
| axs[2, 0].set_xlabel('CH index') | ||
|
|
||
| # vertical correctors | ||
| axs[2, 1].plot(cvs) | ||
|
|
||
| idx = np.where(outcv)[0] | ||
|
|
||
| axs[2, 1].plot(idx, cvs[idx], 'o', color='red', mfc='none') | ||
|
|
||
| for i in idx: | ||
| axs[2, 1].annotate( | ||
| cv_names[i], | ||
| (i, cvs[i]), | ||
| xytext=(3, 3), | ||
| textcoords='offset points', | ||
| fontsize=8, | ||
| color='red', | ||
| ) | ||
| axs[2, 1].set_title( | ||
| 'vertical correctors strengths explaining the distortions' | ||
| ) | ||
| axs[2, 1].set_xlabel('CV index') | ||
|
|
||
| plt.suptitle('SI BPMs pos-mortem analysis\n{}'.format(stamp)) | ||
|
|
||
| return fig | ||
|
|
||
|
|
||
| def save_and_show(fig, outdir): | ||
| """.""" | ||
| filename = outdir / 'postmortem_analysis.png' | ||
|
|
||
| fig.savefig( | ||
| filename, | ||
| dpi=300, | ||
| bbox_inches='tight', | ||
| ) | ||
| print(f'Post-mortem analysis figure saved to {filename}.') | ||
|
|
||
| try: | ||
| subprocess.Popen(['xdg-open', str(filename)]) | ||
| except Exception as e: | ||
| print(f'Failed to open image: {e}') | ||
|
|
||
|
|
||
| def main(): | ||
| """.""" | ||
| parser = argparse.ArgumentParser() | ||
|
|
||
| parser.add_argument( | ||
| '-f', | ||
| '--folder', | ||
| default=None, | ||
| help=( | ||
| 'Base folder where the output directory will be created. ' | ||
| 'Defaults to ~/shared/screens-iocs/data_by_day. ' | ||
| 'If unavailable or w/o writing permissions, falls back to $HOME.' | ||
| ), | ||
| ) | ||
|
|
||
| args = parser.parse_args() | ||
|
|
||
| acq, data = acquire_data() | ||
|
|
||
| outdir, stamp = prepare_output_dir( | ||
| data, | ||
| folder=args.folder, | ||
| ) | ||
|
|
||
| acq.save_data(str(outdir / f'postmortem_data_{stamp}.pickle')) | ||
|
|
||
| sofb = SOFB(SOFB.DEVICES.SI) | ||
|
|
||
| ( | ||
| orbx_dist, | ||
| orby_dist, | ||
| orbx_drift, | ||
| orby_drift, | ||
| tim, | ||
| nsamples, | ||
| ) = compute_orbit_distortion(data) | ||
|
|
||
| ( | ||
| chs, | ||
| cvs, | ||
| outch, | ||
| outcv, | ||
| ) = compute_correctors(sofb, orbx_dist, orby_dist) | ||
|
|
||
| fig = plot_results( | ||
| sofb, | ||
| stamp, | ||
| orbx_dist, | ||
| orby_dist, | ||
| orbx_drift, | ||
| orby_drift, | ||
| tim, | ||
| nsamples, | ||
| chs, | ||
| cvs, | ||
| outch, | ||
| outcv, | ||
| ) | ||
|
|
||
| save_and_show(fig, outdir) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| main() | ||
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.
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 would use cvs = corrs[-160:]
Actually, get these len constants from the sofb objet...