Skip to content

Commit 5cf4574

Browse files
authored
Merge pull request #118 from theislab/fix_simulators
fixed missing methods and imports
2 parents 257a89b + e45bd95 commit 5cf4574

File tree

9 files changed

+102
-8
lines changed

9 files changed

+102
-8
lines changed
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
#from batchglm.models.glm_beta import InputDataGLM, Model, Simulator
2-
#from batchglm.train.tf2.glm_beta import Estimator
1+
from batchglm.models.glm_beta import InputDataGLM, Model, Simulator
2+
# from batchglm.train.tf2.glm_beta import Estimator

batchglm/api/models/tf2/glm_nb.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
from batchglm.models.glm_nb import InputDataGLM, Model, Simulator
2-
from batchglm.train.tf2.glm_nb import Estimator
2+
# from batchglm.train.tf2.glm_nb import Estimator
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
#from batchglm.models.glm_norm import InputDataGLM, Model, Simulator
2-
#from batchglm.train.tf2.glm_norm import Estimator
1+
from batchglm.models.glm_norm import InputDataGLM, Model, Simulator
2+
# from batchglm.train.tf2.glm_norm import Estimator

batchglm/models/glm_beta/external.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,6 @@
55
from batchglm.models.base_glm import closedform_glm_mean, closedform_glm_scale
66

77
import batchglm.data as data_utils
8-
from batchglm.utils.linalg import groupwise_solve_lm
8+
from batchglm.utils.linalg import groupwise_solve_lm
9+
10+
from batchglm import pkg_constants

batchglm/models/glm_beta/model.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,16 @@ def eta_loc(self) -> np.ndarray:
3232
assert False, "size factors not allowed"
3333
return eta
3434

35+
def eta_loc_j(self, j) -> np.ndarray:
36+
# Make sure that dimensionality of sliced array is kept:
37+
if isinstance(j, int) or isinstance(j, np.int32) or isinstance(j, np.int64):
38+
j = [j]
39+
eta = np.matmul(self.design_loc, self.a[:, j])
40+
if self.size_factors is not None:
41+
assert False, "size factors not allowed"
42+
eta = self.np_clip_param(eta, "eta_loc")
43+
return eta
44+
3545
# Re-parameterizations:
3646

3747
@property

batchglm/models/glm_beta/simulator.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from .model import Model
44
from .external import InputDataGLM, _SimulatorGLM
5+
from .external import pkg_constants
56

67

78
class Simulator(_SimulatorGLM, Model):
@@ -22,6 +23,42 @@ def __init__(
2223
num_features=num_features
2324
)
2425

