Skip to content

Commit 2b1752e

Browse files
fix: unnecessary skipping of lines in kmm
1 parent bb1185b commit 2b1752e

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

kmm/positions/positions.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,19 @@ class Config:
1515

1616
@staticmethod
1717
@validate_arguments
18-
def from_path(path: Path):
18+
def from_path(
19+
path: Path,
20+
raise_on_malformed_data: bool = True,
21+
):
1922
"""
2023
Loads positions from .kmm or .kmm2 file.
2124
"""
2225
if path.suffix == ".kmm":
2326
dataframe = kmm.positions.read_kmm(path)
2427
elif path.suffix == ".kmm2":
25-
dataframe = kmm.positions.read_kmm2(path)
28+
dataframe = kmm.positions.read_kmm2(
29+
path, raise_on_malformed_data=raise_on_malformed_data
30+
)
2631
else:
2732
raise ValueError(f"Unable to parse file type {path.suffix}")
2833

@@ -42,7 +47,7 @@ def read_sync_adjust(
4247
"""
4348
header = kmm.Header.from_path(header_path, raise_on_malformed_data)
4449
return (
45-
Positions.from_path(kmm_path)
50+
Positions.from_path(kmm_path, raise_on_malformed_data)
4651
.sync_frame_index(header, adjustment, raise_on_malformed_data)
4752
.geodetic()
4853
)

kmm/positions/read_kmm2.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,21 +46,27 @@
4646

4747

4848
@validate_arguments
49-
def read_kmm2(path: Path):
49+
def read_kmm2(path: Path, raise_on_malformed_data: bool = True):
5050
skiprows = [
5151
index
5252
for index, line in enumerate(path.read_text(encoding="latin1").splitlines())
5353
if pattern.match(line) or pattern2.match(line)
5454
]
55-
55+
with open(path, "r", encoding="latin1") as f:
56+
line = f.readline()
57+
if line.startswith("VER"):
58+
skiprows = [0] + skiprows
59+
elif raise_on_malformed_data and not line.startswith("POS"):
60+
raise ValueError("Malformed data, first line is not POS or VER")
5661
try:
5762
try:
5863
df = pd.read_csv(
5964
path,
60-
skiprows=[0] + skiprows,
65+
skiprows=skiprows,
6166
delimiter="\t",
6267
encoding="latin1",
6368
low_memory=False,
69+
header=None,
6470
)
6571
except pd.errors.EmptyDataError:
6672
return pd.DataFrame(columns=expected_columns)

0 commit comments

Comments
 (0)