Instruction:
Write a Python 3 script to
retrieve the number of milliseconds during which the virtual machine has used the CPU. This value is a subset of elapsedMs.

Response:
def GetCpuUsedMs(self):
        '''Retrieves the number of milliseconds during which the virtual machine
           has used the CPU. This value includes the time used by the guest
           operating system and the time used by virtualization code for tasks for this
           virtual machine. You can combine this value with the elapsed time
           (VMGuestLib_GetElapsedMs) to estimate the effective virtual machine
           CPU speed. This value is a subset of elapsedMs.'''
        counter = c_uint64()
        ret = vmGuestLib.VMGuestLib_GetCpuUsedMs(self.handle.value, byref(counter))
        if ret != VMGUESTLIB_ERROR_SUCCESS: raise VMGuestLibException(ret)
        return counter.value


Instruction:
Given the following Python 3 function, write the documentation
def GetElapsedMs(self):
        '''Retrieves the number of milliseconds that have passed in the virtual
           machine since it last started running on the server. The count of elapsed
           time restarts each time the virtual machine is powered on, resumed, or
           migrated using VMotion. This value counts milliseconds, regardless of
           whether the virtual machine is using processing power during that time.

           You can combine this value with the CPU time used by the virtual machine
           (VMGuestLib_GetCpuUsedMs) to estimate the effective virtual machine
           CPU speed. cpuUsedMs is a subset of this value.'''
        counter = c_uint64()
        ret = vmGuestLib.VMGuestLib_GetElapsedMs(self.handle.value, byref(counter))
        if ret != VMGUESTLIB_ERROR_SUCCESS: raise VMGuestLibException(ret)
        return counter.value

Response: Retrieves the number of milliseconds that have passed in the virtual machine since it last started running on the server.


Instruction:
Make a summary of the following Python 3 code
def GetHostCpuUsedMs(self):
        '''Undocumented.'''
        counter = c_uint64()
        ret = vmGuestLib.VMGuestLib_GetHostCpuUsedMs(self.handle.value, byref(counter))
        if ret != VMGUESTLIB_ERROR_SUCCESS: raise VMGuestLibException(ret)
        return counter.value

Response: Retrieves the amount of CPU usage of the host process.


Instruction:
Write a Python 3 script to
retrieve the size of memory that is reserved for the host memory.

Response:
def GetHostMemKernOvhdMB(self):
        '''Undocumented.'''
        counter = c_uint()
        ret = vmGuestLib.VMGuestLib_GetHostMemKernOvhdMB(self.handle.value, byref(counter))
        if ret != VMGUESTLIB_ERROR_SUCCESS: raise VMGuestLibException(ret)
        return counter.value


Instruction:
Can you generate a brief explanation for the following Python 3 code
def GetHostMemMappedMB(self):
        '''Undocumented.'''
        counter = c_uint()
        ret = vmGuestLib.VMGuestLib_GetHostMemMappedMB(self.handle.value, byref(counter))
        if ret != VMGUESTLIB_ERROR_SUCCESS: raise VMGuestLibException(ret)
        return counter.value

Response: Retrieves the amount of memory that is mapped to the host memory.


Instruction:
Write a Python 3 function that can
retrieve the number of physical memory allocated to the host memory.

Response:
def GetHostMemPhysFreeMB(self):
        '''Undocumented.'''
        counter = c_uint()
        ret = vmGuestLib.VMGuestLib_GetHostMemPhysFreeMB(self.handle.value, byref(counter))
        if ret != VMGUESTLIB_ERROR_SUCCESS: raise VMGuestLibException(ret)
        return counter.value


Instruction:
Here you have a function in Python 3, explain what it does
def GetHostMemPhysMB(self):
        '''Undocumented.'''
        counter = c_uint()
        ret = vmGuestLib.VMGuestLib_GetHostMemPhysMB(self.handle.value, byref(counter))
        if ret != VMGUESTLIB_ERROR_SUCCESS: raise VMGuestLibException(ret)
        return counter.value

Response: Retrieves the physical memory size of the host memory.


Instruction:
Can you generate the documentation for the following Python 3 function
def GetHostMemSharedMB(self):
        '''Undocumented.'''
        counter = c_uint()
        ret = vmGuestLib.VMGuestLib_GetHostMemSharedMB(self.handle.value, byref(counter))
        if ret != VMGUESTLIB_ERROR_SUCCESS: raise VMGuestLibException(ret)
        return counter.value

Response: Retrieves the amount of memory that is shared by the host.


Instruction:
Implement a function in Python 3 to
retrieve the amount of memory that has been swapped for the host memory.

Response:
def GetHostMemSwappedMB(self):
        '''Undocumented.'''
        counter = c_uint()
        ret = vmGuestLib.VMGuestLib_GetHostMemSwappedMB(self.handle.value, byref(counter))
        if ret != VMGUESTLIB_ERROR_SUCCESS: raise VMGuestLibException(ret)
        return counter.value


Instruction:
Can you tell what is the following Python 3 function doing
def GetHostMemUnmappedMB(self):
        '''Undocumented.'''
        counter = c_uint()
        ret = vmGuestLib.VMGuestLib_GetHostMemUnmappedMB(self.handle.value, byref(counter))
        if ret != VMGUESTLIB_ERROR_SUCCESS: raise VMGuestLibException(ret)
        return counter.value

Response: Retrieves the amount of memory that is not mapped to the host memory.


Instruction:
Explain what the following Python 3 code does
def GetHostMemUsedMB(self):
        '''Undocumented.'''
        counter = c_uint()
        ret = vmGuestLib.VMGuestLib_GetHostMemUsedMB(self.handle.value, byref(counter))
        if ret != VMGUESTLIB_ERROR_SUCCESS: raise VMGuestLibException(ret)
        return counter.value

Response: Retrieves the amount of memory that is used by the host process.


Instruction:
Write a Python 3 function for
retrieving the number of CPU cores that the host is using.

Response:
def GetHostNumCpuCores(self):
        '''Undocumented.'''
        counter = c_uint()
        ret = vmGuestLib.VMGuestLib_GetHostNumCpuCores(self.handle.value, byref(counter))
        if ret != VMGUESTLIB_ERROR_SUCCESS: raise VMGuestLibException(ret)
        return counter.value


Instruction:
Here you have a function in Python 3, explain what it does
def GetHostProcessorSpeed(self):
        '''Retrieves the speed of the ESX system's physical CPU in MHz.'''
        counter = c_uint()
        ret = vmGuestLib.VMGuestLib_GetHostProcessorSpeed(self.handle.value, byref(counter))
        if ret != VMGUESTLIB_ERROR_SUCCESS: raise VMGuestLibException(ret)
        return counter.value

Response: Retrieves the speed of the ESX system s physical CPU in MHz.


Instruction:
How would you code a function in Python 3 to
retrieve the amount of memory the virtual machine is actively using its estimated working set size.

Response:
def GetMemActiveMB(self):
        '''Retrieves the amount of memory the virtual machine is actively using its
           estimated working set size.'''
        counter = c_uint()
        ret = vmGuestLib.VMGuestLib_GetMemActiveMB(self.handle.value, byref(counter))
        if ret != VMGUESTLIB_ERROR_SUCCESS: raise VMGuestLibException(ret)
        return counter.value


Instruction:
Can you tell what is the following Python 3 function doing
def GetMemBalloonedMB(self):
        '''Retrieves the amount of memory that has been reclaimed from this virtual
           machine by the vSphere memory balloon driver (also referred to as the
           "vmmemctl" driver).'''
        counter = c_uint()
        ret = vmGuestLib.VMGuestLib_GetMemBalloonedMB(self.handle.value, byref(counter))
        if ret != VMGUESTLIB_ERROR_SUCCESS: raise VMGuestLibException(ret)
        return counter.value

Response:
Retrieves the amount of memory that has been reclaimed from this virtual
           machine by the vSphere memory balloon driver.


Instruction:
Make a summary of the following Python 3 code
def GetMemBalloonMaxMB(self):
        '''Undocumented.'''
        counter = c_uint()
        ret = vmGuestLib.VMGuestLib_GetMemBalloonMaxMB(self.handle.value, byref(counter))
        if ret != VMGUESTLIB_ERROR_SUCCESS: raise VMGuestLibException(ret)
        return counter.value

Response: Retrieves the maximum amount of memory balloon available for the virtual machine.


Instruction:
How would you explain what the following Python 3 function does
def GetMemBalloonTargetMB(self):
        '''Undocumented.'''
        counter = c_uint()
        ret = vmGuestLib.VMGuestLib_GetMemBalloonTargetMB(self.handle.value, byref(counter))
        if ret != VMGUESTLIB_ERROR_SUCCESS: raise VMGuestLibException(ret)
        return counter.value

Response: Retrieves the amount of memory that is required to balloon the virtual machine can use.


Instruction:
Can you create a Python 3 function that
retrieves the upper limit of memory that is available to the virtual machine. For information about setting a memory limit see Limits and Reservations on page 14.

Response:
def GetMemLimitMB(self):
        '''Retrieves the upper limit of memory that is available to the virtual
           machine. For information about setting a memory limit, see "Limits and
           Reservations" on page 14.'''
        counter = c_uint()
        ret = vmGuestLib.VMGuestLib_GetMemLimitMB(self.handle.value, byref(counter))
        if ret != VMGUESTLIB_ERROR_SUCCESS: raise VMGuestLibException(ret)
        return counter.value


Instruction:
Explain what the following Python 3 code does
def GetMemLLSwappedMB(self):
        '''Undocumented.'''
        counter = c_uint()
        ret = vmGuestLib.VMGuestLib_GetMemLLSwappedMB(self.handle.value, byref(counter))
        if ret != VMGUESTLIB_ERROR_SUCCESS: raise VMGuestLibException(ret)
        return counter.value

Response: Retrieves the amount of memory that is longer than the current memory space.


Instruction:
Can you generate the documentation for the following Python 3 function
def GetMemMappedMB(self):
        '''Retrieves the amount of memory that is allocated to the virtual machine.
           Memory that is ballooned, swapped, or has never been accessed is
           excluded.'''
        counter = c_uint()
        ret = vmGuestLib.VMGuestLib_GetMemMappedMB(self.handle.value, byref(counter))
        if ret != VMGUESTLIB_ERROR_SUCCESS: raise VMGuestLibException(ret)
        return counter.value

Response: Retrieves the amount of memory that is allocated to the virtual machine.


Instruction:
How would you explain what the following Python 3 function does
def GetMemOverheadMB(self):
        '''Retrieves the amount of "overhead" memory associated with this virtual
           machine that is currently consumed on the host system. Overhead
           memory is additional memory that is reserved for data structures required
           by the virtualization layer.'''
        counter = c_uint()
        ret = vmGuestLib.VMGuestLib_GetMemOverheadMB(self.handle.value, byref(counter))
        if ret != VMGUESTLIB_ERROR_SUCCESS: raise VMGuestLibException(ret)
        return counter.value

Response: Retrieves the amount of overhead memory associated with this virtual machine that is currently consumed on the host system. Overhead Memory is reserved for data structures required by the virtualization layer.


Instruction:
How would you code a function in Python 3 to
retrieve the minimum amount of memory that is reserved for the virtual machine.

Response:
def GetMemReservationMB(self):
        '''Retrieves the minimum amount of memory that is reserved for the virtual
           machine. For information about setting a memory reservation, see "Limits
           and Reservations" on page 14.'''
        counter = c_uint()
        ret = vmGuestLib.VMGuestLib_GetMemReservationMB(self.handle.value, byref(counter))
        if ret != VMGUESTLIB_ERROR_SUCCESS: raise VMGuestLibException(ret)
        return counter.value


Instruction:
Write a Python 3 function for
retrieving the amount of physical memory that is shared with this virtual machine that is copy - on - write on the host.

