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

    Written by <NAME>, 27/06/08.

    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 <fstream>

#include "app.h"
#include "progressbar.h"
#include "get_set.h"
#include "image/buffer.h"
#include "image/voxel.h"
#include "dwi/tractography/file.h"
#include "dwi/tractography/properties.h"

using namespace MR;
using namespace MR::DWI;
using namespace App;

MRTRIX_APPLICATION

void usage ()
{
  DESCRIPTION
  + "apply a normalisation map to a tracks file.";

  ARGUMENTS
  + Argument ("tracks", "the input track file.").type_file ()
  + Argument ("transform", "the image containing the transform.").type_image_in()
  + Argument ("output", "the output fraction image").type_image_out();
}







void run ()
{
  Tractography::Properties properties;
  Tractography::Reader<> file;
  file.open (argument[0], properties);

  Image::Buffer<float> data (argument[1]);
  Image::Buffer<float>::voxel_type vox (data);

  Tractography::Writer<> writer;
  writer.create (argument[2], properties);

  std::vector<Point<float> > tck;

  Image::Interp::Linear<Image::Buffer<float>::voxel_type> interp (vox);
  ProgressBar progress ("normalising tracks...");

  while (file.next (tck)) {
    for (std::vector<Point<float> >::iterator i = tck.begin(); i != tck.end(); ++i) {
      interp.scanner (*i);
      interp[3] = 0;
      (*i) [0] = interp.value();
      interp[3] = 1;
      (*i) [1] = interp.value();
      interp[3] = 2;
      (*i) [2] = interp.value();
    }
    writer.append (tck);
    writer.total_count++;
    ++progress;
  }

}
<commit_msg>multi-threaded version of tcknormalise<commit_after>/*
    Copyright 2008 Brain Research Institute, Melbourne, Australia

    Written by <NAME>, 27/06/08.

    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 <fstream>

#include "app.h"
#include "progressbar.h"
#include "get_set.h"
#include "image/buffer_preload.h"
#include "image/voxel.h"
#include "thread/queue.h"
#include "dwi/tractography/file.h"
#include "dwi/tractography/properties.h"

using namespace MR;
using namespace MR::DWI;
using namespace App;

MRTRIX_APPLICATION

void usage ()
{
  DESCRIPTION
  + "apply a normalisation map to a tracks file.";

  ARGUMENTS
  + Argument ("tracks", "the input track file.").type_file ()
  + Argument ("transform", "the image containing the transform.").type_image_in()
  + Argument ("output", "the output fraction image").type_image_out();
}



typedef float value_type;
typedef std::vector<Point<value_type> > TrackType;



class Loader 
{
  public:
    Loader (const std::string& file) {
        reader.open (file, properties);
      }

    bool operator() (TrackType& item) {
      return reader.next (item);
    }

    Tractography::Properties properties;
  protected:
    Tractography::Reader<value_type> reader;
};



class Warper
{
  public:
    Warper (const Image::BufferPreload<value_type>::voxel_type& warp_voxel) :
      interp (warp_voxel) { }

    bool operator () (const TrackType& in, TrackType& out) {
      out.resize (in.size());
      for (size_t n = 0; n < in.size(); ++n) 
        out[n] = pos(in[n]);
      return true;
    }

    Point<value_type> pos (const Point<value_type>& x) {
      Point<value_type> p;
      if (!interp.scanner (x)) {
        interp[3] = 0; p[0] = interp.value();
        interp[3] = 1; p[1] = interp.value();
        interp[3] = 2; p[2] = interp.value();
      }
      return p;
    }


  protected:
    Image::Interp::Linear<Image::BufferPreload<value_type>::voxel_type> interp;
};



class Writer 
{
  public:
    Writer (const std::string& file, const Tractography::Properties& properties) :
      progress ("normalising tracks...") {
        writer.create (file, properties);
      }

    bool operator() (const TrackType& item) {
      writer.append (item);
      writer.total_count++;
      ++progress;
      return true;
    }

  protected:
    ProgressBar progress;
    Tractography::Properties properties;
    Tractography::Writer<value_type> writer;
};





void run ()
{
  Loader loader (argument[0]);

  Image::BufferPreload<value_type> data (argument[1]);
  Image::BufferPreload<value_type>::voxel_type vox (data);
  Warper warper (vox);

  Writer writer (argument[2], loader.properties);

  Thread::run_batched_queue (loader, 1, TrackType(), 1024, warper, 0, TrackType(), 1024, writer, 1);
}

{'fraction_non_alphanumeric': 0.12502216705089556, 'fraction_numerical': 0.010285511615534669, 'mean_word_length': 3.486873508353222, 'pattern_counts': {'":': 0, '<': 32, '<?xml version=': 0, '>': 31, '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': '3583267', 'n_tokens_mistral': 1861, 'n_tokens_neox': 1749, 'n_words': 555}