zeissxml.py 3.48 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
GEPARD - Gepard-Enabled PARticle Detection
Copyright (C) 2018  Lars Bittrich and Josef Brandt, Leibniz-Institut für 
Polymerforschung Dresden e. V. <bittrich-lars@ipfdd.de>    

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program, see COPYING.  
If not, see <https://www.gnu.org/licenses/>.
"""

from xml.sax import make_parser, handler

class Marker:
    def __init__(self, name, x, y, z):
        self.name, self.x, self.y, self.z = name, x, y, z
        
    def getPos(self):
        return self.x, self.y, self.z

    def __repr__(self):
        return str(self)
    
    def __str__(self):
        return f'Marker: "{self.name}" at: {self.getPos()} µm'
    
class Region:
    def __init__(self):
        self.centerx, self.centery = None, None
        self.width, self.height = None, None
        
    def __repr__(self):
        return str(self)
    
    def __str__(self):
        return f'Region center: {self.centerx, self.centery} µm\n' + \
               f'Region size: {self.width, self.height} µm'
               
class ZRange:
    def __init__(self):
        self.z0, self.zn = None, None
        self.dz = None
        
    def __repr__(self):
        return str(self)
    
    def __str__(self):
        return f'Z-range in: {self.z0, self.zn} µm at step size: {self.dz} µm'

class ZeissHandler(handler.ContentHandler):
    def __init__(self):
        self.markers = []
        self.region = Region()
        self.zrange = ZRange()
        self.intag = False
        self.subtag = ''
        
    def characters(self, content):
        if self.intag:
            self.content += content.strip()
        
    def startElement(self, name, attrs):  
        if name == 'Marker':
            self.markers.append(Marker(attrs['Id'], attrs['StageXPosition'],
                                       attrs['StageYPosition'], 
                                       attrs['FocusPosition']))
        elif name == 'TileRegion' or name == 'ZStackSetup':
            self.intag = True
        if self.intag:
            self.content = ''
            if name in ['First','Last','Interval']:
                self.subtag = name
            
    def endElement(self, name):
        if name == 'TileRegion' or name == 'ZStackSetup':
            self.intag = False
        if self.intag and name == 'CenterPosition':
            self.region.centerx, self.region.centery = map(float, self.content.split(','))
        elif self.intag and name == 'ContourSize':
            self.region.width, self.region.height = map(float, self.content.split(','))
        elif self.intag and name == 'Value' and \
            self.subtag in ['First','Last','Interval']:
            attrmap = {'First':'z0','Last':'zn','Interval':'dz'}
            # values a given in meter even though µm are set as units? 
            #  -> we convert to µm 
            self.zrange.__setattr__(attrmap[self.subtag], 
                                    float(self.content)*1.0e6)
            self.subtag = ''