Instruction:
Write a Python 3 script to
rank operation on Hermitian-packed matrix.

Response:
def cublasChpr2(handle, uplo, n, alpha, x, inx, y, incy, AP):
    """
    Rank-2 operation on Hermitian-packed matrix.

    """

    status = _libcublas.cublasChpr2_v2(handle,
                                       _CUBLAS_FILL_MODE[uplo],
                                       n, ctypes.byref(cuda.cuFloatComplex(alpha.real,
                                                                           alpha.imag)),
                                       int(x), incx, int(y), incy, int(AP))
    cublasCheckStatus(status)


Instruction:
Can you generate the documentation for the following Python 3 function
def cublasZhpr2(handle, uplo, n, alpha, x, inx, y, incy, AP):

    status = _libcublas.cublasZhpr2_v2(handle,
                                       _CUBLAS_FILL_MODE[uplo],
                                       n, ctypes.byref(cuda.cuDoubleComplex(alpha.real,
                                                                            alpha.imag)),
                                       int(x), incx, int(y), incy, int(AP))
    cublasCheckStatus(status)

Response: Matrix - matrix product for Hermitian - packed matrix.


Instruction:
Can you generate the documentation for the following Python 3 function
def cublasZgemm(handle, transa, transb, m, n, k, alpha, A, lda, B, ldb, beta, C, ldc):

    status = _libcublas.cublasZgemm_v2(handle,
                                       _CUBLAS_OP[transa],
                                       _CUBLAS_OP[transb], m, n, k,
                                       ctypes.byref(cuda.cuDoubleComplex(alpha.real,
                                                                         alpha.imag)),
                                       int(A), lda, int(B), ldb,
                                       ctypes.byref(cuda.cuDoubleComplex(beta.real,
                                                                         beta.imag)),
                                       int(C), ldc)
    cublasCheckStatus(status)

Response: Matrix - matrix product for complex general matrix.


Instruction:
How would you explain what the following Python 3 function does
def cublasCsymm(handle, side, uplo, m, n, alpha, A, lda, B, ldb, beta, C, ldc):

    status = _libcublas.cublasCsymm_v2(handle,
                                       _CUBLAS_SIDE_MODE[side],
                                       _CUBLAS_FILL_MODE[uplo],
                                       m, n, ctypes.byref(cuda.cuFloatComplex(alpha.real,
                                                                              alpha.imag)),
                                       int(A), lda, int(B), ldb,
                                       ctypes.byref(cuda.cuFloatComplex(beta.real,
                                                                        beta.imag)),
                                       int(C), ldc)
    cublasCheckStatus(status)

Response: Matrix - matrix product for complex symmetric matrix.


Instruction:
Given the following Python 3 function, write the documentation
def cublasDsyrk(handle, uplo, trans, n, k, alpha, A, lda, beta, C, ldc):

    status = _libcublas.cublasDsyrk_v2(handle,
                                       _CUBLAS_FILL_MODE[uplo],
                                       _CUBLAS_OP[trans],
                                       n, k, ctypes.byref(cuda.cuFloatComplex(alpha.real,
                                                                              alpha.imag)),
                                       int(A), lda,
                                       ctypes.byref(cuda.cuFloatComplex(beta.real,
                                                                        beta.imag)),
                                       int(C), ldc)
    cublasCheckStatus(status)

Response: Matrix - k operation on real symmetric matrix.


Instruction:
Make a summary of the following Python 3 code
def cublasStrmm(handle, side, uplo, trans, diag, m, n, alpha, A, lda, B, ldb, C, ldc):

    status = _libcublas.cublasStrmm_v2(handle,
                                       _CUBLAS_SIDE_MODE[side],
                                       _CUBLAS_FILL_MODE[uplo],
                                       _CUBLAS_OP[trans],
                                       _CUBLAS_DIAG[diag],
                                       m, n, ctypes.byref(ctypes.c_float(alpha)),
                                       int(A), lda, int(B), ldb, int(C), ldc)
    cublasCheckStatus(status)

Response: Matrix - matrix product for real triangular matrix.


Instruction:
Can you tell what is the following Python 3 function doing
def cublasZtrmm(handle, side, uplo, trans, diag, m, n, alpha, A, lda, B, ldb, C, ldc):

    status = _libcublas.cublasZtrmm_v2(handle,
                                       _CUBLAS_SIDE_MODE[side],
                                       _CUBLAS_FILL_MODE[uplo],
                                       _CUBLAS_OP[trans],
                                       _CUBLAS_DIAG[diag],
                                       m, n, ctypes.byref(cuda.cuDoubleComplex(alpha.real,
                                                                               alpha.imag)),
                                       int(A), lda, int(B), ldb, int(C), ldc)
    cublasCheckStatus(status)

Response: Matrix - matrix product for complex triangular matrix.


Instruction:
Can you create a Python 3 function that
solves a real triangular system with multiple right - hand sides.

Response:
def cublasStrsm(handle, side, uplo, trans, diag, m, n, alpha, A, lda, B, ldb):
    """
    Solve a real triangular system with multiple right-hand sides.

    """

    status = _libcublas.cublasStrsm_v2(handle,
                                       _CUBLAS_SIDE_MODE[side],
                                       _CUBLAS_FILL_MODE[uplo],
                                       _CUBLAS_OP[trans],
                                       _CUBLAS_DIAG[diag],
                                       m, n, ctypes.byref(ctypes.c_float(alpha)),
                                       int(A), lda, int(B), ldb)
    cublasCheckStatus(status)


Instruction:
Write a Python 3 function for
solving a real triangular system with multiple right-hand sides.

Response:
def cublasDtrsm(handle, side, uplo, trans, diag, m, n, alpha, A, lda, B, ldb):
    """
    Solve a real triangular system with multiple right-hand sides.

    """

    status = _libcublas.cublasDtrsm_v2(handle,
                                       _CUBLAS_SIDE_MODE[side],
                                       _CUBLAS_FILL_MODE[uplo],
                                       _CUBLAS_OP[trans],
                                       _CUBLAS_DIAG[diag],
                                       m, n, ctypes.byref(ctypes.c_double(alpha)),
                                       int(A), lda, int(B), ldb)
    cublasCheckStatus(status)


Instruction:
Explain what the following Python 3 code does
def cublasZtrsm(handle, side, uplo, transa, diag, m, n, alpha, A, lda, B, ldb):

    status = _libcublas.cublasZtrsm_v2(handle,
                                       _CUBLAS_SIDE_MODE[side],
                                       _CUBLAS_FILL_MODE[uplo],
                                       _CUBLAS_OP[trans],
                                       _CUBLAS_DIAG[diag],
                                       m, n, ctypes.byref(cuda.cuDoubleComplex(alpha.real,
                                                                               alpha.imag)),
                                       int(A), lda, int(B), ldb)
    cublasCheckStatus(status)

Response: Matrix - matrix product for complex triangular system.


Instruction:
Make a summary of the following Python 3 code
def cublasZherk(handle, uplo, trans, n, k, alpha, A, lda, beta, C, ldc):

    status = _libcublas.cublasZherk_v2(handle,
                                       _CUBLAS_FILL_MODE[uplo],
                                       _CUBLAS_OP[trans],
                                       n, k, ctypes.byref(ctypes.c_double(alpha)),
                                       int(A), lda,
                                       ctypes.byref(ctypes.c_double(beta)),
                                       int(C), ldc)
    cublasCheckStatus(status)

Response: Matrix - rank - k operation on Hermitian matrix.


Instruction:
Explain what the following Python 3 code does
def cublasCher2k(handle, uplo, trans, n, k, alpha, A, lda, B, ldb, beta, C, ldc):

    status = _libcublas.cublasCher2k_v2(handle,
                                        _CUBLAS_FILL_MODE[uplo],
                                        _CUBLAS_OP[trans],
                                        n, k, ctypes.byref(cuda.cuFloatComplex(alpha.real,
                                                                               alpha.imag)),
                                        int(A), lda, int(B), ldb,
                                        ctypes.byref(cuda.cuFloatComplex(beta.real,
                                                                         beta.imag)),
                                        int(C), ldc)
    cublasCheckStatus(status)

Response: Matrix - rank 2k operation on Hermitian matrix.


Instruction:
Make a summary of the following Python 3 code
def load_EROS_lc(filename='lm0010n22323.time'):

    module_path = dirname(__file__)
    file_path = join(module_path, 'lightcurves', filename)

    data = np.loadtxt(file_path)
    date = data[:, 0]
    mag = data[:, 1]
    err = data[:, 2]

    return date, mag, err

Response: Reads an EROS light curve and returns its data.


Instruction:
Can you generate the documentation for the following Python 3 function
def load_rf_model():

    import gzip
    try:
        import cPickle as pickle
    except:
        import pickle

    module_path = dirname(__file__)
    file_path = join(module_path, 'models/rf.model.sub.github.gz')

    # For Python 3.
    if sys.version_info.major >= 3:
        clf = pickle.load(gzip.open(file_path, 'rb'), encoding='latin1')
    # For Python 2.
    else:
        clf = pickle.load(gzip.open(file_path, 'rb'))

    return clf

Response: Loads the UPSILoN random forests classifier.


Instruction:
How would you explain what the following Python 3 function does
def sample_dropout_mask(x, dropout_probability=.5, columns=None, stream=None, target=None,
                        dropout_mask=None, dropout_prob_array=None):
    """ Samples a dropout mask and applies it in place"""

    assert x.flags.c_contiguous

    if columns is not None:
        assert len(columns) == 2
        x_tmp = x
        x = extract_columns(x, columns[0], columns[1])

    shape = x.shape

    if dropout_prob_array is None:
        dropout_prob_array = gpuarray.empty(shape, x.dtype, allocator=memory_pool.allocate)
    sampler.fill_uniform(dropout_prob_array, stream)

    if dropout_mask is None:
        dropout_mask = gpuarray.empty(shape, np.int8, allocator=memory_pool.allocate)

    if target is None: target = x

    all_kernels['sample_dropout_mask'](
        x, target, dropout_mask, dropout_prob_array,
        np.float32(dropout_probability))

    if columns is not None:
        insert_columns(x, x_tmp, columns[0])

    return dropout_mask

Response: Samples a dropout mask and applies it in place


Instruction:
Implement a Python 3 function for
loading but don t evaluate a GCL expression from a string.

