File: BrainSetAutoLoaderFileMetricByNode.cxx

package info (click to toggle)
caret 5.6.4~dfsg.1-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd, wheezy
  • size: 31,904 kB
  • ctags: 28,901
  • sloc: cpp: 378,050; python: 6,718; ansic: 5,507; makefile: 333; sh: 46
file content (191 lines) | stat: -rw-r--r-- 6,389 bytes parent folder | download
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

#include <iostream>

#include <QDir>
#include <QFile>
#include <QRegExp>
#include <QStringList>

#include "BrainModelSurface.h"
#include "BrainModelSurfaceNodeColoring.h"
#include "BrainModelSurfaceOverlay.h"
#include "BrainSet.h"
#include "BrainSetAutoLoaderFileMetricByNode.h"
#include "DebugControl.h"
#include "DisplaySettingsMetric.h"
#include "MetricFile.h"
#include "StringUtilities.h"
#include "VolumeFile.h"
#include "VoxelIJK.h"
/**
 *  constructor
 */
BrainSetAutoLoaderFileMetricByNode::BrainSetAutoLoaderFileMetricByNode(BrainSet* bsIn,
                                          const int autoLoaderIndexIn)
   : BrainSetAutoLoaderFileMetric(bsIn, autoLoaderIndexIn)
{

}

/**
 * destructor.
 */
BrainSetAutoLoaderFileMetricByNode::~BrainSetAutoLoaderFileMetricByNode()
{

}

/**
 * auto load the metric file for the specified node number (return error message).
 */
QString
BrainSetAutoLoaderFileMetricByNode::loadFileForNode(const int nodeNumber)
{
   QString errorMessage = "";
   MetricFile* mf = brainSet->getMetricFile();
   int lastCol = this->getAutoLoadLastMetricColumnNumber();
   if (this->getAutoLoadReplaceLastFileEnabled() &&
       (lastCol >= 0) &&
       (lastCol < mf->getNumberOfColumns())) {
      mf->removeColumn(lastCol);
      this->setAutoLoadLastMetricColumnNumber(-1);
      lastCol = -1;
   }

   int autoLoadColumnNumber = -1;
   const QString directoryName =
      this->getAutoLoadDirectoryName();
   if (QFile::exists(directoryName) == false) {
      errorMessage +=
        "Auto Load Metric selected but directory (" + directoryName + ") is invalid.\n";
   }
   else {
      const QString fileNamePrefix = QString::number(nodeNumber);

      const QString reString("(\\d+)_(.*)."
                    + SpecFile::getMetricFileExtension());
      QRegExp re(reString, Qt::CaseInsensitive);

      QString metricFileNameToLoad = "";
      QDir dir(directoryName);
      QStringList fileList = dir.entryList(QDir::Files);
      int fileCounter = 0;
      for (int ni = 0; ni < fileList.size(); ni++) {
         const QString fileName = fileList.at(ni);

         if (re.exactMatch(fileName)) {
            QStringList matches = re.capturedTexts();
            if (matches.size() >= 3) {
               //
               // Note: zero'th capture is the entire string
               //
               const int fileNodeNumber = matches.at(1).toInt();
               if (nodeNumber == fileNodeNumber) {
                  const QString fullPath =
                     directoryName + "/" + fileName;
                  metricFileNameToLoad = fullPath;
                  break;
               }
            }
         }
         fileCounter++;
      }

      //std::cout << "Searched " << fileCounter << " files." << std::endl;
      QString columnName = "";
      if (metricFileNameToLoad.isEmpty() == false) {
         if (DebugControl::getDebugOn()) {
            std::cout << "Load: "
                      << metricFileNameToLoad.toAscii().constData()
                      << std::endl;
         }
         try {
            int col = -1;
            int lastCol = this->getAutoLoadLastMetricColumnNumber();
            if (this->getAutoLoadReplaceLastFileEnabled() &&
                (lastCol >= 0) &&
                (lastCol < brainSet->getMetricFile()->getNumberOfColumns())) {
               std::vector<int> columnDestination;
               columnDestination.push_back(lastCol);
               std::vector<QString> columnNames;
               columnNames.push_back("");
               brainSet->readMetricFile(metricFileNameToLoad,
                                  columnDestination,
                                  columnNames,
                                  AbstractFile::FILE_COMMENT_MODE_LEAVE_AS_IS,
                                  false);
               col = lastCol;
            }
            else {
               brainSet->readMetricFile(metricFileNameToLoad,
                                  true,
                                  false);
               col = brainSet->getMetricFile()->getNumberOfColumns() - 1;
            }
            BrainModelSurfaceOverlay* metricOverlay = NULL;
            int overlayIndex = 3 - this->autoLoaderIndex;
            if ((overlayIndex >= 0) &&
                (overlayIndex < brainSet->getNumberOfSurfaceOverlays())) {
               metricOverlay = brainSet->getSurfaceOverlay(overlayIndex);
            }

            if (metricOverlay != NULL) {
               BrainModelSurface* displaySurface = this->getAutoLoadMetricDisplaySurface();
               if (displaySurface != NULL) {
                  const int surfaceIndex = brainSet->getBrainModelIndex(displaySurface);
                  metricOverlay->setOverlay(surfaceIndex, BrainModelSurfaceOverlay::OVERLAY_METRIC);
                  metricOverlay->setDisplayColumnSelected(surfaceIndex, col);
                  metricOverlay->setThresholdColumnSelected(surfaceIndex, col);
               }
            }
            autoLoadColumnNumber = col;
            columnName = brainSet->getMetricFile()->getColumnName(col);
         }
         catch (FileException e) {
            errorMessage += (e.whatQString() + "\n");
         }
      }
      else {
         errorMessage = ("No metric file for node "
                         + QString::number(nodeNumber) + "\n");
      }

      //if (DebugControl::getDebugOn()) {
          std::cout << "Node Number: "
                    << nodeNumber
                    << ", file: "
                    << metricFileNameToLoad.toAscii().constData()
                    << ", column-name: "
                    << columnName.toAscii().constData()
                    << std::endl;
      //}

   }

   if (autoLoadColumnNumber >= 0) {
       this->setAutoLoadLastMetricColumnNumber(autoLoadColumnNumber);
   }
   brainSet->getDisplaySettingsMetric()->update();
   brainSet->getNodeColoring()->assignColors();
   brainSet->clearAllDisplayLists();

   return errorMessage;
}

/**
 * is auto loading metric file by node supported for this auto loader
 */
bool
BrainSetAutoLoaderFileMetricByNode::isLoadFileForNodeSupported() const
{
    return true;
}

/**
 * Auto load the metric file for the specified voxel (return error message).
 */
QString
BrainSetAutoLoaderFileMetricByNode::loadFileForVoxel(const VoxelIJK& voxel)
{
    return "Auto-Loading files by voxel not suppored.";
}