text:
import numpy as np
from numpy import newaxis as na
from ssmtoybox.mtran import LinearizationTransform, FullySymmetricStudentTransform
from ssmtoybox.bq.bqmtran import GaussianProcessTransform, StudentTProcessTransform, BQTransform
from ssmtoybox.bq.bqkern import RBFStudent
from ssmtoybox.ssmod import TransitionModel, MeasurementModel, UNGMTransition, UNGMMeasurement
from ssmtoybox.ssinf import StudentianInference
from ssmtoybox.utils import log_cred_ratio, mse_matrix, gauss_mixture, multivariate_t, RandomVariable, GaussRV, \
    StudentRV

# Gaussian mixture random variable
class GaussianMixtureRV(RandomVariable):

    def __init__(self, dim, means, covs, alphas):
        if len(means) != len(covs) != len(alphas):
            raise ValueError('Same number of means, covariances and mixture weights needs to be supplied!')

        if not np.isclose(np.sum(alphas), 1.0):
            ValueError('Mixture weights must sum to unity!')

        self.dim = dim
        self.means = means
        self.covs = covs
        self.alphas = alphas

    def sample(self, size):
        return np.moveaxis(gauss_mixture(self.means, self.covs, self.alphas, size), -1, 0)

    def get_stats(self):
        return self.means, self.covs, self.alphas

# Student's t-filters
class ExtendedStudent(StudentianInference):

    def __init__(self, dyn, obs, dof=4.0, fixed_dof=True):
        tf = LinearizationTransform(dyn.dim_in)
        th = LinearizationTransform(obs.dim_in)
        super(ExtendedStudent, self).__init__(dyn, obs, tf, th, dof, fixed_dof)

class GPQStudent(StudentianInference):

    def __init__(self, dyn, obs, kern_par_dyn, kern_par_obs, point_hyp=None, dof=4.0, fixed_dof=True):
        """
        Student filter with Gaussian Process quadrature moment transforms using fully-symmetric sigma-point set.

        Parameters
        ----------
        dyn : TransitionModel

        obs : MeasurementModel

        kern_par_dyn : numpy.ndarray
            Kernel parameters for the GPQ moment transform of the dynamics.

        kern_par_obs : numpy.ndarray
            Kernel parameters for the GPQ moment transform of the measurement function.

        point_hyp : dict
            Point set parameters with keys:
              * `'degree'`: Degree (order) of the quadrature rule.
              * `'kappa'`: Tuning parameter of controlling spread of sigma-points around the center.

        dof : float
            Desired degree of freedom for the filtered density.

        fixed_dof : bool
            If `True`, DOF will be fixed for all time steps, which preserves the heavy-tailed behaviour of the filter.
            If `False`, DOF will be increasing after each measurement update, which means the heavy-tailed behaviour is
            not preserved and therefore converges to a Gaussian filter.
        """

        # degrees of freedom for SSM noises
        _, _, q_dof = dyn.noise_rv.get_stats()
        _, _, r_dof = obs.noise_rv.get_stats()

        # add DOF of the noises to the sigma-point parameters
        if point_hyp is None:
                point_hyp = dict()
        point_hyp_dyn = point_hyp
        point_hyp_obs = point_hyp
        point_hyp_dyn.update({'dof': q_dof})
        point_hyp_obs.update({'dof': r_dof})

        # init moment transforms
        t_dyn = GaussianProcessTransform(dyn.dim_in, kern_par_dyn, 'rbf-student', 'fs', point_hyp_dyn)
        t_obs = GaussianProcessTransform(obs.dim_in, kern_par_obs, 'rbf-student', 'fs', point_hyp_obs)
        super(GPQStudent, self).__init__(dyn, obs, t_dyn, t_obs, dof, fixed_dof)

class FSQStudent(StudentianInference):
    """Filter based on fully symmetric quadrature rules."""

    def __init__(self, dyn, obs, degree=3, kappa=None, dof=4.0, fixed_dof=True):

        # degrees of freedom for SSM noises
        _, _, q_dof = dyn.noise_rv.get_stats()
        _, _, r_dof = obs.noise_rv.get_stats()

        # init moment transforms
        t_dyn = FullySymmetricStudentTransform(dyn.dim_in, degree, kappa, q_dof)
        t_obs = FullySymmetricStudentTransform(obs.dim_in, degree, kappa, r_dof)
        super(FSQStudent, self).__init__(dyn, obs, t_dyn, t_obs, dof, fixed_dof)

def rbf_student_mc_weights(x, kern, num_samples, num_batch):
    # MC approximated BQ weights using RBF kernel and Student density
    # MC computed by batches, because without batches we would run out of memory for large sample sizes

    assert isinstance(kern, RBFStudent)
    # kernel parameters and input dimensionality
    par = kern.par
    dim, num_pts = x.shape

    # inverse kernel matrix
    iK = kern.eval_inv_dot(kern.par, x, scaling=False)
    mean, scale, dof = np.zeros((dim, )), np.eye(dim), kern.dof

    # compute MC estimates by batches
    num_samples_batch = num_samples // num_batch
    q_batch = np.zeros((num_pts, num_batch, ))
    Q_batch = np.zeros((num_pts, num_pts, num_batch))
    R_batch = np.zeros((dim, num_pts, num_batch))
    for ib in range(num_batch):

        # multivariate t samples
        x_samples = multivariate_t(mean, scale, dof, num_samples_batch).T

        # evaluate kernel
        k_samples = kern.eval(par, x_samples, x, scaling=False)
        kk_samples = k_samples[:, na, :] * k_samples[..., na]
        xk_samples = x_samples[..., na] * k_samples[na, ...]

        # intermediate sums
        q_batch[..., ib] = k_samples.sum(axis=0)
        Q_batch[..., ib] = kk_samples.sum(axis=0)
        R_batch[..., ib] = xk_samples.sum(axis=1)

    # MC approximations == sum the sums divide by num_samples
    c = 1/num_samples
    q = c * q_batch.sum(axis=-1)
    Q = c * Q_batch.sum(axis=-1)
    R = c * R_batch.sum(axis=-1)

    # BQ moment transform weights
    wm = q.dot(iK)
    wc = iK.dot(Q).dot(iK)
    wcc = R.dot(iK)
    return wm, wc, wcc, Q

def eval_perf_scores(x, mf, Pf):
    xD, steps, mc_sims, num_filt = mf.shape

    # average RMSE over simulations
    rmse = np.sqrt(((x[..., na] - mf) ** 2).sum(axis=0))
    rmse_avg = rmse.mean(axis=1)

    reg = 1e-6 * np.eye(xD)

    # average inclination indicator over simulations
    lcr = np.empty((steps, mc_sims, num_filt))
    for f in range(num_filt):
        for k in range(steps):
            mse = mse_matrix(x[:, k, :], mf[:, k, :, f]) + reg
            for imc in range(mc_sims):
                lcr[k, imc, f] = log_cred_ratio(x[:, k, imc], mf[:, k, imc, f], Pf[..., k, imc, f], mse)
    lcr_avg = lcr.mean(axis=1)

    return rmse_avg, lcr_avg

def run_filters(filters, z):
    num_filt = len(filters)
    zD, steps, mc_sims = z.shape
    xD = filters[0].mod_dyn.dim_state

    # init space for filtered mean and covariance
    mf = np.zeros((xD, steps, mc_sims, num_filt))
    Pf = np.zeros((xD, xD, steps, mc_sims, num_filt))

    # run filters
    for i, f in enumerate(filters):
        print('Running {} ...'.format(f.__class__.__name__))
        for imc in range(mc_sims):
            mf[..., imc, i], Pf[..., imc, i] = f.forward_pass(z[..., imc])
            f.reset()

    # return filtered mean and covariance
    return mf, Pf

max_stars_repo_name: jacobnzw/SSMToybox

max_issues_repo_name: jacobnzw/SSMToybox

max_forks_repo_name: jacobnzw/SSMToybox

reason: import numpy,from numpy


text:
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
# vi: set ft=python sts=4 ts=4 sw=4 et:
"""The image module provides basic functions for working with images in nipy.
Functions are provided to load, save and create image objects, along with
iterators to easily slice through volumes.

    load : load an image from a file

    save : save an image to a file

    fromarray : create an image from a numpy array

Examples
--------
See documentation for load and save functions for 'working' examples.

"""

import os

import numpy as np

import nibabel as nib

from nipy.core.api import Image, is_image
from .nifti_ref import (ni_affine_pixdim_from_affine, affine_transform_from_array)

def load(filename):
    """Load an image from the given filename.

    Parameters
    ----------
    filename : string
        Should resolve to a complete filename path.

    Returns
    -------
    image : An `Image` object
        If successful, a new `Image` object is returned.

    See Also
    --------
    save_image : function for saving images
    fromarray : function for creating images from numpy arrays

    Examples
    --------
    >>> from nipy.io.api import load_image
    >>> from nipy.testing import anatfile
    >>> img = load_image(anatfile)
    >>> img.shape
    (33, 41, 25)
    """
    img = nib.load(filename)
    aff = img.get_affine()
    hdr = img.get_header()
    # If the header implements it, get a list of names, one per axis,
    # and put this into the coordinate map.  In fact, no image format
    # implements this at the moment, so in practice, the following code
    # is not currently called.
    axis_renames = {}
    try:
        axis_names = hdr.axis_names
    except AttributeError:
        pass
    else:
        # axis_renames is a dictionary: dict([(int, str)]) that has keys
        # in range(3). The axes of the Image are renamed from 'ijk' using
        # these names
        for i in range(min([len(axis_names), 3])):
            name = axis_names[i]
            if not (name is None or name == ''):
                axis_renames[i] = name
    zooms = hdr.get_zooms()
    # affine_transform is a 3-d transform
    affine_transform3d, affine_transform = \
        affine_transform_from_array(aff, 'ijk', pixdim=zooms[3:])
    img = Image(img.get_data(), affine_transform.renamed_domain(axis_renames),
                metadata={'header': hdr})
    return img

def save(img, filename, dtype=None):
    """Write the image to a file.

    Parameters
    ----------
    img : An `Image` object
    filename : string
        Should be a valid filename.

    Returns
    -------
    image : An `Image` object

    See Also
    --------
    load_image : function for loading images
    fromarray : function for creating images from numpy arrays

    Examples
    --------
    Make a temporary directory to store files

    >>> import os
    >>> from tempfile import mkdtemp
    >>> tmpdir = mkdtemp()

    Make some some files and save them

    >>> import numpy as np
    >>> from nipy.core.api import fromarray
    >>> from nipy.io.api import save_image
    >>> data = np.zeros((91,109,91), dtype=np.uint8)
    >>> img = fromarray(data, 'kji', 'zxy')
    >>> fname1 = os.path.join(tmpdir, 'img1.nii.gz')
    >>> saved_img1 = save_image(img, fname1)
    >>> saved_img1.shape
    (91, 109, 91)
    >>> fname2 = os.path.join(tmpdir, 'img2.img.gz')
    >>> saved_img2 = save_image(img, fname2)
    >>> saved_img2.shape
    (91, 109, 91)
    >>> fname = 'test.mnc'
    >>> saved_image3 = save_image(img, fname)
    Traceback (most recent call last):
       ...
    ValueError: Cannot save file type "minc"

    Finally, we clear up our temporary files:

    >>> import shutil
    >>> shutil.rmtree(tmpdir)

    Notes
    -----
    Filetype is determined by the file extension in 'filename'.  Currently the
    following filetypes are supported:

    * Nifti single file : ['.nii', '.nii.gz']
    * Nifti file pair : ['.hdr', '.hdr.gz']
    * Analyze file pair : ['.img', 'img.gz']
    """
    # Get header from image
    try:
        original_hdr = img.header
    except AttributeError:
        original_hdr = None
    # Make NIFTI compatible affine_transform
    affine_3dorless_transform, pixdim = ni_affine_pixdim_from_affine(img.coordmap)

    # what are we going to do with pixdim?
    # LPIImage will all have pixdim[3:] == 1...

    aff = affine_3dorless_transform.affine

    # rzs = Fimg.affine[:3,:], JT for Matthew, I changed this below is this correct?
    rzs = img.coordmap.affine[:-1,:-1]
    zooms = np.sqrt(np.sum(rzs * rzs, axis=0))

    ftype = _type_from_filename(filename)
    if ftype.startswith('nifti1'):
        klass = nib.Nifti1Image
    elif ftype == 'analyze':
        klass = nib.Spm2AnalyzeImage
    else:
        raise ValueError('Cannot save file type "%s"' % ftype)
    # make new image
    out_img = klass(data=img.get_data(),
                    affine=aff,
                    header=original_hdr)
    hdr = out_img.get_header()
    # work out phase, freqency, slice from coordmap names
    axisnames = affine_3dorless_transform.function_domain.coord_names

    # let the hdr do what it wants from the axisnames
    try:
        hdr.set_dim_info_from_names(axisnames)
    except AttributeError:
        pass
    # Set zooms
    hdr.set_zooms(zooms)
    # save to disk
    out_img.to_filespec(filename)
    return img

