Skip to content

Commit

Permalink
Merge pull request #711 from larsoner/collections
Browse files Browse the repository at this point in the history
MAINT: Import abstract classes from collections.abc for Python 3.8 compatibility
  • Loading branch information
effigies authored Jan 14, 2019
2 parents 8b1a1b2 + 8fa9b29 commit d45417a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
28 changes: 16 additions & 12 deletions nibabel/cifti2/cifti2.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,12 @@
'''
from __future__ import division, print_function, absolute_import
import re
import collections

try:
from collections.abc import MutableSequence, MutableMapping, Iterable
except ImportError:
# PY2 compatibility
from collections import MutableSequence, MutableMapping, Iterable
from collections import OrderedDict
from .. import xmlutils as xml
from ..filebasedimages import FileBasedHeader
from ..dataobj_images import DataobjImage
Expand Down Expand Up @@ -104,7 +108,7 @@ def _underscore(string):
return re.sub(r'([a-z0-9])([A-Z])', r'\1_\2', string).lower()


class Cifti2MetaData(xml.XmlSerializable, collections.MutableMapping):
class Cifti2MetaData(xml.XmlSerializable, MutableMapping):
""" A list of name-value pairs
* Description - Provides a simple method for user-supplied metadata that
Expand All @@ -124,7 +128,7 @@ class Cifti2MetaData(xml.XmlSerializable, collections.MutableMapping):
data : list of (name, value) tuples
"""
def __init__(self, metadata=None):
self.data = collections.OrderedDict()
self.data = OrderedDict()
if metadata is not None:
self.update(metadata)

Expand Down Expand Up @@ -173,7 +177,7 @@ def _to_xml_element(self):
return metadata


class Cifti2LabelTable(xml.XmlSerializable, collections.MutableMapping):
class Cifti2LabelTable(xml.XmlSerializable, MutableMapping):
""" CIFTI2 label table: a sequence of ``Cifti2Label``s
* Description - Used by NamedMap when IndicesMapToDataType is
Expand All @@ -191,7 +195,7 @@ class Cifti2LabelTable(xml.XmlSerializable, collections.MutableMapping):
"""

def __init__(self):
self._labels = collections.OrderedDict()
self._labels = OrderedDict()

def __len__(self):
return len(self._labels)
Expand Down Expand Up @@ -427,7 +431,7 @@ def _to_xml_element(self):
return surf


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


class Cifti2Vertices(xml.XmlSerializable, collections.MutableSequence):
class Cifti2Vertices(xml.XmlSerializable, MutableSequence):
"""CIFTI2 vertices - association of brain structure and a list of vertices
* Description - Contains a BrainStructure type and a list of vertex indices
Expand Down Expand Up @@ -733,7 +737,7 @@ def _to_xml_element(self):
return volume


class Cifti2VertexIndices(xml.XmlSerializable, collections.MutableSequence):
class Cifti2VertexIndices(xml.XmlSerializable, MutableSequence):
"""CIFTI2 vertex indices: vertex indices for an associated brain model
The vertex indices (which are independent for each surface, and
Expand Down Expand Up @@ -889,7 +893,7 @@ def _to_xml_element(self):
return brain_model


class Cifti2MatrixIndicesMap(xml.XmlSerializable, collections.MutableSequence):
class Cifti2MatrixIndicesMap(xml.XmlSerializable, MutableSequence):
"""Class for Matrix Indices Map
* Description - Provides a mapping between matrix indices and their
Expand Down Expand Up @@ -1076,7 +1080,7 @@ def _to_xml_element(self):
return mat_ind_map


class Cifti2Matrix(xml.XmlSerializable, collections.MutableSequence):
class Cifti2Matrix(xml.XmlSerializable, MutableSequence):
""" CIFTI2 Matrix object
This is a list-like container where the elements are instances of
Expand Down Expand Up @@ -1122,7 +1126,7 @@ def _get_indices_from_mim(self, mim):
applies_to_matrix_dimension = mim.applies_to_matrix_dimension
if not isinstance(
applies_to_matrix_dimension,
collections.Iterable
Iterable
):
applies_to_matrix_dimension = (int(applies_to_matrix_dimension),)
return applies_to_matrix_dimension
Expand Down
8 changes: 6 additions & 2 deletions nibabel/externals/oset.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@

from __future__ import absolute_import

from collections import MutableSet
try:
from collections.abc import MutableSet
except ImportError:
# PY2 compatibility
from collections import MutableSet

KEY, PREV, NEXT = range(3)

Expand Down Expand Up @@ -82,4 +86,4 @@ def __eq__(self, other):
return set(self) == set(other)

def __del__(self):
self.clear() # remove circular references
self.clear() # remove circular references

0 comments on commit d45417a

Please sign in to comment.