|
1
|
|
|
import copy |
|
2
|
|
|
from typing import Tuple, Optional, Generator |
|
3
|
|
|
|
|
4
|
|
|
import numpy as np |
|
5
|
|
|
|
|
6
|
|
|
from ... import TypePatchSize |
|
7
|
|
|
from ...data.subject import Subject |
|
8
|
|
|
from ...utils import to_tuple |
|
9
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
class PatchSampler: |
|
12
|
|
|
r"""Base class for TorchIO samplers. |
|
13
|
|
|
|
|
14
|
|
|
Args: |
|
15
|
|
|
patch_size: Tuple of integers :math:`(d, h, w)` to generate patches |
|
16
|
|
|
of size :math:`d \times h \times w`. |
|
17
|
|
|
If a single number :math:`n` is provided, :math:`d = h = w = n`. |
|
18
|
|
|
""" |
|
19
|
|
|
def __init__(self, patch_size: TypePatchSize): |
|
20
|
|
|
patch_size_array = np.array(to_tuple(patch_size, length=3)) |
|
21
|
|
|
if np.any(patch_size_array < 1): |
|
22
|
|
|
message = ( |
|
23
|
|
|
'Patch dimensions must be positive integers,' |
|
24
|
|
|
f' not {patch_size_array}' |
|
25
|
|
|
) |
|
26
|
|
|
raise ValueError(message) |
|
27
|
|
|
self.patch_size = patch_size_array.astype(np.uint16) |
|
28
|
|
|
|
|
29
|
|
|
def extract_patch(self): |
|
30
|
|
|
raise NotImplementedError |
|
31
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
class RandomSampler(PatchSampler): |
|
34
|
|
|
r"""Base class for TorchIO samplers. |
|
35
|
|
|
|
|
36
|
|
|
Args: |
|
37
|
|
|
patch_size: Tuple of integers :math:`(d, h, w)` to generate patches |
|
38
|
|
|
of size :math:`d \times h \times w`. |
|
39
|
|
|
If a single number :math:`n` is provided, :math:`d = h = w = n`. |
|
40
|
|
|
""" |
|
41
|
|
|
def __call__( |
|
42
|
|
|
self, |
|
43
|
|
|
sample: Subject, |
|
44
|
|
|
num_patches: Optional[int] = None, |
|
45
|
|
|
) -> Generator[Subject, None, None]: |
|
46
|
|
|
raise NotImplementedError |
|
47
|
|
|
|
|
48
|
|
|
def get_probability_map(self, sample: Subject): |
|
49
|
|
|
raise NotImplementedError |
|
50
|
|
|
|