Example #1
0
def save_dataset_instance(db_filename, kw_filename, instance_filename):
    # Create a new Dataset instance
    dataset = Dataset('./raw_data/' + db_filename + '.txt')
    # Add some features
    dataset.add_features('./raw_data/' + kw_filename + '.txt')
    # Save new file
    dataset.save('./raw_data/' + instance_filename + '.pkl')
    return dataset
Example #2
0
 def test_dataset_initializes(self):
     """ Test whether dataset initializes properly. """
     Dataset(get_test_data_path() + 'test_dataset.txt', get_test_data_path() + 'test_features.txt')
     self.assertIsNotNone(self.dataset.masker)
     self.assertIsNotNone(self.dataset.image_table)
     self.assertEqual(len(self.dataset.mappables), 5)
     self.assertIsNotNone(self.dataset.masker)
     self.assertIsNotNone(self.dataset.r)
     self.assertIsNotNone(self.dataset.mappables[0].data['extra_field'].iloc[2], 'field')
Example #3
0
def neurosynthInit(dbsize):
    """Initialize Neurosynth Database, return database object"""
    print "Initializing Neurosynth database..."
    db = Dataset('data/' + str(dbsize) + 'terms/database.txt')
    db.add_features('data/' + str(dbsize) + 'terms/features.txt')

    #print "Loading standard space brain..."
    #img = nb.load("data/MNI152_T1_2mm_brain.nii.gz")
    #standard = img.get_data()
    return db
    def __init__(self,
                 db,
                 dataset=None,
                 studies=None,
                 features=None,
                 reset_db=False,
                 reset_dataset=False,
                 download_data=False):
        """
        Initialize instance from a pickled Neurosynth Dataset instance or a
        pair of study and analysis .txt files.
        Args:
            db: the SQLAlchemy database connection to use.
            dataset: an optional filename of a pickled neurosynth Dataset
                instance.
            studies: name of file containing activation data. If passed, a new
                Dataset instance will be constructed.
            features: name of file containing feature data.
            reset_db: if True, will drop and re-create all database tables
                before adding new content. If False (default), will add content
                incrementally.
            reset_dataset: if True, will regenerate the pickled Neurosynth
                dataset.
            download_data: if True, ignores any existing files and downloads
                the latest Neurosynth data files from GitHub.
        """

        if (studies is not None and not os.path.exists(studies)) \
                or settings.RESET_ASSETS:
            print("WARNING: RESETTING ALL NEUROSYNTH ASSETS!")
            self.reset_assets(download_data)

        # Load or create Neurosynth Dataset instance
        if dataset is None or reset_dataset or (isinstance(dataset, str) and
                                                not os.path.exists(dataset)):
            print("\tInitializing a new Dataset...")
            if (studies is None) or (features is None):
                raise ValueError(
                    "To generate a new Dataset instance, both studies and "
                    "analyses must be provided.")
            dataset = Dataset(studies)
            dataset.add_features(features)
            dataset.save(settings.PICKLE_DATABASE)
        else:
            print("Loading existing Dataset...")
            dataset = Dataset.load(dataset)
            if features is not None:
                dataset.add_features(features)

        self.dataset = dataset
        self.db = db

        if reset_db:
            print("WARNING: RESETTING DATABASE!!!")
            self.reset_database()
Example #5
0
    def get_dataset(self, force_load=False):
        if os.path.exists(os.path.join(self.datadir,
                                       'dataset.pkl')) and not force_load:
            print('loading database from',
                  os.path.join(self.datadir, 'dataset.pkl'))
            self.dataset = Dataset.load(
                os.path.join(self.datadir, 'dataset.pkl'))
        else:
            print('loading database - this takes a few minutes')
            self.dataset = Dataset(os.path.join(self.datadir, 'database.txt'))
            self.dataset.add_features(
                os.path.join(self.datadir, 'features.txt'))

            self.dataset.save(os.path.join(self.datadir, 'dataset.pkl'))
