def vRaman(x,omega=1.0,delta=0.0,epsilon=0.048,phi=4.0/3.0):
    x=np.array(x)    
    s=1
    v=np.outer(omega*np.exp(1.0j*2.0*phi*x),Fx(s)*np.sqrt(2.0)/2.0).reshape(x.size,2*s+1,2*s+1)
    v=sLA.block_diag(*[np.triu(v[i])+np.conjugate(np.triu(v[i],1)).transpose() for i in range(x.size)])
    v+=sLA.block_diag(*[np.diag([epsilon-delta,0.0,epsilon+delta])]*x.size)
    return v
Beispiel #2
0
    def prob(self, state):
        """
        Calculates the probability of a given state using the exponential
        family parameterization learned from a prior dataset. Note that
        we are assuming A(theta) = 0 (i.e., the graph is triangulated).
        Also note the similarity to calc_mu.
        """
        n1 = state[0,0:self.num_sites]
        n2 = state[0,self.num_sites:]
        mu_s = state
        s = np.zeros((self.num_nodes, self.num_nodes))
        mu_st11 = np.matrix(s)
        mu_st10 = np.matrix(s)
        mu_st01 = np.matrix(s)
        mu_st00 = np.matrix(s)

        """
        Calculate the edges from n->n'.
        This is the upper-right quadrant.
        """	
        upright = self.calc_mu_quadrant(n1, n2, False)
        mu_st11[0:self.num_sites,self.num_sites:] += upright[3] # nn11
        mu_st10[0:self.num_sites,self.num_sites:] += upright[2] # nn10
        mu_st01[0:self.num_sites,self.num_sites:] += upright[1] # nn01
        mu_st00[0:self.num_sites,self.num_sites:] += upright[0] # nn00
        """
        Calculate the edges from n'->n'.
        This is the lower-right quadrant.
        """
        lowright = self.calc_mu_quadrant(n2, n2, True)
        mu_st11[self.num_sites:,self.num_sites:] += lowright[3] # nn11
        mu_st10[self.num_sites:,self.num_sites:] += lowright[2] # nn10
        mu_st01[self.num_sites:,self.num_sites:] += lowright[1] # nn01
        mu_st00[self.num_sites:,self.num_sites:] += lowright[0] # nn00
        """
        We only want the upper triangle, since otherwise we will double
        count the n'->n' edges.
        """
        mu_st11 = np.triu(mu_st11)
        mu_st10 = np.triu(mu_st10)
        mu_st01 = np.triu(mu_st01)
        mu_st00 = np.triu(mu_st00)
        """
        Sum(x_s)
        """
        sum_s = mu_s * self.nodes[0].T
        sum_s += (mu_s - 1) * -1 * self.nodes[1].T
        sum_s = sum_s[0,0]
        """
        Sum(x_s,x_t)
        """
        sum_st00 = np.sum(np.multiply(mu_st00, self.edges[0]))
        sum_st01 = np.sum(np.multiply(mu_st01, self.edges[1]))
        sum_st10 = np.sum(np.multiply(mu_st10, self.edges[2]))
        sum_st11 = np.sum(np.multiply(mu_st11, self.edges[3]))
        result = sum_s + sum_st00 + sum_st01 + sum_st10 + sum_st11
        #print "S: {0} ST[00]: {1} ST[01]: {2} ST[10]: {3} ST[11]: {4}".format(sum_s, sum_st00, sum_st01, sum_st10, sum_st11)
        #print "Prob: {0}".format(result)
        #pseudo-likelihood
        return result
Beispiel #3
0
def make_connections(n,density=0.25):
	"""
	This function will return a random adjacency matrix of size
	n x n. You read the matrix like this:
	
	if matrix[2,7] = 1, then cities '2' and '7' are connected.
	if matrix[2,7] = 0, then the cities are _not_ connected.
	
	:param n: number of cities
	:param density: controls the ratio of 1s to 0s in the matrix
	
	:returns: an n x n adjacency matrix
	"""
	
	import networkx
	
	# Generate a random adjacency matrix and use it to build a networkx graph
	a=numpy.int32(numpy.triu((numpy.random.random_sample(size=(n,n))<density)))
	G=networkx.from_numpy_matrix(a)
	
	# If the network is 'not connected' (i.e., there are isolated nodes)
	# generate a new one. Keep doing this until we get a connected one.
	# Yes, there are more elegant ways to do this, but I'm demonstrating
	# while loops!
	while not networkx.is_connected(G):
		a=numpy.int32(numpy.triu((numpy.random.random_sample(size=(n,n))<density)))
		G=networkx.from_numpy_matrix(a)
	
	# Cities should be connected to themselves.
	numpy.fill_diagonal(a,1)
	
	return a + numpy.triu(a,1).T
    def __init__(self, rng, matches_vec, batch_size,
            sample_diff_every_epoch=True, n_same_pairs=None):
        """
        If `n_same_pairs` is given, this number of same pairs is sampled,
        otherwise all same pairs are used.
        """
        self.rng = rng
        self.matches_vec = matches_vec
        self.batch_size = batch_size

        if n_same_pairs is None:
            # Use all pairs
            I, J = np.where(np.triu(distance.squareform(matches_vec)))  # indices of same pairs
        else:
            # Sample same pairs
            n_pairs = min(n_same_pairs, len(np.where(matches_vec == True)[0]))
            same_sample = self.rng.choice(
                np.where(matches_vec == True)[0], size=n_pairs, replace=False
                )
            same_vec = np.zeros(self.matches_vec.shape[0], dtype=np.bool)
            same_vec[same_sample] = True
            I, J = np.where(np.triu(distance.squareform(same_vec)))

        self.x1_same_indices = []
        self.x2_same_indices = []
        for i, j in zip(I, J):
            self.x1_same_indices.append(i)
            self.x2_same_indices.append(j)

        if not sample_diff_every_epoch:
            self.x1_diff_indices, self.x2_diff_indices = self._sample_diff_pairs()
        self.sample_diff_every_epoch = sample_diff_every_epoch
Beispiel #5
0
 def test_compute_modes(self):
     ws = N.identity(self.num_states)
     tol = 1e-6
     weights_full = N.mat(N.random.random((self.num_states, self.num_states)))
     weights_full = N.triu(weights_full) + N.triu(weights_full, 1).H
     weights_full = weights_full*weights_full
     weights_diag = N.random.random(self.num_states)
     weights_list = [None, weights_diag, weights_full]
     vec_array = N.random.random((self.num_states, self.num_vecs))
     for weights in weights_list:
         IP = VectorSpaceMatrices(weights=weights).compute_inner_product_mat
         correlation_mat_true = IP(vec_array, vec_array)
         eigen_vals_true, eigen_vecs_true = util.eigh(correlation_mat_true)
         build_coeff_mat_true = eigen_vecs_true * N.mat(N.diag(
             eigen_vals_true**-0.5))
         modes_true = vec_array.dot(build_coeff_mat_true)
         
         modes, eigen_vals, eigen_vecs, correlation_mat = \
             compute_POD_matrices_snaps_method(vec_array, self.mode_indices, 
             inner_product_weights=weights, return_all=True)
         
         N.testing.assert_allclose(eigen_vals, eigen_vals_true, rtol=tol)
         N.testing.assert_allclose(eigen_vecs, eigen_vecs_true)
         N.testing.assert_allclose(correlation_mat, correlation_mat_true)
         N.testing.assert_allclose(modes, modes_true[:,self.mode_indices])
                     
         modes, eigen_vals, eigen_vecs = \
             compute_POD_matrices_direct_method(vec_array, self.mode_indices, 
             inner_product_weights=weights, return_all=True)
         
         N.testing.assert_allclose(eigen_vals, eigen_vals_true)
         N.testing.assert_allclose(N.abs(eigen_vecs), N.abs(eigen_vecs_true))
         N.testing.assert_allclose(N.abs(modes), N.abs(modes_true[:,self.mode_indices]))
Beispiel #6
0
def test_dingdong():
    """Simple test of dingdong(n) for n = 9. Only tests to
    see if matrix is symmetric. Again, very lame testing,
    but it is a start"""
    a = rogues.dingdong(9)
    # Matrix must be Hermetian
    assert((np.triu(a) == np.triu(np.conj(a.T))).all())
Beispiel #7
0
        def _test(n):
            """Do an nxn test case worth 5 points."""
            A = self._luTestCase(n)
            L1, U1 = lu(A)
            if not np.allclose(L1.dot(U1), A):
                return _test(n)
            stu = s.lu(A.copy())
            try:
                L2, U2 = stu
            except(TypeError, ValueError):
                raise ValueError("lu() failed to return two arrays")

            pts = 5
            if not all(np.allclose(*x) for x in [(np.tril(L2), L2),
                   (np.triu(U2), U2), (L1, L2), (U1, U2), (A, L2.dot(U2))]):
                pts = 2
                self.feedback += "\n\n{}\nA =\n{}".format('- '*20, A)
                if not np.allclose(np.tril(L2), L2):
                    self.feedback += "\nL not lower triangular:\n{}".format(L2)
                    pts -= 1
                if not np.allclose(np.triu(U2), U2):
                    self.feedback += "\nU not upper triangular:\n{}".format(U2)
                    pts -= 1
                pts += self._eqTest(L1, L2, "lu() failed (L incorrect)")
                pts += self._eqTest(U1, U2, "lu() failed (U incorrect)")
                pts += self._eqTest(A, L2.dot(U2), "lu() failed (A != LU)")
            return pts
Beispiel #8
0
    def test_trsv(self):
        seed(1234)
        for ind, dtype in enumerate(DTYPES):
            n = 15
            A = (rand(n, n)+eye(n)).astype(dtype)
            x = rand(n).astype(dtype)
            func, = get_blas_funcs(('trsv',), dtype=dtype)

            y1 = func(a=A, x=x)
            y2 = solve(triu(A), x)
            assert_array_almost_equal(y1, y2)

            y1 = func(a=A, x=x, lower=1)
            y2 = solve(tril(A), x)
            assert_array_almost_equal(y1, y2)

            y1 = func(a=A, x=x, diag=1)
            A[arange(n), arange(n)] = dtype(1)
            y2 = solve(triu(A), x)
            assert_array_almost_equal(y1, y2)

            y1 = func(a=A, x=x, diag=1, trans=1)
            y2 = solve(triu(A).T, x)
            assert_array_almost_equal(y1, y2)

            y1 = func(a=A, x=x, diag=1, trans=2)
            y2 = solve(triu(A).conj().T, x)
            assert_array_almost_equal(y1, y2)
def generate_random_network(beta, N, k_bar, kappa0, gamma):
    mu = beta * math.sin(math.pi / beta) / (2 * math.pi * k_bar)
    graphe_random = nx.Graph()
    kappa = powerlaw.Power_Law(xmin=kappa0, parameters=[gamma]).generate_random(N)
    theta = np.random.uniform(0, 2 * math.pi, N)
    # print 1+(len(kappa)/sum(np.log(kappa)))
    th = np.outer(np.ones(N), theta)
    # print "th"
    th2 = np.outer(theta, np.ones(N))
    # print "th2"
    kp = np.outer(np.ones(N), kappa)
    # print "kp"
    kp2 = np.outer(kappa, np.ones(N))
    # print "kp2"
    dth = math.pi - np.abs(math.pi - np.abs(th - th2))
    # print "dth"
    khi = N * dth / (2 * math.pi * mu * kp * kp2)
    # print "khi"
    r = np.random.uniform(0, 1, [N, N])
    # print "r"
    r2 = np.triu(np.log(1 / r - 1) / beta, k=1)
    p2 = np.triu(np.log(khi), k=1)
    # print np.where(r2>p2) ie ln(1/r-1) / beta > ln(khi) <=> r < 1/(1+khi^beta)
    graphe_random.add_edges_from(np.transpose(np.where(r2 > p2)))
    return graphe_random
 def pressure(self):
     ps = 1.
     sig = 1.
     eps = 1.
     d = 2.
     N = np.size(self.c.x)
     
     dx = self.c.dx()
     dy = self.c.dy()
     dz = self.c.dz()
     dr2 = self.c.dr2()
     
     r_mag = (dx ** 2 + dy ** 2 + dz ** 2)
     r_mag = np.nan_to_num(r_mag)
     
     px = dx * (24 * eps / r_mag) * ((2 * (sig**12 / r_mag**6)) - (sig**6 / r_mag**3))
     px = np.nan_to_num(px)
     px = np.triu(px)
     py = dy * (24 * eps / r_mag) * ((2 * (sig**12 / r_mag**6)) - (sig**6 / r_mag**3))
     py = np.nan_to_num(py)
     py = np.triu(py)
     pz = dz * (24 * eps / r_mag) * ((2 * (sig**12 / r_mag**6)) - (sig**6 / r_mag**3))
     pz = np.nan_to_num(pz)
     pz = np.triu(pz)
     pt = px + py + pz
     return 1 / (d * N * self.ke()) * np.sum(pt)
Beispiel #11
0
    def _pipe_as_source(self):
        rnd_norm_matr = np.random.randn(self.n_node, self.n_node)
        rnd_cov_matr = np.abs(np.triu(rnd_norm_matr) +
                              np.triu(rnd_norm_matr).T)
        signal = np.random.multivariate_normal(np.zeros(self.n_node),
                                               rnd_cov_matr,
                                               self.n_sample)

        start_ix = 0
        while True:
            clip = signal[start_ix:start_ix+self.win_width, :]

            # Properly format the yield
            signal_packet = {}
            signal_packet['data'] = clip
            signal_packet['meta'] = \
                    {'ax_0':
                     {'label': 'Samples',
                      'index': np.arange(start_ix, start_ix+self.win_width)},
                     'ax_1':
                     {'label': 'Nodes',
                      'index': np.array(map(str, xrange(self.n_node)))}}
            yield signal_packet

            # Advance the pointer
            start_ix += self.win_shift

            if start_ix+self.win_width > self.n_sample:
                my_display('\nEnd of signal.\n')
                break
