Instruction:
Can you generate the documentation for the following Python 3 function
def get(self, obj, **kwargs):
        assert self.getter is not None, "Getter accessor is not specified."
        if callable(self.getter):
            return self.getter(obj, **_get_context(self._getter_argspec, kwargs))

        assert isinstance(self.getter, string_types), "Accessor must be a function or a dot-separated string."

        for attr in self.getter.split("."):
            if isinstance(obj, dict):
                obj = obj[attr]
            else:
                obj = getattr(obj, attr)

        if callable(obj):
            return obj()

        return obj

Response: Get an attribute from an object.


Instruction:
Create a Python 3 function to
set value for obj s attribute.

Response:
def set(self, obj, value):
        """Set value for obj's attribute.

        :param obj: Result object or dict to assign the attribute to.
        :param value: Value to be assigned.
        """
        assert self.setter is not None, "Setter accessor is not specified."
        if callable(self.setter):
            return self.setter(obj, value)

        assert isinstance(self.setter, string_types), "Accessor must be a function or a dot-separated string."

        def _set(obj, attr, value):
            if isinstance(obj, dict):
                obj[attr] = value
            else:
                setattr(obj, attr, value)
            return value

        path = self.setter.split(".")
        for attr in path[:-1]:
            obj = _set(obj, attr, {})

        _set(obj, path[-1], value)


Instruction:
Can you create a Python 3 function that
gets an attribute s accessor with the getter and the setter.

Response:
def accessor(self):
        """Get an attribute's accessor with the getter and the setter.

        :return: `Accessor` instance.
        """
        if isinstance(self.attr, Accessor):
            return self.attr

        if callable(self.attr):
            return Accessor(getter=self.attr)

        attr = self.attr or self.name
        return Accessor(getter=attr, setter=attr)


Instruction:
Create a Python 3 function for
serializing the attribute of the input data.

Response:
def serialize(self, value, **kwargs):
        """Serialize the attribute of the input data.

        Gets the attribute value with accessor and converts it using the
        type serialization. Schema will place this serialized value into
        corresponding compartment of the HAL structure with the name of the
        attribute as a key.

        :param value: Value to get the attribute value from.
        :return: Serialized attribute value.
        """
        if types.Type.is_type(self.attr_type):
            try:
                value = self.accessor.get(value, **kwargs)
            except (AttributeError, KeyError):
                if not hasattr(self, "default") and self.required:
                    raise
                value = self.default() if callable(self.default) else self.default

            return self.attr_type.serialize(value, **_get_context(self._attr_type_serialize_argspec, kwargs))

        return self.attr_type


Instruction:
Make a summary of the following Python 3 code
def deserialize(cls, value, output=None, **kwargs):
        errors = []
        result = {}
        for attr in cls.__attrs__.values():
            try:
                result[attr.name] = attr.deserialize(value, **kwargs)
            except NotImplementedError:
                # Links don't support deserialization
                continue
            except ValueError as e:
                errors.append(exceptions.ValidationError(e, attr.name))
            except exceptions.ValidationError as e:
                e.attr = attr.name
                errors.append(e)
            except (KeyError, AttributeError):
                if attr.required:
                    errors.append(exceptions.ValidationError("Missing attribute.", attr.name))

        if errors:
            raise exceptions.ValidationError(errors)

        if output is None:
            return result
        for attr in cls.__attrs__.values():
            if attr.name in result:
                attr.accessor.set(output, result[attr.name])

Response: Deserialize the HAL structure into the output value.


Instruction:
Implement a Python 3 function for
parsing a 9 bytes packet in the Temperature format and returns a dictionary containing the data extracted.

Response:
def parse(self, data):
        """Parse a 9 bytes packet in the Temperature format and return a
        dictionary containing the data extracted. An example of a return value
        would be:

        .. code-block:: python

            {
                'id': "0x2EB2",
                'packet_length': 8,
                'packet_type': 80,
                'packet_type_name': 'Temperature sensors',
                'sequence_number': 0,
                'packet_subtype': 1,
                'packet_subtype_name': "THR128/138, THC138",
                'temperature': 21.3,
                'signal_level': 9,
                'battery_level': 6,
            }

        :param data: bytearray to be parsed
        :type data: bytearray

        :return: Data dictionary containing the parsed values
        :rtype: dict
        """

        self.validate_packet(data)

        id_ = self.dump_hex(data[4:6])
        # channel = data[5] TBC

        temperature = ((data[6] & 0x7f) * 256 + data[7]) / 10
        signbit = data[6] & 0x80
        if signbit != 0:
            temperature = -temperature

        sensor_specific = {
            'id': id_,
            # 'channel': channel, TBC
            'temperature': temperature
        }

        results = self.parse_header_part(data)
        results.update(RfxPacketUtils.parse_signal_and_battery(data[8]))
        results.update(sensor_specific)

        return results


Instruction:
Write a Python 3 script to
parse a comma - separated list of ints.

Response:
def ints(l, ifilter=lambda x: x, idescr=None):
    """ Parses a comma-separated list of ints. """
    if isinstance(l, string_types):
        if l[0] == '[' and l[-1] == ']':
            l = l[1:-1]
        l = list(map(lambda x: x.strip(), l.split(',')))
    try:
        l = list(map(ifilter, list(map(int, l))))
    except:
        raise ValueError("Bad list of {}integers"
                         .format("" if idescr is None else idescr + " "))
    return l


Instruction:
Implement a Python 3 function for
giving a list of IP addresses return a list of all hosts in the network.

Response:
def ip_address_list(ips):
    """ IP address range validation and expansion. """
    # first, try it as a single IP address
    try:
        return ip_address(ips)
    except ValueError:
        pass
    # then, consider it as an ipaddress.IPv[4|6]Network instance and expand it
    return list(ipaddress.ip_network(u(ips)).hosts())


Instruction:
Can you implement a function in Python 3 that
validates and return a valid port number.

Response:
def port_number(port):
    """ Port number validation. """
    try:
        port = int(port)
    except ValueError:
        raise ValueError("Bad port number")
    if not 0 <= port < 2 ** 16:
        raise ValueError("Bad port number")
    return port


Instruction:
Can you generate a brief explanation for the following Python 3 code
def port_number_range(prange):
    # first, try it as a normal port number
    try:
        return port_number(prange)
    except ValueError:
        pass
    # then, consider it as a range with the format "x-y" and expand it
    try:
        bounds = list(map(int, re.match(r'^(\d+)\-(\d+)$', prange).groups()))
        if bounds[0] > bounds[1]:
            raise AttributeError()
    except (AttributeError, TypeError):
        raise ValueError("Bad port number range")
    return list(range(bounds[0], bounds[1] + 1))

Response: Validate and expansion of a port number range.


Instruction:
Can you create a Python 3 function that
extracts and converts the RFX common header part of all valid packets to a plain dictionary.

Response:
def parse_header_part(self, data):
        """Extracts and converts the RFX common header part of all valid
        packets to a plain dictionary. RFX header part is the 4 bytes prior
        the sensor vendor specific data part.

        The RFX common header part contains respectively:
        - packet length
        - packet type
        - packet sub-type
        - sequence number

        :param data: bytearray of received data
        :type data: bytearray

        """
        packet_length = data[0]
        packet_type = data[1]
        packet_subtype = data[2]
        sequence_number = data[3]
        return {
            'packet_length': packet_length,
            'packet_type': packet_type,
            'packet_type_name': self.PACKET_TYPES.get(packet_type),
            'packet_subtype': packet_subtype,
            'packet_subtype_name': self.PACKET_SUBTYPES.get(packet_subtype),
            'sequence_number': sequence_number
        }


Instruction:
Given the following Python 3 function, write the documentation
def load(self, data):
        self.loaded_at = datetime.utcnow()
        self.raw = data
        self.data = self.parse(data)
        return self.data

Response: This method is used to load the raw data and start parsing the data.


Instruction:
Explain what the following Python 3 code does
def validate_packet(self, data):

        # Validate length.
        # The first byte in the packet should be equal to the number of
        # remaining bytes (i.e. length excluding the first byte).
        expected_length = data[0] + 1

        if len(data) != expected_length:
            raise InvalidPacketLength(
                "Expected packet length to be %s bytes but it was %s bytes"
                % (expected_length, len(data))
            )

        # Validate minimal length.
        # The packet contains at least the RFX header:
        #  packet_length (1 byte) + packet_type (1 byte)
        #  + packet_subtype (1 byte) + sequence_number (1 byte)
        if expected_length < 4:
            raise MalformedPacket(
                    "Expected packet length to be larger than 4 bytes but \
                    it was %s bytes"
                    % (len(data))
            )

        # Validate Packet Type.
        # This specifies the family of devices.
        # Check it is one of the supported packet types
        packet_type = data[1]

        if self.PACKET_TYPES and packet_type not in self.PACKET_TYPES:
            types = ",".join("0x{:02x}".format(pt) for pt in self.PACKET_TYPES)
            raise UnknownPacketType(
                "Expected packet type to be one of [%s] but recieved %s"
                % (types, packet_type)
            )

        # Validate Packet Subtype.
        # This specifies the sub-family of devices.
        # Check it is one of the supported packet subtypes for current type
        sub_type = data[2]

        if self.PACKET_SUBTYPES and sub_type not in self.PACKET_SUBTYPES:
            types = \
                ",".join("0x{:02x}".format(pt) for pt in self.PACKET_SUBTYPES)
            raise UnknownPacketSubtype(
                "Expected packet type to be one of [%s] but recieved %s"
                % (types, sub_type))

        return True

Response: Validate a packet against this packet handler and determine if it meets the requirements.


Instruction:
Write a Python 3 function for
enforcing an instance of the schematics class.

Response:
def _enforce_instance(model_or_class):
    """
    It's a common mistake to not initialize a
    schematics class. We should handle that by just
    calling the default constructor.
    """
    if isinstance(model_or_class, type) and issubclass(model_or_class, BaseType):
        return model_or_class()
    return model_or_class


Instruction:
Make a summary of the following Python 3 code
def render(file):
    fp = file.open()
    content = fp.read()
    fp.close()

    notebook = nbformat.reads(content.decode('utf-8'), as_version=4)

    html_exporter = HTMLExporter()
    html_exporter.template_file = 'basic'
    (body, resources) = html_exporter.from_notebook_node(notebook)
    return body, resources

Response: Generate the result HTML.


Instruction:
How would you explain what the following Python 3 function does
def preview(file):
    body, resources = render(file)
    default_ipython_style = resources['inlining']['css'][1]
    return render_template(
        'invenio_previewer/ipynb.html',
        file=file,
        content=body,
        style=default_ipython_style
    )

Response: Render the IPython Notebook.


Instruction:
Can you tell what is the following Python 3 function doing
def transmute_route(app, fn, context=default_context):
    transmute_func = TransmuteFunction(fn)
    routes, handler = create_routes_and_handler(transmute_func, context)
    for r in routes:
        """
        the route being attached is a great place to start building up a
        swagger spec.  the SwaggerSpec object handles creating the
        swagger spec from transmute routes for you.

        almost all web frameworks provide some app-specific context
        that one can add values to. It's recommended to attach
        and retrieve the swagger spec from there.
        """
        if not hasattr(app, SWAGGER_ATTR_NAME):
            setattr(app, SWAGGER_ATTR_NAME, SwaggerSpec())
        swagger_obj = getattr(app, SWAGGER_ATTR_NAME)
        swagger_obj.add_func(transmute_func, context)
        app.route(r, methods=transmute_func.methods)(handler)

