ome / omero-py

Python project containing Ice remoting code for OMERO
https://www.openmicroscopy.org/omero
GNU General Public License v2.0
20 stars 33 forks source link

get objects by map annotations #285

Closed will-moore closed 3 years ago

will-moore commented 3 years ago

See request for "How to query objects by map annotation?" We don't have any examples at https://docs.openmicroscopy.org/omero/5.6.3/developers/Python.html and need to look into Mapr source to find out how. https://forum.image.sc/t/harmonization-of-image-metadata-for-different-file-formats-omero-mde/50827/10

This adds method to gateway:

conn.getObjectsByMapAnnotations('Image', key='Primary Antibody')

conn.getObjectsByMapAnnotations('Image', value='ACA', ns="openmicroscopy.org/omero/client/mapAnnotation")

conn.getObjectsByMapAnnotations('Image', key='Primary Antibody', value='ACA')

To consider:

TODO:

pwalczysko commented 3 years ago

used folllowing workflow to set up the testing env:

  1. create a new conda env as per https://docs.openmicroscopy.org/omero/5.6.3/users/cli/installation.html
  2. run following:
    pip uninstall omero-py
    pip install git+https://github.com/will-moore/omero-py.git@get_objects_by_map_annotations

Use a script which contains lines from the description of this PR

pwalczysko commented 3 years ago

What is the way to sanitize one's script against the case that no images with specified KVPs were found ? The "generator" object which is returned by the methods added here has not a length zero even if turned into a list and checked for length ?

Constantly crashing on things like

<generator object _BlitzGateway.getObjects at 0x10ec0deb8>
WARNING:omero.gateway:ApiUsageException on <class 'omero.gateway.OmeroGatewaySafeCallWrapper'> to <dd32cf4a-c600-4149-80b6-f7ad7bd1aa64omero.api.IQuery> findAllByQuery(('select obj from Image obj join fetch obj.details.owner as owner join fetch obj.details.creationEvent where obj.id in (:ids)', object #0 (::omero::sys::Parameters)
{
    map = 
    {
        key = ids
        value = object #1 (::omero::RList)
        {
            _val = 
            {
            }
        }
    }
    theFilter = <nil>
    theOptions = <nil>
}, <ServiceOptsDict: {'omero.client.uuid': 'dd32cf4a-c600-4149-80b6-f7ad7bd1aa64', 'omero.session.uuid': '3d72dd68-4075-48c4-9d6d-f370c6f61e17'}>), {})
Traceback (most recent call last):
  File "/Users/pwalczysko/miniconda3/envs/env563/lib/python3.6/site-packages/omero/gateway/__init__.py", line 4832, in __call__
    return self.f(*args, **kwargs)
  File "/Users/pwalczysko/miniconda3/envs/env563/lib/python3.6/site-packages/omero_api_IQuery_ice.py", line 710, in findAllByQuery
    return _M_omero.api.IQuery._op_findAllByQuery.invoke(self, ((query, params), _ctx))
omero.ApiUsageException: exception ::omero::ApiUsageException
{
    serverStackTrace = ome.conditions.ApiUsageException: unexpected end of subtree [select obj from ome.model.core.Image obj join fetch obj.details.owner as owner join fetch obj.details.creationEvent where obj.id in ()]; nested exception is org.hibernate.hql.ast.QuerySyntaxException: unexpected end of subtree [select obj from ome.model.core.Image obj join fetch obj.details.owner as owner join fetch obj.details.creationEvent where obj.id in ()]

The bit of code which is crashing is not the new method itself, but the following code which is trying to consume the "generator" object created by the method...

...
result2 = conn.getObjectsByMapAnnotations('Image', key='Primar Antibody')
    print ("result2", "\n")
    print(result2)
    if len(list(result2)) > 0:
        for i in result2:
            print(i.name, "\n", i.id)
...

The bit inside the for loop above is crashing, but only when no images are found. When some images are found, then the script ends with a success.

pwalczysko commented 3 years ago

Just tried to read the retrieved Images (objects) which were returned by the methods added in this PR just as specified in https://docs.openmicroscopy.org/omero/5.6.3/developers/Python.html#read-data

But, again, when no images are found, the same error as in https://github.com/ome/omero-py/pull/285#issuecomment-814987008 happened. Maybe something is really wrong with the Image objects (generators) returned by the new methods here ?

pwalczysko commented 3 years ago

Tested all three options in the header of this PR. All works, including the cases where no images are retrieved. Also tried to add the KVP on a Project, worked too. All good fmpov.

joshmoore commented 3 years ago

In general, probably a good example of something that needs to be moved server-side (or into the eventual JSON API).

:+1: