<commit_before>
// DCMQI includes
#include "dcmqi/Helper.h"

// DCMTK includes
#include <dcmtk/ofstd/oflist.h>

namespace dcmqi {

  bool Helper::isUndefinedOrPathDoesNotExist(const string &var, const string &humanReadableName) {
    return Helper::isUndefined(var, humanReadableName) || !Helper::pathExists(var);
  }

  bool Helper::isUndefinedOrPathsDoNotExist(vector<string> &var, const string &humanReadableName) {
    return Helper::isUndefined(var, humanReadableName) || !Helper::pathsExist(var);
  }

  bool Helper::pathsExist(const vector<string> &paths) {
    bool allExist = true;
    for(vector<string>::const_iterator iter = paths.begin(); iter != paths.end(); ++iter) {
      if (!Helper::pathExists(*iter)){
        allExist = false;
      }
    }
    return allExist;
  }

  bool Helper::pathExists(const string &path) {
    struct stat buffer;
    if (stat (path.c_str(), &buffer) != 0) {
      cerr << "Error: " << path << " not found!" << endl;
      return false;
    } else {
      return true;
    }
  }

  string Helper::getFileExtensionFromType(const string& type) {
    string extension = ".nrrd";
    if (type == "nii" || type == "nifti")
      extension = ".nii.gz";
    else if (type == "mhd")
      extension = ".mhd";
    else if (type == "mha")
      extension = ".mha";
    else if (type == "img")
      extension = ".img";
    else if (type == "hdr")
      extension = ".hdr";
    else if (type == "nrrd")
      extension = ".nrrd";
    return extension;
  }

  vector<string> Helper::getFileListRecursively(string directory) {
    OFList<OFString> fileList;
    vector<string> dicomImageFiles;
#if _WIN32
    replace(directory.begin(), directory.end(), '/', PATH_SEPARATOR);
#endif
    cout << "Searching recursively " << directory << " for DICOM files" << endl;
    if(OFStandard::searchDirectoryRecursively(directory.c_str(), fileList)) {
      for(OFListIterator(OFString) fileListIterator=fileList.begin(); fileListIterator!=fileList.end(); fileListIterator++) {
        dicomImageFiles.push_back((*fileListIterator).c_str());
      }
    }
    return dicomImageFiles;
  }

  vector<DcmDataset*> Helper::loadDatasets(const vector<string>& dicomImageFiles) {
    vector<DcmDataset*> dcmDatasets;
    OFString tmp, sopInstanceUID;
    DcmFileFormat* sliceFF = new DcmFileFormat();
    for(size_t dcmFileNumber=0; dcmFileNumber<dicomImageFiles.size(); dcmFileNumber++){
      if(sliceFF->loadFile(dicomImageFiles[dcmFileNumber].c_str()).good()){
        DcmDataset* currentDataset = sliceFF->getAndRemoveDataset();
        currentDataset->findAndGetOFString(DCM_SOPInstanceUID, sopInstanceUID);
        bool exists = false;
        for(size_t i=0;i<dcmDatasets.size();i++) {
          dcmDatasets[i]->findAndGetOFString(DCM_SOPInstanceUID, tmp);
          if (tmp == sopInstanceUID) {
            cout << dicomImageFiles[dcmFileNumber].c_str() << " with SOPInstanceUID: " << sopInstanceUID
                 << " already exists" << endl;
            exists = true;
            break;
          }
        }
        if (!exists) {
          dcmDatasets.push_back(currentDataset);
        }
      } else {
        cerr << "Failed to read " << dicomImageFiles[dcmFileNumber] << ". Skipping it." << endl;
      }
    }
    delete sliceFF;
    return dcmDatasets;
  }


  string Helper::floatToStrScientific(float f) {
    ostringstream sstream;
    sstream << scientific << f;
    return sstream.str();
  }

