Exemplo n.º 1
0
def index():
    if request.form.has_key("changeCat") or request.form.has_key("changeProb"):
        myObj = None
        for node in roots:
            if (node.nodeType() == "Category" and node.payload.name == request.form['selectedCat']) or node.nodeType() == "Problem":
                myObj = node
                break
        if myObj!=None:
            del roots[:]
            for thing in myObj.returnRootChildren():
                roots.append(thing)
    #else
    elif len(roots) == 0:
        metaData=Tree.query()
        if len(metaData.fetch(None))==0:
            r1 = Node(storeCat("Lawn Equipment"))
            roots.append(r1)
            lm = r1.addSubNode(storeCat("Lawn Mower"))
            we = r1.addSubNode(storeCat("Weed Eater"))
            r1.addSubNode(storeCat("Edger"))

            r2 = Node(storeCat("Mobile Phone"))
            rr2 = r2.addSubNode(storeProb("Are you having a problem?", None))
            roots.append(r2);
            gp = rr2.addSubNode(storeProb("Does the lawn mower have gas?", None))
            rr2.addSubNode(storeProb("Is the lawn mower making noises?", None))
            gp.addSubNode(storeProb(None, "You don't have any gas!"))

            we.addSubNode(storeCat("Torro"))
            honda = lm.addSubNode(storeCat("Honda"))
            bd = lm.addSubNode(storeCat("B&D"))
            honda.addSubNode(storeProb("WOW",None))
            bd.addSubNode(storeCat("itWORKS!"))
            r1.printTree()

            treeDict = r1.convertTree()
            Tree(str(r1.convertTree())).put()
            Tree(str(r2.convertTree())).put()
            r1Prime = Node(treeDict)
            r1Prime.printTree()
        else:
            for probsol in Problem.query(): config.probList.append(probsol)
            for cat in Category.query(): config.catList.append(cat)
            trees = Tree.query()  # get item list
            for tree in trees:  # find correct item
                roots.append(Node(ast.literal_eval(tree.tree)))



    return render_template("testindex.html",
                           roots=roots)
Exemplo n.º 2
0
def checkpoint_model(model_file_name, struct=None, modules=None, model=None, figname='hist.png', data_loader=None):
    if not(os.path.exists(os.path.join("./experiments", args.dataset, args.experiment, args.subexperiment))):
        os.makedirs(os.path.join("./experiments", args.dataset,
                    args.experiment, args.subexperiment, 'figures'))
        os.makedirs(os.path.join("./experiments", args.dataset,
                    args.experiment, args.subexperiment, 'checkpoints'))

    # If model is not given, then build one.
    if not(model) and modules and struct:
        model = Tree(struct, modules, cuda_on=args.cuda)

    # save the model:
    save_dir = "./experiments/{}/{}/{}/{}".format(
        args.dataset, args.experiment, args.subexperiment, 'checkpoints')
    model_path = save_dir + '/' + model_file_name
    torch.save(model, model_path)
    print("Model saved to {}".format(model_path))

    # save tree histograms:
    if args.visualise_split and not(data_loader is None):
        save_hist_dir = "./experiments/{}/{}/{}/{}".format(
            args.dataset, args.experiment, args.subexperiment, 'figures')
        visualise_routers_behaviours(model, data_loader, fig_scale=6, axis_font=20, subtitle_font=20,
                                     cuda_on=args.cuda, objects=args.classes, plot_on=False,
                                     save_as=save_hist_dir + '/' + figname)
Exemplo n.º 3
0
def import_geojson(geojson: GeoJson, request: Request):
    """
    Import trees from GeoJSON
    """
    if not request.headers.get("Master") == os.environ.get("MASTER_TOKEN"):
        raise HTTPException(
            status_code=status.HTTP_401_UNAUTHORIZED,
            detail="Invalid authentication token",
        )

    TreeDB.objects.all().delete()

    for feature in geojson.features:
        tree = Tree(
            species=feature["properties"].get("species", "onbekend"),
            lon=feature["geometry"]["coordinates"][0],
            lat=feature["geometry"]["coordinates"][1],
            oid=feature["properties"].get("oid"),
            notes=feature["properties"].get("notes"),
            tags=feature["properties"].get("tags", []),
            dead=feature["properties"].get("dead", False),
        )

        new_tree = TreeDB(**tree.to_dict())
        new_tree.save()

    return {"detail": f"Imported {len(geojson.features)} features"}
Exemplo n.º 4
0
def create_root():
    myRoot = Node(storeCat(request.args.get("name")))
    roots.append(myRoot)
    print "There were " + str(len(Tree.query().fetch(None))) + " entries."
    Tree(tree=str(myRoot.convertTree())).put()
    print "Now there are " + str(len(Tree.query().fetch(None))) + " entries."
    return get_tree()
Exemplo n.º 5
0
 def test_create_rename_delete(self):
     tree = Tree(alias='mytree')
     tree.save(force_insert=True)
     self.assertIsNotNone(tree.id)
     self.assertEqual(tree.alias, 'mytree')
     tree.alias = 'not_mytree'
     tree.save(force_update=True)
     self.assertEqual(tree.alias, 'not_mytree')
     tree.delete()
     self.assertIsNone(tree.id)
Exemplo n.º 6
0
 def post(self):
     data = json.loads(self.request.body)
     latlng = data["coordinates"]
     m_lat = latlng["lat"]
     m_lng = latlng["lng"]
     number = data["number"]
     date = data['date']
     my_user = users.get_current_user()
     my_userid = my_user.user_id()
     m_email = my_user.nickname()
     # current_user = Planter.query().filter(Planter.user_id == my_userid).fetch()[0]
     # current_user.numberOfTrees = numberOfTrees
     # current_user.put()
     tree = Tree(lat=m_lat, long=m_lng, number=number, user_id=my_userid, email=m_email, date=date)
     tree.put()
Exemplo n.º 7
0
    def add_tree(payload):
        print("inside of the add metod", flush=True)
        body = request.get_json()
        print(body, flush=True)

        new_type = body.get('type', None)

        new_owner = body.get('owner_id', None)
        print("new_owner", new_owner, flush=True)

        new_lat = body.get('latitude', None)
        new_long = body.get('longitude', None)

        new_date = body.get('plantedDate', None)
        print("new_date", new_date, flush=True)

        try:
            # tree = Tree(new_type, int(new_owner))
            # print(tree)

            tree = Tree(new_type, int(new_owner), float(new_lat),
                        float(new_long), new_date)
            tree.insert()

            selection = Tree.query.order_by(Tree.id).all()
            formatted_trees = [tr.format() for tr in selection]

            return jsonify({
                'success': True,
                'created': tree.id,
                'trees': formatted_trees,
                'total_trees': len(selection)
            })

        except:
            abort(422)
Exemplo n.º 8
0
    def save_tree(self,tree_dict):
        
        try:
            tree_dict = self.merged_tree(tree_dict)
        except:
            return False, ['invalid_parameters']
            
        #tree_name,moderatorname,conventions,root_branch_link,root_branch_content
        
        if tree_dict['moderatorname'] is None:
            return False, ['unauthenticated','no_moderator']
        
        if tree_dict['tree_name'] is None:
            return False, ['empty_name']
        
        author_info = UserInfo.get_by_username(tree_dict['moderatorname'])
        
        if author_info is None:
            return False, ['unauthenticated','moderator_not_found']
        
        if not self.is_user_info_current(author_info):
            return False, ['unauthenticated','moderator_not_current']
        
        if author_info.username is None:
            return False, ['invalid_user']
        
        errors = []
        
        if tree_dict['content_max'] < 16:
            errors.append('min_content_max')
            
        if tree_dict['link_max'] < 16:
            errors.append('min_link_max')
            
        if len(tree_dict['link_prompt']) > tree_dict['link_max']:
            errors.append('link_prompt_too_large')
            
        if len(tree_dict['content_prompt']) > tree_dict['content_max']:
            errors.append('content_prompt_too_large')
        
        tree_key = Tree.create_key(tree_dict['tree_name'])
        
        empty_name = tree_dict['tree_name'] is None or len(tree_dict['tree_name']) == 0
        
        if empty_name:
            errors.append('empty_name')
        else:
            match = re.search(r'^[\d\w_\-]+$', tree_dict['tree_name'])
            isvalid = match and 4 <= len(tree_dict['tree_name']) and len(tree_dict['tree_name']) <= 20;
            if not isvalid:
                errors.append('invalid_name')
        
        branch = Branch(id=tree_dict['tree_name'])
        branch.authorname = tree_dict['moderatorname']
        branch.link = tree_dict['root_branch_link']
        branch.content = tree_dict['root_branch_content']
        branch.tree_name = tree_dict['tree_name']
        
        if branch.link == None or len(branch.link) == 0:
            errors.append('empty_root_branch_link')
        
        if branch.content == None or len(branch.content) == 0:
            errors.append('empty_root_branch_content')
        
#let the user complete the other validation before trying to create the tree        
        if len(errors) != 0:
            return False, errors
        
        
        tree = tree_key.get();
        
        if tree:
            errors.append('tree_exists')
        
        if len(errors) == 0:
            #if two users enter identical information at the same time, then
            #whoever gets it second is the winner
            tree = Tree(id=tree_dict['tree_name'].lower(),name=tree_dict['tree_name'])
            tree.moderatorname = tree_dict['moderatorname']
            tree.conventions = tree_dict['conventions']
            
            tree.link_moderator_only = tree_dict['link_moderator_only']
            tree.link_max = tree_dict['link_max']
            tree.link_prompt = tree_dict['link_prompt']
            
            tree.content_moderator_only = tree_dict['content_moderator_only']
            tree.content_max = tree_dict['content_max']
            tree.content_prompt = tree_dict['content_prompt']
            
            tree.single_thread = tree_dict['single_thread']
            
            tree.branch_max = tree_dict['branch_max']
            
            branch.put()
            tree.put()
        
            notification = Notification()
            notification.from_username = tree.moderatorname
            #only set the to_username when a different user is performing the action
            notification.notification_type = 'new_tree'
            notification.tree_name = tree_dict['tree_name']
            notification.put()

        
        if len(errors) == 0:
            return True, tree
        else:
            return False, errors
Exemplo n.º 9
0
    def setUpClass(cls):
        cls.sitetree = SiteTree()

        t1 = Tree(alias='tree1')
        t1.save(force_insert=True)

        t1_root = TreeItem(title='root', tree=t1, url='/')
        t1_root.save(force_insert=True)

        t1_root_child1 = TreeItem(title='child1',
                                  tree=t1,
                                  parent=t1_root,
                                  url='/about/')
        t1_root_child1.save(force_insert=True)

        t1_root_child2 = TreeItem(title='child2',
                                  tree=t1,
                                  parent=t1_root,
                                  url='articles_list',
                                  urlaspattern=True)
        t1_root_child2.save(force_insert=True)

        t1_root_child2_sub1 = TreeItem(title='subchild1',
                                       tree=t1,
                                       parent=t1_root_child2,
                                       url='articles_detailed art_id',
                                       urlaspattern=True)
        t1_root_child2_sub1.save(force_insert=True)

        t1_root_child2_sub2 = TreeItem(title='subchild2',
                                       tree=t1,
                                       parent=t1_root_child2,
                                       url='/not_articles/10/')
        t1_root_child2_sub2.save(force_insert=True)

        t2 = Tree(alias='tree2')
        t2.save(force_insert=True)

        t2_root1 = TreeItem(title='{{ t2_root1_title }}', tree=t2, url='/')
        t2_root1.save(force_insert=True)

        t2_root2 = TreeItem(title='put {{ t2_root2_title }} inside',
                            tree=t2,
                            url='/sub/')
        t2_root2.save(force_insert=True)

        t2_root3 = TreeItem(title='for logged in only',
                            tree=t2,
                            url='/some/',
                            access_loggedin=True)
        t2_root3.save(force_insert=True)

        cls.t1 = t1
        cls.t1_root = t1_root
        cls.t1_root_child1 = t1_root_child1
        cls.t1_root_child2 = t1_root_child2
        cls.t1_root_child2_sub1 = t1_root_child2_sub1
        cls.t1_root_child2_sub2 = t1_root_child2_sub2

        cls.t2 = t2
        cls.t2_root1 = t2_root1

        cls.t2_root2 = t2_root2
        cls.t2_root3 = t2_root3
Exemplo n.º 10
0
 def test_unique_aliases(self):
     tree1 = Tree(alias='mytree')
     tree1.save(force_insert=True)
     tree2 = Tree(alias='mytree')
     self.assertRaises(Exception, tree2.save)
Exemplo n.º 11
0
 def test_add_tree_duplicate(self):
     Tree(name='oak', max_height=40).save()
     with self.assertRaises(TreeError):
         database.add_tree('oak', 50)
