Ignore:
Timestamp:
08/14/24 02:48:39 (4 weeks ago)
Author:
Maciej Komosinski
Message:

Minor optimizations

File:
1 edited

Legend:

Unmodified
Added
Removed
  • framspy/dissimilarity/density_distribution.py

    r1322 r1325  
    4141
    4242
    43     def calculateNeighberhood(self,array,mean_coords):
     43    def calculateNeighborhood(self,array,mean_coords):
    4444        """ Calculates number of elements for given sample and set ups the center of this sample
    4545        to the center of mass (calculated by mean of every coordinate)
     
    5555        weight = len(array)
    5656        if weight > 0:
    57             point = [np.mean(array[:,0]),np.mean(array[:,1]),np.mean(array[:,2])]
     57            point = np.mean(array, axis=0)  # equivalent to [np.mean(array[:,0]),np.mean(array[:,1]),np.mean(array[:,2])]
    5858            return weight, point
    5959        else:
     
    6262
    6363    def calculateDistPoints(self,point1, point2):
    64         """ Returns euclidean distance between two points
     64        """ Returns Euclidean distance between two points
    6565        Args (distribution):
    6666            point1 ([float,float,float]) - coordinates of first point
     
    7171
    7272        Returns:
    73             [float]: euclidean distance
     73            [float]: Euclidean distance
    7474        """
    7575        if self.frequency:
    76             return abs(point1-point2)
     76            return abs(point1-point2) # TODO vector instead of scalar returned?
    7777        else:
    78             return np.sqrt(np.sum(np.square(point1-point2)))
     78            return np.linalg.norm(point1-point2, ord=2)
    7979
    8080
     
    107107        """
    108108        lens = len(s1)
    109         indices = []
    110         for i in range(lens):
    111             if s1[i]==0 and s2[i]==0:
    112                     indices.append(i)
     109        indices = [i for i in range(lens) if s1[i]==0 and s2[i]==0]
    113110
    114111        return np.delete(s1, indices), np.delete(s2, indices)
     
    126123        """
    127124        lens = len(s1[0])
    128         indices = []
    129         for i in range(lens):
    130             if s1[1][i]==0 and s2[1][i]==0:
    131                 indices.append(i)
     125        indices = [i for i in range(lens) if s1[1][i]==0 and s2[1][i]==0]
    132126
    133127        s1 = [np.delete(s1[0], indices, axis=0), np.delete(s1[1], indices, axis=0)]
     
    168162                        feature_array.append(len(array[rows]))
    169163                    else:
    170                         weight, point = self.calculateNeighberhood(array[rows],[edges_x[x]+step_x_half,edges_y[y]+step_y_half,edges_z[z]+step_z_half])
     164                        weight, point = self.calculateNeighborhood(array[rows],[edges_x[x]+step_x_half,edges_y[y]+step_y_half,edges_z[z]+step_z_half])
    171165                        feature_array.append(point)
    172166                        weight_array.append(weight)
     
    180174
    181175    def getSignaturesForPair(self,array1,array2):
    182         """Generates signatures for given pair of models represented by array of voxels.
     176        """Generates signatures for a given pair of models represented by array of voxels.
    183177        We calculate space for given models by taking the extremas for each axis and dividing the space by the resolution.
    184178        This divided space generate us samples which contains points. Each sample will have new coordinates which are mean of all points from it and weight which equals to the number of points.
     
    248242
    249243    def calculateDissimforVoxels(self, voxels1, voxels2):
    250         """Calculates EMD for pair of voxels representing models.
     244        """Calculates EMD for a pair of voxels representing models.
    251245        Args:
    252246            voxels1 np.array([np.array(,dtype=float)]: list of voxels representing model1.
     
    254248
    255249        Returns:
    256             float: dissim for pair of list of voxels
     250            float: dissim for a pair of list of voxels
    257251        """
    258252        numvox1 = len(voxels1)
     
    278272        if self.metric == 'l1':
    279273            if self.frequency:
    280                 out = np.linalg.norm((s1-s2), ord=1)
     274                out = np.linalg.norm(s1-s2, ord=1)
    281275            else:
    282                 out = np.linalg.norm((s1[1]-s2[1]), ord=1)
     276                out = np.linalg.norm(s1[1]-s2[1], ord=1)
    283277
    284278        elif self.metric == 'l2':
    285279            if self.frequency:
    286                 out = np.linalg.norm((s1-s2))
     280                out = np.linalg.norm(s1-s2)
    287281            else:
    288                 out = np.linalg.norm((s1[1]-s2[1]))
     282                out = np.linalg.norm(s1[1]-s2[1])
    289283
    290284        elif self.metric == 'emd':
     
    320314
    321315        Returns:
    322             float: dissim for pair of strings representing models.
     316            float: dissim for a pair of strings representing models.
    323317        """     
    324318
     
    349343        listOfVoxels = [self.getVoxels(g) for g in listOfGeno]
    350344        for i in range(numOfGeno):
    351             for j in range(numOfGeno):
     345            for j in range(numOfGeno): # could only calculate a triangle if the definition of similarity and its calculation guarantees symmetry
    352346                dissimMatrix[i,j] = self.calculateDissimforVoxels(listOfVoxels[i], listOfVoxels[j])
    353347        return dissimMatrix
Note: See TracChangeset for help on using the changeset viewer.