Class: OpenTox::Parser::Table

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTable

Returns a new instance of Table.



537
538
539
540
541
# File 'lib/parser.rb', line 537

def initialize
  @data = {}
  @activity_errors = []
  @max_class_values = 3
end

Instance Attribute Details

#compoundsObject

Returns the value of attribute compounds.



535
536
537
# File 'lib/parser.rb', line 535

def compounds
  @compounds
end

#dataObject

Returns the value of attribute data.



535
536
537
# File 'lib/parser.rb', line 535

def data
  @data
end

#featuresObject

Returns the value of attribute features.



535
536
537
# File 'lib/parser.rb', line 535

def features
  @features
end

Instance Method Details

#add_to_dataset(dataset) ⇒ Object



582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
# File 'lib/parser.rb', line 582

def add_to_dataset(dataset)
  features.each do |feature_name|
    feature_uri = File.join(dataset.uri,"feature",URI.encode(feature_name))
    dataset.add_feature(feature_uri,{DC.title => feature_name})
  end

  @data.each do |compound,row|
    unless row.empty?
      row.each do |feature,value|
        if OpenTox::Algorithm::numeric?(value)
          value = value.to_f
        elsif value.nil? or value.empty?
          value = nil
        else
          value = value.to_s
        end
        feature_uri = File.join(dataset.uri,"feature",URI.encode(feature))
        dataset.add(compound, feature_uri, value)
        #dataset.features[feature_uri][RDF.type] = feature_types(feature)
        #dataset.features[feature_uri][OT.acceptValue] = feature_values(feature)
        if feature_types(feature).include? OT.NumericFeature
          dataset.features[feature_uri][RDF.type] = [OT.NumericFeature]
        else
          dataset.features[feature_uri][RDF.type] = [OT.NominalFeature]
          dataset.features[feature_uri][OT.acceptValue] = feature_values(feature) 
        end
      end
    end
  end
end

#clean_featuresObject



555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
# File 'lib/parser.rb', line 555

def clean_features
  ignored_features = []
  features.each do |feature|
    if feature_values(feature).size > @max_class_values
      if feature_types(feature).size == 1 and feature_types(feature).first == OT.NumericFeature
        # REGRESSION
      elsif feature_types(feature).include? OT.NumericFeature
        @data.each{|c,row| row[feature] = nil unless OpenTox::Algorithm::numeric?(row[feature]) } # delete nominal features
        @activity_errors << "Nominal feature values of #{feature} ignored (using numeric features for regression models)."
      else
        @activity_errors << "Feature #{feature} ignored (more than #{@max_class_values} nominal feature values and no numeric values)."
        ignored_features << feature
        next
      end
    elsif feature_values(feature).size <= 1
        @activity_errors << "Feature #{feature} ignored (less than 2 feature values)."
        ignored_features << feature
    else
      # CLASSIFICATION
    end
  end
  ignored_features.each do |feature|
    @data.each{ |c,row| row.delete feature }
  end
  @activity_errors
end

#feature_types(feature) ⇒ Object



547
548
549
# File 'lib/parser.rb', line 547

def feature_types(feature)
  @data.collect{|c, row| feature_type(row[feature])}.uniq.compact
end

#feature_values(feature) ⇒ Object



543
544
545
# File 'lib/parser.rb', line 543

def feature_values(feature)
  @data.collect{|c, row| row[feature]}.uniq.compact
end