fepegar / torchio

@@ -24,7 +24,7 @@
Loading
24 24
        patches_left = num_patches if num_patches is not None else True
25 25
        while patches_left:
26 26
            index_ini = [
27 -
                torch.randint(int(x) + 1, (1,)).item()
27 +
                torch.randint(x + 1, (1,)).item()
28 28
                for x in valid_range
29 29
            ]
30 30
            index_ini_array = np.asarray(index_ini)

@@ -1,11 +1,10 @@
Loading
1 1
import random
2 -
import numpy as np
3 2
import warnings
4 3
from itertools import islice
5 -
from typing import List, Iterator, Optional
4 +
from typing import List, Iterator, Optional, Sequence
6 5
7 6
import humanize
8 -
from torch.utils.data import Dataset, DataLoader
7 +
from torch.utils.data import Dataset, DataLoader, RandomSampler
9 8
10 9
from .subject import Subject
11 10
from .sampler import PatchSampler
@@ -147,15 +146,16 @@
Loading
147 146
        self.shuffle_subjects = shuffle_subjects
148 147
        self.shuffle_patches = shuffle_patches
149 148
        self.samples_per_volume = samples_per_volume
150 -
        if type(samples_per_volume) == int:
151 -
            self.samples_per_volume = [samples_per_volume for _ in range(
152 -
                self.num_subjects)]
153 -
        assert type(self.samples_per_volume) == list, ('`samples_per_volume`'
154 -
                                                       'should be an int'
155 -
                                                       'or a list')
156 -
        assert len(self.samples_per_volume) == self.num_subjects, (
157 -
            '`samples_per_volume` (list) length must be equal'
158 -
            'to the number of subjects')
149 +
        if isinstance(samples_per_volume, int):
150 +
            self.samples_per_volume = self.num_subjects * [samples_per_volume]
151 +
152 +
        if not isinstance(self.samples_per_volume, Sequence):
153 +
            raise TypeError('`samples_per_volume` should be an int or a list')
154 +
155 +
        if len(self.samples_per_volume) != self.num_subjects:
156 +
            raise ValueError('`samples_per_volume` (list) length must be equal'
157 +
                             'to the number of subjects')
158 +
159 159
        self.sampler = sampler
160 160
        self.num_workers = num_workers
161 161
        self.verbose = verbose
@@ -163,12 +163,6 @@
Loading
163 163
        self.patches_list: List[Subject] = []
164 164
        self.num_sampled_patches = 0
165 165
166 -
        # Same random shuffling applied to subjects and volumes
167 -
        if self.shuffle_subjects:
168 -
            tmp_seed = np.random.random()
169 -
            random.Random(tmp_seed).shuffle(self.subjects_dataset._subjects)
170 -
            random.Random(tmp_seed).shuffle(self.samples_per_volume)
171 -
172 166
        if start_background:
173 167
            self._initialize_subjects_iterable()
174 168
@@ -242,13 +236,7 @@
Loading
242 236
        # If the counter of samples per volume is empty (i.e., end of the
243 237
        # epoch), refill it.
244 238
        if sum(self.counter_samples_per_volume) == 0:
245 -
            if self.shuffle_subjects:
246 -
                tmp_seed = np.random.random()
247 -
                random.Random(tmp_seed).shuffle(
248 -
                    self.subjects_dataset._subjects)
249 -
                random.Random(tmp_seed).shuffle(self.samples_per_volume)
250 -
                self._initialize_subjects_iterable()
251 -
239 +
            self._initialize_subjects_iterable()
252 240
            self.counter_samples_per_volume = self.samples_per_volume.copy()
253 241
            self.idx_subject = -1
254 242
            self.curr_subject = None
@@ -308,10 +296,20 @@
Loading
308 296
    def _get_subjects_iterable(self) -> Iterator:
309 297
        # I need a DataLoader to handle parallelism
310 298
        # But this loader is always expected to yield single subject samples
299 +
300 +
        # Same random shuffling applied to subjects and volumes
301 +
        if self.shuffle_subjects:
302 +
            random_idx = list(RandomSampler(self.subjects_dataset))
303 +
            local_sub_dataset = [self.subjects_dataset[i] for i in random_idx]
304 +
            self.samples_per_volume = [self.samples_per_volume[i]
305 +
                                       for i in random_idx]
306 +
        else:
307 +
            local_sub_dataset = self.subjects_dataset
308 +
311 309
        self._print(
312 310
            f'\nCreating subjects loader with {self.num_workers} workers')
313 311
        subjects_loader = DataLoader(
314 -
            self.subjects_dataset,
312 +
            local_sub_dataset,
315 313
            num_workers=self.num_workers,
316 314
            batch_size=1,
317 315
            collate_fn=self._get_first_item,
Files Coverage
tests 99.78%
torchio 95.29%
print_system.py 0.00%
Project Totals (124 files) 96.65%
1961.1
TRAVIS_PYTHON_VERSION=3.8
TRAVIS_OS_NAME=linux
1961.2
TRAVIS_PYTHON_VERSION=3.7
TRAVIS_OS_NAME=linux
1961.3
TRAVIS_PYTHON_VERSION=3.6
TRAVIS_OS_NAME=linux

No yaml found.

Create your codecov.yml to customize your Codecov experience

Sunburst
The inner-most circle is the entire project, moving away from the center are folders then, finally, a single file. The size and color of each slice is representing the number of statements and the coverage, respectively.
Icicle
The top section represents the entire project. Proceeding with folders and finally individual files. The size and color of each slice is representing the number of statements and the coverage, respectively.
Grid
Each block represents a single file in the project. The size and color of each block is represented by the number of statements and the coverage, respectively.
Loading