Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
82 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
laserchicken/feature_extractor/entropy_feature_extractor.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import numpy as np | ||
from laserchicken import keys | ||
from laserchicken.feature_extractor.abc import AbstractFeatureExtractor | ||
|
||
class EntropyFeatureExtractor(AbstractFeatureExtractor): | ||
|
||
# TODO: make this settable from command line | ||
layer_thickness = 0.5 | ||
zmin = None | ||
zmax = None | ||
|
||
@classmethod | ||
def requires(cls): | ||
return [] | ||
|
||
@classmethod | ||
def provides(cls): | ||
return ['z_entropy'] | ||
|
||
def get_params(self): | ||
p = [self.layer_thickness] | ||
if(not self.zmin is None): | ||
p.append(self.zmin) | ||
if(not self.zmax is None): | ||
p.append(self.zmax) | ||
return p | ||
|
||
|
||
def extract(self,sourcepc,neighborhood,targetpc,targetindex): | ||
z = sourcepc[keys.point]["z"]["data"][neighborhood] | ||
_zmin = np.min(z) if self.zmin is None else self.zmin | ||
_zmax = np.max(z) if self.zmax is None else self.zmax | ||
nbins = int(np.ceil((_zmax - _zmin)/self.layer_thickness)) | ||
data = np.histogram(z,bins = nbins,range = (_zmin,_zmax),density = True)[0] | ||
entropyfunc = np.vectorize(xlog2x) | ||
norm = np.sum(data) | ||
return -(entropyfunc(data/norm)).sum() | ||
|
||
|
||
def xlog2x(x): | ||
return 0 if x == 0 else x * np.log2(x) |
36 changes: 36 additions & 0 deletions
36
laserchicken/test_feature_extractor/test_entropy_feature_extractor.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import os | ||
import random | ||
import unittest | ||
|
||
from laserchicken import compute_neighbors, feature_extractor, keys, read_las, utils | ||
|
||
class TestExtractEntropy(unittest.TestCase): | ||
|
||
_test_file_name = 'AHN3.las' | ||
_test_data_source = 'testdata' | ||
pointcloud = None | ||
|
||
def test_entropy_in_cylinders(self): | ||
"""Test computing of eigenvalues in cylinder.""" | ||
num_all_pc_points = len(self.pointcloud[keys.point]["x"]["data"]) | ||
rand_indices = [random.randint(0, num_all_pc_points) for p in range(20)] | ||
target_pointcloud = utils.copy_pointcloud(self.pointcloud, rand_indices) | ||
numtargets = len(target_pointcloud[keys.point]["x"]["data"]) | ||
radius = 25 | ||
result_index_lists = compute_neighbors.compute_cylinder_neighbourhood_indicies( | ||
self.pointcloud, target_pointcloud, radius) | ||
feature_extractor.compute_features(self.pointcloud, result_index_lists, target_pointcloud, | ||
["z_entropy"],layer_thickness = 0.1) | ||
for i in range(numtargets): | ||
H = utils.get_feature(target_pointcloud, i, "z_entropy") | ||
self.assertTrue(H >= 0) | ||
self.assertEqual("laserchicken.feature_extractor.entropy_feature_extractor", | ||
target_pointcloud[keys.provenance][0]["module"]) | ||
self.assertEqual([0.1],target_pointcloud[keys.provenance][0]["parameters"]) | ||
|
||
def setUp(self): | ||
self.pointcloud = read_las.read(os.path.join(self._test_data_source, self._test_file_name)) | ||
random.seed(102938482634) | ||
|
||
def tearDown(self): | ||
pass |