def _type_from_filename(filename):
    ''' Return image type determined from filename

    Filetype is determined by the file extension in 'filename'.
    Currently the following filetypes are supported:

    * Nifti single file : ['.nii', '.nii.gz']
    * Nifti file pair : ['.hdr', '.hdr.gz']
    * Analyze file pair : ['.img', '.img.gz']

    >>> _type_from_filename('test.nii')
    'nifti1single'
    >>> _type_from_filename('test')
    'nifti1single'
    >>> _type_from_filename('test.hdr')
    'nifti1pair'
    >>> _type_from_filename('test.hdr.gz')
    'nifti1pair'
    >>> _type_from_filename('test.img.gz')
    'analyze'
    >>> _type_from_filename('test.mnc')
    'minc'
    '''
    if filename.endswith('.gz'):
        filename = filename[:-3]
    elif filename.endswith('.bz2'):
        filename = filename[:-4]
    _, ext = os.path.splitext(filename)
    if ext in ('', '.nii'):
        return 'nifti1single'
    if ext == '.hdr':
        return 'nifti1pair'
    if ext == '.img':
        return 'analyze'
    if ext == '.mnc':
        return 'minc'
    raise ValueError('Strange file extension "%s"' % ext)

def as_image(image_input):
    ''' Load image from filename or pass through image instance

    Parameters
    ----------
    image_input : str or Image instance
       image or string filename of image.  If a string, load image and
       return.  If an image, pass through without modification

    Returns
    -------
    img : Image or Image-like instance
       Input object if `image_input` seemed to be an image, loaded Image
       object if `image_input` was a string.

    Raises
    ------
    TypeError : if neither string nor image-like passed

    Examples
    --------
    >>> from nipy.testing import anatfile
    >>> from nipy.io.api import load_image
    >>> img = as_image(anatfile)
    >>> img2 = as_image(img)
    >>> img2 is img
    True
    '''
    if is_image(image_input):
        return image_input
    if isinstance(image_input, basestring):
        return load(image_input)
    raise TypeError('Expecting an image-like object or filename string')

ext: py

max_stars_repo_name: neurospin/nipy

max_issues_repo_name: neurospin/nipy

max_forks_repo_name: neurospin/nipy


text:
import json
import numpy as np
import pandas as pd

from typing import Union, Tuple

def get_velocity(
        status: pd.Series
) -> float:
    """
    Get average velocity of vehicle in one scene
    Args:
        status (pd.Series): series status of vehicle in scene

    Returns:
        (float): average velocity of vehicle in scene

    """
    # get average velocity
    avg_vel = list()
    for _row in status:
        avg_vel.append(json.loads(_row)["velocity"])
    avg_vel = np.mean(avg_vel)
    return float(avg_vel)

def get_turning(
        heading: pd.Series,
        avg_vel: float
) -> str:
    """
    Get turning direction of vehicle
    Args:
        heading (pd.Series): series heading of vehicle in scene
        avg_vel (float): average velocity of this vehicle in scene

    Returns:
        (str): could be "left". "right", "stay", or "straight"
    """
    # convert to numpy
    heading_np = heading.to_numpy()

    heading_diff = np.diff(heading_np + np.pi)
    heading_sum_diff = np.sum(heading_diff)
    heading_sum_diff = (heading_sum_diff
                        if abs(heading_sum_diff) > 1e-2
                        else 0)

    if heading_sum_diff > 0:
        return "left"
    elif heading_sum_diff < 0:
        return "right"
    else:
        if abs(avg_vel) < 1:
            return "stay"
        else:
            return "straight"

reason: import numpy


text:
import matplotlib.pyplot as plt
import numpy as np
import os

from collection import ColorCollection

# create base folders
base_folder = '/home/markus/Git/BT/Experiments/MountainCar-v0/'
infixes = ['dropout', 'zoneout', 'shakeout']
#merge_folders = ['ddqn_reg_{}_15_0', 'ddqn_reg_{}_15_0','ddqn_reg_{}_15_0']
merge_folders = ['ddqn_reg_{}_ucb_15_8', 'ddqn_reg_{}_ucb_15_8','ddqn_reg_{}_ucb_15_8']
#merge_folders = ['ddqn_{}_15_1', 'ddqn_{}_15_2', 'ddqn_{}_15_1','ddqn_{}_15_2']
#merge_folders = ['ddqn_{}_15_1', 'ddqn_{}_15_2']
W = 50
show = False
running_avg = True
max_episode = 2500 - int(running_avg) * 2 * W
sigma = 15
exp_decay = 0.99
use_kernel = 'exp'
colors = [  #['#af11a5', '#af11a580'],  # lila
            ['#ff1000', '#ff230080'],  # red
            ['#007d1c', '#00b35880'],  # green
            ['#1142aa', '#057d9f80'],  # blue
            ['#ffe500', '#ffe50080'],  # yellow
            ['#ff8100', '#ff810080'],  # orange
            ['#4d10a3', '#4d10a380']]  # lila
pt_label_offset = 6
shift = 320
indices = None

out_folder = '/home/markus/Git/BT/Thesis/img/Evaluations/mountaincar'
file_name = 'mc_ucb_reg_ens_all_15.eps'

# create figure
fig = plt.figure(dpi=300)
plt.clf()
#ratio = 3.2
ratio = 2.5
x = 8.26 # full width
x = 0.8 * x
o = 6
pt_height = 0.76 * (x / ratio) * 72 # to points
fig.set_size_inches(x, x / ratio)

best_rewards = np.ones([len(infixes)]) * -201
if np.ndim(best_rewards) == 0: best_rewards = np.expand_dims(best_rewards, 0)
eff_indices = (indices if indices is not None else range(len(infixes)))
for i in eff_indices:
    infix = infixes[i]
    run = merge_folders[i].format(infix)

    comp_path = os.path.join(base_folder, run, "rewards.npy")
    rewards = np.load(comp_path)
    mean_rewards = np.mean(rewards, axis=0)
    var_rewards = np.var(rewards, axis=0)
    offset = 1.96 * np.sqrt(var_rewards / len(rewards))

    N = 2 * W + 1
    dx = max_episode/N
    gx = np.arange(-W, W + 1)
    steps = W - np.arange(0, N)

    if use_kernel == 'gaussian':
        kernel = np.exp(-(gx/sigma)**2/2)
    elif use_kernel == 'exp':
        kernel = exp_decay ** steps
    elif use_kernel == 'uniform':
        kernel = np.ones((N,))

    kernel[W+1:] = 0
    kernel = kernel / np.sum(kernel)
    #running_avg_mean_rewards = np.convolve(mean_rewards, np.ones((N,)) / N, mode='valid')
    #running_avg_var_rewards = np.convolve(var_rewards, np.ones((N,)) / N ** 2, mode='valid')

    running_avg_rewards = np.empty([len(rewards), max_episode])
    for k in range(len(rewards)):
        running_avg_rewards[k] = np.convolve(rewards[k], kernel, mode='valid')

    running_avg_mean_rewards = np.mean(running_avg_rewards, axis=0)
    running_avg_var_rewards = np.var(running_avg_rewards, axis=0)
    running_avg_offset = 1.96 * np.sqrt(running_avg_var_rewards / len(running_avg_var_rewards))

    print("{} has max {}".format(infixes[i], np.max(rewards)))
    print("{} has min {}".format(infixes[i], np.min(rewards)))
    print("{} has mean max {}".format(infixes[i], np.max(mean_rewards)))
    print("{} has mean min {}".format(infixes[i], np.min(mean_rewards)))
    print("{} has runavg max {}".format(infixes[i], np.max(running_avg_mean_rewards)))
    print("{} has runavg min {}".format(infixes[i], np.min(running_avg_mean_rewards)))

    best_episode = np.argmax(mean_rewards)
    print("{} hbest mean episode is {}".format(infixes[i], best_episode))

    best_episode = np.argmax(np.max(rewards, 0))
    print("{} best single episode is {}".format(infixes[i], best_episode))

    best_episode = np.argmax(running_avg_mean_rewards)
    print("{} best runavg episode is {}".format(infixes[i], best_episode))

    if running_avg:
        arng = np.arange(W, W + len(running_avg_mean_rewards))
        plt.fill_between(arng, running_avg_mean_rewards - running_avg_offset, running_avg_mean_rewards + running_avg_offset, facecolor=colors[i][1])
        plt.plot(arng, running_avg_mean_rewards, label="M", color=colors[i][0])

        best_rewards[i] = (np.max(running_avg_mean_rewards))

        print("mean reward is {}".format(np.mean(running_avg_mean_rewards)))
    else:
        #plt.fill_between(np.arange(len(mean_rewards)), mean_rewards - offset, mean_rewards + offset, facecolor=colors[i][1])
        plt.plot(mean_rewards, label="M", color=colors[i][0])
        print("mean reward is {}".format(np.mean(mean_rewards)))

    plt.xlabel("t")
    plt.tight_layout()

y_plot_int = (np.max(best_rewards) + 200) / (1 - 2 / pt_height * o)
pt_offset_ratio = y_plot_int / pt_height
o = pt_offset_ratio * o
upper_y = np.max(best_rewards) + pt_offset_ratio * (pt_label_offset + 10 + 6 + 3)
s_best_rewards = np.flip(np.argsort(-best_rewards, axis=0)[:len(eff_indices)], axis=0)
if np.ndim(s_best_rewards) == 0: s_best_rewards = np.expand_dims(s_best_rewards, 0)
for ei in range(len(eff_indices)):
    ri = eff_indices[s_best_rewards[ei]]
    plt.axhline(y=best_rewards[ri], color=colors[ri][0], linestyle=':')
    plt.text(ei * shift, best_rewards[ri] + pt_offset_ratio * (pt_label_offset + 10 + 6), str(round(best_rewards[ri], 2)), color=colors[ri][0],
             horizontalalignment='left', verticalalignment='top', bbox={'facecolor': 'white', 'alpha': 0.7, 'pad': 3})

plt.ylim([-200 - o, upper_y + o])
save_path = os.path.join(out_folder, file_name.format(infix))
plt.tight_layout()
plt.savefig(save_path, dpi=300)
print("Saved to {}".format(save_path))
if show: plt.show()

max_stars_repo_name: Kognitive/QLearningComparison

max_issues_repo_name: Kognitive/QLearningComparison

max_forks_repo_name: Kognitive/QLearningComparison


text:
#! python
# -*- coding: utf-8 -*-
"""Property list of buffers

Author: Kazuya O'moto <komoto@jeol.co.jp>
"""
from pprint import pformat
import wx
from wx import aui
import numpy as np
from mwx.controls import Icon
from mwx.graphman import Layer
from mwx.framework import CtrlInterface
from wx.lib.mixins.listctrl import CheckListCtrlMixin