Exemplo n.º 12
0
def index():
    if request.form.has_key("changeCat") or request.form.has_key("changeProb"):
        myObj = None
        for node in roots:
            if (node.nodeType() == "Category"
                    and node.payload.name == request.form['selectedCat']
                ) or node.nodeType() == "Problem":
                myObj = node
                break
        if myObj != None:
            del roots[:]
            del problems[:]
            del models[:]
            roots.append(myObj)
            for thing in myObj.returnRootChildren():
                items.append(thing)
    elif request.form.has_key("changeItem"):
        myObj = None
        for node in items:
            if (node.nodeType() == "Category"
                    and node.payload.name == request.form['selectedItem']):
                myObj = node
                break
        if myObj != None:
            del items[:]
            items.append(myObj)
            for thing in myObj.returnRootChildren():
                models.append(thing)
    elif request.form.has_key("changeModelParts"):
        myObj = None
        for node in models:
            if (node.nodeType() == "Category"
                    and node.payload.name == request.form['selectedModel']):
                myObj = node
                break
        if myObj != None:
            del models[:]
            models.append(myObj)
            for thing in myObj.returnRootChildren():
                if thing.nodeType() == "Category":
                    parts.append(thing)
            return render_template("parts.html", models=models, parts=parts)
    elif request.form.has_key("Parts"):
        return render_template("parts.html", models=models, parts=parts)
    elif request.form.has_key("Part"):
        myObj = None
        for node in parts:
            if node.nodeType(
            ) == "Category" and node.payload.name == request.form[
                    'selectedPart']:
                myObj = node
                break
        if myObj != None:
            del roots[:]
            del items[:]
            del models[:]
            del parts[:]
            part = myObj
        return render_template("partSelectedDiv.html", part=part)
    elif request.form.has_key("changeModelProblems"):
        myObj = None
        for node in models:
            if (node.nodeType() == "Category"
                    and node.payload.name == request.form['selectedModel']):
                myObj = node
                break
        if myObj != None:
            del models[:]
            models.append(myObj)
            for thing in myObj.returnRootChildren():
                if thing.nodeType() == "Problem":
                    problems.append(thing)
            return render_template("problems.html",
                                   models=models,
                                   problems=problems)
    elif request.form.has_key("Problems"):
        return render_template("problems.html",
                               models=models,
                               problems=problems)
    elif request.form.has_key("Problem") and request.form['yesProbs']:
        myObj = None
        for node in problems:
            if (node.nodeType() == "Problem" and
                (node.payload.problem == request.form['selectedThing']
                 or node.payload.solution == request.form['selectedThing'])):
                myObj = node
                break
        if myObj != None:
            del roots[:]
            del items[:]
            del parts[:]
            del problems[:]
            problems.append(myObj)
            for thing in myObj.returnRootChildren():
                if thing.payload.problem:
                    problems.append(thing)
                else:
                    answer = thing.payload.solution
            return render_template("problems.html",
                                   models=models,
                                   problems=problems,
                                   answer=answer)
    #Needs to be pushed to github
    #else
    elif len(roots) == 0:
        metaData = Tree.query()
        if len(metaData.fetch(None)) == 0:
            r1 = Node(storeCat("Lawn Equipment"))
            roots.append(r1)
            lm = r1.addSubNode(storeCat("Lawn Mower"))
            we = r1.addSubNode(storeCat("Weed Eater"))
            ed = r1.addSubNode(storeCat("Edger"))

            r2 = Node(storeCat("Mobile Phone"))
            att = r2.addSubNode(storeCat("AT&T"))
            verizon = r2.addSubNode(storeCat("Verizon"))
            sprint = r2.addSubNode(storeCat("Sprint"))
            nexs = sprint.addSubNode(storeCat("Nexus"))
            iphones = sprint.addSubNode(storeCat("iPhone 7"))
            galaxys = sprint.addSubNode(storeCat("Galaxy 7S"))
            nexa = att.addSubNode(storeCat("Nexus"))
            iphonea = att.addSubNode(storeCat("iPhone 7"))
            galaxya = att.addSubNode(storeCat("Galaxy 7S"))
            nexv = verizon.addSubNode(storeCat("Nexus"))
            iphonev = verizon.addSubNode(storeCat("iPhone 7"))
            galaxyv = verizon.addSubNode(storeCat("Galaxy 7S"))

            nexsprobone = nexs.addSubNode(storeProb("Broken Screen?", None))
            nexsprobtwo = nexs.addSubNode(
                storeProb("Broken home button?", None))
            nexsprobthree = nexs.addSubNode(
                storeProb("Phone doesn't turn on?", None))
            nexsprobone.addSubNode(storeProb(None, "You need a new screen!"))
            nexsprobtwo.addSubNode(
                storeProb(None, "You need a new home button!"))
            nexsprobthree.addSubNode(storeProb(None,
                                               "You need a new battery!"))
            nexaprobone = nexa.addSubNode(storeProb("Broken Screen?", None))
            nexaprobtwo = nexa.addSubNode(
                storeProb("Broken home button?", None))
            nexaprobthree = nexa.addSubNode(
                storeProb("Phone doesn't turn on?", None))
            nexaprobone.addSubNode(storeProb(None, "You need a new screen!"))
            nexaprobtwo.addSubNode(
                storeProb(None, "You need a new home button!"))
            nexaprobthree.addSubNode(storeProb(None,
                                               "You need a new battery!"))
            nexvprobone = nexv.addSubNode(storeProb("Broken Screen?", None))
            nexvprobtwo = nexv.addSubNode(
                storeProb("Broken home button?", None))
            nexvprobthree = nexv.addSubNode(
                storeProb("Phone doesn't turn on?", None))
            nexvprobone.addSubNode(storeProb(None, "You need a new screen!"))
            nexvprobtwo.addSubNode(
                storeProb(None, "You need a new home button!"))
            nexvprobthree.addSubNode(storeProb(None,
                                               "You need a new battery!"))

            iphonesprobone = iphones.addSubNode(
                storeProb("Broken Screen?", None))
            iphonesprobtwo = iphones.addSubNode(
                storeProb("Broken home button?", None))
            iphonesprobthree = iphones.addSubNode(
                storeProb("Phone doesn't turn on?", None))
            iphonesprobone.addSubNode(storeProb(None,
                                                "You need a new screen!"))
            iphonesprobtwo.addSubNode(
                storeProb(None, "You need a new home button!"))
            iphonesprobthree.addSubNode(
                storeProb(None, "You need a new battery!"))
            iphoneaprobone = iphonea.addSubNode(
                storeProb("Broken Screen?", None))
            iphoneaprobtwo = iphonea.addSubNode(
                storeProb("Broken home button?", None))
            iphoneaprobthree = iphonea.addSubNode(
                storeProb("Phone doesn't turn on?", None))
            iphoneaprobone.addSubNode(storeProb(None,
                                                "You need a new screen!"))
            iphoneaprobtwo.addSubNode(
                storeProb(None, "You need a new home button!"))
            iphoneaprobthree.addSubNode(
                storeProb(None, "You need a new battery!"))
            iphonevprobone = iphonev.addSubNode(
                storeProb("Broken Screen?", None))
            iphonevprobtwo = iphonev.addSubNode(
                storeProb("Broken home button?", None))
            iphonevprobthree = iphonev.addSubNode(
                storeProb("Phone doesn't turn on?", None))
            iphonevprobone.addSubNode(storeProb(None,
                                                "You need a new screen!"))
            iphonevprobtwo.addSubNode(
                storeProb(None, "You need a new home button!"))
            iphonevprobthree.addSubNode(
                storeProb(None, "You need a new battery!"))

            galaxysprobone = galaxys.addSubNode(
                storeProb("Broken Screen?", None))
            galaxysprobtwo = galaxys.addSubNode(
                storeProb("Broken home button?", None))
            galaxysprobthree = galaxys.addSubNode(
                storeProb("Phone doesn't turn on?", None))
            galaxysprobone.addSubNode(storeProb(None,
                                                "You need a new screen!"))
            galaxysprobtwo.addSubNode(
                storeProb(None, "You need a new home button!"))
            galaxysprobthree.addSubNode(
                storeProb(None, "You need a new battery!"))
            galaxyaprobone = galaxya.addSubNode(
                storeProb("Broken Screen?", None))
            galaxyaprobtwo = galaxya.addSubNode(
                storeProb("Broken home button?", None))
            galaxyaprobthree = galaxya.addSubNode(
                storeProb("Phone doesn't turn on?", None))
            galaxyaprobone.addSubNode(storeProb(None,
                                                "You need a new screen!"))
            galaxyaprobtwo.addSubNode(
                storeProb(None, "You need a new home button!"))
            galaxyaprobthree.addSubNode(
                storeProb(None, "You need a new battery!"))
            galaxyvprobone = galaxyv.addSubNode(
                storeProb("Broken Screen?", None))
            galaxyvprobtwo = galaxyv.addSubNode(
                storeProb("Broken home button?", None))
            galaxyvprobthree = galaxyv.addSubNode(
                storeProb("Phone doesn't turn on?", None))
            galaxyvprobone.addSubNode(storeProb(None,
                                                "You need a new screen!"))
            galaxyvprobtwo.addSubNode(
                storeProb(None, "You need a new home button!"))
            galaxyvprobthree.addSubNode(
                storeProb(None, "You need a new battery!"))

            nexs.addSubNode(storeCat("Screen"))
            nexs.addSubNode(storeCat("Home Button"))
            nexs.addSubNode(storeCat("Battery"))
            nexa.addSubNode(storeCat("Screen"))
            nexa.addSubNode(storeCat("Home Button"))
            nexa.addSubNode(storeCat("Battery"))
            nexv.addSubNode(storeCat("Screen"))
            nexv.addSubNode(storeCat("Home Button"))
            nexv.addSubNode(storeCat("Battery"))

            iphones.addSubNode(storeCat("Screen"))
            iphones.addSubNode(storeCat("Home Button"))
            iphones.addSubNode(storeCat("Battery"))
            iphonea.addSubNode(storeCat("Screen"))
            iphonea.addSubNode(storeCat("Home Button"))
            iphonea.addSubNode(storeCat("Battery"))
            iphonev.addSubNode(storeCat("Screen"))
            iphonev.addSubNode(storeCat("Home Button"))
            iphonev.addSubNode(storeCat("Battery"))

            galaxys.addSubNode(storeCat("Screen"))
            galaxys.addSubNode(storeCat("Home Button"))
            galaxys.addSubNode(storeCat("Battery"))
            galaxya.addSubNode(storeCat("Screen"))
            galaxya.addSubNode(storeCat("Home Button"))
            galaxya.addSubNode(storeCat("Battery"))
            galaxyv.addSubNode(storeCat("Screen"))
            galaxyv.addSubNode(storeCat("Home Button"))
            galaxyv.addSubNode(storeCat("Battery"))

            roots.append(r2)

            torro = we.addSubNode(storeCat("Torro"))
            torro.addSubNode(storeCat("Gas Tank"))
            torro.addSubNode(storeCat("Pull String"))
            torro.addSubNode(storeCat("Spark Plugs"))
            craftsmen = we.addSubNode(storeCat("Craftsmen"))
            craftsmen.addSubNode(storeCat("Gas Tank"))
            craftsmen.addSubNode(storeCat("Nylon String"))
            honda = lm.addSubNode(storeCat("Honda"))
            honda.addSubNode(storeCat("Gas Tank"))
            honda.addSubNode(storeCat("Blades"))
            honda.addSubNode(storeCat("Spark Plugs"))
            bd = lm.addSubNode(storeCat("B&D"))
            bd.addSubNode(storeCat("Wheels"))
            bd.addSubNode(storeCat("Bearings"))
            torro2 = ed.addSubNode(storeCat("Torro"))
            torro2.addSubNode(storeCat("Handles"))
            torro2.addSubNode(storeCat("Screws"))

            torroprobone = torro.addSubNode(storeProb("Do you have gas?",
                                                      None))
            torroprobtwo = torro.addSubNode(
                storeProb("Is your pull string tangled?", None))
            torroprobone.addSubNode(storeProb(None, "You have no gas!"))
            torroprobtwo.addSubNode(
                storeProb(None, "You need to untangle your pull string!"))
            craftsmenprobone = craftsmen.addSubNode(
                storeProb("Do you have gas?", None))
            craftsmenprobtwo = craftsmen.addSubNode(
                storeProb("Is your pull string tangled?", None))
            craftsmenprobone.addSubNode(storeProb(None, "You have no gas!"))
            craftsmenprobtwo.addSubNode(
                storeProb(None, "You need to untangle your pull string!"))
            hondaprobone = honda.addSubNode(storeProb("Do you have gas?",
                                                      None))
            hondaprobtwo = honda.addSubNode(
                storeProb("Are the blades dull?", None))
            hondaprobone.addSubNode(storeProb(None, "You have no gas!"))
            hondaprobtwo.addSubNode(storeProb(None, "Your blades are dull!"))
            bdprobone = bd.addSubNode(
                storeProb("Are your wheels wobbling?", None))
            bdprobtwo = bd.addSubNode(
                storeProb("Are you hearing a squeaking noise as you mow?",
                          None))
            bdprobone.addSubNode(
                storeProb(None, "You need to tighten the wheels!"))
            bdprobtwo.addSubNode(
                storeProb(
                    None,
                    "You need to put some damn WD-40 on those bearings!!!"))
            torro2probone = torro2.addSubNode(
                storeProb("Are you handles wobbling off as you cut?", None))
            torro2probtwo = torro2.addSubNode(
                storeProb("Are your handles rusty?", None))
            torro2probone.addSubNode(
                storeProb(None, "You need to tighten the screws!"))
            torro2probtwo.addSubNode(
                storeProb(None, "You definitely need some new handles!"))

            r1.printTree()

            #treeDict = r1.convertTree()
            Tree(str(r1.convertTree())).put()
            Tree(str(r2.convertTree())).put()
            #r1Prime = Node(treeDict)
            #r1Prime.printTree()
        else:
            for probsol in Problem.query():
                config.probList.append(probsol)
            for cat in Category.query():
                config.catList.append(cat)
            trees = Tree.query()  # get item list
            for tree in trees:  # find correct item
                roots.append(Node(ast.literal_eval(tree.tree)))

    return render_template("testindex.html", roots=roots, items=items)
