Instruction:
Can you implement a function in Python 3 that
gets uncertainty of a period. The uncertainty is defined as the half width of the frequencies around the peak, that becomes lower than average + standard deviation of the power spectrum. Since we may not have fine resolution around the peak, we do not assume it is gaussian. So, no scaling factor of 2.355 (= 2 * sqrt(2 * ln2)) is applied. Parameters ---------- fx : array_like An array of frequencies. fy : array_like An array of amplitudes. jmax : int An index at the peak frequency. fx_width : int, optional Width of power spectrum to calculate uncertainty. Returns ------- p_uncertain : float Period uncertainty.

Response:
def get_period_uncertainty(self, fx, fy, jmax, fx_width=100):
        """
        Get uncertainty of a period.

        The uncertainty is defined as the half width of the frequencies
        around the peak, that becomes lower than average + standard deviation
        of the power spectrum.

        Since we may not have fine resolution around the peak,
        we do not assume it is gaussian. So, no scaling factor of
        2.355 (= 2 * sqrt(2 * ln2)) is applied.

        Parameters
        ----------
        fx : array_like
            An array of frequencies.
        fy : array_like
            An array of amplitudes.
        jmax : int
            An index at the peak frequency.
        fx_width : int, optional
            Width of power spectrum to calculate uncertainty.

        Returns
        -------
        p_uncertain : float
            Period uncertainty.
        """

        # Get subset
        start_index = jmax - fx_width
        end_index = jmax + fx_width
        if start_index < 0:
            start_index = 0
        if end_index > len(fx) - 1:
            end_index = len(fx) - 1

        fx_subset = fx[start_index:end_index]
        fy_subset = fy[start_index:end_index]
        fy_mean = np.median(fy_subset)
        fy_std = np.std(fy_subset)

        # Find peak
        max_index = np.argmax(fy_subset)

        # Find list whose powers become lower than average + std.
        index = np.where(fy_subset <= fy_mean + fy_std)[0]

        # Find the edge at left and right. This is the full width.
        left_index = index[(index < max_index)]
        if len(left_index) == 0:
            left_index = 0
        else:
            left_index = left_index[-1]
        right_index = index[(index > max_index)]
        if len(right_index) == 0:
            right_index = len(fy_subset) - 1
        else:
            right_index = right_index[0]

        # We assume the half of the full width is the period uncertainty.
        half_width = (1. / fx_subset[left_index]
                      - 1. / fx_subset[right_index]) / 2.
        period_uncertainty = half_width

        return period_uncertainty


Instruction:
Can you create a Python 3 function that
returns the residuals of the Fourier Series.

Response:
def residuals(self, pars, x, y, order):
        """
        Residual of Fourier Series.

        Parameters
        ----------
        pars : array_like
            Fourier series parameters.
        x : array_like
            An array of date.
        y : array_like
            An array of true values to fit.
        order : int
            An order of Fourier Series.
        """

        return y - self.fourier_series(pars, x, order)


Instruction:
Implement a function in Python 3 to
function to fit Fourier Series.

Response:
def fourier_series(self, pars, x, order):
        """
        Function to fit Fourier Series.

        Parameters
        ----------
        x : array_like
            An array of date divided by period. It doesn't need to be sorted.
        pars :  array_like
            Fourier series parameters.
        order : int
            An order of Fourier series.
        """

        sum = pars[0]
        for i in range(order):
            sum += pars[i * 2 + 1] * np.sin(2 * np.pi * (i + 1) * x) \
                   + pars[i * 2 + 2] * np.cos(2 * np.pi * (i + 1) * x)

        return sum


Instruction:
Can you generate the documentation for the following Python 3 function
def get_stetson_k(self, mag, avg, err):

        residual = (mag - avg) / err
        stetson_k = np.sum(np.fabs(residual)) \
                    / np.sqrt(np.sum(residual * residual)) / np.sqrt(len(mag))

        return stetson_k

Response: Returns the Stetson K feature.


Instruction:
Write a Python 3 function for
returning ratio of amplitude of higher and lower magnitudes than average.

Response:
def half_mag_amplitude_ratio(self, mag, avg, weight):
        """
        Return ratio of amplitude of higher and lower magnitudes.

        A ratio of amplitude of higher and lower magnitudes than average,
        considering weights. This ratio, by definition, should be higher
        for EB than for others.

        Parameters
        ----------
        mag : array_like
            An array of magnitudes.
        avg : float
            An average value of magnitudes.
        weight : array_like
            An array of weight.

        Returns
        -------
        hl_ratio : float
            Ratio of amplitude of higher and lower magnitudes than average.
        """

        # For lower (fainter) magnitude than average.
        index = np.where(mag > avg)
        lower_weight = weight[index]
        lower_weight_sum = np.sum(lower_weight)
        lower_mag = mag[index]
        lower_weighted_std = np.sum((lower_mag
                                     - avg) ** 2 * lower_weight) / \
                             lower_weight_sum

        # For higher (brighter) magnitude than average.
        index = np.where(mag <= avg)
        higher_weight = weight[index]
        higher_weight_sum = np.sum(higher_weight)
        higher_mag = mag[index]
        higher_weighted_std = np.sum((higher_mag
                                      - avg) ** 2 * higher_weight) / \
                              higher_weight_sum

        # Return ratio.
        return np.sqrt(lower_weighted_std / higher_weighted_std)


Instruction:
Given the following Python 3 function, write the documentation
def half_mag_amplitude_ratio2(self, mag, avg):

        # For lower (fainter) magnitude than average.
        index = np.where(mag > avg)
        fainter_mag = mag[index]

        lower_sum = np.sum((fainter_mag - avg) ** 2) / len(fainter_mag)

        # For higher (brighter) magnitude than average.
        index = np.where(mag <= avg)
        brighter_mag = mag[index]

        higher_sum = np.sum((avg - brighter_mag) ** 2) / len(brighter_mag)

        # Return ratio.
        return np.sqrt(lower_sum / higher_sum)

Response: Return ratio of amplitude of higher and lower magnitudes than average.


Instruction:
Can you tell what is the following Python 3 function doing
def get_eta(self, mag, std):

        diff = mag[1:] - mag[:len(mag) - 1]
        eta = np.sum(diff * diff) / (len(mag) - 1.) / std / std

        return eta

Response: Returns the value of the Eta feature.


Instruction:
Create a Python 3 function for
calculating 10 and 90% percentile of slope.

Response:
def slope_percentile(self, date, mag):
        """
        Return 10% and 90% percentile of slope.

        Parameters
        ----------
        date : array_like
            An array of phase-folded date. Sorted.
        mag : array_like
            An array of phase-folded magnitudes. Sorted by date.

        Returns
        -------
        per_10 : float
            10% percentile values of slope.
        per_90 : float
            90% percentile values of slope.
        """

        date_diff = date[1:] - date[:len(date) - 1]
        mag_diff = mag[1:] - mag[:len(mag) - 1]

        # Remove zero mag_diff.
        index = np.where(mag_diff != 0.)
        date_diff = date_diff[index]
        mag_diff = mag_diff[index]

        # Derive slope.
        slope = date_diff / mag_diff

        percentile_10 = np.percentile(slope, 10.)
        percentile_90 = np.percentile(slope, 90.)

        return percentile_10, percentile_90


Instruction:
Make a summary of the following Python 3 code
def get_cusum(self, mag):

        c = np.cumsum(mag - self.weighted_mean) / len(mag) / self.weighted_std

        return np.max(c) - np.min(c)

Response: Returns the cumulative sum of the entry in the set of magnitudes.


Instruction:
Create a Python 3 function for
returning all features with its names and values.

Response:
def get_features2(self):
        """
        Return all features with its names.

        Returns
        -------
        names : list
            Feature names.
        values : list
            Feature values
        """

        feature_names = []
        feature_values = []

        # Get all the names of features.
        all_vars = vars(self)
        for name in all_vars.keys():
            # Omit input variables such as date, mag, err, etc.
            if not (name == 'date' or name == 'mag' or name == 'err'
                    or name == 'n_threads' or name == 'min_period'):
                # Filter some other unnecessary features.
                if not (name == 'f' or name == 'f_phase'
                        or name == 'period_log10FAP'
                        or name == 'weight' or name == 'weighted_sum'
                        or name == 'median' or name == 'mean' or name == 'std'):
                    feature_names.append(name)

        # Sort by the names.
        # Sorting should be done to keep maintaining the same order of features.
        feature_names.sort()

        # Get feature values.
        for name in feature_names:
            feature_values.append(all_vars[name])

        return feature_names, feature_values


Instruction:
Write a Python 3 function that can
return all features with its names.

Response:
def get_features_all(self):
        """
        Return all features with its names.

        Regardless of being used for train and prediction. Sorted by the names.

        Returns
        -------
        all_features : OrderedDict
            Features dictionary.
        """

        features = {}

        # Get all the names of features.
        all_vars = vars(self)
        for name in all_vars.keys():
            if name in feature_names_list_all:
                features[name] = all_vars[name]

        # Sort by the keys (i.e. feature names).
        features = OrderedDict(sorted(features.items(), key=lambda t: t[0]))

        return features


Instruction:
How would you explain what the following Python 3 function does
def init(device_id=None, random_seed=None):

    if device_id is None:
        random_seed = _os.environ.get('CUDA_DEVICE')

    if random_seed is None:
        random_seed = _os.environ.get('RANDOM_SEED')

    global is_initialized
    if not is_initialized:
        is_initialized = True

        global context
        context.init_context(device_id)

        from pycuda import gpuarray, driver, curandom

        # Initialize memory pool
        global memory_pool
        memory_pool.init()

        # Initialize PRG
        global sampler
        sampler.set_seed(random_seed)

        # Initialize pycuda_ops
        from hebel import pycuda_ops
        pycuda_ops.init()

Response: Initialize the CUDA context and CUBLAS context and CUBLAS context and CUBLAS context and CUBLAS context and CUBLAS context and CUBLAS context and CUBLAS context and CUBLAS context and CUBLAS context and CUBLAS context and CUBLAS context and CUBLAS context and CUBLAS context and CUBLAS context and CUBLAS context and CUBLAS context.


Instruction:
Here you have a function in Python 3, explain what it does
def inflate_context_tuple(ast_rootpath, root_env):
  with util.LogTime('inflate_context_tuple'):
    # We only need to look at tuple members going down.
    inflated = ast_rootpath[0].eval(root_env)
    current = inflated
    env = root_env
    try:
      for node in ast_rootpath[1:]:
        if is_tuple_member_node(node):
          assert framework.is_tuple(current)
          with util.LogTime('into tuple'):
            thunk, env = inflated.get_thunk_env(node.name)
            current = framework.eval(thunk, env)

        elif framework.is_list(current):
          with util.LogTime('eval thing'):
            current = framework.eval(node, env)

        if framework.is_tuple(current):
          inflated = current
    except (gcl.EvaluationError, ast.UnparseableAccess):
      # Eat evaluation error, probably means the rightmost tuplemember wasn't complete.
      # Return what we have so far.
      pass
    return inflated

Response: Instantiate a tuple from a TupleNode.


