content
stringlengths
42
6.51k
def postprocess_output(model_output, narrative_token, dialog_token, eos_token): """ Performs the reverse of preprocess_input. Removes dialog, dialog, and eos special tokens from model output after tokenizer decoding. Text between a narrative_token and eos_token gets surrounded with '***'. """ #R...
def getKeys(dct, merge=False): """ Get dictionary keys for Python3 Input: > dct: A dictionary Output: > The first key, all other keys """ if not isinstance(dct, (dict,list)): print("Argument is not a dictionary") return None if isinstance(dct,list...
def load_patents(file): """ Read and split patent file. Parameters ---------- file : str, file-like Path to, or file-handle to file containing patents in stupid text format. Returns ------- list[str] List of patents in stupid text-format. """ if isinstance(f...
def date_str(year, month, day, hour=0, minute=0, second=0., microsecond = None): """ Creates an ISO 8601 string. """ # Get microsecond if not provided if microsecond is None: if type(second) is float: microsecond = int((second - int(second)) * 1000000) else: m...
def xor(arr1, arr2): """XOR two byte arrays. Shorter array should be passed second.""" if len(arr2) < len(arr1): l1, l2 = len(arr1), len(arr2) arr2 = arr2 * int(l1 / l2) + arr2[:l1 % l2] return [c1 ^ c2 for c1, c2 in zip(arr1, arr2)]
def ngrams(seq, min_n, max_n): """ Return min_n to max_n n-grams of elements from a given sequence. """ text_len = len(seq) res = [] for n in range(min_n, min(max_n + 1, text_len + 1)): for i in range(text_len - n + 1): res.append(seq[i: i + n]) return res
def makeFields(prefix, n): """Generate a list of field names with this prefix up to n""" return [prefix+str(n) for n in range(1,n+1)]
def file_size_format(size, suffix="B"): """Given a file size, return a string with its standard compressed form.""" for unit in ["", "K", "M", "G", "T", "P", "E", "Z"]: if size < 1024: break size /= 1024 else: unit = "Yi" return f"{size:.1f} {unit}{suffix}"
def index_containing_substring(list_str, substring): """For a given list of strings finds the index of the element that contains the substring. Parameters ---------- list_str: list of strings substring: substring Returns ------- index: containing the substring or -1 """ ...
def make_album(artist, title, tracks=0): """Build a dictionary containing information about an album.""" album_dict = { 'artist': artist.title(), 'title': title.title(), } if tracks: album_dict['tracks'] = tracks return album_dict
def badness(page_width, word_lengths, i, j): """ Get the "badness" of a line of text given the width of the page you are trying to fit it on """ total_width = sum(word_lengths[i-1:j]) + j - i + 1 if page_width < total_width: return float('inf') return (page_width - total_width) ** 3
def get_folded_phase(t,best_period): """Fold the observation times with the best period of the variable signal.""" # light curve over period, take the remainder (i.e. the "phase" of one period) phase = (t / best_period) % 1 return phase
def get_train_valid_test_split_(splits_string, size): """ Get dataset splits from comma or '/' separated string list.""" splits = [] if splits_string.find(',') != -1: splits = [float(s) for s in splits_string.split(',')] elif splits_string.find('/') != -1: splits = [float(s) for s in sp...
def boolean(val): """Convert the given value to a boolean True/False value, if it isn't already. True values are '1', 'yes', 'true', and 'on' (case insensitive), everything else is False.""" return str(val).lower() in ('1', 'yes', 'true', 'on')
def UnderscoreToCamelCase(under_score): """Helper function which converts under_score names to camelCase. In proto buffers, fields have under_scores. In Javascript, fields have camelCase. Args: under_score: A name, segmented by under_scores. Returns: A name, segmented as camelCase. """ segments =...
def default_context(plugin, context): """ Return the default context for plugins rendered with a template, which simply is a single variable named ``plugin`` containing the plugin instance. """ return {"plugin": plugin}
def raw_unicode(raw): """Make a unicode string from a raw string. This function takes a string containing unicode escape characters, and returns the corresponding unicode string. Useful for writing unicode string literals in your python source while being upwards- compatible with Python 3. For ex...
def _maybe_timed(x): """Unpack (est, fit_time) tuples if provided""" return x if isinstance(x, tuple) and len(x) == 2 else (x, 0.0)
def _fixh5path(h5path): """Fix an h5path for use in h5py file objects""" if h5path in [None, '', ' ', '/']: h5path = '/' if h5path[0] != '/': h5path = '/' + h5path if h5path[-1] != '/': h5path = h5path + '/' return h5path
def singleInstanceFromTimingRef(dictOfDF): """ Parameters ---------- dictOfDF : TYPE DESCRIPTION. Returns ------- dictOfDF_NoTiming : Dictionary of DataFrames Removes timing reference chirp at beginning of sweep. """ dictOfDF_NoTiming = {} for count, key in...
def calc_dx(x): """Computes the grid spacing.""" dx = x[1] - x[0] return dx
def print_grad(named_parameters): """ visualize grad """ total_norm = 0 param_to_norm = {} param_to_shape = {} for n, p in named_parameters: if p.grad is not None: param_norm = p.grad.data.norm(2) total_norm += param_norm ** 2 param_to_norm[n] = p...
def determine_anisotropy_classification(branch_classification: str) -> int: """ Return value based on branch classification. Only C-C branches have a value, but this can be changed here. Classification can differ from 'C - C', 'C - I', 'I - I' (e.g. 'C - E') in which case a value (0) is still retur...
def humanReadable(size_in_bytes): """Returns sizes in human-readable units.""" try: size_in_bytes = int(size_in_bytes) except ValueError: size_in_bytes = 0 units = [(" KB", 10**6), (" MB", 10**9), (" GB", 10**12), (" TB", 10**15)] for suffix, limit in units: if size_in_bytes ...
def calculate_change(old_val, new_val): """ Return a float representing the decimal change between old_val and new_val. """ if old_val == 0 and new_val == 0: return 0.0 if old_val == 0: return float(new_val - old_val) / (float(old_val + new_val) / 2) return float(new_val - old_va...
def capitalize_word(word): """ Capitalize the first letter of the word. :param word: a string input. :return: the word in title form. """ return word.title()
def handle033a(tokens): """ Processes the 033A (Ort und Verlag) field. Currently, only subfield p and n are supported. For details (in German), see: https://www.gbv.de/bibliotheken/verbundbibliotheken/02Verbund/01Erschliessung/02Richtlinien/01KatRicht/4030.pdf :param tokens: a list of tokens of the fiel...
def writeBenchScript(dir, bench): """ This method creates a script in dir which will be eventually passed to the simulated system (to run a specific benchmark at bootup). """ file_name = '{}/run_{}'.format(dir, bench) bench_file = open(file_name,"w+") bench_file.write('/home/gem5/NPB3.3-...
def div(n,length): """ """ result = [] for i in range(length+1): if i % n == 0 or i == length: result.append(i) return result
def sizeof_fmt(num, suffix="B"): """ Returns the filesize as human readable string. https://stackoverflow.com/questions/1094841/reusable-library-to-get-human- readable-version-of-file-size """ for unit in ["", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi"]: if abs(num) < 1024.0: ...
def occurrences(resp_json, return_obj, options): """Taxa in time and space.""" # from ..elc import ages # Currently SEAD does not support age parameterization # The database also does not include age in the occurrence response # factor = ages.set_age_scaler(options=options, db='pbdb') for r...
def get_numerical_value(expression): """ Recursively check if the expression can be treated as a numerical value. :param expression: :return: boolean """ if expression.startswith("(") and expression.endswith(")") and (len(expression) > 2): new_expression = expression[1:-1] retur...
def _is_manager_secured(response_history): """ Checks if the manager is secured (ssl enabled) The manager is secured if the request was redirected to https """ if response_history: first_response = response_history[0] return first_response.is_redirect \ and first_response.h...
def compute_artist_rating(ratings): """Returns an artist rating based on the ratings of its albums.""" return float(sum(ratings)) / max(len(ratings), 1)
def merge_reduce(source_dict, other_dict, func): """ merge the reduce result with the same key Args: source_dict: other_dict: func: the function to merge two dict Returns: dict1: result after merge """ dict1_keys = source_dict.keys() for k, v in other_dict.i...
def add_phasing_err(chrA, chrB, snp_pos, errors_positions): """ Given two haplotypes with the snp positions (as in the msmc input format) as well as the positions of some phasing errors, this method will output two new chromosomes switched at positions where phasing errors happen. """ snp_pos = ...
def interpolate(t: float, a: float, b: float, fa: float, fb: float) -> float: """interpolate between a and b""" return (t-a) * (fa - fb)/(b-a) + fa
def subtract_hour(string): """Subtracts an hour from the string - 24 hour format.""" if string == "0": return "23" hour = int(string) return str(hour - 1)
def Cornfeld_mean(*variables): """Return the mean of a sample using Cornfeld's method. Input: *float (all variables as arguments) Output: int """ return (max(variables) + min(variables)) / 2
def iterable(item): """If item is iterable, returns item. Otherwise, returns [item]. Useful for guaranteeing a result that can be iterated over. """ try: iter(item) return item except TypeError: return [item]
def sources(capacity_factor: bool = True): """ This function provides the links to the sources used for obtaining certain information. The arguments can either be set to the appropriate boolean based on the sources required. Parameters: ----------- capacity_factor: bool This arg...
def size_fmt(num: float, suffix='B') -> str: """Returns readable str form of given size number (e.g. bytes) in more readable higher form, e.g. 1_000_000 -> 976.6KiB""" for unit in ['','Ki','Mi','Gi','Ti','Pi','Ei','Zi']: if abs(num) < 1024.0: return "%3.1f%s%s" % (num, unit, suffix) ...
def is_valid_ascii(path): """Check if string is valid ascii""" try: path.decode('ascii') valid= True except UnicodeDecodeError: valid= False return valid
def is_sublist(l1, l2, pos=False): """ Check if l1 is a sublist of l2 (it contains all elements from l1 in the same order). if pos is True, return position where sublist starts, not a boolean value """ if l1 == []: return 0 if pos else True elif l1 == l2: return 0 if pos else Tr...
def parse_arguments(*args): """ Parse arguments into a dictionary. Returns ------- tuple First element with codes and second element with names. Examples >>> utils.collect_args({"A": 1, "B": 2}, 100) {"A": 1, "B": 2, 100: 100} """ d = {} for arg in args: ...
def mil(val): """convert mil to mm""" return float(val) * 0.0254
def _extract_value_index(line, start=0): """Returns position of last non-whitespace character """ shortline = line.rstrip() return len(shortline)
def character_talents(talent_data): """Accepts a JSON object containing a players talents and returns the players current active specalization.""" talents = talent_data["talents"] # Starts empty just incase the player hasn't got a spec selected. active_spec = "" for talent in talents: ...
def extract_section_by_name(path, section_name): """ :param path: :type path: list :param section_name: :type section_name: str :return: :rtype: list|None """ section = section_name.split(" ") predicate = lambda x, section: section == x[:len(section)] try: return ...
def _string_tolist(s): """Convert the authorization comma separated string to list """ return [v.strip() for v in s.split(',') if v]
def returnCentre(tlbr): """This function returns centre of bounding box. Args: tlbr (list) : list of values in str [topleft_x,topleft_y,bottomright_x,bottomright_y] Returns: list: Centre cordinates of bounding box """ int_tlbr = [int(tl...
def Bingham(x, ystress=1.0, eta_bg=0.1): """Bingham model Note: .. math:: \sigma=\sigma_y+\eta_{bg}\cdot\dot\gamma Args: ystress: yield stress [Pa] eta_bg : Background viscosity [Pa s] Returns: stress : Shear Stress, [Pa] """ return ystress + eta_bg * x
def uniques(some_list): """ Get unique values of a list """ new_list = [] for item in some_list: if item not in new_list: new_list.append(item) return new_list
def _strip_plugin_path(path): """remove "plugins." prefix if it exist""" return path[8:] if path.startswith("plugins.") else path
def findElementFromDict(dictionary,key): """ Function to find a element into a dictionary for the key """ element = None try: element = dictionary[key] return element except KeyError: return None
def get_url(package, version=None): """ Return homepage, repo, bugtracker URLs for a package. All PEARs in pear.php.net are centrally controlled and hosted. """ package = package.replace('PEAR2_','') urls = { 'homepage' : 'https://github.com/pear2/%s' % package, ...
def compute_vat_vd(rut): """ :param rut(str): rut to check Obtiene el digito de verificacion de un rut :return result(str): vat vd """ result = None factores = [3, 7, 13, 17, 19, 23, 29, 37, 41, 43, 47, 53, 59, 67, 71] rut_ajustado=str(rut).rjust( 15, '0') s = sum(int(rut_ajust...
def cache_data(path, generator, regen=False): """ This function caches data from the passed function. - If the specified path does not exist, then the function is called to generate the data and the data is then saved for future use. - If the specified path does exist, the function is ignored and th...
def trim_lost_U(seq_F, qual_F, LOSTUSEQS): """ test for lost U at the 3' end of the PCR primer sequence """ keepgoing = 1 for lostuseq in LOSTUSEQS: if keepgoing: if len(seq_F) < len(lostuseq): return seq_F, qual_F if seq_F[:len(lostuseq)] == lostuseq: ...
def concat(s1, s2): """ Helper function to reduce expressions :param s1: Sthing we can iterate over with binary sequence and '_' :param s2: Sthing we can iterate over with binary sequence and '_' :return: Merged version of input, when certain bits are different this place is being replaced by '_' ...
def create_bullet_camera_targets( position, should_save: bool, should_send: bool, ): """ Creates bullet camera targets. Args: camera_control: The method of camera control. bullet_odicts: The bullet object dictionaries. If the camera control method is `stack`, we assume that the ...
def find_parameter_groups(columns): """ Find parameter groups within a list This finds all the parameters which should be grouped together into tuples rather than being separated. For example, if you have a list like ``["CORE_CLIMATESENSITIVITY", "RF_BBAER_DIR_WM2", "OUT_ZERO_TEMP_PERIOD_1", "OUT_Z...
def get_star_column_number(line): """ For a line in a star file describing a column entry (e.g., '_rlnEstimatedResolution #5'), retrieve the value of that column (e.g. 5) """ column_num = int(line.split()[1].replace("#","")) return column_num
def partition(a_list): """assumes a_list is a list of numbers modifies a_list by internally moving its elements, such that the rightmost element (the pivot) has to its right only greater or equal elements, and has to its left only lesser or equalelements returns an int, the index of the pivot...
def take(n, iter): """Returns first n values from the given iterator.""" result = [] try: for i in range(n): result.append(next(iter)) except StopIteration: pass return result
def check_for_age_range(child_age, age_range): """ check if input child age is in the query's age range :param child_age: the age to check (int) :param age_range: the age range (list) :return: True if the age is in the range, else False """ return True if min(age_range) <= child_age <= max(a...
def _HasOption(_, option): """Validate the option exists in the config file. Args: option: string, the config option to check. Returns: bool, True if test is not in the option name. """ return 'test' not in option
def calc_AIC(k, maxlogL): """ | Calculate Akaike Information Criterion. | k = number of parameters | maxlogL = maximum log likelihood """ aic = 2 * (k - maxlogL) return aic
def co_code_findloadednames(co): """Find in the code of a code object, all loaded names. (by LOAD_NAME, LOAD_GLOBAL or LOAD_FAST) """ import dis from opcode import HAVE_ARGUMENT, opmap hasloadname = (opmap['LOAD_NAME'], opmap['LOAD_GLOBAL'], opmap['LOAD_FAST']) insns = dis.ge...
def descope_message_ids_set(msgset_key): """Descope messages set with '.' :returns: (queue, project) """ tokens = msgset_key.split('.') return tokens[1] or None, tokens[0] or None
def enum_dict_aslist(d): """Summarize a dictionary with enums as keys as a shortened list with only the names of each enum member. Args: d: Dictionary with enums as keys. Returns: List of tuples for all dictionary key-value pairs, with each tuple containing the enum m...
def do_some_work(x: int) -> int: """Efficiently computes a simple polynomial just for kicks 5 + 3x + 4x^2 """ return 5 + x * (3 + x * (4))
def sub_string_exists_in(substring_list, string): """Return true if one of the substring in the list is found in |string|.""" for substring in substring_list: if substring in string: return True return False
def dict_is_song(info_dict): """Determine if a dictionary returned by youtube_dl is from a song (and not an album for example).""" if "full album" in info_dict["title"].lower(): return False if int(info_dict["duration"]) > 7200: return False return True
def _css_escape(text: str) -> str: """ @see https://developer.mozilla.org/en-US/docs/Web/CSS/string """ if not text: return '' # -- probably not needed. # text = text.encode('unicode-escape').decode('ascii').replace('\\u', '\\') return text.replace("'", '\\27')
def home(request): """ Home page. """ return {'location':'home'}
def alma_query(ra, dec, mirror="almascience.eso.org", radius=1, extra=''): """ Open browser with ALMA archive query around central position """ #ra, dec = self.get('pan fk5').strip().split() url = (f"https://{mirror}/asax/?result_view=observation" f"&raDec={ra}%20{dec},{radius}{extra}") ...
def build_tracker_url(portal_url=None): """ Creates a url that can be used to open Tracker for ArcGIS ================== ==================================================================== **Argument** **Description** ------------------ ---------------------------...
def YCoCgtoRGB(Y, Co, Cg): """ convert YCoCg to RGB color The YCoCg color model was developed to increase the effectiveness of the image compression. This color model comprises the luminance (Y) and two color difference components (Co - offset orange, Cg - offset green). :param Y: Y value (0;255) ...
def reverse(lyst): """ Returns a reversed list. """ return [lyst[p] for p in range(len(lyst) - 1, -1, -1)]
def extended_gcd(n_1, n_2): """ Returns (bezout_a, bezout_b, gcd) using the extended euclidean algorithm. Params n1: int n2: int Returns bezout_a: int bezout_b: int gcd: int """ x = 0 x_old = 1 y = 1 y_old = 0 while n_2 ...
def ipstr(barray): """Print a string of ip digits""" return ".".join(f'{x}' for x in barray)
def sdb_latitude(latitude): """Return an 8 character, zero padded string version of the latitude parameter. **Arguments:** * *latitude* -- Latitude. """ adjusted = (90 + float(latitude)) * 100000 return str(int(adjusted)).zfill(8)
def enet_params_from_sum(pen_val_1, pen_val_2): """ Computes the elastic net pen_val and mix_val from the two penalty values. pen_val_1 = pen_val * mix_val pen_val_2 = pen_val * (1 - mix_val ) Parameters ---------- pen_val_1, pen_val_2: float The two penalty values in the sum. ...
def left_to_right_check(input_line: str, pivot: int): """ Check row-wise visibility from left to right. Return True if number of building from the left-most hint is visible looking to the right, False otherwise. input_line - representing board row. pivot - number on the left-most hint of the in...
def errors_sq1_mean(y_pred, y_real): """ like sq_mean but with one always added ot the err difference so the square of the error won't get smaller if the difference is less than 1 :param y_pred: list of predicted :param y_real: list of real :return: """ if len(y_pred) != len(y_real): ...
def short_uuid4(uid): """When seeing a glimpse of the UID is enough.""" return str(uid)[:8]
def append_node(node, symbol): """ Return the appended node. Append a symbol to the node. :param node: The node that will be appended to :type node: list :param symbol: The symbol that is appended :type symbol: str :return: The new node :rtype: list """ # Create a list with the...
def calc_cost(cl1_len, cl2_len, num_cl1, num_cl2): """ Assign a cost to a pattern based on if the majority of its occurances are observed in regions of a time series that are annotated with the same binary label. The cost calculation takes into account a possible difference in the total lengths of the s...
def kth_to_last(head, k): """ 2.2 Return Kth to Last: Implement an algorithm to find the kth to last element of a singly linked list. """ def advance(node, n): if node == None or n == 0: return (node, n) else: return advance(node.next, n - 1) (runner, index) ...
def has_smach_interface(obj): """Returns True if the object has SMACH interface accessors.""" return (hasattr(obj, 'get_registered_input_keys') and hasattr(obj, 'get_registered_output_keys') and hasattr(obj, 'get_registered_outcomes'))
def fibonacci(n, m=42): """this is probaly the stupidiest way to calculate a fibonacci number - but it is a nice recursion test..""" if n < 2: return n return fibonacci(n-2) + fibonacci(n-1)
def file_get_contents(filename): """Fetches textfile""" with open(filename) as file: return file.read()
def binary_search0(xs, x): """ Perform binary search for a specific value in the given sorted list :param xs: a sorted list :param x: the target value :return: an index if the value was found, or None if not """ lft, rgt = 0, len(xs) - 1 while lft <= rgt: mid = (lft + rgt) // 2...
def get_bubble_mask(im_list, min_size): """ Computes the standard deviation of a series of images of the video. The parts of the video which are not moving in time (no particles) could be a bubble or particles which are stuck. We are especially interested in bubble regions or dried particle regions whic...
def cipher(text, shift, encrypt=True): """ The function allows encipher your message. In short, each letter is replaced by a letter some fixed number of positions down the alphabet. Parameters ---------- text: a string of the word, sentence or paragraph to encipher shift: an integer of fi...
def is_bioproject(text): """ Pattern for project numbers. """ return text.startswith("PRJ")
def standardize_sample_duration(sample_duration): """ sample_duration: a string description of duration at which devices sample and measure pollutants returns a standardized string - '24-hour' or '8-hour' or '1-hour' """ durations = ['24','8','1'] for duration in durations: if dur...
def classification_string(classification_idx): """ converts classification idx to string """ classifications = ['T-Shirt/top', \ 'Trouser', \ 'Pullover', \ 'Dress', \ 'Coat', \ 'Sandal', \ ...
def _parse_compute_full_uri_to_partial_uri(full_uri, version='v1'): """ Take a GCP Compute object's self_link of the form `https://www.googleapis.com/compute/{version}/projects/{project}/{location specifier}/{subtype}/{resource name}` and converts it to its partial URI `{project}/{location specifier}/{s...