def test_scalar_mul(self): res = vector.scalar_mul(1.5, self.v) assert_allclose(self, res, vector.Vector([-0.75, 2.25, 3.75]))
def test_type_shape(self, data, expected_result): result = ConvertToMultiChannelBasedOnBratsClasses()(data) assert_allclose(result, expected_result) self.assertTrue(result.dtype in (bool, torch.bool))
def test_foreground_mask(self, in_type, arguments, image, mask): input_image = in_type(image) result = ForegroundMask(**arguments)(input_image) assert_allclose(result, mask, type_test="tensor")
def test_correct_results(self, _, args, input_image, expected): converter = KeepLargestConnectedComponent(**args) result = converter(input_image) assert_allclose(result, expected, type_test=False)
def test_value(self, input_data, mode2, expected_box, expected_area): expected_box = convert_data_type(expected_box, dtype=np.float32)[0] boxes1 = convert_data_type(input_data["boxes"], dtype=np.float32)[0] mode1 = input_data["mode"] half_bool = input_data["half"] spatial_size = input_data["spatial_size"] # test float16 if half_bool: boxes1 = convert_data_type(boxes1, dtype=np.float16)[0] expected_box = convert_data_type(expected_box, dtype=np.float16)[0] # test convert_box_mode, convert_box_to_standard_mode result2 = convert_box_mode(boxes=boxes1, src_mode=mode1, dst_mode=mode2) assert_allclose(result2, expected_box, type_test=True, device_test=True, atol=0.0) result1 = convert_box_mode(boxes=result2, src_mode=mode2, dst_mode=mode1) assert_allclose(result1, boxes1, type_test=True, device_test=True, atol=0.0) result_standard = convert_box_to_standard_mode(boxes=boxes1, mode=mode1) expected_box_standard = convert_box_to_standard_mode(boxes=expected_box, mode=mode2) assert_allclose(result_standard, expected_box_standard, type_test=True, device_test=True, atol=0.0) # test box_area, box_iou, box_giou, box_pair_giou assert_allclose(box_area(result_standard), expected_area, type_test=True, device_test=True, atol=0.0) iou_metrics = (box_iou, box_giou) for p in iou_metrics: self_iou = p(boxes1=result_standard[1:2, :], boxes2=result_standard[1:1, :]) assert_allclose(self_iou, np.array([[]]), type_test=False) self_iou = p(boxes1=result_standard[1:2, :], boxes2=result_standard[1:2, :]) assert_allclose(self_iou, np.array([[1.0]]), type_test=False) self_iou = box_pair_giou(boxes1=result_standard[1:1, :], boxes2=result_standard[1:1, :]) assert_allclose(self_iou, np.array([]), type_test=False) self_iou = box_pair_giou(boxes1=result_standard[1:2, :], boxes2=result_standard[1:2, :]) assert_allclose(self_iou, np.array([1.0]), type_test=False) # test box_centers, centers_in_boxes, boxes_center_distance result_standard_center = box_centers(result_standard) expected_center = convert_box_mode(boxes=boxes1, src_mode=mode1, dst_mode="cccwhd")[:, :3] assert_allclose(result_standard_center, expected_center, type_test=True, device_test=True, atol=0.0) center = expected_center center[2, :] += 10 result_centers_in_boxes = centers_in_boxes(centers=center, boxes=result_standard) assert_allclose(result_centers_in_boxes, np.array([False, True, False]), type_test=False) center_dist, _, _ = boxes_center_distance(boxes1=result_standard[1:2, :], boxes2=result_standard[1:1, :]) assert_allclose(center_dist, np.array([[]]), type_test=False) center_dist, _, _ = boxes_center_distance(boxes1=result_standard[1:2, :], boxes2=result_standard[1:2, :]) assert_allclose(center_dist, np.array([[0.0]]), type_test=False) center_dist, _, _ = boxes_center_distance(boxes1=result_standard[0:1, :], boxes2=result_standard[0:1, :]) assert_allclose(center_dist, np.array([[0.0]]), type_test=False) # test clip_boxes_to_image clipped_boxes, keep = clip_boxes_to_image(expected_box_standard, spatial_size, remove_empty=True) assert_allclose( expected_box_standard[keep, :], expected_box_standard[1:, :], type_test=True, device_test=True, atol=0.0 ) assert_allclose( id(clipped_boxes) != id(expected_box_standard), True, type_test=False, device_test=False, atol=0.0 ) # test non_max_suppression nms_box = non_max_suppression( boxes=result_standard, scores=boxes1[:, 1] / 2.0, nms_thresh=1.0, box_overlap_metric=box_giou ) assert_allclose(nms_box, [1, 2, 0], type_test=False) nms_box = non_max_suppression( boxes=result_standard, scores=boxes1[:, 1] / 2.0, nms_thresh=-1.0, box_overlap_metric=box_iou ) assert_allclose(nms_box, [1], type_test=False)
def test_split_patch_single_call(self, in_type, input_parameters, image, expected): input_image = in_type(image) splitter = SplitOnGrid(**input_parameters) output = splitter(input_image) assert_allclose(output, expected, type_test=False)
def test_alpha_1(self, im_shape, input_type): im = self.get_data(im_shape, input_type) alpha = [1.0, 1.0] t = RandGibbsNoise(1.0, alpha) out = t(deepcopy(im)) assert_allclose(out, 0 * im, rtol=1e-7, atol=1e-2, type_test="tensor")
def test_type_shape(self, input_data, data, expected_fg, expected_bg): result = FgBgToIndicesd(**input_data)(data) assert_allclose(result["label_fg_indices"], expected_fg) assert_allclose(result["label_bg_indices"], expected_bg)
def test_affine(self, input_param, input_data, expected_val): g = Affined(**input_param) result = g(input_data)["img"] assert_allclose(result, expected_val, rtol=1e-4, atol=1e-4)
def test_grid_distortiond(self, input_param, input_data, expected_val_img, expected_val_mask): g = GridDistortiond(**input_param) result = g(input_data) assert_allclose(result["img"], expected_val_img, type_test=False, rtol=1e-4, atol=1e-4) assert_allclose(result["mask"], expected_val_mask, type_test=False, rtol=1e-4, atol=1e-4)
def test_value(self, input_args, label, image, expected_indices): indices = ClassesToIndices(**input_args)(label, image) for i, e in zip(indices, expected_indices): assert_allclose(i, e)
def test_mat_vec_prod(self): res = matrix.mat_vec_prod(self.m, self.v) assert_allclose(self, res, vector.Vector([3.75, 10.5])) with self.assertRaises(ValueError): matrix.mat_vec_prod(self.m, vector.Vector([1.0]))
def test_softmax(self): res = vector.softmax(self.v) self.assertIsInstance(res, vector.Vector) self.assertAlmostEqual(sum(res.list), 1.0) assert_allclose(self, res, vector.Vector([0.03511903, 0.25949646, 0.70538451]))
def test_ReLU(self): res = vector.ReLU(self.v) self.assertIsInstance(res, vector.Vector) assert_allclose(self, res, vector.Vector([0.0, 1.5, 2.5]))
def test_resample(self, input_param, input_data, expected_val): g = Resample(**input_param) result = g(**input_data) if "device" in input_data: self.assertEqual(result.device, input_data["device"]) assert_allclose(result, expected_val, rtol=1e-4, atol=1e-4)
def test_correct_results(self, _, args, input_image, expected): converter = FillHoles(**args) for p in TEST_NDARRAYS: result = converter(p(clone(input_image))) assert_allclose(result, p(expected))
def test_value(self, input_param, input_data, expected_value): set_determinism(seed=0) result = TorchVision(**input_param)(input_data) assert_allclose(result, expected_value, rtol=1e-3)
def test_single(self): c = Cumulative() c.extend([2, 3]) c.append(1) assert_allclose(c.get_buffer(), torch.tensor([2, 3, 1]))
def test_0_prob(self, im_shape, input_type): im = self.get_data(im_shape, input_type) alpha = [0.5, 1.0] t = RandGibbsNoise(0.0, alpha) out = t(im) assert_allclose(out, im, rtol=1e-7, atol=0, type_test="tensor")
def test_output(self, class_args, probs_map, expected): nms = ProbNMS(**class_args) output = nms(probs_map) assert_allclose(output, expected)
def test_ras_to_lps(self, param, expected): assert_allclose(orientation_ras_lps(param), expected)
def test_value(self, argments, image, expected_data): result = HistogramNormalized(**argments)(image)["img"] assert_allclose(result, expected_data, type_test="tensor") self.assertEqual(get_equivalent_dtype(result.dtype, data_type=np.ndarray), argments.get("dtype", np.float32))
def test_value(self, arguments, image, expected_data, atol): for p in TEST_NDARRAYS: result = SavitzkyGolaySmooth(**arguments)(p(image.astype(np.float32))) assert_allclose(result, p(expected_data.astype(np.float32)), rtol=1e-4, atol=atol, type_test="tensor")
def test_correct_results(self, input_data, expected): add_extreme_points_channel = AddExtremePointsChannel() result = add_extreme_points_channel(**input_data) assert_allclose(result[IMG_CHANNEL], expected, rtol=1e-4)
def test_correct_results(self, _, args, input_image, expected): converter = LabelFilter(**args) result = converter(input_image) assert_allclose(result, expected)
def test_value(self, input_param, img, expected_value): result = VoteEnsembled(**input_param)(img) assert_allclose(result["output"], expected_value)
def test_array_function(self, device="cpu", dtype=float): a = np.random.RandomState().randn(100, 100) b = MetaTensor(a, device=device) assert_allclose(np.sum(a), np.sum(b)) assert_allclose(np.sum(a, axis=1), np.sum(b, axis=1)) assert_allclose(np.linalg.qr(a), np.linalg.qr(b)) c = MetaTensor([1.0, 2.0, 3.0], device=device, dtype=dtype) assert_allclose(np.argwhere(c == 1.0).astype(int).tolist(), [[0]]) assert_allclose(np.concatenate([c, c]), np.asarray([1.0, 2.0, 3.0, 1.0, 2.0, 3.0])) if pytorch_after(1, 8, 1): assert_allclose(c > np.asarray([1.0, 1.0, 1.0]), np.asarray([False, True, True])) assert_allclose( c > torch.as_tensor([1.0, 1.0, 1.0], device=device), torch.as_tensor([False, True, True], device=device) )
def test_value(self, input_param, input_data, expected_value): result = CenterSpatialCropd(**input_param)(input_data) assert_allclose(result["img"], expected_value, type_test=False)
def test_identity(self): for p in TEST_NDARRAYS: img = p(self.imt) identity = Identity() assert_allclose(img, identity(img))
def test_mode(self, array, expected, to_long): res = mode(array, to_long=to_long) assert_allclose(res, expected)