def test_to_ndim_1(self): for v in [ 429., [429.], [[429.]], np.array(429.), np.array([429.]), np.array([[429.]]) ]: r = npu.to_ndim_1(v) self.assertTrue(checks.is_numpy_array(r)) self.assertEqual(np.shape(r), (1, )) npt.assert_almost_equal(r, np.array([429.])) for v in [[429., 5.], [[429., 5.]], [[[429.], [5.]]], np.array([429., 5.]), np.array([[429., 5.]]), np.array([[[429.], [5.]]])]: r = npu.to_ndim_1(v) self.assertTrue(checks.is_numpy_array(r)) self.assertEqual(np.shape(r), (2, )) npt.assert_almost_equal(r, np.array([429., 5.])) for v in [[429., 5., 2., 14.], [[429., 5., 2., 14.]], [[429., 5.], [2., 14.]], [[429.], [5.], [2.], [14.]], np.array([429., 5., 2., 14.]), np.array([[429., 5., 2., 14.]]), np.array([[429., 5.], [2., 14.]]), np.array([[[429.], [5.], [2.], [14.]]])]: r = npu.to_ndim_1(v) self.assertTrue(checks.is_numpy_array(r)) self.assertEqual(np.shape(r), (4, )) npt.assert_almost_equal(r, np.array([429., 5., 2., 14.])) npt.assert_equal(npu.to_ndim_1(None), np.array([None])) a = np.array([2., 14.]) b = npu.to_ndim_1(a, copy=False) b[1] = 42. npt.assert_almost_equal(b, np.array([2., 42.])) npt.assert_almost_equal(a, np.array([2., 42.])) a = [2., 14.] b = npu.to_ndim_1(a, copy=False) b[1] = 42. npt.assert_almost_equal(b, np.array([2., 42.])) npt.assert_almost_equal(a, np.array([2., 14.])) a = [2., 14.] b = npu.to_ndim_1(a, copy=True) b[1] = 42. npt.assert_almost_equal(b, np.array([2., 42.])) npt.assert_almost_equal(a, np.array([2., 14.]))
def immutable_copy_of(arg): if checks.is_numpy_array(arg): result = np.copy(arg) if arg.flags.writeable else arg else: result = np.array(arg) result.flags.writeable = False return result
def sign(arg): if isinstance(arg, dt.timedelta): arg = arg.total_seconds() elif checks.is_numpy_array( arg) and arg.dtype == object and np.size(arg) > 0 and isinstance( arg.item(0), dt.timedelta): arg = np.vectorize(lambda x: x.total_seconds())(arg) return np.sign(arg)
def to_scalar(arg, raise_value_error=True): try: if checks.is_float(arg): return arg elif checks.is_numpy_array(arg): return np.asscalar(arg) else: return np.asscalar(np.array(arg)) except: if raise_value_error: raise return arg
def __init__(self, obs_matrix): super().__init__() if not checks.is_numpy_array(obs_matrix) and not checks.is_iterable(obs_matrix): obs_matrix = (obs_matrix,) self._obs_matrix = npu.make_immutable( block_diag( *[npu.to_ndim_2(om, ndim_1_to_col=False, copy=False) for om in obs_matrix])) self._to_string_helper_KalmanFilterObsModel = None self._str_KalmanFilterObsModel = None
def are_views_of_same(arg1, arg2): if not checks.is_numpy_array(arg1) or not checks.is_numpy_array(arg2): return False return (arg1.base is arg2) or (arg2.base is arg1) or ( (arg1.base is arg2.base) and arg1.base is not None)
def is_view_of(arg1, arg2): if not checks.is_numpy_array(arg1) or not checks.is_numpy_array(arg2): return False return arg1.base is arg2