signature:
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"""

implementation:
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


signature:
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"""

implementation:
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 )


signature:
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 ."""

implementation:
# 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 )


signature:
def save_variables_to_hdf5 ( file_path , variables , mode = 'w' , h5path = '/' ) :
    """Parameters
 file _ path : str
 variables : dict
 Dictionary with objects . Object name - > object
 mode : str
 HDF5 file access mode
 See h5py documentation for details .
 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 )
 Notes
 It is recommended to use numpy arrays as objects .
 List or tuples of strings won ' t work , convert them into numpy . arrays before ."""

implementation:
if not isinstance ( variables , dict ) :
    raise ValueError ( 'Expected `variables` to be a dict, got a {}.' . format ( type ( variables ) ) )
if not variables :
    raise ValueError ( 'Expected `variables` to be a non-empty dict.' )
h5file = h5py . File ( file_path , mode = mode )
h5group = h5file . require_group ( h5path )
for vn in variables :
    data = variables [ vn ]
    # fix for string numpy arrays
    if hasattr ( data , 'dtype' ) and ( data . dtype . type is np . string_ or data . dtype . type is np . unicode_ ) :
        dt = h5py . special_dtype ( vlen = str )
        data = data . astype ( dt )
    if isinstance ( data , dict ) :
        for key in data : # h5group . create _ dataset ( str ( key ) )
        # import ipdb
        # ipdb . set _ trace ( )
            h5group [ str ( key ) ] = data [ key ]
    elif isinstance ( data , list ) :
        for idx , item in enumerate ( data ) : # h5group . create _ dataset ( str ( idx ) )
            h5group [ str ( idx ) ] = item
    else :
        h5group [ vn ] = data
h5file . close ( )


signature:
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"""

implementation:
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


signature:
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"""

implementation:
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


signature:
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"""

implementation:
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


signature:
def mask ( self , image ) :
    """self . mask setter
 Parameters
 image : str or img - like object .
 See NeuroImage constructor docstring ."""

implementation:
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


signature:
def check_compatibility ( self , one_img , another_img = None ) :
    """Parameters
 one _ img : str or img - like object .
 See NeuroImage constructor docstring .
 anoter _ img : str or img - like object .
 See NeuroImage constructor docstring .
 If None will use the first image of self . images , if there is any .
 Raises
 NiftiFilesNotCompatible
 If one _ img and another _ img aren ' t compatible .
 ValueError
 If another _ img is None and there are no other images in this set ."""

implementation:
if another_img is None :
    if len ( self . items ) > 0 :
        another_img = self . items [ 0 ]
    else :
        raise ValueError ( 'self.items is empty, need an image to compare ' 'with {}' . format ( repr_imgs ( one_img ) ) )
try :
    if self . all_compatible :
        check_img_compatibility ( one_img , another_img )
    if self . mask is not None :
        check_img_compatibility ( one_img , self . mask , only_check_3d = True )
except :
    raise


signature:
def set_labels ( self , labels ) :
    """Parameters
 labels : list of int or str
 This list will be checked to have the same size as
 Raises
 ValueError
 if len ( labels ) ! = self . n _ subjs"""

implementation:
if not isinstance ( labels , string_types ) and len ( labels ) != self . n_subjs :
    raise ValueError ( 'The number of given labels ({}) is not the same ' 'as the number of subjects ({}).' . format ( len ( labels ) , self . n_subjs ) )
self . labels = labels


signature:
def _load_images_and_labels ( self , images , labels = None ) :
    """Read the images , load them into self . items and set the labels ."""

implementation:
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 )


signature:
def to_matrix ( self , smooth_fwhm = 0 , outdtype = None ) :
    """Return numpy . ndarray with the masked or flatten image data and
 the relevant information ( mask indices and volume shape ) .
 Parameters
 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 .
 Returns
 outmat , mask _ indices , vol _ shape
 outmat : 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 ."""

implementation:
if not self . all_compatible :
    raise ValueError ( "`self.all_compatible` must be True in order to use this function." )
if not outdtype :
    outdtype = self . items [ 0 ] . dtype
# extract some info from the mask
n_voxels = None
mask_indices = None
mask_shape = self . items [ 0 ] . shape [ : 3 ]
if self . has_mask :
    mask_arr = self . mask . get_data ( )
    mask_indices = np . nonzero ( mask_arr )
    mask_shape = self . mask . shape
    n_voxels = np . count_nonzero ( mask_arr )
# if the mask is empty will use the whole image
if n_voxels is None :
    log . debug ( 'Non-zero voxels have not been found in mask {}' . format ( self . mask ) )
    n_voxels = np . prod ( mask_shape )
    mask_indices = None
# get the shape of the flattened subject data
ndims = self . items [ 0 ] . ndim
if ndims == 3 :
    subj_flat_shape = ( n_voxels , )
elif ndims == 4 :
    subj_flat_shape = ( n_voxels , self . items [ 0 ] . shape [ 3 ] )
else :
    raise NotImplementedError ( 'The subject images have {} dimensions. ' 'Still have not implemented t_matrix for this shape.' . format ( ndims ) )
# create and fill the big matrix
outmat = np . zeros ( ( self . n_subjs , ) + subj_flat_shape , dtype = outdtype )
try :
    for i , image in enumerate ( self . items ) :
        if smooth_fwhm > 0 :
            image . fwhm = smooth_fwhm
        if self . has_mask :
            image . set_mask ( self . mask )
        outmat [ i , : ] , _ , _ = image . mask_and_flatten ( )
        image . clear_data ( )
except Exception as exc :
    raise Exception ( 'Error flattening file {0}' . format ( image ) ) from exc
else :
    return outmat , mask_indices , mask_shape


signature:
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 ."""

implementation:
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


signature:
def _init_subj_data ( self , subj_files ) :
    """Parameters
 subj _ files : list or dict of str
 file _ path - > int / str"""

implementation:
try :
    if isinstance ( subj_files , list ) :
        self . from_list ( subj_files )
    elif isinstance ( subj_files , dict ) :
        self . from_dict ( subj_files )
    else :
        raise ValueError ( 'Could not recognize subj_files argument variable type.' )
except Exception as exc :
    raise Exception ( 'Cannot read subj_files input argument.' ) from exc


signature:
def _load_image ( file_path ) :
    """Parameters
 file _ path : str
 Path to the nifti file
 Returns
 nipy . Image with a file _ path member"""

implementation:
if not os . path . exists ( file_path ) :
    raise FileNotFound ( file_path )
try :
    nii_img = load_nipy_img ( file_path )
    nii_img . file_path = file_path
    return nii_img
except Exception as exc :
    raise Exception ( 'Reading file {0}.' . format ( file_path ) ) from exc


