<commit_before>/*===================================================================

The Medical Imaging Interaction Toolkit (MITK)

Copyright (c) German Cancer Research Center, 
Division of Medical and Biological Informatics.
All rights reserved.

This software is distributed WITHOUT ANY WARRANTY; without 
even the implied warranty of MERCHANTABILITY or FITNESS FOR 
A PARTICULAR PURPOSE.

See LICENSE.txt or http://www.mitk.org for details.

===================================================================*/

#ifndef __mitkNrrdTbssImageReader_cpp
#define __mitkNrrdTbssImageReader_cpp

#include "mitkNrrdTbssImageReader.h"

#include "itkImageFileReader.h"
#include "itkMetaDataObject.h"
#include "itkNrrdImageIO.h"
#include "itkNiftiImageIO.h"

#include <itkImageFileWriter.h>

#include <iostream>
#include <fstream>

#include "itksys/SystemTools.hxx"


namespace mitk
{
      void NrrdTbssImageReader
      ::GenerateData()
  {

    // Since everything is completely read in GenerateOutputInformation() it is stored
    // in a cache variable. A timestamp is associated.
    // If the timestamp of the cache variable is newer than the MTime, we only need to
    // assign the cache variable to the DataObject.
    // Otherwise, the tree must be read again from the file and OuputInformation must
    // be updated!
    if ( ( ! m_OutputCache ) || ( this->GetMTime( ) > m_CacheTime.GetMTime( ) ) )
    {
      this->GenerateOutputInformation();
      itkWarningMacro("Cache regenerated!");
    }

    if (!m_OutputCache)
    {
      itkWarningMacro("Tree cache is empty!")
    }

    int vecsize = m_OutputCache->GetImage()->GetVectorLength();

    static_cast<OutputType*>(this->GetOutput())
        ->SetImage(m_OutputCache->GetImage());
    static_cast<OutputType*>(this->GetOutput())
        ->SetGroupInfo(m_OutputCache->GetGroupInfo());
    static_cast<OutputType*>(this->GetOutput())
        ->SetMetaInfo(m_OutputCache->GetMetaInfo());
    static_cast<OutputType*>(this->GetOutput())
        ->SetIsMeta(m_OutputCache->GetIsMeta());
    static_cast<OutputType*>(this->GetOutput())
        ->SetContainsDistanceMap(m_OutputCache->GetContainsDistanceMap());
    static_cast<OutputType*>(this->GetOutput())
        ->SetContainsMeanSkeleton(m_OutputCache->GetContainsMeanSkeleton());
    static_cast<OutputType*>(this->GetOutput())
        ->SetContainsSkeletonMask(m_OutputCache->GetContainsSkeletonMask());
    static_cast<OutputType*>(this->GetOutput())
        ->SetContainsGradient(m_OutputCache->GetContainsGradient());
    static_cast<OutputType*>(this->GetOutput())
        ->InitializeFromVectorImage();

  }

