Skip to content

Commit 8f7297f

Browse files
committed
RF: Switch to locally-defined underscore function
Remove externals/inflection _underscore taken from inflection 0.3.1 https://github.com/jpvanhal/inflection/blob/663982e/inflection.py Tests modified from: https://github.com/jpvanhal/inflection/blob/663982e/test_inflection.py#L352-L357
1 parent 9f22915 commit 8f7297f

File tree

4 files changed

+29
-432
lines changed

4 files changed

+29
-432
lines changed

nibabel/cifti2/cifti2.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@
1919
'''
2020
from __future__ import division, print_function, absolute_import
2121

22+
import re
2223
import numpy as np
2324

2425
from .. import xmlutils as xml
25-
from ..externals import inflection
2626
from ..externals.six import string_types
2727
from ..externals.six.moves import reduce
2828
from ..filebasedimages import FileBasedHeader, FileBasedImage
@@ -93,6 +93,12 @@ def _value_or_make_klass(val, klass):
9393
return _value_if_klass(val, klass)
9494

9595

96+
def _underscore(string):
97+
""" Convert a string from camelcase to underscored """
98+
string = re.sub(r'([A-Z]+)([A-Z][a-z])', r'\1_\2', string)
99+
return re.sub(r'([a-z0-9])([A-Z])', r'\1_\2', string).lower()
100+
101+
96102
class Cifti2MetaData(xml.XmlSerializable):
97103
""" A list of key-value pairs stored in the list self.data """
98104

@@ -466,7 +472,7 @@ def _to_xml_element(self):
466472

467473
for key in ['IndexOffset', 'IndexCount', 'ModelType', 'BrainStructure',
468474
'SurfaceNumberOfVertices']:
469-
attr = inflection.underscore(key)
475+
attr = _underscore(key)
470476
value = getattr(self, attr)
471477
if value is not None:
472478
brain_model.attrib[key] = str(value)
@@ -598,7 +604,7 @@ def _to_xml_element(self):
598604
mat_ind_map.attrib['AppliesToMatrixDimension'] = ','.join(dims_as_strings)
599605
for key in ['IndicesMapToDataType', 'NumberOfSeriesPoints', 'SeriesExponent',
600606
'SeriesStart', 'SeriesStep', 'SeriesUnit']:
601-
attr = inflection.underscore(key)
607+
attr = _underscore(key)
602608
value = getattr(self, attr)
603609
if value is not None:
604610
mat_ind_map.attrib[key] = str(value)

nibabel/cifti2/parse_cifti2_fast.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@
1818
Cifti2MatrixIndicesMap, Cifti2NamedMap, Cifti2Parcel,
1919
Cifti2Surface, Cifti2TransformationMatrixVoxelIndicesIJKtoXYZ,
2020
Cifti2Vertices, Cifti2Volume, CIFTI_BrainStructures,
21-
CIFTI_MODEL_TYPES)
21+
CIFTI_MODEL_TYPES, _underscore)
2222
from .. import xmlutils as xml
23-
from ..externals import inflection
2423
from ..externals.six import BytesIO
2524
from ..externals.six.moves import reduce
2625
from ..nifti1 import Nifti1Extension, extension_codes, intent_codes
@@ -182,8 +181,7 @@ def StartElementHandler(self, name, attrs):
182181
("SeriesStep", float),
183182
("SeriesUnit", str)]:
184183
if key in attrs:
185-
attr = inflection.underscore(key)
186-
setattr(mim, attr, dtype(attrs[key]))
184+
setattr(mim, _underscore(key), dtype(attrs[key]))
187185
matrix = self.struct_state[-1]
188186
assert isinstance(matrix, Cifti2Matrix)
189187
matrix.add_cifti_matrix_indices_map(mim)
@@ -296,8 +294,7 @@ def StartElementHandler(self, name, attrs):
296294
("BrainStructure", str),
297295
("SurfaceNumberOfVertices", int)]:
298296
if key in attrs:
299-
attr = inflection.underscore(key)
300-
setattr(model, attr, dtype(attrs[key]))
297+
setattr(model, _underscore(key), dtype(attrs[key]))
301298
assert model.brain_structure in CIFTI_BrainStructures
302299
assert model.model_type in CIFTI_MODEL_TYPES
303300
mim.add_cifti_brain_model(model)

nibabel/cifti2/tests/test_cifti2.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,20 @@ def test_cifti2_metadata():
4040
md.remove_metadata(['b', 'bval'])
4141
assert_equal(len(md.data), 0)
4242
assert_equal(md.to_xml().decode('utf-8'), '<MetaData />')
43+
44+
45+
def test_underscoring():
46+
# Pairs taken from inflection tests
47+
# https://github.com/jpvanhal/inflection/blob/663982e/test_inflection.py#L113-L125
48+
pairs = (("Product", "product"),
49+
("SpecialGuest", "special_guest"),
50+
("ApplicationController", "application_controller"),
51+
("Area51Controller", "area51_controller"),
52+
("HTMLTidy", "html_tidy"),
53+
("HTMLTidyGenerator", "html_tidy_generator"),
54+
("FreeBSD", "free_bsd"),
55+
("HTML", "html"),
56+
)
57+
58+
for camel, underscored in pairs:
59+
assert_equal(ci.cifti2._underscore(camel), underscored)

0 commit comments

Comments
 (0)