content
stringlengths
42
6.51k
def h(x, fx): """helper function as in the PLoS article, doi:10.1371/journal.pcbi.1005352.t003 pg 16/24""" fx = fx % 3 x = x % 3 if fx > x: return x + 1 elif fx < x: return x - 1 else: return x
def parse_flag(value): """ Convert string to boolean (True or false) :param value: string value :return: True if the value is equal to "true" (case-insensitive), otherwise False """ return type(value) == str and value.lower() == "true"
def post_process_botok_segmented_data(segmented_text): """Remove unwanted space from segmented text Args: segmented_text (str): Botok segmented text Returns: str: clean segmented text """ clean_segmented_text = segmented_text.replace('\n ', '\n') clean_segmented_text = clean_se...
def parse_address(text): """ parse name and address from WOS C1 field """ if not text or str(text) == 'nan': return [] state = 'NAME' # NAME | ADDRESS | ADDRESS_END name = '' address = '' results = [] for c in text: if state == 'NAME': if c == ']': ...
def possessive(value): """ Returns a possessive form of a name according to English rules Mike returns Mike's, while James returns James' """ if value[-1] == 's': return "%s'" % value return "%s's" % value
def switch_position_from_wire_position(base, global_pos): """Each switch occupies two wire positions (pos, pos+1); given a wire position (plus, a base for offsetting the switch within subnetwork that created it), this function returns the "canonical" position for the switch, that is, the "upper" positio...
def hund_case_a_landau_g_factor(o, j, s, l, gs, gl): """ Hund case A Landau g-factor .. math:: g = \\frac{\\Omega}{J(J+1)} \\left( g_sS + g_l\\Lambda \\right) Parameters: o (float): Omega of level j (float): J of level l (float): Lambda of level s (float): Sigma...
def frac(h,w,n=5): """ This function takes an image shape as input (h,w) and a integer n. It returns the closest image shape (h_out,w_out) of the original one such that h_out < n and w_out < n. We can then cut the image in h_out*w_out pieces. examples: frac(500,375,5) returns (4,3) frac(...
def find_in_ordered_data(value, array_1d): """Returns the index in the ordered list-like array of value; or None if not present.""" def find_in_subset(value, array_1d, start, end): # recursive binary split if start >= end: return None mid = start + (end - start) // 2 ...
def calculate_rbgs(prbs): """Calculates the number of resource block groups.""" if prbs % 2 == 0: return prbs / 2 else: return (prbs // 2) + 1
def uniform2unit(theta, vmin, vmax): """ mapping from uniform distribution on unit hypercube to uniform distribution on parameter space """ return (theta - vmin) / (vmax - vmin)
def parse_scales_line(line): """ Args: - line: Returns: - scales_dict """ def advance_past_token(str, token): return str[str.find(token) + len(token):] scales_dict = {} line = advance_past_token(line, 'Scales:') pair_str = line.split(',') for pair_str in pair_str: dname, scale = pair_str.split(':')...
def loop_solution(max_number: int) -> int: """Loop solution.""" sum_of_squares = 0 square_of_sum = 0 for i in range(1, max_number + 1): sum_of_squares += i**2 square_of_sum += i square_of_sum = square_of_sum**2 return square_of_sum - sum_of_squares
def rotate(n, rotations=1, width=32): """Bitwise rotate an int. bin(rotate(1, rotations=0)) -> '0b1' bin(rotate(1, rotations=1)) -> '0b10000000000000000000000000000000' bin(rotate(1, rotations=2)) -> '0b1000000000000000000000000000000' bin(rotate(1, rotations=...
def _decode_to_string(to_decode): """ This function is needed for Python 3, because a subprocess can return bytes instead of a string. """ try: return to_decode.decode("utf-8") except AttributeError: # bytesToDecode was of type string before return to_decode
def index_to_position(index, strides): """ Converts a multidimensional tensor `index` into a single-dimensional position in storage based on strides. Args: index (array-like): index tuple of ints strides (array-like): tensor strides Return: int : position in storage """ ...
def is_armstrong(number): """ Verifies if given number is armstrong number or not. Args: number: number which we want to test Returns: true if given number is armstrong number. """ total_sum = 0 str_number = str(number) length = len(str_number) for num in str_numbe...
def quantize_tick_up(tick, grid): """ Quantizes a given tick number to the closest higher tick on the grid. For example, for ``tick=900`` and ``grid=480``, this returns ``960``. :param tick: The tick number. :param grid: The grid to be quanitzed to. :returns: The closest higher tick on the gri...
def format_join(grouped): """Formatted output: join factors and exponents.""" joined = [] for group in grouped: group = '^'.join(map(str, group)) joined.extend([group]) joined = ' * '.join(map(str, joined)) return joined
def opposite_dir(d): """Return opposite direction.""" opposites = dict(n='s', e='w', s='n', w='e') return opposites[d]
def sorted_dict_repr(d:dict, order:list): """ Representer of dictionary `d`, with key order `order`. Example: ---------- >>> sorted_dict_repr({'a': 1, '0': 3}, ['0', 'a']) "{'0': 3, 'a': 1}" """ return '{' + ', '.join([f"{repr(k)}: {repr(d[k])}" for k in order]) + '}'
def token_accuracy(hypotheses, references, level="word"): """ Compute the accuracy of hypothesis tokens: correct tokens / all tokens Tokens are correct if they appear in the same position in the reference. :param hypotheses: list of hypotheses (strings) :param references: list of references (string...
def calc_benjamini_hochberg_corrected_value(p_value, index, total_num_tests): """ Perform the k-calculation for Benjamini-Hochberg correction. See http://en.wikipedia.org/wiki/False_discovery_rate#Independent_tests for more detail. :Parameters: - `p_value`: the uncorrected p-value ...
def preprocessligpdb(Ligand_list): """It takes the list of ligand PDB filenames and process them for the matching PARAMETERS ---------- Ligand_list : list of strings list of ligand PDB filenames RETURNS ------- lig_aux : list of ligand PDB filenames processed """...
def determine_percentage(numerator, other): """ Determines a fraction. :param numerator: The numerator of the calculation :type numerator: int :param other: The values that will be added to the numerator to calculate the denominator :type other: int :return: The fraction of the numerato...
def merge(list1, list2): """ Merge two sorted lists. Returns a new sorted list containing all of the elements that are in either list1 and list2. This function can be iterative. """ merged = [] idx1 = 0 idx2 = 0 while idx1 < len(list1) and idx2 < len(list2): ...
def crlf_lines(physical_line): """ Line contains CR (e.g. as a CRLF line ending). Many free software projects have a strong focus on POSIX platforms (like Linux, *BSD, Unix, Mac OS X, etc.) and they all use LF-only line endings. Only Win32 platform uses CRLF line endings. So if you have a Win32...
def within_threshold(pos,item): """ Function: check if the tip of pressed is within the threshold of piano key boundaries Arguments: pos: x,y pixel coordinates of the tip of finger item: boundaries of bbox of a particular key of a piano returns: boolean value""" ...
def ifib(n): """Iterative function""" a, b = 0, 1 if n > 1: for i in range(n): a, b = b, a + b return a return 1
def map_llc_display_result_to_dictionary_list(land_charge_result): """Produce a list of jsonable dictionaries of an alchemy result set """ if not isinstance(land_charge_result, list): return list(map(lambda land_charge: land_charge.to_display_dict(), [land_charge_result])) ...
def calculate_trend(series_range, series_list): """ With the given range and list, calculate the linear regression such that the returned value illustrates if the numbers are trending positive or negative. :param series_range: A range of values against which to calculate a linear regression. :param...
def filter_out_none(dictionary, keys=None): """ Filter out items whose value is None. If `keys` specified, only return non-None items with matched key. """ ret = {} if keys is None: keys = [] for key, value in dictionary.items(): if value is None or key not in keys: ...
def sort(profiles, sorting_criteria, lang_dict): """ sort profiles that were filtered by show function some of the categories: name a-z,name z-a, age low-high etc. """ list_of_values = list() sorted_profiles = list() if sorting_criteria == lang_dict["sort_prof_tuple1"]: for...
def bipartite_match(graph): """ Program to find the bipartite match. Hopcroft-Karp bipartite max-cardinality matching and max independent set David Eppstein, UC Irvine, 27 Apr 2002. Find maximum cardinality matching of a bipartite graph (U,V,E). The input format is a dictionary mapping members...
def word(i: int) -> int: """Return an unsigned 16 bit word of an integer. """ if i < 0: i = (-i ^ 0xFFFF) + 1 return i & 0xFFFF
def flatten(S): """ Helper function to recursively flatten a list :param S: The nested list :return: The flattened list """ if S == []: return S if isinstance(S[0], list): return flatten(S[0]) + flatten(S[1:]) return S[:1] + flatten(S[1:])
def get_name(your_name): """Pass `your_name` parameter as output.""" return { "name": your_name, }
def get_create_table_field_data(field_data): """ Generates the field wise query segments needed to create a table. :param field_data: List of dicts with each dict having keys 'name', 'type' and, optionally, 'modifiers' :return: none """ field_query_list = [] for field in field_data: ...
def get_add_term_cal_Fz(uZ_nodim, membrane_geometry): """ This is a proper adoption of particle-flux conservation law on mBLA method. The actual use of it is cal_Fz, which is related to the under-relaxed fixed-point iteration. """ if membrane_geometry=='FMS': return (2/3.)*(1. - uZ_nodim) re...
def node_id(node): """ mapping of name of hydrophone node to ID Parameter --------- node : str name or ID of the hydrophone node Returns ------- str ID of hydrophone node """ # broadband hydrophones if node == "Oregon_Shelf_Base_Seafloor" or node == "LJ01D":...
def sfc_rad(swup_sfc, swdn_sfc, lwup_sfc, lwdn_sfc): """All-sky surface upward radiative flux.""" return swup_sfc - swdn_sfc + lwup_sfc - lwdn_sfc
def get_pretrained_index_weight(word_pretrained_index, words_weight): """ Get the map from word index in pretrained embeddings and weights """ index_weights = {} for word, idx in word_pretrained_index.items(): if word in words_weight: index_weights[idx] = words_weight[word] else:...
def formed_bond_keys(tra): """ keys for bonds that are formed in the transformation """ #print('tra test:', len(tra), tra) #if len(tra) == 1: # frm_bnd_keys = tra #else: frm_bnd_keys, _ = tra return frm_bnd_keys
def has_won_recursive(board): """Validate NxN board recursively.""" def find(row, col, move, count, x_or_o): def valid_sq(): if -1 < row < len(board) and \ -1 < col < len(board) and \ -1 < count <= len(board)and \ x_or_o == board[row][col]: return True return ...
def _gen_table_cols(col_ids): """Generate Dash table columns in the expected format. :param col_ids: list of columns; must be in format <table-alias.name>, like "s.serial_number", as in the SQL select statement -- except for derived column values which must literally ...
def dfs_traverse_recursive(graph, start, visited=None): """ Traversal by recursive depth first search. """ if visited is None: visited = set() visited.add(start) for next_node in graph[start]: if next_node not in visited: dfs_traverse_recursive(graph, next_node, visit...
def _escape_char(in_c): """ Escape some special characters in java .properties files. :param in_c: Input character >>> "\\:" == _escape_char(':') True >>> "\\=" == _escape_char('=') True >>> _escape_char('a') 'a' """ return '\\' + in_c if in_c in ('"', '\'', '\\') else in_c
def test_ident(i): """Is the unicode string valid in a Python 3 identifier.""" # Some characters are not valid at the start of a name, but we still want to # include them. So prefix with 'a', which is valid at the start. return ('a' + i).isidentifier()
def word_lengths(lst): """Return a list of word lengths.""" return [len(str_) for str_ in lst]
def to_dict(dictish): """ Given something that closely resembles a dictionary, we attempt to coerce it into a propery dictionary. """ if hasattr(dictish, 'iterkeys'): m = dictish.iterkeys elif hasattr(dictish, 'keys'): m = dictish.keys else: raise ValueError(dictish) ...
def none_if_empty(tup): """Returns None if passed an empty tuple This is helpful since a SimpleVar is actually an IndexedVar with a single index of None rather than the more intuitive empty tuple. """ if tup is (): return None else: return tup
def get_store_key(key): """Return the key to use with homeassistant.helpers.storage.Storage.""" return key if "/" in key else f"hacs.{key}"
def _add_dot(ext_list): """Add a dot (.) to the beginning of each extension in a list. Args: ext_list (list): A list of file extensions. Returns: A list of extensions, with a dot prepended to each extension, if it doesn't already exist. """ # LOOP THROUGH EXTENSIONS fo...
def remove_nulls(data): """ Removes those key value pairs from data where value is None :param data: :return: the cleaned up data """ null_keys = [] for key, val in data.items(): if val == None: null_keys.append(key) for key in null_keys: del data[key] re...
def multiplicative_inverse(a, b): """Returns a tuple (r, i, j) such that r = gcd(a, b) = ia + jb """ # r = gcd(a,b) i = multiplicitive inverse of a mod b # or j = multiplicitive inverse of b mod a # Neg return values for i or j are made positive mod b or a respectively # Iterateive Ver...
def _dict_sub(left, right): """ >>> _dict_sub({'a': 21}, {'a': 7}) {'a': 14} :param left: :param right: :return: """ new = left.copy() for key, value in right.items(): new[key] -= value return new
def metric_max_over_ground_truths(metric_fn, predictions, ground_truths): """Take the average best score against all ground truth answers. This is a bit different than SQuAD in that there are multiple answers **and** predictions that we average over. For some situations (e.g., *top k* beams or multiple human r...
def find_min_iterative(array, left, right): """ Find the minimum in rotated array in O(log n) time. >>> find_min_iterative([1,2,3,4,5,6], 0, 5) 1 >>> find_min_iterative([6, 5, 4, 3, 2, 1], 0, 5) 1 >>> find_min_iterative([6, 5, 1, 4, 3, 2], 0, 5) 1 """ while array[left] > array[right]...
def clustercenter(samples): """ Computes the geometric center of a set of vectors. samples = [ [v11,...,v1N], ... [vn1,...,vnN] ] """ N, dim = len(samples), len(samples[0]) if N == 1: # singleton cluster return samples[0] # Cluster center is the average in all dimensions dsum = [0.0] * dim for d...
def is_in(elements, value): """ Determines if a value is in a list. It also handles degenerate cases when instead of a list, elements is True, False or None """ if not elements: return False elif elements is True: return True else: return value in elements
def _setUnits(value): """ Convert a value in bytes so a human-readable format with units. """ if value > 1024.0**3: value = value / 1024.0**3 unit = 'GB' elif value > 1024.0**2: value = value / 1024.0**2 unit = 'MB' elif value > 1024.0**1: value = value / 1024.0*1 unit = 'kB' else: unit = ' B' r...
def verify_non_decreasing(arr: list) -> bool: """ O(n) & O(n) """ len_arr = len(arr) if len_arr < 3: return True stack = [] count_del = 0 for element in arr: while stack and stack[-1] > element: count_del += 1 stack.pop() if count_del >...
def scale(pnt, frac): """scale a point""" return pnt[0] * frac, pnt[1] * frac
def get_middle_indexes(lst): """ Fetch indexes of one or two middle numbers of a list. """ n = len(lst) if n <= 2: return [None] if n % 2 == 0: return [n / 2 - 1, n / 2] else: return [n // 2]
def subset_dict(dictionary, keys): """ Returns a dict that contains all key,value pairs in dictionary where the key is one of the provided keys. This is used by some of the methods in message.py. """ keys = set(keys) return {key: value for key, value in dictionary.items() if key in keys}
def parse_git_url(url): """ return (organization, repository) tuple from url line of .git/config file """ if url.startswith("git@"): # deal with git@github.com:org/repo.git url = url.split(":")[1] org, repo = url.rstrip(".git").split("/")[-2:] return org, repo
def tri_reflect_y(a, b, c): """Reflects the given triangle through the x-axis and returns the co-ordinates of the new triangle""" return (1 - c, 1 - b, 1 - a)
def sign(x) -> int: """Return the sign of the argument. [-1, 0, 1]""" return x and (1, -1)[x < 0]
def content_function(obj): """Just return content""" _, _, _, content = obj return 'content', content
def is_breakpoint(BREAKPOINTS, PC): """ Determine if the current programme counter is at a predetermined breakpoint. Parameters ---------- BREAKPOINT : list, mandatory A list of the predetermined breakpoints PC: int, mandatory The current value of the program counter Re...
def get_chunk_type(tok, idx_to_tag): # method implemented in https://github.com/guillaumegenthial/sequence_tagging/blob/master/model/data_utils.py """ Args: tok: id of token, ex 4 idx_to_tag: dictionary {4: "B-PER", ...} Returns: tuple: "B", "PER" """ tag_name = idx_to_ta...
def human_readable(additions): """Print additions in a human-readable way Parameters ---------- additions : list Additions to a given word Returns ------- str Easily readable additions """ result = "" for a in additions: result = result + a["letter"].upp...
def distanceSquared(a,b): """Squared L2 distance""" if len(a)!=len(b): raise RuntimeError('Vector dimensions not equal') sum=0 for i in range(len(a)): sum = sum + (a[i]-b[i])*(a[i]-b[i]) return sum
def open_file(file): """ Receives 1 parameter: a file (mainly .txt). Then it opens the file, reads it's lines, returns the lines through the "text" variable, and closes the file automatically. """ with open(file, "r") as f: text = f.readlines() return text
def make_jinja2_filename(file_name: str) -> str: """ Add .jinja2 to a filename. :param file_name: the filename without an extension. :return: the filename. """ return f'{file_name}.jinja2'
def GetExonsRange(exons, first, last, full=True, min_overlap=0, min_exon_size=0): """get exons in range (first:last) (peptide coordinates). Set full to False, if you don't require full coverage. """ new = [] me = 3 * min_exon_...
def l2_norm(lst): """ Calculates the l2 norm of a list of numbers """ return sum([x*x for x in lst])
def left_to_right_check(input_line: str, pivot: int) -> bool: """ 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 o...
def to_tterm(t): """ turns into hashable term""" if not isinstance(t, tuple): return t h, bs = t cs = map(to_tterm, bs) ds = tuple(cs) return (h, ds)
def partition(lst, fn): """Partition lst by predicate. - lst: list of items - fn: function that returns True or False Returns new list: [a, b], where `a` are items that passed fn test, and `b` are items that failed fn test. >>> def is_even(num): ... return num % ...
def get_last_byte(buf): """Get last 1byte value.""" return ord(buf[-1:])
def sign (x): """Return `-1` if `x < 0`, `0` if `x == 0` and `1` if `x > 0`.""" return 0 if x == 0 else (1 if x > 0 else -1)
def integer_at_least(actual_value, expected_value): """Assert that actual_value is an integer of at least expected_value.""" result = isinstance(actual_value, int) if result: result = actual_value >= expected_value if result: return result else: raise AssertionError( ...
def post_data(data): """ Take a dictionary of test data (suitable for comparison to an instance) and return a dict suitable for POSTing. """ ret = {} for key, value in data.items(): if value is None: ret[key] = "" elif type(value) in (list, tuple): if value a...
def mk_or_expr(expr1, expr2): """ returns an or expression of the form (EXPR1 \/ EXPR2) where EXPR1 and EXPR2 are expressions """ return {"type" : "or" , "expr1" : expr1 , "expr2" : expr2 }
def get_y_values(x: int, slopes: list, coordinates: list, edge_count: int) -> list: """ Calculate the y value of the current x from each edge :param x: x-coordinate of the current node :param slopes:a list of slopes of all edges of the polygon :param coordinates: a list of vertices of the polyg...
def swsphericalh_A(s, l, m): """ Angular separation constant at a=0. Eq. (50). Has no dependence on m. The formula is A_0 = l(l+1) - s(s+1) Parameters ---------- s: int Spin-weight of interest l: int Angular quantum number of interest m: int Magnetic quantum numbe...
def time_format(s): """ Takes seconds data and formats it into days, hours, minutes and seconds """ minutes, seconds = divmod(s, 60) hours, minutes = divmod(minutes, 60) days, hours = divmod(hours, 24) if s < 3600: return f'{minutes}m' elif s < 86400: return f'{hours}h {m...
def add_char(num, char=" "): """Creates a string value give a number and character. args: num (int): Amount to repeat character kwargs: char (str): Character value to loop Returns (str): Iterated string value for given character """ string = "" for i in range(num): st...
def turn(p1, p2, p3): """ 0 if the points are colinear 1 if the points define a left-turn -1 if the points define a right-turn """ # Compute the z-coordinate of the vectorial product p1p2 x p2p3 z = (p2[0] - p1[0]) * (p3[1] - p1[1]) - (p2[1] - p1[1]) * (p3[0]- p1[0]) return 0 if z == 0 e...
def remove_unique_entries(A,B): """ Remove any entries that only exists in one of the data sets """ A_common, B_common = {}, {} common_keys = list(set(A.keys()) & set(B.keys())) sorted_keys = sorted(common_keys) for key in sorted_keys: A_common[key] = {} B_common[key] = {} ...
def _sorted_pe(inlist): """ Generate suitable inputs to ``3dQwarp``. Example ------- >>> paths, args = _sorted_pe([ ... ("dir-AP_epi.nii.gz", {"PhaseEncodingDirection": "j-"}), ... ("dir-AP_bold.nii.gz", {"PhaseEncodingDirection": "j-"}), ... ("dir-PA_epi.nii.gz", {"PhaseEnc...
def substractArray(array1, array2): """ Substracts array2 of array1. That means, that every number in array1, which is also present in array2 is deleted """ result = array1.copy() for i in range(0,len(array2)): while (result.count(array2[i]) > 0): result.remove(array2[i]) ...
def str_to_grid(sudoku, fill_possibilities=False): """ Convert an 81-char string sequence representing sudoku into a grid (dict) with keys as indexes [0-80] and values as possible assignments. Valid values are [1-9] for filled cells. All other characters represent an empty cell. """ if len(s...
def process_contact_line(line): """Return the contact counts for a line of ibuContacts data.""" return [int(field) for field in line.split()]
def get_user_full_name(username: str) -> str: """Returns a user's full name given a username or original value if not found""" full_name = username with open("/etc/passwd") as f: for line in f: if line.split(":")[0] == username: full_name = line.split(":")[4].strip("/n") ...
def linear_model(slope, x0, x): """Returns the function value of a linear model. The model is y=slope*x + x0. Parameters: * slope: the slope of the linear function * x0: the y-value at x=0 * x: the x value for which the y value should be determined """ return slope * x + x0
def hex_to_rgb(value="FFFFFF"): """Converts hex color to RGB color. Args: value (str, optional): Hex color code as a string. Defaults to 'FFFFFF'. Returns: tuple: RGB color as a tuple. """ value = value.lstrip("#") lv = len(value) return tuple(int(value[i : i + lv // 3], 16...
def get_cost(prices, item, quantity): """ Calculates cost of item based on quantity Args: prices (dict): {item_code: price} item (str): item_code quantity (int): quantity of item in basket """ return quantity * prices[item]
def find_nouns(tagged_words, skip_words_in_nlp = 0): """ Compile a list of nouns in tagged word list Note: treebankTagger works better than standard nltk.pos_words(). Tags can be compiled with treebank_tagger = nltk.data.load( 'taggers/maxent_treebank_pos_tagger/english.pickle') ...