Conditions | 7 |
Total Lines | 96 |
Code Lines | 49 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.
There are several approaches to avoid long parameter lists:
1 | """ |
||
29 | def __init__( |
||
30 | self, |
||
31 | file_loader, |
||
32 | data_dir_paths: List[str], |
||
33 | labeled: bool, |
||
34 | sample_label: Optional[str], |
||
35 | intra_group_prob: float, |
||
36 | intra_group_option: str, |
||
37 | sample_image_in_group: bool, |
||
38 | seed: Optional[int], |
||
39 | image_shape: Union[Tuple[int, ...], List[int]], |
||
40 | ): |
||
41 | """ |
||
42 | :param file_loader: a subclass of FileLoader |
||
43 | :param data_dir_paths: paths of the directory storing data, |
||
44 | the data has to be saved under two different sub-directories: |
||
45 | |||
46 | - images |
||
47 | - labels |
||
48 | |||
49 | :param labeled: bool, true if the data is labeled, false if unlabeled |
||
50 | :param sample_label: "sample" or "all", read `get_label_indices` |
||
51 | in deepreg/dataset/util.py for more details. |
||
52 | :param intra_group_prob: float between 0 and 1, |
||
53 | |||
54 | - 0 means generating only inter-group samples, |
||
55 | - 1 means generating only intra-group samples |
||
56 | |||
57 | :param intra_group_option: str, "forward", "backward, or "unconstrained" |
||
58 | :param sample_image_in_group: bool, |
||
59 | |||
60 | - if true, only one image pair will be yielded for each group, |
||
61 | so one epoch has num_groups pairs of data, |
||
62 | - if false, iterate through this loader will generate all possible pairs |
||
63 | |||
64 | :param seed: controls the randomness in sampling, |
||
65 | if seed=None, then the randomness is not fixed |
||
66 | :param image_shape: list or tuple of length 3, |
||
67 | corresponding to (dim1, dim2, dim3) of the 3D image |
||
68 | """ |
||
69 | super().__init__( |
||
70 | image_shape=image_shape, |
||
71 | labeled=labeled, |
||
72 | sample_label=sample_label, |
||
73 | seed=seed, |
||
74 | ) |
||
75 | assert isinstance( |
||
76 | data_dir_paths, list |
||
77 | ), f"data_dir_paths must be list of strings, got {data_dir_paths}" |
||
78 | # init |
||
79 | # the indices for identifying an image pair is (group1, sample1, group2, sample2, label) |
||
80 | self.num_indices = 5 |
||
81 | self.intra_group_option = intra_group_option |
||
82 | self.intra_group_prob = intra_group_prob |
||
83 | self.sample_image_in_group = sample_image_in_group |
||
84 | # set file loaders |
||
85 | # grouped data are not paired data, so moving/fixed share the same file loader for images/labels |
||
86 | loader_image = file_loader( |
||
87 | dir_paths=data_dir_paths, name="images", grouped=True |
||
88 | ) |
||
89 | self.loader_moving_image = loader_image |
||
90 | self.loader_fixed_image = loader_image |
||
91 | if self.labeled is True: |
||
92 | loader_label = file_loader( |
||
93 | dir_paths=data_dir_paths, name="labels", grouped=True |
||
94 | ) |
||
95 | self.loader_moving_label = loader_label |
||
96 | self.loader_fixed_label = loader_label |
||
97 | self.validate_data_files() |
||
98 | # get group related stats |
||
99 | self.num_groups = self.loader_moving_image.get_num_groups() |
||
100 | self.num_images_per_group = self.loader_moving_image.get_num_images_per_group() |
||
101 | if self.intra_group_prob < 1: |
||
102 | if self.num_groups < 2: |
||
103 | raise ValueError( |
||
104 | f"There are {self.num_groups} groups, " |
||
105 | f"we need at least two groups for inter group sampling" |
||
106 | ) |
||
107 | # calculate number of samples and save pre-calculated sample indices |
||
108 | if self.sample_image_in_group is True: |
||
109 | # one image pair in each group (pair) will be yielded |
||
110 | self.sample_indices = None |
||
111 | self._num_samples = self.num_groups |
||
112 | else: |
||
113 | # all possible pair in each group (pair) will be yielded |
||
114 | if intra_group_prob not in [0, 1]: |
||
115 | raise ValueError( |
||
116 | "Mixing intra and inter groups is not supported" |
||
117 | " when not sampling pairs." |
||
118 | ) |
||
119 | if intra_group_prob == 0: # inter group |
||
120 | self.sample_indices = self.get_inter_sample_indices() |
||
121 | else: # intra group |
||
122 | self.sample_indices = self.get_intra_sample_indices() |
||
123 | |||
124 | self._num_samples = len(self.sample_indices) # type: ignore |
||
125 | |||
286 |