Example #1
0
    def test_cython_vs_numpy(self):
        """
        Compare cython histogram with numpy histogram
        """
#        logger.info(self.ai.__repr__())
        data = self.data
        tth_np, I_np = self.ai.xrpd_numpy(data,
                                     len(self.fit2d), correctSolidAngle=False)
        tth_cy, I_cy = self.ai.xrpd_cython(data,
                                     len(self.fit2d), correctSolidAngle=False)
        logger.info("before xrpd_splitPixel")
        tth_sp, I_sp = self.ai.xrpd_splitPixel(data,
                                     len(self.fit2d), correctSolidAngle=False)
        logger.info("After xrpd_splitPixel")
        rwp = Rwp((tth_cy, I_cy), (tth_np, I_np))
        logger.info("Rwp = %.3f" % rwp)
        if logger.getEffectiveLevel() == logging.DEBUG:
            logging.info("Plotting results")
            fig = pylab.figure()
            fig.suptitle('Numpy Histogram vs Cython: Rwp=%.3f' % rwp)
            sp = fig.add_subplot(111)
            sp.plot(self.fit2d.T[0], self.fit2d.T[1], "-y", label='fit2d')
            sp.plot(tth_np, I_np, "-b", label='numpy')
            sp.plot(tth_cy, I_cy , "-r", label="cython")
            sp.plot(tth_sp, I_sp , "-g", label="SplitPixel")
            handles, labels = sp.get_legend_handles_labels()
            fig.legend(handles, labels)
            fig.show()
            raw_input("Press enter to quit")

        assert rwp < 3
 def test_cython_vs_fit2d(self):
     """
     Compare cython histogram with results of fit2d
     """
     #        logger.info(self.ai.__repr__())
     tth, I = self.ai.xrpd_cython(self.data,
                                  len(self.fit2d),
                                  "tmp/cython.dat",
                                  correctSolidAngle=False,
                                  pixelSize=None)
     #        logger.info(tth)
     #        logger.info(I)
     rwp = Rwp((tth, I), self.fit2d.T)
     logger.info("Rwp cython/fit2d = %.3f" % rwp)
     if logger.getEffectiveLevel() == logging.DEBUG:
         logger.info("Plotting results")
         fig = pylab.figure()
         fig.suptitle('Cython Histogram vs Fit2D: Rwp=%.3f' % rwp)
         sp = fig.add_subplot(111)
         sp.plot(self.fit2d.T[0], self.fit2d.T[1], "-b", label='fit2d')
         sp.plot(tth, I, "-r", label="cython")
         handles, labels = sp.get_legend_handles_labels()
         fig.legend(handles, labels)
         fig.show()
         raw_input("Press enter to quit")
     assert rwp < 11
Example #3
0
    def test_cythonSP_vs_fit2d(self):
        """
        Compare cython splitPixel with results of fit2d
        """
        logger.info(self.ai.__repr__())
        pos = self.ai.cornerArray(self.data.shape)
        t0 = time.time()
        logger.info("in test_cythonSP_vs_fit2d Before SP")

        tth, I = self.ai.xrpd_splitPixel(self.data,
                                     len(self.fit2d), self.tmpfiles["cythonSP"], correctSolidAngle=False)
        logger.info("in test_cythonSP_vs_fit2d Before")
        t1 = time.time() - t0
#        logger.info(tth)
#        logger.info(I)
        rwp = Rwp((tth, I), self.fit2d.T)
        logger.info("Rwp cythonSP(t=%.3fs)/fit2d = %.3f" % (t1, rwp))
        if logger.getEffectiveLevel() == logging.DEBUG:
            logger.info("Plotting results")
            fig = pylab.figure()
            fig.suptitle('CythonSP Histogram vs Fit2D: Rwp=%.3f' % rwp)
            sp = fig.add_subplot(111)
            sp.plot(self.fit2d.T[0], self.fit2d.T[1], "-b", label='fit2d')
            sp.plot(tth, I, "-r", label="cython")
            handles, labels = sp.get_legend_handles_labels()
            fig.legend(handles, labels)
            fig.show()
            raw_input("Press enter to quit")
        assert rwp < 11
