-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* rm untested feature * add tests; rm deprecated features * fix formatting
- Loading branch information
1 parent
f2a2b09
commit 291a1a3
Showing
15 changed files
with
341 additions
and
99 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 was deleted.
Oops, something went wrong.
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
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
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,99 @@ | ||
import pytest | ||
import torch | ||
|
||
from nos.physics import ( | ||
HelmholtzDomainMSE, | ||
HelmholtzDomainResidual, | ||
) | ||
|
||
|
||
@pytest.fixture(scope="function") | ||
def wave_1d(): | ||
n_obs = 13 | ||
n_eval = 31 | ||
|
||
x = torch.rand(n_obs, n_eval, 1) | ||
x.requires_grad = True | ||
|
||
ks = 10 * torch.rand(n_obs).reshape(-1, 1, 1) | ||
|
||
u = torch.sin(ks * x) | ||
|
||
return x, u, ks | ||
|
||
|
||
@pytest.fixture(scope="function") | ||
def wave_1d_wrong(): | ||
n_obs = 13 | ||
n_eval = 31 | ||
|
||
x = torch.rand(n_obs, n_eval, 1) | ||
x.requires_grad = True | ||
|
||
ks = 10 * torch.rand(n_obs).reshape(-1, 1, 1) | ||
|
||
u = torch.sin(ks * 1.1 * x) | ||
|
||
return x, u, ks | ||
|
||
|
||
class TestHelmholtzDomainResidual: | ||
def test_can_initialize(self): | ||
res = HelmholtzDomainResidual() | ||
|
||
assert isinstance(res, HelmholtzDomainResidual) | ||
|
||
def test_can_forward(self, wave_1d): | ||
x, u, ks = wave_1d | ||
|
||
res = HelmholtzDomainResidual() | ||
res_val = res(x, u, ks) | ||
|
||
assert isinstance(res_val, torch.Tensor) | ||
|
||
def test_forward_correct(self, wave_1d): | ||
x, u, ks = wave_1d | ||
|
||
res = HelmholtzDomainResidual() | ||
res_val = res(x, u, ks) | ||
|
||
assert torch.allclose(res_val, torch.zeros(res_val.shape), atol=1e-5) | ||
|
||
def test_forward_wrong(self, wave_1d_wrong): | ||
x, u, ks = wave_1d_wrong | ||
|
||
res = HelmholtzDomainResidual() | ||
res_val = res(x, u, ks) | ||
|
||
assert not torch.any(torch.isclose(res_val, torch.zeros(res_val.shape), atol=1e-5)) | ||
|
||
|
||
class TestHelmholtzDomainMSE: | ||
def test_can_initialize(self): | ||
res = HelmholtzDomainMSE() | ||
|
||
assert isinstance(res, HelmholtzDomainMSE) | ||
|
||
def test_can_forward(self, wave_1d): | ||
x, u, ks = wave_1d | ||
|
||
res = HelmholtzDomainMSE() | ||
res_val = res(x, u, ks) | ||
|
||
assert isinstance(res_val, torch.Tensor) | ||
|
||
def test_forward_correct(self, wave_1d): | ||
x, u, ks = wave_1d | ||
|
||
res = HelmholtzDomainMSE() | ||
res_val = res(x, u, ks) | ||
|
||
assert res_val < 1e-8 | ||
|
||
def test_forward_wrong(self, wave_1d_wrong): | ||
x, u, ks = wave_1d_wrong | ||
|
||
res = HelmholtzDomainMSE() | ||
res_val = res(x, u, ks) | ||
|
||
assert res_val > 1.0 |
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,50 @@ | ||
import pytest | ||
import torch | ||
|
||
from nos.physics import ( | ||
Laplace, | ||
) | ||
|
||
|
||
@pytest.fixture(scope="function") | ||
def derivative_pair(): | ||
xx, yy = torch.meshgrid( | ||
torch.linspace(-1, 1, 100), | ||
torch.linspace(-1, 1, 100), | ||
) | ||
x = torch.stack([xx.flatten(), yy.flatten()], dim=-1) | ||
x = x.unsqueeze(0) # 1 observation | ||
x.requires_grad = True | ||
u = torch.sin(x[:, :, 0]).unsqueeze(-1) | ||
return x, u | ||
|
||
|
||
class TestLaplace: | ||
def test_can_initialize(self): | ||
_ = Laplace() | ||
|
||
assert True | ||
|
||
def test_can_forward(self, derivative_pair): | ||
x, u = derivative_pair | ||
|
||
laplace = Laplace() | ||
lpl_u = laplace(x, u) | ||
|
||
assert isinstance(lpl_u, torch.Tensor) | ||
|
||
def test_forward_shape_correct(self, derivative_pair): | ||
x, u = derivative_pair | ||
|
||
laplace = Laplace() | ||
lpl_u = laplace(x, u) | ||
|
||
assert u.shape == lpl_u.shape | ||
|
||
def test_forward_correct(self, derivative_pair): | ||
x, u = derivative_pair | ||
|
||
laplace = Laplace() | ||
lpl_u = laplace(x, u) | ||
|
||
assert torch.allclose(lpl_u, -u) |
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,50 @@ | ||
import torch | ||
|
||
from nos.physics import ( | ||
WeightSchedulerLinear, | ||
) | ||
|
||
|
||
class TestWeightSchedulerLin: | ||
def test_can_initialize(self): | ||
scheduler = WeightSchedulerLinear(1, 2) | ||
|
||
assert isinstance(scheduler, WeightSchedulerLinear) | ||
|
||
def test_data_weight_correct(self): | ||
scheduler = WeightSchedulerLinear(1, 2) | ||
|
||
epochs = torch.arange(0, 100) | ||
weight = scheduler._get_data_weight(epochs) | ||
|
||
assert isinstance(weight, torch.Tensor) | ||
assert torch.allclose(weight, torch.ones(weight.shape)) | ||
|
||
def test_pde_weight_correct(self): | ||
scheduler = WeightSchedulerLinear(10, 20) | ||
|
||
epochs = torch.arange(0, 100) | ||
weight = scheduler._get_pde_weight(epochs) | ||
|
||
assert torch.allclose(weight[0:10], torch.zeros(10)) | ||
assert torch.allclose(weight[20:], 1e-3 * torch.ones(80)) | ||
assert torch.allclose(weight[10:21], 1e-3 * (epochs[10:21] - 10.0) / 10.0) | ||
|
||
def test_forward_weight_correct(self): | ||
scheduler = WeightSchedulerLinear(10, 20) | ||
|
||
epochs = torch.arange(0, 100) | ||
weight = scheduler(epochs) | ||
|
||
assert torch.allclose(weight[0:10, 1], torch.zeros(10)) | ||
assert torch.allclose(weight[20:, 1], 1e-3 * torch.ones(80)) | ||
assert torch.allclose(weight[10:21, 1], 1e-3 * (epochs[10:21] - 10.0) / 10.0) | ||
|
||
assert torch.allclose(weight[:, 0], torch.ones(100)) | ||
|
||
def test_forward_float_weight_correct(self): | ||
scheduler = WeightSchedulerLinear(10, 20) | ||
|
||
weight = scheduler(15.0) | ||
|
||
assert torch.allclose(weight, torch.tensor([1.0, 5e-4])) |
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,36 @@ | ||
import torch | ||
|
||
from nos.transforms import ( | ||
CenterQuantileScaler, | ||
) | ||
|
||
|
||
class TestCenterQuantileScaler: | ||
x = torch.rand(7, 89, 13) | ||
y = torch.rand(17, 97, 13) | ||
|
||
def test_can_init(self): | ||
transform = CenterQuantileScaler(self.x) | ||
|
||
assert isinstance(transform, CenterQuantileScaler) | ||
|
||
def test_can_forward(self): | ||
transform = CenterQuantileScaler(self.x) | ||
|
||
out = transform(self.y) | ||
|
||
assert isinstance(out, torch.Tensor) | ||
|
||
def test_forward_centered(self): | ||
transform = CenterQuantileScaler(self.x) | ||
|
||
out = transform(self.y) | ||
|
||
assert torch.isclose(torch.median(out), torch.zeros(1), atol=1e-1) | ||
|
||
def test_can_undo(self): | ||
transform = CenterQuantileScaler(self.x) | ||
|
||
out = transform.undo(self.y) | ||
|
||
assert isinstance(out, torch.Tensor) |
Oops, something went wrong.