Instruction:
Create a Python 3 function to
check that the input image is a proper img.

Response:
def _check_medimg(image, make_it_3d=True):
    """Check that image is a proper img. Turn filenames into objects.

    Parameters
    ----------
    image: img-like object or str
        Can either be:
        - a file path to a medical image file, e.g. NifTI, .mhd/raw, .mha
        - any object with get_data() method and affine & header attributes, e.g., nibabel.Nifti1Image.
        - a Numpy array, which will be wrapped by a nibabel.Nifti2Image class with an `eye` affine.
        If niimg is a string, consider it as a path to Nifti image and
        call nibabel.load on it. If it is an object, check if get_data()
        and get_affine() methods are present, raise TypeError otherwise.

    make_it_3d: boolean, optional
        If True, check if the image is a 3D image and raise an error if not.

    Returns
    -------
    result: nifti-like
       result can be nibabel.Nifti1Image or the input, as-is. It is guaranteed
       that the returned object has get_data() and get_affine() methods.
    """
    if isinstance(image, string_types):
        # a filename, load it
        img = open_volume_file(image)

        if make_it_3d:
            img = _make_it_3d(img)

        return img

    elif isinstance(image, np.array):
        return nib.Nifti2Image(image, affine=np.eye(image.ndim + 1))

    elif isinstance(image, nib.Nifti1Image) or is_img(image):
        return image

    else:
        raise TypeError('Data given cannot be converted to a medical image'
                        ' image: this object -"{}"- does not have'
                        ' get_data or get_affine methods'.format(type(image)))


Instruction:
How would you code a function in Python 3 to
search for dicoms in folders and save file paths into self. dicom_paths set.

Response:
def _store_dicom_paths(self, folders):
        """Search for dicoms in folders and save file paths into
        self.dicom_paths set.

        :param folders: str or list of str
        """
        if isinstance(folders, str):
            folders = [folders]

        for folder in folders:

            if not os.path.exists(folder):
                raise FolderNotFound(folder)

            self.items.extend(list(find_all_dicom_files(folder)))


Instruction:
Here you have a function in Python 3, explain what it does
def from_set(self, fileset, check_if_dicoms=True):
        if check_if_dicoms:
            self.items = []
            for f in fileset:
                if is_dicom_file(f):
                    self.items.append(f)
        else:
            self.items = fileset

Response: Overwrites self. items with the given set of files.


Instruction:
Can you tell what is the following Python 3 function doing
def update(self, dicomset):
        if not isinstance(dicomset, DicomFileSet):
            raise ValueError('Given dicomset is not a DicomFileSet.')

        self.items = list(set(self.items).update(dicomset))

Response: Update this set with the union of itself and dicomset.


Instruction:
Can you implement a function in Python 3 that
copies all files within this set to the output_folder.

Response:
def copy_files_to_other_folder(self, output_folder, rename_files=True,
                                   mkdir=True, verbose=False):
        """
        Copies all files within this set to the output_folder

        Parameters
        ----------
        output_folder: str
        Path of the destination folder of the files

        rename_files: bool
        Whether or not rename the files to a sequential format

        mkdir: bool
        Whether to make the folder if it does not exist

        verbose: bool
        Whether to print to stdout the files that are beind copied
        """
        import shutil

        if not os.path.exists(output_folder):
            os.mkdir(output_folder)

        if not rename_files:
            for dcmf in self.items:
                outf = os.path.join(output_folder, os.path.basename(dcmf))
                if verbose:
                    print('{} -> {}'.format(dcmf, outf))
                shutil.copyfile(dcmf, outf)
        else:
            n_pad = len(self.items)+2
            for idx, dcmf in enumerate(self.items):
                outf = '{number:0{width}d}.dcm'.format(width=n_pad, number=idx)
                outf = os.path.join(output_folder, outf)
                if verbose:
                    print('{} -> {}'.format(dcmf, outf))
                shutil.copyfile(dcmf, outf)


Instruction:
Can you create a Python 3 function that
creates a lambda function to read DICOM files.

Response:
def get_dcm_reader(store_metadata=True, header_fields=None):
        """
        Creates a lambda function to read DICOM files.
        If store_store_metadata is False, will only return the file path.
        Else if you give header_fields, will return only the set of of
        header_fields within a DicomFile object or the whole DICOM file if
        None.

        :return: function
        This function has only one parameter: file_path
        """
        if not store_metadata:
            return lambda fpath: fpath

        if header_fields is None:
            build_dcm = lambda fpath: DicomFile(fpath)
        else:
            dicom_header = namedtuple('DicomHeader', header_fields)
            build_dcm = lambda fpath: dicom_header._make(DicomFile(fpath).get_attributes(header_fields))

        return build_dcm


Instruction:
Can you generate a brief explanation for the following Python 3 code
def scrape_all_files(self):
        try:
            for dcmf in self.items:
                yield self.read_dcm(dcmf)
        except IOError as ioe:
            raise IOError('Error reading DICOM file: {}.'.format(dcmf)) from ioe

Response:
Generator that yields one by one the return value for self. read_dcm
        for each DICOM file within this set


Instruction:
Can you generate the documentation for the following Python 3 function
def get_unique_field_values(dcm_file_list, field_name):
    field_values = set()

    for dcm in dcm_file_list:
        field_values.add(str(DicomFile(dcm).get_attributes(field_name)))

    return field_values

Response: Returns a set of unique field values from a list of DICOM files


Instruction:
Here you have a function in Python 3, explain what it does
def find_all_dicom_files(root_path):
    dicoms = set()

    try:
        for fpath in get_all_files(root_path):
            if is_dicom_file(fpath):
                dicoms.add(fpath)
    except IOError as ioe:
        raise IOError('Error reading file {0}.'.format(fpath)) from ioe

    return dicoms

Response: Returns a list of the dicom files within root_path


Instruction:
How would you code a function in Python 3 to
check if the file exists and returns True if it exists and False otherwise.

Response:
def is_dicom_file(filepath):
    """
    Tries to read the file using dicom.read_file,
    if the file exists and dicom.read_file does not raise
    and Exception returns True. False otherwise.

    :param filepath: str
     Path to DICOM file

    :return: bool
    """
    if not os.path.exists(filepath):
        raise IOError('File {} not found.'.format(filepath))

    filename = os.path.basename(filepath)
    if filename == 'DICOMDIR':
        return False

    try:
        _ = dicom.read_file(filepath)
    except Exception as exc:
        log.debug('Checking if {0} was a DICOM, but returned '
                  'False.'.format(filepath))
        return False

    return True


Instruction:
Can you generate the documentation for the following Python 3 function
def group_dicom_files(dicom_paths, hdr_field='PatientID'):
    dicom_groups = defaultdict(list)
    try:
        for dcm in dicom_paths:
            hdr = dicom.read_file(dcm)
            group_key = getattr(hdr, hdr_field)
            dicom_groups[group_key].append(dcm)
    except KeyError as ke:
        raise KeyError('Error reading field {} from file {}.'.format(hdr_field, dcm)) from ke

    return dicom_groups

Response:
Group in a dictionary all the DICOM files in dicom_paths
    separated by hdr_field.


Instruction:
Write a Python 3 function that can
decompress all DICOM files in input_dir and return a list of DICOM files in output_dir.

Response:
def decompress(input_dir, dcm_pattern='*.dcm'):
    """ Decompress all *.dcm files recursively found in DICOM_DIR.
    This uses 'gdcmconv --raw'.
    It works when 'dcm2nii' shows the `Unsupported Transfer Syntax` error. This error is
    usually caused by lack of JPEG2000 support in dcm2nii compilation.

    Read more:
    http://www.nitrc.org/plugins/mwiki/index.php/dcm2nii:MainPage#Transfer_Syntaxes_and_Compressed_Images

    Parameters
    ----------
    input_dir: str
        Folder path

    dcm_patther: str
        Pattern of the DICOM file names in `input_dir`.

    Notes
    -----
    The *.dcm files in `input_folder` will be overwritten.
    """
    dcmfiles = sorted(recursive_glob(input_dir, dcm_pattern))
    for dcm in dcmfiles:
        cmd = 'gdcmconv --raw -i "{0}" -o "{0}"'.format(dcm)
        log.debug('Calling {}.'.format(cmd))
        subprocess.check_call(cmd, shell=True)


Instruction:
Here you have a function in Python 3, explain what it does
def get_attributes(self, attributes, default=''):
        if isinstance(attributes, str):
            attributes = [attributes]

        attrs = [getattr(self, attr, default) for attr in attributes]

        if len(attrs) == 1:
            return attrs[0]

        return tuple(attrs)

Response: Returns the values of the attributes values from this DicomFile.


Instruction:
Create a Python 3 function for
returning a new object containing all images in the given axis.

Response:
def merge_images(images, axis='t'):
    """ Concatenate `images` in the direction determined in `axis`.

    Parameters
    ----------
    images: list of str or img-like object.
        See NeuroImage constructor docstring.

    axis: str
      't' : concatenate images in time
      'x' : concatenate images in the x direction
      'y' : concatenate images in the y direction
      'z' : concatenate images in the z direction

    Returns
    -------
    merged: img-like object
    """
    # check if images is not empty
    if not images:
        return None

    # the given axis name to axis idx
    axis_dim = {'x': 0,
                'y': 1,
                'z': 2,
                't': 3,
                }

    # check if the given axis name is valid
    if axis not in axis_dim:
        raise ValueError('Expected `axis` to be one of ({}), got {}.'.format(set(axis_dim.keys()), axis))

    # check if all images are compatible with each other
    img1 = images[0]
    for img in images:
        check_img_compatibility(img1, img)

    # read the data of all the given images
    # TODO: optimize memory consumption by merging one by one.
    image_data = []
    for img in images:
        image_data.append(check_img(img).get_data())

    # if the work_axis is bigger than the number of axis of the images,
    # create a new axis for the images
    work_axis = axis_dim[axis]
    ndim = image_data[0].ndim
    if ndim - 1 < work_axis:
        image_data = [np.expand_dims(img, axis=work_axis) for img in image_data]

    # concatenate and return
    return np.concatenate(image_data, axis=work_axis)


Instruction:
Can you generate the documentation for the following Python 3 function
def nifti_out(f):
    @wraps(f)
    def wrapped(*args, **kwargs):
        r = f(*args, **kwargs)

        img = read_img(args[0])
        return nib.Nifti1Image(r, affine=img.get_affine(), header=img.header)

    return wrapped

