def cubical_persistence(images, title, plot_diagrams=False, betti_curves=False, scaled=False): homology_dimensions = (0, 1, 2) cp = CubicalPersistence( homology_dimensions=homology_dimensions, coeff=2, periodic_dimensions=None, infinity_values=None, reduced_homology=True, n_jobs=N_JOBS, ) diagrams_cubical_persistence = cp.fit_transform(images) if scaled: sc = Scaler(metric="bottleneck") diagrams_cubical_persistence = sc.fit_transform( diagrams_cubical_persistence) else: scaled_diagrams_cubical_persistence = diagrams_cubical_persistence if plot_diagrams: fig = cp.plot(diagrams_cubical_persistence) fig.update_layout(title=title) fig.show() if betti_curves: BC = BettiCurve() X_betti_curves = BC.fit_transform(diagrams_cubical_persistence) fig = BC.plot(X_betti_curves) fig.update_layout(title=title) fig.show() if title is not None: print(f"Computed CP for {title}") return diagrams_cubical_persistence
def generate_sample_representations(paths_to_patches, labels): sample_rep_dir = DOTENV_KEY2VAL["GEN_FIGURES_DIR"] + "/sample_rep/" try: os.mkdir(sample_rep_dir) except OSError: print("Creation of the directory %s failed" % sample_rep_dir) else: print("Successfully created the directory %s " % sample_rep_dir) for i, path in enumerate(paths_to_patches): patch = np.load(path) cp = CubicalPersistence( homology_dimensions=(0, 1, 2), coeff=2, periodic_dimensions=None, infinity_values=None, reduced_homology=True, n_jobs=N_JOBS, ) diagrams_cubical_persistence = cp.fit_transform( patch.reshape(1, 30, 36, 30) ) for h_dim in HOMOLOGY_DIMENSIONS: cp.plot( diagrams_cubical_persistence, homology_dimensions=[h_dim], ).update_traces( marker=dict(size=10, color=HOMOLOGY_CMAP[h_dim]), ).write_image( sample_rep_dir + f"persistence_diagram_{labels[i]}_H_{h_dim}.png", scale=SCALE, ) representation_names = [ "Persistence landscape", "Betti curve", "Persistence image", "Heat kernel", "Silhouette", ] for j, rep in enumerate(representation_names): # Have not found a better way of doing this yet. if rep == "Persistence landscape": rep = PersistenceLandscape( n_layers=N_LAYERS, n_bins=VEC_SIZE, n_jobs=N_JOBS ) elif rep == "Betti curve": rep = BettiCurve() elif rep == "Persistence image": rep = PersistenceImage( sigma=0.001, n_bins=VEC_SIZE, n_jobs=N_JOBS ) elif rep == "Heat kernel": rep = HeatKernel(sigma=0.001, n_bins=VEC_SIZE, n_jobs=N_JOBS) elif rep == "Silhouette": rep = Silhouette(power=1.0, n_bins=VEC_SIZE, n_jobs=N_JOBS) vectorial_representation = rep.fit_transform( diagrams_cubical_persistence ) if representation_names[j] in ["Persistence image", "Heat kernel"]: for h_dim in range(vectorial_representation.shape[1]): plt.imshow( vectorial_representation[0:, h_dim, :, :].reshape( VEC_SIZE, VEC_SIZE ), cmap=(HOMOLOGY_CMAP[h_dim] + "s").capitalize(), ) # plt.title( # f"{representation_names[j]} representation of a " # f"{labels[i]} patient in h_{image}" # ) plt.savefig( sample_rep_dir + f"{representation_names[j].replace(' ', '_')}" f"_{labels[i]}_h_{h_dim}.png", bbox_inches="tight", ) else: rep.plot(vectorial_representation).update_layout( title=None, margin=dict(l=0, r=0, b=0, t=0, pad=4), ).write_image( sample_rep_dir + f"{representation_names[j].replace(' ', '_')}" f"_{labels[i]}.png", scale=SCALE, ) print(f"Done plotting {labels[i]} sample")
im_filtration = radial_filtration.fit_transform(im_binarized) radplot = radial_filtration.plot(im_filtration, colorscale="jet") radplot.update_layout(template='plotly_dark') # PLOTS st.subheader("") st.write(binplot) st.subheader("Figure 1. Binarized Plot") st.subheader("") st.subheader("") st.write(radplot) st.subheader("Figure 2. Radial Filtration Plot") st.subheader("") # CUBICAL SIMPLICIAL COMPLEXES cubical_persistence = CubicalPersistence(n_jobs=-1) im_cubical = cubical_persistence.fit_transform(im_filtration) cubplot = cubical_persistence.plot(im_cubical) cubplot.update_layout(template='plotly_dark') st.subheader("") st.write(cubplot) st.subheader("Figure 3. Cubical Simplicial Complex Persistence Diagram") st.subheader("")