We're Hiring!

Extracting OME XML metadata

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.

Extracting OME XML metadata

Postby a.herbert » Thu Jan 12, 2012 11:26 am

Dear All,

I would like to extract the OME XML metadata for my images using python. I know the image contains the information I am after since when I export it as OME TIFF it contains the following metadata:

Code: Select all
<Plane DeltaT="0.0" ExposureTime="5.000000237487257E-4" PositionX="-40.36" PositionY="63.14" PositionZ="-2.195" TheC="0" TheT="0" TheZ="0"/>


The item that I am interested in is the ExposureTime attribute. I can obtain this through the gateway if I extract the annotations. One of them is 'original_metadata.txt' and will contain a set of key-values pairs for attributes including:

Code: Select all
Image 1. Exposure time=0.500 secs


I can also extract the original log file for the DeltaVision image file and get the exposure time from that.

I would prefer to use the 'original_metadata.txt' as it is easier to parse. However there appears to be a bug in the ImageWrapper.loadOriginalMetadata() function within the API since I do not get any values returned for my image. Reading the source code for this function it loops over the annotations list and for each one checks that it is a file annotation and isOriginalMetadata() is true. However for my image this is not the case. Below I show some python code using the gateway to get the data:

Code: Select all
>>> img = conn.getObject('Image', 2228)
>>> meta = img.loadOriginalMetadata()
>>> print meta
None
>>> for a in img.listAnnotations():
...     print "%s (%s) = %s" % (a.getFileName(), a._obj.ns.val, a.isOriginalMetadata())
...
original_metadata.txt (openmicroscopy.org/omero/import/companionFile) = False
E05_111220_EH3_p184_55_R3D.dv.log (openmicroscopy.org/omero/import/companionFile) = False


I am not sure why the wrapper for original_metadata.txt is not being picked up as the metadata. Is this a genuine bug or am I missing something when creating my wrapper objects (e.g. loading states)?

Within FileAnnotationWrapper.isOriginalMetadata(self) should the following code be changed:

Code: Select all
if self._obj.ns is not None and \
        self._obj.ns.val == omero.constants.namespaces.NSCOMPANIONFILE and \
        self._obj.file.name.val.startswith("original_metadata"):
    return True


to

Code: Select all
if self._obj.ns is not None and \
        self._obj.ns.val == omero.constants.namespaces.NSCOMPANIONFILE and \
        self.getFileName().startswith("original_metadata"):
    return True


At the moment it looks like I will have to manually loop over the annotations myself and identify the metadata file using the name.

Thanking you in advance.

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

Re: Extracting OME XML metadata

Postby a.herbert » Thu Jan 12, 2012 11:47 am

Update:

I have changed the code in omero_dist/lib/python/omero/gateway/__init__.py as suggested in my previous post to:

Code: Select all
    def isOriginalMetadata(self):
        """
        Checks if this file annotation is an 'original_metadata' file
       
        @return:    True if namespace and file name follow metadata convention
        @rtype:     Boolean
        """
       
        try:
            #if self._obj.ns is not None and self._obj.ns.val == omero.constants.namespaces.NSCOMPANIONFILE and self._obj.file.name.val.startswith("original_metadata"):
            if self._obj.ns is not None and self._obj.ns.val == omero.constants.namespaces.NSCOMPANIONFILE and self.getFileName().startswith("original_metadata"):
                return True
        except:
            logger.info(traceback.format_exc())
        return False


The call to ImageWrapper.loadOriginalMetadata() now returns the metadata.

Regards,

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

Re: Extracting OME XML metadata

Postby wmoore » Thu Jan 12, 2012 12:01 pm

Thanks Alex,

This bug has been noted and fixed in the development version of OMERO.

Mentioned in these forums (with workarounds):
http://www.openmicroscopy.org/community ... 3333#p3333
http://www.openmicroscopy.org/community ... f=16&t=920

Apologies for the hassle.

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

Re: Extracting OME XML metadata

Postby a.herbert » Thu Jan 12, 2012 2:35 pm

Hi Will,

Thanks for the info.

I started this thread because the forum search returned no results. Instead it produced this message:

Code: Select all
The following words in your search query were ignored because they are too common words: metadata


I get this for most of my searches using the forum search engine. Is there a problem with the forum search? Next time I'll use Google instead. That would have saved me some effort.

Regards,

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

Re: Extracting OME XML metadata

Postby wmoore » Thu Jan 12, 2012 4:23 pm

Thanks for that Alex. We've fixed some of the search parameters so things should be working a bit better now.

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


Return to Developer Discussion

Who is online

Users browsing this forum: No registered users and 0 guests