Table Of Contents

Previous topic

branching.py

Next topic

expression.py

This Page

by_level.py

class OutputByLevel(Output):

Module for data output in hierarchical “data in columns” format. (writing tables to files). This module writes the data in a more spreadsheet-compatible manner.

def __init__(self, datadb, data_type, id_list, main_level, result_type, output_filename, output_constraint=None, default_decimal_places=1, archiving=False, result_padding=True, aggregation_def=None, expression_def=None, opres_vars=None):

Creates the output class. fully inherited, executes self.run()

>>> from pprint import pprint
>>> from simo.output.test.init_objects import InitData, TestLogger
>>> idata = InitData()
>>> idata.init()
>>> testdb = idata.testdb
>>> const_obj = idata.const_obj
>>> origvars = const_obj.variables
>>> const_obj.variables = {('stratum',2):origvars[('stratum', 2)]}
>>> aggr_obj = idata.aggr_obj
>>> expr_obj = idata.expr_obj
>>> from simo.output.by_level import OutputByLevel
>>> out = OutputByLevel(testdb, 'result', ['stand1'], 'stratum',
...               'optimized',
...               'output/test/by_level.txt', TestLogger(),
...               const_obj, 1, False, True, aggr_obj, expr_obj,
...               ['cash_flow', 'Income', 'Scrapwood', 'Volume',
...               'BIOMASS_branches', 'BIOMASS_stumps'])
>>> try: 
...     file = open('output/test/by_level.txt', 'r')
...     for line in file:
...         print line.rstrip('\n')
... finally:
...     file.close()
 sim unit; branch; iteration; data level;         id; date;  SP;   BA;  HgM
   stand1;      0;         0;    stratum; o-stratum1-1; 2009; 1.0; 17.0; 19.0
   stand1;      0;         0;    stratum; o-stratum1-2; 2009; 2.0;  8.0; 18.5
   stand1;      0;         1;    stratum; o-stratum1-1; 2009; 1.0; 17.0; 19.0
   stand1;      0;         1;    stratum; o-stratum1-2; 2009; 2.0;  8.0; 18.5
>>> const_obj.variables = origvars
>>> out = OutputByLevel(testdb, 'result', ['stand1'], 'stratum',
...               'optimized',
...               'output/test/by_level.txt', TestLogger(),
...               const_obj, 1, False, True, aggr_obj,
...               ['cash_flow', 'Income', 'Scrapwood', 'Volume',
...               'BIOMASS_branches', 'BIOMASS_stumps'])
>>> try: 
...     file = open('output/test/by_level.txt', 'r')
...     for line in file:
...         print line.rstrip('\n')
... finally:
...     file.close()
 sim unit; branch; iteration; data level;         id; date; AREA;  SC;  SP;   BA;  HgM;    d;    h
   stand1;      0;         0;  comp_unit;     o-stand1; 2009;  1.0; 3.0;    ;     ;     ;     ;
   stand1;      0;         0;    stratum; o-stratum1-1; 2009;     ;    ; 1.0; 17.0; 19.0;     ;
   stand1;      0;         0;       tree;  o-tree1-1-1; 2009;     ;    ;    ;     ;     ; 17.0; 18.0
   stand1;      0;         0;    stratum; o-stratum1-2; 2009;     ;    ; 2.0;  8.0; 18.5;     ;
   stand1;      0;         0;       tree;  o-tree1-2-1; 2009;     ;    ;    ;     ;     ; 22.0; 21.0
   stand1;      0;         1;  comp_unit;     o-stand1; 2009;  1.0; 3.0;    ;     ;     ;     ;
   stand1;      0;         1;    stratum; o-stratum1-1; 2009;     ;    ; 1.0; 17.0; 19.0;     ;
   stand1;      0;         1;       tree;  o-tree1-1-1; 2009;     ;    ;    ;     ;     ; 17.0; 18.0
   stand1;      0;         1;    stratum; o-stratum1-2; 2009;     ;    ; 2.0;  8.0; 18.5;     ;
   stand1;      0;         1;       tree;  o-tree1-2-1; 2009;     ;    ;    ;     ;     ; 22.0; 21.0

def run(self):

