Closed smith6jt-cop closed 11 months ago
There are two problems that I see. The first is that the mask file you provided has the masks in the z dimension, not the channel dimension, i.e., it is (1,1,2,xdim,ydim) rather than (1,2,1,xdim,ydim). The masks are specified in the channel dimension because the image itself might be 2D or 3D. The second is that the mask is not an indexed image. See comment regarding mask file requirements.
Our mask file shows the masks in the channel dimension in the OME metadata: Also, shape is the same:
As the example data:
The mask file was produced in QuPath with UseInstanceLabels() to assign unique ID to each cell. To give each cell a different shade of gray when there are thousands of cells, the masks are converted to 32 bit which results in a decimal ID for each cell. Are you saying that each cell ID needs to be an integer? If so, I will need to find another way to generate the masks.
The proper generation of mask images using UseInstanceLabels() in the most recent version of QuPath-0.5 fixed this error.
great. closing this issue.
ValueError Traceback (most recent call last) Cell In[3], line 11 7 PCA_model = [] 8 # or give a specific model 9 # PCA_model = pickle.load( open( "2D_PCA_model.pickle", "rb" )) ---> 11 seg_metrics_2D = read_and_eval_seg(img_path, mask_path, PCA_model, output_directory) 13 print(seg_metrics_2D)
File ~/CellSegmentationEvaluator/SimpleCSE/read_and_eval_seg.py:57, in read_and_eval_seg(img_path, mask_path, PCA_model, output_directory) 55 except: 56 print('2D PCA model file missing') ---> 57 seg_metrics = single_method_eval(img, mask, PCA_model, output_directory, bz=bestz) 58 else: 59 if PCA_model==[]:
File ~/CellSegmentationEvaluator/SimpleCSE/seg_eval_pkg.py:718, in single_method_eval(img, mask, PCA_model, output_dir, bz, unit, pixelsizex, pixelsizey) 713 metrics[channel_names[channel]][ 714 "FractionOfFirstPCForegroundOutsideCells" 715 ] = foreground_PCA 717 # get cell type labels --> 718 cell_type_labels = cell_type(current_mask, img_channels) 719 else: 720 img_channels = np.squeeze(img["data"][0, :, bestz, :, :])
File ~/CellSegmentationEvaluator/SimpleCSE/seg_eval_pkg.py:431, in cell_type(mask, channels) 429 feature_matrix_z = np.vstack(feature_matrix_z_pieces).T 430 for c in range(1, 11): --> 431 model = KMeans(n_clusters=c).fit(feature_matrix_z) 432 labellist.append(model.labels.astype(int)) 433 print(label_list)
File ~/anaconda3/lib/python3.11/site-packages/sklearn/base.py:1152, in _fit_context..decorator..wrapper(estimator, *args, *kwargs)
1145 estimator._validate_params()
1147 with config_context(
1148 skip_parameter_validation=(
1149 prefer_skip_nested_validation or global_skip_validation
1150 )
1151 ):
-> 1152 return fit_method(estimator, args, **kwargs)
File ~/anaconda3/lib/python3.11/site-packages/sklearn/cluster/_kmeans.py:1484, in KMeans.fit(self, X, y, sample_weight) 1449 """Compute k-means clustering. 1450 1451 Parameters (...) 1473 Fitted estimator. 1474 """ 1475 X = self._validate_data( 1476 X, 1477 accept_sparse="csr", (...) 1481 accept_large_sparse=False, 1482 ) -> 1484 self._check_params_vs_input(X) 1486 random_state = check_random_state(self.random_state) 1487 sample_weight = _check_sample_weight(sample_weight, X, dtype=X.dtype)
File ~/anaconda3/lib/python3.11/site-packages/sklearn/cluster/_kmeans.py:1416, in KMeans._check_params_vs_input(self, X) 1415 def _check_params_vs_input(self, X): -> 1416 super()._check_params_vs_input(X, default_n_init=10) 1418 self._algorithm = self.algorithm 1419 if self._algorithm in ("auto", "full"):
File ~/anaconda3/lib/python3.11/site-packages/sklearn/cluster/_kmeans.py:866, in _BaseKMeans._check_params_vs_input(self, X, default_n_init) 863 def _check_params_vs_input(self, X, default_n_init=None): 864 # n_clusters 865 if X.shape[0] < self.n_clusters: --> 866 raise ValueError( 867 f"n_samples={X.shape[0]} should be >= n_clusters={self.n_clusters}." 868 ) 870 # tol 871 self._tol = _tolerance(X, self.tol)
ValueError: n_samples=1 should be >= n_clusters=2.