Instruction:
Implement a Python 3 function for
checking if one_img and another_img are compatible with Nifti1Image.

Response:
def check_img_compatibility(one_img, another_img, only_check_3d=False):
    """Return true if one_img and another_img have the same shape.
    False otherwise.
    If both are nibabel.Nifti1Image will also check for affine matrices.

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

    another_img: nibabel.Nifti1Image  or np.ndarray

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

    Raises
    ------
    NiftiFilesNotCompatible
    """
    nd_to_check = None
    if only_check_3d:
        nd_to_check = 3

    if hasattr(one_img, 'shape') and hasattr(another_img, 'shape'):
        if not have_same_shape(one_img, another_img, nd_to_check=nd_to_check):
            msg = 'Shape of the first image: \n{}\n is different from second one: \n{}'.format(one_img.shape,
                                                                                another_img.shape)
            raise NiftiFilesNotCompatible(repr_imgs(one_img), repr_imgs(another_img), message=msg)

    if hasattr(one_img, 'get_affine') and hasattr(another_img, 'get_affine'):
        if not have_same_affine(one_img, another_img, only_check_3d=only_check_3d):
            msg = 'Affine matrix of the first image: \n{}\n is different ' \
                  'from second one:\n{}'.format(one_img.get_affine(), another_img.get_affine())
            raise NiftiFilesNotCompatible(repr_imgs(one_img), repr_imgs(another_img), message=msg)


Instruction:
How would you explain what the following Python 3 function does
def have_same_affine(one_img, another_img, only_check_3d=False):
    img1 = check_img(one_img)
    img2 = check_img(another_img)

    ndim1 = len(img1.shape)
    ndim2 = len(img2.shape)

    if ndim1 < 3:
        raise ValueError('Image {} has only {} dimensions, at least 3 dimensions is expected.'.format(repr_imgs(img1), ndim1))

    if ndim2 < 3:
        raise ValueError('Image {} has only {} dimensions, at least 3 dimensions is expected.'.format(repr_imgs(img2), ndim1))

    affine1 = img1.get_affine()
    affine2 = img2.get_affine()
    if only_check_3d:
        affine1 = affine1[:3, :3]
        affine2 = affine2[:3, :3]

    try:
        return np.allclose(affine1, affine2)
    except ValueError:
        return False
    except:
        raise

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


Instruction:
Explain what the following Python 3 code does
def _make_it_3d(img):
    shape = get_shape(img)
    if len(shape) == 3:
        return img

    elif (len(shape) == 4 and shape[3] == 1):
        # "squeeze" the image.
        try:
            data   = get_data(img)
            affine = img.get_affine()
            img    = nib.Nifti1Image(data[:, :, :, 0], affine)
        except Exception as exc:
            raise Exception("Error making image '{}' a 3D volume file.".format(img)) from exc
        else:
            return img
    else:
        raise TypeError("A 3D image is expected, but an image with a shape of {} was given.".format(shape))

Response: Enforce that img is a 3D img - like object raise a TypeError.


Instruction:
Here you have a function in Python 3, explain what it does
def check_img(image, make_it_3d=False):
    if isinstance(image, string_types):
        # a filename, load it
        if not op.exists(image):
            raise FileNotFound(image)

        try:
            img = nib.load(image)
            if make_it_3d:
                img = _make_it_3d(img)
        except Exception as exc:
            raise Exception('Error loading image file {}.'.format(image)) from exc
        else:
            return img

    elif isinstance(image, nib.Nifti1Image) or 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 an image is a proper img.


Instruction:
Write a Python 3 function that can
print of img or imgs

Response:
def repr_imgs(imgs):
    """Printing of img or imgs"""
    if isinstance(imgs, string_types):
        return imgs

    if isinstance(imgs, collections.Iterable):
        return '[{}]'.format(', '.join(repr_imgs(img) for img in imgs))

    # try get_filename
    try:
        filename = imgs.get_filename()
        if filename is not None:
            img_str = "{}('{}')".format(imgs.__class__.__name__, filename)
        else:
            img_str = "{}(shape={}, affine={})".format(imgs.__class__.__name__,
                                                       repr(get_shape(imgs)),
                                                       repr(imgs.get_affine()))
    except Exception as exc:
        log.error('Error reading attributes from img.get_filename()')
        return repr(imgs)
    else:
        return img_str


Instruction:
Can you generate the documentation for the following Python 3 function
def have_same_shape(array1, array2, nd_to_check=None):
    shape1 = array1.shape
    shape2 = array2.shape
    if nd_to_check is not None:
        if len(shape1) < nd_to_check:
            msg = 'Number of dimensions to check {} is out of bounds for the shape of the first image: \n{}\n.'.format(shape1)
            raise ValueError(msg)
        elif len(shape2) < nd_to_check:
            msg = 'Number of dimensions to check {} is out of bounds for the shape of the second image: \n{}\n.'.format(shape2)
            raise ValueError(msg)

        shape1 = shape1[:nd_to_check]
        shape2 = shape2[:nd_to_check]

    return shape1 == shape2

Response: Returns True if array1 and array2 have the same shapes False otherwise.


Instruction:
Can you write a function in Python 3 where it
returns True if both images have the same geometry.

Response:
def have_same_geometry(fname1, fname2):
    """
    @param fname1: string
    File path of an image

    @param fname2: string
    File path of an image

    @return: bool
    True if both have the same geometry
    """
    img1shape = nib.load(fname1).get_shape()
    img2shape = nib.load(fname2).get_shape()
    return have_same_shape(img1shape, img2shape)


Instruction:
Can you implement a function in Python 3 that
returns True if both images have the same spatial geometry.

Response:
def have_same_spatial_geometry(fname1, fname2):
    """
    @param fname1: string
    File path of an image

    @param fname2: string
    File path of an image

    @return: bool
    True if both have the same geometry
    """
    img1shape = nib.load(fname1).get_shape()
    img2shape = nib.load(fname2).get_shape()
    return img1shape[:3] == img2shape[:3]


Instruction:
How would you explain what the following Python 3 function does
def dir_match(regex, wd=os.curdir):
    ls = os.listdir(wd)

    filt = re.compile(regex).match
    return filter_list(ls, filt)

Response:
Create a list of regex matches that result from the match_regex
    of all files within wd.


Instruction:
Write a Python 3 function for
returning absolute paths of folders that match the regex within folder_path and all its children folders. Note: The regex matching is done using the match function of the re module. Parameters ---------- folder_path: string regex: string Returns ------- A list of strings.

Response:
def recursive_dir_match(folder_path, regex=''):
    """
    Returns absolute paths of folders that match the regex within folder_path and
    all its children folders.

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

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

    regex: string

    Returns
    -------
    A list of strings.
    """
    outlist = []
    for root, dirs, files in os.walk(folder_path):
        outlist.extend([op.join(root, f) for f in dirs
                        if re.match(regex, f)])

    return outlist


Instruction:
Can you implement a function in Python 3 that
returns a list of files that match the search_regex within file_dir.

Response:
def get_file_list(file_dir, regex=''):
    """
    Creates a list of files that match the search_regex within file_dir.
    The list of files will have file_dir as path prefix.

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

    @param search_regex:

    Returns:
    --------
    List of paths to files that match the search_regex
    """
    file_list = os.listdir(file_dir)
    file_list.sort()

    if regex:
        file_list = search_list(file_list, regex)

    file_list = [op.join(file_dir, fname) for fname in file_list]

    return file_list


Instruction:
Can you generate a brief explanation for the following Python 3 code
def recursive_find_search(folder_path, regex=''):
    outlist = []
    for root, dirs, files in os.walk(folder_path):
        outlist.extend([op.join(root, f) for f in files
                        if re.search(regex, f)])

    return outlist

Response: Recursive function to find files that match the regex within folder_path and return a list of strings.


Instruction:
Given the following Python 3 function, write the documentation
def iter_recursive_find(folder_path, *regex):
    for root, dirs, files in os.walk(folder_path):
        if len(files) > 0:
            outlist = []
            for f in files:
                for reg in regex:
                    if re.search(reg, f):
                        outlist.append(op.join(root, f))
            if len(outlist) == len(regex):
                yield outlist

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


Instruction:
Can you create a Python 3 function that
uses glob to find all files or folders that match the regex starting from the base_directory.

Response:
def recursive_glob(base_directory, regex=''):
    """
    Uses glob to find all files or folders that match the regex
    starting from the base_directory.

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

    regex: str

    Returns
    -------
    files: list

    """
    files = glob(op.join(base_directory, regex))
    for path, dirlist, filelist in os.walk(base_directory):
        for dir_name in dirlist:
            files.extend(glob(op.join(path, dir_name, regex)))

    return files


Instruction:
Write a Python 3 function that can
return the path to the latest modified file in input_dir.

Response:
def get_last_file(input_dir, glob_pattern='*', key=op.getctime, reverse=True):
    """ Return the path to the latest file in `input_dir`.
     The `key` argument defines which information to use for sorting
     the list of files, could be:
        - creation date: os.path.getctime,
        - modification date: os.path.getmtime,
        etc.

    Parameters
    ----------
    input_dir: str
        Path to the folder where to perform the `glob`.

    glob_pattern: str
        `glob` Pattern to filter the files in `input_dir`.

    key: str
        Sorting key function

    reverse: bool
        Set to True if you want the sorting to be in decreasing order,
        False otherwise.

    Returns
    -------
    latest_filepath: str
        Path to the latest modified file in `input_dir`.
    """
    files = glob(op.join(input_dir, glob_pattern))
    files.sort(key=key, reverse=reverse)
    return files[0]


Instruction:
Write a Python 3 function for
composing an error message with keyword arguments.

Response:
def compose_err_msg(msg, **kwargs):
    """Append key-value pairs to msg, for display.

    Parameters
    ----------
    msg: string
        arbitrary message
    kwargs: dict
        arbitrary dictionary

    Returns
    -------
    updated_msg: string
        msg, with "key: value" appended. Only string values are appended.

    Example
    -------
    >>> compose_err_msg('Error message with arguments...', arg_num=123, \
        arg_str='filename.nii', arg_bool=True)
    'Error message with arguments...\\narg_str: filename.nii'
    >>>
    """
    updated_msg = msg
    for k, v in sorted(kwargs.items()):
        if isinstance(v, _basestring):  # print only str-like arguments
            updated_msg += "\n" + k + ": " + v

    return updated_msg


