From 229733a43b3ec6ed49a25f8342ce7bc6bac4466e Mon Sep 17 00:00:00 2001
From: Steffen Korn <steffen.korn@cern.ch>
Date: Wed, 26 Apr 2023 16:24:52 +0200
Subject: [PATCH] Adding check for unique variable blocks

---
 python/HelperModules/configparser.py | 35 +++++++++++++++++++++-------
 1 file changed, 27 insertions(+), 8 deletions(-)

diff --git a/python/HelperModules/configparser.py b/python/HelperModules/configparser.py
index e855fd5a..7e54e126 100644
--- a/python/HelperModules/configparser.py
+++ b/python/HelperModules/configparser.py
@@ -153,7 +153,7 @@ class configparser():
 
         opthandler.register_check("Name", check="set")
         opthandler.register_check("Label", check="set")
-        opthandler.register_check("Scaling", check="inlist", lst=["None", "minmax", "minmax_symmetric", "standard","pT_scale"])
+        opthandler.register_check("Scaling", check="inlist", lst=["None", "minmax", "minmax_symmetric", "standard", "pT_scale"])
         opthandler.register_check(["Binning", "CustomBinning"], check="set")
         opthandler.register_check("Scale", check="is_inlist", lst=["Linear", "Log"])
         return opthandler
@@ -271,7 +271,7 @@ class configparser():
         opthandler.register_check("ValidationSize", check="larger", thr=0)
         opthandler.register_check("ValidationSize", check="smaller", thr=1)
         opthandler.register_check("Verbosity", check="inlist", lst=[0, 1, 2])
-        opthandler.register_check("GetONNXModel" ,check="inlist", lst=[True,False])
+        opthandler.register_check("GetONNXModel", check="inlist", lst=[True, False])
         return opthandler
 
     def _initDNNModel(self):
@@ -321,8 +321,8 @@ class configparser():
             "MSE",
             "MAE",
             "HuberLoss"])
-        opthandler.register_check("RestoreBest", check="inlist", lst=[True,False])
-        opthandler.register_check("ReweightTargetDistribution", check="inlist", lst=[True,False])
+        opthandler.register_check("RestoreBest", check="inlist", lst=[True, False])
+        opthandler.register_check("ReweightTargetDistribution", check="inlist", lst=[True, False])
         return opthandler
 
     def _initGNNModel(self):
@@ -389,7 +389,7 @@ class configparser():
         opthandler.register_check("Loss", check="inlist", lst=["binary_crossentropy", "categorical_crossentropy"])
         opthandler.register_check("Patience", check="larger", thr=0)
         opthandler.register_check("MinDelta", check="larger", thr=0)
-        opthandler.register_check("RestoreBest", check="inlist", lst=[True,False])
+        opthandler.register_check("RestoreBest", check="inlist", lst=[True, False])
         return opthandler
 
     def _initBDTModel(self):
@@ -418,7 +418,7 @@ class configparser():
         opthandler.register_check("Patience", check="larger", thr=0)
         opthandler.register_check("Loss", check="inlist", lst=["deviance", "log_loss", "squared_error", "absolute_error"])
         opthandler.register_check("Criterion", check="inlist", lst=["friedman_mse", "mse"])
-        opthandler.register_check("ReweightTargetDistribution", check="inlist", lst=[True,False])
+        opthandler.register_check("ReweightTargetDistribution", check="inlist", lst=[True, False])
         return opthandler
 
     def _prep(self):
@@ -482,7 +482,7 @@ class configparser():
             else:
                 self.m_cfgvals[block[0]] = self._fill(block[1:], self._initBlock(block[0]), lnumbers)
 
-    def _check(self):
+    def _blockcheck(self):
         """Check if necessary blocks are present.
 
         :returns:
@@ -494,6 +494,24 @@ class configparser():
             except KeyError:
                 ErrorMessage(f"No {s} block detected!")
 
+    def _sanitychecks(self):
+        """perform sanity checks
+
+        :returns:
+
+        """
+        self._checkuniquevars()
+
+    def _checkuniquevars(self):
+        """Check if all variables have a unique name
+
+        :returns:
+
+        """
+        vnames = [v.get("Name") for v in self.m_cfgvals["VARIABLE"]]
+        if len(np.unique(vnames)) != len(vnames):
+            ErrorMessage("\n".join([f"VARIABLE {x} exists multiple times" for x in set(vnames) if vnames.count(x) > 1]))
+
     def read(self):
         """Reading config file and performing the parsing.
 
@@ -502,7 +520,8 @@ class configparser():
         """
         self._prep()
         self._build()
-        self._check()
+        self._blockcheck()
+        self._sanitychecks()
         if not isinstance(self.m_cfgvals["OUTPUT"], list):
             self.m_cfgvals["OUTPUT"] = [self.m_cfgvals["OUTPUT"]]
         self.m_cfgvals["DIR"] = directoryhandler(f"{self.m_cfgvals['GENERAL'].get('Job')}")
-- 
GitLab