# [AstroPy] Linearity correction by polynomial fitting

Sergio Pascual sergio.pasra@gmail....
Fri Apr 19 05:31:10 CDT 2013

You can use numpy.nditer to loop over the array efficiently, have a look here:

http://docs.scipy.org/doc/numpy/reference/arrays.nditer.html

If "data" is your data cube and "my_fitting_function" is a function
that takes a 1D array and returns a number,
the following code collapses your 3d array into a 2d array "result"

it = numpy.nditer([data, None],
flags=['reduce_ok', 'external_loop'],
op_axes=[None, [0, 1, -1])
it.operands[1][...] = 0

for x, y in it:
y[...] = my_fitting_function(x)

result = it.operands[1]

This is slow, but not as slow as looping directly over data.
Furthermore, nditer handles dynamic allocation
of arrays and casting. And the C API is similar, so translating to
Numpy C (which is way faster) is straightforward.

Regards, Sergio

2013/4/18 Joe Philip Ninan <indiajoe@gmail.com>
>
> Hi,
>
> Context:
> I have a data cube, which is from an up-the-ramp readout of a detector.
> (i.e. data cube with the axis X pixel,Y pixel and Time)
> In order to fit a linearity correction function which maps from detector counts to actual count, I was planning to fit a straight line on the time axis (for each pixel), upto a threshold and then fit a 2 or 3 degree polynomial to map the difference between the extrapolated straight line and the non-linear counts of the pixel above the threshold (till it hits hard saturation).
>
> Now, the issue: [I want to avoid any python loops.]
> Calculating the slope and difference could be easily implemented by numpy masked array multiplications and other ndarray arithmetic.
> But I couldn't find anything for fitting a polynomial for every pixel along an axis of an ndarray.
> Is there a tool to do this? Without implementing a python loop to loop through each pixel and calculate coefficients?
> Worst case scenario, i think i will have to write a C routine to do polynomial fit and call it from python. But i would really love if a pure python alternative exists.
>
> Thanks
> -cheers
> joe
>
>
>
>
>
> --
> /---------------------------------------------------------------
> "GNU/Linux: because a PC is a terrible thing to waste" -  GNU Generation
>
> ************************************************
> Research Scholar        /________________\