gurudesh/pytracebugs huggingface dataset:
# Buggy code
def index_to_doctag(self, i_index):
    """Return string key for given i_index, if available. Otherwise return raw int doctag (same int)."""
    candidate_offset = i_index - self.max_rawint - 1
    if 0 <= candidate_offset < len(self.offset2doctag):
        return self.ffset2doctag[candidate_offset]
    else:
        return i_index


# Traceback (AttributeError)
from gensim.models import Doc2Vec

model = Doc2Vec.load('some-3.2-model/model')
model.infer_vector(['foo'])

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-13-851ff3602fcf> in <module>()
----> 1 model.infer_vector(['foo'])

~/anaconda2/envs/faker/lib/python3.6/site-packages/gensim/models/doc2vec.py in infer_vector(self, doc_words, alpha, min_alpha, steps)
541
542         """
--> 543         doctag_vectors, doctag_locks = self.trainables.get_doctag_trainables(doc_words, self.docvecs.vector_size)
544         doctag_indexes = [0]
545         work = zeros(self.trainables.layer1_size, dtype=REAL)

AttributeError: 'DocvecsArray' object has no attribute 'vector_size'

# Fixed code
def index_to_doctag(self, i_index):
    """Return string key for given i_index, if available. Otherwise return raw int doctag (same int)."""
    candidate_offset = i_index - self.max_rawint - 1
    if 0 <= candidate_offset < len(self.offset2doctag):
        return self.offset2doctag[candidate_offset]
    else:
        return i_index
# Buggy code
def __init__(self, *args):
    super(WordOccurrenceAccumulator, self).__init__(*args)
    self._occurrences = np.zeros(self._vocab_size, dtype="uint32")
    self._co_occurrences = sps.lil_matrix(
        (self._vocab_size, self._vocab_size), dtype="uint32"
    )

    self._uniq_words = np.zeros(
        (self._vocab_size + 1,), dtype=bool
    )  # add 1 for none token
    self._mask = self._uniq_words[:-1]  # to exclude none token
    self._counter = Counter()


# Traceback (AssertionError)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Python35-x64\lib\site-packages\gensim\test\test_text_analysis.py", line 57, in test_occurrence_counting
self.assertEqual(3, accumulator.get_occurrences("this"))
AssertionError: 3 != 0
-------------------- >> begin captured logging << --------------------
gensim.topic_coherence.text_analysis: INFO: 1 batches submitted to accumulate stats from 64 documents (3 virtual)
gensim.topic_coherence.text_analysis: INFO: 2 accumulators retrieved from output queue
gensim.topic_coherence.text_analysis: INFO: accumulated word occurrence stats for 4 virtual documents
--------------------- >> end captured logging << ---------------------
======================================================================
FAIL: test_occurrence_counting2 (gensim.test.test_text_analysis.TestParallelWordOccurrenceAccumulator)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Python35-x64\lib\site-packages\gensim\test\test_text_analysis.py", line 67, in test_occurrence_counting2
self.assertEqual(2, accumulator.get_occurrences("human"))
AssertionError: 2 != 0
-------------------- >> begin captured logging << --------------------
gensim.topic_coherence.text_analysis: INFO: 2 accumulators retrieved from output queue
gensim.topic_coherence.text_analysis: INFO: accumulated word occurrence stats for 10 virtual documents
--------------------- >> end captured logging << ---------------------

# Fixed code
def __init__(self, *args):
    super(WordOccurrenceAccumulator, self).__init__(*args)
    self._occurrences = np.zeros(self._vocab_size, dtype="uint32")
    self._co_occurrences = sps.lil_matrix(
        (self._vocab_size, self._vocab_size), dtype="uint32"
    )

    self._uniq_words = np.zeros(
        (self._vocab_size + 1,), dtype=bool
    )  # add 1 for none token
    self._counter = Counter()
# Buggy code
def analyze_text(self, window, doc_num=None):
    self._slide_window(window, doc_num)
    if self._mask.any():
        self._occurrences[self._mask] += 1
        self._counter.update(itertools.combinations(np.nonzero(self._mask)[0], 2))


# Traceback (AssertionError)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Python35-x64\lib\site-packages\gensim\test\test_text_analysis.py", line 57, in test_occurrence_counting
self.assertEqual(3, accumulator.get_occurrences("this"))
AssertionError: 3 != 0
-------------------- >> begin captured logging << --------------------
gensim.topic_coherence.text_analysis: INFO: 1 batches submitted to accumulate stats from 64 documents (3 virtual)
gensim.topic_coherence.text_analysis: INFO: 2 accumulators retrieved from output queue
gensim.topic_coherence.text_analysis: INFO: accumulated word occurrence stats for 4 virtual documents
--------------------- >> end captured logging << ---------------------
======================================================================
FAIL: test_occurrence_counting2 (gensim.test.test_text_analysis.TestParallelWordOccurrenceAccumulator)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Python35-x64\lib\site-packages\gensim\test\test_text_analysis.py", line 67, in test_occurrence_counting2
self.assertEqual(2, accumulator.get_occurrences("human"))
AssertionError: 2 != 0
-------------------- >> begin captured logging << --------------------
gensim.topic_coherence.text_analysis: INFO: 2 accumulators retrieved from output queue
gensim.topic_coherence.text_analysis: INFO: accumulated word occurrence stats for 10 virtual documents
--------------------- >> end captured logging << ---------------------

# Fixed code
def analyze_text(self, window, doc_num=None):
    self._slide_window(window, doc_num)
    mask = self._uniq_words[:-1]  # to exclude none token
    if mask.any():
        self._occurrences[mask] += 1
        self._counter.update(itertools.combinations(np.nonzero(mask)[0], 2))
# Buggy code
def _symmetrize(self):
    """Word pairs may have been encountered in (i, j) and (j, i) order.
    Rather than enforcing a particular ordering during the update process,
    we choose to symmetrize the co-occurrence matrix after accumulation has completed.
    """
    co_occ = self._co_occurrences
    co_occ.setdiag(self._occurrences)  # diagonal should be equal to occurrence counts
    self._co_occurrences = (
        co_occ + co_occ.T - sps.diags(co_occ.diagonal(), dtype="uint32")
    )


# Traceback (AssertionError)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Python35-x64\lib\site-packages\gensim\test\test_text_analysis.py", line 57, in test_occurrence_counting
self.assertEqual(3, accumulator.get_occurrences("this"))
AssertionError: 3 != 0
-------------------- >> begin captured logging << --------------------
gensim.topic_coherence.text_analysis: INFO: 1 batches submitted to accumulate stats from 64 documents (3 virtual)
gensim.topic_coherence.text_analysis: INFO: 2 accumulators retrieved from output queue
gensim.topic_coherence.text_analysis: INFO: accumulated word occurrence stats for 4 virtual documents
--------------------- >> end captured logging << ---------------------
======================================================================
FAIL: test_occurrence_counting2 (gensim.test.test_text_analysis.TestParallelWordOccurrenceAccumulator)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Python35-x64\lib\site-packages\gensim\test\test_text_analysis.py", line 67, in test_occurrence_counting2
self.assertEqual(2, accumulator.get_occurrences("human"))
AssertionError: 2 != 0
-------------------- >> begin captured logging << --------------------
gensim.topic_coherence.text_analysis: INFO: 2 accumulators retrieved from output queue
gensim.topic_coherence.text_analysis: INFO: accumulated word occurrence stats for 10 virtual documents
--------------------- >> end captured logging << ---------------------

# Fixed code
def _symmetrize(self):
    """Word pairs may have been encountered in (i, j) and (j, i) order.
    Rather than enforcing a particular ordering during the update process,
    we choose to symmetrize the co-occurrence matrix after accumulation has completed.
    """
    co_occ = self._co_occurrences
    co_occ.setdiag(self._occurrences)  # diagonal should be equal to occurrence counts
    self._co_occurrences = (
        co_occ + co_occ.T - sps.diags(co_occ.diagonal(), offsets=0, dtype="uint32")
    )
# Buggy code
def load_fasttext_format(cls, model_file, encoding="utf8"):
    """
    Load the input-hidden weight matrix from the fast text output files.

    Note that due to limitations in the FastText API, you cannot continue training
    with a model loaded this way, though you can query for word similarity etc.

    `model_file` is the path to the FastText output files.
    FastText outputs two training files - `/path/to/train.vec` and `/path/to/train.bin`
    Expected value for this example: `/path/to/train`

    """
    model = cls()
    model.wv = cls.load_word2vec_format("%s.vec" % model_file, encoding=encoding)
    model.load_binary_data("%s.bin" % model_file, encoding=encoding)
    return model


# Traceback (AssertionError)
Traceback (most recent call last):
File "app.py", line 18, in <module>
model = FastText.load_fasttext_format(os.path.abspath('./wiki.fr'))
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/gensim/models/wrappers/fasttext.py", line 238, in load_fasttext_format
model.load_binary_data('%s.bin' % model_file)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/gensim/models/wrappers/fasttext.py", line 255, in load_binary_data
self.load_dict(f)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/gensim/models/wrappers/fasttext.py", line 277, in load_dict
assert len(self.wv.vocab) == vocab_size, 'mismatch between vocab sizes'
AssertionError: mismatch between vocab sizes

# Fixed code
def load_fasttext_format(cls, model_file, encoding="utf8"):
    """
    Load the input-hidden weight matrix from the fast text output files.

    Note that due to limitations in the FastText API, you cannot continue training
    with a model loaded this way, though you can query for word similarity etc.

    `model_file` is the path to the FastText output files.
    FastText outputs two model files - `/path/to/model.vec` and `/path/to/model.bin`

    Expected value for this example: `/path/to/model` or `/path/to/model.bin`,
    as gensim requires only `.bin` file to load entire fastText model.

    """
    model = cls()
    if not model_file.endswith(".bin"):
        model_file += ".bin"
    model.file_name = model_file
    model.load_binary_data(encoding=encoding)
    return model
# Buggy code
def load_binary_data(self, model_binary_file, encoding="utf8"):
    """Loads data from the output binary file created by FastText training"""
    with utils.smart_open(model_binary_file, "rb") as f:
        self.load_model_params(f)
        self.load_dict(f, encoding=encoding)
        self.load_vectors(f)


# Traceback (AssertionError)
Traceback (most recent call last):
File "app.py", line 18, in <module>
model = FastText.load_fasttext_format(os.path.abspath('./wiki.fr'))
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/gensim/models/wrappers/fasttext.py", line 238, in load_fasttext_format
model.load_binary_data('%s.bin' % model_file)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/gensim/models/wrappers/fasttext.py", line 255, in load_binary_data
self.load_dict(f)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/gensim/models/wrappers/fasttext.py", line 277, in load_dict
assert len(self.wv.vocab) == vocab_size, 'mismatch between vocab sizes'
AssertionError: mismatch between vocab sizes

# Fixed code
def load_binary_data(self, encoding="utf8"):
    """Loads data from the output binary file created by FastText training"""
    with utils.smart_open(self.file_name, "rb") as f:
        self.load_model_params(f)
        self.load_dict(f, encoding=encoding)
        self.load_vectors(f)
# Buggy code
def load_vectors(self, file_handle):
    if self.new_format:
        self.struct_unpack(file_handle, "@?")  # bool quant_input in fasttext.cc
    num_vectors, dim = self.struct_unpack(file_handle, "@2q")
    # Vectors stored by [Matrix::save](https://github.com/facebookresearch/fastText/blob/master/src/matrix.cc)
    assert self.vector_size == dim, "mismatch between model sizes"
    float_size = struct.calcsize("@f")
    if float_size == 4:
        dtype = np.dtype(np.float32)
    elif float_size == 8:
        dtype = np.dtype(np.float64)

    self.num_original_vectors = num_vectors
    self.wv.syn0_all = np.fromfile(file_handle, dtype=dtype, count=num_vectors * dim)
    self.wv.syn0_all = self.wv.syn0_all.reshape((num_vectors, dim))
    assert self.wv.syn0_all.shape == (
        self.bucket + len(self.wv.vocab),
        self.vector_size,
    ), "mismatch between weight matrix shape and vocab/model size"
    self.init_ngrams()


# Traceback (AssertionError)
Traceback (most recent call last):
File "app.py", line 18, in <module>
model = FastText.load_fasttext_format(os.path.abspath('./wiki.fr'))
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/gensim/models/wrappers/fasttext.py", line 238, in load_fasttext_format
model.load_binary_data('%s.bin' % model_file)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/gensim/models/wrappers/fasttext.py", line 255, in load_binary_data
self.load_dict(f)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/gensim/models/wrappers/fasttext.py", line 277, in load_dict
assert len(self.wv.vocab) == vocab_size, 'mismatch between vocab sizes'
AssertionError: mismatch between vocab sizes

# Fixed code
def load_vectors(self, file_handle):
    if self.new_format:
        self.struct_unpack(file_handle, "@?")  # bool quant_input in fasttext.cc
    num_vectors, dim = self.struct_unpack(file_handle, "@2q")
    # Vectors stored by [Matrix::save](https://github.com/facebookresearch/fastText/blob/master/src/matrix.cc)
    assert self.vector_size == dim, (
        "mismatch between vector size in model params ({}) and model vectors ({})".format(
            self.vector_size, dim
        )
    )
    float_size = struct.calcsize("@f")
    if float_size == 4:
        dtype = np.dtype(np.float32)
    elif float_size == 8:
        dtype = np.dtype(np.float64)

    self.num_original_vectors = num_vectors
    self.wv.syn0_all = np.fromfile(file_handle, dtype=dtype, count=num_vectors * dim)
    self.wv.syn0_all = self.wv.syn0_all.reshape((num_vectors, dim))
    assert self.wv.syn0_all.shape == (
        self.bucket + len(self.wv.vocab),
        self.vector_size,
    ), "mismatch between actual weight matrix shape {} and expected shape {}".format(
        self.wv.syn0_all.shape, (self.bucket + len(self.wv.vocab), self.vector_size)
    )

    self.init_ngrams()
# Buggy code
def add_transform_to_subject_history(self, subject):
    from .augmentation import RandomTransform
    from . import Compose, OneOf, CropOrPad, EnsureShapeMultiple
    from .preprocessing.label import SequentialLabels

    call_others = (
        RandomTransform,
        Compose,
        OneOf,
        CropOrPad,
        EnsureShapeMultiple,
        SequentialLabels,
    )
    if not isinstance(self, call_others):
        subject.add_transform(self, self._get_reproducing_arguments())


# Traceback (TypeError)
assert logits.shape == target.shape #1,4,16,16,16 vs. 1,1,16,16,16
AssertionError

Exception ignored in: <function tqdm.__del__ at 0x7f2fef73a550>
Traceback (most recent call last):
File "/home/nico/.local/lib/python3.8/site-packages/tqdm/std.py", line 1090, in __del__
File "/home/nico/.local/lib/python3.8/site-packages/tqdm/std.py", line 1303, in close
File "/home/nico/.local/lib/python3.8/site-packages/tqdm/std.py", line 1481, in display
File "/home/nico/.local/lib/python3.8/site-packages/tqdm/std.py", line 1093, in __repr__
File "/home/nico/.local/lib/python3.8/site-packages/tqdm/std.py", line 1443, in format_dict
TypeError: cannot unpack non-iterable NoneType object

Process finished with exit code 1

# Fixed code
def add_transform_to_subject_history(self, subject):
    from .augmentation import RandomTransform
    from . import Compose, OneOf, CropOrPad, EnsureShapeMultiple
    from .preprocessing import SequentialLabels

    call_others = (
        RandomTransform,
        Compose,
        OneOf,
        CropOrPad,
        EnsureShapeMultiple,
        SequentialLabels,
    )
    if not isinstance(self, call_others):
        subject.add_transform(self, self._get_reproducing_arguments())
# Buggy code
def mean(tensor: torch.Tensor) -> torch.Tensor:
    mask = tensor > tensor.mean()
    return mask


# Traceback (RuntimeError)
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-5-f7eeffa30bdc> in <module>()
10 ]
11 transform = tio.Compose(transforms)
---> 12 preprocessed = transform(subject)

5 frames
/usr/local/lib/python3.6/dist-packages/torchio/transforms/transform.py in __call__(self, data)
100             subject = copy.copy(subject)
101         with np.errstate(all='raise'):
--> 102             transformed = self.apply_transform(subject)
103         self.add_transform_to_subject_history(transformed)
104         for image in transformed.get_images(intensity_only=False):

/usr/local/lib/python3.6/dist-packages/torchio/transforms/augmentation/composition.py in apply_transform(self, subject)
44     def apply_transform(self, subject: Subject) -> Subject:
45         for transform in self.transforms:
---> 46             subject = transform(subject)
47         return subject
48

/usr/local/lib/python3.6/dist-packages/torchio/transforms/transform.py in __call__(self, data)
100             subject = copy.copy(subject)
101         with np.errstate(all='raise'):
--> 102             transformed = self.apply_transform(subject)
103         self.add_transform_to_subject_history(transformed)
104         for image in transformed.get_images(intensity_only=False):

/usr/local/lib/python3.6/dist-packages/torchio/transforms/preprocessing/intensity/normalization_transform.py in apply_transform(self, subject)
46     def apply_transform(self, subject: Subject) -> Subject:
47         for image_name, image in self.get_images_dict(subject).items():
---> 48             mask = Transform.get_mask(self.masking_method, subject, image.data)
49             self.apply_normalization(subject, image_name, mask)
50         return subject

/usr/local/lib/python3.6/dist-packages/torchio/transforms/transform.py in get_mask(masking_method, subject, tensor)
394             return Transform.ones(tensor)
395         elif callable(masking_method):
--> 396             return masking_method(tensor)
397         elif type(masking_method) is str:
398             if masking_method in subject and isinstance(subject[masking_method], LabelMap):

/usr/local/lib/python3.6/dist-packages/torchio/transforms/transform.py in mean(tensor)
386     @staticmethod
387     def mean(tensor: torch.Tensor) -> torch.Tensor:
--> 388         mask = tensor > tensor.mean()
389         return mask
390

RuntimeError: Can only calculate the mean of floating types. Got Short instead.

# Fixed code
def mean(tensor: torch.Tensor) -> torch.Tensor:
    mask = tensor > tensor.float().mean()
    return mask
# Buggy code
def _parse_path(
    self, path: Union[TypePath, Sequence[TypePath]]
) -> Union[Path, List[Path]]:
    if path is None:
        return None
    if isinstance(path, (str, Path)):
        return self._parse_single_path(path)
    else:
        return [self._parse_single_path(p) for p in path]


# Traceback (AttributeError)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-4-bf55f6468bbb> in <module>
----> 1 x.as_pil()

AttributeError: 'numpy.ndarray' object has no attribute 'as_pil'

In [5]: im = tio.ScalarImage(tensor=x)

In [6]: im.as_pil()
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
/usr/local/Caskroom/miniconda/base/envs/torchio/lib/python3.8/site-packages/PIL/Image.py in open(fp, mode)
2881     try:
-> 2882         fp.seek(0)
2883     except (AttributeError, io.UnsupportedOperation):

AttributeError: 'NoneType' object has no attribute 'seek'

During handling of the above exception, another exception occurred:

AttributeError                            Traceback (most recent call last)
<ipython-input-6-bc29f2434f0a> in <module>
----> 1 im.as_pil()

~/git/torchio/torchio/data/image.py in as_pil(self)
466         """Get the image as an instance of :class:`PIL.Image`."""
467         self.check_is_2d()
--> 468         return ImagePIL.open(self.path)
469
470     def get_center(self, lps: bool = False) -> TypeTripletFloat:

/usr/local/Caskroom/miniconda/base/envs/torchio/lib/python3.8/site-packages/PIL/Image.py in open(fp, mode)
2882         fp.seek(0)
2883     except (AttributeError, io.UnsupportedOperation):
-> 2884         fp = io.BytesIO(fp.read())
2885         exclusive_fp = True
2886

# Fixed code
def _parse_path(
    self, path: Union[TypePath, Sequence[TypePath]]
) -> Optional[Union[Path, List[Path]]]:
    if path is None:
        return None
    if isinstance(path, (str, Path)):
        return self._parse_single_path(path)
    else:
        return [self._parse_single_path(p) for p in path]
# Buggy code
def as_pil(self) -> ImagePIL:
    """Get the image as an instance of :class:`PIL.Image`."""
    self.check_is_2d()
    return ImagePIL.open(self.path)


# Traceback (AttributeError)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-4-bf55f6468bbb> in <module>
----> 1 x.as_pil()

AttributeError: 'numpy.ndarray' object has no attribute 'as_pil'

In [5]: im = tio.ScalarImage(tensor=x)

In [6]: im.as_pil()
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
/usr/local/Caskroom/miniconda/base/envs/torchio/lib/python3.8/site-packages/PIL/Image.py in open(fp, mode)
2881     try:
-> 2882         fp.seek(0)
2883     except (AttributeError, io.UnsupportedOperation):

AttributeError: 'NoneType' object has no attribute 'seek'

During handling of the above exception, another exception occurred:

AttributeError                            Traceback (most recent call last)
<ipython-input-6-bc29f2434f0a> in <module>
----> 1 im.as_pil()

~/git/torchio/torchio/data/image.py in as_pil(self)
466         """Get the image as an instance of :class:`PIL.Image`."""
467         self.check_is_2d()
--> 468         return ImagePIL.open(self.path)
469
470     def get_center(self, lps: bool = False) -> TypeTripletFloat:

/usr/local/Caskroom/miniconda/base/envs/torchio/lib/python3.8/site-packages/PIL/Image.py in open(fp, mode)
2882         fp.seek(0)
2883     except (AttributeError, io.UnsupportedOperation):
-> 2884         fp = io.BytesIO(fp.read())
2885         exclusive_fp = True
2886

# Fixed code
def as_pil(self) -> ImagePIL:
    """Get the image as an instance of :class:`PIL.Image`.

    .. note:: Values will be clamped to 0-255 and cast to uint8.
    """
    self.check_is_2d()
    tensor = self.data
    if len(tensor) == 1:
        tensor = torch.cat(3 * [tensor])
    if len(tensor) != 3:
        raise RuntimeError("The image must have 1 or 3 channels")
    tensor = tensor.permute(3, 1, 2, 0)[0]
    array = tensor.clamp(0, 255).numpy()
    return ImagePIL.fromarray(array.astype(np.uint8))
# Buggy code
def __init__(self, *args, **kwargs: Dict[str, Any]):
    if args:
        if len(args) == 1 and isinstance(args[0], dict):
            kwargs.update(args[0])
        else:
            message = "Only one dictionary as positional argument is allowed"
            raise ValueError(message)
    super().__init__(**kwargs)
    self.images = [(k, v) for (k, v) in self.items() if isinstance(v, Image)]
    self._parse_images(self.images)
    self.update_attributes()  # this allows me to do e.g. subject.t1
    self.history = []


# Traceback (ValueError)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-2-6b7dc2edb3cc> in <module>
----> 1 icbm.spatial_shape

~/git/torchio/torchio/data/subject.py in spatial_shape(self)
95         Consistency of shapes across images in the subject is checked first.
96         """
---> 97         return self.shape[1:]
98
99     @property

