Example #1
0
    def persistent(self):

        """
        This method is used to create dionysus persistent homology
        implementation of given vectors
        :return: dionysus diagrams
        """
        vectors = self._get_vectors()

        f_lower_star = dn.fill_freudenthal(vectors)
        p = dn.homology_persistence(f_lower_star)
        dgms = dn.init_diagrams(p, f_lower_star)

        return dgms
    h1 = np.asarray(h1)
    h0 = h0.reshape((-1, 2))
    h1 = h1.reshape((-1, 2))
    return [h0, h1]


PI_null_images = []
PI_one_images = []

for i in range(images.shape[0]):
    print(i)
    f_lower_star = d.fill_freudenthal(images[i, :, :, 0].astype(float))
    f_upper_star = d.fill_freudenthal(images[i, :, :, 0].astype(float),
                                      reverse=True)
    p = d.homology_persistence(f_lower_star)
    dgms_temp = d.init_diagrams(p, f_lower_star)
    h0_temp = homology_persistent_diagrams(dgms_temp)[0]
    h1_temp = homology_persistent_diagrams(dgms_temp)[1]
    pim = PersImage(pixels=[20, 20], spread=1)
    PI_0_temp = pim.transform(h0_temp[1:, :])
    PI_1_temp = pim.transform(h1_temp)
    PI_null_images.append(PI_0_temp)
    PI_one_images.append(PI_1_temp)

PI_null_images = np.array(PI_null_images, dtype=np.float32)
PI_null_images = PI_null_images.reshape((-1, 20, 20, 1))  # reshape
PI_null_images = PI_null_images / (PI_null_images.max() / 255.0)  # normalize

PI_one_images = np.array(PI_one_images, dtype=np.float32)
PI_one_images = PI_one_images.reshape((-1, 20, 20, 1))  # reshape
PI_one_images = PI_one_images / (PI_one_images.max() / 255.0)  # normalize
Example #3
0
    ##############################
    #Compute Persistence Diagrams#
    prime = 23
    maxscale = float(sys.argv[3])
    #Choose the prime base for the coefficient field that we use to construct the persistence cohomology.
    threshold = float(sys.argv[2])
    print('Base coefficient field: Z/', prime, 'Z', sep='')
    print('Maximal scale:', float(sys.argv[3]))
    print('Persistence threshold for selecting significant cocyles:',
          threshold, '\n')

    vr = dionysus.fill_rips(dataset, 2, float(sys.argv[3]))
    #Vietoris-Rips complex
    cp = dionysus.cohomology_persistence(vr, prime, True)
    #Create the persistent cohomology based on the chosen parameters.
    dgms = dionysus.init_diagrams(cp, vr)
    #Calculate the persistent diagram using the designated coefficient field and complex.
    now = datetime.datetime.now()
    print('End Time (VR-computation):', now.strftime("%Y-%m-%d %H:%M:%S"))
    ###Plot the barcode and diagrams using matplotlib incarnation within Dionysus2. This mechanism is different in Dionysus.
    #Plots of persistence barcodes of Vietoris-Rips complex for dimension 0 and 1.
    fig = plt.figure(figsize=(5, 5), dpi=100)
    plt.title('Persistence Barcode for dim 0')
    dionysus.plot.plot_bars(dgms[0], show=True)
    pdf.savefig(fig)
    fig = plt.figure(figsize=(5, 5), dpi=100)
    plt.title('Persistence Barcode for dim 1')
    dionysus.plot.plot_bars(dgms[1], show=True)
    pdf.savefig(fig)
    plt.close('all')
Example #4
0
# vim: ft=python foldmethod=marker foldlevel=0

import dionysus as d

#---#

simplices = [([2], 4), ([1, 2], 5), ([0, 2], 6), ([0], 1), ([1], 2), ([0,
                                                                       1], 3)]
f = d.Filtration()
for vertices, time in simplices:
    f.append(d.Simplex(vertices, time))
f.sort()
for s in f:
    print(s)
#var> f

#---#

#var> f

m = d.homology_persistence(f)

#chk>

#---#

dgms = d.init_diagrams(m, f)
print(dgms)
#var> dgms
D = 20
#Here is the data insertion part


def sample_spherical(npoints, ndim=D):
    vec = np.random.randn(ndim, npoints)
    vec /= np.linalg.norm(vec, axis=0)
    return vec


dat1 = np.array(sample_spherical(100).T)
vr = dionysus.fill_rips(dat1, 2, 10)  #Vietoris-Rips complex
cp = dionysus.cohomology_persistence(vr, prime,
                                     True)  #Create the persistent cohomology
dgms = dionysus.init_diagrams(
    cp, vr
)  #Calculate the persistent diagram using the designated coefficient field.
#record for plottings..
A = np.array([0, 0, 1, 1])