Response: This function will handle the transmute route for all the web frameworks. It will create the routes and handler for all the web frameworks.


Instruction:
Here you have a function in Python 3, explain what it does
def create_routes_and_handler(transmute_func, context):
    @wraps(transmute_func.raw_func)
    def handler():
        exc, result = None, None
        try:
            args, kwargs = ParamExtractorFlask().extract_params(
                context, transmute_func, request.content_type
            )
            result = transmute_func(*args, **kwargs)
        except Exception as e:
            exc = e
            """
            attaching the traceack is done for you in Python 3, but
            in Python 2 the __traceback__ must be
            attached to the object manually.
            """
            exc.__traceback__ = sys.exc_info()[2]
        """
        transmute_func.process_result handles converting
        the response from the function into the response body,
        the status code that should be returned, and the
        response content-type.
        """
        response = transmute_func.process_result(
            context, result, exc, request.content_type
        )
        return Response(
            response["body"],
            status=response["code"],
            mimetype=response["content-type"],
            headers=response["headers"]
        )
    return (
        _convert_paths_to_flask(transmute_func.paths),
        handler
    )

Response: Creates a route and handler for the given Transmute function.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _convert_paths_to_flask(transmute_paths):
    paths = []
    for p in transmute_paths:
        paths.append(p.replace("{", "<").replace("}", ">"))
    return paths

Response: converts a list of transmute - core s path syntax into flask s path syntax


Instruction:
How would you implement a function in Python 3 that
adds a swagger page to the app.

Response:
def add_swagger(app, json_route, html_route, **kwargs):
    """
    add a swagger html page, and a swagger.json generated
    from the routes added to the app.
    """
    spec = getattr(app, SWAGGER_ATTR_NAME)
    if spec:
        spec = spec.swagger_definition(**kwargs)
    else:
        spec = {}
    encoded_spec = json.dumps(spec).encode("UTF-8")

    @app.route(json_route)
    def swagger():
        return Response(
            encoded_spec,
            # we allow CORS, so this can be requested at swagger.io
            headers={"Access-Control-Allow-Origin": "*"},
            content_type="application/json",
        )

    # add the statics
    static_root = get_swagger_static_root()
    swagger_body = generate_swagger_html(
        STATIC_PATH, json_route
    ).encode("utf-8")

    @app.route(html_route)
    def swagger_ui():
        return Response(swagger_body, content_type="text/html")

    # the blueprint work is the easiest way to integrate a static
    # directory into flask.
    blueprint = Blueprint('swagger', __name__, static_url_path=STATIC_PATH,
                          static_folder=static_root)
    app.register_blueprint(blueprint)


Instruction:
How would you explain what the following Python 3 function does
def _log_enabled_protocols(self, flags, protocols):

        enabled, disabled = [], []

        for procol, flag in sorted(zip(protocols, flags)):

            if flag == '1':
                enabled.append(procol)
                status = 'Enabled'
            else:
                disabled.append(procol)
                status = 'Disabled'

            message = "{0:21}: {1}".format(procol, status)
            self.log.info(message)

        return enabled, disabled

Response: Log the status of each protocol where 1 is the enabled and 0 is the disabled ones.


Instruction:
Given the following Python 3 function, write the documentation
def parse(self, data):

        self.validate_packet(data)

        packet_length = data[0]
        packet_type = data[1]
        sub_type = data[2]
        sequence_number = data[3]
        command_type = data[4]
        transceiver_type = data[5]
        transceiver_type_text = _MSG1_RECEIVER_TYPE.get(data[5])
        firmware_version = data[6]

        flags = self._int_to_binary_list(data[7])
        flags.extend(self._int_to_binary_list(data[8]))
        flags.extend(self._int_to_binary_list(data[9]))

        enabled, disabled = self._log_enabled_protocols(flags, PROTOCOLS)

        return {
            'packet_length': packet_length,
            'packet_type': packet_type,
            'packet_type_name': self.PACKET_TYPES.get(packet_type),
            'sequence_number': sequence_number,
            'sub_type': sub_type,
            'sub_type_name': self.PACKET_SUBTYPES.get(sub_type),
            'command_type': command_type,
            'transceiver_type': transceiver_type,
            'transceiver_type_text': transceiver_type_text,
            'firmware_version': firmware_version,
            'enabled_protocols': enabled,
            'disabled_protocols': disabled,
        }

Response: Parse a 13 byte packet in the Status format.


Instruction:
Can you tell what is the following Python 3 function doing
def set_time_items(glob):
    a = glob['args']
    l = glob['logger']

    class __TimeManager(object):
        def __init__(self):
            c = a._collisions
            self._stats = getattr(a, c.get("stats") or "stats", False)
            self._timings = getattr(a, c.get("timings") or "timings", False)
            self.enabled = self._stats or self._timings
            self.last = self.start = time.time()
            self.times = []

        def stats(self):
            end = time.time()
            b = ""
            for d, s, e in self.times:
                b += "\n{}\n> {} seconds".format(d, e - s)
            l.time("Total time: {} seconds{}".format(end - self.start, b))

    glob['time_manager'] = manager = __TimeManager()

    def _take_time(start=None, descr=None):
        t = manager.last = time.time()
        if start is not None and descr is not None:
            manager.times.append((descr, float(start), float(t)))
        return t - (start or 0)

    class Timer(object):
        class TimeoutError(Exception):
            pass  # TimeoutError is not handled in Python 2

        def __init__(self, description=None, message=TO_MSG, timeout=None,
                     fail_on_timeout=False):
            self.fail = fail_on_timeout
            self.id = len(manager.times)
            self.descr = "#" + str(self.id) + \
                         (": " + (description or "")).rstrip(": ")
            self.message = message
            self.start = _take_time()
            self.timeout = timeout

        def __enter__(self):
            if manager.enabled:
                if self.timeout is not None:
                    signal.signal(signal.SIGALRM, self._handler)
                    signal.alarm(self.timeout)
                if manager._timings and self.descr:
                    l.time(self.descr)
                return self

        def __exit__(self, exc_type, exc_value, exc_traceback):
            if manager.enabled:
                d = _take_time(self.start, self.descr)
                if manager._timings:
                    l.time("> Time elapsed: {} seconds".format(d))
                if self.timeout is not None:
                    if self.fail and exc_type is Timer.TimeoutError:
                        return True

        def _handler(self, signum, frame):
            raise Timer.TimeoutError(self.message)

    glob['Timer'] = Timer

    def get_time(message=None, start=manager.start):
        if manager._timings:
            l.time("> {}: {} seconds".format(message or "Time elapsed since "
                                          "execution start", _take_time(start)))

    glob['get_time'] = get_time

    def get_time_since_last(message=None):
        get_time(message or "Time elapsed since last measure", manager.last)

    glob['get_time_since_last'] = get_time_since_last

Response: This function prepares the benchmark items for inclusion in main script s global scope dictionary.


Instruction:
Create a Python 3 function for
capturing the variable in the parameters dictionary

Response:
def _capture_variable(iterator, parameters):
    """
    return the replacement string.
    this assumes the preceeding {{ has already been
    popped off.
    """
    key = ""
    next_c = next(iterator)
    while next_c != "}":
        key += next_c
        next_c = next(iterator)
    # remove the final "}"
    next(iterator)
    return parameters[key]


Instruction:
Can you generate a brief explanation for the following Python 3 code
def rgb_distance(rgb1, rgb2):
    '''
    Calculate the distance between two RGB sequences.
    '''
    return sum(map(lambda c: (c[0] - c[1]) ** 2,
        zip(rgb1, rgb2)))

Response: Calculate the distance between two RGB sequences.


Instruction:
How would you explain what the following Python 3 function does
def rgb_reduce(r, g, b, mode=8):
    '''
    Convert an RGB colour to 8 or 16 colour ANSI graphics.
    '''
    colours = ANSI_COLOURS[:mode]
    matches = [(rgb_distance(c, map(int, [r, g, b])), i)
        for i, c in enumerate(colours)]
    matches.sort()
    return sequence('m')(str(30 + matches[0][1]))

Response: Convert an RGB colour to 8 or 16 colour ANSI graphics.


Instruction:
Can you tell what is the following Python 3 function doing
def rgb256(r, g, b):
    '''
    Convert an RGB colour to 256 colour ANSI graphics.
    '''
    grey = False
    poss = True
    step = 2.5

    while poss: # As long as the colour could be grey scale
        if r < step or g < step or b < step:
            grey = r < step and g < step and b < step
            poss = False

        step += 42.5

    if grey:
        colour = 232 + int(float(sum([r, g, b]) / 33.0))
    else:
        colour = sum([16] + [int (6 * float(val) / 256) * mod
            for val, mod in ((r, 36), (g, 6), (b, 1))])

    return sequence('m', fields=3)(38, 5, colour)

Response: Convert an RGB colour to 256 colour ANSI graphics.


Instruction:
Can you generate the documentation for the following Python 3 function
def uri(self):
        return url_for(
            '.{0}_files'.format(self.pid.pid_type),
            pid_value=self.pid.pid_value,
            filename=self.file.key)

Response: Get file download link.


Instruction:
Implement a Python 3 function for
checking if the file has one of the extensions.

Response:
def has_extensions(self, *exts):
        """Check if file has one of the extensions."""
        file_ext = splitext(self.filename)[1]
        file_ext = file_ext.lower()

        for e in exts:
            if file_ext == e:
                return True
        return False


Instruction:
Here you have a function in Python 3, explain what it does
def render(file):
    with file.open() as fp:
        encoding = detect_encoding(fp, default='utf-8')
        file_content = fp.read().decode(encoding)
        json_data = json.loads(file_content, object_pairs_hook=OrderedDict)
        return json.dumps(json_data, indent=4, separators=(',', ': '))

Response: Pretty print the JSON file for rendering.


Instruction:
Here you have a function in Python 3, explain what it does
def validate_json(file):
    max_file_size = current_app.config.get(
        'PREVIEWER_MAX_FILE_SIZE_BYTES', 1 * 1024 * 1024)
    if file.size > max_file_size:
        return False

    with file.open() as fp:
        try:
            json.loads(fp.read().decode('utf-8'))
            return True
        except:
            return False

Response: Validate a JSON file.


Instruction:
Make a summary of the following Python 3 code
def optspace(edm_missing, rank, niter=500, tol=1e-6, print_out=False):
    from .opt_space import opt_space
    N = edm_missing.shape[0]
    X, S, Y, __ = opt_space(edm_missing, r=rank, niter=niter,
                            tol=tol, print_out=print_out)
    edm_complete = X.dot(S.dot(Y.T))
    edm_complete[range(N), range(N)] = 0.0
    return edm_complete

Response: Complete and denoise EDM using OptSpace algorithm.


Instruction:
Write a Python 3 function that can
complete and denoise EDM using rank alternation.

Response:
def rank_alternation(edm_missing, rank, niter=50, print_out=False, edm_true=None):
    """Complete and denoise EDM using rank alternation.

    Iteratively impose rank and strucutre to complete marix entries

    :param edm_missing: EDM with 0 where no measurement was taken.
    :param rank: expected rank of complete EDM.
    :param niter: maximum number of iterations.
    :param edm: if given, the relative EDM error is tracked.

    :return: Completed matrix and array of errors (empty if no true edm is given).
    The matrix is of the correct structure, but might not have the right measured entries.

    """
    from pylocus.basics import low_rank_approximation
    errs = []
    N = edm_missing.shape[0]
    edm_complete = edm_missing.copy()
    edm_complete[edm_complete == 0] = np.mean(edm_complete[edm_complete > 0])
    for i in range(niter):
        # impose matrix rank
        edm_complete = low_rank_approximation(edm_complete, rank)

        # impose known entries
        edm_complete[edm_missing > 0] = edm_missing[edm_missing > 0]

        # impose matrix structure
        edm_complete[range(N), range(N)] = 0.0
        edm_complete[edm_complete < 0] = 0.0
        edm_complete = 0.5 * (edm_complete + edm_complete.T)

        if edm_true is not None:
            err = np.linalg.norm(edm_complete - edm_true)
            errs.append(err)
    return edm_complete, errs


