5 * Created by westcott on 1/11/11.
6 * Copyright 2011 Schloss Lab. All rights reserved.
10 #include "nmdscommand.h"
11 #include "readphylipvector.h"
13 //**********************************************************************************************************************
14 vector<string> NMDSCommand::getValidParameters(){
16 string Array[] = {"phylip","axes","mindim","maxdim","iters","maxiters","trace","epsilon","outputdir","inputdir"};
17 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
21 m->errorOut(e, "NMDSCommand", "getValidParameters");
25 //**********************************************************************************************************************
26 NMDSCommand::NMDSCommand(){
29 //initialize outputTypes
30 vector<string> tempOutNames;
31 outputTypes["nmds"] = tempOutNames;
32 outputTypes["stress"] = tempOutNames;
35 m->errorOut(e, "NMDSCommand", "NMDSCommand");
39 //**********************************************************************************************************************
40 vector<string> NMDSCommand::getRequiredParameters(){
42 string Array[] = {"phylip"};
43 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
47 m->errorOut(e, "NMDSCommand", "getRequiredParameters");
51 //**********************************************************************************************************************
52 vector<string> NMDSCommand::getRequiredFiles(){
54 vector<string> myArray;
58 m->errorOut(e, "NMDSCommand", "getRequiredFiles");
62 //**********************************************************************************************************************
64 NMDSCommand::NMDSCommand(string option) {
68 //allow user to run help
69 if(option == "help") { help(); abort = true; }
72 //valid paramters for this command
73 string Array[] = {"phylip","axes","mindim","maxdim","iters","maxiters","trace","epsilon","outputdir", "inputdir"};
74 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
76 OptionParser parser(option);
77 map<string, string> parameters = parser. getParameters();
79 ValidParameters validParameter;
80 map<string, string>::iterator it;
82 //check to make sure all parameters are valid for command
83 for (it = parameters.begin(); it != parameters.end(); it++) {
84 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
86 //if the user changes the input directory command factory will send this info to us in the output parameter
87 string inputDir = validParameter.validFile(parameters, "inputdir", false);
88 if (inputDir == "not found"){ inputDir = ""; }
91 it = parameters.find("phylip");
92 //user has given a template file
93 if(it != parameters.end()){
94 path = m->hasPath(it->second);
95 //if the user has not given a path then, add inputdir. else leave path alone.
96 if (path == "") { parameters["phylip"] = inputDir + it->second; }
99 it = parameters.find("axes");
100 //user has given a template file
101 if(it != parameters.end()){
102 path = m->hasPath(it->second);
103 //if the user has not given a path then, add inputdir. else leave path alone.
104 if (path == "") { parameters["axes"] = inputDir + it->second; }
108 //initialize outputTypes
109 vector<string> tempOutNames;
110 outputTypes["nmds"] = tempOutNames;
111 outputTypes["stress"] = tempOutNames;
113 //required parameters
114 phylipfile = validParameter.validFile(parameters, "phylip", true);
115 if (phylipfile == "not open") { phylipfile = ""; abort = true; }
116 else if (phylipfile == "not found") { phylipfile = ""; m->mothurOut("You must provide a distance file before running the nmds command."); m->mothurOutEndLine(); abort = true; }
118 axesfile = validParameter.validFile(parameters, "axes", true);
119 if (axesfile == "not open") { axesfile = ""; abort = true; }
120 else if (axesfile == "not found") { axesfile = ""; }
122 //if the user changes the output directory command factory will send this info to us in the output parameter
123 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
125 outputDir += m->hasPath(phylipfile); //if user entered a file with a path then preserve it
128 string temp = validParameter.validFile(parameters, "mindim", false); if (temp == "not found") { temp = "1"; }
129 convert(temp, mindim);
131 temp = validParameter.validFile(parameters, "maxiters", false); if (temp == "not found") { temp = "500"; }
132 convert(temp, maxIters);
134 temp = validParameter.validFile(parameters, "iters", false); if (temp == "not found") { temp = "10"; }
135 convert(temp, iters);
137 temp = validParameter.validFile(parameters, "maxdim", false); if (temp == "not found") { temp = "2"; }
138 convert(temp, maxdim);
140 temp = validParameter.validFile(parameters, "epsilon", false); if (temp == "not found") { temp = "0.000000000001"; }
141 convert(temp, epsilon);
143 temp = validParameter.validFile(parameters, "trace", false); if (temp == "not found") { temp = "F"; }
144 trace = m->isTrue(temp);
146 if (mindim < 1) { m->mothurOut("mindim must be at least 1."); m->mothurOutEndLine(); abort = true; }
147 if (maxdim < mindim) { m->mothurOut("maxdim must be greater than mindim."); m->mothurOutEndLine(); abort = true; }
151 catch(exception& e) {
152 m->errorOut(e, "NMDSCommand", "NMDSCommand");
156 //**********************************************************************************************************************
157 void NMDSCommand::help(){
159 m->mothurOut("The nmds command is modelled after the nmds code written in R by Sarah Goslee, using Non-metric multidimensional scaling function using the majorization algorithm from Borg & Groenen 1997, Modern Multidimensional Scaling."); m->mothurOutEndLine();
160 m->mothurOut("The nmds command parameters are phylip, axes, mindim, maxdim, maxiters, iters, epsilon and trace."); m->mothurOutEndLine();
161 m->mothurOut("The phylip parameter allows you to enter your distance file."); m->mothurOutEndLine();
162 m->mothurOut("The axes parameter allows you to enter a file containing a starting configuration."); m->mothurOutEndLine();
163 m->mothurOut("The maxdim parameter allows you to select how maximum dimensions to use. Default=2"); m->mothurOutEndLine();
164 m->mothurOut("The mindim parameter allows you to select how minimum dimensions to use. Default=1"); m->mothurOutEndLine();
165 m->mothurOut("The maxiters parameter allows you to select the maximum number of iters to try with each random configuration. Default=500"); m->mothurOutEndLine();
166 m->mothurOut("The iters parameter allows you to select the number of random configuration to try. Default=10"); m->mothurOutEndLine();
167 m->mothurOut("The epsilon parameter allows you to select set an acceptable stopping point. Default=1e-12."); m->mothurOutEndLine();
168 m->mothurOut("The trace parameter allows you to see the output after each iter. Default=F"); m->mothurOutEndLine();
169 m->mothurOut("Example nmds(phylip=yourDistanceFile).\n");
170 m->mothurOut("Note: No spaces between parameter labels (i.e. phylip), '=' and parameters (i.e.yourDistanceFile).\n\n");
172 catch(exception& e) {
173 m->errorOut(e, "NMDSCommand", "help");
177 //**********************************************************************************************************************
178 NMDSCommand::~NMDSCommand(){}
179 //**********************************************************************************************************************
180 int NMDSCommand::execute(){
183 if (abort == true) { return 0; }
185 cout.setf(ios::fixed, ios::floatfield);
186 cout.setf(ios::showpoint);
188 vector<string> names;
189 vector< vector< double> > matrix;
191 //read in phylip file
192 ReadPhylipVector readFile(phylipfile);
193 names = readFile.read(matrix);
194 if (m->control_pressed) { return 0; }
197 vector< vector<double> > axes;
198 if (axesfile != "") { axes = readAxes(names); }
200 for (int i = mindim; i <= maxdim; i++) {
201 m->mothurOut("Processing Dimension: " + toString(i)); m->mothurOutEndLine();
203 string outputFileName = outputDir + m->getRootName(m->getSimpleName(phylipfile)) + "dim" + toString(i) + ".nmds";
204 string stressFileName = outputDir + m->getRootName(m->getSimpleName(phylipfile)) + "dim" + toString(i) + ".stress.nmds";
205 outputNames.push_back(outputFileName); outputTypes["nmds"].push_back(outputFileName);
206 outputNames.push_back(stressFileName); outputTypes["stress"].push_back(stressFileName);
209 m->openOutputFile(outputFileName, out);
210 m->openOutputFile(stressFileName, out2);
212 out2.setf(ios::fixed, ios::floatfield);
213 out2.setf(ios::showpoint);
214 out.setf(ios::fixed, ios::floatfield);
215 out.setf(ios::showpoint);
217 out2 << "Iter\tStress\tCorr" << endl;
219 for (int j = 0; j < iters; j++) {
220 if (trace) { m->mothurOut(toString(j+1)); m->mothurOutEndLine(); }
222 //get configuration - either randomly generate or resize to this dimension
223 vector< vector<double> > thisConfig;
224 if (axesfile == "") { thisConfig = generateStartingConfiguration(names.size(), i); }
225 else { thisConfig = getConfiguration(axes, i); }
226 if (m->control_pressed) { out.close(); out2.close(); for (int k = 0; k < outputNames.size(); k++) { remove(outputNames[k].c_str()); } return 0; }
228 //calc nmds for this dimension
230 vector< vector<double> > endConfig = nmdsCalc(matrix, thisConfig, stress);
231 if (m->control_pressed) { out.close(); out2.close(); for (int k = 0; k < outputNames.size(); k++) { remove(outputNames[k].c_str()); } return 0; }
233 //calc euclid distances for new config
234 vector< vector<double> > newEuclid = linearCalc.calculateEuclidianDistance(endConfig);
235 if (m->control_pressed) { out.close(); out2.close(); for (int k = 0; k < outputNames.size(); k++) { remove(outputNames[k].c_str()); } return 0; }
237 //calc correlation between original distances and euclidean distances from this config
238 double corr = linearCalc.calcPearson(matrix, newEuclid);
240 if (m->control_pressed) { out.close(); out2.close(); for (int k = 0; k < outputNames.size(); k++) { remove(outputNames[k].c_str()); } return 0; }
243 out << "Config" << (j+1) << '\t';
244 for (int k = 0; k < i; k++) { out << "X" << (k+1) << '\t'; }
246 out2 << (j+1) << '\t' << stress << '\t' << corr << endl;
248 output(endConfig, names, out);
250 if (m->control_pressed) { out.close(); out2.close(); for (int k = 0; k < outputNames.size(); k++) { remove(outputNames[k].c_str()); } return 0; }
254 out.close(); out2.close();
257 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
259 m->mothurOutEndLine();
260 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
261 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
262 m->mothurOutEndLine();
266 catch(exception& e) {
267 m->errorOut(e, "NMDSCommand", "execute");
271 //**********************************************************************************************************************
272 vector< vector<double> > NMDSCommand::nmdsCalc(vector< vector<double> >& matrix, vector< vector<double> >& config, double& stress1) {
275 vector< vector<double> > newConfig = config;
277 //calc euclid distances
278 vector< vector<double> > euclid = linearCalc.calculateEuclidianDistance(newConfig);
279 if (m->control_pressed) { return newConfig; }
281 double stress2 = calculateStress(matrix, euclid);
282 stress1 = stress2 + 1.0 + epsilon;
284 if (trace) { m->mothurOutEndLine(); m->mothurOut("Iter\tStress"); m->mothurOutEndLine(); }
287 while ((count < maxIters) && (abs(stress1 - stress2) > epsilon)) {
292 if (m->control_pressed) { return newConfig; }
294 vector< vector<double> > b; b.resize(euclid.size());
295 for (int i = 0; i < b.size(); i++) { b[i].resize(euclid[i].size(), 0.0); }
297 vector<double> columnSums; columnSums.resize(euclid.size(), 0.0);
298 for (int i = 0; i < euclid.size(); i++) {
299 for (int j = 0; j < euclid[i].size(); j++) {
300 //eliminate divide by zero error
301 if (euclid[i][j] != 0) {
302 b[i][j] = matrix[i][j] / euclid[i][j];
303 columnSums[j] += b[i][j];
309 //put in diagonal sums
310 for (int i = 0; i < euclid.size(); i++) { b[i][i] = columnSums[i]; }
312 int numInLowerTriangle = matrix.size() * (matrix.size()-1) / 2.0;
313 double n = (1.0 + sqrt(1.0 + 8.0 * numInLowerTriangle)) / 2.0;
316 newConfig = linearCalc.matrix_mult(newConfig, b);
317 for (int i = 0; i < newConfig.size(); i++) {
318 for (int j = 0; j < newConfig[i].size(); j++) {
319 newConfig[i][j] *= (1.0 / n);
323 euclid = linearCalc.calculateEuclidianDistance(newConfig);
325 stress2 = calculateStress(matrix, euclid);
327 if (trace) { m->mothurOut(count + "\t" + toString(stress1)); m->mothurOutEndLine(); }
333 catch(exception& e) {
334 m->errorOut(e, "NMDSCommand", "generateStartingConfiguration");
339 //**********************************************************************************************************************
340 //generate random config
341 vector< vector<double> > NMDSCommand::generateStartingConfiguration(int numNames, int dimension) {
343 vector< vector<double> > axes; axes.resize(dimension);
344 for (int i = 0; i < axes.size(); i++) { axes[i].resize(numNames); }
346 //generate random number between -1 and 1, precision 6
347 for (int i = 0; i < axes.size(); i++) {
348 for (int j = 0; j < axes[i].size(); j++) {
350 if (m->control_pressed) { return axes; }
352 //generate random int between 0 and 99999
353 int myrand = (int)((float)(rand()) / ((RAND_MAX / 99998) + 1));
355 //generate random sign
356 int mysign = (int)((float)(rand()) / ((RAND_MAX / 99998) + 1));
358 //if mysign is even then sign = positive, else sign = negative
359 if ((mysign % 2) == 0) { mysign = 1.0; }
360 else { mysign = -1.0; }
362 axes[i][j] = mysign * myrand / (float) 100000;
368 catch(exception& e) {
369 m->errorOut(e, "NMDSCommand", "generateStartingConfiguration");
373 //**********************************************************************************************************************
374 //normalize configuration
375 int NMDSCommand::normalizeConfiguration(vector< vector<double> >& axes, int numNames, int dimension) {
377 vector<double> averageAxes; averageAxes.resize(dimension, 0.0);
380 for (int i = 0; i < axes.size(); i++) {
381 for (int j = 0; j < axes[i].size(); j++) { averageAxes[i] += axes[i][j]; }
383 averageAxes[i] /= (float) numNames;
387 double sumDenom = 0.0;
388 for (int i = 0; i < axes.size(); i++) {
389 for (int j = 0; j < axes[i].size(); j++) {
390 sumDenom += ((axes[i][j] - averageAxes[i]) * (axes[i][j] - averageAxes[i]));
394 double denom = sqrt((sumDenom / (float) (axes.size() * numNames)));
396 for (int i = 0; i < axes.size(); i++) {
397 for (int j = 0; j < axes[i].size(); j++) {
398 axes[i][j] = (axes[i][j] - averageAxes[i]) / denom;
404 catch(exception& e) {
405 m->errorOut(e, "NMDSCommand", "normalizeConfiguration");
409 //**********************************************************************************************************************
411 vector< vector<double> > NMDSCommand::getConfiguration(vector< vector<double> >& axes, int dimension) {
413 vector< vector<double> > newAxes; newAxes.resize(dimension);
415 for (int i = 0; i < dimension; i++) {
416 newAxes[i] = axes[i];
421 catch(exception& e) {
422 m->errorOut(e, "NMDSCommand", "getConfiguration");
426 //**********************************************************************************************************************
427 //find raw stress, and normalize using
428 double NMDSCommand::calculateStress(vector< vector<double> >& matrix, vector< vector<double> >& config) {
430 double normStress = 0.0;
432 double rawStress = 0.0;
435 for (int i = 0; i < matrix.size(); i++) {
436 for (int j = 0; j < matrix[i].size(); j++) {
437 if (m->control_pressed) { return normStress; }
439 rawStress += ((matrix[i][j] - config[i][j]) * (matrix[i][j] - config[i][j]));
440 denom += (config[i][j] * config[i][j]);
445 if ((rawStress != 0.0) && (denom != 0.0)) {
446 normStress = sqrt((rawStress / denom));
451 catch(exception& e) {
452 m->errorOut(e, "NMDSCommand", "calculateStress");
457 //**********************************************************************************************************************
458 int NMDSCommand::output(vector< vector<double> >& config, vector<string>& names, ofstream& out) {
461 for (int i = 0; i < names.size(); i++) {
463 out << names[i] << '\t';
465 for (int j = 0; j < config.size(); j++) {
466 out << config[j][i] << '\t';
476 catch(exception& e) {
477 m->errorOut(e, "NMDSCommand", "output");
481 /*****************************************************************/
482 vector< vector<double> > NMDSCommand::readAxes(vector<string> names){
485 m->openInputFile(axesfile, in);
487 string headerLine = m->getline(in); m->gobble(in);
489 //count the number of axis you are reading
493 int pos = headerLine.find("axis");
494 if (pos != string::npos) {
496 headerLine = headerLine.substr(pos+4);
497 }else { done = true; }
500 if (maxdim > count) {
501 m->mothurOut("You requested maxdim = " + toString(maxdim) + ", but your file only includes " + toString(count) + ". Using " + toString(count) + "."); m->mothurOutEndLine();
503 if (maxdim < mindim) { m->mothurOut("Also adjusting mindim to " + toString(maxdim-1) + "."); m->mothurOutEndLine(); }
506 vector< vector<double> > axes; axes.resize(maxdim);
507 for (int i = 0; i < axes.size(); i++) { axes[i].resize(names.size(), 0.0); }
509 map <string, vector<double> > orderedAxes;
510 map <string, vector<double> >::iterator it;
514 if (m->control_pressed) { in.close(); return axes; }
517 in >> group; m->gobble(in);
520 if (!m->inUsersGroups(group, names)) { ignore = true; m->mothurOut(group + " is in your axes file and not in your distance file, ignoring."); m->mothurOutEndLine(); }
522 vector<double> thisGroupsAxes;
523 for (int i = 0; i < count; i++) {
527 //only save the axis we want
528 if (i < maxdim) { thisGroupsAxes.push_back(temp); }
531 if (!ignore) { orderedAxes[group] = thisGroupsAxes; }
538 if (names.size() != orderedAxes.size()) { m->mothurOut("[ERROR]: your axes file does not match your distance file, aborting."); m->mothurOutEndLine(); m->control_pressed = true; return axes; }
540 //put axes info in same order as distance file, just in case
541 for (int i = 0; i < names.size(); i++) {
542 it = orderedAxes.find(names[i]);
544 if (it != orderedAxes.end()) {
545 vector<double> thisGroupsAxes = it->second;
547 for (int j = 0; j < thisGroupsAxes.size(); j++) {
548 axes[j][i] = thisGroupsAxes[j];
551 }else { m->mothurOut("[ERROR]: your axes file does not match your distance file, aborting."); m->mothurOutEndLine(); m->control_pressed = true; return axes; }
556 catch(exception& e) {
557 m->errorOut(e, "NMDSCommand", "readAxes");
561 /**********************************************************************************************************************
562 vector< vector<double> > NMDSCommand::calculateStressGradientVector(vector<seqDist>& eDists, vector<seqDist>& D, double rawStress, double stress, vector< vector<double> >& axes) {
564 vector< vector<double> > gradient; gradient.resize(dimension);
565 for (int i = 0; i < gradient.size(); i++) { gradient[i].resize(axes[0].size(), 0.0); }
568 for (int i = 0; i < eDists.size(); i++) { sumDij += (eDists[i].dist * eDists[i].dist); }
570 for (int i = 0; i < eDists.size(); i++) {
572 for (int j = 0; j < dimension; j++) {
574 if (m->control_pressed) { return gradient; }
576 double firstTerm1 = (stress / rawStress) * (eDists[i].dist - D[i].dist);
577 double firstTerm2 = (stress / sumDij) * eDists[i].dist;
578 double firstTerm = firstTerm1 - firstTerm2;
580 float r = (dimension-1.0);
581 double temp = 1.0 / (pow(eDists[i].dist, r));
582 float absTemp = abs(axes[j][eDists[i].seq1] - axes[j][eDists[i].seq2]);
583 double secondTerm = pow(absTemp, r) * temp;
586 if ((axes[j][eDists[i].seq1] - axes[j][eDists[i].seq2]) == 0) { sigNum = 0.0; }
587 else if ((axes[j][eDists[i].seq1] - axes[j][eDists[i].seq2]) < 0) { sigNum = -1.0; }
589 double results = (firstTerm * secondTerm * sigNum);
590 cout << i << '\t' << j << '\t' << "results = " << results << endl;
591 gradient[j][eDists[i].seq1] += results;
592 gradient[j][eDists[i].seq2] -= results;
598 catch(exception& e) {
599 m->errorOut(e, "NMDSCommand", "calculateStressGradientVector");
603 //**********************************************************************************************************************
604 double NMDSCommand::calculateMagnitude(vector< vector<double> >& gradient) {
606 double magnitude = 0.0;
609 for (int i = 0; i < gradient.size(); i++) {
610 for (int j = 0; j < gradient[i].size(); j++) {
611 sum += (gradient[i][j] * gradient[i][j]);
615 magnitude = sqrt(((1.0/(float)gradient[0].size()) * sum));
619 catch(exception& e) {
620 m->errorOut(e, "NMDSCommand", "calculateMagnitude");
624 //**********************************************************************************************************************
625 //described in Kruskal paper page 121 + 122
626 double NMDSCommand::calculateStep(vector< vector<double> >& prevGrad, vector< vector<double> >& grad, vector<double>& prevStress) {
628 double newStep = step;
632 double sumDenom1 = 0.0;
633 double sumDenom2 = 0.0;
634 for (int i = 0; i < prevGrad.size(); i++) {
635 for (int j = 0; j < prevGrad[i].size(); j++) {
636 sumDenom1 += (grad[i][j] * grad[i][j]);
637 sumDenom2 += (prevGrad[i][j] * prevGrad[i][j]);
638 sumNum += (grad[i][j] * prevGrad[i][j]);
642 double cosTheta = sumNum / (sqrt(sumDenom1) * sqrt(sumDenom2));
643 cosTheta *= cosTheta;
646 double angle = pow(4.0, cosTheta);
649 double currentStress = prevStress[prevStress.size()-1];
650 double lastStress = prevStress[0];
651 if (prevStress.size() > 1) { lastStress = prevStress[prevStress.size()-2]; }
652 double fivePrevStress = prevStress[0];
653 if (prevStress.size() > 5) { fivePrevStress = prevStress[prevStress.size()-6]; }
655 double fiveStepRatio = min(1.0, (currentStress / fivePrevStress));
657 //calc relaxation factor
658 double relaxation = 1.3 / (1.0 + pow(fiveStepRatio, 5.0));
660 //calc good luck factor
661 double goodLuck = min(1.0, (currentStress / lastStress));
664 //cout << "\ncos = " << cosTheta << " step = " << step << " angle = " << angle << " relaxation = " << relaxation << " goodluck = " << goodLuck << endl;
665 newStep = step * angle * relaxation * goodLuck;
669 catch(exception& e) {
670 m->errorOut(e, "NMDSCommand", "calculateStep");
674 //**********************************************************************************************************************
675 vector< vector<double> > NMDSCommand::calculateNewConfiguration(double magnitude, vector< vector<double> >& axes, vector< vector<double> >& gradient) {
678 vector< vector<double> > newAxes = axes;
680 for (int i = 0; i < newAxes.size(); i++) {
682 if (m->control_pressed) { return newAxes; }
684 for (int j = 0; j < newAxes[i].size(); j++) {
685 newAxes[i][j] = axes[i][j] + ((step / magnitude) * gradient[i][j]);
691 catch(exception& e) {
692 m->errorOut(e, "NMDSCommand", "calculateNewConfiguration");
696 /**********************************************************************************************************************
697 //adjust eDists so that it creates monotonically increasing series of succesive values that increase or stay the same, but never decrease
698 vector<seqDist> NMDSCommand::satisfyMonotonicity(vector<seqDist> eDists, vector<int> partitions) {
701 //find averages of each partitions
702 vector<double> sums; sums.resize(partitions.size(), 0.0);
703 vector<int> sizes; sizes.resize(partitions.size(), 0);
705 for (int i = 0; i < partitions.size(); i++) {
706 //i is not the last one
707 int start = partitions[i];
709 if (i != (partitions.size()-1)) { end = partitions[i+1]; }
710 else{ end = eDists.size(); }
712 for (int j = start; j < end; j++) { sums[i] += eDists[j].dist; }
714 sizes[i] = (end - start);
718 vector<seqDist> D = eDists;
720 //i represents the "active block"
722 while (i < partitions.size()) {
724 if (m->control_pressed) { return D; }
726 bool upActive = true;
727 bool upSatisfied = false;
728 bool downSatisfied = false;
730 //while we are not done with this block
731 while ((!upSatisfied) || (!downSatisfied)) {
735 //are we are upSatisfied? - is the average of the next block greater than mine?
736 if (i != (partitions.size()-1)) { //if we are the last guy then we are upsatisfied
737 if ((sums[i+1]/(float)sizes[i+1]) >= (sums[i]/(float)sizes[i])) {
741 //find new weighted average
742 double newSum = sums[i] + sums[i+1];
744 //merge blocks - putting everything in i
746 sizes[i] += sizes[i+1];
747 partitions[i] = partitions[i+1];
749 sums.erase(sums.begin()+(i+1));
750 sizes.erase(sizes.begin()+(i+1));
751 partitions.erase(partitions.begin()+(i+1));
755 }else { upSatisfied = true; upActive = false; }
759 //are we are DownSatisfied? - is the average of the previous block less than mine?
760 if (i != 0) { //if we are the first guy then we are downSatisfied
761 if ((sums[i-1]/(float)sizes[i-1]) <= (sums[i]/(float)sizes[i])) {
762 downSatisfied = true;
765 //find new weighted average
766 double newSum = sums[i] + sums[i-1];;
768 //merge blocks - putting everything in i-1
770 sizes[i-1] += sizes[i];
772 sums.erase(sums.begin()+i);
773 sizes.erase(sizes.begin()+i);
774 partitions.erase(partitions.begin()+i);
779 }else { downSatisfied = true; upActive = true; }
783 i++; // go to next block
786 //sanity check - for rounding errors
787 vector<double> averages; averages.resize(sums.size(), 0.0);
788 for (int i = 0; i < sums.size(); i++) { averages[i] = sums[i] / (float) sizes[i]; }
789 for (int i = 0; i < averages.size(); i++) { if (averages[i+1] < averages[i]) { averages[i+1] = averages[i]; } }
793 for (int i = 0; i < averages.size(); i++) {
794 for (int j = 0; j < sizes[i]; j++) {
795 D[placeHolder].dist = averages[i];
802 catch(exception& e) {
803 m->errorOut(e, "NMDSCommand", "satisfyMonotonicity");
808 //**********************************************************************************************************************