-
Notifications
You must be signed in to change notification settings - Fork 0
/
populate_dicom.rb
51 lines (46 loc) · 1.32 KB
/
populate_dicom.rb
1
2
3
4
5
6
7
8
9
10
11
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
40
41
42
43
44
45
46
47
48
49
50
51
# Libraries required:
require 'find'
require 'active_record'
require 'dicom'
# Load your yml config from rails:
db_config = YAML::load(File.open("./config/database.yml"))
# Connect to the proper database:
ActiveRecord::Base.establish_connection(db_config['development'])
# Load the custom model we created earlier:
require './app/models/examination'
# Discover all the files contained in the specified directory and all its sub-directories:
dirs = ["./dicom"]
files = Array.new
for dir in dirs
Find.find(dir) do |path|
if FileTest.directory?(path)
next
else
files << path # Store the file in our array
end
end
end
# Use a loop to run through all the files, reading its data and transferring it to the database.
files.each do |file|
# Read the file:
dcm = DICOM::DObject.read(file)
# If the file was read successfully as a DICOM file, go ahead and extract content:
if dcm.read?
dcm.print
study = dcm.value("0008,1030")
name = dcm.value("0010,0010")
voltage = dcm.value("0018,0060")
current = dcm.value("0018,1151")
exposure = dcm.value("0018,1152")
summary = dcm.print
# Store the data in the database:
e = Examination.new
e.study = study
e.name = name
e.voltage = voltage
e.current = current
e.exposure = exposure
e.summary = summary
e.save
end
end