def distance_matrix(data, distance_constructor=Euclidean, progress_callback=None):
    """ A helper function that computes an :obj:`Orange.misc.SymMatrix` of all
    pairwise distances between instances in `data`.
    
    :param data: A data table
    :type data: :obj:`Orange.data.Table`
    
    :param distance_constructor: An DistanceConstructor instance (defaults to :obj:`Euclidean`).
    :type distance_constructor: :obj:`Orange.distances.DistanceConstructor`

    :param progress_callback: A function (taking one argument) to use for
        reporting the on the progress.
    :type progress_callback: function
    
    :rtype: :class:`Orange.misc.SymMatrix`
    
    """
    matrix = Orange.misc.SymMatrix(len(data))
    dist = distance_constructor(data)

    iter_count = matrix.dim * (matrix.dim - 1) / 2
    milestones = progress_bar_milestones(iter_count, 100)
    
    for count, ((i, ex1), (j, ex2)) in enumerate(_pairs(enumerate(data))):
        matrix[i, j] = dist(ex1, ex2)
        if progress_callback and count in milestones:
            progress_callback(100.0 * count / iter_count)
            
    return matrix 
Esempio n. 2
0
def distance_matrix(data,
                    distance_constructor=Euclidean,
                    progress_callback=None):
    """ A helper function that computes an :obj:`Orange.misc.SymMatrix` of all
    pairwise distances between instances in `data`.
    
    :param data: A data table
    :type data: :obj:`Orange.data.Table`
    
    :param distance_constructor: An DistanceConstructor instance (defaults to :obj:`Euclidean`).
    :type distance_constructor: :obj:`Orange.distances.DistanceConstructor`

    :param progress_callback: A function (taking one argument) to use for
        reporting the on the progress.
    :type progress_callback: function
    
    :rtype: :class:`Orange.misc.SymMatrix`
    
    """
    matrix = Orange.misc.SymMatrix(len(data))
    dist = distance_constructor(data)

    iter_count = matrix.dim * (matrix.dim - 1) / 2
    milestones = progress_bar_milestones(iter_count, 100)

    for count, ((i, ex1), (j, ex2)) in enumerate(_pairs(enumerate(data))):
        matrix[i, j] = dist(ex1, ex2)
        if progress_callback and count in milestones:
            progress_callback(100.0 * count / iter_count)

    return matrix
    def compute_distances(self, separate_keys, partitions, data):
        """ Compute the distances between genotypes.
        """
        if separate_keys and partitions:
            self.progressBarInit()
            matrix = Orange.core.SymMatrix(len(partitions))
            profiles = [linearize(data, indices) for _, indices in partitions]
            dist_func = self.DISTANCE_FUNCTIONS[self.distance_measure][1]
            from Orange.utils import progress_bar_milestones

            count = (len(profiles) * len(profiles) - 1) / 2
            milestones = progress_bar_milestones(count)
            iter_count = 0
            for i in range(len(profiles)):
                for j in range(i + 1, len(profiles)):
                    matrix[i, j] = dist_func(profiles[i], profiles[j])
                    iter_count += 1
                    if iter_count in milestones:
                        self.progressBarSet(100.0 * iter_count / count)
            self.progressBarFinished()

            items = [
                ["{0}={1}".format(key, value) for key, value in zip(separate_keys, values)] for values, _ in partitions
            ]
            items = [" | ".join(item) for item in items]
            matrix.setattr("items", items)
        else:
            matrix = None

        self.matrix = matrix
Esempio n. 4
0
    def compute_combined(self):
        self.error(0)
        dim = 0
        matrices = self.matrices.values()
        new = None
        if matrices and len(set(m.dim for m in matrices)) != 1:
            # Do dimensions match
            self.error(0, "Matrices are of different dimensions.")
        elif matrices:
            dim = matrices[0].dim
            method = self.METHODS[self.selected_method][1]
            new = Orange.misc.SymMatrix(dim)
            milestones = progress_bar_milestones(100, dim * (dim + 1) / 2)
            iter = 0
            for i in range(dim):
                for j in range(i, dim):
                    vals = [m[i, j] for m in matrices]
                    new[i, j] = method(vals)

            items = [getattr(m, "items") for m in matrices]
            items = filter(None, items)
            if items:
                new.setattr("items", items[0])

        self.send("Combined Matrix", new)
Esempio n. 5
0
    def compute_combined(self):
        self.error(0)
        dim = 0
        matrices = self.matrices.values()
        new = None
        if matrices and len(set(m.dim for m in matrices)) != 1:
            # Do dimensions match
            self.error(0, "Matrices are of different dimensions.")
        elif matrices:
            dim = matrices[0].dim
            method = self.METHODS[self.selected_method][1]
            new = Orange.misc.SymMatrix(dim)
            milestones = progress_bar_milestones(100, dim * (dim + 1) / 2)
            iter = 0
            for i in range(dim):
                for j in range(i, dim):
                    vals = [m[i, j] for m in matrices]
                    new[i, j] = method(vals)

            items = [getattr(m, "items") for m in matrices]
            items = filter(None, items)
            if items:
                new.setattr("items", items[0])

        self.send("Combined Matrix", new)
Esempio n. 6
0
    def compute_distances(self, separate_keys, partitions, data):
        """ Compute the distances between genotypes.
        """
        if separate_keys and partitions:
            self.progressBarInit()
            matrix = Orange.core.SymMatrix(len(partitions))
            profiles = [linearize(data, indices) for _, indices in partitions]
            dist_func = self.DISTANCE_FUNCTIONS[self.distance_measure][1]
            from Orange.utils import progress_bar_milestones
            count = (len(profiles) * len(profiles) - 1) / 2
            milestones = progress_bar_milestones(count)
            iter_count = 0
            for i in range(len(profiles)):
                for j in range(i + 1, len(profiles)):
                    matrix[i, j] = dist_func(profiles[i], profiles[j])
                    iter_count += 1
                    if iter_count in milestones:
                        self.progressBarSet(100.0 * iter_count / count)
            self.progressBarFinished()

            items = [["{0}={1}".format(key, value) for key, value in zip(separate_keys, values)] \
                      for values, _ in partitions]
            items = [" | ".join(item) for item in items]
            matrix.setattr("items", items)
        else:
            matrix = None

        self.matrix = matrix