signature:
def _smooth_img ( nii_img , smooth_fwhm ) :
    """Parameters
 nii _ img : nipy . Image
 smooth _ fwhm : float
 Returns
 smoothed nipy . Image"""

implementation:
# delayed import because could not install nipy on Python 3 on OSX
from nipy . algorithms . kernel_smooth import LinearFilter
if smooth_fwhm <= 0 :
    return nii_img
filter = LinearFilter ( nii_img . coordmap , nii_img . shape )
return filter . smooth ( nii_img )


signature:
def from_dict ( self , subj_files ) :
    """Parameters
 subj _ files : dict of str
 file _ path - > int / str"""

implementation:
for group_label in subj_files :
    try :
        group_files = subj_files [ group_label ]
        self . items . extend ( [ self . _load_image ( get_abspath ( imgf ) ) for imgf in group_files ] )
        self . labels . extend ( [ group_label ] * len ( group_files ) )
    except Exception as exc :
        raise Exception ( 'Error while reading files from ' 'group {0}.' . format ( group_label ) ) from exc


signature:
def from_list ( self , subj_files ) :
    """Parameters
 subj _ files : list of str
 file _ paths"""

implementation:
for sf in subj_files :
    try :
        nii_img = self . _load_image ( get_abspath ( sf ) )
        self . items . append ( nii_img )
    except Exception as exc :
        raise Exception ( 'Error while reading file {0}.' . format ( sf ) ) from exc


signature:
def set_labels ( self , subj_labels ) :
    """Parameters
 subj _ labels : list of int or str
 This list will be checked to have the same size as files list
 ( self . items )"""

implementation:
if len ( subj_labels ) != self . n_subjs :
    raise ValueError ( 'The number of given labels is not the same as the number of subjects.' )
self . labels = subj_labels


signature:
def to_matrix ( self , smooth_fwhm = 0 , outdtype = None ) :
    """Create a Numpy array with the data and return the relevant information ( mask indices and volume shape ) .
 Parameters
 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 .
 Returns
 outmat , mask _ indices , vol _ shape
 outmat : 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 ."""

implementation:
vol = self . items [ 0 ] . get_data ( )
if not outdtype :
    outdtype = vol . dtype
n_voxels = None
mask_indices = None
mask_shape = self . items [ 0 ] . shape
if self . has_mask :
    mask_arr = get_img_data ( self . mask_file )
    mask_indices = np . where ( mask_arr > 0 )
    mask_shape = mask_arr . shape
    n_voxels = np . count_nonzero ( mask_arr )
if n_voxels is None :
    log . debug ( 'Non-zero voxels have not been found in mask {}' . format ( self . mask_file ) )
    n_voxels = np . prod ( vol . shape )
outmat = np . zeros ( ( self . n_subjs , n_voxels ) , dtype = outdtype )
try :
    for i , nipy_img in enumerate ( self . items ) :
        vol = self . _smooth_img ( nipy_img , smooth_fwhm ) . get_data ( )
        if self . has_mask is not None :
            outmat [ i , : ] = vol [ mask_indices ]
        else :
            outmat [ i , : ] = vol . flatten ( )
except Exception as exc :
    raise Exception ( 'Error when flattening file {0}' . format ( nipy_img . file_path ) ) from exc
else :
    return outmat , mask_indices , mask_shape


signature:
def die ( msg , code = - 1 ) :
    """Writes msg to stderr and exits with return code"""

implementation:
sys . stderr . write ( msg + "\n" )
sys . exit ( code )


signature:
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"""

implementation:
p = subprocess . Popen ( cmd_args , stdout = subprocess . PIPE )
( output , err ) = p . communicate ( )
return output


signature:
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 ."""

implementation:
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


signature:
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"""

implementation:
log . info ( cmd )
ret = condor_submit ( cmd )
if ret != 0 :
    subprocess . call ( cmd , shell = shell )


signature:
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 ."""

implementation:
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 )


signature:
def clean ( ctx ) :
    """Clean previously built package artifacts ."""

implementation:
ctx . run ( f'python setup.py clean' )
dist = ROOT . joinpath ( 'dist' )
print ( f'removing {dist}' )
shutil . rmtree ( str ( dist ) )


signature:
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 ."""

implementation:
artifacts = ' ' . join ( shlex . quote ( str ( n ) ) for n in ROOT . joinpath ( 'dist' ) . glob ( 'pipfile[-_]cli-*' ) )
ctx . run ( f'twine upload --repository="{repo}" {artifacts}' )


signature:
def load_command_table ( self , args ) : # pylint : disable = too - many - statements
    """Load all Service Fabric commands"""

implementation:
# 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 )


signature:
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 ."""

implementation:
# 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 )


signature:
def _check_medimg ( image , make_it_3d = True ) :
    """Check that image is a proper img . Turn filenames into objects .
 Parameters
 image : img - like object or str
 Can either be :
 - a file path to a medical image file , e . g . NifTI , . mhd / raw , . mha
 - any object with get _ data ( ) method and affine & header attributes , e . g . , nibabel . Nifti1Image .
 - a Numpy array , which will be wrapped by a nibabel . Nifti2Image class with an ` eye ` affine .
 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 .
 make _ it _ 3d : boolean , optional
 If True , check if the image is a 3D image and raise an error if not .
 Returns
 result : nifti - like
 result can be nibabel . Nifti1Image or the input , as - is . It is guaranteed
 that the returned object has get _ data ( ) and get _ affine ( ) methods ."""

implementation:
if isinstance ( image , string_types ) : # a filename , load it
    img = open_volume_file ( image )
    if make_it_3d :
        img = _make_it_3d ( img )
    return img
elif isinstance ( image , np . array ) :
    return nib . Nifti2Image ( image , affine = np . eye ( image . ndim + 1 ) )
elif isinstance ( image , nib . Nifti1Image ) or is_img ( image ) :
    return image
else :
    raise TypeError ( 'Data given cannot be converted to a medical image' ' image: this object -"{}"- does not have' ' get_data or get_affine methods' . format ( type ( image ) ) )


signature:
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"""

implementation:
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


signature:
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"""

implementation:
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 ) ) )


signature:
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"""

implementation:
if check_if_dicoms :
    self . items = [ ]
    for f in fileset :
        if is_dicom_file ( f ) :
            self . items . append ( f )
else :
    self . items = fileset


signature:
def update ( self , dicomset ) :
    """Update this set with the union of itself and dicomset .
 Parameters
 dicomset : DicomFileSet"""

implementation:
if not isinstance ( dicomset , DicomFileSet ) :
    raise ValueError ( 'Given dicomset is not a DicomFileSet.' )
self . items = list ( set ( self . items ) . update ( dicomset ) )


signature:
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"""

