Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_voxelgrid_sampling_return_type(simple_pyntcloud, sampling_method):
voxelgrid_id = simple_pyntcloud.add_structure("voxelgrid")
sample = simple_pyntcloud.get_sample(
sampling_method,
voxelgrid_id=voxelgrid_id)
assert type(sample) == DataFrame
sample = simple_pyntcloud.get_sample(
sampling_method,
voxelgrid_id=voxelgrid_id,
as_PyntCloud=True)
assert type(sample) == PyntCloud
def test_regular_bounding_box_changes_the_shape_of_the_bounding_box(x, y, z):
cloud = PyntCloud(pd.DataFrame(
data={
"x": np.array(x, dtype=np.float32),
"y": np.array(y, dtype=np.float32),
"z": np.array(z, dtype=np.float32)
}))
voxelgrid = VoxelGrid(points=cloud.xyz, n_x=2, n_y=2, n_z=2, regular_bounding_box=False)
voxelgrid.compute()
irregular_last_centroid = voxelgrid.voxel_centers[-1]
voxelgrid = VoxelGrid(points=cloud.xyz, n_x=2, n_y=2, n_z=2)
voxelgrid.compute()
regular_last_centroid = voxelgrid.voxel_centers[-1]
def test_to_file(tmpdir, diamond, extension, color, mesh):
extra_write_args = {}
if mesh:
extra_write_args["also_save"] = ["mesh"]
if extension == ".ply":
extra_write_args["as_text"] = False
if extension == "_ascii.ply":
extra_write_args["as_text"] = True
diamond.to_file(str(tmpdir.join("written{}".format(extension))), **extra_write_args)
written_file = PyntCloud.from_file(str(tmpdir.join("written{}".format(extension))))
assert_points_xyz(written_file)
if color:
assert_points_color(written_file)
if mesh:
assert_mesh(written_file)
def test_voxelgrid_sampling():
cloud = PyntCloud.from_file(path + "/data/voxelgrid.ply")
with pytest.raises(TypeError):
cloud.get_sample("voxelgrid_centers")
vg_id = cloud.add_structure("voxelgrid")
with pytest.raises(KeyError):
cloud.get_sample("voxelgrid_centers", voxelgrid=vg_id[:-2])
sample = cloud.get_sample("voxelgrid_centers", voxelgrid=vg_id)
assert point_in_array_2D([0.25, 0.25, 0.25], sample.values)
sample = cloud.get_sample("voxelgrid_centroids", voxelgrid=vg_id)
assert point_in_array_2D([0.2, 0.2, 0.2], sample.values)
import os
import pytest
from pyntcloud import PyntCloud
path = os.path.abspath(os.path.dirname(__file__))
cloud = PyntCloud.from_file(path + "/data/test_data_filters.ply")
def test_kdtree_filters():
"""filters.f_kdtree.
- Raise TypeError when missing required arguments
- Raise KeyError when structure.id is invalid
- Raise TypeError when wrong argument is given (k instead of r)
- Manually check known result.
"""
with pytest.raises(TypeError):
cloud.get_filter("ROR")
kdtree = cloud.add_structure("kdtree")
# not dataframe
with pytest.raises(TypeError):
PyntCloud(points)
points = pd.DataFrame(points)
# not x, y, z
with pytest.raises(ValueError):
PyntCloud(points)
points = pd.DataFrame(points.values, columns=["x", "y", "z"])
assert PyntCloud(points)
cloud = PyntCloud(points)
cloud.add_structure("voxelgrid")
assert len(cloud.structures) == 1
# dummy filter
x_above_05 = cloud.points["x"] > 0.5
cloud.points = cloud.points[x_above_05]
assert len(cloud.structures) == 0
def ply2gii(in_file, metadata, out_file=None):
"""Convert from ply to GIfTI"""
from pathlib import Path
from numpy import eye
from nibabel.gifti import (
GiftiMetaData,
GiftiCoordSystem,
GiftiImage,
GiftiDataArray,
)
from pyntcloud import PyntCloud
in_file = Path(in_file)
surf = PyntCloud.from_file(str(in_file))
# Update centroid metadata
metadata.update(
zip(
("SurfaceCenterX", "SurfaceCenterY", "SurfaceCenterZ"),
["%.4f" % c for c in surf.centroid],
)
)
# Prepare data arrays
da = (
GiftiDataArray(
data=surf.xyz.astype("float32"),
datatype="NIFTI_TYPE_FLOAT32",
intent="NIFTI_INTENT_POINTSET",
meta=GiftiMetaData.from_dict(metadata),
def write_df(path, df):
pc = PyntCloud(df)
pc.to_file(path)
def pointcloud2ply(vertices, normals, out_file=None):
"""Converts the file to PLY format"""
from pathlib import Path
import pandas as pd
from pyntcloud import PyntCloud
df = pd.DataFrame(np.hstack((vertices, normals)))
df.columns = ["x", "y", "z", "nx", "ny", "nz"]
cloud = PyntCloud(df)
if out_file is None:
out_file = Path("pointcloud.ply").resolve()
cloud.to_file(str(out_file))
return out_file