Response:
def reads(s, filename=None, loader=None, implicit_tuple=True, allow_errors=False):
  """Load but don't evaluate a GCL expression from a string."""
  return ast.reads(s,
      filename=filename or '<input>',
      loader=loader or default_loader,
      implicit_tuple=implicit_tuple,
      allow_errors=allow_errors)


Instruction:
Write a Python 3 function that can
load but don t evaluate a GCL expression from a file.

Response:
def read(filename, loader=None, implicit_tuple=True, allow_errors=False):
  """Load but don't evaluate a GCL expression from a file."""
  with open(filename, 'r') as f:
    return reads(f.read(),
                 filename=filename,
                 loader=loader,
                 implicit_tuple=implicit_tuple,
                 allow_errors=allow_errors)


Instruction:
Can you generate the documentation for the following Python 3 function
def loads(s, filename=None, loader=None, implicit_tuple=True, env={}, schema=None):
  ast = reads(s, filename=filename, loader=loader, implicit_tuple=implicit_tuple)
  if not isinstance(env, framework.Environment):
    # For backwards compatibility we accept an Environment object. Otherwise assume it's a dict
    # whose bindings will add/overwrite the default bindings.
    env = framework.Environment(dict(_default_bindings, **env))
  obj = framework.eval(ast, env)
  return mod_schema.validate(obj, schema)

Response: Load and evaluate a GCL expression from a string.


Instruction:
Create a Python 3 function for
loading and evaluate a GCL expression from a file.

Response:
def load(filename, loader=None, implicit_tuple=True, env={}, schema=None):
  """Load and evaluate a GCL expression from a file."""
  with open(filename, 'r') as f:
    return loads(f.read(),
                 filename=filename,
                 loader=loader,
                 implicit_tuple=implicit_tuple,
                 env=env,
                 schema=schema)


Instruction:
Can you write a function in Python 3 where it
increases linearly then stays flat

Response:
def linear_scheduler_up(init_value, target_value, duration):
    """ Increases linearly and then stays flat """

    value = init_value
    t = 0
    while True:
        yield value
        t += 1
        if t < duration:
            value = init_value + t * (target_value - init_value) / duration
        else:
            value = target_value


Instruction:
Can you write a function in Python 3 where it
increases linearly to target_value, stays at target_value until t_decrease and then decreases linearly

Response:
def linear_scheduler_up_down(init_value, target_value, final_value,
                             duration_up, t_decrease, duration_down):
    """ Increases linearly to target_value, stays at target_value until
    t_decrease and then decreases linearly
    """

    value = init_value
    t = 0

    while True:
        yield value
        t += 1
        if t < duration_up:
            value = init_value + t * (target_value - init_value) / \
                    float(duration_up)
        elif t > t_decrease:
            value = target_value - (t - t_decrease) * \
                    (target_value - final_value) / \
                    float(duration_down)
        else:
            value = target_value


Instruction:
Write a Python 3 script to
load a YAML configuration from a string or file - like object.

Response:
def load(stream, overrides=None, **kwargs):
    """
    Loads a YAML configuration from a string or file-like object.

    Parameters
    ----------
    stream : str or object
        Either a string containing valid YAML or a file-like object
        supporting the .read() interface.
    overrides : dict, optional
        A dictionary containing overrides to apply. The location of
        the override is specified in the key as a dot-delimited path
        to the desired parameter, e.g. "model.corruptor.corruption_level".

    Returns
    -------
    graph : dict or object
        The dictionary or object (if the top-level element specified an
        Python object to instantiate).

    Notes
    -----
    Other keyword arguments are passed on to `yaml.load`.
    """

    global is_initialized
    if not is_initialized:
        initialize()

    if isinstance(stream, basestring):
        string = stream
    else:
        string = '\n'.join(stream.readlines())

    # processed_string = preprocess(string)

    proxy_graph = yaml.load(string, **kwargs)

    from . import init
    init_dict = proxy_graph.get('init', {})
    init(**init_dict)

    if overrides is not None:
        handle_overrides(proxy_graph, overrides)
    return instantiate_all(proxy_graph)


Instruction:
Write a Python 3 script to
load a YAML file into a dictionary.

Response:
def load_path(path, overrides=None, **kwargs):
    """
    Convenience function for loading a YAML configuration from a file.

    Parameters
    ----------
    path : str
        The path to the file to load on disk.
    overrides : dict, optional
        A dictionary containing overrides to apply. The location of
        the override is specified in the key as a dot-delimited path
        to the desired parameter, e.g. "model.corruptor.corruption_level".

    Returns
    -------
    graph : dict or object
        The dictionary or object (if the top-level element specified an
        Python object to instantiate).

    Notes
    -----
    Other keyword arguments are passed on to `yaml.load`.
    """
    f = open(path, 'r')
    content = ''.join(f.readlines())
    f.close()

    if not isinstance(content, str):
        raise AssertionError("Expected content to be of type str but it is "+str(type(content)))

    return load(content, **kwargs)


Instruction:
Write a Python 3 function that can
handle any overrides for this model configuration.

Response:
def handle_overrides(graph, overrides):
    """
    Handle any overrides for this model configuration.

    Parameters
    ----------
    graph : dict or object
        A dictionary (or an ObjectProxy) containing the object graph
        loaded from a YAML file.
    overrides : dict
        A dictionary containing overrides to apply. The location of
        the override is specified in the key as a dot-delimited path
        to the desired parameter, e.g. "model.corruptor.corruption_level".
    """
    for key in overrides:
        levels = key.split('.')
        part = graph
        for lvl in levels[:-1]:
            try:
                part = part[lvl]
            except KeyError:
                raise KeyError("'%s' override failed at '%s'", (key, lvl))
        try:
            part[levels[-1]] = overrides[key]
        except KeyError:
            raise KeyError("'%s' override failed at '%s'", (key, levels[-1]))


Instruction:
Can you create a Python 3 function that
initializes the configuration system by installing YAML handlers.

Response:
def initialize():
    """
    Initialize the configuration system by installing YAML handlers.
    Automatically done on first call to load() specified in this file.
    """
    global is_initialized
    # Add the custom multi-constructor
    yaml.add_multi_constructor('!obj:', multi_constructor)
    yaml.add_multi_constructor('!pkl:', multi_constructor_pkl)
    yaml.add_multi_constructor('!import:', multi_constructor_import)
    yaml.add_multi_constructor('!include:', multi_constructor_include)

    def import_constructor(loader, node):
        value = loader.construct_scalar(node)
        return try_to_import(value)

    yaml.add_constructor('!import', import_constructor)
    yaml.add_implicit_resolver(
        '!import',
        re.compile(r'(?:[a-zA-Z_][\w_]+\.)+[a-zA-Z_][\w_]+')
    )
    is_initialized = True


Instruction:
Can you generate a brief explanation for the following Python 3 code
def feed_forward(self, input_data, prediction=False):

        if input_data.shape[1] != self.W.shape[0]:
            raise ValueError('Number of outputs from previous layer (%d) '
                             'does not match number of inputs to this layer (%d)' %
                             (input_data.shape[1], self.W.shape[0]))

        activations = linalg.dot(input_data, self.W)
        activations = add_vec_to_mat(activations, self.b, inplace=True)

        return activations

Response: Propagate forward through the layer.


Instruction:
Implement a function in Python 3 to
give abscissas x (which need not be equally spaced) and ordinates y, and given a desired oversampling factor ofac (a typical value being 4 or larger). this routine creates an array wk1 with a sequence of nout increasing frequencies (not angular frequencies) up to hifac times the "average" Nyquist frequency, and creates an array wk2 with the values of the Lomb normalized periodogram at those frequencies. The arrays x and y are not altered. This routine also returns jmax such that wk2(jmax) is the maximum element in wk2, and prob, an estimate of the significance of that maximum against the hypothesis of random noise. A small value of prob indicates that a significant periodic signal is present. Reference: Press, W. H. & Rybicki, G. B. 1989 ApJ vol. 338, p. 277-280. Fast algorithm for spectral analysis of unevenly sampled data (1989ApJ...338..277P) Arguments: X : Abscissas array, (e.g. an array of times). Y : Ordinates array, (e.g. corresponding counts). Ofac : Oversampling factor. Hifac : Hifac * "average" Nyquist frequency = highest frequency for which values of the Lomb normalized periodogram will be calculated. n_threads : number of threads to use. Returns: Wk1 : An array of Lomb periodogram frequencies. Wk2 : An array of corresponding values of the Lomb periodogram. Nout : Wk1 & Wk2 dimensions (number of calculated frequencies) Jmax : The array index corresponding to the MAX( Wk2 ). Prob : False Alarm Probability of the largest Periodogram value MACC : Number of interpolation points per 1/4 cycle of highest frequency History: 02/23/2009, v1.0, MF Translation of IDL code (orig. Numerical recipies)

