@njit()<EOL>def _cluster_hits(hits, clusters, assigned_hit_array, cluster_hit_indices, column_cluster_distance, row_cluster_distance, frame_cluster_distance, min_hit_charge, max_hit_charge, ignore_same_hits, noisy_pixels, disabled_pixels):

total_hits = hits.shape[<NUM_LIT:0>]<EOL>if total_hits == <NUM_LIT:0>:<EOL><INDENT>return <NUM_LIT:0>  <EOL><DEDENT>max_cluster_hits = cluster_hit_indices.shape[<NUM_LIT:0>]<EOL>if total_hits != clusters.shape[<NUM_LIT:0>]:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>if total_hits != assigned_hit_array.shape[<NUM_LIT:0>]:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>if min_hit_charge == <NUM_LIT:0>:<EOL><INDENT>charge_correction = <NUM_LIT:1><EOL><DEDENT>else:<EOL><INDENT>charge_correction = <NUM_LIT:0><EOL><DEDENT>start_event_hit_index = <NUM_LIT:0><EOL>start_event_cluster_index = <NUM_LIT:0><EOL>cluster_size = <NUM_LIT:0><EOL>event_number = hits[<NUM_LIT:0>]['<STR_LIT>']<EOL>event_cluster_index = <NUM_LIT:0><EOL>for i in range(total_hits):<EOL><INDENT>if _new_event(hits[i]['<STR_LIT>'], event_number):<EOL><INDENT>_finish_event(<EOL>hits=hits,<EOL>clusters=clusters,<EOL>start_event_hit_index=start_event_hit_index,<EOL>stop_event_hit_index=i,<EOL>start_event_cluster_index=start_event_cluster_index,<EOL>stop_event_cluster_index=start_event_cluster_index + event_cluster_index)<EOL>start_event_hit_index = i<EOL>start_event_cluster_index = start_event_cluster_index + event_cluster_index<EOL>event_number = hits[i]['<STR_LIT>']<EOL>event_cluster_index = <NUM_LIT:0><EOL><DEDENT>if assigned_hit_array[i] > <NUM_LIT:0>:  <EOL><INDENT>continue<EOL><DEDENT>if not _hit_ok(<EOL>hit=hits[i],<EOL>min_hit_charge=min_hit_charge,<EOL>max_hit_charge=max_hit_charge) or (disabled_pixels.shape[<NUM_LIT:0>] != <NUM_LIT:0> and _pixel_masked(hits[i], disabled_pixels)):<EOL><INDENT>_set_hit_invalid(hit=hits[i], cluster_id=-<NUM_LIT:1>)<EOL>assigned_hit_array[i] = <NUM_LIT:1><EOL>continue<EOL><DEDENT>_set_1d_array(cluster_hit_indices, -<NUM_LIT:1>, cluster_size)<EOL>cluster_hit_indices[<NUM_LIT:0>] = i<EOL>assigned_hit_array[i] = <NUM_LIT:1><EOL>cluster_size = <NUM_LIT:1>  <EOL>for j in cluster_hit_indices:  <EOL><INDENT>if j < <NUM_LIT:0>:  <EOL><INDENT>break<EOL><DEDENT>for k in range(cluster_hit_indices[<NUM_LIT:0>] + <NUM_LIT:1>, total_hits):<EOL><INDENT>if _new_event(hits[k]['<STR_LIT>'], event_number):<EOL><INDENT>break<EOL><DEDENT>if assigned_hit_array[k] > <NUM_LIT:0>:<EOL><INDENT>continue<EOL><DEDENT>if not _hit_ok(<EOL>hit=hits[k],<EOL>min_hit_charge=min_hit_charge,<EOL>max_hit_charge=max_hit_charge) or (disabled_pixels.shape[<NUM_LIT:0>] != <NUM_LIT:0> and _pixel_masked(hits[k], disabled_pixels)):<EOL><INDENT>_set_hit_invalid(hit=hits[k], cluster_id=-<NUM_LIT:1>)<EOL>assigned_hit_array[k] = <NUM_LIT:1><EOL>continue<EOL><DEDENT>if _is_in_max_difference(hits[j]['<STR_LIT>'], hits[k]['<STR_LIT>'], column_cluster_distance) and _is_in_max_difference(hits[j]['<STR_LIT>'], hits[k]['<STR_LIT>'], row_cluster_distance) and _is_in_max_difference(hits[j]['<STR_LIT>'], hits[k]['<STR_LIT>'], frame_cluster_distance):<EOL><INDENT>if not ignore_same_hits or hits[j]['<STR_LIT>'] != hits[k]['<STR_LIT>'] or hits[j]['<STR_LIT>'] != hits[k]['<STR_LIT>']:<EOL><INDENT>cluster_size += <NUM_LIT:1><EOL>if cluster_size > max_cluster_hits:<EOL><INDENT>raise IndexError('<STR_LIT>')<EOL><DEDENT>cluster_hit_indices[cluster_size - <NUM_LIT:1>] = k<EOL>assigned_hit_array[k] = <NUM_LIT:1><EOL><DEDENT>else:<EOL><INDENT>_set_hit_invalid(hit=hits[k], cluster_id=-<NUM_LIT:2>)<EOL>assigned_hit_array[k] = <NUM_LIT:1><EOL><DEDENT><DEDENT><DEDENT><DEDENT>if cluster_size == <NUM_LIT:1> and noisy_pixels.shape[<NUM_LIT:0>] != <NUM_LIT:0> and _pixel_masked(hits[cluster_hit_indices[<NUM_LIT:0>]], noisy_pixels):<EOL><INDENT>_set_hit_invalid(hit=hits[cluster_hit_indices[<NUM_LIT:0>]], cluster_id=-<NUM_LIT:1>)<EOL><DEDENT>else:<EOL><INDENT>_finish_cluster(<EOL>hits=hits,<EOL>clusters=clusters,<EOL>cluster_size=cluster_size,<EOL>cluster_hit_indices=cluster_hit_indices,<EOL>cluster_index=start_event_cluster_index + event_cluster_index,<EOL>cluster_id=event_cluster_index,<EOL>charge_correction=charge_correction,<EOL>noisy_pixels=noisy_pixels,<EOL>disabled_pixels=disabled_pixels)<EOL>event_cluster_index += <NUM_LIT:1><EOL><DEDENT><DEDENT>_finish_event(<EOL>hits=hits,<EOL>clusters=clusters,<EOL>start_event_hit_index=start_event_hit_index,<EOL>stop_event_hit_index=total_hits,<EOL>start_event_cluster_index=start_event_cluster_index,<EOL>stop_event_cluster_index=start_event_cluster_index + event_cluster_index)<EOL>total_clusters = start_event_cluster_index + event_cluster_index<EOL>return total_clusters<EOL>

Main precompiled function that loopes over the hits and clusters them


if not hit_fields:<EOL><INDENT>hit_fields_mapping_inverse = {}<EOL>hit_fields_mapping = {}<EOL><DEDENT>else:<EOL><INDENT>hit_fields_mapping_inverse = dict((k, v) for k, v in hit_fields.items())<EOL>hit_fields_mapping = dict((v, k) for k, v in hit_fields.items())<EOL><DEDENT>for old_name, new_name in self._default_hit_fields_mapping.items():<EOL><INDENT>if old_name not in hit_fields_mapping:<EOL><INDENT>hit_fields_mapping[old_name] = new_name<EOL>hit_fields_mapping_inverse[new_name] = old_name<EOL><DEDENT><DEDENT>self._hit_fields_mapping = hit_fields_mapping<EOL>self._hit_fields_mapping_inverse = hit_fields_mapping_inverse<EOL>

Tell the clusterizer the meaning of the field names.

        The hit_fields parameter is a dict, e.g., {"new field name": "standard field name"}.

        If None default mapping is set.

        Example:
        --------
        Internally, the clusterizer uses the hit fields names "column"/"row". If the name of the hits fields are "x"/"y", call:
        set_hit_fields(hit_fields={'x': 'column',
                                   'y': 'row'})


if not cluster_fields:<EOL><INDENT>cluster_fields_mapping_inverse = {}<EOL>cluster_fields_mapping = {}<EOL><DEDENT>else:<EOL><INDENT>cluster_fields_mapping_inverse = dict((k, v) for k, v in cluster_fields.items())<EOL>cluster_fields_mapping = dict((v, k) for k, v in cluster_fields.items())<EOL><DEDENT>for old_name, new_name in self._default_cluster_fields_mapping.items():<EOL><INDENT>if old_name not in cluster_fields_mapping:<EOL><INDENT>cluster_fields_mapping[old_name] = new_name<EOL>cluster_fields_mapping_inverse[new_name] = old_name<EOL><DEDENT><DEDENT>self._cluster_fields_mapping = cluster_fields_mapping<EOL>self._cluster_fields_mapping_inverse = cluster_fields_mapping_inverse<EOL>

Tell the clusterizer the meaning of the field names.

        The cluster_fields parameter is a dict, e.g., {"new filed name": "standard field name"}.


if not hit_dtype:<EOL><INDENT>hit_dtype = np.dtype([])<EOL><DEDENT>else:<EOL><INDENT>hit_dtype = np.dtype(hit_dtype)<EOL><DEDENT>cluster_hits_descr = hit_dtype.descr<EOL>for dtype_name, dtype in self._default_cluster_hits_descr:<EOL><INDENT>if self._hit_fields_mapping[dtype_name] not in hit_dtype.fields:<EOL><INDENT>cluster_hits_descr.append((dtype_name, dtype))<EOL><DEDENT><DEDENT>self._cluster_hits_descr = cluster_hits_descr<EOL>self._init_arrays(size=<NUM_LIT:0>)<EOL>

Set the data type of the hits.

        Fields that are not mentioned here are NOT copied into the clustered hits array.
        Clusterizer has to know the hit data type to produce the clustered hit result with the same data types.

        Parameters:
        -----------
        hit_dtype : numpy.dtype or equivalent
            Defines the dtype of the hit array.

        Example:
        --------
        hit_dtype = [("column", np.uint16), ("row", np.uint16)], where
        "column", "row" is the field name of the input hit array.


if not cluster_dtype:<EOL><INDENT>cluster_dtype = np.dtype([])<EOL><DEDENT>else:<EOL><INDENT>cluster_dtype = np.dtype(cluster_dtype)<EOL><DEDENT>cluster_descr = cluster_dtype.descr<EOL>for dtype_name, dtype in self._default_cluster_descr:<EOL><INDENT>if self._cluster_fields_mapping[dtype_name] not in cluster_dtype.fields:<EOL><INDENT>cluster_descr.append((dtype_name, dtype))<EOL><DEDENT><DEDENT>self._cluster_descr = cluster_descr<EOL>self._init_arrays(size=<NUM_LIT:0>)<EOL>

Set the data type of the cluster.

        Parameters:
        -----------
        cluster_dtype : numpy.dtype or equivalent
            Defines the dtype of the cluster array.


if isinstance(description, list):<EOL><INDENT>for item in description:<EOL><INDENT>if len(item) != <NUM_LIT:2>:<EOL><INDENT>raise TypeError("<STR_LIT>")<EOL><DEDENT>self._cluster_descr.append(item)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>if len(description) != <NUM_LIT:2>:<EOL><INDENT>raise TypeError("<STR_LIT>")<EOL><DEDENT>self._cluster_descr.append(description)<EOL><DEDENT>self._init_arrays(size=<NUM_LIT:0>)<EOL>

Adds a field or a list of fields to the cluster result array. Has to be defined as a numpy dtype entry, e.g.: ('parameter', '<i4')


self.cluster_functions._end_of_cluster_function = self._jitted(function)<EOL>self._end_of_cluster_function = function<EOL>

Adding function to module.
        This is maybe the only way to make the clusterizer to work with multiprocessing.


self.cluster_functions._end_of_event_function = self._jitted(function)<EOL>self._end_of_event_function = function<EOL>


self._min_hit_charge = value<EOL>

Charge values below this value will effectively ignore the hit.
        Value has influence on clustering charge weighting.


if value is None:<EOL><INDENT>value = <NUM_LIT:0><EOL><DEDENT>self._max_hit_charge = value<EOL>

Charge values above this value will effectively ignore the hit.
        Value of None or 0 will deactivate this feature.


self._column_cluster_distance = value<EOL>

Setting up max. column cluster distance.


self._row_cluster_distance = value<EOL>


self._frame_cluster_distance = value<EOL>


self._ignore_same_hits = value<EOL>

Whether a duplicate hit in the event with the same column and row is ignored or not.


def cluster_hits(self, hits, noisy_pixels=None, disabled_pixels=None):

