Beispiel #1
0
def duplicate(g, n=1):
    if g.nb_vertices(scale=1) == n:
        return g
    g1 = g.sub_mtg(g.root)
    for i in range(n-1):
        g1 = union(g1,g)
    return g1
Beispiel #2
0
def import_mtgfile(filename):
    """
    parameters:
    -----------
    filename : names of mtg
    
    return:
    -------
    a function which import mtg corresponding to the mtg names
    """
    def name(f):
        "return base name without extension"
        return f.basename().splitext()[0]

    filenames = filename
    files = shared_data(openalea.strawberry).glob('*.mtg')
    mtg_path = dict((name(f), f) for f in files)
    mtgfile = dict((k, f) for k, f in mtg_path.items() if k in filenames)
    if len(filenames) == 1:
        g = MTG(mtgfile[filenames[0]])
        return g
    else:
        metaMTG = MTG()
        for i in mtgfile:
            metaMTG = algo.union(metaMTG, MTG(mtgfile[i]))
        return metaMTG
Beispiel #3
0
def duplicate(g, replicates=1, grem=None):
    """construct a mtg replicating g n times and adding grem"""
    g_rep = replicate(g, replicates)
    if grem is not None:
        g = grem
        g = union(g, g_rep)
    else:
        g = g_rep
    return g
Beispiel #4
0
def replicate(g, target=1):
    """ replicate the plants in g up to obtain target new plants"""
    current = g.nb_vertices(scale=1)

    if target <= current:
        return g

    assert target % current == 0
    # otherwise use nrem + union

    nduplication = int(numpy.log2(1. * target / current))
    missing = target - current * numpy.power(2, nduplication)
    cards = numpy.power(2, range(nduplication))
    add_g = [False] * len(cards)
    for i in reversed(range(len(cards))):
        if cards[i] <= missing:
            add_g[i] = True
            missing -= cards[i]
    assert missing == 0

    g_add = None
    g_dup = g.sub_mtg(g.root)
    for i in range(nduplication):
        g1 = g_dup.sub_mtg(g_dup.root)
        g_dup = union(g_dup, g1)
        if add_g[i]:
            g1 = g.sub_mtg(g.root)
            if g_add is None:
                g_add = g1
            else:
                g_add = union(g_add, g1)

    if g_add is not None:
        g_dup = union(g_dup, g_add)

    return g_dup
Beispiel #5
0
def replicate(g, target=1):

    current = g.nb_vertices(scale=1)

    if target <= current:
        return g
    
    assert target % current == 0
    #otherwise use nrem + union
    
    nduplication = int(numpy.log2(1. * target / current))   
    missing = target - current * numpy.power(2,nduplication)
    cards = numpy.power(2, range(nduplication))
    add_g = [False] * len(cards)
    for i in reversed(range(len(cards))):
        if cards[i] <= missing:
            add_g[i] = True
            missing -= cards[i]
    assert missing == 0
    
    g_add = None
    g_dup = g.sub_mtg(g.root)
    for i in range(nduplication):
        g1 = g_dup.sub_mtg(g_dup.root)
        g_dup = union(g_dup, g1)
        if add_g[i]:
            g1 = g.sub_mtg(g.root)
            if g_add is None:
                g_add = g1
            else:
                g_add = union(g_add, g1)
    
    if g_add is not None:        
        g_dup = union(g_dup,g_add)
       
    return g_dup
Beispiel #6
0
    def setup_canopy(self, age = 10):
    
        self.canopy_age = age
        
        # produce plants positionned at origin        
        if self.nrem > 0:
            canopy = RunAdel(age, self.pars_rem, adelpars=self.run_adel_pars)
            grem = mtg_factory(canopy, adel_metamer, leaf_sectors=self.nsect, leaves=self.leaves, stand=None, split=self.split, aborting_tiller_reduction=self.aborting_tiller_reduction)
            grem = mtg_interpreter(grem, self.leaves, face_up = self.face_up, classic= self.classic)
        
        if self.nquot > 0:
            canopy = RunAdel(age, self.pars_quot, adelpars=self.run_adel_pars)
            gquot = mtg_factory(canopy, adel_metamer, leaf_sectors=self.nsect, leaves=self.leaves, stand=None, split=self.split, aborting_tiller_reduction=self.aborting_tiller_reduction)
            gquot = mtg_interpreter(gquot, self.leaves, face_up = self.face_up, classic= self.classic)
            gquot = replicate(gquot, self.nquot * self.duplicate)
            
        if self.nrem > 0:
            g = grem
            if self.nquot > 0:
                g = union(g, gquot)
        else:
            g = gquot
        
        #update positions and domain if smart stand is used       
        if self.stand.density_curve is not None:
            new_nplants, self.domain, self.positions, self.domain_area = self.stand.smart_stand(self.nplants, at=age)
            assert new_nplants == self.nplants

        # dispose plants and renumber them
        pos = g.property('position ')
        az = g.property('azimuth')
        lab = g.property('label')
        geom = g.property('geometry')
        for i,vid in enumerate(g.vertices(1)):
            lab[vid] = 'plant' + str(i + 1)
            pos[vid] = self.positions[i]
            az[vid] = self.plant_azimuths[i]
            for gid in g.components_at_scale(vid, g.max_scale()):
                if gid in geom:
                    geom[gid] = transform_geom(geom[gid], self.positions[i], self.plant_azimuths[i])
        
        return g
