Instruction:
Create a Python 3 function to
wrap of ~. select function that returns a new GMQLDataset containing only the regions whose field score is greater than the sample s size.

Response:
def reg_select(self, predicate=None, semiJoinDataset=None, semiJoinMeta=None):
        """
        *Wrapper of* ``SELECT``

        Wrapper of the :meth:`~.select` function filtering regions only based on region attributes.

        :param predicate: logical predicate on the values of the regions
        :param semiJoinDataset: an other GMQLDataset
        :param semiJoinMeta: a list of metadata
        :return: a new GMQLDataset

        An example of usage::

            new_dataset = dataset.reg_select((dataset.chr == 'chr1') | (dataset.pValue < 0.9))

        You can also use Metadata attributes in selection::

            new_dataset = dataset.reg_select(dataset.score > dataset['size'])

        This statement selects all the regions whose field score is strictly higher than the sample
        metadata attribute size.

        The region selection predicate can use all the classical equalities and disequalities
        {>, <, >=, <=, ==, !=} and predicates can be connected by the classical logical symbols
        {& (AND), | (OR), ~ (NOT)} plus the *isin* function.

        In order to be sure about the correctness of the expression, please use parenthesis to delimit
        the various predicates.
        """
        return self.select(region_predicate=predicate, semiJoinMeta=semiJoinMeta, semiJoinDataset=semiJoinDataset)


Instruction:
Create a Python 3 function to
create a new GMQLDataset from the input dataset and returns the new dataset with all the samples in the input one.

Response:
def project(self, projected_meta=None, new_attr_dict=None, all_but_meta=None,
                projected_regs=None, new_field_dict=None, all_but_regs=None):
        """
        *Wrapper of* ``PROJECT``

        The PROJECT operator creates, from an existing dataset, a new dataset with all the samples
        (with their regions and region values) in the input one, but keeping for each sample in the input
        dataset only those metadata and/or region attributes expressed in the operator parameter list.
        Region coordinates and values of the remaining metadata and region attributes remain equal
        to those in the input dataset. Differently from the SELECT operator, PROJECT allows to:
            * Remove existing metadata and/or region attributes from a dataset;
            * Create new metadata and/or region attributes to be added to the result.

        :param projected_meta: list of metadata attributes to project on
        :param new_attr_dict: an optional dictionary of the form {'new_meta_1': function1,
               'new_meta_2': function2, ...} in which every function computes
               the new metadata attribute based on the values of the others
        :param all_but_meta: list of metadata attributes that must be excluded from the projection
        :param projected_regs: list of the region fields to select
        :param new_field_dict: an optional dictionary of the form {'new_field_1':
               function1, 'new_field_2': function2, ...} in which every function
               computes the new region field based on the values of the others
        :param all_but_regs: list of region fields that must be excluded from the projection
        :return: a new GMQLDataset
        """
        projected_meta_exists = False
        if isinstance(projected_meta, list) and \
                all([isinstance(x, str) for x in projected_meta]):
            projected_meta = Some(projected_meta)
            projected_meta_exists = True
        elif projected_meta is None:
            projected_meta = none()
        else:
            raise TypeError("projected_meta must be a list of strings or None."
                            " {} was provided".format(type(projected_meta)))

        if isinstance(new_attr_dict, dict):
            meta_ext = []
            expBuild = self.pmg.getNewExpressionBuilder(self.__index)
            for k in new_attr_dict.keys():
                item = new_attr_dict[k]
                if isinstance(k, str):
                    if isinstance(item, MetaField):
                        me = expBuild.createMetaExtension(k, item.getMetaExpression())
                    elif isinstance(item, int):
                        me = expBuild.createMetaExtension(k, expBuild.getMEType("int", str(item)))
                    elif isinstance(item, str):
                        me = expBuild.createMetaExtension(k, expBuild.getMEType("string", item))
                    elif isinstance(item, float):
                        me = expBuild.createMetaExtension(k, expBuild.getMEType("float", str(item)))
                    else:
                        raise TypeError("Type {} of item of new_attr_dict is not valid".format(type(item)))
                    meta_ext.append(me)
                else:
                    raise TypeError("The key of new_attr_dict must be a string. "
                                    "{} was provided".format(type(k)))
            meta_ext = Some(meta_ext)
        elif new_attr_dict is None:
            meta_ext = none()
        else:
            raise TypeError("new_attr_dict must be a dictionary."
                            " {} was provided".format(type(new_attr_dict)))

        if isinstance(all_but_meta, list) and \
            all([isinstance(x, str) for x in all_but_meta]):
            if not projected_meta_exists:
                all_but_meta = Some(all_but_meta)
                all_but_value = True
            else:
                raise ValueError("all_but_meta and projected_meta are mutually exclusive")
        elif all_but_meta is None:
            all_but_meta = none()
            all_but_value = False
        else:
            raise TypeError("all_but_meta must be a list of strings."
                            " {} was provided".format(type(all_but_meta)))
        projected_meta = all_but_meta if all_but_value else projected_meta

        projected_regs_exists = False
        if isinstance(projected_regs, list) and \
                all([isinstance(x, str) for x in projected_regs]):
            projected_regs = Some(projected_regs)
            projected_regs_exists = True
        elif projected_regs is None:
            projected_regs = none()
        else:
            raise TypeError("projected_regs must be a list of strings or None."
                            " {} was provided".format(type(projected_regs)))

        if isinstance(new_field_dict, dict):
            regs_ext = []
            expBuild = self.pmg.getNewExpressionBuilder(self.__index)
            for k in new_field_dict.keys():
                item = new_field_dict[k]
                if isinstance(k, str):
                    if isinstance(item, RegField):
                        re = expBuild.createRegionExtension(k, item.getRegionExpression())
                    elif isinstance(item, MetaField):
                        re = expBuild.createRegionExtension(k, item.reMetaNode)
                    elif isinstance(item, int):
                        re = expBuild.createRegionExtension(k, expBuild.getREType("float", str(item)))
                    elif isinstance(item, str):
                        re = expBuild.createRegionExtension(k, expBuild.getREType("string", item))
                    elif isinstance(item, float):
                        re = expBuild.createRegionExtension(k, expBuild.getREType("float", str(item)))
                    else:
                        raise TypeError("Type {} of item of new_field_dict is not valid".format(type(item)))
                    regs_ext.append(re)
                else:
                    raise TypeError("The key of new_field_dict must be a string. "
                                    "{} was provided".format(type(k)))
            regs_ext = Some(regs_ext)
        elif new_field_dict is None:
            regs_ext = none()
        else:
            raise TypeError("new_field_dict must be a dictionary."
                            " {} was provided".format(type(new_field_dict)))

        if isinstance(all_but_regs, list) and \
            all([isinstance(x, str) for x in all_but_regs]):
            if not projected_regs_exists:
                all_but_regs = Some(all_but_regs)
            else:
                raise ValueError("all_but_meta and projected_meta are mutually exclusive")
        elif all_but_regs is None:
            all_but_regs = none()
        else:
            raise TypeError("all_but_regs must be a list of strings."
                            " {} was provided".format(type(all_but_regs)))

        new_index = self.opmng.project(self.__index, projected_meta, meta_ext, all_but_value,
                                       projected_regs, all_but_regs, regs_ext)
        return GMQLDataset(index=new_index, location=self.location,
                           local_sources=self._local_sources, remote_sources=self._remote_sources,
                           meta_profile=self.meta_profile)


Instruction:
Can you write a function in Python 3 where it
projects the metadata based on a list of attribute names and return a new GMQLDataset object

Response:
def meta_project(self, attr_list=None, all_but=None, new_attr_dict=None):
        """
        *Wrapper of* ``PROJECT``

        Project the metadata based on a list of attribute names

        :param attr_list: list of the metadata fields to select
        :param all_but: list of metadata that must be excluded from the projection.
        :param new_attr_dict: an optional dictionary of the form {'new_field_1': function1,
               'new_field_2': function2, ...} in which every function computes
               the new field based on the values of the others
        :return: a new GMQLDataset

        Notice that if attr_list is specified, all_but cannot be specified and viceversa.

        Examples::

            new_dataset = dataset.meta_project(attr_list=['antibody', 'ID'],
                                               new_attr_dict={'new_meta': dataset['ID'] + 100})

        """
        return self.project(projected_meta=attr_list, new_attr_dict=new_attr_dict, all_but_meta=all_but)


Instruction:
Can you tell what is the following Python 3 function doing
def extend(self, new_attr_dict):
        if isinstance(new_attr_dict, dict):
            expBuild = self.pmg.getNewExpressionBuilder(self.__index)
            aggregates = []
            for k in new_attr_dict.keys():
                if isinstance(k, str):
                    item = new_attr_dict[k]
                    if isinstance(item, Aggregate):
                        op_name = item.get_aggregate_name()
                        op_argument = Some(item.get_argument()) if item.is_unary() else none()
                        regsToMeta = expBuild.getRegionsToMeta(op_name, k, op_argument)
                        aggregates.append(regsToMeta)
                    else:
                        raise TypeError("The items in new_reg_fields must be Aggregates."
                                        " {} was provided".format(type(item)))
                else:
                    raise TypeError("The key of new_attr_dict must be a string. "
                                    "{} was provided".format(type(k)))
        else:
            raise TypeError("new_attr_dict must be a dictionary. "
                            "{} was provided".format(type(new_attr_dict)))

        new_index = self.opmng.extend(self.__index, aggregates)
        return GMQLDataset(index=new_index, location=self.location,
                           local_sources=self._local_sources,
                           remote_sources=self._remote_sources, meta_profile=self.meta_profile)

Response: This method extends the current GMQLDataset with the values from the input dictionary.


Instruction:
How would you explain what the following Python 3 function does
def normal_cover(self, minAcc, maxAcc, groupBy=None, new_reg_fields=None):
        return self.cover(minAcc, maxAcc, groupBy, new_reg_fields, cover_type="normal")

Response: A wrapper around the COVER method that returns a new set of normal entries for the given time range.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def flat_cover(self, minAcc, maxAcc, groupBy=None, new_reg_fields=None):
        return self.cover(minAcc, maxAcc, groupBy, new_reg_fields, cover_type="flat")

Response: Returns a flat version of the cover function that returns all the regions in the COVER that are in the given range.


Instruction:
Implement a function in Python 3 to
return a COVER that returns only those that are within the given minimum and maximum accumulation index.

Response:
def summit_cover(self, minAcc, maxAcc, groupBy=None, new_reg_fields=None):
        """
        *Wrapper of* ``COVER``

        Variant of the function :meth:`~.cover` that returns only those portions of the COVER
        result where the maximum number of regions overlap (this is done by returning only
        regions that start from a position after which the number of overlaps does not
        increase, and stop at a position where either the number of overlapping regions decreases
        or violates the maximum accumulation index).

        Equivalent to calling::

            cover("summit", ...)
        """
        return self.cover(minAcc, maxAcc, groupBy, new_reg_fields, cover_type="summit")


Instruction:
Implement a function in Python 3 to
return a histogram of all regions contributing to a particular accumulation value.

Response:
def histogram_cover(self, minAcc, maxAcc, groupBy=None, new_reg_fields=None):
        """
        *Wrapper of* ``COVER``

        Variant of the function :meth:`~.cover` that returns all regions contributing to
        the COVER divided in different (contiguous) parts according to their accumulation
        index value (one part for each different accumulation value), which is assigned to
        the AccIndex region attribute.

        Equivalent to calling::

            cover("histogram", ...)
        """
        return self.cover(minAcc, maxAcc, groupBy, new_reg_fields, cover_type="histogram")


Instruction:
Can you generate a brief explanation for the following Python 3 code
def join(self, experiment, genometric_predicate, output="LEFT", joinBy=None,
             refName="REF", expName="EXP", left_on=None, right_on=None):
        """
        *Wrapper of* ``JOIN``

        The JOIN operator takes in input two datasets, respectively known as anchor
        (the first/left one) and experiment (the second/right one) and returns a
        dataset of samples consisting of regions extracted from the operands
        according to the specified condition (known as genometric predicate).
        The number of generated output samples is the Cartesian product of the number
        of samples in the anchor and in the experiment dataset (if no joinby close
        if specified). The attributes (and their values) of the regions in the output
        dataset are the union of the region attributes (with their values) in the input
        datasets; homonymous attributes are disambiguated by prefixing their name with
        their dataset name. The output metadata are the union of the input metadata,
        with their attribute names prefixed with their input dataset name.

        :param experiment: an other GMQLDataset
        :param genometric_predicate: a list of Genometric atomic conditions. For an explanation of each of them
                                     go to the respective page.
        :param output: one of four different values that declare which region is given in output
                       for each input pair of anchor and experiment regions satisfying the genometric predicate:

                       * 'LEFT': outputs the anchor regions from the anchor dataset that satisfy the genometric predicate
                       * 'RIGHT': outputs the anchor regions from the experiment dataset that satisfy the genometric predicate
                       * 'INT': outputs the overlapping part (intersection) of the anchor and experiment regions that satisfy
                         the genometric predicate; if the intersection is empty, no output is produced
                       * 'CONTIG': outputs the concatenation between the anchor and experiment regions that satisfy the
                         genometric predicate, i.e. the output region is defined as having left (right) coordinates
                         equal to the minimum (maximum) of the corresponding coordinate values in the anchor and
                         experiment regions satisfying the genometric predicate

        :param joinBy: list of metadata attributes
        :param refName: name that you want to assign to the reference dataset
        :param expName: name that you want to assign to the experiment dataset
        :param left_on: list of region fields of the reference on which the join must be performed
        :param right_on: list of region fields of the experiment on which the join must be performed
        :return: a new GMQLDataset

        An example of usage, in which we perform the join operation between Example_Dataset_1 and Example_Dataset_2
        specifying than we want to join the regions of the former with the first regions at a minimim distance of 120Kb
        of the latter and finally we want to output the regions of Example_Dataset_2 matching the criteria::

            import gmql as gl

            d1 = gl.get_example_dataset("Example_Dataset_1")
            d2 = gl.get_example_dataset("Example_Dataset_2")

            result_dataset = d1.join(experiment=d2,
                                     genometric_predicate=[gl.MD(1), gl.DGE(120000)],
                                     output="right")
        """

        if isinstance(experiment, GMQLDataset):
            other_idx = experiment.__index
        else:
            raise TypeError("experiment must be a GMQLDataset. "
                            "{} was provided".format(type(experiment)))
        if isinstance(genometric_predicate, list) and \
            all([isinstance(x, GenometricCondition) for x in genometric_predicate]):
            regionJoinCondition = self.opmng.getRegionJoinCondition(list(map(lambda x: x.get_gen_condition(),
                                                                             genometric_predicate)))
        else:
            raise TypeError("genometric_predicate must be a list og GenometricCondition. "
                            "{} was found".format(type(genometric_predicate)))

        if isinstance(output, str):
            regionBuilder = self.opmng.getRegionBuilderJoin(output)
        else:
            raise TypeError("output must be a string. "
                            "{} was provided".format(type(output)))

        if not isinstance(expName, str):
            raise TypeError("expName must be a string. {} was provided".format(type(expName)))

        if not isinstance(refName, str):
            raise TypeError("refName must be a string. {} was provided".format(type(expName)))

        if isinstance(joinBy, list) and \
            all([isinstance(x, str) for x in joinBy]):
            metaJoinCondition = Some(self.opmng.getMetaJoinCondition(joinBy))
        elif joinBy is None:
            metaJoinCondition = none()
        else:
            raise TypeError("joinBy must be a list of strings. "
                            "{} was found".format(type(joinBy)))

        left_on_exists = False
        left_on_len = 0
        if isinstance(left_on, list) and \
            all([isinstance(x, str) for x in left_on]):
            left_on_len = len(left_on)
            left_on = Some(left_on)
            left_on_exists = True
        elif left_on is None:
            left_on = none()
        else:
            raise TypeError("left_on must be a list of strings. "
                            "{} was provided".format(type(left_on)))

        if isinstance(right_on, list) and \
            all([isinstance(x, str)] for x in right_on) and \
            left_on_exists and len(right_on) == left_on_len:
            right_on = Some(right_on)
        elif right_on is None and not left_on_exists:
            right_on = none()
        else:
            raise TypeError("right_on must be a list of strings. "
                            "{} was provided".format(type(right_on)))

        new_index = self.opmng.join(self.__index, other_idx,
                                    metaJoinCondition, regionJoinCondition, regionBuilder,
                                    refName, expName, left_on, right_on)
        new_local_sources, new_remote_sources = self.__combine_sources(self, experiment)
        new_location = self.__combine_locations(self, experiment)
        return GMQLDataset(index=new_index, location=new_location,
                           local_sources=new_local_sources,
                           remote_sources=new_remote_sources, meta_profile=self.meta_profile)

Response: This function joins two GMQLDataset objects and returns a GMQLDataset object that is the union of the input two datasets with the same number of samples and the number of samples in the output.


Instruction:
Here you have a function in Python 3, explain what it does
def difference(self, other, joinBy=None, exact=False):

        if isinstance(other, GMQLDataset):
            other_idx = other.__index
        else:
            raise TypeError("other must be a GMQLDataset. "
                            "{} was provided".format(type(other)))

        if isinstance(joinBy, list) and \
            all([isinstance(x, str) for x in joinBy]):
            metaJoinCondition = Some(self.opmng.getMetaJoinCondition(joinBy))
        elif joinBy is None:
            metaJoinCondition = none()
        else:
            raise TypeError("joinBy must be a list of strings. "
                            "{} was provided".format(type(joinBy)))

        if not isinstance(exact, bool):
            raise TypeError("exact must be a boolean. "
                            "{} was provided".format(type(exact)))

        new_index = self.opmng.difference(self.__index, other_idx, metaJoinCondition, exact)

        new_local_sources, new_remote_sources = self.__combine_sources(self, other)
        new_location = self.__combine_locations(self, other)
        return GMQLDataset(index=new_index, location=new_location,
                           local_sources=new_local_sources,
                           remote_sources=new_remote_sources,
                           meta_profile=self.meta_profile)

Response: This method returns a GMQLDataset that contains the difference between the current and the given other GMQLDataset.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def union(self, other, left_name="LEFT", right_name="RIGHT"):

        if not isinstance(left_name, str) or \
            not isinstance(right_name, str):
            raise TypeError("left_name and right_name must be strings. "
                            "{} - {} was provided".format(type(left_name), type(right_name)))

        if isinstance(other, GMQLDataset):
            other_idx = other.__index
        else:
            raise TypeError("other must be a GMQLDataset. "
                            "{} was provided".format(type(other)))

        if len(left_name) == 0 or len(right_name) == 0:
            raise ValueError("left_name and right_name must not be empty")
        new_index = self.opmng.union(self.__index, other_idx, left_name, right_name)
        new_local_sources, new_remote_sources = self.__combine_sources(self, other)
        new_location = self.__combine_locations(self, other)
        return GMQLDataset(index=new_index, location=new_location,
                           local_sources=new_local_sources,
                           remote_sources=new_remote_sources,
                           meta_profile=self.meta_profile)

Response: This function performs a union operation on the current GMQLDataset and returns the result.


Instruction:
Explain what the following Python 3 code does
def merge(self, groupBy=None):

        if isinstance(groupBy, list) and \
            all([isinstance(x, str) for x in groupBy]):
            groupBy = Some(groupBy)
        elif groupBy is None:
            groupBy = none()
        else:
            raise TypeError("groupBy must be a list of strings. "
                            "{} was provided".format(type(groupBy)))

        new_index = self.opmng.merge(self.__index, groupBy)
        return GMQLDataset(index=new_index, location=self.location,
                           local_sources=self._local_sources,
                           remote_sources=self._remote_sources,
                           meta_profile=self.meta_profile)

Response: This method creates a new GMQLDataset that contains all the samples in the input dataset and the union of all the metadata attribute - values of the input samples.


Instruction:
Write a Python 3 script for
grouping the samples by meta and meta_aggregates.

