<commit_before>/*
 * Copyright (c) 2008-2016 the MRtrix3 contributors
 * 
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/
 * 
 * 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.
 * 
 * For more details, see www.mrtrix.org
 * 
 */

#ifndef __image_filter_gaussian_h__
#define __image_filter_gaussian_h__

#include "memory.h"
#include "image.h"
#include "algo/copy.h"
#include "algo/threaded_copy.h"
#include "adapter/gaussian1D.h"
#include "filter/base.h"

namespace MR
{
  namespace Filter
  {
    /** \addtogroup Filters
    @{ */

    /*! Smooth images using a Gaussian kernel.
     *
     * Typical usage:
     * \code
     * auto input = Image<float>::open (argument[0]);
     * Filter::Smooth smooth_filter (input);
     * smooth_filter.set_stdev (2.0);
     * auto output = Image::create<float> (argument[1], smooth_filter);
     * smooth_filter (input, output);
     *
     * \endcode
     */
    class Smooth : public Base
    {

      public:
        template <class HeaderType>
        Smooth (const HeaderType& in) :
            Base (in),
            extent (3, 0),
            stdev (3, 0.0)
        {
          for (int i = 0; i < 3; i++)
            stdev[i] = in.spacing(i);
          datatype() = DataType::Float32;
        }

        template <class HeaderType>
        Smooth (const HeaderType& in, const std::vector<default_type>& stdev) :
            Base (in),
            extent (3, 0),
            stdev (3, 0.0)
        {
          set_stdev (stdev);
          datatype() = DataType::Float32;
        }

        //! Set the extent of smoothing kernel in voxels.
        //! This can be set as a single value to be used for the first 3 dimensions
        //! or separate values, one for each dimension. (Default: 4 standard deviations)
        void set_extent (const std::vector<int>& new_extent)
        {
          if (new_extent.size() != 1 && new_extent.size() != 3)
            throw Exception ("Please supply a single kernel extent value, or three values (one for each spatial dimension)");
          for (size_t i = 0; i < new_extent.size(); ++i) {
            if (!(new_extent[i] & int (1)))
              throw Exception ("expected odd number for extent");
            if (new_extent[i] < 0)
              throw Exception ("the kernel extent must be positive");
          }
          if (new_extent.size() == 1)
            for (unsigned int i = 0; i < 3; i++)
              extent[i] = new_extent[0];
          else
            extent = new_extent;
        }

        void set_stdev (default_type stdev) {
          set_stdev (std::vector<default_type> (3, stdev));
        }

        //! Set the standard deviation of the Gaussian defined in mm.
        //! This must be set as a single value to be used for the first 3 dimensions
        //! or separate values, one for each dimension. (Default: 1 voxel)
        void set_stdev (const std::vector<default_type>& std_dev)
        {
          if (std_dev.size() == 1) {
            for (unsigned int i = 0; i < 3; i++)
              stdev[i] = std_dev[0];
          } else {
            if (stdev.size() != 3)
              throw Exception ("Please supply a single standard deviation value, or three values (one for each spatial dimension)");
            stdev = std_dev;
          }
          for (size_t i = 0; i < stdev.size(); ++i)
            if (stdev[i] < 0.0)
              throw Exception ("the Gaussian stdev values cannot be negative");
        }

        //! Smooth the input image. Both input and output images can be the same image
        template <class InputImageType, class OutputImageType, typename ValueType = float>
        void operator() (InputImageType& input, OutputImageType& output)
        {
          std::shared_ptr <Image<ValueType> > in (std::make_shared<Image<ValueType> > (Image<ValueType>::scratch (input)));<commit_msg>fix bug in settting stdev in smooth filter<commit_after>/*
 * Copyright (c) 2008-2016 the MRtrix3 contributors
 * 
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/
 * 
 * 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.
 * 
 * For more details, see www.mrtrix.org
 * 
 */

#ifndef __image_filter_gaussian_h__
#define __image_filter_gaussian_h__

#include "memory.h"
#include "image.h"
#include "algo/copy.h"
#include "algo/threaded_copy.h"
#include "adapter/gaussian1D.h"
#include "filter/base.h"

namespace MR
{
  namespace Filter
  {
    /** \addtogroup Filters
    @{ */

    /*! Smooth images using a Gaussian kernel.
     *
     * Typical usage:
     * \code
     * auto input = Image<float>::open (argument[0]);
     * Filter::Smooth smooth_filter (input);
     * smooth_filter.set_stdev (2.0);
     * auto output = Image::create<float> (argument[1], smooth_filter);
     * smooth_filter (input, output);
     *
     * \endcode
     */
    class Smooth : public Base
    {

      public:
        template <class HeaderType>
        Smooth (const HeaderType& in) :
            Base (in),
            extent (3, 0),
            stdev (3, 0.0)
        {
          for (int i = 0; i < 3; i++)
            stdev[i] = in.spacing(i);
          datatype() = DataType::Float32;
        }

        template <class HeaderType>
        Smooth (const HeaderType& in, const std::vector<default_type>& stdev) :
            Base (in),
            extent (3, 0),
            stdev (3, 0.0)
        {
          set_stdev (stdev);
          datatype() = DataType::Float32;
        }

        //! Set the extent of smoothing kernel in voxels.
        //! This can be set as a single value to be used for the first 3 dimensions
        //! or separate values, one for each dimension. (Default: 4 standard deviations)
        void set_extent (const std::vector<int>& new_extent)
        {
          if (new_extent.size() != 1 && new_extent.size() != 3)
            throw Exception ("Please supply a single kernel extent value, or three values (one for each spatial dimension)");
          for (size_t i = 0; i < new_extent.size(); ++i) {
            if (!(new_extent[i] & int (1)))
              throw Exception ("expected odd number for extent");
            if (new_extent[i] < 0)
              throw Exception ("the kernel extent must be positive");
          }
          if (new_extent.size() == 1)
            for (unsigned int i = 0; i < 3; i++)
              extent[i] = new_extent[0];
          else
            extent = new_extent;
        }

        void set_stdev (default_type stdev) {
          set_stdev (std::vector<default_type> (3, stdev));
        }

        //! Set the standard deviation of the Gaussian defined in mm.
        //! This must be set as a single value to be used for the first 3 dimensions
        //! or separate values, one for each dimension. (Default: 1 voxel)
        void set_stdev (const std::vector<default_type>& std_dev)
        {
          if (std_dev.size() == 1) {
            for (unsigned int i = 0; i < 3; i++)
              stdev[i] = std_dev[0];
          } else {
            if (std_dev.size() != 3)
              throw Exception ("Please supply a single standard deviation value, or three values (one for each spatial dimension)");
            stdev = std_dev;
          }
          for (size_t i = 0; i < stdev.size(); ++i)
            if (stdev[i] < 0.0)
              throw Exception ("the Gaussian stdev values cannot be negative");
        }

        //! Smooth the input image. Both input and output images can be the same image
        template <class InputImageType, class OutputImageType, typename ValueType = float>
        void operator() (InputImageType& input, OutputImageType& output)
        {
          std::shared_ptr <Image<ValueType> > in (std::make_shared<Image<ValueType> > (Image<ValueType>::scratch (input)));
{'fraction_non_alphanumeric': 0.11139301679352423, 'fraction_numerical': 0.013531472755829407, 'mean_word_length': 2.1237735849056603, 'pattern_counts': {'":': 0, '<': 45, '<?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': '190708', 'n_tokens_mistral': 2516, 'n_tokens_neox': 2404, 'n_words': 850}