Skip to content

Commit 37b1bca

Browse files
improve: handle comma formatted kmms by default
1 parent 2b1752e commit 37b1bca

File tree

3 files changed

+28
-8
lines changed

3 files changed

+28
-8
lines changed

kmm/positions/positions.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class Config:
1818
def from_path(
1919
path: Path,
2020
raise_on_malformed_data: bool = True,
21+
replace_commas: bool=True,
2122
):
2223
"""
2324
Loads positions from .kmm or .kmm2 file.
@@ -26,7 +27,7 @@ def from_path(
2627
dataframe = kmm.positions.read_kmm(path)
2728
elif path.suffix == ".kmm2":
2829
dataframe = kmm.positions.read_kmm2(
29-
path, raise_on_malformed_data=raise_on_malformed_data
30+
path, raise_on_malformed_data=raise_on_malformed_data, replace_commas=replace_commas
3031
)
3132
else:
3233
raise ValueError(f"Unable to parse file type {path.suffix}")
@@ -40,14 +41,15 @@ def read_sync_adjust(
4041
header_path: Path,
4142
adjustment: kmm.PositionAdjustment = kmm.PositionAdjustment.WIRE_CAMERA,
4243
raise_on_malformed_data: bool = True,
44+
replace_commas: bool=True,
4345
):
4446
"""
4547
Loads positions from .kmm or .kmm2 file + .hdr file, then performs
4648
frame index sync, position adjustment and geodetic coordinate transformation.
4749
"""
4850
header = kmm.Header.from_path(header_path, raise_on_malformed_data)
4951
return (
50-
Positions.from_path(kmm_path, raise_on_malformed_data)
52+
Positions.from_path(kmm_path, raise_on_malformed_data=raise_on_malformed_data, replace_commas=replace_commas)
5153
.sync_frame_index(header, adjustment, raise_on_malformed_data)
5254
.geodetic()
5355
)

kmm/positions/read_kmm.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from io import StringIO
12
from pathlib import Path
23

34
import numpy as np
@@ -6,12 +7,19 @@
67

78

89
@validate_arguments
9-
def read_kmm(path: Path):
10+
def read_kmm(path: Path, replace_commas: bool=True):
1011
try:
12+
if replace_commas:
13+
with open(path, "r", encoding="latin1") as f:
14+
content = f.read()
15+
content = content.replace(",", ".")
16+
file_obj = StringIO(content)
17+
else:
18+
file_obj = path
1119
return pd.read_csv(
12-
path,
20+
file_obj,
1321
sep="\t",
14-
encoding="latin1",
22+
encoding="latin1" if not replace_commas else None,
1523
names=[
1624
"centimeter",
1725
"track_section",

kmm/positions/read_kmm2.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import re
2+
from io import StringIO
23
from pathlib import Path
34

45
import numpy as np
@@ -46,7 +47,7 @@
4647

4748

4849
@validate_arguments
49-
def read_kmm2(path: Path, raise_on_malformed_data: bool = True):
50+
def read_kmm2(path: Path, raise_on_malformed_data: bool = True, replace_commas: bool=True):
5051
skiprows = [
5152
index
5253
for index, line in enumerate(path.read_text(encoding="latin1").splitlines())
@@ -58,13 +59,22 @@ def read_kmm2(path: Path, raise_on_malformed_data: bool = True):
5859
skiprows = [0] + skiprows
5960
elif raise_on_malformed_data and not line.startswith("POS"):
6061
raise ValueError("Malformed data, first line is not POS or VER")
62+
6163
try:
64+
if replace_commas:
65+
with open(path, "r", encoding="latin1") as f:
66+
content = f.read()
67+
content = content.replace(",", ".")
68+
file_obj = StringIO(content)
69+
else:
70+
file_obj = path
71+
6272
try:
6373
df = pd.read_csv(
64-
path,
74+
file_obj,
6575
skiprows=skiprows,
6676
delimiter="\t",
67-
encoding="latin1",
77+
encoding="latin1" if not replace_commas else None,
6878
low_memory=False,
6979
header=None,
7080
)

0 commit comments

Comments
 (0)