return '<STR_LIT>'.join([s.capitalize() for s in type_.split('<STR_LIT>')])<EOL>

Normalizes the type passed to the api by capitalizing each part
        of the type. For example:

        sysctl::value -> Sysctl::Value
        user -> User


def _query(self, endpoint, path=None, query=None,<EOL>order_by=None, limit=None, offset=None, include_total=False,<EOL>summarize_by=None, count_by=None, count_filter=None,<EOL>request_method='<STR_LIT:GET>'):

log.debug('<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'.format(endpoint, path, query, limit,<EOL>offset, summarize_by, count_by,<EOL>count_filter))<EOL>url = self._url(endpoint, path=path)<EOL>payload = {}<EOL>if query is not None:<EOL><INDENT>payload['<STR_LIT>'] = query<EOL><DEDENT>if order_by is not None:<EOL><INDENT>payload[PARAMETERS['<STR_LIT>']] = order_by<EOL><DEDENT>if limit is not None:<EOL><INDENT>payload['<STR_LIT>'] = limit<EOL><DEDENT>if include_total is True:<EOL><INDENT>payload[PARAMETERS['<STR_LIT>']] =json.dumps(include_total)<EOL><DEDENT>if offset is not None:<EOL><INDENT>payload['<STR_LIT>'] = offset<EOL><DEDENT>if summarize_by is not None:<EOL><INDENT>payload[PARAMETERS['<STR_LIT>']] = summarize_by<EOL><DEDENT>if count_by is not None:<EOL><INDENT>payload[PARAMETERS['<STR_LIT>']] = count_by<EOL><DEDENT>if count_filter is not None:<EOL><INDENT>payload[PARAMETERS['<STR_LIT>']] = count_filter<EOL><DEDENT>if not (payload):<EOL><INDENT>payload = None<EOL><DEDENT>if not self.token:<EOL><INDENT>auth = (self.username, self.password)<EOL><DEDENT>else:<EOL><INDENT>auth = None<EOL><DEDENT>try:<EOL><INDENT>if request_method.upper() == '<STR_LIT:GET>':<EOL><INDENT>r = self._session.get(url, params=payload,<EOL>verify=self.ssl_verify,<EOL>cert=(self.ssl_cert, self.ssl_key),<EOL>timeout=self.timeout,<EOL>auth=auth)<EOL><DEDENT>elif request_method.upper() == '<STR_LIT:POST>':<EOL><INDENT>r = self._session.post(url,<EOL>data=json.dumps(payload, default=str),<EOL>verify=self.ssl_verify,<EOL>cert=(self.ssl_cert, self.ssl_key),<EOL>timeout=self.timeout,<EOL>auth=auth)<EOL><DEDENT>else:<EOL><INDENT>log.error("<STR_LIT>".format(<EOL>request_method))<EOL>raise APIError<EOL><DEDENT>r.raise_for_status()<EOL>if '<STR_LIT>' in r.headers:<EOL><INDENT>self.last_total = r.headers['<STR_LIT>']<EOL><DEDENT>else:<EOL><INDENT>self.last_total = None<EOL><DEDENT>json_body = r.json()<EOL>if json_body is not None:<EOL><INDENT>return json_body<EOL><DEDENT>else:<EOL><INDENT>del json_body<EOL>raise EmptyResponseError<EOL><DEDENT><DEDENT>except requests.exceptions.Timeout:<EOL><INDENT>log.error("<STR_LIT>".format(ERROR_STRINGS['<STR_LIT>'],<EOL>self.host, self.port,<EOL>self.protocol.upper()))<EOL>raise<EOL><DEDENT>except requests.exceptions.ConnectionError:<EOL><INDENT>log.error("<STR_LIT>".format(ERROR_STRINGS['<STR_LIT>'],<EOL>self.host, self.port,<EOL>self.protocol.upper()))<EOL>raise<EOL><DEDENT>except requests.exceptions.HTTPError as err:<EOL><INDENT>log.error("<STR_LIT>".format(err.response.text,<EOL>self.host, self.port,<EOL>self.protocol.upper()))<EOL>raise<EOL><DEDENT>

This method actually querries PuppetDB. Provided an endpoint and an
        optional path and/or query it will fire a request at PuppetDB. If
        PuppetDB can be reached and answers within the timeout we'll decode
        the response and give it back or raise for the HTTP Status Code
        PuppetDB gave back.

        :param endpoint: The PuppetDB API endpoint we want to query.
        :type endpoint: :obj:`string`
        :param path: An additional path if we don't wish to query the\
                bare endpoint.
        :type path: :obj:`string`
        :param query: (optional) A query to further narrow down the resultset.
        :type query: :obj:`string`
        :param order_by: (optional) Set the order parameters for the resultset.
        :type order_by: :obj:`string`
        :param limit: (optional) Tell PuppetDB to limit it's response to this\
                number of objects.
        :type limit: :obj:`int`
        :param offset: (optional) Tell PuppetDB to start it's response from\
                the given offset. This is useful for implementing pagination\
                but is not supported just yet.
        :type offset: :obj:`string`
        :param include_total: (optional) Include the total number of results
        :type order_by: :obj:`bool`
        :param summarize_by: (optional) Specify what type of object you'd like\
                to see counts at the event-counts and aggregate-event-counts \
                endpoints
        :type summarize_by: :obj:`string`
        :param count_by: (optional) Specify what type of object is counted
        :type count_by: :obj:`string`
        :param count_filter: (optional) Specify a filter for the results
        :type count_filter: :obj:`string`

        :raises: :class:`~pypuppetdb.errors.EmptyResponseError`

        :returns: The decoded response from PuppetDB
        :rtype: :obj:`dict` or :obj:`list`


def event_counts(self, summarize_by, **kwargs):

return self._query('<STR_LIT>',<EOL>summarize_by=summarize_by,<EOL>**kwargs)<EOL>

Get event counts from puppetdb.

        :param summarize_by: (Required) The object type to be counted on.
                             Valid values are 'containing_class', 'resource'
                             and 'certname'.
        :type summarize_by: :obj:`string`
        :param count_by: (Optional) The object type that is counted when
                         building the counts of 'successes', 'failures',
                         'noops' and 'skips'. Support values are 'certname'
                         and 'resource' (default)
        :type count_by: :obj:`string`
        :param count_filter: (Optional) A JSON query that is applied to the
                             event-counts output but before the results are
                             aggregated. Supported operators are `=`, `>`,
                             `<`, `>=`, and `<=`. Supported fields are
                             `failures`, `successes`, `noops`, and `skips`.
        :type count_filter: :obj:`string`
        :param \*\*kwargs: The rest of the keyword arguments are passed
                           to the _query function.

        :returns: A list of dictionaries containing the results.
        :rtype: :obj:`list`


return self._query('<STR_LIT:version>')['<STR_LIT:version>']<EOL>

Get version information about the running PuppetDB server.

        :returns: A string representation of the PuppetDB version.
        :rtype: :obj:`string`


return self._query('<STR_LIT>')<EOL>


log.debug('<STR_LIT>'.format(<EOL>endpoint, path))<EOL>try:<EOL><INDENT>endpoint = ENDPOINTS[endpoint]<EOL><DEDENT>except KeyError:<EOL><INDENT>raise APIError<EOL><DEDENT>url = '<STR_LIT>'.format(<EOL>base_url=self.base_url,<EOL>endpoint=endpoint,<EOL>)<EOL>if path is not None:<EOL><INDENT>url = '<STR_LIT>'.format(url, quote(path))<EOL><DEDENT>return url<EOL>

The complete URL we will end up querying. Depending on the
        endpoint we pass in  this will result in different URL's with
        different prefixes.

        :param endpoint: The PuppetDB API endpoint we want to query.
        :type endpoint: :obj:`string`
        :param path: An additional path if we don't wish to query the\
                bare endpoint.
        :type path: :obj:`string`

        :returns: A URL constructed from :func:`base_url` with the\
                apropraite API version/prefix and the rest of the path added\
                to it.
        :rtype: :obj:`string`


def nodes(self, unreported=<NUM_LIT:2>, with_status=False, **kwargs):

nodes = self._query('<STR_LIT>', **kwargs)<EOL>now = datetime.datetime.utcnow()<EOL>if type(nodes) == dict:<EOL><INDENT>nodes = [nodes, ]<EOL><DEDENT>if with_status:<EOL><INDENT>latest_events = self.event_counts(<EOL>query=EqualsOperator("<STR_LIT>", True),<EOL>summarize_by='<STR_LIT>'<EOL>)<EOL><DEDENT>for node in nodes:<EOL><INDENT>node['<STR_LIT>'] = None<EOL>node['<STR_LIT>'] = None<EOL>if with_status:<EOL><INDENT>status = [s for s in latest_events<EOL>if s['<STR_LIT>']['<STR_LIT:title>'] == node['<STR_LIT>']]<EOL>try:<EOL><INDENT>node['<STR_LIT>'] = node['<STR_LIT>']<EOL>if status:<EOL><INDENT>node['<STR_LIT>'] = status[<NUM_LIT:0>]<EOL><DEDENT><DEDENT>except KeyError:<EOL><INDENT>if status:<EOL><INDENT>node['<STR_LIT>'] = status = status[<NUM_LIT:0>]<EOL>if status['<STR_LIT>'] > <NUM_LIT:0>:<EOL><INDENT>node['<STR_LIT>'] = '<STR_LIT>'<EOL><DEDENT>if status['<STR_LIT>'] > <NUM_LIT:0>:<EOL><INDENT>node['<STR_LIT>'] = '<STR_LIT>'<EOL><DEDENT>if status['<STR_LIT>'] > <NUM_LIT:0>:<EOL><INDENT>node['<STR_LIT>'] = '<STR_LIT>'<EOL><DEDENT><DEDENT>else:<EOL><INDENT>node['<STR_LIT>'] = '<STR_LIT>'<EOL><DEDENT><DEDENT>if node['<STR_LIT>'] is not None:<EOL><INDENT>try:<EOL><INDENT>last_report = json_to_datetime(<EOL>node['<STR_LIT>'])<EOL>last_report = last_report.replace(tzinfo=None)<EOL>unreported_border = now - timedelta(hours=unreported)<EOL>if last_report < unreported_border:<EOL><INDENT>delta = (now - last_report)<EOL>node['<STR_LIT>'] = True<EOL>node['<STR_LIT>'] = '<STR_LIT>'.format(<EOL>delta.days,<EOL>int(delta.seconds / <NUM_LIT>),<EOL>int((delta.seconds % <NUM_LIT>) / <NUM_LIT>)<EOL>)<EOL><DEDENT><DEDENT>except AttributeError:<EOL><INDENT>node['<STR_LIT>'] = True<EOL><DEDENT><DEDENT>if not node['<STR_LIT>']:<EOL><INDENT>node['<STR_LIT>'] = True<EOL><DEDENT><DEDENT>yield Node(self,<EOL>name=node['<STR_LIT>'],<EOL>deactivated=node['<STR_LIT>'],<EOL>expired=node['<STR_LIT>'],<EOL>report_timestamp=node['<STR_LIT>'],<EOL>catalog_timestamp=node['<STR_LIT>'],<EOL>facts_timestamp=node['<STR_LIT>'],<EOL>status_report=node['<STR_LIT>'],<EOL>noop=node.get('<STR_LIT>'),<EOL>noop_pending=node.get('<STR_LIT>'),<EOL>events=node['<STR_LIT>'],<EOL>unreported=node.get('<STR_LIT>'),<EOL>unreported_time=node.get('<STR_LIT>'),<EOL>report_environment=node['<STR_LIT>'],<EOL>catalog_environment=node['<STR_LIT>'],<EOL>facts_environment=node['<STR_LIT>'],<EOL>latest_report_hash=node.get('<STR_LIT>'),<EOL>cached_catalog_status=node.get('<STR_LIT>')<EOL>)<EOL><DEDENT>

Query for nodes by either name or query. If both aren't
        provided this will return a list of all nodes. This method
        also fetches the nodes status and event counts of the latest
        report from puppetdb.

        :param with_status: (optional) include the node status in the\
                           returned nodes
        :type with_status: :bool:
        :param unreported: (optional) amount of hours when a node gets
                           marked as unreported
        :type unreported: :obj:`None` or integer
        :param \*\*kwargs: The rest of the keyword arguments are passed
                           to the _query function

        :returns: A generator yieling Nodes.
        :rtype: :class:`pypuppetdb.types.Node`


