Module: DefaultRecon

Included in:
Reconstruction
Defined in:
lib/default_methods/default_recon.rb,
lib/default_methods/recon/raw_sequence.rb,
lib/default_methods/recon/physionoise_helper.rb

Overview

require ‘default_methods/recon/physionoise_helper’

Defined Under Namespace

Classes: DicomRawSequence, PfileRawSequence, RawSequence

Constant Summary collapse

DEFAULT_VOLUME_SKIP =

Default number of volumes to strip from beginning of functional scans.

3

Instance Method Summary collapse

Instance Method Details

#build_physionoise_run_spec(rpipe_scan_spec) ⇒ Object

Build a Run Spec from a Scan Spec This should be moved to the generators and shouldn’t be used here.



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/default_methods/recon/physionoise_helper.rb', line 19

def build_physionoise_run_spec(rpipe_scan_spec)
  run = rpipe_scan_spec['physio_files'].dup
  flash "Physionoise Regressors: #{run[:phys_directory]}"
  run[:bold_reps] = rpipe_scan_spec['bold_reps']
  run[:rep_time] = rpipe_scan_spec['rep_time']
  unless Pathname.new(run[:phys_directory]).absolute?
    run[:phys_directory] = File.join(@rawdir, run[:phys_directory])
  end
  run[:run_directory] = @rawdir
  runs = [run]
end

#build_retroicor_cmd(physio_files, file) ⇒ Object

:respiration_trigger: RESPTrig_epiRT_0303201014_46_27_463

:phys_directory: cardiac/
:cardiac_signal: PPGData_epiRT_0303201014_46_27_463
:cardiac_trigger: PPGTrig_epiRT_0303201014_46_27_463


52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/default_methods/recon/physionoise_helper.rb', line 52

def build_retroicor_cmd(physio_files, file)
  [:cardiac_signal, :respiration_signal].collect {|req| raise ScriptError, "Missing physio config: #{req}" unless physio_files.include?(req)}
  
  prefix = 'p'
  unless Pathname.new(physio_files[:cardiac_signal]).absolute?
    cardiac_signal = File.join(@rawdir, physio_files[:phys_directory], physio_files[:cardiac_signal])
  end
  
  unless Pathname.new(physio_files[:respiration_signal]).absolute?
    respiration_signal = File.join(@rawdir, physio_files[:phys_directory], physio_files[:respiration_signal])
  end
  
  outfile = prefix + file

  icor_format = "3dretroicor -prefix %s -card %s -resp %s %s"
  icor_options = [outfile, cardiac_signal, respiration_signal, file]
  icor_cmd = icor_format % icor_options
  return icor_cmd, outfile
end

#create_physiosnoise_regressors(scan_spec) ⇒ Object

Create Physionoise Regressors for Inclusion in GLM



6
7
8
9
# File 'lib/default_methods/recon/physionoise_helper.rb', line 6

def create_physiosnoise_regressors(scan_spec)
  runs = build_physionoise_run_spec(scan_spec)
  Physionoise.run_physionoise_on(runs, ["--saveFiles"])
end

#generate_physiospecObject

Generate a Physionoise Spec



12
13
14
15
# File 'lib/default_methods/recon/physionoise_helper.rb', line 12

def generate_physiospec
 physiospec = Physiospec.new(@rawdir, File.join(@rawdir, '..', 'cardiac'))
 physiospec.epis_and_associated_phys_files
end

#recon_visitObject Also known as: perform

Reconstructs, strips, and slice timing corrects all scans specified in the recon_spec. This function assumes a destination directory is set up in the filesystem and begins writing to it with no further checking. It will overwrite data if it already exists, be careful.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/default_methods/default_recon.rb', line 12

def recon_visit
	
	setup_directory(@origdir, "RECON")
	
	Dir.chdir(@origdir) do		  
		@scans.each_with_index do |scan_spec, i|
			outfile = "%s_%s.nii" % [@subid, scan_spec['label']]
			
       # Set Discarded Acquisitions - Volumes to Skip from Recon Spec, Scan Spec or Default
			@volumes_to_skip = @volume_skip || scan_spec['volume_skip'] || scan_spec['volumes_to_skip'] || DEFAULT_VOLUME_SKIP
			
			reconstruct_scan(scan_spec, outfile)
			
			if scan_spec['type'] == "func"
         # if scan_spec['physio_files']
         #   create_physiosnoise_regressors(scan_spec)
         #   outfile = run_retroicor(scan_spec['physio_files'], outfile)
         # end
			  
				slice_time_correct(outfile, scan_spec['alt_direction'] ||= 'alt+z')
			else
				File.copy('tmp.nii', outfile)
			end
			
			File.delete('tmp.nii') if File.exist? 'tmp.nii'
		end
	end
end

#reconstruct_scan(scan_spec, outfile) ⇒ Object

Reconstructs a scan from dicoms or pfile to nifti, anatomical or functional. Uses a scan_spec hash to drive. Writes the result in current working directory. Raises an error if to3d system call fails. Conventions: I****.dcm filenaming, I0002.dcm is second file in series,



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/default_methods/default_recon.rb', line 47

def reconstruct_scan(scan_spec, outfile)	
	if scan_spec['dir']
	  sequence = DicomRawSequence.new(scan_spec, @rawdir, @volumes_to_skip)
  elsif scan_spec['pfile']
    sequence = PfileRawSequence.new(scan_spec, @rawdir, @volumes_to_skip)
   else
     raise ConfigError, "Scan must list either a pfile or a dicom directory."
   end
   
   sequence.prepare(outfile)
end

#run_retroicor(physio_files, file) ⇒ Object

Runs 3dRetroicor for a scan. Returns the output filename if successful or raises an error if there was an error.



33
34
35
36
37
38
39
40
41
# File 'lib/default_methods/recon/physionoise_helper.rb', line 33

def run_retroicor(physio_files, file)
 icor_cmd, outfile = build_retroicor_cmd(physio_files, file)
 flash "3dRetroicor: #{file} \n #{icor_cmd}"
 if run(icor_cmd)
   return outfile
  else
    raise ScriptError, "Problem running #{icor_cmd}"
  end
end

#slice_time_correct(infile, alt_direction = "alt+z") ⇒ Object

Uses to3d to slice time correct a 4D functional nifti file. Writes result in the current working directory.



60
61
62
63
64
65
66
# File 'lib/default_methods/default_recon.rb', line 60

def slice_time_correct(infile, alt_direction = "alt+z")
	$Log.info "Slice Timing Correction: #{infile}"
	cmd = "3dTshift -tzero 0 -tpattern #{alt_direction} -prefix a#{infile} #{infile}"
	unless run(cmd)
	  raise ScriptError, "Failed to slice time correct: #{cmd}"
  end
end