Response:
def group(self, meta=None, meta_aggregates=None, regs=None,
              regs_aggregates=None, meta_group_name="_group"):
        """
        *Wrapper of* ``GROUP``

        The GROUP operator is used for grouping both regions and/or metadata of input
        dataset samples according to distinct values of certain attributes (known as grouping
        attributes); new grouping attributes are added to samples in the output dataset,
        storing the results of aggregate function evaluations over metadata and/or regions
        in each group of samples.
        Samples having missing values for any of the grouping attributes are discarded.

        :param meta: (optional) a list of metadata attributes
        :param meta_aggregates: (optional) {'new_attr': fun}
        :param regs: (optional) a list of region fields
        :param regs_aggregates: {'new_attr': fun}
        :param meta_group_name: (optional) the name to give to the group attribute in the
               metadata
        :return: a new GMQLDataset

        Example of usage. We group samples by `antibody` and we aggregate the region pvalues taking the maximum value
        calling the new region field `maxPvalue`::

            import gmql as gl

            d1 = gl.get_example_dataset("Example_Dataset_1")
            result = d1.group(meta=['antibody'], regs_aggregates={'maxPvalue': gl.MAX("pvalue")})
        """

        if isinstance(meta, list) and \
            all([isinstance(x, str) for x in meta]):
            meta = Some(meta)
        elif meta is None:
            meta = none()
        else:
            raise TypeError("meta must be a list of strings. "
                            "{} was provided".format(type(meta)))
        expBuild = self.pmg.getNewExpressionBuilder(self.__index)
        if isinstance(meta_aggregates, dict):
            metaAggregates = []
            for k in meta_aggregates:
                if isinstance(k, str):
                    item = meta_aggregates[k]
                    if isinstance(item, (SUM, MIN, MAX, AVG, BAG,
                                         BAGD, STD, MEDIAN, COUNTSAMP)):
                        functionName = item.get_aggregate_name()
                        argument = item.get_argument()
                        if argument is None:
                            argument = none()
                        else:
                            argument = Some(argument)
                        metaAggregates.append(expBuild.createMetaAggregateFunction(functionName,
                                                                                k, argument))
                    else:
                        raise TypeError("the item of the dictionary must be an Aggregate of the following: "
                                        "SUM, MIN, MAX, AVG, BAG, BAGD, STD, COUNTSAMP. "
                                        "{} was provided".format(type(item)))
                else:
                    raise TypeError("keys of meta_aggregates must be string. "
                                    "{} was provided".format(type(k)))
            metaAggregates = Some(metaAggregates)
        elif meta_aggregates is None:
            metaAggregates = none()
        else:
            raise TypeError("meta_aggregates must be a dictionary of Aggregate functions. "
                            "{} was provided".format(type(meta_aggregates)))
        if isinstance(regs, list) and \
            all([isinstance(x, str) for x in regs]):
            regs = Some(regs)
        elif regs is None:
            regs = none()
        else:
            raise TypeError("regs must be a list of strings. "
                            "{} was provided".format(type(regs)))

        if isinstance(regs_aggregates, dict):
            regionAggregates = []
            for k in regs_aggregates.keys():
                if isinstance(k, str):
                    item = regs_aggregates[k]
                    if isinstance(item, (SUM, MIN, MAX, AVG, BAG, BAGD,
                                         MEDIAN, COUNT)):
                        op_name = item.get_aggregate_name()
                        op_argument = item.get_argument()
                        if op_argument is None:
                            op_argument = none()
                        else:
                            op_argument = Some(op_argument)
                        regsToReg = expBuild.getRegionsToRegion(op_name, k, op_argument)
                        regionAggregates.append(regsToReg)
                    else:
                        raise TypeError("the item of the dictionary must be an Aggregate of the following: "
                                        "SUM, MIN, MAX, AVG, BAG, BAGD, MEDIAN, COUNT. "
                                        "{} was provided".format(type(item)))
                else:
                    raise TypeError("The key of new_reg_fields must be a string. "
                                    "{} was provided".format(type(k)))
            regionAggregates = Some(regionAggregates)
        elif regs_aggregates is None:
            regionAggregates = none()
        else:
            raise TypeError("new_reg_fields must be a list of dictionary. "
                            "{} was provided".format(type(regs_aggregates)))

        if isinstance(meta_group_name, str):
            pass
        else:
            raise TypeError("meta_group_name must be a string. "
                            "{} was provided".format(type(meta_group_name)))

        new_index = self.opmng.group(self.__index, meta, metaAggregates, meta_group_name, regs, regionAggregates)

        return GMQLDataset(index=new_index, location=self.location,
                           local_sources=self._local_sources,
                           remote_sources=self._remote_sources,
                           meta_profile=self.meta_profile)


Instruction:
Here you have a function in Python 3, explain what it does
def meta_group(self, meta, meta_aggregates=None):
        return self.group(meta=meta, meta_aggregates=meta_aggregates)

Response: Group only for metadata.


Instruction:
Here you have a function in Python 3, explain what it does
def regs_group(self, regs, regs_aggregates=None):
        return self.group(regs=regs, regs_aggregates=regs_aggregates)

Response:
*Wrapper of* ``GROUP``

        Group operation only for region data. For further information check :meth:`~.group`


Instruction:
How would you explain what the following Python 3 function does
def materialize(self, output_path=None, output_name=None,  all_load=True):
        current_mode = get_mode()
        new_index = self.__modify_dag(current_mode)
        if current_mode == 'local':
            return Materializations.materialize_local(new_index, output_path, all_load)
        elif current_mode == 'remote':
            return Materializations.materialize_remote(new_index, output_name, output_path, all_load)
        else:
            raise ValueError("Current mode is not defined. {} given".format(current_mode))

Response: Materialize the GMQL dataset.


Instruction:
Make a summary of the following Python 3 code
def step(self):
        message = self.get_message()
        if message is None:

            self.sleep(self.idle_time)
            return None

        self.dispatch(message)
        # In non-greenthread environments this does nothing. In green-thread
        # environments this yields the context so messages can be acted upon
        # before exhausting the threadpool.
        self.sleep(0)

Response: Grab a new message and dispatch it to the handler.


Instruction:
Given the following Python 3 function, write the documentation
def command(self, command, token, team_id=None, methods=['GET'], **kwargs):
        if team_id is None:
            team_id = self.team_id
        if team_id is None:
            raise RuntimeError('TEAM_ID is not found in your configuration!')

        def deco(func):
            self._commands[(team_id, command)] = (func, token, methods, kwargs)
            return func
        return deco

Response: A decorator to register a command.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def dispatch(self):
        from flask import request

        method = request.method

        data = request.args
        if method == 'POST':
            data = request.form

        token = data.get('token')
        team_id = data.get('team_id')
        command = data.get('command') or data.get('trigger_word')

        if isinstance(command, string_types):
            command = command.strip().lstrip('/')

        try:
            self.validate(command, token, team_id, method)
        except SlackError as e:
            return self.response(e.msg)

        func, _, _, kwargs = self._commands[(team_id, command)]
        kwargs.update(data.to_dict())

        return func(**kwargs)

Response: Dispatch http request to registerd commands.


Instruction:
Make a summary of the following Python 3 code
def validate(self, command, token, team_id, method):
        if (team_id, command) not in self._commands:
            raise SlackError('Command {0} is not found in team {1}'.format(
                             command, team_id))

        func, _token, methods, kwargs = self._commands[(team_id, command)]

        if method not in methods:
            raise SlackError('{} request is not allowed'.format(method))

        if token != _token:
            raise SlackError('Your token {} is invalid'.format(token))

Response: Validate request queries with registerd commands


Instruction:
Can you generate the documentation for the following Python 3 function
def response(self, text, response_type='ephemeral', attachments=None):
        from flask import jsonify
        if attachments is None:
            attachments = []

        data = {
            'response_type': response_type,
            'text': text,
            'attachments': attachments,
        }
        return jsonify(**data)

Response: Return a response with json format


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

            with open(self.pidfile, 'r') as pidfile:

                try:

                    pid = int(pidfile.read().strip())

                except ValueError:

                    return None

                try:

                    os.kill(pid, 0)

                except OSError as e:

                    if e.errno == errno.EPERM:

                        return pid

                    elif e.errno == errno.ESRCH:

                        return None

                    LOG.exception(
                        "os.kill returned unhandled error "
                        "{0}".format(e.strerror)
                    )
                    sys.exit(exit.PIDFILE_ERROR)

                return pid

        except IOError:

            if not os.path.isfile(self.pidfile):

                return None

            LOG.exception("Failed to read pidfile {0}.".format(self.pidfile))
            sys.exit(exit.PIDFILE_INACCESSIBLE)

Response: Get the pid which represents a daemonized process.


Instruction:
Can you write a function in Python 3 where it
sets the pid for a running process.

Response:
def pid(self, pidnum):
        """Set the pid for a running process."""
        try:

            with open(self.pidfile, "w+") as pidfile:

                pidfile.write("{0}\n".format(pidnum))

        except IOError:

            LOG.exception("Failed to write pidfile {0}).".format(self.pidfile))
            sys.exit(exit.PIDFILE_INACCESSIBLE)


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

            os.remove(self.pidfile)

        except IOError:

            if not os.path.isfile(self.pidfile):

                return None

            LOG.exception("Failed to clear pidfile {0}).".format(self.pidfile))
            sys.exit(exit.PIDFILE_INACCESSIBLE)

Response: Stop managing the current pid.


Instruction:
Write a Python 3 function that can
get an gevent pool used to dispatch requests.

Response:
def pool(self):
        """Get an gevent pool used to dispatch requests."""
        self._pool = self._pool or gevent.pool.Pool(size=self.pool_size)
        return self._pool


Instruction:
Can you tell what is the following Python 3 function doing
def set_essid(interface, essid):
    interface = _get_bytes(interface)
    essid = _get_bytes(essid)

    wrq = ffi.new('struct iwreq*')

    with iwlib_socket() as sock:
        if essid.lower() in (b'off', b'any'):
            wrq.u.essid.flags = 0
            essid = b''
        elif essid.lower() == b'on':
            buf = ffi.new('char []', iwlib.IW_ESSID_MAX_SIZE+1)
            wrq.u.essid.pointer = buf
            wrq.u.essid.length = iwlib.IW_ESSID_MAX_SIZE + 1
            wrq.u.essid.flags = 0
            if iwlib.iw_get_ext(sock, interface, iwlib.SIOCGIWESSID, wrq) < 0:
                raise ValueError("Error retrieving previous ESSID: %s" % (os.strerror(ffi.errno)))
            wrq.u.essid.flags = 1
        elif len(essid) > iwlib.IW_ESSID_MAX_SIZE:
            raise ValueError("ESSID '%s' is longer than the maximum %d" % (essid, iwlib.IW_ESSID_MAX_SIZE))
        else:
            wrq.u.essid.pointer = ffi.new_handle(essid)
            wrq.u.essid.length = len(essid)
            wrq.u.essid.flags = 1

        if iwlib.iw_get_kernel_we_version() < 21:
            wrq.u.essid.length += 1

        if iwlib.iw_set_ext(sock, interface, iwlib.SIOCSIWESSID, wrq) < 0:
            errno = ffi.errno
            strerror = "Couldn't set essid on device '%s': %s" % (interface.decode('utf8'), os.strerror(errno))
            raise OSError(errno, strerror)

Response: Sets the ESSID of a given interface.


Instruction:
Given the following Python 3 function, write the documentation
def read_adjacency_matrix(file_path, separator):
    # Open file
    file_row_generator = get_file_row_generator(file_path, separator)

    # Initialize lists for row and column sparse matrix arguments
    row = list()
    col = list()
    append_row = row.append
    append_col = col.append

    # Read all file rows
    for file_row in file_row_generator:
        source_node = np.int64(file_row[0])
        target_node = np.int64(file_row[1])

        # Add edge
        append_row(source_node)
        append_col(target_node)

        # Since this is an undirected network also add the reciprocal edge
        append_row(target_node)
        append_col(source_node)

    row = np.array(row, dtype=np.int64)
    col = np.array(col, dtype=np.int64)
    data = np.ones_like(row, dtype=np.float64)

    number_of_nodes = np.max(row)  # I assume that there are no missing nodes at the end.

    # Array count should start from 0.
    row -= 1
    col -= 1

    # Form sparse adjacency matrix
    adjacency_matrix = sparse.coo_matrix((data, (row, col)), shape=(number_of_nodes, number_of_nodes))

    return adjacency_matrix

Response: Reads an edge list in csv format and returns the adjacency matrix in SciPy Sparse COOrdinate format.


Instruction:
Given the following Python 3 function, write the documentation
def read_node_label_matrix(file_path, separator, number_of_nodes):
    # Open file
    file_row_generator = get_file_row_generator(file_path, separator)

    # Initialize lists for row and column sparse matrix arguments
    row = list()
    col = list()
    append_row = row.append
    append_col = col.append

    # Populate the arrays
    for file_row in file_row_generator:
        node = np.int64(file_row[0])
        label = np.int64(file_row[1])

        # Add label
        append_row(node)
        append_col(label)

    number_of_categories = len(set(col))  # I assume that there are no missing labels. There may be missing nodes.
    labelled_node_indices = np.array(list(set(row)))

    row = np.array(row, dtype=np.int64)
    col = np.array(col, dtype=np.int64)
    data = np.ones_like(row, dtype=np.float64)

    # Array count should start from 0.
    row -= 1
    col -= 1
    labelled_node_indices -= 1

    # Form sparse adjacency matrix
    node_label_matrix = sparse.coo_matrix((data, (row, col)), shape=(number_of_nodes, number_of_categories))
    node_label_matrix = node_label_matrix.tocsr()

    return node_label_matrix, number_of_categories, labelled_node_indices

Response: Reads node - label pairs in csv format and returns a list of tuples and a sparse adjacency matrix.


Instruction:
Create a Python 3 function for
ensuring that the Garuda directory and files are created.

Response:
def ensure_data():
    '''
    Ensure that the Garuda directory and files
    '''
    if not os.path.exists(GARUDA_DIR):
        os.makedirs(GARUDA_DIR)
    Path(f'{GARUDA_DIR}/__init__.py').touch()


Instruction:
Explain what the following Python 3 code does
def protoc_arguments():
    '''
    Construct protobuf compiler arguments
    '''
    proto_include = resource_filename('grpc_tools', '_proto')
    return [
        protoc.__file__, '-I', GARUDA_DIR, f'--python_out={GARUDA_DIR}',
        f'--grpc_python_out={GARUDA_DIR}', GARUDA_PROTO_PATH,
        f'-I{proto_include}']

Response: Construct protobuf compiler arguments


Instruction:
Can you generate the documentation for the following Python 3 function
def write_average_score_row(fp, score_name, scores):
    row = "--" + score_name + "--"
    fp.write(row)
    for vector in scores:
        row = list(vector)
        row = [str(score) for score in row]
        row = "\n" + "\t".join(row)
        fp.write(row)

Response: Simple utility function that writes an average score row in a file designated by a file pointer.


Instruction:
Can you write a function in Python 3 where it
catches specific errors in admin actions and shows a friendly error.

Response:
def catch_errors(f):
    """
    Catches specific errors in admin actions and shows a friendly error.
    """

    @functools.wraps(f)
    def wrapper(self, request, *args, **kwargs):
        try:
            return f(self, request, *args, **kwargs)
        except exceptions.CertificateExpired:
            self.message_user(
                request,
                _('The AFIP Taxpayer certificate has expired.'),
                messages.ERROR,
            )
        except exceptions.UntrustedCertificate:
            self.message_user(
                request,
                _('The AFIP Taxpayer certificate is untrusted.'),
                messages.ERROR,
            )
        except exceptions.CorruptCertificate:
            self.message_user(
                request,
                _('The AFIP Taxpayer certificate is corrupt.'),
                messages.ERROR,
            )
        except exceptions.AuthenticationError as e:
            logger.exception('AFIP auth failed')
            self.message_user(
                request,
                _('An unknown authentication error has ocurred: %s') % e,
                messages.ERROR,
            )

    return wrapper


Instruction:
Explain what the following Python 3 code does
def augmented_tf_idf(attribute_matrix):
    number_of_documents = attribute_matrix.shape[0]

    max_term_frequencies = np.ones(number_of_documents, dtype=np.float64)
    idf_array = np.ones(attribute_matrix.shape[1], dtype=np.float64)

    # Calculate inverse document frequency
    attribute_matrix = attribute_matrix.tocsc()
    for j in range(attribute_matrix.shape[1]):
        document_frequency = attribute_matrix.getcol(j).data.size
        if document_frequency > 1:
            idf_array[j] = np.log(number_of_documents/document_frequency)

    # Calculate maximum term frequencies for a user
    attribute_matrix = attribute_matrix.tocsr()
    for i in range(attribute_matrix.shape[0]):
        max_term_frequency = attribute_matrix.getrow(i).data
        if max_term_frequency.size > 0:
            max_term_frequency = max_term_frequency.max()
            if max_term_frequency > 0.0:
                max_term_frequencies[i] = max_term_frequency

    # Do augmented tf-idf normalization
    attribute_matrix = attribute_matrix.tocoo()
    attribute_matrix.data = 0.5 + np.divide(0.5*attribute_matrix.data, np.multiply((max_term_frequencies[attribute_matrix.row]), (idf_array[attribute_matrix.col])))
    attribute_matrix = attribute_matrix.tocsr()

    return attribute_matrix

Response: Augmented TF - IDF normalization on a bag - of - words vector representation of data.


Instruction:
Make a summary of the following Python 3 code
def get_file_row_generator(file_path, separator, encoding=None):
    with open(file_path, encoding=encoding) as file_object:
        for line in file_object:
            words = line.strip().split(separator)
            yield words

Response: Reads an separated value file row by row.


Instruction:
How would you implement a function in Python 3 that
stores a pickle object to a given path.

Response:
def store_pickle(file_path, data):
    """
    Pickle some data to a given path.

    Inputs: - file_path: Target file path.
            - data: The python object to be serialized via pickle.
    """
    pkl_file = open(file_path, 'wb')
    pickle.dump(data, pkl_file)
    pkl_file.close()


Instruction:
Can you tell what is the following Python 3 function doing
def load_pickle(file_path):
    pkl_file = open(file_path, 'rb')
    data = pickle.load(pkl_file)
    pkl_file.close()
    return data

Response: Load a pickle file from disk.


Instruction:
Make a summary of the following Python 3 code
def makeServoIDPacket(curr_id, new_id):
	pkt = Packet.makeWritePacket(curr_id, xl320.XL320_ID, [new_id])
	return pkt

Response: Returns a packet to set the servo to a new ID


Instruction:
Can you generate the documentation for the following Python 3 function
def pagerank_limit_push(s, r, w_i, a_i, push_node, rho):
    # Calculate the A and B quantities to infinity
    A_inf = rho*r[push_node]
    B_inf = (1-rho)*r[push_node]

    # Update approximate Pagerank and residual vectors
    s[push_node] += A_inf
    r[push_node] = 0.0

    # Update residual vector at push node's adjacent nodes
    r[a_i] += B_inf * w_i

Response: Calculates the approximate Pagerank and residual vectors at a given push node.


Instruction:
Can you tell what is the following Python 3 function doing
def pagerank_lazy_push(s, r, w_i, a_i, push_node, rho, lazy):
    # Calculate the A, B and C quantities
    A = rho*r[push_node]
    B = (1-rho)*(1 - lazy)*r[push_node]
    C = (1-rho)*lazy*(r[push_node])

    # Update approximate Pagerank and residual vectors
    s[push_node] += A
    r[push_node] = C

    # Update residual vector at push node's adjacent nodes
    r[a_i] += B * w_i

Response: Calculates the approximate Pagerank and residual vectors at a given push node.


Instruction:
How would you explain what the following Python 3 function does
def cumulative_pagerank_difference_limit_push(s, r, w_i, a_i, push_node, rho):
    # Calculate the commute quantity
    commute = (1-rho)*r[push_node]

    # Update approximate regularized commute and residual vectors
    r[push_node] = 0.0

    # Update residual vector at push node's adjacent nodes
    commute_probabilities = commute * w_i
    s[a_i] += commute_probabilities
    r[a_i] += commute_probabilities

Response: This function performs a random step without a self - loop.


Instruction:
Write a Python 3 function for
including pyramid_multiauth into a pyramid configurator.

