From 4370211b107d7859a2f9766f3ca3c42de5266a6e Mon Sep 17 00:00:00 2001
From: Nikolai <osterei33@gmx.de>
Date: Thu, 9 Aug 2018 09:25:26 +0200
Subject: [PATCH] trying to implement 2D ratio hist

---
 plotting.py           | 29 +++++++++++++++++++++++++++++
 scripts/plot_NN_2D.py | 29 +++++++++++++++++++++++++++--
 2 files changed, 56 insertions(+), 2 deletions(-)

diff --git a/plotting.py b/plotting.py
index db24def..c4f9000 100644
--- a/plotting.py
+++ b/plotting.py
@@ -250,6 +250,35 @@ def plot_hist_2D_events(plotname, valsx, valsy, nbinsx, xmin, xmax, nbinsy, ymin
     plot_hist_2D(plotname, xedges, yedges, hist, varx_label, vary_label, log)
 
 
+def plot_hist_2D_events_ratio(plotname,
+                              valsx1, valsy1,
+                              valsx2, valsy2,
+                              nbinsx, xmin, xmax, nbinsy, ymin, ymax,
+                              weights1=None,
+                              weights2=None,
+                              varx_label=None, vary_label=None, log=False):
+
+    xedges = np.linspace(xmin, xmax, nbinsx)
+    yedges = np.linspace(ymin, ymax, nbinsy)
+
+    hist1, xedges, yedges = np.histogram2d(valsx1, valsy1, bins=(xedges, yedges), weights=weights1)
+    hist2, xedges, yedges = np.histogram2d(valsx2, valsy2, bins=(xedges, yedges), weights=weights2)
+
+    hist1 = hist1.T
+    hist2 = hist2.T
+
+    hist1 /= hist1.sum()
+    hist2 /= hist2.sum()
+
+    hist2[hist2==0] = np.nan
+
+    hist = hist1/hist2
+
+    print(hist)
+
+    plot_hist_2D(plotname, xedges, yedges, hist, varx_label, vary_label, log)
+
+
 def plot_cond_avg_actmax_2D(plotname, model, layer, neuron, ranges,
                             varx_index, vary_index,
                             nbinsx, xmin, xmax, nbinsy, ymin, ymax,
diff --git a/scripts/plot_NN_2D.py b/scripts/plot_NN_2D.py
index ba54b1e..996c29e 100755
--- a/scripts/plot_NN_2D.py
+++ b/scripts/plot_NN_2D.py
@@ -13,6 +13,7 @@ from KerasROOTClassification.plotting import (
     plot_NN_vs_var_2D,
     plot_profile_2D,
     plot_hist_2D_events,
+    plot_hist_2D_events_ratio,
     plot_cond_avg_actmax_2D
 )
 from KerasROOTClassification.utils import get_single_neuron_function, get_max_activation_events
@@ -23,7 +24,7 @@ parser.add_argument("output_filename")
 parser.add_argument("varx")
 parser.add_argument("vary")
 parser.add_argument("-m", "--mode",
-                    choices=["mean_sig", "mean_bkg", "profile_sig", "profile_bkg", "hist_sig", "hist_bkg", "hist_actmax", "cond_actmax"],
+                    choices=["mean_sig", "mean_bkg", "profile_sig", "profile_bkg", "hist_sig", "hist_bkg", "hist_ratio", "hist_actmax", "cond_actmax"],
                     default="mean_sig")
 parser.add_argument("-l", "--layer", type=int, help="Layer index (takes last layer by default)")
 parser.add_argument("-n", "--neuron", type=int, default=0, help="Neuron index (takes first neuron by default)")
@@ -134,7 +135,7 @@ elif args.mode.startswith("profile"):
         **opt_kwargs
     )
 
-elif args.mode.startswith("hist"):
+elif args.mode.startswith("hist") and not args.mode == "hist_ratio":
 
     if not args.mode == "hist_actmax":
         if args.mode == "hist_sig":
@@ -171,6 +172,30 @@ elif args.mode.startswith("hist"):
         log=args.log,
     )
 
+elif args.mode == "hist_ratio":
+    valsx1 = c.x_test[c.y_test==1][:,varx_index]
+    valsx2 = c.x_test[c.y_test==0][:,varx_index]
+    if not plot_vs_activation:
+        valsy1 = c.x_test[c.y_test==1][:,vary_index]
+        valsy2 = c.x_test[c.y_test==0][:,vary_index]
+    else:
+        valsy1 = c.scores_test[c.y_test==1].reshape(-1)
+        valsy2 = c.scores_test[c.y_test==0].reshape(-1)
+    weights1 = c.w_test[c.y_test==1]
+    weights2 = c.w_test[c.y_test==0]
+
+    plot_hist_2D_events_ratio(
+        args.output_filename,
+        valsx1, valsy1,
+        valsx2, valsy2,
+        xmin=varx_range[0], xmax=varx_range[1], nbinsx=varx_range[2],
+        ymin=vary_range[0], ymax=vary_range[1], nbinsy=vary_range[2],
+        weights1=weights1,
+        weights2=weights2,
+        varx_label=varx_label, vary_label=vary_label,
+        log=args.log,
+    )
+
 elif args.mode.startswith("cond_actmax"):
 
     x_test_scaled = c.scaler.transform(c.x_test)
-- 
GitLab