2019-11-15 09:02:17 +00:00
|
|
|
# encoding: utf-8
|
|
|
|
require 'rubyXL'
|
2022-01-23 06:00:57 +00:00
|
|
|
require "json"
|
2019-11-15 09:02:17 +00:00
|
|
|
class CancerpredictsController < ApplicationController
|
|
|
|
def initialize
|
|
|
|
super
|
|
|
|
@app_title = "cancerpredict"
|
|
|
|
end
|
2020-01-13 11:35:30 +00:00
|
|
|
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] = ('<img class="head_logo" alt ="' + Pathname.new(@image.temp_file.file.file).basename.to_s+'" src="'+@url+'"/>')
|
|
|
|
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] = ('<img class="head_logo" alt ="' + Pathname.new(@image.temp_file.file.file).basename.to_s+'" src="'+@url+'"/>')
|
|
|
|
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]]
|
2020-10-23 02:52:23 +00:00
|
|
|
elsif params['get_mapping_data_from_csv'].to_i == 1
|
|
|
|
result = {}
|
2022-01-23 06:00:57 +00:00
|
|
|
result['mapping_data_from_csv'] = JSON.parse(@form_to_show.mapping_data_from_csv) rescue {}
|
2020-01-13 11:35:30 +00:00
|
|
|
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 = {}
|
2020-10-23 02:52:23 +00:00
|
|
|
# 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
|
2022-01-23 06:00:57 +00:00
|
|
|
mapping_data_from_csv = JSON.parse(@form_to_show.mapping_data_from_csv) rescue {}
|
2020-10-23 02:52:23 +00:00
|
|
|
@form_to_show.form_show.each do |num,property|
|
|
|
|
@variable = property[:variable]
|
|
|
|
if @variable.present?
|
|
|
|
if property[:is_num] == 1
|
2022-01-22 10:47:32 +00:00
|
|
|
if property[:is_float] == 1
|
|
|
|
result[@variable] = params['data'][@variable].to_f rescue 0.0
|
|
|
|
else
|
|
|
|
result[@variable] = params['data'][@variable].to_i rescue 0
|
|
|
|
end
|
2020-10-23 02:52:23 +00:00
|
|
|
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'
|
2022-01-23 06:00:57 +00:00
|
|
|
formula_variables = @form_to_show.tmp_lpv_variables
|
|
|
|
begin
|
|
|
|
eval_formula(result)
|
|
|
|
rescue => e
|
|
|
|
@form_to_show.generate_eval_formula
|
|
|
|
eval_formula(result)
|
|
|
|
end
|
|
|
|
result['lpv'] = instance_variable_get(formula_variables.last)
|
2020-10-23 02:52:23 +00:00
|
|
|
result['lpv_variable'] = {}
|
|
|
|
formula_variables.each do |variable_name|
|
2022-01-23 06:00:57 +00:00
|
|
|
result['lpv_variable']["#{variable_name[1..-1]}"] = instance_variable_get(variable_name)
|
2020-01-13 11:35:30 +00:00
|
|
|
end
|
|
|
|
@years = @form_to_show.years
|
2020-03-04 07:37:59 +00:00
|
|
|
result['table'] = '<input id="current_year" type="hidden" value="'+@years[0].to_s+'" index="0"/><p id="cancer_table_texts">'+@form_to_show.table_above_texts[locale].to_s+'</p>'
|
2020-02-03 07:26:04 +00:00
|
|
|
result['table'] += ('<a style="display: inline-block;">'+(locale == 'zh_tw' ? '第' : '')+'</a><a style="display: inline-block;">')
|
2020-01-13 11:35:30 +00:00
|
|
|
result['years'] = @years
|
|
|
|
@years.each{|year| result['table'] += ('<button class="cancer_years cancer_table_btn btn btn-default btn-sm">'+year.to_s+'</button>')}
|
2020-02-03 07:26:04 +00:00
|
|
|
result['table'] += ('</a><a style="display: inline-block;">'+(locale == 'zh_tw' ? '年' : '')+'</a>')
|
2020-03-18 10:22:13 +00:00
|
|
|
#@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')]
|
2020-01-13 11:35:30 +00:00
|
|
|
@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'] += '<table><thead><tr>'
|
|
|
|
@table_head.each_with_index{|head,index| result['table'] += ('<th class="cancer_th '+@head_name[index]+'">' + head + '</th>')}
|
|
|
|
result['table'] += '</tr></thead><tbody>'
|
|
|
|
@therapy_choices.each_with_index do |choice,i|
|
|
|
|
result['table'] += '<tr class="'+@therapy_names[i].to_s+'">'
|
|
|
|
@table_head.each_with_index do |head,index|
|
|
|
|
result['table'] += ('<td class="cancer_td '+ @head_name[index]+'">' + ((index == 0) ? (((i==0)? '' : '+') + choice) : '-') + '</td>')
|
|
|
|
end
|
|
|
|
result['table'] += '</tr>'
|
|
|
|
end
|
2020-10-23 02:52:23 +00:00
|
|
|
year = params['data']['year'] rescue nil
|
|
|
|
if year.nil?
|
|
|
|
year = @years.first.to_f
|
|
|
|
else
|
|
|
|
year = year.to_f
|
|
|
|
end
|
|
|
|
year_index = @years.index(year)
|
2022-01-23 06:00:57 +00:00
|
|
|
@servive_ratio = eval(@form_to_show.tmp_years_settings_for_ruby[year_index])
|
2020-10-23 02:52:23 +00:00
|
|
|
@servive_ratio = ((1 - @servive_ratio) * 100).round(2)
|
2020-03-04 07:37:59 +00:00
|
|
|
@texts = @form_to_show.text_above_texts[locale].to_s.gsub('<br/>','</span><br/><span>').gsub('{{Surgery_only}}','<span class="'+@therapy_names[0]+' Overall_Survival">'+@servive_ratio.round(2).to_s+'</span>')
|
2020-01-13 11:35:30 +00:00
|
|
|
@texts = @texts.split('{{years}}')
|
|
|
|
@texts.delete('')
|
|
|
|
result['texts'] = '<span>'+@texts[0]
|
|
|
|
@years.each{|year| result['texts'] += ('<button class="cancer_years cancer_table_btn btn btn-default btn-sm" style="float:none;">'+year.to_s+'</button>')}
|
|
|
|
if @texts.count > 1
|
|
|
|
result['texts'] += (@texts[1]+'</span>') if @texts.count > 1
|
|
|
|
else
|
|
|
|
result['texts'] += '</span>'
|
|
|
|
end
|
2020-03-04 07:37:59 +00:00
|
|
|
if !@form_to_show.surgery_only_texts[locale].blank?
|
|
|
|
@surgery_only_texts = @form_to_show.surgery_only_texts[locale]
|
|
|
|
@surgery_only_texts.insert(0,'<p class="show"><span>')
|
|
|
|
@surgery_only_texts = @surgery_only_texts.gsub('{{Surgery_only}}','<span class="'+@therapy_names[0]+' Overall_Survival">'+@servive_ratio.round.to_s+'</span><span>')
|
|
|
|
@surgery_only_texts = @surgery_only_texts.gsub('{{surgery_year}}','</span><span class="surgery_year">'+@years[-1].to_s+'</span><span>')
|
|
|
|
@surgery_only_texts += '</span>'
|
|
|
|
else
|
|
|
|
@surgery_only_texts = ''
|
|
|
|
end
|
2020-01-13 11:35:30 +00:00
|
|
|
result['texts'] += @surgery_only_texts
|
|
|
|
result['texts'] += '<span class="addition">'+@form_to_show.extra_texts[locale]+'</span><div class="extra-text" style="display:none;"><div class="texts_show" style="clear:both;"></div></div></p>'
|
|
|
|
result['extra_therapy_texts'] = @form_to_show.extra_therapy_texts[locale] rescue @form_to_show.extra_therapy_texts['zh_tw']
|
|
|
|
result['servive_ratio'] = @servive_ratio
|
|
|
|
end
|
|
|
|
result = result.merge(params)
|
|
|
|
render :json=> result
|
|
|
|
# rescue
|
|
|
|
# render :json=> params
|
|
|
|
#end
|
|
|
|
end
|
2019-11-15 09:02:17 +00:00
|
|
|
def index
|
|
|
|
uid = OrbitHelper.params[:uid] rescue ""
|
|
|
|
tags = OrbitHelper.widget_tags
|
|
|
|
categories = OrbitHelper.widget_categories || []
|
2020-01-28 10:29:04 +00:00
|
|
|
@table_str = File.read('public/cancer_tool_table_tmp_'+I18n.locale.to_s+'.txt')
|
2020-01-13 11:35:30 +00:00
|
|
|
{
|
|
|
|
"cancerpredict" => [],
|
|
|
|
"extras"=>{"table"=> @table_str}
|
|
|
|
}
|
2019-11-15 09:02:17 +00:00
|
|
|
end
|
|
|
|
def widget
|
|
|
|
uid = OrbitHelper.params[:uid] rescue ""
|
|
|
|
tags = OrbitHelper.widget_tags
|
|
|
|
categories = OrbitHelper.widget_categories || []
|
|
|
|
end
|
2020-01-13 11:35:30 +00:00
|
|
|
def create_first_field
|
2020-03-18 11:03:51 +00:00
|
|
|
if Cancerpredictfields.where("title"=>(@app_title + "_back")).count == 0
|
2020-01-13 11:35:30 +00:00
|
|
|
@form_to_show = Cancerpredictfields.new()
|
|
|
|
@form_to_show.title = @app_title +"_back"
|
|
|
|
@form_to_show.save
|
|
|
|
end
|
|
|
|
@form_to_show
|
2020-03-18 11:03:51 +00:00
|
|
|
if Cancerpredictfields.where("title"=>@app_title).count == 0
|
2020-01-13 11:35:30 +00:00
|
|
|
@form_to_show = Cancerpredictfields.new()
|
|
|
|
@form_to_show.title=@app_title
|
|
|
|
@form_to_show.save
|
|
|
|
@form_to_show = Cancerpredictfields.where("title"=>@app_title).first
|
|
|
|
else
|
|
|
|
@form_to_show = Cancerpredictfields.where("title"=>@app_title).first
|
|
|
|
end
|
2019-11-15 09:02:17 +00:00
|
|
|
end
|
2020-10-23 02:52:23 +00:00
|
|
|
def read_mapping_file(mapping_file)
|
|
|
|
if mapping_file.class != CancerPredictMappingFile
|
|
|
|
mapping_file = CancerPredictMappingFile.find(mapping_file_id) rescue nil
|
|
|
|
end
|
|
|
|
if !mapping_file.nil?
|
|
|
|
csv_rows = CSV.read(mapping_file.temp_file.file.path)
|
|
|
|
titles = csv_rows[0]
|
|
|
|
infos = {}
|
|
|
|
titles.each_with_index do |title,i|
|
|
|
|
infos[title] = []
|
|
|
|
csv_rows[1..-1].each do |row|
|
|
|
|
infos[title] << row[i].to_f
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return infos
|
|
|
|
else
|
|
|
|
return {}
|
|
|
|
end
|
|
|
|
end
|
2019-11-15 09:02:17 +00:00
|
|
|
end
|