Beispiel #7
0
def on_change_get_files(widget, event, data):
    # load mtgs
    misc.all_mtg = MTG()
    for file in data:
        g = read_mtg_file(file_paths[file])
        misc.all_mtg = union(misc.all_mtg, g)

    # update table
    if misc.all_mtg:
        df = get_table_mtg(misc.all_mtg)
        # TODO:
        update_grid(df, tableMTG)
    else:
        update_grid(pd.DataFrame(), tableMTG)

    # update genotype selections
    update_selection_items()

    # update file description
    print_files_description()
Beispiel #8
0
def reader_uploaded():
    if files_upload.get_files():
        upfile = files_upload.get_files()
        for f in upfile:
            # if a mtg file with the same name already exists - nothing is done
            if not os.path.isfile(data_directory + '/' + f['name']):
                # ADD the file to the mtg database
                # TODO: Add the possibility to not add it
                content = f['file_obj'].read()
                tmp = open(data_directory + '/' + f['name'], 'w')
                tmp.write(content.decode('utf-8'))
                tmp.close()
                # then add it to the all_mtg
                g = read_mtg(content.decode('utf-8'))
                misc.all_mtg = union(misc.all_mtg, g)

        global files, file_paths
        files, file_paths = get_files()
        files_selection.items = files
        update_selection_items()
def import_mtgfile(filename):
    """Import a MTG file from genotype name, in sharedata repo

    :param filename: genotype = name of the file
    :type filename: string
    :return: a MTG loaded from the file
    :rtype: MTG
    """
    filenames = filename
    files = shared_data(openalea.strawberry).glob('*.mtg')
    mtg_path = dict((name(f), f) for f in files)
    mtgfile = dict((k, f) for k, f in mtg_path.items() if k in filenames)
    if len(filenames) == 1:
        g = MTG(mtgfile[filenames[0]])
        return g
    else:
        metaMTG = MTG()
        for i in mtgfile:
            metaMTG = algo.union(metaMTG, MTG(mtgfile[i]))
        return metaMTG
Beispiel #10
0
def test_extract_at_plant_scale():
    files = shared_data(openalea.strawberry).glob('*.mtg')

    mtg_path = dict((name(f), f) for f in files)
    mtg = MTG()
    for k, genotype in enumerate(mtg_path):
        if (genotype=="Sicile") or (genotype=="Nils") or (genotype=='origine_stolon_gariguetteDV_6novembre2018') \
        or (genotype=='friendlyfruit_varieties'):
            pass
        else:
            mtg = union(mtg, read_mtg_file(mtg_path[genotype]))

    genotypes = mtg.property('Genotype')

    for geno in set(genotypes.values()):
        vids = [
            vid for vid in mtg.vertices(scale=1) if genotypes.get(vid) == geno
        ]
        df = extract_at_plant_scale(mtg, vids)
        assert not df.empty
        assert set(df['Genotype']) == {geno}
Beispiel #11
0
def mtg_turtle(g, symbols):
    ''' Compute the geometry on each node of the MTG using Turtle geometry. '''

    from openalea.mtg import turtle

    plants = g.component_roots_at_scale_iter(g.root, scale=1)
    nplants = g.nb_vertices(scale=1)

    gt = MTG()

    def adel_visitor(g, v, turtle):
        # 1. retriev the node
        n = g.node(v)
        angle = float(n.Laz) if n.Laz else 0.
        turtle.rollL(angle)
        if g.edge_type(v) == '+':
            if not n.label.startswith('L'):
                angle = n.Ginc or n.Einc
                angle = float(angle) if angle is not None else 0.
                turtle.up(angle)

        # 2. Compute the geometric symbol
        mesh, can_label = compute_element(n, symbols)
        if mesh:
            n.geometry = transform(turtle, mesh)
            n.can_label = can_label

        # 3. Update the turtle
        turtle.setId(v)
        if n.label.startswith('S'):
            turtle.f(n.length)
        # Get the azimuth angle

    for plant in plants:
       gplant = g.sub_mtg(plant)
       scene = turtle.TurtleFrame(gplant,visitor=adel_visitor)
       gt = union(gplant,gt)
       
    return gt
Beispiel #12
0
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
Beispiel #13
0
def test1():
    g = aml.MTG('data/test7.mtg')
    g1 = g.sub_mtg(g.root)
    assert len(g1) == len(g)
    g2 = union(g, g1)
    assert len(g2) == 2 * len(g) - 1