Skip to content

Commit

Permalink
WIP adding scale bars
Browse files Browse the repository at this point in the history
  • Loading branch information
niksirbi committed Jan 17, 2025
1 parent 1041aca commit 4039999
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
48 changes: 48 additions & 0 deletions brainglobe_template_builder/plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,8 @@ def plot_orthographic(
show_slices: list[int] | None = None,
pad_sizes: list[tuple[int, int]] | None = None,
mip_attenuation: float = 0.01,
scale_bar: bool = False,
resolution: float = 0.025,
save_path: Path | None = None,
) -> tuple[plt.Figure, np.ndarray]:
"""Plot orthographic views of a 3D image,
Expand Down Expand Up @@ -258,6 +260,29 @@ def plot_orthographic(
ax.axhline(slice_idxs[h], color="r", linestyle="--", alpha=0.5)
ax.axvline(slice_idxs[v], color="r", linestyle="--", alpha=0.5)

# Add scale bar equal to 1mm to each view
if scale_bar:
bar_length = 1 / resolution
bar_x = frames[j].shape[1] - bar_length - 20
bar_y = frames[j].shape[0] - 10
ax.plot(
[bar_x, bar_x + bar_length],
[bar_y, bar_y],
color="w",
linewidth=2,
clip_on=False,
)
# Add the text on if it's the top view
if j == 3:
ax.text(
bar_x + bar_length / 2,
bar_y - 5,
"1 mm",
color="w",
ha="center",
va="bottom",
)

ax = _clear_spines_and_ticks(ax)

fig.subplots_adjust(
Expand All @@ -275,6 +300,8 @@ def plot_inset_comparison(
y_min: int,
x_min: int,
size: int,
scale_bar: bool = False,
resolution: float = 0.025,
save_path: Path | None = None,
):
"""Plot the same inset from two images side by side.
Expand Down Expand Up @@ -325,6 +352,27 @@ def plot_inset_comparison(
ax.axis("off")
ax.set_title(name)

if scale_bar:
bar_length = 1 / resolution
bar_x = img_inset.shape[1] - bar_length - 5
bar_y = img_inset.shape[0] - 5
ax.plot(
[bar_x, bar_x + bar_length],
[bar_y, bar_y],
color="w",
linewidth=2,
clip_on=False,
)
if i == 1: # Add the text on the second image only
ax.text(
bar_x + bar_length / 2,
bar_y - 2.5,
"1 mm",
color="w",
ha="center",
va="bottom",
)

fig.tight_layout()
if save_path:
save_figure(fig, save_path.parent, save_path.name.split(".")[0])
Expand Down
8 changes: 7 additions & 1 deletion examples/plots/template_and_individual.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
# Load matplotlib parameters (to allow for proper font export)
plt.style.use(current_dir / "plots.mplstyle")
# Load config file containing template building parameters
config = load_config(current_dir / "config_25um.yaml")
config = load_config(current_dir / "config_50um.yaml")

# Setup directories based on config file
atlas_dir, template_dir, plots_dir = setup_directories(config)
Expand Down Expand Up @@ -101,6 +101,8 @@
show_slices=config["show_slices"],
pad_sizes=pad_sizes,
mip_attenuation=config["mip_attenuation"],
scale_bar=True,
resolution=config["resolution_um"] * 1e-3, # convert to mm
save_path=plots_dir / "final_template_orthographic",
)
print("Plotted final template in orthographic view")
Expand All @@ -116,6 +118,8 @@
show_slices=config["show_slices"],
pad_sizes=pad_sizes,
mip_attenuation=config["mip_attenuation"],
scale_bar=True,
resolution=config["resolution_um"] * 1e-3, # convert to mm
save_path=plots_dir / f"{example_subject}_orthographic",
)
print(f"Plotted {example_subject} in orthographic view")
Expand All @@ -129,5 +133,7 @@
img2=("template", template_img),
**inset_param,
save_path=plots_dir / plot_file_name,
scale_bar=True,
resolution=config["resolution_um"] * 1e-3, # convert to mm
)
print(f"Plotted inset comparison between {example_subject} and template")

0 comments on commit 4039999

Please sign in to comment.