Response:
def GetMemSharedMB(self):
        '''Retrieves the amount of physical memory associated with this virtual
           machine that is copy-on-write (COW) shared on the host.'''
        counter = c_uint()
        ret = vmGuestLib.VMGuestLib_GetMemSharedMB(self.handle.value, byref(counter))
        if ret != VMGUESTLIB_ERROR_SUCCESS: raise VMGuestLibException(ret)
        return counter.value


Instruction:
Given the following Python 3 function, write the documentation
def GetMemSharedSavedMB(self):
        '''Retrieves the estimated amount of physical memory on the host saved
           from copy-on-write (COW) shared guest physical memory.'''
        counter = c_uint()
        ret = vmGuestLib.VMGuestLib_GetMemSharedSavedMB(self.handle.value, byref(counter))
        if ret != VMGUESTLIB_ERROR_SUCCESS: raise VMGuestLibException(ret)
        return counter.value

Response:
Retrieves the estimated amount of physical memory on the host saved
           from copy - on - write ( COW ) shared guest physical memory.


Instruction:
How would you code a function in Python 3 to
retrieve the number of memory shares allocated to the virtual machine.

Response:
def GetMemShares(self):
        '''Retrieves the number of memory shares allocated to the virtual machine.
           For information about how an ESX server uses memory shares to manage
           virtual machine priority, see the vSphere Resource Management Guide.'''
        counter = c_uint()
        ret = vmGuestLib.VMGuestLib_GetMemShares(self.handle.value, byref(counter))
        if ret != VMGUESTLIB_ERROR_SUCCESS: raise VMGuestLibException(ret)
        return counter.value


Instruction:
Can you generate a brief explanation for the following Python 3 code
def GetMemSwappedMB(self):
        '''Retrieves the amount of memory that has been reclaimed from this virtual
           machine by transparently swapping guest memory to disk.'''
        counter = c_uint()
        ret = vmGuestLib.VMGuestLib_GetMemSwappedMB(self.handle.value, byref(counter))
        if ret != VMGUESTLIB_ERROR_SUCCESS: raise VMGuestLibException(ret)
        return counter.value

Response:
Retrieves the amount of memory that has been reclaimed from this virtual
           machine by transparently swapping guest memory to disk.


Instruction:
Implement a function in Python 3 to
retrieve the amount of memory that the VM can swap into.

Response:
def GetMemSwapTargetMB(self):
        '''Undocumented.'''
        counter = c_uint()
        ret = vmGuestLib.VMGuestLib_GetMemSwapTargetMB(self.handle.value, byref(counter))
        if ret != VMGUESTLIB_ERROR_SUCCESS: raise VMGuestLibException(ret)
        return counter.value


Instruction:
Can you generate a brief explanation for the following Python 3 code
def GetMemTargetSizeMB(self):
        '''Retrieves the size of the target memory allocation for this virtual machine.'''
        counter = c_uint()
        ret = vmGuestLib.VMGuestLib_GetMemTargetSizeMB(self.handle.value, byref(counter))
        if ret != VMGUESTLIB_ERROR_SUCCESS: raise VMGuestLibException(ret)
        return counter.value

Response: Retrieves the size of the target memory allocation for this virtual machine.


Instruction:
Implement a Python 3 function for
retrieving the estimated amount of physical host memory currently consumed for this virtual machine s physical memory.

Response:
def GetMemUsedMB(self):
        '''Retrieves the estimated amount of physical host memory currently
           consumed for this virtual machine's physical memory.'''
        counter = c_uint()
        ret = vmGuestLib.VMGuestLib_GetMemUsedMB(self.handle.value, byref(counter))
        if ret != VMGUESTLIB_ERROR_SUCCESS: raise VMGuestLibException(ret)
        return counter.value


Instruction:
Implement a Python 3 function for
retrieving the amount of memory that is compressed for the virtual machine.

Response:
def GetMemZippedMB(self):
        '''Undocumented.'''
        counter = c_uint()
        ret = vmGuestLib.VMGuestLib_GetMemZippedMB(self.handle.value, byref(counter))
        if ret != VMGUESTLIB_ERROR_SUCCESS: raise VMGuestLibException(ret)
        return counter.value


Instruction:
Here you have a function in Python 3, explain what it does
def GetMemZipSavedMB(self):
        '''Undocumented.'''
        counter = c_uint()
        ret = vmGuestLib.VMGuestLib_GetMemZipSavedMB(self.handle.value, byref(counter))
        if ret != VMGUESTLIB_ERROR_SUCCESS: raise VMGuestLibException(ret)
        return counter.value

Response: Retrieves the amount of memory that has been saved to the memory zip.


Instruction:
Can you create a Python 3 function that
evaluates a B - or M - or I - spline with the specified internal knots order and coefficients.

Response:
def spev(t_int, C, deg, x, cov_C=None, M_spline=False, I_spline=False, n=0):
    """Evaluate a B-, M- or I-spline with the specified internal knots, order and coefficients.

    `deg` boundary knots are appended at both sides of the domain.

    The zeroth order basis functions are modified to ensure continuity at the
    right-hand boundary.

    Note that the I-splines include the :math:`i=0` case in order to have a "DC
    offset". This way your functions do not have to start at zero. If you want
    to not include this, simply set the first coefficient in `C` to zero.

    Parameters
    ----------
    t_int : array of float, (`M`,)
        The internal knot locations. Must be monotonic (this is NOT checked).
    C : array of float, (`M + deg - 1`,)
        The coefficients applied to the basis functions.
    deg : nonnegative int
        The polynomial degree to use.
    x : array of float, (`N`,)
        The locations to evaluate the spline at.
    cov_C : array of float, (`M + deg - 1`,) or (`M + deg - 1`, `M + deg - 1`), optional
        The covariance matrix of the coefficients. If a 1d array is passed, this
        is treated as the variance. If None, then the uncertainty is not
        computed.
    M_spline : bool, optional
        If True, compute the M-spline instead of the B-spline. M-splines are
        normalized to integrate to unity, as opposed to B-splines which sum to
        unity at all points. Default is False (compute B-spline).
    I_spline : bool, optional
        If True, compute the I-spline instead of the B-spline. Note that this
        will override `M_spline`. I-splines are the integrals of the M-splines,
        and hence ensure curves are monotonic if all coefficients are of the
        same sign. Note that the I-splines returned will be of polynomial degree
        `deg` (i.e., the integral of what is returned from calling the function
        with `deg=deg-1` and `M_spline=True`. Default is False (compute B-spline
        or M-spline).
    n : int, optional
        The derivative order to compute. Default is 0. If `n>d`, all zeros are
        returned (i.e., the discontinuities are not included).

    Returns
    -------
    `y` or (`y`, `cov_y`): The values (and possibly uncertainties) of the spline
    at the specified locations.
    """
    C = scipy.asarray(C, dtype=float)
    t_int = scipy.asarray(t_int, dtype=float)
    if (t_int != scipy.sort(t_int)).any():
        raise ValueError("Knots must be in increasing order!")
    # if len(scipy.unique(t_int)) != len(t_int):
    #     raise ValueError("Knots must be unique!")
    if n > deg:
        return scipy.zeros_like(x, dtype=float)
    if I_spline:
        # I_{i,k} = int_L^x M_{i,k}(u)du, so just take the derivative of the
        # underlying M-spline. Discarding the first coefficient dumps the "DC
        # offset" term.
        if cov_C is not None:
            cov_C = scipy.asarray(cov_C)
            if cov_C.ndim == 1:
                cov_C = cov_C[1:]
            elif cov_C.ndim == 2:
                cov_C = cov_C[1:, 1:]
        if n > 0:
            return spev(
                t_int, C[1:], deg - 1, x,
                cov_C=cov_C, M_spline=True, I_spline=False, n=n - 1
            )
        M_spline = True
    if n > 0:
        if M_spline:
            t = scipy.concatenate(([t_int[0]] * deg, t_int, [t_int[-1]] * deg))
            C = (deg + 1.0) * (
                C[1:] / (t[deg + 2:len(t_int) + 2 * deg] - t[1:len(t_int) + deg - 1]) -
                C[:-1] / (t[deg + 1:len(t_int) + 2 * deg - 1] - t[:len(t_int) + deg - 2])
            )
        else:
            C = C[1:] - C[:-1]
        return spev(
            t_int, C, deg - 1, x,
            cov_C=cov_C, M_spline=True, I_spline=False, n=n - 1
        )

    if len(C) != len(t_int) + deg - 1:
        raise ValueError("Length of C must be equal to M + deg - 1!")

    # Append the external knots directly at the boundary:
    t = scipy.concatenate(([t_int[0]] * deg, t_int, [t_int[-1]] * deg))

    # Compute the different orders:
    B = scipy.zeros((deg + 1, len(t) - 1, len(x)))

    # NOTE: The first dimension is indexed by deg, and is zero-indexed.

    # Zeroth order: constant function
    d = 0
    for i in xrange(deg, deg + len(t_int) - 2 + 1):
        # The second condition contains a hack to make the basis functions
        # continuous at the right-hand edge.
        mask = (t[i] <= x) & (
            (x < t[i + 1]) | ((i == deg + len(t_int) - 2) & (x == t[-1]))
        )
        B[d, i, mask] = 1.0 / (t[i + 1] - t[i]) if M_spline else 1.0

    # Loop over other orders:
    for d in xrange(1, deg + 1):
        for i in xrange(deg - d, deg + len(t_int) - 2 + 1):
            if t[i + d] != t[i]:
                v = (x - t[i]) * B[d - 1, i, :]
                if not M_spline:
                    v /= t[i + d] - t[i]
                B[d, i, :] += v
            if t[i + d + 1] != t[i + 1]:
                v = (t[i + d + 1] - x) * B[d - 1, i + 1, :]
                if not M_spline:
                    v /= t[i + d + 1] - t[i + 1]
                B[d, i, :] += v
            if M_spline and ((t[i + d] != t[i]) or (t[i + d + 1] != t[i + 1])):
                B[d, i, :] *= (d + 1) / (d * (t[i + d + 1] - t[i]))

    B = B[deg, 0:len(C), :].T

    # Now compute the I-splines, if needed:
    if I_spline:
        I = scipy.zeros_like(B)
        for i in xrange(0, len(C)):
            for m in xrange(i, len(C)):
                I[:, i] += (t[m + deg + 1] - t[m]) * B[:, m] / (deg + 1.0)
        B = I

    y = B.dot(C)
    if cov_C is not None:
        cov_C = scipy.asarray(cov_C)
        # If there are no covariances, promote cov_C to a diagonal matrix
        if cov_C.ndim == 1:
            cov_C = scipy.diag(cov_C)
        cov_y = B.dot(cov_C).dot(B.T)
        return (y, cov_y)
    else:
        return y


Instruction:
Create a Python 3 function to
build an argparse argument parser to parse the command line.

Response:
def parser(subparsers):
    "Build an argparse argument parser to parse the command line."

    # create the parser for the version subcommand.
    parser_version = subparsers.add_parser(
        'version',
        help="Output the version of %(prog)s to the console.")
    parser_version.set_defaults(func=command_version)

    return parser_version


Instruction:
How would you code a function in Python 3 to
find the specified Crispy FormHelper and instantiate it.

Response:
def wafer_form_helper(context, helper_name):
    '''
    Find the specified Crispy FormHelper and instantiate it.
    Handy when you are crispyifying other apps' forms.
    '''
    request = context.request
    module, class_name = helper_name.rsplit('.', 1)
    if module not in sys.modules:
        __import__(module)
    mod = sys.modules[module]
    class_ = getattr(mod, class_name)
    return class_(request=request)


Instruction:
Write a Python 3 function for
redirecting to the profile URL

Response:
def redirect_profile(request):
    '''
    The default destination from logging in, redirect to the actual profile URL
    '''
    if request.user.is_authenticated:
        return HttpResponseRedirect(reverse('wafer_user_profile',
                                            args=(request.user.username,)))
    else:
        return redirect_to_login(next=reverse(redirect_profile))


