<s> """<STR_LIT>""" <EOL> import pysb <EOL> import pysb . bng <EOL> import sympy <EOL> import re <EOL> try : <EOL> from cStringIO import StringIO <EOL> except ImportError : <EOL> from io import StringIO <EOL> from pysb . export import Exporter , pad <EOL> class MatlabExporter ( Exporter ) : <EOL> """<STR_LIT>""" <EOL> def export ( self ) : <EOL> """<STR_LIT>""" <EOL> output = StringIO ( ) <EOL> pysb . bng . generate_equations ( self . model ) <EOL> docstring = '<STR_LIT>' <EOL> if self . docstring : <EOL> docstring += self . docstring . replace ( '<STR_LIT:\n>' , '<STR_LIT>' ) <EOL> model_name = self . model . name . replace ( '<STR_LIT:.>' , '<STR_LIT:_>' ) <EOL> params_str = '<STR_LIT>' + '<STR_LIT:U+0020>' * <NUM_LIT:16> <EOL> params_str_list = [ ] <EOL> for i , p in enumerate ( self . model . parameters ) : <EOL> cur_p_str = "<STR_LIT>" % ( _fix_underscores ( p . name ) , p . value ) <EOL> if i == len ( self . model . parameters ) - <NUM_LIT:1> : <EOL> cur_p_str += '<STR_LIT>' <EOL> else : <EOL> cur_p_str += '<STR_LIT>' <EOL> params_str_list . append ( cur_p_str ) <EOL> params_str += ( '<STR_LIT:\n>' + '<STR_LIT:U+0020>' * <NUM_LIT:16> ) . join ( params_str_list ) <EOL> initial_values_str = ( '<STR_LIT>' + '<STR_LIT:U+0020>' * <NUM_LIT:12> ) % len ( self . model . species ) <EOL> initial_values_str += ( '<STR_LIT:\n>' + '<STR_LIT:U+0020>' * <NUM_LIT:12> ) . join ( <EOL> [ '<STR_LIT>' % <EOL> ( i + <NUM_LIT:1> , _fix_underscores ( ic [ <NUM_LIT:1> ] . name ) , ic [ <NUM_LIT:0> ] ) <EOL> for i , ic in enumerate ( self . model . initial_conditions ) ] ) <EOL> observables_str = '<STR_LIT>' + '<STR_LIT:U+0020>' * <NUM_LIT:16> <EOL> observables_str_list = [ ] <EOL> for i , obs in enumerate ( self . model . observables ) : <EOL> cur_obs_str = "<STR_LIT>" % ( _fix_underscores ( obs . name ) , <EOL> '<STR_LIT:U+0020>' . join ( [ str ( sp + <NUM_LIT:1> ) for sp in obs . species ] ) , <EOL> '<STR_LIT:U+0020>' . join ( [ str ( c ) for c in obs . coefficients ] ) ) <EOL> if i == len ( self . model . observables ) - <NUM_LIT:1> : <EOL> cur_obs_str += '<STR_LIT>' <EOL> else : <EOL> cur_obs_str += '<STR_LIT>' <EOL> observables_str_list . append ( cur_obs_str ) <EOL> observables_str += ( '<STR_LIT:\n>' + '<STR_LIT:U+0020>' * <NUM_LIT:16> ) . join ( observables_str_list ) <EOL> species_list = [ '<STR_LIT>' % s for i , s in enumerate ( self . model . species ) ] <EOL> odes_list = [ '<STR_LIT>' % ( i + <NUM_LIT:1> , sympy . ccode ( self . model . odes [ i ] ) ) <EOL> for i in range ( len ( self . model . odes ) ) ] <EOL> odes_species_list = [ item for sublist in zip ( species_list , odes_list ) <EOL> for item in sublist ] <EOL> odes_str = ( '<STR_LIT:\n>' + '<STR_LIT:U+0020>' * <NUM_LIT:12> ) . join ( odes_species_list ) <EOL> odes_str = re . sub ( r'<STR_LIT>' , lambda m : '<STR_LIT>' % ( int ( m . group ( <NUM_LIT:1> ) ) + <NUM_LIT:1> ) , odes_str ) <EOL> odes_str = re . sub ( r'<STR_LIT>' , '<STR_LIT>' , odes_str ) <EOL> for i , p in enumerate ( self . model . parameters ) : <EOL> odes_str = re . sub ( r'<STR_LIT>' % p . name , <EOL> '<STR_LIT>' % _fix_underscores ( p . name ) , odes_str ) <EOL> output . write ( pad ( r"""<STR_LIT>""" , <NUM_LIT:0> ) % <EOL> { '<STR_LIT>' : docstring , <EOL> '<STR_LIT>' : model_name , <EOL> '<STR_LIT>' : params_str , <EOL> '<STR_LIT>' : initial_values_str , <EOL> '<STR_LIT>' : observables_str , <EOL> '<STR_LIT>' : params_str , <EOL> '<STR_LIT>' : odes_str } ) <EOL> return output . getvalue ( ) <EOL> def _fix_underscores ( name ) : <EOL> if name . startswith ( '<STR_LIT:_>' ) : <EOL> return '<STR_LIT:X>' + name <EOL> else : <EOL> return name </s>