class CheckList(wx.ListCtrl, CheckListCtrlMixin, CtrlInterface):
    """ CheckList with FSM
    list item order = buffer order リストアイテムとバッファの並び順 0..n は常に一致します．
    """
    ## @property
    ## def checked_items(self):
    ##     return [j for j in range(self.ItemCount) if self.IsChecked(j)]

    @property
    def selected_items(self):
        return [j for j in range(self.ItemCount) if self.IsSelected(j)]

    @property
    def focused_item(self):
        return self.FocusedItem

    @property
    def all_items(self):
        rows = range(self.ItemCount)
        cols = range(self.ColumnCount)
        return [[self.GetItemText(j, k) for k in cols] for j in rows]

    def __init__(self, parent, target, **kwargs):
        wx.ListCtrl.__init__(self, parent,
            size=(400,130), style=wx.LC_REPORT|wx.LC_HRULES, **kwargs)
        CheckListCtrlMixin.__init__(self)
        CtrlInterface.__init__(self)

        self.parent = parent
        self.Target = target

        self.alist = ( # assoc-list of column names
            ("id", 42),
            ("name", 160),
            ("shape", 90),
            ("dtype", 60),
            ("Mb",   40),
            ("unit", 60),
            ## ("mean", 60),
            ## ("std", 60),
            ## ("med", 60),
            ("max", 50),
            ("min", 50),
            ("annotation", 240),
        )
        for k, (name, w) in enumerate(self.alist):
            self.InsertColumn(k, name, width=w)

        for j, frame in enumerate(self.Target.all_frames):
            self.InsertItem(j, str(j))
            self.Update(frame) # update all --> 計算が入ると時間がかかる

        self.handler.update({
            0 : {
                            '*' : (0, lambda v: v.Skip()),
               'Lbutton dclick' : (0, self.OnShowItems), # -> frame_shown
                'enter pressed' : (0, self.OnShowItems), # -> frame_shown
               'delete pressed' : (0, self.OnRemoveItems), # -> frame_removed/shown
                  'C-a pressed' : (0, self.OnSelectAllItems),
                  'C-o pressed' : (0, self.OnLoadItems),
                  'C-s pressed' : (0, self.OnSaveItems),
                 'M-up pressed' : (0, self.Target.OnPageUp),
               'M-down pressed' : (0, self.Target.OnPageDown),
            },
        })
        self.handler.clear(0)

        self.__dir = True
        self.SetToolTip('')
        self.ToolTip.SetMaxWidth(2048)

        self.Bind(wx.EVT_MOTION, self.OnMotion)
        self.Bind(wx.EVT_LIST_COL_CLICK, self.OnSortItems)

        self.context = { # bound to the target
            None: {
                  'frame_shown' : [ None, self.on_frame_shown ],
                 'frame_hidden' : [ None, self.on_frame_hidden ],
                 'frame_loaded' : [ None, self.on_frame_loaded ],
                'frame_removed' : [ None, self.on_frames_removed ],
               'frame_modified' : [ None, self.Update ],
                'frame_updated' : [ None, self.Update ],
            }
        }
        self.Target.handler.append(self.context)

    def Destroy(self):
        self.Target.handler.remove(self.context)
        return wx.ListCtrl.Destroy(self)

    def Update(self, frame):
        ls = ("{}".format(frame.index),
              "{}".format(frame.name),
              "{}".format(frame.buffer.shape),
              "{}".format(frame.buffer.dtype),
          "{:.1f}".format(frame.buffer.nbytes/1e6),
          "{:g}{}".format(frame.unit, '*' if frame.localunit else ''),
          ## "{:.2f}".format(np.mean(frame.buffer)),
          ## "{:.2f}".format(np.std(frame.buffer)),
          ## "{:.2f}".format(np.median(frame.buffer)),
            "{:g}".format(frame.buffer.max()),
            "{:g}".format(frame.buffer.min()),
              "{}".format(frame.annotation),
        )
        j = frame.index
        for k, v in enumerate(ls):
            self.SetItem(j, k, v)
        if frame.pathname:
            self.CheckItem(j)

    def OnMotion(self, evt):
        j, flag = self.HitTest(evt.GetPosition())
        tip = ''
        if j >= 0:
            attr = self.Target.all_frames[j].attributes
            if attr:
                tip = pformat(attr, width=80, depth=1) # compact=0:PY3
            else:
                tip = "No attributes"
        self.SetToolTip(tip)
        evt.Skip()

    def OnShowItems(self, evt):
        self.Target.select(self.FocusedItem)

    def OnRemoveItems(self, evt):
        del self.Target[self.selected_items]

    def OnSortItems(self, evt): #<wx._controls.ListEvent>
        col = evt.GetColumn()
        if col == 0: # reverse the first column
            self.__dir = False
        self.__dir = not self.__dir # toggle 0:ascend/1:descend

        def _eval(x):
            try:
                return eval(x.replace('*','')) # localunit* とか
            except Exception:
                return x

        frames = self.Target.all_frames
        if frames:
            frame = self.Target.frame
            la = sorted(self.all_items, key=lambda v: _eval(v[col]), reverse=self.__dir)
            frames[:] = [frames[int(c[0])] for c in la] # sort by new Id of items

            for j, c in enumerate(la):
                self.Select(j, False)        # 1, deselect all items,
                for k, v in enumerate(c[1:]): # 2, except for id(0), update text:str
                    self.SetItem(j, k+1, v)
            self.Target.select(frame) # invokes [frame_shown] to select the item

    def OnSelectAllItems(self, evt):
        for j in range(self.ItemCount):
            self.Select(j)

    def OnLoadItems(self, evt):
        self.parent.parent.import_index(target=self.Target)

    def OnSaveItems(self, evt):
        self.parent.parent.export_index(
            frames=[self.Target.all_frames[j] for j in self.selected_items] or None)

    ## --------------------------------
    ## Actions of frame-handler
    ## --------------------------------

    def on_frame_loaded(self, frame):
        j = frame.index
        self.InsertItem(j, str(j))
        for k in range(j+1, self.ItemCount): # id(0) を更新する
            self.SetItem(k, 0, str(k))
        self.Update(frame)

    def on_frame_shown(self, frame):
        j = frame.index
        self.SetItemFont(j, self.Font.Bold())
        self.Select(j)
        self.Focus(j)

    def on_frame_hidden(self, frame):
        j = frame.index
        self.SetItemFont(j, self.Font)
        self.Select(j, False)

    def on_frames_removed(self, indices):
        for j in reversed(indices):
            self.DeleteItem(j)
        for k in range(self.ItemCount): # id(0) を更新する
            self.SetItem(k, 0, str(k))

class Plugin(Layer):
    """Property list of Grpah buffers
    """
    menu = "Plugins/Extensions"
    menustr = "&Buffer listbox\tCtrl+b"
    caption = "Property list"
    dockable = False
    unloadable = False

    all_pages = property(
        lambda self: [self.nb.GetPage(i) for i in range(self.nb.PageCount)])

    def Init(self):
        self.nb = aui.AuiNotebook(self, size=(400,150),
            style = (aui.AUI_NB_DEFAULT_STYLE|aui.AUI_NB_RIGHT)
                  &~(aui.AUI_NB_CLOSE_ON_ACTIVE_TAB|aui.AUI_NB_MIDDLE_CLICK_CLOSE)
        )
        self.layout((self.nb,), expand=2, border=0)
        self.attach(self.graph, "graph")
        self.attach(self.output, "output")

        def on_focus_set(v):
            self.parent.select_view(self.nb.CurrentPage.Target)
            v.Skip()

        self.nb.Bind(wx.EVT_CHILD_FOCUS, on_focus_set)

        self.Menu[0:0] = [
            (101, "&Edit annotation", "Edit annotation", Icon('edit'),
                lambda v: self.ask()),
            (),
        ]

    def attach(self, target, caption):
        if target not in [lc.Target for lc in self.all_pages]:
            lc = CheckList(self, target)
            self.nb.AddPage(lc, caption)

    def detach(self, target):
        for k, lc in enumerate(self.all_pages):
            if target is lc.Target:
                self.nb.DeletePage(k)

    def show_page(self, target):
        self.nb.SetSelection(next((k
            for k, lc in enumerate(self.all_pages) if target is lc.Target), -1))

    def ask(self, prompt='Enter an annotation'):
        """Get response from the user using a dialog box."""
        page = self.nb.CurrentPage
        frames = page.Target.all_frames
        if frames:
            frame = frames[page.focused_item]
            with wx.TextEntryDialog(self, prompt,
                caption='Input Dialog', value=frame.annotation) as dlg:
                if dlg.ShowModal() == wx.ID_OK:
                    for j in page.selected_items:
                        frames[j].annotation = dlg.Value

if __name__ == "__main__":
    import glob
    from mwx.graphman import Frame

    app = wx.App()
    frm = Frame(None)
    frm.load_plug(__file__, show=1, docking=0)
    for path in glob.glob(r"C:/usr/home/workspace/images/*.bmp"):
        print("loading path =", path)
        frm.load_buffer(path)
    frm.Show()
    app.MainLoop()


text:
import os
import numpy as np
import pytest
from alchemistry_toolkit.parsers.log_parser import *
from alchemistry_toolkit.estimators.weights_generator import *

current_path = os.path.dirname(os.path.abspath(__file__))
data_path = os.path.join(current_path, 'sample_inputs')

file_lambda_MetaD = data_path + '/lambda_MetaD.log'
file_EXE_updating = data_path + '/EXE_updating.log'
file_EXE_equilibrated = data_path + '/EXE_equilibrated.log'
file_EXE_fixed = data_path + '/EXE_fixed.log'

test_1 = EXE_LogInfo(file_lambda_MetaD)
test_2 = EXE_LogInfo(file_EXE_updating)
test_3 = EXE_LogInfo(file_EXE_equilibrated)
test_4 = EXE_LogInfo(file_EXE_fixed)

def test_adjust_weights():
    # Part 1: Tests with final counts
    c1, w1 = test_1.get_final_data()
    c2, w2 = test_2.get_final_data()
    c3, w3 = test_3.get_final_data()
    c4, w4 = test_4.get_final_data()

    w_adj_1, rmsd_1 = adjust_weights(c1, w1)
    w_adj_2, rmsd_2 = adjust_weights(c2, w2)
    w_adj_3, rmsd_3 = adjust_weights(c3, w3)
    w_adj_4, rmsd_4 = adjust_weights(c4, w4)

    expected_1 = np.array([0, 7.80622153, 14.18712932, 17.3500546 , 19.29906404,
                           20.69265301, 20.67641217, 18.43027303, 15.59433296])
    expected_2 = np.array([0, 7.98738303, 14.86814794, 17.29113768, 18.76978532,
                           20.08823888, 20.57088274, 19.34025672, 16.66820895])
    expected_3 = np.array([0, 7.81886552, 13.88378867, 16.77357959, 18.90675151,
                           20.36135662, 21.2115675 , 18.51300374, 14.91915581])
    expected_4 = np.array([0, 7.8394568 , 13.93409582, 16.80223691, 18.89663727,
                           20.36083512, 21.14485547, 18.69810792, 15.1645849 ])

    np.testing.assert_array_almost_equal(expected_1, w_adj_1)
    np.testing.assert_array_almost_equal(expected_2, w_adj_2)
    np.testing.assert_array_almost_equal(expected_3, w_adj_3)
    np.testing.assert_array_almost_equal(expected_4, w_adj_4)

    assert rmsd_1 == 0.03154
    assert rmsd_2 == 0.30296
    assert rmsd_3 == 0.29085
    assert rmsd_4 == 0.36279

    # Part 2: Test with equilibrated counts
    _, _ = test_3.get_WL_data()
    c5 = test_3.equil_c  # equilibrated counts
    w5 = test_3.equil_w  # equilibrated weights
    w_adj_5, rmsd_5 = adjust_weights(c5, w5)
    expected_5 = np.array([0, 7.58441458, 13.8391983 , 16.88768357, 18.78953266,
                           20.59009494, 21.27996925, 17.37912344, 14.87146973])
    np.testing.assert_array_almost_equal(expected_5, w_adj_5)
    assert rmsd_5 == 0.11119


text:
"""
This module contains different data sampling methods
"""

import logging
import random
import datetime
import math
from abc import ABC, abstractmethod
from io import BytesIO

import shapely.affinity
import shapely.geometry
import shapely.ops
import dateutil.parser
import numpy as np

from PIL import Image
from PIL.TiffTags import TAGS

from sentinelhub import get_json, BBox, CRS, download_data, DownloadRequest, MimeType, WebFeatureService, \
    DataSource, Geometry

from .tasks import Task
from .sampling_utils import random_sample, random_sample_image, sample_image_with_bbox, \
    get_resolution, count_points, triangulate, random_sample_point
from .geopedia import get_layer_item_list
from .image_utils import merge_images, encode_image, hex_to_rgb

BASE_INDEX_URL = 'https://services.sentinel-hub.com/index/s2/v3/tiles/'

LOGGER = logging.getLogger(__name__)

class Sampling(ABC):

    def __init__(self):
        self.index = 0

    def __iter__(self):
        """Iteration method

        :return: Iterator over samples
        :rtype: Iterator[dict]
        """
        self.index = 0
        return self

    @abstractmethod
    def __next__(self):
        """Next method
        """
        raise NotImplementedError

    @staticmethod
    def _expand_geo_shape(geo_shape, factor):
        if isinstance(factor, (float, int)):
            factor = (factor, factor)
        return shapely.affinity.scale(geo_shape, factor[0], factor[1], origin=shapely.geometry.Point((0, 0)))

