Ignore:
Timestamp:
02/06/12 16:33:23 (2 years ago)
Author:
markotoplak
Branch:
default
Message:

Moved instance_distance_matrix to Orange.distance.distance_matrix

File:
1 edited

Legend:

Unmodified
Added
Removed
  • Orange/distance/__init__.py

    r9723 r9752  
    11import Orange 
    2  
    3 #%s/ExamplesDistanceConstructor/DistanceConstructor/gc 
    4 #%s/ExamplesDistance_Normalized/DistanceNormalized/gc 
    5 #ExampleDistance -> Distance 
    6 #Hamming -> HammingDistance 
    7 #DTW -> DTWDistance 
    8 #Euclidean -> EuclideanDistance 
    9 #Manhattan -> ... 
    10 #Maximal -> ... 
    11 #Relief -> .. 
    12 #DTWConstructor 
    13 #EuclideanConstructor 
    14 #HammingConstructor 
    15 #ManhattanConstructor 
    16 #MaximalConstructor 
    17 #ReliefConstructor 
    18 #PearsonRConstructor -> PearsonR 
    19 #PearsonR -> PearsonRDistance 
    20 #SpearmanRConstructor -> SpearmanR 
    21 #SpearmanR -> SpearmanRDistance 
    22 #MahalanobisConstructor ->  Mahalanobis 
    23 #Mahalanobis -> MahalanobisDistance 
    242 
    253from Orange.core import \ 
     
    4220    ExamplesDistanceConstructor_Relief as Relief 
    4321 
     22from Orange.misc import progress_bar_milestones 
     23 
    4424import statc 
    4525import numpy 
     
    260240            return 1.0 
    261241     
    262      
    263 def distance_matrix(data, distance_constructor, progress_callback=None): 
    264     """ A helper function that computes an obj:`Orange.core.SymMatrix` of all 
     242def _pairs(seq, same = False): 
     243    """ Return all pairs from elements of `seq`. 
     244    """ 
     245    seq = list(seq) 
     246    same = 0 if same else 1 
     247    for i in range(len(seq)): 
     248        for j in range(i + same, len(seq)): 
     249            yield seq[i], seq[j] 
     250    
     251def distance_matrix(data, distance_constructor=Euclidean, progress_callback=None): 
     252    """ A helper function that computes an :obj:`Orange.data.SymMatrix` of all 
    265253    pairwise distances between instances in `data`. 
    266254     
     
    268256    :type data: :obj:`Orange.data.Table` 
    269257     
    270     :param distance_constructor: An DistanceConstructor instance. 
     258    :param distance_constructor: An DistanceConstructor instance (defaults to :obj:`Euclidean`). 
    271259    :type distance_constructor: :obj:`Orange.distances.DistanceConstructor` 
    272      
    273     """ 
    274     from Orange.misc import progressBarMilestones as progress_milestones 
    275     matrix = Orange.core.SymMatrix(len(data)) 
     260 
     261    :param progress_callback: A function (taking one argument) to use for 
     262        reporting the on the progress. 
     263    :type progress_callback: function 
     264     
     265    :rtype: :class:`Orange.data.SymMatrix` 
     266     
     267    """ 
     268    matrix = Orange.data.SymMatrix(len(data)) 
    276269    dist = distance_constructor(data) 
    277      
    278     msize = len(data)*(len(data) - 1)/2 
    279     milestones = progress_milestones(msize, 100) 
    280     count = 0 
    281     for i in range(len(data)): 
    282         for j in range(i + 1, len(data)): 
    283             matrix[i, j] = dist(data[i], data[j]) 
     270 
     271    iter_count = matrix.dim * (matrix.dim - 1) / 2 
     272    milestones = progress_bar_milestones(iter_count, 100) 
     273     
     274    for count, ((i, ex1), (j, ex2)) in enumerate(_pairs(enumerate(data))): 
     275        matrix[i, j] = dist(ex1, ex2) 
     276        if progress_callback and count in milestones: 
     277            progress_callback(100.0 * count / iter_count) 
    284278             
    285             if progress_callback and count in milestones: 
    286                 progress_callback(100.0 * count / msize) 
    287             count += 1 
    288              
    289     return matrix 
     279    return matrix  
Note: See TracChangeset for help on using the changeset viewer.