int Metric_algo_variance::execute( GsTL_project* proj ){
	SmartPtr<Named_interface> ni =
				Root::instance()->interface( metricData_manager + "/metricRegistrar");

	MetricDataManager* mDataRegistrar = dynamic_cast<MetricDataManager*>(ni.raw_ptr());

	for(int i=0; i< props_.size(); ++i) {
		Temporary_propRegion_Selector regionProp(region_,props_[i]);
		float sum = 0.0f;
		float sum2 = 0.0f;
		int n = 0;

		Grid_continuous_property::iterator it = props_[i]->begin();

		for ( ; it != props_[i]->end(); ++it) {
			float v = *it;
			sum += v;
			sum2 += v * v;
			n++;
		}
		float ex = sum / (float)n;
		float ex2 = sum2 / (float) n;
		float variance = ex2 - ex * ex;  // Var[X] = E[X^2] - E[X]^2
		MetricData* metric = new MetricScalarData(props_[i],paramXml_, variance );
		mDataRegistrar->registerMetricData(grid_name_,props_[i]->name(),metric);
	}
	return 0;
}
Exemple #2
0
void MetricDataTable::setMetricModel(){

    delete model_;

    SmartPtr<Named_interface> ni =
            Root::instance()->interface( metricData_manager + "/metricRegistrar");

    //MetricDataManager* mDataRegistrar = dynamic_cast<MetricDataManager*>(ni.raw_ptr());
    MetricDataManager* mDataRegistrar = (MetricDataManager*)(ni.raw_ptr());
    //MetricDataManager* mDataRegistrar;
    std::vector<std::string> metric_names = mDataRegistrar->get_metric_names();

    model_ = new QStandardItemModel(metric_names.size(), 2, this);
    model_->setHeaderData(0, Qt::Horizontal, tr("Name"));
    model_->setHeaderData(1, Qt::Horizontal, tr("Type"));
    for(unsigned int i=0; i<metric_names.size(); ++i ) {
        const MetricDataToProperties m = mDataRegistrar->get_properties_for_metric(metric_names[i]);
        std::cout << "Current Metric Name " << metric_names[i] <<std::endl;
        //MetricDataToProperties m;
        model_->setData(model_->index(i,0, QModelIndex()), QString::fromStdString(metric_names[i]) );
        model_->setData(model_->index(i,0, QModelIndex()), QString::fromStdString(m.type()) );
    }

    tableView_->setModel(model_);

}
VectorXf param_sensitivity_widget::readResponseValues(
    QStringList realizations,QString response)
{

    // Sort the realizations stringlist
    realizations.sort();


    SmartPtr<Named_interface> ni =
        Root::instance()->interface( metricData_manager +
                                     "/metricRegistrar");

    MetricDataManager* mRegistrar =
        dynamic_cast<MetricDataManager*>(ni.raw_ptr());

    // Grab the grid name
    std::string gridName = mdsObject_->getGridName(0);

    // Obtain pointer to metricToProperties map
    MetricDataToProperties  propFromMetricRegistrar =
        mRegistrar->get_properties_for_metric(response.toStdString());

    // Determine what kind of metricData we are looking at
    std::string response_type = propFromMetricRegistrar.
                                data_type().toStdString();

    // Response values
    VectorXf responseValue(realizations.size());

    if (response_type == "MetricTimeSeriesData")
    {

        for (unsigned int i = 0; i < realizations.size(); ++i )
        {
            MetricData* mdata = mRegistrar->getMetricData(gridName,
                                realizations.at(i).
                                toStdString(),
                                response.toStdString());

            // Should be a safe cast as this function is only called for data that
            // has form "TimeSeries"
            MetricTimeSeriesData *mDataTS =
                static_cast<MetricTimeSeriesData*>(mdata);

            // Get number of data points in this response for particular property
            // We assume that the data/time have same number of points
            int numDataPoints = mDataTS->data().size();

            responseValue[i] = (mDataTS->data().at(numDataPoints -1));
        }
    }

    return responseValue;
}
bool ChartDataFromPropertyMdiSubWindow::initialize(QString metricName, QStringList props ){

	SmartPtr<Named_interface> ni =
	         Root::instance()->interface( metricData_manager + "/metricRegistrar");

	//return dynamic_cast<MetricDataManager*>(ni.raw_ptr());
	MetricDataManager* mRegistrar = dynamic_cast<MetricDataManager*>(ni.raw_ptr());

	metricName_ = metricName;
	this->setWindowTitle("Metric name: "+metricName_);
//	table_->SetName(metricName.toStdString());

	//vtkSmartPointer<vtkStringArray> arrX = vtkSmartPointer<vtkStringArray>::New();
	arrX_ = vtkSmartPointer<vtkStringArray>::New();
	arrX_->SetName("Metrics");
	table_->AddColumn(arrX_);
	arrY_ = vtkSmartPointer<vtkFloatArray>::New();
	arrY_->SetName("Value");
	table_->AddColumn(arrY_);
	table_->SetNumberOfRows(props.size());

	std::string metricName_str = metricName_.toStdString();
	for( int i=0; i<props.size(); ++i  ) {
		QStringList path = props.at(i).split("/");
		MetricData* mdata = mRegistrar->getMetricData(path.at(0).toStdString(),
													  path.at(1).toStdString(),
													  metricName_str);
		if( mdata==0 ) return false;

//		QString valStr =
		arrX_->InsertValue(i,props.value(i).toStdString().c_str());
		arrY_->SetValue(i,mdata->data(0));

	}
//	table_->Update();

	vtkPlot *bar = chart_->AddPlot(vtkChart::BAR);
    bar->SetInputData(table_, 0, 1);


	tableView_->SetSplitMultiComponentColumns(true);
	tableView_->AddRepresentationFromInput(table_);
	tableView_->Update();

	return true;

}
int Metric_algo_mean::execute( GsTL_project* proj ){
	SmartPtr<Named_interface> ni =
				Root::instance()->interface( metricData_manager + "/metricRegistrar");

	MetricDataManager* mDataRegistrar = dynamic_cast<MetricDataManager*>(ni.raw_ptr());

	for(int i=0; i< props_.size(); ++i) {
		Temporary_propRegion_Selector regionProp(region_,props_[i]);
		float sum = 0.0f;
		int n = 0;

		Grid_continuous_property::iterator it = props_[i]->begin();

		for ( ; it != props_[i]->end(); ++it) {
			sum += *it;
			n++;
		}
		MetricData* metric = new MetricScalarData(props_[i],paramXml_, sum / (float)n);
		mDataRegistrar->registerMetricData(grid_name_,props_[i]->name(),metric);
	}

	return 0;

}
void ExternalResponseInputFilter::exec()
{
    std::cout << "Executing External Response Input Filter" << std::endl;

    // First Parse The XML file
    QDomDocument doc("ExternalResponses");

    QFile file(QString::fromStdString(this->filename));

    if (!file.open(QIODevice::ReadOnly))
    {
        std::cerr << "Failed to open response file" << std::endl;
        return;
    }
    if (!doc.setContent(&file))
    {
        std::cerr << "Failed to parse response file" << std::endl;
        file.close();
        return;
    }

    QDomElement root = doc.documentElement();
    if (root.tagName()!= "Responses")
    {
        std::cerr << "Failed to find root" << std::endl;
        return;

    }

    QDomNode n = root.firstChild();

    while (!n.isNull())
    {
        QDomElement e = n.toElement();

        if (!e.isNull())
        {
            if (e.tagName() == "Response")
            {
                // Check to make sure grid exists
                QString gridStr = e.attribute("grid");

                // Check to make sure property exists
                QString propStr = e.attribute("property");

                // Check to make sure name exists, it not create it
                QString nameStr = e.attribute("name");

                // Read type of data this response is storing
                QString typeStr = e.attribute("type");

                // If we have a time series, we store both time and values
                if (typeStr == "time-series")
                {
                    std::vector<float> time;
                    std::vector<float> value;

                    // Grab child node
                    QDomNode subNode = e.firstChild();

                    while (!subNode.isNull())
                    {
                        // Convert node to element
                        QDomElement subNodeElem = subNode.toElement();

                        // Read raw text
                        QString rawText = subNodeElem.text();

                        // Break raw text up into seperate values seperated by ;
                        istringstream seperatedString(rawText.toStdString());

                        while (seperatedString)
                        {
                            std::string s;
                            if (!getline(seperatedString,s,';'))
                                break;
                            else
                            {
                                // Convert from string to float
                                istringstream os(s);
                                float d;
                                os >> d;

                                if (subNodeElem.nodeName() == "Time")
                                {

                                    time.push_back(d);
                                }
                                else if (subNodeElem.nodeName() == "Value")
                                    value.push_back(d);
                                else
                                    break;
                            }
                        }
                        subNode = subNode.nextSibling();
                    }

                    // Ensure number of time and values are the same
                    if (time.size() != value.size())
                        std::cerr << "ERROR: time and values do not align"
                                  << std::endl;

                    // GENERATE NEW METRIC DATA HERE
                    // Need: Pointer to property
                    //       -QDomElement metaDataXml
                    // which has <algorithm name = "whatever">
                    // and       <Name value = "whatever">

                    // Find pointer to appropiate property
                    // Obtain smart point to current grid


                    SmartPtr<Named_interface> grid_ni =
                            Root::instance()->interface(gridModels_manager +
                                                        "/" +
                                                        gridStr.toStdString());

                    if (grid_ni.raw_ptr() == 0)
                    {
                        std::cerr << "Grid not found" << std::endl;
                        file.close();
                        return;

                    }



                    Geostat_grid* grid =
                            dynamic_cast<Geostat_grid*> (grid_ni.raw_ptr());

                    // Grab GsTLGridProperty from Grid
                    GsTLGridProperty* currentProperty =
                            grid->select_property(propStr.toStdString());

                    // Generate the required metaDataXml
                    QDomDocument doc("metaDataXml");
                    QDomElement metaDataXml = doc.createElement("metaRoot");
                    doc.appendChild(metaDataXml);

                    QDomElement algoXml = doc.createElement("algorithm");
                    algoXml.setAttribute("name",nameStr);
                    metaDataXml.appendChild(algoXml);

                    QDomElement nameXml = doc.createElement("Name");
                    nameXml.setAttribute("value",nameStr);
                    metaDataXml.appendChild(nameXml);

                    SmartPtr<Named_interface> ni =
                            Root::instance()->interface(
                                metricData_manager + "/metricRegistrar");

                    MetricDataManager* mDataRegistrar =
                            dynamic_cast<MetricDataManager*>(ni.raw_ptr());

                    MetricTimeSeriesData *metric = new
                            MetricTimeSeriesData(currentProperty,
                                                 metaDataXml,value,time);

                    mDataRegistrar->registerMetricData(gridStr.toStdString(),
                                                       propStr.toStdString(),
                                                       metric );

                    // Move onto next response
                    time.clear();
                    value.clear();
                }

                else if (typeStr == "vector")
                {
                    std::vector<float> value;

                    // Grab child node
                    QDomNode subNode = e.firstChild();

                    while (!subNode.isNull())
                    {
                        // Convert node to element
                        QDomElement subNodeElem = subNode.toElement();

                        // Read raw text
                        QString rawText = subNodeElem.text();

                        // Break raw text up into seperate values seperated by ;
                        istringstream seperatedString(rawText.toStdString());

                        while (seperatedString)
                        {
                            std::string s;
                            if (!getline(seperatedString,s,';'))
                                break;
                            else
                            {
                                // Convert from string to float
                                istringstream os(s);
                                float d;
                                os >> d;

                                if (subNodeElem.nodeName() == "Value")
                                    value.push_back(d);
                                else
                                    break;
                            }
                        }
                        subNode = subNode.nextSibling();
                    }


                    // GENERATE NEW METRIC DATA HERE
                    // Need: Pointer to property
                    //       -QDomElement metaDataXml
                    // which has <algorithm name = "whatever">
                    // and       <Name value = "whatever">

                    // Find pointer to appropiate property
                    // Obtain smart point to current grid


                    SmartPtr<Named_interface> grid_ni =
                            Root::instance()->interface(gridModels_manager +
                                                        "/" +
                                                        gridStr.toStdString());

                    if (grid_ni.raw_ptr() == 0)
                    {
                        std::cerr << "Grid not found" << std::endl;
                        file.close();
                        return;

                    }



                    Geostat_grid* grid =
                            dynamic_cast<Geostat_grid*> (grid_ni.raw_ptr());

                    // Grab GsTLGridProperty from Grid
                    GsTLGridProperty* currentProperty =
                            grid->select_property(propStr.toStdString());

                    // Generate the required metaDataXml
                    QDomDocument doc("metaDataXml");
                    QDomElement metaDataXml = doc.createElement("metaRoot");
                    doc.appendChild(metaDataXml);

                    QDomElement algoXml = doc.createElement("algorithm");
                    algoXml.setAttribute("name",nameStr);
                    metaDataXml.appendChild(algoXml);

                    QDomElement nameXml = doc.createElement("Name");
                    nameXml.setAttribute("value",nameStr);
                    metaDataXml.appendChild(nameXml);

                    SmartPtr<Named_interface> ni =
                            Root::instance()->interface(
                                metricData_manager + "/metricRegistrar");

                    MetricDataManager* mDataRegistrar =
                            dynamic_cast<MetricDataManager*>(ni.raw_ptr());

                    MetricVectorData *metric = new
                            MetricVectorData(currentProperty,
                                                 metaDataXml,value);

                    mDataRegistrar->registerMetricData(gridStr.toStdString(),
                                                       propStr.toStdString(),
                                                       metric );

                    // Move onto next response

                    value.clear();

                }
Exemple #7
0
// filename
bool MDS_action::init(std::string& parameters, GsTL_project* proj,
		Error_messages_handler* errors) {


	std::vector< std::string > params =
                        String_Op::decompose_string( parameters,
                                                     Actions::separator,
                                                     Actions::unique );
	
	if( params.size() != 2 ) {
		errors->report( "Need the name of the output and filename to an xml file" );
		return false;
	}

	std::vector<MultiDimScalingSpace::grid_prop_pairT> props_grid;

	proj_ = proj;

	output_pset_name_ = params[1];
	if(output_pset_name_.empty()) {
		errors->report( "No output point set name specified" );
		output_pset_name_ = "MDS_points";
	}


	SmartPtr<Named_interface> ni =
			Root::instance()->interface( metricData_manager + "/metricRegistrar");

	MetricDataManager* mDataRegistrar = dynamic_cast<MetricDataManager*>(ni.raw_ptr());

	//Get the XML definition of the filter
	QDomDocument doc("MDSAction");
	QFile file(params[0].c_str());
	if (!doc.setContent(&file))
	{
		file.close();
		errors->report( "The file "+params[0]+" is not a valid xml format file" );
		return false;
	}
	file.close();

	QDomElement root = doc.documentElement();

	QDomElement elemKernel = root.firstChildElement("Kernel");
	std::string kernel_type = elemKernel.attribute("type").toStdString();

	QDomElement elemMetricData = root.firstChildElement("MetricData");
	QDomElement elemMetric = elemMetricData.firstChildElement("Metric");
	std::vector<std::string> mdata_names; // vector of metric names
	for( ; !elemMetric.isNull(); elemMetric = elemMetric.nextSiblingElement("Metric")  ) {
		std::string metric_name = elemMetric.attribute("name").toStdString();
        mdata_names.push_back(metric_name);
	}

	QDomElement elem = root.firstChildElement("Grid");
	for( ; !elem.isNull(); elem = elem.nextSiblingElement("Grid")  ) {
		std::string grid_name = elem.attribute("name").toStdString();
		QDomElement gridProps = elem.firstChildElement("Property");
		for (; !gridProps.isNull(); gridProps = gridProps.nextSiblingElement("Property")) {
			std::string property_name = gridProps.attribute("name").toStdString();
            props_grid.push_back(MultiDimScalingSpace::grid_prop_pairT(grid_name,property_name));
			MetricDataCollection mdataCollection =
					mDataRegistrar->getMetricDataCollection(grid_name, property_name, mdata_names);
			if(mdataCollection.empty()) {
				errors->report( "Some metric data for the property "+property_name+ " on the grid "+grid_name+" could not be found" );
				return false;
			}
			mdataCollections_.push_back(mdataCollection);
		}
	}
	num_inputs_ = mdataCollections_.size();

	// set kernels

	SmartPtr<Named_interface> kernel_ni =
			Root::instance()->new_interface( kernel_type, kernel_manager + "/" );

	if (!kernel_ni.raw_ptr()) {
		errors->report( "Kernel "+kernel_type+" could not be found" );
		return false;
	}
	Distance_kernel* kernel = dynamic_cast<Distance_kernel*>(kernel_ni.raw_ptr());
	if (!kernel) {
		errors->report( "Kernel "+kernel_type+" has an invalid kernel type for operating on the given data" );
		return false;
	}
	for (int i =0; i < num_inputs_; i++) {
		mdataCollections_[i].setKernel(kernel->clone());
	}


	ni = Root::instance()->new_interface( "multi_dim_scaling",mds_manager + "/" + output_pset_name_);

	MultiDimScalingSpace*  mdsObject = dynamic_cast<MultiDimScalingSpace*>(ni.raw_ptr());

	mdsObject->initialize(output_pset_name_,props_grid,mdata_names);


	return errors->empty();

}
Exemple #8
0
bool MDS_action::initFromDom(QDomDocument parameters, GsTL_project* proj,
		Error_messages_handler* errors)
{
	std::vector<MultiDimScalingSpace::grid_prop_pairT> props_grid;

	SmartPtr<Named_interface> ni =
			Root::instance()->interface( metricData_manager + "/metricRegistrar");

	MetricDataManager* mDataRegistrar = dynamic_cast<MetricDataManager*>(ni.raw_ptr());

	// Get root element
	QDomElement root = parameters.documentElement();

	// Read kernel type
	QDomElement elemKernel = root.firstChildElement("Kernel");
	std::string kernel_type = elemKernel.attribute("type").toStdString();

	// Read space name
	QDomElement spaceName = root.firstChildElement("Spacename");
	std::string space_name = spaceName.attribute("name").toStdString();

	// Read metric data
	QDomElement elemMetricData = root.firstChildElement("MetricData");
	QDomElement elemMetric = elemMetricData.firstChildElement("Metric");
	std::vector<std::string> mdata_names; // vector of metric names
	for( ; !elemMetric.isNull(); elemMetric = elemMetric.nextSiblingElement("Metric")  ) {
		std::string metric_name = elemMetric.attribute("name").toStdString();
		mdata_names.push_back(metric_name);
	}

	// Read Grid
	QDomElement elem = root.firstChildElement("Grid");

	for( ; !elem.isNull(); elem = elem.nextSiblingElement("Grid")  )
	{
		std::string grid_name = elem.attribute("name").toStdString();
		QDomElement gridProps = elem.firstChildElement("Property");
		for (; !gridProps.isNull(); gridProps = gridProps.nextSiblingElement("Property"))
		{
            std::string property_name =
                    gridProps.attribute("name").toStdString();
            props_grid.push_back(MultiDimScalingSpace::grid_prop_pairT(
                                     grid_name,property_name));
			MetricDataCollection mdataCollection =
                    mDataRegistrar->getMetricDataCollection(grid_name,
                                                            property_name, mdata_names);

                        if(mdataCollection.empty()) {
                errors->report( "Some metric data for the property "+
                                property_name+ " on the grid "+grid_name+
                                " could not be found" );
				return false;
			}
			mdataCollections_.push_back(mdataCollection);
		}
	}

	num_inputs_ = mdataCollections_.size();

	// set kernels
	SmartPtr<Named_interface> kernel_ni =
			Root::instance()->new_interface( kernel_type, kernel_manager + "/" );

	if (!kernel_ni.raw_ptr()) {
		errors->report( "Kernel "+kernel_type+" could not be found" );
		return false;
	}
	Distance_kernel* kernel = dynamic_cast<Distance_kernel*>(kernel_ni.raw_ptr());
	if (!kernel) {
		errors->report( "Kernel "+kernel_type+" has an invalid kernel type for operating on the given data" );
		return false;
	}
	for (int i =0; i < num_inputs_; i++) {
		mdataCollections_[i].setKernel(kernel->clone());
	}

	ni = Root::instance()->new_interface( "multi_dim_scaling",mds_manager + "/" + space_name);

	MultiDimScalingSpace*  mdsObject = dynamic_cast<MultiDimScalingSpace*>(ni.raw_ptr());

	mdsObject->initialize(space_name,props_grid,mdata_names);

	// Parse cluster information
	vector<MDSUtil::clusterBlock> kmeansResults;

	QDomElement cluster = root.firstChildElement("Clusters");
	QDomElement clusterUsed = cluster.firstChildElement("ClustersUsed");
	for (;!clusterUsed.isNull(); clusterUsed = clusterUsed.nextSiblingElement("ClustersUsed") )
	{
		// Top level of blocks
		MDSUtil::clusterBlock currentBlock;
		currentBlock.numBlock = clusterUsed.attribute("num").toInt();

		QDomElement clusterID = clusterUsed.firstChildElement("ClusterID");
		for (; !clusterID.isNull(); clusterID = clusterID.nextSiblingElement("ClusterID"))
		{
			std::string currentClustNo = clusterID.attribute("ID").toStdString();
			QDomElement property = clusterID.firstChildElement("Property");
			MDSUtil::cluster currentCluster;

			for (; !property.isNull(); property = property.nextSiblingElement("Property"))
			{
				pair<QString, int> currentProperty;
				currentProperty.first = property.attribute("name");
				currentProperty.second = property.attribute("num").toInt();
				currentCluster.components.push_back(currentProperty);
			}

			currentCluster.numComponents = currentCluster.components.size();

			currentBlock.block.push_back(currentCluster);
		}

		kmeansResults.push_back(currentBlock);
	}

	mdsObject->setKMeanResults(kmeansResults);


	return errors->empty();
}