From 269a4f5804afd44bd0a36fb734d0e2ac68378b85 Mon Sep 17 00:00:00 2001
From: rloth <romain.loth@iscpif.fr>
Date: Mon, 25 Sep 2017 14:21:29 +0200
Subject: [PATCH] transitive titling + option for no titling

---
 explorerjs.html                             |  1 +
 settings_explorerjs.js                      |  5 +-
 twmain/extras_explorerjs.js                 | 51 +++++++++++++++++----
 twpresets/settings_explorerjs.comex.js      |  8 ++--
 twpresets/settings_explorerjs.devdefault.js |  5 +-
 5 files changed, 55 insertions(+), 15 deletions(-)

diff --git a/explorerjs.html b/explorerjs.html
index aecd2f6..b0c0640 100644
--- a/explorerjs.html
+++ b/explorerjs.html
@@ -724,6 +724,7 @@
                         <label for="attr-titling-metric" class="smlabel input-group-addon">Titling metric</label>
                         <select id="attr-titling-metric" name="attr-titling-metric"
                                 class="custom-select form-control">
+                                <option value="off">No titling (preserve original values)</option>
                                 <!-- other attrs filled by fillAttrsInForm(., 'num') -->
                         </select>
                       </div>
diff --git a/settings_explorerjs.js b/settings_explorerjs.js
index 4fc097c..6a35ffe 100644
--- a/settings_explorerjs.js
+++ b/settings_explorerjs.js
@@ -73,12 +73,13 @@ TW.conf = (function(TW){
     'auto-outdegree'  : {'col': "heatmap",  'n': 7,  'binmode': 'samepop', 'legend': 'Auto OutDegree'},
     'cluster_index'   : {'col': "cluster" ,          'binmode': 'off'        },
     'clust_louvain'   : {'col': "cluster" ,          'binmode': 'off',
-                         'legend':'Louvain clustering'                       },
+                         'legend':'Louvain clustering', 'titlingMetric': 'auto-outdegree'},
     'country':{
                          'col':"cluster" ,
                          'binmode': 'off',
                          'legend': 'Country'
-                       },
+                         'titlingMetric': 'off';
+              },
     'total_occurrences':{
                          'col':"heatmap" ,
                          'binmode': 'samerange',
diff --git a/twmain/extras_explorerjs.js b/twmain/extras_explorerjs.js
index c0556cf..9413bd0 100755
--- a/twmain/extras_explorerjs.js
+++ b/twmain/extras_explorerjs.js
@@ -430,7 +430,10 @@ function updateColorsLegend ( daclass, forTypes, groupedByTicks ) {
           }
 
           // we add a title to cluster classes by ranking their nodes and taking k best labels, except if type is "social"
-          if (TW.facetOptions[daclass] && TW.facetOptions[daclass].col == 'cluster' && curType != TW.categories[1]) {
+          if (TW.facetOptions[daclass]
+           && TW.facetOptions[daclass].titlingMetric
+           && TW.facetOptions[daclass].titlingMetric != 'off'
+           && TW.facetOptions[daclass].col == 'cluster') {
 
             // let t0 = performance.now()
 
@@ -455,10 +458,42 @@ function updateColorsLegend ( daclass, forTypes, groupedByTicks ) {
               getVal = function(node) {return node.size}
             }
 
-            for (let j in legendInfo[l]['nids']) {
-              let n = TW.partialGraph.graph.nodes(legendInfo[l]['nids'][j])
+            // our source of words (labels)
+            let ndsToRetrieveNameFrom = {}
+
+            // if node0 contains meaningful labels for titling
+            // we can title node1 clusters using node0 neighborhood
+            // => we'll use metric on bipartite neighborhood labels
+            // POSS it could be a conf option to use another type or not
+            if (curType == TW.categories[1] && TW.Relations["XR"]) {
+              // transitive step from nodetype to their surrogate nodetype
+              for (var i in legendInfo[l]['nids']) {
+                let start_nid = legendInfo[l]['nids'][i]
+                let transitiveNids = TW.Relations["XR"][start_nid]
+                for (var j in transitiveNids) {
+                  let nei_nid = transitiveNids[j]
+                  if (!ndsToRetrieveNameFrom[nei_nid]) {
+                    ndsToRetrieveNameFrom[nei_nid] = 1
+                  }
+                  else {
+                    ndsToRetrieveNameFrom[nei_nid] += 1   // <== coef
+                  }
+                }
+              }
+            }
+            // normal case => directly use metric on these nodes' labels
+            else {
+              for (var i in legendInfo[l]['nids']) {
+                let nid = legendInfo[l]['nids'][i]
+                ndsToRetrieveNameFrom[nid] = 1
+              }
+            }
+
+            for (var nid in ndsToRetrieveNameFrom) {
+              let n = TW.Nodes[nid]
+              let coef = ndsToRetrieveNameFrom[nid]
 
-              let theRankingVal = getVal(n)
+              let theRankingVal = getVal(n) * Math.sqrt(coef)
 
               if (titles.length < maxLen) {
                 titles.push({'key':n.label, 'val':theRankingVal})
@@ -1107,9 +1142,9 @@ function fillAttrsInForm(menuId, optionalAttTypeConstraint) {
   let elChooser = document.getElementById(menuId)
 
   // 1- remove any previous options from possible previous graphs
-  let autoOptions = document.getElementById(menuId).querySelectorAll('option')
-  for (var i = 0 ; i <= autoOptions.length - 1 ; i++) {
-    elChooser.removeChild(autoOptions[i])
+  let filledOptions = document.getElementById(menuId).querySelectorAll('option[data-opttype=filled]')
+  for (var i = 0 ; i <= filledOptions.length - 1 ; i++) {
+    elChooser.removeChild(filledOptions[i])
   }
 
   // 2- ls | uniq all options (no matter what active type they belong too)
@@ -1146,7 +1181,7 @@ function fillAttrsInForm(menuId, optionalAttTypeConstraint) {
       opt.dataset.opttype = "auto"
     }
     else {
-      opt.dataset.opttype = "fromFacets"
+      opt.dataset.opttype = "filled"
     }
     elChooser.appendChild(opt)
   }
diff --git a/twpresets/settings_explorerjs.comex.js b/twpresets/settings_explorerjs.comex.js
index f9063e2..1680cc1 100644
--- a/twpresets/settings_explorerjs.comex.js
+++ b/twpresets/settings_explorerjs.comex.js
@@ -73,12 +73,13 @@ TW.conf = (function(TW){
     'auto-outdegree'  : {'col': "heatmap",  'n': 7,  'binmode': 'samepop', 'legend': 'Auto OutDegree'},
     'cluster_index'   : {'col': "cluster" ,          'binmode': 'off'        },
     'clust_louvain'   : {'col': "cluster" ,          'binmode': 'off',
-                         'legend':'Louvain clustering'                       },
+                         'legend':'Louvain clustering', 'titlingMetric': 'auto-outdegree'},
     'country':{
                          'col':"cluster" ,
                          'binmode': 'off',
                          'legend': 'Country'
-                       },
+                         'titlingMetric': 'off';
+              },
 
     'normfactor':{
                          'col':"heatmap" ,
@@ -89,7 +90,8 @@ TW.conf = (function(TW){
     'ACR':{
                          'col':"cluster" ,
                          'binmode': 'off',
-                         'legend': 'Affiliation'
+                         'legend': 'Affiliation',
+                         'titlingMetric': 'off';
                        },
     'nbjobs':{
                          'col':"heatmap" ,
diff --git a/twpresets/settings_explorerjs.devdefault.js b/twpresets/settings_explorerjs.devdefault.js
index 3b07b32..a9270c2 100644
--- a/twpresets/settings_explorerjs.devdefault.js
+++ b/twpresets/settings_explorerjs.devdefault.js
@@ -73,12 +73,13 @@ TW.conf = (function(TW){
     'auto-outdegree'  : {'col': "heatmap",  'n': 7,  'binmode': 'samepop', 'legend': 'Auto OutDegree'},
     'cluster_index'   : {'col': "cluster" ,          'binmode': 'off'        },
     'clust_louvain'   : {'col': "cluster" ,          'binmode': 'off',
-                         'legend':'Louvain clustering'                       },
+                         'legend':'Louvain clustering', 'titlingMetric': 'auto-outdegree'},
     'country':{
                          'col':"cluster" ,
                          'binmode': 'off',
                          'legend': 'Country'
-                       },
+                         'titlingMetric': 'off';
+              },
     'total_occurrences':{
                          'col':"heatmap" ,
                          'binmode': 'samerange',
-- 
2.21.0