File: componentPlotter.C

package info (click to toggle)
ball 1.4.3~beta1-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 318,984 kB
  • sloc: cpp: 346,579; ansic: 4,097; python: 2,664; yacc: 1,778; lex: 1,099; xml: 964; sh: 688; sql: 316; awk: 118; makefile: 108
file content (282 lines) | stat: -rw-r--r-- 8,889 bytes parent folder | download | duplicates (2)
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
#include <componentPlotter.h>

#include <qwt_plot_curve.h>
#include <qwt_plot_marker.h>
#include <qwt_plot_zoomer.h>
#include <qwt_scale_widget.h>
#include <qwt_color_map.h>

#include <qwt_legend.h>
#include <qwt_legend_item.h>

#include <QColor>

#include <BALL/QSAR/regressionModel.h>

using namespace BALL::QSAR;

namespace BALL
{
	namespace VIEW
	{

		ComponentPlotter::ComponentPlotter(ModelItem* model_item, bool plot_loadings)
			: Plotter(model_item)
		{
			zoomer_ = NULL;
			model_item_ = model_item;
			plot_loadings_ = plot_loadings;
			component_one_combobox_ = new QComboBox(this);
			component_two_combobox_ = new QComboBox(this);
			component_matrix_ = NULL;
			qwt_plot_->enableAxis(QwtPlot::yLeft);
			if(!plot_loadings_) qwt_plot_->enableAxis(QwtPlot::yRight,1);
			else qwt_plot_->enableAxis(QwtPlot::yRight,0);
			
			data_symbol.setSize(6,6);
			print_data_symbol.setSize(5,5);
			
			plot(1);
			zoomer_ = new QwtPlotZoomer(qwt_plot_->canvas(),this);

			buttonsLayout_->addWidget(component_one_combobox_);
			buttonsLayout_->addWidget(component_two_combobox_);
			connect(component_one_combobox_,SIGNAL(currentIndexChanged(int)),this,SLOT(selectedCompChanged()));
			connect(component_two_combobox_,SIGNAL(currentIndexChanged(int)),this,SLOT(selectedCompChanged()));
			
			if(plot_loadings_) setWindowTitle("Loadings Plotter");
			else setWindowTitle("Latent Variable Plotter");
			
			calculateComponents();
			if(component_matrix_==NULL || component_matrix_->cols()==0)
			{
				throw BALL::Exception::GeneralException(__FILE__,__LINE__,"ComponentPlotter error","No input data available, thus plot cannot be created!");
			}
			
			// loading-plot is identical for all response variables ...
			if(!plot_loadings_)
			{
				int no_y = model_item_->inputDataItem()->data()->getNoResponseVariables();
				if(no_y>1)
				{
					for(unsigned int i=0; i<no_y;i++)
					{
						String s = "Activity "+String(i);
						activity_combobox_->addItem(s.c_str(),i);
					}
					activity_combobox_->show();
				}
			}
		}


		void ComponentPlotter::selectedCompChanged()
		{
			delete zoomer_;
			zoomer_ = NULL;
			plot(1);
			zoomer_ = new QwtPlotZoomer(qwt_plot_->canvas(),this); // if not creating a new zoomer, zooming will not work correctly
		}


		void ComponentPlotter::calculateComponents()
		{
			RegressionModel* model = (RegressionModel*)model_item_->model();
			
			if(!model_item_->getRegistryEntry()->latent_variables)
			{
				std::cout << "Plotting of components can only be done for models that create principle components or partial least squares components!" << std::endl;
				return;
			}
			if(model_item_->inputDataItem()->data()==NULL)
			{
				std::cout << "No input data! Thus creation of PCR/PLS components for plotting is not possible!" << std::endl;
				return;
			}
			if(!plot_loadings_ && model_item_->inputDataItem()->data()->getNoResponseVariables()==0)
			{
				std::cout << "There should be response variable(s) for plotting of PLS/PCR components" << std::endl;
				return;
			}
			
			unsigned int no_components=0;
			
			LatentVariableModel* lv_model = dynamic_cast<LatentVariableModel*>(model_item_->model());
			
			if(!plot_loadings_)
			{
				component_matrix_ = lv_model->getLatentVariables();
				if(component_matrix_->cols()==0||component_matrix_->rows()==0)
				{
					model->setDataSource(model_item_->inputDataItem()->data());
					model->readTrainingData();
					model->train(); 
					component_matrix_ = lv_model->getLatentVariables();
				}
				
				no_components=component_matrix_->cols();
				if(no_components<2) return;
				
				unsigned int size = model_item_->model()->data->getNoSubstances();
				min_response_value_ = 1e10;
				max_response_value_ = -1e10;
				const QSARData* data = model_item_->inputDataItem()->data();
				for(unsigned int j=0; j<size; j++)
				{
					vector<double>* resp = data->getActivity(j);
					double response_value = (*resp)[selected_activity_];
					delete resp;
					if(response_value<min_response_value_) min_response_value_=response_value;
					if(response_value>max_response_value_) max_response_value_=response_value;
				}
			}
			else
			{
				component_matrix_ = lv_model->getLoadings();
				if(component_matrix_->cols()==0||component_matrix_->rows()==0)
				{
					model->setDataSource(model_item_->inputDataItem()->data());
					model->readTrainingData();
					model->train();
					component_matrix_ = lv_model->getLoadings();
				}
			}
			
			no_components=component_matrix_->cols();
			if(no_components<2) return;

			if(component_one_combobox_->count()==0)
			{
				for(unsigned int i=1;i<=no_components;i++)
				{
					String tmp;
					if(!plot_loadings_) tmp="component ";
					else tmp="loading ";
					tmp+=String(i);
					component_one_combobox_->addItem(tmp.c_str(),i);
					component_two_combobox_->addItem(tmp.c_str(),i);
				}
				component_one_combobox_->setCurrentIndex(0);
				component_two_combobox_->setCurrentIndex(1);	
			}
		}