Instruction:
Here you have a function in Python 3, explain what it does
def group_dicom_files(dicom_file_paths, header_fields):
    dist = SimpleDicomFileDistance(field_weights=header_fields)

    path_list = dicom_file_paths.copy()

    path_groups = DefaultOrderedDict(DicomFileSet)

    while len(path_list) > 0:
        file_path1 = path_list.pop()
        file_subgroup = [file_path1]

        dist.set_dicom_file1(file_path1)
        j = len(path_list)-1
        while j >= 0:
            file_path2 = path_list[j]
            dist.set_dicom_file2(file_path2)

            if dist.transform():
                file_subgroup.append(file_path2)
                path_list.pop(j)

            j -= 1
        path_groups[file_path1].from_set(file_subgroup, check_if_dicoms=False)

    return path_groups

Response: Returns a list of lists of DICOM file absolute paths and returns a list of lists of DICOM file paths that have the same header fields.


Instruction:
Can you generate the documentation for the following Python 3 function
def copy_groups_to_folder(dicom_groups, folder_path, groupby_field_name):
    if dicom_groups is None or not dicom_groups:
        raise ValueError('Expected a boyle.dicom.sets.DicomFileSet.')
    if not os.path.exists(folder_path):
        os.makedirs(folder_path, exist_ok=False)

    for dcmg in dicom_groups:
        if groupby_field_name is not None and len(groupby_field_name) > 0:
            dfile = DicomFile(dcmg)
            dir_name = ''
            for att in groupby_field_name:
                dir_name = os.path.join(dir_name, dfile.get_attributes(att))
            dir_name = str(dir_name)
        else:
            dir_name = os.path.basename(dcmg)

        group_folder = os.path.join(folder_path, dir_name)
        os.makedirs(group_folder, exist_ok=False)

        log.debug('Copying files to {}.'.format(group_folder))

        import shutil
        dcm_files = dicom_groups[dcmg]

        for srcf in dcm_files:
            destf = os.path.join(group_folder, os.path.basename(srcf))
            while os.path.exists(destf):
                destf += '+'
            shutil.copy2(srcf, destf)

Response: Copy the DICOM files in the group_name list to the folder_path.


Instruction:
Can you write a function in Python 3 where it
calculates the Levenshtein distance between all files in dicom_files using an anonymized Levenshtein measure between all field names in field_weights and atabase.

Response:
def calculate_file_distances(dicom_files, field_weights=None,
                             dist_method_cls=None, **kwargs):
    """
    Calculates the DicomFileDistance between all files in dicom_files, using an
    weighted Levenshtein measure between all field names in field_weights and
    their corresponding weights.

    Parameters
    ----------
    dicom_files: iterable of str
        Dicom file paths

    field_weights: dict of str to float
        A dict with header field names to float scalar values, that
        indicate a distance measure ratio for the levenshtein distance
        averaging of all the header field names in it. e.g., {'PatientID': 1}

    dist_method_cls: DicomFileDistance class
        Distance method object to compare the files.
        If None, the default DicomFileDistance method using Levenshtein
        distance between the field_wieghts will be used.

    kwargs: DicomFileDistance instantiation named arguments
        Apart from the field_weitghts argument.

    Returns
    -------
    file_dists: np.ndarray or scipy.sparse.lil_matrix of shape NxN
        Levenshtein distances between each of the N items in dicom_files.
    """
    if dist_method_cls is None:
        dist_method = LevenshteinDicomFileDistance(field_weights)
    else:
        try:
            dist_method = dist_method_cls(field_weights=field_weights, **kwargs)
        except:
            log.exception('Could not instantiate {} object with field_weights '
                          'and {}'.format(dist_method_cls, kwargs))

    dist_dtype = np.float16
    n_files = len(dicom_files)

    try:
        file_dists = np.zeros((n_files, n_files), dtype=dist_dtype)
    except MemoryError as mee:
        import scipy.sparse
        file_dists = scipy.sparse.lil_matrix((n_files, n_files),
                                             dtype=dist_dtype)

    for idxi in range(n_files):
        dist_method.set_dicom_file1(dicom_files[idxi])

        for idxj in range(idxi+1, n_files):
            dist_method.set_dicom_file2(dicom_files[idxj])

            if idxi != idxj:
                file_dists[idxi, idxj] = dist_method.transform()

    return file_dists


Instruction:
Write a Python 3 script to
fit the set of keys to the set of dictionaries.

Response:
def fit(self, dcm_file1, dcm_file2):
        """
        Parameters
        ----------
        dcm_file1: str (path to file) or DicomFile or namedtuple

        dcm_file2: str (path to file) or DicomFile or namedtuple
        """
        self.set_dicom_file1(dcm_file1)
        self.set_dicom_file2(dcm_file2)


Instruction:
Explain what the following Python 3 code does
def transform(self):
        if self.dcmf1 is None or self.dcmf2 is None:
            return np.inf

        for field_name in self.field_weights:
            if (str(getattr(self.dcmf1, field_name, ''))
                    != str(getattr(self.dcmf2, field_name, ''))):
                return False

        return True

Response: Returns True if all the field values in self. dcmf1 and self. dcmf2 are the same False otherwise.


Instruction:
How would you explain what the following Python 3 function does
def levenshtein_analysis(self, field_weights=None):
        if field_weights is None:
            if not isinstance(self.field_weights, dict):
                raise ValueError('Expected a dict for `field_weights` parameter, '
                                 'got {}'.format(type(self.field_weights)))

        key_dicoms = list(self.dicom_groups.keys())
        file_dists = calculate_file_distances(key_dicoms, field_weights, self._dist_method_cls)
        return file_dists

Response: Calculates the levenshtein distance averaging of all the header fields in the cluster.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def dist_percentile_threshold(dist_matrix, perc_thr=0.05, k=1):
        triu_idx = np.triu_indices(dist_matrix.shape[0], k=k)
        upper = np.zeros_like(dist_matrix)
        upper[triu_idx] = dist_matrix[triu_idx] < np.percentile(dist_matrix[triu_idx], perc_thr)
        return upper

Response: Thresholds a distance matrix and returns the result.


Instruction:
Implement a function in Python 3 to
return a list of 2 - tuples with pairs of dicom groups that are in the same folder within given depth.

Response:
def get_groups_in_same_folder(self, folder_depth=3):
        """
        Returns a list of 2-tuples with pairs of dicom groups that
        are in the same folder within given depth.

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

        Returns
        -------
        list of tuples of str
        """
        group_pairs = []
        key_dicoms = list(self.dicom_groups.keys())
        idx = len(key_dicoms)
        while idx > 0:
            group1 = key_dicoms.pop()
            dir_group1 = get_folder_subpath(group1, folder_depth)
            for group in key_dicoms:
                if group.startswith(dir_group1):
                    group_pairs.append((group1, group))
            idx -= 1

        return group_pairs


Instruction:
Can you implement a function in Python 3 that
plots the distances between files in the file.

Response:
def plot_file_distances(dist_matrix):
        """
        Plots dist_matrix

        Parameters
        ----------
        dist_matrix: np.ndarray
        """
        import matplotlib.pyplot as plt
        fig = plt.figure()
        ax = fig.add_subplot(111)

        ax.matshow(dist_matrix, interpolation='nearest',
                   cmap=plt.cm.get_cmap('PuBu'))


Instruction:
Write a Python 3 script to
extend the DICOM groups dictionary with the lists of items in indices.

Response:
def merge_groups(self, indices):
        """Extend the lists within the DICOM groups dictionary.
        The indices will indicate which list have to be extended by which
        other list.

        Parameters
        ----------
        indices: list or tuple of 2 iterables of int, bot having the same len
             The indices of the lists that have to be merged, both iterables
             items will be read pair by pair, the first is the index to the
             list that will be extended with the list of the second index.
             The indices can be constructed with Numpy e.g.,
             indices = np.where(square_matrix)
        """
        try:
            merged = merge_dict_of_lists(self.dicom_groups, indices,
                                         pop_later=True, copy=True)
            self.dicom_groups = merged
        except IndexError:
            raise IndexError('Index out of range to merge DICOM groups.')


Instruction:
Can you write a function in Python 3 where it
moves the file groups into a folder.

Response:
def move_to_folder(self, folder_path, groupby_field_name=None):
        """Copy the file groups to folder_path. Each group will be copied into
        a subfolder with named given by groupby_field.

        Parameters
        ----------
        folder_path: str
         Path to where copy the DICOM files.

        groupby_field_name: str
         DICOM field name. Will get the value of this field to name the group
         folder. If empty or None will use the basename of the group key file.
        """
        try:
            copy_groups_to_folder(self.dicom_groups, folder_path, groupby_field_name)
        except IOError as ioe:
            raise IOError('Error moving dicom groups to {}.'.format(folder_path)) from ioe


Instruction:
Make a summary of the following Python 3 code
def get_unique_field_values_per_group(self, field_name,
                                          field_to_use_as_key=None):
        """Return a dictionary where the key is the group key file path and
        the values are sets of unique values of the field name of all DICOM
        files in the group.

        Parameters
        ----------
        field_name: str
         Name of the field to read from all files

        field_to_use_as_key: str
         Name of the field to get the value and use as key.
         If None, will use the same key as the dicom_groups.

        Returns
        -------
        Dict of sets
        """
        unique_vals = DefaultOrderedDict(set)
        for dcmg in self.dicom_groups:
            for f in self.dicom_groups[dcmg]:
                field_val = DicomFile(f).get_attributes(field_name)
                key_val = dcmg
                if field_to_use_as_key is not None:
                    try:
                        key_val = str(DicomFile(dcmg).get_attributes(field_to_use_as_key))
                    except KeyError as ke:
                        raise KeyError('Error getting field {} from '
                                      'file {}'.format(field_to_use_as_key,
                                                       dcmg)) from ke
                unique_vals[key_val].add(field_val)

        return unique_vals

Response: Get the unique values of a field in a group.


Instruction:
Can you tell what is the following Python 3 function doing
def get_config_value(name, fallback=None):

    cli_config = CLIConfig(SF_CLI_CONFIG_DIR, SF_CLI_ENV_VAR_PREFIX)

    return cli_config.get('servicefabric', name, fallback)

Response: Gets a config value by name.


Instruction:
Write a Python 3 script for
checking if a config value is set to a valid bool value.

Response:
def get_config_bool(name):
    """Checks if a config value is set to a valid bool value."""

    cli_config = CLIConfig(SF_CLI_CONFIG_DIR, SF_CLI_ENV_VAR_PREFIX)
    return cli_config.getboolean('servicefabric', name, False)


Instruction:
Can you write a function in Python 3 where it
sets a config by name to a value.

Response:
def set_config_value(name, value):
    """Set a config by name to a value."""

    cli_config = CLIConfig(SF_CLI_CONFIG_DIR, SF_CLI_ENV_VAR_PREFIX)
    cli_config.set_value('servicefabric', name, value)


Instruction:
Create a Python 3 function for
returning the path to certificate related files either a single file path or a tuple.