Example #6
0
def fetch_neurosynth_dataset(data_dir, return_pkl=True):
    """Downloads the Neurosynth dataset

    Parameters
    ----------
    data_dir : str
        Directory in which to download the dataset.
    return_pkl : bool
        If true, creates and returns the .pkl file. Otherwise returns
        the dataset and features files.

    Returns
    -------
    tuple, str
        If save_pkl is false, returns a tuple containing the path to the
        database.txt and the features.txt file. Otherwise returns the path
        to the .pkl file.

    """
    if not os.path.isdir(data_dir):
        os.mkdir(data_dir)

    dataset_file = os.path.join(data_dir, "database.txt")
    if not os.path.isfile(dataset_file):
        logging.info("Downloading the Neurosynth dataset.")
        download(data_dir, unpack=True)
    feature_file = os.path.join(data_dir, "features.txt")

    if return_pkl:
        pkl_file = os.path.join(data_dir, "dataset.pkl")
        if not os.path.isfile(pkl_file):
            logging.info(
                "Converting Neurosynth data to a .pkl file. This may take a while."
            )
            dataset = Dataset(dataset_file, feature_file)
            dataset.save(pkl_file)
        return pkl_file

    return (dataset_file, feature_file)
Example #7
0
from neurosynth.base.dataset import Dataset
from neurosynth.analysis import meta
import os
dataset = Dataset('database.txt')
dataset.add_features('features.txt')
print dataset.get_feature_names()
ids = dataset.get_ids_by_features('emo*', threshold=0.001)
print len(ids)
ma = meta.MetaAnalysis(dataset, ids)
ma.save_results('emotion')
    if mask_img_data.shape != (91, 109, 91):
        resampled_roi = resample_to_img(
            roi, mni152_2mm, interpolation="nearest", copy=True
        )
        resampled_file = join(map_dir, "{0}_mni2mm.nii.gz".format(file))
        resampled_roi.to_filename(resampled_file)
        roi_files[i] = resampled_file
        plot_glass_brain(
            resampled_file,
            output_file=join(map_dir, "{0}_mni2mm.png".format(basename(file))),
        )


print("loading dataset...")
tds = datetime.now()
dataset = Dataset("/Users/kbottenh/Dropbox/Data/neurosynth-v0.7/database.txt")
tdf = datetime.now()


print("dataset loaded! only took {0}".format((tdf - tds)))

