#!/usr/bin/perl -w #--------------------------------------------------------------------------- #@COPYRIGHT : # Copyright 1996, John G. Sled, # McConnell Brain Imaging Centre, # Montreal Neurological Institute, McGill University. # Permission to use, copy, modify, and distribute this # software and its documentation for any purpose and without # fee is hereby granted, provided that the above copyright # notice appear in all copies. The author and McGill University # make no representations about the suitability of this # software for any purpose. It is provided "as is" without # express or implied warranty. #---------------------------------------------------------------------------- #$RCSfile: nu_evaluate.in,v $ #$Revision: 1.4 $ #$Author: claude $ #$Date: 2010-12-09 19:35:01 $ #$State: Exp $ #--------------------------------------------------------------------------- # ------------------------------ MNI Header ---------------------------------- #@NAME : NUevaluate #@INPUT : #@OUTPUT : #@RETURNS : #@DESCRIPTION: correct a volume for intensity non-uniformity artifacts #@ using supplied (.imp) field #@METHOD : #@GLOBALS : #@CALLS : #@CREATED : August 7, 1996 J.G.Sled #@MODIFIED : # $Id: nu_evaluate.in,v 1.4 2010-12-09 19:35:01 claude Exp $ #----------------------------------------------------------------------------- use MNI::Startup qw(nocputimes); use MNI::Spawn; use MNI::FileUtilities qw(check_output_dirs); use MNI::PathUtilities qw(split_path); ; use MNI::MincUtilities qw(:history volume_minmax); use Getopt::Tabular; # Start main program ------------------------------------------------ &Initialize; # Obtain some crude mask if none is given. if( !( defined $user_mask_volume ) || !( -e $user_mask_volume ) ) { my $threshold = 0; &Spawn(['mincstats', '-quiet', '-biModalT', $input_volume], err_action => 'ignore', stdout => \$threshold ); $user_mask_volume = "${TmpDir}/nuc_auto_mask.mnc"; &Spawn(['minccalc', '-quiet', '-byte', '-expression', "if(A[0]>=$threshold){out=1}else{out=0}", $input_volume, $user_mask_volume]); } # Evaluate the field from the compact splines .imp file. Using a # mask is not critical, but not using one will create different # min/max values for the minc ranges, especially if the splines # are "wild" outside the mask. &Spawn(['evaluate_field', '-mask', $user_mask_volume, '-like', $input_volume, $mapping_name, $field_volume]); # Correct the field outside the masked region. &Spawn([ 'correct_field', $field_volume, $user_mask_volume, $field_volume ]); # put lower bound on field intensity if necessary ($volmin) = &volume_minmax($field_volume); if($volmin < $field_floor) { &Spawn(['mincmath', '-clamp', '-const2', $field_floor, 1.79769e+308, $field_volume, "${field_volume}.temp"]); &Spawn(['mv', "${field_volume}.temp", $field_volume]); } &Spawn(['mincmath', '-copy_header', '-zero', '-div', $input_volume, $field_volume, $output_volume]); &CreateHistory($input_volume,$output_volume,$mapping_name, localtime(time) . ">>> $0 @ARGV"); $Compress && &Spawn(['gzip', '-f', $output_volume]); # End of Main Program #----------------------------------------------------------------------------- # ------------------------------ MNI Header ---------------------------------- #@NAME : &CreateHistory #@INPUT : $_[0] name source volume # $_[1] name of output volume to get history # $_[2] name of imp file containing mapping history # $_[3] string to append to history #@OUTPUT : #@RETURNS : #@DESCRIPTION: #@METHOD : #@GLOBALS : #@CALLS : get_history, put_history #@CREATED : #@MODIFIED : #----------------------------------------------------------------------------- sub CreateHistory { my ($input, $output, $imp, $str) = @_; my (@imp_history) = (); # find history string in .imp file open(IMP, $imp) || die "Cannot open .imp file: $imp\n"; if( =~ /MNI Field File/i) { while() { if($_ =~ /^%/) { # take first line after header that starts with a % $imp_history = (substr($_,1)); last; } } } else { warn("Mapping file $imp does not conform\nto MNI field file format." ." History information of output\nvolume will be incomplete.\n"); } my (@history); @history = &get_history($input); push(@history, ($imp_history, $str)); &put_history($output, @history); } # ------------------------------ MNI Header ---------------------------------- #@NAME : &SetupArgTables #@INPUT : none #@OUTPUT : none #@RETURNS : References to the two option tables: # @pref_args # @protocol_args #@DESCRIPTION: Defines the tables of command line (and config file) # options that we pass to ParseArgs. #@METHOD : #@GLOBALS : makes references to many globals (almost all of 'em in fact) # even though most of them won't have been defined when # this is called #@CALLS : #@CREATED : #@MODIFIED : #----------------------------------------------------------------------------- sub SetupArgTables { my (@pref_args, @protocol_args); sub print_version { print "Program $ProgramName, built from:\n$LongVersion\n"; exit; } # Preferences -- these shouldn't affect the results @pref_args = (["General Options", "section"], ["-verbose|-quiet", "boolean", 0, \$Verbose, "be noisy [default; opposite is -quiet]" ], ["-execute|-noexecute", "boolean", 0, \$Execute, "actually execute planned commands [default]"], ["-clobber|-noclobber", "boolean", 0, \$Clobber, "overwrite output files [default: -noclobber]"], ["-compress|-nocompress", "boolean", 0, \$Compress, "compress output files [default: -nocompress]"], ["-tmpdir", "string", 1, \$TmpDir, "temporary working directory"], ["-keeptmp|-nokeeptmp", "boolean", 0, \$KeepTmp, "don't delete temporary files [default: -nokeeptmp]"], ["-notify", "string", 1, \$Notify, "set the user to notify (send email to) on failure " . "[default: \$USER, i.e. you]"], ["-nonotify", "const", 0, \$Notify, "disable email notification"], ["-version", "call", undef, \&print_version, "print version and quit"]); # Protocol (data-specific) options @protocol_args = (["General Options","section"], ["-mapping", "string", 1, \$mapping_name, "specify an intensity mapping", ""], ["-mask", "volume", 1, \$user_mask_volume, "specify region for processing.", ""], ["-field", "string", 1, \$output_field, "save the correction field in this file", ""], ["-floor", "float", 1, \$field_floor, "lower bound for field intensities to avoid numerical problems. " ."(Default 0.1)",""], ["-shrink", "integer", 1, \$shrink_factor, "OBSOLETE", ""]); (\@pref_args, \@protocol_args); } # ------------------------------ MNI Header ---------------------------------- #@NAME : &SetHelp #@INPUT : none #@OUTPUT : none #@RETURNS : nothing #@DESCRIPTION: Sets the $Help and $Usage globals, and registers them # with ParseArgs so that user gets useful error and help # messages. #@METHOD : #@GLOBALS : $Help, $Usage #@CALLS : #@CREATED : #@MODIFIED : #----------------------------------------------------------------------------- sub SetHelp { $Version = '1.12.0'; $LongVersion = 'Package MNI N3, version 1.12.0, compiled by nicks@gust.nmr.mgh.harvard.edu (x86_64-apple-darwin11.4.2) on 2015-06-19 at 15:37:08'; $Usage = < 2); RegisterPrograms([('mincinfo','mincmath', 'mincstats', 'minccalc', 'evaluate_field', 'make_template', 'mincresample', 'correct_field', 'gzip', 'mv' )]); $verbose_option = ($Verbose) ? '-verbose' : '-quiet'; AddDefaultArgs('mincmath', ['-clobber', $verbose_option]); AddDefaultArgs('minccalc', ['-clobber', $verbose_option]); AddDefaultArgs('evaluate_field', ['-clobber', $verbose_option]); }