import math


def normStdevMask(img,mask):
	"""
	normalize an image with mean = 0 and stddev = 1.0 only inside a mask
	"""
	n1	 = mask.sum()
	if n1 == 0:
		return img
	sum1   = (img*mask).sum()
	sumsq1 = (img*img*mask).sum()
	avg1   = sum1/n1
	std1   = math.sqrt((sumsq1 - sum1*sum1/n1)/(n1-1))
	std2   = img.std()
	return (img - avg1) / std1