Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup API / models / simulators / estimators / unittests #130

Merged
merged 37 commits into from
Mar 7, 2022
Merged
Changes from 1 commit
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
365bcab
linting using black / isort, fixed imports
picciama Feb 23, 2022
425f0a6
separate properties from input_data
picciama Feb 23, 2022
1535856
major refactoring removing train/models:
picciama Feb 24, 2022
eb1e551
refactoring numpy estimator:
picciama Feb 25, 2022
fd06d26
cleanup: working fitting procedure:
picciama Feb 25, 2022
b25b315
code reformatting with black / isort and linting
picciama Feb 25, 2022
da23c4f
remove unnecessary compute()
picciama Feb 25, 2022
23ee162
add missing decorators
picciama Feb 25, 2022
01a4574
override theta_loc/scale in training
picciama Feb 28, 2022
fd50d5c
unittests cleanup
picciama Mar 1, 2022
d5b0721
support InputDataGLM when simulating
picciama Mar 1, 2022
66fe624
removed @dask_compute
picciama Mar 1, 2022
784c28e
reintroduced compute()
picciama Mar 1, 2022
f1ae683
cleanup, fix imports and syntax errors
picciama Mar 1, 2022
a3cb3c4
fixed imports, api and type annotations using mypy
picciama Mar 1, 2022
8385c87
reformatting using black
picciama Mar 1, 2022
c186b3d
cleanup using flake8, mypy, black, isort
picciama Mar 1, 2022
2e2c755
added type hints everywhere / fixed mypy issues
picciama Mar 3, 2022
cfaf056
reformatting using black / isort
picciama Mar 3, 2022
04d0e5f
Merge branch 'development' into mp/integrate_simulator
picciama Mar 3, 2022
6a76eb8
reformatting using black / isort
picciama Mar 3, 2022
611e2af
corrected data utils import
picciama Mar 4, 2022
dc63263
better documentation for simuation functions
picciama Mar 4, 2022
b493b54
refactored modelContainer.py -> model_container,py
picciama Mar 4, 2022
12e5909
make model_container attribute in parent estimator
picciama Mar 4, 2022
3767ea4
flake8 / black / isort reformatting
picciama Mar 4, 2022
bd4a619
bugfix: correctly set attribute model_container
picciama Mar 4, 2022
408f83b
bugfix: generate -> generate_artificial_data
picciama Mar 4, 2022
89f1d84
added plotting submodule
picciama Mar 4, 2022
64f53ff
reformatted using black
picciama Mar 4, 2022
6c6d84f
added plotting library
picciama Mar 4, 2022
c0e6cdf
reformatted using black / isort
picciama Mar 4, 2022
00d0dcd
also check python 3.7
picciama Mar 4, 2022
18ebb90
removed python 3.7
picciama Mar 4, 2022
5396eaa
updated numpy to >=1.22.2 to pass safety checks
picciama Mar 4, 2022
6e68734
remove python 3.7
picciama Mar 4, 2022
6048230
fix flake8-bandit issue with newest version:
picciama Mar 4, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
reformatting using black / isort
picciama committed Mar 3, 2022
commit cfaf056d7bc617e4f3b10c87c90916105cddce72
1 change: 1 addition & 0 deletions batchglm/models/glm_beta/model.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import abc
from typing import Any, Callable, Dict, Optional, Tuple, Union

import dask

try:
1 change: 1 addition & 0 deletions batchglm/models/glm_norm/model.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import abc
from typing import Any, Callable, Dict, Optional, Tuple, Union

import dask

