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

Edge align Function created #943

Merged
merged 86 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from 81 commits
Commits
Show all changes
86 commits
Select commit Hold shift + click to select a range
fecde0e
formatted using ruff
MDecarabas Feb 29, 2024
01ac22a
Edge alignment standalone file
MDecarabas Mar 8, 2024
eb1413f
Added edge_align function for beam edge alignment
Mar 18, 2024
67f0929
Need to find a better way to check signal quality.
Mar 18, 2024
0f84512
Formatted using ruff
Mar 18, 2024
33cb0e7
small formatting changes
Mar 19, 2024
5b7718f
removed unused stats import
Mar 19, 2024
980f1de
removed another unused import
Mar 19, 2024
bd43833
Added a check function that yields correct results
Mar 21, 2024
9037214
intermediate commit saving progress
Mar 26, 2024
f0c62f5
finished try/except when bad signal is detected
Apr 10, 2024
6d9ad65
Fixed formatting
Apr 11, 2024
0d18572
Added scipy to environment.yaml & requirements.txt
Apr 12, 2024
401737c
CI #909 switch to 'ruff'
prjemian Feb 26, 2024
562df2b
DOC #909 update release notes
Apr 12, 2024
25ca90e
MNT #909 revise per 'ruff check'
prjemian Feb 26, 2024
89fac23
CI #909 update
prjemian Feb 26, 2024
fea7f07
Bump davidslusser/actions_python_ruff from 1.0.0 to 1.0.1
dependabot[bot] Mar 11, 2024
21bebf1
MNT #872 hoist from MPE devices
prjemian Mar 13, 2024
7e09c68
TST #872 adjust for new hoist
prjemian Mar 13, 2024
52a5e81
MNT #872
prjemian Mar 13, 2024
ed993a8
MNT #872 refactor
prjemian Mar 13, 2024
384d952
MNT #872 hoist utils
prjemian Mar 13, 2024
db16d59
DOC #912
prjemian Mar 14, 2024
7905501
ENV #938 bump the minimum requirement
prjemian Mar 14, 2024
2e61235
DOC #932 update relase notes
prjemian Mar 14, 2024
92685e5
DOC #938 contribution from @sveseli
prjemian Mar 15, 2024
3b4de0f
MNT #914 setup.py --> pyproject.toml
prjemian Mar 20, 2024
9da3291
DOC #921 copyright year
prjemian Mar 20, 2024
482c5bb
MNT #914
prjemian Mar 20, 2024
da4c7af
Added edge_align function for beam edge alignment
Mar 18, 2024
b843e52
added scipy to pyproject.toml
Apr 12, 2024
88496a5
CI #909 switch to 'ruff'
prjemian Feb 26, 2024
405e331
DOC badge
prjemian Mar 27, 2024
2626f98
DOC badge
prjemian Mar 27, 2024
8fa4994
DOC #940 example scans with sscan record
prjemian Mar 17, 2024
5ff31bc
DOC #940 it's only a word ...
prjemian Mar 17, 2024
c269755
DOC #940
prjemian Mar 17, 2024
5d79ea2
DOC #940 it's a demo, actually
prjemian Mar 17, 2024
1803949
DOC #940
prjemian Mar 17, 2024
caad0fc
DOC #940 convert the blocking call to st.wait()
prjemian Mar 18, 2024
2486715
DOC #940 per review
prjemian Mar 18, 2024
c0526a5
DOC #940 per review
prjemian Mar 18, 2024
7306c01
DOC #940 proofreading
prjemian Mar 18, 2024
8c4257d
DOC #940 per review, remove the section with the polling loop
prjemian Mar 18, 2024
842fd5c
DOC #940 new title
prjemian Mar 18, 2024
c7a2bce
DOC #940 more proofing
prjemian Mar 18, 2024
eae3fbd
DOC #940 clarify the default sscan record array size of 1,000
prjemian Mar 20, 2024
9355533
DOC #940 refactor to polling loop. Show the recommended style in a de…
prjemian Mar 20, 2024
2552b85
DOC #940
prjemian Mar 20, 2024
5a08707
Added put_complete=True to sensitivity value and unit for the SRS-570…
canismarko Jan 24, 2024
1b62aca
Added a test for the sensitivity_unit put_complete, and removed the p…
canismarko Jan 25, 2024
e5246d7
CI #955 move requirements file
prjemian Apr 4, 2024
00dc11f
MNT #955 sphinx config
prjemian Apr 4, 2024
2e16fa9
DOC #955 re-arrange
prjemian Apr 5, 2024
5125ec3
DOC #955 apply to lower-level pages
prjemian Apr 5, 2024
65eb6b4
DOC #955 add docs upload
prjemian Apr 6, 2024
3ea2945
CI #955
prjemian Apr 6, 2024
818f955
CI #955
prjemian Apr 6, 2024
07f326a
CI #955 wip
prjemian Apr 6, 2024
010c97c
CI #955
prjemian Apr 6, 2024
cc20fe1
CI #955
prjemian Apr 6, 2024
0295eb4
DOC #917 release note
prjemian Apr 5, 2024
6869bc9
DOC #957 merge overview into home page
prjemian Apr 8, 2024
63c1db0
DOC #955
prjemian Apr 9, 2024
1264326
DOC #955
prjemian Apr 10, 2024
45cdde8
DOC #955
prjemian Apr 10, 2024
d47ce56
DOC #955
prjemian Apr 10, 2024
75d3169
Added edge_align function for beam edge alignment
Mar 18, 2024
af16d1b
Added edge_align function for beam edge alignment
Mar 18, 2024
97162a7
Merge branch 'main' into edge_align
MDecarabas Apr 12, 2024
fc72290
Added edge_align function for beam edge alignment
Mar 18, 2024
8a808ab
DOC #909 update release notes
Apr 12, 2024
83c8427
DOC #932 update relase notes
prjemian Mar 14, 2024
befaf0d
Added edge_align function for beam edge alignment
Mar 18, 2024
16321cc
CI #909 switch to 'ruff'
prjemian Feb 26, 2024
0715d14
DOC #955
prjemian Apr 9, 2024
7490e48
Fixed merge issue
Apr 12, 2024
1caf284
fixed unused import
Apr 12, 2024
4850968
change for tests to pass
Apr 12, 2024
e097341
Formatting changes
Apr 12, 2024
b75f9b2
Fixed repeated section
Apr 12, 2024
2d7e753
Merge branch 'main' into edge_align
MDecarabas Apr 12, 2024
89792bd
Small formatting changes
Apr 12, 2024
c7015c3
Small syntax change
Apr 12, 2024
4539af0
Added toolz & scipy to conf.py
Apr 12, 2024
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
81 changes: 81 additions & 0 deletions .ruff.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# Exclude a variety of commonly ignored directories.
exclude = [
".bzr",
".direnv",
".eggs",
".git",
".git-rewrite",
".hg",
".ipynb_checkpoints",
".mypy_cache",
".nox",
".pants.d",
".pyenv",
".pytest_cache",
".pytype",
".ruff_cache",
".svn",
".tox",
".venv",
".vscode",
"__pypackages__",
"_build",
"buck-out",
"build",
"dist",
"node_modules",
"site-packages",
"venv",
]

