We're Hiring!

OMERO.scripts: Can only getObjects() from default group

General and open developer discussion about using OMERO APIs from C++, Java, Python, Matlab and more! Please new questions at https://forum.image.sc/tags/omero
Please note:
Historical discussions about OMERO. Please look for and ask new questions at https://forum.image.sc/tags/omero

If you are having trouble with custom code, please provide a link to a public repository, ideally GitHub.

OMERO.scripts: Can only getObjects() from default group

Postby a.herbert » Wed Nov 21, 2012 2:39 pm

I have a few OMERO.scripts on the server that run using the general guidelines here:

http://trac.openmicroscopy.org.uk/ome/wiki/OmeroScripts

E.g.

Code: Select all
client = script.client("SCRIPTNAME", "SCRIPTDESCRIPTION",
         script.TYPE("VARIABLENAME").[in()|out()|inout()], ...)
conn = BlitzGateway(client_obj=client)


The BlitzGateway connection is then used to perform work such as retrieving images.

The scripts worked fine on the pre 4.4 versions of OMERO since the Insight client had to be viewing images within one group. Switching groups in Insight refreshed all the available images and, I presume, updated the group security context when running scripts.

Within OMERO 4.4 it is now possible to view images from more than one group. So you can launch the script to run on images that are not part of the user's default/current group. The framework passes in the Image or Dataset IDs but calls to getObject/getObjects fail to return any items since the connection context is using the default group. So the following calls return nothing for other groups:

Code: Select all
ds = conn.getObject('Dataset', ...)
objects = conn.getObjects("Image", ...)


I cannot find a method within the BlitzGateway that will retrieve any objects that the user owns. It is tied to the current security context and so only works for the current group.

A workaround would be to iterate all the user's groups using the getOtherGroups() method, set the connection to the group and search for the object:

Code: Select all
id = long(1)
for g in conn.getOtherGroups():
    conn.setGroupForSession(g.getId())
    o = conn.getObject('Image', id)
    if o:
        break


Is there a better way to approach this? For example should the Insight client pass more information through to the scripts framework containing the group associated with the image/dataset IDs. No change will then need to be made to scripts that retrieve objects since the group context will be correct for the input IDs.

Thanks,

Alex
a.herbert
 
Posts: 53
Joined: Tue Jan 11, 2011 1:35 pm

Re: OMERO.scripts: Can only getObjects() from default group

Postby wmoore » Wed Nov 21, 2012 3:26 pm

Hi Alex,

Are you running your scripts from Insight? If so, Insight will attempt to set the correct context. E.g. if you choose an Image(s) and run script (script dialog auto-populates the IDs etc) then the client object you get in the script should be able to access the chosen image. This should allow pre-4.4 scripts to work without modification. (Let us know if you think this is not working for you).
However, if you are manually entering image IDs etc, or running scripts manually (not from Insight / webclient) then your scripts will have to be able to query & work across groups:

If you look at http://www.openmicroscopy.org/site/supp ... ython.html and find the example with:

Code: Select all
conn.SERVICE_OPTS.setOmeroGroup('-1')


If you include this line, then all subsequent use of "conn" should work across all groups, for querying etc.
However, if you want to save anything (E.g. create an annotation link etc) then you will want to set the group to match the group of the objects you are working with:

image = conn.getObject("Image", iid)
groupId = image.details.group.id.val
conn.SERVICE_OPTS.setOmeroGroup(groupId)

The "conn" wrapper should use the OmeroGroup for all of it's interactions with the server. If you have written your own queries manually, you will need to pass in the SERVICE OPTS to ensure the correct group is used.
You can find lots of examples of this in https://github.com/openmicroscopy/openm ... _init__.py (search for SERVICE_OPTS).

Hope that helps,

Will.
User avatar
wmoore
Team Member
 
Posts: 674
Joined: Mon May 18, 2009 12:46 pm

Re: OMERO.scripts: Can only getObjects() from default group

Postby a.herbert » Fri Nov 23, 2012 12:58 pm

Hi Will,

You are right. I was feeding image IDs to a script that creates its own connection. The connection was associated to the default group.

I have updated the script to correctly set the group ID as suggested. The script now works when trying to extract images from their IDs.

However I get an error when I try to create a new image in a non-default group. Here is an example script where the image I am extracting is not in the user's default group:

Code: Select all
#!/usr/bin/python
import omero
from omero.gateway import BlitzGateway
from omero.rtypes import *

