Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Enhancement] Shapes attributes #32

Merged
merged 7 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 21 additions & 14 deletions src/lmd/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import multiprocessing as mp
import os
import platform
import re
import sys

# import warnings
Expand Down Expand Up @@ -552,30 +553,36 @@ def __init__(

def from_xml(self, root):
"""Load a shape from an XML shape node. Used internally for reading LMD generated XML files.

Args:
root: XML input node.
"""
self.name = root.tag

# get number of points
point_count = int(root.find("PointCount").text)
self.points = np.ones((point_count, 2), dtype=int)
point_count = int(root.find("PointCount").text)
points = np.empty((point_count, 2), dtype=int)

# compile regex
xpattern = re.compile("X_(\d+)")
ypattern = re.compile("Y_(\d+)")

# parse all points
for child in root:
if "_" in child.tag:
tokens = child.tag.split("_")

axes = tokens[0]
axes_id = 0 if axes == "X" else 1

point_id = int(tokens[-1]) - 1
value = int(child.text)

self.points[point_id, axes_id] = value

self.points = np.array(self.points)
xmatch = re.findall(xpattern, child.tag)
ymatch = re.findall(ypattern, child.tag)

if xmatch:
point_id = int(xmatch[0]) - 1
points[point_id, 0] = int(child.text)
elif ymatch:
point_id = int(ymatch[0]) - 1
points[point_id, 1] = int(child.text)
elif child.tag == "CapID":
self.well = str(child.text)

self.points = np.array(points)

def to_xml(self, id: int, orientation_transform: np.ndarray, scale: int):
"""Generate XML shape node needed internally for export.
Expand Down
28 changes: 27 additions & 1 deletion src/lmd/lmd_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import os
import geopandas as gpd
import shapely
from lxml import etree as ET

def test_collection():
calibration = np.array([[0, 0], [0, 100], [50, 50]])
Expand All @@ -15,7 +16,32 @@ def test_collection():
def test_shape():
rectangle_coordinates = np.array([[10,10], [40,10], [40,40], [10,40], [10,10]])
rectangle = Shape(rectangle_coordinates)


def test_shape_from_xml():
# Define shape in xml
shape_xml = """
<Shape_1>
<PointCount>3</PointCount>
<CapID>A1</CapID>
<TEST>this is a test</TEST>
<X_1>0</X_1>
<Y_1>-0</Y_1>
<X_2>0</X_2>
<Y_2>-1</Y_2>
<X_3>1</X_3>
<Y_3>-0</Y_3>
</Shape_1>
""".strip()

# Parse xml
shape_xml = ET.fromstring(bytes(shape_xml, encoding="utf-8"))

# Load xml with Shape
shape = Shape()
shape.from_xml(shape_xml)
assert (shape.points == np.array([[ 0, 0], [ 0, -1], [ 1, 0]])).all()
assert shape.well == "A1"

def test_plotting():
calibration = np.array([[0, 0], [0, 100], [50, 50]])
my_first_collection = Collection(calibration_points = calibration)
Expand Down
Loading