Response:
def cert_info():
    """Path to certificate related files, either a single file path or a
    tuple. In the case of no security, returns None."""

    sec_type = security_type()
    if sec_type == 'pem':
        return get_config_value('pem_path', fallback=None)
    if sec_type == 'cert':
        cert_path = get_config_value('cert_path', fallback=None)
        key_path = get_config_value('key_path', fallback=None)
        return cert_path, key_path

    return None


Instruction:
Make a summary of the following Python 3 code
def set_aad_cache(token, cache):
    set_config_value('aad_token', jsonpickle.encode(token))
    set_config_value('aad_cache', jsonpickle.encode(cache))

Response: Set AAD token cache.


Instruction:
Implement a function in Python 3 to
set the authentication settings for the current application.

Response:
def set_auth(pem=None, cert=None, key=None, aad=False):
    """Set certificate usage paths"""

    if any([cert, key]) and pem:
        raise ValueError('Cannot specify both pem and cert or key')

    if any([cert, key]) and not all([cert, key]):
        raise ValueError('Must specify both cert and key')

    if pem:
        set_config_value('security', 'pem')
        set_config_value('pem_path', pem)
    elif cert or key:
        set_config_value('security', 'cert')
        set_config_value('cert_path', cert)
        set_config_value('key_path', key)
    elif aad:
        set_config_value('security', 'aad')
    else:
        set_config_value('security', 'none')


Instruction:
Can you generate a brief explanation for the following Python 3 code
def filter_objlist(olist, fieldname, fieldval):
    return [x for x in olist if getattr(x, fieldname) == fieldval]

Response: Returns a list with of the objects in olist that have a fieldname valued as fieldval


Instruction:
Make a summary of the following Python 3 code
def filter_list(lst, filt):
    return [m for s in lst for m in (filt(s),) if m]

Response: Returns a list of items that pass the filter function filter


Instruction:
How would you implement a function in Python 3 that
returns a list of strings that match the pattern

Response:
def match_list(lst, pattern, group_names=[]):
    """
    Parameters
    ----------
    lst: list of str

    regex: string

    group_names: list of strings
        See re.MatchObject group docstring

    Returns
    -------
    list of strings
        Filtered list, with the strings that match the pattern
    """
    filtfn = re.compile(pattern).match
    filtlst = filter_list(lst, filtfn)
    if not group_names:
        return [m.string for m in filtlst]
    else:
        return [m.group(group_names) for m in filtlst]


Instruction:
How would you explain what the following Python 3 function does
def append_to_keys(adict, preffix):
    return {preffix + str(key): (value if isinstance(value, dict) else value)
            for key, value in list(adict.items())}

Response: Returns a new dict with the preffix appended to the keys of the input dict.


Instruction:
Can you generate the documentation for the following Python 3 function
def is_valid_regex(string):
    try:
        re.compile(string)
        is_valid = True
    except re.error:
        is_valid = False
    return is_valid

Response: Checks whether the given regular expression is valid.


Instruction:
Can you generate the documentation for the following Python 3 function
def is_regex(string):
    is_regex = False
    regex_chars = ['\\', '(', '+', '^', '$']
    for c in regex_chars:
        if string.find(c) > -1:
            return is_valid_regex(string)
    return is_regex

Response: Returns True if the given string is a regular expression False otherwise.


Instruction:
Can you generate the documentation for the following Python 3 function
def is_fnmatch_regex(string):
    is_regex = False
    regex_chars = ['!', '*', '$']
    for c in regex_chars:
        if string.find(c) > -1:
            return True
    return is_regex

Response: Returns True if the given string is considered a fnmatch - style regular expression False otherwise.


Instruction:
Explain what the following Python 3 code does
def where_is(strings, pattern, n=1, lookup_func=re.match):
    count = 0
    for idx, item in enumerate(strings):
        if lookup_func(pattern, item):
            count += 1
            if count == n:
                return idx
    return -1

Response: Returns the index of the nth match found of pattern in strings


Instruction:
Given the following Python 3 function, write the documentation
def generate_config(output_directory):
    if not op.isdir(output_directory):
        os.makedirs(output_directory)

    config_file = op.join(output_directory, "config.ini")
    open_file = open(config_file, "w")
    open_file.write("[BOOL]\nManualNIfTIConv=0\n")
    open_file.close()
    return config_file

Response:
Generate a dcm2nii configuration file that disable the interactive
    mode.


Instruction:
Implement a function in Python 3 to
add slice duration and acquisition times to the nifit1 file in nii_file.

Response:
def add_meta_to_nii(nii_file, dicom_file, dcm_tags=''):
    """ Add slice duration and acquisition times to the headers of the nifit1 files in `nii_file`.
    It will add the repetition time of the DICOM file (field: {0x0018, 0x0080, DS, Repetition Time})
    to the NifTI file as well as any other tag in `dcm_tags`.
    All selected DICOM tags values are set in the `descrip` nifti header field.
    Note that this will modify the header content of `nii_file`.

    Parameters
    ----------
    nii_files: str
        Path to the NifTI file to modify.

    dicom_file: str
        Paths to the DICOM file from where to get the meta data.

    dcm_tags: list of str
        List of tags from the DICOM file to read and store in the nifti file.
    """
    # Load a dicom image
    dcmimage = dicom.read_file(dicom_file)

    # Load the nifti1 image
    image = nibabel.load(nii_file)

    # Check the we have a nifti1 format image
    if not isinstance(image, nibabel.nifti1.Nifti1Image):
        raise Exception(
            "Only Nifti1 image are supported not '{0}'.".format(
                type(image)))

    # check if dcm_tags is one string, if yes put it in a list:
    if isinstance(dcm_tags, str):
        dcm_tags = [dcm_tags]

    # Fill the nifti1 header
    header = image.get_header()

    # slice_duration: Time for 1 slice
    repetition_time = float(dcmimage[("0x0018", "0x0080")].value)
    header.set_dim_info(slice=2)
    nb_slices = header.get_n_slices()
    # Force round to 0 digit after coma. If more, nibabel completes to
    # 6 digits with random numbers...
    slice_duration = round(repetition_time / nb_slices, 0)
    header.set_slice_duration(slice_duration)

    # add free dicom fields
    if dcm_tags:
        content = ["{0}={1}".format(name, dcmimage[tag].value)
                   for name, tag in dcm_tags]
        free_field = numpy.array(";".join(content),
                                 dtype=header["descrip"].dtype)
        image.get_header()["descrip"] = free_field

    # Update the image header
    image.update_header()

    # Save the filled image
    nibabel.save(image, nii_file)


Instruction:
Implement a Python 3 function for
converting all DICOM files within `work_dir` into one or more NifTi files by calling dcm2nii on this folder. Parameters ---------- work_dir: str Path to the folder that contain the DICOM files arguments: str String containing all the flag arguments for `dcm2nii` CLI. Returns ------- sys_code: int dcm2nii execution return code

Response:
def call_dcm2nii(work_dir, arguments=''):
    """Converts all DICOM files within `work_dir` into one or more
    NifTi files by calling dcm2nii on this folder.

    Parameters
    ----------
    work_dir: str
        Path to the folder that contain the DICOM files

    arguments: str
        String containing all the flag arguments for `dcm2nii` CLI.

    Returns
    -------
    sys_code: int
        dcm2nii execution return code
    """
    if not op.exists(work_dir):
        raise IOError('Folder {} not found.'.format(work_dir))

    cmd_line = 'dcm2nii {0} "{1}"'.format(arguments, work_dir)
    log.info(cmd_line)
    return subprocess.check_call(cmd_line, shell=True)


Instruction:
Implement a Python 3 function for
converting DICOM files inside input_dir to Nifti and save the Nifti file in output_dir with a filename prefix.

Response:
def convert_dcm2nii(input_dir, output_dir, filename):
    """ Call MRICron's `dcm2nii` to convert the DICOM files inside `input_dir`
    to Nifti and save the Nifti file in `output_dir` with a `filename` prefix.

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

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

    filename: str
        Output file basename

    Returns
    -------
    filepaths: list of str
        List of file paths created in `output_dir`.
    """
    # a few checks before doing the job
    if not op.exists(input_dir):
        raise IOError('Expected an existing folder in {}.'.format(input_dir))

    if not op.exists(output_dir):
        raise IOError('Expected an existing output folder in {}.'.format(output_dir))

    # create a temporary folder for dcm2nii export
    tmpdir = tempfile.TemporaryDirectory(prefix='dcm2nii_')

    # call dcm2nii
    arguments = '-o "{}" -i y'.format(tmpdir.name)
    try:
        call_out = call_dcm2nii(input_dir, arguments)
    except:
        raise
    else:
        log.info('Converted "{}" to nifti.'.format(input_dir))

        # get the filenames of the files that dcm2nii produced
        filenames  = glob(op.join(tmpdir.name, '*.nii*'))

        # cleanup `filenames`, using only the post-processed (reoriented, cropped, etc.) images by dcm2nii
        cleaned_filenames = remove_dcm2nii_underprocessed(filenames)

        # copy files to the output_dir
        filepaths = []
        for srcpath in cleaned_filenames:
            dstpath = op.join(output_dir, filename)
            realpath = copy_w_plus(srcpath, dstpath)
            filepaths.append(realpath)

            # copy any other file produced by dcm2nii that is not a NifTI file, e.g., *.bvals, *.bvecs, etc.
            basename = op.basename(remove_ext(srcpath))
            aux_files = set(glob(op.join(tmpdir.name, '{}.*'     .format(basename)))) - \
                        set(glob(op.join(tmpdir.name, '{}.nii*'.format(basename))))
            for aux_file in aux_files:
                aux_dstpath = copy_w_ext(aux_file, output_dir, remove_ext(op.basename(realpath)))
                filepaths.append(aux_dstpath)

        return filepaths


