-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
179 additions
and
43 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
|
||
test: | ||
pytest tests/* | ||
pytest -W ignore::DeprecationWarning tests/* | ||
|
||
cov: | ||
pytest --cov-report html --cov-report term-missing --cov=lantern tests/ | ||
pytest -W ignore::DeprecationWarning --cov-report html --cov-report term-missing --cov=lantern tests/ |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from lantern.module import Module |
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
from lantern.loss.loss import Loss, Term | ||
from lantern.loss.elbo_gp import ELBO_GP |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
from lantern.model.surface.surface import Surface | ||
from lantern.model.surface.phenotype import Phenotype |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from torch import nn | ||
import attr | ||
|
||
|
||
@attr.s() | ||
class Module(nn.Module): | ||
"""A base module for lantern components | ||
This module is necessary to play nicely b/w attrs and | ||
pytorch. Some discussion is available here: | ||
https://github.com/python-attrs/attrs/issues/393#issuecomment-510148031 | ||
""" | ||
|
||
def __attrs_pre_init__(self): | ||
# torch module is initialized before assigning attributes | ||
nn.Module.__init__(self) |
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,96 @@ | ||
from gpytorch.models import ApproximateGP | ||
from gpytorch.variational import CholeskyVariationalDistribution | ||
from gpytorch.variational import VariationalStrategy | ||
from gpytorch.distributions import MultivariateNormal, MultitaskMultivariateNormal | ||
from gpytorch.variational import IndependentMultitaskVariationalStrategy | ||
from gpytorch.means import ConstantMean, Mean | ||
from gpytorch.kernels import Kernel, ScaleKernel, RQKernel | ||
import pandas as pd | ||
import numpy as np | ||
import torch | ||
|
||
from lantern.model.surface import Phenotype | ||
from lantern.loss import ELBO_GP | ||
from lantern.dataset import Dataset | ||
|
||
|
||
def test_1d(): | ||
|
||
induc = torch.rand(100, 10) | ||
phen = Phenotype(1, induc) | ||
|
||
assert type(phen.variational_strategy) == VariationalStrategy | ||
|
||
mvn = phen(torch.rand(50, 10)) | ||
assert type(mvn) == MultivariateNormal | ||
assert mvn.mean.shape == (50,) | ||
|
||
assert np.allclose(induc.numpy(), phen._get_induc()) | ||
|
||
induc = torch.rand(100, 10) | ||
phen._set_induc(induc.numpy()) | ||
assert np.allclose(induc.numpy(), phen._get_induc()) | ||
|
||
|
||
def test_multid(): | ||
|
||
induc = torch.rand(100, 10) | ||
phen = Phenotype(4, induc) | ||
|
||
assert type(phen.variational_strategy) == IndependentMultitaskVariationalStrategy | ||
|
||
mvn = phen(torch.rand(50, 10)) | ||
assert type(mvn) == MultitaskMultivariateNormal | ||
assert mvn.mean.shape == (50, 4) | ||
|
||
assert np.allclose(induc.numpy(), phen._get_induc()) | ||
|
||
induc = torch.rand(100, 10) | ||
phen._set_induc(induc.numpy()) | ||
assert np.allclose(induc.numpy(), phen._get_induc()) | ||
|
||
|
||
def test_loss(): | ||
|
||
induc = torch.rand(100, 10) | ||
phen = Phenotype(1, induc) | ||
loss = phen.loss(N=1000) | ||
assert type(loss) == ELBO_GP | ||
|
||
mvn = phen(torch.randn(50, 10)) | ||
|
||
lss = loss(mvn, torch.randn(50)) | ||
assert "neg-loglikelihood" in lss | ||
assert "neg-log-gp-prior" in lss | ||
assert "gp-kl" in lss | ||
|
||
|
||
def test_ds_construct_1d(): | ||
|
||
df = pd.DataFrame( | ||
{"substitutions": ["a1b", "c2d"], "phenotype": [0.0, 1.0], "error": [0.1, 0.2],} | ||
) | ||
ds = Dataset(df) | ||
phen = Phenotype.fromDataset(ds, 10) | ||
|
||
assert phen.K == 10 | ||
assert phen.D == 1 | ||
|
||
|
||
def test_ds_construct_multid(): | ||
|
||
df = pd.DataFrame( | ||
{ | ||
"substitutions": ["a1b", "c2d"], | ||
"p1": [0.0, 1.0], | ||
"p2": [1.0, 0.0], | ||
"e1": [0.1, 0.2], | ||
"e2": [0.2, 0.1], | ||
} | ||
) | ||
|
||
ds = Dataset(df, phenotypes=["p1", "p2"], errors=["e1", "e2"]) | ||
phen = Phenotype.fromDataset(ds, 10) | ||
|
||
assert phen.K == 10 | ||
assert phen.D == 2 |