cancer_predict/app/controllers/cancerpredicts_controller.rb

294 lines
17 KiB
Ruby
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# encoding: utf-8
require 'rubyXL'
class CancerpredictsController < ApplicationController
def initialize
super
@app_title = "cancerpredict"
end
def calculate
uid = OrbitHelper.params[:uid] rescue ""
tags = OrbitHelper.widget_tags
categories = OrbitHelper.widget_categories || []
create_first_field
@record = Cancerpredictrecord.new
@record.title = @app_title
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['age1'] = -(params['data']['age'].to_f/100)**(-0.5)
result['age2'] = -((params['data']['age'].to_f/100)**(-0.5))*Math.log(params['data']['age'].to_f/100,Math.exp(1))
result['size1'] = -Math.log(params['data']['size'].to_f/10,Math.exp(1))
result['nposit'] = -((params['data']['ratio'].to_f+0.1)/0.1)**0.5
if params['data']['grade'].to_i == 2
result['grade_2'] = 1
else
result['grade_2'] = 0
end
if params['data']['grade'].to_i == 3
result['grade_3'] = 1
else
result['grade_3'] = 0
end
if params['data']['subtype'].to_i == 2
result['subtype_HER2'] = 1
else
result['subtype_HER2'] = 0
end
if params['data']['subtype'].to_i == 3
result['subtype_triple'] = 1
else
result['subtype_triple'] = 0
end
if params['data']['pstage'].to_i == 2
result['pstage_2'] = 1
else
result['pstage_2'] = 0
end
if params['data']['pstage'].to_i == 3
result['pstage_3'] = 1
else
result['pstage_3'] = 0
end
if params['data']['pstage'].to_i == 4
result['pstage_4'] = 1
else
result['pstage_4'] = 0
end
if params['data']['lvi'].to_i == 1
result['lvi_yes'] = 1
else
result['lvi_yes'] = 0
end
result['lpv'] = ((result['age1']-0.7276655)*(10.87)+(result['age2']+0.4540707)*8.968+(result['size1']-0.643632)*0.7678+(result['nposit']-1.346932)*0.5339+
result['grade_2']*0.4795+result['grade_3']*0.818+result['subtype_HER2']*0.1806+result['subtype_triple']*0.6457+result['pstage_2']*0.5311+
result['pstage_3']*1.134+result['pstage_4']*2.172+result['lvi_yes']*0.3321-0.04 rescue 'error')
@years = ['1','3','5']
result['table'] = '<input id="current_year" type="hidden" value="'+@years[0]+'" index="0"/><p id="cancer_table_texts">此分析微針對已接受手術後病人下表為此病人分別在第1年、3及5年下根據選定的術後治療所估計的存活率</p>'
result['table'] += '<a>第</a>'
result['years'] = @years
@years.each{|year| result['table'] += ('<button class="cancer_years cancer_table_btn btn btn-default btn-sm">'+year+'</button>')}
result['table'] += '<a>年</a>'
@table_head = (locale == "zh_tw") ? ['治療','額外治療效益','總生存率(%)'] :['Treatment','Additional Benefit','Overall Survival(%)']
@head_name = ['Treatment','Additional_Benefit','Overall_Survival']
@therapy_choices = (locale == "zh_tw") ? ['純手術'] :['Surgery only']
@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
result['texts'] = '<a>此研究分析來自已接受根除性手術後之婦女所得之結果,根據您所輸入的資訊以及治療方式,</a>'+'<div style="clear:both;"></div>'+'<a>在術後第</a>'
@years.each{|year| result['texts'] += ('<button class="cancer_years cancer_table_btn btn btn-default btn-sm">'+year+'</button>')}
result['texts'] += '<a>年</a>'
@servive_ratio = ((Math.exp(-0.001476145)**(Math.exp(result['lpv'])))*100).round
result['texts'] += ('<p class="show"><a>100 位只接受根除性手術的婦女中,有 '+ '<a class="'+@therapy_names[0]+' Overall_Survival">'+@servive_ratio.round.to_s+'</a>'+'<a> 位婦女,術後 </a>'+'<a class="surgery_year">'+@years[0].to_s+'</a>'+'<a>年仍為存活</a></p>')
result['texts'] += '<a class="addition">'+',此外</a><div class="extra-text" style="display:none;"><div class="texts_show" style="clear:both;"></div>'
result['texts'] += '</div>'
result['servive_ratio'] = @servive_ratio
result = result.merge(params)
render :json=> result
end
def index
uid = OrbitHelper.params[:uid] rescue ""
tags = OrbitHelper.widget_tags
categories = OrbitHelper.widget_categories || []
@table_str = '<div id="cancer_table"><div id="show_help_modal" class="modal fade"></div>'
@table_str_left = '<div id="cancer_table_left">'
@table_str_right = '<div id="cancer_table_right">'
create_first_field
@form_to_show.form_show.each do |num,property|
@field_property = {}
property.each do |key,value|
@value= value
if @value.class == BSON::Document || @value.class == Hash
@disp_value = @value[I18n.locale.to_s] rescue ""
else
@disp_value = @value
end
@field_property[key] = @disp_value
end
if @field_property['right'] == 0
@table_str_left += '<div data-key='+num.to_s+'>'
break if @field_property["name"] == ""
@table_str_left += '<label for="'+@field_property["variable"]+'" style="float:left;'+(@field_property["comment_text"] == "" ? "margin-right: 2.125em;" : "")+'" class="cencer_table_name">'
@table_str_left += @field_property["name"]
@table_str_left += '</label>'
if @field_property["comment_text"] != ""
@table_str_left += '<button tabindex="0" class="cancer_help_btn" data-target="#show_help_modal" style="float:left;cursor: pointer;padding: 0em 0.475em;font-size: 1.25em;border-radius: 15px;background-color: rgb(210, 106, 2);border-color: rgb(210, 106, 2);color: white;"></button>'
@table_str_left += '<input class="help_texts" type="hidden" value="'+@field_property["comment_text"]+'" name='+@field_property["variable"]+'/>'
else
@table_str_left += '<div style="margin-right: 2.125em;"></div>'
end
if @field_property["is_num"] == 1
if @field_property["is_float"] == 1
@table_str_left += '<input class="num_only float_num cancer_form_field" id="'+@field_property["variable"]+'" style="float:left;clear:right;" data-range="'+@field_property["range"].to_s+'"/>'
else
@table_str_left += '<form class="for_num">'
@table_str_left += '<input class="num_only cancer_form_field" id="'+@field_property["variable"]+'" style="float:left;clear:right;" data-range="'+@field_property["range"].to_s+'" onkeyup="value=value.replace(/[^\d]/g,\'\') "onbeforepaste="clipboardData.setData(\'text\',clipboardData.getData(\'text\').replace(/[^\d]/g,\'\'))"/>'
@please_choice = (I18n.locale.to_s == "zh_tw") ? "請選擇" : "Please choice"
@table_str_left += '<select class="select_num"><option value="">'+@please_choice+'</option>'
for @num in @field_property["range"][0] .. @field_property["range"][1]
@table_str_left += ('<option value="' + @num.to_s + '">' + @num.to_s + '</option>')
end
@table_str_left += '</select></form>'
end
else
@table_str_left += '<div class= "cancer-btn-group cancer_form_field" id="'+@field_property["variable"]+'" style="float:left;clear:right;">'
@field_property["choice_fields"].each do |create_choice|
@table_str_left += '<button class="cancer_table_btn btn btn-default btn-sm">'+create_choice+'</button>'
@table_str_left += '<input type="hidden" value="0" name="'+create_choice+'"/>'
end
@table_str_left += '</div>'
end
if @field_property["hint"].to_s != ""
@table_str_left +='<div style="color: rgb(104, 104, 104);font-size:0.75em;clear:both;">'+@field_property["hint"]+'</div>'
end
@table_str_left += '</div><div style="clear:both;"></div>'
else
@table_str_right += '<div data-key='+num.to_s+'>'
break if @field_property["name"] == ""
@table_str_right += '<label for="'+@field_property["variable"]+'" style="float:left;'+(@field_property["comment_text"] == "" ? "margin-right: 2.125em;" : "")+'" class="cencer_table_name">'
@table_str_right += @field_property["name"]
@table_str_right += '</label>'
if @field_property["comment_text"] != ""
@table_str_right += '<button tabindex="0" class="cancer_help_btn" data-target="#show_help_modal" style="float:left;cursor: pointer;padding: 0em 0.475em;font-size: 1.25em;border-radius: 15px;background-color: rgb(210, 106, 2);border-color: rgb(210, 106, 2);color: white;"></button>'
@table_str_right += '<input class="help_texts" type="hidden" value="'+@field_property["comment_text"]+'" name='+@field_property["variable"]+'/>'
else
@table_str_right += '<div style="margin-right: 2.125em;"></div>'
end
if @field_property["is_num"] == 1
if @field_property["is_float"] == 1
@table_str_right += '<input class="num_only float_num cancer_form_field" id="'+@field_property["variable"]+'" style="float:left;clear:right;" data-range="'+@field_property["range"].to_s+'"/><button class="btn-sub"></button><button class="btn-add"></button>'
else
@table_str_right += '<form class="for_num">'
@table_str_right += '<input class="num_only cancer_form_field" id="'+@field_property["variable"]+'" style="float:left;clear:right;" data-range="'+@field_property["range"].to_s+'" onkeyup="value=value.replace(/[^\d]/g,\'\') "onbeforepaste="clipboardData.setData(\'text\',clipboardData.getData(\'text\').replace(/[^\d]/g,\'\'))"/>'
@please_choice = (I18n.locale.to_s == "zh_tw") ? "請選擇" : "Please choice"
@table_str_right += '<select class="select_num"><option value="">'+@please_choice+'</option>'
for @num in @field_property["range"][0] .. @field_property["range"][1]
@table_str_right += ('<option value="' + @num.to_s + '">' + @num.to_s + '</option>')
end
@table_str_right += '</select></form>'
end
else
@table_str_right += '<div class= "cancer-btn-group cancer_form_field" id="'+@field_property["variable"]+'" style="float:left;clear:right;">'
@field_property["choice_fields"].each do |create_choice|
@table_str_right += '<button class="cancer_table_btn btn btn-default btn-sm">'+create_choice+'</button>'
@table_str_right += '<input type="hidden" value="0" name="'+create_choice+'"/>'
end
@table_str_right += '</div>'
end
if @field_property["hint"].to_s != ""
@table_str_right +='<div class="hint-texts">'+@field_property["hint"]+'</div>'
end
@table_str_right += '</div><div style="clear:both;"></div>'
end
end
@table_str_left += '</div>'
@table_str_right += '</div>'
@table_str += (@table_str_left+@table_str_right)
@table_str +='<div style="clear:both;"></div>'
@table_result_str = '<div id="cancer_predict_result_block"><div id="cancer_predict_result"><span class="result_title">'+t("cancerpredict.table.result").to_s+'</span><div style="clear:both;"></div>'
@tab_name = ['table','text']
@table_result_str += '<div id="result_contents"><div class="result_tab_group">'
@tab_name.each_with_index{|name,index| @table_result_str += ('<div id="result_'+name+'_tab" class="result_tab'+((index != 0) ? "" : " active")+'"><a>'+t("cancerpredict.table."+name).to_s+'</a></div><div style="clear:both;"></div>')}
@table_result_str += '</div><div class="result_content_group">'
@tab_name.each{|name| @table_result_str += '<div id="result_'+name+'_content" class="result_content"><a>'+'no content'+'</a></div>'}
@table_result_str += '</div></div></div>'
@table_button = '<div id="cancer_table_button_group">'
@submit_btn_str='<button id="cancer_table_submit">'+t('cancerpredict.table.Submit').to_s+'</button>'
@reset_btn_str='<button id="cancer_table_reset">'+t('cancerpredict.table.Reset').to_s+'</button>'
@table_button += (@submit_btn_str+@reset_btn_str+'<div style="clear:both;"></div></div></div>')
@table_result_choice_fileds = '<div id="cancer_table_right_result">' if @form_to_show.form_result_is_right == 1
@table_result_choice_fileds = '<div id="cancer_table_left_result">' if @form_to_show.form_result_is_right == 0
@table_result_choice_fileds += '<span class="result_title">'+t('cancerpredict.table.Therapy_choice').to_s+'</span><div style="clear:both;"></div>'
@table_result_choice_fileds += '<div id="choice_fields">'
@form_to_show.form_show_in_result.each do |num,property|
@field_property = {}
property.each do |key,value|
@value= value
if @value.class == BSON::Document || @value.class == Hash
@disp_value = @value[I18n.locale.to_s] rescue ""
else
@disp_value = @value
end
@field_property[key] = @disp_value
end
@table_result_choice_fileds += '<div data-key='+num.to_s+'>'
break if @field_property["name"] == ""
@table_result_choice_fileds += '<label for="'+@field_property["variable"]+'" style="float:left;'+(@field_property["comment_text"] == "" ? "margin-right: 2.125em;" : "")+'" class="cencer_table_name">'
@table_result_choice_fileds += @field_property["name"]
@table_result_choice_fileds += '</label>'
if @field_property["comment_text"] != ""
@table_result_choice_fileds += '<button tabindex="0" class="cancer_help_btn" data-target="#show_help_modal" style="float:left;cursor: pointer;padding: 0em 0.475em;font-size: 1.25em;border-radius: 15px;background-color: rgb(210, 106, 2);border-color: rgb(210, 106, 2);color: white;"></button>'
@table_result_choice_fileds += '<input class="help_texts" type="hidden" value="'+@field_property["comment_text"]+'" name='+@field_property["variable"]+'></input>'
else
@table_result_choice_fileds += '<div style="margin-right: 2.125em;"></div>'
end
if @field_property["is_num"] == 1
if @field_property["is_float"] == 1
@table_result_choice_fileds += '<input class="num_only float_num cancer_form_field" id="'+@field_property["variable"]+'" style="float:left;clear:right;" data-range="'+@field_property["range"].to_s+'"/><button class="btn-sub"></button><button class="btn-add"></button>'
else
@table_result_choice_fileds += '<form class="for_num">'
@table_result_choice_fileds += '<input class="num_only cancer_form_field" id="'+@field_property["variable"]+'" style="float:left;clear:right;" data-range="'+@field_property["range"].to_s+'" onkeyup="value=value.replace(/[^\d]/g,\'\') "onbeforepaste="clipboardData.setData(\'text\',clipboardData.getData(\'text\').replace(/[^\d]/g,\'\'))"/><button class="btn-sub"></button><button class="btn-add"></button>'
@please_choice = (I18n.locale.to_s == "zh_tw") ? "請選擇" : "Please choice"
@table_result_choice_fileds += '<select class="select_num"><option value="">'+@please_choice+'</option>'
for @num in @field_property["range"][0] .. @field_property["range"][1]
@table_result_choice_fileds += ('<option value="' + @num.to_s + '">' + @num.to_s + '</option>')
end
@table_result_choice_fileds += '</select></form>'
end
else
@table_result_choice_fileds += '<div class= "cancer-btn-group cancer_form_field" id="'+@field_property["variable"]+'" style="float:left;clear:right;">'
@field_property["choice_fields"].each do |create_choice|
@table_result_choice_fileds += '<button class="cancer_table_btn btn btn-default btn-sm">'+create_choice+'</button>'
@table_result_choice_fileds += '<input type="hidden" value="0" name="'+create_choice+'"></input>'
end
@table_result_choice_fileds += '</div>'
end
if @field_property["hint"].to_s != ""
@table_result_choice_fileds +='<div class="hint-texts">'+@field_property["hint"]+'</div>'
end
@table_result_choice_fileds += '</div><div style="clear:both;"></div>'
end
@table_result_choice_fileds += '</div></div></div>'
@table_str += (@table_button+@table_result_str+@table_result_choice_fileds+'<div style="clear:both;"></div>')
{
"cancerpredict" => [],
"extras"=>{"table"=> @table_str}
}
end
def widget
uid = OrbitHelper.params[:uid] rescue ""
tags = OrbitHelper.widget_tags
categories = OrbitHelper.widget_categories || []
end
def create_first_field
if Cancerpredictfields.where("title"=>(@app_title + "_back")).take_while{true}.count == 0
@form_to_show = Cancerpredictfields.new()
@form_to_show.title = @app_title +"_back"
@form_to_show.save
end
@form_to_show
if Cancerpredictfields.where("title"=>@app_title).take_while{true}.count == 0
@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
end
end