Linter fixes

NB I have permitted bare except: blocks in smart_scan. This should be reverted.
This commit is contained in:
Richard Bowman 2024-12-03 06:43:45 +00:00
parent 57177c6a56
commit 27aec769a2
4 changed files with 16 additions and 23 deletions

View file

@ -14,8 +14,6 @@ from typing import Annotated, Mapping, Optional, Sequence
from fastapi import Depends from fastapi import Depends
from labthings_fastapi.thing import Thing from labthings_fastapi.thing import Thing
from labthings_fastapi.dependencies.raw_thing import raw_thing_dependency
from labthings_fastapi.dependencies.thing import direct_thing_client_dependency
from labthings_fastapi.dependencies.blocking_portal import BlockingPortal from labthings_fastapi.dependencies.blocking_portal import BlockingPortal
from labthings_fastapi.decorators import thing_action from labthings_fastapi.decorators import thing_action
from labthings_fastapi.types.numpy import NDArray from labthings_fastapi.types.numpy import NDArray

View file

@ -10,7 +10,6 @@ and return the calibration data.
This module is only intended to be called from the OpenFlexure Microscope This module is only intended to be called from the OpenFlexure Microscope
server, and depends on that server and its underlying LabThings library. server, and depends on that server and its underlying LabThings library.
""" """
import logging
import time import time
from typing import Annotated, Any, Callable, Dict, List, Mapping, NamedTuple, Optional, Sequence, Tuple from typing import Annotated, Any, Callable, Dict, List, Mapping, NamedTuple, Optional, Sequence, Tuple
from fastapi import Depends, HTTPException from fastapi import Depends, HTTPException

View file

@ -1,3 +1,5 @@
# ruff: noqa: E722
import shutil import shutil
import zipfile import zipfile
import threading import threading
@ -13,12 +15,10 @@ from pydantic import BaseModel
from scipy.stats import norm from scipy.stats import norm
from scipy.ndimage import zoom from scipy.ndimage import zoom
from scipy.interpolate import interp1d from scipy.interpolate import interp1d
from copy import deepcopy
from datetime import datetime from datetime import datetime
from subprocess import CompletedProcess, Popen, PIPE, SubprocessError, run, STDOUT from subprocess import CompletedProcess, Popen, PIPE, SubprocessError, STDOUT
from threading import Event, Thread from threading import Event, Thread
import glob import glob
import zipfile
import json import json
import piexif import piexif
@ -109,14 +109,14 @@ def limit_focus_change(prev_pos, prev_z, new_pos, new_z, limit):
return "accept" return "accept"
def distance_to_site(current, next): # def distance_to_site(current, next):
current = np.array(current, dtype="float64") # current = np.array(current, dtype="float64")
next = np.array(next, dtype="float64") # next = np.array(next, dtype="float64")
if (next[1] - current[1]) ** 2 + (next[0] - current[0]) ** 2 < 0: # if (next[1] - current[1]) ** 2 + (next[0] - current[0]) ** 2 < 0:
print(f"Negative distance between {next} and {current}") # print(f"Negative distance between {next} and {current}")
return np.sqrt( # return np.sqrt(
(next[1] - current[1]) ** 2 + (next[0] - current[0]) ** 2, dtype="float64" # (next[1] - current[1]) ** 2 + (next[0] - current[0]) ** 2, dtype="float64"
) # )
def steps_from_centre(current_loc, starting_loc, dx, dy): def steps_from_centre(current_loc, starting_loc, dx, dy):
step_size = np.array([dx,dy]) step_size = np.array([dx,dy])
@ -455,7 +455,7 @@ class SmartScanThing(Thing):
max_dist = self.max_range max_dist = self.max_range
if self.autofocus_dz == 0: if self.autofocus_dz == 0:
logger.info(f'Running scan without autofocus') logger.info('Running scan without autofocus')
elif self.autofocus_dz <= 200: elif self.autofocus_dz <= 200:
logger.warning(f'Your dz range is {self.autofocus_dz} steps, which is too short to attempt to focus. Running without autofocus') logger.warning(f'Your dz range is {self.autofocus_dz} steps, which is too short to attempt to focus. Running without autofocus')
@ -495,7 +495,7 @@ class SmartScanThing(Thing):
# TODO: generalise to have 2D displacements for x and y (as the # TODO: generalise to have 2D displacements for x and y (as the
# camera and stage may not be aligned). # camera and stage may not be aligned).
CSM = csm.image_to_stage_displacement_matrix CSM = csm.image_to_stage_displacement_matrix
csm_calibration_width = csm.last_calibration["image_resolution"][1] #csm_calibration_width = csm.last_calibration["image_resolution"][1]
overlap = self.overlap overlap = self.overlap
@ -510,7 +510,6 @@ class SmartScanThing(Thing):
focused_path = [] # This holds a list of all points where focus succeeded focused_path = [] # This holds a list of all points where focus succeeded
true_path = [] # This holds a list of all points visited true_path = [] # This holds a list of all points visited
i = 0 i = 0
ids = []
start_time = time.strftime("%H_%M_%S-%d_%m_%Y") start_time = time.strftime("%H_%M_%S-%d_%m_%Y")
scan_folder = self.new_scan_folder(scan_name) scan_folder = self.new_scan_folder(scan_name)
@ -628,9 +627,7 @@ class SmartScanThing(Thing):
# if more than 92% of the image is background, treat it as background and continue # if more than 92% of the image is background, treat it as background and continue
if not image_is_sample: if not image_is_sample:
logger.info(f"Skipping {stage.position} as it is {round(background_detect.background_fraction(),0)}% background.") logger.info(f"Skipping {stage.position} as it is {round(background_detect.background_fraction(),0)}% background.")
capture_image = False
else: else:
capture_image = True
# if not, it's sample. run an autofocus and use the updated height # if not, it's sample. run an autofocus and use the updated height
new_pos = [ new_pos = [
[stage.position["x"] - dx, stage.position["y"]], [stage.position["x"] - dx, stage.position["y"]],
@ -1017,10 +1014,10 @@ class SmartScanThing(Thing):
p = Popen(cmd, stdout=PIPE, stderr = STDOUT, bufsize=1, universal_newlines=True) p = Popen(cmd, stdout=PIPE, stderr = STDOUT, bufsize=1, universal_newlines=True)
os.set_blocking(p.stdout.fileno(), False) os.set_blocking(p.stdout.fileno(), False)
logger.info(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))) logger.info(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())))
while p.poll() == None: while p.poll() is None:
try: try:
output = p.stdout.readline() output = p.stdout.readline()
if output != "" and output != None: if output != "" and output is not None:
logger.info(output) logger.info(output)
except: except:
pass pass
@ -1028,7 +1025,7 @@ class SmartScanThing(Thing):
for line in p.stdout: for line in p.stdout:
try: try:
output = p.stdout.readline() output = p.stdout.readline()
if output != "" and output != None: if output != "" and output is not None:
logger.info(output) logger.info(output)
except: except:
pass pass

View file

@ -2,7 +2,6 @@ import json
import os import os
import tempfile import tempfile
from fastapi import Depends, FastAPI
from fastapi.testclient import TestClient from fastapi.testclient import TestClient
from labthings_fastapi.client import ThingClient from labthings_fastapi.client import ThingClient
from PIL import Image from PIL import Image