content
stringlengths
42
6.51k
def vector_mx_mult(v, m): """ Pre-multiply matrix m by vector v """ rows = len(m) if len(v) != rows: raise ValueError('vectormatmult mismatched rows') columns = len(m[0]) result = [] for c in range(0, columns): result.append(sum(v[r]*m[r][c] for r in range(rows))) ret...
def first_sample_of_frame(frame, frame_shift_in_samples, window_size_in_samples): """ Returns the sample-index of the first sample of the frame with index 'frame'. Caution: this may be negative; we treat out-of-range samples as zero. Analogous to ...
def jaccard_coefficient(x,y): """ Jaccard index used to display the similarity between sample sets. :param A: set x :param y: set y :return: similarity of A and B or A intersect B """ return len(set(x) & set(y)) / len(set(x) | set(y))
def scale_to_range(min_max_old, element, min_max_new=[0, 10]): """Scale element from min_max_new to the new range, min_max_old. Args: min_max_old: Original range of the data, [min, max]. element: Integer that will be scaled to the new range, must be within the old_range. min...
def have_readme(answer): """Check if project has a readme""" return answer['check_readme']
def primes_till_N(N:int): """ Generate all primes less than N Args: N (int): upper bound Returns: list: list of prime numbers """ nums = [True] * N primes = [] for x in range(2,N): if nums[x]: primes.append(x) for i in range(x, len(nums), x)...
def p_spam_given_word(word_prob): """Uses byes therom to compute p(spam | message contains word)""" word, prob_if_spam, prob_if_not_spam=word_prob return prob_if_spam/(prob_if_spam+prob_if_not_spam)
def build_own_answer_detection_prompt(question, answer): """Builds the prompt to check if the model can distinguish between own answers vs answers generated by someone else. Args: answer: String: the model should guess if the answer was generated by the model itself question: String: the questi...
def cleanSection(word,onlywhitespace=False): """Cleans the arguments of the config function call for parsing.""" newword=str() quoted=str() for char in word: if quoted != str(): if char == quoted[len(quoted)-1]: quoted = quoted[:len(quoted)-1] if newwo...
def term_order(figure): """ Return the order of terms a,b,c in a syllogism by figure >>> term_order("1") ['ab', 'bc'] """ if figure == "1": return ["ab", "bc"] if figure == "2": return ["ba", "cb"] if figure == "3": return ["ab", "cb"] if figure == "4": ...
def get_tags_prod_reacts_plain_reaction(eq_reaction): """Get Reaction Species tags for products and reactions from Reaction Engine""" prods_t_s = [ (tag, stoic) for tag, stoic in eq_reaction.items() if stoic > 0 ] reacs_t_s = [ (tag, stoic) for tag, stoic in eq_reaction.items() if stoic ...
def search_linear(xs, target): """ Find and return the index of target in sequence xs """ for (i, v) in enumerate(xs): if v == target: return i return -1
def _is_number(s) -> str: """Returns whether the parameter is a number or string :param s: :return: """ if (isinstance(s, (float, int)) or (s.isdigit() if hasattr(s, 'isdigit') else False)) and not isinstance(s, bool): return 'number' return 'string'
def _is_positive_float(item): """Verify that value is a positive number.""" if not isinstance(item, (int, float)): return False return item > 0
def set_date_range(start_date, end_date): """ Set properly date range @start_date: string date "yyyy-mm-dd" @end_date: string date "yyyy-mm-dd" """ return [{'startDate': start_date, 'endDate': end_date}]
def get_experience_for_next_level(level: int) -> int: """Gets the amount of experience needed to advance from the specified level to the next one. :param level: The current level. :return: The experience needed to advance to the next level. """ return 50 * level * level - 150 * level + 200
def parse_records(database_records): """ Parses database records into a clean json-like structure Param: database_records (a list of db.Model instances) Example: parse_records(User.query.all()) Returns: a list of dictionaries, each corresponding to a record, like... [ {"id...
def completeList(alist): """ This takes a list of strings, and extends the list with lots of permutations of uppercase/lowercase/title formattings. """ alist = list(alist) alist.extend([l+'s' for l in alist]) # add an s on the end, just in case list2 = alist[:] list2.extend([t.lower() for t in alist]) list2.ex...
def extract_last_modified_user(gfile): """ Extractors for the gfile metadata https://developers.google.com/drive/v2/reference/files#resource-representations :param gfile: :return: """ if 'lastModifyingUser' not in gfile: return '' user = gfile['lastModifyingUser'] email = '' ...
def markersdates(markers): """returns the list of dates for a list of markers """ return [m[4] for m in markers]
def selection(unsorted): """Take in an unsorted list and sort it using the selection sort method.""" if len(unsorted) < 2: return unsorted for count in range(0, len(unsorted) - 1): minimum = count for each in range(count, len(unsorted)): if unsorted[each] < unsorted[mini...
def long_substr(strgs): """ Returns a list with the longest common substring sequences from @strgs Based on: http://stackoverflow.com/questions/2892931/longest-common-substring-from-more-than-two-strings-python """ # Copy the list strgs = strgs[:] if len(strgs) > 1 and len(strgs[0]) > 0: ...
def unif_var(a, b): """Uniform distribution variance.""" return (a - b) ** 2 / 12
def bytes_from_int(x: int): """ Convert integer to bytes """ return x.to_bytes((x.bit_length() + 7) // 8, byteorder='big')
def _quadrature_trapezoid(x1, x2, f, norm_func): """ Composite trapezoid quadrature """ x3 = 0.5*(x1 + x2) f1 = f(x1) f2 = f(x2) f3 = f(x3) s2 = 0.25 * (x2 - x1) * (f1 + 2*f3 + f2) round_err = 0.25 * abs(x2 - x1) * (float(norm_func(f1)) + 2*fl...
def is_finish(lst: list) -> bool: """ To be used with the filter function, keep only the bouts that did NOT go the distance in a 3 or 5 round bout. NB: This will also get rid of doctor stoppages between rounds. """ if ( (lst[2] == "3" and lst[3] == "5:00") or (lst[2] == "5" and l...
def is_valid_color(color): """Checks that a given string represents a valid hex colour. :param str color: The color to check. :rtype: ``bool``""" if not color: return False if color[0] != "#": return False if len(color) != 7: return False for char in color[1:]: ...
def get_opts(options): """ Args: options: options object. Returns: args (tuple): positional options. kwargs (map): keyword arguments. """ if isinstance(options, tuple): if len(options) == 2 and isinstance(options[-1], dict): args, kwargs = options ...
def _get_sub_prop(container, keys, default=None): """Get a nested value from a dictionary. This method works like ``dict.get(key)``, but for nested values. Args: container (Dict): A dictionary which may contain other dictionaries as values. keys (Iterable): A sequen...
def is_none_or_white_space(s: str) -> bool: """judge if a str is None or only contains white space. s: source str. return: True if it's None or empty str, otherwise False.""" if s is None: return True tmp = s.strip().rstrip() if tmp is None or tmp == '': return True return Fa...
def remove_from_string(string, letters): """Given a string and a list of individual letters, returns a new string which is the same as the old one except all occurrences of those letters have been removed from it.""" for i in string: for j in letters: if i == j: strin...
def fib_memo(n, memo=None): """ The standard recursive definition of the Fibonacci sequence to find a single Fibonacci number but improved using a memoization technique to minimize the number of recursive calls. It runs in O(n) time with O(n) space complexity. """ if not isinstance(n, int): ...
def quote(val): """quote val""" return f"'{val}'"
def is_in_buid(br_location, ue_location): """ Args: ue_location:the location of the ue (x,y)tuple Returns: False/True: whether the ue is in the building """ x_start = br_location[0] x_end = br_location[1] y_start = br_location[2] y_end = br_location[3]...
def get_complement_prop(numerator: float, denominator: float): """ Get the complement of a fraction. Args: numerator: Numerator for calculation denominator: Denominator for calculation """ # assert numerator <= denominator # assert 0.0 <= numerator # assert 0.0 <= denomina...
def phi_psi_omega_to_abego(phi: float, psi: float, omega: float) -> str: """ :param: phi: The phi angle. :param: psi: The psi angle. :param: omega: The omega angle. :return: The abego string. From Buwei https://wiki.ipd.uw.edu/protocols/dry_lab/rosetta/scaffold_generation_with_piecewise_blue...
def _dict2dict(adj_dict): """Takes a dictionary based representation of an adjacency list and returns a dict of dicts based representation. """ item = adj_dict.popitem() adj_dict[item[0]] = item[1] if not isinstance(item[1], dict): new_dict = {} for key, value in adj_dict.items()...
def get_ids(iterable): """Retrieve the identifier of a number of objects.""" return [element.id for element in iterable]
def level_number(level): """Level: string or number e.g. 'DEBUG'=10,'INFO'=20,'WARN'=30,'ERROR'=40""" if type(level) is str: import logging try: level_number = getattr(logging,level) except: level_number = logging.DEBUG else: level_number = level return level_number
def remove_empty_elements(spec): """ Removes empty elements from the dictionary and all sub-dictionaries. """ whitelist = ['none'] # Don't eliminate artifact's {"archive": {"none": {}}} if isinstance(spec, dict): kids = {k: remove_empty_elements(v) for k, v in spec.items() if v or k in whit...
def topological_sort(items, partial_order): """ Perform topological sort. :param items: a list of items to be sorted. :param partial_order: a list of pairs. If pair (a,b) is in it, it means that item a should appear before item b. Returns a list of the items in one of the possible order...
def hisat2_index_from_prefix(prefix): """ Given a prefix, return a list of the corresponding hisat2 index files. """ return ['{prefix}.{n}.ht2'.format(prefix=prefix, n=n) for n in range(1, 9)]
def convert_verbose(verbose): """Convert the results of the --verbose flag into a list of logger names if --verbose is not provided, args.verbose will be None and logging should be at the info level. if --verbose is provided, but not followed by any arguments, then args.verbose = [] and debug loggi...
def B_icm(r, ne_fn, B_ref=10., r_ref=0., eta=0.5, **kwargs): """ Magnetic field [muG] in the ICM, proportional to a power of the electron number density. r : distance from the center of the cluster [kpc] ne_fn : function for the electron number density [cm^-3] B_ref : reference value of the magneti...
def determineWhatIsDeleted(ref, alt): """Must take the deletion from the ends. """ altIdx = ref.find(alt) if altIdx == 0: # the tail is deleted # AGGTCTG to A # TCCTACACCTACT to TCCTACA so TCCTACA(CCTACT) (want) endDelIdx = altIdx + len(alt) return ref[endDelIdx:]...
def trim_postcode(pc): """Convert a dirty postcode to a valid one but remain the same if invalid. Parameters ---------- pc : str input postcode Returns ------- str A valid postcode or the invalid one. """ pc2 = pc.strip().upper().replace(" ", "") if le...
def _TruncatedString(string: str, n: int = 80) -> str: """Return the truncated first 'n' characters of a string. Args: string: The string to truncate. n: The maximum length of the string to return. Returns: The truncated string. """ if len(string) > n: return string[:n - 3] + '...' else: ...
def elk_index(elk_index_name): """ Index setup for ELK Stack bulk install """ index_tag_full = {} index_tag_inner = {} index_tag_inner['_index'] = elk_index_name index_tag_inner['_type'] = elk_index_name index_tag_full['index'] = index_tag_inner return index_tag_full
def map_sequence(seq, sequence_map, unk_item_id): """ Transform a splitted sequence of items into another sequence of items according to the rules encoded in the dict item2id seq: iterable sequence_map: dict unk_item_id: int""" item_ids = [] for item in seq: item_id = s...
def average(values): """Computes the arithmetic mean of a list of numbers. >>> print average([20, 30, 70]) 40.0 """ return sum(values, 0.0) / len(values)
def synthesize_dsdna(name, sequence): """ Synthesizes a longer stretch of dsDNA, up to 3 kb in size. Parameters ---------- name : str sequence : str Returns ------- dict Notes ----- .. [1] https://www.transcriptic.com/platform/#ordering_assembly """ assert all(...
def get_readable_lines_per_file(lines_per_file: int): """Get the human readable for number of lines per files Parameters ---------- lines_per_file : int number of lines per file """ lpf = lines_per_file units = ["", "k", "m", "b"] i = 0 while lpf >= 1000: lpf = lpf /...
def change_rate_extractor(change_rates, initial_currency, final_currency): """ Function which tests directions of exchange factors and returns the appropriate conversion factor. Example ------- >>> change_rate_extractor( ... change_rates = {'EUR/USD': .8771929824561404}, ......
def gcd(n1, n2): """ Non-Recursive function to return the GCD (greatest common divisor) of n1 and n2 """ if n1 < n2: n1, n2 = n2, n1 while n1 % n2: n1, n2 = n2, n1 % n2 return n2
def make_shape_str(expected_shape) -> str: """Converts the given description of a shape (where each element corresponds to a description of a single dimension) into its human-readable counterpart. Args: expected_shape (tuple[any]): the expected shape of the thing Returns: ...
def _split_by_length(msg, size): """ Splits a string into a list of strings up to the given size. :: >>> _split_by_length('hello', 2) ['he', 'll', 'o'] :param str msg: string to split :param int size: number of characters to chunk into :returns: **list** with chunked string components """ r...
def fader(val, perc1, perc2, perc3, color1, color2, color3): """ Accepts a decimal (0.1, 0.5, etc) and slots it into one of three categories based on the percentage. """ if val > perc3: return color3 if val > perc2: return color2 return color1
def rtrd_str2list(str): """Format Route Target string to list""" if not str: return [] if isinstance(str, list): return str return str.split(',')
def remove_empty_entities(d): """ Recursively remove empty lists, empty dicts, or None elements from a dictionary. :param d: Input dictionary. :return: Dictionary with all empty lists, and empty dictionaries removed. """ def empty(x): return x is None or x == {} or x == [] or x == '' ...
def _stringify_token(token): """ >>> token = {'label': 'TEST', 'values': ('one', 'two')} >>> _stringify_token(token) "{'label': 'TEST', 'values': ('one', 'two')}" """ return str( "{'label': '" + str(token['label']) + "', 'values': " + str(tuple([str(val) for val in token['values']])) + "}" )
def _listrepr(x): """ Represent a list in a short fashion, for easy representation. """ try: len(x) except TypeError: return None else: return '<%d element%s>' % (len(x), 's'[:len(x) >= 2])
def is_iterable(var): """ check if the variable is iterable :param var: :return bool: >>> is_iterable('abc') False >>> is_iterable(123.) False >>> is_iterable((1, )) True >>> is_iterable(range(2)) True """ res = (hasattr(var, '__iter__') and not isinstance(var, str)...
def to_class_path(cls): """Returns class path for a class Takes a class and returns the class path which is composed of the module plus the class name. This can be reversed later to get the class using ``from_class_path``. :returns: string >>> from kitsune.search.models import Record >>> ...
def is_csres_colname(colname): """Returns 'True' if 'colname' is a C-state residency CSV column name.""" return (colname.startswith("CC") or colname.startswith("PC")) and \ colname.endswith("%") and len(colname) > 3
def is_equal_to_as_set(l1, l2): """ return true if two lists contain the same content :param l1: first list :param l2: second list :return: whether lists match """ # Note specifically that set(l1) == set(l2) does not work as expected. return len(set(l1) & set(l2)) == len(set(l1)) and \ ...
def cipher(text, shift, encrypt=True): """ Encrypt and decrypt letter text by using the Caesar cipher. Parameters ---------- text : string A string only contains letters from alphabet shift : int A number that you wish to replace each original letter to the letter with that number o...
def clock_hours(value): """ 2 bytes: seconds: 0 to 59 """ return {'hours': value}
def drop_ventrally_signed(feat_names): """ EM: drops the ventrally signed features Param: features_names = list of features names Return: filtered_names = list of features names without ventrally signed """ absft = [ft for ft in feat_names if '_abs' in ft] ventr = [ft.replac...
def external_trigger_level(session, Type='Real64', RepCap='', AttrID=1150009, buffsize=0, action=['Get', '']): """[External Trigger Level (volts)] The external trigger level, in volts, when Trigger.Source is External. Range of allowable values -1.0 V to 1.0 V. Reset value: 1.0 V. """ return session, Typ...
def get_start_stops(feat, cns): """ return a range dependent on the position of the cns relative to the feature """ if cns[0] > cns[1]: cns = cns[1], cns[0] if feat['start'] < cns[0] and feat['end'] > cns[1]: # intronicns cnsns: return cns[0], cns[1] featm = (feat['start'] + ...
def compute_optalpha(norm_r, norm_Fty, epsilon, comp_alpha=True): """ Compute optimal alpha for WRI Parameters ---------- norm_r: Float Norm of residual norm_Fty: Float Norm of adjoint wavefield squared epsilon: Float Noise level comp_alpha: Bool Whether ...
def MAPk(y_true, y_pred, k=20): """ average precision at k with k=20 """ actual = set(y_true) # precision at i is a percentage of correct # items among first i recommendations; the # correct count will be summed up by n_hit n_hit = 0 precision = 0 for i, p in enumerate(y_pr...
def csv_to_list(s): """Split commas separated string into a list (remove whitespace too)""" return [x for x in s.replace(" ", "").split(",")]
def _match_android(crosstool_top, cpu): """_match_android will try to detect wether the inbound crosstool is the Android NDK toolchain. It can either be `//external:android/crosstool` or be part of the `@androidndk` workspace. After that, translate Android CPUs to Go CPUs.""" if str(crosstool_top) =...
def add_modules_to_metadata(modules, metadata): """ modules is a list of lists of otus, metadata is a dictionary of dictionaries where outer dict keys are features, inner dict keys are metadata names and values are metadata values """ for module_, otus in enumerate(modules): for otu in otus:...
def base36decode(base36_string): """Converts base36 string into integer.""" return int(base36_string, 36)
def get_fitness_score(subject, goal): """ In this case, subject and goal is a list of 5 numbers. Return a score that is the total difference between the subject and the goal. """ total = 0 for i in range(len(subject)): total += abs(goal[i] - subject[i]) return total
def preprocess_text(message): """ Delete some artifacts from text Keyword arguments: message -- a plain sentence or paragraph """ return message.replace("\t", " ").replace("\r\n", " ").replace("\r", " ").replace("\n", " ")
def lp(v1, v2=None, p=2): """Find the L_p norm. If passed one vector, find the length of that vector. If passed two vectors, find the length of the difference between them. """ if v2: vec = {k: abs(v1[k] - v2[k]) for k in (v1.keys() | v2.keys())} else: vec = v1 return sum(v *...
def indexing(smaller, larger, M): """converts x-y indexes to index in the upper triangular matrix""" return larger + smaller * (M - 1) - smaller * (smaller - 1) // 2
def bu_to_mm(x): """Convert blender units to mm.""" return x * 1000.0 if x is not None else x
def get_column2position(lineno: int, text_original: str, text_dedented: str): """the text supplied to parse is 'dedented', and can contain line-breaks, so both the reported lineno and character position may be wrong. This function updates a mapping of a character to its actual place in the document NOT...
def count_rec(nexts, current, part, weight, length): """ INPUT: - ``nexts, current, part`` -- skew partitions - ``weight`` -- non-negative integer list - ``length`` -- integer TESTS:: sage: from sage.combinat.ribbon_tableau import count_rec sage: count_rec([], [], [[2, 1,...
def Strip(mac_oui): """Strip extraneous characters out of MAC/OUI""" data = mac_oui.replace(":","").replace("-","").replace(" ","") return data
def cross(A, B): """Cross product of elements in A and elements in B.""" return [s+t for s in A for t in B]
def get_moves(left, cur_move, limit): """Get possible moves from left of possible length limit.""" if len(cur_move) == limit or not left: return {tuple(sorted(cur_move))} moves = set() if len(cur_move) > 0: moves = moves | {tuple(sorted(cur_move))} for element in left: next_m...
def no_set_task_bar(on=0): """Esconder o Icone Barra de Tarefas e Menu Iniciar DESCRIPTION Esta restricao remove o icone Barra de Tarefas e Menu Iniciar do Painel de Controle e o item Propriedades do menu de contexto do menu Iniciar. COMPATIBILITY Todos. MODIFIED ...
def _sorted(dictionary): """Returns a sorted list of the dict keys, with error if keys not sortable.""" try: return sorted(dictionary) except TypeError: raise TypeError('tree only supports dicts with sortable keys.')
def get_threat_detail(threats): """ Iterate over threat details from the response and retrieve details of threats. :param threats: list of threats from response :return: list of detailed elements of threats :rtype: list """ return [{ 'Title': threat.get('title', ''), 'Catego...
def lorentzian(x_value, amplitude, center, width): """ Evaluate a lorentzian function with the given parameters at x_value. """ numerator = (width**2) denominator = (x_value - center)**2 + width**2 return amplitude * (numerator / denominator)
def remove_punc(comment): """Takes a string and removes all punctuation except for ':' and then returns the result. """ temp = '' punc = '''!()-[]{};'"\<>./?@#$%^&*_~''' for char in comment: if char not in punc: temp = temp+char elif char == "-": tem...
def node_type(node): """ Node numbering scheme is as follows: [c1-c309] [c321-c478] old compute nodes (Sandy Bridge) [c579-c628],[c639-c985] new compute nodes (Haswell) Special nodes: c309-c320 old big memory nodes (Sandy Bridge) c629-c638 new big memory nodes (Haswell) c577,c578 old huge memory nodes (HP Prol...
def _evalPrefix(cad): """ Internal function """ pot={'k':1e3,'M':1e6,'G':1e9,'T':1e12,'P':1e15,'E':1e18 ,'m':1e-3,'u':1e-6,'n':1e-9,'p':1e-12,'f':1e-15,'a':1e-18} if cad in pot: return pot[cad] return None
def update_label2(label2, isubcase): """strips off SUBCASE from the label2 to simplfify the output keys (e.g., displacements)""" # strip off any comments # 'SUBCASE 1 $ STAT' # 'SUBCASE 1 $ 0.900 P' label2 = label2.split('$')[0].strip() if label2: subcase_expected = 'SUBCASE %i' % isu...
def _cal_num_each_train_labal(num_labeled_train, category): """calculate the number of data of each class from the whole labeled data size and the number of class""" return int(num_labeled_train / len(category))
def key_by(items, key, children_key=False): """ Index a list of dicts by the value of given key - forced to lowercase. Set child_key to say 'children' to recurse down a tree with 'children' attributes on nodes. """ index = {} def inner(items, parent_key=None): for item in items: ...
def is_not_round(neuron_max_x_y, coef=2): """Receives the length/width of the object, and returns True if the object is not round. coef: the x/y or y/x maximal ratio.""" x_len, y_len = neuron_max_x_y if (x_len/y_len) > coef or (y_len/x_len) > coef: return True else: return Fal...
def dictify(dict_, dict_type=dict): """Turn nested dicts into nested dict_type dicts. E.g. turn OrderedDicts into dicts, or dicts into OrderedDicts. """ if not isinstance(dict_, dict): return dict_ new_dict = dict_type() for key in dict_: if isinstance(dict_[key], dict...
def _2str(s): """Convert s to a string. Return '.' if s is None or an empty string.""" return '.' if s is None or str(s) == '' else str(s)
def _output_list(data): """Returns a list rather than a numpy array, which is unwanted.""" return [0, 1, 2, 3], {}