_id stringlengths 2 7 | title stringlengths 1 88 | partition stringclasses 3
values | text stringlengths 75 19.8k | language stringclasses 1
value | meta_information dict |
|---|---|---|---|---|---|
q264300 | get_rcfile_section | validation | def get_rcfile_section(app_name, section_name):
""" Return the dictionary containing the rcfile section configuration
variables.
Parameters
----------
section_name: str
Name of the section in the rcfiles.
app_name: str
Name of the application to look for its rcfiles.
Returns
-------
settings: dict
Dict with variable values
"""
try:
settings = rcfile(app_name, section_name)
except IOError:
raise
except:
raise KeyError('Error looking for section {} in {} '
' rcfiles.'.format(section_name, app_name))
else:
return settings | python | {
"resource": ""
} |
q264301 | get_rcfile_variable_value | validation | def get_rcfile_variable_value(var_name, app_name, section_name=None):
""" Return the value of the variable in the section_name section of the
app_name rc file.
Parameters
----------
var_name: str
Name of the variable to be searched for.
section_name: str
Name of the section in the rcfiles.
app_name: str
Name of the application to look for its rcfiles.
Returns
-------
var_value: str
The value of the variable with given var_name.
"""
cfg = get_rcfile_section(app_name, section_name)
if var_name in cfg:
raise KeyError('Option {} not found in {} '
'section.'.format(var_name, section_name))
return cfg[var_name] | python | {
"resource": ""
} |
q264302 | find_in_sections | validation | def find_in_sections(var_name, app_name):
""" Return the section and the value of the variable where the first
var_name is found in the app_name rcfiles.
Parameters
----------
var_name: str
Name of the variable to be searched for.
app_name: str
Name of the application to look for its rcfiles.
Returns
-------
section_name: str
Name of the section in the rcfiles where var_name was first found.
var_value: str
The value of the first variable with given var_name.
"""
sections = get_sections(app_name)
if not sections:
raise ValueError('No sections found in {} rcfiles.'.format(app_name))
for s in sections:
try:
var_value = get_rcfile_variable_value(var_name, section_name=s,
app_name=app_name)
except:
pass
else:
return s, var_value
raise KeyError('No variable {} has been found in {} '
'rcfiles.'.format(var_name, app_name)) | python | {
"resource": ""
} |
q264303 | filter_list | validation | def filter_list(lst, pattern):
"""
Filters the lst using pattern.
If pattern starts with '(' it will be considered a re regular expression,
otherwise it will use fnmatch filter.
:param lst: list of strings
:param pattern: string
:return: list of strings
Filtered list of strings
"""
if is_fnmatch_regex(pattern) and not is_regex(pattern):
#use fnmatch
log.info('Using fnmatch for {0}'.format(pattern))
filst = fnmatch.filter(lst, pattern)
else:
#use re
log.info('Using regex match for {0}'.format(pattern))
filst = match_list(lst, pattern)
if filst:
filst.sort()
return filst | python | {
"resource": ""
} |
q264304 | get_subdict | validation | def get_subdict(adict, path, sep=os.sep):
"""
Given a nested dictionary adict.
This returns its childen just below the path.
The path is a string composed of adict keys separated by sep.
:param adict: nested dict
:param path: str
:param sep: str
:return: dict or list or leaf of treemap
"""
return reduce(adict.__class__.get, [p for p in op.split(sep) if p], adict) | python | {
"resource": ""
} |
q264305 | get_dict_leaves | validation | def get_dict_leaves(data):
"""
Given a nested dictionary, this returns all its leave elements in a list.
:param adict:
:return: list
"""
result = []
if isinstance(data, dict):
for item in data.values():
result.extend(get_dict_leaves(item))
elif isinstance(data, list):
result.extend(data)
else:
result.append(data)
return result | python | {
"resource": ""
} |
q264306 | get_possible_paths | validation | def get_possible_paths(base_path, path_regex):
"""
Looks for path_regex within base_path. Each match is append
in the returned list.
path_regex may contain subfolder structure.
If any part of the folder structure is a
:param base_path: str
:param path_regex: str
:return list of strings
"""
if not path_regex:
return []
if len(path_regex) < 1:
return []
if path_regex[0] == os.sep:
path_regex = path_regex[1:]
rest_files = ''
if os.sep in path_regex:
#split by os.sep
node_names = path_regex.partition(os.sep)
first_node = node_names[0]
rest_nodes = node_names[2]
folder_names = filter_list(os.listdir(base_path), first_node)
for nom in folder_names:
new_base = op.join(base_path, nom)
if op.isdir(new_base):
rest_files = get_possible_paths(new_base, rest_nodes)
else:
rest_files = filter_list(os.listdir(base_path), path_regex)
files = []
if rest_files:
files = [op.join(base_path, f) for f in rest_files]
return files | python | {
"resource": ""
} |
q264307 | FileTreeMap.create_folder | validation | def create_folder(dirpath, overwrite=False):
""" Will create dirpath folder. If dirpath already exists and overwrite is False,
will append a '+' suffix to dirpath until dirpath does not exist."""
if not overwrite:
while op.exists(dirpath):
dirpath += '+'
os.makedirs(dirpath, exist_ok=overwrite)
return dirpath | python | {
"resource": ""
} |
q264308 | FileTreeMap._import_config | validation | def _import_config(filepath):
"""
Imports filetree and root_path variable values from the filepath.
:param filepath:
:return: root_path and filetree
"""
if not op.isfile(filepath):
raise IOError('Data config file not found. '
'Got: {0}'.format(filepath))
cfg = import_pyfile(filepath)
if not hasattr(cfg, 'root_path'):
raise KeyError('Config file root_path key not found.')
if not hasattr(cfg, 'filetree'):
raise KeyError('Config file filetree key not found.')
return cfg.root_path, cfg.filetree | python | {
"resource": ""
} |
q264309 | FileTreeMap.remove_nodes | validation | def remove_nodes(self, pattern, adict):
"""
Remove the nodes that match the pattern.
"""
mydict = self._filetree if adict is None else adict
if isinstance(mydict, dict):
for nom in mydict.keys():
if isinstance(mydict[nom], dict):
matchs = filter_list(mydict[nom], pattern)
for nom in matchs:
mydict = self.remove_nodes(pattern, mydict[nom])
mydict.pop(nom)
else:
mydict[nom] = filter_list(mydict[nom], pattern)
else:
matchs = set(filter_list(mydict, pattern))
mydict = set(mydict) - matchs
return mydict | python | {
"resource": ""
} |
q264310 | FileTreeMap.count_node_match | validation | def count_node_match(self, pattern, adict=None):
"""
Return the number of nodes that match the pattern.
:param pattern:
:param adict:
:return: int
"""
mydict = self._filetree if adict is None else adict
k = 0
if isinstance(mydict, dict):
names = mydict.keys()
k += len(filter_list(names, pattern))
for nom in names:
k += self.count_node_match(pattern, mydict[nom])
else:
k = len(filter_list(mydict, pattern))
return k | python | {
"resource": ""
} |
q264311 | as_float_array | validation | def as_float_array(X, copy=True, force_all_finite=True):
"""Converts an array-like to an array of floats
The new dtype will be np.float32 or np.float64, depending on the original
type. The function can create a copy or modify the argument depending
on the argument copy.
Parameters
----------
X : {array-like, sparse matrix}
copy : bool, optional
If True, a copy of X will be created. If False, a copy may still be
returned if X's dtype is not a floating point type.
Returns
-------
XT : {array, sparse matrix}
An array of type np.float
"""
if isinstance(X, np.matrix) or (not isinstance(X, np.ndarray)
and not sp.issparse(X)):
return check_array(X, ['csr', 'csc', 'coo'], dtype=np.float64,
copy=copy, force_all_finite=force_all_finite,
ensure_2d=False)
elif sp.issparse(X) and X.dtype in [np.float32, np.float64]:
return X.copy() if copy else X
elif X.dtype in [np.float32, np.float64]: # is numpy array
return X.copy('F' if X.flags['F_CONTIGUOUS'] else 'C') if copy else X
else:
return X.astype(np.float32 if X.dtype == np.int32 else np.float64) | python | {
"resource": ""
} |
q264312 | indexable | validation | def indexable(*iterables):
"""Make arrays indexable for cross-validation.
Checks consistent length, passes through None, and ensures that everything
can be indexed by converting sparse matrices to csr and converting
non-interable objects to arrays.
Parameters
----------
iterables : lists, dataframes, arrays, sparse matrices
List of objects to ensure sliceability.
"""
result = []
for X in iterables:
if sp.issparse(X):
result.append(X.tocsr())
elif hasattr(X, "__getitem__") or hasattr(X, "iloc"):
result.append(X)
elif X is None:
result.append(X)
else:
result.append(np.array(X))
check_consistent_length(*result)
return result | python | {
"resource": ""
} |
q264313 | check_array | validation | def check_array(array, accept_sparse=None, dtype=None, order=None, copy=False,
force_all_finite=True, ensure_2d=True, allow_nd=False):
"""Input validation on an array, list, sparse matrix or similar.
By default, the input is converted to an at least 2nd numpy array.
Parameters
----------
array : object
Input object to check / convert.
accept_sparse : string, list of string or None (default=None)
String[s] representing allowed sparse matrix formats, such as 'csc',
'csr', etc. None means that sparse matrix input will raise an error.
If the input is sparse but not in the allowed format, it will be
converted to the first listed format.
order : 'F', 'C' or None (default=None)
Whether an array will be forced to be fortran or c-style.
copy : boolean (default=False)
Whether a forced copy will be triggered. If copy=False, a copy might
be triggered by a conversion.
force_all_finite : boolean (default=True)
Whether to raise an error on np.inf and np.nan in X.
ensure_2d : boolean (default=True)
Whether to make X at least 2d.
allow_nd : boolean (default=False)
Whether to allow X.ndim > 2.
Returns
-------
X_converted : object
The converted and validated X.
"""
if isinstance(accept_sparse, str):
accept_sparse = [accept_sparse]
if sp.issparse(array):
array = _ensure_sparse_format(array, accept_sparse, dtype, order,
copy, force_all_finite)
else:
if ensure_2d:
array = np.atleast_2d(array)
array = np.array(array, dtype=dtype, order=order, copy=copy)
if not allow_nd and array.ndim >= 3:
raise ValueError("Found array with dim %d. Expected <= 2" %
array.ndim)
if force_all_finite:
_assert_all_finite(array)
return array | python | {
"resource": ""
} |
q264314 | check_X_y | validation | def check_X_y(X, y, accept_sparse=None, dtype=None, order=None, copy=False,
force_all_finite=True, ensure_2d=True, allow_nd=False,
multi_output=False):
"""Input validation for standard estimators.
Checks X and y for consistent length, enforces X 2d and y 1d.
Standard input checks are only applied to y. For multi-label y,
set multi_ouput=True to allow 2d and sparse y.
Parameters
----------
X : nd-array, list or sparse matrix
Input data.
y : nd-array, list or sparse matrix
Labels.
accept_sparse : string, list of string or None (default=None)
String[s] representing allowed sparse matrix formats, such as 'csc',
'csr', etc. None means that sparse matrix input will raise an error.
If the input is sparse but not in the allowed format, it will be
converted to the first listed format.
order : 'F', 'C' or None (default=None)
Whether an array will be forced to be fortran or c-style.
copy : boolean (default=False)
Whether a forced copy will be triggered. If copy=False, a copy might
be triggered by a conversion.
force_all_finite : boolean (default=True)
Whether to raise an error on np.inf and np.nan in X.
ensure_2d : boolean (default=True)
Whether to make X at least 2d.
allow_nd : boolean (default=False)
Whether to allow X.ndim > 2.
Returns
-------
X_converted : object
The converted and validated X.
"""
X = check_array(X, accept_sparse, dtype, order, copy, force_all_finite,
ensure_2d, allow_nd)
if multi_output:
y = check_array(y, 'csr', force_all_finite=True, ensure_2d=False)
else:
y = column_or_1d(y, warn=True)
_assert_all_finite(y)
check_consistent_length(X, y)
return X, y | python | {
"resource": ""
} |
q264315 | column_or_1d | validation | def column_or_1d(y, warn=False):
""" Ravel column or 1d numpy array, else raises an error
Parameters
----------
y : array-like
Returns
-------
y : array
"""
shape = np.shape(y)
if len(shape) == 1:
return np.ravel(y)
if len(shape) == 2 and shape[1] == 1:
if warn:
warnings.warn("A column-vector y was passed when a 1d array was"
" expected. Please change the shape of y to "
"(n_samples, ), for example using ravel().",
DataConversionWarning, stacklevel=2)
return np.ravel(y)
raise ValueError("bad input shape {0}".format(shape)) | python | {
"resource": ""
} |
q264316 | warn_if_not_float | validation | def warn_if_not_float(X, estimator='This algorithm'):
"""Warning utility function to check that data type is floating point.
Returns True if a warning was raised (i.e. the input is not float) and
False otherwise, for easier input validation.
"""
if not isinstance(estimator, str):
estimator = estimator.__class__.__name__
if X.dtype.kind != 'f':
warnings.warn("%s assumes floating point values as input, "
"got %s" % (estimator, X.dtype))
return True
return False | python | {
"resource": ""
} |
q264317 | as_ndarray | validation | def as_ndarray(arr, copy=False, dtype=None, order='K'):
"""Convert an arbitrary array to numpy.ndarray.
In the case of a memmap array, a copy is automatically made to break the
link with the underlying file (whatever the value of the "copy" keyword).
The purpose of this function is mainly to get rid of memmap objects, but
it can be used for other purposes. In particular, combining copying and
casting can lead to performance improvements in some cases, by avoiding
unnecessary copies.
If not specified, input array order is preserved, in all cases, even when
a copy is requested.
Caveat: this function does not copy during bool to/from 1-byte dtype
conversions. This can lead to some surprising results in some rare cases.
Example:
a = numpy.asarray([0, 1, 2], dtype=numpy.int8)
b = as_ndarray(a, dtype=bool) # array([False, True, True], dtype=bool)
c = as_ndarray(b, dtype=numpy.int8) # array([0, 1, 2], dtype=numpy.int8)
The usually expected result for the last line would be array([0, 1, 1])
because True evaluates to 1. Since there is no copy made here, the original
array is recovered.
Parameters
----------
arr: array-like
input array. Any value accepted by numpy.asarray is valid.
copy: bool
if True, force a copy of the array. Always True when arr is a memmap.
dtype: any numpy dtype
dtype of the returned array. Performing copy and type conversion at the
same time can in some cases avoid an additional copy.
order: string
gives the order of the returned array.
Valid values are: "C", "F", "A", "K", None.
default is "K". See ndarray.copy() for more information.
Returns
-------
ret: np.ndarray
Numpy array containing the same data as arr, always of class
numpy.ndarray, and with no link to any underlying file.
"""
if order not in ('C', 'F', 'A', 'K', None):
raise ValueError("Invalid value for 'order': {}".format(str(order)))
if isinstance(arr, np.memmap):
if dtype is None:
if order in ('K', 'A', None):
ret = np.array(np.asarray(arr), copy=True)
else:
ret = np.array(np.asarray(arr), copy=True, order=order)
else:
if order in ('K', 'A', None):
# always copy (even when dtype does not change)
ret = np.asarray(arr).astype(dtype)
else:
# load data from disk without changing order
# Changing order while reading through a memmap is incredibly
# inefficient.
ret = _asarray(np.array(arr, copy=True), dtype=dtype, order=order)
elif isinstance(arr, np.ndarray):
ret = _asarray(arr, dtype=dtype, order=order)
# In the present cas, np.may_share_memory result is always reliable.
if np.may_share_memory(ret, arr) and copy:
# order-preserving copy
ret = ret.T.copy().T if ret.flags['F_CONTIGUOUS'] else ret.copy()
elif isinstance(arr, (list, tuple)):
if order in ("A", "K"):
ret = np.asarray(arr, dtype=dtype)
else:
ret = np.asarray(arr, dtype=dtype, order=order)
else:
raise ValueError("Type not handled: {}".format(arr.__class__))
return ret | python | {
"resource": ""
} |
q264318 | xfm_atlas_to_functional | validation | def xfm_atlas_to_functional(atlas_filepath, anatbrain_filepath, meanfunc_filepath,
atlas2anat_nonlin_xfm_filepath, is_atlas2anat_inverted,
anat2func_lin_xfm_filepath,
atlasinanat_out_filepath, atlasinfunc_out_filepath,
interp='nn', rewrite=True, parallel=False):
"""Call FSL tools to apply transformations to a given atlas to a functional image.
Given the transformation matrices.
Parameters
----------
atlas_filepath: str
Path to the 3D atlas volume file.
anatbrain_filepath: str
Path to the anatomical brain volume file (skull-stripped and registered to the same space as the atlas,
e.g., MNI).
meanfunc_filepath: str
Path to the average functional image to be used as reference in the last applywarp step.
atlas2anat_nonlin_xfm_filepath: str
Path to the atlas to anatomical brain linear transformation .mat file.
If you have the inverse transformation, i.e., anatomical brain to atlas, set is_atlas2anat_inverted to True.
is_atlas2anat_inverted: bool
If False will have to calculate the inverse atlas2anat transformation to apply the transformations.
This step will be performed with FSL invwarp.
anat2func_lin_xfm_filepath: str
Path to the anatomical to functional .mat linear transformation file.
atlasinanat_out_filepath: str
Path to output file which will contain the 3D atlas in the subject anatomical space.
atlasinfunc_out_filepath: str
Path to output file which will contain the 3D atlas in the subject functional space.
verbose: bool
If verbose will show DEBUG log info.
rewrite: bool
If True will re-run all the commands overwriting any existing file. Otherwise will check if
each file exists and if it does won't run the command.
parallel: bool
If True will launch the commands using ${FSLDIR}/fsl_sub to use the cluster infrastructure you have setup
with FSL (SGE or HTCondor).
"""
if is_atlas2anat_inverted:
# I already have the inverted fields I need
anat_to_mni_nl_inv = atlas2anat_nonlin_xfm_filepath
else:
# I am creating the inverted fields then...need output file path:
output_dir = op.abspath (op.dirname(atlasinanat_out_filepath))
ext = get_extension(atlas2anat_nonlin_xfm_filepath)
anat_to_mni_nl_inv = op.join(output_dir, remove_ext(op.basename(atlas2anat_nonlin_xfm_filepath)) + '_inv' + ext)
# setup the commands to be called
invwarp_cmd = op.join('${FSLDIR}', 'bin', 'invwarp')
applywarp_cmd = op.join('${FSLDIR}', 'bin', 'applywarp')
fslsub_cmd = op.join('${FSLDIR}', 'bin', 'fsl_sub')
# add fsl_sub before the commands
if parallel:
invwarp_cmd = fslsub_cmd + ' ' + invwarp_cmd
applywarp_cmd = fslsub_cmd + ' ' + applywarp_cmd
# create the inverse fields
if rewrite or (not is_atlas2anat_inverted and not op.exists(anat_to_mni_nl_inv)):
log.debug('Creating {}.\n'.format(anat_to_mni_nl_inv))
cmd = invwarp_cmd + ' '
cmd += '-w {} '.format(atlas2anat_nonlin_xfm_filepath)
cmd += '-o {} '.format(anat_to_mni_nl_inv)
cmd += '-r {} '.format(anatbrain_filepath)
log.debug('Running {}'.format(cmd))
check_call(cmd)
# transform the atlas to anatomical space
if rewrite or not op.exists(atlasinanat_out_filepath):
log.debug('Creating {}.\n'.format(atlasinanat_out_filepath))
cmd = applywarp_cmd + ' '
cmd += '--in={} '.format(atlas_filepath)
cmd += '--ref={} '.format(anatbrain_filepath)
cmd += '--warp={} '.format(anat_to_mni_nl_inv)
cmd += '--interp={} '.format(interp)
cmd += '--out={} '.format(atlasinanat_out_filepath)
log.debug('Running {}'.format(cmd))
check_call(cmd)
# transform the atlas to functional space
if rewrite or not op.exists(atlasinfunc_out_filepath):
log.debug('Creating {}.\n'.format(atlasinfunc_out_filepath))
cmd = applywarp_cmd + ' '
cmd += '--in={} '.format(atlasinanat_out_filepath)
cmd += '--ref={} '.format(meanfunc_filepath)
cmd += '--premat={} '.format(anat2func_lin_xfm_filepath)
cmd += '--interp={} '.format(interp)
cmd += '--out={} '.format(atlasinfunc_out_filepath)
log.debug('Running {}'.format(cmd))
check_call(cmd) | python | {
"resource": ""
} |
q264319 | fwhm2sigma | validation | def fwhm2sigma(fwhm):
"""Convert a FWHM value to sigma in a Gaussian kernel.
Parameters
----------
fwhm: float or numpy.array
fwhm value or values
Returns
-------
fwhm: float or numpy.array
sigma values
"""
fwhm = np.asarray(fwhm)
return fwhm / np.sqrt(8 * np.log(2)) | python | {
"resource": ""
} |
q264320 | sigma2fwhm | validation | def sigma2fwhm(sigma):
"""Convert a sigma in a Gaussian kernel to a FWHM value.
Parameters
----------
sigma: float or numpy.array
sigma value or values
Returns
-------
fwhm: float or numpy.array
fwhm values corresponding to `sigma` values
"""
sigma = np.asarray(sigma)
return np.sqrt(8 * np.log(2)) * sigma | python | {
"resource": ""
} |
q264321 | _smooth_data_array | validation | def _smooth_data_array(arr, affine, fwhm, copy=True):
"""Smooth images with a a Gaussian filter.
Apply a Gaussian filter along the three first dimensions of arr.
Parameters
----------
arr: numpy.ndarray
3D or 4D array, with image number as last dimension.
affine: numpy.ndarray
Image affine transformation matrix for image.
fwhm: scalar, numpy.ndarray
Smoothing kernel size, as Full-Width at Half Maximum (FWHM) in millimeters.
If a scalar is given, kernel width is identical on all three directions.
A numpy.ndarray must have 3 elements, giving the FWHM along each axis.
copy: bool
if True, will make a copy of the input array. Otherwise will directly smooth the input array.
Returns
-------
smooth_arr: numpy.ndarray
"""
if arr.dtype.kind == 'i':
if arr.dtype == np.int64:
arr = arr.astype(np.float64)
else:
arr = arr.astype(np.float32)
if copy:
arr = arr.copy()
# Zeroe possible NaNs and Inf in the image.
arr[np.logical_not(np.isfinite(arr))] = 0
try:
# Keep the 3D part of the affine.
affine = affine[:3, :3]
# Convert from FWHM in mm to a sigma.
fwhm_sigma_ratio = np.sqrt(8 * np.log(2))
vox_size = np.sqrt(np.sum(affine ** 2, axis=0))
sigma = fwhm / (fwhm_sigma_ratio * vox_size)
for n, s in enumerate(sigma):
ndimage.gaussian_filter1d(arr, s, output=arr, axis=n)
except:
raise ValueError('Error smoothing the array.')
else:
return arr | python | {
"resource": ""
} |
q264322 | smooth_imgs | validation | def smooth_imgs(images, fwhm):
"""Smooth images using a Gaussian filter.
Apply a Gaussian filter along the three first dimensions of each image in images.
In all cases, non-finite values in input are zeroed.
Parameters
----------
imgs: str or img-like object or iterable of img-like objects
See boyle.nifti.read.read_img
Image(s) to smooth.
fwhm: scalar or numpy.ndarray
Smoothing kernel size, as Full-Width at Half Maximum (FWHM) in millimeters.
If a scalar is given, kernel width is identical on all three directions.
A numpy.ndarray must have 3 elements, giving the FWHM along each axis.
Returns
-------
smooth_imgs: nibabel.Nifti1Image or list of.
Smooth input image/s.
"""
if fwhm <= 0:
return images
if not isinstance(images, string_types) and hasattr(images, '__iter__'):
only_one = False
else:
only_one = True
images = [images]
result = []
for img in images:
img = check_img(img)
affine = img.get_affine()
smooth = _smooth_data_array(img.get_data(), affine, fwhm=fwhm, copy=True)
result.append(nib.Nifti1Image(smooth, affine))
if only_one:
return result[0]
else:
return result | python | {
"resource": ""
} |
q264323 | _smooth_array | validation | def _smooth_array(arr, affine, fwhm=None, ensure_finite=True, copy=True, **kwargs):
"""Smooth images by applying a Gaussian filter.
Apply a Gaussian filter along the three first dimensions of arr.
This is copied and slightly modified from nilearn:
https://github.com/nilearn/nilearn/blob/master/nilearn/image/image.py
Added the **kwargs argument.
Parameters
==========
arr: numpy.ndarray
4D array, with image number as last dimension. 3D arrays are also
accepted.
affine: numpy.ndarray
(4, 4) matrix, giving affine transformation for image. (3, 3) matrices
are also accepted (only these coefficients are used).
If fwhm='fast', the affine is not used and can be None
fwhm: scalar, numpy.ndarray, 'fast' or None
Smoothing strength, as a full-width at half maximum, in millimeters.
If a scalar is given, width is identical on all three directions.
A numpy.ndarray must have 3 elements, giving the FWHM along each axis.
If fwhm == 'fast', a fast smoothing will be performed with
a filter [0.2, 1, 0.2] in each direction and a normalisation
to preserve the local average value.
If fwhm is None, no filtering is performed (useful when just removal
of non-finite values is needed).
ensure_finite: bool
if True, replace every non-finite values (like NaNs) by zero before
filtering.
copy: bool
if True, input array is not modified. False by default: the filtering
is performed in-place.
kwargs: keyword-arguments
Arguments for the ndimage.gaussian_filter1d function.
Returns
=======
filtered_arr: numpy.ndarray
arr, filtered.
Notes
=====
This function is most efficient with arr in C order.
"""
if arr.dtype.kind == 'i':
if arr.dtype == np.int64:
arr = arr.astype(np.float64)
else:
# We don't need crazy precision
arr = arr.astype(np.float32)
if copy:
arr = arr.copy()
if ensure_finite:
# SPM tends to put NaNs in the data outside the brain
arr[np.logical_not(np.isfinite(arr))] = 0
if fwhm == 'fast':
arr = _fast_smooth_array(arr)
elif fwhm is not None:
# Keep only the scale part.
affine = affine[:3, :3]
# Convert from a FWHM to a sigma:
fwhm_over_sigma_ratio = np.sqrt(8 * np.log(2))
vox_size = np.sqrt(np.sum(affine ** 2, axis=0))
sigma = fwhm / (fwhm_over_sigma_ratio * vox_size)
for n, s in enumerate(sigma):
ndimage.gaussian_filter1d(arr, s, output=arr, axis=n, **kwargs)
return arr | python | {
"resource": ""
} |
q264324 | smooth_img | validation | def smooth_img(imgs, fwhm, **kwargs):
"""Smooth images by applying a Gaussian filter.
Apply a Gaussian filter along the three first dimensions of arr.
In all cases, non-finite values in input image are replaced by zeros.
This is copied and slightly modified from nilearn:
https://github.com/nilearn/nilearn/blob/master/nilearn/image/image.py
Added the **kwargs argument.
Parameters
==========
imgs: Niimg-like object or iterable of Niimg-like objects
See http://nilearn.github.io/manipulating_images/manipulating_images.html#niimg.
Image(s) to smooth.
fwhm: scalar, numpy.ndarray, 'fast' or None
Smoothing strength, as a Full-Width at Half Maximum, in millimeters.
If a scalar is given, width is identical on all three directions.
A numpy.ndarray must have 3 elements, giving the FWHM along each axis.
If fwhm == 'fast', a fast smoothing will be performed with
a filter [0.2, 1, 0.2] in each direction and a normalisation
to preserve the scale.
If fwhm is None, no filtering is performed (useful when just removal
of non-finite values is needed)
Returns
=======
filtered_img: nibabel.Nifti1Image or list of.
Input image, filtered. If imgs is an iterable, then filtered_img is a
list.
"""
# Use hasattr() instead of isinstance to workaround a Python 2.6/2.7 bug
# See http://bugs.python.org/issue7624
if hasattr(imgs, "__iter__") \
and not isinstance(imgs, string_types):
single_img = False
else:
single_img = True
imgs = [imgs]
ret = []
for img in imgs:
img = check_niimg(img)
affine = img.get_affine()
filtered = _smooth_array(img.get_data(), affine, fwhm=fwhm,
ensure_finite=True, copy=True, **kwargs)
ret.append(new_img_like(img, filtered, affine, copy_header=True))
if single_img:
return ret[0]
else:
return ret | python | {
"resource": ""
} |
q264325 | ClientCertAuthentication.signed_session | validation | def signed_session(self, session=None):
"""Create requests session with any required auth headers
applied.
:rtype: requests.Session.
"""
if session:
session = super(ClientCertAuthentication, self).signed_session(session)
else:
session = super(ClientCertAuthentication, self).signed_session()
if self.cert is not None:
session.cert = self.cert
if self.ca_cert is not None:
session.verify = self.ca_cert
if self.no_verify:
session.verify = False
return session | python | {
"resource": ""
} |
q264326 | AdalAuthentication.signed_session | validation | def signed_session(self, session=None):
"""Create requests session with AAD auth headers
:rtype: requests.Session.
"""
from sfctl.config import (aad_metadata, aad_cache)
if session:
session = super(AdalAuthentication, self).signed_session(session)
else:
session = super(AdalAuthentication, self).signed_session()
if self.no_verify:
session.verify = False
authority_uri, cluster_id, client_id = aad_metadata()
existing_token, existing_cache = aad_cache()
context = adal.AuthenticationContext(authority_uri,
cache=existing_cache)
new_token = context.acquire_token(cluster_id,
existing_token['userId'], client_id)
header = "{} {}".format("Bearer", new_token['accessToken'])
session.headers['Authorization'] = header
return session | python | {
"resource": ""
} |
q264327 | voxspace_to_mmspace | validation | def voxspace_to_mmspace(img):
""" Return a grid with coordinates in 3D physical space for `img`."""
shape, affine = img.shape[:3], img.affine
coords = np.array(np.meshgrid(*(range(i) for i in shape), indexing='ij'))
coords = np.rollaxis(coords, 0, len(shape) + 1)
mm_coords = nib.affines.apply_affine(affine, coords)
return mm_coords | python | {
"resource": ""
} |
q264328 | get_3D_coordmap | validation | def get_3D_coordmap(img):
'''
Gets a 3D CoordinateMap from img.
Parameters
----------
img: nib.Nifti1Image or nipy Image
Returns
-------
nipy.core.reference.coordinate_map.CoordinateMap
'''
if isinstance(img, nib.Nifti1Image):
img = nifti2nipy(img)
if img.ndim == 4:
from nipy.core.reference.coordinate_map import drop_io_dim
cm = drop_io_dim(img.coordmap, 3)
else:
cm = img.coordmap
return cm | python | {
"resource": ""
} |
q264329 | get_img_info | validation | def get_img_info(image):
"""Return the header and affine matrix from a Nifti file.
Parameters
----------
image: img-like object or str
Can either be:
- a file path to a Nifti image
- any object with get_data() and get_affine() methods, e.g., nibabel.Nifti1Image.
If niimg is a string, consider it as a path to Nifti image and
call nibabel.load on it. If it is an object, check if get_data()
and get_affine() methods are present, raise TypeError otherwise.
Returns
-------
hdr, aff
"""
try:
img = check_img(image)
except Exception as exc:
raise Exception('Error reading file {0}.'.format(repr_imgs(image))) from exc
else:
return img.get_header(), img.get_affine() | python | {
"resource": ""
} |
q264330 | get_img_data | validation | def get_img_data(image, copy=True):
"""Return the voxel matrix of the Nifti file.
If safe_mode will make a copy of the img before returning the data, so the input image is not modified.
Parameters
----------
image: img-like object or str
Can either be:
- a file path to a Nifti image
- any object with get_data() and get_affine() methods, e.g., nibabel.Nifti1Image.
If niimg is a string, consider it as a path to Nifti image and
call nibabel.load on it. If it is an object, check if get_data()
and get_affine() methods are present, raise TypeError otherwise.
copy: bool
If safe_mode will make a copy of the img before returning the data, so the input image is not modified.
Returns
-------
array_like
"""
try:
img = check_img(image)
if copy:
return get_data(img)
else:
return img.get_data()
except Exception as exc:
raise Exception('Error when reading file {0}.'.format(repr_imgs(image))) from exc | python | {
"resource": ""
} |
q264331 | load_nipy_img | validation | def load_nipy_img(nii_file):
"""Read a Nifti file and return as nipy.Image
Parameters
----------
param nii_file: str
Nifti file path
Returns
-------
nipy.Image
"""
# delayed import because could not install nipy on Python 3 on OSX
import nipy
if not os.path.exists(nii_file):
raise FileNotFound(nii_file)
try:
return nipy.load_image(nii_file)
except Exception as exc:
raise Exception('Reading file {0}.'.format(repr_imgs(nii_file))) from exc | python | {
"resource": ""
} |
q264332 | niftilist_to_array | validation | def niftilist_to_array(img_filelist, outdtype=None):
"""
From the list of absolute paths to nifti files, creates a Numpy array
with the data.
Parameters
----------
img_filelist: list of str
List of absolute file paths to nifti files. All nifti files must have
the same shape.
outdtype: dtype
Type of the elements of the array, if not set will obtain the dtype from
the first nifti file.
Returns
-------
outmat: Numpy array with shape N x prod(vol.shape)
containing the N files as flat vectors.
vol_shape: Tuple with shape of the volumes, for reshaping.
"""
try:
first_img = img_filelist[0]
vol = get_img_data(first_img)
except IndexError as ie:
raise Exception('Error getting the first item of img_filelis: {}'.format(repr_imgs(img_filelist[0]))) from ie
if not outdtype:
outdtype = vol.dtype
outmat = np.zeros((len(img_filelist), np.prod(vol.shape)), dtype=outdtype)
try:
for i, img_file in enumerate(img_filelist):
vol = get_img_data(img_file)
outmat[i, :] = vol.flatten()
except Exception as exc:
raise Exception('Error on reading file {0}.'.format(img_file)) from exc
return outmat, vol.shape | python | {
"resource": ""
} |
q264333 | _crop_img_to | validation | def _crop_img_to(image, slices, copy=True):
"""Crops image to a smaller size
Crop img to size indicated by slices and modify the affine accordingly.
Parameters
----------
image: img-like object or str
Can either be:
- a file path to a Nifti image
- any object with get_data() and get_affine() methods, e.g., nibabel.Nifti1Image.
If niimg is a string, consider it as a path to Nifti image and
call nibabel.load on it. If it is an object, check if get_data()
and get_affine() methods are present, raise TypeError otherwise.
Image to be cropped.
slices: list of slices
Defines the range of the crop.
E.g. [slice(20, 200), slice(40, 150), slice(0, 100)]
defines a 3D cube
If slices has less entries than image has dimensions,
the slices will be applied to the first len(slices) dimensions.
copy: boolean
Specifies whether cropped data is to be copied or not.
Default: True
Returns
-------
cropped_img: img-like object
Cropped version of the input image
"""
img = check_img(image)
data = img.get_data()
affine = img.get_affine()
cropped_data = data[slices]
if copy:
cropped_data = cropped_data.copy()
linear_part = affine[:3, :3]
old_origin = affine[:3, 3]
new_origin_voxel = np.array([s.start for s in slices])
new_origin = old_origin + linear_part.dot(new_origin_voxel)
new_affine = np.eye(4)
new_affine[:3, :3] = linear_part
new_affine[:3, 3] = new_origin
new_img = nib.Nifti1Image(cropped_data, new_affine)
return new_img | python | {
"resource": ""
} |
q264334 | crop_img | validation | def crop_img(image, rtol=1e-8, copy=True):
"""Crops img as much as possible
Will crop img, removing as many zero entries as possible
without touching non-zero entries. Will leave one voxel of
zero padding around the obtained non-zero area in order to
avoid sampling issues later on.
Parameters
----------
image: img-like object or str
Can either be:
- a file path to a Nifti image
- any object with get_data() and get_affine() methods, e.g., nibabel.Nifti1Image.
If niimg is a string, consider it as a path to Nifti image and
call nibabel.load on it. If it is an object, check if get_data()
and get_affine() methods are present, raise TypeError otherwise.
Image to be cropped.
rtol: float
relative tolerance (with respect to maximal absolute
value of the image), under which values are considered
negligeable and thus croppable.
copy: boolean
Specifies whether cropped data is copied or not.
Returns
-------
cropped_img: image
Cropped version of the input image
"""
img = check_img(image)
data = img.get_data()
infinity_norm = max(-data.min(), data.max())
passes_threshold = np.logical_or(data < -rtol * infinity_norm,
data > rtol * infinity_norm)
if data.ndim == 4:
passes_threshold = np.any(passes_threshold, axis=-1)
coords = np.array(np.where(passes_threshold))
start = coords.min(axis=1)
end = coords.max(axis=1) + 1
# pad with one voxel to avoid resampling problems
start = np.maximum(start - 1, 0)
end = np.minimum(end + 1, data.shape[:3])
slices = [slice(s, e) for s, e in zip(start, end)]
return _crop_img_to(img, slices, copy=copy) | python | {
"resource": ""
} |
q264335 | new_img_like | validation | def new_img_like(ref_niimg, data, affine=None, copy_header=False):
"""Create a new image of the same class as the reference image
Parameters
----------
ref_niimg: image
Reference image. The new image will be of the same type.
data: numpy array
Data to be stored in the image
affine: 4x4 numpy array, optional
Transformation matrix
copy_header: boolean, optional
Indicated if the header of the reference image should be used to
create the new image
Returns
-------
new_img: image
A loaded image with the same type (and header) as the reference image.
"""
# Hand-written loading code to avoid too much memory consumption
if not (hasattr(ref_niimg, 'get_data')
and hasattr(ref_niimg,'get_affine')):
if isinstance(ref_niimg, _basestring):
ref_niimg = nib.load(ref_niimg)
elif operator.isSequenceType(ref_niimg):
ref_niimg = nib.load(ref_niimg[0])
else:
raise TypeError(('The reference image should be a niimg, %r '
'was passed') % ref_niimg )
if affine is None:
affine = ref_niimg.get_affine()
if data.dtype == bool:
default_dtype = np.int8
if (LooseVersion(nib.__version__) >= LooseVersion('1.2.0') and
isinstance(ref_niimg, nib.freesurfer.mghformat.MGHImage)):
default_dtype = np.uint8
data = as_ndarray(data, dtype=default_dtype)
header = None
if copy_header:
header = copy.copy(ref_niimg.get_header())
header['scl_slope'] = 0.
header['scl_inter'] = 0.
header['glmax'] = 0.
header['cal_max'] = np.max(data) if data.size > 0 else 0.
header['cal_max'] = np.min(data) if data.size > 0 else 0.
return ref_niimg.__class__(data, affine, header=header) | python | {
"resource": ""
} |
q264336 | get_h5file | validation | def get_h5file(file_path, mode='r'):
""" Return the h5py.File given its file path.
Parameters
----------
file_path: string
HDF5 file path
mode: string
r Readonly, file must exist
r+ Read/write, file must exist
w Create file, truncate if exists
w- Create file, fail if exists
a Read/write if exists, create otherwise (default)
Returns
-------
h5file: h5py.File
"""
if not op.exists(file_path):
raise IOError('Could not find file {}.'.format(file_path))
try:
h5file = h5py.File(file_path, mode=mode)
except:
raise
else:
return h5file | python | {
"resource": ""
} |
q264337 | extract_datasets | validation | def extract_datasets(h5file, h5path='/'):
""" Return all dataset contents from h5path group in h5file in an OrderedDict.
Parameters
----------
h5file: h5py.File
HDF5 file object
h5path: str
HDF5 group path to read datasets from
Returns
-------
datasets: OrderedDict
Dict with variables contained in file_path/h5path
"""
if isinstance(h5file, str):
_h5file = h5py.File(h5file, mode='r')
else:
_h5file = h5file
_datasets = get_datasets(_h5file, h5path)
datasets = OrderedDict()
try:
for ds in _datasets:
datasets[ds.name.split('/')[-1]] = ds[:]
except:
raise RuntimeError('Error reading datasets in {}/{}.'.format(_h5file.filename, h5path))
finally:
if isinstance(h5file, str):
_h5file.close()
return datasets | python | {
"resource": ""
} |
q264338 | _get_node_names | validation | def _get_node_names(h5file, h5path='/', node_type=h5py.Dataset):
"""Return the node of type node_type names within h5path of h5file.
Parameters
----------
h5file: h5py.File
HDF5 file object
h5path: str
HDF5 group path to get the group names from
node_type: h5py object type
HDF5 object type
Returns
-------
names: list of str
List of names
"""
if isinstance(h5file, str):
_h5file = get_h5file(h5file, mode='r')
else:
_h5file = h5file
if not h5path.startswith('/'):
h5path = '/' + h5path
names = []
try:
h5group = _h5file.require_group(h5path)
for node in _hdf5_walk(h5group, node_type=node_type):
names.append(node.name)
except:
raise RuntimeError('Error getting node names from {}/{}.'.format(_h5file.filename, h5path))
finally:
if isinstance(h5file, str):
_h5file.close()
return names | python | {
"resource": ""
} |
q264339 | NeuroImageSet.mask | validation | def mask(self, image):
""" self.mask setter
Parameters
----------
image: str or img-like object.
See NeuroImage constructor docstring.
"""
if image is None:
self._mask = None
try:
mask = load_mask(image)
except Exception as exc:
raise Exception('Could not load mask image {}.'.format(image)) from exc
else:
self._mask = mask | python | {
"resource": ""
} |
q264340 | NeuroImageSet._load_images_and_labels | validation | def _load_images_and_labels(self, images, labels=None):
"""Read the images, load them into self.items and set the labels."""
if not isinstance(images, (list, tuple)):
raise ValueError('Expected an iterable (list or tuple) of strings or img-like objects. '
'Got a {}.'.format(type(images)))
if not len(images) > 0:
raise ValueError('Expected an iterable (list or tuple) of strings or img-like objects '
'of size higher than 0. Got {} items.'.format(len(images)))
if labels is not None and len(labels) != len(images):
raise ValueError('Expected the same length for image set ({}) and '
'labels list ({}).'.format(len(images), len(labels)))
first_file = images[0]
if first_file:
first_img = NeuroImage(first_file)
else:
raise('Error reading image {}.'.format(repr_imgs(first_file)))
for idx, image in enumerate(images):
try:
img = NeuroImage(image)
self.check_compatibility(img, first_img)
except:
log.exception('Error reading image {}.'.format(repr_imgs(image)))
raise
else:
self.items.append(img)
self.set_labels(labels) | python | {
"resource": ""
} |
q264341 | NeuroImageSet.to_file | validation | def to_file(self, output_file, smooth_fwhm=0, outdtype=None):
"""Save the Numpy array created from to_matrix function to the output_file.
Will save into the file: outmat, mask_indices, vol_shape and self.others (put here whatever you want)
data: Numpy array with shape N x prod(vol.shape)
containing the N files as flat vectors.
mask_indices: matrix with indices of the voxels in the mask
vol_shape: Tuple with shape of the volumes, for reshaping.
Parameters
----------
output_file: str
Path to the output file. The extension of the file will be taken into account for the file format.
Choices of extensions: '.pyshelf' or '.shelf' (Python shelve)
'.mat' (Matlab archive),
'.hdf5' or '.h5' (HDF5 file)
smooth_fwhm: int
Integer indicating the size of the FWHM Gaussian smoothing kernel
to smooth the subject volumes before creating the data matrix
outdtype: dtype
Type of the elements of the array, if None will obtain the dtype from
the first nifti file.
"""
outmat, mask_indices, mask_shape = self.to_matrix(smooth_fwhm, outdtype)
exporter = ExportData()
content = {'data': outmat,
'labels': self.labels,
'mask_indices': mask_indices,
'mask_shape': mask_shape, }
if self.others:
content.update(self.others)
log.debug('Creating content in file {}.'.format(output_file))
try:
exporter.save_variables(output_file, content)
except Exception as exc:
raise Exception('Error saving variables to file {}.'.format(output_file)) from exc | python | {
"resource": ""
} |
q264342 | die | validation | def die(msg, code=-1):
"""Writes msg to stderr and exits with return code"""
sys.stderr.write(msg + "\n")
sys.exit(code) | python | {
"resource": ""
} |
q264343 | check_call | validation | def check_call(cmd_args):
"""
Calls the command
Parameters
----------
cmd_args: list of str
Command name to call and its arguments in a list.
Returns
-------
Command output
"""
p = subprocess.Popen(cmd_args, stdout=subprocess.PIPE)
(output, err) = p.communicate()
return output | python | {
"resource": ""
} |
q264344 | call_command | validation | def call_command(cmd_name, args_strings):
"""Call CLI command with arguments and returns its return value.
Parameters
----------
cmd_name: str
Command name or full path to the binary file.
arg_strings: list of str
Argument strings list.
Returns
-------
return_value
Command return value.
"""
if not op.isabs(cmd_name):
cmd_fullpath = which(cmd_name)
else:
cmd_fullpath = cmd_name
try:
cmd_line = [cmd_fullpath] + args_strings
log.info('Calling: {}.'.format(cmd_line))
retval = subprocess.check_call(cmd_line)
except CalledProcessError as ce:
log.exception("Error calling command {} with arguments: "
"{} \n With return code: {}".format(cmd_name, args_strings,
ce.returncode))
raise
else:
return retval | python | {
"resource": ""
} |
q264345 | condor_call | validation | def condor_call(cmd, shell=True):
"""
Tries to submit cmd to HTCondor, if it does not succeed, it will
be called with subprocess.call.
Parameters
----------
cmd: string
Command to be submitted
Returns
-------
"""
log.info(cmd)
ret = condor_submit(cmd)
if ret != 0:
subprocess.call(cmd, shell=shell) | python | {
"resource": ""
} |
q264346 | condor_submit | validation | def condor_submit(cmd):
"""
Submits cmd to HTCondor queue
Parameters
----------
cmd: string
Command to be submitted
Returns
-------
int
returncode value from calling the submission command.
"""
is_running = subprocess.call('condor_status', shell=True) == 0
if not is_running:
raise CalledProcessError('HTCondor is not running.')
sub_cmd = 'condor_qsub -shell n -b y -r y -N ' \
+ cmd.split()[0] + ' -m n'
log.info('Calling: ' + sub_cmd)
return subprocess.call(sub_cmd + ' ' + cmd, shell=True) | python | {
"resource": ""
} |
q264347 | clean | validation | def clean(ctx):
"""Clean previously built package artifacts.
"""
ctx.run(f'python setup.py clean')
dist = ROOT.joinpath('dist')
print(f'removing {dist}')
shutil.rmtree(str(dist)) | python | {
"resource": ""
} |
q264348 | upload | validation | def upload(ctx, repo):
"""Upload the package to an index server.
This implies cleaning and re-building the package.
:param repo: Required. Name of the index server to upload to, as specifies
in your .pypirc configuration file.
"""
artifacts = ' '.join(
shlex.quote(str(n))
for n in ROOT.joinpath('dist').glob('pipfile[-_]cli-*')
)
ctx.run(f'twine upload --repository="{repo}" {artifacts}') | python | {
"resource": ""
} |
q264349 | SFCommandLoader.load_command_table | validation | def load_command_table(self, args): #pylint: disable=too-many-statements
"""Load all Service Fabric commands"""
# Need an empty client for the select and upload operations
with CommandSuperGroup(__name__, self,
'rcctl.custom_cluster#{}') as super_group:
with super_group.group('cluster') as group:
group.command('select', 'select')
with CommandSuperGroup(__name__, self, 'rcctl.custom_reliablecollections#{}',
client_factory=client_create) as super_group:
with super_group.group('dictionary') as group:
group.command('query', 'query_reliabledictionary')
group.command('execute', 'execute_reliabledictionary')
group.command('schema', 'get_reliabledictionary_schema')
group.command('list', 'get_reliabledictionary_list')
group.command('type-schema', 'get_reliabledictionary_type_schema')
with ArgumentsContext(self, 'dictionary') as ac:
ac.argument('application_name', options_list=['--application-name', '-a'])
ac.argument('service_name', options_list=['--service-name', '-s'])
ac.argument('dictionary_name', options_list=['--dictionary-name', '-d'])
ac.argument('output_file', options_list=['--output-file', '-out'])
ac.argument('input_file', options_list=['--input-file', '-in'])
ac.argument('query_string', options_list=['--query-string', '-q'])
ac.argument('type_name', options_list=['--type-name', '-t'])
return OrderedDict(self.command_table) | python | {
"resource": ""
} |
q264350 | open_volume_file | validation | def open_volume_file(filepath):
"""Open a volumetric file using the tools following the file extension.
Parameters
----------
filepath: str
Path to a volume file
Returns
-------
volume_data: np.ndarray
Volume data
pixdim: 1xN np.ndarray
Vector with the description of the voxels physical size (usually in mm) for each volume dimension.
Raises
------
IOError
In case the file is not found.
"""
# check if the file exists
if not op.exists(filepath):
raise IOError('Could not find file {}.'.format(filepath))
# define helper functions
def open_nifti_file(filepath):
return NiftiImage(filepath)
def open_mhd_file(filepath):
return MedicalImage(filepath)
vol_data, hdr_data = load_raw_data_with_mhd(filepath)
# TODO: convert vol_data and hdr_data into MedicalImage
return vol_data, hdr_data
def open_mha_file(filepath):
raise NotImplementedError('This function has not been implemented yet.')
# generic loader function
def _load_file(filepath, loader):
return loader(filepath)
# file_extension -> file loader function
filext_loader = {
'nii': open_nifti_file,
'mhd': open_mhd_file,
'mha': open_mha_file,
}
# get extension of the `filepath`
ext = get_extension(filepath)
# find the loader from `ext`
loader = None
for e in filext_loader:
if ext in e:
loader = filext_loader[e]
if loader is None:
raise ValueError('Could not find a loader for file {}.'.format(filepath))
return _load_file(filepath, loader) | python | {
"resource": ""
} |
q264351 | rename_file_group_to_serial_nums | validation | def rename_file_group_to_serial_nums(file_lst):
"""Will rename all files in file_lst to a padded serial
number plus its extension
:param file_lst: list of path.py paths
"""
file_lst.sort()
c = 1
for f in file_lst:
dirname = get_abspath(f.dirname())
fdest = f.joinpath(dirname, "{0:04d}".format(c) +
OUTPUT_DICOM_EXTENSION)
log.info('Renaming {0} to {1}'.format(f, fdest))
f.rename(fdest)
c += 1 | python | {
"resource": ""
} |
q264352 | DicomFileSet._store_dicom_paths | validation | def _store_dicom_paths(self, folders):
"""Search for dicoms in folders and save file paths into
self.dicom_paths set.
:param folders: str or list of str
"""
if isinstance(folders, str):
folders = [folders]
for folder in folders:
if not os.path.exists(folder):
raise FolderNotFound(folder)
self.items.extend(list(find_all_dicom_files(folder))) | python | {
"resource": ""
} |
q264353 | DicomFileSet.from_set | validation | def from_set(self, fileset, check_if_dicoms=True):
"""Overwrites self.items with the given set of files.
Will filter the fileset and keep only Dicom files.
Parameters
----------
fileset: iterable of str
Paths to files
check_if_dicoms: bool
Whether to check if the items in fileset are dicom file paths
"""
if check_if_dicoms:
self.items = []
for f in fileset:
if is_dicom_file(f):
self.items.append(f)
else:
self.items = fileset | python | {
"resource": ""
} |
q264354 | DicomFileSet.update | validation | def update(self, dicomset):
"""Update this set with the union of itself and dicomset.
Parameters
----------
dicomset: DicomFileSet
"""
if not isinstance(dicomset, DicomFileSet):
raise ValueError('Given dicomset is not a DicomFileSet.')
self.items = list(set(self.items).update(dicomset)) | python | {
"resource": ""
} |
q264355 | DicomFileSet.copy_files_to_other_folder | validation | def copy_files_to_other_folder(self, output_folder, rename_files=True,
mkdir=True, verbose=False):
"""
Copies all files within this set to the output_folder
Parameters
----------
output_folder: str
Path of the destination folder of the files
rename_files: bool
Whether or not rename the files to a sequential format
mkdir: bool
Whether to make the folder if it does not exist
verbose: bool
Whether to print to stdout the files that are beind copied
"""
import shutil
if not os.path.exists(output_folder):
os.mkdir(output_folder)
if not rename_files:
for dcmf in self.items:
outf = os.path.join(output_folder, os.path.basename(dcmf))
if verbose:
print('{} -> {}'.format(dcmf, outf))
shutil.copyfile(dcmf, outf)
else:
n_pad = len(self.items)+2
for idx, dcmf in enumerate(self.items):
outf = '{number:0{width}d}.dcm'.format(width=n_pad, number=idx)
outf = os.path.join(output_folder, outf)
if verbose:
print('{} -> {}'.format(dcmf, outf))
shutil.copyfile(dcmf, outf) | python | {
"resource": ""
} |
q264356 | DicomGenericSet.get_dcm_reader | validation | def get_dcm_reader(store_metadata=True, header_fields=None):
"""
Creates a lambda function to read DICOM files.
If store_store_metadata is False, will only return the file path.
Else if you give header_fields, will return only the set of of
header_fields within a DicomFile object or the whole DICOM file if
None.
:return: function
This function has only one parameter: file_path
"""
if not store_metadata:
return lambda fpath: fpath
if header_fields is None:
build_dcm = lambda fpath: DicomFile(fpath)
else:
dicom_header = namedtuple('DicomHeader', header_fields)
build_dcm = lambda fpath: dicom_header._make(DicomFile(fpath).get_attributes(header_fields))
return build_dcm | python | {
"resource": ""
} |
q264357 | DicomGenericSet.scrape_all_files | validation | def scrape_all_files(self):
"""
Generator that yields one by one the return value for self.read_dcm
for each file within this set
"""
try:
for dcmf in self.items:
yield self.read_dcm(dcmf)
except IOError as ioe:
raise IOError('Error reading DICOM file: {}.'.format(dcmf)) from ioe | python | {
"resource": ""
} |
q264358 | get_unique_field_values | validation | def get_unique_field_values(dcm_file_list, field_name):
"""Return a set of unique field values from a list of DICOM files
Parameters
----------
dcm_file_list: iterable of DICOM file paths
field_name: str
Name of the field from where to get each value
Returns
-------
Set of field values
"""
field_values = set()
for dcm in dcm_file_list:
field_values.add(str(DicomFile(dcm).get_attributes(field_name)))
return field_values | python | {
"resource": ""
} |
q264359 | find_all_dicom_files | validation | def find_all_dicom_files(root_path):
"""
Returns a list of the dicom files within root_path
Parameters
----------
root_path: str
Path to the directory to be recursively searched for DICOM files.
Returns
-------
dicoms: set
Set of DICOM absolute file paths
"""
dicoms = set()
try:
for fpath in get_all_files(root_path):
if is_dicom_file(fpath):
dicoms.add(fpath)
except IOError as ioe:
raise IOError('Error reading file {0}.'.format(fpath)) from ioe
return dicoms | python | {
"resource": ""
} |
q264360 | is_dicom_file | validation | def is_dicom_file(filepath):
"""
Tries to read the file using dicom.read_file,
if the file exists and dicom.read_file does not raise
and Exception returns True. False otherwise.
:param filepath: str
Path to DICOM file
:return: bool
"""
if not os.path.exists(filepath):
raise IOError('File {} not found.'.format(filepath))
filename = os.path.basename(filepath)
if filename == 'DICOMDIR':
return False
try:
_ = dicom.read_file(filepath)
except Exception as exc:
log.debug('Checking if {0} was a DICOM, but returned '
'False.'.format(filepath))
return False
return True | python | {
"resource": ""
} |
q264361 | group_dicom_files | validation | def group_dicom_files(dicom_paths, hdr_field='PatientID'):
"""Group in a dictionary all the DICOM files in dicom_paths
separated by the given `hdr_field` tag value.
Parameters
----------
dicom_paths: str
Iterable of DICOM file paths.
hdr_field: str
Name of the DICOM tag whose values will be used as key for the group.
Returns
-------
dicom_groups: dict of dicom_paths
"""
dicom_groups = defaultdict(list)
try:
for dcm in dicom_paths:
hdr = dicom.read_file(dcm)
group_key = getattr(hdr, hdr_field)
dicom_groups[group_key].append(dcm)
except KeyError as ke:
raise KeyError('Error reading field {} from file {}.'.format(hdr_field, dcm)) from ke
return dicom_groups | python | {
"resource": ""
} |
q264362 | DicomFile.get_attributes | validation | def get_attributes(self, attributes, default=''):
"""Return the attributes values from this DicomFile
Parameters
----------
attributes: str or list of str
DICOM field names
default: str
Default value if the attribute does not exist.
Returns
-------
Value of the field or list of values.
"""
if isinstance(attributes, str):
attributes = [attributes]
attrs = [getattr(self, attr, default) for attr in attributes]
if len(attrs) == 1:
return attrs[0]
return tuple(attrs) | python | {
"resource": ""
} |
q264363 | merge_images | validation | def merge_images(images, axis='t'):
""" Concatenate `images` in the direction determined in `axis`.
Parameters
----------
images: list of str or img-like object.
See NeuroImage constructor docstring.
axis: str
't' : concatenate images in time
'x' : concatenate images in the x direction
'y' : concatenate images in the y direction
'z' : concatenate images in the z direction
Returns
-------
merged: img-like object
"""
# check if images is not empty
if not images:
return None
# the given axis name to axis idx
axis_dim = {'x': 0,
'y': 1,
'z': 2,
't': 3,
}
# check if the given axis name is valid
if axis not in axis_dim:
raise ValueError('Expected `axis` to be one of ({}), got {}.'.format(set(axis_dim.keys()), axis))
# check if all images are compatible with each other
img1 = images[0]
for img in images:
check_img_compatibility(img1, img)
# read the data of all the given images
# TODO: optimize memory consumption by merging one by one.
image_data = []
for img in images:
image_data.append(check_img(img).get_data())
# if the work_axis is bigger than the number of axis of the images,
# create a new axis for the images
work_axis = axis_dim[axis]
ndim = image_data[0].ndim
if ndim - 1 < work_axis:
image_data = [np.expand_dims(img, axis=work_axis) for img in image_data]
# concatenate and return
return np.concatenate(image_data, axis=work_axis) | python | {
"resource": ""
} |
q264364 | nifti_out | validation | def nifti_out(f):
""" Picks a function whose first argument is an `img`, processes its
data and returns a numpy array. This decorator wraps this numpy array
into a nibabel.Nifti1Image."""
@wraps(f)
def wrapped(*args, **kwargs):
r = f(*args, **kwargs)
img = read_img(args[0])
return nib.Nifti1Image(r, affine=img.get_affine(), header=img.header)
return wrapped | python | {
"resource": ""
} |
q264365 | div_img | validation | def div_img(img1, div2):
""" Pixelwise division or divide by a number """
if is_img(div2):
return img1.get_data()/div2.get_data()
elif isinstance(div2, (float, int)):
return img1.get_data()/div2
else:
raise NotImplementedError('Cannot divide {}({}) by '
'{}({})'.format(type(img1),
img1,
type(div2),
div2)) | python | {
"resource": ""
} |
q264366 | apply_mask | validation | def apply_mask(img, mask):
"""Return the image with the given `mask` applied."""
from .mask import apply_mask
vol, _ = apply_mask(img, mask)
return vector_to_volume(vol, read_img(mask).get_data().astype(bool)) | python | {
"resource": ""
} |
q264367 | abs_img | validation | def abs_img(img):
""" Return an image with the binarised version of the data of `img`."""
bool_img = np.abs(read_img(img).get_data())
return bool_img.astype(int) | python | {
"resource": ""
} |
q264368 | icc_img_to_zscore | validation | def icc_img_to_zscore(icc, center_image=False):
""" Return a z-scored version of `icc`.
This function is based on GIFT `icatb_convertImageToZScores` function.
"""
vol = read_img(icc).get_data()
v2 = vol[vol != 0]
if center_image:
v2 = detrend(v2, axis=0)
vstd = np.linalg.norm(v2, ord=2) / np.sqrt(np.prod(v2.shape) - 1)
eps = np.finfo(vstd.dtype).eps
vol /= (eps + vstd)
return vol | python | {
"resource": ""
} |
q264369 | spatial_map | validation | def spatial_map(icc, thr, mode='+'):
""" Return the thresholded z-scored `icc`. """
return thr_img(icc_img_to_zscore(icc), thr=thr, mode=mode).get_data() | python | {
"resource": ""
} |
q264370 | write_meta_header | validation | def write_meta_header(filename, meta_dict):
""" Write the content of the `meta_dict` into `filename`.
Parameters
----------
filename: str
Path to the output file
meta_dict: dict
Dictionary with the fields of the metadata .mhd file
"""
header = ''
# do not use tags = meta_dict.keys() because the order of tags matters
for tag in MHD_TAGS:
if tag in meta_dict.keys():
header += '{} = {}\n'.format(tag, meta_dict[tag])
with open(filename, 'w') as f:
f.write(header) | python | {
"resource": ""
} |
q264371 | dump_raw_data | validation | def dump_raw_data(filename, data):
""" Write the data into a raw format file. Big endian is always used.
Parameters
----------
filename: str
Path to the output file
data: numpy.ndarray
n-dimensional image data array.
"""
if data.ndim == 3:
# Begin 3D fix
data = data.reshape([data.shape[0], data.shape[1]*data.shape[2]])
# End 3D fix
a = array.array('f')
for o in data:
a.fromlist(list(o.flatten()))
# if is_little_endian():
# a.byteswap()
with open(filename, 'wb') as rawf:
a.tofile(rawf) | python | {
"resource": ""
} |
q264372 | write_mhd_file | validation | def write_mhd_file(filename, data, shape=None, meta_dict=None):
""" Write the `data` and `meta_dict` in two files with names
that use `filename` as a prefix.
Parameters
----------
filename: str
Path to the output file.
This is going to be used as a preffix.
Two files will be created, one with a '.mhd' extension
and another with '.raw'. If `filename` has any of these already
they will be taken into account to build the filenames.
data: numpy.ndarray
n-dimensional image data array.
shape: tuple
Tuple describing the shape of `data`
Default: data.shape
meta_dict: dict
Dictionary with the fields of the metadata .mhd file
Default: {}
Returns
-------
mhd_filename: str
Path to the .mhd file
raw_filename: str
Path to the .raw file
"""
# check its extension
ext = get_extension(filename)
fname = op.basename(filename)
if ext != '.mhd' or ext != '.raw':
mhd_filename = fname + '.mhd'
raw_filename = fname + '.raw'
elif ext == '.mhd':
mhd_filename = fname
raw_filename = remove_ext(fname) + '.raw'
elif ext == '.raw':
mhd_filename = remove_ext(fname) + '.mhd'
raw_filename = fname
else:
raise ValueError('`filename` extension {} from {} is not recognised. '
'Expected .mhd or .raw.'.format(ext, filename))
# default values
if meta_dict is None:
meta_dict = {}
if shape is None:
shape = data.shape
# prepare the default header
meta_dict['ObjectType'] = meta_dict.get('ObjectType', 'Image')
meta_dict['BinaryData'] = meta_dict.get('BinaryData', 'True' )
meta_dict['BinaryDataByteOrderMSB'] = meta_dict.get('BinaryDataByteOrderMSB', 'False')
meta_dict['ElementType'] = meta_dict.get('ElementType', NUMPY_TO_MHD_TYPE[data.dtype.type])
meta_dict['NDims'] = meta_dict.get('NDims', str(len(shape)))
meta_dict['DimSize'] = meta_dict.get('DimSize', ' '.join([str(i) for i in shape]))
meta_dict['ElementDataFile'] = meta_dict.get('ElementDataFile', raw_filename)
# target files
mhd_filename = op.join(op.dirname(filename), mhd_filename)
raw_filename = op.join(op.dirname(filename), raw_filename)
# write the header
write_meta_header(mhd_filename, meta_dict)
# write the data
dump_raw_data(raw_filename, data)
return mhd_filename, raw_filename | python | {
"resource": ""
} |
q264373 | copy_mhd_and_raw | validation | def copy_mhd_and_raw(src, dst):
"""Copy .mhd and .raw files to dst.
If dst is a folder, won't change the file, but if dst is another filepath,
will modify the ElementDataFile field in the .mhd to point to the
new renamed .raw file.
Parameters
----------
src: str
Path to the .mhd file to be copied
dst: str
Path to the destination of the .mhd and .raw files.
If a new file name is given, the extension will be ignored.
Returns
-------
dst: str
"""
# check if src exists
if not op.exists(src):
raise IOError('Could not find file {}.'.format(src))
# check its extension
ext = get_extension(src)
if ext != '.mhd':
msg = 'The src file path must be a .mhd file. Given: {}.'.format(src)
raise ValueError(msg)
# get the raw file for this src mhd file
meta_src = _read_meta_header(src)
# get the source raw file
src_raw = meta_src['ElementDataFile']
if not op.isabs(src_raw):
src_raw = op.join(op.dirname(src), src_raw)
# check if dst is dir
if op.isdir(dst):
# copy the mhd and raw file to its destiny
shutil.copyfile(src, dst)
shutil.copyfile(src_raw, dst)
return dst
# build raw file dst file name
dst_raw = op.join(op.dirname(dst), remove_ext(op.basename(dst))) + '.raw'
# add extension to the dst path
if get_extension(dst) != '.mhd':
dst += '.mhd'
# copy the mhd and raw file to its destiny
log.debug('cp: {} -> {}'.format(src, dst))
log.debug('cp: {} -> {}'.format(src_raw, dst_raw))
shutil.copyfile(src, dst)
shutil.copyfile(src_raw, dst_raw)
# check if src file name is different than dst file name
# if not the same file name, change the content of the ElementDataFile field
if op.basename(dst) != op.basename(src):
log.debug('modify {}: ElementDataFile: {} -> {}'.format(dst, src_raw,
op.basename(dst_raw)))
meta_dst = _read_meta_header(dst)
meta_dst['ElementDataFile'] = op.basename(dst_raw)
write_meta_header(dst, meta_dst)
return dst | python | {
"resource": ""
} |
q264374 | sav_to_pandas_rpy2 | validation | def sav_to_pandas_rpy2(input_file):
"""
SPSS .sav files to Pandas DataFrame through Rpy2
:param input_file: string
:return:
"""
import pandas.rpy.common as com
w = com.robj.r('foreign::read.spss("%s", to.data.frame=TRUE)' % input_file)
return com.convert_robj(w) | python | {
"resource": ""
} |
q264375 | sav_to_pandas_savreader | validation | def sav_to_pandas_savreader(input_file):
"""
SPSS .sav files to Pandas DataFrame through savreader module
:param input_file: string
:return:
"""
from savReaderWriter import SavReader
lines = []
with SavReader(input_file, returnHeader=True) as reader:
header = next(reader)
for line in reader:
lines.append(line)
return pd.DataFrame(data=lines, columns=header) | python | {
"resource": ""
} |
q264376 | ExportData.save_varlist | validation | def save_varlist(filename, varnames, varlist):
"""
Valid extensions '.pyshelf', '.mat', '.hdf5' or '.h5'
@param filename: string
@param varnames: list of strings
Names of the variables
@param varlist: list of objects
The objects to be saved
"""
variables = {}
for i, vn in enumerate(varnames):
variables[vn] = varlist[i]
ExportData.save_variables(filename, variables) | python | {
"resource": ""
} |
q264377 | cli | validation | def cli():
"""Create CLI environment"""
return VersionedCLI(cli_name=SF_CLI_NAME,
config_dir=SF_CLI_CONFIG_DIR,
config_env_var_prefix=SF_CLI_ENV_VAR_PREFIX,
commands_loader_cls=SFCommandLoader,
help_cls=SFCommandHelp) | python | {
"resource": ""
} |
q264378 | drain_rois | validation | def drain_rois(img):
"""Find all the ROIs in img and returns a similar volume with the ROIs
emptied, keeping only their border voxels.
This is useful for DTI tractography.
Parameters
----------
img: img-like object or str
Can either be:
- a file path to a Nifti image
- any object with get_data() and get_affine() methods, e.g., nibabel.Nifti1Image.
If niimg is a string, consider it as a path to Nifti image and
call nibabel.load on it. If it is an object, check if get_data()
and get_affine() methods are present, raise TypeError otherwise.
Returns
-------
np.ndarray
an array of same shape as img_data
"""
img_data = get_img_data(img)
out = np.zeros(img_data.shape, dtype=img_data.dtype)
krn_dim = [3] * img_data.ndim
kernel = np.ones(krn_dim, dtype=int)
vals = np.unique(img_data)
vals = vals[vals != 0]
for i in vals:
roi = img_data == i
hits = scn.binary_hit_or_miss(roi, kernel)
roi[hits] = 0
out[roi > 0] = i
return out | python | {
"resource": ""
} |
q264379 | largest_connected_component | validation | def largest_connected_component(volume):
"""Return the largest connected component of a 3D array.
Parameters
-----------
volume: numpy.array
3D boolean array.
Returns
--------
volume: numpy.array
3D boolean array with only one connected component.
"""
# We use asarray to be able to work with masked arrays.
volume = np.asarray(volume)
labels, num_labels = scn.label(volume)
if not num_labels:
raise ValueError('No non-zero values: no connected components found.')
if num_labels == 1:
return volume.astype(np.bool)
label_count = np.bincount(labels.ravel().astype(np.int))
# discard the 0 label
label_count[0] = 0
return labels == label_count.argmax() | python | {
"resource": ""
} |
q264380 | large_clusters_mask | validation | def large_clusters_mask(volume, min_cluster_size):
""" Return as mask for `volume` that includes only areas where
the connected components have a size bigger than `min_cluster_size`
in number of voxels.
Parameters
-----------
volume: numpy.array
3D boolean array.
min_cluster_size: int
Minimum size in voxels that the connected component must have.
Returns
--------
volume: numpy.array
3D int array with a mask excluding small connected components.
"""
labels, num_labels = scn.label(volume)
labels_to_keep = set([i for i in range(num_labels)
if np.sum(labels == i) >= min_cluster_size])
clusters_mask = np.zeros_like(volume, dtype=int)
for l in range(num_labels):
if l in labels_to_keep:
clusters_mask[labels == l] = 1
return clusters_mask | python | {
"resource": ""
} |
q264381 | create_rois_mask | validation | def create_rois_mask(roislist, filelist):
"""Look for the files in filelist containing the names in roislist, these files will be opened, binarised
and merged in one mask.
Parameters
----------
roislist: list of strings
Names of the ROIs, which will have to be in the names of the files in filelist.
filelist: list of strings
List of paths to the volume files containing the ROIs.
Returns
-------
numpy.ndarray
Mask volume
"""
roifiles = []
for roi in roislist:
try:
roi_file = search_list(roi, filelist)[0]
except Exception as exc:
raise Exception('Error creating list of roi files. \n {}'.format(str(exc)))
else:
roifiles.append(roi_file)
return binarise(roifiles) | python | {
"resource": ""
} |
q264382 | get_unique_nonzeros | validation | def get_unique_nonzeros(arr):
""" Return a sorted list of the non-zero unique values of arr.
Parameters
----------
arr: numpy.ndarray
The data array
Returns
-------
list of items of arr.
"""
rois = np.unique(arr)
rois = rois[np.nonzero(rois)]
rois.sort()
return rois | python | {
"resource": ""
} |
q264383 | get_rois_centers_of_mass | validation | def get_rois_centers_of_mass(vol):
"""Get the center of mass for each ROI in the given volume.
Parameters
----------
vol: numpy ndarray
Volume with different values for each ROI.
Returns
-------
OrderedDict
Each entry in the dict has the ROI value as key and the center_of_mass coordinate as value.
"""
from scipy.ndimage.measurements import center_of_mass
roisvals = np.unique(vol)
roisvals = roisvals[roisvals != 0]
rois_centers = OrderedDict()
for r in roisvals:
rois_centers[r] = center_of_mass(vol, vol, r)
return rois_centers | python | {
"resource": ""
} |
q264384 | _partition_data | validation | def _partition_data(datavol, roivol, roivalue, maskvol=None, zeroe=True):
""" Extracts the values in `datavol` that are in the ROI with value `roivalue` in `roivol`.
The ROI can be masked by `maskvol`.
Parameters
----------
datavol: numpy.ndarray
4D timeseries volume or a 3D volume to be partitioned
roivol: numpy.ndarray
3D ROIs volume
roivalue: int or float
A value from roivol that represents the ROI to be used for extraction.
maskvol: numpy.ndarray
3D mask volume
zeroe: bool
If true will remove the null timeseries voxels. Only applied to timeseries (4D) data.
Returns
-------
values: np.array
An array of the values in the indicated ROI.
A 2D matrix if `datavol` is 4D or a 1D vector if `datavol` is 3D.
"""
if maskvol is not None:
# get all masked time series within this roi r
indices = (roivol == roivalue) * (maskvol > 0)
else:
# get all time series within this roi r
indices = roivol == roivalue
if datavol.ndim == 4:
ts = datavol[indices, :]
else:
ts = datavol[indices]
# remove zeroed time series
if zeroe:
if datavol.ndim == 4:
ts = ts[ts.sum(axis=1) != 0, :]
return ts | python | {
"resource": ""
} |
q264385 | get_3D_from_4D | validation | def get_3D_from_4D(image, vol_idx=0):
"""Pick one 3D volume from a 4D nifti image file
Parameters
----------
image: img-like object or str
Volume defining different ROIs.
Can either be:
- a file path to a Nifti image
- any object with get_data() and get_affine() methods, e.g., nibabel.Nifti1Image.
If niimg is a string, consider it as a path to Nifti image and
call nibabel.load on it. If it is an object, check if get_data()
and get_affine() methods are present, raise TypeError otherwise.
vol_idx: int
Index of the 3D volume to be extracted from the 4D volume.
Returns
-------
vol, hdr, aff
The data array, the image header and the affine transform matrix.
"""
img = check_img(image)
hdr, aff = get_img_info(img)
if len(img.shape) != 4:
raise AttributeError('Volume in {} does not have 4 dimensions.'.format(repr_imgs(img)))
if not 0 <= vol_idx < img.shape[3]:
raise IndexError('IndexError: 4th dimension in volume {} has {} volumes, '
'not {}.'.format(repr_imgs(img), img.shape[3], vol_idx))
img_data = img.get_data()
new_vol = img_data[:, :, :, vol_idx].copy()
hdr.set_data_shape(hdr.get_data_shape()[:3])
return new_vol, hdr, aff | python | {
"resource": ""
} |
q264386 | HdfDataBuffer.get_dataset | validation | def get_dataset(self, ds_name, mode='r'):
"""
Returns a h5py dataset given its registered name.
:param ds_name: string
Name of the dataset to be returned.
:return:
"""
if ds_name in self._datasets:
return self._datasets[ds_name]
else:
return self.create_empty_dataset(ds_name) | python | {
"resource": ""
} |
q264387 | HdfDataBuffer.create_empty_dataset | validation | def create_empty_dataset(self, ds_name, dtype=np.float32):
"""
Creates a Dataset with unknown size.
Resize it before using.
:param ds_name: string
:param dtype: dtype
Datatype of the dataset
:return: h5py DataSet
"""
if ds_name in self._datasets:
return self._datasets[ds_name]
ds = self._group.create_dataset(ds_name, (1, 1), maxshape=None,
dtype=dtype)
self._datasets[ds_name] = ds
return ds | python | {
"resource": ""
} |
q264388 | HdfDataBuffer.create_dataset | validation | def create_dataset(self, ds_name, data, attrs=None, dtype=None):
"""
Saves a Numpy array in a dataset in the HDF file, registers it as
ds_name and returns the h5py dataset.
:param ds_name: string
Registration name of the dataset to be registered.
:param data: Numpy ndarray
:param dtype: dtype
Datatype of the dataset
:return: h5py dataset
"""
if ds_name in self._datasets:
ds = self._datasets[ds_name]
if ds.dtype != data.dtype:
warnings.warn('Dataset and data dtype are different!')
else:
if dtype is None:
dtype = data.dtype
ds = self._group.create_dataset(ds_name, data.shape,
dtype=dtype)
if attrs is not None:
for key in attrs:
setattr(ds.attrs, key, attrs[key])
ds.read_direct(data)
self._datasets[ds_name] = ds
return ds | python | {
"resource": ""
} |
q264389 | HdfDataBuffer.save | validation | def save(self, ds_name, data, dtype=None):
"""
See create_dataset.
"""
return self.create_dataset(ds_name, data, dtype) | python | {
"resource": ""
} |
q264390 | NumpyHDFStore._fill_missing_values | validation | def _fill_missing_values(df, range_values, fill_value=0, fill_method=None):
"""
Will get the names of the index colums of df, obtain their ranges from
range_values dict and return a reindexed version of df with the given
range values.
:param df: pandas DataFrame
:param range_values: dict or array-like
Must contain for each index column of df an entry with all the values
within the range of the column.
:param fill_value: scalar or 'nearest', default 0
Value to use for missing values. Defaults to 0, but can be any
"compatible" value, e.g., NaN.
The 'nearest' mode will fill the missing value with the nearest value in
the column.
:param fill_method: {'backfill', 'bfill', 'pad', 'ffill', None}, default None
Method to use for filling holes in reindexed DataFrame
'pad' / 'ffill': propagate last valid observation forward to next valid
'backfill' / 'bfill': use NEXT valid observation to fill gap
:return: pandas Dataframe and used column ranges
reindexed DataFrame and dict with index column ranges
"""
idx_colnames = df.index.names
idx_colranges = [range_values[x] for x in idx_colnames]
fullindex = pd.Index([p for p in product(*idx_colranges)],
name=tuple(idx_colnames))
fulldf = df.reindex(index=fullindex, fill_value=fill_value,
method=fill_method)
fulldf.index.names = idx_colnames
return fulldf, idx_colranges | python | {
"resource": ""
} |
q264391 | NumpyHDFStore.get | validation | def get(self, key):
"""
Retrieve pandas object or group of Numpy ndarrays
stored in file
Parameters
----------
key : object
Returns
-------
obj : type of object stored in file
"""
node = self.get_node(key)
if node is None:
raise KeyError('No object named %s in the file' % key)
if hasattr(node, 'attrs'):
if 'pandas_type' in node.attrs:
return self._read_group(node)
return self._read_array(node) | python | {
"resource": ""
} |
q264392 | NumpyHDFStore.put | validation | def put(self, key, value, attrs=None, format=None, append=False, **kwargs):
"""
Store object in HDFStore
Parameters
----------
key : str
value : {Series, DataFrame, Panel, Numpy ndarray}
format : 'fixed(f)|table(t)', default is 'fixed'
fixed(f) : Fixed format
Fast writing/reading. Not-appendable, nor searchable
table(t) : Table format
Write as a PyTables Table structure which may perform worse but allow more flexible operations
like searching/selecting subsets of the data
append : boolean, default False
This will force Table format, append the input data to the
existing.
encoding : default None, provide an encoding for strings
"""
if not isinstance(value, np.ndarray):
super(NumpyHDFStore, self).put(key, value, format, append, **kwargs)
else:
group = self.get_node(key)
# remove the node if we are not appending
if group is not None and not append:
self._handle.removeNode(group, recursive=True)
group = None
if group is None:
paths = key.split('/')
# recursively create the groups
path = '/'
for p in paths:
if not len(p):
continue
new_path = path
if not path.endswith('/'):
new_path += '/'
new_path += p
group = self.get_node(new_path)
if group is None:
group = self._handle.createGroup(path, p)
path = new_path
ds_name = kwargs.get('ds_name', self._array_dsname)
ds = self._handle.createArray(group, ds_name, value)
if attrs is not None:
for key in attrs:
setattr(ds.attrs, key, attrs[key])
self._handle.flush()
return ds | python | {
"resource": ""
} |
q264393 | NumpyHDFStore.put_df_as_ndarray | validation | def put_df_as_ndarray(self, key, df, range_values, loop_multiindex=False,
unstack=False, fill_value=0, fill_method=None):
"""Returns a PyTables HDF Array from df in the shape given by its index columns range values.
:param key: string object
:param df: pandas DataFrame
:param range_values: dict or array-like
Must contain for each index column of df an entry with all the values
within the range of the column.
:param loop_multiindex: bool
Will loop through the first index in a multiindex dataframe, extract a
dataframe only for one value, complete and fill the missing values and
store in the HDF.
If this is True, it will not use unstack.
This is as fast as unstacking.
:param unstack: bool
Unstack means that this will use the first index name to
unfold the DataFrame, and will create a group with as many datasets
as valus has this first index.
Use this if you think the filled dataframe won't fit in your RAM memory.
If set to False, this will transform the dataframe in memory first
and only then save it.
:param fill_value: scalar or 'nearest', default 0
Value to use for missing values. Defaults to 0, but can be any
"compatible" value, e.g., NaN.
The 'nearest' mode will fill the missing value with the nearest value in
the column.
:param fill_method: {'backfill', 'bfill', 'pad', 'ffill', None}, default None
Method to use for filling holes in reindexed DataFrame
'pad' / 'ffill': propagate last valid observation forward to next valid
'backfill' / 'bfill': use NEXT valid observation to fill gap
:return: PyTables data node
"""
idx_colnames = df.index.names
#idx_colranges = [range_values[x] for x in idx_colnames]
#dataset group name if not given
if key is None:
key = idx_colnames[0]
if loop_multiindex:
idx_values = df.index.get_level_values(0).unique()
for idx in idx_values:
vals, _ = self._fill_missing_values(df.xs((idx,), level=idx_colnames[0]),
range_values,
fill_value=fill_value,
fill_method=fill_method)
ds_name = str(idx) + '_' + '_'.join(vals.columns)
self._push_dfblock(key, vals, ds_name, range_values)
return self._handle.get_node('/' + str(key))
#separate the dataframe into blocks, only with the first index
else:
if unstack:
df = df.unstack(idx_colnames[0])
for idx in df:
vals, _ = self._fill_missing_values(df[idx], range_values,
fill_value=fill_value,
fill_method=fill_method)
vals = np.nan_to_num(vals)
ds_name = '_'.join([str(x) for x in vals.name])
self._push_dfblock(key, vals, ds_name, range_values)
return self._handle.get_node('/' + str(key))
#not separate the data
vals, _ = self._fill_missing_values(df, range_values,
fill_value=fill_value,
fill_method=fill_method)
ds_name = self._array_dsname
return self._push_dfblock(key, vals, ds_name, range_values) | python | {
"resource": ""
} |
q264394 | MedicalImage.smooth_fwhm | validation | def smooth_fwhm(self, fwhm):
""" Set a smoothing Gaussian kernel given its FWHM in mm. """
if fwhm != self._smooth_fwhm:
self._is_data_smooth = False
self._smooth_fwhm = fwhm | python | {
"resource": ""
} |
q264395 | MedicalImage.apply_mask | validation | def apply_mask(self, mask_img):
"""First set_mask and the get_masked_data.
Parameters
----------
mask_img: nifti-like image, NeuroImage or str
3D mask array: True where a voxel should be used.
Can either be:
- a file path to a Nifti image
- any object with get_data() and get_affine() methods, e.g., nibabel.Nifti1Image.
If niimg is a string, consider it as a path to Nifti image and
call nibabel.load on it. If it is an object, check if get_data()
and get_affine() methods are present, raise TypeError otherwise.
Returns
-------
The masked data deepcopied
"""
self.set_mask(mask_img)
return self.get_data(masked=True, smoothed=True, safe_copy=True) | python | {
"resource": ""
} |
q264396 | MedicalImage.set_mask | validation | def set_mask(self, mask_img):
"""Sets a mask img to this. So every operation to self, this mask will be taken into account.
Parameters
----------
mask_img: nifti-like image, NeuroImage or str
3D mask array: True where a voxel should be used.
Can either be:
- a file path to a Nifti image
- any object with get_data() and get_affine() methods, e.g., nibabel.Nifti1Image.
If niimg is a string, consider it as a path to Nifti image and
call nibabel.load on it. If it is an object, check if get_data()
and get_affine() methods are present, raise TypeError otherwise.
Note
----
self.img and mask_file must have the same shape.
Raises
------
FileNotFound, NiftiFilesNotCompatible
"""
mask = load_mask(mask_img, allow_empty=True)
check_img_compatibility(self.img, mask, only_check_3d=True) # this will raise an exception if something is wrong
self.mask = mask | python | {
"resource": ""
} |
q264397 | MedicalImage._mask_data | validation | def _mask_data(self, data):
"""Return the data masked with self.mask
Parameters
----------
data: np.ndarray
Returns
-------
masked np.ndarray
Raises
------
ValueError if the data and mask dimensions are not compatible.
Other exceptions related to numpy computations.
"""
self._check_for_mask()
msk_data = self.mask.get_data()
if self.ndim == 3:
return data[msk_data], np.where(msk_data)
elif self.ndim == 4:
return _apply_mask_to_4d_data(data, self.mask)
else:
raise ValueError('Cannot mask {} with {} dimensions using mask {}.'.format(self, self.ndim, self.mask)) | python | {
"resource": ""
} |
q264398 | MedicalImage.apply_smoothing | validation | def apply_smoothing(self, smooth_fwhm):
"""Set self._smooth_fwhm and then smooths the data.
See boyle.nifti.smooth.smooth_imgs.
Returns
-------
the smoothed data deepcopied.
"""
if smooth_fwhm <= 0:
return
old_smooth_fwhm = self._smooth_fwhm
self._smooth_fwhm = smooth_fwhm
try:
data = self.get_data(smoothed=True, masked=True, safe_copy=True)
except ValueError as ve:
self._smooth_fwhm = old_smooth_fwhm
raise
else:
self._smooth_fwhm = smooth_fwhm
return data | python | {
"resource": ""
} |
q264399 | MedicalImage.mask_and_flatten | validation | def mask_and_flatten(self):
"""Return a vector of the masked data.
Returns
-------
np.ndarray, tuple of indices (np.ndarray), tuple of the mask shape
"""
self._check_for_mask()
return self.get_data(smoothed=True, masked=True, safe_copy=False)[self.get_mask_indices()],\
self.get_mask_indices(), self.mask.shape | python | {
"resource": ""
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.