def pipeline1(images): """ Binarizer --> Height Filtration, Erosion Filtration, Dilation Filtration --> Cubical Persistance --> Amp, PE return: Array of pipelines """ # Pipeline parameters bin_thresholds = [np.percentile(images[0], 93) / np.max(images[0])] directions = [ np.array([np.cos(t), np.sin(t)]) for t in np.linspace(0, 2 * np.pi, 8)[:-1] ] n_iterations = np.linspace(1, 21, 5).astype(int).tolist() features = [('bottleneck', Amplitude(metric='bottleneck', n_jobs=-1)), ('PE', PersistenceEntropy(n_jobs=-1))] # Make filtrations binned_steps = [('binarizer_{}'.format(t), Binarizer(threshold=t, n_jobs=-1)) for t in bin_thresholds] filtrations = [('height_{}'.format(d), HeightFiltration(direction=d, n_jobs=-1)) for d in directions] filtrations += [('erosion_{}'.format(i), ErosionFiltration(n_iterations=i, n_jobs=-1)) for i in n_iterations] filtrations += [('dilation_{}'.format(i), DilationFiltration(n_iterations=i, n_jobs=-1)) for i in n_iterations] # Make pipelines cubical_lower = ('cubical', CubicalPersistence(n_jobs=-1)) partial_pipeline_steps = [] partial_pipeline_steps.append([cubical_lower]) partial_pipeline_steps.append([('inverter', Inverter(n_jobs=-1)), cubical_lower]) for b, f in itertools.product(binned_steps, filtrations): partial_pipeline_steps.append( [b, f, ('cubical', CubicalPersistence(n_jobs=-1))]) feature_pipelines = [] for s, f in itertools.product(partial_pipeline_steps, features): feature_pipelines.append(Pipeline(s + [f])) return feature_pipelines
def bettiCurve_pipe1(img_file): """ Pipeline 1: Binarizer --> Height Filtration --> Cubical Persistance --> Betti Curve """ img = cv2.imread(img_file) img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) # blur the image to reduce noise figure_size = 9 # the dimension of the x and y axis of the kernal. img = cv2.blur(img, (figure_size, figure_size)) shape = img.shape images = np.zeros((1, *shape)) images[0] = img bz = Binarizer(threshold=40 / 255) binned = bz.fit_transform(images) p = make_pipeline(HeightFiltration(direction=np.array([1, 1])), CubicalPersistence(), BettiCurve(n_bins=50)) return p.fit_transform(binned)
def test_height_transform(direction, images, expected): height = HeightFiltration(direction=direction) assert_almost_equal(height.fit_transform(images), expected)
def test_height_errors(): direction = 'a' height = HeightFiltration(direction=direction) with pytest.raises(TypeError): height.fit(images_2D)
def test_height_not_fitted(): height = HeightFiltration() with pytest.raises(NotFittedError): height.transform(images_2D)
def test_height_fit_transform_plot(): HeightFiltration().fit_transform_plot(images_2D, sample=0)
X_copy = np.copy(X) X_copy = X_copy.reshape((40000, 28, 28)) X[X <= .4] = 0 X[X > .4] = 1 X = X.reshape((40000, 28, 28)) X_bin = X # X_index = np.zeros((40000, 28, 28)) # for i in tqdm(range(40000)): # img = X[i] # x_arr, y_arr = np.nonzero(img) # X_index[i] = np.array([x_arr, y_arr]).T hf = HeightFiltration() df = DilationFiltration() rf = RadialFiltration(np.array([8, 15])) X_used = X_bin y_used = y ## Create the Filtrations X_hf = hf.fit_transform(X_used, y_used) X_df = df.fit_transform(X_used, y_used) X_rf = rf.fit_transform(X_used, y_used) fig = plt.figure() fig.set_size_inches((12, 9)) a = fig.add_subplot(2, 2, 1) a.set_title("Height filtration") imgplot = plt.imshow(X_hf[4], cmap="viridis")
pio.renderers.default = 'plotly_mimetype' images_2D = np.stack([np.ones((3, 4)), np.concatenate([np.ones((3, 2)), np.zeros((3, 2))], axis=1), np.zeros((3, 4))], axis=0) images_3D = np.stack([np.ones((3, 4, 2)), np.concatenate([np.ones((3, 2, 2)), np.zeros((3, 2, 2))], axis=1), np.zeros((3, 4, 2))], axis=0) @pytest.mark.parametrize("transformer", [HeightFiltration(), RadialFiltration(), DilationFiltration(), ErosionFiltration(), SignedDistanceFiltration(), DensityFiltration()]) def test_invalid_input_shape(transformer): X = np.ones((1, 1, 1, 1, 1)) with pytest.raises(ValueError, match="Input of `fit`"): transformer.fit(X) def test_height_not_fitted(): height = HeightFiltration() with pytest.raises(NotFittedError): height.transform(images_2D) def test_height_errors():