Coverage for pygeodesy/hausdorff.py : 90%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
# -*- coding: utf-8 -*-
L{HausdorffEquirectangular}, L{HausdorffEuclidean}, L{HausdorffHaversine}, L{HausdorffKarney} and L{HausdorffVincentys} to compute U{Hausdorff <https://WikiPedia.org/wiki/Hausdorff_distance>} distances between two sets of C{LatLon}, C{NumPy}, C{tuples} or other types of points.
Only L{HausdorffKarney} depends on an external package, Charles Karney's U{geographiclib<https://PyPI.org/project/geographiclib>}.
Typical usage is as follows. First, create a C{Hausdorff} calculator from a given set of C{LatLon} points, called the C{model} or C{template} points.
C{h = HausdorffXyz(points1, ...)}
Get the C{directed} or C{symmetric} Hausdorff distance to a second set of C{LatLon} points, named the C{target} points, by using
C{t6 = h.directed(points2)}
respectively
C{t6 = h.symmetric(points2)}.
Or, use function C{hausdorff_} with a proper C{distance} function and optionally a C{point} function passed as keyword arguments as follows
C{t6 = hausdorff_(points1, points2, ..., distance=..., point=...)}.
In all cases, the returned result C{t6} is a L{Hausdorff6Tuple}.
For C{(lat, lon, ...)} points in a C{NumPy} array or plain C{tuples}, wrap the points in a L{Numpy2LatLon} respectively L{Tuple2LatLon} instance, more details in the documentation thereof.
For other points, create a L{Hausdorff} sub-class with the appropriate C{distance} method overloading L{Hausdorff.distance} and optionally a C{point} method overriding L{Hausdorff.point} as the next example.
>>> from pygeodesy import Hausdorff, hypot_ >>> >>> class H3D(Hausdorff): >>> """Custom Hausdorff example. >>> """ >>> def distance(self, p1, p2): >>> return hypot_(p1.x - p2.x, p1.y - p2.y, p1.z - p2.z) >>> >>> h3D = H3D(xyz1, ..., units="...") >>> d6 = h3D.directed(xyz2)
Transcribed from the original SciPy U{Directed Hausdorff Code <https://GitHub.com/scipy/scipy/blob/master/scipy/spatial/_hausdorff.pyx>} version 0.19.0, Copyright (C) Tyler Reddy, Richard Gowers, and Max Linke, 2016, distributed under the same BSD license as SciPy, including C{early breaking} and C{random sampling} as in U{Abdel Aziz Taha, Allan Hanbury "An Efficient Algorithm for Calculating the Exact Hausdorff Distance" <https://Publik.TUWien.ac.AT/files/PubDat_247739.pdf>}, IEEE Trans. Pattern Analysis Machine Intelligence (PAMI), vol 37, no 11, pp 2153-2163, Nov 2015. '''
_scaler, vincentys_
'''Hausdorff issue. '''
'''6-Tuple C{(hd, i, j, mn, md, units)} with the U{Hausdorff <https://WikiPedia.org/wiki/Hausdorff_distance>} distance C{hd}, indices C{i} and C{j}, the total count C{mn}, the C{I{mean} Hausdorff} distance C{md} and the name of the distance C{units}.
For C{directed Hausdorff} distances, count C{mn} is the number of model points considered. For C{symmetric Hausdorff} distances count C{mn} twice that.
Indices C{i} and C{j} are the C{model} respectively C{target} point with the C{hd} distance.
Mean distance C{md} is C{None} if an C{early break} occurred and U{early breaking<https://Publik.TUWien.ac.AT/files/PubDat_247739.pdf>} was enabled by keyword argument C{early=True}. '''
'''Hausdorff base class, requires method L{Hausdorff.distance} to be overloaded. '''
'''New L{Hausdorff} calculator.
@param points: Initial set of points, aka the C{model} or C{template} (C{LatLon}[], C{Numpy2LatLon}[], C{Tuple2LatLon}[] or C{other}[]). @keyword seed: Random sampling seed (C{any}) or C{None}, C{0} or C{False} for no U{random sampling<https:// Publik.TUWien.ac.AT/files/PubDat_247739.pdf>}. @keyword name: Optional calculator name (C{str}). @keyword units: Optional, distance units (C{str}).
@raise HausdorffError: Insufficient number of B{C{points}} or invalid B{C{seed}}. ''' self.name = name self.units = units
'''Compute only the C{forward Hausdorff} distance.
@param points: Second set of points, aka the C{target} (C{LatLon}[], C{Numpy2LatLon}[], C{Tuple2LatLon}[] or C{other}[]). @keyword early: Enable or disable U{early breaking<https:// Publik.TUWien.ac.AT/files/PubDat_247739.pdf>} (C{bool}).
@return: A L{Hausdorff6Tuple}C{(hd, i, j, mn, md, units)}.
@raise HausdorffError: Insufficient number of B{C{points}}. ''' self.units, self.distance, self.point)
'''Distance between 2 points from C{.point}.
@note: This method I{must be overloaded}.
@raise AssertionError: Not overloaded. ''' self._notOverloaded(self.distance.__name__, point1, point2)
'''Convert a C{model} or C{target} point for the C{.distance} method. '''
def seed(self): '''Get the random sampling seed (C{any} or C{None}). '''
def seed(self, seed): '''Set the random sampling seed.
@param seed: Valid L{Random(seed)} or C{None}, C{0} or C{False} for no U{random sampling<https:// Publik.TUWien.ac.AT/files/PubDat_247739.pdf>}.
@raise HausdorffError: Invalid B{C{seed}}. ''' except (TypeError, ValueError): raise HausdorffError('%s invalid: %r' % ('seed', seed)) else: self._seed = None
'''Compute the combined C{forward and reverse Hausdorff} distance.
@param points: Second set of points, aka the C{target} (C{LatLon}[], C{Numpy2LatLon}[], C{Tuple2LatLon}[] or C{other}[]). @keyword early: Enable or disable U{early breaking<https:// Publik.TUWien.ac.AT/files/PubDat_247739.pdf>} (C{bool}).
@return: A L{Hausdorff6Tuple}C{(hd, i, j, mn, md, units)}.
@raise HausdorffError: Insufficient number of B{C{points}}. ''' self.units, self.distance, self.point)
def units(self): '''Get the distance units (C{str} or C{""}). '''
def units(self, units): '''Set the distance units.
@param units: New units name (C{str}). ''' self._units = str(units or "")
'''L{Hausdorff} base class for distances in C{degrees} from C{LatLon} points in C{degrees}. '''
'''L{Hausdorff} base class for distances in C{radians} from C{LatLon} points converted from C{degrees} to C{radians}. '''
'''Convert C{(lat, lon)} point in degrees to C{(a, b)} in radians.
@return: An L{PhiLam2Tuple}C{(phi, lam)}. '''
'''Compute the C{Hausdorff} distance based on the C{equirectangular} distance (in radians squared) like function L{equirectangular_}.
@see: L{HausdorffEuclidean}, L{HausdorffHaversine}, L{HausdorffKarney} and L{HausdorffVincentys}. '''
'''New L{HausdorffEquirectangular} calculator.
@param points: Initial set of points, aka the C{model} or C{template} (C{LatLon}[], C{Numpy2LatLon}[], C{Tuple2LatLon}[] or C{other}[]). @keyword adjust: Adjust the wrapped, unrolled longitudinal delta by the cosine of the mean latitude (C{bool}). @keyword wrap: Wrap and L{unroll180} longitudes (C{bool}). @keyword seed: Random seed (C{any}) or C{None}, C{0} or C{False} for no U{random sampling<https:// Publik.TUWien.ac.AT/files/PubDat_247739.pdf>}. @keyword name: Optional calculator name (C{str}).
@raise HausdorffError: Insufficient number of B{C{points}} or invalid B{C{adjust}} or B{C{seed}}. ''' self._adjust = False self._wrap = True
'''Return the L{equirectangular_} distance in C{radians squared}. '''
'''Compute the C{Hausdorff} distance based on the C{Euclidean} distance (in radians) from function L{euclidean_}.
@see: L{HausdorffEquirectangular}, L{HausdorffHaversine}, L{HausdorffKarney} and L{HausdorffVincentys}. '''
'''New L{HausdorffEuclidean} calculator.
@param points: Initial set of points, aka the C{model} or C{template} (C{LatLon}[], C{Numpy2LatLon}[], C{Tuple2LatLon}[] or C{other}[]). @keyword adjust: Adjust the wrapped, unrolled longitudinal delta by the cosine of the mean latitude (C{bool}). @keyword seed: Random sampling seed (C{any}) or C{None}, C{0} or C{False} for no U{random sampling<https:// Publik.TUWien.ac.AT/files/PubDat_247739.pdf>}. @keyword name: Optional calculator name (C{str}).
@raise HausdorffError: Insufficient number of B{C{points}} or invalid B{C{seed}}. ''' self._adjust = False
'''Return the L{euclidean_} distance in C{radians}. '''
'''Compute the C{Hausdorff} distance based on the I{angular} C{Haversine} distance (in radians) from function L{haversine_}.
@note: See note under L{HausdorffVincentys}.
@see: L{HausdorffEquirectangular}, L{HausdorffEuclidean}, L{HausdorffKarney} and L{HausdorffVincentys}. '''
'''New L{HausdorffHaversine} calculator.
@param points: Initial set of points, aka the C{model} or C{template} (C{LatLon}[], C{Numpy2LatLon}[], C{Tuple2LatLon}[] or C{other}[]). @keyword wrap: Wrap and L{unroll180} longitudes (C{bool}). @keyword seed: Random sampling seed (C{any}) or C{None}, C{0} or C{False} for no U{random sampling<https:// Publik.TUWien.ac.AT/files/PubDat_247739.pdf>}. @keyword name: Optional calculator name (C{str}).
@raise HausdorffError: Insufficient number of B{C{points}} or invalid B{C{seed}}. ''' self._wrap = True
'''Return the L{haversine_} distance in C{radians}. '''
'''Compute the C{Hausdorff} distance based on the I{angular} distance (in degrees) from I{Charles Karney's} U{GeographicLib <https://PyPI.org/project/geographiclib>} U{Geodesic <https://geographiclib.sourceforge.io/1.49/python/code.html>} Inverse method.
@see: L{HausdorffEquirectangular}, L{HausdorffEuclidean}, L{HausdorffHaversine} and L{HausdorffVincentys}. '''
'''New L{HausdorffKarney} calculator.
@param points: Initial set of points, aka the C{model} or C{template} (C{LatLon}[], C{Numpy2LatLon}[], C{Tuple2LatLon}[] or C{other}[]). @keyword datum: Optional datum (L{Datum} to use, overriding the default C{I{model} points[0].datum}. @keyword wrap: Wrap and L{unroll180} longitudes (C{bool}). @keyword seed: Random sampling seed (C{any}) or C{None}, C{0} or C{False} for no U{random sampling<https:// Publik.TUWien.ac.AT/files/PubDat_247739.pdf>}. @keyword name: Optional calculator name (C{str}).
@raise HausdorffError: Insufficient number of B{C{points}} or invalid B{C{seed}}.
@raise ImportError: Package U{GeographicLib <https://PyPI.org/project/geographiclib>} missing.
@raise TypeError: Invalid B{C{datum}}. ''' self._wrap = True raise TypeError except (AttributeError, TypeError): raise _IsNotError('valid', datum=self.datum or datum)
def datum(self): '''Get the datum of this calculator (L{Datum}). '''
'''Return the non-negative I{angular} distance in C{degrees}. ''' # see .ellipsoidalKarney.LatLon._inverse # XXX g.DISTANCE needed for 's12', distance in meters?
'''Compute the C{Hausdorff} distance based on the I{angular} C{Vincenty} distance (in radians) from function L{vincentys_}.
@note: See note under L{vincentys_}.
@see: L{HausdorffEquirectangular}, L{HausdorffEuclidean}, L{HausdorffHaversine} and L{HausdorffKarney}. '''
'''New L{HausdorffVincentys} calculator.
@param points: Initial set of points, aka the C{model} or C{template} (C{LatLon}[], C{Numpy2LatLon}[], C{Tuple2LatLon}[] or C{other}[]). @keyword wrap: Wrap and L{unroll180} longitudes (C{bool}). @keyword seed: Random sampling seed (C{any}) or C{None}, C{0} or C{False} for no U{random sampling<https:// Publik.TUWien.ac.AT/files/PubDat_247739.pdf>}. @keyword name: Optional calculator name (C{str}).
@raise HausdorffError: Insufficient number of B{C{points}} or invalid B{C{seed}}. ''' self._wrap = True
'''Return the L{vincentys_} distance in C{radians}. '''
'''(INTERNAL) Core of function L{hausdorff.hausdorff} and methods C{directed} and C{symmetric} of classes C{hausdorff.Hausdorff...}. ''' # shuffling the points generally increases the # chance of an early break in the inner j loop
# forward or forward and backward else: # no early break else: # swap model and target
'''Default B{C{point}} callable for function L{hausdorff_}.
@param p: The original C{model} or C{target} point (C{any}).
@return: The point, suitable for the L{hausdorff_} B{C{distance}} callable. ''' return p
distance=None, point=_point): '''Compute the C{directed} or C{symmetric} U{Hausdorff distance<https:// WikiPedia.org/wiki/Hausdorff_distance>} between 2 sets of points with or without U{early breaking<https://Publik.TUWien.ac.AT/files/PubDat_247739.pdf>} and U{random sampling<https://Publik.TUWien.ac.AT/files/PubDat_247739.pdf>}.
@param model: First set of points (C{LatLon}[], C{Numpy2LatLon}[], C{Tuple2LatLon}[] or C{other}[]). @param target: Second set of points (C{LatLon}[], C{Numpy2LatLon}[], C{Tuple2LatLon}[] or C{other}[]). @keyword both: Return the C{directed} (forward only) or the C{symmetric} (combined forward and reverse) C{Hausdorff} distance (C{bool}). @keyword early: Enable or disable U{early breaking<https://Publik.TUWien.ac.AT/ files/PubDat_247739.pdf>} (C{bool}). @keyword seed: Random sampling seed (C{any}) or C{None}, C{0} or C{False} for no U{random sampling<https://Publik.TUWien.ac.AT/files/PubDat_247739.pdf>}. @keyword units: Optional, name of the distance units (C{str}). @keyword distance: Callable returning the distance between a B{C{model}} and B{C{target}} point (signature C{(point1, point2)}). @Keyword point: Callable returning the B{C{model}} or B{C{target}} point suitable for B{C{distance}} (signature C{(point)}).
@return: A L{Hausdorff6Tuple}C{(hd, i, j, mn, md, units)}.
@raise HausdorffError: Insufficient number of B{C{model}} or B{C{target}} points.
@raise TypeError: If B{C{distance}} or B{C{point}} is not callable. ''' if not callable(distance): raise _IsNotError(callable.__name__, distance=distance) if not callable(point): raise _IsNotError(callable.__name__, point=point)
_, ps1 = points2(model, closed=False, Error=HausdorffError) _, ps2 = points2(target, closed=False, Error=HausdorffError) return _hausdorff_(ps1, ps2, both, early, seed, units, distance, point)
'''Return a C{seed}ed random range function generator.
@param seed: Initial, internal L{Random} state (C{hashable}).
@note: L{Random} B{C{seed}} C{None} seeds from the current time or from a platform-specific randomness source, if available.
@return: A function to generatore random ranges.
@example:
>>> rrange = randomrangenerator('R') >>> for r in rrange(n): >>> ... # r is random in 0..n-1 '''
'''Like standard L{range}C{start, stop=..., step=...)}, except the returned values are in random order.
@note: Especially C{range(n)} behaves like standard L{Random.sample}C{(range(n), n)} but avoids creating a tuple with the entire C{population} and a list containing all sample values (for large C{n}). '''
else:
# **) MIT License # # Copyright (C) 2016-2020 -- mrJean1 at Gmail -- All Rights Reserved. # # Permission is hereby granted, free of charge, to any person obtaining a # copy of this software and associated documentation files (the "Software"), # to deal in the Software without restriction, including without limitation # the rights to use, copy, modify, merge, publish, distribute, sublicense, # and/or sell copies of the Software, and to permit persons to whom the # Software is furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included # in all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR # OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, # ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR # OTHER DEALINGS IN THE SOFTWARE. |