Skip to content

Commit d6a646b

Browse files
authored
Correct warnings (#350)
* edit warnings in classify module * edit warnings in focal module * add pathfinding warnings fix * remove global warnings simplefilter * edit line length for flake in classify warning message
1 parent d2fb383 commit d6a646b

File tree

3 files changed

+49
-32
lines changed

3 files changed

+49
-32
lines changed

xrspatial/classify.py

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ class cupy(object):
2727

2828

2929
import warnings
30-
warnings.simplefilter('default')
3130

3231

3332
def color_values(agg, color_key, alpha=255):
@@ -537,21 +536,25 @@ def _run_numpy_natural_break(data, num_sample, k):
537536

538537
# warning if number of total data points to fit the model bigger than 40k
539538
if sample_data.size >= 40000:
540-
warnings.warn('natural_breaks Warning: Natural break classification '
541-
'(Jenks) has a complexity of O(n^2), '
542-
'your classification with {} data points may take '
543-
'a long time.'.format(sample_data.size),
544-
Warning)
539+
with warnings.catch_warnings():
540+
warnings.simplefilter('default')
541+
warnings.warn('natural_breaks Warning: Natural break '
542+
'classification (Jenks) has a complexity of O(n^2), '
543+
'your classification with {} data points may take '
544+
'a long time.'.format(sample_data.size),
545+
Warning)
545546

546547
uv = np.unique(sample_data)
547548
uvk = len(uv)
548549

549550
if uvk < k:
550-
warnings.warn('natural_breaks Warning: Not enough unique values '
551-
'in data array for {} classes. '
552-
'n_samples={} should be >= n_clusters={}. '
553-
'Using k={} instead.'.format(k, uvk, k, uvk),
554-
Warning)
551+
with warnings.catch_warnings():
552+
warnings.simplefilter('default')
553+
warnings.warn('natural_breaks Warning: Not enough unique values '
554+
'in data array for {} classes. '
555+
'n_samples={} should be >= n_clusters={}. '
556+
'Using k={} instead.'.format(k, uvk, k, uvk),
557+
Warning)
555558
uv.sort()
556559
bins = uv
557560
else:
@@ -649,21 +652,25 @@ def _run_cupy_natural_break(data, num_sample, k):
649652

650653
# warning if number of total data points to fit the model bigger than 40k
651654
if sample_data.size >= 40000:
652-
warnings.warn('natural_breaks Warning: Natural break classification '
653-
'(Jenks) has a complexity of O(n^2), '
654-
'your classification with {} data points may take '
655-
'a long time.'.format(sample_data.size),
656-
Warning)
655+
with warnings.catch_warnings():
656+
warnings.simplefilter('default')
657+
warnings.warn('natural_breaks Warning: Natural break '
658+
'classification (Jenks) has a complexity of O(n^2), '
659+
'your classification with {} data points may take '
660+
'a long time.'.format(sample_data.size),
661+
Warning)
657662

658663
uv = cupy.unique(sample_data)
659664
uvk = len(uv)
660665

661666
if uvk < k:
662-
warnings.warn('natural_breaks Warning: Not enough unique values '
663-
'in data array for {} classes. '
664-
'n_samples={} should be >= n_clusters={}. '
665-
'Using k={} instead.'.format(k, uvk, k, uvk),
666-
Warning)
667+
with warnings.catch_warnings():
668+
warnings.simplefilter('default')
669+
warnings.warn('natural_breaks Warning: Not enough unique values '
670+
'in data array for {} classes. '
671+
'n_samples={} should be >= n_clusters={}. '
672+
'Using k={} instead.'.format(k, uvk, k, uvk),
673+
Warning)
667674
uv.sort()
668675
bins = uv
669676
else:

xrspatial/focal.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313
from typing import Optional
1414

15-
warnings.simplefilter('default')
1615

1716
# TODO: Make convolution more generic with numba first-class functions.
1817

@@ -50,8 +49,10 @@ def _get_distance(distance_str):
5049

5150
unit = DEFAULT_UNIT
5251
if len(splits) == 1:
53-
warnings.warn('Raster distance unit not provided. '
54-
'Use meter as default.', Warning)
52+
with warnings.catch_warnings():
53+
warnings.simplefilter('default')
54+
warnings.warn('Raster distance unit not provided. '
55+
'Use meter as default.', Warning)
5556
elif len(splits) == 2:
5657
unit = splits[1]
5758

@@ -135,8 +136,10 @@ def calc_cellsize(raster: xr.DataArray,
135136
unit = raster.attrs['unit']
136137
else:
137138
unit = DEFAULT_UNIT
138-
warnings.warn('Raster distance unit not provided. '
139-
'Use meter as default.', Warning)
139+
with warnings.catch_warnings():
140+
warnings.simplefilter('default')
141+
warnings.warn('Raster distance unit not provided. '
142+
'Use meter as default.', Warning)
140143

141144
# TODO: check coordinate system
142145
# if in lat-lon, need to convert to meter, lnglat_to_meters
@@ -297,8 +300,10 @@ def annulus_kernel(cellsize_x: int,
297300

298301
if r_outer - r_inner < np.sqrt((cellsize_x / 2)**2 +
299302
(cellsize_y / 2)**2):
300-
warnings.warn('Annulus radii are closer than cellsize distance.',
301-
Warning)
303+
with warnings.catch_warnings():
304+
warnings.simplefilter('default')
305+
warnings.warn('Annulus radii are closer than cellsize distance.',
306+
Warning)
302307

303308
# Get the two circular kernels for the annulus
304309
kernel_outer = circle_kernel(cellsize_x, cellsize_y, outer_radius)

xrspatial/pathfinding.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
from typing import Union, Optional
77

88
import warnings
9-
warnings.simplefilter('default')
109

1110

1211
NONE = -1
@@ -350,18 +349,24 @@ def a_star_search(surface: xr.DataArray,
350349
start_py, start_px = _find_nearest_pixel(start_py, start_px,
351350
surface.data, barriers)
352351
if _is_not_crossable(surface.data[start_py, start_px], barriers):
353-
warnings.warn('Start at a non crossable pixel', Warning)
352+
with warnings.catch_warnings():
353+
warnings.simplefilter("default")
354+
warnings.warn('Start at a non crossable pixel', Warning)
354355

355356
goal_py, goal_px = _find_pixel_id(goal[0], goal[1], x_coords, y_coords)
356357
if snap_goal:
357358
# find nearest valid pixel to the goal location
358359
goal_py, goal_px = _find_nearest_pixel(goal_py, goal_px,
359360
surface.data, barriers)
360361
if _is_not_crossable(surface.data[goal_py, goal_px], barriers):
361-
warnings.warn('End at a non crossable pixel', Warning)
362+
with warnings.catch_warnings():
363+
warnings.simplefilter("default")
364+
warnings.warn('End at a non crossable pixel', Warning)
362365

363366
if start_py == NONE or goal_py == NONE:
364-
warnings.warn('No valid pixels in input surface', Warning)
367+
with warnings.catch_warnings():
368+
warnings.simplefilter("default")
369+
warnings.warn('No valid pixels in input surface', Warning)
365370

366371
# 2d output image that stores the path
367372
path_img = np.zeros_like(surface, dtype=np.float64)

0 commit comments

Comments
 (0)