• Login
  • Help/Guide
  • About Trac
  • Preferences
  • Wiki
  • Timeline
  • Roadmap
  • Browse Source
  • View Tickets
  • Search

Context Navigation

  • ← Previous Change
  • Next Change →

Changeset 93 for Xml/Validator/WebApp/validator/OmeValidator.py

Show
Ignore:
Timestamp:
09/19/07 13:52:48 (14 months ago)
Author:
andrew
Message:

Applied Version 2 - September 2007 changes to schema and improved Tiff chacks in validator

Files:
1 modified

  • Xml/Validator/WebApp/validator/OmeValidator.py (modified) (12 diffs)

Legend:

Unmodified
Added
Removed
  • Xml/Validator/WebApp/validator/OmeValidator.py

    r88 r93  
    1818 
    1919# Standard Imports 
    20 import logging, cherrypy 
     20import logging 
    2121from xml.dom.minidom import getDOMImplementation 
    2222from StringIO import StringIO 
    … …  
    2525from stat import * 
    2626 
    27 # LocalDir for shames 
     27 
     28# Load schemas from configured directory  
     29import cherrypy 
     30# LocalDir for schemas 
    2831SCHEMA_DIR = cherrypy.config.get("validator.schema", os.path.join(os.getcwd(),"schema")) 
    29  
     32def schemaFilePath(inFilename): 
     33        return os.path.join(os.getcwd(), SCHEMA_DIR, inFilename) 
     34""" 
     35# Load schemas from current directory  
     36def schemaFilePath(inFilename): 
     37        return inFilename 
     38""" 
    3039# Try to load Image for XML Schema Vlaidation Support 
    3140haveTiffSupport = True 
    … …  
    135144        warningList = None 
    136145        unresolvedList = None 
     146         
     147        """ 
     148        Create variables used to check internal consistency 
     149        """ 
     150        # count of TiffData elements founf in the XML 
     151        omeTiffDataCount = None 
     152        # count of image frames found in Tiff file 
     153        tiffFileFrames = None 
     154         
     155        # count of pixels 
     156        omePixelsCount = None 
     157        # count of planes 
     158        omePlanesCount = None 
     159        # count of Z * C * T 
     160        ome5dPlaneCount = None 
     161        # count of number of times a tiff data block has asked for "all available frames" - value of more then 1 indicates an error 
     162        theAllFrameCount = None 
    137163         
    138164        def __init__(self): 
    … …  
    212238                # create an IO string for the xml string provided 
    213239                stringXml = StringIO(self.theDom.toxml()) 
     240                 
     241                # 
     242                # print self.theDom.toprettyxml() 
     243                 
    214244                # building the document tree from the input xml 
    215245                try: 
    … …  
    232262                # choose the schema source 
    233263                # assume the new schema 
    234                 theSchemaFile = os.path.join(os.getcwd(), SCHEMA_DIR, "ome-2007-07.xsd") 
     264                theSchemaFile = "ome-2007-07.xsd" 
    235265                # if old schema 
    236266                if self.theNamespace == "http://www.openmicroscopy.org/XMLschemas/OME/FC/ome.xsd": 
    … …  
    238268                        if self.isOmeTiff: 
    239269                                # use special tiff version of old schema 
    240                                 theSchemaFile = os.path.join(os.getcwd(), SCHEMA_DIR, "ome-fc-tiff.xsd") 
     270                                theSchemaFile = "ome-fc-tiff.xsd" 
    241271                        else: 
    242272                                # use normal version of old schema 
    243                                 theSchemaFile = os.path.join(os.getcwd(), SCHEMA_DIR, "ome-fc.xsd") 
     273                                theSchemaFile = "ome-fc.xsd" 
    244274                 
    245275                # loading the OME schema to validate against 
    246276                try: 
    247                         schema = etree.XMLSchema(etree.parse(theSchemaFile)) 
     277                        schema = etree.XMLSchema(etree.parse(schemaFilePath(theSchemaFile))) 
    248278                except: 
    249279                        #chosen scema failed to laod 
    … …  
    275305                        if reference not in handlerContent.ids: 
    276306                                self.unresolvedList.append(ParseMessage(None, None, None, "UnresolvedID",None, reference)) 
     307                 
     308                # store the internal counters 
     309                self.omeTiffDataCount = handlerContent.omeTiffDataCount 
     310                self.omePixelsCount = handlerContent.omePixelsCount 
     311                self.omePlanesCount = handlerContent.omePlanesCount 
     312                self.ome5dPlaneCount = handlerContent.ome5dPlaneCount 
     313                self.omeTiffDataPlaneCount = handlerContent.omeTiffDataPlaneCount 
     314                self.theAllFrameCount = handlerContent.theAllFrameCount 
    277315                 
    278316                # store the namespace 
    … …  
    358396                                        # parse the new string/file object into the report and validate it  
    359397                                        theTiffReport.parse(theFileString) 
     398                                        theTiffReport.validateTiffImageData(image) 
     399                                        """ 
     400                                        # print theXml 
     401                                        print "Tiff Frames    : %s" % theTiffReport.tiffFileFrames       
     402                                        print "Ome Frames     : %s" % theTiffReport.omeTiffDataCount     
     403                                        print "Ome Pixels     : %s" % theTiffReport.omePixelsCount       
     404                                        print "Ome Planes     : %s" % theTiffReport.omePlanesCount       
     405                                        print "Ome 5dPlane    : %s" % theTiffReport.ome5dPlaneCount      
     406                                        print "Ome TiffPlane  : %s" % theTiffReport.omeTiffDataPlaneCount        
     407                                        print "Ome AllFrame   : %s" % theTiffReport.theAllFrameCount     
     408                                        """ 
     409                                         
    360410                return theTiffReport 
    361411        validateTiff = classmethod(validateTiff) 
    362412         
     413        def validateTiffImageData(self, inImage, ): 
     414                """ 
     415                Examines the tiff image data to compare with  
     416                """ 
     417                 
     418                """ code to look at the list of tiff image dimensions 
     419                theTiffWidth = None 
     420                theTiffHeight = None 
     421                try: 
     422                        #theTiffWidth = int(inImage.tag[256]) 
     423                        print inImage.tag[256] 
     424                except KeyError: 
     425                        pass 
     426                except ValueError: 
     427                        pass 
     428                try: 
     429                        theTiffHeight = int(inImage.tag[257]) 
     430                except KeyError: 
     431                        pass 
     432                except ValueError: 
     433                        pass 
     434                """ 
     435                 
     436                #look for frames 
     437                self.tiffFileFrames = 0 
     438                try: 
     439                    while True: 
     440                        inImage.seek( self.tiffFileFrames ) 
     441                        self.tiffFileFrames = self.tiffFileFrames + 1 
     442                except EOFError: 
     443                    inImage.seek( 0 ) 
     444                    pass 
     445                 
     446                self.isOmeTiffConsistent = True 
     447                 
     448                # compare with values from xml and tiff 
     449                if self.tiffFileFrames > self.ome5dPlaneCount: 
     450                        self.warningList.append(ParseMessage(None, None, None, "TIFF", ("Frames %s needing %s" % (self.tiffFileFrames,self.ome5dPlaneCount)) , "Extra frames are present in this Tiff file")) 
     451 
     452                if self.tiffFileFrames < self.ome5dPlaneCount: 
     453                        self.warningList.append(ParseMessage(None, None, None, "TIFF", ("Frames %s out of %s" % (self.tiffFileFrames,self.ome5dPlaneCount)) , "Not all possible frames are present in this Tiff file")) 
     454 
     455                # compare with values from xml TiffData and tiff 
     456                totalTiffDataFrames = self.omeTiffDataPlaneCount + (self.tiffFileFrames * self.theAllFrameCount) 
     457                if self.tiffFileFrames > totalTiffDataFrames: 
     458                        self.warningList.append(ParseMessage(None, None, None, "TIFF", ("Frames %s referenced %s" % (self.tiffFileFrames,totalTiffDataFrames)) , "Unreferenced frames are present in this Tiff file")) 
     459 
     460                if self.tiffFileFrames < totalTiffDataFrames: 
     461                        self.errorList.append(ParseMessage(None, None, None, "TIFF", ("Frames %s out of %s" % (self.tiffFileFrames,totalTiffDataFrames)) , "Not all required frames are present in this Tiff file")) 
     462                        self.isOmeTiffConsistent = False 
    363463 
    364464# Used by sax parser to handle errors when processing Elements 
    … …  
    399499                self.inBinDataContent = False 
    400500                self.shortFormXml = "" 
     501                # internal check counters 
    401502                self.skipCount = 0 
     503                self.omeTiffDataCount = 0 
     504                self.omePixelsCount = 0 
     505                self.omePlanesCount = 0 
     506                self.ome5dPlaneCount = 0 
     507                self.omeTiffDataPlaneCount = 0 
     508                self.theAllFrameCount = 0 
    402509                self.hasCustomAttributes = False 
    403510                 
    … …  
    486593                if name[-7:] == "BinData": 
    487594                        self.inBinData = True 
     595                 
     596                if name[-8:] == "TiffData": 
     597                        self.omeTiffDataCount = self.omeTiffDataCount + 1 
     598                        # record the number of planes used by the TiffData block 
     599                        if "NumPlanes" in attribs: 
     600                                # use the number of planes specified 
     601                                self.omeTiffDataPlaneCount = self.omeTiffDataPlaneCount + int(attribs.getValue("NumPlanes")) 
     602                                if self.theAllFrameCount > 0: 
     603                                        self.errorList.append(ParseMessage(None, None, None, "OME","", "Inconsistent use of TiffData element [Type 1]")) 
     604                                 
     605                        else: 
     606                                if "IFD" in attribs: 
     607                                        # use one frame 
     608                                        self.omeTiffDataPlaneCount = self.omeTiffDataPlaneCount + 1 
     609                                        if self.theAllFrameCount > 0: 
     610                                                self.errorList.append(ParseMessage(None, None, None, "OME","", "Inconsistent use of TiffData element [Type 2]")) 
     611                                else: 
     612                                        # use all the frames in the tiff 
     613                                        self.theAllFrameCount = self.theAllFrameCount + 1 
     614                                        if (self.theAllFrameCount > 1) or (self.omeTiffDataPlaneCount > 0): 
     615                                                self.errorList.append(ParseMessage(None, None, None, "OME","", "Inconsistent use of TiffData element [Type 3]")) 
     616                         
     617                if name[-6:] == "Pixels": 
     618                        self.omePixelsCount = self.omePixelsCount + 1 
     619                        try: 
     620                                # total up planes needed from Z, C and T 
     621                                theZ = int(attribs.getValue("SizeZ")) 
     622                                theC = int(attribs.getValue("SizeC")) 
     623                                theT = int(attribs.getValue("SizeT")) 
     624                                # print "Z: %s, C: %s, T: %s" % (theZ, theC, theT) 
     625                                self.ome5dPlaneCount = self.ome5dPlaneCount + (theZ * theC * theT) 
     626                        except KeyError: 
     627                                pass 
     628                        except ValueError: 
     629                                pass 
     630                 
     631                if name[-5:] == "Plane": 
     632                        self.omePlanesCount = self.omePlanesCount + 1 
     633                 
    488634                self.domify(name, attribs) 
    489635                         
    … …  
    556702 
    557703if __name__ == '__main__': 
    558         for aFilename in ["samples/sdub.ome", "samples/tiny.ome", "samples/broke.ome"]: 
    559                 print "============ XML file %s ============ " % aFilename 
    560                 print XmlReport.validateFile(aFilename) 
     704        for aFilename in ["samples/completesamplenopre.xml","samples/completesample.xml","samples/completesamplenoenc.xml", 
     705                        "samples/sdub.ome", "samples/sdub-fix.ome", "samples/sdub-fix-pre.ome",  
     706                        "samples/tiny.ome", "samples/broke.ome"]: 
     707                        print "============ XML file %s ============ " % aFilename 
     708                        print XmlReport.validateFile(aFilename) 
     709         
    561710         
    562711        for aFilename in ["samples/4d2wOME.tif", "samples/4d2wOME-fixed.tif", 
    … …  
    567716         
    568717        print "============" 
    569  
    570  

Download in other formats:

  • Unified Diff
  • Zip Archive

Trac Powered

Powered by Trac 0.11
By Edgewall Software.

Visit the Trac open source project at
http://trac.edgewall.org/