code: def test_non_negative_tucker(init, hals, monkeypatch):
      """Test for non-negative Tucker"""
      tol_norm_2 = 10e-1
      tol_max_abs = 10e-1
      core, factors = random_tucker(
          (3, 4, 3), rank=[3, 4, 3], non_negative=True
      )
      tensor = tucker_to_tensor((core, factors))
      if hals:
          nn_method = non_negative_tucker_hals
      else:
          nn_method = non_negative_tucker
  
      nn_core, nn_factors = nn_method(
          tensor, rank=[3, 4, 3], init=init, n_iter_max=100
      )
  
      # Make sure all components are positive
      for factor in nn_factors:
          assert_(tl.all(factor >= 0))
      assert_(tl.all(nn_core >= 0))
  
      nn_reconstructed_tensor = tucker_to_tensor((nn_core, nn_factors))
      error = tl.norm(tensor - nn_reconstructed_tensor, 2)
      error /= tl.norm(tensor, 2)
      assert_(
          error < tol_norm_2,
          "norm 2 of reconstruction error higher than tol",
      )
  
      # Test the max abs difference between the reconstruction and the tensor
      assert_(
          tl.norm(tensor - nn_reconstructed_tensor, "inf")
          < tol_max_abs,
          "abs norm of reconstruction error higher than tol",
      )
  
      # Test for a single rank passed
      # (should be used for all modes)
      rank = 3
      target_shape = (rank,) * tl.ndim(tensor)
      core, factors = nn_method(tensor, rank=rank, n_iter_max=2)
      assert_(
          tl.shape(core) == target_shape,
          f"core has the wrong shape, got {tl.shape(core)}, but expected {target_shape}.",
      )
      for i, f in enumerate(factors):
          expected_shape = (tl.shape(tensor)[i], rank)
          assert_(
              tl.shape(f) == expected_shape,
              f"{i}-th factor has the wrong shape, got {tl.shape(f)}, but expected {expected_shape}.",
          )
  
      if hals:
          assert_class_wrapper_correctly_passes_arguments(
              monkeypatch,
              non_negative_tucker_hals,
              Tucker_NN_HALS,
              ignore_args={"return_errors"},
              rank=3,
          )
      else:
          assert_class_wrapper_correctly_passes_arguments(
              monkeypatch,
              non_negative_tucker,
              Tucker_NN,
              ignore_args={"return_errors"},
              rank=3,
          )
  
file_path: tensorly/tensorly/decomposition/tests/test_tucker.py
imports: import pytest
  import numpy
  import tensorly
  from .._tucker import tucker, partial_tucker, non_negative_tucker, non_negative_tucker_hals, Tucker, Tucker_NN, Tucker_NN_HALS
  from ...tucker_tensor import tucker_to_tensor
  from ...tenalg import multi_mode_dot
  from ...random import random_tucker
  from ...testing import assert_equal, assert_, assert_array_equal, assert_class_wrapper_correctly_passes_arguments
label_desc: Use image node_id 4 for calling a global function with example usage: test_non_negative_tucker(init, hals, monkeypatch) without return types