Response:
def fasper(x, y, ofac, hifac, n_threads, MACC=4):
    """
    Given abscissas x (which need not be equally spaced) and ordinates
    y, and given a desired oversampling factor ofac (a typical value
    being 4 or larger). this routine creates an array wk1 with a
    sequence of nout increasing frequencies (not angular frequencies)
    up to hifac times the "average" Nyquist frequency, and creates
    an array wk2 with the values of the Lomb normalized periodogram at
    those frequencies. The arrays x and y are not altered. This
    routine also returns jmax such that wk2(jmax) is the maximum
    element in wk2, and prob, an estimate of the significance of that
    maximum against the hypothesis of random noise. A small value of prob
    indicates that a significant periodic signal is present.

    Reference:
        Press, W. H. & Rybicki, G. B. 1989
        ApJ vol. 338, p. 277-280.
        Fast algorithm for spectral analysis of unevenly sampled data
        (1989ApJ...338..277P)

    Arguments:
            X     : Abscissas array, (e.g. an array of times).
            Y     : Ordinates array, (e.g. corresponding counts).
            Ofac : Oversampling factor.
            Hifac : Hifac * "average" Nyquist frequency = highest frequency
                     for which values of the Lomb normalized periodogram will
                     be calculated.
            n_threads : number of threads to use.

     Returns:
            Wk1 : An array of Lomb periodogram frequencies.
            Wk2 : An array of corresponding values of the Lomb periodogram.
            Nout : Wk1 & Wk2 dimensions (number of calculated frequencies)
            Jmax : The array index corresponding to the MAX( Wk2 ).
            Prob : False Alarm Probability of the largest Periodogram value
            MACC : Number of interpolation points per 1/4 cycle
                        of highest frequency

    History:
        02/23/2009, v1.0, MF
            Translation of IDL code (orig. Numerical recipies)
    """
    #Check dimensions of input arrays
    n = long(len(x))
    if n != len(y):
        print('Incompatible arrays.')
        return

    #print x, y, hifac, ofac

    nout = int(0.5*ofac*hifac*n)
    nfreqt = long(ofac*hifac*n*MACC)     #Size the FFT as next power
    nfreq = 64                         # of 2 above nfreqt.

    while nfreq < nfreqt:
        nfreq = 2*nfreq

    ndim = long(2*nfreq)

    #Compute the mean, variance
    ave = y.mean()
    ##sample variance because the divisor is N-1
    var = ((y - y.mean())**2).sum()/(len(y) - 1)
    # and range of the data.
    xmin = x.min()
    xmax = x.max()
    xdif = xmax - xmin

    #extrapolate the data into the workspaces
    if is_pyfftw:
        wk1 = pyfftw.n_byte_align_empty(int(ndim), 16, 'complex') * 0.
        wk2 = pyfftw.n_byte_align_empty(int(ndim), 16, 'complex') * 0.
    else:
        wk1 = zeros(ndim, dtype='complex')
        wk2 = zeros(ndim, dtype='complex')

    fac = ndim/(xdif*ofac)
    fndim = ndim
    ck = ((x - xmin)*fac) % fndim
    ckk = (2.0*ck) % fndim

    for j in range(0, n):
        __spread__(y[j] - ave, wk1, ndim, ck[j], MACC)
        __spread__(1.0, wk2, ndim, ckk[j], MACC)

    #Take the Fast Fourier Transforms.
    if is_pyfftw:
        fft_wk1 = pyfftw.builders.ifft(wk1, planner_effort='FFTW_ESTIMATE',
            threads=n_threads)
        wk1 = fft_wk1() * len(wk1)
        fft_wk2 = pyfftw.builders.ifft(wk2, planner_effort='FFTW_ESTIMATE',
            threads=n_threads)
        wk2 = fft_wk2() * len(wk2)
    else:
        wk1 = ifft(wk1)*len(wk1)
        wk2 = ifft(wk2)*len(wk1)

    wk1 = wk1[1:nout + 1]
    wk2 = wk2[1:nout + 1]
    rwk1 = wk1.real
    iwk1 = wk1.imag
    rwk2 = wk2.real
    iwk2 = wk2.imag

    df = 1.0/(xdif*ofac)

    #Compute the Lomb value for each frequency
    hypo2 = 2.0*abs(wk2)
    hc2wt = rwk2/hypo2
    hs2wt = iwk2/hypo2

    cwt = sqrt(0.5 + hc2wt)
    swt = sign(hs2wt)*(sqrt(0.5 - hc2wt))
    den = 0.5*n + hc2wt*rwk2 + hs2wt*iwk2
    cterm = (cwt*rwk1 + swt*iwk1)**2./den
    sterm = (cwt*iwk1 - swt*rwk1)**2./(n - den)

    wk1 = df*(arange(nout, dtype='float') + 1.)
    wk2 = (cterm + sterm)/(2.0*var)
    pmax = wk2.max()
    jmax = wk2.argmax()

    #Significance estimation
    #expy = exp(-wk2)
    #effm = 2.0*(nout)/ofac
    #sig = effm*expy
    #ind = (sig > 0.01).nonzero()
    #sig[ind] = 1.0-(1.0-expy[ind])**effm

    #Estimate significance of largest peak value
    expy = exp(-pmax)
    effm = 2.0*(nout)/ofac
    prob = effm*expy

    if prob > 0.01:
        prob = 1.0 - (1.0 - expy)**effm

    return wk1, wk2, nout, jmax, prob


Instruction:
Can you generate a brief explanation for the following Python 3 code
def get_no_validate(self, key):
    x, env = self.get_thunk_env(key)

    # Check if this is a Thunk that needs to be lazily evaluated before we
    # return it.
    if isinstance(x, framework.Thunk):
      x = framework.eval(x, env)

    return x

Response: Return an item without validating the schema.


Instruction:
How would you explain what the following Python 3 function does
def env(self, current_scope):
    return self.__env_cache.get(
            current_scope.ident,
            framework.Environment, current_scope,
            names=self.keys(),
            parent=framework.Environment({'self': current_scope}, parent=self.__parent_env))

Response:
Return an environment that will look up in current_scope for keys in
    this tuple and the parent env otherwise.


Instruction:
Create a Python 3 function for
returning the thunk and environment for validating it in for the given key.

Response:
def get_thunk_env(self, k):
    """Return the thunk AND environment for validating it in for the given key.

    There might be different envs in case the thunk comes from a different (composed) tuple. If the thunk needs its
    environment bound on retrieval, that will be done here.
    """
    if k not in self.__items:
      raise exceptions.EvaluationError('Unknown key: %r in tuple %r' % (k, self))
    x = self.__items[k]

    env = self.env(self)

    # Bind this to the tuple's parent environment
    if isinstance(x, framework.BindableThunk):
      return x.bind(self.__parent_env), env
    return x, env


Instruction:
Given the following Python 3 function, write the documentation
def attach_schema(self, schem):
    self.tuple_schema = schema.AndSchema.make(self.tuple_schema, schem)

Response: Attach a tuple schema to this object.


Instruction:
How would you code a function in Python 3 to
return the evaluated schema expression from a subkey.

Response:
def get_schema_spec(self, key):
    """Return the evaluated schema expression from a subkey."""
    member_node = self._ast_node.member.get(key, None)
    if not member_node:
      return schema.AnySchema()

    s = framework.eval(member_node.member_schema, self.env(self))
    if not isinstance(s, schema.Schema):
      raise ValueError('Node %r with schema node %r should evaluate to Schema, got %r' % (member_node, member_node.member_schema, s))
    return s


Instruction:
Here you have a function in Python 3, explain what it does
def get_required_fields(self):
    return [m.name for m in self._ast_node.members if m.member_schema.required]

Response: Return the names of fields that are required according to the schema.


Instruction:
Write a Python 3 function for
returning the AST node for the given member from the first tuple that serves it.

Response:
def get_member_node(self, key):
    """Return the AST node for the given member, from the first tuple that serves it."""
    for tup, _ in self.lookups:
      if key in tup:
        return tup.get_member_node(key)
    raise RuntimeError('Key not found in composite tuple: %r' % key)


Instruction:
Given the following Python 3 function, write the documentation
def exportable_keys(self):
    keys = collections.defaultdict(list)
    for tup in self._tuples:
      for key, private in tup._keys_and_privacy().items():
        keys[key].append(private)
    return [k for k, ps in keys.items() if not any(ps)]

Response: Return a list of keys that are exportable from this tuple.


Instruction:
Can you generate the documentation for the following Python 3 function
def resolve(self, current_file, rel_path):
    search_path = [path.dirname(current_file)] + self.search_path

    target_path = None
    for search in search_path:
      if self.exists(path.join(search, rel_path)):
        target_path = path.normpath(path.join(search, rel_path))
        break

    if not target_path:
      raise exceptions.EvaluationError('No such file: %r, searched %s' %
                            (rel_path, ':'.join(search_path)))

    return target_path, path.abspath(target_path)

Response: Search the filesystem for a file with the given relative path.


Instruction:
Write a Python 3 script to
search the filesystem for a fake file.

Response:
def resolve(self, current_file, rel_path):
    """Search the filesystem."""
    p = path.join(path.dirname(current_file), rel_path)
    if p not in self.file_dict:
      raise RuntimeError('No such fake file: %r' % p)
    return p, p


Instruction:
Explain what the following Python 3 code does
def check_call_arguments(to_call, kwargs):
    if 'self' in kwargs.keys():
        raise TypeError("Your dictionary includes an entry for 'self', "
                        "which is just asking for trouble")

    orig_to_call = getattr(to_call, '__name__', str(to_call))
    if not isinstance(to_call, types.FunctionType):
        if hasattr(to_call, '__init__'):
            to_call = to_call.__init__
        elif hasattr(to_call, '__call__'):
            to_call = to_call.__call__

    args, varargs, keywords, defaults = inspect.getargspec(to_call)

    if any(not isinstance(arg, str) for arg in args):
        raise TypeError('%s uses argument unpacking, which is deprecated and '
                        'unsupported by this pylearn2' % orig_to_call)

    if varargs is not None:
        raise TypeError('%s has a variable length argument list, but '
                        'this is not supported by config resolution' %
                        orig_to_call)

    if keywords is None:
        bad_keywords = [arg_name for arg_name in kwargs.keys()
                        if arg_name not in args]

        if len(bad_keywords) > 0:
            bad = ', '.join(bad_keywords)
            args = [ arg for arg in args if arg != 'self' ]
            if len(args) == 0:
                matched_str = '(It does not support any keywords, actually)'
            else:
                matched = [ match(keyword, args) for keyword in bad_keywords ]
                matched_str = 'Did you mean %s?' % (', '.join(matched))
            raise TypeError('%s does not support the following '
                            'keywords: %s. %s' %
                            (orig_to_call, bad, matched_str))

    if defaults is None:
        num_defaults = 0
    else:
        num_defaults = len(defaults)

    required = args[:len(args) - num_defaults]
    missing = [arg for arg in required if arg not in kwargs]

    if len(missing) > 0:
        #iff the im_self (or __self__) field is present, this is a
        # bound method, which has 'self' listed as an argument, but
        # which should not be supplied by kwargs
        is_bound = hasattr(to_call, 'im_self') or hasattr(to_call, '__self__')
        if len(missing) > 1 or missing[0] != 'self' or not is_bound:
            if 'self' in missing:
                missing.remove('self')
            missing = ', '.join([str(m) for m in missing])
            raise TypeError('%s did not get these expected '
                            'arguments: %s' % (orig_to_call, missing))

Response: Checks the signature of a function or class or class to examine the arguments of a class or callable.


Instruction:
Here you have a function in Python 3, explain what it does
def predict(rf_model, features):

    import numpy as np

    from upsilon.extract_features.feature_set import get_feature_set
    feature_set = get_feature_set()

    # Grab only necessary features.
    cols = [feature for feature in features if feature in feature_set]
    cols = sorted(cols)
    filtered_features = []
    for i in range(len(cols)):
        filtered_features.append(features[cols[i]])
    filtered_features = np.array(filtered_features).reshape(1, -1)

    # Classify.
    classes = rf_model.classes_
    # Note that we're classifying a single source, so [0] need tobe added.
    probabilities = rf_model.predict_proba(filtered_features)[0]

    # Classification flag.
    flag = 0
    if features['period_SNR'] < 20. or is_period_alias(features['period']):
        flag = 1

    # Return class, probability, and flag.
    max_index = np.where(probabilities == np.max(probabilities))
    return classes[max_index][0], probabilities[max_index][0], flag

