Skip to content

Commit 9d2a3fb

Browse files
committed
perf: read_ply replace pandas.read_csv engine=python with c
1 parent e968956 commit 9d2a3fb

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

pyntcloud/io/ply.py

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
import numpy as np
55
import pandas as pd
66
from collections import defaultdict
7+
from contextlib import contextmanager
8+
from io import StringIO
9+
from itertools import islice
10+
711

812
sys_byteorder = ('>', '<')[sys.byteorder == 'little']
913

@@ -132,8 +136,13 @@ def read_ply(filename, allow_bool=False):
132136

133137
names = [x[0] for x in dtypes["vertex"]]
134138

135-
data["points"] = pd.read_csv(filename, sep=" ", header=None, engine="python",
136-
skiprows=top, skipfooter=bottom, usecols=names, names=names)
139+
with open(filename, 'r') as f:
140+
lines = f.readlines()
141+
142+
with _file_from_lines(lines, top, len(lines) - bottom) as f:
143+
data["points"] = pd.read_csv(
144+
f, sep=" ", header=None, usecols=names, names=names
145+
)
137146

138147
for n, col in enumerate(data["points"].columns):
139148
data["points"][col] = data["points"][col].astype(
@@ -146,8 +155,10 @@ def read_ply(filename, allow_bool=False):
146155
usecols = [1, 2, 3, 5, 6, 7, 8, 9, 10] if has_texture else [1, 2, 3]
147156
names = names[usecols]
148157

149-
data["mesh"] = pd.read_csv(
150-
filename, sep=" ", header=None, engine="python", skiprows=top, usecols=usecols, names=names)
158+
with _file_from_lines(lines, top) as f:
159+
data["mesh"] = pd.read_csv(
160+
f, sep=" ", header=None, usecols=usecols, names=names
161+
)
151162

152163
for n, col in enumerate(data["mesh"].columns):
153164
data["mesh"][col] = data["mesh"][col].astype(
@@ -261,3 +272,11 @@ def describe_element(name, df):
261272
element.append('property ' + f + ' ' + df.columns.values[i])
262273

263274
return element
275+
276+
277+
@contextmanager
278+
def _file_from_lines(lines, start=None, stop=None):
279+
with StringIO() as f:
280+
f.writelines("".join(islice(lines, start, stop)))
281+
f.seek(0)
282+
yield f

0 commit comments

Comments
 (0)