Uses data from init to run the class-specific output (self.by_level)

def _flatten_header(self, header):

This will return a one dimentional list, flattened from a two dimentional list

>>> wh = out._get_wide_header()
>>> wh 
[['sim unit', 'branch', 'iteration', 'data level', 'id', 'date'],
['AREA', 'SC'], ['SP', 'BA', 'HgM'], ['d', 'h']]
>>> out._flatten_header(wh) 
['sim unit', 'branch', 'iteration', 'data level', 'id', 'date', 'AREA',
 'SC', 'SP', 'BA', 'HgM', 'd', 'h']

def _get_wide_header(self):

This function will get a two dimentional header list for all the tables in the datadb, one list per table with the common arguments at the start.

>>> out._get_wide_header()
[['sim unit', 'branch', 'iteration', 'data level', 'id', 'date'], ['AREA', 'SC'], ['SP', 'BA', 'HgM'], ['d', 'h']]

def _set_wide_vars(self, item, level, wide_header):

This function will create a (wide variant) row from the given variables, setting values to their correct places based on level.

>>> out._set_wide_vars(['sim unit', 'branch', 'iteration', 'data level', 'id', 'date', 'sp', 'ba', 'hgm'], 2, out._get_wide_header())
['sim unit', 'branch', 'iteration', 'data level', 'id', 'date', '', '', 'sp', 'ba', 'hgm', '', '']

def _get_strings_wide(self, wide_header=None, constraints=None, dates=None, date_res=0, level=1, sim_unit=None, core=None):

This function functions exactly as _get_strings, except that it uses the wide table variant (header from _get_wide_header, rows from _set_wide_vars) wide_header - header to be used (method will get one if None) constraints is a dictionary that gives sql constraints as key op value(s). op is the first param of the value list. if op == in, the list can have additional params. dates - a tuple with (first_date, last_date) date_res - 0 or 1; date_res = 0: 2009, date_res = 1: 2009-03-26 level - where are we in the hierarchy? level 1 == base (for recursion) sim_unit - current simulation unit id (for recursion) core - current date, iteration, branch and data_id (for recursion):

>>> strings = out._get_strings_wide()
>>> pprint(strings)
[' sim unit; branch; iteration; data level;           id; date; AREA;  SC;  SP;   BA;  HgM;    d;    h',
 u'   stand1;      0;         0;  comp_unit;     o-stand1; 2009;  1.0; 3.0;    ;     ;     ;     ;     ',
 u'   stand1;      0;         0;    stratum; o-stratum1-1; 2009;     ;    ; 1.0; 17.0; 19.0;     ;     ',
 u'   stand1;      0;         0;       tree;  o-tree1-1-1; 2009;     ;    ;    ;     ;     ; 17.0; 18.0',
 u'   stand1;      0;         0;    stratum; o-stratum1-2; 2009;     ;    ; 2.0;  8.0; 18.5;     ;     ',
 u'   stand1;      0;         0;       tree;  o-tree1-2-1; 2009;     ;    ;    ;     ;     ; 22.0; 21.0',
 u'   stand1;      0;         1;  comp_unit;     o-stand1; 2009;  1.0; 3.0;    ;     ;     ;     ;     ',
 u'   stand1;      0;         1;    stratum; o-stratum1-1; 2009;     ;    ; 1.0; 17.0; 19.0;     ;     ',
 u'   stand1;      0;         1;       tree;  o-tree1-1-1; 2009;     ;    ;    ;     ;     ; 17.0; 18.0',
 u'   stand1;      0;         1;    stratum; o-stratum1-2; 2009;     ;    ; 2.0;  8.0; 18.5;     ;     ',
 u'   stand1;      0;         1;       tree;  o-tree1-2-1; 2009;     ;    ;    ;     ;     ; 22.0; 21.0',
 u'   stand2;      0;         0;  comp_unit;     o-stand2; 2009;  2.0; 2.0;    ;     ;     ;     ;     ',
 u'   stand2;      0;         0;    stratum; o-stratum2-1; 2009;     ;    ; 2.0; 24.0; 31.0;     ;     ',
 u'   stand2;      0;         0;       tree;  o-tree2-1-1; 2009;     ;    ;    ;     ;     ; 30.0; 29.0',
 u'   stand2;      0;         0;    stratum; o-stratum2-2; 2009;     ;    ; 3.0; 12.0; 26.0;     ;     ',
 u'   stand2;      0;         0;       tree;  o-tree2-2-1; 2009;     ;    ;    ;     ;     ; 26.0; 24.0',
 u'   stand2;      0;         0;       tree;  o-tree2-2-2; 2009;     ;    ;    ;     ;     ; 28.0; 27.0',
 u'   stand2;      0;         1;  comp_unit;     o-stand2; 2009;  2.0; 2.0;    ;     ;     ;     ;     ',
 u'   stand2;      0;         1;    stratum; o-stratum2-1; 2009;     ;    ; 2.0; 24.0; 31.0;     ;     ',
 u'   stand2;      0;         1;       tree;  o-tree2-1-1; 2009;     ;    ;    ;     ;     ; 30.0; 29.0',
 u'   stand2;      0;         1;    stratum; o-stratum2-2; 2009;     ;    ; 3.0; 12.0; 26.0;     ;     ',
 u'   stand2;      0;         1;       tree;  o-tree2-2-1; 2009;     ;    ;    ;     ;     ; 26.0; 24.0',
 u'   stand2;      0;         1;       tree;  o-tree2-2-2; 2009;     ;    ;    ;     ;     ; 28.0; 27.0',
 u'   stand2;      1;         0;  comp_unit;     o-stand2; 2009;  2.0; 2.0;    ;     ;     ;     ;     ',
 u'   stand2;      1;         0;    stratum; o-stratum2-3; 2009;     ;    ; 2.0;  0.0;  0.0;     ;     ',
 u'   stand2;      1;         1;  comp_unit;     o-stand2; 2009;  2.0; 2.0;    ;     ;     ;     ;     ',
 u'   stand2;      1;         1;    stratum; o-stratum2-3; 2009;     ;    ; 2.0;  0.0;  0.0;     ;     ']

