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

Add basic curvature #974

Merged
merged 84 commits into from
Nov 21, 2024
Merged
Changes from 1 commit
Commits
Show all changes
84 commits
Select commit Hold shift + click to select a range
9be0604
Add beginnings of curvature file
SylviaWhittle Sep 11, 2024
541a514
Add angle_diff_signed
SylviaWhittle Sep 11, 2024
7e7cfdd
Add test_angle_diff_signed
SylviaWhittle Sep 11, 2024
6569f94
Add discrete_angle_difference_per_nm_circular
SylviaWhittle Oct 1, 2024
138ab89
Fix normalisation and flipped subtractions in first and last index
SylviaWhittle Oct 1, 2024
7fa02ef
Add test for discrete_angle_difference_per_nm_circular
SylviaWhittle Oct 1, 2024
c5be319
Add find_curvature_defects_simple_threshold
SylviaWhittle Oct 2, 2024
bce731c
Add test for find_curvature_defects_simple_threshold
SylviaWhittle Oct 2, 2024
ee6c8af
Add calculate_trace_distances_to_last_points_circular
SylviaWhittle Oct 3, 2024
c262ad9
Add calculate_distances_between_distances_circular
SylviaWhittle Oct 3, 2024
9701b20
Add test_trace_distances_to_last_points_circular
SylviaWhittle Oct 3, 2024
de0c0b2
Add test_calculate_distances_between_defects_circular
SylviaWhittle Oct 3, 2024
6ee2bc5
Add calculate_number_of_defects
SylviaWhittle Oct 4, 2024
e175e87
add test_caclculate_number_of_defects
SylviaWhittle Oct 4, 2024
7cc6b0b
Add calculate_curvature_stats_single_grain_circular
SylviaWhittle Oct 4, 2024
f4cb94c
Add calculate_curvature_stats_image
SylviaWhittle Oct 4, 2024
f5013f8
Add plot_curvatures
SylviaWhittle Oct 4, 2024
f8734cd
Add plot_curvatures_individual_grains
SylviaWhittle Oct 4, 2024
39f3295
Add test_plot_curvature
SylviaWhittle Oct 4, 2024
6bcb570
Add run_curvature_stats
SylviaWhittle Oct 4, 2024
acf0c56
Fix test_angle_diff_signed
SylviaWhittle Oct 20, 2024
7326fad
Add test_calculate_curvature_stats_image
SylviaWhittle Oct 20, 2024
613b1cf
Disable defects functions and add linear version of angles_per_nm
SylviaWhittle Oct 20, 2024
a70a773
Fix plotting for updated curvature functions and remove defects funct…
SylviaWhittle Oct 20, 2024
01f0fce
Disable plotting test
SylviaWhittle Oct 20, 2024
ec0dabd
Integrate curvature stats into processing.py
SylviaWhittle Oct 20, 2024
cfb9cf6
Add curvature config option: run
SylviaWhittle Oct 20, 2024
beb263d
Add 'pad_width' to disordered tracing output for later grain alignment
SylviaWhittle Oct 20, 2024
8775c6e
Fix disordered tracing test given addition of 'pad_width'
SylviaWhittle Oct 20, 2024
311a6c0
Add curvature config to tests
SylviaWhittle Oct 20, 2024
cfb9074
Fix curvature tests and disable all relating to defects
SylviaWhittle Oct 20, 2024
76d60e4
Fix processing tests
SylviaWhittle Oct 20, 2024
b48bf08
[pre-commit.ci] Fixing issues with pre-commit
pre-commit-ci[bot] Oct 20, 2024
016782a
Make curvature plotting functions part of the Images class
SylviaWhittle Oct 22, 2024
1132050
Add whole image curvature plot and individual grain curvature plots f…
SylviaWhittle Oct 22, 2024
011c0c6
Add plots and config to validation
SylviaWhittle Oct 22, 2024
7ab8c91
Add test for plot_curvatures
SylviaWhittle Oct 23, 2024
d72d053
Add beginnings of curvature file
SylviaWhittle Sep 11, 2024
a963492
Add angle_diff_signed
SylviaWhittle Sep 11, 2024
90e8722
Add test_angle_diff_signed
SylviaWhittle Sep 11, 2024
9b15cb8
Add discrete_angle_difference_per_nm_circular
SylviaWhittle Oct 1, 2024
e702aea
Fix normalisation and flipped subtractions in first and last index
SylviaWhittle Oct 1, 2024
307a89b
Add test for discrete_angle_difference_per_nm_circular
SylviaWhittle Oct 1, 2024
e2356ab
Add find_curvature_defects_simple_threshold
SylviaWhittle Oct 2, 2024
2a76b75
Add test for find_curvature_defects_simple_threshold
SylviaWhittle Oct 2, 2024
05f22d7
Add calculate_trace_distances_to_last_points_circular
SylviaWhittle Oct 3, 2024
c9af571
Add calculate_distances_between_distances_circular
SylviaWhittle Oct 3, 2024
6dac959
Add test_trace_distances_to_last_points_circular
SylviaWhittle Oct 3, 2024
a71aeeb
Add test_calculate_distances_between_defects_circular
SylviaWhittle Oct 3, 2024
533ba7b
Add calculate_number_of_defects
SylviaWhittle Oct 4, 2024
ccf0acd
add test_caclculate_number_of_defects
SylviaWhittle Oct 4, 2024
b559441
Add calculate_curvature_stats_single_grain_circular
SylviaWhittle Oct 4, 2024
5525cd7
Add calculate_curvature_stats_image
SylviaWhittle Oct 4, 2024
ef895c5
Add plot_curvatures
SylviaWhittle Oct 4, 2024
f1fef84
Add plot_curvatures_individual_grains
SylviaWhittle Oct 4, 2024
5b6bb88
Add test_plot_curvature
SylviaWhittle Oct 4, 2024
378ca58
Add run_curvature_stats
SylviaWhittle Oct 4, 2024
7f5f389
Fix test_angle_diff_signed
SylviaWhittle Oct 20, 2024
0b1f0e1
Add test_calculate_curvature_stats_image
SylviaWhittle Oct 20, 2024
da3fff4
Disable defects functions and add linear version of angles_per_nm
SylviaWhittle Oct 20, 2024
7757dbd
Fix plotting for updated curvature functions and remove defects funct…
SylviaWhittle Oct 20, 2024
a9a647e
Disable plotting test
SylviaWhittle Oct 20, 2024
25e6847
Integrate curvature stats into processing.py
SylviaWhittle Oct 20, 2024
d4dc4c1
Add curvature config option: run
SylviaWhittle Oct 20, 2024
b982bde
Add 'pad_width' to disordered tracing output for later grain alignment
SylviaWhittle Oct 20, 2024
767ee71
Fix disordered tracing test given addition of 'pad_width'
SylviaWhittle Oct 20, 2024
f44992f
Add curvature config to tests
SylviaWhittle Oct 20, 2024
8b9e2b2
Fix curvature tests and disable all relating to defects
SylviaWhittle Oct 20, 2024
0dd6823
[pre-commit.ci] Fixing issues with pre-commit
pre-commit-ci[bot] Oct 20, 2024
3873a65
Make curvature plotting functions part of the Images class
SylviaWhittle Oct 22, 2024
0ed667e
Add whole image curvature plot and individual grain curvature plots f…
SylviaWhittle Oct 22, 2024
4d683aa
Add plots and config to validation
SylviaWhittle Oct 22, 2024
84acd11
Add test for plot_curvatures
SylviaWhittle Oct 23, 2024
0a78655
Fix: Formatting from rebase
SylviaWhittle Nov 7, 2024
a76b333
Fix tests: after rebase
SylviaWhittle Nov 7, 2024
9daab07
Merge remote-tracking branch 'origin/SylviaWhittle/curvature' into Sy…
SylviaWhittle Nov 7, 2024
3f42cae
[pre-commit.ci] Fixing issues with pre-commit
pre-commit-ci[bot] Nov 7, 2024
e4928b2
Merge main into curvature
SylviaWhittle Nov 18, 2024
e6ef86a
Remove: Add defect code
SylviaWhittle Nov 18, 2024
6dbfad1
Fix: test_process_scan_both: topostats file needed updating to contai…
SylviaWhittle Nov 19, 2024
1bd174d
Fix: test_calculate_curvature_stats_image: use absolute curvature values
SylviaWhittle Nov 19, 2024
695ac3c
Merge main into curvature
SylviaWhittle Nov 19, 2024
7d0bb3e
Fix: test_process_scan_both: Merge conflict
SylviaWhittle Nov 19, 2024
b368847
Add: test_process_scan_stages parametrisation for when curvature sta…
SylviaWhittle Nov 20, 2024
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
Prev Previous commit
Next Next commit
Fix plotting for updated curvature functions and remove defects funct…
…ionality
  • Loading branch information
