Skip to content

Commit 63276c0

Browse files
committed
up
1 parent d8b603a commit 63276c0

1 file changed

Lines changed: 40 additions & 2 deletions

File tree

ya_glm/utils.py

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import numpy as np
22
from copy import deepcopy
3-
from inspect import signature
4-
3+
from numbers import Number
54

65
def get_sequence_decr_max(max_val=1, min_val_mult=1e-3, num=20,
76
spacing='lin', decr=True):
@@ -180,3 +179,42 @@ def get_coef_and_intercept(est, copy=False, error=False):
180179
else:
181180
return coef, intercept
182181

182+
183+
def maybe_add(d, **kws):
184+
"""
185+
Adds keywork argumnts to a dict if their values are not None.
186+
"""
187+
for k, v in kws.items():
188+
if v is not None:
189+
d[k] = v
190+
191+
return d
192+
193+
194+
def clip_zero(x, zero_tol=1e-8):
195+
"""
196+
Sets x or the elements of x to zero when they are very small.
197+
198+
Parameters
199+
----------
200+
x: Number, array-like
201+
The value or values to clip.
202+
203+
zero_tol: float
204+
The tolerance below which we declare a number to be zero.
205+
206+
Output
207+
------
208+
x_clipped: Number or np.array
209+
210+
"""
211+
if isinstance(x, Number):
212+
if abs(x) <= zero_tol:
213+
return 0
214+
else:
215+
return x
216+
217+
x_ = np.zeros_like(x)
218+
non_zero_mask = abs(np.array(x)) > zero_tol
219+
x_[non_zero_mask] = x[non_zero_mask]
220+
return x_

0 commit comments

Comments
 (0)