If your result_variables.xml skips a level, the others will still be output:

>>> old_const = out.output_level_constraint
>>> out.output_level_constraint = set((1, 3))
>>> pprint(out._get_strings_wide())
[' sim unit; branch; iteration; data level;           id; date; AREA;  SC;    d;    h',
 u'   stand1;      0;         0;  comp_unit;     o-stand1; 2009;  1.0; 3.0;     ;     ',
 u'   stand1;      0;         0;       tree;  o-tree1-1-1; 2009;     ;    ; 17.0; 18.0',
 u'   stand1;      0;         0;       tree;  o-tree1-2-1; 2009;     ;    ; 22.0; 21.0',
 u'   stand1;      0;         1;  comp_unit;     o-stand1; 2009;  1.0; 3.0;     ;     ',
 u'   stand1;      0;         1;       tree;  o-tree1-1-1; 2009;     ;    ; 17.0; 18.0',
 u'   stand1;      0;         1;       tree;  o-tree1-2-1; 2009;     ;    ; 22.0; 21.0',
 u'   stand2;      0;         0;  comp_unit;     o-stand2; 2009;  2.0; 2.0;     ;     ',
 u'   stand2;      0;         0;       tree;  o-tree2-1-1; 2009;     ;    ; 30.0; 29.0',
 u'   stand2;      0;         0;       tree;  o-tree2-2-1; 2009;     ;    ; 26.0; 24.0',
 u'   stand2;      0;         0;       tree;  o-tree2-2-2; 2009;     ;    ; 28.0; 27.0',
 u'   stand2;      0;         1;  comp_unit;     o-stand2; 2009;  2.0; 2.0;     ;     ',
 u'   stand2;      0;         1;       tree;  o-tree2-1-1; 2009;     ;    ; 30.0; 29.0',
 u'   stand2;      0;         1;       tree;  o-tree2-2-1; 2009;     ;    ; 26.0; 24.0',
 u'   stand2;      0;         1;       tree;  o-tree2-2-2; 2009;     ;    ; 28.0; 27.0',
 u'   stand2;      1;         0;  comp_unit;     o-stand2; 2009;  2.0; 2.0;     ;     ',
 u'   stand2;      1;         1;  comp_unit;     o-stand2; 2009;  2.0; 2.0;     ;     ']