Response: Predict the class of a single source.


Instruction:
Explain what the following Python 3 code does
def fmt(str, args=None, env=None):
  # Normally, we'd just call str.format(**args), but we only want to evaluate
  # values from the tuple which are actually used in the string interpolation,
  # so we use proxy objects.

  # If no args are given, we're able to take the current environment.
  args = args or env
  proxies = {k: StringInterpolationProxy(args, k) for k in args.keys()}
  return str.format(**proxies)

Response:
Interpolate a string with the same
 name.


Instruction:
Make a summary of the following Python 3 code
def compose_all(tups):
  from . import ast  # I weep for humanity
  return functools.reduce(lambda x, y: x.compose(y), map(ast.make_tuple, tups), ast.make_tuple({}))

Response: Compose all given tuples together.


Instruction:
Can you create a Python 3 function that
returns True if a given tuple has a key and the key is bound.

Response:
def has_key(tup, key):
  """has(tuple, string) -> bool

  Return whether a given tuple has a key and the key is bound.
  """
  if isinstance(tup, framework.TupleLike):
    return tup.is_bound(key)
  if isinstance(tup, dict):
    return key in tup
  if isinstance(tup, list):
    if not isinstance(key, int):
      raise ValueError('Key must be integer when checking list index')
    return key < len(tup)
  raise ValueError('Not a tuple-like object: %r' % tup)


Instruction:
Write a Python 3 function that can
flatten a list of lists.

Response:
def flatten(list_of_lists):
  """flatten([[A]]) -> [A]

  Flatten a list of lists.
  """
  ret = []
  for lst in list_of_lists:
    if not isinstance(lst, list):
      raise ValueError('%r is not a list' % lst)
    ret.extend(lst)
  return ret


Instruction:
Implement a Python 3 function for
extracting superdark data from DARKFILE or DRKCFILE.

Response:
def extract_dark(prihdr, scihdu):
    """Extract superdark data from ``DARKFILE`` or ``DRKCFILE``.

    Parameters
    ----------
    prihdr : obj
        FITS primary header HDU.

    scihdu : obj
        Extension HDU of the science image.
        This is only used to extract subarray data.

    Returns
    -------
    dark : ndarray or `None`
        Superdark, if any. Subtract this to apply ``DARKCORR``.

    """
    if prihdr.get('PCTECORR', 'OMIT') == 'COMPLETE':
        darkfile = prihdr.get('DRKCFILE', 'N/A')
    else:
        darkfile = prihdr.get('DARKFILE', 'N/A')

    if darkfile == 'N/A':
        return None

    darkfile = from_irafpath(darkfile)
    ampstring = prihdr['CCDAMP']

    # Calculate DARKTIME
    exptime = prihdr.get('EXPTIME', 0.0)
    flashdur = prihdr.get('FLASHDUR', 0.0)
    darktime = exptime + flashdur
    if exptime > 0:  # Not BIAS
        darktime += 3.0

    with fits.open(darkfile) as hdudark:
        if ampstring == 'ABCD':
            dark = np.concatenate(
                (hdudark['sci', 1].data,
                 hdudark['sci', 2].data[::-1, :]), axis=1)
        elif ampstring in ('A', 'B', 'AB'):
            dark = extract_ref(scihdu, hdudark['sci', 2])
        else:
            dark = extract_ref(scihdu, hdudark['sci', 1])

    dark = dark * darktime

    return dark


Instruction:
How would you code a function in Python 3 to
extract postflash data from FITS primary header HDU.

Response:
def extract_flash(prihdr, scihdu):
    """Extract postflash data from ``FLSHFILE``.

    Parameters
    ----------
    prihdr : obj
        FITS primary header HDU.

    scihdu : obj
        Extension HDU of the science image.
        This is only used to extract subarray data.

    Returns
    -------
    flash : ndarray or `None`
        Postflash, if any. Subtract this to apply ``FLSHCORR``.

    """
    flshfile = prihdr.get('FLSHFILE', 'N/A')
    flashsta = prihdr.get('FLASHSTA', 'N/A')
    flashdur = prihdr.get('FLASHDUR', 0.0)

    if flshfile == 'N/A' or flashdur <= 0:
        return None

    if flashsta != 'SUCCESSFUL':
        warnings.warn('Flash status is {0}'.format(flashsta),
                      AstropyUserWarning)

    flshfile = from_irafpath(flshfile)
    ampstring = prihdr['CCDAMP']

    with fits.open(flshfile) as hduflash:
        if ampstring == 'ABCD':
            flash = np.concatenate(
                (hduflash['sci', 1].data,
                 hduflash['sci', 2].data[::-1, :]), axis=1)
        elif ampstring in ('A', 'B', 'AB'):
            flash = extract_ref(scihdu, hduflash['sci', 2])
        else:
            flash = extract_ref(scihdu, hduflash['sci', 1])

    flash = flash * flashdur

    return flash


Instruction:
Explain what the following Python 3 code does
def extract_flatfield(prihdr, scihdu):
    for ff in ['DFLTFILE', 'LFLTFILE']:
        vv = prihdr.get(ff, 'N/A')
        if vv != 'N/A':
            warnings.warn('{0}={1} is not accounted for'.format(ff, vv),
                          AstropyUserWarning)

    flatfile = prihdr.get('PFLTFILE', 'N/A')

    if flatfile == 'N/A':
        return None

    flatfile = from_irafpath(flatfile)
    ampstring = prihdr['CCDAMP']

    with fits.open(flatfile) as hduflat:
        if ampstring == 'ABCD':
            invflat = np.concatenate(
                (1 / hduflat['sci', 1].data,
                 1 / hduflat['sci', 2].data[::-1, :]), axis=1)
        elif ampstring in ('A', 'B', 'AB'):
            invflat = 1 / extract_ref(scihdu, hduflat['sci', 2])
        else:
            invflat = 1 / extract_ref(scihdu, hduflat['sci', 1])

    return invflat

Response: Extract flatfield data from the FITS primary header HDU and the science image.


Instruction:
Explain what the following Python 3 code does
def from_irafpath(irafpath):
    s = irafpath.split('$')

    if len(s) != 2:
        return irafpath
    if len(s[0]) == 0:
        return irafpath

    try:
        refdir = os.environ[s[0]]
    except KeyError:
        raise ValueError('{0} environment variable undefined'.format(s[0]))

    return os.path.join(refdir, s[1])

Response: Resolve IRAF path like jref$ into actual file path.


Instruction:
Write a Python 3 function for
extracting the section of the relevant reference image that corresponds to the given science image.

Response:
def extract_ref(scihdu, refhdu):
    """Extract section of the reference image that
    corresponds to the given science image.

    This only returns a view, not a copy of the
    reference image's array.

    Parameters
    ----------
    scihdu, refhdu : obj
        Extension HDU's of the science and reference image,
        respectively.

    Returns
    -------
    refdata : array-like
        Section of the relevant reference image.

    Raises
    ------
    NotImplementedError
        Either science or reference data are binned.

    ValueError
        Extracted section size mismatch.

    """
    same_size, rx, ry, x0, y0 = find_line(scihdu, refhdu)

    # Use the whole reference image
    if same_size:
        return refhdu.data

    # Binned data
    if rx != 1 or ry != 1:
        raise NotImplementedError(
            'Either science or reference data are binned')

    # Extract a view of the sub-section
    ny, nx = scihdu.data.shape
    refdata = refhdu.data[y0:y0+ny, x0:x0+nx]

    if refdata.shape != (ny, nx):
        raise ValueError('Extracted reference image is {0} but science image '
                         'is {1}'.format(refdata.shape, (ny, nx)))

    return refdata


Instruction:
Explain what the following Python 3 code does
def find_line(scihdu, refhdu):
    sci_bin, sci_corner = get_corner(scihdu.header)
    ref_bin, ref_corner = get_corner(refhdu.header)

    # We can use the reference image directly, without binning
    # and without extracting a subset.
    if (sci_corner[0] == ref_corner[0] and sci_corner[1] == ref_corner[1] and
            sci_bin[0] == ref_bin[0] and sci_bin[1] == ref_bin[1] and
            scihdu.data.shape[1] == refhdu.data.shape[1]):
        same_size = True
        rx = 1
        ry = 1
        x0 = 0
        y0 = 0

    # Reference image is binned more than the science image.
    elif ref_bin[0] > sci_bin[0] or ref_bin[1] > sci_bin[1]:
        same_size = False
        rx = ref_bin[0] / sci_bin[0]
        ry = ref_bin[1] / sci_bin[1]
        x0 = (sci_corner[0] - ref_corner[0]) / ref_bin[0]
        y0 = (sci_corner[1] - ref_corner[1]) / ref_bin[1]

    # For subarray input images, whether they are binned or not.
    else:
        same_size = False

        # Ratio of bin sizes.
        ratiox = sci_bin[0] / ref_bin[0]
        ratioy = sci_bin[1] / ref_bin[1]

        if (ratiox * ref_bin[0] != sci_bin[0] or
                ratioy * ref_bin[1] != sci_bin[1]):
            raise ValueError('Science and reference data size mismatch')

        # cshift is the offset in units of unbinned pixels.
        # Divide by ref_bin to convert to units of pixels in the ref image.
        cshift = (sci_corner[0] - ref_corner[0], sci_corner[1] - ref_corner[1])
        xzero = cshift[0] / ref_bin[0]
        yzero = cshift[1] / ref_bin[1]

        if (xzero * ref_bin[0] != cshift[0] or
                yzero * ref_bin[1] != cshift[1]):
            warnings.warn('Subimage offset not divisible by bin size',
                          AstropyUserWarning)

        rx = ratiox
        ry = ratioy
        x0 = xzero
        y0 = yzero

    # Ensure integer index
    x0 = int(x0)
    y0 = int(y0)

    return same_size, rx, ry, x0, y0

Response: Return a copy of the base image that is part of the given science image and the corresponding subset of a reference image.


Instruction:
Create a Python 3 function to
obtain the bin and corner information for a subarray.

