@@ -12,89 +12,91 @@ class LocalCC(object):
1212 @staticmethod
1313 def _get_yx (element ):
1414 ''' Helper. Takes in a WKBElement.
15- Return a shapely.geometry.point.Point object
15+ Return a shapely.geometry.point.Point object
1616 '''
1717 return wkb .loads (bytes (element .data ))
1818
1919 @staticmethod
20- def _get_df (np_centroids ):
21- '''Helper. Takes numpy centroids array
22- Return a sorted dataframe by site and label
20+ def _get_df (centroids ):
21+ '''Helper. Takes a list of centroids coords.
22+ Return a sorted dataframe by site and label
2323 '''
24- data = np .zeros ( (len (np_centroids ),5 ) )
24+ data = np .zeros ( (len (centroids ),5 ) )
2525 headers = ['y' ,'x' ,'mapobject_id' ,'label' ,'site' ]
26- data [:] = np_centroids
26+ data [:] = centroids
2727 data_df = pd .DataFrame (data , columns = headers )
2828 df = data_df .sort_values (['site' , 'label' ])
2929 return df
3030
31+
32+
33+
3134 def __init__ (self , centroids , wellY , wellX ):
3235 '''
3336 Parameters
3437 ----------
35- centroids: list of tuples (centroid WKB element, mapobject_id, label,site);
38+ centroids: list of tuples (centroid WKB element, mapobject_id, label,site);
3639 i.e. query objects from MapobjectSegmentation table
3740 '''
38- self .centroids = centroids
39-
40- self .centroids_coordinates = [(abs (self ._get_yx (element [0 ]).y ),abs (self ._get_yx (element [0 ]).x ), int (element [1 ]), int (element [2 ]), int (element [3 ])) for element in self .centroids ]
41-
41+
42+ self .centroids_coordinates = [
43+ ( round (abs (self ._get_yx (element [0 ]).y ),1 ),\
44+ round (abs (self ._get_yx (element [0 ]).x ),1 ),\
45+ int (element [1 ]),\
46+ int (element [2 ]),\
47+ int (element [3 ]) ) for element in centroids ]
48+
4249 self .df = self ._get_df (self .centroids_coordinates )
4350 self .yx_coordinates = np .asarray ((self .df ['y' ],self .df ['x' ])).transpose ()
4451 self .wellY = wellY
4552 self .wellX = wellX
4653 self .well_diagonal = np .round (math .sqrt (self .wellX ** 2 + self .wellY ** 2 ))
47-
48-
49-
50- def real_distances (self ):
54+
55+
56+ def gen_real_distances (self ):
57+ '''
58+ Returns
59+ -------
60+ generator
61+ Sum of distances calculated from real positions
62+ '''
63+
64+ for yx_real in self .yx_coordinates :
65+ real_dist = distance .cdist (np .transpose (yx_real [:,np .newaxis ]), self .yx_coordinates , 'euclidean' )
66+ real_masked = np .ma .masked_where (real_dist == 0 ,real_dist ) # mask 0 values
67+ real_masked_divide = np .divide (self .well_diagonal , real_masked )
68+ yield np .sum (real_masked_divide .filled (fill_value = 0 ))
69+
70+
71+ def gen_random_distances (self ):
5172 '''
5273 Returns
5374 -------
54- numpy array of distances from real positions
55- '''
56- real_dists = distance .cdist (self .yx_coordinates , self .yx_coordinates , 'euclidean' )
57- real_masked = np .ma .masked_where (real_dists == 0 ,real_dists ) # mask 0 values
58- real_masked_divide = np .divide (self .well_diagonal , real_masked )
59- return real_masked_divide .filled (fill_value = 0 )
60-
61- def random_distances (self ):
75+ generator
76+ Sum of distances calculated from random positions
6277 '''
63- Returns
64- -------
65- numpy array of distances from random positions
66- '''
67- rand_dists = list ()
68-
78+
6979 for yx_real in self .yx_coordinates :
7080 y_rand = np .random .uniform (0 ,self .wellY ,len (self .yx_coordinates )- 1 )
71- x_rand = np .random .uniform (0 ,self .wellX ,len (self .yx_coordinates )- 1 )
81+ x_rand = np .random .uniform (0 ,self .wellX ,len (self .yx_coordinates )- 1 )
7282 yx_coordinates_random = np .concatenate ( (y_rand [:,np .newaxis ],x_rand [:,np .newaxis ]), axis = 1 )
73-
83+
7484 rand_dist = np .divide (self .well_diagonal , distance .cdist (np .transpose (yx_real [:,np .newaxis ]), yx_coordinates_random , 'euclidean' ) )
75- rand_dists .append (np .squeeze (rand_dist ))
76- return np .asarray (rand_dists )
77-
85+ yield np .sum (np .squeeze (rand_dist ))
86+
7887
7988 def get_lcc (self ,real_dists ,random_dists ):
8089 '''
8190 Parameters
8291 ----------
83- real_dists: numpy arrays of real distances
84- random_dists: numpy array of random distances
92+ real_dists: generator of real distances
93+ random_dists: generator of random distances
8594
8695 Returns
8796 -------
88- a numpy array i.e. a LCC value and mapobject_id for centroid
89- '''
90- sum_real = np .sum (real_dists , axis = 1 )
91- sum_random = np .sum (random_dists , axis = 1 )
92- lcc = sum_real - sum_random
93-
94- self .df ['lcc' ] = pd .Series (lcc )
95- return self .df
96- # lcc[lcc[:,1].argsort()] sort array based on mapobject_id
97- # lcc[lcc[:,0].argsort()] sort array based on lcc value
98-
97+ a pandas Dataframe i.e. y,x,mapobject_id,label,site,lcc
98+ '''
99+ lcc = [re_d - rn_d for re_d ,rn_d in zip (real_dists ,random_dists )]
99100
100-
101+ self .df ['lcc' ] = pd .Series (lcc )
102+ return self .df
0 commit comments