Commit 07102815 authored by Romain Loth's avatar Romain Loth

clean up nodetypes attribution + turn off 'others' category heuristics as it...

clean up nodetypes attribution + turn off 'others' category heuristics as it creates a case where TW.categories is not always the inverse relation than TW.catDict, which too many things rely upon to be allowed
parent e5d1e4ec
// KEPT FOR REFERENCE, BINNING NOW PRECOMPUTED in parseCustom
// rewrite of gradientColors with binning and for attributes that can have negative float values
// /!\ age and growth_rate attributes referred to by name
function colorsRelByBins_old(daclass) {
cancelSelection(false);
var binColors
var doModifyLabel = false
TW.handpickedcolor = true
// for debug of heatmapColoring
var totalsPerBinMin = {}
// should be = binColors.length
var nTicksParam = (daclass == 'age') ? 8 : 12
// do first loop entirely to get percentiles => bins, then modify alt_color
// estimating ticks
let tickThresholds = []
let valArray = []
for (var j=0 ; j < TW.nNodes ; j++) {
let n = TW.partialGraph.graph.nodes(TW.nodeIds[j])
if (
!n.hidden
&& n.attributes
&& n.attributes.category == 'terms'
&& n.attributes[daclass] != undefined
) {
valArray.push(Number(n.attributes[daclass]))
}
}
var len = valArray.length
valArray.sort(function(a, b) {return a - b;}) // important :)
for (var l=0 ; l < nTicksParam ; l++) {
let nthVal = Math.floor(len * l / nTicksParam)
tickThresholds.push(valArray[nthVal])
}
// also always add the max+1 as last tick (excluded upper bound of last bin)
tickThresholds.push((valArray[len-1])+1)
console.info(`[|===|=== ${nTicksParam} color ticks ===|===|]\n`, tickThresholds)
cancelSelection(false);
if (daclass == 'age') {
// 9 colors
binColors = TW.gui.getHeatmapColors(9)
}
else if (daclass == 'growth_rate') {
doModifyLabel = true
// 12 colors
binColors = TW.gui.getHeatmapColors(12)
}
// verification
if (nTicksParam != binColors.length) {
console.warn (`heatmapColoring setup mismatch: nTicksParam ${nTicksParam} should == nColors ${binColors.length}`)
}
// get the nodes
for (var j=0 ; j < TW.nNodes ; j++) {
let n = TW.partialGraph.graph.nodes(TW.nodeIds[j])
if (! n.hidden
&& n.attributes
&& n.attributes.category == 'terms'
&& ! isUndef(n.attributes[daclass])
) {
var valSt = n.attributes[daclass]
var originalLabel = TW.Nodes[n.id].label
if (doModifyLabel) {
n.label = `(${valSt}) ${originalLabel}`
}
else {
n.label = originalLabel
}
var theVal = parseFloat(valSt)
var foundBin = false
// console.log('theVal:',theVal)
if( !isNaN(theVal) ) { //is float
// iterate over bins
for(var k=0 ; k < tickThresholds.length-1; k++) {
var binMin = tickThresholds[k]
var binMax = tickThresholds[(k+1)]
if((theVal >= binMin) && (theVal < binMax)) {
// TW.partialGraph._core.graph.nodesIndex[n.id].binMin = binMin
// TW.partialGraph._core.graph.nodesIndex[n.id].color = binColors[j]
n.binMin = binMin
n.color = binColors[k]
n.customAttrs.alt_color = binColors[k]
n.customAttrs.altgrey_color = false
foundBin = true
// console.log(`theVal ${theVal} => found its bin ${binMin} ... ${binColors[k]}`)
if (!totalsPerBinMin[binMin]) {
totalsPerBinMin[binMin] = 1
}
else {
totalsPerBinMin[binMin]++
}
break
}
}
// case no bin after loop (perhaps more ticks than colors-1 ??)
if (!foundBin) {
console.warn('no bin for theVal', theVal, n.id)
n.binMin = null
n.color = '#000'
n.customAttrs.alt_color = '#000'
}
}
else {
// case no val
// console.log('no val for', n.id)
n.binMin = null
n.color = '#555'
n.customAttrs.alt_color = '#555'
}
}
}
// console.debug(valArray)
console.info('coloring distribution per tick thresholds' , totalsPerBinMin)
// Edge precompute alt_rgb by new source-target nodes-colours combination
repaintEdges()
// set_ClustersLegend ( daclass )
TW.partialGraph.render();
}
......@@ -220,6 +220,14 @@ function sortNodeTypes(observedTypesDict, optionalNodeConf) {
observedTypes.sort(function(a,b) {return observedTypesDict[b] - observedTypesDict[a]})
let nbNodeTypes = 2
if (observedTypes.length > nbNodeTypes) {
console.warn(`The graph source data has more different node types than
supported. Less frequent node types will be ignored.
Max allowed types: ${nbNodeTypes},
Found: ${observedTypes.length} (namely: ${observedTypes})`)
}
var declaredTypes = []
for (var i = 0 ; i < nbNodeTypes ; i++ ) {
if (optionalNodeConf && optionalNodeConf["node"+i]) {
......@@ -234,10 +242,14 @@ function sortNodeTypes(observedTypesDict, optionalNodeConf) {
}
}
var newcats = []
var catDict = {}
// console.log("observedTypes", observedTypes)
// console.log("declaredTypes", declaredTypes)
var newcats = [] // will become TW.categories
var catDict = {} // will become TW.catDict
var nTypes = observedTypes.length
if(nTypes==0) {
newcats[0]="Terms";
catDict["Terms"] = 0;
......@@ -251,43 +263,72 @@ function sortNodeTypes(observedTypesDict, optionalNodeConf) {
console.log(`cat unique (${observedTypes[0]}) =>0`)
}
if(nTypes>1) {
// allows multiple node types, with an "all the rest" node1
// try stipulated cats, then fallbacks
// possible: loop
if (observedTypesDict[declaredTypes[0]]) {
newcats[0] = declaredTypes[0];
catDict[declaredTypes[0]] = 0;
}
if (observedTypesDict[declaredTypes[1]]) {
newcats[1] = declaredTypes[1];
catDict[declaredTypes[1]] = 1;
// allows multiple node types even if not well declared
// ----------------------------------------------------
// POSSIBLE: an "all the rest" last nodeType ?
let alreadyUsed = {}
// try declared cats in declared position, independantly from each other
for (var i = 0 ; i < nbNodeTypes; i++) {
if (observedTypesDict[declaredTypes[i]]) {
let validatedType = declaredTypes[i]
newcats[i] = validatedType;
alreadyUsed[validatedType] = true
}
}
// NB: type for nodes0 will be the majoritary by default, unless taken
if (!newcats[0]) {
if (observedTypes[0] != newcats[1]) {
newcats[0] = observedTypes[0] // 0 is the most frequent here
catDict[observedTypes[0]] = 0;
}
else {
newcats[0] = observedTypes[1] // 1 is second most frequent
catDict[observedTypes[1]] = 0;
// console.log("found stipulated cats", newcats, catDict)
// fallbacks: if some or all stipulated cats are not found
// ---------
// heuristic A: fill missing ones, by frequence
// (eg if nodes0 was not found, then type for nodes0 will be the
// majoritary observed one, unless taken where we move one up)
for (var i = 0 ; i < nbNodeTypes; i++) {
if (typeof newcats[i] == "undefined") {
for (var j = 0 ; j < nTypes ; j++) {
if (!alreadyUsed[observedTypes[j]]) {
newcats[i] = observedTypes[j]
alreadyUsed[observedTypes[j]] = true
break
}
}
}
}
// all the rest
for(var i in observedTypes) {
let c = observedTypes[i]
// or c is in "all the rest" group
// (POSS extend to multitypes)
if (c != newcats[0] && c != newcats[1]) {
if (!newcats[1]) newcats[1] = c;
else newcats[1] += '/'+c
catDict[c] = 1;
// console.log("after filling majority cats", newcats, catDict)
// all the rest (heuristic B)
if (!newcats[nbNodeTypes-1]) {
for(var i in observedTypes) {
// without a group others: if there is more than two cats altogether,
// only the last cat counts as node1 cat
let c = observedTypes[i]
// -------------------------------------------- for a group "others"
// with a group "others": if there is more than two cats altogether,
// all the non majoritary or non-stipulated
// are grouped here as node1 cat
// but problem: it break the symetry b/w TW.categories and TW.catDict
//
// // c is in "all the rest" group (POSS extend to multitypes)
// if (c != newcats[0] && c != newcats[1]) {
// if (!newcats[1]) newcats[1] = c;
// else newcats[1] += '/'+c
// catDict[c] = 1;
// }
// -------------------------------------------/ for a group "others"
}
}
}
// reverse lookup
for (var i in newcats) {
catDict[newcats[i]] = i
}
return {'categories': newcats, 'lookup_dict': catDict}
}
......@@ -650,10 +691,18 @@ function dictfyGexf( gexf , categories ){
for(var i in categories) {
nodesByType[i] = []
let subCats = categories[i].split(/\//g)
for (var j in subCats) {
catDict[subCats[j]] = i
}
// without a group "others" -------------------
catDict[categories[i]] = i
// POSS subCats for cat "others" if open types mapped to n types
//
// ----------------------- with a group "others"
// let subCats = categories[i].split(/\//g)
// for (var j in subCats) {
// catDict[subCats[j]] = i
// }
// ---------------------- /with a group "others"
}
......@@ -1077,11 +1126,17 @@ function dictfyJSON( data , categories ) {
for(var i in categories) {
nodesByType[i] = []
let subCats = categories[i].split(/\//g)
for (var j in subCats) {
catDict[subCats[j]] = i
}
// without a group "others" -------------------
catDict[categories[i]] = i
// POSS subCats for cat "others" if open types mapped to n types
//
// ----------------------- with a group "others"
// let subCats = categories[i].split(/\//g)
// for (var j in subCats) {
// catDict[subCats[j]] = i
// }
// ---------------------- /with a group "others"
}
// normalization, same as parseGexf
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment