# Path: cogspaces/raw_datasets/hcp.py
# def fetch_hcp(data_dir=None, n_subjects=None, subjects=None,
#               from_file=False,
#               on_disk=True):
#     root = get_data_dirs(data_dir)[0]
#     mask = fetch_hcp_mask(data_dir)
#     if not from_file:
#         rest = fetch_hcp_timeseries(data_dir, data_type='rest',
#                                     n_subjects=n_subjects, subjects=subjects,
#                                     on_disk=on_disk)
#         task = fetch_hcp_timeseries(data_dir, data_type='task',
#                                     n_subjects=n_subjects, subjects=subjects,
#                                     on_disk=on_disk)
#         contrasts = fetch_hcp_contrasts(data_dir,
#                                         output='nistats',
#                                         n_subjects=n_subjects,
#                                         subjects=subjects,
#                                         on_disk=on_disk)
#         behavioral = fetch_behavioral_data(data_dir)
#         indices = []
#         for df in rest, task, contrasts:
#             if not df.empty:
#                 indices.append(df.index.get_level_values('subject').
#                                unique().values)
#         if indices:
#             index = indices[0]
#             for this_index in indices[1:]:
#                 index = np.union1d(index, this_index)
#             behavioral = behavioral.loc[index]
#         else:
#             behavioral = pd.DataFrame([])
#     else:
#         rest = pd.read_csv(join(root, 'parietal', 'rest.csv'))
#         task = pd.read_csv(join(root, 'parietal', 'task.csv'))
#         contrasts = pd.read_csv(join(root, 'parietal',
#                                      'contrasts.csv'))
#         behavioral = pd.read_csv(join(root, 'parietal',
#                                       'behavioral.csv'))
#         behavioral.set_index('subject', inplace=True)
#         rest.set_index(['subject', 'session', 'direction'], inplace=True)
#         task.set_index(['subject', 'task', 'direction'], inplace=True)
#         contrasts.set_index(['subject', 'task', 'contrast', 'direction'],
#                             inplace=True)
#         if subjects is None:
#             subjects = fetch_subject_list(data_dir=data_dir,
#                                           n_subjects=n_subjects)
#         rest = rest.loc[subjects]
#         task = task.loc[subjects]
#         contrasts = contrasts.loc[subjects]
#         behavioral = behavioral.loc[subjects]
# 
#     return Bunch(rest=rest,
#                  contrasts=contrasts,
#                  task=task,
#                  behavioral=behavioral,
#                  mask=mask,
#                  root=root)
# 
# Path: cogspaces/datasets/utils.py
# def get_data_dir(data_dir=None):
#     """ Returns the directories in which to look for data.
# 
#     Parameters
#     ----------
#     data_dir: string, optional
#         Path of the utils directory. Used to force utils storage in a specified
#         location. Default: None
# 
#     Returns
#     -------
#     path: string
#         Path of the data directory.
#     """
# 
#     # Check data_dir which force storage in a specific location
#     if data_dir is not None:
#         assert (isinstance(data_dir, str))
#         return data_dir
#     elif 'COGSPACES_DATA' in os.environ:
#         return os.environ['COGSPACES_DATA']
#     else:
#         return os.path.expanduser('~/cogspaces_data')


import glob
import os
import re
import pandas as pd
import yaml
from os.path import join
from sklearn.utils import Bunch
from .hcp import fetch_hcp as hcp_builder_fetch_hcp
from ..datasets.utils import get_data_dir
    z_maps = glob.glob(join(source_dir, '*/*/*', 'z_*.nii.gz'))
    subjects = []
    contrasts = []
    tasks = []
    filtered_z_maps = []
    directions = []
    for z_map in z_maps:
        dirname, contrast = os.path.split(z_map)
        contrast = contrast[2:-7]
        dirname, _ = os.path.split(dirname)
        dirname, task = os.path.split(dirname)
        dirname, subject = os.path.split(dirname)
        subject = int(subject[-3:])
        subjects.append(subject)
        contrasts.append(contrast)
        tasks.append(task)
        filtered_z_maps.append(z_map)
        directions.append('level1')
    df = pd.DataFrame(data={'subject': subjects,
                            'task': tasks,
                            'contrast': contrasts,
                            'direction': 'level1',
                            'study': 'human_voice',
                            'z_map': filtered_z_maps, })
    df.set_index(['subject', 'task', 'contrast', 'direction'], inplace=True)
    df.sort_index(inplace=True)
    return df


def fetch_la5c(data_dir=None):