~/git/torchio/torchio/data/subject.py in shape(self)
85         Consistency of shapes across images in the subject is checked first.
86         """
---> 87         self.check_consistent_shape()
88         image = self.get_images(intensity_only=False)[0]
89         return image.shape

~/git/torchio/torchio/data/subject.py in check_consistent_shape(self)
135                 f'\n{pprint.pformat(shapes_dict)}'
136             )
--> 137             raise ValueError(message)
138
139     def check_consistent_orientation(self) -> None:

ValueError: Images in subject have inconsistent shapes:
{'brain': (1, 193, 229, 193),
'eyes': (1, 193, 229, 193),
'pd': (1, 193, 229, 193),
't1': (1, 193, 229, 193),
't2': (1, 193, 229, 193),
'tissues': (3, 193, 229, 193)}

# Fixed code
def __init__(self, *args, **kwargs: Dict[str, Any]):
    if args:
        if len(args) == 1 and isinstance(args[0], dict):
            kwargs.update(args[0])
        else:
            message = "Only one dictionary as positional argument is allowed"
            raise ValueError(message)
    super().__init__(**kwargs)
    self._parse_images(self.get_images(intensity_only=False))
    self.update_attributes()  # this allows me to do e.g. subject.t1
    self.history = []
# Buggy code
def __repr__(self):
    string = (
        f"{self.__class__.__name__}"
        f"(Keys: {tuple(self.keys())}; images: {len(self.images)})"
    )
    return string


# Traceback (ValueError)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-2-6b7dc2edb3cc> in <module>
----> 1 icbm.spatial_shape

~/git/torchio/torchio/data/subject.py in spatial_shape(self)
95         Consistency of shapes across images in the subject is checked first.
96         """
---> 97         return self.shape[1:]
98
99     @property