return self.api_version<EOL>

The version of the API we're querying against.

        :returns: Current API version.
        :rtype: :obj:`string`


catalogs = self.catalogs(path=node)<EOL>return next(x for x in catalogs)<EOL>

Get the available catalog for a given node.

        :param node: (Required) The name of the PuppetDB node.
        :type: :obj:`string`

        :returns: An instance of Catalog
        :rtype: :class:`pypuppetdb.types.Catalog`


nodes = self.nodes(path=name)<EOL>return next(node for node in nodes)<EOL>

Gets a single node from PuppetDB.

        :param name: The name of the node search.
        :type name: :obj:`string`

        :return: An instance of Node
        :rtype: :class:`pypuppetdb.types.Node`


return self._query('<STR_LIT>', path=metric)<EOL>

Query for a specific metrc.

        :param metric: The name of the metric we want.
        :type metric: :obj:`string`

        :returns: The return of :meth:`~pypuppetdb.api.BaseAPI._query`.


if regex is None:<EOL><INDENT>regex = '<STR_LIT>'<EOL><DEDENT>files = glob(os.path.join(base_directory, regex))<EOL>for path, dirlist, filelist in os.walk(base_directory):<EOL><INDENT>for ignored in IGNORE:<EOL><INDENT>try:<EOL><INDENT>dirlist.remove(ignored)<EOL><DEDENT>except:<EOL><INDENT>pass<EOL><DEDENT><DEDENT>for dir_name in dirlist:<EOL><INDENT>files.extend(glob(os.path.join(path, dir_name, regex)))<EOL><DEDENT><DEDENT>return files<EOL>

Uses glob to find all files that match the regex in base_directory.

    @param base_directory: string

    @param regex: string

    @return: set


install_deps = []<EOL>try:<EOL><INDENT>for fpath in args:<EOL><INDENT>install_deps.extend([str(d.req or d.url) for d in parse_requirements(fpath)])<EOL><DEDENT><DEDENT>except:<EOL><INDENT>print('<STR_LIT>'.format(fpath))<EOL><DEDENT>return [dep for dep in install_deps if dep != '<STR_LIT:None>']<EOL>

Parse all requirements files given and return a list of the dependencies


xl_path, choice = _check_xl_path(xl_path)<EOL>reader = XL_READERS[choice]<EOL>return reader(xl_path)<EOL>

Return the workbook from the Excel file in `xl_path`.


def concat_sheets(xl_path: str, sheetnames=None, add_tab_names=False):

xl_path, choice = _check_xl_path(xl_path)<EOL>if sheetnames is None:<EOL><INDENT>sheetnames = get_sheet_list(xl_path)<EOL><DEDENT>sheets = pd.read_excel(xl_path, sheetname=sheetnames)<EOL>if add_tab_names:<EOL><INDENT>for tab in sheets:<EOL><INDENT>sheets[tab]['<STR_LIT>'] = [tab] * len(sheets[tab])<EOL><DEDENT><DEDENT>return pd.concat([sheets[tab] for tab in sheets])<EOL>

Return a pandas DataFrame with the concat'ed
    content of the `sheetnames` from the Excel file in
    `xl_path`.

    Parameters
    ----------
    xl_path: str
        Path to the Excel file

    sheetnames: list of str
        List of existing sheet names of `xl_path`.
        If None, will use all sheets from `xl_path`.

    add_tab_names: bool
        If True will add a 'Tab' column which says from which
        tab the row comes from.

    Returns
    -------
    df: pandas.DataFrame


vals = pd.Series(values)<EOL>return vals[vals.duplicated()]<EOL>


for col in col_names:<EOL><INDENT>if not hasattr(df, col):<EOL><INDENT>raise AttributeError("<STR_LIT>".format(col,<EOL>df.columns))<EOL><DEDENT><DEDENT>

Raise an AttributeError if `df` does not have a column named as an item of
    the list of strings `col_names`.


try:<EOL><INDENT>wb = load_workbook(filename=xl_path, read_only=True)<EOL><DEDENT>except:<EOL><INDENT>raise<EOL><DEDENT>else:<EOL><INDENT>return wb<EOL><DEDENT>


def get_sheet_list(xl_path: str) -> List:

wb = read_xl(xl_path)<EOL>if hasattr(wb, '<STR_LIT>'):<EOL><INDENT>return wb.sheetnames<EOL><DEDENT>else:<EOL><INDENT>return wb.sheet_names()<EOL><DEDENT>

Return a list with the name of the sheets in
    the Excel file in `xl_path`.


_check_cols(df, [col_name])<EOL>if '<STR_LIT:O>' in df[col_name] or pd.np.issubdtype(df[col_name].dtype, str): <EOL><INDENT>return [nom.lower() for nom in df[pd.notnull(df)][col_name] if not pd.isnull(nom)]<EOL><DEDENT>else:<EOL><INDENT>return [nom for nom in df[pd.notnull(df)][col_name] if not pd.isnull(nom)]<EOL><DEDENT>

Return a list of not null values from the `col_name` column of `df`.


_check_cols(df, [col_name])<EOL>dups = df[pd.notnull(df[col_name]) & df.duplicated(subset=[col_name])]<EOL>return dups<EOL>

Return a DataFrame with the duplicated values of the column `col_name`
    in `df`.


return get_last_file(input_dir, glob_pattern, key=op.getctime)<EOL>

Return the path to the last created file in `input_dir`.
    See `get_last_file` docstring for description of the parameters.


files = glob(op.join(base_directory, regex))<EOL>for path, dirlist, filelist in os.walk(base_directory):<EOL><INDENT>for dir_name in dirlist:<EOL><INDENT>files.extend(glob(op.join(path, dir_name, regex)))<EOL><DEDENT><DEDENT>return files<EOL>

Uses glob to find all files or folders that match the regex
starting from the base_directory.

Parameters
----------
base_directory: str

regex: str

Returns
-------
files: list


return glob(op.join(base_directory, regex))<EOL>

Uses glob to find all files that match the regex
in base_directory.

@param base_directory: string

@param regex: string

@return: set


file_list = os.listdir(file_dir)<EOL>file_list.sort()<EOL>if regex:<EOL><INDENT>file_list = search_list(file_list, regex)<EOL><DEDENT>file_list = [op.join(file_dir, fname) for fname in file_list]<EOL>return file_list<EOL>

Creates a list of files that match the search_regex within file_dir.
The list of files will have file_dir as path prefix.

Parameters
----------
@param file_dir:

@param search_regex:

Returns:
--------
List of paths to files that match the search_regex


outlist = []<EOL>for root, dirs, files in os.walk(folder_path):<EOL><INDENT>outlist.extend([op.join(root, f) for f in dirs<EOL>if re.match(regex, f)])<EOL><DEDENT>return outlist<EOL>

Returns absolute paths of folders that match the regex within folder_path and
all its children folders.

Note: The regex matching is done using the match function
of the re module.

Parameters
----------
folder_path: string

regex: string

Returns
-------
A list of strings.


outlist = []<EOL>for root, dirs, files in os.walk(folder_path):<EOL><INDENT>outlist.extend([op.join(root, f) for f in files<EOL>if re.match(regex, f)])<EOL><DEDENT>return outlist<EOL>

Returns absolute paths of files that match the regex within folder_path and
all its children folders.

Note: The regex matching is done using the match function
of the re module.

Parameters
----------
folder_path: string

regex: string

Returns
-------
A list of strings.


outlist = []<EOL>for root, dirs, files in os.walk(folder_path):<EOL><INDENT>outlist.extend([op.join(root, f) for f in files<EOL>if re.search(regex, f)])<EOL><DEDENT>return outlist<EOL>

Returns absolute paths of files that match the regex within file_dir and
all its children folders.

Note: The regex matching is done using the search function
of the re module.

Parameters
----------
folder_path: string

regex: string

Returns
-------
A list of strings.


return commonprefix(self.get_node_filepaths(nodepath))<EOL>

Returns the common filepath between all leaves in the filetree.


return [fnom for fnom in file_lst if not fnom.startswith('<STR_LIT:.>')]<EOL>

Removes the filenames that start with '.'

:param file_lst: list of strings

:return: list of strings


if not op.isfile(filepath):<EOL><INDENT>raise IOError('<STR_LIT>'<EOL>'<STR_LIT>'.format(filepath))<EOL><DEDENT>cfg = import_pyfile(filepath)<EOL>if not hasattr(cfg, '<STR_LIT>'):<EOL><INDENT>raise KeyError('<STR_LIT>')<EOL><DEDENT>if not hasattr(cfg, '<STR_LIT>'):<EOL><INDENT>raise KeyError('<STR_LIT>')<EOL><DEDENT>return cfg.root_path, cfg.filetree<EOL>

Imports filetree and root_path variable values from the filepath.

:param filepath:
:return: root_path and filetree


return self._filetree.keys()<EOL>

Return a list of the names of the root nodes.


mydict = self._filetree if adict is None else adict<EOL>if isinstance(mydict, dict):<EOL><INDENT>for nom in mydict.keys():<EOL><INDENT>if isinstance(mydict[nom], dict):<EOL><INDENT>matchs = filter_list(mydict[nom], pattern)<EOL>for nom in matchs:<EOL><INDENT>mydict = self.remove_nodes(pattern, mydict[nom])<EOL>mydict.pop(nom)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>mydict[nom] = filter_list(mydict[nom], pattern)<EOL><DEDENT><DEDENT><DEDENT>else:<EOL><INDENT>matchs = set(filter_list(mydict, pattern))<EOL>mydict = set(mydict) - matchs<EOL><DEDENT>return mydict<EOL>

Remove the nodes that match the pattern.


if is_fnmatch_regex(pattern) and not is_regex(pattern):<EOL><INDENT>log.info('<STR_LIT>'.format(pattern))<EOL>filst = fnmatch.filter(lst, pattern)<EOL><DEDENT>else:<EOL><INDENT>log.info('<STR_LIT>'.format(pattern))<EOL>filst = match_list(lst, pattern)<EOL><DEDENT>if filst:<EOL><INDENT>filst.sort()<EOL><DEDENT>return filst<EOL>

Filters the lst using pattern.
If pattern starts with '(' it will be considered a re regular expression,
otherwise it will use fnmatch filter.

:param lst: list of strings

:param pattern: string

:return: list of strings
Filtered list of strings


if not path_regex:<EOL><INDENT>return []<EOL><DEDENT>if len(path_regex) < <NUM_LIT:1>:<EOL><INDENT>return []<EOL><DEDENT>if path_regex[<NUM_LIT:0>] == os.sep:<EOL><INDENT>path_regex = path_regex[<NUM_LIT:1>:]<EOL><DEDENT>rest_files = '<STR_LIT>'<EOL>if os.sep in path_regex:<EOL><INDENT>node_names = path_regex.partition(os.sep)<EOL>first_node = node_names[<NUM_LIT:0>]<EOL>rest_nodes = node_names[<NUM_LIT:2>]<EOL>folder_names = filter_list(os.listdir(base_path), first_node)<EOL>for nom in folder_names:<EOL><INDENT>new_base = op.join(base_path, nom)<EOL>if op.isdir(new_base):<EOL><INDENT>rest_files = get_possible_paths(new_base, rest_nodes)<EOL><DEDENT><DEDENT><DEDENT>else:<EOL><INDENT>rest_files = filter_list(os.listdir(base_path), path_regex)<EOL><DEDENT>files = []<EOL>if rest_files:<EOL><INDENT>files = [op.join(base_path, f) for f in rest_files]<EOL><DEDENT>return files<EOL>

Looks for path_regex within base_path. Each match is append
in the returned list.
path_regex may contain subfolder structure.
If any part of the folder structure is a

:param base_path: str

:param path_regex: str

:return list of strings


def populate_subtree(basepath, treemap, verbose=False):

