code: def test_parafac2_normalize_factors():
      rng = tl.check_random_state(1234)
      rank = 2  # Rank 2 so we only need to test rank of minimum and maximum
  
      random_parafac2_tensor = random_parafac2(
          shapes=[(15 + rng.randint(5), 30) for _ in range(25)],
          rank=rank,
          random_state=rng,
      )
      random_parafac2_tensor.factors[0] = (
          random_parafac2_tensor.factors[0] + 0.1
      )
      norms = tl.ones(rank)
      for factor in random_parafac2_tensor.factors:
          norms = norms * tl.norm(factor, axis=0)
  
      slices = parafac2_to_tensor(random_parafac2_tensor)
  
      unnormalized_rec = parafac2(
          slices,
          rank,
          random_state=rng,
          normalize_factors=False,
          n_iter_max=2,
      )
      assert unnormalized_rec.weights[0] == 1
  
      normalized_rec = parafac2(
          slices, rank, random_state=rng, normalize_factors=True
      )
  
      assert_array_almost_equal(
          T.norm(normalized_rec.factors[0], axis=0), tl.ones(rank)
      )
      assert_array_almost_equal(
          T.norm(normalized_rec.factors[1], axis=0), tl.ones(rank)
      )
      assert_array_almost_equal(
          T.norm(normalized_rec.factors[2], axis=0), tl.ones(rank)
      )
      assert (
          abs(tl.max(norms) - tl.max(normalized_rec.weights))
          / tl.max(norms)
          < 0.05
      )
      assert (
          abs(tl.min(norms) - tl.min(normalized_rec.weights))
          / tl.min(norms)
          < 0.05
      )
  
file_path: tensorly/tensorly/decomposition/tests/test_parafac2.py
imports: import itertools
  import numpy
  import pytest
  import tensorly
  from ...random import random_parafac2
  from ...None import backend
  from ...testing import assert_, assert_class_wrapper_correctly_passes_arguments, assert_array_almost_equal, assert_allclose
  from .._parafac2 import Parafac2, parafac2, initialize_decomposition, _BroThesisLineSearch
  from ...parafac2_tensor import Parafac2Tensor, parafac2_to_tensor, parafac2_to_slices
  from ...metrics.factors import congruence_coefficient
label_desc: Use image node_id 6 for calling a global function with example usage: test_parafac2_normalize_factors() without return types