~/git/torchio/torchio/data/subject.py in shape(self)
85         Consistency of shapes across images in the subject is checked first.
86         """
---> 87         self.check_consistent_shape()
88         image = self.get_images(intensity_only=False)[0]
89         return image.shape

~/git/torchio/torchio/data/subject.py in check_consistent_shape(self)
135                 f'\n{pprint.pformat(shapes_dict)}'
136             )
--> 137             raise ValueError(message)
138
139     def check_consistent_orientation(self) -> None:

ValueError: Images in subject have inconsistent shapes:
{'brain': (1, 193, 229, 193),
'eyes': (1, 193, 229, 193),
'pd': (1, 193, 229, 193),
't1': (1, 193, 229, 193),
't2': (1, 193, 229, 193),
'tissues': (3, 193, 229, 193)}

# Fixed code
def __repr__(self):
    num_images = len(self.get_images(intensity_only=False))
    string = (
        f"{self.__class__.__name__}(Keys: {tuple(self.keys())}; images: {num_images})"
    )
    return string
# Buggy code
def shape(self):
    """Return shape of first image in subject.

    Consistency of shapes across images in the subject is checked first.
    """
    self.check_consistent_shape()
    image = self.get_images(intensity_only=False)[0]
    return image.shape


# Traceback (ValueError)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-2-6b7dc2edb3cc> in <module>
----> 1 icbm.spatial_shape

~/git/torchio/torchio/data/subject.py in spatial_shape(self)
95         Consistency of shapes across images in the subject is checked first.
96         """
---> 97         return self.shape[1:]
98
99     @property

