def test_generic(): from jina.types.ndarray.generic import NdArray from scipy.sparse import coo_matrix row = np.array([0, 3, 1, 0]) col = np.array([0, 3, 1, 2]) data = np.array([4, 5, 7, 9]) a = coo_matrix((data, (row, col)), shape=(4, 4)) dense_a = a.toarray() b = NdArray(a, is_sparse=True) assert b.is_sparse dense_b = b.value.toarray() assert b.is_sparse np.testing.assert_equal(dense_b, dense_a) c = np.random.random([10, 3, 4]) # without change of `is_sparse`, this should raise error with pytest.raises(AttributeError): b.value = c b.is_sparse = False b.value = c np.testing.assert_equal(b.value, c)
def test_ndarray_get_set(field): a = Document() b = np.random.random([10, 10]) setattr(a, field, b) np.testing.assert_equal(getattr(a, field), b) b = np.random.random([10, 10]) c = NdArray() c.value = b setattr(a, field, c) np.testing.assert_equal(getattr(a, field), b) b = np.random.random([10, 10]) c = NdArray() c.value = b setattr(a, field, c._pb_body) np.testing.assert_equal(getattr(a, field), b)
def test_array2pb(): # i don't understand why is this set? # os env should be available to that process-context only if 'JINA_ARRAY_QUANT' in os.environ: print(f'quant is on: {os.environ["JINA_ARRAY_QUANT"]}') del os.environ['JINA_ARRAY_QUANT'] d = NdArray() d.value = e4 np.testing.assert_almost_equal(d.value, e4)
def test_array_protobuf_conversions_with_quantize(quantize, proto_type): random_array = np.random.rand(random.randrange(0, 50), random.randrange(0, 20)).astype(proto_type) d = NdArray(quantize=quantize) d.value = random_array np.testing.assert_almost_equal(d.value, random_array, decimal=2)
def test_array_protobuf_conversions(proto_type): random_array = np.random.rand(random.randrange(0, 50), random.randrange(0, 20)).astype(proto_type) d = NdArray() d.value = random_array np.testing.assert_almost_equal(d.value, random_array)