Instruction:
Can you implement a function in Python 3 that
returns a subset of filepaths. Keep only the files that have a basename longer than the others with same suffix.

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

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

    Returns
    -------
    cleaned_paths: iterable of str
    """
    cln_flist = []

    # sort them by size
    len_sorted = sorted(filepaths, key=len)

    for idx, fpath in enumerate(len_sorted):
        remove = False

        # get the basename and the rest of the files
        fname = op.basename(fpath)
        rest  = len_sorted[idx+1:]

        # check if the basename is in the basename of the rest of the files
        for rest_fpath in rest:
            rest_file = op.basename(rest_fpath)
            if rest_file.endswith(fname):
                remove = True
                break

        if not remove:
            cln_flist.append(fpath)

    return cln_flist


Instruction:
Create a Python 3 function for
transforming a named tuple into a dictionary

Response:
def dictify(a_named_tuple):
    """Transform a named tuple into a dictionary"""
    return dict((s, getattr(a_named_tuple, s)) for s in a_named_tuple._fields)


Instruction:
Can you generate the documentation for the following Python 3 function
def merge_dict_of_lists(adict, indices, pop_later=True, copy=True):
    def check_indices(idxs, x):
        for i in chain(*idxs):
            if i < 0 or i >= x:
                raise IndexError("Given indices are out of dict range.")

    check_indices(indices, len(adict))

    rdict = adict.copy() if copy else adict

    dict_keys = list(rdict.keys())
    for i, j in zip(*indices):
        rdict[dict_keys[i]].extend(rdict[dict_keys[j]])

    if pop_later:
        for i, j in zip(*indices):
            rdict.pop(dict_keys[j], '')

    return rdict

Response: Extend the within a dict of lists.


Instruction:
Here you have a function in Python 3, explain what it does
def append_dict_values(list_of_dicts, keys=None):
    if keys is None:
        keys = list(list_of_dicts[0].keys())

    dict_of_lists = DefaultOrderedDict(list)
    for d in list_of_dicts:
        for k in keys:
            dict_of_lists[k].append(d[k])
    return dict_of_lists

Response: Returns a dict of lists from a list of dicts with the same keys and append them to the output dict.


Instruction:
Can you tell what is the following Python 3 function doing
def import_pyfile(filepath, mod_name=None):
    import sys
    if sys.version_info.major == 3:
        import importlib.machinery
        loader = importlib.machinery.SourceFileLoader('', filepath)
        mod = loader.load_module(mod_name)
    else:
        import imp
        mod = imp.load_source(mod_name, filepath)

    return mod

Response: Imports the contents of filepath as a Python module.


Instruction:
Write a Python 3 script to
copy the files in the file tree map to destpath.

Response:
def copy(configfile='', destpath='', overwrite=False, sub_node=''):
    """Copies the files in the built file tree map
    to despath.

    :param configfile: string
     Path to the FileTreeMap config file

    :param destpath: string
     Path to the files destination

    :param overwrite: bool
     Overwrite files if they already exist.

    :param sub_node: string
     Tree map configuration sub path.
     Will copy only the contents within this sub-node

    """
    log.info('Running {0} {1} {2}'.format(os.path.basename(__file__),
                                          whoami(),
                                          locals()))

    assert(os.path.isfile(configfile))

    if os.path.exists(destpath):
        if os.listdir(destpath):
            raise FolderAlreadyExists('Folder {0} already exists. Please clean '
                                      'it or change destpath.'.format(destpath))
    else:
        log.info('Creating folder {0}'.format(destpath))
        path(destpath).makedirs_p()

    from boyle.files.file_tree_map import FileTreeMap
    file_map = FileTreeMap()

    try:
        file_map.from_config_file(configfile)
    except Exception as e:
        raise FileTreeMapError(str(e))

    if sub_node:
        sub_map = file_map.get_node(sub_node)
        if not sub_map:
            raise FileTreeMapError('Could not find sub node '
                                   '{0}'.format(sub_node))

        file_map._filetree = {}
        file_map._filetree[sub_node] = sub_map

    try:
        file_map.copy_to(destpath, overwrite=overwrite)
    except Exception as e:
        raise FileTreeMapError(str(e))


Instruction:
Can you write a function in Python 3 where it
returns ndarray of references in self. reflst whose self id is None.

Response:
def get_noneid_references(self):
        """
        Returns
        -------
        ndarray
        Array of references in self.reflst whose self id is None.
        """
        #return [self.reflst[idx] for idx, idval in enumerate(self) if idval is None]
        try:
            nun = np.array(None).astype(self.dtype)
            return np.array(self.reflst)[self == nun]
        except:
            nun = None
            return np.array(self.reflst)[self is None]


Instruction:
Create a Python 3 function for
printing a table of general vs. reference names.

Response:
def _print_general_vs_table(self, idset1, idset2):
        """
        :param idset1:
        :param idset2:
        """
        ref1name = ''
        set1_hasref = isinstance(idset1, idset_with_reference)
        if set1_hasref:
            ref1arr = np.array(idset1.reflst)
            ref1name = idset1.refname

        ref2name = ref1name
        set2_hasref = isinstance(idset2, idset_with_reference)
        if set2_hasref:
            ref2arr = np.array(idset2.reflst)
            ref2name = idset2.refname
        else:
            ref2name = ref1name

        #First show a general table
        hdr11 = '{0} > {1}'.format(idset1.name, idset2.name)
        hdr12 = '{0} > {1} {2}'.format(idset1.name, idset2.name, ref2name)
        hdr13 = '{0} < {1}'.format(idset1.name, idset2.name)
        hdr14 = '{0} < {1} {2}'.format(idset1.name, idset2.name, ref1name)
        table = [[hdr11, hdr12, hdr13, hdr14]]

        set1 = set(idset1)
        set2 = set(idset2)
        row11 = list(set1 - set2)
        if set1_hasref:
            row12 = [ref1arr[np.where(idset1 == nom)][0] for nom in row11]
        else:
            row12 = ['Not found' for _ in row11]

        row13 = list(set2 - set1)
        if set2_hasref:
            row14 = [ref2arr[np.where(idset2 == nom)][0] for nom in row13]
        else:
            row14 = ['Not found' for _ in row13]

        tablst = self._tabulate_4_lists(row11, row12, row13, row14)
        table.extend(tablst)

        if len(table) > 1:
            print(tabulate(table, headers='firstrow'))
            print('\n')


Instruction:
Given the following Python 3 function, write the documentation
def _print_foreign_repetition_table(self, idset1, idset2):

        assert(isinstance(idset1, idset_with_reference))
        assert(isinstance(idset2, idset))

        reps = idset2.get_repetitions()
        if len(reps) < 1:
            return

        refs = np.array(idset1.reflst)
        table = [['{0} {1} values of repetitions in {2}'.format(idset1.name,
                                                                idset1.refname,
                                                                idset2.name),
                  '']]

        for rep in reps:
            if np.any(idset1 == rep):
                matches = refs[np.where(idset1 == rep)]
                myrep = rep
                for m in matches:
                    table.append([myrep, m])
                    myrep = ''

        print(tabulate(table, headers='firstrow'))
        print('\n')

Response: Print the foreign repetitions table.


Instruction:
Explain what the following Python 3 code does
def print_compare_idsets_one_ref(self, idset1_name, idset2_name):
        try:
            idset1 = self[idset1_name]
            idset2 = self[idset2_name]
        except KeyError as ke:
            log.error('Error compare_idsets: getting keys {0} and {1}'.format(idset1_name,
                                                                              idset2_name))
            import sys, pdb
            pdb.post_mortem(sys.exc_info()[2])
            raise

        assert(isinstance(idset1, idset_with_reference))
        assert(isinstance(idset2, idset))

        self._print_general_vs_table(idset1, idset2)
        self._print_foreign_repetition_table(idset1, idset2)

Response: Print the general vs foreign repetition table for two idsets.


Instruction:
Create a Python 3 function to
convert the input. sav file into other format.

Response:
def convert_sav(inputfile, outputfile=None, method='rpy2', otype='csv'):
    """ Transforms the input .sav SPSS file into other format.
    If you don't specify an outputfile, it will use the
    inputfile and change its extension to .csv
    """
    assert(os.path.isfile(inputfile))
    assert(method=='rpy2' or method=='savread')

    if method == 'rpy2':
        df = sav_to_pandas_rpy2(inputfile)
    elif method == 'savread':
        df = sav_to_pandas_savreader(inputfile)

    otype_exts = {'csv': '.csv',
                  'hdf': '.h5',
                  'stata': '.dta',
                  'json': '.json',
                  'pickle': '.pickle',
                  'excel': '.xls',
                  'html': '.html'}

    if outputfile is None:
        outputfile = inputfile.replace(path(inputfile).ext, '')

    outputfile = add_extension_if_needed(outputfile, otype_exts[otype])

    if otype == 'csv':
        df.to_csv(outputfile)
    elif otype == 'hdf':
        df.to_hdf(outputfile, os.path.basename(outputfile))
    elif otype == 'stata':
        df.to_stata(outputfile)
    elif otype == 'json':
        df.to_json(outputfile)
    elif otype == 'pickle':
        df.to_pickle(outputfile)
    elif otype == 'excel':
        df.to_excel(outputfile)
    elif otype == 'html':
        df.to_html(outputfile)
    else:
        df.to_csv(outputfile)


Instruction:
How would you explain what the following Python 3 function does
def load_mask(image, allow_empty=True):
    img    = check_img(image, make_it_3d=True)
    values = np.unique(img.get_data())

    if len(values) == 1:
        # We accept a single value if it is not 0 (full true mask).
        if values[0] == 0 and not allow_empty:
            raise ValueError('Given mask is invalid because it masks all data')

    elif len(values) == 2:
        # If there are 2 different values, one of them must be 0 (background)
        if 0 not in values:
            raise ValueError('Background of the mask must be represented with 0.'
                             ' Given mask contains: {}.'.format(values))

    elif len(values) != 2:
        # If there are more than 2 values, the mask is invalid
            raise ValueError('Given mask is not made of 2 values: {}. '
                             'Cannot interpret as true or false'.format(values))

    return nib.Nifti1Image(as_ndarray(get_img_data(img), dtype=bool), img.get_affine(), img.get_header())

Response: Load a Nifti mask volume.


Instruction:
Here you have a function in Python 3, explain what it does
def load_mask_data(image, allow_empty=True):
    mask = load_mask(image, allow_empty=allow_empty)
    return get_img_data(mask), mask.get_affine()

Response: Loads a Nifti mask volume and returns its data matrix as boolean and affine.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def union_mask(filelist):
    firstimg = check_img(filelist[0])
    mask     = np.zeros_like(firstimg.get_data())

    # create space for all features and read from subjects
    try:
        for volf in filelist:
            roiimg = check_img(volf)
            check_img_compatibility(firstimg, roiimg)
            mask  += get_img_data(roiimg)
    except Exception as exc:
        raise ValueError('Error joining mask {} and {}.'.format(repr_imgs(firstimg), repr_imgs(volf))) from exc
    else:
        return as_ndarray(mask > 0, dtype=bool)

Response: Returns a binarised mask with the union of the files in filelist.


Instruction:
How would you implement a function in Python 3 that
reads a Nifti file nii_file and a mask Nifti file and apply the mask to the nii_file.

Response:
def apply_mask(image, mask_img):
    """Read a Nifti file nii_file and a mask Nifti file.
    Returns the voxels in nii_file that are within the mask, the mask indices
    and the mask shape.

    Parameters
    ----------
    image: img-like object or boyle.nifti.NeuroImage or str
        Can either be:
        - a file path to a Nifti image
        - any object with get_data() and get_affine() methods, e.g., nibabel.Nifti1Image.
        If niimg is a string, consider it as a path to Nifti image and
        call nibabel.load on it. If it is an object, check if get_data()
        and get_affine() methods are present, raise TypeError otherwise.

    mask_img: img-like object or boyle.nifti.NeuroImage or str
        3D mask array: True where a voxel should be used.
        See img description.

    Returns
    -------
    vol[mask_indices], mask_indices

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

    Raises
    ------
    NiftiFilesNotCompatible, ValueError
    """
    img  = check_img(image)
    mask = check_img(mask_img)
    check_img_compatibility(img, mask)

    vol          = img.get_data()
    mask_data, _ = load_mask_data(mask)

    return vol[mask_data], mask_data


Instruction:
Can you tell what is the following Python 3 function doing
def apply_mask_4d(image, mask_img):  # , smooth_mm=None, remove_nans=True):
    img  = check_img(image)
    mask = check_img(mask_img)
    check_img_compatibility(img, mask, only_check_3d=True)

    vol = get_data(img)
    series, mask_data = _apply_mask_to_4d_data(vol, mask)
    return series, mask_data

Response: Read a Nifti file nii_file and a mask Nifti file and apply mask to the session series.


Instruction:
Create a Python 3 function to
transform a given vector to a volume.

Response:
def vector_to_volume(arr, mask, order='C'):
    """Transform a given vector to a volume. This is a reshape function for
    3D flattened and maybe masked vectors.

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

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

    Returns
    -------
    np.ndarray
    """
    if mask.dtype != np.bool:
        raise ValueError("mask must be a boolean array")

    if arr.ndim != 1:
        raise ValueError("vector must be a 1-dimensional array")

    if arr.ndim == 2 and any(v == 1 for v in arr.shape):
        log.debug('Got an array of shape {}, flattening for my purposes.'.format(arr.shape))
        arr = arr.flatten()

    volume = np.zeros(mask.shape[:3], dtype=arr.dtype, order=order)
    volume[mask] = arr
    return volume


Instruction:
Given the following Python 3 function, write the documentation
def matrix_to_4dvolume(arr, mask, order='C'):
    if mask.dtype != np.bool:
        raise ValueError("mask must be a boolean array")

    if arr.ndim != 2:
        raise ValueError("X must be a 2-dimensional array")

    if mask.sum() != arr.shape[0]:
        # raise an error if the shape of arr is not what expected
        raise ValueError('Expected arr of shape ({}, samples). Got {}.'.format(mask.sum(), arr.shape))

    data = np.zeros(mask.shape + (arr.shape[1],), dtype=arr.dtype,
                    order=order)
    data[mask, :] = arr
    return data

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


Instruction:
How would you explain what the following Python 3 function does
def niftilist_mask_to_array(img_filelist, mask_file=None, outdtype=None):
    img = check_img(img_filelist[0])
    if not outdtype:
        outdtype = img.dtype

    mask_data, _ = load_mask_data(mask_file)
    indices      = np.where      (mask_data)

    mask = check_img(mask_file)

    outmat = np.zeros((len(img_filelist), np.count_nonzero(mask_data)),
                      dtype=outdtype)

    for i, img_item in enumerate(img_filelist):
        img = check_img(img_item)
        if not are_compatible_imgs(img, mask):
            raise NiftiFilesNotCompatible(repr_imgs(img), repr_imgs(mask_file))

        vol = get_img_data(img)
        outmat[i, :] = vol[indices]

    return outmat, mask_data

Response: Given a list of absolute paths to nifti files creates a Numpy array containing the masked data.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def create(_):

    endpoint = client_endpoint()

    if not endpoint:
        raise CLIError("Connection endpoint not found. "
                       "Before running sfctl commands, connect to a cluster using "
                       "the 'sfctl cluster select' command.")

    no_verify = no_verify_setting()

    if security_type() == 'aad':
        auth = AdalAuthentication(no_verify)
    else:
        cert = cert_info()
        ca_cert = ca_cert_info()
        auth = ClientCertAuthentication(cert, ca_cert, no_verify)

    return ServiceFabricClientAPIs(auth, base_url=endpoint)

Response: Create a client for Service Fabric APIs.


Instruction:
Can you tell what is the following Python 3 function doing
def aggregate(self, clazz, new_col, *args):
        if is_callable(clazz) and not is_none(new_col) and has_elements(*args):
            return self.__do_aggregate(clazz, new_col, *args)

Response: Aggregate the rows of the DataFrame into a single value.


Instruction:
Here you have a function in Python 3, explain what it does
def subset(self, *args):
        cols = {}
        for k in self.colnames:
            if k in args:
                cols[str(k)] = \
                    self.__data_columns[self.colnames.index(k)].values
        return DataFrame(**cols)

Response: Returns a subset of the DataFrame with only the columns you selected


Instruction:
Can you tell what is the following Python 3 function doing
def modify(self, clazz, new_col, *args):
        if is_callable(clazz) and not is_none(new_col) and has_elements(*args):
            return self.__do_modify(clazz, new_col, *args)

Response:
Modify some columns i. e. apply a function and add the
otos to the table.


Instruction:
How would you explain what the following Python 3 function does
def aggregate(*args):

    if args and isinstance(args[0], dataframe.DataFrame):
        return args[0].aggregate(args[1], args[2], *args[3:])
    elif not args:
        raise ValueError("No arguments provided")
    else:
        return pipeable.Pipeable(pipeable.PipingMethod.AGGREGATE, *args)

Response: This method returns a dataframe object that is piped into the base object of the base object.


Instruction:
Make a summary of the following Python 3 code
def modify(*args):

    if args and isinstance(args[0], dataframe.DataFrame):
        return args[0].modify(args[1], args[2], *args[3:])
    elif not args:
        raise ValueError("No arguments provided")
    else:
        return pipeable.Pipeable(pipeable.PipingMethod.MODIFY, *args)

Response: A function that takes either a dataframe and a tuple of arguments required for modification.


Instruction:
Explain what the following Python 3 code does
def _escape_char(c, escape_char=ESCAPE_CHAR):
    buf = []
    for byte in c.encode('utf8'):
        buf.append(escape_char)
        buf.append('%X' % _ord(byte))
    return ''.join(buf)

Response: Escape a single character


Instruction:
Can you create a Python 3 function that
escapes a string so that it only contains characters in a safe set.

Response:
def escape(to_escape, safe=SAFE, escape_char=ESCAPE_CHAR, allow_collisions=False):
    """Escape a string so that it only contains characters in a safe set.

    Characters outside the safe list will be escaped with _%x_,
    where %x is the hex value of the character.

    If `allow_collisions` is True, occurrences of `escape_char`
    in the input will not be escaped.

    In this case, `unescape` cannot be used to reverse the transform
    because occurrences of the escape char in the resulting string are ambiguous.
    Only use this mode when:

    1. collisions cannot occur or do not matter, and
    2. unescape will never be called.

    .. versionadded: 1.0
        allow_collisions argument.
        Prior to 1.0, behavior was the same as allow_collisions=False (default).

    """
    if isinstance(to_escape, bytes):
        # always work on text
        to_escape = to_escape.decode('utf8')

    if not isinstance(safe, set):
        safe = set(safe)

    if allow_collisions:
        safe.add(escape_char)
    elif escape_char in safe:
        # escape char can't be in safe list
        safe.remove(escape_char)

    chars = []
    for c in to_escape:
        if c in safe:
            chars.append(c)
        else:
            chars.append(_escape_char(c, escape_char))
    return u''.join(chars)


Instruction:
Implement a Python 3 function for
determining whether this backend can send a notification to the given user and notice_type.

Response:
def can_send(self, user, notice_type):
        """
        Determines whether this backend is allowed to send a notification to
        the given user and notice_type.
        """
        from notification.models import NoticeSetting
        return NoticeSetting.for_user(user, notice_type, self.medium_id).send


Instruction:
Can you implement a function in Python 3 that
returns a dictionary with the format identifier as the key. The values are a dictionary with the format identifier as the key. The values are a string containing the rendered templates with the given context.

Response:
def get_formatted_messages(self, formats, label, context):
        """
        Returns a dictionary with the format identifier as the key. The values are
        are fully rendered templates with the given context.
        """
        format_templates = {}
        for fmt in formats:
            # conditionally turn off autoescaping for .txt extensions in format
            if fmt.endswith(".txt"):
                context.autoescape = False
            format_templates[fmt] = render_to_string((
                "notification/%s/%s" % (label, fmt),
                "notification/%s" % fmt), context_instance=context)
        return format_templates


Instruction:
How would you explain what the following Python 3 function does
def copy_attributes(source, destination, ignore_patterns=[]):
    for attr in _wildcard_filter(dir(source), *ignore_patterns):
        setattr(destination, attr, getattr(source, attr))

Response: Copy the attributes from a source object to a destination object.


Instruction:
How would you implement a function in Python 3 that
returns a DataFrameRow given its index.

Response:
def row(self, idx):
        """
        Returns DataFrameRow of the DataFrame given its index.

        :param idx: the index of the row in the DataFrame.
        :return: returns a DataFrameRow
        """
        return DataFrameRow(idx, [x[idx] for x in self], self.colnames)


Instruction:
Write a Python 3 script to
return the list of all the notice settings for each medium.

Response:
def notice_settings(request):
    """
    The notice settings view.

    Template: :template:`notification/notice_settings.html`

    Context:

        notice_types
            A list of all :model:`notification.NoticeType` objects.

        notice_settings
            A dictionary containing ``column_headers`` for each ``NOTICE_MEDIA``
            and ``rows`` containing a list of dictionaries: ``notice_type``, a
            :model:`notification.NoticeType` object and ``cells``, a list of
            tuples whose first value is suitable for use in forms and the second
            value is ``True`` or ``False`` depending on a ``request.POST``
            variable called ``form_label``, whose valid value is ``on``.
    """
    notice_types = NoticeType.objects.all()
    settings_table = []
    for notice_type in notice_types:
        settings_row = []
        for medium_id, medium_display in NOTICE_MEDIA:
            form_label = "%s_%s" % (notice_type.label, medium_id)
            setting = NoticeSetting.for_user(request.user, notice_type, medium_id)
            if request.method == "POST":
                if request.POST.get(form_label) == "on":
                    if not setting.send:
                        setting.send = True
                        setting.save()
                else:
                    if setting.send:
                        setting.send = False
                        setting.save()
            settings_row.append((form_label, setting.send))
        settings_table.append({"notice_type": notice_type, "cells": settings_row})

    if request.method == "POST":
        next_page = request.POST.get("next_page", ".")
        return HttpResponseRedirect(next_page)

    settings = {
        "column_headers": [medium_display for medium_id, medium_display in NOTICE_MEDIA],
        "rows": settings_table,
    }

    return render_to_response("notification/notice_settings.html", {
        "notice_types": notice_types,
        "notice_settings": settings,
    }, context_instance=RequestContext(request))


Instruction:
Explain what the following Python 3 code does
def query(self, input = '', params = {}):
        # Get and construct query parameters
        # Default parameters
        payload = {'input': input,
                    'appid': self.appid}
        # Additional parameters (from params), formatted for url
        for key, value in params.items():
            # Check if value is list or tuple type (needs to be comma joined)
            if isinstance(value, (list, tuple)):
                payload[key] = ','.join(value)
            else:
                payload[key] = value

        # Catch any issues with connecting to Wolfram Alpha API
        try:
            r = requests.get("http://api.wolframalpha.com/v2/query", params=payload)

            # Raise Exception (to be returned as error)
            if r.status_code != 200:
                raise Exception('Invalid response status code: %s' % (r.status_code))
            if r.encoding != 'utf-8':
                raise Exception('Invalid encoding: %s' % (r.encoding))

        except Exception, e:
            return Result(error = e)

        return Result(xml = r.text)

Response: Query Wolfram Alpha and return a Result object


Instruction:
Given the following Python 3 function, write the documentation
def pods(self):
        # Return empty list if xml_tree is not defined (error Result object)
        if not self.xml_tree:
            return []

        # Create a Pod object for every pod group in xml
        return [Pod(elem) for elem in self.xml_tree.findall('pod')]

Response: Return list of all Pod objects in result


Instruction:
Can you write a function in Python 3 where it
returns a dictionary of available formats corresponding to a list of the values AttributeNames

Response:
def format(self):
        """
        Dictionary of available formats, corresponding to a list of the values
        Example: pod.format['plaintext'] will return a list of every plaintext
                 content in the pod's subpods
        """
        formats = {}

        # Iterate through all the tags (formats) in subpods
        # 'state' is a tag but not an acceptable format
        for subpod in self.root.findall('subpod'):

            # elem will be a specific format
            for elem in list(subpod):

                # skip any subpod state xml groups (not a format)
                if elem.tag == 'state':
                    continue

                # Content of elem (specific format)
                content = elem.text

                # img needs special content packaging
                if elem.tag == 'img':
                    content = {'url': elem.get('src'),
                               'alt': elem.get('alt'),
                               'title': elem.get('title'),
                               'width': int(elem.get('width', 0)),
                               'height': int(elem.get('height', 0))}

                # Create / append to return dict
                if elem.tag not in formats:
                    formats[elem.tag] = [content]
                else:
                    formats[elem.tag].append(content)

        return formats


Instruction:
Write a Python 3 function that can
find a node in the tree.

Response:
def find(self, *args):
        """
        Find a node in the tree. If the node is not found it is added first and then returned.

        :param args: a tuple
        :return: returns the node
        """
        curr_node = self.__root
        return self.__traverse(curr_node, 0,  *args)


Instruction:
Can you tell what is the following Python 3 function doing
def get_notification_language(user):
    if getattr(settings, "NOTIFICATION_LANGUAGE_MODULE", False):
        try:
            app_label, model_name = settings.NOTIFICATION_LANGUAGE_MODULE.split(".")
            model = models.get_model(app_label, model_name)
            # pylint: disable-msg=W0212
            language_model = model._default_manager.get(user__id__exact=user.id)
            if hasattr(language_model, "language"):
                return language_model.language
        except (ImportError, ImproperlyConfigured, model.DoesNotExist):
            raise LanguageStoreNotAvailable
    raise LanguageStoreNotAvailable

Response:
Returns the language of the user s notification. Raises LanguageStoreNotAvailable if this site does not use translated
    notifications.


Instruction:
How would you explain what the following Python 3 function does
def send_now(users, label, extra_context=None, sender=None):
    sent = False
    if extra_context is None:
        extra_context = {}

    notice_type = NoticeType.objects.get(label=label)

    current_language = get_language()

    for user in users:
        # get user language for user from language store defined in
        # NOTIFICATION_LANGUAGE_MODULE setting
        try:
            language = get_notification_language(user)
        except LanguageStoreNotAvailable:
            language = None

        if language is not None:
            # activate the user's language
            activate(language)

        for backend in NOTIFICATION_BACKENDS.values():
            if backend.can_send(user, notice_type):
                backend.deliver(user, sender, notice_type, extra_context)
                sent = True

    # reset environment to original language
    activate(current_language)
    return sent

Response: Sends a new notice for the given list of users.


Instruction:
Can you implement a function in Python 3 that
queues the user notifications in NoticeQueueBatch.

Response:
def queue(users, label, extra_context=None, sender=None):
    """
    Queue the notification in NoticeQueueBatch. This allows for large amounts
    of user notifications to be deferred to a seperate process running outside
    the webserver.
    """
    if extra_context is None:
        extra_context = {}
    if isinstance(users, QuerySet):
        users = [row["pk"] for row in users.values("pk")]
    else:
        users = [user.pk for user in users]
    notices = []
    for user in users:
        notices.append((user, label, extra_context, sender))
    NoticeQueueBatch(pickled_data=base64.b64encode(pickle.dumps(notices))).save()


Instruction:
Can you create a Python 3 function that
writes tersoff potential file from dictionary of parameters to string

Response:
def write_tersoff_potential(parameters):
    """Write tersoff potential file from parameters to string

    Parameters
    ----------
    parameters: dict
       keys are tuple of elements with the values being the parameters length 14
    """
    lines = []
    for (e1, e2, e3), params in parameters.items():
        if len(params) != 14:
            raise ValueError('tersoff three body potential expects 14 parameters')
        lines.append(' '.join([e1, e2, e3] + ['{:16.8g}'.format(_) for _ in params]))
    return '\n'.join(lines)


Instruction:
Make a summary of the following Python 3 code
def subset(self, *args):
        args = list(args)
        args.extend([x for x in
                     self.__grouping.grouping_colnames if x not in args])
        return GroupedDataFrame(self.__grouping.ungroup().subset(*args),
                                *self.__grouping.grouping_colnames)

Response: Subset only some of the columns of the DataFrame.


Instruction:
Can you create a Python 3 function that
modifies some columns of a class that extends class Callable and add the result to the table.

Response:
def modify(self, clazz, new_col, *args):
        """
        Modify some columns (i.e. apply a function)
         and add the result to the table.

        :param clazz: name of a class that extends class Callable
        :type clazz: class
        :param new_col: name of the new column
        :type new_col: str
        :param args: list of column names of the object that
         function should be applied to
        :type args: tuple
        :return: returns a new GroupedDataFrame object with the modified
          values, i.e. the new column of values
        :rtype: GroupedDataFrame
        """
        if is_callable(clazz) \
                and not is_none(new_col) \
                and has_elements(*args) \
                and is_disjoint(self.__grouping.grouping_colnames,
                                args,
                                __DISJOINT_SETS_ERROR__):
            return self.__do_modify(clazz, new_col, *args)


Instruction:
Write a Python 3 script for
aggregating the rows of each group into a single value.

Response:
def aggregate(self, clazz, new_col, *args):
        """
        Aggregate the rows of each group into a single value.

        :param clazz: name of a class that extends class Callable
        :type clazz: class
        :param new_col: name of the new column
        :type new_col: str
        :param args: list of column names of the object that
         function should be applied to
        :type args: varargs
        :return: returns a new dataframe object with the aggregated value
        :rtype: DataFrame
        """
        if is_callable(clazz) \
                and not is_none(new_col) \
                and has_elements(*args) \
                and is_disjoint(self.__grouping.grouping_colnames,
                                args,
                                __DISJOINT_SETS_ERROR__):
            return self.__do_aggregate(clazz, new_col, *args)


Instruction:
Write a Python 3 function that can
make LTSV Reader for reading selected labels.

Response:
def reader(ltsvfile, labels=None):
    """Make LTSV Reader for reading selected labels.

    :param  ltsvfile: iterable of lines.
    :param  labels: sequence of labels. (optional)
    :return: generator of record in [[label, value], ...] form.
    """
    label_pattern = re.compile(r"^[0-9A-Za-z_.-]+:")

    if labels is not None:
        prefixes = tuple(L + ':' for L in labels
                if label_pattern.match(L + ':'))
        for record in ltsvfile:
            record = record.rstrip('\r\n')
            yield [x.split(':', 1) for x in record.split('\t')
                    if x.startswith(prefixes)]
        return

    for record in ltsvfile:
        record = record.rstrip('\r\n')
        yield [x.split(':', 1) for x in record.split('\t')
                if label_pattern.match(x)]


Instruction:
Create a Python 3 function for
making LTSV Reader for reading selected labels.

Response:
def DictReader(ltsvfile, labels=None, dict_type=dict):
    """Make LTSV Reader for reading selected labels.

    :param  ltsvfile: iterable of lines.
    :param  labels: sequence of labels.
    :return: generator of record in {label: value, ...} form.
    """
    for rec in reader(ltsvfile, labels):
        yield dict_type(rec)


Instruction:
Create a Python 3 function for
checking if elements of set2 are not in set1.

Response:
def is_disjoint(set1, set2, warn):
    """
    Checks if elements of set2 are in set1.

    :param set1: a set of values
    :param set2: a set of values
    :param warn: the error message that should be thrown
     when the sets are NOT disjoint
    :return: returns true no elements of set2 are in set1
    """
    for elem in set2:
        if elem in set1:
            raise ValueError(warn)
    return True


Instruction:
Write a Python 3 function for
checking if all elements from set2 are in set1.

Response:
def contains_all(set1, set2, warn):
    """
    Checks if all elements from set2 are in set1.

    :param set1:  a set of values
    :param set2:  a set of values
    :param warn: the error message that should be thrown
     when the sets are not containd
    :return: returns true if all values of set2 are in set1
    """
    for elem in set2:
        if elem not in set1:
            raise ValueError(warn)
    return True


Instruction:
Given the following Python 3 function, write the documentation
def to_XML(self):
        marcxml_template = """<record xmlns="http://www.loc.gov/MARC21/slim/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.loc.gov/MARC21/slim
