cancer_predict/app/models/cancerpredictfields.rb

478 lines
32 KiB
Ruby
Raw Normal View History

2019-11-15 00:44:28 +00:00
class Cancerpredictfields
require "pathname"
2022-01-22 09:27:15 +00:00
require 'json'
2019-11-15 00:44:28 +00:00
include Mongoid::Document
include Mongoid::Timestamps
Field_relations = {"number_field"=>"Fixnum","text_area"=>"String"}
FIELDINFO = {"variable"=>"String","name"=>"String","is_num"=>"Fixnum","hint"=>"String","comment_text"=>"String","choice_fields"=>"Array","range"=>"Array","right"=>"Fixnum","is_float"=>"Fixnum","revert_value"=>"Fixnum","map_values"=>"Array","cancer_predict_mapping_file"=>"String","lpv_impact"=>"Float","active_choice"=>"number_field","disable_condition"=>"text_area"}
NonLoclaized = ["variable","is_num","range","right","is_float","revert_value","map_values","cancer_predict_mapping_file","lpv_impact","active_choice","disable_condition"]
AdvanceFields = ["revert_value","map_values","cancer_predict_mapping_file"]
TherapyFields = ["variable","name","hint","comment_text","choice_fields","lpv_impact","active_choice","disable_condition"]
TherapyOnly = ["lpv_impact","active_choice","disable_condition"]
2019-11-15 00:44:28 +00:00
field :title ,type:String ,default:""
field :advance_mode, type: Boolean, default: true
field :form_show , :type=> Hash ,default:{
"0"=>{"variable"=>"sex_value", "name"=>{"zh_tw"=>"性別<br/>(Sex)", "en"=>"Sex"}, "is_num"=>0, "hint"=>{"zh_tw"=>"", "en"=>""}, "comment_text"=>{"zh_tw"=>"", "en"=>""}, "choice_fields"=>{"zh_tw"=>["", ""], "en"=>["Male", "Female"]}, "range"=>[], "right"=>0, "is_float"=>0, "revert_value"=>1, "map_values"=>[], "cancer_predict_mapping_file"=>"", "need_map_values"=>0},
"1"=>{"variable"=>"age", "name"=>{"zh_tw"=>"年齡<br/>(Age)", "en"=>"Age"}, "is_num"=>1, "hint"=>{"zh_tw"=>"從 20 歲(含)開始至 98 歲", "en"=>"Age must be between 20 and 98"}, "comment_text"=>{"zh_tw"=>"", "en"=>""}, "choice_fields"=>{"zh_tw"=>[], "en"=>[]}, "range"=>[20, 98], "right"=>0, "is_float"=>0, "revert_value"=>0, "map_values"=>[], "cancer_predict_mapping_file"=>BSON::ObjectId('5f8e60441d41c801f600011a'), "need_map_values"=>0},
"2"=>{"variable"=>"calH", "name"=>{"zh_tw"=>"心臟鈣化分數<br/>(Heart Calcification score)", "en"=>"Heart Calcification score"}, "is_num"=>1, "hint"=>{"zh_tw"=>"請輸入0到5000的數字", "en"=>"Please enter a number between 0 and 5000"}, "comment_text"=>{"zh_tw"=>"", "en"=>""}, "choice_fields"=>{"zh_tw"=>[], "en"=>[]}, "range"=>[0, 5000], "right"=>0, "is_float"=>1, "revert_value"=>0, "map_values"=>[], "cancer_predict_mapping_file"=>BSON::ObjectId('5f8e66c71d41c801f6000139'), "need_map_values"=>0},
"3"=>{"variable"=>"calAH", "name"=>{"zh_tw"=>"升主動脈鈣化分數", "en"=>"Aorta ascendens Calcification score"}, "hint"=>{"zh_tw"=>"請輸入0到10000的數字", "en"=>"Please enter a number between 0 and 10000"}, "comment_text"=>{"zh_tw"=>"", "en"=>""}, "choice_fields"=>{"zh_tw"=>[], "en"=>[]}, "is_num"=>1, "range"=>[0, 10000], "right"=>0, "is_float"=>1, "revert_value"=>0, "map_values"=>[], "cancer_predict_mapping_file"=>BSON::ObjectId('5f8e6ded1d41c801f600013d'), "need_map_values"=>0},
"4"=>{"variable"=>"calDH", "name"=>{"zh_tw"=>"降主動脈鈣化分數", "en"=>"Aorta descendens Calcification score"}, "hint"=>{"zh_tw"=>"請輸 入0到10000的數字", "en"=>"Please enter a number between 0 and 10000"}, "comment_text"=>{"zh_tw"=>"", "en"=>""}, "choice_fields"=>{"zh_tw"=>[], "en"=>[]}, "is_num"=>1, "range"=>[0, 10000], "right"=>0, "is_float"=>1, "revert_value"=>0, "map_values"=>[], "cancer_predict_mapping_file"=>BSON::ObjectId('5f8e6ded1d41c801f600013e'), "need_map_values"=>0},
"5"=>{"variable"=>"fat", "name"=>{"zh_tw"=>"脂肪分數", "en"=>"Fat"}, "hint"=>{"zh_tw"=>"請輸入20到408的數字", "en"=>"Please enter a number between 20 and 408"}, "comment_text"=>{"zh_tw"=>"", "en"=>""}, "choice_fields"=>{"zh_tw"=>[], "en"=>[]}, "is_num"=>1, "range"=>[20, 408], "right"=>0, "is_float"=>1, "revert_value"=>0, "map_values"=>[], "cancer_predict_mapping_file"=>BSON::ObjectId('5f8e6ded1d41c801f600013f'), "need_map_values"=>0},
"6"=>{"variable"=>"N4", "name"=>{"zh_tw"=>"腫瘤", "en"=>"Neoplasia"}, "hint"=>{"zh_tw"=>"", "en"=>""}, "comment_text"=>{"zh_tw"=>"", "en"=>""}, "choice_fields"=>{"zh_tw"=>["", "沒有"], "en"=>["yes", "no"]}, "is_num"=>0, "range"=>[], "right"=>0, "is_float"=>0, "revert_value"=>1, "map_values"=>[], "cancer_predict_mapping_file"=>"", "need_map_values"=>0},
"7"=>{"variable"=>"N12", "name"=>{"zh_tw"=>"癡呆症", "en"=>"Dementias"}, "hint"=>{"zh_tw"=>"", "en"=>""}, "comment_text"=>{"zh_tw"=>"", "en"=>""}, "choice_fields"=>{"zh_tw"=>["", "沒有"], "en"=>["yes", "no"]}, "is_num"=>0, "range"=>[], "right"=>0, "is_float"=>0, "revert_value"=>1, "map_values"=>[], "cancer_predict_mapping_file"=>"", "need_map_values"=>0},
"8"=>{"variable"=>"N20", "name"=>{"zh_tw"=>"慢性肝炎", "en"=>" Chronic hepatitis"}, "hint"=>{"zh_tw"=>"", "en"=>""}, "comment_text"=>{"zh_tw"=>"", "en"=>""}, "choice_fields"=>{"zh_tw"=>["", "沒有"], "en"=>["yes", "no"]}, "is_num"=>0, "range"=>[], "right"=>0, "is_float"=>0, "revert_value"=>1, "map_values"=>[], "cancer_predict_mapping_file"=>"", "need_map_values"=>0},
"9"=>{"variable"=>"N31", "name"=>{"zh_tw"=>"垂體侏儒症", "en"=>" Pituitary dwarfism"}, "hint"=>{"zh_tw"=>"", "en"=>""}, "comment_text"=>{"zh_tw"=>"", "en"=>""}, "choice_fields"=>{"zh_tw"=>["", "沒有"], "en"=>["yes", "no"]}, "is_num"=>0, "range"=>[], "right"=>0, "is_float"=>0, "revert_value"=>1, "map_values"=>[], "cancer_predict_mapping_file"=>"", "need_map_values"=>0},
"10"=>{"variable"=>"O6", "name"=>{"zh_tw"=>"慢性腎病", "en"=>" Chronic Kidney Disease"}, "hint"=>{"zh_tw"=>"", "en"=>""}, "comment_text"=>{"zh_tw"=>"", "en"=>""}, "choice_fields"=>{"zh_tw"=>["", "沒有"], "en"=>["yes", "no"]}, "is_num"=>0, "range"=>[], "right"=>1, "is_float"=>0, "revert_value"=>1, "map_values"=>[], "cancer_predict_mapping_file"=>"", "need_map_values"=>0},
"11"=>{"variable"=>"N34", "name"=>{"zh_tw"=>"克羅恩病和潰瘍性結腸炎", "en"=>"Crohn's disease and ulcerative colitis"}, "hint"=>{"zh_tw"=>"", "en"=>""}, "comment_text"=>{"zh_tw"=>"", "en"=>""}, "choice_fields"=>{"zh_tw"=>["", "沒有"], "en"=>["yes", "no"]}, "is_num"=>0, "range"=>[], "right"=>1, "is_float"=>0, "revert_value"=>1, "map_values"=>[], "cancer_predict_mapping_file"=>"", "need_map_values"=>0},
"12"=>{"variable"=>"N14", "name"=>{"zh_tw"=>"帕金森氏", "en"=>"Parkinson's disease"}, "hint"=>{"zh_tw"=>"", "en"=>""}, "comment_text"=>{"zh_tw"=>"", "en"=>""}, "choice_fields"=>{"zh_tw"=>["", "沒有"], "en"=>["yes", "no"]}, "is_num"=>0, "range"=>[], "right"=>1, "is_float"=>0, "revert_value"=>1, "map_values"=>[], "cancer_predict_mapping_file"=>"", "need_map_values"=>0},
"13"=>{"variable"=>"N26", "name"=>{"zh_tw"=>"多發性硬化症", "en"=>"Multiple sclerosis"}, "hint"=>{"zh_tw"=>"", "en"=>""}, "comment_text"=>{"zh_tw"=>"", "en"=>""}, "choice_fields"=>{"zh_tw"=>["", "沒有"], "en"=>["yes", "no"]}, "is_num"=>0, "range"=>[], "right"=>1, "is_float"=>0, "revert_value"=>1, "map_values"=>[], "cancer_predict_mapping_file"=>"", "need_map_values"=>0},
"14"=>{"variable"=>"O3", "name"=>{"zh_tw"=>"高血壓", "en"=>"Multiple sclerosis"}, "hint"=>{"zh_tw"=>"", "en"=>""}, "comment_text"=>{"zh_tw"=>"", "en"=>""}, "choice_fields"=>{"zh_tw"=>["", "沒有"], "en"=>["yes", "no"]}, "is_num"=>0, "range"=>[], "right"=>1, "is_float"=>0, "revert_value"=>1, "map_values"=>[], "cancer_predict_mapping_file"=>"", "need_map_values"=>0},
"15"=>{"variable"=>"O20", "name"=>{"zh_tw"=>"嚴重精神疾病", "en"=>"Severe mental illness"}, "hint"=>{"zh_tw"=>"", "en"=>""}, "comment_text"=>{"zh_tw"=>"", "en"=>""}, "choice_fields"=>{"zh_tw"=>["", "沒有"], "en"=>["yes", "no"]}, "is_num"=>0, "range"=>[], "right"=>1, "is_float"=>0, "revert_value"=>1, "map_values"=>[], "cancer_predict_mapping_file"=>"", "need_map_values"=>0},
"16"=>{"variable"=>"O18", "name"=>{"zh_tw"=>"類風溼關節炎", "en"=>"Rheumatoid arthritis"}, "hint"=>{"zh_tw"=>"", "en"=>""}, "comment_text"=>{"zh_tw"=>"", "en"=>""}, "choice_fields"=>{"zh_tw"=>["", "沒有"], "en"=>["yes", "no"]}, "is_num"=>0, "range"=>[], "right"=>1, "is_float"=>0, "revert_value"=>1, "map_values"=>[], "cancer_predict_mapping_file"=>"", "need_map_values"=>0},
"17"=>{"variable"=>"O11", "name"=>{"zh_tw"=>"非出血性腦血管疾病", "en"=>"Non-Hemorrhagic Cerebrovascular Disease"}, "hint"=>{"zh_tw"=>"", "en"=>""}, "comment_text"=>{"zh_tw"=>"", "en"=>""}, "choice_fields"=>{"zh_tw"=>["", "沒有"], "en"=>["yes", "no"]}, "is_num"=>0, "range"=>[], "right"=>1, "is_float"=>0, "revert_value"=>1, "map_values"=>[], "need_map_values"=>0},
"18"=>{"variable"=>"N29", "name"=>{"zh_tw"=>"強直性脊柱炎", "en"=>"Ankylosing spondylitis"}, "hint"=>{"zh_tw"=>"", "en"=>""}, "comment_text"=>{"zh_tw"=>"", "en"=>""}, "choice_fields"=>{"zh_tw"=>["", "沒有"], "en"=>["yes", "no"]}, "is_num"=>0, "range"=>[], "right"=>1, "is_float"=>0, "revert_value"=>1, "map_values"=>[], "need_map_values"=>0},
"19"=>{"variable"=>"N6", "name"=>{"zh_tw"=>"動脈血管", "en"=>"Arterial vasculopathy"}, "is_num"=>0, "hint"=>{"zh_tw"=>"", "en"=>""}, "comment_text"=>{"zh_tw"=>"", "en"=>""}, "choice_fields"=>{"zh_tw"=>["", "沒有"], "en"=>["yes", "no"]}, "range"=>[], "right"=>1, "is_float"=>0, "revert_value"=>1, "map_values"=>[], "need_map_values"=>0},
"20"=>{"variable"=>"O14", "name"=>{"zh_tw"=>"葡萄糖不耐症", "en"=>nil}, "is_num"=>0, "hint"=>{"zh_tw"=>"", "en"=>nil}, "comment_text"=>{"zh_tw"=>"", "en"=>nil}, "choice_fields"=>{"zh_tw"=>["", "沒有"], "en"=>nil}, "range"=>[], "right"=>1, "is_float"=>0, "revert_value"=>1, "map_values"=>[], "need_map_values"=>0},
"21"=>{"variable"=>"N43", "name"=>{"zh_tw"=>"Basedow's disease", "en"=>nil}, "is_num"=>0, "hint"=>{"zh_tw"=>"", "en"=>nil}, "comment_text"=>{"zh_tw"=>"", "en"=>nil}, "choice_fields"=>{"zh_tw"=>["", "沒有"], "en"=>nil}, "range"=>[], "right"=>1, "is_float"=>0, "revert_value"=>1, "map_values"=>[], "need_map_values"=>0},
"22"=>{"variable"=>"O17", "name"=>{"zh_tw"=>"偏頭痛", "en"=>nil}, "is_num"=>0, "hint"=>{"zh_tw"=>"", "en"=>nil}, "comment_text"=>{"zh_tw"=>"", "en"=>nil}, "choice_fields"=>{"zh_tw"=>["", "沒有"], "en"=>nil}, "range"=>[], "right"=>1, "is_float"=>0, "revert_value"=>1, "map_values"=>[], "need_map_values"=>0},
"23"=>{"variable"=>"O9", "name"=>{"zh_tw"=>"心臟衰竭", "en"=>nil}, "hint"=>{"zh_tw"=>"", "en"=>nil}, "comment_text"=>{"zh_tw"=>"", "en"=>nil}, "choice_fields"=>{"zh_tw"=>["", "沒有"], "en"=>nil}, "is_num"=>0, "range"=>[], "right"=>1, "is_float"=>0, "revert_value"=>1, "map_values"=>[], "need_map_values"=>0}
}
field :form_show_in_result , :type=> Hash ,default: {}
field :form_result_is_right , :type=> Integer ,default: 0
field :text_descibe ,type:Hash ,default: {
"zh_tw"=>"歡迎使用台灣心血管 疾病預後預測系統<br />\r\n本預測系統由全民健保資料庫2017年~2020年間共1700位病人電腦斷層影像所建立之預測模型<br />\r\n請 在下方填入相關資訊",
"en"=>"Welcome to the Taiwan cardiovascular disease prognosis prediction&nbsp;System!<br />\r\nThe prediction system is a prediction model established by CT images of 1,700 patients form the National Health Insurance Database&nbsp;between 2017 to 2020.<br />\r\nTo start, please select the information below."
}
field :small ,type:Hash ,default:{'font_size'=>"0.825em",'active'=>0}
field :medium ,type:Hash ,default:{'font_size'=>"1em",'active'=>1}
field :large ,type:Hash ,default:{'font_size'=>"1.25em",'active'=>0}
2020-03-04 07:37:59 +00:00
field :head_images_id ,type:Array , default: []
field :title_images_id ,type:Array , default: []
field :title_texts ,type:Hash ,default: {"zh_tw"=>"臺灣心血管疾病存活預測", "en"=>"Cardiovascular Disease Survival Forecast in Taiwan"}
field :table_above_texts ,type:Hash ,default: {"zh_tw"=>"下表之分析為針對手術後病人,根據選定的術後治療,分別估計在一年、一年半、兩年及兩年半的心血管疾病住院或死亡機率。", "en"=>"The analysis is for women who had undergone surgery.The table shows the 1,1.5, 2 and 2.5 year survival rates,based on the treatment you have selected."}
field :text_above_texts ,type:Hash ,default: {"zh_tw"=>"此研究分析來自於照射胸部電腦斷層所得之結果,根據您所輸入的資訊,在第{{years}}年內,有{{Surgery_only}}%的 機率可能心血管疾病住院或死亡。", "en"=>"This research comes from the results obtained by irradiating the chest CT. According to the information you entered that you have the {{Surgery_only}}% of hospitalization or death for cardiovascular disease within {{years}} years."}
field :surgery_only_texts ,type:Hash ,default: {"zh_tw"=>"", "en"=>""}
field :extra_texts ,type:Hash ,default: {"zh_tw"=>",此外", "en"=>""}
field :extra_therapy_texts ,type:Hash ,default: {"zh_tw"=>"100 位在術後有接受{{extra_therapy}}的婦女中,有{{survival_num}}位婦女,術後{{surgery_year}}年仍為存活(多了{{Additional_Benefit}}位)", "en"=>"{{survival_num}} out of 100 women treated with {{extra_therapy}} are alive (an extra {{Additional_Benefit}})"}
field :danger_texts ,type:Hash ,default: {"zh_tw"=>"請注意紅框的輸入資料是否符合要求!", "en"=>"Please check whether input data in red blocks are correct!"}
field :years ,type:Array ,default:[1, 1.5, 2, 2.5]
field :texts_between_Result_and_result_block ,type:Hash ,default: {"zh_tw"=>"如果欲將預測結果應用於臨床上,請務必與您的主治醫師討論後再做最後決定。", "en"=>"Please note that the patients need to consult with their medical doctors before making any decision."}
#field :image_uploader ,type:Object
2022-01-22 04:56:22 +00:00
field :prediction_formula , type: String ,default: "A = 0.1327868* (sex_value- 0.4858824)
+ 0.0371720* (age_test1 - 61.56000) -0.07447278* (age_test2 - 13.10152)
2022-01-22 04:56:22 +00:00
+ 0.4315686* (age_test3 - 0.9844332)
2022-01-22 04:56:22 +00:00
+ 0.0009163615*( calH_test1 - 182.9347)
2022-01-22 04:56:22 +00:00
-0.0007536899*( calH_test2 - 124.8706) -0.00004697183*( calH_test3 -80.75636)
2022-01-22 04:56:22 +00:00
+ 0.0001401325*( calAH_test1 - 700.7824)
2022-01-22 04:56:22 +00:00
-0.001349783*( calAH_test2 - 634.2167) +0.001753832*( calAH_test3 -419.3361)
2022-01-22 04:56:22 +00:00
+ 0.0001906046*( calDH_test1 -835.2894) -0.000251567*( calDH_test2 - 213.1630)
2022-01-22 04:56:22 +00:00
-0.002173942*( fat_test1 -108.4149)+0.003066541*( fat_test2 - 28.33497)
2022-01-22 04:56:22 +00:00
+0.6700708*(N4-0.3241176)
2022-01-22 04:56:22 +00:00
+0.3336162*(O3-0.4994118)
2022-01-22 04:56:22 +00:00
+0.1322476*(O20-0.1741176)
2022-01-22 04:56:22 +00:00
+0.9084972*(O18-0.008823529)
2022-01-22 04:56:22 +00:00
+0.2978388*(N12-0.1152941)
2022-01-22 04:56:22 +00:00
+0.1777935*(N20-0.3582353)
2022-01-22 04:56:22 +00:00
+1.588042*(N31-0.002352941)
2022-01-22 04:56:22 +00:00
+0.2197419*(O6-0.07823529)
2022-01-22 04:56:22 +00:00
+1.791159*(N34-0.001176471)
2022-01-22 04:56:22 +00:00
+0.4305973*(N14-0.02176471)
2022-01-22 04:56:22 +00:00
-0.4472885*(N29-0.02411765)
2022-01-22 04:56:22 +00:00
+0.2601319*(N26-0.04941176)
2022-01-22 04:56:22 +00:00
-0.2364269*(O11-0.1164706)
2022-01-22 04:56:22 +00:00
+0.1784179*(N6-0.1070588)
2022-01-22 04:56:22 +00:00
+0.6023170*(O14-0.01294118)
2022-01-22 04:56:22 +00:00
-1.031959*(N43-0.007058824)
2022-01-22 04:56:22 +00:00
+0.4257809*(O17-0.01823529)
2022-01-22 04:56:22 +00:00
+0.2002546*(O9-0.06176471)"
field :years_settings , type: Array , default: ["0.8095037^( exp(A) )", "0.729158^( exp(A) )", "0.6717211^( exp(A) )", "0.6056773^( exp(A) )"]
2022-01-22 09:27:15 +00:00
field :tmp_years_settings , type: Array , default: []
2022-01-23 06:00:57 +00:00
field :tmp_years_settings_for_ruby , type: Array , default: []
field :hidden_variables, type: String, default: ""
field :fix_hidden_variables, type: Array, default: []
field :tmp_hidden_variables_for_ruby, type: String, default: ""
field :tmp_hidden_variables_for_js, type: String, default: ""
2022-01-22 09:27:15 +00:00
field :lpv_calc, type: Hash, default: {} #for js code
field :tmp_lpv_ruby_code, type: String, default: ""
2022-01-23 06:00:57 +00:00
field :tmp_lpv_variables, type: Array, default: []
field :mapping_data_from_csv , type: String ,default: ""
field :all_variables, type: Array, default: []
field :treatment_method, type: Array, default: ['Surgery_only']
field :treatment_method_active_indices, type: Array, default: [1]
field :result_table, type: String, default: "", localize: true
field :result_text, type: String, default: "", localize: true
field :therapy_lpv, type: Array, default: [0]
2019-11-15 00:44:28 +00:00
#before_create :set_expire
2022-01-22 09:27:15 +00:00
before_save do
2022-01-22 10:47:32 +00:00
self.form_show.each do |num,property|
property[:need_map_values] = (property[:map_values].class == Array && property[:choice_fields].class == Array && property[:map_values].length == property[:choice_fields].length) ? 1 : 0
end
2022-01-22 09:27:15 +00:00
result_keys = []
self.form_show.each do |num,property|
variable_name = property[:variable]
if variable_name.present?
result_keys << variable_name
end
end
self.form_show_in_result.each do |num,property|
variable_name = property[:variable]
if variable_name.present?
result_keys << variable_name
end
end
2022-01-22 09:27:15 +00:00
mapping_data = JSON.parse(self.mapping_data_from_csv) rescue {}
if self.advance_mode && mapping_data.present?
2022-01-22 09:27:15 +00:00
mapping_data.each do |k,v|
result_keys += (v.keys rescue [])
end
end
result_keys = result_keys.uniq
self.all_variables = result_keys
formula = text_to_math(self.prediction_formula)
tmp_hidden_variables = text_to_math(self.hidden_variables)
2022-01-22 09:27:15 +00:00
result_keys.each do |k|
formula = formula.gsub(/(\A|[^\w])#{k}($|[^\w])/){|f| "#{$1}result[\"#{k.strip}\"]#{$2}" }
tmp_hidden_variables = tmp_hidden_variables.gsub(/(\A|[^\w])#{k}($|[^\w])/){|f| "#{$1}result[\"#{k.strip}\"]#{$2}" }
2022-01-22 09:27:15 +00:00
end
self.tmp_hidden_variables_for_js = tmp_hidden_variables.rstrip.gsub(/\n\s+/,"\n ").gsub("\n",";\n") + ";"
self.fix_hidden_variables = []
self.tmp_hidden_variables_for_ruby = tmp_hidden_variables.split(/^([^=!]+)=([^=!])/).select{|s| s.present?}.each_slice(2).map do |a,b|
a = a.strip
self.fix_hidden_variables << a
if b
("result[\"#{a}\"]=" + b.gsub("\n",""))
else
a
end
end.join("\n")
self.fix_hidden_variables = self.fix_hidden_variables.uniq
formula = formula.split(/^([^=!]+)=([^=!])/).select{|s| s.present?}.each_slice(2).map do |a,b|
a = a.strip
2022-01-23 06:00:57 +00:00
if b
("result[\"#{a}\"]=" + b.gsub("\n",""))
2022-01-23 06:00:57 +00:00
else
a
2022-01-23 06:00:57 +00:00
end
end.join("\n")
self.fix_hidden_variables.each do |v|
formula = formula.gsub(/(\A|[^\w\"])#{v}($|[^\w])/){|f| "#{$1}result[\"#{v.strip}\"]#{$2}"}
self.tmp_hidden_variables_for_ruby = self.tmp_hidden_variables_for_ruby.gsub(/(\A|[^\w\"])#{v}($|[^\w])/){|f| "#{$1}result[\"#{v.strip}\"]#{$2}"}
end
2022-01-22 09:27:15 +00:00
self.tmp_lpv_ruby_code = formula
formula_variables = formula.enum_for(:scan,/([^\=\(\)]+)?=[^=]/).map {|x| x[-1] }.compact.map{|s| s.strip[8..-3]}
2022-01-23 06:00:57 +00:00
self.tmp_lpv_variables = formula_variables
2022-01-22 09:27:15 +00:00
self.tmp_years_settings = self.years_settings.map do |s|
text_to_math(s)
2022-01-22 09:27:15 +00:00
end
2022-01-23 06:00:57 +00:00
self.tmp_years_settings_for_ruby = self.tmp_years_settings.clone
formula_variables.each do |variable_name|
self.tmp_years_settings_for_ruby = self.tmp_years_settings_for_ruby.map do |y|
y.gsub(variable_name,"result[\"#{variable_name}\"]")
end
end
self.treatment_method = ['Surgery_only']
self.form_show_in_result.values.each do |choice|
variable = choice["variable"]
if variable.present?
self.treatment_method << variable
end
end
tmp_table_translations = {}
tmp_text_translations = {}
@years = self.years
# @head_name = ['Treatment','Additional_Benefit','Overall_Survival']
@head_name = ['Treatment','Overall_Survival']
@therapy_names = self.treatment_method
I18n.available_locales.each do |locale|
I18n.with_locale(locale) do
2022-01-27 09:02:29 +00:00
@table_head = @head_name.map{|name| I18n.t('cancerpredict.table.'+name)}
@therapy_choices = [I18n.t('cancerpredict.table.Surgeryonly')]
2022-01-27 09:02:29 +00:00
self.form_show_in_result.values.each{|choice| @therapy_choices.push choice["name"][locale].to_s}
tmp_table = "<span class=\"result_title print_only\">#{I18n.t("cancerpredict.table.table")}</span><div style=\"clear: both\"></div>"
tmp_table += '<input id="current_year" type="hidden" value="'+@years[0].to_s+'" index="0"/><p id="cancer_table_texts">'+self.table_above_texts[locale].to_s+'</p>'
tmp_table += ('<a style="display: inline-block;">'+(locale.to_s == 'zh_tw' ? '第' : '')+'</a><a style="display: inline-block;">')
@years.each{|year| tmp_table += ('<button class="cancer_years cancer_table_btn btn btn-default btn-sm">'+year.to_s+'</button>')}
tmp_table += ('</a><a style="display: inline-block;">'+(locale == 'zh_tw' ? '年' : '')+'</a>')
tmp_table += '<table><thead><tr>'
@table_head.each_with_index{|head,index| tmp_table += ('<th class="cancer_th '+@head_name[index]+'">' + head + '</th>')}
tmp_table += '</tr></thead><tbody>'
@therapy_choices.each_with_index do |choice,i|
tmp_table += '<tr class="'+@therapy_names[i].to_s+'">'
@table_head.each_with_index do |head,index|
tmp_table += ('<td class="cancer_td '+ @head_name[index]+'">' + ((index == 0) ? (((i==0)? '' : '+') + choice) : '-') + '</td>')
end
tmp_table += '</tr>'
end
tmp_table_translations[locale] = tmp_table
@texts = self.text_above_texts[locale].to_s.gsub('<br/>','</span><br/><span>').gsub('{{Surgery_only}}','<span class="'+@therapy_names[0]+' Overall_Survival"></span>')
@texts = @texts.split('{{years}}')
@texts.delete('')
tmp_text = "<span class=\"result_title print_only\">#{I18n.t("cancerpredict.table.text")}</span><div style=\"clear: both\"></div>"
2022-01-27 09:02:29 +00:00
tmp_text += ('<span>'+@texts[0].to_s)
@years.each{|year| tmp_text += ('<button class="cancer_years cancer_table_btn btn btn-default btn-sm" style="float:none;">'+year.to_s+'</button>')}
if @texts.count > 1
tmp_text += (@texts[1]+'</span>') if @texts.count > 1
else
tmp_text += '</span>'
end
if !self.surgery_only_texts[locale].blank?
@surgery_only_texts = self.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"></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
tmp_text += @surgery_only_texts
2022-01-27 09:02:29 +00:00
tmp_text += '<span class="addition">'+(self.extra_texts[locale].to_s rescue '')+'</span><div class="extra-text" style="display:none;"><div class="texts_show" style="clear:both;"></div></div></p>'
tmp_text_translations[locale] = tmp_text
end
end
self.result_table_translations = tmp_table_translations
self.result_text_translations = tmp_text_translations
self.treatment_method_active_indices = [1]
self.form_show_in_result.each do |num, property|
v = property[:active_choice]
if v.present?
self.treatment_method_active_indices << (v - 1)
else
self.treatment_method_active_indices << 1
2022-01-23 06:00:57 +00:00
end
end
2022-01-22 09:27:15 +00:00
self.lpv_calc = get_years_settings_dict
2022-01-23 06:00:57 +00:00
self.generate_eval_formula
end
def generate_eval_formula
eval_hidden_variables = "def eval_hidden_variables(result); #{self.tmp_hidden_variables_for_ruby}; end"
CancerpredictsController.module_eval(eval_hidden_variables)
2022-01-23 06:00:57 +00:00
eval_formula = "def eval_formula(result); #{self.tmp_lpv_ruby_code}; end"
CancerpredictsController.module_eval(eval_formula)
2022-01-22 09:27:15 +00:00
end
def generate_jscode
js_code = "var map_values , mapping_hash , temp_index ,temp_value , index , closest_value;\r\n"
mapping_data_from_csv = JSON.parse(self.mapping_data_from_csv) rescue {}
tmp_hash = self.form_show.values + self.form_show_in_result.values
tmp_hash.each do |property|
2022-01-22 09:27:15 +00:00
@variable = property[:variable]
if @variable.present?
if property[:is_num] == 1
js_code += " result['#{@variable}'] = Number(result_json['#{@variable}']);\r\n"
2022-01-22 09:27:15 +00:00
elsif property[:choice_fields].present?
if !(self.advance_mode)
js_code += " result['#{@variable}'] = Number(result_json['#{@variable}']);\r\n"
2022-01-22 09:27:15 +00:00
else
if property[:need_map_values] == 1
js_code += " map_values = #{property[:map_values]};\r\n"
js_code += " result['#{@variable}'] = map_values[Number(result_json['#{@variable}'']) - 1];\r\n"
2022-01-22 09:27:15 +00:00
else
if property[:revert_value] != 1
js_code += " result['#{@variable}'] = Number(result_json['#{@variable}']) - 1;\r\n"
else
js_code += " result['#{@variable}'] = (#{property[:choice_fields].length} - Number(result_json['#{@variable}']));\r\n"
end
2022-01-22 09:27:15 +00:00
end
end
end
if self.advance_mode && property[:cancer_predict_mapping_file].present?
2022-01-22 09:27:15 +00:00
if (mapping_data_from_csv != {} && !mapping_data_from_csv[@variable].blank?)
js_code += " mapping_hash = mapping_data_from_csv['#{@variable}'];\r\n"
js_code += " temp_index = 0;\r\n"
js_code += " temp_value = result['#{@variable}'];\r\n"
js_code += " index = 0;
2022-01-22 09:27:15 +00:00
$.each(mapping_hash,function(k,v){
if( index == 0 ){
2022-01-22 09:27:15 +00:00
var index_val = v.indexOf(temp_value);
if( index_val != -1 ){
temp_index = index_val;
}else{
closest_value = v.get_nearest_value(temp_value);
temp_index = v.indexOf(closest_value)
}
}
result[k] = v[temp_index];
index++;
});\r\n"
end
end
end
end
js_code += "\n Object.keys(result).forEach(function(k){
if(Number.isNaN(result[k])){
result[k] = 0;
}
})"
js_code += "\n #{self.tmp_hidden_variables_for_js}"
formula = text_to_math(self.prediction_formula)
self.all_variables.each do |k|
formula = formula.gsub(/#{k}?(-|\+|\*|\s|\=)/){ "result[\"#{k.strip}\"]#{$1}" }
2022-01-22 09:27:15 +00:00
end
formula_variables = self.tmp_lpv_variables.map{|v| v}
js_code = "\n function calculate_first_lpv(result_json){
2022-01-22 09:27:15 +00:00
result = {};
#{js_code}
try{
#{formula.gsub(/\s{2,10}/," ").gsub("\n","\n ")}
}catch(e){console.log(e)};
result['lpv_variable'] = {};
#{formula_variables.map{|v| "result['lpv_variable']['#{v}'] = #{v};"}.join("\n ") }
result['lpv'] = #{formula_variables.count == 0 ? 0 : formula_variables.last};
result['lpv_variable']['lpv'] = result['lpv'];
2022-01-22 09:27:15 +00:00
return result;
};
function calculate_and_change_result_value(obj){
obj.servive_ratio_arr = [];
for(var i = 0;i<obj.active_treatment.length;i++){
var servive_ratio = round((1 - calculate_servive_ratio(obj.year,obj.lpv_real[i]))*100,2);
2022-01-22 09:27:15 +00:00
var benefit = servive_ratio - obj.servive_ratio_arr[obj.servive_ratio_arr.length-1];
obj.servive_ratio_arr.push(servive_ratio);
$('tr.'+obj.active_treatment[i]+' td.Overall_Survival').html(servive_ratio+'%');
$('.'+obj.active_treatment[i]+'.Overall_Survival').html(Math.round(servive_ratio));
2022-01-22 09:27:15 +00:00
if(i != 0){
$('tr.'+obj.active_treatment[i]+' td.Additional_Benefit').html(round(benefit,2)+'%');
$('.'+obj.active_treatment[i]+'.Additional_Benefit').html(Math.round(benefit));
}
}
//$('.'+obj.active_treatment[0]+'.Overall_Survival').html(Math.round(obj.servive_ratio_arr[0]));
};"
@years = self.years
switch_texts = "
2022-01-23 08:58:03 +00:00
#{formula_variables.map{|v| "var #{v} = obj['#{v}'];"}.join("\n ")}
switch(year) {"
2022-01-22 09:27:15 +00:00
@years.each do |year|
year_index = @years.index(year)
switch_texts +=
"
2022-01-23 08:58:03 +00:00
case '#{year}':
servive_ratio = #{text_to_math(self.years_settings[year_index])};
2022-01-23 08:58:03 +00:00
break;"
2022-01-22 09:27:15 +00:00
end
switch_texts += "
2022-01-23 08:58:03 +00:00
default:
console.log('not found year.');
}"
2022-01-22 09:27:15 +00:00
js_code = js_code +"
function calculate_servive_ratio(year,obj){
2022-01-23 08:58:03 +00:00
var servive_ratio;#{switch_texts}
2022-01-22 09:27:15 +00:00
return servive_ratio;
}
"
return js_code
end
def text_to_math(text)
text.gsub("\r\n","\n").gsub('^','**').gsub('exp','Math.exp').gsub('log','Math.log')
end
def replace_str_with_idx(org_str,st, ed, replace_str)
org_str.slice!(st, ed - st + 1)
org_str.insert(st, replace_str)
org_str
end
2022-01-22 09:27:15 +00:00
def auto_write_predict_js
js_codes = generate_jscode
module_app_path = Pathname.new(File.expand_path(__dir__)).dirname.dirname.to_s
save_path = module_app_path + '/app/assets/javascripts/cancer_predict.js'
file_texts = File.read(save_path)
need_write = false
2022-01-22 09:27:15 +00:00
str1 = "/* auto add start */"
index1 = file_texts.index(str1)
str2 = "/* auto add end */"
index2 = file_texts.index(str2)
if (!index1.nil? && !index2.nil?)
file_texts = replace_str_with_idx(file_texts, index1 + str1.length, index2 - 1, js_codes)
need_write = true
end
2022-01-22 09:27:15 +00:00
str3 = "/*lpv_calc_formula_start*/"
index3 = file_texts.index(str3)
str4 = "/*lpv_calc_formula_end*/"
index4 = file_texts.index(str4)
if (!index3.nil? && !index4.nil?)
file_texts = replace_str_with_idx(file_texts, index3 + str3.length, index4 - 1, self.lpv_calc.to_json.gsub("@",".") + ';')
need_write = true
end
tmp_disable_jscodes = ""
str5 = "/*disable_condition start*/"
index5 = file_texts.index(str5)
str6 = "/*disable_condition end*/"
index6 = file_texts.index(str6)
self.form_show_in_result.each do |num,property|
if property[:disable_condition].present?
tmp = property[:disable_condition].clone
self.all_variables.each do |k|
tmp = tmp.gsub(/(\A|[^\w])#{k}($|[^\w])/){|f| "#{$1}post_json[\"#{k}\"]#{$2}" }
end
variable = property[:variable]
tmp_disable_jscodes += "\n if(#{tmp}){
$('##{variable} .cancer_table_btn').attr('disabled','disabled');
$('[for=\"#{variable}\"]').css('color','rgb(204, 204, 204)');
}else{
$('##{variable} .cancer_table_btn').removeAttr('disabled');
$('[for=\"#{variable}\"]').css('color','');
}"
end
end
if (!index5.nil? && !index6.nil?)
tmp_disable_jscodes += "\n\t\t\t"
file_texts = replace_str_with_idx(file_texts, index5 + str5.length, index6 - 1, tmp_disable_jscodes)
need_write = true
end
str7 = "/*therapy_lpv start*/"
index7 = file_texts.index(str7)
str8 = "/*therapy_lpv end*/"
index8 = file_texts.index(str8)
self.therapy_lpv = [0]
self.form_show_in_result.each do |num,property|
if property[:lpv_impact].present?
self.therapy_lpv << property[:lpv_impact]
else
self.therapy_lpv << 0
end
end
if (!index7.nil? && !index8.nil?)
file_texts = replace_str_with_idx(file_texts, index7 + str7.length, index8 - 1, self.therapy_lpv.to_s + ';')
need_write = true
end
if need_write
2022-01-22 09:27:15 +00:00
File.write(save_path,file_texts)
end
end
def get_years_settings_dict
lpv_variable_name = (prediction_formula.include?("=") ? prediction_formula.split("=")[0].strip : "" rescue "")
res = self.years.map.with_index do |y, i|
tmp_formula = self.tmp_years_settings[i]
if lpv_variable_name.present?
tmp_formula = tmp_formula.gsub(lpv_variable_name, "lpv_current")
else
tmp_formula = tmp_formula.gsub("lpv", "lpv_current")
end
[y.to_s.sub(".","@"), tmp_formula]
end
res.to_h
end
2019-11-15 00:44:28 +00:00
end