[AstroPy] Linearity correction by polynomial fitting
Fri Apr 19 05:31:10 CDT 2013
You can use numpy.nditer to loop over the array efficiently, have a look here:
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],
op_flags=[['readonly'], ['readwrite', 'allocate']],
op_axes=[None, [0, 1, -1])
it.operands[...] = 0
for x, y in it:
y[...] = my_fitting_function(x)
result = it.operands
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.
2013/4/18 Joe Philip Ninan <email@example.com>
> 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.
> "GNU/Linux: because a PC is a terrible thing to waste" - GNU Generation
> Joe Philip Ninan http://sites.google.com/site/jpninan/
> Research Scholar /________________\
> DAA, | Vadakeparambil |
> TIFR, | Pullad P.O. |
> Mumbai-05, India. | Kerala, India |
> Ph: +917738438212 | PIN:689548 |
> AstroPy mailing list
More information about the AstroPy