Changeset 93 for Xml/Validator/WebApp/validator/OmeValidator.py
- Timestamp:
- 09/19/07 13:52:48 (14 months ago)
- Files:
-
- 1 modified
-
Xml/Validator/WebApp/validator/OmeValidator.py (modified) (12 diffs)
Legend:
- Unmodified
- Added
- Removed
-
Xml/Validator/WebApp/validator/OmeValidator.py
r88 r93 18 18 19 19 # Standard Imports 20 import logging , cherrypy20 import logging 21 21 from xml.dom.minidom import getDOMImplementation 22 22 from StringIO import StringIO … … 25 25 from stat import * 26 26 27 # LocalDir for shames 27 28 # Load schemas from configured directory 29 import cherrypy 30 # LocalDir for schemas 28 31 SCHEMA_DIR = cherrypy.config.get("validator.schema", os.path.join(os.getcwd(),"schema")) 29 32 def schemaFilePath(inFilename): 33 return os.path.join(os.getcwd(), SCHEMA_DIR, inFilename) 34 """ 35 # Load schemas from current directory 36 def schemaFilePath(inFilename): 37 return inFilename 38 """ 30 39 # Try to load Image for XML Schema Vlaidation Support 31 40 haveTiffSupport = True … … 135 144 warningList = None 136 145 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 137 163 138 164 def __init__(self): … … 212 238 # create an IO string for the xml string provided 213 239 stringXml = StringIO(self.theDom.toxml()) 240 241 # 242 # print self.theDom.toprettyxml() 243 214 244 # building the document tree from the input xml 215 245 try: … … 232 262 # choose the schema source 233 263 # assume the new schema 234 theSchemaFile = os.path.join(os.getcwd(), SCHEMA_DIR, "ome-2007-07.xsd")264 theSchemaFile = "ome-2007-07.xsd" 235 265 # if old schema 236 266 if self.theNamespace == "http://www.openmicroscopy.org/XMLschemas/OME/FC/ome.xsd": … … 238 268 if self.isOmeTiff: 239 269 # 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" 241 271 else: 242 272 # use normal version of old schema 243 theSchemaFile = os.path.join(os.getcwd(), SCHEMA_DIR, "ome-fc.xsd")273 theSchemaFile = "ome-fc.xsd" 244 274 245 275 # loading the OME schema to validate against 246 276 try: 247 schema = etree.XMLSchema(etree.parse( theSchemaFile))277 schema = etree.XMLSchema(etree.parse(schemaFilePath(theSchemaFile))) 248 278 except: 249 279 #chosen scema failed to laod … … 275 305 if reference not in handlerContent.ids: 276 306 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 277 315 278 316 # store the namespace … … 358 396 # parse the new string/file object into the report and validate it 359 397 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 360 410 return theTiffReport 361 411 validateTiff = classmethod(validateTiff) 362 412 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 363 463 364 464 # Used by sax parser to handle errors when processing Elements … … 399 499 self.inBinDataContent = False 400 500 self.shortFormXml = "" 501 # internal check counters 401 502 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 402 509 self.hasCustomAttributes = False 403 510 … … 486 593 if name[-7:] == "BinData": 487 594 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 488 634 self.domify(name, attribs) 489 635 … … 556 702 557 703 if __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 561 710 562 711 for aFilename in ["samples/4d2wOME.tif", "samples/4d2wOME-fixed.tif", … … 567 716 568 717 print "============" 569 570
