class DICOM::AuditTrail

The AuditTrail class handles key/value storage for the Anonymizer. When using the advanced Anonymization options such as enumeration and UID replacement, the AuditTrail class keeps track of key/value pairs and dumps this information to a text file using the json format. This enables us to ensure a unique relationship between the anonymized values and the original values, as well as preserving this relationship for later restoration of original values.

Attributes

dictionary[R]

The hash used for storing the key/value pairs of this instace.

Public Class Methods

new() click to toggle source

Creates a new AuditTrail instance.

# File lib/dicom/audit_trail.rb, line 30
def initialize
  # Define the key/value hash used for tag records:
  @dictionary = Hash.new
end
read(file_name) click to toggle source

Creates a new AuditTrail instance by loading the information stored in the specified file.

@param [String] file_name the path to a file containing a previously stored audit trail @return [AuditTrail] the created AuditTrail instance

# File lib/dicom/audit_trail.rb, line 22
def self.read(file_name)
  audit_trail = AuditTrail.new
  audit_trail.load(file_name)
  return audit_trail
end

Public Instance Methods

add_record(tag, original, replacement) click to toggle source

Adds a tag record to the log.

@param [String] tag the tag string (e.q. '0010,0010') @param [String, Integer, Float] original the original value (e.q. 'John Doe') @param [String, Integer, Float] replacement the replacement value (e.q. 'Patient1')

# File lib/dicom/audit_trail.rb, line 41
def add_record(tag, original, replacement)
  @dictionary[tag] = Hash.new unless @dictionary.key?(tag)
  @dictionary[tag][original] = replacement
end
load(file_name) click to toggle source

Loads the key/value dictionary hash from a specified file.

@param [String] file_name the path to a file containing a previously stored audit trail

# File lib/dicom/audit_trail.rb, line 50
def load(file_name)
  @dictionary = JSON.load(File.new(file_name, "r:UTF-8"))
end
original(tag, replacement) click to toggle source

Retrieves the original value used for the given combination of tag & replacement value.

@param [String] tag the tag string (e.q. '0010,0010') @param [String, Integer, Float] replacement the replacement value (e.q. 'Patient1') @return [String, Integer, Float] the original value of the given tag

# File lib/dicom/audit_trail.rb, line 60
def original(tag, replacement)
  original = nil
  if @dictionary.key?(tag)
    original = @dictionary[tag].key(replacement)
  end
  return original
end
records(tag) click to toggle source

Gives the key/value pairs for a specific tag.

@param [String] tag the tag string (e.q. '0010,0010') @return [Hash] the key/value pairs of a specific tag

# File lib/dicom/audit_trail.rb, line 73
def records(tag)
  if @dictionary.key?(tag)
    return @dictionary[tag]
  else
    return Hash.new
  end
end
replacement(tag, original) click to toggle source

Retrieves the replacement value used for the given combination of tag & original value.

@param [String] tag the tag string (e.q. '0010,0010') @param [String, Integer, Float] original the original value (e.q. 'John Doe') @return [String, Integer, Float] the replacement value of the given tag

# File lib/dicom/audit_trail.rb, line 87
def replacement(tag, original)
  replacement = nil
  replacement = @dictionary[tag][original] if @dictionary.key?(tag)
  return replacement
end
write(file_name) click to toggle source

Dumps the key/value pairs to a json string which is written to the specified file.

@param [String] file_name the path to be used for storing key/value pairs on disk

# File lib/dicom/audit_trail.rb, line 97
def write(file_name)
  # Encode json string:
  str = JSON.pretty_generate(@dictionary)
  # Create directory if needed:
  unless File.directory?(File.dirname(file_name))
    require 'fileutils'
    FileUtils.mkdir_p(File.dirname(file_name))
  end
  # Write to file:
  File.open(file_name, 'w') {|f| f.write(str) }
end