# Path: neurosynth/analysis/cluster.py
# class Clusterable(object):
#     def __init__(self, dataset, mask=None, features=None,
#                  feature_threshold=None, min_voxels=None, min_studies=None):
#     def transform(self, transformer, transpose=False):
# def magic(dataset, method='coactivation', roi_mask=None,
#           coactivation_mask=None, features=None, feature_threshold=0.05,
#           min_voxels_per_study=None, min_studies_per_voxel=None,
#           reduce_reference='pca', n_components=100,
#           distance_metric='correlation', clustering_algorithm='kmeans',
#           n_clusters=5, clustering_kwargs={}, output_dir=None, filename=None,
#           coactivation_images=False, coactivation_threshold=0.1):
# 
# Path: neurosynth/analysis/reduce.py
# def average_within_regions(dataset, regions, masker=None, threshold=None,
#                            remove_zero=True):
# def apply_grid(dataset, masker=None, scale=5, threshold=None):
# def get_random_voxels(dataset, n_voxels):
# def _get_top_words(model, feature_names, n_top_words=40):
# def run_lda(abstracts, n_topics=50, n_words=31, n_iters=1000, alpha=None,
#             beta=0.001):
#     def clean_str(string):
#     def get_sort(lst):
# 
# Path: neurosynth/analysis/decode.py
# def decode(self, images, save=None, round=4, names=None, **kwargs):
#     """ Decodes a set of images.
# 
#     Args:
#       images: The images to decode. Can be:
#         - A single String specifying the filename of the image to decode
#         - A list of filenames
#         - A single NumPy array containing the image data
#       save: Optional filename to save results to. If None (default), returns
#         all results as an array.
#       round: Optional integer indicating number of decimals to round result
#         to. Defaults to 4.
#       names: Optional list of names corresponding to the images in filenames.
#         If passed, must be of same length and in same order as filenames.
#         By default, the columns in the output will be named using the image
#         filenames.
# 
#     Returns:
#       An n_features x n_files numpy array, where each feature is a row and
#       each image is a column. The meaning of the values depends on the
#       decoding method used. """
# 
#     if isinstance(images, string_types):
#         images = [images]
# 
#     if isinstance(images, list):
#         imgs_to_decode = imageutils.load_imgs(images, self.masker)
#     else:
#         imgs_to_decode = images
# 
#     methods = {
#         'pearson': self._pearson_correlation,
#         'dot': self._dot_product,
#         'roi': self._roi_association
#     }
# 
#     result = np.around(
#         methods[self.method](imgs_to_decode, **kwargs), round)
# 
#     # if save is not None:
# 
#     if names is None:
#         if type(images).__module__ == np.__name__:
#             names = ['image_%d' % i for i in range(images.shape[1])]
#         elif self.method == 'roi':
#             names = ['cluster_%d' % i for i in range(result.shape[1])]
#         else:
#             names = images
# 
#     result = pd.DataFrame(result, columns=names, index=self.feature_names)
# 
#     if save is not None:
#         result.to_csv(save, index_label='Feature')
#     return result
# 
# Path: neurosynth/analysis/meta.py
# def analyze_features(dataset, features=None, image_type='association-test_z',
#                      threshold=0.001, q=0.01, output_dir=None, prefix=None):
#     def __init__(self, dataset, ids, ids2=None, q=0.01, prior=0.5,
#                  min_studies=1):
#         def p_to_z(p, sign):
#     def save_results(self, output_dir='.', prefix='', prefix_sep='_',
#                      image_list=None):
# class MetaAnalysis(object):
# 
# Path: neurosynth/analysis/stats.py
# def pearson(x, y):
# def two_way(cells):
# def one_way(data, n):
# def fdr(p, q=.05):
# 
# Path: neurosynth/analysis/network.py
# def coactivation(dataset, seed, threshold=0.0, output_dir='.', prefix='', r=6):
# 
# Path: neurosynth/tests/utils.py
# def get_test_dataset(prefix='test'):
#     test_data_path = get_test_data_path()
#     dataset = Dataset(test_data_path + '%s_dataset.txt' % prefix)
#     dataset.add_features(test_data_path + '%s_features.txt' % prefix)
#     return dataset
# 
# def get_test_data_path():
#     """Returns the path to test datasets, terminated with separator (/ vs \)"""
#     # TODO: do not rely on __file__
#     return join(dirname(__file__), 'data') + pathsep


import unittest
import numpy as np
import tempfile
import os
import shutil
import nibabel as nb
from neurosynth.analysis import cluster
from neurosynth.analysis import reduce
from neurosynth.analysis import decode
from neurosynth.analysis import meta
from neurosynth.analysis import stats
from neurosynth.analysis import network
from neurosynth.tests.utils import get_test_dataset, get_test_data_path
from numpy.testing import assert_array_almost_equal
from glob import glob
        from sklearn.decomposition import PCA
        from sklearn.cluster import KMeans


class TestAnalysis(unittest.TestCase):

    @classmethod
    def setUpClass(self):
        """ Create a new Dataset and add features. """
        self.dataset = get_test_dataset()