file_nodes = OrderedDict()<EOL>if isinstance(treemap, tuple):<EOL><INDENT>try:<EOL><INDENT>file_nodes = process_tuple_node(basepath, treemap)<EOL><DEDENT>except:<EOL><INDENT>raise FileTreeMapError('<STR_LIT>'<EOL>'<STR_LIT>'.format(basepath, treemap))<EOL><DEDENT><DEDENT>if isinstance(treemap, list):<EOL><INDENT>for node in treemap:<EOL><INDENT>try:<EOL><INDENT>file_nodes.update(process_tuple_node(basepath, node))<EOL><DEDENT>except:<EOL><INDENT>raise FileTreeMapError('<STR_LIT>'<EOL>'<STR_LIT>'.format(basepath, node))<EOL><DEDENT><DEDENT><DEDENT>elif isinstance(treemap, dict):<EOL><INDENT>for k in treemap.keys():<EOL><INDENT>cname = k<EOL>child_map = treemap[k]<EOL>if isinstance(child_map, tuple) or isinstance(child_map, dict):<EOL><INDENT>try:<EOL><INDENT>file_nodes[cname] = populate_subtree(basepath, child_map)<EOL><DEDENT>except:<EOL><INDENT>raise FileTreeMapError('<STR_LIT>'<EOL>'<STR_LIT>'.format(basepath,<EOL>child_map))<EOL><DEDENT><DEDENT>elif isinstance(child_map, str):<EOL><INDENT>if child_map[<NUM_LIT:0>] == os.sep:<EOL><INDENT>raise FileTreeMapError('<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'.format(str(child_map),<EOL>os.sep))<EOL><DEDENT>subpaths = get_possible_paths(basepath, child_map)<EOL>if subpaths:<EOL><INDENT>file_nodes[cname] = subpaths<EOL><DEDENT><DEDENT><DEDENT><DEDENT>if verbose:<EOL><INDENT>log.info('<STR_LIT>'.format(basepath, file_nodes.keys()))<EOL><DEDENT>return file_nodes<EOL>

:param path: str

:param treemap: dict

:return: dict


result = []<EOL>if isinstance(data, dict):<EOL><INDENT>for item in data.values():<EOL><INDENT>result.extend(get_dict_leaves(item))<EOL><DEDENT><DEDENT>elif isinstance(data, list):<EOL><INDENT>result.extend(data)<EOL><DEDENT>else:<EOL><INDENT>result.append(data)<EOL><DEDENT>return result<EOL>

Given a nested dictionary, this returns all its leave elements in a list.

:param adict:

:return: list


if not overwrite:<EOL><INDENT>while op.exists(dirpath):<EOL><INDENT>dirpath += '<STR_LIT:+>'<EOL><DEDENT><DEDENT>os.makedirs(dirpath, exist_ok=overwrite)<EOL>return dirpath<EOL>

Will create dirpath folder. If dirpath already exists and overwrite is False,
        will append a '+' suffix to dirpath until dirpath does not exist.


files = self.get_node(nodepath)<EOL>return get_dict_leaves(files)<EOL>


def count_node_match(self, pattern, adict=None):

mydict = self._filetree if adict is None else adict<EOL>k = <NUM_LIT:0><EOL>if isinstance(mydict, dict):<EOL><INDENT>names = mydict.keys()<EOL>k += len(filter_list(names, pattern))<EOL>for nom in names:<EOL><INDENT>k += self.count_node_match(pattern, mydict[nom])<EOL><DEDENT><DEDENT>else:<EOL><INDENT>k = len(filter_list(mydict, pattern))<EOL><DEDENT>return k<EOL>

Return the number of nodes that match the pattern.

:param pattern:

:param adict:
:return: int


for key in config_map:<EOL><INDENT>if config_map[key] == '<STR_LIT:None>':<EOL><INDENT>config_map[key] = None<EOL><DEDENT>setattr(self, key, config_map[key])<EOL><DEDENT>


p = subprocess.Popen(['<STR_LIT>', '<STR_LIT>', filepath], stdout=subprocess.PIPE,<EOL>stderr=subprocess.PIPE)<EOL>result, err = p.communicate()<EOL>if p.returncode != <NUM_LIT:0>:<EOL><INDENT>raise IOError(err)<EOL><DEDENT>l = result.strip()<EOL>l = int(l.split()[<NUM_LIT:0>])<EOL>return l<EOL>

Returns the length of the file using the 'wc' GNU command

    Parameters
    ----------
    filepath: str

    Returns
    -------
    float


if not op.exists(folderpath):<EOL><INDENT>raise FolderNotFound(folderpath)<EOL><DEDENT>return op.abspath(folderpath)<EOL>

Returns the absolute path of folderpath.
    If the path does not exist, will raise IOError.


if not folder:<EOL><INDENT>for f in filelist:<EOL><INDENT>os.remove(f)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>for f in filelist:<EOL><INDENT>os.remove(op.join(folder, f))<EOL><DEDENT><DEDENT>

Deletes all files in filelist

    Parameters
    ----------
    filelist: list of str
        List of the file paths to be removed

    folder: str
        Path to be used as common directory for all file paths in filelist


file_obj.seek(<NUM_LIT:0>, os.SEEK_END)<EOL>return file_obj.tell()<EOL>

Returns the length of the size of the file

    Parameters
    ----------
    file_obj: file-like object

    Returns
    -------
    float


def create_subjects_file(filelist, labels, output_file, split='<STR_LIT::>'):

if len(filelist) != len(labels):<EOL><INDENT>raise ValueError('<STR_LIT>'<EOL>'<STR_LIT>'.format(len(filelist), len(labels)))<EOL><DEDENT>lines = []<EOL>for i, subj in enumerate(filelist):<EOL><INDENT>lab  = labels[i]<EOL>line = subj + split + str(lab)<EOL>lines.append(line)<EOL><DEDENT>lines = np.array(lines)<EOL>np.savetxt(output_file, lines, fmt='<STR_LIT:%s>')<EOL>

Creates a file where each line is <subject_file>:<subject_class_label>.

    Parameters
    ----------
    filelist: list of str
    List of filepaths

    labels: list of int, str or labels that can be transformed with str()
    List of labels

    output_file: str
    Output file path

    split: str
    Split character for each line


def parse_subjects_list(filepath, datadir='<STR_LIT>', split='<STR_LIT::>', labelsf=None):

labels = []<EOL>subjs  = []<EOL>if datadir:<EOL><INDENT>datadir += op.sep<EOL><DEDENT>with open(filepath, '<STR_LIT:r>') as f:<EOL><INDENT>for s in f:<EOL><INDENT>line = s.strip().split(split)<EOL>if len(line) == <NUM_LIT:2>:<EOL><INDENT>labels.append(np.float(line[<NUM_LIT:1>]))<EOL>subjf = line[<NUM_LIT:0>].strip()<EOL><DEDENT>else:<EOL><INDENT>subjf = line.strip()<EOL><DEDENT>if not op.isabs(subjf):<EOL><INDENT>subjs.append(datadir + subjf)<EOL><DEDENT>else:<EOL><INDENT>subjs.append(subjf)<EOL><DEDENT><DEDENT><DEDENT>if labelsf is not None:<EOL><INDENT>labels = np.loadtxt(labelsf)<EOL><DEDENT>return [labels, subjs]<EOL>

Parses a file with a list of: <subject_file>:<subject_class_label>.

    Parameters
    ----------
    filepath: str
    Path to file with a list of: <subject_file>:<subject_class_label>.
    Where ':' can be any split character

    datadir: str
    String to be path prefix of each line of the fname content,
    only in case the lines are relative file paths.

    split: str
    Split character for each line

    labelsf: str
    Path to file with a list of the labels if it is not included in
    fname. It will overwrite the labels from fname.

    Returns
    -------
    [labels, subjs] where labels is a list of labels and subjs a list of
    filepaths


if path[<NUM_LIT:0>] == op.sep:<EOL><INDENT>folder_depth += <NUM_LIT:1><EOL><DEDENT>return op.sep.join(path.split(op.sep)[<NUM_LIT:0>:folder_depth])<EOL>

Returns a folder path of path with depth given by folder_dept:

Parameters
----------
path: str

folder_depth: int > 0

Returns
-------
A folder path

Example
-------
>>> get_folder_subpath('/home/user/mydoc/work/notes.txt', 3)
>>> '/home/user/mydoc'


return tempfile.NamedTemporaryFile(dir=dirpath, suffix=suffix)<EOL>

Uses tempfile to create a NamedTemporaryFile using
the default arguments.

Parameters
----------
dirpath: str
Directory where it must be created.
If dir is specified, the file will be created
in that directory, otherwise, a default directory is used.
The default directory is chosen from a platform-dependent
list, but the user of the application can control the
directory location by setting the TMPDIR, TEMP or TMP
environment variables.

suffix: str
File name suffix.
It does not put a dot between the file name and the
suffix; if you need one, put it at the beginning of suffix.

Returns
-------
file object

Note
----
Please, close it once you have used the file.


def add_extension_if_needed(filepath, ext, check_if_exists=False):

if not filepath.endswith(ext):<EOL><INDENT>filepath += ext<EOL><DEDENT>if check_if_exists:<EOL><INDENT>if not op.exists(filepath):<EOL><INDENT>raise IOError('<STR_LIT>' + filepath)<EOL><DEDENT><DEDENT>return filepath<EOL>

Add the extension ext to fpath if it doesn't have it.

    Parameters
    ----------
    filepath: str
    File name or path

    ext: str
    File extension

    check_if_exists: bool

    Returns
    -------
    File name or path with extension added, if needed.


for line in open(filepath):<EOL><INDENT>if srch_str in line:<EOL><INDENT>return line<EOL><DEDENT><DEDENT>return None<EOL>

Return the first line in file defined by filepath
    that contains srch_str

    Parameters
    ----------
    srch_str: str

    filepath: str

    Returns
    ----------
    str


return [op.join(path, str(item)) for item in filelist]<EOL>

Joins path to each line in filelist

    Parameters
    ----------
    path: str

    filelist: list of str

    Returns
    -------
    list of filepaths


with open(filepath, '<STR_LIT:w>') as f:<EOL><INDENT>f.writelines(lines)<EOL><DEDENT>

Write the given lines to the file in filepath

    Parameters
    ----------
    filepath: str

    lines: list of str


if keys is None:<EOL><INDENT>keys = list(list_of_dicts[<NUM_LIT:0>].keys())<EOL><DEDENT>dict_of_lists = DefaultOrderedDict(list)<EOL>for d in list_of_dicts:<EOL><INDENT>for k in keys:<EOL><INDENT>dict_of_lists[k].append(d[k])<EOL><DEDENT><DEDENT>return dict_of_lists<EOL>

Return a dict of lists from a list of dicts with the same keys.
For each dict in list_of_dicts with look for the values of the
given keys and append it to the output dict.

Parameters
----------
list_of_dicts: list of dicts

keys: list of str
    List of keys to create in the output dict
    If None will use all keys in the first element of list_of_dicts
Returns
-------
DefaultOrderedDict of lists


def create_dataset(self, ds_name, data, attrs=None, dtype=None):

if ds_name in self._datasets:<EOL><INDENT>ds = self._datasets[ds_name]<EOL>if ds.dtype != data.dtype:<EOL><INDENT>warnings.warn('<STR_LIT>')<EOL><DEDENT><DEDENT>else:<EOL><INDENT>if dtype is None:<EOL><INDENT>dtype = data.dtype<EOL><DEDENT>ds = self._group.create_dataset(ds_name, data.shape,<EOL>dtype=dtype)<EOL>if attrs is not None:<EOL><INDENT>for key in attrs:<EOL><INDENT>setattr(ds.attrs, key, attrs[key])<EOL><DEDENT><DEDENT><DEDENT>ds.read_direct(data)<EOL>self._datasets[ds_name] = ds<EOL>return ds<EOL>

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


self._hdf_file.close()<EOL>if self._remove_on_destroy:<EOL><INDENT>os.remove(self._fname)<EOL><DEDENT>


return tempfile.NamedTemporaryFile(dir=dir, suffix=suffix)<EOL>

Uses tempfile to create a NamedTemporaryFile using
the default arguments.

@param dir: string
Directory where it must be created.
If dir is specified, the file will be created
in that directory, otherwise, a default directory is used.
The default directory is chosen from a platform-dependent
list, but the user of the application can control the
directory location by setting the TMPDIR, TEMP or TMP
environment variables.

@param suffix: string
File name suffix.
It does not put a dot between the file name and the
suffix; if you need one, put it at the beginning of suffix.

@return: file object

