Class: VisitRawDataDirectory

Inherits:
Object
  • Object
show all
Defined in:
lib/visit_raw_data_directory.rb

Overview

Encapsulates a directory of data acquired during one participant visit. These are the raw data directories that are transfered directly from the scanners and archived in the raw data section of the vtrak filesystem. After initializing, the visit can be scanned to extract metadata for all of the images acquired during the visit. The scanning is done in a fairly naive manner: the visit directory is recursively walked and in each subdirectory any and all pfiles will be imported in addition to one single dicom if any exist. Thus, only a single dicom file among many in a scan session is used to retrieve information. checking the individual files for data integrity must be handled elsewhere if at all.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(directory, scan_procedure_name = nil) ⇒ VisitRawDataDirectory

A new Visit instance needs to know the path to its raw data and scan_procedure name. The scan_procedure name must match a name in the database, if not a new scan_procedure entry will be inserted.

Raises:

  • (IOError)


50
51
52
53
54
55
56
57
58
59
# File 'lib/visit_raw_data_directory.rb', line 50

def initialize(directory, scan_procedure_name=nil)
  raise(IOError, "Visit directory not found: #{directory}") unless File.exist?(File.expand_path(directory))
  @visit_directory = File.expand_path(directory)
  @working_directory = Dir.tmpdir
  @datasets = Array.new
  @timestamp = nil
  @rmr_number = nil
  @scan_procedure_name = scan_procedure_name.nil? ? get_scan_procedure_based_on_raw_directory : scan_procedure_name
  @db = nil
end

Instance Attribute Details

#datasetsObject (readonly)

An array of :RawImageDataset objects acquired during this visit.



39
40
41
# File 'lib/visit_raw_data_directory.rb', line 39

def datasets
  @datasets
end

#dbObject

Returns the value of attribute db.



46
47
48
# File 'lib/visit_raw_data_directory.rb', line 46

def db
  @db
end

#rmr_numberObject (readonly)

RMR number for this visit.



43
44
45
# File 'lib/visit_raw_data_directory.rb', line 43

def rmr_number
  @rmr_number
end

#scan_procedure_nameObject (readonly)

scan_procedure name



45
46
47
# File 'lib/visit_raw_data_directory.rb', line 45

def scan_procedure_name
  @scan_procedure_name
end

#timestampObject (readonly)

Timestamp for this visit, obtained from the first :RawImageDataset



41
42
43
# File 'lib/visit_raw_data_directory.rb', line 41

def timestamp
  @timestamp
end

#visit_directoryObject (readonly)

The absolute path of the visit directory, as a string.



37
38
39
# File 'lib/visit_raw_data_directory.rb', line 37

def visit_directory
  @visit_directory
end

Instance Method Details

#db_insert!(db_file) ⇒ Object

Inserts each dataset in this visit into the specified database. The specifics of the database insert are handled by the #RawImageDataset class.



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/visit_raw_data_directory.rb', line 79

def db_insert!(db_file)
  @db = SQLite3::Database.new(db_file)
  @db.results_as_hash = true
  @db.type_translation = true
  
  begin
    # checks scan_procedure in db, inserts if neccessary, returns id
    scan_procedure_id = fetch_or_insert_scan_procedure
    
    # insert or update visit as needed
    if visit_is_new? # this is a new visit
      visit_id = insert_new_visit(scan_procedure_id)    
    else # visit already exists in DB
      visit_id = get_existing_visit_id
      update_existing_visit(visit_id, scan_procedure_id)
    end
  
    # insert each dataset from the visit, also insert an entry in series descriptions table if necessary.
    @datasets.each do |dataset|
      update_series_descriptions_table(dataset.series_description)
      if dataset_is_new?(dataset)
        insert_new_dataset(dataset, visit_id)
      else # dataset is already in DB
        dataset_id = get_existing_dataset_id(dataset)
        update_existing_dataset(dataset, dataset_id)
      end
    end
  rescue Exception => e
    puts e.message
  ensure
    @db.close
    @db = nil
  end
end

#scanObject

Recursively walks the filesystem inside the visit directory. At each subdirectory, any and all pfiles are scanned and imported in addition to one and only one dicom file. After scanning exception if no valid rmr is found in the datasets, be prepared to catch it.



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/visit_raw_data_directory.rb', line 65

def scan
  flash "Scanning visit raw data directory #{@visit_directory}"
  d = Pathname.new(@visit_directory)
  d.each_subdirectory do |dd|
    dd.each_pfile { |pf| @datasets << import_dataset(pf, dd) }
    dd.first_dicom { |fd| @datasets << import_dataset(fd, dd) }
  end
  @timestamp = get_visit_timestamp
  @rmr_number = get_rmr_number
  flash "Completed scanning #{@visit_directory}"
end