def visibilities (self,array=None,observation=None,nodes=None,smear=False,**kw): """Creates nodes computing visibilities of component. 'array' is an IfrArray object, or None if the global context is to be used. 'observation' is an Observation object, or None if the global context is to be used. 'smear' is True if smearing is to be applied. 'nodes' is a base node (which will be qualified with p,q). If None, a default may be used. Returns something that must be qualified with (p,q) to get a visibility node. """; observation = Context.get_observation(observation); cohnodes = self.coherency(array,observation); # return coherency directly, if we're at phase centre if self.direction is observation.phase_centre: return cohnodes; # else apply, phase shifts and (optionally) smearing visnodes = nodes or self.ns.vis; array = Context.get_array(array); if not visnodes(*array.ifrs()[0]).initialized(): # apply smear factor if asked to (and if it's implemented) smear = (smear or self.is_smeared()) and \ self.smear_factor(array,observation.phase_centre); if smear: cohsm = visnodes('smear'); for p,q in array.ifrs(): cohsm(p,q) << smear(p,q)*cohnodes(p,q); cohnodes = cohsm; # phase shift self.direction.make_phase_shift(visnodes,cohnodes,array, Context.get_observation(observation).phase_center); return visnodes;
def coherency (self,array=None,observation=None,nodes=None,**kw): coherency = nodes or self.ns.coh; array = Context.get_array(array); if not coherency(*array.ifrs()[0]).initialized(): observation = Context.get_observation(observation); dir0 = observation.phase_centre; # baseline UVs uv1 = self.ns.uv1; shp = self.ns.shp; # rotation matrix xfm = self.transformation_matrix(); uv_ifr = array.uv_ifr(dir0); # inverse wavelength 1/wl = freq/c iwl = self.ns0.inv_wavelength << ((self.ns0.freq<<Meq.Freq) / 2.99792458e+8); # flux scale, conserved peak value, times the sqrt(1/scale) fluxscale = self.ns.fluxscale << self.brightness(observation)*(2*math.pi/(self._scale)); for ifr in array.ifrs(): uv=uv1(*ifr) << Meq.MatrixMultiply(xfm,uv_ifr(*ifr)*iwl) shptf= shp(*ifr) << Meq.ShapeletVisTf(modes=self._children,dep_mask=0xff); coherency(*ifr) << fluxscale*Meq.Compounder(children=[uv,shptf], common_axes=[hiid('L'),hiid('M')]); return coherency;
def coherency (self,array=None,observation=None,nodes=None,**kw): coherency = nodes or self.ns.coh; array = Context.get_array(array); if not coherency(*array.ifrs()[0]).initialized(): observation = Context.get_observation(observation); dir0 = observation.phase_centre; radec0 = dir0.radec(); # 1/wl = freq/c iwl = self.ns.inv_wavelength << ((self.ns.freq<<Meq.Freq) / 2.99792458e+8); # 1/(wl^2): scaling factor applied to exp() argument below m_iwlsq = self.ns.m_inv_wavelength_sq << Meq.Sqr(iwl); # scaling factor of gaussian for unit flux gscale = self.ns.diskcomponent_scale << Meq.Constant(1.0); # baseline UVs uv_ifr = array.uv_ifr(dir0); # flux scale -- coherency multiplied by scale constant above fluxscale = self.ns.fluxscale << self.brightness(observation) * gscale; # transformed uv's (rotated and scaled) uv1sq = self.ns.uv1sq; u1sq = self.ns.u1sq; v1sq = self.ns.v1sq; sumuvsq = self.ns.sumuvsq; sigma = self._parm("sigma"); # now generate nodes for ifr in array.ifrs(): # rotate uvs and scale to wavelength uv1s = uv1sq(*ifr) << Meq.Sqr(uv_ifr(*ifr)); u1s = u1sq(*ifr) << Meq.Selector(uv1s,index=0); v1s = v1sq(*ifr) << Meq.Selector(uv1s,index=1); rr = sumuvsq(*ifr) <<Meq.Sqrt((u1s+v1s)*m_iwlsq); coherency(*ifr) << fluxscale * Meq.Bessel(rr*2*math.pi*sigma,order=self._order)/rr; return coherency;
def brightness (self,observation=None,nodes=None,always_matrix=False): """Returns the brightness matrix for a point source. 'observation' argument is used to select a linear or circular basis; if not supplied, the global context is used. If always_matrix=True, returns matrix even if source is unpolarized. """; observation = observation or Context.observation; if not observation: raise ValueError,"observation not specified in global Meow.Context, or in this function call"; coh = nodes or self.ns.brightness; if not coh.initialized(): # if not polarized, just return I if not always_matrix and not self._polarized: if self._has_spi: if self._constant_flux: coh << Context.unitCoherency(self.stokes("I"))*self.norm_spectrum(); else: coh << Meq.Multiply(self.stokes("I"),self.norm_spectrum(),Context.unitCoherency); else: coh = Context.unitCoherency(self.stokes("I")); else: coh_els = self.coherency_elements(observation); if self._constant_flux: if self._has_spi: coh1 = self.ns.coh1 << Meq.Matrix22(*[Context.unitCoherency(x) for x in coh_els]); coh << coh1*self.norm_spectrum(); else: coh << Meq.Matrix22(*[Context.unitCoherency(x) for x in coh_els]); else: coh1 = self.ns.coh1 << Meq.Matrix22(*coh_els); if self._has_spi: coh << Meq.Multiply(coh1,self.norm_spectrum(),Context.unit_coherency); else: coh = Context.unitCoherency(coh1); return coh;
def coherency (self,array=None,observation=None,nodes=None,**kw): observation = Context.get_observation(observation); array = Context.get_array(array); coherency = nodes or self.ns.coh; if not coherency(*array.ifrs()[0]).initialized(): # create a brick uvw = array.uvw_ifr(observation.phase_center); for ifr in array.ifrs(): coherency(*ifr) << Meq.UVInterpolWave(brick=self.uvbrick(), uvw=uvw(*ifr), method=self._interpol_method, additional_info=self._interpol_debug); return coherency;
def make_phase_shift (self,vis,vis0,array=None,dir0=None): """phase-shifts visibilities given by vis0(p,q) from dir0 (the global phase center by default) to the given direction. Shifted visibilities are created as vis(p,q). """; dir0 = Context.get_dir0(dir0); array = Context.get_array(array); # if direction is the same, use an Identity transform if self is dir0: for p,q in array.ifrs(): vis(p,q) << Meq.Identity(vis0(p,q)); # else apply KJones and smear, if need be else: Kj = self.KJones(array=array,dir0=dir0); Jones.apply_corruption(vis,vis0,Kj,array.ifrs());
def coherency (self,array=None,observation=None,nodes=None,**kw): nodes = nodes or self.ns.vis; array = Context.get_array(array); ifrs = array.ifrs(); # no components -- use 0 if not self._components: [ nodes(*ifr) << 0.0 for ifr in ifrs ]; else: # each component is either a SkyComponent, else a visibility basenode # for up list of (component,visibility_node) pairs, where component=None if component is directly a node cv_list = [ (comp,comp.visibilities(array,observation)) if isinstance(comp,SkyComponent) else (None,comp) for comp in self._components ]; # determine which components are now solvable solvables = [ vis for comp,vis in cv_list if comp and comp.get_solvables() ]; nonsolvables = [ vis for comp,vis in cv_list if not (comp and comp.get_solvables()) ]; # if both types are present, add separately for optimum cache reuse if solvables and nonsolvables: solv = nodes('solv'); nonsolv = nodes('nonsolv'); # use the intelligence in Parallelization to add in a smart way, depending on out # parallelization settings Parallelization.add_visibilities(solv,solvables,ifrs); Parallelization.add_visibilities(nonsolv,nonsolvables,ifrs); for ifr in ifrs: nodes(*ifr) << Meq.Add(solv(*ifr),nonsolv(*ifr)); else: # use the intelligence in Parallelization to add in a smart way, depending on out # parallelization settings Parallelization.add_visibilities(nodes,solvables+nonsolvables,ifrs); return nodes;
def outer_join(first: str, second: str, first_fv: {str, str}, second_fv: {str, str}, context: Context) -> str: join_variables = first_fv.intersection(second_fv) first_only = first_fv.difference(second_fv) second_only = second_fv.difference(first_fv) sql = """CREATE TABLE {dest} AS SELECT {varlist1} FROM {src1} LEFT JOIN {src2} ON {join_condition} UNION SELECT {varlist2} FROM {src2} LEFT JOIN {src1} ON {join_condition} """ name = context.get_table_name() varlist1 = ", ".join([first + "." + e for e, _ in first_only] + [first + "." + e for e, _ in join_variables] + [second + "." + e for e, _ in second_only]) varlist2 = ", ".join([first + "." + e for e, _ in first_only] + [second + "." + e for e, _ in join_variables] + [second + "." + e for e, _ in second_only]) join_condition = " AND ".join([ "".join([first, ".", e, " = ", second, ".", e]) for e, _ in join_variables ]) context.cursor.execute( sql.format(dest=name, src1=first, src2=second, varlist1=varlist1, varlist2=varlist2, join_condition=join_condition)) context.conn.commit() return name
def test_notification(admin_user, specific_user): AddNotification(make_bind_data( variables={"priority": "priority"}, constants={ "message": "Hi {{ name }}!", "message_identifier": "hi mom", "url": "http://burymewithmymoney.com/", "recipient_type": (RecipientType.SPECIFIC_USER if specific_user else RecipientType.ADMINS), "recipient": (admin_user if specific_user else None), "priority": Priority.CRITICAL } )).execute(Context.from_variables(name="Justin Case", shop=factories.get_default_shop())) notif = Notification.objects.last() assert isinstance(notif, Notification) if specific_user: assert notif.recipient == admin_user assert Notification.objects.unread_for_user(admin_user).get(pk=notif.pk) assert notif.identifier == "hi mom" assert notif.message == "Hi Justin Case!" assert notif.priority == Priority.CRITICAL assert notif.url == "http://burymewithmymoney.com/" with pytest.raises(ValueError): notif.url = "http://www.theuselessweb.com/" assert not notif.is_read notif.mark_read(admin_user) # Once, for setting notif.mark_read(admin_user) # Twice, for branch checking assert notif.marked_read_by == admin_user assert very_recently(notif.marked_read_on)
def diff(first: str, second: str, first_fv: {str, str}, second_fv: {str, str}, context: Context) -> str: if not second_fv.issubset(first_fv): raise RuntimeError("For A \\ B, B has to be subset of A") join_variables = second_fv sql = """CREATE TABLE {dest} AS SELECT {varlist0} FROM (SELECT {varlist1} FROM {src1} EXCEPT SELECT {varlist2} FROM {src2}) AS Some_table INNER JOIN {src1} ON {join_condition} """ name = context.get_table_name() varlist0 = ", ".join([first + "." + e for e, _ in first_fv]) varlist1 = ", ".join([first + "." + e for e, _ in join_variables]) varlist2 = ", ".join([second + "." + e for e, _ in join_variables]) join_condition = " AND ".join( [first + "." + e + " = Some_table." + e for e, _ in join_variables]) #print("diff: ", end="") #print(sql.format(dest=name, varlist0=varlist0, varlist1=varlist1, varlist2=varlist2, src1=first, # src2=second, join_condition=join_condition)) context.cursor.execute( sql.format(dest=name, varlist0=varlist0, varlist1=varlist1, varlist2=varlist2, src1=first, src2=second, join_condition=join_condition)) context.conn.commit() return name
def test_log_entry_on_unloggable_object(target_obj): event = get_initialized_test_event() event.variable_values["order"] = target_obj # invalidate log target _before_ creating context ctx = Context.from_event(event, shop=factories.get_default_shop()) n_log_entries = ctx.log_entry_queryset.count() ctx.add_log_entry_on_log_target("blap", "blorr") assert ctx.log_entry_queryset.count() == n_log_entries # couldn't add :(
def render(self, me, configuration, template, obj_space, cap_space, shmem, **kwargs): context = Context.new_context(me, configuration, obj_space, cap_space, shmem, **kwargs) t = self.env.get_template(template) return t.render(context)
def KJones (self,array=None,dir0=None): """makes and returns a series of Kjones (phase shift) nodes for this direction, given a reference direction dir0, or using the global phase center if not supplied. Return value is an under-qualified node K, which should be qualified with a station index. If 'smearing' is True, then uses an alternative implementation of K -- makes a separate node K('arg'), which holds the complex argument of K. This argument node can be used to compute smearing factors. """; # if direction is same, K is identity for all stations if self is dir0: Kj = self.ns.K << 1; return lambda p: Kj; else: Kj = self.ns.K; if dir0: Kj = Kj.qadd(dir0.radec()); array = Context.get_array(array); stations = array.stations(); Kjarg = Kj('arg'); if not Kj(stations[0]).initialized(): if Kjarg(stations[0]).initialized(): for p in stations: Kj(p) << Meq.VisPhaseShift(Kjarg(p)); else: lmn_1 = self.lmn_1(dir0); uvw = array.uvw(dir0); for p in stations: Kj(p) << Meq.VisPhaseShift(lmn=lmn_1,uvw=uvw(p)); return Kj;
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 KJones(self, array=None, dir0=None): """makes and returns a series of Kjones (phase shift) nodes for this direction, given a reference direction dir0, or using the global phase center if not supplied. Return value is an under-qualified node K, which should be qualified with a station index. If 'smearing' is True, then uses an alternative implementation of K -- makes a separate node K('arg'), which holds the complex argument of K. This argument node can be used to compute smearing factors. """ # if direction is same, K is identity for all stations if self is dir0: Kj = self.ns.K << 1 return lambda p: Kj else: Kj = self.ns.K if dir0: Kj = Kj.qadd(dir0.radec()) array = Context.get_array(array) stations = array.stations() Kjarg = Kj('arg') if not Kj(stations[0]).initialized(): if Kjarg(stations[0]).initialized(): for p in stations: Kj(p) << Meq.VisPhaseShift(Kjarg(p)) else: lmn_1 = self.lmn_1(dir0) uvw = array.uvw(dir0) for p in stations: Kj(p) << Meq.VisPhaseShift(lmn=lmn_1, uvw=uvw(p)) return Kj
def coherency (self,array=None,observation=None,nodes=None,**kw): """Returns the Gaussian coherency (at l=m=0). We'll use PSVTensor to generate this here. """; coherency = nodes or self.ns.coh; array = Context.get_array(array); uvw = Meow.Context.array.uvw_ifr(); # build up tensors for source if not self.ns.BT.initialized(): B_static = self.brightness_static(); if B_static: self.ns.BT << Meq.Constant(B_static,dims=[1,2,2]); else: self.ns.BT << Meq.Composer(self.brightness(),dims=[1,2,2]); # lmn tensor is 0,0,1, since this tree is supposed to predict coherency at phase center self.ns.lmnT ** Meq.Constant([0,0,1],dims=[1,3]); # shape tensor if not self.ns.shapeT.initialized(): shape_static = self.shape_static(); if shape_static: self.ns.shapeT << Meq.Constant(shape_static,dims=[1,3]); else: self.ns.shapeT << Meq.Composer(dims=[1,3],*self.shape()); # coherency nodes if not coherency(*array.ifrs()[0]).initialized(): for p,q in array.ifrs(): coherency(p,q) << Meq.PSVTensor(self.ns.lmnT,self.ns.BT,uvw(p,q),self.ns.shapeT); return coherency;
def __init__(self, old_context, filename, reader, restart=1): url = old_context.get_url() headers = old_context.get_headers() self.app = old_context.browser.app self.root = tktools.make_toplevel( old_context.browser.master, class_="GrailTransfer") self.root.protocol("WM_DELETE_WINDOW", self.stop) import Context self.context = Context.SimpleContext(self, self) self.context._url = self.context._baseurl = url reader.last_context = self.context self.__filename = filename self.__reader = reader self.__save_file = reader.save_file reader.save_file = self if filename: self.root.title("Grail: Downloading " + os.path.basename(filename)) else: self.root.title("Grail Download") self.root.iconname("Download") # self.content_length = None if headers.has_key('content-length'): self.content_length = int(headers['content-length']) self.create_widgets(url, filename, self.content_length) # if restart: reader.restart(reader.url) reader.bufsize = 8096 tktools.set_transient(self.root, old_context.browser.master) history = old_context.app.global_history if not history.inhistory_p(url): history.remember_url(url) self.root.update_idletasks()
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 render(self, me, assembly, template, obj_space, cap_space, shmem, **kwargs): context = Context.new_context(me, assembly, obj_space, cap_space, shmem, **kwargs) t = self.env.get_template(template) return t.render(context)
def coherency(self, array=None, observation=None, nodes=None, **kw): """Returns the Gaussian coherency (at l=m=0). We'll use PSVTensor to generate this here. """ coherency = nodes or self.ns.coh array = Context.get_array(array) uvw = Meow.Context.array.uvw_ifr() # build up tensors for source if not self.ns.BT.initialized(): B_static = self.brightness_static() if B_static: self.ns.BT << Meq.Constant(B_static, dims=[1, 2, 2]) else: self.ns.BT << Meq.Composer(self.brightness(), dims=[1, 2, 2]) # lmn tensor is 0,0,1, since this tree is supposed to predict coherency at phase center self.ns.lmnT ** Meq.Constant([0, 0, 1], dims=[1, 3]) # shape tensor if not self.ns.shapeT.initialized(): shape_static = self.shape_static() if shape_static: self.ns.shapeT << Meq.Constant(shape_static, dims=[1, 3]) else: self.ns.shapeT << Meq.Composer(dims=[1, 3], *self.shape()) # coherency nodes if not coherency(*array.ifrs()[0]).initialized(): for p, q in array.ifrs(): coherency(p, q) << Meq.PSVTensor(self.ns.lmnT, self.ns.BT, uvw(p, q), self.ns.shapeT) return coherency
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 OnUpdate(self, fTime): _ctx = Context.GetInstance() if Aurora.app().StartLogin(_ctx.userName, _ctx.password): self.stateMachine.ChangeState(self.stateMachine.ES_Running) else: print("Login Failed") self.stateMachine.ChangeState(self.stateMachine.ES_Failed)
def ResetScreen(self): _ctx = Context.GetInstance() flag = 0 if _ctx.fullscreen: flag = pygame.FULLSCREEN self.screen = pygame.display.set_mode((_ctx.width, _ctx.height), flag, 32)
def OnInit(): random.seed() print(sys.path) _ctx = Context.GetInstance() _ctx.stateMachine = StateMachine() _ctx.viewer = Viewer.GetInstance() _ctx.viewer.Init() _ctx.LoadImages()
def test_basic_exec(): script = get_test_script() # `en` is not in the conditions context = Context.from_variables(order_language="en") script.execute(context) assert not context.get("success") # `fi` is matched by the first condition and cond_op is 'or' context = Context.from_variables(order_language="fi") script.execute(context) assert context.get("success") # `ja` is matched by the other condition, and cond_op is 'or' context = Context.from_variables(order_language="ja") script.execute(context) assert context.get("success")
def empty_table(fv: {str, str}, context: Context) -> str: sql = """CREATE TABLE {dest} ( {varlist} );""" name = context.get_table_name() var_list = ",\n".join([e + " " + i for e, i in fv]) context.cursor.execute(sql.format(dest=name, varlist=var_list)) context.conn.commit() return name
def projection(first: str, fv: {str, str}, context: Context) -> str: sql = """CREATE TABLE {dest} AS SELECT {varlist} FROM {src} """ name = context.get_table_name() varlist = ", ".join([first + "." + e for e, _ in fv]) context.cursor.execute(sql.format(dest=name, varlist=varlist, src=first)) context.conn.commit() return name
def cross_join(first: str, second: str, context: Context) -> str: sql = """CREATE TABLE {dest} AS SELECT * FROM {src1} CROSS JOIN {src2} """ name = context.get_table_name() context.cursor.execute(sql.format(dest=name, src1=first, src2=second)) context.conn.commit() return name
def test_none_condop(): step = Step(cond_op=StepConditionOperator.NONE, conditions=[ NonEmpty({"v": {"variable": "a"}}), NonEmpty({"v": {"variable": "b"}}), ], actions=[SetDebugFlag({})]) context = Context.from_variables(a=False, b=False) step.execute(context) assert context.get("debug")
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 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 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 table_copy(first: str, context: Context) -> str: sql = """CREATE TABLE {dest} AS SELECT * FROM {src}; """ name = context.get_table_name() context.cursor.execute(sql.format(dest=name, src=first)) context.conn.commit() return name
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 test_disabled_steps(): script = get_test_script() steps = script.get_steps() steps[0].enabled = False script.set_steps(steps) # Disabled steps don't run context = Context.from_variables() script.execute(context) assert not context.get("success")
def Init(self): pygame.init() self.ResetScreen() pygame.display.set_caption('Aurora Client Viewer') self.background_position = [0, 0] _ctx = Context.GetInstance() self.background_image = pygame.image.load( _ctx.rootDir + "images/beijing.jpg").convert() self.entities = {}
def table_with_condition(table: Table, condition: Condition, context: Context) -> Table: sql = """CREATE TABLE {dest} AS SELECT * FROM {src} WHERE {condition} """ name = context.get_table_name() context.cursor.execute( sql.format(dest=name, src=table.name, condition=condition.to_str())) context.conn.commit() return Table(name, table.fv, table.is_negative)
def lm (self,dir0=None): """Returns an LM 2-vector node for this direction. All args are as per lmn()."""; dir0 = Context.get_dir0(dir0); lmnst = self.lmn_static(dir0); if lmnst: lm = self.ns.lm(*dir0._quals,**dir0._kwquals); return lm ** Meq.Constant(value=Timba.array.array(lmnst[0:2])); else: return self._lmn_component('lm',dir0,[0,1]);
def lm(self, dir0=None): """Returns an LM 2-vector node for this direction. All args are as per lmn().""" dir0 = Context.get_dir0(dir0) lmnst = self.lmn_static(dir0) if lmnst: lm = self.ns.lm(*dir0._quals, **dir0._kwquals) return lm**Meq.Constant(value=Timba.array.array(lmnst[0:2])) else: return self._lmn_component('lm', dir0, [0, 1])
def test_template_in_action(): ac = ATestTemplateUsingAction(data={"template_data": TEST_TEMPLATE_DATA}) context = Context.from_variables(name=u"Sir Test") template = ac.get_template(context) test_template_render(template) japanese_render = ac.get_template_values(context, ("ja",)) name = template.context.get("name") assert name.upper() in japanese_render["body"] ac = ATestUnilingualTemplateUsingAction(data={"template_data": TEST_UNI_TEMPLATE_DATA}) assert name in ac.get_template_values(context)["subject"]
def test_log_entries(): event = get_initialized_test_event() ctx = Context.from_event(event, shop=factories.get_default_shop()) order = ctx.get("order") n_log_entries = ctx.log_entry_queryset.count() ctx.add_log_entry_on_log_target("blap", "blorr") order.add_log_entry("blep") assert ctx.log_entry_queryset.count() == n_log_entries + 2 # they got added assert order.log_entries.last().message == "blep" # it's what we added assert ctx.log_entry_queryset.last().message == "blep" # from this perspective too
def sqrt_visibilities (self,array=None,observation=None,nodes=None): self.using_station_decomposition = True; observation = Context.get_observation(observation); array = Context.get_array(array); if nodes is None: nodes = self.ns.sqrt_visibility.qadd(observation.radec0()); stations = array.stations(); if nodes(stations[0]).initialized(): return nodes; # create decomposition nodes sqrtcoh = self.sqrt_coherency(observation); # get K jones per station if self.direction is observation.phase_center: for p in array.stations(): nodes(p) << Meq.Identity(sqrtcoh); # else apply KJones else: Kj = self.direction.KJones(array=array,dir0=observation.phase_center); for p in array.stations(): nodes(p) << Meq.MatrixMultiply(Kj(p),sqrtcoh); return nodes;
def uv (self,dir0,*quals): """returns station UV node(s) for a given phase centre direction, or using the global phase center if None is given. If a station is supplied, returns UV node for that station"""; dir0 = Context.get_dir0(dir0); radec0 = dir0.radec(); uv = self.ns.uv.qadd(radec0); if not uv(self.stations()[0]).initialized(): uvw = self.uvw(dir0); for station in self.stations(): uv(station) << Meq.Selector(uvw(station),index=(0,1),multi=True); return uv(*quals);
def uv_ifr (self,dir0,*quals): """returns interferometer UV node(s) for a given phase centre direction. or using the global phase center if None is given. If an IFR is supplied, returns UVW node for that IFR"""; dir0 = Context.get_dir0(dir0); radec0 = dir0.radec(); uv_ifr = self.ns.uv_ifr.qadd(radec0); if not uv_ifr(*(self.ifrs()[0])).initialized(): uvw_ifr = self.uvw_ifr(dir0); for ifr in self.ifrs(): uv_ifr(*ifr) << Meq.Selector(uvw_ifr(*ifr),index=(0,1),multi=True); return uv_ifr(*quals);
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 lmn_1 (self,dir0=None): """Returns L,M,N-1 3-vector node for this direction. All args are as per lmn()."""; dir0 = Context.get_dir0(dir0); lmn_1 = self.ns.lmn_minus1(*dir0._quals,**dir0._kwquals); if not lmn_1.initialized(): lmnst = self.lmn_static(dir0); if lmnst: lmn_1 << Meq.Constant(value=Timba.array.array((lmnst[0],lmnst[1],lmnst[2]-1))); else: lmn = self.lmn(dir0); lmn_1 << Meq.Paster(lmn,self.n(dir0)-1,index=2); return lmn_1;
def setUp(self): self.manager = ContextManager() self.context = Context(self.manager) self.defaultContext = DefaultContext(self.manager) self.declarationContext = DeclarationContext(self.manager) self.expressionContext = ExpressionContext(self.manager) self.flowControlContext = FlowControlContext(self.manager) self.contexts = [self.declarationContext, self.expressionContext, self.flowControlContext, self.defaultContext] self.manager.addContext('Default', self.defaultContext) self.manager.addContext('Declaration', self.declarationContext) self.manager.addContext('Expression', self.expressionContext) self.manager.addContext('FlowControl', self.flowControlContext) self.manager.setCurrentContexts(self.contexts)
def setUp(self): self.manager = ContextManager() self.context = Context(self.manager) self.manager.setCurrentContexts([self.context]) self.context.addOperator('(') self.context.addOperator(')') self.context.addOperator('.') self.context.addOperator('+') self.context.addOperator('++') self.context.addOperator('-') self.context.addOperator('*') self.context.addOperator('=') self.context.addOperator('==') self.context.addOperator('&&')
def ssl_verify_callback(ssl_ctx_ptr, x509_ptr, errnum, errdepth, ok): # Deprecated ssl_ctx = Context.map()[long(ssl_ctx_ptr)] if errnum in unknown_issuer: if ssl_ctx.get_allow_unknown_ca(): sys.stderr.write("policy: %s: permitted...\n" % (m2.x509_get_verify_error(errnum))) sys.stderr.flush() ok = 1 # CRL checking goes here... if ok: if ssl_ctx.get_verify_depth() >= errdepth: ok = 1 else: ok = 0 return ok
def uvw_ifr(self, dir0=None, *quals): """returns interferometer UVW node(s) for a given phase centre direction, or using the global phase center if None is given. If an IFR is supplied, returns UVW node for that IFR""" dir0 = Context.get_dir0(dir0) radec0 = dir0.radec() uvw_ifr = self.ns.uvw_ifr.qadd(radec0) if not uvw_ifr(*(self.ifrs()[0])).initialized(): uvw = self.uvw(dir0) # If preferring baseline UVWs, then self.uvw() will now have initialized uvw_ifr for us. # So check once more, and make our own via subtraction if needed if not uvw_ifr(*(self.ifrs()[0])).initialized(): for sta1, sta2 in self.ifrs(): uvw_ifr(sta1, sta2) << uvw(sta2) - uvw(sta1) return uvw_ifr(*quals)
def __init__(self,ns,name,l,m,dir0=None, static=True,quals=[],kwquals={}): Parameterization.__init__(self,ns,name, quals=quals,kwquals=kwquals); self._dir0 = Context.get_dir0(dir0); self._add_parm('l',l,tags="direction"); self._add_parm('m',m,tags="direction"); self.static = static; if static and self._is_constant('l') and self._is_constant('m'): n = math.sqrt(1-l*l-m*m); self._add_parm('n',n,tags="direction"); self.static = l,m,n; self.static_radec = {}; else: self.static = None;
def runTest(self,test,stdout): Context.resetInstanceNumbers() try: testModule = __import__(test,fromlist=test) testCase = getattr(testModule,'case_'+test) config = Configuration(self._symbolMap,useNuma=False) case = testCase(config,stdout) case.fillConfiguration(self._symbolMap) try: case.prepare(test) case.runTest() except Exception as e: if self.args['verbose']: traceback.print_exc(file=stdout) raw_input("Press enter to continue") else: raise e finally: case.destroy() except Exception as e: traceback.print_exc(file=stdout) return "\033[1;37;41m FAILED \033[0m "+type(e).__name__+": "+str(e) return "\033[1;37;42m Passed \033[0m"
def lmn_static (self,dir0=None): """Returns static LMN tuple, given a reference direction dir0, or using the global phase center if not supplied. Both this direction and the reference direction must be static, otherwise None is returned. """; dir0 = Context.get_dir0(dir0); if not self.static or not dir0.static: return None; ra0,dec0 = dir0.radec_static(); # see if we have already computed this lmn lmn = self.static_lmn.get((ra0,dec0),None); if lmn is None: ra,dec = self.radec_static(); lmn = self.static_lmn[(ra0,dec0)] = radec_to_lmn(ra,dec,ra0,dec0); return lmn;
def radec_static (self,dir0=None): """Returns static RA-Dec tuple, given a reference direction dir0, or using the global phase center if not supplied. Both this direction and the reference direction must be static, otherwise None is returned. BUG here: reference direction is ignored """; dir0 = Context.get_dir0(dir0); if not self.static or not dir0.radec_static(): return None; ra0,dec0 = dir0.radec_static(); l,m,n = self.static; # see if we have already computed this lmn radec = self.static_radec.get((ra0,dec0),None); if radec is None: radec = self.static_radec[(ra0,dec0)] = lm_to_radec(l,m,ra0,dec0); return radec;
def smear_factor (self,array=None,dir0=None): """Returns smearing factor associated with this direction. Returns something that must be qualified with p,q, or can be None if there's no smearing. By default, uses the Direction implementation.""" if self is dir0: return None; else: Kj = self.ns.K; if dir0: Kj = Kj.qadd(dir0.radec()); Kjsm = Kj('smear'); array = Context.get_array(array); ifrs = array.ifrs(); if not Kjsm(*ifrs[0]).initialized(): Kjarg = self._KJonesArg(Kj,array,dir0); for p,q in ifrs: Kjsm(p,q) << Meq.TFSmearFactor(Kjarg(p),Kjarg(q)); return Kjsm;
def lmn (self,dir0=None): """Returns LMN 3-vector node for this direction, given a reference direction dir0, or using the global phase center if not supplied. Qualifiers from dir0 are added in. All other lmn-related methods below call on this one to get the basic lmn 3-vector. """; dir0 = Context.get_dir0(dir0); lmn = self.ns.lmn(*dir0._quals,**dir0._kwquals); if not lmn.initialized(): if self is dir0: lmn << Meq.Constant(value=Timba.array.array((0.,0.,1.))); else: lmnst = self.lmn_static(dir0); if lmnst: lmn << Meq.Constant(value=Timba.array.array(lmnst)); else: lmn << Meq.LMN(radec_0=dir0.radec(),radec=self.radec()); return lmn;
def uvw_ifr (self,dir0=None,*quals): """returns interferometer UVW node(s) for a given phase centre direction, or using the global phase center if None is given. If an IFR is supplied, returns UVW node for that IFR"""; dir0 = Context.get_dir0(dir0) radec0 = dir0.radec() uvw_ifr = self.ns.uvw_ifr.qadd(radec0) if not uvw_ifr(*(self.ifrs()[0])).initialized(): if self._ms_uvw: for (ip,p),(iq,q) in self.ifr_index(): uvw_ifr(p,q) << Meq.Spigot(station_1_index=ip,station_2_index=iq,input_col='UVW',include_deriv=self._include_uvw_deriv) else: uvw = self.uvw(dir0); # If preferring baseline UVWs, then self.uvw() will now have initialized uvw_ifr for us. # So check once more, and make our own via subtraction if needed if not uvw_ifr(*(self.ifrs()[0])).initialized(): for sta1,sta2 in self.ifrs(): uvw_ifr(sta1,sta2) << uvw(sta2) - uvw(sta1) return uvw_ifr(*quals);
class TestLexer(unittest.TestCase): def setUp(self): self.manager = ContextManager() self.context = Context(self.manager) self.manager.setCurrentContexts([self.context]) self.context.addOperator('(') self.context.addOperator(')') self.context.addOperator('.') self.context.addOperator('+') self.context.addOperator('++') self.context.addOperator('-') self.context.addOperator('*') self.context.addOperator('=') self.context.addOperator('==') self.context.addOperator('&&') def testAdvance(self): lexer = LexerStateMachine(""" hi krizz 987 456 """, self.context) self.assertEqual(lexer.peep().data[0], 'hi') self.assertEqual(lexer.advance().data[0], 'krizz') self.assertEqual(lexer.advance().data[0], 987) self.assertEqual(lexer.advance().data[0], 456) def test_peep_should_raise_error_when_the_returned_symbol_is_not_equal_to_the_expected_symbol(self): lexer = LexerStateMachine('(', self.context) self.assertRaises(SyntaxError, lexer.peep, '*') def test_peep_should_not_raise_error_when_the_returned_symbol_is_equal_to_the_expected_symbol(self): lexer = LexerStateMachine('(', self.context) lexer.peep() #self.assertRaises(None, lexer.peep, '(') def test_peep_should_not_raise_error_when_the_expected_symbol_is_none(self): lexer = LexerStateMachine('(', self.context) lexer.peep() def test_advance_should_raise_error_when_the_returned_token_is_not_equal_to_the_expected_token(self): lexer = LexerStateMachine('( )', self.context) self.assertRaises(SyntaxError, lexer.advance, '*') def test_advance_should_not_raise_error_when_the_returned_token_is_equal_to_the_expected_token(self): lexer = LexerStateMachine('( )', self.context) lexer.advance() #self.assertRaises(None, lexer.advance, ')') def test_advance_should_not_raise_error_when_expected_symbol_is_none(self): lexer = LexerStateMachine('( )', self.context) lexer.advance() def test_the_lexer_will_contain_the_first_token_in_the_class(self): lexer = LexerStateMachine('12', self.context) testToken = lexer.currentToken self.assertEqual(testToken.data[0], 12) self.assertEqual(testToken.id, '(literal)') def test_peep_will_return_the_currentToken_inside_the_lexer(self): lexer = LexerStateMachine('151915354', self.context) testToken = lexer.peep() self.assertEqual(testToken.data[0], 151915354) self.assertEqual(testToken.id, '(literal)') testToken1 = lexer.peep() self.assertEqual(testToken1.data[0], 151915354) self.assertEqual(testToken1.id, '(literal)') def test_the_lexer_will_make_a_token_for_the_number_with_e_and_E(self): lexer = LexerStateMachine('12E10', self.context) testToken = lexer.peep() self.assertEqual(testToken.data[0],120000000000) self.assertEqual(testToken.id, '(floating)') def test_advance_will_get_the_second_word_in_the_string(self): lexer = LexerStateMachine('Price 12E10', self.context) testToken = lexer.peep() self.assertEqual(testToken.data[0], 'Price') self.assertEqual(testToken.id, '(identifier)') testToken = lexer.advance() self.assertEqual(testToken.data[0],120000000000) self.assertEqual(testToken.id, '(floating)') def test_advance_will_get_the_number_with_eE_power_to_positive(self): lexer = LexerStateMachine('Price 12E+10', self.context) testToken = lexer.peep() self.assertEqual(testToken.data[0], 'Price') self.assertEqual(testToken.id, '(identifier)') testToken = lexer.advance() self.assertEqual(testToken.data[0],120000000000) self.assertEqual(testToken.id, '(floating)') def test_advance_will_get_the_number_with_eE_power_to_negative(self): lexer = LexerStateMachine('Price 12E-10', self.context) testToken = lexer.peep() self.assertEqual(testToken.data[0], 'Price') self.assertEqual(testToken.id, '(identifier)') testToken = lexer.advance() self.assertAlmostEqual(testToken.data[0], 1.2e-09) self.assertEqual(testToken.id, '(floating)') def test_advance_will_throw_exception_for_two_notation_is_included_in_the_e_floating_point(self): lexer = LexerStateMachine('Price 12E--10', self.context) try: lexer.advance() raise SyntaxError("Exception test failed") except SyntaxError as e: self.assertEqual("Error[1][11]:Unexpected symbol \"-\" been found after -"+'\n'+ 'Price 12E--10'+'\n'+ ' ^', e.msg) def test_advance_will_throw_exception_for_plus_plus_appear_inside_the_floating_point_e(self): lexer = LexerStateMachine('Price 12E++10', self.context) try: lexer.advance() raise SyntaxError("Exception test failed") except SyntaxError as e: self.assertEqual("Error[1][11]:Unexpected symbol \"+\" been found after +"+'\n'+ 'Price 12E++10'+'\n'+ ' ^', e.msg) def test_advance_will_throw_exception_for_the_error_That_occurred_after_the_first_line(self): lexer = LexerStateMachine("Price"+"\n"+ "12E++10", self.context) try: lexer.advance() raise SyntaxError("Exception test failed") except SyntaxError as e: self.assertEqual("Error[2][5]:Unexpected symbol \"+\" been found after +"+'\n'+ '12E++10'+'\n'+ ' ^', e.msg) lexer = LexerStateMachine("Price"+"\n"+ "IsRising"+"\n"+ "12E++10", self.context) try: lexer.advance() lexer.advance() raise SyntaxError("Exception test failed") except SyntaxError as e: self.assertEqual("Error[3][5]:Unexpected symbol \"+\" been found after +"+'\n'+ '12E++10'+'\n'+ ' ^', e.msg) def test_advance_will_throw_exception_for_invalid_number_after_minus_sign(self): lexer = LexerStateMachine('Price 12E-/10', self.context) try: lexer.advance() raise SyntaxError("Exception test failed") except SyntaxError as e: self.assertEqual("Error[1][11]:Unexpected symbol \"/\" been found after -"+'\n'+ 'Price 12E-/10'+'\n'+ ' ^', e.msg) def test_advance_will_throw_exception_for_unknown_symbol_inside_the_floating_point_e(self): lexer = LexerStateMachine('Price 12E*10', self.context) try: lexer.advance() raise SyntaxError("Exception test failed") except SyntaxError as e: self.assertEqual("Error[1][10]:Expecting a positive or negative number after E/e."+'\n'+ 'Price 12E*10'+'\n'+ ' ^', e.msg) def test_advance_will_throw_exception_if_no_number_is_added_after_the_e_or_E(self): lexer = LexerStateMachine('Dummy 12E', self.context) try: lexer.advance() raise SyntaxError("Exception test failed") except SyntaxError as e: self.assertEqual("Error[1][10]:Expecting a positive or negative number after E/e."+'\n'+ 'Dummy 12E'+'\n'+ ' ^', e.msg) def test_advance_will_throw_exception_if_alphabet_added_after_the_E(self): lexer = LexerStateMachine('Dummy 12EA', self.context) try: lexer.advance() raise SyntaxError("Exception test failed") except SyntaxError as e: self.assertEqual("Error[1][10]:Expecting a positive or negative number after E/e."+'\n'+ 'Dummy 12EA'+'\n'+ ' ^', e.msg) def test_advance_will_accept_E_and_e_as_floating_point(self): lexer = LexerStateMachine('Dummy 12e-10', self.context) testToken = lexer.advance() self.assertAlmostEqual(testToken.data[0], 1.2e-09) self.assertEqual(testToken.id, '(floating)') def test_advance_will_throw_the_same_error_if_e_is_replace_E_in_the_floating_point(self): lexer = LexerStateMachine('Price 12e--10', self.context) try: lexer.advance() raise SyntaxError("Exception test failed") except SyntaxError as e: self.assertEqual("Error[1][11]:Unexpected symbol \"-\" been found after -"+'\n'+ 'Price 12e--10'+'\n'+ ' ^', e.msg) lexer = LexerStateMachine('Price 12e++10', self.context) try: lexer.advance() raise SyntaxError("Exception test failed") except SyntaxError as e: self.assertEqual("Error[1][11]:Unexpected symbol \"+\" been found after +"+'\n'+ 'Price 12e++10'+'\n'+ ' ^', e.msg) lexer = LexerStateMachine('Price 12e*10', self.context) try: lexer.advance() raise SyntaxError("Exception test failed") except SyntaxError as e: self.assertEqual("Error[1][10]:Expecting a positive or negative number after E/e."+'\n'+ 'Price 12e*10'+'\n'+ ' ^', e.msg) lexer = LexerStateMachine('Dummy 12e', self.context) try: lexer.advance() raise SyntaxError("Exception test failed") except SyntaxError as e: self.assertEqual("Error[1][10]:Expecting a positive or negative number after E/e."+'\n'+ 'Dummy 12e'+'\n'+ ' ^', e.msg) lexer = LexerStateMachine('Dummy 12eA', self.context) try: lexer.advance() raise SyntaxError("Exception test failed") except SyntaxError as e: self.assertEqual("Error[1][10]:Expecting a positive or negative number after E/e."+'\n'+ 'Dummy 12eA'+'\n'+ ' ^', e.msg) def test_advance_will_return_the_actual_value_if_the_floating_point_with_bigger_number_on_both_sides(self): lexer = LexerStateMachine('Dummy 13123e151', self.context) testToken = lexer.advance() self.assertAlmostEqual(testToken.data[0], 1.3123e+155) self.assertEqual(testToken.id, '(floating)') def test_advance_will_return_a_floating_point_when_a_number_with_dot_is_found(self): lexer = LexerStateMachine('Dummy 123.456', self.context) testToken = lexer.advance() self.assertAlmostEqual(testToken.data[0], 123.456) self.assertEqual(testToken.id, '(floating)') def test_advance_will_return_a_floating_point_when_the_number_is_start_with_a_dot(self): lexer = LexerStateMachine('Dummy .456', self.context) testToken = lexer.advance() self.assertAlmostEqual(testToken.data[0], 0.456) self.assertEqual(testToken.id, '(floating)') def test_advance_will_return_a_floating_point_if_before_the_dot_will_have_a_zero(self): lexer = LexerStateMachine('Dummy 0.123456', self.context) testToken = lexer.advance() self.assertAlmostEqual(testToken.data[0], 0.123456) self.assertEqual(testToken.id, '(floating)') def test_advance_will_return_a_floating_point_if_the_number_is_end_with_a_dot(self): lexer = LexerStateMachine('Dummy 20.', self.context) testToken = lexer.advance() self.assertAlmostEqual(testToken.data[0], 20.0) self.assertEqual(testToken.id, '(floating)') def test_advance_will_return_a_floating_point_if_the_floating_point_is_in_the_begining_of_the_string(self): lexer = LexerStateMachine('.25', self.context) testToken = lexer.currentToken self.assertAlmostEqual(testToken.data[0], 0.25) self.assertEqual(testToken.id, '(floating)') def test_advance_will_return_a_floating_point_if_the_number_is_greater_than_1(self): lexer = LexerStateMachine('123456.25', self.context) testToken = lexer.currentToken self.assertAlmostEqual(testToken.data[0], 123456.25) self.assertEqual(testToken.id, '(floating)') def test_advance_will_throw_exception_when_there_is_only_a_dot_in_the_statement(self): lexer = LexerStateMachine('Dummy .', self.context) try: lexer.advance() raise SyntaxError("Exception test failed") except SyntaxError as e: self.assertEqual("Error[1][8]:Expecting number after ."+'\n'+ 'Dummy .'+'\n'+ ' ^', e.msg) def test_advance_will_produce_a_token_which_carry_the_dot(self): lexer = LexerStateMachine('Dummy.', self.context) self.assertEqual(lexer.currentToken.id,'(identifier)') self.assertEqual(lexer.currentToken.data[0], 'Dummy') testToken = lexer.advance() self.assertEqual(testToken.id, '.') def test_advance_will_produce_a_token_for_dot_after_underscore(self): lexer = LexerStateMachine('Dummy_.', self.context) testToken = lexer.advance() self.assertEqual(testToken.id, '.') def test_advance_will_produce_a_token_for_dot_after_number(self): lexer = LexerStateMachine('Dummy78.', self.context) self.assertEqual(lexer.currentToken.id,'(identifier)') self.assertEqual(lexer.currentToken.data[0], 'Dummy78') testToken = lexer.advance() self.assertEqual(testToken.id, '.') def test_advance_will_throw_an_error_if_the_expression_contain_dot_only(self): lexer = LexerStateMachine('Dummy .', self.context) try: lexer.advance() raise SyntaxError("Exception test failed") except SyntaxError as e: self.assertEqual("Error[1][8]:Expecting number after ."+'\n'+ 'Dummy .'+'\n'+ ' ^', e.msg) def test_advance_will_throw_exception_if_the_dot_is_being_in_the_first_location(self): try: lexer = LexerStateMachine('.', self.context) raise SyntaxError("Exception test failed") except SyntaxError as e: self.assertEqual("Error[1][2]:Expecting number after ."+'\n'+ '.'+'\n'+ ' ^', e.msg) def test_advance_will_generate_a_token_for_hexadecimal_number(self): lexer = LexerStateMachine('Dummy 0X456', self.context) testToken = lexer.advance() self.assertAlmostEqual(testToken.data[0], 1110) self.assertEqual(testToken.id, '(literal)') def test_advance_will_generate_a_token_for_hexadecimal_that_contain_alphabet(self): lexer = LexerStateMachine('Dummy 0XABCDEF', self.context) testToken = lexer.advance() self.assertAlmostEqual(testToken.data[0], 11259375) self.assertEqual(testToken.id, '(literal)') def test_advance_will_generate_a_token_for_hexadecimal_that_contain_lower_alphabet(self): lexer = LexerStateMachine('Dummy 0Xfedcba', self.context) testToken = lexer.advance() self.assertAlmostEqual(testToken.data[0], 16702650) self.assertEqual(testToken.id, '(literal)') def test_advance_will_generate_a_token_for_hexamdecimal_that_mixed_with_number_and_valid_alphabet(self): lexer = LexerStateMachine('Dummy 0X1a2b3c4d5e6f', self.context) testToken = lexer.advance() self.assertAlmostEqual(testToken.data[0], 28772997619311) self.assertEqual(testToken.id, '(literal)') def test_advacne_will_generate_a_token_for_the_expression_with_0x(self): lexer = LexerStateMachine('Dummy 0x1a2b3c4d5e6f', self.context) testToken = lexer.advance() self.assertAlmostEqual(testToken.data[0], 28772997619311) self.assertEqual(testToken.id, '(literal)') def test_advance_will_generate_exception_for_no_number_or_valid_alphabet_appeared_after_the_0X(self): lexer = LexerStateMachine('Dummy 0X', self.context) try: lexer.advance() raise SyntaxError("Exception test failed") except SyntaxError as e: self.assertEqual("Error[1][9]:Expecting hex number after 0X"+'\n'+ 'Dummy 0X'+'\n'+ ' ^', e.msg) lexer = LexerStateMachine('Dummy 0x', self.context) try: lexer.advance() raise SyntaxError("Exception test failed") except SyntaxError as e: self.assertEqual("Error[1][9]:Expecting hex number after 0x"+'\n'+ 'Dummy 0x'+'\n'+ ' ^', e.msg) lexer = LexerStateMachine('Dummy 0xghi', self.context) try: lexer.advance() raise SyntaxError("Exception test failed") except SyntaxError as e: self.assertEqual("Error[1][9]:Expecting hex number after 0x"+'\n'+ 'Dummy 0xghi'+'\n'+ ' ^', e.msg) lexer = LexerStateMachine('Dummy 0xz', self.context) try: lexer.advance() raise SyntaxError("Exception test failed") except SyntaxError as e: self.assertEqual("Error[1][9]:Expecting hex number after 0x"+'\n'+ 'Dummy 0xz'+'\n'+ ' ^', e.msg) lexer = LexerStateMachine('Dummy 0x1234567989abcdefh', self.context) try: lexer.advance() raise SyntaxError("Exception test failed") except SyntaxError as e: self.assertEqual("Error[1][25]:Expecting hex number after f"+'\n'+ 'Dummy 0x1234567989abcdefh'+'\n'+ ' ^', e.msg) lexer = LexerStateMachine('Dummy 0x 123', self.context) try: lexer.advance() raise SyntaxError("Exception test failed") except SyntaxError as e: self.assertEqual("Error[1][9]:Expecting hex number after 0x"+'\n'+ 'Dummy 0x 123'+'\n'+ ' ^', e.msg) def test_advance_will_make_two_token_for_seperation_with_space(self): lexer = LexerStateMachine('0x123 Hello 0x456 0x789', self.context) testToken = lexer.currentToken self.assertAlmostEqual(testToken.data[0], 291) self.assertEqual(testToken.id, '(literal)') testToken = lexer.advance() self.assertEqual(testToken.data[0], "Hello") self.assertEqual(testToken.id, '(identifier)') testToken = lexer.advance() self.assertEqual(testToken.data[0], 1110) self.assertEqual(testToken.id, '(literal)') testToken = lexer.advance() self.assertEqual(testToken.data[0], 1929) self.assertEqual(testToken.id, '(literal)') def test_advance_will_generate_a_token_for_octal_number(self): lexer = LexerStateMachine('Dummy 012', self.context) testToken = lexer.advance() self.assertAlmostEqual(testToken.data[0], 10) self.assertEqual(testToken.id, '(literal)') def test_advance_will_generate_a_token_for_a_bigger_octal_number(self): lexer = LexerStateMachine('Dummy 01234567', self.context) testToken = lexer.advance() self.assertAlmostEqual(testToken.data[0], 342391) self.assertEqual(testToken.id, '(literal)') def test_advance_will_throw_exception_for_invalid_octal_number_been_found(self): lexer = LexerStateMachine('Dummy 08', self.context) try: lexer.advance() raise SyntaxError("Exception test failed") except SyntaxError as e: self.assertEqual("Error[1][8]:Expecting X/x, B/b, . or octal number after 0."+'\n'+ 'Dummy 08'+'\n'+ ' ^', e.msg) def test_advance_will_throw_exception_for_invalid_number_after_some_valid_number(self): lexer = LexerStateMachine('Dummy 01238', self.context) try: lexer.advance() raise SyntaxError("Exception test failed") except SyntaxError as e: self.assertEqual("Error[1][11]:Expecting octal number after 3"+'\n'+ 'Dummy 01238'+'\n'+ ' ^', e.msg) def test_advance_will_throw_exception_for_invalid_number_after_long_valid_number(self): lexer = LexerStateMachine('Dummy 012123456123457778', self.context) try: lexer.advance() raise SyntaxError("Exception test failed") except SyntaxError as e: self.assertEqual("Error[1][24]:Expecting octal number after 7"+'\n'+ 'Dummy 012123456123457778'+'\n'+ ' ^', e.msg) def test_advance_will_return_a_token_that_constructed_from_binary_number(self): lexer = LexerStateMachine('Dummy 0b1011', self.context) testToken = lexer.advance() self.assertAlmostEqual(testToken.data[0], 11) self.assertEqual(testToken.id, '(literal)') def test_advance_will_throw_exception_for_invalid_number_appeared_after_0b(self): lexer = LexerStateMachine('Dummy 0b10115', self.context) try: lexer.advance() raise SyntaxError("Exception test failed") except SyntaxError as e: self.assertEqual("Error[1][13]:Expecting binary number after 1"+'\n'+ 'Dummy 0b10115'+'\n'+ ' ^', e.msg) def test_advance_will_throw_exception_for_the_alphabet_after_0_is_not_x(self): lexer = LexerStateMachine('Dummy 0c1234', self.context) try: lexer.advance() raise SyntaxError("Exception test failed") except SyntaxError as e: self.assertEqual("Error[1][8]:Expecting X/x, B/b, . or octal number after 0."+'\n'+ 'Dummy 0c1234'+'\n'+ ' ^', e.msg) def test_advance_will_create_token_for_identifier(self): lexer = LexerStateMachine('var1', self.context) testToken = lexer.currentToken self.assertEqual(testToken.data[0], 'var1') self.assertEqual(testToken.id, '(identifier)') def test_advance_will_generate_token_for_identifier_that_contain_underscore(self): lexer = LexerStateMachine('_var1', self.context) testToken = lexer.currentToken self.assertEqual(testToken.data[0], '_var1') self.assertEqual(testToken.id, '(identifier)') def test_advance_will_recognize_a_long_identifier_with_mixture_of_underscore(self): lexer = LexerStateMachine('_var1_is_ready_to_read_and_edit_for_100_time_in_3_processor', self.context) testToken = lexer.currentToken self.assertEqual(testToken.data[0], '_var1_is_ready_to_read_and_edit_for_100_time_in_3_processor') self.assertEqual(testToken.id, '(identifier)') def test_advance_will_create_an_operator_token(self): lexer = LexerStateMachine('x+3', self.context) testToken = lexer.advance() self.assertEqual(testToken.id, '+') def test_advance_will_create_token_for_prefix_and_postfix(self): lexer = LexerStateMachine('x++++3', self.context) testToken = lexer.advance() self.assertEqual(testToken.id, '++') testToken = lexer.advance() self.assertEqual(testToken.id, '++') testToken = lexer.advance() self.assertEqual(testToken.id, '(literal)') self.assertEqual(testToken.data[0], 3) def test_advance_will_treat_the_zero_as_a_normal_number(self): lexer = LexerStateMachine('x = 0', self.context) testToken = lexer.advance() self.assertEqual(testToken.id, '=') testToken = lexer.advance() self.assertEqual(testToken.id, '(literal)') self.assertEqual(testToken.data[0],0) def test_advance_will_get_equal_equal(self): lexer = LexerStateMachine('x == 0', self.context) testToken = lexer.advance() self.assertEqual(testToken.id, '==') testToken = lexer.advance() self.assertEqual(testToken.id, '(literal)') self.assertEqual(testToken.data[0],0) def test_lexer_will_split_the_token_if_there_is_multiple_line_in_the_string(self): lexer = LexerStateMachine("""x =myVar + hisVar*oursVar -0X123F""", self.context) testToken = lexer.currentToken self.assertEqual(testToken.data[0],'x') self.assertEqual(testToken.id, '(identifier)') testToken = lexer.advance() self.assertEqual(testToken.id, '=') testToken = lexer.advance() self.assertEqual(testToken.data[0],'myVar') self.assertEqual(testToken.id, '(identifier)') testToken = lexer.advance() self.assertEqual(testToken.id, '+') testToken = lexer.advance() self.assertEqual(testToken.data[0],'hisVar') self.assertEqual(testToken.id, '(identifier)') testToken = lexer.advance() self.assertEqual(testToken.id, '*') testToken = lexer.advance() self.assertEqual(testToken.data[0],'oursVar') self.assertEqual(testToken.id, '(identifier)') testToken = lexer.advance() self.assertEqual(testToken.id, '-') testToken = lexer.advance() self.assertEqual(testToken.data[0],4671) self.assertEqual(testToken.id, '(literal)') def test_peep_will_throw_exception_for_unexpected_token(self): lexer = LexerStateMachine("""x =myVar + hisVar*oursVar -0X123F""", self.context) try: lexer.peep('(literal)') raise SyntaxError("Exception test failed!") except SyntaxError as e: self.assertEqual("Error[1][1]:Expecting (literal) before (identifier)"+'\n'+ 'x =myVar +'+'\n'+ '^', e.msg) def test_lexer_will_recognize_logical_AND(self): lexer = LexerStateMachine('x && 0', self.context) testToken = lexer.advance() self.assertEqual(testToken.id, '&&') testToken = lexer.advance() self.assertEqual(testToken.id, '(literal)') self.assertEqual(testToken.data[0],0)
def process_a_file(file_tuple): global testrun, pairtreepath, datapath, genremapdir, felecterrors, selecttruths, debug, phraseset, pagevocabset, meaningfulheaders thisID, metadata_evidence = file_tuple perfileerrorlog = list() return_dict = dict() return_dict["htid"] = thisID return_dict["metadata"] = (thisID, "0", "0", "0", "0", "0") return_dict["errors"] = [] return_dict["phrasecounts"] = dict() if testrun: cleanID = clean_pairtree(thisID.replace("norm.txt", "")) else: cleanID = clean_pairtree(thisID) if not testrun: filepath, postfix = FileCabinet.pairtreepath(thisID, datapath) filename = filepath + postfix + '/' + postfix + ".zip" else: filename = datapath + thisID # ACTUALLY READ THE FILE. if filename.endswith('.zip'): pagelist, successflag = read_zip(filename) else: pagelist, successflag = read_txt(filename) if successflag == "missing file": print(thisID + " is missing.") perfileerrorlog.append(thisID + '\t' + "missing") return_dict["errors"] = perfileerrorlog return return_dict elif successflag == "pagination error": print(thisID + " has a pagination problem.") perfileerrorlog.append(thisID + '\t' + "paginationerror") return_dict["errors"] = perfileerrorlog return return_dict tokens, pre_matched, pre_english, pagedata, headerlist = NormalizeVolume.as_stream(pagelist, verbose=debug) if pre_english < 0.6: perfileerrorlog.append(thisID + '\t' + "not english") tokencount = len(tokens) if len(tokens) < 10: print(thisID, "has only tokencount", len(tokens)) perfileerrorlog.append(thisID + '\t' + 'short') correct_tokens, pages, post_matched, post_english = NormalizeVolume.correct_stream(tokens, verbose = debug) # Combine page dictionaries into a master dictionary. # If you ask, why didn't you just produce one in the first place? ... # answer has to do with flexibility of the Volume module for other purposes. pagecounter = 0 masterdict = dict() for page in pages: for item in page: if item in masterdict: masterdict[item] += page[item] else: masterdict[item] = page[item] # Now that we have a master dictionary, consider whether there are long-s problems. # This algorithm works adequately. errors = 1 truths = 1 # Initialized to 1 as a Laplacian correction. for word in felecterrors: errors = errors + masterdict.get(word, 0) for word in selecttruths: truths = truths + masterdict.get(word, 0) if truths > errors: LongSproblem = False else: LongSproblem = True if LongSproblem == False: corrected = correct_tokens deleted = dict() added = dict() else: deleted, added, corrected, changedphrases, unchanged = Context.catch_ambiguities(correct_tokens, debug) # okay, this is crazy and not efficient to run, but it's easy to write and there are a small number # of these files -- so I'm going to count the new contextually-corrected tokens by re-running them # through Volume. correct_tokens, pages, post_matched, post_english = NormalizeVolume.correct_stream(corrected, verbose = debug) corrected = correct_tokens # If we are upvoting tokens in the header, they need to be added here. for index, page in enumerate(pages): thispageheader = headerlist[index] header_tokens, header_pages, dummy1, dummy2 = NormalizeVolume.correct_stream(thispageheader, verbose = debug) headerdict = header_pages[0] for key, value in headerdict.items(): if key in meaningfulheaders: if key in page: page[key] += 2 # a fixed increment no matter how many times the word occurs in the # header else: page[key] = 2 print("Word " + key + " in headerdict for " + thisID + " at " + str(index) + " but not main page.") # Write corrected file. cleanHTID = clean_pairtree(thisID) if testrun: if cleanHTID.endswith(".clean.txt"): outHTID = cleanHTID.replace(".clean.txt", "") elif cleanHTID.endswith("norm.txt"): outHTID = cleanHTID.replace("norm.txt", ".norm.txt") elif cleanHTID.endswith(".txt"): outHTID = cleanHTID.replace(".txt", "norm.txt") else: outHTID = cleanHTID + ".norm.txt" outfilename = outpath + "texts/" + outHTID else: outfilename = filepath + postfix + '/' + postfix + ".norm.txt" with open(outfilename, mode = 'w', encoding = 'utf-8') as file: for token in corrected: if token != '\n' and token != "“" and not (token.startswith('<') and token.endswith('>')): token = token + " " file.write(token) if len(pages) != len(pagedata): perfileerrorlog.append("Discrepancy between page data and page metadata in \t" + thisID) totalwordsinvol = 0 if testrun: if cleanHTID.endswith(".clean.txt"): outHTID = cleanHTID.replace(".clean.txt", ".pg.tsv") elif cleanHTID.endswith("norm.txt"): outHTID = cleanHTID.replace("norm.txt", ".pg.tsv") elif cleanHTID.endswith(".txt"): outHTID = cleanHTID.replace(".txt", ".pg.tsv") else: outHTID = cleanHTID + ".pg.tsv" outfilename = outpath + "pagefeatures/" + outHTID else: outfilename = filepath + postfix + '/' + postfix + ".pg.tsv" with open(outfilename, mode = 'w', encoding = 'utf-8') as file: if metadata_evidence["biography"]: file.write("-1\t#metaBiography\t0\n") if metadata_evidence["drama"]: file.write("-1\t#metaDrama\t0\n") if metadata_evidence["fiction"]: file.write("-1\t#metaFiction\t0\n") if metadata_evidence["poetry"]: file.write("-1\t#metaPoetry\t0\n") numberofpages = len(pages) for index, page in enumerate(pages): # This is a shameful hack that should be deleted later. if testrun and "estimated" in page and "percentage" in page and (index + 3) > numberofpages: continue if testrun and "untypical" in page and (index +2) > numberofpages: continue otherfeatures = 0 for feature, count in page.items(): if feature in pagevocabset or feature.startswith("#"): outline = str(index) + '\t' + feature + '\t' + str(count) + '\n' # pagenumber, featurename, featurecount file.write(outline) else: otherfeatures += count if not feature.startswith("#"): totalwordsinvol += count # This is because there are structural features like #allcapswords # that should not be counted toward total token count. structural_features = pagedata[index] for feature, count in structural_features.items(): if count > 0 or feature == "#textlines": outline = str(index) + '\t' + feature + '\t' + str(count) + '\n' file.write(outline) if otherfeatures > 0: outline = str(index) + '\t' + "wordNotInVocab" + '\t' + str(otherfeatures) + '\n' file.write(outline) metatuple = (thisID, str(totalwordsinvol), str(pre_matched), str(pre_english), str(post_matched), str(post_english)) return_dict["metadata"] = metatuple return_dict["errors"] = perfileerrorlog return return_dict
def uvw (self,dir0=None,*quals): """returns station UVW node(s) for a given phase centre direction, or using the global phase center if None is given. For the global phase center (dir0=None), can also use UVWs from the MS or from MEP tables, according to whatever was specified in the constructor. For other directions, UVWs are always computed. If a station is supplied, returns UVW node for that station"""; if dir0 is not None: radec0 = Context.get_dir0(dir0).radec(); uvw = self.ns.uvw.qadd(radec0); else: uvw = self.ns.uvw; if not uvw(self.stations()[0]).initialized(): if self._ms_uvw: # read UVWs from MS # if baseline UVWs preferred, create spigots for all of them if self._prefer_baseline_uvw: uvw_ifr = self.ns.uvw_ifr.qadd(radec0) if dir0 is not None else self.ns.uvw_ifr; for (ip,p),(iq,q) in self.ifr_index(): uvw_ifr(p,q) << Meq.Spigot(station_1_index=ip,station_2_index=iq, input_col='UVW',include_deriv=self._include_uvw_deriv); # find the reference station if uvw_refant is UVW_REFANT_DEFAULT: ip0,p0 = self.station_index()[0]; else: try: num = self.stations().index(uvw_refant); except: raise ValueError,"reference antenna '%s' not found"%uvw_refant; ip0,p0 = self.station_index()[num]; # reference station gets (0,0,0), the rest is via subtraction if self._include_uvw_deriv: uvw(p0) << Meq.Constant([0,0,0,0,0,0],dims=[2,3]); else: uvw(p0) << Meq.Constant([0,0,0],dims=[3]); uvw(p0)._multiproc = True; # hint to parallelizer to clone this node on all processors for iq,q in self.station_index(): if iq < ip0: if self._prefer_baseline_uvw: muvw = uvw_ifr(q,p0); else: muvw = uvw('minus',q) << Meq.Spigot(station_1_index=iq,station_2_index=ip0, input_col='UVW',include_deriv=self._include_uvw_deriv); spigdef = -muvw; elif iq > ip0: if self._prefer_baseline_uvw: spigdef = Meq.Identity(uvw_ifr(p0,q)); else: spigdef = Meq.Spigot(station_1_index=ip0,station_2_index=iq, input_col='UVW',include_deriv=self._include_uvw_deriv); else: continue; if self._resamplers: uvw(q) << Meq.Resampler(uvw(q,"res") << spigdef,mode=1); else: uvw(q) << spigdef; elif self._uvw_table: # read UVWs from MEP table for station in self.stations(): uvw(station) << Meq.Composer( self.ns.u.qadd(radec0)(station) << Meq.Parm(table_name=self._uvw_table), self.ns.v.qadd(radec0)(station) << Meq.Parm(table_name=self._uvw_table), self.ns.w.qadd(radec0)(station) << Meq.Parm(table_name=self._uvw_table) ); else: # compute UVWs via a UVW node radec0 = Context.get_dir0(dir0).radec(); xyz0 = self.xyz0(); xyz = self.xyz(); for station in self.stations(): uvw_def = Meq.UVW(radec=radec0,xyz_0=xyz0,xyz=xyz(station), include_deriv=self._include_uvw_deriv); if self._mirror_uvw: uvw(station) << Meq.Negate(self.ns.m_uvw(station) << uvw_def ); else: uvw(station) << uvw_def; return uvw(*quals);