Skip to content

Commit

Permalink
Merge pull request #79 from zenWai/fix-regions-2d-heatmap
Browse files Browse the repository at this point in the history
Resolve polygon overlay issues 2D
  • Loading branch information
alessandrofelder authored Feb 19, 2025
2 parents a1380ab + c4eefbc commit 8077ca8
Showing 1 changed file with 28 additions and 3 deletions.
31 changes: 28 additions & 3 deletions brainglobe_heatmap/heatmaps.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Dict, Optional, Tuple, Union
from typing import Dict, List, Optional, Tuple, Union

import matplotlib as mpl
import matplotlib.pyplot as plt
Expand Down Expand Up @@ -353,13 +353,38 @@ def plot_subplot(
self.regions_meshes, self.scene.root
)

segments: List[Dict[str, Union[str, np.ndarray, float]]] = []
for r, coords in projected.items():
name, segment = r.split("_segment_")
name, segment_nr = r.split("_segment_")
x = coords[:, 0]
y = coords[:, 1]
# calculate area of polygon with Shoelace formula
area = 0.5 * np.abs(
np.dot(x, np.roll(y, 1)) - np.dot(y, np.roll(x, 1))
)

segments.append(
{
"name": name,
"segment_nr": segment_nr,
"coords": coords,
"area": area,
}
)

# Sort region segments by area (largest first)
segments.sort(key=lambda s: s["area"], reverse=True)

for segment in segments:
name = segment["name"]
segment_nr = segment["segment_nr"]
coords = segment["coords"]

ax.fill(
coords[:, 0],
coords[:, 1],
color=self.colors[name],
label=name if segment == "0" and name != "root" else None,
label=name if segment_nr == "0" and name != "root" else None,
lw=1,
ec="k",
zorder=-1 if name == "root" else None,
Expand Down

0 comments on commit 8077ca8

Please sign in to comment.