Instruction:
Make a summary of the following Python 3 code
def reviewed_badge(user, talk):
    context = {
        'reviewed': False,
    }

    review = None
    if user and not user.is_anonymous():
        review = talk.reviews.filter(reviewer=user).first()

    if review:
        context['reviewed'] = True
        context['review_is_current'] = review.is_current()

    return context

Response: Returns a badge for the user s reviews of the talk


Instruction:
Given the following Python 3 function, write the documentation
def linear_warp(X, d, n, *args):
    X = scipy.asarray(X, dtype=float)

    a = args[2 * d]
    b = args[2 * d + 1]

    if n == 0:
        return (X - a) / (b - a)
    elif n == 1:
        return 1.0 / (b - a) * scipy.ones_like(X)
    else:
        return scipy.zeros_like(X)

Response: r Linear warping of the inputs X to the unit hypercube.


Instruction:
Create a Python 3 function to
evaluate the possibly recursive warping function and its derivatives.

Response:
def w_func(self, X, d, n):
        """Evaluate the (possibly recursive) warping function and its derivatives.

        Parameters
        ----------
        X : array, (`M`,)
            The points (from dimension `d`) to evaluate the warping function at.
        d : int
            The dimension to warp.
        n : int
            The derivative order to compute. So far only 0 and 1 are supported.
        """
        if n == 0:
            wX = self.w(X, d, 0)
            if isinstance(self.k, WarpedKernel):
                wX = self.k.w_func(wX, d, 0)
            return wX
        elif n == 1:
            wXn = self.w(X, d, n)
            if isinstance(self.k, WarpedKernel):
                wX = self.w_func(X, d, 0)
                wXn *= self.k.w_func(wX, d, n)
            return wXn
        else:
            raise ValueError("Derivative orders greater than one are not supported!")


Instruction:
How would you explain what the following Python 3 function does
def enforce_bounds(self, v):
        self._enforce_bounds = v
        self.k.enforce_bounds = v
        self.w.enforce_bounds = v

Response: Set enforce_bounds for both kernels and kernels to a new value.


Instruction:
How would you code a function in Python 3 to
set the free parameters.

Response:
def free_params(self, value):
        """Set the free parameters. Note that this bypasses enforce_bounds.
        """
        value = scipy.asarray(value, dtype=float)
        self.K_up_to_date = False
        self.k.free_params = value[:self.k.num_free_params]
        self.w.free_params = value[self.k.num_free_params:self.k.num_free_params + self.w.num_free_params]


Instruction:
Implement a Python 3 function for
setting the ( free ) hyperparameters.

Response:
def set_hyperparams(self, new_params):
        """Set the (free) hyperparameters.

        Parameters
        ----------
        new_params : :py:class:`Array` or other Array-like
            New values of the free parameters.

        Raises
        ------
        ValueError
            If the length of `new_params` is not consistent with :py:attr:`self.params`.
        """
        new_params = scipy.asarray(new_params, dtype=float)

        if len(new_params) == len(self.free_params):
            num_free_k = sum(~self.k.fixed_params)
            self.k.set_hyperparams(new_params[:num_free_k])
            self.w.set_hyperparams(new_params[num_free_k:])
        else:
            raise ValueError("Length of new_params must be %s!" % (len(self.free_params),))


Instruction:
Can you generate a brief explanation for the following Python 3 code
def get(self, request, *args, **kwargs):

        # Prepare base
        if 'pk' in kwargs:
            self.object = self.get_object()
        else:
            self.object = None
        form_class = self.get_form_class()

        # Get prefix
        if 'field_prefix' in form_class.Meta.__dict__:
            # Get name from the form
            field_prefix = form_class.Meta.field_prefix
        else:
            # Get name from the class
            field_prefix = str(form_class).split("'")[1].split(".")[-1]
        self.field_prefix = field_prefix

        # Build form
        form = self.get_form(form_class)

        # Find groups
        if 'groups' in dir(self):
            # Save groups
            groups = self.groups
            # Redefine groups inside the form
            form.__groups__ = lambda: groups
            # Initialize list of fields
            fields = []
        else:
            groups = None

        # Add special prefix support to properly support form independency
        form.add_prefix = lambda fields_name, field_prefix=field_prefix: "%s_%s" % (field_prefix, fields_name)
        if 'autofill' not in dir(form.Meta):
            form.Meta.autofill = {}

        # For every extra form
        forms = []
        position_form_default = 0
        for (formelement, linkerfield, modelfilter) in self.forms:
            if formelement is None:
                formobj = form
                position_form_default = len(forms)
            else:
                # Locate linked element
                if self.object:
                    related_name = formelement._meta.model._meta.get_field(linkerfield).related_query_name()
                    queryset = getattr(self.object, related_name)
                    if modelfilter:
                        queryset = queryset.filter(eval("Q(%s)" % (modelfilter)))
                    get_method = getattr(queryset, 'get', None)
                    if get_method:
                        instance = queryset.get()
                    else:
                        instance = queryset
                else:
                    instance = None

                if 'autofill' in dir(formelement.Meta):
                    formname = str(formelement).split('.')[-1].split("'")[0]
                    for key in formelement.Meta.autofill:
                        form.Meta.autofill['{}_{}'.format(formname, key)] = formelement.Meta.autofill[key]

                # Get prefix
                if 'field_prefix' in formelement.Meta.__dict__:
                    # Get name from the form
                    field_prefix = formelement.Meta.field_prefix
                else:
                    # Get name from the class
                    field_prefix = str(formelement).split("'")[1].split(".")[-1]
                self.field_prefix = field_prefix

                # Prepare form
                formobj = formelement(instance=instance)
                formobj.form_name = form.form_name

                # Excluded fields
                if 'exclude' not in formobj.Meta.__dict__:
                    formobj.Meta.exclude = [linkerfield]
                elif linkerfield not in formobj.Meta.exclude:
                    formobj.Meta.exclude.append(linkerfield)
                if linkerfield in formobj.fields:
                    del(formobj.fields[linkerfield])

                # Add special prefix support to properly support form independency
                formobj.add_prefix = lambda fields_name, field_prefix=field_prefix: "%s_%s" % (field_prefix, fields_name)
                formobj.scope_prefix = field_prefix

            # Save fields to the list
            if groups:
                for field in formobj:
                    fields.append(field)
            else:
                # Add the form to the list of forms
                forms.append(formobj)

        if position_form_default == 0:
            open_tabs = 1
        else:
            open_tabs = 0
        # Remember list of fields
        if groups:
            form.list_fields = fields

        # Add context and return new context
        return self.render_to_response(self.get_context_data(form=form, forms=forms, open_tabs=open_tabs, position_form_default=position_form_default))

Response: Handles GET requests and instantiates blank versions of the form and its inline formsets.


Instruction:
Here you have a function in Python 3, explain what it does
def post(self, request, *args, **kwargs):

        # Prepare base
        if 'pk' in kwargs:
            self.object = self.get_object()
        else:
            self.object = None
        form_class = self.get_form_class()

        # Get prefix
        if 'field_prefix' in form_class.Meta.__dict__:
            # Get name from the form
            field_prefix = form_class.Meta.field_prefix
            # Initialize list of fields
        else:
            # Get name from the class
            field_prefix = str(form_class).split("'")[1].split(".")[-1]
        self.field_prefix = field_prefix

        # Build the form
        form = self.get_form(form_class)

        # Find groups
        if 'groups' in dir(self):
            # Save groups
            groups = self.groups
            # Redefine groups inside the form
            form.__groups__ = lambda: groups
            # Initialize list of fields
            fields = []
        else:
            groups = None

        # Add special prefix support to properly support form independency
        form.add_prefix = lambda fields_name, field_prefix=field_prefix: "%s_%s" % (field_prefix, fields_name)

        # Check validation
        valid = form.is_valid()
        if (not valid) and ('non_field_errors' in dir(self)):
            errors = [element[5] for element in list(self.non_field_errors())[:-1]]
        elif form.errors.as_data():
            errors = []
            for element in form.errors.as_data():
                for err in form.errors.as_data()[element][0]:
                    errors.append(err)
        else:
            errors = []

        # For every extra form
        temp_forms = []
        position_form_default = 0
        for (formelement, linkerfield, modelfilter) in self.forms:
            if formelement is None:
                formobj = form
                position_form_default = len(temp_forms)
            else:
                # Locate linked element
                if self.object:
                    related_name = formelement._meta.model._meta.get_field(linkerfield).related_query_name()
                    queryset = getattr(self.object, related_name)
                    if modelfilter:
                        queryset = queryset.filter(eval("Q(%s)" % (modelfilter)))
                    get_method = getattr(queryset, 'get', None)
                    if get_method:
                        instance = queryset.get()
                    else:
                        instance = queryset
                else:
                    instance = None

                # Get prefix
                if 'field_prefix' in formelement.Meta.__dict__:
                    # Get name from the form
                    field_prefix = formelement.Meta.field_prefix
                else:
                    # Get name from the class
                    field_prefix = str(formelement).split("'")[1].split(".")[-1]
                self.field_prefix = field_prefix

                # Prepare form
                formobj = formelement(instance=instance, data=self.request.POST)
                formobj.form_name = form.form_name

                # Excluded fields
                if 'exclude' not in formobj.Meta.__dict__:
                    formobj.Meta.exclude = [linkerfield]
                elif linkerfield not in formobj.Meta.exclude:
                    formobj.Meta.exclude.append(linkerfield)
                if linkerfield in formobj.fields:
                    del(formobj.fields[linkerfield])

                # Link it to the main model
                formobj.add_prefix = lambda fields_name, field_prefix=field_prefix: "%s_%s" % (field_prefix, fields_name)

                # Validate
                valid *= formobj.is_valid()

                # append error
                if not formobj.is_valid() and ('non_field_errors' in dir(formobj)):
                    errors += [element[5] for element in list(formobj.non_field_errors())[:-1]]

            # Save fields to the list
            if groups:
                for field in formobj:
                    # raise Exception (field.__dict__)
                    if 'unblock_t2ime' in field.html_name:
                        raise Exception(field.field.__dict__)
                    fields.append(field)

            # Add a new form
            temp_forms.append((formobj, linkerfield))

        # execute validation specified
        validate_forms = None
        if valid and ("validate" in dir(self)):
            validate_forms = [tform[0] for tform in temp_forms]
            errors = self.validate(*validate_forms)
            # valid = len(errors) == 0
            valid = False
            if errors is None or len(errors) == 0:
                valid = True

        # Remember list of fields
        if groups:
            form.list_fields = fields
            forms = []
        else:
            if validate_forms:
                forms = validate_forms
            else:
                forms = [tform[0] for tform in temp_forms]

        if position_form_default == 0:
            open_tabs = 1
        else:
            open_tabs = 0

        # Check validation result
        if valid:
            # Everything is OK, call valid
            return self.form_valid(form, temp_forms)
        else:
            # Something went wrong, attach error and call invalid
            form.list_errors = errors
            return self.form_invalid(form, forms, open_tabs, position_form_default)

Response: This method is called by the base class when a POST request is made.


Instruction:
How would you explain what the following Python 3 function does
def form_valid(self, form, forms):
        if self.object:
            form.save()
            for (formobj, linkerfield) in forms:
                if form != formobj:
                    formobj.save()
        else:
            self.object = form.save()
            for (formobj, linkerfield) in forms:
                if form != formobj:
                    setattr(formobj.instance, linkerfield, self.object)
                    formobj.save()
        return HttpResponseRedirect(self.get_success_url())

Response: Called if all forms are valid. Creates a Recipe instance along with associated Ingredients and Instructions and redirects to a success page.