>>> out.output_level_constraint = old_const

def by_level(self, file, constraints=None, dates=None, date_res=0):

This function will use _get_strings_wide with the given restrictions and date resolution, and writes the results into the given file.

>>> out.by_level('output/test/by_level.txt')
>>> try:
...     file = open('output/test/by_level.txt', 'r')
...     for line in file:
...         print line.rstrip('\n')
... finally:
...     file.close()
 sim unit; branch; iteration; data level;           id; date; AREA;  SC;  SP;   BA;  HgM;    d;    h
   stand1;      0;         0;  comp_unit;     o-stand1; 2009;  1.0; 3.0;    ;     ;     ;     ;
   stand1;      0;         0;    stratum; o-stratum1-1; 2009;     ;    ; 1.0; 17.0; 19.0;     ;
   stand1;      0;         0;       tree;  o-tree1-1-1; 2009;     ;    ;    ;     ;     ; 17.0; 18.0
   stand1;      0;         0;    stratum; o-stratum1-2; 2009;     ;    ; 2.0;  8.0; 18.5;     ;
   stand1;      0;         0;       tree;  o-tree1-2-1; 2009;     ;    ;    ;     ;     ; 22.0; 21.0
   stand1;      0;         1;  comp_unit;     o-stand1; 2009;  1.0; 3.0;    ;     ;     ;     ;
   stand1;      0;         1;    stratum; o-stratum1-1; 2009;     ;    ; 1.0; 17.0; 19.0;     ;
   stand1;      0;         1;       tree;  o-tree1-1-1; 2009;     ;    ;    ;     ;     ; 17.0; 18.0
   stand1;      0;         1;    stratum; o-stratum1-2; 2009;     ;    ; 2.0;  8.0; 18.5;     ;
   stand1;      0;         1;       tree;  o-tree1-2-1; 2009;     ;    ;    ;     ;     ; 22.0; 21.0
   stand2;      0;         0;  comp_unit;     o-stand2; 2009;  2.0; 2.0;    ;     ;     ;     ;
   stand2;      0;         0;    stratum; o-stratum2-1; 2009;     ;    ; 2.0; 24.0; 31.0;     ;
   stand2;      0;         0;       tree;  o-tree2-1-1; 2009;     ;    ;    ;     ;     ; 30.0; 29.0
   stand2;      0;         0;    stratum; o-stratum2-2; 2009;     ;    ; 3.0; 12.0; 26.0;     ;
   stand2;      0;         0;       tree;  o-tree2-2-1; 2009;     ;    ;    ;     ;     ; 26.0; 24.0
   stand2;      0;         0;       tree;  o-tree2-2-2; 2009;     ;    ;    ;     ;     ; 28.0; 27.0
   stand2;      0;         1;  comp_unit;     o-stand2; 2009;  2.0; 2.0;    ;     ;     ;     ;
   stand2;      0;         1;    stratum; o-stratum2-1; 2009;     ;    ; 2.0; 24.0; 31.0;     ;
   stand2;      0;         1;       tree;  o-tree2-1-1; 2009;     ;    ;    ;     ;     ; 30.0; 29.0
   stand2;      0;         1;    stratum; o-stratum2-2; 2009;     ;    ; 3.0; 12.0; 26.0;     ;
   stand2;      0;         1;       tree;  o-tree2-2-1; 2009;     ;    ;    ;     ;     ; 26.0; 24.0
   stand2;      0;         1;       tree;  o-tree2-2-2; 2009;     ;    ;    ;     ;     ; 28.0; 27.0
   stand2;      1;         0;  comp_unit;     o-stand2; 2009;  2.0; 2.0;    ;     ;     ;     ;
   stand2;      1;         0;    stratum; o-stratum2-3; 2009;     ;    ; 2.0;  0.0;  0.0;     ;
   stand2;      1;         1;  comp_unit;     o-stand2; 2009;  2.0; 2.0;    ;     ;     ;     ;
   stand2;      1;         1;    stratum; o-stratum2-3; 2009;     ;    ; 2.0;  0.0;  0.0;     ;