Class: DICOM::AuditTrail

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

Overview

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.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAuditTrail

Creates a new AuditTrail instance.



30
31
32
33
34
35
# File 'lib/dicom/audit_trail.rb', line 30

def initialize
  # The AuditTrail requires JSON for serialization:
  require 'json'
  # Define the key/value hash used for tag records:
  @dictionary = Hash.new
end

Instance Attribute Details

#dictionaryObject (readonly)

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



14
15
16
# File 'lib/dicom/audit_trail.rb', line 14

def dictionary
  @dictionary
end

Class Method Details

.read(file_name) ⇒ AuditTrail

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

Parameters:

  • file_name (String)

    the path to a file containing a previously stored audit trail

Returns:

  • (AuditTrail)

    the created AuditTrail instance



22
23
24
25
26
# 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

Instance Method Details

#add_record(tag, original, replacement) ⇒ Object

Adds a tag record to the log.

Parameters:

  • tag (String)

    the tag string (e.q. ‘0010,0010’)

  • original (String, Integer, Float)

    the original value (e.q. ‘John Doe’)

  • replacement (String, Integer, Float)

    the replacement value (e.q. ‘Patient1’)



43
44
45
46
# File 'lib/dicom/audit_trail.rb', line 43

def add_record(tag, original, replacement)
  @dictionary[tag] = Hash.new unless @dictionary.key?(tag)
  @dictionary[tag][original] = replacement
end

#load(file_name) ⇒ Object

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

Parameters:

  • file_name (String)

    the path to a file containing a previously stored audit trail



52
53
54
# File 'lib/dicom/audit_trail.rb', line 52

def load(file_name)
  @dictionary = JSON.load(File.new(file_name, "r:UTF-8"))
end

#original(tag, replacement) ⇒ String, ...

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

Parameters:

  • tag (String)

    the tag string (e.q. ‘0010,0010’)

  • replacement (String, Integer, Float)

    the replacement value (e.q. ‘Patient1’)

Returns:

  • (String, Integer, Float)

    the original value of the given tag



62
63
64
65
66
67
68
# File 'lib/dicom/audit_trail.rb', line 62

def original(tag, replacement)
  original = nil
  if @dictionary.key?(tag)
    original = @dictionary[tag].key(replacement)
  end
  return original
end

#records(tag) ⇒ Hash

Gives the key/value pairs for a specific tag.

Parameters:

  • tag (String)

    the tag string (e.q. ‘0010,0010’)

Returns:

  • (Hash)

    the key/value pairs of a specific tag



75
76
77
78
79
80
81
# File 'lib/dicom/audit_trail.rb', line 75

def records(tag)
  if @dictionary.key?(tag)
    return @dictionary[tag]
  else
    return Hash.new
  end
end

#replacement(tag, original) ⇒ String, ...

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

Parameters:

  • tag (String)

    the tag string (e.q. ‘0010,0010’)

  • original (String, Integer, Float)

    the original value (e.q. ‘John Doe’)

Returns:

  • (String, Integer, Float)

    the replacement value of the given tag



89
90
91
92
93
# File 'lib/dicom/audit_trail.rb', line 89

def replacement(tag, original)
  replacement = nil
  replacement = @dictionary[tag][original] if @dictionary.key?(tag)
  return replacement
end

#write(file_name) ⇒ Object

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

Parameters:

  • file_name (String)

    the path to be used for storing key/value pairs on disk



99
100
101
102
103
104
105
106
107
108
109
# File 'lib/dicom/audit_trail.rb', line 99

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