Exemplo n.º 1
0
    def setUpClass(cls):
        cls.sitetree = SiteTree()

        t1 = Tree(alias='tree3')
        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='/0/', inmenu=True, hidden=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=True, hidden=True)
        t1_root_child3.save(force_insert=True)

        t1_root_child4 = TreeItem(title='child4', tree=t1, parent=t1_root, url='/3/', inmenu=True, 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)

        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
Exemplo n.º 2
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.º 3
0
def main():
   
    listings_data = (row for row in open('data/listings.txt', 'rU'))
    products_data = [row for row in open('data/products.txt', 'rU')]
   

    # Build product trees
    # 1. product_tree is the 4-level tree used to split product data for ranking purposes
    # 2. product_search_tree is the Binary Search tree used to quickly aggregate the results and output them 
    print 'Constructing trees'
    product_tree = Tree()
    for index, prod in enumerate(products_data):
        product = Product(prod)
        product_tree.insert(product)
        if index == 0:
            product_search_tree = BinaryNode(product)
        else:
            product_search_tree.insert(product)

    print 'Matching listings'
    for listing_row in listings_data:
        listing = Listing(listing_row)
        match = product_tree.find(listing)
        if match is not None:
            product_search_tree.insert_payload(data=match, payload=listing.original_string)

    print 'traversing product tree and writing output'
    with codecs.open('results.txt','w',encoding='utf-8') as result_file:
        product_search_tree.traverse_with_action(lambda node: result_file.write(node.result_output))
Exemplo n.º 4
0
 def get(self):
     map_template = JINJA_ENVIRONMENT.get_template('templates/map.html')
     my_user = users.get_current_user()
     if my_user == None:
         self.redirect('/login')
     else:
         logout_url = users.create_logout_url('/')
         alltrees = Tree.query().fetch()
         trees_per_user = {}
         for tree in alltrees:
             if tree.email not in trees_per_user:
                 trees_per_user[tree.email] = tree.number #adds email key only once
             elif tree.number > trees_per_user[tree.email]: #swaps key's value if greater than
                 trees_per_user[tree.email] = tree.number
         trees = Tree.query().filter(Tree.user_id == my_user.user_id()).fetch()
         othertrees = Tree.query().filter(Tree.user_id != my_user.user_id()).fetch()
         dict_for_template = {
             'email': my_user.nickname(),
             'trees': trees,
             'othertrees':othertrees,
             'loadmytrees': "false",
             'containertype': "leaderboard-container",
             'headermessage': "Community Leaderboard",
             'logouturl': logout_url,
             'planters': sorted(trees_per_user.items(), key=lambda x: x[1], reverse=True)
             }
         self.response.write(map_template.render(dict_for_template))
Exemplo n.º 5
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.º 6
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.º 7
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.º 8
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.º 9
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.º 10
0
def trees_json():
    """
    List tree objects in JSON format
    """
    trees = []
    for tree in TreeDB.objects:
        trees.append(Tree.from_mongo(tree))
    return trees
Exemplo n.º 11
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.º 12
0
 def get(self):
     map_template = JINJA_ENVIRONMENT.get_template('templates/map.html')
     my_user = users.get_current_user()
     if my_user == None:
         self.redirect('/login')
     else:
         logout_url = users.create_logout_url('/')
         trees = Tree.query().filter(Tree.user_id == my_user.user_id()).fetch()
         othertrees = Tree.query().filter(Tree.user_id != my_user.user_id()).fetch()
         dict_for_template = {
             'email': my_user.nickname(),
             'trees': trees,
             'othertrees':othertrees,
             'loadmytrees': "about",
             'containertype': "about-container",
             'headermessage': "Save the Trees Initiative",
             'logouturl': logout_url,
             }
         self.response.write(map_template.render(dict_for_template))
Exemplo n.º 13
0
def add_tree(
    tree: Tree,
    current_user: User = Depends(get_current_active_user),
    status_code=status.HTTP_201_CREATED,
):
    """
    Add trees to DB
    """
    new_tree = TreeDB(**tree.to_dict())
    new_tree.save()
    return {"detail": "New object added", "id": str(new_tree.id)}
