ap-- / python-oceanoptics

*discontinued* Python module for oceanoptics spectrometers
MIT License
21 stars 11 forks source link

Nonlinearity Correction #16

Open sdickreuter opened 9 years ago

sdickreuter commented 9 years ago

I could'nt open a second pull request, so here is a patch for the implementation of the nonlinearity correction:

--- ../python-oceanoptics_upstream/oceanoptics/base.py  2015-01-14 10:22:10.573384769 +0100
+++ oceanoptics/base.py 2015-01-16 09:40:37.732968993 +0100
@@ -168,7 +170,7 @@
         only_valid_pixels : bool, optional
             only optical active pixels are returned.
         correct_nonlinearity : bool, optional
-            does nothing yet.
+            corrects for nonlinearity of CCD-Chip
         correct_darkcounts : bool, optional
             does nothing yet.
         correct_saturation : bool, optional
@@ -183,8 +185,14 @@
             data = np.array(self._request_spectrum()[self._valid_pixels], dtype=np.float64)
         else:
             data = np.array(self._request_spectrum(), dtype=np.float64)
+        if correct_nonlinearity:
+            data = data/self._calc_nonlinearity(data)
         return data

+    def _calc_nonlinearity(self, counts):
+        return sum( self._nl_factors[i] * counts**i for i in range(8) )
+
+
     def spectrum(self, raw=False, only_valid_pixels=True,
             correct_nonlinearity=True, correct_darkcounts=True,
             correct_saturation=True):
@@ -197,7 +205,7 @@
         only_valid_pixels : bool, optional
             only optical active pixels are returned.
         correct_nonlinearity : bool, optional
-            does nothing yet.
+            corrects for nonlinearity of CCD-Chip
         correct_darkcounts : bool, optional
             does nothing yet.
         correct_saturation : bool, optional
ap-- commented 9 years ago

While your implementation of the nonlinearity correction is correct, you must not apply this correction to a spectrum that was not dark-count-corrected!

So before this can be merged into master, the dark-count-correction needs to be implemented and in the code we need to be sure, that if the non-linearity-correction is enabled that the dark-count-correction is enabled too.

sdickreuter commented 9 years ago

Do you mean that the a dark spectrum has to be substracted before the nonlinearity correction ? That would mean that for using the nonlinearity one would have to inlcude a framework for saving the spectra into the oceanoptics-module, that could get ugly ...

By the way, I got the formula for the correction from this site: http://www.manualsdir.com/manuals/292553/ocean-optics-ooinlcorrect.html

ap-- commented 9 years ago

"Do you mean that the a dark spectrum has to be substracted before the nonlinearity correction ?" No. The counts of the dark pixels (the ones that are physically blocked on the CCD) need to be subtracted from the spectrum.

This is because otherwise the assumption that doubling the integration time = doubling the intensity which should be doubling the counts is wrong.

I guess in spectra suite if you only enable the non-linearity correction but not the dark count correction, the dark counts are added again, after the nonlinearity correction.

sdickreuter commented 9 years ago

Ah ok I understand, I'll have a look into this next week, should'nt be too hard to implement I guess.

ap-- commented 9 years ago

Will be resolved when PR #19 gets merged.