-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29 from datasig-ac-uk/fix/from-jax-array-issues
Fix/from jax array issues
- Loading branch information
Showing
4 changed files
with
179 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
|
||
|
||
import pytest | ||
|
||
try: | ||
import jax | ||
from jax import numpy as jnp | ||
except ImportError: | ||
jax = None | ||
jnp = None | ||
|
||
import roughpy as rp | ||
import numpy as np | ||
from numpy.testing import assert_array_equal | ||
|
||
@pytest.mark.skipif(jax is None, reason="JAX test require jax to be installed") | ||
class TestJaxArrayCreation: | ||
|
||
@pytest.fixture(scope="class") | ||
def context(self): | ||
return rp.get_context(2, 2, rp.SPReal) | ||
|
||
@pytest.fixture(scope="class") | ||
def prng_key(self): | ||
return jax.random.PRNGKey(12345) | ||
|
||
def test_increment_stream_from_jax_array(self): | ||
array = jnp.array([ | ||
[-0.25860816, -0.36977386, 0.6619457, -0.50442713, 0.08028925, -1.06701028], | ||
[-0.26208243, 0.22464547, 0.39521545, -0.62663144, -0.34344956, -1.67293704], | ||
[-0.55824, -0.19376263, 0.86616075, -0.58314389, -0.69254208, -1.53291035], | ||
[-0.52306908, -0.09234464, 1.17564034, -0.7388621, -0.91333717, -1.50844121], | ||
[-0.80696738, -0.09417236, 0.75135314, -1.20548987, -1.42038512, -1.86834741], | ||
[-0.6642682, -0.12166289, 1.04914618, -1.01415539, -1.58841276, -2.54356289] | ||
]) | ||
|
||
stream = rp.LieIncrementStream.from_increments(np.array(array), width=6, depth=2, dtype="float32") | ||
|
||
lsig01 = stream.log_signature(rp.RealInterval(0, 1)) | ||
lsig12 = stream.log_signature(rp.RealInterval(1, 2)) | ||
lsig24 = stream.log_signature(rp.RealInterval(2, 4)) | ||
lsig46 = stream.log_signature(rp.RealInterval(4, 6)) | ||
|
||
assert_array_equal(np.array(lsig01)[:6], array[0, :]) | ||
assert not np.any(np.isnan(lsig12)) | ||
assert not np.any(np.isnan(lsig24)) | ||
assert not np.any(np.isnan(lsig46)) | ||
|
||
|
||
|
||
@pytest.mark.xfail(condition=True, reason="No device support is currently available") | ||
def test_create_tensor_from_jax_array(self, prng_key, context): | ||
array = jax.random.uniform(prng_key, shape=(context.tensor_size(2),), dtype="float32", minval=-1.0, maxval=1.0) | ||
|
||
ts = rp.FreeTensor(array, ctx=context) | ||
|
||
assert_array_equal(np.array(ts), np.array(jax.device_get(array), copy=True)) |