for thres in [0, 0.025, 0.05, 0.1, 0.2]:
    #######Thresholding the features on the persistent diagrams describing persistent cohomology of VR complex(CC)
    dgms_tmp = denoise_dgm(dgms, thres)  #thresholding CC diagrams
    dgms_base_0 = dionysus._dionysus.Diagram(
    )  #take persistent points of dim 0
    dgms_base_1 = dionysus._dionysus.Diagram(
    )  #take persistent points of dim 1
    #pybind issue, need to be correct instead of succint.
    for itr in range(len(dgms_tmp[0])):
        dgms_base_0.append(dgms_tmp[0][itr])
    for itr in range(len(dgms_tmp[1])):
Example #6
0
ax.set_xlim(dims[0][0],dims[0][1])
ax.set_ylim(dims[1][0],dims[1][1])
ax.set_zlim(dims[2][0],dims[2][1])
ax.scatter(sphere_data[:,0],sphere_data[:,1],sphere_data[:,2])
ax.set_aspect("equal")

plt.show()


"""
computing homology
"""
#VR filtration TDA
test_f = d.fill_rips(TDA_data, 2, 10.)
p = d.homology_persistence(test_f)
dgms = d.init_diagrams(p, test_f)

#scatters
d.plot.plot_diagram(dgms[0])
d.plot.plot_diagram(dgms[1])
plt.show()

#bars
d.plot.plot_bars(dgms[0])
plt.show()

d.plot.plot_bars(dgms[1])
plt.show()


#alpha filtration TDA
    ax.scatter(xs=X[:, 0], ys=X[:, 1], zs=X[:, 2], s=10, c='b')
    plt.title('Scatter plot of data points')
    pdf.savefig(fig)

# The persistent cohomology of $X$ is in the plot. As expected from the dataset, the $1$-dimensional persistent cohomology
# of $X$ has one prominent topological feature.

# In[5]:

prime = 23  #choose the prime base for the coefficient field that we use to construct the persistence cohomology.
vr = dionysus.fill_rips(X, 2, 2.)  #Vietoris-Rips complex
cp = dionysus.cohomology_persistence(
    vr, prime,
    True)  #Create the persistent cohomology based on the chosen parameters.
dgms = dionysus.init_diagrams(
    cp, vr
)  #Calculate the persistent diagram usingā£ the designated coefficient field and complex.

# In[6]:

with PdfPages('fig_cc-pca_dataset_ph.pdf') as pdf:
    fig = plt.figure(figsize=(4, 4), dpi=100)
    plt.xlim([0, 2])
    plt.yticks([])
    plt.title('Barcode(dim 1) of data points')
    dionysus.plot.plot_bars(dgms[1], show=True)
    #dionysus.plot.plot_diagram(dgms[1], show=True)
    #dionysus.plot.plot_diagram(dgms[0], show=True)
    #Plot the barcode and diagrams using matplotlib incarnation within Dionysus2. This mechanism is different in Dionysus.
    pdf.savefig(fig)
Example #8
0
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from sklearn import manifold, datasets

n_points = 100
Y, color = datasets.make_circles(n_points, random_state=1, factor=0.5, noise=0)

fig, ax = plt.subplots()
ax.scatter(Y[:,0], Y[:,1], c=color, cmap=plt.cm.Spectral)
# plt.show()

import dionysus as d

max_epsilon = 1
k_skeleton = 3

f = d.fill_rips(Y, k_skeleton, max_epsilon)
p = d.homology_persistence(f)
dgms = d.init_diagrams(p, f)

fig, axes = plt.subplots(nrows=2, ncols=2)
d.plot.plot_diagram(dgms[0], ax=axes[0,0], show=False)
d.plot.plot_diagram(dgms[1], ax=axes[0,1], show=False)
d.plot.plot_bars(dgms[0], ax=axes[1,0], show=False)
d.plot.plot_bars(dgms[1], ax=axes[1,1], show=False)

axes[0,0].set_title("Dimension 0")
axes[0,1].set_title("Dimension 1")

plt.show()
Example #9
0
for cell in grid:
    weight += [max([pw[n] for n in cell])]
    dense += [max([pd[n] for n in cell])]

fil_weight = []
for v_f, v_rho in zip(weight, dense):
    if v_rho <= 0.4:
        fil_weight += [v_f]
    else:
        fil_weight += [Mf]
f = d.Filtration()
for cell, level in zip(grid, fil_weight):
    f.append(d.Simplex(cell, level))
f.sort()
res = d.homology_persistence(f)
dgm = d.init_diagrams(res, f)
for i, dg in enumerate(dgm):
    for pt in dg:
        if pt.death - pt.birth > 0.1:
            print(i, pt.birth, pt.death)

for l in range(2, 3):
    rho_0 = mrho + l * rstep
    epsilon = 0.3
    epsilon0 = 0.4
    print("rho_0", rho_0)
    estep = epsilon * (1 - epsilon0) / 6
    for k in range(7):
        print("epsilon:", epsilon)
        for n in range(17):
            a = mf * (16 - n) / 16 + Mf * n / 16