conn = BlitzGateway('user', 'password', host='localhost')
conn.connect()

conn.SERVICE_OPTS.setOmeroGroup(-1)

imageId = 4
img = conn.getObject('Image', imageId)

zctTileList = []
for z in range(img.getSizeZ()):
    for c in range(img.getSizeC()):
        for t in range(img.getSizeT()):
            zctTileList.append((z, c, t))

pixels = img.getPrimaryPixels()

def tile_gen():
    for plane in pixels.getPlanes(zctTileList):
        yield plane

groupId = img.details.group.id.val
conn.SERVICE_OPTS.setOmeroGroup(groupId)
newImg = conn.createImageFromNumpySeq(
    tile_gen(), '%s New' % img.getName(), img.getSizeZ(), img.getSizeC(),
    img.getSizeT(), dataset=img.getDataset())


This gives the following error:

Code: Select all
ERROR:omero.gateway:Failed to setPlane() on rawPixelsStore while creating Image
Traceback (most recent call last):
  File "/usr/local/omero_dist/lib/python/omero/gateway/__init__.py", line 2755, in createImageFromNumpySeq
    rawPixelsStore.setPixelsId(pixelsId, True, self.SERVICE_OPTS)
  File "/usr/local/omero_dist/lib/python/omero_api_RawPixelsStore_ice.py", line 110, in setPixelsId
    return _M_omero.api.RawPixelsStore._op_setPixelsId.invoke(self, ((pixelsId, bypassOriginalFile), _ctx))
ValidationException: exception ::omero::ValidationException
{
    serverStackTrace = ome.conditions.ValidationException: Cannot read pixels id=163


If I look in Insight I can see the image has been created using the Images tab. But it doesn't have any pixels. It is an unfinished orphaned image. I tried it without the dataset parameter to createImageFromNumpySeq() and it still fails.

Commenting out the 'conn.SERVICE_OPTS.setOmeroGroup(groupId)' line gives a different error:

Code: Select all
WARNING:omero.gateway:SecurityViolation on <class 'omero.gateway.OmeroGatewaySafeCallWrapper'> to <200f2c2c-e5e1-469e-98f7-df9a22f280eeomero.api.IUpdate> saveObject((object #0 (::omero::model::DatasetImageLink)


So the attempt to change to the correct group allows the code to run a bit further.

Note that running the same script with an image in the user's default group works fine.

Any suggestions?

Alex
a.herbert
 
Posts: 53
Joined: Tue Jan 11, 2011 1:35 pm

Re: OMERO.scripts: Can only getObjects() from default group

Postby wmoore » Sat Nov 24, 2012 10:58 pm

Hi Alex,

Just looking at the code in conn.createImageFromNumpySeq() - There are a number of places there that we fail to include conn.SERVICE_OPTS in calls to the OMERO server.

Using your script as a test, I've just gone through that code and added self.SERVICE_OPTS to all the OMERO server API calls. You can see the result at http://gist.github.com/4141708

This now works for me - a new image is created in the same dataset as the source image (in a non-default group). I'll create a pull request to get this fix in the next point release of OMERO.

I guess you have several options:
- You could use that code directly in your script, using 'conn' in place of 'self'.
- You could replace that code in your Blitz gateway /lib/python/omero/gateway/__init__.py (but since I haven't tested this anywhere except with your test, this might be a bit premature).
- You could add that code into a NEW method in your Blitz gateway, E.g. conn.createImageFIXED() and then call that from your script. Once we've tested my changes a bit more thoroughly, you'd be safe to update the original method.

Apologies for the bug,
Hope that helps,

Will.
User avatar
wmoore
Team Member
 
Posts: 674
Joined: Mon May 18, 2009 12:46 pm

Re: OMERO.scripts: Can only getObjects() from default group

Postby wmoore » Sat Nov 24, 2012 11:17 pm

User avatar
wmoore
Team Member
 
Posts: 674
Joined: Mon May 18, 2009 12:46 pm

Re: OMERO.scripts: Can only getObjects() from default group

Postby a.herbert » Mon Nov 26, 2012 1:08 pm

Thanks Will.

Most of our users are in one group so it does not effect them. I'll try your fix on my test server and roll it out if someone complains about broken functionality.

Alex
a.herbert
 
Posts: 53
Joined: Tue Jan 11, 2011 1:35 pm


Return to Developer Discussion

Who is online

Users browsing this forum: Google [Bot] and 1 guest