~/git/torchio/torchio/data/subject.py in shape(self)
85         Consistency of shapes across images in the subject is checked first.
86         """
---> 87         self.check_consistent_shape()
88         image = self.get_images(intensity_only=False)[0]
89         return image.shape

~/git/torchio/torchio/data/subject.py in check_consistent_shape(self)
135                 f'\n{pprint.pformat(shapes_dict)}'
136             )
--> 137             raise ValueError(message)
138
139     def check_consistent_orientation(self) -> None:

ValueError: Images in subject have inconsistent shapes:
{'brain': (1, 193, 229, 193),
'eyes': (1, 193, 229, 193),
'pd': (1, 193, 229, 193),
't1': (1, 193, 229, 193),
't2': (1, 193, 229, 193),
'tissues': (3, 193, 229, 193)}

# Fixed code
def shape(self):
    """Return shape of first image in subject.

    Consistency of shapes across images in the subject is checked first.
    """
    self.check_consistent_attribute("shape")
    return self.get_first_image().shape
# Buggy code
def spatial_shape(self):
    """Return spatial shape of first image in subject.

    Consistency of shapes across images in the subject is checked first.
    """
    return self.shape[1:]


# Traceback (ValueError)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-2-6b7dc2edb3cc> in <module>
----> 1 icbm.spatial_shape

~/git/torchio/torchio/data/subject.py in spatial_shape(self)
95         Consistency of shapes across images in the subject is checked first.
96         """
---> 97         return self.shape[1:]
98
99     @property

