Skip to content

Commit cab0c7f

Browse files
safesintesidaavoo
andauthored
FIX: slow read_off method (#353)
* FIXED `read_off` method from `off.py` ADDED comment parameter to skip comment rows and end of line comments CHANGED to c engine to increase drastically the performance ADDED n_rows and REMOVED skipfooter to use c engine ADDED dtype parameter to directly cast the element types SET points read_csv to off buffer: no skip of first lines needed * Deleted commented section * FIX off.py Fixed typo in parameters name Added n_points and n_faces default as 0 * REFACTOR changed deprecated jit to njit * FIX ValueError when using `read_obj` * Added throw ValueError on empty file * Update pyntcloud/io/obj.py --------- Co-authored-by: Edoardo Bortolozzo <edoardo.bortolozzo@studenti.unipd.it> Co-authored-by: David de la Iglesia Castro <daviddelaiglesiacastro@gmail.com>
1 parent a9fcae9 commit cab0c7f

File tree

3 files changed

+40
-20
lines changed

3 files changed

+40
-20
lines changed

pyntcloud/io/obj.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ def read_obj(filename):
7272
for i in range(sum(c.isdigit() for c in f[0].split(" "))):
7373
mesh_columns.append("v{}".format(i + 1))
7474

75-
mesh = pd.DataFrame([re.split(r'\D+', x) for x in f], dtype='i4', columns=mesh_columns).astype('i4')
75+
# trying to coerce type to integer throws error, casted afetr passes tests
76+
mesh = pd.DataFrame([re.split(r'\D+', x) for x in f], columns=mesh_columns).astype('i4')
7677
mesh -= 1 # index starts with 1 in obj file
7778

7879
data["mesh"] = mesh

pyntcloud/io/off.py

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@ def read_off(filename):
88

99
first_line = off.readline()
1010
if "OFF" not in first_line:
11-
raise ValueError('The file does not start whith the word OFF')
11+
raise ValueError('The file does not start with the word OFF')
1212
color = True if "C" in first_line else False
1313

14+
n_points = 0
15+
n_faces = 0
16+
1417
count = 1
1518
for line in off:
1619
count += 1
@@ -22,22 +25,38 @@ def read_off(filename):
2225
n_faces = int(line[1])
2326
break
2427

28+
if (n_points == 0):
29+
raise ValueError('The file has no points')
30+
2531
data = {}
2632
point_names = ["x", "y", "z"]
27-
if color:
28-
point_names.extend(["red", "green", "blue"])
29-
30-
data["points"] = pd.read_csv(filename, sep=" ", header=None, engine="python",
31-
skiprows=count, skipfooter=n_faces,
32-
names=point_names, index_col=False)
33-
for n in ["x", "y", "z"]:
34-
data["points"][n] = data["points"][n].astype(np.float32)
33+
point_types = {'x': np.float32, 'y': np.float32, 'z': np.float32}
3534

3635
if color:
37-
for n in ["red", "green", "blue"]:
38-
data["points"][n] = data["points"][n].astype(np.uint8)
39-
40-
data["mesh"] = pd.read_csv(filename, sep=" ", header=None, engine="python",
41-
skiprows=(count + n_points), usecols=[1, 2, 3],
42-
names=["v1", "v2", "v3"])
36+
point_names.extend(["red", "green", "blue"])
37+
point_types = dict(point_types, **{'red': np.uint8, 'green': np.uint8, 'blue': np.uint8})
38+
39+
data["points"] = pd.read_csv(
40+
off,
41+
sep=" ",
42+
header=None,
43+
engine="c",
44+
nrows=n_points,
45+
names=point_names,
46+
dtype=point_types,
47+
index_col=False,
48+
comment="#"
49+
)
50+
51+
data["mesh"] = pd.read_csv(
52+
filename,
53+
sep=" ",
54+
header=None,
55+
engine="c",
56+
skiprows=(count + n_points),
57+
nrows=n_faces,
58+
usecols=[1, 2, 3],
59+
names=["v1", "v2", "v3"],
60+
comment="#"
61+
)
4362
return data

pyntcloud/utils/numba.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
from numba import jit
1+
from numba import njit
22

33

4-
@jit
4+
@njit
55
def groupby_count(xyz, indices, out):
66
for i in range(xyz.shape[0]):
77
out[indices[i]] += 1
88
return out
99

1010

11-
@jit
11+
@njit
1212
def groupby_sum(xyz, indices, N, out):
1313
for i in range(xyz.shape[0]):
1414
out[indices[i]] += xyz[i][N]
1515
return out
1616

1717

18-
@jit
18+
@njit
1919
def groupby_max(xyz, indices, N, out):
2020
for i in range(xyz.shape[0]):
2121
if xyz[i][N] > out[indices[i]]:

0 commit comments

Comments
 (0)