Skip to content

Commit 5f4cd36

Browse files
author
FelixAbrahamsson
committed
refactor: rename read functions and input positions instead of dataframe
1 parent 5cf0488 commit 5f4cd36

File tree

7 files changed

+53
-70
lines changed

7 files changed

+53
-70
lines changed

kmm/positions/__init__.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
from kmm.positions.kmm import kmm
2-
from kmm.positions.kmm2 import kmm2
3-
from kmm.positions.read_raw import read_raw
1+
from kmm.positions.read_kmm import read_kmm
2+
from kmm.positions.read_kmm2 import read_kmm2
43
from kmm.positions.geodetic import geodetic
54
from kmm.positions.wire_camera_positions import wire_camera_positions
65
from kmm.positions.positions import Positions

kmm/positions/geodetic.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,28 @@
1-
import pandas as pd
21
from sweref99 import projections
32

3+
from kmm.positions.positions import Positions
4+
45

56
tm = projections.make_transverse_mercator("SWEREF_99_TM")
67

78

8-
def geodetic(positions: pd.DataFrame):
9-
if len(positions) == 0:
10-
positions = positions.assign(longitude=[], latitude=[])
9+
def geodetic(positions: Positions):
10+
dataframe = positions.dataframe
11+
if len(dataframe) == 0:
12+
dataframe = dataframe.assign(longitude=[], latitude=[])
1113
else:
12-
latitude, longitude = zip(*positions.apply(
14+
latitude, longitude = zip(*dataframe.apply(
1315
lambda row: (tm.grid_to_geodetic(row["sweref99_tm_x"], row["sweref99_tm_y"])),
1416
axis="columns",
1517
result_type="reduce",
1618
))
17-
positions = positions.assign(longitude=longitude, latitude=latitude)
18-
return positions
19+
dataframe = dataframe.assign(longitude=longitude, latitude=latitude)
20+
return positions.replace(dataframe=dataframe)
1921

2022

2123
def test_geodetic():
22-
from kmm.positions.kmm2 import kmm2
2324

24-
df = kmm2("tests/ascending_B.kmm2")
25-
df = geodetic(df)
25+
positions = Positions.from_path("tests/ascending_B.kmm2")
26+
df = geodetic(positions).dataframe
2627
assert ((df["latitude"] < 68) & (df["latitude"] > 55)).all()
2728
assert ((df["longitude"] < 25) & (df["longitude"] > 7)).all()

kmm/positions/positions.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,14 @@ def from_path(path: Path):
1818
"""
1919
Loads positions from .kmm or .kmm2 file.
2020
"""
21-
return Positions(dataframe=kmm.positions.read_raw(path))
21+
if path.suffix == ".kmm":
22+
dataframe = kmm.positions.read_kmm(path)
23+
elif path.suffix == ".kmm2":
24+
dataframe = kmm.positions.read_kmm2(path)
25+
else:
26+
raise ValueError(f"Unable to parse file type {path.suffix}")
27+
28+
return Positions(dataframe=dataframe)
2229

2330
@staticmethod
2431
@validate_arguments
@@ -30,7 +37,6 @@ def read_sync_adjust(
3037
"""
3138
Loads positions from .kmm or .kmm2 file + .hdr file, then performs
3239
frame index sync, position adjustment and geodetic coordinate transformation.
33-
.
3440
"""
3541
header = kmm.Header.from_path(header_path)
3642
return (
@@ -55,13 +61,12 @@ def sync_frame_index(self, header):
5561

5662
def adjust(self, adjustment: kmm.PositionAdjustment, header):
5763
if adjustment == kmm.PositionAdjustment.WIRE_CAMERA:
58-
dataframe = kmm.positions.wire_camera_positions(self.dataframe, header.car_direction)
64+
return kmm.positions.wire_camera_positions(self, header.car_direction)
5965
else:
6066
raise ValueError(f"Unknown adjustment option {adjustment}")
61-
return self.replace(dataframe=dataframe)
6267

6368
def geodetic(self):
64-
return self.replace(dataframe=kmm.positions.geodetic(self.dataframe))
69+
return kmm.positions.geodetic(self)
6570

6671

6772
def test_read_kmm():

kmm/positions/kmm.py renamed to kmm/positions/read_kmm.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66

77
@validate_arguments
8-
def kmm(path: Path):
8+
def read_kmm(path: Path):
99
return pd.read_csv(
1010
path,
1111
sep="\t",

kmm/positions/kmm2.py renamed to kmm/positions/read_kmm2.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111

1212
@validate_arguments
13-
def kmm2(path: Path):
13+
def read_kmm2(path: Path):
1414

1515
skiprows = [
1616
index

kmm/positions/read_raw.py

Lines changed: 0 additions & 30 deletions
This file was deleted.
Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import pandas as pd
21
from pydantic import validate_arguments
32

43
from kmm import CarDirection
4+
from kmm.positions.positions import Positions
55

66

77
@validate_arguments(config=dict(arbitrary_types_allowed=True))
8-
def wire_camera_positions(positions: pd.DataFrame, direction: CarDirection):
9-
ascending = kmm_ascending(positions)
8+
def wire_camera_positions(positions: Positions, direction: CarDirection):
9+
ascending = kmm_ascending(positions.dataframe)
1010

1111
if (
1212
(direction == CarDirection.A and ascending)
@@ -23,14 +23,16 @@ def wire_camera_positions(positions: pd.DataFrame, direction: CarDirection):
2323
else:
2424
raise ValueError
2525

26-
return (
27-
positions
28-
.assign(meter=lambda df: df["meter"] + correction)
26+
return positions.replace(
27+
dataframe=(
28+
positions.dataframe
29+
.assign(meter=lambda df: df["meter"] + correction)
30+
)
2931
)
3032

3133

32-
def kmm_ascending(positions):
33-
total_meter = positions["kilometer"] * 1000 + positions["meter"]
34+
def kmm_ascending(dataframe):
35+
total_meter = dataframe["kilometer"] * 1000 + dataframe["meter"]
3436
diff = total_meter.values[:-1] - total_meter.values[1:]
3537
descending = (diff < 0).mean()
3638
ascending = (diff > 0).mean()
@@ -43,18 +45,24 @@ def kmm_ascending(positions):
4345

4446

4547
def test_camera_positions_kmm():
46-
import kmm
47-
48-
df = kmm.positions.kmm("tests/ascending_B.kmm")
49-
header = kmm.Header.from_path("tests/ascending_B.hdr")
50-
df_calibrated = wire_camera_positions(df, header.car_direction)
51-
assert df_calibrated["meter"].iloc[0] == df["meter"].iloc[0] - 8
48+
from kmm import Header
49+
50+
positions = Positions.from_path("tests/ascending_B.kmm")
51+
header = Header.from_path("tests/ascending_B.hdr")
52+
positions_calibrated = wire_camera_positions(positions, header.car_direction)
53+
assert (
54+
positions_calibrated.dataframe["meter"].iloc[0]
55+
== positions.dataframe["meter"].iloc[0] - 8
56+
)
5257

5358

5459
def test_camera_positions_kmm2():
55-
import kmm
56-
57-
df = kmm.positions.kmm2("tests/ascending_B.kmm2")
58-
header = kmm.Header.from_path("tests/ascending_B.hdr")
59-
df_calibrated = wire_camera_positions(df, header.car_direction)
60-
assert df_calibrated["meter"].iloc[0] == df["meter"].iloc[0] - 8
60+
from kmm import Header
61+
62+
positions = Positions.from_path("tests/ascending_B.kmm2")
63+
header = Header.from_path("tests/ascending_B.hdr")
64+
positions_calibrated = wire_camera_positions(positions, header.car_direction)
65+
assert (
66+
positions_calibrated.dataframe["meter"].iloc[0]
67+
== positions.dataframe["meter"].iloc[0] - 8
68+
)

0 commit comments

Comments
 (0)