Beispiel #12
0
    def test_tpsv(self):
        seed(1234)
        for ind, dtype in enumerate(DTYPES):
            n = 10
            x = rand(n).astype(dtype)
            # Upper triangular array
            A = triu(rand(n, n)) if ind < 2 else triu(rand(n, n)+rand(n, n)*1j)
            A += eye(n)
            # Form the packed storage
            c, r = tril_indices(n)
            Ap = A[r, c]
            func, = get_blas_funcs(('tpsv',), dtype=dtype)

            y1 = func(n=n, ap=Ap, x=x)
            y2 = solve(A, x)
            assert_array_almost_equal(y1, y2)

            y1 = func(n=n, ap=Ap, x=x, diag=1)
            A[arange(n), arange(n)] = dtype(1)
            y2 = solve(A, x)
            assert_array_almost_equal(y1, y2)

            y1 = func(n=n, ap=Ap, x=x, diag=1, trans=1)
            y2 = solve(A.T, x)
            assert_array_almost_equal(y1, y2)

            y1 = func(n=n, ap=Ap, x=x, diag=1, trans=2)
            y2 = solve(A.conj().T, x)
            assert_array_almost_equal(y1, y2)
    def get_signal_correlation(self, corr='spearman'):
        logging.debug("Calculating signal correlation")

        response = self.response[:, 1:, :self.numbercells, 0] # orientation x freq x cell, no blank
        response = response.reshape(self.number_ori * (self.number_tf-1), self.numbercells).T
        N, Nstim = response.shape

        signal_corr = np.zeros((N, N))
        signal_p = np.empty((N, N))
        if corr == 'pearson':
            for i in range(N):
                for j in range(i, N): # matrix is symmetric
                    signal_corr[i, j], signal_p[i, j] = st.pearsonr(response[i], response[j])

        elif corr == 'spearman':
            for i in range(N):
                for j in range(i, N): # matrix is symmetric
                    signal_corr[i, j], signal_p[i, j] = st.spearmanr(response[i], response[j])

        else:
            raise Exception('correlation should be pearson or spearman')

        signal_corr = np.triu(signal_corr) + np.triu(signal_corr, 1).T  # fill in lower triangle
        signal_p = np.triu(signal_p) + np.triu(signal_p, 1).T  # fill in lower triangle

        return signal_corr, signal_p
    def get_representational_similarity(self, corr='spearman'):
        logging.debug("Calculating representational similarity")

        response = self.response[:, 1:, :self.numbercells, 0] # orientation x freq x phase x cell, no blank
        response = response.reshape(self.number_ori * (self.number_tf-1), self.numbercells)
        Nstim, N = response.shape

        rep_sim = np.zeros((Nstim, Nstim))
        rep_sim_p = np.empty((Nstim, Nstim))
        if corr == 'pearson':
            for i in range(Nstim):
                for j in range(i, Nstim): # matrix is symmetric
                    rep_sim[i, j], rep_sim_p[i, j] = st.pearsonr(response[i], response[j])

        elif corr == 'spearman':
            for i in range(Nstim):
                for j in range(i, Nstim): # matrix is symmetric
                    rep_sim[i, j], rep_sim_p[i, j] = st.spearmanr(response[i], response[j])

        else:
            raise Exception('correlation should be pearson or spearman')

        rep_sim = np.triu(rep_sim) + np.triu(rep_sim, 1).T # fill in lower triangle
        rep_sim_p = np.triu(rep_sim_p) + np.triu(rep_sim_p, 1).T  # fill in lower triangle

        return rep_sim, rep_sim_p
  def test_iris(self):

    # Generate full set of constraints for comparison with reference implementation
    mask = (self.iris_labels[None] == self.iris_labels[:, None])
    a, b = np.nonzero(np.triu(mask, k=1))
    c, d = np.nonzero(np.triu(~mask, k=1))

    # Full metric
    mmc = MMC(convergence_threshold=0.01)
    mmc.fit(self.iris_points, [a, b, c, d])
    expected = [[+0.00046504, +0.00083371, -0.00111959, -0.00165265],
                [+0.00083371, +0.00149466, -0.00200719, -0.00296284],
                [-0.00111959, -0.00200719, +0.00269546, +0.00397881],
                [-0.00165265, -0.00296284, +0.00397881, +0.00587320]]
    assert_array_almost_equal(expected, mmc.metric(), decimal=6)

    # Diagonal metric
    mmc = MMC(diagonal=True)
    mmc.fit(self.iris_points, [a, b, c, d])
    expected = [0, 0, 1.21045968, 1.22552608]
    assert_array_almost_equal(np.diag(expected), mmc.metric(), decimal=6)

    # Supervised Full
    mmc = MMC_Supervised()
    mmc.fit(self.iris_points, self.iris_labels)
    csep = class_separation(mmc.transform(), self.iris_labels)
    self.assertLess(csep, 0.15)

    # Supervised Diagonal
    mmc = MMC_Supervised(diagonal=True)
    mmc.fit(self.iris_points, self.iris_labels)
    csep = class_separation(mmc.transform(), self.iris_labels)
    self.assertLess(csep, 0.2)
    def compute_distances_and_pairs(self, pdb_file, nr_contacts=None, nr_noncontacts=None):
        #distance and contacts
        self.features['pair']['Cbdist'] = pdb.distance_map(pdb_file, self.L)

        #mask positions that have too many gaps
        gap_freq = 1 - (self.Ni / self.neff)
        highly_gapped_pos = np.where(gap_freq > self.max_gap_percentage)[0]
        self.features['pair']['Cbdist'][:,highly_gapped_pos] = np.nan
        self.features['pair']['Cbdist'][highly_gapped_pos, :] = np.nan

        #if there are unresolved residues, there will be nan in the distance_map
        with np.errstate(invalid='ignore'):
            self.features['pair']['contact'] = (self.features['pair']['Cbdist'] <= self.contact_threshold) * 1
            self.features['pair']['nocontact'] = (self.features['pair']['Cbdist'] > self.non_contact_threshold) * 1

        indices_contact = np.where(np.triu(self.features['pair']['contact'], k=self.seq_separation))
        indices_contact = tuple(shuffle(indices_contact[0],indices_contact[1], random_state=0))
        if nr_contacts:
            indices_contact = indices_contact[0][:nr_contacts], indices_contact[1][:nr_contacts]

        indices_nocontact = np.where(np.triu(self.features['pair']['nocontact'], k=self.seq_separation))
        indices_nocontact = tuple(shuffle(indices_nocontact[0],indices_nocontact[1], random_state=0))
        if nr_noncontacts:
            indices_nocontact = indices_nocontact[0][:nr_noncontacts], indices_nocontact[1][:nr_noncontacts]


        #update indices of i<j for only relevant pairs
        self.ij_ind_upper = np.array(list(indices_contact[0]) + list(indices_nocontact[0])), np.array(list(indices_contact[1]) + list(indices_nocontact[1]))
Beispiel #17
0
def test_tzrzf():
    """
    This test performs an RZ decomposition in which an m x n upper trapezoidal
    array M (m <= n) is factorized as M = [R 0] * Z where R is upper triangular
    and Z is unitary.
    """
    seed(1234)
    m, n = 10, 15
    for ind, dtype in enumerate(DTYPES):
        tzrzf, tzrzf_lw = get_lapack_funcs(('tzrzf', 'tzrzf_lwork'),
                                           dtype=dtype)
        lwork = _compute_lwork(tzrzf_lw, m, n)

        if ind < 2:
            A = triu(rand(m, n).astype(dtype))
        else:
            A = triu((rand(m, n) + rand(m, n)*1j).astype(dtype))

        # assert wrong shape arg, f2py returns generic error
        assert_raises(Exception, tzrzf, A.T)
        rz, tau, info = tzrzf(A, lwork=lwork)
        # Check success
        assert_(info == 0)

        # Get Z manually for comparison
        R = np.hstack((rz[:, :m], np.zeros((m, n-m), dtype=dtype)))
        V = np.hstack((np.eye(m, dtype=dtype), rz[:, m:]))
        Id = np.eye(n, dtype=dtype)
        ref = [Id-tau[x]*V[[x], :].T.dot(V[[x], :].conj()) for x in range(m)]
        Z = reduce(np.dot, ref)
        assert_allclose(R.dot(Z) - A, zeros_like(A, dtype=dtype),
                        atol=10*np.spacing(dtype(1.0).real), rtol=0.)
Beispiel #18
0
    def __init__(self, endmembers, alphas, energy_interaction, volume_interaction=None, entropy_interaction=None):

        self.n_endmembers = len(endmembers)

        # Create array of van Laar parameters
        self.alphas = np.array(alphas)

        # Create 2D arrays of interaction parameters
        self.We = np.triu(2. / (self.alphas[:, np.newaxis] + self.alphas), 1)
        self.We[np.triu_indices(self.n_endmembers, 1)] *= np.array([i for row in energy_interaction
                                                                for i in row])

        if entropy_interaction is not None:
            self.Ws = np.triu(2. / (self.alphas[:, np.newaxis] + self.alphas), 1)
            self.Ws[np.triu_indices(self.n_endmembers, 1)] *= np.array([i for row in entropy_interaction
                                                                        for i in row])
        else:
            self.Ws = np.zeros((self.n_endmembers, self.n_endmembers))

        if volume_interaction is not None:
            self.Wv = np.triu(2. / (self.alphas[:, np.newaxis] + self.alphas), 1)
            self.Wv[np.triu_indices(self.n_endmembers, 1)] *= np.array([i for row in volume_interaction
                                                                        for i in row])
        else:
            self.Wv = np.zeros((self.n_endmembers, self.n_endmembers))


        # initialize ideal solution model
        IdealSolution.__init__(self, endmembers)
Beispiel #19
0
    def updatePsi(self):
        self.PsicXc = np.zeros((self.nc,self.nc), dtype=np.float)
        self.PsicXe = np.zeros((self.ne,self.ne), dtype=np.float)
        self.PsicXcXe = np.zeros((self.nc,self.ne), dtype=np.float)
        #
        # print self.thetac
        # print self.pc
        # print self.distanceXc
        newPsicXc = np.exp(-np.sum(self.thetac*np.power(self.distanceXc,self.pc), axis=2))
        print newPsicXc[0]
        self.PsicXc = np.triu(newPsicXc,1)
        self.PsicXc = self.PsicXc + self.PsicXc.T + np.mat(eye(self.nc))+np.multiply(np.mat(eye(self.nc)),np.spacing(1))
        self.UPsicXc = np.linalg.cholesky(self.PsicXc)
        self.UPsicXc = self.UPsicXc.T
        print self.PsicXc[0]
        print self.UPsicXc
        exit()

        newPsicXe = np.exp(-np.sum(self.thetac*np.power(self.distanceXe,self.pc), axis=2))
        self.PsicXe = np.triu(newPsicXe,1)
        self.PsiXe = self.PsicXe + self.PsicXe.T + np.mat(eye(self.ne))+np.multiply(np.mat(eye(self.ne)),np.spacing(1))
        self.UPsicXe = np.linalg.cholesky(self.PsicXe)
        self.UPsicXe = self.UPsicXe.T

        newPsiXeXc = np.exp(-np.sum(self.thetad*np.power(self.distanceXcXe,self.pd), axis=2))
        self.PsicXcXe = np.triu(newPsiXeXc,1)
    def __call__(self, container, sim_wide_params):
        distance_matrices = container.get_distance_matrices()
        # LJ
        accelerations, pe = moldyn.lennardJonesForce(
                distance_matrices,
                radius=self.radius,
                epsilon=self.epsilon,
                cutoff_dist=self.cutoff_dist,
                anchor_ixs = sim_wide_params.anchor_ixs)
        self.pe = pe  # should LJ func calc this differently since we are doing a cutoff?
        # Damping
        dx, dy, dr = distance_matrices  # KLUDGE
        ixs_not_touching = dr > self.cutoff_dist
        self.ixs_not_touching = ixs_not_touching
        dvx, dvy, dvr = moldyn.get_distance_matrices(container.velocities)
##        if np.any(container.velocities > 1):
##            pass
##        if container.time > 1:
##            pass
        dr[dr == 0] = 0.1  # prevent / 0 errors. Will this cause problems?
        accel = self.damping_magnitude * (dvx*dx + dvy*dy)/dr
        accel[ixs_not_touching] = 0
        accelerations[:, 0] += np.sum(np.triu(accel * dx/dr), axis=0)
        accelerations[:, 1] += np.sum(np.triu(accel * dy/dr), axis=0)
        return accelerations
Beispiel #21
0
Datei: BP.py Projekt: guyz/fe
def build_binomial_price_tree(s0, u, d, n):
    '''
    Builds the underlying's price tree by working forward from
    t=0 to expiration (t=n) --> total of n+1 time slots.
    The price tree is an upper triangular 2D array, where each column
    marks the different possible security prices S(t).
    
    arguments:
    - s0: initial stock price
    - u: upward movement interest rate
    - d: downward movement interest rate
    - n: number of periods
    '''
    
    a = np.arange(0,n+1)
    A = np.tile(a,(n+1,1))
    C = np.triu(np.ones( (n+1,n+1) ))

    # U_e is the matrix of all exponents pertaining to up movements.
    # e.g: the first row is 0, 1, 2, counting (and multiplying)
    # by the amount of times we moved 'up'.
    U_e = np.triu(A-A.T)
    # base matrix
    U_b = u*C
    
    # Similarily - down direction.
    D_e = np.triu(A.T)
    D_b = d*C
    
    # Up/Down binomial distribution matrices
    U = np.power(U_b, U_e)
    D = np.power(D_b, D_e)
    
    return np.triu( s0 * ( U*D ) )