implementation:
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 )


signature:
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"""

implementation:
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


signature:
def scrape_all_files ( self ) :
    """Generator that yields one by one the return value for self . read _ dcm
 for each file within this set"""

implementation:
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


signature:
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"""

implementation:
field_values = set ( )
for dcm in dcm_file_list :
    field_values . add ( str ( DicomFile ( dcm ) . get_attributes ( field_name ) ) )
return field_values


signature:
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"""

implementation:
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


signature:
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"""

implementation:
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


signature:
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"""

implementation:
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


signature:
def decompress ( input_dir , dcm_pattern = '*.dcm' ) :
    """Decompress all * . dcm files recursively found in DICOM _ DIR .
 This uses ' gdcmconv - - raw ' .
 It works when ' dcm2nii ' shows the ` Unsupported Transfer Syntax ` error . This error is
 usually caused by lack of JPEG2000 support in dcm2nii compilation .
 Read more :
 http : / / www . nitrc . org / plugins / mwiki / index . php / dcm2nii : MainPage # Transfer _ Syntaxes _ and _ Compressed _ Images
 Parameters
 input _ dir : str
 Folder path
 dcm _ patther : str
 Pattern of the DICOM file names in ` input _ dir ` .
 Notes
 The * . dcm files in ` input _ folder ` will be overwritten ."""

implementation:
dcmfiles = sorted ( recursive_glob ( input_dir , dcm_pattern ) )
for dcm in dcmfiles :
    cmd = 'gdcmconv --raw -i "{0}" -o "{0}"' . format ( dcm )
    log . debug ( 'Calling {}.' . format ( cmd ) )
    subprocess . check_call ( cmd , shell = True )


signature:
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 ."""

implementation:
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 )


signature:
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"""

implementation:
# 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 )


signature:
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 ."""

implementation:
@ 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


signature:
def thr_img ( img , thr = 2. , mode = '+' ) :
    """Use the given magic function name ` func ` to threshold with value ` thr `
 the data of ` img ` and return a new nibabel . Nifti1Image .
 Parameters
 img : img - like
 thr : float or int
 The threshold value .
 mode : str
 Choices : ' + ' for positive threshold ,
 ' + - ' for positive and negative threshold and
 ' - ' for negative threshold .
 Returns
 thr _ img : nibabel . Nifti1Image
 Thresholded image"""

implementation:
vol = read_img ( img ) . get_data ( )
if mode == '+' :
    mask = vol > thr
elif mode == '+-' or mode == '-+' :
    mask = np . abs ( vol ) > thr
elif mode == '-' :
    mask = vol < - thr
else :
    raise ValueError ( "Expected `mode` to be one of ('+', '+-', '-+', '-'), " "got {}." . format ( mode ) )
return vol * mask


signature:
def div_img ( img1 , div2 ) :
    """Pixelwise division or divide by a number"""

implementation:
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 ) )


signature:
def apply_mask ( img , mask ) :
    """Return the image with the given ` mask ` applied ."""

implementation:
from . mask import apply_mask
vol , _ = apply_mask ( img , mask )
return vector_to_volume ( vol , read_img ( mask ) . get_data ( ) . astype ( bool ) )


signature:
def abs_img ( img ) :
    """Return an image with the binarised version of the data of ` img ` ."""

implementation:
bool_img = np . abs ( read_img ( img ) . get_data ( ) )
return bool_img . astype ( int )


signature:
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 ."""

implementation:
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


signature:
def spatial_map ( icc , thr , mode = '+' ) :
    """Return the thresholded z - scored ` icc ` ."""

implementation: return thr_img ( icc_img_to_zscore ( icc ) , thr = thr , mode = mode ) . get_data ( )


signature:
def filter_icc ( icc , mask = None , thr = 2 , zscore = True , mode = "+" ) :
    """Threshold then mask an IC correlation map .
 Parameters
 icc : img - like
 The ' raw ' ICC map .
 mask : img - like
 If not None . Will apply this masks in the end of the process .
 thr : float
 The threshold value .
 zscore : bool
 If True will calculate the z - score of the ICC before thresholding .
 mode : str
 Choices : ' + ' for positive threshold ,
 ' + - ' for positive and negative threshold and
 ' - ' for negative threshold .
 Returns
 icc _ filt : nibabel . NiftiImage
 Thresholded and masked ICC ."""

implementation:
if zscore :
    icc_filt = thr_img ( icc_img_to_zscore ( icc ) , thr = thr , mode = mode )
else :
    icc_filt = thr_img ( icc , thr = thr , mode = mode )
if mask is not None :
    icc_filt = apply_mask ( icc_filt , mask )
return icc_filt


signature:
def check_mhd_img ( image , make_it_3d = False ) :
    """Check that image is a proper img . Turn filenames into objects .
 Parameters
 image : img - like object or str
 Can either be :
 - a file path to a . mhd file . ( if it is a . raw file , this won ' t work ) .
 - any object with get _ data ( ) and get _ affine ( ) methods , e . g . , nibabel . Nifti1Image .
 If niimg is a string , consider it as a path to . mhd image and
 call load _ raw _ data _ with _ mhd on it . If it is an object , check if get _ data ( )
 and get _ affine ( ) methods are present , raise TypeError otherwise .
 make _ it _ 3d : boolean , optional
 If True , check if the image is a 3D image and raise an error if not .
 Returns
 result : nifti - like
 result can be nibabel . Nifti1Image or the input , as - is . It is guaranteed
 that the returned object has get _ data ( ) and get _ affine ( ) methods ."""

implementation:
if isinstance ( image , string_types ) : # a filename , load it
    if not op . exists ( image ) :
        raise FileNotFound ( image )
    ext = get_extension ( image ) . lower ( )
    if not 'mhd' in ext :
        warnings . warn ( 'Expecting a filepath with `.mhd` extension, got {}.' . format ( image ) )
    img , hdr = load_raw_data_with_mhd ( image )
    if make_it_3d :
        img = _make_it_3d ( img )
    return img
elif is_img ( image ) :
    return image
else :
    raise TypeError ( 'Data given cannot be converted to a nifti' ' image: this object -"{}"- does not have' ' get_data or get_affine methods' . format ( type ( image ) ) )


signature:
def _make_it_3d ( img ) :
    """Enforce that img is a 3D img - like object , if it is not , raise a TypeError .
 i . e . , remove dimensions of size 1.
 Parameters
 img : numpy . ndarray
 Image data array
 Returns
 3D numpy ndarray object"""

implementation:
shape = img . shape
if len ( shape ) == 3 :
    return img
elif len ( shape ) == 4 and shape [ 3 ] == 1 : # " squeeze " the image .
    return img [ : , : , : , 0 ]
else :
    raise TypeError ( 'A 3D image is expected, but an image with a shape of {} was given.' . format ( shape ) )


signature:
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"""

implementation:
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 )


