Page 1 of 1

uploading data from matlab

PostPosted: Fri May 20, 2011 9:20 am
by achessel
Hi all...

I am trying to import images from matlab inside a ScreenPlateWell hierarchy but having trouble doing so... The only function I found to upload actual pixel info (ie in matlab a matrix of value of whatever type) is uploadPlane (in the Gateway for example, or others seemingly equivalent in RawPixelsStore). Is that true, or is there a higher level function?

When I try to use it on a newly created pixels objects, it complains that Pixels.dimensionOrder is not set. So I am sure those info are somewhere in the doc, but I somehow was unable to find them: What are the acceptable values for those not quite self-explaining fields? (Pixels.dimensionOrder, but also model::PixelsType in createImage, etc...) For the various model objects, what fields are mandatory? What are the constructors, as they do not seems to be on the slice2html doc?

Finally if you had a code snippet for creating a valid image with valid pixels set to given matlab matrices (or the equivalent in java or python as it should be quite similar) I would be very grateful indeed...

Many thanks...

Re: uploading data from matlab

PostPosted: Fri May 20, 2011 9:50 am
by wmoore
Hi,

I can point you at some Python code that creates a new Image in OMERO from a sequence of 2D arrays (numpy). http://trac.openmicroscopy.org.uk/ome/b ... _.py#L2395

The basic sequence is:
* Lookup the appropriate PixelsType, depending on the type of data you have:

Code: Select all
pixelsType = queryService.findByQuery("from PixelsType as p where p.value='%s'" % pType, None)


* Use the PixelsService to create a new image of the correct dimensions:
Code: Select all
iId = pixelsService.createImage(sizeX, sizeY, sizeZ, sizeT, channelList, pixelsType, imageName, description)


* Then you have to get the PixelsId from that image, to initialise the rawPixelsStore. I use the containerService to give me the Image with pixels loaded:
Code: Select all
image = containerService.getImages("Image", [imageId], None)[0]
pixelsId = image.getPrimaryPixels().getId().getValue()
rawPixelsStore.setPixelsId(pixelsId, True)


* Then you can upload the data using setPlane:
Code: Select all
rawPixelsStore.setPlane(plane, z, c, t)


The only bit that may be significantly different for MATLAB is converting the plane from whatever data structure you have (in this case a 2D numpy array) into a suitable byte-stream for setPlane.

Hope that helps,

Will.

Re: uploading data from matlab

PostPosted: Fri May 20, 2011 4:19 pm
by achessel
That was wery helpful, thanks. The 'suitable byte-stream for setPlane' bit was less straitforward until I found the had hoc java helper function though... A quick question while I am here: how to set the default rendering color (for the first channel to be red, the second blue the third green for example)?

And I may as well put the result here, it might help others:

Code: Select all
function imageId=mat2omeroImage(factory, redPlane,greenPlane,bluePlane, imageName, description)

queryService = factory.getQueryService();
pixelsService = factory.getPixelsService();
rawPixelsStore = factory.createRawPixelsStore();
containerService = factory.getContainerService();

% Lookup the appropriate PixelsType, depending on the type of data you have:
p = omero.sys.ParametersI();
p.add('type',rstring('double'));      

q=['from PixelsType as p where p.value= :type'];
pixelsType = queryService.findByQuery(q,p);

% Use the PixelsService to create a new image of the correct dimensions:
%iId = pixelsService.createImage(rint(size(redPlane,2)), rint(size(redPlane,1)), rint(1), rint(1), toJavaList([rint(1) rint(2) rint(3)]), pixelsType, rstring('test'), rstring([]))
iId = pixelsService.createImage(uint32(size(redPlane,2)), uint32(size(redPlane,1)), uint32(1), uint32(1), toJavaList([uint32(1) uint32(2) uint32(3)]), pixelsType,char(imageName), char(description));
imageId = iId.getValue();

% Then you have to get the PixelsId from that image, to initialise the rawPixelsStore. I use the containerService to give me the Image with pixels loaded:
image = containerService.getImages('Image',  toJavaList(uint64(imageId)),[]).get(0);
pixels = image.getPrimaryPixels();
pixelsId = pixels.getId().getValue();
rawPixelsStore.setPixelsId(pixelsId, true)

% Then you can upload the data using setPlane:
bytear=omerojava.util.GatewayUtils.convertClientToServer(pixels, redPlane') ;
rawPixelsStore.setPlane(bytear, int32(0),int32(0),int32(0))
pixelsService.setChannelGlobalMinMax(pixelsId, 0, double(min(min(redPlane))), double(max(max(redPlane))))

bytear=omerojava.util.GatewayUtils.convertClientToServer(pixels, greenPlane') ;
rawPixelsStore.setPlane(bytear, int32(0),int32(1),int32(0))
pixelsService.setChannelGlobalMinMax(pixelsId, 1, double(min(min(greenPlane))), double(max(max(greenPlane))))

bytear=omerojava.util.GatewayUtils.convertClientToServer(pixels, bluePlane') ;
rawPixelsStore.setPlane(bytear, int32(0),int32(2),int32(0))
pixelsService.setChannelGlobalMinMax(pixelsId, 2, double(min(min(bluePlane))), double(max(max(bluePlane))))

Re: uploading data from matlab

PostPosted: Fri May 20, 2011 8:41 pm
by wmoore
Hi,

Glad things are working for you so far.
This should help with the color settings

Code: Select all
renderingEngine.lookupPixels(pixelsId)
if not renderingEngine.lookupRenderingDef(pixelsId):
    renderingEngine.resetDefaults() 
renderingEngine.load()
# optional setting of rendering 'window' (levels)
renderingEngine.setChannelWindow(cIndex, float(minValue), float(maxValue))
red = 0
green = 255
blue = 0
alpha = 255
renderingEngine.setRGBA(cIndex, red, green, blue, alpha)
renderingEngine.saveCurrentSettings()

Re: uploading data from matlab

PostPosted: Mon May 23, 2011 2:15 pm
by achessel
Hum, it seems there is a bit missing I can't figure out... As it is, it says
Code: Select all
    serverExceptionClass = "ome.conditions.InternalException"
    message = " Wrapped Exception: (java.lang.IllegalStateException):
               Object unloaded:ome.model.display.RenderingDef:Id_13042"


The rest on the code is as above; I tried some stuff, but...

Many thanks

Re: uploading data from matlab

PostPosted: Tue May 24, 2011 8:40 am
by wmoore
Hi,

OK, I think I have an idea of the problem.
After the renderingEngine.resetDefaults()
you need to try lookupRenderingDef() again, which should work this time.
Code: Select all
    renderingEngine.lookupPixels(pixelsId)
    if not renderingEngine.lookupRenderingDef(pixelsId):
        renderingEngine.resetDefaults() 
    if not renderingEngine.lookupRenderingDef(pixelsId):
        raise "Still No Rendering Def"
    renderingEngine.load()


One more thing: Don't forget to call close() on any stateful services (rawPixelsStore and renderingEngine) when you have finished with them, to free up resources on the server.

Will.

Re: uploading data from matlab

PostPosted: Tue May 24, 2011 9:26 am
by achessel
that did work, many thanks...