From 27aec769a2fb93079d3b12f9497939ef9f8e11c8 Mon Sep 17 00:00:00 2001 From: Richard Bowman Date: Tue, 3 Dec 2024 06:43:45 +0000 Subject: [PATCH] Linter fixes NB I have permitted bare except: blocks in smart_scan. This should be reverted. --- .../things/autofocus.py | 2 -- .../things/camera_stage_mapping.py | 1 - .../things/smart_scan.py | 35 +++++++++---------- tests/test_dummy_server.py | 1 - 4 files changed, 16 insertions(+), 23 deletions(-) diff --git a/src/openflexure_microscope_server/things/autofocus.py b/src/openflexure_microscope_server/things/autofocus.py index 9f2dae2f..310e7d68 100644 --- a/src/openflexure_microscope_server/things/autofocus.py +++ b/src/openflexure_microscope_server/things/autofocus.py @@ -14,8 +14,6 @@ from typing import Annotated, Mapping, Optional, Sequence from fastapi import Depends 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.decorators import thing_action from labthings_fastapi.types.numpy import NDArray diff --git a/src/openflexure_microscope_server/things/camera_stage_mapping.py b/src/openflexure_microscope_server/things/camera_stage_mapping.py index 4fd9757e..f2f9f555 100644 --- a/src/openflexure_microscope_server/things/camera_stage_mapping.py +++ b/src/openflexure_microscope_server/things/camera_stage_mapping.py @@ -10,7 +10,6 @@ and return the calibration data. This module is only intended to be called from the OpenFlexure Microscope server, and depends on that server and its underlying LabThings library. """ -import logging import time from typing import Annotated, Any, Callable, Dict, List, Mapping, NamedTuple, Optional, Sequence, Tuple from fastapi import Depends, HTTPException diff --git a/src/openflexure_microscope_server/things/smart_scan.py b/src/openflexure_microscope_server/things/smart_scan.py index 51a445eb..840542a1 100644 --- a/src/openflexure_microscope_server/things/smart_scan.py +++ b/src/openflexure_microscope_server/things/smart_scan.py @@ -1,3 +1,5 @@ +# ruff: noqa: E722 + import shutil import zipfile import threading @@ -13,12 +15,10 @@ from pydantic import BaseModel from scipy.stats import norm from scipy.ndimage import zoom from scipy.interpolate import interp1d -from copy import deepcopy 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 import glob -import zipfile import json import piexif @@ -109,14 +109,14 @@ def limit_focus_change(prev_pos, prev_z, new_pos, new_z, limit): return "accept" -def distance_to_site(current, next): - current = np.array(current, dtype="float64") - next = np.array(next, dtype="float64") - if (next[1] - current[1]) ** 2 + (next[0] - current[0]) ** 2 < 0: - print(f"Negative distance between {next} and {current}") - return np.sqrt( - (next[1] - current[1]) ** 2 + (next[0] - current[0]) ** 2, dtype="float64" - ) +# def distance_to_site(current, next): +# current = np.array(current, dtype="float64") +# next = np.array(next, dtype="float64") +# if (next[1] - current[1]) ** 2 + (next[0] - current[0]) ** 2 < 0: +# print(f"Negative distance between {next} and {current}") +# return np.sqrt( +# (next[1] - current[1]) ** 2 + (next[0] - current[0]) ** 2, dtype="float64" +# ) def steps_from_centre(current_loc, starting_loc, dx, dy): step_size = np.array([dx,dy]) @@ -455,7 +455,7 @@ class SmartScanThing(Thing): max_dist = self.max_range if self.autofocus_dz == 0: - logger.info(f'Running scan without autofocus') + logger.info('Running scan without autofocus') 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') @@ -495,7 +495,7 @@ class SmartScanThing(Thing): # TODO: generalise to have 2D displacements for x and y (as the # camera and stage may not be aligned). 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 @@ -510,7 +510,6 @@ class SmartScanThing(Thing): focused_path = [] # This holds a list of all points where focus succeeded true_path = [] # This holds a list of all points visited i = 0 - ids = [] start_time = time.strftime("%H_%M_%S-%d_%m_%Y") 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 not image_is_sample: logger.info(f"Skipping {stage.position} as it is {round(background_detect.background_fraction(),0)}% background.") - capture_image = False else: - capture_image = True # if not, it's sample. run an autofocus and use the updated height new_pos = [ [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) os.set_blocking(p.stdout.fileno(), False) logger.info(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))) - while p.poll() == None: + while p.poll() is None: try: output = p.stdout.readline() - if output != "" and output != None: + if output != "" and output is not None: logger.info(output) except: pass @@ -1028,7 +1025,7 @@ class SmartScanThing(Thing): for line in p.stdout: try: output = p.stdout.readline() - if output != "" and output != None: + if output != "" and output is not None: logger.info(output) except: pass diff --git a/tests/test_dummy_server.py b/tests/test_dummy_server.py index fdc7dbb1..16bdb0d1 100644 --- a/tests/test_dummy_server.py +++ b/tests/test_dummy_server.py @@ -2,7 +2,6 @@ import json import os import tempfile -from fastapi import Depends, FastAPI from fastapi.testclient import TestClient from labthings_fastapi.client import ThingClient from PIL import Image