Response:
Takes a function whose first argument is an img processes its
    data and returns a numpy array. This decorator takes a numpy array containing the image data and returns a nibabel. Nifti1Image.


Instruction:
Make a summary of the following Python 3 code
def thr_img(img, thr=2., mode='+'):

    vol  = read_img(img).get_data()

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

    return vol * mask

Response: This function returns a new image that is threshold with value thr.


Instruction:
Make a summary of the following Python 3 code
def div_img(img1, div2):
    if is_img(div2):
        return img1.get_data()/div2.get_data()
    elif isinstance(div2, (float, int)):
        return img1.get_data()/div2
    else:
        raise NotImplementedError('Cannot divide {}({}) by '
                                  '{}({})'.format(type(img1),
                                                  img1,
                                                  type(div2),
                                                  div2))

Response: Pixelwise division or divide by a number


Instruction:
Can you generate the documentation for the following Python 3 function
def apply_mask(img, mask):
    from .mask import apply_mask

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

Response: Return the image with the given mask applied.


Instruction:
Write a Python 3 script to
return an image with the binarised version of the data of img.

Response:
def abs_img(img):
    """ Return an image with the binarised version of the data of `img`."""
    bool_img = np.abs(read_img(img).get_data())
    return bool_img.astype(int)


Instruction:
How would you implement a function in Python 3 that
returns a z - scored version of icc.

Response:
def icc_img_to_zscore(icc, center_image=False):
    """ Return a z-scored version of `icc`.
    This function is based on GIFT `icatb_convertImageToZScores` function.
    """
    vol = read_img(icc).get_data()

    v2 = vol[vol != 0]
    if center_image:
        v2 = detrend(v2, axis=0)

    vstd = np.linalg.norm(v2, ord=2) / np.sqrt(np.prod(v2.shape) - 1)

    eps = np.finfo(vstd.dtype).eps
    vol /= (eps + vstd)

    return vol


Instruction:
How would you code a function in Python 3 to
return the thresholded z - scored icc.

Response:
def spatial_map(icc, thr, mode='+'):
    """ Return the thresholded z-scored `icc`. """
    return thr_img(icc_img_to_zscore(icc), thr=thr, mode=mode).get_data()


Instruction:
Make a summary of the following Python 3 code
def filter_icc(icc, mask=None, thr=2, zscore=True, mode="+"):
    if zscore:
        icc_filt = thr_img(icc_img_to_zscore(icc), thr=thr, mode=mode)
    else:
        icc_filt = thr_img(icc, thr=thr, mode=mode)

    if mask is not None:
        icc_filt = apply_mask(icc_filt, mask)

    return icc_filt

Response: Threshold then mask an IC correlation map.


Instruction:
How would you explain what the following Python 3 function does
def check_mhd_img(image, make_it_3d=False):
    if isinstance(image, string_types):
        # a filename, load it
        if not op.exists(image):
            raise FileNotFound(image)

        ext = get_extension(image).lower()
        if not 'mhd' in ext:
            warnings.warn('Expecting a filepath with `.mhd` extension, got {}.'.format(image))

        img, hdr = load_raw_data_with_mhd(image)
        if make_it_3d:
            img = _make_it_3d(img)

        return img

    elif is_img(image):
        return image

    else:
        raise TypeError('Data given cannot be converted to a nifti'
                        ' image: this object -"{}"- does not have'
                        ' get_data or get_affine methods'.format(type(image)))

Response: Checks that the given image is a proper img.


Instruction:
Write a Python 3 function that can
enforce that img is a 3D img - like object raise a TypeError.

Response:
def _make_it_3d(img):
    """Enforce that img is a 3D img-like object, if it is not, raise a TypeError.
    i.e., remove dimensions of size 1.

    Parameters
    ----------
    img: numpy.ndarray
        Image data array

    Returns
    -------
    3D numpy ndarray object
    """
    shape = img.shape

    if len(shape) == 3:
        return img

    elif len(shape) == 4 and shape[3] == 1:
        # "squeeze" the image.
        return img[:, :, :, 0]

    else:
        raise TypeError('A 3D image is expected, but an image with a shape of {} was given.'.format(shape))


Instruction:
Implement a function in Python 3 to
write the content of the meta_dict into filename.

Response:
def write_meta_header(filename, meta_dict):
    """ Write the content of the `meta_dict` into `filename`.

    Parameters
    ----------
    filename: str
        Path to the output file

    meta_dict: dict
        Dictionary with the fields of the metadata .mhd file
    """
    header = ''
    # do not use tags = meta_dict.keys() because the order of tags matters
    for tag in MHD_TAGS:
        if tag in meta_dict.keys():
            header += '{} = {}\n'.format(tag, meta_dict[tag])

    with open(filename, 'w') as f:
        f.write(header)


Instruction:
Write a Python 3 function for
writing the data into a raw format file.

Response:
def dump_raw_data(filename, data):
    """ Write the data into a raw format file. Big endian is always used.

    Parameters
    ----------
    filename: str
        Path to the output file

    data: numpy.ndarray
        n-dimensional image data array.
    """
    if data.ndim == 3:
        # Begin 3D fix
        data = data.reshape([data.shape[0], data.shape[1]*data.shape[2]])
        # End 3D fix

    a = array.array('f')
    for o in data:
        a.fromlist(list(o.flatten()))

    # if is_little_endian():
    #     a.byteswap()

    with open(filename, 'wb') as rawf:
        a.tofile(rawf)


Instruction:
Here you have a function in Python 3, explain what it does
def write_mhd_file(filename, data, shape=None, meta_dict=None):
    # check its extension
    ext = get_extension(filename)
    fname = op.basename(filename)
    if ext != '.mhd' or ext != '.raw':
        mhd_filename = fname + '.mhd'
        raw_filename = fname + '.raw'
    elif ext == '.mhd':
        mhd_filename = fname
        raw_filename = remove_ext(fname) + '.raw'
    elif ext == '.raw':
        mhd_filename = remove_ext(fname) + '.mhd'
        raw_filename = fname
    else:
        raise ValueError('`filename` extension {} from {} is not recognised. '
                         'Expected .mhd or .raw.'.format(ext, filename))

    # default values
    if meta_dict is None:
        meta_dict = {}

    if shape is None:
        shape = data.shape

    # prepare the default header
    meta_dict['ObjectType']             = meta_dict.get('ObjectType',             'Image')
    meta_dict['BinaryData']             = meta_dict.get('BinaryData',             'True' )
    meta_dict['BinaryDataByteOrderMSB'] = meta_dict.get('BinaryDataByteOrderMSB', 'False')
    meta_dict['ElementType']            = meta_dict.get('ElementType',            NUMPY_TO_MHD_TYPE[data.dtype.type])
    meta_dict['NDims']                  = meta_dict.get('NDims',                  str(len(shape)))
    meta_dict['DimSize']                = meta_dict.get('DimSize',                ' '.join([str(i) for i in shape]))
    meta_dict['ElementDataFile']        = meta_dict.get('ElementDataFile',        raw_filename)

    # target files
    mhd_filename = op.join(op.dirname(filename), mhd_filename)
    raw_filename = op.join(op.dirname(filename), raw_filename)

    # write the header
    write_meta_header(mhd_filename, meta_dict)

    # write the data
    dump_raw_data(raw_filename, data)

    return mhd_filename, raw_filename

Response: Write the data and meta_dict to a. mhd file.


Instruction:
Write a Python 3 script to
copy the mhd and raw files to dst.

Response:
def copy_mhd_and_raw(src, dst):
    """Copy .mhd and .raw files to dst.

    If dst is a folder, won't change the file, but if dst is another filepath,
    will modify the ElementDataFile field in the .mhd to point to the
    new renamed .raw file.

    Parameters
    ----------
    src: str
        Path to the .mhd file to be copied

    dst: str
        Path to the destination of the .mhd and .raw files.
        If a new file name is given, the extension will be ignored.

    Returns
    -------
    dst: str
    """
    # check if src exists
    if not op.exists(src):
        raise IOError('Could not find file {}.'.format(src))

    # check its extension
    ext = get_extension(src)
    if ext != '.mhd':
        msg = 'The src file path must be a .mhd file. Given: {}.'.format(src)
        raise ValueError(msg)

    # get the raw file for this src mhd file
    meta_src = _read_meta_header(src)

    # get the source raw file
    src_raw = meta_src['ElementDataFile']
    if not op.isabs(src_raw):
        src_raw = op.join(op.dirname(src), src_raw)

    # check if dst is dir
    if op.isdir(dst):
        # copy the mhd and raw file to its destiny
        shutil.copyfile(src, dst)
        shutil.copyfile(src_raw, dst)
        return dst

    # build raw file dst file name
    dst_raw = op.join(op.dirname(dst), remove_ext(op.basename(dst))) + '.raw'

    # add extension to the dst path
    if get_extension(dst) != '.mhd':
        dst += '.mhd'

    # copy the mhd and raw file to its destiny
    log.debug('cp: {} -> {}'.format(src,     dst))
    log.debug('cp: {} -> {}'.format(src_raw, dst_raw))
    shutil.copyfile(src, dst)
    shutil.copyfile(src_raw, dst_raw)

    # check if src file name is different than dst file name
    # if not the same file name, change the content of the ElementDataFile field
    if op.basename(dst) != op.basename(src):
        log.debug('modify {}: ElementDataFile: {} -> {}'.format(dst, src_raw,
                                                                op.basename(dst_raw)))
        meta_dst = _read_meta_header(dst)
        meta_dst['ElementDataFile'] = op.basename(dst_raw)
        write_meta_header(dst, meta_dst)

    return dst


Instruction:
Can you tell what is the following Python 3 function doing
def sav_to_pandas_rpy2(input_file):
    import pandas.rpy.common as com

    w = com.robj.r('foreign::read.spss("%s", to.data.frame=TRUE)' % input_file)
    return com.convert_robj(w)

Response: SPSS. sav files to Pandas DataFrame through Rpy2


Instruction:
Write a Python 3 function for
reading the SPSS. sav files to Pandas DataFrame through savreader module

Response:
def sav_to_pandas_savreader(input_file):
    """
    SPSS .sav files to Pandas DataFrame through savreader module

    :param input_file: string

    :return:
    """
    from savReaderWriter import SavReader
    lines = []
    with SavReader(input_file, returnHeader=True) as reader:
        header = next(reader)
        for line in reader:
            lines.append(line)

    return pd.DataFrame(data=lines, columns=header)


