MNE-CPP  0.1.9
A Framework for Electrophysiology
tfplot.cpp
Go to the documentation of this file.
1 //=============================================================================================================
34 //=============================================================================================================
35 // INCLUDES
36 //=============================================================================================================
37 
38 #include "tfplot.h"
39 
40 #include "helpers/colormap.h"
41 
42 //=============================================================================================================
43 // QT INCLUDES
44 //=============================================================================================================
45 
46 #include <QGridLayout>
47 #include <QGraphicsView>
48 #include <QGraphicsPixmapItem>
49 #include <QDebug>
50 
51 //=============================================================================================================
52 // EIGEN INCLUDES
53 //=============================================================================================================
54 
55 //=============================================================================================================
56 // USED NAMESPACES
57 //=============================================================================================================
58 
59 using namespace DISPLIB;
60 
61 //=============================================================================================================
62 // DEFINE MEMBER METHODS
63 //=============================================================================================================
64 
65 TFplot::TFplot(Eigen::MatrixXd tf_matrix,
66  qreal sample_rate,
67  qreal lower_frq,
68  qreal upper_frq,
69  ColorMaps cmap = Jet)
70 {
71  qreal max_frq = sample_rate/2.0;
72  qreal frq_per_px = max_frq/tf_matrix.rows();
73 
74  if(upper_frq > max_frq || upper_frq <= 0) upper_frq = max_frq;
75  if(lower_frq < 0 || lower_frq >= max_frq) lower_frq = 0;
76  if(upper_frq < lower_frq) {
77  qreal temp = upper_frq;
78  upper_frq = lower_frq;
79  lower_frq = temp;
80  }
81 
82  qint32 lower_px = floor(lower_frq / frq_per_px);
83  qint32 upper_px = floor(upper_frq / frq_per_px);
84 
85  Eigen::MatrixXd zoomed_tf_matrix = Eigen::MatrixXd::Zero(upper_px-lower_px, tf_matrix.cols());
86  //How to print to console here
87  //printf(("fff "+QString::number(zoomed_tf_matrix(12,12))).toUtf8().data());// << "; " << zoomed_tf_matrix.rows(2) << "; ";
88 
89  qint32 pxls = 0;
90  for(qint32 it = lower_px; it < upper_px; it++) {
91  zoomed_tf_matrix.row(pxls) = tf_matrix.row(it);
92  pxls++;
93  }
94 
95  //zoomed_tf_matrix = tf_matrix.block(tf_matrix.rows() - upper_px, 0, upper_px-lower_px, tf_matrix.cols());
96 
97  calc_plot(zoomed_tf_matrix, sample_rate, cmap, lower_frq, upper_frq);
98 }
99 
100 //=============================================================================================================
101 
102 TFplot::TFplot(Eigen::MatrixXd tf_matrix,
103  qreal sample_rate,
104  ColorMaps cmap = Jet)
105 {
106  calc_plot(tf_matrix, sample_rate, cmap, 0, 0);
107 }
108 
109 //=============================================================================================================
110 
111 void TFplot::calc_plot(Eigen::MatrixXd tf_matrix,
112  qreal sample_rate,
113  ColorMaps cmap,
114  qreal lower_frq = 0,
115  qreal upper_frq = 0)
116 {
117  //normalisation of the tf-matrix
118  qreal norm1 = tf_matrix.maxCoeff();
119  qreal mnorm = tf_matrix.minCoeff();
120  if(std::fabs(mnorm) > norm1) norm1 = mnorm;
121  tf_matrix /= norm1;
122 
123  //setup image
124  QImage * image_to_tf_plot = new QImage(tf_matrix.cols(), tf_matrix.rows(), QImage::Format_RGB32);
125 
126  //setup pixelcolors in image
127  QColor color;
128  for ( qint32 y = 0; y < tf_matrix.rows(); y++ ) {
129  for ( qint32 x = 0; x < tf_matrix.cols(); x++ ) {
130  switch (cmap) {
131  case Jet:
132  color.setRgb(ColorMap::valueToJet(std::fabs(tf_matrix(y, x))));
133  break;
134  case Hot:
135  color.setRgb(ColorMap::valueToHot(std::fabs(tf_matrix(y, x))));
136  break;
137  case HotNeg1:
138  color.setRgb(ColorMap::valueToHotNegative1(std::fabs(tf_matrix(y, x))));
139  break;
140  case HotNeg2:
141  color.setRgb(ColorMap::valueToHotNegative2(std::fabs(tf_matrix(y, x))));
142  break;
143  case Bone:
144  color.setRgb(ColorMap::valueToBone(std::fabs(tf_matrix(y, x))));
145  break;
146  case RedBlue:
147  color.setRgb(ColorMap::valueToRedBlue(std::fabs(tf_matrix(y, x))));
148  break;
149  }
150  image_to_tf_plot->setPixel(x, tf_matrix.rows() - 1 - y, color.rgb());
151  }
152  }
153 
154  *image_to_tf_plot = image_to_tf_plot->scaled(tf_matrix.cols(), tf_matrix.cols()/2, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
155  *image_to_tf_plot = image_to_tf_plot->scaledToWidth(/*0.9 **/ 1026, Qt::SmoothTransformation);
156  //image to pixmap
157  QGraphicsPixmapItem *tf_pixmap = new QGraphicsPixmapItem(QPixmap::fromImage(*image_to_tf_plot));
158  //tf_pixmap->setScale(100);
159  QGraphicsScene *tf_scene = new QGraphicsScene();
160  tf_scene->addItem(tf_pixmap);
161 
162  QImage * coeffs_image = new QImage(10, tf_matrix.rows(), QImage::Format_RGB32);
163  qreal norm = tf_matrix.maxCoeff();
164  for(qint32 it = 0; it < tf_matrix.rows(); it++) {
165  for ( qint32 x = 0; x < 10; x++ ) {
166  switch (cmap) {
167  case Jet:
168  color.setRgb(ColorMap::valueToJet(it*norm/tf_matrix.rows()));
169  break;
170  case Hot:
171  color.setRgb(ColorMap::valueToHot(it*norm/tf_matrix.rows()));
172  break;
173  case HotNeg1:
174  color.setRgb(ColorMap::valueToHotNegative1(it*norm/tf_matrix.rows()));
175  break;
176  case HotNeg2:
177  color.setRgb(ColorMap::valueToHotNegative2(it*norm/tf_matrix.rows()));
178  break;
179  case Bone:
180  color.setRgb(ColorMap::valueToBone(it*norm/tf_matrix.rows()));
181  break;
182  case RedBlue:
183  color.setRgb(ColorMap::valueToRedBlue(it*norm/tf_matrix.rows()));
184  break;
185  }
186  coeffs_image->setPixel(x, tf_matrix.rows() - 1 - it, color.rgb());
187  }
188  }
189 
190  *coeffs_image = coeffs_image->scaled(10, tf_matrix.cols()/2, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
191  *coeffs_image = coeffs_image->scaledToHeight(image_to_tf_plot->height(), Qt::SmoothTransformation);
192 
193  QLayout * layout = new QGridLayout();
194  QGraphicsView * view = new QGraphicsView();
195  view->setObjectName("tf_view");
196  view->setScene(tf_scene);
197  QLinearGradient lgrad(tf_scene->sceneRect().topLeft(), tf_scene->sceneRect().bottomRight());
198  lgrad.setColorAt(0.0, Qt::white);
199  lgrad.setColorAt(1.0, Qt::lightGray);
200 
201  tf_scene->setBackgroundBrush(lgrad);//Qt::white);
202 
203  //setup x-axis
204  QGraphicsTextItem *x_axis_name = new QGraphicsTextItem("time [sec]", tf_pixmap);
205  x_axis_name->setFont(QFont("arial", 14));
206 
207  QList<QGraphicsItem *> x_axis_values;
208  QList<QGraphicsItem *> x_axis_lines;
209 
210  qreal scaleXText = (tf_matrix.cols() - 1) / sample_rate / 20.0; // divide signallength
211 
212  for(qint32 j = 0; j < 21; j++) {
213  QGraphicsTextItem *text_item = new QGraphicsTextItem(QString::number(j * scaleXText, 'f', 2), tf_pixmap);
214  text_item->setFont(QFont("arial", 10));
215  x_axis_values.append(text_item); // scalevalue as string
216  QGraphicsLineItem *x_line_item = new QGraphicsLineItem(tf_pixmap);
217  x_line_item->setLine(0,-3,0,3);
218  x_line_item->setPen(QPen(Qt::darkGray, 2, Qt::SolidLine, Qt::SquareCap, Qt::MiterJoin));
219  x_axis_lines.append(x_line_item); // scalelines
220  }
221 
222  x_axis_name->setPos(tf_pixmap->boundingRect().width()/2 - x_axis_name->boundingRect().width()/2,
223  tf_pixmap->boundingRect().height() + 0.8 * x_axis_values.at(0)->boundingRect().height());
224 
225  qreal scale_x = qreal(tf_pixmap->boundingRect().width()) / qreal(x_axis_values.length()-1);
226 
227  for(qint32 i = 0; i < x_axis_values.length(); i++) {
228  x_axis_values.at(i)->setPos(qreal(i)*scale_x - x_axis_values.at(0)->boundingRect().width()/2,
229  tf_pixmap->boundingRect().height());
230  x_axis_lines.at(i)->setPos(qreal(i)*scale_x,
231  tf_pixmap->boundingRect().height());
232 
233  }//end x axis
234 
235  //y-axis
236  QGraphicsTextItem *y_axis_name = new QGraphicsTextItem("frequency [Hz]", tf_pixmap);
237  y_axis_name->setFont(QFont("arial", 14));
238 
239  QList<QGraphicsItem *> y_axis_values;
240  QList<QGraphicsItem *> y_axis_lines;
241 
242  qreal scale_y_text = 0;
243 
244  if(lower_frq == 0 && upper_frq == 0) {
245  scale_y_text = 0.5* sample_rate / 10.0; // divide signallength
246  } else {
247  scale_y_text = (upper_frq - lower_frq) / 10.0;
248  }
249 
250  for(qint32 j = 0; j < 11; j++) {
251  QGraphicsTextItem *text_item = new QGraphicsTextItem(QString::number(lower_frq + j*scale_y_text,//pow(10, j)/pow(10, 11) /*(j+1)/log(12)*/ * max_frequency,//scale_y_text,
252  'f', 0), tf_pixmap);
253  text_item->setFont(QFont("arial", 10));
254  y_axis_values.append(text_item); // scalevalue as string
255  QGraphicsLineItem *y_line_item = new QGraphicsLineItem(tf_pixmap);
256  y_line_item->setLine(-3,0,3,0);
257  y_line_item->setPen(QPen(Qt::darkGray, 2, Qt::SolidLine, Qt::SquareCap, Qt::MiterJoin));
258  y_axis_lines.append(y_line_item); // scalelines
259  }
260 
261  y_axis_name->setPos(- x_axis_values.at(0)->boundingRect().width() - y_axis_name->boundingRect().height(),
262  tf_pixmap->boundingRect().height()/2 + y_axis_name->boundingRect().height()/2);
263  y_axis_name->setRotation(-90);
264 
265  qreal scale_y = qreal(tf_pixmap->boundingRect().height()) / qreal(y_axis_values.length()-1);
266 
267  for(qint32 i = 0; i < y_axis_values.length(); i++) {
268  y_axis_values.at(i)->setPos( -y_axis_values.last()->boundingRect().width()
269  -0.5*y_axis_lines.last()->boundingRect().width()
270  -1
271  ,tf_pixmap->boundingRect().height()
272  - y_axis_values.last()->boundingRect().height()/2
273  - qreal(i)*scale_y);
274  y_axis_lines.at(i)->setPos(0, qreal(i)*scale_y);
275  }
276  //end y axis
277 
278  QGraphicsPixmapItem * coeffs_item = tf_scene->addPixmap(QPixmap::fromImage(*coeffs_image));//addItem();
279  coeffs_item->setParentItem(tf_pixmap);
280  coeffs_item->setPos(tf_pixmap->boundingRect().width() +5, 0);
281 
282  QGraphicsSimpleTextItem *axis_name_item = new QGraphicsSimpleTextItem("coefficients", coeffs_item);
283  QGraphicsSimpleTextItem *axis_zero_item = new QGraphicsSimpleTextItem("0", coeffs_item);
284  QGraphicsSimpleTextItem *axis_one_item = new QGraphicsSimpleTextItem("1", coeffs_item);
285  axis_name_item->setFont(QFont("arial", 14));
286  axis_zero_item->setFont(QFont("arial", 10));
287  axis_one_item->setFont(QFont("arial", 10));
288 
289  axis_name_item->setPos(coeffs_item->boundingRect().width() + 1,
290  coeffs_item->boundingRect().height()/2 + axis_name_item->boundingRect().height()/2);
291  axis_name_item->setRotation(-90);
292  axis_zero_item->setPos( 1 + coeffs_item->boundingRect().width(),
293  coeffs_item->boundingRect().height()- axis_zero_item->boundingRect().height());
294  axis_one_item->setPos( 1 + coeffs_item->boundingRect().width(), 0);
295  //end coeffs picture
296 
297  view->fitInView(layout->contentsRect(),Qt::KeepAspectRatio);
298  layout->addWidget(view);
299  this->setLayout(layout);
300 }
301 
302 //=============================================================================================================
303 
304 void TFplot::resizeEvent(QResizeEvent *event)
305 {
306  Q_UNUSED(event);
307 
308  QWidget *widget = this->layout()->itemAt(0)-> widget();
309  if (widget != NULL ) {
310  QGraphicsView* view = (QGraphicsView*)widget;
311  view->fitInView(view->sceneRect(),Qt::KeepAspectRatio);
312  }
313 }
314 
DISPLIB::TFplot::TFplot
TFplot(Eigen::MatrixXd tf_matrix, qreal sample_rate, qreal lower_frq, qreal upper_frq, ColorMaps cmap)
Definition: tfplot.cpp:65
tfplot.h
TFplot class declaration.
DISPLIB::ColorMap::valueToHot
static QRgb valueToHot(double v)
Definition: colormap.h:736
DISPLIB::ColorMap::valueToRedBlue
static QRgb valueToRedBlue(double v)
Definition: colormap.h:768
DISPLIB::ColorMap::valueToJet
static QRgb valueToJet(double v)
Definition: colormap.h:728
DISPLIB::ColorMap::valueToHotNegative2
static QRgb valueToHotNegative2(double v)
Definition: colormap.h:752
DISPLIB::TFplot::calc_plot
void calc_plot(Eigen::MatrixXd tf_matrix, qreal sample_rate, ColorMaps cmap, qreal lower_frq, qreal upper_frq)
Definition: tfplot.cpp:111
DISPLIB::ColorMap::valueToHotNegative1
static QRgb valueToHotNegative1(double v)
Definition: colormap.h:744
DISPLIB::ColorMap::valueToBone
static QRgb valueToBone(double v)
Definition: colormap.h:760
colormap.h
ColorMap class declaration.