<EOL>self.cluster_functions._end_of_cluster_function = self._jitted(self._end_of_cluster_function)<EOL>self.cluster_functions._end_of_event_function = self._jitted(self._end_of_event_function)<EOL>n_hits = hits.shape[<NUM_LIT:0>]  <EOL>if (n_hits < int(<NUM_LIT:0.5> * self._cluster_hits.size)) or (n_hits > self._cluster_hits.size):<EOL><INDENT>self._init_arrays(size=int(<NUM_LIT> * n_hits))  <EOL><DEDENT>else:<EOL><INDENT>self._assigned_hit_array.fill(<NUM_LIT:0>)  <EOL>self._cluster_hit_indices.fill(-<NUM_LIT:1>)  <EOL><DEDENT>self._clusters.dtype.names = self._unmap_cluster_field_names(self._clusters.dtype.names)  <EOL>self._cluster_hits.dtype.names = self._unmap_hit_field_names(self._cluster_hits.dtype.names)  <EOL>self._check_struct_compatibility(hits)<EOL>for field_name in hits.dtype.fields:<EOL><INDENT>if field_name in self._hit_fields_mapping_inverse:<EOL><INDENT>cluster_hits_field_name = self._hit_fields_mapping_inverse[field_name]<EOL><DEDENT>else:<EOL><INDENT>cluster_hits_field_name = field_name<EOL><DEDENT>if cluster_hits_field_name in self._cluster_hits.dtype.fields:<EOL><INDENT>self._cluster_hits[cluster_hits_field_name][:n_hits] = hits[field_name]<EOL><DEDENT><DEDENT>noisy_pixels_array = np.array([]) if noisy_pixels is None else np.array(noisy_pixels)<EOL>if noisy_pixels_array.shape[<NUM_LIT:0>] != <NUM_LIT:0>:<EOL><INDENT>noisy_pixels_max_range = np.array([max(<NUM_LIT:0>, np.max(noisy_pixels_array[:, <NUM_LIT:0>])), max(<NUM_LIT:0>, np.max(noisy_pixels_array[:, <NUM_LIT:1>]))])<EOL>noisy_pixels = np.zeros(noisy_pixels_max_range + <NUM_LIT:1>, dtype=np.bool)<EOL>noisy_pixels[noisy_pixels_array[:, <NUM_LIT:0>], noisy_pixels_array[:, <NUM_LIT:1>]] = <NUM_LIT:1><EOL><DEDENT>else:<EOL><INDENT>noisy_pixels = np.zeros((<NUM_LIT:0>, <NUM_LIT:0>), dtype=np.bool)<EOL><DEDENT>disabled_pixels_array = np.array([]) if disabled_pixels is None else np.array(disabled_pixels)<EOL>if disabled_pixels_array.shape[<NUM_LIT:0>] != <NUM_LIT:0>:<EOL><INDENT>disabled_pixels_max_range = np.array([np.max(disabled_pixels_array[:, <NUM_LIT:0>]), np.max(disabled_pixels_array[:, <NUM_LIT:1>])])<EOL>disabled_pixels = np.zeros(disabled_pixels_max_range + <NUM_LIT:1>, dtype=np.bool)<EOL>disabled_pixels[disabled_pixels_array[:, <NUM_LIT:0>], disabled_pixels_array[:, <NUM_LIT:1>]] = <NUM_LIT:1><EOL><DEDENT>else:<EOL><INDENT>disabled_pixels = np.zeros((<NUM_LIT:0>, <NUM_LIT:0>), dtype=np.bool)<EOL>

Cluster given hit array.

        The noisy_pixels and disabled_pixels parameters are iterables of column/row index pairs, e.g. [[column_1, row_1], [column_2, row_2], ...].
        The noisy_pixels parameter allows for removing clusters that consist of a single noisy pixels. Clusters with 2 or more noisy pixels are not removed.
        The disabled_pixels parameter allows for ignoring pixels.


for key, _ in self._cluster_hits_descr:<EOL><INDENT>if key in self._hit_fields_mapping_inverse:<EOL><INDENT>mapped_key = self._hit_fields_mapping_inverse[key]<EOL><DEDENT>else:<EOL><INDENT>mapped_key = key<EOL><DEDENT>if mapped_key in ['<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>']:<EOL><INDENT>continue<EOL><DEDENT>if key not in hits.dtype.names:<EOL><INDENT>raise TypeError('<STR_LIT>' % key)<EOL><DEDENT>if self._cluster_hits.dtype[mapped_key] != hits.dtype[key]:<EOL><INDENT>raise TypeError('<STR_LIT>' % (key, hits.dtype[key], self._cluster_hits.dtype[mapped_key]))<EOL><DEDENT><DEDENT>additional_hit_fields = set(hits.dtype.names) - set([key for key, val in self._cluster_hits_descr])<EOL>if additional_hit_fields:<EOL><INDENT>logging.warning('<STR_LIT>' % "<STR_LIT:U+002CU+0020>".join(additional_hit_fields))<EOL><DEDENT>

Takes the hit array and checks if the important data fields have the same data type than the hit clustered array and that the field names are correct.


self.dataset = get_test_dataset()<EOL>self.real_dataset = get_test_dataset(prefix='<STR_LIT>')<EOL>


maskfile = get_resource_path() + '<STR_LIT>'<EOL>self.masker = Masker(maskfile)<EOL>


return t<EOL>


t.value = float(t.value)<EOL>return t<EOL>


p[<NUM_LIT:0>] = p[<NUM_LIT:1>].loc[set(p[<NUM_LIT:1>].index) - set(p[<NUM_LIT:3>].index)]<EOL>


p[<NUM_LIT:0>] = pd.concat(<EOL>[p[<NUM_LIT:1>], p[<NUM_LIT:3>]], axis=<NUM_LIT:1>).dropna().apply(self.func, axis=<NUM_LIT:1>)<EOL>


p[<NUM_LIT:0>] = pd.concat(<EOL>[p[<NUM_LIT:1>], p[<NUM_LIT:3>]], axis=<NUM_LIT:1>).fillna(<NUM_LIT:0.0>).apply(self.func, axis=<NUM_LIT:1>)<EOL>


p[<NUM_LIT:0>] = p[<NUM_LIT:1>][p[<NUM_LIT:1>] < p[<NUM_LIT:3>]]<EOL>


p[<NUM_LIT:0>] = p[<NUM_LIT:1>][p[<NUM_LIT:1>] >= p[<NUM_LIT:3>]]<EOL>


p[<NUM_LIT:0>] = '<STR_LIT:U+0020>'.join([p[<NUM_LIT:1>], p[<NUM_LIT:2>]])<EOL>

feature : WORD WORD
                | feature WORD


p[<NUM_LIT:0>] = self.dataset.get_studies(<EOL>features=p[<NUM_LIT:1>], frequency_threshold=self.threshold, func=self.func,<EOL>return_type='<STR_LIT>')<EOL>


p[<NUM_LIT:0>] = p[<NUM_LIT:2>]<EOL>


p[<NUM_LIT:0>] = p[<NUM_LIT:1>]<EOL>


def get_sphere(coords, r=<NUM_LIT:4>, vox_dims=(<NUM_LIT:2>, <NUM_LIT:2>, <NUM_LIT:2>), dims=(<NUM_LIT>, <NUM_LIT>, <NUM_LIT>)):

r = float(r)<EOL>xx, yy, zz = [slice(-r / vox_dims[i], r / vox_dims[<EOL>i] + <NUM_LIT>, <NUM_LIT:1>) for i in range(len(coords))]<EOL>cube = np.vstack([row.ravel() for row in np.mgrid[xx, yy, zz]])<EOL>sphere = cube[:, np.sum(np.dot(np.diag(<EOL>vox_dims), cube) ** <NUM_LIT:2>, <NUM_LIT:0>) ** <NUM_LIT> <= r]<EOL>sphere = np.round(sphere.T + coords)<EOL>return sphere[(np.min(sphere, <NUM_LIT:1>) >= <NUM_LIT:0>) &<EOL>(np.max(np.subtract(sphere, dims), <NUM_LIT:1>) <= -<NUM_LIT:1>), :].astype(int)<EOL>

Return all points within r mm of coordinates. Generates a cube
   and then discards all points outside sphere. Only returns values that
   fall within the dimensions of the image.


def map_peaks_to_image(peaks, r=<NUM_LIT:4>, vox_dims=(<NUM_LIT:2>, <NUM_LIT:2>, <NUM_LIT:2>), dims=(<NUM_LIT>, <NUM_LIT>, <NUM_LIT>),<EOL>header=None):

data = np.zeros(dims)<EOL>for p in peaks:<EOL><INDENT>valid = get_sphere(p, r, vox_dims, dims)<EOL>valid = valid[:, ::-<NUM_LIT:1>]<EOL>data[tuple(valid.T)] = <NUM_LIT:1><EOL><DEDENT>return nifti1.Nifti1Image(data, None, header=header)<EOL>

Take a set of discrete foci (i.e., 2-D array of xyz coordinates)
    and generate a corresponding image, convolving each focus with a
    hard sphere of radius r.


def load_imgs(filenames, masker, nan_to_num=True):

if isinstance(filenames, string_types):<EOL><INDENT>filenames = [filenames]<EOL><DEDENT>data = np.zeros((masker.n_vox_in_mask, len(filenames)))<EOL>for i, f in enumerate(filenames):<EOL><INDENT>data[:, i] = masker.mask(f, nan_to_num)<EOL><DEDENT>return data<EOL>

Load multiple images from file into an ndarray.

    Args:
      filenames: A single filename or list of filenames pointing to valid
        images.
      masker: A Masker instance.
      nan_to_num: Optional boolean indicating whether to convert NaNs to zero.

    Returns:
      An m x n 2D numpy array, where m = number of voxels in mask and
      n = number of images passed.


def save_img(data, filename, masker, header=None):

if not header:<EOL><INDENT>header = masker.get_header()<EOL><DEDENT>header.set_data_dtype(data.dtype)  <EOL>header['<STR_LIT>'] = data.max()<EOL>header['<STR_LIT>'] = data.min()<EOL>img = nifti1.Nifti1Image(masker.unmask(data), None, header)<EOL>img.to_filename(filename)<EOL>


def threshold_img(data, threshold, mask=None, mask_out='<STR_LIT>'):

if mask is not None:<EOL><INDENT>mask = threshold_img(mask, threshold, mask_out=mask_out)<EOL>return data * mask.astype(bool)<EOL><DEDENT>if mask_out.startswith('<STR_LIT:b>'):<EOL><INDENT>data[data < threshold] = <NUM_LIT:0><EOL><DEDENT>elif mask_out.startswith('<STR_LIT:a>'):<EOL><INDENT>data[data > threshold] = <NUM_LIT:0><EOL><DEDENT>return data<EOL>

Threshold data, setting all values in the array above/below threshold
    to zero.
    Args:
        data (ndarray): The image data to threshold.
        threshold (float): Numeric threshold to apply to image.
        mask (ndarray): Optional 1D-array with the same length as the data. If
            passed, the threshold is first applied to the mask, and the
            resulting indices are used to threshold the data. This is primarily
            useful when, e.g., applying a statistical threshold to a z-value
            image based on a p-value threshold.
        mask_out (str): Thresholding direction. Can be 'below' the threshold
            (default) or 'above' the threshold. Note: use 'above' when masking
            based on p values.


def create_grid(image, scale=<NUM_LIT:4>, apply_mask=True, save_file=None):

