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 cutout_stack(stack, mask_outer=0.2): stack = stackify(stack) _, h0, w0 = len(stack), stack[0].data.shape[0], stack[0].data.shape[1] dy, dx = (int(mask_outer * h0), int(mask_outer * w0)) if mask_outer == 0: data = np.array([img.data for img in stack]) else: data = np.array([img.data[dy:-dy, dx:-dx] for img in stack]) return LEEMStack(data)
def stack2vectors(stack, mask_outer=0.2): stack = stackify(stack) _, h0, w0 = len(stack), stack[0].data.shape[0], stack[0].data.shape[1] dy, dx = (int(mask_outer * h0), int(mask_outer * w0)) h, w = (h0 - 2 * dy, w0 - 2 * dx) if mask_outer == 0: data = np.array([img.data.flatten() for img in stack]).T else: data = np.array([img.data[dy:-dy, dx:-dx].flatten() for img in stack]).T return data, h, w
def get_max_variance_idx(stack): stack = stackify(stack) max_var = 0 max_index = 0 for i, img in enumerate(stack): var = np.var((img.data.flatten() - np.amin(img.data)) / (np.amax(img.data) - np.amin(img.data))) if var > max_var: max_var = var max_index = i return max_index
def __init__(self, stack, profile, BZ_pix, d, gamma=None, Vi=0): self.stack = stackify(stack) self.profile = profile if gamma is None: gamma = profile.position self.gamma = np.array(gamma) self.k_BZ = 2 * np.pi / d self.k_res = self.k_BZ / BZ_pix self.Vi = Vi self.data = None
def calculate_dose(stack, pressurefield="pressure1", approx=1): """Maybe get rid of approx?.""" stack = stackify(stack) pressure = getattr(stack, pressurefield)[::approx] rel_time = stack.rel_time[::approx] approx_dose = np.cumsum(pressure * np.gradient(rel_time)) * 1e6 # scale up to original length: long_dose = np.repeat(approx_dose, approx) cutoff = len(long_dose) - len(stack) if cutoff < 0: raise ValueError("Error calculating dose") return long_dose[cutoff // 2:len(stack) + cutoff // 2]
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 get_rsm(stack, cut, xy0, kpara_per_pix): stack = stackify(stack) res_y, res_x = len(stack), np.rint(cut.length).astype(int) z = np.zeros((res_y, res_x)) kx = np.zeros((res_y + 1, res_x + 1)) kx[:, :] = kpara_per_pix * cut.length * np.linspace(-0.5, 0.5, res_x + 1) ky = np.zeros((res_y + 1, res_x + 1)) kpara = get_kpara(cut, xy0, kpara_per_pix, length=res_x + 1) dE = np.mean(np.diff(stack.energy)) for i, img in enumerate(progress_bar(stack, "Calculating RSM...")): ky[i, :] = get_kperp(stack.energy[i] - dE / 2, kpara) z[i, :] = np.log(cut(img.data, length=res_x)) ky[-1, :] = get_kperp(stack.energy[-1] + dE / 2, kpara) return kx, ky, z
def align(stack, **kwargs): """ Use these keyword arguments: algorithm={"ecc"} roi: defines a ROI, which can be any shape (circle, rectangle...). Defaults to 15% rectangular cutoff for ecc: trafo={"translation","rigid","affine"} (default=translation) max_iter=int maximum iterations, default: 500 eps=number threshold to reach, default: 1e-4 avg=int alignment ist averaged by matching with avg Number of previous images, default: 1 """ stack = stackify(stack) alignment = find_alignment_matrices(stack, **kwargs) stack = apply_alignment_matrices(stack, alignment) stack.alignment = alignment return stack, alignment
def rsm(stack, start, end, xy0, kpara_per_pix=7.67e7): stack = stackify(stack) cut = RSMCut(start=start, end=end) kx, ky, z = get_rsm(stack, cut, xy0=xy0, kpara_per_pix=kpara_per_pix) return kx, ky, z
def test_stackify(stack, stack_fname): assert isinstance(utility.stackify(stack), base.LEEMStack) assert isinstance(utility.stackify(stack_fname), base.LEEMStack)