def get_persistent_entropy(point_clouds): ''' Creates Vietoris Rips Filtration and calculates Persistent Entropy Returns ------- List with persistent entropy of 0th homology group for each series ''' vietorisrips_tr = VietorisRipsPersistence( metric='manhattan', homology_dimensions=_homology_dimensions, max_edge_length=_max_edge_length, n_jobs=_n_jobs, ) diagrams = vietorisrips_tr.fit_transform(point_clouds) entropy_tr = PersistenceEntropy() features = entropy_tr.fit_transform(diagrams) return features
def fpd_cluster(data, c, hom_dimension, metric='wasserstein', verbose=False, max_iter=10, frand='no', fuzzy=True): # Compute topological fuzzy clusters of a collection of point clouds # # INPUTS # data - collection of datasets # c - number of clusters # verbose - True or False to give iteration information # max_iter - max number of iterations to compute # p - dimension of persistence diagram (0=connected components, 1=holes, 2=voids, etc.) # max_range - Max distance to consider between points for VR complex # T - replace points at infinity with large hyperparameter T # frand - optional Fuzzy RAND reference matrix # fuzzy - fuzzy clustering if True, hard clustering if False # (if unsure of value for max_range or T, set as the furthest distance between two points) # # OUTPUTS # r - membership values # M - list of cluster centres # frand_indices - returns Fuzzy RAND index at each iteration (if reference matrix given) VR = VietorisRipsPersistence(homology_dimensions=[hom_dimension]) diagrams = VR.fit_transform(data) # diagrams = np.delete(diagrams, axis=2, obj=2) r, M = pd_fuzzy(diagrams, c, verbose, max_iter, frand=frand, fuzzy=fuzzy, metric=metric) return r, M
def get_diagrams_torch(point_clouds, maxdim = 1): # Calculates persistence diagrams from point clouds. # Complexity of calculation increase with the maximum homology dimension, taken into account # point_clouds - pytorch tensor of the shape (n_samples, n_points, dim) # maxdim - maximum homology dimension # Returns tuple (diagrams_torch, diagrams_np, VR_persistence) #diagrams_torch - pytorch tensors of the shape (n_samples, maxdim + 1, n_features, 2) # n_features - maximum number of topological features, across different samples. # The last axis has the structure [birth_scale, death_scale] # The last two elements in tuple are needed for plotting diagrams only homology_dimensions = tuple(range(maxdim + 1)) VR_persistence = VietorisRipsPersistence(homology_dimensions = homology_dimensions) point_clouds_np = point_clouds.numpy() diagrams_np = VR_persistence.fit_transform(point_clouds_np) homology_dimensions = diagrams_np[:, :, 2, np.newaxis] diagrams_torch = [] for i in range(maxdim + 1): diagrams_fixed_Hdim = np.select([homology_dimensions == i], [diagrams_np[:, :, :2]]) diagrams_torch.append(torch.FloatTensor(diagrams_fixed_Hdim[:, np.newaxis, :, :])) diagrams_torch = torch.cat(tuple(diagrams_torch), dim=1) return diagrams_torch, diagrams_np, VR_persistence
def test_vrp_low_infinity_values(X, metric): vrp = VietorisRipsPersistence(max_edge_length=0.001, metric=metric, infinity_values=-1) assert_almost_equal(vrp.fit_transform(X)[:, :, :2], np.zeros((1, 2, 2)))
def test_vrp_list_of_arrays_different_size(): X_2 = np.array([[0., 1.], [1., 2.]]) vrp = VietorisRipsPersistence() assert_almost_equal(vrp.fit_transform([X_pc[0], X_2])[0], X_vrp_exp[0])
# ``X_sw`` is now a complicated-looking array, but it has a simple interpretation. Again, ``X_sw[i]`` is the ``i``-th window on ``X``, and it contains ``window_size`` samples from the original time series. This time, the samples are not scalars but 1D arrays. # # What if we suspect that the way in which the **correlations** between the variables evolve over time can help forecast the target ``y``? This is a common situation in neuroscience, where each variable could be data from a single EEG sensor, for instance. # # ``giotto-tda`` exposes a ``PearsonDissimilarity`` transformer which creates a 2D dissimilarity matrix from each window in ``X_sw``, and stacks them together into a single 3D object. This is the correct format (and information content!) for a typical topological transformer in ``gtda.homology``. See also [Topological feature extraction from graphs](https://github.com/giotto-ai/giotto-tda/blob/master/examples/persistent_homology_graphs.ipynb) for an in-depth look. Finally, we can extract simple scalar features using a selection of transformers in ``gtda.diagrams``. # In[6]: from gtda.time_series import PearsonDissimilarity from gtda.homology import VietorisRipsPersistence from gtda.diagrams import Amplitude PD = PearsonDissimilarity() X_pd = PD.fit_transform(X_sw) VR = VietorisRipsPersistence(metric="precomputed") X_vr = VR.fit_transform(X_pd) # "precomputed" required on dissimilarity data Ampl = Amplitude() X_a = Ampl.fit_transform(X_vr) X_vr # Notice that we are not acting on ``y`` above. We are simply creating features from each window using topology! *Note*: it's two features per window because we used the default value for ``homology_dimensions`` in ``VietorisRipsPersistence``, not because we had two variables in the time series initially! # # We can now put this all together into a ``giotto-tda`` ``Pipeline`` which combines both the sliding window transformation on ``X`` and resampling of ``y`` with the feature extraction from the windows on ``X``. # # *Note*: while we could import the ``Pipeline`` class and use its constructor, we use the convenience function ``make_pipeline`` instead, which is a drop-in replacement for [scikit-learn's](https://scikit-learn.org/stable/modules/generated/sklearn.pipeline.make_pipeline.html). # # 请注意,我们没有对上面的“y”采取行动。我们只是在使用拓扑从每个窗口创建特征! *注意*:每个窗口有两个特征,因为我们在“VietorisRipsPersistence”中使用了“ homology_dimensions”的默认值,而不是因为我们最初在时间序列中有两个变量! # # 现在我们可以将所有这些放到giotto-tda“Pipeline”中,它将“X”上的滑动窗口转换和“y”的重采样与从“X”的Windows窗口上提取的特征结合在一起。 # # *注意*:虽然我们可以导入“ Pipeline”类并使用其构造函数,但我们使用便利功能“ make_pipeline”代替,它是[scikit-learn's](https:// scikit-learn.org/stable/modules/generation/sklearn.pipeline.make_pipeline.html)。
RGB color; the keyword argument name must be a standard mpl colormap name.''' return plt.cm.get_cmap(name, n) #cmap = get_cmap(1000000) knots = [] for index,i in enumerate([4000,5000,6000,7000]): for j in range(5): X = np.random.randn(i,3) / 1000 X[:,0] += np.cos(np.arange(i)*2*np.pi/i) X[:,1] += np.sin(np.arange(i)*2*np.pi/i) Z = TSNE(n_jobs=-1, init='random', random_state=np.random.randint(5,42)).fit_transform(X) plt.scatter(Z[:,0] , Z[:,1] , c = np.random.rand(3,)) plt.show() knots.append(Z) homology_dimensions = (0, 1) VR = VietorisRipsPersistence( metric='euclidean', homology_dimensions=homology_dimensions) # Array of persistence diagrams, one per point cloud in the input diagrams = VR.fit_transform(knots) PE = PersistenceEntropy() F = PE.fit_transform(diagrams) C = AgglomerativeClustering(n_clusters=5).fit_transform(X) print(C.labels_)
(3 * (np.arctan(np.cos(t) / np.sin(t)))) / 2) ] for t in range(1, 400)]) twistedcircle = np.concatenate((twistedcircle1, twistedcircle2)) plot_point_cloud(twistedcircle) #Consider a specific example. twistedcircle1 = np.asarray( [[np.sin(t), np.cos(t), np.cos(((np.arctan(np.cos(t) / np.sin(t)))) / 2)] for t in range(1, 400)]) twistedcircle2 = np.asarray( [[np.sin(t), np.cos(t), -np.cos(((np.arctan(np.cos(t) / np.sin(t)))) / 2)] for t in range(1, 400)]) twistedcircle = np.concatenate((twistedcircle1, twistedcircle2)) plot_point_cloud(twistedcircle) # The homology ranks we choose to consider homology_dimensions = (0, 1) VR = VietorisRipsPersistence(metric='euclidean', max_edge_length=10, homology_dimensions=homology_dimensions) # Creating persistence diagrams, one per point cloud in the input. diagrams = VR.fit_transform([twistedcircle]) print(f'diagrams.shape = {diagrams.shape}') # Plotting the persistence diagram of the twisted circle. plot_diagram(diagrams[0])
# Representing the circle in 3d with parametric equations. circle = np.asarray([[np.sin(t), np.cos(t), 0] for t in range(400)]) plot_point_cloud(circle) # Representing the sphere in 3d with parametric equations sphere = np.asarray([[np.cos(s) * np.cos(t), np.cos(s) * np.sin(t), np.sin(s)] for t in range(20) for s in range(20)]) plot_point_cloud(sphere) # Representing the torus in 3d with parametric equations torus = np.asarray([[(2 + np.cos(s)) * np.cos(t), (2 + np.cos(s)) * np.sin(t), np.sin(s)] for t in range(20) for s in range(20)]) plot_point_cloud(torus) # Saving the results into an array topological_spaces = np.asarray([circle, sphere, torus]) # The homology ranks we choose to consider homology_dimensions = (0, 1, 2) VR = VietorisRipsPersistence(metric='euclidean', max_edge_length=10, homology_dimensions=homology_dimensions) # Array of persistence diagrams, one per point cloud in the input diagrams = VR.fit_transform(topological_spaces) print(f'diagrams.shape = {diagrams.shape}') # Plotting the persistence diagram of the circle plot_diagram(diagrams[0])