code: def get_bids_files(
      main_path,
      file_tag="*",
      file_type="*",
      sub_label="*",
      modality_folder="*",
      filters=None,
      sub_folder=True,
  ):
      """Search for files in a :term:`BIDS` dataset following given constraints.
  
      This utility function allows to filter files in the :term:`BIDS` dataset by
      any of the fields contained in the file names. Moreover it allows to search
      for specific types of files or particular tags.
  
      The provided filters have to correspond to a file name field, so
      any file not containing the field will be ignored. For example the filter
      ('sub', '01') would return all files corresponding to the first
      subject that specifically contain in the file name 'sub-01'. If more
      filters are given then we constraint the possible files names accordingly.
  
      Notice that to search in the derivatives folder, it has to be given as
      part of the main_path. This is useful since the current convention gives
      exactly the same inner structure to derivatives than to the main
      :term:`BIDS` dataset folder, so we can search it in the same way.
  
      Parameters
      ----------
      main_path : :obj:`str`
          Directory of the :term:`BIDS` dataset.
  
      file_tag : :obj:`str` accepted by glob, default='*'
          The final tag of the desired files. For example 'bold' if one is
          interested in the files related to the neuroimages.
  
      file_type : :obj:`str` accepted by glob, default='*'
          The type of the desired files. For example to be able to request only
          'nii' or 'json' files for the 'bold' tag.
  
      sub_label : :obj:`str` accepted by glob, default='*'
          Such a common filter is given as a direct option since it applies also
          at the level of directories. the label is what follows the 'sub' field
          in the :term:`BIDS` convention as 'sub-label'.
  
      modality_folder : :obj:`str` accepted by glob, default='*'
          Inside the subject and optional session folders a final level of
          folders is expected in the :term:`BIDS` convention that groups files
          according to different neuroimaging modalities and any other additions
          of the dataset provider. For example the 'func' and 'anat' standard
          folders. If given as the empty string '', files will be searched
          inside the sub-label/ses-label directories.
  
      filters : :obj:`list` of :obj:`tuple` (:obj:`str`, :obj:`str`), optional
          Filters are of the form (field, label). Only one filter per field
          allowed. A file that does not match a filter will be discarded.
          Filter examples would be ('ses', '01'), ('dir', 'ap') and
          ('task', 'localizer').
  
      sub_folder : :obj:`bool`, default=True
          Determines if the files searched are at the level of
          subject/session folders or just below the dataset main folder.
          Setting this option to False with other default values would return
          all the files below the main directory, ignoring files in subject
          or derivatives folders.
  
      Returns
      -------
      files : :obj:`list` of :obj:`str`
          List of file paths found.
  
      """
      if sub_folder:
          ses_level = ""
          files = os.path.join(main_path, "sub-*", "ses-*")
          session_folder_exists = glob.glob(files)
          if session_folder_exists:
              ses_level = "ses-*"
  
          files = os.path.join(
              main_path,
              f"sub-{sub_label}",
              ses_level,
              modality_folder,
              f"sub-{sub_label}*_{file_tag}.{file_type}",
          )
      else:
          files = os.path.join(main_path, f"*{file_tag}.{file_type}")
  
      files = glob.glob(files)
      files.sort()
  
      filters = filters or []
      if filters:
          files = [parse_bids_filename(file_) for file_ in files]
          for key, value in filters:
              files = [
                  file_
                  for file_ in files
                  if (key in file_ and file_[key] == value)
              ]
          return [ref_file["file_path"] for ref_file in files]
  
      return files
  
file_path: nilearn/nilearn/interfaces/bids/query.py
imports: from __future__ import annotations
  import glob
  import json
  import os
  from pathlib import Path
  from warnings import warn
label_desc: Use image node_id 4 for calling a global function with example usage: get_bids_files(main_path, file_tag, file_type, sub_label, modality_folder, filters, sub_folder) and returns: files, unknown