Updated documentation to subclass-based extensions
This commit is contained in:
parent
7a849c725a
commit
bacc111a87
10 changed files with 249 additions and 318 deletions
|
|
@ -7,52 +7,59 @@ from labthings.views import ActionView
|
|||
# Used in our timelapse function
|
||||
from openflexure_microscope.captures.capture_manager import generate_basename
|
||||
|
||||
## Extension methods
|
||||
|
||||
# Create the extension class
|
||||
class TimelapseExtension(BaseExtension):
|
||||
def __init__(self):
|
||||
# Superclass init function
|
||||
super().__init__("org.openflexure.timelapse-extension", version="0.0.0")
|
||||
|
||||
def timelapse(microscope, n_images, t_between):
|
||||
"""
|
||||
Save a set of images in a timelapse
|
||||
# Add our API views
|
||||
self.add_view(TimelapseAPIView, "/timelapse")
|
||||
|
||||
Args:
|
||||
microscope: Microscope object
|
||||
n_images (int): Number of images to take
|
||||
t_between (int/float): Time, in seconds, between sequential captures
|
||||
"""
|
||||
base_file_name = generate_basename()
|
||||
folder = "TIMELAPSE_{}".format(base_file_name)
|
||||
def timelapse(self, microscope, n_images, t_between):
|
||||
"""
|
||||
Save a set of images in a timelapse
|
||||
|
||||
# Take exclusive control over both the camera and stage
|
||||
with microscope.camera.lock, microscope.stage.lock:
|
||||
for n in range(n_images):
|
||||
# Elegantly handle action cancellation
|
||||
if current_action() and current_action().stopped:
|
||||
return
|
||||
# Generate a filename
|
||||
filename = f"{base_file_name}_image{n}"
|
||||
# Create a file to save the image to
|
||||
output = microscope.camera.new_image(
|
||||
filename=filename, folder=folder, temporary=False
|
||||
)
|
||||
Args:
|
||||
microscope: Microscope object
|
||||
n_images (int): Number of images to take
|
||||
t_between (int/float): Time, in seconds, between sequential captures
|
||||
"""
|
||||
base_file_name = generate_basename()
|
||||
folder = "TIMELAPSE_{}".format(base_file_name)
|
||||
|
||||
# Capture
|
||||
microscope.camera.capture(output)
|
||||
# Take exclusive control over both the camera and stage
|
||||
with microscope.camera.lock, microscope.stage.lock:
|
||||
for n in range(n_images):
|
||||
# Elegantly handle action cancellation
|
||||
if current_action() and current_action().stopped:
|
||||
return
|
||||
# Generate a filename
|
||||
filename = f"{base_file_name}_image{n}"
|
||||
# Create a file to save the image to
|
||||
output = microscope.camera.new_image(
|
||||
filename=filename, folder=folder, temporary=False
|
||||
)
|
||||
|
||||
# Add system metadata
|
||||
output.put_metadata(microscope.metadata, system=True)
|
||||
# Capture
|
||||
microscope.camera.capture(output)
|
||||
|
||||
# Update task progress (only does anyting if the function is running in a LabThings task)
|
||||
progress_pct = ((n + 1) / n_images) * 100 # Progress, in percent
|
||||
update_action_progress(progress_pct)
|
||||
# Add system metadata
|
||||
output.put_metadata(microscope.metadata, system=True)
|
||||
|
||||
# Wait for the specified time
|
||||
time.sleep(t_between)
|
||||
# Update task progress (only does anyting if the function is running in a LabThings task)
|
||||
progress_pct = ((n + 1) / n_images) * 100 # Progress, in percent
|
||||
update_action_progress(progress_pct)
|
||||
|
||||
# Wait for the specified time
|
||||
time.sleep(t_between)
|
||||
|
||||
|
||||
## Extension views
|
||||
|
||||
|
||||
class TimelapseAPI(ActionView):
|
||||
class TimelapseAPIView(ActionView):
|
||||
"""
|
||||
Take a series of images in a timelapse
|
||||
"""
|
||||
|
|
@ -71,16 +78,9 @@ class TimelapseAPI(ActionView):
|
|||
microscope = find_component("org.openflexure.microscope")
|
||||
|
||||
# Start "timelapse"
|
||||
return timelapse(microscope, args.get("n_images"), args.get("t_between"))
|
||||
return self.extension.timelapse(
|
||||
microscope, args.get("n_images"), args.get("t_between")
|
||||
)
|
||||
|
||||
|
||||
## Create extension
|
||||
|
||||
# Create your extension object
|
||||
my_extension = BaseExtension("com.myname.timelapse-extension", version="0.0.0")
|
||||
|
||||
# Add methods to your extension
|
||||
my_extension.add_method(timelapse, "timelapse")
|
||||
|
||||
# Add API views to your extension
|
||||
my_extension.add_view(TimelapseAPI, "/timelapse")
|
||||
LABTHINGS_EXTENSIONS = (TimelapseExtension,)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue