Skip to content

Commit

Permalink
Merge branch 'Project-MONAI:dev' into 4980-get-wsi-at-mpp
Browse files Browse the repository at this point in the history
  • Loading branch information
NikolasSchmitz authored Sep 21, 2024
2 parents a8bb436 + fa1c1af commit 6094ffd
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 5 deletions.
2 changes: 1 addition & 1 deletion monai/data/image_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -1359,7 +1359,7 @@ def _get_affine(self, header: dict) -> np.ndarray:
x, y = direction.shape
affine_diam = min(x, y) + 1
affine: np.ndarray = np.eye(affine_diam)
affine[:x, :y] = direction
affine[:x, :y] = direction.T
affine[: (affine_diam - 1), -1] = origin # len origin is always affine_diam - 1
return affine

Expand Down
3 changes: 2 additions & 1 deletion monai/transforms/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,8 @@ def weighted_patch_samples(
if not v[-1] or not isfinite(v[-1]) or v[-1] < 0: # uniform sampling
idx = r_state.randint(0, len(v), size=n_samples)
else:
r, *_ = convert_to_dst_type(r_state.random(n_samples), v)
r_samples = r_state.random(n_samples)
r, *_ = convert_to_dst_type(r_samples, v, dtype=r_samples.dtype)
idx = searchsorted(v, r * v[-1], right=True) # type: ignore
idx, *_ = convert_to_dst_type(idx, v, dtype=torch.int) # type: ignore
# compensate 'valid' mode
Expand Down
2 changes: 1 addition & 1 deletion tests/test_bundle_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
TEST_CASE_10 = [
["network.json", "test_output.pt", "test_input.pt", "large_files.yaml"],
"test_bundle",
"https://github.com/Project-MONAI/MONAI-extra-test-data/releases/download/0.8.1/test_bundle_v0.1.2.zip",
"https://github.com/Project-MONAI/MONAI-extra-test-data/releases/download/0.8.1/test_bundle_v0.1.3.zip",
{"model.pt": "27952767e2e154e3b0ee65defc5aed38", "model.ts": "97746870fe591f69ac09827175b00675"},
]

Expand Down
8 changes: 6 additions & 2 deletions tests/test_nrrd_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@
"dimension": 4,
"space": "left-posterior-superior",
"sizes": [3, 4, 4, 1],
"space directions": [[0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]],
"space origin": [0.0, 0.0, 0.0],
"space directions": [[0.7, 0.0, 0.0], [0.0, 0.0, -0.8], [0.0, 0.9, 0.0]],
"space origin": [1.0, 5.0, 20.0],
},
]

Expand Down Expand Up @@ -110,6 +110,10 @@ def test_read_with_header(self, data_shape, filename, expected_shape, dtype, ref
np.testing.assert_allclose(image_array, test_image)
self.assertIsInstance(image_header, dict)
self.assertTupleEqual(tuple(image_header["spatial_shape"]), expected_shape)
np.testing.assert_allclose(
image_header["affine"],
np.array([[-0.7, 0.0, 0.0, -1.0], [0.0, 0.0, -0.9, -5.0], [0.0, -0.8, 0.0, 20.0], [0.0, 0.0, 0.0, 1.0]]),
)

@parameterized.expand([TEST_CASE_8])
def test_read_with_header_index_order_c(self, data_shape, filename, expected_shape, dtype, reference_header):
Expand Down
30 changes: 30 additions & 0 deletions tests/test_rand_weighted_crop.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,21 @@ def get_data(ndim):
[[63, 37], [31, 43], [66, 20]],
]
)
im = SEG1_2D
weight_map = np.zeros_like(im, dtype=np.int32)
weight_map[0, 30, 20] = 3
weight_map[0, 45, 44] = 1
weight_map[0, 60, 50] = 2
TESTS.append(
[
"int w 2d",
dict(spatial_size=(10, 12), num_samples=3),
p(im),
q(weight_map),
(1, 10, 12),
[[60, 50], [30, 20], [45, 44]],
]
)
im = SEG1_3D
weight = np.zeros_like(im)
weight[0, 5, 30, 17] = 1.1
Expand Down Expand Up @@ -149,6 +164,21 @@ def get_data(ndim):
[[32, 24, 40], [32, 24, 40], [32, 24, 40]],
]
)
im = SEG1_3D
weight_map = np.zeros_like(im, dtype=np.int32)
weight_map[0, 6, 22, 19] = 4
weight_map[0, 8, 40, 31] = 2
weight_map[0, 13, 20, 24] = 3
TESTS.append(
[
"int w 3d",
dict(spatial_size=(8, 10, 12), num_samples=3),
p(im),
q(weight_map),
(1, 8, 10, 12),
[[13, 20, 24], [6, 22, 19], [8, 40, 31]],
]
)


class TestRandWeightedCrop(CropTest):
Expand Down

0 comments on commit 6094ffd

Please sign in to comment.