Example #10
0
    def compute_layer_mask(self, x, hiddens, subgraph_indices=[0], percentile=0):
        f,nm = self.compute_induced_filtration(x[0], hiddens, percentile=percentile)
        m = dion.homology_persistence(f)
        dgms = dion.init_diagrams(m,f)
        subgraphs = {}
        dion.plot.plot_diagram(dgms[0],show=False)
        plt.savefig('/home/tgebhart/projects/homo_explico/logdir/experiments/alexnet_vis/plt_3.png', format='png')
        plt.close()

        # compute ones tensors of same hidden dimensions
        muls = [torch.zeros(x.reshape((x.shape[0],-1)).shape)]

        for h in hiddens:
            print('h shape', h.reshape((h.shape[0],-1)).shape)
            muls.append(torch.zeros(h.reshape((h.shape[0],-1)).shape))

        fac = 1.0
        for i,c in enumerate(m):
            if len(c) == 2:
                if f[c[0].index][0] in subgraphs:
                    subgraphs[f[c[0].index][0]].add_edge(f[c[0].index][0],f[c[1].index][0], weight=f[i].data)
                else:
                    eaten = False
                    for k, v in subgraphs.items():
                        if v.has_node(f[c[0].index][0]):
                            v.add_edge(f[c[0].index][0], f[c[1].index][0], weight=f[i].data)
                            eaten = True
                            break
                    if not eaten:
                        g = nx.Graph()
                        g.add_edge(f[c[0].index][0], f[c[1].index][0], weight=f[i].data)
                        subgraphs[f[c[0].index][0]] = g

        #  I don't think we need this composition. Disjoint subgraphs are fine.
        # subgraph = nx.compose_all([subgraphs[k] for k in list(subgraphs.keys())[:thru]])

        ids = dict((v,k) for k,v in nm.items())

        ks = list(subgraphs.keys())
        print('KS: ',ks)
        ks_sub = [ks[s] for s in subgraph_indices]
        # lifetimes = np.empty(thru)
        # t = 0
        # for pt in dgms[0]:
        #     if pt.death < float('inf'):
        #         lifetimes[t] = pt.birth - pt.death
        #         t += 1
        #     if t >= thru:
        #         break
        # max_lifetime = max(lifetimes)
        # min_lifetime = min(lifetimes)

        for k in ks_sub:
            for e in subgraphs[k].edges(data=True):
                n1 = ids[e[0]]
                n2 = ids[e[1]]
                # allow the outcoming activation from that node at that channel (idx 1)
                if n1[1] > 0:
                    muls[n1[0]][n1[1],n1[2]] = fac
                else:
                    muls[n1[0]][n1[2]] = fac
                if n2[1] > 0:
                    muls[n2[0]][n2[1],n2[2]] = fac
                else:
                    muls[n2[0]][n2[2]] = fac

        return muls