Response:
def includeme(config):
    """Include pyramid_multiauth into a pyramid configurator.

    This function provides a hook for pyramid to include the default settings
    for auth via pyramid_multiauth.  Activate it like so:

        config.include("pyramid_multiauth")

    This will pull the list of registered authn policies from the deployment
    settings, and configure and install each policy in order.  The policies to
    use can be specified in one of two ways:

        * as the name of a module to be included.
        * as the name of a callable along with a set of parameters.

    Here's an example suite of settings:

        multiauth.policies = ipauth1 ipauth2 pyramid_browserid

        multiauth.policy.ipauth1.use = pyramid_ipauth.IPAuthentictionPolicy
        multiauth.policy.ipauth1.ipaddrs = 123.123.0.0/16
        multiauth.policy.ipauth1.userid = local1

        multiauth.policy.ipauth2.use = pyramid_ipauth.IPAuthentictionPolicy
        multiauth.policy.ipauth2.ipaddrs = 124.124.0.0/16
        multiauth.policy.ipauth2.userid = local2

    This will configure a MultiAuthenticationPolicy with three policy objects.
    The first two will be IPAuthenticationPolicy objects created by passing
    in the specified keyword arguments.  The third will be a BrowserID
    authentication policy just like you would get from executing:

        config.include("pyramid_browserid")

    As a side-effect, the configuration will also get the additional views
    that pyramid_browserid sets up by default.

    The *group finder function* and the *authorization policy* are also read
    from configuration if specified:

        multiauth.authorization_policy = mypyramidapp.acl.Custom
        multiauth.groupfinder  = mypyramidapp.acl.groupfinder
    """
    # Grab the pyramid-wide settings, to look for any auth config.
    settings = config.get_settings()
    # Hook up a default AuthorizationPolicy.
    # Get the authorization policy from config if present.
    # Default ACLAuthorizationPolicy is usually what you want.
    authz_class = settings.get("multiauth.authorization_policy",
                               "pyramid.authorization.ACLAuthorizationPolicy")
    authz_policy = config.maybe_dotted(authz_class)()
    # If the app configures one explicitly then this will get overridden.
    # In autocommit mode this needs to be done before setting the authn policy.
    config.set_authorization_policy(authz_policy)
    # Get the groupfinder from config if present.
    groupfinder = settings.get("multiauth.groupfinder", None)
    groupfinder = config.maybe_dotted(groupfinder)
    # Look for callable policy definitions.
    # Suck them all out at once and store them in a dict for later use.
    policy_definitions = get_policy_definitions(settings)
    # Read and process the list of policies to load.
    # We build up a list of callables which can be executed at config commit
    # time to obtain the final list of policies.
    # Yeah, it's complicated.  But we want to be able to inherit any default
    # views or other config added by the sub-policies when they're included.
    # Process policies in reverse order so that things at the front of the
    # list can override things at the back of the list.
    policy_factories = []
    policy_names = settings.get("multiauth.policies", "").split()
    for policy_name in reversed(policy_names):
        if policy_name in policy_definitions:
            # It's a policy defined using a callable.
            # Just append it straight to the list.
            definition = policy_definitions[policy_name]
            factory = config.maybe_dotted(definition.pop("use"))
            policy_factories.append((factory, policy_name, definition))
        else:
            # It's a module to be directly included.
            try:
                factory = policy_factory_from_module(config, policy_name)
            except ImportError:
                err = "pyramid_multiauth: policy %r has no settings "\
                      "and is not importable" % (policy_name,)
                raise ValueError(err)
            policy_factories.append((factory, policy_name, {}))
    # OK.  We now have a list of callbacks which need to be called at
    # commit time, and will return the policies in reverse order.
    # Register a special action to pull them into our list of policies.
    policies = []

    def grab_policies():
        for factory, name, kwds in policy_factories:
            policy = factory(**kwds)
            if policy:
                policy._pyramid_multiauth_name = name
                if not policies or policy is not policies[0]:
                    # Remember, they're being processed in reverse order.
                    # So each new policy needs to go at the front.
                    policies.insert(0, policy)

    config.action(None, grab_policies, order=PHASE2_CONFIG)
    authn_policy = MultiAuthenticationPolicy(policies, groupfinder)
    config.set_authentication_policy(authn_policy)


Instruction:
Make a summary of the following Python 3 code
def policy_factory_from_module(config, module):
    # Remember the policy that's active before including the module, if any.
    orig_policy = config.registry.queryUtility(IAuthenticationPolicy)
    # Include the module, so we get any default views etc.
    config.include(module)
    # That might have registered and commited a new policy object.
    policy = config.registry.queryUtility(IAuthenticationPolicy)
    if policy is not None and policy is not orig_policy:
        return lambda: policy
    # Or it might have set up a pending action to register one later.
    # Find the most recent IAuthenticationPolicy action, and grab
    # out the registering function so we can call it ourselves.
    for action in reversed(config.action_state.actions):
        # Extract the discriminator and callable.  This is complicated by
        # Pyramid 1.3 changing action from a tuple to a dict.
        try:
            discriminator = action["discriminator"]
            callable = action["callable"]
        except TypeError:              # pragma: nocover
            discriminator = action[0]  # pragma: nocover
            callable = action[1]       # pragma: nocover
        # If it's not setting the authn policy, keep looking.
        if discriminator is not IAuthenticationPolicy:
            continue

        # Otherwise, wrap it up so we can extract the registered object.
        def grab_policy(register=callable):
            old_policy = config.registry.queryUtility(IAuthenticationPolicy)
            register()
            new_policy = config.registry.queryUtility(IAuthenticationPolicy)
            config.registry.registerUtility(old_policy, IAuthenticationPolicy)
            return new_policy

        return grab_policy
    # Or it might not have done *anything*.
    # So return a null policy factory.
    return lambda: None

Response: Create a function that returns a function that will be called when the module is loaded.


Instruction:
Can you generate the documentation for the following Python 3 function
def get_policy_definitions(settings):
    policy_definitions = {}
    for name in settings:
        if not name.startswith("multiauth.policy."):
            continue
        value = settings[name]
        name = name[len("multiauth.policy."):]
        policy_name, setting_name = name.split(".", 1)
        if policy_name not in policy_definitions:
            policy_definitions[policy_name] = {}
        policy_definitions[policy_name][setting_name] = value
    return policy_definitions

Response: Find all multiauth policy definitions from the settings dict.


Instruction:
Explain what the following Python 3 code does
def load_file(filename):
    if filename in ('stdin', '-', None):
        instances = [i for i in gc.get_objects() if isinstance(i, pycodestyle.Checker) or isinstance(i, pep8.Checker)]
        if len(instances) != 1:
            raise ValueError('Expected only 1 instance of pycodestyle.Checker, got {0} instead.'.format(len(instances)))
        return 'stdin', ''.join(instances[0].lines)
    with codecs.open(filename, encoding='utf-8') as handle:
        return filename, handle.read()

Response: Read file to memory.


Instruction:
Can you write a function in Python 3 where it
returns True if code should be ignored False otherwise.

Response:
def ignore(code):
    """Should this code be ignored.

    :param str code: Error code (e.g. D201).

    :return: True if code should be ignored, False otherwise.
    :rtype: bool
    """
    if code in Main.options['ignore']:
        return True
    if any(c in code for c in Main.options['ignore']):
        return True
    return False


Instruction:
Implement a function in Python 3 to
add options to flake8.

Response:
def add_options(cls, parser):
        """Add options to flake8.

        :param parser: optparse.OptionParser from pycodestyle.
        """
        parser.add_option('--show-pydocstyle', action='store_true', help='show explanation of each PEP 257 error')
        parser.config_options.append('show-pydocstyle')


Instruction:
Given the following Python 3 function, write the documentation
def parse_options(cls, options):
        # Handle flake8 options.
        cls.options['explain'] = bool(options.show_pydocstyle)
        cls.options['ignore'] = options.ignore

        # Handle pydocstyle options.
        config = pydocstyle.RawConfigParser()
        for file_name in pydocstyle.ConfigurationParser.PROJECT_CONFIG_FILES:
            if config.read(os.path.join(os.path.abspath('.'), file_name)):
                break
        if not config.has_section('pydocstyle'):
            return
        native_options = dict()
        for option in config.options('pydocstyle'):
            if option == 'ignore':
                native_options['ignore'] = config.get('pydocstyle', option)
            if option in ('explain', 'source'):
                native_options[option] = config.getboolean('pydocstyle', option)
        native_options['show-source'] = native_options.pop('source', None)
        if native_options.get('ignore'):
            native_options['ignore'] = native_options['ignore'].split(',')
        cls.options.update(dict((k, v) for k, v in native_options.items() if v))

Response: Read parsed options from flake8.


Instruction:
Write a Python 3 script to
run analysis on a single file.

Response:
def run(self):
        """Run analysis on a single file."""
        pydocstyle.Error.explain = self.options['explain']
        filename, source = load_file(self.filename)
        for error in pydocstyle.PEP257Checker().check_source(source, filename):
            if not hasattr(error, 'code') or ignore(error.code):
                continue
            lineno = error.line
            offset = 0  # Column number starting from 0.
            explanation = error.explanation if pydocstyle.Error.explain else ''
            text = '{0} {1}{2}'.format(error.code, error.message.split(': ', 1)[1], explanation)
            yield lineno, offset, text, Main


Instruction:
Write a Python 3 function that can
return the barcode s number without the verification digit.

Response:
def numbers(self):
        """"
        Returns the barcode's number without the verification digit.

        :return: list(int)
        """
        numstring = '{:011d}{:02d}{:04d}{}{}'.format(
            self._receipt.point_of_sales.owner.cuit,  # 11 digits
            int(self._receipt.receipt_type.code),  # 2 digits
            self._receipt.point_of_sales.number,  # point of sales
            self._receipt.validation.cae,  # 14 digits
            self._receipt.validation.cae_expiration.strftime('%Y%m%d'),  # 8
        )
        return [int(num) for num in numstring]


Instruction:
How would you implement a function in Python 3 that
returns the verification digit for a given list of numbers.

Response:
def verification_digit(numbers):
        """
        Returns the verification digit for a given numbre.

        The verification digit is calculated as follows:

        * A = sum of all even-positioned numbers
        * B = A * 3
        * C = sum of all odd-positioned numbers
        * D = B + C
        * The results is the smallset number N, such that (D + N) % 10 == 0

        NOTE: Afip's documentation seems to have odd an even mixed up in the
        explanation, but all examples follow the above algorithm.

        :param list(int) numbers): The numbers for which the digits is to be
            calculated.
        :return: int
        """
        a = sum(numbers[::2])
        b = a * 3
        c = sum(numbers[1::2])
        d = b + c
        e = d % 10
        if e == 0:
            return e
        return 10 - e


Instruction:
Explain what the following Python 3 code does
def full_number(self):
        return '{}{}'.format(
            ''.join(str(n) for n in self.numbers),
            ReceiptBarcodeGenerator.verification_digit(self.numbers),
        )

Response: Returns the full number including the verification digit.


Instruction:
Write a Python 3 function for
reading an adjacency matrix in csv format and returns the adjacency matrix in SciPy Sparse COOrdinate format.