# Same as Black.
line-length = 115
indent-width = 4

# Assume Python 3.8
target-version = "py38"

[lint]
# Enable Pyflakes (`F`) and a subset of the pycodestyle (`E`) codes by default.
# Unlike Flake8, Ruff doesn't enable pycodestyle warnings (`W`) or
# McCabe complexity (`C901`) by default.
select = ["E4", "E7", "E9", "F"]
ignore = ["E402", "E741", "F405"]

# Allow fix for all enabled rules (when `--fix`) is provided.
fixable = ["ALL"]
unfixable = []

# Allow unused variables when underscore-prefixed.
dummy-variable-rgx = "^(_+|(_+[a-zA-Z0-9_]*[a-zA-Z0-9]+?))$"

[lint.per-file-ignores]
"__init__.py" = ["F401", "F403"]
"**/{tests,docs,tools}/*" = ["E402"]

[format]
# Like Black, use double quotes for strings.
quote-style = "double"

# Like Black, indent with spaces, rather than tabs.
indent-style = "space"

# Like Black, respect magic trailing commas.
skip-magic-trailing-comma = false

# Like Black, automatically detect the appropriate line ending.
line-ending = "auto"

# Enable auto-formatting of code examples in docstrings. Markdown,
# reStructuredText code/literal blocks and doctests are all supported.
#
# This is currently disabled by default, but it is planned for this
# to be opt-out in the future.
docstring-code-format = false

# Set the line length limit used when formatting code snippets in
# docstrings.
#
# This only has an effect when the `docstring-code-format` setting is
# enabled.
docstring-code-line-length = "dynamic"
10 changes: 10 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ describe future plans.

release expected by 2024-04-12

New Features
------------

* Add new plan for edge alignment called edge_align

New Features
MDecarabas marked this conversation as resolved.
Show resolved Hide resolved
------------

* Add new plan for edge alignment called edge_align

Fixes
-----

Expand Down
1 change: 1 addition & 0 deletions apstools/plans/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from .alignment import TuneResults
from .alignment import lineup
from .alignment import lineup2
from .alignment import edge_align
from .alignment import tune_axes
from .command_list import CommandFileReadError
from .command_list import command_list_as_table
Expand Down
121 changes: 119 additions & 2 deletions apstools/plans/alignment.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

import numpy as np
import pyRestTable
from scipy.optimize import curve_fit
MDecarabas marked this conversation as resolved.
Show resolved Hide resolved
from scipy.special import erf

