1313// [CLASS] DataSets
1414//============================================================================
1515define ( [
16+ 'text!vp_base/html/m_ml/dataSets.html!strip' ,
1617 'vp_base/js/com/com_util' ,
1718 'vp_base/js/com/com_Const' ,
1819 'vp_base/js/com/com_String' ,
1920 'vp_base/js/com/component/PopupComponent' ,
20- ] , function ( com_util , com_Const , com_String , PopupComponent ) {
21+ 'vp_base/js/com/com_generatorV2' ,
22+ 'vp_base/data/m_ml/mlLibrary' ,
23+ ] , function ( dsHTML , com_util , com_Const , com_String , PopupComponent , com_generator , ML_LIBRARIES ) {
2124
2225 /**
2326 * DataSets
@@ -29,15 +32,126 @@ define([
2932 this . config . dataview = false ;
3033
3134 this . state = {
32-
35+ loadType : 'load_boston' ,
36+ userOption : '' ,
37+ allocateTo : 'ldata' ,
3338 ...this . state
3439 }
40+
41+ this . mlConfig = ML_LIBRARIES ;
42+ this . loadTypeList = {
43+ 'Load Data' : [
44+ 'load_boston' , 'load_iris' , 'load_diabetes' , 'load_digits' , 'load_linnerud' , 'load_wine' , 'load_breast_cancer'
45+ ] ,
46+ 'Create Data' : [
47+ 'make_classification' , 'make_blobs' , 'make_circles' , 'make_moons'
48+ ]
49+ }
50+
51+ }
52+
53+ _bindEvent ( ) {
54+ super . _bindEvent ( ) ;
55+ let that = this ;
56+
57+ // select model
58+ $ ( this . wrapSelector ( '#loadType' ) ) . on ( 'change' , function ( ) {
59+ let loadType = $ ( this ) . val ( ) ;
60+ that . state . loadType = loadType ;
61+ $ ( that . wrapSelector ( '.vp-data-option-box' ) ) . html ( that . templateForOption ( loadType ) ) ;
62+
63+ // change allocateTo default variable name
64+ if ( that . loadTypeList [ 'Load Data' ] . includes ( loadType ) ) {
65+ $ ( that . wrapSelector ( '#allocateTo' ) ) . val ( 'ldata' ) ;
66+ that . state . allocateTo = 'ldata' ;
67+ } else {
68+ $ ( that . wrapSelector ( '#allocateTo' ) ) . val ( 'df' ) ;
69+ that . state . allocateTo = 'df' ;
70+ }
71+ } ) ;
3572 }
3673
3774 templateForBody ( ) {
38- return 'Data Set test' ;
75+ let page = $ ( dsHTML ) ;
76+
77+ let that = this ;
78+ // load types
79+ let loadTypeTag = new com_String ( ) ;
80+ Object . keys ( this . loadTypeList ) . forEach ( category => {
81+ let optionTag = new com_String ( ) ;
82+ that . loadTypeList [ category ] . forEach ( opt => {
83+ let optConfig = that . mlConfig [ opt ] ;
84+ let selectedFlag = '' ;
85+ if ( opt == that . state . modelType ) {
86+ selectedFlag = 'selected' ;
87+ }
88+ optionTag . appendFormatLine ( '<option value="{0}" {1}>{2}</option>' ,
89+ opt , selectedFlag , optConfig . name ) ;
90+ } )
91+ loadTypeTag . appendFormatLine ( '<optgroup label="{0}">{1}</optgroup>' ,
92+ category , optionTag . toString ( ) ) ;
93+ } ) ;
94+ $ ( page ) . find ( '#loadType' ) . html ( loadTypeTag . toString ( ) ) ;
95+
96+ // render option page
97+ $ ( page ) . find ( '.vp-data-option-box' ) . html ( this . templateForOption ( this . state . loadType ) ) ;
98+
99+ return page ;
39100 }
40101
102+ templateForOption ( loadType ) {
103+ let config = this . mlConfig [ loadType ] ;
104+ let state = this . state ;
105+
106+ let optBox = new com_String ( ) ;
107+ // render tag
108+ config . options . forEach ( opt => {
109+ optBox . appendFormatLine ( '<label for="{0}" title="{1}">{2}</label>'
110+ , opt . name , opt . name , opt . name ) ;
111+ let content = com_generator . renderContent ( this , opt . component [ 0 ] , opt , state ) ;
112+ optBox . appendLine ( content [ 0 ] . outerHTML ) ;
113+ } ) ;
114+
115+ // show user option
116+ if ( config . code . includes ( '${etc}' ) ) {
117+ // render user option
118+ optBox . appendFormatLine ( '<label for="{0}">{1}</label>' , 'userOption' , 'User option' ) ;
119+ optBox . appendFormatLine ( '<input type="text" class="vp-input vp-state" id="{0}" placeholder="{1}" value="{2}"/>' ,
120+ 'userOption' , 'key=value, ...' , this . state . userOption ) ;
121+ }
122+ return optBox . toString ( ) ;
123+ }
124+
125+ generateCode ( ) {
126+ let { loadType, userOption, allocateTo } = this . state ;
127+ let code = new com_String ( ) ;
128+ let config = this . mlConfig [ loadType ] ;
129+ code . appendLine ( config . import ) ;
130+ code . appendLine ( ) ;
131+
132+ // model code
133+ let modelCode = config . code ;
134+ modelCode = com_generator . vp_codeGenerator ( this , config , this . state , userOption ) ;
135+
136+ if ( this . loadTypeList [ 'Load Data' ] . includes ( loadType ) ) {
137+ code . appendFormatLine ( '{0} = {1}' , allocateTo , modelCode ) ;
138+ // FIXME: decide between 2 codes
139+ // code.appendFormat("df_{0} = pd.concat([pd.DataFrame({1}.data, columns={2}.feature_names), pd.DataFrame({3}.target, columns=['target'])], axis=1)", allocateTo, allocateTo, allocateTo, allocateTo);
140+ code . appendFormat ( "df_{0} = pd.DataFrame(np.hstack(({1}.data, {2}.target.reshape(-1,1))), columns=np.hstack(({3}.feature_names, ['target'])))" , allocateTo , allocateTo , allocateTo , allocateTo ) ;
141+ } else {
142+ code . appendFormatLine ( "_X, _y = {0}" , modelCode ) ;
143+ code . appendLine ( "_columns = np.hstack((['X{}'.format(i+1) for i in range(len(_X[0]))],['target']))" ) ;
144+ code . appendFormat ( "{0} = pd.DataFrame(np.hstack((_X, _y.reshape(-1,1))), columns=_columns)" , allocateTo ) ;
145+ }
146+
147+ if ( allocateTo != '' ) {
148+ code . appendLine ( ) ;
149+ code . append ( allocateTo ) ;
150+ }
151+
152+
153+ return code . toString ( ) ;
154+ }
41155 }
42156
43157 return DataSets ;
0 commit comments