python类allclose()的实例源码

test_fields.py 文件源码 项目:pyfds 作者: emtpb 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def test_field_component_boundary_1():
    fc = fls.FieldComponent(100)
    fc.values = np.random.rand(100)
    fc.boundaries = [reg.Boundary(reg.LineRegion([5, 6, 7], [0, 0.2], 'test boundary'))]
    fc.boundaries[0].value = 23
    fc.apply_bounds(step=0)
    assert np.allclose(fc.values[[5, 6, 7]], [23, 23, 23])
test_fields.py 文件源码 项目:pyfds 作者: emtpb 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def test_field_component_boundary_2():
    fc = fls.FieldComponent(100)
    fc.values = np.ones(100)
    fc.boundaries = [reg.Boundary(reg.LineRegion([5, 6, 7], [0, 0.2], 'test boundary'))]
    fc.boundaries[0].value = [23, 42, 23]
    fc.boundaries[0].additive = True
    fc.apply_bounds(step=0)
    assert np.allclose(fc.values[[5, 6, 7]], [24, 43, 24])
test_fields.py 文件源码 项目:pyfds 作者: emtpb 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def test_field_component_output():
    fc = fls.FieldComponent(100)
    fc.outputs = [reg.Output(reg.LineRegion([0, 1, 2], [0, 0.2], 'test output'))]
    fc.write_outputs()
    fc.write_outputs()
    assert np.allclose(fc.outputs[0].signals, [[0, 0], [0, 0], [0, 0]])
    assert np.allclose(fc.outputs[0].mean_signal, np.zeros(2))
test_fields.py 文件源码 项目:pyfds 作者: emtpb 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 0
def test_field1d_init():
    # create a field where the main material is 5
    fld = fls.Field1D(100, 0.1, 100, 0.1, int(5))
    # check if the "material parameter" 'real' for the complete field is 5
    assert np.allclose(fld.material_vector('real'), 5)
test_fields.py 文件源码 项目:pyfds 作者: emtpb 项目源码 文件源码 阅读 33 收藏 0 点赞 0 评论 0
def test_field1d_d_x2():
    fld = fls.Field1D(3, 1, 3, 1, 5)
    assert np.allclose(fld.d_x2().toarray(), [[-2, 1, 0], [1, -2, 1], [0, 1, -2]])
test_fields.py 文件源码 项目:pyfds 作者: emtpb 项目源码 文件源码 阅读 29 收藏 0 点赞 0 评论 0
def test_field2d_init():
    # create a field where the main material is 5
    fld = fls.Field2D(100, 0.1, 100, 0.1, 100, 0.1, int(5))
    # check if the "material parameter" 'real' for the complete field is 5
    assert np.allclose(fld.material_vector('real'), 5)
    assert np.size(fld.material_vector('real')) == 10000
test_fields.py 文件源码 项目:pyfds 作者: emtpb 项目源码 文件源码 阅读 52 收藏 0 点赞 0 评论 0
def test_field2d_d_x():
    fld = fls.Field2D(2, 1, 2, 1, 10, 1, int(5))
    assert np.allclose(fld.d_x().toarray(), [[-1, 1, 0, 0], [0, -1, 1, 0],
                                             [0, 0, -1, 1], [0, 0, 0, -1]])
    assert np.allclose(fld.d_x(variant='backward').toarray(), [[1, 0, 0, 0], [-1, 1, 0, 0],
                                                               [0, -1, 1, 0], [0, 0, -1, 1]])
    assert np.allclose(fld.d_x(variant='central').toarray(), [[0, 0.5, 0, 0], [-0.5, 0, 0.5, 0],
                                                              [0, -0.5, 0, 0.5], [0, 0, -0.5, 0]])
test_fields.py 文件源码 项目:pyfds 作者: emtpb 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def test_field2d_d_x2():
    fld = fls.Field2D(2, 1, 2, 1, 10, 1, int(5))
    assert np.allclose(fld.d_x2().toarray(), [[-2, 1, 0, 0], [1, -2, 1, 0],
                                              [0, 1, -2, 1], [0, 0, 1, -2]])
test_fields.py 文件源码 项目:pyfds 作者: emtpb 项目源码 文件源码 阅读 29 收藏 0 点赞 0 评论 0
def test_field2d_d_y():
    fld = fls.Field2D(2, 1, 2, 1, 10, 1, int(5))
    assert np.allclose(fld.d_y().toarray(), [[-1, 0, 1, 0], [0, -1, 0, 1],
                                             [0, 0, -1, 0], [0, 0, 0, -1]])
    assert np.allclose(fld.d_y(variant='backward').toarray(), [[1, 0, 0, 0], [0, 1, 0, 0],
                                                               [-1, 0, 1, 0], [0, -1, 0, 1]])
    assert np.allclose(fld.d_y(variant='central').toarray(), [[0, 0, 0.5, 0], [0, 0, 0, 0.5],
                                                              [-0.5, 0, 0, 0], [0, -0.5, 0, 0]])
test_fields.py 文件源码 项目:pyfds 作者: emtpb 项目源码 文件源码 阅读 37 收藏 0 点赞 0 评论 0
def test_field1d_get_position():
    fld = fls.Field1D(4, 0.1, 1, 1, int(5))
    assert np.allclose(fld.get_position(fld.get_index(0.1)), 0.1)
test_fields.py 文件源码 项目:pyfds 作者: emtpb 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 0
def test_field2d_get_position():
    fld = fls.Field2D(4, 0.1, 3, 0.1, 1, 1, int(5))
    assert np.allclose(fld.get_position(fld.get_index((0.2, 0.1))), (0.2, 0.1))
test_fields.py 文件源码 项目:pyfds 作者: emtpb 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def test_field1d_get_line_region():
    fld = fls.Field1D(4, 0.1, 1, 1, int(5))
    fld.material_regions.append(reg.MaterialRegion(fld.get_line_region((0.1, 0.2)), int(23)))
    assert np.allclose(fld.material_vector('real'), [5, 23, 23, 5])
test_fields.py 文件源码 项目:pyfds 作者: emtpb 项目源码 文件源码 阅读 26 收藏 0 点赞 0 评论 0
def test_field2d_get_line_region():
    fld = fls.Field2D(3, 1, 4, 0.5, 1, 1, int(5))
    region = fld.get_line_region((1, 0, 1, 1.5))
    assert np.allclose(region.indices, [1, 4, 7, 10])
    region = fld.get_line_region((0, 0, 2, 0))
    assert np.allclose(region.indices, [0, 1, 2])
    region = fld.get_line_region((0, 0, 2, 1.5))
    assert np.allclose(region.indices, [0, 4, 7, 11])
    region = fld.get_line_region((0, 1.5, 2, 0))
    assert np.allclose(region.indices, [9, 7, 4, 2])
test_fields.py 文件源码 项目:pyfds 作者: emtpb 项目源码 文件源码 阅读 30 收藏 0 点赞 0 评论 0
def test_field2d_get_rect_region():
    fld = fls.Field2D(3, 1, 4, 0.5, 1, 1, int(5))
    region = fld.get_rect_region((0, 0, 1, 1))
    assert np.allclose(region.indices, [0, 3, 6, 1, 4, 7])
    region = fld.get_rect_region((2, 1.5, -1, -1))
    assert np.allclose(region.indices, [4, 7, 10, 5, 8, 11])
test_regions.py 文件源码 项目:pyfds 作者: emtpb 项目源码 文件源码 阅读 19 收藏 0 点赞 0 评论 0
def test_output():
    out = reg.Output(reg.LineRegion([0, 1, 2], [0, 0.2], 'test output'))
    out.signals = [np.linspace(0, 1) for _ in range(len(out.region.indices))]
    assert np.allclose(out.mean_signal, np.linspace(0, 1))
serving_test.py 文件源码 项目:treecat 作者: posterior 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def test_server_marginals(N, V, C, M):
    model = generate_fake_model(N, V, C, M)
    config = TINY_CONFIG.copy()
    config['model_num_clusters'] = M
    model['config'] = config
    server = TreeCatServer(model)

    # Evaluate on random data.
    table = generate_dataset(N, V, C)['table']
    marginals = server.marginals(table.data)
    for v in range(V):
        beg, end = table.ragged_index[v:v + 2]
        totals = marginals[:, beg:end].sum(axis=1)
        assert np.allclose(totals, 1.0)
serving_test.py 文件源码 项目:treecat 作者: posterior 项目源码 文件源码 阅读 32 收藏 0 点赞 0 评论 0
def test_ensemble_latent_correlation(N, V, C, M):
    set_random_seed(make_seed(N, V, C, M))
    ensemble = generate_fake_ensemble(N, V, C, M)
    server = EnsembleServer(ensemble)

    correlation = server.latent_correlation()
    print(correlation)
    assert np.all(0 <= correlation)
    assert np.all(correlation <= 1)
    assert np.allclose(correlation, correlation.T)
    for v in range(V):
        assert correlation[v, :].argmax() == v
        assert correlation[:, v].argmax() == v
sgcrf.py 文件源码 项目:sgcrfpy 作者: dswah 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def active_set_Lam(self, fixed, vary):
        grad = self.grad_wrt_Lam(fixed, vary)
        assert np.allclose(grad, grad.T, 1e-3)
        return np.where((np.abs(np.triu(grad)) > self.lamL) | (self.Lam != 0))
        # return np.where((np.abs(grad) > self.lamL) | (~np.isclose(self.Lam, 0)))
test_fit.py 文件源码 项目:sgcrfpy 作者: dswah 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def test_theta_0():
    rng.seed(0)
    n_samples = 100
    Y = rng.randn(n_samples, 5)
    X = rng.randn(n_samples, 5)

    sgcrf = SparseGaussianCRF(lamL=0.01, lamT=0.01)
    sgcrf.fit(X, Y)

    assert np.allclose(sgcrf.Lam, np.eye(5), .1, .2)
test_stats.py 文件源码 项目:npstreams 作者: LaurentRDC 项目源码 文件源码 阅读 26 收藏 0 点赞 0 评论 0
def test_trivial(self):
        """ Test iaverage on stream of zeroes """
        stream = repeat(np.zeros( (64,64), dtype = np.float ), times = 5)
        for av in iaverage(stream):
            self.assertTrue(np.allclose(av, np.zeros_like(av)))


问题


面经


文章

微信
公众号

扫码关注公众号