code: def test_isfc_nans():
      # Set parameters for toy time series data
      n_subjects = 20
      n_TRs = 60
      n_voxels = 30
      random_state = 42
  
      logger.info("Testing ISC options")
  
      data = simulated_timeseries(
          n_subjects,
          n_TRs,
          n_voxels=n_voxels,
          data_type="array",
          random_state=random_state,
      )
  
      # Inject NaNs into data
      data[0, 0, 0] = np.nan
  
      # Don't tolerate NaNs, should lose zeroeth voxel
      isfcs_loo = isfc(
          data,
          pairwise=False,
          vectorize_isfcs=False,
          tolerate_nans=False,
      )
      assert np.sum(np.isnan(isfcs_loo)) == n_subjects * (
          n_voxels * 2 - 1
      )
  
      # With vectorized ISFCs
      isfcs_loo, iscs_loo = isfc(
          data,
          pairwise=False,
          vectorize_isfcs=True,
          tolerate_nans=False,
      )
      assert np.sum(np.isnan(isfcs_loo)) == n_subjects * (n_voxels - 1)
  
      # Tolerate all NaNs, only subject with NaNs yields NaN
      isfcs_loo = isfc(
          data,
          pairwise=False,
          vectorize_isfcs=False,
          tolerate_nans=True,
      )
      assert np.sum(np.isnan(isfcs_loo)) == n_voxels * 2 - 1
  
      isfcs_loo, iscs_loo = isfc(
          data, pairwise=False, vectorize_isfcs=True, tolerate_nans=True
      )
      assert np.sum(np.isnan(isfcs_loo)) == n_voxels - 1
  
      # Pairwise approach shouldn't care
      isfcs_pw_T = isfc(
          data, pairwise=True, vectorize_isfcs=False, tolerate_nans=True
      )
      isfcs_pw_F = isfc(
          data,
          pairwise=True,
          vectorize_isfcs=False,
          tolerate_nans=False,
      )
      assert np.allclose(isfcs_pw_T, isfcs_pw_F, equal_nan=True)
      assert (
          np.sum(np.isnan(isfcs_pw_T))
          == np.sum(np.isnan(isfcs_pw_F))
          == (n_voxels * 2 - 1) * (n_subjects - 1)
      )
  
      isfcs_pw_T, iscs_pw_T = isfc(
          data, pairwise=True, vectorize_isfcs=True, tolerate_nans=True
      )
      isfcs_pw_F, iscs_pw_T = isfc(
          data, pairwise=True, vectorize_isfcs=True, tolerate_nans=False
      )
      assert np.allclose(isfcs_pw_T, isfcs_pw_F, equal_nan=True)
      assert (
          np.sum(np.isnan(isfcs_pw_T))
          == np.sum(np.isnan(isfcs_pw_F))
          == (n_voxels - 1) * (n_subjects - 1)
      )
  
      # Set proportion of nans to reject (70% and 90% non-NaN)
      data[0, 0, :] = np.nan
      data[0, 1, : n_subjects - int(n_subjects * 0.7)] = np.nan
      data[0, 2, : n_subjects - int(n_subjects * 0.9)] = np.nan
  
      isfcs_loo_T = isfc(
          data,
          pairwise=False,
          vectorize_isfcs=False,
          tolerate_nans=True,
      )
      isfcs_loo_F = isfc(
          data,
          pairwise=False,
          vectorize_isfcs=False,
          tolerate_nans=False,
      )
      isfcs_loo_95 = isfc(
          data,
          pairwise=False,
          vectorize_isfcs=False,
          tolerate_nans=0.95,
      )
      isfcs_loo_90 = isfc(
          data,
          pairwise=False,
          vectorize_isfcs=False,
          tolerate_nans=0.90,
      )
      isfcs_loo_80 = isfc(
          data, pairwise=False, vectorize_isfcs=False, tolerate_nans=0.8
      )
      isfcs_loo_70 = isfc(
          data, pairwise=False, vectorize_isfcs=False, tolerate_nans=0.7
      )
      isfcs_loo_60 = isfc(
          data, pairwise=False, vectorize_isfcs=False, tolerate_nans=0.6
      )
      assert (
          np.sum(np.isnan(isfcs_loo_F))
          == np.sum(np.isnan(isfcs_loo_95))
          == 3420
      )
      assert (
          np.sum(np.isnan(isfcs_loo_80))
          == np.sum(np.isnan(isfcs_loo_90))
          == 2430
      )
      assert (
          np.sum(np.isnan(isfcs_loo_T))
          == np.sum(np.isnan(isfcs_loo_60))
          == np.sum(np.isnan(isfcs_loo_70))
          == 1632
      )
      assert np.array_equal(
          np.sum(np.isnan(isfcs_loo_F), axis=0),
          np.sum(np.isnan(isfcs_loo_95), axis=0),
      )
      assert np.array_equal(
          np.sum(np.isnan(isfcs_loo_80), axis=0),
          np.sum(np.isnan(isfcs_loo_90), axis=0),
      )
      assert np.all(
          (
              np.array_equal(
                  np.sum(np.isnan(isfcs_loo_T), axis=0),
                  np.sum(np.isnan(isfcs_loo_60), axis=0),
              ),
              np.array_equal(
                  np.sum(np.isnan(isfcs_loo_T), axis=0),
                  np.sum(np.isnan(isfcs_loo_70), axis=0),
              ),
              np.array_equal(
                  np.sum(np.isnan(isfcs_loo_60), axis=0),
                  np.sum(np.isnan(isfcs_loo_70), axis=0),
              ),
          )
      )
  
      isfcs_loo_T, _ = isfc(
          data, pairwise=False, vectorize_isfcs=True, tolerate_nans=True
      )
      isfcs_loo_F, _ = isfc(
          data,
          pairwise=False,
          vectorize_isfcs=True,
          tolerate_nans=False,
      )
      isfcs_loo_95, _ = isfc(
          data, pairwise=False, vectorize_isfcs=True, tolerate_nans=0.95
      )
      isfcs_loo_90, _ = isfc(
          data, pairwise=False, vectorize_isfcs=True, tolerate_nans=0.90
      )
      isfcs_loo_80, _ = isfc(
          data, pairwise=False, vectorize_isfcs=True, tolerate_nans=0.8
      )
      isfcs_loo_70, _ = isfc(
          data, pairwise=False, vectorize_isfcs=True, tolerate_nans=0.7
      )
      isfcs_loo_60, _ = isfc(
          data, pairwise=False, vectorize_isfcs=True, tolerate_nans=0.6
      )
      assert (
          np.sum(np.isnan(isfcs_loo_F))
          == np.sum(np.isnan(isfcs_loo_95))
          == 1680
      )
      assert (
          np.sum(np.isnan(isfcs_loo_80))
          == np.sum(np.isnan(isfcs_loo_90))
          == 1194
      )
      assert (
          np.sum(np.isnan(isfcs_loo_T))
          == np.sum(np.isnan(isfcs_loo_60))
          == np.sum(np.isnan(isfcs_loo_70))
          == 802
      )
      assert np.array_equal(
          np.sum(np.isnan(isfcs_loo_F), axis=0),
          np.sum(np.isnan(isfcs_loo_95), axis=0),
      )
      assert np.array_equal(
          np.sum(np.isnan(isfcs_loo_80), axis=0),
          np.sum(np.isnan(isfcs_loo_90), axis=0),
      )
      assert np.all(
          (
              np.array_equal(
                  np.sum(np.isnan(isfcs_loo_T), axis=0),
                  np.sum(np.isnan(isfcs_loo_60), axis=0),
              ),
              np.array_equal(
                  np.sum(np.isnan(isfcs_loo_T), axis=0),
                  np.sum(np.isnan(isfcs_loo_70), axis=0),
              ),
              np.array_equal(
                  np.sum(np.isnan(isfcs_loo_60), axis=0),
                  np.sum(np.isnan(isfcs_loo_70), axis=0),
              ),
          )
      )
  
      data = simulated_timeseries(
          n_subjects,
          n_TRs,
          n_voxels=n_voxels,
          data_type="array",
          random_state=random_state,
      )
  
      # Make sure voxel with NaNs across all subjects is always removed
      data[0, 0, :] = np.nan
      isfcs_loo_T = isfc(
          data,
          pairwise=False,
          vectorize_isfcs=False,
          tolerate_nans=True,
      )
      isfcs_loo_F = isfc(
          data,
          pairwise=False,
          vectorize_isfcs=False,
          tolerate_nans=False,
      )
      assert np.allclose(isfcs_loo_T, isfcs_loo_F, equal_nan=True)
      assert (
          np.sum(np.isnan(isfcs_loo_T))
          == np.sum(np.isnan(isfcs_loo_F))
          == 1180
      )
  
      isfcs_pw_T = isfc(
          data, pairwise=True, vectorize_isfcs=False, tolerate_nans=True
      )
      isfcs_pw_F = isfc(
          data,
          pairwise=True,
          vectorize_isfcs=False,
          tolerate_nans=False,
      )
      assert np.allclose(isfcs_pw_T, isfcs_pw_F, equal_nan=True)
  
      assert (
          np.sum(np.isnan(isfcs_pw_T))
          == np.sum(np.isnan(isfcs_pw_T))
          == 11210
      )
  
      # Check for NaN-handling in targets
      n_targets = 15
      data = simulated_timeseries(
          n_subjects,
          n_TRs,
          n_voxels=n_voxels,
          data_type="array",
          random_state=random_state,
      )
      targets_data = simulated_timeseries(
          n_subjects, n_TRs, n_voxels=n_targets, data_type="array"
      )
  
      # Inject NaNs into targets_data
      targets_data[0, 0, 0] = np.nan
  
      # Don't tolerate NaNs, should lose zeroeth voxel
      isfcs_loo = isfc(
          data,
          targets=targets_data,
          pairwise=False,
          vectorize_isfcs=False,
          tolerate_nans=False,
      )
      assert np.sum(np.isnan(isfcs_loo)) == (n_subjects - 1) * (
          n_targets * 2
      )
  
      # Single NaN in targets will get averaged out with tolerate
      isfcs_loo = isfc(
          data,
          targets=targets_data,
          pairwise=False,
          vectorize_isfcs=False,
          tolerate_nans=True,
      )
      assert np.sum(np.isnan(isfcs_loo)) == 0
  
file_path: brainiak/tests/isc/test_isc.py
imports: import numpy
  import logging
  import pytest
  from brainiak.isc import isc, isfc, bootstrap_isc, permutation_isc, squareform_isfc, timeshift_isc, phaseshift_isc
  from scipy.spatial.distance import squareform
label_desc: Use image node_id 12 for calling a global function with example usage: test_isfc_nans() without return types