Esempio n. 1
0
        def problem_params(lr, gam, memories, inpst, neurons):
            """
            Return the lowest eigenvector of the classical Hamiltonian
            constructed by the learning rule, gamma, memories, and input.
            """
            # Bias Hamiltonian
            alpha = gam * np.array(inpst)

            # Memory Hamiltonian
            beta = np.zeros((qubits, qubits))
            if lr == "hebb":
                # Hebb rule
                memMat = sp.matrix(memories).T
                beta = sp.triu(memMat * memMat.T) / float(neurons)
            elif lr == "stork":
                # Storkey rule
                Wm = sp.zeros((neurons, neurons))
                for m, mem in enumerate(memories):
                    Am = sp.outer(mem, mem) - sp.eye(neurons)
                    Wm += (Am - Am * Wm - Wm * Am) / float(neurons)
                beta = sp.triu(Wm)
            elif lr == "proj":
                # Moore-Penrose pseudoinverse rule
                memMat = sp.matrix(memories).T
                beta = sp.triu(memMat * sp.linalg.pinv(memMat))

            # Find the eigenvectors
            evals, evecs = sp.linalg.eig(np.diag(alpha) + beta)
            idx = evals.argsort()

            return evals[idx], evecs[:, idx], np.diag(alpha), beta
Esempio n. 2
0
        def problem_params(lr, gam, memories, inpst, neurons):
            """
            Return the lowest eigenvector of the classical Hamiltonian
            constructed by the learning rule, gamma, memories, and input.
            """
            # Bias Hamiltonian
            alpha = gam * np.array(inpst)

            # Memory Hamiltonian
            beta = np.zeros((qubits, qubits))
            if lr == 'hebb':
                # Hebb rule
                memMat = sp.matrix(memories).T
                beta = sp.triu(memMat * memMat.T) / float(neurons)
            elif lr == 'stork':
                # Storkey rule
                Wm = sp.zeros((neurons, neurons))
                for m, mem in enumerate(memories):
                    Am = sp.outer(mem, mem) - sp.eye(neurons)
                    Wm += (Am - Am * Wm - Wm * Am) / float(neurons)
                beta = sp.triu(Wm)
            elif lr == 'proj':
                # Moore-Penrose pseudoinverse rule
                memMat = sp.matrix(memories).T
                beta = sp.triu(memMat * sp.linalg.pinv(memMat))

            # Find the eigenvectors
            evals, evecs = sp.linalg.eig(np.diag(alpha) + beta)
            idx = evals.argsort()

            return evals[idx], evecs[:, idx], np.diag(alpha), beta
Esempio n. 3
0
    def from_gene(self, gene): 

        sg = gene.splicegraph.vertices
        breakpoints = sp.unique(sg.ravel())
        self.segments = sp.zeros((2, 0), dtype='int')
        for j in range(1, breakpoints.shape[0]):
            s = sp.sum(sg[0, :] < breakpoints[j])
            e = sp.sum(sg[1, :] < breakpoints[j])
            if s > e:
                self.segments = sp.c_[self.segments, [breakpoints[j-1], breakpoints[j]]]

        ### match nodes to segments
        self.seg_match = sp.zeros((0, sg.shape[1]), dtype='bool')
        for j in range(sg.shape[1]):
            tmp = ((sg[0, j] <= self.segments[0, :]) & (sg[1, j] >= self.segments[1, :]))
            if self.seg_match.shape[0] == 0:
                self.seg_match = tmp.copy().reshape((1, tmp.shape[0]))
            else:
                self.seg_match = sp.r_[self.seg_match, tmp.reshape((1, tmp.shape[0]))]

        ### create edge graph between segments
        self.seg_edges = sp.zeros((self.segments.shape[1], self.segments.shape[1]), dtype='bool')
        k, l = sp.where(sp.triu(gene.splicegraph.edges))

        for m in range(k.shape[0]):
            ### donor segment
            d = sp.where(self.seg_match[k[m], :])[0][-1]
            ### acceptor segment
            a = sp.where(self.seg_match[l[m], :])[0][0]
            self.seg_edges[d, a] = True
Esempio n. 4
0
    def from_gene(self, gene):

        sg = gene.splicegraph.vertices
        breakpoints = sp.unique(sg.ravel())
        self.segments = sp.zeros((2, 0), dtype='int')
        for j in range(1, breakpoints.shape[0]):
            s = sp.sum(sg[0, :] < breakpoints[j])
            e = sp.sum(sg[1, :] < breakpoints[j])
            if s > e:
                self.segments = sp.c_[self.segments,
                                      [breakpoints[j - 1], breakpoints[j]]]

        ### match nodes to segments
        self.seg_match = sp.zeros((0, sg.shape[1]), dtype='bool')
        for j in range(sg.shape[1]):
            tmp = ((sg[0, j] <= self.segments[0, :]) &
                   (sg[1, j] >= self.segments[1, :]))
            if self.seg_match.shape[0] == 0:
                self.seg_match = tmp.copy().reshape((1, tmp.shape[0]))
            else:
                self.seg_match = sp.r_[self.seg_match,
                                       tmp.reshape((1, tmp.shape[0]))]

        ### create edge graph between segments
        self.seg_edges = sp.zeros(
            (self.segments.shape[1], self.segments.shape[1]), dtype='bool')
        k, l = sp.where(sp.triu(gene.splicegraph.edges))
        for m in range(k.shape[0]):
            ### donor segment
            d = sp.where(self.seg_match[k[m], :])[0][-1]
            ### acceptor segment
            a = sp.where(self.seg_match[l[m], :])[0][0]
            self.seg_edges[d, a] = True
Esempio n. 5
0
    def add_intron_retention(self, idx1, idx2):

        adj_mat = sp.triu(self.edges)

        self.vertices = sp.c_[
            self.vertices,
            sp.array([self.vertices[0, idx1], self.vertices[1, idx2]],
                     dtype='int')]

        self.new_edge()

        adj_mat = sp.r_[adj_mat, sp.zeros((1, adj_mat.shape[1]), dtype='int')]
        adj_mat = sp.c_[adj_mat, sp.zeros((adj_mat.shape[0], 1), dtype='int')]

        ### check if adjacency matrix is symmetric
        ### otherwise or is not justyfied
        assert (sp.all(sp.all(adj_mat - (self.edges - adj_mat).T == 0)))

        ### AK: under the assumption that our splice graph representation is symmetric
        ### I preserve symmetry by using OR over the adj_mat column and row

        self.edges[:, -1] = adj_mat[:, idx1] | adj_mat[idx2, :].T
        self.edges[-1, :] = adj_mat[:, idx1].T | adj_mat[idx2, :]

        self.terminals = sp.c_[
            self.terminals,
            sp.array([self.terminals[0, idx1], self.terminals[1, idx2]],
                     dtype='int')]
Esempio n. 6
0
def calcElasticEnergy(pdb, refpdb, sel="calpha", cutoff=12, k=1):
    import prody as pd
    import numpy as np
    import scipy as sci
    import scipy.spatial as sp

    # Select desired atoms, retrieve reference parameters, and initialize Kirchhoff matrix
    gnm = pd.GNM()
    gnm.buildKirchhoff(refpdb.calpha, cutoff, k)
    kirchhoff = gnm.getKirchhoff()
    np.fill_diagonal(kirchhoff, 0)
    kirchhoff = abs(kirchhoff)

    # Calculate distances between Kirchhoff contacts
    eq_dists = sp.distance.cdist(
        refpdb.calpha.select(sel).getCoords(), refpdb.calpha.select(sel).getCoords(), metric="euclidean"
    )
    dists = sp.cdist(pdb.calpha.select(sel).getCoords(), pdb.calpha.select(sel).getCoords(), metric="euclidean")

    # Iterate over coords within ensemble
    energies = k * np.multiply(np.square(dists - eq_dists), kirchhoff)
    energies = sci.triu(energies)
    energies = np.sum(energies)

    return energies
Esempio n. 7
0
def calcMultiStateEnergy(ensemble, ensemble_ref, cutoff=12, k=1, U0=None):
    import prody as pd
    import scipy as sci
    import scipy.spatial as sp
    import numpy as np

    n_conf = ensemble.numConfs()
    n_ref = ensemble_ref.numConfs()
    energies = np.zeros((n_ref, n_conf))

    for i, conf_ref in zip(xrange(n_ref), ensemble_ref.iterCoordsets()):
        gnm = pd.GNM()
        gnm.buildKirchhoff(conf_ref, cutoff, k)
        kirchhoff = gnm.getKirchhoff()
        np.fill_diagonal(kirchhoff, 0)
        kirchhoff = abs(kirchhoff)
        eq_dists = sp.distance.squareform(sp.distance.pdist(conf_ref, metric="euclidean"))
        for j, conf in zip(xrange(n_conf), ensemble.iterCoordsets()):
            dists = sp.distance.squareform(sp.distance.pdist(conf, metric="euclidean"))
            springs = (k / 2) * np.multiply(np.square(dists - eq_dists), kirchhoff)
            springs = sci.triu(springs)
            energies[i, j] = np.sum(springs)

            # return np.min(energies, axis=0)
    return energies
Esempio n. 8
0
def sample_plaquette(p1, p2, p3, normalize_GS=False):

    # Generates one of 4 types of plaquette depending on parameters
    # p1..3 (p4=1-p1-p2-p3)
    # Type i is a plaqutte containing i GSs (up to flip) including the
    # FM state. Achieved by generating i weak bonds in the cycle, one
    # of which is AFM.

    # Returned J magnitudes on cycle edges are in {1,2}. One of
    # the mag-1 edges is AFM; all other edges are FM.

    # Note that this (may) return an *integer-valued* matrix, but when put into full problem in generate_2D_problem, gets converted to real.

    R = sp.rand()
    if R < p1:
        num_GS = 1
    elif R < p1 + p2:
        num_GS = 2
    elif R < p1 + p2 + p3:
        num_GS = 3
    else:
        num_GS = 4

    J_plaq = 2 * make_plaquette_adj()
    edges = sp.where(sp.triu(J_plaq))
    num_edges = len(edges[0])  # i.e 4

    P = sp.random.permutation(num_edges)
    # Set the weak edges
    for e in P[0:num_GS]:
        (v1, v2) = edges[0][e], edges[1][e]
        J_plaq[v1, v2] *= 0.5
        J_plaq[v2, v1] *= 0.5
    # Arbitrarily select the first of the permutation to be AFM
    e_flip = P[0]
    (v1, v2) = edges[0][e_flip], edges[1][e_flip]
    J_plaq[v1, v2] *= -1
    J_plaq[v2, v1] *= -1

    # Scale such that each cycle has the same GS energy
    if normalize_GS:
        J_plaq = sp.float64(J_plaq)
        minus_EGS = sp.triu(J_plaq).sum()
        J_plaq /= minus_EGS

    return J_plaq
Esempio n. 9
0
    def update_terminals(self):

        self.terminals = sp.zeros(self.vertices.shape, dtype='int')
        self.terminals[0,
                       sp.where(
                           sp.sum(sp.tril(self.edges), axis=1) == 0)[0]] = 1
        self.terminals[1,
                       sp.where(
                           sp.sum(sp.triu(self.edges), axis=1) == 0)[0]] = 1
Esempio n. 10
0
def modalDiffMatrix(n):
    """Return the modal differentiation matrix for 
       Chebyshev coefficients"""
    k = np.arange(n)
    a = (-1)**k
    A = sp.triu(1 - np.outer(a, a))
    D = np.dot(A, np.diag(k))
    D[0, :] = D[0, :] / 2
    return D
Esempio n. 11
0
def modalDiffMatrix(n):
    """Return the modal differentiation matrix for 
       Chebyshev coefficients"""
    k = np.arange(n)
    a = (-1)**k
    A = sp.triu(1-np.outer(a,a))
    D = np.dot(A,np.diag(k))
    D[0,:] = D[0,:]/2
    return D  
Esempio n. 12
0
def stork2(neurons, memories):
    memMat = sp.zeros((neurons,neurons))
    for m, mem in enumerate(memories):
        for i in range(neurons):
            for j in range(i, neurons):
                hij = sp.sum([ memMat[i,k]*mem[k] for k in range(neurons) ])
                hji = sp.sum([ memMat[j,k]*mem[k] for k in range(neurons) ])
                # Don't forget to make the normalization a float!
                memMat[i,j] += (mem[i]*mem[j] - mem[i]*hji - hij*mem[j])/float(neurons)
    return sp.triu(memMat)
def kpca_cluster(data,nclusters=100,ncomponents=40,topwhat=10,zscored=False):
    '''

    Computes clustering of bag-of-words vectors of articles

    INPUT
    folder      model folder
    nclusters   number of clusters

    '''
    from sklearn.cluster import KMeans
    # filtering out some noise words
    stops = map(lambda x:x.lower().strip(),open('stopwords.txt').readlines()[6:])

    # vectorize non-stopwords 
    bow = TfidfVectorizer(min_df=2,stop_words=stops)
    X = bow.fit_transform(data)

    # creating bow-index-to-word map
    idx2word = dict(zip(bow.vocabulary_.values(),bow.vocabulary_.keys()))

    # using now stopwords and filtering out digits
    print 'Computing pairwise distances' 
    K = pairwise_distances(X,metric='l2',n_jobs=1)
    perc = 50.0
    width = percentile(K.flatten(),perc)

    # KPCA transform bow vectors
    Xc = KernelPCA(n_components=ncomponents,kernel='rbf',gamma=width).fit_transform(X)
    
    if zscored:
        Xc = zscore(Xc)
    
    # compute clusters
    km = KMeans(n_clusters=nclusters).fit(Xc)
    Xc = km.predict(Xc)

    clusters = []
    for icluster in range(nclusters):
        nmembers = (Xc==icluster).sum()
        if True:#nmembers < len(data) / 5.0 and nmembers > 1: # only group clusters big enough but not too big
            members = (Xc==icluster).nonzero()[0]
            topwordidx = array(X[members,:].sum(axis=0))[0].argsort()[-topwhat:][::-1]
            topwords = ' '.join([idx2word[wi] for wi in topwordidx])
            meanDist = triu(pairwise_distances(X[members,:],metric='l2',n_jobs=1)).sum()
            meanDist = meanDist / (len(members) + (len(members)**2 - len(members))/2.0)
            # print u'Cluster %d'%icluster + u' %d members'%nmembers + u' mean Distance %f'%meanDist + u'\n\t'+topwords
            clusters.append({
                'name':'Cluster-%d'%icluster,
                'description': topwords,
                'members': list(members),
                'meanL2Distances': meanDist
                })

    return clusters
Esempio n. 14
0
def Calculate_NCG(X, Pi, alpha, beta1, beta2, d_old, g_norm_old):

    # Probability of observing an edge between clusters
    eps = 1e-10

    N = X.shape[0]
    k = Pi.shape[1]

    psi = sc.special.psi
    Y = sc.ones((N, N)) - sc.identity(N) - X

    d_log_B1 = psi(beta1) - psi(beta1 + beta2)
    d_log_B2 = psi(beta2) - psi(beta1 + beta2)

    d_log_B1 = sc.diag(
        sc.diag(d_log_B1)) + sc.log(eps) * sc.triu(sc.ones(
            (k, k)), 1) + sc.log(eps) * sc.tril(sc.ones((k, k)), -1)
    d_log_B2 = sc.diag(
        sc.diag(d_log_B2)) + sc.log(1 - eps) * sc.triu(sc.ones(
            (k, k)), 1) + sc.log(1 - eps) * sc.tril(sc.ones((k, k)), -1)

    # L_prime_i,j = dL / dPi_i,j
    L_prime = X.dot(Pi.dot(d_log_B1.T)) + Y.dot(Pi.dot(d_log_B2.T)) \
        + sc.outer(sc.ones(N), psi(alpha)) - sc.log(Pi) - sc.outer(sc.ones(N), sc.ones(k))

    u = sc.concatenate([sc.identity(k - 1), -sc.ones((1, k - 1))], axis=0)

    # Natural gradient
    g = L_prime.dot(u)

    # Norm of natural gradient
    g_norm = 0
    for i in range(N):
        l = L_prime[i, :]
        p = Pi[i, :]
        B = sc.diag(p) - sc.outer(p, p)
        g_norm += l.dot(B.dot(l))

# Natural Conjugate Gradient
    d = g + g_norm / g_norm_old * d_old

    return d, g_norm
Esempio n. 15
0
    def read_from_do_cor(self, path):

        vac = fitsio.FITS(path)
        head = vac[1].read_header()
        self._llmin = head['LLMIN']
        self._llmax = head['LLMAX']
        self._dll = head['DLL']
        self._n1d = int((self._llmax - self._llmin) / self._dll + 1)

        ### All Matrix
        self._mat = {}
        self._mat["DA"] = vac[2]['DA'][:]
        self._mat["WE"] = vac[2]['WE'][:]
        self._mat["NB"] = vac[2]['NB'][:]

        ### Variance
        self._var = sp.zeros((self._n1d, 4))
        self._var[:, 0] = 10.**(sp.arange(self._n1d) * self._dll + self._llmin)
        self._var[:, 1] = sp.diag(self._mat["DA"])
        self._var[:, 2] = sp.diag(self._mat["WE"])
        self._var[:, 3] = sp.diag(self._mat["NB"])

        ### Correlation
        self._cor = sp.zeros((self._n1d, 4))
        self._cor[:, 0] = 10.**(sp.arange(self._n1d) * self._dll)

        inDown = False
        upperTri = sp.triu(self._mat["NB"])
        if (upperTri > 0.).sum() == 0:
            inDown = True
            self._cor[:, 0] = self._cor[:, 0][::-1]

        norm = 1.
        if sp.trace(self._mat["NB"]) > 0:
            norm = sp.sum(
                sp.diag(self._mat["DA"], k=0) *
                sp.diag(self._mat["WE"], k=0)) / sp.trace(self._mat["WE"],
                                                          offset=0)

        for i in range(self._n1d):
            d = i
            if inDown:
                d = 1 + i - self._n1d
            tda = sp.diag(self._mat["DA"], k=d)
            twe = sp.diag(self._mat["WE"], k=d)
            tnb = sp.trace(self._mat["NB"], offset=d)
            if tnb > 0:
                self._cor[i, 1] = sp.sum(tda * twe) / sp.sum(twe) / norm
                self._cor[i, 2] = sp.sum(twe) / norm
                self._cor[i, 3] = tnb

        vac.close()

        return
Esempio n. 16
0
def generateDIMask(theDIs, cons, sStruct, k=6,consthresh=0.95,ambigChar='X'):
	L,M = theDIs.shape
	assert L == M , "Non square DI matrix!?"
	DImask = S.ones(theDIs.shape)
	
	#
	#1 Sequence Separation
	#
	DImask = S.triu(DImask,k)
	
	#
	#2 Conservation Filter
	#
	#Scan the consensus sequence and find overly conserved columns, note those, and note conserved cysteine columns
	conservedSites = S.array([i != ambigChar for i in cons],dtype=S.int8)
	conservedCysteine = S.array([i == 'C' for i in cons],dtype=S.int8)
	
	DImask = (1-conservedSites).reshape(-1,1)*(DImask*(1-conservedSites))
	
	#
	#3 Cysteine columns may be strongly conserved, but only allow their best contact.
	#TODO : implement Cysteine Best-Friend pairs
	
	
	#
	#4 Secondary Structure Masking
	#Seet Table S2 in th EVFold paper
	#
	abovez = lambda x:max(x,0)
	for start,end,type in sStruct:
		if type is "H":
			#alpha helix
			#Table S2 in EVFold paper. Not how I would have done it.
			DImask[abovez(start-1):end+1,abovez(start-1):end+1] = 0.0 #Constraints H,H; H-1,H; H-1,H+1 in Table S2
			DImask[abovez(start-2):abovez(end-4),start+4:end+2] = 0.0 #Constraints H-2,H; (assumed) H,H+2 in Table S2
			DImask[abovez(start-3):abovez(end-9),start+9:end+3] = 0.0 #Constraints H-3,H; (assumed) H,H+3 in Table S2
			#TODO : Not mentioned in the paper, but what if helices are longer than this? We could just continue in a pattern!
		elif type is "E":
			#beta strand
			width = 1 if (end-start) < 5 else 2
			DImask[abovez(start-width):end+width,abovez(start-width):end+width] = 0.0 #constraints E,E ; E-1, E+1; E-2,E+2 in Table S2
			DImask[abovez(start-2):end,start:end] = 0.0#constraint E-2,E
			DImask[start:end,start:end+2] = 0.0#constraints (assumed) E,E+2
			DImask[abovez(start-3):end,start:end] = 0.0#constraint E-3,E
			DImask[start:end,start:end+3] = 0.0#constraints (assumed) E,E+3
			DImask[abovez(start-4):start,start+4:end] = 0.0#constraint E-4,E in table S2
			DImask[start:abovez(end-4),end:end+4] = 0.0#constraint (assumed) E,E+4 in table S2i
	
	
	return DImask
