Commit 677b08d5 authored by Joel Collins's avatar Joel Collins
Browse files

Restructured scan extension

parent 43c16f1b
Loading
Loading
Loading
Loading
+24 −33
Original line number Diff line number Diff line
@@ -8,12 +8,12 @@ from functools import reduce
from openflexure_microscope.captures.capture_manager import generate_basename
from labthings.server.find import find_component, find_extension
from labthings.server.extensions import BaseExtension
from labthings.server.decorators import use_args, ThingAction
from labthings.server.decorators import use_args
from labthings.server import fields

from openflexure_microscope.devel import abort, update_task_progress

from labthings.server.view import View
from labthings.server.view import View, ActionView
import time


@@ -54,18 +54,6 @@ def flatten_grid(grid):
    return grid


### Progress

_images_to_be_captured: int = 1
_images_captured_so_far: int = 0


def progress():
    progress = (_images_captured_so_far / _images_to_be_captured) * 100
    logging.info(progress)
    return progress


### Capturing


@@ -99,10 +87,20 @@ def capture(
    )


### Scanning
class ScanExtension(BaseExtension):
    def __init__(self):
        self._images_to_be_captured: int = 1
        self._images_captured_so_far: int = 0
        return BaseExtension.__init__(self, "org.openflexure.scan", version="2.0.0")

    def progress(self):
        progress = (self._images_captured_so_far / self._images_to_be_captured) * 100
        logging.info(progress)
        return progress
        
    ### Scanning
    def tile(
        self,
        microscope,
        basename: str = None,
        temporary: bool = False,
@@ -118,12 +116,9 @@ def tile(
        annotations: dict = {},
        tags: list = [],
    ):
    global _images_to_be_captured
    global _images_captured_so_far

        # Keep task progress
    _images_to_be_captured = reduce((lambda x, y: x * y), grid)
    _images_captured_so_far = 0
        self._images_to_be_captured = reduce((lambda x, y: x * y), grid)
        self._images_captured_so_far = 0

        # Generate a basename if none given
        if not basename:
@@ -210,11 +205,11 @@ def tile(
                        tags=tags,
                    )
                    # Update task progress
                _images_captured_so_far += 1
                update_task_progress(progress())
                    self._images_captured_so_far += 1
                    update_task_progress(self.progress())
                else:
                    logging.debug("Entering z-stack")
                stack(
                    self.stack(
                        microscope=microscope,
                        basename=basename,
                        temporary=temporary,
@@ -235,6 +230,7 @@ def tile(


    def stack(
        self,
        microscope,
        basename: str = None,
        temporary: bool = False,
@@ -248,7 +244,6 @@ def stack(
        annotations: dict = {},
        tags: list = [],
    ):
    global _images_captured_so_far

        # Store initial position
        initial_position = microscope.stage.position
@@ -275,8 +270,8 @@ def stack(
                    tags=tags,
                )
                # Update task progress
            _images_captured_so_far += 1
            update_task_progress(progress())
                self._images_captured_so_far += 1
                update_task_progress(self.progress())

                if i != steps - 1:
                    logging.debug("Moving z by {}".format(step_size))
@@ -286,11 +281,9 @@ def stack(
                microscope.stage.move_abs(initial_position)


### Web views

scan_extension_v2 = ScanExtension()

@ThingAction
class TileScanAPI(View):
class TileScanAPI(ActionView):
    @use_args(
        {
            "filename": fields.String(missing=None, example=None),
@@ -328,7 +321,7 @@ class TileScanAPI(View):
        logging.info("Running tile scan...")

        # return a handle on the scan task
        return tile(
        return scan_extension_v2.tile(
            microscope,
            basename=args.get("filename"),
            temporary=args.get("temporary"),
@@ -345,6 +338,4 @@ class TileScanAPI(View):
        )


scan_extension_v2 = BaseExtension("org.openflexure.scan", version="2.0.0")

scan_extension_v2.add_view(TileScanAPI, "/tile", endpoint="tile")