Beispiel #22
0
def _flat2D(fld, center='Atlantic'):
    """convert mds 2D data into global 2D field"""

    nx = fld.shape[1]
    ny = fld.shape[0]
    n = ny//nx//4
    
    # eastern and western hemispheres
    eastern=np.concatenate((fld[:n*nx,:],fld[n*nx:2*(n*nx)]),axis=1)
    tmp    = fld[2*(n*nx)+nx:,        ::-1]
    western=np.concatenate((tmp[2::n,:].transpose(),
                            tmp[1::n,:].transpose(),
                            tmp[0::n,:].transpose()))
    # Arctic face is special
    arctic  = fld[2*(n*nx):2*(n*nx)+nx,:]
    arctice = np.concatenate((np.triu(arctic[::-1,:nx//2].transpose()),
                              np.zeros((nx//2,nx))),axis=1)
    # arcticw = np.concatenate((arctic[:,nx:nx//2-1:-1].transpose(),
    #                           np.zeros((nx//2,nx//2)),
    #                           arctic[nx:nx//2-1:-1,nx//2-1::-1]),axis=1)
    mskr = np.tri(nx//2)[::-1,:]
    arcticw = np.concatenate((arctic[0:nx//2,nx:nx//2-1:-1].transpose(),
                              arctic[nx//2:nx,nx:nx//2-1:-1].transpose()*mskr,
                              np.triu(arctic[nx:nx//2-1:-1,nx:nx//2-1:-1]),
                              arctic[nx:nx//2-1:-1,nx//2-1::-1]*mskr),axis=1)
    #
    if center == 'Pacific':
        gfld = np.concatenate( ( np.concatenate((eastern,arctice)),
                                 np.concatenate((western,arcticw)) ), axis=1)
    else:
        gfld = np.concatenate( ( np.concatenate((western,arcticw)),
                                 np.concatenate((eastern,arctice)) ), axis=1)
    
    return gfld
  def __call__(self,x,v=0,t=0,calc_auxilary=True):
    distance_matrix = self.distance_matrix
    sigma,eps,m,r_tol = self.sigma,self.eps,self._masses,self.r_tol
    dx = distance_matrix(x)		# compute distance matrix THIS IS THE MAIN TIME COMPUTATION
    r = distance_matrix.radii(dx)
    bad_diagonal = isnan(r)
    if (abs(r) < r_tol).any():		# This is some exception handling for forces that get out of control
      idx = find(abs(r) < r_tol)
      err_string = "Two points within 1e-8 of eachother r = \n"
      err_string += str(r) + "\n"
      err_string += "\n r{} = \n".format(array(unravel_index(idx,r.shape)))
      for x in r.ravel()[idx]:
	err_string += "{:.12f} \n".format(float(x))
#      raise ValueError(err_string)
    K2 = (sigma/r)**6		      # Save these constants for calculating potential energy
    K1 = K2**2 
    fmatrix = 24*eps/r*(2*K1 - K2)*dx.transpose(2,0,1)   
    K1[bad_diagonal] = 0	      # Set diagonal back to zero.  
    K2[bad_diagonal] = 0	      # Set diagonal back to zero.  
    fmatrix[:,bad_diagonal] = 0	      # Set diagonal back to zero.  
    if calc_auxilary:
      self.potential_energy = sum(triu(4*eps*(K1 - K2))) # Calculate potential energy
      self.kinetic_energy = 3/2*sum(self._masses*sum(v**2,axis=1),axis=0)/2.
      N = dx.shape[0]
      d = dx.shape[2] 	     
      L = self.L[abs(self.L)>0]
      self.pressure = 0
      if N > 1:
	press_matrix = sum(fmatrix*dx.transpose(2,0,1),axis=0) # this is the "dot product"
	self.pressure = (sum(triu(press_matrix)) + N/(N-1)*self.kinetic_energy*2.)/d/prod(L)
    ans = sum(fmatrix,axis=1)/m		      # Divide by masses to get acceleration
    return ans.T			       
Beispiel #24
0
def verifyloglikelihood(theta_vector, graphe, constantes, silent=False):
    """On calcule somme (aij * logpij + (1-aij)* log(1-pij))"""
    # aij
    a = (nx.to_numpy_matrix(graphe)).A

    # pij
    kappa0, gamma, beta, Nobs, k_bar = constantes
    mu = beta * math.sin(math.pi / beta) / (2 * math.pi * k_bar)
    kappa = {node: max(kappa0, degree - gamma / beta) for node, degree in graphe.degree().items()}
    kp = np.outer(np.ones(Nobs), kappa.values())
    kp2 = np.outer(kappa.values(), np.ones(Nobs))
    mukpkp2 = 2 * math.pi * mu * kp * kp2
    th = np.outer(np.ones(Nobs), theta_vector)
    th2 = np.outer(theta_vector, np.ones(Nobs))
    dth = math.pi - np.abs(math.pi - np.abs(th - th2))
    khi = Nobs * dth / mukpkp2
    p = 1 / (1 + khi ** beta)

    # log(pij)
    lp = np.triu(np.log(p), k=1)
    l_p = np.triu(np.log(1 - p), k=1)

    # aij*log(pij)
    lp[a == 0] = 0
    l_p[a == 1] = 0
    if not silent: print np.sum(lp + l_p), np.sum(lp), np.sum(l_p)
    return np.sum(lp + l_p)
Beispiel #25
0
    def pstep(self, level=0):
        """Propagates the momenta for half a time step."""

        dt = self.pdt[level]
        dt2 = dt**2
        dt3 = dt**3 / 3.0
        m = dstrip(self.beads.m3)[0].reshape(self.beads.natoms, 3)

        hh0 = np.dot(self.cell.h, self.h0.ih)
        pi_ext = np.dot(hh0, np.dot(self.stressext, hh0.T)) * self.h0.V / self.cell.V
        L = np.diag([3, 2, 1])

        stress = dstrip(self.stress_mts(level))
        self.p += dt * (self.cell.V * np.triu(stress))

        # integerates the kinetic part of the stress with the force at the inner-most level.
        if(level == self.nmtslevels - 1):

            self.p += dt * (self.cell.V * np.triu(-self.beads.nbeads * pi_ext) + Constants.kb * self.temp * L)

            pc = dstrip(self.beads.pc).reshape(self.beads.natoms, 3)
            fc = np.sum(dstrip(self.forces.forces_mts(level)), axis=0).reshape(self.beads.natoms, 3) / self.beads.nbeads
            fcTonm = (fc / dstrip(self.beads.m3)[0].reshape(self.beads.natoms, 3)).T

            self.p += np.triu(dt2 * np.dot(fcTonm, pc) + dt3 * np.dot(fcTonm, fc)) * self.beads.nbeads
def calculate2_pseudoV(pred, truth, rnd=0.01, full_matrix=True, sym=False):
    if full_matrix:
        pred_cp = pred
        truth_cp = truth
    else: # make matrix upper triangular
        pred_cp = np.triu(pred)
        truth_cp = np.triu(truth)

    # Avoid dividing by zero by rounding everything less than rnd up to rnd
    # Note: it is ok to do this after making the matrix upper triangular
    # since the bottom triangle of the matrix will not affect the score

    size = np.array(pred_cp.shape)[1]
    res = 0 # result to be returned

    # do one row at a time to reduce memory usage
    for x in xrange(size):
        # (1 - rnd) will cast the pred_cp/truth_cp matrices automatically if they are int8
        pred_row = (1 - rnd) * pred_cp[x, ] + rnd
        truth_row = (1 - rnd) * truth_cp[x, ] + rnd

        pred_row /= np.sum(pred_row)
        truth_row /= np.sum(truth_row)
        if sym:
            res += np.sum(truth_row * np.log(truth_row/pred_row)) + np.sum(pred_row * np.log(pred_row/truth_row))
        else:
            res += np.sum(truth_row * np.log(truth_row/pred_row))
    return res
Beispiel #27
0
    def test_dynamic_programming_logic(self):
        # Test for the dynamic programming part
        # This test is directly taken from Cormen page 376.
        arrays = [np.random.random((30, 35)),
                  np.random.random((35, 15)),
                  np.random.random((15, 5)),
                  np.random.random((5, 10)),
                  np.random.random((10, 20)),
                  np.random.random((20, 25))]
        m_expected = np.array([[0., 15750., 7875., 9375., 11875., 15125.],
                               [0.,     0., 2625., 4375.,  7125., 10500.],
                               [0.,     0.,    0.,  750.,  2500.,  5375.],
                               [0.,     0.,    0.,    0.,  1000.,  3500.],
                               [0.,     0.,    0.,    0.,     0.,  5000.],
                               [0.,     0.,    0.,    0.,     0.,     0.]])
        s_expected = np.array([[0,  1,  1,  3,  3,  3],
                               [0,  0,  2,  3,  3,  3],
                               [0,  0,  0,  3,  3,  3],
                               [0,  0,  0,  0,  4,  5],
                               [0,  0,  0,  0,  0,  5],
                               [0,  0,  0,  0,  0,  0]], dtype=np.int)
        s_expected -= 1  # Cormen uses 1-based index, python does not.

        s, m = _multi_dot_matrix_chain_order(arrays, return_costs=True)

        # Only the upper triangular part (without the diagonal) is interesting.
        assert_almost_equal(np.triu(s[:-1, 1:]),
                            np.triu(s_expected[:-1, 1:]))
        assert_almost_equal(np.triu(m), np.triu(m_expected))
def vRaman(x,omega=1.0,delta=0.0,epsilon=0.048,phi=4.0/3.0):
    x=np.array(x)    
    s=1
    v=v=np.einsum('i,jk->ijk',omega*np.exp(1.0j*2.0*phi*x),Fx(s)*np.sqrt(2.0)/2.0)
    v=np.array([np.triu(v[i])+np.conjugate(np.triu(v[i],1)).transpose() for i in range(x.size)])
    v+=np.array([np.diag([epsilon+delta,0.0,epsilon-delta])]*x.size)
    return v
	def __init__(self, num_visibles, num_hiddens):
		"""
		Initializes the parameters of the SemiRBM.

		@type  num_visibles: integer
		@param num_visibles: number of visible units
		@type  num_hiddens:  integer
		@param num_hiddens:  number of hidden units
		"""

		AbstractBM.__init__(self, num_visibles, num_hiddens)

		# additional hyperparameters
		self.learning_rate_lateral = 0.01
		self.momentum_lateral = 0.5
		self.weight_decay_lateral = 0.

		self.damping = 0.2
		self.num_lateral_updates = 20
		self.sampling_method = AbstractBM.MF

		# additional parameters
		self.L = np.matrix(np.random.randn(num_visibles, num_visibles)) / num_visibles / 200
		self.L = np.triu(self.L) + np.triu(self.L).T - 2. * np.diag(np.diag(self.L))
		self.dL = np.zeros_like(self.L)
Beispiel #30
0
 def _getImageDirection(self, threshold=None):
     """
     _getImageDirection(threshold=None)
     
     Returns the direction of the sequence of avalanches as: 
     "Top_to_bottom","Left_to_right", "Bottom_to_top","Right_to_left"
     
     Parameters:
     ----------------
     threshold : int
         Minimum value of the gray level change to conisider
         a pixel as part of an avalanche (i.e. it is switched)
     """
     # Top, left, bottom, rigth
     imageDirections=["Top_to_bottom","Left_to_right", "Bottom_to_top","Right_to_left"]
     # Check if color Image is available
     if not self._isColorImage:
         self._isColorImageDone(ask=False)        
     switchTimesMasked = self._switchTimes2D
     pixelsUnderMasks = []
     # first identify first 10 avalanches of whole image
     firstAvsList = np.unique(self._switchTimes2D)[:11]
     # Prepare the mask
     m = np.ones((self.dimX, self.dimY))
     # Top mask
     mask = np.rot90(np.triu(m)) * np.triu(m)
     top = switchTimesMasked * mask
     pixelsUnderMasks.append(sum([np.sum(top==elem) for elem in firstAvsList]))
     # Now we need to rotate the mask
     for i in range(3):
         mask = np.rot90(mask)
         top = switchTimesMasked * mask
         pixelsUnderMasks.append(sum([np.sum(top==elem) for elem in firstAvsList]))
     max_in_mask = scipy.array(pixelsUnderMasks).argmax()
     return imageDirections[max_in_mask]
Beispiel #31
0
        xmean = np.clip(t_xmean, -0.8, 0.8)

        t_zmean = np.zeros(nVar, )
        for k in range(mu):
            t_zmean += arz[:, arindex[k]] * w[k]
        t_zmean = t_zmean / mu

        zmean = t_zmean

        ps = (1 - cs) * ps + (np.sqrt(cs *
                                      (2 - cs)) * mueff) * np.matmul(B, zmean)
        hsig = (np.linalg.norm(ps) / np.sqrt(1 -
                                             (1 - cs)**(2 * counteval / la)) /
                chiN) < (1.4 + (2 / (nVar + 1)))

        pc = (1 - cc) * pc + hsig * np.sqrt(
            cc * (2 - cc) * mueff) * (np.matmul(B, np.matmul(D, zmean)))

        C = (1 - c1 - cmu)*C + c1*(np.matmul(pc, np.transpose(pc)) + (1 + hsig)*cc*(2-cc)*C) + \
            cmu*np.matmul((np.matmul(B, np.matmul(D, arz[:,arindex[0:mu]]))) ,  \
            np.matmul(np.diag(w),np.transpose((np.matmul(B, np.matmul(D, arz[:,arindex[0:mu]]))))))

        sigma = sigma * np.exp((cs / damps) * (np.linalg.norm(ps) / chiN - 1))

        if counteval - eigeneval > la / (c1 + cmu) / nVar / 10:
            eigeneval = counteval
            C = np.triu(C) + np.transpose(np.triu(C, 1))
            D, B = np.linalg.eig(C)
            D = np.diag(np.sqrt(D))
Beispiel #32
0
def get_attn_subsequent_mask(seq):
    attn_shape = [seq.size(0), seq.size(1), seq.size(1)]
    subsequent_mask = np.triu(np.ones(attn_shape), k=1)
    subsequent_mask = torch.from_numpy(subsequent_mask).byte()
    return subsequent_mask
def generate_visualizations(model,
                            config,
                            voc,
                            src_str='((()))(())()',
                            run_name='SAN',
                            iteration=0,
                            score=1.0,
                            device='cuda:1'):

    dlang = DyckLanguage(1, 0.5, 0.25)

    #Convert source string to ids tensor
    src = sents_to_idx(voc, [src_str]).transpose(0, 1).to(device)[:-1]

    #Create directory to save visualizations
    dir_path = os.path.join("Figures", run_name)
    if os.path.exists(dir_path) == False:
        os.mkdir(dir_path)

    # Ploting attention weights
    def visualize_attn(src, src_str):
        output, attn = model.model(src, get_attns=True)
        src_len = len(src_str)
        attn_maps = []
        attn_map = attn[0][0, :src_len, :src_len].detach().cpu().numpy()
        for i in range(config.depth):
            for j in range(config.heads):
                attn_map = attn[i][
                    0, j, :src_len, :src_len].detach().cpu().numpy()
                plt.figure(figsize=(15, 10))
                sns.set(font_scale=1.5)
                g = sns.heatmap(np.log(attn_map),
                                mask=(attn_map == 0).astype(float),
                                annot=attn_map,
                                cmap=sns.cubehelix_palette(100,
                                                           start=0.7,
                                                           rot=-0.5,
                                                           gamma=1.5),
                                vmin=-2.6,
                                vmax=0,
                                cbar=False,
                                xticklabels=list(src_str),
                                yticklabels=list(src_str),
                                linewidths=.5)  #cmap="YlGnBu")
                yticks = g.set_yticklabels(labels=list(src_str),
                                           rotation=360,
                                           size=30)
                xticks = g.set_xticklabels(labels=list(src_str), size=30)
                plt.title(
                    'Attention Weights Layer: {} Head: {} (It: {}, Score: {})'.
                    format(i + 1, j + 1, iteration, np.round(score, 3)),
                    size=20)
                fig = g.get_figure()
                fig.savefig(os.path.join(
                    dir_path,
                    'attn_weights_depth-{}_heads-{}_it-{}.png'.format(
                        i + 1, j + 1, iteration)),
                            bbox_inches='tight')
                attn_maps.append(attn_map)
        return attn_maps

    attn_maps = visualize_attn(src, src_str)

    # Computing and Ploting Intermediate representations

    # Obtaining Embeddings
    embeddings = model.model.encoder(src) * np.sqrt(3)
    embeddings_np = embeddings.detach().cpu().numpy().squeeze()
    #Plotting Embeddings
    plt.figure(figsize=(10, 3))
    embed_unq = embeddings_np[[0, 3]]
    g = sns.heatmap(embed_unq,
                    annot=embed_unq,
                    cmap=sns.color_palette("coolwarm", 7),
                    linewidth=1.5,
                    linecolor='black',
                    yticklabels=['(', ')'])
    g.set_title('Embeddings (It: {}, Score: {})'.format(
        iteration, np.round(score)))
    fig = g.get_figure()
    fig.savefig(os.path.join(dir_path,
                             'embeddings_it-{}.png'.format(iteration)),
                bbox_inches='tight')

    # Computing queries, keys and values
    kqv = list(model.model.transformer_encoder.parameters())[0].detach()
    b = list(model.model.transformer_encoder.parameters())[1].detach()
    query_matrix, query_bias = kqv[:config.d_model], b[:config.d_model]
    key_matrix, key_bias = kqv[config.d_model:config.d_model *
                               2], b[config.d_model:config.d_model * 2]
    value_matrix, value_bias = kqv[config.d_model * 2:], b[config.d_model * 2:]
    kqv_vectors = torch.mm(embeddings.squeeze(), kqv.transpose(0, 1)) + b
    queries, keys, values = kqv_vectors[:, :config.
                                        d_model], kqv_vectors[:,
                                                              config.d_model:
                                                              config.d_model *
                                                              2], kqv_vectors[:,
                                                                              config
                                                                              .
                                                                              d_model
                                                                              *
                                                                              2:]
    # Plotting Query Matrix
    sns.set(font_scale=1.2)
    query_matrix_np = query_matrix.detach().cpu().numpy().squeeze()
    query_bias_np = query_bias.detach().cpu().numpy().squeeze()[:, None]
    f, (ax1, ax2,
        ax3) = plt.subplots(1,
                            3,
                            sharey=False,
                            gridspec_kw={'width_ratios': [6, 2, 0.8]},
                            figsize=(10, 5))
    #f.tight_layout()
    g1 = sns.heatmap(query_matrix_np,
                     annot=query_matrix_np.round(3),
                     cmap=sns.color_palette("coolwarm", 7),
                     linewidth=1.5,
                     linecolor='black',
                     ax=ax1,
                     cbar=False)
    g1.set_title('Query Matrix')

    g2 = sns.heatmap(query_bias_np,
                     annot=query_bias_np.round(3),
                     cmap=sns.color_palette("coolwarm", 7),
                     linewidth=1.5,
                     linecolor='black',
                     ax=ax2,
                     cbar_ax=ax3)
    g2.set_title('Query Bias')
    f.suptitle("Iteration: {}, Score: {}".format(iteration, np.round(score,
                                                                     3)))
    f.subplots_adjust(top=0.8)
    f.savefig(os.path.join(dir_path, 'query_wb_it-{}.png'.format(iteration)),
              bbox_inches='tight')
    # Plotting Key Matrix
    sns.set(font_scale=1.2)
    key_matrix_np = key_matrix.detach().cpu().numpy().squeeze()
    key_bias_np = key_bias.detach().cpu().numpy().squeeze()[:, None]
    #plt.figure(figsize = (10, 10))
    f, (ax1, ax2,
        ax3) = plt.subplots(1,
                            3,
                            sharey=False,
                            gridspec_kw={'width_ratios': [6, 2, 0.8]},
                            figsize=(10, 5))
    #f.tight_layout()
    g1 = sns.heatmap(key_matrix_np,
                     annot=key_matrix_np.round(3),
                     cmap=sns.color_palette("coolwarm", 7),
                     linewidth=1.5,
                     linecolor='black',
                     ax=ax1,
                     cbar=False)
    g1.set_title('Key Matrix')

    g2 = sns.heatmap(key_bias_np,
                     annot=key_bias_np.round(3),
                     cmap=sns.color_palette("coolwarm", 7),
                     linewidth=1.5,
                     linecolor='black',
                     ax=ax2,
                     cbar_ax=ax3)
    g2.set_title('Key Bias')
    f.suptitle("Iteration: {}, Score: {}".format(iteration, np.round(score,
                                                                     3)))
    f.subplots_adjust(top=0.8)
    f.savefig(os.path.join(dir_path, 'key_wb_it-{}.png'.format(iteration)),
              bbox_inches='tight')
    # Ploting Value Matrix
    sns.set(font_scale=1.2)
    value_matrix_np = value_matrix.detach().cpu().numpy().squeeze()
    value_bias_np = value_bias.detach().cpu().numpy().squeeze()[:, None]
    #plt.figure(figsize = (10, 10))
    f, (ax1, ax2,
        ax3) = plt.subplots(1,
                            3,
                            sharey=False,
                            gridspec_kw={'width_ratios': [6, 2, 0.8]},
                            figsize=(10, 5))
    #f.tight_layout()
    g1 = sns.heatmap(value_matrix_np,
                     annot=value_matrix_np.round(3),
                     cmap=sns.color_palette("coolwarm", 7),
                     linewidth=1.5,
                     linecolor='black',
                     ax=ax1,
                     cbar=False)
    g1.set_title('Value Matrix')

    g2 = sns.heatmap(value_bias_np,
                     annot=value_bias_np.round(3),
                     cmap=sns.color_palette("coolwarm", 7),
                     linewidth=1.5,
                     linecolor='black',
                     ax=ax2,
                     cbar_ax=ax3)
    g2.set_title('Value Bias')
    f.suptitle("Iteration: {}, Score: {}".format(iteration, np.round(score,
                                                                     3)))
    f.subplots_adjust(top=0.8)
    f.savefig(os.path.join(dir_path, 'value_wb_it-{}.png'.format(iteration)),
              bbox_inches='tight')
    #Plotting value vectors
    plt.figure(figsize=(10, 3))
    values_np = values.detach().cpu().numpy().squeeze()
    values_unq = values_np[[0, 3]]
    #pdb.set_trace()
    g = sns.heatmap(values_unq,
                    annot=values_unq,
                    cmap=sns.color_palette("coolwarm", 7),
                    linewidth=1.5,
                    linecolor='black',
                    yticklabels=['(', ')'])
    g.set_title('Values')
    fig = g.get_figure()
    fig.suptitle("Iteration: {}, Score: {}".format(iteration,
                                                   np.round(score, 3)))
    fig.subplots_adjust(top=0.8)
    fig.savefig(os.path.join(dir_path, 'values_it-{}.png'.format(iteration)),
                bbox_inches='tight')

    # Computing Attention Map
    n = len(queries)
    mask = torch.tensor(np.triu(np.ones((n, n))).T).float().to(device)
    scores = torch.mm(queries, keys.T) / (np.sqrt(3))
    scores = scores * mask + (-1e9) * (1 - mask)
    attn_map = nn.functional.softmax(scores, dim=-1)
    assert np.allclose(attn_map.detach().cpu().numpy(), attn_maps[0])

    # Computing attention outputs
    attn_outs = torch.mm(attn_map.float(), values.float())
    # Ploting attention outputs
    seq = src_str
    depths = dlang.depth_counter(seq).squeeze()
    lens = np.array([i + 1 for i in range(len(seq))])
    dlratio = depths / lens
    sns.set(font_scale=3, style='ticks', rc={"lines.linewidth": 4})
    src_chars = src_str
    src_charsv0 = list(src_chars)
    src_chars = ['{}_{}'.format(ch, i) for i, ch in enumerate(src_chars)]
    attn_values = attn_outs.detach().cpu().numpy()
    data = pd.DataFrame([src_chars, attn_values]).transpose()
    data.columns = ['dyck', '0-Element']
    fig = plt.figure(figsize=(25, 10))
    plt.plot(src_chars,
             attn_values[:, 0],
             marker='o',
             label='Coordinate-0',
             markersize=12,
             color='r')
    plt.plot(src_chars,
             attn_values[:, 1],
             marker='D',
             label='Coordinate-1',
             markersize=12,
             color='m')
    plt.plot(src_chars,
             attn_values[:, 2],
             marker='v',
             label='Coordinate-2',
             markersize=12,
             color='g')
    plt.plot(src_chars,
             dlratio,
             '--',
             marker='s',
             markersize=12,
             color='c',
             label='Depth : Length')
    plt.legend(loc="upper right")
    plt.title("Output of Self-Attention Block (It: {}, Score: {})".format(
        iteration, np.round(score, 3)))
    plt.grid()
    plt.rc('grid', linestyle="-", color='black')
    plt.savefig(os.path.join(dir_path, 'attn_outs-{}.png'.format(iteration)),
                bbox_inches='tight')

    # Computing outputs on applying a linear layer on attention outputs
    attn_ffn_w = list(model.model.transformer_encoder.parameters())[2].detach()
    attn_ffn_b = list(model.model.transformer_encoder.parameters())[3].detach()
    attn_ffn = torch.mm(attn_outs, attn_ffn_w.transpose(0, 1)) + attn_ffn_b
    sns.set(font_scale=1.2)
    attn_ffn_w_np = attn_ffn_w.detach().cpu().numpy().squeeze()
    attn_ffn_b_np = attn_ffn_b.detach().cpu().numpy().squeeze()[:, None]
    #plt.figure(figsize = (10, 10))
    f, (ax1, ax2,
        ax3) = plt.subplots(1,
                            3,
                            sharey=False,
                            gridspec_kw={'width_ratios': [6, 2, 0.8]},
                            figsize=(10, 5))
    f.tight_layout()
    g1 = sns.heatmap(attn_ffn_w_np,
                     annot=attn_ffn_w_np.round(3),
                     cmap=sns.color_palette("coolwarm", 7),
                     linewidth=1.5,
                     linecolor='black',
                     ax=ax1,
                     cbar=False)
    g1.set_title('Attn-FFN Matrix')

    g2 = sns.heatmap(attn_ffn_b_np,
                     annot=attn_ffn_b_np.round(3),
                     cmap=sns.color_palette("coolwarm", 7),
                     linewidth=1.5,
                     linecolor='black',
                     ax=ax2,
                     cbar_ax=ax3)
    g2.set_title('Attn-FFN Bias')
    f.suptitle("Iteration: {}, Score: {}".format(iteration, np.round(score,
                                                                     3)))
    f.subplots_adjust(top=0.8)
    f.savefig(os.path.join(dir_path, 'attnffn_wb_it-{}.png'.format(iteration)),
              bbox_inches='tight')

    # Last few important layers
    ln1 = model.model.transformer_encoder.layers[0].norm1
    ln2 = model.model.transformer_encoder.layers[0].norm2
    linear1 = model.model.transformer_encoder.layers[0].linear1
    linear2 = model.model.transformer_encoder.layers[0].linear2
    try:
        activation = model.model.transformer_encoder.layers[0].activation
    except:
        activation = F.relu
    # Feeding attn_ffn obtained in the last cell to residual and layer norm layers
    res_out = embeddings.squeeze() + attn_ffn
    res_ln_out = ln1(res_out)

    # Applying a feed forward network (d_model -> d_ffn -> d_model) to the resulting output from last set
    pos_ffn = linear2(activation(linear1(res_ln_out)))

    # Applying residual + layer norm to the vectors obtained from last step
    res_out2 = (res_ln_out + pos_ffn)
    res_ln_out2 = ln2(res_out2)

    pos_ffn1_w = list(linear1.parameters())[0]
    pos_ffn1_b = list(linear1.parameters())[1]

    #Plotting Pos_FFN-1 Weights
    sns.set(font_scale=1.2)
    pos_ffn1_w_np = pos_ffn1_w.detach().cpu().numpy().squeeze()
    pos_ffn1_b_np = pos_ffn1_b.detach().cpu().numpy().squeeze()[:, None]
    #plt.figure(figsize = (10, 10))
    f, (ax1, ax2,
        ax3) = plt.subplots(1,
                            3,
                            sharey=False,
                            gridspec_kw={'width_ratios': [6, 2, 0.4]},
                            figsize=(10, 5))
    #f.tight_layout()
    g1 = sns.heatmap(pos_ffn1_w_np,
                     annot=pos_ffn1_w_np.round(3),
                     cmap=sns.color_palette("coolwarm", 7),
                     linewidth=1.5,
                     linecolor='black',
                     ax=ax1,
                     cbar=False)
    g1.set_title('Pos-FFN Layer-1 Matrix')

    g2 = sns.heatmap(pos_ffn1_b_np,
                     annot=pos_ffn1_b_np.round(3),
                     cmap=sns.color_palette("coolwarm", 7),
                     linewidth=1.5,
                     linecolor='black',
                     ax=ax2,
                     cbar_ax=ax3)
    g2.set_title('Pos-FFN Layer-1 Bias')
    f.suptitle("Iteration: {}, Score: {}".format(iteration, np.round(score,
                                                                     3)))
    f.subplots_adjust(top=0.8)
    f.savefig(os.path.join(dir_path, 'posffn1_wb_it-{}.png'.format(iteration)),
              bbox_inches='tight')

    pos_ffn2_w = list(linear2.parameters())[0]
    pos_ffn2_b = list(linear2.parameters())[1]

    #Plotting Pos_FFN Weights
    sns.set(font_scale=1.2)
    pos_ffn2_w_np = pos_ffn2_w.detach().cpu().numpy().squeeze()
    pos_ffn2_b_np = pos_ffn2_b.detach().cpu().numpy().squeeze()[:, None]
    #plt.figure(figsize = (10, 10))
    f, (ax1, ax2,
        ax3) = plt.subplots(1,
                            3,
                            sharey=False,
                            gridspec_kw={'width_ratios': [6, 2, 0.4]},
                            figsize=(10, 5))
    #f.tight_layout()
    g1 = sns.heatmap(pos_ffn2_w_np,
                     annot=pos_ffn2_w_np.round(3),
                     cmap=sns.color_palette("coolwarm", 7),
                     linewidth=1.5,
                     linecolor='black',
                     ax=ax1,
                     cbar=False)
    g1.set_title('Pos-FFN Layer-1 Matrix')

    g2 = sns.heatmap(pos_ffn2_b_np,
                     annot=pos_ffn2_b_np.round(3),
                     cmap=sns.color_palette("coolwarm", 7),
                     linewidth=1.5,
                     linecolor='black',
                     ax=ax2,
                     cbar_ax=ax3)
    g2.set_title('Pos-FFN Layer-2 Bias')
    f.suptitle("Iteration: {}, Score: {}".format(iteration, np.round(score,
                                                                     3)))
    f.subplots_adjust(top=0.8)
    f.savefig(os.path.join(dir_path, 'posffn2_wb_it-{}.png'.format(iteration)),
              bbox_inches='tight')

    # Feeding the encoder representations (obtained above) to the output linear layer (called decoder)
    decoder_weights = list(model.model.decoder.parameters())[0].detach()
    decoder_reps = torch.mm(res_ln_out2, decoder_weights.T)
    sns.set(font_scale=1.2)
    decoder_w_np = decoder_weights.detach().cpu().numpy().squeeze()
    plt.figure(figsize=(10, 5))
    #f,(ax1,ax2, ax3) = plt.subplots(1,3,sharey=False, gridspec_kw={'width_ratios':[6,2, 0.8]}, figsize = (10,5))
    #f.tight_layout()
    g1 = sns.heatmap(decoder_w_np,
                     annot=decoder_w_np.round(3),
                     cmap=sns.color_palette("coolwarm", 7),
                     linewidth=1.5,
                     linecolor='black',
                     cbar=True)
    g1.set_title('Decoder Weights (It: {}, Score: {})'.format(
        iteration, np.round(score, 3)))
    plt.savefig(os.path.join(dir_path,
                             'decoder_wb_it-{}.png'.format(iteration)),
                bbox_inches='tight')
    model.to(device)
Beispiel #34
0
def future_mask(seq_length):
    future_mask = np.triu(np.ones((seq_length, seq_length)),
                          k=1).astype('bool')
    return torch.from_numpy(future_mask)
Beispiel #35
0
def _complex_symrand(dim, dtype):
    a1, a2 = symrand(dim), symrand(dim)
    # add antisymmetric matrix as imag part
    a = a1 +1j*(triu(a2)-tril(a2))
    return a.astype(dtype)
def subsequent_mask(size):
    "Mask out subsequent positions."
    attn_shape = (1, size, size)
    subsequent_mask = np.triu(np.ones(attn_shape), k=1).astype('uint8')
    return torch.from_numpy(subsequent_mask) == 0
import numpy as np
import pytest

from tictactoe import is_winning_game


@pytest.mark.parametrize(
    'grid,expected',
    (
        (np.zeros((3, 3)), False),
        (np.triu(np.ones((3, 3)), 1) + np.tril(np.ones((3, 3)) * -1, -1), False),
        (np.ones((3, 3)), True),
        (np.ones((3, 3)) * -1, True),
        (np.eye(3), True),
        (np.eye(3) * -1, True),
        (np.flipud(np.eye(3)), True),
        (np.flipud(np.eye(3)) * -1, True)
    )
)
def test_is_winning_game(grid, expected):
    assert expected == is_winning_game(grid)
 def compute_score_full(self, L, tau):
     s = -abs(np.arange(0, L - 1)[:, None] / 2 -
              np.arange(L)[None, :]) / tau
     s = np.tril(s, 0) + np.triu(s - float("inf"), 1)
     s = np.exp(s - s.max(1, keepdims=True))
     return s / s.sum(1, keepdims=True)
Beispiel #39
0
Será que se eu estudar mais português vou ter um desempenho melhor nas outras matérias? (lembre-se que o ENEM é uma prova que demanda interpretação de texto, então essa prerrogativa pode fazer sentido).
Será que se eu considerar provas de anos anteriores e comparar as correlações com linguagem_códigos elas serão maiores?

A verdade é que uma simples análise de correlação nos gera diversas hipóteses. **Se tiver curiosidade e quiser fazer essas análises fica como um desafio extra!**

Tentamos plotar diversos gráficos para visualizar a correlação de uma melhor forma. Seguem os códigos:
"""

from string import ascii_letters
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

# Generate a mask for the upper triangle
mask = np.triu(np.ones_like(corr, dtype=np.bool))

# Set up the matplotlib figure
f, ax = plt.subplots(figsize=(11, 9))

# Generate a custom diverging colormap
cmap = sns.diverging_palette(220, 10, as_cmap=True)

# Draw the heatmap with the mask and correct aspect ratio
sns.heatmap(corr,
            mask=mask,
            cmap=cmap,
            vmax=.3,
            center=0,
            square=True,
            linewidths=.5,
Beispiel #40
0
# just so we can carry it forward later
res = np.load(f)
if 'subjs' in res.keys():
    subjs = np.load(f)['subjs']
    delme = np.load(f)['delme']
else:
    subjs, delme = None, None

ncomps = all_ICs[-1].shape[0]
# this makes all_ICs 2D, with all ncomps for nrep=1, then all ncomps for nrep=2, and etc
all_ICs = np.array(all_ICs).reshape(nreals * ncomps, -1)

# the cross-realization correlation matrix is nreals*ncomps by nreals*ncomps
print 'Computing correlations across all components'
crcm = np.corrcoef(all_ICs)
crcm_abs = np.abs(np.triu(crcm, k=1))

# determine threshold based on valley of CRCM histogram
u_idx = np.triu_indices(crcm.shape[0], k=1)
[bins, centers, patches] = pl.hist(np.abs(crcm)[u_idx], bins=nbins)
cc_threshold = .8  # centers[np.argmin(bins)]
print 'Correlation threshold: %.2f' % cc_threshold

# because we only pay attention at the magnitude of the connections, when we remove a given cell from consideration we mark it as -1. The lower part of the matrix has 0s, and it also has meaning behind it (low connection)
print 'Aligning components'
aligned_ics = []  # list of lists
for ic in range(ncomps):
    max_corr_idx = np.argmax(crcm_abs)
    idx = np.unravel_index(max_corr_idx, crcm_abs.shape)
    # like in the original RAICAR paper, we discover the realization and ICs
    re = [d / ncomps for d in idx]
Beispiel #41
0
 def reJ(self):
     return np.triu(self.J) + np.triu(self.J, k=1).T
def generate_twin_graphs(size=100, weight_range=1, connected_rate=0.2, noise_rate=0.2,
                                      edge_feature_dim=3):
     '''
     G is a template matrix for pattern.
     Generate two twin graphs G1, G2 from G for graph matching.
     (The numbers of nodes in G1 and G2 may be different from G)
     Randomly permute nodes and add some noise to edges and nodes.

     Both of edge weights and node attributes can be negative.

     size: number of nodes in G.
     weight_range: default:1, for scale
     '''
     # Create G
     # M = np.triu((np.random.rand(size,size)*2-1)*weight_range, 1) # upper tri matrix. Diag elements are 0.
     # Mi = np.triu(np.random.randint(edge_feature_dim, size=(size, size)),1) # M features type index
     connected_nodes = np.triu(np.random.rand(size,size) < connected_rate, 1).astype(int)
     # M_tmp = np.zeros([size, size, edge_feature_dim])
     M_tmp = {}
     for i in range(size):
         for j in range(size):
             if connected_nodes[i, j]:
                 # M_tmp[i, j, Mi[i, j]] = M[i, j]
                 edge_feature = np.random.rand(edge_feature_dim) * weight_range # single edge feature vector
                 M_tmp[(i, j)] = edge_feature
                 M_tmp[(j, i)] = edge_feature # make M_tmp symetric matrix
     M = M_tmp
     # M = M + np.transpose(M, axes=[1, 0, 2])
     M1 = dict()
     M2 = dict()
     M1_size = round((1+random.random()*noise_rate)*size)
     M2_size = round((1+random.random()*noise_rate)*size)

     idx1 = np.random.permutation(M1_size)
     idx2 = np.random.permutation(M2_size)

     for (i, j) in M.keys():
         M1[(idx1[i], idx1[j])] = M[(i, j)]
         M2[(idx2[i], idx2[j])] = M[(i, j)]
     # Generate the permutation of G1 and G2
     # M1 = np.triu((np.random.rand(M1_size,M1_size)*2-1)*weight_range, 1) # upper tri matrix. Diag elements are 0.
     # M1i = np.triu(np.random.randint(edge_feature_dim, size=(M1_size, M1_size)),1) # M1 features type index
     connected_nodes_m1 = np.triu(np.random.rand(M1_size,M1_size) < connected_rate, 1).astype(int)
     connected_nodes_m1[:size, :size] = connected_nodes
     # M1_tmp = np.zeros([size, size, edge_feature_dim])
     for i in range(M1_size):
         for j in range(M1_size):
             if connected_nodes_m1[i, j] and (i, j) not in M1.keys():
                 # M1_tmp[i, j, M1i[i, j]] = M1[i, j]
                 edge_feature = np.random.rand(edge_feature_dim) * weight_range # single edge feature vector
                 M1[(i, j)] = edge_feature
                 M1[(j, i)] = edge_feature # make M_tmp symetric matrix
     # M1 = M1 + np.transpose(M1, axes=[1, 0, 2])

     # M2 = np.triu((np.random.rand(M2_size,M2_size)*2-1)*weight_range, 1) # upper tri matrix. Diag elements are 0.
     # M2i = np.triu(np.random.randint(edge_feature_dim, size=(M2_size, M2_size)),1) # M2 features type index
     connected_nodes_m2 = np.triu(np.random.rand(M2_size,M2_size) < connected_rate, 1).astype(int)
     connected_nodes_m2[:size, :size] = connected_nodes
     # M2_tmp = np.zeros([size, size, edge_feature_dim])
     for i in range(M2_size):
         for j in range(M2_size):
             if connected_nodes_m2[i, j] and (i, j) not in M2.keys():
                 # M2_tmp[i, j, M2i[i, j]] = M2[i, j]
                 edge_feature = np.random.rand(edge_feature_dim) * weight_range # single edge feature vector
                 M2[(i, j)] = edge_feature
                 M2[(j, i)] = edge_feature # make M_tmp symetric matrix
     # M2 = M2 + np.transpose(M2, axes=[1, 0, 2])


     # node attributes
     V = (np.random.rand(size)*2-1)*weight_range
     V1 = (np.random.rand(M1_size)*2-1)*weight_range
     V2 = (np.random.rand(M2_size)*2-1)*weight_range
     V1[:size] = V
     V2[:size] = V

     # Generate random permutation matrix

     V1 = V1[idx1]
     V2 = V2[idx2]


     #The original method for generating noises.
     # Adding noise
     # Adding noise to nodes
     V1_noise = np.random.normal(0,1,M1_size)
     V1_noise = V1_noise*weight_range*noise_rate
     V1 = V1 + V1_noise
     V2_noise = np.random.normal(0,1,M2_size)
     V2_noise = V2_noise*weight_range*noise_rate
     V2 = V2 + V2_noise

     # Adding noise to edges
     # The disturbed graph will be also undirected.(Different from the matlab version)

     # M1_noise = np.triu(np.random.normal(0,1,[M1_size,M1_size]), 1)
     # M1_noise = M1_noise*weight_range*noise_rate
     # M1_noise = M1_noise + M1_noise.transpose()
     # none_zero = M1.nonzero()
     # M1[none_zero] += M1_noise[none_zero]
     for i in range(M1_size):
         for j in range(M1_size):
             if connected_nodes_m1[i, j]:
                 # M1[i, j, Mi[i, j]] += M1_noise[i, j]
                 noise = np.random.normal(0, 1, 1)
                 M1[(i, j)] += noise
                 M1[(j, i)] += noise

     # M2_noise = np.triu(np.random.normal(0,1,[M2_size,M2_size]), 1)
     # M2_noise = M2_noise*weight_range*noise_rate
     # M2_noise = M2_noise + M2_noise.transpose()
     # none_zero = M1.nonzero()
     # M1[none_zero] += M1_noise[none_zero]
     for i in range(M2_size):
         for j in range(M2_size):
             if connected_nodes_m2[i, j]:
                 # M2[i, j, Mi[i, j]] += M2_noise[i, j]
                 noise = np.random.normal(0, 1, 1)
                 M2[(i, j)] += noise
                 M2[(j, i)] += noise
     '''
     # The second method for generating noises.
     M1_noise = np.triu(np.random.rand(M1_size, M1_size), 1)
     M1_noise = M1_noise + M1_noise.transpose()
     M1_noise = (M1_noise >= noise_rate).astype(int)
     M1 = M1*M1_noise

     M2_noise = np.triu(np.random.rand(M2_size, M2_size), 1)
     M2_noise = M2_noise + M2_noise.transpose()
     M2_noise = (M2_noise >= noise_rate).astype(int)
     M2 = M2*M2_noise
     '''
     # try return M1, M2, V1, V2,
     # also return idx1, idx2 (The permutation)
     return M1, M2, V1, V2, idx1, idx2
Beispiel #43
0
def sqr(sqrA):
    return np.triu(np.outer(sqrA, sqrA)) + np.triu(np.outer(sqrA, sqrA), k=1)
Beispiel #44
0
def rands(rands_ele):
    """
	Create random QUBO
	"""
    return np.triu(np.random.rand(rands_ele, rands_ele))
Beispiel #45
0
def split_edges_walk_pooling(
    adj: sp.coo_matrix,
    validation_frac: float = 0.05,
    test_frac: float = 0.1,
    seed: int = 0,
    validation_edges_in_adj: bool = False,
    practical_neg_sample: bool = True,
):
    """Split edges consistent with the method used in Walk Pooling."""
    rng = np.random.default_rng(seed)

    def random_perm(size, dtype=np.int64):
        perm = np.arange(size, dtype=dtype)
        rng.shuffle(perm)
        return perm

    row = adj.row
    col = adj.col
    mask = row < col
    row, col = row[mask], col[mask]
    num_edges = row.size
    num_nodes = adj.shape[0]
    n_v = int(validation_frac *
              num_edges)  # number of validation positive edges
    n_t = int(test_frac * num_edges)  # number of test positive edges
    # split positive edges
    perm = random_perm(num_edges)
    row, col = row[perm], col[perm]
    r, c = row[:n_v], col[:n_v]
    val_pos = np.stack([r, c], axis=1)
    r, c = row[n_v:n_v + n_t], col[n_v:n_v + n_t]
    test_pos = np.stack([r, c], axis=1)
    r, c = row[n_v + n_t:], col[n_v + n_t:]
    train_pos = np.stack([r, c], axis=1)

    # sample negative edges
    if practical_neg_sample:
        neg_adj_mask = np.ones((num_nodes, num_nodes), dtype=bool)
        neg_adj_mask = np.triu(neg_adj_mask, 1)
        neg_adj_mask[row, col] = 0

        # Sample the test negative edges first
        neg_row, neg_col = np.where(neg_adj_mask)
        perm = random_perm(neg_row.size)[:n_t]
        neg_row, neg_col = neg_row[perm], neg_col[perm]
        test_neg = np.stack([neg_row, neg_col], axis=1)

        # Sample the train and val negative edges with only knowing
        # the train positive edges
        row, col = train_pos.T
        neg_adj_mask = np.ones((num_nodes, num_nodes), dtype=bool)
        neg_adj_mask = np.triu(neg_adj_mask, 1)
        neg_adj_mask[row, col] = 0

        # Sample the train and validation negative edges
        neg_row, neg_col = np.where(neg_adj_mask)
        n_tot = n_v + train_pos.size
        perm = random_perm(neg_row.size)[:n_tot]
        neg_row, neg_col = neg_row[perm], neg_col[perm]

        row, col = neg_row[:n_v], neg_col[:n_v]
        val_neg = np.stack([row, col], axis=1)

        row, col = neg_row[n_v:], neg_col[n_v:]
        train_neg = np.stack([row, col], axis=1)

    else:
        # If practical_neg_sample == False, the sampled negative edges
        # in the training and validation set aware the test set

        neg_adj_mask = np.ones((num_nodes, num_nodes), dtype=bool)
        neg_adj_mask = np.triu(neg_adj_mask, 1)
        neg_adj_mask[row, col] = 0

        # Sample all the negative edges and split into val, test, train negs
        neg_row, neg_col = np.where(neg_adj_mask)
        perm = random_perm(neg_row.size)[:row.size]
        neg_row, neg_col = neg_row[perm], neg_col[perm]

        row, col = neg_row[:n_v], neg_col[:n_v]
        val_neg = np.stack([row, col], axis=1)

        row, col = neg_row[n_v:n_v + n_t], neg_col[n_v:n_v + n_t]
        test_neg = np.stack([row, col], axis=1)

        row, col = neg_row[n_v + n_t:], neg_col[n_v + n_t:]
        train_neg = np.stack([row, col], axis=1)

    pos = (np.concatenate(
        (train_pos,
         val_pos), axis=0) if validation_edges_in_adj else train_pos)
    adj_train = sp.coo_matrix((np.ones(
        (pos.shape[0], ), dtype=np.float32), pos.T),
                              shape=adj.shape)
    adj_train = adj_train + adj_train.T
    return (
        adj_train,
        train_pos,
        train_neg,
        val_pos,
        val_neg,
        test_pos,
        test_neg,
    )
Beispiel #46
0
def mul(mulA, mulB):
    return np.triu(np.outer(mulA, mulB)) + np.triu(np.outer(mulA, mulB), k=1)
Beispiel #47
0
def load_fb_data(n_clusters, get_couples=False):

    feat_file = "data/fb/musae_facebook_features.json"

    all_features = load_musae_features(feat_file)

    n_nodes = all_features.shape[0]

    res_edges = load_edges(f"data/fb/fb_res_edges.csv")
    adj_complete = sp.csr_matrix(
        (np.ones(res_edges.shape[0]), (res_edges[:, 0], res_edges[:, 1])),
        shape=(n_nodes, n_nodes))

    test_edges = load_edges(f"data/fb/fb_test_edges.csv")
    test_matrix = sp.csr_matrix(
        (np.ones(test_edges.shape[0]), (test_edges[:, 0], test_edges[:, 1])),
        shape=(n_nodes, n_nodes))

    valid_edges = load_edges(f"data/fb/fb_train_edges.csv")
    valid_matrix = sp.csr_matrix((np.ones(valid_edges.shape[0]),
                                  (valid_edges[:, 0], valid_edges[:, 1])),
                                 shape=(n_nodes, n_nodes))

    adj_complete = adj_complete.toarray()

    # some values have a self loop, I remove it
    for i in range(n_nodes):
        adj_complete[i, i] = 0

    com_idx_to_clust_idx = {}

    node_to_clust = {}
    clust_to_node = {}
    with open(f"data/fb/{n_clusters}/labels_fb.csv", "r") as fin:
        for i, line in enumerate(fin):
            clust = int(line.strip())
            if (clust_to_node.get(clust) == None):
                clust_to_node[clust] = []

            com_idx_to_clust_idx[i] = len(clust_to_node[clust])

            clust_to_node[clust].append(i)

            node_to_clust[i] = clust

    if get_couples:
        return get_data_couples(all_features, adj_complete, test_matrix,
                                valid_matrix, node_to_clust, clust_to_node,
                                com_idx_to_clust_idx)

    clust_to_adj = {}
    for key in clust_to_node.keys():
        tmp_adj = adj_complete[clust_to_node[key], :]
        tmp_adj = tmp_adj[:, clust_to_node[key]]
        tmp_adj = tmp_adj + tmp_adj.T
        tmp_adj = np.triu(tmp_adj)
        clust_to_adj[key] = sp.csr_matrix(tmp_adj)

    adj_complete = None

    test_matrix = test_matrix.toarray()
    clust_to_test = {}
    for key in clust_to_node.keys():
        tmp_adj = test_matrix[clust_to_node[key], :]
        tmp_adj = tmp_adj[:, clust_to_node[key]]
        tmp_adj = tmp_adj + tmp_adj.T
        tmp_adj = np.triu(tmp_adj)
        clust_to_test[key] = sp.csr_matrix(tmp_adj)
    test_matrix = None

    valid_matrix = valid_matrix.toarray()
    clust_to_valid = {}
    for key in clust_to_node.keys():
        tmp_adj = valid_matrix[clust_to_node[key], :]
        tmp_adj = tmp_adj[:, clust_to_node[key]]
        tmp_adj = tmp_adj + tmp_adj.T
        tmp_adj = np.triu(tmp_adj)
        clust_to_valid[key] = sp.csr_matrix(tmp_adj)
    valid_matrix = None

    clust_to_features = {}
    for key in clust_to_node.keys():
        tmp_feat = all_features[clust_to_node[key], :]
        clust_to_features[key] = sp.csr_matrix(tmp_feat)

    return clust_to_adj, clust_to_features, clust_to_test, clust_to_valid, clust_to_node, node_to_clust, com_idx_to_clust_idx
Beispiel #48
0
def subid_det(y, i, n, u, w="SV"):

    input_observations, input_dimensions = u.shape  # nu, m
    output_observations, output_dimensions = y.shape  # ny, l

    assert output_observations == input_observations, "Number of data points different in input and output"
    assert i > 0, "Number of block rows should be positive"
    assert (output_observations - 2 * i + 1) >= (2 * output_dimensions *
                                                 i), "Not enough data points"

    j = input_observations - (
        2 * i - 1)  # Determine the number of columns in the Hankel matrices

    Y = hankel_matrix(y / np.sqrt(j), 2 * i, j)
    U = hankel_matrix(u / np.sqrt(j), 2 * i, j)

    m = input_dimensions
    l = output_dimensions

    # Compute the R factor
    R = np.triu(np.linalg.qr(np.vstack((U, Y)).conj().T,
                             mode='complete')[1]).conj().T  # R factor
    R = R[:2 * i * (m + l), :2 * i * (m + l)]

    # Calculate oblique projection
    Rf = R[(2 * m + l) * i:, :]  # Future outputs
    Rp = np.vstack(
        (R[:m * i, :],
         R[2 * m * i:(2 * m + l) * i, :]))  # Past(inputs and) outputs
    Ru = R[m * i:2 * m * i, :]  # Future inputs

    Rfp = project_on_perpendicular(Rf, Ru)  # Perpendicular Future outputs
    Rpp = project_on_perpendicular(Rp, Ru)  # Perpendicular Past

    # The oblique projection "obl/Ufp = Yf/Ufp * pinv(Wp/Ufp) * Wp" Computed as 6.1 on page 166
    Ob = matdiv(Rfp, Rpp) @ Rp

    # # Funny rankv check (SVD takes too long). This check is needed to avoid rank deficiency warnings
    # if np.linalg.norm(Rpp[:, (2 * m+l) * i - 2 * l: (2 * m + l) * i], 'fro') < 1e-10:
    #     Ob = (Rfp @ np.linalg.pinv(Rpp.conj().T).conj().T) @ Rp  # Oblique projection
    # else:
    #     Ob = matdiv(Rfp, Rpp) @ Rp

    # Compute the SVD
    U, S, V = np.linalg.svd(Ob)
    ss = np.diag(S)

    # Determine the order from the singular values
    U1 = U[:, :n]

    # Determine gam and gamm
    gam = U1 @ np.diag(np.sqrt(np.diag(ss[:n])))
    gamm = gam[:l * (i - 1), :]

    # Compute Obm (the second oblique projection)
    Rf = R[(2 * m + l) * i + l:, :]
    Rp = np.vstack((R[:m * (i + 1), :], R[2 * m * i:(2 * m + l) * i + l, :]))
    Ru = R[m * i + m:2 * m * i, :]

    Rfp = project_on_perpendicular(Rf, Ru)
    Rpp = project_on_perpendicular(Rp, Ru)

    Obm = matdiv(Rfp, Rpp) @ Rp

    # # Funny rankv check (SVD takes too long). This check is needed to avoid rank deficiency warnings
    # if np.linalg.norm(Rpp[:, (2 * m+l) * i - 2 * l: (2 * m + l) * i], 'fro') < 1e-10:
    #     Ob = (Rfp @ np.linalg.pinv(Rpp.conj().T).conj().T) @ Rp  # Oblique projection
    # else:
    #     Ob = matdiv(Rfp, Rpp) @ Rp

    # Determine the states Xi and Xip
    Xi = np.linalg.pinv(gam) @ Ob
    Xip = np.linalg.pinv(gamm) @ Obm

    # Solve linear system of equations
    Rhs = np.vstack((Xi, R[m * i:m * (i + 1), :]))
    Lhs = np.vstack((Xip, R[(2 * m + l) * i:(2 * m + l) * i + l, :]))

    sol = matdiv(Lhs, Rhs)

    # Extract the system matrices
    A = sol[:n, :n]
    B = sol[:n, n:n + m]
    C = sol[n:n + l, :n]
    D = sol[n:n + l, n:n + m]

    return A, B, C, D, ss
Beispiel #49
0
def load_pubmed_data(n_clusters,
                     random=False,
                     sparsest_cut=False,
                     get_couples=False):
    folder = "pubmed" if sparsest_cut == False else "pubmed_sparsest_cut"
    data = sio.loadmat(f'data/{folder}/pubmed.mat')

    adj_complete = data['W'].toarray()
    all_features = data['fea']

    res_edges = load_edges(f"data/{folder}/res_edges.csv")
    adj_complete = sp.csr_matrix(
        (np.ones(res_edges.shape[0]), (res_edges[:, 0], res_edges[:, 1])),
        shape=adj_complete.shape)

    test_edges = load_edges(f"data/{folder}/test_edges.csv")
    test_matrix = sp.csr_matrix(
        (np.ones(test_edges.shape[0]), (test_edges[:, 0], test_edges[:, 1])),
        shape=adj_complete.shape)

    valid_edges = load_edges(f"data/{folder}/train_edges.csv")
    valid_matrix = sp.csr_matrix((np.ones(valid_edges.shape[0]),
                                  (valid_edges[:, 0], valid_edges[:, 1])),
                                 shape=adj_complete.shape)

    adj_complete = adj_complete.toarray()

    # some values have a self loop, I remove it
    for i in range(adj_complete.shape[0]):
        adj_complete[i, i] = 0

    com_idx_to_clust_idx = {}
    node_to_clust = {}
    clust_to_node = {}

    labels_folder = folder if not random else f"{folder}_random"

    with open(f"data/{labels_folder}/{n_clusters}/labels_pubmed.csv",
              "r") as fin:
        for i, line in enumerate(fin):
            clust = int(line.strip())
            if (clust_to_node.get(clust) == None):
                clust_to_node[clust] = []

            com_idx_to_clust_idx[i] = len(clust_to_node[clust])

            clust_to_node[clust].append(i)

            node_to_clust[i] = clust

    if get_couples:
        return get_data_couples(all_features, adj_complete, test_matrix,
                                valid_matrix, node_to_clust, clust_to_node,
                                com_idx_to_clust_idx)

    clust_to_adj = {}
    for key in clust_to_node.keys():
        tmp_adj = adj_complete[clust_to_node[key], :]
        tmp_adj = tmp_adj[:, clust_to_node[key]]
        tmp_adj = tmp_adj + tmp_adj.T
        tmp_adj = np.triu(tmp_adj)
        print("TRIUUUU")
        clust_to_adj[key] = sp.csr_matrix(tmp_adj)

    adj_complete = None

    test_matrix = test_matrix.toarray()
    clust_to_test = {}
    for key in clust_to_node.keys():
        tmp_adj = test_matrix[clust_to_node[key], :]
        tmp_adj = tmp_adj[:, clust_to_node[key]]
        tmp_adj = tmp_adj + tmp_adj.T
        tmp_adj = np.triu(tmp_adj)
        clust_to_test[key] = sp.csr_matrix(tmp_adj)
    test_matrix = None

    valid_matrix = valid_matrix.toarray()
    clust_to_valid = {}
    for key in clust_to_node.keys():
        tmp_adj = valid_matrix[clust_to_node[key], :]
        tmp_adj = tmp_adj[:, clust_to_node[key]]
        tmp_adj = tmp_adj + tmp_adj.T
        tmp_adj = np.triu(tmp_adj)
        clust_to_valid[key] = sp.csr_matrix(tmp_adj)
    valid_matrix = None

    clust_to_features = {}
    for key in clust_to_node.keys():
        tmp_feat = all_features[clust_to_node[key], :]
        clust_to_features[key] = sp.csr_matrix(tmp_feat)

    return clust_to_adj, clust_to_features, clust_to_test, clust_to_valid, clust_to_node, node_to_clust, com_idx_to_clust_idx
Beispiel #50
0
def load_amazon_electronics_computers_data(n_clusters,
                                           random=False,
                                           is_photos=False,
                                           get_couples=False):
    print(f"random: {random}")
    path = "amazon_electronics_photo" if is_photos else "amazon_electronics_computers"

    data = np.load(f"data/{path}/{path}.npz")
    data = from_flat_dict(data)

    adj_complete = data["adj_matrix"]
    all_features = data['attr_matrix']

    res_edges = load_edges(f"data/{path}/{path}.npz_res_edges.csv")
    adj_complete = sp.csr_matrix(
        (np.ones(res_edges.shape[0]), (res_edges[:, 0], res_edges[:, 1])),
        shape=adj_complete.shape)

    test_edges = load_edges(f"data/{path}/{path}.npz_test_edges.csv")
    test_matrix = sp.csr_matrix(
        (np.ones(test_edges.shape[0]), (test_edges[:, 0], test_edges[:, 1])),
        shape=adj_complete.shape)

    valid_edges = load_edges(f"data/{path}/{path}.npz_train_edges.csv")
    valid_matrix = sp.csr_matrix((np.ones(valid_edges.shape[0]),
                                  (valid_edges[:, 0], valid_edges[:, 1])),
                                 shape=adj_complete.shape)

    adj_complete = adj_complete.toarray()

    # some values have a self loop, I remove it
    for i in range(adj_complete.shape[0]):
        adj_complete[i, i] = 0

    com_idx_to_clust_idx = {}
    node_to_clust = {}
    clust_to_node = {}

    labels_folder = path if not random else f"{path}_random"

    with open(f"data/{labels_folder}/{n_clusters}/labels_{path}.npz.csv",
              "r") as fin:
        for i, line in enumerate(fin):
            clust = int(line.strip())
            if (clust_to_node.get(clust) == None):
                clust_to_node[clust] = []

            com_idx_to_clust_idx[i] = len(clust_to_node[clust])

            clust_to_node[clust].append(i)

            node_to_clust[i] = clust

    if get_couples:
        return get_data_couples(all_features, adj_complete, test_matrix,
                                valid_matrix, node_to_clust, clust_to_node,
                                com_idx_to_clust_idx)

    clust_to_adj = {}
    for key in clust_to_node.keys():
        tmp_adj = adj_complete[clust_to_node[key], :]
        tmp_adj = tmp_adj[:, clust_to_node[key]]
        tmp_adj = tmp_adj + tmp_adj.T
        tmp_adj = np.triu(tmp_adj)
        clust_to_adj[key] = sp.csr_matrix(tmp_adj)

    adj_complete = None

    test_matrix = test_matrix.toarray()
    clust_to_test = {}
    for key in clust_to_node.keys():
        tmp_adj = test_matrix[clust_to_node[key], :]
        tmp_adj = tmp_adj[:, clust_to_node[key]]
        tmp_adj = tmp_adj + tmp_adj.T
        tmp_adj = np.triu(tmp_adj)
        clust_to_test[key] = sp.csr_matrix(tmp_adj)
    test_matrix = None

    valid_matrix = valid_matrix.toarray()
    clust_to_valid = {}
    for key in clust_to_node.keys():
        tmp_adj = valid_matrix[clust_to_node[key], :]
        tmp_adj = tmp_adj[:, clust_to_node[key]]
        tmp_adj = tmp_adj + tmp_adj.T
        tmp_adj = np.triu(tmp_adj)
        clust_to_valid[key] = sp.csr_matrix(tmp_adj)
    valid_matrix = None

    all_features = data['attr_matrix']
    clust_to_features = {}
    for key in clust_to_node.keys():
        tmp_feat = all_features[clust_to_node[key], :]
        clust_to_features[key] = sp.csr_matrix(tmp_feat)

    return clust_to_adj, clust_to_features, clust_to_test, clust_to_valid, clust_to_node, node_to_clust, com_idx_to_clust_idx
Beispiel #51
0
    def update(self, arfitness):
        """
        Update values of method from new evaluated generation

        :param arfitness: list of function values to individuals
        :type arfitness: list
        """
        self.counteval += self.lamda
        self.arfitness = arfitness
        # sort by fitness and compute weighted mean into xmean
        self.arindex = np.argsort(self.arfitness)
        self.arfitness = self.arfitness[self.arindex]
        self.xmean = np.dot(self.arx[self.arindex[:self.mu]].T, self.weights)
        self.zmean = np.dot(self.arz[self.arindex[:self.mu]].T, self.weights)
        self.ps = np.dot((1 - self.cs), self.ps) + np.dot(
            (np.sqrt(self.cs *
                     (2 - self.cs) * self.mueff)), np.dot(self.B, self.zmean))
        self.hsig = np.linalg.norm(
            self.ps) / np.sqrt(1 -
                               (1 - self.cs)**(2 * self.counteval / self.lamda)
                               ) / self.chiN < 1.4 + 2 / (self.N + 1)
        self.pc = np.dot((1 - self.cc), self.pc) + np.dot(
            np.dot(self.hsig, np.sqrt(self.cc * (2 - self.cc) * self.mueff)),
            np.dot(np.dot(self.B, self.D), self.zmean),
        )
        # adapt covariance matrix C
        self.C = (np.dot((1 - self.c1 - self.cmu), self.C) + np.dot(
            self.c1,
            ((self.pc * self.pc.T) + np.dot(
                (1 - self.hsig) * self.cc * (2 - self.cc), self.C)),
        ) + np.dot(
            self.cmu,
            np.dot(
                np.dot(
                    np.dot(np.dot(self.B, self.D),
                           self.arz[self.arindex[:self.mu]].T),
                    np.diag(self.weights),
                ),
                (np.dot(np.dot(self.B, self.D),
                        self.arz[self.arindex[:self.mu]].T)).T,
            ),
        ))
        # adapt step size sigma
        self.sigma = self.sigma * np.exp(
            (self.cs / self.damps) * (np.linalg.norm(self.ps) / self.chiN - 1))
        # diagonalization
        if (self.counteval - self.eigenval > self.lamda /
            (self.c1 + self.cmu) / self.N / 10):
            self.eigenval = self.counteval
            self.C = np.triu(self.C) + np.transpose(np.triu(self.C, 1))
            self.D, self.B = np.linalg.eig(self.C)
            self.D = np.diag(np.sqrt(self.D))
            # history
        self.history["short_best"].append(arfitness[0])
        if len(self.history["short_best"]) >= self.short_history_len:
            self.history["short_best"].popleft()
        if self.generation % 5 == 0:  # last 20 %
            self.history["long_best"].append(arfitness[0])
            self.history["long_median"].append(np.median(arfitness))
        if len(self.history["long_best"]) >= self.long_history_len_up:
            self.history["long_best"].popleft()
            self.history["long_median"].popleft()
        self.checkStop()
        if self.generation % 20 == 0:
            self.logState()
coded by: Rakeeb
code for: implenting Gauss Seidel method
'''

import numpy as np 

x_exact= np.array([7.859713071,0.422926408,-0.073592239,-0.540643016,0.010626163]) #exact solution given

a= np.array([[0.2,0.1,1,1,0],[0.1,4,-1,1,-1],[1,-1,60,0,-2],[1,1,0,8,4],[0,-1,-2,4,700]])
b= np.array([1,2,3,4,5])

# Gauss Seidel method:

x= np.zeros(b.size)  # initial value

U= np.triu(a,k=1) #upper triangular part of "a" without main diagonal
D_L= np.tril(a,k=0) #lower triangular part of "a" with the main diagonal
'''
As was done in class, U is the upper triangular part and D_L is the (D+L) matrix, where D is the diagonal part
 and L is the lower triangular part of the given matrix. We have to take the inverse of the (D+L) part and 
 multiply it to the right hand side.
'''

D_L_inv= np.linalg.inv(D_L)

i=0  # for counting iteration number
while (any(np.abs(x_exact[j]-x[j])>0.01 for j in range (b.size))):
	s= b-np.matmul(U,x)
	x= np.matmul(D_L_inv,s)
	i=i+1
print('solution by Gauss-Seidel method: ',x ,'\nNumber of  itarations needed: ',i )		
Beispiel #53
0
embedding = node_position_model.fit_transform(X.T).T

# #############################################################################
# Visualization
plt.figure(1, facecolor='w', figsize=(10, 8))
plt.clf()
ax = plt.axes([0., 0., 1., 1.])
plt.axis('off')

# Display a graph of the partial correlations
partial_correlations = edge_model.precision_.copy()
d = 1 / np.sqrt(np.diag(partial_correlations))
partial_correlations *= d
partial_correlations *= d[:, np.newaxis]
non_zero = (np.abs(np.triu(partial_correlations, k=1)) > 0.02)

# Plot the nodes using the coordinates of our embedding
plt.scatter(embedding[0], embedding[1], s=100 * d ** 2, c=labels,
            cmap=plt.cm.nipy_spectral)

# Plot the edges
start_idx, end_idx = np.where(non_zero)
# a sequence of (*line0*, *line1*, *line2*), where::
#            linen = (x0, y0), (x1, y1), ... (xm, ym)
segments = [[embedding[:, start], embedding[:, stop]]
            for start, stop in zip(start_idx, end_idx)]
values = np.abs(partial_correlations[non_zero])
lc = LineCollection(segments,
                    zorder=0, cmap=plt.cm.hot_r,
                    norm=plt.Normalize(0, .7 * values.max()))
def main(train_path, out_folder_path):

    # Allow Altair to make plots using more than 5000 rows
    alt.data_transformers.disable_max_rows()

    # Read data from file path
    train = pd.read_csv(train_path)
    columns = [
        'housing_median_age', 'total_rooms', 'total_bedrooms', 'population',
        'households', 'median_income', 'ocean_proximity', 'latitude',
        'longitude'
    ]
    X_train = train[columns]
    y_train = train['median_house_value']

    def make_chart(x, x_title):
        """
        Creates an altair scatterplot with the input on the x-axis and 
        the median house value on the y-axis
    
        Parameters
        ----------
        x: string
            the column name of the x-axis column to be created
        x_title: string
            the title of the x-axis (and to be used in the chart title)
    
        Returns
        ----------
        altair chart object
            scatterplot of defined x compared to median house value
        """
        chart = alt.Chart(train).mark_point(opacity=0.2).encode(
            alt.X(x, title=x_title),
            alt.Y('median_house_value:Q',
                  title="Median House Value")).properties(
                      width=300,
                      height=250,
                      title="Median House Value per " + x_title)
        return chart

    # Make charts and save them
    make_chart('housing_median_age',
               "House Median Age").save(out_folder_path +
                                        'median-age_scatterplot.png')
    make_chart('total_rooms',
               "Total Rooms").save(out_folder_path +
                                   'total-rooms_scatterplot.png')
    make_chart('total_bedrooms',
               "Total Bedrooms").save(out_folder_path +
                                      'total-bedrooms_scatterplot.png')
    make_chart('population', "Population").save(out_folder_path +
                                                'population_scatterplot.png')
    make_chart('households', "Households").save(out_folder_path +
                                                'households_scatterplot.png')
    make_chart('median_income',
               "Median Income").save(out_folder_path +
                                     'median-income_scatterplot.png')

    # Look at the relationship between total_rooms and total_bedrooms
    alt.Chart(X_train).mark_point(opacity=0.2).encode(
        alt.X('total_bedrooms', title="Total Bedrooms"),
        alt.Y('total_rooms', title="Total Rooms")).properties(
            width=300,
            height=250,
            title="Relationship between Bedroom and Room Counts").save(
                out_folder_path + 'total-rooms_total-bedrooms.png')

    # Visualize Correlation Matrix between variables
    # Rename correlation matrix titles
    corrmatrix_titles = {
        "housing_median_age": "Median House Age",
        "total_rooms": "Total Rooms",
        "total_bedrooms": "Total Bedrooms",
        "population": "Population",
        "households": "Households",
        "median_income": "Median Income",
        "latitude": "Latitude",
        "longitude": "Longitude",
        "median_house_value": "Median House Value"
    }
    corrMatrix = train.corr()
    corrMatrix = corrMatrix.rename(columns=corrmatrix_titles)
    corrMatrix = corrMatrix.rename(index=corrmatrix_titles)

    # Create mask for upper triangle
    upper_mask = np.triu(np.ones_like(corrMatrix, dtype=np.bool))

    # Create matplotlib figure
    corr_plot, ax = plt.subplots(figsize=(12, 12))

    # Create heatmap
    cmap = sns.diverging_palette(240, 10, as_cmap=True)

    # Overlay mask on heat map
    sns.heatmap(corrMatrix,
                mask=upper_mask,
                cmap=cmap,
                vmax=0.5,
                center=0,
                square=True,
                linewidths=.8,
                cbar_kws={"shrink": 0.5})
    plt.title('Correlation Matrix')

    # Save heatmap to file
    corr_plot.savefig(out_folder_path + 'correlation_heatmap.png')

    # Create Variance Inflation Factor Table
    # Drop `ocean_proximity`, `latitude` and `longitude` columns
    mc_data = pd.DataFrame.drop(
        X_train, columns=['ocean_proximity', 'longitude', 'latitude'])
    mc_data['intercept'] = 1

    # Create and return dataframe with VIFs
    vif = pd.DataFrame()
    vif['variable'] = mc_data.columns
    vif['VIF'] = [
        variance_inflation_factor(mc_data.values, i)
        for i in range(mc_data.shape[1])
    ]
    vif.to_csv(out_folder_path + 'vif_table.csv', index=False)

    # Sources:
    # https://campus.datacamp.com/courses/generalized-linear-models-in-python/multivariable-logistic-regression?ex=4

    assert os.path.isfile(out_folder_path + 'correlation_heatmap.png')
    assert os.path.isfile(out_folder_path + 'households_scatterplot.png')
    assert os.path.isfile(out_folder_path + 'median-age_scatterplot.png')
    assert os.path.isfile(out_folder_path + 'median-income_scatterplot.png')
    assert os.path.isfile(out_folder_path + 'population_scatterplot.png')
    assert os.path.isfile(out_folder_path + 'total-bedrooms_scatterplot.png')
    assert os.path.isfile(out_folder_path + 'total-rooms_scatterplot.png')
    assert os.path.isfile(out_folder_path + 'total-rooms_total-bedrooms.png')
    assert os.path.isfile(out_folder_path + 'vif_table.csv')
Beispiel #55
0
def test_tsqr_uncertain(m_min, n_max, chunks, vary_rows, vary_cols,
                        error_type):
    mat = cupy.random.rand(m_min * 2, n_max)
    m, n = m_min * 2, n_max
    mat[0:m_min, 0] += 1
    _c0 = mat[:, 0]
    _r0 = mat[0, :]
    c0 = da.from_array(_c0, chunks=m_min, name="c", asarray=False)
    r0 = da.from_array(_r0, chunks=n_max, name="r", asarray=False)
    data = da.from_array(mat, chunks=chunks, name="A", asarray=False)
    if vary_rows:
        data = data[c0 > 0.5, :]
        mat = mat[_c0 > 0.5, :]
        m = mat.shape[0]
    if vary_cols:
        data = data[:, r0 > 0.5]
        mat = mat[:, _r0 > 0.5]
        n = mat.shape[1]

    # qr
    m_q = m
    n_q = min(m, n)
    m_r = n_q
    n_r = n

    # svd
    m_u = m
    n_u = min(m, n)
    n_s = n_q
    m_vh = n_q
    n_vh = n
    d_vh = max(m_vh, n_vh)  # full matrix returned

    if error_type is None:
        # test QR
        q, r = da.linalg.tsqr(data)
        q = q.compute()  # because uncertainty
        r = r.compute()
        assert_eq((m_q, n_q), q.shape)  # shape check
        assert_eq((m_r, n_r), r.shape)  # shape check
        assert_eq(mat, np.dot(q, r))  # accuracy check
        assert_eq(np.eye(n_q, n_q), np.dot(q.T, q))  # q must be orthonormal
        assert_eq(r, np.triu(r))  # r must be upper triangular

        # test SVD
        u, s, vh = da.linalg.tsqr(data, compute_svd=True)
        u = u.compute()  # because uncertainty
        s = s.compute()
        vh = vh.compute()
        s_exact = np.linalg.svd(mat)[1]
        assert_eq(s, s_exact)  # s must contain the singular values
        assert_eq((m_u, n_u), u.shape)  # shape check
        assert_eq((n_s, ), s.shape)  # shape check
        assert_eq((d_vh, d_vh), vh.shape)  # shape check
        assert_eq(np.eye(n_u, n_u), np.dot(u.T, u))  # u must be orthonormal
        assert_eq(np.eye(d_vh, d_vh), np.dot(vh,
                                             vh.T))  # vh must be orthonormal
        assert_eq(mat, np.dot(np.dot(u, np.diag(s)),
                              vh[:n_q]))  # accuracy check
    else:
        with pytest.raises(error_type):
            q, r = da.linalg.tsqr(data)
        with pytest.raises(error_type):
            u, s, vh = da.linalg.tsqr(data, compute_svd=True)
Beispiel #56
0
 def get_subseq_mask(self, size):
     k = 1 + size[-1] - size[-2]
     subsequent_mask = np.triu(np.ones(size), k=k).astype('uint8')
     subsequent_mask = torch.from_numpy(subsequent_mask).to(device)
     return subsequent_mask
 def test_invalid_adjacency(self, dim):
     """Test if function raises a ``ValueError`` for a matrix that is not symmetric"""
     with pytest.raises(ValueError, match="Input must be a NumPy array"):
         adj_asym = np.triu(np.ones((dim, dim)))
         sample.sample(A=adj_asym, n_mean=1.0)
def similarity_fig_from_weighted_prediction(round_idx,
                                            respondent_idx,
                                            similarity_edge_list=None,
                                            weighted_pred=None):
    """
    The similarity_edge_list has the priority for construction than the weighted prediction.

    :param round_idx: integer, ranging from 1 to 10
    :param respondent_idx: id of respondents, ranging from the 1 to 35
    :param similarity_edge_list: edges between similar points, a list of tuples(x_i, x_j),
    indicating that x_i and x_j are similar
    :param weighted_pred: confident of confidence weighted prediction,
    either at a specific round_idx or for a specific respondent at all times
    :return: a similarity connection graph of the 50 defendants, and the similarity_edge_list
    """
    _, X_test, _, y_test, _ = load_dataset(onehot=True)

    # (x,y) position of each nodes, here I set them into the circle for appreciation of beauty
    pos = dict()
    for i in range(50):
        pos[X_test.index.values[i]] = (np.cos(2 * np.pi * i / 50),
                                       np.sin(2 * np.pi * i / 50))
    # add all the nodes to the network
    G = nx.Graph()
    G.add_nodes_from(pos.keys())
    for n, p in pos.items():
        G.node[n]['pos'] = p

    # similarity_edge_list has the privilege to be used for the construction of the network
    if similarity_edge_list is None:
        if weighted_pred is None:
            # use the 14 classes weighted prediction if both are None
            y_test = weighted_prediction_round(
                round_idx=round_idx)[respondent_idx - 1].values.reshape(-1, 1)
        else:
            y_test = weighted_pred[respondent_idx - 1].values.reshape(-1, 1)
        # to integrate the labels into a pairwise fashion
        mask = (y_test[None] == y_test[:, None])[:, :, 0]
        a, b = np.nonzero(np.triu(mask, k=1))
        similarity_edge_list = [(X_test.index.values[a_],
                                 X_test.index.values[b_])
                                for a_, b_ in zip(a, b)]

    G.add_edges_from(similarity_edge_list)
    '''Make the Graph via Plot.ly'''
    edge_trace = go.Scatter(x=[],
                            y=[],
                            line=dict(width=0.5, color='#888'),
                            hoverinfo='none',
                            mode='lines')

    for edge in G.edges():
        x0, y0 = G.node[edge[0]]['pos']
        x1, y1 = G.node[edge[1]]['pos']
        edge_trace['x'] += tuple([x0, x1, None])
        edge_trace['y'] += tuple([y0, y1, None])

    node_trace = go.Scatter(
        x=[],
        y=[],
        text=[],
        mode='markers',
        hoverinfo='text',
        marker=dict(
            showscale=True,
            # color-scale options
            # 'Greys' | 'YlGnBu' | 'Greens' | 'YlOrRd' | 'Bluered' | 'RdBu' |
            # 'Reds' | 'Blues' | 'Picnic' | 'Rainbow' | 'Portland' | 'Jet' |
            # 'Hot' | 'Blackbody' | 'Earth' | 'Electric' | 'Viridis' |
            colorscale='YlGnBu',
            reversescale=True,
            color=[],
            size=10,
            colorbar=dict(thickness=15,
                          title='Node Connections',
                          xanchor='left',
                          titleside='right'),
            line=dict(width=2)))

    for node in G.nodes():
        x, y = G.node[node]['pos']
        node_trace['x'] += tuple([x])
        node_trace['y'] += tuple([y])

    for node, adjacencies in enumerate(G.adjacency()):
        node_trace['marker']['color'] += tuple([len(adjacencies[1])])
        node_info = '# of connections: ' + str(len(adjacencies[1])) + '<br>Defendant_ID: '\
                    + str(X_test.index.values[node])
        node_trace['text'] += tuple([node_info])

    fig = go.Figure(
        data=[edge_trace, node_trace],
        layout=go.Layout(
            width=700,
            height=600,
            title=
            '<br>Similarity Graph of COMPAS Defendants (Respondent-%2d_Week-%2d)'
            % (respondent_idx, round_idx),
            titlefont=dict(size=16),
            showlegend=False,
            hovermode='closest',
            margin=dict(b=20, l=50, r=50, t=40),
            xaxis=dict(showgrid=False, zeroline=False, showticklabels=False),
            yaxis=dict(showgrid=False, zeroline=False, showticklabels=False)))

    return fig, list(G.edges())
Beispiel #59
0
def test_tril_triu_non_square_arrays():
    A = cupy.random.randint(0, 11, (30, 35))
    dA = da.from_array(A, chunks=(5, 5), asarray=False)
    assert_eq(da.triu(dA), np.triu(A))
    assert_eq(da.tril(dA), np.tril(A))
Beispiel #60
0
def leastsq(func,
            x0,
            args=(),
            Dfun=None,
            full_output=0,
            col_deriv=0,
            ftol=1.49012e-8,
            xtol=1.49012e-8,
            gtol=0.0,
            maxfev=0,
            epsfcn=None,
            factor=100,
            diag=None):
    """
    Minimize the sum of squares of a set of equations.

    ::

        x = arg min(sum(func(y)**2,axis=0))
                 y

    Parameters
    ----------
    func : callable
        should take at least one (possibly length N vector) argument and
        returns M floating point numbers. It must not return NaNs or
        fitting might fail.
    x0 : ndarray
        The starting estimate for the minimization.
    args : tuple, optional
        Any extra arguments to func are placed in this tuple.
    Dfun : callable, optional
        A function or method to compute the Jacobian of func with derivatives
        across the rows. If this is None, the Jacobian will be estimated.
    full_output : bool, optional
        non-zero to return all optional outputs.
    col_deriv : bool, optional
        non-zero to specify that the Jacobian function computes derivatives
        down the columns (faster, because there is no transpose operation).
    ftol : float, optional
        Relative error desired in the sum of squares.
    xtol : float, optional
        Relative error desired in the approximate solution.
    gtol : float, optional
        Orthogonality desired between the function vector and the columns of
        the Jacobian.
    maxfev : int, optional
        The maximum number of calls to the function. If `Dfun` is provided
        then the default `maxfev` is 100*(N+1) where N is the number of elements
        in x0, otherwise the default `maxfev` is 200*(N+1).
    epsfcn : float, optional
        A variable used in determining a suitable step length for the forward-
        difference approximation of the Jacobian (for Dfun=None). 
        Normally the actual step length will be sqrt(epsfcn)*x
        If epsfcn is less than the machine precision, it is assumed that the 
        relative errors are of the order of the machine precision.
    factor : float, optional
        A parameter determining the initial step bound
        (``factor * || diag * x||``). Should be in interval ``(0.1, 100)``.
    diag : sequence, optional
        N positive entries that serve as a scale factors for the variables.

    Returns
    -------
    x : ndarray
        The solution (or the result of the last iteration for an unsuccessful
        call).
    cov_x : ndarray
        Uses the fjac and ipvt optional outputs to construct an
        estimate of the jacobian around the solution. None if a
        singular matrix encountered (indicates very flat curvature in
        some direction).  This matrix must be multiplied by the
        residual variance to get the covariance of the
        parameter estimates -- see curve_fit.
    infodict : dict
        a dictionary of optional outputs with the key s:

        ``nfev``
            The number of function calls
        ``fvec``
            The function evaluated at the output
        ``fjac``
            A permutation of the R matrix of a QR
            factorization of the final approximate
            Jacobian matrix, stored column wise.
            Together with ipvt, the covariance of the
            estimate can be approximated.
        ``ipvt``
            An integer array of length N which defines
            a permutation matrix, p, such that
            fjac*p = q*r, where r is upper triangular
            with diagonal elements of nonincreasing
            magnitude. Column j of p is column ipvt(j)
            of the identity matrix.
        ``qtf``
            The vector (transpose(q) * fvec).

    mesg : str
        A string message giving information about the cause of failure.
    ier : int
        An integer flag.  If it is equal to 1, 2, 3 or 4, the solution was
        found.  Otherwise, the solution was not found. In either case, the
        optional output variable 'mesg' gives more information.

    Notes
    -----
    "leastsq" is a wrapper around MINPACK's lmdif and lmder algorithms.

    cov_x is a Jacobian approximation to the Hessian of the least squares
    objective function.
    This approximation assumes that the objective function is based on the
    difference between some observed target data (ydata) and a (non-linear)
    function of the parameters `f(xdata, params)` ::

           func(params) = ydata - f(xdata, params)

    so that the objective function is ::

           min   sum((ydata - f(xdata, params))**2, axis=0)
         params

    """
    x0 = asarray(x0).flatten()
    n = len(x0)
    if not isinstance(args, tuple):
        args = (args, )
    shape, dtype = _check_func('leastsq', 'func', func, x0, args, n)
    m = shape[0]
    if n > m:
        raise TypeError('Improper input: N=%s must not exceed M=%s' % (n, m))
    if epsfcn is None:
        epsfcn = finfo(dtype).eps
    if Dfun is None:
        if maxfev == 0:
            maxfev = 200 * (n + 1)
        retval = _minpack._lmdif(func, x0, args, full_output, ftol, xtol, gtol,
                                 maxfev, epsfcn, factor, diag)
    else:
        if col_deriv:
            _check_func('leastsq', 'Dfun', Dfun, x0, args, n, (n, m))
        else:
            _check_func('leastsq', 'Dfun', Dfun, x0, args, n, (m, n))
        if maxfev == 0:
            maxfev = 100 * (n + 1)
        retval = _minpack._lmder(func, Dfun, x0, args, full_output, col_deriv,
                                 ftol, xtol, gtol, maxfev, factor, diag)

    errors = {
        0: ["Improper input parameters.", TypeError],
        1: [
            "Both actual and predicted relative reductions "
            "in the sum of squares\n  are at most %f" % ftol, None
        ],
        2: [
            "The relative error between two consecutive "
            "iterates is at most %f" % xtol, None
        ],
        3: [
            "Both actual and predicted relative reductions in "
            "the sum of squares\n  are at most %f and the "
            "relative error between two consecutive "
            "iterates is at \n  most %f" % (ftol, xtol), None
        ],
        4: [
            "The cosine of the angle between func(x) and any "
            "column of the\n  Jacobian is at most %f in "
            "absolute value" % gtol, None
        ],
        5: [
            "Number of calls to function has reached "
            "maxfev = %d." % maxfev, ValueError
        ],
        6: [
            "ftol=%f is too small, no further reduction "
            "in the sum of squares\n  is possible."
            "" % ftol, ValueError
        ],
        7: [
            "xtol=%f is too small, no further improvement in "
            "the approximate\n  solution is possible." % xtol, ValueError
        ],
        8: [
            "gtol=%f is too small, func(x) is orthogonal to the "
            "columns of\n  the Jacobian to machine "
            "precision." % gtol, ValueError
        ],
        'unknown': ["Unknown error.", TypeError]
    }

    info = retval[-1]  # The FORTRAN return value

    if info not in [1, 2, 3, 4] and not full_output:
        if info in [5, 6, 7, 8]:
            warnings.warn(errors[info][0], RuntimeWarning)
        else:
            try:
                raise errors[info][1](errors[info][0])
            except KeyError:
                raise errors['unknown'][1](errors['unknown'][0])

    mesg = errors[info][0]
    if full_output:
        cov_x = None
        if info in [1, 2, 3, 4]:
            from numpy.dual import inv
            from numpy.linalg import LinAlgError
            perm = take(eye(n), retval[1]['ipvt'] - 1, 0)
            r = triu(transpose(retval[1]['fjac'])[:n, :])
            R = dot(r, perm)
            try:
                cov_x = inv(dot(transpose(R), R))
            except (LinAlgError, ValueError):
                pass
        return (retval[0], cov_x) + retval[1:-1] + (mesg, info)
    else:
        return (retval[0], info)