# Path: cogspaces/raw_datasets/hcp.py
# def fetch_hcp(data_dir=None, n_subjects=None, subjects=None,
#               from_file=False,
#               on_disk=True):
#     root = get_data_dirs(data_dir)[0]
#     mask = fetch_hcp_mask(data_dir)
#     if not from_file:
#         rest = fetch_hcp_timeseries(data_dir, data_type='rest',
#                                     n_subjects=n_subjects, subjects=subjects,
#                                     on_disk=on_disk)
#         task = fetch_hcp_timeseries(data_dir, data_type='task',
#                                     n_subjects=n_subjects, subjects=subjects,
#                                     on_disk=on_disk)
#         contrasts = fetch_hcp_contrasts(data_dir,
#                                         output='nistats',
#                                         n_subjects=n_subjects,
#                                         subjects=subjects,
#                                         on_disk=on_disk)
#         behavioral = fetch_behavioral_data(data_dir)
#         indices = []
#         for df in rest, task, contrasts:
#             if not df.empty:
#                 indices.append(df.index.get_level_values('subject').
#                                unique().values)
#         if indices:
#             index = indices[0]
#             for this_index in indices[1:]:
#                 index = np.union1d(index, this_index)
#             behavioral = behavioral.loc[index]
#         else:
#             behavioral = pd.DataFrame([])
#     else:
#         rest = pd.read_csv(join(root, 'parietal', 'rest.csv'))
#         task = pd.read_csv(join(root, 'parietal', 'task.csv'))
#         contrasts = pd.read_csv(join(root, 'parietal',
#                                      'contrasts.csv'))
#         behavioral = pd.read_csv(join(root, 'parietal',
#                                       'behavioral.csv'))
#         behavioral.set_index('subject', inplace=True)
#         rest.set_index(['subject', 'session', 'direction'], inplace=True)
#         task.set_index(['subject', 'task', 'direction'], inplace=True)
#         contrasts.set_index(['subject', 'task', 'contrast', 'direction'],
#                             inplace=True)
#         if subjects is None:
#             subjects = fetch_subject_list(data_dir=data_dir,
#                                           n_subjects=n_subjects)
#         rest = rest.loc[subjects]
#         task = task.loc[subjects]
#         contrasts = contrasts.loc[subjects]
#         behavioral = behavioral.loc[subjects]
# 
#     return Bunch(rest=rest,
#                  contrasts=contrasts,
#                  task=task,
#                  behavioral=behavioral,
#                  mask=mask,
#                  root=root)
# 
# Path: cogspaces/datasets/utils.py
# def get_data_dir(data_dir=None):
#     """ Returns the directories in which to look for data.
# 
#     Parameters
#     ----------
#     data_dir: string, optional
#         Path of the utils directory. Used to force utils storage in a specified
#         location. Default: None
# 
#     Returns
#     -------
#     path: string
#         Path of the data directory.
#     """
# 
#     # Check data_dir which force storage in a specific location
#     if data_dir is not None:
#         assert (isinstance(data_dir, str))
#         return data_dir
#     elif 'COGSPACES_DATA' in os.environ:
#         return os.environ['COGSPACES_DATA']
#     else:
#         return os.path.expanduser('~/cogspaces_data')


# Path: cogspaces/raw_datasets/contrast.py
import glob
import os
import re
import pandas as pd
import yaml
from os.path import join
from sklearn.utils import Bunch
from .hcp import fetch_hcp as hcp_builder_fetch_hcp
from ..datasets.utils import get_data_dir

    z_maps = glob.glob(join(source_dir, '*/*/*', 'z_*.nii.gz'))
    subjects = []
    contrasts = []
    tasks = []
    filtered_z_maps = []
    directions = []
    for z_map in z_maps:
        dirname, contrast = os.path.split(z_map)
        contrast = contrast[2:-7]
        dirname, _ = os.path.split(dirname)
        dirname, task = os.path.split(dirname)
        dirname, subject = os.path.split(dirname)
        subject = int(subject[-3:])
        subjects.append(subject)
        contrasts.append(contrast)
        tasks.append(task)
        filtered_z_maps.append(z_map)
        directions.append('level1')
    df = pd.DataFrame(data={'subject': subjects,
                            'task': tasks,
                            'contrast': contrasts,
                            'direction': 'level1',
                            'study': 'human_voice',
                            'z_map': filtered_z_maps, })
    df.set_index(['subject', 'task', 'contrast', 'direction'], inplace=True)
    df.sort_index(inplace=True)
    return df


def fetch_la5c(data_dir=None):

    data_dir = get_data_dir(data_dir)