signature:
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 ."""

implementation:
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 )


signature:
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"""

implementation:
# 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


signature:
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"""

implementation:
# 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


signature:
def sav_to_pandas_rpy2 ( input_file ) :
    """SPSS . sav files to Pandas DataFrame through Rpy2
 : param input _ file : string
 : return :"""

implementation:
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 )


signature:
def sav_to_pandas_savreader ( input_file ) :
    """SPSS . sav files to Pandas DataFrame through savreader module
 : param input _ file : string
 : return :"""

implementation:
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 )


signature:
def save_variables ( filename , variables ) :
    """Save given variables in a file .
 Valid extensions : ' . pyshelf ' or ' . shelf ' ( Python shelve )
 ' . mat ' ( Matlab archive ) ,
 ' . hdf5 ' or ' . h5 ' ( HDF5 file )
 Parameters
 filename : str
 Output file path .
 variables : dict
 Dictionary varname - > variable
 Raises
 ValueError : if the extension of the filesname is not recognized ."""

implementation:
ext = get_extension ( filename ) . lower ( )
out_exts = { '.pyshelf' , '.shelf' , '.mat' , '.hdf5' , '.h5' }
output_file = filename
if not ext in out_exts :
    output_file = add_extension_if_needed ( filename , '.pyshelf' )
    ext = get_extension ( filename )
if ext == '.pyshelf' or ext == '.shelf' :
    save_variables_to_shelve ( output_file , variables )
elif ext == '.mat' :
    save_variables_to_mat ( output_file , variables )
elif ext == '.hdf5' or ext == '.h5' :
    from . hdf5 import save_variables_to_hdf5
    save_variables_to_hdf5 ( output_file , variables )
else :
    raise ValueError ( 'Filename extension {0} not accepted.' . format ( ext ) )


signature:
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"""

implementation:
variables = { }
for i , vn in enumerate ( varnames ) :
    variables [ vn ] = varlist [ i ]
ExportData . save_variables ( filename , variables )


signature:
def cli ( ) :
    """Create CLI environment"""

implementation: 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 )


signature:
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"""

implementation:
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


signature:
def pick_rois ( rois_img , roi_values , bg_val = 0 ) :
    """Return the ` rois _ img ` only with the ROI values from ` roi _ values ` .
 Parameters
 rois _ img : niimg - like
 roi _ values : list of int or float
 The list of values from rois _ img .
 bg _ val : int or float
 The background value of ` rois _ img ` .
 Returns
 subset _ rois _ img : nibabel . Nifti2Image"""

implementation:
img = read_img ( rois_img )
img_data = img . get_data ( )
if bg_val == 0 :
    out = np . zeros ( img_data . shape , dtype = img_data . dtype )
else :
    out = np . ones ( img_data . shape , dtype = img_data . dtype ) * bg_val
for r in roi_values :
    out [ img_data == r ] = r
return nib . Nifti2Image ( out , affine = img . affine , header = img . header )


signature:
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 ."""

implementation:
# 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 ( )


signature:
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 ."""

implementation:
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


signature:
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"""

implementation:
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 )


signature:
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 ."""

implementation:
rois = np . unique ( arr )
rois = rois [ np . nonzero ( rois ) ]
rois . sort ( )
return rois


signature:
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 ."""

implementation:
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


signature:
def partition_timeseries ( image , roi_img , mask_img = None , zeroe = True , roi_values = None , outdict = False ) :
    """Partition the timeseries in tsvol according to the ROIs in roivol .
 If a mask is given , will use it to exclude any voxel outside of it .
 The outdict indicates whether you want a dictionary for each set of timeseries keyed by the ROI value
 or a list of timeseries sets . If True and roi _ img is not None will return an OrderedDict , if False
 or roi _ img or roi _ list is None will return a list .
 Background value is assumed to be 0 and won ' t be used here .
 Parameters
 image : img - like object or str
 4D timeseries volume
 roi _ img : img - like object or str
 3D volume defining different ROIs .
 mask _ img : img - like object or str
 3D mask volume
 zeroe : bool
 If true will remove the null timeseries voxels .
 roi _ values : list of ROI values ( int ? )
 List of the values of the ROIs to indicate the
 order and which ROIs will be processed .
 outdict : bool
 If True will return an OrderedDict of timeseries sets , otherwise a list .
 Returns
 timeseries : list or OrderedDict
 A dict with the timeseries as items and keys as the ROIs voxel values or
 a list where each element is the timeseries set ordered by the sorted values in roi _ img or by the roi _ values
 argument ."""

implementation:
img = read_img ( image )
rois = read_img ( roi_img )
# check if roi _ img and image are compatible
check_img_compatibility ( img , rois , only_check_3d = True )
# check if rois has all roi _ values
roi_data = rois . get_data ( )
if roi_values is not None :
    for rv in roi_values :
        if not np . any ( roi_data == rv ) :
            raise ValueError ( 'Could not find value {} in rois_img {}.' . format ( rv , repr_imgs ( roi_img ) ) )
else :
    roi_values = get_unique_nonzeros ( roi_data )
# check if mask and image are compatible
if mask_img is None :
    mask_data = None
else :
    mask = load_mask ( mask_img )
    check_img_compatibility ( img , mask , only_check_3d = True )
    mask_data = mask . get_data ( )
# choose function to call
if outdict :
    extract_data = _extract_timeseries_dict
else :
    extract_data = _extract_timeseries_list
# extract data and return it
try :
    return extract_data ( img . get_data ( ) , rois . get_data ( ) , mask_data , roi_values = roi_values , zeroe = zeroe )
except :
    raise