Example #4
0
    def test_OpenCL(self):
        logger.info("Testing histogram-based algorithm (forward-integration)")
        for devtype in ("GPU", "CPU"):
            ids = ocl.select_device(devtype,
                                    extensions=["cl_khr_int64_base_atomics"])
            if ids is None:
                logger.error("No suitable %s OpenCL device found" % devtype)
                continue
            else:
                logger.info("I found a suitable device %s %s: %s %s " %
                            (devtype, ids, ocl.platforms[ids[0]],
                             ocl.platforms[ids[0]].devices[ids[1]]))

            for ds in self.datasets:
                ai = pyFAI.load(ds["poni"])
                data = fabio.open(ds["img"]).data
                res = ai.xrpd_OpenCL(data,
                                     1000,
                                     devicetype="all",
                                     platformid=ids[0],
                                     deviceid=ids[1],
                                     useFp64=True)
                t0 = time.time()
                ref = ai.xrpd(data, 1000)
                t1 = time.time()
                res = ai.xrpd_OpenCL(data, 1000, safe=False)
                t2 = time.time()
                logger.info(
                    "For image %15s;\tspeed up is %.3fx;\trate is %.3f Hz" %
                    (os.path.basename(ds["img"]),
                     ((t1 - t0) / (t2 - t1)), 1. / (t2 - t1)))
                r = Rwp(ref, res)
                self.assertTrue(
                    r < 10, "Rwp=%.3f for OpenCL processing of %s" % (r, ds))
Example #5
0
    def test_OpenCL(self):
        logger.info("Testing histogram-based algorithm (forward-integration)")
        for devtype in ("GPU", "CPU"):
            ids = ocl.select_device(devtype,
                                    extensions=["cl_khr_int64_base_atomics"])
            if ids is None:
                logger.error("No suitable %s OpenCL device found" % devtype)
                continue
            else:
                logger.info("I found a suitable device %s %s: %s %s " %
                            (devtype, ids, ocl.platforms[ids[0]],
                             ocl.platforms[ids[0]].devices[ids[1]]))

            for ds in self.datasets:
                ai = pyFAI.load(ds["poni"])
                data = fabio.open(ds["img"]).data
                res = ai.xrpd_OpenCL(data,
                                     self.N,
                                     devicetype="all",
                                     platformid=ids[0],
                                     deviceid=ids[1],
                                     useFp64=True)
                ref = ai.integrate1d(data,
                                     self.N,
                                     method="splitBBox",
                                     unit="2th_deg")
                r = Rwp(ref, res)
                logger.info(
                    "OpenCL histogram vs histogram SplitBBox has R= %.3f for dataset %s"
                    % (r, ds))
                self.assertTrue(
                    r < 6,
                    "Rwp=%.3f for OpenCL histogram processing of %s" % (r, ds))
                del ai, data
                gc.collect()
Example #6
0
 def testQ(self):
     res = {}
     for m in ("numpy", "cython", "BBox", "splitpixel", "lut", "lut_ocl"):
         res[m] = self.ai.integrate1d(self.data,
                                      self.npt,
                                      method=m,
                                      radial_range=(0.5, 5.8))
     for a in res:
         for b in res:
             R = Rwp(res[a], res[b])
             mesg = "testQ: %s vs %s measured R=%s<%s" % (a, b, R,
                                                          self.Rmax)
             if R > self.Rmax:
                 logger.error(mesg)
             else:
                 logger.info(mesg)
             self.assertTrue(R <= self.Rmax, mesg)
Example #7
0
 def testCython(self):
     qref, Iref, s = self.ai.saxs(self.data, self.npt)
     q, I, s = self.ai.saxs(self.data,
                            self.npt,
                            error_model="poisson",
                            method="cython")
     self.assertTrue(q[0] > 0, "q[0]>0 %s" % q[0])
     self.assertTrue(q[-1] < 8, "q[-1] < 8, got %s" % q[-1])
     self.assertTrue(s.min() >= 0, "s.min() >= 0 got %s" % (s.min()))
     self.assertTrue(s.max() < 21, "s.max() < 21 got %s" % (s.max()))
     self.assertTrue(I.max() < 52000, "I.max() < 52000 got %s" % (I.max()))
     self.assertTrue(I.min() >= 0, "I.min() >= 0 got %s" % (I.min()))
     R = Rwp((q, I), (qref, Iref))
     if R > 20: logger.error("Cython has R=%s" % R)
     if logger.getEffectiveLevel() == logging.DEBUG:
         pylab.errorbar(q, I, s, label="Cython R=%.1f" % R)
         pylab.yscale("log")
     self.assertTrue(R < 20, "Cython: Measure R=%s<2" % R)
