MorphoGraphX
PCAnalysis.hpp
1 //
2 // This file is part of MorphoGraphX - http://www.MorphoGraphX.org
3 // Copyright (C) 2012-2015 Richard S. Smith and collaborators.
4 //
5 // If you use MorphoGraphX in your work, please cite:
6 // http://dx.doi.org/10.7554/eLife.05864
7 //
8 // MorphoGraphX is free software, and is licensed under under the terms of the
9 // GNU General (GPL) Public License version 2.0, http://www.gnu.org/licenses.
10 //
11 #ifndef PCAnalysis_HPP
12 #define PCAnalysis_HPP
13 
14 #include <Process.hpp>
15 
16 #include <Information.hpp>
17 
18 #include <QRegExp>
19 #include <QString>
20 #include <QStringList>
21 
22 namespace mgx {
23 namespace process {
24 
33 class PCAnalysis : public GlobalProcess {
34 public:
36  : Process(process)
37  , GlobalProcess(process)
38  {
39  }
40 
41  bool operator()(const QStringList& parms)
42  {
43  if(!checkState().stack())
44  return false;
45  Stack* stk = currentStack();
46  Store* store = stk->currentStore();
47  QString fn = parms[0];
48  QString correction = parms[1].trimmed();
49  Point3f correcting_factor;
50  if(correction == "Ellipsoid")
51  correcting_factor = sqrt(5);
52  else if(correction == "Cuboid")
53  correcting_factor = sqrt(3);
54  else if(correction == "Elliptical Cylinder")
55  correcting_factor = Point3f(sqrt(3), sqrt(4), sqrt(4));
56  else if(correction == "Maximum Span")
57  correcting_factor = 0;
58  else if(correction.endsWith('%')) {
59  QString cor = correction.left(correction.size() - 1);
60  bool ok;
61  float cr = cor.toFloat(&ok);
62  if(ok) {
63  correcting_factor = -cr / 100.f;
64  } else {
65  setErrorMessage(QString("Error, percentage value is not valid: '%1'").arg(cor));
66  return false;
67  }
68  } else {
69  bool ok;
70  float cr = correction.toFloat(&ok);
71  if(ok)
72  correcting_factor = cr;
73  else {
74  QStringList vs = correction.split(QRegExp("[ ,-;:-]"));
75  if(vs.size() == 3) {
76  correcting_factor.x() = vs[0].toFloat(&ok);
77  if(!ok) {
78  setErrorMessage(QString("Invalid x value for correction factor: '%1'.").arg(vs[0]));
79  return false;
80  }
81  correcting_factor.y() = vs[1].toFloat(&ok);
82  if(!ok) {
83  setErrorMessage(QString("Invalid y value for correction factor: '%1'.").arg(vs[1]));
84  return false;
85  }
86  correcting_factor.z() = vs[2].toFloat(&ok);
87  if(!ok) {
88  setErrorMessage(QString("Invalid z value for correction factor: '%1'.").arg(vs[2]));
89  return false;
90  }
91  } else {
92  setErrorMessage(QString("Invalid correction string '%1', expected one of 'Ellipsoid'"
93  ", 'Cuboid', 'Elliptical Cylinder', Maximum Span', a percentage, a single "
94  "value or three values"));
95  return false;
96  }
97  }
98  }
99  bool draw_result = false;
100  bool res
101  = operator()(stk, store, fn, parms[3].toInt(), parms[2], correcting_factor, parms[4].toInt(), draw_result);
102  if(res and draw_result) {
103  Mesh* m = mesh(stk->id());
104  m->showSurface();
105  m->showLabel();
106  }
107  return res;
108  }
109 
124  bool operator()(Stack* stack, Store* store, QString filename, int treshold, QString shape,
125  Point3f correcting_factor, int slices, bool& draw_result);
126 
127  QString folder() const {
128  return "Shape Analysis";
129  }
130  QString name() const {
131  return "PCAnalysis";
132  }
134  {
135  return "Compute the principle components of the image. If the threshold is -1, then all the values are used, \n"
136  "as is. If 'Draw Result' is set to true, the current mesh will be erased and replaced with shapes \n"
137  "representing the cells fit. 'Splan Correction' can be either a shape, a single value of a vector \n"
138  "of 3 values, corresponding to the correction to apply for the eigen-values on all three directions.";
139  }
141  {
142  return QStringList() << "Output"
143  << "Span Correction"
144  << "Draw Result"
145  << "Threshold"
146  << "Shape Details";
147  }
149  {
150  return QStringList() << "File to write the output to"
151  << "Span correction can be made using pre-computed formula for regular shapes, or a "
152  "percentage of the PC."
153  << "Shape used to display the result"
154  << "If the stack is not labeled, a single volume is considered with all voxels of "
155  "intensity greater than this threshold."
156  << "How finely to draw the shape (for cylinders or ellipsoids)";
157  }
159  {
160  return QStringList() << "output.csv"
161  << "Ellipsoid"
162  << "No"
163  << "100"
164  << "3";
165  }
167  {
168  ParmChoiceMap map;
169  map[1] = QStringList() << "Ellipsoid"
170  << "Cuboid"
171  << "Elliptical Cylinder"
172  << "Maximum Span";
173  map[2] = QStringList() << "No"
174  << "Ellipsoids"
175  << "Cuboids"
176  << "Cylinder";
177  return map;
178  }
179  QIcon icon() const {
180  return QIcon(":/images/PCAnalysis.png");
181  }
182 };
183 } // namespace process
184 } // namespace mgx
185 
186 #endif // PCAnalysis_HPP
QIcon icon() const
Icon to use to represent the process in the GUI.
Definition: PCAnalysis.hpp:179
void showSurface()
Show the surface to the user.
Definition: Mesh.hpp:801
QString folder() const
Folder in which to place the process.
Definition: PCAnalysis.hpp:127
int id() const
Id of a stack.
Definition: Stack.hpp:59
split(const QString &sep, SplitBehavior behavior=KeepEmptyParts, Qt::CaseSensitivity cs=Qt::CaseSensitive)
const Store * currentStore() const
Returns the current store.
Definition: Stack.hpp:122
CU_HOST_DEVICE void x(const T &v)
Short access to the first element.
Definition: Vector.hpp:661
Process()
Default constructor.
QStringList parmDescs() const
List of parameters descriptions.
Definition: PCAnalysis.hpp:148
CheckState checkState()
Call this function and convert the result to a boolean.
The Store class holds the actual 3D data and properties specific to it.
Definition: Store.hpp:35
This class holds the actual mesh as a VV Graph and all sort of properties for it, including visualiza...
Definition: Mesh.hpp:177
endsWith(const QString &s, Qt::CaseSensitivity cs=Qt::CaseSensitive)
CU_HOST_DEVICE void y(const T &v)
Short access to the second element.
Definition: Vector.hpp:670
This namespace contains all the API of MorphoGraphX.
Definition: Assert.hpp:26
bool setErrorMessage(const QString &str)
Set an error message that will be displayed if the process returns false.
QStringList parmDefaults() const
List of default parms.
Definition: PCAnalysis.hpp:158
void showLabel()
Show the labels of the surface triangles.
Definition: Mesh.hpp:820
QString name() const
Returns the name of the process.
Definition: PCAnalysis.hpp:130
File containing the definition of a Process.
The Stack class represent the dimensions of the 3D data, and the frames transformations.
Definition: Stack.hpp:35
QStringList parmNames() const
List of named parameters.
Definition: PCAnalysis.hpp:140
Global processes have full mutable access to all properties of the process.
Definition: Process.hpp:904
bool operator()(const QStringList &parms)
Implementation of the process with generic arguments.
Definition: PCAnalysis.hpp:41
This namespace contains all the classes needed to define processes.
CU_HOST_DEVICE void z(const T &v)
Short access to the third element.
Definition: Vector.hpp:679
ParmChoiceMap parmChoice() const
Purely for GUI purposes, provides for some of the parms parameter a choice.
Definition: PCAnalysis.hpp:166
toFloat(bool *ok=0)
left(int n)
QString description() const
Returns a description of the process for the GUI.
Definition: PCAnalysis.hpp:133
Perform a PCA on each labeled regions of the segmented stack, and create shapes in the mesh reflectin...
Definition: PCAnalysis.hpp:33