if isinstance(image, string_types):<EOL><INDENT>image = nb.load(image)<EOL><DEDENT>centers = []<EOL>x_length, y_length, z_length = image.shape<EOL>for x in range(<NUM_LIT:0>, x_length, scale):<EOL><INDENT>for y in range(<NUM_LIT:0>, y_length, scale):<EOL><INDENT>for z in range(<NUM_LIT:0>, z_length, scale):<EOL><INDENT>centers.append((x, y, z))<EOL><DEDENT><DEDENT><DEDENT>grid = np.zeros(image.shape)<EOL>for (i, (x, y, z)) in enumerate(centers):<EOL><INDENT>for mov_x in range((-scale + <NUM_LIT:1>) // <NUM_LIT:2>, (scale + <NUM_LIT:1>) // <NUM_LIT:2>):<EOL><INDENT>for mov_y in range((-scale + <NUM_LIT:1>) // <NUM_LIT:2>, (scale + <NUM_LIT:1>) // <NUM_LIT:2>):<EOL><INDENT>for mov_z in range((-scale + <NUM_LIT:1>) // <NUM_LIT:2>, (scale + <NUM_LIT:1>) // <NUM_LIT:2>):<EOL><INDENT>try:  <EOL><INDENT>grid[x + mov_x, y + mov_y, z + mov_z] = i + <NUM_LIT:1><EOL><DEDENT>except:<EOL><INDENT>pass<EOL><DEDENT><DEDENT><DEDENT><DEDENT><DEDENT>if apply_mask:<EOL><INDENT>mask = image<EOL>if isinstance(mask, string_types):<EOL><INDENT>mask = nb.load(mask)<EOL><DEDENT>if type(mask).__module__ != np.__name__:<EOL><INDENT>mask = mask.get_data()<EOL><DEDENT>grid[~mask.astype(bool)] = <NUM_LIT:0.0><EOL><DEDENT>grid = nb.Nifti1Image(grid, image.get_affine(), image.get_header())<EOL>if save_file is not None:<EOL><INDENT>nb.save(grid, save_file)<EOL><DEDENT>return grid<EOL>

Creates an image containing labeled cells in a 3D grid.
    Args:
        image: String or nibabel image. The image used to define the grid
            dimensions. Also used to define the mask to apply to the grid.
            Only voxels with non-zero values in the mask will be retained; all
            other voxels will be zeroed out in the returned image.
        scale: The scaling factor which controls the grid size. Value reflects
            diameter of cube in voxels.
        apply_mask: Boolean indicating whether or not to zero out voxels not in
            image.
        save_file: Optional string giving the path to save image to. Image
            written out is a standard Nifti image. If save_file is None, no
            image is written.
    Returns:
        A nibabel image with the same dimensions as the input image. All voxels
        in each cell in the 3D grid are assigned the same non-zero label.


def __init__(self, volume, layers=None):

if isinstance(volume, string_types):<EOL><INDENT>volume = nb.load(volume)<EOL><DEDENT>self.volume = volume<EOL>data = self.volume.get_data()<EOL>self.dims = data.shape<EOL>self.vox_dims = self.get_header().get_zooms()<EOL>self.full = np.float64(data.ravel())<EOL>self.global_mask = np.where(self.full)<EOL>self.reset()<EOL>if layers is not None:<EOL><INDENT>self.add(layers)<EOL><DEDENT>

Initialize a new Masker.
        Args:
            volume: A volume indicating the global space within which all
                subsequent layers must reside. Any voxel in the mask with a
                non-zero valid is considered valid for analyses. Can be either
                an image filename or a NiBabel image.
            layers: Optional masking layers to add; see docstring for add().


self.layers = {}<EOL>self.stack = []<EOL>self.set_mask()<EOL>self.n_vox_in_vol = len(np.where(self.current_mask)[<NUM_LIT:0>])<EOL>

Reset/remove all layers, keeping only the initial volume.


def add(self, layers, above=None, below=None):

def add_named_layer(name, image):<EOL><INDENT>image = self.get_image(image, output='<STR_LIT>')<EOL>if above is not None:<EOL><INDENT>image[image < above] = <NUM_LIT:0.><EOL><DEDENT>if below is not None:<EOL><INDENT>image[image > below] = <NUM_LIT:0.><EOL><DEDENT>self.layers[name] = image<EOL>self.stack.append(name)<EOL><DEDENT>if isinstance(layers, dict):<EOL><INDENT>for (name, image) in layers.items():<EOL><INDENT>add_named_layer(name, image)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>if not isinstance(layers, list):<EOL><INDENT>layers = [layers]<EOL><DEDENT>for image in layers:<EOL><INDENT>name = '<STR_LIT>' % len(self.stack)<EOL>add_named_layer(name, image)<EOL><DEDENT><DEDENT>self.set_mask()<EOL>

Add one or more layers to the stack of masking layers.
        Args:
            layers: A string, NiBabel image, list, or dict. If anything other
                than a dict is passed, assigns sequential layer names based on
                the current position in stack; if a dict, uses key as the name
                and value as the mask image.


if not isinstance(layers, list):<EOL><INDENT>layers = [layers]<EOL><DEDENT>for l in layers:<EOL><INDENT>if isinstance(l, string_types):<EOL><INDENT>if l not in self.layers:<EOL><INDENT>raise ValueError("<STR_LIT>"<EOL>"<STR_LIT>" % l)<EOL><DEDENT>self.stack.remove(l)<EOL><DEDENT>else:<EOL><INDENT>l = self.stack.pop(l)<EOL><DEDENT>del self.layers[l]<EOL><DEDENT>self.set_mask()<EOL>

Remove one or more layers from the stack of masking layers.
        Args:
            layers: An int, string or list of strings and/or ints. Ints are
                interpreted as indices in the stack to remove; strings are
                interpreted as names of layers to remove. Negative ints will
                also work--i.e., remove(-1) will drop the last layer added.


def get_image(self, image, output='<STR_LIT>'):

if isinstance(image, string_types):<EOL><INDENT>image = nb.load(image)<EOL><DEDENT>if type(image).__module__.startswith('<STR_LIT>'):<EOL><INDENT>if output == '<STR_LIT:image>':<EOL><INDENT>return image<EOL><DEDENT>image = image.get_data()<EOL><DEDENT>if not type(image).__module__.startswith('<STR_LIT>'):<EOL><INDENT>raise ValueError("<STR_LIT>"<EOL>"<STR_LIT>")<EOL><DEDENT>if image.shape[:<NUM_LIT:3>] == self.volume.shape:<EOL><INDENT>if output == '<STR_LIT:image>':<EOL><INDENT>return nb.nifti1.Nifti1Image(image, None, self.get_header())<EOL><DEDENT>elif output == '<STR_LIT>':<EOL><INDENT>return image<EOL><DEDENT>else:<EOL><INDENT>image = image.ravel()<EOL><DEDENT><DEDENT>if output == '<STR_LIT>':<EOL><INDENT>return image.ravel()<EOL><DEDENT>image = np.reshape(image, self.volume.shape)<EOL>if output == '<STR_LIT>':<EOL><INDENT>return image<EOL><DEDENT>return nb.nifti1.Nifti1Image(image, None, self.get_header())<EOL>

A flexible method for transforming between different
        representations of image data.
        Args:
            image: The input image. Can be a string (filename of image),
                NiBabel image, N-dimensional array (must have same shape as
                self.volume), or vectorized image data (must have same length
                as current conjunction mask).
            output: The format of the returned image representation. Must be
                one of:
                    'vector': A 1D vectorized array
                    'array': An N-dimensional array, with
                        shape = self.volume.shape
                    'image': A NiBabel image
        Returns: An object containing image data; see output options above.


def mask(self, image, nan_to_num=True, layers=None, in_global_mask=False):

self.set_mask(layers)<EOL>image = self.get_image(image, output='<STR_LIT>')<EOL>if in_global_mask:<EOL><INDENT>masked_data = image[self.global_mask]<EOL>masked_data[~self.get_mask(in_global_mask=True)] = <NUM_LIT:0><EOL><DEDENT>else:<EOL><INDENT>masked_data = image[self.current_mask]<EOL><DEDENT>if nan_to_num:<EOL><INDENT>masked_data = np.nan_to_num(masked_data)<EOL><DEDENT>return masked_data<EOL>

Vectorize an image and mask out all invalid voxels.

        Args:
            images: The image to vectorize and mask. Input can be any object
                handled by get_image().
            layers: Which mask layers to use (specified as int, string, or
                list of ints and strings). When None, applies the conjunction
                of all layers.
            nan_to_num: boolean indicating whether to convert NaNs to 0.
            in_global_mask: Whether to return the resulting masked vector in
                the globally masked space (i.e., n_voxels =
                len(self.global_mask)). If False (default), returns in the full
                image space (i.e., n_voxels = len(self.volume)).
        Returns:
          A 1D NumPy array of in-mask voxels.


def unmask(self, data, layers=None, output='<STR_LIT>'):

self.set_mask(layers)<EOL>if data.ndim == <NUM_LIT:2>:<EOL><INDENT>n_volumes = data.shape[<NUM_LIT:1>]<EOL>image = np.zeros(self.full.shape + (n_volumes,))<EOL>image[self.current_mask, :] = data<EOL>image = np.reshape(image, self.volume.shape + (n_volumes,))<EOL><DEDENT>else:<EOL><INDENT>image = np.zeros(self.full.shape)<EOL>image[self.current_mask] = data<EOL><DEDENT>return self.get_image(image, output)<EOL>

Reconstruct a masked vector into the original 3D volume.
        Args:
            data: The 1D vector to reconstruct. (Can also be a 2D vector where
                the second dimension is time, but then output will always
                be set to 'array'--i.e., a 4D image will be returned.)
            layers: Which mask layers to use (specified as int, string, or list
                of ints and strings). When None, applies the conjunction of all
                layers. Note that the layers specified here must exactly match
                the layers used in the mask() operation, otherwise the shape of
                the mask will be incorrect and bad things will happen.
            output: What kind of object to return. See options in get_image().
                By default, returns an N-dimensional array of reshaped data.


def get_mask(self, layers=None, output='<STR_LIT>', in_global_mask=True):

if in_global_mask:<EOL><INDENT>output = '<STR_LIT>'<EOL><DEDENT>if layers is None:<EOL><INDENT>layers = self.layers.keys()<EOL><DEDENT>elif not isinstance(layers, list):<EOL><INDENT>layers = [layers]<EOL><DEDENT>layers = map(lambda x: x if isinstance(x, string_types)<EOL>else self.stack[x], layers)<EOL>layers = [self.layers[l] for l in layers if l in self.layers]<EOL>layers.append(self.full)<EOL>layers = np.vstack(layers).T.astype(bool)<EOL>mask = layers.all(axis=<NUM_LIT:1>)<EOL>mask = self.get_image(mask, output)<EOL>return mask[self.global_mask] if in_global_mask else mask<EOL>

Set the current mask by taking the conjunction of all specified
        layers.

        Args:
            layers: Which layers to include. See documentation for add() for
                format.
            include_global_mask: Whether or not to automatically include the
                global mask (i.e., self.volume) in the conjunction.


return self.volume.get_header()<EOL>


def download(path='<STR_LIT:.>', url=None, unpack=False):

if url is None:<EOL><INDENT>url = '<STR_LIT>'<EOL><DEDENT>if os.path.exists(path) and os.path.isdir(path):<EOL><INDENT>basename = os.path.basename(url).split('<STR_LIT:?>')[<NUM_LIT:0>]<EOL>filename = os.path.join(path, basename)<EOL><DEDENT>else:<EOL><INDENT>filename = path<EOL><DEDENT>f = open(filename, '<STR_LIT:wb>')<EOL>u = urlopen(url)<EOL>file_size = int(u.headers["<STR_LIT>"][<NUM_LIT:0>])<EOL>print("<STR_LIT>".format(<EOL>url, file_size))<EOL>bytes_dl = <NUM_LIT:0><EOL>block_size = <NUM_LIT><EOL>while True:<EOL><INDENT>buffer = u.read(block_size)<EOL>if not buffer:<EOL><INDENT>break<EOL><DEDENT>bytes_dl += len(buffer)<EOL>f.write(buffer)<EOL>p = float(bytes_dl) / file_size<EOL>status = r"<STR_LIT>".format(bytes_dl, p)<EOL>status = status + chr(<NUM_LIT:8>) * (len(status) + <NUM_LIT:1>)<EOL>sys.stdout.write(status)<EOL><DEDENT>f.close()<EOL>if unpack:<EOL><INDENT>import tarfile<EOL>tarfile.open(filename, '<STR_LIT>').extractall(os.path.dirname(filename))<EOL><DEDENT>

Download the latest data files.
    Args:
        path (str): Location to save the retrieved data files. Defaults to
            current directory.
        unpack (bool): If True, unzips the data file post-download.


def download_abstracts(dataset, path='<STR_LIT:.>', email=None, out_file=None):

try:<EOL><INDENT>from Bio import Entrez, Medline<EOL><DEDENT>except:<EOL><INDENT>raise Exception(<EOL>'<STR_LIT>')<EOL><DEDENT>if email is None:<EOL><INDENT>raise Exception('<STR_LIT>')<EOL><DEDENT>Entrez.email = email<EOL>if isinstance(dataset, Dataset):<EOL><INDENT>pmids = dataset.image_table.ids.astype(str).tolist()<EOL><DEDENT>elif isinstance(dataset, list):<EOL><INDENT>pmids = [str(pmid) for pmid in dataset]<EOL><DEDENT>else:<EOL><INDENT>raise Exception(<EOL>'<STR_LIT>'.format(type(dataset)))<EOL><DEDENT>records = []<EOL>chunks = [pmids[x: x + <NUM_LIT>] for x in range(<NUM_LIT:0>, len(pmids), <NUM_LIT>)]<EOL>for chunk in chunks:<EOL><INDENT>h = Entrez.efetch(db='<STR_LIT>', id=chunk, rettype='<STR_LIT>',<EOL>retmode='<STR_LIT:text>')<EOL>records += list(Medline.parse(h))<EOL><DEDENT>data = [[study['<STR_LIT>'], study['<STR_LIT>']]<EOL>for study in records if study.get('<STR_LIT>', None)]<EOL>df = pd.DataFrame(columns=['<STR_LIT>', '<STR_LIT>'], data=data)<EOL>if out_file is not None:<EOL><INDENT>df.to_csv(os.path.join(os.path.abspath(path), out_file), index=False)<EOL><DEDENT>return df<EOL>

Download the abstracts for a dataset/list of pmids


logger.info("<STR_LIT>" % filename)<EOL>activations = pd.read_csv(filename, sep='<STR_LIT:\t>')<EOL>activations.columns = [col.lower()<EOL>for col in list(activations.columns)]<EOL>mc = ['<STR_LIT:x>', '<STR_LIT:y>', '<STR_LIT:z>', '<STR_LIT:id>', '<STR_LIT>']<EOL>if (set(mc) - set(list(activations.columns))):<EOL><INDENT>logger.error(<EOL>"<STR_LIT>"<EOL>"<STR_LIT>")<EOL>return<EOL><DEDENT>spaces = activations['<STR_LIT>'].unique()<EOL>xyz = activations[['<STR_LIT:x>', '<STR_LIT:y>', '<STR_LIT:z>']].values<EOL>for s in spaces:<EOL><INDENT>if s != self.transformer.target:<EOL><INDENT>inds = activations['<STR_LIT>'] == s<EOL>xyz[inds] = self.transformer.apply(s, xyz[inds])<EOL><DEDENT><DEDENT>activations[['<STR_LIT:x>', '<STR_LIT:y>', '<STR_LIT:z>']] = xyz<EOL>ijk = pd.DataFrame(<EOL>transformations.xyz_to_mat(xyz), columns=['<STR_LIT:i>', '<STR_LIT>', '<STR_LIT:k>'])<EOL>activations = pd.concat([activations, ijk], axis=<NUM_LIT:1>)<EOL>return activations<EOL>

Load activation data from a text file.

        Args:
            filename (str): a string pointing to the location of the txt file
                to read from.


logger.info("<STR_LIT>")<EOL>if r is not None:<EOL><INDENT>self.r = r<EOL><DEDENT>self.image_table = ImageTable(self)<EOL>

Create and store a new ImageTable instance based on the current
        Dataset. Will generally be called privately, but may be useful as a
        convenience method in cases where the user wants to re-generate the
        table with a new smoothing kernel of different radius.

        Args:
            r (int): An optional integer indicating the radius of the smoothing
                kernel. By default, this is None, which will keep whatever
                value is currently set in the Dataset instance.


def get_studies(self, features=None, expression=None, mask=None,<EOL>peaks=None, frequency_threshold=<NUM_LIT>,<EOL>activation_threshold=<NUM_LIT:0.0>, func=np.sum, return_type='<STR_LIT>',<EOL>r=<NUM_LIT:6><EOL>):

results = []<EOL>if features is not None:<EOL><INDENT>if return_type == '<STR_LIT>':<EOL><INDENT>if expression is not None or mask is not None orpeaks is not None:<EOL><INDENT>raise ValueError(<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>")<EOL><DEDENT>return self.feature_table.get_ids(<EOL>features, frequency_threshold, func, get_weights=True)<EOL><DEDENT>else:<EOL><INDENT>results.append(self.feature_table.get_ids(<EOL>features, frequency_threshold, func))<EOL><DEDENT><DEDENT>if expression is not None:<EOL><INDENT>_ids = self.feature_table.get_ids_by_expression(<EOL>expression, frequency_threshold, func)<EOL>results.append(list(_ids))<EOL><DEDENT>if mask is not None:<EOL><INDENT>mask = self.masker.mask(mask, in_global_mask=True).astype(bool)<EOL>num_vox = np.sum(mask)<EOL>prop_mask_active = self.image_table.data.T.dot(mask).astype(float)<EOL>if isinstance(activation_threshold, float):<EOL><INDENT>prop_mask_active /= num_vox<EOL><DEDENT>indices = np.where(prop_mask_active > activation_threshold)[<NUM_LIT:0>]<EOL>results.append([self.image_table.ids[ind] for ind in indices])<EOL><DEDENT>if peaks is not None:<EOL><INDENT>r = float(r)<EOL>found = set()<EOL>for p in peaks:<EOL><INDENT>xyz = np.array(p, dtype=float)<EOL>x = self.activations['<STR_LIT:x>']<EOL>y = self.activations['<STR_LIT:y>']<EOL>z = self.activations['<STR_LIT:z>']<EOL>dists = np.sqrt(np.square(x - xyz[<NUM_LIT:0>]) + np.square(y - xyz[<NUM_LIT:1>]) +<EOL>np.square(z - xyz[<NUM_LIT:2>]))<EOL>inds = np.where((dists > <NUM_LIT>) & (dists < <NUM_LIT>))[<NUM_LIT:0>]<EOL>tmp = dists[inds]<EOL>found |= set(self.activations[dists <= r]['<STR_LIT:id>'].unique())<EOL><DEDENT>results.append(found)<EOL><DEDENT>ids = list(reduce(lambda x, y: set(x) & set(y), results))<EOL>if return_type == '<STR_LIT>':<EOL><INDENT>return ids<EOL><DEDENT>elif return_type == '<STR_LIT:data>':<EOL><INDENT>return self.get_image_data(ids)<EOL><DEDENT>

Get IDs or data for studies that meet specific criteria.

        If multiple criteria are passed, the set intersection is returned. For
        example, passing expression='emotion' and mask='my_mask.nii.gz' would
        return only those studies that are associated with emotion AND report
        activation within the voxels indicated in the passed image.

        Args:
            ids (list): A list of IDs of studies to retrieve.
            features (list or str): The name of a feature, or a list of
                features, to use for selecting studies.
            expression (str): A string expression to pass to the PEG for study
                retrieval.
            mask: the mask image (see Masker documentation for valid data
                types).
            peaks (ndarray or list): Either an n x 3 numpy array, or a list of
                lists or tuples (e.g., [(-10, 22, 14)]) specifying the world
                (x/y/z) coordinates of the target location(s).
            frequency_threshold (float): For feature-based or expression-based
                selection, the threshold for selecting studies--i.e., the
                cut-off for a study to be included. Must be a float in range
                [0, 1].
            activation_threshold (int or float): For mask-based selection,
                threshold for a study to be included based on amount of
                activation displayed. If an integer, represents the absolute
                number of voxels that must be active within the mask in order
                for a study to be selected. If a float, it represents the
                proportion of voxels that must be active.
            func (Callable): The function to use when aggregating over the list
                of features. See documentation in FeatureTable.get_ids() for a
                full explanation. Only used for feature- or expression-based
                selection.
            return_type (str): A string specifying what data to return. Valid
                options are:
                'ids': returns a list of IDs of selected studies.
                'images': returns a voxel x study matrix of data for all
                selected studies.
                'weights': returns a dict where the keys are study IDs and the
                values are the computed weights. Only valid when performing
                feature-based selection.
            r (int): For peak-based selection, the distance cut-off (in mm)
                for inclusion (i.e., only studies with one or more activations
                within r mm of one of the passed foci will be returned).

        Returns:
            When return_type is 'ids' (default), returns a list of IDs of the
            selected studies. When return_type is 'data', returns a 2D numpy
            array, with voxels in rows and studies in columns. When return_type
            is 'weights' (valid only for expression-based selection), returns
            a dict, where the keys are study IDs, and the values are the
            computed weights.

        Examples
        --------
        Select all studies tagged with the feature 'emotion':

            >>> ids = dataset.get_studies(features='emotion')

        Select all studies that activate at least 20% of voxels in an amygdala
        mask, and retrieve activation data rather than IDs:

            >>> data = dataset.get_studies(mask='amygdala_mask.nii.gz',
                threshold=0.2, return_type='images')

        Select studies that report at least one activation within 12 mm of at
        least one of three specific foci:

            >>> ids = dataset.get_studies(peaks=[[12, -20, 30], [-26, 22, 22],
                                                [0, 36, -20]], r=12)


def add_features(self, features, append=True, merge='<STR_LIT>',<EOL>duplicates='<STR_LIT:ignore>', min_studies=<NUM_LIT:0.0>, threshold=<NUM_LIT>):

if (not append) or not hasattr(self, '<STR_LIT>'):<EOL><INDENT>self.feature_table = FeatureTable(self)<EOL><DEDENT>self.feature_table.add_features(features, merge=merge,<EOL>duplicates=duplicates,<EOL>min_studies=min_studies,<EOL>threshold=threshold)<EOL>

Construct a new FeatureTable from file.

        Args:
            features: Feature data to add. Can be:
                (a) A text file containing the feature data, where each row is
                a study in the database, with features in columns. The first
                column must contain the IDs of the studies to match up with the
                image data.
                (b) A pandas DataFrame, where studies are in rows, features are
                in columns, and the index provides the study IDs.
            append (bool): If True, adds new features to existing ones
                incrementally. If False, replaces old features.
            merge, duplicates, min_studies, threshold: Additional arguments
                passed to FeatureTable.add_features().


def get_image_data(self, ids=None, voxels=None, dense=True):

return self.image_table.get_image_data(ids, voxels=voxels, dense=dense)<EOL>

A convenience wrapper for ImageTable.get_image_data().

        Args:
            ids (list, array): A list or 1D numpy array of study ids to
                return. If None, returns data for all studies.
            voxels (list, array): A list or 1D numpy array of voxel indices
                (i.e., rows) to return. If None, returns data for all voxels.


def get_feature_data(self, ids=None, **kwargs):

return self.feature_table.get_feature_data(ids, **kwargs)<EOL>

A convenience wrapper for FeatureTable.get_image_data().


if features:<EOL><INDENT>return self.feature_table.get_ordered_names(features)<EOL><DEDENT>else:<EOL><INDENT>return self.feature_table.feature_names<EOL><DEDENT>

Returns names of features. If features is None, returns all
        features. Otherwise assumes the user is trying to find the order of the
        features.


counts = np.sum(self.get_feature_data() >= threshold, <NUM_LIT:0>)<EOL>return dict(zip(self.get_feature_names(), list(counts)))<EOL>

Returns a dictionary, where the keys are the feature names
        and the values are the number of studies tagged with the feature.


try:<EOL><INDENT>dataset = pickle.load(open(filename, '<STR_LIT:rb>'))<EOL><DEDENT>except UnicodeDecodeError:<EOL><INDENT>dataset = pickle.load(open(filename, '<STR_LIT:rb>'), encoding='<STR_LIT>')<EOL><DEDENT>if hasattr(dataset, '<STR_LIT>'):<EOL><INDENT>dataset.feature_table._csr_to_sdf()<EOL><DEDENT>return dataset<EOL>

Load a pickled Dataset instance from file.


if hasattr(self, '<STR_LIT>'):<EOL><INDENT>self.feature_table._sdf_to_csr()<EOL><DEDENT>pickle.dump(self, open(filename, '<STR_LIT:wb>'), -<NUM_LIT:1>)<EOL>if hasattr(self, '<STR_LIT>'):<EOL><INDENT>self.feature_table._csr_to_sdf()<EOL><DEDENT>

Pickle the Dataset instance to the provided file.


if dense and ids is None and voxels is None:<EOL><INDENT>logger.warning(<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>")<EOL><DEDENT>result = self.data<EOL>if ids is not None:<EOL><INDENT>idxs = np.where(np.in1d(np.array(self.ids), np.array(ids)))[<NUM_LIT:0>]<EOL>result = result[:, idxs]<EOL><DEDENT>if voxels is not None:<EOL><INDENT>result = result[voxels, :]<EOL><DEDENT>return result.toarray() if dense else result<EOL>

Slices and returns a subset of image data.

        Args:
            ids (list, array): A list or 1D numpy array of study ids to
                return. If None, returns data for all studies.
            voxels (list, array): A list or 1D numpy array of voxel indices
                (i.e., rows) to return. If None, returns data for all voxels.
            dense (bool): Optional boolean. When True (default), convert the
                result to a dense array before returning. When False, keep as
                sparse matrix.

        Returns:
          A 2D numpy array with voxels in rows and studies in columns.


self.data = self.get_image_data(ids, dense=False)  <EOL>idxs = np.where(np.in1d(np.array(self.ids), np.array(ids)))[<NUM_LIT:0>]<EOL>self.ids = [self.ids[i] for i in idxs]<EOL>

Trim ImageTable to keep only the passed studies. This is a
        convenience method, and should generally be avoided in favor of
        non-destructive alternatives that don't require slicing (e.g.,
            matrix multiplication).


def save_images_to_file(self, ids, outroot='<STR_LIT>'):

pass<EOL>

Reconstructs vectorized images corresponding to the specified
        study ids and saves them to file, prepending with the outroot
        (default: current directory).


self.dataset = dataset<EOL>self.data = pd.DataFrame()<EOL>

Initialize a new FeatureTable. Takes as input a parent DataSet
        instance and feature data (if provided).


def add_features(self, features, merge='<STR_LIT>', duplicates='<STR_LIT:ignore>',<EOL>min_studies=<NUM_LIT:0>, threshold=<NUM_LIT>):

if isinstance(features, string_types):<EOL><INDENT>if not os.path.exists(features):<EOL><INDENT>raise ValueError("<STR_LIT>" % features)<EOL><DEDENT>try:<EOL><INDENT>features = pd.read_csv(features, sep='<STR_LIT:\t>', index_col=<NUM_LIT:0>)<EOL><DEDENT>except Exception as e:<EOL><INDENT>logger.error("<STR_LIT>" % (features, e))<EOL><DEDENT><DEDENT>if min_studies:<EOL><INDENT>valid = np.where(<EOL>(features.values >= threshold).sum(<NUM_LIT:0>) >= min_studies)[<NUM_LIT:0>]<EOL>features = features.iloc[:, valid]<EOL><DEDENT>n_studies = len(features)<EOL>n_common_ids = len(<EOL>set(features.index) & set(self.dataset.image_table.ids))<EOL>if float(n_common_ids) / n_studies < <NUM_LIT>:  <EOL><INDENT>msg = "<STR_LIT>" % n_common_ids if n_common_ids else "<STR_LIT>"<EOL>logger.warning(<EOL>msg + "<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>")<EOL><DEDENT>old_data = self.data.to_dense()<EOL>common_features = list(set(old_data.columns) & set(features.columns))<EOL>if duplicates == '<STR_LIT:ignore>':<EOL><INDENT>features = features.drop(common_features, axis=<NUM_LIT:1>)<EOL><DEDENT>elif duplicates == '<STR_LIT:replace>':<EOL><INDENT>old_data = old_data.drop(common_features, axis=<NUM_LIT:1>)<EOL><DEDENT>data = old_data.merge(<EOL>features, how=merge, left_index=True, right_index=True)<EOL>self.data = data.fillna(<NUM_LIT:0.0>).to_sparse()<EOL>

Add new features to FeatureTable.
        Args:
            features (str, DataFrame): A filename to load data from, or a
                pandas DataFrame. In either case, studies are in rows and
                features are in columns. Values in cells reflect the weight of
                the intersecting feature for the intersecting study. Feature
                names and study IDs should be included as the first column
                and first row, respectively.
            merge (str): The merge strategy to use when merging new features
                with old. This is passed to pandas.merge, so can be 'left',
                'right', 'outer', or 'inner'. Defaults to outer (i.e., all data
                in both new and old will be kept, and missing values will be
                assigned zeros.)
            duplicates (str): string indicating how to handle features whose
                name matches an existing feature. Valid options:
                    'ignore' (default): ignores the new feature, keeps old data
                    'replace': replace the old feature's data with the new data
                    'merge': keeps both features, renaming them so they're
                        different
            min_studies (int): minimum number of studies that pass threshold in
                order to add feature.
            threshold (float): minimum frequency threshold each study must
                exceed in order to count towards min_studies.


def get_feature_data(self, ids=None, features=None, dense=True):

result = self.data<EOL>if ids is not None:<EOL><INDENT>result = result.ix[ids]<EOL><DEDENT>if features is not None:<EOL><INDENT>result = result.ix[:, features]<EOL><DEDENT>return result.to_dense() if dense else result<EOL>

Slices and returns a subset of feature data.

        Args:
            ids (list, array): A list or 1D numpy array of study ids to
                return rows for. If None, returns data for all studies
                (i.e., all rows in array).
            features (list, array): A list or 1D numpy array of named features
                to return. If None, returns data for all features (i.e., all
                columns in array).
            dense (bool): Optional boolean. When True (default), convert the
                result to a dense array before returning. When False, keep as
                sparse matrix. Note that if ids is not None, the returned array
                will always be dense.
        Returns:
          A pandas DataFrame with study IDs in rows and features incolumns.


idxs = np.where(<EOL>np.in1d(self.data.columns.values, np.array(features)))[<NUM_LIT:0>]<EOL>return list(self.data.columns[idxs].values)<EOL>

Given a list of features, returns features in order that they
        appear in database.

        Args:
            features (list): A list or 1D numpy array of named features to
            return.

        Returns:
            A list of features in order they appear in database.


def get_ids(self, features, threshold=<NUM_LIT:0.0>, func=np.sum, get_weights=False):

if isinstance(features, str):<EOL><INDENT>features = [features]<EOL><DEDENT>features = self.search_features(features)  <EOL>feature_weights = self.data.ix[:, features]<EOL>weights = feature_weights.apply(func, <NUM_LIT:1>)<EOL>above_thresh = weights[weights >= threshold]<EOL>return above_thresh if get_weights else list(above_thresh.index)<EOL>

Returns a list of all studies in the table that meet the desired
        feature-based criteria.

        Will most commonly be used to retrieve studies that use one or more
        features with some minimum frequency; e.g.,:

            get_ids(['fear', 'anxiety'], threshold=0.001)

        Args:
            features (lists): a list of feature names to search on.
            threshold (float): optional float indicating threshold features
                must pass to be included.
            func (Callable): any numpy function to use for thresholding
                (default: sum). The function will be applied to the list of
                features and the result compared to the threshold. This can be
                used to change the meaning of the query in powerful ways. E.g,:
                    max: any of the features have to pass threshold
                        (i.e., max > thresh)
                    min: all features must each individually pass threshold
                        (i.e., min > thresh)
                    sum: the summed weight of all features must pass threshold
                        (i.e., sum > thresh)
            get_weights (bool): if True, returns a dict with ids => weights.

        Returns:
            When get_weights is false (default), returns a list of study
                names. When true, returns a dict, with study names as keys
                and feature weights as values.


if isinstance(search, string_types):<EOL><INDENT>search = [search]<EOL><DEDENT>search = [s.replace('<STR_LIT:*>', '<STR_LIT>') for s in search]<EOL>cols = list(self.data.columns)<EOL>results = []<EOL>for s in search:<EOL><INDENT>results.extend([f for f in cols if re.match(s + '<STR_LIT:$>', f)])<EOL><DEDENT>return list(set(results))<EOL>

Returns all features that match any of the elements in the input
        list.

        Args:
            search (str, list): A string or list of strings defining the query.

        Returns:
            A list of matching feature names.


def get_ids_by_expression(self, expression, threshold=<NUM_LIT>, func=np.sum):

lexer = lp.Lexer()<EOL>lexer.build()<EOL>parser = lp.Parser(<EOL>lexer, self.dataset, threshold=threshold, func=func)<EOL>parser.build()<EOL>return parser.parse(expression).keys().values<EOL>

Use a PEG to parse expression and return study IDs.


def get_features_by_ids(self, ids=None, threshold=<NUM_LIT>, func=np.mean,<EOL>get_weights=False):

weights = self.data.ix[ids].apply(func, <NUM_LIT:0>)<EOL>above_thresh = weights[weights >= threshold]<EOL>return above_thresh if get_weights else list(above_thresh.index)<EOL>

Returns features for which the mean loading across all specified
        studies (in ids) is >= threshold.


data = self.data.to_dense()<EOL>self.data = {<EOL>'<STR_LIT>': list(data.columns),<EOL>'<STR_LIT:index>': list(data.index),<EOL>'<STR_LIT>': sparse.csr_matrix(data.values)<EOL>}<EOL>

Convert FeatureTable to SciPy CSR matrix.


self.data = pd.DataFrame(self.data['<STR_LIT>'].todense(),<EOL>index=self.data['<STR_LIT:index>'],<EOL>columns=self.data['<STR_LIT>']).to_sparse()<EOL>


t = linalg.pinv(mat)<EOL>foci = np.hstack((foci, np.ones((foci.shape[<NUM_LIT:0>], <NUM_LIT:1>))))<EOL>return np.dot(foci, t)[:, <NUM_LIT:0>:<NUM_LIT:3>]<EOL>

Convert coordinates from one space to another using provided
    transformation matrix.


def xyz_to_mat(foci, xyz_dims=None, mat_dims=None):

foci = np.hstack((foci, np.ones((foci.shape[<NUM_LIT:0>], <NUM_LIT:1>))))<EOL>mat = np.array([[-<NUM_LIT:0.5>, <NUM_LIT:0>, <NUM_LIT:0>, <NUM_LIT>], [<NUM_LIT:0>, <NUM_LIT:0.5>, <NUM_LIT:0>, <NUM_LIT>], [<NUM_LIT:0>, <NUM_LIT:0>, <NUM_LIT:0.5>, <NUM_LIT>]]).T<EOL>result = np.dot(foci, mat)[:, ::-<NUM_LIT:1>]  <EOL>return np.round_(result).astype(int)<EOL>

Convert an N x 3 array of XYZ coordinates to matrix indices.


def mat_to_xyz(foci, mat_dims=None, xyz_dims=None):

foci = np.hstack((foci, np.ones((foci.shape[<NUM_LIT:0>], <NUM_LIT:1>))))<EOL>mat = np.array([[-<NUM_LIT:2>, <NUM_LIT:0>, <NUM_LIT:0>, <NUM_LIT>], [<NUM_LIT:0>, <NUM_LIT:2>, <NUM_LIT:0>, -<NUM_LIT>], [<NUM_LIT:0>, <NUM_LIT:0>, <NUM_LIT:2>, -<NUM_LIT>]]).T<EOL>result = np.dot(foci, mat)[:, ::-<NUM_LIT:1>]  <EOL>return np.round_(result).astype(int)<EOL>

Convert an N x 3 array of matrix indices to XYZ coordinates.


return np.array([[<NUM_LIT>, <NUM_LIT>, -<NUM_LIT>, -<NUM_LIT>],<EOL>[-<NUM_LIT>, <NUM_LIT>, -<NUM_LIT>, -<NUM_LIT>],<EOL>[<NUM_LIT>, <NUM_LIT>, <NUM_LIT>, <NUM_LIT>],<EOL>[<NUM_LIT:0.0>, <NUM_LIT:0.0>, <NUM_LIT:0.0>, <NUM_LIT:1.0>]]).T<EOL>

Convert Talairach to MNI coordinates using the Lancaster transform.
    Adapted from BrainMap scripts; see http://brainmap.org/icbm2tal/
    Details are described in Lancaster et al. (2007)
    (http://brainmap.org/new/pubs/LancasterHBM07.pdf).


def __init__(self, transforms=None, target='<STR_LIT>'):

self.target = target<EOL>self.transformations = transforms<EOL>


self.transformations[name] = mat<EOL>


if name in self.transformations:<EOL><INDENT>return transform(foci, self.transformations[name])<EOL><DEDENT>else:<EOL><INDENT>logger.info(<EOL>"<STR_LIT>"<EOL>"<STR_LIT>" % name)<EOL>return foci<EOL><DEDENT>

Apply a named transformation to a set of foci.

        If the named transformation doesn't exist, return foci untransformed.


if level is None:<EOL><INDENT>level = os.environ.get('<STR_LIT>', '<STR_LIT>')<EOL><DEDENT>if level is not None:<EOL><INDENT>logger.setLevel(getattr(logging, level.upper()))<EOL><DEDENT>return logger.getEffectiveLevel()<EOL>

Set neurosynth's logging level

    Args
      level : str
        Name of the logging level (warning, error, info, etc) known
        to logging module.  If no level provided, it would get that one
        from environment variable NEUROSYNTH_LOGLEVEL


def wrap(func):<EOL><INDENT>def wrapped_func(*args, **kwargs):<EOL><INDENT>warnings.warn(msg, category=DeprecationWarning)<EOL>return func(*args, **kwargs)<EOL><DEDENT>return wrapped_func<EOL><DEDENT>if len(args) == <NUM_LIT:1> and callable(args[<NUM_LIT:0>]):<EOL><INDENT>msg = "<STR_LIT>""<STR_LIT>" % args[<NUM_LIT:0>].__name__<EOL>return wrap(args[<NUM_LIT:0>])<EOL><DEDENT>else:<EOL><INDENT>msg = args[<NUM_LIT:0>]<EOL>return wrap<EOL><DEDENT>

Deprecation warning decorator. Takes optional deprecation message,
    otherwise will use a generic warning.


def feature_selection(feat_select, X, y):

<EOL>if re.match('<STR_LIT>', feat_select) is not None:<EOL><INDENT>n = int(feat_select.split('<STR_LIT:->')[<NUM_LIT:0>])<EOL>selector = SelectKBest(k=n)<EOL>import warnings<EOL>with warnings.catch_warnings():<EOL><INDENT>warnings.simplefilter('<STR_LIT:ignore>', category=UserWarning)<EOL>features_selected = np.where(<EOL>selector.fit(X, y).get_support() is True)[<NUM_LIT:0>]<EOL><DEDENT><DEDENT>elif re.match('<STR_LIT>', feat_select) is not None:<EOL><INDENT>n = int(feat_select.split('<STR_LIT:->')[<NUM_LIT:0>])<EOL>from random import shuffle<EOL>features = range(<NUM_LIT:0>, X.shape[<NUM_LIT:1>])<EOL>shuffle(features)<EOL>features_selected = features[:n]<EOL><DEDENT>return features_selected<EOL>

Implements various kinds of feature selection


def get_studies_by_regions(dataset, masks, threshold=<NUM_LIT>, remove_overlap=True,<EOL>studies=None, features=None,<EOL>regularization="<STR_LIT>"):

import nibabel as nib<EOL>import os<EOL>try:<EOL><INDENT>loaded_masks = [nib.load(os.path.relpath(m)) for m in masks]<EOL><DEDENT>except OSError:<EOL><INDENT>print('<STR_LIT>')<EOL><DEDENT>grouped_ids = [dataset.get_studies(mask=m, activation_threshold=threshold)<EOL>for m in loaded_masks]<EOL>flat_ids = reduce(lambda a, b: a + b, grouped_ids)<EOL>if remove_overlap:<EOL><INDENT>import collections<EOL>flat_ids = [id for (id, count) in<EOL>collections.Counter(flat_ids).items() if count == <NUM_LIT:1>]<EOL>grouped_ids = [[x for x in m if x in flat_ids] for m in<EOL>grouped_ids]  <EOL><DEDENT>y = [[idx] * len(ids) for (idx, ids) in enumerate(grouped_ids)]<EOL>y = reduce(lambda a, b: a + b, y)  <EOL>y = np.array(y)<EOL>X = [dataset.get_feature_data(ids=group_ids, features=features)<EOL>for group_ids in grouped_ids]<EOL>X = np.vstack(tuple(X))<EOL>if regularization:<EOL><INDENT>X = regularize(X, method=regularization)<EOL><DEDENT>return (X, y)<EOL>

Set up data for a classification task given a set of masks

        Given a set of masks, this function retrieves studies associated with
        each mask at the specified threshold, optionally removes overlap and
        filters by studies and features, and returns studies by feature matrix
        (X) and class labels (y)

        Args:
            dataset: a Neurosynth dataset
            maks: a list of paths to Nifti masks
            threshold: percentage of voxels active within the mask for study
                to be included
            remove_overlap: A boolean indicating if studies studies that
                appear in more than one mask should be excluded
            studies: An optional list of study names used to constrain the set
                used in classification. If None, will use all features in the
                dataset.
            features: An optional list of feature names used to constrain the
                set used in classification. If None, will use all features in
                the dataset.
            regularize: Optional boolean indicating if X should be regularized

        Returns:
            A tuple (X, y) of np arrays.
            X is a feature by studies matrix and y is a vector of class labels


all_features = dataset.get_feature_names()<EOL>i = [all_features.index(f) for f in features]<EOL>return i<EOL>

Returns a list with the order that features requested appear in
    dataset


def classify_regions(dataset, masks, method='<STR_LIT>', threshold=<NUM_LIT>,<EOL>remove_overlap=True, regularization='<STR_LIT>',<EOL>output='<STR_LIT>', studies=None, features=None,<EOL>class_weight='<STR_LIT>', classifier=None,<EOL>cross_val='<STR_LIT>', param_grid=None, scoring='<STR_LIT>'):

(X, y) = get_studies_by_regions(dataset, masks, threshold, remove_overlap,<EOL>studies, features,<EOL>regularization=regularization)<EOL>return classify(X, y, method, classifier, output, cross_val,<EOL>class_weight, scoring=scoring, param_grid=param_grid)<EOL>

Perform classification on specified regions

        Given a set of masks, this function retrieves studies associated with
        each mask at the specified threshold, optionally removes overlap and
        filters by studies and features. Then it trains an algorithm to
        classify studies based on features and tests performance.

        Args:
            dataset: a Neurosynth dataset
            maks: a list of paths to Nifti masks
            method: a string indicating which method to used.
                'SVM': Support Vector Classifier with rbf kernel
                'ERF': Extremely Randomized Forest classifier
                'Dummy': A dummy classifier using stratified classes as
                    predictor
            threshold: percentage of voxels active within the mask for study
                to be included
            remove_overlap: A boolean indicating if studies studies that
                appear in more than one mask should be excluded
            regularization: A string indicating type of regularization to use.
                If None, performs no regularization.
                'scale': Unit scale without demeaning
            output: A string indicating output type
                'summary': Dictionary with summary statistics including score
                    and n
                'summary_clf': Same as above but also includes classifier
                'clf': Only returns classifier
                Warning: using cv without grid will return an untrained
                classifier
            studies: An optional list of study names used to constrain the set
                used in classification. If None, will use all features in the
                dataset.
            features: An optional list of feature names used to constrain the
                set used in classification. If None, will use all features in
                the dataset.
            class_weight: Parameter to pass to classifier determining how to
                weight classes
            classifier: An optional sci-kit learn classifier to use instead of
                pre-set up classifiers set up using 'method'
            cross_val: A string indicating type of cross validation to use.
                Can also pass a scikit_classifier
            param_grid: A dictionary indicating which parameters to optimize
                using GridSearchCV. If None, no GridSearch will be used

        Returns:
            A tuple (X, y) of np arrays.
            X is a feature by studies matrix and y is a vector of class labels


def classify(X, y, clf_method='<STR_LIT>', classifier=None, output='<STR_LIT>',<EOL>cross_val=None, class_weight=None, regularization=None,<EOL>param_grid=None, scoring='<STR_LIT>', refit_all=True,<EOL>feat_select=None):

<EOL>clf = Classifier(clf_method, classifier, param_grid)<EOL>if cross_val is not None:<EOL><INDENT>score = clf.cross_val_fit(X, y, cross_val, scoring=scoring,<EOL>feat_select=feat_select,<EOL>class_weight=class_weight)<EOL><DEDENT>else:<EOL><INDENT>score = clf.fit(X, y, class_weight=class_weight).score(X, y)<EOL><DEDENT>from collections import Counter<EOL>if output == '<STR_LIT>':<EOL><INDENT>return clf<EOL><DEDENT>else:<EOL><INDENT>if output == '<STR_LIT>':<EOL><INDENT>output = {'<STR_LIT>': score, '<STR_LIT:n>': dict(Counter(y))}<EOL><DEDENT>elif output == '<STR_LIT>':<EOL><INDENT>output = {<EOL>'<STR_LIT>': score,<EOL>'<STR_LIT:n>': dict(Counter(y)),<EOL>'<STR_LIT>': clf,<EOL>'<STR_LIT>': clf.features_selected,<EOL>'<STR_LIT>': clf.predictions<EOL>}<EOL><DEDENT>return output<EOL><DEDENT>

Wrapper for scikit-learn classification functions
    Imlements various types of classification and cross validation


def __init__(self, clf_method='<STR_LIT>', classifier=None, param_grid=None):

<EOL>self.features_selected = None<EOL>self.predictions = None<EOL>if classifier is not None:<EOL><INDENT>self.clf = classifier<EOL>from sklearn.svm import LinearSVC<EOL>import random<EOL>if isinstance(self.clf, LinearSVC):<EOL><INDENT>self.clf.set_params().random_state = random.randint(<NUM_LIT:0>, <NUM_LIT:200>)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>if clf_method == '<STR_LIT>':<EOL><INDENT>from sklearn import svm<EOL>self.clf = svm.SVC()<EOL><DEDENT>elif clf_method == '<STR_LIT>':<EOL><INDENT>from sklearn.ensemble import ExtraTreesClassifier<EOL>self.clf = ExtraTreesClassifier(<EOL>n_estimators=<NUM_LIT:100>, max_depth=None, min_samples_split=<NUM_LIT:1>,<EOL>random_state=<NUM_LIT:0>)<EOL><DEDENT>elif clf_method == '<STR_LIT>':<EOL><INDENT>from sklearn.ensemble import GradientBoostingClassifier<EOL>self.clf = GradientBoostingClassifier(n_estimators=<NUM_LIT:100>,<EOL>max_depth=<NUM_LIT:1>)<EOL><DEDENT>elif clf_method == '<STR_LIT>':<EOL><INDENT>from sklearn.dummy import DummyClassifier<EOL>self.clf = DummyClassifier(strategy='<STR_LIT>')<EOL><DEDENT>else:<EOL><INDENT>raise Exception('<STR_LIT>')<EOL><DEDENT><DEDENT>if isinstance(param_grid, dict):<EOL><INDENT>from sklearn.grid_search import GridSearchCV<EOL>self.clf = GridSearchCV(estimator=self.clf,<EOL>param_grid=param_grid)<EOL><DEDENT>


def fit(self, X, y, cv=None, class_weight='<STR_LIT>'):

<EOL>self.X = X<EOL>self.y = y<EOL>self.set_class_weight(class_weight=class_weight, y=y)<EOL>self.clf = self.clf.fit(X, y)<EOL>return self.clf<EOL>


def set_class_weight(self, class_weight='<STR_LIT>', y=None):

if class_weight is None:<EOL><INDENT>cw = None<EOL>try:<EOL><INDENT>self.clf.set_params(class_weight=cw)<EOL><DEDENT>except ValueError:<EOL><INDENT>pass<EOL><DEDENT><DEDENT>elif class_weight == '<STR_LIT>':<EOL><INDENT>c = np.bincount(y)<EOL>ii = np.nonzero(c)[<NUM_LIT:0>]<EOL>c = c / float(c.sum())<EOL>cw = dict(zip(ii[::-<NUM_LIT:1>], c[ii]))<EOL>try:<EOL><INDENT>self.clf.set_params(class_weight=cw)<EOL><DEDENT>except ValueError:<EOL><INDENT>import warnings<EOL>warnings.warn(<EOL>"<STR_LIT>"<EOL>"<STR_LIT>")<EOL><DEDENT><DEDENT>

Sets the class_weight of the classifier to match y


def cross_val_fit(self, X, y, cross_val='<STR_LIT>', scoring='<STR_LIT>',<EOL>feat_select=None, class_weight='<STR_LIT>'):

from sklearn import cross_validation<EOL>self.X = X<EOL>self.y = y<EOL>self.set_class_weight(class_weight=class_weight, y=y)<EOL>if isinstance(cross_val, string_types):<EOL><INDENT>if re.match('<STR_LIT>', cross_val) is not None:<EOL><INDENT>n = int(cross_val.split('<STR_LIT:->')[<NUM_LIT:0>])<EOL>self.cver = cross_validation.StratifiedKFold(self.y, n)<EOL><DEDENT>else:<EOL><INDENT>raise Exception('<STR_LIT>')<EOL><DEDENT><DEDENT>else:<EOL><INDENT>self.cver = cross_val<EOL><DEDENT>if feat_select is not None:<EOL><INDENT>self.features_selected = []<EOL><DEDENT>from sklearn.grid_search import GridSearchCV<EOL>if isinstance(self.clf, GridSearchCV):<EOL><INDENT>import warnings<EOL>if feat_select is not None:<EOL><INDENT>warnings.warn(<EOL>"<STR_LIT>"<EOL>"<STR_LIT>")<EOL><DEDENT>self.clf.set_params(cv=self.cver, scoring=scoring)<EOL>with warnings.catch_warnings():<EOL><INDENT>warnings.simplefilter('<STR_LIT:ignore>', category=UserWarning)<EOL>self.clf = self.clf.fit(X, y)<EOL><DEDENT>self.cvs = self.clf.best_score_<EOL><DEDENT>else:<EOL><INDENT>self.cvs = self.feat_select_cvs(<EOL>feat_select=feat_select, scoring=scoring)<EOL><DEDENT>if feat_select is not None:<EOL><INDENT>fs = feature_selection(<EOL>feat_select, X, y)<EOL>self.features_selected.append(fs)<EOL>X = X[:, fs]<EOL><DEDENT>self.clf.fit(X, y)<EOL>return self.cvs.mean()<EOL>

Fits X to outcomes y, using clf and cv_method


def feat_select_cvs(self, scoring=None, feat_select=None):

scores = []<EOL>self.predictions = []<EOL>for train, test in self.cver:<EOL><INDENT>X_train, X_test, y_train, y_test = self.X[<EOL>train], self.X[test], self.y[train], self.y[test]<EOL>if feat_select is not None:<EOL><INDENT>fs = feature_selection(<EOL>feat_select, X_train, y_train)<EOL>self.features_selected.append(fs)<EOL>X_train, X_test = X_train[<EOL>:, fs], X_test[:, fs]<EOL><DEDENT>self.clf.fit(X_train, y_train)<EOL>predicition, s = get_score(<EOL>X_test, y_test, self.clf, scoring=scoring)<EOL>scores.append(s)<EOL>self.predictions.append((y_test, predicition))<EOL><DEDENT>return np.array(scores)<EOL>

Returns cross validated scores (just like cross_val_score),
        but includes feature selection as part of the cross validation loop


def fit_dataset(self, dataset, y, features=None,<EOL>feature_type='<STR_LIT>'):

<EOL>if feature_type == '<STR_LIT>':<EOL><INDENT>X = np.rot90(dataset.feature_table.data.toarray())<EOL><DEDENT>elif feature_type == '<STR_LIT>':<EOL><INDENT>X = np.rot90(dataset.image_table.data.toarray())<EOL><DEDENT>self.sk_classifier.fit(X, y)<EOL>

Given a dataset, fits either features or voxels to y


def average_within_regions(dataset, regions, masker=None, threshold=None,<EOL>remove_zero=True):

if masker is not None:<EOL><INDENT>masker = masker<EOL><DEDENT>else:<EOL><INDENT>if isinstance(dataset, Dataset):<EOL><INDENT>masker = dataset.masker<EOL><DEDENT>else:<EOL><INDENT>if not type(regions).__module__.startswith('<STR_LIT>'):<EOL><INDENT>raise ValueError(<EOL>"<STR_LIT>"<EOL>"<STR_LIT>")<EOL><DEDENT><DEDENT><DEDENT>if not type(regions).__module__.startswith('<STR_LIT>'):<EOL><INDENT>regions = masker.mask(regions)<EOL><DEDENT>if isinstance(dataset, Dataset):<EOL><INDENT>dataset = dataset.get_image_data(dense=False)<EOL><DEDENT>if regions.ndim == <NUM_LIT:2>:<EOL><INDENT>m = regions<EOL>for i in range(regions.shape[<NUM_LIT:1>]):<EOL><INDENT>_nz = np.nonzero(m[:, i])[<NUM_LIT:0>]<EOL>if isinstance(threshold, int):<EOL><INDENT>m[_nz, i] = <NUM_LIT:1.0><EOL><DEDENT>else:<EOL><INDENT>m[_nz, i] = <NUM_LIT:1.0> / np.count_nonzero(m[:, i])<EOL><DEDENT><DEDENT><DEDENT>else:<EOL><INDENT>labels = np.unique(regions)<EOL>if remove_zero:<EOL><INDENT>labels = labels[np.nonzero(labels)]<EOL><DEDENT>n_regions = labels.size<EOL>m = np.zeros((regions.size, n_regions))<EOL>for i in range(n_regions):<EOL><INDENT>if isinstance(threshold, int):<EOL><INDENT>m[regions == labels[i], i] = <NUM_LIT:1.0><EOL><DEDENT>else:<EOL><INDENT>m[regions == labels[i], i] = <NUM_LIT:1.0> /np.sum(regions == labels[i])<EOL><DEDENT><DEDENT><DEDENT>result = dataset.T.dot(m).T<EOL>if threshold is not None:<EOL><INDENT>result[result < threshold] = <NUM_LIT:0.0><EOL>result = result.astype(bool)<EOL><DEDENT>return result<EOL>

Aggregates over all voxels within each ROI in the input image.

    Takes a Dataset and a Nifti image that defines distinct regions, and
    returns a numpy matrix of  ROIs x mappables, where the value at each
    ROI is the proportion of active voxels in that ROI. Each distinct ROI
    must have a unique value in the image; non-contiguous voxels with the
    same value will be assigned to the same ROI.

    Args:
        dataset: Either a Dataset instance from which image data are
            extracted, or a Numpy array containing image data to use. If
            the latter, the array contains voxels in rows and
            features/studies in columns. The number of voxels must be equal
            to the length of the vectorized image mask in the regions
            image.
        regions: An image defining the boundaries of the regions to use.
            Can be one of:
            1) A string name of the NIFTI or Analyze-format image
            2) A NiBabel SpatialImage
            3) A list of NiBabel images
            4) A 1D numpy array of the same length as the mask vector in
                the Dataset's current Masker.
        masker: Optional masker used to load image if regions is not a
            numpy array. Must be passed if dataset is a numpy array.
        threshold: An optional float in the range of 0 - 1 or integer. If
            passed, the array will be binarized, with ROI values above the
            threshold assigned to True and values below the threshold
            assigned to False. (E.g., if threshold = 0.05, only ROIs in
            which more than 5% of voxels are active will be considered
            active.) If threshold is integer, studies will only be
            considered active if they activate more than that number of
            voxels in the ROI.
        remove_zero: An optional boolean; when True, assume that voxels
            with value of 0 should not be considered as a separate ROI, and
            will be ignored.

    Returns:
        A 2D numpy array with ROIs in rows and mappables in columns.


