void
Grid::savePredictionGrid(QString fname)
{
    fstream file_op((fname.toStdString()).c_str(), ios::out);

    //first line is the width of the grid
    file_op << som_width << endl;

    // second line is the height of the grid
    file_op << som_height << endl;

    for (int i = 0; i < files_.size(); i++)
    {
        // output the vector index and filename
        QList<std::string> temp = files_[i];
        for(int j = 0; j < files_[i].size(); j++ )
        {
            file_op << i;
            file_op << "," + temp.takeFirst() << endl;
            emit repaintSignal(); // tell the GUI to redraw itself
        }
    }
    file_op.close();

}
void TransFuncEditorIntensityGradient::createConnections() {
    if (!supported_)
        return;

    // buttons
    connect(loadButton_,  SIGNAL(clicked()), this, SLOT(loadTransferFunction()));
    connect(saveButton_,  SIGNAL(clicked()), this, SLOT(saveTransferFunction()));
    connect(clearButton_, SIGNAL(clicked()), painter_, SLOT(resetTransferFunction()));

    connect(gridEnabledButton_,      SIGNAL(clicked()), this, SLOT(toggleShowGrid()));
    connect(histogramEnabledButton_, SIGNAL(clicked()), this, SLOT(toggleShowHistogram()));

    connect(quadButton_,   SIGNAL(clicked()), painter_, SLOT(addQuadPrimitive()));
    connect(bananaButton_, SIGNAL(clicked()), painter_, SLOT(addBananaPrimitive()));
    connect(deleteButton_, SIGNAL(clicked()), painter_, SLOT(deletePrimitive()));
    connect(colorButton_,  SIGNAL(clicked()), painter_, SLOT(colorizePrimitive()));

    connect(histogramBrightness_, SIGNAL(sliderMoved(int)), painter_, SLOT(histogramBrightnessChanged(int)));
    connect(histogramLog_, SIGNAL(stateChanged(int)), painter_, SLOT(toggleHistogramLogarithmic(int)));

    // slider
    connect(fuzziness_, SIGNAL(valueChanged(int)), painter_, SLOT(fuzzinessChanged(int)));
    connect(transparency_, SIGNAL(valueChanged(int)), painter_, SLOT(transparencyChanged(int)));

    connect(fuzziness_, SIGNAL(sliderPressed()), this, SLOT(startTracking()));
    connect(transparency_, SIGNAL(sliderPressed()), this, SLOT(startTracking()));

    connect(fuzziness_, SIGNAL(sliderReleased()), this, SLOT(stopTracking()));
    connect(transparency_, SIGNAL(sliderReleased()), this, SLOT(stopTracking()));

    connect(painter_, SIGNAL(setTransparencySlider(int)), this, SLOT(setTransparency(int)));
    connect(painter_, SIGNAL(primitiveDeselected()), this, SLOT(primitiveDeselected()));
    connect(painter_, SIGNAL(primitiveSelected()), this, SLOT(primitiveSelected()));
    connect(painter_, SIGNAL(toggleInteractionMode(bool)), this, SLOT(toggleInteractionMode(bool)));
    connect(painter_, SIGNAL(repaintSignal()), this, SLOT(repaintSignal()));

}
GridDisplay::GridDisplay(int winSize, Tracklist *tracklist, Grid* grid_, QWidget *parent)
: MyDisplay(tracklist, parent), _winSize(winSize)
{
	this->grid_ = grid_;
	_cellSize = grid_->getCellSize(_winSize);
	setMinimumSize(winSize, winSize);
	setMouseTracking(true);
	setAcceptDrops(true);
	squareHasInitialized.resize(grid_->getHeight() * grid_->getWidth());
	for(int i = 0; i < grid_->getHeight() * grid_->getWidth(); i++)
		squareHasInitialized[i] = false;
	oldXPos = -1;
	oldYPos = -1;
	oldX1Pos = -1;
	oldY1Pos = -1;
	fullScreenMouseOn = false;
	initDone = false;
	fullScreenTimer = new QTimer(this);
	fullScreenTimer->setInterval(150);
	colourMapMode_ = false;


	connect(this, SIGNAL(clearMode()), grid_, SLOT(clearMode()));
	connect(this, SIGNAL(extractMode()), grid_, SLOT(setExtractMode()));
	connect(this, SIGNAL(trainMode()), grid_, SLOT(setTrainMode()));
	connect(this, SIGNAL(predictMode()), grid_, SLOT(setPredictMode()));
	connect(this, SIGNAL(initMode()), grid_, SLOT(setInitMode()));
	connect(this, SIGNAL(savePredictionGridSignal(QString)), grid_, SLOT(savePredictionGrid(QString)));
	connect(this, SIGNAL(openPredictionGridSignal(QString)), grid_, SLOT(openPredictionGrid(QString)));
	connect(grid_, SIGNAL(repaintSignal()), this, SLOT(repaintSlot()));
	connect(this, SIGNAL(cancelButtonPressed()), grid_, SLOT(cancelPressed()));
	connect(this, SIGNAL(hashLoadPressed()), grid_, SLOT(openHash()));
	connect(fullScreenTimer, SIGNAL(timeout()), this, SLOT(fullScreenMouseMove()));
	connect(grid_, SIGNAL(errorBox(QString)), this, SLOT(showErrorMessage(QString)));
	connect(this, SIGNAL(resetGridAction()), grid_, SLOT(resetGridSlot()));
}
void TransFuncIntensityGradientPainter::updateTF() {
    tf_->invalidateTexture();
    emit repaintSignal();
}
void Grid::predict() {
    int ready = 0;
std:
    string fileName;

    if ( _collection->getNumTracks() > 0 ) {
        ready = 1;
        fileName = "margrid_train.mf";
    } else if (trainFile_.length() > 0) {
        fileName = trainFile_;
    } else {
        emit errorBox("Need to load a collection of audio files first!");
        return;
    }

    realvec som_in;
    realvec som_res;
    realvec som_res1;

    realvec som_fmatrix;
    QDir dir;

    ifstream iss1;

    iss1.open("som.mpl");
    som_ = mng.getMarSystem(iss1);
    iss1.close();

    ifstream niss1;
    niss1.open("norm.mpl");
    norm_ = mng.getMarSystem(niss1);
    niss1.close();



    cout << "Starting prediction" << endl;
    som_->updctrl("mrs_string/mode", "predict");

    Collection l1;
    l1.read(fileName);

    cout << "Read collection" << endl;

    total_->updctrl("mrs_string/filename", fileName);

    total_->updctrl("mrs_natural/pos", 0);

    som_->updctrl("mrs_natural/inSamples", 1);


    realvec predict_res(som_->getctrl("mrs_natural/onObservations")->to<mrs_natural>(),
                        som_->getctrl("mrs_natural/onSamples")->to<mrs_natural>());
    norm_->updctrl("mrs_natural/inSamples", 1);



    realvec norm_som_res;


    mrs_natural inObs = total_->getctrl("mrs_natural/inObservations")->to<mrs_natural>();
    mrs_natural inSms = total_->getctrl("mrs_natural/inSamples")->to<mrs_natural>();

    mrs_natural onObs = total_->getctrl("mrs_natural/onObservations")->to<mrs_natural>();
    mrs_natural onSms = total_->getctrl("mrs_natural/onSamples")->to<mrs_natural>();

    som_in.create(inObs, inSms);
    som_res.create(onObs, onSms);
    som_res1.create(onObs+2, onSms);
    norm_som_res.create(onObs+2, onSms);



    ofstream oss1;
    oss1.open("som4.mpl");
    oss1 << *som_ << endl;
    oss1.close();


    realvec norm_som_fmatrix;
    ifstream iss;
    iss.open("norm_som_fmatrix.txt");
    iss >> norm_som_fmatrix;


    cout << l1.size() <<endl;

    QString tempString;
    for (int index = 0; index < l1.size(); index++)
    {
        if(cancel_)
        {
            cancel_ = false;
            break;
        }

        total_->updctrl("mrs_natural/label", index);
        total_->updctrl("mrs_bool/memReset", true);
        total_->updctrl("mrs_natural/cindex", index);


        total_->process(som_in, som_res);
        for (int o=0; o < onObs; o++)
            som_res1(o, 0) = som_res(o, 0);
        QString current = total_->getctrl("mrs_string/currentlyPlaying")->to<mrs_string>().c_str();
        cout << total_->getctrl("mrs_string/currentlyPlaying")->to<mrs_string>() << endl;

        norm_->process(som_res1, norm_som_res);

        realvec foobar;
        foobar.create(som_->getctrl("mrs_natural/inObservations")->to<mrs_natural>(), som_->getctrl("mrs_natural/inSamples")->to<mrs_natural>());

        norm_som_fmatrix.getCol(index, foobar);

        som_->process(foobar, predict_res);

        grid_x = predict_res(0);
        grid_y = predict_res(1);
        addTrack(grid_x, grid_y, current);
        emit repaintSignal();


        total_->updctrl("mrs_natural/advance", 1);
    }


    cout << "end_prediction" << endl;
}