Instruction:
Write a Python 3 script for
returning the IP address of the given interface name e. g. eth0

Response:
def address_by_interface(ifname):
    """Returns the IP address of the given interface name, e.g. 'eth0'

    Parameters
    ----------
    ifname : str
        Name of the interface whose address is to be returned. Required.

    Taken from this Stack Overflow answer: https://stackoverflow.com/questions/24196932/how-can-i-get-the-ip-address-of-eth0-in-python#24196955
    """
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    return socket.inet_ntoa(fcntl.ioctl(
        s.fileno(),
        0x8915,  # SIOCGIFADDR
        struct.pack('256s', bytes(ifname[:15], 'utf-8'))
    )[20:24])


Instruction:
Can you write a function in Python 3 where it
submits a job to the cluster

Response:
def submit(self, cmd_string, blocksize, tasks_per_node, job_name="parsl"):
        """ Submit a job
        Args:
             - cmd_string  :(String) - Name of the container to initiate
             - blocksize   :(float) - Number of replicas
             - tasks_per_node (int) : command invocations to be launched per node

        Kwargs:
             - job_name (String): Name for job, must be unique
        Returns:
             - None: At capacity, cannot provision more
             - job_id: (string) Identifier for the job
        """
        if not self.resources:
            cur_timestamp = str(time.time() * 1000).split(".")[0]
            job_name = "{0}-{1}".format(job_name, cur_timestamp)

            if not self.deployment_name:
                deployment_name = '{}-deployment'.format(job_name)
            else:
                deployment_name = '{}-{}-deployment'.format(self.deployment_name,
                                                            cur_timestamp)

            formatted_cmd = template_string.format(command=cmd_string,
                                                   worker_init=self.worker_init)

            self.deployment_obj = self._create_deployment_object(job_name,
                                                                 self.image,
                                                                 deployment_name,
                                                                 cmd_string=formatted_cmd,
                                                                 replicas=self.init_blocks,
                                                                 volumes=self.persistent_volumes)
            logger.debug("Deployment name :{}".format(deployment_name))
            self._create_deployment(self.deployment_obj)
            self.resources[deployment_name] = {'status': 'RUNNING',
                                               'pods': self.init_blocks}

        return deployment_name


Instruction:
Write a Python 3 script for
creating a kubernetes deployment object for the job.

Response:
def _create_deployment_object(self, job_name, job_image,
                                  deployment_name, port=80,
                                  replicas=1,
                                  cmd_string=None,
                                  engine_json_file='~/.ipython/profile_default/security/ipcontroller-engine.json',
                                  engine_dir='.',
                                  volumes=[]):
        """ Create a kubernetes deployment for the job.
        Args:
              - job_name (string) : Name of the job and deployment
              - job_image (string) : Docker image to launch
        KWargs:
             - port (integer) : Container port
             - replicas : Number of replica containers to maintain
        Returns:
              - True: The deployment object to launch
        """

        # sorry, quick hack that doesn't pass this stuff through to test it works.
        # TODO it also doesn't only add what is set :(
        security_context = None
        if self.user_id and self.group_id:
            security_context = client.V1SecurityContext(run_as_group=self.group_id,
                                                        run_as_user=self.user_id,
                                                        run_as_non_root=self.run_as_non_root)

        # Create the enviornment variables and command to initiate IPP
        environment_vars = client.V1EnvVar(name="TEST", value="SOME DATA")

        launch_args = ["-c", "{0}; /app/deploy.sh;".format(cmd_string)]

        volume_mounts = []
        # Create mount paths for the volumes
        for volume in volumes:
            volume_mounts.append(client.V1VolumeMount(mount_path=volume[1],
                                                      name=volume[0]))
        # Configureate Pod template container
        container = None
        if security_context:
            container = client.V1Container(
                name=job_name,
                image=job_image,
                ports=[client.V1ContainerPort(container_port=port)],
                volume_mounts=volume_mounts,
                command=['/bin/bash'],
                args=launch_args,
                env=[environment_vars],
                security_context=security_context)
        else:
            container = client.V1Container(
                name=job_name,
                image=job_image,
                ports=[client.V1ContainerPort(container_port=port)],
                volume_mounts=volume_mounts,
                command=['/bin/bash'],
                args=launch_args,
                env=[environment_vars])
        # Create a secret to enable pulling images from secure repositories
        secret = None
        if self.secret:
            secret = client.V1LocalObjectReference(name=self.secret)

        # Create list of volumes from (pvc, mount) tuples
        volume_defs = []
        for volume in volumes:
            volume_defs.append(client.V1Volume(name=volume[0],
                                               persistent_volume_claim=client.V1PersistentVolumeClaimVolumeSource(
                                                   claim_name=volume[0])))

        # Create and configurate a spec section
        template = client.V1PodTemplateSpec(
            metadata=client.V1ObjectMeta(labels={"app": job_name}),
            spec=client.V1PodSpec(containers=[container],
                                  image_pull_secrets=[secret],
                                  volumes=volume_defs
                                  ))

        # Create the specification of deployment
        spec = client.ExtensionsV1beta1DeploymentSpec(replicas=replicas,
                                                      template=template)

        # Instantiate the deployment object
        deployment = client.ExtensionsV1beta1Deployment(
            api_version="extensions/v1beta1",
            kind="Deployment",
            metadata=client.V1ObjectMeta(name=deployment_name),
            spec=spec)

        return deployment


Instruction:
Write a Python 3 script for
creating the kubernetes deployment

Response:
def _create_deployment(self, deployment):
        """ Create the kubernetes deployment """

        api_response = self.kube_client.create_namespaced_deployment(
            body=deployment,
            namespace=self.namespace)

        logger.debug("Deployment created. status='{0}'".format(str(api_response.status)))


Instruction:
Implement a function in Python 3 to
delete the specified deployment.

Response:
def _delete_deployment(self, deployment_name):
        """ Delete deployment """

        api_response = self.kube_client.delete_namespaced_deployment(
            name=deployment_name,
            namespace=self.namespace,
            body=client.V1DeleteOptions(
                propagation_policy='Foreground',
                grace_period_seconds=5))

        logger.debug("Deployment deleted. status='{0}'".format(
            str(api_response.status)))


Instruction:
Can you tell what is the following Python 3 function doing
def initialize_scaling(self):
        debug_opts = "--debug" if self.worker_debug else ""
        max_workers = "" if self.max_workers == float('inf') else "--max_workers={}".format(self.max_workers)

        worker_logdir = "{}/{}".format(self.run_dir, self.label)
        if self.worker_logdir_root is not None:
            worker_logdir = "{}/{}".format(self.worker_logdir_root, self.label)

        l_cmd = self.launch_cmd.format(debug=debug_opts,
                                       prefetch_capacity=self.prefetch_capacity,
                                       task_url=self.worker_task_url,
                                       result_url=self.worker_result_url,
                                       cores_per_worker=self.cores_per_worker,
                                       max_workers=max_workers,
                                       nodes_per_block=self.provider.nodes_per_block,
                                       heartbeat_period=self.heartbeat_period,
                                       heartbeat_threshold=self.heartbeat_threshold,
                                       poll_period=self.poll_period,
                                       logdir=worker_logdir)
        self.launch_cmd = l_cmd
        logger.debug("Launch command: {}".format(self.launch_cmd))

        self._scaling_enabled = self.provider.scaling_enabled
        logger.debug("Starting HighThroughputExecutor with provider:\n%s", self.provider)
        if hasattr(self.provider, 'init_blocks'):
            try:
                self.scale_out(blocks=self.provider.init_blocks)
            except Exception as e:
                logger.error("Scaling out failed: {}".format(e))
                raise e

Response: Initialize the launch command and call the scale_out method.


Instruction:
Write a Python 3 function for
starting the Interchange process and connect to it.

Response:
def start(self):
        """Create the Interchange process and connect to it.
        """
        self.outgoing_q = zmq_pipes.TasksOutgoing("127.0.0.1", self.interchange_port_range)
        self.incoming_q = zmq_pipes.ResultsIncoming("127.0.0.1", self.interchange_port_range)
        self.command_client = zmq_pipes.CommandClient("127.0.0.1", self.interchange_port_range)

        self.is_alive = True

        self._executor_bad_state = threading.Event()
        self._executor_exception = None
        self._queue_management_thread = None
        self._start_queue_management_thread()
        self._start_local_queue_process()

        logger.debug("Created management thread: {}".format(self._queue_management_thread))

        if self.provider:
            self.initialize_scaling()
        else:
            self._scaling_enabled = False
            logger.debug("Starting HighThroughputExecutor with no provider")


Instruction:
How would you explain what the following Python 3 function does
def _queue_management_worker(self):
        logger.debug("[MTHREAD] queue management worker starting")

        while not self._executor_bad_state.is_set():
            try:
                msgs = self.incoming_q.get(timeout=1)
                # logger.debug("[MTHREAD] get has returned {}".format(len(msgs)))

            except queue.Empty:
                logger.debug("[MTHREAD] queue empty")
                # Timed out.
                pass

            except IOError as e:
                logger.exception("[MTHREAD] Caught broken queue with exception code {}: {}".format(e.errno, e))
                return

            except Exception as e:
                logger.exception("[MTHREAD] Caught unknown exception: {}".format(e))
                return

            else:

                if msgs is None:
                    logger.debug("[MTHREAD] Got None, exiting")
                    return

                else:
                    for serialized_msg in msgs:
                        try:
                            msg = pickle.loads(serialized_msg)
                            tid = msg['task_id']
                        except pickle.UnpicklingError:
                            raise BadMessage("Message received could not be unpickled")

                        except Exception:
                            raise BadMessage("Message received does not contain 'task_id' field")

                        if tid == -1 and 'exception' in msg:
                            logger.warning("Executor shutting down due to exception from interchange")
                            self._executor_exception, _ = deserialize_object(msg['exception'])
                            logger.exception("Exception: {}".format(self._executor_exception))
                            # Set bad state to prevent new tasks from being submitted
                            self._executor_bad_state.set()
                            # We set all current tasks to this exception to make sure that
                            # this is raised in the main context.
                            for task in self.tasks:
                                self.tasks[task].set_exception(self._executor_exception)
                            break

                        task_fut = self.tasks[tid]

                        if 'result' in msg:
                            result, _ = deserialize_object(msg['result'])
                            task_fut.set_result(result)

                        elif 'exception' in msg:
                            try:
                                s, _ = deserialize_object(msg['exception'])
                                # s should be a RemoteExceptionWrapper... so we can reraise it
                                try:
                                    s.reraise()
                                except Exception as e:
                                    task_fut.set_exception(e)
                            except Exception as e:
                                # TODO could be a proper wrapped exception?
                                task_fut.set_exception(
                                    DeserializationError("Received exception, but handling also threw an exception: {}".format(e)))
                        else:
                            raise BadMessage("Message received is neither result or exception")

            if not self.is_alive:
                break
        logger.info("[MTHREAD] queue management worker finished")

Response: This function is called by the thread that handles the incoming queue for tasks and handles them.


Instruction:
Write a Python 3 script for
starting the interchange process locally and uses an internal command queue to get the worker task and result ports that the interchange has bound to.

Response:
def _start_local_queue_process(self):
        """ Starts the interchange process locally

        Starts the interchange process locally and uses an internal command queue to
        get the worker task and result ports that the interchange has bound to.
        """
        comm_q = Queue(maxsize=10)
        self.queue_proc = Process(target=interchange.starter,
                                  args=(comm_q,),
                                  kwargs={"client_ports": (self.outgoing_q.port,
                                                           self.incoming_q.port,
                                                           self.command_client.port),
                                          "worker_ports": self.worker_ports,
                                          "worker_port_range": self.worker_port_range,
                                          "logdir": "{}/{}".format(self.run_dir, self.label),
                                          "suppress_failure": self.suppress_failure,
                                          "heartbeat_threshold": self.heartbeat_threshold,
                                          "poll_period": self.poll_period,
                                          "logging_level": logging.DEBUG if self.worker_debug else logging.INFO
                                  },
        )
        self.queue_proc.start()
        try:
            (worker_task_port, worker_result_port) = comm_q.get(block=True, timeout=120)
        except queue.Empty:
            logger.error("Interchange has not completed initialization in 120s. Aborting")
            raise Exception("Interchange failed to start")

        self.worker_task_url = "tcp://{}:{}".format(self.address, worker_task_port)
        self.worker_result_url = "tcp://{}:{}".format(self.address, worker_result_port)


Instruction:
Create a Python 3 function to
put a worker on hold.

Response:
def hold_worker(self, worker_id):
        """Puts a worker on hold, preventing scheduling of additional tasks to it.

        This is called "hold" mostly because this only stops scheduling of tasks,
        and does not actually kill the worker.

        Parameters
        ----------

        worker_id : str
            Worker id to be put on hold
        """
        c = self.command_client.run("HOLD_WORKER;{}".format(worker_id))
        logger.debug("Sent hold request to worker: {}".format(worker_id))
        return c


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

        managers = self.connected_managers

        for manager in managers:
            if manager['block_id'] == block_id:
                logger.debug("[HOLD_BLOCK]: Sending hold to manager:{}".format(manager['manager']))
                self.hold_worker(manager['manager'])

Response: Sends a hold command to all managers which are in a specific block_id


Instruction:
Implement a function in Python 3 to
scale out the number of blocks by blocks Raises NotImplementedError

Response:
def scale_out(self, blocks=1):
        """Scales out the number of blocks by "blocks"

        Raises:
             NotImplementedError
        """
        r = []
        for i in range(blocks):
            if self.provider:
                external_block_id = str(len(self.blocks))
                launch_cmd = self.launch_cmd.format(block_id=external_block_id)
                internal_block = self.provider.submit(launch_cmd, 1, 1)
                logger.debug("Launched block {}->{}".format(external_block_id, internal_block))
                if not internal_block:
                    raise(ScalingFailed(self.provider.label,
                                        "Attempts to provision nodes via provider has failed"))
                r.extend([external_block_id])
                self.blocks[external_block_id] = internal_block
            else:
                logger.error("No execution provider available")
                r = None
        return r


Instruction:
Given the following Python 3 function, write the documentation
def scale_in(self, blocks=None, block_ids=[]):

        if block_ids:
            block_ids_to_kill = block_ids
        else:
            block_ids_to_kill = list(self.blocks.keys())[:blocks]

        # Hold the block
        for block_id in block_ids_to_kill:
            self._hold_block(block_id)

        # Now kill via provider
        to_kill = [self.blocks.pop(bid) for bid in block_ids_to_kill]

        if self.provider:
            r = self.provider.cancel(to_kill)

        return r

Response: This method scales in the number of active blocks by specified amount.


Instruction:
Can you write a function in Python 3 where it
returns status of all blocks.

Response:
def status(self):
        """Return status of all blocks."""

        status = []
        if self.provider:
            status = self.provider.status(self.blocks.values())

        return status


Instruction:
Explain what the following Python 3 code does
def shutdown(self, hub=True, targets='all', block=False):

        logger.info("Attempting HighThroughputExecutor shutdown")
        # self.outgoing_q.close()
        # self.incoming_q.close()
        self.queue_proc.terminate()
        logger.info("Finished HighThroughputExecutor shutdown attempt")
        return True

Response: This method is used to shutdown the executor.


Instruction:
Implement a Python 3 function for
reading from the device into a buffer.

Response:
def readinto(self, buf, **kwargs):
        """
        Read into ``buf`` from the device. The number of bytes read will be the
        length of ``buf``.

        If ``start`` or ``end`` is provided, then the buffer will be sliced
        as if ``buf[start:end]``. This will not cause an allocation like
        ``buf[start:end]`` will so it saves memory.

        :param bytearray buffer: buffer to write into
        :param int start: Index to start writing at
        :param int end: Index to write up to but not include
        """
        self.i2c.readfrom_into(self.device_address, buf, **kwargs)
        if self._debug:
            print("i2c_device.readinto:", [hex(i) for i in buf])


Instruction:
How would you explain what the following Python 3 function does
def write(self, buf, **kwargs):
        self.i2c.writeto(self.device_address, buf, **kwargs)
        if self._debug:
            print("i2c_device.write:", [hex(i) for i in buf])

Response: Write the bytes from buffer to the device.


Instruction:
Implement a Python 3 function for
writing the bytes from out_buffer then immediately reads into in_buffer.

Response:
def write_then_readinto(self, out_buffer, in_buffer, *,
                            out_start=0, out_end=None, in_start=0, in_end=None, stop=True):
        """
        Write the bytes from ``out_buffer`` to the device, then immediately
        reads into ``in_buffer`` from the device. The number of bytes read
        will be the length of ``in_buffer``.
        Transmits a stop bit after the write, if ``stop`` is set.

        If ``out_start`` or ``out_end`` is provided, then the output buffer
        will be sliced as if ``out_buffer[out_start:out_end]``. This will
        not cause an allocation like ``buffer[out_start:out_end]`` will so
        it saves memory.

        If ``in_start`` or ``in_end`` is provided, then the input buffer
        will be sliced as if ``in_buffer[in_start:in_end]``. This will not
        cause an allocation like ``in_buffer[in_start:in_end]`` will so
        it saves memory.

        :param bytearray out_buffer: buffer containing the bytes to write
        :param bytearray in_buffer: buffer containing the bytes to read into
        :param int out_start: Index to start writing from
        :param int out_end: Index to read up to but not include
        :param int in_start: Index to start writing at
        :param int in_end: Index to write up to but not include
        :param bool stop: If true, output an I2C stop condition after the buffer is written
        """
        if out_end is None:
            out_end = len(out_buffer)
        if in_end is None:
            in_end = len(in_buffer)
        if hasattr(self.i2c, 'writeto_then_readfrom'):
            if self._debug:
                print("i2c_device.writeto_then_readfrom.out_buffer:",
                      [hex(i) for i in out_buffer[out_start:out_end]])
            # In linux, at least, this is a special kernel function call
            self.i2c.writeto_then_readfrom(self.device_address, out_buffer, in_buffer,
                                           out_start=out_start, out_end=out_end,
                                           in_start=in_start, in_end=in_end, stop=stop)
            if self._debug:
                print("i2c_device.writeto_then_readfrom.in_buffer:",
                      [hex(i) for i in in_buffer[in_start:in_end]])
        else:
            # If we don't have a special implementation, we can fake it with two calls
            self.write(out_buffer, start=out_start, end=out_end, stop=stop)
            if self._debug:
                print("i2c_device.write_then_readinto.write.out_buffer:",
                      [hex(i) for i in out_buffer[out_start:out_end]])
            self.readinto(in_buffer, start=in_start, end=in_end)
            if self._debug:
                print("i2c_device.write_then_readinto.readinto.in_buffer:",
                      [hex(i) for i in in_buffer[in_start:in_end]])


Instruction:
Given the following Python 3 function, write the documentation
def read_sample(filename):

    file = open(filename, 'r')

    sample = [[float(val) for val in line.split()] for line in file if len(line.strip()) > 0]

    file.close()
    return sample

Response: Read sample from a simple text file.


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

    amount_rows = len(sample);
    return [ [ euclidean_distance(sample[i], sample[j]) for j in range(amount_rows) ] for i in range(amount_rows) ];

Response: Calculates the distance matrix for a sequence of data points.


Instruction:
Can you create a Python 3 function that
reads image as N - dimension matrix

Response:
def read_image(filename):
    """!
    @brief Returns image as N-dimension (depends on the input image) matrix, where one element of list describes pixel.

    @param[in] filename (string): Path to image.

    @return (list) Pixels where each pixel described by list of RGB-values.

    """

    with Image.open(filename) as image_source:
        data = [list(pixel) for pixel in image_source.getdata()]
        return data


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

    image_gray_array = [0.0] * len(image_rgb_array);
    for index in range(0, len(image_rgb_array), 1):
        image_gray_array[index] = float(image_rgb_array[index][0]) * 0.2989 + float(image_rgb_array[index][1]) * 0.5870 + float(image_rgb_array[index][2]) * 0.1140;

    return image_gray_array;

Response: Returns image as 1 - dimension gray colored matrix where one element of list describes pixel.


Instruction:
How would you explain what the following Python 3 function does
def stretch_pattern(image_source):
    wsize, hsize = image_source.size;

    # Crop digit exactly
    (ws, hs, we, he) = gray_pattern_borders(image_source);
    image_source = image_source.crop((ws, hs, we, he));

    # Stretch it to initial sizes
    image_source = image_source.resize((wsize, hsize), Image.ANTIALIAS);

    # Transform image to simple array
    data = [pixel for pixel in image_source.getdata()];
    image_pattern = rgb2gray(data);

    return (image_pattern, image_source);

Response: Returns stretched image as 1 - dimension gray colored matrix with size of input image.


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

    width, height = image.size;

    width_start = width;
    width_end = 0;
    height_start = height;
    height_end = 0;

    row, col = 0, 0;
    for pixel in image.getdata():
        value = float(pixel[0]) * 0.2989 + float(pixel[1]) * 0.5870 + float(pixel[2]) * 0.1140;

        if (value < 128):
            if (width_end < col):
                width_end = col;

            if (height_end < row):
                height_end = row;

            if (width_start > col):
                width_start = col;

            if (height_start > row):
                height_start = row;

        col += 1;
        if (col >= width):
            col = 0;
            row += 1;

    return (width_start, height_start, width_end + 1, height_end + 1);

Response: Returns the coordinates of the gray pattern content on the input image.


Instruction:
Write a Python 3 function that can
calculate the average distance between nearest neighbors of a list of points.

Response:
def average_neighbor_distance(points, num_neigh):
    """!
    @brief Returns average distance for establish links between specified number of nearest neighbors.

    @param[in] points (list): Input data, list of points where each point represented by list.
    @param[in] num_neigh (uint): Number of neighbors that should be used for distance calculation.

    @return (double) Average distance for establish links between 'num_neigh' in data set 'points'.

    """

    if num_neigh > len(points) - 1:
        raise NameError('Impossible to calculate average distance to neighbors when number of object is less than number of neighbors.');

    dist_matrix = [ [ 0.0 for i in range(len(points)) ] for j in range(len(points)) ];
    for i in range(0, len(points), 1):
        for j in range(i + 1, len(points), 1):
            distance = euclidean_distance(points[i], points[j]);
            dist_matrix[i][j] = distance;
            dist_matrix[j][i] = distance;

        dist_matrix[i] = sorted(dist_matrix[i]);

    total_distance = 0;
    for i in range(0, len(points), 1):
        # start from 0 - first element is distance to itself.
        for j in range(0, num_neigh, 1):
            total_distance += dist_matrix[i][j + 1];

    return ( total_distance / (num_neigh * len(points)) );


Instruction:
Write a Python 3 function for
calculating the median of the input points using Euclidean distance.

Response:
def medoid(data, indexes=None, **kwargs):
    """!
    @brief Calculate medoid for input points using Euclidean distance.

    @param[in] data (list): Set of points for that median should be calculated.
    @param[in] indexes (list): Indexes of input set of points that will be taken into account during median calculation.
    @param[in] **kwargs: Arbitrary keyword arguments (available arguments: 'metric', 'data_type').

    Keyword Args:

        - metric (distance_metric): Metric that is used for distance calculation between two points.
        - data_type (string): Data type of input sample 'data' (available values: 'points', 'distance_matrix').

    @return (uint) index of point in input set that corresponds to median.

    """

    index_median = None
    distance = float('Inf')

    metric = kwargs.get('metric', type_metric.EUCLIDEAN_SQUARE)
    data_type = kwargs.get('data_type', 'points')

    if data_type == 'points':
        calculator = lambda index1, index2: metric(data[index1], data[index2])
    elif data_type == 'distance_matrix':
        if isinstance(data, numpy.matrix):
            calculator = lambda index1, index2: data.item(index1, index2)

        else:
            calculator = lambda index1, index2: data[index1][index2]
    else:
        raise TypeError("Unknown type of data is specified '%s'." % data_type)

    if indexes is None:
        range_points = range(len(data))
    else:
        range_points = indexes

    for index_candidate in range_points:
        distance_candidate = 0.0
        for index in range_points:
            distance_candidate += calculator(index_candidate, index)

        if distance_candidate < distance:
            distance = distance_candidate
            index_median = index_candidate

    return index_median


Instruction:
How would you explain what the following Python 3 function does
def euclidean_distance_square(a, b):

    if ( ((type(a) == float) and (type(b) == float)) or ((type(a) == int) and (type(b) == int)) ):
        return (a - b)**2.0;

    distance = 0.0;
    for i in range(0, len(a)):
        distance += (a[i] - b[i])**2.0;

    return distance;

Response: Calculates the Euclidian distance between two vectors a and b.


Instruction:
Explain what the following Python 3 code does
def manhattan_distance(a, b):

    if ( ((type(a) == float) and (type(b) == float)) or ((type(a) == int) and (type(b) == int)) ):
        return abs(a - b);

    distance = 0.0;
    dimension = len(a);

    for i in range(0, dimension):
        distance += abs(a[i] - b[i]);

    return distance;

Response: Calculates the Manhattan distance between two vectors.


Instruction:
How would you explain what the following Python 3 function does
def average_inter_cluster_distance(cluster1, cluster2, data = None):

    distance = 0.0;

    if (data is None):
        for i in range(len(cluster1)):
            for j in range(len(cluster2)):
                distance += euclidean_distance_square(cluster1[i], cluster2[j]);
    else:
        for i in range(len(cluster1)):
            for j in range(len(cluster2)):
                distance += euclidean_distance_square(data[ cluster1[i]], data[ cluster2[j]]);

    distance /= float(len(cluster1) * len(cluster2));
    return distance ** 0.5;

Response: Calculates the average inter - cluster distance between two clusters.


Instruction:
Write a Python 3 script to
calculate the average intra - cluster distance between two clusters.

Response:
def average_intra_cluster_distance(cluster1, cluster2, data=None):
    """!
    @brief Calculates average intra-cluster distance between two clusters.
    @details Clusters can be represented by list of coordinates (in this case data shouldn't be specified),
             or by list of indexes of points from the data (represented by list of points), in this case
             data should be specified.

    @param[in] cluster1 (list): The first cluster.
    @param[in] cluster2 (list): The second cluster.
    @param[in] data (list): If specified than elements of clusters will be used as indexes,
               otherwise elements of cluster will be considered as points.

    @return (double) Average intra-cluster distance between two clusters.

    """

    distance = 0.0

    for i in range(len(cluster1) + len(cluster2)):
        for j in range(len(cluster1) + len(cluster2)):
            if data is None:
                # the first point
                if i < len(cluster1):
                    first_point = cluster1[i]
                else:
                    first_point = cluster2[i - len(cluster1)]

                # the second point
                if j < len(cluster1):
                    second_point = cluster1[j]
                else:
                    second_point = cluster2[j - len(cluster1)]

            else:
                # the first point
                if i < len(cluster1):
                    first_point = data[cluster1[i]]
                else:
                    first_point = data[cluster2[i - len(cluster1)]]

                if j < len(cluster1):
                    second_point = data[cluster1[j]]
                else:
                    second_point = data[cluster2[j - len(cluster1)]]

            distance += euclidean_distance_square(first_point, second_point)

    distance /= float((len(cluster1) + len(cluster2)) * (len(cluster1) + len(cluster2) - 1.0))
    return distance ** 0.5


Instruction:
Write a Python 3 script for
calculating the variance increase distance between two clusters.

Response:
def variance_increase_distance(cluster1, cluster2, data = None):
    """!
    @brief Calculates variance increase distance between two clusters.
    @details Clusters can be represented by list of coordinates (in this case data shouldn't be specified),
             or by list of indexes of points from the data (represented by list of points), in this case
             data should be specified.

    @param[in] cluster1 (list): The first cluster.
    @param[in] cluster2 (list): The second cluster.
    @param[in] data (list): If specified than elements of clusters will be used as indexes,
               otherwise elements of cluster will be considered as points.

    @return (double) Average variance increase distance between two clusters.

    """

    # calculate local sum
    if data is None:
        member_cluster1 = [0.0] * len(cluster1[0])
        member_cluster2 = [0.0] * len(cluster2[0])

    else:
        member_cluster1 = [0.0] * len(data[0])
        member_cluster2 = [0.0] * len(data[0])

    for i in range(len(cluster1)):
        if data is None:
            member_cluster1 = list_math_addition(member_cluster1, cluster1[i])
        else:
            member_cluster1 = list_math_addition(member_cluster1, data[ cluster1[i] ])

    for j in range(len(cluster2)):
        if data is None:
            member_cluster2 = list_math_addition(member_cluster2, cluster2[j])
        else:
            member_cluster2 = list_math_addition(member_cluster2, data[ cluster2[j] ])

    member_cluster_general = list_math_addition(member_cluster1, member_cluster2)
    member_cluster_general = list_math_division_number(member_cluster_general, len(cluster1) + len(cluster2))

    member_cluster1 = list_math_division_number(member_cluster1, len(cluster1))
    member_cluster2 = list_math_division_number(member_cluster2, len(cluster2))

    # calculate global sum
    distance_general = 0.0
    distance_cluster1 = 0.0
    distance_cluster2 = 0.0

    for i in range(len(cluster1)):
        if data is None:
            distance_cluster1 += euclidean_distance_square(cluster1[i], member_cluster1)
            distance_general += euclidean_distance_square(cluster1[i], member_cluster_general)

        else:
            distance_cluster1 += euclidean_distance_square(data[ cluster1[i]], member_cluster1)
            distance_general += euclidean_distance_square(data[ cluster1[i]], member_cluster_general)

    for j in range(len(cluster2)):
        if data is None:
            distance_cluster2 += euclidean_distance_square(cluster2[j], member_cluster2)
            distance_general += euclidean_distance_square(cluster2[j], member_cluster_general)

        else:
            distance_cluster2 += euclidean_distance_square(data[ cluster2[j]], member_cluster2)
            distance_general += euclidean_distance_square(data[ cluster2[j]], member_cluster_general)

    return distance_general - distance_cluster1 - distance_cluster2


Instruction:
Explain what the following Python 3 code does
def calculate_ellipse_description(covariance, scale = 2.0):

    eigh_values, eigh_vectors = numpy.linalg.eigh(covariance)
    order = eigh_values.argsort()[::-1]

    values, vectors = eigh_values[order], eigh_vectors[order]
    angle = numpy.degrees(numpy.arctan2(*vectors[:,0][::-1]))

    if 0.0 in values:
        return 0, 0, 0

    width, height = 2.0 * scale * numpy.sqrt(values)
    return angle, width, height

Response: Calculates the description of an ellipse using a covariance matrix.


Instruction:
Here you have a function in Python 3, explain what it does
def data_corners(data, data_filter = None):

    dimensions = len(data[0])

    bypass = data_filter
    if bypass is None:
        bypass = range(len(data))

    maximum_corner = list(data[bypass[0]][:])
    minimum_corner = list(data[bypass[0]][:])

    for index_point in bypass:
        for index_dimension in range(dimensions):
            if data[index_point][index_dimension] > maximum_corner[index_dimension]:
                maximum_corner[index_dimension] = data[index_point][index_dimension]

            if data[index_point][index_dimension] < minimum_corner[index_dimension]:
                minimum_corner[index_dimension] = data[index_point][index_dimension]

    return minimum_corner, maximum_corner

Response: Returns a tuple of two points that corresponds to the maximum and minimum corner in each dimension of the specified data.


Instruction:
Implement a function in Python 3 to
calculate the norm of an input vector that is known as a vector length.

Response:
def norm_vector(vector):
    """!
    @brief Calculates norm of an input vector that is known as a vector length.

    @param[in] vector (list): The input vector whose length is calculated.

    @return (double) vector norm known as vector length.

    """

    length = 0.0
    for component in vector:
        length += component * component

    length = length ** 0.5

    return length


Instruction:
Write a Python 3 function for
executing specified function or method with measuring of execution time.

Response:
def timedcall(executable_function, *args):
    """!
    @brief Executes specified method or function with measuring of execution time.

    @param[in] executable_function (pointer): Pointer to function or method.
    @param[in] args (*): Arguments of called function or method.

    @return (tuple) Execution time and result of execution of function or method (execution_time, result_execution).

    """

    time_start = time.clock();
    result = executable_function(*args);
    time_end = time.clock();

    return (time_end - time_start, result);


Instruction:
Here you have a function in Python 3, explain what it does
def extract_number_oscillations(osc_dyn, index = 0, amplitude_threshold = 1.0):

    number_oscillations = 0;
    waiting_differential = False;
    threshold_passed = False;
    high_level_trigger = True if (osc_dyn[0][index] > amplitude_threshold) else False;

    for values in osc_dyn:
        if ( (values[index] >= amplitude_threshold) and (high_level_trigger is False) ):
            high_level_trigger = True;
            threshold_passed = True;

        elif ( (values[index] < amplitude_threshold) and (high_level_trigger is True) ):
            high_level_trigger = False;
            threshold_passed = True;

        if (threshold_passed is True):
            threshold_passed = False;
            if (waiting_differential is True and high_level_trigger is False):
                number_oscillations += 1;
                waiting_differential = False;

            else:
                waiting_differential = True;

    return number_oscillations;

Response: Returns the number of oscillations in a dynamic list.


Instruction:
Can you generate the documentation for the following Python 3 function
def allocate_sync_ensembles(dynamic, tolerance = 0.1, threshold = 1.0, ignore = None):

    descriptors = [ [] for _ in range(len(dynamic[0])) ];

    # Check from the end for obtaining result
    for index_dyn in range(0, len(dynamic[0]), 1):
        if ((ignore is not None) and (index_dyn in ignore)):
            continue;

        time_stop_simulation = len(dynamic) - 1;
        active_state = False;

        if (dynamic[time_stop_simulation][index_dyn] > threshold):
            active_state = True;

        # if active state is detected, it means we don't have whole oscillatory period for the considered oscillator, should be skipped.
        if (active_state is True):
            while ( (dynamic[time_stop_simulation][index_dyn] > threshold) and (time_stop_simulation > 0) ):
                time_stop_simulation -= 1;

            # if there are no any oscillation than let's consider it like noise
            if (time_stop_simulation == 0):
                continue;

            # reset
            active_state = False;

        desc = [0, 0, 0]; # end, start, average time of oscillation
        for t in range(time_stop_simulation, 0, -1):
            if ( (dynamic[t][index_dyn] > threshold) and (active_state is False) ):
                desc[0] = t;
                active_state = True;
            elif ( (dynamic[t][index_dyn] < threshold) and (active_state is True) ):
                desc[1] = t;
                active_state = False;

                break;

        if (desc == [0, 0, 0]):
            continue;

        desc[2] = desc[1] + (desc[0] - desc[1]) / 2.0;
        descriptors[index_dyn] = desc;

    # Cluster allocation
    sync_ensembles = [];
    desc_sync_ensembles = [];

    for index_desc in range(0, len(descriptors), 1):
        if (descriptors[index_desc] == []):
            continue;

        if (len(sync_ensembles) == 0):
            desc_ensemble = descriptors[index_desc];
            reducer = (desc_ensemble[0] - desc_ensemble[1]) * tolerance;

            desc_ensemble[0] = desc_ensemble[2] + reducer;
            desc_ensemble[1] = desc_ensemble[2] - reducer;

            desc_sync_ensembles.append(desc_ensemble);
            sync_ensembles.append([ index_desc ]);
        else:
            oscillator_captured = False;
            for index_ensemble in range(0, len(sync_ensembles), 1):
                if ( (desc_sync_ensembles[index_ensemble][0] > descriptors[index_desc][2]) and (desc_sync_ensembles[index_ensemble][1] < descriptors[index_desc][2])):
                    sync_ensembles[index_ensemble].append(index_desc);
                    oscillator_captured = True;
                    break;

            if (oscillator_captured is False):
                desc_ensemble = descriptors[index_desc];
                reducer = (desc_ensemble[0] - desc_ensemble[1]) * tolerance;

                desc_ensemble[0] = desc_ensemble[2] + reducer;
                desc_ensemble[1] = desc_ensemble[2] - reducer;

                desc_sync_ensembles.append(desc_ensemble);
                sync_ensembles.append([ index_desc ]);

    return sync_ensembles;

Response: Allocate clusters in line with ensembles of synchronous oscillators that are not in the same sync ensemble.


Instruction:
Can you generate the documentation for the following Python 3 function
def draw_clusters(data, clusters, noise = [], marker_descr = '.', hide_axes = False, axes = None, display_result = True):
    # Get dimension
    dimension = 0;
    if ( (data is not None) and (clusters is not None) ):
        dimension = len(data[0]);
    elif ( (data is None) and (clusters is not None) ):
        dimension = len(clusters[0][0]);
    else:
        raise NameError('Data or clusters should be specified exactly.');

    "Draw clusters"
    colors = [ 'red', 'blue', 'darkgreen', 'brown', 'violet',
               'deepskyblue', 'darkgrey', 'lightsalmon', 'deeppink', 'yellow',
               'black', 'mediumspringgreen', 'orange', 'darkviolet', 'darkblue',
               'silver', 'lime', 'pink', 'gold', 'bisque' ];

    if (len(clusters) > len(colors)):
        raise NameError('Impossible to represent clusters due to number of specified colors.');

    fig = plt.figure();

    if (axes is None):
        # Check for dimensions
        if ((dimension) == 1 or (dimension == 2)):
            axes = fig.add_subplot(111);
        elif (dimension == 3):
            axes = fig.gca(projection='3d');
        else:
            raise NameError('Drawer supports only 2d and 3d data representation');

    color_index = 0;
    for cluster in clusters:
        color = colors[color_index];
        for item in cluster:
            if (dimension == 1):
                if (data is None):
                    axes.plot(item[0], 0.0, color = color, marker = marker_descr);
                else:
                    axes.plot(data[item][0], 0.0, color = color, marker = marker_descr);

            if (dimension == 2):
                if (data is None):
                    axes.plot(item[0], item[1], color = color, marker = marker_descr);
                else:
                    axes.plot(data[item][0], data[item][1], color = color, marker = marker_descr);

            elif (dimension == 3):
                if (data is None):
                    axes.scatter(item[0], item[1], item[2], c = color, marker = marker_descr);
                else:
                    axes.scatter(data[item][0], data[item][1], data[item][2], c = color, marker = marker_descr);

        color_index += 1;

    for item in noise:
        if (dimension == 1):
            if (data is None):
                axes.plot(item[0], 0.0, 'w' + marker_descr);
            else:
                axes.plot(data[item][0], 0.0, 'w' + marker_descr);

        if (dimension == 2):
            if (data is None):
                axes.plot(item[0], item[1], 'w' + marker_descr);
            else:
                axes.plot(data[item][0], data[item][1], 'w' + marker_descr);

        elif (dimension == 3):
            if (data is None):
                axes.scatter(item[0], item[1], item[2], c = 'w', marker = marker_descr);
            else:
                axes.scatter(data[item][0], data[item][1], data[item][2], c = 'w', marker = marker_descr);

    axes.grid(True);

    if (hide_axes is True):
        axes.xaxis.set_ticklabels([]);
        axes.yaxis.set_ticklabels([]);

        if (dimension == 3):
            axes.zaxis.set_ticklabels([]);

    if (display_result is True):
        plt.show();

    return axes;

Response: Displays clusters for data in 2D or 3D or 3D format.


Instruction:
Create a Python 3 function for
setting the parameters for a single matplotlib axes.

Response:
def set_ax_param(ax, x_title = None, y_title = None, x_lim = None, y_lim = None, x_labels = True, y_labels = True, grid = True):
    """!
    @brief Sets parameters for matplotlib ax.

    @param[in] ax (Axes): Axes for which parameters should applied.
    @param[in] x_title (string): Title for Y.
    @param[in] y_title (string): Title for X.
    @param[in] x_lim (double): X limit.
    @param[in] y_lim (double): Y limit.
    @param[in] x_labels (bool): If True - shows X labels.
    @param[in] y_labels (bool): If True - shows Y labels.
    @param[in] grid (bool): If True - shows grid.

    """
    from matplotlib.font_manager import FontProperties;
    from matplotlib import rcParams;

    if (_platform == "linux") or (_platform == "linux2"):
        rcParams['font.sans-serif'] = ['Liberation Serif'];
    else:
        rcParams['font.sans-serif'] = ['Arial'];

    rcParams['font.size'] = 12;

    surface_font = FontProperties();
    if (_platform == "linux") or (_platform == "linux2"):
        surface_font.set_name('Liberation Serif');
    else:
        surface_font.set_name('Arial');

    surface_font.set_size('12');

    if (y_title is not None): ax.set_ylabel(y_title, fontproperties = surface_font);
    if (x_title is not None): ax.set_xlabel(x_title, fontproperties = surface_font);

    if (x_lim is not None): ax.set_xlim(x_lim[0], x_lim[1]);
    if (y_lim is not None): ax.set_ylim(y_lim[0], y_lim[1]);

    if (x_labels is False): ax.xaxis.set_ticklabels([]);
    if (y_labels is False): ax.yaxis.set_ticklabels([]);

    ax.grid(grid);


Instruction:
Here you have a function in Python 3, explain what it does
def draw_dynamics_set(dynamics, xtitle = None, ytitle = None, xlim = None, ylim = None, xlabels = False, ylabels = False):
    # Calculate edge for confortable representation.
    number_dynamics = len(dynamics);
    if (number_dynamics == 1):
        draw_dynamics(dynamics[0][0], dynamics[0][1], xtitle, ytitle, xlim, ylim, xlabels, ylabels);
        return;

    number_cols = int(numpy.ceil(number_dynamics ** 0.5));
    number_rows = int(numpy.ceil(number_dynamics / number_cols));

    real_index = 0, 0;
    double_indexer = True;
    if ( (number_cols == 1) or (number_rows == 1) ):
        real_index = 0;
        double_indexer = False;

    (_, axarr) = plt.subplots(number_rows, number_cols);
    #plt.setp([ax for ax in axarr], visible = False);

    for dynamic in dynamics:
        axarr[real_index] = draw_dynamics(dynamic[0], dynamic[1], xtitle, ytitle, xlim, ylim, xlabels, ylabels, axes = axarr[real_index]);
        #plt.setp(axarr[real_index], visible = True);

        if (double_indexer is True):
            real_index = real_index[0], real_index[1] + 1;
            if (real_index[1] >= number_cols):
                real_index = real_index[0] + 1, 0;
        else:
            real_index += 1;

    plt.show();

Response: Draws a set of dynamics in the network.


Instruction:
Can you create a Python 3 function that
displays image segments using colored image.

Response:
def draw_image_color_segments(source, clusters, hide_axes = True):
    """!
    @brief Shows image segments using colored image.
    @details Each color on result image represents allocated segment. The first image is initial and other is result of segmentation.

    @param[in] source (string): Path to image.
    @param[in] clusters (list): List of clusters (allocated segments of image) where each cluster
                                consists of indexes of pixel from source image.
    @param[in] hide_axes (bool): If True then axes will not be displayed.

    """

    image_source = Image.open(source);
    image_size = image_source.size;

    (fig, axarr) = plt.subplots(1, 2);

    plt.setp([ax for ax in axarr], visible = False);

    available_colors = [ (0, 162, 232),   (34, 177, 76),   (237, 28, 36),
                         (255, 242, 0),   (0, 0, 0),       (237, 28, 36),
                         (255, 174, 201), (127, 127, 127), (185, 122, 87),
                         (200, 191, 231), (136, 0, 21),    (255, 127, 39),
                         (63, 72, 204),   (195, 195, 195), (255, 201, 14),
                         (239, 228, 176), (181, 230, 29),  (153, 217, 234),
                         (112, 146, 180) ];

    image_color_segments = [(255, 255, 255)] * (image_size[0] * image_size[1]);

    for index_segment in range(len(clusters)):
        for index_pixel in clusters[index_segment]:
            image_color_segments[index_pixel] = available_colors[index_segment];

    stage = array(image_color_segments, numpy.uint8);
    stage = numpy.reshape(stage, (image_size[1], image_size[0]) + ((3),)); # ((3),) it's size of RGB - third dimension.
    image_cluster = Image.fromarray(stage, 'RGB');

    axarr[0].imshow(image_source, interpolation = 'none');
    axarr[1].imshow(image_cluster, interpolation = 'none');

    for i in range(2):
        plt.setp(axarr[i], visible = True);

        if (hide_axes is True):
            axarr[i].xaxis.set_ticklabels([]);
            axarr[i].yaxis.set_ticklabels([]);
            axarr[i].xaxis.set_ticks_position('none');
            axarr[i].yaxis.set_ticks_position('none');

    plt.show();


Instruction:
Implement a Python 3 function for
drawing black masks of segments from a source image.

Response:
def draw_image_mask_segments(source, clusters, hide_axes = True):
    """!
    @brief Shows image segments using black masks.
    @details Each black mask of allocated segment is presented on separate plot.
             The first image is initial and others are black masks of segments.

    @param[in] source (string): Path to image.
    @param[in] clusters (list): List of clusters (allocated segments of image) where each cluster
                                consists of indexes of pixel from source image.
    @param[in] hide_axes (bool): If True then axes will not be displayed.

    """
    if (len(clusters) == 0):
        print("Warning: Nothing to draw - list of clusters is empty.")
        return;

    image_source = Image.open(source);
    image_size = image_source.size;

    # Calculate edge for confortable representation.
    number_clusters = len(clusters) + 1; # show with the source image

    number_cols = int(numpy.ceil(number_clusters ** 0.5));
    number_rows = int(numpy.ceil(number_clusters / number_cols));

    real_index = 0, 0;
    double_indexer = True;
    if ( (number_cols == 1) or (number_rows == 1) ):
        real_index = 0;
        double_indexer = False;

    (fig, axarr) = plt.subplots(number_rows, number_cols);
    plt.setp([ax for ax in axarr], visible = False);

    axarr[real_index].imshow(image_source, interpolation = 'none');
    plt.setp(axarr[real_index], visible = True);

    if (hide_axes is True):
        axarr[real_index].xaxis.set_ticklabels([]);
        axarr[real_index].yaxis.set_ticklabels([]);
        axarr[real_index].xaxis.set_ticks_position('none');
        axarr[real_index].yaxis.set_ticks_position('none');

    if (double_indexer is True):
        real_index = 0, 1;
    else:
        real_index += 1;

    for cluster in clusters:
        stage_cluster = [(255, 255, 255)] * (image_size[0] * image_size[1]);
        for index in cluster:
            stage_cluster[index] = (0, 0, 0);

        stage = array(stage_cluster, numpy.uint8);
        stage = numpy.reshape(stage, (image_size[1], image_size[0]) + ((3),)); # ((3),) it's size of RGB - third dimension.

        image_cluster = Image.fromarray(stage, 'RGB');

        axarr[real_index].imshow(image_cluster, interpolation = 'none');
        plt.setp(axarr[real_index], visible = True);

        if (hide_axes is True):
            axarr[real_index].xaxis.set_ticklabels([]);
            axarr[real_index].yaxis.set_ticklabels([]);

            axarr[real_index].xaxis.set_ticks_position('none');
            axarr[real_index].yaxis.set_ticks_position('none');

        if (double_indexer is True):
            real_index = real_index[0], real_index[1] + 1;
            if (real_index[1] >= number_cols):
                real_index = real_index[0] + 1, 0;
        else:
            real_index += 1;

    plt.show();


Instruction:
Can you write a function in Python 3 where it
calculates the linear sum of the input list that can be represented by list in case of multidimensional elements.

Response:
def linear_sum(list_vector):
    """!
    @brief Calculates linear sum of vector that is represented by list, each element can be represented by list - multidimensional elements.

    @param[in] list_vector (list): Input vector.

    @return (list|double) Linear sum of vector that can be represented by list in case of multidimensional elements.

    """
    dimension = 1;
    linear_sum = 0.0;
    list_representation = (type(list_vector[0]) == list);

    if (list_representation is True):
        dimension = len(list_vector[0]);
        linear_sum = [0] * dimension;

    for index_element in range(0, len(list_vector)):
        if (list_representation is True):
            for index_dimension in range(0, dimension):
                linear_sum[index_dimension] += list_vector[index_element][index_dimension];
        else:
            linear_sum += list_vector[index_element];

    return linear_sum;


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

    square_sum = 0.0;
    list_representation = (type(list_vector[0]) == list);

    for index_element in range(0, len(list_vector)):
        if (list_representation is True):
            square_sum += sum(list_math_multiplication(list_vector[index_element], list_vector[index_element]));
        else:
            square_sum += list_vector[index_element] * list_vector[index_element];

    return square_sum;

Response: Calculates the square sum of the list - multidimensional elements.


Instruction:
Write a Python 3 script to
return the number of mathematical subtraction between list a and b.

Response:
def list_math_substraction_number(a, b):
    """!
    @brief Calculates subtraction between list and number.
    @details Each element from list 'a' is subtracted by number 'b'.

    @param[in] a (list): List of elements that supports mathematical subtraction.
    @param[in] b (list): Value that supports mathematical subtraction.

    @return (list) Results of subtraction between list and number.

    """
    return [a[i] - b for i in range(len(a))];


Instruction:
Here you have a function in Python 3, explain what it does
def list_math_addition(a, b):
    return [a[i] + b[i] for i in range(len(a))];

Response: Adds two lists of mathematic elements to the end of a list.


Instruction:
Explain what the following Python 3 code does
def list_math_addition_number(a, b):
    return [a[i] + b for i in range(len(a))];

Response: Adds two lists of mathematic addition numbers.


Instruction:
Create a Python 3 function for
returning a list of numbers that are divided by number b.

Response:
def list_math_division_number(a, b):
    """!
    @brief Division between list and number.
    @details Each element from list 'a' is divided by number 'b'.

    @param[in] a (list): List of elements that supports mathematic division.
    @param[in] b (double): Value that supports mathematic division.

    @return (list) Result of division between list and number.

    """
    return [a[i] / b for i in range(len(a))];


Instruction:
Given the following Python 3 function, write the documentation
def list_math_division(a, b):
    return [a[i] / b[i] for i in range(len(a))];

Response: Returns a list of mathematic division of two lists.


Instruction:
Here you have a function in Python 3, explain what it does
def list_math_multiplication_number(a, b):
    return [a[i] * b for i in range(len(a))];

Response: Returns a list that supports mathematic division between list a and number b.


Instruction:
Explain what the following Python 3 code does
def list_math_multiplication(a, b):
    return [a[i] * b[i] for i in range(len(a))];

Response: Returns the mathematic multiplication of two lists.


Instruction:
Write a Python 3 script to
create all the oscillators and the user s entries between all the oscillators.

Response:
def __create_all_to_all_connections(self):
        """!
        @brief Creates connections between all oscillators.

        """

        if (self._conn_represent == conn_represent.MATRIX):
            for index in range(0, self._num_osc, 1):
                self._osc_conn.append([True] * self._num_osc);
                self._osc_conn[index][index] = False;

        elif (self._conn_represent == conn_represent.LIST):
            for index in range(0, self._num_osc, 1):
                self._osc_conn.append([neigh for neigh in range(0, self._num_osc, 1) if index != neigh]);


Instruction:
Create a Python 3 function to
create four grid structure connections.

Response:
def __create_grid_four_connections(self):
        """!
        @brief Creates network with connections that make up four grid structure.
        @details Each oscillator may be connected with four neighbors in line with 'grid' structure: right, upper, left, lower.

        """

        side_size = self.__width;
        if (self._conn_represent == conn_represent.MATRIX):
            self._osc_conn = [[0] * self._num_osc for index in range(0, self._num_osc, 1)];
        elif (self._conn_represent == conn_represent.LIST):
            self._osc_conn = [[] for index in range(0, self._num_osc, 1)];
        else:
            raise NameError("Unknown type of representation of connections");

        for index in range(0, self._num_osc, 1):
            upper_index = index - side_size;
            lower_index = index + side_size;
            left_index = index - 1;
            right_index = index + 1;

            node_row_index = math.ceil(index / side_size);
            if (upper_index >= 0):
                self.__create_connection(index, upper_index);

            if (lower_index < self._num_osc):
                self.__create_connection(index, lower_index);

            if ( (left_index >= 0) and (math.ceil(left_index / side_size) == node_row_index) ):
                self.__create_connection(index, left_index);

            if ( (right_index < self._num_osc) and (math.ceil(right_index / side_size) == node_row_index) ):
                self.__create_connection(index, right_index);


Instruction:
How would you implement a function in Python 3 that
creates network with eight neighbors.

Response:
def __create_grid_eight_connections(self):
        """!
        @brief Creates network with connections that make up eight grid structure.
        @details Each oscillator may be connected with eight neighbors in line with grid structure: right, right-upper, upper, upper-left, left, left-lower, lower, lower-right.

        """

        self.__create_grid_four_connections();     # create connection with right, upper, left, lower.
        side_size = self.__width;

        for index in range(0, self._num_osc, 1):
            upper_left_index = index - side_size - 1;
            upper_right_index = index - side_size + 1;

            lower_left_index = index + side_size - 1;
            lower_right_index = index + side_size + 1;

            node_row_index = math.floor(index / side_size);
            upper_row_index = node_row_index - 1;
            lower_row_index = node_row_index + 1;

            if ( (upper_left_index >= 0) and (math.floor(upper_left_index / side_size) == upper_row_index) ):
                self.__create_connection(index, upper_left_index);

            if ( (upper_right_index >= 0) and (math.floor(upper_right_index / side_size) == upper_row_index) ):
                self.__create_connection(index, upper_right_index);

            if ( (lower_left_index < self._num_osc) and (math.floor(lower_left_index / side_size) == lower_row_index) ):
                self.__create_connection(index, lower_left_index);

            if ( (lower_right_index < self._num_osc) and (math.floor(lower_right_index / side_size) == lower_row_index) ):
                self.__create_connection(index, lower_right_index);


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

        if (self._conn_represent == conn_represent.MATRIX):
            for index in range(0, self._num_osc, 1):
                self._osc_conn.append([0] * self._num_osc);
                self._osc_conn[index][index] = False;
                if (index > 0):
                    self._osc_conn[index][index - 1] = True;

                if (index < (self._num_osc - 1)):
                    self._osc_conn[index][index + 1] = True;

        elif (self._conn_represent == conn_represent.LIST):
            for index in range(self._num_osc):
                self._osc_conn.append([]);
                if (index > 0):
                    self._osc_conn[index].append(index - 1);

                if (index < (self._num_osc - 1)):
                    self._osc_conn[index].append(index + 1);

Response: Creates the list of classical list connections.


Instruction:
Explain what the following Python 3 code does
def __create_none_connections(self):
        if (self._conn_represent == conn_represent.MATRIX):
            for _ in range(0, self._num_osc, 1):
                self._osc_conn.append([False] * self._num_osc);
        elif (self._conn_represent == conn_represent.LIST):
            self._osc_conn = [[] for _ in range(0, self._num_osc, 1)];

Response: Creates network with no connections.


Instruction:
Can you write a function in Python 3 where it
creates the connection in line with representation of matrix connections.

Response:
def _create_structure(self, type_conn = conn_type.ALL_TO_ALL):
        """!
        @brief Creates connection in line with representation of matrix connections [NunOsc x NumOsc].

        @param[in] type_conn (conn_type): Connection type (all-to-all, bidirectional list, grid structure, etc.) that is used by the network.

        """

        self._osc_conn = list();

        if (type_conn == conn_type.NONE):
            self.__create_none_connections();

        elif (type_conn == conn_type.ALL_TO_ALL):
            self.__create_all_to_all_connections();

        elif (type_conn == conn_type.GRID_FOUR):
            self.__create_grid_four_connections();

        elif (type_conn == conn_type.GRID_EIGHT):
            self.__create_grid_eight_connections();

        elif (type_conn == conn_type.LIST_BIDIR):
            self.__create_list_bidir_connections();

        elif (type_conn == conn_type.DYNAMIC):
            self.__create_dynamic_connection();

        else:
            raise NameError('The unknown type of connections');


Instruction:
Create a Python 3 function for
returning True if there is a connection between i and j oscillators and False otherwise.

Response:
def has_connection(self, i, j):
        """!
        @brief Returns True if there is connection between i and j oscillators and False - if connection doesn't exist.

        @param[in] i (uint): index of an oscillator in the network.
        @param[in] j (uint): index of an oscillator in the network.

        """
        if (self._conn_represent == conn_represent.MATRIX):
            return (self._osc_conn[i][j]);

        elif (self._conn_represent == conn_represent.LIST):
            for neigh_index in range(0, len(self._osc_conn[i]), 1):
                if (self._osc_conn[i][neigh_index] == j):
                    return True;
            return False;

        else:
            raise NameError("Unknown type of representation of coupling");


Instruction:
Explain what the following Python 3 code does
def set_connection(self, i, j):

        if (self.structure != conn_type.DYNAMIC):
            raise NameError("Connection between oscillators can be changed only in case of dynamic type.");

        if (self._conn_represent == conn_represent.MATRIX):
            self._osc_conn[i][j] = True;
            self._osc_conn[j][i] = True;
        else:
            self._osc_conn[i].append(j);
            self._osc_conn[j].append(i);

Response: Sets the connection between two specified oscillators.


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

        if (self._conn_represent == conn_represent.LIST):
            return self._osc_conn[index];      # connections are represented by list.
        elif (self._conn_represent == conn_represent.MATRIX):
            return [neigh_index for neigh_index in range(self._num_osc) if self._osc_conn[index][neigh_index] == True];
        else:
            raise NameError("Unknown type of representation of connections");

Response: Returns a list of neighbors of the specified oscillator with the specified index.


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

        random.seed()

        for _ in range(0, self.__numlocal):
            # set (current) random medoids
            self.__current = random.sample(range(0, len(self.__pointer_data)), self.__number_clusters)

            # update clusters in line with random allocated medoids
            self.__update_clusters(self.__current)

            # optimize configuration
            self.__optimize_configuration()

            # obtain cost of current cluster configuration and compare it with the best obtained
            estimation = self.__calculate_estimation()
            if estimation < self.__optimal_estimation:
                self.__optimal_medoids = self.__current[:]
                self.__optimal_estimation = estimation

        self.__update_clusters(self.__optimal_medoids)

Response: This method is called by the process method in order to process the current cluster.


Instruction:
Can you tell what is the following Python 3 function doing
def __update_clusters(self, medoids):

        self.__belong = [0] * len(self.__pointer_data)
        self.__clusters = [[] for i in range(len(medoids))]
        for index_point in range(len(self.__pointer_data)):
            index_optim = -1
            dist_optim = 0.0

            for index in range(len(medoids)):
                dist = euclidean_distance_square(self.__pointer_data[index_point], self.__pointer_data[medoids[index]])

                if (dist < dist_optim) or (index is 0):
                    index_optim = index
                    dist_optim = dist

            self.__clusters[index_optim].append(index_point)
            self.__belong[index_point] = index_optim

        # If cluster is not able to capture object it should be removed
        self.__clusters = [cluster for cluster in self.__clusters if len(cluster) > 0]

Response: Updates the list of clusters based on the pointer data.


Instruction:
Make a summary of the following Python 3 code
def __find_another_nearest_medoid(self, point_index, current_medoid_index):
        other_medoid_index = -1
        other_distance_nearest = float('inf')
        for index_medoid in self.__current:
            if (index_medoid != current_medoid_index):
                other_distance_candidate = euclidean_distance_square(self.__pointer_data[point_index], self.__pointer_data[current_medoid_index])

                if other_distance_candidate < other_distance_nearest:
                    other_distance_nearest = other_distance_candidate
                    other_medoid_index = index_medoid

        return other_medoid_index

Response: Finds the nearest medoid for the specified point that is differ from the specified medoid. Returns the index of the nearest medoid.


Instruction:
Can you tell what is the following Python 3 function doing
def __calculate_estimation(self):
        estimation = 0.0
        for index_cluster in range(0, len(self.__clusters)):
            cluster = self.__clusters[index_cluster]
            index_medoid = self.__current[index_cluster]
            for index_point in cluster:
                estimation += euclidean_distance_square(self.__pointer_data[index_point], self.__pointer_data[index_medoid])

        return estimation

Response: Calculates the estimation of the current clusters. The lower the estimation the more optimally configuration of clusters. The more optimally configuration of clusters is more optimally configuration of clusters.


Instruction:
Given the following Python 3 function, write the documentation
def visualize(self, display=True):

        kdnodes = self.__get_nodes()
        level = kdnodes[0]

        for kdnode in kdnodes:
            self.__print_node(level, kdnode)

        self.__tree_text += self.__tree_level_text
        if display is True:
            print(self.__tree_text)

        return self.__tree_text

Response: Display the KD - tree.


Instruction:
How would you code a function in Python 3 to
insert a new point with payload into the kd - tree.

Response:
def insert(self, point, payload):
        """!
        @brief Insert new point with payload to kd-tree.

        @param[in] point (list): Coordinates of the point of inserted node.
        @param[in] payload (any-type): Payload of inserted node. It can be identificator of the node or
                    some useful payload that belongs to the point.

        @return (node) Inserted node to the kd-tree.

        """

        if self.__root is None:
            self.__dimension = len(point)
            self.__root = node(point, payload, None, None, 0)
            self.__point_comparator = self.__create_point_comparator(type(point))
            return self.__root

        cur_node = self.__root

        while True:
            if cur_node.data[cur_node.disc] <= point[cur_node.disc]:
                # If new node is greater or equal than current node then check right leaf
                if cur_node.right is None:
                    discriminator = cur_node.disc + 1
                    if discriminator >= self.__dimension:
                        discriminator = 0

                    cur_node.right = node(point, payload, None, None, discriminator, cur_node)
                    return cur_node.right
                else:
                    cur_node = cur_node.right

            else:
                # If new node is less than current then check left leaf
                if cur_node.left is None:
                    discriminator = cur_node.disc + 1
                    if discriminator >= self.__dimension:
                        discriminator = 0

                    cur_node.left = node(point, payload, None, None, discriminator, cur_node)
                    return cur_node.left
                else:
                    cur_node = cur_node.left


Instruction:
Can you create a Python 3 function that
removes a node from the k - d tree.

Response:
def remove(self, point, **kwargs):
        """!
        @brief Remove specified point from kd-tree.
        @details It removes the first found node that satisfy to the input parameters. Make sure that
                  pair (point, payload) is unique for each node, othewise the first found is removed.

        @param[in] point (list): Coordinates of the point of removed node.
        @param[in] **kwargs: Arbitrary keyword arguments (available arguments: 'payload').

        Keyword Args:

            - payload (any): Payload of the node that should be removed.

        @return (node) Root if node has been successfully removed, otherwise None.

        """

        # Get required node
        node_for_remove = None
        if 'payload' in kwargs:
            node_for_remove = self.find_node_with_payload(point, kwargs['payload'], None)
        else:
            node_for_remove = self.find_node(point, None)

        if node_for_remove is None:
            return None

        parent = node_for_remove.parent
        minimal_node = self.__recursive_remove(node_for_remove)
        if parent is None:
            self.__root = minimal_node

            # If all k-d tree was destroyed
            if minimal_node is not None:
                minimal_node.parent = None
        else:
            if parent.left is node_for_remove:
                parent.left = minimal_node
            elif parent.right is node_for_remove:
                parent.right = minimal_node

        return self.__root


Instruction:
Explain what the following Python 3 code does
def find_minimal_node(self, node_head, discriminator):

        min_key = lambda cur_node: cur_node.data[discriminator]
        stack = []
        candidates = []
        isFinished = False
        while isFinished is False:
            if node_head is not None:
                stack.append(node_head)
                node_head = node_head.left
            else:
                if len(stack) != 0:
                    node_head = stack.pop()
                    candidates.append(node_head)
                    node_head = node_head.right
                else:
                    isFinished = True

        return min(candidates, key = min_key)

Response: Find the minimal node in line with coordinate that is defined by discriminator.


Instruction:
Can you create a Python 3 function that
fills the KD - tree by specified data and create point comparator in line with data type.

Response:
def __fill_tree(self, data_list, payload_list):
        """!
        @brief Fill KD-tree by specified data and create point comparator in line with data type.

        @param[in] data_list (array_like): Data points that should be inserted to the tree.
        @param[in] payload_list (array_like): Data point payloads that follows data points inserted to the tree.

        """
        if data_list is None or len(data_list) == 0:
            return # Just return from here, tree can be filled by insert method later

        if payload_list is None:
            # Case when payload is not specified.
            for index in range(0, len(data_list)):
                self.insert(data_list[index], None)
        else:
            # Case when payload is specified.
            for index in range(0, len(data_list)):
                self.insert(data_list[index], payload_list[index])

        self.__point_comparator = self.__create_point_comparator(type(self.__root.data))


Instruction:
Explain what the following Python 3 code does
def __create_point_comparator(self, type_point):
        if type_point == numpy.ndarray:
            return lambda obj1, obj2: numpy.array_equal(obj1, obj2)

        return lambda obj1, obj2: obj1 == obj2

Response: Create a function that returns a function that compares objects with the point data type.


Instruction:
Write a Python 3 script to
search node that satisfies to input parameters in search rule.

Response:
def __find_node_by_rule(self, point, search_rule, cur_node):
        """!
        @brief Search node that satisfy to parameters in search rule.
        @details If node with specified parameters does not exist then None will be returned,
                  otherwise required node will be returned.

        @param[in] point (list): Coordinates of the point whose node should be found.
        @param[in] search_rule (lambda): Rule that is called to check whether node satisfies to search parameter.
        @param[in] cur_node (node): Node from which search should be started.

        @return (node) Node if it satisfies to input parameters, otherwise it return None.

        """

        req_node = None

        if cur_node is None:
            cur_node = self.__root

        while cur_node:
            if cur_node.data[cur_node.disc] <= point[cur_node.disc]:
                # Check if it's required node
                if search_rule(cur_node):
                    req_node = cur_node
                    break

                cur_node = cur_node.right

            else:
                cur_node = cur_node.left

        return req_node


Instruction:
Given the following Python 3 function, write the documentation
def find_node_with_payload(self, point, point_payload, cur_node = None):

        rule_search = lambda node, point=point, payload=point_payload: self.__point_comparator(node.data, point) and node.payload == payload
        return self.__find_node_by_rule(point, rule_search, cur_node)

Response: Returns a node with the specified coordinates and payload.


Instruction:
Here you have a function in Python 3, explain what it does
def find_node(self, point, cur_node = None):

        rule_search = lambda node, point=point: self.__point_comparator(node.data, point)
        return self.__find_node_by_rule(point, rule_search, cur_node)

Response: Returns a node with coordinates that are defined by the specified point.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def find_nearest_dist_node(self, point, distance, retdistance = False):

        best_nodes = self.find_nearest_dist_nodes(point, distance)

        if best_nodes == []:
            return None

        nearest = min(best_nodes, key = lambda item: item[0])

        if retdistance is True:
            return nearest
        else:
            return nearest[1]

Response: Find nearest neighbor in area with radius = distance.


Instruction:
Here you have a function in Python 3, explain what it does
def find_nearest_dist_nodes(self, point, distance):

        best_nodes = []
        if self.__root is not None:
            self.__recursive_nearest_nodes(point, distance, distance * distance, self.__root, best_nodes)

        return best_nodes

Response: Find nearest neighbors in area that are covered by specified distance from the center of the area.


Instruction:
Make a summary of the following Python 3 code
def __recursive_nearest_nodes(self, point, distance, sqrt_distance, node_head, best_nodes):

        if node_head.right is not None:
            minimum = node_head.data[node_head.disc] - distance
            if point[node_head.disc] >= minimum:
                self.__recursive_nearest_nodes(point, distance, sqrt_distance, node_head.right, best_nodes)

        if node_head.left is not None:
            maximum = node_head.data[node_head.disc] + distance
            if point[node_head.disc] < maximum:
                self.__recursive_nearest_nodes(point, distance, sqrt_distance, node_head.left, best_nodes)

        candidate_distance = euclidean_distance_square(point, node_head.data)
        if candidate_distance <= sqrt_distance:
            best_nodes.append( (candidate_distance, node_head) )

Response:
!
        @brief Returns list of neighbors such as tuple (distance, node) that is located in area that is covered by distance.

        @param[in] point (list): Coordinates that is considered as centroind for searching
        @param[in] distance (double): Distance from the center where seaching is performed.
        @param[in] sqrt_distance (double): Square distance from the center where searching is performed.
        @param[in] node_head (node): Node from that searching is performed.
        @param[in|out] best_nodes (list): List of founded nodes.


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

        if node_parent.left is not None:
            yield node_parent.left
        if node_parent.right is not None:
            yield node_parent.right

Response: Returns a list of children of a node.


Instruction:
Write a Python 3 function for
returning a list of tuples that are the items of the tree that are defined by the node specified by start_node and level.

Response:
def traverse(self, start_node = None, level = None):
        """!
        @brief Traverses all nodes of subtree that is defined by node specified in input parameter.

        @param[in] start_node (node): Node from that travering of subtree is performed.
        @param[in, out] level (uint): Should be ignored by application.

        @return (list) All nodes of the subtree.

        """

        if start_node is None:
            start_node  = self.__root
            level = 0

        if start_node is None:
            return []

        items = [ (level, start_node) ]
        for child in self.children(start_node):
            if child is not None:
                items += self.traverse(child, level + 1)

        return items


Instruction:
Given the following Python 3 function, write the documentation
def __process_by_ccore(self):
        if isinstance(self.__initializer, kmeans_plusplus_initializer):
            initializer = wrapper.elbow_center_initializer.KMEANS_PLUS_PLUS
        else:
            initializer = wrapper.elbow_center_initializer.RANDOM

        result = wrapper.elbow(self.__data, self.__kmin, self.__kmax, initializer)

        self.__kvalue = result[0]
        self.__wce = result[1]

Response: A method that processes the data from the ELBO and stores the result in the object.


Instruction:
Make a summary of the following Python 3 code
def __process_by_python(self):
        for amount in range(self.__kmin, self.__kmax):
            centers = self.__initializer(self.__data, amount).initialize()
            instance = kmeans(self.__data, centers, ccore=True)
            instance.process()

            self.__wce.append(instance.get_total_wce())

        self.__calculate_elbows()
        self.__find_optimal_kvalue()

Response: Performs processing using python implementation.


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

        x0, y0 = 0.0, self.__wce[0]
        x1, y1 = float(len(self.__wce)), self.__wce[-1]

        for index_elbow in range(1, len(self.__wce) - 1):
            x, y = float(index_elbow), self.__wce[index_elbow]

            segment = abs((y0 - y1) * x + (x1 - x0) * y + (x0 * y1 - x1 * y0))
            norm = math.sqrt((x1 - x0) ** 2 + (y1 - y0) ** 2)
            distance = segment / norm

            self.__elbows.append(distance)

Response:
!
        @brief Calculates potential elbows.
        @details Elbow is calculated as a distance from each point (x, y) to segment from kmin-point (x0, y0) to kmax-point (x1, y1).


Instruction:
Explain what the following Python 3 code does
def __find_optimal_kvalue(self):
        optimal_elbow_value = max(self.__elbows)
        self.__kvalue = self.__elbows.index(optimal_elbow_value) + 1 + self.__kmin

Response: Find the optimal k - value for the current set of keys.


Instruction:
Here you have a function in Python 3, explain what it does
def calculate_connvectivity_radius(self, amount_clusters, maximum_iterations = 100):

        maximum_distance = max(self.__ordering)

        upper_distance = maximum_distance
        lower_distance = 0.0

        result = None

        amount, borders = self.extract_cluster_amount(maximum_distance)
        if amount <= amount_clusters:
            for _ in range(maximum_iterations):
                radius = (lower_distance + upper_distance) / 2.0

                amount, borders = self.extract_cluster_amount(radius)
                if amount == amount_clusters:
                    result = radius
                    break

                elif amount == 0:
                    break

                elif amount > amount_clusters:
                    lower_distance = radius

                elif amount < amount_clusters:
                    upper_distance = radius

        return result, borders

Response: Calculates the connectivity radius of allocation specified amount of clusters using the ordering diagram and indexes of values of values of values of ordering diagram.


Instruction:
Explain what the following Python 3 code does
def extract_cluster_amount(self, radius):

        amount_clusters = 1

        cluster_start = False
        cluster_pick = False
        total_similarity = True
        previous_cluster_distance = None
        previous_distance = None

        cluster_borders = []

        for index_ordering in range(len(self.__ordering)):
            distance = self.__ordering[index_ordering]
            if distance >= radius:
                if cluster_start is False:
                    cluster_start = True
                    amount_clusters += 1

                    if index_ordering != 0:
                        cluster_borders.append(index_ordering)

                else:
                    if (distance < previous_cluster_distance) and (cluster_pick is False):
                        cluster_pick = True

                    elif (distance > previous_cluster_distance) and (cluster_pick is True):
                        cluster_pick = False
                        amount_clusters += 1

                        if index_ordering != 0:
                            cluster_borders.append(index_ordering)

                previous_cluster_distance = distance

            else:
                cluster_start = False
                cluster_pick = False

            if (previous_distance is not None) and (distance != previous_distance):
                total_similarity = False

            previous_distance = distance

        if (total_similarity is True) and (previous_distance > radius):
            amount_clusters = 0

        return amount_clusters, cluster_borders

Response: Returns the amount of clusters allocated by the given radius on the ordering diagram.


Instruction:
Explain what the following Python 3 code does
def __process_by_ccore(self):

        (self.__clusters, self.__noise, self.__ordering, self.__eps,
         objects_indexes, objects_core_distances, objects_reachability_distances) = \
            wrapper.optics(self.__sample_pointer, self.__eps, self.__minpts, self.__amount_clusters, self.__data_type)

        self.__optics_objects = []
        for i in range(len(objects_indexes)):
            if objects_core_distances[i] < 0.0:
                objects_core_distances[i] = None

            if objects_reachability_distances[i] < 0.0:
                objects_reachability_distances[i] = None

            optics_object = optics_descriptor(objects_indexes[i], objects_core_distances[i], objects_reachability_distances[i])
            optics_object.processed = True

            self.__optics_objects.append(optics_object)

Response: Processes the optics objects in the cluster by CCORE.


Instruction:
Can you tell what is the following Python 3 function doing
def __initialize(self, sample):

        self.__processed = [False] * len(sample)
        self.__optics_objects = [optics_descriptor(i) for i in range(len(sample))]      # List of OPTICS objects that corresponds to objects from input sample.
        self.__ordered_database = []        # List of OPTICS objects in traverse order.

        self.__clusters = None      # Result of clustering (list of clusters where each cluster contains indexes of objects from input data).
        self.__noise = None

Response: Initializes internal states and resets clustering results in line with input sample.


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

        self.__initialize(self.__sample_pointer)

        for optic_object in self.__optics_objects:
            if optic_object.processed is False:
                self.__expand_cluster_order(optic_object)

        self.__extract_clusters()

Response: Initializes the cluster allocation and builds the cluster ordering diagram.


Instruction:
Explain what the following Python 3 code does
def get_ordering(self):

        if self.__ordering is None:
            self.__ordering = []

            for cluster in self.__clusters:
                for index_object in cluster:
                    optics_object = self.__optics_objects[index_object]
                    if optics_object.reachability_distance is not None:
                        self.__ordering.append(optics_object.reachability_distance)

        return self.__ordering

Response: Returns the internal clustering ordering of the input data - set.


Instruction:
Create a Python 3 function to
create a neighbor searcher in line with data type.

Response:
def __create_neighbor_searcher(self, data_type):
        """!
        @brief Returns neighbor searcher in line with data type.

        @param[in] data_type (string): Data type (points or distance matrix).

        """
        if data_type == 'points':
            return self.__neighbor_indexes_points
        elif data_type == 'distance_matrix':
            return self.__neighbor_indexes_distance_matrix
        else:
            raise TypeError("Unknown type of data is specified '%s'" % data_type)


Instruction:
Explain what the following Python 3 code does
def __expand_cluster_order(self, optics_object):

        optics_object.processed = True

        neighbors_descriptor = self.__neighbor_searcher(optics_object)
        optics_object.reachability_distance = None

        self.__ordered_database.append(optics_object)

        # Check core distance
        if len(neighbors_descriptor) >= self.__minpts:
            neighbors_descriptor.sort(key = lambda obj: obj[1])
            optics_object.core_distance = neighbors_descriptor[self.__minpts - 1][1]

            # Continue processing
            order_seed = list()
            self.__update_order_seed(optics_object, neighbors_descriptor, order_seed)

            while len(order_seed) > 0:
                optic_descriptor = order_seed[0]
                order_seed.remove(optic_descriptor)

                neighbors_descriptor = self.__neighbor_searcher(optic_descriptor)
                optic_descriptor.processed = True

                self.__ordered_database.append(optic_descriptor)

                if len(neighbors_descriptor) >= self.__minpts:
                    neighbors_descriptor.sort(key = lambda obj: obj[1])
                    optic_descriptor.core_distance = neighbors_descriptor[self.__minpts - 1][1]

                    self.__update_order_seed(optic_descriptor, neighbors_descriptor, order_seed)
                else:
                    optic_descriptor.core_distance = None

        else:
            optics_object.core_distance = None

Response: Expand the cluster order from not processed optics - object that corresponds to object from input data.


Instruction:
Write a Python 3 script to
extract clusters and noise from ordered database.

Response:
def __extract_clusters(self):
        """!
        @brief Extract clusters and noise from order database.

        """

        self.__clusters = []
        self.__noise = []

        current_cluster = self.__noise
        for optics_object in self.__ordered_database:
            if (optics_object.reachability_distance is None) or (optics_object.reachability_distance > self.__eps):
                if (optics_object.core_distance is not None) and (optics_object.core_distance <= self.__eps):
                    self.__clusters.append([ optics_object.index_object ])
                    current_cluster = self.__clusters[-1]
                else:
                    self.__noise.append(optics_object.index_object)
            else:
                current_cluster.append(optics_object.index_object)


Instruction:
Write a Python 3 function that can
update the list of reachable objects in the order of the given optics - object.

Response:
def __update_order_seed(self, optic_descriptor, neighbors_descriptors, order_seed):
        """!
        @brief Update sorted list of reachable objects (from core-object) that should be processed using neighbors of core-object.

        @param[in] optic_descriptor (optics_descriptor): Core-object whose neighbors should be analysed.
        @param[in] neighbors_descriptors (list): List of neighbors of core-object.
        @param[in|out] order_seed (list): List of sorted object in line with reachable distance.

        """

        for neighbor_descriptor in neighbors_descriptors:
            index_neighbor = neighbor_descriptor[0]
            current_reachable_distance = neighbor_descriptor[1]

            if self.__optics_objects[index_neighbor].processed is not True:
                reachable_distance = max(current_reachable_distance, optic_descriptor.core_distance)
                if self.__optics_objects[index_neighbor].reachability_distance is None:
                    self.__optics_objects[index_neighbor].reachability_distance = reachable_distance

                    # insert element in queue O(n) - worst case.
                    index_insertion = len(order_seed)
                    for index_seed in range(0, len(order_seed)):
                        if reachable_distance < order_seed[index_seed].reachability_distance:
                            index_insertion = index_seed
                            break

                    order_seed.insert(index_insertion, self.__optics_objects[index_neighbor])

                else:
                    if reachable_distance < self.__optics_objects[index_neighbor].reachability_distance:
                        self.__optics_objects[index_neighbor].reachability_distance = reachable_distance
                        order_seed.sort(key = lambda obj: obj.reachability_distance)


Instruction:
Here you have a function in Python 3, explain what it does
def __neighbor_indexes_points(self, optic_object):
        kdnodes = self.__kdtree.find_nearest_dist_nodes(self.__sample_pointer[optic_object.index_object], self.__eps)
        return [[node_tuple[1].payload, math.sqrt(node_tuple[0])] for node_tuple in kdnodes if
                node_tuple[1].payload != optic_object.index_object]

Response: Returns neighbors of the specified object in case of sequence of points.


Instruction:
How would you code a function in Python 3 to
return neighbors of the specified object in case of distance matrix.

Response:
def __neighbor_indexes_distance_matrix(self, optic_object):
        """!
        @brief Return neighbors of the specified object in case of distance matrix.

        @param[in] optic_object (optics_descriptor): Object for which neighbors should be returned in line with connectivity radius.

        @return (list) List of indexes of neighbors in line the connectivity radius.

        """
        distances = self.__sample_pointer[optic_object.index_object]
        return [[index_neighbor, distances[index_neighbor]] for index_neighbor in range(len(distances))
                if ((distances[index_neighbor] <= self.__eps) and (index_neighbor != optic_object.index_object))]


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

        self.__features = [];

        if (len(self.__tree.leafes) == 1):
            # parameters are too general, copy all entries
            for entry in self.__tree.leafes[0].entries:
                self.__features.append(entry);

        else:
            # copy all leaf clustering features
            for node in self.__tree.leafes:
                self.__features.append(node.feature);

Response: Extract features from CF - tree cluster.


Instruction:
Implement a Python 3 function for
decoding the data from the CF - tree features.

Response:
def __decode_data(self):
        """!
        @brief Decodes data from CF-tree features.

        """

        self.__clusters = [ [] for _ in range(self.__number_clusters) ];
        self.__noise = [];

        for index_point in range(0, len(self.__pointer_data)):
            (_, cluster_index) = self.__get_nearest_feature(self.__pointer_data[index_point], self.__features);

            self.__clusters[cluster_index].append(index_point);


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

        for index_point in range(0, len(self.__pointer_data)):
            point = self.__pointer_data[index_point];
            self.__tree.insert_cluster( [ point ] );

            if (self.__tree.amount_entries > self.__entry_size_limit):
                self.__tree = self.__rebuild_tree(index_point);

Response: Inserts input data into the tree.