44"""
55
66
7+ import warnings
8+
9+
710import numpy as np
811import datastock as ds
912
@@ -21,6 +24,7 @@ def check(
2124 # options
2225 closed = None ,
2326 clockwise = None ,
27+ strict = None ,
2428):
2529 """ High-level routine to format a 2d polygon
2630
@@ -55,10 +59,11 @@ def check(
5559 # check inputs
5660 # -------------
5761
58- key , close , clockwise = _check (
62+ key , close , clockwise , strict = _check (
5963 key = key ,
6064 closed = closed ,
6165 clockwise = clockwise ,
66+ strict = strict ,
6267 )
6368
6469 # -------------
@@ -98,15 +103,29 @@ def check(
98103 # no duplicates
99104 # -------------
100105
101- uni = np .unique ([x0_closed [:- 1 ], x1_closed [:- 1 ]], axis = 1 )
106+ uni , ind_uni = np .unique (
107+ [x0_closed [:- 1 ], x1_closed [:- 1 ]],
108+ axis = 1 ,
109+ return_index = True ,
110+ )
102111 if uni .shape [1 ] < (x0_closed .size - 1 ):
103- ndup = x0 .size - 1 - uni .shape [1 ]
112+ ind_dup = np .array ([
113+ ii for ii in range (x0_closed .size - 1 )
114+ if ii not in ind_uni
115+ ])
116+ ndup = x0_closed .size - 1 - uni .shape [1 ]
104117 msg = (
105118 f"Polygon 2d '{ key } ' seems to have { ndup } duplicate points!\n "
106- "\t - x0 = {x0_closed[:-1]}\n "
107- "\t - x1 = {x1_closed[:-1]}\n "
119+ f"\t - total nb of pts (with duplicates) = { x0_closed .size - 1 } \n "
120+ f"\t - total nb of pts (no duplicates) = { uni .shape [1 ]} \n "
121+ f"\t - duplicates: { ndup } \n "
122+ f"\t \t - x0 = { x0_closed [ind_dup ]} \n "
123+ f"\t \t - x1 = { x1_closed [ind_dup ]} \n "
108124 )
109- raise Exception (msg )
125+ if strict is True :
126+ raise Exception (msg )
127+ else :
128+ warnings .warn (msg )
110129
111130 # -------------
112131 # clockwise
@@ -143,6 +162,7 @@ def _check(
143162 key = None ,
144163 closed = None ,
145164 clockwise = None ,
165+ strict = None ,
146166):
147167
148168 # ---------------
@@ -175,7 +195,17 @@ def _check(
175195 default = True ,
176196 )
177197
178- return key , closed , clockwise
198+ # ---------------
199+ # strict
200+ # ---------------
201+
202+ strict = ds ._generic_check ._check_var (
203+ strict , 'strict' ,
204+ types = bool ,
205+ default = True ,
206+ )
207+
208+ return key , closed , clockwise , strict
179209
180210
181211# ###########################################################
@@ -186,4 +216,4 @@ def _check(
186216
187217def is_clockwise (x0 , x1 ):
188218 area_signed = np .sum ((x0 [1 :] - x0 [:- 1 ]) * (x1 [1 :] + x1 [:- 1 ]))
189- return area_signed > 0
219+ return area_signed > 0
0 commit comments