code: def test_phase_randomize():
      import numpy as np
      from scipy.fftpack import fft
      from scipy.stats import pearsonr
      from brainiak.utils.utils import phase_randomize
  
      data = np.repeat(
          np.repeat(
              np.random.randn(60)[:, np.newaxis, np.newaxis], 30, axis=1
          ),
          20,
          axis=2,
      )
      assert np.array_equal(data[..., 0], data[..., 1])
  
      # Phase-randomize data across subjects (same across voxels)
      shifted_data = phase_randomize(
          data, voxelwise=False, random_state=1
      )
      assert shifted_data.shape == data.shape
      assert not np.array_equal(
          shifted_data[..., 0], shifted_data[..., 1]
      )
      assert not np.array_equal(shifted_data[..., 0], data[..., 0])
  
      # Check that uneven n_TRs doesn't explode
      _ = phase_randomize(data[:-1, ...])
  
      # Check that random_state returns same shifts
      shifted_data_ = phase_randomize(
          data, voxelwise=False, random_state=1
      )
      assert np.array_equal(shifted_data, shifted_data_)
  
      shifted_data_ = phase_randomize(
          data, voxelwise=False, random_state=2
      )
      assert not np.array_equal(shifted_data, shifted_data_)
  
      # Phase-randomize subjects and voxels
      shifted_data = phase_randomize(
          data, voxelwise=True, random_state=1
      )
      assert shifted_data.shape == data.shape
      assert not np.array_equal(
          shifted_data[..., 0], shifted_data[..., 1]
      )
      assert not np.array_equal(shifted_data[..., 0], data[..., 0])
      assert not np.array_equal(
          shifted_data[:, 0, 0], shifted_data[:, 1, 0]
      )
  
      # Try with 2-dimensional input
      shifted_data = phase_randomize(
          data[..., 0], voxelwise=True, random_state=1
      )
      assert shifted_data.ndim == 2
      assert not np.array_equal(shifted_data[:, 0], shifted_data[:, 1])
  
      # Create correlated noisy data
      corr_data = np.repeat(
          np.random.randn(60)[:, np.newaxis, np.newaxis], 2, axis=2
      ) + np.random.randn(60, 1, 2)
  
      # Get correlation and frequency domain for data
      corr_r = pearsonr(corr_data[:, 0, 0], corr_data[:, 0, 1])[0]
      corr_freq = fft(corr_data, axis=0)
  
      # Phase-randomize time series and get correlation/frequency
      shifted_data = phase_randomize(corr_data)
      shifted_r = pearsonr(
          shifted_data[:, 0, 0], shifted_data[:, 0, 1]
      )[0]
      shifted_freq = fft(shifted_data, axis=0)
  
      # Check that phase-randomization reduces correlation
      assert np.abs(shifted_r) < np.abs(corr_r)
  
      # Check that amplitude spectrum is preserved
      assert np.allclose(np.abs(shifted_freq), np.abs(corr_freq))
  
file_path: brainiak/tests/utils/test_utils.py
imports: import pytest
label_desc: Use image node_id 9 for calling a global function with example usage: test_phase_randomize() without return types