Skip to content

Commit

Permalink
added pytests and .lax-File for Netzkater demo data
Browse files Browse the repository at this point in the history
  • Loading branch information
lwiniwar committed Nov 30, 2022
1 parent f2b6834 commit b005b1b
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 1 deletion.
Binary file added demo/las_623_5718_1_th_2014-2019.lax
Binary file not shown.
2 changes: 1 addition & 1 deletion src/pyForMetrix/metricCalculators/publications.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def __call__(self, points_in_poly, rumple_pixel_size=1):
outArray[3] = scipy.stats.kurtosis(points[:, 2])
outArray[4:6] = np.percentile(points[:, 2], [10, 90])
outArray[6] = np.count_nonzero(points[:, 2] > outArray[0]) / points.shape[0]
rumple = rumple_index(points, rumple_pixel_size)
rumple = rumple_index(points_in_poly, rumple_pixel_size)
outArray[7] = rumple

return outArray
Expand Down
62 changes: 62 additions & 0 deletions tests/test_plot_metrics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import geopandas, pathlib
datadir = pathlib.Path(__file__).parent / '../demo'

def ensure_netzkater_data():
import os, wget, zipfile
if not os.path.exists(datadir / 'las_623_5718_1_th_2014-2019.laz'):
if not os.path.exists(datadir / 'data_netzkater.zip'):
print('Downloading file')
wget.download(
'https://geoportal.geoportal-th.de/hoehendaten/LAS/las_2014-2019/las_623_5718_1_th_2014-2019.zip',
str((datadir / 'data_netzkater.zip').absolute()))
print('Unzipping file')
zipfile.ZipFile(str((datadir / 'data_netzkater.zip').absolute())).extractall(str(datadir.absolute()))


def test_paper_metrics():
from pyForMetrix.metricCalculators.publications import MCalc_Hollaus_et_al_2009, MCalc_White_et_al_2015, \
MCalc_Xu_et_al_2019, MCalc_Woods_et_al_2009
from pyForMetrix.metrix import PlotMetrics
ensure_netzkater_data()
polys = geopandas.read_file(datadir / 'netzkater_polygons.gpkg')
pm = PlotMetrics([datadir / 'las_623_5718_1_th_2014-2019.laz'], polys)
mcs = [MCalc_Hollaus_et_al_2009(), MCalc_White_et_al_2015(), MCalc_Xu_et_al_2019(), MCalc_Woods_et_al_2009()]
results = pm.calc_custom_metrics(mcs)
assert results.shape == (7,62)
print(results)

def test_lidRmetrics():
from pyForMetrix.metricCalculators.types import \
MCalc_lidRmetrics_lad, \
MCalc_lidRmetrics_kde, \
MCalc_lidRmetrics_dispersion, \
MCalc_lidRmetrics_voxels, \
MCalc_lidRmetrics_HOME, \
MCalc_lidRmetrics_percabove, \
MCalc_lidRmetrics_echo, \
MCalc_lidRmetrics_basic, \
MCalc_lidRmetrics_Lmoments, \
MCalc_lidRmetrics_rumple, \
MCalc_lidRmetrics_percentiles, \
MCalc_lidRmetrics_interval, \
MCalc_lidRmetrics_canopydensity

from pyForMetrix.metrix import PlotMetrics
ensure_netzkater_data()
polys = geopandas.read_file(datadir / 'netzkater_polygons.gpkg')
pm = PlotMetrics([datadir / 'las_623_5718_1_th_2014-2019.laz'], polys)
mcs = [MCalc_lidRmetrics_lad(),
MCalc_lidRmetrics_kde(),
MCalc_lidRmetrics_dispersion(),
MCalc_lidRmetrics_voxels(),
MCalc_lidRmetrics_HOME(),
MCalc_lidRmetrics_percabove(),
MCalc_lidRmetrics_echo(),
MCalc_lidRmetrics_basic(),
MCalc_lidRmetrics_rumple(),
MCalc_lidRmetrics_percentiles(),
MCalc_lidRmetrics_interval(),
MCalc_lidRmetrics_canopydensity()]
results = pm.calc_custom_metrics(mcs)
assert results.shape == (7,78)
print(results)
27 changes: 27 additions & 0 deletions tests/test_raster_metrics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from test_plot_metrics import ensure_netzkater_data, datadir


def test_group_metrics():
from pyForMetrix.metricCalculators.types import MCalc_VisMetrics, MCalc_DensityMetrics, \
MCalc_HeightMetrics, MCalc_EchoMetrics, MCalc_CoverMetrics, MCalc_VarianceMetrics
from pyForMetrix.metrix import RasterMetrics
from pyForMetrix.normalizer import normalize
ensure_netzkater_data()
import laspy
data = laspy.read(datadir / 'las_623_5718_1_th_2014-2019.laz')
points = {
'points': data.xyz,
'classification': data.classification,
'echo_number': data.return_number,
'scan_angle_rank': data.scan_angle_rank,
'pt_src_id': data.point_source_id
}
normalize(points)
rm = RasterMetrics(points, raster_size=25)
mcs = [MCalc_EchoMetrics(), MCalc_DensityMetrics(), MCalc_CoverMetrics(),
MCalc_VarianceMetrics(), MCalc_HeightMetrics(), MCalc_VisMetrics()]
results = rm.calc_custom_metrics(mcs)
assert results.shape == (40, 40, 35)
assert abs(results.sel({'val':'p100'}).data.max() - 105.63799999) < 0.0001

print(results)

0 comments on commit b005b1b

Please sign in to comment.