class SentinelHubSampling(Sampling):

    def __init__(self, window_shape, resolution, buffer=10, data_source=DataSource.SENTINEL2_L1C):
        """
        :param window_shape: Shape of the sampling window in pixels
        :type window_shape: (int, int)
        :param resolution: Sampling resolution in meters
        :type resolution: int or float
        :param buffer: Buffer around sampled window which has to be inside the sampling area in pixels
        :type buffer: int
        :param data_source: Source of satellite data
        :type data_source: sentinelhub.DataSource
        """
        super().__init__()

        self.window_shape = window_shape
        self.resolution = resolution
        self.buffer = buffer
        self.data_source = data_source

    def __next__(self):
        attempts = 16
        while attempts > 0:
            try:
                tile_info = self.get_random_tile()

                sampling_geometry = self.get_sampling_geometry(tile_info)

                bbox = self.get_random_bbox(sampling_geometry)

                return Task(bbox=bbox, acq_time=self.get_sensing_time(tile_info),
                            window_shape=self.window_shape, data_list=[], tile_id=self.get_tile_id(tile_info))
            except ValueError:
                attempts -= 1

        raise ValueError('Failed to sample a new task')

    def get_random_bbox(self, area_geometry):
        """
        :param area_geometry: Geometry object which has to be in UTM CRS
        :type area_geometry: sentinelhub.Geometry
        """
        if not CRS.is_utm(area_geometry.crs):
            raise ValueError('Geometry object has to be in UTM CRS for sampling')

        reduced_geo = self._expand_geo_shape(area_geometry.geometry, 1 / self.resolution)
        sampled_rectangle = random_sample(reduced_geo, (self.window_shape[0] + 2 * self.buffer,
                                                        self.window_shape[1] + 2 * self.buffer))
        expanded_rectangle = self._expand_geo_shape(sampled_rectangle, self.resolution)

        return BBox(expanded_rectangle.bounds, crs=area_geometry.crs)

    @abstractmethod
    def get_random_tile(self):
        raise NotImplementedError

    @abstractmethod
    def get_sampling_geometry(self):
        raise NotImplementedError

    @abstractmethod
    def get_sensing_time(self):
        raise NotImplementedError

    @abstractmethod
    def get_tile_id(self):
        raise NotImplementedError

class ShIndexSampling(SentinelHubSampling):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        if self.data_source is not DataSource.SENTINEL2_L1C:
            raise NotImplementedError

        self._archive_size = None
        self._archive_check_time = None  # In stateless service won't be needed anymore

    def get_random_tile(self):
        random_tile_id = random.randint(1, self.get_archive_size())
        tile_info = self.get_tile_info(random_tile_id)

        cover_percentage = float(tile_info['coverArea']) / 12055600804.0

        if cover_percentage >= 0.1 and random.random() <= cover_percentage:
            return tile_info

        raise ValueError('Could not get a random tile with enough coverage from the archive')

    def get_archive_size(self):
        """ This will collect archive size at most once per day
        """
        current_time = datetime.datetime.now()
        if self._archive_size is None or self._archive_check_time < current_time - datetime.timedelta(days=1):
            self._archive_check_time = current_time
            self._archive_size = self.get_tile_id(self.get_tile_info('lastTile'), esa_id=False)
        return self._archive_size

    @staticmethod
    def get_tile_info(tile_id):
        LOGGER.info('Collecting data from S-2 index for tile %s', str(tile_id))
        return get_json('{}{}'.format(BASE_INDEX_URL, tile_id))

    @staticmethod
    def get_tile_id(tile_info, esa_id=True):
        """ Can return either ESA tile ID or SH index tile ID
        """
        if esa_id:
            return tile_info['pdiId']
        return int(tile_info['id'])

    @staticmethod
    def get_crs(tile_info):
        return CRS(tile_info['tileOrigin']['crs']['properties']['name'].rsplit(':', 1)[1])

    @staticmethod
    def get_sensing_time(tile_info):
        datetime_obj = datetime.datetime.strptime('{}'.format(tile_info['sensingTime']), '%Y-%m-%dT%H:%M:%S.%f')
        return datetime_obj.date()

    @staticmethod
    def get_sampling_geometry(tile_info):
        return Geometry(shapely.geometry.shape(tile_info['coverGeometry']), crs=ShIndexSampling.get_crs(tile_info))

class ShOgcSampling(SentinelHubSampling):

    def __init__(self, area_of_interest, time_interval, maxcc, *args, **kwargs):
        """
        :param area_of_interest: Area of interest to sample from
        :type area_of_interest: sentinelhub.Geometry
        :param time_interval: Time interval when to sample
        :type time_interval:[datetime.datetime, datetime.datetime]
        :param maxcc: Maximal cloud coverage to allow to be sampled
        :type maxcc: float
        """
        super().__init__(*args, **kwargs)

        self.area_of_interest = area_of_interest
        self.time_interval = time_interval
        self.maxcc = maxcc

        self.time_interval_list = self._time_split(self.time_interval, resolution=datetime.timedelta(weeks=4))
        self.aoi_triangulation = triangulate(self.area_of_interest.geometry)

    def get_random_tile(self):
        """Get a random tile over AOI and time interval"""
        random_point = random_sample_point(self.area_of_interest.geometry, triangles=self.aoi_triangulation,
                                           use_int_coords=False)
        small_bbox = self._get_small_bbox(random_point)

        for time_interval in self._get_shuffled_time_intervals():
            tiles = list(WebFeatureService(bbox=small_bbox, time_interval=time_interval,
                                           data_source=self.data_source, maxcc=self.maxcc))
            if tiles:
                return random.choice(tiles)

        raise ValueError('Could not sample a random point from given areas of interest, maybe no data is available')

    def _get_small_bbox(self, point):
        """ Create a small random bbox from a point for WFS search
        TODO: sentinelhub-py should support WFS searches with a point object
        """
        x, y = point
        eps = 0.0001 if self.area_of_interest.crs is CRS.WGS84 else 0.01
        return BBox([x - eps, y - eps, x + eps, y + eps], crs=self.area_of_interest.crs)

    @staticmethod
    def _time_split(time_interval, resolution=datetime.timedelta(weeks=4)):
        """ Split time interval into several, at most resolution long
        """
        if len(time_interval) > 2:
            raise ValueError('Splitting only works on [ datetime, datetime ] array.')

        delta_t = time_interval[1] - time_interval[0]
        n_t = math.ceil(delta_t / resolution)
        delta_t = delta_t / n_t
        return [[time_interval[0] + i * delta_t, time_interval[0] + (i + 1) * delta_t] for i in range(n_t)]

    def _get_shuffled_time_intervals(self):
        """ Shuffles time intervals to allow a random selection
        """
        shuffled_time_intervals = self.time_interval_list[:]
        random.shuffle(shuffled_time_intervals)
        return shuffled_time_intervals

    def get_sampling_geometry(self, tile_info):
        tile_geometry = Geometry(tile_info['geometry'], crs=self.area_of_interest.crs)

        utm_crs = ShOgcSampling.get_crs(tile_info)
        tile_geometry = tile_geometry.transform(utm_crs)
        utm_aoi_geometry = self.area_of_interest.transform(utm_crs)

        # Warning: This intersection can be too small for sampling, that is why __next__ method is retrying the process
        return Geometry(tile_geometry.geometry.intersection(utm_aoi_geometry.geometry), utm_crs)

    @staticmethod
    def get_crs(tile_info):
        return CRS(tile_info['properties']['crs'].rsplit(':', 1)[1])

    @staticmethod
    def get_tile_id(tile_info):
        """ Returns ESA tile ID
        """
        return tile_info['properties']['id']

    @staticmethod
    def get_sensing_time(tile_info):
        return datetime.datetime.strptime('{}'.format(tile_info['properties']['date']), '%Y-%m-%d')

class GeopediaLayerSampling(Sampling):

    def __init__(self, source, feature_interval=None):
        super().__init__()

        self.source = source

        self.item_list = None
        self.feature_interval = feature_interval

    def __next__(self):

        if self.item_list is None:
            self.set_item_list()

        self.index += 1
        if self.index >= len(self.item_list):
            self.index = 0

        task = self.make_task(self.item_list[self.index])
        return task

    def set_item_list(self):
        LOGGER.info('Collecting data from Geopedia layer %d', int(self.source.geopedia_layer))
        self.item_list = get_layer_item_list(int(self.source.geopedia_layer), interval=self.feature_interval)

        random.shuffle(self.item_list)
        LOGGER.info('Collected data about %d features', len(self.item_list))

    @staticmethod
    def get_bbox_coords(tile_coords, offset, window_shape, resolution=(10, 10)):
        x, y = tile_coords[0] + resolution[0] * offset[0], tile_coords[1] - resolution[1] * offset[1]
        return [x, y, x + resolution[0] * window_shape[0], y - resolution[1] * window_shape[1]]

class GeopediaWaterBodySampling(GeopediaLayerSampling):

    def __init__(self, source, window_shape, resolution):
        super().__init__(source)

        self.window_shape = window_shape
        self.resolution = resolution

    def make_task(self, item):
        props = item['properties']

        feature_id = int(item['@id'].rsplit('/', 1)[1])

        image, bbox = self._collect_data(props['Mask'][0]['objectPath'])
        wb_geometry = shapely.geometry.shape(item['geometry'])

        # sampled_image, sampled_bbox = self.random_sample_bbox(image, bbox, wb_geometry)
        (sampled_image, sampled_bbox), wb_geometry = self.random_sample_geometry(image, bbox, wb_geometry)

        colored_sampled_image = np.zeros(sampled_image.shape[:2] + (3,), dtype=np.uint8)
        colored_sampled_image[sampled_image == 1] = hex_to_rgb(self.source.layers[0]['classes'][0]['color'])

        data_list = [{
            "layer": self.source.layers[0]['title'],
            "image": encode_image(colored_sampled_image)
        }]

        vector_data = [
            # bbox.get_geojson(),
            shapely.geometry.mapping(wb_geometry)
            # item['geometry']  # This is already shown in Geopedia WMS layer
        ]

        return Task(bbox=sampled_bbox, acq_time=self.parse_time(props), window_shape=self.window_shape,
                    data_list=data_list, feature_id=feature_id, vector_data=vector_data)

    @staticmethod
    def parse_time(gpd_props):
        return dateutil.parser.parse(gpd_props['SAT_IMAGE_DATE'].split('T')[0]).date()

    def _collect_data(self, url):
        download_list = [DownloadRequest(url=url, save_response=False, data_type=MimeType.RAW)]
        raw_image = download_data(download_list)[0].result(timeout=60)

        bbox = self.get_bbox(BytesIO(raw_image))

        image = np.array(Image.open(BytesIO(raw_image)))

        return image, bbox

    @staticmethod
    def get_bbox(image_bytes):
        with Image.open(image_bytes) as img:
            meta_dict = {TAGS[key]: value for key, value in img.tag.items()}

            if meta_dict['GeoAsciiParamsTag'] != ('WGS 84|',):
                raise ValueError('Expected image in WGS84')

        window_shape = meta_dict['ImageWidth'][0], meta_dict['ImageLength'][0]

        transform = (meta_dict['ModelTiepointTag'][3], meta_dict['ModelPixelScaleTag'][0], 0.0,
                     meta_dict['ModelTiepointTag'][4], 0.0, -meta_dict['ModelPixelScaleTag'][1])

        bbox = BBox(GeopediaLayerSampling.get_bbox_coords((transform[0], transform[3]), (0, 0), window_shape,
                                                          (transform[1], -transform[5])), crs=CRS.WGS84)
        return bbox

    def random_sample_geometry(self, image, bbox, wb_geometry_initial):
        resolution = get_resolution(image, list(bbox))
        bbox_polygon = bbox.get_geometry()
        bbox_polygon = self._expand_geo_shape(bbox_polygon, (1 / resolution[0], 1 / resolution[1]))

        wb_geometry = self._expand_geo_shape(wb_geometry_initial, (1 / resolution[0], 1 / resolution[1]))

        wb_geometry = wb_geometry.buffer(max(self.window_shape))
        wb_geometry = wb_geometry.simplify(2, preserve_topology=False)  # This simplifies geometry
        wb_geometry = wb_geometry.intersection(bbox_polygon)

        LOGGER.debug('Reduced number of points in sampling vector shape from %d to %d',
                     count_points(wb_geometry_initial), count_points(wb_geometry))

        sampled_rectangle = random_sample(wb_geometry, self.window_shape)
        sampled_bbox = list(map(float, self._expand_geo_shape(sampled_rectangle, resolution).bounds))

        wb_geometry = self._expand_geo_shape(wb_geometry, resolution)

        return sample_image_with_bbox(image, bbox, sampled_bbox), wb_geometry

    def random_sample_bbox(self, image, bbox, wb_geometry):
        image, bbox = sample_image_with_bbox(image, bbox, list(wb_geometry.bounds), buffer=10)

        return random_sample_image(image, bbox, self.window_shape)

