We're Hiring!

uploading data from matlab

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.

uploading data from matlab

Postby achessel » Fri May 20, 2011 9:20 am

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...
achessel
 
Posts: 67
Joined: Fri Jan 14, 2011 1:58 pm

Re: uploading data from matlab

Postby wmoore » Fri May 20, 2011 9:50 am

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.
User avatar
wmoore
Team Member
 
Posts: 674
Joined: Mon May 18, 2009 12:46 pm

Re: uploading data from matlab

Postby achessel » Fri May 20, 2011 4:19 pm

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))))
achessel
 
Posts: 67
Joined: Fri Jan 14, 2011 1:58 pm

Re: uploading data from matlab

Postby wmoore » Fri May 20, 2011 8:41 pm

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()
User avatar
wmoore
Team Member
 
Posts: 674
Joined: Mon May 18, 2009 12:46 pm

Re: uploading data from matlab

Postby achessel » Mon May 23, 2011 2:15 pm

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
achessel
 
Posts: 67
Joined: Fri Jan 14, 2011 1:58 pm

Re: uploading data from matlab

Postby wmoore » Tue May 24, 2011 8:40 am

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.
User avatar
wmoore
Team Member
 
Posts: 674
Joined: Mon May 18, 2009 12:46 pm

Re: uploading data from matlab

Postby achessel » Tue May 24, 2011 9:26 am

that did work, many thanks...
achessel
 
Posts: 67
Joined: Fri Jan 14, 2011 1:58 pm


Return to Developer Discussion

Who is online

Users browsing this forum: No registered users and 0 guests