#!/usr/bin/env python

# Copyright (c) 2008-2025 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/.
#
# Covered Software is provided under this License on an "as is"
# basis, without warranty of any kind, either expressed, implied, or
# statutory, including, without limitation, warranties that the
# Covered Software is free of defects, merchantable, fit for a
# particular purpose or non-infringing.
# See the Mozilla Public License v. 2.0 for more details.
#
# For more details, see http://www.mrtrix.org/.

# Script that generates a five-tissue-type (5TT) segmented image: the format appropriate for ACT
#
# In this script, major stages of processing can be performed in one of two ways:
#   - Using FSL tools: BET for brain extraction, FAST for tissue segmentation, FIRST for sub-cortical grey matter segmentation
#   - Using segmentations from FreeSurfer
# Alternative algorithms for performing this conversion can be added by creating a new file in lib/mrtrix3/_5ttgen/ and
#   defining the appropriate functions; 5ttgen will automatically make that algorithm available at the command-line



def usage(cmdline): #pylint: disable=unused-variable
  from mrtrix3 import algorithm #pylint: disable=no-name-in-module, import-outside-toplevel

  cmdline.set_author('Robert E. Smith (robert.smith@florey.edu.au)')
  cmdline.set_synopsis('Generate a 5TT image suitable for ACT')
  cmdline.add_citation('Smith, R. E.; Tournier, J.-D.; Calamante, F. & Connelly, A. Anatomically-constrained tractography: Improved diffusion MRI streamlines tractography through effective use of anatomical information. NeuroImage, 2012, 62, 1924-1938')
  cmdline.add_description('5ttgen acts as a \'master\' script for generating a five-tissue-type (5TT) segmented tissue image suitable for use in Anatomically-Constrained Tractography (ACT). A range of different algorithms are available for completing this task. When using this script, the name of the algorithm to be used must appear as the first argument on the command-line after \'5ttgen\'. The subsequent compulsory arguments and options available depend on the particular algorithm being invoked.')
  cmdline.add_description('Each algorithm available also has its own help page, including necessary references; e.g. to see the help page of the \'fsl\' algorithm, type \'5ttgen fsl\'.')

  common_options = cmdline.add_argument_group('Options common to all 5ttgen algorithms')
  common_options.add_argument('-nocrop', action='store_true', default=False, help='Do NOT crop the resulting 5TT image to reduce its size (keep the same dimensions as the input image)')
  common_options.add_argument('-sgm_amyg_hipp', action='store_true', default=False, help='Represent the amygdalae and hippocampi as sub-cortical grey matter in the 5TT image')

  # Import the command-line settings for all algorithms found in the relevant directory
  algorithm.usage(cmdline)



def execute(): #pylint: disable=unused-variable
  from mrtrix3 import algorithm, app, run #pylint: disable=no-name-in-module, import-outside-toplevel

  # Find out which algorithm the user has requested
  alg = algorithm.get_module(app.ARGS.algorithm)

  alg.check_output_paths()

  app.make_scratch_dir()
  alg.get_inputs()
  app.goto_scratch_dir()

  alg.execute()

  stderr = run.command('5ttcheck result.mif').stderr
  if '[WARNING]' in stderr:
    app.warn('Generated image does not perfectly conform to 5TT format:')
    for line in stderr.splitlines():
      app.warn(line)



# Execute the script
import mrtrix3
mrtrix3.execute() #pylint: disable=no-member
