Skip to content

Commit

Permalink
Merge d5dcccf into 2d4c000
Browse files Browse the repository at this point in the history
  • Loading branch information
stuwilkins committed Mar 28, 2016
2 parents 2d4c000 + d5dcccf commit e21761d
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion csxtools/image/stack.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import numpy as np
from ..ext import image as extimage

import logging
logger = logging.getLogger(__name__)


def stackmean(array):
"""Cacluate the mean of a stack
Expand All @@ -24,25 +27,44 @@ def stackmean(array):
return X


def stacksum(array):
def stacksum(array, norm=False):
"""Cacluate the sum of a stack
This function calculates the sum of a stack of images (or any array).
It ignores values that are np.NAN and does not include them in the sum
calculation. It assumes an array of shape (.. i, j, x, y) where x and y
are the size of the returned array (x, y).
If norm is True then the output sum is corrected for elements where NaNs
are encountered and renormalized to the value expected based on the stack
size.
Parameters
----------
array : array_like
Input array of at least 3 dimensions.
norm : bool
If true then normalize output to account for NaNs.
Returns
-------
tuple
tuple of 2 arrays of the sum and number of points in the sum
"""
X, Y = extimage.stackprocess(array, 0)

total_elements = array.size / (array.shape[-1] * array.shape[-2])

if not norm and np.sum(Y != total_elements):
logger.warning("stacksum encountered NaN values and excluded these "
"values from the sum. Consider using the number of "
"points, to renormalize the image. Hint: use "
"norm=True")

if norm:
X = X * (total_elements / Y)

return X, Y


Expand Down

0 comments on commit e21761d

Please sign in to comment.