pyFAI/sandbox/chi_square_ds8.py

95 lines
4.0 KiB
Python
Raw Permalink Normal View History

#!/usr/bin/env python
2015-10-03 00:24:16 +08:00
# coding: utf-8
# author: Jérôme Kieffer
#
# Project: Fast Azimuthal integration
2016-12-01 17:03:17 +08:00
# https://github.com/silx-kit/pyFAI
2015-10-03 00:24:16 +08:00
#
# 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.
2014-04-30 20:43:21 +08:00
2015-10-03 00:24:16 +08:00
# tests if the distribution of Chi2 is centered around 1:
2014-04-30 20:43:21 +08:00
# Needs a large dataset (thousands of images)
2014-04-30 20:45:19 +08:00
import os
2014-04-30 20:43:21 +08:00
import sys
import glob
import pylab
pylab.ion()
import numpy
from math import sqrt
import fabio
2018-01-10 23:56:07 +08:00
import logging
logger = logging.getLogger(__file__)
2014-04-30 20:43:21 +08:00
pyFAI = sys.modules["pyFAI"]
from lxml import etree
ai = pyFAI.AzimuthalIntegrator(detector="Pilatus1M")
2014-04-30 20:44:38 +08:00
images = glob.glob("/data/bm29/inhouse/opd29/20140430/raw/water_008_*.edf")
2014-04-30 20:43:21 +08:00
images.sort()
img = images[0]
2014-04-30 20:46:04 +08:00
xml = etree.parse(os.path.splitext(img)[0] + ".xml")
2014-04-30 20:43:21 +08:00
wl = float(xml.xpath("//wavelength")[0].getchildren()[0].text)
centerX = float(xml.xpath("//beamCenter_1")[0].getchildren()[0].text)
centerY = float(xml.xpath("//beamCenter_2")[0].getchildren()[0].text)
directDist = float(xml.xpath("//detectorDistance")[0].getchildren()[0].text) * 1000.0
msk = xml.xpath("//maskFile")[0].getchildren()[0].getchildren()[0].text
msk = numpy.logical_or(fabio.open(msk).data, ai.detector.mask)
ai.setFit2D(directDist=directDist, centerX=centerX, centerY=centerY)
ai.wavelength = wl
I_splitBB = [];sigma_splitBB = [];I_splitFull = [];sigma_splitFull = [];I_nosplit = [];sigma_nosplit = []
2014-04-30 22:52:05 +08:00
for fn in images[:10]:
2014-04-30 20:43:21 +08:00
img = fabio.open(fn).data
2014-04-30 20:46:04 +08:00
xml = etree.parse(os.path.splitext(fn)[0] + ".xml")
2014-04-30 20:43:21 +08:00
monitor = float(xml.xpath("//beamStopDiode")[0].getchildren()[0].text)
print(fn, monitor);
variance = numpy.maximum(img, 1)
q, i, s = ai.integrate1d(img, 1040, unit="q_nm^-1", method="numpy", variance=variance, mask=msk, normalization_factor=monitor)
I_nosplit.append(i)
sigma_nosplit.append(s)
q, i, s = ai.integrate1d(img, 1040, unit="q_nm^-1", method="splitbbox", variance=variance, mask=msk, normalization_factor=monitor)
I_splitBB.append(i)
sigma_splitBB.append(s)
q, i, s = ai.integrate1d(img, 1040, unit="q_nm^-1", method="splitpixel", variance=variance, mask=msk, normalization_factor=monitor)
I_splitFull.append(i)
sigma_splitFull.append(s)
I_splitBB = numpy.vstack(I_splitBB)
I_splitFull = numpy.vstack(I_splitFull)
I_nosplit = numpy.vstack(I_nosplit)
sigma_nosplit = numpy.vstack(sigma_nosplit)
sigma_splitBB = numpy.vstack(sigma_splitBB)
sigma_splitFull = numpy.vstack(sigma_splitFull)
Chi2_splitBB = [];Chi2_splitFull = []; Chi2_nosplit = []
Iavg_splitFull = I_splitFull.mean(axis=0)
Iavg_splitBB = I_splitBB.mean(axis=0)
Iavg_nosplit = I_nosplit.mean(axis=0)
for i in range(I_splitBB.shape[0]):
Chi2_splitBB.append((((I_splitBB[i] - Iavg_splitBB) / sigma_splitBB[i]) ** 2).mean())
Chi2_splitFull.append((((I_splitFull[i] - Iavg_splitFull) / sigma_splitFull[i]) ** 2).mean())
Chi2_nosplit.append((((I_nosplit[i] - Iavg_nosplit) / sigma_nosplit[i]) ** 2).mean())
pylab.hist(Chi2_splitBB, 50, label="splitBB")
pylab.hist(Chi2_splitFull, 50, label="splitFull")
pylab.hist(Chi2_nosplit, 50, label="no_split")
pylab.xlabel("$\chi^2$")
pylab.ylabel("count")
pylab.legend()
pylab.show()