static status deleteCellTable(Table tab, TableCell cell, BoolObj keep) { if ( cell->layout_manager == (LayoutManager)tab ) { int tx = valInt(cell->column) + valInt(cell->col_span); int ty = valInt(cell->row) + valInt(cell->row_span); int x, y; removeCellImageTable(tab, cell, keep); for(y=valInt(cell->row); y<ty; y++) { TableRow row = getRowTable(tab, toInt(y), OFF); if ( row ) { for(x=valInt(cell->column); x<tx; x++) elementVector((Vector)row, toInt(x), NIL); } } assign(cell, layout_manager, NIL); changedTable(tab); requestComputeLayoutManager((LayoutManager)tab, DEFAULT); } succeed; }
static status appendTable(Table tab, TableCell cell, Int x, Int y) { int cspan = valInt(cell->col_span); int rspan = valInt(cell->row_span); int dy; if ( isDefault(x) ) x = tab->current->x; if ( isDefault(y) ) y = tab->current->y; if ( notNil(tab->device) && notNil(cell->image) ) send(tab->device, NAME_display, cell->image, EAV); assign(cell, layout_manager, tab); assign(cell, column, x); assign(cell, row, y); for(dy=0; dy<rspan; dy++) { Int cy = toInt(valInt(y)+dy); TableRow row = getRowTable(tab, cy, ON); int dx; for(dx=0; dx<cspan; dx++) { Int cx = toInt(valInt(x)+dx); cellTableRow(row, cx, cell); } } advance_table(tab); requestComputeLayoutManager((LayoutManager)tab, DEFAULT); return changedTable(tab); }
//**************************************************************************** //**************************************************************************** //**************************************************************************** // RunKPLS - run kpls to classify objects //**************************************************************************** void PatternAnalysisWizard::runKPLS() { #ifdef USE_KPLS //Find out which features are checked (which columns to use). std::vector<int> columnsToUse; QList<QAbstractButton *> buttons = featureGroup->buttons(); for(int b = 0; b<buttons.size(); ++b) { if( buttons.at(b)->isChecked() ) { if(featureGroup->id(buttons.at(b)) != 0) { columnsToUse.push_back( featureGroup->id(buttons.at(b)) ); } else { for(int col=0; col<(int)m_table->GetNumberOfColumns(); ++col) { std::string col_name = m_table->GetColumnName(col); if(col_name.find("Zern")!=std::string::npos) { columnsToUse.push_back(col); } } } } } if(columnsToUse.size() <= 0) return; this->KPLSrun(columnsToUse); emit changedTable(); emit enableModels(); #else QMessageBox::information(this, tr("MESSAGE"), tr("FARSIGHT was not compiled with KPLS library")); #endif }
static status rowSpanTableCell(TableCell cell, Int span) { if ( cell->row_span != span ) { Table tab = table_of_cell(cell); if ( tab ) { int x,y; int fy = valInt(cell->row); int h = valInt(span); int oh = valInt(cell->row_span); int ty = max(h,oh)+fy; for(y=fy+1; y<ty; y++) { TableRow row = getRowTable(tab, toInt(y), ON); for(x=valInt(cell->column); x<valInt(cell->column)+valInt(cell->col_span); x++) { Any e; if ( y-fy < h ) e = cell; else e = NIL; cellTableRow(row, toInt(x), e); } } assign(cell, row_span, span); changedTable(tab); /* changed line-pattern */ requestComputeLayoutManager((LayoutManager)tab, DEFAULT); } else { assign(cell, row_span, span); } } succeed; }
static status colSpanTableCell(TableCell cell, Int span) { if ( cell->col_span != span ) { Table tab = table_of_cell(cell); if ( tab ) { int x,y; int fx = valInt(cell->column); int w = valInt(span); int ow = valInt(cell->col_span); int tx = max(w,ow)+fx; for(y=valInt(cell->row); y<valInt(cell->row)+valInt(cell->row_span); y++) { TableRow row = getRowTable(tab, toInt(y), ON); for(x=fx+1; x<tx; x++) { Any e; if ( x-fx < w ) e = cell; else e = NIL; cellTableRow(row, toInt(x), e); } } assign(cell, col_span, span); changedTable(tab); /* changed line-pattern */ requestComputeLayoutManager((LayoutManager)tab, DEFAULT); } else { assign(cell, col_span, span); } } succeed; }
//**************************************************************************** //**************************************************************************** //**************************************************************************** // RunSVM - run support vector machine in one class mode to find outliers. //**************************************************************************** void PatternAnalysisWizard::runSVM() { bool normalize = true; //ALWAYS TRUE!! double nu = 0.10; //Find out which featueres are checked (which columns to use). std::vector<int> columnsToUse; QList<QAbstractButton *> buttons = featureGroup->buttons(); for(int b = 0; b<buttons.size(); ++b) { if( buttons.at(b)->isChecked() ) { columnsToUse.push_back( featureGroup->id(buttons.at(b)) ); } } //Setup the scaling values std::vector<double> f_max; //The maximum value of each feature f_max.assign(columnsToUse.size(), -DBL_MAX); std::vector<double> f_min; //The minimum value of each feature f_min.assign(columnsToUse.size(), DBL_MAX); std::vector< std::vector< double > > features; //Will contain the normalized features features.resize( m_table->GetNumberOfRows() ); //exract data from the model and get min/max values: for(int r=0; r<(int)features.size(); ++r) { for(int c=0; c<(int)columnsToUse.size(); ++c) { int col = columnsToUse.at(c); double val = m_table->GetValue(r,col).ToDouble(); features.at(r).push_back( val ); if( normalize ) { //Also gather min/max f_max.at(c) = val > f_max.at(c) ? val : f_max.at(c); f_min.at(c) = val < f_min.at(c) ? val : f_min.at(c); } } } if( normalize ) { //Normalize/Scale the Features Data: double upper = 1; double lower = -1; double desired_range = upper-lower; for(int r=0; r<(int)features.size(); ++r) { for(int c=0; c<(int)columnsToUse.size(); ++c) { double oldval = features.at(r).at(c); features.at(r).at(c) = lower + desired_range * (oldval - f_min.at(c)) / (f_max.at(c) - f_min.at(c)); } } } //Create the libSVM problem: #define Malloc(type,n) (type *)malloc((n)*sizeof(type)) struct svm_problem prob; prob.l = (int)features.size(); //Number of objects prob.y = Malloc(double,prob.l); //Array Containing target values (unknowns) prob.x = Malloc(struct svm_node *,prob.l); //Array of Pointers to Nodes for(int r=0; r<prob.l; ++r) { prob.y[r] = 1; //This is the label (target) and it is unknown struct svm_node *x_space = Malloc(struct svm_node, columnsToUse.size()+1); //Individual node for(int c=0; c<(int)columnsToUse.size(); ++c) { x_space[c].index = c+1; x_space[c].value = features.at(r).at(c); } x_space[ columnsToUse.size() ].index = -1; prob.x[r] = &x_space[0]; //Point to this new set of nodes. } //Set the Parameters struct svm_parameter param; param.svm_type = ONE_CLASS; param.kernel_type = RBF; param.degree = 3; param.gamma = 1.0/double(prob.l); // 1/k //param.gamma = 1; param.coef0 = 0; //param.nu = 0.1; param.nu = nu; param.cache_size = 100; param.C = 1; param.eps = .001; param.p = 0.1; param.shrinking = 1; param.probability = 0; param.nr_weight = 0; param.weight_label = NULL; param.weight = NULL; //Now train struct svm_model *m_svm_model; m_svm_model = svm_train(&prob,¶m); svm_destroy_param(¶m); free(prob.y); free(prob.x); //Predict: std::vector<int> outliers; for(int r=0; r<prob.l; r++) { struct svm_node *x = Malloc(struct svm_node,columnsToUse.size()+1); //Individual node for(int c=0; c<(int)columnsToUse.size(); ++c) { x[c].index = c+1; x[c].value = features.at(r).at(c); } x[ columnsToUse.size() ].index = -1; double v = svm_predict(m_svm_model,x); free(x); if( v == -1 ) { outliers.push_back( r ); } } svm_destroy_model(m_svm_model); //If need to create a new column do so now: vtkAbstractArray * output = m_table->GetColumnByName(columnForPrediction); if(output == 0) { vtkSmartPointer<vtkDoubleArray> column = vtkSmartPointer<vtkDoubleArray>::New(); column->SetName( columnForPrediction ); column->SetNumberOfValues( m_table->GetNumberOfRows() ); m_table->AddColumn(column); } for(int row = 0; (int)row < m_table->GetNumberOfRows(); ++row) //Set all values to 0 { m_table->SetValueByName(row,columnForPrediction, 0); } for(int i = 0; i < (int)outliers.size(); ++i) //Set outliers to 1 { int row = outliers.at(i); m_table->SetValueByName(row,columnForPrediction, 1); } emit changedTable(); /* model->setHeaderData( columnForPrediction, Qt::Horizontal, tr("outlier?") ); //stop signalling: model->blockSignals(true); int z = 0; int o = 1; for(int row = 0; (int)row < model->rowCount(); ++row) //Set all values to 0 { model->setData(model->index(row, columnForPrediction), z); } for(int i = 0; i < (int)outliers.size()-1; ++i) //Set outliers to 1 { model->setData(model->index(outliers.at(i), columnForPrediction), o); } //turn signals back on & change one more piece of data to force dataChanged signal model->blockSignals(false); model->setData(model->index(outliers.back(), columnForPrediction), o); */ }