def apply_grid(dataset, masker=None, scale=<NUM_LIT:5>, threshold=None):

if masker is None:<EOL><INDENT>if isinstance(dataset, Dataset):<EOL><INDENT>masker = dataset.masker<EOL><DEDENT>else:<EOL><INDENT>raise ValueError(<EOL>"<STR_LIT>")<EOL><DEDENT><DEDENT>grid = imageutils.create_grid(masker.volume, scale)<EOL>cm = masker.mask(grid, in_global_mask=True)<EOL>data = average_within_regions(dataset, cm, threshold)<EOL>return (data, grid)<EOL>

Imposes a 3D grid on the brain volume and averages across all voxels
    that fall within each cell.
    Args:
        dataset: Data to apply grid to. Either a Dataset instance, or a numpy
            array with voxels in rows and features in columns.
        masker: Optional Masker instance used to map between the created grid
            and the dataset. This is only needed if dataset is a numpy array;
            if dataset is a Dataset instance, the Masker in the dataset will
            be used.
        scale: int; scaling factor (in mm) to pass onto create_grid().
        threshold: Optional float to pass to reduce.average_within_regions().
    Returns:
        A tuple of length 2, where the first element is a numpy array of
        dimensions n_cubes x n_studies, and the second element is a numpy
        array, with the same dimensions as the Masker instance in the current
        Dataset, that maps voxel identities onto cell IDs in the grid.