class GeopediaOldAppResults(GeopediaLayerSampling):
    """ Special case of Geopedia sampling when we work with results of old version of classification results
    """

    def make_task(self, item):
        props = item['properties']

        tile_id = int(props["TileId"])
        window_shape = tuple(map(lambda prop_name: int(props[prop_name]), ["Size X", "Size Y"]))
        offset = tuple(map(lambda prop_name: int(props[prop_name]), ["Offset X", "Offset Y"]))

        tile_info = ShIndexSampling.get_tile_info(tile_id)
        tile_coords = self.get_tile_coords(tile_info)

        bbox = BBox(self.get_bbox_coords(tile_coords, offset, window_shape),
                    crs=ShIndexSampling.get_crs(tile_info))

        return Task(bbox=bbox, acq_time=ShIndexSampling.get_sensing_time(tile_info),
                    window_shape=window_shape, tile_id=tile_id, tile_coords=tile_coords, offset=offset,
                    data_list=self._collect_data(props['Masks']))

    @staticmethod
    def get_tile_coords(tile_info):
        return tile_info['tileOrigin']['coordinates']

    @staticmethod
    def get_bbox_coords(tile_coords, offset, window_shape, resolution=(10, 10)):
        x, y = tile_coords[0] + resolution[0] * offset[0], tile_coords[1] - resolution[1] * offset[1]
        return [x, y, x + resolution[0] * window_shape[0], y - resolution[1] * window_shape[1]]

    def _collect_data(self, mask_list):
        download_list = [DownloadRequest(url=mask_props['objectPath'], save_response=False, data_type=MimeType.PNG)
                         for mask_props in mask_list]
        images = [future.result(timeout=60) for future in download_data(download_list)]
        image_names = [mask_props['niceName'] for mask_props in mask_list]

        # TODO: Change this hardcoded part
        data_dict = {}
        for image, image_name in zip(images, image_names):
            class_name = image_name.rsplit('_', 1)[1].split('.')[0]
            layer_name = {
                'Opaque clouds': 'Clouds',
                'Thick clouds': 'Clouds',
                'Thin clouds': 'Clouds',
                'Shadows': 'Shadows',
                'Land': 'Surface',
                'Water': 'Surface',
                'Snow': 'Surface'
            }[class_name]
            data_dict[layer_name] = data_dict.get(layer_name, {})
            data_dict[layer_name][class_name] = image

        return self._join_data_dict(data_dict)

    def _join_data_dict(self, data_dict):
        data_list = []

        for layer_prop in self.source.layers:
            layer_name = layer_prop['title']
            if layer_name in data_dict:
                images = []
                colors = []
                for class_prop in layer_prop['classes']:
                    class_name = class_prop['title']
                    if class_name in data_dict[layer_name]:
                        images.append(data_dict[layer_name][class_name])
                        colors.append(class_prop['color'])

                if images:
                    merged_image = merge_images(images, colors)

                    data_list.append({
                        "layer": layer_name,
                        "image": encode_image(merged_image)
                    })

        return data_list

max_stars_repo_name: sentinel-hub/classification-app-backend

max_issues_repo_name: sentinel-hub/classification-app-backend

max_forks_repo_name: sentinel-hub/classification-app-backend


text:
import numpy as np
import pandas as pd
import sys
from tqdm import tqdm
import h5py
from sklearn.metrics.pairwise import cosine_similarity
import pkg_resources
import re

from missingpy import KNNImputer, MissForest

COMPL = {"A":"T","T":"A","G":"C","C":"G"}

# ---------------------------------
# IOUtils
# ---------------------------------
def file_loader(x):
    if x.endswith('.csv'):
        return pd.read_csv(x, index_col=0)
    elif x.endswith('.parquet'):
        return pd.read_parquet(x)
    else:
        return pd.read_csv(x, sep='\t', index_col=0)

# ---------------------------------
# NMF Utils
# ---------------------------------
def split_negatives(x: pd.DataFrame, tag: str = '_n', axis: int = 0):
    """
    Split dataframe into positive and negative components.
    --------------------
    Args:
        * x: pd.DataFrame input matrix
        * tag: string that will be added to the end of the negative variable names
        * axis: which axis to create a positive dimension for
            NOTE: this is for 2D numpy arrays

    Returns:
        * pd.DataFrame with new positive transformed matrix.
    """
    x_neg = -1 * x.copy()
    x_neg = x_neg.where(x_neg > 0, 0)

    if axis:
        x.columns = x.columns.astype(str)
        x_neg.columns = [x+'_n' for x in x.columns]
    else:
        x.index = x.index.astype(str)
        x_neg.index = [x+'_n' for x in x.index]

    return pd.concat([x.where(x > 0, 0), x_neg], axis=axis)

def impute_values(df: pd.DataFrame, method: str = 'mean', **kwargs):
    """
    Impute missing values in DataFrame (np.nan or None).
    ------------------------
    Args:
        * df: pd.DataFrame of (samples x features)
        * method: string for what method of imputation to use
            ** 'mean': mean imputation
            ** 'knn': K-NN imputation (see missingpy.KNNImputer)
            ** 'rf': random forest imputation (see missingpy.MissForest)

    Returns:
        * pd.DataFrame: imputed values (samples x features)
    """
    assert method in ('mean','knn','rf'), '{} not yet implemented.'.format(method)

    if method=='mean':
        return df.fillna(df.mean(0))
    elif method=='knn':
        X = df.values
        imputer = KNNImputer(**kwargs)
        X_impute = imputer.fit_transform(X)
        return pd.DataFrame(X_impute, index=df.index, columns=df.columns)
    elif method=='rf':
        X = df.values
        imputer = MissForest(**kwargs)
        X_impute = imputer.fit_transform(X)
        return pd.DataFrame(X_impute, index=df.index, columns=df.columns)

def l2fc(df: pd.DataFrame, center: str = 'median', axis: int = 1):
    """
    Log2 Fold-Change Input Dataframe
    -------------------------
    Args:
        * df: pd.DataFrame
        * center: center-metrix to compute log-fold change over
            ** 'median'
            ** 'mean'
        * axis: int axis to compute median and LFC across (i.e. samples)

    Returns:
        * pd.Dataframe: log2FC-transformed pandas dataframe
    """
    X = df.values

    if center == 'median':
        X_mid = np.median(X, axis)
    elif center == 'mean':
        X_mid = np.mean(X, axis)

    if axis==1:
        return pd.DataFrame(np.log2(X) - np.log2(X_mid)[:,np.newaxis], index=df.index, columns=df.columns)
    else:
        return pd.DataFrame(np.log2(X) - np.log2(X_mid)[np.newaxis], index=df.index, columns=df.columns)

def compute_phi(mu: float, var: float, beta: float):
    """
    Compute Phi
    ------------------------
    Compute the Dispersion parameter.
    """
    return var / (mu ** (2-beta))

def transfer_weights(W: pd.DataFrame, H: pd.DataFrame, active_thresh:float = 1e-2):
    """
    Transfers weights from output of NMF.
    ------------------------
    Args:
        * W: input W matrix (K x n_features)
        * H: input H matrix (n_samples x K)
        * active_thresh: active threshold to consider a factor loading significant

    Returns:
        * W_final: normalized W matrix (K x n_features)
        * H_final: normalized H matrix (n_samples x K)
        * nsig: number of signatures found
    """
    W = W.copy()
    H = H.copy()

    nonzero_idx = (np.sum(H, axis=1) * np.sum(W, axis=0)) > active_thresh

    W_active = W[:, nonzero_idx]
    H_active = H[nonzero_idx, :]
    nsig = np.sum(nonzero_idx)

    # Normalize W and transfer weight to H matrix
    W_weight = np.sum(W_active, axis=0)
    W_final = W_active / W_weight
    H_final = W_weight[:, np.newaxis] * H_active

    return W_final, H_final, nsig, nonzero_idx

def select_signatures(W: pd.DataFrame, H: pd.DataFrame):
    """
    Scales NMF output by sample and feature totals to select Signatures.
    ------------------------
    Args:
        * W: input W matrix (K x n_features)
        * H: input H matrix (n_samples x K)

    Returns:
        * W: output W matrix with max_id, max, and max_norm columns
        * H: output H matrix with max_id, max, and max_norm columns
    """
    Wnorm = W.copy()
    Hnorm = H.copy()

    # Scale Matrix
    for j in range(W.shape[1]):
        Wnorm.iloc[:,j] *= H.sum(1).values[j]
        Hnorm.iloc[j,:] *= W.sum(0).values[j]

    # Normalize
    Wnorm = Wnorm.div(Wnorm.sum(1),axis=0)
    Hnorm = Hnorm.div(Hnorm.sum(0),axis=1)

    H = H.T
    Hnorm = Hnorm.T

    # Get Max Values
    H_max_id = H.idxmax(axis=1, skipna=True).astype('int')
    H['max'] = H.max(axis=1, skipna=True)
    H['max_id'] = H_max_id
    Hnorm['max_norm']=Hnorm.max(axis=1, skipna=True)

    W_max_id = W.idxmax(axis=1, skipna=True).astype('int')
    W['max'] = W.max(axis=1, skipna=True)
    W['max_id'] = W_max_id
    Wnorm['max_norm']=Wnorm.max(axis=1, skipna=True)

    H['max_norm'] = Hnorm['max_norm']
    W['max_norm'] = Wnorm['max_norm']

    _rename = {x:'S'+x for x in list(H)[:-3]}
    H = H.rename(columns=_rename)
    W = W.rename(columns=_rename)

    return W,H

def select_markers(
    X: pd.DataFrame, \
    W: pd.DataFrame, \
    H: pd.DataFrame, \
    cut_norm: float = 0.5, \
    cut_diff: float = 1.0, \
    verbose: bool = False \
    ):
    """
    Marker selection from NMF.
    ------------------------
    Args:
        * X: Input X matrix (n_samples x n_features)
        * W: input W matrix (K x n_features) with max_id, max, and max_norm columns
        * H: input H matrix (n_samples x K) with max_id, max, and max_norm columns
        * cut_norm: minimum normalized signature strength
        * cut_diff: minimum difference between selected signature and other signatures

    Returns:
        * Pandas Dataframe of NMF markers
        * Pandas Dataframe of full W matrix
    """
    markers = list()
    full = list()

    pd.options.mode.chained_assignment = None

    for n in tqdm(np.unique(W['max_id']), desc='Clusters: ', disable=not verbose):
        if H[H['max_id']==n].shape[0] > 0:
            tmp = W[W['max_id']==n]
            tmp.loc[:,'mean_on'] = X.loc[np.array(tmp.index), H[H['max_id']==n].index].mean(axis=1)
            tmp.loc[:,'mean_off'] = X.loc[np.array(tmp.index), H[H['max_id']!=n].index].mean(axis=1)
            tmp.loc[:,'diff'] = tmp.loc[:,'mean_on'] - tmp.loc[:,'mean_off']

            tmp.sort_values('diff', ascending=False, inplace=True)
            full.append(tmp)
            markers.append(tmp[(tmp['diff'] > cut_diff) & (tmp['max_norm'] >= cut_norm)])

    nmf_markers = X.loc[pd.concat(markers).index,H.max_id.sort_values().index]
    nmf_markers.index.name = 'feat'

    return nmf_markers, pd.concat(full)

# ---------------------------------
# Mutational Signature Utils
# ---------------------------------
def load_cosmic_signatures(cosmic: str):
    """
    Load cosmic signatures.
    -------------------------
    Pre-processed Cosmic Mutational Signatures.
    """
    if cosmic == 'cosmic2':
        print("   * Using {} signatures".format(cosmic))
        cosmic = pd.read_csv(pkg_resources.resource_filename('signatureanalyzer', 'ref/cosmic_v2/sa_cosmic2.tsv'), sep='\t').dropna(1)
        cosmic_index = "Somatic Mutation Type"
    elif cosmic == 'cosmic3':
        print("   * Using {} signatures".format(cosmic))
        cosmic = pd.read_csv(pkg_resources.resource_filename('signatureanalyzer', 'ref/cosmic_v3/sa_cosmic3_sbs.tsv'), sep='\t').dropna(1)
        cosmic_index = "Somatic Mutation Type"
    elif cosmic == 'cosmic3_exome':
        print("   * Using {} signatures".format(cosmic))
        cosmic = pd.read_csv(pkg_resources.resource_filename('signatureanalyzer', 'ref/cosmic_v3/sa_cosmic3_sbs_exome.tsv'), sep='\t').dropna(1)
        cosmic_index = "Somatic Mutation Type"
    elif cosmic == 'cosmic3_DBS':
        print("   * Using {} signatures".format(cosmic))
        cosmic = pd.read_csv(pkg_resources.resource_filename('signatureanalyzer', 'ref/cosmic_v3/sa_cosmic3_dbs.tsv'), sep='\t').dropna(1)
        cosmic_index = "Somatic Mutation Type"
    elif cosmic == 'cosmic3_ID':
        print("   * Using {} signatures".format(cosmic))
        cosmic = pd.read_csv(pkg_resources.resource_filename('signatureanalyzer', 'ref/cosmic_v3/sa_cosmic3_id.tsv'), sep='\t').dropna(1)
        cosmic_index = "Mutation Type"
    else:
        raise Exception("Not yet implemented for {}".format(cosmic))

    return cosmic, cosmic_index

