<commit_before>require 'libxml'

module Seek

  class Treatments

    attr_reader :sample_names, :values

    def initialize xml
      @sample_names = []
      @values = {}

      begin
        doc = LibXML::XML::Parser.string(xml).parse
      rescue
        doc=nil
      end

      unless doc.nil?
        extract_from_document doc
      end

    end

    private

    def extract_from_document doc
      doc.root.namespaces.default_prefix="ss"
      sample_sheet = find_samples_sheet doc
      unless sample_sheet.nil?
        row, first_col, last_col = find_treatment_row_and_columns_in_sheet sample_sheet
        unless row.nil?
          collect_values row, first_col, last_col, sample_sheet
          collect_sample_names row, 1, sample_sheet
        end
      end
    end

    def collect_sample_names first_row, col, sheet
      sheet_name = sheet.attributes["name"]
      @sample_names = sheet.find("//ss:sheet[@name='#{sheet_name}']/ss:rows/ss:row/ss:cell[@row >= '#{(first_row+1).to_s}' and @column = '#{col.to_s}']").collect do |cell|
        cell.content
      end
    end

    def collect_values row, first_col, last_col, sheet
      sheet_name = sheet.attributes["name"]
      col_keys = {}
      sheet.find("//ss:sheet[@name='#{sheet_name}']/ss:rows/ss:row/ss:cell[@row >= '#{row.to_s}' and @column >= '#{first_col.to_s}' and @column <= '#{last_col.to_s}']").each do |cell|
        this_col_alpha = cell.attributes["column_alpha"]
        if cell.attributes["row"]==row.to_s

          @values[cell.content]=[]
          col_keys[this_col_alpha]=cell.content
        else
          @values[col_keys[this_col_alpha]] << cell.content
        end
      end
    end

    def find_treatment_row_and_columns_in_sheet sheet
      sheet_name = sheet.attributes["name"]
      treatment_cell_element = sheet.find("//ss:sheet[@name='#{sheet_name}']/ss:rows/ss:row/ss:cell").find do |cell|
        cell.content.match(/treatment.*/i)
      end
      unless treatment_cell_element.nil?
        #find the next column for this row that contains content
        row = treatment_cell_element.attributes["row"]
        col = treatment_cell_element.attributes["column"]
        next_cell_element = sheet.find("//ss:sheet[@name='#{sheet_name}']/ss:rows/ss:row/ss:cell[@row='#{row}' and @column > '#{col}']").find do |cell|
          !cell.content.blank?
        end

        end_column=next_cell_element.attributes["column"] unless next_cell_element.nil?

        return row.to_i+1, col.to_i, end_column.to_i-1

      end
    end
<commit_msg>got all tests working for Treatments<commit_after>require 'libxml'

module Seek

  class Treatments

    attr_reader :sample_names, :values

    def initialize xml
      @sample_names = []
      @values = {}

      begin
        doc = LibXML::XML::Parser.string(xml).parse
      rescue Exception=>e
        doc=nil
        Rails.logger.warn "Invalid xml encountered. - #{e.message}"
      end

      unless doc.nil?
        extract_from_document doc
      end

    end

    private

    def extract_from_document doc
      doc.root.namespaces.default_prefix="ss"
      sample_sheet = find_samples_sheet doc
      unless sample_sheet.nil?
        row, first_col, last_col = find_treatment_row_and_columns_in_sheet sample_sheet
        unless row.nil?
          collect_values row, first_col, last_col, sample_sheet
          collect_sample_names row, 1, sample_sheet
        end
      end
    end

    def collect_sample_names first_row, col, sheet
      sheet_name = sheet.attributes["name"]
      @sample_names = sheet.find("//ss:sheet[@name='#{sheet_name}']/ss:rows/ss:row/ss:cell[@row >= '#{(first_row+1).to_s}' and @column = '#{col.to_s}']").collect do |cell|
        cell.content
      end
    end

    def collect_values row, first_col, last_col, sheet
      sheet_name = sheet.attributes["name"]
      col_keys = {}
      sheet.find("//ss:sheet[@name='#{sheet_name}']/ss:rows/ss:row/ss:cell[@row >= '#{row.to_s}' and @column >= '#{first_col.to_s}' and @column <= '#{last_col.to_s}']").each do |cell|
        this_col_alpha = cell.attributes["column_alpha"]
        if cell.attributes["row"]==row.to_s

          @values[cell.content]=[]
          col_keys[this_col_alpha]=cell.content
        else
          @values[col_keys[this_col_alpha]] << cell.content
        end
      end
    end

    def find_treatment_row_and_columns_in_sheet sheet
      sheet_name = sheet.attributes["name"]
      treatment_cell_element = sheet.find("//ss:sheet[@name='#{sheet_name}']/ss:rows/ss:row/ss:cell").find do |cell|
        cell.content.match(/treatment.*/i)
      end
      unless treatment_cell_element.nil?
        #find the next column for this row that contains content
        row = treatment_cell_element.attributes["row"].to_i
        col = treatment_cell_element.attributes["column"].to_i
        next_cell_element = sheet.find("//ss:sheet[@name='#{sheet_name}']/ss:rows/ss:row/ss:cell[@row='#{row}' and @column > '#{col}']").find do |cell|
          !cell.content.blank?
        end


        if next_cell_element.nil?
          next_cell_element = sheet.find("//ss:sheet[@name='#{sheet_name}']/ss:rows/ss:row/ss:cell[@row='#{row.to_i+1}' and @column > '#{col}']").find do |cell|
            !cell.content.blank?
          end
          end_column = next_cell_element.attributes["column"].to_i unless next_cell_element.nil?
        else
          end_column = next_cell_element.attributes["column"].to_i-1
        end


        return row+1, col, end_column.to_i

      end
    end

{'fraction_non_alphanumeric': 0.13013574660633484, 'fraction_numerical': 0.0016289592760180996, 'mean_word_length': 2.826869806094183, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 7}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '623322', 'n_tokens_mistral': 1798, 'n_tokens_neox': 1765, 'n_words': 325}