Exemplo n.º 1
0
def test_power__default_matrices__should_compute_correctly():
    A = sp.sparray((2, 2), default=2.0)
    B = sp.sparray((2, 2), default=3.0)

    C = A**B

    assert C[0, 0] == 8.0
    assert C[0, 1] == 8.0
    assert C[1, 0] == 8.0
    assert C[1, 1] == 8.0
Exemplo n.º 2
0
def test_add__two_matrices_with_default_values__should_be_sum_of_default_values(
):
    A = sp.sparray((3, 3), default=2.52)
    B = sp.sparray((3, 3), default=7.33)

    C = A + B

    assert C[0, 0] == 2.52 + 7.33
    assert C[0, 2] == 2.52 + 7.33
    assert C[1, 0] == 2.52 + 7.33
    assert C[1, 1] == 2.52 + 7.33
    assert C[2, 2] == 2.52 + 7.33
Exemplo n.º 3
0
def test_frobenius_norm__custom_matrices__should_be_equal_to_result_for_dense_matrices(
):
    A = sp.sparray((3, 3), default=0.0)
    B = sp.sparray((3, 3), default=7.0)

    A[0, 0] = 27
    A[1, 0] = 9
    A[2, 2] = 45

    B[2, 2] = 4500

    actual = ((A - B) * (A - B)).sum()
    expected = np.linalg.norm(A.dense() - B.dense(), 'fro')**2

    assert np.allclose(actual, expected, rtol=1e-13, atol=1e-15)
Exemplo n.º 4
0
def test_init__default_arguments__should_set_properties_correctly():
    shape = (2, 7, 9)

    s = sp.sparray(shape)

    assert s.shape == (2, 7, 9)
    assert s.ndim == 3
    assert s.dtype == float
Exemplo n.º 5
0
def test_delitem__indices_within_shape__should_work_correctly():
    s = sp.sparray((25, 14))

    s[5, 2] = 87.5

    del s[5, 2]

    assert s[5, 2] == 0.0
Exemplo n.º 6
0
    def __init__(self, llabel='', rlabel='', lshape=[], rshape=[], lmins=[], rmins=[], lmaxs=[],  rmaxs=[], lcircular=[], rcircular=[], default=0.0, dtype=float):

        self.LDistrib= Distrib(llabel, lshape, lmins, lmaxs, lcircular)
        self.RDistrib=Distrib(rlabel, rshape, rmins, rmaxs, rcircular)

        self.probs=sp.sparray(lshape+rshape,default,dtype)

        self.shape=self.probs.shape
Exemplo n.º 7
0
def test_mod__default_matrices__should_compute_correctly():
    A = sp.sparray((3, 3))
    B = sp.sparray((3, 3), default=2.0)

    A[0, 0] = 5

    C = A % B

    assert C[0, 0] == 1.0
    assert C[0, 1] == 0.0
    assert C[0, 2] == 0.0
    assert C[1, 0] == 0.0
    assert C[1, 1] == 0.0
    assert C[1, 2] == 0.0
    assert C[2, 0] == 0.0
    assert C[2, 1] == 0.0
    assert C[2, 2] == 0.0
Exemplo n.º 8
0
def test_iadd__default_matrix__should_double_when_add_itself():
    A = sp.sparray((2, 2), default=1.3)

    A += A

    assert A[0, 0] == 2.6
    assert A[0, 1] == 2.6
    assert A[1, 0] == 2.6
    assert A[1, 1] == 2.6
Exemplo n.º 9
0
    def Build(self):

        data = dict()
        for x in range(self.X.GetNumElements()*self.U.GetNumElements()):
            res = self.StepBuild(x)
            data.update(res)
        proba=sp.sparray(self.probs.shape,0.0,float,data)
        print "Nb of non zero proba: ",len(data)
        self.probs.SetData(proba)