for i in np.arange(0, len(mask_names)):
    print("{0}\nmeta-analyzing {1}...".format(datetime.now(), mask_names[i]))
    tmas = datetime.now()
    ids = dataset.get_studies(
        mask=roi_files[i],
    )
    ma = meta.MetaAnalysis(dataset, ids)
    ma.save_results(
        output_dir=sink_dir,
        prefix=mask_names[i],
Example #9
0
import neurosynth.base.dataset
from neurosynth.base.dataset import Dataset
print neurosynth.base.dataset.__file__
dataset = Dataset('../data/unprocessed/abstract/full_database_revised.txt')
dataset.add_features('../data/unprocessed/abstract/abstract_features.txt')
dataset.save('../data/datasets/dataset_abs_words_pandas.pkl')

dataset = Dataset('../data/unprocessed/abstract/full_database_revised.txt')
dataset.add_features('../data/unprocessed/abstract_topics/doc_features100.txt')
dataset.save('../data/datasets/dataset_abs_topics_pandas.pkl')
Example #10
0
def get_test_dataset():
    test_data_path = get_test_data_path()
    dataset = Dataset(test_data_path + 'test_dataset.txt')
    dataset.add_features(test_data_path + 'test_features.txt')
    return dataset
Example #11
0
        resampled_roi = resample_to_img(roi,
                                        mni152_2mm,
                                        interpolation='nearest',
                                        copy=True)
        resampled_file = join(map_dir, '{0}_mni2mm.nii.gz'.format(file))
        resampled_roi.to_filename(resampled_file)
        roi_files[i] = resampled_file
        plot_glass_brain(resampled_file,
                         output_file=join(
                             map_dir, '{0}_mni2mm.png'.format(basename(file))))

# In[16]:

print('loading dataset...')
tds = datetime.now()
dataset = Dataset('/Users/Katie/Dropbox/Data/neurosynth-v0.7/database.txt')
dataset.add_features('/Users/Katie/Dropbox/Data/neurosynth-v0.7/features.txt')
tdf = datetime.now()

print('dataset loaded! only took {0}'.format((tdf - tds)))

for i in np.arange(0, len(mask_names)):
    print('{0}\nmeta-analyzing {1}...'.format(datetime.now(), mask_names[i]))
    tmas = datetime.now()
    ids = dataset.get_studies(mask=roi_files[i], )
    ma = meta.MetaAnalysis(dataset, ids)
    ma.save_results(
        output_dir=sink_dir,
        prefix=mask_names[i],
        image_list=['association-test_z', 'association-test_z_FDR_0.01'])
    tmaf = datetime.now()
Example #12
0
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
Example #13
0
xfm2vol.run()


#make masks to input into neurosynth
def cluster2masks(clusterfile):
    clustermap = nb.load(clusterfile).get_data()
    for x in range(1, clustermap.max() + 1):
        clustermask = (clustermap == x).astype(int)
        nImg = nb.Nifti1Image(clustermask, None)
        nb.save(
            nImg,
            os.path.abspath(clusterfile + '_clustermask' + str(x) + '.nii'))


cluster2masks(volume_file)

dataset_file = '/home/raid3/watanabe/neurosynth/data/dataset.pkl'
if not os.path.exists(dataset_file):
    dataset = Dataset('/home/raid3/watanabe/neurosynth/data/database.txt')
    dataset.add_features('/home/raid3/watanabe/neurosynth/data/features.txt')
    dataset.save(dataset_file)
else:
    dataset = cPickle.load(open(dataset_file, 'rb'))

clustermask = volume_file + '_clustermask' + str(3) + '.nii'

ids = dataset.get_ids_by_mask(clustermask)
features = dataset.feature_table.get_features_by_ids(ids)

#mri_surf2vol --identity fsaverage4 --surfval /scr/ilz1/Data/attemptsurface.nii --hemi 'lh' --o /scr/ilz1/Data/results/surf2volume.nii --template /scr/ilz1/Data/freesurfer/fsaverage4/mri/orig.mgz
Example #14
0
 def __init__(self,dbsize):
   print "Initializing Neurosynth database..."
   self.db = Dataset('data/' + str(dbsize) + 'terms/database.txt')
   self.db.add_features('data/' + str(dbsize) + 'terms/features.txt')
   self.ids = self.getIDs()
   self.decoder = None
Example #15
0
from neurosynth.analysis import meta
from neurosynth.base.dataset import Dataset
""" Create a new Dataset instance from a database file and load features.
This is basically the example from the quickstart in the README.
Assumes you have database.txt and features.txt files in the current dir.
"""
""" Load a Dataset and generate a full set of meta-analysis
images--i.e., run a meta-analysis on every single feature.
"""

neurosynth_data_dir = "/home/data/nbc/misc-projects/niconn-macm/code/neurosynth/"

if not op.isfile(op.join(neurosynth_data_dir, "dataset.pkl")):
    # Create Dataset instance from a database file.
    dataset = Dataset(op.join(neurosynth_data_dir, "database.txt"))

    # Load features from file
    dataset.add_features(op.join(neurosynth_data_dir, "features.txt"))

    # Pickle the Dataset to file so we can use Dataset.load() next time
    # instead of having to sit through the generation process again.
    dataset.save(op.join(neurosynth_data_dir, "dataset.pkl"))

# Load pickled Dataset--assumes you've previously saved it. If not,
# follow the create_a_new_dataset_and_load_features example.
dataset = Dataset.load(op.join(neurosynth_data_dir, "dataset.pkl"))

# Get the full list of feature names
feature_list = dataset.get_feature_names()