@note:
Close it once you have used the file.


node = self.get_node(key)<EOL>if node is None:<EOL><INDENT>raise KeyError('<STR_LIT>' % key)<EOL><DEDENT>if hasattr(node, '<STR_LIT>'):<EOL><INDENT>if '<STR_LIT>' in node.attrs:<EOL><INDENT>return self._read_group(node)<EOL><DEDENT><DEDENT>return self._read_array(node)<EOL>

Retrieve pandas object or group of Numpy ndarrays
stored in file

Parameters
----------
key : object

Returns
-------
obj : type of object stored in file


def create_empty_dataset(self, ds_name, dtype=np.float32):

if ds_name in self._datasets:<EOL><INDENT>return self._datasets[ds_name]<EOL><DEDENT>ds = self._group.create_dataset(ds_name, (<NUM_LIT:1>, <NUM_LIT:1>), maxshape=None,<EOL>dtype=dtype)<EOL>self._datasets[ds_name] = ds<EOL>return ds<EOL>

Creates a Dataset with unknown size.
Resize it before using.

:param ds_name: string

:param dtype: dtype
Datatype of the dataset

:return: h5py DataSet


@staticmethod<EOL><INDENT>def _fill_missing_values(df, range_values, fill_value=<NUM_LIT:0>, fill_method=None):<DEDENT>

idx_colnames  = df.index.names<EOL>idx_colranges = [range_values[x] for x in idx_colnames]<EOL>fullindex = pd.Index([p for p in product(*idx_colranges)],<EOL>name=tuple(idx_colnames))<EOL>fulldf = df.reindex(index=fullindex, fill_value=fill_value,<EOL>method=fill_method)<EOL>fulldf.index.names = idx_colnames<EOL>return fulldf, idx_colranges<EOL>

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


def put(self, key, value, attrs=None, format=None, append=False, **kwargs):

if not isinstance(value, np.ndarray):<EOL><INDENT>super(NumpyHDFStore, self).put(key, value, format, append, **kwargs)<EOL><DEDENT>else:<EOL><INDENT>group = self.get_node(key)<EOL>if group is not None and not append:<EOL><INDENT>self._handle.removeNode(group, recursive=True)<EOL>group = None<EOL><DEDENT>if group is None:<EOL><INDENT>paths = key.split('<STR_LIT:/>')<EOL>path = '<STR_LIT:/>'<EOL>for p in paths:<EOL><INDENT>if not len(p):<EOL><INDENT>continue<EOL><DEDENT>new_path = path<EOL>if not path.endswith('<STR_LIT:/>'):<EOL><INDENT>new_path += '<STR_LIT:/>'<EOL><DEDENT>new_path += p<EOL>group = self.get_node(new_path)<EOL>if group is None:<EOL><INDENT>group = self._handle.createGroup(path, p)<EOL><DEDENT>path = new_path<EOL><DEDENT><DEDENT>ds_name = kwargs.get('<STR_LIT>', self._array_dsname)<EOL>ds = self._handle.createArray(group, ds_name, value)<EOL>if attrs is not None:<EOL><INDENT>for key in attrs:<EOL><INDENT>setattr(ds.attrs, key, attrs[key])<EOL><DEDENT><DEDENT>self._handle.flush()<EOL>return ds<EOL><DEDENT>

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


mashelf = shelve.open(file_path, '<STR_LIT:n>')<EOL>for vn in variables.keys():<EOL><INDENT>try:<EOL><INDENT>mashelf[vn] = variables[vn]<EOL><DEDENT>except KeyError as ke:<EOL><INDENT>raise Exception('<STR_LIT>'.format(vn)) from ke<EOL><DEDENT><DEDENT>mashelf.close()<EOL>

Parameters
----------
file_path: str

variables: dict
    Dictionary with objects. Object name -> object

Notes
-----
    Before calling this function, create a varlist this way:

    shelfvars = []
    for v in varnames:
        shelfvars.append(eval(v))

    #to_restore variables from shelf
    my_shelf = shelve.open(filename)
    for key in my_shelf:
       globals()[key]=my_shelf[key]
    my_shelf.close()


ext = get_extension(filename).lower()<EOL>out_exts = {'<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>'}<EOL>output_file = filename<EOL>if not ext in out_exts:<EOL><INDENT>output_file = add_extension_if_needed(filename, '<STR_LIT>')<EOL>ext = get_extension(filename)<EOL><DEDENT>if ext == '<STR_LIT>' or ext == '<STR_LIT>':<EOL><INDENT>save_variables_to_shelve(output_file, variables)<EOL><DEDENT>elif ext == '<STR_LIT>':<EOL><INDENT>save_variables_to_mat(output_file, variables)<EOL><DEDENT>elif ext == '<STR_LIT>' or ext == '<STR_LIT>':<EOL><INDENT>from .hdf5 import save_variables_to_hdf5<EOL>save_variables_to_hdf5(output_file, variables)<EOL><DEDENT>else:<EOL><INDENT>raise ValueError('<STR_LIT>'.format(ext))<EOL><DEDENT>

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.


@staticmethod<EOL><INDENT>def save_varlist(filename, varnames, varlist):<DEDENT>

variables = {}<EOL>for i, vn in enumerate(varnames):<EOL><INDENT>variables[vn] = varlist[i]<EOL><DEDENT>ExportData.save_variables(filename, variables)<EOL>

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


return _get_node_names(h5file, h5path, node_type=h5py.Group)<EOL>

Return the groups names within h5file/h5path

    Parameters
    ----------
    h5file: h5py.File or path to hdf5 file
        HDF5 file object

    h5path: str
        HDF5 group path to get the group names from

    Returns
    -------
    groupnames: list of str
        List of group names


def _get_node_names(h5file, h5path='<STR_LIT:/>', node_type=h5py.Dataset):

if isinstance(h5file, str):<EOL><INDENT>_h5file = get_h5file(h5file, mode='<STR_LIT:r>')<EOL><DEDENT>else:<EOL><INDENT>_h5file = h5file<EOL><DEDENT>if not h5path.startswith('<STR_LIT:/>'):<EOL><INDENT>h5path = '<STR_LIT:/>' + h5path<EOL><DEDENT>names = []<EOL>try:<EOL><INDENT>h5group = _h5file.require_group(h5path)<EOL>for node in _hdf5_walk(h5group, node_type=node_type):<EOL><INDENT>names.append(node.name)<EOL><DEDENT><DEDENT>except:<EOL><INDENT>raise RuntimeError('<STR_LIT>'.format(_h5file.filename, h5path))<EOL><DEDENT>finally:<EOL><INDENT>if isinstance(h5file, str):<EOL><INDENT>_h5file.close()<EOL><DEDENT><DEDENT>return names<EOL>

Return the node of type node_type names within h5path of h5file.

    Parameters
    ----------
    h5file: h5py.File
        HDF5 file object

    h5path: str
        HDF5 group path to get the group names from

    node_type: h5py object type
        HDF5 object type

    Returns
    -------
    names: list of str
        List of names


if not op.exists(file_path):<EOL><INDENT>raise IOError('<STR_LIT>'.format(file_path))<EOL><DEDENT>try:<EOL><INDENT>h5file = h5py.File(file_path, mode=mode)<EOL><DEDENT>except:<EOL><INDENT>raise<EOL><DEDENT>else:<EOL><INDENT>return h5file<EOL><DEDENT>

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


def _get_nodes(h5file, h5path='<STR_LIT:/>', node_type=h5py.Dataset):

if isinstance(h5file, str):<EOL><INDENT>_h5file = get_h5file(h5file, mode='<STR_LIT:r>')<EOL><DEDENT>else:<EOL><INDENT>_h5file = h5file<EOL><DEDENT>if not h5path.startswith('<STR_LIT:/>'):<EOL><INDENT>h5path = '<STR_LIT:/>' + h5path<EOL><DEDENT>names = []<EOL>try:<EOL><INDENT>h5group = _h5file.require_group(h5path)<EOL>for node in _hdf5_walk(h5group, node_type=node_type):<EOL><INDENT>names.append(node)<EOL><DEDENT><DEDENT>except:<EOL><INDENT>raise RuntimeError('<STR_LIT>'.format(str(node_type), _h5file.filename, h5path))<EOL><DEDENT>finally:<EOL><INDENT>if isinstance(h5file, str):<EOL><INDENT>_h5file.close()<EOL><DEDENT><DEDENT>return names<EOL>

Return the nodes within h5path of the h5file.

    Parameters
    ----------
    h5file: h5py.File
        HDF5 file object

    h5path: str
        HDF5 group path to get the nodes from

    node_type: h5py object type
        The type of the nodes that you want to get

    Returns
    -------
    nodes: list of node_type objects


if not store_metadata:<EOL><INDENT>return lambda fpath: fpath<EOL><DEDENT>if header_fields is None:<EOL><INDENT>build_dcm = lambda fpath: DicomFile(fpath)<EOL><DEDENT>else:<EOL><INDENT>dicom_header = namedtuple('<STR_LIT>', header_fields)<EOL>build_dcm = lambda fpath: dicom_header._make(DicomFile(fpath).get_attributes(header_fields))<EOL><DEDENT>return build_dcm<EOL>

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


self.items = []<EOL>self._store_dicom_paths(folders)<EOL>

Restart the self.items and stores all dicom file paths found
within folders

Parameters
----------
folders: str or list of str


def from_set(self, fileset, check_if_dicoms=True):

if check_if_dicoms:<EOL><INDENT>self.items = []<EOL>for f in fileset:<EOL><INDENT>if is_dicom_file(f):<EOL><INDENT>self.items.append(f)<EOL><DEDENT><DEDENT><DEDENT>else:<EOL><INDENT>self.items = fileset<EOL><DEDENT>

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


def __init__(self, folders, read_metadata=True, header_fields=None):

DicomFileSet.__init__(self,  folders)<EOL>self.read_dcm = self.get_dcm_reader(read_metadata, header_fields)<EOL>

:param folders: str or list of strs
Path or paths to folders to be searched for Dicom files

:param read_metadata: bool
If True, will make a list of DicomFiles, otherwise will store
a simple DICOM header (namedtuples) with the fields specified
in header_fields.

:param header_fields: set of strings
Set of header fields to be read from each DICOM file in a DicomHeader.
If store_metadata is False, this won't be used. Else and if this is
None, will store the whole DicomFile.


file_lst.sort()<EOL>c = <NUM_LIT:1><EOL>for f in file_lst:<EOL><INDENT>dirname = get_abspath(f.dirname())<EOL>fdest = f.joinpath(dirname, "<STR_LIT>".format(c) +<EOL>OUTPUT_DICOM_EXTENSION)<EOL>log.info('<STR_LIT>'.format(f, fdest))<EOL>f.rename(fdest)<EOL>c += <NUM_LIT:1><EOL><DEDENT>

Will rename all files in file_lst to a padded serial
    number plus its extension

    :param file_lst: list of path.py paths


if isinstance(folders, str):<EOL><INDENT>folders = [folders]<EOL><DEDENT>for folder in folders:<EOL><INDENT>if not os.path.exists(folder):<EOL><INDENT>raise FolderNotFound(folder)<EOL><DEDENT>self.items.extend(list(find_all_dicom_files(folder)))<EOL><DEDENT>

Search for dicoms in folders and save file paths into
        self.dicom_paths set.

        :param folders: str or list of str


if not isinstance(dicomset, DicomFileSet):<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>self.items = list(set(self.items).update(dicomset))<EOL>

Update this set with the union of itself and dicomset.

        Parameters
        ----------
        dicomset: DicomFileSet


self.dcmf2 = self._read_dcmfile(dcm_file)<EOL>

Parameters
----------
dcm_file: str (path to file) or DicomFile or namedtuple


try:<EOL><INDENT>merged = merge_dict_of_lists(self.dicom_groups, indices,<EOL>pop_later=True, copy=True)<EOL>self.dicom_groups = merged<EOL><DEDENT>except IndexError:<EOL><INDENT>raise IndexError('<STR_LIT>')<EOL><DEDENT>

Extend the lists within the DICOM groups dictionary.
        The indices will indicate which list have to be extended by which
        other list.

        Parameters
        ----------
        indices: list or tuple of 2 iterables of int, bot having the same len
             The indices of the lists that have to be merged, both iterables
             items will be read pair by pair, the first is the index to the
             list that will be extended with the list of the second index.
             The indices can be constructed with Numpy e.g.,
             indices = np.where(square_matrix)