Response:
def read_adjacency_matrix(file_path, separator, numbering="matlab"):
    """
    Reads an edge list in csv format and returns the adjacency matrix in SciPy Sparse COOrdinate format.

    Inputs:  - file_path: The path where the adjacency matrix is stored.
             - separator: The delimiter among values (e.g. ",", "\t", " ")
             - numbering: Array numbering style: * "matlab"
                                                 * "c"

    Outputs: - adjacency_matrix: The adjacency matrix in SciPy Sparse COOrdinate format.
    """
    # Open file
    file_row_generator = get_file_row_generator(file_path, separator)

    file_row = next(file_row_generator)
    number_of_rows = file_row[1]
    number_of_columns = file_row[3]
    directed = file_row[7]
    if directed == "True":
        directed = True
    elif directed == "False":
        directed = False
    else:
        print("Invalid metadata.")
        raise RuntimeError

    # Initialize lists for row and column sparse matrix arguments
    row = list()
    col = list()
    data = list()
    append_row = row.append
    append_col = col.append
    append_data = data.append

    # Read all file rows
    for file_row in file_row_generator:
        source_node = np.int64(file_row[0])
        target_node = np.int64(file_row[1])
        edge_weight = np.float64(file_row[2])

        # Add edge
        append_row(source_node)
        append_col(target_node)
        append_data(edge_weight)

        # Since this is an undirected network also add the reciprocal edge
        if not directed:
            if source_node != target_node:
                append_row(target_node)
                append_col(source_node)
                append_data(edge_weight)

    row = np.array(row, dtype=np.int64)
    col = np.array(col, dtype=np.int64)
    data = np.array(data, dtype=np.float64)

    if numbering == "matlab":
        row -= 1
        col -= 1
    elif numbering == "c":
        pass
    else:
        print("Invalid numbering style.")
        raise RuntimeError

    # Form sparse adjacency matrix
    adjacency_matrix = spsp.coo_matrix((data, (row, col)), shape=(number_of_rows, number_of_columns))

    return adjacency_matrix


Instruction:
Write a Python 3 script to
read node - label pairs in csv format and returns a list of tuples and a matrix of node - label pairs.

Response:
def read_node_label_matrix(file_path, separator, numbering="matlab"):
    """
    Reads node-label pairs in csv format and returns a list of tuples and a node-label matrix.

    Inputs:  - file_path: The path where the node-label matrix is stored.
             - separator: The delimiter among values (e.g. ",", "\t", " ")
             - number_of_nodes: The number of nodes of the full graph. It is possible that not all nodes are labelled.
             - numbering: Array numbering style: * "matlab"
                                                 * "c"

    Outputs: - node_label_matrix: The node-label associations in a NumPy array of tuples format.
             - number_of_categories: The number of categories/classes the nodes may belong to.
             - labelled_node_indices: A NumPy array containing the labelled node indices.
    """
    # Open file
    file_row_generator = get_file_row_generator(file_path, separator)

    file_row = next(file_row_generator)
    number_of_rows = file_row[1]
    number_of_categories = int(file_row[3])

    # Initialize lists for row and column sparse matrix arguments
    row = list()
    col = list()
    append_row = row.append
    append_col = col.append

    # Populate the arrays
    for file_row in file_row_generator:
        node = np.int64(file_row[0])
        label = np.int64(file_row[1])

        # Add label
        append_row(node)
        append_col(label)

    labelled_node_indices = np.array(list(set(row)))

    row = np.array(row, dtype=np.int64)
    col = np.array(col, dtype=np.int64)
    data = np.ones_like(row, dtype=np.float64)

    if numbering == "matlab":
        row -= 1
        col -= 1
        labelled_node_indices -= 1
    elif numbering == "c":
        pass
    else:
        print("Invalid numbering style.")
        raise RuntimeError

    # Form sparse adjacency matrix
    node_label_matrix = spsp.coo_matrix((data, (row, col)), shape=(number_of_rows, number_of_categories))
    node_label_matrix = node_label_matrix.tocsr()

    return node_label_matrix, number_of_categories, labelled_node_indices


Instruction:
Can you tell what is the following Python 3 function doing
def write_screen_name_to_topics(filepath, user_label_matrix, node_to_id, id_to_name, label_to_lemma, lemma_to_keyword, separator=","):
    user_label_matrix = spsp.coo_matrix(user_label_matrix)

    shape = user_label_matrix.shape
    nnz = user_label_matrix.getnnz()

    row = user_label_matrix.row
    col = user_label_matrix.col
    data = user_label_matrix.data

    name_to_topic_set = defaultdict(set)

    for edge in range(row.size):
        node = row[edge]
        user_twitter_id = node_to_id[node]
        name = id_to_name[user_twitter_id]

        label = col[edge]
        lemma = label_to_lemma[label]
        # topic = lemma_to_keyword[lemma]

        name_to_topic_set[name].add(lemma)

    with open(filepath, "w") as f:
        # Write metadata.
        file_row = "n_rows:" + separator + str(shape[0]) + separator +\
                   "nnz:" + separator + str(nnz) + separator +\
                   "\n"
        f.write(file_row)

        for name, topic_set in name_to_topic_set.items():
            file_row = list()
            file_row.append(name)
            file_row.extend(topic_set)
            file_row = separator.join(file_row) + "\n"
            f.write(file_row)

Response: Writes a user name and associated topic names per row.


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

	d = {
		'id': pkt[4],
		# 'instruction': xl320.InstrToStr[pkt[7]],
		# 'length': (pkt[6] << 8) + pkt[5],
		# 'params': pkt[8:-2],
		'Model Number': (pkt[10] << 8) + pkt[9],
		'Firmware Ver': pkt[11],
		'Error': ErrorStatusMsg[pkt[8]],
		# 'crc': pkt[-2:]
	}

	return d

Response: This function turns a packet into a dictionary.


Instruction:
Here you have a function in Python 3, explain what it does
def sweep(port, rate, ID, retry=3):
	if port == 'dummy':
		s = ServoSerial(port, rate, fake=True)
	else:
		s = ServoSerial(port, rate)

	if ID < 0:
		ID = xl320.XL320_BROADCAST_ADDR

	try:
		s.open()
	except SerialException as e:
		# print('Error opening serial port:')
		print('-'*40)
		print(sys.argv[0], ':')
		print(e)
		exit(1)

	pkt = makePingPacket(ID)
	# print('ping', pkt)
	s.write(pkt)

	# as more servos add up, I might need to increase the cnt number???
	for cnt in range(retry):
		ans = s.read()

		if ans:
			for pkt in ans:
				servo = packetToDict(pkt)
				utils.prettyPrintPacket(servo)
				print('raw pkt: {}'.format(pkt))
		else:
			print('Try {}: no servos found'.format(cnt))

		time.sleep(0.1)

	s.close()

Response: Send a ping packet to the specified ID and returns any returned values.


Instruction:
Can you generate the documentation for the following Python 3 function
def calculate_epsilon_effective(rho, epsilon, seed_degree, neighbor_degrees, mean_degree):
    # Calculate a weighted neighborhood degree average.
    # neighborhood_degree = rho*seed_degree + (1-rho)*neighbor_degrees.mean()
    neighborhood_degree = neighbor_degrees.mean()

    # Calculate the seed neighborhood normalized effective epsilon.
    epsilon_effective = (epsilon*np.log(1 + seed_degree))/np.log(1 + neighborhood_degree)

    # Calculate the maximum epsilon for at least one push on a neighboring node.
    # Also the minimum epsilon for a push on all the neighboring nodes.
    epsilon_effective_maximum = np.max(1/(seed_degree*neighbor_degrees))
    epsilon_effective_minimum = np.min(1/(seed_degree*neighbor_degrees))

    # print(epsilon_effective, epsilon_effective_maximum, epsilon_effective_minimum)

    # The maximum epsilon is absolute, whereas we regularize for the minimum.
    if epsilon_effective > epsilon_effective_maximum:
        epsilon_effective = epsilon_effective_maximum
    elif epsilon_effective < epsilon_effective_minimum:
        epsilon_effective = (epsilon_effective_minimum + epsilon_effective)/2

    return epsilon_effective

Response: Calculates the effective epsilon threshold for a single node.


Instruction:
Explain what the following Python 3 code does
def arcte_with_lazy_pagerank(adjacency_matrix, rho, epsilon, number_of_threads=None):
    adjacency_matrix = sparse.csr_matrix(adjacency_matrix)
    number_of_nodes = adjacency_matrix.shape[0]

    if number_of_threads is None:
        number_of_threads = get_threads_number()
    if number_of_threads == 1:
        # Calculate natural random walk transition probability matrix.
        rw_transition, out_degree, in_degree = get_natural_random_walk_matrix(adjacency_matrix, make_shared=False)

        a = adjacency_matrix.copy()
        a.data = np.ones_like(a.data)
        edge_count_vector = np.squeeze(np.asarray(a.sum(axis=0), dtype=np.int64))

        iterate_nodes = np.where(edge_count_vector != 0)[0]
        argsort_indices = np.argsort(edge_count_vector[iterate_nodes])
        iterate_nodes = iterate_nodes[argsort_indices][::-1]
        iterate_nodes = iterate_nodes[np.where(edge_count_vector[iterate_nodes] > 1.0)[0]]

        # iterate_nodes = np.where(out_degree != 0)[0]
        # argsort_indices = np.argsort(out_degree[iterate_nodes])
        # iterate_nodes = iterate_nodes[argsort_indices][::-1]
        # iterate_nodes = iterate_nodes[np.where(out_degree[iterate_nodes] > 1.0)[0]]

        local_features = arcte_with_lazy_pagerank_worker(iterate_nodes,
                                                         rw_transition.indices,
                                                         rw_transition.indptr,
                                                         rw_transition.data,
                                                         out_degree,
                                                         in_degree,
                                                         rho,
                                                         epsilon)
    else:
        # Calculate natural random walk transition probability matrix.
        rw_transition, out_degree, in_degree = get_natural_random_walk_matrix(adjacency_matrix, make_shared=True)

        a = adjacency_matrix.copy()
        a.data = np.ones_like(a.data)
        edge_count_vector = np.squeeze(np.asarray(a.sum(axis=0), dtype=np.int64))

        iterate_nodes = np.where(edge_count_vector != 0)[0]
        argsort_indices = np.argsort(edge_count_vector[iterate_nodes])
        iterate_nodes = iterate_nodes[argsort_indices][::-1]
        iterate_nodes = iterate_nodes[np.where(edge_count_vector[iterate_nodes] > 1.0)[0]]

        # iterate_nodes = np.where(out_degree != 0)[0]
        # argsort_indices = np.argsort(out_degree[iterate_nodes])
        # iterate_nodes = iterate_nodes[argsort_indices][::-1]
        # iterate_nodes = iterate_nodes[np.where(out_degree[iterate_nodes] > 1.0)[0]]

        pool = mp.Pool(number_of_threads)
        node_chunks = list(parallel_chunks(iterate_nodes, number_of_threads))
        node_count = 0
        for chunk in node_chunks:
            node_count += len(list(chunk))
        results = list()
        for chunk_no in range(len(pool._pool)):
            pool.apply_async(arcte_with_lazy_pagerank_worker,
                             args=(node_chunks[chunk_no],
                                   rw_transition.indices,
                                   rw_transition.indptr,
                                   rw_transition.data,
                                   out_degree,
                                   in_degree,
                                   rho,
                                   epsilon),
                             callback=results.append)
        pool.close()
        pool.join()
        # local_features = sparse.hstack(results)
        local_features = results[0]
        for additive_features in results[1:]:
            local_features += additive_features
        local_features = sparse.csr_matrix(local_features)

    # Form base community feature matrix.
    identity_matrix = sparse.csr_matrix(sparse.eye(number_of_nodes, number_of_nodes, dtype=np.float64))
    adjacency_matrix_ones = adjacency_matrix
    adjacency_matrix_ones.data = np.ones_like(adjacency_matrix.data)
    base_community_features = identity_matrix + adjacency_matrix_ones

    # Stack horizontally matrices to form feature matrix.
    try:
        features = sparse.hstack([base_community_features, local_features]).tocsr()
    except ValueError as e:
        print("Failure with horizontal feature stacking.")
        features = base_community_features

    return features