Example #8
0
    def test_OpenCL_LUT(self):
        logger.info("Testing LUT-based algorithm (backward-integration)")
        for devtype in ("GPU", "CPU"):
            ids = ocl.select_device(devtype, best=True)
            if ids is None:
                logger.error("No suitable %s OpenCL device found" % devtype)
                continue
            else:
                logger.info("I found a suitable device %s %s: %s %s " %
                            (devtype, ids, ocl.platforms[ids[0]],
                             ocl.platforms[ids[0]].devices[ids[1]]))

            for ds in self.datasets:
                ai = pyFAI.load(ds["poni"])
                data = fabio.open(ds["img"]).data
                ref = ai.integrate1d(data,
                                     self.N,
                                     method="splitBBox",
                                     unit="2th_deg")
                try:
                    res = ai.integrate1d(data,
                                         self.N,
                                         method="ocl_lut_%i,%i" %
                                         (ids[0], ids[1]),
                                         unit="2th_deg")
                except (pyFAI.opencl.pyopencl.MemoryError, MemoryError,
                        pyFAI.opencl.pyopencl.RuntimeError,
                        RuntimeError) as error:
                    logger.warning(
                        "Memory error on %s dataset %s: %s%s. Converted into warnining: device may not have enough memory."
                        % (devtype, os.path.basename(
                            ds["img"]), os.linesep, error))
                    break
                else:
                    ref = ai.xrpd(data, self.N)
                    r = Rwp(ref, res)
                    logger.info(
                        "OpenCL CSR vs histogram SplitBBox has R= %.3f for dataset %s"
                        % (r, ds))
                    self.assertTrue(
                        r < 3,
                        "Rwp=%.3f for OpenCL LUT processing of %s" % (r, ds))
                del ai, data
                gc.collect()
Example #9
0
    def test_numpy_vs_fit2d(self):
        """
        Compare numpy histogram with results of fit2d
        """
#        logger.info(self.ai.__repr__())
        tth, I = self.ai.xrpd_numpy(self.data,
                                     len(self.fit2d), self.tmpfiles["numpy"], correctSolidAngle=False)
        rwp = Rwp((tth, I), self.fit2d.T)
        logger.info("Rwp numpy/fit2d = %.3f" % rwp)
        if logger.getEffectiveLevel() == logging.DEBUG:
            logger.info("Plotting results")
            fig = pylab.figure()
            fig.suptitle('Numpy Histogram vs Fit2D: Rwp=%.3f' % rwp)
            sp = fig.add_subplot(111)
            sp.plot(self.fit2d.T[0], self.fit2d.T[1], "-b", label='fit2d')
            sp.plot(tth, I, "-r", label="numpy histogram")
            handles, labels = sp.get_legend_handles_labels()
            fig.legend(handles, labels)
            fig.show()
            raw_input("Press enter to quit")
        assert rwp < 11
