>>> 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>
... </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 # doctest: +NORMALIZE_WHITESPACE
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 # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE
{'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
Set active array and adjust table values with trend
Get values from the active array
Set values of active array
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]])
>>> tr.factor_dict
{'1': 1.1000000000000001, '3': 1.3, '2': 1.2}
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']
... # doctest: +ELLIPSIS
<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
... # doctest: +ELLIPSIS
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.],
[ 2., 1., 1., 160., 400., 56.]])
>>> ct.arrays[0].dict
{'1:1:1:160:370': 55.0, '2:1:1:160:400': 56.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., 56.])
>>> ct.get_values([0], [1])
array([ 55.])
>>> ct.get_values([0], [2])
array([ 56.])
Set values of active array:
>>> ct.set_values([1], [1], numpy.array([65, 66]))
>>> ct.active_array.array
array([[ 1., 1., 1., 160., 370., 65.],
[ 2., 1., 1., 160., 400., 66.]])