Beam blanked unblanked #125
Merged
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.
Context
There is a common programming pattern in instamatic to unblank and then blank (or vice versa) the beam in short succession. The
get_image
method built into controller as well as many experimental procedures often do that either to briefly shield the detector or to collect a single image without exposing the sample too much. Unfortunately, this is typically done by 1) checking if beam is blanked, 2) checking if auto-blank is on, 3) blanking the beam, 4) reverting the blanking if needed after the fact. This pattern is not very large, but it repeats over the whole code base 22 times, according to my counting.This small PR suggest adding two new context manager methods to the
Beam
object:blanked
andunblanked
. These can be used via thewith
keyword and they significantly shorten the boilerplate code used to check for previous blanking, auto-blanking, delay etc., placing them all into a one call and determining the scope via indentation. For example, in the controller theget_image
function gets simplified from this (empty lines ommited):To this:
Apart from adding the new method, I have applied them in 10 selected places where it was straightforward and where I knew it does not change the behavior. As a consequence you can see that this PR adds only a few more line than it removes, even though it expands the documentation and the existing test.
Code changes
beam.blanked
andbeam.unblanked
context manager methods that can simplify code with sequential calls tobeam.blank
andbeam.unblank
.