Exemplo n.º 10
0
def test_dense__custom_matrix__should_be_equiv_to_ndarray():
    A = sp.sparray((2, 2), default=1.1)

    A[0, 0] = 25.0
    A[1, 0] = 7.5
    A[1, 1] = 9.2

    desired = np.array([[25.0, 1.1], [7.5, 9.2]])

    assert np.allclose(A.dense(), desired, rtol=1e-13, atol=1e-15)
Exemplo n.º 11
0
def test_mult__two_custom_matrices__should_multiply_elementwise():
    A = sp.sparray((3, 3), default=1.1)
    B = sp.sparray((3, 3), default=2.5)

    A[0, 0] = 3.5
    A[1, 0] = 5.7
    B[2, 2] = 8.9
    B[0, 0] = 2.1

    C = A * B

    assert C[0, 0] == 3.5 * 2.1
    assert C[0, 1] == 1.1 * 2.5
    assert C[0, 2] == 1.1 * 2.5
    assert C[1, 0] == 5.7 * 2.5
    assert C[1, 1] == 1.1 * 2.5
    assert C[1, 2] == 1.1 * 2.5
    assert C[2, 0] == 1.1 * 2.5
    assert C[2, 1] == 1.1 * 2.5
    assert C[2, 2] == 1.1 * 8.9
Exemplo n.º 12
0
def test_getitem_setitem__indices_within_shape__should_work_correctly():
    A = sp.sparray((3, 3))

    A[0, 0] = 10.0
    assert A[0, 0] == 10.0

    A[2, 1] = 47.98
    assert A[2, 1] == 47.98

    A[0, 2] = -1.32
    assert A[0, 2] == -1.32
Exemplo n.º 13
0
def test_add__two_custom_matrices__should_be_sum():
    A = sp.sparray((3, 3), default=2.52)
    B = sp.sparray((3, 3), default=7.33)

    A[0, 2] = 1.5
    A[1, 1] = 3.0
    B[0, 0] = 8.3
    B[0, 2] = 0.3

    C = A + B

    assert C[0, 0] == 2.52 + 8.30
    assert C[0, 1] == 2.52 + 7.33
    assert C[0, 2] == 1.80
    assert C[1, 0] == 2.52 + 7.33
    assert C[1, 1] == 3.00 + 7.33
    assert C[1, 2] == 2.52 + 7.33
    assert C[2, 0] == 2.52 + 7.33
    assert C[2, 1] == 2.52 + 7.33
    assert C[2, 2] == 2.52 + 7.33
Exemplo n.º 14
0
def test_sub__two_custom_matrics__should_subtract_correctly():
    A = sp.sparray((3, 3), default=4.32)
    B = sp.sparray((3, 3), default=1.10)

    A[0, 0] = 7.83
    B[0, 0] = 10.32

    B[1, 2] = 3.3

    C = A - B

    assert C[0, 0] == 7.83 - 10.32
    assert C[0, 1] == 4.32 - 1.10
    assert C[0, 2] == 4.32 - 1.10
    assert C[1, 0] == 4.32 - 1.10
    assert C[1, 1] == 4.32 - 1.10
    assert C[1, 2] == 4.32 - 3.30
    assert C[2, 0] == 4.32 - 1.10
    assert C[2, 1] == 4.32 - 1.10
    assert C[2, 2] == 4.32 - 1.10
Exemplo n.º 15
0
def test_div__two_custom_matrics__should_divide_elementwise():
    A = sp.sparray((3, 3), default=1.1)
    B = sp.sparray((3, 3), default=2.5)

    A[0, 0] = 3.5
    A[1, 0] = 5.7
    B[2, 2] = 8.9
    B[0, 0] = 2.1

    C = A / B

    assert C[0, 0] == 3.5 / 2.1
    assert C[0, 1] == 1.1 / 2.5
    assert C[0, 2] == 1.1 / 2.5
    assert C[1, 0] == 5.7 / 2.5
    assert C[1, 1] == 1.1 / 2.5
    assert C[1, 2] == 1.1 / 2.5
    assert C[2, 0] == 1.1 / 2.5
    assert C[2, 1] == 1.1 / 2.5
    assert C[2, 2] == 1.1 / 8.9
