# [SciPy-User] Re[SciPy-user] binning to polar coordinates

Jerome Kieffer Jerome.Kieffer@esrf...
Fri Aug 31 03:31:15 CDT 2012

```On Fri, 31 Aug 2012 00:36:21 -0700 (PDT)
arsbbr <arsbbr@gmx.net> wrote:

> thank you for your tip! Unfortunately I'm getting big gaps for my real data
> in the small rho region. The optimal algorithm should interpolate in the
> small rho region and bin in the high rho region.
> I guess your split pixel solution would be best. But this a whole API with a
> lot of corrections and methods for real experimental data. Is it possible to
> use your sophisticated algorithm only for the transformation from Cartesian
> to a evenly stepped polar grid? Speed is not the issue with my data, so a
> numpy solution would be fine.

I wish this would be possible using only numpy but I failed.
I had to write it in a low-level (Cython) way to get decent speed because the algorithm is iterating over all input pixels.

What you are looking for is in:
https://github.com/kif/pyFAI/blob/master/src/splitBBox.pyx

Use the function histoBBox2d(weights,
pos0, delta_pos0,
pos1, delta_pos1,
bins=(100, 36))

If you refer to the manual of numpy.histogram2d:
-> bins has the same meaning, i.e. the shape of the output image
-> pos0 and pos1 are the same a x and y; they should contain the coordinated in (rho,theta) of the center of input pixels.
-> delta_pos0 and delta_pos1 are the distance in (rho,theta) from pixel center to the pixel corner. This is of course new compared to numpy
-> weights is as in numpy the intensity of input pixels.
All array need to have the same size.

The output is:
-> your transformed image (maybe it's transposed), i.e. weighted_histogram/unweighted_histogram
-> axis in rho (or theta) 1D
-> axis in theta (or rho) 1D
-> weighted histogram (2D)
-> unweighted histogram (2D)

splitBBox should be stand alone: just use this setup.py to compile the module (needs Cython)
########################################
try:
from setuptools import setup
except ImportError:
from distutils.core import setup

from distutils.core import  Extension
from Cython.Distutils import build_ext

# for numpy
from numpy.distutils.misc_util import get_numpy_include_dirs

split_ext = Extension("splitBBox",
include_dirs=get_numpy_include_dirs(),
sources=['splitBBox.pyx'],

setup(name='histogram',
version="0.3.0",
author="Jerome Kieffer",
author_email="jerome.kieffer@esrf.eu",
description='test for azim int',
ext_modules=[split_ext],
cmdclass={'build_ext': build_ext},
)
#####################################################
Nota:
I am working on X-ray diffraction so
Your "rho" could be named "2theta", tth or q.