Instruction:
How would you explain what the following Python 3 function does
def form_invalid(self, form, forms, open_tabs, position_form_default):
        # return self.render_to_response( self.get_context_data( form = form, forms = forms ) )
        return self.render_to_response(self.get_context_data(form=form, forms=forms, open_tabs=open_tabs, position_form_default=position_form_default))

Response: Called if a form is invalid. Re - renders the context data with the data filled forms and errors.


Instruction:
Explain what the following Python 3 code does
def wrap_fmin_slsqp(fun, guess, opt_kwargs={}):
    opt_kwargs = dict(opt_kwargs)

    opt_kwargs.pop('method', None)

    eqcons = []
    ieqcons = []
    if 'constraints' in opt_kwargs:
        if isinstance(opt_kwargs['constraints'], dict):
            opt_kwargs['constraints'] = [opt_kwargs['constraints'],]
        for con in opt_kwargs.pop('constraints'):
            if con['type'] == 'eq':
                eqcons += [con['fun'],]
            elif con['type'] == 'ineq':
                ieqcons += [con['fun'],]
            else:
                raise ValueError("Invalid constraint type %s!" % (con['type'],))

    if 'jac' in opt_kwargs:
        warnings.warn("Jacobian not supported for default solver SLSQP!",
                      RuntimeWarning)
        opt_kwargs.pop('jac')

    if 'tol' in opt_kwargs:
        opt_kwargs['acc'] = opt_kwargs.pop('tol')

    if 'options' in opt_kwargs:
        opts = opt_kwargs.pop('options')
        opt_kwargs = dict(opt_kwargs.items() + opts.items())

    # Other keywords with less likelihood for causing failures are silently ignored:
    opt_kwargs.pop('hess', None)
    opt_kwargs.pop('hessp', None)
    opt_kwargs.pop('callback', None)

    out, fx, its, imode, smode = scipy.optimize.fmin_slsqp(
        fun,
        guess,
        full_output=True,
        eqcons=eqcons,
        ieqcons=ieqcons,
        **opt_kwargs
    )

    Result = collections.namedtuple('Result',
                                    ['status', 'success', 'fun', 'x', 'message', 'nit'])

    return Result(status=imode,
                  success=(imode == 0),
                  fun=fx,
                  x=out,
                  message=smode,
                  nit=its)

Response: A wrapper for fmin_slsqp that allows the user to call a function with the given parameters.


Instruction:
Can you tell what is the following Python 3 function doing
def yn2Kn2Der(nu, y, n=0, tol=5e-4, nterms=1, nu_step=0.001):
    n = int(n)
    y = scipy.asarray(y, dtype=float)

    if n == 0:
        K = y**(nu / 2.0) * scipy.special.kv(nu, scipy.sqrt(y))
        K[y == 0.0] = scipy.special.gamma(nu) / 2.0**(1.0 - nu)
    else:
        K = scipy.zeros_like(y)
        for k in scipy.arange(0.0, n + 1.0, dtype=float):
            K += (
                scipy.special.binom(n, k) * fixed_poch(1.0 + nu / 2.0 - k, k) *
                y**(nu / 2.0 - k) * Kn2Der(nu, y, n=n-k)
            )
        # Do the extra work to handle y == 0 only if we need to:
        mask = (y == 0.0)
        if (mask).any():
            if int(nu) == nu:
                K[mask] = 0.5 * (
                    yn2Kn2Der(nu - nu_step, y[mask], n=n, tol=tol, nterms=nterms, nu_step=nu_step) +
                    yn2Kn2Der(nu + nu_step, y[mask], n=n, tol=tol, nterms=nterms, nu_step=nu_step)
                )
            else:
                if n > nu:
                    K[mask] = scipy.special.gamma(-nu) * fixed_poch(1 + nu - n, n) * scipy.inf
                else:
                    K[mask] = scipy.special.gamma(nu) * scipy.special.gamma(n + 1.0) / (
                        2.0**(1.0 - nu + 2.0 * n) * fixed_poch(1.0 - nu, n) *
                        scipy.special.factorial(n)
                    )
    if tol > 0.0:
        # Replace points within tol (absolute distance) of zero with the power
        # series approximation:
        mask = (y <= tol) & (y > 0.0)
        K[mask] = 0.0
        if int(nu) == nu:
            K[mask] = 0.5 * (
                yn2Kn2Der(nu - nu_step, y[mask], n=n, tol=tol, nterms=nterms, nu_step=nu_step) +
                yn2Kn2Der(nu + nu_step, y[mask], n=n, tol=tol, nterms=nterms, nu_step=nu_step)
            )
        else:
            for k in scipy.arange(n, n + nterms, dtype=float):
                K[mask] += (
                    scipy.special.gamma(nu) * fixed_poch(1.0 + k - n, n) * y[mask]**(k - n) / (
                        2.0**(1.0 - nu + 2 * k) * fixed_poch(1.0 - nu, k) * scipy.special.factorial(k))
                    )
            for k in scipy.arange(0, nterms, dtype=float):
                K[mask] += (
                    scipy.special.gamma(-nu) * fixed_poch(1.0 + nu + k - n, n) *
                    y[mask]**(nu + k - n) / (
                        2.0**(1.0 + nu + 2.0 * k) * fixed_poch(1.0 + nu, k) *
                        scipy.special.factorial(k)
                    )
                )

    return K

Response: r Returns the modified Bessel function and its derivatives.


Instruction:
Given the following Python 3 function, write the documentation
def incomplete_bell_poly(n, k, x):
    if n == 0 and k == 0:
        return scipy.ones(x.shape[0], dtype=float)
    elif k == 0 and n >= 1:
        return scipy.zeros(x.shape[0], dtype=float)
    elif n == 0 and k >= 1:
        return scipy.zeros(x.shape[0], dtype=float)
    else:
        result = scipy.zeros(x.shape[0], dtype=float)
        for m in xrange(0, n - k + 1):
            result += x[:, m] * scipy.special.binom(n - 1, m) * incomplete_bell_poly(n - (m + 1), k - 1, x)
        return result

Response: rRecursive evaluation of the incomplete Bell polynomial.


Instruction:
How would you code a function in Python 3 to
generate the restricted growth strings for all of the partitions of a set of n - member sets.

Response:
def generate_set_partition_strings(n):
    """Generate the restricted growth strings for all of the partitions of an `n`-member set.

    Uses Algorithm H from page 416 of volume 4A of Knuth's `The Art of Computer
    Programming`. Returns the partitions in lexicographical order.

    Parameters
    ----------
    n : scalar int, non-negative
        Number of (unique) elements in the set to be partitioned.

    Returns
    -------
    partitions : list of :py:class:`Array`
        List has a number of elements equal to the `n`-th Bell number (i.e.,
        the number of partitions for a set of size `n`). Each element has
        length `n`, the elements of which are the restricted growth strings
        describing the partitions of the set. The strings are returned in
        lexicographic order.
    """
    # Handle edge cases:
    if n == 0:
        return []
    elif n == 1:
        return [scipy.array([0])]

    partitions = []

    # Step 1: Initialize
    a = scipy.zeros(n, dtype=int)
    b = scipy.ones(n, dtype=int)

    while True:
        # Step 2: Visit
        partitions.append(a.copy())
        if a[-1] == b[-1]:
            # Step 4: Find j. j is the index of the first element from the end
            # for which a != b, with the exception of the last element.
            j = (a[:-1] != b[:-1]).nonzero()[0][-1]
            # Step 5: Increase a_j (or terminate):
            if j == 0:
                break
            else:
                a[j] += 1
                # Step 6: Zero out a_{j+1} to a_n:
                b[-1] = b[j] + (a[j] == b[j])
                a[j + 1:] = 0
                b[j + 1 :-1] = b[-1]
        else:
            # Step 3: Increase a_n:
            a[-1] += 1

    return partitions


Instruction:
How would you explain what the following Python 3 function does
def generate_set_partitions(set_):
    set_ = scipy.asarray(set_)
    strings = generate_set_partition_strings(len(set_))
    partitions = []
    for string in strings:
        blocks = []
        for block_num in scipy.unique(string):
            blocks.append(set_[string == block_num])
        partitions.append(blocks)

    return partitions

Response: Generates all of the partitions of a set.


Instruction:
Can you implement a function in Python 3 that
finds unique rows in numpy. array.

Response:
def unique_rows(arr, return_index=False, return_inverse=False):
    """Returns a copy of arr with duplicate rows removed.

    From Stackoverflow "Find unique rows in numpy.array."

    Parameters
    ----------
    arr : :py:class:`Array`, (`m`, `n`)
        The array to find the unique rows of.
    return_index : bool, optional
        If True, the indices of the unique rows in the array will also be
        returned. I.e., unique = arr[idx]. Default is False (don't return
        indices).
    return_inverse: bool, optional
        If True, the indices in the unique array to reconstruct the original
        array will also be returned. I.e., arr = unique[inv]. Default is False
        (don't return inverse).

    Returns
    -------
    unique : :py:class:`Array`, (`p`, `n`) where `p` <= `m`
        The array `arr` with duplicate rows removed.
    """
    b = scipy.ascontiguousarray(arr).view(
        scipy.dtype((scipy.void, arr.dtype.itemsize * arr.shape[1]))
    )
    try:
        out = scipy.unique(b, return_index=True, return_inverse=return_inverse)
        dum = out[0]
        idx = out[1]
        if return_inverse:
            inv = out[2]
    except TypeError:
        if return_inverse:
            raise RuntimeError(
                "Error in scipy.unique on older versions of numpy prevents "
                "return_inverse from working!"
            )
        # Handle bug in numpy 1.6.2:
        rows = [_Row(row) for row in b]
        srt_idx = sorted(range(len(rows)), key=rows.__getitem__)
        rows = scipy.asarray(rows)[srt_idx]
        row_cmp = [-1]
        for k in xrange(1, len(srt_idx)):
            row_cmp.append(rows[k-1].__cmp__(rows[k]))
        row_cmp = scipy.asarray(row_cmp)
        transition_idxs = scipy.where(row_cmp != 0)[0]
        idx = scipy.asarray(srt_idx)[transition_idxs]
    out = arr[idx]
    if return_index:
        out = (out, idx)
    elif return_inverse:
        out = (out, inv)
    elif return_index and return_inverse:
        out = (out, idx, inv)
    return out