      void NrrdTbssImageReader
      ::GenerateOutputInformation()
  {
    OutputType::Pointer outputForCache = OutputType::New();

    if ( m_FileName == "")
    {
      throw itk::ImageFileReaderException(__FILE__, __LINE__, "Sorry, the filename to be read is empty!");
    }
    else
    {
      try
      {
        const std::string& locale = "C";
        const std::string& currLocale = setlocale( LC_ALL, NULL );

        if ( locale.compare(currLocale)!=0 )
        {
          try
          {
            MITK_INFO << " ** Changing locale from " << setlocale(LC_ALL, NULL) << " to '" << locale << "'";
            setlocale(LC_ALL, locale.c_str());
          }
          catch(...)
          {
            MITK_INFO << "Could not set locale " << locale;
          }
        }


        MITK_INFO << "NrrdTbssImageReader READING IMAGE INFORMATION";
        ImageType::Pointer img;

        std::string ext = itksys::SystemTools::GetFilenameLastExtension(m_FileName);
        ext = itksys::SystemTools::LowerCase(ext);
        if (ext == ".tbss")
        {
          typedef itk::ImageFileReader<ImageType> FileReaderType;
          FileReaderType::Pointer reader = FileReaderType::New();
          reader->SetFileName(this->m_FileName);
          itk::NrrdImageIO::Pointer io = itk::NrrdImageIO::New();
          reader->SetImageIO(io);

          reader->Update();
          img = reader->GetOutput();


          MITK_INFO << "NrrdTbssImageReader READING HEADER INFORMATION";


          itk::MetaDataDictionary imgMetaDictionary = img->GetMetaDataDictionary();
          std::vector<std::string> imgMetaKeys = imgMetaDictionary.GetKeys();
          std::vector<std::string>::const_iterator itKey = imgMetaKeys.begin();
          std::string metaString;


          //int numberOfGradientImages = 0;
          std::string measurementInfo;
          bool isMeta = false;
          bool containsSkeleton;
          bool containsSkeletonMask;
          bool containsGradient;
          bool containsDistanceMap;



          std::vector<std::pair<mitk::TbssImage::MetaDataFunction, int > > metaInfo;

          std::vector< std::pair<std::string, int> > groups;

          for (; itKey != imgMetaKeys.end(); itKey ++)
          {
            itk::ExposeMetaData<std::string> (imgMetaDictionary, *itKey, metaString);

            MITK_INFO << *itKey << " ---> " << metaString;

            if (itKey->find("Group_index") != std::string::npos)
            {

              std::vector<std::string> tokens;
              this->Tokenize(metaString, tokens, " ");

              if(tokens.size()==2)
              {

                std::cout << tokens.at(0) << " " << tokens.at(1) << std::endl;

                std::pair< std::string, int > p;
                p.first = tokens.at(0);
                std::string s = tokens.at(1);
                p.second = atoi(tokens.at(1).c_str());
                groups.push_back(p);
              }<commit_msg>fixed plot in case of group names with spaces<commit_after>/*===================================================================

The Medical Imaging Interaction Toolkit (MITK)

Copyright (c) German Cancer Research Center, 
Division of Medical and Biological Informatics.
All rights reserved.

This software is distributed WITHOUT ANY WARRANTY; without 
even the implied warranty of MERCHANTABILITY or FITNESS FOR 
A PARTICULAR PURPOSE.

See LICENSE.txt or http://www.mitk.org for details.

===================================================================*/

#ifndef __mitkNrrdTbssImageReader_cpp
#define __mitkNrrdTbssImageReader_cpp

#include "mitkNrrdTbssImageReader.h"

#include "itkImageFileReader.h"
#include "itkMetaDataObject.h"
#include "itkNrrdImageIO.h"
#include "itkNiftiImageIO.h"

#include <itkImageFileWriter.h>

#include <iostream>
#include <fstream>

#include "itksys/SystemTools.hxx"


namespace mitk
{
      void NrrdTbssImageReader
      ::GenerateData()
  {

    // Since everything is completely read in GenerateOutputInformation() it is stored
    // in a cache variable. A timestamp is associated.
    // If the timestamp of the cache variable is newer than the MTime, we only need to
    // assign the cache variable to the DataObject.
    // Otherwise, the tree must be read again from the file and OuputInformation must
    // be updated!
    if ( ( ! m_OutputCache ) || ( this->GetMTime( ) > m_CacheTime.GetMTime( ) ) )
    {
      this->GenerateOutputInformation();
      itkWarningMacro("Cache regenerated!");
    }

    if (!m_OutputCache)
    {
      itkWarningMacro("Tree cache is empty!")
    }

    int vecsize = m_OutputCache->GetImage()->GetVectorLength();

    static_cast<OutputType*>(this->GetOutput())
        ->SetImage(m_OutputCache->GetImage());
    static_cast<OutputType*>(this->GetOutput())
        ->SetGroupInfo(m_OutputCache->GetGroupInfo());
    static_cast<OutputType*>(this->GetOutput())
        ->SetMetaInfo(m_OutputCache->GetMetaInfo());
    static_cast<OutputType*>(this->GetOutput())
        ->SetIsMeta(m_OutputCache->GetIsMeta());
    static_cast<OutputType*>(this->GetOutput())
        ->SetContainsDistanceMap(m_OutputCache->GetContainsDistanceMap());
    static_cast<OutputType*>(this->GetOutput())
        ->SetContainsMeanSkeleton(m_OutputCache->GetContainsMeanSkeleton());
    static_cast<OutputType*>(this->GetOutput())
        ->SetContainsSkeletonMask(m_OutputCache->GetContainsSkeletonMask());
    static_cast<OutputType*>(this->GetOutput())
        ->SetContainsGradient(m_OutputCache->GetContainsGradient());
    static_cast<OutputType*>(this->GetOutput())
        ->InitializeFromVectorImage();

  }

      void NrrdTbssImageReader
      ::GenerateOutputInformation()
  {
    OutputType::Pointer outputForCache = OutputType::New();

    if ( m_FileName == "")
    {
      throw itk::ImageFileReaderException(__FILE__, __LINE__, "Sorry, the filename to be read is empty!");
    }
    else
    {
      try
      {
        const std::string& locale = "C";
        const std::string& currLocale = setlocale( LC_ALL, NULL );

        if ( locale.compare(currLocale)!=0 )
        {
          try
          {
            MITK_INFO << " ** Changing locale from " << setlocale(LC_ALL, NULL) << " to '" << locale << "'";
            setlocale(LC_ALL, locale.c_str());
          }
          catch(...)
          {
            MITK_INFO << "Could not set locale " << locale;
          }
        }


        MITK_INFO << "NrrdTbssImageReader READING IMAGE INFORMATION";
        ImageType::Pointer img;

        std::string ext = itksys::SystemTools::GetFilenameLastExtension(m_FileName);
        ext = itksys::SystemTools::LowerCase(ext);
        if (ext == ".tbss")
        {
          typedef itk::ImageFileReader<ImageType> FileReaderType;
          FileReaderType::Pointer reader = FileReaderType::New();
          reader->SetFileName(this->m_FileName);
          itk::NrrdImageIO::Pointer io = itk::NrrdImageIO::New();
          reader->SetImageIO(io);

          reader->Update();
          img = reader->GetOutput();


          MITK_INFO << "NrrdTbssImageReader READING HEADER INFORMATION";


          itk::MetaDataDictionary imgMetaDictionary = img->GetMetaDataDictionary();
          std::vector<std::string> imgMetaKeys = imgMetaDictionary.GetKeys();
          std::vector<std::string>::const_iterator itKey = imgMetaKeys.begin();
          std::string metaString;


          //int numberOfGradientImages = 0;
          std::string measurementInfo;
          bool isMeta = false;
          bool containsSkeleton;
          bool containsSkeletonMask;
          bool containsGradient;
          bool containsDistanceMap;



          std::vector<std::pair<mitk::TbssImage::MetaDataFunction, int > > metaInfo;

          std::vector< std::pair<std::string, int> > groups;

          for (; itKey != imgMetaKeys.end(); itKey ++)
          {
            itk::ExposeMetaData<std::string> (imgMetaDictionary, *itKey, metaString);

            MITK_INFO << *itKey << " ---> " << metaString;

            if (itKey->find("Group_index") != std::string::npos)
            {

              std::vector<std::string> tokens;
              this->Tokenize(metaString, tokens, " ");


              std::pair< std::string, int > p;

              p.first="";
              for (int i=0; i<tokens.size()-1;i++)
              {
                p.first.append(" ");
                p.first.append(tokens.at(i));
              }

              std::cout << p.first << std::endl;

              p.second = atoi(tokens.at(tokens.size()-1 ).c_str());
              groups.push_back(p);

{'fraction_non_alphanumeric': 0.1483560545308741, 'fraction_numerical': 0.0011583355609017197, 'mean_word_length': 2.869010685970355, 'pattern_counts': {'":': 0, '<': 108, '<?xml version=': 0, '>': 127, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '7663444', 'n_tokens_mistral': 3246, 'n_tokens_neox': 3150, 'n_words': 647}