def compl(seq: str, reverse: bool = False):
    """
    Gets the complement of a string
    Args:
        * seq: string (does not have to be base pair)
        * reverse: set to true to reverse seq

    Returns:
        * complement of seq
    """
    return ''.join([COMPL[x] if x in COMPL.keys() else x for x in (reversed(seq) if reverse else seq)])

def sbs_annotation_converter(x: str) -> str:
    """
    Eithers swaps from word -> arrow format for SBS or vice versa.
        word: (REF)(ALT)(LEFT)(RIGHT)
        arrow: (LEFT)[(REF)>(ALT)](RIGHT)
    """
    if '>' in x:
        return x[2]+x[4]+x[0]+x[6]
    else:
        return x[2]+'['+x[0]+'>'+x[1]+']'+x[3]

def _map_id_sigs(
    df: pd.DataFrame
    ) -> pd.Series:
    """
    Map Insertion-Deletion Substitution Signatures.
    -----------------------
    Args:
        * df: pandas.core.frame.DataFrame with index to be mapped

    Returns:
        * pandas.core.series.Series with matching indices to input cosmic
    """
    def _convert_to_cosmic(x):
        i1 = 'DEL' if 'del' in x else 'INS'
        if x[0].isdigit():
            i2 = 'MH' if 'm' in x else 'repeats'
            i3 = re.search('[\d+]+', x).group()
        else:
            i2 = x[0]
            i3 = '1'
        i4 = re.search('[\d+]+$', x).group()
        if i1 == 'DEL' and i2 != 'MH':
            i4 = str(int(i4[0]) - 1) + i4[1:]
        return '_'.join([i1, i2, i3, i4])

    if df.index.name is None: df.index.name = 'index'
    df_idx = df.index.name

    context_s = df.reset_index()[df_idx]
    return context_s.apply(_convert_to_cosmic)

def _map_dbs_sigs(
    df: pd.DataFrame,
    cosmic_df: pd.DataFrame,
    sub_index: str = 'Substitution Type'
    ) -> pd.Series:
    """
    Map Doublet-Base Substitution Signatures.
    -----------------------
    Args:
        * df: pandas.core.frame.DataFrame with index to be mapped
        * cosmic_df: dataframe with Cosmic indices to map to
        * sub_index: substitution index - the column to map to in the cosmic dataframe

    Returns:
        * pandas.core.series.Series with matching indices to input cosmic
    """
    def _check_to_flip(x, ref):
        if x in ref:
            return x
        else:
            return compl(x)

    if df.index.name is None: df.index.name = 'index'
    df_idx = df.index.name

    context_s = df.reset_index()[df_idx]
    return context_s.apply(lambda x: _check_to_flip(x, set(cosmic_df[sub_index])))

def _map_sbs_sigs(
    df: pd.DataFrame,
    cosmic_df: pd.DataFrame,
    sub_index: str = 'Substitution Type'
    ) -> pd.Series:
    """
    Map Single-Base Substitution Signatures.
    -----------------------
    Args:
        * df: pandas.core.frame.DataFrame with index to be mapped
        * cosmic_df: dataframe with Cosmic indices to map to
        * sub_index: substitution index - the column to map to in the cosmic dataframe

    Returns:
        * pandas.core.series.Series with matching indices to input cosmic
    """
    def _check_to_flip(x, ref):
        if x[2:-2] in ref:
            return x
        else:
            return compl(x)

    if df.index.name is None: df.index.name = 'index'
    df_idx = df.index.name

    if ">" not in df.index[0]:
        # Already in word format
        context_s = df.reset_index()[df_idx].apply(sbs_annotation_converter)
    else:
        # Already in arrow format
        context_s = df.reset_index()[df_idx]

    return context_s.apply(lambda x: _check_to_flip(x, set(cosmic_df[sub_index])))

def postprocess_msigs(res: dict, cosmic: pd.DataFrame, cosmic_index: str, cosmic_type: str):
    """
    Post process ARD-NMF on mutational signatures.
    ------------------------
    Args:
        * res: results dictionary from ARD-NMF (see ardnmf function)
        * cosmic: cosmic pd.DataFrmae
        * cosmic_index: feature index column in cosmic
            ** ex. in cosmic_v2, "Somatic Mutation Type" columns map to
                A[C>A]A, A[C>A]C, etc.

    Returns:
        * None, edits res dictionary directly
    """
    if cosmic_type in ('cosmic2','cosmic3','cosmic3_exome'):
        res["Wraw"]["mut"] = _map_sbs_sigs(res["Wraw"], cosmic).values
    elif cosmic_type == 'cosmic3_DBS':
        res["Wraw"]["mut"] = _map_dbs_sigs(res["Wraw"], cosmic).values
    elif cosmic_type == 'cosmic3_ID':
        res["Wraw"]["mut"] = _map_id_sigs(res["Wraw"]).values

    # Column names of NMF signatures & COSMIC References
    nmf_cols = ["S"+x for x in list(map(str, set(res["signatures"].max_id)))]
    ref_cols = list(cosmic.columns[cosmic.dtypes == 'float64'])

    # Create cosine similarity matrix
    X = res["Wraw"].set_index("mut").join(cosmic.set_index(cosmic_index)).dropna(1).loc[:,nmf_cols+ref_cols]
    res["cosine"] = pd.DataFrame(cosine_similarity(X.T), index=X.columns, columns=X.columns).loc[ref_cols,nmf_cols]

    # Add assignments
    s_assign = dict(res["cosine"].idxmax())
    s_assign = {key:key+"-" + s_assign[key] for key in s_assign}

    # Update column names
    res["W"] = res["W"].rename(columns=s_assign)
    res["H"] = res["H"].rename(columns=s_assign)
    res["Wraw"] = res["Wraw"].rename(columns=s_assign)
    res["Hraw"] = res["Hraw"].T.rename(columns=s_assign)
    res["cosine"] = res["cosine"].rename(columns=s_assign)

def assign_signature_weights_to_maf(maf: pd.DataFrame, W: pd.DataFrame, H: pd.DataFrame):
    """
    Assign probabilities for each signature to each mutation in the maf
    ------------------------
    Args:
        * maf: input maf as a pd.DataFrame
        * W: W-matrix from NMF
        * H: H-matrix from NMF

    Returns:
        Maf with columns appended to the end for the weights of each signature
    """
    sig_columns = W.columns[W.columns.str.startswith('S')]
    W_prob = W.loc[maf[W.index.name], sig_columns].reset_index(drop=True)
    H_prob = H.loc[maf['sample'], sig_columns].reset_index(drop=True)
    signature_probabilities = W_prob * H_prob
    signature_probabilities /= np.sum(signature_probabilities.values, axis=1, keepdims=True)
    maf[sig_columns] = signature_probabilities
    return maf

# ---------------------------------
# Parsing Output H5 Utils
# ---------------------------------
def get_nruns_from_output(file: str) -> int:
    """
    Get number of NMF runs from an .h5 output file.
    ------------------------
    Args:
        * output file from NMF (.h5)

    Returns:
        * number of NMF runs (int)
    """
    with h5py.File(file) as f:
        return(max([int(x.split('run')[1]) for x in list(f.keys()) if 'run' in x])+1)

def get_nlogs_from_output(file: str) -> pd.DataFrame:
    """
    Returns a dataframe of the final output log from each run.
    ------------------------
    Args:
        * output file from NMF (.h5)

    Returns:
        * pd.DataFrame of reporting statistics for each run
    """
    n_runs = get_nruns_from_output(file)
    df = pd.concat([pd.read_hdf(file,"run{}/log".format(i)).reset_index().iloc[-1] for i in range(n_runs)],1).T
    df.index = np.arange(n_runs)
    return df

# ---------------------------------
# Preprocessing input mafs
# ---------------------------------
def get_dnps_from_maf(maf: pd.DataFrame):
    """
    Get DNPs from a maf which has adjacent SNPs
    ________________________
    Args:
        * maf: maf DataFrame

    Returns:
        * pd.DataFrame of maf with only adjacent SNPs (annotated as DNPs)
    """
    sub_mafs = []
    for _, df in maf.loc[maf['Variant_Type'] == 'SNP'].groupby(['sample', 'Chromosome']):
        df = df.sort_values('Start_position')
        start_pos = np.array(df['Start_position'])
        pos_diff = np.diff(start_pos)
        idx = []
        if len(pos_diff) == 1 and pos_diff[0] == 1:
            idx.append(0)
        if len(pos_diff) >= 2 and pos_diff[0] == 1 and pos_diff[1] > 1:
            idx.append(0)
        idx.extend(np.flatnonzero((pos_diff[:-2] > 1) & (pos_diff[1:-1] == 1) & (pos_diff[2:] > 1)) + 1)
        if len(pos_diff) >= 2 and pos_diff[-1] == 1 and pos_diff[-2] > 1:
            idx.append(len(pos_diff) - 1)
        if idx:
            idx = np.array(idx)
            rows = df.iloc[idx][['Hugo_Symbol', 'Tumor_Sample_Barcode', 'sample',
                'Chromosome', 'Start_position', 'Reference_Allele', 'Tumor_Seq_Allele2']].reset_index(drop=True)
            rows_plus_one = df.iloc[idx + 1].reset_index()
            rows['Variant_Type'] = 'DNP'
            rows['End_position'] = rows['Start_position'] + 1
            rows['Reference_Allele'] = rows['Reference_Allele'] + rows_plus_one['Reference_Allele']
            rows['Tumor_Seq_Allele2'] = rows['Tumor_Seq_Allele2'] + rows_plus_one['Tumor_Seq_Allele2']
            sub_mafs.append(rows)
    return pd.concat(sub_mafs).reset_index(drop=True)

def get_true_snps_from_maf(maf: pd.DataFrame):
    """
    Get SNPs from a maf which has adjacent SNPs
    ________________________
    Args:
        * maf: maf DataFrame

    Returns:
        * pd.DataFrame of maf with adjacent SNPs filtered out
    """
    sub_mafs = []
    for _, df in maf.loc[maf['Variant_Type'] == 'SNP'].groupby(['sample', 'Chromosome']):
        df = df.sort_values('Start_position')
        start_pos = np.array(df['Start_position'])
        pos_diff = np.diff(start_pos)
        rem_idx = np.flatnonzero(pos_diff <= 1)
        rem_idx = np.concatenate([rem_idx, rem_idx + 1])
        idx = np.delete(np.arange(len(start_pos)), rem_idx)
        if len(idx):
            rows = df.iloc[idx]
            sub_mafs.append(rows)
    return pd.concat(sub_mafs).reset_index(drop=True)

max_stars_repo_name: julianhess/getzlab-SignatureAnalyzer

max_issues_repo_name: julianhess/getzlab-SignatureAnalyzer

max_forks_repo_name: julianhess/getzlab-SignatureAnalyzer


text:
"""
Test for file IO
"""
import pytest
import numpy as np

brbd_to_test = []
try:
    import biorbd
    brbd_to_test.append(biorbd)
except:
    pass
try:
    import biorbd_casadi
    brbd_to_test.append(biorbd_casadi)
except:
    pass

# --- Options --- #
@pytest.mark.parametrize("brbd", brbd_to_test)
def test_np_mx_to_generalized(brbd):
    biorbd_model = brbd.Model("../../models/pyomecaman.bioMod")

    q = brbd.GeneralizedCoordinates(biorbd_model)
    qdot = brbd.GeneralizedVelocity((biorbd_model.nbQdot()))
    qddot = brbd.GeneralizedAcceleration((biorbd_model.nbQddot()))
    tau = biorbd_model.InverseDynamics(q, qdot, qddot)
    biorbd_model.ForwardDynamics(q, qdot, tau)

    if brbd.currentLinearAlgebraBackend() == 1:
        tau = biorbd_model.InverseDynamics(q.to_mx(), qdot.to_mx(), qddot.to_mx())
        biorbd_model.ForwardDynamics(q, qdot, tau.to_mx())
    else:
        tau = biorbd_model.InverseDynamics(q.to_array(), qdot.to_array(), qddot.to_array())
        biorbd_model.ForwardDynamics(q, qdot, tau.to_array())