Instruction:
Can you tell what is the following Python 3 function doing
def enumerate_scope(ast_rootpath, root_env=None, include_default_builtins=False):
  with util.LogTime('enumerate_scope'):
    scope = {}
    for node in reversed(ast_rootpath):
      if is_tuple_node(node):
        for member in node.members:
          if member.name not in scope:
            scope[member.name] = Completion(member.name, False, member.comment.as_string(), member.location)

    if include_default_builtins:  # Backwards compat flag
      root_env = gcl.default_env

    if root_env:
      for k in root_env.keys():
        if k not in scope and not hide_from_autocomplete(root_env[k]):
          v = root_env[k]
          scope[k] = Completion(k, True, dedent(v.__doc__ or ''), None)

    return scope

Response: Enumerates all keys that are in scope in a given tuple.


Instruction:
How would you implement a function in Python 3 that
returns a dict of all completions that are declared in the given AST rootpath.

Response:
def find_deref_completions(ast_rootpath, root_env=gcl.default_env):
  """Returns a dict of { name => Completions }."""
  with util.LogTime('find_deref_completions'):
    tup = inflate_context_tuple(ast_rootpath, root_env)
    path = path_until(ast_rootpath, is_deref_node)
    if not path:
      return {}
    deref = path[-1]
    haystack = deref.haystack(tup.env(tup))
    if not hasattr(haystack, 'keys'):
      return {}
    return {n: get_completion(haystack, n) for n in haystack.keys()}


Instruction:
Can you tell what is the following Python 3 function doing
def is_identifier_position(rootpath):
  if len(rootpath) >= 2 and is_tuple_member_node(rootpath[-2]) and is_identifier(rootpath[-1]):
    return True
  if len(rootpath) >= 1 and is_tuple_node(rootpath[-1]):
    # No deeper node than tuple? Must be identifier position, otherwise we'd have a TupleMemberNode.
    return True
  return False

Response: Return whether the cursor is in identifier - position in a member declaration.


Instruction:
Can you write a function in Python 3 where it
finds completions at the cursor.

Response:
def find_completions_at_cursor(ast_tree, filename, line, col, root_env=gcl.default_env):
  """Find completions at the cursor.

  Return a dict of { name => Completion } objects.
  """
  q = gcl.SourceQuery(filename, line, col - 1)
  rootpath = ast_tree.find_tokens(q)

  if is_identifier_position(rootpath):
    return find_inherited_key_completions(rootpath, root_env)

  try:
    ret = find_deref_completions(rootpath, root_env) or enumerate_scope(rootpath, root_env=root_env)
    assert isinstance(ret, dict)
    return ret
  except gcl.EvaluationError:
    # Probably an unbound value or something--just return an empty list
    return {}


Instruction:
Can you tell what is the following Python 3 function doing
def find_inherited_key_completions(rootpath, root_env):
  tup = inflate_context_tuple(rootpath, root_env)
  if isinstance(tup, runtime.CompositeTuple):
    keys = set(k for t in tup.tuples[:-1] for k in t.keys())
    return {n: get_completion(tup, n) for n in keys}
  return {}

Response: Return the completions of INHERITED tuples.


Instruction:
Here you have a function in Python 3, explain what it does
def find_value_at_cursor(ast_tree, filename, line, col, root_env=gcl.default_env):
  q = gcl.SourceQuery(filename, line, col)
  rootpath = ast_tree.find_tokens(q)
  rootpath = path_until(rootpath, is_thunk)

  if len(rootpath) <= 1:
    # Just the file tuple itself, or some non-thunk element at the top level
    return None

  tup = inflate_context_tuple(rootpath, root_env)
  try:
    if isinstance(rootpath[-1], ast.Inherit):
      # Special case handling of 'Inherit' nodes, show the value that's being
      # inherited.
      return tup[rootpath[-1].name]
    return rootpath[-1].eval(tup.env(tup))
  except gcl.EvaluationError as e:
    return e

Response: Find the value of the object under the cursor.


Instruction:
Can you tell what is the following Python 3 function doing
def add_vec_to_mat(mat, vec, axis=None, inplace=False,
                   target=None, substract=False):
    """ Add a vector to a matrix
    """

    assert mat.flags.c_contiguous

    if axis is None:
        if vec.shape[0] == mat.shape[0]:
            axis = 0
        elif vec.shape[0] == mat.shape[1]:
            axis = 1
        else:
            raise ValueError('Vector length must be equal '
                             'to one side of the matrix')

    n, m = mat.shape

    block = (_compilation_constants['add_vec_block_size'],
             _compilation_constants['add_vec_block_size'], 1)
    gridx = ceil_div(n, block[0])
    gridy = ceil_div(m, block[1])
    grid = (gridx, gridy, 1)

    if inplace:
        target = mat
    elif target is None:
        target = gpuarray.empty_like(mat)

    if axis == 0:
        assert vec.shape[0] == mat.shape[0]
        add_col_vec_kernel.prepared_call(
            grid, block,
            mat.gpudata,
            vec.gpudata,
            target.gpudata,
            np.uint32(n),
            np.uint32(m),
            np.int32(substract))
    elif axis == 1:
        assert vec.shape[0] == mat.shape[1]
        add_row_vec_kernel.prepared_call(
            grid, block,
            mat.gpudata,
            vec.gpudata,
            target.gpudata,
            np.uint32(n),
            np.uint32(m),
            np.int32(substract))
    return target

Response: Add a vector to a matrix.


Instruction:
Make a summary of the following Python 3 code
def vector_normalize(mat, max_vec_norm=1.):
    assert mat.flags.c_contiguous
    n, m = mat.shape

    vector_normalize_kernel.prepared_call(
        (m, 1, 1), (32, 1, 1),
        mat.gpudata,
        np.float32(max_vec_norm),
        np.int32(m),
        np.int32(n))

Response: Normalize each column vector in mat to length max_vec_norm.


Instruction:
Here you have a function in Python 3, explain what it does
def preprocess(string):

    split = string.split('${')

    rval = [split[0]]

    for candidate in split[1:]:
        subsplit = candidate.split('}')

        if len(subsplit) < 2:
            raise ValueError('Open ${ not followed by } before ' \
                    + 'end of string or next ${ in "' \
                    + string + '"')

        varname = subsplit[0]

        if varname == 'PYLEARN2_TRAIN_FILE_NAME':
            warnings.warn("PYLEARN2_TRAIN_FILE_NAME is deprecated and may be "
                    "removed from the library on or after Oct 22, 2013. Switch"
                    " to PYLEARN2_TRAIN_FILE_FULL_STEM")

        try:
            val = os.environ[varname]
        except KeyError:
            if varname == 'PYLEARN2_DATA_PATH':
                raise NoDataPathError()
            if varname == 'PYLEARN2_VIEWER_COMMAND':
                raise EnvironmentVariableError(environment_variable_essay)

            raise ValueError('Unrecognized environment variable "' + varname
                    + '". Did you mean ' + match(varname, os.environ.keys())
                    + '?')

        rval.append(val)

        rval.append('}'.join(subsplit[1:]))

    rval = ''.join(rval)

    return rval

Response: Preprocesses a string by replacing the VARNAME with the value of the environment variable PYLEARN2_TRAIN_FILE_FULL_STEM.


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

    r = find_number(s)

    if r == None:
        return [ s ]
    else:
        tokens = []
        if r[0] > 0:
            tokens.append(s[0:r[0]])
        tokens.append( float(s[r[0]:r[1]]) )
        if r[1] < len(s):
            tokens.extend(tokenize_by_number(s[r[1]:]))
        return tokens
    assert False

Response:
splits a string into a list of tokens
AttributeNames each is either a string containing no numbers
AttributeNames or a float


Instruction:
Create a Python 3 function for
wronging a mispelling candidates: a set of correct words returns a guess of which candidate is the right one This should be used with a small number of candidates and a high potential edit distance. ie, use it to correct a wrong filename in a directory, wrong class name in a module, etc. Don't use it to correct small typos of freeform natural language words.

Response:
def match(wrong, candidates):
    """
        wrong: a mispelling
        candidates: a set of correct words

        returns a guess of which candidate is the right one

        This should be used with a small number of candidates and a high potential
        edit distance.
        ie, use it to correct a wrong filename in a directory, wrong class name
        in a module, etc. Don't use it to correct small typos of freeform natural
        language words.
    """

    assert len(candidates) > 0

    # Current implementation tries all candidates and outputs the one
    # with the min score
    # Could try to do something smarter

    def score(w1,w2):
        # Current implementation returns negative dot product of
        # the two words mapped into a feature space by mapping phi
        # w -> [ phi(w1), .1 phi(first letter of w), .1 phi(last letter of w) ]
        # Could try to do something smarter

        w1 = w1.lower()
        w2 = w2.lower()

        def phi(w):
            # Current feature mapping is to the vector of counts of
            # all letters and two-letter sequences
            # Could try to do something smarter
            rval = {}

            for i in xrange(len(w)):
                l = w[i]
                rval[l] = rval.get(l,0.) + 1.
                if i < len(w)-1:
                    b = w[i:i+2]
                    rval[b] = rval.get(b,0.) + 1.

            return rval

        d1 = phi(w1)
        d2 = phi(w2)

        def mul(d1, d2):
            rval = 0

            for key in set(d1).union(d2):
                rval += d1.get(key,0) * d2.get(key,0)

            return rval

        tot_score = mul(phi(w1),phi(w2)) / float(len(w1)*len(w2)) + \
            0.1 * mul(phi(w1[0:1]), phi(w2[0:1])) + \
            0.1 * mul(phi(w1[-1:]), phi(w2[-1:]))

        return  tot_score

    scored_candidates = [ (-score(wrong, candidate), candidate)
            for candidate in candidates ]

    scored_candidates.sort()

    return scored_candidates[0][1]


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

    def censor(ch):
        if (ch >= 'A' and ch <= 'z') or (ch >= '0' and ch <= '9'):
            return ch
        return '*'

    return ''.join([censor(ch) for ch in s])

Response: Returns a string with all non - alphanumeric characters replaced with *


Instruction:
How would you code a function in Python 3 to
check if a given period is possibly an alias.

Response:
def is_period_alias(period):
    """
    Check if a given period is possibly an alias.

    Parameters
    ----------
    period : float
        A period to test if it is a possible alias or not.

    Returns
    -------
    is_alias : boolean
        True if the given period is in a range of period alias.
    """

    # Based on the period vs periodSN plot of EROS-2 dataset (Kim+ 2014).
    # Period alias occurs mostly at ~1 and ~30.
    # Check each 1, 2, 3, 4, 5 factors.
    for i in range(1, 6):
        # One-day and one-month alias
        if (.99 / float(i)) < period < (1.004 / float(i)):
            return True
        if (1.03 / float(i)) < period < (1.04 / float(i)):
            return True
        if (29.2 / float(i)) < period < (29.9 / float(i)):
            return True

        # From candidates from the two fields 01, 08.
        # All of them are close to one day (or sidereal) alias.
        if (0.96465 / float(i)) < period < (0.96485 / float(i)):
            return True
        if (0.96725 / float(i)) < period < (0.96745 / float(i)):
            return True
        if (0.98190 / float(i)) < period < (0.98230 / float(i)):
            return True
        if (1.01034 / float(i)) < period < (1.01076 / float(i)):
            return True
        if (1.01568 / float(i)) < period < (1.01604 / float(i)):
            return True
        if (1.01718 / float(i)) < period < (1.01742 / float(i)):
            return True

        # From the all candidates from the entire LMC fields.
        # Some of these could be overlapped with the above cuts.
        if (0.50776 / float(i)) < period < (0.50861 / float(i)):
            return True
        if (0.96434 / float(i)) < period < (0.9652 / float(i)):
            return True
        if (0.96688 / float(i)) < period < (0.96731 / float(i)):
            return True
        if (1.0722 / float(i)) < period < (1.0729 / float(i)):
            return True
        if (27.1 / float(i)) < period < (27.5 / float(i)):
            return True

    # Not in the range of any alias.
    return False


