content
stringlengths
42
6.51k
def circle_coordinates(x, y, radius): """Calculates the bounds of a circle, given centre and radius.""" x1 = x - radius # Left x2 = x + radius # Right y1 = y - radius # Bottom y2 = y + radius # Top return (x1, y1, x2, y2)
def check_property_differences(spec_properties, dev_properties, bidirectional): """Compare properties in the parsed YAML Parameters ---------- spec_data : list List of dictionaries with specification data properties E.g [{'name': 'AdminModeDefault'}, {'name': 'AsynchCmdRepl...
def bool_str(val): """CloudFormation Template formatted boolean string. CloudFormation uses all lowercase for boolean values, which means that str() will not work correctly for boolean values. Args: val (bool) : Boolean value to convert to a string Returns: (str) : String of repre...
def kb_ids2known_facts(kb_ids): """Creates list of all known facts from kb dict""" facts = set() for struct in kb_ids: arrays = kb_ids[struct][0] num_facts = len(arrays[0]) for i in range(num_facts): fact = [x[i] for x in arrays] facts.add(tuple(fact)) re...
def is_casava_v180_or_later(header_line): """Check if the header looks like it is Illumina software post-casava v1.8 Parameters ---------- header_line : bytes A header line Returns ------- bool ``True`` for if casava v1.8+, otherwise ``False`` Examples -------- ...
def combine_dictionaries(dict1,dict2): """append lists that share the same key, and add new keys WARNING: this only works if the dictionaries have values that are lists""" outdict = dict1 for key in dict2: if key in outdict: assert(isinstance(dict2[key],list))...
def filter_training_seats_only(queryset, name, seats): """Limit Memberships to only entries with some training seats allowed.""" if seats: return queryset.filter(instructor_training_seats_total__gt=0) else: return queryset
def cleanup_data(obj): """Removes the None values from the object and returns the object Args: obj: object to cleanup Returns: object: cleaned object """ if isinstance(obj, (list, tuple, set)): return type(obj)(cleanup_data(x) for x in obj if x is not None) elif isinstanc...
def _flatten_nested_iterable(struct): """ :param struct: :return: """ result = [] for item in struct: if hasattr(item, '__iter__') and not isinstance(item, str): result.extend(_flatten_nested_iterable(item)) else: result.append(item) return result
def fixed_width_repr_of_int(value, width, pad_left=True): """ Format the given integer and ensure the result string is of the given width. The string will be padded space on the left if the number is small or replaced as a string of asterisks if the number is too big. :param int value: An inte...
def min_cats(sorted_counts, N=1): """Given list of tuples of cats, return a list of cats that appear more frequently than N times (1 by default). Also return a list of tuples""" out_list = [] out_tup_list = [] for i in sorted_counts: if i[1] > N: out_list.append(i[0]) ...
def strip_if_string(val): """ :param val: any :return: str|None """ if isinstance(val, str): val = val.strip() if '' == val: val = None return val
def _build_hasheable_corpus(corpus): """Hashes and get `corpus`. Parameters ---------- corpus : list of list of (int, int) Given corpus. Returns ------- list of list of (int, int) Hashable corpus. """ return [tuple(doc) for doc in corpus]
def join_neighs_XOR_notrelpos(idxs0_ki, idxs1_ki): """Join neighs with XOR. Parameters ---------- idxs0_ki: list or np.ndarray the indices of the neighs of neighbourhood0 idxs1_ki: list or np.ndarray the indices of the neighs of neighbourhood1 Returns ------- neighs: li...
def hasProperty(propertyName, typeObj): """check up if the as parameter given type has a property with the given name Keyword arguments: propertyName -- name of the property to look for typeObj -- type object to check up """ if not hasattr(typeObj, 'properties'): return False f...
def _get_short_cid( container_id ): """returns a shortened container id. Useful for logging, where using a full length container id is not necessary and would just add noise to the log. The shortened container id will contain enough information to uniquely identify the container for most situa...
def compress(nodelist): """ compress will return a hostlist string given a list of hostnames. :param: nodelist: The hostlist string. :return: The hostlist string. """ if type(nodelist) == str: left_br = nodelist.replace("[", "") right_br = left_br.replace("]", "") nodel...
def get_clf_mode(train, test): """ Detect whether we are in single-label to single-label mode or not. """ first = "single" for example in train: if example.get("labeled", "multi") == "multi": first = "multi" for example in test: if example.get("labeled", "multi") == "multi": ...
def get_dict_properties(item, fields, mixed_case_fields=[], formatters={}): """Return a tuple containing the item properties. :param item: a single dict resource :param fields: tuple of strings with the desired field names :param mixed_case_fields: tuple of field names to preserve case :param forma...
def click_dfs(field, num_rows, num_cols, given_i, given_j): """ Time: O(num_rows * num_cols) Space: O(num_rows * num_cols) """ if 0 <= given_i < num_rows and 0 <= given_j < num_cols and field[given_i][given_j] == 0: field[given_i][given_j] = -2 else: return field for i in ...
def rgb2htmlcolor(rgb_tuple): """ convert an (R, G, B) tuple to #RRGGBB """ return '#%02x%02x%02x' % rgb_tuple
def get_area(ptlist): """ Calculate the area of a polygon defined by a list of points. The variable ptlist is a list of (x, y) point pairs. Be careful, the implementation can give unexpected results with self-intersecting polygons. The output will always be non-negative. Created: 2015 April 2...
def get_cleaned_log(raw_log): """ Method to clean the log from stray spaces and new lines :param raw_log: :return: list of string without stray spaces and new lines """ return [line.strip() for line in raw_log if line.strip()]
def find_active_desktop(desktops): """Returns the currently active desktop. Args: desktops: A list containing all Desktop instances. Returns: An instance of Desktop representing the currently active desktop or None if it can't be found. """ for d in desktops: if d.active: ...
def check_if_hits(row, column, fleet): """ This method checks if the shot of the human player at the square represented by row and column hits any of the ships of fleet. :param row: int :param column: int :param fleet: list :returns result: bool - True if so and False otherwise "...
def format_internal_tas(row): """ Concatenate TAS components into a single field for internal use. Args: row: row of data with TAS elements Returns: TAS components concatenated into a single string """ # This formatting should match formatting in dataactcore.models....
def parse_fixture_line(line): """ Parses hold specifications. Line format is <universe> <address> <panel number> <x> <y> [<route number>] """ len_minus_routes = 5 words = line.split() if len(words) > 0 and words[0] == "#": return None assert len(words) >= len_minus_routes strand,...
def str_to_float(item: str) -> float: """[summary] Args: item (str): [description] Returns: float: [description] """ """Converts a str to a float.""" return float(item)
def get_1rep_max(weight, reps=1): """Reduces weight and rep combination to 1-rep-max weight. """ return int(weight * (reps-1) * .033 + weight)
def validate_passphrases(passphrases, validator): """Validate passphrases with validator function.""" return [passphrase for passphrase in passphrases if validator(passphrase)]
def apply_rules(l, m, r, rules): """Apply selected rule to cell Apply rule to cell given its current state m and neighbours states l and r. Args: l: left neighbour cell state. m: current cell state. r: right neighbour cell state. rules: array current rule. ...
def dev_dupe_dicter(finals): """ Prepare dictionary to clean duplicate autoloaders. :param finals: Dict of URL:content-length pairs. :type finals: dict(str: str) """ revo = {} for key, val in finals.items(): revo.setdefault(val, set()).add(key) return revo
def _roitmpl2roiname(roitmpl): """ generate roiNames out of roitempl written in an amazing function to easyly change pattern for roiNames in all further function at once. This way keep the featureAttributes (fa) consistent """ roiRname = 'Right'+roitmpl roiLname = 'Left...
def compresoneadjtuple(s): """useful to compress adjacent entries""" if len(s) < 1: return s, True finals=[] for pos in range(len(s)-1): firstt, secondt = s[pos],s[pos+1] # if (firstt[1]==secondt[0]) or (firstt[1]+1==secondt[0]): if (firstt[1] == secondt[0]): finals.append((firstt[0],secondt[1...
def find_base_style(masters): """Find a base style shared between all masters. Return empty string if none is found. """ if not masters: return "" base_style = (masters[0].name or "").split() for master in masters: style = master.name.split() base_style = [s for s in styl...
def country_list(cities_data): """Returns a list of all the countries represented in cities_data. """ countries = [] for r in cities_data: if r['country'] not in countries: countries.append(r['country']) return countries
def get_list_from_config(name, config): """ Gets a list item from config. If it doesn't exist, gets empty list. If it is not a list, wrap it in a list """ result = config[name] or [] if not isinstance(result, list): result = [result] return result
def dot(x, y): """ Sum of multiplying X and Y elementwise. :param list or tuple x: 1st array. :param list or tuple y: 2nd array. :return: sum of multiplied array. :rtype: int or float :raise ValueError: when x or y is empty """ if x and y: return sum([i * j for i, j in zip(x...
def standard_codec_name(name: str) -> str: """ Map a codec name to the preferred standardized version. The preferred names were taken from this list published by IANA: U{http://www.iana.org/assignments/character-sets/character-sets.xhtml} @param name: Text encoding name, in lower case. ...
def get_title(video): """Return title""" print('Trying: ' + video['title']) if ' | ' in video['title']: return video['title'].split(' | ')[1] elif ' - ' in video['title']: return video['title'].split(' - ')[1] else: return video['title']
def getValueTryRemoveTailZero(float_num): """ try to remove .0 from an float number, 2.00 -> 2 keep other float as it was, 2.02 -> 2.02 :param float float_num :return float/int """ int_num = int(float_num) if int_num == float_num: return int_num return float_...
def ndvi(b4, b8): """ Normalized Difference Vegetation Index (Rouse Jr et al., 1974). .. math:: NDVI = (b8 - b4)/(b8 + b4) :param b4: Red. :type b4: numpy.ndarray or float :param b8: NIR. :type b8: numpy.ndarray or float :returns NDVI: Index value .. Tip:: Rouse, J. W., ...
def constant_time_compare(val1, val2): """ Returns True if the two strings are equal, False otherwise. The time taken is independent of the number of characters that match. For the sake of simplicity, this function executes in constant time only when the two strings have the same length. It short-circuits when t...
def accuracy(y, yhat): """Returns the accuracy. Higher is better. :param y: true function values :param yhat: predicted function values """ return float(sum(map(lambda x: x[0] == x[1], zip(y, yhat)))) / len(y)
def potatoes(p0, w0, p1): """ - p1/100 = water1 / water1 + (1 - p0/100) * w0 => water1 = w0 * p1/100 * (1 - p0/100) / (1 - p1/100) - dry = w0 * (1 - p0/100) - w1 = water1 + dry = w0 * (100 - p0) / (100 - p1) Example: 98/100 = water1 / water1 + (1- 99/100) * 100 water1 = 49 w1 = 49...
def decode_string(string, encoding=None): """Decode a string with specified encoding :type string: str or bytes :param string: string to decode :param str encoding: encoding of string to decode :rtype: str :return: decoded string """ if isinstance(string, str): return string ...
def deep_get(target_dict, *args, **kwargs): """ Get a value from target_dict entering in the nested keys. If keys does not exist, it returns None Example target_dict={a: {b: 5}}; key_list=[a,b] returns 5; both key_list=[a,b,c] and key_list=[f,h] return None :param target_dict: dictionary to be read ...
def concat_block_texts(blocks: list) -> str: """Combine child block texts to get the text for an abstract block.""" return " ".join([b["Text"] for b in blocks])
def compare(lhs, rhs): """Implements cmp() for Python 2 and 3 alike""" if lhs == None: if rhs == None: return 0 else: return -1 else: if rhs == None: return 1 else: return (lhs > rhs) - (lhs < rhs)
def _merge_peaks(l): """ Merge signals if the difference of nuclear to cytoplasmic ratio is 1 """ idx = [] while len(l)>0: first, *rest = l first = set(first) lf = -1 while len(first)>lf: lf = len(first) rest2 = [] for r in rest: ...
def get_shared_link_header(shared_link, password=None): """ Gets the HTTP header required to use a shared link to grant access to a shared item. :param shared_link: The shared link. :type shared_link: `unicode` :param password: The password for the shared link. :type pas...
def get_url_without_scheme(url: str) -> str: """Get the target url without scheme @type url: str @param url: The target URL @returns str: The url without scheme """ if '://' in url: return url[(url.index('://')+3):] return url
def depth_to_sample(depth,depth_data): """ Convert depth to sample index """ return int((depth - depth_data['depth_start']) / depth_data['depth_per_pixel'] - 0.5)
def backbone_edges(dot_bracket: str): """Return the RNA backbone edges of the dot-bracket string.""" num_pos = len(dot_bracket) n1 = range(num_pos - 1) n2 = range(1, num_pos) return list(zip(n1, n2))
def load_result_to_json(keywords, segmentation_result): """Function that converts the segmentation results (keywords and text blocks) into json :param keywords: resulting or put keywords :param segmentation_result: resulting text blocks :return: result in json """ id_segment = [] for id, se...
def get_tiles(tile_rangs): """get each scene id and the tile x y bounds Args: tile_rangs(list): save scene id and x y bounds ###########tile_range####### #[['20200529_003832_100d', [[53910, 53961], [24896, 24921]]]]# Returns: tile_xyz(list): a list contains scene id, x, y, z. #...
def max_cmp(L, cmp=None): """ Returns the largest item of a list (or iterable) with respect to a comparison function. INPUT: ``L`` -- an iterable ``cmp`` -- an optional comparison function. ``cmp(x, y)`` should return a negative value if `x < y`, `0` if `x == y`, and a positi...
def format_by_line_length(possible_votes, max_length=60): """ Note: I've tried to format with a nice aligned table but it's not possible to get it right (unless you hardcode it maybe) because the font used in the game does not have consistent characters (varying width) """ lines = [] line =...
def _NormalizeArgd(rawDict): """ Normalize the argument dictionary. All parameters start with '#' will be checked whether it is not None(client did not pass it). After checking a new dictionary will be generated and returned. :param rawDict: a dict, contains request args with required attribute flag...
def _count(bs) -> int: """Given a sequence of bools, count the Trues""" return sum(1 for b in bs if b)
def sub_brackets(x): """Reformats 'a[0]' to 'a_0'""" return x.replace("[", "_").replace("]", "")
def escape_perl_string(v): """Escape characters with special meaning in perl""" return str(v).replace("$","\\$").replace("\"","\\\"").replace("@","\\@")
def parse_slide_pipes(slide_desc_str): """ Parse slide content. This function parses the content of a slides, constructs the visual representation. """ tile_tokens = slide_desc_str.split("-") return tile_tokens
def get_video_url(array): """ get video url from list :param array: :return: """ if isinstance(array, list) and len(array) >= 1: return array[-1] return None
def unigram(wordcount_dict, one_doc_length): """ Compute probabilities of words in one document, and return a dictionary of term probability. """ wordprob_dict = {} word_instance = len(list(wordcount_dict.keys())) for word, wordcount in wordcount_dict.items(): # prob = lidston_smoothing(wordcoun...
def replace_line_breaks(string): """replaces: '\r\n' and '\n\r' and '\r' and '\n' and with '<br />' """ r = '<br />' return string.replace('\r\n', r).replace('\n\r', r).replace('\r', r).replace('\n', r)
def lstrip_list(s): """ Return list with empty items from start of list removed. """ for i in range(len(s)): if s[i]: break else: return [] return s[i:]
def _escape_token(token, alphabet=None): """Escape away underscores and OOV characters and append '_'. This allows the token to be expressed as the concatenation of a list of subtokens from the vocabulary. The underscore acts as a sentinel which allows us to invertibly concatenate multiple such lists. ...
def count_occurences(data, feature): """Counts how often each unique value of a feature occurs in the whole dataset. Args: data: The dataset to count. feature: The feature to count. Returns: A dictionary where the keys are the data values and the values are the occurenc...
def get_hsc_psf_url(ra, dec, band='i', rerun='', tract='', patch='', imgtype='coadd'): """ see hsc query manual https://hscdata.mtk.nao.ac.jp/psf/4/manual.html#Bulk_mode """ url = 'https://hscdata.mtk.nao.ac.jp/psf/4/cgi/getpsf?ra={ra}&dec={dec}&filter={band}&rerun={rerun}&tract={tract}&patch={patch}&type={imgtyp...
def convolution_size_equation(size, filter_size, padding, stride): """ Output size of convolutional layer """ return (size - filter_size + 2 * padding) // stride + 1
def learning_rate_with_decay(lr, global_step, discount_step, discount_factor): """ Near-optimal step decay learning rate schedule as proposed by https://arxiv.org/abs/1904.12838. """ return lr * discount_factor if global_step % discount_step == 0 and global_step > 0 else lr
def fix_type(argv): # Fix TypeError caused by argv, agrv returns str """ Get a list and fix the type errors""" for x in range(len(argv)): if isinstance(argv[x], str): if argv[x].isnumeric() is True: argv[x] = int(argv[x]) elif '.' in argv[x]: argv...
def recvFixedLength(open_socket, lengthMsg): """ Description Receive a fixed length message on an open socket. Parm lengthMsg, an integer, which is the length in characters of the message to receive. Return the message received as a string. """ pieces = [] nbBytesRecv = 0 while nbBytesRecv < lengthMsg: ...
def nested_get(ind, coll, lazy=False): """ Get nested index from collection Examples -------- >>> nested_get(1, 'abc') 'b' >>> nested_get([1, 0], 'abc') ('b', 'a') >>> nested_get([[1, 0], [0, 1]], 'abc') (('b', 'a'), ('a', 'b')) """ if isinstance(ind, list): if lazy...
def midi2freq(note): """ convert a midi note to its frequency in hertz https://en.wikipedia.org/wiki/Scientific_pitch_notation """ return 440*2.0**((note-69)/12)
def firstNN(*args): """ Return the first argument not None. Example ------- >>> firstNN(None, False, True) False >>> firstNN(True, False, True) True >>> firstNN(None, None, True) True >>> firstNN(None, 2, True) 2 >>> firstNN(None, ...
def convert_list_to_dict(input_list): """ Convert a list of values into a dict with int as keys :param input_list: list, list to convert :return: dict -> {<int_keys>: <list_elements>} """ return {k: v for k, v in enumerate(input_list)}
def marker_func(label): """Given a label, returns the decided marker""" if "paw" in label or "hoof" in label: return "^" elif "ankle" in label or "middle" in label: return "D" elif "knee" in label or "top" in label: return "s" return "o"
def get_prop_cen_z_offset(class_str): """Get the proposal z centroid offset depending on the class. """ if class_str == 'Car': offset = 2.17799973487854 elif class_str == 'Pedestrian': offset = 0.351921409368515 elif class_str == 'Cyclist': offset = 0.8944902420043945 el...
def get_2pttype_for_dictkey(dictkey): """ Convert key in blinding factor dictionary to sets of strings used in the fits file to designate which kind of 2pt function is being analyzed. """ if dictkey == 'gal_gal_cl': return 'GPF','GPF' elif dictkey == 'gal_shear_cl': return 'GPF',...
def make_one_if_possible(shape): """ Format layer's input or output shape. Parameters ---------- shape : int or tuple Returns ------- int or tuple """ if isinstance(shape, (tuple, list)) and len(shape) == 1: return shape[0] return shape
def mask_out_bits(segbits, mask, tags_to_mask=None): """ Given a set of bits and a list of tags to affect (optional) removes all the bits from each tag that are present (and equal) in the masking set. """ if tags_to_mask is None: tags_to_mask = segbits.keys() # Mask out matching bits ...
def tobytes(s): """force_bytes(s) -> bytes Ensures the given argument is of type bytes Example: >>> force_bytes(b'abc') b'abc' >>> force_bytes('abc') b'abc' >>> force_bytes(1) Traceback (most recent call last): ... TypeError: Expecting a...
def report_from_raw_data(lang, data): """ Basic report on raw, flat data from the API (not parsed into a tree yet). """ report = {'lang': lang} # general counts report['#topics'] = len(data['topics']) report['#videos'] = len(data['videos']) report['#exercises'] = len(data['exercises']) ...
def _getLastRevision(lines): """Returns last revision of the PDB entry, if applicable.""" if lines['REVDAT']: for i, line in lines['REVDAT']: return line[13:22] break else: return "No revision yet"
def escape(s): """Escape template string syntax.""" return s.replace("\\", r"\\").replace("`", r"\`").replace("$", r"\$")
def fib(n): """ A recursive implementation of finding the nth number in the fibonacci sequence """ if n <= 1: return n return fib(n - 1) + fib(n - 2)
def sign_of(x): """ The sign function for a real or integer parameter Return -1, 0 or 1 depending of the sign of x """ return (x and (1, -1)[x < 0])
def rigSideSep(text): """ @param text: string to be split by '/' @return: returns string array were split by '/' character """ ret = [] obj = str(text) if str(text) == '': return ret parts = obj.split('/') if len(parts) <= 1: ret.append(obj) else: ret.ap...
def flat_header_val_to_dict(header_val): """ Transform a header string of comma separated parameters into a dict """ val_dict = {} val_comps = header_val.rsplit(',') if len(val_comps): for val_comp in val_comps: key, sep, val = val_comp.partition("=") if sep != "=...
def type_factor(NT, NP, SC=30.0): """ NT - Number of planet types in area NP - Number of planets in area SC - a number used to scale how bad it is to differ from the optimal number of planet types. Lower number means less bad Returns a number between 0.0 and 1.0 indicating how good the rati...
def calc_product_matrices(m_1_ij, m_2_ij, *argv): """Product of two or more matrices.""" if len(argv) == 0: m_1_11, m_1_12, m_1_13, m_1_21, m_1_22, m_1_23, m_1_31, m_1_32, \ m_1_33 = m_1_ij m_2_11, m_2_12, m_2_13, m_2_21, m_2_22, m_2_23, m_2_31, m_2_32, \ m_2_33 = m_2_ij ...
def horner(x0: float, coefficients: list) -> float: """A function that implements the Horner's method for evaluating a polynomial, with coefficients, at x = x0. Time complexity: O(n), where n = len(coefficients).""" assert isinstance(coefficients, list) assert all( isinstance(x, float) or i...
def convert_uint16_to_array(value): """ Convert a number into an array of 2 bytes (LSB). """ return [(value >> 0 & 0xFF), (value >> 8 & 0xFF)]
def bitwise_or (value, mask): """Peforms a bitwise-or operation on `value` and `mask` leaving any "bits" marked "X" unchanged. Note that `value` and `mask` are both strings containing '0', '1', or 'X' and a string result is returned. """ return ''.join(v if m == '0' else m for v, m in zip(value...
def util_key_exists ( keys, key ): """Returns boolean for key in list""" result = False for i in keys: if (i == key): result = True return result
def check_uniqueness_in_rows(board: list, row=True): """ Check buildings of unique height in each row. Return True if buildings in a row have unique length, False otherwise. >>> check_uniqueness_in_rows(['***21**', '412453*', '423145*', '*543215', \ '*35214*', '*41532*', '*2*1***']) True ...
def ForceKernel(r, h): """ Returns the quantity equivalent to (fraction of mass enclosed)/ r^3 for a cubic-spline mass distribution of compact support radius h. Used to calculate the softened gravitational force. Arguments: r - radius h - softening """ if r > h: return 1./(r*r*r) h...
def asbytes(s): """Turns unicode into bytes, if needed. Assumes UTF-8. """ if isinstance(s, bytes): return s else: return s.encode("utf-8")