# --- Options --- #
@pytest.mark.parametrize("brbd", brbd_to_test)
def test_imu_to_array(brbd):
    m = brbd.Model("../../models/IMUandCustomRT/pyomecaman_withIMUs.bioMod")
    q = np.zeros((m.nbQ(),))

    if brbd.currentLinearAlgebraBackend() == 1:
        from casadi import MX

        q_sym = MX.sym("q", m.nbQ(), 1)
        imu_func = brbd.to_casadi_func("imu", m.IMU, q_sym)
        imu = imu_func(q)[:, :4]

    else:
        imu = m.IMU(q)[0].to_array()

    np.testing.assert_almost_equal(
        imu,
        np.array(
            [
                [0.99003329, -0.09933467, 0.09983342, 0.26719],
                [0.10925158, 0.98903828, -0.09933467, 0.04783],
                [-0.08887169, 0.10925158, 0.99003329, -0.20946],
                [0.0, 0.0, 0.0, 1.0],
            ]
        ),
    )

@pytest.mark.parametrize("brbd", brbd_to_test)
def test_vector3d(brbd):
    biorbd_model = brbd.Model()
    vec = np.random.rand(
        3,
    )
    biorbd_model.setGravity(vec)

    if brbd.currentLinearAlgebraBackend() == 1:
        from casadi import MX

        vec = MX.ones(3, 1)
        biorbd_model.setGravity(vec)

max_stars_repo_name: aceglia/biorbd

max_issues_repo_name: aceglia/biorbd


text:
# coding=utf-8
# Copyright 2019 The Interval Bound Propagation Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Tests for CROWN bounds."""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import interval_bound_propagation as ibp
import numpy as np
import sonnet as snt
import tensorflow.compat.v1 as tf

def _generate_identity_spec(modules, shape, dimension=1):
  spec = ibp.LinearSpecification(tf.reshape(tf.eye(dimension), shape),
                                 prune_irrelevant=False)
  initial_bound = ibp.crown.create_initial_backward_bounds(spec, modules)
  return initial_bound

class CROWNBoundsTest(tf.test.TestCase):

  def testFCBackwardBounds(self):
    m = snt.Linear(1, initializers={
        'w': tf.constant_initializer(1.),
        'b': tf.constant_initializer(2.),
    })
    z = tf.constant([[1, 2, 3]], dtype=tf.float32)
    m(z)  # Connect to create weights.
    m = ibp.LinearFCWrapper(m)
    input_bounds = ibp.IntervalBounds(z - 1., z + 1.)
    m.propagate_bounds(input_bounds)  # Create IBP bounds.
    crown_init_bounds = _generate_identity_spec([m], shape=(1, 1, 1))
    output_bounds = m.propagate_bounds(crown_init_bounds)
    concrete_bounds = output_bounds.concretize()
    with self.test_session() as sess:
      sess.run(tf.global_variables_initializer())
      lw, uw, lb, ub, cl, cu = sess.run([output_bounds.lower.w,
                                         output_bounds.upper.w,
                                         output_bounds.lower.b,
                                         output_bounds.upper.b,
                                         concrete_bounds.lower,
                                         concrete_bounds.upper])
      self.assertTrue(np.all(lw == 1.))
      self.assertTrue(np.all(lb == 2.))
      self.assertTrue(np.all(uw == 1.))
      self.assertTrue(np.all(ub == 2.))
      cl = cl.item()
      cu = cu.item()
      self.assertAlmostEqual(5., cl)
      self.assertAlmostEqual(11., cu)

  def testConv2dBackwardBounds(self):
    m = snt.Conv2D(
        output_channels=1,
        kernel_shape=(2, 2),
        padding='VALID',
        stride=1,
        use_bias=True,
        initializers={
            'w': tf.constant_initializer(1.),
            'b': tf.constant_initializer(2.),
        })
    z = tf.constant([1, 2, 3, 4], dtype=tf.float32)
    z = tf.reshape(z, [1, 2, 2, 1])
    m(z)  # Connect to create weights.
    m = ibp.LinearConv2dWrapper(m)
    input_bounds = ibp.IntervalBounds(z - 1., z + 1.)
    m.propagate_bounds(input_bounds)   # Create IBP bounds.
    crown_init_bounds = _generate_identity_spec([m], shape=(1, 1, 1, 1, 1))
    output_bounds = m.propagate_bounds(crown_init_bounds)
    concrete_bounds = output_bounds.concretize()
    with self.test_session() as sess:
      sess.run(tf.global_variables_initializer())
      l, u = sess.run([concrete_bounds.lower, concrete_bounds.upper])
      l = l.item()
      u = u.item()
      self.assertAlmostEqual(8., l)
      self.assertAlmostEqual(16., u)

  def testReluBackwardBounds(self):
    m = tf.nn.relu
    z = tf.constant([[-2, 3]], dtype=tf.float32)
    m = ibp.IncreasingMonotonicWrapper(m)
    input_bounds = ibp.IntervalBounds(z - 1., z + 1.)
    m.propagate_bounds(input_bounds)  # Create IBP bounds.
    crown_init_bounds = _generate_identity_spec([m], shape=(1, 2, 2),
                                                dimension=2)
    output_bounds = m.propagate_bounds(crown_init_bounds)
    concrete_bounds = output_bounds.concretize()
    with self.test_session() as sess:
      l, u = sess.run([concrete_bounds.lower, concrete_bounds.upper])
      self.assertAlmostEqual([[0., 2.]], l.tolist())
      self.assertAlmostEqual([[0., 4.]], u.tolist())

if __name__ == '__main__':
  tf.test.main()

max_stars_repo_name: cknabs/interval-bound-propagation

max_issues_repo_name: Qiming-Wu/interval-bound-propagation

max_forks_repo_name: Qiming-Wu/interval-bound-propagation


text:
#!/usr/bin/env python

from model import Model
from effectlayer import EffectParameters
from renderer import Renderer
import os
import socket
import time
import sys
import numpy
import math
import struct
import logging

logging.basicConfig()
logger = logging.getLogger()

class AnimationController(object):
    """Manages the main animation loop. Calls into the renderer to populate
       a frame of LED data, then sends it to the OPC server. This class manages frame
       rate control, and handles the advancement of time in EffectParameters.
       """

    def __init__(self, model, renderer, params=None, server=None):
        self.opc = FastOPC(server)
        self.model = model
        self.renderer = renderer
        self.params = params or EffectParameters()

        self._fpsFrames = 0
        self._fpsTime = 0
        self._fpsLogPeriod = 1    # How often to log frame rate

    def advanceTime(self):
        """Update the timestep in EffectParameters.

           This is where we enforce our target frame rate, by sleeping until the minimum amount
           of time has elapsed since the previous frame. We try to synchronize our actual frame
           rate with the target frame rate in a slightly loose way which allows some jitter in
           our clock, but which keeps the frame rate centered around our ideal rate if we can keep up.

           This is also where we log the actual frame rate to the console periodically, so we can
           tell how well we're doing.
           """

        now = time.time()
        dt = now - self.params.time
        dtIdeal = 1.0 / self.params.targetFrameRate

        if dt > dtIdeal * 2:
            # Big jump forward. This may mean we're just starting out, or maybe our animation is
            # skipping badly. Jump immediately to the current time and don't look back.

            self.params.time = now

        else:
            # We're approximately keeping up with our ideal frame rate. Advance our animation
            # clock by the ideal amount, and insert delays where necessary so we line up the
            # animation clock with the real-time clock.

            self.params.time += dtIdeal
            if dt < dtIdeal:
                time.sleep(dtIdeal - dt)

        # Log frame rate

        self._fpsFrames += 1
        if now > self._fpsTime + self._fpsLogPeriod:
            fps = self._fpsFrames / (now - self._fpsTime)
            self._fpsTime = now
            self._fpsFrames = 0
            logger.debug("%7.2f FPS" % fps)

    def renderLayers(self):
        """Generate a complete frame of LED data by rendering each layer."""

        # Note: You'd think it would be faster to use float32 on the rPI, but
        #       32-bit floats take a slower path in NumPy sadly.
        # Also: Numpy makes float64s by default
        frame = numpy.zeros((self.model.numLEDs, 3))

        self.renderer.render(self.model, self.params, frame)
        return frame

    def frameToHardwareFormat(self, frame):
        """Convert a frame in our abstract floating-point format to the specific format used
           by the OPC server. Does not clip to the range [0,255], this is handled by FastOPC.

           Modifies 'frame' in-place.
           """
        numpy.multiply(frame, 255, frame)

    def drawFrame(self):
        """Render a frame and send it to the OPC server"""
        self.advanceTime()
        pixels = self.renderLayers()
        self.frameToHardwareFormat(pixels)
        self.opc.putPixels(0, pixels)

    def drawingLoop(self):
        """Render frames forever or until keyboard interrupt"""
        try:
            while True:
                self.drawFrame()
        except KeyboardInterrupt:
            pass

class FastOPC(object):
    """High-performance Open Pixel Control client, using Numeric Python.
       By default, assumes the OPC server is running on localhost. This may be overridden
       with the OPC_SERVER environment variable, or the 'server' keyword argument.
       """

    def __init__(self, server=None):
        self.server = server or os.getenv('OPC_SERVER') or '127.0.0.1:7890'
        self.host, port = self.server.split(':')
        self.port = int(port)
        self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.socket.connect((self.host, self.port))

    def putPixels(self, channel, pixels):
        """Send a list of 8-bit colors to the indicated channel. (OPC command 0x00).
           'Pixels' is an array of any shape, in RGB order. Pixels range from 0 to 255.

           They need not already be clipped to this range; that's taken care of here.
           'pixels' is clipped in-place. If any values are out of range, the array is modified.
           """

        numpy.clip(pixels, 0, 255, pixels)
        packedPixels = pixels.astype('B').tostring()
        header = struct.pack('>BBH',
            channel,
            0x00,  # Command
            len(packedPixels))
        self.socket.send(header + packedPixels)

max_stars_repo_name: FlamingLotusGirls/soma

max_issues_repo_name: FlamingLotusGirls/soma

max_forks_repo_name: FlamingLotusGirls/soma


text:
import os
import time

import subprocess
import argparse
import glob
import psutil

from pathlib import Path

import numpy as np
import pandas as pd
from tqdm import tqdm