Response:
def get_corner(hdr, rsize=1):
    """Obtain bin and corner information for a subarray.

    ``LTV1``, ``LTV2``, ``LTM1_1``, and ``LTM2_2`` keywords
    are extracted from the given extension header and converted
    to bin and corner values (0-indexed).

    ``LTV1`` for the CCD uses the beginning of the illuminated
    portion as the origin, not the beginning of the overscan region.
    Thus, the computed X-corner has the same origin as ``LTV1``,
    which is what we want, but it differs from the ``CENTERA1``
    header keyword, which has the beginning of the overscan region
    as origin.

    .. note:: Translated from ``calacs/lib/getcorner.c``.

    Parameters
    ----------
    hdr : obj
        Extension header.

    rsize : int, optional
        Size of reference pixel in units of high-res pixels.

    Returns
    -------
    bin : tuple of int
        Pixel size in X and Y.

    corner : tuple of int
        Corner of subarray in X and Y.

    """
    ltm, ltv = get_lt(hdr)
    return from_lt(rsize, ltm, ltv)


Instruction:
Implement a function in Python 3 to
obtain the LTV and LTM keywords from the header.

Response:
def get_lt(hdr):
    """Obtain the LTV and LTM keyword values.

    Note that this returns the values just as read from the header,
    which means in particular that the LTV values are for one-indexed
    pixel coordinates.

    LTM keywords are the diagonal elements of MWCS linear
    transformation matrix, while LTV's are MWCS linear transformation
    vector (1-indexed).

    .. note:: Translated from ``calacs/lib/getlt.c``.

    Parameters
    ----------
    hdr : obj
        Extension header.

    Returns
    -------
    ltm, ltv : tuple of float
        ``(LTM1_1, LTM2_2)`` and ``(LTV1, LTV2)``.
        Values are ``(1, 1)`` and ``(0, 0)`` if not found,
        to accomodate reference files with missing info.

    Raises
    ------
    ValueError
        Invalid LTM* values.

    """
    ltm = (hdr.get('LTM1_1', 1.0), hdr.get('LTM2_2', 1.0))

    if ltm[0] <= 0 or ltm[1] <= 0:
        raise ValueError('(LTM1_1, LTM2_2) = {0} is invalid'.format(ltm))

    ltv = (hdr.get('LTV1', 0.0), hdr.get('LTV2', 0.0))
    return ltm, ltv


Instruction:
Can you generate a brief explanation for the following Python 3 code
def from_lt(rsize, ltm, ltv):
    dbinx = rsize / ltm[0]
    dbiny = rsize / ltm[1]

    dxcorner = (dbinx - rsize) - dbinx * ltv[0]
    dycorner = (dbiny - rsize) - dbiny * ltv[1]

    # Round off to the nearest integer.
    bin = (_nint(dbinx), _nint(dbiny))
    corner = (_nint(dxcorner), _nint(dycorner))

    return bin, corner

Response:
Compute the bin and corner location of unbinned pixels in units
   .


Instruction:
Given the following Python 3 function, write the documentation
def hdr_vals_for_overscan(root):
    with fits.open(root + '_spt.fits') as hdu:
        spthdr = hdu[0].header
    with fits.open(root + '_raw.fits') as hdu:
        prihdr = hdu[0].header
    xstart = spthdr['SS_A1CRN']
    ystart = spthdr['SS_A2CRN']
    xsize = spthdr['SS_A1SZE']
    ysize = spthdr['SS_A2SZE']
    ccdamp = prihdr['CCDAMP']

    return ccdamp, xstart, ystart, xsize, ysize

Response:
Retrieve header keyword values from RAW and SPT
    FITS files to pass on to check_oscntab.


Instruction:
Can you generate the documentation for the following Python 3 function
def check_oscntab(oscntab, ccdamp, xsize, ysize, leading, trailing):
    tab = Table.read(oscntab)
    ccdamp = ccdamp.lower().rstrip()
    for row in tab:
        if (row['CCDAMP'].lower().rstrip() in ccdamp and
                row['NX'] == xsize and row['NY'] == ysize and
                row['TRIMX1'] == leading and row['TRIMX2'] == trailing):
            return True
    return False

Response: Checks if the input parameters are in the OSCNTAB file.


Instruction:
Can you tell what is the following Python 3 function doing
def check_overscan(xstart, xsize, total_prescan_pixels=24,
                   total_science_pixels=4096):
    """Check image for bias columns.

    Parameters
    ----------
    xstart : int
        Starting column of the readout in detector coordinates.

    xsize : int
        Number of columns in the readout.

    total_prescan_pixels : int
        Total prescan pixels for a single amplifier on a detector.
        Default is 24 for WFC.

    total_science_pixels : int
        Total science pixels across a detector.
        Default is 4096 for WFC (across two amplifiers).

    Returns
    -------
    hasoverscan : bool
        Indication if there are bias columns in the image.

    leading : int
        Number of bias columns on the A/C amplifiers
        side of the CCDs ("TRIMX1" in ``OSCNTAB``).

    trailing : int
        Number of bias columns on the B/D amplifiers
        side of the CCDs ("TRIMX2" in ``OSCNTAB``).

    """
    hasoverscan = False
    leading = 0
    trailing = 0

    if xstart < total_prescan_pixels:
        hasoverscan = True
        leading = abs(xstart - total_prescan_pixels)

    if (xstart + xsize) > total_science_pixels:
        hasoverscan = True
        trailing = abs(total_science_pixels -
                       (xstart + xsize - total_prescan_pixels))

    return hasoverscan, leading, trailing

Response: Check image for bias columns.


Instruction:
Explain what the following Python 3 code does
def process_request(self, request):
        referer_url = request.META.get('HTTP_REFERER')
        return_to_index_url = request.session.get('return_to_index_url')

        try:
            if all((
                return_to_index_url,
                referer_url,
                request.method == 'GET',
                not request.is_ajax(),
                resolve(request.path).url_name in ('wagtailadmin_explore_root',
                                                   'wagtailadmin_explore'),
            )):
                perform_redirection = False
                referer_match = resolve(urlparse(referer_url).path)
                if all((
                    referer_match.namespace == 'wagtailadmin_pages',
                    referer_match.url_name in (
                        'add',
                        'edit',
                        'delete',
                        'unpublish',
                        'copy'
                    ),
                )):
                    perform_redirection = True
                elif all((
                    not referer_match.namespace,
                    referer_match.url_name in (
                        'wagtailadmin_pages_create',
                        'wagtailadmin_pages_edit',
                        'wagtailadmin_pages_delete',
                        'wagtailadmin_pages_unpublish'
                    ),
                )):
                    perform_redirection = True
                if perform_redirection:
                    del request.session['return_to_index_url']
                    return HttpResponseRedirect(return_to_index_url)

        except Resolver404:
            pass

        return None

Response: Process the request and return the appropriate HTTP response.


Instruction:
Explain what the following Python 3 code does
def run(configobj=None):
    acs2d(configobj['input'],
          exec_path=configobj['exec_path'],
          time_stamps=configobj['time_stamps'],
          verbose=configobj['verbose'],
          quiet=configobj['quiet']
          )

Response: Run TEAL interface for the acs2d function.


Instruction:
Can you write a function in Python 3 where it
runs TEAL interface for the acscte function.

Response:
def run(configobj=None):
    """
    TEAL interface for the `acscte` function.

    """
    acscte(configobj['input'],
           exec_path=configobj['exec_path'],
           time_stamps=configobj['time_stamps'],
           verbose=configobj['verbose'],
           quiet=configobj['quiet'],
           single_core=configobj['single_core']
           )


Instruction:
Make a summary of the following Python 3 code
def _check_inputs(self):
        valid_detector = True
        valid_filter = True
        valid_date = True
        # Determine the submitted detector is valid
        if self.detector not in self._valid_detectors:
            msg = ('{} is not a valid detector option.\n'
                   'Please choose one of the following:\n{}\n'
                   '{}'.format(self.detector,
                               '\n'.join(self._valid_detectors),
                               self._msg_div))
            LOG.error(msg)
            valid_detector = False

        # Determine if the submitted filter is valid
        if (self.filt is not None and valid_detector and
                self.filt not in self.valid_filters[self.detector]):
            msg = ('{} is not a valid filter for {}\n'
                   'Please choose one of the following:\n{}\n'
                   '{}'.format(self.filt, self.detector,
                               '\n'.join(self.valid_filters[self.detector]),
                               self._msg_div))
            LOG.error(msg)
            valid_filter = False

        # Determine if the submitted date is valid
        date_check = self._check_date()
        if date_check is not None:
            LOG.error('{}\n{}'.format(date_check, self._msg_div))
            valid_date = False

        if not valid_detector or not valid_filter or not valid_date:
            return False

        return True

Response: Checks the inputs to ensure they are valid.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _check_date(self, fmt='%Y-%m-%d'):
        result = None
        try:
            dt_obj = dt.datetime.strptime(self.date, fmt)
        except ValueError:
            result = '{} does not match YYYY-MM-DD format'.format(self.date)
        else:
            if dt_obj < self._acs_installation_date:
                result = ('The observation date cannot occur '
                          'before ACS was installed ({})'
                          .format(self._acs_installation_date.strftime(fmt)))
            elif dt_obj > self._extrapolation_date:
                result = ('The observation date cannot occur after the '
                          'maximum allowable date, {}. Extrapolations of the '
                          'instrument throughput after this date lead to '
                          'high uncertainties and are therefore invalid.'
                          .format(self._extrapolation_date.strftime(fmt)))
        finally:
            return result

Response: Convenience method for determining if the input date is valid.


Instruction:
How would you code a function in Python 3 to
submit a request to the ACS Zeropoint Calculator.

Response:
def _submit_request(self):
        """Submit a request to the ACS Zeropoint Calculator.

        If an exception is raised during the request, an error message is
        given. Otherwise, the response is saved in the corresponding
        attribute.

        """
        try:
            self._response = urlopen(self._url)
        except URLError as e:
            msg = ('{}\n{}\nThe query failed! Please check your inputs. '
                   'If the error persists, submit a ticket to the '
                   'ACS Help Desk at hsthelp.stsci.edu with the error message '
                   'displayed above.'.format(str(e), self._msg_div))
            LOG.error(msg)
            self._failed = True
        else:
            self._failed = False


