def test_cylinder(self): """Compute the density for a cylinder given as index of source pc.""" neighborhoods = compute_neighborhoods(self.point_cloud, self.targetpc, self.cyl) extractor = PointDensityFeatureExtractor() densities = extractor.extract(self.point_cloud, neighborhoods, None, None, self.cyl) np.testing.assert_allclose(densities, self.real_density)
def test_sphere(self): """Compute the density for a sphere given as index of the source pc.""" neighborhoods = list(compute_neighborhoods(self.point_cloud, self.targetpc, self.sphere)) extractor = PointDensityFeatureExtractor() densities = extractor.extract(self.point_cloud, neighborhoods, None, None, self.sphere) np.testing.assert_allclose(densities, self.volume_density)
def setUp(self): """ Set up the test. Create a sphere and a cylinder of points and a central point The cylinder has no points on the equator of the sphere """ # create the points self.radius = 0.5 self.xyz = [[0., 0., 0.]] self._set_sphere_data() self._set_cylinder_data() # create the pc self.point_cloud, self.npts = self._get_pc(np.array(self.xyz)) self.target_point_cloud = self._get_central_point(0) self.indexpc = 0 # create the volume/neighborhood self.cylinder = InfiniteCylinder(self.radius + 1E-3) self.neighborhoods = list( compute_neighborhoods(self.point_cloud, self.target_point_cloud, self.cylinder)) # theoretical value of the echo ratio self.theoretical_value = (self.npt_sphere + 1) / (self.npt_sphere + self.npt_cyl + 1)
def test_valid(self): """Compute the echo ratio for a sphere/cylinder at different target points without crashing.""" # read the data self.point_cloud = read_las.read( os.path.join(self._test_data_source, self._test_file_name)) # get the target point clouds random.seed(102938482634) self.target_point_cloud = self._get_random_targets() self.target_point_cloud_index = 0 # volume descriptions radius = 0.5 self.cyl = InfiniteCylinder(radius) neighbors = compute_neighborhoods(self.point_cloud, self.target_point_cloud, self.cyl) cylinder_index = [] for x in neighbors: cylinder_index += x # extractor extractor = PulsePenetrationFeatureExtractor() for index in cylinder_index: extractor.extract(self.point_cloud, index, None, None, None)
def setUp(self): """ Set up the test. Load in a bunch of real data from AHN3. """ np.random.seed(1234) _TEST_FILE_NAME = 'AHN3.las' _TEST_DATA_SOURCE = 'testdata' _CYLINDER = InfiniteCylinder(4) _PC_260807 = load(os.path.join(_TEST_DATA_SOURCE, _TEST_FILE_NAME)) _PC_1000 = copy_point_cloud( _PC_260807, array_mask=(np.random.choice(range( len(_PC_260807[keys.point]['x']['data'])), size=1000, replace=False))) _1000_NEIGHBORHOODS_IN_260807 = list( compute_neighbors.compute_neighborhoods(_PC_260807, _PC_1000, _CYLINDER)) self.point_cloud = _PC_260807 self.neigh = _1000_NEIGHBORHOODS_IN_260807
def test_cell_no_points(self): point_cloud = create_emtpy_point_cloud() targets = create_point_cloud(np.zeros(1), np.zeros(1), np.zeros(1)) neighborhoods = compute_neighborhoods(point_cloud, targets, Cell(1)) neighborhood = next(neighborhoods) print(neighborhood) assert_equal(len(neighborhood[0]), 0)
def assert_target_number_matches_neighborhood_number(environment_point_cloud): n_targets = 99 targets = create_point_cloud(np.array(range(n_targets)), np.array(range(n_targets)), np.array(range(n_targets))) neighborhoods = list( compute_neighborhoods(environment_point_cloud, targets, Cube(2))) assert_equal(len(neighborhoods), n_targets)
def test_cell_grid_origin(self): _, points = create_points_in_xy_grid(lambda x, y: np.random.rand()) point_cloud = create_point_cloud(points[:, 0], points[:, 1], points[:, 2]) targets = create_point_cloud(np.array([0]), np.array([0]), np.array([0])) # Center of grid neighborhoods = list( compute_neighborhoods(point_cloud, targets, Cell(1.99))) assert_equal(len(neighborhoods[0]), 1)
def test_compute_neighbors_cylinderVolume(self): """Compute neighbors should detect cylinder volume and find neighbors accordingly""" target_point_cloud = self._get_random_targets() cylinder = InfiniteCylinder(0.5) neighborhoods = compute_neighborhoods(self.point_cloud, target_point_cloud, cylinder) self._assert_all_points_within_cylinder(neighborhoods, target_point_cloud, cylinder.radius)
def test_compute_neighbors_sphereVolume(self): """Compute neighbors should detect sphere volume and find neighbors accordingly""" target_point_cloud = self._get_random_targets() sphere = Sphere(0.5) neighborhoods = compute_neighborhoods(self.point_cloud, target_point_cloud, sphere) self._assert_all_points_within_sphere(neighborhoods, target_point_cloud, sphere.radius)
def test_cube_grid(self): _, points = create_points_in_xy_grid(lambda x, y: 10 * (x % 2)) point_cloud = create_point_cloud(points[:, 0], points[:, 1], points[:, 2]) targets = create_point_cloud(np.array([4.5]), np.array([4.5]), np.array([0])) # Center of grid neighborhoods = list( compute_neighborhoods(point_cloud, targets, Cube(2))) assert_equal(len(neighborhoods[0]), 2)
def test_cylinder(self): """Compute the density for a cylinder given as index of source pc.""" neighbors_index = compute_neighborhoods(self.point_cloud, self.targetpc, self.cyl) extractor = PointDensityFeatureExtractor() for index in neighbors_index: d = extractor.extract(self.point_cloud, index, None, None, self.cyl) self.assertEqual(d, self.areadens)
def test_sphere(self): """Compute the density for a sphere given as index of the source pc.""" neighbors_index = list(compute_neighborhoods(self.point_cloud, self.targetpc, self.sphere)) extractor = PointDensityFeatureExtractor() for index in neighbors_index: d = extractor.extract(self.point_cloud, index, None, None, self.sphere) self.assertEqual(d, self.voldens)
def test_cell_grid(self): _, points = create_points_in_xy_grid(lambda x, y: np.random.rand()) point_cloud = create_point_cloud(points[:, 0], points[:, 1], points[:, 2]) targets = create_point_cloud(np.array([4.5]), np.array([4.5]), np.array([4.5])) # Center of grid neighborhoods = compute_neighborhoods(point_cloud, targets, Cell(2)) neighborhood = next(neighborhoods) assert_equal(len(neighborhood[0]), 4)
def test_cell_grid_larger_sample_size(self): _, points = create_points_in_xy_grid(lambda x, y: np.random.rand()) point_cloud = create_point_cloud(points[:, 0], points[:, 1], points[:, 2]) targets = create_point_cloud(np.array([4.5]), np.array([4.5]), np.array([4.5])) # Center of grid neighborhoods = compute_neighborhoods( point_cloud, targets, Cell(5), sample_size=10000) # Result 36 neighbors _ = next(neighborhoods)
def test_cylinder_index(self): """Compute the density for a cylinder given as index of source pc.""" neighbors = compute_neighborhoods(self.point_cloud, self.targetpc, self.cyl) neighbors_index = [] for x in neighbors: neighbors_index += x extractor = PointDensityFeatureExtractor() for index in neighbors_index: extractor.extract(self.point_cloud, index, None, None, self.cyl)
def test_cell(self): n_included = 123 n_excluded = 456 x = np.append(np.zeros(n_included), np.ones(n_excluded)) environment = create_point_cloud(x, x, x) target = create_point_cloud(np.zeros(1), np.zeros(1), np.zeros(1)) cube = Cube(1) # volume = 1.0 neighborhoods = compute_neighborhoods(environment, target, cube) extractor = PointDensityFeatureExtractor() densities = extractor.extract(environment, neighborhoods, target, [0], cube) np.testing.assert_allclose(densities, n_included)
def test_cell(self): n_included = 123 n_excluded = 456 x = np.append(np.zeros(n_included), np.ones(n_excluded)) environment = create_point_cloud(x, x, x) target = create_point_cloud(np.zeros(1), np.zeros(1), np.zeros(1)) cube = Cube(1) # volume = 1.0 neighbors_index = compute_neighborhoods(environment, target, cube) extractor = PointDensityFeatureExtractor() for index in neighbors_index: d = extractor.extract(environment, index, target, [0], cube) self.assertEqual(d, n_included)
def normalize(point_cloud, cell_size=None): z = point_cloud[keys.point]['z']['data'] point_cloud[keys.point][normalized_height] = {"type": 'float64', "data": np.array(z)} if cell_size is None: n_points = point_cloud[keys.point][normalized_height]['data'].size min_z = _calculate_min_z(range(n_points), point_cloud) point_cloud[keys.point][normalized_height]['data'] = z - min_z else: targets = _create_spanning_grid(point_cloud, cell_size) neighborhoods = compute_neighborhoods(point_cloud, targets, Cell(cell_size), sample_size=None) for neighborhood in neighborhoods: min_z = _calculate_min_z(neighborhood, point_cloud) point_cloud[keys.point][normalized_height]['data'][neighborhood] = z[neighborhood] - min_z import sys module = sys.modules[__name__] add_metadata(point_cloud, module, {'cell_size':cell_size}) return point_cloud
def setUp(self): # read the data self.point_cloud = read_las.read( os.path.join(self._test_data_source, self._test_file_name)) # get the target point clouds random.seed(102938482634) self.target_pc_sequential = self._get_random_targets() self.target_pc_vector = utils.copy_point_cloud( self.target_pc_sequential) self.target_pc_index = 0 # volume descriptions radius = 0.5 self.cyl = InfiniteCylinder(radius) self.neighbors = compute_neighborhoods(self.point_cloud, self.target_pc_sequential, self.cyl) self.cylinder_index = [] for x in self.neighbors: self.cylinder_index += x
def normalize(point_cloud, cell_size=None): z = point_cloud[keys.point]['z']['data'] point_cloud[keys.point][normalized_height] = {"type": 'float64', "data": np.array(z)} if cell_size is None: n_points = point_cloud[keys.point][normalized_height]['data'].size _, min_z, _ = range_extractor().extract(point_cloud, range(n_points), None, None, None) point_cloud[keys.point][normalized_height]['data'] = z - min_z else: targets = create_spanning_grid(point_cloud, cell_size) neighborhood_sets = compute_neighborhoods(point_cloud, targets, Cell(cell_size), sample_size=None) for neighborhood_set in neighborhood_sets: for neighborhood in neighborhood_set: _, min_z, _ = range_extractor().extract(point_cloud, neighborhood, None, None, None) point_cloud[keys.point][normalized_height]['data'][neighborhood] = z[neighborhood] - min_z import sys module = sys.modules[__name__] add_metadata(point_cloud, module, {'cell_size':cell_size}) return point_cloud
def setUp(self): """ Create a grid of 4 targets and an environment point cloud and neighbors and the echo ratios of those targets. """ self.radius = 0.5 targets = np.array([[10., 0., 5.], [10., 10., 5.], [0., 0., 5.], [0., 10., 5.]]) # Grid w. steps 10 & height 5 self.echo_ratios = np.array([0., 0.9, 0.5, 0.8]) environment_parts = [ self._create_environment_part(t, ratio, self.radius) for t, ratio in zip(targets, self.echo_ratios) ] environment = np.vstack(environment_parts) self.target_pc = create_point_cloud(targets[:, 0], targets[:, 1], targets[:, 2]) self.environment_pc = create_point_cloud(environment[:, 0], environment[:, 1], environment[:, 2]) self.cylinder = InfiniteCylinder(self.radius) self.neighbors = list( compute_neighborhoods(self.environment_pc, self.target_pc, self.cylinder))
target = read_las.read(args.target) print(("Number of points: %s ") % (pc[point]['x']['data'].shape[0])) print(("Number of points in target: %s ") % (target[point]['x']['data'].shape[0])) print("------ Computing neighborhood is started ------") start = time.time() #compute_neighborhoods is now a generator. To get the result of a generator the user #needs to call next(compute_neighborhoods). The following shows how to get the results. # #indices_cyl=compute_neighborhoods(pc, target, InfiniteCylinder(np.float(args.radius))) # neighbors = compute_neighborhoods(pc, target, InfiniteCylinder(np.float(args.radius))) iteration = 0 target_idx_base = 0 fullstarttime = time.time() for x in neighbors: end = time.time() difftime = end - start print(("build kd-tree: %f sec") % (difftime)) print("Computed neighborhoods list length at iteration %d is: %d" % (iteration, len(x))) start1 = time.time() print("------ Feature calculation is started ------") compute_features(pc, x, target_idx_base, target,
def test_cube_no_points(self): point_cloud = create_emtpy_point_cloud() targets = create_point_cloud(np.zeros(1), np.zeros(1), np.zeros(1)) neighborhoods = list( compute_neighborhoods(point_cloud, targets, Cube(1))) assert_equal(len(neighborhoods[0]), 0)
def test_cylinder_index(self): """Compute the density for a cylinder given as index of source pc.""" neighborhoods = compute_neighborhoods(self.point_cloud, self.target_point_cloud, self.cyl) extractor = PointDensityFeatureExtractor() extractor.extract(self.point_cloud, neighborhoods, None, None, self.cyl)
print("------ Building kd-tree is started ------") # Import pc = read_las.read(args.input) print(("Number of points: %s ") % (pc[point]['x']['data'].shape[0])) # Neighborhood calculation start = time.time() #compute_neighborhoods is now a generator. To get the result of a generator the user #needs to call next(compute_neighborhoods). The following shows how to get the results. # #indices_cyl=compute_neighborhoods(pc, target, Sphere(args.radius)) # neighbors = compute_neighborhoods(pc, pc, Sphere(np.float(args.radius))) indices_cyl = [] for x in neighbors: print("Iteration %d" % num_iterations) indices_cyl += x num_iterations += 1 end = time.time() difftime = end - start print(("build kd-tree: %f sec") % (difftime)) print("Computed neighborhoods list length is: %d" % len(indices_cyl)) output = open(args.output, 'wb') pickle.dump(indices_cyl, output) output.close()
_CYLINDER = InfiniteCylinder(4) _PC_260807 = read_ply.read(os.path.join(_TEST_DATA_SOURCE, _TEST_FILE_NAME)) _PC_1000 = copy_point_cloud(_PC_260807, array_mask=(np.random.choice(range( len(_PC_260807[keys.point]['x']['data'])), size=1000, replace=False))) _PC_10 = copy_point_cloud(_PC_260807, array_mask=(np.random.choice(range( len(_PC_260807[keys.point]['x']['data'])), size=10, replace=False))) _1000_NEIGHBORHOODS_IN_260807 = next( compute_neighbors.compute_neighborhoods(_PC_260807, _PC_1000, _CYLINDER, sample_size=500)) _10_NEIGHBORHOODS_IN_260807 = next( compute_neighbors.compute_neighborhoods(_PC_260807, _PC_10, _CYLINDER, sample_size=500)) _260807_NEIGHBORHOODS_IN_10 = next( compute_neighbors.compute_neighborhoods(_PC_10, _PC_260807, _CYLINDER, sample_size=500)) features_by_name = create_default_feature_map() feature_names = [name for name in features_by_name]
target = read_las.read(args.target) print(("Number of points: %s ") % (pc[point]['x']['data'].shape[0])) print(("Number of points in target: %s ") % (target[point]['x']['data'].shape[0])) print("------ Computing neighborhood is started ------") start = time.time() #compute_neighborhoods is now a generator. To get the result of a generator the user #needs to call next(compute_neighborhoods). The following shows how to get the results. # #indices_cyl=compute_neighborhoods(pc, target, Cell(np.float(args.radius))) # neighbors = compute_neighborhoods(pc, target, Cell(np.float(args.radius))) iteration = 0 target_idx_base = 0 for x in neighbors: end = time.time() difftime = end - start print(("build kd-tree: %f sec") % (difftime)) print("Computed neighborhoods list length at iteration %d is: %d" % (iteration, len(x))) start1 = time.time() print("------ Feature calculation is started ------") compute_features(pc, x, target_idx_base, target, [ 'max_z', 'min_z', 'mean_z', 'median_z', 'std_z', 'var_z', 'coeff_var_z', 'skew_z', 'kurto_z', 'sigma_z', 'perc_20', 'perc_40', 'perc_60', 'perc_80', 'perc_90', 'pulse_penetration_ratio',
import numpy as np import pandas as pd import time # Import pc = read_las.read(args.input) # Neighborhood calculation start = time.time() #compute_neighborhoods is now a generator. To get the result of a generator the user #needs to call next(compute_neighborhoods). The following shows how to get the results. # #indices_cyl=compute_neighborhoods(pc, target, Sphere(2.5)) # neighbors = compute_neighborhoods(pc, pc, Sphere(2.5)) iteration = 0 target_idx_base = 0 for x in neighbors: end = time.time() difftime = end - start print(("build kd-tree: %f sec") % (difftime)) print("Computed neighborhoods list length at iteration %d is: %d" % (iteration, len(indices_cyl))) start1 = time.time() compute_features(pc, indices_cyl, target_idx_base, pc, [ 'max_z', 'min_z', 'mean_z', 'median_z', 'std_z', 'var_z', 'range', 'coeff_var_z', 'skew_z', 'kurto_z', 'eigenv_1', 'eigenv_2', 'eigenv_3', 'z_entropy', 'sigma_z', 'perc_20', 'perc_40', 'perc_60', 'perc_80' ], Sphere(2.5))