Commit graph

178 commits

Author SHA1 Message Date
Richard Bowman
3f9c3447de Fixed typos in comments spotted in code review 2023-03-23 13:31:25 +00:00
Richard Bowman
106b50d333 Format fixes from black 2022-08-09 13:17:45 +01:00
Richard Bowman
9dbb083ee7 More Marshmallow argument updates 2022-08-09 13:16:10 +01:00
Richard Bowman
e22f36b308 Start explicit metadata argument for marshmallow
A side effect of re-locking dependencies is having a newer marshmallow.
This deprecates having `description=` as a keyword, and puts it in a
separate metadata arg instead.

This commit is the start of my search-and-replaceing to update to the new
format.
2022-08-09 12:52:57 +01:00
Richard Bowman
78ef9c531b Format fixes from black 2022-08-09 09:37:03 +01:00
Richard Bowman
a9fff24848 Explicitly specify encoding
The update to pylint means we were failing because open()
doesn't specify the encoding everywhere.  I have now specified utf-8
everywhere.  This was patchy before, and is default behaviour on the
Pi.  There's a slim chance it will cause some issues on Windows, but
that shouldn't affect anyone outside the developers.

I've also ignored a couple of new spurious warnings, and explicitly
ignored some unused variables.
2022-08-09 07:16:44 +01:00
Richard Bowman
e356d11e7e Linter/type checking fixes 2022-04-20 13:31:33 +01:00
Richard Bowman
18145d4b67 Refactored new (and some old) code to tidy up scan function
I have:
* Moved the autofocus and focus-prediction code into a
new `FocusManager` class.  This hopefully keeps the logic relating to
axial motion in one place.
* Moved set-up code relating to autofocus and background detection
into separate functions.
* Changed the scan path from a ragged list-of-lists to a one dimensional
list.  I've left the old 2D function and added a new 1D function that
calls it and coverts, so it's not a breaking change.

The actual sequence of moves executed, and the accompanying logic, is
unchanged from the previous commit.  I've tested this a couple of times,
on my microscope with actual hardware.
I don't think it's necessary to test more widely as this is only a
refactoring change, not an algorithm change.

The shift from 2D to 1D scan path was one I initially decided against,
because I was trying to keep the new code as small as possible, and
avoid refactoring what's already there.  Since I'm doing that anyway,
I have taken the opportunity to eliminate the concept of scan
"lines".

The old behaviour was to always use the position of the last point
as the starting point for the next autofocus.  There was an exception
to this for raster scans, where the big jump used the first point
of the last line instead.  The new behaviour always uses the closest
point, which reproduces this behaviour without the need for a hard
coded exception.

If the "fast" scan axis (y) has a longer step size than the "slow"
scan axis (x), we may base the autofocus off the previous row, rather
than the previous point in the current row.  I don't see that this
should be any less reliable than the current behaviour, and is
arguably better.  It's also unlikely to be noticed, because our
default scan settings have longer spacing in X.  I think the
reduced complexity of the code is definitely worth the small
chance of changing some edge-case behaviour.
2022-04-20 13:14:23 +01:00
Richard Bowman
7f66235cac Lint and type fixes 2022-04-06 23:36:55 +01:00
Richard Bowman
333b52aa9b background detect and better focus handling
The primary change here is that there's now an option
to skip autofocus if the background detect plugin says
the current image is background.

I have also overhauled the way it picks the next Z position
based on Joe's code; instead of just using the last point, it will
pick the closest point where we have a successful autofocus
recorded.  This will usually be the last point, except for raster
scans where it neatly reproduces the behaviour of the old code
but without needing to treat it as a special case (when we jump
back to the start of a line, it will use the z position of the start
of the previous line, rather than the end of the previous line).

This does represent a minor change to previous behaviour, but
it should not break anything that isn't already broken, i.e. it might
cause slightly odd behaviour if autofocus gives random results -
but probably indistinguishable from the current behaviour.
2022-04-06 23:15:25 +01:00
Richard Bowman
23bf3410f3 Code formatting fixes from black 2022-01-13 14:47:38 +00:00
Joe Knapper
2d494b41d9 Added docstring for measure_settling_time and hold 2022-01-11 15:46:15 +00:00
Joe Knapper
ecbdd8a5bf Added measure_settling_time function to autofocus 2021-12-01 13:09:32 +00:00
Richard Bowman
040d7d4ba6 Fix action wrapper
The action wrapper was using an unbound function - it
now passes the extension object as self.

We were incorrectly passing endpoint names as positional arguments -
this is fixed and I now use a keyword argument.
2021-08-17 12:11:20 +01:00
Richard
9fa545f3fa Typing fix
mypy doesn't like adding attributes to functions, so
I have used the described_operation decorator to
add type hints to the functions.
2021-07-25 03:38:20 +01:00
Richard
7b54abc210 blackened 2021-07-19 22:22:58 +01:00
Richard
36be07b276 Responses and parameters for zipbuilder 2021-07-19 21:32:08 +01:00
Richard
ba0f78df84 fix typing in autofocus
I have an Optional dz, which is then
populated with a default if it's None.
I've added an explicit cast() to explain this to mypy.
2021-07-19 21:18:25 +01:00
Richard
72f3c11fdd linting and typing fixes 2021-07-19 20:29:13 +01:00
Richard
b7c9d3b73b Sorted remaining content_type issues and blackened 2021-07-19 20:09:09 +01:00
Richard
9892c7079a moved find_microscope into functions 2021-07-13 22:50:42 +01:00
Richard
c81c4eb64d Better API docs for autofocus extension 2021-07-06 15:24:17 +01:00
Richard Bowman
4d405443bb Merge branch 'scan-shape-limits' into 'master'
Add minimum scan dimension