Exemplo n.º 13
0
    def save(self, request):
        from django.contrib.gis.geos import Point

        plot = Plot()
        plot.data_owner = request.user

        address = self.cleaned_data.get('edit_address_street')
        if address:
            plot.address_street = address
            plot.geocoded_address = address
        city = self.cleaned_data.get('edit_address_city')
        geo_address = self.cleaned_data.get('geocode_address')
        if geo_address:
            plot.geocoded_address = geo_address
        if city:
            plot.address_city = city
        zip_ = self.cleaned_data.get('edit_address_zip')
        if zip_:
            plot.address_zip = zip_

        plot_width = self.cleaned_data.get('plot_width')
        plot_width_in = self.cleaned_data.get('plot_width_in')
        if plot_width:
            plot.width = float(plot_width)
        if plot_width_in:
            plot.width = plot.width + (float(plot_width_in) / 12)
        plot_length = self.cleaned_data.get('plot_length')
        plot_length_in = self.cleaned_data.get('plot_length_in')
        if plot_length:
            plot.length = float(plot_length)
        if plot_length_in:
            plot.length = plot.length + (float(plot_length_in) / 12)
        plot_type = self.cleaned_data.get('plot_type')
        if plot_type:
            plot.type = plot_type
        power_lines = self.cleaned_data.get('power_lines')
        if power_lines != "":
            plot.powerline_conflict_potential = power_lines
        sidewalk_damage = self.cleaned_data.get('sidewalk_damage')
        if sidewalk_damage:
            plot.sidewalk_damage = sidewalk_damage
        owner_additional_id = self.cleaned_data.get('owner_additional_id')
        if owner_additional_id:
            plot.owner_additional_id = owner_additional_id

        import_event, created = ImportEvent.objects.get_or_create(
            file_name='site_add', )
        plot.import_event = import_event

        pnt = Point(self.cleaned_data.get('lon'),
                    self.cleaned_data.get('lat'),
                    srid=4326)
        plot.geometry = pnt
        plot.last_updated_by = request.user
        plot.save()

        species = self.cleaned_data.get('species_id')
        species_other1 = self.cleaned_data.get('species_other1')
        species_other2 = self.cleaned_data.get('species_other2')
        height = self.cleaned_data.get('height')
        canopy_height = self.cleaned_data.get('canopy_height')
        dbh = self.cleaned_data.get('dbh')
        dbh_type = self.cleaned_data.get('dbh_type')
        condition = self.cleaned_data.get('condition')
        canopy_condition = self.cleaned_data.get('canopy_condition')

        #TODO: fix this
        pests = self.cleaned_data.get('pests')
        if species or height or canopy_height or dbh or \
           condition or canopy_condition or pests:
            # print species, height, canopy_height, dbh, condition, canopy_condition
            if species:
                spp = Species.objects.filter(id=species)
                if spp:
                    new_tree = Tree(species=spp[0])
                else:
                    new_tree = Tree()
            else:
                new_tree = Tree()

            new_tree.pests = pests

            if species_other1:
                new_tree.species_other1 = species_other1
            if species_other2:
                new_tree.species_other2 = species_other2
            if height:
                new_tree.height = height
            if canopy_height:
                new_tree.canopy_height = canopy_height
            if dbh:
                if dbh_type == 'circumference':
                    new_tree.dbh = dbh / math.pi
                else:
                    new_tree.dbh = dbh
            if condition:
                new_tree.condition = condition
            if canopy_condition:
                new_tree.canopy_condition = canopy_condition

            new_tree.import_event = import_event
            new_tree.last_updated_by = request.user
            new_tree.plot = plot
            new_tree.save()
            #print new_tree.__dict__

        return plot
 def test_add_tree_duplicate(self, mock_get_float, mock_get_string):
     Tree(name='oak', max_height=40).save()
     controller.add_tree()
     tree_count = Tree.select().count()
     self.assertEqual(1, tree_count)  # only one Tree
