Exemple #1
0
def test_timseries_extraction_extract(conf):
    """Test timeseries extraction and save methods of the TimeseriesExtraction class."""

    dir_path_tmp = tempfile.TemporaryDirectory()
    dir_path = dir_path_tmp.name

    func_file = tempfile.NamedTemporaryFile(mode='w+', suffix='.nii.gz')
    img_data = np.random.rand(50, 50, 50, 20)
    img = nib.Nifti1Image(img_data, np.eye(4))
    img.to_filename(func_file.name)

    # Create a temp parcel file
    parcels_tmp = tempfile.NamedTemporaryFile(mode='w+', suffix='.nii.gz')
    parcels = np.zeros((50, 50, 50))
    parcels[10:20, 0, 0], parcels[0, 10:20, 0], parcels[0, 0, 10:20] = 1, 2, 3
    nib.Nifti1Image(parcels, np.eye(4)).to_filename(parcels_tmp.name)
    net_parcels_map_nifti_file = parcels_tmp.name

    # Create empty mask file
    mask_tmp = tempfile.NamedTemporaryFile(mode='w+', suffix='.nii.gz')
    mask = np.zeros((50, 50, 50))
    nib.Nifti1Image(parcels, np.eye(4)).to_filename(mask_tmp.name)
    mask = mask_tmp.name

    if conf:
        conf_file = tempfile.NamedTemporaryFile(mode='w+', suffix='.tsv')
        conf_mat = np.random.rand(20)
        conf_df = pd.DataFrame({
            'Conf1': conf_mat,
            "Conf2": [np.nan] * len(conf_mat)
        })
        conf_df.to_csv(conf_file.name, sep='\t', index=False)
        conf = conf_file.name

    smooth = 1
    network = 'Default'
    ID = '002'
    smooth = 2
    hpass = 100
    coords = [[10] * 3, [15] * 3, [20] * 3]
    node_size = 2
    extract_strategy = 'mean'

    roi, labels, atlas, uatlas = [None] * 4

    te = TimeseriesExtraction(net_parcels_nii_path=net_parcels_map_nifti_file,
                              node_size=node_size,
                              conf=conf,
                              func_file=func_file.name,
                              roi=roi,
                              dir_path=dir_path,
                              ID=ID,
                              network=network,
                              smooth=smooth,
                              hpass=hpass,
                              mask=mask,
                              extract_strategy=extract_strategy)
    te.prepare_inputs()

    # Test parc extraction
    te.extract_ts_parc()
    assert np.shape(te.ts_within_nodes) == (np.shape(img_data)[-1],
                                            len(np.unique(parcels)) - 1)

    # Test save and clean up
    te._mask_path = te._mask_img
    te.save_and_cleanup()

    assert '_parcel_masker' not in te.__dict__.keys()

    func_file.close()
    parcels_tmp.close()
    mask_tmp.close()
    if conf:
        conf_file.close()
Exemple #2
0
def test_timseries_extraction_prepare_inputs(conf, hpass, mask, func_file,
                                             dim):
    """ Test preparing inputs method of the TimeseriesExtraction class."""

    base_dir = str(Path(__file__).parent / "examples")
    net_parcels_map_nifti_file = f"{base_dir}/miscellaneous/002_parcels_Default.nii.gz"
    dir_path = f"{base_dir}/BIDS/sub-25659/ses-1/func"

    if func_file:
        func_tmp = tempfile.NamedTemporaryFile(mode='w+', suffix='.nii.gz')
        if dim == 4:
            img_data = np.random.rand(50, 50, 50, 20)
        else:
            img_data = np.random.rand(50, 50, 50)
        img = nib.Nifti1Image(img_data, np.eye(4))
        img.to_filename(func_tmp.name)
        func_file = func_tmp.name
    else:
        func_file = "missing/file"

    if mask:
        mask_tmp = tempfile.NamedTemporaryFile(mode='w+', suffix='.nii.gz')
        mask_data = np.zeros(np.shape(img_data))
        mask_img = nib.Nifti1Image(mask_data, np.eye(4))
        mask_img.to_filename(mask_tmp.name)
        mask = mask_tmp.name

    if conf:
        conf_mat = np.random.rand(20)
        conf_tmp = tempfile.NamedTemporaryFile(mode='w+', suffix='.tsv')
        conf_df = pd.DataFrame({'Column1': conf_mat})
        conf_df.to_csv(conf_tmp.name)
        conf = conf_tmp.name
    else:
        conf = "missing/file"

    smooth = 1
    network = 'Default'
    ID = '002'
    smooth = 2
    coords = [[10] * 3, [15] * 3, [20] * 3]
    node_size = 8
    extract_strategy = 'zscore'
    roi, labels, atlas, uatlas = [None] * 4

    te = TimeseriesExtraction(net_parcels_nii_path=net_parcels_map_nifti_file,
                              node_size=node_size,
                              conf=conf,
                              func_file=func_file,
                              roi=roi,
                              dir_path=dir_path,
                              ID=ID,
                              network=network,
                              smooth=smooth,
                              hpass=hpass,
                              mask=mask,
                              extract_strategy=extract_strategy)
    te.prepare_inputs()

    assert np.shape(te._func_img) == np.shape(img_data)

    if hpass and hpass > 0:
        assert te.hpass == hpass
        assert te._detrending is False
    else:
        assert te.hpass is None
        assert te._detrending is True

    if hpass and dim == 4:
        assert te._t_r == img.header.get_zooms()[-1]
    else:
        assert te._t_r == None

    if mask:
        assert np.shape(te._mask_img) == np.shape(img_data)

    if func_file:
        func_tmp.close()
    if conf:
        conf_tmp.close()
    if mask:
        mask_tmp.close()