A colleague needed a table listing image ID, image filename, and number or ROIs for all images in a project. Here’s the python script I came up with:
Query Metadata from omero
Imports
import omero
from omero.gateway import BlitzGateway
import pandas as pd
import getpass
Connect to omero
conn = BlitzGateway('XXXXX', getpass.getpass(), host='omero.server.org')
conn.connect()
········
True
Retrieve datasets
This sets up an iterator over the needed datasets but does not load any data into memory (yet).
datasets = conn.getObjects(obj_type='Dataset', ids=[1,2,3.4])
Loop over datasets and store requested information in a list of tuples.
records = []
for dataset in datasets:
id = dataset.id
imgs = conn.getObjects("Image", opts={'dataset':id})
for img in imgs:
records.append((id, img.id, img.getName(), img.getROICount() ))
Write records into a DataFrame
df = pd.DataFrame.from_records(records, columns=['dataset id', 'image id', 'image filename', 'roi count'])
Write data as csv file.
df.to_csv('ds_id_fname_Nroi.csv', index=False)
Create file annotation object and attach to the omero project that encapsulates the datasets.
file_annotation = conn.createFileAnnfromLocalFile('ds_id_fname_Nroi.csv', mimetype="text/plain", desc="Table of images , their dataset id, image id, filename, and ROI count.")
project.linkAnnotation(file_annotation)
Running this produces a csv file on disk and attaches this file as a file annotation on the omero project.