Skip to content

Instantly share code, notes, and snippets.

@will-moore
Created November 16, 2017 22:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save will-moore/370e9665b8d35fd15694315e85389272 to your computer and use it in GitHub Desktop.
Save will-moore/370e9665b8d35fd15694315e85389272 to your computer and use it in GitHub Desktop.
An OMERO.script for adding metadata to images in OMERO
# See https://www.openmicroscopy.org/community/viewtopic.php?f=4&t=8397#p18758
import omero.scripts as scripts
import omero
from omero.gateway import BlitzGateway
from omero.model import InstrumentI, MicroscopeI, ObjectiveSettingsI, ObjectiveI
from omero.rtypes import rstring, rdouble, rlong
def add_metadata(conn, script_params):
update_service = conn.getUpdateService()
types_service = conn.getTypesService()
for image in conn.getObjects("Image", script_params['IDs']):
microscope = MicroscopeI()
# optional parameters - check if they've been set...
if 'Microscope_Manufacturer' in script_params:
microscope.manufacturer = rstring(script_params['Microscope_Manufacturer'])
if 'Microscope_Model' in script_params:
microscope.model = rstring(script_params['Microscope_Model'])
# Type is not optional param
# Lookup the enumeration we need
t = types_service.getEnumeration('MicroscopeType', script_params['Microscope_Type'])
microscope.type = t
instrument = InstrumentI()
instrument.microscope = microscope
# Objective and Objective Settings.
objective = ObjectiveI()
if 'Nominal_Magnification' in script_params:
objective.nominalMagnification = rdouble(script_params['Nominal_Magnification'])
# immersion and correction cannot be null
i = types_service.getEnumeration('Immersion', script_params['Objective_Immersion'])
objective.immersion = i
c = types_service.getEnumeration('Correction', script_params['Objective_Correction'])
objective.correction = c
objective.instrument = instrument
obj_settings = ObjectiveSettingsI()
obj_settings.objective = objective
# Update the omero.model.ImageI object from the image wrapper
img = image._obj
img.instrument = instrument
img.objectiveSettings = obj_settings
update_service.saveObject(img)
def run_script():
"""
The main entry point of the script, as called by the client via the
scripting service, passing the required parameters.
"""
data_types = [rstring('Image')]
microscope_types = [rstring('Upright'), rstring('Inverted'),
rstring('Dissection'), rstring('Electrophysiology'), rstring('Other')]
immersion_enums = [rstring('Oil'), rstring('Water'), rstring('WaterDipping'),
rstring('Air'), rstring('Multi'), rstring('Glycerol'),
rstring('Other'), rstring('Unknown')]
# not all correction times listed
correction_enums = [rstring('UV'), rstring('PlanApo'), rstring('PlanFluor'),
rstring('Other'), rstring('Unknown')]
client = scripts.client(
'Add_Metadata.py',
"""Add Acquisition Metadata to Images""",
# provide 'Data_Type' and 'IDs' parameters so that webclient/Insight
# auto-populates with currently selected images.
scripts.String(
"Data_Type", optional=False, grouping="01",
description="The data you want to work with.", values=data_types,
default="Image"),
scripts.List(
"IDs", optional=False, grouping="02",
description="List of Image IDs").ofType(rlong(0)),
# Optional microscope attributes
scripts.String(
"Microscope_Model", grouping="3",
description="Microscope Model"),
scripts.String(
"Microscope_Manufacturer", grouping="3.1",
description="Microscope Manufacturer"),
# Non-optional microscope type
scripts.String(
"Microscope_Type", optional=False, grouping="3.2",
values=microscope_types, description="Microscope Model"),
# Objective
scripts.String(
"Objective_Immersion", grouping="5", optional=False,
description="Objective Immersion", values=immersion_enums),
# Use groupings to indent param in script UI
scripts.Int("Nominal_Magnification", grouping="5.1"),
scripts.String("Objective_Correction", grouping="5.2",
optional=False, values=correction_enums),
)
try:
conn = BlitzGateway(client_obj=client)
script_params = client.getInputs(unwrap=True)
print script_params
# call the main script
add_metadata(conn, script_params)
# Return message to the client
client.setOutput("Message", rstring("Metadata added"))
finally:
client.closeSession()
if __name__ == "__main__":
run_script()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment