Search is not available for this dataset
text
stringlengths
75
104k
def send(self, message_type, task_id, message): """ Sends a message to the UDP receiver Parameter --------- message_type: monitoring.MessageType (enum) In this case message type is RESOURCE_INFO most often task_id: int Task identifier of the task for which resource monitoring is being reported message: object Arbitrary pickle-able object that is to be sent Returns: # bytes sent """ x = 0 try: buffer = pickle.dumps((self.source_id, # Identifier for manager int(time.time()), # epoch timestamp message_type, message)) except Exception as e: print("Exception during pickling {}".format(e)) return try: x = self.sock.sendto(buffer, (self.ip, self.port)) except socket.timeout: print("Could not send message within timeout limit") return False return x
def monitor_wrapper(f, task_id, monitoring_hub_url, run_id, sleep_dur): """ Internal Wrap the Parsl app with a function that will call the monitor function and point it at the correct pid when the task begins. """ def wrapped(*args, **kwargs): p = Process(target=monitor, args=(os.getpid(), task_id, monitoring_hub_url, run_id, sleep_dur)) p.start() try: return f(*args, **kwargs) finally: # There's a chance of zombification if the workers are killed by some signals p.terminate() p.join() return wrapped
def execute_wait(self, cmd, walltime=2, envs={}): ''' Synchronously execute a commandline string on the shell. Args: - cmd (string) : Commandline string to execute - walltime (int) : walltime in seconds Kwargs: - envs (dict) : Dictionary of env variables Returns: - retcode : Return code from the execution, -1 on fail - stdout : stdout string - stderr : stderr string Raises: None. ''' # Execute the command stdin, stdout, stderr = self.ssh_client.exec_command( self.prepend_envs(cmd, envs), bufsize=-1, timeout=walltime ) # Block on exit status from the command exit_status = stdout.channel.recv_exit_status() return exit_status, stdout.read().decode("utf-8"), stderr.read().decode("utf-8")
def execute_no_wait(self, cmd, walltime=2, envs={}): ''' Execute asynchronousely without waiting for exitcode Args: - cmd (string): Commandline string to be executed on the remote side - walltime (int): timeout to exec_command KWargs: - envs (dict): A dictionary of env variables Returns: - None, stdout (readable stream), stderr (readable stream) Raises: - ChannelExecFailed (reason) ''' # Execute the command stdin, stdout, stderr = self.ssh_client.exec_command( self.prepend_envs(cmd, envs), bufsize=-1, timeout=walltime ) return None, stdout, stderr
def push_file(self, local_source, remote_dir): ''' Transport a local file to a directory on a remote machine Args: - local_source (string): Path - remote_dir (string): Remote path Returns: - str: Path to copied file on remote machine Raises: - BadScriptPath : if script path on the remote side is bad - BadPermsScriptPath : You do not have perms to make the channel script dir - FileCopyException : FileCopy failed. ''' remote_dest = remote_dir + '/' + os.path.basename(local_source) try: self.makedirs(remote_dir, exist_ok=True) except IOError as e: logger.exception("Pushing {0} to {1} failed".format(local_source, remote_dir)) if e.errno == 2: raise BadScriptPath(e, self.hostname) elif e.errno == 13: raise BadPermsScriptPath(e, self.hostname) else: logger.exception("File push failed due to SFTP client failure") raise FileCopyException(e, self.hostname) try: self.sftp_client.put(local_source, remote_dest, confirm=True) # Set perm because some systems require the script to be executable self.sftp_client.chmod(remote_dest, 0o777) except Exception as e: logger.exception("File push from local source {} to remote destination {} failed".format( local_source, remote_dest)) raise FileCopyException(e, self.hostname) return remote_dest
def pull_file(self, remote_source, local_dir): ''' Transport file on the remote side to a local directory Args: - remote_source (string): remote_source - local_dir (string): Local directory to copy to Returns: - str: Local path to file Raises: - FileExists : Name collision at local directory. - FileCopyException : FileCopy failed. ''' local_dest = local_dir + '/' + os.path.basename(remote_source) try: os.makedirs(local_dir) except OSError as e: if e.errno != errno.EEXIST: logger.exception("Failed to create script_dir: {0}".format(script_dir)) raise BadScriptPath(e, self.hostname) # Easier to check this than to waste time trying to pull file and # realize there's a problem. if os.path.exists(local_dest): logger.exception("Remote file copy will overwrite a local file:{0}".format(local_dest)) raise FileExists(None, self.hostname, filename=local_dest) try: self.sftp_client.get(remote_source, local_dest) except Exception as e: logger.exception("File pull failed") raise FileCopyException(e, self.hostname) return local_dest
def isdir(self, path): """Return true if the path refers to an existing directory. Parameters ---------- path : str Path of directory on the remote side to check. """ result = True try: self.sftp_client.lstat(path) except FileNotFoundError: result = False return result
def makedirs(self, path, mode=511, exist_ok=False): """Create a directory on the remote side. If intermediate directories do not exist, they will be created. Parameters ---------- path : str Path of directory on the remote side to create. mode : int Permissions (posix-style) for the newly-created directory. exist_ok : bool If False, raise an OSError if the target directory already exists. """ if exist_ok is False and self.isdir(path): raise OSError('Target directory {} already exists'.format(path)) self.execute_wait('mkdir -p {}'.format(path)) self.sftp_client.chmod(path, mode)
def run(self, message): """ This function needs to be fast at the same time aware of the possibility of ZMQ pipes overflowing. The timeout increases slowly if contention is detected on ZMQ pipes. We could set copy=False and get slightly better latency but this results in ZMQ sockets reaching a broken state once there are ~10k tasks in flight. This issue can be magnified if each the serialized buffer itself is larger. """ self.zmq_socket.send_pyobj(message, copy=True) reply = self.zmq_socket.recv_pyobj() return reply
def put(self, message): """ This function needs to be fast at the same time aware of the possibility of ZMQ pipes overflowing. The timeout increases slowly if contention is detected on ZMQ pipes. We could set copy=False and get slightly better latency but this results in ZMQ sockets reaching a broken state once there are ~10k tasks in flight. This issue can be magnified if each the serialized buffer itself is larger. """ timeout_ms = 0 while True: socks = dict(self.poller.poll(timeout=timeout_ms)) if self.zmq_socket in socks and socks[self.zmq_socket] == zmq.POLLOUT: # The copy option adds latency but reduces the risk of ZMQ overflow self.zmq_socket.send_pyobj(message, copy=True) return else: timeout_ms += 1 logger.debug("Not sending due to full zmq pipe, timeout: {} ms".format(timeout_ms))
def _wake_up_timer(self, kill_event): """Internal. This is the function that the thread will execute. waits on an event so that the thread can make a quick exit when close() is called Args: - kill_event (threading.Event) : Event to wait on """ while True: prev = self._wake_up_time # Waiting for the event returns True only when the event # is set, usually by the parent thread time_to_die = kill_event.wait(float(max(prev - time.time(), 0))) if time_to_die: return if prev == self._wake_up_time: self.make_callback(kind='timer') else: print("Sleeping a bit more")
def notify(self, event_id): """Let the FlowControl system know that there is an event.""" self._event_buffer.extend([event_id]) self._event_count += 1 if self._event_count >= self.threshold: logger.debug("Eventcount >= threshold") self.make_callback(kind="event")
def make_callback(self, kind=None): """Makes the callback and resets the timer. KWargs: - kind (str): Default=None, used to pass information on what triggered the callback """ self._wake_up_time = time.time() + self.interval self.callback(tasks=self._event_buffer, kind=kind) self._event_buffer = []
def make_callback(self, kind=None): """Makes the callback and resets the timer. """ self._wake_up_time = time.time() + self.interval self.callback(*self.cb_args)
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])
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
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
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)))
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)))
def initialize_scaling(self): """ Compose the launch command and call the scale_out This should be implemented in the child classes to take care of executor specific oddities. """ 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
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")
def _queue_management_worker(self): """Listen to the queue for task status messages and handle them. Depending on the message, tasks will be updated with results, exceptions, or updates. It expects the following messages: .. code:: python { "task_id" : <task_id> "result" : serialized result object, if task succeeded ... more tags could be added later } { "task_id" : <task_id> "exception" : serialized exception object, on failure } We do not support these yet, but they could be added easily. .. code:: python { "task_id" : <task_id> "cpu_stat" : <> "mem_stat" : <> "io_stat" : <> "started" : tstamp } The `None` message is a die request. """ 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")
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)
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
def _hold_block(self, block_id): """ Sends hold command to all managers which are in a specific block Parameters ---------- block_id : str Block identifier of the block to be put on hold """ 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'])
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
def scale_in(self, blocks=None, block_ids=[]): """Scale in the number of active blocks by specified amount. The scale in method here is very rude. It doesn't give the workers the opportunity to finish current tasks or cleanup. This is tracked in issue #530 Parameters ---------- blocks : int Number of blocks to terminate and scale_in by block_ids : list List of specific block ids to terminate. Optional Raises: NotImplementedError """ 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
def status(self): """Return status of all blocks.""" status = [] if self.provider: status = self.provider.status(self.blocks.values()) return status
def shutdown(self, hub=True, targets='all', block=False): """Shutdown the executor, including all workers and controllers. This is not implemented. Kwargs: - hub (Bool): Whether the hub should be shutdown, Default:True, - targets (list of ints| 'all'): List of block id's to kill, Default:'all' - block (Bool): To block for confirmations or not Raises: NotImplementedError """ 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
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])
def write(self, buf, **kwargs): """ Write the bytes from ``buffer`` to the device. Transmits a stop bit if ``stop`` is set. If ``start`` or ``end`` is provided, then the buffer will be sliced as if ``buffer[start:end]``. This will not cause an allocation like ``buffer[start:end]`` will so it saves memory. :param bytearray buffer: buffer containing the bytes to write :param int start: Index to start writing from :param int end: Index to read up to but not include :param bool stop: If true, output an I2C stop condition after the buffer is written """ self.i2c.writeto(self.device_address, buf, **kwargs) if self._debug: print("i2c_device.write:", [hex(i) for i in buf])
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]])
def read_sample(filename): """! @brief Returns data sample from simple text file. @details This function should be used for text file with following format: @code point_1_coord_1 point_1_coord_2 ... point_1_coord_n point_2_coord_1 point_2_coord_2 ... point_2_coord_n ... ... @endcode @param[in] filename (string): Path to file with data. @return (list) Points where each point represented by list of coordinates. """ 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
def calculate_distance_matrix(sample): """! @brief Calculates distance matrix for data sample (sequence of points) using Euclidean distance as a metric. @param[in] sample (array_like): Data points that are used for distance calculation. @return (list) Matrix distance between data points. """ amount_rows = len(sample); return [ [ euclidean_distance(sample[i], sample[j]) for j in range(amount_rows) ] for i in range(amount_rows) ];
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
def rgb2gray(image_rgb_array): """! @brief Returns image as 1-dimension (gray colored) matrix, where one element of list describes pixel. @details Luma coding is used for transformation and that is calculated directly from gamma-compressed primary intensities as a weighted sum: \f[Y = 0.2989R + 0.587G + 0.114B\f] @param[in] image_rgb_array (list): Image represented by RGB list. @return (list) Image as gray colored matrix, where one element of list describes pixel. @code colored_image = read_image(file_name); gray_image = rgb2gray(colored_image); @endcode @see read_image() """ 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;
def stretch_pattern(image_source): """! @brief Returns stretched content as 1-dimension (gray colored) matrix with size of input image. @param[in] image_source (Image): PIL Image instance. @return (list, Image) Stretched image as gray colored matrix and source image. """ 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);
def gray_pattern_borders(image): """! @brief Returns coordinates of gray image content on the input image. @param[in] image (Image): PIL Image instance that is processed. @return (tuple) Returns coordinates of gray image content as (width_start, height_start, width_end, height_end). """ 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);
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)) );
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'). <b>Keyword Args:</b><br> - 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
def euclidean_distance_square(a, b): """! @brief Calculate square Euclidian distance between vector a and b. @param[in] a (list): The first vector. @param[in] b (list): The second vector. @return (double) Square Euclidian distance between two vectors. """ 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;
def manhattan_distance(a, b): """! @brief Calculate Manhattan distance between vector a and b. @param[in] a (list): The first cluster. @param[in] b (list): The second cluster. @return (double) Manhattan distance between two vectors. """ 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;
def average_inter_cluster_distance(cluster1, cluster2, data = None): """! @brief Calculates average inter-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 where each element can represent index from the data or object itself. @param[in] cluster2 (list): The second cluster where each element can represent index from the data or object itself. @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 inter-cluster distance between two clusters. """ 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;
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
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
def calculate_ellipse_description(covariance, scale = 2.0): """! @brief Calculates description of ellipse using covariance matrix. @param[in] covariance (numpy.array): Covariance matrix for which ellipse area should be calculated. @param[in] scale (float): Scale of the ellipse. @return (float, float, float) Return ellipse description: angle, width, height. """ 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
def data_corners(data, data_filter = None): """! @brief Finds maximum and minimum corner in each dimension of the specified data. @param[in] data (list): List of points that should be analysed. @param[in] data_filter (list): List of indexes of the data that should be analysed, if it is 'None' then whole 'data' is analysed to obtain corners. @return (list) Tuple of two points that corresponds to minimum and maximum corner (min_corner, max_corner). """ 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
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
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);
def extract_number_oscillations(osc_dyn, index = 0, amplitude_threshold = 1.0): """! @brief Extracts number of oscillations of specified oscillator. @param[in] osc_dyn (list): Dynamic of oscillators. @param[in] index (uint): Index of oscillator in dynamic. @param[in] amplitude_threshold (double): Amplitude threshold when oscillation is taken into account, for example, when oscillator amplitude is greater than threshold then oscillation is incremented. @return (uint) Number of oscillations of specified oscillator. """ 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;
def allocate_sync_ensembles(dynamic, tolerance = 0.1, threshold = 1.0, ignore = None): """! @brief Allocate clusters in line with ensembles of synchronous oscillators where each synchronous ensemble corresponds to only one cluster. @param[in] dynamic (dynamic): Dynamic of each oscillator. @param[in] tolerance (double): Maximum error for allocation of synchronous ensemble oscillators. @param[in] threshold (double): Amlitude trigger when spike is taken into account. @param[in] ignore (bool): Set of indexes that shouldn't be taken into account. @return (list) Grours (lists) of indexes of synchronous oscillators, for example, [ [index_osc1, index_osc3], [index_osc2], [index_osc4, index_osc5] ]. """ 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;
def draw_clusters(data, clusters, noise = [], marker_descr = '.', hide_axes = False, axes = None, display_result = True): """! @brief Displays clusters for data in 2D or 3D. @param[in] data (list): Points that are described by coordinates represented. @param[in] clusters (list): Clusters that are represented by lists of indexes where each index corresponds to point in data. @param[in] noise (list): Points that are regarded to noise. @param[in] marker_descr (string): Marker for displaying points. @param[in] hide_axes (bool): If True - axes is not displayed. @param[in] axes (ax) Matplotlib axes where clusters should be drawn, if it is not specified (None) then new plot will be created. @param[in] display_result (bool): If specified then matplotlib axes will be used for drawing and plot will not be shown. @return (ax) Matplotlib axes where drawn clusters are presented. """ # 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;
def draw_dynamics(t, dyn, x_title = None, y_title = None, x_lim = None, y_lim = None, x_labels = True, y_labels = True, separate = False, axes = None): """! @brief Draw dynamics of neurons (oscillators) in the network. @details It draws if matplotlib is not specified (None), othewise it should be performed manually. @param[in] t (list): Values of time (used by x axis). @param[in] dyn (list): Values of output of oscillators (used by y axis). @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] separate (list): Consists of lists of oscillators where each such list consists of oscillator indexes that will be shown on separated stage. @param[in] axes (ax): If specified then matplotlib axes will be used for drawing and plot will not be shown. @return (ax) Axes of matplotlib. """ number_lines = 0; stage_xlim = None; if (x_lim is not None): stage_xlim = x_lim; elif (len(t) > 0): stage_xlim = [0, t[len(t) - 1]]; if ( (isinstance(separate, bool) is True) and (separate is True) ): if (isinstance(dyn[0], list) is True): number_lines = len(dyn[0]); else: number_lines = 1; elif (isinstance(separate, list) is True): number_lines = len(separate); else: number_lines = 1; dysplay_result = False; if (axes is None): dysplay_result = True; (fig, axes) = plt.subplots(number_lines, 1); # Check if we have more than one dynamic if (isinstance(dyn[0], list) is True): num_items = len(dyn[0]); for index in range(0, num_items, 1): y = [item[index] for item in dyn]; if (number_lines > 1): index_stage = -1; # Find required axes for the y if (isinstance(separate, bool) is True): index_stage = index; elif (isinstance(separate, list) is True): for index_group in range(0, len(separate), 1): if (index in separate[index_group]): index_stage = index_group; break; if (index_stage != -1): if (index_stage != number_lines - 1): axes[index_stage].get_xaxis().set_visible(False); axes[index_stage].plot(t, y, 'b-', linewidth = 0.5); set_ax_param(axes[index_stage], x_title, y_title, stage_xlim, y_lim, x_labels, y_labels, True); else: axes.plot(t, y, 'b-', linewidth = 0.5); set_ax_param(axes, x_title, y_title, stage_xlim, y_lim, x_labels, y_labels, True); else: axes.plot(t, dyn, 'b-', linewidth = 0.5); set_ax_param(axes, x_title, y_title, stage_xlim, y_lim, x_labels, y_labels, True); if (dysplay_result is True): plt.show(); return axes;
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);
def draw_dynamics_set(dynamics, xtitle = None, ytitle = None, xlim = None, ylim = None, xlabels = False, ylabels = False): """! @brief Draw lists of dynamics of neurons (oscillators) in the network. @param[in] dynamics (list): List of network outputs that are represented by values of output of oscillators (used by y axis). @param[in] xtitle (string): Title for Y. @param[in] ytitle (string): Title for X. @param[in] xlim (double): X limit. @param[in] ylim (double): Y limit. @param[in] xlabels (bool): If True - shows X labels. @param[in] ylabels (bool): If True - shows Y labels. """ # 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();
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();
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();
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;
def square_sum(list_vector): """! @brief Calculates square 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 (double) Square sum of 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;
def list_math_subtraction(a, b): """! @brief Calculates subtraction of two lists. @details Each element from list 'a' is subtracted by element from list 'b' accordingly. @param[in] a (list): List of elements that supports mathematical subtraction. @param[in] b (list): List of elements that supports mathematical subtraction. @return (list) Results of subtraction of two lists. """ return [a[i] - b[i] for i in range(len(a))];
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))];
def list_math_addition(a, b): """! @brief Addition of two lists. @details Each element from list 'a' is added to element from list 'b' accordingly. @param[in] a (list): List of elements that supports mathematic addition.. @param[in] b (list): List of elements that supports mathematic addition.. @return (list) Results of addtion of two lists. """ return [a[i] + b[i] for i in range(len(a))];
def list_math_addition_number(a, b): """! @brief Addition between list and number. @details Each element from list 'a' is added to number 'b'. @param[in] a (list): List of elements that supports mathematic addition. @param[in] b (double): Value that supports mathematic addition. @return (list) Result of addtion of two lists. """ return [a[i] + b for i in range(len(a))];
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))];
def list_math_division(a, b): """! @brief Division of two lists. @details Each element from list 'a' is divided by element from list 'b' accordingly. @param[in] a (list): List of elements that supports mathematic division. @param[in] b (list): List of elements that supports mathematic division. @return (list) Result of division of two lists. """ return [a[i] / b[i] for i in range(len(a))];
def list_math_multiplication_number(a, b): """! @brief Multiplication between list and number. @details Each element from list 'a' is multiplied by number 'b'. @param[in] a (list): List of elements that supports mathematic division. @param[in] b (double): Number that supports mathematic division. @return (list) Result of division between list and number. """ return [a[i] * b for i in range(len(a))];
def list_math_multiplication(a, b): """! @brief Multiplication of two lists. @details Each element from list 'a' is multiplied by element from list 'b' accordingly. @param[in] a (list): List of elements that supports mathematic multiplication. @param[in] b (list): List of elements that supports mathematic multiplication. @return (list) Result of multiplication of elements in two lists. """ return [a[i] * b[i] for i in range(len(a))];
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]);
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);
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);
def __create_list_bidir_connections(self): """! @brief Creates network as bidirectional list. @details Each oscillator may be conneted with two neighbors in line with classical list structure: right, left. """ 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);
def __create_none_connections(self): """! @brief Creates network without connections. """ 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)];
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');
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");
def set_connection(self, i, j): """! @brief Couples two specified oscillators in the network with dynamic connections. @param[in] i (uint): index of an oscillator that should be coupled with oscillator 'j' in the network. @param[in] j (uint): index of an oscillator that should be coupled with oscillator 'i' in the network. @note This method can be used only in case of DYNAMIC connections, otherwise it throws expection. """ 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);
def get_neighbors(self, index): """! @brief Finds neighbors of the oscillator with specified index. @param[in] index (uint): index of oscillator for which neighbors should be found in the network. @return (list) Indexes of neighbors of the specified oscillator. """ 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");
def process(self): """! @brief Performs cluster analysis in line with rules of CLARANS algorithm. @see get_clusters() @see get_medoids() """ 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)
def __update_clusters(self, medoids): """! @brief Forms cluster in line with specified medoids by calculation distance from each point to 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]
def __optimize_configuration(self): """! @brief Finds quasi-optimal medoids and updates in line with them clusters in line with algorithm's rules. """ index_neighbor = 0 while (index_neighbor < self.__maxneighbor): # get random current medoid that is to be replaced current_medoid_index = self.__current[random.randint(0, self.__number_clusters - 1)] current_medoid_cluster_index = self.__belong[current_medoid_index] # get new candidate to be medoid candidate_medoid_index = random.randint(0, len(self.__pointer_data) - 1) while candidate_medoid_index in self.__current: candidate_medoid_index = random.randint(0, len(self.__pointer_data) - 1) candidate_cost = 0.0 for point_index in range(0, len(self.__pointer_data)): if point_index not in self.__current: # get non-medoid point and its medoid point_cluster_index = self.__belong[point_index] point_medoid_index = self.__current[point_cluster_index] # get other medoid that is nearest to the point (except current and candidate) other_medoid_index = self.__find_another_nearest_medoid(point_index, current_medoid_index) other_medoid_cluster_index = self.__belong[other_medoid_index] # for optimization calculate all required distances # from the point to current medoid distance_current = euclidean_distance_square(self.__pointer_data[point_index], self.__pointer_data[current_medoid_index]) # from the point to candidate median distance_candidate = euclidean_distance_square(self.__pointer_data[point_index], self.__pointer_data[candidate_medoid_index]) # from the point to nearest (own) medoid distance_nearest = float('inf') if ( (point_medoid_index != candidate_medoid_index) and (point_medoid_index != current_medoid_cluster_index) ): distance_nearest = euclidean_distance_square(self.__pointer_data[point_index], self.__pointer_data[point_medoid_index]) # apply rules for cost calculation if (point_cluster_index == current_medoid_cluster_index): # case 1: if (distance_candidate >= distance_nearest): candidate_cost += distance_nearest - distance_current # case 2: else: candidate_cost += distance_candidate - distance_current elif (point_cluster_index == other_medoid_cluster_index): # case 3 ('nearest medoid' is the representative object of that cluster and object is more similar to 'nearest' than to 'candidate'): if (distance_candidate > distance_nearest): pass; # case 4: else: candidate_cost += distance_candidate - distance_nearest if (candidate_cost < 0): # set candidate that has won self.__current[current_medoid_cluster_index] = candidate_medoid_index # recalculate clusters self.__update_clusters(self.__current) # reset iterations and starts investigation from the begining index_neighbor = 0 else: index_neighbor += 1
def __find_another_nearest_medoid(self, point_index, current_medoid_index): """! @brief Finds the another nearest medoid for the specified point that is differ from the specified medoid. @param[in] point_index: index of point in dataspace for that searching of medoid in current list of medoids is perfomed. @param[in] current_medoid_index: index of medoid that shouldn't be considered as a nearest. @return (uint) index of the another nearest medoid for the point. """ 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
def __calculate_estimation(self): """! @brief Calculates estimation (cost) of the current clusters. The lower the estimation, the more optimally configuration of clusters. @return (double) estimation of current clusters. """ 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
def thirteen_oscillator_three_stimulated_ensembles_list(): "Good example of three synchronous ensembels" "Not accurate due to false skipes are observed" parameters = legion_parameters(); parameters.Wt = 4.0; parameters.fi = 10.0; template_dynamic_legion(15, 1000, 1000, conn_type = conn_type.LIST_BIDIR, stimulus = [1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1], params = parameters, separate_repr = [ [0, 1, 2], [3, 4, 5, 9, 10], [6, 7, 8], [11, 12, 13, 14] ]);
def thirteen_simplify_oscillator_three_stimulated_ensembles_list(): "Good example of three synchronous ensembels" "Not accurate due to false skipes are observed" parameters = legion_parameters(); parameters.Wt = 4.0; parameters.fi = 0.8; parameters.ENABLE_POTENTIONAL = False; template_dynamic_legion(15, 1000, 1000, conn_type = conn_type.LIST_BIDIR, stimulus = [1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1], params = parameters, separate_repr = [ [0, 1, 2], [3, 4, 5, 9, 10], [6, 7, 8], [11, 12, 13, 14] ]);
def sixteen_oscillator_two_stimulated_ensembles_grid(): "Not accurate false due to spikes are observed" parameters = legion_parameters(); parameters.teta_x = -1.1; template_dynamic_legion(16, 2000, 1500, conn_type = conn_type.GRID_FOUR, params = parameters, stimulus = [1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1]);
def simple_segmentation_example(): "Perfect results!" parameters = legion_parameters(); parameters.eps = 0.02; parameters.alpha = 0.005; parameters.betta = 0.1; parameters.gamma = 7.0; parameters.teta = 0.9; parameters.lamda = 0.1; parameters.teta_x = -0.5; parameters.teta_p = 7.0; parameters.Wz = 0.7; parameters.mu = 0.01; parameters.fi = 3.0; parameters.teta_xz = 0.1; parameters.teta_zx = 0.1; parameters.ENABLE_POTENTIONAL = False; template_dynamic_legion(81, 2500, 2500, conn_type = conn_type.GRID_FOUR, params = parameters, stimulus = [1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0], separate_repr = [ [0, 1, 2, 9, 10, 11, 18, 19, 20], [14, 15, 16, 17, 23, 24, 25, 26, 33, 34, 35, 42, 43, 44, 51, 52, 53], [45, 46, 47, 48, 54, 55, 56, 57, 63, 64, 65, 66, 72, 73, 74, 75] ]);
def visualize(self, display=True): """! @brief Display KD-tree to console. @param[in] display (bool): If 'True' then tree will be shown in console. @return (string) Text representation of the KD-tree. """ 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
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
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'). <b>Keyword Args:</b><br> - 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
def __recursive_remove(self, node_removed): """! @brief Delete node and return root of subtree. @param[in] node_removed (node): Node that should be removed. @return (node) Minimal node in line with coordinate that is defined by descriminator. """ # Check if it is leaf if (node_removed.right is None) and (node_removed.left is None): return None discriminator = node_removed.disc # Check if only left branch exist if node_removed.right is None: node_removed.right = node_removed.left node_removed.left = None # Find minimal node in line with coordinate that is defined by discriminator minimal_node = self.find_minimal_node(node_removed.right, discriminator) parent = minimal_node.parent if parent.left is minimal_node: parent.left = self.__recursive_remove(minimal_node) elif parent.right is minimal_node: parent.right = self.__recursive_remove(minimal_node) minimal_node.parent = node_removed.parent minimal_node.disc = node_removed.disc minimal_node.right = node_removed.right minimal_node.left = node_removed.left # Update parent for successors of previous parent. if minimal_node.right is not None: minimal_node.right.parent = minimal_node if minimal_node.left is not None: minimal_node.left.parent = minimal_node return minimal_node
def find_minimal_node(self, node_head, discriminator): """! @brief Find minimal node in line with coordinate that is defined by discriminator. @param[in] node_head (node): Node of KD tree from that search should be started. @param[in] discriminator (uint): Coordinate number that is used for comparison. @return (node) Minimal node in line with descriminator from the specified node. """ 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)
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))
def __create_point_comparator(self, type_point): """! @brief Create point comparator. @details In case of numpy.array specific comparator is required. @param[in] type_point (data_type): Type of point that is stored in KD-node. @return (callable) Callable point comparator to compare to points. """ if type_point == numpy.ndarray: return lambda obj1, obj2: numpy.array_equal(obj1, obj2) return lambda obj1, obj2: obj1 == obj2
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
def find_node_with_payload(self, point, point_payload, cur_node = None): """! @brief Find node with specified coordinates and payload. @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] point_payload (any): Payload of the node that is searched in the tree. @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. """ 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)
def find_node(self, point, cur_node = None): """! @brief Find node with coordinates that are defined by specified point. @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] cur_node (node): Node from which search should be started. @return (node) Node if it satisfies to input parameters, otherwise it return None. """ rule_search = lambda node, point=point: self.__point_comparator(node.data, point) return self.__find_node_by_rule(point, rule_search, cur_node)
def find_nearest_dist_node(self, point, distance, retdistance = False): """! @brief Find nearest neighbor in area with radius = distance. @param[in] point (list): Maximum distance where neighbors are searched. @param[in] distance (double): Maximum distance where neighbors are searched. @param[in] retdistance (bool): If True - returns neighbors with distances to them, otherwise only neighbors is returned. @return (node|list) Nearest neighbor if 'retdistance' is False and list with two elements [node, distance] if 'retdistance' is True, where the first element is pointer to node and the second element is distance to it. """ 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]
def find_nearest_dist_nodes(self, point, distance): """! @brief Find neighbors that are located in area that is covered by specified 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. @return (list) Neighbors in area that is specified by point (center) and distance (radius). """ best_nodes = [] if self.__root is not None: self.__recursive_nearest_nodes(point, distance, distance * distance, self.__root, best_nodes) return best_nodes
def __recursive_nearest_nodes(self, point, distance, sqrt_distance, node_head, best_nodes): """! @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. """ 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) )
def children(self, node_parent): """! @brief Returns list of children of node. @param[in] node_parent (node): Node whose children are required. @return (list) Children of node. If node haven't got any child then None is returned. """ if node_parent.left is not None: yield node_parent.left if node_parent.right is not None: yield node_parent.right
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