Table Of Contents

Previous topic

aggr.py

Next topic

by_level.py

This Page

branching.py

class OutputOpres(Output):

This module handles writing operation results into files

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, opres_vars=None, full_date=False, dates=None):

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

>>> import os
>>> from glob import glob
>>> removing = glob('output/test/*.dot')
>>> for file in removing:
...     os.remove(file)
>>> from simo.output.test.init_objects import InitData, TestLogger
>>> idata = InitData()
>>> idata.init()
>>> testdb = idata.testdb
>>> const_obj = idata.const_obj
>>> aggr_obj = idata.aggr_obj
>>> expr_obj = idata.expr_obj
>>> from simo.output.branching import OutputBranching
>>> out = OutputBranching(testdb, 'result', ['stand1'], 'comp_unit',
...               'optimized',
...               'output/test', TestLogger(),
...               const_obj, 1, False, True, aggr_obj, expr_obj,
...               None, None, True)
>>> try: 
...     file = open('output/test/stand1_0.dot', 'r')
...     for line in file:
...         print line.rstrip('\n')
... finally:
...     file.close()
digraph branches {
nodesep=0.01;
ranksep=0.01;
size="8.27,11.69";
ratio=fill;
...
20090207;
...

def run(self):

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

def _get_branch_root_str(self, date, item):

Returns a properly formatted string to describe a root point from where a branch starts

>>> import datetime
>>> ddate = datetime.date(2000, 1, 1)
>>> out._get_branch_root_str(ddate, 1)
'b_1_20000101 [label="1" fontcolor=red group="1"];'

def _get_branch_node_strs(self, item):

Returns a properly formatted string list of node points. item is a list containing a date and the appropriate groups (groups are (from_branch, to_branch) tuples)

>>> out._get_branch_node_strs([ddate, (0, 1), (0, 2)])
['b_1_20000101 [group="1"];', 'b_2_20000101 [group="2"];']

def _get_branch_ranking_str(self, item):

Returns a properly formatted string used to align different nodes. item is a list containing a date and the appropriate groups (groups are (from_branch, to_branch) tuples)

>>> out._get_branch_ranking_str([ddate, (0, 1), (0, 2)])
'{ rank = same; 20000101; b_1_20000101; b_2_20000101; }'

def _get_branch_branch_str(self, date, group1, group2, name):

Returns a properly formatted string describing a link from one branch to another (groups are branch numbers)

>>> out._get_branch_branch_str(ddate, 0, 1, 'move operation')
'b_0_20000101 -> b_1_20000101 [ label="move operation" fontsize=14 fontcolor=red];'

def _get_branching_strings(self):

>>> ddate = datetime.date(2009, 2, 7)
>>> data = {'stratum': [(ddate, {'id':'stratum2-1', 'oid':'branched',
...                              'parent id': 'stand1',
...                              'values': [('SP', 0.1), ('BA', 1.2),
...                                         ('DgM', 2.3), ('HgM', 3.4)]})]}
>>> out.datadb.add_data_from_dictionary(data, 0, 0)
>>> dates = out.datadb.get_dates_by_id_and_iter(1)
>>> str21 = out._get_branching_strings(dates[('stand1', 0)],
...                                    'stand1', 0)
>>> for i in str21:
...     for j in i:
...         print j
...     print ''
digraph branches {
nodesep=0.01;
ranksep=0.01;
size="8.27,11.69";
ratio=fill;

{
node [shape=plaintext fontsize=14 height=.01];

20090207;
}

{
node [shape=doublecircle];

b_0_20090207 [label="0" fontcolor=red group="0"];
}

{
node [shape=point]

b_0_20090207 [group="0"];
}

{ rank = same; 20090207; b_0_20090207; }


}

def _format_date(self, date):

Formats date string according to date_res (either full date or just the year)

>>> out._format_date(datetime.date(2000,1,1))
'20000101'
>>> out.date_res = 0
>>> out._format_date(datetime.date(2000,1,1))
'2000'
>>> out.date_res = 1

def branching(self, path):

Will write branching .dots for each id on the main level from the database to the given path

>>> out.branching('output/test')
>>> lines = []
>>> try:
...     file = open('output/test/stand1_0.dot', 'r')
...     for line in file:
...         lines.append(line.rstrip('\n'))
... finally:
...     file.close()
>>> resultlines = []
>>> for item in str21:
...     for line in item:
...         resultlines.append(line)
...     resultlines.append('')
>>> lines == resultlines
True
>>> from glob import glob
>>> set([os.path.basename(file) for file in glob('output/test/*.dot')]) == \
...     set(['stand1_0.dot', 'stand1_1.dot', 'stand2_0.dot',
...          'stand2_1.dot'])
True
>>> r = out.datadb.db.execute("DELETE FROM stratum WHERE oid='branched'")