from utils.monitors import ResourceMonitor, TurboStatMonitor, PCMMonitor

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "-b", "--bin",
        help="Path to the benchmark app binary",
        type=str,
        default='./bin/benchmark_app'
    )
    parser.add_argument(
        "-d", "--device",
        help="Device to run the benchmark.",
        type=str,
        default='CPU'
    )
    parser.add_argument(
        "-m", "--model",
        help="Path to the model or model's directory to use.",
        type=str,
        default='./models/'
    )
    parser.add_argument(
        "-c", "--config",
        help="Config file from which configurations of {cores, requests, streams, and batch size} are read.",
        type=str,
        # required=True
    )
    parser.add_argument(
        "-o", "--output",
        help="Path to output directory file where results are stored.",
        default="./results/",
        type=str
    )
    parser.add_argument(
        "--prefix",
        help="Prefix to name the different output files generated.",
        type=str,
        default=None
    )
    parser.add_argument(
        "-t",
        help="Duration of each experiment.",
        type=int,
        default=2
    )
    parser.add_argument(
        "-p", "--precision",
        help="Specifies precision of the model, if one model is set. If a directory, it will select only models of the selected precision.",
        type=str,
        default=None
    )
    parser.add_argument(
        "--source",
        help="File with environment variables to 'source'.",
        type=str,
        default="/opt/intel/openvino/bin/setupvars.sh"
    )

    args = parser.parse_args()

    if 'xml' in args.model:
        if not os.path.isfile(args.model):
            raise ValueError('{} does not exist.'.format(args.model))
        models = [Path(args.model)]
    elif os.path.isdir(args.model):
        models = [ path for path in Path(args.model).rglob('*.xml')]
        if args.precision:
            models = [
                model for model in models
                if args.precision.lower() in str(model).lower()
            ]
    else:
        raise ValueError(
            '{} is not a valid directory nor xml file.'.format(args.model)
        )

    if args.precision is None:
        args.precision = "fp32*"

    # exec(open(args.source).read())
    # subprocess.Popen('source {}'.format(args.source), shell=True)
    # ld_path = Path(__file__).parent.absolute() / 'bin/lib'
    openvino_path = "/opt/intel/openvino_2021/deployment_tools"
    ld_paths = [
        "{}/bin/lib".format(os.getcwd()),
        '{}/inference_engine/external/tbb/lib/'.format(openvino_path),
        '{}/inference_engine/lib/intel64/'.format(openvino_path),
        '{}/ngraph/lib/'.format(openvino_path)
    ]
    os.environ['LD_LIBRARY_PATH'] = ':'.join(ld_paths)

    cpu_count = psutil.cpu_count(logical=False)

    if args.config is not None:
        configs = pd.read_csv(args.config)
        configs.fillna(0, inplace=True)
        for col in configs.columns:
            configs[col] = configs[col].astype(int)
    else:
        default_config = [[cpu_count, 1, 1, 1]]
        configs = pd.DataFrame(default_config, columns=['cores', 'streams', 'requests', 'batch'])

    #System monitors
    cpu_monitor = ResourceMonitor()
    rapl_monitor = TurboStatMonitor()
    pcm_monitor = PCMMonitor()

    proc = psutil.Process()
    benchmark_stats = []
    benchmark_metrics = ['Count', 'Duration', 'Latency', 'Throughput']

    configs_per_model = len(configs)
    total_runs = len(models) * configs_per_model

    with tqdm(total=total_runs, desc="Total runs") as pbar:
        for model in tqdm(models, desc='Models to run', total=len(models)):
            model_name = model.stem
            for _, cores, nstreams, nireq, batch in tqdm(configs.itertuples(), desc='Runs with {}'.format(model_name), total=configs_per_model, leave=False):
                if not cores:
                    cores = cpu_count
                if not nstreams:
                    nstreams = cpu_count
                if not nireq:
                    nireq = nstreams
                if not batch:
                    batch = 1

                proc.cpu_affinity(list(np.arange(0, cores)))

                pcm_monitor.start(interval=1.0)
                cpu_monitor.start(interval=1.0)
                rapl_monitor.start(interval=1.0)
                cmd = [
                    args.bin,
                    '-m', str(model),
                    '-d', args.device,
                    '-nireq', str(nireq),
                    '-b', str(batch),
                    '-t', str(args.t)
                ]

                if args.device not in ['MYRIAD', 'GPU']:
                    cmd += ['-nstreams', str(nstreams)]

                subproc = subprocess.Popen(
                        cmd,
                        stdout=subprocess.PIPE,
                        stderr=subprocess.PIPE
                )

                out, err = subproc.communicate()

                cpu_monitor.stop(checkpoint=True)
                rapl_monitor.stop(checkpoint=True)
                pcm_monitor.stop(checkpoint=True)

                stats = [model_name, cores, nstreams, nireq, batch, args.precision]
                for line in out.decode('utf-8').split('\n')[-10:]:
                    if not any(metric in line for metric in benchmark_metrics):
                        continue

                    metric, value = line.split(':')
                    stats.append(float(value.strip().split(' ')[0]))

                benchmark_stats.append(stats)

                # print(j)
                # progress = i*configs_per_model + j
                pbar.update(1)

    benchmark_metrics = ['Model', 'CPUs', 'streams', 'requests', 'batch_size', 'precision'] + benchmark_metrics
    df = pd.DataFrame(benchmark_stats, columns=benchmark_metrics)

    if args.prefix:
        output_file = args.output + '/' + args.prefix + '-'
    else:
        output_file = args.output + '/'

    df.to_csv('{}summary.csv'.format(output_file), sep=',', index=False, float_format='%.3f')
    df = pd.concat([df, cpu_monitor.checkpoints, rapl_monitor.checkpoints, pcm_monitor.checkpoints], axis=1, sort=False)
    df.to_csv('{}detailed.csv'.format(output_file), sep=',', index=False, float_format='%.3f')

if __name__ == '__main__':
    main()

max_stars_repo_name: HiEST/py-sysmon

max_issues_repo_name: HiEST/py-sysmon

max_forks_repo_name: HiEST/py-sysmon


text:
import copy
import numpy as np
import chess as python_chess
from collections import deque
from typing import Optional, List

from src.config import ConfigChess
from src.chess.move import Move

class Board(python_chess.Board):
    def __init__(
        self,
        board_fen: Optional[str] = None,
        array: Optional[np.ndarray] = None,
        history_size: int = 8,
    ):
        self.board_size = ConfigChess.board_size
        self.number_unique_pieces = ConfigChess.number_unique_pieces
        if array is not None:
            assert isinstance(array, np.ndarray)
            assert all([dim == self.board_size for dim in array.shape])
            assert np.unique(array).size <= self.number_unique_pieces + 1
            self.array = array.astype("int8")
            board_fen = self.array_to_board_fen(self.array)
            fen = self.get_fen(board_fen)
            python_chess.Board.__init__(self, fen=fen)
        else:
            board_fen = (
                ConfigChess.initial_board_fen if board_fen is None else board_fen
            )
            fen = self.get_fen(board_fen)
            python_chess.Board.__init__(self, fen=fen)
            self.array = self.board_fen_to_array(self.board_fen())
        self.state_history = deque(maxlen=history_size)
        for time_step in range(history_size):
            self.state_history.append(np.zeros(self.state.shape))
        self.state_history.append(self.state)

    @property
    def array_one_hot(self) -> np.ndarray:
        return np.eye(self.number_unique_pieces + 1)[self.array]

    @property
    def moves(self) -> List[Move]:
        return [Move(uci=move.uci()) for move in self.legal_moves]

    @property
    def state(self) -> np.ndarray:
        return np.dstack(
            [
                self.array_one_hot,
                np.full((self.board_size, self.board_size), self.is_repetition()),
            ]
        )

    @property
    def full_state(self) -> np.ndarray:
        return np.dstack(
            [np.dstack(self.state_history)]
            + [
                np.full((self.board_size, self.board_size), feature)
                for feature in [
                    self.has_queenside_castling_rights(self.turn),
                    self.has_kingside_castling_rights(self.turn),
                    self.has_queenside_castling_rights(not self.turn),
                    self.has_kingside_castling_rights(not self.turn),
                    self.fullmove_number,
                    self.halfmove_clock,
                ]
            ]
        )

    @staticmethod
    def get_fen(board_fen: str):
        return " ".join(
            [
                board_fen,
                ConfigChess.initial_turn,
                ConfigChess.initial_castling_rights,
                ConfigChess.initial_ep_quare,
                ConfigChess.initial_halfmove_clock,
                ConfigChess.initial_fullmove_number,
            ]
        )

    @staticmethod
    def from_one_hot(array_oh: np.ndarray) -> np.ndarray:
        array = np.argmax(array_oh, axis=-1)
        array[np.where(array > ConfigChess.number_unique_pieces / 2)] = (
            array[np.where(array > ConfigChess.number_unique_pieces / 2)]
            - ConfigChess.number_unique_pieces
            + 1
        )
        return array

    @staticmethod
    def piece_symbol_to_int(piece_symbol: Optional[str]) -> int:
        if piece_symbol is None:
            return 0
        piece_int = ConfigChess.piece_symbols.index(piece_symbol.lower())
        player = 1 if piece_symbol.isupper() else -1
        return player * piece_int

    @staticmethod
    def int_to_piece_symbol(piece_int: int) -> Optional[str]:
        player, piece_symbol = (
            np.sign(piece_int),
            ConfigChess.piece_symbols[np.abs(piece_int)],
        )
        if piece_symbol is None:
            return piece_symbol
        return piece_symbol if player < 0 else piece_symbol.upper()

    def legal_moves_mask(self, all_possible_moves: List[Move]) -> np.ndarray:
        return np.asarray([move in self.moves for move in all_possible_moves])

    def board_fen_to_array(self, fen: str) -> np.ndarray:
        mat = []
        board_fen = fen.replace("/", "")
        for elem in board_fen:
            if elem.isdigit():
                mat.extend(int(elem) * [0])
            else:
                mat.append(self.piece_symbol_to_int(elem))
        return (
            np.asarray(mat).reshape((self.board_size, self.board_size)).astype("int8")
        )

    def array_to_board_fen(self, array: np.ndarray) -> str:
        fen = ""
        cases_counter, empty_cases_counter = 0, 0
        for piece_int in np.nditer(array):
            piece_symbol = self.int_to_piece_symbol(piece_int)
            if piece_symbol is None:
                empty_cases_counter += 1
            else:
                if empty_cases_counter > 0:
                    fen += str(empty_cases_counter)
                    empty_cases_counter = 0
                fen += piece_symbol
            cases_counter += 1
            if cases_counter % self.board_size == 0:
                if empty_cases_counter:
                    fen += str(empty_cases_counter)
                if cases_counter != self.board_size * self.board_size:
                    fen += "/"
                empty_cases_counter = 0
        return fen

    def update_array(self):
        self.array = self.board_fen_to_array(self.board_fen())
        self.state_history.append(self.state)

    def get_random_move(self) -> Optional[Move]:
        try:
            return np.random.choice(self.moves)
        except ValueError:
            return None

    def play(
        self, move: Move, on_copy: bool = False, keep_same_player: bool = False
    ) -> "Board":
        board = copy.deepcopy(self) if on_copy else self
        board.push_uci(move.uci)
        if keep_same_player:
            board = board.mirror()
            board.turn = True  # virtually, it is always white to play
        board.update_array()
        if not on_copy:
            self.__dict__.update(board.__dict__)
        return board

    def play_random(self) -> "Board":
        return self.play(self.get_random_move())

    def get_result(self):
        if not self.is_game_over():
            return None
        result = self.result()
        # 1-0 or 0-1
        if len(result) == 3:
            if result[0] > result[1]:
                return 1
            else:
                return -1
        # 1/2-1/2
        elif len(result) == 7:
            return 0

    def display_ascii(self):
        for row in self.array:
            print(
                "".join(map(lambda x: self.int_to_piece_symbol(x) if x else ".", row))
            )

max_stars_repo_name: Dalkio/custom-alphazero

max_issues_repo_name: Dalkio/custom-alphazero

max_forks_repo_name: Dalkio/custom-alphazero


text:
from typing import List

import numpy as np
from napari.layers import Layer
from qtpy.QtCore import QEvent
from qtpy.QtWidgets import QToolTip
from vispy.app import MouseEvent

from ..common_gui.channel_control import ChannelProperty
from ..common_gui.napari_image_view import ImageView

class StackImageView(ImageView):
    """
    :cvar settings: StackSettings
    """

    def __init__(self, settings, channel_property: ChannelProperty, name: str):
        super().__init__(settings, channel_property, name)
        self.viewer_widget.canvas.events.mouse_press.connect(self.component_click)
        self.additional_layers: List[Layer] = []
        # self.image_area.pixmap.click_signal.connect(self.component_click)

    def component_unmark(self, _num):
        self.viewer.layers.unselect_all()
        for el in self.additional_layers:
            el.selected = True
        self.viewer.layers.remove_selected()
        self.additional_layers = []

    def component_mark(self, num):
        self.component_unmark(num)

        for image_info in self.image_info.values():
            bound_info = image_info.segmentation_info.bound_info.get(num, None)
            if bound_info is None:
                continue
            # TODO think about marking on bright background
            slices = bound_info.get_slices()
            slices[image_info.image.stack_pos] = slice(None)
            component_mark = image_info.segmentation_info.segmentation[tuple(slices)] == num
            translate_grid = image_info.segmentation.translate_grid + (bound_info.lower) * image_info.segmentation.scale
            translate_grid[image_info.image.stack_pos] = 0
            self.additional_layers.append(
                self.viewer.add_image(
                    component_mark,
                    scale=image_info.segmentation.scale,
                    blending="additive",
                    colormap="gray",
                    opacity=0.5,
                )
            )
            self.additional_layers[-1].translate_grid = translate_grid

    def component_click(self, event: MouseEvent):
        cords = np.array([int(x) for x in self.viewer.active_layer.coordinates])
        for image_info in self.image_info.values():
            if image_info.segmentation_info.segmentation is None:
                continue
            if not image_info.coords_in(cords):
                continue
            moved_coords = image_info.translated_coords(cords)
            component = image_info.segmentation_info.segmentation[tuple(moved_coords)]
            if component:
                self.component_clicked.emit(component)

    def event(self, event: QEvent):
        if event.type() == QEvent.ToolTip and self.components:
            # text = str(self.component)
            text_list = []
            for el in self.components:
                if self.settings.component_is_chosen(el):
                    text_list.append("☑{}".format(el))
                else:
                    text_list.append("☐{}".format(el))
            QToolTip.showText(event.globalPos(), " ".join(text_list))
        return super().event(event)

max_stars_repo_name: monotropauniflora/PartSeg

max_issues_repo_name: monotropauniflora/PartSeg

max_forks_repo_name: monotropauniflora/PartSeg