Exemplo n.º 15
0
def setupTree():
    if len(roots) == 0:
        metaData = Tree.query()
        if len(metaData.fetch(None)) == 0:
            root1 = Node(storeCat("Lawn Equipment"))
            roots.append(root1)
            lawnmower = root1.addSubNode(storeCat("Lawn Mower"))
            weedeater = root1.addSubNode(storeCat("Weed Eater"))
            edger = root1.addSubNode(storeCat("Edger"))

            wontStart = lawnmower.addSubNode(
                storeProb("Lawn mower won't start", None))
            ws1 = wontStart.addSubNode(
                storeProb("Is there gas in the tank?",
                          "Put gas in the mower!"))
            ws2 = ws1.addSubNode(
                storeProb("Will the mower jump start?",
                          "Replace the battery!"))
            ws3 = ws2.addSubNode(
                storeProb("Do the spark plugs appear clean?",
                          "Clear or replace the plugs!",
                          url="https://goo.gl/FJneX0"))
            wontCut = lawnmower.addSubNode(
                storeProb("Mower isn't cutting the grass well", None))
            wc1 = wontCut.addSubNode(
                storeProb("Is the grass a reasonable height?",
                          "Raise the cutting height on your lawn mower"))
            wc2 = wc1.addSubNode(
                storeProb("Does the blade appear to be sharp?",
                          "Replace or sharpen the blade",
                          "https://goo.gl/ANuuah"))

            toro = lawnmower.addSubNode(storeCat("Toro", type='part'))
            honda = lawnmower.addSubNode(storeCat("Honda", type='part'))
            craftsman = lawnmower.addSubNode(storeCat("Craftsman",
                                                      type='part'))
            honda.addSubNode(
                storeCat("Spark Plug",
                         type='part',
                         url="https://goo.gl/4988HT"))
            honda.addSubNode(
                storeCat("Blade", type='part', url="https://goo.gl/6IzHDH"))
            honda.addSubNode(
                storeCat("Battery", type='part', url="https://goo.gl/j32Qs0"))
            toro.addSubNode(
                storeCat("Spark Plug",
                         type='part',
                         url="https://goo.gl/iSq3IM"))
            toro.addSubNode(
                storeCat("Blade", type='part', url="https://goo.gl/awrF4W"))
            toro.addSubNode(
                storeCat("Battery", type='part', url="https://goo.gl/hxFbMM"))
            craftsman.addSubNode(
                storeCat("Spark Plug",
                         type='part',
                         url="https://goo.gl/uMbjzb"))
            craftsman.addSubNode(
                storeCat("Blade", type='part', url="https://goo.gl/6mcSAe"))
            craftsman.addSubNode(
                storeCat("Battery", type='part', url="https://goo.gl/Txykkp"))

            root2 = Node(storeCat("Smartphone"))
            roots.append(root2)
            android = root2.addSubNode(storeCat("Google Android"))
            apple = root2.addSubNode(storeCat("Apple iOS"))

            slow = android.addSubNode(storeProb("My phone is slow", None))
            sl1 = slow.addSubNode(
                storeProb("Has the phone been restarted recently?",
                          "Reboot the phone"))
            sl2 = sl1.addSubNode(
                storeProb("Are there only a few apps open?",
                          "Close the apps you aren't actively using"))
            sl3 = sl2.addSubNode(
                storeProb("Is atleast 90% of the phone storage free?",
                          "Removed unused apps or upgrade the storage"))
            app = android.addSubNode(
                storeProb("I want to develop an app", "Follow a tutorial",
                          None))
            app.addSubNode(
                storeProb(
                    "Have you tried following a tutorial?",
                    "Use an online tutorial",
                    url=
                    "https://developer.android.com/training/basics/firstapp/index.html"
                ))

            slow = apple.addSubNode(storeProb("My phone is slow", None))
            sl1 = slow.addSubNode(
                storeProb("Has the phone been restarted recently?",
                          "Reboot the phone"))
            sl2 = sl1.addSubNode(
                storeProb("Are there only a few apps open?",
                          "Close the apps you aren't actively using"))
            sl3 = sl2.addSubNode(
                storeProb("Is atleast 90% of the phone storage free?",
                          "Removed unused apps or upgrade the storage"))
            app = apple.addSubNode(
                storeProb("I want to develop an app", "Follow a tutorial",
                          None))
            app.addSubNode(
                storeProb(
                    "Have you tried following a tutorial?",
                    "Use an online tutorial",
                    url=
                    "https://developer.apple.com/library/content/referencelibrary/GettingStarted/DevelopiOSAppsSwift/"
                ))

            moto = android.addSubNode(storeCat("Motorola", type='part'))
            samsung = android.addSubNode(storeCat("Samsung", type='part'))
            huawei = android.addSubNode(storeCat("Huawei", type='part'))
            moto.addSubNode(
                storeCat("Case", type='part', url="https://goo.gl/aSNG2U"))
            samsung.addSubNode(
                storeCat("Case", type='part', url="https://goo.gl/T10QPU"))
            huawei.addSubNode(
                storeCat("Case", type='part', url="https://goo.gl/frSgRq"))
            apple.addSubNode(
                storeCat("Case", type='part', url="https://goo.gl/6vclg7"))

            root1.printTree()
            root2.printTree()

            testTree = str(root1.convertTree())
            print testTree

            # treeDict = root1.convertTree()
            Tree(tree=testTree).put()
            Tree(tree=str(root2.convertTree())).put()
            # r1Prime = Node(treeDict)
            # print r1Prime

            print root1
            print root2

        else:
            for probsol in Problem.query():
                probList.append(probsol)
            for cat in Category.query():
                catList.append(cat)
            trees = Tree.query()  # get item list
            for tree in trees:  # find correct item
                roots.append(Node(ast.literal_eval(tree.tree)))
            for root in roots:
                root.printTree()
            print "Length of probList is " + str(len(probList))
            print "Length of catList is " + str(len(catList))
