|
| 1 | +import abc |
| 2 | +from typing import Any, Callable, Dict, Optional, Tuple, Union |
| 3 | + |
| 4 | +import dask.array |
| 5 | +import numpy as np |
| 6 | + |
| 7 | +from .external import ModelGLM |
| 8 | + |
| 9 | + |
| 10 | +class Model(ModelGLM, metaclass=abc.ABCMeta): |
| 11 | + """ |
| 12 | + Generalized Linear Model (GLM) with Poisson noise. |
| 13 | + """ |
| 14 | + |
| 15 | + def link_loc(self, data) -> Union[np.ndarray, dask.array.core.Array]: |
| 16 | + return np.log(data) |
| 17 | + |
| 18 | + def inverse_link_loc(self, data) -> Union[np.ndarray, dask.array.core.Array]: |
| 19 | + return np.exp(data) |
| 20 | + |
| 21 | + def link_scale(self, data) -> Union[np.ndarray, dask.array.core.Array]: |
| 22 | + return np.log(data) |
| 23 | + |
| 24 | + def inverse_link_scale(self, data) -> Union[np.ndarray, dask.array.core.Array]: |
| 25 | + return np.exp(data) |
| 26 | + |
| 27 | + @property |
| 28 | + def eta_loc(self) -> Union[np.ndarray, dask.array.core.Array]: |
| 29 | + eta = np.matmul(self.design_loc, self.theta_location_constrained) |
| 30 | + if self.size_factors is not None: |
| 31 | + eta += self.size_factors |
| 32 | + eta = self.np_clip_param(eta, "eta_loc") |
| 33 | + return eta |
| 34 | + |
| 35 | + def eta_loc_j(self, j) -> Union[np.ndarray, dask.array.core.Array]: |
| 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.theta_location_constrained[:, j]) |
| 40 | + if self.size_factors is not None: |
| 41 | + eta += self.size_factors |
| 42 | + eta = self.np_clip_param(eta, "eta_loc") |
| 43 | + return eta |
| 44 | + |
| 45 | + # Re-parameterizations: |
| 46 | + |
| 47 | + @property |
| 48 | + def lam(self) -> Union[np.ndarray, dask.array.core.Array]: |
| 49 | + return self.location |
| 50 | + |
| 51 | + # param constraints: |
| 52 | + |
| 53 | + def bounds(self, sf, dmax, dtype) -> Tuple[Dict[str, Any], Dict[str, Any]]: |
| 54 | + |
| 55 | + bounds_min = { |
| 56 | + "theta_location": np.log(np.nextafter(0, np.inf, dtype=dtype)) / sf, |
| 57 | + "eta_loc": np.log(np.nextafter(0, np.inf, dtype=dtype)) / sf, |
| 58 | + "loc": np.nextafter(0, np.inf, dtype=dtype), |
| 59 | + "scale": np.nextafter(0, np.inf, dtype=dtype), |
| 60 | + "likelihood": dtype(0), |
| 61 | + "ll": np.log(np.nextafter(0, np.inf, dtype=dtype)), |
| 62 | + # Not used and should be removed: https://github.com/theislab/batchglm/issues/148 |
| 63 | + "theta_scale": np.log(np.nextafter(0, np.inf, dtype=dtype)) / sf, |
| 64 | + "eta_scale": np.log(np.nextafter(0, np.inf, dtype=dtype)) / sf, |
| 65 | + } |
| 66 | + bounds_max = { |
| 67 | + "theta_location": np.nextafter(np.log(dmax), -np.inf, dtype=dtype) / sf, |
| 68 | + "eta_loc": np.nextafter(np.log(dmax), -np.inf, dtype=dtype) / sf, |
| 69 | + "loc": np.nextafter(dmax, -np.inf, dtype=dtype) / sf, |
| 70 | + "scale": np.nextafter(dmax, -np.inf, dtype=dtype) / sf, |
| 71 | + "likelihood": dtype(1), |
| 72 | + "ll": dtype(0), |
| 73 | + # Not used and should be removed: https://github.com/theislab/batchglm/issues/148 |
| 74 | + "theta_scale": np.log(dmax) / sf, |
| 75 | + "eta_scale": np.log(dmax) / sf, |
| 76 | + |
| 77 | + } |
| 78 | + return bounds_min, bounds_max |
| 79 | + |
| 80 | + # simulator: |
| 81 | + |
| 82 | + @property |
| 83 | + def rand_fn_ave(self) -> Optional[Callable]: |
| 84 | + return lambda shape: np.random.poisson(500, shape) + 1 |
| 85 | + |
| 86 | + @property |
| 87 | + def rand_fn(self) -> Optional[Callable]: |
| 88 | + return lambda shape: np.abs(np.random.uniform(0.5, 2, shape)) |
| 89 | + |
| 90 | + @property |
| 91 | + def rand_fn_loc(self) -> Optional[Callable]: |
| 92 | + return None |
| 93 | + |
| 94 | + @property |
| 95 | + def rand_fn_scale(self) -> Optional[Callable]: |
| 96 | + return None |
| 97 | + |
| 98 | + def generate_data(self) -> np.ndarray: |
| 99 | + """ |
| 100 | + Sample random data based on poisson distribution and parameters. |
| 101 | + """ |
| 102 | + return np.random.poisson(lam=self.lam) |
0 commit comments