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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
|
# /*#########################################################################
# Copyright (C) 2004-2022 European Synchrotron Radiation Facility
#
# This file is part of the PyMca X-ray Fluorescence Toolkit developed at
# the ESRF by the Software group.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
# ###########################################################################*/
"""
This plugin is used to load motor positions from a CSV or HDF5 file.
The number of values associated with a given positioner should be equal
to the number of pixels in the stack image. A single scalar value can also be
provided for a motor, if it didn't move during the experiment.
A CSV file should have unique motor names in the header line, and can have
an arbitrary number of motors/columns.
Motor positions in a HDF5 files are 1-dimensional datasets whose names are
the motor names. The user is allowed to select the HDF5 group containing all
motor datasets.
Data loaded with this plugin can then be used by other tools, such as the
"Stack motor positions" plugin.
"""
__authors__ = ["P. Knobel"]
__license__ = "MIT"
import logging
from PyMca5 import StackPluginBase
from PyMca5.PyMcaGui.io.hdf5.HDF5Widget import getGroupNameDialog
from PyMca5.PyMcaGui.io import PyMcaFileDialogs
from PyMca5.PyMcaIO import specfilewrapper
try:
import h5py
except ImportError:
h5py = None
try:
# if silx is available, we can open SPEC
from silx.io import open as silx_open
h5open = silx_open
from silx.io import is_dataset
except ImportError:
silx_open = None
if h5py is not None:
# at least we can open hdf5 files
def h5open(filename):
return h5py.File(filename, "r")
def is_dataset(item):
return isinstance(item, h5py.Dataset)
else:
# no luck, only CSV files available
h5open = None
is_dataset = None
# suppress errors and warnings if fabio is missing
if silx_open is not None:
logging.getLogger("silx.io.fabioh5").setLevel(logging.CRITICAL)
_logger = logging.getLogger(__name__)
class LoadPositionersStackPlugin(StackPluginBase.StackPluginBase):
"""
This plugin is used to load motor positions from a CSV or HDF5 file.
The number of values associated with a given positioner should be equal
to the number of pixels in the stack image. A single scalar value can also be
provided for a motor, if it didn't move during the experiment.
A CSV file should have unique motor names in the header line, and can have
an arbitrary number of motors/columns.
Motor positions in a HDF5 files are 1-dimensional datasets whose names are
the motor names. The user is allowed to select the HDF5 group containing all
motor datasets. Only compatible motors will be loaded.
Data loaded with this plugin can then be used by other plugins.
"""
def __init__(self, stackWindow):
if _logger.getEffectiveLevel() == logging.DEBUG:
StackPluginBase.pluginBaseLogger.setLevel(logging.DEBUG)
StackPluginBase.StackPluginBase.__init__(self, stackWindow)
self.methodDict = {'Load positioners': [self._loadFromFile,
"Load positioners from file"]}
self.__methodKeys = ['Load positioners']
#Methods implemented by the plugin
def getMethods(self):
return self.__methodKeys
def getMethodToolTip(self, name):
return self.methodDict[name][1]
def getMethodPixmap(self, name):
return self.methodDict[name][2]
def applyMethod(self, name):
return self.methodDict[name][0]()
def _loadFromFile(self):
stack = self.getStackDataObject()
if stack is None:
return
mcaIndex = stack.info.get('McaIndex')
if not (mcaIndex in [0, -1, 2]):
raise IndexError("1D index must be 0, 2, or -1")
# test io dependencies
if h5py is None:
filefilter = []
else:
filefilter = ['HDF5 (*.h5 *.nxs *.hdf *.hdf5)']
filefilter.append('CSV (*.csv *.txt)')
if silx_open is not None:
filefilter.append('Any (*)')
filename, ffilter = PyMcaFileDialogs.\
getFileList(parent=None,
filetypelist=filefilter,
message='Load',
mode='OPEN',
single=True,
getfilter=True,
currentfilter=filefilter[0])
if len(filename):
_logger.debug("file name = %s file filter = %s", filename, ffilter)
else:
_logger.debug("nothing selected")
return
filename = filename[0]
positioners = {}
if not ffilter.startswith('CSV'):
h5GroupName = getGroupNameDialog(filename)
if h5GroupName is None:
return
with h5open(filename) as h5f:
h5Group = h5f[h5GroupName]
positioners = {}
for dsname in h5Group:
# links and subgroups just ignored for the time being
if not is_dataset(h5Group[dsname]):
continue
positioners[dsname] = h5Group[dsname][()]
else:
sf = specfilewrapper.Specfile(filename)
scan = sf[0]
labels = scan.alllabels()
data = scan.data()
scan = None
sf = None
for i, label in enumerate(labels):
positioners[label] = data[i, :]
self._stackWindow.setPositioners(positioners)
MENU_TEXT = "Load positioners from file"
def getStackPluginInstance(stackWindow, **kw):
ob = LoadPositionersStackPlugin(stackWindow)
return ob
|