Instruction:
Can you generate the documentation for the following Python 3 function
def save_variables(filename, variables):
        ext = get_extension(filename).lower()
        out_exts = {'.pyshelf', '.shelf', '.mat', '.hdf5', '.h5'}

        output_file = filename
        if not ext in out_exts:
            output_file = add_extension_if_needed(filename, '.pyshelf')
            ext = get_extension(filename)

        if ext == '.pyshelf' or ext == '.shelf':
            save_variables_to_shelve(output_file, variables)

        elif ext == '.mat':
            save_variables_to_mat(output_file, variables)

        elif ext == '.hdf5' or ext == '.h5':
            from .hdf5 import save_variables_to_hdf5
            save_variables_to_hdf5(output_file, variables)

        else:
            raise ValueError('Filename extension {0} not accepted.'.format(ext))

Response: Save given variables in a file.


Instruction:
Write a Python 3 script for
saving the variables in the specified file.

Response:
def save_varlist(filename, varnames, varlist):
        """
        Valid extensions '.pyshelf', '.mat', '.hdf5' or '.h5'

        @param filename: string

        @param varnames: list of strings
        Names of the variables

        @param varlist: list of objects
        The objects to be saved
        """
        variables = {}
        for i, vn in enumerate(varnames):
            variables[vn] = varlist[i]

        ExportData.save_variables(filename, variables)


Instruction:
Here you have a function in Python 3, explain what it does
def drain_rois(img):
    img_data = get_img_data(img)

    out = np.zeros(img_data.shape, dtype=img_data.dtype)

    krn_dim = [3] * img_data.ndim
    kernel  = np.ones(krn_dim, dtype=int)

    vals = np.unique(img_data)
    vals = vals[vals != 0]

    for i in vals:
        roi  = img_data == i
        hits = scn.binary_hit_or_miss(roi, kernel)
        roi[hits] = 0
        out[roi > 0] = i

    return out

Response: Return a similar volume with the ROIs emptied.


Instruction:
Given the following Python 3 function, write the documentation
def pick_rois(rois_img, roi_values, bg_val=0):
    img = read_img(rois_img)
    img_data = img.get_data()

    if bg_val == 0:
        out = np.zeros(img_data.shape, dtype=img_data.dtype)
    else:
        out = np.ones(img_data.shape, dtype=img_data.dtype) * bg_val

    for r in roi_values:
        out[img_data == r] = r

    return nib.Nifti2Image(out, affine=img.affine, header=img.header)

Response: Return the rois_img only with the ROI values from roi_values.


Instruction:
Can you generate the documentation for the following Python 3 function
def largest_connected_component(volume):
    # We use asarray to be able to work with masked arrays.
    volume = np.asarray(volume)
    labels, num_labels = scn.label(volume)
    if not num_labels:
        raise ValueError('No non-zero values: no connected components found.')

    if num_labels == 1:
        return volume.astype(np.bool)

    label_count = np.bincount(labels.ravel().astype(np.int))
    # discard the 0 label
    label_count[0] = 0
    return labels == label_count.argmax()

Response: Return the largest connected component of a 3D array.


Instruction:
Given the following Python 3 function, write the documentation
def large_clusters_mask(volume, min_cluster_size):
    labels, num_labels = scn.label(volume)

    labels_to_keep = set([i for i in range(num_labels)
                         if np.sum(labels == i) >= min_cluster_size])

    clusters_mask = np.zeros_like(volume, dtype=int)
    for l in range(num_labels):
        if l in labels_to_keep:
            clusters_mask[labels == l] = 1

    return clusters_mask

Response: Return as mask for volume that includes only areas where the connected components have a size bigger than min_cluster_size.


Instruction:
How would you code a function in Python 3 to
create a mask from a list of ROIs in a file list.

Response:
def create_rois_mask(roislist, filelist):
    """Look for the files in filelist containing the names in roislist, these files will be opened, binarised
    and merged in one mask.

    Parameters
    ----------
    roislist: list of strings
        Names of the ROIs, which will have to be in the names of the files in filelist.

    filelist: list of strings
        List of paths to the volume files containing the ROIs.

    Returns
    -------
    numpy.ndarray
        Mask volume
    """
    roifiles = []

    for roi in roislist:
        try:
            roi_file = search_list(roi, filelist)[0]
        except Exception as exc:
            raise Exception('Error creating list of roi files. \n {}'.format(str(exc)))
        else:
            roifiles.append(roi_file)

    return binarise(roifiles)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def get_unique_nonzeros(arr):
    rois = np.unique(arr)
    rois = rois[np.nonzero(rois)]
    rois.sort()

    return rois

Response: Returns a sorted list of the non - zero unique values of arr.


Instruction:
Can you implement a function in Python 3 that
gets the center of mass for each ROI in the given volume.

Response:
def get_rois_centers_of_mass(vol):
    """Get the center of mass for each ROI in the given volume.

    Parameters
    ----------
    vol: numpy ndarray
        Volume with different values for each ROI.

    Returns
    -------
    OrderedDict
        Each entry in the dict has the ROI value as key and the center_of_mass coordinate as value.
    """
    from scipy.ndimage.measurements import center_of_mass

    roisvals = np.unique(vol)
    roisvals = roisvals[roisvals != 0]

    rois_centers = OrderedDict()
    for r in roisvals:
        rois_centers[r] = center_of_mass(vol, vol, r)

    return rois_centers


Instruction:
Write a Python 3 script to
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.

Response:
def partition_timeseries(image, roi_img, mask_img=None, zeroe=True, roi_values=None, outdict=False):
    """Partition the timeseries in tsvol according to the ROIs in roivol.
    If a mask is given, will use it to exclude any voxel outside of it.

    The outdict indicates whether you want a dictionary for each set of timeseries keyed by the ROI value
    or a list of timeseries sets. If True and roi_img is not None will return an OrderedDict, if False
    or roi_img or roi_list is None will return a list.

    Background value is assumed to be 0 and won't be used here.

    Parameters
    ----------
    image: img-like object or str
        4D timeseries volume

    roi_img: img-like object or str
        3D volume defining different ROIs.

    mask_img: img-like object or str
        3D mask volume

    zeroe: bool
        If true will remove the null timeseries voxels.

    roi_values: list of ROI values (int?)
        List of the values of the ROIs to indicate the
        order and which ROIs will be processed.

    outdict: bool
        If True will return an OrderedDict of timeseries sets, otherwise a list.

    Returns
    -------
    timeseries: list or OrderedDict
        A dict with the timeseries as items and keys as the ROIs voxel values or
        a list where each element is the timeseries set ordered by the sorted values in roi_img or by the roi_values
        argument.

    """
    img  = read_img(image)
    rois = read_img(roi_img)

    # check if roi_img and image are compatible
    check_img_compatibility(img, rois, only_check_3d=True)

    # check if rois has all roi_values
    roi_data = rois.get_data()
    if roi_values is not None:
        for rv in roi_values:
            if not np.any(roi_data == rv):
                raise ValueError('Could not find value {} in rois_img {}.'.format(rv, repr_imgs(roi_img)))
    else:
        roi_values = get_unique_nonzeros(roi_data)

    # check if mask and image are compatible
    if mask_img is None:
        mask_data = None
    else:
        mask = load_mask(mask_img)
        check_img_compatibility(img, mask, only_check_3d=True)
        mask_data = mask.get_data()

    # choose function to call
    if outdict:
        extract_data = _extract_timeseries_dict
    else:
        extract_data = _extract_timeseries_list

    # extract data and return it
    try:
        return extract_data(img.get_data(), rois.get_data(), mask_data,
                            roi_values=roi_values, zeroe=zeroe)
    except:
        raise


Instruction:
Can you write a function in Python 3 where it
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.

Response:
def _partition_data(datavol, roivol, roivalue, maskvol=None, zeroe=True):
    """ Extracts the values in `datavol` that are in the ROI with value `roivalue` in `roivol`.
    The ROI can be masked by `maskvol`.

    Parameters
    ----------
    datavol: numpy.ndarray
        4D timeseries volume or a 3D volume to be partitioned

    roivol: numpy.ndarray
        3D ROIs volume

    roivalue: int or float
        A value from roivol that represents the ROI to be used for extraction.

    maskvol: numpy.ndarray
        3D mask volume

    zeroe: bool
        If true will remove the null timeseries voxels.  Only applied to timeseries (4D) data.

    Returns
    -------
    values: np.array
        An array of the values in the indicated ROI.
        A 2D matrix if `datavol` is 4D or a 1D vector if `datavol` is 3D.
    """
    if maskvol is not None:
        # get all masked time series within this roi r
        indices = (roivol == roivalue) * (maskvol > 0)
    else:
        # get all time series within this roi r
        indices = roivol == roivalue

    if datavol.ndim == 4:
        ts = datavol[indices, :]
    else:
        ts = datavol[indices]

    # remove zeroed time series
    if zeroe:
        if datavol.ndim == 4:
            ts = ts[ts.sum(axis=1) != 0, :]

    return ts


Instruction:
How would you implement a function in Python 3 that
partitions the timeseries in tsvol according to the ROIs in maskvol.

Response:
def _extract_timeseries_dict(tsvol, roivol, maskvol=None, roi_values=None, zeroe=True):
    """Partition the timeseries in tsvol according to the ROIs in roivol.
    If a mask is given, will use it to exclude any voxel outside of it.

    Parameters
    ----------
    tsvol: numpy.ndarray
        4D timeseries volume or a 3D volume to be partitioned

    roivol: numpy.ndarray
        3D ROIs volume

    maskvol: numpy.ndarray
        3D mask volume

    zeroe: bool
        If true will remove the null timeseries voxels.

    roi_values: list of ROI values (int?)
        List of the values of the ROIs to indicate the
        order and which ROIs will be processed.

    Returns
    -------
    ts_dict: OrderedDict
        A dict with the timeseries as items and keys as the ROIs voxel values.
    """
    _check_for_partition(tsvol, roivol, maskvol)

    # get unique values of the atlas
    if roi_values is None:
        roi_values = get_unique_nonzeros(roivol)

    ts_dict = OrderedDict()
    for r in roi_values:
        ts = _partition_data(tsvol, roivol, r, maskvol, zeroe)

        if len(ts) == 0:
            ts = np.zeros(tsvol.shape[-1])

        ts_dict[r] = ts

    return ts_dict