Instruction:
How would you explain what the following Python 3 function does
def _parse_and_format(self):

        soup = BeautifulSoup(self._response.read(), 'html.parser')

        # Grab all elements in the table returned by the ZPT calc.
        td = soup.find_all('td')

        # Remove the units attached to PHOTFLAM and PHOTPLAM column names.
        td = [val.text.split(' ')[0] for val in td]

        # Turn the single list into a 2-D numpy array
        data = np.reshape(td,
                          (int(len(td) / self._block_size), self._block_size))
        # Create the QTable, note that sometimes self._response will be empty
        # even though the return was successful; hence the try/except to catch
        # any potential index errors. Provide the user with a message and
        # set the zpt_table to None.
        try:
            tab = QTable(data[1:, :],
                         names=data[0],
                         dtype=[str, float, float, float, float, float])
        except IndexError as e:
            msg = ('{}\n{}\n There was an issue parsing the request. '
                   'Try resubmitting the query. If this issue persists, please '
                   'submit a ticket to the Help Desk at'
                   'https://stsci.service-now.com/hst'
                   .format(e, self._msg_div))
            LOG.info(msg)
            self._zpt_table = None
        else:
            # If and only if no exception was raised, attach the units to each
            # column of the QTable. Note we skip the FILTER column because
            # Quantity objects in astropy must be numerical (i.e. not str)
            for col in tab.colnames:
                if col.lower() == 'filter':
                    continue
                tab[col].unit = self._data_units[col]

            self._zpt_table = tab

Response: Parse and format the results returned by the ACS Zeropoint Calculator.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def fetch(self):
        LOG.info('Checking inputs...')
        valid_inputs = self._check_inputs()

        if valid_inputs:
            LOG.info('Submitting request to {}'.format(self._url))
            self._submit_request()
            if self._failed:
                return

            LOG.info('Parsing the response and formatting the results...')
            self._parse_and_format()
            return self.zpt_table

        LOG.error('Please fix the incorrect input(s)')

Response: Submit the request to the ACS Zeropoints Calculator and parse the response into a table with the correct units.


Instruction:
Implement a Python 3 function for
returning a QuerySet of all the model instances that can be edited by the admin site.

Response:
def get_queryset(self, request):
        """
        Returns a QuerySet of all model instances that can be edited by the
        admin site.
        """
        qs = self.model._default_manager.get_queryset()
        ordering = self.get_ordering(request)
        if ordering:
            qs = qs.order_by(*ordering)
        return qs


Instruction:
Can you generate the documentation for the following Python 3 function
def unpublish_view(self, request, object_id):
        kwargs = {'model_admin': self, 'object_id': object_id}
        view_class = self.unpublish_view_class
        return view_class.as_view(**kwargs)(request)

Response: Instantiates a class - based view that redirects to Wagtail s unpublish view for models that extend Page.


Instruction:
Write a Python 3 function for
returning a list of permissions that can be assigned to the current page.

Response:
def get_permissions_for_registration(self):
        """
        Utilised by Wagtail's 'register_permissions' hook to allow permissions
        for a model to be assigned to groups in settings. This is only required
        if the model isn't a Page model, and isn't registered as a Snippet
        """
        from wagtail.wagtailsnippets.models import SNIPPET_MODELS
        if not self.is_pagemodel and self.model not in SNIPPET_MODELS:
            return self.permission_helper.get_all_model_permissions()
        return Permission.objects.none()


Instruction:
Create a Python 3 function for
returning a list of urls that can be used to register the admin urls for the current object.

Response:
def get_admin_urls_for_registration(self):
        """
        Utilised by Wagtail's 'register_admin_urls' hook to register urls for
        our the views that class offers.
        """
        urls = (
            url(get_url_pattern(self.opts),
                self.index_view, name=get_url_name(self.opts)),
            url(get_url_pattern(self.opts, 'create'),
                self.create_view, name=get_url_name(self.opts, 'create')),
            url(get_object_specific_url_pattern(self.opts, 'edit'),
                self.edit_view, name=get_url_name(self.opts, 'edit')),
            url(get_object_specific_url_pattern(self.opts, 'confirm_delete'),
                self.confirm_delete_view,
                name=get_url_name(self.opts, 'confirm_delete')),
        )
        if self.inspect_view_enabled:
            urls = urls + (
                url(get_object_specific_url_pattern(self.opts, 'inspect'),
                    self.inspect_view,
                    name=get_url_name(self.opts, 'inspect')),
            )
        if self.is_pagemodel:
            urls = urls + (
                url(get_url_pattern(self.opts, 'choose_parent'),
                    self.choose_parent_view,
                    name=get_url_name(self.opts, 'choose_parent')),
                url(get_object_specific_url_pattern(self.opts, 'unpublish'),
                    self.unpublish_view,
                    name=get_url_name(self.opts, 'unpublish')),
                url(get_object_specific_url_pattern(self.opts, 'copy'),
                    self.copy_view,
                    name=get_url_name(self.opts, 'copy')),
            )
        return urls

        def construct_main_menu(self, request, menu_items):
            warnings.warn((
                "The 'construct_main_menu' method is now deprecated. You "
                "should also remove the construct_main_menu hook from "
                "wagtail_hooks.py in your app folder."), DeprecationWarning)
            return menu_items


Instruction:
Can you implement a function in Python 3 that
creates a menu item for this group.

Response:
def get_menu_item(self):
        """
        Utilised by Wagtail's 'register_menu_item' hook to create a menu
        for this group with a SubMenu linking to listing pages for any
        associated ModelAdmin instances
        """
        if self.modeladmin_instances:
            submenu = SubMenu(self.get_submenu_items())
            return GroupMenuItem(self, self.get_menu_order(), submenu)


Instruction:
Make a summary of the following Python 3 code
def get_permissions_for_registration(self):
        qs = Permission.objects.none()
        for instance in self.modeladmin_instances:
            qs = qs | instance.get_permissions_for_registration()
        return qs

Response: Returns a queryset of permissions that can be assigned to this class


Instruction:
Write a Python 3 function for
returning a list of admin urls for this model admin.

Response:
def get_admin_urls_for_registration(self):
        """
        Utilised by Wagtail's 'register_admin_urls' hook to register urls for
        used by any associated ModelAdmin instances
        """
        urls = []
        for instance in self.modeladmin_instances:
            urls.extend(instance.get_admin_urls_for_registration())
        return urls


Instruction:
Can you implement a function in Python 3 that
returns True if the menu item is shown.

Response:
def is_shown(self, request):
        """
        If there aren't any visible items in the submenu, don't bother to show
        this menu item
        """
        for menuitem in self.menu._registered_menu_items:
            if menuitem.is_shown(request):
                return True
        return False


Instruction:
Can you create a Python 3 function that
runs TEAL interface for the acscteforwardmodel function.

Response:
def run(configobj=None):
    """
    TEAL interface for the `acscteforwardmodel` function.

    """
    acscteforwardmodel(configobj['input'],
                       exec_path=configobj['exec_path'],
                       time_stamps=configobj['time_stamps'],
                       verbose=configobj['verbose'],
                       quiet=configobj['quiet'],
                       single_core=configobj['single_core']
                       )


Instruction:
Can you generate the documentation for the following Python 3 function
def run(configobj=None):
    acsccd(configobj['input'],
           exec_path=configobj['exec_path'],
           time_stamps=configobj['time_stamps'],
           verbose=configobj['verbose'],
           quiet=configobj['quiet']  #,
           #dqicorr=configobj['dqicorr'],
           #atodcorr=configobj['atodcorr'],
           #blevcorr=configobj['blevcorr'],
           #biascorr=configobj['biascorr']
           )

Response: Run TEAL s acsccd function.


Instruction:
Implement a function in Python 3 to
run TEAL interface for the TEAL module

Response:
def run(configobj=None):
    """TEAL interface for :func:`destripe_plus`."""
    destripe_plus(
        configobj['input'],
        suffix=configobj['suffix'],
        stat=configobj['stat'],
        maxiter=configobj['maxiter'],
        sigrej=configobj['sigrej'],
        lower=configobj['lower'],
        upper=configobj['upper'],
        binwidth=configobj['binwidth'],
        scimask1=configobj['scimask1'],
        scimask2=configobj['scimask2'],
        dqbits=configobj['dqbits'],
        rpt_clean=configobj['rpt_clean'],
        atol=configobj['atol'],
        cte_correct=configobj['cte_correct'],
        clobber=configobj['clobber'],
        verbose=configobj['verbose'])


Instruction:
Create a Python 3 function for
returning a tuple containing a queryset to implement the search and a boolean indicating if the results may contain duplicates.

Response:
def get_search_results(self, request, queryset, search_term):
        """
        Returns a tuple containing a queryset to implement the search,
        and a boolean indicating if the results may contain duplicates.
        """
        # Apply keyword searches.
        def construct_search(field_name):
            if field_name.startswith('^'):
                return "%s__istartswith" % field_name[1:]
            elif field_name.startswith('='):
                return "%s__iexact" % field_name[1:]
            elif field_name.startswith('@'):
                return "%s__search" % field_name[1:]
            else:
                return "%s__icontains" % field_name

        use_distinct = False
        if self.search_fields and search_term:
            orm_lookups = [construct_search(str(search_field))
                           for search_field in self.search_fields]
            for bit in search_term.split():
                or_queries = [models.Q(**{orm_lookup: bit})
                              for orm_lookup in orm_lookups]
                queryset = queryset.filter(reduce(operator.or_, or_queries))
            if not use_distinct:
                for search_spec in orm_lookups:
                    if lookup_needs_distinct(self.opts, search_spec):
                        use_distinct = True
                        break

        return queryset, use_distinct


Instruction:
Implement a function in Python 3 to
return all the parameters except IGNORED_PARAMS

Response:
def get_filters_params(self, params=None):
        """
        Returns all params except IGNORED_PARAMS
        """
        if not params:
            params = self.params
        lookup_params = params.copy()  # a dictionary of the query string
        # Remove all the parameters that are globally and systematically
        # ignored.
        for ignored in IGNORED_PARAMS:
            if ignored in lookup_params:
                del lookup_params[ignored]
        return lookup_params


Instruction:
Can you generate the documentation for the following Python 3 function
def get_ordering_field(self, field_name):
        try:
            field = self.opts.get_field(field_name)
            return field.name
        except FieldDoesNotExist:
            # See whether field_name is a name of a non-field
            # that allows sorting.
            if callable(field_name):
                attr = field_name
            elif hasattr(self.model_admin, field_name):
                attr = getattr(self.model_admin, field_name)
            else:
                attr = getattr(self.model, field_name)
            return getattr(attr, 'admin_order_field', None)

Response: Returns the name of the proper model field that allows sorting.