Instruction:
Write a Python 3 function for
serializing object to a file denoted by filepath.

Response:
def save(filepath, obj, on_overwrite = 'ignore'):
    """
    Serialize `object` to a file denoted by `filepath`.

    Parameters
    ----------
    filepath : str
        A filename. If the suffix is `.joblib` and joblib can be
        imported, `joblib.dump` is used in place of the regular
        pickling mechanisms; this results in much faster saves by
        saving arrays as separate .npy files on disk. If the file
        suffix is `.npy` than `numpy.save` is attempted on `obj`.
        Otherwise, (c)pickle is used.

    obj : object
        A Python object to be serialized.

    on_overwrite: A string specifying what to do if the file already
                exists.
                ignore: just overwrite it
                backup: make a copy of the file (<filepath>.bak) and
                        delete it when done saving the new copy.
                        this allows recovery of the old version of
                        the file if saving the new one fails
    """

    filepath = preprocess(filepath)

    if os.path.exists(filepath):
        if on_overwrite == 'backup':
            backup = filepath + '.bak'
            shutil.move(filepath, backup)
            save(filepath, obj)
            try:
                os.remove(backup)
            except Exception, e:
                warnings.warn("Got an error while traing to remove "+backup+":"+str(e))
            return
        else:
            assert on_overwrite == 'ignore'

    try:
        _save(filepath, obj)
    except RuntimeError, e:
        """ Sometimes for large theano graphs, pickle/cPickle exceed the
            maximum recursion depth. This seems to me like a fundamental
            design flaw in pickle/cPickle. The workaround I employ here
            is the one recommended to someone who had a similar problem
            on stackexchange:

            http://stackoverflow.com/questions/2134706/hitting-maximum-recursion-depth-using-pythons-pickle-cpickle

            Obviously this does not scale and could cause a crash
            but I don't see another solution short of writing our
            own implementation of pickle.
        """
        if str(e).find('recursion') != -1:
            warnings.warn('pylearn2.utils.save encountered the following '
                          'error: ' + str(e) +
                          '\nAttempting to resolve this error by calling ' +
                          'sys.setrecusionlimit and retrying')
            old_limit = sys.getrecursionlimit()
            try:
                sys.setrecursionlimit(50000)
                _save(filepath, obj)
            finally:
                sys.setrecursionlimit(old_limit)


Instruction:
Can you tell what is the following Python 3 function doing
def get_pickle_protocol():
    try:
        protocol_str = os.environ['PYLEARN2_PICKLE_PROTOCOL']
    except KeyError:
        # If not defined, we default to 0 because this is the default
        # protocol used by cPickle.dump (and because it results in
        # maximum portability)
        protocol_str = '0'
    if protocol_str == 'pickle.HIGHEST_PROTOCOL':
        return pickle.HIGHEST_PROTOCOL
    return int(protocol_str)

Response: Get the pickle protocol for this node.


Instruction:
Can you generate the documentation for the following Python 3 function
def load_train_file(config_file_path):
    from pylearn2.config import yaml_parse

    suffix_to_strip = '.yaml'

    # publish environment variables related to file name
    if config_file_path.endswith(suffix_to_strip):
        config_file_full_stem = config_file_path[0:-len(suffix_to_strip)]
    else:
        config_file_full_stem = config_file_path

    for varname in ["PYLEARN2_TRAIN_FILE_NAME", #this one is deprecated
            "PYLEARN2_TRAIN_FILE_FULL_STEM"]: #this is the new, accepted name
        environ.putenv(varname, config_file_full_stem)

    directory = config_file_path.split('/')[:-1]
    directory = '/'.join(directory)
    if directory != '':
        directory += '/'
    environ.putenv("PYLEARN2_TRAIN_DIR", directory)
    environ.putenv("PYLEARN2_TRAIN_BASE_NAME", config_file_path.split('/')[-1] )
    environ.putenv("PYLEARN2_TRAIN_FILE_STEM", config_file_full_stem.split('/')[-1] )

    return yaml_parse.load_path(config_file_path)

Response: Loads and parses a yaml file for a Train object.


Instruction:
Create a Python 3 function for
propagating forward through the layer.

Response:
def feed_forward(self, input_data, prediction=False):
        """Propagate forward through the layer

        **Parameters:**

        input_data : ``GPUArray``
            Inpute data to perform dropout on.

        prediction : bool, optional
            Whether to use prediction model. If true, then the data is
            scaled by ``1 - dropout_probability`` uses dropout.

        **Returns:**

        dropout_data : ``GPUArray``
            The data after performing dropout.
        """

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

        if not prediction:
            dropout_input = gpuarray.empty_like(input_data)
            dropout_mask = sample_dropout_mask(input_data,
                                               self.dropout_probability, target=dropout_input
                                           )
            return dropout_input, dropout_mask
        else:
            return (input_data * (1 - self.dropout_probability),)


Instruction:
Can you tell what is the following Python 3 function doing
def backprop(self, input_data, df_output, cache=None):

        if self.compute_input_gradients:
            apply_dropout_mask(df_output, dropout_mask)

        return tuple(), df_output

Response: Backpropagate through the hidden layer.


Instruction:
Can you implement a function in Python 3 that
creates ctypes pointer to object.

Response:
def POINTER(obj):
    """
    Create ctypes pointer to object.

    Notes
    -----
    This function converts None to a real NULL pointer because of bug
    in how ctypes handles None on 64-bit platforms.

    """

    p = ctypes.POINTER(obj)
    if not isinstance(p.from_param, classmethod):
        def from_param(cls, x):
            if x is None:
                return cls()
            else:
                return x
        p.from_param = classmethod(from_param)

    return p


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

    addr = int(g.gpudata)
    if g.dtype == np.int8:
        return ctypes.cast(addr, POINTER(ctypes.c_byte))
    if g.dtype == np.uint8:
        return ctypes.cast(addr, POINTER(ctypes.c_ubyte))
    if g.dtype == np.int16:
        return ctypes.cast(addr, POINTER(ctypes.c_short))
    if g.dtype == np.uint16:
        return ctypes.cast(addr, POINTER(ctypes.c_ushort))
    if g.dtype == np.int32:
        return ctypes.cast(addr, POINTER(ctypes.c_int))
    if g.dtype == np.uint32:
        return ctypes.cast(addr, POINTER(ctypes.c_uint))
    if g.dtype == np.int64:
        return ctypes.cast(addr, POINTER(ctypes.c_long))
    if g.dtype == np.uint64:
        return ctypes.cast(addr, POINTER(ctypes.c_ulong))
    if g.dtype == np.float32:
        return ctypes.cast(addr, POINTER(ctypes.c_float))
    elif g.dtype == np.float64:
        return ctypes.cast(addr, POINTER(ctypes.c_double))
    elif g.dtype == np.complex64:
        return ctypes.cast(addr, POINTER(cuFloatComplex))
    elif g.dtype == np.complex128:
        return ctypes.cast(addr, POINTER(cuDoubleComplex))
    else:
        raise ValueError('unrecognized type')

Response: Return ctypes pointer to data in GPUAarray object.


Instruction:
Write a Python 3 script to
allocate memory on the device associated with the current active .

Response:
def cudaMalloc(count, ctype=None):
    """
    Allocate device memory.

    Allocate memory on the device associated with the current active
    context.

    Parameters
    ----------
    count : int
        Number of bytes of memory to allocate
    ctype : _ctypes.SimpleType, optional
        ctypes type to cast returned pointer.

    Returns
    -------
    ptr : ctypes pointer
        Pointer to allocated device memory.

    """

    ptr = ctypes.c_void_p()
    status = _libcudart.cudaMalloc(ctypes.byref(ptr), count)
    cudaCheckStatus(status)
    if ctype != None:
        ptr = ctypes.cast(ptr, ctypes.POINTER(ctype))
    return ptr


Instruction:
Make a summary of the following Python 3 code
def cudaMallocPitch(pitch, rows, cols, elesize):

    ptr = ctypes.c_void_p()
    status = _libcudart.cudaMallocPitch(ctypes.byref(ptr),
                                        ctypes.c_size_t(pitch), cols*elesize,
                                        rows)
    cudaCheckStatus(status)
    return ptr, pitch

Response:
Allocate pitched memory on the device associated with the current active
   .


Instruction:
Write a Python 3 function for
copying memory from host to device.

Response:
def cudaMemcpy_htod(dst, src, count):
    """
    Copy memory from host to device.

    Copy data from host memory to device memory.

    Parameters
    ----------
    dst : ctypes pointer
        Device memory pointer.
    src : ctypes pointer
        Host memory pointer.
    count : int
        Number of bytes to copy.

    """

    status = _libcudart.cudaMemcpy(dst, src,
                                   ctypes.c_size_t(count),
                                   cudaMemcpyHostToDevice)
    cudaCheckStatus(status)


Instruction:
How would you implement a function in Python 3 that
copies memory from device to host memory.

Response:
def cudaMemcpy_dtoh(dst, src, count):
    """
    Copy memory from device to host.

    Copy data from device memory to host memory.

    Parameters
    ----------
    dst : ctypes pointer
        Host memory pointer.
    src : ctypes pointer
        Device memory pointer.
    count : int
        Number of bytes to copy.

    """

    status = _libcudart.cudaMemcpy(dst, src,
                                   ctypes.c_size_t(count),
                                   cudaMemcpyDeviceToHost)
    cudaCheckStatus(status)


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

    free = ctypes.c_size_t()
    total = ctypes.c_size_t()
    status = _libcudart.cudaMemGetInfo(ctypes.byref(free),
                                       ctypes.byref(total))
    cudaCheckStatus(status)
    return free.value, total.value

Response: Return the amount of free and total device memory in bytes.


Instruction:
Can you create a Python 3 function that
gets current CUDA device number.

Response:
def cudaGetDevice():
    """
    Get current CUDA device.

    Return the identifying number of the device currently used to
    process CUDA operations.

    Returns
    -------
    dev : int
        Device number.

    """

    dev = ctypes.c_int()
    status = _libcudart.cudaGetDevice(ctypes.byref(dev))
    cudaCheckStatus(status)
    return dev.value


Instruction:
Write a Python 3 function that can
get the version of the CUDA driver as an integer.