Example #11
0
def GCC2(dataset,threshold,maxscale,CEthreshold=1e-5,lp=1,lq=2,Nsteps=1000,lambda_coef=0.5,PLOT=True):
    sys.argv = np.asarray(['GCC2_fun.GCC2','dataset',threshold,maxscale,CEthreshold,lp,lq,Nsteps,lambda_coef])
    return_list = []
    print('Number of arguments:', str(len(sys.argv)), 'arguments.')
    if len(sys.argv)!=8 and len(sys.argv)!=9:
        print("""
        ### usage: GCC2(dataset,threshold,maxscale,CEthreshold=1e-5,lp=1,lq=2,Nsteps=1000,lambda_coef=0.5)
        ###[dataset] The dataset you want to analyze using circular coordinates in numpy array. The cols of the array are dimensions/variables; the rows of the array are samples.
        ###[threshold] The threhold on persistence which we use to select those significant cocyles from all cocycles constructed from the Vietoris-Rips complex built upon the data. If negative integer M, the 1-cocycles with the 1,2,...,M-th largest persistence will be picked. This option would override the threshold option. 
        ###[CEthreshold] The threshold that we use to determine the constant edges. When the coordinate functions' values changed below this threshold, we consider it as a constant edge and plot it.
        ###[maxscal] The maximal scale at which we shall construct the Vietoris-Rips complex for circular coordinate computation.
        ###[lp] [lq] The generalized penalty function is in form of (1-lambda_parameter)*L^[lp]+lambda_parameter*L^[lq].
        ###[Nsteps] How many iterations you want to run in the tensorflow optimizer to obtain our circular coordinates? If negative number, no optimization would be executed.
        ###[lambda] This is a float parameter, if supplied, then only that lambda in the genealized coordinate would be calculated. 

        ###Functionality of this code.
        ####Part1: Construct the Vietoris-Rips complex built upon the data and associated persistence diagrams and barcodes.
        #####Scatter plot and associated persistence diagrams and barcodes, with significant topological features selected.
        ####Part2: Output the circular coordinates with different penalty functions.
        ####Part3: Output the embeddings with different penalty functions.""")
        return return_list
    print('Argument List:', str(sys.argv),'\n')
    filenam=sys.argv[1]
    print('Data file:', filenam)


    #From Python_code/utils.py
    def coboundary_1(vr, thr):
        D = [[],[]]
        data = []
        indexing = {}
        ix = [0]*2
        for s in vr:
            if s.dimension() != 1:
                continue
            elif s.data > thr:
                #break
                continue
            indexing.setdefault(s.dimension(),{})
            indexing.setdefault(s.dimension()-1,{})
            if not s in indexing[s.dimension()]:
                indexing[s.dimension()][s] = ix[s.dimension()]
                ix[s.dimension()] += 1
            for dat, k in enumerate(s.boundary()): 
                if not k in indexing[s.dimension()-1]:
                    indexing[k.dimension()][k] = k[0]
                    ix[k.dimension()] += 1
                D[0].append(indexing[s.dimension()][s]) #rows
                D[1].append(indexing[k.dimension()][k]) #cols
                data.append(1. if dat % 2 == 0 else -1.)
        return sp.sparse.csr_matrix((data, (D[0], D[1]))), indexing


    def optimizer_inputs(vr, bars, cocycle, init_z, prime, thr):
        bdry,indexing = coboundary_1(vr,thr)
        n, m = bdry.shape # edges X nodes
        #-----------------
        l2_cocycle = [0]*len(init_z) #reorganize the coordinates so they fit with the coboundary indices
        for i, coeff in enumerate(init_z):
            l2_cocycle[i] = coeff
        l2_cocycle = np.array(l2_cocycle)
        #-----------------
        f = np.zeros((n,1)) # cocycle we need to smooth out, reorganize to fit coboundary
        for c2 in cocycle:
            if c2.element<(prime//2):
                f[indexing[1][vr[c2.index]]] += c2.element
            else:
                f[indexing[1][vr[c2.index]]] += c2.element-prime  
        return l2_cocycle,f,bdry

    #Dionysus2 only.
    #This code is composed in such way that it produces the whole thing in a single pdf file.
    #dataset = np.loadtxt(filenam)

    #from matplotlib.backends.backend_pdf import PdfPages
    
    title_str='Circular Coordinates with Generalized Penalty Functions'
    # Create the PdfPages object to which we will save the pages:
    # The with statement makes sure that the PdfPages object is closed properly at the end of the block, even if an Exception occurs.
    
    os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
    filenam = os.path.splitext(filenam)[0]
    pdfnam = filenam+'_output.pdf'
    print('Output file:', pdfnam,'\n')

    now = datetime.datetime.now()
    print('>>>>>>Start Time(VR computation):',now.strftime("%Y-%m-%d %H:%M:%S"))

    ##############################
    #Scatter plots for datapoints#
    if PLOT:
        fig = plt.figure(figsize=(5,5), dpi=100)
        plt.xlabel('X')
        plt.ylabel('Y')
        plt.gca().set_aspect('equal', 'datalim')
        plt.scatter(dataset.T[0,:],dataset.T[1,:],s=10, c='b')
        plt.axis('equal')
        plt.title('Scatter plot of data points')
        #pdf.savefig(fig)
    #                            #
    ##############################
    ##############################
    #Compute Persistence Diagrams#
    prime = 23
    maxscale = float(sys.argv[3])
    #Choose the prime base for the coefficient field that we use to construct the persistence cohomology.
    threshold = float(sys.argv[2])
    print('Base coefficient field: Z/', prime ,'Z',sep='')
    print('Maximal scale:', float(sys.argv[3]))
    print('Persistence threshold for selecting significant cocyles:',threshold)

    vr = dionysus.fill_rips(dataset, 2, float(sys.argv[3]))
    #Vietoris-Rips complex
    cp = dionysus.cohomology_persistence(vr, prime, True)
    #Create the persistent cohomology based on the chosen parameters.
    dgms = dionysus.init_diagrams(cp, vr)
    #Calculate the persistent diagram using the designated coefficient field and complex.
    now = datetime.datetime.now()
    print('>>>>>>End Time (VR-computation):',now.strftime("%Y-%m-%d %H:%M:%S"))
    ###Plot the barcode and diagrams using matplotlib incarnation within Dionysus2. This mechanism is different in Dionysus.
    if PLOT:
        #Plots of persistence barcodes of Vietoris-Rips complex for dimension 0 and 1.
        fig = plt.figure(figsize=(5,5), dpi=100)
        plt.title('Persistence Barcode for dim 0')
        dionysus.plot.plot_bars(dgms[0], show=True)
        #pdf.savefig(fig)
        fig = plt.figure(figsize=(5,5), dpi=100)
        plt.title('Persistence Barcode for dim 1')
        dionysus.plot.plot_bars(dgms[1], show=True)
        #pdf.savefig(fig)
        plt.show()

        #Plots of persistence diagrams of Vietoris-Rips complex for dimension 0 and 1.
        fig = plt.figure(figsize=(5,5), dpi=100)
        plt.title('Persistence Diagram for dim 0')
        dionysus.plot.plot_diagram(dgms[0], show=True)
        #pdf.savefig(fig)
        fig = plt.figure(figsize=(5,5), dpi=100)
        plt.title('Persistence Diagram for dim 1')
        dionysus.plot.plot_diagram(dgms[1], show=True)
        #pdf.savefig(fig)
        plt.show()


    ######Select and highlight the features selected.
    bars = [bar for bar in dgms[1] ]
    #Choose cocycle that persist at least threshold we choose.
    cocycles = [cp.cocycle(bar.data) for bar in bars]
    #Sort the list 'cocycles' by the persistence corresponding to each bar
    cocycles_persistence = [bar.death-bar.birth for bar in bars]
    #print(cocycles_persistence)
    cocycles_ind = np.argsort(-np.asarray(cocycles_persistence), axis=-1, kind=None, order=None) 
    cocycles = [cocycles[i] for i in cocycles_ind]
    bars = [bars[i] for i in cocycles_ind]
    if threshold<0:
        leadings = int(np.abs(threshold))
        #Override threshold option
        threshold = 0 
        print('\n>>>>>>Threshold overridden, the 1-cocyles with the ',leadings,' largest persistence would be selected for computation.')
        cocycles = cocycles[0:leadings]
        bars = bars[0:leadings]
    else:
        bars = [bar for bar in dgms[1] if bar.death-bar.birth > threshold and bar.death-bar.birth < float(sys.argv[3]) ]
        cocycles = [cp.cocycle(bar.data) for bar in bars]
    print('>>>>>>Selected significant features:')
    for B_Lt in bars:
        print(B_Lt,'\tpersistence = ',B_Lt.death-B_Lt.birth)
    ####################
    #PersistenceBarcode#
    if PLOT:
        #Red highlight ***ALL*** cocyles that persist more than threshold value on barcode, when more than one cocyles have persisted over threshold values, this plots the first one.
        fig = plt.figure(figsize=(5,5), dpi=100)
        dionysus.plot.plot_bars(dgms[1], show=False)
        Lt1 = [[bar.birth,bar.death] for bar in bars]
        #Lt1 stores the bars with persistence greater than the [threshold].
        Lt1_tmp = [[bar.birth,bar.death] for bar in dgms[1] ]
        for Lt in Lt1:
            loc=0
            target=Lt
            for g in range(len(Lt1_tmp)):
                if Lt1_tmp[g][0] == target[0] and Lt1_tmp[g][1] == target[1]:
                    loc=g
            #Searching correct term
            plt.plot([Lt[0],Lt[1]],[loc,loc],'r-')
            #print(Lt)

        plt.title('Selected cocycles on barcodes (red bars)')
        #pdf.savefig(fig)
        plt.show()
    #                  #
    ####################
    ####################
    #PersistenceDiagram#
    if PLOT:
        #Red highlight ***ALL*** cocyles that persist more than threshold value on diagram.
        fig = plt.figure(figsize=(5,5), dpi=100)
        dionysus.plot.plot_diagram(dgms[1], show=False)
        Lt2 = [[point.birth,point.death] for point in bars ]
        #Lt2 stores the (multi-)points with persistence greater than the [threshold].
        for Lt in Lt2:
            plt.plot(Lt[0],Lt[1],'ro')
        plt.title('Selected cocycles on diagram (red points)')
        plt.figure(figsize=(5,5), dpi=100)
        #pdf.savefig(fig)
        plt.show()
    #                  #
    ####################

    #                            #
    ##############################
    ##############################
    #    Visualization of GCC    #
    overall_coords = np.zeros(dataset.shape[0], dtype = float)
    #from Python_code import utils
    toll = float(sys.argv[4])#tolerance for constant edges.
    print('\nConstant edges, with coordinates difference <',toll)
    print('Optimizer maximal iteration steps=',int(sys.argv[7]))

    lp=int(sys.argv[5])
    lq=int(sys.argv[6])
    now = datetime.datetime.now()
    print('>>>>>> Start Time (GCC computation):',now.strftime("%Y-%m-%d %H:%M:%S"))
    if len(sys.argv)>=9:
        lambda_list = [float(sys.argv[8])]
    else:
        lambda_list = [0,0.5,1]
    for lambda_parameter in lambda_list:
        print('>>>>>> lambda = ',lambda_parameter,'. => Analysis of Circular coordinates \n (mod {} - {}*L{} + {}*L{})'.format(prime,1-lambda_parameter,lp,lambda_parameter,lq))
        embedding = []
        if PLOT:
            fig = plt.figure(figsize=(5,5), dpi=100)
            plt.text(0.3,0.5,'Analysis of Circular coordinates \n (mod {} - {}*L{} + {}*L{})'.format(prime,1-lambda_parameter,lp,lambda_parameter,lq),transform=plt.gca().transAxes)
            #pdf.savefig(fig)
            plt.show()
        print('Penalty function =>',(1-lambda_parameter),'*L^',lp,"+",lambda_parameter,"*L^",lq,sep='')
        for g in range(len(cocycles)):
            chosen_cocycle = cocycles[g]
            chosen_bar = bars[g]
            #print(chosen_cocycle,chosen_bar)
            NEW_THRESHOLD = max([vr[c2.index].data for c2 in chosen_cocycle]) 
            vr_L2 = dionysus.Filtration([s for s in vr if s.data <=NEW_THRESHOLD])
            coords = dionysus.smooth(vr_L2, chosen_cocycle, prime)
            l2_cocycle,f,bdry = optimizer_inputs(vr, bars, chosen_cocycle, coords, prime, NEW_THRESHOLD)
            l2_cocycle = l2_cocycle.reshape(-1, 1)
            ##It does not seem to work to have double invokes here...
            B_mat = bdry.todense()         
            z_init = l2_cocycle
            z = tf.Variable(z_init, name='z', trainable=True, dtype=tf.float64)
            trainable_variables = [z]
            def loss_function():
                cost_z = (1-lambda_parameter)*tf.math.pow( tf.math.reduce_sum( tf.math.pow( tf.abs(f - tf.linalg.matmul(B_mat,z) ),lp ) ), 1/lp) + lambda_parameter*tf.math.pow( tf.math.reduce_sum( tf.math.pow( tf.math.abs(f - tf.linalg.matmul(B_mat,z) ),lq ) ), 1/lq)
                return cost_z
            #print(B_mat.shape)
            #l2_cocycle=np.zeros((B_mat.shape[1],1))
            tf.random.set_seed(1)
            optimizer = tf.optimizers.Adam(learning_rate=1e-4, beta_1=0.9, beta_2=0.999, epsilon=1e-7, amsgrad=False)
            #optimizer = tf.keras.optimizers.SGD(learning_rate=1e-4) #Not recommended if you are not using tensorflow-gpu, may result a different result sometimes.
            B_mat = tf.Variable(B_mat, name="B_mat", trainable=False, dtype=tf.float64)
            f = tf.Variable(f, name="f", trainable=False, dtype=tf.float64)
            if int(sys.argv[7])>0:
                print('Before optim cost:',loss_function().numpy())
                for i in range(int(sys.argv[7])):
                    train = optimizer.minimize(loss_function, var_list=trainable_variables)
                    #err = np.sum(np.abs(z.numpy() - z_init))
                    #print('>>> step',train.numpy(),' err=',err)
                print('After  optim cost:',loss_function().numpy(),' in ',train.numpy(),' steps')
                res_tf=z.numpy()
            else:
                print('Non-optimized cost:',loss_function().numpy())
                res_tf=z_init
            #print("Absolute differentiation =>",np.sum(np.abs(res_tf-np.asarray(coords) )))
            overall_coords=overall_coords+res_tf.T[0,:]
            color = np.mod(res_tf.T[0,:],1)
            #print(color)
            return_list.append(color)
            if PLOT:
                fig = plt.figure(figsize=(5,5), dpi=100)
                plt.scatter(dataset.T[0,:],dataset.T[1,:],s=10, c=color, cmap="hsv",zorder=10)
                plt.clim(0,1)
                plt.colorbar()
                plt.axis('equal')
                plt.title('Circular coordinates {}-th cocyle (mod {} - {}*L{} + {}*L{})'.format(g+1,prime,1-lambda_parameter,lp,lambda_parameter,lq))
                plt.show()
            edges_constant = []
            thr = chosen_bar.birth
            #####Constatn edges
            #Want to check constant edges in all edges that were there when the cycle was created
            for s in vr:
                if s.dimension() != 1:
                    continue
                elif s.data > thr:
                    break
                if abs(color[s[0]]-color[s[1]]) <= toll:
                    edges_constant.append([dataset[s[0],:],dataset[s[1],:]])
            edges_constant = np.array(edges_constant)
            #pdf.savefig(fig)
            
            #print('Loop End Time:',now.strftime("%Y-%m-%d %H:%M:%S"))
            if PLOT:
                fig = plt.figure(figsize=(5,5), dpi=100)
                if edges_constant.T!=[]:
                  plt.plot(edges_constant.T[0,:],edges_constant.T[1,:], c='k', alpha=.5)
                plt.scatter(dataset.T[0,:],dataset.T[1,:],s=10, c=color, cmap="hsv",zorder=10)
                plt.clim(0,1)
                plt.colorbar()
                plt.axis('equal')
                plt.title('Circular coordinates/constant edges, \n {}-th cocyle (mod {} - {}*L{} + {}*L{})'.format(g+1,prime,1-lambda_parameter,lp,lambda_parameter,lq))
                #pdf.savefig(fig)
                plt.show()
            color_filenam = filenam+'_CircularCoordinates_'+str(lambda_parameter)+'_'+str(g)+'.txt'
            np.savetxt(color_filenam,color)
            print('Penalty function =>',(1-lambda_parameter),'*L^',lp,"+",lambda_parameter,"*L^",lq,' Coordinates=>',color_filenam,sep='')
            
            if PLOT:
                fig = plt.figure(figsize=(5,5), dpi=100)
                angle = np.arctan(dataset.T[0,:]/dataset.T[1,:])
                plt.scatter(angle,color,s=10, c='b',zorder=10)
                plt.ylim([0,1])
                plt.xlim([-np.pi/2,np.pi/2])
                plt.title('Correlation plot against angle, \n {}-th cocyle (mod {} - {}*L{} + {}*L{})'.format(g+1,prime,1-lambda_parameter,lp,lambda_parameter,lq))
                #pdf.savefig(fig)
                plt.show()
            embedding.extend([[np.sin(a) for a in 2*np.pi*color], [np.cos(a) for a in 2*np.pi*color]])
            if PLOT:
                fig = plt.figure(figsize=(5,5), dpi=100)
                dist2 = np.sqrt(np.power(dataset.T[0,:],2)+np.power(dataset.T[1,:],2))
                plt.scatter(dist2,color,s=10, c='b',zorder=10)
                plt.ylim([0,1])
                plt.xlim([0,maxscale])
                plt.title('Correlation plot aginst distance, \n {}-th cocyle (mod {} - {}*L{} + {}*L{})'.format(g+1,prime,1-lambda_parameter,lp,lambda_parameter,lq))
                #pdf.savefig(fig)
                plt.show()
            

        emb_filenam = filenam+'_Embedding_'+str(lambda_parameter)+'.txt'
        np.savetxt(emb_filenam, np.array(embedding))
        print('Penalty function =>',(1-lambda_parameter),'*L^',lp,"+",lambda_parameter,"*L^",lq,' Embeddings=>',emb_filenam,sep='')
        #We plot the final circular coordinates with all co-cycles combined.
        overall_edges_constant = []
        overall_thr = float(sys.argv[2]) #For the combined coordinates, we choose the global threshold.
        for s in vr:
            if s.dimension() != 1:
                continue
            elif s.data > overall_thr:
                break
            if abs(overall_coords[s[0]]-overall_coords[s[1]]) <= toll:
                overall_edges_constant.append([dataset[s[0],:],dataset[s[1],:]])
        overall_edges_constant = np.array(overall_edges_constant)
        if PLOT:
            #fig = plt.figure(figsize=(5,5), dpi=100)
            #if overall_edges_constant.T!=[]:
            #  plt.plot(overall_edges_constant.T[0,:],overall_edges_constant.T[1,:], c='k', alpha=.5)
            #plt.scatter(dataset.T[0,:],dataset.T[1,:],s=10, c=overall_coords, cmap="hsv",zorder=10)
            #plt.clim(0,1)
            #plt.colorbar()
            #plt.axis('equal')
            #plt.title('Combined circular coordinates/constant edges \n (mod {} - {}*L{} + {}*L{})'.format(prime,1-lambda_parameter,lp,lambda_parameter,lq))
            #pdf.savefig(fig)
            #plt.show()

            fig = plt.figure(figsize=(5,5), dpi=100)
            angle = np.arctan(dataset.T[0,:]/dataset.T[1,:])
            plt.scatter(angle,overall_coords,s=10, c='b',zorder=10)
            plt.ylim([0,1])
            plt.xlim([-np.pi/2,np.pi/2])
            plt.title('Correlation plot \n (mod {} - {}*L{} + {}*L{})'.format(prime,1-lambda_parameter,lp,lambda_parameter,lq))
            #pdf.savefig(fig)
            plt.show()

            fig = plt.figure(figsize=(5,5), dpi=100)
            dist2 = np.sqrt(np.power(dataset.T[0,:],2)+np.power(dataset.T[1,:],2))
            #print(color)
            plt.scatter(dist2,color,s=10, c='b',zorder=10)
            plt.ylim([0,1])
            plt.xlim([0,maxscale])
            plt.title('Correlation plot aginst distance, \n {}-th cocyle (mod {} - {}*L{} + {}*L{})'.format(g+1,prime,1-lambda_parameter,lp,lambda_parameter,lq))
            #pdf.savefig(fig)
            plt.show()

    now = datetime.datetime.now()
    print('>>>>>> End Time (GCC computation):',now.strftime("%Y-%m-%d %H:%M:%S"))
    return return_list
Example #12
0
def EPH_mask_demo(vertex_values, simplices):
    times = []
    t0 = time.time()

    s2v_lst = [[
        sorted(s),
        sorted([[vertex_values[v], v] for v in s], key=lambda x: x[0])
    ] for s in simplices]
    f_ord = [dionysus.Simplex(s[0], s[1][-1][0]) for s in s2v_lst]  #takes max
    f_ext = [dionysus.Simplex([-1] + s[0], s[1][0][0])
             for s in s2v_lst]  #takes min

    ord_dict = {tuple(s[0]): s[1][-1][1] for s in s2v_lst}
    ext_dict = {tuple([-1] + s[0]): s[1][0][1] for s in s2v_lst}

    t1 = time.time()
    times.append(t1 - t0)
    t0 = time.time()

    f_ord.sort(key=lambda s: (s.data, len(s)))
    f_ext.sort(key=lambda s: (-s.data, len(s)))

    t1 = time.time()
    times.append(t1 - t0)
    t0 = time.time()
    #computes persistence
    f = dionysus.Filtration([dionysus.Simplex([-1], -float('inf'))] + f_ord +
                            f_ext)
    m = dionysus.homology_persistence(f)
    t1 = time.time()
    times.append(t1 - t0)
    t0 = time.time()

    dgms = [[[], []], [[], []], [[], []], [[],
                                           []]]  #H0ord, H0ext, H1rel, H1ext

    for i in range(len(m)):

        dim = f[i].dimension()

        if m.pair(i) < i:
            continue  # skip negative simplices to avoid double counting
        if m.pair(
                i
        ) != m.unpaired:  #should be no unpaired apart from H0 from fictitious -1 vertex
            pos, neg = f[i], f[m.pair(i)]
            if pos.data != neg.data:  #off diagonal
                if -1 in pos and -1 in neg:  #rel1
                    dgms[2][0].append(ext_dict[tuple(neg)])
                    dgms[2][1].append(ext_dict[tuple(pos)])

                elif -1 not in pos and -1 not in neg:  #ord0
                    dgms[1][0].append(ord_dict[tuple(pos)])
                    dgms[1][1].append(ord_dict[tuple(neg)])

                else:
                    if dim == 0:  #H0ext
                        dgms[0][0].append(ord_dict[tuple(pos)])
                        dgms[0][1].append(ext_dict[tuple(neg)])

                    if dim == 1:  #H1ext
                        dgms[3][0].append(ext_dict[tuple(neg)])
                        dgms[3][1].append(ord_dict[tuple(pos)])

    t1 = time.time()
    times.append(t1 - t0)
    t0 = time.time()

    dgms = dionysus.init_diagrams(m, f)

    t1 = time.time()
    times.append(t1 - t0)
    return times
Example #13
0
    def compute_dropouts(self, x, hiddens, thru=2, p=1, dropout_type='static', percentile=0, muls=None, invert=False, mat=None):
        if dropout_type == 'static':
            f = self.compute_static_filtration(x, hiddens, percentile=percentile)
        if dropout_type == 'induced':
            f = self.compute_induced_filtration(x, hiddens, percentile=percentile, mat=mat)
        m = dion.homology_persistence(f)
        dgms = dion.init_diagrams(m,f)
        subgraphs = {}

        # compute ones tensors of same hidden dimensions
        muls = []
        if invert:
            for h in hiddens:
                muls.append(torch.zeros(h.shape))
        else:
            for h in hiddens:
                muls.append(torch.ones(h.shape))

        fac = 1.0 if invert else 0.0

        for i,c in enumerate(m):
            if len(c) == 2:
                if f[c[0].index][0] in subgraphs:
                    subgraphs[f[c[0].index][0]].add_edge(f[c[0].index][0],f[c[1].index][0],weight=f[i].data)
                else:
                    eaten = False
                    for k, v in subgraphs.items():
                        if v.has_node(f[c[0].index][0]):
                            v.add_edge(f[c[0].index][0], f[c[1].index][0], weight=f[i].data)
                            eaten = True
                            break
                    if not eaten:
                        g = nx.Graph()
                        g.add_edge(f[c[0].index][0], f[c[1].index][0], weight=f[i].data)
                        subgraphs[f[c[0].index][0]] = g

        #  I don't think we need this composition. Disjoint subgraphs are fine.
        # subgraph = nx.compose_all([subgraphs[k] for k in list(subgraphs.keys())[:thru]])

        sub_keys = list(subgraphs.keys())[:thru]
        lifetimes = np.empty(thru)
        t = 0
        for pt in dgms[0]:
            if pt.death < float('inf'):
                lifetimes[t] = pt.birth - pt.death
                t += 1
            if t >= thru:
                break
        max_lifetime = max(lifetimes)
        min_lifetime = min(lifetimes)
        ids = self.layerwise_ids()
        layer_types = self.layer_types

        for i in range(len(sub_keys)):
            k = sub_keys[i]
            lifetime = lifetimes[i]
            lim = p*lifetime/max_lifetime
            for e in subgraphs[k].edges(data=True):
                r = np.random.uniform()
                if r < lim:
                    for l in range(len(ids)-1):
                        if e[0] in ids[l] and e[1] in ids[l+1]:
                            # drop out this edge connection
                            # if layer_types[l] == 'convolution':
                            #     num_filters = self.filters
                            #     inp_size = self.input_size
                            #     stride = self.stride
                            #     kernel_size = self.kernel_size
                            #     next_ids = list(ids[l+1])
                            #     # muls[l] = (28-self.kernel_size+1)**2*self.filters
                            #     filt_num = math.floor((e[1]-ids[l][-1])/((inp_size-kernel_size+1)**2))
                            #     print('filtnum', filt_num)
                            #     # filters are 0-indexed, so add 1 and 2 to get ids
                            #     start_id = (inp_size-kernel_size+stride)**2*(filt_num+1)
                            #     end_id = (inp_size-kernel_size+stride)**2*(filt_num+2)
                            #     print('s min end', start_id, end_id, e[0], e[1])
                            #     # find which element of the kernel produced this edge
                            #     index_no = (e[1]-start_id) % (kernel_size*kernel_size)
                            #     image_no = (e[1]-start_id)//(kernel_size*kernel_size)

                            muls[l][e[1]-ids[l+1][0]] = fac

        return muls