Esempio n. 17
0
    def backward(self):
        #######################################
        #      Start Backward Computation     #
        #######################################
        basics.niceprint("Start Backward Computation")

        # define function for dividing columns of a matrix by their respective norm

        def colnorm(M):
            norms = np.linalg.norm(M, axis=0, keepdims=True)
            return M / norms, norms

        # initialize random initial upper triangular matrix

        tri, _ = colnorm(sp.triu(np.random.rand(self.dim, self.dim)))

        for revnstep, (tpast, tfuture) in enumerate(
                zip(reversed(self.time_mainrun[0:-1]),
                    reversed(self.time_mainrun[1:]))):
            nstep = len(self.time_mainrun) - 1 - revnstep
            basics.printProgressBar(nstep,
                                    len(self.time_mainrun),
                                    prefix='Progress:',
                                    suffix='Complete',
                                    length=20)
            # solve R_{n,n+1}*X = C_{n+1}

            tri = sp.linalg.solve_triangular(self.R[nstep - 1, :, :], tri)

            # normlize upper triangular matrix
            tri, growth = colnorm(tri)

            # compute growth factor

            self.CLE[nstep -
                     1, :] = -np.log(growth) / (self.dt * self.rescale_rate)

            # change from triangular representation to normal coordinates

            self.CLV[nstep - 1, :, :] = np.matmul(self.BLV[nstep - 1, :, :],
                                                  tri)

            np.memmap.flush(self.CLV)
            np.memmap.flush(self.CLE)
        basics.printProgressBar(nstep,
                                len(self.time_mainrun),
                                prefix='Progress:',
                                suffix='Complete',
                                length=20)
Esempio n. 18
0
def optimal_gauss_kernel_size(train, optimize_steps,
                              progress=ProgressIndicator()):
    """ Return the optimal kernel size for a spike density estimation
    of a SpikeTrain for a gaussian kernel. This function takes a single
    spike train, which can be a superposition of multiple spike trains
    (created with :func:`collapsed_spike_trains`) that should be included
    in a spike density estimation.
    See (Shimazaki, Shinomoto. Journal of Computational Neuroscience. 2010).

    :param SpikeTrain train: The spike train for which the kernel
        size should be optimized.
    :param optimize_steps: Array of kernel sizes to try (the best of
        these sizes will be returned).
    :type optimize_steps: Quantity 1D
    :param progress: Set this parameter to report progress. Will be
        advanced by len(`optimize_steps`) steps.
    :type progress: :class:`spykeutils.progress_indicator.ProgressIndicator`
    :returns: Best of the given kernel sizes
    :rtype: Quantity scalar
    """
    x = sp.asarray(train.rescale(optimize_steps.units))
    steps = sp.asarray(optimize_steps)

    N = len(train)
    tau = sp.triu(sp.vstack([x] * N) - sp.vstack([x] * N).T, 1)
    idx = sp.triu(sp.ones((N,N)), 1)
    TAU = tau.T[idx.T==1]**2

    C = {}
    for s in steps:
        C[s] = N/s + 1/s * sum(2 * sp.exp(-TAU/(4 * s**2)) -
                               4 * sp.sqrt(2) * sp.exp(-TAU/(2 * s**2)))
        progress.step()

    # Return kernel size with smallest cost
    return min(C, key=C.get)*train.units
Esempio n. 19
0
    def add_intron(self, idx1, flag1, idx2, flag2):
        """adds new introns into splicegraph between idx1 and idx2"""

        ### if flag1, all end terminal exons in idx1 are preserved
        ### if flag2, all start terminal exons in idx2 are preserved

        if idx2.shape[0] > 0:
            adj_mat = sp.triu(self.edges)

            if flag1:
                for i1 in idx1:

                    ### if exon is end-terminal
                    if sp.all(adj_mat[i1, :] == 0):

                        self.vertices = sp.c_[self.vertices, self.vertices[:,
                                                                           i1]]

                        self.new_edge()
                        self.edges[:, -1] = self.edges[:, i1]
                        self.edges[-1, :] = self.edges[i1, :]

                        self.terminals = sp.c_[self.terminals,
                                               self.terminals[:, i1]]
            if flag2:
                for i2 in idx2:
                    ### if exon is start-terminal
                    if sp.all(adj_mat[:, i2] == 0):
                        self.vertices = sp.c_[self.vertices, self.vertices[:,
                                                                           i2]]

                        self.new_edge()
                        self.edges[:, -1] = self.edges[:, i2]
                        self.edges[-1, :] = self.edges[i2, :]

                        self.terminals = sp.c_[self.terminals,
                                               self.terminals[:, i2]]

        for i1 in idx1:
            for i2 in idx2:
                self.edges[i1, i2] = 1
                self.edges[i2, i1] = 1

        self.uniquify()
Esempio n. 20
0
    def aic(self, X):
        """Akaike information criterion for the current model fit
        and the proposed data

        Parameters
        ----------
        X : array of shape(n_samples, n_dimensions)

        Returns
        -------
        aic: float (the greater the better)
        """
        n_vars = self.means_.shape[1]
        score = self.score(X).sum()
        df = 0
        for k in range(self.n_components):
            df += (np.abs(scipy.triu(self.precs_[k])) > self.tol).sum()
            df += n_vars

        return score - df
Esempio n. 21
0
    def Read_SymMatrix(self, name):
        self.file.seek(0, 0)

        # Initialize the C matrix
        C = numpy.zeros((6,6), self.dtype)

        # By default, the elastic type is general
        elastic_type = 'general'
        
        myline = self.file.readline()
        while ( myline != "" and myline != '$' + name +'\n' ):
            myline = self.file.readline()
    
        try:
            for i in range(6):
                myline = self.file.readline()
                myline.strip()
                elements = myline.split()
                for j in range(i, 6):
                    C[i, j] = float(elements[j])
            C = C + triu(C,1).T.astype(self.dtype)
            
            # Check if the elastic type is orthotropic
            temp = True
            for (i, j) in [(0,3),(0,4),(0,5),(1,3),(1,4),(1,5),(2,3),(2,4),(2,5),(3,4),(3,5),(4,5)]:
                temp = temp and C[i, j] == 0
            if(temp):
                elastic_type = 'orthotropic'
            
            # Check if the elastic type is isotropic
            if((C[0, 1] == C[0, 2]) and (C[0, 2] == C[1, 2]) \
                and (C[3, 3] == C[4, 4]) and (C[4, 4] == C[5, 5]) \
                and (C[0, 0] == C[0, 1] + C[3, 3])):
                    elastic_type = 'isotropic'
        except:
            if self.print_output:
                print self.filename, 'contains no valid', name, 'section.'
            return None
        if (self.file.readline() != '$End' + name + '\n') and self.print_output:
            print self.filename, 'has an invalid', name, 'section.'
        return C, elastic_type
Esempio n. 22
0
    def add_intron(self, idx1, flag1, idx2, flag2):
        """adds new introns into splicegraph between idx1 and idx2"""

        ### if flag1, all end terminal exons in idx1 are preserved
        ### if flag2, all start terminal exons in idx2 are preserved

        if idx2.shape[0] > 0:
            adj_mat = sp.triu(self.edges)

            if flag1:
                for i1 in idx1:

                    ### if exon is end-terminal
                    if sp.all(adj_mat[i1, :] == 0):

                        self.vertices = sp.c_[self.vertices, self.vertices[:, i1]]

                        self.new_edge()
                        self.edges[:, -1] = self.edges[:, i1]
                        self.edges[-1, :] = self.edges[i1, :]

                        self.terminals = sp.c_[self.terminals, self.terminals[:, i1]]
            if flag2:
                for i2 in idx2:
                    ### if exon is start-terminal
                    if sp.all(adj_mat[:, i2] == 0):
                        self.vertices = sp.c_[self.vertices, self.vertices[:, i2]]

                        self.new_edge()
                        self.edges[:, -1] = self.edges[:, i2]
                        self.edges[-1, :] = self.edges[i2, :]

                        self.terminals = sp.c_[self.terminals, self.terminals[:, i2]]

        for i1 in idx1:
            for i2 in idx2:
                self.edges[i1, i2] = 1
                self.edges[i2, i1] = 1
        
        self.uniquify()
Esempio n. 23
0
    def add_intron_retention(self, idx1, idx2):
        
        adj_mat = sp.triu(self.edges)

        self.vertices = sp.c_[self.vertices, sp.array([self.vertices[0, idx1], self.vertices[1, idx2]], dtype='int')]

        self.new_edge()

        adj_mat = sp.r_[adj_mat, sp.zeros((1, adj_mat.shape[1]), dtype='int')]
        adj_mat = sp.c_[adj_mat, sp.zeros((adj_mat.shape[0], 1), dtype='int')]

        ### check if adjacency matrix is symmetric
        ### otherwise or is not justyfied
        assert(sp.all(sp.all(adj_mat - (self.edges - adj_mat).T == 0)))

        ### AK: under the assumption that our splice graph representation is symmetric
        ### I preserve symmetry by using OR over the adj_mat column and row
        
        self.edges[:, -1] = adj_mat[:, idx1] | adj_mat[idx2, :].T
        self.edges[-1, :] = adj_mat[:, idx1].T | adj_mat[idx2, :]

        self.terminals = sp.c_[self.terminals, sp.array([self.terminals[0, idx1], self.terminals[1, idx2]], dtype='int')]
Esempio n. 24
0
def factLU(A):
    n=len(A)
    for k in range(n-1):
        if s.absolute(A[k][k])<1e-20:
            print('Pivote cercano a cero en k=%d' % k)
            L=[[0.0]*n for i in range(n)]
            U=[[0.0]*n for i in range(n)]
            return L,U
        else:
            for i in range(k+1,n):
                A[i][k] = A[i][k]/A[k][k]
                for j in range(k+1,n):
                    A[i][j] = A[i][j]-A[i][k]*A[k][j]

    #==================================
    #OBSERVE QUE: 
    #Las siguientes lineas en realidad no hacen falta
    U=s.triu(A)
    L=s.tril(A,-1)
    for i in range(n):
        L[i][i] =1.0
    return L,U
Esempio n. 25
0
def parameters(cmdargs):
    """
    cmdargs:
             -q, qubits 
             -k, lrule
             -f, nmems
    """

    # The Hopfield parameters
    hparams = {
        'numNeurons': cmdargs['qubits'],
        'inputState': [ 2*sp.random.random_integers(0,1)-1 
                        for k in xrange(cmdargs['qubits']) ],
        'learningRule': cmdargs['simtype'],
        'numMemories': int(cmdargs['farg'])
        }

    # Construct memories
    memories = [ [ 2*sp.random.random_integers(0,1)-1 
                   for k in xrange(hparams['numNeurons']) ]
                 for j in xrange(hparams['numMemories']) ]

    # At least one pattern must be one Hamming unit away from the input
    memories[0] = list(hparams['inputState'])
    memories[0][sp.random.random_integers(0,hparams['numNeurons']-1)] *= -1

    # Make sure all other patterns have Hamming distance > 1
    def hamdist(a,b):
        """ Calculate Hamming distance. """
        return sp.sum(abs(sp.array(a)-sp.array(b))/2.0)
    # Loop over additional memories, if there are any
    for imem, mem in enumerate(memories[1:]):
        while hamdist(mem, hparams['inputState']) < 2.0:
            # Flip a random spin
            rndbit = sp.random.random_integers(0,hparams['numNeurons']-1)
            memories[imem+1][rndbit] *= -1
        
    # Basic simulation params
    nQubits = hparams['numNeurons']
    T = 1000.0 # sp.arange(0.1, 15, 0.5)
    # T = sp.array([10.0, 20.0, 50.0, 100.0])
    dt = 0.01*T

    # Define states for which to track probabilities in time
    # import statelabels
    # label_list = statelabels.GenerateLabels(nQubits)
    # stateoverlap = []
    # for mem in memories:
    #     # Convert spins to bits
    #     bitstr = ''.join([ '0' if k == 1 else '1' for k in mem ])
    #     # Get the index of the current (converted) memory and add it to list
    #     stateoverlap.append([ label_list.index(bitstr), bitstr ])
    stateoverlap = None

    # Output parameters
    binary = 1 # Save output files as binary Numpy format
    progressout = 0 # Output simulation progress over anneal timesteps

    eigspecdat = 1 # Output data for eigspec
    eigspecplot = 0 # Plot eigspec
    eigspecnum = 2**nQubits # Number of eigenvalues to output
    fidelplot = 0 # Plot fidelity
    fideldat = 0 # Output fidelity data
    fidelnumstates = 2**nQubits # Check fidelity with this number of eigenstates
    overlapdat = 0 # Output overlap data
    overlapplot = 0 # Plot overlap
    solveMethod = 'ExpPert' # 'ExpPert', 'SuzTrot', 'ForRuth', 'BCM'

    # Output directory stuff
    probdir = 'data/hopfield_exp1_tuned/n'+str(nQubits)+'p'+\
        str(hparams['numMemories'])+hparams['learningRule']
    if isinstance(T, collections.Iterable):
        probdir += 'MultiT'
    if os.path.isdir(probdir):
        outlist = sorted([ int(name) for name in os.listdir(probdir) 
                           if name.isdigit() ])
    else:
        outlist = []
    outnum = outlist[-1] + 1 if outlist else 0
    outputdir = probdir + '/' + str(outnum) + '/'

    probshow = 0 # Print final state probabilities to screen
    probout = 1 # Output probabilities to file
    mingap = 0 # Record the minimum spectral gap

    errchk = 0 # Error-checking on/off (for simulation accuracy)
    eps = 0.01 # Numerical error in normalization condition (1 - norm < eps)

    # Specify a QUBO (convert to Ising = True), or alpha, beta directly 
    # (convert = False), and also specify the signs on the Ising Hamiltonian 
    # terms (you can specify coefficients too for some problems if needed)
    isingConvert = 0
    isingSigns = {'hx': -1, 'hz': -1, 'hzz': -1}

    # Construct network Ising parameters
    neurons = nQubits

    # This is gamma, the appropriate weighting on the input vector
    # isingSigns['hz'] *= 1 - (len(hparams['inputState']) - 
    #                          hparams['inputState'].count(0))/(2*neurons)
    # isingSigns['hz'] *= 1.0/(5*neurons)
    isingSigns['hz'] *= 0.2

    alpha = sp.array(hparams['inputState'])
    beta = sp.zeros((neurons,neurons))
    delta = sp.array([])

    # Construct the memory matrix according to a learning rule
    if hparams['learningRule'] == 'hebb':
        # Hebb rule
        isingSigns['hz'] *= 0.7
        memMat = sp.matrix(memories).T
        beta = sp.triu(memMat*memMat.T)/float(neurons)
    elif hparams['learningRule'] == 'stork':
        # Storkey rule
        isingSigns['hz'] *= 0.15
        Wm = sp.zeros((neurons,neurons))
        for m, mem in enumerate(memories):
            Am = sp.outer(mem,mem) - sp.eye(neurons)
            Wm += (Am - Am*Wm - Wm*Am)/float(neurons)
        beta = sp.triu(Wm)
    elif hparams['learningRule'] == 'proj':
        isingSigns['hz'] *= 0.15
        # Moore-Penrose pseudoinverse rule
        memMat = sp.matrix(memories).T
        beta = sp.triu(memMat * sp.linalg.pinv(memMat))

    # Some outputs
    outputs = {
        'nQubits': nQubits,
        'learningRule': hparams['learningRule'],
        'outdir': probdir,
        'inputState': hparams['inputState'],
        'memories': memories,
        'answer': memories[0],
        'annealTime': list(T) if isinstance(T, collections.Iterable) else T
               }

    ############################################################################
    ######## All variables must be specified here, do NOT change the keys ######
    ############################################################################

    return {
        'nQubits': nQubits,
        'Q': None,
        'T': T,
        'dt': dt,
        'outputdir': outputdir,
        'errchk': errchk,
        'eps': eps,
        'isingConvert': isingConvert,
        'isingSigns': isingSigns,
        'outputs': outputs,
        'alpha': alpha,
        'beta': beta,
        'delta': delta,
        'eigdat': eigspecdat,
        'eigplot': eigspecplot,
        'eignum': eigspecnum,
        'fiddat': fideldat,
        'fidplot': fidelplot,
        'fidnumstates': fidelnumstates,
        'overlapdat': overlapdat,
        'overlapplot': overlapplot,
        'outdir': outputdir,
        'binary': binary,
        'progressout': progressout,
        'probshow': probshow,
        'probout': probout,
        'mingap': mingap,
        'stateoverlap': stateoverlap,
        'hzscale': None,
        'hzzscale': None,
        'hxscale': None,
        'solveMethod': solveMethod
        }
Esempio n. 26
0
def detect_multipleskips(genes, idx_alt):
    # [idx_multiple_skips, exon_multiple_skips, id_multiple_skips] = detect_multipleskips(genes, idx_alt) ;

    id = 0
    idx_multiple_skips = []
    id_multiple_skips = []
    exon_multiple_skips = []
    for ix in idx_alt:
        if ix % 50 == 0:
            print '.',
        num_exons = genes[ix].splicegraph.get_len()
        edges = genes[ix].splicegraph.edges
        labels = repmat(sp.arange(num_exons), num_exons, 1).T
        
        # adjecency matrix: upper half only
        A = sp.zeros((num_exons, num_exons))
        for i in range(num_exons - 1):
            for j in range(i + 1, num_exons):
                A[i, j] = edges[i, j]
        
        # possible starting and ending exons of a multiple exon skip
        Pairs = lil_matrix((num_exons, num_exons))
        Ai = sp.dot(sp.dot(A, A), A) #paths of length 3
        while sp.any(Ai.ravel() > 0):
            coords = sp.where((A > 0 ) & (Ai > 0)) # multiple skip
            Pairs[coords[0], coords[1]] = 1
            Ai = sp.dot(Ai, A)  # paths of length ..+1
        
        edge = sp.where(Pairs.toarray() == 1)
        
        if edge[0].shape[0] > 10000:
            print 'Warning: not processing gene %d, because there are more than 10000 potential hits.' % ix
            continue
        
        for cnt in range(edge[0].shape[0]):
            exon_idx_first = edge[0][cnt]
            exon_idx_last = edge[1][cnt]
      
            if edges[exon_idx_first, exon_idx_last] == 1:
                
                # find all pairs shortest path
                exist_path = sp.triu(edges).astype('double')
                exist_path[exon_idx_first, exon_idx_last] = 0
                exist_path[exist_path == 0] = sp.inf
                # set diagonal to 0
                exist_path[sp.arange(exist_path.shape[0]), sp.arange(exist_path.shape[0])] = 0
                
                long_exist_path = -sp.triu(edges).astype('double')
                long_exist_path[exon_idx_first, exon_idx_last] = 0
                long_exist_path[long_exist_path == 0] = sp.inf
                # set diagonal to 0
                long_exist_path[sp.arange(long_exist_path.shape[0]), sp.arange(long_exist_path.shape[0])] = 0
                
                path = sp.isfinite(exist_path) * labels
                long_path = sp.isfinite(long_exist_path) * labels
                
                for k in range(num_exons):
                    for i in range(num_exons):
                        idx = sp.where((exist_path[i, k] + exist_path[k, :]) < exist_path[i, :])[0]
                        exist_path[i, idx] = exist_path[i, k] + exist_path[k, idx]
                        path[i, idx] = path[k, idx]
                        
                        idx = sp.where((long_exist_path[i,k] + long_exist_path[k, :]) < long_exist_path[i, :])[0]
                        long_exist_path[i, idx] = long_exist_path[i, k] + long_exist_path[k, idx]
                        long_path[i, idx] = long_path[k, idx]
                
                temp_ix = sp.isfinite(long_exist_path)
                long_exist_path[temp_ix] = -long_exist_path[temp_ix]
                
                if (exist_path[exon_idx_first, exon_idx_last] > 2) and sp.isfinite(exist_path[exon_idx_first, exon_idx_last]):
                    backtrace = sp.array([path[exon_idx_first, exon_idx_last]])
                    while backtrace[-1] > exon_idx_first:
                        backtrace = sp.r_[backtrace, path[exon_idx_first, backtrace[-1]]]
                    backtrace = backtrace[:-1]
                    backtrace = backtrace[::-1]
                    idx_multiple_skips.append(ix) #repmat(ix, 1, backtrace.shape[0] + 2))
                    exon_multiple_skips.append([exon_idx_first, backtrace, exon_idx_last])
                    id_multiple_skips.append(id) # * sp.ones((1, backtrace.shape[0] + 2)))
                    id += 1
                elif (long_exist_path[exon_idx_first, exon_idx_last] > 2) and sp.isfinite(long_exist_path[exon_idx_first, exon_idx_last]):
                    backtrace = sp.array([long_path[exon_idx_first, exon_idx_last]])
                    while backtrace[-1] > exon_idx_first:
                        backtrace = sp.r_[backtrace, long_path[exon_idx_first, backtrace[-1]]]
                    backtrace = backtrace[:-1]
                    backtrace = backtrace[::-1]
                    idx_multiple_skips.append(ix) #repmat(ix, 1, backtrace.shape[0] + 2))
                    exon_multiple_skips.append([exon_idx_first, backtrace, exon_idx_last])
                    id_multiple_skips.append(id) # * sp.ones((1, backtrace.shape[0] + 2)))
                    id += 1

    print 'Number of multiple exon skips:\t\t\t\t\t%d' % len(idx_multiple_skips)
    return (idx_multiple_skips, exon_multiple_skips, id_multiple_skips)