Instruction:
Can you tell what is the following Python 3 function doing
def _extract_timeseries_list(tsvol, roivol, maskvol=None, roi_values=None, zeroe=True):
    _check_for_partition(tsvol, roivol, maskvol)

    if roi_values is None:
        roi_values = get_unique_nonzeros(roivol)

    ts_list = []
    for r in roi_values:
        ts = _partition_data(tsvol, roivol, r, maskvol, zeroe)

        if len(ts) == 0:
            ts = np.zeros(tsvol.shape[-1])

        ts_list.append(ts)

    return ts_list

Response: Partition the timeseries in tsvol according to the ROIs in maskvol.


Instruction:
Can you tell what is the following Python 3 function doing
def get_3D_from_4D(image, vol_idx=0):
    img      = check_img(image)
    hdr, aff = get_img_info(img)

    if len(img.shape) != 4:
        raise AttributeError('Volume in {} does not have 4 dimensions.'.format(repr_imgs(img)))

    if not 0 <= vol_idx < img.shape[3]:
        raise IndexError('IndexError: 4th dimension in volume {} has {} volumes, '
                         'not {}.'.format(repr_imgs(img), img.shape[3], vol_idx))

    img_data = img.get_data()
    new_vol  = img_data[:, :, :, vol_idx].copy()

    hdr.set_data_shape(hdr.get_data_shape()[:3])

    return new_vol, hdr, aff

Response: Pick one 3D volume from a 4D nifti image file.


Instruction:
How would you implement a function in Python 3 that
creates the HDF5 file for the current object.

Response:
def create_hdf_file(self):
        """
        :return: h5py DataSet
        """
        mode = 'w'
        if not self._overwrite and os.path.exists(self._fname):
            mode = 'a'

        self._hdf_file = h5py.File(self._fname, mode)

        if self._hdf_basepath == '/':
            self._group = self._hdf_file['/']
        else:
            self._group = self._hdf_file.create_group(self._hdf_basepath)


Instruction:
Write a Python 3 function that can
return a h5py dataset given its registered name.