Instruction:
Given the following Python 3 function, write the documentation
def compute_stats(vals, check_nan=False, robust=False, axis=1, plot_QQ=False, bins=15, name=''):
    if axis != 1 and robust:
        raise NotImplementedError("Values of axis other than 1 are not supported "
                                  "with the robust keyword at this time!")
    if robust:
        # TODO: This stuff should really be vectorized if there is something that allows it!
        if check_nan:
            mean = scipy.stats.nanmedian(vals, axis=axis)
            # TODO: HANDLE AXIS PROPERLY!
            std = scipy.zeros(vals.shape[0], dtype=float)
            for k in xrange(0, len(vals)):
                ch = vals[k]
                ok_idxs = ~scipy.isnan(ch)
                if ok_idxs.any():
                    std[k] = (scipy.stats.scoreatpercentile(ch[ok_idxs], 75) -
                              scipy.stats.scoreatpercentile(ch[ok_idxs], 25))
                else:
                    # Leave a nan where there are no non-nan values:
                    std[k] = scipy.nan
            std /= IQR_TO_STD
        else:
            mean = scipy.median(vals, axis=axis)
            # TODO: HANDLE AXIS PROPERLY!
            std = scipy.asarray([scipy.stats.scoreatpercentile(ch, 75.0) -
                                 scipy.stats.scoreatpercentile(ch, 25.0)
                                 for ch in vals]) / IQR_TO_STD
    else:
        if check_nan:
            mean = scipy.stats.nanmean(vals, axis=axis)
            std = scipy.stats.nanstd(vals, axis=axis)
        else:
            mean = scipy.mean(vals, axis=axis)
            std = scipy.std(vals, axis=axis)
    if plot_QQ:
        f = plt.figure()
        gs = mplgs.GridSpec(2, 2, height_ratios=[8, 1])
        a_QQ = f.add_subplot(gs[0, 0])
        a_hist = f.add_subplot(gs[0, 1])
        a_slider = f.add_subplot(gs[1, :])

        title = f.suptitle("")

        def update(val):
            """Update the index from the results to be displayed.
            """
            a_QQ.clear()
            a_hist.clear()
            idx = slider.val
            title.set_text("%s, n=%d" % (name, idx))

            nan_idxs = scipy.isnan(vals[idx, :])
            if not nan_idxs.all():
                osm, osr = scipy.stats.probplot(vals[idx, ~nan_idxs], dist='norm', plot=None, fit=False)
                a_QQ.plot(osm, osr, 'bo', markersize=10)
                a_QQ.set_title('QQ plot')
                a_QQ.set_xlabel('quantiles of $\mathcal{N}(0,1)$')
                a_QQ.set_ylabel('quantiles of data')

                a_hist.hist(vals[idx, ~nan_idxs], bins=bins, normed=True)
                locs = scipy.linspace(vals[idx, ~nan_idxs].min(), vals[idx, ~nan_idxs].max())
                a_hist.plot(locs, scipy.stats.norm.pdf(locs, loc=mean[idx], scale=std[idx]))
                a_hist.set_title('Normalized histogram and reported PDF')
                a_hist.set_xlabel('value')
                a_hist.set_ylabel('density')

            f.canvas.draw()

        def arrow_respond(slider, event):
            """Event handler for arrow key events in plot windows.

            Pass the slider object to update as a masked argument using a lambda function::

                lambda evt: arrow_respond(my_slider, evt)

            Parameters
            ----------
            slider : Slider instance associated with this handler.
            event : Event to be handled.
            """
            if event.key == 'right':
                slider.set_val(min(slider.val + 1, slider.valmax))
            elif event.key == 'left':
                slider.set_val(max(slider.val - 1, slider.valmin))

        slider = mplw.Slider(a_slider,
                             'index',
                             0,
                             len(vals) - 1,
                             valinit=0,
                             valfmt='%d')
        slider.on_changed(update)
        update(0)
        f.canvas.mpl_connect('key_press_event', lambda evt: arrow_respond(slider, evt))

    return (mean, std)

Response: Compute the average statistics for the given values along the specified axis.


Instruction:
How would you code a function in Python 3 to
make a plot of a mean curve with uncertainty envelopes.

Response:
def univariate_envelope_plot(x, mean, std, ax=None, base_alpha=0.375, envelopes=[1, 3], lb=None, ub=None, expansion=10, **kwargs):
    """Make a plot of a mean curve with uncertainty envelopes.
    """
    if ax is None:
        f = plt.figure()
        ax = f.add_subplot(1, 1, 1)
    elif ax == 'gca':
        ax = plt.gca()

    mean = scipy.asarray(mean, dtype=float).copy()
    std = scipy.asarray(std, dtype=float).copy()

    # Truncate the data so matplotlib doesn't die:
    if lb is not None and ub is not None and expansion != 1.0:
        expansion *= ub - lb
        ub = ub + expansion
        lb = lb - expansion
    if ub is not None:
        mean[mean > ub] = ub
    if lb is not None:
        mean[mean < lb] = lb

    l = ax.plot(x, mean, **kwargs)
    color = plt.getp(l[0], 'color')
    e = []
    for i in envelopes:
        lower = mean - i * std
        upper = mean + i * std
        if ub is not None:
            lower[lower > ub] = ub
            upper[upper > ub] = ub
        if lb is not None:
            lower[lower < lb] = lb
            upper[upper < lb] = lb
        e.append(ax.fill_between(x, lower, upper, facecolor=color, alpha=base_alpha / i))
    return (l, e)


Instruction:
Given the following Python 3 function, write the documentation
def summarize_sampler(sampler, weights=None, burn=0, ci=0.95, chain_mask=None):
    try:
        k = sampler.flatchain.shape[-1]
    except AttributeError:
        # Assumes array input is only case where there is no "flatchain" attribute.
        k = sampler.shape[-1]

    if isinstance(sampler, emcee.EnsembleSampler):
        if chain_mask is None:
            chain_mask = scipy.ones(sampler.chain.shape[0], dtype=bool)
        flat_trace = sampler.chain[chain_mask, burn:, :]
        flat_trace = flat_trace.reshape((-1, k))
    elif isinstance(sampler, emcee.PTSampler):
        if chain_mask is None:
            chain_mask = scipy.ones(sampler.nwalkers, dtype=bool)
        flat_trace = sampler.chain[temp_idx, chain_mask, burn:, :]
        flat_trace = flat_trace.reshape((-1, k))
    elif isinstance(sampler, scipy.ndarray):
        if sampler.ndim == 4:
            if chain_mask is None:
                chain_mask = scipy.ones(sampler.shape[1], dtype=bool)
            flat_trace = sampler[temp_idx, chain_mask, burn:, :]
            flat_trace = flat_trace.reshape((-1, k))
            if weights is not None:
                weights = weights[temp_idx, chain_mask, burn:]
                weights = weights.ravel()
        elif sampler.ndim == 3:
            if chain_mask is None:
                chain_mask = scipy.ones(sampler.shape[0], dtype=bool)
            flat_trace = sampler[chain_mask, burn:, :]
            flat_trace = flat_trace.reshape((-1, k))
            if weights is not None:
                weights = weights[chain_mask, burn:]
                weights = weights.ravel()
        elif sampler.ndim == 2:
            flat_trace = sampler[burn:, :]
            flat_trace = flat_trace.reshape((-1, k))
            if weights is not None:
                weights = weights[burn:]
                weights = weights.ravel()
    else:
        raise ValueError("Unknown sampler class: %s" % (type(sampler),))

    cibdry = 100.0 * (1.0 - ci) / 2.0
    if weights is None:
        mean = scipy.mean(flat_trace, axis=0)
        ci_l, ci_u = scipy.percentile(flat_trace, [cibdry, 100.0 - cibdry], axis=0)
    else:
        mean = weights.dot(flat_trace) / weights.sum()
        ci_l = scipy.zeros(k)
        ci_u = scipy.zeros(k)
        p = scipy.asarray([cibdry, 100.0 - cibdry])
        for i in range(0, k):
            srt = flat_trace[:, i].argsort()
            x = flat_trace[srt, i]
            w = weights[srt]
            Sn = w.cumsum()
            pn = 100.0 / Sn[-1] * (Sn - w / 2.0)
            j = scipy.digitize(p, pn) - 1
            ci_l[i], ci_u[i] = x[j] + (p - pn[j]) / (pn[j + 1] - pn[j]) * (x[j + 1] - x[j])

    return (mean, ci_l, ci_u)

Response: r Create summary statistics of the sampler.


