def randomize(self, label: np.ndarray, image: Optional[np.ndarray] = None) -> None: self.spatial_size = fall_back_tuple(self.spatial_size, default=label.shape[1:]) self.centers = generate_pos_neg_label_crop_centers( label, self.spatial_size, self.num_samples, self.pos_ratio, image, self.image_threshold, self.R)
def randomize( self, label: np.ndarray, fg_indices: Optional[np.ndarray] = None, bg_indices: Optional[np.ndarray] = None, image: Optional[np.ndarray] = None, ) -> None: self.spatial_size = fall_back_tuple(self.spatial_size, default=label.shape[1:]) if np.greater(self.spatial_size, label.shape[1:]).any(): self.centers = [ None, ] * self.num_samples return if fg_indices is None or bg_indices is None: fg_indices_, bg_indices_ = map_binary_to_indices( label, image, self.image_threshold) else: fg_indices_ = fg_indices bg_indices_ = bg_indices self.centers = generate_pos_neg_label_crop_centers( self.spatial_size, self.num_samples, self.pos_ratio, label.shape[1:], fg_indices_, bg_indices_, self.R, ) self.offset_centers = [] for center in self.centers: if 0 < self.offset <= 1: offset = [ self.R.randint(self.offset * sz // 2) * self.R.choice([1, -1]) for sz in self.spatial_size ] elif self.offset > 1: offset = [ self.R.randint(self.offset) * self.R.choice([1, -1]) for sz in self.spatial_size ] else: offset = [ 0, ] * len(self.spatial_size) self.offset_centers.append( [int(c + b) for c, b in zip(center, offset)]) self.centers = self.offset_centers
def randomize( self, label: np.ndarray, fg_indices: Optional[np.ndarray] = None, bg_indices: Optional[np.ndarray] = None, image: Optional[np.ndarray] = None, ) -> None: self.spatial_size = fall_back_tuple(self.spatial_size, default=label.shape[1:]) if fg_indices is None or bg_indices is None: fg_indices_, bg_indices_ = map_binary_to_indices(label, image, self.image_threshold) else: fg_indices_ = fg_indices bg_indices_ = bg_indices self.centers = generate_pos_neg_label_crop_centers( self.spatial_size, self.num_samples, self.pos_ratio, label.shape[1:], fg_indices_, bg_indices_, self.R )
def randomize(self, label, image): self.centers = generate_pos_neg_label_crop_centers( label, self.size, self.num_samples, self.pos_ratio, image, self.image_threshold, self.R)
def randomize(self, label, image) -> None: # type: ignore # see issue #495 self.spatial_size = fall_back_tuple(self.spatial_size, default=label.shape[1:]) self.centers = generate_pos_neg_label_crop_centers( label, self.spatial_size, self.num_samples, self.pos_ratio, image, self.image_threshold, self.R )
def randomize( self, label: np.ndarray, fg_indices: Optional[np.ndarray] = None, bg_indices: Optional[np.ndarray] = None, image: Optional[np.ndarray] = None, ) -> None: self.spatial_size = fall_back_tuple(self.spatial_size, default=label.shape[1:]) if np.greater(self.spatial_size, label.shape[1:]).any(): self.centers = [ None, ] * self.num_samples return # Select subregion to assure valid roi valid_start = np.floor_divide(self.spatial_size, 2) # add 1 for random valid_end = np.subtract(label.shape[1:] + np.array(1), self.spatial_size / np.array(2)).astype( np.uint16) # int generation to have full range on upper side, but subtract unfloored size/2 to prevent rounded range # from being too high for i in range( len(valid_start) ): # need this because np.random.randint does not work with same start and end if valid_start[i] == valid_end[i]: valid_end[i] += 1 def _correct_centers(center_ori: List[np.ndarray], valid_start: np.ndarray, valid_end: np.ndarray) -> List[np.ndarray]: for i, c in enumerate(center_ori): center_i = c if c < valid_start[i]: center_i = valid_start[i] if c >= valid_end[i]: center_i = valid_end[i] - 1 center_ori[i] = center_i return center_ori if fg_indices is None or bg_indices is None: fg_indices_, bg_indices_ = map_binary_to_indices( label, image, self.image_threshold) else: fg_indices_ = fg_indices bg_indices_ = bg_indices self.centers = generate_pos_neg_label_crop_centers( self.spatial_size, self.num_samples, self.pos_ratio, label.shape[1:], fg_indices_, bg_indices_, self.R, ) self.offset_centers = [] for center in self.centers: if 0 < self.offset <= 1: offset = [ self.R.randint(self.offset * sz // 2) * self.R.choice([1, -1]) for sz in self.spatial_size ] elif self.offset > 1: offset = [ self.R.randint(self.offset) * self.R.choice([1, -1]) for sz in self.spatial_size ] else: offset = [ 0, ] * len(self.spatial_size) # print('Offset: ', offset, "Center: ", center) offset_centers = [int(c + b) for c, b in zip(center, offset)] self.offset_centers.append( _correct_centers(offset_centers, valid_start, valid_end)) self.centers = self.offset_centers
def randomize(self, label): self.centers = generate_pos_neg_label_crop_centers(label, self.size, self.num_samples, self.pos_ratio, self.R)
def test_type_shape(self, input_data, expected_type, expected_count, expected_shape): result = generate_pos_neg_label_crop_centers(**input_data) self.assertIsInstance(result, expected_type) self.assertEqual(len(result), expected_count) self.assertEqual(len(result[0]), expected_shape)