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 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686
|
/*LICENSE_START*/
/*
* Copyright 1995-2002 Washington University School of Medicine
*
* http://brainmap.wustl.edu
*
* This file is part of CARET.
*
* CARET is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* CARET 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.
*
* You should have received a copy of the GNU General Public License
* along with CARET; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
/*LICENSE_END*/
#include <algorithm>
#include <cmath>
#include <iostream>
#include <stack>
#include <QStringList>
#include "CommandMetricMathPostfix.h"
#include "FileFilters.h"
#include "FileUtilities.h"
#include "MathUtilities.h"
#include "MetricFile.h"
#include "ProgramParameters.h"
#include "ScriptBuilderParameters.h"
/**
* constructor.
*/
CommandMetricMathPostfix::CommandMetricMathPostfix(const QString& operationSwitchIn,
const QString& shortDescriptionIn)
: CommandBase(operationSwitchIn,
shortDescriptionIn)
{
whitespace = " \t\n\r";
metricColumnIdentifierName = "at-sign";
metricColumnIdentifierCharacter = "@";
metricFileColumnSeparatorName = "colon";
metricFileColumnSeparatorCharacter = ":";
}
/**
* destructor.
*/
CommandMetricMathPostfix::~CommandMetricMathPostfix()
{
}
/**
* get the script builder parameters.
*/
void
CommandMetricMathPostfix::getScriptBuilderParameters(ScriptBuilderParameters& paramsOut) const
{
paramsOut.clear();
paramsOut.addFile("Input Metric File Name", FileFilters::getMetricFileFilter());
paramsOut.addFile("Output Metric File Name", FileFilters::getMetricFileFilter());
paramsOut.addString("Output Metric Column Name/Number");
paramsOut.addVariableListOfParameters("Expression");
}
/**
* get full help information.
*/
QString
CommandMetricMathPostfix::getHelpInformation() const
{
QString helpInfo =
(indent3 + getShortDescription() + "\n"
+ indent6 + parameters->getProgramNameWithoutPath() + " " + getOperationSwitch() + " \n"
+ indent9 + "<input-metric-file-name>\n"
+ indent9 + "<output-metric-file-name>\n"
+ indent9 + "<output-column-name-number>\n"
+ indent9 + "<postfix-expression-within-double-quotes>\n"
+ indent9 + "\n"
+ indent9 + "Perform mathematical operations on a metric file. The \n"
+ indent9 + "mathematical expression must be in post-fix (reverse \n"
+ indent9 + "polish notation). See \n"
+ indent9 + " http://en.wikipedia.org/wiki/Reverse_Polish_notation\n"
+ indent9 + "\n"
+ indent9 + "The postfix expression must be in double quotes. Otherwise,\n"
+ indent9 + "operators, such as \"*\" will match all files in the \n"
+ indent9 + "current directory.\n"
+ indent9 + "\n"
+ indent9 + "A metric column is identified by a sequence of characters\n"
+ indent9 + "the are immediately proceeded and immediately followed by\n"
+ indent9 + "an " + metricColumnIdentifierName + " (" + metricColumnIdentifierCharacter + "). \n"
+ indent9 + "\n"
+ indent9 + "These characters are either the name of the metric column or\n"
+ indent9 + "the number of the metric column which starts at one. To use\n"
+ indent9 + "a metric column that is in a metric file other than the input\n"
+ indent9 + "metric file, start with an " + metricColumnIdentifierName + ", followed by the name of\n"
+ indent9 + "the metric file, followed by a " + metricFileColumnSeparatorName + " (" + metricFileColumnSeparatorCharacter + "), \n"
+ indent9 + "followed by the identifier of the metric column, and lastly,\n"
+ indent9 + "an " + metricColumnIdentifierName + ".\n"
+ indent9 + "\n"
+ indent9 + "Spaces are allowed in file names or column identifiers:\n"
+ indent9 + " Example \"@test file::subject 2@\"\n"
+ indent9 + "\n"
+ indent9 + "Examples (for the column named \"activation\" located in:\n"
+ indent9 + "the file named \"experiment.metric\"\n"
+ indent9 + " " + metricColumnIdentifierCharacter + "activation" + metricColumnIdentifierCharacter + "\n"
+ indent9 + " " + metricColumnIdentifierCharacter + "experiment.metric::activation" + metricColumnIdentifierCharacter + "\n"
+ indent9 + "\n"
+ indent9 + "If the output metric column is a name and it does not exist\n"
+ indent9 + "it will be created.\n"
+ indent9 + "\n"
+ indent9 + "Binary operators supported are:\n"
+ indent9 + " + addition\n"
+ indent9 + " - subtraction\n"
+ indent9 + " * multiplication\n"
+ indent9 + " / division\n"
+ indent9 + " ^ exponention\n"
+ indent9 + " max2 maximum-value\n"
+ indent9 + " min2 minimum-value\n"
+ indent9 + "\n"
+ indent9 + "Unary operations supported are:\n"
+ indent9 + " abs absolute-value\n"
+ indent9 + " exp exponential function\n"
+ indent9 + " flipsign flip the sign\n"
+ indent9 + " log natural log\n"
+ indent9 + " log2 base 2 logarithm\n"
+ indent9 + " log10 base 10 logarithm\n"
+ indent9 + " sqrt square root\n"
+ indent9 + "\n"
+ indent9 + "Predefined values from each nodes metric values\n"
+ indent9 + " nodeavg Average value at each node\n"
+ indent9 + " nodemax Maximum value at each node \n"
+ indent9 + " nodemin Minimum value at each node \n"
+ indent9 + " nodesum Sum of values at each node \n"
+ indent9 + "\n"
+ indent9 + "Example: \"5 1 2 + 4 * + 3 -\" \n"
+ indent9 + " Infix => ((1 + 2) * 4 + 5 - 3)\n"
+ indent9 + " evaluates to 14.\n"
+ indent9 + "Example: \"2 3 * 2 3 + min2\" \n"
+ indent9 + " Infix => min2(2 * 3, 2 + 3)\n"
+ indent9 + " evaluates to 5.\n"
+ indent9 + "Example: \"" + metricColumnIdentifierCharacter + "one" + metricColumnIdentifierCharacter
+ indent9 + " " + metricColumnIdentifierCharacter + "two" + metricColumnIdentifierCharacter + " add\" \n"
+ indent9 + " adds the columns named \"one\" and \"two\"."
+ indent9 + "\n");
return helpInfo;
}
/**
* execute the command.
*/
void
CommandMetricMathPostfix::executeCommand() throw (BrainModelAlgorithmException,
CommandException,
FileException,
ProgramParametersException,
StatisticException)
{
//
// Get the input and output file names
//
const QString inputMetricFileName =
parameters->getNextParameterAsString("Input Metric File Name");
const QString outputMetricFileName =
parameters->getNextParameterAsString("Output Metric File Name");
const QString outputColumnNameNumber =
parameters->getNextParameterAsString("Output Metric Column Name/Number");
const QString inputExpression =
parameters->getNextParameterAsString("Postfix Expression");
checkForExcessiveParameters();
//
// Get the mathematical expression
//
std::queue<QString> expression;
convertInputToQueueOfTokens(inputExpression,
expression);
if (expression.empty()) {
throw CommandException("No mathematical expression provided.");
}
//
// Process the postfix expression
//
processPostFixExpression(inputMetricFileName,
outputMetricFileName,
outputColumnNameNumber,
expression);
}
/**
* convert the input string into a queue of tokens.
*/
void
CommandMetricMathPostfix::convertInputToQueueOfTokens(const QString& inputString,
std::queue<QString>& tokensOut) const throw (CommandException)
{
//
// The input string should have each operand/operator separated by whitespace.
// Metric column identifiers may contain spaces so they need to be handled specially.
//
const int len = inputString.length();
QString token;
for (int i = 0; i < len; i++) {
const QString c(inputString[i]);
if (isWhiteSpace(c)) {
if (token.isEmpty() == false) {
tokensOut.push(token);
token.clear();
}
}
else if (c == metricColumnIdentifierCharacter) {
token += c;
bool done = false;
while (done == false) {
i++;
if (i >= len) {
throw CommandException("Metric column identifier is missing closing \""
+ metricColumnIdentifierCharacter
+ "\" \""
+ token
+ "\".");
}
const QString c(inputString[i]);
token += c;
if (c == metricColumnIdentifierCharacter) {
tokensOut.push(token);
token.clear();
done = true;
}
}
}
else {
token += c;
}
}
if (token.isEmpty() == false) {
tokensOut.push(token);
token.clear();
}
}
/**
* process the postfix expression.
* Algorithm from http://en.wikipedia.org/wiki/Reverse_Polish_notation
*/
void
CommandMetricMathPostfix::processPostFixExpression(const QString& inputMetricFileName,
const QString& outputMetricFileName,
const QString& outputMetricColumnNameOrNumber,
std::queue<QString>& postFixExpression) throw (CommandException)
{
//
// Read the metric file
//
MetricFile metricFile;
metricFile.readFile(inputMetricFileName);
//
// Check the number of nodes
//
numberOfNodes = metricFile.getNumberOfNodes();
if (numberOfNodes <= 0) {
throw CommandException("Input metric file contains no nodes.");
}
const int numberOfInputMetricColumns = metricFile.getNumberOfColumns();
//
// Find the output column (create if necessary)
//
const int outputColumnNumber = metricFile.getColumnFromNameOrNumber(outputMetricColumnNameOrNumber,
true);
//
// Stack used for processing the postfix expression
//
std::stack<float*> valueStack;
//
// Loop through the queue until it is empty
//
while (postFixExpression.empty() == false) {
const QString token = postFixExpression.front();
const int tokenLength = token.length();
postFixExpression.pop();
std::cout << "Processing: \"" << token.toAscii().constData() << "\"" << std::endl;
const QChar firstChar = token[0];
//
// Examing the first character
//
if (QString(firstChar) == metricColumnIdentifierCharacter) { // column name/number
//
// Remove metric column identifier at beginning and end of name
//
if (token.endsWith(metricColumnIdentifierCharacter) == false) {
throw CommandException("Invalid metric column identifier (missing closing "
+ metricColumnIdentifierCharacter
+ ")");
}
QString columnID = token.mid(1, tokenLength - 2);
//
// Is this a file name and column ID
//
QString columnFileName;
const int fileColumnSeparatorIndex = columnID.indexOf(metricFileColumnSeparatorCharacter);
if (fileColumnSeparatorIndex >= 0) {
columnFileName = columnID.left(fileColumnSeparatorIndex);
columnID = columnID.mid(fileColumnSeparatorIndex + 1);
if (columnFileName.isEmpty()) {
throw CommandException("Invalid metric column ID filename \""
+ token
+ "\"");
}
}
if (columnID.isEmpty()) {
throw CommandException("Invalid metric column ID \""
+ token
+ "\"");
}
std::cout << "column ID \""
<< columnID.toAscii().constData()
<< "\""
<< std::endl;
//
// Is data in a metric file that is not the input metric file
//
if (columnFileName.isEmpty() == false) {
MetricFile columnMetricFile;
columnMetricFile.readFile(columnFileName);
if (columnMetricFile.getNumberOfNodes() == numberOfNodes) {
//
// Push column value onto stack
//
try {
float* array = getArray();
const int columnNumber = columnMetricFile.getColumnFromNameOrNumber(columnID, false);
columnMetricFile.getColumnForAllNodes(columnNumber, array);
valueStack.push(array);
}
catch (FileException& e) {
throw CommandException(e);
}
}
else {
throw CommandException(FileUtilities::basename(columnFileName)
+ " has a different number of nodes than "
+ inputMetricFileName);
}
}
else {
//
// Push column value onto stack
//
try {
float* array = getArray();
const int columnNumber = metricFile.getColumnFromNameOrNumber(columnID, false);
metricFile.getColumnForAllNodes(columnNumber, array);
valueStack.push(array);
}
catch (FileException& e) {
throw CommandException(e);
}
}
}
else if (firstChar.isDigit()) { // a number
//
// Convert to a float
//
bool ok = false;
const float f = token.toFloat(&ok);
if (ok == false) {
throw CommandException("Invalid number " + token);
}
//
// Put the float in an array with size number of nodes
//
float* array = getArray();
for (int i = 0; i < numberOfNodes; i++) {
array[i] = f;
}
//
// Push onto the values stack
//
valueStack.push(array);
}
else if ((token == "abs") || // unary operators
(token == "exp") ||
(token == "flipsign") ||
(token == "log") ||
(token == "log2") ||
(token == "log10") ||
(token == "sqrt")) {
if (valueStack.size() < 1) {
throw CommandException("Invalid expression (insufficient operands) at " + token);
}
//
// Get the one column of data
//
float* f1 = valueStack.top();
valueStack.pop();
//
// Evaluate the expression
//
float* value = getArray();
if (token == "abs") {
for (int i = 0; i < numberOfNodes; i++) {
value[i] = std::fabs(f1[i]);
}
}
else if (token == "exp") {
for (int i = 0; i < numberOfNodes; i++) {
value[i] = std::exp(f1[i]);
}
}
else if (token == "flipsign") {
for (int i = 0; i < numberOfNodes; i++) {
value[i] = -f1[i];
}
}
else if (token == "log") {
for (int i = 0; i < numberOfNodes; i++) {
value[i] = std::log(f1[i]);
}
}
else if (token == "log2") {
for (int i = 0; i < numberOfNodes; i++) {
value[i] = MathUtilities::log(2.0, f1[i]);
}
}
else if (token == "log10") {
for (int i = 0; i < numberOfNodes; i++) {
value[i] = std::log10(f1[i]);
}
}
else if (token == "sqrt") {
for (int i = 0; i < numberOfNodes; i++) {
value[i] = std::sqrt(f1[i]);
}
}
else {
throw CommandException("PROGRAM ERROR unrecognized operator at line "
+ QString(__LINE__)
+ " in "
+ QString(__FILE__));
}
//
// put result back onto stack
//
valueStack.push(value);
//
// Release arrays
//
releaseArray(f1);
}
else if ((token == "+") || // binary operators
(token == "-") ||
(token == "*") ||
(token == "/") ||
(token == "^") ||
(token == "max2") ||
(token == "min2")) {
if (valueStack.size() < 2) {
throw CommandException("Invalid expression (insufficient operands) at " + token);
}
//
// Get the two column of data
//
float* f1 = valueStack.top();
valueStack.pop();
float* f2 = valueStack.top();
valueStack.pop();
//
// Evaluate the expression
//
float* value = getArray();
if (token == "+") {
for (int i = 0; i < numberOfNodes; i++) {
value[i] = f1[i] + f2[i];
}
}
else if (token == "-") {
for (int i = 0; i < numberOfNodes; i++) {
value[i] = f2[i] - f1[i];
}
}
else if (token == "*") {
for (int i = 0; i < numberOfNodes; i++) {
value[i] = f1[i] * f2[i];
}
}
else if (token == "/") {
for (int i = 0; i < numberOfNodes; i++) {
value[i] = f2[i] / f1[i];
}
}
else if (token == "^") {
for (int i = 0; i < numberOfNodes; i++) {
value[i] = std::pow(f2[i], f1[i]);
}
}
else if (token == "max2") {
for (int i = 0; i < numberOfNodes; i++) {
value[i] = std::max(f1[i], f2[i]);
}
}
else if (token == "min2") {
for (int i = 0; i < numberOfNodes; i++) {
value[i] = std::min(f1[i], f2[i]);
}
}
else {
throw CommandException("PROGRAM ERROR unrecognized operator at line "
+ QString(__LINE__)
+ " in "
+ QString(__FILE__));
}
//
// put result back onto stack
//
valueStack.push(value);
//
// Release arrays
//
releaseArray(f1);
releaseArray(f2);
}
else if ((token == "nodeavg") ||
(token == "nodemax") ||
(token == "nodemin") ||
(token == "nodesum")) {
//
// Evaluate the expression
//
float* value = getArray();
if (token == "nodeavg") {
for (int i = 0; i < numberOfNodes; i++) {
float sum = 0.0;
for (int j = 0; j < numberOfInputMetricColumns; j++) {
sum += metricFile.getValue(i, j);
}
value[i] = sum / static_cast<float>(numberOfInputMetricColumns);
}
}
else if (token == "nodemax") {
for (int i = 0; i < numberOfNodes; i++) {
float nodeMax = -std::numeric_limits<float>::max();
for (int j = 0; j < numberOfInputMetricColumns; j++) {
nodeMax = std::max(nodeMax, metricFile.getValue(i, j));
}
value[i] = nodeMax;
}
}
else if (token == "nodemin") {
for (int i = 0; i < numberOfNodes; i++) {
float nodeMin = std::numeric_limits<float>::max();
for (int j = 0; j < numberOfInputMetricColumns; j++) {
nodeMin = std::min(nodeMin, metricFile.getValue(i, j));
}
value[i] = nodeMin;
}
}
else if (token == "nodesum") {
for (int i = 0; i < numberOfNodes; i++) {
float sum = 0.0;
for (int j = 0; j < numberOfInputMetricColumns; j++) {
sum += metricFile.getValue(i, j);
}
value[i] = sum;
}
}
else {
throw CommandException("PROGRAM ERROR unrecognized operator at line "
+ QString(__LINE__)
+ " in "
+ QString(__FILE__));
}
//
// put result back onto stack
//
valueStack.push(value);
}
else {
throw CommandException("Invalid expression at " + token);
}
}
if (valueStack.size() != 1) {
throw CommandException("Invalid expression");
}
//
// Get the result array
//
float* resultArray = valueStack.top();
valueStack.pop();
metricFile.setColumnForAllNodes(outputColumnNumber,
resultArray);
//
// Free memory
//
releaseArray(resultArray);
//std::cout << "Result: " << valueStack.top() << std::endl;
//
// Write the output metric file
//
metricFile.writeFile(outputMetricFileName);
}
/**
* get an array with number of nodes elements.
*/
float*
CommandMetricMathPostfix::getArray()
{
//
// Is a previously used array available
//
if (arrayStorage.empty()) {
float* f = new float[numberOfNodes];
return f;
}
//
// Create a new array
//
float* f = arrayStorage.front();
arrayStorage.pop();
return f;
}
/**
* release an array.
*/
void
CommandMetricMathPostfix::releaseArray(float* f)
{
arrayStorage.push(f);
}
/**
* see if a character is a whitespace.
*/
bool
CommandMetricMathPostfix::isWhiteSpace(const QString& s) const
{
if (whitespace.indexOf(s) >= 0) {
return true;
}
return false;
}
|