def __call__(self, sample: dict) -> dict: # normalize first norm = MRFLabelNormalization(self.min_, self.max_) sample = norm(sample) maps = pymia_tfm.check_and_return(sample[pymia_def.KEY_LABELS], np.ndarray) mask = pymia_tfm.check_and_return(sample[self.mask], np.ndarray) mask = np.concatenate([mask] * maps.shape[-1], -1) maps[mask == 0] = 0 sample[pymia_def.KEY_LABELS] = maps return sample
def __call__(self, sample: dict) -> dict: for entry in self.entries: if entry not in sample: raise ValueError( pymia_tfm.ENTRY_NOT_EXTRACTED_ERR_MSG.format(entry)) np_entry = pymia_tfm.check_and_return(sample[entry], np.ndarray) frame = np_entry[..., self.temporal_idx, :] if len(frame.shape) == 3: # without batch dimension shape = frame[..., 0].shape # spatial dimension # permute real and imaginary independently frame[..., 0] = np.random.permutation( frame[..., 0].reshape(-1)).reshape(shape) frame[..., 1] = np.random.permutation( frame[..., 1].reshape(-1)).reshape(shape) else: # we have the batch dimension shape = frame[0, ..., 0].shape # spatial dimension for batch_idx in range(frame.shape[0]): # permute real and imaginary independently frame[batch_idx, ..., 0] = np.random.permutation( frame[batch_idx, ..., 0].reshape(-1)).reshape(shape) frame[batch_idx, ..., 1] = np.random.permutation( frame[batch_idx, ..., 1].reshape(-1)).reshape(shape) np_entry[..., self.temporal_idx, :] = frame sample[entry] = np_entry return sample
def __call__(self, sample: dict) -> dict: maps = pymia_tfm.check_and_return(sample[pymia_def.KEY_LABELS], np.ndarray) for idx, entry in enumerate(self.min_.keys()): maps[..., idx] = (maps[..., idx] - self.min_[entry]) / ( self.max_[entry] - self.min_[entry]) # normalize to 0..1 sample[pymia_def.KEY_LABELS] = maps return sample
def __call__(self, sample: dict) -> dict: for entry in self.entries: if entry not in sample: raise ValueError( pymia_tfm.ENTRY_NOT_EXTRACTED_ERR_MSG.format(entry)) np_entry = pymia_tfm.check_and_return(sample[entry], np.ndarray) np_entry[..., self.temporal_idx, :] = 0 sample[entry] = np_entry return sample
def __call__(self, sample: dict) -> dict: # unsqueeze samples to be 4-D tensors, as required by TorchIO for entry in self.entries: if entry not in sample: if tfm.raise_error_if_entry_not_extracted: raise ValueError(tfm.ENTRY_NOT_EXTRACTED_ERR_MSG.format(entry)) continue np_entry = tfm.check_and_return(sample[entry], np.ndarray) sample[entry] = np.expand_dims(np_entry, -1) # apply TorchIO transforms for t in self.transforms: sample = t(sample) # squeeze samples back to original format for entry in self.entries: np_entry = tfm.check_and_return(sample[entry].numpy(), np.ndarray) sample[entry] = np_entry.squeeze(-1) return sample
def __call__(self, sample: dict) -> dict: # unsqueeze samples to add a batch dimensions, as required by batchgenerators for entry in self.entries: if entry not in sample: if tfm.raise_error_if_entry_not_extracted: raise ValueError(tfm.ENTRY_NOT_EXTRACTED_ERR_MSG.format(entry)) continue np_entry = tfm.check_and_return(sample[entry], np.ndarray) sample[entry] = np.expand_dims(np_entry, 0) # apply batchgenerators transforms for t in self.transforms: sample = t(**sample) # squeeze samples back to original format for entry in self.entries: np_entry = tfm.check_and_return(sample[entry], np.ndarray) sample[entry] = np_entry.squeeze(0) return sample