Skip to content

Commit

Permalink
Merge pull request #44 from stuwilkins/add_flatfield
Browse files Browse the repository at this point in the history
ENH: Added flatfield functions
  • Loading branch information
stuwilkins committed Mar 28, 2016
2 parents ef10ff0 + 6db86e8 commit 2d4c000
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ install:
h5py coverage jsonschema jinja2 sphinx pyyaml ipywidgets tzlocal prettytable boltons
pymongo=2.9 pims=0.3.3 tifffile databroker sphinx-bootstrap-theme pandoc
- source activate testenv
- ln -s /home/travis/mc/envs/testenv/lib/libgmp.so.10 /home/travis/mc/envs/testenv/lib/libgmp.so.3
#- ln -s /home/travis/mc/envs/testenv/lib/libgmp.so.10 /home/travis/mc/envs/testenv/lib/libgmp.so.3
- pip install coveralls
- pip install codecov
- pip install pytest-pep8
Expand Down
80 changes: 80 additions & 0 deletions csxtools/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,3 +229,83 @@ def get_fastccd_timestamps(header):
handler_overrides=hover)]
timestamps = img[0]['data']['fccd_image_lightfield']
return timestamps


def calculate_flatfield(image, limits=(0.6, 1.4)):
"""Calculate a flatfield from fluo data
This routine calculates the flatfield correction from fluorescence data
The image is thresholded by limits from the median value of the image.
The flatfield is then constructed from the mean of the image divided by
the masked (by NaN) image resulting in a true flatfield correction
Parameters
----------
image : array_like
This is the 2D image to convert to a flatfield correction.
limits : tuple
Pixels outwith the median value multiplied by these limits will be
excluded by setting to NaN.
Returns
-------
Array of flatfield correction.
"""

flat = image
limits = np.array(limits)
limits = np.nanmedian(image) * limits

flat[flat < limits[0]] = np.nan
flat[flat > limits[1]] = np.nan
flat = np.nanmean(flat) / flat
flat = np.rot90(flat)

return flat


def get_fastccd_flatfield(light, dark, flat=None, limits=(0.6, 1.4)):
"""Calculate a flatfield from two headers
This routine calculates the flatfield using the
:func:calculate_flatfield() function after obtaining the images from
the headers.
Parameters
----------
light : databroker header
The header containing the light images
dark : databroker header
The header from the run containin the dark images
flat : flatfield image (optional)
The array to be used for the initial flatfield
Returns
-------
array_like
Flatfield correction
"""
images = get_images_to_3D(get_fastccd_images(light, dark, flat))
images = stackmean(images)
flat = calculate_flatfield(images, limits)
removed = np.sum(np.isnan(flat))
if removed != 0:
logger.warning("Flatfield correction removed %d pixels (%.2f %%)" %
(removed, removed * 100 / flat.size))
return flat


def fccd_mask():
"""Return the initial flatfield mask for the FastCCD
Returns
-------
np.array of flatfield
"""
flat = np.ones((960, 960))
flat[120:250, 0:480] = np.nan
flat = np.rot90(flat)

return flat

0 comments on commit 2d4c000

Please sign in to comment.