Class: HQMF::DataCriteria

Inherits:
Object
  • Object
show all
Includes:
Conversion::Utilities
Defined in:
lib/hqmf-model/data_criteria.rb

Overview

Represents a data criteria specification

Direct Known Subclasses

Converter::SimpleDataCriteria

Constant Summary collapse

SOURCE_DATA_CRITERIA_TEMPLATE_ID =
'2.16.840.1.113883.3.100.1.1'
SOURCE_DATA_CRITERIA_TEMPLATE_TITLE =
'Source data criteria'
XPRODUCT =
'XPRODUCT'
UNION =
'UNION'
INTERSECT =
'INTERSECT'
SATISFIES_ALL =
'satisfies_all'
SATISFIES_ANY =
'satisfies_any'
VARIABLE =
'variable'
FIELDS =
{'SEVERITY' => {title:'Severity', coded_entry_method: :severity, code: 'SEV', code_system:'2.16.840.1.113883.5.4', template_id: '2.16.840.1.113883.3.560.1.1021.2', field_type: :value},
'ORDINAL' => {title:'Ordinal', coded_entry_method: :ordinality, code: '117363000', code_system:'2.16.840.1.113883.6.96', template_id: '2.16.840.1.113883.3.560.1.1012.2', field_type: :value},
'REASON' => {title:'Reason', coded_entry_method: :reason, code: '410666004', code_system:'2.16.840.1.113883.6.96', template_id: '2.16.840.1.113883.3.560.1.1017.2', field_type: :value},
'SOURCE' => {title:'Source', coded_entry_method: :source, code: '260753009', code_system:'2.16.840.1.113883.6.96', template_id: '2.16.840.1.113883.3.560.1.2001.2', field_type: :value},
'CUMULATIVE_MEDICATION_DURATION' => {title:'Cumulative Medication Duration', coded_entry_method: :cumulative_medication_duration, code: '363819003', code_system:'2.16.840.1.113883.6.96', template_id: '2.16.840.1.113883.3.560.1.1001.3', field_type: :value},
'FLFS' => {title:'Fulfills', coded_entry_method: :fulfills, code: 'FLFS', field_type: :reference},
'FACILITY_LOCATION' => {title:'Facility Location', coded_entry_method: :facility, code: 'SDLOC', field_type: :value},
'FACILITY_LOCATION_ARRIVAL_DATETIME' => {title:'Facility Location Arrival Date/Time', coded_entry_method: :facility_arrival, code: 'SDLOC_ARRIVAL', field_type: :nested_timestamp},
'FACILITY_LOCATION_DEPARTURE_DATETIME' => {title:'Facility Location Departure Date/Time', coded_entry_method: :facility_departure, code: 'SDLOC_DEPARTURE', field_type: :nested_timestamp},
'DISCHARGE_DATETIME' => {title:'Discharge Date/Time', coded_entry_method: :discharge_time, code: '442864001', code_system:'2.16.840.1.113883.6.96', template_id: '2.16.840.1.113883.3.560.1.1025.1', field_type: :timestamp},
'DISCHARGE_STATUS' => {title:'Discharge Status', coded_entry_method: :discharge_disposition, code: '309039003', code_system:'2.16.840.1.113883.6.96', template_id: '2.16.840.1.113883.3.560.1.1003.2', field_type: :value},
'ADMISSION_DATETIME' => {title:'Admission Date/Time', coded_entry_method: :admit_time, code: '399423000', code_system:'2.16.840.1.113883.6.96', field_type: :timestamp},
'LENGTH_OF_STAY' => {title:'Length of Stay', coded_entry_method: :length_of_stay, code: '183797002', code_system:'2.16.840.1.113883.6.96', template_id: '2.16.840.1.113883.3.560.1.1029.3', field_type: :value},
'DOSE' => {title:'Dose', coded_entry_method: :dose, code: '398232005', code_system:'2.16.840.1.113883.6.96', template_id: '2.16.840.1.113883.3.560.1.1004.1', field_type: :value},
'ROUTE' => {title:'Route', coded_entry_method: :route, code: '263513008', code_system:'2.16.840.1.113883.6.96', template_id: '2.16.840.1.113883.3.560.1.1020.2', field_type: :value},
'START_DATETIME' => {title:'Start Date/Time', coded_entry_method: :start_date, code: '398201009', code_system:'2.16.840.1.113883.6.96', template_id: '2.16.840.1.113883.3.560.1.1027.1', field_type: :timestamp},
'FREQUENCY' => {title:'Frequency', coded_entry_method: :frequency, code: '260864003', code_system:'2.16.840.1.113883.6.96', template_id: '2.16.840.1.113883.3.560.1.1006.1', field_type: :value},
'ANATOMICAL_STRUCTURE' => {title:'Anatomical Structure', coded_entry_method: :anatomical_structure, code: '91723000', code_system:'2.16.840.1.113883.6.96', template_id: '2.16.840.1.113883.3.560.1.1000.2', field_type: :value},
'STOP_DATETIME' => {title:'Stop Date/Time', coded_entry_method: :end_date, code: '397898000', code_system:'2.16.840.1.113883.6.96', template_id: '2.16.840.1.113883.3.560.1.1026.1', field_type: :timestamp},
'INCISION_DATETIME' => {title:'Incision Date/Time', coded_entry_method: :incision_time, code: '34896006', code_system:'2.16.840.1.113883.6.96', template_id: '2.16.840.1.113883.3.560.1.1007.1', field_type: :timestamp},
'REMOVAL_DATETIME' => {title:'Removal Date/Time', coded_entry_method: :removal_time, code: '118292001', code_system:'2.16.840.1.113883.6.96', template_id: '2.16.840.1.113883.3.560.1.1032.1', field_type: :timestamp},
'TRANSFER_TO' => {title:'Transfer To', coded_entry_method: :transfer_to, code: 'TRANSFER_TO', template_id: '2.16.840.1.113883.3.560.1.72', field_type: :value},
'TRANSFER_FROM' => {title:'Transfer From', coded_entry_method: :transfer_from, code: 'TRANSFER_FROM', template_id: '2.16.840.1.113883.3.560.1.71', field_type: :value},
}
VALUE_FIELDS =
{'SEV'      => 'SEVERITY',
 '117363000' => 'ORDINAL',
 '410666004' => 'REASON',
 '260753009' => 'SOURCE',
 '363819003' => 'CUMULATIVE_MEDICATION_DURATION',
 'SDLOC'     => 'FACILITY_LOCATION',
 '442864001' => 'DISCHARGE_DATETIME',
 '309039003' => 'DISCHARGE_STATUS',
 '399423000' => 'ADMISSION_DATETIME',
 '183797002' => 'LENGTH_OF_STAY',
 '398232005' => 'DOSE',
 '263513008' => 'ROUTE',
 '398201009' => 'START_DATETIME',
 '260864003' =>'FREQUENCY',
 '91723000'  => 'ANATOMICAL_STRUCTURE',
 '397898000' => 'STOP_DATETIME',
 '34896006'  => 'INCISION_DATETIME',
 '118292001' =>'REMOVAL_DATETIME',
 'SDLOC_ARRIVAL'   => 'FACILITY_LOCATION_ARRIVAL_DATETIME',
 'SDLOC_DEPARTURE' => 'FACILITY_LOCATION_DEPARTURE_DATETIME'
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Conversion::Utilities

#build_hash, #check_equality, #json_array, #openstruct_to_json

Constructor Details

#initialize(id, title, display_name, description, code_list_id, children_criteria, derivation_operator, definition, status, value, field_values, effective_time, inline_code_list, negation, negation_code_list_id, temporal_references, subset_operators, specific_occurrence, specific_occurrence_const, source_data_criteria = nil, comments = nil, variable = false) ⇒ DataCriteria

Create a new data criteria instance

Parameters:



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/hqmf-model/data_criteria.rb', line 93

def initialize(id, title, display_name, description, code_list_id, children_criteria, derivation_operator, definition, status, value, field_values, effective_time, inline_code_list, negation, negation_code_list_id, temporal_references, subset_operators, specific_occurrence, specific_occurrence_const, source_data_criteria=nil, comments=nil, variable=false)

  status = normalize_status(definition, status)
  @settings = HQMF::DataCriteria.get_settings_for_definition(definition, status)

  @id = id
  @title = title
  @description = description
  @code_list_id = code_list_id
  @negation_code_list_id = negation_code_list_id
  @children_criteria = children_criteria
  @derivation_operator = derivation_operator
  @definition = definition
  @status = status
  @value = value
  @field_values = field_values
  @effective_time = effective_time
  @inline_code_list = inline_code_list
  @negation = negation
  @negation_code_list_id = negation_code_list_id
  @temporal_references = temporal_references
  @subset_operators = subset_operators
  @specific_occurrence = specific_occurrence
  @specific_occurrence_const = specific_occurrence_const
  @source_data_criteria = source_data_criteria || id
  @comments = comments
  @variable = variable
end

Instance Attribute Details

#children_criteriaObject

Returns the value of attribute children_criteria.



67
68
69
# File 'lib/hqmf-model/data_criteria.rb', line 67

def children_criteria
  @children_criteria
end

#code_list_idObject (readonly)

Returns the value of attribute code_list_id.



66
67
68
# File 'lib/hqmf-model/data_criteria.rb', line 66

def code_list_id
  @code_list_id
end

#commentsObject

Returns the value of attribute comments.



67
68
69
# File 'lib/hqmf-model/data_criteria.rb', line 67

def comments
  @comments
end

#definitionObject

Returns the value of attribute definition.



67
68
69
# File 'lib/hqmf-model/data_criteria.rb', line 67

def definition
  @definition
end

#derivation_operatorObject (readonly)

Returns the value of attribute derivation_operator.



66
67
68
# File 'lib/hqmf-model/data_criteria.rb', line 66

def derivation_operator
  @derivation_operator
end

#descriptionObject (readonly)

Returns the value of attribute description.



66
67
68
# File 'lib/hqmf-model/data_criteria.rb', line 66

def description
  @description
end

#display_nameObject

Returns the value of attribute display_name.



67
68
69
# File 'lib/hqmf-model/data_criteria.rb', line 67

def display_name
  @display_name
end

#effective_timeObject

Returns the value of attribute effective_time.



67
68
69
# File 'lib/hqmf-model/data_criteria.rb', line 67

def effective_time
  @effective_time
end

#field_valuesObject

Returns the value of attribute field_values.



67
68
69
# File 'lib/hqmf-model/data_criteria.rb', line 67

def field_values
  @field_values
end

#idObject

Returns the value of attribute id.



67
68
69
# File 'lib/hqmf-model/data_criteria.rb', line 67

def id
  @id
end

#inline_code_listObject

Returns the value of attribute inline_code_list.



67
68
69
# File 'lib/hqmf-model/data_criteria.rb', line 67

def inline_code_list
  @inline_code_list
end

#negationObject

Returns the value of attribute negation.



67
68
69
# File 'lib/hqmf-model/data_criteria.rb', line 67

def negation
  @negation
end

#negation_code_list_idObject

Returns the value of attribute negation_code_list_id.



67
68
69
# File 'lib/hqmf-model/data_criteria.rb', line 67

def negation_code_list_id
  @negation_code_list_id
end

#source_data_criteriaObject (readonly)

Returns the value of attribute source_data_criteria.



66
67
68
# File 'lib/hqmf-model/data_criteria.rb', line 66

def source_data_criteria
  @source_data_criteria
end

#specific_occurrenceObject (readonly)

Returns the value of attribute specific_occurrence.



66
67
68
# File 'lib/hqmf-model/data_criteria.rb', line 66

def specific_occurrence
  @specific_occurrence
end

#specific_occurrence_constObject (readonly)

Returns the value of attribute specific_occurrence_const.



66
67
68
# File 'lib/hqmf-model/data_criteria.rb', line 66

def specific_occurrence_const
  @specific_occurrence_const
end

#statusObject

Returns the value of attribute status.



67
68
69
# File 'lib/hqmf-model/data_criteria.rb', line 67

def status
  @status
end

#subset_operatorsObject

Returns the value of attribute subset_operators.



67
68
69
# File 'lib/hqmf-model/data_criteria.rb', line 67

def subset_operators
  @subset_operators
end

#temporal_referencesObject

Returns the value of attribute temporal_references.



67
68
69
# File 'lib/hqmf-model/data_criteria.rb', line 67

def temporal_references
  @temporal_references
end

#titleObject (readonly)

Returns the value of attribute title.



66
67
68
# File 'lib/hqmf-model/data_criteria.rb', line 66

def title
  @title
end

#valueObject

Returns the value of attribute value.



67
68
69
# File 'lib/hqmf-model/data_criteria.rb', line 67

def value
  @value
end

#variableObject (readonly)

Returns the value of attribute variable.



66
67
68
# File 'lib/hqmf-model/data_criteria.rb', line 66

def variable
  @variable
end

Class Method Details

.create_from_category(id, title, description, code_list_id, category, sub_category = nil, negation = false, negation_code_list_id = nil) ⇒ Object

create a new data criteria given a category and sub_category. A sub category can either be a status or a sub category



123
124
125
126
# File 'lib/hqmf-model/data_criteria.rb', line 123

def self.create_from_category(id, title, description, code_list_id, category, sub_category=nil, negation=false, negation_code_list_id=nil)
  settings = HQMF::DataCriteria.get_settings_for_definition(category, sub_category)
  HQMF::DataCriteria.new(id, title, nil, description, code_list_id, nil, nil, settings['definition'], settings['status'], nil, nil, nil, nil, negation, negation_code_list_id, nil, nil, nil,nil)
end

.definition_for_template_id(template_id) ⇒ Object



298
299
300
# File 'lib/hqmf-model/data_criteria.rb', line 298

def self.definition_for_template_id(template_id)
  get_template_id_map()[template_id]
end

.from_json(id, json) ⇒ Object

Create a new data criteria instance from a JSON hash keyed with symbols



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/hqmf-model/data_criteria.rb', line 156

def self.from_json(id, json)

  title = json["title"] if json["title"]
  display_name = json["display_name"] if json["display_name"]
  description = json["description"] if json["description"]
  code_list_id = json["code_list_id"] if json["code_list_id"]
  children_criteria = json["children_criteria"] if json["children_criteria"]
  derivation_operator = json["derivation_operator"] if json["derivation_operator"]
  definition = json["definition"] if json["definition"]
  status = json["status"] if json["status"]
  value = convert_value(json["value"]) if json["value"]
  field_values = json["field_values"].inject({}){|memo,(k,v)| memo[k.to_s] = convert_value(v); memo} if json["field_values"]
  effective_time = HQMF::Range.from_json(json["effective_time"]) if json["effective_time"]
  inline_code_list = json["inline_code_list"].inject({}){|memo,(k,v)| memo[k.to_s] = v; memo} if json["inline_code_list"]
  negation = json["negation"] || false
  negation_code_list_id = json['negation_code_list_id'] if json['negation_code_list_id']
  temporal_references = json["temporal_references"].map {|reference| HQMF::TemporalReference.from_json(reference)} if json["temporal_references"]
  subset_operators = json["subset_operators"].map {|operator| HQMF::SubsetOperator.from_json(operator)} if json["subset_operators"]
  specific_occurrence = json['specific_occurrence'] if json['specific_occurrence']
  specific_occurrence_const = json['specific_occurrence_const'] if json['specific_occurrence_const']
  source_data_criteria = json['source_data_criteria'] if json['source_data_criteria']
  comments = json['comments'] if json['comments']
  variable = json['variable'] || false

  HQMF::DataCriteria.new(id, title, display_name, description, code_list_id, children_criteria, derivation_operator, definition, status, value, field_values,
                         effective_time, inline_code_list, negation, negation_code_list_id, temporal_references, subset_operators,specific_occurrence,specific_occurrence_const,source_data_criteria, comments, variable)
end

.get_settings_for_definition(definition, status) ⇒ Object



287
288
289
290
291
292
293
294
295
296
# File 'lib/hqmf-model/data_criteria.rb', line 287

def self.get_settings_for_definition(definition, status)
  settings_file = File.expand_path('../data_criteria.json', __FILE__)
  settings_map = get_settings_map
  key = definition + ((status.nil? || status.empty?) ? '' : "_#{status}")
  settings = settings_map[key]
  
  raise "data criteria is not supported #{key}" if settings.nil? || settings["not_supported"]

  settings
end

.get_settings_mapObject



281
282
283
284
285
# File 'lib/hqmf-model/data_criteria.rb', line 281

def self.get_settings_map
  return @settings_map if @settings_map
  settings_file = File.expand_path('../data_criteria.json', __FILE__)
  @settings_map = JSON.parse(File.read(settings_file))
end

.get_template_id_mapObject



320
321
322
323
# File 'lib/hqmf-model/data_criteria.rb', line 320

def self.get_template_id_map
  @@template_id_map ||= read_template_id_map
  @@template_id_map
end

.statuses_by_definitionObject



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/hqmf-model/data_criteria.rb', line 215

def self.statuses_by_definition
  settings_file = File.expand_path('../data_criteria.json', __FILE__)
  settings_map = JSON.parse(File.read(settings_file))
  all_defs = (settings_map.map {|key, value| {category: value['category'],definition:value['definition'],status:(value['status'].empty? ? nil : value['status']), sub_category: value['sub_category'],title:value['title']} unless value['not_supported']}).compact
  by_categories = {}
  all_defs.each do |definition| 
    by_categories[definition[:category]]||={}
    status = definition[:status]
    def_key = definition[:definition]
    if status.nil? and definition[:sub_category] and !definition[:sub_category].empty?
      status = definition[:sub_category]
      def_key = def_key.gsub("_#{status}",'')
    end
    by_categories[definition[:category]][def_key]||={category:def_key,statuses:[]}
    by_categories[definition[:category]][def_key][:statuses] << status unless status.nil?
  end
  status_by_category = {}
  by_categories.each {|key, value| status_by_category[key] = value.values}
  status_by_category.delete('derived')
  status_by_category.delete('variable')
  status_by_category.delete('measurement_period')
  status_by_category.values.flatten
end

.template_id_for_definition(definition, status, negation) ⇒ Object



302
303
304
# File 'lib/hqmf-model/data_criteria.rb', line 302

def self.template_id_for_definition(definition, status, negation)
  get_template_id_map().key({'definition' => definition, 'status' => status || '', 'negation' => negation})
end

.title_for_template_id(template_id) ⇒ Object



306
307
308
309
310
311
312
313
314
315
316
317
318
# File 'lib/hqmf-model/data_criteria.rb', line 306

def self.title_for_template_id(template_id)
  value = get_template_id_map()[template_id]
  if value
    settings = self.get_settings_for_definition(value['definition'], value['status'])
    if settings
      settings['title']
    else
      'Unknown data criteria'
    end
  else
    'Unknown template id'
  end
end

Instance Method Details

#all_code_set_oidsObject



261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/hqmf-model/data_criteria.rb', line 261

def all_code_set_oids
  
  # root oid
  referenced_oids = [code_list_id]
  
  # value oid
  referenced_oids << value.code_list_id if value != nil and value.type == 'CD'
  
  # negation oid
  referenced_oids << negation_code_list_id if negation_code_list_id != nil
  
  # field oids
  if field_values != nil
    referenced_oids.concat (field_values.map {|key,field| field.code_list_id if field != nil and field.type == 'CD'})
  end
  
  referenced_oids
  
end

#base_jsonObject



194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/hqmf-model/data_criteria.rb', line 194

def base_json
  x = nil
  json = build_hash(self, [:title,:display_name,:description,:code_list_id,:children_criteria, :derivation_operator, :property, :type, :definition, :status, :hard_status, :negation, :negation_code_list_id,:specific_occurrence,:specific_occurrence_const,:source_data_criteria,:variable])
  json[:children_criteria] = @children_criteria unless @children_criteria.nil? || @children_criteria.empty?
  json[:value] = ((@value.is_a? String) ? @value : @value.to_json) if @value
  json[:field_values] = @field_values.inject({}) {|memo,(k,v)| memo[k] = (!v.nil? ? v.to_json : nil); memo} if @field_values
  json[:effective_time] = @effective_time.to_json if @effective_time
  json[:inline_code_list] = @inline_code_list if @inline_code_list
  json[:temporal_references] = x if x = json_array(@temporal_references)
  json[:subset_operators] = x if x = json_array(@subset_operators)
  json[:comments] = @comments if @comments
  json
end

#hard_statusObject



136
137
138
# File 'lib/hqmf-model/data_criteria.rb', line 136

def hard_status
  @settings['hard_status']
end

#has_subset(subset_operator) ⇒ Object



211
212
213
# File 'lib/hqmf-model/data_criteria.rb', line 211

def has_subset(subset_operator)
  @subset_operators.reduce(false) {|found, item| found ||= item == subset_operator }
end

#has_temporal(temporal_reference) ⇒ Object



208
209
210
# File 'lib/hqmf-model/data_criteria.rb', line 208

def has_temporal(temporal_reference)
  @temporal_references.reduce(false) {|found, item| found ||= item == temporal_reference }
end

#is_same_type?(criteria) ⇒ Boolean

Returns:

  • (Boolean)


184
185
186
187
# File 'lib/hqmf-model/data_criteria.rb', line 184

def is_same_type?(criteria)
   return @definition == criteria.definition && @hard_status == criteria.hard_status && 
          @negation == criteria.negation && all_code_set_oids.sort == criteria.all_code_set_oids.sort
end

#patient_api_functionObject



133
134
135
# File 'lib/hqmf-model/data_criteria.rb', line 133

def patient_api_function
  @settings['patient_api_function'].to_sym unless @settings['patient_api_function'].empty?
end

#propertyObject



130
131
132
# File 'lib/hqmf-model/data_criteria.rb', line 130

def property
  @settings['property'].to_sym unless @settings['property'].nil?
end

#referenced_data_criteria(document) ⇒ Object



239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/hqmf-model/data_criteria.rb', line 239

def referenced_data_criteria(document)
  referenced = []
  if (@children_criteria)
    @children_criteria.each do |id|
      dc = document.data_criteria(id) 
      referenced << id
      referenced.concat(dc.referenced_data_criteria(document))
    end
  end
  if (@temporal_references)
    @temporal_references.each do |tr|
      id = tr.reference.id
      if (id != HQMF::Document::MEASURE_PERIOD_ID)
        dc = document.data_criteria(id) 
        referenced << id
        referenced.concat(dc.referenced_data_criteria(document))
      end
    end
  end
  referenced
end

#to_jsonObject



189
190
191
192
# File 'lib/hqmf-model/data_criteria.rb', line 189

def to_json
  json = base_json
  {self.id.to_s.to_sym => json}
end

#typeObject



127
128
129
# File 'lib/hqmf-model/data_criteria.rb', line 127

def type
  @settings['category'].to_sym
end

#update_copy(hard_status, title, description, derivation_operator, definition) ⇒ Object



139
140
141
142
143
144
145
# File 'lib/hqmf-model/data_criteria.rb', line 139

def update_copy(hard_status, title, description, derivation_operator, definition)
  @settings['hard_status'] = hard_status
  @title = title
  @description = description
  @derivation_operator = derivation_operator
  @definition = definition
end