.. _cashflowtable-py:
################
cashflowtable.py
################
::
>>> 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'''
...
...
...
...
... PRICE_REGION
... comp_unit
...
...
... assortment
... within-operation
...
...
...
...
...
...
...
...
... 1 1 1 160 370 55
... 2 1 1 160 400 56
... 2 1 1 160 430 58
... 2 1 1 160 460 60
... 2 1 1 160 490 60
... 1 1 2 70 300 20
... 2 1 2 70 300 20
... 1 2 2 70 300 20
... 2 2 2 70 300 20
... 1 3 2 70 300 20
... 2 3 2 70 300 20
...
...
...
...
... '''
>>> 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'>> mb.errors
set([])
>>> mb.all_ok
True
****************************
class CashFlowTable(object):
****************************
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': ,
'assortment': }
>>> cf.classifier_order
['PRICE_REGION', 'assortment']
>>> cf.keycount
1
>>> cf.persist
True
>>> cf.db_cfiers
['assortment']
def __init__(self, ns, elem, validator, dirs):
==============================================
Initialize cash flow table
def _parse_base(self, ns, elem, validator):
============================================
Parse the classifiers used in the cash flow table as well as the table data:
def _get_trend(self, date):
============================
Set currently active trend, None if no active trends
def _adjust_table(self, trend, date):
======================================
Adjust cash flow table values with the current trend
********************
class Trend(object):
********************
Class for cash flow table time trends
Attributes:
- start: date object
- end: date object
- classifiers: dictionary of classifier objects
- keycount: number of classifiers as integer
- classifier_order: classifier order as list of integers
- factor_array: trend factors in a numpy array
- factor_dict: trend factors in a dictionary
def _parse_trend(self, ns, elem, validator):
============================================
Parse trend element into Trend object::
>>> from lxml import etree
>>> from simo.builder.modelbase.cashflowtable import Trend
>>> trendxml = u"""
...
...
... 2000-01-01
... 2001-01-01
...
...
...
... SP
... stratum
...
...
...
... 1 1.1
... 2 1.2
... 3 1.3
...
... """
>>> 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')]
def adjust(self, date, values, factor):
=======================================
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 CashFlowTable(object):
****************************
Class for cash flow tables
Attributes:
- name: cash flow table name
- classifiers: dictionary:
- keys: classifier indices
- values: Classifier objects
- keycount: classifier key count
- arrays: list of Array objects
- Narrays: number of tables (arrays)
- active_array: currently active table
- trends: list of Trend objects
def __init__(self, ns, elem, validator, dirs):
==============================================
Parses the XML data into class attributes::
>>> mb.obj['cash_flow_table']['testxml']['timber_prices']
... # doctest: +ELLIPSIS
>>> ct = mb.obj['cash_flow_table']['testxml']['timber_prices']
>>> for i, j in ct.classifiers.iteritems(): print i, j
... # doctest: +ELLIPSIS
PRICE_REGION
assortment
>>> 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
def _parse_base(self, ns, elem):
================================
Parses the classifiers used in the cash flow table as well as the table data
def _get_trend(self, date):
===========================
Set currently active trend, None if no active trends
def _check_classifiers(self, cfiers):
=====================================
docstring for __check_classifiers
def _adjust_table(self, trend, date):
=====================================
Adjust cash flow table values with the current trend
def set_active_array(self, date):
=================================
Set active array and adjust table values with trend::
>>> import datetime
>>> dt = datetime.date(2005,1,1)
>>> ct.set_active_array(dt)
def get_values(self, classifiers, labels):
==========================================
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.])
def set_values(self, classifiers, labels, values):
==================================================
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.]])