<commit_before>/*
    Copyright 2011 Brain Research Institute, Melbourne, Australia

    Written by <NAME>, 2011.

    This file is part of MRtrix.

    MRtrix is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    MRtrix is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with MRtrix.  If not, see <http://www.gnu.org/licenses/>.

 */



#include "dwi/tractography/SIFT/sifter.h"

#include "dwi/tractography/mapping/loader.h"

#include "timer.h"


namespace MR
{
  namespace DWI
  {
    namespace Tractography
    {
      namespace SIFT
      {







      SIFTer::SIFTer (Image::Buffer<float>& i, const DWI::Directions::FastLookupSet& d) :
        MapType (i, d),
        fod_data (i),
        dirs (d),
        fmls (dirs, Math::SH::LforN (fod_data.dim (3))),
        output_debug (false),
        remove_untracked_lobes (false),
        min_FOD_integral (0.0),
        term_number (0),
        term_ratio (0),
        term_mu (std::numeric_limits<double>::max())
      {
        Track_lobe_contribution::set_scaling (fod_data);
        H.info() = fod_data.info();
        H.set_ndim (3);
      }



      SIFTer::~SIFTer ()
      {
        for (std::vector<TckCont*>::iterator i = contributions.begin(); i != contributions.end(); ++i) {
          if (*i) {
            delete *i;
            *i = NULL;
          }
        }
      }



      void SIFTer::perform_FOD_segmentation ()
      {
        SH_Writer writer (fod_data, proc_mask);
        Thread::run_queue_threaded_pipe (writer, DWI::SH_coefs(), fmls, DWI::FOD_lobes(), *this);
      }



      void SIFTer::map_streamlines (const std::string& path)
      {
        Tractography::Properties properties;
        Tractography::Reader<float> file (path, properties);

        if (properties.find ("count") == properties.end())
          throw Exception ("Input .tck file does not specify number of streamlines (run tckfixcount on your .tck file!)");
        const track_t num_tracks = to<track_t>(properties["count"]);

        contributions.assign (num_tracks, NULL);

        Tractography::Mapping::TrackLoader loader (file, num_tracks);
        Tractography::Mapping::TrackMapperDixel mapper (H, true, dirs);
        MappedTrackReceiver receiver (*this);
        Thread::run_queue_threaded_pipe (loader, Mapping::TrackAndIndex(), mapper, SetDixel(), receiver);

        tck_file_path = path;
      }



      void SIFTer::remove_excluded_lobes()
      {

        if (!remove_untracked_lobes && !min_FOD_integral)
          return;

        std::vector<size_t> lobe_index_mapping (lobes.size(), 0);
        VoxelAccessor v (accessor);
        Image::LoopInOrder loop (v);

        lobes.clear();
        lobes.push_back (Lobe());
        FOD_sum = 0.0;

        for (loop.start (v); loop.ok(); loop.next (v)) {<commit_msg>Improved SIFT performance during streamline mapping for highly multi-threaded machines<commit_after>/*
    Copyright 2011 Brain Research Institute, Melbourne, Australia

    Written by <NAME>, 2011.

    This file is part of MRtrix.

    MRtrix is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    MRtrix is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with MRtrix.  If not, see <http://www.gnu.org/licenses/>.

 */



#include "dwi/tractography/SIFT/sifter.h"

#include "dwi/tractography/mapping/loader.h"

#include "timer.h"


namespace MR
{
  namespace DWI
  {
    namespace Tractography
    {
      namespace SIFT
      {







      SIFTer::SIFTer (Image::Buffer<float>& i, const DWI::Directions::FastLookupSet& d) :
        MapType (i, d),
        fod_data (i),
        dirs (d),
        fmls (dirs, Math::SH::LforN (fod_data.dim (3))),
        output_debug (false),
        remove_untracked_lobes (false),
        min_FOD_integral (0.0),
        term_number (0),
        term_ratio (0),
        term_mu (std::numeric_limits<double>::max())
      {
        Track_lobe_contribution::set_scaling (fod_data);
        H.info() = fod_data.info();
        H.set_ndim (3);
      }



      SIFTer::~SIFTer ()
      {
        for (std::vector<TckCont*>::iterator i = contributions.begin(); i != contributions.end(); ++i) {
          if (*i) {
            delete *i;
            *i = NULL;
          }
        }
      }



      void SIFTer::perform_FOD_segmentation ()
      {
        SH_Writer writer (fod_data, proc_mask);
        Thread::run_queue_threaded_pipe (writer, DWI::SH_coefs(), fmls, DWI::FOD_lobes(), *this);
      }



      void SIFTer::map_streamlines (const std::string& path)
      {
        Tractography::Properties properties;
        Tractography::Reader<float> file (path, properties);

        if (properties.find ("count") == properties.end())
          throw Exception ("Input .tck file does not specify number of streamlines (run tckfixcount on your .tck file!)");
        const track_t num_tracks = to<track_t>(properties["count"]);

        contributions.assign (num_tracks, NULL);

        Mapping::TrackLoader loader (file, num_tracks);
        Mapping::TrackMapperDixel mapper (H, true, dirs);
        MappedTrackReceiver receiver (*this);
        Thread::run_batched_queue_custom_threading (loader, 1, Mapping::TrackAndIndex(), 100, mapper, 0, SetDixel(), 100, receiver, 0);

        tck_file_path = path;
      }



      void SIFTer::remove_excluded_lobes()
      {

        if (!remove_untracked_lobes && !min_FOD_integral)
          return;

        std::vector<size_t> lobe_index_mapping (lobes.size(), 0);
        VoxelAccessor v (accessor);
        Image::LoopInOrder loop (v);

        lobes.clear();
        lobes.push_back (Lobe());
        FOD_sum = 0.0;

        for (loop.start (v); loop.ok(); loop.next (v)) {
{'fraction_non_alphanumeric': 0.11177278973889143, 'fraction_numerical': 0.006871278057718736, 'mean_word_length': 2.6187845303867405, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, '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': '349729', 'n_tokens_mistral': 2113, 'n_tokens_neox': 1988, 'n_words': 566}