示例#1
0
def test_fetch_cobre(tmp_path, request_mocker):
    metadata, ids = _cobre_metadata()
    request_mocker.url_mapping["*phenotypic_data.tsv.gz"] = _cobre_data(ids)
    request_mocker.url_mapping["*articles/4197885"] = metadata

    # All subjects
    cobre_data = check_deprecation(func.fetch_cobre,
                                   "'fetch_cobre' has been deprecated")(
                                       n_subjects=None, data_dir=str(tmp_path))

    phenotypic_names = [
        'func', 'confounds', 'phenotypic', 'description', 'desc_con',
        'desc_phenotypic'
    ]

    # test length of functional filenames to max 146
    assert len(cobre_data.func) == 146
    # test length of corresponding confounds files of same length to max 146
    assert len(cobre_data.confounds) == 146
    # test return type variables
    assert sorted(cobre_data) == sorted(phenotypic_names)
    # test functional filenames in a list
    assert isinstance(cobre_data.func, list)
    # test confounds files in a list
    assert isinstance(cobre_data.confounds, list)
    assert isinstance(cobre_data.func[0], str)
    # returned phenotypic data will be an array
    assert isinstance(cobre_data.phenotypic, np.recarray)

    # Fetch only 30 subjects
    data_30_subjects = func.fetch_cobre(n_subjects=30, data_dir=str(tmp_path))
    assert len(data_30_subjects.func) == 30
    assert len(data_30_subjects.confounds) == 30

    # Test more than maximum subjects
    test_150_subjects = func.fetch_cobre(n_subjects=150,
                                         data_dir=str(tmp_path))
    assert len(test_150_subjects.func) == 146
示例#2
0
def test_fetch_cobre(tmp_path, request_mocker):
    ids_n = [
        40000, 40001, 40002, 40003, 40004, 40005, 40006, 40007, 40008, 40009,
        40010, 40011, 40012, 40013, 40014, 40015, 40016, 40017, 40018, 40019,
        40020, 40021, 40022, 40023, 40024, 40025, 40026, 40027, 40028, 40029,
        40030, 40031, 40032, 40033, 40034, 40035, 40036, 40037, 40038, 40039,
        40040, 40041, 40042, 40043, 40044, 40045, 40046, 40047, 40048, 40049,
        40050, 40051, 40052, 40053, 40054, 40055, 40056, 40057, 40058, 40059,
        40060, 40061, 40062, 40063, 40064, 40065, 40066, 40067, 40068, 40069,
        40071, 40072, 40073, 40074, 40075, 40076, 40077, 40078, 40079, 40080,
        40081, 40082, 40084, 40085, 40086, 40087, 40088, 40089, 40090, 40091,
        40092, 40093, 40094, 40095, 40096, 40097, 40098, 40099, 40100, 40101,
        40102, 40103, 40104, 40105, 40106, 40107, 40108, 40109, 40110, 40111,
        40112, 40113, 40114, 40115, 40116, 40117, 40118, 40119, 40120, 40121,
        40122, 40123, 40124, 40125, 40126, 40127, 40128, 40129, 40130, 40131,
        40132, 40133, 40134, 40135, 40136, 40137, 40138, 40139, 40140, 40141,
        40142, 40143, 40144, 40145, 40146, 40147
    ]

    ids = np.asarray(ids_n, dtype='|U17')

    current_age = np.ones(len(ids), dtype='<f8')
    gender = np.ones(len(ids), dtype='<f8')
    handedness = np.ones(len(ids), dtype='<f8')

    subject_type = np.empty(len(ids), dtype="S10")
    subject_type[0:74] = 'Control'
    subject_type[74:146] = 'Patient'
    diagnosis = np.ones(len(ids), dtype='<f8')
    frames_ok = np.ones(len(ids), dtype='<f8')
    fd = np.ones(len(ids), dtype='<f8')
    fd_scrubbed = np.ones(len(ids), dtype='<f8')

    csv = np.rec.array([
        ids, current_age, gender, handedness, subject_type, diagnosis,
        frames_ok, fd, fd_scrubbed
    ],
                       dtype=[('ID', '|U17'), ('Current Age', '<f8'),
                              ('Gender', '<f8'), ('Handedness', '<f8'),
                              ('Subject Type', '|U17'), ('Diagnosis', '<f8'),
                              ('Frames OK', '<f8'), ('FD', '<f8'),
                              ('FD Scrubbed', '<f8')])

    # Create a dummy 'files'
    cobre_dir = str(tmp_path / 'cobre')
    os.mkdir(cobre_dir)

    # Create the tsv
    name_f = os.path.join(cobre_dir, 'phenotypic_data.tsv')
    with open(name_f, 'wb') as f:
        header = '# {0}\n'.format('\t'.join(csv.dtype.names))
        f.write(header.encode())
        np.savetxt(f, csv, delimiter='\t', fmt='%s')

    # create an empty gz file
    f_in = open(name_f)
    name_f_gz = os.path.join(cobre_dir, 'phenotypic_data.tsv.gz')
    f_out = gzip.open(name_f_gz, 'wb')
    f_out.close()
    f_in.close()

    dummy = os.path.join(cobre_dir, '4197885')
    dummy_data = []

    for i in np.hstack(ids_n):
        # Func file
        f = 'fmri_00' + str(i) + '.nii.gz'

        m = 'fmri_00' + str(i) + '.tsv.gz'
        dummy_data.append({'download_url': 'whatever', 'name': f})
        dummy_data.append({'download_url': 'whatever', 'name': m})

    # Add the TSV file
    dummy_data.append({
        'download_url': 'whatever',
        'name': 'phenotypic_data.tsv.gz'
    })
    # Add JSON files
    dummy_data.append({
        'download_url': 'whatever',
        'name': 'keys_confounds.json'
    })
    dummy_data.append({
        'download_url': 'whatever',
        'name': 'keys_phenotypic_data.json'
    })

    dummy_data = {'files': dummy_data}
    json.dump(dummy_data, open(dummy, 'w'))
    local_url = "file://" + dummy

    # All subjects
    cobre_data = check_deprecation(func.fetch_cobre,
                                   "'fetch_cobre' has been deprecated")(
                                       n_subjects=None,
                                       data_dir=str(tmp_path),
                                       url=local_url)

    phenotypic_names = [
        'func', 'confounds', 'phenotypic', 'description', 'desc_con',
        'desc_phenotypic'
    ]

    # test length of functional filenames to max 146
    assert len(cobre_data.func) == 146
    # test length of corresponding confounds files of same length to max 146
    assert len(cobre_data.confounds) == 146
    # test return type variables
    assert sorted(cobre_data) == sorted(phenotypic_names)
    # test functional filenames in a list
    assert isinstance(cobre_data.func, list)
    # test confounds files in a list
    assert isinstance(cobre_data.confounds, list)
    assert isinstance(cobre_data.func[0], str)
    # returned phenotypic data will be an array
    assert isinstance(cobre_data.phenotypic, np.recarray)

    # Fetch only 30 subjects
    data_30_subjects = func.fetch_cobre(n_subjects=30,
                                        url=local_url,
                                        data_dir=str(tmp_path))
    assert len(data_30_subjects.func) == 30
    assert len(data_30_subjects.confounds) == 30

    # Test more than maximum subjects
    test_150_subjects = func.fetch_cobre(n_subjects=150,
                                         url=local_url,
                                         data_dir=str(tmp_path))
    assert len(test_150_subjects.func) == 146
    os.remove(dummy)