Exemplo n.º 14
0
def get_tree(oid: str):
    """
    Retrieve tree from DB
    """
    try:
        selected_tree = TreeDB.objects.get(id=oid)
        return Tree.from_mongo(selected_tree)
    except TreeDB.DoesNotExist:
        raise HTTPException(
            status_code=status.HTTP_404_NOT_FOUND, detail="Object ID not found"
        )
Exemplo n.º 15
0
 def get(self):
     
     trees = Tree.main_trees()
     branchdatas = Branch.get_first_branchs(trees)
     template_values = {
         'trees': trees,
         'branchdatas': branchdatas,
         'username': self.username()
     }
     
     self.render('main.html', template_values)
Exemplo n.º 16
0
 def setUp(self): 
     self.test_product_1 = Product('{"product_name":"Nikon-s6100","manufacturer":"Nikon","model":"S6100","family":"Coolpix","announced-date":"2011-02-08T19:00:00.000-05:00"}')
     self.test_product_2_no_family = Product('{"product_name":"Casio_QV-5000SX","manufacturer":"Casio","model":"QV-5000SX","announced-date":"1998-04-19T20:00:00.000-04:00"}')
     self.test_product_3 = Product('{"product_name":"Casio_Exilim_EX-H20G","manufacturer":"Casio","model":"EX-H20g","family":"Exilim","announced-date":"2010-09-19T20:00:00.000-04:00"}')
     self.test_listing_1 = Listing('{"title":"Canon PowerShot D10 12.1 MP Waterproof Digital Camera with 3x Optical Image Stabilized Zoom and 2.5-inch LCD (Blue/Silver)","manufacturer":"Canon Canada","currency":"CAD","price":"420.33"}')
     self.test_listing_2 = Listing('{"title":"Olympus PEN E-PL1 12.3MP Live MOS Micro Four Thirds Interchangeable Lens Digital Camera with 14-42mm f/3.5-5.6 Zuiko Digital Zoom Lens (Black)","manufacturer":"Olympus Canada","currency":"CAD","price":"598.97"}')
     self.test_listing_3_unicode = Listing('{"title":"Canon - EOS 400D - Appareil photo numérique reflex boîtier nu - 10,1 Mpix","manufacturer":"Canon","currency":"EUR","price":"279.00"}')
     self.tree = Tree()
     self.tree.insert(self.test_product_1)
     self.tree.insert(self.test_product_2_no_family)
     self.tree.insert(self.test_product_3)