Response:
def cudaDriverGetVersion():
    """
    Get installed CUDA driver version.

    Return the version of the installed CUDA driver as an integer. If
    no driver is detected, 0 is returned.

    Returns
    -------
    version : int
        Driver version.

    """

    version = ctypes.c_int()
    status = _libcudart.cudaDriverGetVersion(ctypes.byref(version))
    cudaCheckStatus(status)
    return version.value


Instruction:
Make a summary of the following Python 3 code
def cudaPointerGetAttributes(ptr):

    attributes = cudaPointerAttributes()
    status = \
        _libcudart.cudaPointerGetAttributes(ctypes.byref(attributes), ptr)
    cudaCheckStatus(status)
    return attributes.memoryType, attributes.device

Response: Get memory pointer attributes.


Instruction:
Write a Python 3 function that can
evaluate a thunk in an environment.

Response:
def eval(thunk, env):
  """Evaluate a thunk in an environment.

  Will defer the actual evaluation to the thunk itself, but adds two things:
  caching and recursion detection.

  Since we have to use a global evaluation stack (because there is a variety of functions that may
  be invoked, not just eval() but also __getitem__, and not all of them can pass along a context
  object), GCL evaluation is not thread safe.

  With regard to schemas:

  - A schema can be passed in from outside. The returned object will be validated to see that it
    conforms to the schema. The schema will be attached to the value if possible.
  - Some objects may contain their own schema, such as tuples. This would be out of scope of the
    eval() function, were it not for:
  - Schema validation can be disabled in an evaluation call stack. This is useful if we're
    evaluating a tuple only for its schema information. At that point, we're not interested if the
    object is value-complete.
  """
  key = Activation.key(thunk, env)
  if Activation.activated(key):
    raise exceptions.RecursionError('Reference cycle')

  with Activation(key):
    return eval_cache.get(key, thunk.eval, env)


Instruction:
Explain what the following Python 3 code does
def get_node(self, key):
    if key in self.names:
      return self.values.get_member_node(key) if hasattr(self.values, 'get_member_node') else None
    return self.parent.get_node(key)

Response: Delegate to our current value provider for the node belonging to this key.


Instruction:
Can you tell what is the following Python 3 function doing
def create_table(cls):
        schema_editor = getattr(connection, 'schema_editor', None)
        if schema_editor:
            with schema_editor() as schema_editor:
                schema_editor.create_model(cls)
        else:
            raw_sql, _ = connection.creation.sql_create_model(
                cls,
                no_style(),
                [])
            cls.delete_table()
            cursor = connection.cursor()
            try:
                cursor.execute(*raw_sql)
            finally:
                cursor.close()

Response: Create a temporary table for the given model.


Instruction:
Write a Python 3 script to
delete a temporary table for model in test data base.

Response:
def delete_table(cls):
        """
        delete_table

        Manually delete a temporary table for model in test data base.
        :return:
        """
        schema_editor = getattr(connection, 'schema_editor', None)
        if schema_editor:
            with connection.schema_editor() as schema_editor:
                schema_editor.delete_model(cls)
        else:
            cursor = connection.cursor()
            try:
                with warnings.catch_warnings():
                    warnings.filterwarnings('ignore', 'unknown table')
                    cursor.execute('DROP TABLE IF EXISTS {0}'.format(cls._meta.db_table))
            finally:
                cursor.close()


Instruction:
Can you write a function in Python 3 where it
creates temporary model only for given test method

Response:
def fake_me(cls, source):
        """
        fake_me

        Class or method decorator

        Class decorator: create temporary table for all tests in SimpleTestCase.
        Method decorator: create temporary model only for given test method.
        :param source: SimpleTestCase or test function
        :return:
        """
        if source and type(source) == type and issubclass(source, SimpleTestCase):
            return cls._class_extension(source)
        elif hasattr(source, '__call__'):
            return cls._decorator(source)
        else:
            raise AttributeError('source - must be a SimpleTestCase subclass of function')


Instruction:
Implement a function in Python 3 to
reset the current configuration to default settings.

Response:
def reset(cls):
        """
        Reset to default settings
        """
        cls.debug = False
        cls.disabled = False
        cls.overwrite = False
        cls.playback_only = False
        cls.recv_timeout = 5
        cls.recv_endmarkers = []
        cls.recv_size = None


Instruction:
Make a summary of the following Python 3 code
def walk(value, walker, path=None, seen=None):
  seen = seen or set()
  path = path or []

  # Recursion
  if id(value) in seen:
    walker.visitRecursion(path)
    return

  # Error
  if isinstance(value, Exception):
    walker.visitError(path, value)
    return

  # List
  if isinstance(value, list):
    # Not actually a tuple, but okay
    recurse = walker.enterList(value, path)
    if not recurse: return
    next_walker = walker if recurse is True else recurse

    with TempSetAdd(seen, id(value)):
      for i, x in enumerate(value):
        walk(x, next_walker, path=path + ['[%d]' % i], seen=seen)

      walker.leaveList(value, path)
    return

  # Scalar
  if not isinstance(value, framework.TupleLike):
    walker.visitScalar(path, value)
    return

  # Tuple
  recurse = walker.enterTuple(value, path)
  if not recurse: return
  next_walker = walker if recurse is True else recurse

  with TempSetAdd(seen, id(value)):
    keys = sorted(value.keys())
    for key in keys:
      key_path = path + [key]
      elm = get_or_error(value, key)
      walk(elm, next_walker, path=key_path, seen=seen)

    walker.leaveTuple(value, path)

Response: Walks the _evaluated_ tree of the given GCL tuple.


Instruction:
Here you have a function in Python 3, explain what it does
def fingerprint(value):
  h = hashlib.sha256()
  _digest(value, h)
  return h.digest().encode('hex')

Response: Return a hash value that uniquely identifies the GCL value.


Instruction:
Implement a Python 3 function for
returning the last 2 error messages from an error stack.

Response:
def compact_error(err):
  """Return the the last 2 error messages from an error stack.

  These error messages turns out to be the most descriptive.
  """
  def err2(e):
    if isinstance(e, exceptions.EvaluationError) and e.inner:
      message, i = err2(e.inner)
      if i == 1:
        return ', '.join([e.args[0], str(e.inner)]), i + 1
      else:
        return message, i + 1
    else:
      return str(e), 1
  return err2(err)[0]


Instruction:
Can you tell what is the following Python 3 function doing
def backprop(self, input_data, targets,
                 cache=None):
        """ Backpropagate through the logistic layer.

        **Parameters:**

        input_data : ``GPUArray``
            Inpute data to compute activations for.

        targets : ``GPUArray``
            The target values of the units.

        cache : list of ``GPUArray``
            Cache obtained from forward pass. If the cache is
            provided, then the activations are not recalculated.

        **Returns:**

        gradients : tuple of ``GPUArray``
            Gradients with respect to the weights and biases in the
            form ``(df_weights, df_biases)``.

        df_input : ``GPUArray``
            Gradients with respect to the input.
        """

        if cache is not None:
            activations = cache
        else:
            activations = self.feed_forward(input_data, prediction=False)

        if activations.shape != targets.shape:
            raise ValueError('Activations (shape = %s) and targets (shape = %s) are different sizes' %
                             (activations.shape, targets.shape))

        delta = substract_matrix(activations, targets)
        nan_to_zeros(delta, delta)

        # Gradient wrt weights
        df_W = linalg.dot(input_data, delta, transa='T')
        # Gradient wrt bias
        df_b = matrix_sum_out_axis(delta, 0)

        # Gradient wrt input
        df_input = linalg.dot(delta, self.W, transb='T')

        # L1 penalty
        if self.l1_penalty_weight:
            df_W += self.l1_penalty_weight * sign(self.W)

        # L2 penalty
        if self.l2_penalty_weight:
            df_W += self.l2_penalty_weight * self.W

        return (df_W, df_b), df_input

Response: Backpropagate through the logistic layer.


Instruction:
Create a Python 3 function to
compute the cross entropy error of the input data.

Response:
def cross_entropy_error(self, input_data, targets, average=True,
                            cache=None, prediction=False):
        """ Return the cross entropy error
        """

        if cache is not None:
            activations = cache
        else:
            activations = \
              self.feed_forward(input_data, prediction=prediction)

        loss = cross_entropy_logistic(activations, targets)

        if average: loss /= targets.shape[0]
        # assert np.isfinite(loss)
        return loss.get()


Instruction:
Create a Python 3 function for
parsing comment lines and make subsequent indented lines into a code block athe.

Response:
def stylize_comment_block(lines):
  """Parse comment lines and make subsequent indented lines into a code block
  block.
  """
  normal, sep, in_code = range(3)
  state = normal
  for line in lines:
    indented = line.startswith('    ')
    empty_line = line.strip() == ''

    if state == normal and empty_line:
      state = sep
    elif state in [sep, normal] and indented:
      yield ''
      if indented:
        yield '.. code-block:: javascript'
        yield ''
        yield line
        state = in_code
      else:
        state = normal
    elif state == sep and not empty_line:
      yield ''
      yield line
      state = normal
    else:
      yield line
      if state == in_code and not (indented or empty_line):
        sep = normal


Instruction:
Given the following Python 3 function, write the documentation
def sort_members(tup, names):
  scalars, tuples = partition(lambda x: not is_tuple_node(tup.member[x].value), names)
  unbound, bound = partition(lambda x: tup.member[x].value.is_unbound(), scalars)
  return usorted(unbound) + usorted(bound), usorted(tuples)

Response: Return two pairs of members scalar and tuple members.


Instruction:
Can you write a function in Python 3 where it
resolves filename relatively against one of the given paths.

Response:
def resolve_file(fname, paths):
  """Resolve filename relatively against one of the given paths, if possible."""
  fpath = path.abspath(fname)
  for p in paths:
    spath = path.abspath(p)
    if fpath.startswith(spath):
      return fpath[len(spath) + 1:]
  return fname


Instruction:
Can you tell what is the following Python 3 function doing
def generate(self):
    header = ' '.join('=' * self.width[i] for i in range(self.w))
    lines = [
        ' '.join(row[i].ljust(self.width[i]) for i in range(self.w))
        for row in self.rows]
    return [header] + lines + [header]

Response: Generate a list of strings representing the table in RST format.


Instruction:
Can you generate the documentation for the following Python 3 function
def partition(pred, iterable):
    'Use a predicate to partition entries into false entries and true entries'
    # partition(is_odd, range(10)) --> 0 2 4 6 8   and  1 3 5 7 9
    t1, t2 = itertools.tee(iterable)
    return list(filter(negate(pred), t1)), list(filter(pred, t2))

Response: Use a predicate to partition entries into false entries and true entries


Instruction:
Can you generate a brief explanation for the following Python 3 code
def select(self, model):
    res = []

    def doSelect(value, pre, remaining):
      if not remaining:
        res.append((pre, value))
      else:
        # For the other selectors to work, value must be a Tuple or a list at this point.
        if not is_tuple(value) and not isinstance(value, list):
          return

        qhead, qtail = remaining[0], remaining[1:]
        if isinstance(qhead, tuple) and is_tuple(value):
          for alt in qhead:
            if alt in value:
              doSelect(value[alt], pre + [alt], qtail)
        elif qhead == '*':
          if isinstance(value, list):
            indices = range(len(value))
            reprs = [listKey(i) for i in indices]
          else:
            indices = value.keys()
            reprs = indices

          for key, rep in zip(indices, reprs):
            doSelect(value[key], pre + [rep], qtail)
        elif isinstance(qhead, int) and isinstance(value, list):
          doSelect(value[qhead], pre + [listKey(qhead)], qtail)
        elif is_tuple(value):
          if qhead in value:
            doSelect(value[qhead], pre + [qhead], qtail)

    for selector in self.selectors:
      doSelect(model, [], selector)

    return QueryResult(res)

