From f2588a506e63a963e30f85a5897e1b4a64382ecf Mon Sep 17 00:00:00 2001 From: jtc42 Date: Fri, 7 Feb 2020 17:05:30 +0000 Subject: [PATCH] Add more complete Capture object schemas --- .../api/v2/views/captures.py | 24 ++++++++++++++++++- openflexure_microscope/camera/capture.py | 16 +++++++------ 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/openflexure_microscope/api/v2/views/captures.py b/openflexure_microscope/api/v2/views/captures.py index 097ff665..f52a7ed0 100644 --- a/openflexure_microscope/api/v2/views/captures.py +++ b/openflexure_microscope/api/v2/views/captures.py @@ -14,6 +14,28 @@ from labthings.server.find import find_component from marshmallow import pre_dump +class InstrumentSchema(Schema): + id = fields.UUID() + configuration = fields.Dict() + settings = fields.Dict() + state = fields.Dict() + +class CaptureMetadataImageSchema(Schema): + id = fields.UUID() + acquisitionDate = fields.String(format="date") + format = fields.String() + name = fields.String() + tags = fields.List(fields.String()) + annotations = fields.Dict() + + +class CaptureMetadataSchema(Schema): + experimenter = fields.Dict() # TODO: Make schema + experimenterGroup = fields.Dict() # TODO: Make schema + dataset = fields.Dict() # TODO: Make schema + image = fields.Nested(CaptureMetadataImageSchema()) + instrument = fields.Nested(InstrumentSchema()) + class CaptureSchema(Schema): id = fields.String() file = fields.String( @@ -21,7 +43,7 @@ class CaptureSchema(Schema): ) exists = fields.Bool(data_key="available") name = fields.String() - metadata = fields.Dict() + metadata = fields.Nested(CaptureMetadataSchema()) links = fields.Dict() diff --git a/openflexure_microscope/camera/capture.py b/openflexure_microscope/camera/capture.py index 81edff1a..0f190c58 100644 --- a/openflexure_microscope/camera/capture.py +++ b/openflexure_microscope/camera/capture.py @@ -4,7 +4,6 @@ import os import shutil import glob import datetime -import yaml import json import logging from PIL import Image @@ -35,11 +34,9 @@ def pull_usercomment_dict(filepath): try: return json.loads(exif_dict["Exif"][37510].decode()) except json.decoder.JSONDecodeError: - # TODO: Remove YAML support in a later version - logging.warning( - f"Capture {filepath} has metadata stored in YAML format. This is now deprecated in favour of JSON." + logging.error( + f"Capture {filepath} has old, corrupt, or missing OpenFlexure metadata. Unable to reload to server." ) - return yaml.load(exif_dict["Exif"][37510].decode()) else: return None @@ -68,7 +65,8 @@ def build_captures_from_exif(capture_path): exif = pull_usercomment_dict(f) if exif: capture = capture_from_exif(f, exif) - captures.append(capture) + if capture: + captures.append(capture) else: logging.error("Invalid data at {}. Skipping.".format(f)) @@ -95,7 +93,11 @@ def capture_from_exif(path, exif_dict): capture.split_file_path(capture.file) # Image metadata - image_metadata = exif_dict.pop("image") + try: + image_metadata = exif_dict.pop("image") + except KeyError as e: + logging.error(f"Unable to obtain OpenFlexure metadata from file {path}") + return None # Populate capture parameters capture.id = image_metadata.get("id")