~/git/torchio/torchio/data/subject.py in shape(self)
85         Consistency of shapes across images in the subject is checked first.
86         """
---> 87         self.check_consistent_shape()
88         image = self.get_images(intensity_only=False)[0]
89         return image.shape

~/git/torchio/torchio/data/subject.py in check_consistent_shape(self)
135                 f'\n{pprint.pformat(shapes_dict)}'
136             )
--> 137             raise ValueError(message)
138
139     def check_consistent_orientation(self) -> None:

ValueError: Images in subject have inconsistent shapes:
{'brain': (1, 193, 229, 193),
'eyes': (1, 193, 229, 193),
'pd': (1, 193, 229, 193),
't1': (1, 193, 229, 193),
't2': (1, 193, 229, 193),
'tissues': (3, 193, 229, 193)}

# Fixed code
def spatial_shape(self):
    """Return spatial shape of first image in subject.

    Consistency of spatial shapes across images in the subject is checked
    first.
    """
    self.check_consistent_spatial_shape()
    return self.get_first_image().spatial_shape
# Buggy code
def spacing(self):
    """Return spacing of first image in subject.

    Consistency of shapes across images in the subject is checked first.
    """
    self.check_consistent_shape()
    image = self.get_images(intensity_only=False)[0]
    return image.spacing


# Traceback (ValueError)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-2-6b7dc2edb3cc> in <module>
----> 1 icbm.spatial_shape

~/git/torchio/torchio/data/subject.py in spatial_shape(self)
95         Consistency of shapes across images in the subject is checked first.
96         """
---> 97         return self.shape[1:]
98
99     @property