See merge request openflexure/openflexure-microscope-server!123
2021-05-18 10:55:06 +00:00
Richard
f2a4b976b9 Fix whitespace 2021-05-13 10:43:50 +01:00
Richard
0c6e6caade Improve errors with better exception
I've swapped the ValueError for a RuntimeError subclass to
make it clearer what's gone wrong.
2021-05-05 10:05:47 +01:00
Richard Bowman
14c4908a98 fix linter error 2021-05-04 13:41:39 +01:00
Richard Bowman
6e7adb66cd Fix handling of missing CSM matrix
The code checks for the camera-stage mapper matrix by looking at its truth value.
However, this is ambiguous because the matrix has many elements.  That means that
if it's missing, we get the expected result, but if it's present
we get an error.

I have avoided dict.get() and instead look it up with [] notation.
This will raise a KeyError if it's missing, which I
handle helpfully.
If the key is present but the value is not castable to an array, we'll
get an exception anyway.
2021-04-28 22:29:56 +01:00
Richard Bowman
82eba1e99a fixed typing error 2021-04-27 20:17:07 +01:00
Richard Bowman
87f636ecc0 Code format and linting 2021-04-27 19:56:54 +01:00
Richard Bowman
bb303d4766 Simplified auto gain/exposure algorithm
adjust_shutter_and_gain_from_raw now uses a simpler algorithm.
The code is longer overall but hides some details in fuctions.
I've also split the loop into one for shutter speed and one for gain.
This isn't the algorithm that I originally wrote, but it seems to
work and is more readable.
2021-04-27 18:54:21 +01:00
Richard Bowman
79b0d48cf4 Swap Generator annotation for Iterator 2021-04-27 17:13:40 +01:00
Richard Bowman
88de3089ea Fixed linter errors 2021-04-21 13:38:14 +01:00
Richard Bowman
8c50f73b34 Fixed type of context manager 2021-04-21 12:54:53 +01:00
Richard Bowman
fd32c22e30 Fixed formatting 2021-04-21 12:40:10 +01:00
Richard Bowman
1f1a75c02a Merge branch 'master' into auto-exposure-gain-awb 2021-04-21 12:10:47 +01:00
Richard Bowman
1cea75be70 Formatting fixes 2021-03-31 09:40:31 +01:00
Richard Bowman
e73ee8875c Fixed type annotations in autofocus and mock stage
I don't know why these didn't fail before - possibly because of
incomplete type information from old numpy...
I removed a few annotations because they were failing (e.g.
np.sum can return a scalar or an array), but I don't think
this should be a problem - the function inputs and return values
are still typed.
2021-03-30 17:51:00 +01:00
Richard Bowman
cd5e32b843 Removed deprecated np.float
numpy 1.20 deprecates np.float, which was only ever an alias for
``float``.  I've replaced all occurrences of np.float with float, as
recommended.  There should be no change in functionality.
2021-03-30 17:40:19 +01:00
Richard Bowman
a373afd955 swap print for logging in old function 2021-03-16 11:01:57 +00:00
Richard Bowman
3b435ab2ee Deleted obsolete functions 2021-02-18 15:56:11 +00:00
Richard Bowman
b06dd187ec Fixed args in endpoints 2021-02-15 20:18:34 +00:00
Richard Bowman
069d27bf15 Lock access to picamera
The picamera object isn't thread safe; I have now wrapped all access
in context managers that acquire the BaseCamera.lock

I also added an endpoint to reset the LST without auto-gain.
2021-02-15 20:15:55 +00:00
Richard Bowman
a9709b14ea Added locking to recalibrate functions
When testing the UI I noticed that the different recalibrate routines
don't use locks properly, so they try to run
concurrently.

This commit isn't tested yet...
2021-02-15 19:21:27 +00:00
Richard Bowman
0f1a400979 Fixed type hint 2021-02-15 17:04:44 +00:00
Richard Bowman
4c27d88b37 Fixed description and linted 2021-02-11 15:54:58 +00:00
Richard Bowman
5527c4eb7b Fix auto-exposure and AWB
The previous "auto calibrate" button for the camera relied on a
combination of the built-in autoexposure/AWB and
some JPEG-based tweaks to get the exposure settings right.
This often led to confusing results, e.g. oscillating between green and
 pink images.

 I have rewritten the auto-exposure code to adjust gain and shutter
 speed based on the raw image.  This seems to be
 very reliable, at the expense of being quite slow.  That's a price I'm
 happy to pay.

 I have replaced both the camera's auto white balance and my
 JPEG-based AWB hack with a single-shot AWB method that
 looks at the raw image.
2021-02-11 14:30:20 +00:00
Richard Bowman
792e782968 Validate scan shape on the server
The scan API view now uses a schema with validation
constraints to enforce a minimum size of 1 in any dimension.
This requires better error handling in the app, which I've added.
It also needs a fix in LabThings so the error propagates correctly
2021-02-09 09:21:56 +00:00
Joel Collins
58b2967f53 Fixed autofocus 'PiCameraStreamer' object has no attribute 'annotate_text' error 2020-12-07 10:12:30 +00:00
Joel Collins
311366c943 Added type hints to CSM extension 2020-12-04 17:05:40 +00:00
Joel Collins
920354537a Integrated CSMExtension 2020-12-04 16:05:39 +00:00