Response:
def get_dataset(self, ds_name, mode='r'):
        """
        Returns a h5py dataset given its registered name.

        :param ds_name: string
        Name of the dataset to be returned.

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


Instruction:
Write a Python 3 script to
create an empty dataset with unknown size.

Response:
def create_empty_dataset(self, ds_name, dtype=np.float32):
        """
        Creates a Dataset with unknown size.
        Resize it before using.

        :param ds_name: string

        :param dtype: dtype
        Datatype of the dataset

        :return: h5py DataSet
        """
        if ds_name in self._datasets:
            return self._datasets[ds_name]

        ds = self._group.create_dataset(ds_name, (1, 1), maxshape=None,
                                        dtype=dtype)
        self._datasets[ds_name] = ds

        return ds


Instruction:
Can you tell what is the following Python 3 function doing
def create_dataset(self, ds_name, data, attrs=None, dtype=None):
        if ds_name in self._datasets:
            ds = self._datasets[ds_name]
            if ds.dtype != data.dtype:
                warnings.warn('Dataset and data dtype are different!')

        else:
            if dtype is None:
                dtype = data.dtype

            ds = self._group.create_dataset(ds_name, data.shape,
                                            dtype=dtype)

            if attrs is not None:
                for key in attrs:
                    setattr(ds.attrs, key, attrs[key])

        ds.read_direct(data)
        self._datasets[ds_name] = ds

        return ds

Response: Creates a new dataset in the HDF file and registers it as a new dataset in the HDF file.


Instruction:
Can you generate the documentation for the following Python 3 function
def save(self, ds_name, data, dtype=None):
        return self.create_dataset(ds_name, data, dtype)

Response: Save data to a new dataset.


Instruction:
Explain what the following Python 3 code does
def _fill_missing_values(df, range_values, fill_value=0, fill_method=None):
        idx_colnames  = df.index.names

        idx_colranges = [range_values[x] for x in idx_colnames]

        fullindex = pd.Index([p for p in product(*idx_colranges)],
                             name=tuple(idx_colnames))

        fulldf = df.reindex(index=fullindex, fill_value=fill_value,
                            method=fill_method)

        fulldf.index.names = idx_colnames

        return fulldf, idx_colranges

Response: This function will fill missing values in the index column of df with the given values.


Instruction:
Create a Python 3 function for
retrieving pandas object or group of Numpy ndarrays stored in file

Response:
def get(self, key):
        """
        Retrieve pandas object or group of Numpy ndarrays
        stored in file

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

        Returns
        -------
        obj : type of object stored in file
        """
        node = self.get_node(key)
        if node is None:
            raise KeyError('No object named %s in the file' % key)

        if hasattr(node, 'attrs'):
            if 'pandas_type' in node.attrs:
                return self._read_group(node)

        return self._read_array(node)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def put(self, key, value, attrs=None, format=None, append=False, **kwargs):
        if not isinstance(value, np.ndarray):
            super(NumpyHDFStore, self).put(key, value, format, append, **kwargs)
        else:
            group = self.get_node(key)

            # remove the node if we are not appending
            if group is not None and not append:
                self._handle.removeNode(group, recursive=True)
                group = None

            if group is None:
                paths = key.split('/')

                # recursively create the groups
                path = '/'
                for p in paths:
                    if not len(p):
                        continue
                    new_path = path
                    if not path.endswith('/'):
                        new_path += '/'
                    new_path += p
                    group = self.get_node(new_path)
                    if group is None:
                        group = self._handle.createGroup(path, p)
                    path = new_path

            ds_name = kwargs.get('ds_name', self._array_dsname)

            ds = self._handle.createArray(group, ds_name, value)
            if attrs is not None:
                for key in attrs:
                    setattr(ds.attrs, key, attrs[key])

            self._handle.flush()

            return ds

Response: Put a value into the HDFStore.


Instruction:
How would you explain what the following Python 3 function does
def _push_dfblock(self, key, df, ds_name, range_values):
        #create numpy array and put into hdf_file
        vals_colranges = [range_values[x] for x in df.index.names]
        nu_shape = [len(x) for x in vals_colranges]

        return self.put(key, np.reshape(df.values, tuple(nu_shape)),
                        attrs={'axes': df.index.names},
                        ds_name=ds_name, append=True)

Response:
Push a new block into the hdf_file
        table


Instruction:
Can you tell what is the following Python 3 function doing
def put_df_as_ndarray(self, key, df, range_values, loop_multiindex=False,
                          unstack=False, fill_value=0, fill_method=None):
        """Returns a PyTables HDF Array from df in the shape given by its index columns range values.

        :param key: string object

        :param df: pandas DataFrame

        :param range_values: dict or array-like
        Must contain for each index column of df an entry with all the values
        within the range of the column.

        :param loop_multiindex: bool
        Will loop through the first index in a multiindex dataframe, extract a
        dataframe only for one value, complete and fill the missing values and
        store in the HDF.
        If this is True, it will not use unstack.
        This is as fast as unstacking.

        :param unstack: bool
        Unstack means that this will use the first index name to
        unfold the DataFrame, and will create a group with as many datasets
        as valus has this first index.
        Use this if you think the filled dataframe won't fit in your RAM memory.
        If set to False, this will transform the dataframe in memory first
        and only then save it.

        :param fill_value: scalar or 'nearest', default 0
        Value to use for missing values. Defaults to 0, but can be any
        "compatible" value, e.g., NaN.
        The 'nearest' mode will fill the missing value with the nearest value in
         the column.

        :param fill_method:  {'backfill', 'bfill', 'pad', 'ffill', None}, default None
        Method to use for filling holes in reindexed DataFrame
        'pad' / 'ffill': propagate last valid observation forward to next valid
        'backfill' / 'bfill': use NEXT valid observation to fill gap

        :return: PyTables data node
        """
        idx_colnames = df.index.names
        #idx_colranges = [range_values[x] for x in idx_colnames]

        #dataset group name if not given
        if key is None:
            key = idx_colnames[0]

        if loop_multiindex:
            idx_values = df.index.get_level_values(0).unique()

            for idx in idx_values:
                vals, _ = self._fill_missing_values(df.xs((idx,), level=idx_colnames[0]),
                                                    range_values,
                                                    fill_value=fill_value,
                                                    fill_method=fill_method)

                ds_name = str(idx) + '_' + '_'.join(vals.columns)

                self._push_dfblock(key, vals, ds_name, range_values)

            return self._handle.get_node('/' + str(key))

        #separate the dataframe into blocks, only with the first index
        else:
            if unstack:
                df = df.unstack(idx_colnames[0])
                for idx in df:
                    vals, _ = self._fill_missing_values(df[idx], range_values,
                                                        fill_value=fill_value,
                                                        fill_method=fill_method)
                    vals = np.nan_to_num(vals)

                    ds_name = '_'.join([str(x) for x in vals.name])

                    self._push_dfblock(key, vals, ds_name, range_values)

                return self._handle.get_node('/' + str(key))

        #not separate the data
        vals, _ = self._fill_missing_values(df, range_values,
                                            fill_value=fill_value,
                                            fill_method=fill_method)

        ds_name = self._array_dsname

        return self._push_dfblock(key, vals, ds_name, range_values)

Response: Returns a PyTables HDF Array from a pandas DataFrame


Instruction:
Implement a function in Python 3 to
get the data in the image.

Response:
def get_data(self, safe_copy=False):
        """Get the data in the image.
         If save_copy is True, will perform a deep copy of the data and return it.

        Parameters
        ----------
        smoothed: (optional) bool
            If True and self._smooth_fwhm > 0 will smooth the data before masking.

        masked: (optional) bool
            If True and self.has_mask will return the masked data, the plain data otherwise.

        safe_copy: (optional) bool

        Returns
        -------
        np.ndarray
        """
        if safe_copy:
            data = get_data(self.img)
        else:
            data = self.img.get_data(caching=self._caching)

        return data


Instruction:
Write a Python 3 script to
set a smoothing Gaussian kernel given its FWHM in mm.

Response:
def smooth_fwhm(self, fwhm):
        """ Set a smoothing Gaussian kernel given its FWHM in mm.  """
        if fwhm != self._smooth_fwhm:
            self._is_data_smooth = False
        self._smooth_fwhm = fwhm


Instruction:
How would you code a function in Python 3 to
get the data in the image.

Response:
def get_data(self, smoothed=True, masked=True, safe_copy=False):
        """Get the data in the image.
         If save_copy is True, will perform a deep copy of the data and return it.

        Parameters
        ----------
        smoothed: (optional) bool
            If True and self._smooth_fwhm > 0 will smooth the data before masking.

        masked: (optional) bool
            If True and self.has_mask will return the masked data, the plain data otherwise.

        safe_copy: (optional) bool

        Returns
        -------
        np.ndarray
        """
        if not safe_copy and smoothed == self._is_data_smooth and masked == self._is_data_masked:
            if self.has_data_loaded() and self._caching == 'fill':
                return self.get_data()

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

        is_smoothed = False
        if smoothed and self._smooth_fwhm > 0:
            try:
                data = _smooth_data_array(data, self.get_affine(), self._smooth_fwhm, copy=False)
            except ValueError as ve:
                raise ValueError('Error smoothing image {} with a {}mm FWHM '
                                 'kernel.'.format(self.img, self._smooth_fwhm)) from ve
            else:
                is_smoothed = True

        is_data_masked = False
        if masked and self.has_mask():
            try:
                data = self.unmask(self._mask_data(data)[0])
            except:
                raise
            else:
                is_data_masked = True

        if not safe_copy:
            self._is_data_masked = is_data_masked
            self._is_data_smooth = is_smoothed

        return data


Instruction:
Here you have a function in Python 3, explain what it does
def apply_mask(self, mask_img):
        self.set_mask(mask_img)
        return self.get_data(masked=True, smoothed=True, safe_copy=True)

Response: Set the mask and get_masked_data methods.


Instruction:
How would you explain what the following Python 3 function does
def set_mask(self, mask_img):
        mask = load_mask(mask_img, allow_empty=True)
        check_img_compatibility(self.img, mask, only_check_3d=True) # this will raise an exception if something is wrong
        self.mask = mask

Response: Sets a mask img to this.


Instruction:
Make a summary of the following Python 3 code
def _mask_data(self, data):
        self._check_for_mask()

        msk_data = self.mask.get_data()
        if self.ndim == 3:
            return data[msk_data], np.where(msk_data)
        elif self.ndim == 4:
            return _apply_mask_to_4d_data(data, self.mask)
        else:
            raise ValueError('Cannot mask {} with {} dimensions using mask {}.'.format(self, self.ndim, self.mask))

Response: Return the data masked with self. mask


Instruction:
Can you generate the documentation for the following Python 3 function
def apply_smoothing(self, smooth_fwhm):
        if smooth_fwhm <= 0:
            return

        old_smooth_fwhm   = self._smooth_fwhm
        self._smooth_fwhm = smooth_fwhm
        try:
            data = self.get_data(smoothed=True, masked=True, safe_copy=True)
        except ValueError as ve:
            self._smooth_fwhm = old_smooth_fwhm
            raise
        else:
            self._smooth_fwhm = smooth_fwhm
            return data

Response: Returns the data in the new memory with the given fwhm.


Instruction:
Can you implement a function in Python 3 that
returns a vector of the masked data.

Response:
def mask_and_flatten(self):
        """Return a vector of the masked data.

        Returns
        -------
        np.ndarray, tuple of indices (np.ndarray), tuple of the mask shape
        """
        self._check_for_mask()

        return self.get_data(smoothed=True, masked=True, safe_copy=False)[self.get_mask_indices()],\
               self.get_mask_indices(), self.mask.shape


Instruction:
Write a Python 3 script for
unmasking the image using the mask

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

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

        if arr.ndim == 2:
            return matrix_to_4dvolume(arr, self.mask.get_data())
        elif arr.ndim == 1:
            return vector_to_volume(arr, self.mask.get_data())


Instruction:
Write a Python 3 function for
saving this object instance in outpath.

Response:
def to_file(self, outpath):
        """Save this object instance in outpath.

        Parameters
        ----------
        outpath: str
            Output file path
        """
        if not self.has_mask() and not self.is_smoothed():
            save_niigz(outpath, self.img)
        else:
            save_niigz(outpath, self.get_data(masked=True, smoothed=True),
                       self.get_header(), self.get_affine())


Instruction:
How would you explain what the following Python 3 function does
def _read_meta_header(filename):
    fileIN = open(filename, 'r')
    line   = fileIN.readline()

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

Response: Read the. mhd header file and return a dictionary of meta data.


Instruction:
Can you implement a function in Python 3 that
loads the n - dimensional image data array from a. mhd file.

Response:
def load_raw_data_with_mhd(filename):
    """Return a dictionary of meta data from meta header file.

    Parameters
    ----------
    filename: str
        Path to a .mhd file

    Returns
    -------
    data: numpy.ndarray
        n-dimensional image data array.

    meta_dict: dict
        A dictionary with the .mhd header content.
    """
    meta_dict = _read_meta_header(filename)
    dim       = int(meta_dict['NDims'])

    assert (meta_dict['ElementType'] in MHD_TO_NUMPY_TYPE)

    arr = [int(i) for i in meta_dict['DimSize'].split()]
    volume = reduce(lambda x, y: x*y, arr[0:dim-1], 1)

    pwd       = op.dirname(filename)
    raw_file  = meta_dict['ElementDataFile']
    data_file = op.join(pwd, raw_file)

    ndtype    = MHD_TO_NUMPY_TYPE[meta_dict['ElementType']]
    arrtype   = NDARRAY_TO_ARRAY_TYPE[ndtype]

    with open(data_file, 'rb') as fid:
        binvalues = array.array(arrtype)
        binvalues.fromfile(fid, volume*arr[dim-1])

    data = np.array  (binvalues, ndtype)
    data = np.reshape(data, (arr[dim-1], volume))

    if dim >= 3:
        # Begin 3D fix
        dimensions = [int(i) for i in meta_dict['DimSize'].split()]
        # dimensions.reverse() ??
        data       = data.reshape(dimensions)
        # End 3D fix

    return data, meta_dict


Instruction:
Make a summary of the following Python 3 code
def get_3D_from_4D(filename, vol_idx=0):
    def remove_4th_element_from_hdr_string(hdr, fieldname):
        if fieldname in hdr:
            hdr[fieldname] = ' '.join(hdr[fieldname].split()[:3])

    vol, hdr = load_raw_data_with_mhd(filename)

    if vol.ndim != 4:
        raise ValueError('Volume in {} does not have 4 dimensions.'.format(op.join(op.dirname(filename),
                                                                                hdr['ElementDataFile'])))

    if not 0 <= vol_idx < vol.shape[3]:
        raise IndexError('IndexError: 4th dimension in volume {} has {} volumes, not {}.'.format(filename,
                                                                                vol.shape[3], vol_idx))

    new_vol = vol[:, :, :, vol_idx].copy()

    hdr['NDims'] = 3
    remove_4th_element_from_hdr_string(hdr, 'ElementSpacing')
    remove_4th_element_from_hdr_string(hdr, 'DimSize')

    return new_vol, hdr

Response: Return a 3D volume from a 4D nifti image file.


Instruction:
Can you write a function in Python 3 where it
returns a new object that caches the output of a function func at the specified level.

Response:
def cache(func, memory, func_memory_level=None, memory_level=None,
          **kwargs):
    """ Return a joblib.Memory object.

    The memory_level determines the level above which the wrapped
    function output is cached. By specifying a numeric value for
    this level, the user can to control the amount of cache memory
    used. This function will cache the function call or not
    depending on the cache level.

    Parameters
    ----------
    func: function
        The function which output is to be cached.

    memory: instance of joblib.Memory or string
        Used to cache the function call.

    func_memory_level: int, optional
        The memory_level from which caching must be enabled for the wrapped
        function.

    memory_level: int, optional
        The memory_level used to determine if function call must
        be cached or not (if user_memory_level is equal of greater than
        func_memory_level the function is cached)

    kwargs: keyword arguments
        The keyword arguments passed to memory.cache

    Returns
    -------
    mem: joblib.MemorizedFunc
        object that wraps the function func. This object may be
        a no-op, if the requested level is lower than the value given
        to _cache()). For consistency, a joblib.Memory object is always
        returned.
    """
    verbose = kwargs.get('verbose', 0)

    # memory_level and func_memory_level must be both None or both integers.
    memory_levels = [memory_level, func_memory_level]
    both_params_integers = all(isinstance(lvl, int) for lvl in memory_levels)
    both_params_none = all(lvl is None for lvl in memory_levels)

    if not (both_params_integers or both_params_none):
        raise ValueError('Reference and user memory levels must be both None '
                         'or both integers.')

    if memory is not None and (func_memory_level is None or
                               memory_level >= func_memory_level):
        if isinstance(memory, _basestring):
            memory = Memory(cachedir=memory, verbose=verbose)
        if not isinstance(memory, MEMORY_CLASSES):
            raise TypeError("'memory' argument must be a string or a "
                            "joblib.Memory object. "
                            "%s %s was given." % (memory, type(memory)))
        if (memory.cachedir is None and memory_level is not None
                and memory_level > 1):
            warnings.warn("Caching has been enabled (memory_level = %d) "
                          "but no Memory object or path has been provided"
                          " (parameter memory). Caching deactivated for "
                          "function %s." %
                          (memory_level, func.__name__),
                          stacklevel=2)
    else:
        memory = Memory(cachedir=None, verbose=verbose)
    return _safe_cache(memory, func, **kwargs)


Instruction:
Explain what the following Python 3 code does
def _cache(self, func, func_memory_level=1, **kwargs):

        verbose = getattr(self, 'verbose', 0)

        # Creates attributes if they don't exist
        # This is to make creating them in __init__() optional.
        if not hasattr(self, "memory_level"):
            self.memory_level = 0
        if not hasattr(self, "memory"):
            self.memory = Memory(cachedir=None, verbose=verbose)
        if isinstance(self.memory, _basestring):
            self.memory = Memory(cachedir=self.memory, verbose=verbose)

        # If cache level is 0 but a memory object has been provided, set
        # memory_level to 1 with a warning.
        if self.memory_level == 0:
            if (isinstance(self.memory, _basestring)
                    or self.memory.cachedir is not None):
                warnings.warn("memory_level is currently set to 0 but "
                              "a Memory object has been provided. "
                              "Setting memory_level to 1.")
                self.memory_level = 1

        return cache(func, self.memory, func_memory_level=func_memory_level,
                     memory_level=self.memory_level, **kwargs)

Response: Wrapper for the cache function.


Instruction:
Make a summary of the following Python 3 code
def save_niigz(filepath, vol, header=None, affine=None):
    # delayed import because could not install nipy on Python 3 on OSX
    we_have_nipy = False
    try:
        import nipy.core.image as     niim
        from   nipy            import save_image
    except:
        pass
    else:
        we_have_nipy = True

    if isinstance(vol, np.ndarray):
        log.debug('Saving numpy nifti file: {}.'.format(filepath))
        ni = nib.Nifti1Image(vol, affine, header)
        nib.save(ni, filepath)

    elif isinstance(vol, nib.Nifti1Image):
        log.debug('Saving nibabel nifti file: {}.'.format(filepath))
        nib.save(vol, filepath)

    elif we_have_nipy and isinstance(vol, niim.Image):
        log.debug('Saving nipy nifti file: {}.'.format(filepath))
        save_image(vol, filepath)

    #elif isinstance(vol, NeuroImage):
    #    log.debug('Saving boyle.NeuroImage nifti file: {}.'.format(filepath))
    #    nib.save(vol.img, filepath)

    else:
        raise ValueError('Could not recognise input vol filetype. Got: {}.'.format(repr_imgs(vol)))

Response: Saves a volume into a Nifti file.


Instruction:
Make a summary of the following Python 3 code
def spatialimg_to_hdfgroup(h5group, spatial_img):
    try:
        h5group['data']   = spatial_img.get_data()
        h5group['affine'] = spatial_img.get_affine()

        if hasattr(h5group, 'get_extra'):
            h5group['extra'] = spatial_img.get_extra()

        hdr = spatial_img.get_header()
        for k in list(hdr.keys()):
            h5group['data'].attrs[k] = hdr[k]

    except ValueError as ve:
        raise Exception('Error creating group ' + h5group.name) from ve

Response: Saves a Nifti1Image into an HDF5 group.


Instruction:
Here you have a function in Python 3, explain what it does
def spatialimg_to_hdfpath(file_path, spatial_img, h5path=None, append=True):
    if h5path is None:
        h5path = '/img'

    mode = 'w'
    if os.path.exists(file_path):
        if append:
            mode = 'a'

    with h5py.File(file_path, mode) as f:
        try:
            h5img = f.create_group(h5path)
            spatialimg_to_hdfgroup(h5img, spatial_img)

        except ValueError as ve:
            raise Exception('Error creating group ' + h5path) from ve

Response: Saves a Nifti1Image into an HDF5 file.


Instruction:
Can you generate the documentation for the following Python 3 function
def hdfpath_to_nifti1image(file_path, h5path):
    with h5py.File(file_path, 'r') as f:
        return hdfgroup_to_nifti1image(f[h5path])

Response: Returns a nibabel Nifti1Image from a HDF5 file path


Instruction:
Can you write a function in Python 3 where it
returns a nibabel Nifti1Image from a HDF5 group datasets

Response:
def hdfgroup_to_nifti1image(h5group):
    """Returns a nibabel Nifti1Image from a HDF5 group datasets

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

    Returns
    -------
    nibabel Nifti1Image
    """
    try:
        data   = h5group['data'][:]
        affine = h5group['affine'][:]

        extra = None
        if 'extra' in h5group:
            extra = h5group['extra'][:]

        header = get_nifti1hdr_from_h5attrs(h5group['data'].attrs)

        img = nib.Nifti1Image(data, affine, header=header, extra=extra)

        return img

    except KeyError as ke:
        raise Exception('Could not read Nifti1Image datasets from ' + h5group.name) from ke


Instruction:
Write a Python 3 function that can
transform an H5py Attributes set to a dict.

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

    Parameters
    ----------
    h5attrs: H5py Attributes

    Returns
    --------
    dict
    """
    hdr = nib.Nifti1Header()
    for k in list(h5attrs.keys()):
        hdr[str(k)] = np.array(h5attrs[k])

    return hdr