signature:
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 ."""

implementation:
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


signature:
def _extract_timeseries_dict ( tsvol , roivol , maskvol = None , roi_values = None , zeroe = True ) :
    """Partition the timeseries in tsvol according to the ROIs in roivol .
 If a mask is given , will use it to exclude any voxel outside of it .
 Parameters
 tsvol : numpy . ndarray
 4D timeseries volume or a 3D volume to be partitioned
 roivol : numpy . ndarray
 3D ROIs volume
 maskvol : numpy . ndarray
 3D mask volume
 zeroe : bool
 If true will remove the null timeseries voxels .
 roi _ values : list of ROI values ( int ? )
 List of the values of the ROIs to indicate the
 order and which ROIs will be processed .
 Returns
 ts _ dict : OrderedDict
 A dict with the timeseries as items and keys as the ROIs voxel values ."""

implementation:
_check_for_partition ( tsvol , roivol , maskvol )
# get unique values of the atlas
if roi_values is None :
    roi_values = get_unique_nonzeros ( roivol )
ts_dict = OrderedDict ( )
for r in roi_values :
    ts = _partition_data ( tsvol , roivol , r , maskvol , zeroe )
    if len ( ts ) == 0 :
        ts = np . zeros ( tsvol . shape [ - 1 ] )
    ts_dict [ r ] = ts
return ts_dict


signature:
def _extract_timeseries_list ( tsvol , roivol , maskvol = None , roi_values = None , zeroe = True ) :
    """Partition the timeseries in tsvol according to the ROIs in roivol .
 If a mask is given , will use it to exclude any voxel outside of it .
 Parameters
 tsvol : numpy . ndarray
 4D timeseries volume or a 3D volume to be partitioned
 roivol : numpy . ndarray
 3D ROIs volume
 maskvol : numpy . ndarray
 3D mask volume
 zeroe : bool
 If true will remove the null timeseries voxels . Only applied to timeseries ( 4D ) data .
 roi _ values : list of ROI values ( int ? )
 List of the values of the ROIs to indicate the
 order and which ROIs will be processed .
 Returns
 ts _ list : list
 A list with the timeseries arrays as items"""

implementation:
_check_for_partition ( tsvol , roivol , maskvol )
if roi_values is None :
    roi_values = get_unique_nonzeros ( roivol )
ts_list = [ ]
for r in roi_values :
    ts = _partition_data ( tsvol , roivol , r , maskvol , zeroe )
    if len ( ts ) == 0 :
        ts = np . zeros ( tsvol . shape [ - 1 ] )
    ts_list . append ( ts )
return ts_list


signature:
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 ."""

implementation:
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


signature:
def create_hdf_file ( self ) :
    """: return : h5py DataSet"""

implementation:
mode = 'w'
if not self . _overwrite and os . path . exists ( self . _fname ) :
    mode = 'a'
self . _hdf_file = h5py . File ( self . _fname , mode )
if self . _hdf_basepath == '/' :
    self . _group = self . _hdf_file [ '/' ]
else :
    self . _group = self . _hdf_file . create_group ( self . _hdf_basepath )


signature:
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 :"""

implementation:
if ds_name in self . _datasets :
    return self . _datasets [ ds_name ]
else :
    return self . create_empty_dataset ( ds_name )


signature:
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"""

implementation:
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


signature:
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"""

implementation:
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


signature:
def save ( self , ds_name , data , dtype = None ) :
    """See create _ dataset ."""

implementation: return self . create_dataset ( ds_name , data , dtype )


signature:
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"""

implementation:
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


signature:
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"""

implementation:
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 )


signature:
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"""

implementation:
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


signature:
def _push_dfblock ( self , key , df , ds_name , range_values ) :
    """: param key : string
 : param df : pandas Dataframe
 : param ds _ name : string"""

implementation:
# create numpy array and put into hdf _ file
vals_colranges = [ range_values [ x ] for x in df . index . names ]
nu_shape = [ len ( x ) for x in vals_colranges ]
return self . put ( key , np . reshape ( df . values , tuple ( nu_shape ) ) , attrs = { 'axes' : df . index . names } , ds_name = ds_name , append = True )


signature:
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"""

implementation:
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 )


signature:
def get_data ( self , safe_copy = False ) :
    """Get the data in the image .
 If save _ copy is True , will perform a deep copy of the data and return it .
 Parameters
 smoothed : ( optional ) bool
 If True and self . _ smooth _ fwhm > 0 will smooth the data before masking .
 masked : ( optional ) bool
 If True and self . has _ mask will return the masked data , the plain data otherwise .
 safe _ copy : ( optional ) bool
 Returns
 np . ndarray"""

implementation:
if safe_copy :
    data = get_data ( self . img )
else :
    data = self . img . get_data ( caching = self . _caching )
return data


signature:
def smooth_fwhm ( self , fwhm ) :
    """Set a smoothing Gaussian kernel given its FWHM in mm ."""

implementation:
if fwhm != self . _smooth_fwhm :
    self . _is_data_smooth = False
self . _smooth_fwhm = fwhm


signature:
def get_data ( self , smoothed = True , masked = True , safe_copy = False ) :
    """Get the data in the image .
 If save _ copy is True , will perform a deep copy of the data and return it .
 Parameters
 smoothed : ( optional ) bool
 If True and self . _ smooth _ fwhm > 0 will smooth the data before masking .
 masked : ( optional ) bool
 If True and self . has _ mask will return the masked data , the plain data otherwise .
 safe _ copy : ( optional ) bool
 Returns
 np . ndarray"""

implementation:
if not safe_copy and smoothed == self . _is_data_smooth and masked == self . _is_data_masked :
    if self . has_data_loaded ( ) and self . _caching == 'fill' :
        return self . get_data ( )
if safe_copy :
    data = get_data ( self . img )
else :
    data = self . img . get_data ( caching = self . _caching )
is_smoothed = False
if smoothed and self . _smooth_fwhm > 0 :
    try :
        data = _smooth_data_array ( data , self . get_affine ( ) , self . _smooth_fwhm , copy = False )
    except ValueError as ve :
        raise ValueError ( 'Error smoothing image {} with a {}mm FWHM ' 'kernel.' . format ( self . img , self . _smooth_fwhm ) ) from ve
    else :
        is_smoothed = True
is_data_masked = False
if masked and self . has_mask ( ) :
    try :
        data = self . unmask ( self . _mask_data ( data ) [ 0 ] )
    except :
        raise
    else :
        is_data_masked = True
if not safe_copy :
    self . _is_data_masked = is_data_masked
    self . _is_data_smooth = is_smoothed
return data


signature:
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"""

implementation:
self . set_mask ( mask_img )
return self . get_data ( masked = True , smoothed = True , safe_copy = True )


signature:
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"""

implementation:
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


signature:
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 ."""

implementation:
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 ) )


signature:
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 ."""

implementation:
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


signature:
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"""

implementation:
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


signature:
def unmask ( self , arr ) :
    """Use self . mask to reshape arr and self . img to get an affine and header to create
 a new self . img using the data in arr .
 If self . has _ mask ( ) is False , will return the same arr ."""

implementation:
self . _check_for_mask ( )
if 1 > arr . ndim > 2 :
    raise ValueError ( 'The given array has {} dimensions while my mask has {}. ' 'Masked data must be 1D or 2D array. ' . format ( arr . ndim , len ( self . mask . shape ) ) )
if arr . ndim == 2 :
    return matrix_to_4dvolume ( arr , self . mask . get_data ( ) )