Instruction:
Explain what the following Python 3 code does
def get_ordering(self, request, queryset):
        params = self.params
        ordering = list(self.get_default_ordering(request))
        if ORDER_VAR in params:
            # Clear ordering and used params
            ordering = []
            order_params = params[ORDER_VAR].split('.')
            for p in order_params:
                try:
                    none, pfx, idx = p.rpartition('-')
                    field_name = self.list_display[int(idx)]
                    order_field = self.get_ordering_field(field_name)
                    if not order_field:
                        continue  # No 'admin_order_field', skip it
                    # reverse order if order_field has already "-" as prefix
                    if order_field.startswith('-') and pfx == "-":
                        ordering.append(order_field[1:])
                    else:
                        ordering.append(pfx + order_field)
                except (IndexError, ValueError):
                    continue  # Invalid ordering specified, skip it.

        # Add the given query's ordering fields, if any.
        ordering.extend(queryset.query.order_by)

        # Ensure that the primary key is systematically present in the list of
        # ordering fields so we can guarantee a deterministic order across all
        # database backends.
        pk_name = self.opts.pk.name
        if not (set(ordering) & {'pk', '-pk', pk_name, '-' + pk_name}):
            # The two sets do not intersect, meaning the pk isn't present. So
            # we add it.
            ordering.append('-pk')

        return ordering

Response: Returns the list of ordering fields for the change list.


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

        # We must cope with more than one column having the same underlying
        # sort field, so we base things on column numbers.
        ordering = self._get_default_ordering()
        ordering_fields = OrderedDict()
        if ORDER_VAR not in self.params:
            # for ordering specified on model_admin or model Meta, we don't
            # know the right column numbers absolutely, because there might be
            # morr than one column associated with that ordering, so we guess.
            for field in ordering:
                if field.startswith('-'):
                    field = field[1:]
                    order_type = 'desc'
                else:
                    order_type = 'asc'
                for index, attr in enumerate(self.list_display):
                    if self.get_ordering_field(attr) == field:
                        ordering_fields[index] = order_type
                        break
        else:
            for p in self.params[ORDER_VAR].split('.'):
                none, pfx, idx = p.rpartition('-')
                try:
                    idx = int(idx)
                except ValueError:
                    continue  # skip it
                ordering_fields[idx] = 'desc' if pfx == '-' else 'asc'
        return ordering_fields

Response: Returns an OrderedDict of ordering field numbers and asc or desc.


Instruction:
Given the following Python 3 function, write the documentation
def get_field_label(self, field_name, field=None):
        label = None
        if field is not None:
            label = getattr(field, 'verbose_name', None)
            if label is None:
                label = getattr(field, 'name', None)
        if label is None:
            label = field_name
        return label.capitalize()

Response: Return a label to display for a field


Instruction:
Can you implement a function in Python 3 that
returns a display value for a field.

Response:
def get_field_display_value(self, field_name, field=None):
        """ Return a display value for a field """

        """
        Firstly, check for a 'get_fieldname_display' property/method on
        the model, and return the value of that, if present.
        """
        val_funct = getattr(self.instance, 'get_%s_display' % field_name, None)
        if val_funct is not None:
            if callable(val_funct):
                return val_funct()
            return val_funct

        """
        Secondly, if we have a real field, we can try to display something
        more useful for it.
        """
        if field is not None:
            try:
                field_type = field.get_internal_type()
                if (
                    field_type == 'ForeignKey' and
                    field.related_model == get_image_model()
                ):
                    # The field is an image
                    return self.get_image_field_display(field_name, field)

                if (
                    field_type == 'ForeignKey' and
                    field.related_model == Document
                ):
                    # The field is a document
                    return self.get_document_field_display(field_name, field)

            except AttributeError:
                pass

        """
        Resort to getting the value of 'field_name' from the instance.
        """
        return getattr(self.instance, field_name,
                       self.model_admin.get_empty_value_display())


Instruction:
Can you generate a brief explanation for the following Python 3 code
def get_image_field_display(self, field_name, field):
        image = getattr(self.instance, field_name)
        if image:
            fltr, _ = Filter.objects.get_or_create(spec='max-400x400')
            rendition = image.get_rendition(fltr)
            return rendition.img_tag
        return self.model_admin.get_empty_value_display()

Response: Get the display of an image field


Instruction:
Explain what the following Python 3 code does
def get_document_field_display(self, field_name, field):
        document = getattr(self.instance, field_name)
        if document:
            return mark_safe(
                '%s (%s, %s)' % (
                    document.url,
                    document.title,
                    document.file_extension.upper(),
                    filesizeformat(document.file.size),
                )
            )
        return self.model_admin.get_empty_value_display()

Response: Render a link to a document field.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def get_dict_for_field(self, field_name):
        try:
            field = self.model._meta.get_field(field_name)
        except FieldDoesNotExist:
            field = None
        return {
            'label': self.get_field_label(field_name, field),
            'value': self.get_field_display_value(field_name, field),
        }

Response:
Returns a dictionary containing label and value values to display
            for a field.


Instruction:
Explain what the following Python 3 code does
def get_fields_dict(self):
        fields = []
        for field_name in self.model_admin.get_inspect_view_fields():
            fields.append(self.get_dict_for_field(field_name))
        return fields

Response:
Return a list of label value dictionaries to represent the
        fiels named by the model_admin class s get_inspect_view_fields method.


Instruction:
Can you write a function in Python 3 where it
generates the actual list of data for the given result.

Response:
def items_for_result(view, result):
    """
    Generates the actual list of data.
    """
    model_admin = view.model_admin
    for field_name in view.list_display:
        empty_value_display = model_admin.get_empty_value_display()
        row_classes = ['field-%s' % field_name]
        try:
            f, attr, value = lookup_field(field_name, result, model_admin)
        except ObjectDoesNotExist:
            result_repr = empty_value_display
        else:
            empty_value_display = getattr(attr, 'empty_value_display', empty_value_display)
            if f is None or f.auto_created:
                allow_tags = getattr(attr, 'allow_tags', False)
                boolean = getattr(attr, 'boolean', False)
                if boolean or not value:
                    allow_tags = True

                if django.VERSION >= (1, 9):
                    result_repr = display_for_value(value, empty_value_display, boolean)
                else:
                    result_repr = display_for_value(value, boolean)
                # Strip HTML tags in the resulting text, except if the
                # function has an "allow_tags" attribute set to True.
                if allow_tags:
                    result_repr = mark_safe(result_repr)
                if isinstance(value, (datetime.date, datetime.time)):
                    row_classes.append('nowrap')
            else:
                if isinstance(f, models.ManyToOneRel):
                    field_val = getattr(result, f.name)
                    if field_val is None:
                        result_repr = empty_value_display
                    else:
                        result_repr = field_val
                else:
                    if django.VERSION >= (1, 9):
                        result_repr = display_for_field(value, f, empty_value_display)
                    else:
                        result_repr = display_for_field(value, f)

                if isinstance(f, (models.DateField, models.TimeField, models.ForeignKey)):
                    row_classes.append('nowrap')
        if force_text(result_repr) == '':
            result_repr = mark_safe(' ')
        row_classes.extend(model_admin.get_extra_class_names_for_field_col(field_name, result))
        row_attributes_dict = model_admin.get_extra_attrs_for_field_col(field_name, result)
        row_attributes_dict['class'] = ' ' . join(row_classes)
        row_attributes = ''.join(' %s="%s"' % (key, val) for key, val in row_attributes_dict.items())
        row_attributes_safe = mark_safe(row_attributes)
        yield format_html('<td{}>{}</td>', row_attributes_safe, result_repr)


Instruction:
Explain what the following Python 3 code does
def result_list(context):
    view = context['view']
    object_list = context['object_list']
    headers = list(result_headers(view))
    num_sorted_fields = 0
    for h in headers:
        if h['sortable'] and h['sorted']:
            num_sorted_fields += 1
    context.update({
        'result_headers': headers,
        'num_sorted_fields': num_sorted_fields,
        'results': list(results(view, object_list))})
    return context

Response: Displays the headers and data list together


Instruction:
Create a Python 3 function to
run TEAL acssum function.

Response:
def run(configobj=None):
    """
    TEAL interface for the `acssum` function.

    """
    acssum(configobj['input'],
           configobj['output'],
           exec_path=configobj['exec_path'],
           time_stamps=configobj['time_stamps'],
           verbose=configobj['verbose'],
           quiet=configobj['quiet']
           )


