code: def test_validate_parafac2_tensor():
      rng = tl.check_random_state(12345)
      true_shape = [(4, 5)] * 3
      true_rank = 2
      weights, factors, projections = random_parafac2(
          true_shape, rank=true_rank
      )
  
      # Check shape and rank returned
      shape, rank = _validate_parafac2_tensor(
          (weights, factors, projections)
      )
      assert_equal(
          shape,
          true_shape,
          err_msg=f"Returned incorrect shape (got {shape}, expected {true_shape})",
      )
      assert_equal(
          rank,
          true_rank,
          err_msg=f"Returned incorrect rank (got {rank}, expected {true_rank})",
      )
  
      # One of the factors has the wrong rank
      for mode in range(3):
          false_shape = (tl.shape(factors[mode])[0], true_rank + 1)
          factors[mode], copy = (
              tl.tensor(rng.random_sample(false_shape)),
              factors[mode],
          )
          with assert_raises(ValueError):
              _validate_parafac2_tensor((weights, factors, projections))
  
          factors[mode] = copy
  
      # Not three factor matrices
      with assert_raises(ValueError):
          _validate_parafac2_tensor((weights, factors[1:], projections))
  
      # Not enough projections
      with assert_raises(ValueError):
          _validate_parafac2_tensor((weights, factors, projections[1:]))
  
      # Wrong number of weights
      with assert_raises(ValueError):
          _validate_parafac2_tensor((weights[1:], factors, projections))
  
      # The projections aren't orthogonal
      false_projections = [
          tl.tensor(rng.random_sample(tl.shape(P))) for P in projections
      ]
      with assert_raises(ValueError):
          _validate_parafac2_tensor(
              (weights, factors, false_projections)
          )
  
file_path: tensorly/tensorly/tests/test_parafac2_tensor.py
imports: import numpy
  import pytest
  from ..None import backend
  from ..base import unfold, tensor_to_vec
  from ..parafac2_tensor import parafac2_to_tensor, parafac2_to_unfolded, parafac2_to_vec, _validate_parafac2_tensor, parafac2_to_slice, parafac2_to_slices, parafac2_normalise, apply_parafac2_projections
  from ..testing import assert_array_equal, assert_array_almost_equal, assert_equal, assert_raises
  from ..random import random_parafac2
label_desc: Use image node_id 1 for calling a global function with example usage: test_validate_parafac2_tensor() without return types