Exemplo n.º 1
0
def test_isfc_options():

    # Set parameters for toy time series data
    n_subjects = 20
    n_TRs = 60
    n_voxels = 30

    logger.info("Testing ISFC options")

    data = simulated_timeseries(n_subjects,
                                n_TRs,
                                n_voxels=n_voxels,
                                data_type='array')
    isfcs = isfc(data, pairwise=False, summary_statistic=None)

    # Just two subjects
    isfcs = isfc(data[..., :2], pairwise=False, summary_statistic=None)

    # ISFC with pairwise approach
    isfcs = isfc(data, pairwise=True, summary_statistic=None)

    # ISFC with summary statistics
    isfcs = isfc(data, pairwise=True, summary_statistic='mean')
    isfcs = isfc(data, pairwise=True, summary_statistic='median')

    # Check output p-values
    data = correlated_timeseries(20, 60, noise=.5, random_state=42)
    isfcs = isfc(data, pairwise=False)
    assert np.all(isfcs[0, 1, :] > .5) and np.all(isfcs[1, 0, :] > .5)
    assert np.all(isfcs[:2, 2, :] < .5) and np.all(isfcs[2, :2, :] < .5)

    isfcs = isfc(data, pairwise=True)
    assert np.all(isfcs[0, 1, :] > .5) and np.all(isfcs[1, 0, :] > .5)
    assert np.all(isfcs[:2, 2, :] < .5) and np.all(isfcs[2, :2, :] < .5)

    # Check that ISC and ISFC diagonal are identical
    iscs = isc(data, pairwise=False)
    isfcs = isfc(data, pairwise=False)
    for s in np.arange(len(iscs)):
        assert np.allclose(isfcs[..., s].diagonal(), iscs[s, :], rtol=1e-03)

    # Check that ISC and ISFC diagonal are identical
    iscs = isc(data, pairwise=True)
    isfcs = isfc(data, pairwise=True)
    for s in np.arange(len(iscs)):
        assert np.allclose(isfcs[..., s].diagonal(), iscs[s, :], rtol=1e-03)

    logger.info("Finished testing ISFC options")
Exemplo n.º 2
0
def test_isfc_options():

    # Set parameters for toy time series data
    n_subjects = 20
    n_TRs = 60
    n_voxels = 30

    logger.info("Testing ISFC options")

    data = simulated_timeseries(n_subjects, n_TRs,
                                n_voxels=n_voxels, data_type='array')
    isfcs = isfc(data, pairwise=False, summary_statistic=None)

    # Just two subjects
    isfcs = isfc(data[..., :2], pairwise=False, summary_statistic=None)

    # ISFC with pairwise approach
    isfcs = isfc(data, pairwise=True, summary_statistic=None)

    # ISFC with summary statistics
    isfcs = isfc(data, pairwise=True, summary_statistic='mean')
    isfcs = isfc(data, pairwise=True, summary_statistic='median')

    # Check output p-values
    data = correlated_timeseries(20, 60, noise=.5,
                                 random_state=42)
    isfcs = isfc(data, pairwise=False)
    assert np.all(isfcs[0, 1, :] > .5) and np.all(isfcs[1, 0, :] > .5)
    assert np.all(isfcs[:2, 2, :] < .5) and np.all(isfcs[2, :2, :] < .5)

    isfcs = isfc(data, pairwise=True)
    assert np.all(isfcs[0, 1, :] > .5) and np.all(isfcs[1, 0, :] > .5)
    assert np.all(isfcs[:2, 2, :] < .5) and np.all(isfcs[2, :2, :] < .5)

    # Check that ISC and ISFC diagonal are identical
    iscs = isc(data, pairwise=False)
    isfcs = isfc(data, pairwise=False)
    for s in np.arange(len(iscs)):
        assert np.allclose(isfcs[..., s].diagonal(), iscs[s, :], rtol=1e-03)

    # Check that ISC and ISFC diagonal are identical
    iscs = isc(data, pairwise=True)
    isfcs = isfc(data, pairwise=True)
    for s in np.arange(len(iscs)):
        assert np.allclose(isfcs[..., s].diagonal(), iscs[s, :], rtol=1e-03)

    logger.info("Finished testing ISFC options")