~/git/torchio/torchio/data/subject.py in shape(self)
85         Consistency of shapes across images in the subject is checked first.
86         """
---> 87         self.check_consistent_shape()
88         image = self.get_images(intensity_only=False)[0]
89         return image.shape

~/git/torchio/torchio/data/subject.py in check_consistent_shape(self)
135                 f'\n{pprint.pformat(shapes_dict)}'
136             )
--> 137             raise ValueError(message)
138
139     def check_consistent_orientation(self) -> None:

ValueError: Images in subject have inconsistent shapes:
{'brain': (1, 193, 229, 193),
'eyes': (1, 193, 229, 193),
'pd': (1, 193, 229, 193),
't1': (1, 193, 229, 193),
't2': (1, 193, 229, 193),
'tissues': (3, 193, 229, 193)}

# Fixed code
def spacing(self):
    """Return spacing of first image in subject.

    Consistency of spacings across images in the subject is checked first.
    """
    self.check_consistent_attribute("spacing")
    return self.get_first_image().spacing
# Buggy code
def check_consistent_shape(self) -> None:
    shapes_dict = {}
    iterable = self.get_images_dict(intensity_only=False).items()
    for image_name, image in iterable:
        shapes_dict[image_name] = image.shape
    num_unique_shapes = len(set(shapes_dict.values()))
    if num_unique_shapes > 1:
        message = (
            "Images in subject have inconsistent shapes:"
            f"\n{pprint.pformat(shapes_dict)}"
        )
        raise ValueError(message)


# Traceback (ValueError)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-2-6b7dc2edb3cc> in <module>
----> 1 icbm.spatial_shape

~/git/torchio/torchio/data/subject.py in spatial_shape(self)
95         Consistency of shapes across images in the subject is checked first.
96         """
---> 97         return self.shape[1:]
98
99     @property

~/git/torchio/torchio/data/subject.py in shape(self)
85         Consistency of shapes across images in the subject is checked first.
86         """
---> 87         self.check_consistent_shape()
88         image = self.get_images(intensity_only=False)[0]
89         return image.shape

~/git/torchio/torchio/data/subject.py in check_consistent_shape(self)
135                 f'\n{pprint.pformat(shapes_dict)}'
136             )
--> 137             raise ValueError(message)
138
139     def check_consistent_orientation(self) -> None:

ValueError: Images in subject have inconsistent shapes:
{'brain': (1, 193, 229, 193),
'eyes': (1, 193, 229, 193),
'pd': (1, 193, 229, 193),
't1': (1, 193, 229, 193),
't2': (1, 193, 229, 193),
'tissues': (3, 193, 229, 193)}

# Fixed code
def check_consistent_shape(self) -> None:
    self.check_consistent_attribute("shape")
# Buggy code
def add_image(self, image, image_name):
    self[image_name] = image
    self.update_attributes()


# Traceback (ValueError)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-2-6b7dc2edb3cc> in <module>
----> 1 icbm.spatial_shape

~/git/torchio/torchio/data/subject.py in spatial_shape(self)
95         Consistency of shapes across images in the subject is checked first.
96         """
---> 97         return self.shape[1:]
98
99     @property

