/usr/lib/python2.7/dist-packages/pyFAI/geometryRefinement.py is in pyfai 0.3.5-1.
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 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 | #!/usr/bin/env python
# -*- coding: utf8 -*-
#
# Project: Azimuthal integration
# https://forge.epn-campus.eu/projects/azimuthal
#
# File: "$Id$"
#
# Copyright (C) European Synchrotron Radiation Facility, Grenoble, France
#
# Principal author: Jérôme Kieffer (Jerome.Kieffer@ESRF.eu)
#
# 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. If not, see <http://www.gnu.org/licenses/>.
#
__author__ = "Jerome Kieffer"
__contact__ = "Jerome.Kieffer@ESRF.eu"
__license__ = "GPLv3+"
__copyright__ = "European Synchrotron Radiation Facility, Grenoble, France"
__date__ = "23/12/2011"
__status__ = "development"
import os, tempfile, subprocess, logging, threading
import numpy
from math import pi
from azimuthalIntegrator import AzimuthalIntegrator
from scipy.optimize import fmin, leastsq, fmin_slsqp, anneal
from scipy.interpolate import interp2d
if os.name != "nt":
WindowsError = RuntimeError
logger = logging.getLogger("pyFAI.geometryRefinement")
#logger.setLevel(logging.DEBUG)
ROCA = "/opt/saxs/roca"
################################################################################
# GeometryRefinement
################################################################################
class GeometryRefinement(AzimuthalIntegrator):
def __init__(self, data, dist=1, poni1=None, poni2=None, rot1=0, rot2=0, rot3=0, pixel1=1, pixel2=1, splineFile=None):
self.data = numpy.array(data, dtype="float64")
if (poni1 is None) and (poni2 is None):
AzimuthalIntegrator.__init__(self, dist, 0, 0, rot1, rot2, rot3, pixel1, pixel2, splineFile)
else:
AzimuthalIntegrator.__init__(self, dist, poni1, poni2 , rot1, rot2, rot3, pixel1, pixel2, splineFile)
if (poni1 is None) and (poni2 is None):
tth = self.data[:, 2]
asrt = tth.argsort()
tth = tth[asrt]
srtdata = self.data[asrt]
smallRing = srtdata[tth < (tth.min() + 1e-6)]
center = smallRing.sum(axis=0) / len(smallRing)
self.poni1 = center[0] * self.pixel1
self.poni2 = center[1] * self.pixel2
self._dist_min = 0
self._dist_max = 10
self._poni1_min = -10000 * pixel1
self._poni1_max = 15000 * pixel1
self._poni2_min = -10000 * pixel2
self._poni2_max = 15000 * pixel2
self._rot1_min = -pi
self._rot1_max = pi
self._rot2_min = -pi
self._rot2_max = pi
self._rot3_min = -pi
self._rot3_max = pi
def residu1(self, param, d1, d2, tthRef):
return self.tth(d1, d2, param) - tthRef
def residu2(self, param, d1, d2, tthRef):
return (self.residu1(param, d1, d2, tthRef) ** 2).sum()
def refine1(self):
self.param = numpy.array([self._dist, self._poni1, self._poni2, self._rot1, self._rot2, self._rot3], dtype="float64")
newParam, rc = leastsq(self.residu1, self.param, args=(self.data[:, 0], self.data[:, 1], self.data[:, 2]))
oldDeltaSq = self.chi2(tuple(self.param))
newDeltaSq = self.chi2(tuple(newParam))
logger.info("Least square retcode=%s %s --> %s", rc, oldDeltaSq, newDeltaSq)
if newDeltaSq < oldDeltaSq:
i = abs(self.param - newParam).argmax()
d = ["dist", "poni1", "poni2", "rot1", "rot2", "rot3"]
logger.info("maxdelta on %s: %s --> %s ", d[i], self.param[i], newParam[i])
self.param = newParam
self.dist, self.poni1, self.poni2, self.rot1, self.rot2, self.rot3 = tuple(newParam)
return newDeltaSq
else:
return oldDeltaSq
def refine2(self, maxiter=1000000):
self.param = numpy.array([self.dist, self.poni1, self.poni2, self.rot1, self.rot2, self.rot3], dtype="float64")
if logger.getEffectiveLevel() <= logging.INFO:
disp = 1
else:
disp = 0
newParam = fmin_slsqp(self.residu2, self.param, iter=maxiter, args=(self.data[:, 0], self.data[:, 1], self.data[:, 2]),
bounds=[(self._dist_min, self._dist_max),
(self._poni1_min, self._poni1_max),
(self._poni2_min, self._poni2_max),
(self._rot1_min, self._rot1_max),
(self._rot2_min, self._rot2_max),
(self._rot3_min, self._rot3_max)],
acc=1.0e-12, iprint=disp)
oldDeltaSq = self.chi2()
newDeltaSq = self.chi2(newParam)
logger.info("Constrained Least square %s --> %s", oldDeltaSq, newDeltaSq)
if newDeltaSq < oldDeltaSq:
i = abs(self.param - newParam).argmax()
d = ["dist", "poni1", "poni2", "rot1", "rot2", "rot3"]
logger.info("maxdelta on %s: %s --> %s ", d[i], self.param[i], newParam[i])
self.param = newParam
self.dist, self.poni1, self.poni2, self.rot1, self.rot2, self.rot3 = tuple(newParam)
return newDeltaSq
else:
return oldDeltaSq
def simplex(self, maxiter=1000000):
self.param = numpy.array([self.dist, self.poni1, self.poni2, self.rot1, self.rot2, self.rot3], dtype="float64")
newParam = fmin(self.residu2, self.param, args=(self.data[:, 0], self.data[:, 1], self.data[:, 2]),
maxiter=maxiter, xtol=1.0e-12)
oldDeltaSq = self.chi2(tuple(self.param))
newDeltaSq = self.chi2(tuple(newParam))
logger.info("Simplex %s --> %s", oldDeltaSq, newDeltaSq)
if newDeltaSq < oldDeltaSq:
i = abs(self.param - newParam).argmax()
d = ["dist", "poni1", "poni2", "rot1", "rot2", "rot3"]
logger.info("maxdelta on %s : %s --> %s ", d[i], self.param[i], newParam[i])
self.param = newParam
self.dist, self.poni1, self.poni2, self.rot1, self.rot2, self.rot3 = tuple(newParam)
return newDeltaSq
else:
return oldDeltaSq
def anneal(self, maxiter=1000000):
self.param = [self.dist, self.poni1, self.poni2, self.rot1, self.rot2, self.rot3]
result = anneal(self.residu2, self.param, args=(self.data[:, 0], self.data[:, 1], self.data[:, 2]),
lower=[self._dist_min, self._poni1_min, self._poni2_min, self._rot1_min , self._rot2_min, self._rot3_min],
upper=[self._dist_max, self._poni1_max, self._poni2_max, self._rot1_max , self._rot2_max, self._rot3_max],
maxiter=maxiter)
newParam = result[0]
oldDeltaSq = self.chi2()
newDeltaSq = self.chi2(newParam)
logger.info("Anneal %s --> %s", oldDeltaSq, newDeltaSq)
if newDeltaSq < oldDeltaSq:
i = abs(self.param - newParam).argmax()
d = ["dist", "poni1", "poni2", "rot1", "rot2", "rot3"]
logger.info("maxdelta on %s : %s --> %s ", d[i], self.param[i], newParam[i])
self.param = newParam
self.dist, self.poni1, self.poni2, self.rot1, self.rot2, self.rot3 = tuple(newParam)
return newDeltaSq
else:
return oldDeltaSq
def chi2(self, param=None):
if param == None:
param = self.param
return self.residu2(param, self.data[:, 0], self.data[:, 1], self.data[:, 2])
def roca(self):
"""
run roca to optimise the parameter set
"""
tmpf = tempfile.NamedTemporaryFile()
for line in self.data:
tmpf.write("%s %s %s %s" % (line[2], line[0], line[1], os.linesep))
tmpf.flush()
roca = subprocess.Popen([ROCA, "debug=8", "maxdev=1", "input=" + tmpf.name,
str(self.pixel1), str(self.pixel2),
str(self.poni1 / self.pixel1), str(self.poni2 / self.pixel2),
str(self.dist), str(self.rot1), str(self.rot2), str(self.rot3)],
stdout=subprocess.PIPE)
newParam = [self.dist, self.poni1, self.poni2, self.rot1, self.rot2, self.rot3]
for line in roca.stdout:
word = line.split()
if len(word) == 3:
if word[0] == "cen1":
newParam[1] = float(word[1]) * self.pixel1
if word[0] == "cen2":
newParam[2] = float(word[1]) * self.pixel2
if word[0] == "dis":
newParam[0] = float(word[1])
if word[0] == "rot1":
newParam[3] = float(word[1])
if word[0] == "rot2":
newParam[4] = float(word[1])
if word[0] == "rot3":
newParam[5] = float(word[1])
print "Roca", self.chi2(), "--> ", self.chi2(newParam)
if self.chi2(tuple(newParam)) < self.chi2(tuple(self.param)):
self.param = newParam
self.dist, self.poni1, self.poni2, self.rot1, self.rot2, self.rot3 = tuple(newParam)
tmpf.close()
def set_dist_max(self, value):
if isinstance(value, float):
self._dist_max = value
else:
self._dist_max = float(value)
def get_dist_max(self):
return self._dist_max
dist_max = property(get_dist_max, set_dist_max)
def set_dist_min(self, value):
if isinstance(value, float):
self._dist_min = value
else:
self._dist_min = float(value)
def get_dist_min(self):
return self._dist_min
dist_min = property(get_dist_min, set_dist_min)
def set_poni1_min(self, value):
if isinstance(value, float):
self._poni1_min = value
else:
self._poni1_min = float(value)
def get_poni1_min(self):
return self._poni1_min
poni1_min = property(get_poni1_min, set_poni1_min)
def set_poni1_max(self, value):
if isinstance(value, float):
self._poni1_max = value
else:
self._poni1_max = float(value)
def get_poni1_max(self):
return self._poni1_max
poni1_max = property(get_poni1_max, set_poni1_max)
def set_poni2_min(self, value):
if isinstance(value, float):
self._poni2_min = value
else:
self._poni2_min = float(value)
def get_poni2_min(self):
return self._poni2_min
poni2_min = property(get_poni2_min, set_poni2_min)
def set_poni2_max(self, value):
if isinstance(value, float):
self._poni2_max = value
else:
self._poni2_max = float(value)
def get_poni2_max(self):
return self._poni2_max
poni2_max = property(get_poni2_max, set_poni2_max)
def set_rot1_min(self, value):
if isinstance(value, float):
self._rot1_min = value
else:
self._rot1_min = float(value)
def get_rot1_min(self):
return self._rot1_min
rot1_min = property(get_rot1_min, set_rot1_min)
def set_rot1_max(self, value):
if isinstance(value, float):
self._rot1_max = value
else:
self._rot1_max = float(value)
def get_rot1_max(self):
return self._rot1_max
rot1_max = property(get_rot1_max, set_rot1_max)
def set_rot2_min(self, value):
if isinstance(value, float):
self._rot2_min = value
else:
self._rot2_min = float(value)
def get_rot2_min(self):
return self._rot2_min
rot2_min = property(get_rot2_min, set_rot2_min)
def set_rot2_max(self, value):
if isinstance(value, float):
self._rot2_max = value
else:
self._rot2_max = float(value)
def get_rot2_max(self):
return self._rot2_max
rot2_max = property(get_rot2_max, set_rot2_max)
def set_rot3_min(self, value):
if isinstance(value, float):
self._rot3_min = value
else:
self._rot3_min = float(value)
def get_rot3_min(self):
return self._rot3_min
rot3_min = property(get_rot3_min, set_rot3_min)
def set_rot3_max(self, value):
if isinstance(value, float):
self._rot3_max = value
else:
self._rot3_max = float(value)
def get_rot3_max(self):
return self._rot3_max
rot3_max = property(get_rot3_max, set_rot3_max)
|