try:
8 changes: 4 additions & 4 deletions batchglm/train/numpy/base_glm/estimator.py
Original file line number Diff line number Diff line change
@@ -165,9 +165,9 @@ def train(
# Reverse update by feature if update leads to worse loss:
ll_proposal = -self.model_container.ll_byfeature_j(j=idx_update)
idx_bad_step = idx_update[np.where(ll_proposal > ll_current[idx_update])[0]]

theta_scale_new = self.model_container.theta_scale.compute()

theta_scale_new[:, idx_bad_step] = theta_scale_new[:, idx_bad_step] - b_step[:, idx_bad_step]
self.model_container.theta_scale = theta_scale_new
else:
@@ -190,9 +190,9 @@ def train(
# Reverse update by feature if update leads to worse loss:
ll_proposal = -self.model_container.ll_byfeature_j(j=idx_update)
idx_bad_step = idx_update[np.where(ll_proposal > ll_current[idx_update])[0]]

theta_location_new = self.model_container.theta_location.compute()

theta_location_new[:, idx_bad_step] = theta_location_new[:, idx_bad_step] - a_step[:, idx_bad_step]
self.model_container.theta_location = theta_location_new
else:
10 changes: 3 additions & 7 deletions batchglm/train/numpy/base_glm/modelContainer.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import abc
from typing import Callable, Union, Any
from typing import Any, Callable, Union

import dask.array
import numpy as np
@@ -257,10 +257,7 @@ def hessian_location_scale(self) -> Union[np.ndarray, dask.array.core.Array]:
:return: (features x inferred param x inferred param)
"""
w = self.hessian_weight_location_scale
return np.einsum(
"fob,oc->fbc",
np.einsum("ob,of->fob", self.xh_loc, w), self.xh_scale
)
return np.einsum("fob,oc->fbc", np.einsum("ob,of->fob", self.xh_loc, w), self.xh_scale)

@property
@abc.abstractmethod
@@ -358,8 +355,7 @@ def ll(self) -> Union[np.ndarray, dask.array.core.Array]:
def ll_j(self, j) -> Union[np.ndarray, dask.array.core.Array]:
pass


@property # type: ignore
@property # type: ignore
@dask_compute
def ll_byfeature(self) -> np.ndarray:
return np.sum(self.ll, axis=0)
5 changes: 2 additions & 3 deletions batchglm/train/numpy/glm_nb/modelContainer.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Union, Callable
from typing import Callable, Union

import dask
import numpy as np
@@ -8,7 +8,6 @@


class ModelContainer(BaseModelContainer):

@property
def fim_weight(self):
raise NotImplementedError("This method is currently unimplemented as it isn't used by any built-in procedures.")
@@ -54,7 +53,7 @@ def jac_weight(self):
@property
def jac_weight_j(self):
raise NotImplementedError("This method is currently unimplemented as it isn't used by any built-in procedures.")

@property
def jac_weight_scale(self) -> Union[np.ndarray, dask.array.core.Array]:
"""
5 changes: 2 additions & 3 deletions tests/numpy/test_accuracy_extreme_values.py
Original file line number Diff line number Diff line change
@@ -15,9 +15,7 @@ class _TestAccuracyXtremeAll(TestAccuracy):
Test whether numerical extremes throw error in initialisation or during first training steps.
"""

def _test_accuracy_extreme_values(
self, idx: Union[List[int], int, np.ndarray], val: float, noise_model: str
):
def _test_accuracy_extreme_values(self, idx: Union[List[int], int, np.ndarray], val: float, noise_model: str):
model = get_generated_model(noise_model=noise_model, num_conditions=2, num_batches=4, sparse=False, mode=None)
model._x[:, idx] = val
estimator = get_estimator(noise_model=noise_model, model=model, init_location="standard", init_scale="standard")
@@ -74,5 +72,6 @@ def test_beta(self) -> bool:
# self._test_zero_variance(noise_model="beta")
return True


if __name__ == "__main__":
unittest.main()
16 changes: 10 additions & 6 deletions tests/numpy/utils.py
Original file line number Diff line number Diff line change
@@ -2,25 +2,27 @@

import numpy as np

from batchglm.models.base_glm import _ModelGLM
from batchglm.models.glm_beta import Model as BetaModel
from batchglm.models.glm_nb import Model as NBModel
from batchglm.models.glm_norm import Model as NormModel
from batchglm.models.base_glm import _ModelGLM
# from batchglm.train.numpy.glm_beta import Estimator as BetaEstimator
from batchglm.train.numpy.glm_nb import Estimator as NBEstimator

# from batchglm.train.numpy.glm_norm import Estimator as NormEstimator
from batchglm.train.numpy.base_glm import EstimatorGlm

# from batchglm.train.numpy.glm_beta import Estimator as BetaEstimator
from batchglm.train.numpy.glm_nb import Estimator as NBEstimator


def get_estimator(noise_model: str, **kwargs) -> EstimatorGlm:
if noise_model == "nb":
return NBEstimator(**kwargs)
elif noise_model == "norm":
raise NotImplementedError("Norm Estimator is not yet implemented.")
#estimator = NormEstimator(**kwargs)
# estimator = NormEstimator(**kwargs)
elif noise_model == "beta":
raise NotImplementedError("Beta Estimator is not yet implemented.")
#estimator = BetaEstimator(**kwargs)
# estimator = BetaEstimator(**kwargs)
raise ValueError(f"Noise model {noise_model} not recognized.")


@@ -36,7 +38,9 @@ def get_model(noise_model: str) -> _ModelGLM:
raise ValueError(f"Noise model {noise_model} not recognized.")


def get_generated_model(noise_model: str, num_conditions: int, num_batches: int, sparse: bool, mode: Optional[str] = None) -> _ModelGLM:
def get_generated_model(
noise_model: str, num_conditions: int, num_batches: int, sparse: bool, mode: Optional[str] = None
) -> _ModelGLM:
model = get_model(noise_model=noise_model)

def random_uniform(low: float, high: float):