voxels = np.arange(dataset.masker.n_vox_in_vol)<EOL>np.random.shuffle(voxels)<EOL>selected = voxels[<NUM_LIT:0>:n_voxels]<EOL>return dataset.get_image_data(voxels=selected)<EOL>

Returns mappable data for a random subset of voxels.

    May be useful as a baseline in predictive analyses--e.g., to compare
    performance of a more principled feature selection method with simple
    random selection.

    Args:
        dataset: A Dataset instance
        n_voxels: An integer specifying the number of random voxels to select.

    Returns:
        A 2D numpy array with (randomly-selected) voxels in rows and mappables
        in columns.


def _get_top_words(model, feature_names, n_top_words=<NUM_LIT>):

topic_words = []<EOL>for topic in model.components_:<EOL><INDENT>top_words = [feature_names[i] for i in topic.argsort()[:-n_top_words-<NUM_LIT:1>:-<NUM_LIT:1>]]<EOL>topic_words += [top_words]<EOL><DEDENT>return topic_words<EOL>

Return top forty words from each topic in trained topic model.


def run_lda(abstracts, n_topics=<NUM_LIT:50>, n_words=<NUM_LIT>, n_iters=<NUM_LIT:1000>, alpha=None,<EOL>beta=<NUM_LIT>):

