1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
|
/**
*
* This file is part of Tulip (www.tulip-software.org)
*
* Authors: David Auber and the Tulip development Team
* from LaBRI, University of Bordeaux
*
* Tulip is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation, either version 3
* of the License, or (at your option) any later version.
*
* Tulip is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
*/
#include "ConvolutionClustering.h"
#include "ConvolutionClusteringSetup.h"
#include <tulip/ForEach.h>
using namespace std;
namespace tlp {
PLUGIN(ConvolutionClustering)
ConvolutionClustering::ConvolutionClustering(PluginContext* context):DoubleAlgorithm(context), metric(NULL) {
addInParameter<NumericProperty*>("metric",
HTML_HELP_OPEN() \
HTML_HELP_DEF( "type", "NumericProperty" ) \
HTML_HELP_DEF( "value", "An existing node metric" ) \
HTML_HELP_BODY() \
"An existing node metric property" \
HTML_HELP_CLOSE(), "viewMetric", false);
}
//convolution function, build a triangular function center in 0 with a width width and a
double g(int k,double width,double amplitude) {
double slope=amplitude/width;
if ((k<=-width) || (k>=width))
return 0;
else {
if (k<0)
return ((double)k*slope+amplitude); //partie croissante du signal triangulaire
else
return ((double)-k*slope+amplitude); //partie d�croissante du signal triangulaire
}
}
//================================================================================
int getInterval(int d,const vector<int> &ranges) {
for (unsigned int i=0; i<ranges.size()-1; ++i) {
if ((d>=ranges[i]) && (d<ranges[i+1])) return i;
}
return ranges.size()-2;
}
//================================================================================
void ConvolutionClustering::setParameters(int histosize,int threshold,int width) {
this->histosize=histosize;
this->threshold=threshold;
this->width=width;
}
//================================================================================
void ConvolutionClustering::getParameters(int &histosize,int &threshold,int &width) {
histosize=this->histosize;
threshold=this->threshold;
width=this->width;
}
//================================================================================
list<int> ConvolutionClustering::getLocalMinimum() {
vector<double> &discretHisto = *getHistogram();
list<int> localMinimum;
localMinimum.push_back(0);
double previous = discretHisto[0];
double current = discretHisto[1];
bool slopeSens = !(previous > current); //false descent
for (unsigned int i = 1; i < discretHisto.size(); ++i) {
current = discretHisto[i];
bool newSlopeSens = !(previous > current);
if (newSlopeSens != slopeSens) {
//new Local minimum
if (slopeSens==false) {
int local = localMinimum.back();
if ((int) i - local < width/2) {
localMinimum.pop_back();
localMinimum.push_back((i+local)/2);
}
else
localMinimum.push_back(i);
}
slopeSens=newSlopeSens;
}
previous = current;
}
return localMinimum;
}
//================================================================================
void ConvolutionClustering::autoSetParameter() {
map<double,int> histo;
Iterator<node> *itN=graph->getNodes();
while (itN->hasNext()) {
node itn=itN->next();
double tmp = metric->getNodeDoubleValue(itn);
if (histo.find(tmp)==histo.end())
histo[tmp]=1;
else
histo[tmp]+=1;
}
delete itN;
if (histo.empty()) return;
//===============================================================================
//Find good step for discretization
//We take the minimum interval between to bar in the continue histogram
double deltaXMin=-1;
double deltaXMax=0;
double deltaSum=0;
map<double,int>::iterator itMap=histo.begin();
double lastValue=(*itMap).first;
++itMap;
for (; itMap!=histo.end(); ++itMap) {
double delta = itMap->first-lastValue;
deltaSum+=delta;
if (delta > deltaXMax)
deltaXMax = delta;
else if (delta < deltaXMin || deltaXMin < 0 )
deltaXMin = delta;
lastValue=(*itMap).first;
}
histosize=(int)((metric->getNodeDoubleMax()-metric->getNodeDoubleMin())/deltaXMin);
if (histosize > 16384) histosize = 16384; //histosize = histosize <? 16384;
if (histosize < 64) histosize = 64; //histosize = histosize >? 64;
//===============================================================================
//Find good with for the convolution function
//We take the maximum width of the biggest hole
//width=(int)(deltaXMax*histosize/(metric->getNodeMax()-metric->getNodeMin()));
//width=(int)(deltaXMin*histosize/(metric->getNodeMax()-metric->getNodeMin()));
deltaSum /= histo.size();
width=(int)(deltaSum*histosize/(metric->getNodeDoubleMax()-metric->getNodeDoubleMin()));
//===============================================================================
//Find good threshold
//make the average of all local minimum
vector<double> &discretHisto=*getHistogram();
double sum=0;
int nbElement=1;
if (discretHisto.size() > 1) {
double previous = discretHisto[0];
double current = discretHisto[1];
bool slopeSens = !(previous > current);
for (unsigned int i=1; i<discretHisto.size(); ++i) {
double current = discretHisto[i];
bool newSlopeSens = !(previous > current);
if (newSlopeSens != slopeSens) {
//new Local minimum
nbElement++;
sum+=(current + previous)/ 2;
slopeSens = newSlopeSens;
}
previous = current;
}
}
threshold=(int)(sum/nbElement);
}
//================================================================================
vector<double> *ConvolutionClustering::getHistogram() {
//building of the histogram of values
histogramOfValues.clear();
double minVal = metric->getNodeDoubleMin();
double maxMinRange = metric->getNodeDoubleMax() - minVal;
node n;
forEach(n, graph->getNodes()) {
int tmp=(int)((metric->getNodeDoubleValue(n) - minVal) * (double)histosize /
maxMinRange);
if (histogramOfValues.find(tmp) == histogramOfValues.end())
histogramOfValues[tmp]=1;
else
histogramOfValues[tmp]+=1;
}
//Apply the convolution on the histogram of values
//Convolution parameter, this version work only with integer
smoothHistogram.clear();
smoothHistogram.resize(histosize);
for (int pos=0; pos<histosize; ++pos)
smoothHistogram[pos]=0;
map<int,int>::iterator itMap;
for (itMap=histogramOfValues.begin(); itMap!=histogramOfValues.end(); ++itMap) {
double value=itMap->second;
int index=itMap->first;
for (int i=-width; i<=width; ++i) {
if ((index+i)>=0 && (index+i)<histosize)
smoothHistogram[index+i] += value*g(i,width,1);
}
}
return &smoothHistogram;
}
//================================================================================
void ConvolutionClustering::getClusters(const std::vector<int> &ranges) {
node n;
double minVal = metric->getNodeDoubleMin();
double maxMinRange = metric->getNodeDoubleMax() - minVal;
forEach(n,graph->getNodes()) {
int tmp = getInterval((int)((metric->getNodeDoubleValue(n) - minVal)*(double)histosize / maxMinRange), ranges);
result->setNodeValue(n,tmp);
}
}
//================================================================================
bool ConvolutionClustering::run() {
histosize=128;
if (dataSet != NULL)
dataSet->get("metric", metric);
if (metric == NULL)
metric=graph->getProperty<DoubleProperty>("viewMetric");
autoSetParameter();
getHistogram();
ConvolutionClusteringSetup *mysetup = new ConvolutionClusteringSetup(this);
bool setupResult = mysetup->exec();
delete mysetup;
if (setupResult==QDialog::Rejected) {
pluginProgress->setError("user cancellation");
return false;
}
vector<int> ranges;
ranges.push_back(0);
list<int> localMinimum = getLocalMinimum();
while (!localMinimum.empty()) {
ranges.push_back(localMinimum.front());
localMinimum.pop_front();
}
ranges.push_back(histosize);
//Ensure that there is elements inside each intervals.
getClusters(ranges);
return true;
}
bool ConvolutionClustering::check(std::string& errorMsg) {
if (dataSet != NULL)
dataSet->get("metric", metric);
if (metric == NULL)
metric=graph->getProperty<DoubleProperty>("viewMetric");
if (metric->getNodeDoubleMax() == metric->getNodeDoubleMin()) {
errorMsg = "All metric values are the same";
return false;
}
return true;
}
}
|