dist = SimpleDicomFileDistance(field_weights=header_fields)<EOL>path_list = dicom_file_paths.copy()<EOL>path_groups = DefaultOrderedDict(DicomFileSet)<EOL>while len(path_list) > <NUM_LIT:0>:<EOL><INDENT>file_path1 = path_list.pop()<EOL>file_subgroup = [file_path1]<EOL>dist.set_dicom_file1(file_path1)<EOL>j = len(path_list)-<NUM_LIT:1><EOL>while j >= <NUM_LIT:0>:<EOL><INDENT>file_path2 = path_list[j]<EOL>dist.set_dicom_file2(file_path2)<EOL>if dist.transform():<EOL><INDENT>file_subgroup.append(file_path2)<EOL>path_list.pop(j)<EOL><DEDENT>j -= <NUM_LIT:1><EOL><DEDENT>path_groups[file_path1].from_set(file_subgroup, check_if_dicoms=False)<EOL><DEDENT>return path_groups<EOL>

Gets a list of DICOM file absolute paths and returns a list of lists of
DICOM file paths. Each group contains a set of DICOM files that have
exactly the same headers.

Parameters
----------
dicom_file_paths: list of str
    List or set of DICOM file paths

header_fields: list of str
    List of header field names to check on the comparisons of the DICOM files.

Returns
-------
dict of DicomFileSets
    The key is one filepath representing the group (the first found).


self.set_dicom_file1(dcm_file1)<EOL>self.set_dicom_file2(dcm_file2)<EOL>

Parameters
----------
dcm_file1: str (path to file) or DicomFile or namedtuple

dcm_file2: str (path to file) or DicomFile or namedtuple


group_pairs = []<EOL>key_dicoms = list(self.dicom_groups.keys())<EOL>idx = len(key_dicoms)<EOL>while idx > <NUM_LIT:0>:<EOL><INDENT>group1 = key_dicoms.pop()<EOL>dir_group1 = get_folder_subpath(group1, folder_depth)<EOL>for group in key_dicoms:<EOL><INDENT>if group.startswith(dir_group1):<EOL><INDENT>group_pairs.append((group1, group))<EOL><DEDENT><DEDENT>idx -= <NUM_LIT:1><EOL><DEDENT>return group_pairs<EOL>

Returns a list of 2-tuples with pairs of dicom groups that
are in the same folder within given depth.

Parameters
----------
folder_depth: int
Path depth to check for folder equality.

Returns
-------
list of tuples of str


cln_flist = []<EOL>len_sorted = sorted(filepaths, key=len)<EOL>for idx, fpath in enumerate(len_sorted):<EOL><INDENT>remove = False<EOL>fname = op.basename(fpath)<EOL>rest  = len_sorted[idx+<NUM_LIT:1>:]<EOL>for rest_fpath in rest:<EOL><INDENT>rest_file = op.basename(rest_fpath)<EOL>if rest_file.endswith(fname):<EOL><INDENT>remove = True<EOL>break<EOL><DEDENT><DEDENT>if not remove:<EOL><INDENT>cln_flist.append(fpath)<EOL><DEDENT><DEDENT>return cln_flist<EOL>

Return a subset of `filepaths`. Keep only the files that have a basename longer than the
    others with same suffix.
    This works based on that dcm2nii appends a preffix character for each processing
    step it does automatically in the DICOM to NifTI conversion.

    Parameters
    ----------
    filepaths: iterable of str

    Returns
    -------
    cleaned_paths: iterable of str


def convert_dcm2nii(input_dir, output_dir, filename):

<EOL>if not op.exists(input_dir):<EOL><INDENT>raise IOError('<STR_LIT>'.format(input_dir))<EOL><DEDENT>if not op.exists(output_dir):<EOL><INDENT>raise IOError('<STR_LIT>'.format(output_dir))<EOL><DEDENT>tmpdir = tempfile.TemporaryDirectory(prefix='<STR_LIT>')<EOL>arguments = '<STR_LIT>'.format(tmpdir.name)<EOL>try:<EOL><INDENT>call_out = call_dcm2nii(input_dir, arguments)<EOL><DEDENT>except:<EOL><INDENT>raise<EOL><DEDENT>else:<EOL><INDENT>log.info('<STR_LIT>'.format(input_dir))<EOL>filenames  = glob(op.join(tmpdir.name, '<STR_LIT>'))<EOL>cleaned_filenames = remove_dcm2nii_underprocessed(filenames)<EOL>filepaths = []<EOL>for srcpath in cleaned_filenames:<EOL><INDENT>dstpath = op.join(output_dir, filename)<EOL>realpath = copy_w_plus(srcpath, dstpath)<EOL>filepaths.append(realpath)<EOL>basename = op.basename(remove_ext(srcpath))<EOL>aux_files = set(glob(op.join(tmpdir.name, '<STR_LIT>'     .format(basename)))) -set(glob(op.join(tmpdir.name, '<STR_LIT>'.format(basename))))<EOL>for aux_file in aux_files:<EOL><INDENT>aux_dstpath = copy_w_ext(aux_file, output_dir, remove_ext(op.basename(realpath)))<EOL>filepaths.append(aux_dstpath)<EOL><DEDENT><DEDENT>return filepaths<EOL><DEDENT>

Call MRICron's `dcm2nii` to convert the DICOM files inside `input_dir`
    to Nifti and save the Nifti file in `output_dir` with a `filename` prefix.

    Parameters
    ----------
    input_dir: str
        Path to the folder that contains the DICOM files

    output_dir: str
        Path to the folder where to save the NifTI file

    filename: str
        Output file basename

    Returns
    -------
    filepaths: list of str
        List of file paths created in `output_dir`.


num_elems = len(iterable)<EOL>for i in range(num_elems, -<NUM_LIT:1>, -<NUM_LIT:1>):<EOL><INDENT>for c in combinations(iterable, i):<EOL><INDENT>yield c<EOL><DEDENT><DEDENT>

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)
>>> (1,)
>>> (2,)
>>> (3,)
>>> ()


def get_attributes(self, attributes, default='<STR_LIT>'):

if isinstance(attributes, str):<EOL><INDENT>attributes = [attributes]<EOL><DEDENT>attrs = [getattr(self, attr, default) for attr in attributes]<EOL>if len(attrs) == <NUM_LIT:1>:<EOL><INDENT>return attrs[<NUM_LIT:0>]<EOL><DEDENT>return tuple(attrs)<EOL>

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.


def as_ndarray(arr, copy=False, dtype=None, order='<STR_LIT>'):

if order not in ('<STR_LIT:C>', '<STR_LIT:F>', '<STR_LIT:A>', '<STR_LIT>', None):<EOL><INDENT>raise ValueError("<STR_LIT>".format(str(order)))<EOL><DEDENT>if isinstance(arr, np.memmap):<EOL><INDENT>if dtype is None:<EOL><INDENT>if order in ('<STR_LIT>', '<STR_LIT:A>', None):<EOL><INDENT>ret = np.array(np.asarray(arr), copy=True)<EOL><DEDENT>else:<EOL><INDENT>ret = np.array(np.asarray(arr), copy=True, order=order)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>if order in ('<STR_LIT>', '<STR_LIT:A>', None):<EOL><INDENT>ret = np.asarray(arr).astype(dtype)<EOL><DEDENT>else:<EOL><INDENT>ret = _asarray(np.array(arr, copy=True), dtype=dtype, order=order)<EOL><DEDENT><DEDENT><DEDENT>elif isinstance(arr, np.ndarray):<EOL><INDENT>ret = _asarray(arr, dtype=dtype, order=order)<EOL>if np.may_share_memory(ret, arr) and copy:<EOL><INDENT>ret = ret.T.copy().T if ret.flags['<STR_LIT>'] else ret.copy()<EOL><DEDENT><DEDENT>elif isinstance(arr, (list, tuple)):<EOL><INDENT>if order in ("<STR_LIT:A>", "<STR_LIT>"):<EOL><INDENT>ret = np.asarray(arr, dtype=dtype)<EOL><DEDENT>else:<EOL><INDENT>ret = np.asarray(arr, dtype=dtype, order=order)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>raise ValueError("<STR_LIT>".format(arr.__class__))<EOL><DEDENT>return ret<EOL>

Convert an arbitrary array to numpy.ndarray.

    In the case of a memmap array, a copy is automatically made to break the
    link with the underlying file (whatever the value of the "copy" keyword).

    The purpose of this function is mainly to get rid of memmap objects, but
    it can be used for other purposes. In particular, combining copying and
    casting can lead to performance improvements in some cases, by avoiding
    unnecessary copies.

    If not specified, input array order is preserved, in all cases, even when
    a copy is requested.

    Caveat: this function does not copy during bool to/from 1-byte dtype
    conversions. This can lead to some surprising results in some rare cases.
    Example:

        a = numpy.asarray([0, 1, 2], dtype=numpy.int8)
        b = as_ndarray(a, dtype=bool)  # array([False, True, True], dtype=bool)
        c = as_ndarray(b, dtype=numpy.int8)  # array([0, 1, 2], dtype=numpy.int8)

    The usually expected result for the last line would be array([0, 1, 1])
    because True evaluates to 1. Since there is no copy made here, the original
    array is recovered.

    Parameters
    ----------
    arr: array-like
        input array. Any value accepted by numpy.asarray is valid.

    copy: bool
        if True, force a copy of the array. Always True when arr is a memmap.

    dtype: any numpy dtype
        dtype of the returned array. Performing copy and type conversion at the
        same time can in some cases avoid an additional copy.

    order: string
        gives the order of the returned array.
        Valid values are: "C", "F", "A", "K", None.
        default is "K". See ndarray.copy() for more information.

    Returns
    -------
    ret: np.ndarray
        Numpy array containing the same data as arr, always of class
        numpy.ndarray, and with no link to any underlying file.


if not hasattr(x, '<STR_LIT>') and not hasattr(x, '<STR_LIT>'):<EOL><INDENT>if hasattr(x, '<STR_LIT>'):<EOL><INDENT>x = np.asarray(x)<EOL><DEDENT>else:<EOL><INDENT>raise TypeError("<STR_LIT>" % x)<EOL><DEDENT><DEDENT>return x.shape[<NUM_LIT:0>] if hasattr(x, '<STR_LIT>') else len(x)<EOL>

Return number of samples in array-like x.


if not isinstance(estimator, str):<EOL><INDENT>estimator = estimator.__class__.__name__<EOL><DEDENT>if X.dtype.kind != '<STR_LIT:f>':<EOL><INDENT>warnings.warn("<STR_LIT>"<EOL>"<STR_LIT>" % (estimator, X.dtype))<EOL>return True<EOL><DEDENT>return False<EOL>

Warning utility function to check that data type is floating point.

    Returns True if a warning was raised (i.e. the input is not float) and
    False otherwise, for easier input validation.


def check_array(array, accept_sparse=None, dtype=None, order=None, copy=False,<EOL>force_all_finite=True, ensure_2d=True, allow_nd=False):

if isinstance(accept_sparse, str):<EOL><INDENT>accept_sparse = [accept_sparse]<EOL><DEDENT>if sp.issparse(array):<EOL><INDENT>array = _ensure_sparse_format(array, accept_sparse, dtype, order,<EOL>copy, force_all_finite)<EOL><DEDENT>else:<EOL><INDENT>if ensure_2d:<EOL><INDENT>array = np.atleast_2d(array)<EOL><DEDENT>array = np.array(array, dtype=dtype, order=order, copy=copy)<EOL>if not allow_nd and array.ndim >= <NUM_LIT:3>:<EOL><INDENT>raise ValueError("<STR_LIT>" %<EOL>array.ndim)<EOL><DEDENT>if force_all_finite:<EOL><INDENT>_assert_all_finite(array)<EOL><DEDENT><DEDENT>return array<EOL>