Exemplo n.º 16
0
def index(request):
    if request.method != 'POST':
        return render(request, 'index.html')
    else:
        if request.is_ajax():
            if request.POST.get('userName'):
                user_name = request.POST.get('userName')
                user = User.objects.get(user_name=user_name)
            else:
                user = None

            if request.POST.get("mode") == "grow":

                parent = request.POST.get('parent')
                child = request.POST.get('child')

                # case 1: parent already trunk -- create branch from child
                try:
                    trunk = Trunk.objects.get(text=parent)
                    drawing = trunk.drawing
                    if trunk:
                        try:
                            branch = Branch.objects.get(text=child)
                            data = {
                                "already": True,
                                "parent": parent,
                                "child": child
                            }
                            return JsonResponse(data)
                        except Branch.DoesNotExist:
                            branch = Branch(text=child, trunk=trunk)
                            branch.save()
                            branch.get_tips()
                            data = drawing.get_data()
                            if trunk.branch_set.count() > 1:
                                data["tree?"] = trunk.text
                                branch.stage = "filled"
                                branch.save()
                            return JsonResponse(data)
                        # display message saying that this combo already exists.

                except Trunk.DoesNotExist:
                    # case 2: parent already branch -- create twig from child
                    print("trying case 2")
                    try:
                        branch = Branch.objects.get(text=parent)
                        drawing = branch.trunk.drawing
                        if branch:
                            try:
                                twig = Twig.objects.get(text=child)
                                data = {
                                    "already": True,
                                    "parent": parent,
                                    "child": child
                                }
                                return JsonResponse(data)
                            except Twig.DoesNotExist:
                                twig = Twig(text=child, branch=branch)
                                twig.save()
                                twig.get_tips()
                                data = drawing.get_data()
                                if branch.trunk.branch_set.count() > 1:
                                    data["tree?"] = branch.trunk.text
                                    twig.stage = "filled"
                                    twig.save()
                                return JsonResponse(data)

                    except Branch.DoesNotExist:
                        # case 3: parent already twig -- create leaf from child
                        print("trying case 3")
                        try:
                            twig = Twig.objects.get(text=parent)
                            drawing = twig.branch.trunk.drawing
                            if twig:
                                try:
                                    leaf = Leaf.objects.get(text=child)
                                    leaf.twigs.add(twig)
                                    leaf.save()
                                    leaf.get_tips(parent)
                                    data = drawing.get_data()
                                    return JsonResponse(data)
                                except Leaf.DoesNotExist:
                                    leaf = Leaf(text=child)
                                    leaf.save()
                                    leaf.twigs.add(twig)
                                    leaf.save()
                                    leaf.get_tips(parent)
                                    data = drawing.get_data()
                                    if twig.branch.trunk.branch_set.count(
                                    ) > 1:
                                        data["tree?"] = twig.branch.trunk.text
                                        leaf.stage = "filled"
                                        leaf.save()
                                    return JsonResponse(data)

                        except Twig.DoesNotExist:
                            # case 4: neither child nor parent saved to tree --
                            # create branch from child and trunk from parent
                            print("trying case 4")
                            trunk = Trunk.objects.create(text=parent)
                            branch = Branch.objects.create(text=child)
                            branch.get_tips()
                            branch.trunk = trunk
                            branch.save()
                            drawing = Drawing.objects.create(text=parent,
                                                             trunk=trunk)
                            data = drawing.get_data()
                            trunk.stage = "filled"
                            trunk.save()
                            branch.stage = "filled"
                            branch.save()
                            return JsonResponse(data)

            elif request.POST.get("mode") == "prompt":
                trunk_name = request.POST.get("trunkName")
                trunk = Trunk.objects.get(text=trunk_name)
                try:
                    tree = Tree.objects.get(name=trunk_name)
                    tree.rank_twigs()
                    tree.save()
                    sorted_twigs = Twig.objects.order_by('-rank').filter(
                        tree=tree)
                    current_twig = sorted_twigs[tree.twigs_learned]
                    print("current twig is " + current_twig.text)
                    data = {"twig": current_twig.text}
                    return JsonResponse(data)
                except Tree.DoesNotExist:
                    tree = Tree(name=trunk_name, trunk=trunk)
                    tree.rank_twigs()
                    tree.save()
                    sorted_twigs = Twig.objects.order_by('-rank').filter(
                        tree=tree)
                    current_twig = sorted_twigs[tree.twigs_learned]
                    data = {"twig": current_twig.text}
                    return JsonResponse(data)

            elif request.POST.get("mode") == "remind":
                trunk_name = request.POST.get("trunkName")
                tree = Tree.objects.get(name=trunk_name)
                sorted_twigs = Twig.objects.order_by('-rank').filter(tree=tree)
                current_twig = sorted_twigs[tree.twigs_learned]
                tries = request.POST.get("tries").strip(',')
                for leaf in current_twig.leaf_set.all():
                    if leaf.text in tries:
                        leaf.learned = True
                        leaf.save()
                total_count = current_twig.leaf_set.count()
                learned_count = current_twig.leaf_set.all().filter(
                    learned=True).count()
                if total_count == learned_count:
                    tree.twigs_learned += 1
                    tree.save()
                    current_twig = sorted_twigs[tree.twigs_learned]
                    data = {"twig": current_twig.text}
                    return JsonResponse(data)
                else:
                    missed_leaf = current_twig.leaf_set.all().filter(
                        learned=False)[0]
                    print("missed leaf is: " + missed_leaf.text)
                    next_twigs_list = [
                        i.text for i in missed_leaf.twigs.all()
                        [1:missed_leaf.reminders + 1]
                    ]
                    print(next_twigs_list)

                    next_twigs = map(lambda x: " AND " + x, next_twigs_list)
                    data = {
                        "miss": current_twig.text,
                        "next_twigs": next_twigs
                    }
                    missed_leaf.reminders += 1
                    missed_leaf.save()
                    return JsonResponse(data)

            elif request.POST.get("mode") == "check":
                to_check = request.POST.get("toCheck")
                try:
                    matching_leaf = current_twig.leaf_set.get(text=to_check)
                    next_leaf = current_twig.misses[indexofmissedleaf + 1]
                    other_twigs = request.POST.get(nextTwigs)
                    next_leaf_next_twigs = [
                        i.text
                        for i in next_leaf.twigs[0:missed_leaf.reminders]
                    ]
                    if other_twigs in [twig.text for i in matching_leaf.twigs]:
                        # user successfully remembered missed leaf she was reminded of. go on
                        # to next leaf...
                        matching_leaf.learned = True
                        matching_leaf.save()
                        current_twig.misses.remove(matching_leaf)
                        data = {
                            "miss": next_leaf.text,
                            "next_twigs": next_leaf_next_twigs
                        }
                        return JsonResponse(data)
                except matching_leaf.DoesNotExist:
                    # user failed to remember missed leaf she was reminded of.
                    # give another reminder, unless she has exhausted them all...
                    missed_leaf = current_twig.misses[0]
                    next_twigs = [
                        i.text
                        for i in missed_leaf.twigs[0:missed_leaf.reminders]
                    ]
                    data = {"miss": current_twig, "next_twigs": [i.text]}
                    return JsonResponse(data)

        else:
            print("request wasn't ajax")
            return render(request, 'index.html')