elif arr . ndim == 1 :
    return vector_to_volume ( arr , self . mask . get_data ( ) )


signature:
def to_file ( self , outpath ) :
    """Save this object instance in outpath .
 Parameters
 outpath : str
 Output file path"""

implementation:
if not self . has_mask ( ) and not self . is_smoothed ( ) :
    save_niigz ( outpath , self . img )
else :
    save_niigz ( outpath , self . get_data ( masked = True , smoothed = True ) , self . get_header ( ) , self . get_affine ( ) )


signature:
def setup_logging ( log_config_file = op . join ( op . dirname ( __file__ ) , 'logger.yml' ) , log_default_level = LOG_LEVEL , env_key = MODULE_NAME . upper ( ) + '_LOG_CFG' ) :
    """Setup logging configuration ."""

implementation:
path = log_config_file
value = os . getenv ( env_key , None )
if value :
    path = value
if op . exists ( path ) :
    log_cfg = yaml . load ( read ( path ) . format ( MODULE_NAME ) )
    logging . config . dictConfig ( log_cfg )
    # print ( ' Started logging using config file { 0 } . ' . format ( path ) )
else :
    logging . basicConfig ( level = log_default_level )
    # print ( ' Started default logging . Could not find config file '
    # ' in { 0 } . ' . format ( path ) )
log = logging . getLogger ( __name__ )
log . debug ( 'Start logging.' )


signature:
def _read_meta_header ( filename ) :
    """Return a dictionary of meta data from meta header file .
 Parameters
 filename : str
 Path to a . mhd file
 Returns
 meta _ dict : dict
 A dictionary with the . mhd header content ."""

implementation:
fileIN = open ( filename , 'r' )
line = fileIN . readline ( )
meta_dict = { }
tag_flag = [ False ] * len ( MHD_TAGS )
while line :
    tags = str . split ( line , '=' )
    # print tags [ 0]
    for i in range ( len ( MHD_TAGS ) ) :
        tag = MHD_TAGS [ i ]
        if ( str . strip ( tags [ 0 ] ) == tag ) and ( not tag_flag [ i ] ) : # print tags [ 1]
            meta_dict [ tag ] = str . strip ( tags [ 1 ] )
            tag_flag [ i ] = True
    line = fileIN . readline ( )
# comment
fileIN . close ( )
return meta_dict


signature:
def load_raw_data_with_mhd ( filename ) :
    """Return a dictionary of meta data from meta header file .
 Parameters
 filename : str
 Path to a . mhd file
 Returns
 data : numpy . ndarray
 n - dimensional image data array .
 meta _ dict : dict
 A dictionary with the . mhd header content ."""

implementation:
meta_dict = _read_meta_header ( filename )
dim = int ( meta_dict [ 'NDims' ] )
assert ( meta_dict [ 'ElementType' ] in MHD_TO_NUMPY_TYPE )
arr = [ int ( i ) for i in meta_dict [ 'DimSize' ] . split ( ) ]
volume = reduce ( lambda x , y : x * y , arr [ 0 : dim - 1 ] , 1 )
pwd = op . dirname ( filename )
raw_file = meta_dict [ 'ElementDataFile' ]
data_file = op . join ( pwd , raw_file )
ndtype = MHD_TO_NUMPY_TYPE [ meta_dict [ 'ElementType' ] ]
arrtype = NDARRAY_TO_ARRAY_TYPE [ ndtype ]
with open ( data_file , 'rb' ) as fid :
    binvalues = array . array ( arrtype )
    binvalues . fromfile ( fid , volume * arr [ dim - 1 ] )
data = np . array ( binvalues , ndtype )
data = np . reshape ( data , ( arr [ dim - 1 ] , volume ) )
if dim >= 3 : # Begin 3D fix
    dimensions = [ int ( i ) for i in meta_dict [ 'DimSize' ] . split ( ) ]
    # dimensions . reverse ( ) ? ?
    data = data . reshape ( dimensions )
    # End 3D fix
return data , meta_dict


signature:
def get_3D_from_4D ( filename , vol_idx = 0 ) :
    """Return a 3D volume from a 4D nifti image file
 Parameters
 filename : str
 Path to the 4D . mhd file
 vol _ idx : int
 Index of the 3D volume to be extracted from the 4D volume .
 Returns
 vol , hdr
 The data array and the new 3D image header ."""

implementation:
def remove_4th_element_from_hdr_string ( hdr , fieldname ) :
    if fieldname in hdr :
        hdr [ fieldname ] = ' ' . join ( hdr [ fieldname ] . split ( ) [ : 3 ] )
vol , hdr = load_raw_data_with_mhd ( filename )
if vol . ndim != 4 :
    raise ValueError ( 'Volume in {} does not have 4 dimensions.' . format ( op . join ( op . dirname ( filename ) , hdr [ 'ElementDataFile' ] ) ) )
if not 0 <= vol_idx < vol . shape [ 3 ] :
    raise IndexError ( 'IndexError: 4th dimension in volume {} has {} volumes, not {}.' . format ( filename , vol . shape [ 3 ] , vol_idx ) )
new_vol = vol [ : , : , : , vol_idx ] . copy ( )
hdr [ 'NDims' ] = 3
remove_4th_element_from_hdr_string ( hdr , 'ElementSpacing' )
remove_4th_element_from_hdr_string ( hdr , 'DimSize' )
return new_vol , hdr


signature:
def _safe_cache ( memory , func , ** kwargs ) :
    """A wrapper for mem . cache that flushes the cache if the version
 number of nibabel has changed ."""

implementation:
cachedir = memory . cachedir
if cachedir is None or cachedir in __CACHE_CHECKED :
    return memory . cache ( func , ** kwargs )
version_file = os . path . join ( cachedir , 'module_versions.json' )
versions = dict ( )
if os . path . exists ( version_file ) :
    with open ( version_file , 'r' ) as _version_file :
        versions = json . load ( _version_file )
modules = ( nibabel , )
# Keep only the major + minor version numbers
my_versions = dict ( ( m . __name__ , LooseVersion ( m . __version__ ) . version [ : 2 ] ) for m in modules )
commons = set ( versions . keys ( ) ) . intersection ( set ( my_versions . keys ( ) ) )
collisions = [ m for m in commons if versions [ m ] != my_versions [ m ] ]
# Flush cache if version collision
if len ( collisions ) > 0 :
    if nilearn . CHECK_CACHE_VERSION :
        warnings . warn ( "Incompatible cache in %s: " "different version of nibabel. Deleting " "the cache. Put nilearn.CHECK_CACHE_VERSION " "to false to avoid this behavior." % cachedir )
        try :
            tmp_dir = ( os . path . split ( cachedir ) [ : - 1 ] + ( 'old_%i' % os . getpid ( ) , ) )
            tmp_dir = os . path . join ( * tmp_dir )
            # We use rename + unlink to be more robust to race
            # conditions
            os . rename ( cachedir , tmp_dir )
            shutil . rmtree ( tmp_dir )
        except OSError : # Another process could have removed this dir
            pass
        try :
            os . makedirs ( cachedir )
        except OSError : # File exists ?
            pass
    else :
        warnings . warn ( "Incompatible cache in %s: " "old version of nibabel." % cachedir )
