Table Of Contents

Previous topic

parametertable.py

Next topic

table.py

This Page

predictionmodel.py

>>> from simo.builder.modelbase.predictionmodel import PredictionModel
>>> from lxml import etree
>>> xml = u'''<model_group name="Distribution models">
...             <model>
...             <name>Productive_value_of_land_pine_Pukkala</name>
...             <implemented_at>StaticstandModels.dll</implemented_at>
...             <implemented_in>C</implemented_in>
...             <author>
...                 <name>Timo Pukkala</name>
...             </author>
...             <description>...</description>
...             <published_in>...</published_in>
...             <species_list>
...                 <species>1</species>
...             </species_list>
...             <geographical_coverage>...</geographical_coverage>
...             <applies_for>...</applies_for>
...             <research_material>...</research_material>
...             <variables>
...                 <variable>
...                     <name>TS</name>
...                     <level>comp_unit</level>
...                 </variable>
...             </variables>
...             <parameters>
...                 <parameter>
...                     <name>IR</name>
...                 </parameter>
...             </parameters>
...             <result>
...                 <object>self</object>
...                 <variables>
...                     <variable>
...                         <name>PVland_Sp</name>
...                     </variable>
...                 </variables>
...             </result>
...         </model>
...     </model_group>'''
>>> class Validator:
...     def elem_name(self, text):
...         return text
...     def variable_ind(self, level, variable, active=False):
...         return (1,1)
...     def add_model(self, mname, mtype):
...         pass
>>> elem = etree.fromstring(xml)

class PredictionModel(POModel):

Class for prediction model definitions

Properties:

  • type: get model type as string
  • name: get model name as string
  • language: get model implementation language as string
  • dirs: get model library directories as a list of strings
  • function: get model function object
  • library: get model library object
  • wrapper: get model library wrapper object

Attributes:

  • n_vars: number of input variables
  • vars: input variables in a dictionary: level as the key and value is a dictionary with the structure {‘index’: <variable indices in a list>, ‘order’: <variable order in a list>, ‘limits’: <variable limits in a list>}
  • n_params: number of input parameters
  • params: number of input parameters in a list where each item is a (<parameter name>, <parameter limits>) -tuple
  • result_level: result level indice as int
  • result_vars: result variables in a list of ResultVariable instances

def __init__(self, ns, elem, validator, dirs):

Construct prediction model object from XML element:

>>> pr = PredictionModel('', elem[0], Validator(), 'dummydir',
...                      elem.attrib['name'])
>>> pr.name
'Productive_value_of_land_pine_Pukkala'
>>> pr.group
'Distribution models'
>>> pr.language
'c'
>>> pr.n_vars
1
>>> pr.vars
{1: {'index': array([1]), 'order': [0], 'limits': [None]}}
>>> pr.n_params
1
>>> pr.param_names
['IR']
>>> pr.param_limits
[None]
>>> pr.result_level
1

class ResultVariable(object):

Class for prediction model result variables

Attributes:

  • variable
  • time_span
  • time_unit
  • cumulation

def __init__(self, variable, timespan=None, unit=None, cumul=None):

class PredictionModelParam(Persistent):

Class for prediction model parameters

def __init__(self, ns, elem, task, model):

Initialize prediction model parameter object:

>>> execfile('builder/modelbase/test/mocktask.py')
>>> xml_no_param = u'''<prediction>
...     <rect_factor>1.15</rect_factor>
...     <risk_level>2</risk_level>
... </prediction>'''
>>> xml = u'''<prediction>
...     <parameters>
...         <parameter>
...             <name>IR</name>
...             <value>10.5</value>
...         </parameter>
...         <parameter>
...             <name>PARAMETER2</name>
...             <value>VALUE</value>
...         </parameter>
...         </parameters>
...     <rect_factor>1.15</rect_factor>
...     <risk_level>2</risk_level>
... </prediction>'''
>>> from simo.builder.modelbase.predictionmodel import PredictionModelParam
>>> elem = etree.fromstring(xml_no_param)
>>> pmp = PredictionModelParam('', elem, task, pr)
>>> abs(pmp.rect_factor - 1.15) < 0.0001
True
>>> abs(pmp.risk_level - 2.0) < 0.0001
True
>>> task.validator.errors 
set(["No parameters defined in model chain for prediction model
'Productive_value_of_land_pine_Pukkala', 1 parameters expected"])
>>> task.validator.errors = set([])
>>> elem = etree.fromstring(xml)
>>> pmp = PredictionModelParam('', elem, task, pr)
>>> pmp.parameters
[10.5]
>>> task.validator.errors 
set(["Parameter 'PARAMETER2' is not a valid parameter for prediction
      model 'Productive_value_of_land_pine_Pukkala'"])