Instruction:
How would you implement a function in Python 3 that
completes and denoise EDM using semidefinite relaxation.

Response:
def semidefinite_relaxation(edm_missing, lamda, W=None, print_out=False, **kwargs):
    """Complete and denoise EDM using semidefinite relaxation.

    Returns solution to the relaxation of the following problem:
        D = argmin || W * (D - edm_missing) ||
            s.t. D is EDM
    where edm_missing is measured matrix, W is a weight matrix, and * is pointwise multiplication.

    Refer to paper "Euclidean Distance Matrices - Essential Theory, Algorithms and Applications",
    Algorithm 5, for details. (https://www.doi.org/%2010.1109/MSP.2015.2398954)

    :param edm_missing: EDM with 0 where no measurement was taken.
    :param lamda: Regularization parameter.
    :param W: Optional mask. If no mask is given, a binary mask is created based on missing elements of edm_missing.
              If mask is given
    :param kwargs: more options passed to the solver. See cvxpy documentation for all options.
    """
    from .algorithms import reconstruct_mds

    def kappa(gram):
        n = len(gram)
        e = np.ones(n)
        return np.outer(np.diag(gram), e) + np.outer(e, np.diag(gram).T) - 2 * gram

    def kappa_cvx(gram, n):
        e = np.ones((n, 1))
        return reshape(diag(gram), (n, 1)) * e.T + e * reshape(diag(gram), (1, n)) - 2 * gram

    method = kwargs.pop('method', 'maximize')
    options = {'solver': 'CVXOPT'}
    options.update(kwargs)

    if W is None:
        W = (edm_missing > 0)
    else:
        W[edm_missing == 0] = 0.0

    n = edm_missing.shape[0]
    V = np.c_[-np.ones((n - 1, 1)) / np.sqrt(n), np.eye(n - 1) -
              np.ones((n - 1, n - 1)) / (n + np.sqrt(n))].T

    H = Variable((n - 1, n - 1), PSD=True)
    G = V * H * V.T  # * is overloaded
    edm_optimize = kappa_cvx(G, n)

    if method == 'maximize':
        obj = Maximize(trace(H) - lamda *
                       norm(multiply(W, (edm_optimize - edm_missing)), p=1))
    # TODO: add a reference to paper where "minimize" is used instead of maximize.
    elif method == 'minimize':
        obj = Minimize(trace(H) + lamda *
                       norm(multiply(W, (edm_optimize - edm_missing)), p=1))

    prob = Problem(obj)

    total = prob.solve(**options)
    if print_out:
        print('total cost:', total)
        print('SDP status:', prob.status)

    if H.value is not None:
        Gbest = V.dot(H.value).dot(V.T)
        if print_out:
            print('eigenvalues:', np.sum(np.linalg.eigvals(Gbest)[2:]))
        edm_complete = kappa(Gbest)
    else:
        edm_complete = edm_missing

    if (print_out):
        if H.value is not None:
            print('trace of H:', np.trace(H.value))
        print('other cost:', lamda *
              norm(multiply(W, (edm_complete - edm_missing)), p=1).value)

    return np.array(edm_complete)


Instruction:
Here you have a function in Python 3, explain what it does
def completion_acd(edm, X0, W=None, tol=1e-6, sweeps=3):
    from .algorithms import reconstruct_acd
    Xhat, costs = reconstruct_acd(edm, X0, W, tol=tol, sweeps=sweeps)
    return get_edm(Xhat)

Response: Complete an arbitrary denoise EDM using an iterative decent algorithm.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def completion_dwmds(edm, X0, W=None, tol=1e-10, sweeps=100):
    from .algorithms import reconstruct_dwmds
    Xhat, costs = reconstruct_dwmds(edm, X0, W, n=1, tol=tol, sweeps=sweeps)
    return get_edm(Xhat)

Response: Complete an EDM using DWMDS.


Instruction:
How would you implement a function in Python 3 that
converts the transmute_path to a tornado compatible regex and return a tornado. web. URLSpec object.

Response:
def url_spec(transmute_path, handler, *args, **kwargs):
    """
    convert the transmute_path
    to a tornado compatible regex,
    and return a tornado url object.
    """
    p = _to_tornado_pattern(transmute_path)
    for m in METHODS:
        method = getattr(handler, m)
        if hasattr(method, "transmute_func"):
            method.transmute_func.paths.add(transmute_path)
    return tornado.web.URLSpec(
        p, handler,
        *args, **kwargs
    )


Instruction:
Create a Python 3 function to
initialize points according to predefined modes.

Response:
def set_points(self, mode='', points=None, range_=RANGE, size=1):
        """ Initialize points according to predefined modes.

        :param range_:[xmin, xmax, ymin, ymax], range of point sets
        """
        if mode == 'last':
            if points is None:
                print('Error: empty last point specification given.')
                return
            tol = 0.1
            [i, j, k] = points
            alpha = 2.0 * np.random.rand(1) + 0.5
            beta = 2.0 * np.random.rand(1) + 0.5
            if i >= 0 and j < 0 and k < 0:
                # at one corner of triangle
                assert i < 3
                other = np.delete(np.arange(3), i)
                u = (self.points[i, :] - self.points[other[0], :]
                     ) / np.linalg.norm(
                         self.points[i, :] - self.points[other[0], :])
                v = (self.points[i, :] - self.points[other[1], :]
                     ) / np.linalg.norm(
                         self.points[i, :] - self.points[other[1], :])
                self.points[-1, :] = self.points[i, :] + alpha * u + beta * v
            elif i >= 0 and j >= 0 and k < 0:
                found = False
                safety_it = 0
                while not found:
                    alpha = np.random.uniform(tol, 1 - tol)
                    beta = 1.0 - alpha
                    gamma = 2 * np.random.rand(1) + tol
                    assert j < 3
                    other = np.delete(np.arange(3), (i, j))
                    u = (
                        self.points[i, :] - self.points[other, :]
                    )  # /np.linalg.norm(self.points[i,:] - self.points[other,:])
                    v = (
                        self.points[j, :] - self.points[other, :]
                    )  # /np.linalg.norm(self.points[j,:] - self.points[other,:])
                    self.points[-1, :] = (1.0 + gamma) * (
                        self.points[other, :] + alpha * u + beta * v)
                    #check if new direction lies between u and v.
                    new_direction = self.points[-1, :] - self.points[other, :]
                    new_direction = new_direction.reshape(
                        (-1, )) / np.linalg.norm(new_direction)
                    u = u.reshape((-1, )) / np.linalg.norm(u)
                    v = v.reshape((-1, )) / np.linalg.norm(v)
                    #print('{} + {} = {}'.format(acos(np.dot(new_direction,u)),acos(np.dot(new_direction,v)),acos(np.dot(u,v))))
                    if abs(
                            acos(np.dot(new_direction, u)) +
                            acos(np.dot(new_direction, v)) -
                            acos(np.dot(u, v))) < 1e-10:
                        found = True
                    safety_it += 1
                    if safety_it > 100:
                        print('Error: nothing found after 100 iterations.')
                        return
            elif i >= 0 and j >= 0 and k >= 0:
                # inside triangle
                assert k < 3
                found = False
                safety_it = 0
                while not found:
                    alpha = np.random.rand(1) + tol
                    beta = np.random.rand(1) + tol
                    other = np.delete(np.arange(3), i)
                    u = self.points[other[0], :] - self.points[i, :]
                    v = self.points[other[1], :] - self.points[i, :]
                    temptative_point = self.points[i, :] + alpha * u + beta * v
                    vjk = self.points[other[1], :] - self.points[other[0], :]
                    njk = [vjk[1], -vjk[0]]
                    if (np.dot(self.points[j, :] - self.points[i, :], njk) >
                            0) != (np.dot(temptative_point - self.points[j, :],
                                          njk) > 0):
                        self.points[-1, :] = temptative_point
                        found = True
                    safety_it += 1
                    if safety_it > 100:
                        print('Error: nothing found after 100 iterations.')
                        return
            elif i < 0 and j < 0 and k < 0:
                x = range_[0] + (
                    range_[1] - range_[0]) * np.random.rand(1)
                y = range_[2] + (
                    range_[1] - range_[0]) * np.random.rand(1)
                self.points[-1, :] = [x, y]
            else:
                print("Error: non-valid arguments.")
        elif mode == 'random':
            """ Create N uniformly distributed points in [0, size] x [0, size]
            """
            self.points = np.random.uniform(0, size, (self.N, self.d))
        elif mode == 'normal':
            self.points = np.random.normal(0, size, (self.N, self.d))
        elif mode == 'circle':
            from math import cos, sin
            x_range = size / 2.0
            y_range = size / 2.0
            c = np.array((x_range, y_range))
            r = 0.9 * min(x_range, y_range)
            theta = 2 * pi / self.N
            for i in range(self.N):
                theta_tot = i * theta
                self.points[i, :] = c + np.array(
                    (r * cos(theta_tot), r * sin(theta_tot)))
        elif mode == 'set':
            """
            Place points according to hard coded rule.
            """
            if self.N == 3:
                x = [-1.0, 1.0, 0.0]
                y = [-1.0, -1.0, 1.0]
            elif self.N == 4:
                x = [-1.0, 1.0, 0.0, 0.0]
                y = [-1.0, -1.0, 1.0, 0.0]
            elif self.N == 5:
                x = [-0.0, 1.5, 1.5, -0.0, -1.0]
                y = [-1.0, -1.0, 1.0, 1.0, 0.0]
            else:
                print("Error: No rule defined for N = ", self.N)
                return
            self.points = np.c_[x, y]
        elif mode == 'geogebra':
            if self.N == 4:
                self.points = np.array(((1.5, 1.8), (7.9, 2.5), (2.3, 5.1),
                                        (3.34, -1.36)))
            elif self.N == 5:
                self.points = np.array(((1.5, 1.8), (7.9, 2.5), (2.3, 5.1),
                                        (3.34, -1.36), (5, 1.4)))
            else:
                print("Error: No rule defined for N = ", self.N)
        elif mode == '':
            if points is None:
                raise NotImplementedError("Need to give either mode or points.")
            else:
                self.points = points
                self.N, self.d = points.shape

        self.init()


Instruction:
Write a Python 3 function for
returning the set of inner angles between 0 and pi and the corners corresponding to each entry of theta.

Response:
def create_theta(self):
        """
        Returns the set of inner angles (between 0 and pi)
        reconstructed from point coordinates.
        Also returns the corners corresponding to each entry of theta.
        """
        import itertools
        from pylocus.basics_angles import from_0_to_pi
        theta = np.empty((self.M, ))
        corners = np.empty((self.M, 3))
        k = 0
        indices = np.arange(self.N)
        for triangle in itertools.combinations(indices, 3):
            for counter, idx in enumerate(triangle):
                corner = idx
                other = np.delete(triangle, counter)
                corners[k, :] = [corner, other[0], other[1]]
                theta[k] = self.get_inner_angle(corner, other)
                theta[k] = from_0_to_pi(theta[k])
                if DEBUG:
                    print(self.abs_angles[corner, other[0]],
                          self.abs_angles[corner, other[1]])
                    print('theta', corners[k, :], theta[k])
                k = k + 1
            inner_angle_sum = theta[k - 1] + theta[k - 2] + theta[k - 3]
            assert abs(inner_angle_sum - pi) < 1e-10, \
                'inner angle sum: {} {} {}'.format(
                    triangle, inner_angle_sum, (theta[k - 1], theta[k - 2], theta[k - 3]))
        self.theta = theta
        self.corners = corners
        return theta, corners


Instruction:
Implement a Python 3 function for
calculating angles theta_ik and theta_jk theta produce point Pk.

Response:
def get_orientation(k, i, j):
        from pylocus.basics_angles import from_0_to_2pi
        """calculate angles theta_ik and theta_jk theta produce point Pk.
        Should give the same as get_absolute_angle! """
        theta_ij = own.abs_angles[i, j]
        theta_ji = own.abs_angles[j, i]

        # complicated
        xi = own.points[i, 0]
        xj = own.points[j, 0]
        yi = own.points[i, 1]
        yj = own.points[j, 1]
        w = np.array([yi - yj, xj - xi])
        test = np.dot(own.points[k, :] - own.points[i, :], w) > 0

        # more elegant
        theta_ik = truth.abs_angles[i, k]
        diff = from_0_to_2pi(theta_ik - theta_ij)
        test2 = (diff > 0 and diff < pi)
        assert (test == test2), "diff: %r, scalar prodcut: %r" % (diff, np.dot(
            own.points[k, :] - own.points[i, :], w))

        thetai_jk = truth.get_theta(i, j, k)
        thetaj_ik = truth.get_theta(j, i, k)
        if test:
            theta_ik = theta_ij + thetai_jk
            theta_jk = theta_ji - thetaj_ik
        else:
            theta_ik = theta_ij - thetai_jk
            theta_jk = theta_ji + thetaj_ik
        theta_ik = from_0_to_2pi(theta_ik)
        theta_jk = from_0_to_2pi(theta_jk)
        return theta_ik, theta_jk


Instruction:
Here you have a function in Python 3, explain what it does
def get_indices(self, k):
        indices_rays = []
        indices_triangles = []
        corners_rays = []
        angles_rays = []
        for t, triangle in enumerate(self.corners):
            if triangle[0] == k:
                indices_rays.append(t)
                corners_rays.append(triangle)
                angles_rays.append(self.theta[t])
            else:
                indices_triangles.append(t)
        np_corners_rays = np.vstack(corners_rays)
        np_angles_rays = np.vstack(angles_rays).reshape((-1, ))
        return indices_rays, indices_triangles, np_corners_rays, np_angles_rays

Response: Get indices of theta vector that have k as first corner.


Instruction:
Make a summary of the following Python 3 code
def get_G(self, k, add_noise=True):
        G = np.ones((self.N - 1, self.N - 1))
        if (add_noise):
            noise = pi * 0.1 * np.random.rand(
                (self.N - 1) * (self.N - 1)).reshape((self.N - 1, self.N - 1))
        other_indices = np.delete(range(self.N), k)
        for idx, i in enumerate(other_indices):
            for jdx, j in enumerate(other_indices):
                if (add_noise and
                        i != j):  # do not add noise on diagonal elements.
                    thetak_ij = self.get_inner_angle(k,
                                                     (i, j)) + noise[idx, jdx]
                else:
                    thetak_ij = self.get_inner_angle(k, (i, j))
                G[idx, jdx] = cos(thetak_ij)
                G[jdx, idx] = cos(thetak_ij)
        return G

Response: get G matrix from angles.


Instruction:
How would you code a function in Python 3 to
get all convex polygones of size m

Response:
def get_convex_polygons(self, m, print_out=False):
        """
        :param m: size of polygones (number of corners)

        :return: (ordered) indices of all convex polygones of size m.
        """
        convex_polygons = []
        for corners in itertools.combinations(np.arange(self.N), m):
            p = np.zeros(m, np.uint)
            p[0] = corners[0]
            left = corners[1:]
            # loop through second corners
            for i, second in enumerate(corners[1:m - 1]):
                p[1] = second
                left = np.delete(corners, (0, i + 1))
                for j, last in enumerate(corners[i + 2:]):
                    left = np.delete(corners, (0, i + 1, j + i + 2))
                    p[-1] = last
                    # loop through all permutations of left corners.
                    for permut in itertools.permutations(left):
                        p[2:-1] = permut
                        sum_theta = 0
                        # sum over all inner angles.
                        for k in range(m):
                            sum_theta += self.get_inner_angle(
                                p[1], (p[0], p[2]))
                            p = np.roll(p, 1)
                        angle = sum_theta
                        sum_angle = (m - 2) * pi
                        if (abs(angle - sum_angle) < 1e-14 or
                                abs(angle) < 1e-14):
                            if (print_out):
                                print("convex polygon found:    ", p)
                            convex_polygons.append(p.copy())
                        #  elif (angle < sum_angle):
                        #  if (print_out): print("non convex polygon found:",p,angle)
                        elif (angle > sum_angle):
                            if (print_out):
                                print("oops")
        return convex_polygons


Instruction:
Write a Python 3 function that can
get the constraints for the polygones

Response:
def get_polygon_constraints(self,
                                range_polygones=range(3, 5),
                                print_out=False):
        """
        :param range_polygones: list of numbers of polygones to test.

        :return A, b: the constraints on the theta-vector of the form A*theta = b
        """
        rows_A = []
        rows_b = []
        for m in range_polygones:
            if (print_out):
                print('checking {}-polygones'.format(m))
            polygons = self.get_convex_polygons(m)
            row_A, row_b = self.get_polygon_constraints_m(polygons, print_out)
            rows_A.append(row_A)
            rows_b.append(row_b)
        self.A = np.vstack(rows_A)
        self.b = np.hstack(rows_b)
        return self.A, self.b


Instruction:
Explain what the following Python 3 code does
def get_polygon_constraints_m(self, polygons_m, print_out=False):
        rows_b = []
        rows_A = []
        m = len(polygons_m[0])
        rows_b.append((m - 2) * pi * np.ones(
            len(polygons_m), ))
        for p in polygons_m:
            row = np.zeros((self.theta.shape[0], ))
            for k in range(m):
                index = get_index(self.corners, p[1], (p[0], p[2]))
                row[index] = 1
                p = np.roll(p, 1)
            assert np.sum(row) == m
            rows_A.append(row)
        A = np.vstack(rows_A)
        b = np.hstack(rows_b)
        num_constraints = A.shape[0]
        A_repeat = np.repeat(A.astype(bool), 3).reshape((1, -1))
        corners = self.corners.reshape((1, -1))
        corners_tiled = np.tile(corners, num_constraints)
        if (print_out):
            print('shape of A {}'.format(A.shape))
        if (print_out):
            print('chosen angles m={}:\n{}'.format(m, (corners_tiled)[A_repeat]
                                                   .reshape((-1, m * 3))))
        if (print_out):
            print('{}-polygones: {}'.format(m, rows_A))
        self.A = A
        self.b = b
        return A, b

Response: returns the constraints A b for each of the given set of polygones.


Instruction:
Can you create a Python 3 function that
gets linear constraints on KE matrix.

Response:
def get_KE_constraints(self):
        """Get linear constraints on KE matrix.
        """
        C2 = np.eye(self.m)
        C2 = C2[:self.m - 2, :]
        to_be_deleted = []
        for idx_vij_1 in range(self.m - 2):
            idx_vij_2 = idx_vij_1 + 1
            C2[idx_vij_1, idx_vij_2] = -1
            i1 = np.where(self.C[idx_vij_1, :] == 1)[0][0]
            i2 = np.where(self.C[idx_vij_2, :] == 1)[0][0]
            j = np.where(self.C[idx_vij_1, :] == -1)[0][0]
            if i1 == i2:
                i = i1
                k = np.where(self.C[idx_vij_2, :] == -1)[0][0]
                i_indices = self.C[:, j] == 1
                j_indices = self.C[:, k] == -1
                idx_vij_3 = np.where(np.bitwise_and(
                    i_indices, j_indices))[0][0]
                #print('v{}{}, v{}{}, v{}{}\n{}    {}    {}'.format(j,i,k,i,k,j,idx_vij_1,idx_vij_2,idx_vij_3))
                C2[idx_vij_1, idx_vij_3] = 1
            else:
                #print('v{}{}, v{}{} not considered.'.format(j,i1,j,i2))
                to_be_deleted.append(idx_vij_1)
        C2 = np.delete(C2, to_be_deleted, axis=0)
        b = np.zeros((C2.shape[0], 1))
        return C2, b


Instruction:
Implement a function in Python 3 to
parse a 12 byte packet in the Lighting2 format and returns a dictionary containing the parsed data.

Response:
def parse(self, data):
        """Parse a 12 bytes packet in the Lighting2 format and return a
        dictionary containing the data extracted. An example of a return value
        would be:

        .. code-block:: python

            {
                'id': "0x111F342",
                'packet_length': 10,
                'packet_type': 17,
                'packet_type_name': 'Humidity sensors',
                'sequence_number': 19,
                'packet_subtype': 0,
                'packet_subtype_name': "AC",
                'unit_code': 10,
                'command': 1,
                'command_text': "Off",
                'level': 7,
                'signal_level': 9,
            }

        :param data: bytearray to be parsed
        :type data: bytearray

        :return: Data dictionary containing the parsed values
        :rtype: dict
        """

        self.validate_packet(data)

        results = self.parse_header_part(data)
        sub_type = results['packet_subtype']

        id_ = self.dump_hex(data[4:8])

        unit_code = data[8]
        command = data[9]
        command_text = SUB_TYPE_COMMANDS.get(sub_type, {}).get(command)
        dim_level = DIM_LEVEL_TO_PERCENT.get(data[10], '--??--')

        sensor_specific = {
            'id': id_,
            'unit_code': unit_code,
            'command': command,
            'command_text': command_text,
            'dim_level': dim_level
        }

        results.update(RfxPacketUtils.parse_signal_upper(data[11]))
        results.update(sensor_specific)

        return results


Instruction:
Can you implement a function in Python 3 that
converts an array of 6 bytes to a 48bit integer.

Response:
def _bytes_to_uint_48(self, bytes_):
        """Converts an array of 6 bytes to a 48bit integer.

        :param data: bytearray to be converted to a 48bit integer
        :type data: bytearray

        :return: the integer
        :rtype: int
        """
        return ((bytes_[0] * pow(2, 40)) + (bytes_[1] * pow(2, 32)) +
                (bytes_[2] * pow(2, 24)) + (bytes_[3] << 16) +
                (bytes_[4] << 8) + bytes_[4])


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

        self.validate_packet(data)

        TOTAL_DIVISOR = 223.666

        id_ = self.dump_hex(data[4:6])
        count = data[6]
        instant = data[7:11]
        total = data[11:16]

        current_watts = self._bytes_to_uint_32(instant)
        total_watts = self._bytes_to_uint_48(total) / TOTAL_DIVISOR

        sensor_specific = {
            'count': count,
            'current_watts': current_watts,
            'id': id_,
            'total_watts': total_watts
        }

        results = self.parse_header_part(data)
        results.update(RfxPacketUtils.parse_signal_and_battery(data[17]))
        results.update(sensor_specific)

        return results

Response: Parses a 18 bytes packet in the Electricity format and returns a dictionary containing the data extracted.


Instruction:
Explain what the following Python 3 code does
def get_key_from_path_and_site(path, site_id):
    '''
    cache key has to be < 250 chars to avoid memcache.Client.MemcachedKeyLengthError
    The best algoritm is SHA-224 whose output (224 chars) respects this limitations
    '''
    key = '{}_{}'.format(path, site_id)
    key = hashlib.sha224(key.encode('utf-8')).hexdigest()
    return key

Response: Get the cache key from a path and site ID.


Instruction:
Explain what the following Python 3 code does
def validate_csv(file):
    try:
        # Detect encoding and dialect
        with file.open() as fp:
            encoding = detect_encoding(fp, default='utf-8')
            sample = fp.read(
                current_app.config.get('PREVIEWER_CSV_VALIDATION_BYTES', 1024))
            delimiter = csv.Sniffer().sniff(sample.decode(encoding)).delimiter
            is_valid = True
    except Exception as e:
        current_app.logger.debug(
            'File {0} is not valid CSV: {1}'.format(file.uri, e))
        encoding = ''
        delimiter = ''
        is_valid = False

    return {
        'delimiter': delimiter,
        'encoding': encoding,
        'is_valid': is_valid
    }

Response: Return dialect information about given csv file.


Instruction:
Can you implement a function in Python 3 that
renders appropiate template with embed flag.

Response:
def preview(file):
    """Render appropiate template with embed flag."""
    file_info = validate_csv(file)
    return render_template(
        'invenio_previewer/csv_bar.html',
        file=file,
        delimiter=file_info['delimiter'],
        encoding=file_info['encoding'],
        js_bundles=current_previewer.js_bundles + ['previewer_csv_js'],
        css_bundles=current_previewer.css_bundles,
    )


Instruction:
How would you explain what the following Python 3 function does
def make_tree(file):
    max_files_count = current_app.config.get('PREVIEWER_ZIP_MAX_FILES', 1000)
    tree = {'type': 'folder', 'id': -1, 'children': {}}

    try:
        with file.open() as fp:
            zf = zipfile.ZipFile(fp)
            # Detect filenames encoding.
            sample = ' '.join(zf.namelist()[:max_files_count])
            if not isinstance(sample, binary_type):
                sample = sample.encode('utf-16be')
            encoding = chardet.detect(sample).get('encoding', 'utf-8')
            for i, info in enumerate(zf.infolist()):
                if i > max_files_count:
                    raise BufferError('Too many files inside the ZIP file.')
                comps = info.filename.split(os.sep)
                node = tree
                for c in comps:
                    if not isinstance(c, text_type):
                        c = c.decode(encoding)
                    if c not in node['children']:
                        if c == '':
                            node['type'] = 'folder'
                            continue
                        node['children'][c] = {
                            'name': c,
                            'type': 'item',
                            'id': 'item{0}'.format(i),
                            'children': {}
                        }
                    node = node['children'][c]
                node['size'] = info.file_size
    except BufferError:
        return tree, True, None
    except (zipfile.LargeZipFile):
        return tree, False, 'Zipfile is too large to be previewed.'
    except Exception as e:
        current_app.logger.warning(str(e), exc_info=True)
        return tree, False, 'Zipfile is not previewable.'

    return tree, False, None

Response: Create tree structure from a ZIP archive.


Instruction:
Write a Python 3 function for
returning appropriate template and pass the file and an embed flag.

Response:
def preview(file):
    """Return appropriate template and pass the file and an embed flag."""
    tree, limit_reached, error = make_tree(file)
    list = children_to_list(tree)['children']
    return render_template(
        "invenio_previewer/zip.html",
        file=file,
        tree=list,
        limit_reached=limit_reached,
        error=error,
        js_bundles=current_previewer.js_bundles + ['previewer_fullscreen_js'],
        css_bundles=current_previewer.css_bundles,
    )


Instruction:
Explain what the following Python 3 code does
def create_noisy_edm(edm, noise, n=None):
    N = edm.shape[0]
    if n is None:
        n = N
    found = False
    max_it = 100
    i = 0
    while not found:
        i += 1
        dm = np.sqrt(edm) + np.random.normal(scale=noise, size=edm.shape)
        dm = np.triu(dm)
        edm_noisy = np.power(dm + dm.T, 2)
        edm_noisy[range(N), range(N)] = 0.0
        edm_noisy[n:, n:] = edm[n:, n:]
        if (edm_noisy >= 0).all():
            found = True
        if i > max_it:
            print('create_noisy_edm: last EDM', edm_noisy)
            raise RuntimeError(
                'Could not generate all positive edm in {} iterations.'.format(max_it))
    return edm_noisy

Response: Create noisy version of the input EDM.


Instruction:
Make a summary of the following Python 3 code
def create_mask(N, method='all', nmissing=0):

    weights = np.ones((N, N))
    weights[range(N), range(N)] = 0

    if method == 'none':
        return weights

    # create indices object to choose from
    elif method == 'all':
        all_indices = np.triu_indices(N, 1)
    elif method == 'first':
        all_indices = [np.zeros(N - 1).astype(np.int),
                       np.arange(1, N).astype(np.int)]
    ntotal = len(all_indices[0])
    # randomly choose from indices and set to 0
    choice = np.random.choice(ntotal, nmissing, replace=False)
    chosen = [all_indices[0][choice], all_indices[1][choice]]
    weights[chosen] = 0
    weights[chosen[1], chosen[0]] = 0
    return weights

Response: Create weight mask according to method.


Instruction:
Make a summary of the following Python 3 code
def describe(**kwargs):
    # if we have a single method, make it a list.
    if isinstance(kwargs.get("paths"), string_type):
        kwargs["paths"] = [kwargs["paths"]]
    if isinstance(kwargs.get("methods"), string_type):
        kwargs["methods"] = [kwargs["methods"]]
    attrs = TransmuteAttributes(**kwargs)

    def decorator(f):
        if hasattr(f, "transmute"):
            f.transmute = f.transmute | attrs
        else:
            f.transmute = attrs
        return f

    return decorator

Response:
This is a decorator that can customize the rest API
    that transmute generates.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def add_swagger(app, json_route, html_route):
    app.router.add_route('GET', json_route, create_swagger_json_handler(app))
    add_swagger_api_route(app, html_route, json_route)

Response: add a swagger. json route and a swagger. html route


Instruction:
Implement a Python 3 function for
adding a route to the swagger api.

Response:
def add_swagger_api_route(app, target_route, swagger_json_route):
    """
    mount a swagger statics page.

    app: the aiohttp app object
    target_route: the path to mount the statics page.
    swagger_json_route: the path where the swagger json definitions is
                        expected to be.
    """
    static_root = get_swagger_static_root()
    swagger_body = generate_swagger_html(
        STATIC_ROOT, swagger_json_route
    ).encode("utf-8")

    async def swagger_ui(request):
        return web.Response(body=swagger_body, content_type="text/html")

    app.router.add_route("GET", target_route, swagger_ui)
    app.router.add_static(STATIC_ROOT, static_root)


Instruction:
Write a Python 3 function that can
create a handler that returns the swagger definition for an application.

Response:
def create_swagger_json_handler(app, **kwargs):
    """
    Create a handler that returns the swagger definition
    for an application.

    This method assumes the application is using the
    TransmuteUrlDispatcher as the router.
    """

    spec = get_swagger_spec(app).swagger_definition(**kwargs)
    encoded_spec = json.dumps(spec).encode("UTF-8")

    async def swagger(request):
        return web.Response(
            # we allow CORS, so this can be requested at swagger.io
            headers={
                "Access-Control-Allow-Origin": "*"
            },
            body=encoded_spec,
            content_type="application/json",
        )

    return swagger


Instruction:
How would you implement a function in Python 3 that
creates detail plots for the single - term experiment.

Response:
def create_multispan_plots(tag_ids):
    """Create detail plots (first row) and total block(second row) of experiments.

    Args:
            tag_ids: list of tag-dictionaries, where the dictionaries must have fields 'name' (used for naming)
                     and 'id' (used for numbering axis_dict)

    Returns:
            Figure element fig, ax_dict containing the first row plots (accessed via id) and ax_total containing the
            second row block.
    """
    import matplotlib.gridspec as gridspec
    fig = plt.figure()
    nrows = 1
    if len(tag_ids) > 1:
        nrows = 2
    fig.set_size_inches(10, 5*nrows)

    gs = gridspec.GridSpec(nrows, len(tag_ids))
    ax_list = [fig.add_subplot(g) for g in gs]
    ax_dict = {}
    for i, tag_dict in enumerate(tag_ids):
        ax_dict[tag_dict['id']] = ax_list[i]
        ax_dict[tag_dict['id']].set_title(
            'System {} (id {})'.format(tag_dict['name'], tag_dict['id']))

    if nrows > 1:
        ax_total = plt.subplot(gs[1, :])

        title = 'Combined {}'.format(tag_ids[0]['name'])
        for i in range(1, len(tag_ids)):
            title = title + ' and {}'.format(tag_ids[i]['name'])
        ax_total.set_title(title)
        gs.tight_layout(fig, rect=[0, 0.03, 1, 0.95])
        return fig, ax_dict, ax_total
    gs.tight_layout(fig, rect=[0, 0.03, 1, 0.95])
    return fig, ax_dict, None


Instruction:
Make a summary of the following Python 3 code
def change_angles(method, theta, tol=1e-10):
    try:
        theta_new = np.zeros(theta.shape)
        for i, thet in enumerate(theta):
            try:
                # theta is vector
                theta_new[i] = method(thet, tol)
            except:
                # theta is matrix
                for j, th in enumerate(thet):
                    try:
                        theta_new[i, j] = method(th, tol)
                    except:
                        # theta is tensor
                        for k, t in enumerate(th):
                            theta_new[i, j, k] = method(t, tol)
        return theta_new
    except:
        return method(theta, tol)

Response: Function used by all angle conversion functions


Instruction:
Can you implement a function in Python 3 that
calculates coordinates of point Pk given two points Pi Pj and inner angles.

Response:
def get_point(theta_ik, theta_jk, Pi, Pj):
    """ Calculate coordinates of point Pk given two points Pi, Pj and inner angles.  :param theta_ik: Inner angle at Pi to Pk.
    :param theta_jk: Inner angle at Pj to Pk.
    :param Pi: Coordinates of point Pi.
    :param Pj: Coordinates of point Pj.

    :return: Coordinate of point Pk.
    """
    A = np.array([[sin(theta_ik), -cos(theta_ik)],
                  [sin(theta_jk), -cos(theta_jk)]])
    B = np.array([[sin(theta_ik), -cos(theta_ik), 0, 0],
                  [0, 0, sin(theta_jk), -cos(theta_jk)]])
    p = np.r_[Pi, Pj]
    Pk = np.linalg.solve(A, np.dot(B, p))
    return Pk


Instruction:
Can you generate a brief explanation for the following Python 3 code
def keys(self):
        return_value = []
        for s in self.serializers:
            return_value += s.content_type
        return return_value

Response: return a list of the content types this set supports


Instruction:
Create a Python 3 function to
give a function and a TransmuteAttributes object and a set of parameters that can be used to generate the transmute parameters for the current language.

Response:
def get_parameters(signature, transmute_attrs, arguments_to_ignore=None):
    """given a function, categorize which arguments should be passed by
    what types of parameters. The choices are:

    * query parameters: passed in as query parameters in the url
    * body parameters: retrieved from the request body (includes forms)
    * header parameters: retrieved from the request header
    * path parameters: retrieved from the uri path

    The categorization is performed for an argument "arg" by:

    1. examining the transmute parameters attached to the function (e.g.
    func.transmute_query_parameters), and checking if "arg" is mentioned. If so,
    it is added to the category.

    2. If the argument is available in the path, it will be added
    as a path parameter.

    3. If the method of the function is GET and only GET, then "arg" will be
    be added to the expected query parameters. Otherwise, "arg" will be added as
    a body parameter.
    """
    params = Parameters()
    used_keys = set(arguments_to_ignore or [])
    # examine what variables are categorized first.
    for key in ["query", "header", "path"]:
        param_set = getattr(params, key)
        explicit_parameters = getattr(transmute_attrs, key + "_parameters")
        used_keys |= load_parameters(
            param_set, explicit_parameters, signature, transmute_attrs
        )

    body_parameters = transmute_attrs.body_parameters
    if isinstance(body_parameters, str):
        name = body_parameters
        params.body = Param(
            argument_name=name,
            description=transmute_attrs.parameter_descriptions.get(name),
            arginfo=signature.get_argument(name),
        )
        used_keys.add(name)
    else:
        used_keys |= load_parameters(
            params.body, transmute_attrs.body_parameters, signature, transmute_attrs
        )

    # extract the parameters from the paths
    for name in _extract_path_parameters_from_paths(transmute_attrs.paths):
        params.path[name] = Param(
            argument_name=name,
            description=transmute_attrs.parameter_descriptions.get(name),
            arginfo=signature.get_argument(name),
        )
        used_keys.add(name)

    # check the method type, and decide if the parameters should be extracted
    # from query parameters or the body
    default_param_key = "query" if transmute_attrs.methods == set(["GET"]) else "body"
    default_params = getattr(params, default_param_key)

    # parse all positional params
    for arginfo in signature:
        if arginfo.name in used_keys:
            continue
        used_keys.add(arginfo.name)
        default_params[arginfo.name] = Param(
            arginfo.name,
            description=transmute_attrs.parameter_descriptions.get(arginfo.name),
            arginfo=arginfo,
        )

    return params


Instruction:
How would you implement a function in Python 3 that
extracts the parameters present in a list of paths.

Response:
def _extract_path_parameters_from_paths(paths):
    """
    from a list of paths, return back a list of the
    arguments present in those paths.

    the arguments available in all of the paths must match: if not,
    an exception will be raised.
    """
    params = set()
    for path in paths:
        parts = PART_REGEX.split(path)
        for p in parts:
            match = PARAM_REGEX.match(p)
            if match:
                params.add(match.group("name"))
    return params


Instruction:
Implement a Python 3 function for
building the schema for the body parameter.

Response:
def _build_body_schema(serializer, body_parameters):
    """ body is built differently, since it's a single argument no matter what. """
    description = ""
    if isinstance(body_parameters, Param):
        schema = serializer.to_json_schema(body_parameters.arginfo.type)
        description = body_parameters.description
        required = True
    else:
        if len(body_parameters) == 0:
            return None
        required = set()
        body_properties = {}
        for name, param in body_parameters.items():
            arginfo = param.arginfo
            body_properties[name] = serializer.to_json_schema(arginfo.type)
            body_properties[name]["description"] = param.description
            if arginfo.default is NoDefault:
                required.add(name)
        schema = {
            "type": "object",
            "required": list(required),
            "properties": body_properties,
        }
        required = len(required) > 0
    return BodyParameter(
        {
            "name": "body",
            "description": description,
            "required": required,
            "schema": schema,
        }
    )


Instruction:
Can you generate the documentation for the following Python 3 function
def parse(self, data):

        self.validate_packet(data)

        results = self.parse_header_part(data)
        sub_type = results['packet_subtype']

        id_ = self.dump_hex(data[4:6])

        direction = data[6] * 256 + data[7]
        if sub_type != 0x05:
            av_speed = (data[8] * 256 + data[9]) * 0.1
        else:
            av_speed = '--??--'
        gust = (data[10] * 256 + data[11]) * 0.1
        if sub_type == 0x04:
            temperature = ((data[12] & 0x7f) * 256 + data[13]) / 10
            signbit = data[12] & 0x80
            if signbit != 0:
                temperature = -temperature
        else:
            temperature = '--??--'
        if sub_type == 0x04:
            wind_chill = ((data[14] & 0x7f) * 256 + data[15]) / 10
            signbit = data[14] & 0x80
            if signbit != 0:
                wind_chill = -wind_chill
        else:
            wind_chill = '--??--'

        sensor_specific = {
            'id': id_,
            'direction': direction,
            'wind_gust': gust
        }
        if av_speed != '--??--':
            sensor_specific['av_speed'] = av_speed
        if temperature != '--??--':
            sensor_specific['temperature'] = temperature
        if wind_chill != '--??--':
            sensor_specific['wind_chill'] = wind_chill

        results.update(RfxPacketUtils.parse_signal_and_battery(data[16]))
        results.update(sensor_specific)

        return results

Response: Parses a 17 bytes packet in the Wind format and returns a dictionary containing the parsed values.


Instruction:
Make a summary of the following Python 3 code
def procrustes(anchors, X, scale=True, print_out=False):
    def centralize(X):
        n = X.shape[0]
        ones = np.ones((n, 1))
        return X - np.multiply(1 / n * np.dot(ones.T, X), ones)
    m = anchors.shape[0]
    N, d = X.shape
    assert m >= d, 'Have to give at least d anchor nodes.'
    X_m = X[N - m:, :]
    ones = np.ones((m, 1))

    mux = 1 / m * np.dot(ones.T, X_m)
    muy = 1 / m * np.dot(ones.T, anchors)
    sigmax = 1 / m * np.linalg.norm(X_m - mux)**2
    sigmaxy = 1 / m * np.dot((anchors - muy).T, X_m - mux)
    try:
        U, D, VT = np.linalg.svd(sigmaxy)
    except np.LinAlgError:
        print('strange things are happening...')
        print(sigmaxy)
        print(np.linalg.matrix_rank(sigmaxy))
    #this doesn't work and doesn't seem to be necessary! (why?)
    #  S = np.eye(D.shape[0])
    #  if (np.linalg.det(U)*np.linalg.det(VT.T) < 0):
    #  print('switching')
    #  S[-1,-1] = -1.0
    #  else:
    #  print('not switching')
    #  c = np.trace(np.dot(np.diag(D),S))/sigmax
    #  R = np.dot(U, np.dot(S,VT))
    if (scale):
        c = np.trace(np.diag(D)) / sigmax
    else:
        c = np.trace(np.diag(D)) / sigmax
        if (print_out):
            print('Optimal scale would be: {}. Setting it to 1 now.'.format(c))
        c = 1.0
    R = np.dot(U, VT)
    t = muy.T - c * np.dot(R, mux.T)
    X_transformed = (c * np.dot(R, (X - mux).T) + muy.T).T
    return X_transformed, R, t, c

Response: Fit X to an anchor set by applying optimal translation rotation and reflection.


Instruction:
Can you create a Python 3 function that
reconstructs point set using E ( dge ) - MDS.

Response:
def reconstruct_emds(edm, Om, all_points, method=None, **kwargs):
    """ Reconstruct point set using E(dge)-MDS.
    """
    from .point_set import dm_from_edm
    N = all_points.shape[0]
    d = all_points.shape[1]
    dm = dm_from_edm(edm)
    if method is None:
        from .mds import superMDS
        Xhat, __ = superMDS(all_points[0, :], N, d, Om=Om, dm=dm)
    else:
        C = kwargs.get('C', None)
        b = kwargs.get('b', None)
        if C is None or b is None:
            raise NameError(
                'Need constraints C and b for reconstruct_emds in iterative mode.')
        KE_noisy = np.multiply(np.outer(dm, dm), Om)
        if method == 'iterative':
            from .mds import iterativeEMDS
            Xhat, __ = iterativeEMDS(
                all_points[0, :], N, d, KE=KE_noisy, C=C, b=b)
        elif method == 'relaxed':
            from .mds import relaxedEMDS
            Xhat, __ = relaxedEMDS(
                all_points[0, :], N, d, KE=KE_noisy, C=C, b=b)
        else:
            raise NameError('Undefined method', method)
    Y, R, t, c = procrustes(all_points, Xhat, scale=False)
    return Y


Instruction:
Given the following Python 3 function, write the documentation
def reconstruct_cdm(dm, absolute_angles, all_points, W=None):
    from pylocus.point_set import dmi_from_V, sdm_from_dmi, get_V
    from pylocus.mds import signedMDS

    N = all_points.shape[0]

    V = get_V(absolute_angles, dm)

    dmx = dmi_from_V(V, 0)
    dmy = dmi_from_V(V, 1)

    sdmx = sdm_from_dmi(dmx, N)
    sdmy = sdm_from_dmi(dmy, N)

    points_x = signedMDS(sdmx, W)
    points_y = signedMDS(sdmy, W)

    Xhat = np.c_[points_x, points_y]
    Y, R, t, c = procrustes(all_points, Xhat, scale=False)
    return Y

Response: Reconstruct point set from angle and distance measurements using coordinate difference matrices.


Instruction:
How would you implement a function in Python 3 that
reconstructs point set using MDS and matrix completion algorithms.

Response:
def reconstruct_mds(edm, all_points, completion='optspace', mask=None, method='geometric', print_out=False, n=1):
    """ Reconstruct point set using MDS and matrix completion algorithms.
    """
    from .point_set import dm_from_edm
    from .mds import MDS
    N = all_points.shape[0]
    d = all_points.shape[1]
    if mask is not None:
        edm_missing = np.multiply(edm, mask)
        if completion == 'optspace':
            from .edm_completion import optspace
            edm_complete = optspace(edm_missing, d + 2)
        elif completion == 'alternate':
            from .edm_completion import rank_alternation
            edm_complete, errs = rank_alternation(
                edm_missing, d + 2, print_out=False, edm_true=edm)
        else:
            raise NameError('Unknown completion method {}'.format(completion))
        if (print_out):
            err = np.linalg.norm(edm_complete - edm)**2 / \
                np.linalg.norm(edm)**2
            print('{}: relative error:{}'.format(completion, err))
        edm = edm_complete
    Xhat = MDS(edm, d, method, False).T
    Y, R, t, c = procrustes(all_points[n:], Xhat, True)
    #Y, R, t, c = procrustes(all_points, Xhat, True)
    return Y


Instruction:
Can you tell what is the following Python 3 function doing
def reconstruct_sdp(edm, all_points, W=None, print_out=False, lamda=1000, **kwargs):
    from .edm_completion import semidefinite_relaxation
    edm_complete = semidefinite_relaxation(
        edm, lamda=lamda, W=W, print_out=print_out, **kwargs)
    Xhat = reconstruct_mds(edm_complete, all_points, method='geometric')
    return Xhat, edm_complete

Response: Reconstruct point set using semi - definite rank relaxation.


Instruction:
How would you code a function in Python 3 to
reconstruct the point set using S ( squared R ) L ( squares ) method.

Response:
def reconstruct_srls(edm, all_points, W=None, print_out=False, n=1, rescale=False,
                     z=None):
    """ Reconstruct point set using S(quared)R(ange)L(east)S(quares) method.
    """
    from .lateration import SRLS, get_lateration_parameters
    Y = all_points.copy()
    indices = range(n)
    for index in indices:
        anchors, w, r2 = get_lateration_parameters(all_points, indices, index,
                                                   edm, W)
        if print_out:
            print('SRLS parameters:')
            print('anchors', anchors)
            print('w', w)
            print('r2', r2)

        srls = SRLS(anchors, w, r2, rescale, z, print_out)
        if rescale:
            srls = srls[0]  # second element of output is the scale
        Y[index, :] = srls
    return Y


Instruction:
Can you create a Python 3 function that
reconstructs the acd from the given ELM matrix X0 using alternating coordinate descent.

Response:
def reconstruct_acd(edm, X0, W=None, print_out=False, tol=1e-10, sweeps=10):
    """ Reconstruct point set using alternating coordinate descent.

    :param X0: Nxd matrix of starting points.
    :param tol: Stopping criterion: if the stepsize in all coordinate directions
                is less than tol for 2 consecutive sweeps, we stop.
    :param sweep: Maximum number of sweeps. One sweep goes through all coordintaes and points once.
    """

    def get_unique_delta(delta, i, coord, print_out=False):
        delta_unique = delta.copy()
        ftest = []
        for de in delta_unique:
            X_ktest = X_k.copy()
            X_ktest[i, coord] += de
            ftest.append(f(X_ktest, edm, W))
        # choose delta_unique with lowest cost.
        if print_out:
            print(ftest)
            print(delta_unique)
        delta_unique = delta_unique[ftest == min(ftest)]
        if print_out:
            print(delta_unique)
        if len(delta_unique) > 1:
            # if multiple delta_uniques give the same cost, choose the biggest step.
            delta_unique = max(delta_unique)
        return delta_unique

    def sweep():
        for i in range(N):
            loop_point(i)

    def loop_coordinate(coord, i):
        delta = get_step_size(i, coord, X_k, edm, W)
        if len(delta) > 1:
            delta_unique = get_unique_delta(delta, i, coord)
        elif len(delta) == 1:
            delta_unique = delta[0]
        else:
            print('Warning: did not find delta!', delta)
            delta_unique = 0.0
        try:
            X_k[i, coord] += delta_unique
        except:
            get_unique_delta(delta, i, coord, print_out=True)
            raise

        cost_this = f(X_k, edm, W)
        costs.append(cost_this)

        if delta_unique <= tol:
            coordinates_converged[i, coord] += 1
        else:
            if print_out:
                print('======= coordinate {} of {} not yet converged'.format(coord, i))
            coordinates_converged[i, coord] = 0

    def loop_point(i):
        coord_counter = 0
        while not (coordinates_converged[i] >= 2).all():
            if print_out:
                print('==== point {}'.format(i))
            coord_counter += 1
            for coord in range(d):
                if print_out:
                    print('======= coord {}'.format(coord))
                loop_coordinate(coord, i)
            if coord_counter > coord_n_it:
                break
    from .distributed_mds import get_step_size, f

    N, d = X0.shape
    if W is None:
        W = np.ones((N, N)) - np.eye(N)

    X_k = X0.copy()

    costs = []
    coordinates_converged = np.zeros(X_k.shape)
    coord_n_it = 3
    for sweep_counter in range(sweeps):
        if print_out:
            print('= sweep', sweep_counter)
        sweep()
        if (coordinates_converged >= 2).all():
            if (print_out):
                print('acd: all coordinates converged after {} sweeps.'.format(
                    sweep_counter))
            return X_k, costs
    if (print_out):
        print('acd: did not converge after {} sweeps'.format(sweep_counter + 1))
    return X_k, costs


Instruction:
Create a Python 3 function for
reconstructing point set using d ( istributed ) w (eighted ) MDS.

Response:
def reconstruct_dwmds(edm, X0, W=None, n=None, r=None, X_bar=None, print_out=False, tol=1e-10, sweeps=100):
    """ Reconstruct point set using d(istributed)w(eighted) MDS.

    Refer to paper "Distributed Weighted-Multidimensional Scaling for Node Localization in Sensor Networks" for
    implementation details (doi.org/10.1145/1138127.1138129)

    :param X0: Nxd matrix of starting points.
    :param n: Number of points of unknown position. The first n points in X0 and edm are considered unknown.
    :param tol: Stopping criterion: when the cost is below this level, we stop.
    :param sweeps: Maximum number of sweeps.
    """
    from .basics import get_edm
    from .distributed_mds import get_b, get_Si

    N, d = X0.shape
    if r is None and n is None:
        raise ValueError('either r or n have to be given.')
    elif n is None:
        n = r.shape[0]

    if W is None:
        W = np.ones((N, N)) - np.eye(N)

    X_k = X0.copy()

    costs = []
    # don't have to ignore i=j, because W[i,i] is zero.
    a = np.sum(W[:n, :n], axis=1).flatten() + 2 * \
        np.sum(W[:n, n:], axis=1).flatten()
    if r is not None:
        a += r.flatten()
    for k in range(sweeps):
        S = 0
        for i in range(n):
            edm_estimated = get_edm(X_k)
            bi = get_b(i, edm_estimated, W, edm, n)
            if r is not None and X_bar is not None:
                X_k[i] = 1 / a[i] * (r[i] * X_bar[i, :] +
                                     X_k.T.dot(bi).flatten())
                Si = get_Si(i, edm_estimated, edm, W, n, r, X_bar[i], X_k[i])
            else:
                X_k[i] = 1 / a[i] * X_k.T.dot(bi).flatten()
                Si = get_Si(i, edm_estimated, edm, W, n)
            S += Si
        costs.append(S)
        if k > 1 and abs(costs[-1] - costs[-2]) < tol:
            if (print_out):
                print('dwMDS: converged after', k)
            break
    return X_k, costs


Instruction:
Create a Python 3 function for
parsing a 12 byte packet in the Rain format and returns a dictionary containing the parsed data.

Response:
def parse(self, data):
        """Parse a 12 bytes packet in the Rain format and return a
        dictionary containing the data extracted. An example of a return value
        would be:

        .. code-block:: python

            {
                'id': "0x2EB2",
                'packet_length': 11,
                'packet_type': 85,
                'packet_type_name': 'Rain sensors',
                'sequence_number': 0,
                'packet_subtype': 1,
                'packet_subtype_name': "RGR126/682/918",
                'rain_rate': 5,
                'rain_total': 130.0,
                'signal_level': 9,
                'battery_level': 6,
            }

        :param data: bytearray to be parsed
        :type data: bytearray

        :return: Data dictionary containing the parsed values
        :rtype: dict
        """

        self.validate_packet(data)

        results = self.parse_header_part(data)
        sub_type = results['packet_subtype']

        id_ = self.dump_hex(data[4:6])

        rain_rate_high = data[6]
        rain_rate_low = data[7]
        if sub_type == 0x01:
            rain_rate = rain_rate_high * 0x100 + rain_rate_low
        elif sub_type == 0x02:
            rain_rate = float(rain_rate_high * 0x100 + rain_rate_low) / 100
        else:
            rain_rate = '--??--'
        rain_total1 = data[8]
        rain_total2 = data[9]
        rain_total3 = data[10]
        if sub_type != 0x06:
            rain_total = float(
                rain_total1 * 0x1000 + rain_total2 * 0x100 + rain_total3) / 10
        else:
            rain_total = '--??--'

        sensor_specific = {
            'id': id_
        }
        if rain_rate != '--??--':
            sensor_specific['rain_rate'] = rain_rate
        if rain_total != '--??--':
            sensor_specific['rain_total'] = rain_total

        results.update(RfxPacketUtils.parse_signal_and_battery(data[11]))
        results.update(sensor_specific)

        return results


Instruction:
Given the following Python 3 function, write the documentation
def get_lateration_parameters(all_points, indices, index, edm, W=None):
    if W is None:
        W = np.ones(edm.shape)

    # delete points that are not considered anchors
    anchors = np.delete(all_points, indices, axis=0)
    r2 = np.delete(edm[index, :], indices)
    w = np.delete(W[index, :], indices)

    # set w to zero where measurements are invalid
    if np.isnan(r2).any():
        nan_measurements = np.where(np.isnan(r2))[0]
        r2[nan_measurements] = 0.0
        w[nan_measurements] = 0.0
    if np.isnan(w).any():
        nan_measurements = np.where(np.isnan(w))[0]
        r2[nan_measurements] = 0.0
        w[nan_measurements] = 0.0

    # delete anchors where weight is zero to avoid ill-conditioning
    missing_anchors = np.where(w == 0.0)[0]
    w = np.asarray(np.delete(w, missing_anchors))
    r2 = np.asarray(np.delete(r2, missing_anchors))
    w.resize(edm.shape[0] - len(indices) - len(missing_anchors), 1)
    r2.resize(edm.shape[0] - len(indices) - len(missing_anchors), 1)
    anchors = np.delete(anchors, missing_anchors, axis=0)
    assert w.shape[0] == anchors.shape[0]
    assert np.isnan(w).any() == False
    assert np.isnan(r2).any() == False
    return anchors, w, r2

Response: Get parameters relevant for lateration from full all_points edm and W.


Instruction:
Write a Python 3 script for
squaring range least squares algorithm written by A. Beck P. Stoica in Approximate and Exact solutions of Source Localization Problems.