Instruction:
How would you implement a function in Python 3 that
returns in a list all images found under h5group.

Response:
def all_childnodes_to_nifti1img(h5group):
    """Returns in a list all images found under h5group.

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

    Returns
    -------
    list of nifti1Image
    """
    child_nodes = []
    def append_parent_if_dataset(name, obj):
        if isinstance(obj, h5py.Dataset):
            if name.split('/')[-1] == 'data':
                child_nodes.append(obj.parent)

    vols = []
    h5group.visititems(append_parent_if_dataset)
    for c in child_nodes:
        vols.append(hdfgroup_to_nifti1image(c))

    return vols


Instruction:
Create a Python 3 function for
inserting all given nifti files from file_list into one dataset in fname.

Response:
def insert_volumes_in_one_dataset(file_path, h5path, file_list, newshape=None,
                                  concat_axis=0, dtype=None, append=True):
    """Inserts all given nifti files from file_list into one dataset in fname.
    This will not check if the dimensionality of all files match.

    Parameters
    ----------
    file_path: string
        HDF5 file path

    h5path: string

    file_list: list of strings

    newshape: tuple or lambda function
        If None, it will not reshape the images.
        If a lambda function, this lambda will receive only the shape array.
        e.g., newshape = lambda x: (np.prod(x[0:3]), x[3])
        If a tuple, it will try to reshape all the images with the same shape.
        It must work for all the images in file_list.

    concat_axis: int
        Axis of concatenation after reshaping

    dtype: data type
    Dataset data type
    If not set, will use the type of the first file.

    append: bool

    Raises
    ------
    ValueError if concat_axis is bigger than data dimensionality.

    Note
    ----
    For now, this only works if the dataset ends up being a 2D matrix.
    I haven't tested for multi-dimensionality concatenations.
    """

    def isalambda(v):
        return isinstance(v, type(lambda: None)) and v.__name__ == '<lambda>'

    mode = 'w'
    if os.path.exists(file_path):
        if append:
            mode = 'a'

    #loading the metadata into spatialimages
    imgs = [nib.load(vol) for vol in file_list]

    #getting the shapes of all volumes
    shapes = [np.array(img.get_shape()) for img in imgs]

    #getting the reshaped shapes
    if newshape is not None:
        if isalambda(newshape):
            nushapes = np.array([newshape(shape) for shape in shapes])
        else:
            nushapes = np.array([shape for shape in shapes])

    #checking if concat_axis is available in this new shapes
    for nushape in nushapes:
        assert(len(nushape) - 1 < concat_axis)

    #calculate the shape of the new dataset
    n_dims = nushapes.shape[1]
    ds_shape = np.zeros(n_dims, dtype=np.int)
    for a in list(range(n_dims)):
        if a == concat_axis:
            ds_shape[a] = np.sum(nushapes[:, concat_axis])
        else:
            ds_shape[a] = np.max(nushapes[:, a])

    #get the type of the new dataset
    #dtypes = [img.get_data_dtype() for img in imgs]
    if dtype is None:
        dtype = imgs[0].get_data_dtype()

    with h5py.File(file_path, mode) as f:
        try:
            ic = 0
            h5grp = f.create_group(os.path.dirname(h5path))
            h5ds = h5grp.create_dataset(os.path.basename(h5path),
                                        ds_shape, dtype)
            for img in imgs:

                #get the shape of the current image
                nushape = nushapes[ic, :]

                def append_to_dataset(h5ds, idx, data, concat_axis):
                    """
                    @param h5ds: H5py DataSet
                    @param idx: int
                    @param data: ndarray
                    @param concat_axis: int
                    @return:
                    """
                    shape = data.shape
                    ndims = len(shape)

                    if ndims == 1:
                        if concat_axis == 0:
                            h5ds[idx] = data

                    elif ndims == 2:
                        if concat_axis == 0:
                            h5ds[idx ] = data
                        elif concat_axis == 1:
                            h5ds[idx ] = data

                    elif ndims == 3:
                        if concat_axis == 0:
                            h5ds[idx ] = data
                        elif concat_axis == 1:
                            h5ds[idx ] = data
                        elif concat_axis == 2:
                            h5ds[idx ] = data

                #appending the reshaped image into the dataset
                append_to_dataset(h5ds, ic,
                                  np.reshape(img.get_data(), tuple(nushape)),
                                  concat_axis)

                ic += 1

        except ValueError as ve:
            raise Exception('Error creating group {} in hdf file {}'.format(h5path, file_path)) from ve


Instruction:
Can you generate a brief explanation for the following Python 3 code
def treefall(iterable):
    num_elems = len(iterable)
    for i in range(num_elems, -1, -1):
        for c in combinations(iterable, i):
            yield c

Response: Generator that yields all possible combinations of the elements of iterable and its subsets.


Instruction:
Can you generate the documentation for the following Python 3 function
def get_reliabledictionary_list(client, application_name, service_name):
    cluster = Cluster.from_sfclient(client)
    service = cluster.get_application(application_name).get_service(service_name)
    for dictionary in service.get_dictionaries():
        print(dictionary.name)

Response: List existing reliable dictionaries and respective schema for given application and service.


Instruction:
Write a Python 3 script to
query Schema information for existing reliable dictionaries.

Response:
def get_reliabledictionary_schema(client, application_name, service_name, dictionary_name, output_file=None):
    """Query Schema information for existing reliable dictionaries.

    Query Schema information existing reliable dictionaries for given application and service.

    :param application_name: Name of the application.
    :type application_name: str
    :param service_name: Name of the service.
    :type service_name: str
    :param dictionary: Name of the reliable dictionary.
    :type dictionary: str
    :param output_file: Optional file to save the schema.
    """
    cluster = Cluster.from_sfclient(client)
    dictionary = cluster.get_application(application_name).get_service(service_name).get_dictionary(dictionary_name)

    result = json.dumps(dictionary.get_information(), indent=4)

    if (output_file == None):
        output_file = "{}-{}-{}-schema-output.json".format(application_name, service_name, dictionary_name)

    with open(output_file, "w") as output:
        output.write(result)
    print('Printed schema information to: ' + output_file)
    print(result)


Instruction:
Can you tell what is the following Python 3 function doing
def query_reliabledictionary(client, application_name, service_name, dictionary_name, query_string, partition_key=None, partition_id=None, output_file=None):
    cluster = Cluster.from_sfclient(client)
    dictionary = cluster.get_application(application_name).get_service(service_name).get_dictionary(dictionary_name)

    start = time.time()
    if (partition_id != None):
        result = dictionary.query(query_string, PartitionLookup.ID, partition_id)
    elif (partition_key != None):
        result = dictionary.query(query_string, PartitionLookup.KEY, partition_key)
    else:
        result = dictionary.query(query_string)

    if type(result) is str:
        print(result)
        return
    else:
        result = json.dumps(result.get("value"), indent=4)

    print("Query took " + str(time.time() - start) + " seconds")

    if (output_file == None):
        output_file = "{}-{}-{}-query-output.json".format(application_name, service_name, dictionary_name)

    with open(output_file, "w") as output:
        output.write(result)
    print()
    print('Printed output to: ' + output_file)
    print(result)

