Class: Remi::Testing::BusinessRules::DataSubject
- Inherits:
-
Object
- Object
- Remi::Testing::BusinessRules::DataSubject
- Defined in:
- lib/remi/testing/business_rules.rb
Instance Attribute Summary collapse
-
#data_subject ⇒ Object
readonly
Returns the value of attribute data_subject.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
- #add_field(field_name) ⇒ Object
- #append_data_with(example) ⇒ Object
-
#column_hash ⇒ Object
Public: Converts the data subject to a hash where the keys are the table columns and the values are an array for the value of column for each row.
- #csv_options ⇒ Object
- #cumulative_dist_from_freq_table(table, freq_field: 'frequency') ⇒ Object
- #distribute_values(table) ⇒ Object
- #example_to_df(example) ⇒ Object
- #field ⇒ Object
- #fields ⇒ Object
- #freq_by(*field_names) ⇒ Object
- #generate_values_from_cumulative_dist(n_records, cumulative_dist) ⇒ Object
- #get_attrib(name) ⇒ Object
-
#initialize(name, subject) ⇒ DataSubject
constructor
A new instance of DataSubject.
- #replicate_rows(n_rows) ⇒ Object
- #size ⇒ Object
- #stub_data ⇒ Object
- #stub_data_with(example) ⇒ Object
- #unique_integer_field(field_name) ⇒ Object
-
#where(field_name, operation) ⇒ Object
Would like to have this return a new DataSubject and not a dataframe.
- #where_between(field_name, low_value, high_value) ⇒ Object
- #where_gt(field_name, value) ⇒ Object
- #where_in(field_name, list) ⇒ Object
- #where_is(field_name, value) ⇒ Object
- #where_lt(field_name, value) ⇒ Object
Constructor Details
#initialize(name, subject) ⇒ DataSubject
Returns a new instance of DataSubject.
256 257 258 259 260 261 262 |
# File 'lib/remi/testing/business_rules.rb', line 256 def initialize(name, subject) @name = name @data_subject = subject @fields = DataFieldCollection.new stub_data end |
Instance Attribute Details
#data_subject ⇒ Object (readonly)
Returns the value of attribute data_subject.
265 266 267 |
# File 'lib/remi/testing/business_rules.rb', line 265 def data_subject @data_subject end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
264 265 266 |
# File 'lib/remi/testing/business_rules.rb', line 264 def name @name end |
Instance Method Details
#add_field(field_name) ⇒ Object
267 268 269 |
# File 'lib/remi/testing/business_rules.rb', line 267 def add_field(field_name) @fields.add_field(self, field_name) end |
#append_data_with(example) ⇒ Object
344 345 346 |
# File 'lib/remi/testing/business_rules.rb', line 344 def append_data_with(example) @data_subject.df = @data_subject.df.concat example_to_df(example) end |
#column_hash ⇒ Object
Public: Converts the data subject to a hash where the keys are the table columns and the values are an array for the value of column for each row.
289 290 291 292 293 294 |
# File 'lib/remi/testing/business_rules.rb', line 289 def column_hash @data_subject.df.to_h.reduce({}) do |h, (k,v)| h[k.symbolize] = v.to_a h end end |
#csv_options ⇒ Object
404 405 406 |
# File 'lib/remi/testing/business_rules.rb', line 404 def @data_subject. end |
#cumulative_dist_from_freq_table(table, freq_field: 'frequency') ⇒ Object
357 358 359 360 361 362 363 364 365 366 367 |
# File 'lib/remi/testing/business_rules.rb', line 357 def cumulative_dist_from_freq_table(table, freq_field: 'frequency') cumulative_dist = {} freq_total = 0 table.hashes.each do |row| low = freq_total high = freq_total + row[freq_field].to_f freq_total = high cumulative_dist[(low...high)] = row.tap { |r| r.delete(freq_field) } end cumulative_dist end |
#distribute_values(table) ⇒ Object
384 385 386 387 388 389 390 391 392 |
# File 'lib/remi/testing/business_rules.rb', line 384 def distribute_values(table) cumulative_dist = cumulative_dist_from_freq_table(table) generated_data = generate_values_from_cumulative_dist(@data_subject.df.size, cumulative_dist) generated_data.each do |field_name, data_array| vector_name = fields[field_name].field_name @data_subject.df[vector_name] = Daru::Vector.new(data_array, index: @data_subject.df.index) end end |
#example_to_df(example) ⇒ Object
329 330 331 332 333 334 335 336 337 |
# File 'lib/remi/testing/business_rules.rb', line 329 def example_to_df(example) df = example.to_df(@data_subject.df.row[0].to_h, field_symbolizer: @data_subject.field_symbolizer) data_subject.fields.each do |vector, | if [:type] == :json df[vector].recode! { |v| JSON.parse(v) rescue v } end end df end |
#field ⇒ Object
271 272 273 |
# File 'lib/remi/testing/business_rules.rb', line 271 def field @fields.only end |
#fields ⇒ Object
275 276 277 |
# File 'lib/remi/testing/business_rules.rb', line 275 def fields @fields end |
#freq_by(*field_names) ⇒ Object
394 395 396 |
# File 'lib/remi/testing/business_rules.rb', line 394 def freq_by(*field_names) @data_subject.df.group_by(field_names).size * 1.0 / @data_subject.df.size end |
#generate_values_from_cumulative_dist(n_records, cumulative_dist) ⇒ Object
369 370 371 372 373 374 375 376 377 378 379 380 381 382 |
# File 'lib/remi/testing/business_rules.rb', line 369 def generate_values_from_cumulative_dist(n_records, cumulative_dist) # Use the same key for reproducible tests psuedorand = Random.new(3856382695386) 1.upto(n_records).reduce({}) do |h, idx| r = psuedorand.rand row_as_hash = cumulative_dist.select { |range| range.include? r }.values.first row_as_hash.each do |field_name, value| h[field_name] ||= [] h[field_name] << value end h end end |
#get_attrib(name) ⇒ Object
283 284 285 |
# File 'lib/remi/testing/business_rules.rb', line 283 def get_attrib(name) @data_subject.send(name) end |
#replicate_rows(n_rows) ⇒ Object
349 350 351 352 353 354 355 |
# File 'lib/remi/testing/business_rules.rb', line 349 def replicate_rows(n_rows) replicated_df = Daru::DataFrame.new([], order: @data_subject.df.vectors.to_a) @data_subject.df.each do |vector| replicated_df[vector.name] = vector.to_a * n_rows end @data_subject.df = replicated_df end |
#size ⇒ Object
279 280 281 |
# File 'lib/remi/testing/business_rules.rb', line 279 def size @data_subject.df.size end |
#stub_data ⇒ Object
325 326 327 |
# File 'lib/remi/testing/business_rules.rb', line 325 def stub_data @data_subject.stub_df if @data_subject.respond_to? :stub_df end |
#stub_data_with(example) ⇒ Object
339 340 341 342 |
# File 'lib/remi/testing/business_rules.rb', line 339 def stub_data_with(example) stub_data @data_subject.df = example_to_df(example) end |
#unique_integer_field(field_name) ⇒ Object
398 399 400 401 402 |
# File 'lib/remi/testing/business_rules.rb', line 398 def unique_integer_field(field_name) vector_name = fields[field_name].field_name i = 0 @data_subject.df[vector_name].recode! { |v| i += 1 } end |
#where(field_name, operation) ⇒ Object
Would like to have this return a new DataSubject and not a dataframe. Need more robust duping to make that feasible. Don’t use results for anything more than size.
299 300 301 |
# File 'lib/remi/testing/business_rules.rb', line 299 def where(field_name, operation) @data_subject.df.where(@data_subject.df[field_name.symbolize(@data_subject.field_symbolizer)].recode { |v| operation.call(v) }) end |
#where_between(field_name, low_value, high_value) ⇒ Object
315 316 317 |
# File 'lib/remi/testing/business_rules.rb', line 315 def where_between(field_name, low_value, high_value) where(field_name, ->(v) { v.to_f.between?(low_value.to_f, high_value.to_f) }) end |
#where_gt(field_name, value) ⇒ Object
311 312 313 |
# File 'lib/remi/testing/business_rules.rb', line 311 def where_gt(field_name, value) where(field_name, ->(v) { v.to_f > value.to_f }) end |
#where_in(field_name, list) ⇒ Object
319 320 321 322 |
# File 'lib/remi/testing/business_rules.rb', line 319 def where_in(field_name, list) list_array = list.split(',').map { |v| v.strip } where(field_name, ->(v) { list_array.include?(v) }) end |
#where_is(field_name, value) ⇒ Object
303 304 305 |
# File 'lib/remi/testing/business_rules.rb', line 303 def where_is(field_name, value) where(field_name, ->(v) { v == value }) end |
#where_lt(field_name, value) ⇒ Object
307 308 309 |
# File 'lib/remi/testing/business_rules.rb', line 307 def where_lt(field_name, value) where(field_name, ->(v) { v.to_f < value.to_f }) end |