def normalize_stack(stack, mcp, dark_counts=100): stack = stackify(stack) mcp = imgify(mcp) if not isinstance(dark_counts, (int, float, complex)): dark_counts = imgify(dark_counts) stack_normed = stack.copy() for i, img in enumerate(progress_bar(stack, "Normalizing...")): stack_normed[i] = normalize_image(img, mcp, dark_counts=dark_counts) # is this monkey-patching necessary?: stack_normed.mcp = mcp stack_normed.dark_counts = dark_counts return stack_normed
def normalize(img_or_stack, *args, **kwargs): try: img = imgify(img_or_stack) return normalize_image(img, *args, **kwargs) except FileNotFoundError: stack = stackify(img_or_stack) return normalize_stack(stack, *args, **kwargs)
def normalize_image(img, mcp, dark_counts=100): img = imgify(img) mcp = imgify(mcp) if not isinstance(dark_counts, (int, float, complex)): dark_image = imgify(dark_counts) dark_counts = dark_image.data img = img.copy() try: normed_mcp = np.clip(np.nan_to_num(mcp.data - dark_counts), 1, None) normed = (img.data - dark_counts) / normed_mcp except ValueError as e: raise ValueError( "Normalize: Resolution of MCP or dark image does not match." ) from e img.data = np.nan_to_num(np.clip(normed, 0, None)) return img
def normalize( self, mcp: Image = None, dark_counts: Union[int, float, Image] = 100, inplace: bool = False, ) -> LEEMImg: """Normalization of LEEM Images Parameters ---------- mcp : Image, optional [description], by default None dark_counts : Union[int, float, Image], optional [description], by default 100 inplace : bool, optional [description], by default False Returns ------- LEEMImg [description] """ if mcp is None: mcp = self.mcp # pylint: disable=access-member-before-definition mcp = imgify(mcp) if not isinstance(dark_counts, (int, float, complex)): dark_counts = imgify(dark_counts) result = np.nan_to_num((self - dark_counts) / (mcp - dark_counts)) if inplace: self.image = result.image self.mcp = mcp self.dark_counts = dark_counts return self result.mcp = mcp result.dark_counts = dark_counts return result
def normalize(self, mcp: Union[Image, str, None] = None, inplace: bool = False, **kwargs) -> LEEMStack: """ Normalization of images in stack The images in the stack are normalized by appling Image.normalize(mcp) to each image Parameters ---------- mcp : Image, str or None Image or filename of image if str. If 'None' the mcp attribute of the images will be used inplace : bool, default False If 'True' the stack itself will be normalized If 'False' a copy of the stack will be normalized **kwargs Additional keyword arguments are passed through to Image.normalize .. note:: Consider passing 'dark_counts' as a keyword argument, to specify non-default dark counts of the images Returns ------- LEEMStack LEEMStack with registered images. The stack itself is returned when 'inplace'=True See Also -------- LEEMImg.normalize """ # if not a copy funny things might happen when mcp is part of stack mcp = imgify(mcp).copy() if inplace: stack = self else: stack = self.copy() for img in tqdm(stack): # if an image is multiple times in a stack and inplace is True, it should not be # normalized twice, so if the mcp is already the current mcp, it will not be processed if img.mcp is None or not img.mcp == mcp: img.normalize(mcp=mcp, inplace=True, **kwargs) return stack
def test_imgify(img, img_fname): assert isinstance(utility.imgify(img), base.LEEMImg) assert isinstance(utility.imgify(img_fname), base.LEEMImg)