```"""
=======================

"""

__author__ = ["Luke Chang", "Sam Greydanus"]

import os
import nibabel as nib
from nltools.prefs import MNI_Template, resolve_mni_path
import pandas as pd
import numpy as np
import warnings

"""Generate a set of spheres in the brain mask space

Args:
centers: a vector of sphere centers of the form [px, py, pz] or
[[px1, py1, pz1], ..., [pxn, pyn, pzn]]

"""
from nltools.data import Brain_Data

else:
raise ValueError(
"mask is not a nibabel instance or a valid " "file name"
)

else:

"""create a sphere of given radius at some point p in the brain mask

Args:
p: point (in coordinates of the brain mask) of the center of the
sphere

"""
m = [dims[0] / 2, dims[1] / 2, dims[2] / 2]
x, y, z = np.ogrid[
-m[0] : dims[0] - m[0], -m[1] : dims[1] - m[1], -m[2] : dims[2] - m[2]
]
mask_r = x * x + y * y + z * z <= r * r

activation = np.zeros(dims)
translation_affine = np.array(
[
[1, 0, 0, p[0] - m[0]],
[0, 1, 0, p[1] - m[1]],
[0, 0, 1, p[2] - m[2]],
[0, 0, 0, 1],
]
)

return nib.Nifti1Image(activation, affine=translation_affine)

if any(isinstance(i, list) for i in coordinates):
raise ValueError(
"Make sure length of radius list matches"
"length of coordinate list."
)
out = Brain_Data(
)
for r, c in zip(radius, coordinates):
else:
out = out.to_nifti()
out.get_data()[out.get_data() > 0.5] = 1
out.get_data()[out.get_data() < 0.5] = 0
return out

Args:
custom_mask: nibabel instance or string to file path; optional

Returns:
out: Brain_Data instance of multiple binary masks

"""

from nltools.data import Brain_Data

raise ValueError("Make sure mask is a nibabel or Brain_Data instance.")
tmp = []
out.data = np.array(tmp)
return out

overlapping areas are ignored

Args:
custom_mask: nibabel instance or string to file path; optional

Returns:
out: Brain_Data instance of a mask with different integers indicating

"""

from nltools.data import Brain_Data

else:
raise ValueError("Make sure mask is a nibabel or Brain_Data " "instance.")

# Create list of masks and find any overlaps
m_list = []
intersect = Brain_Data(
nib.Nifti1Image(np.abs(intersect.get_data() - 1), intersect.affine),
)

merge = []
if auto_label:
# Combine all masks into sequential order
# ignoring any areas of overlap
for i in range(len(m_list)):
merge.append(
np.multiply(
)
* (i + 1)
)
out.data = np.sum(np.array(merge).T, 1).astype(int)
else:
# Collapse masks using value as label
for i in range(len(m_list)):
merge.append(
np.multiply(
)
)
out.data = np.sum(np.array(merge).T, 1)
return out
else:
warnings.warn("Doesn't need to be collapased")

"""This function will create convert an expanded binary mask of ROIs
(see expand_mask) based on a vector of of values. The dataframe of values
must correspond to ROI numbers.

This is useful for populating a parcellation scheme by a vector of Values

Args:
data: Pandas series, dataframe, list, np.array of ROI by observation

Returns:
out: (Brain_Data) Brain_Data instance where each ROI is now populated
with a value
"""
from nltools.data import Brain_Data

if not isinstance(data, (pd.Series, pd.DataFrame)):
if isinstance(data, list):
data = pd.Series(data)
elif isinstance(data, np.ndarray):
if len(data.shape) == 1:
data = pd.Series(data)
elif len(data.shape) == 2:
data = pd.DataFrame(data)
data = data.T
else:
raise ValueError(
"Data must have the same number of rows as rois in mask"
)
else:
raise NotImplementedError

else:
raise ValueError("Data must be a pandas series or data frame.")

raise ValueError("Data must have the same number of rows as mask has ROIs.")

if isinstance(data, pd.Series):
out.data = np.zeros(out.data.shape)