~/git/torchio/torchio/data/subject.py in shape(self)
85         Consistency of shapes across images in the subject is checked first.
86         """
---> 87         self.check_consistent_shape()
88         image = self.get_images(intensity_only=False)[0]
89         return image.shape

~/git/torchio/torchio/data/subject.py in check_consistent_shape(self)
135                 f'\n{pprint.pformat(shapes_dict)}'
136             )
--> 137             raise ValueError(message)
138
139     def check_consistent_orientation(self) -> None:

ValueError: Images in subject have inconsistent shapes:
{'brain': (1, 193, 229, 193),
'eyes': (1, 193, 229, 193),
'pd': (1, 193, 229, 193),
't1': (1, 193, 229, 193),
't2': (1, 193, 229, 193),
'tissues': (3, 193, 229, 193)}

# Fixed code
def add_image(self, image: Image, image_name: str) -> None:
    self[image_name] = image
    self.update_attributes()
# Buggy code
def apply_transform(self, sample: Subject) -> dict:
    sample.check_consistent_shape()
    bspline_params = self.get_params(
        self.num_control_points,
        self.max_displacement,
        self.num_locked_borders,
    )
    for image in self.get_images(sample):
        if image[TYPE] != INTENSITY:
            interpolation = Interpolation.NEAREST
        else:
            interpolation = self.interpolation
        if image.is_2d():
            bspline_params[..., -3] = 0  # no displacement in LR axis
        image[DATA] = self.apply_bspline_transform(
            image[DATA],
            image[AFFINE],
            bspline_params,
            interpolation,
        )
    random_parameters_dict = {"coarse_grid": bspline_params}
    sample.add_transform(self, random_parameters_dict)
    return sample


# Traceback (ValueError)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-2-6b7dc2edb3cc> in <module>
----> 1 icbm.spatial_shape

~/git/torchio/torchio/data/subject.py in spatial_shape(self)
95         Consistency of shapes across images in the subject is checked first.
96         """
---> 97         return self.shape[1:]
98
99     @property

~/git/torchio/torchio/data/subject.py in shape(self)
85         Consistency of shapes across images in the subject is checked first.
86         """
---> 87         self.check_consistent_shape()
88         image = self.get_images(intensity_only=False)[0]
89         return image.shape

~/git/torchio/torchio/data/subject.py in check_consistent_shape(self)
135                 f'\n{pprint.pformat(shapes_dict)}'
136             )
--> 137             raise ValueError(message)
138
139     def check_consistent_orientation(self) -> None:

ValueError: Images in subject have inconsistent shapes:
{'brain': (1, 193, 229, 193),
'eyes': (1, 193, 229, 193),
'pd': (1, 193, 229, 193),
't1': (1, 193, 229, 193),
't2': (1, 193, 229, 193),
'tissues': (3, 193, 229, 193)}

# Fixed code
def apply_transform(self, sample: Subject) -> dict:
    sample.check_consistent_spatial_shape()
    bspline_params = self.get_params(
        self.num_control_points,
        self.max_displacement,
        self.num_locked_borders,
    )
    for image in self.get_images(sample):
        if image[TYPE] != INTENSITY:
            interpolation = Interpolation.NEAREST
        else:
            interpolation = self.interpolation
        if image.is_2d():
            bspline_params[..., -3] = 0  # no displacement in LR axis
        image[DATA] = self.apply_bspline_transform(
            image[DATA],
            image[AFFINE],
            bspline_params,
            interpolation,
        )
    random_parameters_dict = {"coarse_grid": bspline_params}
    sample.add_transform(self, random_parameters_dict)
    return sample
# Buggy code
def _get_sample_shape(sample: Subject) -> TypeTripletInt:
    """Return the shape of the first image in the sample."""
    sample.check_consistent_shape()
    for image_dict in sample.get_images(intensity_only=False):
        data = image_dict.spatial_shape  # remove channels dimension
        break
    return data


# Traceback (ValueError)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-2-6b7dc2edb3cc> in <module>
----> 1 icbm.spatial_shape

~/git/torchio/torchio/data/subject.py in spatial_shape(self)
95         Consistency of shapes across images in the subject is checked first.
96         """
---> 97         return self.shape[1:]
98
99     @property

