module.py
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 ini={}
00012 cm_kernel=None
00013
00014
00015 import os
00016 import csv
00017 import math
00018
00019
00020 def init(i):
00021 return {'cm_return':0}
00022
00023
00024 def build(i):
00025
00026 """
00027 Build model
00028
00029 Input: {
00030 data
00031 ct_dimensions_input
00032 ct_dimensions_output
00033 desc
00034 model_name - (earth, svm)
00035 (record_data_to_file_prefix) - if !='', use this filename prefix instead of randomly generated
00036 }
00037
00038 Output: {
00039 cm_return - return code >0 if error
00040 file_with_model - file with model
00041 }
00042
00043 """
00044
00045 model_name=i.get('model_name','')
00046 if model_name=='':
00047 return {'cm_return':1, 'cm_error':'model name is not specified'}
00048
00049
00050 model_code=ini['cfg']['model_code'].replace(cm_kernel.convert_str_to_special('model_name'), model_name)
00051
00052 cm_kernel.print_for_con('')
00053 cm_kernel.print_for_con('Loading module code ...')
00054
00055 r=cm_kernel.load_code({'cm_path':ini['path'], 'cm_code_name':model_code})
00056 if r['cm_return']>0: return r
00057
00058 code=r['code']
00059 a=getattr(code, 'build')
00060 return a(i)
00061
00062
00063 def predict(i):
00064
00065 """
00066 Predict using model
00067
00068 Input: {
00069 model_file
00070 data
00071 ct_dimensions_input
00072 (ct_dimensions_output) - for comparison
00073 desc - cM data description
00074 model_name - (earth, svm)
00075 (max_variation_percent) - for comparison, report points where variation is more than this number (default=0.2)
00076 }
00077
00078 Output: {
00079 cm_return - return code >0 if error
00080 (rmse) - if comparison, root mean square error for predictions vs original
00081 (max_var) - list of points with variation more than max_variation_percent
00082 }
00083
00084 """
00085
00086 model_name=i.get('model_name','')
00087 if model_name=='':
00088 return {'cm_return':1, 'cm_error':'model name is not specified'}
00089
00090 data=i.get('data',{})
00091 desc=i.get('desc',{})
00092
00093
00094 model_code=ini['cfg']['model_code'].replace(cm_kernel.convert_str_to_special('model_name'), model_name)
00095
00096 cm_kernel.print_for_con('')
00097 cm_kernel.print_for_con('Loading module code ...')
00098
00099 r=cm_kernel.load_code({'cm_path':ini['path'], 'cm_code_name':model_code})
00100 if r['cm_return']>0: return r
00101
00102 code=r['code']
00103 a=getattr(code, 'predict')
00104 rr=a(i)
00105 if rr['cm_return']>0: return rr
00106
00107
00108 fx=i.get('ct_output_file_s_json','')
00109 if fx!='':
00110 cm_kernel.print_for_con('')
00111 cm_kernel.print_for_con('Recording to json file '+fx+' ...')
00112
00113 r=cm_kernel.save_array_to_file_as_json({'cm_filename':fx, 'cm_array':{'data':data, 'desc':desc}})
00114 if r['cm_return']>0: return r
00115
00116 return rr