Response: This function extracts local community features for all graph nodes based on the partitioning of node - centric similarity vectors.


Instruction:
Here you have a function in Python 3, explain what it does
def get_folds_generator(node_label_matrix,
                        labelled_node_indices,
                        number_of_categories,
                        dataset_memory_folder,
                        percentage,
                        number_of_folds=10):
    """
    Read or form and store the seed nodes for training and testing.

    Inputs: - node_label_matrix: The node-label ground truth in a SciPy sparse matrix format.
            - labelled_node_indices: A NumPy array containing the labelled node indices.
            - number_of_categories: The number of categories/classes in the learning.
            - memory_path: The folder where the results are stored.
            - percentage: The percentage of labelled samples that will be used for training.

    Output: - folds: A generator containing train/test set folds.
    """
    number_of_labeled_nodes = labelled_node_indices.size
    training_set_size = int(np.ceil(percentage*number_of_labeled_nodes/100))

    ####################################################################################################################
    # Read or generate folds
    ####################################################################################################################
    fold_file_path = dataset_memory_folder + "/folds/" + str(percentage) + "_folds.txt"
    train_list = list()
    test_list = list()
    if not os.path.exists(fold_file_path):
        with open(fold_file_path, "w") as fp:
            for trial in np.arange(number_of_folds):
                train, test = valid_train_test(node_label_matrix[labelled_node_indices, :],
                                               training_set_size,
                                               number_of_categories,
                                               trial)
                train = labelled_node_indices[train]
                test = labelled_node_indices[test]

                # Write test nodes
                row = [str(node) for node in test]
                row = "\t".join(row) + "\n"
                fp.write(row)

                # Write train nodes
                row = [str(node) for node in train]
                row = "\t".join(row) + "\n"
                fp.write(row)

                train_list.append(train)
                test_list.append(test)
    else:
        file_row_gen = get_file_row_generator(fold_file_path, "\t")

        for trial in np.arange(number_of_folds):
            # Read test nodes
            test = next(file_row_gen)
            test = [int(node) for node in test]
            test = np.array(test)

            # Read train nodes
            train = next(file_row_gen)
            train = [int(node) for node in train]
            train = np.array(train)

            train_list.append(train)
            test_list.append(test)

    folds = ((train, test) for train, test in zip(train_list, test_list))
    return folds

Response: Returns a generator that generates train and test folds for training and testing.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def generate_folds(node_label_matrix, labelled_node_indices, number_of_categories, percentage, number_of_folds=10):
    number_of_labeled_nodes = labelled_node_indices.size
    training_set_size = int(np.ceil(percentage*number_of_labeled_nodes/100))

    ####################################################################################################################
    # Generate folds
    ####################################################################################################################
    train_list = list()
    test_list = list()
    for trial in np.arange(number_of_folds):
        train, test = valid_train_test(node_label_matrix[labelled_node_indices, :],
                                       training_set_size,
                                       number_of_categories,
                                       trial)
        train = labelled_node_indices[train]
        test = labelled_node_indices[test]

        train_list.append(train)
        test_list.append(test)

    folds = ((train, test) for train, test in zip(train_list, test_list))
    return folds

Response: Generates the train and test set folds for training and testing.


Instruction:
Can you generate the documentation for the following Python 3 function
def iterative_stratification(node_label_matrix, training_set_size, number_of_categories, random_seed=0):
    number_of_labelled_nodes = node_label_matrix.shape[0]
    testing_set_size = number_of_labelled_nodes - training_set_size
    training_set_proportion = training_set_size/number_of_labelled_nodes
    testing_set_proportion = testing_set_size/number_of_labelled_nodes

    # Calculate the desired number of examples of each label at each subset.
    desired_label_number = np.zeros((2, number_of_categories), dtype=np.int64)
    node_label_matrix = node_label_matrix.tocsc()
    for j in range(number_of_categories):
        category_label_number = node_label_matrix.getcol(j).indices.size
        desired_label_number[0, j] = math.ceil(category_label_number*training_set_proportion)
        desired_label_number[1, j] = category_label_number - desired_label_number[0, j]

    train_ids = list()
    test_ids = list()

    append_train_id = train_ids.append
    append_test_id = test_ids.append

    # Randomize process
    np.random.seed(random_seed)
    while True:
        if len(train_ids) + len(test_ids) >= number_of_labelled_nodes:
            break
        # Find the label with the fewest (but at least one) remaining examples, breaking the ties randomly
        remaining_label_distribution = desired_label_number.sum(axis=0)
        min_label = np.min(remaining_label_distribution[np.where(remaining_label_distribution > 0)[0]])
        label_indices = np.where(remaining_label_distribution == min_label)[0]
        chosen_label = int(np.random.choice(label_indices, 1)[0])

        # Find the subset with the largest number of desired examples for this label,
        # breaking ties by considering the largest number of desired examples, breaking further ties randomly.
        fold_max_remaining_labels = np.max(desired_label_number[:, chosen_label])
        fold_indices = np.where(desired_label_number[:, chosen_label] == fold_max_remaining_labels)[0]
        chosen_fold = int(np.random.choice(fold_indices, 1)[0])

        # Choose a random example for the selected label.
        relevant_nodes = node_label_matrix.getcol(chosen_label).indices
        chosen_node = int(np.random.choice(np.setdiff1d(relevant_nodes,
                                                        np.union1d(np.array(train_ids),
                                                                   np.array(test_ids))),
                                           1)[0])
        if chosen_fold == 0:
            append_train_id(chosen_node)
            desired_label_number[0, node_label_matrix.getrow(chosen_node).indices] -= 1
        elif chosen_fold == 1:
            append_test_id(chosen_node)
            desired_label_number[1, node_label_matrix.getrow(chosen_node).indices] -= 1
        else:
            raise RuntimeError

    return np.array(train_ids), np.array(test_ids)

Response: This function generates an iterative stratification of the data folds.


Instruction:
Create a Python 3 function for
giving the discriminator distances y_pred and y_test this function forms the node - label prediction matrix.

Response:
def form_node_label_prediction_matrix(y_pred, y_test):
    """
    Given the discriminator distances, this function forms the node-label prediction matrix.

    It is assumed that the number of true labels is known.

    Inputs:  - y_pred: A NumPy array that contains the distance from the discriminator for each label for each user.
             - y_test: The node-label ground truth for the test set in a SciPy sparse CSR matrix format.

    Outputs: - y_pred: The node-label prediction for the test set in a SciPy sparse CSR matrix format.
    """
    number_of_test_nodes = y_pred.shape[0]

    # We calculate the number of true labels for each node.
    true_number_of_labels = np.squeeze(y_test.sum(axis=1))

    # We sort the prediction array for each node.
    index = np.argsort(y_pred, axis=1)

    row = np.empty(y_test.getnnz(), dtype=np.int64)
    col = np.empty(y_test.getnnz(), dtype=np.int64)
    start = 0
    for n in np.arange(number_of_test_nodes):
        end = start + true_number_of_labels[0, n]
        row[start:end] = n
        col[start:end] = index[n, -1:-true_number_of_labels[0, n]-1:-1]
        start = end
    data = np.ones_like(row, dtype=np.int8)

    y_pred = sparse.coo_matrix((data, (row, col)), shape=y_test.shape)

    return y_pred


Instruction:
Can you tell what is the following Python 3 function doing
def calculate_measures(y_pred, y_test):
    y_pred = y_pred.toarray()
    y_test = y_test.toarray()

    macro_precision, macro_recall, macro_F1, macro_support = precision_recall_fscore_support(y_test,
                                                                                y_pred,
                                                                                beta=1.0,
                                                                                average="macro")

    micro_precision, micro_recall, micro_F1, micro_support = precision_recall_fscore_support(y_test,
                                                                                y_pred,
                                                                                beta=1.0,
                                                                                average="micro")

    F1 = f1_score(y_test,
                  y_pred,
                  average=None)

    measures = [macro_recall, micro_recall, macro_precision, micro_precision, macro_F1, micro_F1, F1]
    return measures

Response: Calculates the F - scores and F - score averages given a classification result and ground truth.


Instruction:
Can you tell what is the following Python 3 function doing
def decode(buff):
		pp = list(map(ord, buff))
		if 0 == len(pp) == 1:
			pp = []
		return pp

Response: Transforms the raw buffer data read in into a list of bytes


