Class: RTKIT::Patient

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

Overview

Contains the DICOM data and methods related to a patient.

Relations

  • A Patient has many Study instances.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, id, dataset, options = {}) ⇒ Patient

Creates a new Patient instance. The Patient’s ID string is used to uniquely identify a patient.

Parameters

  • name – String. The Name of the Patient.

  • id – String. The ID of the Patient.

  • dataset – The DataSet instance which the Patient is associated with.

  • options – A hash of parameters.

Options

  • :birth_date – A Date String of the Patient’s birth date.

  • :sex – A code string indicating the Patient’s sex.

Raises:

  • (ArgumentError)


60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/rtkit/patient.rb', line 60

def initialize(name, id, dataset, options={})
  raise ArgumentError, "Invalid argument 'name'. Expected String, got #{name.class}." unless name.is_a?(String)
  raise ArgumentError, "Invalid argument 'id'. Expected String, got #{id.class}." unless id.is_a?(String)
  raise ArgumentError, "Invalid argument 'dataset'. Expected DataSet, got #{dataset.class}." unless dataset.is_a?(DataSet)
  raise ArgumentError, "Invalid option ':birth_date'. Expected String, got #{options[:birth_date].class}." if options[:birth_date] && !options[:birth_date].is_a?(String)
  raise ArgumentError, "Invalid option ':sex'. Expected String, got #{options[:sex].class}." if options[:sex] && !options[:sex].is_a?(String)
  # Key attributes:
  @name = name
  @id = id
  @dataset = dataset
  # Default attributes:
  @frames = Array.new
  @studies = Array.new
  @associated_frames = Hash.new
  @associated_studies = Hash.new
  # Optional attributes:
  @birth_date = options[:birth_date]
  @sex = options[:sex]
  # Register ourselves with the dataset:
  @dataset.add_patient(self)
end

Instance Attribute Details

#birth_dateObject

The Patient’s birth date.



12
13
14
# File 'lib/rtkit/patient.rb', line 12

def birth_date
  @birth_date
end

#datasetObject (readonly)

The DataSet which this Patient belongs to.



14
15
16
# File 'lib/rtkit/patient.rb', line 14

def dataset
  @dataset
end

#framesObject (readonly)

An array of Frame (of Reference) instances belonging to this Patient.



16
17
18
# File 'lib/rtkit/patient.rb', line 16

def frames
  @frames
end

#idObject

The Patient’s ID (string).



18
19
20
# File 'lib/rtkit/patient.rb', line 18

def id
  @id
end

#nameObject

The Patient’s name.



20
21
22
# File 'lib/rtkit/patient.rb', line 20

def name
  @name
end

#sexObject

The Patient’s sex.



22
23
24
# File 'lib/rtkit/patient.rb', line 22

def sex
  @sex
end

#studiesObject (readonly)

An array of Study references.



24
25
26
# File 'lib/rtkit/patient.rb', line 24

def studies
  @studies
end

Class Method Details

.load(dcm, dataset) ⇒ Object

Creates a new Patient instance by loading patient information from the specified DICOM object. The Patient’s ID string value is used to uniquely identify a patient.

Parameters

  • dcm – An instance of a DICOM object (DICOM::DObject).

  • dataset – The DataSet instance that this Patient belongs to.

Raises:

  • (ArgumentError)


34
35
36
37
38
39
40
41
42
43
44
# File 'lib/rtkit/patient.rb', line 34

def self.load(dcm, dataset)
  raise ArgumentError, "Invalid argument 'dcm'. Expected DObject, got #{dcm.class}." unless dcm.is_a?(DICOM::DObject)
  raise ArgumentError, "Invalid argument 'dataset'. Expected DataSet, got #{dataset.class}." unless dataset.is_a?(DataSet)
  name = dcm.value(PATIENTS_NAME)
  id = dcm.value(PATIENTS_ID)
  birth_date = dcm.value(BIRTH_DATE)
  sex = dcm.value(SEX)
  pat = self.new(name, id, dataset, :birth_date => birth_date, :sex => sex)
  pat.add(dcm)
  return pat
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?

Returns true if the argument is an instance with attributes equal to self.



84
85
86
87
88
# File 'lib/rtkit/patient.rb', line 84

def ==(other)
  if other.respond_to?(:to_patient)
    other.send(:state) == state
  end
end

#add(dcm) ⇒ Object

