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

    Written by <NAME>, 24/06/11.

    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 "app.h"
#include "point.h"
#include "image/voxel.h"
#include "filter/optimal_threshold.h"
#include "filter/median3D.h"
#include "ptr.h"
#include "dataset/histogram.h"
#include "dataset/buffer.h"
#include "dataset/copy.h"
#include "dataset/loop.h"
#include "dwi/gradient.h"

MRTRIX_APPLICATION

using namespace MR;
using namespace App;

void usage () {
  AUTHOR = "<NAME> (<EMAIL>)";

DESCRIPTION 
  + "Generates an whole brain mask from a DWI image."
  "Both diffusion weighted and b=0 volumes are required to "
  "obtain a mask that includes both brain tissue and CSF.";

ARGUMENTS 
   + Argument ("image",
    "the input DWI image containing volumes that are both diffusion weighted and b=0")
    .type_image_in ()

   + Argument ("image",
    "the output whole brain mask image")
    .type_image_out ();

OPTIONS 

  + Option ("grad",
  "specify the diffusion-weighted gradient scheme used in the acquisition. "
  "The program will normally attempt to use the encoding stored in the image "
  "header. This should be supplied as a 4xN text file with each line is in "
  "the format [ X Y Z b ], where [ X Y Z ] describe the direction of the "
  "applied gradient, and b gives the b-value in units (1000 s/mm^2).")
  + Argument ("encoding").type_file();
}


typedef float value_type;

void run () {
  Image::Header input_header (argument[0]);
  assert (!input_header.is_complex());
  Image::Voxel<value_type> input_voxel (input_header);

  Image::Header mask_header (input_header);
  mask_header.set_ndim(3);
  mask_header.create(argument[1]);
  Image::Voxel<float> mask_voxel (mask_header);

  Math::Matrix<value_type> grad;
  Options opt = get_options ("grad");
  if (opt.size())
    grad.load (opt[0][0]);
  else {
    if (!input_header.DW_scheme().is_set())
      throw Exception ("no diffusion encoding found in image \"" + input_header.name() + "\"");
    grad = input_header.DW_scheme();
  }
  std::vector<int> bzeros, dwis;
  DWI::guess_DW_directions (dwis, bzeros, grad);
  info ("found " + str(dwis.size()) + " diffusion-weighted directions");
<commit_msg>Updated gen_brain_mask to use gradient handling as defined in src/dwi/gradient.h<commit_after>/*
    Copyright 2008 Brain Research Institute, Melbourne, Australia

    Written by <NAME>, 24/06/11.

    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 "app.h"
#include "point.h"
#include "image/voxel.h"
#include "filter/optimal_threshold.h"
#include "filter/median3D.h"
#include "ptr.h"
#include "dataset/histogram.h"
#include "dataset/buffer.h"
#include "dataset/copy.h"
#include "dataset/loop.h"
#include "dwi/gradient.h"

MRTRIX_APPLICATION

using namespace MR;
using namespace App;

void usage () {
  AUTHOR = "<NAME> (<EMAIL>)";

DESCRIPTION 
  + "Generates an whole brain mask from a DWI image."
  "Both diffusion weighted and b=0 volumes are required to "
  "obtain a mask that includes both brain tissue and CSF.";

ARGUMENTS 
   + Argument ("image",
    "the input DWI image containing volumes that are both diffusion weighted and b=0")
    .type_image_in ()

   + Argument ("image",
    "the output whole brain mask image")
    .type_image_out ();

OPTIONS 
  + DWI::GradOption;

}


typedef float value_type;

void run () {
  Image::Header input_header (argument[0]);
  assert (!input_header.is_complex());
  Image::Voxel<value_type> input_voxel (input_header);

  Image::Header mask_header (input_header);
  mask_header.set_ndim(3);
  mask_header.create(argument[1]);
  Image::Voxel<float> mask_voxel (mask_header);

  std::vector<int> bzeros, dwis;
  Math::Matrix<float> grad = DWI::get_DW_scheme<float> (input_header);
  DWI::guess_DW_directions (dwis, bzeros, grad);
  info ("found " + str(dwis.size()) + " diffusion-weighted directions");

{'fraction_non_alphanumeric': 0.1065398796817388, 'fraction_numerical': 0.008150591888220454, 'mean_word_length': 3.9367816091954024, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 20, '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': '2811700', 'n_tokens_mistral': 1660, 'n_tokens_neox': 1575, 'n_words': 579}