# Path: neurosynth/analysis/cluster.py
# class Clusterable(object):
#     def __init__(self, dataset, mask=None, features=None,
#                  feature_threshold=None, min_voxels=None, min_studies=None):
#     def transform(self, transformer, transpose=False):
# def magic(dataset, method='coactivation', roi_mask=None,
#           coactivation_mask=None, features=None, feature_threshold=0.05,
#           min_voxels_per_study=None, min_studies_per_voxel=None,
#           reduce_reference='pca', n_components=100,
#           distance_metric='correlation', clustering_algorithm='kmeans',
#           n_clusters=5, clustering_kwargs={}, output_dir=None, filename=None,
#           coactivation_images=False, coactivation_threshold=0.1):
# 
# Path: neurosynth/analysis/reduce.py
# def average_within_regions(dataset, regions, masker=None, threshold=None,
#                            remove_zero=True):
# def apply_grid(dataset, masker=None, scale=5, threshold=None):
# def get_random_voxels(dataset, n_voxels):
# def _get_top_words(model, feature_names, n_top_words=40):
# def run_lda(abstracts, n_topics=50, n_words=31, n_iters=1000, alpha=None,
#             beta=0.001):
#     def clean_str(string):
#     def get_sort(lst):
# 
# Path: neurosynth/analysis/decode.py
# def decode(self, images, save=None, round=4, names=None, **kwargs):
#     """ Decodes a set of images.
# 
#     Args:
#       images: The images to decode. Can be:
#         - A single String specifying the filename of the image to decode
#         - A list of filenames
#         - A single NumPy array containing the image data
#       save: Optional filename to save results to. If None (default), returns
#         all results as an array.
#       round: Optional integer indicating number of decimals to round result
#         to. Defaults to 4.
#       names: Optional list of names corresponding to the images in filenames.
#         If passed, must be of same length and in same order as filenames.
#         By default, the columns in the output will be named using the image
#         filenames.
# 
#     Returns:
#       An n_features x n_files numpy array, where each feature is a row and
#       each image is a column. The meaning of the values depends on the
#       decoding method used. """
# 
#     if isinstance(images, string_types):
#         images = [images]
# 
#     if isinstance(images, list):
#         imgs_to_decode = imageutils.load_imgs(images, self.masker)
#     else:
#         imgs_to_decode = images
# 
#     methods = {
#         'pearson': self._pearson_correlation,
#         'dot': self._dot_product,
#         'roi': self._roi_association
#     }
# 
#     result = np.around(
#         methods[self.method](imgs_to_decode, **kwargs), round)
# 
#     # if save is not None:
# 
#     if names is None:
#         if type(images).__module__ == np.__name__:
#             names = ['image_%d' % i for i in range(images.shape[1])]
#         elif self.method == 'roi':
#             names = ['cluster_%d' % i for i in range(result.shape[1])]
#         else:
#             names = images
# 
#     result = pd.DataFrame(result, columns=names, index=self.feature_names)
# 
#     if save is not None:
#         result.to_csv(save, index_label='Feature')
#     return result
# 
# Path: neurosynth/analysis/meta.py
# def analyze_features(dataset, features=None, image_type='association-test_z',
#                      threshold=0.001, q=0.01, output_dir=None, prefix=None):
#     def __init__(self, dataset, ids, ids2=None, q=0.01, prior=0.5,
#                  min_studies=1):
#         def p_to_z(p, sign):
#     def save_results(self, output_dir='.', prefix='', prefix_sep='_',
#                      image_list=None):
# class MetaAnalysis(object):
# 
# Path: neurosynth/analysis/stats.py
# def pearson(x, y):
# def two_way(cells):
# def one_way(data, n):
# def fdr(p, q=.05):
# 
# Path: neurosynth/analysis/network.py
# def coactivation(dataset, seed, threshold=0.0, output_dir='.', prefix='', r=6):
# 
# Path: neurosynth/tests/utils.py
# def get_test_dataset(prefix='test'):
#     test_data_path = get_test_data_path()
#     dataset = Dataset(test_data_path + '%s_dataset.txt' % prefix)
#     dataset.add_features(test_data_path + '%s_features.txt' % prefix)
#     return dataset
# 
# def get_test_data_path():
#     """Returns the path to test datasets, terminated with separator (/ vs \)"""
#     # TODO: do not rely on __file__
#     return join(dirname(__file__), 'data') + pathsep


# Path: neurosynth/tests/test_analysis.py
import unittest
import numpy as np
import tempfile
import os
import shutil
import nibabel as nb
from neurosynth.analysis import cluster
from neurosynth.analysis import reduce
from neurosynth.analysis import decode
from neurosynth.analysis import meta
from neurosynth.analysis import stats
from neurosynth.analysis import network
from neurosynth.tests.utils import get_test_dataset, get_test_data_path
from numpy.testing import assert_array_almost_equal
from glob import glob
        from sklearn.decomposition import PCA
        from sklearn.cluster import KMeans



class TestAnalysis(unittest.TestCase):

    @classmethod
    def setUpClass(self):
        """ Create a new Dataset and add features. """
        self.dataset = get_test_dataset()

        self.real_dataset = get_test_dataset(prefix='test_real')