def absorption_coefficient(self, temperature, pressure, partial_pressure, wavenumber, cut_off=25.): """Calculates the absorption coefficient. Args: temperature: Temperature [K]. pressure: Pressure [atm]. partial_pressure: Partial pressure [atm]. wavenumber: Numpy array of wavenumbers [cm-1] (wavenumber). cut_off: Distance [cm-1] from the transition frequency where the line is cut off. Returns: Numpy array of absorption coefficients [cm2] (wavenumber). """ lines = shallow_copy(self) lines.s = self.correct_line_strengths(temperature) lines.v = lines.pressure_shift_transition_wavenumbers(pressure) profile = shallow_copy(lines.line_profile) profile.update(lines, temperature, pressure, partial_pressure) k = zeros(wavenumber.size) for i in range(lines.s.size): left = searchsorted(wavenumber, lines.v[i] - cut_off, side="left") right = searchsorted(wavenumber, lines.v[i] + cut_off, side="right") k[left:right] += lines.s[i] * profile.profile( lines, wavenumber[left:right], i) return k
def apply_state(self, state: State): """applies a state to the environment, in terms of the agent's state variables, node evacuation status and blocked edges""" agent, to_copy = state.agent, state.agent_state agent.update(to_copy) self.time = agent.time self.require_evac_nodes = shallow_copy(state.require_evac_nodes) self.blocked_edges = shallow_copy(state.blocked_edges) for v in self.G.get_vertices(): v_requires_evac = v in self.require_evac_nodes v.evacuated = not v_requires_evac v.n_people = v.n_people_initial if v_requires_evac else v.n_people for e in self.G.get_edges(): e.blocked = e in self.blocked_edges
def assure_iter(s, cls, copy=False, iterate=True): """Given not a list, would place it into a list. If None - empty list is returned Parameters ---------- s: list or anything cls: class Which iterable class to assure copy: bool, optional If correct iterable is passed, it would generate its shallow copy iterate: bool, optional If it is not a list, but something iterable (but not a text_type) iterate over it. """ if isinstance(s, cls): return s if not copy else shallow_copy(s) elif isinstance(s, text_type): return cls((s, )) elif iterate and hasattr(s, '__iter__'): return cls(s) elif s is None: return cls() else: return cls((s, ))
def inference_on_batch(self, insts: List[DocInstance], **kwargs): self.refresh_batch(False) # ----- if len(insts) == 0: return {} # ----- # todo(note): first do shallow copy! for one_doc in insts: for one_sent in one_doc.sents: one_sent.pred_entity_fillers = [ z for z in one_sent.entity_fillers ] one_sent.pred_events = [ shallow_copy(z) for z in one_sent.events ] # ----- ndoc, nsent = len(insts), 0 iconf = self.conf.iconf with BK.no_grad_env(): # splitting into buckets all_packs = self.bter.run(insts, training=False) for one_pack in all_packs: ms_items, bert_expr, basic_expr = one_pack nsent += len(ms_items) self.predictor.predict(ms_items, bert_expr, basic_expr) info = { "doc": ndoc, "sent": nsent, "num_evt": sum(len(z.pred_events) for z in insts) } if iconf.decode_verbose: zlog(f"Decode one mini-batch: {info}") return info
def merge(self, batch, merge_profiling_stats=True): '''Merge this batch (``a``) with another batch (``b``). This creates a new batch ``c`` containing arrays and graphs from both batches ``a`` and ``b``: * Arrays or Graphs that exist in either ``a`` or ``b`` will be referenced in ``c`` (not copied). * Arrays or Graphs that exist in both batches will keep only a reference to the version in ``b`` in ``c``. All other cases will lead to an exception. ''' merged = shallow_copy(self) for key, val in batch.items(): # TODO: What is the goal of `val.spec.roi is None`? Why should that # mean that the key in merged gets overwritten? if key not in merged or val.spec.roi is None: merged[key] = val elif key in merged: merged[key] = val if merge_profiling_stats: merged.profiling_stats.merge_with(batch.profiling_stats) if batch.loss is not None: merged.loss = batch.loss if batch.iteration is not None: merged.iteration = batch.iteration return merged
def sanitise_notice_for_database(notice): sanitised_notice = shallow_copy(notice) if 'attachment_raw' in sanitised_notice: del sanitised_notice['attachment_raw'] return sanitised_notice
def handle_notices_diff(notices): notices_coll = mc.get_default_database().notices different_notices = [] print 'Checking ', len(notices), 'notices' for notice in notices: sanitised_notice = sanitise_notice_for_database(notice) notice_cpy = shallow_copy(sanitised_notice) try: del notice_cpy['uid'] except KeyError: pass db_notice = notices_coll.find_one( {'$or': [{ 'uid': sanitised_notice['uid'] }, notice_cpy]}) if db_notice is None: different_notices.append(notice) print 'Different notices: ', [ sanitise_notice_for_database(notice) for notice in different_notices ] if len(different_notices) > 0: for notice in different_notices: sanitised_notice = sanitise_notice_for_database(notice) hooks.notices_updated([notice]) notices_coll.insert_one(sanitised_notice)
def conv(cls, dic, copy=False): if not isinstance(dic, dict): return copy and shallow_copy(dic) or dic new_dic = cls.__new__(cls) # not trigger __init__ for k, v in dic.iteritems(): # __setitem__ use this, need super super(cls, new_dic).__setitem__(k, cls.conv(v, copy=copy)) return new_dic
def sanitise_notice_for_database(notice): sanitised_notice = shallow_copy(notice) try: del sanitised_notice['attachment_raw'] except KeyError: pass return sanitised_notice
def _copy_sent(sent, cur_event): ret = shallow_copy(sent) ret.events = [] ret.pred_events = [] if cur_event is not None: # only one event for center ret.events.append( cur_event) # for events, use the original one copied_event = deep_copy( cur_event) # for pred, use the copied one copied_event.links.clear( ) # and clear links(args) for prediction ret.pred_events.append(copied_event) ret.entity_fillers = shallow_copy( ret.entity_fillers) # for training ret.pred_entity_fillers = [] # to predict ret.orig_sent = sent # used in prediction to append back to the original instances return ret
def conv(cls, dic, copy=False): if not isinstance(dic, dict): return copy and shallow_copy(dic) or dic mydic = cls() # cls__new__(cls) better for k, v in dic.iteritems(): # __setitem__ 调用了这里,也得super super(cls, mydic).__setitem__(k, cls.conv(v, copy=copy)) return mydic
def set_styles(self, i, styles): while style: l, style = styles[0] del styles[0] if i <= 0 and i + l > 0: clone = shallow_copy(self) clone.style = style return clone i += l return self
def duplicate(self): if hasattr(self, '_cow_shallow_copy'): dup = self._cow_shallow_copy() else: dup = shallow_copy(self) if not hasattr(self, '_cow_copies'): self._cow_copies = [] self._cow_copies.append(dup) dup._cow_is_copy_of = self return dup
def copy(x): if x is None: return None elif isinstance(x, list): x = list(x) else: try: x = x.copy() except AttributeError: from copy import copy as shallow_copy x = shallow_copy(x) return x
def copy_mutable(self): ''' Clone the game object, deep copying everything except the cards themselves ''' clone = shallow_copy(self) # deep copy as mutable for attr, value in self.__dict__.items(): if attr.startswith('__'): continue if isinstance(value, tuple): clone.__dict__[attr] = copy_nested(value) return clone
def _insts2msitems(self, insts): # ----- # a new sent with new containers but old contents for ef def _copy_sent(sent, cur_event): ret = shallow_copy(sent) ret.events = [] ret.pred_events = [] if cur_event is not None: # only one event for center ret.events.append( cur_event) # for events, use the original one copied_event = deep_copy( cur_event) # for pred, use the copied one copied_event.links.clear( ) # and clear links(args) for prediction ret.pred_events.append(copied_event) ret.entity_fillers = shallow_copy( ret.entity_fillers) # for training ret.pred_entity_fillers = [] # to predict ret.orig_sent = sent # used in prediction to append back to the original instances return ret # ----- ret = [] for inst in insts: center_cands = [] if isinstance(inst, DocInstance): center_cands.extend(inst.sents) else: assert isinstance(inst, MultiSentItem) center_cands.append( inst.sents[inst.center_idx] ) # only include the center one if already ms # get the new specially copied ones for one_center_sent in center_cands: one_ms_item = one_center_sent.preps["ms"] one_center_idx = one_ms_item.center_idx # todo(note): new instance for each event (use input events!!) for one_event in one_center_sent.events: one_ms = shallow_copy(one_ms_item) one_ms.sents = [ _copy_sent(s, (one_event if (i == one_center_idx) else None)) for i, s in enumerate(one_ms.sents) ] # special copy sents ret.append(one_ms) return ret
def to_pickle(self, path): """Save the current Graph to a pickle. Parameters ---------- path : str File path where the pickled object will be stored. """ pickle_obj = shallow_copy(self) is_oldpygsp = all([ isinstance(self, pygsp.graphs.Graph), int(sys.version.split(".")[1]) < 7 ]) if is_oldpygsp: pickle_obj.logger = pickle_obj.logger.name with open(path, "wb") as f: pickle.dump(pickle_obj, f, protocol=pickle.HIGHEST_PROTOCOL)
def apply_state(self, state: State, active_agent=None): """applies a state to the environment, in terms of the max_player's state variables & node evacuation status""" agent, to_copy = state.agent, state.agent_state agent2, to_copy2 = state.agent2, state.agent2_state agent.update(to_copy) agent2.update(to_copy2) self.time = agent.time if active_agent is not None: self.time = (active_agent is agent) and agent.time or agent2.time self.require_evac_nodes = shallow_copy(state.require_evac_nodes) for v in self.G.get_vertices(): v.agents = set([]) v_requires_evac = v in self.require_evac_nodes v.evacuated = not v_requires_evac v.n_people = v.n_people_initial if v_requires_evac else v.n_people # update agent locations agent.loc.agents.add(agent) agent2.loc.agents.add(agent2) self.agent_actions = state.agent_actions
def __smoothen(signal, frac_smooth=0.3, remove_outlier=True): if type(signal) != list and type(signal) != np.ndarray: from copy import copy as shallow_copy signal_line = signal.axes.lines[0] smooth_line = shallow_copy(signal_line) signal_x, signal_y = signal_line.get_data() smooth_y = __smoothen(signal_y, frac_smooth, False) smooth_line.set_data(signal_x, smooth_y) smooth_line.set_color('g') signal_line.set_alpha(0.2) signal.axes.add_line(smooth_line) return signal def __median_absolute_deviation_outlier(points, thresh=3.5): if len(points.shape) == 1: points = points[:, None] median = np.median(points, axis=0) diff = np.sum((points - median)**2, axis=-1) diff = np.sqrt(diff) med_abs_deviation = np.median(diff) modified_z_score = 0.6745 * diff / med_abs_deviation return modified_z_score > thresh x = np.array(signal) if remove_outlier: outliers = __median_absolute_deviation_outlier(x) x = x[~outliers] window_length = int(x.shape[0] * frac_smooth) if window_length % 2 == 0: window_length += 1 if window_length < 3: return x elif window_length > x.shape[0]: window_length = x.shape[0] if window_length % 2 == 0: window_length -= 1 return savgol_filter(x, window_length, 1)
def get_edge_deadlines(self): vandals = [agent for agent in self.agents if agent.is_vandal()] if not vandals: return vandal_states = [self.get_state(vandal) for vandal in vandals] V = self.G.get_vertices() agent_locs = {v: shallow_copy(v.agents) for v in V} self.G.display('Initial State: (Vandals simulation)') while not all([vandal.terminated for vandal in vandals]): print('\nT={} (Vandals simulation)'.format(self.time)) for vandal in vandals: vandal.act(self) self.tick() self.G.display('Final State: (Vandals simulation)') # restore initial state, keeping edge blocking times (edge deadlines) for vandal_state in vandal_states: self.apply_state(vandal_state) for v in V: v.agents = agent_locs[v] print("Finished vandals simulation. Edge deadlines:") print( set([(e, e.deadline) for e in self.G.get_edges() if e.deadline < float('inf')]))
def answer_to(self, question: str) -> str: sentence = self.clear_punctuation(question) sentence = sentence.lower() keywords = Keystack(sentence, shallow_copy(self.phrasing_memory)) keywords = keywords.prioritize() subs = dict(PhraseMemory.pre_substitutors()) words = sentence.split(' ') words = Reassembler.replace(words, subs) sentence = ' '.join(words) response = "" try: response: str = self.decomposer.process_keywords(keywords, sentence) except ReassemblyRuleNotFoundException as r_err: logging.exception(r_err.msg) except DecompositionRuleNotFoundException as d_err: logging.exception(d_err.msg) except KeywordProcessingFailedException as k_err: logging.exception(k_err.msg) return response
def copy(self, old=None, new=None): """Return a deep enough copy with updated bond tags. Arguments: old :: site :: external bond target which should be changed to new new :: site :: new bond target look :: tuple subset of (0, 1) :: if an axis is in this tuple, bonds on that axis will be updated """ output = shallow_copy(self) output.ind = self.ind.copy() for axis in (0, 1): output.ind[axis] = self.ind[axis].copy() for i, ind in enumerate(output.ind[axis]): output.ind[axis][i] = ind.copy() if isinstance(ind, bond): output.ind[axis][i].tag = output.ind[axis][i].tag.copy() # update bond with reference to copied site for j, e in enumerate(output.ind[axis][i].tag): if (e == self): output.ind[axis][i].tag[j] = output if (e == old): output.ind[axis][i].tag[j] = new return output
def get_agent_state(self): return shallow_copy(self)
def copy(game): game = shallow_copy(game) game.players = tuple(game.players) game.team = game.team and frozenset(game.team) return game
def set_childs(self, childs): clone = shallow_copy(self) clone.childs = list(childs[:]) clone.compute_weights() return clone
def set_style(self, style): clone = shallow_copy(self) clone.style = style return clone
def copy(self): """ Return a shallow copy """ return shallow_copy(self)
def schedule_shallow_copy(self): return {time: shallow_copy(actions) for time, actions in self.agent_actions.items()}
def clone(self, f, *a): self = shallow_copy(self) self.tracker = self.tracker and SuspectTracker(self._, set(self.tracker.spies)) getattr(self, f)(*a) return self
def clone(self, name: str = None) -> Weapon: result = shallow_copy(self) if name is not None: result.name = name return result
def copy(self): return shallow_copy(self)
def get_require_evac_nodes(self): return shallow_copy(self.require_evac_nodes)