# Write json files if configuration is different
if versions != my_versions :
    with open ( version_file , 'w' ) as _version_file :
        json . dump ( my_versions , _version_file )
__CACHE_CHECKED [ cachedir ] = True
return memory . cache ( func , ** kwargs )


signature:
def cache ( func , memory , func_memory_level = None , memory_level = None , ** kwargs ) :
    """Return a joblib . Memory object .
 The memory _ level determines the level above which the wrapped
 function output is cached . By specifying a numeric value for
 this level , the user can to control the amount of cache memory
 used . This function will cache the function call or not
 depending on the cache level .
 Parameters
 func : function
 The function which output is to be cached .
 memory : instance of joblib . Memory or string
 Used to cache the function call .
 func _ memory _ level : int , optional
 The memory _ level from which caching must be enabled for the wrapped
 function .
 memory _ level : int , optional
 The memory _ level used to determine if function call must
 be cached or not ( if user _ memory _ level is equal of greater than
 func _ memory _ level the function is cached )
 kwargs : keyword arguments
 The keyword arguments passed to memory . cache
 Returns
 mem : joblib . MemorizedFunc
 object that wraps the function func . This object may be
 a no - op , if the requested level is lower than the value given
 to _ cache ( ) ) . For consistency , a joblib . Memory object is always
 returned ."""

implementation:
verbose = kwargs . get ( 'verbose' , 0 )
# memory _ level and func _ memory _ level must be both None or both integers .
memory_levels = [ memory_level , func_memory_level ]
both_params_integers = all ( isinstance ( lvl , int ) for lvl in memory_levels )
both_params_none = all ( lvl is None for lvl in memory_levels )
if not ( both_params_integers or both_params_none ) :
    raise ValueError ( 'Reference and user memory levels must be both None ' 'or both integers.' )
if memory is not None and ( func_memory_level is None or memory_level >= func_memory_level ) :
    if isinstance ( memory , _basestring ) :
        memory = Memory ( cachedir = memory , verbose = verbose )
    if not isinstance ( memory , MEMORY_CLASSES ) :
        raise TypeError ( "'memory' argument must be a string or a " "joblib.Memory object. " "%s %s was given." % ( memory , type ( memory ) ) )
    if ( memory . cachedir is None and memory_level is not None and memory_level > 1 ) :
        warnings . warn ( "Caching has been enabled (memory_level = %d) " "but no Memory object or path has been provided" " (parameter memory). Caching deactivated for " "function %s." % ( memory_level , func . __name__ ) , stacklevel = 2 )
else :
    memory = Memory ( cachedir = None , verbose = verbose )
return _safe_cache ( memory , func , ** kwargs )


signature:
def _cache ( self , func , func_memory_level = 1 , ** kwargs ) :
    """Return a joblib . Memory object .
 The memory _ level determines the level above which the wrapped
 function output is cached . By specifying a numeric value for
 this level , the user can to control the amount of cache memory
 used . This function will cache the function call or not
 depending on the cache level .
 Parameters
 func : function
 The function the output of which is to be cached .
 memory _ level : int
 The memory _ level from which caching must be enabled for the wrapped
 function .
 Returns
 mem : joblib . Memory
 object that wraps the function func . This object may be
 a no - op , if the requested level is lower than the value given
 to _ cache ( ) ) . For consistency , a joblib . Memory object is always
 returned ."""

implementation:
verbose = getattr ( self , 'verbose' , 0 )
# Creates attributes if they don ' t exist
# This is to make creating them in _ _ init _ _ ( ) optional .
if not hasattr ( self , "memory_level" ) :
    self . memory_level = 0
if not hasattr ( self , "memory" ) :
    self . memory = Memory ( cachedir = None , verbose = verbose )
if isinstance ( self . memory , _basestring ) :
    self . memory = Memory ( cachedir = self . memory , verbose = verbose )
# If cache level is 0 but a memory object has been provided , set
# memory _ level to 1 with a warning .
if self . memory_level == 0 :
    if ( isinstance ( self . memory , _basestring ) or self . memory . cachedir is not None ) :
        warnings . warn ( "memory_level is currently set to 0 but " "a Memory object has been provided. " "Setting memory_level to 1." )
        self . memory_level = 1
return cache ( func , self . memory , func_memory_level = func_memory_level , memory_level = self . memory_level , ** kwargs )


signature:
def save_niigz ( filepath , vol , header = None , affine = None ) :
    """Saves a volume into a Nifti ( . nii . gz ) file .
 Parameters
 vol : Numpy 3D or 4D array
 Volume with the data to be saved .
 file _ path : string
 Output file name path
 affine : ( optional ) 4x4 Numpy array
 Array with the affine transform of the file .
 This is needed if vol is a np . ndarray .
 header : ( optional ) nibabel . nifti1 . Nifti1Header , optional
 Header for the file , optional but recommended .
 This is needed if vol is a np . ndarray .
 Note
 affine and header only work for numpy volumes ."""

implementation:
# delayed import because could not install nipy on Python 3 on OSX
we_have_nipy = False
try :
    import nipy . core . image as niim
    from nipy import save_image
except :
    pass
else :
    we_have_nipy = True
if isinstance ( vol , np . ndarray ) :
    log . debug ( 'Saving numpy nifti file: {}.' . format ( filepath ) )
    ni = nib . Nifti1Image ( vol , affine , header )
    nib . save ( ni , filepath )
elif isinstance ( vol , nib . Nifti1Image ) :
    log . debug ( 'Saving nibabel nifti file: {}.' . format ( filepath ) )
    nib . save ( vol , filepath )
elif we_have_nipy and isinstance ( vol , niim . Image ) :
    log . debug ( 'Saving nipy nifti file: {}.' . format ( filepath ) )
    save_image ( vol , filepath )
# elif isinstance ( vol , NeuroImage ) :
# log . debug ( ' Saving boyle . NeuroImage nifti file : { } . ' . format ( filepath ) )
# nib . save ( vol . img , filepath )
else :
    raise ValueError ( 'Could not recognise input vol filetype. Got: {}.' . format ( repr_imgs ( vol ) ) )


