From d792810b79d045ea7cd0e01fe9650b9d121606a6 Mon Sep 17 00:00:00 2001 From: TeetotalingTom <114575548+TeetotalingTom@users.noreply.github.com> Date: Sun, 16 Jun 2024 23:21:11 +0200 Subject: [PATCH] Expand compatibility to older Python/NumPy versions (#16) * Updated compatibilities, helper, and tests * Updated compatibilities, helper, and tests --- pyproject.toml | 4 +- tests/helper.py | 26 ++++++++++-- tests/test_ibw.py | 102 +++++++++++++++++++++++----------------------- 3 files changed, 75 insertions(+), 57 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index cd07d4b..bd98107 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -13,7 +13,7 @@ authors = [ ] description = "interface for reading binary IGOR files" readme = "Readme.rst" -requires-python = ">=3.8, <4" +requires-python = ">=3.6, <4" classifiers = [ 'Intended Audience :: Developers', 'Intended Audience :: Science/Research', @@ -23,7 +23,7 @@ classifiers = [ ] license = {text = "GNU Lesser General Public License v3 or later (LGPLv3+)"} dependencies = [ - "numpy>=1.25.1", + "numpy>=1.19.2", ] dynamic = ["version"] diff --git a/tests/helper.py b/tests/helper.py index 18979a0..1988194 100644 --- a/tests/helper.py +++ b/tests/helper.py @@ -1,10 +1,27 @@ import pathlib -from pprint import pformat - +import numpy as _numpy +from pprint import pformat, PrettyPrinter from igor2.binarywave import load as loadibw data_dir = pathlib.Path(__file__).parent / "data" +class NumpyPrettyPrinter(PrettyPrinter): + def format(self, object, context, maxlevels, level): + if isinstance(object, _numpy.ndarray): + # Generate the entire array string using numpy's own mechanism but ensuring dtype is displayed + array_repr = _numpy.array2string(object, separator=', ') + # Append dtype only once for the whole array + formatted_array = f"array({array_repr}, dtype='{object.dtype}')" + return (formatted_array, True, False) + # Default handling for other types + return super().format(object, context, maxlevels, level) + + +def custom_pformat(object, indent=1, width=80, depth=None, *, compact=False): + printer = NumpyPrettyPrinter(indent=indent, width=width, depth=depth, compact=compact) + return printer.pformat(object) + + def assert_equal_dump_no_whitespace_no_byte(data_a, data_b): def repl(x): @@ -12,11 +29,12 @@ def repl(x): [" ", ""], # ignore whitespaces ["b'", "'"], # ignore bytes vs str ["\n", ""], # ignore newlines - # teat all floats as equal + # treat all floats as equal ["float32", "float"], ["float64", "float"], ["'>f4'", "float"], ["'>f8'", "float"], + ["'float'", "float"], ]: x = x.replace(old, new) return x @@ -35,7 +53,7 @@ def dumpibw(filename): def format_data(data): - lines = pformat(data).splitlines() + lines = custom_pformat(data).splitlines() return '\n'.join([line.rstrip() for line in lines]) diff --git a/tests/test_ibw.py b/tests/test_ibw.py index 9fb0410..6a1755f 100644 --- a/tests/test_ibw.py +++ b/tests/test_ibw.py @@ -51,8 +51,8 @@ def test_ibw_02(): {'version': 5, 'wave': {'bin_header': {'checksum': 5554, 'dataEUnitsSize': 0, - 'dimEUnitsSize': array([0, 0, 0, 0]), - 'dimLabelsSize': array([0, 0, 0, 0]), + 'dimEUnitsSize': array([0, 0, 0, 0], dtype='int64'), + 'dimLabelsSize': array([0, 0, 0, 0], dtype='int64'), 'formulaSize': 0, 'noteSize': 0, 'optionsSize1': 0, @@ -64,7 +64,7 @@ def test_ibw_02(): 'formula': '', 'labels': [[], [], [], []], 'note': '', - 'sIndices': array([ 4, 7, 8, 14, 18]), + 'sIndices': array([ 4, 7, 8, 14, 18], dtype='int64'), 'wData': array(['Mary', 'had', 'a', 'little', 'lamb'], dtype='|S6'), 'wave_header': {'aModified': 0, @@ -77,8 +77,8 @@ def test_ibw_02(): 'dataUnits': array(['', '', '', ''], dtype='|S1'), 'depID': 22, - 'dimEUnits': array([0, 0, 0, 0]), - 'dimLabels': array([0, 0, 0, 0]), + 'dimEUnits': array([0, 0, 0, 0], dtype='int64'), + 'dimLabels': array([0, 0, 0, 0], dtype='int64'), 'dimUnits': array([['', '', '', ''], ['', '', '', ''], ['', '', '', ''], @@ -89,12 +89,12 @@ def test_ibw_02(): 'fsValid': 0, 'kindBits': '\\x00', 'modDate': 3001571215, - 'nDim': array([5, 0, 0, 0]), + 'nDim': array([5, 0, 0, 0], dtype='int64'), 'next': 0, 'npnts': 5, 'sIndices': 69557296, - 'sfA': array([ 1., 1., 1., 1.]), - 'sfB': array([ 0., 0., 0., 0.]), + 'sfA': array([ 1., 1., 1., 1.], dtype='float64'), + 'sfB': array([ 0., 0., 0., 0.], dtype='float64'), 'srcFldr': 0, 'swModified': 1, 'topFullScale': 0.0, @@ -103,7 +103,7 @@ def test_ibw_02(): 'wModified': 0, 'waveNoteH': 0, 'whUnused': array([0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0]), + 0, 0, 0, 0, 0, 0, 0], dtype='int64'), 'whVersion': 1, 'whpad1': array(['', '', '', '', '', ''], dtype='|S1'), @@ -209,8 +209,8 @@ def test_ibw_04(): {'version': 5, 'wave': {'bin_header': {'checksum': -12033, 'dataEUnitsSize': 0, - 'dimEUnitsSize': array([0, 0, 0, 0]), - 'dimLabelsSize': array([64, 0, 0, 0]), + 'dimEUnitsSize': array([0, 0, 0, 0], dtype='int64'), + 'dimLabelsSize': array([64, 0, 0, 0], dtype='int64'), 'formulaSize': 0, 'noteSize': 15, 'optionsSize1': 0, @@ -234,8 +234,8 @@ def test_ibw_04(): 'dataUnits': array(['', '', '', ''], dtype='|S1'), 'depID': 27, - 'dimEUnits': array([0, 0, 0, 0]), - 'dimLabels': array([69554136, 0, 0, 0]), + 'dimEUnits': array([0, 0, 0, 0], dtype='int64'), + 'dimLabels': array([69554136, 0, 0, 0], dtype='int64'), 'dimUnits': array([['', '', '', ''], ['', '', '', ''], ['', '', '', ''], @@ -246,12 +246,12 @@ def test_ibw_04(): 'fsValid': 0, 'kindBits': '\\x00', 'modDate': 3001573601, - 'nDim': array([5, 0, 0, 0]), + 'nDim': array([5, 0, 0, 0], dtype='int64'), 'next': 69555212, 'npnts': 5, 'sIndices': 0, - 'sfA': array([ 1., 1., 1., 1.]), - 'sfB': array([ 0., 0., 0., 0.]), + 'sfA': array([ 1., 1., 1., 1.], dtype='float64'), + 'sfB': array([ 0., 0., 0., 0.], dtype='float64'), 'srcFldr': -32349, 'swModified': 1, 'topFullScale': 0.0, @@ -260,7 +260,7 @@ def test_ibw_04(): 'wModified': 0, 'waveNoteH': 69554032, 'whUnused': array([0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0]), + 0, 0, 0, 0, 0, 0, 0, 0], dtype='int64'), 'whVersion': 1, 'whpad1': array(['', '', '', '', '', ''], dtype='|S1'), @@ -277,8 +277,8 @@ def test_ibw_05(): {'version': 5, 'wave': {'bin_header': {'checksum': -15649, 'dataEUnitsSize': 0, - 'dimEUnitsSize': array([0, 0, 0, 0]), - 'dimLabelsSize': array([0, 0, 0, 0]), + 'dimEUnitsSize': array([0, 0, 0, 0], dtype='int64'), + 'dimLabelsSize': array([0, 0, 0, 0], dtype='int64'), 'formulaSize': 0, 'noteSize': 0, 'optionsSize1': 0, @@ -302,8 +302,8 @@ def test_ibw_05(): 'dataUnits': array(['', '', '', ''], dtype='|S1'), 'depID': 29, - 'dimEUnits': array([0, 0, 0, 0]), - 'dimLabels': array([0, 0, 0, 0]), + 'dimEUnits': array([0, 0, 0, 0], dtype='int64'), + 'dimLabels': array([0, 0, 0, 0], dtype='int64'), 'dimUnits': array([['', '', '', ''], ['', '', '', ''], ['', '', '', ''], @@ -314,12 +314,12 @@ def test_ibw_05(): 'fsValid': 0, 'kindBits': '\\x00', 'modDate': 3001573964, - 'nDim': array([0, 0, 0, 0]), + 'nDim': array([0, 0, 0, 0], dtype='int64'), 'next': 0, 'npnts': 0, 'sIndices': 0, - 'sfA': array([ 1., 1., 1., 1.]), - 'sfB': array([ 0., 0., 0., 0.]), + 'sfA': array([ 1., 1., 1., 1.], dtype='float64'), + 'sfB': array([ 0., 0., 0., 0.], dtype='float64'), 'srcFldr': 0, 'swModified': 1, 'topFullScale': 0.0, @@ -328,7 +328,7 @@ def test_ibw_05(): 'wModified': 1, 'waveNoteH': 0, 'whUnused': array([0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0]), + 0, 0, 0, 0, 0, 0, 0, 0], dtype='int64'), 'whVersion': 1, 'whpad1': array(['', '', '', '', '', ''], dtype='|S1'), @@ -349,7 +349,7 @@ def test_ibw_06(): 'wfmSize': 166}, 'note': '', 'padding': array([], dtype=float64), - 'wData': array([ 5., 4., 3., 2., 1.]), + 'wData': array([ 5., 4., 3., 2., 1.], dtype='float64'), 'wave_header': {'aModified': 0, 'bname': 'double', 'botFullScale': 0.0, @@ -388,8 +388,8 @@ def test_ibw_07(): {'version': 5, 'wave': {'bin_header': {'checksum': 184, 'dataEUnitsSize': 0, - 'dimEUnitsSize': array([0, 0, 0, 0]), - 'dimLabelsSize': array([0, 0, 0, 0]), + 'dimEUnitsSize': array([0, 0, 0, 0], dtype='int64'), + 'dimLabelsSize': array([0, 0, 0, 0], dtype='int64'), 'formulaSize': 0, 'noteSize': 0, 'optionsSize1': 0, @@ -401,7 +401,7 @@ def test_ibw_07(): 'formula': '', 'labels': [[], [], [], []], 'note': '', - 'sIndices': array([ 4, 7, 8, 14, 18]), + 'sIndices': array([ 4, 7, 8, 14, 18], dtype='int64'), 'wData': array(['Mary', 'had', 'a', 'little', 'lamb'], dtype='|S6'), 'wave_header': {'aModified': 0, @@ -414,8 +414,8 @@ def test_ibw_07(): 'dataUnits': array(['', '', '', ''], dtype='|S1'), 'depID': 32, - 'dimEUnits': array([0, 0, 0, 0]), - 'dimLabels': array([0, 0, 0, 0]), + 'dimEUnits': array([0, 0, 0, 0], dtype='int64'), + 'dimLabels': array([0, 0, 0, 0], dtype='int64'), 'dimUnits': array([['', '', '', ''], ['', '', '', ''], ['', '', '', ''], @@ -426,12 +426,12 @@ def test_ibw_07(): 'fsValid': 0, 'kindBits': '\\x00', 'modDate': 3001571215, - 'nDim': array([5, 0, 0, 0]), + 'nDim': array([5, 0, 0, 0], dtype='int64'), 'next': 0, 'npnts': 5, 'sIndices': 8133100, - 'sfA': array([ 1., 1., 1., 1.]), - 'sfB': array([ 0., 0., 0., 0.]), + 'sfA': array([ 1., 1., 1., 1.], dtype='float64'), + 'sfB': array([ 0., 0., 0., 0.], dtype='float64'), 'srcFldr': -1007, 'swModified': 0, 'topFullScale': 0.0, @@ -440,7 +440,7 @@ def test_ibw_07(): 'wModified': 1, 'waveNoteH': 0, 'whUnused': array([0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0]), + 0, 0, 0, 0, 0, 0, 0, 0, 0], dtype='int64'), 'whVersion': 1, 'whpad1': array(['', '', '', '', '', ''], dtype='|S1'), @@ -500,8 +500,8 @@ def test_ibw_09(): {'version': 5, 'wave': {'bin_header': {'checksum': 13214, 'dataEUnitsSize': 0, - 'dimEUnitsSize': array([0, 0, 0, 0]), - 'dimLabelsSize': array([64, 0, 0, 0]), + 'dimEUnitsSize': array([0, 0, 0, 0], dtype='int64'), + 'dimLabelsSize': array([64, 0, 0, 0], dtype='int64'), 'formulaSize': 0, 'noteSize': 15, 'optionsSize1': 0, @@ -525,8 +525,8 @@ def test_ibw_09(): 'dataUnits': array(['', '', '', ''], dtype='|S1'), 'depID': 30, - 'dimEUnits': array([0, 0, 0, 0]), - 'dimLabels': array([8138784, 0, 0, 0]), + 'dimEUnits': array([0, 0, 0, 0], dtype='int64'), + 'dimLabels': array([8138784, 0, 0, 0], dtype='int64'), 'dimUnits': array([['', '', '', ''], ['', '', '', ''], ['', '', '', ''], @@ -537,12 +537,12 @@ def test_ibw_09(): 'fsValid': 0, 'kindBits': '\\x00', 'modDate': 3001573601, - 'nDim': array([5, 0, 0, 0]), + 'nDim': array([5, 0, 0, 0], dtype='int64'), 'next': 8125236, 'npnts': 5, 'sIndices': 0, - 'sfA': array([ 1., 1., 1., 1.]), - 'sfB': array([ 0., 0., 0., 0.]), + 'sfA': array([ 1., 1., 1., 1.], dtype='float64'), + 'sfB': array([ 0., 0., 0., 0.], dtype='float64'), 'srcFldr': -1007, 'swModified': 0, 'topFullScale': 0.0, @@ -551,7 +551,7 @@ def test_ibw_09(): 'wModified': 1, 'waveNoteH': 8131596, 'whUnused': array([0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0]), + 0, 0, 0, 0, 0, 0, 0], dtype='int64'), 'whVersion': 1, 'whpad1': array(['', '', '', '', '', ''], dtype='|S1'), @@ -569,8 +569,8 @@ def test_ibw_10(): {'version': 5, 'wave': {'bin_header': {'checksum': 27541, 'dataEUnitsSize': 0, - 'dimEUnitsSize': array([0, 0, 0, 0]), - 'dimLabelsSize': array([0, 0, 0, 0]), + 'dimEUnitsSize': array([0, 0, 0, 0], dtype='int64'), + 'dimLabelsSize': array([0, 0, 0, 0], dtype='int64'), 'formulaSize': 0, 'noteSize': 0, 'optionsSize1': 0, @@ -594,8 +594,8 @@ def test_ibw_10(): 'dataUnits': array(['', '', '', ''], dtype='|S1'), 'depID': 31, - 'dimEUnits': array([0, 0, 0, 0]), - 'dimLabels': array([0, 0, 0, 0]), + 'dimEUnits': array([0, 0, 0, 0], dtype='int64'), + 'dimLabels': array([0, 0, 0, 0], dtype='int64'), 'dimUnits': array([['', '', '', ''], ['', '', '', ''], ['', '', '', ''], @@ -606,12 +606,12 @@ def test_ibw_10(): 'fsValid': 0, 'kindBits': '\\x00', 'modDate': 3001573964, - 'nDim': array([0, 0, 0, 0]), + 'nDim': array([0, 0, 0, 0], dtype='int64'), 'next': 8133140, 'npnts': 0, 'sIndices': 0, - 'sfA': array([ 1., 1., 1., 1.]), - 'sfB': array([ 0., 0., 0., 0.]), + 'sfA': array([ 1., 1., 1., 1.], dtype='float64'), + 'sfB': array([ 0., 0., 0., 0.], dtype='float64'), 'srcFldr': -1007, 'swModified': 0, 'topFullScale': 0.0, @@ -620,7 +620,7 @@ def test_ibw_10(): 'wModified': 1, 'waveNoteH': 0, 'whUnused': array([0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0]), + 0, 0, 0, 0, 0, 0, 0, 0], dtype='int64'), 'whVersion': 1, 'whpad1': array(['', '', '', '', '', ''], dtype='|S1'),