# encoding: utf-8
require 'rubyXL'
class CancerpredictsController < ApplicationController
def initialize
super
@app_title = "cancerpredict"
end
def calculate
create_first_field
#begin
if params['header'].to_i == 1
locale = params['locale'].to_s rescue 'zh_tw'
locale = 'zh_tw' if locale == 'zh_cn'
result = {}
@head_images = {}
@form_to_show.head_images_id.each do |image_id|
next if image_id.to_s == ""
@image = Headimages.find_by(:id=>image_id.to_s)
@url = @image.temp_file.to_s
@head_images[@image.sort_number.to_i] = ('')
end
result['head_images'] = Hash[@head_images.sort].values.join('')
@head_images = {}
@form_to_show.title_images_id.each do |image_id|
next if image_id.to_s == ""
@image = Headimages.find_by(:id=>image_id.to_s)
@url = @image.temp_file.to_s
@head_images[@image.sort_number.to_i] = ('
')
end
result['danger_texts'] = (@form_to_show.danger_texts[locale] rescue '')
result['title'] = Hash[@head_images.sort].values.join('')
result['page_title'] = @form_to_show.title_texts[params[:locale]]
elsif params['get_mapping_data_from_csv'].to_i == 1
result = {}
result['mapping_data_from_csv'] = YAML.load(@form_to_show.mapping_data_from_csv) rescue {}
else
@record = Cancerpredictrecord.new
@record.title = @app_title
@choice_keys = []
@choice_values = []
@choice_names = []
@form_to_show.form_show.values.each{|choice| @choice_keys.push choice[:variable]}
@form_to_show.form_show.values.each{|choice| @choice_values.push choice[:choice_fields]}
@form_to_show.form_show.values.each{|choice| @choice_names.push choice[:name]}
@choice_keys.each_with_index{|key,i| @record.names[key] = @choice_names[i]}
@choice_keys.each_with_index{|key,i| @record.values[key] = @choice_values[i]}
params['data'].each do |rec_key,rec_value|
@record.result[rec_key] = rec_value
end
@record.submit_time = Time.now.to_s
@record.save
locale = params['data']['locale'].to_s rescue 'zh_tw'
locale = 'zh_tw' if locale == 'zh_cn'
result = {}
# result['sex_value'] = params['data']['sex'].to_i - 1
# result['Age_value'] = params['data']['age'].to_i
# if params['data']['calcification_score'].to_i < 1400
# result['cal_value'] = 0
# else
# result['cal_value'] = 1
# end
mapping_data_from_csv = YAML.load(@form_to_show.mapping_data_from_csv) rescue {}
@form_to_show.form_show.each do |num,property|
@variable = property[:variable]
if @variable.present?
if property[:is_num] == 1
result[@variable] = params['data'][@variable].to_f rescue 0.0
elsif property[:choice_fields].present?
if property[:map_values].class == Array && property[:choice_fields].class == Array && property[:map_values].length == property[:choice_fields].length
result[@variable] = property[:map_values][params['data'][@variable].to_i - 1]
else
if property[:revert_value].to_i != 1
result[@variable] = params['data'][@variable].to_i - 1
else
result[@variable] = ((property[:choice_fields].length - params['data'][@variable].to_i) rescue params['data'][@variable].to_i)
end
end
end
if property[:cancer_predict_mapping_file].present?
if (mapping_data_from_csv != {})
mapping_hash = mapping_data_from_csv[@variable]
temp_index = 0
temp_value = result[@variable]
mapping_hash.each_with_index do |(k,v),i|
if i == 0
index_val = v.index(temp_value) rescue nil
if !index_val.nil?
temp_index = index_val
else
closest_value = v.min_by{|x| (temp_value-x).abs}
temp_index = v.index(closest_value)
end
end
result[k] = v[temp_index]
end
end
end
end
end
#result['lpv'] = -0.51427548* (result['sex_value']- 0.508312) + 0.05764604* (result['Age_value'] - 61.894501) + 0.49138819*(result['cal_value'] - 0.334399) rescue 'error'
formula = @form_to_show.prediction_formula.gsub("\r\n"," ").gsub("^","**")
result.keys.each do |k|
formula = formula.gsub(/#{k}?(-|\+|\*|\s|\=)/){ "result[\"#{k}\"]#{$1}" }
end
formula_variables = formula.enum_for(:scan,/([^\=]*)?=/).map { Regexp.last_match[1] }.map{|s| s.strip.split(/(\s*|;\r\n)/).last}
@form_to_show.form_show.values.map{|p| p[:variable]}.each{ |k| formula = formula.gsub(/#{k}?(-|\+|\*|\s|\=)/,"result[\"#{k}\"#{$1}]") }
eval(formula)
result['lpv'] = eval(formula_variables.last)
result['lpv_variable'] = {}
formula_variables.each do |variable_name|
eval( "result['lpv_variable']['#{variable_name}'] = #{variable_name}" )
end
@years = @form_to_show.years
result['table'] = '
'+@form_to_show.table_above_texts[locale].to_s+'
' result['table'] += (''+(locale == 'zh_tw' ? '第' : '')+'') result['years'] = @years @years.each{|year| result['table'] += ('')} result['table'] += (''+(locale == 'zh_tw' ? '年' : '')+'') #@head_name = ['Treatment','Additional_Benefit','Overall_Survival'] @head_name = ['Treatment','Overall_Survival'] @table_head = @head_name.map{|name| I18n.t('cancerpredict.table.'+name)} @therapy_choices = [I18n.t('cancerpredict.table.Surgeryonly')] @form_to_show.form_show_in_result.values.each{|choice| @therapy_choices.push choice["name"][locale]} @therapy_names = ['Surgery_only'] @form_to_show.form_show_in_result.values.each{|choice| @therapy_names.push choice["variable"]} result['treatmeny_method'] = @therapy_names result['table'] += '' + head + ' | ')} result['table'] += '
---|
' + ((index == 0) ? (((i==0)? '' : '+') + choice) : '-') + ' | ') end result['table'] += '