Esempio n. 27
0
def parameters(cmdargs):
    """
    cmdargs:
             -q, qubits 
             -k, lrule
             -f, nmems
    """

    # The Hopfield parameters
    hparams = {
        'numNeurons': cmdargs['qubits'],
        'inputState': [ 2*sp.random.random_integers(0,1)-1 
                        for k in xrange(cmdargs['qubits']) ],
        'learningRule': cmdargs['simtype'],
        'numMemories': int(cmdargs['farg'])
        }

    # Construct memories
    memories = [ [ 2*sp.random.random_integers(0,1)-1 
                   for k in xrange(hparams['numNeurons']) ]
                 for j in xrange(hparams['numMemories']) ]

    # At least one pattern must be one Hamming unit away from the input
    memories[0] = list(hparams['inputState'])
    memories[0][sp.random.random_integers(0,hparams['numNeurons']-1)] *= -1

    # Make sure all other patterns have Hamming distance > 1
    def hamdist(a,b):
        """ Calculate Hamming distance. """
        return sp.sum(abs(sp.array(a)-sp.array(b))/2.0)
    # Loop over additional memories, if there are any
    for imem, mem in enumerate(memories[1:]):
        # Avoid duplication and ensure we're far away enough from the input
        while (memories.count(mem) > 1 or 
               hamdist(mem, hparams['inputState']) < 2.0):
            # Flip a random spin
            rndbit = sp.random.random_integers(0,hparams['numNeurons']-1)
            memories[imem+1][rndbit] *= -1
        
    # Basic simulation params
    nQubits = hparams['numNeurons']
    T = 1000.0 # sp.arange(0.1, 15, 0.5)
    # T = sp.array([10.0, 20.0, 50.0, 100.0])
    dt = 0.01*T

    # Define states for which to track probabilities in time
    # import statelabels
    # label_list = statelabels.GenerateLabels(nQubits)
    # stateoverlap = []
    # for mem in memories:
    #     # Convert spins to bits
    #     bitstr = ''.join([ '0' if k == 1 else '1' for k in mem ])
    #     # Get the index of the current (converted) memory and add it to list
    #     stateoverlap.append([ label_list.index(bitstr), bitstr ])
    stateoverlap = None

    # Output parameters
    binary = 1 # Save output files as binary Numpy format
    progressout = 0 # Output simulation progress over anneal timesteps

    eigspecdat = 1 # Output data for eigspec
    eigspecplot = 0 # Plot eigspec
    eigspecnum = 8 # Number of eigenvalues to output
    fidelplot = 0 # Plot fidelity
    fideldat = 0 # Output fidelity data
    fidelnumstates = 2**nQubits # Check fidelity with this number of eigenstates
    overlapdat = 0 # Output overlap data
    overlapplot = 0 # Plot overlap
    solveMethod = 'ExpPert' # 'ExpPert', 'SuzTrot', 'ForRuth', 'BCM'

    # Output directory stuff
    probdir = 'data/hopfield_opt_gamma/n'+str(nQubits)+'p'+\
        str(hparams['numMemories'])+hparams['learningRule']
    if isinstance(T, collections.Iterable):
        probdir += 'MultiT'
    if os.path.isdir(probdir):
        outlist = sorted([ int(name) for name in os.listdir(probdir) 
                           if name.isdigit() ])
    else:
        outlist = []
    outnum = outlist[-1] + 1 if outlist else 0
    outputdir = probdir + '/' + str(outnum) + '/'

    probshow = 0 # Print final state probabilities to screen
    probout = 1 # Output probabilities to file
    mingap = 0 # Record the minimum spectral gap

    errchk = 0 # Error-checking on/off (for simulation accuracy)
    eps = 0.01 # Numerical error in normalization condition (1 - norm < eps)

    # Specify a QUBO (convert to Ising = True), or alpha, beta directly 
    # (convert = False), and also specify the signs on the Ising Hamiltonian 
    # terms (you can specify coefficients too for some problems if needed)
    isingConvert = 0
    isingSigns = {'hx': -1, 'hz': -1, 'hzz': -1}

    # Construct network Ising parameters
    neurons = nQubits

    alpha = sp.array(hparams['inputState'])
    beta = sp.zeros((neurons,neurons))
    delta = sp.array([])

    def kbits(n, k):
        " kbits(n: length of bitstring, k: number of ones) "
        result = []
        for bits in itertools.combinations(range(n), k):
            s = ['0'] * n
            for bit in bits:
                s[bit] = '1'
            result.append(''.join(s))
        return result

    def bitstr2spins(vec):
        """ Return converted list of spins from bitstring @vec. """
        return [ 1 if k == '1' else -1 for k in vec ]

    def calculate_gamma(Jmat, inp, mems):
        """
        Return a threshold+eps for Gamma by analyzing energies
        of the given memory matrix.
        """
        step = 0.001
        eps = 0.1*step
        Glist = np.arange(0,1.+step, step)
        Gthresh = []
        Minp = np.matrix(inp).T
        mem0 = np.matrix(mems[0]).T
        # The input state's energy
        Einp = -Glist*np.sum(Minp.T*Minp) + -np.sum(Minp.T*Jmat*Minp)
        # Loop backwards through Gamma (go high to low)
        for i in range(len(Glist))[::-1]:
            # Calculate new bias matrix
            bias = np.sum(np.array(inp)*np.array(mems[0]))
            # Compute the memory state energies
            lower = -np.sum(mem0.T*Jmat*mem0) - bias
            # Generate bitstrings
            bitstring = []
            for i in range(0,neurons+1): 
                bitstring.append(kbits(neurons, i))
            # Flatten, convert to spins
            spinstr = [ bitstr2spins(item) for item in 
                        list(itertools.chain.from_iterable(bitstring)) ]
            alllist = [ [-np.sum(np.matrix(v)*Jmat*np.matrix(v).T)-bias, v]
                        for v in spinstr ]
            # Sort by energy
            alllist = sorted(alllist, key=lambda x: x[0])
            indices = []
            upper = 0
            # Find the next energy level
            for irec, rec in enumerate(alllist):
                if len(indices) == 2*len(mems):
                    upper = rec[0]
                    break
                if rec[1] in mems:
                    indices.append(irec)
            # If this Gamma value gives the right input state energy,
            # keep it for later analysis
            if lower < Einp[i] and Einp[i] <= upper:
                Gthresh.append(Glist[i])
        Gthresh = list(set(Gthresh))
        # Now pick a Gamma in the middle somewhere
        if len(Gthresh) == 0:
            return 0
        elif len(Gthresh) == 1:
            return Gthresh[0]
        else:
            return Gthresh[(len(Gthresh)-1)/2]

    # Construct the memory matrix according to a learning rule
    if hparams['learningRule'] == 'hebb':
        # Hebb rule
        memMat = sp.matrix(memories).T
        beta = sp.triu(memMat*memMat.T)/float(neurons)
        isingSigns['hz'] *= calculate_gamma(beta, hparams['inputState'], memories)
    elif hparams['learningRule'] == 'stork':
        # Storkey rule
        Wm = sp.zeros((neurons,neurons))
        for m, mem in enumerate(memories):
            Am = sp.outer(mem,mem) - sp.eye(neurons)
            Wm += (Am - Am*Wm - Wm*Am)/float(neurons)
        beta = sp.triu(Wm)
        isingSigns['hz'] *= calculate_gamma(beta, hparams['inputState'], memories)
    elif hparams['learningRule'] == 'proj':
        # Moore-Penrose pseudoinverse rule
        memMat = sp.matrix(memories).T
        beta = sp.triu(memMat * sp.linalg.pinv(memMat))
        isingSigns['hz'] *= calculate_gamma(beta, hparams['inputState'], memories)

    # Some outputs
    outputs = {
        'nQubits': nQubits,
        'learningRule': hparams['learningRule'],
        'outdir': probdir,
        'inputState': hparams['inputState'],
        'memories': memories,
        'bias': -isingSigns['hz'],
        'answer': memories[0],
        'annealTime': list(T) if isinstance(T, collections.Iterable) else T
               }

    ############################################################################
    ######## All variables must be specified here, do NOT change the keys ######
    ############################################################################

    return {
        'nQubits': nQubits,
        'Q': None,
        'T': T,
        'dt': dt,
        'outputdir': outputdir,
        'errchk': errchk,
        'eps': eps,
        'isingConvert': isingConvert,
        'isingSigns': isingSigns,
        'outputs': outputs,
        'alpha': alpha,
        'beta': beta,
        'delta': delta,
        'eigdat': eigspecdat,
        'eigplot': eigspecplot,
        'eignum': eigspecnum,
        'fiddat': fideldat,
        'fidplot': fidelplot,
        'fidnumstates': fidelnumstates,
        'overlapdat': overlapdat,
        'overlapplot': overlapplot,
        'outdir': outputdir,
        'binary': binary,
        'progressout': progressout,
        'probshow': probshow,
        'probout': probout,
        'mingap': mingap,
        'stateoverlap': stateoverlap,
        'hzscale': None,
        'hzzscale': None,
        'hxscale': None,
        'solveMethod': solveMethod
        }
Esempio n. 28
0
def parameters(cmdargs):
    """
    """

    # Basic simulation params
    nQubits = int(cmdargs['simtype'])
    T = 10.0  # sp.arange(0.1, 15, 0.5)
    dt = 0.01*T

    # Output parameters
    binary = 1 # Save as binary Numpy
    progressout = 0 # Output simulation progress over anneal timesteps

    eigspecdat = 1 # Output data for eigspec
    eigspecplot = 0 # Plot eigspec
    eigspecnum = 2**nQubits # Number of eigenvalues
    fidelplot = 0 # Plot fidelity
    fideldat = 0 # Output fidelity data
    fidelnumstates = 2**nQubits # Check fidelity with this number of eigenstates
    overlapdat = 0 # Output overlap data
    overlapplot = 0 # Plot overlap
    solveMethod = 'ExpPert' # 'ExpPert', 'SuzTrot', 'ForRuth', 'BCM'

    # Output directory stuff
    probdir = 'data/random_n'+str(nQubits)
    if isinstance(T, collections.Iterable):
        probdir += 'MultiT'
    if os.path.isdir(probdir):
        outlist = sorted([ int(name) for name in os.listdir(probdir) 
                           if name.isdigit() ])
    else:
        outlist = []
    outnum = outlist[-1] + 1 if outlist else 0
    outputdir = probdir + '/' + str(outnum) + '/'

    probshow = 0 # Print final state probabilities to screen
    probout = 1 # Output probabilities to file
    mingap = 0 # Record the minimum spectral gap

    errchk = 0 # Error-checking on/off (for simulation accuracy)
    eps = 0.01 # Numerical error in normalization condition (1 - norm < eps)

    # Specify a QUBO (convert to Ising = True), or alpha, beta directly 
    # (convert = False), and also specify the signs on the Ising Hamiltonian 
    # terms (you can specify coefficients too for some problems if needed)
    isingConvert = 0
    isingSigns = {'hx': -1, 'hz': -1, 'hzz': -1}

    # Generate random matrix of coefficents between [-1,1]
    beta = sp.triu(2*sp.random.ranf((nQubits,nQubits)) - 1)
    alpha = 2*sp.random.ranf(nQubits) - 1
    delta = sp.array([])

    # Some outputs
    outputs = None

    ############################################################################
    ######## All variables must be specified here, do NOT change the keys ######
    ############################################################################

    return {
        'nQubits': nQubits,
        'Q': None,
        'T': T,
        'dt': dt,
        'outputdir': outputdir,
        'errchk': errchk,
        'eps': eps,
        'isingConvert': isingConvert,
        'isingSigns': isingSigns,
        'outputs': outputs,
        'alpha': alpha,
        'beta': beta,
        'delta': delta,
        'eigdat': eigspecdat,
        'eigplot': eigspecplot,
        'eignum': eigspecnum,
        'fiddat': fideldat,
        'fidplot': fidelplot,
        'fidnumstates': fidelnumstates,
        'overlapdat': overlapdat,
        'overlapplot': overlapplot,
        'outdir': outputdir,
        'binary': binary,
        'progressout': progressout,
        'probshow': probshow,
        'probout': probout,
        'mingap': mingap,
        'stateoverlap': None,
        'hzscale': None,
        'hzzscale': None,
        'hxscale': None,
        'solveMethod': solveMethod
        }
Esempio n. 29
0
def parameters(cmdargs):
    """
    """

    # The Hopfield parameters
    hparams = {
        'numNeurons':
        cmdargs['qubits'],
        'inputState': [
            2 * sp.random.random_integers(0, 1) - 1
            for k in xrange(cmdargs['qubits'])
        ],
        'learningRule':
        cmdargs['simtype'],
        'bias':
        float(cmdargs['farg']),
        'numMemories':
        sp.random.random_integers(1, cmdargs['qubits'])
    }

    # Construct memories
    memories = [[
        2 * sp.random.random_integers(0, 1) - 1
        for k in xrange(hparams['numNeurons'])
    ] for j in xrange(hparams['numMemories'])]

    # At least one pattern must be one Hamming unit away from the input
    memories[0] = list(hparams['inputState'])
    memories[0][sp.random.random_integers(0, hparams['numNeurons'] - 1)] *= -1

    # Make sure all other patterns have Hamming distance > 1
    def hamdist(a, b):
        """ Calculate Hamming distance. """
        return sp.sum(abs(sp.array(a) - sp.array(b)) / 2.0)

    # Loop over additional memories, if there are any
    for imem, mem in enumerate(memories[1:]):
        while hamdist(mem, hparams['inputState']) < 2.0:
            # Flip a random spin
            rndbit = sp.random.random_integers(0, hparams['numNeurons'] - 1)
            memories[imem + 1][rndbit] *= -1

    # Basic simulation params
    nQubits = hparams['numNeurons']
    T = 100.0
    dt = 0.01 * T

    # Output parameters
    binary = 1  # Save output files as binary Numpy format
    progressout = 0  # Output simulation progress over anneal timesteps

    eigspecdat = 1  # Output data for eigspec
    eigspecplot = 0  # Plot eigspec
    eigspecnum = 2**(nQubits - 1)  # Number of eigenvalues to output
    fidelplot = 0  # Plot fidelity
    fideldat = 0  # Output fidelity data
    fidelnumstates = 2**nQubits  # Check fidelity with this number of eigenstates
    overlapdat = 0  # Output overlap data
    overlapplot = 0  # Plot overlap
    solveMethod = 'ExpPert'  # 'ExpPert', 'SuzTrot', 'ForRuth', 'BCM'

    # Output directory stuff
    probdir = 'data/hopfield_gamma_random/' + hparams['learningRule']
    if isinstance(T, collections.Iterable):
        probdir += 'MultiT'
    if os.path.isdir(probdir):
        outlist = sorted(
            [int(name) for name in os.listdir(probdir) if name.isdigit()])
    else:
        outlist = []
    outnum = outlist[-1] + 1 if outlist else 0
    outputdir = probdir + '/' + str(outnum) + '/'

    probshow = 0  # Print final state probabilities to screen
    probout = 1  # Output probabilities to file
    mingap = 1  # Record the minimum spectral gap

    errchk = 0  # Error-checking on/off (for simulation accuracy)
    eps = 0.01  # Numerical error in normalization condition (1 - norm < eps)

    # Specify a QUBO (convert to Ising = True), or alpha, beta directly
    # (convert = False), and also specify the signs on the Ising Hamiltonian
    # terms (you can specify coefficients too for some problems if needed)
    isingConvert = 0
    isingSigns = {'hx': -1, 'hz': -1, 'hzz': -1}

    # Construct network Ising parameters
    neurons = nQubits

    # This is gamma, the appropriate weighting on the input vector
    # isingSigns['hz'] *= 1 - (len(hparams['inputState']) -
    #                          hparams['inputState'].count(0))/(2*neurons)
    isingSigns['hz'] *= hparams['bias']

    alpha = sp.array(hparams['inputState'])
    beta = sp.zeros((neurons, neurons))
    delta = sp.array([])

    # Construct the memory matrix according to a learning rule
    if hparams['learningRule'] == 'hebb':
        # Construct pattern matrix according to Hebb's rule
        for i in range(neurons):
            for j in range(neurons):
                for p in range(len(memories)):
                    beta[i, j] += (memories[p][i] * memories[p][j] -
                                   len(memories) * (i == j))
        beta = sp.triu(beta) / float(neurons)
    elif hparams['learningRule'] == 'stork':
        # Construct the memory matrix according to the Storkey learning rule
        memMat = sp.zeros((neurons, neurons))
        for m, mem in enumerate(memories):
            for i in range(neurons):
                for j in range(neurons):
                    hij = sp.sum(
                        [memMat[i, k] * mem[k] for k in range(neurons)])
                    hji = sp.sum(
                        [memMat[j, k] * mem[k] for k in range(neurons)])
                    # Don't forget to make the normalization a float!
                    memMat[i,
                           j] += 1. / neurons * (mem[i] * mem[j] -
                                                 mem[i] * hji - hij * mem[j])
        beta = sp.triu(memMat)
    elif hparams['learningRule'] == 'proj':
        # Construct memory matrix according to the Moore-Penrose pseudoinverse rule
        memMat = sp.matrix(memories).T
        beta = sp.triu(memMat * sp.linalg.pinv(memMat))

    # Some outputs
    outputs = {
        'nQubits': nQubits,
        'learningRule': hparams['learningRule'],
        'outdir': probdir,
        'inputState': hparams['inputState'],
        'memories': memories,
        'answer': memories[0],
        'bias': hparams['bias'],
        'annealTime': list(T) if isinstance(T, collections.Iterable) else T
    }

    ############################################################################
    ######## All variables must be specified here, do NOT change the keys ######
    ############################################################################

    return {
        'nQubits': nQubits,
        'Q': None,
        'T': T,
        'dt': dt,
        'outputdir': outputdir,
        'errchk': errchk,
        'eps': eps,
        'isingConvert': isingConvert,
        'isingSigns': isingSigns,
        'outputs': outputs,
        'alpha': alpha,
        'beta': beta,
        'delta': delta,
        'eigdat': eigspecdat,
        'eigplot': eigspecplot,
        'eignum': eigspecnum,
        'fiddat': fideldat,
        'fidplot': fidelplot,
        'fidnumstates': fidelnumstates,
        'overlapdat': overlapdat,
        'overlapplot': overlapplot,
        'outdir': outputdir,
        'binary': binary,
        'progressout': progressout,
        'probshow': probshow,
        'probout': probout,
        'mingap': mingap,
        'stateoverlap': None,
        'solveMethod': solveMethod
    }
