MCPcopy Create free account
hub / github.com/PythonOT/POT / test_LazyTensor

Function test_LazyTensor

test/test_utils.py:499–536  ·  view source on GitHub ↗
(nx)

Source from the content-addressed store, hash-verified

497
498
499def test_LazyTensor(nx):
500 n1 = 100
501 n2 = 200
502 shape = (n1, n2)
503
504 rng = np.random.RandomState(42)
505 x1 = rng.randn(n1, 2)
506 x2 = rng.randn(n2, 2)
507
508 x1, x2 = nx.from_numpy(x1, x2)
509
510 # i,j can be integers or slices, x1,x2 have to be passed as keyword arguments
511 def getitem(i, j, x1, x2):
512 return nx.dot(x1[i], x2[j].T)
513
514 # create a lazy tensor
515 T = ot.utils.LazyTensor((n1, n2), getitem, x1=x1, x2=x2)
516
517 assert T.shape == (n1, n2)
518 assert str(T) == "LazyTensor(shape=(100, 200),attributes=(x1,x2))"
519
520 assert T.x1 is x1
521 assert T.x2 is x2
522
523 # get the full tensor (not lazy)
524 assert T[:].shape == shape
525
526 # get one component
527 assert T[1, 1] == nx.dot(x1[1], x2[1].T)
528
529 # get one row
530 assert T[1].shape == (n2,)
531
532 # get one column with slices
533 assert T[::10, 5].shape == (10,)
534
535 with pytest.raises(NotImplementedError):
536 T["error"]
537
538
539def test_OTResult_LazyTensor(nx):

Callers

nothing calls this directly

Calls 3

from_numpyMethod · 0.80
randnMethod · 0.45
dotMethod · 0.45

Tested by

no test coverage detected