Exemplo n.º 17
0
def grow_ant_nodewise():
    """The main function for optimising an ANT """

    # ############## 0: Define the root node and optimise ###################
    # define the root node:
    tree_struct = []  # stores graph information for each node
    tree_modules = []  # stores modules for each node
    root_meta, root_module = define_node(
        args,
        node_index=0,
        level=0,
        parent_index=-1,
        tree_struct=tree_struct,
    )
    tree_struct.append(root_meta)
    tree_modules.append(root_module)

    # train classifier on root node (no split no extension):
    model = Tree(
        tree_struct,
        tree_modules,
        split=False,
        extend=False,
        cuda_on=args.cuda,
    )
    if args.cuda:
        model.cuda()

    # optimise
    model, tree_modules = optimize_fixed_tree(
        model,
        tree_struct,
        train_loader,
        valid_loader,
        test_loader,
        args.epochs_node,
        node_idx=0,
    )
    checkpoint_model('model.pth', struct=tree_struct, modules=tree_modules)
    checkpoint_msc(tree_struct, records)

    # ######################## 1: Growth phase starts ########################
    nextind = 1
    last_node = 0
    for lyr in range(args.maxdepth):
        print(
            "---------------------------------------------------------------")
        print("\nAt layer " + str(lyr))
        for node_idx in range(len(tree_struct)):
            change = False
            if tree_struct[node_idx]['is_leaf'] and not (
                    tree_struct[node_idx]['visited']):

                print("\nProcessing node " + str(node_idx))

                # -------------- Define children candidate nodes --------------
                # ---------------------- (1) Split ----------------------------
                # left child
                identity = True
                meta_l, node_l = define_node(
                    args,
                    node_index=nextind,
                    level=lyr + 1,
                    parent_index=node_idx,
                    tree_struct=tree_struct,
                    identity=identity,
                )
                # right child
                meta_r, node_r = define_node(
                    args,
                    node_index=nextind + 1,
                    level=lyr + 1,
                    parent_index=node_idx,
                    tree_struct=tree_struct,
                    identity=identity,
                )
                # inheriting solver modules to facilitate optimization:
                if args.solver_inherit and meta_l['identity'] and meta_r[
                        'identity'] and not (node_idx == 0):
                    node_l['classifier'] = tree_modules[node_idx]['classifier']
                    node_r['classifier'] = tree_modules[node_idx]['classifier']

                # define a tree with a new split by adding two children nodes:
                model_split = Tree(tree_struct,
                                   tree_modules,
                                   split=True,
                                   node_split=node_idx,
                                   child_left=node_l,
                                   child_right=node_r,
                                   extend=False,
                                   cuda_on=args.cuda)

                # -------------------- (2) Extend ----------------------------
                # define a tree with node extension
                meta_e, node_e = define_node(
                    args,
                    node_index=nextind,
                    level=lyr + 1,
                    parent_index=node_idx,
                    tree_struct=tree_struct,
                    identity=False,
                )
                # Set the router at the current node as one-sided One().
                # TODO: this is not ideal as it changes tree_modules
                tree_modules[node_idx]['router'] = One()

                # define a tree with an extended edge by adding a node
                model_ext = Tree(tree_struct,
                                 tree_modules,
                                 split=False,
                                 extend=True,
                                 node_extend=node_idx,
                                 child_extension=node_e,
                                 cuda_on=args.cuda)

                # ---------------------- Optimise -----------------------------
                best_tr_loss = records['train_best_loss']
                best_va_loss = records['valid_best_loss']
                best_te_loss = records['test_best_loss']

                print("\n---------- Optimizing a binary split ------------")
                if args.cuda:
                    model_split.cuda()

                # split and optimise
                model_split, tree_modules_split, node_l, node_r \
                    = optimize_fixed_tree(model_split, tree_struct,
                                          train_loader, valid_loader, test_loader,
                                          args.epochs_node,
                                          node_idx)

                best_tr_loss_after_split = records['train_best_loss']
                best_va_loss_adter_split = records[
                    'valid_best_loss_nodes_split'][node_idx]
                best_te_loss_after_split = records['test_best_loss']
                tree_struct[node_idx]['train_accuracy_gain_split'] \
                    = best_tr_loss - best_tr_loss_after_split
                tree_struct[node_idx]['valid_accuracy_gain_split'] \
                    = best_va_loss - best_va_loss_adter_split
                tree_struct[node_idx]['test_accuracy_gain_split'] \
                    = best_te_loss - best_te_loss_after_split

                print("\n----------- Optimizing an extension --------------")
                if not (meta_e['identity']):
                    if args.cuda:
                        model_ext.cuda()

                    # make deeper and optimise
                    model_ext, tree_modules_ext, node_e \
                        = optimize_fixed_tree(model_ext, tree_struct,
                                              train_loader, valid_loader, test_loader,
                                              args.epochs_node,
                                              node_idx)

                    best_tr_loss_after_ext = records['train_best_loss']
                    best_va_loss_adter_ext = records[
                        'valid_best_loss_nodes_ext'][node_idx]
                    best_te_loss_after_ext = records['test_best_loss']

                    # TODO: record the gain from split/extra depth:
                    #  need separately record best losses for split & depth
                    tree_struct[node_idx]['train_accuracy_gain_ext'] \
                        = best_tr_loss - best_tr_loss_after_ext
                    tree_struct[node_idx]['valid_accuracy_gain_ext'] \
                        = best_va_loss - best_va_loss_adter_ext
                    tree_struct[node_idx]['test_accuracy_gain_ext'] \
                        = best_te_loss - best_te_loss_after_ext
                else:
                    print('No extension as '
                          'the transformer is an identity function.')

                # ---------- Decide whether to split, extend or keep -----------
                criteria = get_decision(args.criteria, node_idx, tree_struct)

                if criteria == 'split':
                    print("\nSplitting node " + str(node_idx))
                    # update the parent node
                    tree_struct[node_idx]['is_leaf'] = False
                    tree_struct[node_idx]['left_child'] = nextind
                    tree_struct[node_idx]['right_child'] = nextind + 1
                    tree_struct[node_idx]['split'] = True

                    # add the children nodes
                    tree_struct.append(meta_l)
                    tree_modules_split.append(node_l)
                    tree_struct.append(meta_r)
                    tree_modules_split.append(node_r)

                    # update tree_modules:
                    tree_modules = tree_modules_split
                    nextind += 2
                    change = True
                elif criteria == 'extend':
                    print("\nExtending node " + str(node_idx))
                    # update the parent node
                    tree_struct[node_idx]['is_leaf'] = False
                    tree_struct[node_idx]['left_child'] = nextind
                    tree_struct[node_idx]['extended'] = True

                    # add the children nodes
                    tree_struct.append(meta_e)
                    tree_modules_ext.append(node_e)

                    # update tree_modules:
                    tree_modules = tree_modules_ext
                    nextind += 1
                    change = True
                else:
                    # revert weights back to state before split
                    print("No splitting at node " + str(node_idx))
                    print("Revert the weights to the pre-split state.")
                    model = _load_checkpoint('model.pth')
                    tree_modules = model.update_tree_modules()

                # record the visit to the node
                tree_struct[node_idx]['visited'] = True

                # save the model and tree structures:
                checkpoint_model(
                    'model.pth',
                    struct=tree_struct,
                    modules=tree_modules,
                    data_loader=test_loader,
                    figname='hist_split_node_{:03d}.png'.format(node_idx))
                checkpoint_msc(tree_struct, records)
                last_node = node_idx

                # global refinement prior to the next growth
                # NOTE: this is an option not included in the paper.
                if args.finetune_during_growth and (criteria == 1
                                                    or criteria == 2):
                    print("\n-------------- Global refinement --------------")
                    model = Tree(tree_struct,
                                 tree_modules,
                                 split=False,
                                 node_split=last_node,
                                 extend=False,
                                 node_extend=last_node,
                                 cuda_on=args.cuda)
                    if args.cuda:
                        model.cuda()

                    model, tree_modules = optimize_fixed_tree(
                        model,
                        tree_struct,
                        train_loader,
                        valid_loader,
                        test_loader,
                        args.epochs_finetune_node,
                        node_idx,
                    )
        # terminate the tree growth if no split or extend in the final layer
        if not change: break

    # ############### 2: Refinement (finetuning) phase starts #################
    print("\n\n------------------- Fine-tuning the tree --------------------")
    best_valid_accuracy_before = records['valid_best_accuracy']
    model = Tree(tree_struct,
                 tree_modules,
                 split=False,
                 node_split=last_node,
                 child_left=None,
                 child_right=None,
                 extend=False,
                 node_extend=last_node,
                 child_extension=None,
                 cuda_on=args.cuda)
    if args.cuda:
        model.cuda()

    model, tree_modules = optimize_fixed_tree(model, tree_struct, train_loader,
                                              valid_loader, test_loader,
                                              args.epochs_finetune, last_node)

    best_valid_accuracy_after = records['valid_best_accuracy']

    # only save if fine-tuning improves validation accuracy
    if best_valid_accuracy_after - best_valid_accuracy_before > 0:
        checkpoint_model('model.pth',
                         struct=tree_struct,
                         modules=tree_modules,
                         data_loader=test_loader,
                         figname='hist_split_node_finetune.png')
    checkpoint_msc(tree_struct, records)