26+
def param_bounds(
27+
self,
28+
dtype
29+
):
30+
31+
dtype = np.dtype(dtype)
32+
dmin = np.finfo(dtype).min
33+
dmax = np.finfo(dtype).max
34+
dtype = dtype.type
35+
36+
zero = np.nextafter(0, np.inf, dtype=dtype)
37+
one = np.nextafter(1, -np.inf, dtype=dtype)
38+
39+
sf = dtype(pkg_constants.ACCURACY_MARGIN_RELATIVE_TO_LIMIT)
40+
bounds_min = {
41+
"a_var": np.log(zero/(1-zero)) / sf,
42+
"b_var": np.log(zero) / sf,
43+
"eta_loc": np.log(zero/(1-zero)) / sf,
44+
"eta_scale": np.log(zero) / sf,
45+
"mean": np.nextafter(0, np.inf, dtype=dtype),
46+
"samplesize": np.nextafter(0, np.inf, dtype=dtype),
47+
"probs": dtype(0),
48+
"log_probs": np.log(zero),
49+
}
50+
bounds_max = {
51+
"a_var": np.log(one/(1-one)) / sf,
52+
"b_var": np.nextafter(np.log(dmax), -np.inf, dtype=dtype) / sf,
53+
"eta_loc": np.log(one/(1-one)) / sf,
54+
"eta_scale": np.nextafter(np.log(dmax), -np.inf, dtype=dtype) / sf,
55+
"mean": one,
56+
"samplesize": np.nextafter(dmax, -np.inf, dtype=dtype) / sf,
57+
"probs": dtype(1),
58+
"log_probs": dtype(0),
59+
}
60+
return bounds_min, bounds_max
61+
2562
def generate_params(
2663
self,
2764
rand_fn_ave=lambda shape: np.random.uniform(0.2, 0.8, shape),

batchglm/models/glm_norm/external.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,6 @@
55
from batchglm.models.base_glm import closedform_glm_mean, closedform_glm_scale
66

77
import batchglm.data as data_utils
8-
from batchglm.utils.linalg import groupwise_solve_lm
8+
from batchglm.utils.linalg import groupwise_solve_lm
9+
10+
from batchglm import pkg_constants

batchglm/models/glm_norm/model.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,18 @@ def eta_loc(self) -> np.ndarray:
3232
eta *= np.expand_dims(self.size_factors, axis=1)
3333
return eta
3434

35+
def eta_loc_j(self, j) -> np.ndarray:
36+
# Make sure that dimensionality of sliced array is kept:
37+
if isinstance(j, int) or isinstance(j, np.int32) or isinstance(j, np.int64):
38+
j = [j]
39+
eta = np.matmul(self.design_loc, self.a[:, j])
40+
if self.size_factors is not None:
41+
eta *= np.expand_dims(self.size_factors, axis=1)
42+
eta = self.np_clip_param(eta, "eta_loc")
43+
return eta
44+
3545
# Re-parameterizations:
36-
46+
3747
@property
3848
def mean(self) -> np.ndarray:
3949
return self.location

batchglm/models/glm_norm/simulator.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from .model import Model
44
from .external import InputDataGLM, _SimulatorGLM
5+
from .external import pkg_constants
56

67

78
class Simulator(_SimulatorGLM, Model):
@@ -22,6 +23,38 @@ def __init__(
2223
num_features=num_features
2324
)
2425

26+
def param_bounds(
27+
self,
28+
dtype
29+
):
30+
dtype = np.dtype(dtype)
31+
dmin = np.finfo(dtype).min
32+
dmax = np.finfo(dtype).max
33+
dtype = dtype.type
34+
35+
sf = dtype(pkg_constants.ACCURACY_MARGIN_RELATIVE_TO_LIMIT)
36+
bounds_min = {
37+
"a_var": np.nextafter(-dmax, np.inf, dtype=dtype) / sf,
38+
"b_var": np.log(np.nextafter(0, np.inf, dtype=dtype)) / sf,
39+
"eta_loc": np.nextafter(-dmax, np.inf, dtype=dtype) / sf,
40+
"eta_scale": np.log(np.nextafter(0, np.inf, dtype=dtype)) / sf,
41+
"mean": np.nextafter(-dmax, np.inf, dtype=dtype) / sf,
42+
"sd": np.nextafter(0, np.inf, dtype=dtype),
43+
"probs": dtype(0),
44+
"log_probs": np.log(np.nextafter(0, np.inf, dtype=dtype)),
45+
}
46+
bounds_max = {
47+
"a_var": np.nextafter(dmax, -np.inf, dtype=dtype) / sf,
48+
"b_var": np.nextafter(np.log(dmax), -np.inf, dtype=dtype) / sf,
49+
"eta_loc": np.nextafter(dmax, -np.inf, dtype=dtype) / sf,
50+
"eta_scale": np.nextafter(np.log(dmax), -np.inf, dtype=dtype) / sf,
51+
"mean": np.nextafter(dmax, -np.inf, dtype=dtype) / sf,
52+
"sd": np.nextafter(dmax, -np.inf, dtype=dtype) / sf,
53+
"probs": dtype(1),
54+
"log_probs": dtype(0),
55+
}
56+
return bounds_min, bounds_max
57+
2558
def generate_params(
2659
self,
2760
rand_fn_ave=lambda shape: np.random.uniform(10, 1000, shape),

0 commit comments

Comments
 (0)