~/git/torchio/torchio/data/subject.py in shape(self)
85         Consistency of shapes across images in the subject is checked first.
86         """
---> 87         self.check_consistent_shape()
88         image = self.get_images(intensity_only=False)[0]
89         return image.shape

~/git/torchio/torchio/data/subject.py in check_consistent_shape(self)
135                 f'\n{pprint.pformat(shapes_dict)}'
136             )
--> 137             raise ValueError(message)
138
139     def check_consistent_orientation(self) -> None:

ValueError: Images in subject have inconsistent shapes:
{'brain': (1, 193, 229, 193),
'eyes': (1, 193, 229, 193),
'pd': (1, 193, 229, 193),
't1': (1, 193, 229, 193),
't2': (1, 193, 229, 193),
'tissues': (3, 193, 229, 193)}

# Fixed code
def _get_sample_shape(sample: Subject) -> TypeTripletInt:
    """Return the shape of the first image in the sample."""
    sample.check_consistent_spatial_shape()
    for image_dict in sample.get_images(intensity_only=False):
        data = image_dict.spatial_shape  # remove channels dimension
        break
    return data
# Buggy code
def __init__(
    self,
    num_ghosts: Union[int, Tuple[int, int]] = (4, 10),
    axes: Union[int, Tuple[int, ...]] = (0, 1, 2),
    intensity: Union[float, Tuple[float, float]] = (0.5, 1),
    restore: float = 0.02,
    p: float = 1,
    seed: Optional[int] = None,
):
    super().__init__(p=p, seed=seed)
    if not isinstance(axes, tuple):
        try:
            axes = tuple(axes)
        except TypeError:
            axes = (axes,)
    for axis in axes:
        if axis not in (0, 1, 2):
            raise ValueError(f'Axes must be in (0, 1, 2), not "{axes}"')
    self.axes = axes
    if isinstance(num_ghosts, int):
        self.num_ghosts_range = num_ghosts, num_ghosts
    elif isinstance(num_ghosts, tuple) and len(num_ghosts) == 2:
        self.num_ghosts_range = num_ghosts
    self.intensity_range = self.parse_range(intensity, "intensity")
    for n in self.intensity_range:
        if n < 0:
            message = f"Intensity must be a positive number, not {n}"
            raise ValueError(message)
    if not 0 <= restore < 1:
        message = f"Restore must be a number between 0 and 1, not {restore}"
        raise ValueError(message)
    self.restore = restore


# Traceback (ValueError)
Traceback (most recent call last):
File "/opt/anaconda3/lib/python3.7/site-packages/IPython/core/interactiveshell.py", line 3326, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-3-57113dda3e8e>", line 2, in <module>
tranform = torchio.transforms.RandomGhosting(num_ghosts=10, intensity=1)
File ".../torchio/torchio/transforms/augmentation/intensity/random_ghosting.py", line 63, in __init__
raise ValueError(message)
ValueError: Intensity must be a positive number, not -1

# Fixed code
def __init__(
    self,
    num_ghosts: Union[int, Tuple[int, int]] = (4, 10),
    axes: Union[int, Tuple[int, ...]] = (0, 1, 2),
    intensity: Union[float, Tuple[float, float]] = (0.5, 1),
    restore: float = 0.02,
    p: float = 1,
    seed: Optional[int] = None,
):
    super().__init__(p=p, seed=seed)
    if not isinstance(axes, tuple):
        try:
            axes = tuple(axes)
        except TypeError:
            axes = (axes,)
    for axis in axes:
        if axis not in (0, 1, 2):
            raise ValueError(f'Axes must be in (0, 1, 2), not "{axes}"')
    self.axes = axes
    self.num_ghosts_range = self.parse_num_ghosts(num_ghosts)
    self.intensity_range = self.parse_intensity(intensity)
    if not 0 <= restore < 1:
        message = f"Restore must be a number between 0 and 1, not {restore}"
        raise ValueError(message)
    self.restore = restore
# Buggy code
def __getitem__(self, index: int) -> dict:
    if not isinstance(index, int):
        raise ValueError(f'Index "{index}" must be int, not {type(index)}')
    subject = self.subjects[index]
    sample = copy.deepcopy(subject)

    # Apply transform (this is usually the bottleneck)
    if self._transform is not None:
        sample = self._transform(sample)
    return sample


# Traceback (KeyError)
torch.Size([1, 348, 272, 108])
torch.Size([1, 348, 272, 108])
Traceback (most recent call last):
File "Code/non_local_feature_alignment/dataset_manager.py", line 88, in <module>
print(b["img"][torchio.DATA])
KeyError: 'data'

# Fixed code
def __getitem__(self, index: int) -> dict:
    if not isinstance(index, int):
        raise ValueError(f'Index "{index}" must be int, not {type(index)}')
    subject = self.subjects[index]
    sample = copy.deepcopy(subject)
    sample.load()

    # Apply transform (this is usually the bottleneck)
    if self._transform is not None:
        sample = self._transform(sample)
    return sample
# Buggy code
def load(self) -> Tuple[torch.Tensor, np.ndarray]:
    r"""Load the image from disk.

    The file is expected to be monomodal/grayscale and 2D or 3D.
    A channels dimension is added to the tensor.

    Returns:
        Tuple containing a 4D data tensor of size
        :math:`(1, D_{in}, H_{in}, W_{in})`
        and a 2D 4x4 affine matrix
    """
    if self.path is None:
        raise RuntimeError("No path provided for instance of Image")
    tensor, affine = read_image(self.path)
    # https://github.com/pytorch/pytorch/issues/9410#issuecomment-404968513
    tensor = tensor[(None,) * (3 - tensor.ndim)]  # force to be 3D
    # Remove next line and uncomment the two following ones once/if this issue
    # gets fixed:
    # https://github.com/pytorch/pytorch/issues/29010
    # See also https://discuss.pytorch.org/t/collating-named-tensors/78650/4
    tensor = tensor.unsqueeze(0)  # add channels dimension
    # name_dimensions(tensor, affine)
    # tensor = tensor.align_to('channels', ...)
    if self.check_nans and torch.isnan(tensor).any():
        warnings.warn(f'NaNs found in file "{self.path}"')
    self[DATA] = tensor
    self[AFFINE] = affine
    self._loaded = True


# Traceback (KeyError)
torch.Size([1, 348, 272, 108])
torch.Size([1, 348, 272, 108])
Traceback (most recent call last):
File "Code/non_local_feature_alignment/dataset_manager.py", line 88, in <module>
print(b["img"][torchio.DATA])
KeyError: 'data'

# Fixed code
def load(self) -> Tuple[torch.Tensor, np.ndarray]:
    r"""Load the image from disk.

    The file is expected to be monomodal/grayscale and 2D or 3D.
    A channels dimension is added to the tensor.

    Returns:
        Tuple containing a 4D data tensor of size
        :math:`(1, D_{in}, H_{in}, W_{in})`
        and a 2D 4x4 affine matrix
    """
    if self.path is None:
        return
    tensor, affine = read_image(self.path)
    # https://github.com/pytorch/pytorch/issues/9410#issuecomment-404968513
    tensor = tensor[(None,) * (3 - tensor.ndim)]  # force to be 3D
    # Remove next line and uncomment the two following ones once/if this issue
    # gets fixed:
    # https://github.com/pytorch/pytorch/issues/29010
    # See also https://discuss.pytorch.org/t/collating-named-tensors/78650/4
    tensor = tensor.unsqueeze(0)  # add channels dimension
    # name_dimensions(tensor, affine)
    # tensor = tensor.align_to('channels', ...)
    if self.check_nans and torch.isnan(tensor).any():
        warnings.warn(f'NaNs found in file "{self.path}"')
    self[DATA] = tensor
    self[AFFINE] = affine
    self._loaded = True