exportSLF.py 3.52 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
"""
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/>.
"""
import xml.etree.cElementTree as ET
import numpy as np
import os
from typing import *

if TYPE_CHECKING:
    from PyQt5 import QtWidgets
    from .dataset import DataSet
    from .analysis.particleContainer import ParticleContainer
    from .analysis.particleCharacterization import FTIRAperture


def exportSLFFile(dataset: 'DataSet', progressBar: 'QtWidgets.QProgressDialog', minAptSize: float = 3,
                  maxAptSize: float = 1000) -> str:
    """
    Writes an SLF file for import in the Perkin Elmer Spotlight Instrument.
    :param dataset: Dataset of the currently loaded experimetn
    :param progressBar: Statusbar for updating progress
    :param minAptSize: Minimal Aperture Size (in µm)
    :param maxAptSize: Maximum Aperture Size (in µm)
    :returns: Filename of saved file
    """
    partContainer: 'ParticleContainer' = dataset.particleContainer
    progressBar.setWindowTitle("Creating SLF File")
    progressBar.setMaximum(partContainer.getNumberOfParticles() - 1)
    pxScale: float = dataset.getPixelScale()  # µm/px

    # prepare xml file
    root = ET.Element("configuration")
    shapes = ET.SubElement(root, "shapes")

    for i, particle in enumerate(partContainer.particles):
        apt: 'FTIRAperture' = particle.aperture
        width = np.clip(apt.width * pxScale, minAptSize, maxAptSize)
        height = np.clip(apt.height * pxScale, minAptSize, maxAptSize)

        x, y, z = dataset.mapToLength((apt.centerX, apt.centerY), force=True, returnz=True)
        x = round(x, 5)
        y = round(y, 5)
        z = round(z, 5)

        shape = ET.SubElement(shapes, "shape", type="Pki.MolSpec.MSOverlayControls.Marker", hasAperture="True")
        ET.SubElement(shape, "dimensions", x=str(x), y=str(y), z=str(z), width="0", height="0", color="4294901760")
        ET.SubElement(shape, "aperture", width=str(width), height=str(height), rotation=str(apt.angle))
        ET.SubElement(shape, "sample", id="Marker", measured="false")
        progressBar.setValue(i)

    # append last entry to slf file:
    shape = ET.SubElement(shapes, "shape", type="Pki.MolSpec.MSOverlayControls.CrosshairCursor", hasAperture="False")
    ET.SubElement(shape, "dimensions", x=str(3856), y=str(-739), z=str(-1643), width="0", height="0",
                  color="4294901760")
    ET.SubElement(shape, "aperture", width=str(width), height=str(height), rotation=str(apt.angle))
    ET.SubElement(shape, "sample", id="Background", measured="true")

    # write slf-file
    savename = os.path.join(dataset.path, dataset.name)
    tree = ET.ElementTree(root)
    inc = 1
    filename = savename + '.slf'

    while os.path.exists(savename):
        filename = savename + '(' + str(inc) + ').slf'
        inc += 1
    tree.write(filename)
    return os.path.basename(filename)