Response: Select nodes according to the input selector.


Instruction:
Make a summary of the following Python 3 code
def deep(self):
    self.lists = {}
    ret = {}
    for path, value in self.paths_values():
      self.recursiveSet(ret, path, value)
    self.removeMissingValuesFromLists()
    return ret

Response: Returns a deep dict of the values selected.


Instruction:
Make a summary of the following Python 3 code
def ldSet(self, what, key, value):
    if isListKey(key):
      # Make sure we keep the indexes consistent, insert missing_values
      # as necessary. We do remember the lists, so that we can remove
      # missing values after inserting all values from all selectors.
      self.lists[id(what)] = what
      ix = listKeyIndex(key)
      while len(what) <= ix:
        what.append(missing_value)
      what[ix] = value
    else:
      what[key] = value
    return value

Response: Set the value of the list with the given key.


Instruction:
Make a summary of the following Python 3 code
def ldGet(self, what, key):
    if isListKey(key):
      return what[listKeyIndex(key)]
    else:
      return what[key]

Response: List - aware get.


Instruction:
Write a Python 3 function that can
return True if the list contains the key.

Response:
def ldContains(self, what, key):
    """List/dictinary/missing-aware contains.

    If the value is a "missing_value", we'll treat it as non-existent
    so it will be overwritten by an empty list/dict when necessary to
    assign child keys.
    """
    if isListKey(key):
      i = listKeyIndex(key)
      return i < len(what) and what[i] != missing_value
    else:
      return key in what and what[key] != missing_value


Instruction:
Write a Python 3 function that can
return a list of nodes that have a recursive dependency.

Response:
def find_recursive_dependency(self):
    """Return a list of nodes that have a recursive dependency."""
    nodes_on_path = []

    def helper(nodes):
      for node in nodes:
        cycle = node in nodes_on_path
        nodes_on_path.append(node)
        if cycle or helper(self.deps.get(node, [])):
          return True
        nodes_on_path.pop()
      return False

    helper(self.unordered)
    return nodes_on_path


Instruction:
Make a summary of the following Python 3 code
def enterTuple(self, tuple, path):
    if skip_name(path):
      return False
    node = Node(path, tuple)
    if self.condition.matches(node):
      self.unordered.append(node)
      return False
    return True

Response: Called for every tuple.


Instruction:
Create a Python 3 function to
make a sequence of applications from a list of tokens.

Response:
def mkApplications(location, *atoms):
  """Make a sequence of applications from a list of tokens.

  atoms is a list of atoms, which will be handled left-associatively. E.g:

      ['foo', [], []] == foo()() ==> Application(Application('foo', []), [])
  """
  atoms = list(atoms)
  while len(atoms) > 1:
    atoms[0:2] = [Application(location, atoms[0], atoms[1])]

  # Nothing left to apply
  return atoms[0]


Instruction:
How would you implement a function in Python 3 that
calls a function respecting all the various types of functions that exist.

Response:
def call_fn(fn, arglist, env):
  """Call a function, respecting all the various types of functions that exist."""
  if isinstance(fn, framework.LazyFunction):
    # The following looks complicated, but this is necessary because you can't
    # construct closures over the loop variable directly.
    thunks = [(lambda thunk: lambda: framework.eval(thunk, env))(th) for th in arglist.values]
    return fn(*thunks)

  evaled_args = framework.eval(arglist, env)
  if isinstance(fn, framework.EnvironmentFunction):
    return fn(*evaled_args, env=env)

  return fn(*evaled_args)


Instruction:
Can you create a Python 3 function that
returns the schema spec from a run - time tuple.

Response:
def schema_spec_from_tuple(tup):
  """Return the schema spec from a run-time tuple."""
  if hasattr(tup, 'get_schema_spec'):
    # Tuples have a TupleSchema field that contains a model of the schema
    return schema.from_spec({
        'fields': TupleSchemaAccess(tup),
        'required': tup.get_required_fields()})
  return schema.AnySchema()


Instruction:
Can you generate the documentation for the following Python 3 function
def make_schema_from(value, env):

  # So this thing may not need to evaluate anything[0]
  if isinstance(value, framework.Thunk):
    value = framework.eval(value, env)

  # We're a bit messy. In general, this has evaluated to a Schema object, but not necessarily:
  # for tuples and lists, we still need to treat the objects as specs.
  if isinstance(value, schema.Schema):
    return value

  if framework.is_tuple(value):
    # If it so happens that the thing is a tuple, we need to pass in the data in a bit of a
    # different way into the schema factory (in a dictionary with {fields, required} keys).
    return schema_spec_from_tuple(value)

  if framework.is_list(value):
    # [0] This list may contain tuples, which oughta be treated as specs, or already-resolved schema
    # objects (as returned by 'int' and 'string' literals). make_schema_from
    # deals with both.
    return schema.from_spec([make_schema_from(x, env) for x in value])

  raise exceptions.EvaluationError('Can\'t make a schema from %r' % value)

Response: Make a Schema object from the given spec.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def bracketedList(l, r, sep, expr, allow_missing_close=False):
  # We may need to backtrack for lists, because of list comprehension, but not for
  # any of the other lists
  strict = l != '['
  closer = sym(r) if not allow_missing_close else p.Optional(sym(r))
  if strict:
    return sym(l) - listMembers(sep, expr) - closer
  else:
    return sym(l) + listMembers(sep, expr) + closer

Response:
Parse bracketed list.

  Empty list is possible, as is a trailing separator.


Instruction:
Here you have a function in Python 3, explain what it does
def unquote(s):
  # Ignore the left- and rightmost chars (which should be quotes).
  # Use the Python engine to decode the escape sequence
  i, N = 1, len(s) - 1
  ret = []
  while i < N:
    if s[i] == '\\' and i < N - 1:
      ret.append(UNQUOTE_MAP.get(s[i+1], s[i+1]))
      i += 2
    else:
      ret.append(s[i])
      i += 1
  return ''.join(ret)

Response: Unquote the indicated string.


Instruction:
Write a Python 3 script for
functioning to put a name on a pyparsing pattern.

Response:
def pattern(name, pattern):
  """Function to put a name on a pyparsing pattern.

  Just for ease of debugging/tracing parse errors.
  """
  pattern.setName(name)
  astracing.maybe_trace(pattern)
  return pattern


Instruction:
Write a Python 3 function that can
make a grammar that can be used to parse a single expression.

Response:
def make_grammar(allow_errors):
  """Make the part of the grammar that depends on whether we swallow errors or not."""
  if allow_errors in GRAMMAR_CACHE:
    return GRAMMAR_CACHE[allow_errors]

  tuple = p.Forward()
  catch_errors = p.Forward()
  catch_errors << (p.Regex('[^{};]*') - p.Optional(tuple) - p.Regex('[^;}]*'))

  def swallow_remainder():
    if allow_errors:
      return pattern('swallow_remainder', p.Suppress(catch_errors))
    return p.Empty()

  def swallow_errors(rule):
    """Extend the production rule by potentially eating errors.

    This does not return a p.NoMatch() because that messes up the error messages.
    """
    ret = rule
    if allow_errors:
      # Synchronize on the first semicolon or the first unbalanced closing curly
      ret = rule | pattern('catch_errors', parseWithLocation(p.Suppress(catch_errors), UnparseableNode))
    return ret

  class Grammar:
    keywords = ['and', 'or', 'not', 'if', 'then', 'else', 'include', 'inherit', 'null', 'true', 'false',
        'for', 'in']

    # This is a hack: this condition helps uselessly recursing into the grammar for
    # juxtapositions.
    early_abort_scan = ~p.oneOf([';', ',', ']', '}', 'for' ])

    expression = pattern('expression', p.Forward())

    comment = p.Regex('#') + ~p.FollowedBy(sym('.')) + p.restOfLine
    doc_comment = pattern('doc_comment', (sym('#.') - p.restOfLine))

    quotedIdentifier = pattern('quotedIdentifier', p.QuotedString('`', multiline=False))

    # - Must start with an alphascore
    # - May contain alphanumericscores and special characters such as : and -
    # - Must not end in a special character
    identifier = pattern('identifier', parseWithLocation(quotedIdentifier | p.Regex(r'[a-zA-Z_]([a-zA-Z0-9_:-]*[a-zA-Z0-9_])?'), Identifier))

    # Variable identifier (can't be any of the keywords, which may have lower matching priority)
    variable = pattern('variable', ~p.MatchFirst(p.oneOf(keywords)) + pattern('identifier', parseWithLocation(identifier.copy(), Var)))

    # Contants
    integer = pattern('integer', parseWithLocation(p.Word(p.nums), convertAndMake(int, Literal)))
    floating = pattern('floating', parseWithLocation(p.Regex(r'\d*\.\d+'), convertAndMake(float, Literal)))
    dq_string = pattern('dq_string', parseWithLocation(p.QuotedString('"', escChar='\\', unquoteResults=False, multiline=True), convertAndMake(unquote, Literal)))
    sq_string = pattern('sq_string', parseWithLocation(p.QuotedString("'", escChar='\\', unquoteResults=False, multiline=True), convertAndMake(unquote, Literal)))
    boolean = pattern('boolean', parseWithLocation(p.Keyword('true') | p.Keyword('false'), convertAndMake(mkBool, Literal)))
    null = pattern('null', parseWithLocation(p.Keyword('null'), Null))

    # List
    list_ = pattern('list', parseWithLocation(bracketedList('[', ']', ',', expression), List))

    # Tuple
    inherit = pattern('inherit', (kw('inherit') - p.ZeroOrMore(variable)).setParseAction(inheritNodes))
    schema_spec = pattern('schema_spec', parseWithLocation(p.Optional(p.Keyword('private').setParseAction(lambda: True), default=False)
                  - p.Optional(p.Keyword('required').setParseAction(lambda: True), default=False)
                  - p.Optional(expression, default=any_schema_expr), MemberSchemaNode))
    optional_schema = pattern('optional_schema', p.Optional(p.Suppress(':') - schema_spec, default=no_schema))

    expression_value = pattern('expression_value', sym('=') - swallow_errors(expression))
    void_value = pattern('void_value', parseWithLocation(p.FollowedBy(sym(';') | sym('}')), lambda loc: Void(loc, 'nonameyet')))
    member_value = pattern('member_value', swallow_errors(expression_value | void_value))
    named_member = pattern('named_member', parseWithLocation(identifier - optional_schema - member_value - swallow_remainder(), TupleMemberNode))
    documented_member = pattern('documented_member', parseWithLocation(parseWithLocation(p.ZeroOrMore(doc_comment), DocComment) + named_member, attach_doc_comment))
    tuple_member = early_abort_scan + pattern('tuple_member', swallow_errors(inherit | documented_member) - swallow_remainder())

    ErrorAwareTupleNode = functools.partial(TupleNode, allow_errors)
    tuple_members = pattern('tuple_members', parseWithLocation(listMembers(';', tuple_member), ErrorAwareTupleNode))
    tuple << pattern('tuple', parseWithLocation(bracketedList('{', '}', ';', tuple_member, allow_missing_close=allow_errors), ErrorAwareTupleNode))

    # Argument list will live by itself as a atom. Actually, it's a tuple, but we
    # don't call it that because we use that term for something else already :)
    arg_list = pattern('arg_list', bracketedList('(', ')', ',', expression).setParseAction(ArgList))

    parenthesized_expr = pattern('parenthesized_expr', (sym('(') - expression - ')').setParseAction(head))

    unary_op = pattern('unary_op', (p.oneOf(' '.join(functions.unary_operators.keys())) - expression).setParseAction(mkUnOp))

    if_then_else = pattern('if_then_else', parseWithLocation(kw('if') + expression +
                    kw('then') + expression +
                    kw('else') + expression, Condition))

    list_comprehension = pattern('list_comprehension', parseWithLocation(sym('[') + expression + kw('for') + variable + kw('in') +
        expression + p.Optional(kw('if') + expression) + sym(']'), ListComprehension))

    # We don't allow space-application here
    # Now our grammar is becoming very dirty and hackish
    deref = pattern('deref', p.Forward())
    include = pattern('include', parseWithLocation(kw('include') - deref, Include))

    atom = pattern('atom', (tuple
            | sq_string
            | dq_string
            | variable
            | floating
            | integer
            | boolean
            | list_
            | null
            | unary_op
            | parenthesized_expr
            | if_then_else
            | include
            | list_comprehension
            ))

    # We have two different forms of function application, so they can have 2
    # different precedences. This one: fn(args), which binds stronger than
    # dereferencing (fn(args).attr == (fn(args)).attr)
    applic1 = pattern('applic1', parseWithLocation(atom - p.ZeroOrMore(arg_list), mkApplications))

    # Dereferencing of an expression (obj.bar)
    deref << parseWithLocation(applic1 - p.ZeroOrMore(p.Suppress('.') - swallow_errors(identifier)), mkDerefs)

    # All binary operators at various precedence levels go here:
    # This piece of code does the moral equivalent of:
    #
    #     T = F*F | F/F | F
    #     E = T+T | T-T | T
    #
    # etc.
    term = deref
    for op_level in functions.binary_operators_before_juxtaposition:
      operator_syms = list(op_level.keys())
      term = (term - p.ZeroOrMore(p.oneOf(operator_syms) - term)).setParseAction(mkBinOps)

    # Juxtaposition function application (fn arg), must be 1-arg every time
    applic2 = pattern('applic2', parseWithLocation(term - p.ZeroOrMore(early_abort_scan + term), mkApplications))

    term = applic2
    for op_level in functions.binary_operators_after_juxtaposition:
      operator_syms = list(op_level.keys())
      term = (term - p.ZeroOrMore(p.oneOf(operator_syms) - term)).setParseAction(mkBinOps)

    expression << term

    # Two entry points: start at an arbitrary expression, or expect the top-level
    # scope to be a tuple.
    start = pattern('start', expression.copy().ignore(comment))
    start_tuple = tuple_members.ignore(comment)
  GRAMMAR_CACHE[allow_errors] = Grammar
  return Grammar