Input validation on an array, list, sparse matrix or similar.

    By default, the input is converted to an at least 2nd numpy array.

    Parameters
    ----------
    array : object
        Input object to check / convert.

    accept_sparse : string, list of string or None (default=None)
        String[s] representing allowed sparse matrix formats, such as 'csc',
        'csr', etc.  None means that sparse matrix input will raise an error.
        If the input is sparse but not in the allowed format, it will be
        converted to the first listed format.

    order : 'F', 'C' or None (default=None)
        Whether an array will be forced to be fortran or c-style.

    copy : boolean (default=False)
        Whether a forced copy will be triggered. If copy=False, a copy might
        be triggered by a conversion.

    force_all_finite : boolean (default=True)
        Whether to raise an error on np.inf and np.nan in X.

    ensure_2d : boolean (default=True)
        Whether to make X at least 2d.

    allow_nd : boolean (default=False)
        Whether to allow X.ndim > 2.

    Returns
    -------
    X_converted : object
        The converted and validated X.


<EOL>_assert_all_finite(X.data if sp.issparse(X) else X)<EOL>

Throw a ValueError if X contains NaN or infinity.

    Input MUST be an np.ndarray instance or a scipy.sparse matrix.


for v in values:<EOL><INDENT>string = string.replace(v, '<STR_LIT>')<EOL><DEDENT>return string<EOL>

Parameters
----------
string:
values:

Returns
-------


is_regex = False<EOL>regex_chars = ['<STR_LIT:!>', '<STR_LIT:*>', '<STR_LIT:$>']<EOL>for c in regex_chars:<EOL><INDENT>if string.find(c) > -<NUM_LIT:1>:<EOL><INDENT>return True<EOL><DEDENT><DEDENT>return is_regex<EOL>

Returns True if the given string is considered a fnmatch
regular expression, False otherwise.
It will look for

:param string: str


return [preffix + str(item) for item in lst]<EOL>

Parameters
----------
lst:
preffix:

Returns
-------


def match_list(lst, pattern, group_names=[]):

filtfn = re.compile(pattern).match<EOL>filtlst = filter_list(lst, filtfn)<EOL>if not group_names:<EOL><INDENT>return [m.string for m in filtlst]<EOL><DEDENT>else:<EOL><INDENT>return [m.group(group_names) for m in filtlst]<EOL><DEDENT>

Parameters
----------
lst: list of str

regex: string

group_names: list of strings
    See re.MatchObject group docstring

Returns
-------
list of strings
    Filtered list, with the strings that match the pattern


try:<EOL><INDENT>re.compile(string)<EOL>is_valid = True<EOL><DEDENT>except re.error:<EOL><INDENT>is_valid = False<EOL><DEDENT>return is_valid<EOL>

Checks whether the re module can compile the given regular expression.

Parameters
----------
string: str

Returns
-------
boolean


filt = re.compile(pattern).search<EOL>return filter_list(lst, filt)<EOL>

Parameters
----------
pattern: string

lst: list of strings

Returns
-------
filtered_list: list of str
    Filtered lists with the strings in which the pattern is found.


return dict((str(key), dict_1.get(key) or dict_2.get(key))<EOL>for key in set(dict_2) | set(dict_1))<EOL>

Merge two dictionaries.

    Values that evaluate to true take priority over falsy values.
    `dict_1` takes priority over `dict_2`.


def get_rcfile_variable_value(var_name, app_name, section_name=None):

cfg = get_rcfile_section(app_name, section_name)<EOL>if var_name in cfg:<EOL><INDENT>raise KeyError('<STR_LIT>'<EOL>'<STR_LIT>'.format(var_name, section_name))<EOL><DEDENT>return cfg[var_name]<EOL>

Return the value of the variable in the section_name section of the
    app_name rc file.

    Parameters
    ----------
    var_name: str
        Name of the variable to be searched for.

    section_name: str
        Name of the section in the rcfiles.

    app_name: str
        Name of the application to look for its rcfiles.

    Returns
    -------
    var_value: str
        The value of the variable with given var_name.


def rcfile(appname, section=None, args={}, strip_dashes=True):

if strip_dashes:<EOL><INDENT>for k in args.keys():<EOL><INDENT>args[k.lstrip('<STR_LIT:->')] = args.pop(k)<EOL><DEDENT><DEDENT>environ = get_environment(appname)<EOL>if section is None:<EOL><INDENT>section = appname<EOL><DEDENT>config = get_config(appname,<EOL>section,<EOL>args.get('<STR_LIT>', '<STR_LIT>'),<EOL>args.get('<STR_LIT:path>', '<STR_LIT>'))<EOL>config = merge(merge(args, config), environ)<EOL>if not config:<EOL><INDENT>raise IOError('<STR_LIT>'<EOL>'<STR_LIT>'.format(appname))<EOL><DEDENT>return config<EOL>

Read environment variables and config files and return them merged with
    predefined list of arguments.

    Parameters
    ----------
    appname: str
        Application name, used for config files and environment variable
        names.

    section: str
        Name of the section to be read. If this is not set: appname.

    args:
        arguments from command line (optparse, docopt, etc).

    strip_dashes: bool
        Strip dashes prefixing key names from args dict.

    Returns
    --------
    dict
        containing the merged variables of environment variables, config
        files and args.

    Raises
    ------
    IOError
        In case the return value is empty.

    Notes
    -----
    Environment variables are read if they start with appname in uppercase
    with underscore, for example:

        TEST_VAR=1

    Config files compatible with ConfigParser are read and the section name
    appname is read, example:

        [appname]
        var=1

    We can also have host-dependent configuration values, which have
    priority over the default appname values.

        [appname]
        var=1

        [appname:mylinux]
        var=3

    For boolean flags do not try to use: 'True' or 'False',
                                         'on' or 'off',
                                         '1' or '0'.
    Unless you are willing to parse this values by yourself.
    We recommend commenting the variables out with '#' if you want to set a
    flag to False and check if it is in the rcfile cfg dict, i.e.:

        flag_value = 'flag_variable' in cfg

    Files are read from: /etc/appname/config,
                         /etc/appfilerc,
                         ~/.config/appname/config,
                         ~/.config/appname,
                         ~/.appname/config,
                         ~/.appnamerc,
                         appnamerc,
                         .appnamerc,
                         appnamerc file found in 'path' folder variable in args,
                         .appnamerc file found in 'path' folder variable in args,
                         file provided by 'config' variable in args.

    Example
    -------
        args = rcfile(__name__, docopt(__doc__, version=__version__))


try:<EOL><INDENT>settings = rcfile(app_name, section_name)<EOL><DEDENT>except IOError:<EOL><INDENT>raise<EOL><DEDENT>except:<EOL><INDENT>raise KeyError('<STR_LIT>'<EOL>'<STR_LIT>'.format(section_name, app_name))<EOL><DEDENT>else:<EOL><INDENT>return settings<EOL><DEDENT>

Return the dictionary containing the rcfile section configuration
    variables.

    Parameters
    ----------
    section_name: str
        Name of the section in the rcfiles.

    app_name: str
        Name of the application to look for its rcfiles.

    Returns
    -------
    settings: dict
        Dict with variable values


def get_sys_path(rcpath, app_name, section_name=None):

<EOL>if op.exists(rcpath):<EOL><INDENT>return op.realpath(op.expanduser(rcpath))<EOL><DEDENT>try:<EOL><INDENT>settings = rcfile(app_name, section_name)<EOL><DEDENT>except:<EOL><INDENT>raise<EOL><DEDENT>try:<EOL><INDENT>sys_path = op.expanduser(settings[rcpath])<EOL><DEDENT>except KeyError:<EOL><INDENT>raise IOError('<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'.format(rcpath,<EOL>section_name,<EOL>app_name))<EOL><DEDENT>else:<EOL><INDENT>if not op.exists(sys_path):<EOL><INDENT>raise IOError('<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'.format(rcpath, section_name, app_name,<EOL>sys_path))<EOL><DEDENT>return op.realpath(op.expanduser(sys_path))<EOL><DEDENT>

Return a folder path if it exists.

    First will check if it is an existing system path, if it is, will return it
    expanded and absoluted.

    If this fails will look for the rcpath variable in the app_name rcfiles or
    exclusively within the given section_name, if given.

    Parameters
    ----------
    rcpath: str
        Existing folder path or variable name in app_name rcfile with an
        existing one.

    section_name: str
        Name of a section in the app_name rcfile to look exclusively there for
        variable names.

    app_name: str
        Name of the application to look for rcfile configuration files.

    Returns
    -------
    sys_path: str
        A expanded absolute file or folder path if the path exists.

    Raises
    ------
    IOError if the proposed sys_path does not exist.


def _cache(self, func, func_memory_level=<NUM_LIT:1>, **kwargs):

verbose = getattr(self, '<STR_LIT>', <NUM_LIT:0>)<EOL>if not hasattr(self, "<STR_LIT>"):<EOL><INDENT>self.memory_level = <NUM_LIT:0><EOL><DEDENT>if not hasattr(self, "<STR_LIT>"):<EOL><INDENT>self.memory = Memory(cachedir=None, verbose=verbose)<EOL><DEDENT>if isinstance(self.memory, _basestring):<EOL><INDENT>self.memory = Memory(cachedir=self.memory, verbose=verbose)<EOL><DEDENT>if self.memory_level == <NUM_LIT:0>:<EOL><INDENT>if (isinstance(self.memory, _basestring)<EOL>or self.memory.cachedir is not None):<EOL><INDENT>warnings.warn("<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>")<EOL>self.memory_level = <NUM_LIT:1><EOL><DEDENT><DEDENT>return cache(func, self.memory, func_memory_level=func_memory_level,<EOL>memory_level=self.memory_level, **kwargs)<EOL>

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.


p = subprocess.Popen(cmd_args, stdout=subprocess.PIPE)<EOL>(output, err) = p.communicate()<EOL>return output<EOL>

Calls the command

Parameters
----------
cmd_args: list of str
    Command name to call and its arguments in a list.

Returns
-------
Command output


return inspect.stack()[<NUM_LIT:2>][<NUM_LIT:3>]<EOL>


if not op.isabs(cmd_name):<EOL><INDENT>cmd_fullpath = which(cmd_name)<EOL><DEDENT>else:<EOL><INDENT>cmd_fullpath = cmd_name<EOL><DEDENT>try:<EOL><INDENT>cmd_line = [cmd_fullpath] + args_strings<EOL>log.info('<STR_LIT>'.format(cmd_line))<EOL>retval = subprocess.check_call(cmd_line)<EOL><DEDENT>except CalledProcessError as ce:<EOL><INDENT>log.exception("<STR_LIT>"<EOL>"<STR_LIT>".format(cmd_name, args_strings,<EOL>ce.returncode))<EOL>raise<EOL><DEDENT>else:<EOL><INDENT>return retval<EOL><DEDENT>

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.


return inspect.stack()[<NUM_LIT:1>][<NUM_LIT:3>]<EOL>


if (sys.version_info > (<NUM_LIT:3>, <NUM_LIT:0>)):<EOL><INDENT>return which_py3(program)<EOL><DEDENT>else:<EOL><INDENT>return which_py2(program)<EOL><DEDENT>

Returns the absolute path of the given CLI program name.


def check_compatibility(self, one_img, another_img=None):

if another_img is None:<EOL><INDENT>if len(self.items) > <NUM_LIT:0>:<EOL><INDENT>another_img = self.items[<NUM_LIT:0>]<EOL><DEDENT>else:<EOL><INDENT>raise ValueError('<STR_LIT>'<EOL>'<STR_LIT>'.format(repr_imgs(one_img)))<EOL><DEDENT><DEDENT>try:<EOL><INDENT>if self.all_compatible:<EOL><INDENT>check_img_compatibility(one_img, another_img)<EOL><DEDENT>if self.mask is not None:<EOL><INDENT>check_img_compatibility(one_img, self.mask, only_check_3d=True)<EOL><DEDENT><DEDENT>except:<EOL><INDENT>raise<EOL><DEDENT>

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.


for group_label in subj_files:<EOL><INDENT>try:<EOL><INDENT>group_files = subj_files[group_label]<EOL>self.items.extend([self._load_image(get_abspath(imgf)) for imgf in group_files])<EOL>self.labels.extend([group_label]*len(group_files))<EOL><DEDENT>except Exception as exc:<EOL><INDENT>raise Exception('<STR_LIT>'<EOL>'<STR_LIT>'.format(group_label)) from exc<EOL><DEDENT><DEDENT>

Parameters
----------
subj_files: dict of str
    file_path -> int/str