Exemplo n.º 17
0
class TestBinaryProductsAndListings(unittest.TestCase):
    def setUp(self): 
        self.test_product_1 = Product('{"product_name":"Nikon-s6100","manufacturer":"Nikon","model":"S6100","family":"Coolpix","announced-date":"2011-02-08T19:00:00.000-05:00"}')
        self.test_product_2_no_family = Product('{"product_name":"Casio_QV-5000SX","manufacturer":"Casio","model":"QV-5000SX","announced-date":"1998-04-19T20:00:00.000-04:00"}')
        self.test_product_3 = Product('{"product_name":"Casio_Exilim_EX-H20G","manufacturer":"Casio","model":"EX-H20g","family":"Exilim","announced-date":"2010-09-19T20:00:00.000-04:00"}')
        self.test_listing_1 = Listing('{"title":"Canon PowerShot D10 12.1 MP Waterproof Digital Camera with 3x Optical Image Stabilized Zoom and 2.5-inch LCD (Blue/Silver)","manufacturer":"Canon Canada","currency":"CAD","price":"420.33"}')
        self.test_listing_2 = Listing('{"title":"Olympus PEN E-PL1 12.3MP Live MOS Micro Four Thirds Interchangeable Lens Digital Camera with 14-42mm f/3.5-5.6 Zuiko Digital Zoom Lens (Black)","manufacturer":"Olympus Canada","currency":"CAD","price":"598.97"}')
        self.test_listing_3_unicode = Listing('{"title":"Canon - EOS 400D - Appareil photo numérique reflex boîtier nu - 10,1 Mpix","manufacturer":"Canon","currency":"EUR","price":"279.00"}')
        self.tree = Tree()
        self.tree.insert(self.test_product_1)
        self.tree.insert(self.test_product_2_no_family)
        self.tree.insert(self.test_product_3)
 
    def test_binary_manufacturer_setup(self):
        # There's two manufacturers: "casio" and "nikon"
        self.assertEqual(len(self.tree._children), 2)
        self.assertEqual(self.tree._children[0]._type, 'manufacturer')
        self.assertEqual(self.tree._children[1]._type, 'manufacturer')
        self.assertEqual(self.tree._children[0]._id, 'nikon')
        self.assertEqual(self.tree._children[1]._id, 'casio')

    def test_binary_family_setup(self):
        # For the Nikon, there's only one family and it's "coolpix"
        self.assertEqual(len(self.tree._children[0]._children), 1)
        self.assertEqual(self.tree._children[0]._children[0]._type,'family')
        self.assertEqual(self.tree._children[0]._children[0]._id,'coolpix')
        # For Casio, there should be two family children: "None" and "exilim"
        self.assertEqual(len(self.tree._children[1]._children), 2)
        self.assertEqual(self.tree._children[1]._children[0]._type,'family')
        self.assertEqual(self.tree._children[1]._children[0]._id, None)
        self.assertEqual(self.tree._children[1]._children[1]._type,'family')
        self.assertEqual(self.tree._children[1]._children[1]._id,'exilim')
        
    def test_binary_model_setup(self):
        # Only one model for the `nikon` branch
        self.assertEqual(len(self.tree._children[0]._children[0]._children),1)
        # brevity
        nikon_model = self.tree._children[0]._children[0]._children[0]
        self.assertEqual(nikon_model._type, 'model')
        self.assertEqual(nikon_model._id,'s6100')
        # Should be only model modein in separate branches of the `Casio` branch
        self.assertEqual(len(self.tree._children[1]._children[0]._children),1)
        self.assertEqual(len(self.tree._children[1]._children[1]._children),1)
        casio_model_1 = self.tree._children[1]._children[0]._children[0]
        casio_model_2 = self.tree._children[1]._children[1]._children[0]
        self.assertEqual(casio_model_1._type, 'model')
        self.assertEqual(casio_model_1._id,'qv5000sx')
        self.assertEqual(casio_model_2._type,'model')
        self.assertEqual(casio_model_2._id,'exh20g')

    def test_find_returns_product(self):
        self.assertEqual(isinstance(self.tree.find(Listing('{"title":"Casio Exilim EX-H20G EXILIM Hi-Zoom; 14.1 MP; 4320 x 3240 pixels; 4 x; 10 x; 3.2 - 5.7; 3.2 - 7.5 (EX-H20GSREDA)","manufacturer":"CASIO","currency":"GBP","price":"246.24"}')),Product), True)
Exemplo n.º 18
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.º 19
0
 def update_tree(self,tree_dict):
     
     try:
         tree_dict = self.merged_tree(tree_dict)
     except:
         return False, ['invalid_parameters']
     
     if tree_dict['tree_name'] is None:
         return False, ['tree_not_found']
     
     if tree_dict['moderatorname'] is None:
         return False, ['unauthenticated']
     
     author_info = UserInfo.get_by_username(tree_dict['moderatorname'])
     
     if author_info is None or not self.is_user_info_current(author_info):
         return False, ['unauthenticated']
     
     if author_info.username is None:
         return False, ['invalid_user']
     
     tree = Tree.get_by_name(tree_dict['tree_name'])
     
     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']
     
     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 = 'edit_tree'
     notification.tree_name = tree_dict['tree_name']
     notification.put()
         
     return True, tree
