def normalize_2D_xanes_rescale(img_stack, xanes_eng, pre_edge, post_edge): x_eng = xanes_eng pre_s, pre_e = pre_edge post_s, post_e = post_edge img = deepcopy(img_stack) img_median = img_smooth(img, 3) img_flat = img.flatten() img_median_flat = img_median.flatten() img_flat_dif = np.abs(img_flat - img_median_flat) bad_pix_index = (img_flat_dif > 2) img_flat[bad_pix_index] = 0 img_norm = img_flat.reshape(img.shape) xs, xe = find_nearest(x_eng, pre_s), find_nearest(x_eng, pre_e) img_pre_avg = np.mean(img_median[xs: max(xs+1, xe)], axis=0, keepdims=True) img_pre_avg = img_smooth(img_pre_avg, 3) xs, xe = find_nearest(x_eng, post_s), find_nearest(x_eng, post_e) img_post_avg = np.mean(img_median[xs: max(xs + 1, xe)], axis=0, keepdims=True) img_post_avg[np.abs(img_post_avg) < 1e-6] = 1e6 img_post_avg = img_smooth(img_post_avg, 3) img_norm = (img_norm - img_pre_avg) / (img_post_avg - img_pre_avg) img_norm = rm_abnormal(img_norm) return img_norm
def normalize_2D_xanes_pre_edge(img_stack, xanes_eng, pre_edge): pre_s, pre_e = pre_edge img_norm = deepcopy(img_stack) s0 = img_norm.shape x_eng = xanes_eng xs, xe = find_nearest(x_eng, pre_s), find_nearest(x_eng, pre_e) if xs == xe: img_pre = img_norm[xs].reshape(1, s0[1], s0[2]) img_pre = img_smooth(img_pre, 3) img_norm = img_norm - img_pre elif xe > xs: eng_pre = x_eng[xs:xe] img_pre = img_norm[xs:xe] img_pre = img_smooth(img_pre, 3) s = img_pre.shape x_pre = eng_pre.reshape(len(eng_pre), 1) x_bar_pre = np.mean(x_pre) x_dif_pre = x_pre - x_bar_pre SSx_pre = np.dot(x_dif_pre.T, x_dif_pre) y_bar_pre = np.mean(img_pre, axis=0) p = img_pre - y_bar_pre for i in range(s[0]): p[i] = p[i] * x_dif_pre[i] SSxy_pre = np.sum(p, axis=0) b0_pre = y_bar_pre - SSxy_pre / SSx_pre * x_bar_pre b1_pre = SSxy_pre / SSx_pre for i in range(s0[0]): if not i % 10: print(f'current image: {i}') img_norm[i] = img_norm[i] - (b0_pre + b1_pre * x_eng[i]) img_norm = rm_abnormal(img_norm) return img_norm
def normalize_2D_xanes2(img_stack, xanes_eng, pre_edge, post_edge, pre_edge_only_flag=0): pre_s, pre_e = pre_edge post_s, post_e = post_edge s = img_stack.shape xs_pre, xe_pre = find_nearest(xanes_eng, pre_s), find_nearest(xanes_eng, pre_e) xs_post, xe_post = find_nearest(xanes_eng, post_s), find_nearest(xanes_eng, post_e) xe_pre = max(xs_pre+1, xe_pre) xe_post = max(xs_post+1, xe_post) img_pre_mean = np.mean(img_stack[xs_pre:xe_pre], axis=0) img_post_mean = np.mean(img_stack[xs_post:xe_post], axis=0) img_post_mean = np.squeeze(img_smooth(img_post_mean, 5)) img_post_flat = np.sort(img_post_mean.flatten()) img_post_flat = img_post_flat[img_post_flat > 0] n_post = len(img_post_flat) if not n_post: img_post_flat = np.sort(img_post_mean.flatten()) img_post_flat = img_post_flat[img_post_flat >= 0] n_post = len(img_post_flat) thresh_post = img_post_flat[int(n_post * 0.8)] index_zero = img_post_mean < thresh_post num_non_zero = np.sum(np.sum(1 - np.array(index_zero, dtype=int), axis=0), axis=0) index_zero = np.repeat(index_zero.reshape([1, s[1], s[2]]), s[0], axis=0) img = deepcopy(img_stack) img[index_zero] = 0 x = xanes_eng y = np.sum(np.sum(img, axis=1), axis=1)/num_non_zero x1 = x[xs_pre: xe_pre] y1 = y[xs_pre: xe_pre] coef1 = polyfit(x1, y1, 1) x1_mean = np.mean(x1) x2 = x[xs_post: xe_post] y2 = y[xs_post: xe_post] coef2 = polyfit(x2, y2, 1) coef2 = coef2 - coef1 x2_mean = np.mean(x2) img_norm = deepcopy(img_stack) for i in range(s[0]): img_norm[i] = img_norm[i] - (coef1[1] * (x[i]-x1_mean) + img_pre_mean) img_pre_mean = np.mean(img_norm[xs_pre:xe_pre], axis=0) img_post_mean = np.mean(img_norm[xs_post:xe_post], axis=0) img_post_mean = np.squeeze(img_smooth(img_post_mean, 5)) if not pre_edge_only_flag: # normalizing pre-edge only for i in range(s[0]): tmp = coef2[1] * (x[i]-x2_mean) + img_post_mean - img_pre_mean tmp[tmp <= 0] = 1e6 img_norm[i] = img_norm[i] / tmp img_thickness = img_post_mean - img_pre_mean img_thickness[img_thickness<0] = 0 img_norm = rm_abnormal(img_norm) return img_norm, img_thickness
def normalize_2D_xanes_pre_edge_sub_mean(img_stack, xanes_eng, pre_edge, post_edge): img_norm = deepcopy(img_stack) x_eng = deepcopy(xanes_eng) pre_s, pre_e = pre_edge post_s, post_e = post_edge xs = find_nearest(x_eng, pre_s) xe = find_nearest(x_eng, pre_e) tmp_pre = np.mean(img_norm[xs:max(xe, xs+1)], axis=0, keepdims=True) xs = find_nearest(x_eng, post_s) xe = find_nearest(x_eng, post_e) tmp_post = np.mean(img_norm[xs:max(xe, xs + 1)], axis=0, keepdims=True) img_pre_edge_sub_mean = tmp_post - tmp_pre img_pre_edge_sub_mean = rm_abnormal(img_pre_edge_sub_mean) return img_pre_edge_sub_mean
def normalize_2D_xanes_post_edge(img_stack, xanes_eng, post_edge): post_s, post_e = post_edge img_norm = deepcopy(img_stack) s0 = img_norm.shape x_eng = xanes_eng xs, xe = find_nearest(x_eng, post_s), find_nearest(x_eng, post_e) if xs == xe: img_post = img_norm[xs].reshape(1, s0[1], s0[2]) img_post = img_smooth(img_post, 3) img_norm = img_norm / img_post img_norm[np.isnan(img_norm)] = 0 img_norm[np.isinf(img_norm)] = 0 elif xe > xs: eng_post = x_eng[xs:xe] img_post = img_norm[xs:xe] img_post = img_smooth(img_post, 3) s = img_post.shape x_post = eng_post.reshape(len(eng_post), 1) x_bar_post = np.mean(x_post) x_dif_post = x_post - x_bar_post SSx_post = np.dot(x_dif_post.T, x_dif_post) y_bar_post = np.mean(img_post, axis=0) p = img_post - y_bar_post for i in range(s[0]): p[i] = p[i] * x_dif_post[i] SSxy_post = np.sum(p, axis=0) b0_post = y_bar_post - SSxy_post / SSx_post * x_bar_post b1_post = SSxy_post / SSx_post for i in range(s0[0]): tmp = np.abs(b0_post + b1_post * x_eng[i]) tmp[tmp<1e-6] = 1e6 img_norm[i] = img_norm[i] / tmp else: print('check pre-edge/post-edge energy') img_norm = rm_abnormal(img_norm) return img_norm