/usr/share/pyshared/PyMca/NumpyArrayTableModel.py is in pymca 4.5.0-4.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 187 188 189 190 191 192 193 194 195 196 197 | #/*##########################################################################
# Copyright (C) 2004-2011 European Synchrotron Radiation Facility
#
# This file is part of the PyMCA X-ray Fluorescence Toolkit developed at
# the ESRF by the Beamline Instrumentation Software Support (BLISS) group.
#
# This toolkit 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 2 of the License, or (at your option)
# any later version.
#
# PyMCA 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
# PyMCA; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
# Suite 330, Boston, MA 02111-1307, USA.
#
# PyMCA follows the dual licensing model of Trolltech's Qt and Riverbank's PyQt
# and cannot be used as a free plugin for a non-free program.
#
# Please contact the ESRF industrial unit (industry@esrf.fr) if this license
# is a problem for you.
#############################################################################*/
import numpy
from PyQt4 import QtCore, QtGui
if hasattr(QtCore, 'QStringList'):
MyQVariant = QtCore.QVariant
else:
def MyQVariant(x=None):
return x
class NumpyArrayTableModel(QtCore.QAbstractTableModel):
def __init__(self, parent=None, narray=None, fmt="%g", perspective=0):
QtCore.QAbstractTableModel.__init__(self, parent)
if narray is None:
narray = numpy.array([])
self._array = narray
self._format = fmt
self._index = 0
self.assignDataFunction(perspective)
def rowCount(self, parent=None):
return self._rowCount(parent)
def columnCount(self, parent=None):
return self._columnCount(parent)
def data(self, index, role=QtCore.Qt.DisplayRole):
return self._data(index, role)
def _rowCount1D(self, parent=None):
return 1
def _columnCount1D(self, parent=None):
return self._array.shape[0]
def _data1D(self, index, role=QtCore.Qt.DisplayRole):
if index.isValid():
if role == QtCore.Qt.DisplayRole:
row = 0
col = index.column()
return MyQVariant(self._format % self._array[col])
return MyQVariant()
def _rowCount2D(self, parent=None):
return self._array.shape[0]
def _columnCount2D(self, parent=None):
return self._array.shape[1]
def _data2D(self, index, role=QtCore.Qt.DisplayRole):
if index.isValid():
if role == QtCore.Qt.DisplayRole:
row = index.row()
col = index.column()
return MyQVariant(self._format % self._array[row, col])
return MyQVariant()
def _rowCount3DIndex0(self, parent=None):
return self._array.shape[1]
def _columnCount3DIndex0(self, parent=None):
return self._array.shape[2]
def _rowCount3DIndex1(self, parent=None):
return self._array.shape[0]
def _columnCount3DIndex1(self, parent=None):
return self._array.shape[2]
def _rowCount3DIndex2(self, parent=None):
return self._array.shape[0]
def _columnCount3DIndex2(self, parent=None):
return self._array.shape[1]
def _data3DIndex0(self, index, role=QtCore.Qt.DisplayRole):
if index.isValid():
if role == QtCore.Qt.DisplayRole:
row = index.row()
col = index.column()
return MyQVariant(self._format % self._array[self._index,
row,
col])
return MyQVariant()
def _data3DIndex1(self, index, role=QtCore.Qt.DisplayRole):
if index.isValid():
if role == QtCore.Qt.DisplayRole:
row = index.row()
col = index.column()
return MyQVariant(self._format % self._array[row,
self._index,
col])
return MyQVariant()
def _data3DIndex2(self, index, role=QtCore.Qt.DisplayRole):
if index.isValid():
if role == QtCore.Qt.DisplayRole:
row = index.row()
col = index.column()
return MyQVariant(self._format % self._array[row,
col,
self._index])
return MyQVariant()
def setArrayData(self, data, perspective=0):
"""
setStackData(self, data, perspective=0)
data is a 3D array
dimension is the array dimension acting as index of images
"""
self.reset()
self._array = data
self.assignDataFunction(perspective)
self._index = 0
def assignDataFunction(self, dimension):
shape = self._array.shape
if len(shape) == 2:
self._rowCount = self._rowCount2D
self._columnCount = self._columnCount2D
self._data = self._data2D
elif len(shape) == 1:
self._rowCount = self._rowCount1D
self._columnCount = self._columnCount1D
self._data = self._data1D
else:
if dimension == 1:
self._rowCount = self._rowCount3DIndex1
self._columnCount = self._columnCount3DIndex1
self._data = self._data3DIndex1
elif dimension == 2:
self._rowCount = self._rowCount3DIndex2
self._columnCount = self._columnCount3DIndex2
self._data = self._data3DIndex1
else:
self._rowCount = self._rowCount3DIndex0
self._columnCount = self._columnCount3DIndex0
self._data = self._data3DIndex0
self._dimension = dimension
def setCurrentArrayIndex(self, index):
"""
This method is ignored if the current array does not
not a 3-dimensional array.
"""
shape = self._array.shape
if len(shape) != 3:
return
shape = self._array.shape[self._dimension]
if (index < 0) or (index >= shape):
raise ValueError("Index must be an integer lower than %d" % shape)
self._index = index
def setFormat(self, fmt):
self._format = fmt
if __name__ == "__main__":
a = QtGui.QApplication([])
w = QtGui.QTableView()
d = numpy.random.normal(0,1, (5, 1000,1000))
for i in range(5):
d[i, :, :] += i
#m = NumpyArrayTableModel(fmt="%.5f")
#m = NumpyArrayTableModel(None, numpy.arange(100.), fmt="%.5f")
#m = NumpyArrayTableModel(None, numpy.ones((100,20)), fmt="%.5f")
m = NumpyArrayTableModel(None, d, fmt="%.5f")
w.setModel(m)
m.setCurrentArrayIndex(4)
#m.setArrayData(numpy.ones((100,)))
w.show()
a.exec_()
|