if abstracts.index.name != '<STR_LIT>':<EOL><INDENT>abstracts.index = abstracts['<STR_LIT>']<EOL><DEDENT>resdir = os.path.abspath(get_resource_path())<EOL>tempdir = os.path.join(resdir, '<STR_LIT>')<EOL>absdir = os.path.join(tempdir, '<STR_LIT>')<EOL>if not os.path.isdir(tempdir):<EOL><INDENT>os.mkdir(tempdir)<EOL><DEDENT>if alpha is None:<EOL><INDENT>alpha = <NUM_LIT> / n_topics<EOL><DEDENT>if not os.path.isdir(absdir):<EOL><INDENT>print('<STR_LIT>')<EOL>os.mkdir(absdir)<EOL>for pmid in abstracts.index.values:<EOL><INDENT>abstract = abstracts.loc[pmid]['<STR_LIT>']<EOL>with open(os.path.join(absdir, str(pmid) + '<STR_LIT>'), '<STR_LIT:w>') as fo:<EOL><INDENT>fo.write(abstract)<EOL><DEDENT><DEDENT><DEDENT>print('<STR_LIT>')<EOL>mallet_bin = join(dirname(dirname(__file__)),<EOL>'<STR_LIT>')<EOL>import_str = ('<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>').format(mallet=mallet_bin,<EOL>absdir=absdir,<EOL>outdir=tempdir)<EOL>train_str = ('<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>').format(mallet=mallet_bin, out=tempdir,<EOL>n_topics=n_topics, n_words=n_words,<EOL>n_iters=n_iters,<EOL>alpha=alpha, beta=beta)<EOL>subprocess.call(import_str, shell=True)<EOL>subprocess.call(train_str, shell=True)<EOL>def clean_str(string):<EOL><INDENT>return os.path.basename(os.path.splitext(string)[<NUM_LIT:0>])<EOL><DEDENT>def get_sort(lst):<EOL><INDENT>return [i[<NUM_LIT:0>] for i in sorted(enumerate(lst), key=lambda x:x[<NUM_LIT:1>])]<EOL><DEDENT>topic_names = ['<STR_LIT>'.format(i) for i in range(n_topics)]<EOL>n_cols = (<NUM_LIT:2> * n_topics) + <NUM_LIT:1><EOL>dt_df = pd.read_csv(os.path.join(tempdir, '<STR_LIT>'),<EOL>delimiter='<STR_LIT:\t>', skiprows=<NUM_LIT:1>, header=None, index_col=<NUM_LIT:0>)<EOL>dt_df = dt_df[dt_df.columns[:n_cols]]<EOL>dt_df[<NUM_LIT:1>] = dt_df[<NUM_LIT:1>].apply(clean_str)<EOL>weights_df = dt_df[dt_df.columns[<NUM_LIT:2>::<NUM_LIT:2>]]<EOL>weights_df.index = dt_df[<NUM_LIT:1>]<EOL>weights_df.columns = range(n_topics)<EOL>topics_df = dt_df[dt_df.columns[<NUM_LIT:1>::<NUM_LIT:2>]]<EOL>topics_df.index = dt_df[<NUM_LIT:1>]<EOL>topics_df.columns = range(n_topics)<EOL>sorters_df = topics_df.apply(get_sort, axis=<NUM_LIT:1>)<EOL>weights = weights_df.as_matrix()<EOL>sorters = sorters_df.as_matrix()<EOL>for i in range(sorters.shape[<NUM_LIT:0>]):<EOL><INDENT>weights[i, :] = weights[i, sorters[i, :]]<EOL><DEDENT>index = dt_df[<NUM_LIT:1>]<EOL>weights_df = pd.DataFrame(columns=topic_names, data=weights, index=index)<EOL>weights_df.index.name = '<STR_LIT>'<EOL>keys_df = pd.read_csv(os.path.join(tempdir, '<STR_LIT>'),<EOL>delimiter='<STR_LIT:\t>', header=None, index_col=<NUM_LIT:0>)<EOL>keys_df = keys_df[[<NUM_LIT:2>]]<EOL>keys_df.rename(columns={<NUM_LIT:2>: '<STR_LIT>'}, inplace=True)<EOL>keys_df.index = topic_names<EOL>keys_df.index.name = '<STR_LIT>'<EOL>shutil.rmtree(tempdir)<EOL>return weights_df, keys_df<EOL>