Exemplo n.º 3
0
def test_squareform_isfc():

    # 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)

    # Generate square redundant ISFCs
    isfcs_r = isfc(data, vectorize_isfcs=False)
    assert isfcs_r.shape == (n_subjects, n_voxels, n_voxels)

    # Squareform these into condensed ISFCs and ISCs
    isfcs_c, iscs_c = squareform_isfc(isfcs_r)
    assert isfcs_c.shape == (n_subjects, n_voxels * (n_voxels - 1) / 2)
    assert iscs_c.shape == (n_subjects, n_voxels)

    # Go back the other way and check it's the same
    isfcs_new = squareform_isfc(isfcs_c, iscs_c)
    assert np.array_equal(isfcs_r, isfcs_new)

    # Check against ISC function
    assert np.allclose(isc(data), iscs_c, rtol=1e-03)

    # Check for two subjects
    isfcs_r = isfc(data[..., :2], vectorize_isfcs=False)
    assert isfcs_r.shape == (n_voxels, n_voxels)
    isfcs_c, iscs_c = squareform_isfc(isfcs_r)
    assert isfcs_c.shape == (n_voxels * (n_voxels - 1) / 2, )
    assert iscs_c.shape == (n_voxels, )
    assert np.array_equal(isfcs_r, squareform_isfc(isfcs_c, iscs_c))
Exemplo n.º 4
0
def test_squareform_isfc():

    # 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)

    # Generate square redundant ISFCs
    isfcs_r = isfc(data, vectorize_isfcs=False)
    assert isfcs_r.shape == (n_subjects, n_voxels, n_voxels)

    # Squareform these into condensed ISFCs and ISCs
    isfcs_c, iscs_c = squareform_isfc(isfcs_r)
    assert isfcs_c.shape == (n_subjects, n_voxels * (n_voxels - 1) / 2)
    assert iscs_c.shape == (n_subjects, n_voxels)

    # Go back the other way and check it's the same
    isfcs_new = squareform_isfc(isfcs_c, iscs_c)
    assert np.array_equal(isfcs_r, isfcs_new)

    # Check against ISC function
    assert np.allclose(isc(data), iscs_c, rtol=1e-03)

    # Check for two subjects
    isfcs_r = isfc(data[..., :2], vectorize_isfcs=False)
    assert isfcs_r.shape == (n_voxels, n_voxels)
    isfcs_c, iscs_c = squareform_isfc(isfcs_r)
    assert isfcs_c.shape == (n_voxels * (n_voxels - 1) / 2,)
    assert iscs_c.shape == (n_voxels,)
    assert np.array_equal(isfcs_r, squareform_isfc(isfcs_c, iscs_c))
Exemplo n.º 5
0
def target_isfc(data,
                targets,
                stories=None,
                subjects=None,
                hemisphere=None,
                zscore_fcs=True):

    # By default grab all stories
    stories = check_keys(data, keys=stories)

    target_isfcs = {}
    for story in stories:

        target_isfcs[story] = {}

        # By default just grab all subjects
        subject_list = check_keys(data[story], keys=subjects, subkey=story)

        for subject in subject_list:
            target_isfcs[story][subject] = {}

        # Stack targets in third dimension
        target_stack = np.dstack(
            ([targets[story][subject] for subject in subject_list]))

        # By default grab both hemispheres
        hemis = check_keys(data[story][subject_list[0]], keys=hemisphere)

        # Get for specified hemisphere(s)
        for hemi in hemis:

            # Grab ROI data and targets
            data_stack = np.dstack(
                ([data[story][subject][hemi] for subject in subject_list]))

            # Compute ISFCs between ROI and targets
            isfcs = isfc(data_stack, targets=target_stack)

            # Optionally z-score across targets
            if zscore_fcs:
                isfcs = zscore(np.nan_to_num(isfcs), axis=2)

            for s, subject in enumerate(subject_list):
                target_isfcs[story][subject][hemi] = isfcs[s]

        print(f"Finished computing target ISFCs for story '{story}'")

    return target_isfcs
