Reading and writing MTG

MTG

The MTG data structure can be read/write from/to a MTG file format. The functions read_mtg() , write_mtg() , read_mtg_file().

openalea.mtg.io.read_mtg(s, mtg=None, has_date=False)[source]

Create an MTG from its string representation in the MTG format.

Parameter:
  • s (string) - a multi-lines string

Return:

an MTG

Example:

f = open('test.mtg')
txt = f.read()

g = read_mtg(txt)

See also

read_mtg_file().

openalea.mtg.io.read_mtg_file(fn, mtg=None, has_date=False)[source]

Create an MTG from a filename.

Usage:
>>> g = read_mtg_file('test.mtg')

See also

read_mtg().

openalea.mtg.io.write_mtg(g, properties=[], class_at_scale=None, nb_tab=None, display_id=False)[source]

Transform an MTG into a multi-line string in the MTG format.

This method build a generic header, then traverses the MTG and transform each vertex into a line with its label, topoloical relationship and specific properties.

Parameters:
  • g (MTG)

  • properties (list): a list of tuples associating a property name with its type.

    Only these properties will be written in the out file.

Optional Parameters:
  • class_at_scale (dict(name->int)): a map between a class name and its scale.

    If class _at_scale is None, its value will be computed from g.

  • nb_tab (int): the number of tabs used to write the code.

  • display_id (bool): display the id for each vertex

Returns:

a list of strings.

Example:

# Export all the properties defined in `g`.
# We consider that all the properties are real numbers.

properties = [(p, 'REAL') for p in g.property_names() if p not in ['edge_type', 'index', 'label']]
mtg_lines = write_mtg(g, properties)

# Write the result into a file example.mtg

filename = 'example.mtg'
f = open(filename, 'w')
f.write(mtg_lines)
f.close()

LPy

The two functions lpy2mtg() and mtg2lpy() allow to convert the MTG data-structure into lpy and vise-versa. It ease the communication between the two modules. Each structure are traversed and the properties are copied. Properties can be any pyton object.

openalea.mtg.io.lpy2mtg(axial_tree, lsystem, scene=None)[source]
openalea.mtg.io.mtg2lpy(g, lsystem, axial_tree=None)[source]

Create an AxialTree from a MTG with scales.

Usage:

tree = mtg2lpy(g,lsystem)
Parameters:
  • g: The mtg which have been generated by an LSystem.

  • lsystem: A lsystem object containing various information.

    The lsystem is only used to retrieve the context and the parameters associated with each module name.

Optional Parameters:
  • axial_tree: an empty axial tree.

    It is used to avoid complex import in the code.

Return:

axial tree

See also

mtg2axialtree()

AxialTree

openalea.mtg.io.axialtree2mtg(tree, scale, scene, parameters=None)[source]

Create an MTG from an AxialTree.

Tha axial tree has been generated by LPy. It contains both modules with parameters. The geometry is provided by the scene. The shape ids are the same that the module ids in the axial tree. For each module name in the axial tree, a scale and a list of parameters should be defined. The scale dict allow to add a module at a given scale in the MTG. The parameters dict map for each module name a list of parameter name that are added to the MTG.

Parameters:
  • tree: The axial tree generated by the L-system

  • scale: A dict containing the scale for each symbol name.

  • scene: The scene containing the geometry.

  • parameters: list of parameter names for each module.

Return:

mtg

Example:

tree # axial tree
scales = {}
scales['P'] = 1
scales['A'] = 2
scales['GU'] = 3

params ={}
params['P'] = []
params['A'] = ['length', 'radius']
params['GU'] = ['nb_flower']

g = axialtree2mtg(tree, scales, scene, params)
openalea.mtg.io.mtg2axialtree(g, parameters=None, axial_tree=None)[source]

Create a MTG from an AxialTree with scales.

Parameters:
  • axial_tree: The axial tree managed by the L-system. Use an empty AxialTree if you do not want to concatenate this axial_tree with previous results.

  • parameters: list of parameter names for each module.

Return:

mtg

Example:

params = dict()
params ['P'] = []
params['A'] = ['length', radius']
params['GU']=['nb_flower']
tree = mtg2axialtree(g, params)

Cpfg

openalea.mtg.io.read_lsystem_string(string, symbol_at_scale, functional_symbol={}, mtg=None)[source]

Read a string generated by a lsystem.

Parameters:

  • string: The lsystem string representing the axial tree.

  • symbol_at_scale: A dict containing the scale for each symbol name.

Optional parameters:

  • functional_symbol: A dict containing a function for specific symbols.

    The args of the function have to be coherent with those in the string. The return type of the functions have to be a dictionary of properties: dict(name, value)

Return:

MTG object

Mss

openalea.mtg.io.mtg2mss(name, mtg, scene, envelop_type='CvxHull')[source]

Convert an MTG into the multi-scale structure implemented by fractalysis.

Parameters:
  • name: name of the structure

  • mtg: the mtg to convert

  • scene: the scene containing the geometry

  • envelop_type: algorithm used to fit the geometry.between scales.

Returns:

mss data structure.

Download the source file ../../src/mtg/io.py.