Perform topic modeling using Latent Dirichlet Allocation with the
    Java toolbox MALLET.

    Args:
        abstracts:  A pandas DataFrame with two columns ('pmid' and 'abstract')
                    containing article abstracts.
        n_topics:   Number of topics to generate. Default=50.
        n_words:    Number of top words to return for each topic. Default=31,
                    based on Poldrack et al. (2012).
        n_iters:    Number of iterations to run in training topic model.
                    Default=1000.
        alpha:      The Dirichlet prior on the per-document topic
                    distributions.
                    Default: 50 / n_topics, based on Poldrack et al. (2012).
        beta:       The Dirichlet prior on the per-topic word distribution.
                    Default: 0.001, based on Poldrack et al. (2012).

    Returns:
        weights_df: A pandas DataFrame derived from the MALLET
                    output-doc-topics output file. Contains the weight assigned
                    to each article for each topic, which can be used to select
                    articles for topic-based meta-analyses (accepted threshold
                    from Poldrack article is 0.001). [n_topics]+1 columns:
                    'pmid' is the first column and the following columns are
                    the topic names. The names of the topics match the names
                    in df (e.g., topic_000).
        keys_df:    A pandas DataFrame derived from the MALLET
                    output-topic-keys output file. Contains the top [n_words]
                    words for each topic, which can act as a summary of the
                    topic's content. Two columns: 'topic' and 'terms'. The
                    names of the topics match the names in weights (e.g.,
                    topic_000).


def __init__(self, dataset, ids, ids2=None, q=<NUM_LIT>, prior=<NUM_LIT:0.5>,<EOL>min_studies=<NUM_LIT:1>):

self.dataset = dataset<EOL>mt = dataset.image_table<EOL>self.selected_ids = list(set(mt.ids) & set(ids))<EOL>self.selected_id_indices = np.in1d(mt.ids, ids)<EOL>unselected_id_indices = ~self.selected_id_indices if ids2 is Noneelse np.in1d(mt.ids, ids2)<EOL>mappable_id_indices = self.selected_id_indices | unselected_id_indices<EOL>logger.debug("<STR_LIT>")<EOL>n_selected = len(self.selected_ids)<EOL>n_unselected = np.sum(unselected_id_indices)<EOL>n_mappables = n_selected + n_unselected<EOL>n_selected_active_voxels = mt.data.dot(self.selected_id_indices)<EOL>n_unselected_active_voxels = mt.data.dot(unselected_id_indices)<EOL>pF = (n_selected * <NUM_LIT:1.0>) / n_mappables<EOL>mappable_data = mt.data if ids2 is None else mt.data[:, mappable_id_indices]<EOL>pA = np.array((mappable_data.sum(axis=<NUM_LIT:1>) * <NUM_LIT:1.0>) / n_mappables).squeeze()<EOL>logger.debug("<STR_LIT>")<EOL>pAgF = n_selected_active_voxels * <NUM_LIT:1.0> / n_selected<EOL>pAgU = n_unselected_active_voxels * <NUM_LIT:1.0> / n_unselected<EOL>pFgA = pAgF * pF / pA<EOL>logger.debug("<STR_LIT>")<EOL>pA_prior = prior * pAgF + (<NUM_LIT:1> - prior) * pAgU<EOL>pFgA_prior = pAgF * prior / pA_prior<EOL>def p_to_z(p, sign):<EOL><INDENT>p = p / <NUM_LIT:2>  <EOL>p[p < <NUM_LIT>] = <NUM_LIT><EOL>z = np.abs(norm.ppf(p)) * sign<EOL>z[np.isinf(z)] = norm.ppf(<NUM_LIT>) * -<NUM_LIT:1><EOL>return z<EOL><DEDENT>p_vals = stats.one_way(<EOL>np.squeeze(n_selected_active_voxels), n_selected)<EOL>p_vals[p_vals < <NUM_LIT>] = <NUM_LIT><EOL>z_sign = np.sign(<EOL>n_selected_active_voxels - np.mean(<EOL>n_selected_active_voxels)).ravel()<EOL>pAgF_z = p_to_z(p_vals, z_sign)<EOL>fdr_thresh = stats.fdr(p_vals, q)<EOL>pAgF_z_FDR = imageutils.threshold_img(<EOL>pAgF_z, fdr_thresh, p_vals, mask_out='<STR_LIT>')<EOL>cells = np.squeeze(<EOL>np.array([[n_selected_active_voxels, n_unselected_active_voxels],<EOL>[n_selected - n_selected_active_voxels, n_unselected -<EOL>n_unselected_active_voxels]]).T)<EOL>p_vals = stats.two_way(cells)<EOL>p_vals[p_vals < <NUM_LIT>] = <NUM_LIT><EOL>z_sign = np.sign(pAgF - pAgU).ravel()<EOL>pFgA_z = p_to_z(p_vals, z_sign)<EOL>fdr_thresh = stats.fdr(p_vals, q)<EOL>pFgA_z_FDR = imageutils.threshold_img(<EOL>pFgA_z, fdr_thresh, p_vals, mask_out='<STR_LIT>')<EOL>self.images = {<EOL>'<STR_LIT>': pA,<EOL>'<STR_LIT>': pAgF,<EOL>'<STR_LIT>': pFgA,<EOL>('<STR_LIT>' % prior): pA_prior,<EOL>('<STR_LIT>' % prior): pFgA_prior,<EOL>'<STR_LIT>': pAgF_z,<EOL>'<STR_LIT>': pFgA_z,<EOL>('<STR_LIT>' % q): pAgF_z_FDR,<EOL>('<STR_LIT>' % q): pFgA_z_FDR<EOL>}<EOL>if min_studies > <NUM_LIT:0>:<EOL><INDENT>if isinstance(min_studies, int):<EOL><INDENT>min_studies = float(<EOL>min_studies) / n_mappables  <EOL><DEDENT>vox_to_exclude = np.where(pA < min_studies)[<NUM_LIT:0>]  <EOL>for k in self.images:<EOL><INDENT>self.images[k][vox_to_exclude] = <NUM_LIT:0><EOL><DEDENT><DEDENT>