Instruction:
Can you generate the documentation for the following Python 3 function
def plot_sampler(
        sampler, suptitle=None, labels=None, bins=50,
        plot_samples=False, plot_hist=True, plot_chains=True,
        burn=0, chain_mask=None, temp_idx=0, weights=None, cutoff_weight=None,
        cmap='gray_r', hist_color='k', chain_alpha=0.1,
        points=None, covs=None, colors=None, ci=[0.95],
        max_hist_ticks=None, max_chain_ticks=6,
        label_chain_y=False, hide_chain_yticklabels=False, chain_ytick_pad=2.0,
        label_fontsize=None, ticklabel_fontsize=None, chain_label_fontsize=None,
        chain_ticklabel_fontsize=None, xticklabel_angle=90.0,
        bottom_sep=0.075, suptitle_space=0.1, fixed_height=None,
        fixed_width=None, l=0.1, r=0.9, t1=None, b1=None, t2=0.2, b2=0.1,
        ax_space=0.1
    ):
    """Plot the results of MCMC sampler (posterior and chains).

    Loosely based on triangle.py. Provides extensive options to format the plot.

    Parameters
    ----------
    sampler : :py:class:`emcee.Sampler` instance or array, (`n_temps`, `n_chains`, `n_samp`, `n_dim`), (`n_chains`, `n_samp`, `n_dim`) or (`n_samp`, `n_dim`)
        The sampler to plot the chains/marginals of. Can also be an array of
        samples which matches the shape of the `chain` attribute that would be
        present in a :py:class:`emcee.Sampler` instance.
    suptitle : str, optional
        The figure title to place at the top. Default is no title.
    labels : list of str, optional
        The labels to use for each of the free parameters. Default is to leave
        the axes unlabeled.
    bins : int, optional
        Number of bins to use for the histograms. Default is 50.
    plot_samples : bool, optional
        If True, the samples are plotted as individual points. Default is False.
    plot_hist : bool, optional
        If True, histograms are plotted. Default is True.
    plot_chains : bool, optional
        If True, plot the sampler chains at the bottom. Default is True.
    burn : int, optional
        The number of samples to burn before making the marginal histograms.
        Default is zero (use all samples).
    chain_mask : (index) array, optional
        Mask identifying the chains to keep before plotting, in case there are
        bad chains. Default is to use all chains.
    temp_idx : int, optional
        Index of the temperature to plot when plotting a
        :py:class:`emcee.PTSampler`. Default is 0 (samples from the posterior).
    weights : array, (`n_temps`, `n_chains`, `n_samp`), (`n_chains`, `n_samp`) or (`n_samp`,), optional
        The weight for each sample. This is useful for post-processing the
        output from MultiNest sampling, for instance. Default is to not weight
        the samples.
    cutoff_weight : float, optional
        If `weights` and `cutoff_weight` are present, points with
        `weights < cutoff_weight * weights.max()` will be excluded. Default is
        to plot all points.
    cmap : str, optional
        The colormap to use for the histograms. Default is 'gray_r'.
    hist_color : str, optional
        The color to use for the univariate histograms. Default is 'k'.
    chain_alpha : float, optional
        The transparency to use for the plots of the individual chains. Setting
        this to something low lets you better visualize what is going on.
        Default is 0.1.
    points : array, (`D`,) or (`N`, `D`), optional
        Array of point(s) to plot onto each marginal and chain. Default is None.
    covs : array, (`D`, `D`) or (`N`, `D`, `D`), optional
        Covariance matrix or array of covariance matrices to plot onto each
        marginal. If you do not want to plot a covariance matrix for a specific
        point, set its corresponding entry to `None`. Default is to not plot
        confidence ellipses for any points.
    colors : array of str, (`N`,), optional
        The colors to use for the points in `points`. Default is to use the
        standard matplotlib RGBCMYK cycle.
    ci : array, (`num_ci`,), optional
        List of confidence intervals to plot for each non-`None` entry in `covs`.
        Default is 0.95 (just plot the 95 percent confidence interval).
    max_hist_ticks : int, optional
        The maximum number of ticks for the histogram plots. Default is None
        (no limit).
    max_chain_ticks : int, optional
        The maximum number of y-axis ticks for the chain plots. Default is 6.
    label_chain_y : bool, optional
        If True, the chain plots will have y axis labels. Default is False.
    hide_chain_yticklabels : bool, optional
        If True, hide the y axis tick labels for the chain plots. Default is
        False (show y tick labels).
    chain_ytick_pad : float, optional
        The padding (in points) between the y-axis tick labels and the axis for
        the chain plots. Default is 2.0.
    label_fontsize : float, optional
        The font size (in points) to use for the axis labels. Default is
        `axes.labelsize`.
    ticklabel_fontsize : float, optional
        The font size (in points) to use for the axis tick labels. Default is
        `xtick.labelsize`.
    chain_label_fontsize : float, optional
        The font size (in points) to use for the labels of the chain axes.
        Default is `axes.labelsize`.
    chain_ticklabel_fontsize : float, optional
        The font size (in points) to use for the chain axis tick labels. Default
        is `xtick.labelsize`.
    xticklabel_angle : float, optional
        The angle to rotate the x tick labels, in degrees. Default is 90.
    bottom_sep : float, optional
        The separation (in relative figure units) between the chains and the
        marginals. Default is 0.075.
    suptitle_space : float, optional
        The amount of space (in relative figure units) to leave for a figure
        title. Default is 0.1.
    fixed_height : float, optional
        The desired figure height (in inches). Default is to automatically
        adjust based on `fixed_width` to make the subplots square.
    fixed_width : float, optional
        The desired figure width (in inches). Default is `figure.figsize[0]`.
    l : float, optional
        The location (in relative figure units) of the left margin. Default is
        0.1.
    r : float, optional
        The location (in relative figure units) of the right margin. Default is
        0.9.
    t1 : float, optional
        The location (in relative figure units) of the top of the grid of
        histograms. Overrides `suptitle_space` if present.
    b1 : float, optional
        The location (in relative figure units) of the bottom of the grid of
        histograms. Overrides `bottom_sep` if present. Defaults to 0.1 if
        `plot_chains` is False.
    t2 : float, optional
        The location (in relative figure units) of the top of the grid of chain
        plots. Default is 0.2.
    b2 : float, optional
        The location (in relative figure units) of the bottom of the grid of
        chain plots. Default is 0.1.
    ax_space : float, optional
        The `w_space` and `h_space` to use (in relative figure units). Default
        is 0.1.
    """
    masked_weights = None
    if points is not None:
        points = scipy.atleast_2d(points)
        if covs is not None and len(covs) != len(points):
            raise ValueError(
                "If covariance matrices are provided, len(covs) must equal len(points)!"
            )
        elif covs is None:
            covs = [None,] * len(points)
        if colors is None:
            c_cycle = itertools.cycle(['b', 'g', 'r', 'c', 'm', 'y', 'k'])
            colors = [c_cycle.next() for p in points]
    # Create axes:
    try:
        k = sampler.flatchain.shape[-1]
    except AttributeError:
        # Assumes array input is only case where there is no "flatchain" attribute.
        k = sampler.shape[-1]

    if labels is None:
        labels = [''] * k

    # Set up geometry:
    # plot_chains =
    #  True:         False:
    # +-----------+ +-----------+
    # | +-------+ | | +-------+ |
    # | |       | | | |       | |
    # | |       | | | |       | |
    # | |       | | | |       | |
    # | +-------+ | | +-------+ |
    # | +-------+ | +-----------+
    # | |       | |
    # | +-------+ |
    # +-----------+

    # We retain support for the original suptitle_space keyword, but can
    # override with t1 as needed:
    if t1 is None:
        t1 = 1 - suptitle_space

    # We retain support for the original bottom_sep keyword, but can override
    # with b1 as needed:
    if b1 is None:
        if plot_chains:
            b1 = t2 + bottom_sep
        else:
            b1 = 0.1

    if fixed_height is None and fixed_width is None:
        # Default: use matplotlib's default width, handle remaining parameters
        # with the fixed width case below:
        fixed_width = matplotlib.rcParams['figure.figsize'][0]

    if fixed_height is None and fixed_width is not None:
        # Only width specified, compute height to yield square histograms:
        fixed_height = fixed_width * (r - l) / (t1 - b1)
    elif fixed_height is not None and fixed_width is None:
        # Only height specified, compute width to yield square histograms
        fixed_width = fixed_height * (t1 - b1) / (r - l)
    # Otherwise width and height are fixed, and we may not have square
    # histograms, at the user's discretion.

    wspace = ax_space
    hspace = ax_space

    # gs1 is the histograms, gs2 is the chains:
    f = plt.figure(figsize=(fixed_width, fixed_height))
    gs1 = mplgs.GridSpec(k, k)
    gs1.update(bottom=b1, top=t1, left=l, right=r, wspace=wspace, hspace=hspace)
    if plot_chains:
        gs2 = mplgs.GridSpec(1, k)
        gs2.update(bottom=b2, top=t2, left=l, right=r, wspace=wspace, hspace=hspace)
    axes = []
    # j is the row, i is the column.
    for j in xrange(0, k + int(plot_chains)):
        row = []
        for i in xrange(0, k):
            if i > j:
                row.append(None)
            else:
                sharey = row[-1] if i > 0 and i < j and j < k else None
                sharex = axes[-1][i] if j > i and j < k else \
                    (row[-1] if i > 0 and j == k else None)
                gs = gs1[j, i] if j < k else gs2[:, i]
                row.append(f.add_subplot(gs, sharey=sharey, sharex=sharex))
                if j < k and ticklabel_fontsize is not None:
                    row[-1].tick_params(labelsize=ticklabel_fontsize)
                elif j >= k and chain_ticklabel_fontsize is not None:
                    row[-1].tick_params(labelsize=chain_ticklabel_fontsize)
        axes.append(row)
    axes = scipy.asarray(axes)

    # Update axes with the data:
    if isinstance(sampler, emcee.EnsembleSampler):
        if chain_mask is None:
            chain_mask = scipy.ones(sampler.chain.shape[0], dtype=bool)
        flat_trace = sampler.chain[chain_mask, burn:, :]
        flat_trace = flat_trace.reshape((-1, k))
    elif isinstance(sampler, emcee.PTSampler):
        if chain_mask is None:
            chain_mask = scipy.ones(sampler.nwalkers, dtype=bool)
        flat_trace = sampler.chain[temp_idx, chain_mask, burn:, :]
        flat_trace = flat_trace.reshape((-1, k))
    elif isinstance(sampler, scipy.ndarray):
        if sampler.ndim == 4:
            if chain_mask is None:
                chain_mask = scipy.ones(sampler.shape[1], dtype=bool)
            flat_trace = sampler[temp_idx, chain_mask, burn:, :]
            flat_trace = flat_trace.reshape((-1, k))
            if weights is not None:
                weights = weights[temp_idx, chain_mask, burn:]
                weights = weights.ravel()
        elif sampler.ndim == 3:
            if chain_mask is None:
                chain_mask = scipy.ones(sampler.shape[0], dtype=bool)
            flat_trace = sampler[chain_mask, burn:, :]
            flat_trace = flat_trace.reshape((-1, k))
            if weights is not None:
                weights = weights[chain_mask, burn:]
                weights = weights.ravel()
        elif sampler.ndim == 2:
            flat_trace = sampler[burn:, :]
            flat_trace = flat_trace.reshape((-1, k))
            if weights is not None:
                weights = weights[burn:]
                weights = weights.ravel()
        if cutoff_weight is not None and weights is not None:
            mask = weights >= cutoff_weight * weights.max()
            flat_trace = flat_trace[mask, :]
            masked_weights = weights[mask]
        else:
            masked_weights = weights
    else:
        raise ValueError("Unknown sampler class: %s" % (type(sampler),))

    # j is the row, i is the column.
    for i in xrange(0, k):
        axes[i, i].clear()
        if plot_hist:
            axes[i, i].hist(flat_trace[:, i], bins=bins, color=hist_color, weights=masked_weights, normed=True, histtype='stepfilled')
        if plot_samples:
            axes[i, i].plot(flat_trace[:, i], scipy.zeros_like(flat_trace[:, i]), ',', alpha=0.1)
        if points is not None:
            # axvline can only take a scalar x, so we have to loop:
            for p, c, cov in zip(points, colors, covs):
                axes[i, i].axvline(x=p[i], linewidth=3, color=c)
                if cov is not None:
                    xlim = axes[i, i].get_xlim()
                    i_grid = scipy.linspace(xlim[0], xlim[1], 100)
                    axes[i, i].plot(
                        i_grid,
                        scipy.stats.norm.pdf(
                            i_grid,
                            loc=p[i],
                            scale=scipy.sqrt(cov[i, i])
                        ),
                        c,
                        linewidth=3.0
                    )
                    axes[i, i].set_xlim(xlim)
        if i == k - 1:
            axes[i, i].set_xlabel(labels[i], fontsize=label_fontsize)
            plt.setp(axes[i, i].xaxis.get_majorticklabels(), rotation=xticklabel_angle)
        if i < k - 1:
            plt.setp(axes[i, i].get_xticklabels(), visible=False)
        plt.setp(axes[i, i].get_yticklabels(), visible=False)
        for j in xrange(i + 1, k):
            axes[j, i].clear()
            if plot_hist:
                ct, x, y, im = axes[j, i].hist2d(
                    flat_trace[:, i],
                    flat_trace[:, j],
                    bins=bins,
                    cmap=cmap,
                    weights=masked_weights
                )
            if plot_samples:
                axes[j, i].plot(flat_trace[:, i], flat_trace[:, j], ',', alpha=0.1)
            if points is not None:
                for p, c, cov in zip(points, colors, covs):
                    axes[j, i].plot(p[i], p[j], 'o', color=c)
                    if cov is not None:
                        Sigma = scipy.asarray([[cov[i, i], cov[i, j]], [cov[j, i], cov[j, j]]], dtype=float)
                        lam, v = scipy.linalg.eigh(Sigma)
                        chi2 = [-scipy.log(1.0 - cival) * 2.0 for cival in ci]
                        a = [2.0 * scipy.sqrt(chi2val * lam[-1]) for chi2val in chi2]
                        b = [2.0 * scipy.sqrt(chi2val * lam[-2]) for chi2val in chi2]
                        ang = scipy.arctan2(v[1, -1], v[0, -1])
                        for aval, bval in zip(a, b):
                            ell = mplp.Ellipse(
                                [p[i], p[j]],
                                aval,
                                bval,
                                angle=scipy.degrees(ang),
                                facecolor='none',
                                edgecolor=c,
                                linewidth=3
                            )
                            axes[j, i].add_artist(ell)
                # axes[j, i].plot(points[i], points[j], 'o')
            # xmid = 0.5 * (x[1:] + x[:-1])
            # ymid = 0.5 * (y[1:] + y[:-1])
            # axes[j, i].contour(xmid, ymid, ct.T, colors='k')
            if j < k - 1:
                plt.setp(axes[j, i].get_xticklabels(), visible=False)
            if i != 0:
                plt.setp(axes[j, i].get_yticklabels(), visible=False)
            if i == 0:
                axes[j, i].set_ylabel(labels[j], fontsize=label_fontsize)
            if j == k - 1:
                axes[j, i].set_xlabel(labels[i], fontsize=label_fontsize)
                plt.setp(axes[j, i].xaxis.get_majorticklabels(), rotation=xticklabel_angle)
        if plot_chains:
            axes[-1, i].clear()
            if isinstance(sampler, emcee.EnsembleSampler):
                axes[-1, i].plot(sampler.chain[:, :, i].T, alpha=chain_alpha)
            elif isinstance(sampler, emcee.PTSampler):
                axes[-1, i].plot(sampler.chain[temp_idx, :, :, i].T, alpha=chain_alpha)
            else:
                if sampler.ndim == 4:
                    axes[-1, i].plot(sampler[temp_idx, :, :, i].T, alpha=chain_alpha)
                elif sampler.ndim == 3:
                    axes[-1, i].plot(sampler[:, :, i].T, alpha=chain_alpha)
                elif sampler.ndim == 2:
                    axes[-1, i].plot(sampler[:, i].T, alpha=chain_alpha)
            # Plot the weights on top of the chains:
            if weights is not None:
                a_wt = axes[-1, i].twinx()
                a_wt.plot(weights, alpha=chain_alpha, linestyle='--', color='r')
                plt.setp(a_wt.yaxis.get_majorticklabels(), visible=False)
                a_wt.yaxis.set_ticks_position('none')
                # Plot the cutoff weight as a horizontal line and the first sample
                # which is included as a vertical bar. Note that this won't be quite
                # the right behavior if the weights are not roughly monotonic.
                if cutoff_weight is not None:
                    a_wt.axhline(cutoff_weight * weights.max(), linestyle='-', color='r')
                    wi, = scipy.where(weights >= cutoff_weight * weights.max())
                    a_wt.axvline(wi[0], linestyle='-', color='r')
            if burn > 0:
                axes[-1, i].axvline(burn, color='r', linewidth=3)
            if points is not None:
                for p, c in zip(points, colors):
                    axes[-1, i].axhline(y=p[i], linewidth=3, color=c)
                # Reset the xlim since it seems to get messed up:
                axes[-1, i].set_xlim(left=0)
                # try:
                #     [axes[-1, i].axhline(y=pt, linewidth=3) for pt in points[i]]
                # except TypeError:
                #     axes[-1, i].axhline(y=points[i], linewidth=3)
            if label_chain_y:
                axes[-1, i].set_ylabel(labels[i], fontsize=chain_label_fontsize)
            axes[-1, i].set_xlabel('step', fontsize=chain_label_fontsize)
            plt.setp(axes[-1, i].xaxis.get_majorticklabels(), rotation=xticklabel_angle)
            for tick in axes[-1, i].get_yaxis().get_major_ticks():
                tick.set_pad(chain_ytick_pad)
                tick.label1 = tick._get_text1()

    for i in xrange(0, k):
        if max_hist_ticks is not None:
            axes[k - 1, i].xaxis.set_major_locator(plt.MaxNLocator(nbins=max_hist_ticks - 1))
            axes[i, 0].yaxis.set_major_locator(plt.MaxNLocator(nbins=max_hist_ticks - 1))
        if plot_chains and max_chain_ticks is not None:
            axes[k, i].yaxis.set_major_locator(plt.MaxNLocator(nbins=max_chain_ticks - 1))
            axes[k, i].xaxis.set_major_locator(plt.MaxNLocator(nbins=max_chain_ticks - 1))
        if plot_chains and hide_chain_yticklabels:
            plt.setp(axes[k, i].get_yticklabels(), visible=False)

    if suptitle is not None:
        f.suptitle(suptitle)
    f.canvas.draw()
    return f

