>>> from simo.builder.modelbase.modelbase import ModelbaseDef
>>> tdf = open('../../simulator/xml/schemas/Typedefs_SIMO.xsd')
>>> typedef = tdf.read()
>>> tdf.close()
>>> sf = open('../../simulator/xml/schemas/cash_flow_table.xsd')
>>> schema = sf.read()
>>> sf.close()
>>> xml = u'''<cash_flow_tables xmlns="http://www.simo-project.org/simo"
... xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
... <cash_flow_table name="timber_prices" logged="true">
... <base>
... <classifiers>
... <classifier>
... <variable>PRICE_REGION</variable>
... <level>comp_unit</level>
... </classifier>
... <classifier to_db="true">
... <variable>assortment</variable>
... <level>within-operation</level>
... <labels>
... <label>
... <value>1</value>
... <name>log</name>
... </label>
... <label>
... <value>2</value>
... <name>pulp</name>
... </label>
... </labels>
... </classifier>
... </classifiers>
... <tables>
... <table>
... <cash_flow>1 1 1 160 370 55</cash_flow>
... <cash_flow>2 1 1 160 400 56</cash_flow>
... <cash_flow>2 1 1 160 430 58</cash_flow>
... <cash_flow>2 1 1 160 460 60</cash_flow>
... <cash_flow>2 1 1 160 490 60</cash_flow>
... <cash_flow>1 1 2 70 300 20</cash_flow>
... <cash_flow>2 1 2 70 300 20</cash_flow>
... <cash_flow>1 2 2 70 300 20</cash_flow>
... <cash_flow>2 2 2 70 300 20</cash_flow>
... <cash_flow>1 3 2 70 300 20</cash_flow>
... <cash_flow>2 3 2 70 300 20</cash_flow>
... </table>
... </tables>
... </base>
... </cash_flow_table>
... </cash_flow_tables>'''
>>> mb = ModelbaseDef(typedef)
>>> mb.schema = ('cash_flow_table', schema)
>>> try:
... mb.xml = ('testxml', xml, None, '', 'cash_flow_table')
... except ValueError, e:
... e.message
'errors in xml to object conversion'
>>> mb.errors
set(["No lexicon set when validating variable for cash flow table
collection 'testxml'",
"No lexicon set when adding model for cash flow table
collection 'testxml'"])
>>> class Lexicon(object):
... def __init__(self):
... self.models = {}
... def get_variable_ind(self, level, var, active=False):
... return (1, 1)
... def get_level_ind(self, level):
... return 1
... def add_model(self, mtype, mname):
... if mtype not in self.models:
... self.models[mtype] = set()
... self.models[mtype].add(mname)
>>> mb.xml = ('testxml', xml, Lexicon(), '', 'cash_flow_table')
>>> mb.xml['cash_flow_table']['testxml'][:17]
u'<cash_flow_tables'
>>> mb.errors
set([])
>>> mb.all_ok
True
Class for cash flow tables used in operations:
>>> cf = mb.obj['cash_flow_table']['testxml']['timber_prices']
>>> cf.classifiers
{'PRICE_REGION': <simo.builder.modelbase.table.TableClassifier...>,
'assortment': <simo.builder.modelbase.table.TableClassifier...>}
>>> cf.classifier_order
['PRICE_REGION', 'assortment']
>>> cf.keycount
1
>>> cf.persist
True
>>> cf.db_cfiers
['assortment']
Initialize cash flow table
Parse the classifiers used in the cash flow table as well as the table data:
Set currently active trend, None if no active trends
Adjust cash flow table values with the current trend
Class for cash flow table time trends
Attributes:
Parse trend element into Trend object:
>>> from lxml import etree
>>> from simo.builder.modelbase.cashflowtable import Trend
>>> trendxml = u"""
... <trend>
... <time_period>
... <start>2000-01-01</start>
... <end>2001-01-01</end>
... </time_period>
... <classifiers>
... <classifier>
... <variable>SP</variable>
... <level>stratum</level>
... </classifier>
... </classifiers>
... <cumulative_factors>
... <factor>1 1.1</factor>
... <factor>2 1.2</factor>
... <factor>3 1.3</factor>
... </cumulative_factors>
... </trend>"""
>>> elem = etree.fromstring(trendxml)
>>> ns = ""
>>> tr = Trend(ns, elem, mb)
>>> tr.start
datetime.date(2000, 1, 1)
>>> tr.end
datetime.date(2001, 1, 1)
>>> tr.factor_array
array([[ 1. , 1.1],
[ 2. , 1.2],
[ 3. , 1.3]])
>>> sorted([(k, '%.1f' % v) for k, v in tr.factor_dict.items()])
[('1', '1.1'), ('2', '1.2'), ('3', '1.3')]
Calculate interpolation multiplier based on given date:
>>> import datetime, numpy
>>> dt = datetime.date(2005,1,1)
>>> vals = numpy.ones(1, dtype=float)
>>> factor = 0.05
>>> tr.adjust(dt, vals, factor)
array([ 1.25])
Class for cash flow tables
Attributes:
name: cash flow table name
keycount: classifier key count
arrays: list of Array objects
Narrays: number of tables (arrays)
active_array: currently active table
trends: list of Trend objects
Parses the XML data into class attributes:
>>> mb.obj['cash_flow_table']['testxml']['timber_prices']
...
<simo.builder.modelbase.cashflowtable.CashFlowTable object at 0x...>
>>> ct = mb.obj['cash_flow_table']['testxml']['timber_prices']
>>> for i, j in ct.classifiers.iteritems(): print i, j
...
PRICE_REGION <simo.builder.modelbase.table.TableClassifier object at ...>
assortment <simo.builder.modelbase.table.TableClassifier object at ...>
>>> ct.arrays[0].array
array([[ 1., 1., 1., 160., 370., 55.],
[ 1., 1., 2., 70., 300., 20.],
[ 1., 2., 2., 70., 300., 20.],
[ 1., 3., 2., 70., 300., 20.],
[ 2., 1., 1., 160., 400., 56.],
[ 2., 1., 1., 160., 430., 58.],
[ 2., 1., 1., 160., 460., 60.],
[ 2., 1., 1., 160., 490., 60.],
[ 2., 1., 2., 70., 300., 20.],
[ 2., 2., 2., 70., 300., 20.],
[ 2., 3., 2., 70., 300., 20.]])
>>> keys = ct.arrays[0].dict.keys()
>>> keys.sort()
>>> for key in keys:
... print key, ct.arrays[0].dict[key]
1:1:1:160:370 55.0
1:1:2:70:300 20.0
1:2:2:70:300 20.0
1:3:2:70:300 20.0
2:1:1:160:400 56.0
2:1:1:160:430 58.0
2:1:1:160:460 60.0
2:1:1:160:490 60.0
2:1:2:70:300 20.0
2:2:2:70:300 20.0
2:3:2:70:300 20.0
Parses the classifiers used in the cash flow table as well as the table data
Set currently active trend, None if no active trends
docstring for __check_classifiers
Adjust cash flow table values with the current trend
Set active array and adjust table values with trend:
>>> import datetime
>>> dt = datetime.date(2005,1,1)
>>> ct.set_active_array(dt)
Get values from the active array:
>>> ct.get_values([1], [1])
array([ 55., 20., 56., 58., 60., 60., 20.])
>>> ct.get_values([0], [1])
array([ 55., 20., 20., 20.])
>>> ct.get_values([0], [1])
array([ 55., 20., 20., 20.])
>>> ct.get_values([0], [2])
array([ 56., 58., 60., 60., 20., 20., 20.])
>>> ct.get_values([0, 1], [1, 1])
array([ 55., 20.])
>>> ct.get_values([0, 1], [2, 2])
array([ 20.])
Set values of active array:
>>> ct.set_values([0, 1], [1, 1], numpy.array([65, 66]))
>>> ct.active_array.array
array([[ 1., 1., 1., 160., 370., 65.],
[ 1., 1., 2., 70., 300., 66.],
[ 1., 2., 2., 70., 300., 20.],
[ 1., 3., 2., 70., 300., 20.],
[ 2., 1., 1., 160., 400., 56.],
[ 2., 1., 1., 160., 430., 58.],
[ 2., 1., 1., 160., 460., 60.],
[ 2., 1., 1., 160., 490., 60.],
[ 2., 1., 2., 70., 300., 20.],
[ 2., 2., 2., 70., 300., 20.],
[ 2., 3., 2., 70., 300., 20.]])