<s> from __future__ import division <EOL> from vistrails . core . bundles . pyimport import py_import <EOL> from vistrails . core . modules . basic_modules import List , ListType <EOL> from vistrails . core . modules . config import ModuleSettings <EOL> from vistrails . core . modules . output_modules import OutputModule , FileMode , IPythonMode <EOL> from vistrails . core . modules . vistrails_module import Module , ModuleError , Converter <EOL> _numpy = False <EOL> def get_numpy ( required = True ) : <EOL> """<STR_LIT>""" <EOL> global _numpy <EOL> if _numpy is False : <EOL> try : <EOL> _numpy = py_import ( <EOL> '<STR_LIT>' , { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } , <EOL> store_in_config = not required ) <EOL> except ImportError : <EOL> _numpy = None <EOL> if _numpy is None and required : <EOL> raise ImportError ( "<STR_LIT>" ) <EOL> return _numpy <EOL> class InternalModuleError ( Exception ) : <EOL> """<STR_LIT>""" <EOL> def raise_module_error ( self , module_obj ) : <EOL> raise ModuleError ( module_obj , self . message ) <EOL> class TableObject ( object ) : <EOL> columns = None <EOL> rows = None <EOL> names = None <EOL> name = None <EOL> def __init__ ( self , columns , nb_rows , names ) : <EOL> self . columns = len ( columns ) <EOL> self . rows = nb_rows <EOL> self . names = names <EOL> self . _columns = columns <EOL> def get_column ( self , i , numeric = False ) : <EOL> """<STR_LIT>""" <EOL> numpy = get_numpy ( False ) <EOL> if numeric and numpy is not None : <EOL> return numpy . array ( self . _columns [ i ] , dtype = numpy . float32 ) <EOL> else : <EOL> return self . _columns [ i ] <EOL> def get_column_by_name ( self , name , numeric = False ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> col = self . names . index ( name ) <EOL> except ValueError : <EOL> raise KeyError ( name ) <EOL> else : <EOL> return self . get_column ( col , numeric ) <EOL> @ classmethod <EOL> def from_dicts ( cls , dicts , keys = None ) : <EOL> iterator = iter ( dicts ) <EOL> try : <EOL> first = next ( iterator ) <EOL> except StopIteration : <EOL> if keys is None : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> return cls ( [ [ ] ] * len ( keys ) , <NUM_LIT:0> , list ( keys ) ) <EOL> if keys is None : <EOL> keys = first . keys ( ) <EOL> columns = [ [ first [ key ] ] for key in keys ] <EOL> count = <NUM_LIT:1> <EOL> for dct in iterator : <EOL> for i , key in enumerate ( keys ) : <EOL> try : <EOL> v = dct [ key ] <EOL> except KeyError : <EOL> raise ValueError ( "<STR_LIT>" % ( count , key ) ) <EOL> else : <EOL> columns [ i ] . append ( v ) <EOL> count += <NUM_LIT:1> <EOL> return cls ( columns , count , keys ) <EOL> class Table ( Module ) : <EOL> _input_ports = [ ( '<STR_LIT:name>' , '<STR_LIT>' ) ] <EOL> _output_ports = [ ( '<STR_LIT:value>' , '<STR_LIT>' ) ] <EOL> def set_output ( self , port_name , value ) : <EOL> if self . list_depth == <NUM_LIT:0> and value is not None and port_name == '<STR_LIT:value>' : <EOL> if value . name is None : <EOL> value . name = self . force_get_input ( '<STR_LIT:name>' , None ) <EOL> Module . set_output ( self , port_name , value ) <EOL> def choose_column ( nb_columns , column_names = None , name = None , index = None ) : <EOL> """<STR_LIT>""" <EOL> if name is not None : <EOL> if isinstance ( name , unicode ) : <EOL> name = name . encode ( '<STR_LIT:utf-8>' ) <EOL> if column_names is None : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> try : <EOL> name_index = column_names . index ( name ) <EOL> except ValueError : <EOL> try : <EOL> name_index = column_names . index ( name . strip ( ) ) <EOL> except ValueError : <EOL> raise ValueError ( "<STR_LIT>" % name ) <EOL> if index is not None : <EOL> if name_index != index : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> return name_index <EOL> elif index is not None : <EOL> if index < <NUM_LIT:0> or index >= nb_columns : <EOL> raise ValueError ( "<STR_LIT>" % ( <EOL> index , nb_columns ) ) <EOL> return index <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> def choose_columns ( nb_columns , column_names = None , names = None , indexes = None ) : <EOL> """<STR_LIT>""" <EOL> if names is not None : <EOL> if column_names is None : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> result = [ ] <EOL> for name in names : <EOL> if isinstance ( name , unicode ) : <EOL> name = name . encode ( '<STR_LIT:utf-8>' ) <EOL> try : <EOL> idx = column_names . index ( name ) <EOL> except ValueError : <EOL> try : <EOL> idx = column_names . index ( name . strip ( ) ) <EOL> except ValueError : <EOL> raise ValueError ( "<STR_LIT>" % name ) <EOL> result . append ( idx ) <EOL> if indexes is not None : <EOL> if result != indexes : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> return result <EOL> elif indexes is not None : <EOL> for index in indexes : <EOL> if index < <NUM_LIT:0> or index >= nb_columns : <EOL> raise ValueError ( "<STR_LIT>" % ( <EOL> index , nb_columns ) ) <EOL> return indexes <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> class ExtractColumn ( Module ) : <EOL> """<STR_LIT>""" <EOL> _input_ports = [ <EOL> ( '<STR_LIT>' , Table ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , <EOL> { '<STR_LIT>' : True } ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , <EOL> { '<STR_LIT>' : True } ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , <EOL> { '<STR_LIT>' : True , '<STR_LIT>' : "<STR_LIT>" } ) ] <EOL> _output_ports = [ <EOL> ( '<STR_LIT:value>' , '<STR_LIT>' ) ] <EOL> def compute ( self ) : <EOL> table = self . get_input ( '<STR_LIT>' ) <EOL> try : <EOL> column_idx = choose_column ( <EOL> table . columns , <EOL> column_names = table . names , <EOL> name = self . force_get_input ( '<STR_LIT>' , None ) , <EOL> index = self . force_get_input ( '<STR_LIT>' , None ) ) <EOL> self . set_output ( '<STR_LIT:value>' , table . get_column ( <EOL> column_idx , <EOL> self . get_input ( '<STR_LIT>' , allow_default = True ) ) ) <EOL> except ValueError , e : <EOL> raise ModuleError ( self , e . message ) <EOL> class BuildTable ( Module ) : <EOL> """<STR_LIT>""" <EOL> _settings = ModuleSettings ( configure_widget = <EOL> '<STR_LIT>' ) <EOL> _output_ports = [ ( '<STR_LIT:value>' , Table ) ] <EOL> def __init__ ( self ) : <EOL> Module . __init__ ( self ) <EOL> self . input_ports_order = [ ] <EOL> def transfer_attrs ( self , module ) : <EOL> Module . transfer_attrs ( self , module ) <EOL> self . input_ports_order = [ p . name for p in module . input_port_specs ] <EOL> def compute ( self ) : <EOL> items = None <EOL> if self . input_ports_order : <EOL> items = [ ( p , self . get_input ( p ) ) <EOL> for p in self . input_ports_order ] <EOL> if not items : <EOL> raise ModuleError ( self , "<STR_LIT>" ) <EOL> nb_rows = None <EOL> cols = [ ] <EOL> names = [ ] <EOL> for portname , item in items : <EOL> if isinstance ( item , TableObject ) : <EOL> if nb_rows is not None : <EOL> if item . rows != nb_rows : <EOL> raise ModuleError ( <EOL> self , <EOL> "<STR_LIT>" % ( <EOL> item . rows , nb_rows ) ) <EOL> else : <EOL> nb_rows = item . rows <EOL> cols . extend ( item . get_column ( c ) <EOL> for c in xrange ( item . columns ) ) <EOL> if item . names is not None : <EOL> names . extend ( item . names ) <EOL> else : <EOL> names . extend ( "<STR_LIT>" % ( portname , i ) <EOL> for i in xrange ( len ( cols ) - len ( names ) ) ) <EOL> else : <EOL> if nb_rows is not None : <EOL> if len ( item ) != nb_rows : <EOL> raise ModuleError ( <EOL> self , <EOL> "<STR_LIT>" % ( <EOL> len ( item ) , nb_rows ) ) <EOL> else : <EOL> nb_rows = len ( item ) <EOL> cols . append ( item ) <EOL> names . append ( portname ) <EOL> self . set_output ( '<STR_LIT:value>' , TableObject ( cols , nb_rows , names ) ) <EOL> class SingleColumnTable ( Converter ) : <EOL> """<STR_LIT>""" <EOL> _input_ports = [ ( '<STR_LIT>' , List ) ] <EOL> _output_ports = [ ( '<STR_LIT>' , Table ) ] <EOL> def compute ( self ) : <EOL> column = self . get_input ( '<STR_LIT>' ) <EOL> if not isinstance ( column , ListType ) : <EOL> column = list ( column ) <EOL> self . set_output ( '<STR_LIT>' , TableObject ( <EOL> [ column ] , <EOL> len ( column ) , <EOL> [ '<STR_LIT>' ] ) ) <EOL> class HtmlRendererMixin ( object ) : <EOL> @ staticmethod <EOL> def make_html ( table ) : <EOL> document = [ '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ] <EOL> if table . names is not None : <EOL> names = table . names <EOL> else : <EOL> names = [ '<STR_LIT>' % n for n in xrange ( table . columns ) ] <EOL> document . append ( '<STR_LIT>' ) <EOL> document . extend ( '<STR_LIT>' % name for name in names ) <EOL> document . append ( '<STR_LIT>' ) <EOL> columns = [ table . get_column ( col ) for col in xrange ( table . columns ) ] <EOL> for row in xrange ( table . rows ) : <EOL> document . append ( '<STR_LIT>' ) <EOL> for col in xrange ( table . columns ) : <EOL> elem = columns [ col ] [ row ] <EOL> if isinstance ( elem , bytes ) : <EOL> elem = elem . decode ( '<STR_LIT:utf-8>' , '<STR_LIT:replace>' ) <EOL> elif not isinstance ( elem , unicode ) : <EOL> elem = unicode ( elem ) <EOL> document . append ( '<STR_LIT>' % elem ) <EOL> document . append ( '<STR_LIT>' ) <EOL> document . append ( '<STR_LIT>' ) <EOL> return '<STR_LIT>' . join ( document ) <EOL> class TableToFileMode ( FileMode , HtmlRendererMixin ) : <EOL> formats = [ '<STR_LIT:html>' , '<STR_LIT>' ] <EOL> def write_html ( self , table , configuration ) : <EOL> filename = self . get_filename ( configuration , suffix = '<STR_LIT>' ) <EOL> with open ( filename , '<STR_LIT:wb>' ) as fp : <EOL> fp . write ( self . make_html ( table ) ) <EOL> def write_csv ( self , table , configuration ) : <EOL> from . write . write_csv import WriteCSV <EOL> filename = self . get_filename ( configuration , suffix = '<STR_LIT>' ) <EOL> WriteCSV . write ( filename , table ) <EOL> def compute_output ( self , output_module , configuration ) : <EOL> value = output_module . get_input ( "<STR_LIT:value>" ) <EOL> format = configuration . get ( '<STR_LIT>' , '<STR_LIT:html>' ) . lower ( ) <EOL> try : <EOL> func = getattr ( self , '<STR_LIT>' % format ) <EOL> except AttributeError : <EOL> raise AttributeError ( "<STR_LIT>" % format ) <EOL> else : <EOL> func ( value , configuration ) <EOL> class TableToIPythonMode ( IPythonMode , HtmlRendererMixin ) : <EOL> def compute_output ( self , output_module , configuration ) : <EOL> from IPython . core . display import display , HTML <EOL> table = output_module . get_input ( '<STR_LIT:value>' ) <EOL> html = self . make_html ( table ) <EOL> display ( HTML ( data = html ) ) <EOL> class TableOutput ( OutputModule ) : <EOL> _settings = ModuleSettings ( configure_widget = "<STR_LIT>" ) <EOL> _input_ports = [ ( '<STR_LIT:value>' , '<STR_LIT>' ) ] <EOL> _output_modes = [ TableToFileMode , TableToIPythonMode ] <EOL> _modules = [ ( Table , { '<STR_LIT>' : True } ) , ExtractColumn , BuildTable , <EOL> ( SingleColumnTable , { '<STR_LIT>' : True } ) , <EOL> TableOutput ] </s>