Instruction:
How would you explain what the following Python 3 function does
def reads(s, filename, loader, implicit_tuple, allow_errors):
  try:
    the_context.filename = filename
    the_context.loader = loader

    grammar = make_grammar(allow_errors=allow_errors)
    root = grammar.start_tuple if implicit_tuple else grammar.start

    return root.parseWithTabs().parseString(s, parseAll=True)[0]
  except (p.ParseException, p.ParseSyntaxException) as e:
    loc = SourceLocation(s, find_offset(s, e.lineno, e.col))
    raise exceptions.ParseError(the_context.filename, loc, e.msg)

Response: Load but don t evaluate a GCL expression from a string.


Instruction:
Implement a function in Python 3 to
find all AST nodes at the given filename line and column.

Response:
def find_tokens(self, q):
    """Find all AST nodes at the given filename, line and column."""
    found_me = []
    if hasattr(self, 'location'):
      if self.location.contains(q):
        found_me = [self]
    elif self._found_by(q):
      found_me = [self]

    cs = [n.find_tokens(q) for n in self._children()]
    return found_me + list(itertools.chain(*cs))


Instruction:
Can you generate the documentation for the following Python 3 function
def applyTuple(self, tuple, right, env):
    if len(right) != 1:
      raise exceptions.EvaluationError('Tuple (%r) can only be applied to one argument, got %r' % (self.left, self.right))
    right = right[0]

    return tuple(right)

Response: Apply a tuple to something else.


Instruction:
Here you have a function in Python 3, explain what it does
def applyIndex(self, lst, right):
    if len(right) != 1:
      raise exceptions.EvaluationError('%r can only be applied to one argument, got %r' % (self.left, self.right))
    right = right[0]

    if isinstance(right, int):
      return lst[right]

    raise exceptions.EvaluationError("Can't apply %r to argument (%r): integer expected, got %r" % (self.left, self.right, right))

Response: Apply a list to something else.


Instruction:
How would you implement a function in Python 3 that
updates the parameters of the current object based on the current velocity.

Response:
def pre_gradient_update(self):
        """ First step of Nesterov momentum method:
        take step in direction of accumulated gradient
        """

        updates = zip(self.velocity, self.model.n_parameters * [1.])
        self.model.update_parameters(updates)


Instruction:
Create a Python 3 function to
return the classification error rate for the current state of the target.

Response:
def class_error(self, input_data, targets, average=True,
                    cache=None, prediction=False):
        """ Return the classification error rate
        """

        if cache is not None:
            activations = cache
        else:
            activations = \
              self.feed_forward(input_data, prediction=prediction)

        targets = targets.get().argmax(1)
        class_error = np.sum(activations.get().argmax(1) != targets)

        if average: class_error = float(class_error) / targets.shape[0]
        return class_error


Instruction:
Explain what the following Python 3 code does
def dot(x_gpu, y_gpu, transa='N', transb='N', handle=None, target=None):

    if handle is None:
        handle = _global_cublas_handle

    if len(x_gpu.shape) == 1 and len(y_gpu.shape) == 1:

        if x_gpu.size != y_gpu.size:
            raise ValueError('arrays must be of same length: '
                             'x_gpu.size = %d, y_gpu.size = %d' %
                             (x_gpu.size, y_gpu.size))

        # Compute inner product for 1D arrays:
        if (x_gpu.dtype == np.complex64 and y_gpu.dtype == np.complex64):
            cublas_func = cublas.cublasCdotu
        elif (x_gpu.dtype == np.float32 and y_gpu.dtype == np.float32):
            cublas_func = cublas.cublasSdot
        elif (x_gpu.dtype == np.complex128 and y_gpu.dtype == np.complex128):
            cublas_func = cublas.cublasZdotu
        elif (x_gpu.dtype == np.float64 and y_gpu.dtype == np.float64):
            cublas_func = cublas.cublasDdot
        else:
            raise ValueError('unsupported combination of input types: '
                             'x_gpu.dtype = %s, y_gpu.dtype = %s' %
                             (str(x_gpu.dtype), str(y_gpu.dtype)))

        return cublas_func(handle, x_gpu.size, x_gpu.gpudata, 1,
                           y_gpu.gpudata, 1)
    else:

        # Get the shapes of the arguments (accounting for the
        # possibility that one of them may only have one dimension):
        x_shape = x_gpu.shape
        y_shape = y_gpu.shape
        if len(x_shape) == 1:
            x_shape = (1, x_shape[0])
        if len(y_shape) == 1:
            y_shape = (1, y_shape[0])

        # Perform matrix multiplication for 2D arrays:
        if (x_gpu.dtype == np.complex64 and y_gpu.dtype == np.complex64):
            cublas_func = cublas.cublasCgemm
            alpha = np.complex64(1.0)
            beta = np.complex64(0.0)
        elif (x_gpu.dtype == np.float32 and y_gpu.dtype == np.float32):
            cublas_func = cublas.cublasSgemm
            alpha = np.float32(1.0)
            beta = np.float32(0.0)
        elif (x_gpu.dtype == np.complex128 and y_gpu.dtype == np.complex128):
            cublas_func = cublas.cublasZgemm
            alpha = np.complex128(1.0)
            beta = np.complex128(0.0)
        elif (x_gpu.dtype == np.float64 and y_gpu.dtype == np.float64):
            cublas_func = cublas.cublasDgemm
            alpha = np.float64(1.0)
            beta = np.float64(0.0)
        else:
            raise ValueError('unsupported combination of input types: '
                             'x_gpu.dtype = %s, y_gpu.dtype = %s' %
                             (str(x_gpu.dtype), str(y_gpu.dtype)))

        transa = lower(transa)
        transb = lower(transb)

        if transb in ['t', 'c']:
            m, k = y_shape
        elif transb in ['n']:
            k, m = y_shape
        else:
            raise ValueError('invalid value "%s" for transb' % transb)

        if transa in ['t', 'c']:
            l, n = x_shape
        elif transa in ['n']:
            n, l = x_shape
        else:
            raise ValueError('invalid value "%s" for transa' % transa)

        if l != k:
            raise ValueError('objects are not aligned: x_shape = %s, y_shape = %s' %
                             (x_shape, y_shape))

        if transb == 'n':
            lda = max(1, m)
        else:
            lda = max(1, k)

        if transa == 'n':
            ldb = max(1, k)
        else:
            ldb = max(1, n)

        ldc = max(1, m)

        # Note that the desired shape of the output matrix is the transpose
        # of what CUBLAS assumes:

        if target is None:
            target = gpuarray.empty((n, ldc), x_gpu.dtype, allocator=memory_pool.allocate)

        cublas_func(handle, transb, transa, m, n, k, alpha, y_gpu.gpudata,
                    lda, x_gpu.gpudata, ldb, beta, target.gpudata, ldc)

        return target

Response: This function computes the dot product of two arrays.


Instruction:
Can you generate the documentation for the following Python 3 function
def make_tempfile(data=None):
    "Create a temp file, write our PID into it."
    with tempfile.NamedTemporaryFile(mode='w', delete=False) as temp:
        temp.write(six.text_type(data if data is not None else os.getpid()))
        return temp.name

Response: Create a temp file write our PID into it.


Instruction:
How would you implement a function in Python 3 that
returns a list where each element contains the parameters for a task.

Response:
def parameters(self):
        """Return a list where each element contains the parameters for a task.
        """
        parameters = []
        for task in self.tasks:
            parameters.extend(task.parameters)
        return parameters


Instruction:
Can you write a function in Python 3 where it
updates the parameters. ``value`` must be a list/tuple of length ``MultitaskTopLayer.n_tasks``, each element of which must have the correct number of parameters for the task.