Response: Plots the results of the sampler.


Instruction:
Can you create a Python 3 function that
makes a plot of the sampler's "fingerprint": univariate marginal histograms for all hyperparameters. The hyperparameters are mapped to [0, 1] using :py:meth:`hyperprior.elementwise_cdf`, so this can only be used with prior distributions which implement this function. Returns the figure and axis created. Parameters ---------- sampler : :py:class:`emcee.Sampler` instance or array, (`n_temps`, `n_chains`, `n_samp`, `n_dim`), (`n_chains`, `n_samp`, `n_dim`) or (`n_samp`, `n_dim`) The sampler to plot the chains/marginals of. Can also be an array of samples which matches the shape of the `chain` attribute that would be present in a :py:class:`emcee.Sampler` instance. hyperprior : :py:class:`~gptools.utils.JointPrior` instance The joint prior distribution for the hyperparameters. Used to map the values to [0, 1] so that the hyperparameters can all be shown on the same axis. weights : array, (`n_temps`, `n_chains`, `n_samp`), (`n_chains`, `n_samp`) or (`n_samp`,), optional The weight for each sample. This is useful for post-processing the output from MultiNest sampling, for instance. cutoff_weight : float, optional If `weights` and `cutoff_weight` are present, points with `weights < cutoff_weight * weights.max()` will be excluded. Default is to plot all points. nbins : int or array of int, (`D`,), optional The number of bins dividing [0, 1] to use for each histogram. If a single int is given, this is used for all of the hyperparameters. If an array of ints is given, these are the numbers of bins for each of the hyperparameters. The default is to determine the number of bins using the Freedman-Diaconis rule. labels : array of str, (`D`,), optional The labels for each hyperparameter. Default is to use empty strings. burn : int, optional The number of samples to burn before making the marginal histograms. Default is zero (use all samples). chain_mask : (index) array, optional Mask identifying the chains to keep before plotting, in case there are bad chains. Default is to use all chains. temp_idx : int, optional Index of the temperature to plot when plotting a :py:class:`emcee.PTSampler`. Default is 0 (samples from the posterior). points : array, (`D`,) or (`N`, `D`), optional Array of point(s) to plot as horizontal lines. Default is None. plot_samples : bool, optional If True, the samples are plotted as horizontal lines. Default is False. sample_color : str, optional The color to plot the samples in. Default is 'k', meaning black. point_color : str or list of str, optional The color to plot the individual points in. Default is to loop through matplotlib's default color sequence. If a list is provided, it will be cycled through. point_lw : float, optional Line width to use when plotting the individual points. title : str, optional Title to use for the plot. rot_x_labels : bool, optional If True, the labels for the x-axis are rotated 90 degrees. Default is False (do not rotate labels). figsize : 2-tuple, optional The figure size to use. Default is to use the matplotlib default.

Response:
def plot_sampler_fingerprint(
        sampler, hyperprior, weights=None, cutoff_weight=None, nbins=None,
        labels=None, burn=0, chain_mask=None, temp_idx=0, points=None,
        plot_samples=False, sample_color='k', point_color=None, point_lw=3,
        title='', rot_x_labels=False, figsize=None
    ):
    """Make a plot of the sampler's "fingerprint": univariate marginal histograms for all hyperparameters.

    The hyperparameters are mapped to [0, 1] using
    :py:meth:`hyperprior.elementwise_cdf`, so this can only be used with prior
    distributions which implement this function.

    Returns the figure and axis created.

    Parameters
    ----------
    sampler : :py:class:`emcee.Sampler` instance or array, (`n_temps`, `n_chains`, `n_samp`, `n_dim`), (`n_chains`, `n_samp`, `n_dim`) or (`n_samp`, `n_dim`)
        The sampler to plot the chains/marginals of. Can also be an array of
        samples which matches the shape of the `chain` attribute that would be
        present in a :py:class:`emcee.Sampler` instance.
    hyperprior : :py:class:`~gptools.utils.JointPrior` instance
        The joint prior distribution for the hyperparameters. Used to map the
        values to [0, 1] so that the hyperparameters can all be shown on the
        same axis.
    weights : array, (`n_temps`, `n_chains`, `n_samp`), (`n_chains`, `n_samp`) or (`n_samp`,), optional
        The weight for each sample. This is useful for post-processing the
        output from MultiNest sampling, for instance.
    cutoff_weight : float, optional
        If `weights` and `cutoff_weight` are present, points with
        `weights < cutoff_weight * weights.max()` will be excluded. Default is
        to plot all points.
    nbins : int or array of int, (`D`,), optional
        The number of bins dividing [0, 1] to use for each histogram. If a
        single int is given, this is used for all of the hyperparameters. If an
        array of ints is given, these are the numbers of bins for each of the
        hyperparameters. The default is to determine the number of bins using
        the Freedman-Diaconis rule.
    labels : array of str, (`D`,), optional
        The labels for each hyperparameter. Default is to use empty strings.
    burn : int, optional
        The number of samples to burn before making the marginal histograms.
        Default is zero (use all samples).
    chain_mask : (index) array, optional
        Mask identifying the chains to keep before plotting, in case there are
        bad chains. Default is to use all chains.
    temp_idx : int, optional
        Index of the temperature to plot when plotting a
        :py:class:`emcee.PTSampler`. Default is 0 (samples from the posterior).
    points : array, (`D`,) or (`N`, `D`), optional
        Array of point(s) to plot as horizontal lines. Default is None.
    plot_samples : bool, optional
        If True, the samples are plotted as horizontal lines. Default is False.
    sample_color : str, optional
        The color to plot the samples in. Default is 'k', meaning black.
    point_color : str or list of str, optional
        The color to plot the individual points in. Default is to loop through
        matplotlib's default color sequence. If a list is provided, it will be
        cycled through.
    point_lw : float, optional
        Line width to use when plotting the individual points.
    title : str, optional
        Title to use for the plot.
    rot_x_labels : bool, optional
        If True, the labels for the x-axis are rotated 90 degrees. Default is
        False (do not rotate labels).
    figsize : 2-tuple, optional
        The figure size to use. Default is to use the matplotlib default.
    """
    try:
        k = sampler.flatchain.shape[-1]
    except AttributeError:
        # Assumes array input is only case where there is no "flatchain" attribute.
        k = sampler.shape[-1]
    # Process the samples:
    if isinstance(sampler, emcee.EnsembleSampler):
        if chain_mask is None:
            chain_mask = scipy.ones(sampler.chain.shape[0], dtype=bool)
        flat_trace = sampler.chain[chain_mask, burn:, :]
        flat_trace = flat_trace.reshape((-1, k))
    elif isinstance(sampler, emcee.PTSampler):
        if chain_mask is None:
            chain_mask = scipy.ones(sampler.nwalkers, dtype=bool)
        flat_trace = sampler.chain[temp_idx, chain_mask, burn:, :]
        flat_trace = flat_trace.reshape((-1, k))
    elif isinstance(sampler, scipy.ndarray):
        if sampler.ndim == 4:
            if chain_mask is None:
                chain_mask = scipy.ones(sampler.shape[1], dtype=bool)
            flat_trace = sampler[temp_idx, chain_mask, burn:, :]
            flat_trace = flat_trace.reshape((-1, k))
            if weights is not None:
                weights = weights[temp_idx, chain_mask, burn:]
                weights = weights.ravel()
        elif sampler.ndim == 3:
            if chain_mask is None:
                chain_mask = scipy.ones(sampler.shape[0], dtype=bool)
            flat_trace = sampler[chain_mask, burn:, :]
            flat_trace = flat_trace.reshape((-1, k))
            if weights is not None:
                weights = weights[chain_mask, burn:]
                weights = weights.ravel()
        elif sampler.ndim == 2:
            flat_trace = sampler[burn:, :]
            flat_trace = flat_trace.reshape((-1, k))
            if weights is not None:
                weights = weights[burn:]
                weights = weights.ravel()
        if cutoff_weight is not None and weights is not None:
            mask = weights >= cutoff_weight * weights.max()
            flat_trace = flat_trace[mask, :]
            weights = weights[mask]
    else:
        raise ValueError("Unknown sampler class: %s" % (type(sampler),))

    if labels is None:
        labels = [''] * k

    u = scipy.asarray([hyperprior.elementwise_cdf(p) for p in flat_trace], dtype=float).T
    if nbins is None:
        lq, uq = scipy.stats.scoreatpercentile(u, [25, 75], axis=1)
        h = 2.0 * (uq - lq) / u.shape[0]**(1.0 / 3.0)
        n = scipy.asarray(scipy.ceil(1.0 / h), dtype=int)
    else:
        try:
            iter(nbins)
            n = nbins
        except TypeError:
            n = nbins * scipy.ones(u.shape[0])

    hist = [scipy.stats.histogram(uv, numbins=nv, defaultlimits=[0, 1], weights=weights) for uv, nv in zip(u, n)]
    max_ct = max([max(h.count) for h in hist])
    min_ct = min([min(h.count) for h in hist])

    f = plt.figure(figsize=figsize)
    a = f.add_subplot(1, 1, 1)
    for i, (h, pn) in enumerate(zip(hist, labels)):
        a.imshow(
            scipy.atleast_2d(scipy.asarray(h.count[::-1], dtype=float)).T,
            cmap='gray_r',
            interpolation='nearest',
            vmin=min_ct,
            vmax=max_ct,
            extent=(i, i + 1, 0, 1),
            aspect='auto'
        )

    if plot_samples:
        for p in u:
            for i, uv in enumerate(p):
                a.plot([i, i + 1], [uv, uv], sample_color, alpha=0.1)

    if points is not None:
        points = scipy.atleast_2d(scipy.asarray(points, dtype=float))
        u_points = [hyperprior.elementwise_cdf(p) for p in points]
        if point_color is None:
            c_cycle = itertools.cycle(['b', 'g', 'r', 'c', 'm', 'y', 'k'])
        else:
            c_cycle = itertools.cycle(scipy.atleast_1d(point_color))
        for p in u_points:
            c = c_cycle.next()
            for i, uv in enumerate(p):
                a.plot([i, i + 1], [uv, uv], color=c, lw=point_lw)

    a.set_xlim(0, len(hist))
    a.set_ylim(0, 1)
    a.set_xticks(0.5 + scipy.arange(0, len(hist), dtype=float))
    a.set_xticklabels(labels)
    if rot_x_labels:
        plt.setp(a.xaxis.get_majorticklabels(), rotation=90)
    a.set_xlabel("parameter")
    a.set_ylabel("$u=F_P(p)$")
    a.set_title(title)

    return f, a