http://www.loc.gov/standards/marcxml/schema/MARC21slim.xsd">
$LEADER
$CONTROL_FIELDS
$DATA_FIELDS
</record>
"""

        oai_template = """<record>
<metadata>
<oai_marc>
$LEADER$CONTROL_FIELDS
$DATA_FIELDS
</oai_marc>
</metadata>
</record>
"""

        # serialize leader, if it is present and record is marc xml
        leader = self.leader if self.leader is not None else ""
        if leader:  # print only visible leaders
            leader = "<leader>" + leader + "</leader>"

        # discard leader for oai
        if self.oai_marc:
            leader = ""

        # serialize
        xml_template = oai_template if self.oai_marc else marcxml_template
        xml_output = Template(xml_template).substitute(
            LEADER=leader.strip(),
            CONTROL_FIELDS=self._serialize_ctl_fields().strip(),
            DATA_FIELDS=self._serialize_data_fields().strip()
        )

        return xml_output

Response: Serialize object back to XML string.


Instruction:
How would you explain what the following Python 3 function does
def _parse_string(self, xml):
        if not isinstance(xml, HTMLElement):
            xml = dhtmlparser.parseString(str(xml))

        # check if there are any records
        record = xml.find("record")
        if not record:
            raise ValueError("There is no <record> in your MARC XML document!")
        record = record[0]

        self.oai_marc = len(record.find("oai_marc")) > 0

        # leader is separate only in marc21
        if not self.oai_marc:
            leader = record.find("leader")
            if len(leader) >= 1:
                self.leader = leader[0].getContent()

        # parse body in respect of OAI MARC format possibility
        if self.oai_marc:
            self._parse_control_fields(record.find("fixfield"), "id")
            self._parse_data_fields(record.find("varfield"), "id", "label")
        else:
            self._parse_control_fields(record.find("controlfield"), "tag")
            self._parse_data_fields(record.find("datafield"), "tag", "code")

        # for backward compatibility of MARC XML with OAI
        if self.oai_marc and "LDR" in self.controlfields:
            self.leader = self.controlfields["LDR"]

Response:
Parse MARC XML document to dicts which are contained in
        self. controlfields and self. datafields.


Instruction:
Create a Python 3 function for
parsing the control fields.

Response:
def _parse_control_fields(self, fields, tag_id="tag"):
        """
        Parse control fields.

        Args:
            fields (list): list of HTMLElements
            tag_id (str):  parameter name, which holds the information, about
                           field name this is normally "tag", but in case of
                           oai_marc "id".
        """
        for field in fields:
            params = field.params

            # skip tags without parameters
            if tag_id not in params:
                continue

            self.controlfields[params[tag_id]] = field.getContent().strip()


Instruction:
Implement a Python 3 function for
parsing data fields. Args: fields (list): of HTMLElements tag_id (str): parameter name, which holds the information, about field name this is normally "tag", but in case of oai_marc "id" sub_id (str): id of parameter, which holds informations about subfield name this is normally "code" but in case of oai_marc "label"

Response:
def _parse_data_fields(self, fields, tag_id="tag", sub_id="code"):
        """
        Parse data fields.

        Args:
            fields (list): of HTMLElements
            tag_id (str): parameter name, which holds the information, about
                          field name this is normally "tag", but in case of
                          oai_marc "id"
            sub_id (str): id of parameter, which holds informations about
                          subfield name this is normally "code" but in case of
                          oai_marc "label"

        """
        for field in fields:
            params = field.params

            if tag_id not in params:
                continue

            # take care of iX/indX (indicator) parameters
            field_repr = OrderedDict([
                [self.i1_name, params.get(self.i1_name, " ")],
                [self.i2_name, params.get(self.i2_name, " ")],
            ])

            # process all subfields
            for subfield in field.find("subfield"):
                if sub_id not in subfield.params:
                    continue

                content = MARCSubrecord(
                    val=subfield.getContent().strip(),
                    i1=field_repr[self.i1_name],
                    i2=field_repr[self.i2_name],
                    other_subfields=field_repr
                )

                # add or append content to list of other contents
                code = subfield.params[sub_id]
                if code in field_repr:
                    field_repr[code].append(content)
                else:
                    field_repr[code] = [content]

            tag = params[tag_id]
            if tag in self.datafields:
                self.datafields[tag].append(field_repr)
            else:
                self.datafields[tag] = [field_repr]


Instruction:
Create a Python 3 function to
add new control field value with under name into control field dictionary.

Response:
def add_ctl_field(self, name, value):
        """
        Add new control field `value` with under `name` into control field
        dictionary :attr:`controlfields`.
        """
        if len(name) != 3:
            raise ValueError("name parameter have to be exactly 3 chars long!")

        self.controlfields[name] = value


Instruction:
Can you tell what is the following Python 3 function doing
def add_data_field(self, name, i1, i2, subfields_dict):
        if i1 not in self.valid_i_chars:
            raise ValueError("Invalid i1 parameter '" + i1 + "'!")
        if i2 not in self.valid_i_chars:
            raise ValueError("Invalid i2 parameter '" + i2 + "'!")

        if len(name) != 3:
            raise ValueError(
                "`name` parameter have to be exactly 3 chars long!"
            )
        if not subfields_dict:
            raise ValueError(
                "`subfields_dict` have to contain something!"
            )
        if not isinstance(subfields_dict, dict):
            raise ValueError(
                "`subfields_dict` parameter has to be dict instance!"
            )

        # check local keys, convert strings to MARCSubrecord instances
        subrecords = []
        for key, val in subfields_dict.items():
            if len(key) > 1:
                raise KeyError(
                    "`subfields_dict` can be only one character long!"
                )

            # convert other values to lists
            if not isinstance(val, list):
                val = [val]

            subfields = map(
                lambda x: MARCSubrecord(x, i1, i2, None),
                val
            )

            subfields_dict[key] = subfields
            subrecords.extend(subfields)

        # save i/ind values
        subfields_dict[self.i1_name] = i1
        subfields_dict[self.i2_name] = i2

        # append dict, or add new dict into self.datafields
        if name in self.datafields:
            self.datafields[name].append(subfields_dict)
        else:
            self.datafields[name] = [subfields_dict]

        # to each subrecord add reference to list of all subfields in this
        # datafield
        other_subfields = self.datafields[name]
        for record in subrecords:
            record.other_subfields = other_subfields

Response: Add new datafield into the MARC record.


Instruction:
How would you explain what the following Python 3 function does
def get_ctl_field(self, controlfield, alt=None):
        if not alt:
            return self.controlfields[controlfield]

        return self.controlfields.get(controlfield, alt)

Response: Returns the value of a given controlfield.


Instruction:
Write a Python 3 script for
getting the data records for a specific data field and subfield.

Response:
def getDataRecords(self, datafield, subfield, throw_exceptions=True):
        """
        .. deprecated::
            Use :func:`get_subfields` instead.
        """
        return self.get_subfields(
            datafield=datafield,
            subfield=subfield,
            exception=throw_exceptions
        )


Instruction:
How would you code a function in Python 3 to
return content of given subfield in datafield.

Response:
def get_subfields(self, datafield, subfield, i1=None, i2=None,
                      exception=False):
        """
        Return content of given `subfield` in `datafield`.

        Args:
            datafield (str): Section name (for example "001", "100", "700").
            subfield (str):  Subfield name (for example "a", "1", etc..).
            i1 (str, default None): Optional i1/ind1 parameter value, which
               will be used for search.
            i2 (str, default None): Optional i2/ind2 parameter value, which
               will be used for search.
            exception (bool): If ``True``, :exc:`~exceptions.KeyError` is
                      raised when method couldn't found given `datafield` /
                      `subfield`. If ``False``, blank array ``[]`` is returned.

        Returns:
            list: of :class:`.MARCSubrecord`.

        Raises:
            KeyError: If the subfield or datafield couldn't be found.

        Note:
            MARCSubrecord is practically same thing as string, but has defined
            :meth:`.MARCSubrecord.i1` and :attr:`.MARCSubrecord.i2`
            methods.

            You may need to be able to get this, because MARC XML depends on
            i/ind parameters from time to time (names of authors for example).

        """
        if len(datafield) != 3:
            raise ValueError(
                "`datafield` parameter have to be exactly 3 chars long!"
            )
        if len(subfield) != 1:
            raise ValueError(
                "Bad subfield specification - subfield have to be 1 char long!"
            )

        # if datafield not found, return or raise exception
        if datafield not in self.datafields:
            if exception:
                raise KeyError(datafield + " is not in datafields!")

            return []

        # look for subfield defined by `subfield`, `i1` and `i2` parameters
        output = []
        for datafield in self.datafields[datafield]:
            if subfield not in datafield:
                continue

            # records are not returned just like plain string, but like
            # MARCSubrecord, because you will need ind1/ind2 values
            for sfield in datafield[subfield]:
                if i1 and sfield.i1 != i1:
                    continue

                if i2 and sfield.i2 != i2:
                    continue

                output.append(sfield)

        if not output and exception:
            raise KeyError(subfield + " couldn't be found in subfields!")

        return output


Instruction:
Make a summary of the following Python 3 code
def timeit(unit='s'):
    def wrapper(func):
        @wraps(func)
        def inner(*args, **kwargs):
            start = time.time()
            _result = func(*args, **kwargs)
            _format(unit, time.time() - start, func.__name__ + '()')
            return _result
        return inner
    return wrapper

Response:
测试函数耗时

    :param unit: 时间单位，有 's','m','h' 可选（seconds，minutes，hours）


Instruction:
How would you code a function in Python 3 to
print the stats to stdout.

Response:
def _print(stats, limit, label):
    """
    控制输出量
    """
    print("TraceMalloc for {}".format(label))
    for index, stat in enumerate(stats):
        if index < limit:
            print(stat)
        else:
            break


Instruction:
Given the following Python 3 function, write the documentation
def memoryit_block(group_by='lineno', limit=10, label='code block'):
    tracemalloc.start()
    _start = tracemalloc.take_snapshot()
    try:
        yield
    finally:
        _end = tracemalloc.take_snapshot()
        stats = _end.compare_to(_start, group_by)
        _print(stats, limit, label)

Response: memoryit_block - 内存消耗情况 可选 memoryit_block 可选 memoryit_block 可选 memoryit_block 可选 memoryit_block 可选 memoryit_block 可选 memoryit_block 可选 memoryit_block


Instruction:
Explain what the following Python 3 code does
def _get_params(target, param, dof):
    '''Get the given param from each of the DOFs for a joint.'''
    return [target.getParam(getattr(ode, 'Param{}{}'.format(param, s)))
            for s in ['', '2', '3'][:dof]]

Response: Get the given param from each of the DOFs for a joint.


Instruction:
Here you have a function in Python 3, explain what it does
def _set_params(target, param, values, dof):
    '''Set the given param for each of the DOFs for a joint.'''
    if not isinstance(values, (list, tuple, np.ndarray)):
        values = [values] * dof
    assert dof == len(values)
    for s, value in zip(['', '2', '3'][:dof], values):
        target.setParam(getattr(ode, 'Param{}{}'.format(param, s)), value)

Response: Set the given param for each of the DOFs for a joint.


Instruction:
Can you create a Python 3 function that
gives an angle and an axis create a quaternion.

Response:
def make_quaternion(theta, *axis):
    '''Given an angle and an axis, create a quaternion.'''
    x, y, z = axis
    r = np.sqrt(x * x + y * y + z * z)
    st = np.sin(theta / 2.)
    ct = np.cos(theta / 2.)
    return [x * st / r, y * st / r, z * st / r, ct]


Instruction:
Create a Python 3 function to
give a set of bodies compute their center of mass in world coordinates.

Response:
def center_of_mass(bodies):
    '''Given a set of bodies, compute their center of mass in world coordinates.
    '''
    x = np.zeros(3.)
    t = 0.
    for b in bodies:
        m = b.mass
        x += b.body_to_world(m.c) * m.mass
        t += m.mass
    return x / t


Instruction:
Here you have a function in Python 3, explain what it does
def state(self):
        '''The state of this body includes:

            - name of the body (str)
            - position (3-tuple)
            - quaternion (4-tuple)
            - linear velocity (3-tuple)
            - angular velocity (3-tuple)
        '''
        return BodyState(self.name,
                         tuple(self.position),
                         tuple(self.quaternion),
                         tuple(self.linear_velocity),
                         tuple(self.angular_velocity))

Response: Returns a BodyState object containing the current state of the object.


Instruction:
Create a Python 3 function to
set the state of this body.

Response:
def state(self, state):
        '''Set the state of this body.

        Parameters
        ----------
        state : BodyState tuple
            The desired state of the body.
        '''
        assert self.name == state.name, \
            'state name "{}" != body name "{}"'.format(state.name, self.name)
        self.position = state.position
        self.quaternion = state.quaternion
        self.linear_velocity = state.linear_velocity
        self.angular_velocity = state.angular_velocity


Instruction:
Write a Python 3 script to
set the rotation of this object using a rotation matrix.

Response:
def rotation(self, rotation):
        '''Set the rotation of this body using a rotation matrix.

        Parameters
        ----------
        rotation : sequence of 9 floats
            The desired rotation matrix for this body.
        '''
        if isinstance(rotation, np.ndarray):
            rotation = rotation.ravel()
        self.ode_body.setRotation(tuple(rotation))


Instruction:
How would you implement a function in Python 3 that
sets the kinematic or dynamic attributes for this body.

Response:
def is_kinematic(self, is_kinematic):
        '''Set the kinematic/dynamic attribute for this body.

        In pagoda, kinematic bodies have infinite mass and do interact with
        other bodies via collisions.

        Parameters
        ----------
        is_kinematic : bool
            If True, this body will be set to kinematic. If False, it will be
            set to dynamic.
        '''
        if is_kinematic:
            self.ode_body.setKinematic()
        else:
            self.ode_body.setDynamic()


Instruction:
Can you write a function in Python 3 where it
converts a body - relative offset to world coordinates.

Response:
def body_to_world(self, position):
        '''Convert a body-relative offset to world coordinates.

        Parameters
        ----------
        position : 3-tuple of float
            A tuple giving body-relative offsets.

        Returns
        -------
        position : 3-tuple of float
            A tuple giving the world coordinates of the given offset.
        '''
        return np.array(self.ode_body.getRelPointPos(tuple(position)))


Instruction:
How would you explain what the following Python 3 function does
def world_to_body(self, position):
        '''Convert a point in world coordinates to a body-relative offset.

        Parameters
        ----------
        position : 3-tuple of float
            A world coordinates position.

        Returns
        -------
        offset : 3-tuple of float
            A tuple giving the body-relative offset of the given position.
        '''
        return np.array(self.ode_body.getPosRelPoint(tuple(position)))

Response: Convert a point in world coordinates to a body - relative offset.


Instruction:
How would you explain what the following Python 3 function does
def relative_offset_to_world(self, offset):
        '''Convert a relative body offset to world coordinates.

        Parameters
        ----------
        offset : 3-tuple of float
            The offset of the desired point, given as a relative fraction of the
            size of this body. For example, offset (0, 0, 0) is the center of
            the body, while (0.5, -0.2, 0.1) describes a point halfway from the
            center towards the maximum x-extent of the body, 20% of the way from
            the center towards the minimum y-extent, and 10% of the way from the
            center towards the maximum z-extent.

        Returns
        -------
        position : 3-tuple of float
            A position in world coordinates of the given body offset.
        '''
        return np.array(self.body_to_world(offset * self.dimensions / 2))

Response: Convert a relative body offset to world coordinates.


Instruction:
Write a Python 3 function that can
add a force to the current object.

Response:
def add_force(self, force, relative=False, position=None, relative_position=None):
        '''Add a force to this body.

        Parameters
        ----------
        force : 3-tuple of float
            A vector giving the forces along each world or body coordinate axis.
        relative : bool, optional
            If False, the force values are assumed to be given in the world
            coordinate frame. If True, they are assumed to be given in the
            body-relative coordinate frame. Defaults to False.
        position : 3-tuple of float, optional
            If given, apply the force at this location in world coordinates.
            Defaults to the current position of the body.
        relative_position : 3-tuple of float, optional
            If given, apply the force at this relative location on the body. If
            given, this method ignores the ``position`` parameter.
        '''
        b = self.ode_body
        if relative_position is not None:
            op = b.addRelForceAtRelPos if relative else b.addForceAtRelPos
            op(force, relative_position)
        elif position is not None:
            op = b.addRelForceAtPos if relative else b.addForceAtPos
            op(force, position)
        else:
            op = b.addRelForce if relative else b.addForce
            op(force)


Instruction:
Write a Python 3 function that can
add a torque to this body.

Response:
def add_torque(self, torque, relative=False):
        '''Add a torque to this body.

        Parameters
        ----------
        force : 3-tuple of float
            A vector giving the torque along each world or body coordinate axis.
        relative : bool, optional
            If False, the torque values are assumed to be given in the world
            coordinate frame. If True, they are assumed to be given in the
            body-relative coordinate frame. Defaults to False.
        '''
        op = self.ode_body.addRelTorque if relative else self.ode_body.addTorque
        op(torque)


Instruction:
Create a Python 3 function for
connecting this body to another one using a joint.

Response:
def join_to(self, joint, other_body=None, **kwargs):
        '''Connect this body to another one using a joint.

        This method creates a joint to fasten this body to the other one. See
        :func:`World.join`.

        Parameters
        ----------
        joint : str
            The type of joint to use when connecting these bodies.
        other_body : :class:`Body` or str, optional
            The other body to join with this one. If not given, connects this
            body to the world.
        '''
        self.world.join(joint, self, other_body, **kwargs)


Instruction:
Implement a Python 3 function for
connecting two sets of bodies to this one and join them together.

Response:
def connect_to(self, joint, other_body, offset=(0, 0, 0), other_offset=(0, 0, 0),
                   **kwargs):
        '''Move another body next to this one and join them together.

        This method will move the ``other_body`` so that the anchor points for
        the joint coincide. It then creates a joint to fasten the two bodies
        together. See :func:`World.move_next_to` and :func:`World.join`.

        Parameters
        ----------
        joint : str
            The type of joint to use when connecting these bodies.
        other_body : :class:`Body` or str
            The other body to join with this one.
        offset : 3-tuple of float, optional
            The body-relative offset where the anchor for the joint should be
            placed. Defaults to (0, 0, 0). See :func:`World.move_next_to` for a
            description of how offsets are specified.
        other_offset : 3-tuple of float, optional
            The offset on the second body where the joint anchor should be
            placed. Defaults to (0, 0, 0). Like ``offset``, this is given as an
            offset relative to the size and shape of ``other_body``.
        '''
        anchor = self.world.move_next_to(self, other_body, offset, other_offset)
        self.world.join(joint, self, other_body, anchor=anchor, **kwargs)


Instruction:
Given the following Python 3 function, write the documentation
def positions(self):
        '''List of positions for linear degrees of freedom.'''
        return [self.ode_obj.getPosition(i) for i in range(self.LDOF)]

Response: Returns a list of positions for linear degrees of freedom.


Instruction:
Implement a Python 3 function for
listing of position rates for linear degrees of freedom.

Response:
def position_rates(self):
        '''List of position rates for linear degrees of freedom.'''
        return [self.ode_obj.getPositionRate(i) for i in range(self.LDOF)]


Instruction:
Can you generate the documentation for the following Python 3 function
def angles(self):
        '''List of angles for rotational degrees of freedom.'''
        return [self.ode_obj.getAngle(i) for i in range(self.ADOF)]

Response: List of angles for rotational degrees of freedom.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def angle_rates(self):
        '''List of angle rates for rotational degrees of freedom.'''
        return [self.ode_obj.getAngleRate(i) for i in range(self.ADOF)]

Response: List of angle rates for rotational degrees of freedom.


Instruction:
Write a Python 3 function that can
list of axes for this object s degrees of freedom.

Response:
def axes(self):
        '''List of axes for this object's degrees of freedom.'''
        return [np.array(self.ode_obj.getAxis(i))
                for i in range(self.ADOF or self.LDOF)]


Instruction:
Can you create a Python 3 function that
sets the axes for this object.

Response:
def axes(self, axes):
        '''Set the axes for this object's degrees of freedom.

        Parameters
        ----------
        axes : list of axes specifications
            A list of axis values to set. This list must have the same number of
            elements as the degrees of freedom of the underlying ODE object.
            Each element can be

            (a) None, which has no effect on the corresponding axis, or
            (b) three floats specifying the axis to set.
        '''
        assert self.ADOF == len(axes) or self.LDOF == len(axes)
        for i, axis in enumerate(axes):
            if axis is not None:
                self.ode_obj.setAxis(i, 0, axis)


Instruction:
How would you explain what the following Python 3 function does
def lo_stops(self, lo_stops):
        '''Set the lo stop values for this object's degrees of freedom.

        Parameters
        ----------
        lo_stops : float or sequence of float
            A lo stop value to set on all degrees of freedom, or a list
            containing one such value for each degree of freedom. For rotational
            degrees of freedom, these values must be in radians.
        '''
        _set_params(self.ode_obj, 'LoStop', lo_stops, self.ADOF + self.LDOF)

Response: Set the lo stop values for this object s degrees of freedom.


Instruction:
Make a summary of the following Python 3 code
def hi_stops(self, hi_stops):
        '''Set the hi stop values for this object's degrees of freedom.

        Parameters
        ----------
        hi_stops : float or sequence of float
            A hi stop value to set on all degrees of freedom, or a list
            containing one such value for each degree of freedom. For rotational
            degrees of freedom, these values must be in radians.
        '''
        _set_params(self.ode_obj, 'HiStop', hi_stops, self.ADOF + self.LDOF)

Response: Set the hi stop values for this object s degrees of freedom.