signature:
def spatialimg_to_hdfgroup ( h5group , spatial_img ) :
    """Saves a Nifti1Image into an HDF5 group .
 Parameters
 h5group : h5py Group
 Output HDF5 file path
 spatial _ img : nibabel SpatialImage
 Image to be saved
 h5path : str
 HDF5 group path where the image data will be saved .
 Datasets will be created inside the given group path :
 ' data ' , ' extra ' , ' affine ' , the header information will
 be set as attributes of the ' data ' dataset ."""

implementation:
try :
    h5group [ 'data' ] = spatial_img . get_data ( )
    h5group [ 'affine' ] = spatial_img . get_affine ( )
    if hasattr ( h5group , 'get_extra' ) :
        h5group [ 'extra' ] = spatial_img . get_extra ( )
    hdr = spatial_img . get_header ( )
    for k in list ( hdr . keys ( ) ) :
        h5group [ 'data' ] . attrs [ k ] = hdr [ k ]
except ValueError as ve :
    raise Exception ( 'Error creating group ' + h5group . name ) from ve


signature:
def spatialimg_to_hdfpath ( file_path , spatial_img , h5path = None , append = True ) :
    """Saves a Nifti1Image into an HDF5 file .
 Parameters
 file _ path : string
 Output HDF5 file path
 spatial _ img : nibabel SpatialImage
 Image to be saved
 h5path : string
 HDF5 group path where the image data will be saved .
 Datasets will be created inside the given group path :
 ' data ' , ' extra ' , ' affine ' , the header information will
 be set as attributes of the ' data ' dataset .
 Default : ' / img '
 append : bool
 True if you don ' t want to erase the content of the file
 if it already exists , False otherwise .
 Note
 HDF5 open modes
 > > > ' 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 )"""

implementation:
if h5path is None :
    h5path = '/img'
mode = 'w'
if os . path . exists ( file_path ) :
    if append :
        mode = 'a'
with h5py . File ( file_path , mode ) as f :
    try :
        h5img = f . create_group ( h5path )
        spatialimg_to_hdfgroup ( h5img , spatial_img )
    except ValueError as ve :
        raise Exception ( 'Error creating group ' + h5path ) from ve


signature:
def hdfpath_to_nifti1image ( file_path , h5path ) :
    """Returns a nibabel Nifti1Image from a HDF5 group datasets
 Parameters
 file _ path : string
 HDF5 file path
 h5path :
 HDF5 group path in file _ path
 Returns
 nibabel Nifti1Image"""

implementation:
with h5py . File ( file_path , 'r' ) as f :
    return hdfgroup_to_nifti1image ( f [ h5path ] )


signature:
def hdfgroup_to_nifti1image ( h5group ) :
    """Returns a nibabel Nifti1Image from a HDF5 group datasets
 Parameters
 h5group : h5py . Group
 HDF5 group
 Returns
 nibabel Nifti1Image"""

implementation:
try :
    data = h5group [ 'data' ] [ : ]
    affine = h5group [ 'affine' ] [ : ]
    extra = None
    if 'extra' in h5group :
        extra = h5group [ 'extra' ] [ : ]
    header = get_nifti1hdr_from_h5attrs ( h5group [ 'data' ] . attrs )
    img = nib . Nifti1Image ( data , affine , header = header , extra = extra )
    return img
except KeyError as ke :
    raise Exception ( 'Could not read Nifti1Image datasets from ' + h5group . name ) from ke


signature:
def get_nifti1hdr_from_h5attrs ( h5attrs ) :
    """Transforms an H5py Attributes set to a dict .
 Converts unicode string keys into standard strings
 and each value into a numpy array .
 Parameters
 h5attrs : H5py Attributes
 Returns
 dict"""

implementation:
hdr = nib . Nifti1Header ( )
for k in list ( h5attrs . keys ( ) ) :
    hdr [ str ( k ) ] = np . array ( h5attrs [ k ] )
return hdr


signature:
def all_childnodes_to_nifti1img ( h5group ) :
    """Returns in a list all images found under h5group .
 Parameters
 h5group : h5py . Group
 HDF group
 Returns
 list of nifti1Image"""

implementation:
child_nodes = [ ]
def append_parent_if_dataset ( name , obj ) :
    if isinstance ( obj , h5py . Dataset ) :
        if name . split ( '/' ) [ - 1 ] == 'data' :
            child_nodes . append ( obj . parent )
vols = [ ]
h5group . visititems ( append_parent_if_dataset )
for c in child_nodes :
    vols . append ( hdfgroup_to_nifti1image ( c ) )
return vols


signature:
def treefall ( iterable ) :
    """Generate all combinations of the elements of iterable and its subsets .
 Parameters
 iterable : list , set or dict or any iterable object
 Returns
 A generator of all possible combinations of the iterable .
 Example :
 > > > for i in treefall ( [ 1 , 2 , 3 , 4 , 5 ] ) : print ( i )
 > > > ( 1 , 2 , 3)
 > > > ( 1 , 2)
 > > > ( 1 , 3)
 > > > ( 2 , 3)"""

implementation:
num_elems = len ( iterable )
for i in range ( num_elems , - 1 , - 1 ) :
    for c in combinations ( iterable , i ) :
        yield c


signature:
def get_reliabledictionary_list ( client , application_name , service_name ) :
    """List existing reliable dictionaries .
 List existing reliable dictionaries and respective schema for given application and service .
 : param application _ name : Name of the application .
 : type application _ name : str
 : param service _ name : Name of the service .
 : type service _ name : str"""

implementation:
cluster = Cluster . from_sfclient ( client )
service = cluster . get_application ( application_name ) . get_service ( service_name )
for dictionary in service . get_dictionaries ( ) :
    print ( dictionary . name )


signature:
def get_reliabledictionary_schema ( client , application_name , service_name , dictionary_name , output_file = None ) :
    """Query Schema information for existing reliable dictionaries .
 Query Schema information existing reliable dictionaries for given application and service .
 : param application _ name : Name of the application .
 : type application _ name : str
 : param service _ name : Name of the service .
 : type service _ name : str
 : param dictionary : Name of the reliable dictionary .
 : type dictionary : str
 : param output _ file : Optional file to save the schema ."""

implementation:
cluster = Cluster . from_sfclient ( client )
dictionary = cluster . get_application ( application_name ) . get_service ( service_name ) . get_dictionary ( dictionary_name )
result = json . dumps ( dictionary . get_information ( ) , indent = 4 )
if ( output_file == None ) :
    output_file = "{}-{}-{}-schema-output.json" . format ( application_name , service_name , dictionary_name )
with open ( output_file , "w" ) as output :
    output . write ( result )
print ( 'Printed schema information to: ' + output_file )
print ( result )