Instruction:
Can you generate a brief explanation for the following Python 3 code
def read(self, how_much=128):  # FIXME: 128 might be too much ... what is largest?
		# ret = self.readPkts(how_much)
		# return ret
		ret = []
		self.setRTS(self.DD_READ)

		# this in_waiting is wrong ... i can read more than is there
		# if self.serial.in_waiting < 12:
		# 	time.sleep(0.001)
		# 	print('waiting:', self.serial.in_waiting)
		# else:
		# 	print('waiting:', self.serial.in_waiting)

		data = self.serial.read(how_much)
		# print('read() data', data, 'len(data)', len(data))
		if data:
			data = self.decode(data)
			# print('decode', data)
			ret = Packet.findPkt(data)
			# print('ret', ret)
		return ret

Response: This function reads in data from the RTS pin and returns a list of valid packets.


Instruction:
Given the following Python 3 function, write the documentation
def write(self, pkt):
		self.setRTS(self.DD_WRITE)
		self.flushInput()
		# prep data array for transmition
		pkt = bytearray(pkt)
		pkt = bytes(pkt)

		num = self.serial.write(pkt)
		# self.serial.flush()
		# print('wrote {} of len(pkt) = {}'.format(num, len(pkt)))
		return num

Response: This is a simple serial write command. It writes a packet to the serial port and returns the number of bytes written.


Instruction:
Given the following Python 3 function, write the documentation
def sendPkt(self, pkt, retry=5, sleep_time=0.01):
		for cnt in range(retry):
			self.serial.flushInput()
			self.write(pkt)  # send packet to servo
			ans = self.read()  # get return status packet

			if ans:
				# check for error and resend
				return ans

			else:
				# print('>> retry {} <<'.format(cnt))
				time.sleep(sleep_time)

		return None

Response: sends a packet and waits for a return.


Instruction:
Can you generate the documentation for the following Python 3 function
def scipy_sparse_to_csv(filepath, matrix, separator=",", directed=False, numbering="matlab"):
    matrix = spsp.coo_matrix(matrix)

    shape = matrix.shape
    nnz = matrix.getnnz()

    if numbering == "matlab":
        row = matrix.row + 1
        col = matrix.col + 1
        data = matrix.data
    elif numbering == "c":
        row = matrix.row
        col = matrix.col
        data = matrix.data
    else:
        print("Invalid numbering style.")
        raise RuntimeError

    with open(filepath, "w") as f:
        # Write metadata.
        file_row = "n_rows:" + separator + str(shape[0]) + separator +\
                   "n_cols:" + separator + str(shape[1]) + separator +\
                   "nnz:" + separator + str(nnz) + separator +\
                   "directed:" + separator + str(directed) +\
                   "\n"
        f.write(file_row)

        for edge in range(row.size):
            if directed is False:
                if col[edge] < row[edge]:
                    continue
            file_row = str(row[edge]) + separator + str(col[edge]) + separator + str(data[edge]) + "\n"
            f.write(file_row)

Response: Writes sparse matrix in separated value format.


Instruction:
Can you generate the documentation for the following Python 3 function
def read_adjacency_matrix(file_path, separator, undirected):
    # Open file
    file_row_generator = get_file_row_generator(file_path, separator)

    # Initialize lists for row and column sparse matrix arguments.
    row = list()
    col = list()
    data = list()
    append_row = row.append
    append_col = col.append
    append_data = data.append

    # Initialize node anonymizer.
    id_to_node = dict()

    # Read all file rows.
    for file_row in file_row_generator:
        if file_row[0][0] == "#":
            continue

        source_node_id = int(file_row[0])
        target_node_id = int(file_row[1])

        number_of_nodes = len(id_to_node)
        source_node = id_to_node.setdefault(source_node_id,
                                            number_of_nodes)
        number_of_nodes = len(id_to_node)
        target_node = id_to_node.setdefault(target_node_id,
                                            number_of_nodes)

        edge_weight = float(file_row[2])

        # Add edge.
        append_row(source_node)
        append_col(target_node)
        append_data(edge_weight)

        # Since this is an undirected network also add the reciprocal edge.
        if undirected:
            if source_node != target_node:
                append_row(target_node)
                append_col(source_node)
                append_data(edge_weight)

    number_of_nodes = len(id_to_node)
    node_to_id = dict(zip(list(id_to_node.values()),
                          list(id_to_node.keys())))

    row = np.array(row, dtype=np.int64)
    col = np.array(col, dtype=np.int64)
    data = np.array(data, dtype=np.float64)

    # Form sparse adjacency matrix.
    adjacency_matrix = spsp.coo_matrix((data, (row, col)), shape=(number_of_nodes,
                                                                  number_of_nodes))

    return adjacency_matrix, node_to_id

Response: Reads an adjacency matrix in csv format and returns the adjacency matrix.


Instruction:
Can you generate the documentation for the following Python 3 function
def publish(build):
    build.packages.install("wheel")
    build.packages.install("twine")
    build.executables.run([
        "python", "setup.py",
        "sdist", "bdist_wheel", "--universal", "--release"
    ])
    build.executables.run([
        "twine", "upload", "dist/*"
    ])

Response: publish the package itself


Instruction:
Write a Python 3 script for
calculating the approximate personalized PageRank starting from a seed node without self - loops.

Response:
def fast_approximate_personalized_pagerank(s,
                                           r,
                                           w_i,
                                           a_i,
                                           out_degree,
                                           in_degree,
                                           seed_node,
                                           rho=0.2,
                                           epsilon=0.00001):
    """
    Calculates the approximate personalized PageRank starting from a seed node without self-loops.
    """
    # Initialize approximate PageRank and residual distributions
    # s = np.zeros(number_of_nodes, dtype=np.float64)
    # r = np.zeros(number_of_nodes, dtype=np.float64)
    r[seed_node] = 1.0

    # Initialize queue of nodes to be pushed
    pushable = deque()
    pushable.append(seed_node)

    # Do one push anyway
    push_node = pushable.popleft()

    pagerank_limit_push(s,
                        r,
                        w_i[push_node],
                        a_i[push_node],
                        push_node,
                        rho)
    number_of_push_operations = 1

    i = np.where(np.divide(r[a_i[push_node]], in_degree[a_i[push_node]]) >= epsilon)[0]
    if i.size > 0:
        pushable.extend(a_i[push_node][i])

    while len(pushable) > 0:
        # While there are nodes with large residual probabilities, push
        push_node = pushable.popleft()
        if r[push_node]/in_degree[push_node] >= epsilon:
            pagerank_limit_push(s,
                                r,
                                w_i[push_node],
                                a_i[push_node],
                                push_node,
                                rho)
            number_of_push_operations += 1

            i = np.where(np.divide(r[a_i[push_node]], in_degree[a_i[push_node]]) >= epsilon)[0]
            if i.size > 0:
                pushable.extend(a_i[push_node][i])

    return number_of_push_operations


Instruction:
Implement a Python 3 function for
calculating the approximate personalized PageRank starting from a seed node with self - loops.

Response:
def lazy_approximate_personalized_pagerank(s,
                                           r,
                                           w_i,
                                           a_i,
                                           out_degree,
                                           in_degree,
                                           seed_node,
                                           rho=0.2,
                                           epsilon=0.00001,
                                           laziness_factor=0.5):
    """
    Calculates the approximate personalized PageRank starting from a seed node with self-loops.

    Introduced in: Andersen, R., Chung, F., & Lang, K. (2006, October).
                   Local graph partitioning using pagerank vectors.
                   In Foundations of Computer Science, 2006. FOCS'06. 47th Annual IEEE Symposium on (pp. 475-486). IEEE.
    """
    # Initialize approximate PageRank and residual distributions
    # s = np.zeros(number_of_nodes, dtype=np.float64)
    # r = np.zeros(number_of_nodes, dtype=np.float64)
    r[seed_node] = 1.0

    # Initialize queue of nodes to be pushed
    pushable = deque()
    pushable.append(seed_node)

    # Do one push anyway
    push_node = pushable.popleft()

    pagerank_lazy_push(s,
                       r,
                       w_i[push_node],
                       a_i[push_node],
                       push_node,
                       rho,
                       laziness_factor)
    number_of_push_operations = 1

    i = np.where(np.divide(r[a_i[push_node]], in_degree[a_i[push_node]]) >= epsilon)[0]
    if i.size > 0:
        pushable.extend(a_i[push_node][i])

    while r[push_node]/in_degree[push_node] >= epsilon:
        pagerank_lazy_push(s,
                           r,
                           w_i[push_node],
                           a_i[push_node],
                           push_node,
                           rho,
                           laziness_factor)
        number_of_push_operations += 1

    # While there are nodes with large residual probabilities, push
    while len(pushable) > 0:
        push_node = pushable.popleft()

        if r[push_node]/in_degree[push_node] >= epsilon:
            pagerank_lazy_push(s,
                               r,
                               w_i[push_node],
                               a_i[push_node],
                               push_node,
                               rho,
                               laziness_factor)
            number_of_push_operations += 1

            i = np.where(np.divide(r[a_i[push_node]], in_degree[a_i[push_node]]) >= epsilon)[0]
            if i.size > 0:
                pushable.extend(a_i[push_node][i])

        while r[push_node]/in_degree[push_node] >= epsilon:
            pagerank_lazy_push(s,
                               r,
                               w_i[push_node],
                               a_i[push_node],
                               push_node,
                               rho,
                               laziness_factor)
            number_of_push_operations += 1

    return number_of_push_operations


Instruction:
Here you have a function in Python 3, explain what it does
def read_matlab_features(array_paths, number_of_nodes, dimensionality):
    # Read the data array
    file_row_gen = get_file_row_generator(array_paths[0], "\t")
    data = list()
    append_data = data.append
    for file_row in file_row_gen:
        append_data(float(file_row[0]))

    # Read the row array
    file_row_gen = get_file_row_generator(array_paths[1], "\t")
    row = list()
    append_row = row.append
    for file_row in file_row_gen:
        append_row(int(float(file_row[0])))

    # Read the data array
    file_row_gen = get_file_row_generator(array_paths[2], "\t")
    col = list()
    append_col = col.append
    for file_row in file_row_gen:
        append_col(int(float(file_row[0])))

    data = np.array(data).astype(np.float64)
    row = np.array(row).astype(np.int64) - 1  # Due to Matlab numbering
    col = np.array(col).astype(np.int64) - 1  # Due to Matlab numbering

    print(np.max(row), np.min(row))
    print(np.max(col), np.min(col))

    # centroids_new = sparse.coo_matrix((data, (row, col)), shape=(number_of_nodes + 1, k))
    features = spsp.coo_matrix((data, (row, col)), shape=(number_of_nodes, dimensionality))

    return features

Response: Reads the Matlab feature matrix from the given array of files.