Esempio n. 30
0
    print('generating GTEx intron list')
    genes = pickle.load(open(GTEX, 'rb'), encoding='latin')
    gtex_introns = dict()
    gtex_exons = dict()

    for j, g in enumerate(genes[0]):
        if j > 0 and j % 100 == 0:
            sys.stdout.write('.')
            if j % 1000 == 0:
                sys.stdout.write('%i/%i\n' % (j, genes[0].shape[0]))
            sys.stdout.flush()
        sg_edges = spsp.coo_matrix(
            (g.splicegraph_edges_data,
             (g.splicegraph_edges_row, g.splicegraph_edges_col)),
            g.splicegraph_edges_shape).toarray()
        x, y = sp.where(sp.triu(sg_edges))
        for i in range(x.shape[0]):
            intron = ':'.join([
                g.chr,
                str(g.splicegraph.vertices[1, x[i]]),
                str(g.splicegraph.vertices[0, y[i]])
            ])
            try:
                gtex_introns[g.chr].append(intron)
            except KeyError:
                gtex_introns[g.chr] = [intron]
        for i in range(g.splicegraph.vertices.shape[1]):
            exon = ':'.join([
                g.chr,
                str(g.splicegraph.vertices[0, i]),
                str(g.splicegraph.vertices[1, i])
Esempio n. 31
0
        sys.exit(1)


if __name__ == "__main__":
    machEps = macheps()
    print "Python eps =", machEps
    #print "FORTRAN eps =", dlamch('E')
    #print "FORTRAN safe min =", dlamch('S')
    #print "FORTRAN base of the machine =", dlamch('B')
    #print "FORTRAN eps*base =", dlamch('P')
    #print "FORTRAN number of (base) digits in the mantissa =", dlamch('N')
    #print "FORTRAN rmax = ", dlamch('O')

    N = 3
    M = 3
    A0 = triu(rand(M, N) + 1.j * rand(M, N))
    A1 = copy.deepcopy(A0)
    A2 = zeros((M, N), 'D', 2)
    A2[:] = A0
    print A0
    #for j in range(A1.shape[0]):
    #A2[j] = A0[j]
    LIB_G2C = 'g2c'  # for gcc >= 4.3.0, LIB_G2C = 'gfortran'
    from scipy import linalg
    X1 = linalg.pinv(A1)  # the pseudo inverse
    X2 = computeMyPinvCC(A2, LIB_G2C)
    X3 = computeTriangleUpSolve(A1, LIB_G2C)
    print
    print "X1 - X2 = "
    print X1 - X2
    print "X1 - X3 = "
Esempio n. 32
0
tlogsched = np.array(
    [tempstart / (np.log(k + 1) * trlog + 1) for k in xrange(tannealingsteps)])
# hack
tannealingsched = texpsched
annealingsched = expsched


def getbitstr(vec):
    """ Return bitstring from spin vector array. """
    return reduce(lambda x, y: x + y,
                  [str(int(k)) for k in tools.spins2bits(vec)])


# Construct Ising matrix
memMat = sp.matrix(memories).T
isingJ = sp.triu(memMat * sp.linalg.pinv(memMat))
isingJ -= sp.diag(sp.diag(isingJ))
isingJ = sp.triu(memMat * sp.linalg.pinv(memMat))
isingJ += inpbias * sp.diag(vinput)
isingJ = sps.dok_matrix(isingJ)
# get energies of all states
results = []
energies = np.zeros(2**nspins)
for b in [bin(x)[2:].rjust(nspins, '0') for x in range(2**nspins)]:
    svec = tools.bits2spins(np.array([int(k) for k in b]))
    energies[int(b, 2)] = sa.ClassicalIsingEnergy(svec, isingJ)
    results.append([energies[int(b, 2)], b])
print("All possible states and their energies:")
for res in sorted(results)[:10]:
    print("%s    %2.4f" % tuple(res[::-1]))
Esempio n. 33
0
 def update_terminals(self):
     
     self.terminals = sp.zeros(self.vertices.shape, dtype='int')
     self.terminals[0, sp.where(sp.sum(sp.tril(self.edges), axis=1) == 0)[0]] = 1
     self.terminals[1, sp.where(sp.sum(sp.triu(self.edges), axis=1) == 0)[0]] = 1
Esempio n. 34
0
def parameters(cmdargs):
    """
    """

    # The Hopfield parameters
    hparams = {
        'numNeurons': 4,
        'inputState': [1,-1,1,-1],
        'learningRule': cmdargs['simtype'],
        'bias': float(cmdargs['farg'])
        }

    # Construct memories
    # memories = sp.linalg.hadamard(hparams['numNeurons']).tolist()
    # memories = memories[:1]
    memories = [[1,-1,1,-1], 
                [-1,1,1,1],
                [-1,-1,1,1],
                [1,-1,1,1]]
    memories = [memories[0]]
    # Basic simulation params
    nQubits = hparams['numNeurons']
    T = 1000.0
    # dt = 0.01*T
    dt = 0.001*T

    # Output parameters
    binary = 1 # Save output files as binary Numpy format
    progressout = 0 # Output simulation progress over anneal timesteps

    eigspecdat = 1 # Output data for eigspec
    eigspecplot = 0 # Plot eigspec
    eigspecnum = 2**(nQubits-1) # Number of eigenvalues to output
    fidelplot = 0 # Plot fidelity
    fideldat = 0 # Output fidelity data
    fidelnumstates = 2**nQubits # Check fidelity with this number of eigenstates
    overlapdat = 0 # Output overlap data
    overlapplot = 0 # Plot overlap
    solveMethod = 'ExpPert' # 'ExpPert', 'SuzTrot', 'ForRuth', 'BCM'

    # Output directory stuff
    probdir = 'data/testcases_fixed/ortho/n'+str(nQubits)+'p'+\
        str(len(memories))+hparams['learningRule']
    if isinstance(T, collections.Iterable):
        probdir += 'MultiT'
    if os.path.isdir(probdir):
        outlist = sorted([ int(name) for name in os.listdir(probdir) 
                           if name.isdigit() ])
    else:
        outlist = []
    outnum = outlist[-1] + 1 if outlist else 0
    outputdir = probdir + '/' + str(outnum) + '/'

    probshow = 0 # Print final state probabilities to screen
    probout = 1 # Output probabilities to file
    mingap = 1 # Record the minimum spectral gap

    errchk = 0 # Error-checking on/off (for simulation accuracy)
    eps = 0.01 # Numerical error in normalization condition (1 - norm < eps)

    # Specify a QUBO (convert to Ising = True), or alpha, beta directly 
    # (convert = False), and also specify the signs on the Ising Hamiltonian 
    # terms (you can specify coefficients too for some problems if needed)
    isingConvert = 0
    isingSigns = {'hx': -1, 'hz': -1, 'hzz': -1}

    # Construct network Ising parameters
    neurons = nQubits

    # This is gamma, the appropriate weighting on the input vector
    # isingSigns['hz'] *= 1 - (len(hparams['inputState']) - 
    #                          hparams['inputState'].count(0))/(2*neurons)
    isingSigns['hz'] *= hparams['bias']

    alpha = sp.array(hparams['inputState'])
    beta = sp.zeros((neurons,neurons))
    delta = sp.array([])

    # Construct the memory matrix according to a learning rule
    if hparams['learningRule'] == 'hebb':
        # Construct pattern matrix according to Hebb's rule
        memMat = sp.matrix(memories).T
        beta = sp.triu(memMat*memMat.T)/float(neurons)
    elif hparams['learningRule'] == 'stork':
        # Construct the memory matrix according to the Storkey learning rule
        Wm = sp.zeros((neurons,neurons))
        for m, mem in enumerate(memories):
            Am = sp.mat((sp.outer(mem,mem) - sp.eye(neurons)))
            Wm += (Am - Am*Wm - Wm*Am)/float(neurons)
        beta = sp.triu(Wm)
    elif hparams['learningRule'] == 'proj':
        # Construct memory matrix according to the Moore-Penrose pseudoinverse rule
        memMat = sp.matrix(memories).T
        beta = sp.triu(memMat * sp.linalg.pinv(memMat))

    # Some outputs
    outputs = {
        'nQubits': nQubits,
        'learningRule': hparams['learningRule'],
        'outdir': probdir,
        'inputState': hparams['inputState'],
        'memories': memories,
        'answer': memories[0],
        'bias': hparams['bias'],
        'annealTime': list(T) if isinstance(T, collections.Iterable) else T
               }

    ############################################################################
    ######## All variables must be specified here, do NOT change the keys ######
    ############################################################################

    return {
        'nQubits': nQubits,
        'Q': None,
        'T': T,
        'dt': dt,
        'outputdir': outputdir,
        'errchk': errchk,
        'eps': eps,
        'isingConvert': isingConvert,
        'isingSigns': isingSigns,
        'outputs': outputs,
        'alpha': alpha,
        'beta': beta,
        'delta': delta,
        'eigdat': eigspecdat,
        'eigplot': eigspecplot,
        'eignum': eigspecnum,
        'fiddat': fideldat,
        'fidplot': fidelplot,
        'fidnumstates': fidelnumstates,
        'overlapdat': overlapdat,
        'overlapplot': overlapplot,
        'outdir': outputdir,
        'binary': binary,
        'progressout': progressout,
        'probshow': probshow,
        'probout': probout,
        'mingap': mingap,
        'stateoverlap': None,
        'hzscale': None,
        'hzzscale': None,
        'hxscale': None,
        'solveMethod': solveMethod
        }
Esempio n. 35
0
def parameters(cmdargs):
    """
    """

    # The Hopfield parameters
    hparams = {
        'numNeurons': cmdargs['qubits'],
        'inputState': None,
        'learningRule': cmdargs['simtype'],
        'numMemories': 0
    }

    # Set input state
    inpst = sp.eye(cmdargs['qubits'])[0, :] - 1
    inpst[inpst == 0] = 1
    hparams['inputState'] = inpst.tolist()

    # Construct memories
    imems = cmdargs['instance']
    memories = sp.eye(cmdargs['qubits'])[0:imems, :]
    memories[memories == 0] -= 1
    memories = memories.tolist()

    # Basic simulation params
    nQubits = hparams['numNeurons']
    T = 15.  # sp.arange(0.1, 15, 0.5)
    dt = 0.01 * T

    # Define states for which to track probabilities in time
    import statelabels
    label_list = statelabels.GenerateLabels(nQubits)
    stateoverlap = []
    for mem in memories:
        # Convert spins to bits
        bitstr = ''.join(['0' if k == -1 else '1' for k in mem])
        # Get the index of the current (converted) memory and add it to list
        stateoverlap.append([label_list.index(bitstr), bitstr])

    # Output parameters
    binary = 1  # Save output files as binary Numpy format
    progressout = 1  # Output simulation progress over anneal timesteps

    eigspecdat = 1  # Output data for eigspec
    eigspecplot = 0  # Plot eigspec
    eigspecnum = 2**nQubits  # Number of eigenvalues to output
    fidelplot = 0  # Plot fidelity
    fideldat = 0  # Output fidelity data
    fidelnumstates = 2**nQubits  # Check fidelity with this number of eigenstates
    overlapdat = 0  # Output overlap data
    overlapplot = 0  # Plot overlap
    solveMethod = 'ExpPert'  # 'ExpPert', 'SuzTrot', 'ForRuth', 'BCM'

    # Output directory stuff
    probdir = 'data/hopfield_ortho/m' + str(imems) + hparams['learningRule']
    if isinstance(T, collections.Iterable):
        probdir += 'MultiT'
    if os.path.isdir(probdir):
        outlist = sorted(
            [int(name) for name in os.listdir(probdir) if name.isdigit()])
    else:
        outlist = []
    outnum = outlist[-1] + 1 if outlist else 0
    outputdir = probdir + '/' + str(outnum) + '/'

    probshow = 0  # Print final state probabilities to screen
    probout = 1  # Output probabilities to file
    mingap = 1  # Record the minimum spectral gap

    errchk = 0  # Error-checking on/off (for simulation accuracy)
    eps = 0.01  # Numerical error in normalization condition (1 - norm < eps)

    # Specify a QUBO (convert to Ising = True), or alpha, beta directly
    # (convert = False), and also specify the signs on the Ising Hamiltonian
    # terms (you can specify coefficients too for some problems if needed)
    isingConvert = 0
    isingSigns = {'hx': -1, 'hz': -1, 'hzz': -1}

    # Construct network Ising parameters
    neurons = nQubits

    # This is gamma, the appropriate weighting on the input vector
    isingSigns['hz'] *= 1 - (len(hparams['inputState']) -
                             hparams['inputState'].count(0)) / (2 * neurons)

    alpha = sp.array(hparams['inputState'])
    beta = sp.zeros((neurons, neurons))
    delta = sp.array([])

    # Construct the memory matrix according to a learning rule
    if hparams['learningRule'] == 'hebb':
        # Construct pattern matrix according to Hebb's rule
        for i in range(neurons):
            for j in range(neurons):
                for p in range(len(memories)):
                    beta[i, j] += (memories[p][i] * memories[p][j] -
                                   len(memories) * (i == j))
        beta = sp.triu(beta) / float(neurons)
    elif hparams['learningRule'] == 'stork':
        # Construct the memory matrix according to the Storkey learning rule
        memMat = sp.zeros((neurons, neurons))
        for m, mem in enumerate(memories):
            for i in range(neurons):
                for j in range(neurons):
                    hij = sp.sum(
                        [memMat[i, k] * mem[k] for k in range(neurons)])
                    hji = sp.sum(
                        [memMat[j, k] * mem[k] for k in range(neurons)])
                    # Don't forget to make the normalization a float!
                    memMat[i,
                           j] += 1. / neurons * (mem[i] * mem[j] -
                                                 mem[i] * hji - hij * mem[j])
        beta = sp.triu(memMat)
    elif hparams['learningRule'] == 'proj':
        # Construct memory matrix according to the Moore-Penrose pseudoinverse rule
        memMat = sp.matrix(memories).T
        beta = sp.triu(memMat * sp.linalg.pinv(memMat))

    # Usually we specify outputs that may be of interest in the form of a dict,
    # but we don't need any for this problem
    outputs = None

    ############################################################################
    ######## All variables must be specified here, do NOT change the keys ######
    ############################################################################

    return {
        'nQubits': nQubits,
        'Q': None,
        'T': T,
        'dt': dt,
        'outputdir': outputdir,
        'errchk': errchk,
        'eps': eps,
        'isingConvert': isingConvert,
        'isingSigns': isingSigns,
        'outputs': outputs,
        'alpha': alpha,
        'beta': beta,
        'delta': delta,
        'eigdat': eigspecdat,
        'eigplot': eigspecplot,
        'eignum': eigspecnum,
        'fiddat': fideldat,
        'fidplot': fidelplot,
        'fidnumstates': fidelnumstates,
        'overlapdat': overlapdat,
        'overlapplot': overlapplot,
        'outdir': outputdir,
        'binary': binary,
        'progressout': progressout,
        'probshow': probshow,
        'probout': probout,
        'mingap': mingap,
        'stateoverlap': stateoverlap,
        'hzscale': None,
        'hzzscale': None,
        'hxscale': None,
        'solveMethod': solveMethod
    }
Esempio n. 36
0
def parameters(cmdargs):
    """
    """
    nQubits = 8
    T = 10.0
    #T = sp.arange(2,23,4.0) # Output a sequence of anneal times
    dt = 0.01*T

    # Output parameters
    binary = 0 # Save output files as binary Numpy format
    progressout = 0 # Output simulation progress over anneal timesteps

    outputdir = 'data/hopfield_hamming_inv/' # In relation to run.py
    eigspecdat = 1 # Output data for eigspec
    eigspecplot = 0 # Plot eigspec
    eigspecnum = 16 # Number of eigenvalues to output
    fidelplot = 0 # Plot fidelity
    fideldat = 0 # Output fidelity data
    fidelnumstates = 2**nQubits # Check fidelity with this number of eigenstates
    overlapdat = 0 # Output overlap data
    overlapplot = 0 # Plot overlap
    solveMethod = 'ExpPert' # 'ExpPert', 'SuzTrot', 'ForRuth', 'BCM'
    # solveMethod = 'SuzTrot' # 'ExpPert', 'SuzTrot', 'ForRuth', 'BCM'

    probshow = 1 # Print final state probabilities to screen
    probout = 1 # Output probabilities to file
    mingap = 0 # Record the minimum spectral gap

    errchk = 0 # Error-checking on/off (for simulation accuracy)
    eps = 0.01 # Numerical error in normalization condition (1 - norm < eps)

    # Specify a QUBO (convert to Ising = True), or alpha, beta directly 
    # (convert = False), and also specify the signs on the Ising Hamiltonian 
    # terms (you can specify coefficients too for some problems if needed)
    isingConvert = 0
    isingSigns = {'hx': -1, 'hz': -1, 'hzz': -1}

    def spins2bitstr(vec):
        """ Return a converted bitstring from @vec, a list of spins. """
        return ''.join([ '0' if k == 1 else '1' for k in vec ])

    # Construct network Ising parameters
    neurons = nQubits

    # if nQubits % 2 == 0:
    #     memories = sp.linalg.hadamard(neurons).tolist()
    # inputstate = memories[0]

    # memories = [[ 1,-1,-1, 1, 1,-1,-1, 1],
    #             [-1,-1,-1, 1, 1,-1,-1, 1],
    #             [ 1,-1,-1, 1, 1,-1,-1,-1]]
    memories = [[ 1,-1,-1, 1,-1,-1,-1, 1],
                [ 1,-1,-1, 1,-1,-1,-1, 1],
                [ 1,-1,-1, 1,-1,-1,-1,-1]]
    inputstate = memories[0]

    print("Input:")
    print(spins2bitstr(inputstate))
    print('')
    print("Memories:")
    for mem in memories:
        print(spins2bitstr(mem))
    print('')
          
    # This is gamma, the appropriate weighting on the input vector
    # isingSigns['hz'] *= 1 - (len(inputstate) - inputstate.count(0))/(2*neurons)
    # isingSigns['hz'] *= 0.6772598397
    isingSigns['hz'] *= 1.0
    print("Gamma: ", isingSigns['hz'])
    print('')

    alpha = sp.array(inputstate)
    beta = sp.zeros((neurons,neurons))
    delta = sp.array([])

    # Hebb rule - even better matrix style
    memMat = sp.matrix(memories).T
    beta = (sp.triu(memMat*memMat.T)-len(memories)*sp.eye(nQubits))/float(neurons)
    print beta

    # # Hebb rule - matrix style
    # for m, mem in enumerate(memories):
    #     beta += (sp.outer(mem, mem))-sp.eye(neurons)
    # beta = sp.triu(beta)/float(neurons)

    # beta = sp.zeros((neurons,neurons))

    # # Construct pattern matrix according to the Hebb learning rule
    # for i in range(neurons):
    #     for j in range(neurons):
    #         for p in range(len(memories)):
    #             beta[i,j] += ( memories[p][i]*memories[p][j] -
    #                            len(memories)*(i == j) )
    # beta = sp.triu(beta)/float(neurons)

    # Storkey rule (incorrect, but may be useful later)
    # memMat = sp.zeros((neurons,neurons))
    # for m, mem in enumerate(memories):
    #     for i in range(neurons):
    #         for j in range(neurons):
    #             hij = sp.sum([ memMat[i,k]*mem[k] for k in range(neurons) ])
    #             hji = sp.sum([ memMat[j,k]*mem[k] for k in range(neurons) ])
    #             # Don't forget to make the normalization a float!
    #             memMat[i,j] += (mem[i]*mem[j] - mem[i]*hji - hij*mem[j])/float(neurons)
    # beta = sp.triu(memMat)

    # # Storkey rule
    # Wm = sp.zeros((neurons,neurons))
    # for m, mem in enumerate(memories):
    #     Am = sp.mat((sp.outer(mem,mem) - sp.eye(neurons)))
    #     Wm += (Am - Am*Wm - Wm*Am)/float(neurons)
    # beta = sp.triu(Wm)

    # # Construct memory matrix according to the Moore-Penrose pseudoinverse rule
    # memMat = sp.matrix(memories).T
    # beta = sp.triu(memMat * sp.linalg.pinv(memMat))

    # Usually we specify outputs that may be of interest in the form of a dict, 
    # but we don't need any for this problem
    outputs = None

    ############################################################################
    ######## All variables must be specified here, do NOT change the keys ######
    ############################################################################

    return {
        'nQubits': nQubits,
        'Q': None,
        'T': T,
        'dt': dt,
        'outputdir': outputdir,
        'errchk': errchk,
        'eps': eps,
        'isingConvert': isingConvert,
        'isingSigns': isingSigns,
        'outputs': outputs,
        'alpha': alpha,
        'beta': beta,
        'delta': delta,
        'eigdat': eigspecdat,
        'eigplot': eigspecplot,
        'eignum': eigspecnum,
        'fiddat': fideldat,
        'fidplot': fidelplot,
        'fidnumstates': fidelnumstates,
        'overlapdat': overlapdat,
        'overlapplot': overlapplot,
        'outdir': outputdir,
        'binary': binary,
        'progressout': progressout,
        'probshow': probshow,
        'probout': probout,
        'mingap': mingap,
        'stateoverlap': None,
        'hzscale': None,
        'hzzscale': None,
        'hxscale': None,
        'solveMethod': solveMethod
        }
Esempio n. 37
0
def parameters(cmdargs):
    """
    """

    # The Hopfield parameters
    properties = json.load(
        open('data/hopfield_gamma_tune/gamma_problem_inputs.json'))
    hparams = {
        'numNeurons': properties['nQubits'],
        'inputState': properties['inputState'],
        'learningRule': properties['learningRule'],
        'numMemories': len(properties['memories'])
    }

    # Construct memories
    memories = properties['memories']

    # Basic simulation params
    nQubits = hparams['numNeurons']
    T = properties['annealTime']
    dt = 0.01 * T

    # Output parameters
    binary = 1  # Save output files as binary Numpy format
    progressout = 0  # Output simulation progress over anneal timesteps

    eigspecdat = 0  # Output data for eigspec
    eigspecplot = 0  # Plot eigspec
    eigspecnum = 2**nQubits  # Number of eigenvalues to output
    fidelplot = 0  # Plot fidelity
    fideldat = 0  # Output fidelity data
    fidelnumstates = 2**nQubits  # Check fidelity with this number of eigenstates
    overlapdat = 0  # Output overlap data
    overlapplot = 0  # Plot overlap

    # Output directory stuff
    # probdir = 'data/hopfield_gamma_tune/n'+str(nQubits)+'p'+\
    #     str(hparams['numMemories'])+hparams['learningRule']
    # if isinstance(T, collections.Iterable):
    #     probdir += 'MultiT'
    # if os.path.isdir(probdir):
    #     outlist = sorted([ int(name) for name in os.listdir(probdir)
    #                        if name.isdigit() ])
    # else:
    #     outlist = []
    # outnum = outlist[-1] + 1 if outlist else 0
    # outputdir = probdir + '/' + str(outnum) + '/'
    outputdir = 'data/hopfield_gamma_tune'

    probshow = 0  # Print final state probabilities to screen
    probout = 1  # Output probabilities to file
    mingap = 0  # Record the minimum spectral gap

    errchk = 0  # Error-checking on/off (for simulation accuracy)
    eps = 0.01  # Numerical error in normalization condition (1 - norm < eps)

    # Specify a QUBO (convert to Ising = True), or alpha, beta directly
    # (convert = False), and also specify the signs on the Ising Hamiltonian
    # terms (you can specify coefficients too for some problems if needed)
    isingConvert = 0
    isingSigns = {'hx': -1, 'hz': -1, 'hzz': -1}

    # Construct network Ising parameters
    neurons = nQubits

    # This is gamma, the appropriate weighting on the input vector
    isingSigns['hz'] *= float(cmdargs['farg'])

    alpha = sp.array(hparams['inputState'])
    beta = sp.zeros((neurons, neurons))
    delta = sp.array([])

    # Construct the memory matrix according to a learning rule
    if hparams['learningRule'] == 'hebb':
        # Construct pattern matrix according to Hebb's rule
        for i in range(neurons):
            for j in range(neurons):
                for p in range(len(memories)):
                    beta[i, j] += (memories[p][i] * memories[p][j] -
                                   len(memories) * (i == j))
        beta = sp.triu(beta) / float(neurons)
    elif hparams['learningRule'] == 'stork':
        # Construct the memory matrix according to the Storkey learning rule
        memMat = sp.zeros((neurons, neurons))
        for m, mem in enumerate(memories):
            for i in range(neurons):
                for j in range(neurons):
                    hij = sp.sum(
                        [memMat[i, k] * mem[k] for k in range(neurons)])
                    hji = sp.sum(
                        [memMat[j, k] * mem[k] for k in range(neurons)])
                    # Don't forget to make the normalization a float!
                    memMat[i,
                           j] += 1. / neurons * (mem[i] * mem[j] -
                                                 mem[i] * hji - hij * mem[j])
        beta = sp.triu(memMat)
    elif hparams['learningRule'] == 'proj':
        # Construct memory matrix according to the Moore-Penrose pseudoinverse rule
        memMat = sp.matrix(memories).T
        beta = sp.triu(memMat * sp.linalg.pinv(memMat))

    # Some outputs
    outputs = {
        'nQubits': nQubits,
        'learningRule': hparams['learningRule'],
        'inputState': hparams['inputState'],
        'memories': memories,
        'answer': memories[0],
        'annealTime': list(T) if isinstance(T, collections.Iterable) else T
    }

    ############################################################################
    ######## All variables must be specified here, do NOT change the keys ######
    ############################################################################

    return {
        'nQubits': nQubits,
        'Q': None,
        'T': T,
        'dt': dt,
        'outputdir': outputdir,
        'errchk': errchk,
        'eps': eps,
        'isingConvert': isingConvert,
        'isingSigns': isingSigns,
        'outputs': outputs,
        'alpha': alpha,
        'beta': beta,
        'delta': delta,
        'eigdat': eigspecdat,
        'eigplot': eigspecplot,
        'eignum': eigspecnum,
        'fiddat': fideldat,
        'fidplot': fidelplot,
        'fidnumstates': fidelnumstates,
        'overlapdat': overlapdat,
        'overlapplot': overlapplot,
        'outdir': outputdir,
        'binary': binary,
        'progressout': progressout,
        'probshow': probshow,
        'probout': probout,
        'mingap': mingap,
        'stateoverlap': None,
        'hzscale': None,
        'hzzscale': None,
        'hxscale': None
    }
Esempio n. 38
0
def parameters(cmdargs):
    """
    """
    nQubits = 6
    T = 30.0
    #T = sp.arange(2,103,10) # Output a sequence of anneal times
    dt = 0.1

    # Output parameters
    binary = 1 # Save output files as binary Numpy format
    progressout = 0 # Output simulation progress over anneal timesteps

    outputdir = 'data/sixvarqubo_overlap/' # In relation to run.py
    eigspecdat = 0 # Output data for eigspec
    eigspecplot = 0 # Plot eigspec
    eigspecnum = 2**nQubits # Number of eigenvalues to output
    fidelplot = 0 # Plot fidelity
    fideldat = 0 # Output fidelity data
    fidelnumstates = 2**nQubits # Check fidelity with this number of eigenstates
    overlapdat = 0 # Output overlap data
    overlapplot = 0 # Plot overlap
    solveMethod = 'ExpPert' # 'ExpPert', 'SuzTrot', 'ForRuth', 'BCM'

    probshow = 1 # Print final state probabilities to screen
#### BUG ##### probshow requires probout
    probout = 1 # Output probabilities to file

    mingap = 0 # Record the minimum spectral gap

    errchk = 0 # Error-checking on/off (for simulation accuracy)
    eps = 0.01 # Numerical error in normalization condition (1 - norm < eps)

    # Specify a QUBO (convert to Ising = True), or alpha, beta directly 
    # (convert = False), and also specify the signs on the Ising Hamiltonian 
    # terms (you can specify coefficients too for some problems if needed)
    isingConvert = 1
    isingSigns = {'hx': 1, 'hz': 1, 'hzz': -1}

    # Define states for which to track probabilities in time
    import statelabels
    label_list = statelabels.GenerateLabels(nQubits)
    stateoverlap = []
    for bitstr in ['100101', '110101']:
        # Get the index of the current (converted) memory and add it to list
        stateoverlap.append([ label_list.index(bitstr), bitstr ])

    # Define the QUBO and its diagonal
    Q = sp.zeros((nQubits,nQubits))
    a = sp.zeros(nQubits)

    a[0] = 2
    a[1] = 1
    a[2] = -2
    a[3] = -1
    a[4] = 1
    a[5] = -1

    Q[0,1] = Q[1,0] = -1
    Q[0,2] = Q[2,0] = 2
    Q[0,3] = Q[3,0] = -2
    Q[0,4] = Q[4,0] = 2
    Q[0,5] = Q[5,0] = -1

    Q[1,2] = Q[2,1] = 1
    Q[1,3] = Q[3,1] = -1
    Q[1,4] = Q[4,1] = -1
    Q[1,5] = Q[5,1] = 1

    Q[2,3] = Q[3,2] = 2
    Q[2,4] = Q[4,2] = -2
    Q[2,5] = Q[5,2] = 1

    Q[3,4] = Q[4,3] = 2
    Q[3,5] = Q[5,3] = -1

    Q[4,5] = Q[5,4] = 2

    Q = sp.triu(Q) + a*sp.identity(6)

    # Usually we specify outputs that may be of interest in the form of a dict, 
    # but we don't need any for this problem
    outputs = None

    # We could specify these below, but we want to avoid modifying that part
    alpha = beta = delta = None

    ############################################################################
    ######## All variables must be specified here, do NOT change the keys ######
    ############################################################################

    return {
        'nQubits': nQubits,
        'Q': Q,
        'T': T,
        'dt': dt,
        'outputdir': outputdir,
        'errchk': errchk,
        'eps': eps,
        'isingConvert': isingConvert,
        'isingSigns': isingSigns,
        'outputs': outputs,
        'alpha': alpha,
        'beta': beta,
        'delta': delta,
        'eigdat': eigspecdat,
        'eigplot': eigspecplot,
        'eignum': eigspecnum,
        'fiddat': fideldat,
        'fidplot': fidelplot,
        'fidnumstates': fidelnumstates,
        'overlapdat': overlapdat,
        'overlapplot': overlapplot,
        'outdir': outputdir,
        'binary': binary,
        'progressout': progressout,
        'probshow': probshow,
        'probout': probout,
        'mingap': mingap,
        'stateoverlap': stateoverlap,
        'solveMethod': solveMethod
        }
Esempio n. 39
0
def CATS2D(mol, PathLength=10, scale=3):
    """
    #################################################################
    The main program for calculating the CATS descriptors.

    CATS: chemically advanced template serach

    ----> CATS_DA0 ....

    Usage:

        result=CATS2D(mol,PathLength = 10,scale = 1)

        Input: mol is a molecule object.

               PathLength is the max topological distance between two atoms.

               scale is the normalization method (descriptor scaling method)

               scale = 1 indicates that no normalization. That is to say: the

               values of the vector represent raw counts ("counts").

               scale = 2 indicates that division by the number of non-hydrogen

               atoms (heavy atoms) in the molecule.

               scale = 3 indicates that division of each of 15 possible PPP pairs

               by the added occurrences of the two respective PPPs.

        Output: result is a dict format with the definitions of each descritor.
    #################################################################
    """
    Hmol = Chem.RemoveHs(mol)
    AtomNum = Hmol.GetNumAtoms()
    atomtype = AssignAtomType(Hmol)
    DistanceMatrix = Chem.GetDistanceMatrix(Hmol)
    DM = scipy.triu(DistanceMatrix)
    tempdict = {}
    for PL in range(0, PathLength):
        if PL == 0:
            Index = [[k, k] for k in range(AtomNum)]
        else:
            Index1 = scipy.argwhere(DM == PL)
            Index = [[k[0], k[1]] for k in Index1]
        temp = []
        for j in Index:
            temp.extend(MatchAtomType(j, atomtype))
        tempdict.update({PL: temp})

    CATSLabel = FormCATSLabel(PathLength)
    CATS1 = FormCATSDict(tempdict, CATSLabel)

    ####set normalization 3
    AtomPair = [
        "DD",
        "DA",
        "DP",
        "DN",
        "DL",
        "AA",
        "AP",
        "AN",
        "AL",
        "PP",
        "PN",
        "PL",
        "NN",
        "NL",
        "LL",
    ]
    temp = []
    for i, j in tempdict.items():
        temp.extend(j)

    AtomPairNum = {}
    for i in AtomPair:
        AtomPairNum.update({i: temp.count(i)})
    ############################################
    CATS = {}
    if scale == 1:
        CATS = CATS1
    if scale == 2:
        for i in CATS1:
            CATS.update({i: round(CATS1[i] / (AtomNum + 0.0), 3)})
    if scale == 3:
        for i in CATS1:
            if AtomPairNum[i[5:7]] == 0:
                CATS.update({i: round(CATS1[i], 3)})
            else:
                CATS.update(
                    {i: round(CATS1[i] / (AtomPairNum[i[5:7]] + 0.0), 3)})

    return CATS
Esempio n. 40
0
def parameters(cmdargs):
    """
    """

    # import problems.hopfield.params as params

    # learningRule = params.learningRules[params.rule]
    learningRule = cmdargs['simtype']
    # nQubits = params.numQubits
    nQubits = 5
    # T = params.annealTime
    T = sp.arange(0.1, 15, 0.5)
    T = 10.0
    dt = 0.01*T
    inputstate = [1,1,-1,-1,1]

    # Output parameters
    output = 1 # Turn on/off all output except final probabilities
    binary = 1 # Save as binary Numpy
    progressout = 0 # Output simulation progress over anneal timesteps

    eigspecdat = 1 # Output data for eigspec
    eigspecplot = 0 # Plot eigspec
    eigspecnum = 2**nQubits # Number of eigenvalues
    fidelplot = 0 # Plot fidelity
    fideldat = 0 # Output fidelity data
    fidelnumstates = 2**nQubits # Check fidelity with this number of eigenstates
    overlapdat = 0 # Output overlap data
    overlapplot = 0 # Plot overlap

    # Output directory stuff
    pnum = 0
    if 32 > cmdargs['instance']:
        pnum = 1
    elif (496-1) < cmdargs['instance']:
        pnum = 3
    else:
        pnum = 2
    probdir = 'data/hopfield_all_n'+str(nQubits)+'p'+str(pnum)+learningRule
#     probdir = 'problems/all_n'+str(nQubits)+'p'+str(pnum)+learningRule
    if isinstance(T, collections.Iterable):
        probdir += 'MultiT'
    if os.path.isdir(probdir):
        outlist = sorted([ int(name) for name in os.listdir(probdir) 
                           if name.isdigit() ])
    else:
        outlist = []
    outnum = outlist[-1] + 1 if outlist else 0
    outputdir = probdir + '/' + str(outnum) + '/'

    probshow = 0 # Print final state probabilities to screen
    probout = 1 # Output probabilities to file
    mingap = 1 # Record the minimum spectral gap

    errchk = 0 # Error-checking on/off (for simulation accuracy)
    eps = 0.01 # Numerical error in normalization condition (1 - norm < eps)

    # Specify a QUBO (convert to Ising = True), or alpha, beta directly 
    # (convert = False), and also specify the signs on the Ising Hamiltonian 
    # terms (you can specify coefficients too for some problems if needed)
    isingConvert = 0
    isingSigns = {'hx': -1, 'hz': -1, 'hzz': -1}

    neurons = nQubits
    memories = []

    # Load the list of memories
    patternSet = pickle.load(open('problems/n5p'+str(pnum)+'mems.dat', 'rb'))
    # Define the right index for each pnum case
    psetIdx = cmdargs['instance']
    if 31 < cmdargs['instance'] < 496:
        psetIdx -= 32
    elif cmdargs['instance'] >= 496:
        psetIdx -= 496
    # Iterate through the set that corresponds to the [corrected] instance number
    for bitstring in patternSet[psetIdx]:
        # Convert binary to Ising spins
        spins = [ 1 if k == '1' else -1 for k in bitstring ]
        memories.append(spins)

    # Make the input the last memory recorded
    # inputstate = params.inputState
    # Add in the input state
    # if params.includeInput and inputstate not in memories:
    #     memories[0] = inputstate
    if inputstate not in memories:
        memories[0] = inputstate

    # This is gamma, the appropriate weighting on the input vector
    isingSigns['hz'] *= 1 - (len(inputstate) - inputstate.count(0))/(2*neurons)

    # Initialize Hamiltonian parameters
    alpha = sp.array(inputstate)
    beta = sp.zeros((neurons,neurons))
    delta = sp.array([])

    # Construct the memory matrix according to a learning rule
    if learningRule == 'hebb':
        # Construct pattern matrix according to Hebb's rule
        for i in range(neurons):
            for j in range(neurons):
                for p in range(len(memories)):
                    beta[i,j] += ( memories[p][i]*memories[p][j] -
                                   len(memories)*(i == j) )
        beta = sp.triu(beta)/float(neurons)
    elif learningRule == 'stork':
        # Construct the memory matrix according to the Storkey learning rule
        memMat = sp.zeros((neurons,neurons))
        for m, mem in enumerate(memories):
            for i in range(neurons):
                for j in range(neurons):
                    hij = sp.sum([ memMat[i,k]*mem[k] for k in range(neurons) ])
                    hji = sp.sum([ memMat[j,k]*mem[k] for k in range(neurons) ])
                    # Don't forget to make the normalization a float!
                    memMat[i,j] += 1./neurons*(mem[i]*mem[j] - mem[i]*hji - 
                                               hij*mem[j])
        beta = sp.triu(memMat)
    elif learningRule == 'proj':
        # Construct memory matrix according to the Moore-Penrose pseudoinverse rule
        memMat = sp.matrix(memories).T
        beta = sp.triu(memMat * sp.linalg.pinv(memMat))

    # Calculate Hamming distance between input state and each memory
    hammingDistance = []
    for mem in memories:
        dist = sp.sum(abs(sp.array(inputstate)-sp.array(mem))/2)
        hammingDistance.append(dist)

    hamMean = sp.average(hammingDistance)
    hamMed = sp.median(hammingDistance)

    # Some outputs
    outputs = {
        'nQubits': nQubits,
        'learningRule': learningRule,
        'outdir': probdir,
        'inputState': inputstate,
        'memories': memories,
        'hammingDistance': {'dist': hammingDistance,
                            'mean': hamMean,
                            'median': hamMed },
        'annealTime': list(T) if isinstance(T, collections.Iterable) else T
               }

    ############################################################################
    ######## All variables must be specified here, do NOT change the keys ######
    ############################################################################

    return {
        'nQubits': nQubits,
        'Q': None,
        'T': T,
        'dt': dt,
        'outputdir': outputdir,
        'errchk': errchk,
        'eps': eps,
        'isingConvert': isingConvert,
        'isingSigns': isingSigns,
        'outputs': outputs,
        'alpha': alpha,
        'beta': beta,
        'delta': delta,
        'eigdat': eigspecdat,
        'eigplot': eigspecplot,
        'eignum': eigspecnum,
        'fiddat': fideldat,
        'fidplot': fidelplot,
        'fidnumstates': fidelnumstates,
        'overlapdat': overlapdat,
        'overlapplot': overlapplot,
        'outdir': outputdir,
        'binary': binary,
        'progressout': progressout,
        'probshow': probshow,
        'probout': probout,
        'mingap': mingap,
        'stateoverlap': None
        }
Esempio n. 41
0
def CATS2D(mol,PathLength = 10,scale = 3):
    """
    #################################################################
    The main program for calculating the CATS descriptors.
    
    CATS: chemically advanced template serach
    
    ----> CATS_DA0 ....
    
    Usage:
        
        result=CATS2D(mol,PathLength = 10,scale = 1)
        
        Input: mol is a molecule object.
        
               PathLength is the max topological distance between two atoms.
               
               scale is the normalization method (descriptor scaling method)
               
               scale = 1 indicates that no normalization. That is to say: the 
               
               values of the vector represent raw counts ("counts").
               
               scale = 2 indicates that division by the number of non-hydrogen
               
               atoms (heavy atoms) in the molecule.
               
               scale = 3 indicates that division of each of 15 possible PPP pairs
               
               by the added occurrences of the two respective PPPs.
        
        Output: result is a dict format with the definitions of each descritor.
    #################################################################
    """
    Hmol = Chem.RemoveHs(mol)
    AtomNum = Hmol.GetNumAtoms()    
    atomtype = AssignAtomType(Hmol)
    DistanceMatrix = Chem.GetDistanceMatrix(Hmol)
    DM = scipy.triu(DistanceMatrix)
    tempdict = {}
    for PL in range(0,PathLength):
        if PL == 0:            
            Index = [[k,k] for k in range(AtomNum)]
        else:
            Index1 = scipy.argwhere(DM == PL)
            Index = [[k[0],k[1]] for k in Index1]
        temp = []
        for j in Index:
            temp.extend(MatchAtomType(j,atomtype))
        tempdict.update({PL:temp})
    
    CATSLabel = FormCATSLabel(PathLength)
    CATS1 = FormCATSDict(tempdict,CATSLabel)
    
    ####set normalization 3
    AtomPair = ['DD','DA','DP','DN','DL','AA','AP',
                'AN','AL','PP','PN','PL','NN','NL','LL']
    temp = []
    for i,j in tempdict.items():
        temp.extend(j)
        
    AtomPairNum = {}
    for i in AtomPair:
        AtomPairNum.update({i:temp.count(i)})
    ############################################
    CATS = {}
    if scale == 1:
        CATS = CATS1
    if scale == 2:
        for i in CATS1:
            CATS.update({i:round(CATS1[i]/(AtomNum+0.0),3)})            
    if scale == 3:
        for i in CATS1:
            if AtomPairNum[i[5:7]] == 0:
                CATS.update({i:round(CATS1[i],3)})
            else:               
                CATS.update({i:round(CATS1[i]/(AtomPairNum[i[5:7]]+0.0),3)})  
    
    return CATS
Esempio n. 42
0
    def from_gene(self, gene):
        
        for transcript_idx in range(len(gene.transcripts)):
            exon_start_end = gene.exons[transcript_idx]
            
            ### only one exon in the transcript
            if exon_start_end.shape[0] == 1:
                exon1_start = exon_start_end[0, 0]
                exon1_end = exon_start_end[0, 1]

                if self.vertices.shape[1] == 0:
                    self.vertices = sp.array([[exon1_start], [exon1_end]], dtype='int')
                    self.edges = sp.array([[0]], dtype='int')
                else:
                    self.vertices = sp.c_[self.vertices, [exon1_start, exon1_end]]
                    self.new_edge()
            ### more than one exon in the transcript
            else:
                for exon_idx in range(exon_start_end.shape[0] - 1):
                    exon1_start = exon_start_end[exon_idx , 0]
                    exon1_end = exon_start_end[exon_idx, 1]
                    exon2_start = exon_start_end[exon_idx + 1, 0]
                    exon2_end = exon_start_end[exon_idx + 1, 1]
          
                    if self.vertices.shape[1] == 0:
                        self.vertices = sp.array([[exon1_start, exon2_start], [exon1_end, exon2_end]], dtype='int')
                        self.edges = sp.array([[0, 1], [1, 0]], dtype='int')
                    else:
                        exon1_idx = -1
                        exon2_idx = -1
                        ### check if current exon already occurred
                        for idx in range(self.vertices.shape[1]):
                            if ((self.vertices[0, idx] == exon1_start) and (self.vertices[1, idx] == exon1_end)):
                                 exon1_idx = idx
                            if ((self.vertices[0, idx] == exon2_start) and (self.vertices[1, idx] == exon2_end)):
                                 exon2_idx = idx

                        ### both exons already occured -> only add an edge
                        if (exon1_idx != -1) and (exon2_idx != -1):
                            self.edges[exon1_idx, exon2_idx] = 1
                            self.edges[exon2_idx, exon1_idx] = 1
                        else:
                            ### 2nd exon occured
                            if ((exon1_idx == -1) and (exon2_idx != -1)):
                                self.vertices = sp.c_[self.vertices, [exon1_start, exon1_end]]
                                self.new_edge()
                                self.edges[exon2_idx, -1] = 1
                                self.edges[-1, exon2_idx] = 1
                            ### 1st exon occured
                            elif ((exon2_idx == -1) and (exon1_idx != -1)):
                                self.vertices = sp.c_[self.vertices, [exon2_start, exon2_end]]
                                self.new_edge()
                                self.edges[exon1_idx, -1] = 1
                                self.edges[-1, exon1_idx] = 1
                            ### no exon occured
                            else:
                                assert((exon1_idx == -1) and (exon2_idx == -1))
                                self.vertices = sp.c_[self.vertices, [exon1_start, exon1_end]]
                                self.vertices = sp.c_[self.vertices, [exon2_start, exon2_end]]
                                self.new_edge()
                                self.new_edge()
                                self.edges[-2, -1] = 1
                                self.edges[-1, -2] = 1

        ### take care of the sorting by exon start
        s_idx = sp.argsort(self.vertices[0, :])
        self.vertices = self.vertices[:, s_idx]
        self.edges = self.edges[s_idx, :][:, s_idx]
        self.terminals = sp.zeros(self.vertices.shape, dtype='int')
        self.terminals[0, sp.where(sp.tril(self.edges).sum(axis=1) == 0)[0]] = 1
        self.terminals[1, sp.where(sp.triu(self.edges).sum(axis=1) == 0)[0]] = 1
Esempio n. 43
0
def parameters(cmdargs):
    """
    """

    # The Hopfield parameters
    hparams = {
        'numNeurons': cmdargs['qubits'],
        'inputState': [ 2*sp.random.random_integers(0,1)-1 
                        for k in xrange(cmdargs['qubits']) ],
        'learningRule': cmdargs['simtype'],
        'numMemories': int(cmdargs['farg'])
        }

    def hamdist(a,b):
        """ Calculate Hamming distance. """
        return sp.sum(abs(sp.array(a)-sp.array(b))/2.0)

    # Construct memories
    memories = [ [ 2*sp.random.random_integers(0,1)-1 
                   for k in xrange(hparams['numNeurons']) ]
                 for j in xrange(hparams['numMemories']) ]
    # Choose an input state to start
    memories[0] = list(hparams['inputState'])
    # # Pick two random spins to flip
    # rndbit1 = sp.random.random_integers(0,hparams['numNeurons']-1)
    # rndbit2 = sp.random.random_integers(0,hparams['numNeurons']-1)
    # while rndbit1 == rndbit2:
    #     rndbit2 = sp.random.random_integers(0,hparams['numNeurons']-1)
    # # Flip them
    # memories[0][rndbit1] *= -1
    # memories[0][rndbit2] *= -1
    # # Make sure that the input is NOT in the memory set
    # while list(hparams['inputState']) in memories:
    #     # Do it all again to make sure we don't get stuck
    #     memories = [ [ 2*sp.random.random_integers(0,1)-1 
    #                    for k in xrange(hparams['numNeurons']) ]
    #                  for j in xrange(hparams['numMemories']) ]
    #     memories[0] = list(hparams['inputState'])
    #     # Pick two random spins to flip
    #     rndbit1 = sp.random.random_integers(0,hparams['numNeurons']-1)
    #     rndbit2 = sp.random.random_integers(0,hparams['numNeurons']-1)
    #     while rndbit1 == rndbit2:
    #         rndbit2 = sp.random.random_integers(0,hparams['numNeurons']-1)
    #     # Flip them
    #     memories[0][rndbit1] *= -1
    #     memories[0][rndbit2] *= -1
    # # Loop over additional memories, if there are any
    # for imem, mem in enumerate(memories[1:]):
    #     while hamdist(mem, hparams['inputState']) < 2.0:
    #         # Flip a random spin
    #         rndbit = sp.random.random_integers(0,hparams['numNeurons']-1)
    #         memories[imem+1][rndbit] *= -1

    memories[0][:3] *= -1
    # Loop over memories, make sure none are closer than 3.0 to input
    for imem, mem in enumerate(memories[1:]):
        while hamdist(mem, hparams['inputState']) < 2.0:
            # Flip a random spin
            rndbit = sp.random.random_integers(0,hparams['numNeurons']-1)
            memories[imem+1][rndbit] *= -1
    

    # Basic simulation params
    nQubits = hparams['numNeurons']
    T = 1000.0
    dt = 0.01*T

    # Output parameters
    binary = 1 # Save output files as binary Numpy format
    progressout = 0 # Output simulation progress over anneal timesteps

    eigspecdat = 1 # Output data for eigspec
    eigspecplot = 0 # Plot eigspec
    eigspecnum = 2**nQubits # Number of eigenvalues to output
    fidelplot = 0 # Plot fidelity
    fideldat = 0 # Output fidelity data
    fidelnumstates = 2**nQubits # Check fidelity with this number of eigenstates
    overlapdat = 0 # Output overlap data
    overlapplot = 0 # Plot overlap
    solveMethod = 'ExpPert' # 'ExpPert', 'SuzTrot', 'ForRuth', 'BCM'

    # Output directory stuff
    probdir = 'data/hopfield_var_gamma_exp4_hd2/n'+str(nQubits)+'p'+\
        str(hparams['numMemories'])+hparams['learningRule']
    if isinstance(T, collections.Iterable):
        probdir += 'MultiT'
    if os.path.isdir(probdir):
        outlist = sorted([ int(name) for name in os.listdir(probdir) 
                           if name.isdigit() ])
    else:
        outlist = []
    outnum = outlist[-1] + 1 if outlist else 0
    outputdir = probdir + '/' + str(outnum) + '/'

    probshow = 0 # Print final state probabilities to screen
    probout = 1 # Output probabilities to file
    mingap = 0 # Record the minimum spectral gap

    errchk = 0 # Error-checking on/off (for simulation accuracy)
    eps = 0.01 # Numerical error in normalization condition (1 - norm < eps)

    # Specify a QUBO (convert to Ising = True), or alpha, beta directly 
    # (convert = False), and also specify the signs on the Ising Hamiltonian 
    # terms (you can specify coefficients too for some problems if needed)
    isingConvert = 0
    isingSigns = {'hx': -1, 'hz': -1, 'hzz': -1}

    # Construct network Ising parameters
    neurons = nQubits

    # This is gamma, the appropriate weighting on the input vector
    isingSigns['hz'] *= float(cmdargs['garg'])

    alpha = sp.array(hparams['inputState'])
    beta = sp.zeros((neurons,neurons))
    delta = sp.array([])

    # Construct the memory matrix according to a learning rule
    if hparams['learningRule'] == 'hebb':
        # Construct pattern matrix according to Hebb's rule
        for i in range(neurons):
            for j in range(neurons):
                for p in range(len(memories)):
                    beta[i,j] += ( memories[p][i]*memories[p][j] -
                                   len(memories)*(i == j) )
        beta = sp.triu(beta)/float(neurons)
    elif hparams['learningRule'] == 'stork':
        # Construct the memory matrix according to the Storkey learning rule
        memMat = sp.zeros((neurons,neurons))
        for m, mem in enumerate(memories):
            for i in range(neurons):
                for j in range(neurons):
                    hij = sp.sum([ memMat[i,k]*mem[k] for k in range(neurons) ])
                    hji = sp.sum([ memMat[j,k]*mem[k] for k in range(neurons) ])
                    # Don't forget to make the normalization a float!
                    memMat[i,j] += 1./neurons*(mem[i]*mem[j] - mem[i]*hji - 
                                               hij*mem[j])
        beta = sp.triu(memMat)
    elif hparams['learningRule'] == 'proj':
        # Construct memory matrix according to the Moore-Penrose pseudoinverse rule
        memMat = sp.matrix(memories).T
        beta = sp.triu(memMat * sp.linalg.pinv(memMat))

    # Some outputs
    outputs = {
        'nQubits': nQubits,
        'learningRule': hparams['learningRule'],
        'inputState': hparams['inputState'],
        'memories': memories,
        'answer': memories[0],
        'gamma': float(cmdargs['garg']),
        'annealTime': list(T) if isinstance(T, collections.Iterable) else T
               }

    ############################################################################
    ######## All variables must be specified here, do NOT change the keys ######
    ############################################################################

    return {
        'nQubits': nQubits,
        'Q': None,
        'T': T,
        'dt': dt,
        'outputdir': outputdir,
        'errchk': errchk,
        'eps': eps,
        'isingConvert': isingConvert,
        'isingSigns': isingSigns,
        'outputs': outputs,
        'alpha': alpha,
        'beta': beta,
        'delta': delta,
        'eigdat': eigspecdat,
        'eigplot': eigspecplot,
        'eignum': eigspecnum,
        'fiddat': fideldat,
        'fidplot': fidelplot,
        'fidnumstates': fidelnumstates,
        'overlapdat': overlapdat,
        'overlapplot': overlapplot,
        'outdir': outputdir,
        'binary': binary,
        'progressout': progressout,
        'probshow': probshow,
        'probout': probout,
        'mingap': mingap,
        'stateoverlap': None,
        'hzscale': None,
        'hzzscale': None,
        'hxscale': None,
        'solveMethod': solveMethod
        }
Esempio n. 44
0
def detect_multipleskips(genes, gidx, log=False, edge_limit=300):
    # [idx_multiple_skips, exon_multiple_skips] = detect_multipleskips(genes, idx_alt) ;

    idx_multiple_skips = []
    exon_multiple_skips = []
    for iix, ix in enumerate(gidx):
        if log:
            sys.stdout.write('.')
            if (iix + 1) % 50 == 0:
                sys.stdout.write(' - %i/%i, found %i\n' % (iix + 1, genes.shape[0] + 1, len(idx_multiple_skips)))
            sys.stdout.flush()

        genes[iix].from_sparse()
        num_exons = genes[iix].splicegraph.get_len()
        edges = genes[iix].splicegraph.edges.copy()
        labels = repmat(sp.arange(num_exons), num_exons, 1).T
        genes[iix].to_sparse()
        
        # adjecency matrix: upper half only
        A = sp.zeros((num_exons, num_exons))
        for i in range(num_exons - 1):
            for j in range(i + 1, num_exons):
                A[i, j] = edges[i, j]
        
        # possible starting and ending exons of a multiple exon skip
        Pairs = lil_matrix((num_exons, num_exons))
        Ai = sp.dot(sp.dot(A, A), A) #paths of length 3
        while sp.any(Ai.ravel() > 0):
            coords = sp.where((A > 0 ) & (Ai > 0)) # multiple skip
            Pairs[coords[0], coords[1]] = 1
            Ai = sp.dot(Ai, A)  # paths of length ..+1
        
        edge = sp.where(Pairs.toarray() == 1)
        

        if edge[0].shape[0] > edge_limit:
            print '\nWARNING: not processing gene %i (%s); has %i edges; current limit is %i; adjust edge_limit to include.' % (ix, genes[iix].name, edge[0].shape[0], edge_limit)
            continue
        
        for cnt in range(edge[0].shape[0]):
            exon_idx_first = edge[0][cnt]
            exon_idx_last = edge[1][cnt]
      
            if edges[exon_idx_first, exon_idx_last] == 1:
                
                # find all pairs shortest path
                exist_path = sp.triu(edges).astype('double')
                exist_path[exon_idx_first, exon_idx_last] = 0
                exist_path[exist_path == 0] = sp.inf
                # set diagonal to 0
                exist_path[sp.arange(exist_path.shape[0]), sp.arange(exist_path.shape[0])] = 0
                
                long_exist_path = -sp.triu(edges).astype('double')
                long_exist_path[exon_idx_first, exon_idx_last] = 0
                long_exist_path[long_exist_path == 0] = sp.inf
                # set diagonal to 0
                long_exist_path[sp.arange(long_exist_path.shape[0]), sp.arange(long_exist_path.shape[0])] = 0
                
                path = sp.isfinite(exist_path) * labels
                long_path = sp.isfinite(long_exist_path) * labels
                
                for k in range(num_exons):
                    for i in range(num_exons):
                        idx = sp.where((exist_path[i, k] + exist_path[k, :]) < exist_path[i, :])[0]
                        exist_path[i, idx] = exist_path[i, k] + exist_path[k, idx]
                        path[i, idx] = path[k, idx]
                        
                        idx = sp.where((long_exist_path[i,k] + long_exist_path[k, :]) < long_exist_path[i, :])[0]
                        long_exist_path[i, idx] = long_exist_path[i, k] + long_exist_path[k, idx]
                        long_path[i, idx] = long_path[k, idx]
                
                temp_ix = sp.isfinite(long_exist_path)
                long_exist_path[temp_ix] = -long_exist_path[temp_ix]
                
                if (exist_path[exon_idx_first, exon_idx_last] > 2) and sp.isfinite(exist_path[exon_idx_first, exon_idx_last]):
                    backtrace = sp.array([path[exon_idx_first, exon_idx_last]])
                    while backtrace[-1] > exon_idx_first:
                        backtrace = sp.r_[backtrace, path[exon_idx_first, backtrace[-1]]]
                    backtrace = backtrace[:-1]
                    backtrace = backtrace[::-1]
                    idx_multiple_skips.append(ix) #repmat(ix, 1, backtrace.shape[0] + 2))
                    exon_multiple_skips.append([exon_idx_first, backtrace, exon_idx_last])
                elif (long_exist_path[exon_idx_first, exon_idx_last] > 2) and sp.isfinite(long_exist_path[exon_idx_first, exon_idx_last]):
                    backtrace = sp.array([long_path[exon_idx_first, exon_idx_last]])
                    while backtrace[-1] > exon_idx_first:
                        backtrace = sp.r_[backtrace, long_path[exon_idx_first, backtrace[-1]]]
                    backtrace = backtrace[:-1]
                    backtrace = backtrace[::-1]
                    idx_multiple_skips.append(ix) #repmat(ix, 1, backtrace.shape[0] + 2))
                    exon_multiple_skips.append([exon_idx_first, backtrace, exon_idx_last])

    if log:
        print 'Number of multiple exon skips:\t\t\t\t\t%d' % len(idx_multiple_skips)

    return (idx_multiple_skips, exon_multiple_skips)
Esempio n. 45
0
def parameters(cmdargs):
    """
    """
    nQubits = 8
    T = 10.0
    #T = sp.arange(2,23,4.0) # Output a sequence of anneal times
    dt = 0.01 * T

    # Output parameters
    binary = 0  # Save output files as binary Numpy format
    progressout = 0  # Output simulation progress over anneal timesteps

    outputdir = 'data/hopfield_hamming_inv/'  # In relation to run.py
    eigspecdat = 1  # Output data for eigspec
    eigspecplot = 0  # Plot eigspec
    eigspecnum = 16  # Number of eigenvalues to output
    fidelplot = 0  # Plot fidelity
    fideldat = 0  # Output fidelity data
    fidelnumstates = 2**nQubits  # Check fidelity with this number of eigenstates
    overlapdat = 0  # Output overlap data
    overlapplot = 0  # Plot overlap
    solveMethod = 'ExpPert'  # 'ExpPert', 'SuzTrot', 'ForRuth', 'BCM'
    # solveMethod = 'SuzTrot' # 'ExpPert', 'SuzTrot', 'ForRuth', 'BCM'

    probshow = 1  # Print final state probabilities to screen
    probout = 1  # Output probabilities to file
    mingap = 0  # Record the minimum spectral gap

    errchk = 0  # Error-checking on/off (for simulation accuracy)
    eps = 0.01  # Numerical error in normalization condition (1 - norm < eps)

    # Specify a QUBO (convert to Ising = True), or alpha, beta directly
    # (convert = False), and also specify the signs on the Ising Hamiltonian
    # terms (you can specify coefficients too for some problems if needed)
    isingConvert = 0
    isingSigns = {'hx': -1, 'hz': -1, 'hzz': -1}

    def spins2bitstr(vec):
        """ Return a converted bitstring from @vec, a list of spins. """
        return ''.join(['0' if k == 1 else '1' for k in vec])

    # Construct network Ising parameters
    neurons = nQubits

    # if nQubits % 2 == 0:
    #     memories = sp.linalg.hadamard(neurons).tolist()
    # inputstate = memories[0]

    # memories = [[ 1,-1,-1, 1, 1,-1,-1, 1],
    #             [-1,-1,-1, 1, 1,-1,-1, 1],
    #             [ 1,-1,-1, 1, 1,-1,-1,-1]]
    memories = [[1, -1, -1, 1, -1, -1, -1, 1], [1, -1, -1, 1, -1, -1, -1, 1],
                [1, -1, -1, 1, -1, -1, -1, -1]]
    inputstate = memories[0]

    print("Input:")
    print(spins2bitstr(inputstate))
    print('')
    print("Memories:")
    for mem in memories:
        print(spins2bitstr(mem))
    print('')

    # This is gamma, the appropriate weighting on the input vector
    # isingSigns['hz'] *= 1 - (len(inputstate) - inputstate.count(0))/(2*neurons)
    # isingSigns['hz'] *= 0.6772598397
    isingSigns['hz'] *= 1.0
    print("Gamma: ", isingSigns['hz'])
    print('')

    alpha = sp.array(inputstate)
    beta = sp.zeros((neurons, neurons))
    delta = sp.array([])

    # Hebb rule - even better matrix style
    memMat = sp.matrix(memories).T
    beta = (sp.triu(memMat * memMat.T) -
            len(memories) * sp.eye(nQubits)) / float(neurons)
    print beta

    # # Hebb rule - matrix style
    # for m, mem in enumerate(memories):
    #     beta += (sp.outer(mem, mem))-sp.eye(neurons)
    # beta = sp.triu(beta)/float(neurons)

    # beta = sp.zeros((neurons,neurons))

    # # Construct pattern matrix according to the Hebb learning rule
    # for i in range(neurons):
    #     for j in range(neurons):
    #         for p in range(len(memories)):
    #             beta[i,j] += ( memories[p][i]*memories[p][j] -
    #                            len(memories)*(i == j) )
    # beta = sp.triu(beta)/float(neurons)

    # Storkey rule (incorrect, but may be useful later)
    # memMat = sp.zeros((neurons,neurons))
    # for m, mem in enumerate(memories):
    #     for i in range(neurons):
    #         for j in range(neurons):
    #             hij = sp.sum([ memMat[i,k]*mem[k] for k in range(neurons) ])
    #             hji = sp.sum([ memMat[j,k]*mem[k] for k in range(neurons) ])
    #             # Don't forget to make the normalization a float!
    #             memMat[i,j] += (mem[i]*mem[j] - mem[i]*hji - hij*mem[j])/float(neurons)
    # beta = sp.triu(memMat)

    # # Storkey rule
    # Wm = sp.zeros((neurons,neurons))
    # for m, mem in enumerate(memories):
    #     Am = sp.mat((sp.outer(mem,mem) - sp.eye(neurons)))
    #     Wm += (Am - Am*Wm - Wm*Am)/float(neurons)
    # beta = sp.triu(Wm)

    # # Construct memory matrix according to the Moore-Penrose pseudoinverse rule
    # memMat = sp.matrix(memories).T
    # beta = sp.triu(memMat * sp.linalg.pinv(memMat))

    # Usually we specify outputs that may be of interest in the form of a dict,
    # but we don't need any for this problem
    outputs = None

    ############################################################################
    ######## All variables must be specified here, do NOT change the keys ######
    ############################################################################

    return {
        'nQubits': nQubits,
        'Q': None,
        'T': T,
        'dt': dt,
        'outputdir': outputdir,
        'errchk': errchk,
        'eps': eps,
        'isingConvert': isingConvert,
        'isingSigns': isingSigns,
        'outputs': outputs,
        'alpha': alpha,
        'beta': beta,
        'delta': delta,
        'eigdat': eigspecdat,
        'eigplot': eigspecplot,
        'eignum': eigspecnum,
        'fiddat': fideldat,
        'fidplot': fidelplot,
        'fidnumstates': fidelnumstates,
        'overlapdat': overlapdat,
        'overlapplot': overlapplot,
        'outdir': outputdir,
        'binary': binary,
        'progressout': progressout,
        'probshow': probshow,
        'probout': probout,
        'mingap': mingap,
        'stateoverlap': None,
        'hzscale': None,
        'hzzscale': None,
        'hxscale': None,
        'solveMethod': solveMethod
    }
Esempio n. 46
0
def parameters(cmdargs):
    """
    """

    # The Hopfield parameters
    hparams = {
        'numNeurons': cmdargs['qubits'],
        'inputState': None,
        'learningRule': cmdargs['simtype'],
        'numMemories': 0
        }

    # Set input state
    inpst = sp.eye(cmdargs['qubits'])[0,:] - 1
    inpst[inpst == 0] = 1
    hparams['inputState'] = inpst.tolist()

    # Construct memories
    imems = cmdargs['instance']
    memories = sp.eye(cmdargs['qubits'])[0:imems,:]
    memories[memories == 0] -= 1
    memories = memories.tolist()

    # Basic simulation params
    nQubits = hparams['numNeurons']
    T = 15. # sp.arange(0.1, 15, 0.5)
    dt = 0.01*T

    # Define states for which to track probabilities in time
    import statelabels
    label_list = statelabels.GenerateLabels(nQubits)
    stateoverlap = []
    for mem in memories:
        # Convert spins to bits
        bitstr = ''.join([ '0' if k == -1 else '1' for k in mem ])
        # Get the index of the current (converted) memory and add it to list
        stateoverlap.append([ label_list.index(bitstr), bitstr ])

    # Output parameters
    binary = 1 # Save output files as binary Numpy format
    progressout = 1 # Output simulation progress over anneal timesteps

    eigspecdat = 1 # Output data for eigspec
    eigspecplot = 0 # Plot eigspec
    eigspecnum = 2**nQubits # Number of eigenvalues to output
    fidelplot = 0 # Plot fidelity
    fideldat = 0 # Output fidelity data
    fidelnumstates = 2**nQubits # Check fidelity with this number of eigenstates
    overlapdat = 0 # Output overlap data
    overlapplot = 0 # Plot overlap
    solveMethod = 'ExpPert' # 'ExpPert', 'SuzTrot', 'ForRuth', 'BCM'

    # Output directory stuff
    probdir = 'data/hopfield_ortho/m'+str(imems)+hparams['learningRule']
    if isinstance(T, collections.Iterable):
        probdir += 'MultiT'
    if os.path.isdir(probdir):
        outlist = sorted([ int(name) for name in os.listdir(probdir) 
                           if name.isdigit() ])
    else:
        outlist = []
    outnum = outlist[-1] + 1 if outlist else 0
    outputdir = probdir + '/' + str(outnum) + '/'

    probshow = 0 # Print final state probabilities to screen
    probout = 1 # Output probabilities to file
    mingap = 1 # Record the minimum spectral gap

    errchk = 0 # Error-checking on/off (for simulation accuracy)
    eps = 0.01 # Numerical error in normalization condition (1 - norm < eps)

    # Specify a QUBO (convert to Ising = True), or alpha, beta directly 
    # (convert = False), and also specify the signs on the Ising Hamiltonian 
    # terms (you can specify coefficients too for some problems if needed)
    isingConvert = 0
    isingSigns = {'hx': -1, 'hz': -1, 'hzz': -1}

    # Construct network Ising parameters
    neurons = nQubits

    # This is gamma, the appropriate weighting on the input vector
    isingSigns['hz'] *= 1 - (len(hparams['inputState']) - 
                             hparams['inputState'].count(0))/(2*neurons)

    alpha = sp.array(hparams['inputState'])
    beta = sp.zeros((neurons,neurons))
    delta = sp.array([])

    # Construct the memory matrix according to a learning rule
    if hparams['learningRule'] == 'hebb':
        # Construct pattern matrix according to Hebb's rule
        for i in range(neurons):
            for j in range(neurons):
                for p in range(len(memories)):
                    beta[i,j] += ( memories[p][i]*memories[p][j] -
                                   len(memories)*(i == j) )
        beta = sp.triu(beta)/float(neurons)
    elif hparams['learningRule'] == 'stork':
        # Construct the memory matrix according to the Storkey learning rule
        memMat = sp.zeros((neurons,neurons))
        for m, mem in enumerate(memories):
            for i in range(neurons):
                for j in range(neurons):
                    hij = sp.sum([ memMat[i,k]*mem[k] for k in range(neurons) ])
                    hji = sp.sum([ memMat[j,k]*mem[k] for k in range(neurons) ])
                    # Don't forget to make the normalization a float!
                    memMat[i,j] += 1./neurons*(mem[i]*mem[j] - mem[i]*hji - 
                                               hij*mem[j])
        beta = sp.triu(memMat)
    elif hparams['learningRule'] == 'proj':
        # Construct memory matrix according to the Moore-Penrose pseudoinverse rule
        memMat = sp.matrix(memories).T
        beta = sp.triu(memMat * sp.linalg.pinv(memMat))

    # Usually we specify outputs that may be of interest in the form of a dict, 
    # but we don't need any for this problem
    outputs = None

    ############################################################################
    ######## All variables must be specified here, do NOT change the keys ######
    ############################################################################

    return {
        'nQubits': nQubits,
        'Q': None,
        'T': T,
        'dt': dt,
        'outputdir': outputdir,
        'errchk': errchk,
        'eps': eps,
        'isingConvert': isingConvert,
        'isingSigns': isingSigns,
        'outputs': outputs,
        'alpha': alpha,
        'beta': beta,
        'delta': delta,
        'eigdat': eigspecdat,
        'eigplot': eigspecplot,
        'eignum': eigspecnum,
        'fiddat': fideldat,
        'fidplot': fidelplot,
        'fidnumstates': fidelnumstates,
        'overlapdat': overlapdat,
        'overlapplot': overlapplot,
        'outdir': outputdir,
        'binary': binary,
        'progressout': progressout,
        'probshow': probshow,
        'probout': probout,
        'mingap': mingap,
        'stateoverlap': stateoverlap,
        'hzscale': None,
        'hzzscale': None,
        'hxscale': None,
        'solveMethod': solveMethod
        }
Esempio n. 47
0
def parameters(cmdargs):
    """
    """

    # The Hopfield parameters
    hparams = {
        'numNeurons': cmdargs['qubits'],
        'inputState': [ 2*sp.random.random_integers(0,1)-1 
                        for k in xrange(cmdargs['qubits']) ],
        'learningRule': cmdargs['simtype'],
        'numMemories': int(cmdargs['farg'])
        }

    # Instances will be actual instances x # of patterns
    instIdx = cmdargs['instance'] / hparams['numMemories']
    # Subinstance will be given by the modulus
    subinst = cmdargs['instances'] % hparams['numMemories']

    # Get memories
    memdat = pickle.load(
        open('exp2_memset_n'+str(hparams['numNeurons'])+'.dat', 'rb'))
    memories = memdat[instIdx]
    del memdat

    # Define the input state as a pattern with Hamming dist. 1 from
    # the pattern with index = subinst
    hparams['inputState'] = memories[subinst]
    hparams['inputState'][sp_rint(0,hparams['numNeurons']-1)] *= -1

    # Simulation variables
    nQubits = hparams['numNeurons']
    T = 15.0
    dt = 0.01*T

    # Define states for which to track probabilities in time
    import statelabels
    label_list = statelabels.GenerateLabels(nQubits)
    stateoverlap = []
    for mem in memories:
        # Convert spins to bits
        bitstr = ''.join([ '0' if k == 1 else '1' for k in mem ])
        # Get the index of the current (converted) memory and add it to list
        stateoverlap.append([ label_list.index(bitstr), bitstr ])

    # Output parameters
    binary = 1 # Save as binary Numpy
    progressout = 0 # Output simulation progress over anneal timesteps

    eigspecdat = 1 # Output data for eigspec
    eigspecplot = 0 # Plot eigspec
    eigspecnum = 2**nQubits # Number of eigenvalues
    fidelplot = 0 # Plot fidelity
    fideldat = 0 # Output fidelity data
    fidelnumstates = 2**nQubits # Check fidelity with this number of eigenstates
    overlapdat = 0 # Output overlap data
    overlapplot = 0 # Plot overlap
    solveMethod = 'ExpPert' # 'ExpPert', 'SuzTrot', 'ForRuth', 'BCM'

    # Output directory stuff
    probdir = 'data/hopfield_exp2/n'+str(nQubits)+'p'+str(pnum)+learningRule
    if isinstance(T, collections.Iterable):
        probdir += 'MultiT'
    if os.path.isdir(probdir):
        outlist = sorted([ int(name) for name in os.listdir(probdir) 
                           if name.isdigit() ])
    else:
        outlist = []
    outnum = outlist[-1] + 1 if outlist else 0
    outputdir = probdir + '/' + str(outnum) + '/'

    probshow = 0 # Print final state probabilities to screen
    probout = 1 # Output probabilities to file
    mingap = 1 # Record the minimum spectral gap

    errchk = 0 # Error-checking on/off (for simulation accuracy)
    eps = 0.01 # Numerical error in normalization condition (1 - norm < eps)

    # Specify a QUBO (convert to Ising = True), or alpha, beta directly 
    # (convert = False), and also specify the signs on the Ising Hamiltonian 
    # terms (you can specify coefficients too for some problems if needed)
    isingConvert = 0
    isingSigns = {'hx': -1, 'hz': -1, 'hzz': -1}

    # This is gamma, the appropriate weighting on the input vector
    isingSigns['hz'] *= 1.0/(5*nQubits)

    neurons = nQubits

    # Initialize Hamiltonian parameters
    alpha = sp.array(hparams['inputState'])
    beta = sp.zeros((neurons,neurons))
    delta = sp.array([])

    # Construct the memory matrix according to a learning rule
    if hparams['learningRule'] == 'hebb':
        # Construct pattern matrix according to Hebb's rule
        for i in range(neurons):
            for j in range(neurons):
                for p in range(len(memories)):
                    beta[i,j] += ( memories[p][i]*memories[p][j] -
                                   len(memories)*(i == j) )
        beta = sp.triu(beta)/float(neurons)
    elif hparams['learningRule'] == 'stork':
        # Construct the memory matrix according to the Storkey learning rule
        memMat = sp.zeros((neurons,neurons))
        for m, mem in enumerate(memories):
            for i in range(neurons):
                for j in range(neurons):
                    hij = sp.sum([ memMat[i,k]*mem[k] for k in range(neurons) ])
                    hji = sp.sum([ memMat[j,k]*mem[k] for k in range(neurons) ])
                    # Don't forget to make the normalization a float!
                    memMat[i,j] += 1./neurons*(mem[i]*mem[j] - mem[i]*hji - 
                                               hij*mem[j])
        beta = sp.triu(memMat)
    elif hparams['learningRule'] == 'proj':
        # Construct memory matrix according to the Moore-Penrose pseudoinverse rule
        memMat = sp.matrix(memories).T
        beta = sp.triu(memMat * sp.linalg.pinv(memMat))

    # Calculate Hamming distance between input state and each memory
    hammingDistance = []
    for mem in memories:
        dist = sp.sum(abs(sp.array(hparams['inputState'])-sp.array(mem))/2)
        hammingDistance.append(dist)

    hamMean = sp.average(hammingDistance)
    hamMed = sp.median(hammingDistance)

    # Some outputs
    outputs = {
        'nQubits': nQubits,
        'learningRule': hparams['learningRule'],
        'outdir': probdir,
        'hparams['inputState']': hparams['inputState'],
        'memories': memories,
        'hammingDistance': {'dist': hammingDistance,
                            'mean': hamMean,
                            'median': hamMed },
        'annealTime': list(T) if isinstance(T, collections.Iterable) else T
               }

    ############################################################################
    ######## All variables must be specified here, do NOT change the keys ######
    ############################################################################

    return {
        'nQubits': nQubits,
        'Q': None,
        'T': T,
        'dt': dt,
        'outputdir': outputdir,
        'errchk': errchk,
        'eps': eps,
        'isingConvert': isingConvert,
        'isingSigns': isingSigns,
        'outputs': outputs,
        'alpha': alpha,
        'beta': beta,
        'delta': delta,
        'eigdat': eigspecdat,
        'eigplot': eigspecplot,
        'eignum': eigspecnum,
        'fiddat': fideldat,
        'fidplot': fidelplot,
        'fidnumstates': fidelnumstates,
        'overlapdat': overlapdat,
        'overlapplot': overlapplot,
        'outdir': outputdir,
        'binary': binary,
        'progressout': progressout,
        'probshow': probshow,
        'probout': probout,
        'mingap': mingap,
        'stateoverlap': stateoverlap,
        'hzscale': None,
        'hzzscale': None,
        'hxscale': None,
        'solveMethod': solveMethod
        }
Esempio n. 48
0
def plot_graph(vertices,
               edges,
               ax,
               xlim=None,
               highlight=None,
               highlight_color='magenta',
               node_color='b',
               edge_color='#999999'):
    """Takes a graph given as vertices and edges and visualizes its structure"""

    start = vertices.ravel().min()
    stop = vertices.ravel().max()

    ### draw grid
    ax.grid(b=True,
            which='major',
            linestyle='--',
            linewidth=0.2,
            color='#222222')
    ax.yaxis.grid(False)

    ### nodes
    patchlist = []
    exon_num = sp.zeros((stop - start, ))
    exon_loc = sp.zeros((1, stop - start))
    exon_level = sp.zeros((vertices.shape[1], ))  #vertices.shape[1]))
    for i in range(vertices.shape[1]):
        cur_vertex = vertices[:, i] - start
        exon_num[cur_vertex[0]:cur_vertex[1]] += 1
        if sp.all(exon_num < 2):
            exon_loc[0, :] = exon_num
            level = 0
        elif exon_num.max() > exon_loc.shape[0]:
            exon_loc = sp.r_[exon_loc, sp.zeros((1, stop - start))]
            exon_loc[-1, cur_vertex[0]:cur_vertex[1]] = 1
            level = exon_loc.shape[0] - 1
        elif exon_num.max() <= exon_loc.shape[0]:
            idx = sp.where(
                sp.all(exon_loc[:, cur_vertex[0]:cur_vertex[1]] == 0,
                       1))[0].min()
            exon_loc[idx, cur_vertex[0]:cur_vertex[1]] = 1
            level = idx

        exon_level[i] = level

        patchlist.append(
            mpatches.Rectangle([cur_vertex[0] + start, 20 + (level * 20)],
                               cur_vertex[1] - cur_vertex[0],
                               10,
                               facecolor=node_color,
                               edgecolor='none',
                               alpha=0.7))

    ### edges
    linelist = []
    intron_loc = sp.zeros((1, stop - start))
    if edges.shape[0] > 1:
        ii, jj = sp.where(sp.triu(edges) > 0)
        for i, j in zip(ii, jj):
            #for i in range(vertices.shape[1]):
            #for j in range(i + 1, vertices.shape[1]):
            #if edges[i ,j] > 0:
            if vertices[0, i] < vertices[0, j]:
                istart = vertices[1, i]
                istop = vertices[0, j]
                level1 = exon_level[i]
                level2 = exon_level[j]
            else:
                istart = vertices[1, j]
                istop = vertices[0, i]
                level1 = exon_level[j]
                level2 = exon_level[i]

            cur_intron = [istart - start, istop - start]
            intron_loc[cur_intron[0]:cur_intron[1]] += 1
            leveli = [(istart + istop) * 0.5, (level1 + level2) * 0.5]
            #ax.plot([istart, leveli[0], istop], [25 + (level1 * 20), 32 + (leveli[1] * 20), 25 + (level2 * 20)], '-', color=edge_color, linewidth=0.5)
            linelist.append(
                mlines.Line2D([istart, leveli[0], istop], [
                    25 + (level1 * 20), 32 + (leveli[1] * 20), 25 +
                    (level2 * 20)
                ],
                              color=edge_color,
                              linewidth=0.5))
            #ax.plot([leveli[0], istop], [32 + (leveli[1] * 20), 25 + (level2 * 20)], '-', color=edge_color, linewidth=0.5)

    ### draw patches
    for node in patchlist:
        ax.add_patch(node)
    for line in linelist:
        ax.add_line(line)

    ### axes
    if xlim is not None:
        ax.set_xlim(xlim)
    else:
        ax.set_xlim([max(start - 20, 0), stop + 20])
    ax.set_ylim([0, 40 + (exon_loc.shape[0] * 20)])
    ax.set_yticklabels([])

    ### highlight if requested
    if highlight is not None:
        rect = patches.Rectangle((highlight[0], 0),
                                 highlight[1] - highlight[0],
                                 ax.get_ylim()[1],
                                 facecolor=highlight_color,
                                 edgecolor='none',
                                 alpha=0.5)
        ax.add_patch(rect)
Esempio n. 49
0
def detect_multipleskips(genes, gidx, log=False, edge_limit=300):
    # [idx_multiple_skips, exon_multiple_skips] = detect_multipleskips(genes, idx_alt) ;

    idx_multiple_skips = []
    exon_multiple_skips = []
    for iix, ix in enumerate(gidx):
        if log:
            sys.stdout.write('.')
            if (iix + 1) % 50 == 0:
                sys.stdout.write(' - %i/%i, found %i\n' % (iix + 1, genes.shape[0] + 1, len(idx_multiple_skips)))
            sys.stdout.flush()
        num_exons = genes[iix].splicegraph.get_len()
        edges = genes[iix].splicegraph.edges
        labels = repmat(sp.arange(num_exons), num_exons, 1).T
        
        # adjecency matrix: upper half only
        A = sp.zeros((num_exons, num_exons))
        for i in range(num_exons - 1):
            for j in range(i + 1, num_exons):
                A[i, j] = edges[i, j]
        
        # possible starting and ending exons of a multiple exon skip
        Pairs = lil_matrix((num_exons, num_exons))
        Ai = sp.dot(sp.dot(A, A), A) #paths of length 3
        while sp.any(Ai.ravel() > 0):
            coords = sp.where((A > 0 ) & (Ai > 0)) # multiple skip
            Pairs[coords[0], coords[1]] = 1
            Ai = sp.dot(Ai, A)  # paths of length ..+1
        
        edge = sp.where(Pairs.toarray() == 1)
        
        if edge[0].shape[0] > edge_limit:
            print '\nWARNING: not processing gene %i (%s); has %i edges; current limit is %i; adjust edge_limit to include.' % (ix, genes[iix].name, edge[0].shape[0], edge_limit)
            continue
        
        for cnt in range(edge[0].shape[0]):
            exon_idx_first = edge[0][cnt]
            exon_idx_last = edge[1][cnt]
      
            if edges[exon_idx_first, exon_idx_last] == 1:
                
                # find all pairs shortest path
                exist_path = sp.triu(edges).astype('double')
                exist_path[exon_idx_first, exon_idx_last] = 0
                exist_path[exist_path == 0] = sp.inf
                # set diagonal to 0
                exist_path[sp.arange(exist_path.shape[0]), sp.arange(exist_path.shape[0])] = 0
                
                long_exist_path = -sp.triu(edges).astype('double')
                long_exist_path[exon_idx_first, exon_idx_last] = 0
                long_exist_path[long_exist_path == 0] = sp.inf
                # set diagonal to 0
                long_exist_path[sp.arange(long_exist_path.shape[0]), sp.arange(long_exist_path.shape[0])] = 0
                
                path = sp.isfinite(exist_path) * labels
                long_path = sp.isfinite(long_exist_path) * labels
                
                for k in range(num_exons):
                    for i in range(num_exons):
                        idx = sp.where((exist_path[i, k] + exist_path[k, :]) < exist_path[i, :])[0]
                        exist_path[i, idx] = exist_path[i, k] + exist_path[k, idx]
                        path[i, idx] = path[k, idx]
                        
                        idx = sp.where((long_exist_path[i,k] + long_exist_path[k, :]) < long_exist_path[i, :])[0]
                        long_exist_path[i, idx] = long_exist_path[i, k] + long_exist_path[k, idx]
                        long_path[i, idx] = long_path[k, idx]
                
                temp_ix = sp.isfinite(long_exist_path)
                long_exist_path[temp_ix] = -long_exist_path[temp_ix]
                
                if (exist_path[exon_idx_first, exon_idx_last] > 2) and sp.isfinite(exist_path[exon_idx_first, exon_idx_last]):
                    backtrace = sp.array([path[exon_idx_first, exon_idx_last]])
                    while backtrace[-1] > exon_idx_first:
                        backtrace = sp.r_[backtrace, path[exon_idx_first, backtrace[-1]]]
                    backtrace = backtrace[:-1]
                    backtrace = backtrace[::-1]
                    idx_multiple_skips.append(ix) #repmat(ix, 1, backtrace.shape[0] + 2))
                    exon_multiple_skips.append([exon_idx_first, backtrace, exon_idx_last])
                elif (long_exist_path[exon_idx_first, exon_idx_last] > 2) and sp.isfinite(long_exist_path[exon_idx_first, exon_idx_last]):
                    backtrace = sp.array([long_path[exon_idx_first, exon_idx_last]])
                    while backtrace[-1] > exon_idx_first:
                        backtrace = sp.r_[backtrace, long_path[exon_idx_first, backtrace[-1]]]
                    backtrace = backtrace[:-1]
                    backtrace = backtrace[::-1]
                    idx_multiple_skips.append(ix) #repmat(ix, 1, backtrace.shape[0] + 2))
                    exon_multiple_skips.append([exon_idx_first, backtrace, exon_idx_last])

    if log:
        print 'Number of multiple exon skips:\t\t\t\t\t%d' % len(idx_multiple_skips)

    return (idx_multiple_skips, exon_multiple_skips)
Esempio n. 50
0
def plot_graph(vertices, edges, ax, xlim=None, highlight=None, highlight_color='magenta', node_color='b',
               edge_color='#999999'):
    """Takes a graph given as vertices and edges and visualizes its structure"""

    start = vertices.ravel().min()
    stop = vertices.ravel().max()

    ### draw grid
    ax.grid(b=True, which='major', linestyle='--', linewidth=0.2, color='#222222')
    ax.yaxis.grid(False)

    ### nodes
    patchlist = []
    exon_num = sp.zeros((stop - start,))
    exon_loc = sp.zeros((1, stop - start))
    exon_level = sp.zeros((vertices.shape[1], )) #vertices.shape[1]))
    for i in range(vertices.shape[1]):
        cur_vertex = vertices[:, i] - start
        exon_num[cur_vertex[0]:cur_vertex[1]] += 1
        if sp.all(exon_num < 2):
            exon_loc[0, :] = exon_num
            level = 0
        elif exon_num.max() > exon_loc.shape[0]:
            exon_loc = sp.r_[exon_loc, sp.zeros((1, stop - start))]
            exon_loc[-1, cur_vertex[0]:cur_vertex[1]] = 1 
            level = exon_loc.shape[0] - 1
        elif exon_num.max() <= exon_loc.shape[0]:
            idx = sp.where(sp.all(exon_loc[:, cur_vertex[0]:cur_vertex[1]] == 0, 1))[0].min() 
            exon_loc[idx, cur_vertex[0]:cur_vertex[1]] = 1
            level = idx
       
        exon_level[i] = level
        
        patchlist.append(mpatches.Rectangle([cur_vertex[0] + start, 20 + (level * 20)], cur_vertex[1] - cur_vertex[0], 10, facecolor=node_color, edgecolor='none', alpha=0.7))

    ### edges
    linelist = []
    intron_loc = sp.zeros((1, stop - start))
    if edges.shape[0] > 1:
        ii, jj = sp.where(sp.triu(edges) > 0)
        for i, j in zip(ii, jj):
        #for i in range(vertices.shape[1]):
            #for j in range(i + 1, vertices.shape[1]):
                #if edges[i ,j] > 0:
            if vertices[0,i] < vertices[0,j]:
                istart = vertices[1, i]
                istop = vertices[0, j]
                level1 = exon_level[i]
                level2 = exon_level[j]
            else:
                istart = vertices[1, j]
                istop = vertices[0, i]
                level1 = exon_level[j]
                level2 = exon_level[i]
      
            cur_intron = [istart - start, istop - start]
            intron_loc[cur_intron[0]:cur_intron[1]] += 1
            leveli = [(istart + istop) * 0.5, (level1 + level2) * 0.5]
            #ax.plot([istart, leveli[0], istop], [25 + (level1 * 20), 32 + (leveli[1] * 20), 25 + (level2 * 20)], '-', color=edge_color, linewidth=0.5)
            linelist.append(mlines.Line2D([istart, leveli[0], istop], [25 + (level1 * 20), 32 + (leveli[1] * 20), 25 + (level2 * 20)], color=edge_color, linewidth=0.5))
            #ax.plot([leveli[0], istop], [32 + (leveli[1] * 20), 25 + (level2 * 20)], '-', color=edge_color, linewidth=0.5)

    ### draw patches 
    for node in patchlist:
        ax.add_patch(node)
    for line in linelist:
        ax.add_line(line)

    ### axes 
    if xlim is not None:
        ax.set_xlim(xlim)
    else:
        ax.set_xlim([max(start - 20, 0), stop + 20])
    ax.set_ylim([0, 40 + (exon_loc.shape[0] * 20)]) 
    ax.set_yticklabels([])

    ### highlight if requested
    if highlight is not None:
        rect = patches.Rectangle((highlight[0], 0), highlight[1] - highlight[0], ax.get_ylim()[1], facecolor=highlight_color, edgecolor='none', alpha=0.5)
        ax.add_patch(rect)
Esempio n. 51
0
logsched = np.array([ fieldstart/(np.log(k+1)*rlog + 1)
                      for k in xrange(annealingsteps) ])
trlog = (tempend/(tempstart-1))/np.log(tannealingsteps+1)
tlogsched = np.array([ tempstart/(np.log(k+1)*trlog + 1)
                      for k in xrange(tannealingsteps) ])
# hack
tannealingsched = texpsched
annealingsched = expsched

def getbitstr(vec):
    """ Return bitstring from spin vector array. """
    return reduce(lambda x,y: x+y, 
                  [ str(int(k)) for k in tools.spins2bits(vec) ])
# Construct Ising matrix
memMat = sp.matrix(memories).T
isingJ = sp.triu(memMat * sp.linalg.pinv(memMat))
isingJ -= sp.diag(sp.diag(isingJ))
isingJ = sp.triu(memMat * sp.linalg.pinv(memMat))
isingJ += inpbias*sp.diag(vinput)
isingJ = sps.dok_matrix(isingJ)
# get energies of all states
results = []
energies = np.zeros(2**nspins)
for b in [ bin(x)[2:].rjust(nspins, '0') for x in range(2**nspins) ]:
    svec = tools.bits2spins(np.array([ int(k) for k in b ]))
    energies[int(b,2)] = sa.ClassicalIsingEnergy(svec, isingJ)
    results.append([energies[int(b,2)], b])
print("All possible states and their energies:")
for res in sorted(results)[:10]:
    print("%s    %2.4f" % tuple(res[::-1]))
Esempio n. 52
0
def parameters(cmdargs):
    """
    """
    nQubits = 6
    T = 30.0
    #T = sp.arange(2,103,10) # Output a sequence of anneal times
    dt = 0.1

    # Output parameters
    binary = 1  # Save output files as binary Numpy format
    progressout = 0  # Output simulation progress over anneal timesteps

    outputdir = 'data/sixvarqubo_overlap/'  # In relation to run.py
    eigspecdat = 0  # Output data for eigspec
    eigspecplot = 0  # Plot eigspec
    eigspecnum = 2**nQubits  # Number of eigenvalues to output
    fidelplot = 0  # Plot fidelity
    fideldat = 0  # Output fidelity data
    fidelnumstates = 2**nQubits  # Check fidelity with this number of eigenstates
    overlapdat = 0  # Output overlap data
    overlapplot = 0  # Plot overlap
    solveMethod = 'ExpPert'  # 'ExpPert', 'SuzTrot', 'ForRuth', 'BCM'

    probshow = 1  # Print final state probabilities to screen
    #### BUG ##### probshow requires probout
    probout = 1  # Output probabilities to file

    mingap = 0  # Record the minimum spectral gap

    errchk = 0  # Error-checking on/off (for simulation accuracy)
    eps = 0.01  # Numerical error in normalization condition (1 - norm < eps)

    # Specify a QUBO (convert to Ising = True), or alpha, beta directly
    # (convert = False), and also specify the signs on the Ising Hamiltonian
    # terms (you can specify coefficients too for some problems if needed)
    isingConvert = 1
    isingSigns = {'hx': 1, 'hz': 1, 'hzz': -1}

    # Define states for which to track probabilities in time
    import statelabels
    label_list = statelabels.GenerateLabels(nQubits)
    stateoverlap = []
    for bitstr in ['100101', '110101']:
        # Get the index of the current (converted) memory and add it to list
        stateoverlap.append([label_list.index(bitstr), bitstr])

    # Define the QUBO and its diagonal
    Q = sp.zeros((nQubits, nQubits))
    a = sp.zeros(nQubits)

    a[0] = 2
    a[1] = 1
    a[2] = -2
    a[3] = -1
    a[4] = 1
    a[5] = -1

    Q[0, 1] = Q[1, 0] = -1
    Q[0, 2] = Q[2, 0] = 2
    Q[0, 3] = Q[3, 0] = -2
    Q[0, 4] = Q[4, 0] = 2
    Q[0, 5] = Q[5, 0] = -1

    Q[1, 2] = Q[2, 1] = 1
    Q[1, 3] = Q[3, 1] = -1
    Q[1, 4] = Q[4, 1] = -1
    Q[1, 5] = Q[5, 1] = 1

    Q[2, 3] = Q[3, 2] = 2
    Q[2, 4] = Q[4, 2] = -2
    Q[2, 5] = Q[5, 2] = 1

    Q[3, 4] = Q[4, 3] = 2
    Q[3, 5] = Q[5, 3] = -1

    Q[4, 5] = Q[5, 4] = 2

    Q = sp.triu(Q) + a * sp.identity(6)

    # Usually we specify outputs that may be of interest in the form of a dict,
    # but we don't need any for this problem
    outputs = None

    # We could specify these below, but we want to avoid modifying that part
    alpha = beta = delta = None

    ############################################################################
    ######## All variables must be specified here, do NOT change the keys ######
    ############################################################################

    return {
        'nQubits': nQubits,
        'Q': Q,
        'T': T,
        'dt': dt,
        'outputdir': outputdir,
        'errchk': errchk,
        'eps': eps,
        'isingConvert': isingConvert,
        'isingSigns': isingSigns,
        'outputs': outputs,
        'alpha': alpha,
        'beta': beta,
        'delta': delta,
        'eigdat': eigspecdat,
        'eigplot': eigspecplot,
        'eignum': eigspecnum,
        'fiddat': fideldat,
        'fidplot': fidelplot,
        'fidnumstates': fidelnumstates,
        'overlapdat': overlapdat,
        'overlapplot': overlapplot,
        'outdir': outputdir,
        'binary': binary,
        'progressout': progressout,
        'probshow': probshow,
        'probout': probout,
        'mingap': mingap,
        'stateoverlap': stateoverlap,
        'solveMethod': solveMethod
    }
Esempio n. 53
0
def parameters(cmdargs):
    """
    cmdargs:
             -q, qubits 
             -k, lrule
             -f, nmems
             -g, hdist
    """

    # The Hopfield parameters
    hparams = {
        'numNeurons': cmdargs['qubits'],
        'inputState': [],
        'learningRule': cmdargs['simtype'],
        'numMemories': int(cmdargs['farg'])
        }

    # Construct memories
    memories = [ [ 2*sp.random.random_integers(0,1)-1 
                   for k in xrange(hparams['numNeurons']) ] ]
    # Add a few memories that are some Hamming distance away from each other
    def hamdist(a,b):
        """ Calculate Hamming distance. """
        return sp.sum(abs(sp.array(a)-sp.array(b))/2.0)
    for m in range(hparams['numMemories']-1):
        # pick a random memory
        newmem = memories[sp.random.random_integers(0,len(memories)-1)][:]
        # flip a random bit
        rndbit = sp.random.random_integers(0,hparams['numNeurons']-1)
        newmem[rndbit] *= -1
        # make sure no duplicates
        while newmem in memories:
            rndbit = sp.random.random_integers(0,hparams['numNeurons']-1)
            newmem[rndbit] *= -1
        # add it
        memories.append(newmem)

    # Choose a random memory for the input state
    hparams['inputState'] = memories[
        sp.random.random_integers(0,len(memories)-1)]
        
    # Basic simulation params
    nQubits = hparams['numNeurons']
    T = 10.0 # sp.arange(0.1, 15, 0.5)
    # T = sp.array([10.0, 20.0, 50.0, 100.0])
    dt = 0.01*T

    # Define states for which to track probabilities in time
    # import statelabels
    # label_list = statelabels.GenerateLabels(nQubits)
    # stateoverlap = []
    # for mem in memories:
    #     # Convert spins to bits
    #     bitstr = ''.join([ '0' if k == 1 else '1' for k in mem ])
    #     # Get the index of the current (converted) memory and add it to list
    #     stateoverlap.append([ label_list.index(bitstr), bitstr ])
    stateoverlap = None

    # Output parameters
    binary = 1 # Save output files as binary Numpy format
    progressout = 0 # Output simulation progress over anneal timesteps

    eigspecdat = 1 # Output data for eigspec
    eigspecplot = 0 # Plot eigspec
    eigspecnum = 16 # Number of eigenvalues to output
    fidelplot = 0 # Plot fidelity
    fideldat = 0 # Output fidelity data
    fidelnumstates = 2**nQubits # Check fidelity with this number of eigenstates
    overlapdat = 0 # Output overlap data
    overlapplot = 0 # Plot overlap
    solveMethod = 'ExpPert' # 'ExpPert', 'SuzTrot', 'ForRuth', 'BCM'

    # Output directory stuff
    probdir = 'data/hopfield_hamming/n'+str(nQubits)+'p'+\
        str(hparams['numMemories'])+hparams['learningRule']
    if isinstance(T, collections.Iterable):
        probdir += 'MultiT'
    if os.path.isdir(probdir):
        outlist = sorted([ int(name) for name in os.listdir(probdir) 
                           if name.isdigit() ])
    else:
        outlist = []
    outnum = outlist[-1] + 1 if outlist else 0
    outputdir = probdir + '/' + str(outnum) + '/'

    probshow = 0 # Print final state probabilities to screen
    probout = 1 # Output probabilities to file
    mingap = 0 # Record the minimum spectral gap

    errchk = 0 # Error-checking on/off (for simulation accuracy)
    eps = 0.01 # Numerical error in normalization condition (1 - norm < eps)

    # Specify a QUBO (convert to Ising = True), or alpha, beta directly 
    # (convert = False), and also specify the signs on the Ising Hamiltonian 
    # terms (you can specify coefficients too for some problems if needed)
    isingConvert = 0
    isingSigns = {'hx': -1, 'hz': -1, 'hzz': -1}

    # Construct network Ising parameters
    neurons = nQubits

    # This is gamma, the appropriate weighting on the input vector
    # isingSigns['hz'] *= 1 - (len(hparams['inputState']) - 
    #                          hparams['inputState'].count(0))/(2*neurons)
    # isingSigns['hz'] *= 1.0/(5*neurons)

    alpha = sp.array(hparams['inputState'])
    beta = sp.zeros((neurons,neurons))
    delta = sp.array([])

    # Construct the memory matrix according to a learning rule
    if hparams['learningRule'] == 'hebb':
        # Hebb rule
        isingSigns['hz'] *= 0.7
        memMat = sp.matrix(memories).T
        beta = sp.triu(memMat*memMat.T)/float(neurons)
    elif hparams['learningRule'] == 'stork':
        # Storkey rule
        isingSigns['hz'] *= 0.15
        Wm = sp.zeros((neurons,neurons))
        for m, mem in enumerate(memories):
            Am = sp.outer(mem,mem) - sp.eye(neurons)
            Wm += (Am - Am*Wm - Wm*Am)/float(neurons)
        beta = sp.triu(Wm)
    elif hparams['learningRule'] == 'proj':
        isingSigns['hz'] *= 0.15
        # Moore-Penrose pseudoinverse rule
        memMat = sp.matrix(memories).T
        beta = sp.triu(memMat * sp.linalg.pinv(memMat))

    # Some outputs
    outputs = {
        'nQubits': nQubits,
        'learningRule': hparams['learningRule'],
        'outdir': probdir,
        'inputState': hparams['inputState'],
        'memories': memories,
        'answer': memories[0],
        'annealTime': list(T) if isinstance(T, collections.Iterable) else T
               }

    ############################################################################
    ######## All variables must be specified here, do NOT change the keys ######
    ############################################################################

    return {
        'nQubits': nQubits,
        'Q': None,
        'T': T,
        'dt': dt,
        'outputdir': outputdir,
        'errchk': errchk,
        'eps': eps,
        'isingConvert': isingConvert,
        'isingSigns': isingSigns,
        'outputs': outputs,
        'alpha': alpha,
        'beta': beta,
        'delta': delta,
        'eigdat': eigspecdat,
        'eigplot': eigspecplot,
        'eignum': eigspecnum,
        'fiddat': fideldat,
        'fidplot': fidelplot,
        'fidnumstates': fidelnumstates,
        'overlapdat': overlapdat,
        'overlapplot': overlapplot,
        'outdir': outputdir,
        'binary': binary,
        'progressout': progressout,
        'probshow': probshow,
        'probout': probout,
        'mingap': mingap,
        'stateoverlap': stateoverlap,
        'hzscale': None,
        'hzzscale': None,
        'hxscale': None,
        'solveMethod': solveMethod
        }