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 test_dilation_fit_transform_plot(): DilationFiltration().fit_transform_plot(images_2D, sample=0)
def test_dilation_transform(n_iterations, images, expected): dilation = DilationFiltration(n_iterations=n_iterations) assert_almost_equal(dilation.fit_transform(images), expected)
def test_dilation_errors(): n_iterations = 'a' dilation = DilationFiltration(n_iterations=n_iterations) with pytest.raises(TypeError): dilation.fit(images_2D)
def test_dilation_not_fitted(): dilation = DilationFiltration() with pytest.raises(NotFittedError): dilation.transform(images_2D)
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") a = fig.add_subplot(2,2, 2)
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(): direction = 'a'