Welcome    Usage    Browse    Find CID    Search     Log in

cM API Documentation

module.py

Go to the documentation of this file.
00001 #
00002 # Collective Mind
00003 #
00004 # See cM LICENSE.txt for licensing details.
00005 # See cM Copyright.txt for copyright details.
00006 #
00007 # Developer(s): (C) Grigori Fursin, started on 2011.09
00008 #
00009 
00010 # Should always be here
00011 ini={}
00012 cm_kernel=None
00013 
00014 # Local settings
00015 import json
00016 import os
00017 import copy
00018 
00019 # ============================================================================
00020 def init(i):
00021     return {'cm_return':0}
00022 
00023 # ============================================================================
00024 def visualize(i):
00025 
00026     """
00027     Plotting graphs through web
00028 
00029     TBD: FGG: this function has to be fully rewritten using standard cM
00030               data description modules (it was the first prototype with
00031               old cM)
00032 
00033     Input:  {
00034               TODO: describe all variables
00035             }
00036 
00037     Output: {
00038               cm_return  - return code >0 if error
00039             }
00040     """
00041 
00042     # Check styles, etc
00043     if 'cfg' in cm_kernel.ini['web_style']: web=cm_kernel.ini['web_style']['cfg']
00044     else:
00045        return {'cm_return':1, 'cm_error':'web style is not defined'}
00046 
00047     # Clean params
00048     if 'ct_repo_uoa' in i and i['ct_repo_uoa']=='': del(i['ct_repo_uoa'])
00049 
00050     # Check reset
00051     if 'cm_button_reset' in i:
00052        if 'ct_repo_uoa' in i: del(i['ct_repo_uoa'])
00053        if 'ct_data_selection' in i: del(i['ct_data_selection'])
00054 
00055     # Check multiple graphs from experiment table
00056 
00057     for q in range(1, 1000):
00058         yy='use_multiple_graphs_'+str(q)
00059         yy1='use_multiple_graphs_uid_'+str(q)
00060 
00061         if i.get(yy,'')=='on':
00062            if 'ct_data_selection' not in i:
00063               i['ct_data_selection']=[]
00064            if i.get(yy1, '')!='':
00065               i['ct_data_selection'].append(i[yy1])
00066 
00067     # Make selector
00068     enough=False
00069     cm_kernel.print_for_con('<small><B>Selector</B></small><BR>')
00070     cm_kernel.print_for_web(web['table_init']+'<TR><TD align="center">')
00071     
00072     cm_kernel.print_for_con('<form action="" method="post" name="prepare_graph">')
00073 
00074     cm_kernel.print_for_con('<TABLE BORDER="0"><TR><TD valign="TOP">')
00075 
00076     cm_kernel.print_for_web(web['table_init'])
00077 
00078     # ************************** Select repo **************************
00079     ii={'cm_run_module_uoa':ini['cfg']['cm_modules']['cm-web'],
00080         'cm_action':'convert_field_to_html',
00081         'cm_value':'',
00082         'cm_key':'ct_repo_uoa',
00083         'cm_mode':'update',
00084         'cm_submit_on_change':'yes',
00085         'cm_web_form':'prepare_graph',
00086         'cm_html_width':'250',
00087         'hide_add_new_object':'yes',
00088         'cm_data_desc':{"type":"uoa", "cm_module_uoa":ini['cfg']['cm_modules']['cm-repo']}}
00089     if 'ct_repo_uoa' in i: ii['cm_value']=i['ct_repo_uoa']
00090     r1=cm_kernel.access(ii)
00091     if r1['cm_return']>0: return r1
00092 
00093     cm_kernel.print_for_con('<TR><TD><small><B>Repository:</B></small></TD>')
00094     cm_kernel.print_for_web('<TD>'+r1['cm_value_html']+'</TD></TR>')
00095 
00096     # ************************** Select module **************************
00097     ct_module_uoa=''
00098     if 'ct_module_uoa' in i: ct_module_uoa=i['ct_module_uoa']
00099     else: ct_module_uoa=ini['cm_module_uid']
00100 
00101     ii={'cm_run_module_uoa':ini['cfg']['cm_modules']['cm-web'],
00102         'cm_action':'convert_field_to_html',
00103         'cm_value':'',
00104         'cm_key':'ct_module_uoa',
00105         'cm_mode':'update',
00106         'cm_submit_on_change':'yes',
00107         'cm_web_form':'prepare_graph',
00108         'cm_html_width':'250',
00109         'hide_add_new_object':'yes',
00110         'cm_data_desc':{"type":"uoa", 
00111                         'cm_classes_uoa':['a42b93dfd4328bfd'], 
00112                         "cm_module_uoa":ini['cfg']['cm_modules']['cm-module']},
00113         'cm_value': ct_module_uoa}
00114     r1=cm_kernel.access(ii)
00115     if r1['cm_return']>0: return r1
00116 
00117     cm_kernel.print_for_con('<TR><TD><small><B>Module:</B></small></TD>')
00118     cm_kernel.print_for_web('<TD>'+r1['cm_value_html']+'</TD></TR>')
00119 
00120     # ************************** Select multiple graphs **************************
00121 
00122     x=''
00123     if i.get('use_multiple_graphs','')=='on': x='checked'
00124     cm_kernel.print_for_con('<TR><TD><small><B>Use multiple graphs:</B></small></TD>')
00125     cm_kernel.print_for_web('<TD><input type="checkbox" name="use_multiple_graphs" '+x+' onchange="document.prepare_graph.submit();"></TD></TR>')
00126 
00127     # ************************** Reverse order of graphs **************************
00128 
00129     if i.get('use_multiple_graphs','')=='on':
00130        x=''
00131        if i.get('reverse_order_of_graphs','')=='on': x='checked'
00132        cm_kernel.print_for_con('<TR><TD><small><B>Reverse order of graphs:</B></small></TD>')
00133        cm_kernel.print_for_web('<TD><input type="checkbox" name="reverse_order_of_graphs" '+x+' onchange="document.prepare_graph.submit();"></TD></TR>')
00134 
00135     cm_kernel.print_for_con('</TD></TR></TABLE>')
00136 
00137     cm_kernel.print_for_web(web['table_init'])
00138 
00139     cm_kernel.print_for_con('<TR><TD align="left">')
00140 
00141     # ************************** Select data **************************
00142     cm_kernel.print_for_con('<small><B>Points in space:</B></small><BR>')
00143     cm_kernel.print_for_con('<select class="cm-select" size="8" onchange="document.prepare_graph.submit();" name="ct_data_selection[]" multiple style="width:360px">')
00144 
00145     ii={}
00146     ii['cm_run_module_uoa']=ct_module_uoa
00147     if 'ct_repo_uoa' in i and i['ct_repo_uoa']!='-': ii['cm_repo_uoa']=i['ct_repo_uoa']
00148     ii['cm_action']='list'
00149     r=cm_kernel.access(ii)
00150     if r['cm_return']>0: return r
00151 
00152     found=False
00153     for xx in sorted(r['cm_mixed'], key=lambda k: k['cm_uoa']):
00154         x=xx['cm_uoa'];  xu=xx['cm_uid'];  xa=xx['cm_alias']
00155         z=''
00156         if 'ct_data_selection' in i and (x in i['ct_data_selection'] or xu in i['ct_data_selection'] or xa in i['ct_data_selection']): 
00157            z=' SELECTED '
00158            found=True
00159         cm_kernel.print_for_web('<option value="'+xu+'"'+z+'>'+x+'</option>')
00160     if 'ct_data_selection' in i and not found: del(i['ct_data_selection'])
00161     cm_kernel.print_for_con('</select><br>')
00162     
00163     cm_kernel.print_for_con('</TD><TD valign="TOP">')
00164 
00165     cm_kernel.print_for_con('</TD></TR></TABLE>')
00166 
00167     cm_kernel.print_for_con('</TD><TD align="left" valign="top">')
00168 
00169     cm_kernel.print_for_web(web['table_init'])
00170 
00171     # ************************** Make graph choice **************************
00172     if 'available_graph_engines' not in ini['cfg']:
00173        return {'cm_return':1, 'cm_error':'no graph engines configured'}
00174     age=ini['cfg']['available_graph_engines']
00175     choice=[]
00176     for x in age: choice.append(x['cm_index'])
00177 
00178     if 'ct_graph_type' not in i: i['ct_graph_type']=choice[0]
00179     ii={'cm_run_module_uoa':ini['cfg']['cm_modules']['cm-web'],
00180         'cm_action':'convert_field_to_html',
00181         'cm_value':i['ct_graph_type'],
00182         'cm_key':'ct_graph_type',
00183         'cm_mode':'update',
00184         'cm_submit_on_change':'yes',
00185         'cm_web_form':'prepare_graph',
00186         'cm_data_desc':{"type":"text", "has_choice":"yes", "choice":choice}}
00187     r1=cm_kernel.access(ii)
00188     if r1['cm_return']>0: return r1
00189 
00190     cm_kernel.print_for_con('<TR><TD align="left"><small><B>Graph engine:</B></small></TD>')
00191     cm_kernel.print_for_web('<TD>'+r1['cm_value_html']+'</TD></TR>')
00192 
00193     # Check graph parameters
00194     min_axes=0
00195     graph_type=''
00196     has_sorting=''
00197     if 'ct_graph_type' in i and i['ct_graph_type']!='':
00198        # Check how many axis need this graph:
00199        r=cm_kernel.find_index_in_array({'cm_array':age, 'cm_index':i['ct_graph_type']})
00200        if r['cm_return']==0 and 'min_axes' in r['cm_array']: 
00201           min_axes=r['cm_array']['min_axes']
00202           graph_type=r['cm_array'].get('graph_type','')
00203           has_sorting=r['cm_array'].get('has_sorting','')
00204 
00205     # ************************** Make selections depending on the graph **************************
00206     keys=[]
00207     keys.append('')
00208     keys_desc={}
00209     if 'ct_data_selection' in i and len(i['ct_data_selection'])>0:
00210        # Try to load one of the data
00211        ii={}
00212        ii['cm_run_module_uoa']=ct_module_uoa
00213        if 'ct_repo_uoa' in i and i['ct_repo_uoa']!='-': ii['cm_repo_uoa']=i['ct_repo_uoa']
00214        ii['cm_data_uoa']=i['ct_data_selection'][0]
00215        ii['cm_action']='load'
00216        ii['cm_console']='no'
00217        r=cm_kernel.access(ii)
00218        if r['cm_return']>0: return r
00219        d1=r['cm_data_obj']['cfg']
00220 
00221        d=d1
00222        
00223        if i.get('use_raw_dimensions', '')!='on' and \
00224           d1.get('pipeline_module_uoa','')!='' and \
00225           len(d1.get('points',[]))>0:
00226 
00227           d=d1['points'][0]
00228           p=d1['pipeline_module_uoa']
00229 
00230           # Load pipeline module
00231           r=cm_kernel.access({'cm_run_module_uoa':ini['cfg']['cm_modules']['cm-module'],
00232                               'cm_action':'load',
00233                               'cm_data_uoa':p})
00234           if r['cm_return']>0: return r
00235           p_cfg=r['cm_data_obj']['cfg']
00236 
00237           # Get keys!
00238           keys_desc={}
00239           pd=p_cfg.get('pipeline_desc',{})
00240 
00241           # Prepare description by pipeline and input !!!
00242           ii={'cm_run_module_uoa':ini['cfg']['cm_modules']['cm-web'],
00243               'cm_action':'visualize_data',
00244               'cm_array':d.get('pipeline_input',{}).get('state',{}),
00245               'cm_data_desc':pd,
00246               'cm_separator':'#',
00247               'cm_separator_form':'#form1#',
00248               'cm_forms_exists':'yes',
00249               'cm_mode':'add'}
00250           r=cm_kernel.access(ii)
00251           if r['cm_return']>0: return r
00252 
00253           # HERE pd (description) will be updated according to the input!!!
00254           # We will pick up various params from programs, etc ...
00255 
00256           for q in pd:
00257               q1=q[1:]
00258               q2='##pipeline_output#state'+q1
00259               t=pd[q].get('type','')
00260               if pd[q].get('desc_text','')!='' and t!='dict' and t!='list':
00261                  keys_desc[q2]=pd[q]
00262                  keys.append(q2)
00263 
00264        d5=d
00265        dx=d.get('points',[])
00266 
00267        for dx5 in dx:
00268            r=cm_kernel.flatten_array({'cm_array':dx5})
00269            if r['cm_return']==0:
00270               for q in sorted(r['cm_array']):
00271                   if q not in keys: keys.append(q)
00272 
00273        keys=sorted(keys)
00274 
00275        if len(keys)>1:
00276           # For now for any graph
00277           if min_axes>0:
00278 
00279              x=''
00280              if i.get('use_raw_dimensions','')=='on': x='checked'
00281              cm_kernel.print_for_con('<TR><TD><B><small>Use raw dimensions:</small></B></TD>')
00282              cm_kernel.print_for_web('<TD><input type="checkbox" name="use_raw_dimensions" '+x+' onchange="document.prepare_graph.submit();"></TD></TR>')
00283 
00284              x=''
00285              if i.get('convert_text_axes_to_float','')=='on': x='checked'
00286              cm_kernel.print_for_con('<TR><TD><B><small>Convert text axes to float:</small></B></TD>')
00287              cm_kernel.print_for_web('<TD><input type="checkbox" name="convert_text_axes_to_float" '+x+' onchange="document.prepare_graph.submit();"></TD></TR>')
00288 
00289           # ************************** Axes **************************
00290           cm_kernel.print_for_con('</TABLE>')
00291 
00292           cm_kernel.print_for_web(web['table_init'])
00293 
00294           for q in range(0, min_axes):
00295 
00296               cm_kernel.print_for_con('<TR>')
00297       
00298               # Prepare axes name
00299               if q==0:   v1='X'; v2='ct_axis_x'
00300               elif q==1: v1='Y'; v2='ct_axis_y'
00301               elif q==2: v1='Z'; v2='ct_axis_z'
00302 
00303               v3=''
00304               if v2 in i: v3=i[v2]
00305 
00306               ii={'cm_run_module_uoa':ini['cfg']['cm_modules']['cm-web'],
00307                   'cm_action':'convert_field_to_html',
00308                   'cm_key':v2,
00309                   'cm_mode':'update',
00310                   'cm_submit_on_change':'yes',
00311                   'cm_web_form':'prepare_graph',
00312                   'cm_value':v3,
00313                   'cm_data_desc':{"type":"text", "has_choice":"yes", "choice":keys, "choice_desc":keys_desc}}
00314               if i.get('use_raw_dimensions', '')=='on': ii['cm_data_desc']['no_sorting']='yes'
00315               else: ii['cm_data_desc']['sort_by_desc']='yes'
00316 
00317               r1=cm_kernel.access(ii)
00318               if r1['cm_return']>0: return r1
00319 
00320               cm_kernel.print_for_web('<TD><small><B>'+v1+' axis flattened key:</B></small><BR>')
00321               cm_kernel.print_for_web(r1['cm_value_html']+'</TD>')
00322 
00323               # Prepare axes type
00324               v2x=v2+'_type'
00325               v4=''
00326               v5='no'
00327 
00328               if v3 in keys_desc and 'type' in keys_desc[v3]:  
00329                  v4=keys_desc[v3]['type']
00330                  v5='yes'
00331               elif v2x in i: v4=i[v2x]
00332 
00333               if v4=='': v4='float'; i[v2x]=v4
00334 
00335               ii={'cm_run_module_uoa':ini['cfg']['cm_modules']['cm-web'],
00336                   'cm_action':'convert_field_to_html',
00337                   'cm_key':v2x,
00338                   'cm_mode':'update',
00339                   'cm_submit_on_change':'yes',
00340                   'cm_web_form':'prepare_graph',
00341                   'cm_value':v4,
00342                   'cm_disabled':v5,
00343                   'cm_data_desc':{"type":"text", "has_choice":"yes", "choice":['float', 'integer', 'uoa', 'text'], "field_width":"100"}}
00344               if i.get('use_raw_dimensions', '')=='on': ii['cm_data_desc']['no_sorting']='yes'
00345               else: ii['cm_data_desc']['sort_by_desc']='yes'
00346 
00347               r1=cm_kernel.access(ii)
00348               if r1['cm_return']>0: return r1
00349 
00350               cm_kernel.print_for_web('<TD><small><B>'+v1+' axis type:</B></small><BR>')
00351               cm_kernel.print_for_web(r1['cm_value_html']+'</TD>')
00352 
00353               if has_sorting=='yes':
00354                  # Prepare axes sort
00355                  v2x=v2+'_sort'
00356                  v4=i.get(v2x,'')
00357 
00358                  ii={'cm_run_module_uoa':ini['cfg']['cm_modules']['cm-web'],
00359                      'cm_action':'convert_field_to_html',
00360                      'cm_key':v2x,
00361                      'cm_mode':'update',
00362                      'cm_submit_on_change':'yes',
00363                      'cm_web_form':'prepare_graph',
00364                      'cm_value':v4,
00365                      'cm_data_desc':{"type":"text", "has_choice":"yes", "choice":['', 'ascending', 'descending'], "field_width":"100"}}
00366                  if i.get('use_raw_dimensions', '')=='on': ii['cm_data_desc']['no_sorting']='yes'
00367                  else: ii['cm_data_desc']['sort_by_desc']='yes'
00368 
00369                  r1=cm_kernel.access(ii)
00370                  if r1['cm_return']>0: return r1
00371 
00372                  cm_kernel.print_for_web('<TD><small><B>'+v1+' axis sort:</B></small><BR>')
00373                  cm_kernel.print_for_web(r1['cm_value_html']+'</TD>')
00374 
00375               # Prepare axes name
00376               v2x=v2+'_name'
00377               v4=i.get(v2x,'')
00378 
00379               ii={'cm_run_module_uoa':ini['cfg']['cm_modules']['cm-web'],
00380                   'cm_action':'convert_field_to_html',
00381                   'cm_key':v2x,
00382                   'cm_mode':'update',
00383                   'cm_submit_on_change':'yes',
00384                   'cm_web_form':'prepare_graph',
00385                   'cm_value':v4,
00386                   'cm_data_desc':{"type":"text", "field_width":"100"}}
00387               if i.get('use_raw_dimensions', '')=='on': ii['cm_data_desc']['no_sorting']='yes'
00388               else: ii['cm_data_desc']['sort_by_desc']='yes'
00389 
00390               r1=cm_kernel.access(ii)
00391               if r1['cm_return']>0: return r1
00392 
00393               cm_kernel.print_for_web('<TD><small><B>'+v1+' axis name:</B></small><BR>')
00394               cm_kernel.print_for_web(r1['cm_value_html']+'</TD>')
00395 
00396               cm_kernel.print_for_con('</TR>')
00397 
00398           if has_sorting=='yes':
00399              cm_kernel.print_for_con('<TR><TD></TD><TD></TD><TD>')
00400              v=i.get('data_entry_to_sort','1')
00401              ii={'cm_run_module_uoa':ini['cfg']['cm_modules']['cm-web'],
00402                  'cm_action':'convert_field_to_html',
00403                  'cm_key':'data_entry_to_sort',
00404                  'cm_mode':'update',
00405                  'cm_submit_on_change':'yes',
00406                  'cm_web_form':'prepare_graph',
00407                  'cm_value':v,
00408                  'cm_data_desc':{"type":"text", "field_size":"5"}}
00409              r1=cm_kernel.access(ii)
00410              if r1['cm_return']>0: return r1
00411              cm_kernel.print_for_web('<small><b>Data entry to sort:</b><BR>'+r1['cm_value_html']+'</small>')
00412 
00413              cm_kernel.print_for_con('</TD><TD></TD></TR>')
00414 
00415           cm_kernel.print_for_con('</TABLE>')
00416 
00417        # Additional parameters for Python Graphs
00418        if graph_type=='mpl':
00419           cm_kernel.print_for_web(web['table_init'])
00420           cm_kernel.print_for_web('<TR><TD><small>')
00421 
00422           # Size
00423           v=i.get('mpl_image_size_x','9')
00424           ii={'cm_run_module_uoa':ini['cfg']['cm_modules']['cm-web'],
00425               'cm_action':'convert_field_to_html',
00426               'cm_key':'mpl_image_size_x',
00427               'cm_mode':'update',
00428               'cm_submit_on_change':'yes',
00429               'cm_web_form':'prepare_graph',
00430               'cm_value':v,
00431               'cm_data_desc':{"type":"text", "field_size":"5"}}
00432           r1=cm_kernel.access(ii)
00433           if r1['cm_return']>0: return r1
00434           cm_kernel.print_for_web('<b>Figure size X (inch):</b>&nbsp;'+r1['cm_value_html']+'&nbsp;')
00435 
00436           v=i.get('mpl_image_size_y','5')
00437           ii={'cm_run_module_uoa':ini['cfg']['cm_modules']['cm-web'],
00438               'cm_action':'convert_field_to_html',
00439               'cm_key':'mpl_image_size_y',
00440               'cm_mode':'update',
00441               'cm_submit_on_change':'yes',
00442               'cm_web_form':'prepare_graph',
00443               'cm_value':v,
00444               'cm_data_desc':{"type":"text", "field_size":"9"}}
00445           r1=cm_kernel.access(ii)
00446           if r1['cm_return']>0: return r1
00447           cm_kernel.print_for_web('<b>Figure size Y (inch):</b>&nbsp;'+r1['cm_value_html']+'&nbsp;')
00448 
00449           v=i.get('mpl_image_dpi','100')
00450           ii={'cm_run_module_uoa':ini['cfg']['cm_modules']['cm-web'],
00451               'cm_action':'convert_field_to_html',
00452               'cm_key':'mpl_image_dpi',
00453               'cm_mode':'update',
00454               'cm_submit_on_change':'yes',
00455               'cm_web_form':'prepare_graph',
00456               'cm_value':v,
00457               'cm_data_desc':{"type":"text", "field_size":"5"}}
00458           r1=cm_kernel.access(ii)
00459           if r1['cm_return']>0: return r1
00460           cm_kernel.print_for_web('<b>DPI:</b>&nbsp;'+r1['cm_value_html']+'&nbsp;')
00461 
00462           x=''
00463           if i.get('mpl_detach_graph','')=='on': x='checked'
00464           cm_kernel.print_for_con('<B>Detach graph:</B></small><input type="checkbox" name="mpl_detach_graph" '+x+' onchange="document.prepare_graph.submit();"></TD></TR>')
00465 
00466           cm_kernel.print_for_web('</small></TD></TR></TABLE>')
00467 
00468        # Fixing or ranging additional dimensions
00469        cm_kernel.print_for_web(web['table_init'])
00470        cm_kernel.print_for_web('<TR><TD><small>')
00471 
00472        # Size
00473        x=''
00474        if i.get('fix_additional_dimensions','')=='on': x='checked'
00475        cm_kernel.print_for_con('<B>Fix additional dimensions:</B></small><input type="checkbox" name="fix_additional_dimensions" '+x+' onchange="document.prepare_graph.submit();">')
00476 
00477        if i.get('fix_additional_dimensions','')=='on': 
00478           # Prepare axes type
00479           ndf=i.get('number_of_dims_to_fix','1')
00480           indf=int(ndf)
00481 
00482           ii={'cm_run_module_uoa':ini['cfg']['cm_modules']['cm-web'],
00483               'cm_action':'convert_field_to_html',
00484               'cm_key':'number_of_dims_to_fix',
00485               'cm_mode':'update',
00486               'cm_submit_on_change':'yes',
00487               'cm_web_form':'prepare_graph',
00488               'cm_value':ndf,
00489               'cm_data_desc':{"type":"text", "has_choice":"yes", "choice":['1','2','3','4','5','6','7','8','9'], "field_width":"50"}}
00490           r1=cm_kernel.access(ii)
00491           if r1['cm_return']>0: return r1
00492           cm_kernel.print_for_web('<small>&nbsp;&nbsp;<b>Number of dimensions to fix:</b>&nbsp;'+r1['cm_value_html']+'</small>')
00493 
00494           for q in range(0,indf):
00495               qq=str(q)
00496 
00497               # Key
00498               k='fix_dim_'+qq+'_key'
00499               v=i.get(k,'')
00500               ii={'cm_run_module_uoa':ini['cfg']['cm_modules']['cm-web'],
00501                   'cm_action':'convert_field_to_html',
00502                   'cm_key':k,
00503                   'cm_mode':'update',
00504                   'cm_submit_on_change':'yes',
00505                   'cm_web_form':'prepare_graph',
00506                   'cm_value':v,
00507                   'cm_data_desc':{"type":"text", "has_choice":"yes", "choice":keys, "choice_desc":keys_desc}}
00508               if i.get('use_raw_dimensions', '')=='on': ii['cm_data_desc']['no_sorting']='yes'
00509               else: ii['cm_data_desc']['sort_by_desc']='yes'
00510 
00511               r1=cm_kernel.access(ii)
00512               if r1['cm_return']>0: return r1
00513 
00514               cm_kernel.print_for_web('<br><b><small>Flattened key:</B>'+r1['cm_value_html']+'&nbsp;')
00515 
00516               # Less
00517               k='fix_dim_'+qq+'_less'
00518               v=i.get(k,'')
00519               ii={'cm_run_module_uoa':ini['cfg']['cm_modules']['cm-web'],
00520                   'cm_action':'convert_field_to_html',
00521                   'cm_key':k,
00522                   'cm_mode':'update',
00523                   'cm_submit_on_change':'yes',
00524                   'cm_web_form':'prepare_graph',
00525                   'cm_value':v,
00526                   'cm_data_desc':{"type":"text", "field_size":"5"}}
00527               r1=cm_kernel.access(ii)
00528               if r1['cm_return']>0: return r1
00529               cm_kernel.print_for_web('< '+r1['cm_value_html']+'&nbsp;')
00530 
00531               # Equal
00532               k='fix_dim_'+qq+'_equal'
00533               v=i.get(k,'')
00534               ii={'cm_run_module_uoa':ini['cfg']['cm_modules']['cm-web'],
00535                   'cm_action':'convert_field_to_html',
00536                   'cm_key':k,
00537                   'cm_mode':'update',
00538                   'cm_submit_on_change':'yes',
00539                   'cm_web_form':'prepare_graph',
00540                   'cm_value':v,
00541                   'cm_data_desc':{"type":"text", "field_size":"10"}}
00542               r1=cm_kernel.access(ii)
00543               if r1['cm_return']>0: return r1
00544               cm_kernel.print_for_web('== '+r1['cm_value_html']+'&nbsp;')
00545 
00546               # More
00547               k='fix_dim_'+qq+'_more'
00548               v=i.get(k,'')
00549               ii={'cm_run_module_uoa':ini['cfg']['cm_modules']['cm-web'],
00550                   'cm_action':'convert_field_to_html',
00551                   'cm_key':k,
00552                   'cm_mode':'update',
00553                   'cm_submit_on_change':'yes',
00554                   'cm_web_form':'prepare_graph',
00555                   'cm_value':v,
00556                   'cm_data_desc':{"type":"text", "field_size":"5"}}
00557               r1=cm_kernel.access(ii)
00558               if r1['cm_return']>0: return r1
00559               cm_kernel.print_for_web('> '+r1['cm_value_html']+'&nbsp;</small>')
00560 
00561 
00562        cm_kernel.print_for_web('</small></TD></TR></TABLE>')
00563 
00564     cm_kernel.print_for_con('</TD></TR></TABLE>')
00565 
00566     cm_kernel.print_for_con('</TD><TD valign="TOP">')
00567 
00568     cm_kernel.print_for_con('</TD></TR></TABLE>')
00569 
00570     cm_kernel.print_for_con('<input type="submit" class="cm-button" value="Refresh">')
00571     cm_kernel.print_for_con('<input type="submit" class="cm-button" name="cm_button_reset" value="Reset">')
00572 
00573     # Create a 'reproduce' URL
00574     if 'jscript_copy_to_clipboard' in web:
00575        ix=copy.deepcopy(i)
00576        if 'cm_web_get' in ix: del(ix['cm_web_get'])
00577        if 'cm_submenu' in ix: del(ix['cm_submenu'])
00578        if 'cm_web' in ix: del(ix['cm_web'])
00579        if 'cm_action' in ix: del(ix['cm_action'])
00580        if 'cm_console' in ix: del(ix['cm_console'])
00581        if 'cm_web_session' in ix: del(ix['cm_web_session'])
00582        if 'cm_run_module_uoa' in ix: del(ix['cm_run_module_uoa'])
00583        if 'cm_module_uoa' in ix: del(ix['cm_module_uoa'])
00584        if 'cm_menu' in ix: del(ix['cm_menu'])
00585        ii={}
00586        cm_kernel.copy_vars({'cm_input_array':ix, 'cm_output_array':ii, 
00587                             'cm_vars':[]})
00588        r=cm_kernel.convert_cm_array_to_uri({'cm_array':ii})
00589        if r['cm_return']==0:
00590 
00591           reproduce=web['http_prefix']+'cm_menu=scenarios&cm_submenu=ctuning_space_visualize&cm_json='+r['cm_string1']
00592 
00593           cm_kernel.print_for_web(web['jscript_copy_to_clipboard'])
00594           cm_kernel.print_for_web('<input type="button" class="cm-button" onClick="copyToClipboard(\''+reproduce+'\');" value="Reproduce (copy URL to clipboard)">')
00595 
00596     # Prepare axes dimensions
00597     axes=[]
00598     if 'ct_axis_x' in i and i['ct_axis_x']!='': axes.append(i['ct_axis_x'])
00599     if 'ct_axis_y' in i and i['ct_axis_y']!='': axes.append(i['ct_axis_y'])
00600     if 'ct_axis_z' in i and i['ct_axis_z']!='': axes.append(i['ct_axis_z'])
00601 
00602     axes_desc={}
00603     if 'ct_axis_x' in i:
00604        axes_desc[i['ct_axis_x']]={}
00605        if i['ct_axis_x'] in keys_desc: axes_desc[i['ct_axis_x']]=keys_desc[i['ct_axis_x']]
00606        elif 'ct_axis_x_type' in i: axes_desc[i['ct_axis_x']]['type']=i['ct_axis_x_type']
00607 
00608        if i.get('ct_axis_x_sort','')!='': axes_desc[i['ct_axis_x']]['sort']=i['ct_axis_x_sort']
00609        if i.get('ct_axis_x_name','')!='': axes_desc[i['ct_axis_x']]['desc_text']=i['ct_axis_x_name']
00610 
00611     if 'ct_axis_y' in i:
00612        axes_desc[i['ct_axis_y']]={}
00613        if i['ct_axis_y'] in keys_desc: axes_desc[i['ct_axis_y']]=keys_desc[i['ct_axis_y']]
00614        elif 'ct_axis_y_type' in i: axes_desc[i['ct_axis_y']]['type']=i['ct_axis_y_type']
00615 
00616        if i.get('ct_axis_y_sort','')!='': axes_desc[i['ct_axis_y']]['sort']=i['ct_axis_y_sort']
00617        if i.get('ct_axis_y_name','')!='': axes_desc[i['ct_axis_y']]['desc_text']=i['ct_axis_y_name']
00618 
00619     if 'ct_axis_z' in i:
00620        axes_desc[i['ct_axis_z']]={}
00621        if i['ct_axis_z'] in keys_desc: axes_desc[i['ct_axis_z']]=keys_desc[i['ct_axis_z']]
00622        elif 'ct_axis_z_type' in i: axes_desc[i['ct_axis_z']]['type']=i['ct_axis_z_type']
00623 
00624        if i.get('ct_axis_z_sort','')!='': axes_desc[i['ct_axis_z']]['sort']=i['ct_axis_z_sort']
00625        if i.get('ct_axis_z_name','')!='': axes_desc[i['ct_axis_z']]['desc_text']=i['ct_axis_z_name']
00626 
00627     # Check if data is enough to plot graph
00628     if 'ct_data_selection' in i and len(i['ct_data_selection'])>0 and \
00629        'ct_graph_type' in i and i['ct_graph_type']!='':
00630        enough=True 
00631        if len(axes)<min_axes: enough=False
00632 
00633     # Prepare data to save graph
00634     all={'ct_axes':axes}
00635     all['use_multiple_graphs']=i.get('use_multiple_graphs','')
00636     all['convert_text_axes_to_float']=i.get('convert_text_axes_to_float','')
00637     all['ct_module_uoa']=ct_module_uoa
00638     if 'ct_data_selection' in i: 
00639        x=i['ct_data_selection']
00640        if len(i['ct_data_selection'])>0:
00641           if i.get('reverse_order_of_graphs','')=='on':
00642             x=[]
00643             for q in range(len(i['ct_data_selection'])-1, -1, -1):
00644                 x.append(i['ct_data_selection'][q])
00645        all['ct_data_selection']=x
00646     if 'ct_graph_type' in i and i['ct_graph_type']!='': all['ct_graph_type']=i['ct_graph_type']
00647     all['ct_axes_desc']=axes_desc
00648 
00649     all['data_entry_to_sort']=i.get('data_entry_to_sort','')
00650 
00651     if graph_type=='mpl':
00652        all['mpl_extra_params']={'mpl_image_size_x':i.get('mpl_image_size_x',''), \
00653                                 'mpl_image_size_y':i.get('mpl_image_size_y',''), \
00654                                 'mpl_image_dpi':i.get('mpl_image_dpi',''),
00655                                 'mpl_detach_graph':i.get('mpl_detach_graph','')}
00656     if i.get('fix_additional_dimensions','')=='on': 
00657        all['fix_additional_dimensions']='yes'
00658        all['fix_additional_dimensions_data']={}
00659        ndf=i.get('number_of_dims_to_fix','1')
00660        indf=int(ndf)
00661        for q in range(0,indf):
00662            qq=str(q)
00663            kk1='fix_dim_'+qq+'_key';   vv1=i.get(kk1,'')
00664            kk2='fix_dim_'+qq+'_less';  vv2=i.get(kk2,'')
00665            kk3='fix_dim_'+qq+'_equal'; vv3=i.get(kk3,'')
00666            kk4='fix_dim_'+qq+'_more';  vv4=i.get(kk4,'')
00667 
00668            if vv1!='' and (vv2!='' or vv3!='' or vv4==''):
00669               all['fix_additional_dimensions_data'][vv1]={'less':vv2, 'equal':vv3, 'more':vv4}
00670 
00671     r=cm_kernel.convert_cm_array_to_uri({'cm_array':all})
00672     reproduce_save_graph=r['cm_string']
00673 
00674     # Create export csv button
00675     s=web['http_prefix']+'cm_web_module_uoa=ctuning.space&cm_web_action=visualize_selection&cm_file_ext=csv&save_graph=yes&cm_json='+reproduce_save_graph
00676 
00677     cm_kernel.print_for_web('<input type="button" class="cm-button" onClick="window.location=\''+s+'\'" target="_top" value="Export selection as csv">')
00678 
00679     cm_kernel.print_for_con('</FORM>')
00680 
00681     cm_kernel.print_for_con('</TD></TR></TABLE>')
00682 
00683     # Plotting graph if enough info
00684     if i.get('save_graph','')!='yes':
00685        cm_kernel.print_for_con('<BR><small><B>Graph</B></small>')
00686        cm_kernel.print_for_web(web['table_init']+'<TR><TD align="center">')
00687     if not enough:
00688        cm_kernel.print_for_con('<small>Selection is not yet enough to plot the graph ...</small>')
00689     else:
00690        all['url']=web['http_prefix']
00691        all['reproduce_save_graph']=reproduce_save_graph
00692        if 'save_graph' in i: all['save_graph']=i['save_graph']
00693 
00694        r=visualize_selection(all)
00695        if r['cm_return']>0: cm_kernel.print_for_con('cM Error: '+r['cm_error'])
00696 
00697     cm_kernel.print_for_con('</TD></TR></TABLE>')
00698 
00699     return {'cm_return':0}
00700 
00701 # ============================================================================
00702 def visualize_selection(i):
00703 
00704     """
00705     Prepare graph data and call graph engine
00706 
00707     Input:  {
00708               ct_module_uoa                    - module to visualize
00709               ct_data_selection                - list of ctuning.space data
00710               ct_axes                          - axes
00711               ct_graph_type                    - graph engine
00712               ct_axes_desc                     - axes description
00713               (url)                            - starting url of cM
00714               (save_graph)                     - if 'yes', save graph
00715               (cm_file_ext)                    - file extension if save_graph=='yes'
00716               (reproduce_save_graph)           - data to reproduce graph for saving
00717               (use_multiple_graphs)            - if 'on' separate entries into different sub-graphs
00718               (convert_text_axes_to_float)     - convert text axes to consequitive floats
00719               (mpl_extra_params)               - if python mat plot is used, this dictionary has extra params
00720                                                     mpl_image_size_x - image X size in inch
00721                                                     mpl_image_size_y - image Y size in inch
00722                                                     mpl_image_dpi    - image DPI
00723                                                     mpl_detach_graph - if 'on', detach graph
00724               (data_entry_to_sort)             - if sort and multiple entries, use this entry number to sort (-1)
00725               (fix_additional_dimensions)      - if 'yes', fix or range additional dimensions
00726               (fix_additional_dimensions_data) - dictionary of format:
00727                                                    {"flattened key A":{'less':X, 'equal':Y, 'more':Z}, ...}
00728 
00729             }
00730 
00731     Output: {
00732               cm_return             - return code >0 if error
00733 
00734               data to save graph, if save_graph=='yes'
00735             }
00736     """
00737 
00738     # Check vars
00739     if 'ct_data_selection' not in i:
00740        return {'cm_return':1, 'cm_error':'"ct_data_selection" is not defined in "ctuning.space plot"'}
00741     if 'ct_axes' not in i:
00742        return {'cm_return':1, 'cm_error':'"ct_axes" is not defined in "ctuning.space plot"'}
00743     if 'ct_graph_type' not in i:
00744        return {'cm_return':1, 'cm_error':'"ct_graph_type" is not defined in "ctuning.space plot"'}
00745 
00746     # Convert space points to array
00747     d=i['ct_data_selection']
00748     g=i['ct_graph_type']
00749     axes=i['ct_axes']
00750 
00751     ii={'ct_points':d, 'ct_axes':axes, 'ct_module_uoa':i.get('ct_module_uoa','')}
00752     ii['ct_axes_desc']=i['ct_axes_desc']
00753     ii['use_multiple_graphs']=i.get('use_multiple_graphs','')
00754     ii['data_entry_to_sort']=i.get('data_entry_to_sort','')
00755 
00756     ii['fix_additional_dimensions']=i.get('fix_additional_dimensions','')
00757     ii['fix_additional_dimensions_data']=i.get('fix_additional_dimensions_data',{})
00758     r=convert_ct_space_points_to_python_array(ii)
00759     if r['cm_return']>0: return r                                
00760     axes=r['ct_axes']
00761 
00762     a=r['ct_array']
00763     a_id=r['ct_array_id']
00764 
00765     # Check if export to csv
00766     if i.get('save_graph','')=='yes' and i.get('cm_file_ext','')=='csv':
00767        ii={'ct_array':a, 'ct_axes':axes}
00768        if 'ct_axes_desc' in i: ii['ct_axes_desc']=i['ct_axes_desc']
00769        r=convert_python_array_to_csv(ii)
00770        if r['cm_return']>0: return r
00771        csv=r['cm_string']
00772 
00773        tp='csv'
00774 
00775        # Generate tmp file
00776        r=cm_kernel.gen_cm_tmp_file({})
00777        if r['cm_return']>0: return r
00778        tf=r['cm_path']
00779        uid=r['cm_uid']
00780 
00781        # Save table
00782        f=file(tf, 'w')
00783        f.write(csv)
00784        f.close()
00785        
00786        rr={'cm_return':0}
00787 
00788        rr['cm_full_filename']=tf
00789 
00790        rr['cm_web_download']='yes'
00791        rr['cm_web_set_headers']='yes'
00792 
00793        # Prepare content header
00794        ii1={'cm_run_module_uoa':ini['cfg']['cm_modules']['cm-web'],
00795             'cm_action':'prepare_content_header',
00796             'cm_web_content':'.'+tp,
00797             'cm_filename':tf}
00798        r=cm_kernel.access(ii1)
00799        if r['cm_return']>0: return r
00800 
00801        # Substitute special variables
00802        wh=[]
00803        for x in r['cm_web_headers']:
00804            wh.append(x.replace(cm_kernel.convert_str_to_special('filename'), 'table.'+tp))
00805 
00806        rr['cm_web_headers']=wh
00807 
00808        rr['cm_web_postprocess']='no'
00809 
00810        rr['cm_delete_file_after_view']='yes' # for tmp files!
00811 
00812        return rr
00813 
00814     # Call graph action
00815     if 'available_graph_engines' not in ini['cfg']:
00816        return {'cm_return':1, 'cm_error':'no graph engines configured'}
00817     age=ini['cfg']['available_graph_engines']
00818 
00819     r=cm_kernel.find_index_in_array({'cm_array':age, 'cm_index':g})
00820     if r['cm_return']!=0:
00821        return {'cm_return':1, 'cm_error':'Problem with configuration for graph engine '+str(g)}
00822     cm_action=r['cm_array']['cm_action']
00823 
00824     ii={'cm_run_module_uoa':ini['cm_module_uoa'],
00825         'cm_action':cm_action,
00826         'ct_module_uoa':i.get('ct_module_uoa',''),
00827         'ct_array':a,
00828         'ct_array_id':a_id,
00829         'ct_axes':axes,
00830         'ct_points':d,
00831         'mpl_extra_params':i.get('mpl_extra_params',{})}
00832     if 'reproduce_save_graph' in i: ii['reproduce']=i['reproduce_save_graph']
00833     if 'convert_text_axes_to_float' in i: ii['convert_text_axes_to_float']=i['convert_text_axes_to_float']
00834     if 'url' in i: ii['url']=i['url']
00835     if 'save_graph' in i: ii['save_graph']=i['save_graph']
00836     if 'ct_axes_desc' in i: ii['ct_axes_desc']=i['ct_axes_desc']
00837     if 'cm_file_ext' in i: ii['cm_file_ext']=i['cm_file_ext']
00838 
00839     return cm_kernel.access(ii)
00840 
00841 # ============================================================================
00842 def convert_ct_space_points_to_python_array(i):
00843 
00844     """
00845     Convert cTuning space points to python array
00846 
00847     Input:  {
00848               ct_module_uoa                    - module to visualize
00849               ct_points                        - list of UOA with points
00850               ct_axes                          - list with flattened axes (if empty, all axes are taken)
00851               ct_axes_desc                     - axes description
00852               (use_multiple_graphs)            - if 'on' separate entries into different sub-graphs
00853               (data_entry_to_sort)             - if sort and multiple entries, use this entry number to sort (-1)
00854               (fix_additional_dimensions)      - if 'yes', fix or range additional dimensions
00855               (fix_additional_dimensions_data) - dictionary of format:
00856                                                    {"flattened key A":{'less':X, 'equal':Y, 'more':Z}, ...}
00857             }
00858 
00859     Output: {
00860               cm_return    - return code >0 if error
00861               ct_array     - multidimensional array ([ [],[],...])
00862               ct_array_id  - 1D array with {cm_uid, cm_uoa}
00863               ct_axes      - either original one or ALL axes if original len==0
00864             }
00865     """
00866 
00867     # Check vars
00868     if 'ct_points' not in i: return {'cm_return':0, 'cm_error':'"ct_points" is not defined in ctuning.space convert_ct_space_points_to_python_array'}
00869     if 'ct_axes' not in i: return {'cm_return':0, 'cm_error':'"ct_axes" is not defined in ctuning.space convert_ct_space_points_to_python_array'}
00870 
00871     p=i['ct_points']
00872     ct_axes=i['ct_axes']
00873 
00874     # Check if need to use multiple sub graphs
00875     umg=i.get('use_multiple_graphs','')
00876 
00877     fad=i.get('fix_additional_dimensions_data',{})
00878 
00879     # Load points
00880     init=False
00881     pl=0 # if use multiple graphs
00882     pl1=00
00883     for x in p:
00884         ii={'cm_run_module_uoa':ini['cm_module_uid'],
00885             'cm_action':'load',
00886             'cm_data_uoa':x}
00887         if i.get('ct_module_uoa','')!='': ii['cm_run_module_uoa']=i['ct_module_uoa']
00888         rx=cm_kernel.access(ii)
00889         if rx['cm_return']>0: return rx
00890         data=rx['cm_data_obj']['cfg']
00891 
00892         # Continue processing
00893         aggregated=[data]
00894         if 'points' in data:
00895            aggregated=data['points']
00896 
00897         # Initialize axes (FGG: put it here to be able to extract all axes if they were not set in advance)
00898         if not init:
00899            axes=i['ct_axes']
00900 
00901            # Prepare array
00902            a=[]
00903            a_id=[]
00904 
00905            l=len(axes)
00906 
00907            # If len(axes)==0 select ALL!
00908            if l==0:
00909               rq=cm_kernel.flatten_array({'cm_array':aggregated[0]})
00910               if rq['cm_return']>0: return rq
00911 
00912               for qx in sorted(rq['cm_array']):
00913                   axes.append(qx)
00914 
00915               l=len(axes)
00916 
00917         # Initialize aggregated or individual arrays (if use multiple graphs)
00918         if not init or umg=='on':
00919            init=True
00920 
00921            a.append([])
00922            a_id.append([])
00923 
00924            pl1+=1
00925            pl=pl1-1
00926 
00927            for z in range(0, l):
00928                a[pl].append([])
00929 
00930         # Continue processing data
00931         dd=0
00932         for d in aggregated:
00933             # Check if fix dimensions
00934             skip=False
00935             if len(fad)>0:
00936                for y in fad:
00937                    yf=fad[y]
00938                    vvl=yf['less']
00939                    vve=yf['equal']
00940                    vvm=yf['more']
00941 
00942                    if vvl!='' or vve!='' or vvm!='':
00943                       yy=y
00944                       if y[-1]=='$': 
00945                          # Prepare list
00946                          yy=y[:-1]+'0'
00947 
00948                       r=cm_kernel.get_value_by_flattened_key({'cm_array':d, 'cm_key':yy})
00949                       if r['cm_return']>0: return r
00950 
00951                       if r['cm_value']==None: 
00952                          skip=True
00953                          break
00954                       
00955                       value=str(r['cm_value'])
00956 
00957                       if vve!='':
00958                          # equal
00959 
00960                          if value!=vve:
00961                             skip=True
00962                             break
00963 
00964                       elif vvl!='' or vvm!='':
00965                          # less or more - should be float
00966                          fvv=value
00967                          fvvl=float(vvl)
00968                          fvvm=float(vvm)
00969 
00970                          if fvv>fvvl or ffv<fvvm:
00971                             skip=True
00972                             break
00973 
00974             if not skip:
00975                # Process axes
00976                q=0 # axes number
00977                fail=False
00978                tm=[]
00979 
00980                for y in axes:
00981                    yy=y
00982                    if y[-1]=='$': 
00983                       # Prepare list
00984                       yy=y[:-1]+'0'
00985 
00986                    r=cm_kernel.get_value_by_flattened_key({'cm_array':d, 'cm_key':yy})
00987                    if r['cm_return']>0: return r
00988 
00989                    if r['cm_value']==None: 
00990                       fail=True
00991                       break
00992                    
00993                    value=str(r['cm_value'])
00994 
00995                    if 'ct_axes_desc' in i and y in i['ct_axes_desc'] and 'type' in i['ct_axes_desc'][y]:
00996                       tp=i['ct_axes_desc'][y]['type']
00997                       r1=cm_kernel.convert_text_to_value({'cm_text':value,
00998                                                           'cm_type':tp})
00999                       if r1['cm_return']==0:
01000                          tm.append(r1['cm_value'])
01001                       else:
01002                          fail=True
01003                          break
01004                    else:
01005                       a[pl][q].append(value)
01006 
01007                    q+=1
01008 
01009                if not fail:
01010                   for q in range(0, len(axes)):
01011                       a[pl][q].append(tm[q])
01012                   a_id[pl].append({'cm_uid':rx['cm_uid'], 'cm_uoa':rx['cm_uoa'], 'ct_point':dd})
01013 
01014             dd+=1 # point in entry
01015 
01016     # Sorting
01017     d=-1 # Axis to sort
01018     u=0
01019     sort_type=''
01020     for q in ct_axes:
01021         desc=i.get('ct_axes_desc',{}).get(q,{})
01022         if desc.get('sort','')!='':
01023            d=u
01024            sort_type=desc['sort']
01025            break
01026         u+=1
01027 
01028     if d>=0:
01029        x=i.get('data_entry_to_sort','1')
01030        if x=='': x='1'
01031        cp=int(x)-1 # Which point entry to sort on
01032        if cp>=len(a): cp=0
01033 
01034        ll=len(a[cp][0])
01035 
01036 #       print '<PRE>',json.dumps(a,indent=2),'</PRE>'
01037 
01038        for q in range(0, ll-1):
01039            for qq in range(q+1, ll):
01040                if (sort_type=='ascending' and a[cp][d][q]>a[cp][d][qq]) or \
01041                   (sort_type=='descending' and a[cp][d][q]<a[cp][d][qq]):
01042                   for cpx in range(0, len(a)):
01043                      (a_id[cpx][q],a_id[cpx][qq]) = (a_id[cpx][qq], a_id[cpx][q])
01044                      for x in range(0,l):
01045                          (a[cpx][x][q],a[cpx][x][qq]) = (a[cpx][x][qq], a[cpx][x][q])
01046 
01047 #       print '<PRE>',json.dumps(a,indent=2),'</PRE>'
01048 
01049     return {'cm_return':0, 'ct_array':a, 'ct_array_id':a_id, 'ct_axes':axes}
01050 
01051 # ============================================================================
01052 def convert_python_array_to_gws_array(i):
01053 
01054     """
01055     Convert python array to Google Web Services array
01056 
01057     Input:  {
01058               ct_array                     - collective tuning array
01059               ct_array_id                  - array with space data points UIDs
01060               ct_axes                      - axes
01061               ct_axes_desc                 - axes description
01062               ct_axes_type                 - needed types for Google graph
01063               (convert_text_axes_to_float) - convert text axes to consequitive floats
01064             }
01065 
01066     Output: {
01067               cm_return  - return code >0 if error
01068               s_array    - GWS array 
01069               s_array_id - GWS array ID (to process clicks)
01070             }
01071     """
01072 
01073     import numpy as np
01074 
01075     # Check vars
01076     if 'ct_array' not in i: return {'cm_return':1, 'cm_error':'"ct_array" is not defined in "ctuning.space convert_python_array_to_gws_array"'}
01077     if 'ct_array_id' not in i: return {'cm_return':1, 'cm_error':'"ct_array_id" is not defined in "ctuning.space convert_python_array_to_gws_array"'}
01078     if 'ct_axes' not in i:  return {'cm_return':1, 'cm_error':'"ct_axes" is not defined in "ctuning.space convert_python_array_to_gws_array"'}
01079     if 'ct_axes_type' not in i:  return {'cm_return':1, 'cm_error':'"ct_axes_type" is not defined in "ctuning.space convert_python_array_to_gws_array"'}
01080 
01081     axes=i['ct_axes']
01082     l=len(axes)
01083 
01084     axes_desc={}
01085     if 'ct_axes_desc' in i: axes_desc=i['ct_axes_desc']
01086 
01087     axes_type=i['ct_axes_type']
01088 
01089     #Preparing multi-dimensional array for Google Web Services
01090     s_array=''
01091     s_array_id=''
01092 
01093     cta=i['ct_array']
01094     a_id=i['ct_array_id']
01095 
01096 #    sa=[]
01097 #    isa=[]
01098 #    for x in range(0, l):
01099 #        sa.append({})
01100 #        isa.append(0)
01101 
01102     # Check if need to convert text axis to float 
01103     if i.get('convert_text_axes_to_float','')=='on': 
01104        na=str(len(axes))
01105     else:
01106        na=1
01107 
01108     r=convert_text_axes_to_float({'ct_array':cta, 'ct_axes':axes, 'ct_axes_desc':axes_desc})
01109     if r['cm_return']>0: return r
01110     cta=r['ct_array']
01111 
01112      # Check unique X axis type
01113     type_x=axes_desc.get(axes[0],{}).get('type','')
01114 
01115     u=r['unique'][0]
01116     us=r['unique_string'][0]
01117 
01118     ind=np.arange(1,len(u)+1)
01119 
01120     ind1=[]
01121     for q in ind:
01122         ind1.append(q)
01123 
01124     # If X axis is text, add all together (usually bars)
01125     ll=len(cta)
01126     if axes_type[0]=='text': 
01127        for qq in range(0, len(cta[0][0])):
01128            line=''
01129 
01130            # first column
01131            xx=str(cta[0][0][qq])
01132            if axes_type[0]=='text': xx='"'+xx+'"'
01133                 
01134            if line!='': line+=','
01135            line+=xx
01136 
01137            for p in range(0, ll):
01138                xx=''
01139                if len(cta[p][1])>qq: 
01140                   xx=str(cta[p][1][qq])
01141                   if axes_type[1]=='text': xx='"'+xx+'"'
01142                else:
01143                   xx='null'
01144                     
01145                if line!='': line+=','
01146                line+=xx
01147 
01148            if s_array!='': s_array+=','
01149            s_array+='['+line+']\n'
01150 
01151            o1="'1'"
01152            o2="'1'"
01153 
01154            if qq<len(a_id[p]): 
01155               o1="'"+str(a_id[p][qq]['cm_uid'])+"'"
01156               o2="'"+str(a_id[p][qq]['ct_point'])+"'"
01157 
01158               if s_array_id!='': s_array_id+=','
01159               s_array_id+="["+o1+", "+o2+"]\n"
01160     else:
01161        for kk in range(0, ll):
01162            for qq in range(0, len(cta[kk][0])):
01163                line=''
01164 
01165                # first column
01166                xx=str(cta[kk][0][qq])
01167                     
01168                if line!='': line+=','
01169                line+=xx
01170 
01171                for p in range(0, ll):
01172                    xx='null'
01173                    if p==kk:
01174                       xx=str(cta[p][1][qq])
01175                         
01176                    if line!='': line+=','
01177                    line+=xx
01178 
01179                if s_array!='': s_array+=','
01180                s_array+='['+line+']\n'
01181 
01182                o1="'1'"
01183                o2="'1'"
01184 
01185                if qq<len(a_id[kk]): 
01186                   o1="'"+str(a_id[kk][qq]['cm_uid'])+"'"
01187                   o2="'"+str(a_id[kk][qq]['ct_point'])+"'"
01188 
01189                   if s_array_id!='': s_array_id+=','
01190                   s_array_id+="["+o1+", "+o2+"]\n"
01191 
01192     # Print legends, etc, if needed
01193     legend=''
01194     if type_x=='text' or type_x=='uoa':
01195        gs=ini['cfg']['mpl_point_styles']
01196 
01197        legend='<small>'
01198 
01199        legend+='<br><B><i>Colors:&nbsp;</i></b>'
01200        s=0
01201        for p in range(0,len(cta)):
01202            # Load point
01203 
01204            legend+='<span style="color:'+gs[s]['color']+'">'+i['ct_points'][s]+'</span>&nbsp;;&nbsp;&nbsp;&nbsp; '
01205            s+=1
01206            if s>=len(gs):s=0
01207 
01208        legend+='<br><br><B><i>Axis&nbsp;X:&nbsp;</i></b>'
01209        for q in ind1:
01210           legend+='<b>'+str(q)+'</b>&nbsp;-&nbsp;'+us[q-1]+'&nbsp;;&nbsp;&nbsp;&nbsp; '
01211        legend+='<br>'
01212 
01213 
01214        legend+='<br></small>'
01215 
01216     return {'cm_return':0, 's_array': s_array, 's_array_id':s_array_id, 'legend':legend}
01217 
01218 # ============================================================================
01219 def convert_python_array_to_csv(i):
01220 
01221     """
01222     Convert python array to csv
01223 
01224     Input:  {
01225               ct_array       - ct python array
01226               ct_axes        - axes
01227               (ct_axes_desc) - axes description
01228               (cm_separator) - default ';'
01229             }
01230 
01231     Output: {
01232               cm_return   - return code >0 if error
01233               cm_string   - csv string
01234             }
01235     """
01236 
01237     # Check vars
01238     if 'ct_array' not in i: return {'cm_return':1, 'cm_error':'"ct_array" is not defined in "ctuning.space convert_python_array_to_csv"'}
01239     if 'ct_axes' not in i:  return {'cm_return':1, 'cm_error':'"ct_axes" is not defined in "ctuning.space convert_python_array_to_csv"'}
01240 
01241     sep=';'
01242     if 'cm_separator' in i: sep=i['cm_separator']
01243 
01244     axes=i['ct_axes']
01245     axes_desc={}
01246     if 'ct_axes_desc' in i: axes_desc=i['ct_axes_desc']
01247 
01248     cta=i['ct_array']
01249 
01250     str=''
01251 
01252     # Prepare header
01253     line=''
01254     for q in range(0, len(axes)):
01255         tit=axes[q]
01256         if axes[q] in axes_desc and 'desc_text' in axes_desc[axes[q]]: tit='"'+axes_desc[axes[q]]['desc_text']+'"'
01257         if line!='': line+=sep
01258         line+=tit
01259     str+=line+'\n'
01260 
01261     for p in range(0,len(cta)):
01262         str+='\n'
01263         for qq in range(0, len(cta[p][0])):
01264             line=''
01265             for q in range(0, len(axes)):
01266                 if qq<len(cta[p][q]): 
01267                    if line!='': line+=sep
01268                    line+=unicode(cta[p][q][qq]).replace('.',',')
01269             str+=line+'\n'
01270 
01271     return {'cm_return':0, 'cm_string':str}
01272 
01273 # ============================================================================
01274 def gws_2d_column_chart(i):
01275 
01276     """
01277     Google Web Services 2D Column Chart
01278 
01279     Input:  {
01280               ct_module_uoa                - module to visualize
01281               ct_array                     - collective tuning array
01282               ct_array_id                  - array with space data points UIDs
01283               ct_axes                      - axes
01284               ct_axes_desc                 - axes description
01285               ct_points                    - selected entries
01286               url                          - starting url of cM
01287               (convert_text_axes_to_float) - convert text axes to consequitive floats
01288               (legend)                     - if !='', add to the end of the graph
01289 
01290             }
01291 
01292     Output: {
01293               cm_return  - return code >0 if error
01294             }
01295     """
01296 
01297     m=i.get('ct_module_uoa','')
01298     if m=='': m=ini['cm_module_uid']
01299 
01300     ii=i
01301     ii['ct_axes_type']=['text', 'float']
01302     r=convert_python_array_to_gws_array(ii)
01303     if r['cm_return']>0: return r
01304 
01305     legend=r.get('legend','')
01306 
01307     axes=i['ct_axes']
01308     axes_desc={}
01309     if 'ct_axes_desc' in i: axes_desc=i['ct_axes_desc']
01310 
01311     # Check web
01312     if 'cfg' in cm_kernel.ini['web_style']: web=cm_kernel.ini['web_style']['cfg']
01313     else:
01314        return {'cm_return':1, 'cm_error':'web style is not defined'}
01315 
01316     # Check url
01317     url=web['http_prefix']
01318     if 'url' in i: url=i['url']
01319 
01320     s_array=r['s_array']
01321     s_array_id=r['s_array_id']
01322 
01323     cm_kernel.print_for_con('    <script type="text/javascript" src="https://www.google.com/jsapi"></script>')
01324     cm_kernel.print_for_con('    <script type="text/javascript">')
01325     cm_kernel.print_for_con('      google.load("visualization", "1", {packages:["corechart"]});')
01326     cm_kernel.print_for_con('      google.setOnLoadCallback(drawChart);')
01327     cm_kernel.print_for_con('      function drawChart() {')
01328     cm_kernel.print_for_con('        var data = new google.visualization.DataTable();')
01329     cm_kernel.print_for_con('        var data_id = new google.visualization.DataTable();')
01330     cm_kernel.print_for_con("        data.addColumn('string', 'axis_x');")
01331 
01332     cta=i['ct_array']
01333     ll=len(cta)
01334     for p in range(0, ll):
01335         cm_kernel.print_for_con("        data.addColumn('number', 'axis_y');")
01336 
01337     cm_kernel.print_for_con('        data.addRows([')
01338     cm_kernel.print_for_web(s_array)
01339     cm_kernel.print_for_con('        ]);')
01340     cm_kernel.print_for_con("        data_id.addColumn('string', 'cm_data_uid');")
01341     cm_kernel.print_for_con("        data_id.addColumn('string', 'ct_point');")
01342     cm_kernel.print_for_con('        data_id.addRows([')
01343     cm_kernel.print_for_web(s_array_id)
01344     cm_kernel.print_for_con('        ]);')
01345     cm_kernel.print_for_con('')
01346     cm_kernel.print_for_con('        var options = {')
01347     cm_kernel.print_for_con('          title: \'\',')
01348 
01349     v=axes[0]
01350     if v in axes_desc and 'desc_text' in axes_desc[v]: v=axes_desc[v]['desc_text']
01351     cm_kernel.print_for_web('          hAxis: {title: \''+v+'\'},')
01352 
01353     v=axes[1]
01354     if v in axes_desc and 'desc_text' in axes_desc[v]: v=axes_desc[v]['desc_text']
01355     cm_kernel.print_for_web('          vAxis: {title: \''+v+'\'},')
01356 
01357     cm_kernel.print_for_con('          legend: \'none\'')
01358     cm_kernel.print_for_con('        };')
01359     cm_kernel.print_for_con('')
01360 
01361     cm_kernel.print_for_con('        var chart = new google.visualization.ColumnChart(document.getElementById(\'chart_div\'));')
01362     cm_kernel.print_for_con('        chart.draw(data, options);')
01363     cm_kernel.print_for_con('')
01364     cm_kernel.print_for_con('        google.visualization.events.addListener(chart, \'select\', selectHandler);')
01365     cm_kernel.print_for_con('')
01366     cm_kernel.print_for_con('        function selectHandler(e) {')
01367     cm_kernel.print_for_con('          var s = chart.getSelection()')
01368     cm_kernel.print_for_con('          for (var i=0; i<s.length; i++)')
01369     cm_kernel.print_for_con('          {')
01370     cm_kernel.print_for_con('            var item=s[i];')
01371     if url!='':
01372        cm_kernel.print_for_web("            window.location = '"+url+"cm_menu=browse&cm_subaction_view&browse_module_uoa="+m+"&browse_data_uoa='+data_id.getValue(item.row,0)+'###points@'+data_id.getValue(item.row,1)+'#'")
01373     cm_kernel.print_for_con('          }')
01374     cm_kernel.print_for_con('        }')
01375     cm_kernel.print_for_con('')
01376     cm_kernel.print_for_con('      }')
01377     cm_kernel.print_for_con('    </script>')
01378 
01379     cm_kernel.print_for_con('    <div id="chart_div" style="width: 900px; height: 500px;"></div>')
01380 
01381     if legend!='':
01382        cm_kernel.print_for_web('<br>'+legend+'<br>')
01383 
01384     return {'cm_return':0}
01385 
01386 # ============================================================================
01387 def gws_2d_scatter_chart(i):
01388 
01389     """
01390     Google Web Services 2D Scatter Chart
01391 
01392     Input:  {
01393               ct_module_uoa                - module to visualize
01394               ct_array                     - collective tuning array
01395               ct_array_id                  - array with space data points UIDs
01396               ct_axes                      - axes
01397               ct_axes_desc                 - axes description
01398               ct_points                    - selected entries
01399               url                          - starting url of cM
01400               (convert_text_axes_to_float) - convert text axes to consequitive floats
01401               (legend)                     - if !='', add to the end of the graph
01402             }
01403 
01404     Output: {
01405               cm_return  - return code >0 if error
01406             }
01407     """
01408 
01409     m=i.get('ct_module_uoa','')
01410     if m=='': m=ini['cm_module_uid']
01411 
01412     ii=i
01413     ii['ct_axes_type']=['float', 'float']
01414     r=convert_python_array_to_gws_array(ii)
01415     if r['cm_return']>0: return r
01416 
01417     legend=r.get('legend','')
01418 
01419     axes=i['ct_axes']
01420     axes_desc={}
01421     if 'ct_axes_desc' in i: axes_desc=i['ct_axes_desc']
01422 
01423     # Check url
01424     url=''
01425     if 'url' in i: url=i['url']
01426 
01427     s_array=r['s_array']
01428     s_array_id=r['s_array_id']
01429 
01430     cm_kernel.print_for_con('    <script type="text/javascript" src="https://www.google.com/jsapi"></script>')
01431     cm_kernel.print_for_con('    <script type="text/javascript">')
01432     cm_kernel.print_for_con('      google.load("visualization", "1", {packages:["corechart"]});')
01433     cm_kernel.print_for_con('      google.setOnLoadCallback(drawChart);')
01434     cm_kernel.print_for_con('      function drawChart() {')
01435     cm_kernel.print_for_con('        var data = new google.visualization.DataTable();')
01436     cm_kernel.print_for_con('        var data_id = new google.visualization.DataTable();')
01437     cm_kernel.print_for_con("        data.addColumn('number', 'axis_x');")
01438 
01439     cta=i['ct_array']
01440     ll=len(cta)
01441     for p in range(0, ll):
01442         cm_kernel.print_for_con("        data.addColumn('number', 'axis_y');")
01443 
01444     cm_kernel.print_for_con('        data.addRows([')
01445     cm_kernel.print_for_web(s_array)
01446     cm_kernel.print_for_con('        ]);')
01447     cm_kernel.print_for_con("        data_id.addColumn('string', 'cm_data_uid');")
01448     cm_kernel.print_for_con("        data_id.addColumn('string', 'ct_point');")
01449     cm_kernel.print_for_con('        data_id.addRows([')
01450     cm_kernel.print_for_web(s_array_id)
01451     cm_kernel.print_for_con('        ]);')
01452     cm_kernel.print_for_con('')
01453     cm_kernel.print_for_con('        var options = {')
01454     cm_kernel.print_for_con('          title: \'\',')
01455 
01456     v=axes[0]
01457     if v in axes_desc and 'desc_text' in axes_desc[v]: v=axes_desc[v]['desc_text']
01458     cm_kernel.print_for_web('          hAxis: {title: \''+v+'\'},')
01459 
01460     v=axes[1]
01461     if v in axes_desc and 'desc_text' in axes_desc[v]: v=axes_desc[v]['desc_text']
01462     cm_kernel.print_for_web('          vAxis: {title: \''+v+'\'},')
01463     cm_kernel.print_for_con('          legend: \'none\'')
01464     cm_kernel.print_for_con('        };')
01465     cm_kernel.print_for_con('')
01466 
01467     cm_kernel.print_for_con('        var chart = new google.visualization.ScatterChart(document.getElementById(\'chart_div\'));')
01468     cm_kernel.print_for_con('        chart.draw(data, options);')
01469     cm_kernel.print_for_con('')
01470     cm_kernel.print_for_con('        google.visualization.events.addListener(chart, \'select\', selectHandler);')
01471     cm_kernel.print_for_con('')
01472     cm_kernel.print_for_con('        function selectHandler(e) {')
01473     cm_kernel.print_for_con('          var s = chart.getSelection()')
01474     cm_kernel.print_for_con('          for (var i=0; i<s.length; i++)')
01475     cm_kernel.print_for_con('          {')
01476     cm_kernel.print_for_con('            var item=s[i];')
01477     if url!='':
01478        cm_kernel.print_for_web("            window.location = '"+url+"cm_menu=browse&cm_subaction_view&browse_module_uoa="+m+"&browse_data_uoa='+data_id.getValue(item.row,0)+'###points@'+data_id.getValue(item.row,1)+'#'")
01479     cm_kernel.print_for_con('          }')
01480     cm_kernel.print_for_con('        }')
01481     cm_kernel.print_for_con('')
01482     cm_kernel.print_for_con('      }')
01483     cm_kernel.print_for_con('    </script>')
01484 
01485     cm_kernel.print_for_con('    <div id="chart_div" style="width: 900px; height: 500px;"></div>')
01486 
01487     if legend!='':
01488        cm_kernel.print_for_web('<br>'+legend+'<br>')
01489 
01490     return {'cm_return':0}
01491 
01492 # ============================================================================
01493 def mpl_1d_density_graph(i):
01494 
01495     """
01496     Python Matplot 1D density graph
01497 
01498     Input:  {
01499               ct_array                     - collective tuning array
01500               ct_array_id                  - array with space data points UIDs
01501               ct_axes                      - axes
01502               ct_axes_desc                 - axes description
01503               ct_points                    - selected entries
01504               reproduce                    - parameters to reproduce/save graph
01505               (url)                        - starting url of cM
01506               (save_graph)                 - if 'yes', save graph
01507               (cm_file_ext)                - file extension if save_graph=='yes'
01508               (convert_text_axes_to_float) - convert text axes to consequitive floats
01509               (mpl_extra_params)           - if python mat plot is used, this dictionary has extra params
01510                                                 mpl_image_size_x - image X size in inch
01511                                                 mpl_image_size_y - image Y size in inch
01512                                                 mpl_image_dpi    - image DPI
01513                                                 mpl_detach_graph - if 'on', detach graph
01514             }
01515 
01516     Output: {
01517               cm_return   - return code >0 if error
01518             }
01519     """
01520 
01521     import numpy as np
01522     import matplotlib as mpl
01523     mpl.use('Agg') # Abdul had a problem with his installation if XWindows is not installed
01524                    # So this line forces matplotlib to use internal engine without XWindows
01525 
01526     import matplotlib.pyplot as plt
01527     from scipy.stats import gaussian_kde
01528 
01529     font = {'family' : 'arial', 'weight' : 'normal', 'size'   : 10}
01530     r=mpl_get_available_fonts({})
01531     if 'arial' not in r['fonts']: del(font['family'])
01532     plt.rc('font', **font)
01533 
01534     # Check vars
01535     if 'ct_array' not in i: return {'cm_return':1, 'cm_error':'"ct_array" is not defined in "ctuning.space mpl_1d_density_graph"'}
01536     if 'ct_array_id' not in i: return {'cm_return':1, 'cm_error':'"ct_array_id" is not defined in "ctuning.space mpl_1d_density_graph"'}
01537     if 'ct_axes' not in i:  return {'cm_return':1, 'cm_error':'"ct_axes" is not defined in "ctuning.space mpl_1d_density_graph"'}
01538 
01539     cta=i['ct_array']
01540     ct_array_id=i['ct_array_id']
01541 
01542     # Prepare axes
01543     axes=i['ct_axes']
01544 
01545     axis_x=axes[0]
01546 
01547     axes_desc={}
01548     if 'ct_axes_desc' in i: axes_desc=i['ct_axes_desc']
01549 
01550     if axis_x in axes_desc: axis_x_d=axes_desc[axis_x]
01551 
01552     # Check if need to convert text axis to float 
01553     if i.get('convert_text_axes_to_float','')=='on':
01554        r=convert_text_axes_to_float({'ct_array':cta, 'ct_axes':axes, 'ct_axes_desc':axes_desc})
01555        if r['cm_return']>0: return r
01556        cta=r['ct_array']
01557 
01558     # Check that more than 2 points
01559     problem=False
01560     dmin=-1
01561     dmax=-1
01562     for p in range(0,len(cta)):
01563         if len(cta[p][0])<2:
01564            problem=True
01565            break
01566 
01567         if dmin==-1: dmin=min(cta[p][0])
01568         else: dmin=min(dmin, min(cta[p][0]))
01569 
01570         if dmax==-1: dmax=max(cta[p][0])
01571         else: dmax=max(dmax, max(cta[p][0]))
01572 
01573     if problem:
01574        cm_kernel.print_for_con('<BR><B>Problem: data selection should have more than 1 point</B><BR><BR>')
01575     else:
01576        # Plot graph
01577        gs=ini['cfg']['mpl_point_styles']
01578 
01579        mep=i.get('mpl_extra_params',{})
01580        sizex=mep.get('mpl_image_size_x','')
01581        if sizex=='': sizex='9'
01582        sizey=mep.get('mpl_image_size_y','')
01583        if sizey=='': sizey='5'
01584        dpi=mep.get('mpl_image_dpi','')
01585        if dpi=='': dpi='100'
01586   
01587        if sizex!='' and sizey!='' and dpi!='':
01588           fig=plt.figure(figsize=(int(sizex),int(sizey)))#, dpi=300)#int(dpi))
01589        else:
01590           fig=plt.figure()
01591 
01592        sp=fig.add_subplot(111)
01593        s=0
01594        for p in range(0,len(cta)):
01595            density = gaussian_kde(cta[p][0])
01596            xs = np.linspace(dmin,dmax,100)
01597            density.covariance_factor = lambda : .25
01598            density._compute_covariance()
01599            sp.plot(xs,density(xs))
01600            if len(gs)>(s+1): s+=1
01601 
01602        # Set axes names
01603        v=axes[0]
01604        if 'desc_text' in axis_x_d: v=axis_x_d['desc_text']
01605        plt.xlabel(v)
01606 
01607        plt.title('Powered by Collective Mind')
01608 
01609        if mep.get('mpl_detach_graph','')=='on': plt.show()
01610 
01611     ii={'ct_graph': 'mpl_1d_density_graph',
01612         'matplot_obj':plt}
01613     if 'url' in i: ii['url']=i['url']
01614     if 'reproduce' in i: ii['reproduce']=i['reproduce']
01615     if 'save_graph' in i: ii['save_graph']=i['save_graph']
01616     if 'cm_console' in i: ii['cm_console']=i['cm_console']
01617     if 'cm_file_ext' in i: ii['cm_file_ext']=i['cm_file_ext']
01618     return finish_python_graph(ii)
01619 
01620 # ============================================================================
01621 def mpl_2d_scatter_chart(i):
01622 
01623     """
01624     Python Matplot 2D scatter chart
01625 
01626     Input:  {
01627               ct_array                     - collective tuning array
01628               ct_array_id                  - array with space data points UIDs
01629               ct_axes                      - axes
01630               ct_axes_desc                 - axes description
01631               ct_points                    - selected entries
01632               reproduce                    - parameters to reproduce/save graph
01633               (url)                        - starting url of cM
01634               (save_graph)                 - if 'yes', save graph
01635               (cm_file_ext)                - file extension if save_graph=='yes'
01636               (convert_text_axes_to_float) - convert text axes to consequitive floats
01637               (mpl_extra_params)           - if python mat plot is used, this dictionary has extra params
01638                                                 mpl_image_size_x - image X size in inch
01639                                                 mpl_image_size_y - image Y size in inch
01640                                                 mpl_image_dpi    - image DPI
01641                                                 mpl_detach_graph - if 'on', detach graph
01642             }
01643 
01644     Output: {
01645               cm_return   - return code >0 if error
01646             }
01647     """
01648 
01649     import numpy as np
01650     import matplotlib as mpl
01651     mpl.use('Agg') # Abdul had a problem with his installation if XWindows is not installed
01652                    # So this line forces matplotlib to use internal engine without XWindows
01653 
01654     import matplotlib.pyplot as plt
01655 
01656     font = {'family' : 'arial', 'weight' : 'normal', 'size'   : 10}
01657     r=mpl_get_available_fonts({})
01658     if 'arial' not in r['fonts']: del(font['family'])
01659     plt.rc('font', **font)
01660 
01661     # Check vars
01662     if 'ct_array' not in i: return {'cm_return':1, 'cm_error':'"ct_array" is not defined in "ctuning.space mpl_2d_scatter_chart"'}
01663     if 'ct_array_id' not in i: return {'cm_return':1, 'cm_error':'"ct_array_id" is not defined in "ctuning.space mpl_2d_scatter_chart"'}
01664     if 'ct_axes' not in i:  return {'cm_return':1, 'cm_error':'"ct_axes" is not defined in "ctuning.space mpl_2d_scatter_chart"'}
01665 
01666     cta=i['ct_array']
01667     ct_array_id=i['ct_array_id']
01668 
01669     # Prepare axes
01670     axes=i['ct_axes']
01671 
01672     axis_x=axes[0]
01673     axis_y=axes[1]
01674 
01675     axes_desc={}
01676     if 'ct_axes_desc' in i: axes_desc=i['ct_axes_desc']
01677 
01678     if axis_x in axes_desc: axis_x_d=axes_desc[axis_x]
01679     if axis_y in axes_desc: axis_y_d=axes_desc[axis_y]
01680 
01681     # Check unique X axis type
01682     type_x=axes_desc.get(axes[0],{}).get('type','')
01683 
01684     # Check if need to convert text axis to float 
01685     if i.get('convert_text_axes_to_float','')=='on': 
01686        na=str(len(axes))
01687     else:
01688        na=1
01689 
01690     r=convert_text_axes_to_float({'ct_array':cta, 'ct_axes':axes, 'ct_axes_desc':axes_desc})
01691     if r['cm_return']>0: return r
01692     cta=r['ct_array']
01693 
01694     u=r['unique'][0]
01695     us=r['unique_string'][0]
01696 
01697     ind=np.arange(1,len(u)+1)
01698 
01699     ind1=[]
01700     for q in ind:
01701         ind1.append(q)
01702 
01703     # Plot graph
01704     gs=ini['cfg']['mpl_point_styles']
01705 
01706     mep=i.get('mpl_extra_params',{})
01707     sizex=mep.get('mpl_image_size_x','')
01708     if sizex=='': sizex='9'
01709     sizey=mep.get('mpl_image_size_y','')
01710     if sizey=='': sizey='5'
01711     dpi=mep.get('mpl_image_dpi','')
01712     if dpi=='': dpi='100'
01713 
01714     if sizex!='' and sizey!='' and dpi!='':
01715        fig=plt.figure(figsize=(int(sizex),int(sizey)))#, dpi=300)#int(dpi))
01716     else:
01717        fig=plt.figure()
01718 
01719     sp=fig.add_subplot(111)
01720 #    sp.set_yscale('log')
01721 
01722     s=0
01723     for p in range(0,len(cta)):
01724         sp.scatter(cta[p][0], cta[p][1], s=int(gs[s]['size']), edgecolor=gs[s]['color'], c=gs[s]['color'], marker=gs[s]['marker'])
01725         s+=1
01726         if s>=len(gs):s=0
01727 
01728     # Set axes names
01729     v=axes[0]
01730     if 'desc_text' in axis_x_d: v=axis_x_d['desc_text']
01731     plt.xlabel(v)
01732 
01733     v=axes[1]
01734     if 'desc_text' in axis_y_d: v=axis_y_d['desc_text']
01735     plt.ylabel(v)
01736 
01737     plt.title('Powered by Collective Mind')
01738 
01739     if mep.get('mpl_detach_graph','')=='on': 
01740        plt.show()
01741 
01742     ii={'ct_graph': 'mpl_2d_scatter_chart',
01743         'matplot_obj':plt}
01744     if 'url' in i: ii['url']=i['url']
01745     if 'reproduce' in i: ii['reproduce']=i['reproduce']
01746     if 'save_graph' in i: ii['save_graph']=i['save_graph']
01747     if 'cm_console' in i: ii['cm_console']=i['cm_console']
01748     if 'cm_file_ext' in i: ii['cm_file_ext']=i['cm_file_ext']
01749 
01750     ii['mpl_extra_params']=i.get('mpl_extra_params',{})
01751 
01752     # Print legends, etc, if needed
01753     legend=''
01754     if type_x=='text' or type_x=='uoa':
01755        gs=ini['cfg']['mpl_point_styles']
01756 
01757        legend='<small>'
01758 
01759        legend+='<br><B><i>Colors:&nbsp;</i></b>'
01760        s=0
01761        for p in range(0,len(cta)):
01762            # Load point
01763 
01764            legend+='<span style="color:'+gs[s]['color']+'">'+i['ct_points'][s]+'</span>&nbsp;;&nbsp;&nbsp;&nbsp; '
01765            s+=1
01766            if s>=len(gs):s=0
01767 
01768        legend+='<br><br><B><i>Axis&nbsp;X:&nbsp;</i></b>'
01769        for q in ind1:
01770           legend+='<b>'+str(q)+'</b>&nbsp;-&nbsp;'+us[q-1]+'&nbsp;;&nbsp;&nbsp;&nbsp; '
01771        legend+='<br>'
01772 
01773        legend+='<br></small>'
01774 
01775        ii['legend']=legend
01776 
01777     return finish_python_graph(ii)
01778 
01779 # ============================================================================
01780 def mpl_2d_column_chart(i):
01781 
01782     """
01783     Python Matplot 2D column chart
01784 
01785     Input:  {
01786               ct_array                     - collective tuning array
01787               ct_array_id                  - array with space data points UIDs
01788               ct_axes                      - axes
01789               ct_axes_desc                 - axes description
01790               ct_points                    - selected entries
01791               reproduce                    - parameters to reproduce/save graph
01792               (url)                        - starting url of cM
01793               (save_graph)                 - if 'yes', save graph
01794               (cm_file_ext)                - file extension if save_graph=='yes'
01795               (convert_text_axes_to_float) - convert text axes to consequitive floats
01796               (mpl_extra_params)           - if python mat plot is used, this dictionary has extra params
01797                                                 mpl_image_size_x - image X size in inch
01798                                                 mpl_image_size_y - image Y size in inch
01799                                                 mpl_image_dpi    - image DPI
01800                                                 mpl_detach_graph - if 'on', detach graph
01801             }
01802 
01803     Output: {
01804               cm_return   - return code >0 if error
01805             }
01806     """
01807 
01808     import numpy as np
01809     import matplotlib.pyplot as plt
01810 
01811     font = {'family' : 'arial', 'weight' : 'normal', 'size' : 10}
01812     r=mpl_get_available_fonts({})
01813     if 'arial' not in r['fonts']: del(font['family'])
01814     plt.rc('font', **font)
01815 
01816     # Check vars
01817     if 'ct_array' not in i: return {'cm_return':1, 'cm_error':'"ct_array" is not defined in "ctuning.space mpl_2d_column_chart"'}
01818     if 'ct_array_id' not in i: return {'cm_return':1, 'cm_error':'"ct_array_id" is not defined in "ctuning.space mpl_2d_column_chart"'}
01819     if 'ct_axes' not in i:  return {'cm_return':1, 'cm_error':'"ct_axes" is not defined in "ctuning.space mpl_2d_column_chart"'}
01820 
01821     cta=copy.deepcopy(i['ct_array'])
01822     ct_array_id=i['ct_array_id']
01823 
01824     # Prepare axes
01825     axes=i['ct_axes']
01826 
01827     axis_x=axes[0]
01828     axis_y=axes[1]
01829 
01830     axes_desc={}
01831     if 'ct_axes_desc' in i: axes_desc=i['ct_axes_desc']
01832 
01833     if axis_x in axes_desc: axis_x_d=axes_desc[axis_x]
01834     if axis_y in axes_desc: axis_y_d=axes_desc[axis_y]
01835 
01836     # Check unique X axis type
01837     type_x=axes_desc.get(axes[0],{}).get('type','')
01838 
01839     # Check if need to convert text axis to float 
01840     if i.get('convert_text_axes_to_float','')=='on': 
01841        na=str(len(axes))
01842     else:
01843        na=1
01844 
01845     r=convert_text_axes_to_float({'ct_array':cta, 'ct_axes':axes, 'ct_axes_desc':axes_desc})
01846     if r['cm_return']>0: return r
01847     cta=r['ct_array']
01848 
01849     u=r['unique'][0]
01850     us=r['unique_string'][0]
01851 
01852     ind=np.arange(1,len(u)+1)
01853 
01854     ind1=[]
01855     for q in ind:
01856         ind1.append(q)
01857 
01858     # Plot graph
01859     axes=i['ct_axes']
01860     width=0.9/len(cta)
01861    
01862     gs=ini['cfg']['mpl_point_styles']
01863 
01864     mep=i.get('mpl_extra_params',{})
01865     sizex=mep.get('mpl_image_size_x','')
01866     if sizex=='': sizex='9'
01867     sizey=mep.get('mpl_image_size_y','')
01868     if sizey=='': sizey='5'
01869     dpi=mep.get('mpl_image_dpi','')
01870     if dpi=='': dpi='100'
01871 
01872     if sizex!='' and sizey!='' and dpi!='':
01873        fig=plt.figure(figsize=(int(sizex),int(sizey)))#, dpi=300)#int(dpi))
01874     else:
01875        fig=plt.figure()
01876 
01877     sp=fig.add_subplot(111)
01878 
01879     sp.set_xticks(ind1)
01880     sp.set_xticklabels(ind1, rotation=-20)
01881 
01882     s=0
01883     n=0
01884     for p in range(0,len(cta)):
01885         x=[]
01886         for q in cta[p][0]:
01887             x.append(q+width*n) #-width+(q-1)*width+width*n)
01888 
01889         sp.bar(x, cta[p][1], width=width, edgecolor=gs[s]['color'], facecolor=gs[s]['color'], align='center')
01890         s+=1
01891         if s>=len(gs):s=0
01892 
01893         n+=1
01894 
01895     # Set axes names
01896     v=axes[0]
01897     if 'desc_text' in axis_x_d: v=axis_x_d['desc_text']
01898     plt.xlabel(v)
01899 
01900     v=axes[1]
01901     if 'desc_text' in axis_y_d: v=axis_y_d['desc_text']
01902     plt.ylabel(v)
01903 
01904     plt.title('Powered by Collective Mind')
01905 
01906     if mep.get('mpl_detach_graph','')=='on': plt.show()
01907 
01908 #    sp.autoscale(tight=True)
01909 
01910 #    plt.xticks(cta[0], i['ct_array'][0], rotation='vertical')
01911 
01912 #    for tick in plt.xaxis.get_major_ticks():
01913 #        tick.label.set_fontsize(14) 
01914 
01915 #    plt.axhline(y=1)
01916 #    plt.title('Title')
01917 
01918     ii={'ct_graph': 'mpl_2d_column_chart',
01919         'matplot_obj':plt}
01920     if 'url' in i: ii['url']=i['url']
01921     if 'reproduce' in i: ii['reproduce']=i['reproduce']
01922     if 'save_graph' in i: ii['save_graph']=i['save_graph']
01923     if 'cm_console' in i: ii['cm_console']=i['cm_console']
01924     if 'cm_file_ext' in i: ii['cm_file_ext']=i['cm_file_ext']
01925 
01926     ii['mpl_extra_params']=i.get('mpl_extra_params',{})
01927 
01928     # Print legends, etc, if needed
01929     if type_x=='text' or type_x=='uoa':
01930        legend='<br><small><B><i>Axis&nbsp;X:&nbsp;</i></b>'
01931        for q in ind1:
01932           legend+='<b>'+str(q)+'</b>&nbsp;-&nbsp;'+us[q-1]+'&nbsp;;&nbsp;&nbsp;&nbsp; '
01933        legend+='<br>'
01934 
01935        legend+='<br><B><i>Colors:&nbsp;</i></b>'
01936        s=0
01937        for p in range(0,len(cta)):
01938            legend+='<span style="color:'+gs[s]['color']+'">'+i['ct_points'][s]+'</span>&nbsp;;&nbsp;&nbsp;&nbsp; '
01939            s+=1
01940            if s>=len(gs):s=0
01941 
01942        legend+='<br></small>'
01943 
01944        ii['legend']=legend
01945 
01946     return finish_python_graph(ii)
01947 
01948 # ============================================================================
01949 def finish_python_graph(i):
01950 
01951     """
01952     Finish python graph and save to file
01953 
01954     Input:  {
01955               ct_graph     - graph type
01956               matplot_obj  - prepared MatPlot object
01957               cm_file_ext  - file extension
01958               (url)        - starting url of cM
01959               (reproduce)  - parameters to reproduce/save graph
01960               (save_graph) - prepare for saving
01961               (cm_console) - if 'txt' output path to the generated image to the console
01962 
01963               (legend)     - if !='', add to the end of the graph
01964 
01965               (mpl_extra_params)           - if python mat plot is used, this dictionary has extra params
01966                                                 mpl_image_size_x - image X size in inch
01967                                                 mpl_image_size_y - image Y size in inch
01968                                                 mpl_image_dpi    - image DPI
01969             }
01970 
01971     Output: {
01972               cm_return   - return code >0 if error
01973             }
01974     """
01975     # Check vars
01976     if 'matplot_obj' not in i: return {'cm_return':1, 'cm_error':'"matplot_obj" is not defined in "ctuning.space finish_python_graph"'}
01977 
01978     tp="png"
01979     if 'cm_file_ext' in i and i['cm_file_ext']!='': tp=i['cm_file_ext']
01980 
01981     plt=i['matplot_obj']
01982 
01983     # Generate tmp file
01984     r=cm_kernel.gen_cm_tmp_file({})
01985     if r['cm_return']>0: return r
01986     tf=r['cm_path']
01987     uid=r['cm_uid']
01988 
01989     # Save image
01990     dpi=i.get('mpl_extra_params',{}).get('mpl_image_dpi','')
01991     if dpi!='':
01992        plt.savefig(tf, format=tp, dpi=int(dpi))
01993     else:
01994        plt.savefig(tf, format=tp)
01995 
01996     rr={'cm_return':0}
01997 
01998     if i.get('cm_console','')=='txt':
01999        cm_kernel.print_for_con(tf)
02000     else:
02001        if i.get('save_graph','')=='yes':
02002           rr['cm_full_filename']=tf
02003 
02004           rr['cm_web_download']='yes'
02005           rr['cm_web_set_headers']='yes'
02006 
02007           # Prepare content header
02008           ii1={'cm_run_module_uoa':ini['cfg']['cm_modules']['cm-web'],
02009                'cm_action':'prepare_content_header',
02010                'cm_web_content':'.'+tp,
02011                'cm_filename':tf}
02012           r=cm_kernel.access(ii1)
02013           if r['cm_return']>0: return r
02014 
02015           # Substitute special variables
02016           wh=[]
02017           for x in r['cm_web_headers']:
02018               wh.append(x.replace(cm_kernel.convert_str_to_special('filename'), 'figure.'+tp))
02019 
02020           rr['cm_web_headers']=wh
02021 
02022           rr['cm_web_postprocess']='no'
02023 
02024           rr['cm_delete_file_after_view']='yes' # for tmp files!
02025 
02026        else:
02027           if 'url' in i:
02028              url=i['url']
02029           else:
02030              # Check styles, etc
02031              if 'cfg' in cm_kernel.ini['web_style']: web=cm_kernel.ini['web_style']['cfg']
02032              else:
02033                 return {'cm_return':1, 'cm_error':'web style is not defined'}
02034              url=web['http_prefix']
02035 
02036           filename=os.path.basename(tf)
02037 
02038           # Link to generate pdf
02039           rep=''
02040           if 'reproduce' in i: rep=i['reproduce']
02041           cm_kernel.print_for_con('<center>Save as ')
02042           cm_kernel.print_for_web(' <a href="'+url+'cm_web_module_uoa=ctuning.space&cm_web_action=visualize_selection&cm_file_ext=png&save_graph=yes&cm_json='+rep+'" target="_top">png</a>')
02043           cm_kernel.print_for_web(' <a href="'+url+'cm_web_module_uoa=ctuning.space&cm_web_action=visualize_selection&cm_file_ext=eps&save_graph=yes&cm_json='+rep+'" target="_top">eps</a>')
02044           cm_kernel.print_for_web(' <a href="'+url+'cm_web_module_uoa=ctuning.space&cm_web_action=visualize_selection&cm_file_ext=pdf&save_graph=yes&cm_json='+rep+'" target="_top">pdf</a>')
02045           cm_kernel.print_for_con('</center>')
02046 
02047           cm_kernel.print_for_web('<img src="'+url+'cm_web_action=show&cm_module_uoa=web&cm_web_filename='+filename+'&cm_web_content=.'+tp+'">')
02048 
02049           if i.get('legend','')!='':
02050              cm_kernel.print_for_web('<br>'+i['legend']+'<br>')
02051 
02052     return rr
02053 
02054 # ============================================================================
02055 def convert_text_axes_to_float(i):
02056 
02057     """
02058     Finish python graph and save to file
02059 
02060     Input:  {
02061               ct_array     - collective tuning array
02062               ct_axes      - axes
02063               ct_axes_desc - axes description
02064               (num_axes)   - number of axes to process (to allow to convert only X sometimes)
02065             }
02066 
02067     Output: {
02068               cm_return     - return code >0 if error
02069               ct_array      - updated ct array
02070               unique        - if converted from string to number, unique values
02071               unique_string - if converted from string to number, unique values converted to string
02072             }
02073     """
02074 
02075     cta=i['ct_array']
02076     axes=i['ct_axes']
02077     axes_desc=i['ct_axes_desc']
02078     na=int(i.get('num_axes',len(axes)))
02079 
02080     unique=[]
02081     unique_string=[]
02082 
02083     # Check if need to convert text axis to float 
02084     for q in range(0, na):
02085         unique.append([])
02086         unique_string.append([])
02087         if 'type' in axes_desc.get(axes[q],{}):
02088            tp=axes_desc[axes[q]]['type']
02089            if tp!='float':
02090               if tp=='text' or tp=='uoa':
02091                  tmp={}; itmp=1
02092                  for p in range(0, len(cta)):
02093                      for qq in range(0, len(cta[p][q])):
02094                          if cta[p][q][qq] in tmp: 
02095                             v=tmp[cta[p][q][qq]]
02096                          else: 
02097                             v=cta[p][q][qq]
02098 
02099                             unique[q].append(v)
02100                             unique_string[q].append(str(v))
02101 
02102                             tmp[v]=itmp
02103                             v=itmp
02104                             itmp+=1
02105                          cta[p][q][qq]=float(v)
02106 
02107     return {'cm_return':0, 'ct_array':cta, 'unique':unique, 'unique_string':unique_string}
02108 
02109 # ============================================================================
02110 def convert_points_to_flat_csv(i):
02111 
02112     """
02113     Convert points into flat CSV (possibility to choose dimensions).
02114     Needed for machine learning and data mining.
02115 
02116     Input:  {
02117               ct_data_uoa                    - Entry with points
02118               (ct_data_selection)            - Multiple entries if needed
02119               (ct_data_selection_from_files) - Multiple entries from json files prepared by this function
02120               (ct_module_uoa)                - Specify module from scenarios 
02121                                                (otherwise ctuning.space by default)
02122               (use_multiple_graphs)          - if 'on' spread data from ct_data_selection into different dimensions
02123               (ct_dimensions)                - List of dimensions to add
02124               (ct_repo_uoa)                  - Repository
02125               (ct_output_file_s_json)        - output json file (values as strings)
02126               (ct_output_file_json)          - output json file (real values)
02127               (ct_output_file_csv)           - output csv file
02128               (csv_separator)                - separator for data in csv (';' by default)
02129               (csv_decimal_mark)             - decimal char (default ',')
02130             }
02131 
02132     Output: {
02133               cm_return        - return code >0 if error
02134               ct_multi_array_s - list of dictionaries per multi-graph of format 'flat_dimension1':['value1', 'value2', ...],
02135                                                                                 'flat_dimension2':[...],
02136                                                                                 ...
02137               ct_multi_array   - list of dictionaries per multi-graph of format 'flat_dimension1':[value1, value2, ...],
02138                                                                                 'flat_dimension2':[...]
02139                                                                                 ...
02140                                          where all values are based on cM dimensions description in a note
02141               ct_dim_desc      - dictionary of format {'flat_dimension': cM dimension description (categorical/continuous/etc)
02142             }
02143     
02144     Note:   cM dimensions description = {
02145                                           type           - "text", "integer", "uoa", "float", etc
02146                                           data_desc      - textual description of dimension
02147                                           has_choice     - if 'yes', this dimension is categorical
02148                                           choice"        - [list of choices]
02149 
02150                                           explore_start  - if !='', dimension is continuous; min  value
02151                                           explore_stop   -                                   max  value
02152                                           explore_step   -                                   step
02153                                           explore_prefix - add prefix before dimension 
02154                                                            (useful for compiler flags with conintuous parameters)        
02155 
02156                                   }
02157     """
02158 
02159     data1=i.get('ct_data_uoa','')
02160     data=i.get('ct_data_selection',[])
02161     df=False # data from files
02162     if data1!='': data.append(data1)
02163     if len(data)==0:
02164        data=i.get('ct_data_selection_from_files',[])
02165        df=True
02166 
02167     if len(data)==0:
02168        return {'cm_return':1, 'cm_error':'no data selection'}
02169 
02170     repo=i.get('ct_repo_uoa','')
02171 
02172     module=i.get('ct_module_uoa','')
02173 
02174     dim=i.get('ct_dimensions',[])
02175 
02176     sep=i.get('csv_separator',';')
02177 
02178     # Go through all data
02179     csv_s={} # per characteristic (strings)
02180     csv={} # per characteristic (real values)
02181 
02182     umg=i.get('use_multiple_graphs','')
02183     mcsv_s=[] # separated points (strings)
02184     mcsv=[] # separated points (real values)
02185 
02186     max_len=0
02187     keys=[]
02188     dim_desc={}
02189 
02190     npoints=0
02191 
02192     if df: 
02193        for q in data:
02194            cm_kernel.print_for_con('Processing file '+q+' ...')
02195 
02196            # Load data
02197            r=cm_kernel.load_json_file({'cm_filename':q})
02198            if r['cm_return']>0: return r
02199 
02200            a=r['cm_array']
02201            csv_s=a['data']
02202            if len(dim_desc)==0 and len(a['desc'])!=0: dim_desc=a['desc']
02203 
02204            # Preparing csv (values)
02205            for k in csv_s:
02206                kv=csv_s[k]
02207                csv[k]=[]
02208                for v in kv:
02209                    # Check according to description (or float if not there)
02210                    fv=None
02211                    if v!='': 
02212                       desc=dim_desc.get(k,{})
02213                       if len(desc)==0: 
02214                          fv=float(v)
02215                       else:
02216                          t=desc.get('type','')
02217                          if t=='float': fv=float(v)
02218                          elif t=='integer': fv=int(v)
02219                          else: fv=v
02220 
02221                    csv[k].append(fv)
02222 
02223            if umg=='on':
02224               mcsv_s.append(csv_s)
02225               mcsv.append(csv)
02226               csv_s={}
02227               csv={}
02228      
02229     else:
02230        for q in data:
02231            cm_kernel.print_for_con('')
02232            cm_kernel.print_for_con('Processing entry '+q+' ...')
02233 
02234            # Load data
02235            ii={'cm_run_module_uoa':ini['cm_module_uid'],
02236                'cm_action':'load',
02237                'cm_data_uoa':q}
02238            if repo!='': ii['cm_repo_uoa']=repo
02239            if module!='': ii['cm_run_module_uoa']=module
02240            r=cm_kernel.access(ii)
02241            if r['cm_return']>0: return r
02242 
02243            d=r['cm_data_obj']['cfg']
02244 
02245            points=d['points']
02246 
02247            cm_kernel.print_for_con('')
02248            ip=0
02249            npoints=len(points)
02250            for p in points:
02251                ip+=1
02252                cm_kernel.print_for_con('Processing point '+str(ip)+' of '+str(len(points))+' ...')
02253 
02254                # flatten dictionary
02255                r=cm_kernel.flatten_array({'cm_array':p})
02256                if r['cm_return']==0:
02257                   a=r['cm_array']
02258 
02259                   # If first read, initialize various things including keys and descriptions
02260                   if max_len==0: 
02261                      cm_kernel.print_for_con('  Initializing various arrays and detecting descriptions ...')
02262 
02263                      max_len=len(a)
02264                      if len(dim)==0: dim=a.keys()
02265 
02266                      pipeline=d.get('pipeline_module_uoa','')
02267                      if pipeline!='':
02268                         # Load pipeline module
02269                         r=cm_kernel.access({'cm_run_module_uoa':ini['cfg']['cm_modules']['cm-module'],
02270                                             'cm_action':'load',
02271                                             'cm_data_uoa':pipeline})
02272                         if r['cm_return']>0: return r
02273                         p_cfg=r['cm_data_obj']['cfg']
02274 
02275                         pd=p_cfg.get('pipeline_desc',{})
02276 
02277                         # Prepare description by pipeline and input !!!
02278                         # HERE pd (description) will be updated according to the input!!!
02279                         ii={'cm_run_module_uoa':ini['cfg']['cm_modules']['cm-web'],
02280                             'cm_action':'visualize_data',
02281                             'cm_array':p.get('pipeline_input',{}).get('state',{}),
02282                             'cm_data_desc':pd,
02283                             'cm_separator':'#',
02284                             'cm_separator_form':'#form1#',
02285                             'cm_forms_exists':'yes',
02286                             'cm_mode':'add'}
02287                         r=cm_kernel.access(ii)
02288                         if r['cm_return']>0: return r
02289 
02290                         for k in pd:
02291                             v=pd[k]
02292                             k1=k[1:]
02293 
02294                             dim_desc['##pipeline_input#state'+k1]=v
02295                             dim_desc['##pipeline_output#state'+k1]=v
02296 
02297                   for k in dim:
02298                       if k not in a:
02299                          csv_s[k].append('')
02300                          csv[k].append(None)
02301                       else:
02302                          if k not in csv_s: 
02303                             csv_s[k]=[]
02304                             csv[k]=[]
02305                          v=a[k]
02306                          csv_s[k].append(v)
02307 
02308                          # Check according to description (or float if not there)
02309                          fv=None
02310                          if v!='': 
02311                             desc=dim_desc.get(k,{})
02312                             if len(desc)==0: 
02313                                fv=float(v)
02314                             else:
02315                                t=desc.get('type','')
02316                                if t=='float': fv=float(v)
02317                                elif t=='integer': fv=int(v)
02318                                else: fv=v
02319 
02320                          csv[k].append(fv)
02321 
02322            if umg=='on':
02323               mcsv_s.append(csv_s)
02324               mcsv.append(csv)
02325               csv_s={}
02326               csv={}
02327 
02328     # If multi-graph is off, prepare at least one dimension
02329     if umg!='on':
02330        mcsv_s.append(csv_s)
02331        mcsv.append(csv)
02332 
02333     # check saving to json (values as strings)
02334     fx=i.get('ct_output_file_s_json','')
02335     if fx!='':
02336        cm_kernel.print_for_con('')
02337        cm_kernel.print_for_con('Recording to json file '+fx+' ...')
02338 
02339        r=cm_kernel.save_array_to_file_as_json({'cm_filename':fx, 'cm_array':{'data':mcsv_s[0], 'desc':dim_desc}})
02340        if r['cm_return']>0: return r
02341 
02342     # check saving to json (real values)
02343     fx=i.get('ct_output_file_json','')
02344     if fx!='':
02345        cm_kernel.print_for_con('')
02346        cm_kernel.print_for_con('Recording to json file '+fx+' ...')
02347 
02348        r=cm_kernel.save_array_to_file_as_json({'cm_filename':fx, 'cm_array':{'data':mcsv[0], 'desc':dim_desc}})
02349        if r['cm_return']>0: return r
02350 
02351     # check saving to csv
02352     fx=i.get('ct_output_file_csv','')
02353     if fx!='':
02354        ii={'cm_multi_array_s':mcsv_s[0],
02355            'ct_dimensions':dim,
02356            'ct_dim_desc':dim_desc,
02357            'ct_output_file':fx,
02358            'csv_separator':i.get('csv_separator',''),
02359            'csv_decimal_mark':i.get('csv_decimal_mark','')
02360           }
02361        r=convert_ct_multi_array_to_csv(ii)
02362        if r['cm_return']>0: return r
02363 
02364     return {'cm_return':0, 'ct_multi_array_s':mcsv_s, 'ct_multi_array':mcsv, 'ct_dim_desc':dim_desc}
02365 
02366 # ============================================================================
02367 def convert_ct_multi_array_to_csv(i):
02368 
02369     """
02370     Convert cTuning multi-dimensional array to csv
02371 
02372     Input:  {
02373               cm_multi_array_s   - array of strings to convert
02374               ct_dimensions      - list of dimensions to add
02375               ct_dim_desc        - dimension description (categorical/continuous, etc)
02376               ct_output_file     - output csv file
02377               (csv_separator)    - separator for data in csv (';' by default)
02378               (csv_decimal_mark) - decimal char (default ',')
02379               (csv_no_header)    - if 'yes', do not add header
02380             }
02381 
02382     Output: {
02383               cm_return        - return code >0 if error
02384             }
02385     
02386     """
02387 
02388     csv_s=i.get('cm_multi_array_s',{})
02389 
02390     dim=i.get('ct_dimensions',[])
02391     if len(dim)==0: dim=csv_s.keys()
02392 
02393     dim_desc=i.get('ct_dim_desc',{})
02394 
02395     fx=i.get('ct_output_file','')
02396 
02397     sep=i.get('csv_separator',';')
02398     if sep=='': sep=';'
02399 
02400     dec=i.get('csv_decimal_mark',',')
02401     if dec=='': dec=','
02402 
02403     c=''
02404     cm_kernel.print_for_con('')
02405     cm_kernel.print_for_con('Recording to csv file '+fx+' ...')
02406 
02407     # Prepare description line
02408     line=''
02409     if i.get('csv_no_header','')!='yes':
02410        for q in dim:
02411            if line!='': line+=sep
02412            if q in dim_desc and dim_desc[q].get('desc_text','')!='':
02413                 q=dim_desc[q]['desc_text']
02414            line+='"'+q+'"'
02415        c+=line+'\n'
02416 
02417     first_key=csv_s.keys()[0]
02418 
02419     # Finish all data
02420     for q in range(0, len(csv_s[first_key])):
02421         line=''
02422         for d in dim:
02423             if line!='': line+=sep
02424             v=csv_s[d][q]
02425 
02426             desc=dim_desc.get(d,{})
02427             if len(desc)==0: fv=float(v)
02428             else:
02429                t=desc.get('type','')
02430                if t=='float' or t=='integer': v=v.replace('.', dec) # integer here is because sometimes it is represented as 10.0
02431                else: v='"'+v+'"'
02432             line+=v
02433         c+=line+'\n'
02434 
02435     try:
02436        f=open(fx,'wt')
02437        f.write(c+'\n');
02438        f.close()
02439     except Exception as e:
02440        return {'cm_return':1, 'cm_error':'problem writing csv file ('+format(e)+')'}
02441 
02442     return {'cm_return':0}
02443 
02444 # ============================================================================
02445 def mpl2_2d_warm_graph_a(i):
02446 
02447     """
02448     Plot graph from points
02449 
02450     Input:  {
02451               The same as in 'convert_points_to_flat_csv'
02452             }
02453 
02454     Output: {
02455               cm_return        - return code >0 if error
02456             }
02457     
02458     """
02459 
02460     # Convert to flat array ready for machine learning and data mining
02461     r=convert_points_to_flat_csv(i)
02462     if r['cm_return']>0: return r
02463 
02464     ii={'ct_multi_array':r.get('ct_multi_array',[]),
02465         'ct_dimensions':i.get('ct_dimensions',[])}
02466 
02467     return mpl2_2d_warm_graph(ii)
02468 
02469 # ============================================================================
02470 def mpl2_2d_warm_graph(i):
02471 
02472     """
02473     Plot graph from points
02474 
02475     Input:  {
02476               ct_multi_array - see output of 'convert_points_to_flat_csv'
02477               ct_dimensions
02478             }
02479 
02480     Output: {
02481               cm_return        - return code >0 if error
02482             }
02483     
02484     """
02485 
02486     import numpy as np
02487     from matplotlib.mlab import griddata
02488     from matplotlib import cm
02489 
02490     import matplotlib.pyplot as plt
02491     from mpl_toolkits.mplot3d import Axes3D
02492 
02493     ctma=i.get('ct_multi_array',[])[0]
02494     dim=i.get('ct_dimensions',[])
02495 
02496     # Pruning points with None
02497     npoints=len(ctma[dim[0]])
02498     for q in range(npoints-1, 0, -1):
02499         if ctma[dim[0]][q]==None or ctma[dim[1]][q]==None or ctma[dim[2]][q]==None:
02500            del(ctma[dim[0]][q])
02501            del(ctma[dim[1]][q])
02502            del(ctma[dim[2]][q])
02503 
02504     xx=ctma[dim[0]] #[1,2,3]
02505     yy=ctma[dim[1]] #[1,2,3]
02506     zz=ctma[dim[2]] #[1,2,3]
02507 
02508     # predict
02509 #    for q in range(0, npoints):
02510 #        zz[q]=1.905824  +  0.001305469 * np.maximum(0, 400 -  xx[q])   - 9.017562e-05 * np.maximum(0,  yy[q] - 500)   -  0.004044664 * np.maximum(0, 500 -  yy[q]) 
02511 
02512     x=np.array(xx)
02513     y=np.array(yy)
02514     z=np.array(zz)
02515 
02516     X, Y = np.meshgrid(x, y)
02517 
02518     Z=griddata(xx,yy,zz,x,y)
02519 
02520 
02521 #    sp.plot_wireframe(x,y,z)
02522 #    sp.scatter(x,y,z)
02523 
02524 #    fig = plt.figure()
02525 #    plt.gca().invert_xaxis()
02526 #    plt.gca().invert_yaxis()
02527 #    sp=fig.add_subplot(111, projection='3d')
02528 #    surf=sp.plot_surface(X,Y,Z,vmin=abs(Z).min(), vmax=abs(Z).max(), cmap=cm.coolwarm, antialiased=True)
02529 #    fig.colorbar(surf)
02530 #    fig.show()
02531 
02532 #    surf = plt.imshow(Z, vmin=abs(Z).min(), vmax=abs(Z).max(), cmap=cm.coolwarm, interpolation="nearest")
02533 #    plt.colorbar(surf, orientation='horizontal')
02534 #    plt.show()
02535 
02536     x=plt.scatter(X, Y, s=300, c=Z, edgecolor='face')
02537     plt.colorbar(x, orientation='horizontal')
02538     plt.show()
02539 
02540     x=raw_input('Press any key to continue')
02541 
02542     return {'cm_return':0}
02543 
02544 # ============================================================================
02545 def mpl2_2d_scatter_chart_a(i):
02546 
02547     """
02548     Plot graph from points
02549 
02550     Input:  {
02551               The same as in 'convert_points_to_flat_csv'
02552             }
02553 
02554     Output: {
02555               cm_return        - return code >0 if error
02556             }
02557     
02558     """
02559 
02560     # Convert to flat array ready for machine learning and data mining
02561     r=convert_points_to_flat_csv(i)
02562     if r['cm_return']>0: return r
02563 
02564     ctma=r.get('ct_multi_array',[])
02565 
02566     ii={'ct_multi_array':ctma,
02567         'ct_dimensions':i.get('ct_dimensions',[])}
02568 
02569     r=mpl2_2d_scatter_chart(ii)
02570 #    rp=r['reuse_plot']
02571 
02572 #    rp['plt'].plot([1,4000],[1,12])
02573 #    rp['plt'].grid(True)
02574 #
02575 #    ii['reuse_plot']=rp
02576 #
02577 #    ii['ct_multi_array']=[ctma[1]]
02578 #    r=mpl2_2d_scatter_chart(ii)
02579 #    r=mpl2_2d_scatter_chart(ii)
02580     return r
02581 
02582 # ============================================================================
02583 def mpl_get_available_fonts(i):
02584 
02585     """
02586     Get available TTF fonts in matplot
02587 
02588     Input:  {
02589             }
02590 
02591     Output: {
02592               cm_return   - return code >0 if error
02593               fonts       - list of fonts (without path and ttf extension
02594             }
02595     """
02596 
02597     import matplotlib.font_manager
02598 
02599     fn=matplotlib.font_manager.findSystemFonts(fontpaths=None, fontext='ttf')
02600     fn1=[]
02601 
02602     for q in fn:
02603         q1=os.path.basename(q)
02604         q3a, q3b = os.path.splitext(q1)
02605         fn1.append(q3a)
02606 
02607     return {'cm_return':0, 'fonts':fn1}
02608 
02609 # ============================================================================
02610 def mpl2_2d_scatter_chart(i):
02611 
02612     """
02613     Python Matplot 2D scatter chart
02614 
02615     Input:  {
02616               reuse_plot
02617             }
02618 
02619     Output: {
02620               cm_return   - return code >0 if error
02621             }
02622     """
02623 
02624     import numpy as np
02625 
02626     gs=ini['cfg']['mpl_point_styles']
02627 
02628     rp=i.get('reuse_plot',{})
02629     if len(rp)==0:
02630        import matplotlib as mpl
02631        import matplotlib.pyplot as plt
02632 
02633        font = {'family' : 'arial', 'weight' : 'normal', 'size'   : 10}
02634        r=mpl_get_available_fonts({})
02635        if 'arial' not in r['fonts']: del(font['family'])
02636        plt.rc('font', **font)
02637 
02638        plt.ion()
02639 
02640        mep=i.get('mpl_extra_params',{})
02641        sizex=mep.get('mpl_image_size_x','')
02642        if sizex=='': sizex='9'
02643        sizey=mep.get('mpl_image_size_y','')
02644        if sizey=='': sizey='5'
02645        dpi=mep.get('mpl_image_dpi','')
02646        if dpi=='': dpi='100'
02647 
02648        if sizex!='' and sizey!='' and dpi!='':
02649           fig=plt.figure(figsize=(int(sizex),int(sizey)))#, dpi=300)#int(dpi))
02650        else:
02651           fig=plt.figure()
02652 
02653        sp=fig.add_subplot(111)
02654     else:
02655        plt=rp['plt']
02656        plt.ion()
02657        fig=rp['fig']
02658        sp=rp['sp']
02659 
02660     dim=i.get('ct_dimensions',[])
02661 
02662     ctmax=i.get('ct_multi_array',[])
02663     s=0
02664     for ctma in ctmax:
02665 
02666         # Pruning points with None
02667         npoints=len(ctma[dim[0]])
02668         for q in range(npoints-1, 0, -1):
02669             if ctma[dim[0]][q]==None or ctma[dim[1]][q]==None:
02670                del(ctma[dim[0]][q])
02671                del(ctma[dim[1]][q])
02672 
02673         xx=ctma[dim[0]] #[1,2,3]
02674         yy=ctma[dim[1]] #[1,2,3]
02675 
02676         x=np.array(xx)
02677         y=np.array(yy)
02678 
02679         sp.scatter(xx, yy, s=int(gs[s]['size']), edgecolor=gs[s]['color'], c=gs[s]['color'], marker=gs[s]['marker'])
02680         s+=1
02681         if s>=len(gs):s=0
02682 
02683         sp.grid(True)
02684 
02685     plt.draw()
02686 
02687     raw_input('Press any key to continue')
02688 
02689     return {'cm_return':0, 'reuse_plot':{'plt':plt, 'fig':fig, 'sp':sp}}

Generated on Wed May 28 02:49:02 2014 for Collective Mind Framework by DoxyGen 1.6.1
Concept, design and coordination: Grigori Fursin (C) 1993-2013