Exemplo n.º 18
0
def add_tree(name, max_height):
    try:
        Tree(name=name, max_height=max_height).save()
    except IntegrityError as e:
        raise TreeError('Error adding tree because ' + str(e)) 
Exemplo n.º 19
0
    def save(self,request):
        from django.contrib.gis.geos import Point

        plot = Plot()
        plot.data_owner = request.user

        address = self.cleaned_data.get('edit_address_street')
        if address:
            plot.address_street = address
            plot.geocoded_address = address
        city = self.cleaned_data.get('edit_address_city')
        geo_address = self.cleaned_data.get('geocode_address')
        if geo_address:
            plot.geocoded_address = geo_address
        if city:
            plot.address_city = city
        zip_ = self.cleaned_data.get('edit_address_zip')
        if zip_:
            plot.address_zip = zip_
        
        plot_width = self.cleaned_data.get('plot_width')
        plot_width_in = self.cleaned_data.get('plot_width_in')
        if plot_width:
            plot.width = float(plot_width)
        if plot_width_in:
            plot.width = plot.width + (float(plot_width_in) / 12)
        plot_length = self.cleaned_data.get('plot_length')
        plot_length_in = self.cleaned_data.get('plot_length_in')
        if plot_length:
            plot.length = float(plot_length)
        if plot_length_in:
            plot.length = plot.length + (float(plot_length_in) / 12)
        plot_type = self.cleaned_data.get('plot_type')
        if plot_type:
            plot.type = plot_type
        power_lines = self.cleaned_data.get('power_lines')
        if power_lines != "":
            plot.powerline_conflict_potential = power_lines
        sidewalk_damage = self.cleaned_data.get('sidewalk_damage')
        if sidewalk_damage:
            plot.sidewalk_damage = sidewalk_damage

        import_event, created = ImportEvent.objects.get_or_create(file_name='site_add',)
        plot.import_event = import_event

        pnt = Point(self.cleaned_data.get('lon'),self.cleaned_data.get('lat'),srid=4326)
        plot.geometry = pnt
        plot.last_updated_by = request.user
        plot.save()

        species = self.cleaned_data.get('species_id')
        height = self.cleaned_data.get('height')
        canopy_height = self.cleaned_data.get('canopy_height')
        dbh = self.cleaned_data.get('dbh')
        crown_width = self.cleaned_data.get('crown_width')
        dbh_type = self.cleaned_data.get('dbh_type')
        condition = self.cleaned_data.get('condition')
        canopy_condition = self.cleaned_data.get('canopy_condition')

        new_tree = Tree()
        if species:
            spp = Species.objects.filter(symbol=species)
            if spp:
              new_tree.species=spp[0]

        if crown_width:
            new_tree.crown_width = crown_width
        if height:
            new_tree.height = height
        if canopy_height:
            new_tree.canopy_height = canopy_height
        if dbh:
            if dbh_type == 'circumference':
                dbh = dbh / math.pi
            new_tree.dbh = dbh
        if condition:
            new_tree.condition = condition
        if canopy_condition:
            new_tree.canopy_condition = canopy_condition
        
        new_tree.import_event = import_event            
        new_tree.last_updated_by = request.user
        new_tree.plot = plot
        new_tree.save()
        #print new_tree.__dict__
        fauna = self.cleaned_data.get('fauna')
        if fauna:
            print 'fauna',fauna
            fauna_dict = dict(Choices().get_field_choices('fauna'))
            for f in fauna:
              fauna = TreeFauna()
              fauna.reported_by = request.user
              fauna.key = f
              fauna.value = datetime.now()
              fauna.fauna = fauna_dict[f] # or random string
              fauna.tree = new_tree
              fauna.save()
        
        return plot
Exemplo n.º 20
0
    def setUpClass(cls):
        cls.sitetree = SiteTree()

        t1 = Tree(alias='tree3')
        t1.save(force_insert=True)

        t1_root = TreeItem(title='root', tree=t1, url='/', hidden=True)
        t1_root.save(force_insert=True)

        t1_root_child1 = TreeItem(title='child1',
                                  tree=t1,
                                  parent=t1_root,
                                  url='/0/',
                                  access_loggedin=True)
        t1_root_child1.save(force_insert=True)

        t1_root_child2 = TreeItem(title='child2',
                                  tree=t1,
                                  parent=t1_root,
                                  url='/1/',
                                  inmenu=True,
                                  hidden=True)
        t1_root_child2.save(force_insert=True)

        t1_root_child3 = TreeItem(title='child3',
                                  tree=t1,
                                  parent=t1_root,
                                  url='/2/',
                                  inmenu=False)
        t1_root_child3.save(force_insert=True)

        t1_root_child4 = TreeItem(title='child4',
                                  tree=t1,
                                  parent=t1_root,
                                  url='/3/',
                                  hidden=True)
        t1_root_child4.save(force_insert=True)

        t1_root_child5 = TreeItem(title='child5',
                                  tree=t1,
                                  parent=t1_root,
                                  url='/4/',
                                  inmenu=True,
                                  hidden=True)
        t1_root_child5.save(force_insert=True)

        t2 = Tree(alias='tree3_en')
        t2.save(force_insert=True)

        t2_root = TreeItem(title='root_en', tree=t2, url='/')
        t2_root.save(force_insert=True)

        t2_root_child1 = TreeItem(title='child1_en',
                                  tree=t2,
                                  parent=t2_root,
                                  url='/0_en/')
        t2_root_child1.save(force_insert=True)

        t2_root_child2 = TreeItem(title='child2_en',
                                  tree=t2,
                                  parent=t2_root,
                                  url='/1_en/')
        t2_root_child2.save(force_insert=True)

        cls.t1 = t1
        cls.t1_root = t1_root
        cls.t1_root_child1 = t1_root_child1
        cls.t1_root_child2 = t1_root_child2
        cls.t1_root_child2 = t1_root_child3
        cls.t1_root_child2 = t1_root_child4
        cls.t1_root_child2 = t1_root_child5

        cls.t2_root = t2_root