Adds a DICOM object to the patient, by adding it to an existing Study, or creating a new Study.



95
96
97
98
99
100
101
102
# File 'lib/rtkit/patient.rb', line 95

def add(dcm)
  s = study(dcm.value(STUDY_UID))
  if s
    s.add(dcm)
  else
    add_study(Study.load(dcm, self))
  end
end

#add_frame(frame) ⇒ Object

Adds a Frame to this Patient.

Raises:

  • (ArgumentError)


106
107
108
109
110
111
# File 'lib/rtkit/patient.rb', line 106

def add_frame(frame)
  raise ArgumentError, "Invalid argument 'frame'. Expected Frame, got #{frame.class}." unless frame.is_a?(Frame)
  # Do not add it again if the frame already belongs to this instance:
  @frames << frame unless @associated_frames[frame.uid]
  @associated_frames[frame.uid] = frame
end

#add_study(study) ⇒ Object

Adds a Study to this Patient.

Raises:

  • (ArgumentError)


115
116
117
118
119
120
# File 'lib/rtkit/patient.rb', line 115

def add_study(study)
  raise ArgumentError, "Invalid argument 'study'. Expected Study, got #{study.class}." unless study.is_a?(Study)
  # Do not add it again if the study already belongs to this instance:
  @studies << study unless @associated_studies[study.uid]
  @associated_studies[study.uid] = study
end

#create_frame(uid, indicator = nil) ⇒ Object

Creates (and returns) a Frame instance added to this Patient.

Parameters

  • uid – The Frame of Reference UID string.

  • indicator – The Position Reference Indicator string. Defaults to nil.

Raises:

  • (ArgumentError)


135
136
137
138
139
140
141
142
# File 'lib/rtkit/patient.rb', line 135

def create_frame(uid, indicator=nil)
  raise ArgumentError, "Invalid argument 'uid'. Expected String, got #{uid.class}." unless uid.is_a?(String)
  raise ArgumentError, "Invalid argument 'indicator'. Expected String or nil, got #{indicator.class}." unless [String, NilClass].include?(indicator.class)
  frame = Frame.new(uid, self, :indicator => indicator)
  add_frame(frame)
  @dataset.add_frame(frame)
  return frame
end

#frame(*args) ⇒ Object

Returns the Frame instance mathcing the specified Frame Instance UID (if an argument is used). If a specified UID doesn’t match, nil is returned. If no argument is passed, the first Frame instance associated with the Patient is returned.

Parameters

  • uid – String. The value of the Frame Instance UID element.

Raises:

  • (ArgumentError)


152
153
154
155
156
157
158
159
160
161
# File 'lib/rtkit/patient.rb', line 152

def frame(*args)
  raise ArgumentError, "Expected one or none arguments, got #{args.length}." unless [0, 1].include?(args.length)
  if args.length == 1
    raise ArgumentError, "Expected String (or nil), got #{args.first.class}." unless [String, NilClass].include?(args.first.class)
    return @associated_frames[args.first]
  else
    # No argument used, therefore we return the first Frame instance:
    return @frames.first
  end
end

#hashObject

Generates a Fixnum hash value for this instance.



165
166
167
# File 'lib/rtkit/patient.rb', line 165

def hash
  state.hash
end

#study(*args) ⇒ Object

Returns the Study instance mathcing the specified Study Instance UID (if an argument is used). If a specified UID doesn’t match, nil is returned. If no argument is passed, the first Study instance associated with the Patient is returned.

Parameters

  • uid – String. The value of the Study Instance UID element.

Raises:

  • (ArgumentError)


195
196
197
198
199
200
201
202
203
204
# File 'lib/rtkit/patient.rb', line 195

def study(*args)
  raise ArgumentError, "Expected one or none arguments, got #{args.length}." unless [0, 1].include?(args.length)
  if args.length == 1
    raise ArgumentError, "Expected String (or nil), got #{args.first.class}." unless [String, NilClass].include?(args.first.class)
    return @associated_studies[args.first]
  else
    # No argument used, therefore we return the first Study instance:
    return @studies.first
  end
end

#to_patientObject

Returns self.



208
209
210
# File 'lib/rtkit/patient.rb', line 208

def to_patient
  self
end

#uidObject

Returns the unique identifier string, which for this class is the Patient’s ID.



214
215
216
# File 'lib/rtkit/patient.rb', line 214

def uid
  return @id
end