Skip to content

Commit

Permalink
add unit test for all calibrants fake images
Browse files Browse the repository at this point in the history
a fake image is generated for all detectors.
a with_plot parameter can be set to True to generate a fake.pdf
file with all the images.
  • Loading branch information
picca authored and kif committed Oct 23, 2014
1 parent bc89d56 commit 06747e5
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 19 deletions.
14 changes: 8 additions & 6 deletions pyFAI-src/detectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,13 @@ class DetectorMeta(type):
# to modify attributes of the class *after* they have been
# created
def __init__(cls, name, bases, dct):
cls.registry[name.lower()] = cls
if hasattr(cls, "aliases"):
for alias in cls.aliases:
cls.registry[alias.lower().replace(" ", "_")] = cls
cls.registry[alias.lower().replace(" ", "")] = cls
if hasattr(cls, 'MAX_SHAPE'):
cls.registry[name.lower()] = cls
if hasattr(cls, "aliases"):
for alias in cls.aliases:
cls.registry[alias.lower().replace(" ", "_")] = cls
cls.registry[alias.lower().replace(" ", "")] = cls

super(DetectorMeta, cls).__init__(name, bases, dct)


Expand Down Expand Up @@ -1482,7 +1484,7 @@ def __repr__(self):
class Rayonix(Detector):
force_pixel = True
BINNED_PIXEL_SIZE = {}
MAX_SHAPE = (4096 , 4096)

def __init__(self, pixel1=None, pixel2=None):
Detector.__init__(self, pixel1=pixel1, pixel2=pixel2)

Expand Down
66 changes: 53 additions & 13 deletions test/test_calibrant.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@
import unittest
import numpy
import sys

from utilstest import UtilsTest, getLogger
logger = getLogger(__file__)
pyFAI = sys.modules["pyFAI"]

from pyFAI import calibrant
from pyFAI.calibrant import Calibrant, ALL_CALIBRANTS
from pyFAI.detectors import ALL_DETECTORS


class TestCalibrant(unittest.TestCase):
Expand All @@ -43,19 +45,23 @@ class TestCalibrant(unittest.TestCase):
"""
def test_factory(self):
# by default we provide 11 calibrants
l = len(calibrant.ALL_CALIBRANTS)
l = len(ALL_CALIBRANTS)
self.assert_(l > 10, "at least 11 calibrants are available, got %s" % l)

self.assert_("LaB6" in calibrant.ALL_CALIBRANTS, "LaB6 is a calibrant")
self.assert_("LaB6" in ALL_CALIBRANTS, "LaB6 is a calibrant")

#ensure each calibrant instance is uniq
cal1 = calibrant.ALL_CALIBRANTS["LaB6"]
cal1 = ALL_CALIBRANTS["LaB6"]
cal1.wavelength = 1e-10
cal2 = calibrant.ALL_CALIBRANTS["LaB6"]
cal2 = ALL_CALIBRANTS["LaB6"]
self.assert_(cal2.wavelength is None, "calibrant is delivered without wavelength")

# check that it is possible to instanciate all calibrant
for k, v, in ALL_CALIBRANTS.items():
self.assertTrue(isinstance(v, Calibrant))

def test_2th(self):
lab6 = calibrant.ALL_CALIBRANTS["LaB6"]
lab6 = ALL_CALIBRANTS["LaB6"]
lab6.wavelength = 1.54e-10
tth = lab6.get_2th()
self.assert_(len(tth) == 25, "We expect 25 rings for LaB6")
Expand All @@ -68,14 +74,48 @@ def test_2th(self):

def test_fake(self):
"""test for fake image generation"""
det = pyFAI.detectors.detector_factory("pilatus1m")
ai = pyFAI.AzimuthalIntegrator(dist=0.1, poni1=0.1, poni2=0.1, detector=det)
lab6 = pyFAI.calibrant.ALL_CALIBRANTS["LaB6"]
lab6.set_wavelength(1e-10)
img = lab6.fake_calibration_image(ai)
self.assert_(img.max() > 0.8, "Image contains some data")
self.assert_(img.min() == 0, "Image contains some data")
with_plot = False
if with_plot:
import matplotlib
matplotlib.use('Agg')

import matplotlib.pyplot as plt

from matplotlib.backends.backend_pdf import PdfPages
from matplotlib import rcParams

pp = PdfPages('fake.pdf')
rcParams['font.size'] = 6
plt.clf()

detectors = set(ALL_DETECTORS.itervalues())
for idx, detector in enumerate(detectors):
det = detector()
# skip the big detecteors for now
if det.MAX_SHAPE[0] > 5000 or det.MAX_SHAPE[1] > 5000:
continue
ai = pyFAI.AzimuthalIntegrator(dist=0.1, poni1=0.1, poni2=0.1,
detector=detector())
calibrant = ALL_CALIBRANTS["LaB6"]
calibrant.set_wavelength(1e-10)
img = calibrant.fake_calibration_image(ai)

if with_plot:
plt.clf
plt.subplot(3, 4, idx % 12)
plt.title(det.name)
plt.imshow(img, interpolation='nearest')

if idx != 0 and idx % 12 == 0:
pp.savefig()
plt.clf()
print det.name, img.min(), img.max()
#self.assert_(img.max() != 0., "Image (%s) contains some data" % (det.name,))
#self.assert_(img.min() == 0., "Image (%s) contains some data" % (det.name,))

if with_plot:
pp.savefig()
pp.close()

def test_suite_all_calibrant():
testSuite = unittest.TestSuite()
Expand Down

0 comments on commit 06747e5

Please sign in to comment.