Skip to content

Commit

Permalink
Expand compatibility to older Python/NumPy versions (#16)
Browse files Browse the repository at this point in the history
* Updated compatibilities, helper, and tests

* Updated compatibilities, helper, and tests
  • Loading branch information
TeetotalingTom authored Jun 16, 2024
1 parent 33b1a6e commit d792810
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 57 deletions.
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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"]

Expand Down
26 changes: 22 additions & 4 deletions tests/helper.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,40 @@
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):
for old, new in [
[" ", ""], # 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
Expand All @@ -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])


Expand Down
102 changes: 51 additions & 51 deletions tests/test_ibw.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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([['', '', '', ''],
['', '', '', ''],
['', '', '', ''],
Expand All @@ -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,
Expand All @@ -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'),
Expand Down Expand Up @@ -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,
Expand All @@ -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([['', '', '', ''],
['', '', '', ''],
['', '', '', ''],
Expand All @@ -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,
Expand All @@ -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'),
Expand All @@ -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,
Expand All @@ -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([['', '', '', ''],
['', '', '', ''],
['', '', '', ''],
Expand All @@ -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,
Expand All @@ -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'),
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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([['', '', '', ''],
['', '', '', ''],
['', '', '', ''],
Expand All @@ -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,
Expand All @@ -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'),
Expand Down Expand Up @@ -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,
Expand All @@ -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([['', '', '', ''],
['', '', '', ''],
['', '', '', ''],
Expand All @@ -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,
Expand All @@ -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'),
Expand All @@ -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,
Expand All @@ -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([['', '', '', ''],
['', '', '', ''],
['', '', '', ''],
Expand All @@ -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,
Expand All @@ -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'),
Expand Down

0 comments on commit d792810

Please sign in to comment.