from bluesky import plan_stubs as bps
from bluesky import plans as bp
from bluesky import preprocessors as bpp
Expand Down Expand Up @@ -213,10 +216,124 @@ def peak_analysis():
scaler.stage_sigs = old_sigs


def edge_align(detectors, mover, start, end, points, cat=None, md={}):
"""
Align to the edge given mover & detector data, relative to absolute position.

This plan can be used in the queueserver, Jupyter notebooks, and IPython
consoles.

PARAMETERS
----------
detectors *Readable* or [*Readable*]:
Detector object or list of detector objects (each is a Device or
Signal).

mover *Movable*:
Mover object, such as motor or other positioner.

start *float*:
Starting point for the scan. This is an absolute motor location.

end *float*:
Ending point for the scan. This is an absolute motor location.

points *int*:
Number of points in the scan.

cat *databroker.temp().v2*:
Catalog where bluesky data is saved and can be retrieved from.

md *dict*:
User-supplied metadata for this scan.
"""

def guess_erf_params(x_data, y_data):
"""
Provide an initial guess for the parameters of an error function.

Parameters
----------
x_data : A numpy array of the values on the x_axis
y_data : A numpy array of the values on the y_axis

Returns
-------
guess : dict
A dictionary containing the guessed parameters 'low_y_data', 'high_y_data', 'width', and 'midpoint'.
"""

# Sort data to make finding the mid-point easier and to assist in other estimations
y_data_sorted = np.sort(y_data)
x_data_sorted = np.sort(x_data)

# Estimate low and high as the first and last elements (assuming sorted data)
low_y_data = np.min(y_data_sorted)
high_y_data = np.max(y_data_sorted)

low_x_data = np.min(x_data_sorted)
high_x_data = np.max(x_data_sorted)

# Estimate wid as a fraction of the range. This is very arbitrary and might need tuning!
width = (
high_x_data - low_x_data
) / 10 # This is a guess and might need adjustment based on your data's characteristics

# Estimate the midpoint of the x values
midpoint = x_data[int(len(x_data) / 2)]

return [low_y_data, high_y_data, width, midpoint]

def erf_model(x, low, high, width, midpoint):
"""
Create error function for fitting and simulation

Parameters
----------
x : input upon which error function is evaluated
low : min value of error function
high : max value of error function
width : "spread" of error function transition region
midpoint: location of error function's "center"
"""
return (high - low) * 0.5 * (1 - erf((x - midpoint) / width)) + low

if not isinstance(detectors, (tuple, list)):
detectors = [detectors]

_md = dict(purpose="edge_align")
_md.update(md or {})

uid = yield from bp.scan(detectors, mover, start, end, points, md=_md)
cat = cat or utils.getCatalog()
run = cat[uid] # return uids
ds = run.primary.read()

x = ds["mover"]
y = ds["noisy"]

try:
initial_guess = guess_erf_params(x, y)
popt, pcov = curve_fit(erf_model, x, y, p0=initial_guess)
if pcov[3, 3] != np.inf:
print("Significant signal change detected; motor moving to detected edge.")
yield from bps.mv(mover, popt[3])
else:
raise Exception
except Exception as reason:
print(f"reason: {reason}")
print("No significant signal change detected; motor movement skipped.")


def lineup2(
# fmt: off
detectors, mover, rel_start, rel_end, points,
peak_factor=2.5, width_factor=0.8,
detectors,
MDecarabas marked this conversation as resolved.
Show resolved Hide resolved
mover,
rel_start,
rel_end,
points,
peak_factor=2.5,
width_factor=0.8,
feature="centroid",
nscans=2,
signal_stats=None,
Expand Down
9 changes: 5 additions & 4 deletions docs/source/api/_plans.rst
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,15 @@ Custom Scans

.. autosummary::

~apstools.plans.doc_run.documentation_run
~apstools.plans.labels_to_streams.label_stream_decorator
~apstools.plans.alignment.edge_align
~apstools.plans.alignment.lineup
~apstools.plans.alignment.lineup2
~apstools.plans.alignment.tune_axes
~apstools.plans.alignment.TuneAxis
~apstools.plans.doc_run.documentation_run
~apstools.plans.labels_to_streams.label_stream_decorator
~apstools.plans.nscan_support.nscan
~apstools.plans.sscan_support.sscan_1D
~apstools.plans.alignment.TuneAxis
~apstools.plans.alignment.tune_axes

.. _plans.overall:

Expand Down
1 change: 1 addition & 0 deletions environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ dependencies:
- pysumreg
- qt =5
- readline !=8.1.2
- scipy
- setuptools-scm
- spec2nexus
- sphinx >=5
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ dependencies = [
"pyepics>=3.4.2",
"pyRestTable>=2020.0.8",
"pysumreg",
"scipy",
"spec2nexus>=2021.1.7",
"xlrd",
]
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ psutil
pyepics>=3.4.2
pyRestTable
pysumreg
scipy
spec2nexus>=2021.1.7
xlrd