SylviaWhittle committed Nov 7, 2024
commit 7757dbdffc2423a9f38bb5b775c7bd299abec1b7
120 changes: 66 additions & 54 deletions topostats/plottingfuncs.py
Original file line number Diff line number Diff line change
@@ -246,9 +246,7 @@ def __init__(
self.image_type = image_type
self.image_set = image_set
self.core_set = core_set
self.interpolation = (
mpl.rcParams["image.interpolation"] if pixel_interpolation is None else pixel_interpolation
)
self.interpolation = mpl.rcParams["image.interpolation"] if pixel_interpolation is None else pixel_interpolation
cmap = mpl.rcParams["image.cmap"] if cmap is None else cmap
self.cmap = Colormap(cmap).get_cmap()
self.mask_cmap = Colormap(mask_cmap).get_cmap()
@@ -460,8 +458,9 @@ def set_n_ticks(ax: plt.Axes.axes, n_xy: list[int | None, int | None]) -> None:

def plot_curvatures(
image: npt.NDArray,
cropped_images: dict,
grains_curvature_stats_dict: dict,
pixel_to_nm_scaling: float,
all_grain_smoothed_data: dict,
) -> None:
"""
Plot curvature intensity and defects of grains in an image.
@@ -470,37 +469,47 @@ def plot_curvatures(
----------
image : npt.NDArray
Image to plot.
cropped_images : dict
Dictionary containing cropped images of grains and the bounding boxes and padding.
grains_curvature_stats_dict : dict
Dictionary of grain curvature statistics.
pixel_to_nm_scaling : float
Pixel to nanometre scaling factor for the image.
all_grain_smoothed_data : dict
Dictionary containing smoothed grain traces.
"""
_, ax = plt.subplots(figsize=(10, 10))
ax.imshow(image)

# For each grain, plot the points with the colour determined by the curvature value
for grain_curvature_stats in grains_curvature_stats_dict.values():
grain_trace_px = grain_curvature_stats["grain_trace_nm"] / pixel_to_nm_scaling
grain_curvature = grain_curvature_stats["grain_curvature"]
curvature_defects_binary_array = grain_curvature_stats["curvature_defects_binary_array"]

# Plot the grain curvature by plotting lines between the points, with the colour determined by the curvature
for i, is_defect in enumerate(curvature_defects_binary_array):
if is_defect:
ax.plot(
grain_trace_px[i : i + 2, 1],
grain_trace_px[i : i + 2, 0],
color="red",
linewidth=2,
alpha=0.5,
)
else:
# Plot the line with the colour determined by the curvature, with the BrBG colourmap
# Iterate over the grains
for (_, grain_data_curvature), (_, grain_data_smoothed_trace), (_, grain_image_container) in zip(
grains_curvature_stats_dict.items(), all_grain_smoothed_data.items(), cropped_images.items()
):

# Get the coordinate for the grain to accurately position the points
min_row = grain_image_container["bbox"][0]
min_col = grain_image_container["bbox"][1]

pad_width = grain_image_container["pad_width"]

# Iterate over molecules
for (_, molecule_data_curvature), (
_,
molecule_data_smoothed_trace,
) in zip(grain_data_curvature.items(), grain_data_smoothed_trace.items()):

molecule_trace_coords = molecule_data_smoothed_trace["spline_coords"]
norm = plt.Normalize(min(molecule_data_curvature), max(molecule_data_curvature))
# pylint cannot see that mpl.cm.viridis is a valid attribute
# pylint: disable=no-member
cmap = mpl.cm.viridis
for curvature, point in zip(molecule_data_curvature, molecule_trace_coords):
color = cmap(norm(curvature))
ax.plot(
grain_trace_px[i : i + 2, 1],
grain_trace_px[i : i + 2, 0],
color=mpl.cm.BrBG(grain_curvature[i], alpha=0.5),
linewidth=2,
min_col - pad_width + point[1],
min_row - pad_width + point[0],
marker=".",
markersize=5,
color=color,
)

# save the figure
@@ -511,7 +520,7 @@ def plot_curvatures(
def plot_curvatures_individual_grains(
cropped_images: dict,
grains_curvature_stats_dict: dict,
pixel_to_nm_scaling: float,
all_grains_smoothed_data: dict,
) -> None:
"""
Plot curvature intensity and defects of individual grains.
@@ -522,34 +531,37 @@ def plot_curvatures_individual_grains(
Dictionary of cropped images.
grains_curvature_stats_dict : dict
Dictionary of grain curvature statistics.
pixel_to_nm_scaling : float
Pixel to nanometre scaling factor for the image.
all_grains_smoothed_data : dict
Dictionary containing smoothed grain traces.
"""
for grain_index in grains_curvature_stats_dict.keys():
fig, ax = plt.subplots(figsize=(10, 10))
ax.imshow(cropped_images[grain_index])
grain_curvature_stats = grains_curvature_stats_dict[grain_index]
grain_trace_px = grain_curvature_stats["grain_trace_nm"] / pixel_to_nm_scaling
grain_curvature = grain_curvature_stats["grain_curvature"]
curvature_defects_binary_array = grain_curvature_stats["curvature_defects_binary_array"]

# Plot the grain curvature by plotting lines between the points, with the colour determined by the curvature
for i, is_defect in enumerate(curvature_defects_binary_array):
if is_defect:
ax.plot(
grain_trace_px[i : i + 2, 1],
grain_trace_px[i : i + 2, 0],
color="red",
linewidth=2,
alpha=0.5,
)
else:
# Plot the line with the colour determined by the curvature, with the BrBG colourmap
# Iterate over grains
for (grain_index, grain_data_curvature), (_, grain_data_smoothed_trace), (_, grain_image_container) in zip(
grains_curvature_stats_dict.items(), all_grains_smoothed_data.items(), cropped_images.items()
):

grain_image = grain_image_container["original_image"]
_, ax = plt.subplots(figsize=(10, 10))
ax.imshow(grain_image)

# Iterate over molecules
for (_, molecule_data_curvature), (_, molecule_data_smoothed_trace) in zip(
grain_data_curvature.items(), grain_data_smoothed_trace.items()
):

molecule_trace_coords = molecule_data_smoothed_trace["spline_coords"]
norm = plt.Normalize(min(molecule_data_curvature), max(molecule_data_curvature))
# pylint cannot see that mpl.cm.viridis is a valid attribute
# pylint: disable=no-member
cmap = mpl.cm.viridis

for curvature, point in zip(molecule_data_curvature, molecule_trace_coords):
color = cmap(norm(curvature))
ax.plot(
grain_trace_px[i : i + 2, 1],
grain_trace_px[i : i + 2, 0],
color=mpl.cm.BrBG(grain_curvature[i], alpha=0.5),
linewidth=2,
point[1],
point[0],
marker=".",
markersize=5,
color=color,
)

# Save the figure