		// SLOT
		void ComponentPlotter::activityChange()
		{
			component_matrix_=NULL;  // make sure to recalculate min and max of the response variable
			Plotter::activityChange();	
		}


		void ComponentPlotter::plot(bool zoom)
		{
			qwt_plot_->clear();
			
			if(component_matrix_==NULL||component_matrix_->cols()==0) calculateComponents();
			if(component_matrix_==NULL||component_matrix_->cols()<2) return; // only 1 component
			
			double min_y=1e10;
			double max_y=-1e10;
			double min_x=1e10;
			double max_x=-1e10;
			const vector<string>* feature_names;
			if(!plot_loadings_) feature_names = model_item_->model()->getSubstanceNames();
			else feature_names = model_item_->model()->getDescriptorNames();
			const unsigned int size = feature_names->size();
			
			unsigned int comp_one=component_one_combobox_->itemData(component_one_combobox_->currentIndex()).toInt();
			unsigned int comp_two=component_two_combobox_->itemData(component_two_combobox_->currentIndex()).toInt();

			QwtLinearColorMap color_map;
			color_map.setColorInterval(QColor(255,0,0),QColor(0,255,0));
			QwtDoubleInterval interval(min_response_value_,max_response_value_);
			
			const QSARData* data = model_item_->inputDataItem()->data();
			
			for(unsigned int j=1; j<=size; j++)
			{
				QwtPlotMarker* marker= new QwtPlotMarker;
				QwtSymbol symbol = data_symbol;
				
				if(!plot_loadings_)
				{
					vector<double>* resp = data->getActivity(j-1);
					double response_value = (*resp)[selected_activity_];
					delete resp;
					QBrush b(QColor(color_map.rgb(interval,response_value)),Qt::SolidPattern);
					symbol.setBrush(b);
				}
			
				double x_j = (*component_matrix_)(j,comp_one);
				double y_j = (*component_matrix_)(j,comp_two);
				if(y_j<min_y) min_y=y_j;
				if(y_j>max_y) max_y=y_j;
				if(x_j<min_x) min_x=x_j;
				if(x_j>max_x) max_x=x_j;
				marker->setSymbol(symbol);
				marker->setValue(x_j,y_j);
				marker->attach(qwt_plot_); // attached object will be automatically deleted by QwtPlot
				
				if(show_data_labels)
				{
					QString s =(*feature_names)[j-1].c_str();
					QwtText label(s);
					label.setFont(data_label_font);
					marker->setLabel(label);
					marker->setLabelAlignment(data_label_alignment);
				}
			}

			QString s1 = component_one_combobox_->itemText(component_one_combobox_->currentIndex());
			QString s2 = component_two_combobox_->itemText(component_two_combobox_->currentIndex());
			LatentVariableModel* lv_model = dynamic_cast<LatentVariableModel*>(model_item_->model());
			const Eigen::MatrixXd* weights = lv_model->getWeights();
			
			if(!plot_loadings_)
			{
				s1 += "  c="; s1+=String((*weights)(comp_one,1)).c_str();
				s2 += "  c="; s2+=String((*weights)(comp_two,1)).c_str();
			}
			
			qwt_plot_->setAxisTitle(QwtPlot::yLeft,s2);
			qwt_plot_->setAxisTitle(QwtPlot::xBottom,s1);
			
			double x_border=(max_x-min_x)*0.05;
			double y_border=(max_y-min_y)*0.05;
			min_x-=x_border; min_y-=y_border;
			max_x+=x_border; max_y+=y_border;
			
			QwtPlotCurve* zero_line = new QwtPlotCurve;
			QwtPlotCurve* zero_line2 = new QwtPlotCurve;
			double x[2]; x[0]=min_x; x[1]=max_x;
			double y[2]; y[0]=0; y[1]=0;
			zero_line->setData(x,y,2);
			x[0]=0; x[1]=0;
			y[0]=min_y; y[1]=max_y;
			zero_line2->setData(x,y,2);
			QColor c(135,135,135); // grey
			QPen pen(c);
			zero_line->setPen(pen);
			zero_line->attach(qwt_plot_);
			zero_line2->setPen(pen);
			zero_line2->attach(qwt_plot_);
			
			if(zoom)
			{
				qwt_plot_->setAxisScale(QwtPlot::yLeft,min_y,max_y);
				qwt_plot_->setAxisScale(QwtPlot::xBottom,min_x,max_x);
			}
			
			if(!plot_loadings_)
			{	
				QwtScaleWidget* rightAxis = qwt_plot_->axisWidget(QwtPlot::yRight);
				rightAxis->setTitle("response value");
				rightAxis->setColorBarEnabled(true);
				rightAxis->setColorMap(interval,color_map);
				qwt_plot_->setAxisScale(QwtPlot::yRight,min_response_value_,max_response_value_);
			}
		}
	}
}