From 4a3e4585c4e41982b8adb859960ae998d11d10d5 Mon Sep 17 00:00:00 2001
From: polarbean <harry.carey95@gmail.com>
Date: Fri, 14 Mar 2025 16:43:40 +0100
Subject: [PATCH] measure damaged and undamaged region areas

---
 PyNutil/processing/coordinate_extraction.py    |   2 +-
 PyNutil/processing/counting_and_load.py        |  12 +++++++++++-
 .../segmentations/test_s003.png                | Bin 120 -> 6768 bytes
 .../segmentations/test_s003_.png               | Bin 6768 -> 0 bytes
 4 files changed, 12 insertions(+), 2 deletions(-)
 delete mode 100644 tests/test_data/nonlinear_allen_mouse/segmentations/test_s003_.png

diff --git a/PyNutil/processing/coordinate_extraction.py b/PyNutil/processing/coordinate_extraction.py
index 38c291f..37e5358 100644
--- a/PyNutil/processing/coordinate_extraction.py
+++ b/PyNutil/processing/coordinate_extraction.py
@@ -389,7 +389,7 @@ def get_region_areas(
     atlas_map = load_image(flat_file_atlas,slice_dict["anchoring"], atlas_volume, triangulation, (seg_width, seg_height), atlas_labels)
 
     region_areas = flat_to_dataframe(
-        atlas_map, (seg_width, seg_height)
+        atlas_map, damage_mask, (seg_width, seg_height)
     )
     return region_areas, atlas_map
 
diff --git a/PyNutil/processing/counting_and_load.py b/PyNutil/processing/counting_and_load.py
index f15ff36..c09dc67 100644
--- a/PyNutil/processing/counting_and_load.py
+++ b/PyNutil/processing/counting_and_load.py
@@ -270,6 +270,7 @@ def warp_image(image, triangulation, rescaleXY):
 
 def flat_to_dataframe(
     image,
+    damage_mask,
     rescaleXY=None):
     """
     Converts a flat file to a DataFrame.
@@ -287,7 +288,16 @@ def flat_to_dataframe(
         np.array: array in shape of alignment XY scaled by rescaleXY with allen ID for each point
     """
     scale_factor = calculate_scale_factor(image, rescaleXY)
-    df_area_per_label = count_pixels_per_label(image, scale_factor)
+    if damage_mask is not None:
+        damage_mask = cv2.resize(damage_mask.astype(np.uint8), (image.shape[::-1]), interpolation=cv2.INTER_NEAREST).astype(bool)
+        undamaged_df_area_per_label = count_pixels_per_label(image[damage_mask], scale_factor)
+        damaged_df_area_per_label = count_pixels_per_label(image[~damage_mask], scale_factor)
+        undamaged_df_area_per_label = undamaged_df_area_per_label.rename(columns={"region_area": "undamaged_region_area"})
+        damaged_df_area_per_label = damaged_df_area_per_label.rename(columns={"region_area": "damaged_region_area"})
+        df_area_per_label = pd.merge(undamaged_df_area_per_label, damaged_df_area_per_label, on='idx', how='outer').fillna(0)
+        df_area_per_label["region_area"] = df_area_per_label["undamaged_region_area"] + df_area_per_label["damaged_region_area"]
+    else:
+        df_area_per_label = count_pixels_per_label(image, scale_factor)
     return df_area_per_label
 
 
diff --git a/tests/test_data/nonlinear_allen_mouse/segmentations/test_s003.png b/tests/test_data/nonlinear_allen_mouse/segmentations/test_s003.png
index 7ff61a5947eca46caa8b304805a5210b93dbd5d7..260f98b911f9071cb51e7d5a47bd263866afae6b 100644
GIT binary patch
literal 6768
zcmeAS@N?(olHy`uVBq!ia0y~yV7<e@!2E)P2`J*7vw9N)gOs_ai(^Q|oVT|&_8v9h
zVLkZx;NR_M?l&qQ<i26Na+0`aL_yhiexUM(^uO1cfi%NDk132milf5_#A6I(1Cj~~
zr$8JQ7fB%5(4YZQC?Jpu5@TA#10)?BMm3EF#Au2b%^IU+#AsnTT5F6}pQBBJ(YDcO
zBXP8~4D1{Xz9vEAYB`1jmBz)NckS-iWqcq~Z++nR4hniOXWm_t26cw)jxQbU4UKjS
zsntJzu!^0b;NF=#d)~cE_GYj+dY|uuogM|jyLtClP;b8Be4*xO4}NqggIZ%cJ6<s|
z%rQ4MU%&hJ4Iu`<kK38|^m~&Zya(Ro?FEfMJt&T$mJ3G*CPoJ)245>=bZm8Wka=|A
gd~^n3Fixe^9phF#p7cH7F{oSP>FVdQ&MBb@0Gi}9uK)l5

literal 120
zcmeAS@N?(olHy`uVBq!ia0vp^;y^6G0VEi1xa>Xxr0hLi977}|a}P4|H8}7b-tgpq
zzg`ZzhsV~Xll`n-rm!-9nkd@l!YBDqwY{fca)ZpKLk6EFh(2+*dbtFoK=0H<F2TDY
RPXs_ldb;|#taD0e0sz(5Cr|(Y

diff --git a/tests/test_data/nonlinear_allen_mouse/segmentations/test_s003_.png b/tests/test_data/nonlinear_allen_mouse/segmentations/test_s003_.png
deleted file mode 100644
index 260f98b911f9071cb51e7d5a47bd263866afae6b..0000000000000000000000000000000000000000
GIT binary patch
literal 0
HcmV?d00001

literal 6768
zcmeAS@N?(olHy`uVBq!ia0y~yV7<e@!2E)P2`J*7vw9N)gOs_ai(^Q|oVT|&_8v9h
zVLkZx;NR_M?l&qQ<i26Na+0`aL_yhiexUM(^uO1cfi%NDk132milf5_#A6I(1Cj~~
zr$8JQ7fB%5(4YZQC?Jpu5@TA#10)?BMm3EF#Au2b%^IU+#AsnTT5F6}pQBBJ(YDcO
zBXP8~4D1{Xz9vEAYB`1jmBz)NckS-iWqcq~Z++nR4hniOXWm_t26cw)jxQbU4UKjS
zsntJzu!^0b;NF=#d)~cE_GYj+dY|uuogM|jyLtClP;b8Be4*xO4}NqggIZ%cJ6<s|
z%rQ4MU%&hJ4Iu`<kK38|^m~&Zya(Ro?FEfMJt&T$mJ3G*CPoJ)245>=bZm8Wka=|A
gd~^n3Fixe^9phF#p7cH7F{oSP>FVdQ&MBb@0Gi}9uK)l5

-- 
GitLab