def cmscr1d_img_pb(img: np.array, alpha0: float, alpha1: float, alpha2: float, alpha3: float, beta: float, deriv='mesh') \ -> (np.array, np.array, float, float, bool): """Computes the L2-H1 mass conserving flow with source for a 1D image sequence with spatio-temporal and convective regularisation with periodic spatial boundary. Args: img (np.array): 1D image sequence of shape (m, n), where m is the number of time steps and n is the number of pixels. alpha0 (float): The spatial regularisation parameter for v. alpha1 (float): The temporal regularisation parameter for v. alpha2 (float): The spatial regularisation parameter for k. alpha3 (float): The temporal regularisation parameter for k. beta (float): The convective regularisation parameter. deriv (str): Specifies how to approximate pertial derivatives. When set to 'mesh' it uses FEniCS built in function. Returns: v (np.array): A velocity array of shape (m, n). k (np.array): A source array of shape (m, n). res (float): The residual. fun (float): The function value. converged (bool): True if Newton's method converged. """ # Check for valid arguments. valid = {'mesh'} if deriv not in valid: raise ValueError("Argument 'deriv' must be one of %r." % valid) # Create mesh. m, n = img.shape mesh = UnitSquareMesh(m - 1, n - 1) # Define function space. V = dh.create_function_space(mesh, 'periodic') W = dh.create_vector_function_space(mesh, 'periodic') # Convert array to function. f = Function(V) f.vector()[:] = dh.img2funvec_pb(img) # Compute partial derivatives. ft, fx = f.dx(0), f.dx(1) # Compute velocity. v, k, res, fun, converged = cmscr1d_weak_solution(W, f, ft, fx, alpha0, alpha1, alpha2, alpha3, beta) # Convert back to array and return. v = dh.funvec2img(v.vector().get_local(), m, n) k = dh.funvec2img(k.vector().get_local(), m, n) return v, k, res, fun, converged
def cm1d_img_pb(img: np.array, alpha0: float, alpha1: float, deriv='mesh') -> np.array: """Computes the L2-H1 mass conserving flow for a 1D image sequence with periodic spatial boundary. Allows to specify how to approximate partial derivatives of f numerically. Note that the last column of img is ignored. Args: img (np.array): 1D image sequence of shape (m, n), where m is the number of time steps and n is the number of pixels. alpha0 (float): Spatial regularisation parameter. alpha1 (float): Temporal regularisation parameter. deriv (str): Specifies how to approximate pertial derivatives. When set to 'mesh' it uses FEniCS built in function. Returns: v (np.array): A velocity array of shape (m, n). res (float): The residual. func (float): The value of the functional. """ # Check for valid arguments. valid = {'mesh'} if deriv not in valid: raise ValueError("Argument 'deriv' must be one of %r." % valid) # Create mesh. m, n = img.shape mesh = UnitSquareMesh(m - 1, n - 1) # Define function space. V = dh.create_function_space(mesh, 'periodic') # Convert array to function. f = Function(V) f.vector()[:] = dh.img2funvec_pb(img) # Compute partial derivatives. ft, fx = f.dx(0), f.dx(1) # Compute velocity. v, res, fun = cm1d_weak_solution(V, f, ft, fx, alpha0, alpha1) # Convert to array and return. return dh.funvec2img_pb(v.vector().get_local(), m, n), res, fun
def test_img2funvec_pb(self): m, n = 3, 4 img = np.ones((m, n)) v = dh.img2funvec_pb(img) np.testing.assert_allclose(v, np.ones(m * (n - 1)))
def test_img2fun_fun2img_pb(self): m, n = 7, 13 img = np.random.rand(m, n) v = dh.img2funvec_pb(img) np.testing.assert_allclose( dh.funvec2img_pb(v, m, n)[:, 1:], img[:, 1:])