Response:
def parameters(self, value):
        """Update the parameters.

        ``value`` must be a list/tuple of length
        ``MultitaskTopLayer.n_tasks``, each element of which must have
        the correct number of parameters for the task.
        """

        assert len(value) == self.n_parameters
        i = 0
        for task in self.tasks:
            task.parameters = value[i:i + task.n_parameters]
            i += task.n_parameters


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

        activations = []

        for task in self.tasks:
            activations_task = task.feed_forward(input_data, prediction)
            activations.append(activations_task)

        return activations

Response: Call feed_forward for each task and combine the activations for each output unit.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def backprop(self, input_data, targets, cache=None):

        df_input = gpuarray.zeros_like(input_data)

        if cache is None: cache = self.n_tasks * [None]

        gradients = []
        for targets_task, cache_task, task, task_weight  in \
          izip(targets, cache, self.tasks, self.task_weights):
            gradients_task, df_input_task = \
              task.backprop(input_data, targets_task,
                            cache_task)

            df_input = df_input.mul_add(1., df_input_task, task_weight)

            gradients.extend(gradients_task)

        return gradients, df_input

Response: Compute gradients for each task and combine the results.


Instruction:
Write a Python 3 script for
computing the cross - entropy error for all tasks.

Response:
def cross_entropy_error(self, input_data, targets, average=True,
                            cache=None, prediction=False,
                            sum_errors=True):
        """ Computes the cross-entropy error for all tasks.
        """

        loss = []
        if cache is None:
            cache = self.n_tasks * [None]

        for targets_task, cache_task, task in \
            izip(targets, cache, self.tasks):
            loss.append(task.cross_entropy_error(
                input_data, targets_task, average=average,
                cache=cache_task,
                prediction=prediction))

        if sum_errors:
            return sum(loss)
        else:
            return loss


Instruction:
Explain what the following Python 3 code does
def parameters(self, value):
        self.W = value[0] if isinstance(value[0], GPUArray) else \
          gpuarray.to_gpu(value[0])
        self.b = value[1] if isinstance(value[0], GPUArray) else \
          gpuarray.to_gpu(value[1])

Response:
Update the parameters. ``value`` must have the shape
        ``(weights, biases)``


Instruction:
Here you have a function in Python 3, explain what it does
def architecture(self):
        arch = {'class': self.__class__,
                'n_in': self.n_in,
                'n_units': self.n_units,
                'activation_function': self.activation_function
                if hasattr(self, 'activation_function') else None}
        return arch

Response: Returns a dictionary describing the architecture of the layer.


Instruction:
Write a Python 3 script for
propagating forward through the layer.

Response:
def feed_forward(self, input_data, prediction=False):
        """Propagate forward through the layer

        **Parameters:**

        input_data : ``GPUArray``
            Input data to compute activations for.

        prediction : bool, optional
            Whether to use prediction model. Only relevant when using
            dropout. If true, then weights are multiplied by
            1 - dropout if the layer uses dropout.

        **Returns:**

        activations : ``GPUArray``
            The activations of the hidden units.
        """

        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)

        self.f(activations)

        if self.dropout > 0:
            if prediction:
                activations *= 1 - self.dropout
            else:
                dropout_mask = sample_dropout_mask(activations, self.dropout)
                return activations, dropout_mask

        return (activations,)


Instruction:
Make a summary of the following Python 3 code
def cublasCreate():

    handle = ctypes.c_void_p()
    status = _libcublas.cublasCreate_v2(ctypes.byref(handle))
    cublasCheckStatus(status)
    return handle.value

Response:
Initialize CUBLAS.

    Initializes CUBLAS and creates a handle to a structure holding
    the CUBLAS library context.

    Returns
    -------
    handle : void_p
        CUBLAS context.


Instruction:
How would you code a function in Python 3 to
release CUBLAS resources. Releases hardware resources used by CUBLAS. Parameters ---------- handle : void_p CUBLAS context.

Response:
def cublasDestroy(handle):
    """
    Release CUBLAS resources.

    Releases hardware resources used by CUBLAS.

    Parameters
    ----------
    handle : void_p
        CUBLAS context.

    """

    status = _libcublas.cublasDestroy_v2(ctypes.c_void_p(handle))
    cublasCheckStatus(status)


Instruction:
Can you create a Python 3 function that
gets CUBLAS version. Returns version number of installed CUBLAS libraries. Parameters ---------- handle : void_p CUBLAS context. Returns ------- version : int CUBLAS version.

Response:
def cublasGetVersion(handle):
    """
    Get CUBLAS version.

    Returns version number of installed CUBLAS libraries.

    Parameters
    ----------
    handle : void_p
        CUBLAS context.

    Returns
    -------
    version : int
        CUBLAS version.

    """

    version = ctypes.c_int()
    status = _libcublas.cublasGetVersion_v2(handle, ctypes.byref(version))
    cublasCheckStatus(status)
    return version.value


Instruction:
Write a Python 3 function for
setting current CUBLAS library stream.

Response:
def cublasSetStream(handle, id):
    """
    Set current CUBLAS library stream.

    Parameters
    ----------
    handle : id
        CUBLAS context.
    id : int
        Stream ID.

    """

    status = _libcublas.cublasSetStream_v2(handle, id)
    cublasCheckStatus(status)


Instruction:
How would you implement a function in Python 3 that
gets current CUBLAS library stream.

Response:
def cublasGetStream(handle):
    """
    Set current CUBLAS library stream.

    Parameters
    ----------
    handle : void_p
        CUBLAS context.

    Returns
    -------
    id : int
        Stream ID.

    """

    id = ctypes.c_int()
    status = _libcublas.cublasGetStream_v2(handle, ctypes.byref(id))
    cublasCheckStatus(status)
    return id.value


Instruction:
Can you tell what is the following Python 3 function doing
def cublasCgbmv(handle, trans, m, n, kl, ku, alpha, A, lda,
                x, incx, beta, y, incy):
    """
    Matrix-vector product for complex general banded matrix.

    """

    status = _libcublas.cublasCgbmv_v2(handle,
                                       trans, m, n, kl, ku,
                                       ctypes.byref(cuda.cuFloatComplex(alpha.real,
                                                                        alpha.imag)),
                                       int(A), lda, int(x), incx,
                                       ctypes.byref(cuda.cuFloatComplex(beta.real,
                                                                        beta.imag)),
                                       int(y), incy)
    cublasCheckStatus(status)

Response: Matrix - vector product for complex general banded matrix.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def cublasDgemv(handle, trans, m, n, alpha, A, lda, x, incx, beta, y, incy):

    status = _libcublas.cublasDgemv_v2(handle,
                                       _CUBLAS_OP[trans], m, n,
                                       ctypes.byref(ctypes.c_double(alpha)),
                                       int(A), lda, int(x), incx,
                                       ctypes.byref(ctypes.c_double(beta)),
                                       int(y), incy)
    cublasCheckStatus(status)

Response: Matrix - vector product for real general matrix.


Instruction:
Here you have a function in Python 3, explain what it does
def cublasCgemv(handle, trans, m, n, alpha, A, lda, x, incx, beta, y, incy):

    status = _libcublas.cublasCgemv_v2(handle,
                                       _CUBLAS_OP[trans], m, n,
                                       ctypes.byref(cuda.cuFloatComplex(alpha.real,
                                                                        alpha.imag)),
                                       int(A), lda, int(x), incx,
                                       ctypes.byref(cuda.cuFloatComplex(beta.real,
                                                                        beta.imag)),
                                       int(y), incy)
    cublasCheckStatus(status)

Response: Matrix - vector product for complex general matrix.


Instruction:
Write a Python 3 function that can
rank operation on real general matrix.

Response:
def cublasSger(handle, m, n, alpha, x, incx, y, incy, A, lda):
    """
    Rank-1 operation on real general matrix.

    """

    status = _libcublas.cublasSger_v2(handle,
                                      m, n,
                                      ctypes.byref(ctypes.c_float(alpha)),
                                      int(x), incx,
                                      int(y), incy, int(A), lda)
    cublasCheckStatus(status)


Instruction:
Write a Python 3 script for
ranking operation on real general matrix.

Response:
def cublasDger(handle, m, n, alpha, x, incx, y, incy, A, lda):
    """
    Rank-1 operation on real general matrix.

    """

    status = _libcublas.cublasDger_v2(handle,
                                      m, n,
                                      ctypes.byref(ctypes.c_double(alpha)),
                                      int(x), incx,
                                      int(y), incy, int(A), lda)
    cublasCheckStatus(status)


Instruction:
Write a Python 3 script to
rank operation on complex general matrix.

Response:
def cublasCgeru(handle, m, n, alpha, x, incx, y, incy, A, lda):
    """
    Rank-1 operation on complex general matrix.

    """

    status = _libcublas.cublasCgeru_v2(handle,
                                       m, n, ctypes.byref(cuda.cuFloatComplex(alpha.real,
                                                                              alpha.imag)),
                                       int(x), incx, int(y), incy, int(A), lda)
    cublasCheckStatus(status)


Instruction:
How would you explain what the following Python 3 function does
def cublasZgerc(handle, m, n, alpha, x, incx, y, incy, A, lda):

    status = _libcublas.cublasZgerc_v2(handle,
                                       m, n, ctypes.byref(cuda.cuDoubleComplex(alpha.real,
                                                                               alpha.imag)),
                                       int(x), incx, int(y), incy, int(A), lda)
    cublasCheckStatus(status)

Response: Matrix - matrix product for complex general matrix.


Instruction:
Can you tell what is the following Python 3 function doing
def cublasSsbmv(handle, uplo, n, k, alpha, A, lda, x, incx, beta, y, incy):

    status = _libcublas.cublasSsbmv_v2(handle,
                                       _CUBLAS_FILL_MODE[uplo], n, k,
                                       ctypes.byref(ctypes.c_float(alpha)),
                                       int(A), lda, int(x), incx,
                                       ctypes.byref(ctypes.c_float(beta)),
                                       int(y), incy)
    cublasCheckStatus(status)

Response: Matrix - vector product for real symmetric - banded matrix.


Instruction:
Make a summary of the following Python 3 code
def cublasDspmv(handle, uplo, n, alpha, AP, x, incx, beta, y, incy):

    status = _libcublas.cublasDspmv_v2(handle,
                                       _CUBLAS_FILL_MODE[uplo],
                                       n,
                                       ctypes.byref(ctypes.c_double(alpha)),
                                       ctypes.byref(ctypes.c_double(AP)),
                                       int(x),
                                       incx,
                                       ctypes.byref(ctypes.c_double(beta)),
                                       int(y),
                                       incy)
    cublasCheckStatus(status)

Response: Matrix - vector product for real symmetric - packed matrix.


Instruction:
Explain what the following Python 3 code does
def cublasSspr(handle, uplo, n, alpha, x, incx, AP):

    status = _libcublas.cublasSspr_v2(handle,
                                      _CUBLAS_FILL_MODE[uplo], n,
                                      ctypes.byref(ctypes.c_float(alpha)),
                                      int(x), incx, int(AP))
    cublasCheckStatus(status)

Response: Solve symmetric - packed matrix.


Instruction:
How would you implement a function in Python 3 that
ranks operation on real symmetric-packed matrix.