if not isinstance(labels, string_types) and len(labels) != self.n_subjs:<EOL><INDENT>raise ValueError('<STR_LIT>'<EOL>'<STR_LIT>'.format(len(labels), self.n_subjs))<EOL><DEDENT>self.labels = labels<EOL>

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


try:<EOL><INDENT>if isinstance(subj_files, list):<EOL><INDENT>self.from_list(subj_files)<EOL><DEDENT>elif isinstance(subj_files, dict):<EOL><INDENT>self.from_dict(subj_files)<EOL><DEDENT>else:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT><DEDENT>except Exception as exc:<EOL><INDENT>raise Exception('<STR_LIT>') from exc<EOL><DEDENT>

Parameters
----------
subj_files: list or dict of str
    file_path -> int/str


def to_matrix(self, smooth_fwhm=<NUM_LIT:0>, outdtype=None):

if not self.all_compatible:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>if not outdtype:<EOL><INDENT>outdtype = self.items[<NUM_LIT:0>].dtype<EOL><DEDENT>n_voxels     = None<EOL>mask_indices = None<EOL>mask_shape   = self.items[<NUM_LIT:0>].shape[:<NUM_LIT:3>]<EOL>if self.has_mask:<EOL><INDENT>mask_arr     = self.mask.get_data()<EOL>mask_indices = np.nonzero(mask_arr)<EOL>mask_shape   = self.mask.shape<EOL>n_voxels     = np.count_nonzero(mask_arr)<EOL><DEDENT>if n_voxels is None:<EOL><INDENT>log.debug('<STR_LIT>'.format(self.mask))<EOL>n_voxels     = np.prod(mask_shape)<EOL>mask_indices = None<EOL><DEDENT>ndims = self.items[<NUM_LIT:0>].ndim<EOL>if ndims == <NUM_LIT:3>:<EOL><INDENT>subj_flat_shape = (n_voxels, )<EOL><DEDENT>elif ndims == <NUM_LIT:4>:<EOL><INDENT>subj_flat_shape = (n_voxels, self.items[<NUM_LIT:0>].shape[<NUM_LIT:3>])<EOL><DEDENT>else:<EOL><INDENT>raise NotImplementedError('<STR_LIT>'<EOL>'<STR_LIT>'.format(ndims))<EOL><DEDENT>outmat = np.zeros((self.n_subjs, ) + subj_flat_shape, dtype=outdtype)<EOL>try:<EOL><INDENT>for i, image in enumerate(self.items):<EOL><INDENT>if smooth_fwhm > <NUM_LIT:0>:<EOL><INDENT>image.fwhm = smooth_fwhm<EOL><DEDENT>if self.has_mask:<EOL><INDENT>image.set_mask(self.mask)<EOL><DEDENT>outmat[i, :], _, _ = image.mask_and_flatten()<EOL>image.clear_data()<EOL><DEDENT><DEDENT>except Exception as exc:<EOL><INDENT>raise Exception('<STR_LIT>'.format(image)) from exc<EOL><DEDENT>else:<EOL><INDENT>return outmat, mask_indices, mask_shape<EOL><DEDENT>

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.


if image is None:<EOL><INDENT>self._mask = None<EOL><DEDENT>try:<EOL><INDENT>mask = load_mask(image)<EOL><DEDENT>except Exception as exc:<EOL><INDENT>raise Exception('<STR_LIT>'.format(image)) from exc<EOL><DEDENT>else:<EOL><INDENT>self._mask = mask<EOL><DEDENT>

self.mask setter

        Parameters
        ----------
        image: str or img-like object.
            See NeuroImage constructor docstring.


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

Returns in a list all images found under h5group.

    Parameters
    ----------
    h5group: h5py.Group
        HDF group

    Returns
    -------
    list of nifti1Image


hdr = nib.Nifti1Header()<EOL>for k in list(h5attrs.keys()):<EOL><INDENT>hdr[str(k)] = np.array(h5attrs[k])<EOL><DEDENT>return hdr<EOL>

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


try:<EOL><INDENT>data   = h5group['<STR_LIT:data>'][:]<EOL>affine = h5group['<STR_LIT>'][:]<EOL>extra = None<EOL>if '<STR_LIT>' in h5group:<EOL><INDENT>extra = h5group['<STR_LIT>'][:]<EOL><DEDENT>header = get_nifti1hdr_from_h5attrs(h5group['<STR_LIT:data>'].attrs)<EOL>img = nib.Nifti1Image(data, affine, header=header, extra=extra)<EOL>return img<EOL><DEDENT>except KeyError as ke:<EOL><INDENT>raise Exception('<STR_LIT>' + h5group.name) from ke<EOL><DEDENT>

Returns a nibabel Nifti1Image from a HDF5 group datasets

    Parameters
    ----------
    h5group: h5py.Group
        HDF5 group

    Returns
    -------
    nibabel Nifti1Image


def spatialimg_to_hdfpath(file_path, spatial_img, h5path=None, append=True):

if h5path is None:<EOL><INDENT>h5path = '<STR_LIT>'<EOL><DEDENT>mode = '<STR_LIT:w>'<EOL>if os.path.exists(file_path):<EOL><INDENT>if append:<EOL><INDENT>mode = '<STR_LIT:a>'<EOL><DEDENT><DEDENT>with h5py.File(file_path, mode) as f:<EOL><INDENT>try:<EOL><INDENT>h5img = f.create_group(h5path)<EOL>spatialimg_to_hdfgroup(h5img, spatial_img)<EOL><DEDENT>except ValueError as ve:<EOL><INDENT>raise Exception('<STR_LIT>' + h5path) from ve<EOL><DEDENT><DEDENT>

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)


def partition_timeseries(image, roi_img, mask_img=None, zeroe=True, roi_values=None, outdict=False):

img  = read_img(image)<EOL>rois = read_img(roi_img)<EOL>check_img_compatibility(img, rois, only_check_3d=True)<EOL>roi_data = rois.get_data()<EOL>if roi_values is not None:<EOL><INDENT>for rv in roi_values:<EOL><INDENT>if not np.any(roi_data == rv):<EOL><INDENT>raise ValueError('<STR_LIT>'.format(rv, repr_imgs(roi_img)))<EOL><DEDENT><DEDENT><DEDENT>else:<EOL><INDENT>roi_values = get_unique_nonzeros(roi_data)<EOL><DEDENT>if mask_img is None:<EOL><INDENT>mask_data = None<EOL><DEDENT>else:<EOL><INDENT>mask = load_mask(mask_img)<EOL>check_img_compatibility(img, mask, only_check_3d=True)<EOL>mask_data = mask.get_data()<EOL><DEDENT>if outdict:<EOL><INDENT>extract_data = _extract_timeseries_dict<EOL><DEDENT>else:<EOL><INDENT>extract_data = _extract_timeseries_list<EOL><DEDENT>try:<EOL><INDENT>return extract_data(img.get_data(), rois.get_data(), mask_data,<EOL>roi_values=roi_values, zeroe=zeroe)<EOL><DEDENT>except:<EOL><INDENT>raise<EOL><DEDENT>

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.


roifiles = []<EOL>for roi in roislist:<EOL><INDENT>try:<EOL><INDENT>roi_file = search_list(roi, filelist)[<NUM_LIT:0>]<EOL><DEDENT>except Exception as exc:<EOL><INDENT>raise Exception('<STR_LIT>'.format(str(exc)))<EOL><DEDENT>else:<EOL><INDENT>roifiles.append(roi_file)<EOL><DEDENT><DEDENT>return binarise(roifiles)<EOL>

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


return get_unique_nonzeros(check_img(atlas_img).get_data())<EOL>

Extract unique values from the atlas and returns them as an ordered list.

Parameters
----------
atlas_img: 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.

Returns
-------
np.ndarray
    An 1D array of roi values from atlas volume.

Note
----
The roi with value 0 will be considered background so will be removed.


def _partition_data(datavol, roivol, roivalue, maskvol=None, zeroe=True):

if maskvol is not None:<EOL><INDENT>indices = (roivol == roivalue) * (maskvol > <NUM_LIT:0>)<EOL><DEDENT>else:<EOL><INDENT>indices = roivol == roivalue<EOL><DEDENT>if datavol.ndim == <NUM_LIT:4>:<EOL><INDENT>ts = datavol[indices, :]<EOL><DEDENT>else:<EOL><INDENT>ts = datavol[indices]<EOL><DEDENT>if zeroe:<EOL><INDENT>if datavol.ndim == <NUM_LIT:4>:<EOL><INDENT>ts = ts[ts.sum(axis=<NUM_LIT:1>) != <NUM_LIT:0>, :]<EOL><DEDENT><DEDENT>return ts<EOL>

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.


<EOL>volume = np.asarray(volume)<EOL>labels, num_labels = scn.label(volume)<EOL>if not num_labels:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>if num_labels == <NUM_LIT:1>:<EOL><INDENT>return volume.astype(np.bool)<EOL><DEDENT>label_count = np.bincount(labels.ravel().astype(np.int))<EOL>label_count[<NUM_LIT:0>] = <NUM_LIT:0><EOL>return labels == label_count.argmax()<EOL>

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.


rois = np.unique(arr)<EOL>rois = rois[np.nonzero(rois)]<EOL>rois.sort()<EOL>return rois<EOL>

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.


labels, num_labels = scn.label(volume)<EOL>labels_to_keep = set([i for i in range(num_labels)<EOL>if np.sum(labels == i) >= min_cluster_size])<EOL>clusters_mask = np.zeros_like(volume, dtype=int)<EOL>for l in range(num_labels):<EOL><INDENT>if l in labels_to_keep:<EOL><INDENT>clusters_mask[labels == l] = <NUM_LIT:1><EOL><DEDENT><DEDENT>return clusters_mask<EOL>

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.


img      = check_img(image)<EOL>hdr, aff = get_img_info(img)<EOL>if len(img.shape) != <NUM_LIT:4>:<EOL><INDENT>raise AttributeError('<STR_LIT>'.format(repr_imgs(img)))<EOL><DEDENT>if not <NUM_LIT:0> <= vol_idx < img.shape[<NUM_LIT:3>]:<EOL><INDENT>raise IndexError('<STR_LIT>'<EOL>'<STR_LIT>'.format(repr_imgs(img), img.shape[<NUM_LIT:3>], vol_idx))<EOL><DEDENT>img_data = img.get_data()<EOL>new_vol  = img_data[:, :, :, vol_idx].copy()<EOL>hdr.set_data_shape(hdr.get_data_shape()[:<NUM_LIT:3>])<EOL>return new_vol, hdr, aff<EOL>

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.


def _extract_timeseries_dict(tsvol, roivol, maskvol=None, roi_values=None, zeroe=True):

_check_for_partition(tsvol, roivol, maskvol)<EOL>if roi_values is None:<EOL><INDENT>roi_values = get_unique_nonzeros(roivol)<EOL><DEDENT>ts_dict = OrderedDict()<EOL>for r in roi_values:<EOL><INDENT>ts = _partition_data(tsvol, roivol, r, maskvol, zeroe)<EOL>if len(ts) == <NUM_LIT:0>:<EOL><INDENT>ts = np.zeros(tsvol.shape[-<NUM_LIT:1>])<EOL><DEDENT>ts_dict[r] = ts<EOL><DEDENT>return ts_dict<EOL>

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.


firstimg = check_img(filelist[<NUM_LIT:0>])<EOL>mask     = np.zeros_like(firstimg.get_data())<EOL>try:<EOL><INDENT>for volf in filelist:<EOL><INDENT>roiimg = check_img(volf)<EOL>check_img_compatibility(firstimg, roiimg)<EOL>mask  += get_img_data(roiimg)<EOL><DEDENT><DEDENT>except Exception as exc:<EOL><INDENT>raise ValueError('<STR_LIT>'.format(repr_imgs(firstimg), repr_imgs(volf))) from exc<EOL><DEDENT>else:<EOL><INDENT>return as_ndarray(mask > <NUM_LIT:0>, dtype=bool)<EOL><DEDENT>

Creates a binarised mask with the union of the files in filelist.

Parameters
----------
filelist: list of img-like object or boyle.nifti.NeuroImage or str
    List of paths to the volume files containing the 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.

Returns
-------
ndarray of bools
    Mask volume

Raises
------
ValueError


