From 9e41a4a3a9c0e5ae1bda118bfac2ce8769066ed4 Mon Sep 17 00:00:00 2001 From: Florian Wagner Date: Tue, 2 Apr 2019 10:19:56 +0200 Subject: [PATCH] Make doctests numpy > 1.15 compatible (related to #133) --- python/pygimli/__init__.py | 3 --- python/pygimli/meshtools/mapping.py | 16 ++++++++-------- python/pygimli/mplviewer/colorbar.py | 4 ++-- python/pygimli/testing/__init__.py | 9 ++++----- python/pygimli/utils/utils.py | 22 +++++++++++----------- 5 files changed, 25 insertions(+), 29 deletions(-) diff --git a/python/pygimli/__init__.py b/python/pygimli/__init__.py index f583767e7..b57c2be24 100644 --- a/python/pygimli/__init__.py +++ b/python/pygimli/__init__.py @@ -3,9 +3,6 @@ pyGIMLi - An open-source library for modelling and inversion in geophysics """ -# py 2.7 compatiblity -from __future__ import division, print_function - import locale import sys diff --git a/python/pygimli/meshtools/mapping.py b/python/pygimli/meshtools/mapping.py index 2f826613f..89325eb84 100644 --- a/python/pygimli/meshtools/mapping.py +++ b/python/pygimli/meshtools/mapping.py @@ -59,7 +59,7 @@ def cellDataToNodeData(mesh, data, style='mean'): >>> celldata = np.array([1, 2, 3, 4]) >>> nodedata = pg.meshtools.cellDataToNodeData(grid, celldata) >>> print(nodedata.array()) - [ 1. 1.5 2. 2. 2.5 3. 3. 3.5 4. ] + [1. 1.5 2. 2. 2.5 3. 3. 3.5 4. ] """ if len(data) != mesh.cellCount(): raise BaseException("Dimension mismatch, expecting cellCount(): " + @@ -370,10 +370,10 @@ def interpolate(*args, **kwargs): args: :gimliapi:`GIMLI::Mesh`, :gimliapi:`GIMLI::Mesh`, iterable `outData = interpolate(outMesh, inMesh, vals)` Interpolate values based on inMesh to outMesh. - Values can be of length inMesh.cellCount() interpolated to - outMesh.cellCenters() or inMesh.nodeCount() which are interpolated tp - outMesh.positions(). - + Values can be of length inMesh.cellCount() interpolated to + outMesh.cellCenters() or inMesh.nodeCount() which are interpolated tp + outMesh.positions(). + Returns: Interpolated values. @@ -385,7 +385,7 @@ def interpolate(*args, **kwargs): Arguments forwarded to :gimliapi:`GIMLI::interpolate` kwargs: Arguments forwarded to :gimliapi:`GIMLI::interpolate` - + `interpolate(srcMesh, destMesh)` All data from inMesh are interpolated to outMesh @@ -481,7 +481,7 @@ def interpolate(*args, **kwargs): outMat=outMat, **kwargs) return np.array(outMat) - + if len(args) == 4: # args: (inMesh, inData, outPos, outData) if args[1].ndim == 1 and args[2].ndim == 1 and args[3].ndim == 1: @@ -515,7 +515,7 @@ def interpolate(*args, **kwargs): y=args[3], z=args[4], **kwargs) - + return pg.core._pygimli_.interpolate(*args, **kwargs) # end if pg.core: diff --git a/python/pygimli/mplviewer/colorbar.py b/python/pygimli/mplviewer/colorbar.py index e68c126f7..08ba398db 100644 --- a/python/pygimli/mplviewer/colorbar.py +++ b/python/pygimli/mplviewer/colorbar.py @@ -22,9 +22,9 @@ def autolevel(z, nLevs, logScale=None, zmin=None, zmax=None): >>> from pygimli.mplviewer import autolevel >>> x = np.linspace(1, 10, 100) >>> autolevel(x, 3) - array([ 1., 4., 7., 10.]) + array([ 1., 4., 7., 10.]) >>> autolevel(x, 3, logScale=True) - array([ 0.1, 1. , 10. , 100. ]) + array([ 0.1, 1. , 10. , 100. ]) """ locator = None if logScale and min(z) > 0: diff --git a/python/pygimli/testing/__init__.py b/python/pygimli/testing/__init__.py index 2e188e384..4b4e67871 100644 --- a/python/pygimli/testing/__init__.py +++ b/python/pygimli/testing/__init__.py @@ -60,10 +60,9 @@ def test(target=None, show=False, onlydoctests=False, coverage=False, pg.boxprint("Testing pygimli %s" % pg.__version__, sym="+") # Numpy compatibility (array string representation has changed) - if np.__version__[:4] in ("1.14", "1.15"): - np.set_printoptions(legacy="1.13") - - + if np.__version__[:4] == "1.14": + pg.warn("Some doctests will fail due to old numpy version.", + "Consider upgrading to numpy >= 1.15") if target: if isinstance(target, str): @@ -75,7 +74,7 @@ def test(target=None, show=False, onlydoctests=False, coverage=False, mod_name, func_name = target.rsplit('.', 1) mod = importlib.import_module(mod_name) target = getattr(mod, func_name) - + if show: # Keep figure openend if single function is tested plt.ioff() diff --git a/python/pygimli/utils/utils.py b/python/pygimli/utils/utils.py index 1fd8c04ff..94cb266ed 100644 --- a/python/pygimli/utils/utils.py +++ b/python/pygimli/utils/utils.py @@ -176,18 +176,18 @@ def niceLogspace(vMin, vMax, nDec=10): >>> from pygimli.utils import niceLogspace >>> v1 = niceLogspace(vMin=0.1, vMax=0.1, nDec=1) >>> print(v1) - [ 0.1 1. ] + [0.1 1. ] >>> v1 = niceLogspace(vMin=0.09, vMax=0.11, nDec=1) >>> print(v1) - [ 0.01 0.1 1. ] + [0.01 0.1 1. ] >>> v1 = niceLogspace(vMin=0.09, vMax=0.11, nDec=10) >>> print(len(v1)) 21 >>> print(v1) - [ 0.01 0.01258925 0.01584893 0.01995262 0.02511886 0.03162278 - 0.03981072 0.05011872 0.06309573 0.07943282 0.1 0.12589254 - 0.15848932 0.19952623 0.25118864 0.31622777 0.39810717 0.50118723 - 0.63095734 0.79432823 1. ] + [0.01 0.01258925 0.01584893 0.01995262 0.02511886 0.03162278 + 0.03981072 0.05011872 0.06309573 0.07943282 0.1 0.12589254 + 0.15848932 0.19952623 0.25118864 0.31622777 0.39810717 0.50118723 + 0.63095734 0.79432823 1. ] """ if vMin > vMax or vMin < 1e-12: print("vMin:", vMin, "vMax", vMax) @@ -355,11 +355,11 @@ def dist(p, c=None): >>> p[0] = [0.0, 0.0] >>> p[1] = [0.0, 1.0] >>> print(dist(p)) - [ 0. 1. 0. 0.] + [0. 1. 0. 0.] >>> x = pg.RVector(4, 0) >>> y = pg.RVector(4, 1) >>> print(dist(np.array([x, y]).T)) - [ 1. 1. 1. 1.] + [1. 1. 1. 1.] """ if c is None: c = pg.RVector3(0.0, 0.0, 0.0) @@ -403,7 +403,7 @@ def cumDist(p): >>> p[2] = [0.0, 1.0] >>> p[3] = [0.0, 0.0] >>> print(cumDist(p)) - [ 0. 1. 1. 2.] + [0. 1. 1. 2.] """ d = np.zeros(len(p)) d[1:] = np.cumsum(dist(diff(p))) @@ -631,7 +631,7 @@ def uniqueAndSum(indices, to_sum, return_index=False, verbose=False): >>> # you need for example 3 array to find unique node positions in a mesh >>> values = np.arange(0.1, 0.7, 0.1) >>> print(values) - [ 0.1 0.2 0.3 0.4 0.5 0.6] + [0.1 0.2 0.3 0.4 0.5 0.6] >>> # some values to be summed together (for example attributes of nodes) >>> unique_idx, summed_vals = uniqueAndSum(to_sort, values) >>> print(unique_idx) @@ -640,7 +640,7 @@ def uniqueAndSum(indices, to_sum, return_index=False, verbose=False): [1 2] [2 3]] >>> print(summed_vals) - [ 0.3 0.3 0.4 1.1] + [0.3 0.3 0.4 1.1] """ flag_mult = len(indices) != indices.size if verbose: