Browse Source

Modification de l'enregistrement des resultats dans csl

Rémi Synave 2 years ago
parent
commit
edb96d23f0
2 changed files with 72 additions and 75 deletions
  1. 68 73
      csl/Experiment.cpp
  2. 4 2
      csl/Experiment.hpp

+ 68 - 73
csl/Experiment.cpp

@@ -31,9 +31,10 @@ Experiment::Experiment ():QMainWindow()
   filenames_json = NULL;
   suffix = NULL;
   choice = NULL;
+  dataPath = new QString(QDir::currentPath()+"/data");
+  resultPath = NULL;
   algo_side='X';
-  dNFixed = false;
-  expeInit = false;
+  expeStarted = false;
 
   setWindowTitle (tr ("DSL - Draw Strength Line"));
 
@@ -119,25 +120,9 @@ Experiment::Experiment ():QMainWindow()
 void
 Experiment::start ()
 {
-
-  QString dir;
-  QString* directoryname;
+  bool ok = false;
   
-  if(!dNFixed)
-    {
-      dir = QString(QDir::currentPath()+"/data");
-      directoryname = new QString(dir);
-      
-      QDir::setCurrent(*directoryname);
-      dNFixed = true;
-    }else
-    {
-      dir = QString(QDir::currentPath());
-      directoryname = new QString(dir);
-    }
-
-  
-  if(!(QDir(*directoryname).exists()))
+  if(!(QDir(*dataPath).exists()))
     {
       QMessageBox msgBox;
       msgBox.setText("Data files can't be found. data folder is nedded.");
@@ -145,70 +130,80 @@ Experiment::start ()
       std::exit(EXIT_FAILURE);
     }
   
-  if(!dir.isEmpty())
+  suffix = new QString(QInputDialog::getText(this, tr("Your ID"),
+					     tr("ID to use :"), QLineEdit::Normal,
+					     QDir::home().dirName(), &ok));
+
+  if((*suffix) == "algo" || (*suffix) == "random" || (*suffix) == "_algo" || (*suffix) == "_random")
     {
-      bool ok=false;
-      suffix = new QString(QInputDialog::getText(this, tr("Your ID"),
-						 tr("ID to use :"), QLineEdit::Normal,
-						 QDir::home().dirName(), &ok));
+      QMessageBox msgBox;
+      msgBox.setText("Your name can't be [_]algo or [_]random.");
+      msgBox.exec();
+      ok = false;
+    }
 
-      if((*suffix) == "algo" || (*suffix) == "random" || (*suffix) == "_algo" || (*suffix) == "_random")
-	{
-	  QMessageBox msgBox;
-	  msgBox.setText("Your name can't be [_]algo or [_]random.");
-	  msgBox.exec();
-	  ok = false;
-	}
+  resultPath = new QString(QDir::currentPath()+"/"+(*suffix));
+
+  if(QDir(*resultPath).exists())
+    {
+      QMessageBox msgBox;
+      msgBox.setText("This id is already used. Choose another one or take the necessary action to solve the problem.");
+      msgBox.exec();
+      ok = false;
+    }
+  else
+    {
+      QDir(QDir::currentPath()).mkdir(*suffix);
+    }
       
-      if(ok)
-	{
-	  delete filenames;
-	  delete filenames_json;
-	  delete choice;
-	  filenames = new QStringList();
-	  filenames_json = new QStringList();
-	  choice = new QStringList();
+  if(ok)
+    {
+      delete filenames;
+      delete filenames_json;
+      delete choice;
+      filenames = new QStringList();
+      filenames_json = new QStringList();
+      choice = new QStringList();
 	  
-	  QDirIterator it(*directoryname, {"*.jpg"}, QDir::Files);
-	  while (it.hasNext()) {
-	    filenames->append( it.next() );
-	  }
+      QDirIterator it(*dataPath, {"*.jpg"}, QDir::Files);
+      while (it.hasNext()) {
+	  filenames->append( it.next() );
+      }
 
-	  for(int i = 0 ; i<filenames->size() ; i++)
+      for(int i = 0 ; i<filenames->size() ; i++)
+	{
+	  QString jsonFile((*dataPath)+"/"+QFileInfo(filenames->at(i)).baseName()+(*suffix_IA)+".json");
+	  if(! (QFile(jsonFile).exists()) )
 	    {
-	      QString jsonFile(QDir::currentPath ().append("/").append(QFileInfo(filenames->at(i)).baseName().append(*suffix_IA).append(".json")));
-	      if(! (QFile(jsonFile).exists()) )
-		{
-		  qDebug() << "ATTENTION !!!! " << jsonFile << " file missing";
-		  ok=false;
-		}else{
-		filenames_json->append(jsonFile);
-	      }
-	    }
+	      qDebug() << "WARNING !!!! " << jsonFile << " file missing";
+	      ok=false;
+	    }else{
+	    filenames_json->append(jsonFile);
+	  }
+	}
 
-	  if(!ok)
-	    std::exit(EXIT_FAILURE);
+      if(!ok)
+	std::exit(EXIT_FAILURE);
 
-	  std::srand(static_cast<unsigned int>(std::time(nullptr)+std::rand())); 
+      std::srand(static_cast<unsigned int>(std::time(nullptr)+std::rand())); 
 	  
-	  for(int i = 0 ; i<filenames->size()*2 ; i++)
-	    {
-	      int rand1 = std::rand()%filenames->size();
-	      int rand2 = std::rand()%filenames->size();
-	      filenames->swapItemsAt(rand1, rand2);
-	      filenames_json->swapItemsAt(rand1, rand2);
-	    }
-	  openedImage = -1;
-
-	  expeInit = true;
-	  openNext();
+      for(int i = 0 ; i<filenames->size()*2 ; i++)
+	{
+	  int rand1 = std::rand()%filenames->size();
+	  int rand2 = std::rand()%filenames->size();
+	  filenames->swapItemsAt(rand1, rand2);
+	  filenames_json->swapItemsAt(rand1, rand2);
 	}
+      openedImage = -1;
+
+      expeStarted = true;
+      openNext();
     }
   
 }
 
 void Experiment::leftImage(){
-  if(expeInit)
+  if(expeStarted)
     {
       if((algo_side != 'L') && (algo_side != 'R'))
 	{
@@ -225,7 +220,7 @@ void Experiment::leftImage(){
 }
 
 void Experiment::rightImage(){
-  if(expeInit)
+  if(expeStarted)
     {
       if((algo_side != 'L') && (algo_side != 'R'))
 	{
@@ -267,7 +262,7 @@ Experiment::openNext (){
       root["choices"] = choices;
       
       QByteArray ba = QJsonDocument(root).toJson();
-      QFile fout((*suffix)+QString::fromStdString (".json"));
+      QFile fout((*resultPath)+"/"+(*suffix)+QString::fromStdString (".json"));
       fout.open(QIODevice::WriteOnly);
       fout.write(ba);
       
@@ -287,7 +282,7 @@ Experiment::openNext (){
 	  for(int i = 0 ; i < waLeft->getNumberOfLines() ; i++)
 	    {
 	      waRight->addRandomSL();
-	      QString jsonFile = QDir::currentPath ().append("/").append(QFileInfo(filenames->at(openedImage)).baseName().append(QString("_random")).append(".json"));
+	      QString jsonFile = (*resultPath)+"/"+(QFileInfo(filenames->at(openedImage)).baseName()+(QString("_random"))+".json");
 	      waRight->saveStrengthLine (jsonFile.toStdString ());
 	    }
 	}
@@ -298,7 +293,7 @@ Experiment::openNext (){
 	  for(int i = 0 ; i < waRight->getNumberOfLines() ; i++)
 	    {
 	      waLeft->addRandomSL();
-	      QString jsonFile = QDir::currentPath ().append("/").append(QFileInfo(filenames->at(openedImage)).baseName().append(QString("_random")).append(".json"));
+	      QString jsonFile = (*resultPath)+"/"+(QFileInfo(filenames->at(openedImage)).baseName()+(QString("_random"))+".json");
 	      waLeft->saveStrengthLine (jsonFile.toStdString ());
 	    }
 	}

+ 4 - 2
csl/Experiment.hpp

@@ -22,10 +22,12 @@ private:
   const int time = 100;
   const int maxProgressBar = 100;
   const QString *suffix_IA = new QString("_algo");
+  const QString *suffix_random = new QString("_random");
+  QString *dataPath;
+  QString *resultPath;
   int screenWidth, screenHeight;
   char algo_side;
-  bool dNFixed;
-  bool expeInit;
+  bool expeStarted;
 
   QMenu *fileMenu;