Exemplo n.º 20
0
    def Save(self, department=None, tree_type=None, name=None):
        """
		Save the vTree in the db
		"""
        if department:
            self.data['department'] = department
        if tree_type:
            self.data['tree_type'] = tree_type
        if name:
            self.data['name'] = name

        error = []
        if not self.data['department']:
            error.append("Missing department.")
        if not self.data['tree_type']:
            error.append("Missing vTree type.")
        if error:
            print "Error saving vTree:"
            for message in error:
                print "\t", message
            return

        # find the version number and increment it
        existing = Tree.find(department=self.data['department'],
                             tree_type=self.data['tree_type'],
                             name=self.data['name'])
        if existing:
            self.data['version'] = float(existing[0].data['version']) + .1
        else:
            self.data['version'] = 1.0
        # save the main vtree
        self.save()
        # save the directories
        for dir_obj in self.dirs:
            dir_obj.data['vTree_uid'] = self.data['uid']
            dir_obj.save()
        # save the files
        for file_obj in self.files:
            file_obj.data['vTree_uid'] = self.data['uid']
            # have the file object get the conetents of the file
            file_obj.read()
            file_obj.save()
Exemplo n.º 21
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.º 22
0
 def delete_branch(self,authorname,urlsafe_key):
     
     userinfo = UserInfo.get_by_username(authorname)
     
     if userinfo is None or not self.is_user_info_current(userinfo):
         return False, [ 'unauthenticated' ]
     
     branch = ndb.Key(urlsafe=urlsafe_key).get()
     
     if len(branch.children()) != 0:
         return False, [ 'not_empty' ]
     
     tree = Tree.get_by_name(branch.tree_name)
     
     if branch.authorname != authorname and tree.moderatorname != authorname:
         return False, [ 'not_author' ]
     
     branch.detached_parent_branch = branch.parent_branch
     branch.parent_branch = None
     
     branch.put();
     branch.detached_parent_branch.get().empty_children_cache()
     
     return True, None
Exemplo n.º 23
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
Exemplo n.º 24
0
    def save_branch(self,authorname,parent_urlsafe_key,link,content):
        
        userinfo = UserInfo.get_by_username(authorname)    
        if userinfo is None or not self.is_user_info_current(userinfo):
            
            return False, [ 'unauthenticated' ]
        
        branch = Branch()
        branch.authorname = authorname
        
        parent_key = ndb.Key(urlsafe=parent_urlsafe_key)
        parent_branch = parent_key.get()
        
        if parent_branch is None:
            return False, ['parent_branch_not_found']
        
        branch.tree_name = parent_branch.tree_name
        
        tree = Tree.get_by_name(parent_branch.tree_name)
        
        if tree is None:
            return False, ['tree_not_found']

        if tree.moderatorname == authorname or not tree.link_moderator_only:
            branch.link = link
        else:
            branch.link = ""
        
        if tree.moderatorname == authorname or not tree.content_moderator_only:
            branch.content = content
        else:
            branch.content = ""
        
        errors = self.validate_branch(tree, branch,authorname)
                    
#currently there is a db lock on unique links
#eventually we should have a memcache lcok
        
        authored_branch_count = 0
                
        branchs = parent_branch.children()
        
        if tree.single_thread and parent_branch.authorname == authorname:
            errors.append('has_single_thread_parent_branch')
        
        if tree.single_thread and len(branchs) > 0:
            errors.append('has_single_thread_branch')
        
        for branch_branch in branchs:
            if branch_branch.link == branch.link:
                errors.append('has_identical_link')
            if branch_branch.authorname == authorname:
                authored_branch_count += 1
        
        if tree.branch_max > 0 and tree.branch_max <= authored_branch_count:
            errors.append('has_branches')
        
        if len(errors) == 0:
            branch.revision = 0
            branch.parent_branch = parent_key
            branch.parent_branch_authorname = parent_branch.authorname
            branch.put()
            self.create_branch_version(branch)
            
            parent_branch.append_child(branch)
            
            notification = Notification()
            notification.from_username = branch.authorname
            if branch.authorname != parent_branch.authorname:
                notification.to_username = parent_branch.authorname
            notification.notification_type = 'new_branch'
            notification.branch = branch.key
            notification.branch_link = branch.link
            notification.tree_name = branch.tree_name
            notification.put()
            
            return True, branch
        else:
            return False, errors
Exemplo n.º 25
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.º 26
0
    def update_branch(self,authorname,urlsafe_key,link,content):
        
        userinfo = UserInfo.get_by_username(authorname)
        
        if userinfo is None or not self.is_user_info_current(userinfo):
            return False, [ 'unauthenticated' ]
        
        branch = ndb.Key(urlsafe=urlsafe_key).get()
        tree = Tree.get_by_name(branch.tree_name)
        
        if tree is None:
            return False, [ 'tree_not_found' ]
        
        if branch.authorname != authorname and tree.moderatorname != authorname:
            return False, [ 'not_author' ]
        
        if tree.moderatorname == authorname or not tree.link_moderator_only:
            branch.link = link
        if tree.moderatorname == authorname or not tree.content_moderator_only:
            branch.content = content
                
        errors = self.validate_branch(tree, branch,authorname)
        
#Comment on thread safety:
#Branch creation will be a VERY frequent operation. Multiple branchs with identical links
#Isn't an application error, just an annoyance for the user. So we allow this to occur
#without a lock in place
        parent_branch = None

        if branch.parent_branch is not None:
            parent_branch = branch.parent_branch.get();
                    
            branchs = parent_branch.children()
            
            for branch_branch in branchs:
                if branch_branch.link == branch.link and branch.key != branch_branch.key:
                    errors.append('has_identical_link')
            
        if len(errors) == 0:
            if branch.revision is None:
                branch.revision = 1
            else:
                branch.revision = branch.revision + 1
                
            branch.put()
            self.create_branch_version(branch)
            if parent_branch is not None:
                parent_branch.update_child(branch)
            
            notification = Notification()
            notification.from_username = authorname
            #only set the to_username when a different user is performing the action
            if branch.authorname != authorname:
                notification.to_username = branch.authorname
            notification.notification_type = 'edit_branch'
            notification.branch = branch.key
            notification.branch_link = branch.link
            notification.tree_name = branch.tree_name
            notification.put()
                
            return True, branch
        else:
            return False, errors
Exemplo n.º 27
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.º 28
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.º 29
0
def store_post(request, object_ref):
    """
    This function is intended to accept the presented data (if possible)
    and update the database accordingly
    """
    body = request.body
    encoded_request = request.META['CONTENT_TYPE']
    yaml_match = re.compile(r'yaml', re.IGNORECASE)
    json_match = re.compile(r'json', re.IGNORECASE)
  
    data = None
    if yaml_match.search(encoded_request):
        data = yaml_load(body)
    elif json_match.search(encoded_request):
        data = json_load(body)
    else:
        return HttpResponseServerError("Unsupported format specified")
    
    if data == None:
        return HttpResponseServerError("Data could not be decoded")
    
    # If we land here, then we can assume that we have a dictionary called data
    # Which we need to add to the given item
    
    # We know that the uri is / divided. The last one is either an
    # Group identifier or an Key
        
    items = object_ref.split('/')
        
    pos = 0
    previous = None
    while pos < len(items):
        try:
            obj = Tree.objects.get(name = items[pos])
        except Tree.DoesNotExist:
            if pos == 0:
                obj = Tree.add_root(name = items[pos])
            else:
                obj = previous.add_child(name = items[pos])
        pos = pos + 1          
        previous = obj
        
            
    try:
        tree = Tree.objects.get(name = items[-1])
    except Tree.DoesNotExist:
        return HttpResponseServerError("Referenced tree could not be found")
    
    
    for k, v in data.iteritems():
        kv_object = None
        new_object = False
        try:
            kv_object = KeyValue.objects.get(key = k, tree_id = tree)
        except KeyValue.DoesNotExist:
            kv_object = KeyValue()
            new_object = True

        kv_object.key = k
        kv_object.value = v
        kv_object.save()
        if new_object:
            kv_object.tree_id.add(tree)
            kv_object.save()
        
    return make_response(request, { 'result' : 'ok'})
