# Path: cogspaces/datasets/derivative.py
# def fetch_atlas_modl(data_dir=None,
#                      url=None,
#                      resume=True, verbose=1):
#     """Download and load a multi-scale atlas computed using MODL over HCP900.
# 
#     Parameters
#     ----------
#     data_dir: string, optional
#         Path of the data directory. Used to force data storage in a non-
#         standard location. Default: None (meaning: default)
#     url: string, optional
#         Download URL of the dataset. Overwrite the default URL.
#     """
# 
#     if url is None:
#         url = 'http://cogspaces.github.io/assets/data/modl/'
# 
#     data_dir = get_data_dir(data_dir)
#     dataset_name = 'modl'
#     data_dir = _get_dataset_dir(dataset_name, data_dir=data_dir,
#                                 verbose=verbose)
# 
#     keys = ['components_64',
#             'components_128',
#             'components_453_gm',
#             'loadings_128_gm'
#             ]
# 
#     paths = [
#         'components_64.nii.gz',
#         'components_128.nii.gz',
#         'components_453_gm.nii.gz',
#         'loadings_128_gm.npy',
#     ]
#     urls = [url + path for path in paths]
#     files = [(path, url, {}) for path, url in zip(paths, urls)]
# 
#     files = _fetch_files(data_dir, files, resume=resume,
#                          verbose=verbose)
# 
#     params = {key: file for key, file in zip(keys, files)}
# 
#     fdescr = 'Components computed using the MODL package, at various scale,' \
#              'from HCP900 data'
# 
#     params['description'] = fdescr
#     params['data_dir'] = data_dir
# 
#     return Bunch(**params)
# 
# def fetch_mask(data_dir=None, url=None, resume=True, verbose=1):
#     if url is None:
#         url = 'http://cogspaces.github.io/assets/data/hcp_mask.nii.gz'
#     files = [('hcp_mask.nii.gz', url, {})]
# 
#     dataset_name = 'mask'
#     data_dir = get_data_dir(data_dir)
#     dataset_dir = _get_dataset_dir(dataset_name, data_dir=data_dir,
#                                    verbose=verbose)
#     files = _fetch_files(dataset_dir, files, resume=resume,
#                          verbose=verbose)
#     return files[0]


import numpy as np
import torch
from nilearn.input_data import NiftiMasker
from cogspaces.datasets import fetch_atlas_modl, fetch_mask
"""
    Analyse a model trained on data provided by load_reduced_loadings().
"""




# Note that 'components_453_gm' is currently hard-coded there
def compute_components(estimator, config, return_type='img'):
    """Compute components from a FactoredClassifier estimator"""
    module = curate_module(estimator)
    components = module.embedder.linear.weight.detach().numpy()

    if config['data']['reduced']:
        mask = fetch_mask()
        masker = NiftiMasker(mask_img=mask).fit()

# Path: cogspaces/datasets/derivative.py
# def fetch_atlas_modl(data_dir=None,
#                      url=None,
#                      resume=True, verbose=1):
#     """Download and load a multi-scale atlas computed using MODL over HCP900.
# 
#     Parameters
#     ----------
#     data_dir: string, optional
#         Path of the data directory. Used to force data storage in a non-
#         standard location. Default: None (meaning: default)
#     url: string, optional
#         Download URL of the dataset. Overwrite the default URL.
#     """
# 
#     if url is None:
#         url = 'http://cogspaces.github.io/assets/data/modl/'
# 
#     data_dir = get_data_dir(data_dir)
#     dataset_name = 'modl'
#     data_dir = _get_dataset_dir(dataset_name, data_dir=data_dir,
#                                 verbose=verbose)
# 
#     keys = ['components_64',
#             'components_128',
#             'components_453_gm',
#             'loadings_128_gm'
#             ]
# 
#     paths = [
#         'components_64.nii.gz',
#         'components_128.nii.gz',
#         'components_453_gm.nii.gz',
#         'loadings_128_gm.npy',
#     ]
#     urls = [url + path for path in paths]
#     files = [(path, url, {}) for path, url in zip(paths, urls)]
# 
#     files = _fetch_files(data_dir, files, resume=resume,
#                          verbose=verbose)
# 
#     params = {key: file for key, file in zip(keys, files)}
# 
#     fdescr = 'Components computed using the MODL package, at various scale,' \
#              'from HCP900 data'
# 
#     params['description'] = fdescr
#     params['data_dir'] = data_dir
# 
#     return Bunch(**params)
# 
# def fetch_mask(data_dir=None, url=None, resume=True, verbose=1):
#     if url is None:
#         url = 'http://cogspaces.github.io/assets/data/hcp_mask.nii.gz'
#     files = [('hcp_mask.nii.gz', url, {})]
# 
#     dataset_name = 'mask'
#     data_dir = get_data_dir(data_dir)
#     dataset_dir = _get_dataset_dir(dataset_name, data_dir=data_dir,
#                                    verbose=verbose)
#     files = _fetch_files(dataset_dir, files, resume=resume,
#                          verbose=verbose)
#     return files[0]


# Path: cogspaces/report.py
import numpy as np
import torch
from nilearn.input_data import NiftiMasker
from cogspaces.datasets import fetch_atlas_modl, fetch_mask

"""
    Analyse a model trained on data provided by load_reduced_loadings().
"""




# Note that 'components_453_gm' is currently hard-coded there
def compute_components(estimator, config, return_type='img'):
    """Compute components from a FactoredClassifier estimator"""
    module = curate_module(estimator)
    components = module.embedder.linear.weight.detach().numpy()

    if config['data']['reduced']:
        mask = fetch_mask()
        masker = NiftiMasker(mask_img=mask).fit()

        modl_atlas = fetch_atlas_modl()