def generate_bgeom(step=None): from openalea.lpy import Lsystem import os print('Scene generator launched') l = Lsystem( lsysfile, { 'RESOLUTION': 2, 'daystep': 1, 'TIMEBAR': False, 'LEAFY': True, 'WITH_INFLO': True, 'EXPORT_TO_MTG': False }) if step is None: firststep = 0 nbsteps = l.derivationLength else: firststep = step nbsteps = step + 1 open(stepfile, 'w').write(str(nbsteps)) if not os.path.exists(workingrep): os.makedirs(workingrep) for step in range(firststep, nbsteps): if step == firststep: lstring = l.derive(firststep + 1) else: lstring = l.derive(lstring, step, 1) lscene = l.sceneInterpretation(lstring) fname = join(workingrep, bgeomfile.format(str(step).zfill(4))) lscene.save(tempbgeomfile) os.rename(tempbgeomfile, fname) print("Scene", step, "generated ...")
def test_mtglpy_topvine(): fn = path('vinemtg_n.lpy') if not fn.exists(): return l = Lsystem(str(fn)) parameters = {} parameters['carto_file'] = 'geom_n.dat' parameters['picle_file'] = 'vine_n.dat' parameters['TTfin'] = None #l.context().updateNamespace(parameters) c_iter = l.getLastIterationNb() nbstep = l.derivationLength - c_iter tree = l.iterate(l.axiom,c_iter,nbstep) scene = l.sceneInterpretation(tree) mtg = lpy2mtg(tree, l, scene) print len(mtg) axial_tree = AxialTree() axial_tree = mtg2lpy(mtg, l, axial_tree) g = lpy2mtg(axial_tree, l, scene) assert len(g) == len(mtg) #axial_tree = mtg2axialtree(mtg, scale, parameters, axial_tree) # Check assert True return mtg, tree, axial_tree
def test_mtglpy_topvine(): fn = path('vinemtg_n.lpy') if not fn.exists(): return l = Lsystem(str(fn)) parameters = {} parameters['carto_file'] = 'geom_n.dat' parameters['picle_file'] = 'vine_n.dat' parameters['TTfin'] = None #l.context().updateNamespace(parameters) c_iter = l.getLastIterationNb() nbstep = l.derivationLength - c_iter tree = l.iterate(l.axiom, c_iter, nbstep) scene = l.sceneInterpretation(tree) mtg = lpy2mtg(tree, l, scene) print(len(mtg)) axial_tree = AxialTree() axial_tree = mtg2lpy(mtg, l, axial_tree) g = lpy2mtg(axial_tree, l, scene) assert len(g) == len(mtg) #axial_tree = mtg2axialtree(mtg, scale, parameters, axial_tree) # Check assert True return mtg, tree, axial_tree
def run_lsys(**kwds): params = {k: v for k, v in _lsys_params.iteritems()} params.update(kwds) l = Lsystem('sensitivityanalysis.lpy', params) lstring = l.iterate() lscene = l.sceneInterpretation(lstring) return lstring, lscene
def update_itable(self): path = str(self.dirname / 'index-table.json') self.write_itable(self.itable, path) # update combi_parameters.csv path = str(self.dirname / 'combi_params.csv') parameters = self.itable.values() allkeys = set().union(*parameters) def _missing(d): return len(allkeys - set(d.keys())) if any([_missing(p) for p in parameters]): self.activate() new = [] for p in parameters: lsys = Lsystem(self.walter, {'params': p}) newp = { k: lsys.context().locals().get(k, 'undef') for k in allkeys } new.append(newp) parameters = new combi = [] for k, v in zip(self.itable, parameters): d = {'ID': k} d.update(v) combi.append(d) df = pd.DataFrame(combi) df.to_csv(path, index=False, sep='\t')
def str2mtg(s): #s = s.replace('N', 'F') tree = AxialTree(s) l = Lsystem() l.addInterpretationRule('N --> F', 0) geom_tree = l.homomorphism(tree) scene = l.sceneInterpretation(geom_tree) scale = dict(zip(('P','A','N', 'L', 'F'),(1,2,3,3,3))) mtg = axialtree2mtg(tree, scale, scene) return tree, mtg, scene
def test_cut_bug(): lsystem_file = pj(data_access.get_data_dir(), 'check_cut_module.lpy') lsys = Lsystem(lsystem_file) lstring = lsys.iterate() # cut branches new_lstring = force_cut(lstring) lscene = lsys.sceneInterpretation(lstring) for sid in lscene.todict(): assert sid in range(len(lstring)) # suucceed if correction is needed (old lpy version) # assert new_lstring[sid].name == 'Internode' # succeed on more recent lpy version assert lstring[sid].name == 'Internode'
def lsystem(file_name, axiom = '', derivationlength = -1, parameters = {}): """ Build a lsystem object from file_name """ l = Lsystem(file_name, parameters) if len(axiom): l.makeCurrent() if type(axiom) != AxialTree: axiom = AxialTree(axiom) l.axiom = axiom #l.done() if derivationlength >= 0: l.derivationLength = derivationlength return l
def __init__(self, *args, **kwargs): PythonModel.__init__(self, *args, **kwargs) if self.name is None: self.name = 'LPyModel' self._step = 1 self.lsystem = Lsystem() self.axialtree = AxialTree() self.scene_name = self.name + '_scene'
def generate(seed = 0, tree = 0, fdist = 4, regenerate = False): from openalea.lpy import Lsystem import os rep = glob.glob(pattern.format(tree, seed, '*' , fdist)) if len(rep) == 2 and not regenerate: print('Do not regenerate', seed, tree, fdist) return True l = Lsystem(lsysfile,{'RESOLUTION' : 1, 'daystep' : 30, 'TIMEBAR' : False, 'LEAFY' : True, 'WITH_INFLO' : True, 'EXPORT_TO_MTG' : False, 'TREE' : tree, 'SEED' : seed, 'FRUITBRANCHSIZE' : fdist}) if l.tree_load: print('Generate', seed, tree, fdist) try: lstring = l.derive() return True except: return False else : print('Not loaded tree', tree) return False
def run(self, sim_id=None, dry_run=False, **kwds): """Run WALTer in project dir Parameters ---------- sim_id: simulation identifier dry_run: (bool) prevent running the simulation (do only side effects). **kwds: walter parameters values (as named arguments) Examples -------- p = Project() p.run(nb_plt_utiles=1, dist_border_x=0, dist_border_y=0, nbj=30, beginning_CARIBU=290) Returns ------- the lsystem and lstring generated by the run """ self.activate() already_known_id = self.itable.keys() if sim_id is None: sim_id = self.get_id(kwds) if sim_id not in already_known_id: self.update_itable() lsys, lstring = None, None if not dry_run: lsys = Lsystem(self.walter, {'params': kwds, 'ID': sim_id}) time_start = time.time() lstring = lsys.iterate() time_stop = time.time() with open( self.output_path(sim_id=sim_id) / "Simulation_time.txt", 'w') as time_file: time_file.write(str(time_stop - time_start)) return lsys, lstring
def generate_bgeom(step=None, endstep=None): from openalea.lpy import Lsystem import os print('Scene generator launched') l = Lsystem( lsysfile, { 'SEED': 0, 'TREE': 0, 'RESOLUTION': 2, 'TIMESTEP': 1, 'TIMEBAR': False, 'LEAFY': True, 'WITH_INFLO': True, 'TEXTURE': True, 'GENERALIZEDCYLINDER': True, 'WITH_GLM': True, 'FRUIT_MODEL': False, 'GLM_RESTRICTION': None, '_GLM_TYPE': 3, 'EXPORT_TO_MTG': False }) if step is None: firststep = 0 endstep = l.derivationLength else: firststep = step if endstep is None: endstep = l.derivationLength else: assert endstep < l.derivationLength open(stepfile, 'w').write(str(endstep)) if not os.path.exists(workingrep): os.makedirs(workingrep) for step in range(firststep, endstep): if step == firststep: lstring = l.derive(firststep + 1) else: lstring = l.derive(lstring, step, 1) lscene = l.sceneInterpretation(lstring) fname = join(workingrep, bgeomfile.format(str(step).zfill(4))) lscene.save(tempbgeomfile) os.rename(tempbgeomfile, fname) print("Scene", step, "generated ...")
class Vine(object): def __init__(self, lpy_filename = vinedir + '/topvine.lpy'): self.lpy_filename = lpy_filename inter_row = 2.2# m inter_plant = 1.1 self.domain_area = 3 * inter_plant * inter_row def setup_canopy(self, age=0): self.start = age self.lsys = Lsystem(self.lpy_filename) tree = run(self.lsys, nbstep = int(2 + age)) g = lpy2mtg(tree,self.lsys) return g def grow(self,g,time_control): if len(time_control) > 0: axiom = mtg2lpy(g,self.lsys) #time_control.check('dt',1) # /!\ TEMP /!\ ###################################### dt = 1 tree = run(self.lsys,axiom = axiom, nbstep = dt) # /!\ TEMP /!\ ###################################### # tree = run(self.lsys,axiom = axiom, nbstep = time_control.dt) return lpy2mtg(tree,self.lsys) else : return g def reset(self): if self.start is None: self.start = 0 return setup_canopy(self.start) def plot(self,g): tree = mtg2lpy(g,self.lsys) self.lsys.plot(tree) return g def generate_scene(self,g): tree = mtg2lpy(g,self.lsys) self.lsys.sceneInterpretation(tree) return generateScene(tree)
def test_mtglpy(): fn = path('ex_luz4.lpy') if not fn.exists(): return l = Lsystem('ex_luz4.lpy') tree = l.iterate() scene = l.sceneInterpretation(tree) mtg = lpy2mtg(tree, l, scene) print(len(mtg)) axial_tree = AxialTree() axial_tree = mtg2lpy(mtg, l, axial_tree) print(len(axial_tree)) #axial_tree = mtg2axialtree(mtg, scale, parameters, axial_tree) # Check assert True return mtg, tree
def test_mtglpy(): fn = path('ex_luz4.lpy') if not fn.exists(): return l = Lsystem('ex_luz4.lpy') tree = l.iterate() scene = l.sceneInterpretation(tree) mtg = lpy2mtg(tree, l, scene) print len(mtg) axial_tree = AxialTree() axial_tree = mtg2lpy(mtg, l, axial_tree) print len(axial_tree) #axial_tree = mtg2axialtree(mtg, scale, parameters, axial_tree) # Check assert True return mtg, tree
def lpy_plot(self, line): from math import ceil, sqrt, floor try: ip = get_ipython() except NameError: ip = None args = parse_argstring(self.lpy_plot, line) file = args.file sizes = [int(i.strip()) for i in args.size.split(',')] cell = args.cell size_display = (int(sizes[0]), int(sizes[1])) if len(sizes) > 1 else (int(sizes[0]), int(sizes[0])) ls = Lsystem(file) rows = cols = ceil(sqrt(ls.derivationLength + 1)) size = rows * cell start = -size / 2 + cell / 2 sw = SceneWidget(size_display=size_display, size_world=size) sw.add(ls.sceneInterpretation(ls.axiom), position=(start, start, 0)) def plot(): tree = ls.axiom for i in range(1, ls.derivationLength): row = floor(i / rows) col = (i - row * cols) x = row * cell + start y = col * cell + start tree = ls.derive(tree, i, 1) sw.add(ls.sceneInterpretation(tree), (x, y, 0)) ip.events.unregister('post_run_cell', plot) if ip: ip.events.register('post_run_cell', plot) return sw
def force_cut(lstring): fake_rule = """ Fake: produce Fake""" fake_lsys = Lsystem() fake_lsys.addRule(fake_rule) fake_lsys.axiom = lstring return fake_lsys.iterate()
def run_lsystem(lsystem='simple_maize_nolight.lpy', parameters=None): if parameters: lsys = Lsystem(lsystem, {'parameters': parameters}) else: lsys = Lsystem(lsystem) lstring = lsys.iterate() lscene = lsys.sceneInterpretation(lstring) return lsys, lstring, lscene
def generate_mtg(trees=list(range(3)), params=dict()): from openalea.lpy import Lsystem from openalea.mtg import MTG from openalea.mtg.algo import union g = None for tree in trees: print('Generate tree', tree) nparams = params.copy() nparams.update({ 'TREE': tree, 'TIMESTEP': 180 if not params['FRUIT_MODEL'] else 90, 'EXPORT_TO_MTG': True, 'PARALLELFRUITMODEL': False }) nparams.setdefault('WITH_GLM', True) l = Lsystem(basename(lsysfile), nparams) lstring = l.iterate() resmtg = l.resultmtg assert type(resmtg) == MTG if g is None: g = resmtg else: g = union(g, resmtg) return g
def str2mtg(s): #s = s.replace('N', 'F') tree = AxialTree(s) l = Lsystem() l.addInterpretationRule('N --> F', 0) geom_tree = l.homomorphism(tree) scene = l.sceneInterpretation(geom_tree) scale = dict(list(zip(('P', 'A', 'N', 'L', 'F'), (1, 2, 3, 3, 3)))) mtg = axialtree2mtg(tree, scale, scene) return tree, mtg, scene
def parse_extern_modules(lpyfile): """ Parse an lpyfile to retrieve its modules and expected extern parameters """ lines = list(open(lpyfile).readlines()) externs = set() def f(**kwd): nonlocal externs externs = externs.union(set(kwd.keys())) code = ''.join([l for l in lines if l.startswith('extern')]) n = {'extern' : f, 'externs' : externs} exec(code, n, n) code2 = ''.join([l for l in lines if l.startswith('module')]) from openalea.lpy import Lsystem l = Lsystem() l.setCode(code2) l.makeCurrent() modules = {} for m in l.execContext().declaredModules(): modules[m.name] = m.parameterNames l.done() return externs, modules
def lsystem(file_name, axiom='', derivationlength=-1, parameters={}): """ Build a lsystem object from file_name """ l = Lsystem(file_name, parameters) if len(axiom): l.makeCurrent() if type(axiom) != AxialTree: axiom = AxialTree(axiom) l.axiom = axiom #l.done() if derivationlength >= 0: l.derivationLength = derivationlength return l
def test_run(overwrite_desired_data=False): lpy_filename = os.path.join(lgrass.__path__[0], "lgrass.lpy") lsys = Lsystem(lpy_filename) axiom = lsys.axiom lsys.INPUTS_DIRPATH = INPUTS_DIRPATH lsys.OUTPUTS_DIRPATH = OUTPUTS_DIRPATH lsys.derivationLength = NSTEP lsys.DureeExp = NSTEP lstring = lsys.derive(axiom, NSTEP) # convert the outputs to Pandas dataframe surface_biomass = pd.read_csv(lsys.chemin_fichier1.name) evol = pd.read_csv(lsys.chemin_fichier2.name) output_induction_file_path = os.path.join(OUTPUTS_DIRPATH, 'output_induction.csv') output_induction = pd.read_csv(output_induction_file_path) output_organ_lengths_file_path = os.path.join(OUTPUTS_DIRPATH, 'output_organ_lengths.csv') output_organ_lengths = pd.read_csv(output_organ_lengths_file_path) # # compare outputs compare_actual_to_desired(OUTPUTS_DIRPATH, surface_biomass, DESIRED_SORTIE_SURFACE_BIOMASS_FILENAME, lsys.chemin_fichier1.name, overwrite_desired_data) compare_actual_to_desired(OUTPUTS_DIRPATH, evol, DESIRED_SERIE_FOLIAIRE_FILENAME, lsys.chemin_fichier2.name, overwrite_desired_data) compare_actual_to_desired(OUTPUTS_DIRPATH, output_induction, DESIRED_OUTPUT_INDUCTION_FILENAME, output_induction_file_path, overwrite_desired_data) compare_actual_to_desired(OUTPUTS_DIRPATH, output_organ_lengths, DESIRED_OUTPUT_ORGAN_LENGTHS_FILENAME, output_organ_lengths_file_path, overwrite_desired_data) if overwrite_desired_data: print("New desired files written") else: print("Test passed successfully")
flowering_param.param.temp_vern_min = x[0] flowering_param.param.temp_vern_inter = x[1] flowering_param.param.temp_vern_max = x[2] flowering_param.param.daily_vern_rate = x[3] flowering_param.param.basic_vern_rate = x[4] flowering_param.param.photoperiod_min = x[5] flowering_param.param.photoperiod_max = x[6] flowering_param.param.max_photo_ind_rate = x[7] flowering_param.param.coeff_primordia_emission_vegetative = x[8] flowering_param.param.coeff_primordia_emission_reproductive = x[9] name = str(x[10][0]) + "_" + str(x[10][1]) print(name) names.append(name) lpy_filename = os.path.join('lgrass.lpy') testsim[name] = Lsystem(lpy_filename) testsim[name].derivationLength = 200 testsim[name].meteo_path = 'D:/Simon/Comites_de_these/Comite_de_these_2/Modelisation/Meteo_sites_GEVES_daylength.csv' testsim[name].sowing_date = x[10][1] testsim[name].site = x[10][0] testsim[name].flowering_model = flowering_param testsim[name].output_induction_file_name = 'induction_' + name testsim[name].output_organ_lengths_file_name = 'organ_lengths_' + name # function to run an L-system from the 'testsim' dictionnary def runlsystem(n): testsim[names[n]].derive() # permet le declenchement de la fonction "End" du script lpy # print(testsim[names[n]].output_dict) # with open(os.path.join(OUTPUTS_DIRPATH, 'sortie_test_path', str(n) + '.csv'), 'wb') as sortie_test_path:
# -*- coding: utf-8 -*- """Simple converter of lpy files to ply format.""" import os import sys from openalea.lpy import Lsystem from openalea.plantgl.all import Tesselator usage = """Convert lpy file to ply format usage: {} FILE""" if len(sys.argv) != 2: print(usage.format(__name__)) lpy_file = sys.argv[1] lsys = Lsystem(lpy_file) ply_file = os.path.splitext( os.path.basename(os.path.expanduser(lpy_file)))[0] ply_file = ply_file + '.ply' n = lsys.derivationLength tree = lsys.axiom for i in range(n): # Apply rewritting rules on the tree -> One step of simulation tree = lsys.iterate(tree, 1) scene = lsys.sceneInterpretation(tree) d = Tesselator() nind = 0 nvert = 0 vert_part = ''
def import_lpy_controls(filepath): if not path(filepath).isfile(): return control = dict() f = open(filepath, 'r') script = f.read() f.close() if script is None: script = "" beginTag = LpyParsing.InitialisationBeginTag if not beginTag in script: return str(script), control else: txts = str(script).split(beginTag) new_script = txts[0] context_to_translate = txts[1] context = Lsystem().context() context.initialiseFrom(beginTag + context_to_translate) managers = get_managers() visualparameters = [] scalars = [] functions = [] curves = [] geoms = [] lpy_code_version = 1.0 if context.has_key('__lpy_code_version__'): lpy_code_version = context['__lpy_code_version__'] if context.has_key('__scalars__'): scalars_ = context['__scalars__'] scalars = [ProduceScalar(v) for v in scalars_] if context.has_key('__functions__') and lpy_code_version <= 1.0: functions = context['__functions__'] for n, c in functions: c.name = n functions = [c for n, c in functions] funcmanager = managers['Function'] geoms += [(funcmanager, func) for func in functions] if context.has_key('__curves__') and lpy_code_version <= 1.0: curves = context['__curves__'] for n, c in curves: c.name = n curves = [c for n, c in curves] curvemanager = managers['Curve2D'] geoms += [(curvemanager, curve) for curve in curves] if context.has_key('__parameterset__'): for panelinfo, objects in context['__parameterset__']: for typename, obj in objects: visualparameters.append((managers[typename], obj)) for scalar in scalars: control[unicode(scalar.name)] = scalar.value for (manager, geom) in geoms: if geom != list(): control[geom.getName()] = geom for (manager, geom) in visualparameters: if geom != list(): control[geom.getName()] = geom new_controls = [] for name, value in control.items(): interfaces = guess_interface(value) if interfaces: new_controls.append(Control(name, interfaces[0], value)) try: control["color map"] = to_color(context.turtle.getColorList()) except AttributeError: pass else: new_controls.append( Control("color map", 'IColorList', control["color map"])) for control in new_controls: register_control(control)
def shoot_grow(g, demand_only=True): lsys = Lsystem(str(os.path.join(lsysdir, 'morphogenesis.lpy')), {'demand_only': demand_only}) lsys.axiom = mtg2lpy(g, lsys, AxialTree()) axt = lsys.iterate() scene = lsys.sceneInterpretation(axt) return lpy2mtg(axt, lsys, scene)
def shoot_init(carbon_seed_stock=0.1): lsys = Lsystem(str(os.path.join(lsysdir, 'morphogenesis.lpy')), {'carbon_seed_stock': carbon_seed_stock}) axialtree = lsys.axiom scene = lsys.sceneInterpretation(axialtree) return lpy2mtg(axialtree, lsys, scene)
def import_lpy_controls(filepath): if not path(filepath).isfile(): return control = dict() f = open(filepath, "r") script = f.read() f.close() if script is None: script = "" beginTag = LpyParsing.InitialisationBeginTag if not beginTag in script: return str(script), control else: txts = str(script).split(beginTag) new_script = txts[0] context_to_translate = txts[1] context = Lsystem().context() context.initialiseFrom(beginTag + context_to_translate) managers = get_managers() visualparameters = [] scalars = [] functions = [] curves = [] geoms = [] lpy_code_version = 1.0 if context.has_key("__lpy_code_version__"): lpy_code_version = context["__lpy_code_version__"] if context.has_key("__scalars__"): scalars_ = context["__scalars__"] scalars = [ProduceScalar(v) for v in scalars_] if context.has_key("__functions__") and lpy_code_version <= 1.0: functions = context["__functions__"] for n, c in functions: c.name = n functions = [c for n, c in functions] funcmanager = managers["Function"] geoms += [(funcmanager, func) for func in functions] if context.has_key("__curves__") and lpy_code_version <= 1.0: curves = context["__curves__"] for n, c in curves: c.name = n curves = [c for n, c in curves] curvemanager = managers["Curve2D"] geoms += [(curvemanager, curve) for curve in curves] if context.has_key("__parameterset__"): for panelinfo, objects in context["__parameterset__"]: for typename, obj in objects: visualparameters.append((managers[typename], obj)) for scalar in scalars: control[unicode(scalar.name)] = scalar.value for (manager, geom) in geoms: if geom != list(): control[geom.getName()] = geom for (manager, geom) in visualparameters: if geom != list(): control[geom.getName()] = geom new_controls = [] for name, value in control.items(): interfaces = guess_interface(value) if interfaces: new_controls.append(Control(name, interfaces[0], value)) try: control["color map"] = to_color(context.turtle.getColorList()) except AttributeError: pass else: new_controls.append(Control("color map", "IColorList", control["color map"])) for control in new_controls: register_control(control)
def createtree(path): lsys = Lsystem(path) lstring = lsys.iterate() lscene = lsys.sceneInterpretation(lstring) return lscene
# :return: adapted axialtree # """ # # def repr_geom(self): # return __scene # # scene = lsystem.sceneInterpretation(axialtree) # axialtree.__scene = scene # axialtree._repr_geom_ = types.MethodType(repr_geom, axialtree) # # return axialtree context = {} lsystem = Lsystem() axialtree = AxialTree() # axialtree = adapt_axialtree(axialtree, lsystem) code = ''' """ input = lstring="_(0.01)-(90)F(1)" output = lstring """ N = 2 derivation length: N production:
def runmodel(fname): from openalea.lpy import Lsystem l = Lsystem(fname) lstring = l.iterate() l.plot(lstring)
def setup_canopy(self, age=0): self.start = age self.lsys = Lsystem(self.lpy_filename) tree = run(self.lsys, nbstep = int(2 + age)) g = lpy2mtg(tree,self.lsys) return g
def animatemodel(fname): from openalea.lpy import Lsystem l = Lsystem(fname) l.animate()
# :param lsystem: lsystem that can be used to create the 3d representation of axialtree # :return: adapted axialtree # """ # # def repr_geom(self): # return __scene # # scene = lsystem.sceneInterpretation(axialtree) # axialtree.__scene = scene # axialtree._repr_geom_ = types.MethodType(repr_geom, axialtree) # # return axialtree context = {} lsystem = Lsystem() axialtree = AxialTree() # axialtree = adapt_axialtree(axialtree, lsystem) code = ''' """ input = lstring="_(0.01)-(90)F(1)" output = lstring """ N = 2 derivation length: N production:
def run_lgrass(scenario_id=1, inputs_dir_path='inputs', outputs_dir_path='outputs'): """ Run lgrass.lpy file from external script :param str inputs_dir_path: the path to the input files (meteo, list of simulations...) :param str outputs_dir_path: the path to the outputs files :param int scenario_id: the index of the scenario to be run from the list of simulations CSV file """ # Scenario to be run scenarii_df = pd.read_csv(os.path.join('inputs', 'plan_simulation.csv'), index_col='Scenario') scenario = scenarii_df.loc[scenario_id].to_dict() scenario_name = scenario['name'] # Update parameters of flowering model flowering_model = lgrass.flowering_functions.FloweringFunctions() flowering_model.param.__dict__.update((k, scenario[k]) for k in set(scenario).intersection(flowering_model.param.__dict__)) # Load lsystem lpy_filename = os.path.join(lgrass.__path__[0], "lgrass.lpy") lsystem = Lsystem(lpy_filename) # Update parameters lsystem.dt = 1 lsystem.option_profile = "plateau" lsystem.flowering_model = flowering_model lsystem.derivationLength = int(scenario['derivationLength']) lsystem.option_tallage = scenario['option_tallage'] lsystem.option_senescence = scenario['option_senescence'] lsystem.option_floraison = scenario['option_floraison'] lsystem.meteo_path = os.path.join(inputs_dir_path, scenario['meteo_filename']) lsystem.sowing_date = scenario['sowing_date'] lsystem.site = scenario['site'] lsystem.output_induction_file_name = '{}_induction'.format(scenario_name) lsystem.output_organ_lengths_file_name = '{}_organ_lengths'.format(scenario_name) lsystem.ParamP[0]['C'] = scenario['value_C'] lsystem.ParamP[0]['Premiecroiss'] = scenario['Premiecroiss'] lsystem.ParamP[0]['PS_compensation_point'] = scenario['PS_compensation_point'] if outputs_dir_path: lsystem.OUTPUTS_DIRPATH = outputs_dir_path # Run the lsystem try: lsystem.derive() # permet le declenchement de la fonction "End" du script lpy lsystem.clear() except Exception as e: print(e)
if len(sys.argv) == 2: out_meth = 'ply' else: out_meth = sys.argv[2] # Connect to I/O channels in1 = CisInput('LPy_time') if out_meth == 'ply': out = CisPlyOutput('LPy_mesh') elif out_meth == 'obj': out = CisObjOutput('LPy_mesh') else: out = CisAsciiArrayOutput('LPy_mesh', '%f\t%f\t%f\t%f\t%f\t%f\t%f\t%f\t%f\n') # Create lsystem & discretizer lsys = Lsystem(fname) d = Tesselator() # Continue looping until no more input times # TODO: Automated unit conversion mm_to_cm = 0.1 flag = True while (flag): # Receive next step flag, result = in1.recv() if not flag: print("LPy: End of input.") break niter = result[0] print("LPy: Received request for step %d" % niter)
def import_lpy_file(script): """ Extract from an "old style" LPy file script part (str) and associated control (dict). Permit compatibility between LPy and OALab. :param: script to filter (str) :return: lpy script (str) without end begining with "###### INITIALISATION ######" and a dict which contain the control (dict) """ control = dict() if script is None: script = "" beginTag = LpyParsing.InitialisationBeginTag if not beginTag in script: return str(script), control else: txts = str(script).split(beginTag) new_script = txts[0] context_to_translate = txts[1] context = Lsystem().context() context.initialiseFrom(beginTag + context_to_translate) managers = get_managers() visualparameters = [] scalars = [] functions = [] curves = [] geoms = [] lpy_code_version = 1.0 if context.has_key('__lpy_code_version__'): lpy_code_version = context['__lpy_code_version__'] if context.has_key('__scalars__'): scalars_ = context['__scalars__'] scalars = [ProduceScalar(v) for v in scalars_] if context.has_key('__functions__') and lpy_code_version <= 1.0: functions = context['__functions__'] for n, c in functions: c.name = n functions = [c for n, c in functions] funcmanager = managers['Function'] geoms += [(funcmanager, func) for func in functions] if context.has_key('__curves__') and lpy_code_version <= 1.0: curves = context['__curves__'] for n, c in curves: c.name = n curves = [c for n, c in curves] curvemanager = managers['Curve2D'] geoms += [(curvemanager, curve) for curve in curves] if context.has_key('__parameterset__'): for panelinfo, objects in context['__parameterset__']: for typename, obj in objects: visualparameters.append((managers[typename], obj)) control["color map"] = context.turtle.getColorList() for scalar in scalars: control[unicode(scalar.name)] = scalar for (manager, geom) in geoms: if geom != list(): new_obj, new_name = geometry_2_piklable_geometry(manager, geom) control[new_name] = new_obj for (manager, geom) in visualparameters: if geom != list(): new_obj, new_name = geometry_2_piklable_geometry(manager, geom) control[new_name] = new_obj return new_script, control
class LPyModel(PythonModel): dtype = 'LSystem' mimetype = 'text/vnd-lpy' def __init__(self, *args, **kwargs): PythonModel.__init__(self, *args, **kwargs) if self.name is None: self.name = 'LPyModel' self._step = 1 self.lsystem = Lsystem() self.axialtree = AxialTree() self.scene_name = self.name + '_scene' def __copy__(self): m = PythonModel.__copy__(self) m.set_code(self._initial_code) m.lsystem = self.lsystem m.axialtree = self.axialtree m.scene_name = self.scene_name return m def init(self, *args, **kwds): self._step = 1 self._push_ns() self._fill_namespace(*args, **kwds) # BEGIN ACTUAL CODE RUN self.lsystem.setCode(str(self.code), self._ns) if "axiom" in self._ns and self._ns['axiom']: self.lsystem.axiom = self._ns['axiom'] elif "lstring" in self._ns and self._ns['lstring']: self.lsystem.axiom = self._ns['lstring'] self.axialtree = self.lsystem.axiom # END ACTUAL CODE RUN self._populate_ns() self._pop_ns() return self.output_from_ns(self._ns) def output_from_ns(self, namespace): # get outputs from namespace outputs = [] if self.outputs_info: if len(self.outputs_info) > 0: for outp in self.outputs_info: if outp.name.lower() in ["axialtree", "lstring"]: outputs.append(self.axialtree) elif outp.name.lower() == "lsystem": outputs.append(self.lsystem) elif outp.name.lower() == "scene": outputs.append(self.lsystem.sceneInterpretation(self.axialtree)) elif outp.name in namespace: outputs.append(namespace[outp.name]) else: continue # Add output to namespace # if output is not in namespace, this line is not run (see "continue" above) self._ns[outp.name] = outputs[-1] if len(outputs) == 0: return None elif len(outputs) == 1: return outputs[0] else: return outputs def run(self, *args, **kwds): nstep = kwds.pop('nstep', None) self.init(*args, **kwds) if nstep is None: self.axialtree = self.lsystem.iterate() else: self.axialtree = self.lsystem.iterate(nstep) self.lsystem.context().getNamespace(self._ns) self.outputs = self.output_from_ns(self._ns) return self.outputs def step(self, *args, **kwds): nstep = kwds.pop('nstep', None) if self._step > self.lsystem.derivationLength: self._step = 0 if nstep is None: self.axialtree = self.lsystem.iterate(self._step) else: self.axialtree = self.lsystem.iterate(nstep) self.outputs = self.output_from_ns(self._ns) self._step += 1 return self.outputs def animate(self, *args, **kwds): self.init() self.axialtree = self.lsystem.animate() self.outputs = self.output_from_ns(self._ns) self._step = self.lsystem.derivationLength + 1 return self.outputs def set_code(self, code): """ Set the content and parse it to get docstring, inputs and outputs info, some methods """ self._initial_code, control = import_lpy_file(code) self._doc = get_docstring(code) docstring = parse_lpy(code) if docstring is not None: model, self.inputs_info, self.outputs_info = parse_doc(docstring) # Default input if self.inputs_info == []: self.inputs_info = [InputObj('lstring:IStr')] # Default output if self.outputs_info == []: self.outputs_info = [OutputObj("lstring:IStr")] def _set_code(self, code): self.set_code(code) def _set_axialtree(self, axialtree): self._axialtree = adapt_axialtree(axialtree, self.lsystem) code = property(fget=lambda self: self._initial_code, fset=_set_code) axialtree = property(fget=lambda self: self._axialtree, fset=_set_axialtree)