Response:
def SRLS(anchors, w, r2, rescale=False, z=None, print_out=False):
    '''Squared range least squares (SRLS)

    Algorithm written by A.Beck, P.Stoica in "Approximate and Exact solutions of Source Localization Problems".

    :param anchors: anchor points (Nxd)
    :param w: weights for the measurements (Nx1)
    :param r2: squared distances from anchors to point x. (Nx1)
    :param rescale: Optional parameter. When set to True, the algorithm will
        also identify if there is a global scaling of the measurements.  Such a
        situation arise for example when the measurement units of the distance is
        unknown and different from that of the anchors locations (e.g. anchors are
        in meters, distance in centimeters).
    :param z: Optional parameter. Use to fix the z-coordinate of localized point.
    :param print_out: Optional parameter, prints extra information.

    :return: estimated position of point x.
    '''
    def y_hat(_lambda):
        lhs = ATA + _lambda * D
        assert A.shape[0] == b.shape[0]
        assert A.shape[1] == f.shape[0], 'A {}, f {}'.format(A.shape, f.shape)
        rhs = (np.dot(A.T, b) - _lambda * f).reshape((-1,))
        assert lhs.shape[0] == rhs.shape[0], 'lhs {}, rhs {}'.format(
            lhs.shape, rhs.shape)
        try:
            return np.linalg.solve(lhs, rhs)
        except:
            return np.zeros((lhs.shape[1],))

    def phi(_lambda):
        yhat = y_hat(_lambda).reshape((-1, 1))
        sol = np.dot(yhat.T, np.dot(D, yhat)) + 2 * np.dot(f.T, yhat)
        return sol.flatten()

    def phi_prime(_lambda):
        # TODO: test this.
        B = np.linalg.inv(ATA + _lambda * D)
        C = A.T.dot(b) - _lambda*f
        y_prime = -B.dot(D.dot(B.dot(C)) - f)
        y = y_hat(_lambda)
        return 2*y.T.dot(D).dot(y_prime) + 2*f.T.dot(y_prime)

    from scipy import optimize
    from scipy.linalg import sqrtm

    n, d = anchors.shape
    assert r2.shape[1] == 1 and r2.shape[0] == n, 'r2 has to be of shape Nx1'
    assert w.shape[1] == 1 and w.shape[0] == n, 'w has to be of shape Nx1'
    if z is not None:
        assert d == 3, 'Dimension of problem has to be 3 for fixing z.'

    if rescale and z is not None:
        raise NotImplementedError('Cannot run rescale for fixed z.')
    if rescale and n < d + 2:
        raise ValueError(
            'A minimum of d + 2 ranges are necessary for rescaled ranging.')
    elif n < d + 1 and z is None:
        raise ValueError(
            'A minimum of d + 1 ranges are necessary for ranging.')
    elif n < d:
        raise ValueError(
            'A minimum of d ranges are necessary for ranging.')

    Sigma = np.diagflat(np.power(w, 0.5))

    if rescale:
        A = np.c_[-2 * anchors, np.ones((n, 1)), -r2]
    else:
        if z is None:
            A = np.c_[-2 * anchors, np.ones((n, 1))]
        else:
            A = np.c_[-2 * anchors[:, :2], np.ones((n, 1))]

        A = Sigma.dot(A)

    if rescale:
        b = - np.power(np.linalg.norm(anchors, axis=1), 2).reshape(r2.shape)
    else:
        b = r2 - np.power(np.linalg.norm(anchors, axis=1), 2).reshape(r2.shape)
        if z is not None:
            b = b + 2 * anchors[:, 2].reshape((-1, 1)) * z - z**2
        b = Sigma.dot(b)

    ATA = np.dot(A.T, A)

    if rescale:
        D = np.zeros((d + 2, d + 2))
        D[:d, :d] = np.eye(d)
    else:
        if z is None:
            D = np.zeros((d + 1, d + 1))
        else:
            D = np.zeros((d, d))
        D[:-1, :-1] = np.eye(D.shape[0]-1)

    if rescale:
        f = np.c_[np.zeros((1, d)), -0.5, 0.].T
    elif z is None:
        f = np.c_[np.zeros((1, d)), -0.5].T
    else:
        f = np.c_[np.zeros((1, 2)), -0.5].T

    eig = np.sort(np.real(eigvalsh(a=D, b=ATA)))
    if (print_out):
        print('ATA:', ATA)
        print('rank:', np.linalg.matrix_rank(A))
        print('eigvals:', eigvals(ATA))
        print('condition number:', np.linalg.cond(ATA))
        print('generalized eigenvalues:', eig)

    #eps = 0.01
    if eig[-1] > 1e-10:
        lower_bound = - 1.0 / eig[-1]
    else:
        print('Warning: biggest eigenvalue is zero!')
        lower_bound = -1e-5

    inf = 1e5
    xtol = 1e-12

    lambda_opt = 0
    # We will look for the zero of phi between lower_bound and inf.
    # Therefore, the two have to be of different signs.
    if (phi(lower_bound) > 0) and (phi(inf) < 0):
        # brentq is considered the best rootfinding routine.
        try:
            lambda_opt = optimize.brentq(phi, lower_bound, inf, xtol=xtol)
        except:
            print('SRLS error: brentq did not converge even though we found an estimate for lower and upper bonud. Setting lambda to 0.')
    else:
        try:
            lambda_opt = optimize.newton(phi, lower_bound, fprime=phi_prime, maxiter=1000, tol=xtol, verbose=True)
            assert phi(lambda_opt) < xtol, 'did not find solution of phi(lambda)=0:={}'.format(phi(lambda_opt))
        except:
            print('SRLS error: newton did not converge. Setting lambda to 0.')

    if (print_out):
        print('phi(lower_bound)', phi(lower_bound))
        print('phi(inf)', phi(inf))
        print('phi(lambda_opt)', phi(lambda_opt))
        pos_definite = ATA + lambda_opt*D
        eig = np.sort(np.real(eigvals(pos_definite)))
        print('should be strictly bigger than 0:', eig)

    # Compute best estimate
    yhat = y_hat(lambda_opt)

    if print_out and rescale:
        print('Scaling factor :', yhat[-1])

    if rescale:
        return yhat[:d], yhat[-1]
    elif z is None:
        return yhat[:d]
    else:
        return np.r_[yhat[0], yhat[1], z]


Instruction:
Can you generate the documentation for the following Python 3 function
def PozyxLS(anchors, W, r2, print_out=False):
    ''' Algorithm used by pozyx (https://www.pozyx.io/Documentation/how_does_positioning_work)

    :param anchors: anchor points
    :param r2: squared distances from anchors to point x.

    :returns: estimated position of point x.
    '''
    N = anchors.shape[0]
    anchors_term = np.sum(np.power(anchors[:-1], 2), axis=1)
    last_term = np.sum(np.power(anchors[-1], 2))
    b = r2[:-1] - anchors_term + last_term - r2[-1]
    A = -2 * (anchors[:-1] - anchors[-1])
    x, res, rank, s = np.linalg.lstsq(A, b)
    return x

Response: Algorithm used by pozyx. io


Instruction:
How would you code a function in Python 3 to
range least squares algorithm.

Response:
def RLS(anchors, W, r, print_out=False, grid=None, num_points=10):
    """ Range least squares (RLS) using grid search.

    Algorithm written by A.Beck, P.Stoica in "Approximate and Exact solutions of Source Localization Problems".

    :param anchors: anchor points
    :param r2: squared distances from anchors to point x.
    :param grid: where to search for solution.  (min, max) where min and max  are
    lists of d elements, d being the dimension of the setup. If not given, the
    search is conducted within the space covered by the anchors.
    :param num_points: number of grid points per direction.

    :return: estimated position of point x.
    """
    def cost_function(arr):
        X = np.c_[arr]
        r_measured = np.linalg.norm(anchors - X)
        mse = np.linalg.norm(r_measured - r)**2
        return mse

    if grid is None:
        grid = [np.min(anchors, axis=0), np.max(anchors, axis=0)]

    d = anchors.shape[1]
    x = np.linspace(grid[0][0], grid[1][0], num_points)
    y = np.linspace(grid[0][1], grid[1][1], num_points)
    if d == 2:
        errors_test = np.zeros((num_points, num_points))
        for i, xs in enumerate(x):
            for j, ys in enumerate(y):
                errors_test[i, j] = cost_function((xs, ys))
        min_idx = errors_test.argmin()
        min_idx_multi = np.unravel_index(min_idx, errors_test.shape)
        xhat = np.c_[x[min_idx_multi[0]], y[min_idx_multi[1]]]
    elif d == 3:
        z = np.linspace(grid[0][2], grid[1][2], num_points)
        errors_test = np.zeros((num_points, num_points, num_points))

        # TODO: make this more efficient.
        #xx, yy, zz= np.meshgrid(x, y, z)
        for i, xs in enumerate(x):
            for j, ys in enumerate(y):
                for k, zs in enumerate(z):
                    errors_test[i, j, k] = cost_function((xs, ys, zs))
        min_idx = errors_test.argmin()
        min_idx_multi = np.unravel_index(min_idx, errors_test.shape)
        xhat = np.c_[x[min_idx_multi[0]],
                     y[min_idx_multi[1]], z[min_idx_multi[2]]]
    else:
        raise ValueError('Non-supported number of dimensions.')
    return xhat[0]


Instruction:
Write a Python 3 script to
range least squares algorithm cited by A. Beck P. Stoica in Approximate and Exact solutions of Source Localization Problems.

Response:
def RLS_SDR(anchors, W, r, print_out=False):
    """ Range least squares (RLS) using SDR.

    Algorithm cited by A.Beck, P.Stoica in "Approximate and Exact solutions of Source Localization Problems".

    :param anchors: anchor points
    :param r2: squared distances from anchors to point x.

    :return: estimated position of point x.
    """
    from pylocus.basics import low_rank_approximation, eigendecomp
    from pylocus.mds import x_from_eigendecomp

    m = anchors.shape[0]
    d = anchors.shape[1]

    G = Variable(m + 1, m + 1)
    X = Variable(d + 1, d + 1)
    constraints = [G[m, m] == 1.0,
                   X[d, d] == 1.0,
                   G >> 0, X >> 0,
                   G == G.T, X == X.T]
    for i in range(m):
        Ci = np.eye(d + 1)
        Ci[:-1, -1] = -anchors[i]
        Ci[-1, :-1] = -anchors[i].T
        Ci[-1, -1] = np.linalg.norm(anchors[i])**2
        constraints.append(G[i, i] == trace(Ci * X))

    obj = Minimize(trace(G) - 2 * sum_entries(mul_elemwise(r, G[m, :-1].T)))
    prob = Problem(obj, constraints)

    ## Solution
    total = prob.solve(verbose=True)
    rank_G = np.linalg.matrix_rank(G.value)
    rank_X = np.linalg.matrix_rank(X.value)
    if rank_G > 1:
        u, s, v = np.linalg.svd(G.value, full_matrices=False)
        print('optimal G is not of rank 1!')
        print(s)
    if rank_X > 1:
        u, s, v = np.linalg.svd(X.value, full_matrices=False)
        print('optimal X is not of rank 1!')
        print(s)

    factor, u = eigendecomp(X.value, 1)
    xhat = x_from_eigendecomp(factor, u, 1)
    return xhat


Instruction:
Implement a function in Python 3 to
join parameters from the lhs to the rhs

Response:
def _join_parameters(base, nxt):
        """ join parameters from the lhs to the rhs, if compatible. """
        if nxt is None:
            return base
        if isinstance(base, set) and isinstance(nxt, set):
            return base | nxt
        else:
            return nxt


Instruction:
Write a Python 3 function that can
get the swagger_schema operation representation.

Response:
def get_swagger_operation(self, context=default_context):
        """
        get the swagger_schema operation representation.
        """
        consumes = produces = context.contenttype_serializers.keys()
        parameters = get_swagger_parameters(self.parameters, context)
        responses = {
            "400": Response(
                {
                    "description": "invalid input received",
                    "schema": Schema(
                        {
                            "title": "FailureObject",
                            "type": "object",
                            "properties": {
                                "success": {"type": "boolean"},
                                "result": {"type": "string"},
                            },
                            "required": ["success", "result"],
                        }
                    ),
                }
            )
        }

        for code, details in self.response_types.items():
            responses[str(code)] = details.swagger_definition(context)

        return Operation(
            {
                "summary": self.summary,
                "description": self.description,
                "consumes": consumes,
                "produces": produces,
                "parameters": parameters,
                "responses": responses,
                "operationId": self.raw_func.__name__,
                "tags": self.tags,
            }
        )


Instruction:
Can you generate the documentation for the following Python 3 function
def process_result(self, context, result_body, exc, content_type):
        return process_result(self, context, result_body, exc, content_type)

Response: Process a result object and return the appropriate result object.


Instruction:
Write a Python 3 script for
parsing the response types from the given parameters and return back the response types dictionary.

Response:
def _parse_response_types(argspec, attrs):
        """
        from the given parameters, return back the response type dictionaries.
        """
        return_type = argspec.annotations.get("return") or None
        type_description = attrs.parameter_descriptions.get("return", "")
        response_types = attrs.response_types.copy()
        if return_type or len(response_types) == 0:
            response_types[attrs.success_code] = ResponseType(
                type=return_type,
                type_description=type_description,
                description="success",
            )
        return response_types


Instruction:
Explain what the following Python 3 code does
def generate_swagger_html(swagger_static_root, swagger_json_url):
    tmpl = _get_template("swagger.html")
    return tmpl.render(
        swagger_root=swagger_static_root, swagger_json_url=swagger_json_url
    )

Response: Generate a swagger. html file from the given swagger statics and swagger json path.


Instruction:
Write a Python 3 function that can
add a transmute function to the spec

Response:
def add_func(self, transmute_func, transmute_context):
        """ add a transmute function's swagger definition to the spec """
        swagger_path = transmute_func.get_swagger_path(transmute_context)
        for p in transmute_func.paths:
            self.add_path(p, swagger_path)


Instruction:
Make a summary of the following Python 3 code
def add_path(self, path, path_item):
        if path not in self._swagger:
            self._swagger[path] = path_item
        else:
            for method, definition in path_item.items():
                if definition is not None:
                    setattr(self._swagger[path], method, definition)

Response: Add the path items to the swagger file.