Example #10
0
    def test_OpenCL_LUT(self):
        logger.info("Testing LUT-based algorithm (backward-integration)")
        for devtype in ("GPU", "CPU"):
            ids = ocl.select_device(devtype, best=True)
            if ids is None:
                logger.error("No suitable %s OpenCL device found" % devtype)
                continue
            else:
                logger.info("I found a suitable device %s %s: %s %s " %
                            (devtype, ids, ocl.platforms[ids[0]],
                             ocl.platforms[ids[0]].devices[ids[1]]))

            for ds in self.datasets:
                ai = pyFAI.load(ds["poni"])
                data = fabio.open(ds["img"]).data
                try:
                    res = ai.xrpd_LUT_OCL(data,
                                          1000,
                                          devicetype="all",
                                          platformid=ids[0],
                                          deviceid=ids[1])
                except MemoryError as error:
                    logger.warning(
                        "Memory Error on %s dataset %s: %s%s. Converted into warnining: device may not have enough memory."
                        % (devtype, os.path.basename(
                            ds["img"]), os.linesep, error))
                else:
                    t0 = time.time()
                    ref = ai.xrpd(data, 1000)
                    t1 = time.time()
                    res = ai.xrpd_LUT_OCL(data, 1000, safe=False)
                    t2 = time.time()
                    logger.info(
                        "For image %15s;\tspeed up is %.3fx;\trate is %.3f Hz"
                        % (os.path.basename(ds["img"]),
                           ((t1 - t0) / (t2 - t1)), 1. / (t2 - t1)))
                    r = Rwp(ref, res)
                    self.assertTrue(
                        r < 10,
                        "Rwp=%.3f for OpenCL processing of %s" % (r, ds))
Example #11
0
 def test_numpy_vs_cython_1d(self):
     """
     Compare numpy histogram with cython simple implementation
     """
     max_delta = abs(self.bins_numpy - self.bins_cython).max()
     logger.info("Bin-center position for cython/numpy, max delta=%s",
                 max_delta)
     self.assert_(
         max_delta < self.epsilon,
         "Bin-center position for cython/numpy, max delta=%s" % max_delta)
     rwp = Rwp((self.bins_cython, self.I_cython),
               (self.bins_numpy, self.I_numpy))
     logger.info("Rwp Cython/Numpy = %.3f" % rwp)
     self.assert_(rwp < 5.0, "Rwp Cython/Numpy = %.3f" % rwp)
     if logger.getEffectiveLevel() == logging.DEBUG:
         logger.info("Plotting results")
         fig = pylab.figure()
         fig.suptitle('Numpy vs Cython XRPD R=%.3f' % rwp)
         sp = fig.add_subplot(111)
         sp.plot(self.bins_numpy, self.I_numpy, "-b", label='numpy')
         sp.plot(self.bins_cython, self.I_cython, "-r", label="cython")
         handles, labels = sp.get_legend_handles_labels()
         fig.legend(handles, labels)
         fig.show()
         raw_input("Press enter to quit")
     delta_max = abs(self.unweight_numpy - self.unweight_cython).max()
     logger.info("pixel count difference numpy/cython : max delta=%s",
                 delta_max)
     self.assert_(delta_max < 2,
                  "numpy_vs_cython_1d max delta unweight = %s" % delta_max)
     delta_max = abs(self.I_cython - self.I_numpy).max()
     logger.info("Intensity count difference numpy/cython : max delta=%s",
                 delta_max)
     self.assert_(
         delta_max < self.epsilon,
         "Intensity count difference numpy/cython : max delta=%s" %
         delta_max)
Example #12
0
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
import utilstest
import pyFAI, numpy
try:
    from pyFAI.third_party import six
except (ImportError, Exception):
    import six
img = numpy.zeros((512, 512))
for i in range(1, 6):
    img[i * 100, i * 100] = 1
det = pyFAI.detectors.Detector(1e-4, 1e-4)
det.shape = (512, 512)
ai = pyFAI.AzimuthalIntegrator(1, detector=det)
import pylab
from utilstest import Rwp
results = {}
for i, meth in enumerate(
    ["cython", "splitbbox", "splitpixel", "csr_no", "csr_bbox", "csr_full"]):
    tth, I = ai.integrate1d(img, 10000, method=meth, unit="2th_deg")
    pylab.plot(tth, I + i * 1e-3, label=meth)
    ai.reset()
    results[meth] = tth, I
print("no_split R=%.3f" % Rwp(results["csr_no"], results["cython"]))
print("split_bbox R=%.3f" % Rwp(results["csr_bbox"], results["splitbbox"]))
print("split_full R=%.3f" % Rwp(results["csr_full"], results["splitpixel"]))
pylab.legend()
pylab.ion()
pylab.show()
six.moves.input("enter_to_quit")