Response: Query existing reliable dictionary for given application and service.


Instruction:
Can you write a function in Python 3 where it
executes create update delete operations on existing reliable dictionaries.

Response:
def execute_reliabledictionary(client, application_name, service_name, input_file):
    """Execute create, update, delete operations on existing reliable dictionaries.

    carry out create, update and delete operations on existing reliable dictionaries for given application and service.

    :param application_name: Name of the application.
    :type application_name: str
    :param service_name: Name of the service.
    :type service_name: str
    :param output_file: input file with list of json to provide the operation information for reliable dictionaries.
    """

    cluster = Cluster.from_sfclient(client)
    service = cluster.get_application(application_name).get_service(service_name)

    # call get service with headers and params
    with open(input_file) as json_file:
        json_data = json.load(json_file)
        service.execute(json_data)
    return


Instruction:
How would you explain what the following Python 3 function does
def select_arg_verify(endpoint, cert, key, pem, ca, aad, no_verify): #pylint: disable=invalid-name,too-many-arguments

    if not (endpoint.lower().startswith('http')
            or endpoint.lower().startswith('https')):
        raise CLIError('Endpoint must be HTTP or HTTPS')

    usage = ('Valid syntax : --endpoint [ [ --key --cert | --pem | --aad] '
             '[ --ca | --no-verify ] ]')

    if ca and not (pem or all([key, cert])):
        raise CLIError(usage)

    if no_verify and not (pem or all([key, cert]) or aad):
        raise CLIError(usage)

    if no_verify and ca:
        raise CLIError(usage)

    if any([cert, key]) and not all([cert, key]):
        raise CLIError(usage)

    if aad and any([pem, cert, key]):
        raise CLIError(usage)

    if pem and any([cert, key]):
        raise CLIError(usage)

Response: Verify arguments for select command


Instruction:
Given the following Python 3 function, write the documentation
def select(endpoint, cert=None, key=None, pem=None, ca=None, #pylint: disable=invalid-name, too-many-arguments
           aad=False, no_verify=False):
    #pylint: disable-msg=too-many-locals
    """
    Connects to a Service Fabric cluster endpoint.
    If connecting to secure cluster specify an absolute path to a cert (.crt)
    and key file (.key) or a single file with both (.pem). Do not specify both.
    Optionally, if connecting to a secure cluster, specify also an absolute
    path to a CA bundle file or directory of trusted CA certs.
    :param str endpoint: Cluster endpoint URL, including port and HTTP or HTTPS
    prefix
    :param str cert: Absolute path to a client certificate file
    :param str key: Absolute path to client certificate key file
    :param str pem: Absolute path to client certificate, as a .pem file
    :param str ca: Absolute path to CA certs directory to treat as valid
    or CA bundle
    file
    :param bool aad: Use Azure Active Directory for authentication
    :param bool no_verify: Disable verification for certificates when using
    HTTPS, note: this is an insecure option and should not be used for
    production environments
    """
    from sfctl.config import (set_ca_cert, set_auth, set_aad_cache,
                              set_cluster_endpoint,
                              set_no_verify)
    from msrest import ServiceClient, Configuration
    from sfctl.auth import ClientCertAuthentication, AdalAuthentication

    select_arg_verify(endpoint, cert, key, pem, ca, aad, no_verify)

    if aad:
        new_token, new_cache = get_aad_token(endpoint, no_verify)
        set_aad_cache(new_token, new_cache)
        rest_client = ServiceClient(
            AdalAuthentication(no_verify),
            Configuration(endpoint)
        )

        # Make sure basic GET request succeeds
        rest_client.send(rest_client.get('/')).raise_for_status()
    else:
        client_cert = None
        if pem:
            client_cert = pem
        elif cert:
            client_cert = (cert, key)

        rest_client = ServiceClient(
            ClientCertAuthentication(client_cert, ca, no_verify),
            Configuration(endpoint)
        )

        # Make sure basic GET request succeeds
        rest_client.send(rest_client.get('/')).raise_for_status()

    set_cluster_endpoint(endpoint)
    set_no_verify(no_verify)
    set_ca_cert(ca)
    set_auth(pem, cert, key, aad)

Response: Select a new node from a Service Fabric cluster.


Instruction:
Here you have a function in Python 3, explain what it does
def _openpyxl_read_xl(xl_path: str):
    try:
        wb = load_workbook(filename=xl_path, read_only=True)
    except:
        raise
    else:
        return wb

Response: Use openpyxl to read an Excel file.


Instruction:
Make a summary of the following Python 3 code
def _check_xl_path(xl_path: str):
    xl_path = op.abspath(op.expanduser(xl_path))

    if not op.isfile(xl_path):
        raise IOError("Could not find file in {}.".format(xl_path))

    return xl_path, _use_openpyxl_or_xlrf(xl_path)

Response: Check if the given path is a valid Excel file and return the expanded absolute path of xl_path if exists and openpyxl if it is not.


Instruction:
Explain what the following Python 3 code does
def read_xl(xl_path: str):
    xl_path, choice = _check_xl_path(xl_path)
    reader = XL_READERS[choice]

    return reader(xl_path)

Response: Return the workbook from the Excel file in xl_path.


Instruction:
Here you have a function in Python 3, explain what it does
def get_sheet_list(xl_path: str) -> List:
    wb = read_xl(xl_path)

    if hasattr(wb, 'sheetnames'):
        return wb.sheetnames
    else:
        return wb.sheet_names()

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


Instruction:
Here you have a function in Python 3, explain what it does
def concat_sheets(xl_path: str, sheetnames=None, add_tab_names=False):
    xl_path, choice = _check_xl_path(xl_path)

    if sheetnames is None:
        sheetnames = get_sheet_list(xl_path)

    sheets = pd.read_excel(xl_path, sheetname=sheetnames)

    if add_tab_names:
        for tab in sheets:
            sheets[tab]['Tab'] = [tab] * len(sheets[tab])

    return pd.concat([sheets[tab] for tab in sheets])

Response:
Returns a pandas DataFrame with the concated
    content of the sheetnames from the Excel file inxl_path.


Instruction:
Write a Python 3 script for
raising an AttributeError if df does not have a column named as an item of the list of strings col_names.

Response:
def _check_cols(df, col_names):
    """ Raise an AttributeError if `df` does not have a column named as an item of
    the list of strings `col_names`.
    """
    for col in col_names:
        if not hasattr(df, col):
            raise AttributeError("DataFrame does not have a '{}' column, got {}.".format(col,
                                                                                df.columns))


Instruction:
Explain what the following Python 3 code does
def col_values(df, col_name):
    _check_cols(df, [col_name])

    if 'O' in df[col_name] or pd.np.issubdtype(df[col_name].dtype, str): # if the column is of strings
        return [nom.lower() for nom in df[pd.notnull(df)][col_name] if not pd.isnull(nom)]
    else:
        return [nom for nom in df[pd.notnull(df)][col_name] if not pd.isnull(nom)]

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


Instruction:
Can you implement a function in Python 3 that
returns a DataFrame with the duplicated values of the column col_name in df.

Response:
def duplicated_rows(df, col_name):
    """ Return a DataFrame with the duplicated values of the column `col_name`
    in `df`."""
    _check_cols(df, [col_name])

    dups = df[pd.notnull(df[col_name]) & df.duplicated(subset=[col_name])]
    return dups


Instruction:
Can you implement a function in Python 3 that
returns the duplicated items in values

Response:
def duplicated(values: Sequence):
    """ Return the duplicated items in `values`"""
    vals = pd.Series(values)
    return vals[vals.duplicated()]


Instruction:
Can you generate the documentation for the following Python 3 function
def timestamp_with_tzinfo(dt):
    utc = tzutc()

    if dt.tzinfo:
        dt = dt.astimezone(utc).replace(tzinfo=None)
    return dt.isoformat() + 'Z'

Response: Serialize a date or time value into an ISO8601 text representation with a timezone adjusted to UTC timezone.


Instruction:
Implement a function in Python 3 to
convert all values in data to string all values in data.

Response:
def _to_string(data):
    """ Convert to string all values in `data`.

    Parameters
    ----------
    data: dict[str]->object

    Returns
    -------
    string_data: dict[str]->str
    """
    sdata = data.copy()
    for k, v in data.items():
        if isinstance(v, datetime):
            sdata[k] = timestamp_to_date_str(v)

        elif not isinstance(v, (string_types, float, int)):
            sdata[k] = str(v)

    return sdata


Instruction:
Create a Python 3 function to
insert data into table ensuring that data has unique values in unique_fields.

Response:
def insert_unique(table, data, unique_fields=None, *, raise_if_found=False):
    """Insert `data` into `table` ensuring that data has unique values
    in `table` for the fields listed in `unique_fields`.

    If `raise_if_found` is True, will raise an NotUniqueItemError if
    another item with the same `unique_fields` values are found
    previously in `table`.
    If False, will return the `eid` from the item found.

    Parameters
    ----------
    table: tinydb.Table

    data: dict

    unique_fields: list of str
        Name of fields (keys) from `data` which are going to be used to build
        a sample to look for exactly the same values in the database.
        If None, will use every key in `data`.

    raise_if_found: bool

    Returns
    -------
    eid: int
        Id of the object inserted or the one found with same `unique_fields`.

    Raises
    ------
    MoreThanOneItemError
        Raise even with `raise_with_found` == False if it finds more than one item
        with the same values as the sample.

    NotUniqueItemError
        If `raise_if_found` is True and an item with the same `unique_fields`
        values from `data` is found in `table`.
    """
    item = find_unique(table, data, unique_fields)
    if item is not None:
        if raise_if_found:
            raise NotUniqueItemError('Not expected to find an item with the same '
                                     'values for {}. Inserting {} got {} in eid {}.'.format(unique_fields,
                                                                                data,
                                                                                table.get(eid=item),
                                                                                item))
        else:
            return item

    return table.insert(data)


Instruction:
Explain what the following Python 3 code does
def search_sample(table, sample):
    query = _query_sample(sample=sample, operators='__eq__')

    return table.search(query)

Response: Search for items in table that have the same field sub - set values as in sample.


Instruction:
Can you tell what is the following Python 3 function doing
def search_unique(table, sample, unique_fields=None):
    if unique_fields is None:
        unique_fields = list(sample.keys())

    query = _query_data(sample, field_names=unique_fields, operators='__eq__')
    items = table.search(query)

    if len(items) == 1:
        return items[0]

    if len(items) == 0:
        return None

    raise MoreThanOneItemError('Expected to find zero or one items, but found '
                                '{} items.'.format(len(items)))

