def next(self): features = [] relevances = [] while self.curr_line == None: try: self.curr_line = self.f.__next__() except Exception: self.curr_file += 1 self.curr_line = None if self.curr_file > 5: return None self.fname = settings.DATA_DIR + "mslr/mslr_%s%d.txt" % ( self.trainflag, self.curr_file) print("Opening new file: %s" % (self.fname)) self.f = open(self.fname) data = self.curr_line.split(" ") curr_q = int(data[1].split(":")[-1]) relevances.append(int(data[0])) features.append(np.array([float(x.split(":")[-1]) for x in data[2:-1]])) while True: try: self.curr_line = self.f.__next__() except Exception: if self.loop: self.f = open(self.fname) else: self.curr_file += 1 self.curr_line = None if self.curr_file > 5: return None self.fname = settings.DATA_DIR + "mslr/mslr_%s%d.txt" % ( self.trainflag, self.curr_file) print("Opening new file: %s" % (self.fname)) self.f = open(self.fname) self.curr_line = self.f.__next__() data = self.curr_line.split(" ") if int(data[1].split(":")[-1]) != curr_q: ## We have moved onto the next query features = np.array(features, dtype=np.float32) if self.K == None: toret = Context.Context(self.curr_idx, features, features.shape[0], self.L) self.curr_idx += 1 return (toret, np.array(relevances, dtype=np.float32)) else: if features.shape[0] < self.K: ## We want to ignore this context. Doesn't have enough actions return self.next() else: toret = Context.Context(self.curr_idx, features[0:self.K, :], self.K, self.L) self.curr_idx += 1 return (toret, np.array(relevances[0:self.K], dtype=np.float32)) relevances.append(int(data[0])) features.append( np.array([float(x.split(":")[-1]) for x in data[2:-1]]))
def collect_context_words(filename, cat): word = re.sub("-0.json", "", filename) with open(dirname + filename, "r", encoding="utf-8") as f: data = json.load(f) res = { "full_count": data["full_count"], "sample_size": data["sample_size"] } for row in data["data"]: context = Context(row, direction="reversed", role=cat) if len(context.res_words) > 0: for r in context.res_words: if test_proper_name(context.all_words.index(r), r) == True or " " in r[LEMMA]: r_word = "PROP" elif r[POS] == "Num": r_word = "NUM" else: r_word = r[LEMMA] if r_word in res: res[r_word] += 1 else: res.update({r_word: 1}) return res
def get_new_context(self): context_idx = np.random.randint(0, self.X) self.curr_x = Context.Context(context_idx, self.features[context_idx, :, :], self.docsPerQuery[context_idx], self.L) self.curr_r = self.relevances[context_idx, :] return self.curr_x
def split(self, partial_observations, attr_list, attribute_dict={}): if len(attr_list) == 0: return partial_observations_grouped = partial_observations.groupby("Price").sum() mu = np.max(partial_observations_grouped["Converted"] / partial_observations_grouped["Total"]) info = [] for attr in attr_list: attr_index = attr_list.index(attr) obs_attr_0 = partial_observations[partial_observations[attr_list[attr_index]] == 0].groupby("Price").sum() mu_attr_0 = np.max(obs_attr_0["Converted"] / obs_attr_0["Total"]) obs_attr_1 = partial_observations[partial_observations[attr_list[attr_index]] == 1].groupby("Price").sum() mu_attr_1 = np.max(obs_attr_1["Converted"] / obs_attr_1["Total"]) p = partial_observations[partial_observations[attr_list[attr_index]] == 1].size / partial_observations.size attr_info = mu_attr_0 * (1 - p) + mu_attr_1 * p info.append(attr_info) candidate_info = np.max(info) candidate_attr = attr_list[np.argmax(info)] if candidate_info > mu: attr_dict_0 = attribute_dict.copy() attr_dict_0[candidate_attr] = 0 attr_dict_1 = attribute_dict.copy() attr_dict_1[candidate_attr] = 1 del self.context_list[-1] self.context_list.append(Context(n_arms=self.n_arms, dict=attr_dict_0)) self.context_list.append(Context(n_arms=self.n_arms, dict=attr_dict_1)) attr_list.remove(candidate_attr) self.split(partial_observations[partial_observations[candidate_attr] == 0], attr_list=attr_list, attribute_dict=attr_dict_0) self.split(partial_observations[partial_observations[candidate_attr] == 1], attr_list=attr_list, attribute_dict=attr_dict_1) return self.context_list
def offline_evaluate(self, policy): score = 0.0 for x in range(self.X): score += np.sum(self.relevances[ x, policy.get_action( Context.Context(x, self.features[ x, :, :], self.docsPerQuery[x], self.L))]) return score
def next(self): if self.loop: self.curr_idx = np.random.randint(0, len(self.docsPerQuery)) if self.curr_idx >= len(self.docsPerQuery): return None if "clusters" in dir(self): curr_x = Context.Context(self.curr_idx, self.features[self.curr_idx, :, :], self.docsPerQuery[self.curr_idx], self.L, clusters=self.clusters[self.curr_idx]) else: curr_x = Context.Context(self.curr_idx, self.features[self.curr_idx, :, :], self.docsPerQuery[self.curr_idx], self.L) curr_r = self.relevances[self.curr_idx, :] self.curr_idx += 1 return (curr_x, curr_r)
def test_misconfigured_add_notification_is_noop(): n_notifs = Notification.objects.count() AddNotification(make_bind_data( constants={ "recipient_type": RecipientType.SPECIFIC_USER, "message": "This'll never get delivered!", } )).execute(Context()) assert Notification.objects.count() == n_notifs
def next(self): if self.loop: self.curr_idx = np.random.randint(0, self.X) if self.curr_idx >= len(self.docsPerQuery): return None q_idx = self.order[self.curr_idx] curr_x = Context.Context(q_idx, self.features[q_idx, :, :], self.docsPerQuery[q_idx], self.L) curr_r = self.relevances[q_idx, :] self.curr_idx += 1 return (curr_x, curr_r)
def test_state(self): expectedFinalState = "Done(已完成)" actualFinalState = "" context = Context.Context() while (context.currentState != None): actualFinalState = str(context.currentState) print("需求目前狀態=" + actualFinalState) context.action() self.assertEqual(actualFinalState, expectedFinalState)
def test_render_template(): step = Step( conditions=(), actions=[Action.unserialize(action) for action in TEST_STEP_ACTIONS], ) assert step execution_context = Context(variables={ "customer_phone": "0594036495", "language": "fi", "customer_email": "*****@*****.**" }) step.execute(context=execution_context)
def __init__(self, *args, **kwargs): #super(TeXDocument, self).__init__(*args, **kwargs) if 'context' not in kwargs: import Context self.context = Context.Context(load=True) else: self.context = kwargs['context'] if 'config' not in kwargs: import Config self.config = Config.config else: self.config = kwargs['config']
def __init__(self, *args, **kwargs): #super(TeXDocument, self).__init__(*args, **kwargs) if 'context' not in kwargs: import Context self.context = Context.Context(load=True) else: self.context = kwargs['context'] if 'config' not in kwargs: import Config self.config = Config.config else: self.config = kwargs['config'] # post parsing callbacks list self.postParseCallbacks = []
def next(self): """ Generate a new context with ld_features that are uniform 1/0 and with reward that is x_1 XOR x_2 """ if self.loop == False and self.curr_idx > 200: return None features = np.matrix(np.zeros([self.K, self.d])) features = np.matrix(np.random.binomial(1, 0.5, [self.K, self.d])) features = 2 * features - 1 curr_x = Context.Context(self.curr_idx, features, self.K, self.L) curr_r = np.array( [features[i, 0] * features[i, 1] for i in range(self.K)]) self.curr_idx += 1 return (curr_x, curr_r)
def run(fn, text): # Generate tokens lexer = Lexer(fn, text) tokens, error = lexer.make_tokens() if error: return None, error # Generate AST parser = Parser(tokens) ast = parser.parse() if ast.error: return None, ast.error # Run program interpreter = Interpreter() context = Context('<program>') context.symbol_table = global_symbol_table result = interpreter.visit(ast.node, context) return result.value, result.error
def __init__(self, X, N, K, L, eps, one_pass=False): """ Simulate a contextual semi-bandit problem with X contexts, N policies, K base actions and action lists of length L. Use the uniform distribution over contexts. Policies are effectively random, on each context they play L random actions. One policy is optimal and the reward distribution is Ber(1/2+eps) on each base action played by that policy and Bet(1/2-eps) on each other base action. Additionally exposes: get_slate_reward(A) -- total reward for a slate get_base_rewards(A) -- rewards for each base action in slate. """ self.L = L self.N = N self.K = K self.eps = eps self.X = X self.K = K self.one_pass = False ## Initialize the contexts. self.contexts = [] for i in range(self.X): self.contexts.append( Context.Context(i, np.zeros((1, 1)), self.K, self.L)) ## Initialize the policies. piMat = np.zeros((N, X, L), dtype=np.int) for n in range(N): for x in range(X): piMat[n, x, :] = np.random.choice(range(K), L, replace=False) self.Pi = [] for i in range(N): pi = EnumerationPolicy( dict( zip(range(self.X), [piMat[i, j, :] for j in range(self.X)]))) self.Pi.append(pi) ## Initialize the optimal policy self.Pistar = np.random.randint(0, N) self.curr_idx = -1
def __init__(self, X, N, K, L, eps, w_vec=None, link="linear", one_pass=False, reward_noise=0.1): self.X = X self.N = N self.K = K self.L = L self.eps = eps self.one_pass = one_pass self.link = link if w_vec is None: self.weight = np.ones(L) else: self.weight = w_vec self.reward_noise = 0.1 assert len(self.weight) == self.L ## Initialize contexts. self.contexts = [] for i in range(self.X): self.contexts.append( Context.Context(i, np.zeros((1, 1)), self.K, self.L)) piMat = np.zeros((N, X, L), dtype=np.int) for n in range(N): for x in range(X): piMat[n, x, :] = np.random.choice(range(K), L, replace=False) self.Pi = [] for i in range(N): pi = EnumerationPolicy( dict( zip(range(self.X), [piMat[i, j, :] for j in range(self.X)]))) self.Pi.append(pi) ## Initialize the optimal policy self.Pistar = np.random.randint(0, N) self.curr_idx = -1
def __init__(self, X, N, K, eps, one_pass=False, reward_noise=0.0): """ Simulate a K-armed contextual bandit game with X contexts, N policies, and K actions with gap eps. Use the uniform distribution over contexts. Policies are effectively random. Pick one policy to be the best and for each context, the reward distribution Ber(1/2+eps) on the action of that policy and Ber(1/2-eps) on the other actions. Exposes methods: get_new_context() -- construct and return new context get_num_actions() -- enables context-specific K get_reward(a) -- get reward for an action get_all_rewards() -- return rewards for all actions. get_best_reward() -- returns reward for pi^\star for book-keeping purposes. @Deprecated """ self.L = 1 self.N = N self.K = K self.eps = eps self.X = X self.one_pass = one_pass self.reward_noise = reward_noise ## Initialize the contexts. self.contexts = [] for i in range(self.X): self.contexts.append( Context.Context(i, np.zeros(1), self.K, self.L)) ## Initialize the policies. piMat = np.matrix(np.random.randint(0, K, size=[N, X, self.L])) self.Pi = [] for i in range(N): pi = EnumerationPolicy( dict(zip(range(self.X), [piMat[i, j] for j in range(self.X)]))) self.Pi.append(pi) ## Initialize the optimal policy self.Pistar = np.random.randint(0, N)
def build_concordances(config_data, corrected="auto"): data = open_data(config_data) dirname = config_data[PROJECT_NAME] + "/concordances_" + corrected + "/" test_dir(dirname) csv_dicts = {x: [] for x in range(0, len(CAT_NAMES))} correction_data = open_correction_data(config_data, rewrite=True) for row in data: if row["id"] in correction_data and corrected != "auto": cor = correction_data[row["id"]] else: cor = None context = Context(row, corrections=cor, config_data=config_data) if context.disregard == False: csv_dicts[context.role].append(context.get_concordance_dict()) if len(context.coords) > 0: csv_dicts[COORDINATION].append( context.get_concordance_dict("coord")) for x in CAT_NAMES: with open(dirname + x + ".tsv", "w", encoding="utf-8") as f: writer = csv.DictWriter(f, fieldnames=xls_headers, delimiter="\t") writer.writeheader() for row in csv_dicts[CAT_NAMES.index(x)]: writer.writerow(row) workbook = xlwt.Workbook() sheets = {CAT_NAMES.index(x): workbook.add_sheet(x) for x in CAT_NAMES} for cat in range(0, len(CAT_NAMES)): for j in range(0, len(xls_headers)): sheets[cat].write(0, j, xls_headers[j]) for cat in range(0, len(csv_dicts)): for i in range(0, len(csv_dicts[cat])): # if cat ==COORDINATION: print(csv_dicts[cat][i]) # print(len(csv_dicts[cat][i])) for j in range(0, len(csv_dicts[cat][i])): sheets[cat].write(i + 1, j, csv_dicts[cat][i][xls_headers[j]]) workbook.save(config_data[PROJECT_NAME] + "/concordances_" + corrected + ".xls")
def __init__(self, L=5, loop=True, dataset="letter", N=100, metric="ndcg"): self.one_pass = not loop self.L = L self.dataset = dataset self.data = DatasetBandit.DATASETS[dataset](L=self.L) self.K = self.data.K self.d = self.data.d self.has_ldf = self.data.has_ldf self.X = self.data.X self.contexts = [] for i in range(self.X): self.contexts.append( Context.Context(i, np.zeros((1, 1)), self.K, self.L)) self.r_feats = [] for i in range(self.X): self.r_feats.append(self.data.get_r_feat(i)) if metric == "ndcg": self.metric = Metrics.NDCG(self.L, self.data.relevances, False) else: self.metric = Metrics.SumRelevance(self.L) self.N = N tmp_policies = self.build_policies(N) piMat = np.zeros((N, self.X, self.L), dtype=np.int) for n in range(N): for x in range(self.X): act = tmp_policies[n].get_action(self.data.get_context(x)) piMat[n, x, :] = act self.Pi = [] for i in range(N): pi = EnumerationPolicy( dict( zip(range(self.X), [piMat[i, j, :] for j in range(self.X)]))) self.Pi.append(pi) self.Pistar = self.get_best_policy() self.curr_idx = 0 self.curr_x = None self.curr_r = None
def get_best_policy(self, learning_alg=None, classification=True): if learning_alg == None: return self.Pi[self.Pistar] else: ## Prepare dataset ## Train best depth 3 decision tree. dataset = [] for x in range(self.X): context = Context.Context(x, self.features[x, :, :], self.docsPerQuery[x], self.L) dataset.append((context, self.relevances[x, :])) if classification: return Argmax.argmax(self, dataset, policy_type=ClassificationPolicy, learning_alg=learning_alg) else: return Argmax.argmax(self, dataset, policy_type=RegressionPolicy, learning_alg=learning_alg)
def set_policies(self, policies): ## First make sure all policies respect the L requirement self.Pi = [] for pi in policies: assert type( pi) == EnumerationPolicy, "cannot set to non EnumerationPolicy" actions = pi.actions new_actions = {} for (k, v) in actions.items(): new_actions[k] = v[0:self.L] self.Pi.append(EnumerationPolicy(new_actions)) ## Compute Pistar print("---- Evaluating All Policies ----") Pistar = None best_score = 0.0 for p in range(len(self.Pi)): pi = self.Pi[p] score = 0.0 for i in range(len(self.contexts.docsPerQuery)): curr_x = Context.Context(i, self.contexts.features[i, :, :], self.contexts.docsPerQuery[i], self.L) curr_r = self.contexts.relevances[i, :] A = pi.get_action(curr_x) if self.metric != None: (val, clickeddocs, blah) = self.metric.computeMetric(curr_r[A], self.L, curr_x.get_name()) else: val = np.sum(curr_r[A]) score += val print("Policy %d Score %0.3f" % (p, score)) if Pistar == None or score >= best_score: Pistar = p best_score = score print("---- Best Policy is %d ----" % (Pistar)) self.Pistar = Pistar
def GenNewContext(self): newContext = Context(self.name, self.context, self.startPos) newContext.symbTable = SymbolTable(newContext.parent.symbTable) return newContext
def test_conditionless_step_executes(): step = Step(actions=[SetDebugFlag({})]) context = Context() step.execute(context) assert context.get("debug")
import argparse import sys from Context import * parser = argparse.ArgumentParser() parser.add_argument('-i', '--input', help='name of input file') parser.add_argument('output', help='name of output file') args = parser.parse_args() input = open(args.input, 'r') if args.input else sys.stdin output = open(args.output, 'w', encoding='utf-8') context = Context() if not args.input: print('Interactive mode. Ctrl-Z plus Return to exit.') for line in input: out = context.read_line(line) output.write(out) if not args.input: print(f'OUT: {repr(out)}')
def __init__(self, n_arms): self.n_arms = n_arms self.context_list = [] self.context_list.append(Context(self.n_arms))
--- mezzanine/utils/email.py.orig 2016-01-17 01:21:39.000000000 +0000 +++ mezzanine/utils/email.py @@ -4,7 +4,7 @@ from future.builtins import bytes, str from django.contrib.auth.tokens import default_token_generator from django.core.mail import EmailMultiAlternatives from django.core.urlresolvers import reverse -from django.template import loader, Context +from django.template import loader from django.utils.http import int_to_base36 from mezzanine.conf import settings @@ -25,7 +25,7 @@ def subject_template(template, context): Loads and renders an email subject template, returning the subject string. """ - subject = loader.get_template(template).render(Context(context)) + subject = loader.get_template(template).render(context) return " ".join(subject.splitlines()).strip() @@ -55,7 +55,7 @@ def send_mail_template(subject, template addr_bcc = [addr_bcc] # Loads a template passing in vars as context. render = lambda type: loader.get_template("%s.%s" % - (template, type)).render(Context(context)) + (template, type)).render(context) # Create and send email. msg = EmailMultiAlternatives(subject, render("txt"), addr_from, addr_to, addr_bcc,
def cpu(self): ''' Simulate the fetch-decode execute cycle ''' opcode = self.code[self.ip] while opcode != HALT and self.ip < len(self.code): if self.trace == True: print('{}'.format(self.disInstr())) self.ip = self.ip + 1 ## jump to next instruction or to operand if opcode == IADD: b = self.pop() ## 2nd opnd at top of stack a = self.pop() ## 1st opnd 1 below top self.push(a + b) ## push the result elif opcode == ISUB: b = self.pop() a = self.pop() self.push(a - b) elif opcode == IMUL: b = self.pop() a = self.pop() self.push(a * b) elif opcode == ILT: b = self.pop() a = self.pop() self.push(a < b) elif opcode == IEQ: b = self.pop() a = self.pop() self.push(a == b) elif opcode == BR: self.ip = self.code[self.ip] elif opcode == BRT: addr = self.code[self.ip] self.ip = self.ip + 1 if self.pop() == True: self.ip = addr elif opcode == BRF: addr = self.code[self.ip] self.ip = self.ip + 1 if self.pop() == False: self.ip = addr elif opcode == ICONST: self.push(self.code[self.ip]) ## push operand self.ip = self.ip + 1 elif opcode == LOAD: ## load local or arg offset = self.code[self.ip] self.ip = self.ip + 1 self.push(self.ctx.locals[offset]) elif opcode == GLOAD: ## load from global memory addr = self.code[self.ip] self.ip = self.ip + 1 self.push(self.globals[addr]) elif opcode == STORE: offset = self.code[self.ip] self.ip = self.ip + 1 self.ctx.locals[offset] = self.pop() elif opcode == GSTORE: addr = self.code[self.ip] self.ip = self.ip + 1 self.globals[addr] = self.pop() elif opcode == PRINT: print('{}'.format(self.pop())) elif opcode == POP: self.pop() elif opcode == CALL: ## expects all args on stack findex = self.code[self.ip] ## index of target function self.ip = self.ip + 1 nargs = self.metadata[findex].nargs ## how many args got pushed self.ctx = Context(self.ctx,self.ip,self.metadata[findex]) ## copy args into new context firstarg = self.sp-nargs+1; i = 0 while i < nargs: self.ctx.locals[i] = self.stack[firstarg+i] i = i+ 1 self.sp = self.sp - nargs; self.ip = self.metadata[findex].address; ## jump to function elif opcode == RET: self.ip = self.ctx.returnip self.ctx = self.ctx.ctx ## pop else: raise Exception('{:04} {} INVALID OPCODE'.format(self.ip-1,opcode)) if self.trace == True: print('{}'.format(self.stackString())) print('{}'.format(self.callStackString())) opcode = self.code[self.ip] if self.trace == True: print('{}'.format(self.disInstr())) print('{}'.format(self.stackString())) print('{}'.format(self.callStackString())) print('{}'.format(self.dumpDataMemory())) print()
def execute(self,startip = -1): self.ip = self.metadata[0].address if startip == -1 else startip self.ctx = Context(None,0,self.metadata[0]); ## simulate a call to main() self.cpu()
$NetBSD: patch-mezzanine_pages_templatetags_pages__tags.py,v 1.1 2017/12/30 13:44:21 adam Exp $ Django 1.11 support. https://github.com/stephenmcd/mezzanine/pull/1750 --- mezzanine/pages/templatetags/pages_tags.py.orig 2016-01-17 01:21:39.000000000 +0000 +++ mezzanine/pages/templatetags/pages_tags.py @@ -4,7 +4,7 @@ from future.builtins import str from collections import defaultdict from django.core.exceptions import ImproperlyConfigured -from django.template import Context, TemplateSyntaxError, Variable +from django.template import TemplateSyntaxError, Variable from django.template.loader import get_template from django.utils.translation import ugettext_lazy as _ @@ -131,7 +131,7 @@ def page_menu(context, token): context["page_branch_in_footer"] = True t = get_template(template_name) - return t.render(Context(context)) + return t.render(context.flatten()) @register.as_tag
def create(self): return Context.Context(self.collectionOfConditions)