Response:
def cublasDspr(handle, uplo, n, alpha, x, incx, AP):
    """
    Rank-1 operation on real symmetric-packed matrix.

    """

    status = _libcublas.cublasDspr_v2(handle,
                                      _CUBLAS_FILL_MODE[uplo], n,
                                      ctypes.byref(ctypes.c_double(alpha)),
                                      int(x), incx, int(AP))
    cublasCheckStatus(status)


Instruction:
Here you have a function in Python 3, explain what it does
def cublasSspr2(handle, uplo, n, alpha, x, incx, y, incy, AP):

    status = _libcublas.cublasSspr2_v2(handle,
                                       _CUBLAS_FILL_MODE[uplo], n,
                                       ctypes.byref(ctypes.c_float(alpha)),
                                       int(x), incx, int(y), incy, int(AP))

    cublasCheckStatus(status)

Response: Matrix - matrix product for real symmetric - packed matrix.


Instruction:
Can you tell what is the following Python 3 function doing
def cublasDspr2(handle, uplo, n, alpha, x, incx, y, incy, AP):

    status = _libcublas.cublasDspr2_v2(handle,
                                       _CUBLAS_FILL_MODE[uplo], n,
                                       ctypes.byref(ctypes.c_double(alpha)),
                                       int(x), incx, int(y), incy, int(AP))
    cublasCheckStatus(status)

Response: Matrix - matrix product for real symmetric - packed matrix.


Instruction:
Here you have a function in Python 3, explain what it does
def cublasSsymv(handle, uplo, n, alpha, A, lda, x, incx, beta, y, incy):

    status = _libcublas.cublasSsymv_v2(handle,
                                       _CUBLAS_FILL_MODE[uplo], n,
                                       ctypes.byref(ctypes.c_float(alpha)),
                                       int(A), lda, int(x), incx,
                                       ctypes.byref(ctypes.c_float(beta)),
                                       int(y), incy)
    cublasCheckStatus(status)

Response: Matrix - vector product for real symmetric matrix.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def cublasDsymv(handle, uplo, n, alpha, A, lda, x, incx, beta, y, incy):

    status = _libcublas.cublasDsymv_v2(handle,
                                       _CUBLAS_FILL_MODE[uplo], n,
                                       ctypes.byref(ctypes.c_double(alpha)),
                                       int(A), lda, int(x), incx,
                                       ctypes.byref(ctypes.c_double(beta)),
                                       int(y), incy)
    cublasCheckStatus(status)

Response: Matrix - vector product for real symmetric matrix.


Instruction:
Explain what the following Python 3 code does
def cublasCsymv(handle, uplo, n, alpha, A, lda, x, incx, beta, y, incy):

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

Response: Matrix - vector product for complex symmetric matrix.


Instruction:
Can you tell what is the following Python 3 function doing
def cublasZsymv(handle, uplo, n, alpha, A, lda, x, incx, beta, y, incy):

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

Response: Matrix - vector product for complex symmetric matrix.


Instruction:
Can you tell what is the following Python 3 function doing
def cublasSsyr(handle, uplo, n, alpha, x, incx, A, lda):

    status = _libcublas.cublasSsyr_v2(handle,
                                      _CUBLAS_FILL_MODE[uplo], n,
                                      ctypes.byref(ctypes.c_float(alpha)),
                                      int(x), incx, int(A), lda)
    cublasCheckStatus(status)

Response: Matrix - vector product for real symmetric matrix.


Instruction:
Write a Python 3 function for
ranking operation on real symmetric matrix.

Response:
def cublasDsyr(handle, uplo, n, alpha, x, incx, A, lda):
    """
    Rank-1 operation on real symmetric matrix.

    """

    status = _libcublas.cublasDsyr_v2(handle,
                                      _CUBLAS_FILL_MODE[uplo], n,
                                      ctypes.byref(ctypes.c_double(alpha)),
                                      int(x), incx, int(A), lda)
    cublasCheckStatus(status)


Instruction:
Given the following Python 3 function, write the documentation
def cublasCsyr(handle, uplo, n, alpha, x, incx, A, lda):

    status = _libcublas.cublasCsyr_v2(handle,
                                        _CUBLAS_FILL_MODE[uplo], n,
                                        ctypes.byref(cuda.cuFloatComplex(alpha.real,
                                                                        alpha.imag)),
                                        int(x), incx, int(A), lda)
    cublasCheckStatus(status)

Response: Matrix - vector product for real symmetric matrix.


Instruction:
Write a Python 3 script for
ranking operation on complex symmetric matrix.

Response:
def cublasZsyr(handle, uplo, n, alpha, x, incx, A, lda):
    """
    Rank-1 operation on complex symmetric matrix.

    """

    status = _libcublas.cublasZsyr_v2(handle,
                                        _CUBLAS_FILL_MODE[uplo], n,
                                        ctypes.byref(cuda.cuDoubleComplex(alpha.real,
                                                                        alpha.imag)),
                                        int(x), incx, int(A), lda)
    cublasCheckStatus(status)


Instruction:
How would you explain what the following Python 3 function does
def cublasSsyr2(handle, uplo, n, alpha, x, incx, y, incy, A, lda):

    status = _libcublas.cublasSsyr2_v2(handle,
                                       _CUBLAS_FILL_MODE[uplo], n,
                                       ctypes.byref(ctypes.c_float(alpha)),
                                       int(x), incx, int(y), incy,
                                       int(A), lda)
    cublasCheckStatus(status)

Response: Matrix - matrix product for real symmetric matrix.


Instruction:
Can you write a function in Python 3 where it
ranks operation on real symmetric matrix.

Response:
def cublasDsyr2(handle, uplo, n, alpha, x, incx, y, incy, A, lda):
    """
    Rank-2 operation on real symmetric matrix.

    """

    status = _libcublas.cublasDsyr2_v2(handle,
                                       _CUBLAS_FILL_MODE[uplo], n,
                                       ctypes.byref(ctypes.c_double(alpha)),
                                       int(x), incx, int(y), incy,
                                       int(A), lda)
    cublasCheckStatus(status)


Instruction:
Can you tell what is the following Python 3 function doing
def cublasStpmv(handle, uplo, trans, diag, n, AP, x, incx):

    status = _libcublas.cublasStpmv_v2(handle,
                                       _CUBLAS_FILL_MODE[uplo],
                                       _CUBLAS_OP[trans],
                                       _CUBLAS_DIAG[diag],
                                       n, int(AP), int(x), incx)
    cublasCheckStatus(status)

Response: Matrix - vector product for real triangular - packed matrix.


Instruction:
Can you tell what is the following Python 3 function doing
def cublasDtpmv(handle, uplo, trans, diag, n, AP, x, incx):

    status = _libcublas.cublasDtpmv_v2(handle,
                                       _CUBLAS_FILL_MODE[uplo],
                                       _CUBLAS_OP[trans],
                                       _CUBLAS_DIAG[diag],
                                       n, int(AP), int(x), incx)
    cublasCheckStatus(status)

Response: Matrix - vector product for real triangular - packed matrix.


Instruction:
Implement a function in Python 3 to
solve real triangular - packed system with one right - hand side.

Response:
def cublasStpsv(handle, uplo, trans, diag, n, AP, x, incx):
    """
    Solve real triangular-packed system with one right-hand side.

    """

    status = _libcublas.cublasStpsv_v2(handle,
                                       _CUBLAS_FILL_MODE[uplo],
                                       _CUBLAS_OP[trans],
                                       _CUBLAS_DIAG[diag],
                                       n, int(AP), int(x), incx)
    cublasCheckStatus(status)


Instruction:
Write a Python 3 function that can
solve real triangular-packed system with one right-hand side.

Response:
def cublasDtpsv(handle, uplo, trans, diag, n, AP, x, incx):
    """
    Solve real triangular-packed system with one right-hand side.

    """

    status = _libcublas.cublasDtpsv_v2(handle,
                                       _CUBLAS_FILL_MODE[uplo],
                                       _CUBLAS_OP[trans],
                                       _CUBLAS_DIAG[diag],
                                       n, int(AP), int(x), incx)
    cublasCheckStatus(status)


Instruction:
Given the following Python 3 function, write the documentation
def cublasCtpsv(handle, uplo, trans, diag, n, AP, x, incx):

    status = _libcublas.cublasCtpsv_v2(handle,
                                       _CUBLAS_FILL_MODE[uplo],
                                       _CUBLAS_OP[trans],
                                       _CUBLAS_DIAG[diag],
                                       n, int(AP), int(x), incx)
    cublasCheckStatus(status)

Response: Matrix - vector product for complex triangular - packed system.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def cublasZtpsv(handle, uplo, trans, diag, n, AP, x, incx):

    status = _libcublas.cublasZtpsv_v2(handle,
                                       _CUBLAS_FILL_MODE[uplo],
                                       _CUBLAS_OP[trans],
                                       _CUBLAS_DIAG[diag],
                                       n, int(AP), int(x), incx)
    cublasCheckStatus(status)

Response: Matrix - vector product for real triangular - packed system.


Instruction:
Given the following Python 3 function, write the documentation
def cublasDtrmv(handle, uplo, trans, diag, n, A, lda, x, inx):

    status = _libcublas.cublasDtrmv_v2(handle,
                                       _CUBLAS_FILL_MODE[uplo],
                                       _CUBLAS_OP[trans],
                                       _CUBLAS_DIAG[diag],
                                       n, int(A), lda, int(x), inx)
    cublasCheckStatus(status)

Response: Matrix - vector product for real triangular matrix.


Instruction:
Here you have a function in Python 3, explain what it does
def cublasCher(handle, uplo, n, alpha, x, incx, A, lda):

    status = _libcublas.cublasCher_v2(handle,
                                      _CUBLAS_FILL_MODE[uplo],
                                      n, alpha, int(x), incx, int(A), lda)
    cublasCheckStatus(status)

Response: Matrix - matrix product for real and imaginary part.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def cublasZher(handle, uplo, n, alpha, x, incx, A, lda):

    status = _libcublas.cublasZher_v2(handle,
                                      _CUBLAS_FILL_MODE[uplo],
                                      n, alpha, int(x), incx, int(A), lda)
    cublasCheckStatus(status)

Response: Matrix - vector product for Hermitian matrix.


Instruction:
Make a summary of the following Python 3 code
def cublasChpr(handle, uplo, n, alpha, x, incx, AP):

    status = _libcublas.cublasChpr_v2(handle,
                                      _CUBLAS_FILL_MODE[uplo],
                                      n, ctypes.byref(ctypes.c_float(alpha)),
                                      int(x), incx, int(AP))
    cublasCheckStatus(status)

Response: Matrix - rank - 1 operation on Hermitian - packed matrix.


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

Response:
def cublasZhpr(handle, uplo, n, alpha, x, incx, AP):
    """
    Rank-1 operation on Hermitian-packed matrix.

    """

    status = _libcublas.cublasZhpr_v2(handle,
                                      _CUBLAS_FILL_MODE[uplo],
                                      n, ctypes.byref(ctypes.c_double(alpha)),
                                      int(x), incx, int(AP))
    cublasCheckStatus(status)