def persistence_landscape(persistence_diagram, title=None, plot_landscape=False): pl = PersistenceLandscape(n_layers=1, n_bins=100, n_jobs=N_JOBS) persistence_landscape = pl.fit_transform(persistence_diagram) if plot_landscape: fig = pl.plot(persistence_landscape) fig.update_layout( title=title, xaxis_title="Filtration parameter value", yaxis_title="Persistence", ).show() return persistence_landscape
def test_not_fitted(): with pytest.raises(NotFittedError): PersistenceEntropy().transform(X) with pytest.raises(NotFittedError): BettiCurve().transform(X) with pytest.raises(NotFittedError): PersistenceLandscape().transform(X) with pytest.raises(NotFittedError): HeatKernel().transform(X) with pytest.raises(NotFittedError): PersistenceImage().transform(X) with pytest.raises(NotFittedError): Silhouette().transform(X)
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")
def test_pl_transform_shape(n_bins, n_layers): pl = PersistenceLandscape(n_bins=n_bins, n_layers=n_layers) X_res = pl.fit_transform(X) assert X_res.shape == (1, pl._n_dimensions, n_layers, n_bins)
def test_fit_transform_plot_many_hom_dims(hom_dims): BettiCurve().fit_transform_plot(X, sample=0, homology_dimensions=hom_dims) PersistenceLandscape().fit_transform_plot(X, sample=0, homology_dimensions=hom_dims) Silhouette().fit_transform_plot(X, sample=0, homology_dimensions=hom_dims)
def extract_feature_(self, persistence_diagram): # As practice shows, only 1st layer of 1st homology dimension plays role persistence_landscape = PersistenceLandscape(n_jobs=-1).fit_transform( [persistence_diagram])[0, 1, 0, :] return np.array( [np.sum(persistence_landscape) / persistence_landscape.shape[0]])
def persistence_landscape(persistence_diagram): pl = PersistenceLandscape(n_layers=1, n_bins=100, n_jobs=N_JOBS) persistence_landscape = pl.fit_transform(persistence_diagram) return persistence_landscape
def get_persistence_landscape(persistence_diagram): pi = PersistenceLandscape(n_layers=N_LAYERS, n_bins=100, n_jobs=None) print("Computed persistence landscape") return pi.fit_transform(persistence_diagram)
PersistenceImage, Silhouette pio.renderers.default = 'plotly_mimetype' X = np.array([[[0., 0., 0.], [0., 1., 0.], [2., 3., 0.], [4., 6., 1.], [2., 6., 1.]]]) line_plots_traces_params = {"mode": "lines+markers"} heatmap_trace_params = {"colorscale": "viridis"} layout_params = {"title": "New title"} @pytest.mark.parametrize('transformer', [PersistenceEntropy(), NumberOfPoints(), ComplexPolynomial(), BettiCurve(), PersistenceLandscape(), HeatKernel(), PersistenceImage(), Silhouette()]) def test_not_fitted(transformer): with pytest.raises(NotFittedError): transformer.transform(X) @pytest.mark.parametrize('transformer', [HeatKernel(), PersistenceImage()]) @pytest.mark.parametrize('hom_dim_idx', [0, 1]) def test_fit_transform_plot_one_hom_dim(transformer, hom_dim_idx): plotly_params = \ {"trace": heatmap_trace_params, "layout": layout_params} transformer.fit_transform_plot( X, sample=0, homology_dimension_idx=hom_dim_idx, plotly_params=plotly_params
pio.renderers.default = 'plotly_mimetype' X = np.array([[[0., 0., 0.], [0., 1., 0.], [2., 3., 0.], [4., 6., 1.], [2., 6., 1.]]]) line_plots_traces_params = {"mode": "lines+markers"} heatmap_trace_params = {"colorscale": "viridis"} layout_params = {"title": "New title"} @pytest.mark.parametrize('transformer', [ PersistenceEntropy(), NumberOfPoints(), ComplexPolynomial(), BettiCurve(), PersistenceLandscape(), HeatKernel(), PersistenceImage(), Silhouette() ]) def test_not_fitted(transformer): with pytest.raises(NotFittedError): transformer.transform(X) @pytest.mark.parametrize('transformer', [HeatKernel(), PersistenceImage()]) @pytest.mark.parametrize('hom_dim_idx', [0, 1]) def test_fit_transform_plot_one_hom_dim(transformer, hom_dim_idx): plotly_params = \ {"trace": heatmap_trace_params, "layout": layout_params} transformer.fit_transform_plot(X,