Skip to content

Commit d45417a

Browse files
authored
Merge pull request #711 from larsoner/collections
MAINT: Import abstract classes from collections.abc for Python 3.8 compatibility
2 parents 8b1a1b2 + 8fa9b29 commit d45417a

File tree

2 files changed

+22
-14
lines changed

2 files changed

+22
-14
lines changed

nibabel/cifti2/cifti2.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,12 @@
2121
'''
2222
from __future__ import division, print_function, absolute_import
2323
import re
24-
import collections
25-
24+
try:
25+
from collections.abc import MutableSequence, MutableMapping, Iterable
26+
except ImportError:
27+
# PY2 compatibility
28+
from collections import MutableSequence, MutableMapping, Iterable
29+
from collections import OrderedDict
2630
from .. import xmlutils as xml
2731
from ..filebasedimages import FileBasedHeader
2832
from ..dataobj_images import DataobjImage
@@ -104,7 +108,7 @@ def _underscore(string):
104108
return re.sub(r'([a-z0-9])([A-Z])', r'\1_\2', string).lower()
105109

106110

107-
class Cifti2MetaData(xml.XmlSerializable, collections.MutableMapping):
111+
class Cifti2MetaData(xml.XmlSerializable, MutableMapping):
108112
""" A list of name-value pairs
109113
110114
* Description - Provides a simple method for user-supplied metadata that
@@ -124,7 +128,7 @@ class Cifti2MetaData(xml.XmlSerializable, collections.MutableMapping):
124128
data : list of (name, value) tuples
125129
"""
126130
def __init__(self, metadata=None):
127-
self.data = collections.OrderedDict()
131+
self.data = OrderedDict()
128132
if metadata is not None:
129133
self.update(metadata)
130134

@@ -173,7 +177,7 @@ def _to_xml_element(self):
173177
return metadata
174178

175179

176-
class Cifti2LabelTable(xml.XmlSerializable, collections.MutableMapping):
180+
class Cifti2LabelTable(xml.XmlSerializable, MutableMapping):
177181
""" CIFTI2 label table: a sequence of ``Cifti2Label``s
178182
179183
* Description - Used by NamedMap when IndicesMapToDataType is
@@ -191,7 +195,7 @@ class Cifti2LabelTable(xml.XmlSerializable, collections.MutableMapping):
191195
"""
192196

193197
def __init__(self):
194-
self._labels = collections.OrderedDict()
198+
self._labels = OrderedDict()
195199

196200
def __len__(self):
197201
return len(self._labels)
@@ -427,7 +431,7 @@ def _to_xml_element(self):
427431
return surf
428432

429433

430-
class Cifti2VoxelIndicesIJK(xml.XmlSerializable, collections.MutableSequence):
434+
class Cifti2VoxelIndicesIJK(xml.XmlSerializable, MutableSequence):
431435
"""CIFTI2 VoxelIndicesIJK: Set of voxel indices contained in a structure
432436
433437
* Description - Identifies the voxels that model a brain structure, or
@@ -509,7 +513,7 @@ def _to_xml_element(self):
509513
return vox_ind
510514

511515

512-
class Cifti2Vertices(xml.XmlSerializable, collections.MutableSequence):
516+
class Cifti2Vertices(xml.XmlSerializable, MutableSequence):
513517
"""CIFTI2 vertices - association of brain structure and a list of vertices
514518
515519
* Description - Contains a BrainStructure type and a list of vertex indices
@@ -733,7 +737,7 @@ def _to_xml_element(self):
733737
return volume
734738

735739

736-
class Cifti2VertexIndices(xml.XmlSerializable, collections.MutableSequence):
740+
class Cifti2VertexIndices(xml.XmlSerializable, MutableSequence):
737741
"""CIFTI2 vertex indices: vertex indices for an associated brain model
738742
739743
The vertex indices (which are independent for each surface, and
@@ -889,7 +893,7 @@ def _to_xml_element(self):
889893
return brain_model
890894

891895

892-
class Cifti2MatrixIndicesMap(xml.XmlSerializable, collections.MutableSequence):
896+
class Cifti2MatrixIndicesMap(xml.XmlSerializable, MutableSequence):
893897
"""Class for Matrix Indices Map
894898
895899
* Description - Provides a mapping between matrix indices and their
@@ -1076,7 +1080,7 @@ def _to_xml_element(self):
10761080
return mat_ind_map
10771081

10781082

1079-
class Cifti2Matrix(xml.XmlSerializable, collections.MutableSequence):
1083+
class Cifti2Matrix(xml.XmlSerializable, MutableSequence):
10801084
""" CIFTI2 Matrix object
10811085
10821086
This is a list-like container where the elements are instances of
@@ -1122,7 +1126,7 @@ def _get_indices_from_mim(self, mim):
11221126
applies_to_matrix_dimension = mim.applies_to_matrix_dimension
11231127
if not isinstance(
11241128
applies_to_matrix_dimension,
1125-
collections.Iterable
1129+
Iterable
11261130
):
11271131
applies_to_matrix_dimension = (int(applies_to_matrix_dimension),)
11281132
return applies_to_matrix_dimension

nibabel/externals/oset.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@
1515

1616
from __future__ import absolute_import
1717

18-
from collections import MutableSet
18+
try:
19+
from collections.abc import MutableSet
20+
except ImportError:
21+
# PY2 compatibility
22+
from collections import MutableSet
1923

2024
KEY, PREV, NEXT = range(3)
2125

@@ -82,4 +86,4 @@ def __eq__(self, other):
8286
return set(self) == set(other)
8387

8488
def __del__(self):
85-
self.clear() # remove circular references
89+
self.clear() # remove circular references

0 commit comments

Comments
 (0)