Initialize a new MetaAnalysis instance and run an analysis.
        Args:
            dataset: A Dataset instance.
            ids: A list of Mappable IDs to include in the meta-analysis.
            ids2: Optional second list of Mappable IDs. If passed, the set of
                studies will be restricted to the union of ids and ids2 before
                performing the meta-analysis. This is useful for meta-analytic
                contrasts, as the resulting images will in effect identify
                regions that are reported/activated more frequently in one
                list than in the other.
            q: The FDR threshold to use when correcting for multiple
                comparisons. Set to .01 by default.
            prior: The prior to use when calculating conditional probabilities.
                This is the prior probability of a feature being used in a
                study (i.e., p(F)). For example, if set to 0.25, the analysis
                will assume that 1/4 of studies load on the target feature, as
                opposed to the empirically estimated p(F), which is len(ids) /
                total number of studies in the dataset. If prior is not passed,
                defaults to 0.5, reflecting an effort to put all terms on level
                footing and avoid undue influence of base rates (because some
                terms are much more common than others). Note that modifying
                the prior will only affect the effect size/probability maps,
                and not the statistical inference (z-score) maps.
            min_studies: Integer or float indicating which voxels to mask out
                from results due to lack of stability. If an integer is passed,
                all voxels that activate in fewer than this number of studies
                will be ignored (i.e., a value of 0 will be assigned in all
                output images). If a float in the range of 0 - 1 is passed,
                this will be interpreted as a proportion to use as the cut-off
                (e.g., passing 0.03 will exclude all voxels active in fewer
                than 3% of the entire dataset). Defaults to 1, meaning all
                voxels that activate at least one study will be kept.


def save_results(self, output_dir='<STR_LIT:.>', prefix='<STR_LIT>', prefix_sep='<STR_LIT:_>',<EOL>image_list=None):

if prefix == '<STR_LIT>':<EOL><INDENT>prefix_sep = '<STR_LIT>'<EOL><DEDENT>if not exists(output_dir):<EOL><INDENT>makedirs(output_dir)<EOL><DEDENT>logger.debug("<STR_LIT>")<EOL>if image_list is None:<EOL><INDENT>image_list = self.images.keys()<EOL><DEDENT>for suffix, img in self.images.items():<EOL><INDENT>if suffix in image_list:<EOL><INDENT>filename = prefix + prefix_sep + suffix + '<STR_LIT>'<EOL>outpath = join(output_dir, filename)<EOL>imageutils.save_img(img, outpath, self.dataset.masker)<EOL><DEDENT><DEDENT>

Write out any images generated by the meta-analysis.
        Args:
            output_dir (str): folder to write images to
            prefix (str): all image files will be prepended with this string
            prefix_sep (str): glue between the prefix and rest of filename
            image_list (list): optional list of images to save--e.g.,
                ['pFgA_z', 'pAgF']. If image_list is None (default), will save
                all images.


def decode(self, images, save=None, round=<NUM_LIT:4>, names=None, **kwargs):

if isinstance(images, string_types):<EOL><INDENT>images = [images]<EOL><DEDENT>if isinstance(images, list):<EOL><INDENT>imgs_to_decode = imageutils.load_imgs(images, self.masker)<EOL><DEDENT>else:<EOL><INDENT>imgs_to_decode = images<EOL><DEDENT>methods = {<EOL>'<STR_LIT>': self._pearson_correlation,<EOL>'<STR_LIT>': self._dot_product,<EOL>'<STR_LIT>': self._roi_association<EOL>}<EOL>result = np.around(<EOL>methods[self.method](imgs_to_decode, **kwargs), round)<EOL>if names is None:<EOL><INDENT>if type(images).__module__ == np.__name__:<EOL><INDENT>names = ['<STR_LIT>' % i for i in range(images.shape[<NUM_LIT:1>])]<EOL><DEDENT>elif self.method == '<STR_LIT>':<EOL><INDENT>names = ['<STR_LIT>' % i for i in range(result.shape[<NUM_LIT:1>])]<EOL><DEDENT>else:<EOL><INDENT>names = images<EOL><DEDENT><DEDENT>result = pd.DataFrame(result, columns=names, index=self.feature_names)<EOL>if save is not None:<EOL><INDENT>result.to_csv(save, index_label='<STR_LIT>')<EOL><DEDENT>return result<EOL>

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.


self.method = method<EOL>


def load_features(self, features, image_type=None, from_array=False,<EOL>threshold=<NUM_LIT>):

if from_array:<EOL><INDENT>if isinstance(features, list):<EOL><INDENT>features = features[<NUM_LIT:0>]<EOL><DEDENT>self._load_features_from_array(features)<EOL><DEDENT>elif path.exists(features[<NUM_LIT:0>]):<EOL><INDENT>self._load_features_from_images(features)<EOL><DEDENT>else:<EOL><INDENT>self._load_features_from_dataset(<EOL>features, image_type=image_type, threshold=threshold)<EOL><DEDENT>

Load features from current Dataset instance or a list of files.
        Args:
            features: List containing paths to, or names of, features to
                extract. Each element in the list must be a string containing
                either a path to an image, or the name of a feature (as named
                in the current Dataset). Mixing of paths and feature names
                within the list is not allowed.
            image_type: Optional suffix indicating which kind of image to use
                for analysis. Only used if features are taken from the Dataset;
                if features is a list of filenames, image_type is ignored.
            from_array: If True, the features argument is interpreted as a
                string pointing to the location of a 2D ndarray on disk
                containing feature data, where rows are voxels and columns are
                individual features.
            threshold: If features are taken from the dataset, this is the
                threshold passed to the meta-analysis module to generate fresh
                images.


self.feature_images = np.load(features)<EOL>self.feature_names = range(self.feature_images.shape[<NUM_LIT:1>])<EOL>

Load feature data from a 2D ndarray on disk.


def _load_features_from_dataset(self, features=None, image_type=None,<EOL>threshold=<NUM_LIT>):

self.feature_names = self.dataset.feature_table.feature_names<EOL>if features is not None:<EOL><INDENT>self.feature_names = [f for f in features<EOL>if f in self.feature_names]<EOL><DEDENT>from neurosynth.analysis import meta<EOL>self.feature_images = meta.analyze_features(<EOL>self.dataset, self.feature_names, image_type=image_type,<EOL>threshold=threshold)<EOL>if self.masker.layers:<EOL><INDENT>in_mask = self.masker.get_mask(in_global_mask=True)<EOL>self.feature_images = self.feature_images[in_mask, :]<EOL><DEDENT>

Load feature image data from the current Dataset instance. See
        load_features() for documentation.


def _load_features_from_images(self, images, names=None):

if names is not None and len(names) != len(images):<EOL><INDENT>raise Exception(<EOL>"<STR_LIT>")<EOL><DEDENT>self.feature_names = names if names is not None else images<EOL>self.feature_images = imageutils.load_imgs(images, self.masker)<EOL>

Load feature image data from image files.

        Args:
          images: A list of image filenames.
          names: An optional list of strings to use as the feature names. Must
            be in the same order as the images.


<EOL>pass<EOL>


x, y = imgs_to_decode.astype(float), self.feature_images.astype(float)<EOL>return self._xy_corr(x, y)<EOL>

Decode images using Pearson's r.

        Computes the correlation between each input image and each feature
        image across voxels.

        Args:
            imgs_to_decode: An ndarray of images to decode, with voxels in rows
                and images in columns.

        Returns:
            An n_features x n_images 2D array, with each cell representing the
            pearson correlation between the i'th feature and the j'th image
            across all voxels.


return np.dot(imgs_to_decode.T, self.feature_images).T<EOL>


def _roi_association(self, imgs_to_decode, value='<STR_LIT:z>', binarize=None):

imgs_to_decode = imgs_to_decode.squeeze()<EOL>x = average_within_regions(self.dataset, imgs_to_decode).astype(float)<EOL>y = self.dataset.feature_table.data[self.feature_names].values<EOL>if binarize is not None:<EOL><INDENT>y[y > binarize] = <NUM_LIT:1.><EOL>y[y < <NUM_LIT:1.>] = <NUM_LIT:0.><EOL><DEDENT>r = self._xy_corr(x.T, y)<EOL>if value == '<STR_LIT:r>':<EOL><INDENT>return r<EOL><DEDENT>elif value == '<STR_LIT:z>':<EOL><INDENT>f_r = np.arctanh(r)<EOL>return f_r * np.sqrt(y.shape[<NUM_LIT:0>] - <NUM_LIT:3>)<EOL><DEDENT>

Computes the strength of association between activation in a mask
        and presence/absence of a semantic feature. This is essentially a
        generalization of the voxel-wise reverse inference z-score to the
        multivoxel case.


data = np.vstack((x, y))<EOL>ms = data.mean(axis=<NUM_LIT:1>)[(slice(None, None, None), None)]<EOL>datam = data - ms<EOL>datass = np.sqrt(np.sum(datam**<NUM_LIT:2>, axis=<NUM_LIT:1>))<EOL>temp = np.dot(datam[<NUM_LIT:1>:], datam[<NUM_LIT:0>].T)<EOL>rs = temp / (datass[<NUM_LIT:1>:] * datass[<NUM_LIT:0>])<EOL>return rs<EOL>

Correlates row vector x with each row vector in 2D array y.


<EOL>warnings.simplefilter("<STR_LIT:ignore>", RuntimeWarning)<EOL>cells = cells.astype('<STR_LIT>')  <EOL>total = np.apply_over_axes(np.sum, cells, [<NUM_LIT:1>, <NUM_LIT:2>]).ravel()<EOL>chi_sq = np.zeros(cells.shape, dtype='<STR_LIT>')<EOL>for i in range(<NUM_LIT:2>):<EOL><INDENT>for j in range(<NUM_LIT:2>):<EOL><INDENT>exp = np.sum(cells[:, i, :], <NUM_LIT:1>).ravel() *np.sum(cells[:, :, j], <NUM_LIT:1>).ravel() / total<EOL>bad_vox = np.where(exp == <NUM_LIT:0>)[<NUM_LIT:0>]<EOL>chi_sq[:, i, j] = (cells[:, i, j] - exp) ** <NUM_LIT:2> / exp<EOL>chi_sq[bad_vox, i, j] = <NUM_LIT:1.0>  <EOL><DEDENT><DEDENT>chi_sq = np.apply_over_axes(np.sum, chi_sq, [<NUM_LIT:1>, <NUM_LIT:2>]).ravel()<EOL>return special.chdtrc(<NUM_LIT:1>, chi_sq)<EOL>

Two-way chi-square test of independence.
    Takes a 3D array as input: N(voxels) x 2 x 2, where the last two dimensions
    are the contingency table for each of N voxels. Returns an array of
    p-values.


term = data.astype('<STR_LIT>')<EOL>no_term = n - term<EOL>t_exp = np.mean(term, <NUM_LIT:0>)<EOL>t_exp = np.array([t_exp, ] * data.shape[<NUM_LIT:0>])<EOL>nt_exp = n - t_exp<EOL>t_mss = (term - t_exp) ** <NUM_LIT:2> / t_exp<EOL>nt_mss = (no_term - nt_exp) ** <NUM_LIT:2> / nt_exp<EOL>chi2 = t_mss + nt_mss<EOL>return special.chdtrc(<NUM_LIT:1>, chi2)<EOL>

One-way chi-square test of independence.
    Takes a 1D array as input and compares activation at each voxel to
    proportion expected under a uniform distribution throughout the array. Note
    that if you're testing activation with this, make sure that only valid
    voxels (e.g., in-mask gray matter voxels) are included in the array, or
    results won't make any sense!


s = np.sort(p)<EOL>nvox = p.shape[<NUM_LIT:0>]<EOL>null = np.array(range(<NUM_LIT:1>, nvox + <NUM_LIT:1>), dtype='<STR_LIT:float>') * q / nvox<EOL>below = np.where(s <= null)[<NUM_LIT:0>]<EOL>return s[max(below)] if len(below) else -<NUM_LIT:1><EOL>

Determine FDR threshold given a p value array and desired false
    discovery rate q.


def coactivation(dataset, seed, threshold=<NUM_LIT:0.0>, output_dir='<STR_LIT:.>', prefix='<STR_LIT>', r=<NUM_LIT:6>):

if isinstance(seed, string_types):<EOL><INDENT>ids = dataset.get_studies(mask=seed, activation_threshold=threshold)<EOL><DEDENT>else:<EOL><INDENT>ids = dataset.get_studies(peaks=seed, r=r,<EOL>activation_threshold=threshold)<EOL><DEDENT>ma = meta.MetaAnalysis(dataset, ids)<EOL>ma.save_results(output_dir, prefix)<EOL>

Compute and save coactivation map given input image as seed.

    This is essentially just a wrapper for a meta-analysis defined
    by the contrast between those studies that activate within the seed
    and those that don't.

    Args:
      dataset: a Dataset instance containing study and activation data.
      seed: either a Nifti or Analyze image defining the boundaries of the
        seed, or a list of triples (x/y/z) defining the seed(s). Note that
        voxels do not need to be contiguous to define a seed--all supra-
        threshold voxels will be lumped together.
      threshold: optional float indicating the threshold above which voxels
        are considered to be part of the seed ROI (default = 0)
      r: optional integer indicating radius (in mm) of spheres to grow
        (only used if seed is a list of coordinates).
      output_dir: output directory to write to. Defaults to current.
        If none, defaults to using the first part of the seed filename.
      prefix: optional string to prepend to all coactivation images.

    Output:
      A set of meta-analysis images identical to that generated by
      meta.MetaAnalysis.