def stackify(stack, virtual=False): if isinstance(stack, base.LEEMStack): return stack if isinstance(stack, base.LEEMImg): return base.LEEMStack([stack]) # raise ValueError("LEEMStack expected, but got LEEMImg") return base.LEEMStack(stack, virtual=virtual)
def test_stack_constructor_nonsense(): with pytest.raises(ValueError): _ = base.LEEMStack("nonsense") with pytest.raises(FileNotFoundError): _ = base.LEEMStack(["nonsense.dat"], nolazy=True) stack = base.LEEMStack(["nonsense.dat"]) with pytest.raises(FileNotFoundError): assert stack[0].data
def test_stack_constructor_globbing(stack_folder): stack1 = base.LEEMStack(stack_folder, virtual=True) stack2 = base.LEEMStack(stack_folder + "/*.dat", virtual=True) stack3 = base.LEEMStack(stack_folder + "/", virtual=True) assert len(stack1) == len(stack2) == len(stack3) for i in [0, -1]: assert stack1[i] == stack2[i] assert stack1[i] == stack3[i] stack3 = base.LEEMStack(IMG_FNAMES_COMPATIBLE[0]) assert len(stack3) == 1
def test_stack_virtuality(stack_fname): stack2 = base.LEEMStack(stack_fname, virtual=True) if not isinstance(stack2.fnames[0], str) or not stack2.fnames[0].endswith(".dat"): assert not stack2._virtual return stack = base.LEEMStack(stack_fname, virtual=False) assert stack2._images is None assert len(stack) == len(stack2) assert stack[0].data.shape == stack2[0].data.shape assert stack[-1].meta == stack2[-1].meta
def test_stack_set_virtual(stack_fname): if "tif" in stack_fname: return stack = base.LEEMStack(stack_fname, virtual=True) assert stack._images is None stack.virtual = False assert len(stack._images) == len(stack)
def test_stack_normalize(inplace, copy, normed_img): img = base.LEEMImg(TESTDATA_DIR + "bremen.dat") if copy: stack = base.LEEMStack([img.copy(), img.copy()]) else: stack = base.LEEMStack([img, img]) ref = stack if inplace: stack.normalize(mcp=MCP_IMG_FNAME, inplace=inplace, dark_counts=100) else: stack = stack.normalize(mcp=MCP_IMG_FNAME, inplace=inplace, dark_counts=100) assert (stack is ref) == inplace for img, ref_img in zip(stack, ref): assert (img is ref_img) == inplace assert_array_equal(img.image, normed_img.image)
def main(): # pylint: disable=unused-variable import sys stack = base.LEEMStack("testdata/test_IVLEED_VO2-RuO2") stack = stack[::4] # stack = stack[::10] # rsm_calc = rsm.RSM( # stack, # [503, 519], # profile_start=[105, 303], # profile_end=[919, 710], # # kpara_per_pix=3.84e7 # kpara_per_pix=7.67e7 # ) # plt.imshow(np.log(stack[10].data)) # plt.scatter( # [503, 304, 711, 105, 919], # [510, 411, 610, 303, 710], # s=5, color="k" # ) # plt.show() # sys.exit() # for img in stack[:10]: # plt.plot(np.log(rsm_calc.get_line(img.data))) # plt.figure() # kpara = rsm_calc.get_kpara_along_line() # kperp = rsm_calc.get_kperp_along_line(stack.energy[10], kpara) # for energy in stack.energy: # plt.plot(rsm_calc.get_kperp_along_line(energy, kpara)) # plt.plot(kpara) # plt.figure() # plt.plot(kperp) # plt.figure() cut = rsm.RSMCut(start=[105, 303], end=[919, 710]) kx, ky, z = rsm.get_rsm( stack, cut, xy0=[503, 519], kpara_per_pix=7.67e7 ) plt.pcolormesh( kx * 1e-10, ky * 1e-10, z, shading="flat", cmap="gray" ) plot_stack(stack) plt.show()
def pendry_stack(): loaded = False try: stack = base.LEEMStack(TESTDATA_DIR + "pendried_stack.lstk") loaded = True except FileNotFoundError: stack = base.LEEMStack(TESTDATA_DIR + "test_stack_IV_RuO2_normed_aligned_80-130.tif") stack.energy = np.linspace(3.0, 50.0, len(stack)) X, _, _ = cluster.stack2vectors( stack, mask_outer=0.2) # cut away 20% on every side if loaded: X = stack.pendry else: # cut out a few bad frames X = np.delete(X, list((range(10))) + [160], axis=1) energy = np.delete(stack.energy, list((range(10))) + [160]) X = cluster.pendryfy(X, energy) stack.pendry = X stack.save(TESTDATA_DIR + "pendried_stack.lstk") return stack
def test_stack_constructor_nonsense(): with pytest.raises(ValueError): _ = base.LEEMStack("nonsense") with pytest.raises(FileNotFoundError): _ = base.LEEMStack(["nonsense.dat"])
def test_stack_constructor(stack_fname, virtual): stack = base.LEEMStack(stack_fname, virtual=virtual) assert isinstance(stack[0], base.LEEMImg) assert stack[0].image.shape == stack[-1].image.shape
def test_stack_pickling(stack): stack.save(TESTDATA_DIR + "test_stack.lstk") stack2 = base.LEEMStack(TESTDATA_DIR + "test_stack.lstk") assert stack == stack2
def test_stack_constructor_array(): stack = base.LEEMStack(STACK_FNAMES[0]) data = np.stack([img.data for img in stack], axis=0) stack2 = base.LEEMStack(data) assert len(stack2) == len(stack)
def test_stack_constructor_lists(): stack = base.LEEMStack(IMG_FNAMES_COMPATIBLE) stack2 = base.LEEMStack([base.LEEMImg(ifn) for ifn in IMG_FNAMES_COMPATIBLE]) assert stack == stack2 assert stack.path == "NO_PATH"
def test_stack_constructor_globbing(): stack = base.LEEMStack(TESTDATA_DIR + "*.dat") stack2 = base.LEEMStack(TESTDATA_DIR + "*") assert len(stack2) >= len(stack) stack3 = base.LEEMStack(IMG_FNAMES_COMPATIBLE[0]) assert len(stack3) == 1
def main(): # pylint: disable=unused-variable, too-many-locals import matplotlib.pyplot as plt n_components = 15 # for n_clusters in range(8, 9): n_clusters = 8 loaded = False try: # raise FileNotFoundError stack = base.LEEMStack(TESTDATA_DIR + "pendried_stack.lstk") loaded = True except FileNotFoundError: stack = base.LEEMStack(TESTDATA_DIR + "test_stack_IV_RuO2_normed_aligned_80-130.tif") stack.energy = np.linspace(3.0, 50.0, len(stack)) stack = stack[10:] X, h, w = cluster.stack2vectors( stack, mask_outer=0.2) # cut away 20% on every side if loaded: X = stack.pendry else: X = cluster.pendryfy(X, stack.energy, smoothing_params={"both": True}) stack.pendry = X stack.save(TESTDATA_DIR + "pendried_stack.lstk") trafo, inv_trafo, model = cluster.component_analysis( X, "pca", n_components=n_components) W = trafo(X) comps = cluster.vectors2stack(W, h, w) # labels, model = cluster.repeat_cluster_analysis( # W, "pc-kmeans", n_iter=50, init="random", # n_clusters=n_clusters, metric="euclidean_square" # ) labels, model = cluster.elbow_cluster_analysis(W, "pc-kmeans", start=3, end=10, init="random", n_clusters=n_clusters, metric="euclidean_square") fig, ax = plt.subplots() for Y in cluster.extract_IVs(X, labels): ax.plot(stack.energy, Y) fig3, ax3 = plt.subplots() ax3.imshow(labels.reshape(h, w)) fig4, ax4 = plt.subplots() cluster.plot_IVs(stack, labels, ax=ax4) # fig.savefig(f"{n_clusters}_pendry.png") # fig3.savefig(f"{n_clusters}_clustermap.png") # fig4.savefig(f"{n_clusters}_IVs.png") # plt.close(fig) # plt.close(fig3) # plt.close(fig4) plt.show()