Response: Search for items in table that have the same field sub - set values as in sample.


Instruction:
Can you implement a function in Python 3 that
searches in table an item with the value of the unique_fields in the sample.

Response:
def find_unique(table, sample, unique_fields=None):
    """Search in `table` an item with the value of the `unique_fields` in the `sample` sample.
    Check if the the obtained result is unique. If nothing is found will return an empty list,
    if there is more than one item found, will raise an IndexError.

    Parameters
    ----------
    table: tinydb.table

    sample: dict
        Sample data

    unique_fields: list of str
        Name of fields (keys) from `data` which are going to be used to build
        a sample to look for exactly the same values in the database.
        If None, will use every key in `data`.

    Returns
    -------
    eid: int
        Id of the object found with same `unique_fields`.
        None if none is found.

    Raises
    ------
    MoreThanOneItemError
        If more than one example is found.
    """
    res = search_unique(table, sample, unique_fields)
    if res is not None:
        return res.eid
    else:
        return res


Instruction:
Can you create a Python 3 function that
creates a TinyDB query that looks for items that have each field in sample with a value compared with the correspondent operation in operators.

Response:
def _query_sample(sample, operators='__eq__'):
    """Create a TinyDB query that looks for items that have each field in `sample` with a value
    compared with the correspondent operation in `operators`.

    Parameters
    ----------
    sample: dict
        The sample data

    operators: str or list of str
        A list of comparison operations for each field value in `sample`.
        If this is a str, will use the same operator for all `sample` fields.
        If you want different operators for each field, remember to use an OrderedDict for `sample`.
        Check TinyDB.Query class for possible choices.

    Returns
    -------
    query: tinydb.database.Query
    """
    if isinstance(operators, str):
        operators = [operators] * len(sample)

    if len(sample) != len(operators):
        raise ValueError('Expected `operators` to be a string or a list with the same'
                         ' length as `field_names` ({}), got {}.'.format(len(sample),
                                                                         operators))

    queries = []
    for i, fn in enumerate(sample):
        fv = sample[fn]
        op = operators[i]
        queries.append(_build_query(field_name=fn,
                                    field_value=fv,
                                    operator=op))

    return _concat_queries(queries, operators='__and__')


Instruction:
How would you explain what the following Python 3 function does
def _query_data(data, field_names=None, operators='__eq__'):
    if field_names is None:
        field_names = list(data.keys())

    if isinstance(field_names, str):
        field_names = [field_names]

    # using OrderedDict by default, in case operators has different operators for each field.
    sample = OrderedDict([(fn, data[fn]) for fn in field_names])
    return _query_sample(sample, operators=operators)

Response:
Create a tinyDB Query object that looks for items that confirms the correspondent operator
    from operators for each field_names field values from data.


Instruction:
Here you have a function in Python 3, explain what it does
def _concat_queries(queries, operators='__and__'):
    # checks first
    if not queries:
        raise ValueError('Expected some `queries`, got {}.'.format(queries))

    if len(queries) == 1:
        return queries[0]

    if isinstance(operators, str):
        operators = [operators] * (len(queries) - 1)

    if len(queries) - 1 != len(operators):
        raise ValueError('Expected `operators` to be a string or a list with the same'
                         ' length as `field_names` ({}), got {}.'.format(len(queries),
                                                                         operators))

    # recursively build the query
    first, rest, end = queries[0], queries[1:-1], queries[-1:][0]
    bigop = getattr(first, operators[0])
    for i, q in enumerate(rest):
        bigop = getattr(bigop(q), operators[i])

    return bigop(end)

Response: Create a tinyDB Query object that is the concatenation of each query in queries.


Instruction:
Write a Python 3 script to
build a tinyDB Query object with the format that can be used to query the database.

Response:
def _build_query(field_name, field_value, operator='__eq__'):
    """Create a tinyDB Query object with the format:
    (where(`field_name`) `operator` `field_value`)

    Parameters
    ----------
    field_name: str
        The name of the field to be queried.

    field_value:
        The value of the field

    operator: str
        The comparison operator.
        Check TinyDB.Query class for possible choices.

    Returns
    -------
    query: tinydb.database.Query
    """
    qelem = where(field_name)

    if not hasattr(qelem, operator):
        raise NotImplementedError('Operator `{}` not found in query object.'.format(operator))
    else:
        query = getattr(qelem, operator)

    return query(field_value)


Instruction:
Write a Python 3 script for
searching the table with the specified object ID.

Response:
def search_by_eid(self, table_name, eid):
        """Return the element in `table_name` with Object ID `eid`.
        If None is found will raise a KeyError exception.

        Parameters
        ----------
        table_name: str
            The name of the table to look in.

        eid: int
            The Object ID of the element to look for.

        Returns
        -------
        elem: tinydb.database.Element

        Raises
        ------
        KeyError
            If the element with ID `eid` is not found.
        """
        elem = self.table(table_name).get(eid=eid)
        if elem is None:
            raise KeyError('Could not find {} with eid {}.'.format(table_name, eid))

        return elem


Instruction:
Here you have a function in Python 3, explain what it does
def insert_unique(self, table_name, data, unique_fields=None, *, raise_if_found=False):
        return insert_unique(table=self.table(table_name),
                             data=_to_string(data),
                             unique_fields=unique_fields,
                             raise_if_found=raise_if_found)

Response: Insert data into table_name ensuring that data has unique values in unique_fields.


Instruction:
Implement a function in Python 3 to
search in table an item with the value of the unique_fields in the data sample.

Response:
def search_unique(self, table_name, sample, unique_fields=None):
        """ Search in `table` an item with the value of the `unique_fields` in the `data` sample.
        Check if the the obtained result is unique. If nothing is found will return an empty list,
        if there is more than one item found, will raise an IndexError.

        Parameters
        ----------
        table_name: str

        sample: dict
            Sample data

        unique_fields: list of str
            Name of fields (keys) from `data` which are going to be used to build
            a sample to look for exactly the same values in the database.
            If None, will use every key in `data`.

        Returns
        -------
        eid: int
            Id of the object found with same `unique_fields`.
            None if none is found.

        Raises
        ------
        MoreThanOneItemError
            If more than one example is found.
        """
        return search_unique(table=self.table(table_name),
                             sample=sample,
                             unique_fields=unique_fields)


Instruction:
Write a Python 3 script to
search for items in table that have the same field sub - set values as in sample.

Response:
def search_sample(self, table_name, sample):
        """Search for items in `table` that have the same field sub-set values as in `sample`.

        Parameters
        ----------
        table_name: str

        sample: dict
            Sample data

        Returns
        -------
        search_result: list of dict
            List of the items found. The list is empty if no item is found.
        """
        return search_sample(table=self.table(table_name),
                             sample=sample)


Instruction:
Here you have a function in Python 3, explain what it does
def is_unique(self, table_name, sample, unique_fields=None):
        try:
            eid = find_unique(self.table(table_name),
                              sample=sample,
                              unique_fields=unique_fields)
        except:
            return False
        else:
            return eid is not None

Response: Return True if an item with the value of unique_fields from table_name is unique in the table with table_name False otherwise.


Instruction:
How would you explain what the following Python 3 function does
def update_unique(self, table_name, fields, data, cond=None, unique_fields=None,
                      *, raise_if_not_found=False):
        """Update the unique matching element to have a given set of fields.

        Parameters
        ----------
        table_name: str

        fields: dict or function[dict -> None]
            new data/values to insert into the unique element
            or a method that will update the elements.

        data: dict
            Sample data for query

        cond: tinydb.Query
            which elements to update

        unique_fields: list of str

        raise_if_not_found: bool
            Will raise an exception if the element is not found for update.

        Returns
        -------
        eid: int
            The eid of the updated element if found, None otherwise.
        """
        eid = find_unique(self.table(table_name), data, unique_fields)

        if eid is None:
            if raise_if_not_found:
                msg  = 'Could not find {} with {}'.format(table_name, data)
                if cond is not None:
                    msg += ' where {}.'.format(cond)
                raise IndexError(msg)

        else:
            self.table(table_name).update(_to_string(fields), cond=cond, eids=[eid])

        return eid

Response: Update the unique matching element with a given set of fields.


Instruction:
Write a Python 3 function that can
return the number of items that match the sample field values in table table_name

Response:
def count(self, table_name, sample):
        """Return the number of items that match the `sample` field values
        in table `table_name`.
        Check function search_sample for more details.
        """
        return len(list(search_sample(table=self.table(table_name),
                                      sample=sample)))


Instruction:
Write a Python 3 function for
checking if the object contains get_data and get_affine methods and returns a boolean if so returns True if the object contains get_data and get_affine methods and False otherwise.

Response:
def is_img(obj):
    """ Check for get_data and get_affine method in an object

    Parameters
    ----------
    obj: any object
        Tested object

    Returns
    -------
    is_img: boolean
        True if get_data and get_affine methods are present and callable,
        False otherwise.
    """
    try:
        get_data   = getattr(obj, 'get_data')
        get_affine = getattr(obj, 'get_affine')

        return isinstance(get_data,   collections.Callable) and \
               isinstance(get_affine, collections.Callable)
    except AttributeError:
        return False


Instruction:
Write a Python 3 script for
getting the data in the image without having a side effect on the Nifti1Image object

Response:
def get_data(img):
    """Get the data in the image without having a side effect on the Nifti1Image object

    Parameters
    ----------
    img: Nifti1Image

    Returns
    -------
    np.ndarray
    """
    if hasattr(img, '_data_cache') and img._data_cache is None:
        # Copy locally the nifti_image to avoid the side effect of data
        # loading
        img = copy.deepcopy(img)
    # force garbage collector
    gc.collect()
    return img.get_data()


Instruction:
Explain what the following Python 3 code does
def get_shape(img):
    if hasattr(img, 'shape'):
        shape = img.shape
    else:
        shape = img.get_data().shape
    return shape

Response:
Return the shape of the
    image


Instruction:
Can you generate the documentation for the following Python 3 function
def is_valid_coordinate(img, i, j, k):
    imgx, imgy, imgz = get_shape(img)
    return (i >= 0 and i < imgx) and \
           (j >= 0 and j < imgy) and \
           (k >= 0 and k < imgz)

Response: Return True if the given image coordinate values are within the img boundaries.