Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions Smartscope/core/test_commands.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os
import sys
from pathlib import Path
from typing import List, Tuple
from typing import List, Tuple, Literal
import torch
import logging
import time
Expand Down Expand Up @@ -145,12 +145,26 @@ def test_protocol_command(microscope_id,detector_id,command, instance=None, inst
instance = SquareModel.objects.get(pk=instance)
microscope = Microscope.objects.get(pk=microscope_id)
detector = Detector.objects.get(pk=detector_id)
scopeInterface = select_microscope_interface(microscope)
scopeInterface, additional_settings = select_microscope_interface(microscope)
with scopeInterface(microscope = micModels.Microscope.model_validate(microscope),
detector= micModels.Detector.model_validate(detector),
atlas_settings= micModels.AtlasSettings.model_validate(detector)) as scope:
atlas_settings= micModels.AtlasSettings.model_validate(detector),
additional_settings=additional_settings) as scope:
PROTOCOL_COMMANDS_FACTORY[command](scope,params,instance)

def run_microscope_command(microscope_id, detector_id, command, *args):
from Smartscope.core.models import Microscope, Detector
from Smartscope.core.interfaces.microscope_methods import select_microscope_interface
import Smartscope.core.interfaces.microscope as micModels
microscope = Microscope.objects.get(pk=microscope_id)
detector = Detector.objects.get(pk=detector_id)
scopeInterface, additional_settings = select_microscope_interface(microscope)
with scopeInterface(microscope = micModels.Microscope.model_validate(microscope),
detector= micModels.Detector.model_validate(detector),
atlas_settings= micModels.AtlasSettings.model_validate(detector),
additional_settings=additional_settings) as scope:
getattr(scope, command)(*args)


def list_plugins():
from Smartscope.core.settings.worker import PLUGINS_FACTORY
Expand Down Expand Up @@ -197,14 +211,17 @@ def test_find_hole_geometry(grid_id):
print(f'Updated grid {grid} with rotation: {rotation} degrees and spacing: {spacing} pixels.')


def test_image_to_stage_conversion(image_file, coords):
def test_image_to_stage_conversion(image_file, coords, coordinate_system:Literal['smartscope','serialem']='smartscope'):
from pathlib import Path
from Smartscope.lib.image.montage import Montage
from Smartscope.lib.image.target import Target

montage_file = Path(image_file)
montage = Montage(name=montage_file.stem)
montage.raw = montage_file
montage.load_or_process()
coords = [int(i) for i in coords.split(',')]
if coordinate_system == 'serialem':
coords = Target.flip_y(coords, montage.shape_y)
target = Target(coords, from_center=True)
target.convert_image_coords_to_stage(montage, compare=True)
9 changes: 5 additions & 4 deletions Smartscope/lib/image/target.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ def set_area_radius(self, shape_type):
self.radius = min(len1, len2) / 2
# self.area = np.pi * (self.radius ** 2)

def flip_y(self, coords, shape_y):
@staticmethod
def flip_y(coords, shape_y):
flipped_coords= np.array([coords[0],shape_y - coords[1]])
logger.debug(f'Flipping y coords: {coords} to {flipped_coords}')
return flipped_coords
Expand All @@ -107,7 +108,7 @@ def convert_image_coords_to_stage(self, montage, force_legacy=False, compare=Fal
flipped_coords,
montage.metadata.iloc[tile].ImageToStageMatrix
)
logger.debug(f'\nUsed ImageToStageMatrix vectors {montage.metadata.iloc[tile].ImageToStageMatrix} to convert:\n\tY-flipped image coords: {flipped_coords} to\n\tStage coords: {self.stage_coords}')
logger.info(f'\nUsed ImageToStageMatrix vectors {montage.metadata.iloc[tile].ImageToStageMatrix} to convert:\n\tY-flipped image coords: {flipped_coords} to\n\tStage coords: {self.stage_coords}')
self.stage_z = montage.stage_z
if not compare:
return
Expand All @@ -119,10 +120,10 @@ def convert_image_coords_to_stage(self, montage, force_legacy=False, compare=Fal
montage.metadata.iloc[tile].TiltAngle,
return_vector=True
)
logger.debug(f'\nUsed mdoc-derived vector {vector.tolist()} to convert:\n\tImage coords: {self.coords} to\n\tStage coords: {self.stage_coords}')
logger.info(f'\nUsed mdoc-derived vector {vector.tolist()} to convert:\n\tImage coords: {self.coords} to\n\tStage coords: {self.stage_coords}')
self.stage_z = montage.stage_z
mdoc_to_stage = np.array([self.stage_x, self.stage_y])
if compare:
difference = is_to_stage - mdoc_to_stage
logger.debug(f'Difference between ImageToStageMatrix and mdoc-derived vectors: {difference} microns')
logger.info(f'Difference between ImageToStageMatrix and mdoc-derived vectors: {difference} microns')
return is_to_stage, mdoc_to_stage, difference