  void Helper::checkValidityOfFirstSrcImage(DcmSegmentation *segdoc) {<commit_msg>ENH: check that source file contains PixelData<commit_after>
// DCMQI includes
#include "dcmqi/Helper.h"

// DCMTK includes
#include <dcmtk/ofstd/oflist.h>

namespace dcmqi {

  bool Helper::isUndefinedOrPathDoesNotExist(const string &var, const string &humanReadableName) {
    return Helper::isUndefined(var, humanReadableName) || !Helper::pathExists(var);
  }

  bool Helper::isUndefinedOrPathsDoNotExist(vector<string> &var, const string &humanReadableName) {
    return Helper::isUndefined(var, humanReadableName) || !Helper::pathsExist(var);
  }

  bool Helper::pathsExist(const vector<string> &paths) {
    bool allExist = true;
    for(vector<string>::const_iterator iter = paths.begin(); iter != paths.end(); ++iter) {
      if (!Helper::pathExists(*iter)){
        allExist = false;
      }
    }
    return allExist;
  }

  bool Helper::pathExists(const string &path) {
    struct stat buffer;
    if (stat (path.c_str(), &buffer) != 0) {
      cerr << "Error: " << path << " not found!" << endl;
      return false;
    } else {
      return true;
    }
  }

  string Helper::getFileExtensionFromType(const string& type) {
    string extension = ".nrrd";
    if (type == "nii" || type == "nifti")
      extension = ".nii.gz";
    else if (type == "mhd")
      extension = ".mhd";
    else if (type == "mha")
      extension = ".mha";
    else if (type == "img")
      extension = ".img";
    else if (type == "hdr")
      extension = ".hdr";
    else if (type == "nrrd")
      extension = ".nrrd";
    return extension;
  }

  vector<string> Helper::getFileListRecursively(string directory) {
    OFList<OFString> fileList;
    vector<string> dicomImageFiles;
#if _WIN32
    replace(directory.begin(), directory.end(), '/', PATH_SEPARATOR);
#endif
    cout << "Searching recursively " << directory << " for DICOM files" << endl;
    if(OFStandard::searchDirectoryRecursively(directory.c_str(), fileList)) {
      for(OFListIterator(OFString) fileListIterator=fileList.begin(); fileListIterator!=fileList.end(); fileListIterator++) {
        dicomImageFiles.push_back((*fileListIterator).c_str());
      }
    }
    return dicomImageFiles;
  }

  vector<DcmDataset*> Helper::loadDatasets(const vector<string>& dicomImageFiles) {
    vector<DcmDataset*> dcmDatasets;
    OFString tmp, sopInstanceUID;
    DcmFileFormat* sliceFF = new DcmFileFormat();
    for(size_t dcmFileNumber=0; dcmFileNumber<dicomImageFiles.size(); dcmFileNumber++){
      if(sliceFF->loadFile(dicomImageFiles[dcmFileNumber].c_str()).good()){
        DcmDataset* currentDataset = sliceFF->getAndRemoveDataset();
        if(!currentDataset->tagExistsWithValue(DCM_PixelData)){
          std::cerr << "Source DICOM file does not contain PixelData, skipping: " << std::endl
             << "  >>>   " << dicomImageFiles[dcmFileNumber] << std::endl;
          continue;
        };
        currentDataset->findAndGetOFString(DCM_SOPInstanceUID, sopInstanceUID);
        bool exists = false;
        for(size_t i=0;i<dcmDatasets.size();i++) {
          dcmDatasets[i]->findAndGetOFString(DCM_SOPInstanceUID, tmp);
          if (tmp == sopInstanceUID) {
            cout << dicomImageFiles[dcmFileNumber].c_str() << " with SOPInstanceUID: " << sopInstanceUID
                 << " already exists" << endl;
            exists = true;
            break;
          }
        }
        if (!exists) {
          dcmDatasets.push_back(currentDataset);
        }
      } else {
        cerr << "Failed to read " << dicomImageFiles[dcmFileNumber] << ". Skipping it." << endl;
      }
    }
    delete sliceFF;
    return dcmDatasets;
  }


  string Helper::floatToStrScientific(float f) {
    ostringstream sstream;
    sstream << scientific << f;
    return sstream.str();
  }

  void Helper::checkValidityOfFirstSrcImage(DcmSegmentation *segdoc) {
{'fraction_non_alphanumeric': 0.13962367057540223, 'fraction_numerical': 0.0013635124079629125, 'mean_word_length': 3.279463243873979, 'pattern_counts': {'":': 0, '<': 113, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5585532', 'n_tokens_mistral': 2380, 'n_tokens_neox': 2371, 'n_words': 466}