Instruction:
Can you create a Python 3 function that
makes a plot of the sampler's correlation or covariance matrix. Returns the figure and axis created. Parameters ---------- sampler : :py:class:`emcee.Sampler` instance or array, (`n_temps`, `n_chains`, `n_samp`, `n_dim`), (`n_chains`, `n_samp`, `n_dim`) or (`n_samp`, `n_dim`) The sampler to plot the chains/marginals of. Can also be an array of samples which matches the shape of the `chain` attribute that would be present in a :py:class:`emcee.Sampler` instance. method : {'corr', 'cov'} Whether to plot the correlation matrix ('corr') or the covariance matrix ('cov'). The covariance matrix is often not useful because different parameters have wildly different scales. Default is to plot the correlation matrix. labels : array of str, (`D`,), optional The labels for each hyperparameter. Default is to use empty strings. burn : int, optional The number of samples to burn before making the marginal histograms. Default is zero (use all samples). chain_mask : (index) array, optional Mask identifying the chains to keep before plotting, in case there are bad chains. Default is to use all chains. temp_idx : int, optional Index of the temperature to plot when plotting a :py:class:`emcee.PTSampler`. Default is 0 (samples from the posterior). cbar_label : str, optional The label to use for the colorbar. The default is chosen based on the value of the `method` keyword. title : str, optional Title to use for the plot. rot_x_labels : bool, optional If True, the labels for the x-axis are rotated 90 degrees. Default is False (do not rotate labels). figsize : 2-tuple, optional The figure size to use. Default is to use the matplotlib default. xlabel_on_top : bool, optional If True, the x-axis labels are put on top (the way mathematicians present matrices). Default is True.

Response:
def plot_sampler_cov(
        sampler, method='corr', weights=None, cutoff_weight=None, labels=None,
        burn=0, chain_mask=None, temp_idx=0, cbar_label=None, title='',
        rot_x_labels=False, figsize=None, xlabel_on_top=True
    ):
    """Make a plot of the sampler's correlation or covariance matrix.

    Returns the figure and axis created.

    Parameters
    ----------
    sampler : :py:class:`emcee.Sampler` instance or array, (`n_temps`, `n_chains`, `n_samp`, `n_dim`), (`n_chains`, `n_samp`, `n_dim`) or (`n_samp`, `n_dim`)
        The sampler to plot the chains/marginals of. Can also be an array of
        samples which matches the shape of the `chain` attribute that would be
        present in a :py:class:`emcee.Sampler` instance.
    method : {'corr', 'cov'}
        Whether to plot the correlation matrix ('corr') or the covariance matrix
        ('cov'). The covariance matrix is often not useful because different
        parameters have wildly different scales. Default is to plot the
        correlation matrix.
    labels : array of str, (`D`,), optional
        The labels for each hyperparameter. Default is to use empty strings.
    burn : int, optional
        The number of samples to burn before making the marginal histograms.
        Default is zero (use all samples).
    chain_mask : (index) array, optional
        Mask identifying the chains to keep before plotting, in case there are
        bad chains. Default is to use all chains.
    temp_idx : int, optional
        Index of the temperature to plot when plotting a
        :py:class:`emcee.PTSampler`. Default is 0 (samples from the posterior).
    cbar_label : str, optional
        The label to use for the colorbar. The default is chosen based on the
        value of the `method` keyword.
    title : str, optional
        Title to use for the plot.
    rot_x_labels : bool, optional
        If True, the labels for the x-axis are rotated 90 degrees. Default is
        False (do not rotate labels).
    figsize : 2-tuple, optional
        The figure size to use. Default is to use the matplotlib default.
    xlabel_on_top : bool, optional
        If True, the x-axis labels are put on top (the way mathematicians
        present matrices). Default is True.
    """
    try:
        k = sampler.flatchain.shape[-1]
    except AttributeError:
        # Assumes array input is only case where there is no "flatchain" attribute.
        k = sampler.shape[-1]
    # Process the samples:
    if isinstance(sampler, emcee.EnsembleSampler):
        if chain_mask is None:
            chain_mask = scipy.ones(sampler.chain.shape[0], dtype=bool)
        flat_trace = sampler.chain[chain_mask, burn:, :]
        flat_trace = flat_trace.reshape((-1, k))
    elif isinstance(sampler, emcee.PTSampler):
        if chain_mask is None:
            chain_mask = scipy.ones(sampler.nwalkers, dtype=bool)
        flat_trace = sampler.chain[temp_idx, chain_mask, burn:, :]
        flat_trace = flat_trace.reshape((-1, k))
    elif isinstance(sampler, scipy.ndarray):
        if sampler.ndim == 4:
            if chain_mask is None:
                chain_mask = scipy.ones(sampler.shape[1], dtype=bool)
            flat_trace = sampler[temp_idx, chain_mask, burn:, :]
            flat_trace = flat_trace.reshape((-1, k))
            if weights is not None:
                weights = weights[temp_idx, chain_mask, burn:]
                weights = weights.ravel()
        elif sampler.ndim == 3:
            if chain_mask is None:
                chain_mask = scipy.ones(sampler.shape[0], dtype=bool)
            flat_trace = sampler[chain_mask, burn:, :]
            flat_trace = flat_trace.reshape((-1, k))
            if weights is not None:
                weights = weights[chain_mask, burn:]
                weights = weights.ravel()
        elif sampler.ndim == 2:
            flat_trace = sampler[burn:, :]
            flat_trace = flat_trace.reshape((-1, k))
            if weights is not None:
                weights = weights[burn:]
                weights = weights.ravel()
        if cutoff_weight is not None and weights is not None:
            mask = weights >= cutoff_weight * weights.max()
            flat_trace = flat_trace[mask, :]
            weights = weights[mask]
    else:
        raise ValueError("Unknown sampler class: %s" % (type(sampler),))

    if labels is None:
        labels = [''] * k

    if cbar_label is None:
        cbar_label = r'$\mathrm{cov}(p_1, p_2)$' if method == 'cov' else r'$\mathrm{corr}(p_1, p_2)$'

    if weights is None:
        if method == 'corr':
            cov = scipy.corrcoef(flat_trace, rowvar=0, ddof=1)
        else:
            cov = scipy.cov(flat_trace, rowvar=0, ddof=1)
    else:
        cov = scipy.cov(flat_trace, rowvar=0, aweights=weights)
        if method == 'corr':
            stds = scipy.sqrt(scipy.diag(cov))
            STD_1, STD_2 = scipy.meshgrid(stds, stds)
            cov = cov / (STD_1 * STD_2)

    f_cov = plt.figure(figsize=figsize)
    a_cov = f_cov.add_subplot(1, 1, 1)
    a_cov.set_title(title)
    if method == 'cov':
        vmax = scipy.absolute(cov).max()
    else:
        vmax = 1.0
    cax = a_cov.pcolor(cov, cmap='seismic', vmin=-1 * vmax, vmax=vmax)
    divider = make_axes_locatable(a_cov)
    a_cb = divider.append_axes("right", size="10%", pad=0.05)
    cbar = f_cov.colorbar(cax, cax=a_cb, label=cbar_label)
    a_cov.set_xlabel('parameter')
    a_cov.set_ylabel('parameter')
    a_cov.axis('square')
    a_cov.invert_yaxis()
    if xlabel_on_top:
        a_cov.xaxis.tick_top()
        a_cov.xaxis.set_label_position('top')
    a_cov.set_xticks(0.5 + scipy.arange(0, flat_trace.shape[1], dtype=float))
    a_cov.set_yticks(0.5 + scipy.arange(0, flat_trace.shape[1], dtype=float))
    a_cov.set_xticklabels(labels)
    if rot_x_labels:
        plt.setp(a_cov.xaxis.get_majorticklabels(), rotation=90)
    a_cov.set_yticklabels(labels)
    a_cov.set_xlim(0, flat_trace.shape[1])
    a_cov.set_ylim(flat_trace.shape[1], 0)

    return f_cov, a_cov


Instruction:
Can you implement a function in Python 3 that
converts a sample to random variates uniform on : math : [ 0 1 )

Response:
def elementwise_cdf(self, p):
        r"""Convert a sample to random variates uniform on :math:`[0, 1]`.

        For a univariate distribution, this is simply evaluating the CDF. To
        facilitate efficient sampling, this function returns a *vector* of CDF
        values, one value for each variable. Basically, the idea is that, given
        a vector :math:`q` of `num_params` values each of which is distributed
        according to the prior, this function will return variables uniform on
        :math:`[0, 1]` corresponding to each variable. This is the inverse
        operation to :py:meth:`sample_u`.

        Parameters
        ----------
        p : array-like, (`num_params`,)
            Values to evaluate CDF at.
        """
        p1_num_params = len(self.p1.bounds)
        return scipy.concatenate(
            (
                self.p1.elementwise_cdf(p[:p1_num_params]),
                self.p2.elementwise_cdf(p[p1_num_params:])
            )
        )


Instruction:
Create a Python 3 function to
draw random samples of the hyperparameters.

Response:
def random_draw(self, size=None):
        """Draw random samples of the hyperparameters.

        The outputs of the two priors are stacked vertically.

        Parameters
        ----------
        size : None, int or array-like, optional
            The number/shape of samples to draw. If None, only one sample is
            returned. Default is None.
        """
        draw_1 = self.p1.random_draw(size=size)
        draw_2 = self.p2.random_draw(size=size)

        if draw_1.ndim == 1:
            return scipy.hstack((draw_1, draw_2))
        else:
            return scipy.vstack((draw_1, draw_2))


Instruction:
Implement a function in Python 3 to
convert a sample to random variates uniform on the CDF uniform on the CDF.

Response:
def elementwise_cdf(self, p):
        r"""Convert a sample to random variates uniform on :math:`[0, 1]`.

        For a univariate distribution, this is simply evaluating the CDF. To
        facilitate efficient sampling, this function returns a *vector* of CDF
        values, one value for each variable. Basically, the idea is that, given
        a vector :math:`q` of `num_params` values each of which is distributed
        according to the prior, this function will return variables uniform on
        :math:`[0, 1]` corresponding to each variable. This is the inverse
        operation to :py:meth:`sample_u`.

        Parameters
        ----------
        p : array-like, (`num_params`,)
            Values to evaluate CDF at.
        """
        p = scipy.atleast_1d(p)
        if len(p) != len(self.bounds):
            raise ValueError("length of p must equal the number of parameters!")
        if p.ndim != 1:
            raise ValueError("p must be one-dimensional!")
        c = scipy.zeros(len(self.bounds))
        for k in xrange(0, len(self.bounds)):
            if p[k] <= self.bounds[k][0]:
                c[k] = 0.0
            elif p[k] >= self.bounds[k][1]:
                c[k] = 1.0
            else:
                c[k] = (p[k] - self.bounds[k][0]) / (self.bounds[k][1] - self.bounds[k][0])
        return c


Instruction:
Write a Python 3 function that can
draw random samples of the hyperparameters.

Response:
def random_draw(self, size=None):
        """Draw random samples of the hyperparameters.

        Parameters
        ----------
        size : None, int or array-like, optional
            The number/shape of samples to draw. If None, only one sample is
            returned. Default is None.
        """
        return scipy.asarray([numpy.random.uniform(low=b[0], high=b[1], size=size) for b in self.bounds])