mask_data = load_mask_data(mask_img)<EOL>return vol_data[mask_data], mask_data<EOL>

Parameters
----------
vol_data:
mask_img:

Returns
-------
masked_data, mask_indices

masked_data: numpy.ndarray
    2D array of series with shape (image number, voxel number)

Note
----
vol_data and mask_file must have the same shape.


img = check_img(image)<EOL>return img.get_data() > threshold<EOL>

Binarise image with the given threshold

    Parameters
    ----------
    image: img-like object or boyle.nifti.NeuroImage 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.

    threshold: float

    Returns
    -------
    binarised_img: numpy.ndarray
        Mask volume


img    = check_img(image, make_it_3d=True)<EOL>values = np.unique(img.get_data())<EOL>if len(values) == <NUM_LIT:1>:<EOL><INDENT>if values[<NUM_LIT:0>] == <NUM_LIT:0> and not allow_empty:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT><DEDENT>elif len(values) == <NUM_LIT:2>:<EOL><INDENT>if <NUM_LIT:0> not in values:<EOL><INDENT>raise ValueError('<STR_LIT>'<EOL>'<STR_LIT>'.format(values))<EOL><DEDENT><DEDENT>elif len(values) != <NUM_LIT:2>:<EOL><INDENT>raise ValueError('<STR_LIT>'<EOL>'<STR_LIT>'.format(values))<EOL><DEDENT>return nib.Nifti1Image(as_ndarray(get_img_data(img), dtype=bool), img.get_affine(), img.get_header())<EOL>

Load a Nifti mask volume.

    Parameters
    ----------
    image: img-like object or boyle.nifti.NeuroImage 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.

    allow_empty: boolean, optional
        Allow loading an empty mask (full of 0 values)

    Returns
    -------
    nibabel.Nifti1Image with boolean data.


def vector_to_volume(arr, mask, order='<STR_LIT:C>'):

if mask.dtype != np.bool:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>if arr.ndim != <NUM_LIT:1>:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>if arr.ndim == <NUM_LIT:2> and any(v == <NUM_LIT:1> for v in arr.shape):<EOL><INDENT>log.debug('<STR_LIT>'.format(arr.shape))<EOL>arr = arr.flatten()<EOL><DEDENT>volume = np.zeros(mask.shape[:<NUM_LIT:3>], dtype=arr.dtype, order=order)<EOL>volume[mask] = arr<EOL>return volume<EOL>

Transform a given vector to a volume. This is a reshape function for
    3D flattened and maybe masked vectors.

    Parameters
    ----------
    arr: np.array
        1-Dimensional array

    mask: numpy.ndarray
        Mask image. Must have 3 dimensions, bool dtype.

    Returns
    -------
    np.ndarray


def matrix_to_4dvolume(arr, mask, order='<STR_LIT:C>'):

if mask.dtype != np.bool:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>if arr.ndim != <NUM_LIT:2>:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>if mask.sum() != arr.shape[<NUM_LIT:0>]:<EOL><INDENT>raise ValueError('<STR_LIT>'.format(mask.sum(), arr.shape))<EOL><DEDENT>data = np.zeros(mask.shape + (arr.shape[<NUM_LIT:1>],), dtype=arr.dtype,<EOL>order=order)<EOL>data[mask, :] = arr<EOL>return data<EOL>

Transform a given vector to a volume. This is a reshape function for
    4D flattened masked matrices where the second dimension of the matrix
    corresponds to the original 4th dimension.

    Parameters
    ----------
    arr: numpy.array
        2D numpy.array

    mask: numpy.ndarray
        Mask image. Must have 3 dimensions, bool dtype.

    dtype: return type
        If None, will get the type from vector

    Returns
    -------
    data: numpy.ndarray
        Unmasked data.
        Shape: (mask.shape[0], mask.shape[1], mask.shape[2], X.shape[1])


return smooth_imgs(image, smoothmm)<EOL>


sigma = np.asarray(sigma)<EOL>return np.sqrt(<NUM_LIT:8> * np.log(<NUM_LIT:2>)) * sigma<EOL>

Convert a sigma in a Gaussian kernel to a FWHM value.

    Parameters
    ----------
    sigma: float or numpy.array
       sigma value or values

    Returns
    -------
    fwhm: float or numpy.array
       fwhm values corresponding to `sigma` values


def _smooth_data_array(arr, affine, fwhm, copy=True):

if arr.dtype.kind == '<STR_LIT:i>':<EOL><INDENT>if arr.dtype == np.int64:<EOL><INDENT>arr = arr.astype(np.float64)<EOL><DEDENT>else:<EOL><INDENT>arr = arr.astype(np.float32)<EOL><DEDENT><DEDENT>if copy:<EOL><INDENT>arr = arr.copy()<EOL><DEDENT>arr[np.logical_not(np.isfinite(arr))] = <NUM_LIT:0><EOL>try:<EOL><INDENT>affine = affine[:<NUM_LIT:3>, :<NUM_LIT:3>]<EOL>fwhm_sigma_ratio = np.sqrt(<NUM_LIT:8> * np.log(<NUM_LIT:2>))<EOL>vox_size         = np.sqrt(np.sum(affine ** <NUM_LIT:2>, axis=<NUM_LIT:0>))<EOL>sigma            = fwhm / (fwhm_sigma_ratio * vox_size)<EOL>for n, s in enumerate(sigma):<EOL><INDENT>ndimage.gaussian_filter1d(arr, s, output=arr, axis=n)<EOL><DEDENT><DEDENT>except:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>else:<EOL><INDENT>return arr<EOL><DEDENT>

Smooth images with a a Gaussian filter.

    Apply a Gaussian filter along the three first dimensions of arr.

    Parameters
    ----------
    arr: numpy.ndarray
        3D or 4D array, with image number as last dimension.

    affine: numpy.ndarray
        Image affine transformation matrix for image.

    fwhm: scalar, numpy.ndarray
        Smoothing kernel size, as Full-Width at Half Maximum (FWHM) in millimeters.
        If a scalar is given, kernel width is identical on all three directions.
        A numpy.ndarray must have 3 elements, giving the FWHM along each axis.

    copy: bool
        if True, will make a copy of the input array. Otherwise will directly smooth the input array.

    Returns
    -------
    smooth_arr: numpy.ndarray


fwhm = np.asarray(fwhm)<EOL>return fwhm / np.sqrt(<NUM_LIT:8> * np.log(<NUM_LIT:2>))<EOL>

Convert a FWHM value to sigma in a Gaussian kernel.

    Parameters
    ----------
    fwhm: float or numpy.array
       fwhm value or values

    Returns
    -------
    fwhm: float or numpy.array
       sigma values


<EOL>if hasattr(imgs, "<STR_LIT>")and not isinstance(imgs, string_types):<EOL><INDENT>single_img = False<EOL><DEDENT>else:<EOL><INDENT>single_img = True<EOL>imgs = [imgs]<EOL><DEDENT>ret = []<EOL>for img in imgs:<EOL><INDENT>img = check_niimg(img)<EOL>affine = img.get_affine()<EOL>filtered = _smooth_array(img.get_data(), affine, fwhm=fwhm,<EOL>ensure_finite=True, copy=True, **kwargs)<EOL>ret.append(new_img_like(img, filtered, affine, copy_header=True))<EOL><DEDENT>if single_img:<EOL><INDENT>return ret[<NUM_LIT:0>]<EOL><DEDENT>else:<EOL><INDENT>return ret<EOL><DEDENT>

Smooth images by applying a Gaussian filter.
    Apply a Gaussian filter along the three first dimensions of arr.
    In all cases, non-finite values in input image are replaced by zeros.

    This is copied and slightly modified from nilearn:
    https://github.com/nilearn/nilearn/blob/master/nilearn/image/image.py
    Added the **kwargs argument.

    Parameters
    ==========
    imgs: Niimg-like object or iterable of Niimg-like objects
        See http://nilearn.github.io/manipulating_images/manipulating_images.html#niimg.
        Image(s) to smooth.
    fwhm: scalar, numpy.ndarray, 'fast' or None
        Smoothing strength, as a Full-Width at Half Maximum, in millimeters.
        If a scalar is given, width is identical on all three directions.
        A numpy.ndarray must have 3 elements, giving the FWHM along each axis.
        If fwhm == 'fast', a fast smoothing will be performed with
        a filter [0.2, 1, 0.2] in each direction and a normalisation
        to preserve the scale.
        If fwhm is None, no filtering is performed (useful when just removal
        of non-finite values is needed)
    Returns
    =======
    filtered_img: nibabel.Nifti1Image or list of.
        Input image, filtered. If imgs is an iterable, then filtered_img is a
        list.


try:<EOL><INDENT>check_img_compatibility(one_img, another_img)<EOL><DEDENT>except :<EOL><INDENT>return False<EOL><DEDENT>else:<EOL><INDENT>return True<EOL><DEDENT>

Return true if one_img and another_img have the same shape.
    False otherwise.
    If both are nibabel.Nifti1Image will also check for affine matrices.

    Parameters
    ----------
    one_img: nibabel.Nifti1Image or np.ndarray

    another_img: nibabel.Nifti1Image  or np.ndarray

    Returns
    -------
    bool


def have_same_affine(one_img, another_img, only_check_3d=False):

img1 = check_img(one_img)<EOL>img2 = check_img(another_img)<EOL>ndim1 = len(img1.shape)<EOL>ndim2 = len(img2.shape)<EOL>if ndim1 < <NUM_LIT:3>:<EOL><INDENT>raise ValueError('<STR_LIT>'.format(repr_imgs(img1), ndim1))<EOL><DEDENT>if ndim2 < <NUM_LIT:3>:<EOL><INDENT>raise ValueError('<STR_LIT>'.format(repr_imgs(img2), ndim1))<EOL><DEDENT>affine1 = img1.get_affine()<EOL>affine2 = img2.get_affine()<EOL>if only_check_3d:<EOL><INDENT>affine1 = affine1[:<NUM_LIT:3>, :<NUM_LIT:3>]<EOL>affine2 = affine2[:<NUM_LIT:3>, :<NUM_LIT:3>]<EOL><DEDENT>try:<EOL><INDENT>return np.allclose(affine1, affine2)<EOL><DEDENT>except ValueError:<EOL><INDENT>return False<EOL><DEDENT>except:<EOL><INDENT>raise<EOL><DEDENT>

Return True if the affine matrix of one_img is close to the affine matrix of another_img.
    False otherwise.

    Parameters
    ----------
    one_img: nibabel.Nifti1Image

    another_img: nibabel.Nifti1Image

    only_check_3d: bool
        If True will extract only the 3D part of the affine matrices when they have more dimensions.

    Returns
    -------
    bool

    Raises
    ------
    ValueError


if hasattr(img, '<STR_LIT>'):<EOL><INDENT>shape = img.shape<EOL><DEDENT>else:<EOL><INDENT>shape = img.get_data().shape<EOL><DEDENT>return shape<EOL>

Return the shape of img.

    Paramerers
    -----------
    img:

    Returns
    -------
    shape: tuple


img1shape = nib.load(fname1).get_shape()<EOL>img2shape = nib.load(fname2).get_shape()<EOL>return have_same_shape(img1shape, img2shape)<EOL>

@param fname1: string
File path of an image

@param fname2: string
File path of an image

@return: bool
True if both have the same geometry


def have_same_shape(array1, array2, nd_to_check=None):

shape1 = array1.shape<EOL>shape2 = array2.shape<EOL>if nd_to_check is not None:<EOL><INDENT>if len(shape1) < nd_to_check:<EOL><INDENT>msg = '<STR_LIT>'.format(shape1)<EOL>raise ValueError(msg)<EOL><DEDENT>elif len(shape2) < nd_to_check:<EOL><INDENT>msg = '<STR_LIT>'.format(shape2)<EOL>raise ValueError(msg)<EOL><DEDENT>shape1 = shape1[:nd_to_check]<EOL>shape2 = shape2[:nd_to_check]<EOL><DEDENT>return shape1 == shape2<EOL>

Returns true if array1 and array2 have the same shapes, false
otherwise.

Parameters
----------
array1: numpy.ndarray

array2: numpy.ndarray

nd_to_check: int
    Number of the dimensions to check, i.e., if == 3 then will check only the 3 first numbers of array.shape.
Returns
-------
bool