/
dataset.py
executable file
·677 lines (564 loc) · 23.8 KB
/
dataset.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
"""Classes for representing datasets of images and/or coordinates."""
import copy
import inspect
import json
import logging
import os.path as op
import numpy as np
import pandas as pd
from nilearn._utils import load_niimg
from .base import NiMAREBase
from .utils import (
_dict_to_coordinates,
_dict_to_df,
_listify,
_transform_coordinates_to_space,
_try_prepend,
_validate_df,
_validate_images_df,
get_masker,
get_template,
mm2vox,
)
LGR = logging.getLogger(__name__)
class Dataset(NiMAREBase):
"""Storage container for a coordinate- and/or image-based meta-analytic dataset/database.
.. versionchanged:: 0.0.9
* [ENH] Add merge method to Dataset class
.. versionchanged:: 0.0.8
* [FIX] Set ``nimare.dataset.Dataset.basepath`` in :func:`update_path` using absolute path.
Parameters
----------
source : :obj:`str` or :obj:`dict`
JSON file containing dictionary with database information or the dict()
object
target : :obj:`str`, optional
Desired coordinate space for coordinates. Names follow NIDM convention.
Default is 'mni152_2mm' (MNI space with 2x2x2 voxels).
This parameter has no impact on images.
mask : :obj:`str`, :class:`nibabel.nifti1.Nifti1Image`, \
:class:`nilearn.input_data.NiftiMasker` or similar, or None, optional
Mask(er) to use. If None, uses the target space image, with all
non-zero voxels included in the mask.
Attributes
----------
ids : 1D :class:`numpy.ndarray`
Identifiers
masker : :class:`nilearn.input_data.NiftiMasker` or similar
Masker object defining the space and location of the area of interest
(e.g., 'brain').
space : :obj:`str`
Standard space. Same as ``target`` parameter.
annotations : :class:`pandas.DataFrame`
Labels describing studies
coordinates : :class:`pandas.DataFrame`
Peak coordinates from studies
images : :class:`pandas.DataFrame`
Images from studies
metadata : :class:`pandas.DataFrame`
Metadata describing studies
texts : :class:`pandas.DataFrame`
Texts associated with studies
Notes
-----
Images loaded into a Dataset are assumed to be in the same space.
If images have different resolutions or affines from the Dataset's masker,
then they will be resampled automatically, at the point where they're used,
by :obj:`Dataset.masker`.
"""
_id_cols = ["id", "study_id", "contrast_id"]
def __init__(self, source, target="mni152_2mm", mask=None):
if isinstance(source, str):
with open(source, "r") as f_obj:
data = json.load(f_obj)
elif isinstance(source, dict):
data = source
else:
raise Exception("`source` needs to be a file path or a dictionary")
# Datasets are organized by study, then experiment
# To generate unique IDs, we combine study ID with experiment ID
# build list of ids
id_columns = ["id", "study_id", "contrast_id"]
all_ids = []
for pid in data.keys():
for expid in data[pid]["contrasts"].keys():
id_ = f"{pid}-{expid}"
all_ids.append([id_, pid, expid])
id_df = pd.DataFrame(columns=id_columns, data=all_ids)
id_df = id_df.set_index("id", drop=False)
self._ids = id_df.index.values
# Set up Masker
if mask is None:
mask = get_template(target, mask="brain")
self.masker = mask
self.space = target
self.annotations = _dict_to_df(id_df, data, key="labels")
self.coordinates = _dict_to_coordinates(data, masker=self.masker, space=self.space)
self.images = _dict_to_df(id_df, data, key="images")
self.metadata = _dict_to_df(id_df, data, key="metadata")
self.texts = _dict_to_df(id_df, data, key="text")
self.basepath = None
def __repr__(self):
"""Show basic Dataset representation.
It's basically the same as the NiMAREBase representation, but with the number of
experiments in the Dataset represented as well.
"""
# Get default parameter values for the object
signature = inspect.signature(self.__init__)
defaults = {
k: v.default
for k, v in signature.parameters.items()
if v.default is not inspect.Parameter.empty
}
# Eliminate any sub-parameters (e.g., parameters for a MetaEstimator's KernelTransformer),
# as well as default values
params = self.get_params()
params = {k: v for k, v in params.items() if "__" not in k}
# Parameter "target" is stored as attribute "space"
# and we want to show it regardless of whether it's the default or not
params["space"] = self.space
params.pop("target")
params = {k: v for k, v in params.items() if defaults.get(k) != v}
# Convert to strings
param_strs = []
for k, v in params.items():
if isinstance(v, str):
# Wrap string values in single quotes
param_str = f"{k}='{v}'"
else:
# Keep everything else as-is based on its own repr
param_str = f"{k}={v}"
param_strs.append(param_str)
params_str = ", ".join(param_strs)
params_str = f"{len(self.ids)} experiments{', ' if params_str else ''}{params_str}"
rep = f"{self.__class__.__name__}({params_str})"
return rep
@property
def ids(self):
"""numpy.ndarray: 1D array of identifiers in Dataset.
The associated setter for this property is private, as ``Dataset.ids`` is immutable.
"""
return self.__ids
@ids.setter
def _ids(self, ids):
ids = np.sort(np.asarray(ids))
assert isinstance(ids, np.ndarray) and ids.ndim == 1
self.__ids = ids
@property
def masker(self):
""":class:`nilearn.input_data.NiftiMasker` or similar: Masker object.
Defines the space and location of the area of interest (e.g., 'brain').
"""
return self.__masker
@masker.setter
def masker(self, mask):
mask = get_masker(mask)
if hasattr(self, "masker") and not np.array_equal(
self.masker.mask_img.affine, mask.mask_img.affine
):
# This message does not have an associated effect,
# since matrix indices are calculated as necessary
LGR.warning("New masker does not match old masker. Space is assumed to be the same.")
self.__masker = mask
@property
def annotations(self):
""":class:`pandas.DataFrame`: Labels describing studies in the dataset.
Each study/experiment has its own row.
Columns correspond to individual labels (e.g., 'emotion'), and may
be prefixed with a feature group including two underscores
(e.g., 'Neurosynth_TFIDF__emotion').
"""
return self.__annotations
@annotations.setter
def annotations(self, df):
_validate_df(df)
self.__annotations = df.sort_values(by="id")
@property
def coordinates(self):
""":class:`pandas.DataFrame`: Coordinates in the dataset.
.. versionchanged:: 0.0.10
The coordinates attribute no longer includes the associated matrix indices
(columns 'i', 'j', and 'k'). These columns are calculated as needed.
Each study has one row for each peak.
Columns include ['x', 'y', 'z'] (peak locations in mm) and 'space' (Dataset's space).
"""
return self.__coordinates
@coordinates.setter
def coordinates(self, df):
_validate_df(df)
self.__coordinates = df.sort_values(by="id")
@property
def images(self):
""":class:`pandas.DataFrame`: Images in the dataset.
Each image type has its own column (e.g., 'z') with absolute paths to
files and each study has its own row.
Additionally, relative paths to image files are stored in columns with
the suffix '__relative' (e.g., 'z__relative').
Warnings
--------
Images are assumed to be in the same space, although they may have
different resolutions and affines. Images will be resampled as needed
at the point where they are used, via :obj:`Dataset.masker`.
"""
return self.__images
@images.setter
def images(self, df):
_validate_df(df)
self.__images = _validate_images_df(df).sort_values(by="id")
@property
def metadata(self):
""":class:`pandas.DataFrame`: Metadata describing studies in the dataset.
Each metadata field has its own column (e.g., 'sample_sizes') and each study
has its own row.
"""
return self.__metadata
@metadata.setter
def metadata(self, df):
_validate_df(df)
self.__metadata = df.sort_values(by="id")
@property
def texts(self):
""":class:`pandas.DataFrame`: Texts in the dataset.
Each text type has its own column (e.g., 'abstract') and each study
has its own row.
"""
return self.__texts
@texts.setter
def texts(self, df):
_validate_df(df)
self.__texts = df.sort_values(by="id")
def slice(self, ids):
"""Create a new dataset with only requested IDs.
Parameters
----------
ids : array_like
List of study IDs to include in new dataset
Returns
-------
new_dset : :obj:`~nimare.dataset.Dataset`
Reduced Dataset containing only requested studies.
"""
new_dset = copy.deepcopy(self)
new_dset._ids = ids
for attribute in ("annotations", "coordinates", "images", "metadata", "texts"):
df = getattr(new_dset, attribute)
df = df.loc[df["id"].isin(ids)]
setattr(new_dset, attribute, df)
return new_dset
def merge(self, right):
"""Merge two Datasets.
.. versionadded:: 0.0.9
Parameters
----------
right : :obj:`~nimare.dataset.Dataset`
Dataset to merge with.
Returns
-------
:obj:`~nimare.dataset.Dataset`
A Dataset of the two merged Datasets.
"""
assert isinstance(right, Dataset)
shared_ids = np.intersect1d(self.ids, right.ids)
if shared_ids.size:
raise Exception("Duplicate IDs detected in both datasets.")
all_ids = np.concatenate((self.ids, right.ids))
new_dset = copy.deepcopy(self)
new_dset._ids = all_ids
for attribute in ("annotations", "coordinates", "images", "metadata", "texts"):
df1 = getattr(self, attribute)
df2 = getattr(right, attribute)
new_df = df1.append(df2, ignore_index=True, sort=False)
new_df.sort_values(by="id", inplace=True)
new_df.reset_index(drop=True, inplace=True)
new_df = new_df.where(~new_df.isna(), None)
setattr(new_dset, attribute, new_df)
new_dset.coordinates = _transform_coordinates_to_space(
new_dset.coordinates,
self.masker,
self.space,
)
return new_dset
def update_path(self, new_path):
"""Update paths to images.
Prepends new path to the relative path for files in Dataset.images.
Parameters
----------
new_path : :obj:`str`
Path to prepend to relative paths of files in Dataset.images.
"""
self.basepath = op.abspath(new_path)
df = self.images
relative_path_cols = [c for c in df if c.endswith("__relative")]
for col in relative_path_cols:
abs_col = col.replace("__relative", "")
if abs_col in df.columns:
LGR.info(f"Overwriting images column {abs_col}")
df[abs_col] = df[col].apply(_try_prepend, prefix=self.basepath)
self.images = df
def copy(self):
"""Create a copy of the Dataset."""
return copy.deepcopy(self)
def get(self, dict_, drop_invalid=True):
"""Retrieve files and/or metadata from the current Dataset.
Parameters
----------
dict_ : :obj:`dict`
Dictionary specifying images or metadata to collect.
Keys should be variables to be used as keys for results dictionary.
Values should be tuples with two values:
type (e.g., 'image' or 'metadata') and specific field corresponding
to column of type-specific DataFrame (e.g., 'z' or 'sample_sizes').
drop_invalid : :obj:`bool`, optional
Whether to automatically ignore any studies without the required data or not.
Default is False.
Returns
-------
results : :obj:`dict`
A dictionary of lists of requested data. Keys correspond to the keys in ``dict_``.
Examples
--------
>>> dset.get({'z_maps': ('image', 'z'), 'sample_sizes': ('metadata', 'sample_sizes')})
>>> dset.get({'coordinates': ('coordinates', None)})
"""
results = {}
results["id"] = self.ids
keep_idx = np.arange(len(self.ids), dtype=int)
for k, vals in dict_.items():
if vals[0] == "image":
temp = self.get_images(imtype=vals[1])
elif vals[0] == "metadata":
temp = self.get_metadata(field=vals[1])
elif vals[0] == "coordinates":
# Break DataFrame down into a list of study-specific DataFrames
temp = [self.coordinates.loc[self.coordinates["id"] == id_] for id_ in self.ids]
# Replace empty DataFrames with Nones
temp = [t if t.size else None for t in temp]
elif vals[0] == "annotations":
# Break DataFrame down into a list of study-specific DataFrames
temp = [self.annotations.loc[self.annotations["id"] == id_] for id_ in self.ids]
# Replace empty DataFrames with Nones
temp = [t if t.size else None for t in temp]
else:
raise ValueError(f"Input '{vals[0]}' not understood.")
results[k] = temp
temp_keep_idx = np.where([t is not None for t in temp])[0]
keep_idx = np.intersect1d(keep_idx, temp_keep_idx)
# reduce
if drop_invalid and (len(keep_idx) != len(self.ids)):
LGR.info(f"Retaining {len(keep_idx)}/{len(self.ids)} studies")
elif len(keep_idx) != len(self.ids):
raise Exception(
f"Only {len(keep_idx)}/{len(self.ids)} in Dataset contain the necessary data. "
"If you want to analyze the subset of studies with required data, "
"set `drop_invalid` to True."
)
for k in results:
results[k] = [results[k][i] for i in keep_idx]
if dict_.get(k, [None])[0] in ("coordinates", "annotations"):
results[k] = pd.concat(results[k])
return results
def _generic_column_getter(self, attr, ids=None, column=None, ignore_columns=None):
"""Extract information from DataFrame-based attributes.
Parameters
----------
attr : :obj:`str`
The name of the DataFrame-format Dataset attribute to search.
ids : :obj:`list` or None, optional
A list of study IDs within which to extract values.
If None, extract values for all studies in the Dataset.
Default is None.
column : :obj:`str` or None, optional
The column from which to extract values.
If None, a list of all columns with valid values will be returned.
Must be a column within Dataset.[attr].
ignore_columns : :obj:`list` or None, optional
A list of columns to ignore. Only used if ``column`` is None.
Returns
-------
result : :obj:`list` or :obj:`str`
A list of values or a string, depending on if ids is a list (or None) or a string.
"""
if ignore_columns is None:
ignore_columns = self._id_cols
else:
ignore_columns += self._id_cols
df = getattr(self, attr)
return_first = False
if isinstance(ids, str) and column is not None:
return_first = True
ids = _listify(ids)
available_types = [c for c in df.columns if c not in self._id_cols]
if (column is not None) and (column not in available_types):
raise ValueError(
f"{column} not found in {attr}.\nAvailable types: {', '.join(available_types)}"
)
if column is not None:
if ids is not None:
result = df[column].loc[df["id"].isin(ids)].tolist()
else:
result = df[column].tolist()
else:
if ids is not None:
result = {v: df[v].loc[df["id"].isin(ids)].tolist() for v in available_types}
result = {k: v for k, v in result.items() if any(v)}
else:
result = {v: df[v].tolist() for v in available_types}
result = list(result.keys())
if return_first:
return result[0]
else:
return result
def get_labels(self, ids=None):
"""Extract list of labels for which studies in Dataset have annotations.
Parameters
----------
ids : :obj:`list`, optional
A list of IDs in the Dataset for which to find labels. Default is
None, in which case all labels are returned.
Returns
-------
labels : :obj:`list`
List of labels for which there are annotations in the Dataset.
"""
if not isinstance(ids, list) and ids is not None:
ids = _listify(ids)
result = [c for c in self.annotations.columns if c not in self._id_cols]
if ids is not None:
temp_annotations = self.annotations.loc[self.annotations["id"].isin(ids)]
res = temp_annotations[result].any(axis=0)
result = res.loc[res].index.tolist()
return result
def get_texts(self, ids=None, text_type=None):
"""Extract list of texts of a given type for selected IDs.
Parameters
----------
ids : :obj:`list`, optional
A list of IDs in the Dataset for which to find texts. Default is
None, in which case all texts of requested type are returned.
text_type : :obj:`str`, optional
Type of text to extract. Corresponds to column name in
Dataset.texts DataFrame. Default is None.
Returns
-------
texts : :obj:`list`
List of texts of requested type for selected IDs.
"""
result = self._generic_column_getter("texts", ids=ids, column=text_type)
return result
def get_metadata(self, ids=None, field=None):
"""Get metadata from Dataset.
Parameters
----------
ids : :obj:`list`, optional
A list of IDs in the Dataset for which to find metadata. Default is
None, in which case all metadata of requested type are returned.
field : :obj:`str`, optional
Metadata field to extract. Corresponds to column name in
Dataset.metadata DataFrame. Default is None.
Returns
-------
metadata : :obj:`list`
List of values of requested type for selected IDs.
"""
result = self._generic_column_getter("metadata", ids=ids, column=field)
return result
def get_images(self, ids=None, imtype=None):
"""Get images of a certain type for a subset of studies in the dataset.
Parameters
----------
ids : :obj:`list`, optional
A list of IDs in the Dataset for which to find images. Default is
None, in which case all images of requested type are returned.
imtype : :obj:`str`, optional
Type of image to extract. Corresponds to column name in
Dataset.images DataFrame. Default is None.
Returns
-------
images : :obj:`list`
List of images of requested type for selected IDs.
"""
ignore_columns = ["space"]
ignore_columns += [c for c in self.images.columns if c.endswith("__relative")]
result = self._generic_column_getter(
"images",
ids=ids,
column=imtype,
ignore_columns=ignore_columns,
)
return result
def get_studies_by_label(self, labels=None, label_threshold=0.001):
"""Extract list of studies with a given label.
.. versionchanged:: 0.0.10
Fix bug in which all IDs were returned when a label wasn't present in the Dataset.
.. versionchanged:: 0.0.9
Default value for label_threshold changed to 0.001.
Parameters
----------
labels : :obj:`list`, optional
List of labels to use to search Dataset. If a contrast has all of
the labels above the threshold, it will be returned.
Default is None.
label_threshold : :obj:`float`, optional
Default is 0.5.
Returns
-------
found_ids : :obj:`list`
A list of IDs from the Dataset found by the search criteria.
"""
if isinstance(labels, str):
labels = [labels]
elif not isinstance(labels, list):
raise ValueError(f"Argument 'labels' cannot be {type(labels)}")
missing_labels = [label for label in labels if label not in self.annotations.columns]
if missing_labels:
raise ValueError(f"Missing label(s): {', '.join(missing_labels)}")
temp_annotations = self.annotations[self._id_cols + labels]
found_rows = (temp_annotations[labels] >= label_threshold).all(axis=1)
if any(found_rows):
found_ids = temp_annotations.loc[found_rows, "id"].tolist()
else:
found_ids = []
return found_ids
def get_studies_by_mask(self, mask):
"""Extract list of studies with at least one coordinate in mask.
Parameters
----------
mask : img_like
Mask across which to search for coordinates.
Returns
-------
found_ids : :obj:`list`
A list of IDs from the Dataset with at least one focus in the mask.
"""
from scipy.spatial.distance import cdist
mask = load_niimg(mask)
dset_mask = self.masker.mask_img
if not np.array_equal(dset_mask.affine, mask.affine):
LGR.warning("Mask affine does not match Dataset affine. Assuming same space.")
dset_ijk = mm2vox(self.coordinates[["x", "y", "z"]].values, mask.affine)
mask_ijk = np.vstack(np.where(mask.get_fdata())).T
distances = cdist(mask_ijk, dset_ijk)
distances = np.any(distances == 0, axis=0)
found_ids = list(self.coordinates.loc[distances, "id"].unique())
return found_ids
def get_studies_by_coordinate(self, xyz, r=20):
"""Extract list of studies with at least one focus within radius of requested coordinates.
Parameters
----------
xyz : (X x 3) array_like
List of coordinates against which to find studies.
r : :obj:`float`, optional
Radius (in mm) within which to find studies. Default is 20mm.
Returns
-------
found_ids : :obj:`list`
A list of IDs from the Dataset with at least one focus within
radius r of requested coordinates.
"""
from scipy.spatial.distance import cdist
xyz = np.array(xyz)
assert xyz.shape[1] == 3 and xyz.ndim == 2
distances = cdist(xyz, self.coordinates[["x", "y", "z"]].values)
distances = np.any(distances <= r, axis=0)
found_ids = list(self.coordinates.loc[distances, "id"].unique())
return found_ids