From 0e13dc70f1fbabd85e3cf0364fe892cdd95039e6 Mon Sep 17 00:00:00 2001 From: Fabien Maussion Date: Mon, 7 Dec 2015 20:24:35 +0100 Subject: [PATCH] No more deprecation warnings --- docs/INSTALL.rst | 1 + docs/install_virtualenv.rst | 2 +- oggm/prepro/centerlines.py | 17 +++++++++-------- oggm/prepro/climate.py | 8 ++++++-- oggm/prepro/geometry.py | 13 +++++++------ oggm/prepro/gis.py | 17 +++++++++-------- oggm/tests/test_graphics.py | 7 ++----- oggm/tests/test_prepro.py | 18 ++++++++---------- oggm/tests/test_utils.py | 3 ++- oggm/tests/test_workflow.py | 3 ++- oggm/utils.py | 3 +++ 11 files changed, 50 insertions(+), 42 deletions(-) diff --git a/docs/INSTALL.rst b/docs/INSTALL.rst index 09af1e5d9..2004676b2 100644 --- a/docs/INSTALL.rst +++ b/docs/INSTALL.rst @@ -38,6 +38,7 @@ I/O: GIS and geometrical tools: - gdal + - rasterio - shapely - pyproj - geopandas diff --git a/docs/install_virtualenv.rst b/docs/install_virtualenv.rst index bde7d0747..583ab2abc 100644 --- a/docs/install_virtualenv.rst +++ b/docs/install_virtualenv.rst @@ -111,7 +111,7 @@ Details: http://tylerickson.blogspot.co.at/2011/09/installing-gdal-in-python-vir Install further stuffs:: - $ pip install pyproj Pillow geopandas netcdf4 scikit-image configobj joblib + $ pip install pyproj rasterio Pillow geopandas netcdf4 scikit-image configobj joblib And the external libraries:: diff --git a/oggm/prepro/centerlines.py b/oggm/prepro/centerlines.py index 8e54cd0f7..2438f6698 100644 --- a/oggm/prepro/centerlines.py +++ b/oggm/prepro/centerlines.py @@ -33,6 +33,7 @@ # Locals import oggm.conf as cfg from salem import lazy_property +from oggm.utils import tuple2int # Module logger log = logging.getLogger(__name__) @@ -452,9 +453,9 @@ def compute_centerlines(gdir, div_id=None): nc.close() # Find for local maximas on the outline + x, y = tuple2int(poly_pix.exterior.xy) ext_yx = tuple(reversed(poly_pix.exterior.xy)) - ext_yx = (ext_yx[0][:-1], ext_yx[1][:-1]) # last point is first point - zoutline = topo[ext_yx] + zoutline = topo[y[:-1], x[:-1]] # last point is first point # Size of the half window to use to look for local maximas maxorder = np.rint(cfg.params['localmax_window'] / gdir.grid.dx) @@ -495,12 +496,12 @@ def compute_centerlines(gdir, div_id=None): costgrid = _make_costgrid(glacier_mask, glacier_ext, topo) # Terminus - t_coord = np.asarray(ext_yx)[:, np.argmin(zoutline)] + t_coord = np.asarray(ext_yx)[:, np.argmin(zoutline)].astype(np.int64) # Compute the routes lines = [] for h in heads: - h_coord = np.asarray(h.xy)[::-1] + h_coord = np.asarray(h.xy)[::-1].astype(np.int64) indices, _ = route_through_array(costgrid, h_coord, t_coord) lines.append(shpg.LineString(np.array(indices)[:, [1, 0]])) log.debug('%s: computed the routes', gdir.rgi_id) @@ -562,8 +563,8 @@ def compute_downstream_lines(gdir): topo = nc.variables['topo_smoothed'][:] # Variables we gonna need - xmesh, ymesh = np.meshgrid(np.arange(0, gdir.grid.nx, 1), - np.arange(0, gdir.grid.ny, 1)) + xmesh, ymesh = np.meshgrid(np.arange(0, gdir.grid.nx, 1, dtype=np.int64), + np.arange(0, gdir.grid.ny, 1, dtype=np.int64)) _h = [topo[:, 0], topo[0, :], topo[:, -1], topo[-1, :]] _x = [xmesh[:, 0], xmesh[0, :], xmesh[:, -1], xmesh[-1, :]] _y = [ymesh[:, 0], ymesh[0, :], ymesh[:, -1], ymesh[-1, :]] @@ -574,8 +575,8 @@ def compute_downstream_lines(gdir): div_ids = list(gdir.divide_ids) for div_id in div_ids: head = gdir.read_pickle('centerlines', div_id=div_id)[-1].tail - heigts.append(topo[head.y, head.x]) - heads.append((head.y, head.x)) + heigts.append(topo[int(head.y), int(head.x)]) + heads.append((int(head.y), int(head.x))) # Now the lowest first aso = np.argsort(heigts) diff --git a/oggm/prepro/climate.py b/oggm/prepro/climate.py index ab2883e00..8b10f96f8 100644 --- a/oggm/prepro/climate.py +++ b/oggm/prepro/climate.py @@ -113,10 +113,14 @@ def mb_climate_on_height(gdir, heights, time_range=None, year_range=None): time = netCDF4.num2date(time[:], time.units) if time_range is not None: p0 = np.where(time == time_range[0])[0] - if len(p0) == 0: + try: + p0 = p0[0] + except IndexError: raise RuntimeError('time_range[0] not found in file') p1 = np.where(time == time_range[1])[0] - if len(p1) == 0: + try: + p1 = p1[0] + except IndexError: raise RuntimeError('time_range[1] not found in file') else: p0 = 0 diff --git a/oggm/prepro/geometry.py b/oggm/prepro/geometry.py index 498f6e89d..9b45e18a3 100644 --- a/oggm/prepro/geometry.py +++ b/oggm/prepro/geometry.py @@ -35,6 +35,7 @@ import oggm.conf as cfg from oggm import utils import oggm.prepro.centerlines +from oggm.utils import tuple2int # Module logger log = logging.getLogger(__name__) @@ -467,7 +468,7 @@ def catchment_area(gdir, div_id=None): cost_factor = 0. # Make it cheap dic_catch = dict() for i, cl in enumerate(cls): - x, y = cl.line.xy + x, y = tuple2int(cl.line.xy) costgrid[y, x] *= cost_factor for x, y in [(int(x), int(y)) for x, y in cl.line.coords]: assert (y, x) not in dic_catch @@ -477,11 +478,11 @@ def catchment_area(gdir, div_id=None): computed = np.where(mask == 1, 0, np.nan) # Coords of Terminus - endcoords = np.array(cls[0].tail.coords[0])[::-1] + endcoords = np.array(cls[0].tail.coords[0])[::-1].astype(np.int64) # Start with all the paths at the boundaries, they are more likely # to cover much of the glacier - for headx, heady in glacier_pix.exterior.coords: + for headx, heady in tuple2int(glacier_pix.exterior.coords): indices, _ = route_through_array(costgrid, np.array([heady, headx]), endcoords) inds = np.array(indices).T @@ -498,7 +499,7 @@ def catchment_area(gdir, div_id=None): not_computed = np.where(computed == 0) if len(not_computed[0]) == 0: # All points computed !! break - headcoords = np.array([not_computed[0][0], not_computed[1][0]]) + headcoords = np.array([not_computed[0][0], not_computed[1][0]]).astype(np.int64) indices, _ = route_through_array(costgrid, headcoords, endcoords) inds = np.array(indices).T computed[inds[0], inds[1]] = 1 @@ -678,8 +679,8 @@ def catchment_width_geom(gdir, div_id=None): # Filter +- widths at junction points for fid in fl.inflow_indices: - i0 = np.clip(fid-jpix, jpix/2, n-jpix/2) - i1 = np.clip(fid+jpix+1, jpix/2, n-jpix/2) + i0 = np.clip(fid-jpix, jpix/2, n-jpix/2).astype(np.int64) + i1 = np.clip(fid+jpix+1, jpix/2, n-jpix/2).astype(np.int64) widths[i0:i1] = np.NaN valid = np.where(np.isfinite(widths)) diff --git a/oggm/prepro/gis.py b/oggm/prepro/gis.py index 29ac580c6..489d6c880 100644 --- a/oggm/prepro/gis.py +++ b/oggm/prepro/gis.py @@ -25,9 +25,9 @@ from shutil import copyfile from functools import partial # External libs -import osr +from osgeo import osr import salem -import gdal +from osgeo import gdal import pyproj import numpy as np import shapely.ops @@ -38,11 +38,12 @@ from scipy.ndimage.measurements import label # Locals import oggm.conf as cfg +from oggm.utils import tuple2int # Module logger log = logging.getLogger(__name__) -# Variable needed later +# Needed later label_struct = np.ones((3, 3)) @@ -205,12 +206,12 @@ def proj(x, y): (x, y) = glacier_poly_pix.exterior.xy glacier_mask[skdraw.polygon(np.array(y), np.array(x))] = 1 for gint in glacier_poly_pix.interiors: - x, y = gint.xy - glacier_mask[skdraw.polygon(np.array(y), np.array(x))] = 0 + x, y = tuple2int(gint.xy) + glacier_mask[skdraw.polygon(y, x)] = 0 glacier_mask[y, x] = 0 # on the nunataks, no - ext_yx = tuple(reversed(glacier_poly_pix.exterior.xy)) - glacier_mask[ext_yx] = 1 - glacier_ext[ext_yx] = 1 + x, y = tuple2int(glacier_poly_pix.exterior.xy) + glacier_mask[y, x] = 1 + glacier_ext[y, x] = 1 # Because of the 0 values at nunataks boundaries, some "Ice Islands" # can happen within nunataks (e.g.: RGI40-11.00062) diff --git a/oggm/tests/test_graphics.py b/oggm/tests/test_graphics.py index 8a9660368..61f9e6a1c 100644 --- a/oggm/tests/test_graphics.py +++ b/oggm/tests/test_graphics.py @@ -1,12 +1,9 @@ from __future__ import division -from nose.tools import assert_true +import warnings +warnings.filterwarnings("once", category=DeprecationWarning) import os -import sys -import copy -import shutil -import warnings from six.moves.urllib.request import urlopen from six.moves.urllib.error import URLError diff --git a/oggm/tests/test_prepro.py b/oggm/tests/test_prepro.py index 5e2131e7f..777559abf 100644 --- a/oggm/tests/test_prepro.py +++ b/oggm/tests/test_prepro.py @@ -1,27 +1,26 @@ from __future__ import absolute_import, division +import warnings +warnings.filterwarnings("once", category=DeprecationWarning) # , module=r'.*oggm.*' + import unittest import os -import sys -import pickle import shapely.geometry as shpg import numpy as np import shutil import pandas as pd import geopandas as gpd -import matplotlib.pyplot as plt import netCDF4 -import multiprocessing as mp # Local imports from oggm.prepro import gis, centerlines, geometry, climate, inversion import oggm.conf as cfg from oggm.utils import get_demo_file from oggm import utils -import logging from xml.dom import minidom import salem +from oggm.utils import tuple2int # Globals current_dir = os.path.dirname(os.path.abspath(__file__)) @@ -107,7 +106,6 @@ def test_glacier_masks(self): np.testing.assert_allclose(area,gdir.glacier_area, rtol=1e-1) nc.close() - class TestCenterlines(unittest.TestCase): def setUp(self): @@ -211,8 +209,8 @@ def test_baltoro_centerlines(self): my_mask = np.zeros((gdir.grid.ny, gdir.grid.nx), dtype=np.uint8) cls = gdir.read_pickle('centerlines', div_id=1) for cl in cls: - ext_yx = tuple(reversed(cl.line.xy)) - my_mask[ext_yx] = 1 + x, y = tuple2int(cl.line.xy) + my_mask[y, x] = 1 # Transform kien_mask = np.zeros((gdir.grid.ny, gdir.grid.nx), dtype=np.uint8) @@ -240,8 +238,8 @@ def proj(x, y): kgm = transform(project, kgm) - ext_yx = tuple(reversed(kgm.xy)) - kien_mask[ext_yx] = 1 + x, y = tuple2int(kgm.xy) + kien_mask[y, x] = 1 # We test the Heidke Skill score of our predictions rest = kien_mask + 2 * my_mask diff --git a/oggm/tests/test_utils.py b/oggm/tests/test_utils.py index f684f28af..20bd1656a 100644 --- a/oggm/tests/test_utils.py +++ b/oggm/tests/test_utils.py @@ -1,5 +1,6 @@ from __future__ import division - +import warnings +warnings.filterwarnings("once", category=DeprecationWarning) import unittest import os diff --git a/oggm/tests/test_workflow.py b/oggm/tests/test_workflow.py index 8c787e62d..10843c2d5 100644 --- a/oggm/tests/test_workflow.py +++ b/oggm/tests/test_workflow.py @@ -1,5 +1,6 @@ from __future__ import division - +import warnings +warnings.filterwarnings("once", category=DeprecationWarning) import os import logging import shutil diff --git a/oggm/utils.py b/oggm/utils.py index 0c5c469d7..8a4b252e7 100644 --- a/oggm/utils.py +++ b/oggm/utils.py @@ -12,6 +12,7 @@ import shutil import zipfile import sys +from functools import partial # External libs import numpy as np @@ -42,6 +43,8 @@ 7.86570726e-01, 1.06450772e-01, 2.63865083e-04]) +tuple2int = partial(np.array, dtype=np.int64) + def empty_cache(): # pragma: no cover """Empty oggm's cache directory."""