Exemplo n.º 30
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
Exemplo n.º 31
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.º 32
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.º 33
0
 def save(self,request):
     from django.contrib.gis.geos import Point
     species = self.cleaned_data.get('species_id')
     if species:
         spp = Species.objects.filter(symbol=species)
         if spp:
             new_tree = Tree(species=spp[0])
         else:
             new_tree = Tree()
     else:
         new_tree = Tree()
     address = self.cleaned_data.get('edit_address_street')
     if address:
         new_tree.address_street = address
         new_tree.geocoded_address = address
     city = self.cleaned_data.get('edit_address_city')
     geo_address = self.cleaned_data.get('geocode_address')
     if geo_address:
         new_tree.geocoded_address = geo_address
     if city:
         new_tree.address_city = city
     zip_ = self.cleaned_data.get('edit_address_zip')
     if zip_:
         new_tree.address_zip = zip_
     
     plot_width = self.cleaned_data.get('plot_width')
     plot_width_in = self.cleaned_data.get('plot_width_in')
     if plot_width:
         new_tree.plot_width = float(plot_width)
     if plot_width_in:
         new_tree.plot_width = new_tree.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:
         new_tree.plot_length = float(plot_length)
     if plot_length_in:
         new_tree.plot_length = new_tree.plot_length + (float(plot_length_in) / 12)
     plot_type = self.cleaned_data.get('plot_type')
     if plot_type:
         new_tree.plot_type = plot_type
     power_lines = self.cleaned_data.get('power_lines')
     if power_lines != "":
         new_tree.powerline_conflict_potential = power_lines
     height = self.cleaned_data.get('height')
     if height:
         new_tree.height = height
     canopy_height = self.cleaned_data.get('canopy_height')
     if canopy_height:
         new_tree.canopy_height = canopy_height
     dbh = self.cleaned_data.get('dbh')
     dbh_type = self.cleaned_data.get('dbh_type')
     if dbh:
         if dbh_type == 'circumference':
             dbh = dbh / math.pi
         new_tree.dbh = dbh
     sidewalk_damage = self.cleaned_data.get('sidewalk_damage')
     if sidewalk_damage:
         new_tree.sidewalk_damage = sidewalk_damage
     condition = self.cleaned_data.get('condition')
     if condition:
         new_tree.condition = condition
     canopy_condition = self.cleaned_data.get('canopy_condition')
     if canopy_condition:
         new_tree.canopy_condition = canopy_condition
     
     import_event, created = ImportEvent.objects.get_or_create(file_name='site_add',)
     new_tree.import_event = import_event
     
     pnt = Point(self.cleaned_data.get('lon'),self.cleaned_data.get('lat'),srid=4326)
     new_tree.geometry = pnt
     new_tree.last_updated_by = request.user
     new_tree.save()
     
     return new_tree
Exemplo n.º 34
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.º 35
0
    def save(self,request):
        from django.contrib.gis.geos import Point
        species = self.cleaned_data.get('species_id')
        if species:
            spp = Species.objects.filter(symbol=species)
            if spp:
                new_tree = Tree(species=spp[0])
            else:
                new_tree = Tree()
        else:
            new_tree = Tree()
        add = self.cleaned_data.get('edit_address_street')
        if add:
            new_tree.address_street = add
            new_tree.geocoded_address = add
        city = self.cleaned_data.get('edit_address_city')
        if city:
            new_tree.address_city = city

        zip_ = self.cleaned_data.get('edit_address_zip')
        if zip_:
            new_tree.address_zip = zip_
            
        #import pdb;pdb.set_trace()
        pnt = Point(self.cleaned_data.get('lon'),self.cleaned_data.get('lat'),srid=4326)
        new_tree.geometry = pnt
        n = Neighborhood.objects.filter(geometry__contains=pnt)
        z = ZipCode.objects.filter(geometry__contains=pnt)
        if n: new_tree.neighborhood = n[0]
        else: new_tree.neighborhood = None
        if z: new_tree.zipcode = z[0]
        else: new_tree.zipcode = None
        new_tree.last_updated_by = request.user
        new_tree.save()
        dbh = self.cleaned_data.get('dbh')
        if dbh:
            ts = TreeStatus(
                reported_by = request.user,
                value = dbh,
                key = 'dbh',
                tree = new_tree)
            ts.save()
        return new_tree
Exemplo n.º 36
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
Exemplo n.º 37
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.º 38
0
def get_all_trees():
    result = Tree.select().execute()
    return list(result)
Exemplo n.º 39
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.º 40
0
def delete_tree_by_name(name):
    trees_deleted = Tree.delete().where(Tree.name==name).execute()
    return trees_deleted > 0
Exemplo n.º 41
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.º 42
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')