def find_best_partition(self, x, y, avail_attrs, depth): # Randomly select F features from the available ones np.random.shuffle(avail_attrs) feat_ixs = avail_attrs[:self.n_features] # Compute the score for each attribute and keep the one with the highest score best_score = -100 for feat_ix in feat_ixs: score = self.compute_score(x[:, feat_ix], y) if score > best_score: best_feat_ix = feat_ix best_score = score # Annotate this feature as selected in the tree creation (To measure the feature importance in the forest) self.feat_selected[best_feat_ix] = 1 # Remove the attribute from the list of available attributes avail_attrs = [attr for attr in avail_attrs if attr != best_feat_ix] # Create the Node and add a child per value of the selected attribute out_node = Node(attribute=best_feat_ix, avail_attrs=avail_attrs, depth=depth, children={}) for val in self.attr_vals[best_feat_ix]: out_node.add_child(val, np.argwhere(x[:, best_feat_ix] == val)[:, 0]) return out_node
def create_node(self, node_id="anode"): node = Node(id=node_id) node.lastContact = datetime.now() node.status = Node.Status.IDLE db.session.add(node) db.session.commit() return node
def expand_tree(self, tree, x, y, depth): considered_insts = tree.get_instances() for key, val in tree.children.items(): if len(set(y[val])) == 1: # If there is only one class in this subset, set the child terminal value to this class tree.children[key] = Node(terminal_value=y[val[0]], depth=depth) elif len(val) == 0: # If the split left this branch empty, set the terminal value to the majority class of the parent subset labels, counts = np.unique(y[considered_insts], return_counts=True) terminal_value = labels[np.argmax(counts)] tree.children[key] = Node(terminal_value=terminal_value, depth=depth) elif self.min_samples_leaf > len(val) or depth == self.max_depth: # If the number of samples at this leaf is lower than the minimum or maximum depth has been reached, # set the terminal value to the majority class in the branch subset labels, counts = np.unique(y[val], return_counts=True) terminal_value = labels[np.argmax(counts)] tree.children[key] = Node(terminal_value=terminal_value, depth=depth) else: # Otherwise, find the best partition for this leaf and expand the subtree tree.children[key] = self.find_best_partition( x[val, :], y[val], tree.avail_attrs, depth) self.expand_tree(tree.children[key], x[val], y[val], depth + 1) return
def _process_break(self, action): node1 = Node(action.label) node2 = Node(action.label2) self.stack.append(node1) self.stack.append(node2) self.buffer_indices.pop(0) if len(self.buffer_indices) != 0: self.current_token_index = self.buffer_indices[0]
def __init__(self, Name, Parent=None, Description=""): """ Model for the sink :param Name: name of the model :param Parent: parent model :param Description: description of the model :param data: parameters and other required data """ # Instantiate the Node Node.__init__(self, Name, Parent=Parent, Description=Description)
def _make_named_entity(self, concept, literals): wiki_tag = "_".join(literals) wiki_node = Node(wiki_tag, tag=wiki_tag) name_node = Node("name") for i, literal in enumerate(literals): literal_node = Node(literal, tag=literal) name_node.add_child(literal_node, "op{}".format(i + 1)) concept_node = Node(concept) concept_node.add_child(wiki_node, "wiki") concept_node.add_child(name_node, "name") return concept_node
def test_amr_print_with_reentrancy(): r: Node = Node('receive-01') w: Node = Node('we') t: Node = Node('thing') r2: Node = Node('remind-01') p: Node = Node('pay-01') h: Node = Node('hospital') n: Node = Node('now') a: Node = Node('already') r.add_child(w, 'ARG0') r.add_child(t, 'ARG1') r.add_child(h, 'ARG2') r.add_child(n, 'time') r.add_child(a, 'time') t.add_child(r2, 'ARG0-of') r2.add_child(p, 'ARG1') r2.add_child(w, 'ARG2') p.add_child(w, 'ARG0') generated_amr_str = r.amr_print_with_reentrancy() expected_amr_str = """(r / receive-01~e.4 :ARG0 (w / we~e.0) :ARG1 (t / thing~e.7 :ARG0-of~e.7 (r2 / remind-01~e.7 :ARG1 (p / pay-01~e.6 :ARG0 w) :ARG2 w)) :ARG2~e.8 (h / hospital~e.10) :time (n / now~e.2) :time (a / already~e.3))""" smatch = calculate_smatch(generated_amr_str, expected_amr_str) assert smatch == 1
def test_amr_print_with_polarity(): b: Node = Node('bad') neg: Node = Node('-') i: Node = Node('imitate-01') c: Node = Node('country') d: Node = Node('develop-02') m: Node = Node('most') w: Node = Node('world') r: Node = Node('real-04') b.add_child(neg, 'polarity') b.add_child(i, 'ARG1') b.add_child(r, 'ARG1-of') i.add_child(c, 'ARG1') c.add_child(d, 'ARG1-of') c.add_child(w, 'location') d.add_child(m, 'degree') generated_amr_str = b.amr_print() generated_amr_no_spaces = ''.join(generated_amr_str.split()) expected_amr_str = """( d1 / bad :polarity - :ARG1 ( d1_1 / imitate-01 :ARG1 ( d1_1_1 / country :ARG1-of ( d1_1_1_1 / develop-02 :degree ( d1_1_1_1_1 / most ) ) :location ( d1_1_1_2 / world ) ) ) :ARG1-of ( d1_2 / real-04 ) )""" expected_amr_no_spaces = ''.join(expected_amr_str.split()) assert generated_amr_no_spaces == expected_amr_no_spaces, \ 'expected \n' + expected_amr_str + '\ngot\n' + generated_amr_str + '\n'
def __init__(self, machine_manager, volume_manager, context): self._driver = BTRFSDriver(context['node']['volume_path']) self._from_etcd = None self._machine_manager = machine_manager self._volume_manager = volume_manager self._node = Node(context['node'], self._driver) self._work = [] self._volume_loop = self._machine_loop = None self._max_error_count = context['max_error_count'] self._error_timeout = context['max_error_timeout'] self._delay = context['agent_ttl'] self._watch_timeout = context['watch_timeout'] self._started = False
def setUp(self): self.lowestCommonAncestor = LowestCommonAncestor() self.one = Node(1) self.two = Node(2, self.one) self.three = Node(3, self.one) self.four = Node(4, self.two) self.five = Node(5, self.two) self.six = Node(6, self.three) self.seven = Node(7, self.three) self.eight = Node(8, self.four) self.nine = Node(9, self.four)
def add_new_car(self, name, lane, intention): from utils.utils import cars_cross_path graph = self.get_graph() leaf_cars = self.get_leaf_cars() follow_list = list() visited = set() # list of nodes to_visit = [graph[car] for car in leaf_cars] # starts checking the leaves while to_visit: node = to_visit.pop(0) if node.get_name() not in visited: visited.add(node.get_name()) if cars_cross_path(lane, intention, node.get_lane(), node.get_intention()): follow_list.append(node.get_name()) for car in node.get_follow_list(): visited.add(car) else: # If it doesn't collide then add the follow list of the node to to_visit list to_visit = to_visit + [graph[car] for car in graph[node.get_name()].get_follow_list()] for car in follow_list: if car in self.get_leaf_cars(): leaf_cars.remove(car) leaf_cars.add(name) if name == self.get_name() and name in follow_list: follow_list.remove(name) graph[name] = Node(name=name, follow_list=follow_list, lane=lane, intention=intention) return follow_list
def get_nodes(self, node_label_selector, retry_timer=5): """ Returns a list of KubeNodes that is matched to the label selector. """ while True: try: api_client = client.CoreV1Api() if node_label_selector: ret = api_client.list_node( label_selector=node_label_selector) else: ret = api_client.list_node() kubenodes = [ Node.from_kubernetes_client(node_json=node) for node in ret.items ] kubenodes = sorted(kubenodes, key=lambda k: k.name) return kubenodes except ApiException as e: error_dict = json.loads(e.body) self.logger.error("{}: {}".format(error_dict['status'], error_dict['message'])) time.sleep(retry_timer)
def _update_nodes(self): try: nodes = [] for device in self._devices: node = Node(device) nodes.append(node) hub = self._get_hub(nodes) if hub is None or hub.neighbors is None: _LOGGER.error("No hub found") return for node in nodes: self._update_edges(node, nodes) for hop in range(len(nodes)): if not self._update_hop(hop, nodes): break for node in nodes: self._update_relation_type(node, nodes) self._nodes = nodes except Exception as ex: trace_back = sys.exc_info()[2] line = trace_back.tb_lineno _LOGGER.error( f"Failed to update nodes due to error: {ex} [LN: {line}]")
def GET(self,id): page=int(web.input().get('page','1')) item=Node.objects(ID=int(id)).first() #todo 分页 or ajax comments=Tweet.objects(Q(SubjectID=int(id))&Q(flag__ne=1)).order_by("-ID") #flag=1 means share next=None if Auth.is_login(): user=User.objects(id=Auth.uid()).first() mycollect=user.nodes_follow if item.ID in mycollect: collected=True else: collected=False else: user=None collected=False if item and item.creatorID: creator=User.objects(ID=item.creatorID).first() else: creator =None datalist=[] for comment in comments: datalist.append((User.objects(ID=comment.AutherID).first(),comment)) if user==creator : editble=True else: editble=False if user and ( user.email=="*****@*****.**" or user.email=="*****@*****.**" ): editble=True return render.item(editble=editble,user=user,creator=creator,item=item,datalist=datalist,collected=collected)
def __init__(self, mode, values): if mode == GenMode.BST and values is not None: leaves = [Node(val=value) for value in values] root = leaves[0] self.root_node = root for leaf in leaves[1:]: self.build_bst(root, leaf)
def getSingleResult(self, results): for result in results: node = Node(result['_id'], result['name'], result['timestamp'], result['desc'], result['logEntryRef'], result['logCreator'], "", result['icon'], result['source'], result['visible']) return node
def GET(self,uid): myuid=Auth.uid() page=int(web.input().get("page","1")) thisguy=User.objects(ID=int(uid)).first() if not thisguy: return render.notfound() thisguy.tweet_count=Tweet.objects(AutherID=thisguy.ID).count() counts = thisguy.followings_count,thisguy.followed_count,thisguy.tweet_count #update count if page*20<counts[2]: next=page+1 elif page*20-counts[2]>20 : return web.notfound() else: next=None if Auth.is_login(): me=User.objects(id=Auth.uid()).first() else: me=None if not me: relation=None elif me==thisguy: relation="self" elif thisguy.ID in me.followings: relation="ifollowhim" else: relation=None # do i follow that people? datalist=[] posts=Tweet.objects(AutherID=thisguy.ID).order_by("-ID")[(page-1)*20:page*20] for p in posts: if p.SubjectID>0: if p.flag==1: datalist.append(("share" ,Node.objects(ID=p.SubjectID).first(),p, friendly_time(p.created).decode("utf-8"))) elif p.flag==-1: pass else: datalist.append(("comment" ,Node.objects(ID=p.SubjectID).first(),p, friendly_time(p.created).decode("utf-8"))) elif p.flag==-1: pass else: datalist.append(("say",p,friendly_time(p.created).decode("utf-8"))) return render.user(user=me,thisguy=thisguy,counts=counts,next=next,datalist=datalist,relation=relation)
async def update_node(self, hostname: str, ip_address: str, acquire: bool = False) -> None: """Updates the given node or creates a new one if it does not yet exist. :param str hostname: Hostname of the node :param str ip_address: IP Address of the node :param bool acquire: True, if the node should get acquired """ # search by hostname since IPs can change node = self.config.node_repository.get_node_by_hostname(hostname) # create or update the node if node is None: node = Node(name=hostname, online=True, ip_address=ip_address, hostname=hostname) await self.config.node_repository.add_node(node) self.log(node, 'added to registry') elif node.ip_address != ip_address: node.ip_address = ip_address await self.config.node_repository.call_listeners() self.log(node, 'ip address has changed') # update node state if node.online is False: node.online = True self.log(node, 'is online') await self.config.node_repository.call_listeners() await self.config.room_repository.call_listeners() # acquire node if necessary if node.room is not None: if node.acquired is False: if node.ip_address != self.master_ip: self.master.send_acquisition(node.ip_address) node.acquired = True self.log(node, 'acquired') elif acquire and node.ip_address != self.master_ip: # re-acquire if announcement is received again (e.g. in case of a restart) self.master.send_acquisition(node.ip_address) self.log(node, 're-acquired')
def GET(self): nodes=Node.objects(score=20).order_by("-ID") if Auth.is_login(): user=User.objects(id=Auth.uid()).first() else: user=None if nodes.count()>20: nodes=nodes[0:20] return render.explore(user=user,nodes=nodes)
def addCollection(self, values, parent): for i in range (0, len(values)): pred = Node(EOGraph.IRI, EOGraph.firstType) if(type(values[i])==type([])): obj = Node(EOGraph.blankNode) self.addTriple(parent, pred, obj) self.addCollection(values[i], obj) else: obj = Literal(values[i]) self.addTriple(parent, pred, obj) pred = Node(EOGraph.IRI, EOGraph.restType) if(i==len(values)-1): obj = Node(EOGraph.IRI, EOGraph.nilType) else: obj = Node(EOGraph.blankNode) self.addTriple(parent, pred, obj) parent=obj
def add(): if request.method == "GET": return render_template("node/add.html") elif request.method == "POST": id = request.form["id"] node = Node(id) db.session.add(node) db.session.commit() flash("Node '%s' added successfully" % node.id) return redirect(url_for(".show", id=node.id))
def test_amr_print_simple(): r: Node = Node('recommend-01') a: Node = Node('advocate-01') i: Node = Node('it') v: Node = Node('vigorous') r.add_child(a, 'ARG1') a.add_child(i, 'ARG1') a.add_child(v, 'manner') generated_amr_str = r.amr_print() generated_amr_no_spaces = ''.join(generated_amr_str.split()) expected_amr_str = """( d1 / recommend-01 :ARG1 ( d1_1 / advocate-01 :ARG1 ( d1_1_1 / it ) :manner ( d1_1_2 / vigorous ) ) )""" expected_amr_no_spaces = ''.join(expected_amr_str.split()) assert generated_amr_no_spaces == expected_amr_no_spaces, \ 'expected \n' + expected_amr_str + '\ngot\n' + generated_amr_str + '\n'
def DeclareEquations(self): """ This Method is called by the DaeTools. Here is where all the equations are defined :return: """ Node.DeclareEquations(self) self.eq_mass_balance() self.eq_energy_balance() self.eq_momentum_balance()
def GET(self,tag): page=int(web.input().get('page','1')) tag=tag.encode('utf-8') nodes=Node.objects(tags=tag) count=nodes.count() if int(page)*10<count: next=page+1 else: next=None results=[x for x in nodes] return render.tags(Auth,tag,results,count,next)
def m_node(m_driver, p_driver_get_usage): p_driver_get_usage.return_value = 30.0, [1.0, 1.0, 1.0] return Node({ 'volume_path': '/volumes', 'conf_path': '/etc/cobalt.conf', 'max_fill': 0.8, 'conf': { 'name': 'test-node', 'labels': ['ssd'] } }, m_driver)
def node(driver): node_conf = { 'conf_path': '/etc/cobalt.conf', 'volume_path': '/mnt', 'max_fill': 0.8, 'conf': { 'name': 'test-node', 'labels': ['ssd'] } } return Node(node_conf, driver)
def addEoTriples(self, graphDefinition, values, types, parent=None): for key in graphDefinition: pred = Node(EOGraph.IRI, EOGraph.schema.format(key)) if(type(graphDefinition[key])==type({})): nestedGraphObject = graphDefinition[key] node = None if("id" in nestedGraphObject): idValue = values[key] if key in values.keys() else "" node = Node(EOGraph.IRI, idValue) else: node = Node(EOGraph.blankNode) if(key in types.keys()): typeValue = types[key] self.addTriple(node, Node(EOGraph.IRI, EOGraph.fnType), Node(EOGraph.IRI, EOGraph.schema.format(typeValue))) if(parent!=None): self.addTriple(parent, pred, node) self.addEoTriples(nestedGraphObject, values, types, node) else: subj = parent if(key=="id" or key not in values or not values[key]): continue if(type(values[key])==type([])): obj = Node(EOGraph.blankNode) self.addTriple(parent, pred, obj) self.addCollection(values[key], obj) else: obj = Literal(values[key]) self.addTriple(subj, pred, obj)
def fit_tree(self, X, y, depth=0): """Fit a decision tree with recursive splitting on nodes. Args: X (ndarray): training set without class labels. y (ndarray) : class labels for training set. depth (int) : starting depth of decision tree. Returns: tree (Node): root node of learned decision tree. """ # Get number of training observations in current node with each class label 0 and 1. class_distribution = [np.sum(y == i) for i in range(self.n_classes)] # Instantiate node to grow the decision tree. tree = Node(n=y.size, class_distribution=class_distribution, gini_index=_gini(y, self.n_classes)) # Perform recursive splitting to max depth. if depth < self.max_depth: gini_index, split_index = self.get_split(X, y) # Get indices for data and class labels to go to the left child, send the rest to the right child. if split_index is not None: index_left = (X[:, split_index] == 1) X_left, y_left = X[index_left], y[index_left] X_right, y_right = X[~index_left], y[~index_left] tree.gini_index = gini_index tree.feature_index = split_index depth += 1 tree.left = self.fit_tree(X_left, y_left, depth=depth) tree.right = self.fit_tree(X_right, y_right, depth=depth) return tree
def wibednode(id): logging.debug('Node request from node with id %s', id) output = {} logging.debug('NODE REQUEST: %s', request.get_json(force=True)) try: input = json.loads(request.data) validateInput(input) # Attempt to get existing node from id node = Node.query.get(id) # If it doesn't exist, create a new one if not node: if Node.Status(input["status"]) == Node.Status.IDLE: output["reinit"] = {} return jsonify(**output) else: node = Node(id) db.session.add(node) # Update node fields based on input updateNode(node, input) handleExperimentData(node, input, output) handleFirmwareUpgrade(node, input, output) handleCommands(node, input, output) handleSendRestore(node, input, output) except Exception as e: logging.debug('Exception joining node: %s', e) output["errors"] = [str(e)] db.session.rollback() logging.debug('SERVER REPLY: %s', output) return jsonify(**output)
def POST(self): name=web.input().get("name") _tags=web.input().get("tags") source=web.input().get("source") if _tags: tags=[x for x in _tags.split(",")] else: tags=[] user=User.objects(id=Auth.uid()).first() creatorID=user.ID intro=web.input().get("intro") picurl=web.input().get("picurl",None) price=web.input().get("price") link=web.input().get("source") store=get_store("store") price=web.input().get("price") buyinfo=Buy_info(link=link,store=store,price=price) #check if none pic=urllib2.urlopen(picurl).read() ID=Node.objects.count()+1 open(str(ID)+".jpg","wb").write(pic) u = UpYun(BUCKETNAME,USER,PASSWORD) data = open(str(ID)+".jpg",'rb') u.setContentMD5(md5file(data)) a = u.writeFile('/o'+str(ID)+'.jpg',data) if not a: return "get picture erro" #cut_item_picture(STATIC_FILE,ID,str(ID)+".jpg") node=Node(creatorID=creatorID,ID=ID,name=name,des=intro,picurl=picurl,tags=tags,buy_info=[buyinfo]) node.picinfo=101 #a out sidelink node.save() share=Share_items(ID=Share_items.objects.count()+1,creatorID=user.ID,itemID=ID,flag=1,content="") share.save() #加入到shareitem中 #todo count更新 tweet=Tweet(ID=Tweet.objects.count()+1,AutherID=user.ID,SubjectID=ID,flag=1,content=str(ID)) tweet.save() return web.seeother("/item/"+str(ID)+"/")
def _process_shift(self, action): current_token = self.tokens[self.current_token_index] if self.parser_parameters.with_reattach and self._is_named_entity(): if self.parser_parameters.with_gold_concept_labels: node = self._make_named_entity(action.label, self.named_entity_metadata[0][1]) else: node = self._make_named_entity(self.index_word_map[current_token], self.named_entity_metadata[0][1]) self.named_entity_metadata.pop(0) elif self.parser_parameters.with_reattach and self._is_date_entity(): node = self._make_date_entity(self.date_entity_metadata[0][1], self.date_entity_metadata[0][2]) self.date_entity_metadata.pop(0) else: if self.parser_parameters.with_gold_concept_labels: node = Node(action.label) else: node = Node(self.index_word_map[current_token]) self.stack.append(node) self.buffer_indices.pop(0) if len(self.buffer_indices) != 0: self.current_token_index = self.buffer_indices[0]
def POST(self): picurl=web.input().get("check") name=web.input().get("name") _tags=web.input().get("tags") source=web.input().get("source") if _tags: tags=[x for x in _tags.split(",")] else: tags=[] user=User.objects(id=Auth.uid()).first() creatorID=user.ID intro=web.input().get("intro") price=web.input().get("price") link=web.input().get("source") store=web.input().get("store") #store=get_store(link).decode("utf-8") price=web.input().get("price") buyinfo=Buy_info(link=link,store=store,price=price) pic=urllib2.urlopen(picurl).read() ID=Node.objects.count()+1 open(str(ID)+".jpg","wb").write(pic) u = UpYun(BUCKETNAME,USER,PASSWORD) data = open(str(ID)+".jpg",'rb') u.setContentMD5(md5file(data)) a = u.writeFile('/o'+str(ID)+'.jpg',data) if not a: return "get picture erro" #cut_item_picture(STATIC_FILE,ID,str(ID)+".jpg") node=Node(creatorID=creatorID,ID=ID,name=name,des=intro,picurl=picurl,tags=tags,buy_info=[buyinfo]) node.picinfo=101 #a out sidelink node.save() share=Share_items(ID=Share_items.objects.count()+1,creatorID=user.ID,itemID=ID,flag=1,content="") share.save() content=u'''<h1>发布成功</h1><p>你可以<a href="/item/%s/">去看看</a>或者 <a href="%s">回到刚才的逛的</a>'''%(str(ID),source) return render.info(content=content) return web.seeother("/item/"+str(ID)+"/")
def main(): """ Main process for blockchain node. """ logging.basicConfig(level=logging.INFO) logging.info('\nStarting node server...') # Unpack network file with open("./photoblocks/network.json") as f: network = json.load(f) # Test local db connection db = redis.Redis(host='redis', port=6379) while True: try: if db.execute_command('PING'): logging.info(f'\nConnected to the local database server.') break else: continue except Exception as e: logging.error(f'\n{e}') continue # Construct Node object logging.info('\nCreating Node data structure from configuration...') node = Node(network["local"]) logging.info(f'\nNode data structure created.') # Start a node socket client logging.info('\nStarting node socket client on background thread...') thread = threading.Thread(target=ClientSock, args=(network, node)) logging.info('\nThread process created.') thread.daemon = True thread.start() logging.info('\nThread process started.') time.sleep(5) # Start node socket server logging.info('\nStarting node socket server on background thread...') thread = threading.Thread(target=serversock, args=()) logging.info('\nThread process created.') thread.daemon = True thread.start() logging.info('\nThread process started.') while True: time.sleep(5)
def validateInput(input): if not input: raise Exception("No input data provided.") if not "status" in input: raise Exception("No status information provided.") # Convert INT representation of status to Enum. input["status"] = Node.Status(input["status"]) if input["status"] == Node.Status.INIT: if not "model" in input: raise Exception("No model information provided.") if not "version" in input: raise Exception("No firmware version information provided.")
def _parse_nodes(self): node_sheet = self.workbook.sheet_by_name(NODE_SHEET) rows = node_sheet.get_rows() for i, each_row in enumerate(rows): if i == 0: # skip header row continue label = each_row[0].value size = each_row[1].value try: color = each_row[2].value except IndexError: color = random_colors_generator() node = Node(label=label, size=size, color=color) self.nodes.append(node)
def load(self) -> None: """Loads the configuration file and parses it into class attributes.""" self.type = NodeType[self.data.get('type').upper()] # load rooms for room_data in self.data.get('rooms'): self.rooms.append(Room.from_json(room_data)) # load nodes for node_data in self.data.get('nodes'): self.nodes.append(Node.from_json(node_data, self)) # load speakers for speaker_data in self.data.get('speakers'): self.speakers.append(Speaker.from_json(speaker_data, self)) # load rooms calibration data after loading speakers for room_data in self.data.get('rooms'): room = self.room_repository.get_room(room_data.get('id'), fail=True) room.calibration_points_from_json(room_data.get('calibration_points'), self)