Instruction:
Make a summary of the following Python 3 code
def _detsat_one(filename, ext, sigma=2.0, low_thresh=0.1, h_thresh=0.5,
                small_edge=60, line_len=200, line_gap=75,
                percentile=(4.5, 93.0), buf=200, plot=False, verbose=False):
    """Called by :func:`detsat`."""
    if verbose:
        t_beg = time.time()

    fname = '{0}[{1}]'.format(filename, ext)

    # check extension
    if ext not in (1, 4, 'SCI', ('SCI', 1), ('SCI', 2)):
        warnings.warn('{0} is not a valid science extension for '
                      'ACS/WFC'.format(ext), AstropyUserWarning)

    # get the data
    image = fits.getdata(filename, ext)
    # image = im.astype('float64')

    # rescale the image
    p1, p2 = np.percentile(image, percentile)

    # there should always be some counts in the image, anything lower should
    # be set to one. Makes things nicer for finding edges.
    if p1 < 0:
        p1 = 0.0

    if verbose:
        print('Rescale intensity percentiles: {0}, {1}'.format(p1, p2))

    image = exposure.rescale_intensity(image, in_range=(p1, p2))

    # get the edges
    immax = np.max(image)
    edge = canny(image, sigma=sigma,
                 low_threshold=immax * low_thresh,
                 high_threshold=immax * h_thresh)

    # clean up the small objects, will make less noise
    morph.remove_small_objects(edge, min_size=small_edge, connectivity=8,
                               in_place=True)

    # create an array of angles from 0 to 180, exactly 0 will get bad columns
    # but it is unlikely that a satellite will be exactly at 0 degrees, so
    # don't bother checking.
    # then, convert to radians.
    angle = np.radians(np.arange(2, 178, 0.5, dtype=float))

    # perform Hough Transform to detect straight lines.
    # only do if plotting to visualize the image in hough space.
    # otherwise just preform a Probabilistic Hough Transform.
    if plot and plt is not None:
        h, theta, d = transform.hough_line(edge, theta=angle)
        plt.ion()

    # perform Probabilistic Hough Transformation to get line segments.
    # NOTE: Results are slightly different from run to run!
    result = transform.probabilistic_hough_line(
        edge, threshold=210, line_length=line_len,
        line_gap=line_gap, theta=angle)
    result = np.asarray(result)
    n_result = len(result)

    # initially assume there is no satellite
    satellite = False

    # only continue if there was more than one point (at least a line)
    # returned from the PHT
    if n_result > 1:
        if verbose:
            print('Length of PHT result: {0}'.format(n_result))

        # create lists for X and Y positions of lines and build points
        x0 = result[:, 0, 0]
        y0 = result[:, 0, 1]
        x1 = result[:, 1, 0]
        y1 = result[:, 1, 1]

        # set some boundries
        ymax, xmax = image.shape
        topx = xmax - buf
        topy = ymax - buf

        if verbose:
            print('min(x0)={0:4d}, min(x1)={1:4d}, min(y0)={2:4d}, '
                  'min(y1)={3:4d}'.format(min(x0), min(x1), min(y0), min(y1)))
            print('max(x0)={0:4d}, max(x1)={1:4d}, max(y0)={2:4d}, '
                  'max(y1)={3:4d}'.format(max(x0), max(x1), max(y0), max(y1)))
            print('buf={0}'.format(buf))
            print('topx={0}, topy={1}'.format(topx, topy))

        # set up trail angle "tracking" arrays.
        # find the angle of each segment and filter things out.
        # TODO: this may be wrong. Try using arctan2.
        trail_angle = np.degrees(np.arctan((y1 - y0) / (x1 - x0)))
        # round to the nearest 5 degrees, trail should not be that curved
        round_angle = (5 * np.round(trail_angle * 0.2)).astype(int)

        # take out 90 degree things
        mask = round_angle % 90 != 0

        if not np.any(mask):
            if verbose:
                print('No round_angle found')
            return np.empty(0)

        round_angle = round_angle[mask]
        trail_angle = trail_angle[mask]
        result = result[mask]

        ang, num = stats.mode(round_angle)

        # do the filtering
        truth = round_angle == ang[0]

        if verbose:
            print('trail_angle: {0}'.format(trail_angle))
            print('round_angle: {0}'.format(round_angle))
            print('mode(round_angle): {0}'.format(ang[0]))

        # filter out the outliers
        trail_angle = trail_angle[truth]
        result = result[truth]
        n_result = len(result)

        if verbose:
            print('Filtered trail_angle: {0}'.format(trail_angle))

        if n_result < 1:
            return np.empty(0)

        # if there is an unreasonable amount of points, it picked up garbage
        elif n_result > 300:
            warnings.warn(
                'Way too many segments results to be correct ({0}). '
                'Rejecting detection on {1}.'.format(n_result, fname),
                AstropyUserWarning)
            return np.empty(0)

        # remake the point lists with things taken out
        x0 = result[:, 0, 0]
        y0 = result[:, 0, 1]
        x1 = result[:, 1, 0]
        y1 = result[:, 1, 1]

        min_x0 = min(x0)
        min_y0 = min(y0)
        min_x1 = min(x1)
        min_y1 = min(y1)

        max_x0 = max(x0)
        max_y0 = max(y0)
        max_x1 = max(x1)
        max_y1 = max(y1)

        mean_angle = np.mean(trail_angle)

        # make decisions on where the trail went and determine if a trail
        # traversed the image
        # top to bottom
        if (((min_y0 < buf) or (min_y1 < buf)) and
                ((max_y0 > topy) or (max_y1 > topy))):
            satellite = True
            if verbose:
                print('Trail Direction: Top to Bottom')

        # right to left
        elif (((min_x0 < buf) or (min_x1 < buf)) and
              ((max_x0 > topx) or (max_x1 > topx))):
            satellite = True
            if verbose:
                print('Trail Direction: Right to Left')

        # bottom to left
        elif (((min_x0 < buf) or (min_x1 < buf)) and
              ((min_y0 < buf) or (min_y1 < buf)) and
              (-1 > mean_angle > -89)):
            satellite = True
            if verbose:
                print('Trail Direction: Bottom to Left')

        # top to left
        elif (((min_x0 < buf) or (min_x1 < buf)) and
              ((max_y0 > topy) or (max_y1 > topy)) and
              (89 > mean_angle > 1)):
            satellite = True
            if verbose:
                print('Trail Direction: Top to Left')

        # top to right
        elif (((max_x0 > topx) or (max_x1 > topx)) and
              ((max_y0 > topy) or (max_y1 > topy)) and
              (-1 > mean_angle > -89)):
            satellite = True
            if verbose:
                print('Trail Direction: Top to Right')

        # bottom to right
        elif (((max_x0 > topx) or (max_x1 > topx)) and
              ((min_y0 < buf) or (min_y1 < buf)) and
              (89 > mean_angle > 1)):
            satellite = True
            if verbose:
                print('Trail Direction: Bottom to Right')

    if satellite:
        if verbose:
            print('{0} trail segment(s) detected'.format(n_result))
            print('Trail angle list (not returned): ')
            print(trail_angle)
            print('End point list:')
            for i, ((px0, py0), (px1, py1)) in enumerate(result, 1):
                print('{0:5d}. ({1:4d}, {2:4d}), ({3:4d}, {4:4d})'.format(
                    i, px0, py0, px1, py1))

        if plot and plt is not None:
            mean = np.median(image)
            stddev = image.std()
            lower = mean - stddev
            upper = mean + stddev

            fig1, ax1 = plt.subplots()
            ax1.imshow(edge, cmap=plt.cm.gray)
            ax1.set_title('Edge image for {0}'.format(fname))

            for (px0, py0), (px1, py1) in result:  # Draw trails
                ax1.plot((px0, px1), (py0, py1), scalex=False, scaley=False)

            fig2, ax2 = plt.subplots()
            ax2.imshow(
                np.log(1 + h),
                extent=(np.rad2deg(theta[-1]), np.rad2deg(theta[0]),
                        d[-1], d[0]), aspect=0.02)
            ax2.set_title('Hough Transform')
            ax2.set_xlabel('Angles (degrees)')
            ax2.set_ylabel('Distance from Origin (pixels)')

            fig3, ax3 = plt.subplots()
            ax3.imshow(image, vmin=lower, vmax=upper, cmap=plt.cm.gray)
            ax3.set_title(fname)

            for (px0, py0), (px1, py1) in result:  # Draw trails
                ax3.plot((px0, px1), (py0, py1), scalex=False, scaley=False)

            plt.draw()

    else:  # length of result was too small
        result = np.empty(0)

        if verbose:
            print('No trail detected; found {0} segments'.format(n_result))

        if plot and plt is not None:
            fig1, ax1 = plt.subplots()
            ax1.imshow(edge, cmap=plt.cm.gray)
            ax1.set_title(fname)

            # Draw trails
            for (px0, py0), (px1, py1) in result:
                ax1.plot((px0, px1), (py0, py1), scalex=False, scaley=False)

    if verbose:
        t_end = time.time()
        print('Run time: {0} s'.format(t_end - t_beg))

    return result

Response: This function is called by detsat. It will take a file and the extension and detsat it and return the image.


Instruction:
Explain what the following Python 3 code does
def _get_valid_indices(shape, ix0, ix1, iy0, iy1):
    ymax, xmax = shape

    if ix0 < 0:
        ix0 = 0
    if ix1 > xmax:
        ix1 = xmax
    if iy0 < 0:
        iy0 = 0
    if iy1 > ymax:
        iy1 = ymax

    if iy1 <= iy0 or ix1 <= ix0:
        raise IndexError(
            'array[{0}:{1},{2}:{3}] is invalid'.format(iy0, iy1, ix0, ix1))

    return list(map(int, [ix0, ix1, iy0, iy1]))

Response:
Give array shape and desired indices return indices that are
    correctly bounded by the shape.


Instruction:
Implement a Python 3 function for
transforming a point from original image coordinates to rotated image coordinates and back. It assumes the rotation point is the center of an image. This works on a simple rotation transformation:: newx = (startx) * np.cos(angle) - (starty) * np.sin(angle) newy = (startx) * np.sin(angle) + (starty) * np.cos(angle) It takes into account the differences in image size. Parameters ---------- point : tuple Point to be rotated, in the format of ``(x, y)`` measured from origin. angle : float The angle in degrees to rotate the point by as measured counter-clockwise from the X axis. ishape : tuple The shape of the original image, taken from ``image.shape``. rshape : tuple The shape of the rotated image, in the form of ``rotate.shape``. reverse : bool, optional Transform from rotated coordinates back to non-rotated image. Returns ------- rotated_point : tuple Rotated point in the format of ``(x, y)`` as measured from origin.

Response:
def _rotate_point(point, angle, ishape, rshape, reverse=False):
    """Transform a point from original image coordinates to rotated image
    coordinates and back. It assumes the rotation point is the center of an
    image.

    This works on a simple rotation transformation::

        newx = (startx) * np.cos(angle) - (starty) * np.sin(angle)
        newy = (startx) * np.sin(angle) + (starty) * np.cos(angle)

    It takes into account the differences in image size.

    Parameters
    ----------
    point : tuple
        Point to be rotated, in the format of ``(x, y)`` measured from
        origin.

    angle : float
        The angle in degrees to rotate the point by as measured
        counter-clockwise from the X axis.

    ishape : tuple
        The shape of the original image, taken from ``image.shape``.

    rshape : tuple
        The shape of the rotated image, in the form of ``rotate.shape``.

    reverse : bool, optional
        Transform from rotated coordinates back to non-rotated image.

    Returns
    -------
    rotated_point : tuple
        Rotated point in the format of ``(x, y)`` as measured from origin.

    """
    #  unpack the image and rotated images shapes
    if reverse:
        angle = (angle * -1)
        temp = ishape
        ishape = rshape
        rshape = temp

    # transform into center of image coordinates
    yhalf, xhalf = ishape
    yrhalf, xrhalf = rshape

    yhalf = yhalf / 2
    xhalf = xhalf / 2
    yrhalf = yrhalf / 2
    xrhalf = xrhalf / 2

    startx = point[0] - xhalf
    starty = point[1] - yhalf

    # do the rotation
    newx = startx * np.cos(angle) - starty * np.sin(angle)
    newy = startx * np.sin(angle) + starty * np.cos(angle)

    # add back the padding from changing the size of the image
    newx = newx + xrhalf
    newy = newy + yrhalf

    return (newx, newy)