Skip to content

Commit

Permalink
1.6.17
Browse files Browse the repository at this point in the history
  • Loading branch information
erdogant committed Feb 16, 2025
1 parent ad62ae4 commit ba750d5
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 28 deletions.
2 changes: 1 addition & 1 deletion clustimage/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

__author__ = 'Erdogan Tasksen'
__email__ = 'erdogant@gmail.com'
__version__ = '1.6.16'
__version__ = '1.6.17'

# module level doc-string
__doc__ = """
Expand Down
60 changes: 33 additions & 27 deletions clustimage/clustimage.py
Original file line number Diff line number Diff line change
Expand Up @@ -622,35 +622,41 @@ def unique(self, metric=None):
# Unique labels
uilabels = np.unique(self.results['labels'])

# Run over all cluster labels
for label in uilabels:
# Get cluster label
idx = np.where(self.results['labels']==label)[0]
# Compute center of cluster
xycoord_center = np.mean(self.results['xycoord'][idx, :], axis=0)
# Compute the average image by simply averaging the images
img = []
if (self.params['dim'] is not None) and (self.results['pathnames'] is not None):
img = np.vstack(list(map(lambda x: self.imread(x, colorscale=self.params['cv2_imread_colorscale'], dim=self.params['dim'], flatten=True, use_thumbnail_cache=self.params['use_thumbnail_cache']), np.array(self.results['pathnames'])[idx])))
eigen_img.append(imscale(np.mean(img, axis=0)))

# dim = self._get_dim(eigen_img)
# plt.figure();plt.imshow(eigen_img.reshape(dim))
if len(uilabels) >= 1:
# Run over all cluster labels
for label in uilabels:
# Get cluster label
idx = np.where(self.results['labels']==label)[0]
# Compute center of cluster
xycoord_center = np.mean(self.results['xycoord'][idx, :], axis=0)
# Compute the average image by simply averaging the images
img = []
if (self.params['dim'] is not None) and (self.results['pathnames'] is not None):
img = np.vstack(list(map(lambda x: self.imread(x, colorscale=self.params['cv2_imread_colorscale'], dim=self.params['dim'], flatten=True, use_thumbnail_cache=self.params['use_thumbnail_cache']), np.array(self.results['pathnames'])[idx])))
eigen_img.append(imscale(np.mean(img, axis=0)))

# dim = self._get_dim(eigen_img)
# plt.figure();plt.imshow(eigen_img.reshape(dim))

# Compute distance across all samples
dist = distance.cdist(self.results['xycoord'], xycoord_center.reshape(-1, 1).T, metric=metric)
# Take closest sample to the center
idx_closest = np.argmin(dist)
# Store
center_idx.append(idx_closest)
center_coord.append(xycoord_center)
if self.results.get('pathnames', None) is not None:
pathnames.append(self.results['pathnames'][idx_closest])
else:
pathnames.append('')

# Compute distance across all samples
dist = distance.cdist(self.results['xycoord'], xycoord_center.reshape(-1, 1).T, metric=metric)
# Take closest sample to the center
idx_closest = np.argmin(dist)
# Store
center_idx.append(idx_closest)
center_coord.append(xycoord_center)
if self.results.get('pathnames', None) is not None:
pathnames.append(self.results['pathnames'][idx_closest])
else:
pathnames.append('')
# Store and return
self.results_unique = {'labels': uilabels, 'idx': center_idx, 'xycoord_center': np.vstack(center_coord), 'pathnames': pathnames, 'img_mean': np.vstack(eigen_img)}
else:
# Default output
self.results_unique = {'labels': uilabels, 'idx': None, 'xycoord_center': None, 'pathnames': None, 'img_mean': None}

# Store and return
self.results_unique = {'labels': uilabels, 'idx': center_idx, 'xycoord_center': np.vstack(center_coord), 'pathnames': pathnames, 'img_mean': np.vstack(eigen_img)}
# Return
return self.results_unique

def find(self, Xnew, metric=None, k=None, alpha=0.05):
Expand Down

0 comments on commit ba750d5

Please sign in to comment.