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 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 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 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 lpy(self, line, cell=None, local_ns=None): ''' .. todo:: Update the docstring Execute code in Lpy, and pull some of the results back into the Python namespace. In [9]: %lpy -w axiom -n 10 -g As a cell, this will run a block of Lpy code, without returning any value:: In [10]: %%lpy ....: p = [-2, -1, 0, 1, 2] ....: polyout(p, 'x') -2*x^4 - 1*x^3 + 0*x^2 + 1*x^1 + 2 In the notebook, plots are published as the output of the cell, e.g. %lpy plot([1 2 3], [4 5 6]) will create a line plot. Objects can be passed back and forth between Lpy and IPython via the -i and -o flags in line:: In [14]: Z = np.array([1, 4, 5, 10]) In [15]: %lpy -i Z mean(Z) Out[15]: array([ 5.]) In [16]: %lpy -o W W = Z * mean(Z) Out[16]: array([ 5., 20., 25., 50.]) In [17]: W Out[17]: array([ 5., 20., 25., 50.]) The size and format of output plots can be specified:: In [18]: %%lpy -s 600,800 -f svg ...: plot([1, 2, 3]); ''' args = parse_argstring(self.lpy, line) # arguments 'code' in line are prepended to the cell lines if cell is None: code = '' return_output = True else: code = cell return_output = False code = ' '.join(args.code) + code code = unicode_to_str(code) # if there is no local namespace then default to an empty dict if local_ns is None: local_ns = {} parameters = {} if args.input: for input in ','.join(args.input).split(','): input = unicode_to_str(input) if input == '*': parameters.update(self.shell.user_ns) else: try: val = local_ns[input] except KeyError: val = self.shell.user_ns[input] parameters[input] = val if parameters: self._lsys.context().updateNamespace(parameters) if code: self._lsys.setCode(code, parameters) ################################################# # set, input workstring = '' if args.workstring: workstring = args.workstring[0] workstring = unicode_to_str(workstring) try: workstring = local_ns[workstring] except KeyError: workstring = self.shell.user_ns[workstring] except: pass if isinstance(workstring, str): self._lsys.makeCurrent() workstring = lpy.AxialTree(workstring) self._lsys.done() elif isinstance(workstring, MTG): workstring = mtg2lpy(workstring, self._lsys) else: pass # try: # ws = local_ns[workstring] # except KeyError: # ws = self.shell.user_ns[workstring] n = 1 if args.nbstep: n = int(args.nbstep[0]) if workstring: tree = self._lsys.iterate(workstring, n) else: n = self._lsys.derivationLength c_iter = self._lsys.getLastIterationNb() if not workstring: workstring = self._lsys.axiom if len(parameters) > 0: self._lsys.context().updateNamespace(parameters) print('DEBUG: ', workstring, c_iter, n) tree = self._lsys.iterate(workstring, c_iter, n) if args.axialtree: axial_name = unicode_to_str(args.axialtree[0]) self.shell.push({axial_name: tree}) scene = self._lsys.sceneInterpretation(tree) if args.scene and scene: self.shell.push({args.scene[0]: scene}) mtg = None if args.mtg: mtg_name = unicode_to_str(args.mtg[0]) mtg = lpy2mtg(tree, self._lsys, scene=scene) self.shell.push({mtg_name: mtg}) if args.format is not None: plot_format = args.format else: plot_format = 'png' key = 'LpyMagic.Lpy' display_data = {} # Publish text output """ if text_output: display_data.append((key, {'text/plain': text_output})) """ # Publish images image = self._plot3d(scene, format=plot_format) if image is not None: plot_mime_type = _mimetypes.get(plot_format, 'image/png') #width, height = [int(s) for s in size.split(',')] #for image in images: display_data[plot_mime_type] = image """ if args.output: for output in ','.join(args.output).split(','): output = unicode_to_str(output) self.shell.push({output: self._oct.get(output)}) """ #for source, data in display_data: # self._publish_display_data(source, data) self._publish_display_data(data=display_data) if return_output: return tree if not mtg else mtg