Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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
49 changes: 36 additions & 13 deletions mpldxf/backend_dxf.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,19 @@ def _clip_mpl(self, gc, vertices, obj):
line = LineString(vertices)
try:
intersection = line.intersection(cliprect)
except:
intersection = Polygon()
except Exception:
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we catch a more specific Exception here? Generally, it is considered bad practice to have try/except without specifying the exact exception you are expecting.

# Fix plotted values with large gaps/jumps crashing the intersection function
gap_threshold = 50
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it's a good idea to use a hard coded value here. If we have plots ranging between 0 and 2000, for example the pore pressure in a CPT, a gap threshold of 50 is quite small.

smoothed_vertices = [vertices[0]] # Start with the first point
for i in range(1, len(vertices)):
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use np.diff to compute this more effectively, and filter using np.where.

if (
abs(vertices[i][0] - vertices[i - 1][0]) <= gap_threshold
and abs(vertices[i][1] - vertices[i - 1][1])
<= gap_threshold
):
smoothed_vertices.append(vertices[i])
line = LineString(smoothed_vertices)
intersection = line.intersection(cliprect)

# Check if intersection is a multi-part geometry
if intersection.is_empty:
Expand Down Expand Up @@ -323,17 +334,15 @@ def draw_path_collection(
urls,
offset_position,
):
if self._groupd[-1] == "PolyCollection":
# Behandle PolyCollection som en samling av 'patch'-objekter
for path in paths:
# Kombiner master_transform med path_transform for hver path
for i, path in enumerate(paths):
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Has this been reverted to an old version of the code?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the reason for this change?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I tried to figure out where the failure happend, I tried some different approaches and that we didnt handle all_transforms was one thing I was looking into.
I just revert it for now, even though I think the new code is working asmit should as well :)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I figured out it actually fixed a bug as well!
I completely forgot, but some lines from the simple sounding plot was not plotted before this change.
So I will actually leave it as it is here :)

if all_transforms:
combined_transform = master_transform + all_transforms[i]
else:
combined_transform = master_transform
# Her kan du velge å bruke eller tilpasse rgbFace basert på facecolors, hvis det er relevant
if facecolors.size:
rgbFace = facecolors[0] if facecolors is not None else None
else:
rgbFace = None
self._draw_mpl_patch(gc, path, combined_transform, rgbFace)
facecolor = facecolors[i] if i < len(facecolors) else None
edgecolor = edgecolors[i] if i < len(edgecolors) else None
# Draw each path as a filled patch
self._draw_mpl_patch(gc, path, combined_transform, rgbFace=facecolor)

def draw_path(self, gc, path, transform, rgbFace=None):
# print('\nEntered ###DRAW_PATH###')
Expand All @@ -352,7 +361,6 @@ def draw_path(self, gc, path, transform, rgbFace=None):
elif self._groupd[-1] == "line2d":
line = self._draw_mpl_line2d(gc, path, transform)

# Note if this is used then tick marks and lines with markers go through this function
def draw_markers(self, gc, marker_path, marker_trans, path, trans, rgbFace=None):
# print('\nEntered ###DRAW_MARKERS###')
# print('\t', self._groupd)
Expand Down Expand Up @@ -479,6 +487,21 @@ def new_gc(self):
def points_to_pixels(self, points):
return points / 72.0 * self.dpi

def draw_quad_mesh(
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the reason for this change? Do we need it?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I tried to figure out where the failure happend, I tried some different approaches and figured out that we didnt handle quad mesh, so I set up some of the entities we didnt handle to see what wass passed to them.
Thought we just could keep them to be aware of them and implement them if necessary at a later stage.
But I will just remove it for now, since not in use.

self,
gc,
master_transform,
meshWidth,
meshHeight,
coordinates,
offsets,
offsetTrans,
facecolors,
antialiased,
edgecolors,
):
pass


class FigureCanvasDxf(FigureCanvasBase):
"""
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "mpldxf"
version = "0.3.0"
version = "0.4.0"
description = "A fork of mpldxf - a matplotlib backend to write DXF drawings. Modified by NGI to handle geotechnical plots"
authors = ["David Kent",
"Jon-Michael Josefsen <jmj@ngi.no>",
Expand Down