Exemplo n.º 6
0
                                  mask_image)
coords = np.where(mask_image)
data = image.MaskedMultiSubjectData.from_masked_images(masked_images,
                                                       len(func_fns))

print('Calculating mean ISC on {0} voxels'.format(data.shape[1]))
iscs = isc(data, pairwise=False, summary_statistic='mean')
iscs = np.nan_to_num(iscs)

print('Writing ISC map to file...')
nii_template = nib.load(mask_fn)
isc_vol = np.zeros(nii_template.shape)
isc_vol[coords] = iscs
isc_image = nib.Nifti1Image(isc_vol, nii_template.affine,
                            nii_template.header)
nib.save(isc_image, 'example_isc.nii.gz')

isc_mask = (iscs > 0.2)[0, :]
print('Calculating mean ISFC on {0} voxels...'.format(np.sum(isc_mask)))
data_masked = data[:, isc_mask, :]
isfcs = isfc(data_masked, pairwise=False, summary_statistic='mean')

print('Clustering ISFC...')
Z = linkage(isfcs, 'ward')
z = fcluster(Z, 2, criterion='maxclust')
clust_inds = np.argsort(z)

# Show the ISFC matrix, sorted to show the two main clusters
plt.imshow(isfcs[np.ix_(clust_inds, clust_inds)])
plt.show()
Exemplo n.º 7
0
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 * .7)] = np.nan
    data[0, 2, :n_subjects - int(n_subjects * .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=.95)
    isfcs_loo_90 = isfc(data,
                        pairwise=False,
                        vectorize_isfcs=False,
                        tolerate_nans=.90)
    isfcs_loo_80 = isfc(data,
                        pairwise=False,
                        vectorize_isfcs=False,
                        tolerate_nans=.8)
    isfcs_loo_70 = isfc(data,
                        pairwise=False,
                        vectorize_isfcs=False,
                        tolerate_nans=.7)
    isfcs_loo_60 = isfc(data,
                        pairwise=False,
                        vectorize_isfcs=False,
                        tolerate_nans=.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=.95)
    isfcs_loo_90, _ = isfc(data,
                           pairwise=False,
                           vectorize_isfcs=True,
                           tolerate_nans=.90)
    isfcs_loo_80, _ = isfc(data,
                           pairwise=False,
                           vectorize_isfcs=True,
                           tolerate_nans=.8)
    isfcs_loo_70, _ = isfc(data,
                           pairwise=False,
                           vectorize_isfcs=True,
                           tolerate_nans=.7)
    isfcs_loo_60, _ = isfc(data,
                           pairwise=False,
                           vectorize_isfcs=True,
                           tolerate_nans=.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
Exemplo n.º 8
0
def test_isfc_options():

    # Set parameters for toy time series data
    n_subjects = 20
    n_TRs = 60
    n_voxels = 30

    logger.info("Testing ISFC options")

    data = simulated_timeseries(n_subjects,
                                n_TRs,
                                n_voxels=n_voxels,
                                data_type='array')
    isfcs, iscs = isfc(data, pairwise=False, summary_statistic=None)
    assert isfcs.shape == (n_subjects, n_voxels * (n_voxels - 1) / 2)
    assert iscs.shape == (n_subjects, n_voxels)

    # Without vectorized upper triangle
    isfcs = isfc(data,
                 pairwise=False,
                 summary_statistic=None,
                 vectorize_isfcs=False)
    assert isfcs.shape == (n_subjects, n_voxels, n_voxels)

    # Just two subjects
    isfcs, iscs = isfc(data[..., :2], pairwise=False, summary_statistic=None)
    assert isfcs.shape == (n_voxels * (n_voxels - 1) / 2, )
    assert iscs.shape == (n_voxels, )

    isfcs = isfc(data[..., :2],
                 pairwise=False,
                 summary_statistic=None,
                 vectorize_isfcs=False)
    assert isfcs.shape == (n_voxels, n_voxels)

    # ISFC with pairwise approach
    isfcs, iscs = isfc(data, pairwise=True, summary_statistic=None)
    assert isfcs.shape == (n_subjects * (n_subjects - 1) / 2,
                           n_voxels * (n_voxels - 1) / 2)
    assert iscs.shape == (n_subjects * (n_subjects - 1) / 2, n_voxels)

    isfcs = isfc(data,
                 pairwise=True,
                 summary_statistic=None,
                 vectorize_isfcs=False)
    assert isfcs.shape == (n_subjects * (n_subjects - 1) / 2, n_voxels,
                           n_voxels)

    # ISFC with summary statistics
    isfcs, iscs = isfc(data, pairwise=True, summary_statistic='mean')
    isfcs, iscs = isfc(data, pairwise=True, summary_statistic='median')

    # Check output p-values
    data = correlated_timeseries(20, 60, noise=.5, random_state=42)
    isfcs = isfc(data, pairwise=False, vectorize_isfcs=False)
    assert np.all(isfcs[:, 0, 1] > .5) and np.all(isfcs[:, 1, 0] > .5)
    assert np.all(isfcs[:, :2, 2] < .5) and np.all(isfcs[:, 2, :2] < .5)

    isfcs = isfc(data, pairwise=True, vectorize_isfcs=False)
    assert np.all(isfcs[:, 0, 1] > .5) and np.all(isfcs[:, 1, 0] > .5)
    assert np.all(isfcs[:, :2, 2] < .5) and np.all(isfcs[:, 2, :2] < .5)

    # Check that ISC and ISFC diagonal are identical
    iscs = isc(data, pairwise=False)
    isfcs = isfc(data, pairwise=False, vectorize_isfcs=False)
    for s in np.arange(len(iscs)):
        assert np.allclose(isfcs[s, ...].diagonal(), iscs[s, :], rtol=1e-03)
    isfcs, iscs_v = isfc(data, pairwise=False)
    assert np.allclose(iscs, iscs_v, rtol=1e-03)

    # Check that ISC and ISFC diagonal are identical (pairwise)
    iscs = isc(data, pairwise=True)
    isfcs = isfc(data, pairwise=True, vectorize_isfcs=False)
    for s in np.arange(len(iscs)):
        assert np.allclose(isfcs[s, ...].diagonal(), iscs[s, :], rtol=1e-03)
    isfcs, iscs_v = isfc(data, pairwise=True)
    assert np.allclose(iscs, iscs_v, rtol=1e-03)

    # Generate 'targets' data and use for ISFC
    data = simulated_timeseries(n_subjects,
                                n_TRs,
                                n_voxels=n_voxels,
                                data_type='array')
    n_targets = 15
    targets_data = simulated_timeseries(n_subjects,
                                        n_TRs,
                                        n_voxels=n_targets,
                                        data_type='array')
    isfcs = isfc(data,
                 targets=targets_data,
                 pairwise=False,
                 vectorize_isfcs=False)
    assert isfcs.shape == (n_subjects, n_voxels, n_targets)

    # Ensure 'square' output enforced
    isfcs = isfc(data,
                 targets=targets_data,
                 pairwise=False,
                 vectorize_isfcs=True)
    assert isfcs.shape == (n_subjects, n_voxels, n_targets)

    # Check list input for targets
    targets_data = simulated_timeseries(n_subjects,
                                        n_TRs,
                                        n_voxels=n_targets,
                                        data_type='list')
    isfcs = isfc(data,
                 targets=targets_data,
                 pairwise=False,
                 vectorize_isfcs=False)
    assert isfcs.shape == (n_subjects, n_voxels, n_targets)

    # Check that mismatching subjects / TRs breaks targets
    targets_data = simulated_timeseries(n_subjects,
                                        n_TRs,
                                        n_voxels=n_targets,
                                        data_type='array')

    with pytest.raises(ValueError):
        isfcs = isfc(data,
                     targets=targets_data[..., :-1],
                     pairwise=False,
                     vectorize_isfcs=False)
    assert isfcs.shape == (n_subjects, n_voxels, n_targets)

    with pytest.raises(ValueError):
        isfcs = isfc(data,
                     targets=targets_data[:-1, ...],
                     pairwise=False,
                     vectorize_isfcs=False)

    # Check targets for only 2 subjects
    isfcs = isfc(data[..., :2],
                 targets=targets_data[..., :2],
                 pairwise=False,
                 summary_statistic=None)
    assert isfcs.shape == (2, n_voxels, n_targets)

    isfcs = isfc(data[..., :2],
                 targets=targets_data[..., :2],
                 pairwise=True,
                 summary_statistic=None)
    assert isfcs.shape == (2, n_voxels, n_targets)

    # Check that supplying targets enforces leave-one-out
    isfcs_pw = isfc(data,
                    targets=targets_data,
                    pairwise=True,
                    vectorize_isfcs=False,
                    tolerate_nans=False)
    assert isfcs_pw.shape == (n_subjects, n_voxels, n_targets)

    logger.info("Finished testing ISFC options")
Exemplo n.º 9
0
mask_image = io.load_boolean_mask(mask_fn, lambda x: x > 50)
masked_images = image.mask_images(io.load_images(func_fns), mask_image)
coords = np.where(mask_image)
data = image.MaskedMultiSubjectData.from_masked_images(masked_images,
                                                       len(func_fns))

print('Calculating mean ISC on {0} voxels'.format(data.shape[1]))
iscs = isc(data, pairwise=False, summary_statistic='mean')
iscs = np.nan_to_num(iscs)

print('Writing ISC map to file...')
nii_template = nib.load(mask_fn)
isc_vol = np.zeros(nii_template.shape)
isc_vol[coords] = iscs
isc_image = nib.Nifti1Image(isc_vol, nii_template.affine, nii_template.header)
nib.save(isc_image, 'example_isc.nii.gz')

isc_mask = (iscs > 0.2)[0, :]
print('Calculating mean ISFC on {0} voxels...'.format(np.sum(isc_mask)))
data_masked = data[:, isc_mask, :]
isfcs = isfc(data_masked, pairwise=False, summary_statistic='mean')

print('Clustering ISFC...')
Z = linkage(isfcs, 'ward')
z = fcluster(Z, 2, criterion='maxclust')
clust_inds = np.argsort(z)

# Show the ISFC matrix, sorted to show the two main clusters
plt.imshow(isfcs[np.ix_(clust_inds, clust_inds)])
plt.show()
Exemplo n.º 10
0
n_pairs = n_players * (n_players - 1) // 2
n_matchups = 4
k = 100
lstms_pca = np.load(f'results/lstms_tanh-z_pca-k{k}.npy')

# Loop through matchups and repeats and compute ISFC
isfc_results = np.zeros(
    (n_matchups, n_repeats, n_pairs, lstms_pca.shape[-1], lstms_pca.shape[-1]))
for matchup in np.arange(n_matchups):
    for repeat in np.arange(n_repeats):

        lstms = lstms_pca[matchup, repeat, ...]
        lstms = np.rollaxis(lstms, 0, 3)

        # Compute ISCs between each pair for 4 agents
        isfcs = isfc(lstms, pairwise=True, vectorize_isfcs=False)
        isfc_results[matchup, repeat, ...] = isfcs

        print(f"Computed ISFC for matchup {matchup} (repeat {repeat})")

np.save(f'results/isfc_lstm_tanh-z_pca-k{k}.npy', isfc_results)

# Compute ISFC based on confound regression PCs
n_pairs = n_players * (n_players - 1) // 2
n_matchups = 4
k = 100
reg = 'com'
lstms_pca = np.load(f'results/lstms_tanh-z_pca-k{k}_reg-{reg}.npy')

# Loop through matchups and repeats and compute ISFC
isfc_results = np.zeros(
Exemplo n.º 11
0
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 * .7)] = np.nan
    data[0, 2, :n_subjects - int(n_subjects * .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=.95)
    isfcs_loo_90 = isfc(data, pairwise=False, vectorize_isfcs=False,
                        tolerate_nans=.90)
    isfcs_loo_80 = isfc(data, pairwise=False, vectorize_isfcs=False,
                        tolerate_nans=.8)
    isfcs_loo_70 = isfc(data, pairwise=False, vectorize_isfcs=False,
                        tolerate_nans=.7)
    isfcs_loo_60 = isfc(data, pairwise=False, vectorize_isfcs=False,
                        tolerate_nans=.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=.95)
    isfcs_loo_90, _ = isfc(data, pairwise=False, vectorize_isfcs=True,
                           tolerate_nans=.90)
    isfcs_loo_80, _ = isfc(data, pairwise=False, vectorize_isfcs=True,
                           tolerate_nans=.8)
    isfcs_loo_70, _ = isfc(data, pairwise=False, vectorize_isfcs=True,
                           tolerate_nans=.7)
    isfcs_loo_60, _ = isfc(data, pairwise=False, vectorize_isfcs=True,
                           tolerate_nans=.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
Exemplo n.º 12
0
def test_isfc_options():

    # Set parameters for toy time series data
    n_subjects = 20
    n_TRs = 60
    n_voxels = 30

    logger.info("Testing ISFC options")

    data = simulated_timeseries(n_subjects, n_TRs,
                                n_voxels=n_voxels, data_type='array')
    isfcs, iscs = isfc(data, pairwise=False, summary_statistic=None)
    assert isfcs.shape == (n_subjects, n_voxels * (n_voxels - 1) / 2)
    assert iscs.shape == (n_subjects, n_voxels)

    # Without vectorized upper triangle
    isfcs = isfc(data, pairwise=False, summary_statistic=None,
                 vectorize_isfcs=False)
    assert isfcs.shape == (n_subjects, n_voxels, n_voxels)

    # Just two subjects
    isfcs, iscs = isfc(data[..., :2], pairwise=False, summary_statistic=None)
    assert isfcs.shape == (n_voxels * (n_voxels - 1) / 2,)
    assert iscs.shape == (n_voxels,)

    isfcs = isfc(data[..., :2], pairwise=False, summary_statistic=None,
                 vectorize_isfcs=False)
    assert isfcs.shape == (n_voxels, n_voxels)

    # ISFC with pairwise approach
    isfcs, iscs = isfc(data, pairwise=True, summary_statistic=None)
    assert isfcs.shape == (n_subjects * (n_subjects - 1) / 2,
                           n_voxels * (n_voxels - 1) / 2)
    assert iscs.shape == (n_subjects * (n_subjects - 1) / 2,
                          n_voxels)

    isfcs = isfc(data, pairwise=True, summary_statistic=None,
                 vectorize_isfcs=False)
    assert isfcs.shape == (n_subjects * (n_subjects - 1) / 2,
                           n_voxels, n_voxels)

    # ISFC with summary statistics
    isfcs, iscs = isfc(data, pairwise=True, summary_statistic='mean')
    isfcs, iscs = isfc(data, pairwise=True, summary_statistic='median')

    # Check output p-values
    data = correlated_timeseries(20, 60, noise=.5,
                                 random_state=42)
    isfcs = isfc(data, pairwise=False, vectorize_isfcs=False)
    assert np.all(isfcs[:, 0, 1] > .5) and np.all(isfcs[:, 1, 0] > .5)
    assert np.all(isfcs[:, :2, 2] < .5) and np.all(isfcs[:, 2, :2] < .5)

    isfcs = isfc(data, pairwise=True, vectorize_isfcs=False)
    assert np.all(isfcs[:, 0, 1] > .5) and np.all(isfcs[:, 1, 0] > .5)
    assert np.all(isfcs[:, :2, 2] < .5) and np.all(isfcs[:, 2, :2] < .5)

    # Check that ISC and ISFC diagonal are identical
    iscs = isc(data, pairwise=False)
    isfcs = isfc(data, pairwise=False, vectorize_isfcs=False)
    for s in np.arange(len(iscs)):
        assert np.allclose(isfcs[s, ...].diagonal(), iscs[s, :], rtol=1e-03)
    isfcs, iscs_v = isfc(data, pairwise=False)
    assert np.allclose(iscs, iscs_v, rtol=1e-03)

    # Check that ISC and ISFC diagonal are identical (pairwise)
    iscs = isc(data, pairwise=True)
    isfcs = isfc(data, pairwise=True, vectorize_isfcs=False)
    for s in np.arange(len(iscs)):
        assert np.allclose(isfcs[s, ...].diagonal(), iscs[s, :], rtol=1e-03)
    isfcs, iscs_v = isfc(data, pairwise=True)
    assert np.allclose(iscs, iscs_v, rtol=1e-03)

    # Generate 'targets' data and use for ISFC
    data = simulated_timeseries(n_subjects, n_TRs,
                                n_voxels=n_voxels, data_type='array')
    n_targets = 15
    targets_data = simulated_timeseries(n_subjects, n_TRs,
                                        n_voxels=n_targets,
                                        data_type='array')
    isfcs = isfc(data, targets=targets_data, pairwise=False,
                 vectorize_isfcs=False)
    assert isfcs.shape == (n_subjects, n_voxels, n_targets)

    # Ensure 'square' output enforced
    isfcs = isfc(data, targets=targets_data, pairwise=False,
                 vectorize_isfcs=True)
    assert isfcs.shape == (n_subjects, n_voxels, n_targets)

    # Check list input for targets
    targets_data = simulated_timeseries(n_subjects, n_TRs,
                                        n_voxels=n_targets,
                                        data_type='list')
    isfcs = isfc(data, targets=targets_data, pairwise=False,
                 vectorize_isfcs=False)
    assert isfcs.shape == (n_subjects, n_voxels, n_targets)

    # Check that mismatching subjects / TRs breaks targets
    targets_data = simulated_timeseries(n_subjects, n_TRs,
                                        n_voxels=n_targets,
                                        data_type='array')

    with pytest.raises(ValueError):
        isfcs = isfc(data, targets=targets_data[..., :-1],
                     pairwise=False, vectorize_isfcs=False)
    assert isfcs.shape == (n_subjects, n_voxels, n_targets)

    with pytest.raises(ValueError):
        isfcs = isfc(data, targets=targets_data[:-1, ...],
                     pairwise=False, vectorize_isfcs=False)

    # Check targets for only 2 subjects
    isfcs = isfc(data[..., :2], targets=targets_data[..., :2],
                 pairwise=False, summary_statistic=None)
    assert isfcs.shape == (2, n_voxels, n_targets)

    isfcs = isfc(data[..., :2], targets=targets_data[..., :2],
                 pairwise=True, summary_statistic=None)
    assert isfcs.shape == (2, n_voxels, n_targets)

    # Check that supplying targets enforces leave-one-out
    isfcs_pw = isfc(data, targets=targets_data, pairwise=True,
                    vectorize_isfcs=False, tolerate_nans=False)
    assert isfcs_pw.shape == (n_subjects, n_voxels, n_targets)

    logger.info("Finished testing ISFC options")