Exemplo n.º 16
0
def test_mult__A_times_A_with_default_value__should_square_default_value():
    A = sp.sparray((2, 1), default=7)

    C = A * A

    assert C[0, 0] == 7**2
    assert C[0, 1] == 7**2
    assert C[0, 2] == 7**2
    assert C[1, 0] == 7**2
    assert C[1, 1] == 7**2
    assert C[1, 2] == 7**2
    assert C[2, 0] == 7**2
    assert C[2, 1] == 7**2
    assert C[2, 2] == 7**2
Exemplo n.º 17
0
    def ParallelBuild(self):


        if self.StepBuild == None:
            print "Please define StepBuild"
            return False

        print "building distrib"

        manager = Manager()
        shareddict=manager.dict()


        core_worker = cpu_count()
        print "Creating %d workers"%(core_worker)


        #cut the total range of element into subranges for each worker
        split_range=np.array_split(np.array(range(self.X.GetNumElements()*self.U.GetNumElements())),core_worker)

        #create workers
        workers = [Process(target=self.ProcessWorker, args=(i, shareddict, split_range[i])) for i in range(core_worker)]


        for each in workers:
            # print "Starting Worker"
            each.start()

        print "Waiting for process..."
        for each in workers:
            each.join()
            print "\tJoin() done"

        proba=sp.sparray(self.probs.shape,0.0,float,shareddict)
        # print shareddict
        print "Nb of non zero proba: ",len(shareddict)
        self.probs.SetData(proba)
Exemplo n.º 18
0
edge_types = list(edge_types)

graphs = []
k = 0
for par in sents_par:
    k += 1
    print(k)
    s = sents_par[par]
    if any(i >= len(sents) for i in s):
        break
    graph_sents = [sent_graphs[i] for i in s]
    graph_verts = [sent_nodes[i] for i in s]
    verts = []
    for i in range(len(graph_verts)):
        for j in range(len(graph_verts[i])):
            verts.append(graph_verts[i][j] + "-" + str(i))
    adj = np.zeros((len(verts), len(verts), len(edge_types) + 1))
    for i in range(len(graph_sents)):
        for (v1, v2, label) in graph_sents[i].edges.data("label"):
            label = edge_types.index(label)
            v1_ind = sum(map(len, graph_verts[:i])) + graph_verts[i].index(v1)
            v2_ind = sum(map(len, graph_verts[:i])) + graph_verts[i].index(v2)
            adj[v1_ind, v2_ind, label] = 1.0
    for i in range(len(graph_sents) - 1):
        adj[sum(map(len, graph_verts[:i])),
            sum(map(len, graph_verts[:i + 1])),
            len(edge_types)] = 1.0
    graphs.append((verts, sparray.sparray(adj.shape, adj)))

pickle.dump(graphs, open("graphs.pcl", "w+"))
Exemplo n.º 19
0
def test_init__different_default_value__should_set_for_all_elements():
    x = sp.sparray((3, 3), default=7)

    assert x[2, 2] == 7
    assert x[0, 2] == 7
    assert x[1, 1] == 7
Exemplo n.º 20
0
def test_sum__default_matrix__should_compute_sum_correctly():
    A = sp.sparray((2, 2), default=1.3)

    assert A.sum() == 2 * 2 * 1.3
Exemplo n.º 21
0
def test_sum__custom_matrix__should_compute_sum_correctly():
    A = sp.sparray((2, 2), default=1.3)

    A[0, 1] = 99.0

    assert A.sum() == (2 * 2 - 1) * 1.3 + 99.0
Exemplo n.º 22
0
def test_dense__default_matrix__should_be_equiv_to_ndarray():
    A = sp.sparray((2, 2), default=1.1)

    desired = np.array([[1.1, 1.1], [1.1, 1.1]])

    assert np.allclose(A.dense(), desired, rtol=1e-13, atol=1e-15)