From b905961bfbd803b8290ca164a10169c1d88d94d4 Mon Sep 17 00:00:00 2001
From: pswain <peter.swain@ed.ac.uk>
Date: Fri, 17 May 2024 14:39:49 +0100
Subject: [PATCH] change: updated plot_kymograph for new sorting

---
 examples/plot_kymograph.py | 33 +++++++++++++++++++++++++--------
 src/wela/sorting.py        | 10 ++++++----
 2 files changed, 31 insertions(+), 12 deletions(-)

diff --git a/examples/plot_kymograph.py b/examples/plot_kymograph.py
index 0a31406..e32cb7d 100644
--- a/examples/plot_kymograph.py
+++ b/examples/plot_kymograph.py
@@ -1,25 +1,42 @@
 import matplotlib.cm
+import numpy as np
 from wela.figs2pdf import figs2pdf
-from wela.dataloader import dataloader, sort_df
+from wela.dataloader import dataloader
+from wela.sorting import sort_by_maximum
+
 from wela.plotting import kymograph, plot_lineage
 
-datasets = ["Pdr5_3_11_22", "Pdr5_flc_10ugml_1676"]
+datasets = ["Pdr5_3_11_22"]
 for dataname in datasets:
+    # load data
     dl = dataloader(".", "/Users/pswain/wip/tsv_data/pdr_with_gr/")
-    dl.load(dataname, use_tsv=True)
-    sdf = sort_df(dl.df, "median_GFP")
-
+    dl.load(dataname, use_tsv=True, hours=False)
+    dl.df.time = dl.df.time / 60
     title = dataname.split(".")[0]
-    kymograph(sdf, hue="buddings", title=title)
-    kymograph(sdf, hue="median_GFP", title=title)
+    # although only one group for this data
+    groups = dl.df.group.unique()
+    # standard kymographs
+    kymograph(dl.df, hue="buddings", title=title)
+    kymograph(dl.df, hue="median_GFP", title=title, buddings=True)
     kymograph(
-        sdf,
+        dl.df,
         hue="bud_growth_rate",
         title=title,
         vmin=-50,
         vmax=50,
         cmap=matplotlib.cm.Spectral,
     )
+    # sorted kymograph
+    t, data = dl.get_time_series("median_GFP", group=groups[0])
+    sort_order = sort_by_maximum(data[:, t < 5], np.nanmean)
+    kymograph(
+        dl.df,
+        "median_GFP",
+        group=groups[0],
+        buddings=True,
+        sort_order=sort_order,
+        title=title,
+    )
 
 
 if False:
diff --git a/src/wela/sorting.py b/src/wela/sorting.py
index 11694aa..a44a7de 100644
--- a/src/wela/sorting.py
+++ b/src/wela/sorting.py
@@ -4,14 +4,16 @@ import numpy as np
 def sort_by_budding(buddings, bud_number=0):
     """Return indices for each cell when a particular budding occurs."""
     bud_indices = np.transpose(np.nonzero(buddings))
-    sort_order = []
+    particular_bud_indices = []
     for cell_i in range(buddings.shape[0]):
+        # find particular bud for this cell
         cell_buds = bud_indices[bud_indices[:, 0] == cell_i, :]
         if bud_number < cell_buds.shape[0]:
-            sort_order.append(cell_buds[bud_number][1])
+            particular_bud_indices.append(cell_buds[bud_number][1])
         else:
-            sort_order.append(np.nan)
-    sort_order = np.argsort(sort_order)
+            particular_bud_indices.append(np.nan)
+    # arrange by earliest bud
+    sort_order = np.argsort(particular_bud_indices)
     return sort_order
 
 
-- 
GitLab