sequence
stringlengths
546
16.2k
code
stringlengths
108
19.3k
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:get_keys; 3, parameters; 3, 4; 3, 5; 4, identifier:data_list; 5, default_parameter; 5, 6; 5, 7; 6, identifier:leading_columns; 7, identifier:LEADING_COLUMNS; 8, block; 8, 9; 8, 11; 8, 34; 8, 38; 8, 62; 9, expression_statement; 9, 10; 10, comment; 11, expression_statement; 11, 12; 12, assignment; 12, 13; 12, 14; 13, identifier:all_keys; 14, call; 14, 15; 14, 20; 15, attribute; 15, 16; 15, 19; 16, call; 16, 17; 16, 18; 17, identifier:set; 18, argument_list; 19, identifier:union; 20, argument_list; 20, 21; 21, list_splat; 21, 22; 22, generator_expression; 22, 23; 22, 31; 23, call; 23, 24; 23, 25; 24, identifier:list; 25, argument_list; 25, 26; 26, call; 26, 27; 26, 30; 27, attribute; 27, 28; 27, 29; 28, identifier:d; 29, identifier:keys; 30, argument_list; 31, for_in_clause; 31, 32; 31, 33; 32, identifier:d; 33, identifier:data_list; 34, expression_statement; 34, 35; 35, assignment; 35, 36; 35, 37; 36, identifier:leading_keys; 37, list:[]; 38, for_statement; 38, 39; 38, 40; 38, 41; 39, identifier:key; 40, identifier:leading_columns; 41, block; 41, 42; 41, 48; 41, 55; 42, if_statement; 42, 43; 42, 46; 43, comparison_operator:not; 43, 44; 43, 45; 44, identifier:key; 45, identifier:all_keys; 46, block; 46, 47; 47, continue_statement; 48, expression_statement; 48, 49; 49, call; 49, 50; 49, 53; 50, attribute; 50, 51; 50, 52; 51, identifier:leading_keys; 52, identifier:append; 53, argument_list; 53, 54; 54, identifier:key; 55, expression_statement; 55, 56; 56, call; 56, 57; 56, 60; 57, attribute; 57, 58; 57, 59; 58, identifier:all_keys; 59, identifier:remove; 60, argument_list; 60, 61; 61, identifier:key; 62, return_statement; 62, 63; 63, binary_operator:+; 63, 64; 63, 65; 64, identifier:leading_keys; 65, call; 65, 66; 65, 67; 66, identifier:sorted; 67, argument_list; 67, 68; 68, identifier:all_keys
def get_keys(data_list, leading_columns=LEADING_COLUMNS): """Gets all possible keys from a list of dicts, sorting by leading_columns first Args: data_list: list of dicts to pull keys from leading_columns: list of keys to put first in the result Returns: list of keys to be included as columns in excel worksheet """ all_keys = set().union(*(list(d.keys()) for d in data_list)) leading_keys = [] for key in leading_columns: if key not in all_keys: continue leading_keys.append(key) all_keys.remove(key) return leading_keys + sorted(all_keys)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 15; 2, function_name:to_file; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 3, 12; 4, identifier:self; 5, identifier:f; 6, default_parameter; 6, 7; 6, 8; 7, identifier:sorted; 8, True; 9, default_parameter; 9, 10; 9, 11; 10, identifier:relativize; 11, True; 12, default_parameter; 12, 13; 12, 14; 13, identifier:nl; 14, None; 15, block; 15, 16; 15, 18; 15, 36; 15, 51; 15, 76; 16, expression_statement; 16, 17; 17, comment; 18, if_statement; 18, 19; 18, 24; 18, 25; 18, 30; 19, comparison_operator:>=; 19, 20; 19, 23; 20, attribute; 20, 21; 20, 22; 21, identifier:sys; 22, identifier:hexversion; 23, integer:0x02030000; 24, comment; 25, block; 25, 26; 26, expression_statement; 26, 27; 27, assignment; 27, 28; 27, 29; 28, identifier:str_type; 29, identifier:basestring; 30, else_clause; 30, 31; 31, block; 31, 32; 32, expression_statement; 32, 33; 33, assignment; 33, 34; 33, 35; 34, identifier:str_type; 35, identifier:str; 36, if_statement; 36, 37; 36, 40; 36, 45; 37, comparison_operator:is; 37, 38; 37, 39; 38, identifier:nl; 39, None; 40, block; 40, 41; 41, expression_statement; 41, 42; 42, assignment; 42, 43; 42, 44; 43, identifier:opts; 44, string:'w'; 45, else_clause; 45, 46; 46, block; 46, 47; 47, expression_statement; 47, 48; 48, assignment; 48, 49; 48, 50; 49, identifier:opts; 50, string:'wb'; 51, if_statement; 51, 52; 51, 57; 51, 70; 52, call; 52, 53; 52, 54; 53, identifier:isinstance; 54, argument_list; 54, 55; 54, 56; 55, identifier:f; 56, identifier:str_type; 57, block; 57, 58; 57, 66; 58, expression_statement; 58, 59; 59, assignment; 59, 60; 59, 61; 60, identifier:f; 61, call; 61, 62; 61, 63; 62, identifier:file; 63, argument_list; 63, 64; 63, 65; 64, identifier:f; 65, identifier:opts; 66, expression_statement; 66, 67; 67, assignment; 67, 68; 67, 69; 68, identifier:want_close; 69, True; 70, else_clause; 70, 71; 71, block; 71, 72; 72, expression_statement; 72, 73; 73, assignment; 73, 74; 73, 75; 74, identifier:want_close; 75, False; 76, try_statement; 76, 77; 76, 153; 77, block; 77, 78; 77, 105; 78, if_statement; 78, 79; 78, 80; 78, 95; 79, identifier:sorted; 80, block; 80, 81; 80, 89; 81, expression_statement; 81, 82; 82, assignment; 82, 83; 82, 84; 83, identifier:names; 84, call; 84, 85; 84, 88; 85, attribute; 85, 86; 85, 87; 86, identifier:self; 87, identifier:keys; 88, argument_list; 89, expression_statement; 89, 90; 90, call; 90, 91; 90, 94; 91, attribute; 91, 92; 91, 93; 92, identifier:names; 93, identifier:sort; 94, argument_list; 95, else_clause; 95, 96; 96, block; 96, 97; 97, expression_statement; 97, 98; 98, assignment; 98, 99; 98, 100; 99, identifier:names; 100, call; 100, 101; 100, 104; 101, attribute; 101, 102; 101, 103; 102, identifier:self; 103, identifier:iterkeys; 104, argument_list; 105, for_statement; 105, 106; 105, 107; 105, 108; 106, identifier:n; 107, identifier:names; 108, block; 108, 109; 108, 128; 109, expression_statement; 109, 110; 110, assignment; 110, 111; 110, 112; 111, identifier:l; 112, call; 112, 113; 112, 118; 113, attribute; 113, 114; 113, 117; 114, subscript; 114, 115; 114, 116; 115, identifier:self; 116, identifier:n; 117, identifier:to_text; 118, argument_list; 118, 119; 118, 120; 118, 125; 119, identifier:n; 120, keyword_argument; 120, 121; 120, 122; 121, identifier:origin; 122, attribute; 122, 123; 122, 124; 123, identifier:self; 124, identifier:origin; 125, keyword_argument; 125, 126; 125, 127; 126, identifier:relativize; 127, identifier:relativize; 128, if_statement; 128, 129; 128, 132; 128, 137; 129, comparison_operator:is; 129, 130; 129, 131; 130, identifier:nl; 131, None; 132, block; 132, 133; 133, print_statement; 133, 134; 133, 136; 134, chevron; 134, 135; 135, identifier:f; 136, identifier:l; 137, else_clause; 137, 138; 138, block; 138, 139; 138, 146; 139, expression_statement; 139, 140; 140, call; 140, 141; 140, 144; 141, attribute; 141, 142; 141, 143; 142, identifier:f; 143, identifier:write; 144, argument_list; 144, 145; 145, identifier:l; 146, expression_statement; 146, 147; 147, call; 147, 148; 147, 151; 148, attribute; 148, 149; 148, 150; 149, identifier:f; 150, identifier:write; 151, argument_list; 151, 152; 152, identifier:nl; 153, finally_clause; 153, 154; 154, block; 154, 155; 155, if_statement; 155, 156; 155, 157; 156, identifier:want_close; 157, block; 157, 158; 158, expression_statement; 158, 159; 159, call; 159, 160; 159, 163; 160, attribute; 160, 161; 160, 162; 161, identifier:f; 162, identifier:close; 163, argument_list
def to_file(self, f, sorted=True, relativize=True, nl=None): """Write a zone to a file. @param f: file or string. If I{f} is a string, it is treated as the name of a file to open. @param sorted: if True, the file will be written with the names sorted in DNSSEC order from least to greatest. Otherwise the names will be written in whatever order they happen to have in the zone's dictionary. @param relativize: if True, domain names in the output will be relativized to the zone's origin (if possible). @type relativize: bool @param nl: The end of line string. If not specified, the output will use the platform's native end-of-line marker (i.e. LF on POSIX, CRLF on Windows, CR on Macintosh). @type nl: string or None """ if sys.hexversion >= 0x02030000: # allow Unicode filenames str_type = basestring else: str_type = str if nl is None: opts = 'w' else: opts = 'wb' if isinstance(f, str_type): f = file(f, opts) want_close = True else: want_close = False try: if sorted: names = self.keys() names.sort() else: names = self.iterkeys() for n in names: l = self[n].to_text(n, origin=self.origin, relativize=relativize) if nl is None: print >> f, l else: f.write(l) f.write(nl) finally: if want_close: f.close()
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:_sorted_keys; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:keys; 6, block; 6, 7; 6, 9; 6, 13; 6, 33; 6, 78; 6, 107; 6, 127; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:sorted_keys; 12, list:[]; 13, if_statement; 13, 14; 13, 25; 14, boolean_operator:and; 14, 15; 14, 19; 15, parenthesized_expression; 15, 16; 16, comparison_operator:in; 16, 17; 16, 18; 17, string:'epoch'; 18, identifier:keys; 19, parenthesized_expression; 19, 20; 20, comparison_operator:not; 20, 21; 20, 22; 21, string:'epoch'; 22, attribute; 22, 23; 22, 24; 23, identifier:self; 24, identifier:keys_ignored_; 25, block; 25, 26; 26, expression_statement; 26, 27; 27, call; 27, 28; 27, 31; 28, attribute; 28, 29; 28, 30; 29, identifier:sorted_keys; 30, identifier:append; 31, argument_list; 31, 32; 32, string:'epoch'; 33, for_statement; 33, 34; 33, 35; 33, 39; 34, identifier:key; 35, call; 35, 36; 35, 37; 36, identifier:sorted; 37, argument_list; 37, 38; 38, identifier:keys; 39, block; 39, 40; 40, if_statement; 40, 41; 40, 70; 41, not_operator; 41, 42; 42, parenthesized_expression; 42, 43; 43, boolean_operator:or; 43, 44; 43, 64; 44, boolean_operator:or; 44, 45; 44, 58; 45, boolean_operator:or; 45, 46; 45, 52; 46, parenthesized_expression; 46, 47; 47, comparison_operator:in; 47, 48; 47, 49; 48, identifier:key; 49, tuple; 49, 50; 49, 51; 50, string:'epoch'; 51, string:'dur'; 52, parenthesized_expression; 52, 53; 53, comparison_operator:in; 53, 54; 53, 55; 54, identifier:key; 55, attribute; 55, 56; 55, 57; 56, identifier:self; 57, identifier:keys_ignored_; 58, call; 58, 59; 58, 62; 59, attribute; 59, 60; 59, 61; 60, identifier:key; 61, identifier:endswith; 62, argument_list; 62, 63; 63, string:'_best'; 64, call; 64, 65; 64, 68; 65, attribute; 65, 66; 65, 67; 66, identifier:key; 67, identifier:startswith; 68, argument_list; 68, 69; 69, string:'event_'; 70, block; 70, 71; 71, expression_statement; 71, 72; 72, call; 72, 73; 72, 76; 73, attribute; 73, 74; 73, 75; 74, identifier:sorted_keys; 75, identifier:append; 76, argument_list; 76, 77; 77, identifier:key; 78, for_statement; 78, 79; 78, 80; 78, 84; 79, identifier:key; 80, call; 80, 81; 80, 82; 81, identifier:sorted; 82, argument_list; 82, 83; 83, identifier:keys; 84, block; 84, 85; 85, if_statement; 85, 86; 85, 99; 86, boolean_operator:and; 86, 87; 86, 93; 87, call; 87, 88; 87, 91; 88, attribute; 88, 89; 88, 90; 89, identifier:key; 90, identifier:startswith; 91, argument_list; 91, 92; 92, string:'event_'; 93, parenthesized_expression; 93, 94; 94, comparison_operator:not; 94, 95; 94, 96; 95, identifier:key; 96, attribute; 96, 97; 96, 98; 97, identifier:self; 98, identifier:keys_ignored_; 99, block; 99, 100; 100, expression_statement; 100, 101; 101, call; 101, 102; 101, 105; 102, attribute; 102, 103; 102, 104; 103, identifier:sorted_keys; 104, identifier:append; 105, argument_list; 105, 106; 106, identifier:key; 107, if_statement; 107, 108; 107, 119; 108, boolean_operator:and; 108, 109; 108, 113; 109, parenthesized_expression; 109, 110; 110, comparison_operator:in; 110, 111; 110, 112; 111, string:'dur'; 112, identifier:keys; 113, parenthesized_expression; 113, 114; 114, comparison_operator:not; 114, 115; 114, 116; 115, string:'dur'; 116, attribute; 116, 117; 116, 118; 117, identifier:self; 118, identifier:keys_ignored_; 119, block; 119, 120; 120, expression_statement; 120, 121; 121, call; 121, 122; 121, 125; 122, attribute; 122, 123; 122, 124; 123, identifier:sorted_keys; 124, identifier:append; 125, argument_list; 125, 126; 126, string:'dur'; 127, return_statement; 127, 128; 128, identifier:sorted_keys
def _sorted_keys(self, keys): """Sort keys, dropping the ones that should be ignored. The keys that are in ``self.ignored_keys`` or that end on '_best' are dropped. Among the remaining keys: * 'epoch' is put first; * 'dur' is put last; * keys that start with 'event_' are put just before 'dur'; * all remaining keys are sorted alphabetically. """ sorted_keys = [] if ('epoch' in keys) and ('epoch' not in self.keys_ignored_): sorted_keys.append('epoch') for key in sorted(keys): if not ( (key in ('epoch', 'dur')) or (key in self.keys_ignored_) or key.endswith('_best') or key.startswith('event_') ): sorted_keys.append(key) for key in sorted(keys): if key.startswith('event_') and (key not in self.keys_ignored_): sorted_keys.append(key) if ('dur' in keys) and ('dur' not in self.keys_ignored_): sorted_keys.append('dur') return sorted_keys
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:sort_values; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:expr; 5, identifier:by; 6, default_parameter; 6, 7; 6, 8; 7, identifier:ascending; 8, True; 9, block; 9, 10; 9, 12; 9, 25; 9, 44; 10, expression_statement; 10, 11; 11, comment; 12, if_statement; 12, 13; 12, 19; 13, not_operator; 13, 14; 14, call; 14, 15; 14, 16; 15, identifier:isinstance; 16, argument_list; 16, 17; 16, 18; 17, identifier:by; 18, identifier:list; 19, block; 19, 20; 20, expression_statement; 20, 21; 21, assignment; 21, 22; 21, 23; 22, identifier:by; 23, list:[by, ]; 23, 24; 24, identifier:by; 25, expression_statement; 25, 26; 26, assignment; 26, 27; 26, 28; 27, identifier:by; 28, list_comprehension; 28, 29; 28, 41; 29, conditional_expression:if; 29, 30; 29, 34; 29, 40; 30, call; 30, 31; 30, 32; 31, identifier:it; 32, argument_list; 32, 33; 33, identifier:expr; 34, call; 34, 35; 34, 38; 35, attribute; 35, 36; 35, 37; 36, identifier:inspect; 37, identifier:isfunction; 38, argument_list; 38, 39; 39, identifier:it; 40, identifier:it; 41, for_in_clause; 41, 42; 41, 43; 42, identifier:it; 43, identifier:by; 44, return_statement; 44, 45; 45, call; 45, 46; 45, 47; 46, identifier:SortedCollectionExpr; 47, argument_list; 47, 48; 47, 49; 47, 52; 47, 55; 48, identifier:expr; 49, keyword_argument; 49, 50; 49, 51; 50, identifier:_sorted_fields; 51, identifier:by; 52, keyword_argument; 52, 53; 52, 54; 53, identifier:_ascending; 54, identifier:ascending; 55, keyword_argument; 55, 56; 55, 57; 56, identifier:_schema; 57, attribute; 57, 58; 57, 59; 58, identifier:expr; 59, identifier:_schema
def sort_values(expr, by, ascending=True): """ Sort the collection by values. `sort` is an alias name for `sort_values` :param expr: collection :param by: the sequence or sequences to sort :param ascending: Sort ascending vs. descending. Sepecify list for multiple sort orders. If this is a list of bools, must match the length of the by :return: Sorted collection :Example: >>> df.sort_values(['name', 'id']) # 1 >>> df.sort(['name', 'id'], ascending=False) # 2 >>> df.sort(['name', 'id'], ascending=[False, True]) # 3 >>> df.sort([-df.name, df.id]) # 4, equal to #3 """ if not isinstance(by, list): by = [by, ] by = [it(expr) if inspect.isfunction(it) else it for it in by] return SortedCollectionExpr(expr, _sorted_fields=by, _ascending=ascending, _schema=expr._schema)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 14; 2, function_name:reshuffle; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 4, identifier:expr; 5, default_parameter; 5, 6; 5, 7; 6, identifier:by; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:sort; 10, None; 11, default_parameter; 11, 12; 11, 13; 12, identifier:ascending; 13, True; 14, block; 14, 15; 14, 17; 14, 25; 14, 34; 14, 49; 15, expression_statement; 15, 16; 16, comment; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 20; 19, identifier:by; 20, boolean_operator:or; 20, 21; 20, 22; 21, identifier:by; 22, call; 22, 23; 22, 24; 23, identifier:RandomScalar; 24, argument_list; 25, expression_statement; 25, 26; 26, assignment; 26, 27; 26, 28; 27, identifier:grouped; 28, call; 28, 29; 28, 32; 29, attribute; 29, 30; 29, 31; 30, identifier:expr; 31, identifier:groupby; 32, argument_list; 32, 33; 33, identifier:by; 34, if_statement; 34, 35; 34, 36; 35, identifier:sort; 36, block; 36, 37; 37, expression_statement; 37, 38; 38, assignment; 38, 39; 38, 40; 39, identifier:grouped; 40, call; 40, 41; 40, 44; 41, attribute; 41, 42; 41, 43; 42, identifier:grouped; 43, identifier:sort_values; 44, argument_list; 44, 45; 44, 46; 45, identifier:sort; 46, keyword_argument; 46, 47; 46, 48; 47, identifier:ascending; 48, identifier:ascending; 49, return_statement; 49, 50; 50, call; 50, 51; 50, 52; 51, identifier:ReshuffledCollectionExpr; 52, argument_list; 52, 53; 52, 56; 53, keyword_argument; 53, 54; 53, 55; 54, identifier:_input; 55, identifier:grouped; 56, keyword_argument; 56, 57; 56, 58; 57, identifier:_schema; 58, attribute; 58, 59; 58, 60; 59, identifier:expr; 60, identifier:_schema
def reshuffle(expr, by=None, sort=None, ascending=True): """ Reshuffle data. :param expr: :param by: the sequence or scalar to shuffle by. RandomScalar as default :param sort: the sequence or scalar to sort. :param ascending: True if ascending else False :return: collection """ by = by or RandomScalar() grouped = expr.groupby(by) if sort: grouped = grouped.sort_values(sort, ascending=ascending) return ReshuffledCollectionExpr(_input=grouped, _schema=expr._schema)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 20; 2, function_name:cumsum; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 14; 3, 17; 4, identifier:expr; 5, default_parameter; 5, 6; 5, 7; 6, identifier:sort; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:ascending; 10, True; 11, default_parameter; 11, 12; 11, 13; 12, identifier:unique; 13, False; 14, default_parameter; 14, 15; 14, 16; 15, identifier:preceding; 16, None; 17, default_parameter; 17, 18; 17, 19; 18, identifier:following; 19, None; 20, block; 20, 21; 20, 23; 20, 46; 21, expression_statement; 21, 22; 22, comment; 23, if_statement; 23, 24; 23, 31; 23, 38; 24, comparison_operator:==; 24, 25; 24, 28; 25, attribute; 25, 26; 25, 27; 26, identifier:expr; 27, identifier:_data_type; 28, attribute; 28, 29; 28, 30; 29, identifier:types; 30, identifier:boolean; 31, block; 31, 32; 32, expression_statement; 32, 33; 33, assignment; 33, 34; 33, 35; 34, identifier:output_type; 35, attribute; 35, 36; 35, 37; 36, identifier:types; 37, identifier:int64; 38, else_clause; 38, 39; 39, block; 39, 40; 40, expression_statement; 40, 41; 41, assignment; 41, 42; 41, 43; 42, identifier:output_type; 43, attribute; 43, 44; 43, 45; 44, identifier:expr; 45, identifier:_data_type; 46, return_statement; 46, 47; 47, call; 47, 48; 47, 49; 48, identifier:_cumulative_op; 49, argument_list; 49, 50; 49, 51; 49, 52; 49, 55; 49, 58; 49, 61; 49, 64; 49, 67; 50, identifier:expr; 51, identifier:CumSum; 52, keyword_argument; 52, 53; 52, 54; 53, identifier:sort; 54, identifier:sort; 55, keyword_argument; 55, 56; 55, 57; 56, identifier:ascending; 57, identifier:ascending; 58, keyword_argument; 58, 59; 58, 60; 59, identifier:unique; 60, identifier:unique; 61, keyword_argument; 61, 62; 61, 63; 62, identifier:preceding; 63, identifier:preceding; 64, keyword_argument; 64, 65; 64, 66; 65, identifier:following; 66, identifier:following; 67, keyword_argument; 67, 68; 67, 69; 68, identifier:data_type; 69, identifier:output_type
def cumsum(expr, sort=None, ascending=True, unique=False, preceding=None, following=None): """ Calculate cumulative summation of a sequence expression. :param expr: expression for calculation :param sort: name of the sort column :param ascending: whether to sort in ascending order :param unique: whether to eliminate duplicate entries :param preceding: the start point of a window :param following: the end point of a window :return: calculated column """ if expr._data_type == types.boolean: output_type = types.int64 else: output_type = expr._data_type return _cumulative_op(expr, CumSum, sort=sort, ascending=ascending, unique=unique, preceding=preceding, following=following, data_type=output_type)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 20; 2, function_name:cummax; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 14; 3, 17; 4, identifier:expr; 5, default_parameter; 5, 6; 5, 7; 6, identifier:sort; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:ascending; 10, True; 11, default_parameter; 11, 12; 11, 13; 12, identifier:unique; 13, False; 14, default_parameter; 14, 15; 14, 16; 15, identifier:preceding; 16, None; 17, default_parameter; 17, 18; 17, 19; 18, identifier:following; 19, None; 20, block; 20, 21; 20, 23; 21, expression_statement; 21, 22; 22, comment; 23, return_statement; 23, 24; 24, call; 24, 25; 24, 26; 25, identifier:_cumulative_op; 26, argument_list; 26, 27; 26, 28; 26, 29; 26, 32; 26, 35; 26, 38; 26, 41; 27, identifier:expr; 28, identifier:CumMax; 29, keyword_argument; 29, 30; 29, 31; 30, identifier:sort; 31, identifier:sort; 32, keyword_argument; 32, 33; 32, 34; 33, identifier:ascending; 34, identifier:ascending; 35, keyword_argument; 35, 36; 35, 37; 36, identifier:unique; 37, identifier:unique; 38, keyword_argument; 38, 39; 38, 40; 39, identifier:preceding; 40, identifier:preceding; 41, keyword_argument; 41, 42; 41, 43; 42, identifier:following; 43, identifier:following
def cummax(expr, sort=None, ascending=True, unique=False, preceding=None, following=None): """ Calculate cumulative maximum of a sequence expression. :param expr: expression for calculation :param sort: name of the sort column :param ascending: whether to sort in ascending order :param unique: whether to eliminate duplicate entries :param preceding: the start point of a window :param following: the end point of a window :return: calculated column """ return _cumulative_op(expr, CumMax, sort=sort, ascending=ascending, unique=unique, preceding=preceding, following=following)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 20; 2, function_name:cummin; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 14; 3, 17; 4, identifier:expr; 5, default_parameter; 5, 6; 5, 7; 6, identifier:sort; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:ascending; 10, True; 11, default_parameter; 11, 12; 11, 13; 12, identifier:unique; 13, False; 14, default_parameter; 14, 15; 14, 16; 15, identifier:preceding; 16, None; 17, default_parameter; 17, 18; 17, 19; 18, identifier:following; 19, None; 20, block; 20, 21; 20, 23; 21, expression_statement; 21, 22; 22, comment; 23, return_statement; 23, 24; 24, call; 24, 25; 24, 26; 25, identifier:_cumulative_op; 26, argument_list; 26, 27; 26, 28; 26, 29; 26, 32; 26, 35; 26, 38; 26, 41; 27, identifier:expr; 28, identifier:CumMin; 29, keyword_argument; 29, 30; 29, 31; 30, identifier:sort; 31, identifier:sort; 32, keyword_argument; 32, 33; 32, 34; 33, identifier:ascending; 34, identifier:ascending; 35, keyword_argument; 35, 36; 35, 37; 36, identifier:unique; 37, identifier:unique; 38, keyword_argument; 38, 39; 38, 40; 39, identifier:preceding; 40, identifier:preceding; 41, keyword_argument; 41, 42; 41, 43; 42, identifier:following; 43, identifier:following
def cummin(expr, sort=None, ascending=True, unique=False, preceding=None, following=None): """ Calculate cumulative minimum of a sequence expression. :param expr: expression for calculation :param sort: name of the sort column :param ascending: whether to sort in ascending order :param unique: whether to eliminate duplicate entries :param preceding: the start point of a window :param following: the end point of a window :return: calculated column """ return _cumulative_op(expr, CumMin, sort=sort, ascending=ascending, unique=unique, preceding=preceding, following=following)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 20; 2, function_name:cummean; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 14; 3, 17; 4, identifier:expr; 5, default_parameter; 5, 6; 5, 7; 6, identifier:sort; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:ascending; 10, True; 11, default_parameter; 11, 12; 11, 13; 12, identifier:unique; 13, False; 14, default_parameter; 14, 15; 14, 16; 15, identifier:preceding; 16, None; 17, default_parameter; 17, 18; 17, 19; 18, identifier:following; 19, None; 20, block; 20, 21; 20, 23; 20, 30; 21, expression_statement; 21, 22; 22, comment; 23, expression_statement; 23, 24; 24, assignment; 24, 25; 24, 26; 25, identifier:data_type; 26, call; 26, 27; 26, 28; 27, identifier:_stats_type; 28, argument_list; 28, 29; 29, identifier:expr; 30, return_statement; 30, 31; 31, call; 31, 32; 31, 33; 32, identifier:_cumulative_op; 33, argument_list; 33, 34; 33, 35; 33, 36; 33, 39; 33, 42; 33, 45; 33, 48; 33, 51; 34, identifier:expr; 35, identifier:CumMean; 36, keyword_argument; 36, 37; 36, 38; 37, identifier:sort; 38, identifier:sort; 39, keyword_argument; 39, 40; 39, 41; 40, identifier:ascending; 41, identifier:ascending; 42, keyword_argument; 42, 43; 42, 44; 43, identifier:unique; 44, identifier:unique; 45, keyword_argument; 45, 46; 45, 47; 46, identifier:preceding; 47, identifier:preceding; 48, keyword_argument; 48, 49; 48, 50; 49, identifier:following; 50, identifier:following; 51, keyword_argument; 51, 52; 51, 53; 52, identifier:data_type; 53, identifier:data_type
def cummean(expr, sort=None, ascending=True, unique=False, preceding=None, following=None): """ Calculate cumulative mean of a sequence expression. :param expr: expression for calculation :param sort: name of the sort column :param ascending: whether to sort in ascending order :param unique: whether to eliminate duplicate entries :param preceding: the start point of a window :param following: the end point of a window :return: calculated column """ data_type = _stats_type(expr) return _cumulative_op(expr, CumMean, sort=sort, ascending=ascending, unique=unique, preceding=preceding, following=following, data_type=data_type)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 20; 2, function_name:cummedian; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 14; 3, 17; 4, identifier:expr; 5, default_parameter; 5, 6; 5, 7; 6, identifier:sort; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:ascending; 10, True; 11, default_parameter; 11, 12; 11, 13; 12, identifier:unique; 13, False; 14, default_parameter; 14, 15; 14, 16; 15, identifier:preceding; 16, None; 17, default_parameter; 17, 18; 17, 19; 18, identifier:following; 19, None; 20, block; 20, 21; 20, 23; 20, 30; 21, expression_statement; 21, 22; 22, comment; 23, expression_statement; 23, 24; 24, assignment; 24, 25; 24, 26; 25, identifier:data_type; 26, call; 26, 27; 26, 28; 27, identifier:_stats_type; 28, argument_list; 28, 29; 29, identifier:expr; 30, return_statement; 30, 31; 31, call; 31, 32; 31, 33; 32, identifier:_cumulative_op; 33, argument_list; 33, 34; 33, 35; 33, 36; 33, 39; 33, 42; 33, 45; 33, 48; 33, 51; 34, identifier:expr; 35, identifier:CumMedian; 36, keyword_argument; 36, 37; 36, 38; 37, identifier:sort; 38, identifier:sort; 39, keyword_argument; 39, 40; 39, 41; 40, identifier:ascending; 41, identifier:ascending; 42, keyword_argument; 42, 43; 42, 44; 43, identifier:unique; 44, identifier:unique; 45, keyword_argument; 45, 46; 45, 47; 46, identifier:preceding; 47, identifier:preceding; 48, keyword_argument; 48, 49; 48, 50; 49, identifier:following; 50, identifier:following; 51, keyword_argument; 51, 52; 51, 53; 52, identifier:data_type; 53, identifier:data_type
def cummedian(expr, sort=None, ascending=True, unique=False, preceding=None, following=None): """ Calculate cumulative median of a sequence expression. :param expr: expression for calculation :param sort: name of the sort column :param ascending: whether to sort in ascending order :param unique: whether to eliminate duplicate entries :param preceding: the start point of a window :param following: the end point of a window :return: calculated column """ data_type = _stats_type(expr) return _cumulative_op(expr, CumMedian, sort=sort, ascending=ascending, unique=unique, preceding=preceding, following=following, data_type=data_type)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 20; 2, function_name:cumcount; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 14; 3, 17; 4, identifier:expr; 5, default_parameter; 5, 6; 5, 7; 6, identifier:sort; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:ascending; 10, True; 11, default_parameter; 11, 12; 11, 13; 12, identifier:unique; 13, False; 14, default_parameter; 14, 15; 14, 16; 15, identifier:preceding; 16, None; 17, default_parameter; 17, 18; 17, 19; 18, identifier:following; 19, None; 20, block; 20, 21; 20, 23; 20, 29; 21, expression_statement; 21, 22; 22, comment; 23, expression_statement; 23, 24; 24, assignment; 24, 25; 24, 26; 25, identifier:data_type; 26, attribute; 26, 27; 26, 28; 27, identifier:types; 28, identifier:int64; 29, return_statement; 29, 30; 30, call; 30, 31; 30, 32; 31, identifier:_cumulative_op; 32, argument_list; 32, 33; 32, 34; 32, 35; 32, 38; 32, 41; 32, 44; 32, 47; 32, 50; 33, identifier:expr; 34, identifier:CumCount; 35, keyword_argument; 35, 36; 35, 37; 36, identifier:sort; 37, identifier:sort; 38, keyword_argument; 38, 39; 38, 40; 39, identifier:ascending; 40, identifier:ascending; 41, keyword_argument; 41, 42; 41, 43; 42, identifier:unique; 43, identifier:unique; 44, keyword_argument; 44, 45; 44, 46; 45, identifier:preceding; 46, identifier:preceding; 47, keyword_argument; 47, 48; 47, 49; 48, identifier:following; 49, identifier:following; 50, keyword_argument; 50, 51; 50, 52; 51, identifier:data_type; 52, identifier:data_type
def cumcount(expr, sort=None, ascending=True, unique=False, preceding=None, following=None): """ Calculate cumulative count of a sequence expression. :param expr: expression for calculation :param sort: name of the sort column :param ascending: whether to sort in ascending order :param unique: whether to eliminate duplicate entries :param preceding: the start point of a window :param following: the end point of a window :return: calculated column """ data_type = types.int64 return _cumulative_op(expr, CumCount, sort=sort, ascending=ascending, unique=unique, preceding=preceding, following=following, data_type=data_type)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 20; 2, function_name:cumstd; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 14; 3, 17; 4, identifier:expr; 5, default_parameter; 5, 6; 5, 7; 6, identifier:sort; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:ascending; 10, True; 11, default_parameter; 11, 12; 11, 13; 12, identifier:unique; 13, False; 14, default_parameter; 14, 15; 14, 16; 15, identifier:preceding; 16, None; 17, default_parameter; 17, 18; 17, 19; 18, identifier:following; 19, None; 20, block; 20, 21; 20, 23; 20, 30; 21, expression_statement; 21, 22; 22, comment; 23, expression_statement; 23, 24; 24, assignment; 24, 25; 24, 26; 25, identifier:data_type; 26, call; 26, 27; 26, 28; 27, identifier:_stats_type; 28, argument_list; 28, 29; 29, identifier:expr; 30, return_statement; 30, 31; 31, call; 31, 32; 31, 33; 32, identifier:_cumulative_op; 33, argument_list; 33, 34; 33, 35; 33, 36; 33, 39; 33, 42; 33, 45; 33, 48; 33, 51; 34, identifier:expr; 35, identifier:CumStd; 36, keyword_argument; 36, 37; 36, 38; 37, identifier:sort; 38, identifier:sort; 39, keyword_argument; 39, 40; 39, 41; 40, identifier:ascending; 41, identifier:ascending; 42, keyword_argument; 42, 43; 42, 44; 43, identifier:unique; 44, identifier:unique; 45, keyword_argument; 45, 46; 45, 47; 46, identifier:preceding; 47, identifier:preceding; 48, keyword_argument; 48, 49; 48, 50; 49, identifier:following; 50, identifier:following; 51, keyword_argument; 51, 52; 51, 53; 52, identifier:data_type; 53, identifier:data_type
def cumstd(expr, sort=None, ascending=True, unique=False, preceding=None, following=None): """ Calculate cumulative standard deviation of a sequence expression. :param expr: expression for calculation :param sort: name of the sort column :param ascending: whether to sort in ascending order :param unique: whether to eliminate duplicate entries :param preceding: the start point of a window :param following: the end point of a window :return: calculated column """ data_type = _stats_type(expr) return _cumulative_op(expr, CumStd, sort=sort, ascending=ascending, unique=unique, preceding=preceding, following=following, data_type=data_type)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 15; 2, function_name:nth_value; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 3, 12; 4, identifier:expr; 5, identifier:nth; 6, default_parameter; 6, 7; 6, 8; 7, identifier:skip_nulls; 8, False; 9, default_parameter; 9, 10; 9, 11; 10, identifier:sort; 11, None; 12, default_parameter; 12, 13; 12, 14; 13, identifier:ascending; 14, True; 15, block; 15, 16; 15, 18; 16, expression_statement; 16, 17; 17, comment; 18, return_statement; 18, 19; 19, call; 19, 20; 19, 21; 20, identifier:_cumulative_op; 21, argument_list; 21, 22; 21, 23; 21, 24; 21, 29; 21, 32; 21, 35; 21, 38; 22, identifier:expr; 23, identifier:NthValue; 24, keyword_argument; 24, 25; 24, 26; 25, identifier:data_type; 26, attribute; 26, 27; 26, 28; 27, identifier:expr; 28, identifier:_data_type; 29, keyword_argument; 29, 30; 29, 31; 30, identifier:sort; 31, identifier:sort; 32, keyword_argument; 32, 33; 32, 34; 33, identifier:ascending; 34, identifier:ascending; 35, keyword_argument; 35, 36; 35, 37; 36, identifier:_nth; 37, identifier:nth; 38, keyword_argument; 38, 39; 38, 40; 39, identifier:_skip_nulls; 40, identifier:skip_nulls
def nth_value(expr, nth, skip_nulls=False, sort=None, ascending=True): """ Get nth value of a grouped and sorted expression. :param expr: expression for calculation :param nth: integer position :param skip_nulls: whether to skip null values, False by default :param sort: name of the sort column :param ascending: whether to sort in ascending order :return: calculated column """ return _cumulative_op(expr, NthValue, data_type=expr._data_type, sort=sort, ascending=ascending, _nth=nth, _skip_nulls=skip_nulls)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 11; 2, function_name:rank; 3, parameters; 3, 4; 3, 5; 3, 8; 4, identifier:expr; 5, default_parameter; 5, 6; 5, 7; 6, identifier:sort; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:ascending; 10, True; 11, block; 11, 12; 11, 14; 12, expression_statement; 12, 13; 13, comment; 14, return_statement; 14, 15; 15, call; 15, 16; 15, 17; 16, identifier:_rank_op; 17, argument_list; 17, 18; 17, 19; 17, 20; 17, 23; 17, 26; 18, identifier:expr; 19, identifier:Rank; 20, attribute; 20, 21; 20, 22; 21, identifier:types; 22, identifier:int64; 23, keyword_argument; 23, 24; 23, 25; 24, identifier:sort; 25, identifier:sort; 26, keyword_argument; 26, 27; 26, 28; 27, identifier:ascending; 28, identifier:ascending
def rank(expr, sort=None, ascending=True): """ Calculate rank of a sequence expression. :param expr: expression for calculation :param sort: name of the sort column :param ascending: whether to sort in ascending order :return: calculated column """ return _rank_op(expr, Rank, types.int64, sort=sort, ascending=ascending)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 11; 2, function_name:dense_rank; 3, parameters; 3, 4; 3, 5; 3, 8; 4, identifier:expr; 5, default_parameter; 5, 6; 5, 7; 6, identifier:sort; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:ascending; 10, True; 11, block; 11, 12; 11, 14; 12, expression_statement; 12, 13; 13, comment; 14, return_statement; 14, 15; 15, call; 15, 16; 15, 17; 16, identifier:_rank_op; 17, argument_list; 17, 18; 17, 19; 17, 20; 17, 23; 17, 26; 18, identifier:expr; 19, identifier:DenseRank; 20, attribute; 20, 21; 20, 22; 21, identifier:types; 22, identifier:int64; 23, keyword_argument; 23, 24; 23, 25; 24, identifier:sort; 25, identifier:sort; 26, keyword_argument; 26, 27; 26, 28; 27, identifier:ascending; 28, identifier:ascending
def dense_rank(expr, sort=None, ascending=True): """ Calculate dense rank of a sequence expression. :param expr: expression for calculation :param sort: name of the sort column :param ascending: whether to sort in ascending order :return: calculated column """ return _rank_op(expr, DenseRank, types.int64, sort=sort, ascending=ascending)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 11; 2, function_name:percent_rank; 3, parameters; 3, 4; 3, 5; 3, 8; 4, identifier:expr; 5, default_parameter; 5, 6; 5, 7; 6, identifier:sort; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:ascending; 10, True; 11, block; 11, 12; 11, 14; 12, expression_statement; 12, 13; 13, comment; 14, return_statement; 14, 15; 15, call; 15, 16; 15, 17; 16, identifier:_rank_op; 17, argument_list; 17, 18; 17, 19; 17, 20; 17, 23; 17, 26; 18, identifier:expr; 19, identifier:PercentRank; 20, attribute; 20, 21; 20, 22; 21, identifier:types; 22, identifier:float64; 23, keyword_argument; 23, 24; 23, 25; 24, identifier:sort; 25, identifier:sort; 26, keyword_argument; 26, 27; 26, 28; 27, identifier:ascending; 28, identifier:ascending
def percent_rank(expr, sort=None, ascending=True): """ Calculate percentage rank of a sequence expression. :param expr: expression for calculation :param sort: name of the sort column :param ascending: whether to sort in ascending order :return: calculated column """ return _rank_op(expr, PercentRank, types.float64, sort=sort, ascending=ascending)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 11; 2, function_name:row_number; 3, parameters; 3, 4; 3, 5; 3, 8; 4, identifier:expr; 5, default_parameter; 5, 6; 5, 7; 6, identifier:sort; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:ascending; 10, True; 11, block; 11, 12; 11, 14; 12, expression_statement; 12, 13; 13, comment; 14, return_statement; 14, 15; 15, call; 15, 16; 15, 17; 16, identifier:_rank_op; 17, argument_list; 17, 18; 17, 19; 17, 20; 17, 23; 17, 26; 18, identifier:expr; 19, identifier:RowNumber; 20, attribute; 20, 21; 20, 22; 21, identifier:types; 22, identifier:int64; 23, keyword_argument; 23, 24; 23, 25; 24, identifier:sort; 25, identifier:sort; 26, keyword_argument; 26, 27; 26, 28; 27, identifier:ascending; 28, identifier:ascending
def row_number(expr, sort=None, ascending=True): """ Calculate row number of a sequence expression. :param expr: expression for calculation :param sort: name of the sort column :param ascending: whether to sort in ascending order :return: calculated column """ return _rank_op(expr, RowNumber, types.int64, sort=sort, ascending=ascending)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 15; 2, function_name:qcut; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 3, 12; 4, identifier:expr; 5, identifier:bins; 6, default_parameter; 6, 7; 6, 8; 7, identifier:labels; 8, False; 9, default_parameter; 9, 10; 9, 11; 10, identifier:sort; 11, None; 12, default_parameter; 12, 13; 12, 14; 13, identifier:ascending; 14, True; 15, block; 15, 16; 15, 18; 15, 30; 16, expression_statement; 16, 17; 17, comment; 18, if_statement; 18, 19; 18, 24; 19, boolean_operator:or; 19, 20; 19, 23; 20, comparison_operator:is; 20, 21; 20, 22; 21, identifier:labels; 22, None; 23, identifier:labels; 24, block; 24, 25; 25, raise_statement; 25, 26; 26, call; 26, 27; 26, 28; 27, identifier:NotImplementedError; 28, argument_list; 28, 29; 29, string:'Showing bins or customizing labels not supported'; 30, return_statement; 30, 31; 31, call; 31, 32; 31, 33; 32, identifier:_rank_op; 33, argument_list; 33, 34; 33, 35; 33, 36; 33, 39; 33, 42; 33, 45; 34, identifier:expr; 35, identifier:QCut; 36, attribute; 36, 37; 36, 38; 37, identifier:types; 38, identifier:int64; 39, keyword_argument; 39, 40; 39, 41; 40, identifier:sort; 41, identifier:sort; 42, keyword_argument; 42, 43; 42, 44; 43, identifier:ascending; 44, identifier:ascending; 45, keyword_argument; 45, 46; 45, 47; 46, identifier:_bins; 47, identifier:bins
def qcut(expr, bins, labels=False, sort=None, ascending=True): """ Get quantile-based bin indices of every element of a grouped and sorted expression. The indices of bins start from 0. If cuts are not of equal sizes, extra items will be appended into the first group. :param expr: expression for calculation :param bins: number of bins :param sort: name of the sort column :param ascending: whether to sort in ascending order :return: calculated column """ if labels is None or labels: raise NotImplementedError('Showing bins or customizing labels not supported') return _rank_op(expr, QCut, types.int64, sort=sort, ascending=ascending, _bins=bins)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 11; 2, function_name:cume_dist; 3, parameters; 3, 4; 3, 5; 3, 8; 4, identifier:expr; 5, default_parameter; 5, 6; 5, 7; 6, identifier:sort; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:ascending; 10, True; 11, block; 11, 12; 11, 14; 12, expression_statement; 12, 13; 13, comment; 14, return_statement; 14, 15; 15, call; 15, 16; 15, 17; 16, identifier:_rank_op; 17, argument_list; 17, 18; 17, 19; 17, 20; 17, 23; 17, 26; 18, identifier:expr; 19, identifier:CumeDist; 20, attribute; 20, 21; 20, 22; 21, identifier:types; 22, identifier:float64; 23, keyword_argument; 23, 24; 23, 25; 24, identifier:sort; 25, identifier:sort; 26, keyword_argument; 26, 27; 26, 28; 27, identifier:ascending; 28, identifier:ascending
def cume_dist(expr, sort=None, ascending=True): """ Calculate cumulative ratio of a sequence expression. :param expr: expression for calculation :param sort: name of the sort column :param ascending: whether to sort in ascending order :return: calculated column """ return _rank_op(expr, CumeDist, types.float64, sort=sort, ascending=ascending)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 15; 2, function_name:lag; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 3, 12; 4, identifier:expr; 5, identifier:offset; 6, default_parameter; 6, 7; 6, 8; 7, identifier:default; 8, None; 9, default_parameter; 9, 10; 9, 11; 10, identifier:sort; 11, None; 12, default_parameter; 12, 13; 12, 14; 13, identifier:ascending; 14, True; 15, block; 15, 16; 15, 18; 16, expression_statement; 16, 17; 17, comment; 18, return_statement; 18, 19; 19, call; 19, 20; 19, 21; 20, identifier:_shift_op; 21, argument_list; 21, 22; 21, 23; 21, 24; 21, 25; 21, 28; 21, 31; 22, identifier:expr; 23, identifier:Lag; 24, identifier:offset; 25, keyword_argument; 25, 26; 25, 27; 26, identifier:default; 27, identifier:default; 28, keyword_argument; 28, 29; 28, 30; 29, identifier:sort; 30, identifier:sort; 31, keyword_argument; 31, 32; 31, 33; 32, identifier:ascending; 33, identifier:ascending
def lag(expr, offset, default=None, sort=None, ascending=True): """ Get value in the row ``offset`` rows prior to the current row. :param offset: the offset value :param default: default value for the function, when there are no rows satisfying the offset :param expr: expression for calculation :param sort: name of the sort column :param ascending: whether to sort in ascending order :return: calculated column """ return _shift_op(expr, Lag, offset, default=default, sort=sort, ascending=ascending)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 15; 2, function_name:lead; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 3, 12; 4, identifier:expr; 5, identifier:offset; 6, default_parameter; 6, 7; 6, 8; 7, identifier:default; 8, None; 9, default_parameter; 9, 10; 9, 11; 10, identifier:sort; 11, None; 12, default_parameter; 12, 13; 12, 14; 13, identifier:ascending; 14, True; 15, block; 15, 16; 15, 18; 16, expression_statement; 16, 17; 17, comment; 18, return_statement; 18, 19; 19, call; 19, 20; 19, 21; 20, identifier:_shift_op; 21, argument_list; 21, 22; 21, 23; 21, 24; 21, 25; 21, 28; 21, 31; 22, identifier:expr; 23, identifier:Lead; 24, identifier:offset; 25, keyword_argument; 25, 26; 25, 27; 26, identifier:default; 27, identifier:default; 28, keyword_argument; 28, 29; 28, 30; 29, identifier:sort; 30, identifier:sort; 31, keyword_argument; 31, 32; 31, 33; 32, identifier:ascending; 33, identifier:ascending
def lead(expr, offset, default=None, sort=None, ascending=True): """ Get value in the row ``offset`` rows after to the current row. :param offset: the offset value :param default: default value for the function, when there are no rows satisfying the offset :param expr: expression for calculation :param sort: name of the sort column :param ascending: whether to sort in ascending order :return: calculated column """ return _shift_op(expr, Lead, offset, default=default, sort=sort, ascending=ascending)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 14; 2, function_name:value_counts; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 4, identifier:expr; 5, default_parameter; 5, 6; 5, 7; 6, identifier:sort; 7, True; 8, default_parameter; 8, 9; 8, 10; 9, identifier:ascending; 10, False; 11, default_parameter; 11, 12; 11, 13; 12, identifier:dropna; 13, False; 14, block; 14, 15; 14, 17; 14, 25; 14, 35; 15, expression_statement; 15, 16; 16, comment; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 20; 19, identifier:names; 20, list:[expr.name, 'count']; 20, 21; 20, 24; 21, attribute; 21, 22; 21, 23; 22, identifier:expr; 23, identifier:name; 24, string:'count'; 25, expression_statement; 25, 26; 26, assignment; 26, 27; 26, 28; 27, identifier:typos; 28, list:[expr.dtype, types.int64]; 28, 29; 28, 32; 29, attribute; 29, 30; 29, 31; 30, identifier:expr; 31, identifier:dtype; 32, attribute; 32, 33; 32, 34; 33, identifier:types; 34, identifier:int64; 35, return_statement; 35, 36; 36, call; 36, 37; 36, 38; 37, identifier:ValueCounts; 38, argument_list; 38, 39; 38, 42; 38, 51; 38, 54; 38, 57; 39, keyword_argument; 39, 40; 39, 41; 40, identifier:_input; 41, identifier:expr; 42, keyword_argument; 42, 43; 42, 44; 43, identifier:_schema; 44, call; 44, 45; 44, 48; 45, attribute; 45, 46; 45, 47; 46, identifier:Schema; 47, identifier:from_lists; 48, argument_list; 48, 49; 48, 50; 49, identifier:names; 50, identifier:typos; 51, keyword_argument; 51, 52; 51, 53; 52, identifier:_sort; 53, identifier:sort; 54, keyword_argument; 54, 55; 54, 56; 55, identifier:_ascending; 56, identifier:ascending; 57, keyword_argument; 57, 58; 57, 59; 58, identifier:_dropna; 59, identifier:dropna
def value_counts(expr, sort=True, ascending=False, dropna=False): """ Return object containing counts of unique values. The resulting object will be in descending order so that the first element is the most frequently-occuring element. Exclude NA values by default :param expr: sequence :param sort: if sort :type sort: bool :param dropna: Don’t include counts of None, default False :return: collection with two columns :rtype: :class:`odps.df.expr.expressions.CollectionExpr` """ names = [expr.name, 'count'] typos = [expr.dtype, types.int64] return ValueCounts(_input=expr, _schema=Schema.from_lists(names, typos), _sort=sort, _ascending=ascending, _dropna=dropna)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 10; 2, function_name:last; 3, parameters; 3, 4; 3, 5; 3, 8; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:limit; 7, integer:1; 8, dictionary_splat_pattern; 8, 9; 9, identifier:kwargs; 10, block; 10, 11; 10, 13; 11, expression_statement; 11, 12; 12, comment; 13, return_statement; 13, 14; 14, call; 14, 15; 14, 18; 15, attribute; 15, 16; 15, 17; 16, identifier:self; 17, identifier:collection_instance; 18, argument_list; 18, 19; 19, call; 19, 20; 19, 44; 20, attribute; 20, 21; 20, 43; 21, call; 21, 22; 21, 25; 22, attribute; 22, 23; 22, 24; 23, identifier:self; 24, identifier:db_adapter; 25, argument_list; 25, 26; 25, 34; 26, keyword_argument; 26, 27; 26, 28; 27, identifier:db_name; 28, call; 28, 29; 28, 32; 29, attribute; 29, 30; 29, 31; 30, identifier:kwargs; 31, identifier:get; 32, argument_list; 32, 33; 33, string:'db'; 34, keyword_argument; 34, 35; 34, 36; 35, identifier:role; 36, call; 36, 37; 36, 40; 37, attribute; 37, 38; 37, 39; 38, identifier:kwargs; 39, identifier:get; 40, argument_list; 40, 41; 40, 42; 41, string:'role'; 42, string:'replica'; 43, identifier:select; 44, argument_list; 44, 45; 44, 48; 44, 51; 45, keyword_argument; 45, 46; 45, 47; 46, identifier:where; 47, string:'created_at IS NOT NULL'; 48, keyword_argument; 48, 49; 48, 50; 49, identifier:order; 50, string:'created_at DESC'; 51, keyword_argument; 51, 52; 51, 53; 52, identifier:limit; 53, identifier:limit
def last(self, limit=1, **kwargs): """ Returns the last `limit` records inserted in the model's table in the replica database. Rows are sorted by ``created_at``. """ return self.collection_instance( self.db_adapter( db_name=kwargs.get('db'), role=kwargs.get('role', 'replica') ).select( where='created_at IS NOT NULL', order='created_at DESC', limit=limit ) )
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 4; 2, function_name:_compile_itemsort; 3, parameters; 4, block; 4, 5; 4, 7; 4, 16; 4, 27; 4, 38; 4, 50; 4, 60; 4, 61; 4, 62; 4, 63; 4, 64; 4, 65; 4, 88; 4, 89; 4, 117; 5, expression_statement; 5, 6; 6, string:'''return sort function of mappings'''; 7, function_definition; 7, 8; 7, 9; 7, 11; 8, function_name:is_extra; 9, parameters; 9, 10; 10, identifier:key_; 11, block; 11, 12; 12, return_statement; 12, 13; 13, comparison_operator:is; 13, 14; 13, 15; 14, identifier:key_; 15, identifier:Extra; 16, function_definition; 16, 17; 16, 18; 16, 20; 17, function_name:is_remove; 18, parameters; 18, 19; 19, identifier:key_; 20, block; 20, 21; 21, return_statement; 21, 22; 22, call; 22, 23; 22, 24; 23, identifier:isinstance; 24, argument_list; 24, 25; 24, 26; 25, identifier:key_; 26, identifier:Remove; 27, function_definition; 27, 28; 27, 29; 27, 31; 28, function_name:is_marker; 29, parameters; 29, 30; 30, identifier:key_; 31, block; 31, 32; 32, return_statement; 32, 33; 33, call; 33, 34; 33, 35; 34, identifier:isinstance; 35, argument_list; 35, 36; 35, 37; 36, identifier:key_; 37, identifier:Marker; 38, function_definition; 38, 39; 38, 40; 38, 42; 39, function_name:is_type; 40, parameters; 40, 41; 41, identifier:key_; 42, block; 42, 43; 43, return_statement; 43, 44; 44, call; 44, 45; 44, 48; 45, attribute; 45, 46; 45, 47; 46, identifier:inspect; 47, identifier:isclass; 48, argument_list; 48, 49; 49, identifier:key_; 50, function_definition; 50, 51; 50, 52; 50, 54; 51, function_name:is_callable; 52, parameters; 52, 53; 53, identifier:key_; 54, block; 54, 55; 55, return_statement; 55, 56; 56, call; 56, 57; 56, 58; 57, identifier:callable; 58, argument_list; 58, 59; 59, identifier:key_; 60, comment; 61, comment; 62, comment; 63, comment; 64, comment; 65, expression_statement; 65, 66; 66, assignment; 66, 67; 66, 68; 67, identifier:priority; 68, list:[(1, is_remove), # Remove highest priority after values (2, is_marker), # then other Markers (4, is_type), # types/classes lowest before Extra (3, is_callable), # callables after markers (5, is_extra)]; 68, 69; 68, 72; 68, 73; 68, 76; 68, 77; 68, 80; 68, 81; 68, 84; 68, 85; 69, tuple; 69, 70; 69, 71; 70, integer:1; 71, identifier:is_remove; 72, comment; 73, tuple; 73, 74; 73, 75; 74, integer:2; 75, identifier:is_marker; 76, comment; 77, tuple; 77, 78; 77, 79; 78, integer:4; 79, identifier:is_type; 80, comment; 81, tuple; 81, 82; 81, 83; 82, integer:3; 83, identifier:is_callable; 84, comment; 85, tuple; 85, 86; 85, 87; 86, integer:5; 87, identifier:is_extra; 88, comment; 89, function_definition; 89, 90; 89, 91; 89, 93; 90, function_name:item_priority; 91, parameters; 91, 92; 92, identifier:item_; 93, block; 93, 94; 93, 100; 93, 114; 93, 115; 94, expression_statement; 94, 95; 95, assignment; 95, 96; 95, 97; 96, identifier:key_; 97, subscript; 97, 98; 97, 99; 98, identifier:item_; 99, integer:0; 100, for_statement; 100, 101; 100, 104; 100, 105; 101, pattern_list; 101, 102; 101, 103; 102, identifier:i; 103, identifier:check_; 104, identifier:priority; 105, block; 105, 106; 106, if_statement; 106, 107; 106, 111; 107, call; 107, 108; 107, 109; 108, identifier:check_; 109, argument_list; 109, 110; 110, identifier:key_; 111, block; 111, 112; 112, return_statement; 112, 113; 113, identifier:i; 114, comment; 115, return_statement; 115, 116; 116, integer:0; 117, return_statement; 117, 118; 118, identifier:item_priority
def _compile_itemsort(): '''return sort function of mappings''' def is_extra(key_): return key_ is Extra def is_remove(key_): return isinstance(key_, Remove) def is_marker(key_): return isinstance(key_, Marker) def is_type(key_): return inspect.isclass(key_) def is_callable(key_): return callable(key_) # priority list for map sorting (in order of checking) # We want Extra to match last, because it's a catch-all. On the other hand, # Remove markers should match first (since invalid values will not # raise an Error, instead the validator will check if other schemas match # the same value). priority = [(1, is_remove), # Remove highest priority after values (2, is_marker), # then other Markers (4, is_type), # types/classes lowest before Extra (3, is_callable), # callables after markers (5, is_extra)] # Extra lowest priority def item_priority(item_): key_ = item_[0] for i, check_ in priority: if check_(key_): return i # values have hightest priorities return 0 return item_priority
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:_compile_dict; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:schema; 6, block; 6, 7; 6, 9; 6, 21; 6, 25; 6, 29; 6, 85; 6, 310; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:base_validate; 12, call; 12, 13; 12, 16; 13, attribute; 13, 14; 13, 15; 14, identifier:self; 15, identifier:_compile_mapping; 16, argument_list; 16, 17; 16, 18; 17, identifier:schema; 18, keyword_argument; 18, 19; 18, 20; 19, identifier:invalid_msg; 20, string:'dictionary value'; 21, expression_statement; 21, 22; 22, assignment; 22, 23; 22, 24; 23, identifier:groups_of_exclusion; 24, dictionary; 25, expression_statement; 25, 26; 26, assignment; 26, 27; 26, 28; 27, identifier:groups_of_inclusion; 28, dictionary; 29, for_statement; 29, 30; 29, 31; 29, 32; 30, identifier:node; 31, identifier:schema; 32, block; 32, 33; 33, if_statement; 33, 34; 33, 39; 33, 59; 34, call; 34, 35; 34, 36; 35, identifier:isinstance; 36, argument_list; 36, 37; 36, 38; 37, identifier:node; 38, identifier:Exclusive; 39, block; 39, 40; 39, 52; 40, expression_statement; 40, 41; 41, assignment; 41, 42; 41, 43; 42, identifier:g; 43, call; 43, 44; 43, 47; 44, attribute; 44, 45; 44, 46; 45, identifier:groups_of_exclusion; 46, identifier:setdefault; 47, argument_list; 47, 48; 47, 51; 48, attribute; 48, 49; 48, 50; 49, identifier:node; 50, identifier:group_of_exclusion; 51, list:[]; 52, expression_statement; 52, 53; 53, call; 53, 54; 53, 57; 54, attribute; 54, 55; 54, 56; 55, identifier:g; 56, identifier:append; 57, argument_list; 57, 58; 58, identifier:node; 59, elif_clause; 59, 60; 59, 65; 60, call; 60, 61; 60, 62; 61, identifier:isinstance; 62, argument_list; 62, 63; 62, 64; 63, identifier:node; 64, identifier:Inclusive; 65, block; 65, 66; 65, 78; 66, expression_statement; 66, 67; 67, assignment; 67, 68; 67, 69; 68, identifier:g; 69, call; 69, 70; 69, 73; 70, attribute; 70, 71; 70, 72; 71, identifier:groups_of_inclusion; 72, identifier:setdefault; 73, argument_list; 73, 74; 73, 77; 74, attribute; 74, 75; 74, 76; 75, identifier:node; 76, identifier:group_of_inclusion; 77, list:[]; 78, expression_statement; 78, 79; 79, call; 79, 80; 79, 83; 80, attribute; 80, 81; 80, 82; 81, identifier:g; 82, identifier:append; 83, argument_list; 83, 84; 84, identifier:node; 85, function_definition; 85, 86; 85, 87; 85, 90; 86, function_name:validate_dict; 87, parameters; 87, 88; 87, 89; 88, identifier:path; 89, identifier:data; 90, block; 90, 91; 90, 107; 90, 111; 90, 186; 90, 196; 90, 282; 90, 292; 90, 300; 91, if_statement; 91, 92; 91, 98; 92, not_operator; 92, 93; 93, call; 93, 94; 93, 95; 94, identifier:isinstance; 95, argument_list; 95, 96; 95, 97; 96, identifier:data; 97, identifier:dict; 98, block; 98, 99; 99, raise_statement; 99, 100; 100, call; 100, 101; 100, 104; 101, attribute; 101, 102; 101, 103; 102, identifier:er; 103, identifier:DictInvalid; 104, argument_list; 104, 105; 104, 106; 105, string:'expected a dictionary'; 106, identifier:path; 107, expression_statement; 107, 108; 108, assignment; 108, 109; 108, 110; 109, identifier:errors; 110, list:[]; 111, for_statement; 111, 112; 111, 115; 111, 120; 112, pattern_list; 112, 113; 112, 114; 113, identifier:label; 114, identifier:group; 115, call; 115, 116; 115, 119; 116, attribute; 116, 117; 116, 118; 117, identifier:groups_of_exclusion; 118, identifier:items; 119, argument_list; 120, block; 120, 121; 120, 125; 121, expression_statement; 121, 122; 122, assignment; 122, 123; 122, 124; 123, identifier:exists; 124, False; 125, for_statement; 125, 126; 125, 127; 125, 128; 126, identifier:exclusive; 127, identifier:group; 128, block; 128, 129; 129, if_statement; 129, 130; 129, 135; 130, comparison_operator:in; 130, 131; 130, 134; 131, attribute; 131, 132; 131, 133; 132, identifier:exclusive; 133, identifier:schema; 134, identifier:data; 135, block; 135, 136; 135, 182; 136, if_statement; 136, 137; 136, 138; 137, identifier:exists; 138, block; 138, 139; 138, 158; 138, 168; 138, 181; 139, expression_statement; 139, 140; 140, assignment; 140, 141; 140, 142; 141, identifier:msg; 142, conditional_expression:if; 142, 143; 142, 146; 142, 155; 143, attribute; 143, 144; 143, 145; 144, identifier:exclusive; 145, identifier:msg; 146, boolean_operator:and; 146, 147; 146, 152; 147, call; 147, 148; 147, 149; 148, identifier:hasattr; 149, argument_list; 149, 150; 149, 151; 150, identifier:exclusive; 151, string:'msg'; 152, attribute; 152, 153; 152, 154; 153, identifier:exclusive; 154, identifier:msg; 155, binary_operator:%; 155, 156; 155, 157; 156, string:"two or more values in the same group of exclusion '%s'"; 157, identifier:label; 158, expression_statement; 158, 159; 159, assignment; 159, 160; 159, 161; 160, identifier:next_path; 161, binary_operator:+; 161, 162; 161, 163; 162, identifier:path; 163, list:[VirtualPathComponent(label)]; 163, 164; 164, call; 164, 165; 164, 166; 165, identifier:VirtualPathComponent; 166, argument_list; 166, 167; 167, identifier:label; 168, expression_statement; 168, 169; 169, call; 169, 170; 169, 173; 170, attribute; 170, 171; 170, 172; 171, identifier:errors; 172, identifier:append; 173, argument_list; 173, 174; 174, call; 174, 175; 174, 178; 175, attribute; 175, 176; 175, 177; 176, identifier:er; 177, identifier:ExclusiveInvalid; 178, argument_list; 178, 179; 178, 180; 179, identifier:msg; 180, identifier:next_path; 181, break_statement; 182, expression_statement; 182, 183; 183, assignment; 183, 184; 183, 185; 184, identifier:exists; 185, True; 186, if_statement; 186, 187; 186, 188; 187, identifier:errors; 188, block; 188, 189; 189, raise_statement; 189, 190; 190, call; 190, 191; 190, 194; 191, attribute; 191, 192; 191, 193; 192, identifier:er; 193, identifier:MultipleInvalid; 194, argument_list; 194, 195; 195, identifier:errors; 196, for_statement; 196, 197; 196, 200; 196, 205; 197, pattern_list; 197, 198; 197, 199; 198, identifier:label; 199, identifier:group; 200, call; 200, 201; 200, 204; 201, attribute; 201, 202; 201, 203; 202, identifier:groups_of_inclusion; 203, identifier:items; 204, argument_list; 205, block; 205, 206; 205, 218; 206, expression_statement; 206, 207; 207, assignment; 207, 208; 207, 209; 208, identifier:included; 209, list_comprehension; 209, 210; 209, 215; 210, comparison_operator:in; 210, 211; 210, 214; 211, attribute; 211, 212; 211, 213; 212, identifier:node; 213, identifier:schema; 214, identifier:data; 215, for_in_clause; 215, 216; 215, 217; 216, identifier:node; 217, identifier:group; 218, if_statement; 218, 219; 218, 229; 219, boolean_operator:and; 219, 220; 219, 224; 220, call; 220, 221; 220, 222; 221, identifier:any; 222, argument_list; 222, 223; 223, identifier:included; 224, not_operator; 224, 225; 225, call; 225, 226; 225, 227; 226, identifier:all; 227, argument_list; 227, 228; 228, identifier:included; 229, block; 229, 230; 229, 236; 229, 258; 229, 268; 229, 281; 230, expression_statement; 230, 231; 231, assignment; 231, 232; 231, 233; 232, identifier:msg; 233, binary_operator:%; 233, 234; 233, 235; 234, string:"some but not all values in the same group of inclusion '%s'"; 235, identifier:label; 236, for_statement; 236, 237; 236, 238; 236, 239; 237, identifier:g; 238, identifier:group; 239, block; 239, 240; 240, if_statement; 240, 241; 240, 250; 241, boolean_operator:and; 241, 242; 241, 247; 242, call; 242, 243; 242, 244; 243, identifier:hasattr; 244, argument_list; 244, 245; 244, 246; 245, identifier:g; 246, string:'msg'; 247, attribute; 247, 248; 247, 249; 248, identifier:g; 249, identifier:msg; 250, block; 250, 251; 250, 257; 251, expression_statement; 251, 252; 252, assignment; 252, 253; 252, 254; 253, identifier:msg; 254, attribute; 254, 255; 254, 256; 255, identifier:g; 256, identifier:msg; 257, break_statement; 258, expression_statement; 258, 259; 259, assignment; 259, 260; 259, 261; 260, identifier:next_path; 261, binary_operator:+; 261, 262; 261, 263; 262, identifier:path; 263, list:[VirtualPathComponent(label)]; 263, 264; 264, call; 264, 265; 264, 266; 265, identifier:VirtualPathComponent; 266, argument_list; 266, 267; 267, identifier:label; 268, expression_statement; 268, 269; 269, call; 269, 270; 269, 273; 270, attribute; 270, 271; 270, 272; 271, identifier:errors; 272, identifier:append; 273, argument_list; 273, 274; 274, call; 274, 275; 274, 278; 275, attribute; 275, 276; 275, 277; 276, identifier:er; 277, identifier:InclusiveInvalid; 278, argument_list; 278, 279; 278, 280; 279, identifier:msg; 280, identifier:next_path; 281, break_statement; 282, if_statement; 282, 283; 282, 284; 283, identifier:errors; 284, block; 284, 285; 285, raise_statement; 285, 286; 286, call; 286, 287; 286, 290; 287, attribute; 287, 288; 287, 289; 288, identifier:er; 289, identifier:MultipleInvalid; 290, argument_list; 290, 291; 291, identifier:errors; 292, expression_statement; 292, 293; 293, assignment; 293, 294; 293, 295; 294, identifier:out; 295, call; 295, 296; 295, 299; 296, attribute; 296, 297; 296, 298; 297, identifier:data; 298, identifier:__class__; 299, argument_list; 300, return_statement; 300, 301; 301, call; 301, 302; 301, 303; 302, identifier:base_validate; 303, argument_list; 303, 304; 303, 305; 303, 309; 304, identifier:path; 305, call; 305, 306; 305, 307; 306, identifier:iteritems; 307, argument_list; 307, 308; 308, identifier:data; 309, identifier:out; 310, return_statement; 310, 311; 311, identifier:validate_dict
def _compile_dict(self, schema): """Validate a dictionary. A dictionary schema can contain a set of values, or at most one validator function/type. A dictionary schema will only validate a dictionary: >>> validate = Schema({}) >>> with raises(er.MultipleInvalid, 'expected a dictionary'): ... validate([]) An invalid dictionary value: >>> validate = Schema({'one': 'two', 'three': 'four'}) >>> with raises(er.MultipleInvalid, "not a valid value for dictionary value @ data['one']"): ... validate({'one': 'three'}) An invalid key: >>> with raises(er.MultipleInvalid, "extra keys not allowed @ data['two']"): ... validate({'two': 'three'}) Validation function, in this case the "int" type: >>> validate = Schema({'one': 'two', 'three': 'four', int: str}) Valid integer input: >>> validate({10: 'twenty'}) {10: 'twenty'} By default, a "type" in the schema (in this case "int") will be used purely to validate that the corresponding value is of that type. It will not Coerce the value: >>> with raises(er.MultipleInvalid, "extra keys not allowed @ data['10']"): ... validate({'10': 'twenty'}) Wrap them in the Coerce() function to achieve this: >>> from voluptuous import Coerce >>> validate = Schema({'one': 'two', 'three': 'four', ... Coerce(int): str}) >>> validate({'10': 'twenty'}) {10: 'twenty'} Custom message for required key >>> validate = Schema({Required('one', 'required'): 'two'}) >>> with raises(er.MultipleInvalid, "required @ data['one']"): ... validate({}) (This is to avoid unexpected surprises.) Multiple errors for nested field in a dict: >>> validate = Schema({ ... 'adict': { ... 'strfield': str, ... 'intfield': int ... } ... }) >>> try: ... validate({ ... 'adict': { ... 'strfield': 123, ... 'intfield': 'one' ... } ... }) ... except er.MultipleInvalid as e: ... print(sorted(str(i) for i in e.errors)) # doctest: +NORMALIZE_WHITESPACE ["expected int for dictionary value @ data['adict']['intfield']", "expected str for dictionary value @ data['adict']['strfield']"] """ base_validate = self._compile_mapping( schema, invalid_msg='dictionary value') groups_of_exclusion = {} groups_of_inclusion = {} for node in schema: if isinstance(node, Exclusive): g = groups_of_exclusion.setdefault(node.group_of_exclusion, []) g.append(node) elif isinstance(node, Inclusive): g = groups_of_inclusion.setdefault(node.group_of_inclusion, []) g.append(node) def validate_dict(path, data): if not isinstance(data, dict): raise er.DictInvalid('expected a dictionary', path) errors = [] for label, group in groups_of_exclusion.items(): exists = False for exclusive in group: if exclusive.schema in data: if exists: msg = exclusive.msg if hasattr(exclusive, 'msg') and exclusive.msg else \ "two or more values in the same group of exclusion '%s'" % label next_path = path + [VirtualPathComponent(label)] errors.append(er.ExclusiveInvalid(msg, next_path)) break exists = True if errors: raise er.MultipleInvalid(errors) for label, group in groups_of_inclusion.items(): included = [node.schema in data for node in group] if any(included) and not all(included): msg = "some but not all values in the same group of inclusion '%s'" % label for g in group: if hasattr(g, 'msg') and g.msg: msg = g.msg break next_path = path + [VirtualPathComponent(label)] errors.append(er.InclusiveInvalid(msg, next_path)) break if errors: raise er.MultipleInvalid(errors) out = data.__class__() return base_validate(path, iteritems(data), out) return validate_dict
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:to_list; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:length; 6, block; 6, 7; 6, 9; 6, 39; 6, 59; 6, 74; 6, 117; 7, expression_statement; 7, 8; 8, comment; 9, if_statement; 9, 10; 9, 13; 10, comparison_operator:is; 10, 11; 10, 12; 11, identifier:length; 12, None; 13, block; 13, 14; 14, if_statement; 14, 15; 14, 21; 14, 29; 15, not_operator; 15, 16; 16, call; 16, 17; 16, 18; 17, identifier:isinstance; 18, argument_list; 18, 19; 18, 20; 19, identifier:length; 20, identifier:int; 21, block; 21, 22; 22, raise_statement; 22, 23; 23, call; 23, 24; 23, 25; 24, identifier:TypeError; 25, argument_list; 25, 26; 26, binary_operator:%; 26, 27; 26, 28; 27, string:'length must be an int, not %r'; 28, identifier:length; 29, elif_clause; 29, 30; 29, 33; 30, comparison_operator:<; 30, 31; 30, 32; 31, identifier:length; 32, integer:0; 33, block; 33, 34; 34, raise_statement; 34, 35; 35, call; 35, 36; 35, 37; 36, identifier:ValueError; 37, argument_list; 37, 38; 38, string:'length must be non-negative'; 39, if_statement; 39, 40; 39, 49; 40, binary_operator:&; 40, 41; 40, 46; 41, call; 41, 42; 41, 45; 42, attribute; 42, 43; 42, 44; 43, identifier:self; 44, identifier:_query_flags; 45, argument_list; 46, subscript; 46, 47; 46, 48; 47, identifier:_QUERY_OPTIONS; 48, string:'tailable_cursor'; 49, block; 49, 50; 50, raise_statement; 50, 51; 51, call; 51, 52; 51, 57; 52, attribute; 52, 53; 52, 56; 53, attribute; 53, 54; 53, 55; 54, identifier:pymongo; 55, identifier:errors; 56, identifier:InvalidOperation; 57, argument_list; 57, 58; 58, string:"Can't call to_list on tailable cursor"; 59, expression_statement; 59, 60; 60, assignment; 60, 61; 60, 62; 61, identifier:future; 62, call; 62, 63; 62, 68; 63, attribute; 63, 64; 63, 67; 64, attribute; 64, 65; 64, 66; 65, identifier:self; 66, identifier:_framework; 67, identifier:get_future; 68, argument_list; 68, 69; 69, call; 69, 70; 69, 73; 70, attribute; 70, 71; 70, 72; 71, identifier:self; 72, identifier:get_io_loop; 73, argument_list; 74, if_statement; 74, 75; 74, 79; 74, 87; 75, not_operator; 75, 76; 76, attribute; 76, 77; 76, 78; 77, identifier:self; 78, identifier:alive; 79, block; 79, 80; 80, expression_statement; 80, 81; 81, call; 81, 82; 81, 85; 82, attribute; 82, 83; 82, 84; 83, identifier:future; 84, identifier:set_result; 85, argument_list; 85, 86; 86, list:[]; 87, else_clause; 87, 88; 88, block; 88, 89; 88, 93; 89, expression_statement; 89, 90; 90, assignment; 90, 91; 90, 92; 91, identifier:the_list; 92, list:[]; 93, expression_statement; 93, 94; 94, call; 94, 95; 94, 100; 95, attribute; 95, 96; 95, 99; 96, attribute; 96, 97; 96, 98; 97, identifier:self; 98, identifier:_framework; 99, identifier:add_future; 100, argument_list; 100, 101; 100, 106; 100, 111; 100, 114; 100, 115; 100, 116; 101, call; 101, 102; 101, 105; 102, attribute; 102, 103; 102, 104; 103, identifier:self; 104, identifier:get_io_loop; 105, argument_list; 106, call; 106, 107; 106, 110; 107, attribute; 107, 108; 107, 109; 108, identifier:self; 109, identifier:_get_more; 110, argument_list; 111, attribute; 111, 112; 111, 113; 112, identifier:self; 113, identifier:_to_list; 114, identifier:length; 115, identifier:the_list; 116, identifier:future; 117, return_statement; 117, 118; 118, identifier:future
def to_list(self, length): """Get a list of documents. .. testsetup:: to_list MongoClient().test.test_collection.delete_many({}) MongoClient().test.test_collection.insert_many([{'_id': i} for i in range(4)]) from tornado import ioloop .. doctest:: to_list >>> from motor.motor_tornado import MotorClient >>> collection = MotorClient().test.test_collection >>> >>> @gen.coroutine ... def f(): ... cursor = collection.find().sort([('_id', 1)]) ... docs = yield cursor.to_list(length=2) ... while docs: ... print(docs) ... docs = yield cursor.to_list(length=2) ... ... print('done') ... >>> ioloop.IOLoop.current().run_sync(f) [{'_id': 0}, {'_id': 1}] [{'_id': 2}, {'_id': 3}] done :Parameters: - `length`: maximum number of documents to return for this call, or None Returns a Future. .. versionchanged:: 2.0 No longer accepts a callback argument. .. versionchanged:: 0.2 `callback` must be passed as a keyword argument, like ``to_list(10, callback=callback)``, and the `length` parameter is no longer optional. """ if length is not None: if not isinstance(length, int): raise TypeError('length must be an int, not %r' % length) elif length < 0: raise ValueError('length must be non-negative') if self._query_flags() & _QUERY_OPTIONS['tailable_cursor']: raise pymongo.errors.InvalidOperation( "Can't call to_list on tailable cursor") future = self._framework.get_future(self.get_io_loop()) if not self.alive: future.set_result([]) else: the_list = [] self._framework.add_future( self.get_io_loop(), self._get_more(), self._to_list, length, the_list, future) return future
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:find; 3, parameters; 3, 4; 3, 5; 3, 7; 4, identifier:self; 5, list_splat_pattern; 5, 6; 6, identifier:args; 7, dictionary_splat_pattern; 7, 8; 8, identifier:kwargs; 9, block; 9, 10; 9, 12; 9, 26; 9, 39; 10, expression_statement; 10, 11; 11, comment; 12, expression_statement; 12, 13; 13, assignment; 13, 14; 13, 15; 14, identifier:cursor; 15, call; 15, 16; 15, 21; 16, attribute; 16, 17; 16, 20; 17, attribute; 17, 18; 17, 19; 18, identifier:self; 19, identifier:delegate; 20, identifier:find; 21, argument_list; 21, 22; 21, 24; 22, list_splat; 22, 23; 23, identifier:args; 24, dictionary_splat; 24, 25; 25, identifier:kwargs; 26, expression_statement; 26, 27; 27, assignment; 27, 28; 27, 29; 28, identifier:grid_out_cursor; 29, call; 29, 30; 29, 31; 30, identifier:create_class_with_framework; 31, argument_list; 31, 32; 31, 33; 31, 36; 32, identifier:AgnosticGridOutCursor; 33, attribute; 33, 34; 33, 35; 34, identifier:self; 35, identifier:_framework; 36, attribute; 36, 37; 36, 38; 37, identifier:self; 38, identifier:__module__; 39, return_statement; 39, 40; 40, call; 40, 41; 40, 42; 41, identifier:grid_out_cursor; 42, argument_list; 42, 43; 42, 44; 43, identifier:cursor; 44, attribute; 44, 45; 44, 46; 45, identifier:self; 46, identifier:collection
def find(self, *args, **kwargs): """Find and return the files collection documents that match ``filter``. Returns a cursor that iterates across files matching arbitrary queries on the files collection. Can be combined with other modifiers for additional control. For example:: cursor = bucket.find({"filename": "lisa.txt"}, no_cursor_timeout=True) while (yield cursor.fetch_next): grid_out = cursor.next_object() data = yield grid_out.read() This iterates through all versions of "lisa.txt" stored in GridFS. Note that setting no_cursor_timeout to True may be important to prevent the cursor from timing out during long multi-file processing work. As another example, the call:: most_recent_three = fs.find().sort("uploadDate", -1).limit(3) would return a cursor to the three most recently uploaded files in GridFS. Follows a similar interface to :meth:`~motor.MotorCollection.find` in :class:`~motor.MotorCollection`. :Parameters: - `filter`: Search query. - `batch_size` (optional): The number of documents to return per batch. - `limit` (optional): The maximum number of documents to return. - `no_cursor_timeout` (optional): The server normally times out idle cursors after an inactivity period (10 minutes) to prevent excess memory use. Set this option to True prevent that. - `skip` (optional): The number of documents to skip before returning. - `sort` (optional): The order by which to sort results. Defaults to None. - `session` (optional): a :class:`~pymongo.client_session.ClientSession`, created with :meth:`~MotorClient.start_session`. If a :class:`~pymongo.client_session.ClientSession` is passed to :meth:`find`, all returned :class:`MotorGridOut` instances are associated with that session. .. versionchanged:: 1.2 Added session parameter. """ cursor = self.delegate.find(*args, **kwargs) grid_out_cursor = create_class_with_framework( AgnosticGridOutCursor, self._framework, self.__module__) return grid_out_cursor(cursor, self.collection)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:_serial_poller; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 205; 6, expression_statement; 6, 7; 7, comment; 8, while_statement; 8, 9; 8, 10; 9, True; 10, block; 10, 11; 10, 25; 11, expression_statement; 11, 12; 12, assignment; 12, 13; 12, 14; 13, identifier:_next; 14, call; 14, 15; 14, 16; 15, identifier:dict; 16, argument_list; 16, 17; 17, call; 17, 18; 17, 23; 18, attribute; 18, 19; 18, 22; 19, attribute; 19, 20; 19, 21; 20, identifier:self; 21, identifier:_poller; 22, identifier:poll; 23, argument_list; 23, 24; 24, identifier:POLLING_FREQUENCY_MS; 25, if_statement; 25, 26; 25, 35; 25, 62; 25, 107; 25, 169; 26, comparison_operator:in; 26, 27; 26, 34; 27, call; 27, 28; 27, 33; 28, attribute; 28, 29; 28, 32; 29, attribute; 29, 30; 29, 31; 30, identifier:self; 31, identifier:_halt_read_file; 32, identifier:fileno; 33, argument_list; 34, identifier:_next; 35, block; 35, 36; 35, 51; 35, 59; 35, 60; 35, 61; 36, expression_statement; 36, 37; 37, call; 37, 38; 37, 41; 38, attribute; 38, 39; 38, 40; 39, identifier:log; 40, identifier:debug; 41, argument_list; 41, 42; 42, call; 42, 43; 42, 46; 43, attribute; 43, 44; 43, 45; 44, string:"Poller [{}]: halt"; 45, identifier:format; 46, argument_list; 46, 47; 47, call; 47, 48; 47, 49; 48, identifier:hash; 49, argument_list; 49, 50; 50, identifier:self; 51, expression_statement; 51, 52; 52, call; 52, 53; 52, 58; 53, attribute; 53, 54; 53, 57; 54, attribute; 54, 55; 54, 56; 55, identifier:self; 56, identifier:_halt_read_file; 57, identifier:read; 58, argument_list; 59, comment; 60, comment; 61, break_statement; 62, elif_clause; 62, 63; 62, 72; 62, 73; 63, comparison_operator:in; 63, 64; 63, 71; 64, call; 64, 65; 64, 70; 65, attribute; 65, 66; 65, 69; 66, attribute; 66, 67; 66, 68; 67, identifier:self; 68, identifier:_connection; 69, identifier:fileno; 70, argument_list; 71, identifier:_next; 72, comment; 73, block; 73, 74; 73, 89; 73, 100; 74, expression_statement; 74, 75; 75, call; 75, 76; 75, 79; 76, attribute; 76, 77; 76, 78; 77, identifier:log; 78, identifier:debug; 79, argument_list; 79, 80; 80, call; 80, 81; 80, 84; 81, attribute; 81, 82; 81, 83; 82, string:"Poller [{}]: interrupt"; 83, identifier:format; 84, argument_list; 84, 85; 85, call; 85, 86; 85, 87; 86, identifier:hash; 87, argument_list; 87, 88; 88, identifier:self; 89, expression_statement; 89, 90; 90, assignment; 90, 91; 90, 92; 91, identifier:res; 92, call; 92, 93; 92, 98; 93, attribute; 93, 94; 93, 97; 94, attribute; 94, 95; 94, 96; 95, identifier:self; 96, identifier:_connection; 97, identifier:read_until; 98, argument_list; 98, 99; 99, identifier:SERIAL_ACK; 100, expression_statement; 100, 101; 101, call; 101, 102; 101, 105; 102, attribute; 102, 103; 102, 104; 103, identifier:self; 104, identifier:_interrupt_callback; 105, argument_list; 105, 106; 106, identifier:res; 107, elif_clause; 107, 108; 107, 117; 108, comparison_operator:in; 108, 109; 108, 116; 109, call; 109, 110; 109, 115; 110, attribute; 110, 111; 110, 114; 111, attribute; 111, 112; 111, 113; 112, identifier:self; 113, identifier:_send_read_file; 114, identifier:fileno; 115, argument_list; 116, identifier:_next; 117, block; 117, 118; 117, 127; 117, 139; 117, 155; 117, 164; 118, expression_statement; 118, 119; 119, call; 119, 120; 119, 125; 120, attribute; 120, 121; 120, 124; 121, attribute; 121, 122; 121, 123; 122, identifier:self; 123, identifier:_send_read_file; 124, identifier:read; 125, argument_list; 125, 126; 126, integer:1; 127, expression_statement; 127, 128; 128, assignment; 128, 129; 128, 132; 129, pattern_list; 129, 130; 129, 131; 130, identifier:command; 131, identifier:callback; 132, call; 132, 133; 132, 138; 133, attribute; 133, 134; 133, 137; 134, attribute; 134, 135; 134, 136; 135, identifier:self; 136, identifier:_command_queue; 137, identifier:get; 138, argument_list; 139, expression_statement; 139, 140; 140, call; 140, 141; 140, 144; 141, attribute; 141, 142; 141, 143; 142, identifier:log; 143, identifier:debug; 144, argument_list; 144, 145; 145, call; 145, 146; 145, 149; 146, attribute; 146, 147; 146, 148; 147, string:"Poller [{}]: send {}"; 148, identifier:format; 149, argument_list; 149, 150; 149, 154; 150, call; 150, 151; 150, 152; 151, identifier:hash; 152, argument_list; 152, 153; 153, identifier:self; 154, identifier:command; 155, expression_statement; 155, 156; 156, assignment; 156, 157; 156, 158; 157, identifier:res; 158, call; 158, 159; 158, 162; 159, attribute; 159, 160; 159, 161; 160, identifier:self; 161, identifier:_send_command; 162, argument_list; 162, 163; 163, identifier:command; 164, expression_statement; 164, 165; 165, call; 165, 166; 165, 167; 166, identifier:callback; 167, argument_list; 167, 168; 168, identifier:res; 169, else_clause; 169, 170; 169, 171; 170, comment; 171, block; 171, 172; 171, 187; 171, 198; 172, expression_statement; 172, 173; 173, call; 173, 174; 173, 177; 174, attribute; 174, 175; 174, 176; 175, identifier:log; 176, identifier:debug; 177, argument_list; 177, 178; 178, call; 178, 179; 178, 182; 179, attribute; 179, 180; 179, 181; 180, string:"Poller [{}]: updating temp"; 181, identifier:format; 182, argument_list; 182, 183; 183, call; 183, 184; 183, 185; 184, identifier:hash; 185, argument_list; 185, 186; 186, identifier:self; 187, expression_statement; 187, 188; 188, assignment; 188, 189; 188, 190; 189, identifier:res; 190, call; 190, 191; 190, 194; 191, attribute; 191, 192; 191, 193; 192, identifier:self; 193, identifier:_send_command; 194, argument_list; 194, 195; 195, subscript; 195, 196; 195, 197; 196, identifier:GCODES; 197, string:'GET_PLATE_TEMP'; 198, expression_statement; 198, 199; 199, call; 199, 200; 199, 203; 200, attribute; 200, 201; 200, 202; 201, identifier:self; 202, identifier:_status_callback; 203, argument_list; 203, 204; 204, identifier:res; 205, expression_statement; 205, 206; 206, call; 206, 207; 206, 210; 207, attribute; 207, 208; 207, 209; 208, identifier:log; 209, identifier:info; 210, argument_list; 210, 211; 211, call; 211, 212; 211, 215; 212, attribute; 212, 213; 212, 214; 213, string:"Exiting TC poller loop [{}]"; 214, identifier:format; 215, argument_list; 215, 216; 216, call; 216, 217; 216, 218; 217, identifier:hash; 218, argument_list; 218, 219; 219, identifier:self
def _serial_poller(self): """ Priority-sorted list of checks Highest priority is the 'halt' channel, which is used to kill the thread and the serial communication channel and allow everything to be cleaned up. Second is the lid-open interrupt, which should trigger a callback (typically to halt the robot). Third is an enqueued command to send to the Thermocycler. Fourth (if no other work is available) is to query the Thermocycler for its current temp, target temp, and time remaining in its current cycle. """ while True: _next = dict(self._poller.poll(POLLING_FREQUENCY_MS)) if self._halt_read_file.fileno() in _next: log.debug("Poller [{}]: halt".format(hash(self))) self._halt_read_file.read() # Note: this is discarded because we send a set message to halt # the thread--don't currently need to parse it break elif self._connection.fileno() in _next: # Lid-open interrupt log.debug("Poller [{}]: interrupt".format(hash(self))) res = self._connection.read_until(SERIAL_ACK) self._interrupt_callback(res) elif self._send_read_file.fileno() in _next: self._send_read_file.read(1) command, callback = self._command_queue.get() log.debug("Poller [{}]: send {}".format(hash(self), command)) res = self._send_command(command) callback(res) else: # Nothing else to do--update device status log.debug("Poller [{}]: updating temp".format(hash(self))) res = self._send_command(GCODES['GET_PLATE_TEMP']) self._status_callback(res) log.info("Exiting TC poller loop [{}]".format(hash(self)))
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 11; 2, function_name:enqueue_at; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 9; 4, identifier:self; 5, identifier:scheduled_time; 6, identifier:func; 7, list_splat_pattern; 7, 8; 8, identifier:args; 9, dictionary_splat_pattern; 9, 10; 10, identifier:kwargs; 11, block; 11, 12; 11, 14; 11, 24; 11, 34; 11, 44; 11, 54; 11, 64; 11, 74; 11, 107; 11, 127; 12, expression_statement; 12, 13; 13, comment; 14, expression_statement; 14, 15; 15, assignment; 15, 16; 15, 17; 16, identifier:timeout; 17, call; 17, 18; 17, 21; 18, attribute; 18, 19; 18, 20; 19, identifier:kwargs; 20, identifier:pop; 21, argument_list; 21, 22; 21, 23; 22, string:'timeout'; 23, None; 24, expression_statement; 24, 25; 25, assignment; 25, 26; 25, 27; 26, identifier:job_id; 27, call; 27, 28; 27, 31; 28, attribute; 28, 29; 28, 30; 29, identifier:kwargs; 30, identifier:pop; 31, argument_list; 31, 32; 31, 33; 32, string:'job_id'; 33, None; 34, expression_statement; 34, 35; 35, assignment; 35, 36; 35, 37; 36, identifier:job_ttl; 37, call; 37, 38; 37, 41; 38, attribute; 38, 39; 38, 40; 39, identifier:kwargs; 40, identifier:pop; 41, argument_list; 41, 42; 41, 43; 42, string:'job_ttl'; 43, None; 44, expression_statement; 44, 45; 45, assignment; 45, 46; 45, 47; 46, identifier:job_result_ttl; 47, call; 47, 48; 47, 51; 48, attribute; 48, 49; 48, 50; 49, identifier:kwargs; 50, identifier:pop; 51, argument_list; 51, 52; 51, 53; 52, string:'job_result_ttl'; 53, None; 54, expression_statement; 54, 55; 55, assignment; 55, 56; 55, 57; 56, identifier:job_description; 57, call; 57, 58; 57, 61; 58, attribute; 58, 59; 58, 60; 59, identifier:kwargs; 60, identifier:pop; 61, argument_list; 61, 62; 61, 63; 62, string:'job_description'; 63, None; 64, expression_statement; 64, 65; 65, assignment; 65, 66; 65, 67; 66, identifier:meta; 67, call; 67, 68; 67, 71; 68, attribute; 68, 69; 68, 70; 69, identifier:kwargs; 70, identifier:pop; 71, argument_list; 71, 72; 71, 73; 72, string:'meta'; 73, None; 74, expression_statement; 74, 75; 75, assignment; 75, 76; 75, 77; 76, identifier:job; 77, call; 77, 78; 77, 81; 78, attribute; 78, 79; 78, 80; 79, identifier:self; 80, identifier:_create_job; 81, argument_list; 81, 82; 81, 83; 81, 86; 81, 89; 81, 92; 81, 95; 81, 98; 81, 101; 81, 104; 82, identifier:func; 83, keyword_argument; 83, 84; 83, 85; 84, identifier:args; 85, identifier:args; 86, keyword_argument; 86, 87; 86, 88; 87, identifier:kwargs; 88, identifier:kwargs; 89, keyword_argument; 89, 90; 89, 91; 90, identifier:timeout; 91, identifier:timeout; 92, keyword_argument; 92, 93; 92, 94; 93, identifier:id; 94, identifier:job_id; 95, keyword_argument; 95, 96; 95, 97; 96, identifier:result_ttl; 97, identifier:job_result_ttl; 98, keyword_argument; 98, 99; 98, 100; 99, identifier:ttl; 100, identifier:job_ttl; 101, keyword_argument; 101, 102; 101, 103; 102, identifier:description; 103, identifier:job_description; 104, keyword_argument; 104, 105; 104, 106; 105, identifier:meta; 106, identifier:meta; 107, expression_statement; 107, 108; 108, call; 108, 109; 108, 114; 109, attribute; 109, 110; 109, 113; 110, attribute; 110, 111; 110, 112; 111, identifier:self; 112, identifier:connection; 113, identifier:zadd; 114, argument_list; 114, 115; 114, 118; 115, attribute; 115, 116; 115, 117; 116, identifier:self; 117, identifier:scheduled_jobs_key; 118, dictionary; 118, 119; 119, pair; 119, 120; 119, 123; 120, attribute; 120, 121; 120, 122; 121, identifier:job; 122, identifier:id; 123, call; 123, 124; 123, 125; 124, identifier:to_unix; 125, argument_list; 125, 126; 126, identifier:scheduled_time; 127, return_statement; 127, 128; 128, identifier:job
def enqueue_at(self, scheduled_time, func, *args, **kwargs): """ Pushes a job to the scheduler queue. The scheduled queue is a Redis sorted set ordered by timestamp - which in this case is job's scheduled execution time. All args and kwargs are passed onto the job, except for the following kwarg keys (which affect the job creation itself): - timeout - job_id - job_ttl - job_result_ttl - job_description Usage: from datetime import datetime from redis import Redis from rq.scheduler import Scheduler from foo import func redis = Redis() scheduler = Scheduler(queue_name='default', connection=redis) scheduler.enqueue_at(datetime(2020, 1, 1), func, 'argument', keyword='argument') """ timeout = kwargs.pop('timeout', None) job_id = kwargs.pop('job_id', None) job_ttl = kwargs.pop('job_ttl', None) job_result_ttl = kwargs.pop('job_result_ttl', None) job_description = kwargs.pop('job_description', None) meta = kwargs.pop('meta', None) job = self._create_job(func, args=args, kwargs=kwargs, timeout=timeout, id=job_id, result_ttl=job_result_ttl, ttl=job_ttl, description=job_description, meta=meta) self.connection.zadd(self.scheduled_jobs_key, {job.id: to_unix(scheduled_time)}) return job
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:_getMostActiveCells; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 14; 5, 29; 5, 30; 5, 43; 5, 60; 5, 71; 5, 107; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:poolingActivation; 11, attribute; 11, 12; 11, 13; 12, identifier:self; 13, identifier:_poolingActivation; 14, expression_statement; 14, 15; 15, assignment; 15, 16; 15, 17; 16, identifier:nonZeroCells; 17, subscript; 17, 18; 17, 26; 17, 28; 18, call; 18, 19; 18, 22; 19, attribute; 19, 20; 19, 21; 20, identifier:numpy; 21, identifier:argwhere; 22, argument_list; 22, 23; 23, comparison_operator:>; 23, 24; 23, 25; 24, identifier:poolingActivation; 25, integer:0; 26, slice; 26, 27; 27, colon; 28, integer:0; 29, comment; 30, expression_statement; 30, 31; 31, assignment; 31, 32; 31, 33; 32, identifier:poolingActivationSubset; 33, binary_operator:+; 33, 34; 33, 37; 33, 38; 34, subscript; 34, 35; 34, 36; 35, identifier:poolingActivation; 36, identifier:nonZeroCells; 37, line_continuation:\; 38, subscript; 38, 39; 38, 42; 39, attribute; 39, 40; 39, 41; 40, identifier:self; 41, identifier:_poolingActivation_tieBreaker; 42, identifier:nonZeroCells; 43, expression_statement; 43, 44; 44, assignment; 44, 45; 44, 46; 45, identifier:potentialUnionSDR; 46, subscript; 46, 47; 46, 48; 47, identifier:nonZeroCells; 48, subscript; 48, 49; 48, 55; 49, call; 49, 50; 49, 53; 50, attribute; 50, 51; 50, 52; 51, identifier:numpy; 52, identifier:argsort; 53, argument_list; 53, 54; 54, identifier:poolingActivationSubset; 55, slice; 55, 56; 55, 57; 55, 58; 56, colon; 57, colon; 58, unary_operator:-; 58, 59; 59, integer:1; 60, expression_statement; 60, 61; 61, assignment; 61, 62; 61, 63; 62, identifier:topCells; 63, subscript; 63, 64; 63, 65; 64, identifier:potentialUnionSDR; 65, slice; 65, 66; 65, 67; 65, 68; 66, integer:0; 67, colon; 68, attribute; 68, 69; 68, 70; 69, identifier:self; 70, identifier:_maxUnionCells; 71, if_statement; 71, 72; 71, 82; 71, 99; 72, comparison_operator:>; 72, 73; 72, 79; 73, call; 73, 74; 73, 75; 74, identifier:max; 75, argument_list; 75, 76; 76, attribute; 76, 77; 76, 78; 77, identifier:self; 78, identifier:_poolingTimer; 79, attribute; 79, 80; 79, 81; 80, identifier:self; 81, identifier:_minHistory; 82, block; 82, 83; 83, expression_statement; 83, 84; 84, assignment; 84, 85; 84, 88; 85, attribute; 85, 86; 85, 87; 86, identifier:self; 87, identifier:_unionSDR; 88, call; 88, 89; 88, 97; 89, attribute; 89, 90; 89, 96; 90, call; 90, 91; 90, 94; 91, attribute; 91, 92; 91, 93; 92, identifier:numpy; 93, identifier:sort; 94, argument_list; 94, 95; 95, identifier:topCells; 96, identifier:astype; 97, argument_list; 97, 98; 98, identifier:UINT_DTYPE; 99, else_clause; 99, 100; 100, block; 100, 101; 101, expression_statement; 101, 102; 102, assignment; 102, 103; 102, 106; 103, attribute; 103, 104; 103, 105; 104, identifier:self; 105, identifier:_unionSDR; 106, list:[]; 107, return_statement; 107, 108; 108, attribute; 108, 109; 108, 110; 109, identifier:self; 110, identifier:_unionSDR
def _getMostActiveCells(self): """ Gets the most active cells in the Union SDR having at least non-zero activation in sorted order. @return: a list of cell indices """ poolingActivation = self._poolingActivation nonZeroCells = numpy.argwhere(poolingActivation > 0)[:,0] # include a tie-breaker before sorting poolingActivationSubset = poolingActivation[nonZeroCells] + \ self._poolingActivation_tieBreaker[nonZeroCells] potentialUnionSDR = nonZeroCells[numpy.argsort(poolingActivationSubset)[::-1]] topCells = potentialUnionSDR[0: self._maxUnionCells] if max(self._poolingTimer) > self._minHistory: self._unionSDR = numpy.sort(topCells).astype(UINT_DTYPE) else: self._unionSDR = [] return self._unionSDR
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:printNetwork; 3, parameters; 3, 4; 4, identifier:network; 5, block; 5, 6; 5, 8; 5, 21; 6, expression_statement; 6, 7; 7, comment; 8, print_statement; 8, 9; 8, 10; 8, 20; 9, string:"The network has"; 10, call; 10, 11; 10, 12; 11, identifier:len; 12, argument_list; 12, 13; 13, call; 13, 14; 13, 19; 14, attribute; 14, 15; 14, 18; 15, attribute; 15, 16; 15, 17; 16, identifier:network; 17, identifier:regions; 18, identifier:values; 19, argument_list; 20, string:"regions"; 21, for_statement; 21, 22; 21, 23; 21, 31; 22, identifier:p; 23, call; 23, 24; 23, 25; 24, identifier:range; 25, argument_list; 25, 26; 26, call; 26, 27; 26, 30; 27, attribute; 27, 28; 27, 29; 28, identifier:network; 29, identifier:getMaxPhase; 30, argument_list; 31, block; 31, 32; 31, 35; 32, print_statement; 32, 33; 32, 34; 33, string:"=== Phase"; 34, identifier:p; 35, for_statement; 35, 36; 35, 37; 35, 44; 36, identifier:region; 37, call; 37, 38; 37, 43; 38, attribute; 38, 39; 38, 42; 39, attribute; 39, 40; 39, 41; 40, identifier:network; 41, identifier:regions; 42, identifier:values; 43, argument_list; 44, block; 44, 45; 45, if_statement; 45, 46; 45, 58; 46, comparison_operator:==; 46, 47; 46, 57; 47, subscript; 47, 48; 47, 56; 48, call; 48, 49; 48, 52; 49, attribute; 49, 50; 49, 51; 50, identifier:network; 51, identifier:getPhases; 52, argument_list; 52, 53; 53, attribute; 53, 54; 53, 55; 54, identifier:region; 55, identifier:name; 56, integer:0; 57, identifier:p; 58, block; 58, 59; 59, print_statement; 59, 60; 59, 61; 60, string:" "; 61, attribute; 61, 62; 61, 63; 62, identifier:region; 63, identifier:name
def printNetwork(network): """ Given a network, print out regions sorted by phase """ print "The network has",len(network.regions.values()),"regions" for p in range(network.getMaxPhase()): print "=== Phase",p for region in network.regions.values(): if network.getPhases(region.name)[0] == p: print " ",region.name
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:argmaxMulti; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:a; 5, identifier:groupKeys; 6, default_parameter; 6, 7; 6, 8; 7, identifier:assumeSorted; 8, False; 9, block; 9, 10; 9, 12; 9, 41; 9, 59; 9, 71; 9, 88; 9, 89; 9, 90; 9, 91; 9, 103; 10, expression_statement; 10, 11; 11, comment; 12, if_statement; 12, 13; 12, 15; 12, 16; 13, not_operator; 13, 14; 14, identifier:assumeSorted; 15, comment; 16, block; 16, 17; 16, 29; 16, 35; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 20; 19, identifier:sorter; 20, call; 20, 21; 20, 24; 21, attribute; 21, 22; 21, 23; 22, identifier:np; 23, identifier:argsort; 24, argument_list; 24, 25; 24, 26; 25, identifier:groupKeys; 26, keyword_argument; 26, 27; 26, 28; 27, identifier:kind; 28, string:"mergesort"; 29, expression_statement; 29, 30; 30, assignment; 30, 31; 30, 32; 31, identifier:a; 32, subscript; 32, 33; 32, 34; 33, identifier:a; 34, identifier:sorter; 35, expression_statement; 35, 36; 36, assignment; 36, 37; 36, 38; 37, identifier:groupKeys; 38, subscript; 38, 39; 38, 40; 39, identifier:groupKeys; 40, identifier:sorter; 41, expression_statement; 41, 42; 42, assignment; 42, 43; 42, 47; 43, pattern_list; 43, 44; 43, 45; 43, 46; 44, identifier:_; 45, identifier:indices; 46, identifier:lengths; 47, call; 47, 48; 47, 51; 48, attribute; 48, 49; 48, 50; 49, identifier:np; 50, identifier:unique; 51, argument_list; 51, 52; 51, 53; 51, 56; 52, identifier:groupKeys; 53, keyword_argument; 53, 54; 53, 55; 54, identifier:return_index; 55, True; 56, keyword_argument; 56, 57; 56, 58; 57, identifier:return_counts; 58, True; 59, expression_statement; 59, 60; 60, assignment; 60, 61; 60, 62; 61, identifier:maxValues; 62, call; 62, 63; 62, 68; 63, attribute; 63, 64; 63, 67; 64, attribute; 64, 65; 64, 66; 65, identifier:np; 66, identifier:maximum; 67, identifier:reduceat; 68, argument_list; 68, 69; 68, 70; 69, identifier:a; 70, identifier:indices; 71, expression_statement; 71, 72; 72, assignment; 72, 73; 72, 74; 73, identifier:allMaxIndices; 74, call; 74, 75; 74, 78; 75, attribute; 75, 76; 75, 77; 76, identifier:np; 77, identifier:flatnonzero; 78, argument_list; 78, 79; 79, comparison_operator:==; 79, 80; 79, 87; 80, call; 80, 81; 80, 84; 81, attribute; 81, 82; 81, 83; 82, identifier:np; 83, identifier:repeat; 84, argument_list; 84, 85; 84, 86; 85, identifier:maxValues; 86, identifier:lengths; 87, identifier:a; 88, comment; 89, comment; 90, comment; 91, expression_statement; 91, 92; 92, assignment; 92, 93; 92, 94; 93, identifier:indices; 94, subscript; 94, 95; 94, 96; 95, identifier:allMaxIndices; 96, call; 96, 97; 96, 100; 97, attribute; 97, 98; 97, 99; 98, identifier:np; 99, identifier:searchsorted; 100, argument_list; 100, 101; 100, 102; 101, identifier:allMaxIndices; 102, identifier:indices; 103, if_statement; 103, 104; 103, 105; 103, 108; 104, identifier:assumeSorted; 105, block; 105, 106; 106, return_statement; 106, 107; 107, identifier:indices; 108, else_clause; 108, 109; 109, block; 109, 110; 110, return_statement; 110, 111; 111, subscript; 111, 112; 111, 113; 112, identifier:sorter; 113, identifier:indices
def argmaxMulti(a, groupKeys, assumeSorted=False): """ This is like numpy's argmax, but it returns multiple maximums. It gets the indices of the max values of each group in 'a', grouping the elements by their corresponding value in 'groupKeys'. @param a (numpy array) An array of values that will be compared @param groupKeys (numpy array) An array with the same length of 'a'. Each entry identifies the group for each 'a' value. @param assumeSorted (bool) If true, group keys must be organized together (e.g. sorted). @return (numpy array) The indices of one maximum value per group @example _argmaxMulti([5, 4, 7, 2, 9, 8], [0, 0, 0, 1, 1, 1]) returns [2, 4] """ if not assumeSorted: # Use a stable sort algorithm sorter = np.argsort(groupKeys, kind="mergesort") a = a[sorter] groupKeys = groupKeys[sorter] _, indices, lengths = np.unique(groupKeys, return_index=True, return_counts=True) maxValues = np.maximum.reduceat(a, indices) allMaxIndices = np.flatnonzero(np.repeat(maxValues, lengths) == a) # Break ties by finding the insertion points of the the group start indices # and using the values currently at those points. This approach will choose # the first occurrence of each max value. indices = allMaxIndices[np.searchsorted(allMaxIndices, indices)] if assumeSorted: return indices else: return sorter[indices]
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:getAllCellsInColumns; 3, parameters; 3, 4; 3, 5; 4, identifier:columns; 5, identifier:cellsPerColumn; 6, block; 6, 7; 6, 9; 6, 10; 6, 11; 6, 12; 6, 13; 6, 14; 6, 15; 6, 16; 6, 17; 6, 18; 6, 19; 6, 20; 7, expression_statement; 7, 8; 8, comment; 9, comment; 10, comment; 11, comment; 12, comment; 13, comment; 14, comment; 15, comment; 16, comment; 17, comment; 18, comment; 19, comment; 20, return_statement; 20, 21; 21, call; 21, 22; 21, 47; 22, attribute; 22, 23; 22, 46; 23, parenthesized_expression; 23, 24; 24, binary_operator:+; 24, 25; 24, 37; 25, call; 25, 26; 25, 32; 26, attribute; 26, 27; 26, 31; 27, parenthesized_expression; 27, 28; 28, binary_operator:*; 28, 29; 28, 30; 29, identifier:columns; 30, identifier:cellsPerColumn; 31, identifier:reshape; 32, argument_list; 32, 33; 33, tuple; 33, 34; 33, 36; 34, unary_operator:-; 34, 35; 35, integer:1; 36, integer:1; 37, call; 37, 38; 37, 41; 38, attribute; 38, 39; 38, 40; 39, identifier:np; 40, identifier:arange; 41, argument_list; 41, 42; 41, 43; 42, identifier:cellsPerColumn; 43, keyword_argument; 43, 44; 43, 45; 44, identifier:dtype; 45, string:"uint32"; 46, identifier:flatten; 47, argument_list
def getAllCellsInColumns(columns, cellsPerColumn): """ Calculate all cell indices in the specified columns. @param columns (numpy array) @param cellsPerColumn (int) @return (numpy array) All cells within the specified columns. The cells are in the same order as the provided columns, so they're sorted if the columns are sorted. """ # Add # [[beginningOfColumn0], # [beginningOfColumn1], # ...] # to # [0, 1, 2, ..., cellsPerColumn - 1] # to get # [beginningOfColumn0 + 0, beginningOfColumn0 + 1, ... # beginningOfColumn1 + 0, ... # ...] # then flatten it. return ((columns * cellsPerColumn).reshape((-1, 1)) + np.arange(cellsPerColumn, dtype="uint32")).flatten()
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:shuffle_sparse_matrix_and_labels; 3, parameters; 3, 4; 3, 5; 4, identifier:matrix; 5, identifier:labels; 6, block; 6, 7; 6, 9; 6, 11; 6, 19; 6, 29; 6, 38; 6, 47; 6, 56; 6, 58; 7, expression_statement; 7, 8; 8, comment; 9, print_statement; 9, 10; 10, string:"Shuffling data"; 11, expression_statement; 11, 12; 12, assignment; 12, 13; 12, 14; 13, identifier:new_matrix; 14, call; 14, 15; 14, 18; 15, attribute; 15, 16; 15, 17; 16, identifier:matrix; 17, identifier:toDense; 18, argument_list; 19, expression_statement; 19, 20; 20, assignment; 20, 21; 20, 22; 21, identifier:rng_state; 22, call; 22, 23; 22, 28; 23, attribute; 23, 24; 23, 27; 24, attribute; 24, 25; 24, 26; 25, identifier:numpy; 26, identifier:random; 27, identifier:get_state; 28, argument_list; 29, expression_statement; 29, 30; 30, call; 30, 31; 30, 36; 31, attribute; 31, 32; 31, 35; 32, attribute; 32, 33; 32, 34; 33, identifier:numpy; 34, identifier:random; 35, identifier:shuffle; 36, argument_list; 36, 37; 37, identifier:new_matrix; 38, expression_statement; 38, 39; 39, call; 39, 40; 39, 45; 40, attribute; 40, 41; 40, 44; 41, attribute; 41, 42; 41, 43; 42, identifier:numpy; 43, identifier:random; 44, identifier:set_state; 45, argument_list; 45, 46; 46, identifier:rng_state; 47, expression_statement; 47, 48; 48, call; 48, 49; 48, 54; 49, attribute; 49, 50; 49, 53; 50, attribute; 50, 51; 50, 52; 51, identifier:numpy; 52, identifier:random; 53, identifier:shuffle; 54, argument_list; 54, 55; 55, identifier:labels; 56, print_statement; 56, 57; 57, string:"Data shuffled"; 58, return_statement; 58, 59; 59, expression_list; 59, 60; 59, 64; 60, call; 60, 61; 60, 62; 61, identifier:SM32; 62, argument_list; 62, 63; 63, identifier:new_matrix; 64, call; 64, 65; 64, 68; 65, attribute; 65, 66; 65, 67; 66, identifier:numpy; 67, identifier:asarray; 68, argument_list; 68, 69; 69, identifier:labels
def shuffle_sparse_matrix_and_labels(matrix, labels): """ Shuffles a sparse matrix and set of labels together. Resorts to densifying and then re-sparsifying the matrix, for convenience. Still very fast. """ print "Shuffling data" new_matrix = matrix.toDense() rng_state = numpy.random.get_state() numpy.random.shuffle(new_matrix) numpy.random.set_state(rng_state) numpy.random.shuffle(labels) print "Data shuffled" return SM32(new_matrix), numpy.asarray(labels)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:compute; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:self; 5, identifier:sensorToBodyByColumn; 6, identifier:sensorToSpecificObjectByColumn; 7, block; 7, 8; 7, 10; 7, 24; 7, 30; 7, 106; 7, 124; 7, 125; 7, 126; 7, 127; 7, 128; 7, 142; 7, 159; 8, expression_statement; 8, 9; 9, comment; 10, expression_statement; 10, 11; 11, assignment; 11, 12; 11, 13; 12, identifier:votesByCell; 13, call; 13, 14; 13, 17; 14, attribute; 14, 15; 14, 16; 15, identifier:np; 16, identifier:zeros; 17, argument_list; 17, 18; 17, 21; 18, attribute; 18, 19; 18, 20; 19, identifier:self; 20, identifier:cellCount; 21, keyword_argument; 21, 22; 21, 23; 22, identifier:dtype; 23, string:"int"; 24, expression_statement; 24, 25; 25, assignment; 25, 26; 25, 29; 26, attribute; 26, 27; 26, 28; 27, identifier:self; 28, identifier:activeSegmentsByColumn; 29, list:[]; 30, for_statement; 30, 31; 30, 35; 30, 43; 31, tuple_pattern; 31, 32; 31, 33; 31, 34; 32, identifier:connections; 33, identifier:activeSensorToBodyCells; 34, identifier:activeSensorToSpecificObjectCells; 35, call; 35, 36; 35, 37; 36, identifier:zip; 37, argument_list; 37, 38; 37, 41; 37, 42; 38, attribute; 38, 39; 38, 40; 39, identifier:self; 40, identifier:connectionsByColumn; 41, identifier:sensorToBodyByColumn; 42, identifier:sensorToSpecificObjectByColumn; 43, block; 43, 44; 43, 59; 43, 72; 43, 81; 43, 90; 43, 91; 43, 97; 44, expression_statement; 44, 45; 45, assignment; 45, 46; 45, 47; 46, identifier:overlaps; 47, call; 47, 48; 47, 51; 48, attribute; 48, 49; 48, 50; 49, identifier:connections; 50, identifier:computeActivity; 51, argument_list; 51, 52; 52, dictionary; 52, 53; 52, 56; 53, pair; 53, 54; 53, 55; 54, string:"sensorToBody"; 55, identifier:activeSensorToBodyCells; 56, pair; 56, 57; 56, 58; 57, string:"sensorToSpecificObject"; 58, identifier:activeSensorToSpecificObjectCells; 59, expression_statement; 59, 60; 60, assignment; 60, 61; 60, 62; 61, identifier:activeSegments; 62, subscript; 62, 63; 62, 71; 63, call; 63, 64; 63, 67; 64, attribute; 64, 65; 64, 66; 65, identifier:np; 66, identifier:where; 67, argument_list; 67, 68; 68, comparison_operator:>=; 68, 69; 68, 70; 69, identifier:overlaps; 70, integer:2; 71, integer:0; 72, expression_statement; 72, 73; 73, assignment; 73, 74; 73, 75; 74, identifier:votes; 75, call; 75, 76; 75, 79; 76, attribute; 76, 77; 76, 78; 77, identifier:connections; 78, identifier:mapSegmentsToCells; 79, argument_list; 79, 80; 80, identifier:activeSegments; 81, expression_statement; 81, 82; 82, assignment; 82, 83; 82, 84; 83, identifier:votes; 84, call; 84, 85; 84, 88; 85, attribute; 85, 86; 85, 87; 86, identifier:np; 87, identifier:unique; 88, argument_list; 88, 89; 89, identifier:votes; 90, comment; 91, expression_statement; 91, 92; 92, augmented_assignment:+=; 92, 93; 92, 96; 93, subscript; 93, 94; 93, 95; 94, identifier:votesByCell; 95, identifier:votes; 96, integer:1; 97, expression_statement; 97, 98; 98, call; 98, 99; 98, 104; 99, attribute; 99, 100; 99, 103; 100, attribute; 100, 101; 100, 102; 101, identifier:self; 102, identifier:activeSegmentsByColumn; 103, identifier:append; 104, argument_list; 104, 105; 105, identifier:activeSegments; 106, expression_statement; 106, 107; 107, assignment; 107, 108; 107, 109; 108, identifier:candidates; 109, subscript; 109, 110; 109, 123; 110, call; 110, 111; 110, 114; 111, attribute; 111, 112; 111, 113; 112, identifier:np; 113, identifier:where; 114, argument_list; 114, 115; 115, comparison_operator:==; 115, 116; 115, 117; 116, identifier:votesByCell; 117, call; 117, 118; 117, 121; 118, attribute; 118, 119; 118, 120; 119, identifier:np; 120, identifier:max; 121, argument_list; 121, 122; 122, identifier:votesByCell; 123, integer:0; 124, comment; 125, comment; 126, comment; 127, comment; 128, expression_statement; 128, 129; 129, assignment; 129, 130; 129, 133; 130, attribute; 130, 131; 130, 132; 131, identifier:self; 132, identifier:activeCells; 133, call; 133, 134; 133, 137; 134, attribute; 134, 135; 134, 136; 135, identifier:np; 136, identifier:intersect1d; 137, argument_list; 137, 138; 137, 141; 138, attribute; 138, 139; 138, 140; 139, identifier:self; 140, identifier:activeCells; 141, identifier:candidates; 142, if_statement; 142, 143; 142, 150; 142, 151; 142, 152; 143, comparison_operator:==; 143, 144; 143, 149; 144, attribute; 144, 145; 144, 148; 145, attribute; 145, 146; 145, 147; 146, identifier:self; 147, identifier:activeCells; 148, identifier:size; 149, integer:0; 150, comment; 151, comment; 152, block; 152, 153; 153, expression_statement; 153, 154; 154, assignment; 154, 155; 154, 158; 155, attribute; 155, 156; 155, 157; 156, identifier:self; 157, identifier:activeCells; 158, identifier:candidates; 159, expression_statement; 159, 160; 160, assignment; 160, 161; 160, 164; 161, attribute; 161, 162; 161, 163; 162, identifier:self; 163, identifier:inhibitedCells; 164, call; 164, 165; 164, 168; 165, attribute; 165, 166; 165, 167; 166, identifier:np; 167, identifier:setdiff1d; 168, argument_list; 168, 169; 168, 179; 169, subscript; 169, 170; 169, 178; 170, call; 170, 171; 170, 174; 171, attribute; 171, 172; 171, 173; 172, identifier:np; 173, identifier:where; 174, argument_list; 174, 175; 175, comparison_operator:>; 175, 176; 175, 177; 176, identifier:votesByCell; 177, integer:0; 178, integer:0; 179, attribute; 179, 180; 179, 181; 180, identifier:self; 181, identifier:activeCells
def compute(self, sensorToBodyByColumn, sensorToSpecificObjectByColumn): """ Compute the "body's location relative to a specific object" from an array of "sensor's location relative to a specific object" and an array of "sensor's location relative to body" These arrays consist of one module per cortical column. This is a metric computation, similar to that of the SensorToSpecificObjectModule, but with voting. In effect, the columns vote on "the body's location relative to a specific object". Note: Each column can vote for an arbitrary number of cells, but it can't vote for a single cell more than once. This is necessary because we don't want ambiguity in a column to cause some cells to get extra votes. There are a few ways that this could be biologically plausible: - Explanation 1: Nearby dendritic segments are independent coincidence detectors, but perhaps their dendritic spikes don't sum. Meanwhile, maybe dendritic spikes from far away dendritic segments do sum. - Explanation 2: Dendritic spikes from different columns are separated temporally, not spatially. All the spikes from one column "arrive" at the cell at the same time, but the dendritic spikes from other columns arrive at other times. With each of these temporally-separated dendritic spikes, the unsupported cells are inhibited, or the spikes' effects are summed. - Explanation 3: Another population of cells within the cortical column might calculate the "body's location relative to a specific object" in this same "metric" way, but without tallying any votes. Then it relays this SDR subcortically, voting 0 or 1 times for each cell. @param sensorToBodyInputs (list of numpy arrays) The "sensor's location relative to the body" input from each cortical column @param sensorToSpecificObjectInputs (list of numpy arrays) The "sensor's location relative to specific object" input from each cortical column """ votesByCell = np.zeros(self.cellCount, dtype="int") self.activeSegmentsByColumn = [] for (connections, activeSensorToBodyCells, activeSensorToSpecificObjectCells) in zip(self.connectionsByColumn, sensorToBodyByColumn, sensorToSpecificObjectByColumn): overlaps = connections.computeActivity({ "sensorToBody": activeSensorToBodyCells, "sensorToSpecificObject": activeSensorToSpecificObjectCells, }) activeSegments = np.where(overlaps >= 2)[0] votes = connections.mapSegmentsToCells(activeSegments) votes = np.unique(votes) # Only allow a column to vote for a cell once. votesByCell[votes] += 1 self.activeSegmentsByColumn.append(activeSegments) candidates = np.where(votesByCell == np.max(votesByCell))[0] # If possible, select only from current active cells. # # If we were to always activate all candidates, there would be an explosive # back-and-forth between this layer and the sensorToSpecificObject layer. self.activeCells = np.intersect1d(self.activeCells, candidates) if self.activeCells.size == 0: # Otherwise, activate all cells with the maximum number of active # segments. self.activeCells = candidates self.inhibitedCells = np.setdiff1d(np.where(votesByCell > 0)[0], self.activeCells)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:metricCompute; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:self; 5, identifier:sensorToBody; 6, identifier:bodyToSpecificObject; 7, block; 7, 8; 7, 10; 7, 27; 7, 42; 8, expression_statement; 8, 9; 9, comment; 10, expression_statement; 10, 11; 11, assignment; 11, 12; 11, 13; 12, identifier:overlaps; 13, call; 13, 14; 13, 19; 14, attribute; 14, 15; 14, 18; 15, attribute; 15, 16; 15, 17; 16, identifier:self; 17, identifier:metricConnections; 18, identifier:computeActivity; 19, argument_list; 19, 20; 20, dictionary; 20, 21; 20, 24; 21, pair; 21, 22; 21, 23; 22, string:"bodyToSpecificObject"; 23, identifier:bodyToSpecificObject; 24, pair; 24, 25; 24, 26; 25, string:"sensorToBody"; 26, identifier:sensorToBody; 27, expression_statement; 27, 28; 28, assignment; 28, 29; 28, 32; 29, attribute; 29, 30; 29, 31; 30, identifier:self; 31, identifier:activeMetricSegments; 32, subscript; 32, 33; 32, 41; 33, call; 33, 34; 33, 37; 34, attribute; 34, 35; 34, 36; 35, identifier:np; 36, identifier:where; 37, argument_list; 37, 38; 38, comparison_operator:>=; 38, 39; 38, 40; 39, identifier:overlaps; 40, integer:2; 41, integer:0; 42, expression_statement; 42, 43; 43, assignment; 43, 44; 43, 47; 44, attribute; 44, 45; 44, 46; 45, identifier:self; 46, identifier:activeCells; 47, call; 47, 48; 47, 51; 48, attribute; 48, 49; 48, 50; 49, identifier:np; 50, identifier:unique; 51, argument_list; 51, 52; 52, call; 52, 53; 52, 58; 53, attribute; 53, 54; 53, 57; 54, attribute; 54, 55; 54, 56; 55, identifier:self; 56, identifier:metricConnections; 57, identifier:mapSegmentsToCells; 58, argument_list; 58, 59; 59, attribute; 59, 60; 59, 61; 60, identifier:self; 61, identifier:activeMetricSegments
def metricCompute(self, sensorToBody, bodyToSpecificObject): """ Compute the "sensor's location relative to a specific object" from the "body's location relative to a specific object" and the "sensor's location relative to body" @param sensorToBody (numpy array) Active cells of a single module that represents the sensor's location relative to the body @param bodyToSpecificObject (numpy array) Active cells of a single module that represents the body's location relative to a specific object """ overlaps = self.metricConnections.computeActivity({ "bodyToSpecificObject": bodyToSpecificObject, "sensorToBody": sensorToBody, }) self.activeMetricSegments = np.where(overlaps >= 2)[0] self.activeCells = np.unique( self.metricConnections.mapSegmentsToCells( self.activeMetricSegments))
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 20; 2, function_name:compute; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 14; 3, 17; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:feedforwardInput; 7, tuple; 8, default_parameter; 8, 9; 8, 10; 9, identifier:lateralInputs; 10, tuple; 11, default_parameter; 11, 12; 11, 13; 12, identifier:feedforwardGrowthCandidates; 13, None; 14, default_parameter; 14, 15; 14, 16; 15, identifier:learn; 16, True; 17, default_parameter; 17, 18; 17, 19; 18, identifier:predictedInput; 19, None; 20, block; 20, 21; 20, 23; 20, 32; 20, 33; 21, expression_statement; 21, 22; 22, comment; 23, if_statement; 23, 24; 23, 27; 24, comparison_operator:is; 24, 25; 24, 26; 25, identifier:feedforwardGrowthCandidates; 26, None; 27, block; 27, 28; 28, expression_statement; 28, 29; 29, assignment; 29, 30; 29, 31; 30, identifier:feedforwardGrowthCandidates; 31, identifier:feedforwardInput; 32, comment; 33, if_statement; 33, 34; 33, 36; 33, 45; 33, 46; 33, 61; 33, 62; 34, not_operator; 34, 35; 35, identifier:learn; 36, block; 36, 37; 37, expression_statement; 37, 38; 38, call; 38, 39; 38, 42; 39, attribute; 39, 40; 39, 41; 40, identifier:self; 41, identifier:_computeInferenceMode; 42, argument_list; 42, 43; 42, 44; 43, identifier:feedforwardInput; 44, identifier:lateralInputs; 45, comment; 46, elif_clause; 46, 47; 46, 51; 47, not_operator; 47, 48; 48, attribute; 48, 49; 48, 50; 49, identifier:self; 50, identifier:onlineLearning; 51, block; 51, 52; 52, expression_statement; 52, 53; 53, call; 53, 54; 53, 57; 54, attribute; 54, 55; 54, 56; 55, identifier:self; 56, identifier:_computeLearningMode; 57, argument_list; 57, 58; 57, 59; 57, 60; 58, identifier:feedforwardInput; 59, identifier:lateralInputs; 60, identifier:feedforwardGrowthCandidates; 61, comment; 62, else_clause; 62, 63; 63, block; 63, 64; 64, if_statement; 64, 65; 64, 78; 64, 116; 64, 151; 65, parenthesized_expression; 65, 66; 66, boolean_operator:and; 66, 67; 66, 70; 67, comparison_operator:is; 67, 68; 67, 69; 68, identifier:predictedInput; 69, None; 70, comparison_operator:>; 70, 71; 70, 75; 71, call; 71, 72; 71, 73; 72, identifier:len; 73, argument_list; 73, 74; 74, identifier:predictedInput; 75, attribute; 75, 76; 75, 77; 76, identifier:self; 77, identifier:predictedInhibitionThreshold; 78, block; 78, 79; 78, 89; 78, 99; 78, 107; 79, expression_statement; 79, 80; 80, assignment; 80, 81; 80, 82; 81, identifier:predictedActiveInput; 82, call; 82, 83; 82, 86; 83, attribute; 83, 84; 83, 85; 84, identifier:numpy; 85, identifier:intersect1d; 86, argument_list; 86, 87; 86, 88; 87, identifier:feedforwardInput; 88, identifier:predictedInput; 89, expression_statement; 89, 90; 90, assignment; 90, 91; 90, 92; 91, identifier:predictedGrowthCandidates; 92, call; 92, 93; 92, 96; 93, attribute; 93, 94; 93, 95; 94, identifier:numpy; 95, identifier:intersect1d; 96, argument_list; 96, 97; 96, 98; 97, identifier:feedforwardGrowthCandidates; 98, identifier:predictedInput; 99, expression_statement; 99, 100; 100, call; 100, 101; 100, 104; 101, attribute; 101, 102; 101, 103; 102, identifier:self; 103, identifier:_computeInferenceMode; 104, argument_list; 104, 105; 104, 106; 105, identifier:predictedActiveInput; 106, identifier:lateralInputs; 107, expression_statement; 107, 108; 108, call; 108, 109; 108, 112; 109, attribute; 109, 110; 109, 111; 110, identifier:self; 111, identifier:_computeLearningMode; 112, argument_list; 112, 113; 112, 114; 112, 115; 113, identifier:predictedActiveInput; 114, identifier:lateralInputs; 115, identifier:feedforwardGrowthCandidates; 116, elif_clause; 116, 117; 116, 131; 116, 132; 116, 133; 117, not_operator; 117, 118; 118, comparison_operator:<=; 118, 119; 118, 122; 118, 128; 119, attribute; 119, 120; 119, 121; 120, identifier:self; 121, identifier:minSdrSize; 122, call; 122, 123; 122, 124; 123, identifier:len; 124, argument_list; 124, 125; 125, attribute; 125, 126; 125, 127; 126, identifier:self; 127, identifier:activeCells; 128, attribute; 128, 129; 128, 130; 129, identifier:self; 130, identifier:maxSdrSize; 131, comment; 132, comment; 133, block; 133, 134; 133, 142; 134, expression_statement; 134, 135; 135, call; 135, 136; 135, 139; 136, attribute; 136, 137; 136, 138; 137, identifier:self; 138, identifier:_computeInferenceMode; 139, argument_list; 139, 140; 139, 141; 140, identifier:feedforwardInput; 141, identifier:lateralInputs; 142, expression_statement; 142, 143; 143, call; 143, 144; 143, 147; 144, attribute; 144, 145; 144, 146; 145, identifier:self; 146, identifier:_computeLearningMode; 147, argument_list; 147, 148; 147, 149; 147, 150; 148, identifier:feedforwardInput; 149, identifier:lateralInputs; 150, identifier:feedforwardGrowthCandidates; 151, else_clause; 151, 152; 151, 153; 151, 154; 152, comment; 153, comment; 154, block; 154, 155; 155, expression_statement; 155, 156; 156, call; 156, 157; 156, 160; 157, attribute; 157, 158; 157, 159; 158, identifier:self; 159, identifier:_computeLearningMode; 160, argument_list; 160, 161; 160, 162; 160, 163; 161, identifier:feedforwardInput; 162, identifier:lateralInputs; 163, identifier:feedforwardGrowthCandidates
def compute(self, feedforwardInput=(), lateralInputs=(), feedforwardGrowthCandidates=None, learn=True, predictedInput = None,): """ Runs one time step of the column pooler algorithm. @param feedforwardInput (sequence) Sorted indices of active feedforward input bits @param lateralInputs (list of sequences) For each lateral layer, a list of sorted indices of active lateral input bits @param feedforwardGrowthCandidates (sequence or None) Sorted indices of feedforward input bits that active cells may grow new synapses to. If None, the entire feedforwardInput is used. @param learn (bool) If True, we are learning a new object @param predictedInput (sequence) Sorted indices of predicted cells in the TM layer. """ if feedforwardGrowthCandidates is None: feedforwardGrowthCandidates = feedforwardInput # inference step if not learn: self._computeInferenceMode(feedforwardInput, lateralInputs) # learning step elif not self.onlineLearning: self._computeLearningMode(feedforwardInput, lateralInputs, feedforwardGrowthCandidates) # online learning step else: if (predictedInput is not None and len(predictedInput) > self.predictedInhibitionThreshold): predictedActiveInput = numpy.intersect1d(feedforwardInput, predictedInput) predictedGrowthCandidates = numpy.intersect1d( feedforwardGrowthCandidates, predictedInput) self._computeInferenceMode(predictedActiveInput, lateralInputs) self._computeLearningMode(predictedActiveInput, lateralInputs, feedforwardGrowthCandidates) elif not self.minSdrSize <= len(self.activeCells) <= self.maxSdrSize: # If the pooler doesn't have a single representation, try to infer one, # before actually attempting to learn. self._computeInferenceMode(feedforwardInput, lateralInputs) self._computeLearningMode(feedforwardInput, lateralInputs, feedforwardGrowthCandidates) else: # If there isn't predicted input and we have a single SDR, # we are extending that representation and should just learn. self._computeLearningMode(feedforwardInput, lateralInputs, feedforwardGrowthCandidates)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 17; 2, function_name:_learn; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 3, 9; 3, 10; 3, 11; 3, 12; 3, 13; 3, 14; 3, 15; 3, 16; 4, comment; 5, identifier:permanences; 6, identifier:rng; 7, comment; 8, identifier:activeCells; 9, identifier:activeInput; 10, identifier:growthCandidateInput; 11, comment; 12, identifier:sampleSize; 13, identifier:initialPermanence; 14, identifier:permanenceIncrement; 15, identifier:permanenceDecrement; 16, identifier:connectedPermanence; 17, block; 17, 18; 17, 20; 17, 29; 17, 39; 17, 48; 18, expression_statement; 18, 19; 19, comment; 20, expression_statement; 20, 21; 21, call; 21, 22; 21, 25; 22, attribute; 22, 23; 22, 24; 23, identifier:permanences; 24, identifier:incrementNonZerosOnOuter; 25, argument_list; 25, 26; 25, 27; 25, 28; 26, identifier:activeCells; 27, identifier:activeInput; 28, identifier:permanenceIncrement; 29, expression_statement; 29, 30; 30, call; 30, 31; 30, 34; 31, attribute; 31, 32; 31, 33; 32, identifier:permanences; 33, identifier:incrementNonZerosOnRowsExcludingCols; 34, argument_list; 34, 35; 34, 36; 34, 37; 35, identifier:activeCells; 36, identifier:activeInput; 37, unary_operator:-; 37, 38; 38, identifier:permanenceDecrement; 39, expression_statement; 39, 40; 40, call; 40, 41; 40, 44; 41, attribute; 41, 42; 41, 43; 42, identifier:permanences; 43, identifier:clipRowsBelowAndAbove; 44, argument_list; 44, 45; 44, 46; 44, 47; 45, identifier:activeCells; 46, float:0.0; 47, float:1.0; 48, if_statement; 48, 49; 48, 53; 48, 63; 49, comparison_operator:==; 49, 50; 49, 51; 50, identifier:sampleSize; 51, unary_operator:-; 51, 52; 52, integer:1; 53, block; 53, 54; 54, expression_statement; 54, 55; 55, call; 55, 56; 55, 59; 56, attribute; 56, 57; 56, 58; 57, identifier:permanences; 58, identifier:setZerosOnOuter; 59, argument_list; 59, 60; 59, 61; 59, 62; 60, identifier:activeCells; 61, identifier:activeInput; 62, identifier:initialPermanence; 63, else_clause; 63, 64; 64, block; 64, 65; 64, 75; 64, 90; 64, 101; 65, expression_statement; 65, 66; 66, assignment; 66, 67; 66, 68; 67, identifier:existingSynapseCounts; 68, call; 68, 69; 68, 72; 69, attribute; 69, 70; 69, 71; 70, identifier:permanences; 71, identifier:nNonZerosPerRowOnCols; 72, argument_list; 72, 73; 72, 74; 73, identifier:activeCells; 74, identifier:activeInput; 75, expression_statement; 75, 76; 76, assignment; 76, 77; 76, 78; 77, identifier:maxNewByCell; 78, call; 78, 79; 78, 82; 79, attribute; 79, 80; 79, 81; 80, identifier:numpy; 81, identifier:empty; 82, argument_list; 82, 83; 82, 87; 83, call; 83, 84; 83, 85; 84, identifier:len; 85, argument_list; 85, 86; 86, identifier:activeCells; 87, keyword_argument; 87, 88; 87, 89; 88, identifier:dtype; 89, string:"int32"; 90, expression_statement; 90, 91; 91, call; 91, 92; 91, 95; 92, attribute; 92, 93; 92, 94; 93, identifier:numpy; 94, identifier:subtract; 95, argument_list; 95, 96; 95, 97; 95, 98; 96, identifier:sampleSize; 97, identifier:existingSynapseCounts; 98, keyword_argument; 98, 99; 98, 100; 99, identifier:out; 100, identifier:maxNewByCell; 101, expression_statement; 101, 102; 102, call; 102, 103; 102, 106; 103, attribute; 103, 104; 103, 105; 104, identifier:permanences; 105, identifier:setRandomZerosOnOuter; 106, argument_list; 106, 107; 106, 108; 106, 109; 106, 110; 106, 111; 107, identifier:activeCells; 108, identifier:growthCandidateInput; 109, identifier:maxNewByCell; 110, identifier:initialPermanence; 111, identifier:rng
def _learn(# mutated args permanences, rng, # activity activeCells, activeInput, growthCandidateInput, # configuration sampleSize, initialPermanence, permanenceIncrement, permanenceDecrement, connectedPermanence): """ For each active cell, reinforce active synapses, punish inactive synapses, and grow new synapses to a subset of the active input bits that the cell isn't already connected to. Parameters: ---------------------------- @param permanences (SparseMatrix) Matrix of permanences, with cells as rows and inputs as columns @param rng (Random) Random number generator @param activeCells (sorted sequence) Sorted list of the cells that are learning @param activeInput (sorted sequence) Sorted list of active bits in the input @param growthCandidateInput (sorted sequence) Sorted list of active bits in the input that the activeCells may grow new synapses to For remaining parameters, see the __init__ docstring. """ permanences.incrementNonZerosOnOuter( activeCells, activeInput, permanenceIncrement) permanences.incrementNonZerosOnRowsExcludingCols( activeCells, activeInput, -permanenceDecrement) permanences.clipRowsBelowAndAbove( activeCells, 0.0, 1.0) if sampleSize == -1: permanences.setZerosOnOuter( activeCells, activeInput, initialPermanence) else: existingSynapseCounts = permanences.nNonZerosPerRowOnCols( activeCells, activeInput) maxNewByCell = numpy.empty(len(activeCells), dtype="int32") numpy.subtract(sampleSize, existingSynapseCounts, out=maxNewByCell) permanences.setRandomZerosOnOuter( activeCells, growthCandidateInput, maxNewByCell, initialPermanence, rng)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 20; 2, function_name:compute; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 14; 3, 17; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:deltaLocation; 7, tuple; 8, default_parameter; 8, 9; 8, 10; 9, identifier:newLocation; 10, tuple; 11, default_parameter; 11, 12; 11, 13; 12, identifier:featureLocationInput; 13, tuple; 14, default_parameter; 14, 15; 14, 16; 15, identifier:featureLocationGrowthCandidates; 16, tuple; 17, default_parameter; 17, 18; 17, 19; 18, identifier:learn; 19, True; 20, block; 20, 21; 20, 23; 20, 29; 20, 74; 20, 75; 20, 126; 21, expression_statement; 21, 22; 22, comment; 23, expression_statement; 23, 24; 24, assignment; 24, 25; 24, 26; 25, identifier:prevActiveCells; 26, attribute; 26, 27; 26, 28; 27, identifier:self; 28, identifier:activeCells; 29, expression_statement; 29, 30; 30, assignment; 30, 31; 30, 34; 31, attribute; 31, 32; 31, 33; 32, identifier:self; 33, identifier:activeDeltaSegments; 34, subscript; 34, 35; 34, 73; 35, call; 35, 36; 35, 39; 36, attribute; 36, 37; 36, 38; 37, identifier:np; 38, identifier:where; 39, argument_list; 39, 40; 40, binary_operator:&; 40, 41; 40, 57; 41, parenthesized_expression; 41, 42; 42, comparison_operator:>=; 42, 43; 42, 54; 43, call; 43, 44; 43, 49; 44, attribute; 44, 45; 44, 48; 45, attribute; 45, 46; 45, 47; 46, identifier:self; 47, identifier:internalConnections; 48, identifier:computeActivity; 49, argument_list; 49, 50; 49, 51; 50, identifier:prevActiveCells; 51, attribute; 51, 52; 51, 53; 52, identifier:self; 53, identifier:connectedPermanence; 54, attribute; 54, 55; 54, 56; 55, identifier:self; 56, identifier:activationThreshold; 57, parenthesized_expression; 57, 58; 58, comparison_operator:>=; 58, 59; 58, 70; 59, call; 59, 60; 59, 65; 60, attribute; 60, 61; 60, 64; 61, attribute; 61, 62; 61, 63; 62, identifier:self; 63, identifier:deltaConnections; 64, identifier:computeActivity; 65, argument_list; 65, 66; 65, 67; 66, identifier:deltaLocation; 67, attribute; 67, 68; 67, 69; 68, identifier:self; 69, identifier:connectedPermanence; 70, attribute; 70, 71; 70, 72; 71, identifier:self; 72, identifier:activationThreshold; 73, integer:0; 74, comment; 75, if_statement; 75, 76; 75, 82; 75, 110; 76, comparison_operator:==; 76, 77; 76, 81; 77, call; 77, 78; 77, 79; 78, identifier:len; 79, argument_list; 79, 80; 80, identifier:deltaLocation; 81, integer:0; 82, block; 82, 83; 83, expression_statement; 83, 84; 84, assignment; 84, 85; 84, 88; 85, attribute; 85, 86; 85, 87; 86, identifier:self; 87, identifier:activeFeatureLocationSegments; 88, subscript; 88, 89; 88, 109; 89, call; 89, 90; 89, 93; 90, attribute; 90, 91; 90, 92; 91, identifier:np; 92, identifier:where; 93, argument_list; 93, 94; 94, comparison_operator:>=; 94, 95; 94, 106; 95, call; 95, 96; 95, 101; 96, attribute; 96, 97; 96, 100; 97, attribute; 97, 98; 97, 99; 98, identifier:self; 99, identifier:featureLocationConnections; 100, identifier:computeActivity; 101, argument_list; 101, 102; 101, 103; 102, identifier:featureLocationInput; 103, attribute; 103, 104; 103, 105; 104, identifier:self; 105, identifier:connectedPermanence; 106, attribute; 106, 107; 106, 108; 107, identifier:self; 108, identifier:activationThreshold; 109, integer:0; 110, else_clause; 110, 111; 111, block; 111, 112; 112, expression_statement; 112, 113; 113, assignment; 113, 114; 113, 117; 114, attribute; 114, 115; 114, 116; 115, identifier:self; 116, identifier:activeFeatureLocationSegments; 117, call; 117, 118; 117, 121; 118, attribute; 118, 119; 118, 120; 119, identifier:np; 120, identifier:empty; 121, argument_list; 121, 122; 121, 123; 122, integer:0; 123, keyword_argument; 123, 124; 123, 125; 124, identifier:dtype; 125, string:"uint32"; 126, if_statement; 126, 127; 126, 133; 126, 134; 126, 164; 126, 255; 127, comparison_operator:>; 127, 128; 127, 132; 128, call; 128, 129; 128, 130; 129, identifier:len; 130, argument_list; 130, 131; 131, identifier:newLocation; 132, integer:0; 133, comment; 134, block; 134, 135; 134, 141; 135, expression_statement; 135, 136; 136, assignment; 136, 137; 136, 140; 137, attribute; 137, 138; 137, 139; 138, identifier:self; 139, identifier:activeCells; 140, identifier:newLocation; 141, if_statement; 141, 142; 141, 143; 141, 144; 142, identifier:learn; 143, comment; 144, block; 144, 145; 144, 154; 144, 155; 145, expression_statement; 145, 146; 146, call; 146, 147; 146, 150; 147, attribute; 147, 148; 147, 149; 148, identifier:self; 149, identifier:_learnTransition; 150, argument_list; 150, 151; 150, 152; 150, 153; 151, identifier:prevActiveCells; 152, identifier:deltaLocation; 153, identifier:newLocation; 154, comment; 155, expression_statement; 155, 156; 156, call; 156, 157; 156, 160; 157, attribute; 157, 158; 157, 159; 158, identifier:self; 159, identifier:_learnFeatureLocationPair; 160, argument_list; 160, 161; 160, 162; 160, 163; 161, identifier:newLocation; 162, identifier:featureLocationInput; 163, identifier:featureLocationGrowthCandidates; 164, elif_clause; 164, 165; 164, 171; 165, comparison_operator:>; 165, 166; 165, 170; 166, call; 166, 167; 166, 168; 167, identifier:len; 168, argument_list; 168, 169; 169, identifier:prevActiveCells; 170, integer:0; 171, block; 171, 172; 172, if_statement; 172, 173; 172, 179; 172, 180; 172, 181; 172, 182; 172, 207; 173, comparison_operator:>; 173, 174; 173, 178; 174, call; 174, 175; 174, 176; 175, identifier:len; 176, argument_list; 176, 177; 177, identifier:deltaLocation; 178, integer:0; 179, comment; 180, comment; 181, comment; 182, block; 182, 183; 182, 196; 183, expression_statement; 183, 184; 184, assignment; 184, 185; 184, 186; 185, identifier:cellsForDeltaSegments; 186, call; 186, 187; 186, 192; 187, attribute; 187, 188; 187, 191; 188, attribute; 188, 189; 188, 190; 189, identifier:self; 190, identifier:internalConnections; 191, identifier:mapSegmentsToCells; 192, argument_list; 192, 193; 193, attribute; 193, 194; 193, 195; 194, identifier:self; 195, identifier:activeDeltaSegments; 196, expression_statement; 196, 197; 197, assignment; 197, 198; 197, 201; 198, attribute; 198, 199; 198, 200; 199, identifier:self; 200, identifier:activeCells; 201, call; 201, 202; 201, 205; 202, attribute; 202, 203; 202, 204; 203, identifier:np; 204, identifier:unique; 205, argument_list; 205, 206; 206, identifier:cellsForDeltaSegments; 207, else_clause; 207, 208; 207, 209; 207, 210; 208, comment; 209, comment; 210, block; 210, 211; 211, if_statement; 211, 212; 211, 220; 211, 247; 212, comparison_operator:>; 212, 213; 212, 219; 213, call; 213, 214; 213, 215; 214, identifier:len; 215, argument_list; 215, 216; 216, attribute; 216, 217; 216, 218; 217, identifier:self; 218, identifier:activeFeatureLocationSegments; 219, integer:0; 220, block; 220, 221; 220, 235; 221, expression_statement; 221, 222; 222, assignment; 222, 223; 222, 224; 223, identifier:cellsForFeatureLocationSegments; 224, parenthesized_expression; 224, 225; 225, call; 225, 226; 225, 231; 226, attribute; 226, 227; 226, 230; 227, attribute; 227, 228; 227, 229; 228, identifier:self; 229, identifier:featureLocationConnections; 230, identifier:mapSegmentsToCells; 231, argument_list; 231, 232; 232, attribute; 232, 233; 232, 234; 233, identifier:self; 234, identifier:activeFeatureLocationSegments; 235, expression_statement; 235, 236; 236, assignment; 236, 237; 236, 240; 237, attribute; 237, 238; 237, 239; 238, identifier:self; 239, identifier:activeCells; 240, call; 240, 241; 240, 244; 241, attribute; 241, 242; 241, 243; 242, identifier:np; 243, identifier:intersect1d; 244, argument_list; 244, 245; 244, 246; 245, identifier:prevActiveCells; 246, identifier:cellsForFeatureLocationSegments; 247, else_clause; 247, 248; 248, block; 248, 249; 249, expression_statement; 249, 250; 250, assignment; 250, 251; 250, 254; 251, attribute; 251, 252; 251, 253; 252, identifier:self; 253, identifier:activeCells; 254, identifier:prevActiveCells; 255, elif_clause; 255, 256; 255, 262; 255, 263; 256, comparison_operator:>; 256, 257; 256, 261; 257, call; 257, 258; 257, 259; 258, identifier:len; 259, argument_list; 259, 260; 260, identifier:featureLocationInput; 261, integer:0; 262, comment; 263, block; 263, 264; 263, 278; 264, expression_statement; 264, 265; 265, assignment; 265, 266; 265, 267; 266, identifier:cellsForFeatureLocationSegments; 267, parenthesized_expression; 267, 268; 268, call; 268, 269; 268, 274; 269, attribute; 269, 270; 269, 273; 270, attribute; 270, 271; 270, 272; 271, identifier:self; 272, identifier:featureLocationConnections; 273, identifier:mapSegmentsToCells; 274, argument_list; 274, 275; 275, attribute; 275, 276; 275, 277; 276, identifier:self; 277, identifier:activeFeatureLocationSegments; 278, expression_statement; 278, 279; 279, assignment; 279, 280; 279, 283; 280, attribute; 280, 281; 280, 282; 281, identifier:self; 282, identifier:activeCells; 283, call; 283, 284; 283, 287; 284, attribute; 284, 285; 284, 286; 285, identifier:np; 286, identifier:unique; 287, argument_list; 287, 288; 288, identifier:cellsForFeatureLocationSegments
def compute(self, deltaLocation=(), newLocation=(), featureLocationInput=(), featureLocationGrowthCandidates=(), learn=True): """ Run one time step of the Location Memory algorithm. @param deltaLocation (sorted numpy array) @param newLocation (sorted numpy array) @param featureLocationInput (sorted numpy array) @param featureLocationGrowthCandidates (sorted numpy array) """ prevActiveCells = self.activeCells self.activeDeltaSegments = np.where( (self.internalConnections.computeActivity( prevActiveCells, self.connectedPermanence ) >= self.activationThreshold) & (self.deltaConnections.computeActivity( deltaLocation, self.connectedPermanence ) >= self.activationThreshold))[0] # When we're moving, the feature-location input has no effect. if len(deltaLocation) == 0: self.activeFeatureLocationSegments = np.where( self.featureLocationConnections.computeActivity( featureLocationInput, self.connectedPermanence ) >= self.activationThreshold)[0] else: self.activeFeatureLocationSegments = np.empty(0, dtype="uint32") if len(newLocation) > 0: # Drive activations by relaying this location SDR. self.activeCells = newLocation if learn: # Learn the delta. self._learnTransition(prevActiveCells, deltaLocation, newLocation) # Learn the featureLocationInput. self._learnFeatureLocationPair(newLocation, featureLocationInput, featureLocationGrowthCandidates) elif len(prevActiveCells) > 0: if len(deltaLocation) > 0: # Drive activations by applying the deltaLocation to the current location. # Completely ignore the featureLocationInput. It's outdated, associated # with the previous location. cellsForDeltaSegments = self.internalConnections.mapSegmentsToCells( self.activeDeltaSegments) self.activeCells = np.unique(cellsForDeltaSegments) else: # Keep previous active cells active. # Modulate with the featureLocationInput. if len(self.activeFeatureLocationSegments) > 0: cellsForFeatureLocationSegments = ( self.featureLocationConnections.mapSegmentsToCells( self.activeFeatureLocationSegments)) self.activeCells = np.intersect1d(prevActiveCells, cellsForFeatureLocationSegments) else: self.activeCells = prevActiveCells elif len(featureLocationInput) > 0: # Drive activations with the featureLocationInput. cellsForFeatureLocationSegments = ( self.featureLocationConnections.mapSegmentsToCells( self.activeFeatureLocationSegments)) self.activeCells = np.unique(cellsForFeatureLocationSegments)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 13; 2, function_name:getmerge; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 10; 4, identifier:self; 5, identifier:path; 6, identifier:dst; 7, default_parameter; 7, 8; 7, 9; 8, identifier:newline; 9, False; 10, default_parameter; 10, 11; 10, 12; 11, identifier:check_crc; 12, False; 13, block; 13, 14; 13, 16; 13, 25; 13, 34; 13, 40; 13, 48; 13, 70; 14, expression_statement; 14, 15; 15, string:''' Get all the files in the directories that match the source file pattern and merge and sort them to only one file on local fs. :param paths: Directory containing files that will be merged :type paths: string :param dst: Path of file that will be written :type dst: string :param nl: Add a newline character at the end of each file. :type nl: boolean :returns: string content of the merged file at dst '''; 16, if_statement; 16, 17; 16, 19; 17, not_operator; 17, 18; 18, identifier:path; 19, block; 19, 20; 20, raise_statement; 20, 21; 21, call; 21, 22; 21, 23; 22, identifier:InvalidInputException; 23, argument_list; 23, 24; 24, string:"getmerge: no path given"; 25, if_statement; 25, 26; 25, 28; 26, not_operator; 26, 27; 27, identifier:dst; 28, block; 28, 29; 29, raise_statement; 29, 30; 30, call; 30, 31; 30, 32; 31, identifier:InvalidInputException; 32, argument_list; 32, 33; 33, string:"getmerge: no destination given"; 34, expression_statement; 34, 35; 35, assignment; 35, 36; 35, 37; 36, identifier:temporary_target; 37, binary_operator:%; 37, 38; 37, 39; 38, string:"%s._COPYING_"; 39, identifier:dst; 40, expression_statement; 40, 41; 41, assignment; 41, 42; 41, 43; 42, identifier:f; 43, call; 43, 44; 43, 45; 44, identifier:open; 45, argument_list; 45, 46; 45, 47; 46, identifier:temporary_target; 47, string:'w'; 48, expression_statement; 48, 49; 49, assignment; 49, 50; 49, 51; 50, identifier:processor; 51, lambda; 51, 52; 51, 61; 52, lambda_parameters; 52, 53; 52, 54; 52, 55; 52, 58; 53, identifier:path; 54, identifier:node; 55, default_parameter; 55, 56; 55, 57; 56, identifier:dst; 57, identifier:dst; 58, default_parameter; 58, 59; 58, 60; 59, identifier:check_crc; 60, identifier:check_crc; 61, call; 61, 62; 61, 65; 62, attribute; 62, 63; 62, 64; 63, identifier:self; 64, identifier:_handle_getmerge; 65, argument_list; 65, 66; 65, 67; 65, 68; 65, 69; 66, identifier:path; 67, identifier:node; 68, identifier:dst; 69, identifier:check_crc; 70, try_statement; 70, 71; 70, 176; 71, block; 71, 72; 71, 156; 72, for_statement; 72, 73; 72, 74; 72, 91; 73, identifier:item; 74, call; 74, 75; 74, 78; 75, attribute; 75, 76; 75, 77; 76, identifier:self; 77, identifier:_find_items; 78, argument_list; 78, 79; 78, 81; 78, 82; 78, 85; 78, 88; 79, list:[path]; 79, 80; 80, identifier:path; 81, identifier:processor; 82, keyword_argument; 82, 83; 82, 84; 83, identifier:include_toplevel; 84, True; 85, keyword_argument; 85, 86; 85, 87; 86, identifier:recurse; 87, False; 88, keyword_argument; 88, 89; 88, 90; 89, identifier:include_children; 90, True; 91, block; 91, 92; 91, 142; 92, for_statement; 92, 93; 92, 94; 92, 95; 93, identifier:load; 94, identifier:item; 95, block; 95, 96; 96, if_statement; 96, 97; 96, 100; 96, 110; 97, subscript; 97, 98; 97, 99; 98, identifier:load; 99, string:'result'; 100, block; 100, 101; 101, expression_statement; 101, 102; 102, call; 102, 103; 102, 106; 103, attribute; 103, 104; 103, 105; 104, identifier:f; 105, identifier:write; 106, argument_list; 106, 107; 107, subscript; 107, 108; 107, 109; 108, identifier:load; 109, string:'response'; 110, elif_clause; 110, 111; 110, 117; 111, not_operator; 111, 112; 112, comparison_operator:is; 112, 113; 112, 116; 113, subscript; 113, 114; 113, 115; 114, identifier:load; 115, string:'error'; 116, string:''; 117, block; 117, 118; 117, 135; 118, if_statement; 118, 119; 118, 127; 119, call; 119, 120; 119, 125; 120, attribute; 120, 121; 120, 124; 121, attribute; 121, 122; 121, 123; 122, identifier:os; 123, identifier:path; 124, identifier:isfile; 125, argument_list; 125, 126; 126, identifier:temporary_target; 127, block; 127, 128; 128, expression_statement; 128, 129; 129, call; 129, 130; 129, 133; 130, attribute; 130, 131; 130, 132; 131, identifier:os; 132, identifier:remove; 133, argument_list; 133, 134; 134, identifier:temporary_target; 135, raise_statement; 135, 136; 136, call; 136, 137; 136, 138; 137, identifier:FatalException; 138, argument_list; 138, 139; 139, subscript; 139, 140; 139, 141; 140, identifier:load; 141, string:'error'; 142, if_statement; 142, 143; 142, 148; 143, boolean_operator:and; 143, 144; 143, 145; 144, identifier:newline; 145, subscript; 145, 146; 145, 147; 146, identifier:load; 147, string:'response'; 148, block; 148, 149; 149, expression_statement; 149, 150; 150, call; 150, 151; 150, 154; 151, attribute; 151, 152; 151, 153; 152, identifier:f; 153, identifier:write; 154, argument_list; 154, 155; 155, string:"\n"; 156, expression_statement; 156, 157; 157, yield; 157, 158; 158, dictionary; 158, 159; 158, 162; 158, 165; 158, 168; 158, 173; 159, pair; 159, 160; 159, 161; 160, string:"path"; 161, identifier:dst; 162, pair; 162, 163; 162, 164; 163, string:"response"; 164, string:''; 165, pair; 165, 166; 165, 167; 166, string:"result"; 167, True; 168, pair; 168, 169; 168, 170; 169, string:"error"; 170, subscript; 170, 171; 170, 172; 171, identifier:load; 172, string:'error'; 173, pair; 173, 174; 173, 175; 174, string:"source_path"; 175, identifier:path; 176, finally_clause; 176, 177; 177, block; 177, 178; 178, if_statement; 178, 179; 178, 187; 179, call; 179, 180; 179, 185; 180, attribute; 180, 181; 180, 184; 181, attribute; 181, 182; 181, 183; 182, identifier:os; 183, identifier:path; 184, identifier:isfile; 185, argument_list; 185, 186; 186, identifier:temporary_target; 187, block; 187, 188; 187, 194; 188, expression_statement; 188, 189; 189, call; 189, 190; 189, 193; 190, attribute; 190, 191; 190, 192; 191, identifier:f; 192, identifier:close; 193, argument_list; 194, expression_statement; 194, 195; 195, call; 195, 196; 195, 199; 196, attribute; 196, 197; 196, 198; 197, identifier:os; 198, identifier:rename; 199, argument_list; 199, 200; 199, 201; 200, identifier:temporary_target; 201, identifier:dst
def getmerge(self, path, dst, newline=False, check_crc=False): ''' Get all the files in the directories that match the source file pattern and merge and sort them to only one file on local fs. :param paths: Directory containing files that will be merged :type paths: string :param dst: Path of file that will be written :type dst: string :param nl: Add a newline character at the end of each file. :type nl: boolean :returns: string content of the merged file at dst ''' if not path: raise InvalidInputException("getmerge: no path given") if not dst: raise InvalidInputException("getmerge: no destination given") temporary_target = "%s._COPYING_" % dst f = open(temporary_target, 'w') processor = lambda path, node, dst=dst, check_crc=check_crc: self._handle_getmerge(path, node, dst, check_crc) try: for item in self._find_items([path], processor, include_toplevel=True, recurse=False, include_children=True): for load in item: if load['result']: f.write(load['response']) elif not load['error'] is '': if os.path.isfile(temporary_target): os.remove(temporary_target) raise FatalException(load['error']) if newline and load['response']: f.write("\n") yield {"path": dst, "response": '', "result": True, "error": load['error'], "source_path": path} finally: if os.path.isfile(temporary_target): f.close() os.rename(temporary_target, dst)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:countByValue; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 6, expression_statement; 6, 7; 7, comment; 8, return_statement; 8, 9; 9, call; 9, 10; 9, 13; 10, attribute; 10, 11; 10, 12; 11, identifier:self; 12, identifier:transform; 13, argument_list; 13, 14; 14, lambda; 14, 15; 14, 17; 15, lambda_parameters; 15, 16; 16, identifier:rdd; 17, call; 17, 18; 17, 25; 18, attribute; 18, 19; 18, 24; 19, attribute; 19, 20; 19, 23; 20, attribute; 20, 21; 20, 22; 21, identifier:self; 22, identifier:_context; 23, identifier:_context; 24, identifier:parallelize; 25, argument_list; 25, 26; 26, call; 26, 27; 26, 34; 27, attribute; 27, 28; 27, 33; 28, call; 28, 29; 28, 32; 29, attribute; 29, 30; 29, 31; 30, identifier:rdd; 31, identifier:countByValue; 32, argument_list; 33, identifier:items; 34, argument_list
def countByValue(self): """Apply countByValue to every RDD.abs :rtype: DStream .. warning:: Implemented as a local operation. Example: >>> import pysparkling >>> sc = pysparkling.Context() >>> ssc = pysparkling.streaming.StreamingContext(sc, 0.1) >>> ( ... ssc ... .queueStream([[1, 1, 5, 5, 5, 2]]) ... .countByValue() ... .foreachRDD(lambda rdd: print(sorted(rdd.collect()))) ... ) >>> ssc.start() >>> ssc.awaitTermination(0.15) [(1, 2), (2, 1), (5, 3)] """ return self.transform( lambda rdd: self._context._context.parallelize( rdd.countByValue().items()))
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:_contiguous_offsets; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:offsets; 6, block; 6, 7; 6, 9; 6, 15; 6, 43; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, call; 10, 11; 10, 14; 11, attribute; 11, 12; 11, 13; 12, identifier:offsets; 13, identifier:sort; 14, argument_list; 15, for_statement; 15, 16; 15, 17; 15, 26; 16, identifier:i; 17, call; 17, 18; 17, 19; 18, identifier:range; 19, argument_list; 19, 20; 20, binary_operator:-; 20, 21; 20, 25; 21, call; 21, 22; 21, 23; 22, identifier:len; 23, argument_list; 23, 24; 24, identifier:offsets; 25, integer:1; 26, block; 26, 27; 27, assert_statement; 27, 28; 27, 39; 28, comparison_operator:==; 28, 29; 28, 34; 29, binary_operator:+; 29, 30; 29, 33; 30, subscript; 30, 31; 30, 32; 31, identifier:offsets; 32, identifier:i; 33, integer:1; 34, subscript; 34, 35; 34, 36; 35, identifier:offsets; 36, binary_operator:+; 36, 37; 36, 38; 37, identifier:i; 38, integer:1; 39, binary_operator:%; 39, 40; 39, 41; 40, string:"Offsets not contiguous: %s"; 41, tuple; 41, 42; 42, identifier:offsets; 43, return_statement; 43, 44; 44, identifier:offsets
def _contiguous_offsets(self, offsets): """ Sorts the input list of integer offsets, ensures that values are contiguous. """ offsets.sort() for i in range(len(offsets) - 1): assert offsets[i] + 1 == offsets[i + 1], \ "Offsets not contiguous: %s" % (offsets,) return offsets
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:_collapse; 3, parameters; 3, 4; 4, identifier:intervals; 5, block; 5, 6; 5, 8; 5, 12; 5, 67; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:span; 11, None; 12, for_statement; 12, 13; 12, 16; 12, 17; 13, pattern_list; 13, 14; 13, 15; 14, identifier:start; 15, identifier:stop; 16, identifier:intervals; 17, block; 17, 18; 18, if_statement; 18, 19; 18, 22; 18, 31; 18, 49; 19, comparison_operator:is; 19, 20; 19, 21; 20, identifier:span; 21, None; 22, block; 22, 23; 23, expression_statement; 23, 24; 24, assignment; 24, 25; 24, 26; 25, identifier:span; 26, call; 26, 27; 26, 28; 27, identifier:_Interval; 28, argument_list; 28, 29; 28, 30; 29, identifier:start; 30, identifier:stop; 31, elif_clause; 31, 32; 31, 38; 32, comparison_operator:<=; 32, 33; 32, 34; 32, 37; 33, identifier:start; 34, attribute; 34, 35; 34, 36; 35, identifier:span; 36, identifier:stop; 37, identifier:stop; 38, block; 38, 39; 39, expression_statement; 39, 40; 40, assignment; 40, 41; 40, 42; 41, identifier:span; 42, call; 42, 43; 42, 44; 43, identifier:_Interval; 44, argument_list; 44, 45; 44, 48; 45, attribute; 45, 46; 45, 47; 46, identifier:span; 47, identifier:start; 48, identifier:stop; 49, elif_clause; 49, 50; 49, 55; 50, comparison_operator:>; 50, 51; 50, 52; 51, identifier:start; 52, attribute; 52, 53; 52, 54; 53, identifier:span; 54, identifier:stop; 55, block; 55, 56; 55, 59; 56, expression_statement; 56, 57; 57, yield; 57, 58; 58, identifier:span; 59, expression_statement; 59, 60; 60, assignment; 60, 61; 60, 62; 61, identifier:span; 62, call; 62, 63; 62, 64; 63, identifier:_Interval; 64, argument_list; 64, 65; 64, 66; 65, identifier:start; 66, identifier:stop; 67, if_statement; 67, 68; 67, 71; 68, comparison_operator:is; 68, 69; 68, 70; 69, identifier:span; 70, None; 71, block; 71, 72; 72, expression_statement; 72, 73; 73, yield; 73, 74; 74, identifier:span
def _collapse(intervals): """ Collapse an iterable of intervals sorted by start coord. """ span = None for start, stop in intervals: if span is None: span = _Interval(start, stop) elif start <= span.stop < stop: span = _Interval(span.start, stop) elif start > span.stop: yield span span = _Interval(start, stop) if span is not None: yield span
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 14; 2, function_name:remove; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:index; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:hash; 10, None; 11, default_parameter; 11, 12; 11, 13; 12, identifier:keepSorted; 13, True; 14, block; 14, 15; 14, 17; 14, 34; 14, 145; 14, 162; 15, expression_statement; 15, 16; 16, comment; 17, if_statement; 17, 18; 17, 21; 18, comparison_operator:is; 18, 19; 18, 20; 19, identifier:index; 20, None; 21, block; 21, 22; 22, expression_statement; 22, 23; 23, call; 23, 24; 23, 27; 24, attribute; 24, 25; 24, 26; 25, identifier:clibrebound; 26, identifier:reb_remove; 27, argument_list; 27, 28; 27, 32; 27, 33; 28, call; 28, 29; 28, 30; 29, identifier:byref; 30, argument_list; 30, 31; 31, identifier:self; 32, identifier:index; 33, identifier:keepSorted; 34, if_statement; 34, 35; 34, 38; 35, comparison_operator:is; 35, 36; 35, 37; 36, identifier:hash; 37, None; 38, block; 38, 39; 38, 46; 38, 56; 38, 82; 39, expression_statement; 39, 40; 40, assignment; 40, 41; 40, 42; 41, identifier:hash_types; 42, expression_list; 42, 43; 42, 44; 42, 45; 43, identifier:c_uint32; 44, identifier:c_uint; 45, identifier:c_ulong; 46, expression_statement; 46, 47; 47, assignment; 47, 48; 47, 49; 48, identifier:PY3; 49, comparison_operator:==; 49, 50; 49, 55; 50, subscript; 50, 51; 50, 54; 51, attribute; 51, 52; 51, 53; 52, identifier:sys; 53, identifier:version_info; 54, integer:0; 55, integer:3; 56, if_statement; 56, 57; 56, 58; 56, 69; 57, identifier:PY3; 58, block; 58, 59; 58, 64; 59, expression_statement; 59, 60; 60, assignment; 60, 61; 60, 62; 61, identifier:string_types; 62, expression_list; 62, 63; 63, identifier:str; 64, expression_statement; 64, 65; 65, assignment; 65, 66; 65, 67; 66, identifier:int_types; 67, expression_list; 67, 68; 68, identifier:int; 69, else_clause; 69, 70; 70, block; 70, 71; 70, 76; 71, expression_statement; 71, 72; 72, assignment; 72, 73; 72, 74; 73, identifier:string_types; 74, expression_list; 74, 75; 75, identifier:basestring; 76, expression_statement; 76, 77; 77, assignment; 77, 78; 77, 79; 78, identifier:int_types; 79, expression_list; 79, 80; 79, 81; 80, identifier:int; 81, identifier:long; 82, if_statement; 82, 83; 82, 88; 82, 104; 82, 126; 83, call; 83, 84; 83, 85; 84, identifier:isinstance; 85, argument_list; 85, 86; 85, 87; 86, identifier:hash; 87, identifier:string_types; 88, block; 88, 89; 89, expression_statement; 89, 90; 90, call; 90, 91; 90, 94; 91, attribute; 91, 92; 91, 93; 92, identifier:clibrebound; 93, identifier:reb_remove_by_hash; 94, argument_list; 94, 95; 94, 99; 94, 103; 95, call; 95, 96; 95, 97; 96, identifier:byref; 97, argument_list; 97, 98; 98, identifier:self; 99, call; 99, 100; 99, 101; 100, identifier:rebhash; 101, argument_list; 101, 102; 102, identifier:hash; 103, identifier:keepSorted; 104, elif_clause; 104, 105; 104, 110; 105, call; 105, 106; 105, 107; 106, identifier:isinstance; 107, argument_list; 107, 108; 107, 109; 108, identifier:hash; 109, identifier:int_types; 110, block; 110, 111; 111, expression_statement; 111, 112; 112, call; 112, 113; 112, 116; 113, attribute; 113, 114; 113, 115; 114, identifier:clibrebound; 115, identifier:reb_remove_by_hash; 116, argument_list; 116, 117; 116, 121; 116, 125; 117, call; 117, 118; 117, 119; 118, identifier:byref; 119, argument_list; 119, 120; 120, identifier:self; 121, call; 121, 122; 121, 123; 122, identifier:c_uint32; 123, argument_list; 123, 124; 124, identifier:hash; 125, identifier:keepSorted; 126, elif_clause; 126, 127; 126, 132; 127, call; 127, 128; 127, 129; 128, identifier:isinstance; 129, argument_list; 129, 130; 129, 131; 130, identifier:hash; 131, identifier:hash_types; 132, block; 132, 133; 133, expression_statement; 133, 134; 134, call; 134, 135; 134, 138; 135, attribute; 135, 136; 135, 137; 136, identifier:clibrebound; 137, identifier:reb_remove_by_hash; 138, argument_list; 138, 139; 138, 143; 138, 144; 139, call; 139, 140; 139, 141; 140, identifier:byref; 141, argument_list; 141, 142; 142, identifier:self; 143, identifier:hash; 144, identifier:keepSorted; 145, if_statement; 145, 146; 145, 151; 146, call; 146, 147; 146, 148; 147, identifier:hasattr; 148, argument_list; 148, 149; 148, 150; 149, identifier:self; 150, string:'_widgets'; 151, block; 151, 152; 152, expression_statement; 152, 153; 153, call; 153, 154; 153, 157; 154, attribute; 154, 155; 154, 156; 155, identifier:self; 156, identifier:_display_heartbeat; 157, argument_list; 157, 158; 158, call; 158, 159; 158, 160; 159, identifier:pointer; 160, argument_list; 160, 161; 161, identifier:self; 162, expression_statement; 162, 163; 163, call; 163, 164; 163, 167; 164, attribute; 164, 165; 164, 166; 165, identifier:self; 166, identifier:process_messages; 167, argument_list
def remove(self, index=None, hash=None, keepSorted=True): """ Removes a particle from the simulation. Parameters ---------- index : int, optional Specify particle to remove by index. hash : c_uint32 or string, optional Specifiy particle to remove by hash (if a string is passed, the corresponding hash is calculated). keepSorted : bool, optional By default, remove preserves the order of particles in the particles array. Might set it to zero in cases with many particles and many removals to speed things up. """ if index is not None: clibrebound.reb_remove(byref(self), index, keepSorted) if hash is not None: hash_types = c_uint32, c_uint, c_ulong PY3 = sys.version_info[0] == 3 if PY3: string_types = str, int_types = int, else: string_types = basestring, int_types = int, long if isinstance(hash, string_types): clibrebound.reb_remove_by_hash(byref(self), rebhash(hash), keepSorted) elif isinstance(hash, int_types): clibrebound.reb_remove_by_hash(byref(self), c_uint32(hash), keepSorted) elif isinstance(hash, hash_types): clibrebound.reb_remove_by_hash(byref(self), hash, keepSorted) if hasattr(self, '_widgets'): self._display_heartbeat(pointer(self)) self.process_messages()
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:particles; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 16; 5, 20; 5, 43; 5, 49; 5, 78; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:sim; 11, attribute; 11, 12; 11, 15; 12, attribute; 12, 13; 12, 14; 13, identifier:self; 14, identifier:_sim; 15, identifier:contents; 16, expression_statement; 16, 17; 17, assignment; 17, 18; 17, 19; 18, identifier:ps; 19, list:[]; 20, if_statement; 20, 21; 20, 26; 20, 31; 21, comparison_operator:>=; 21, 22; 21, 25; 22, attribute; 22, 23; 22, 24; 23, identifier:self; 24, identifier:testparticle; 25, integer:0; 26, block; 26, 27; 27, expression_statement; 27, 28; 28, assignment; 28, 29; 28, 30; 29, identifier:N; 30, integer:1; 31, else_clause; 31, 32; 32, block; 32, 33; 33, expression_statement; 33, 34; 34, assignment; 34, 35; 34, 36; 35, identifier:N; 36, binary_operator:-; 36, 37; 36, 40; 37, attribute; 37, 38; 37, 39; 38, identifier:sim; 39, identifier:N; 40, attribute; 40, 41; 40, 42; 41, identifier:sim; 42, identifier:N_var; 43, expression_statement; 43, 44; 44, assignment; 44, 45; 44, 46; 45, identifier:ParticleList; 46, binary_operator:*; 46, 47; 46, 48; 47, identifier:Particle; 48, identifier:N; 49, expression_statement; 49, 50; 50, assignment; 50, 51; 50, 52; 51, identifier:ps; 52, call; 52, 53; 52, 56; 53, attribute; 53, 54; 53, 55; 54, identifier:ParticleList; 55, identifier:from_address; 56, argument_list; 56, 57; 57, binary_operator:+; 57, 58; 57, 68; 58, call; 58, 59; 58, 62; 59, attribute; 59, 60; 59, 61; 60, identifier:ctypes; 61, identifier:addressof; 62, argument_list; 62, 63; 63, attribute; 63, 64; 63, 67; 64, attribute; 64, 65; 64, 66; 65, identifier:sim; 66, identifier:_particles; 67, identifier:contents; 68, binary_operator:*; 68, 69; 68, 72; 69, attribute; 69, 70; 69, 71; 70, identifier:self; 71, identifier:index; 72, call; 72, 73; 72, 76; 73, attribute; 73, 74; 73, 75; 74, identifier:ctypes; 75, identifier:sizeof; 76, argument_list; 76, 77; 77, identifier:Particle; 78, return_statement; 78, 79; 79, identifier:ps
def particles(self): """ Access the variational particles corresponding to this set of variational equations. The function returns a list of particles which are sorted in the same way as those in sim.particles The particles are pointers and thus can be modified. If there are N real particles, this function will also return a list of N particles (all of which are variational particles). """ sim = self._sim.contents ps = [] if self.testparticle>=0: N = 1 else: N = sim.N-sim.N_var ParticleList = Particle*N ps = ParticleList.from_address(ctypes.addressof(sim._particles.contents)+self.index*ctypes.sizeof(Particle)) return ps
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:filter_queryset; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 4, identifier:self; 5, identifier:request; 6, identifier:queryset; 7, identifier:view; 8, block; 8, 9; 8, 11; 8, 19; 8, 30; 8, 41; 9, expression_statement; 9, 10; 10, comment; 11, expression_statement; 11, 12; 12, assignment; 12, 13; 12, 16; 13, attribute; 13, 14; 13, 15; 14, identifier:self; 15, identifier:ordering_param; 16, attribute; 16, 17; 16, 18; 17, identifier:view; 18, identifier:SORT; 19, expression_statement; 19, 20; 20, assignment; 20, 21; 20, 22; 21, identifier:ordering; 22, call; 22, 23; 22, 26; 23, attribute; 23, 24; 23, 25; 24, identifier:self; 25, identifier:get_ordering; 26, argument_list; 26, 27; 26, 28; 26, 29; 27, identifier:request; 28, identifier:queryset; 29, identifier:view; 30, if_statement; 30, 31; 30, 32; 31, identifier:ordering; 32, block; 32, 33; 33, return_statement; 33, 34; 34, call; 34, 35; 34, 38; 35, attribute; 35, 36; 35, 37; 36, identifier:queryset; 37, identifier:order_by; 38, argument_list; 38, 39; 39, list_splat; 39, 40; 40, identifier:ordering; 41, return_statement; 41, 42; 42, identifier:queryset
def filter_queryset(self, request, queryset, view): """"Filter the queryset, applying the ordering. The `ordering_param` can be overwritten here. In DRF, the ordering_param is 'ordering', but we support changing it to allow the viewset to control the parameter. """ self.ordering_param = view.SORT ordering = self.get_ordering(request, queryset, view) if ordering: return queryset.order_by(*ordering) return queryset
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:get_ordering; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 4, identifier:self; 5, identifier:request; 6, identifier:queryset; 7, identifier:view; 8, block; 8, 9; 8, 11; 8, 22; 8, 66; 8, 67; 9, expression_statement; 9, 10; 10, comment; 11, expression_statement; 11, 12; 12, assignment; 12, 13; 12, 14; 13, identifier:params; 14, call; 14, 15; 14, 18; 15, attribute; 15, 16; 15, 17; 16, identifier:view; 17, identifier:get_request_feature; 18, argument_list; 18, 19; 19, attribute; 19, 20; 19, 21; 20, identifier:view; 21, identifier:SORT; 22, if_statement; 22, 23; 22, 24; 23, identifier:params; 24, block; 24, 25; 24, 37; 24, 50; 24, 51; 24, 52; 25, expression_statement; 25, 26; 26, assignment; 26, 27; 26, 28; 27, identifier:fields; 28, list_comprehension; 28, 29; 28, 34; 29, call; 29, 30; 29, 33; 30, attribute; 30, 31; 30, 32; 31, identifier:param; 32, identifier:strip; 33, argument_list; 34, for_in_clause; 34, 35; 34, 36; 35, identifier:param; 36, identifier:params; 37, expression_statement; 37, 38; 38, assignment; 38, 39; 38, 42; 39, pattern_list; 39, 40; 39, 41; 40, identifier:valid_ordering; 41, identifier:invalid_ordering; 42, call; 42, 43; 42, 46; 43, attribute; 43, 44; 43, 45; 44, identifier:self; 45, identifier:remove_invalid_fields; 46, argument_list; 46, 47; 46, 48; 46, 49; 47, identifier:queryset; 48, identifier:fields; 49, identifier:view; 50, comment; 51, comment; 52, if_statement; 52, 53; 52, 54; 52, 62; 53, identifier:invalid_ordering; 54, block; 54, 55; 55, raise_statement; 55, 56; 56, call; 56, 57; 56, 58; 57, identifier:ValidationError; 58, argument_list; 58, 59; 59, binary_operator:%; 59, 60; 59, 61; 60, string:"Invalid filter field: %s"; 61, identifier:invalid_ordering; 62, else_clause; 62, 63; 63, block; 63, 64; 64, return_statement; 64, 65; 65, identifier:valid_ordering; 66, comment; 67, return_statement; 67, 68; 68, call; 68, 69; 68, 72; 69, attribute; 69, 70; 69, 71; 70, identifier:self; 71, identifier:get_default_ordering; 72, argument_list; 72, 73; 73, identifier:view
def get_ordering(self, request, queryset, view): """Return an ordering for a given request. DRF expects a comma separated list, while DREST expects an array. This method overwrites the DRF default so it can parse the array. """ params = view.get_request_feature(view.SORT) if params: fields = [param.strip() for param in params] valid_ordering, invalid_ordering = self.remove_invalid_fields( queryset, fields, view ) # if any of the sort fields are invalid, throw an error. # else return the ordering if invalid_ordering: raise ValidationError( "Invalid filter field: %s" % invalid_ordering ) else: return valid_ordering # No sorting was included return self.get_default_ordering(view)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:remove_invalid_fields; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 4, identifier:self; 5, identifier:queryset; 6, identifier:fields; 7, identifier:view; 8, block; 8, 9; 8, 11; 8, 15; 8, 19; 8, 20; 8, 21; 8, 81; 9, expression_statement; 9, 10; 10, comment; 11, expression_statement; 11, 12; 12, assignment; 12, 13; 12, 14; 13, identifier:valid_orderings; 14, list:[]; 15, expression_statement; 15, 16; 16, assignment; 16, 17; 16, 18; 17, identifier:invalid_orderings; 18, list:[]; 19, comment; 20, comment; 21, for_statement; 21, 22; 21, 23; 21, 24; 22, identifier:term; 23, identifier:fields; 24, block; 24, 25; 24, 34; 24, 35; 24, 50; 24, 60; 25, expression_statement; 25, 26; 26, assignment; 26, 27; 26, 28; 27, identifier:stripped_term; 28, call; 28, 29; 28, 32; 29, attribute; 29, 30; 29, 31; 30, identifier:term; 31, identifier:lstrip; 32, argument_list; 32, 33; 33, string:'-'; 34, comment; 35, expression_statement; 35, 36; 36, assignment; 36, 37; 36, 38; 37, identifier:reverse_sort_term; 38, conditional_expression:if; 38, 39; 38, 40; 38, 49; 39, string:''; 40, comparison_operator:is; 40, 41; 40, 45; 41, call; 41, 42; 41, 43; 42, identifier:len; 43, argument_list; 43, 44; 44, identifier:stripped_term; 45, call; 45, 46; 45, 47; 46, identifier:len; 47, argument_list; 47, 48; 48, identifier:term; 49, string:'-'; 50, expression_statement; 50, 51; 51, assignment; 51, 52; 51, 53; 52, identifier:ordering; 53, call; 53, 54; 53, 57; 54, attribute; 54, 55; 54, 56; 55, identifier:self; 56, identifier:ordering_for; 57, argument_list; 57, 58; 57, 59; 58, identifier:stripped_term; 59, identifier:view; 60, if_statement; 60, 61; 60, 62; 60, 72; 61, identifier:ordering; 62, block; 62, 63; 63, expression_statement; 63, 64; 64, call; 64, 65; 64, 68; 65, attribute; 65, 66; 65, 67; 66, identifier:valid_orderings; 67, identifier:append; 68, argument_list; 68, 69; 69, binary_operator:+; 69, 70; 69, 71; 70, identifier:reverse_sort_term; 71, identifier:ordering; 72, else_clause; 72, 73; 73, block; 73, 74; 74, expression_statement; 74, 75; 75, call; 75, 76; 75, 79; 76, attribute; 76, 77; 76, 78; 77, identifier:invalid_orderings; 78, identifier:append; 79, argument_list; 79, 80; 80, identifier:term; 81, return_statement; 81, 82; 82, expression_list; 82, 83; 82, 84; 83, identifier:valid_orderings; 84, identifier:invalid_orderings
def remove_invalid_fields(self, queryset, fields, view): """Remove invalid fields from an ordering. Overwrites the DRF default remove_invalid_fields method to return both the valid orderings and any invalid orderings. """ valid_orderings = [] invalid_orderings = [] # for each field sent down from the query param, # determine if its valid or invalid for term in fields: stripped_term = term.lstrip('-') # add back the '-' add the end if necessary reverse_sort_term = '' if len(stripped_term) is len(term) else '-' ordering = self.ordering_for(stripped_term, view) if ordering: valid_orderings.append(reverse_sort_term + ordering) else: invalid_orderings.append(term) return valid_orderings, invalid_orderings
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 13; 1, 17; 2, function_name:_format_dataframe; 3, parameters; 3, 4; 3, 10; 4, typed_parameter; 4, 5; 4, 6; 5, identifier:df; 6, type; 6, 7; 7, attribute; 7, 8; 7, 9; 8, identifier:pd; 9, identifier:DataFrame; 10, default_parameter; 10, 11; 10, 12; 11, identifier:nautical_units; 12, True; 13, type; 13, 14; 14, attribute; 14, 15; 14, 16; 15, identifier:pd; 16, identifier:DataFrame; 17, block; 17, 18; 17, 20; 17, 49; 17, 117; 17, 139; 17, 178; 18, expression_statement; 18, 19; 19, comment; 20, if_statement; 20, 21; 20, 34; 21, boolean_operator:and; 21, 22; 21, 27; 22, comparison_operator:in; 22, 23; 22, 24; 23, string:"callsign"; 24, attribute; 24, 25; 24, 26; 25, identifier:df; 26, identifier:columns; 27, comparison_operator:==; 27, 28; 27, 33; 28, attribute; 28, 29; 28, 32; 29, attribute; 29, 30; 29, 31; 30, identifier:df; 31, identifier:callsign; 32, identifier:dtype; 33, identifier:object; 34, block; 34, 35; 35, expression_statement; 35, 36; 36, assignment; 36, 37; 36, 40; 37, attribute; 37, 38; 37, 39; 38, identifier:df; 39, identifier:callsign; 40, call; 40, 41; 40, 48; 41, attribute; 41, 42; 41, 47; 42, attribute; 42, 43; 42, 46; 43, attribute; 43, 44; 43, 45; 44, identifier:df; 45, identifier:callsign; 46, identifier:str; 47, identifier:strip; 48, argument_list; 49, if_statement; 49, 50; 49, 51; 50, identifier:nautical_units; 51, block; 51, 52; 51, 62; 51, 79; 51, 98; 52, expression_statement; 52, 53; 53, assignment; 53, 54; 53, 57; 54, attribute; 54, 55; 54, 56; 55, identifier:df; 56, identifier:altitude; 57, binary_operator:/; 57, 58; 57, 61; 58, attribute; 58, 59; 58, 60; 59, identifier:df; 60, identifier:altitude; 61, float:0.3048; 62, if_statement; 62, 63; 62, 68; 63, comparison_operator:in; 63, 64; 63, 65; 64, string:"geoaltitude"; 65, attribute; 65, 66; 65, 67; 66, identifier:df; 67, identifier:columns; 68, block; 68, 69; 69, expression_statement; 69, 70; 70, assignment; 70, 71; 70, 74; 71, attribute; 71, 72; 71, 73; 72, identifier:df; 73, identifier:geoaltitude; 74, binary_operator:/; 74, 75; 74, 78; 75, attribute; 75, 76; 75, 77; 76, identifier:df; 77, identifier:geoaltitude; 78, float:0.3048; 79, if_statement; 79, 80; 79, 85; 80, comparison_operator:in; 80, 81; 80, 82; 81, string:"groundspeed"; 82, attribute; 82, 83; 82, 84; 83, identifier:df; 84, identifier:columns; 85, block; 85, 86; 86, expression_statement; 86, 87; 87, assignment; 87, 88; 87, 91; 88, attribute; 88, 89; 88, 90; 89, identifier:df; 90, identifier:groundspeed; 91, binary_operator:*; 91, 92; 91, 97; 92, binary_operator:/; 92, 93; 92, 96; 93, attribute; 93, 94; 93, 95; 94, identifier:df; 95, identifier:groundspeed; 96, integer:1852; 97, integer:3600; 98, if_statement; 98, 99; 98, 104; 99, comparison_operator:in; 99, 100; 99, 101; 100, string:"vertical_rate"; 101, attribute; 101, 102; 101, 103; 102, identifier:df; 103, identifier:columns; 104, block; 104, 105; 105, expression_statement; 105, 106; 106, assignment; 106, 107; 106, 110; 107, attribute; 107, 108; 107, 109; 108, identifier:df; 109, identifier:vertical_rate; 110, binary_operator:*; 110, 111; 110, 116; 111, binary_operator:/; 111, 112; 111, 115; 112, attribute; 112, 113; 112, 114; 113, identifier:df; 114, identifier:vertical_rate; 115, float:0.3048; 116, integer:60; 117, expression_statement; 117, 118; 118, assignment; 118, 119; 118, 122; 119, attribute; 119, 120; 119, 121; 120, identifier:df; 121, identifier:timestamp; 122, call; 122, 123; 122, 137; 123, attribute; 123, 124; 123, 136; 124, attribute; 124, 125; 124, 135; 125, call; 125, 126; 125, 129; 126, attribute; 126, 127; 126, 128; 127, identifier:pd; 128, identifier:to_datetime; 129, argument_list; 129, 130; 130, binary_operator:*; 130, 131; 130, 134; 131, attribute; 131, 132; 131, 133; 132, identifier:df; 133, identifier:timestamp; 134, float:1e9; 135, identifier:dt; 136, identifier:tz_localize; 137, argument_list; 137, 138; 138, string:"utc"; 139, if_statement; 139, 140; 139, 145; 140, comparison_operator:in; 140, 141; 140, 142; 141, string:"last_position"; 142, attribute; 142, 143; 142, 144; 143, identifier:df; 144, identifier:columns; 145, block; 145, 146; 146, expression_statement; 146, 147; 147, assignment; 147, 148; 147, 149; 148, identifier:df; 149, call; 149, 150; 149, 158; 150, attribute; 150, 151; 150, 157; 151, call; 151, 152; 151, 155; 152, attribute; 152, 153; 152, 154; 153, identifier:df; 154, identifier:query; 155, argument_list; 155, 156; 156, string:"last_position == last_position"; 157, identifier:assign; 158, argument_list; 158, 159; 159, keyword_argument; 159, 160; 159, 161; 160, identifier:last_position; 161, call; 161, 162; 161, 176; 162, attribute; 162, 163; 162, 175; 163, attribute; 163, 164; 163, 174; 164, call; 164, 165; 164, 168; 165, attribute; 165, 166; 165, 167; 166, identifier:pd; 167, identifier:to_datetime; 168, argument_list; 168, 169; 169, binary_operator:*; 169, 170; 169, 173; 170, attribute; 170, 171; 170, 172; 171, identifier:df; 172, identifier:last_position; 173, float:1e9; 174, identifier:dt; 175, identifier:tz_localize; 176, argument_list; 176, 177; 177, string:"utc"; 178, return_statement; 178, 179; 179, call; 179, 180; 179, 183; 180, attribute; 180, 181; 180, 182; 181, identifier:df; 182, identifier:sort_values; 183, argument_list; 183, 184; 184, string:"timestamp"
def _format_dataframe( df: pd.DataFrame, nautical_units=True ) -> pd.DataFrame: """ This function converts types, strips spaces after callsigns and sorts the DataFrame by timestamp. For some reason, all data arriving from OpenSky are converted to units in metric system. Optionally, you may convert the units back to nautical miles, feet and feet/min. """ if "callsign" in df.columns and df.callsign.dtype == object: df.callsign = df.callsign.str.strip() if nautical_units: df.altitude = df.altitude / 0.3048 if "geoaltitude" in df.columns: df.geoaltitude = df.geoaltitude / 0.3048 if "groundspeed" in df.columns: df.groundspeed = df.groundspeed / 1852 * 3600 if "vertical_rate" in df.columns: df.vertical_rate = df.vertical_rate / 0.3048 * 60 df.timestamp = pd.to_datetime(df.timestamp * 1e9).dt.tz_localize("utc") if "last_position" in df.columns: df = df.query("last_position == last_position").assign( last_position=pd.to_datetime( df.last_position * 1e9 ).dt.tz_localize("utc") ) return df.sort_values("timestamp")
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:find_package_indexes_in_dir; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:simple_dir; 6, block; 6, 7; 6, 9; 6, 29; 6, 30; 6, 55; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:packages; 12, call; 12, 13; 12, 14; 13, identifier:sorted; 14, argument_list; 14, 15; 15, set_comprehension; 15, 16; 15, 17; 15, 21; 16, comment; 17, call; 17, 18; 17, 19; 18, identifier:canonicalize_name; 19, argument_list; 19, 20; 20, identifier:x; 21, for_in_clause; 21, 22; 21, 23; 22, identifier:x; 23, call; 23, 24; 23, 27; 24, attribute; 24, 25; 24, 26; 25, identifier:os; 26, identifier:listdir; 27, argument_list; 27, 28; 28, identifier:simple_dir; 29, comment; 30, expression_statement; 30, 31; 31, assignment; 31, 32; 31, 33; 32, identifier:packages; 33, list_comprehension; 33, 34; 33, 35; 33, 38; 34, identifier:x; 35, for_in_clause; 35, 36; 35, 37; 36, identifier:x; 37, identifier:packages; 38, if_clause; 38, 39; 39, call; 39, 40; 39, 45; 40, attribute; 40, 41; 40, 44; 41, attribute; 41, 42; 41, 43; 42, identifier:os; 43, identifier:path; 44, identifier:isdir; 45, argument_list; 45, 46; 46, call; 46, 47; 46, 52; 47, attribute; 47, 48; 47, 51; 48, attribute; 48, 49; 48, 50; 49, identifier:os; 50, identifier:path; 51, identifier:join; 52, argument_list; 52, 53; 52, 54; 53, identifier:simple_dir; 54, identifier:x; 55, return_statement; 55, 56; 56, identifier:packages
def find_package_indexes_in_dir(self, simple_dir): """Given a directory that contains simple packages indexes, return a sorted list of normalized package names. This presumes every directory within is a simple package index directory.""" packages = sorted( { # Filter out all of the "non" normalized names here canonicalize_name(x) for x in os.listdir(simple_dir) } ) # Package indexes must be in directories, so ignore anything else. packages = [x for x in packages if os.path.isdir(os.path.join(simple_dir, x))] return packages
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 17; 2, function_name:factorize; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 11; 3, 14; 4, identifier:train; 5, identifier:test; 6, identifier:features; 7, default_parameter; 7, 8; 7, 9; 8, identifier:na_value; 9, unary_operator:-; 9, 10; 10, integer:9999; 11, default_parameter; 11, 12; 11, 13; 12, identifier:full; 13, False; 14, default_parameter; 14, 15; 14, 16; 15, identifier:sort; 16, True; 17, block; 17, 18; 17, 20; 17, 136; 18, expression_statement; 18, 19; 19, comment; 20, for_statement; 20, 21; 20, 22; 20, 23; 21, identifier:column; 22, identifier:features; 23, block; 23, 24; 23, 74; 23, 87; 23, 100; 24, if_statement; 24, 25; 24, 26; 24, 56; 25, identifier:full; 26, block; 26, 27; 26, 42; 27, expression_statement; 27, 28; 28, assignment; 28, 29; 28, 30; 29, identifier:vs; 30, call; 30, 31; 30, 34; 31, attribute; 31, 32; 31, 33; 32, identifier:pd; 33, identifier:concat; 34, argument_list; 34, 35; 35, list:[train[column], test[column]]; 35, 36; 35, 39; 36, subscript; 36, 37; 36, 38; 37, identifier:train; 38, identifier:column; 39, subscript; 39, 40; 39, 41; 40, identifier:test; 41, identifier:column; 42, expression_statement; 42, 43; 43, assignment; 43, 44; 43, 47; 44, pattern_list; 44, 45; 44, 46; 45, identifier:labels; 46, identifier:indexer; 47, call; 47, 48; 47, 51; 48, attribute; 48, 49; 48, 50; 49, identifier:pd; 50, identifier:factorize; 51, argument_list; 51, 52; 51, 53; 52, identifier:vs; 53, keyword_argument; 53, 54; 53, 55; 54, identifier:sort; 55, identifier:sort; 56, else_clause; 56, 57; 57, block; 57, 58; 58, expression_statement; 58, 59; 59, assignment; 59, 60; 59, 63; 60, pattern_list; 60, 61; 60, 62; 61, identifier:labels; 62, identifier:indexer; 63, call; 63, 64; 63, 67; 64, attribute; 64, 65; 64, 66; 65, identifier:pd; 66, identifier:factorize; 67, argument_list; 67, 68; 67, 71; 68, subscript; 68, 69; 68, 70; 69, identifier:train; 70, identifier:column; 71, keyword_argument; 71, 72; 71, 73; 72, identifier:sort; 73, identifier:sort; 74, expression_statement; 74, 75; 75, assignment; 75, 76; 75, 79; 76, subscript; 76, 77; 76, 78; 77, identifier:train; 78, identifier:column; 79, call; 79, 80; 79, 83; 80, attribute; 80, 81; 80, 82; 81, identifier:indexer; 82, identifier:get_indexer; 83, argument_list; 83, 84; 84, subscript; 84, 85; 84, 86; 85, identifier:train; 86, identifier:column; 87, expression_statement; 87, 88; 88, assignment; 88, 89; 88, 92; 89, subscript; 89, 90; 89, 91; 90, identifier:test; 91, identifier:column; 92, call; 92, 93; 92, 96; 93, attribute; 93, 94; 93, 95; 94, identifier:indexer; 95, identifier:get_indexer; 96, argument_list; 96, 97; 97, subscript; 97, 98; 97, 99; 98, identifier:test; 99, identifier:column; 100, if_statement; 100, 101; 100, 105; 101, comparison_operator:!=; 101, 102; 101, 103; 102, identifier:na_value; 103, unary_operator:-; 103, 104; 104, integer:1; 105, block; 105, 106; 105, 121; 106, expression_statement; 106, 107; 107, assignment; 107, 108; 107, 111; 108, subscript; 108, 109; 108, 110; 109, identifier:train; 110, identifier:column; 111, call; 111, 112; 111, 117; 112, attribute; 112, 113; 112, 116; 113, subscript; 113, 114; 113, 115; 114, identifier:train; 115, identifier:column; 116, identifier:replace; 117, argument_list; 117, 118; 117, 120; 118, unary_operator:-; 118, 119; 119, integer:1; 120, identifier:na_value; 121, expression_statement; 121, 122; 122, assignment; 122, 123; 122, 126; 123, subscript; 123, 124; 123, 125; 124, identifier:test; 125, identifier:column; 126, call; 126, 127; 126, 132; 127, attribute; 127, 128; 127, 131; 128, subscript; 128, 129; 128, 130; 129, identifier:test; 130, identifier:column; 131, identifier:replace; 132, argument_list; 132, 133; 132, 135; 133, unary_operator:-; 133, 134; 134, integer:1; 135, identifier:na_value; 136, return_statement; 136, 137; 137, expression_list; 137, 138; 137, 139; 138, identifier:train; 139, identifier:test
def factorize(train, test, features, na_value=-9999, full=False, sort=True): """Factorize categorical features. Parameters ---------- train : pd.DataFrame test : pd.DataFrame features : list Column names in the DataFrame to be encoded. na_value : int, default -9999 full : bool, default False Whether use all columns from train/test or only from train. sort : bool, default True Sort by values. Returns ------- train : pd.DataFrame test : pd.DataFrame """ for column in features: if full: vs = pd.concat([train[column], test[column]]) labels, indexer = pd.factorize(vs, sort=sort) else: labels, indexer = pd.factorize(train[column], sort=sort) train[column] = indexer.get_indexer(train[column]) test[column] = indexer.get_indexer(test[column]) if na_value != -1: train[column] = train[column].replace(-1, na_value) test[column] = test[column].replace(-1, na_value) return train, test
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:preferred_ordinal; 3, parameters; 3, 4; 3, 5; 4, identifier:cls; 5, identifier:attr_name; 6, block; 6, 7; 6, 9; 6, 18; 6, 47; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:attr_name; 12, call; 12, 13; 12, 16; 13, attribute; 13, 14; 13, 15; 14, identifier:cls; 15, identifier:map; 16, argument_list; 16, 17; 17, identifier:attr_name; 18, if_statement; 18, 19; 18, 24; 18, 36; 19, comparison_operator:in; 19, 20; 19, 21; 20, identifier:attr_name; 21, attribute; 21, 22; 21, 23; 22, identifier:cls; 23, identifier:preferred_order; 24, block; 24, 25; 25, expression_statement; 25, 26; 26, assignment; 26, 27; 26, 28; 27, identifier:ordinal; 28, call; 28, 29; 28, 34; 29, attribute; 29, 30; 29, 33; 30, attribute; 30, 31; 30, 32; 31, identifier:cls; 32, identifier:preferred_order; 33, identifier:index; 34, argument_list; 34, 35; 35, identifier:attr_name; 36, else_clause; 36, 37; 37, block; 37, 38; 38, expression_statement; 38, 39; 39, assignment; 39, 40; 39, 41; 40, identifier:ordinal; 41, call; 41, 42; 41, 43; 42, identifier:len; 43, argument_list; 43, 44; 44, attribute; 44, 45; 44, 46; 45, identifier:cls; 46, identifier:preferred_order; 47, return_statement; 47, 48; 48, tuple; 48, 49; 48, 50; 49, identifier:ordinal; 50, identifier:attr_name
def preferred_ordinal(cls, attr_name): """ Returns an ordering value for a particular attribute key. Unrecognized attributes and OIDs will be sorted lexically at the end. :return: An orderable value. """ attr_name = cls.map(attr_name) if attr_name in cls.preferred_order: ordinal = cls.preferred_order.index(attr_name) else: ordinal = len(cls.preferred_order) return (ordinal, attr_name)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:patience_sort; 3, parameters; 3, 4; 4, identifier:xs; 5, block; 5, 6; 5, 8; 5, 14; 6, expression_statement; 6, 7; 7, string:'''Patience sort an iterable, xs. This function generates a series of pairs (x, pile), where "pile" is the 0-based index of the pile "x" should be placed on top of. Elements of "xs" must be less-than comparable. '''; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:pile_tops; 11, call; 11, 12; 11, 13; 12, identifier:list; 13, argument_list; 14, for_statement; 14, 15; 14, 16; 14, 17; 15, identifier:x; 16, identifier:xs; 17, block; 17, 18; 17, 28; 17, 51; 18, expression_statement; 18, 19; 19, assignment; 19, 20; 19, 21; 20, identifier:pile; 21, call; 21, 22; 21, 25; 22, attribute; 22, 23; 22, 24; 23, identifier:bisect; 24, identifier:bisect_left; 25, argument_list; 25, 26; 25, 27; 26, identifier:pile_tops; 27, identifier:x; 28, if_statement; 28, 29; 28, 35; 28, 43; 29, comparison_operator:==; 29, 30; 29, 31; 30, identifier:pile; 31, call; 31, 32; 31, 33; 32, identifier:len; 33, argument_list; 33, 34; 34, identifier:pile_tops; 35, block; 35, 36; 36, expression_statement; 36, 37; 37, call; 37, 38; 37, 41; 38, attribute; 38, 39; 38, 40; 39, identifier:pile_tops; 40, identifier:append; 41, argument_list; 41, 42; 42, identifier:x; 43, else_clause; 43, 44; 44, block; 44, 45; 45, expression_statement; 45, 46; 46, assignment; 46, 47; 46, 50; 47, subscript; 47, 48; 47, 49; 48, identifier:pile_tops; 49, identifier:pile; 50, identifier:x; 51, expression_statement; 51, 52; 52, yield; 52, 53; 53, expression_list; 53, 54; 53, 55; 54, identifier:x; 55, identifier:pile
def patience_sort(xs): '''Patience sort an iterable, xs. This function generates a series of pairs (x, pile), where "pile" is the 0-based index of the pile "x" should be placed on top of. Elements of "xs" must be less-than comparable. ''' pile_tops = list() for x in xs: pile = bisect.bisect_left(pile_tops, x) if pile == len(pile_tops): pile_tops.append(x) else: pile_tops[pile] = x yield x, pile
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:sort; 3, parameters; 3, 4; 4, identifier:args; 5, block; 5, 6; 5, 8; 5, 17; 5, 33; 5, 44; 5, 63; 5, 68; 5, 82; 5, 92; 5, 100; 5, 172; 5, 192; 5, 204; 5, 210; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:p; 11, call; 11, 12; 11, 13; 12, identifier:OptionParser; 13, argument_list; 13, 14; 14, attribute; 14, 15; 14, 16; 15, identifier:sort; 16, identifier:__doc__; 17, expression_statement; 17, 18; 18, call; 18, 19; 18, 22; 19, attribute; 19, 20; 19, 21; 20, identifier:p; 21, identifier:add_option; 22, argument_list; 22, 23; 22, 24; 22, 27; 22, 30; 23, string:"--sizes"; 24, keyword_argument; 24, 25; 24, 26; 25, identifier:default; 26, False; 27, keyword_argument; 27, 28; 27, 29; 28, identifier:action; 29, string:"store_true"; 30, keyword_argument; 30, 31; 30, 32; 31, identifier:help; 32, string:"Sort by decreasing size [default: %default]"; 33, expression_statement; 33, 34; 34, assignment; 34, 35; 34, 38; 35, pattern_list; 35, 36; 35, 37; 36, identifier:opts; 37, identifier:args; 38, call; 38, 39; 38, 42; 39, attribute; 39, 40; 39, 41; 40, identifier:p; 41, identifier:parse_args; 42, argument_list; 42, 43; 43, identifier:args; 44, if_statement; 44, 45; 44, 51; 45, comparison_operator:!=; 45, 46; 45, 50; 46, call; 46, 47; 46, 48; 47, identifier:len; 48, argument_list; 48, 49; 49, identifier:args; 50, integer:1; 51, block; 51, 52; 52, expression_statement; 52, 53; 53, call; 53, 54; 53, 57; 54, attribute; 54, 55; 54, 56; 55, identifier:sys; 56, identifier:exit; 57, argument_list; 57, 58; 58, call; 58, 59; 58, 62; 59, attribute; 59, 60; 59, 61; 60, identifier:p; 61, identifier:print_help; 62, argument_list; 63, expression_statement; 63, 64; 64, assignment; 64, 65; 64, 67; 65, pattern_list; 65, 66; 66, identifier:fastafile; 67, identifier:args; 68, expression_statement; 68, 69; 69, assignment; 69, 70; 69, 71; 70, identifier:sortedfastafile; 71, binary_operator:+; 71, 72; 71, 81; 72, subscript; 72, 73; 72, 80; 73, call; 73, 74; 73, 77; 74, attribute; 74, 75; 74, 76; 75, identifier:fastafile; 76, identifier:rsplit; 77, argument_list; 77, 78; 77, 79; 78, string:"."; 79, integer:1; 80, integer:0; 81, string:".sorted.fasta"; 82, expression_statement; 82, 83; 83, assignment; 83, 84; 83, 85; 84, identifier:f; 85, call; 85, 86; 85, 87; 86, identifier:Fasta; 87, argument_list; 87, 88; 87, 89; 88, identifier:fastafile; 89, keyword_argument; 89, 90; 89, 91; 90, identifier:index; 91, False; 92, expression_statement; 92, 93; 93, assignment; 93, 94; 93, 95; 94, identifier:fw; 95, call; 95, 96; 95, 97; 96, identifier:must_open; 97, argument_list; 97, 98; 97, 99; 98, identifier:sortedfastafile; 99, string:"w"; 100, if_statement; 100, 101; 100, 104; 100, 105; 100, 159; 101, attribute; 101, 102; 101, 103; 102, identifier:opts; 103, identifier:sizes; 104, comment; 105, block; 105, 106; 105, 130; 105, 149; 106, expression_statement; 106, 107; 107, assignment; 107, 108; 107, 109; 108, identifier:sortlist; 109, call; 109, 110; 109, 111; 110, identifier:sorted; 111, argument_list; 111, 112; 111, 117; 112, call; 112, 113; 112, 116; 113, attribute; 113, 114; 113, 115; 114, identifier:f; 115, identifier:itersizes; 116, argument_list; 117, keyword_argument; 117, 118; 117, 119; 118, identifier:key; 119, lambda; 119, 120; 119, 122; 120, lambda_parameters; 120, 121; 121, identifier:x; 122, tuple; 122, 123; 122, 127; 123, unary_operator:-; 123, 124; 124, subscript; 124, 125; 124, 126; 125, identifier:x; 126, integer:1; 127, subscript; 127, 128; 127, 129; 128, identifier:x; 129, integer:0; 130, expression_statement; 130, 131; 131, call; 131, 132; 131, 135; 132, attribute; 132, 133; 132, 134; 133, identifier:logging; 134, identifier:debug; 135, argument_list; 135, 136; 136, call; 136, 137; 136, 141; 137, attribute; 137, 138; 137, 139; 137, 140; 138, string:"Sort by size: max: {0}, min: {1}"; 139, line_continuation:\; 140, identifier:format; 141, argument_list; 141, 142; 141, 145; 142, subscript; 142, 143; 142, 144; 143, identifier:sortlist; 144, integer:0; 145, subscript; 145, 146; 145, 147; 146, identifier:sortlist; 147, unary_operator:-; 147, 148; 148, integer:1; 149, expression_statement; 149, 150; 150, assignment; 150, 151; 150, 152; 151, identifier:sortlist; 152, list_comprehension; 152, 153; 152, 154; 153, identifier:x; 154, for_in_clause; 154, 155; 154, 158; 155, pattern_list; 155, 156; 155, 157; 156, identifier:x; 157, identifier:s; 158, identifier:sortlist; 159, else_clause; 159, 160; 160, block; 160, 161; 161, expression_statement; 161, 162; 162, assignment; 162, 163; 162, 164; 163, identifier:sortlist; 164, call; 164, 165; 164, 166; 165, identifier:sorted; 166, argument_list; 166, 167; 167, call; 167, 168; 167, 171; 168, attribute; 168, 169; 168, 170; 169, identifier:f; 170, identifier:iterkeys; 171, argument_list; 172, for_statement; 172, 173; 172, 174; 172, 175; 173, identifier:key; 174, identifier:sortlist; 175, block; 175, 176; 175, 182; 176, expression_statement; 176, 177; 177, assignment; 177, 178; 177, 179; 178, identifier:rec; 179, subscript; 179, 180; 179, 181; 180, identifier:f; 181, identifier:key; 182, expression_statement; 182, 183; 183, call; 183, 184; 183, 187; 184, attribute; 184, 185; 184, 186; 185, identifier:SeqIO; 186, identifier:write; 187, argument_list; 187, 188; 187, 190; 187, 191; 188, list:[rec]; 188, 189; 189, identifier:rec; 190, identifier:fw; 191, string:"fasta"; 192, expression_statement; 192, 193; 193, call; 193, 194; 193, 197; 194, attribute; 194, 195; 194, 196; 195, identifier:logging; 196, identifier:debug; 197, argument_list; 197, 198; 198, call; 198, 199; 198, 202; 199, attribute; 199, 200; 199, 201; 200, string:"Sorted file written to `{0}`."; 201, identifier:format; 202, argument_list; 202, 203; 203, identifier:sortedfastafile; 204, expression_statement; 204, 205; 205, call; 205, 206; 205, 209; 206, attribute; 206, 207; 206, 208; 207, identifier:fw; 208, identifier:close; 209, argument_list; 210, return_statement; 210, 211; 211, identifier:sortedfastafile
def sort(args): """ %prog sort fastafile Sort a list of sequences and output with sorted IDs, etc. """ p = OptionParser(sort.__doc__) p.add_option("--sizes", default=False, action="store_true", help="Sort by decreasing size [default: %default]") opts, args = p.parse_args(args) if len(args) != 1: sys.exit(p.print_help()) fastafile, = args sortedfastafile = fastafile.rsplit(".", 1)[0] + ".sorted.fasta" f = Fasta(fastafile, index=False) fw = must_open(sortedfastafile, "w") if opts.sizes: # Sort by decreasing size sortlist = sorted(f.itersizes(), key=lambda x: (-x[1], x[0])) logging.debug("Sort by size: max: {0}, min: {1}".\ format(sortlist[0], sortlist[-1])) sortlist = [x for x, s in sortlist] else: sortlist = sorted(f.iterkeys()) for key in sortlist: rec = f[key] SeqIO.write([rec], fw, "fasta") logging.debug("Sorted file written to `{0}`.".format(sortedfastafile)) fw.close() return sortedfastafile
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:posmap; 3, parameters; 3, 4; 4, identifier:args; 5, block; 5, 6; 5, 8; 5, 17; 5, 28; 5, 47; 5, 54; 5, 55; 5, 65; 5, 71; 5, 88; 5, 89; 5, 95; 5, 104; 5, 110; 5, 123; 5, 124; 5, 130; 5, 151; 5, 152; 5, 158; 5, 178; 5, 179; 5, 185; 5, 204; 5, 205; 5, 211; 5, 217; 5, 246; 5, 247; 5, 253; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:p; 11, call; 11, 12; 11, 13; 12, identifier:OptionParser; 13, argument_list; 13, 14; 14, attribute; 14, 15; 14, 16; 15, identifier:posmap; 16, identifier:__doc__; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 22; 19, pattern_list; 19, 20; 19, 21; 20, identifier:opts; 21, identifier:args; 22, call; 22, 23; 22, 26; 23, attribute; 23, 24; 23, 25; 24, identifier:p; 25, identifier:parse_args; 26, argument_list; 26, 27; 27, identifier:args; 28, if_statement; 28, 29; 28, 35; 29, comparison_operator:!=; 29, 30; 29, 34; 30, call; 30, 31; 30, 32; 31, identifier:len; 32, argument_list; 32, 33; 33, identifier:args; 34, integer:3; 35, block; 35, 36; 36, expression_statement; 36, 37; 37, call; 37, 38; 37, 41; 38, attribute; 38, 39; 38, 40; 39, identifier:sys; 40, identifier:exit; 41, argument_list; 41, 42; 42, call; 42, 43; 42, 46; 43, attribute; 43, 44; 43, 45; 44, identifier:p; 45, identifier:print_help; 46, argument_list; 47, expression_statement; 47, 48; 48, assignment; 48, 49; 48, 53; 49, pattern_list; 49, 50; 49, 51; 49, 52; 50, identifier:frgscffile; 51, identifier:fastafile; 52, identifier:scf; 53, identifier:args; 54, comment; 55, expression_statement; 55, 56; 56, assignment; 56, 57; 56, 58; 57, identifier:cmd; 58, call; 58, 59; 58, 62; 59, attribute; 59, 60; 59, 61; 60, string:"faOneRecord {0} {1}"; 61, identifier:format; 62, argument_list; 62, 63; 62, 64; 63, identifier:fastafile; 64, identifier:scf; 65, expression_statement; 65, 66; 66, assignment; 66, 67; 66, 68; 67, identifier:scffastafile; 68, binary_operator:+; 68, 69; 68, 70; 69, identifier:scf; 70, string:".fasta"; 71, if_statement; 71, 72; 71, 79; 72, not_operator; 72, 73; 73, call; 73, 74; 73, 77; 74, attribute; 74, 75; 74, 76; 75, identifier:op; 76, identifier:exists; 77, argument_list; 77, 78; 78, identifier:scffastafile; 79, block; 79, 80; 80, expression_statement; 80, 81; 81, call; 81, 82; 81, 83; 82, identifier:sh; 83, argument_list; 83, 84; 83, 85; 84, identifier:cmd; 85, keyword_argument; 85, 86; 85, 87; 86, identifier:outfile; 87, identifier:scffastafile; 88, comment; 89, expression_statement; 89, 90; 90, assignment; 90, 91; 90, 92; 91, identifier:sizesfile; 92, binary_operator:+; 92, 93; 92, 94; 93, identifier:scffastafile; 94, string:".sizes"; 95, expression_statement; 95, 96; 96, assignment; 96, 97; 96, 98; 97, identifier:sizes; 98, attribute; 98, 99; 98, 103; 99, call; 99, 100; 99, 101; 100, identifier:Sizes; 101, argument_list; 101, 102; 102, identifier:scffastafile; 103, identifier:mapping; 104, expression_statement; 104, 105; 105, assignment; 105, 106; 105, 107; 106, identifier:scfsize; 107, subscript; 107, 108; 107, 109; 108, identifier:sizes; 109, identifier:scf; 110, expression_statement; 110, 111; 111, call; 111, 112; 111, 115; 112, attribute; 112, 113; 112, 114; 113, identifier:logging; 114, identifier:debug; 115, argument_list; 115, 116; 116, call; 116, 117; 116, 120; 117, attribute; 117, 118; 117, 119; 118, string:"`{0}` has length of {1}."; 119, identifier:format; 120, argument_list; 120, 121; 120, 122; 121, identifier:scf; 122, identifier:scfsize; 123, comment; 124, expression_statement; 124, 125; 125, assignment; 125, 126; 125, 127; 126, identifier:gapsbedfile; 127, binary_operator:+; 127, 128; 127, 129; 128, identifier:scf; 129, string:".gaps.bed"; 130, if_statement; 130, 131; 130, 138; 131, not_operator; 131, 132; 132, call; 132, 133; 132, 136; 133, attribute; 133, 134; 133, 135; 134, identifier:op; 135, identifier:exists; 136, argument_list; 136, 137; 137, identifier:gapsbedfile; 138, block; 138, 139; 138, 146; 139, expression_statement; 139, 140; 140, assignment; 140, 141; 140, 142; 141, identifier:args; 142, list:[scffastafile, "--bed", "--mingap=100"]; 142, 143; 142, 144; 142, 145; 143, identifier:scffastafile; 144, string:"--bed"; 145, string:"--mingap=100"; 146, expression_statement; 146, 147; 147, call; 147, 148; 147, 149; 148, identifier:gaps; 149, argument_list; 149, 150; 150, identifier:args; 151, comment; 152, expression_statement; 152, 153; 153, assignment; 153, 154; 153, 155; 154, identifier:posmapfile; 155, binary_operator:+; 155, 156; 155, 157; 156, identifier:scf; 157, string:".posmap"; 158, if_statement; 158, 159; 158, 166; 159, not_operator; 159, 160; 160, call; 160, 161; 160, 164; 161, attribute; 161, 162; 161, 163; 162, identifier:op; 163, identifier:exists; 164, argument_list; 164, 165; 165, identifier:posmapfile; 166, block; 166, 167; 166, 173; 167, expression_statement; 167, 168; 168, assignment; 168, 169; 168, 170; 169, identifier:args; 170, list:[frgscffile, scf]; 170, 171; 170, 172; 171, identifier:frgscffile; 172, identifier:scf; 173, expression_statement; 173, 174; 174, call; 174, 175; 174, 176; 175, identifier:query; 176, argument_list; 176, 177; 177, identifier:args; 178, comment; 179, expression_statement; 179, 180; 180, assignment; 180, 181; 180, 182; 181, identifier:bedfile; 182, binary_operator:+; 182, 183; 182, 184; 183, identifier:scf; 184, string:".bed"; 185, if_statement; 185, 186; 185, 193; 186, not_operator; 186, 187; 187, call; 187, 188; 187, 191; 188, attribute; 188, 189; 188, 190; 189, identifier:op; 190, identifier:exists; 191, argument_list; 191, 192; 192, identifier:bedfile; 193, block; 193, 194; 193, 199; 194, expression_statement; 194, 195; 195, assignment; 195, 196; 195, 197; 196, identifier:args; 197, list:[posmapfile]; 197, 198; 198, identifier:posmapfile; 199, expression_statement; 199, 200; 200, call; 200, 201; 200, 202; 201, identifier:bed; 202, argument_list; 202, 203; 203, identifier:args; 204, comment; 205, expression_statement; 205, 206; 206, assignment; 206, 207; 206, 208; 207, identifier:bedpefile; 208, binary_operator:+; 208, 209; 208, 210; 209, identifier:scf; 210, string:".bedpe"; 211, expression_statement; 211, 212; 212, assignment; 212, 213; 212, 214; 213, identifier:pairsbedfile; 214, binary_operator:+; 214, 215; 214, 216; 215, identifier:scf; 216, string:".pairs.bed"; 217, if_statement; 217, 218; 217, 233; 218, not_operator; 218, 219; 219, parenthesized_expression; 219, 220; 220, boolean_operator:and; 220, 221; 220, 227; 221, call; 221, 222; 221, 225; 222, attribute; 222, 223; 222, 224; 223, identifier:op; 224, identifier:exists; 225, argument_list; 225, 226; 226, identifier:bedpefile; 227, call; 227, 228; 227, 231; 228, attribute; 228, 229; 228, 230; 229, identifier:op; 230, identifier:exists; 231, argument_list; 231, 232; 232, identifier:pairsbedfile; 233, block; 233, 234; 234, expression_statement; 234, 235; 235, call; 235, 236; 235, 237; 236, identifier:bed_to_bedpe; 237, argument_list; 237, 238; 237, 239; 237, 240; 237, 243; 238, identifier:bedfile; 239, identifier:bedpefile; 240, keyword_argument; 240, 241; 240, 242; 241, identifier:pairsbedfile; 242, identifier:pairsbedfile; 243, keyword_argument; 243, 244; 243, 245; 244, identifier:ca; 245, True; 246, comment; 247, expression_statement; 247, 248; 248, call; 248, 249; 248, 250; 249, identifier:Coverage; 250, argument_list; 250, 251; 250, 252; 251, identifier:bedfile; 252, identifier:sizesfile; 253, expression_statement; 253, 254; 254, call; 254, 255; 254, 256; 255, identifier:Coverage; 256, argument_list; 256, 257; 256, 258; 257, identifier:pairsbedfile; 258, identifier:sizesfile
def posmap(args): """ %prog posmap frgscf.sorted scf.fasta scfID Perform QC on the selected scfID, generate multiple BED files for plotting. """ p = OptionParser(posmap.__doc__) opts, args = p.parse_args(args) if len(args) != 3: sys.exit(p.print_help()) frgscffile, fastafile, scf = args # fasta cmd = "faOneRecord {0} {1}".format(fastafile, scf) scffastafile = scf + ".fasta" if not op.exists(scffastafile): sh(cmd, outfile=scffastafile) # sizes sizesfile = scffastafile + ".sizes" sizes = Sizes(scffastafile).mapping scfsize = sizes[scf] logging.debug("`{0}` has length of {1}.".format(scf, scfsize)) # gaps.bed gapsbedfile = scf + ".gaps.bed" if not op.exists(gapsbedfile): args = [scffastafile, "--bed", "--mingap=100"] gaps(args) # reads frgscf posmap posmapfile = scf + ".posmap" if not op.exists(posmapfile): args = [frgscffile, scf] query(args) # reads bed bedfile = scf + ".bed" if not op.exists(bedfile): args = [posmapfile] bed(args) # reads bedpe bedpefile = scf + ".bedpe" pairsbedfile = scf + ".pairs.bed" if not (op.exists(bedpefile) and op.exists(pairsbedfile)): bed_to_bedpe(bedfile, bedpefile, pairsbedfile=pairsbedfile, ca=True) # base coverage Coverage(bedfile, sizesfile) Coverage(pairsbedfile, sizesfile)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:shred; 3, parameters; 3, 4; 4, identifier:args; 5, block; 5, 6; 5, 8; 5, 17; 5, 28; 5, 48; 5, 53; 5, 60; 5, 66; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:p; 11, call; 11, 12; 11, 13; 12, identifier:OptionParser; 13, argument_list; 13, 14; 14, attribute; 14, 15; 14, 16; 15, identifier:shred; 16, identifier:__doc__; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 22; 19, pattern_list; 19, 20; 19, 21; 20, identifier:opts; 21, identifier:args; 22, call; 22, 23; 22, 26; 23, attribute; 23, 24; 23, 25; 24, identifier:p; 25, identifier:parse_args; 26, argument_list; 26, 27; 27, identifier:args; 28, if_statement; 28, 29; 28, 35; 29, comparison_operator:!=; 29, 30; 29, 34; 30, call; 30, 31; 30, 32; 31, identifier:len; 32, argument_list; 32, 33; 33, identifier:args; 34, integer:1; 35, block; 35, 36; 36, expression_statement; 36, 37; 37, call; 37, 38; 37, 41; 38, attribute; 38, 39; 38, 40; 39, identifier:sys; 40, identifier:exit; 41, argument_list; 41, 42; 42, not_operator; 42, 43; 43, call; 43, 44; 43, 47; 44, attribute; 44, 45; 44, 46; 45, identifier:p; 46, identifier:print_help; 47, argument_list; 48, expression_statement; 48, 49; 49, assignment; 49, 50; 49, 52; 50, pattern_list; 50, 51; 51, identifier:s; 52, identifier:args; 53, expression_statement; 53, 54; 54, assignment; 54, 55; 54, 56; 55, identifier:u; 56, call; 56, 57; 56, 58; 57, identifier:UnitigLayout; 58, argument_list; 58, 59; 59, identifier:s; 60, expression_statement; 60, 61; 61, call; 61, 62; 61, 65; 62, attribute; 62, 63; 62, 64; 63, identifier:u; 64, identifier:shred; 65, argument_list; 66, expression_statement; 66, 67; 67, call; 67, 68; 67, 71; 68, attribute; 68, 69; 68, 70; 69, identifier:u; 70, identifier:print_to_file; 71, argument_list; 71, 72; 72, keyword_argument; 72, 73; 72, 74; 73, identifier:inplace; 74, True
def shred(args): """ %prog shred unitigfile Shred the unitig into one fragment per unitig to fix. This is the last resort as a desperate fix. """ p = OptionParser(shred.__doc__) opts, args = p.parse_args(args) if len(args) != 1: sys.exit(not p.print_help()) s, = args u = UnitigLayout(s) u.shred() u.print_to_file(inplace=True)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:index; 3, parameters; 3, 4; 4, identifier:args; 5, block; 5, 6; 5, 8; 5, 17; 5, 28; 5, 47; 5, 52; 5, 58; 5, 67; 5, 84; 5, 90; 5, 91; 5, 100; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:p; 11, call; 11, 12; 11, 13; 12, identifier:OptionParser; 13, argument_list; 13, 14; 14, attribute; 14, 15; 14, 16; 15, identifier:index; 16, identifier:__doc__; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 22; 19, pattern_list; 19, 20; 19, 21; 20, identifier:opts; 21, identifier:args; 22, call; 22, 23; 22, 26; 23, attribute; 23, 24; 23, 25; 24, identifier:p; 25, identifier:parse_args; 26, argument_list; 26, 27; 27, identifier:args; 28, if_statement; 28, 29; 28, 35; 29, comparison_operator:!=; 29, 30; 29, 34; 30, call; 30, 31; 30, 32; 31, identifier:len; 32, argument_list; 32, 33; 33, identifier:args; 34, integer:1; 35, block; 35, 36; 36, expression_statement; 36, 37; 37, call; 37, 38; 37, 41; 38, attribute; 38, 39; 38, 40; 39, identifier:sys; 40, identifier:exit; 41, argument_list; 41, 42; 42, call; 42, 43; 42, 46; 43, attribute; 43, 44; 43, 45; 44, identifier:p; 45, identifier:print_help; 46, argument_list; 47, expression_statement; 47, 48; 48, assignment; 48, 49; 48, 51; 49, pattern_list; 49, 50; 50, identifier:frgscffile; 51, identifier:args; 52, expression_statement; 52, 53; 53, assignment; 53, 54; 53, 55; 54, identifier:gzfile; 55, binary_operator:+; 55, 56; 55, 57; 56, identifier:frgscffile; 57, string:".gz"; 58, expression_statement; 58, 59; 59, assignment; 59, 60; 59, 61; 60, identifier:cmd; 61, call; 61, 62; 61, 65; 62, attribute; 62, 63; 62, 64; 63, string:"bgzip -c {0}"; 64, identifier:format; 65, argument_list; 65, 66; 66, identifier:frgscffile; 67, if_statement; 67, 68; 67, 75; 68, not_operator; 68, 69; 69, call; 69, 70; 69, 73; 70, attribute; 70, 71; 70, 72; 71, identifier:op; 72, identifier:exists; 73, argument_list; 73, 74; 74, identifier:gzfile; 75, block; 75, 76; 76, expression_statement; 76, 77; 77, call; 77, 78; 77, 79; 78, identifier:sh; 79, argument_list; 79, 80; 79, 81; 80, identifier:cmd; 81, keyword_argument; 81, 82; 81, 83; 82, identifier:outfile; 83, identifier:gzfile; 84, expression_statement; 84, 85; 85, assignment; 85, 86; 85, 87; 86, identifier:tbifile; 87, binary_operator:+; 87, 88; 87, 89; 88, identifier:gzfile; 89, string:".tbi"; 90, comment; 91, expression_statement; 91, 92; 92, assignment; 92, 93; 92, 94; 93, identifier:cmd; 94, call; 94, 95; 94, 98; 95, attribute; 95, 96; 95, 97; 96, string:"tabix -s 2 -b 3 -e 4 {0}"; 97, identifier:format; 98, argument_list; 98, 99; 99, identifier:gzfile; 100, if_statement; 100, 101; 100, 108; 101, not_operator; 101, 102; 102, call; 102, 103; 102, 106; 103, attribute; 103, 104; 103, 105; 104, identifier:op; 105, identifier:exists; 106, argument_list; 106, 107; 107, identifier:tbifile; 108, block; 108, 109; 109, expression_statement; 109, 110; 110, call; 110, 111; 110, 112; 111, identifier:sh; 112, argument_list; 112, 113; 113, identifier:cmd
def index(args): """ %prog index frgscf.sorted Compress frgscffile.sorted and index it using `tabix`. """ p = OptionParser(index.__doc__) opts, args = p.parse_args(args) if len(args) != 1: sys.exit(p.print_help()) frgscffile, = args gzfile = frgscffile + ".gz" cmd = "bgzip -c {0}".format(frgscffile) if not op.exists(gzfile): sh(cmd, outfile=gzfile) tbifile = gzfile + ".tbi" # Sequence, begin, end in 2, 3, 4-th column, respectively cmd = "tabix -s 2 -b 3 -e 4 {0}".format(gzfile) if not op.exists(tbifile): sh(cmd)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:sort; 3, parameters; 3, 4; 4, identifier:args; 5, block; 5, 6; 5, 8; 5, 14; 5, 23; 5, 39; 5, 58; 5, 64; 5, 70; 5, 77; 5, 88; 5, 108; 5, 113; 5, 119; 5, 159; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:valid_sort_methods; 11, tuple; 11, 12; 11, 13; 12, string:"unix"; 13, string:"topo"; 14, expression_statement; 14, 15; 15, assignment; 15, 16; 15, 17; 16, identifier:p; 17, call; 17, 18; 17, 19; 18, identifier:OptionParser; 19, argument_list; 19, 20; 20, attribute; 20, 21; 20, 22; 21, identifier:sort; 22, identifier:__doc__; 23, expression_statement; 23, 24; 24, call; 24, 25; 24, 28; 25, attribute; 25, 26; 25, 27; 26, identifier:p; 27, identifier:add_option; 28, argument_list; 28, 29; 28, 30; 28, 33; 28, 36; 29, string:"--method"; 30, keyword_argument; 30, 31; 30, 32; 31, identifier:default; 32, string:"unix"; 33, keyword_argument; 33, 34; 33, 35; 34, identifier:choices; 35, identifier:valid_sort_methods; 36, keyword_argument; 36, 37; 36, 38; 37, identifier:help; 38, string:"Specify sort method [default: %default]"; 39, expression_statement; 39, 40; 40, call; 40, 41; 40, 44; 41, attribute; 41, 42; 41, 43; 42, identifier:p; 43, identifier:add_option; 44, argument_list; 44, 45; 44, 46; 44, 49; 44, 52; 44, 55; 45, string:"-i"; 46, keyword_argument; 46, 47; 46, 48; 47, identifier:dest; 48, string:"inplace"; 49, keyword_argument; 49, 50; 49, 51; 50, identifier:default; 51, False; 52, keyword_argument; 52, 53; 52, 54; 53, identifier:action; 54, string:"store_true"; 55, keyword_argument; 55, 56; 55, 57; 56, identifier:help; 57, string:"If doing a unix sort, perform sort inplace [default: %default]"; 58, expression_statement; 58, 59; 59, call; 59, 60; 59, 63; 60, attribute; 60, 61; 60, 62; 61, identifier:p; 62, identifier:set_tmpdir; 63, argument_list; 64, expression_statement; 64, 65; 65, call; 65, 66; 65, 69; 66, attribute; 66, 67; 66, 68; 67, identifier:p; 68, identifier:set_outfile; 69, argument_list; 70, expression_statement; 70, 71; 71, call; 71, 72; 71, 75; 72, attribute; 72, 73; 72, 74; 73, identifier:p; 74, identifier:set_home; 75, argument_list; 75, 76; 76, string:"gt"; 77, expression_statement; 77, 78; 78, assignment; 78, 79; 78, 82; 79, pattern_list; 79, 80; 79, 81; 80, identifier:opts; 81, identifier:args; 82, call; 82, 83; 82, 86; 83, attribute; 83, 84; 83, 85; 84, identifier:p; 85, identifier:parse_args; 86, argument_list; 86, 87; 87, identifier:args; 88, if_statement; 88, 89; 88, 95; 89, comparison_operator:!=; 89, 90; 89, 94; 90, call; 90, 91; 90, 92; 91, identifier:len; 92, argument_list; 92, 93; 93, identifier:args; 94, integer:1; 95, block; 95, 96; 96, expression_statement; 96, 97; 97, call; 97, 98; 97, 101; 98, attribute; 98, 99; 98, 100; 99, identifier:sys; 100, identifier:exit; 101, argument_list; 101, 102; 102, not_operator; 102, 103; 103, call; 103, 104; 103, 107; 104, attribute; 104, 105; 104, 106; 105, identifier:p; 106, identifier:print_help; 107, argument_list; 108, expression_statement; 108, 109; 109, assignment; 109, 110; 109, 112; 110, pattern_list; 110, 111; 111, identifier:gffile; 112, identifier:args; 113, expression_statement; 113, 114; 114, assignment; 114, 115; 114, 116; 115, identifier:sortedgff; 116, attribute; 116, 117; 116, 118; 117, identifier:opts; 118, identifier:outfile; 119, if_statement; 119, 120; 119, 123; 120, attribute; 120, 121; 120, 122; 121, identifier:opts; 122, identifier:inplace; 123, block; 123, 124; 124, if_statement; 124, 125; 124, 143; 125, boolean_operator:or; 125, 126; 125, 131; 126, comparison_operator:==; 126, 127; 126, 130; 127, attribute; 127, 128; 127, 129; 128, identifier:opts; 129, identifier:method; 130, string:"topo"; 131, parenthesized_expression; 131, 132; 132, boolean_operator:and; 132, 133; 132, 138; 133, comparison_operator:==; 133, 134; 133, 137; 134, attribute; 134, 135; 134, 136; 135, identifier:opts; 136, identifier:method; 137, string:"unix"; 138, comparison_operator:in; 138, 139; 138, 140; 139, identifier:gffile; 140, tuple; 140, 141; 140, 142; 141, string:"-"; 142, string:"stdin"; 143, block; 143, 144; 143, 153; 144, expression_statement; 144, 145; 145, call; 145, 146; 145, 149; 146, attribute; 146, 147; 146, 148; 147, identifier:logging; 148, identifier:error; 149, argument_list; 149, 150; 150, binary_operator:+; 150, 151; 150, 152; 151, string:"Cannot perform inplace sort when method is `topo`"; 152, string:" or method is `unix` and input is `stdin` stream"; 153, expression_statement; 153, 154; 154, call; 154, 155; 154, 158; 155, attribute; 155, 156; 155, 157; 156, identifier:sys; 157, identifier:exit; 158, argument_list; 159, if_statement; 159, 160; 159, 165; 159, 218; 160, comparison_operator:==; 160, 161; 160, 164; 161, attribute; 161, 162; 161, 163; 162, identifier:opts; 163, identifier:method; 164, string:"unix"; 165, block; 165, 166; 165, 170; 165, 179; 165, 195; 165, 210; 166, expression_statement; 166, 167; 167, assignment; 167, 168; 167, 169; 168, identifier:cmd; 169, string:"sort"; 170, expression_statement; 170, 171; 171, augmented_assignment:+=; 171, 172; 171, 173; 172, identifier:cmd; 173, call; 173, 174; 173, 177; 174, attribute; 174, 175; 174, 176; 175, string:" -k1,1 -k4,4n {0}"; 176, identifier:format; 177, argument_list; 177, 178; 178, identifier:gffile; 179, if_statement; 179, 180; 179, 183; 180, attribute; 180, 181; 180, 182; 181, identifier:opts; 182, identifier:tmpdir; 183, block; 183, 184; 184, expression_statement; 184, 185; 185, augmented_assignment:+=; 185, 186; 185, 187; 186, identifier:cmd; 187, call; 187, 188; 187, 191; 188, attribute; 188, 189; 188, 190; 189, string:" -T {0}"; 190, identifier:format; 191, argument_list; 191, 192; 192, attribute; 192, 193; 192, 194; 193, identifier:opts; 194, identifier:tmpdir; 195, if_statement; 195, 196; 195, 199; 196, attribute; 196, 197; 196, 198; 197, identifier:opts; 198, identifier:inplace; 199, block; 199, 200; 199, 206; 200, expression_statement; 200, 201; 201, augmented_assignment:+=; 201, 202; 201, 203; 202, identifier:cmd; 203, attribute; 203, 204; 203, 205; 204, string:" -o {0}"; 205, identifier:gffile; 206, expression_statement; 206, 207; 207, assignment; 207, 208; 207, 209; 208, identifier:sortedgff; 209, None; 210, expression_statement; 210, 211; 211, call; 211, 212; 211, 213; 212, identifier:sh; 213, argument_list; 213, 214; 213, 215; 214, identifier:cmd; 215, keyword_argument; 215, 216; 215, 217; 216, identifier:outfile; 217, identifier:sortedgff; 218, elif_clause; 218, 219; 218, 224; 219, comparison_operator:==; 219, 220; 219, 223; 220, attribute; 220, 221; 220, 222; 221, identifier:opts; 222, identifier:method; 223, string:"topo"; 224, block; 224, 225; 224, 231; 224, 258; 224, 274; 224, 283; 225, expression_statement; 225, 226; 226, assignment; 226, 227; 226, 228; 227, identifier:GT_HOME; 228, attribute; 228, 229; 228, 230; 229, identifier:opts; 230, identifier:gt_home; 231, if_statement; 231, 232; 231, 239; 232, not_operator; 232, 233; 233, call; 233, 234; 233, 237; 234, attribute; 234, 235; 234, 236; 235, identifier:op; 236, identifier:isdir; 237, argument_list; 237, 238; 238, identifier:GT_HOME; 239, block; 239, 240; 239, 252; 240, expression_statement; 240, 241; 241, call; 241, 242; 241, 245; 242, attribute; 242, 243; 242, 244; 243, identifier:logging; 244, identifier:error; 245, argument_list; 245, 246; 246, call; 246, 247; 246, 250; 247, attribute; 247, 248; 247, 249; 248, string:"GT_HOME={0} directory does not exist"; 249, identifier:format; 250, argument_list; 250, 251; 251, identifier:GT_HOME; 252, expression_statement; 252, 253; 253, call; 253, 254; 253, 257; 254, attribute; 254, 255; 254, 256; 255, identifier:sys; 256, identifier:exit; 257, argument_list; 258, expression_statement; 258, 259; 259, assignment; 259, 260; 259, 261; 260, identifier:cmd; 261, call; 261, 262; 261, 265; 262, attribute; 262, 263; 262, 264; 263, string:"{0}"; 264, identifier:format; 265, argument_list; 265, 266; 266, call; 266, 267; 266, 270; 267, attribute; 267, 268; 267, 269; 268, identifier:op; 269, identifier:join; 270, argument_list; 270, 271; 270, 272; 270, 273; 271, identifier:GT_HOME; 272, string:"bin"; 273, string:"gt"; 274, expression_statement; 274, 275; 275, augmented_assignment:+=; 275, 276; 275, 277; 276, identifier:cmd; 277, call; 277, 278; 277, 281; 278, attribute; 278, 279; 278, 280; 279, string:" gff3 -sort -tidy -retainids -addids no {0}"; 280, identifier:format; 281, argument_list; 281, 282; 282, identifier:gffile; 283, expression_statement; 283, 284; 284, call; 284, 285; 284, 286; 285, identifier:sh; 286, argument_list; 286, 287; 286, 288; 287, identifier:cmd; 288, keyword_argument; 288, 289; 288, 290; 289, identifier:outfile; 290, identifier:sortedgff
def sort(args): """ %prog sort gffile Sort gff file using plain old unix sort based on [chromosome, start coordinate]. or topologically based on hierarchy of features using the gt (genometools) toolkit """ valid_sort_methods = ("unix", "topo") p = OptionParser(sort.__doc__) p.add_option("--method", default="unix", choices=valid_sort_methods, help="Specify sort method [default: %default]") p.add_option("-i", dest="inplace", default=False, action="store_true", help="If doing a unix sort, perform sort inplace [default: %default]") p.set_tmpdir() p.set_outfile() p.set_home("gt") opts, args = p.parse_args(args) if len(args) != 1: sys.exit(not p.print_help()) gffile, = args sortedgff = opts.outfile if opts.inplace: if opts.method == "topo" or (opts.method == "unix" and gffile in ("-", "stdin")): logging.error("Cannot perform inplace sort when method is `topo`" + \ " or method is `unix` and input is `stdin` stream") sys.exit() if opts.method == "unix": cmd = "sort" cmd += " -k1,1 -k4,4n {0}".format(gffile) if opts.tmpdir: cmd += " -T {0}".format(opts.tmpdir) if opts.inplace: cmd += " -o {0}".gffile sortedgff = None sh(cmd, outfile=sortedgff) elif opts.method == "topo": GT_HOME = opts.gt_home if not op.isdir(GT_HOME): logging.error("GT_HOME={0} directory does not exist".format(GT_HOME)) sys.exit() cmd = "{0}".format(op.join(GT_HOME, "bin", "gt")) cmd += " gff3 -sort -tidy -retainids -addids no {0}".format(gffile) sh(cmd, outfile=sortedgff)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:sort_layout; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:thread; 5, identifier:listfile; 6, default_parameter; 6, 7; 6, 8; 7, identifier:column; 8, integer:0; 9, block; 9, 10; 9, 12; 9, 19; 9, 33; 9, 39; 9, 47; 9, 60; 9, 64; 9, 70; 9, 106; 9, 116; 9, 129; 9, 145; 9, 152; 9, 183; 9, 200; 9, 206; 10, expression_statement; 10, 11; 11, comment; 12, import_from_statement; 12, 13; 12, 17; 13, dotted_name; 13, 14; 13, 15; 13, 16; 14, identifier:jcvi; 15, identifier:formats; 16, identifier:base; 17, dotted_name; 17, 18; 18, identifier:DictFile; 19, expression_statement; 19, 20; 20, assignment; 20, 21; 20, 22; 21, identifier:outfile; 22, binary_operator:+; 22, 23; 22, 32; 23, subscript; 23, 24; 23, 31; 24, call; 24, 25; 24, 28; 25, attribute; 25, 26; 25, 27; 26, identifier:listfile; 27, identifier:rsplit; 28, argument_list; 28, 29; 28, 30; 29, string:"."; 30, integer:1; 31, integer:0; 32, string:".sorted.list"; 33, expression_statement; 33, 34; 34, assignment; 34, 35; 34, 36; 35, identifier:threadorder; 36, attribute; 36, 37; 36, 38; 37, identifier:thread; 38, identifier:order; 39, expression_statement; 39, 40; 40, assignment; 40, 41; 40, 42; 41, identifier:fw; 42, call; 42, 43; 42, 44; 43, identifier:open; 44, argument_list; 44, 45; 44, 46; 45, identifier:outfile; 46, string:"w"; 47, expression_statement; 47, 48; 48, assignment; 48, 49; 48, 50; 49, identifier:lt; 50, call; 50, 51; 50, 52; 51, identifier:DictFile; 52, argument_list; 52, 53; 52, 54; 52, 57; 53, identifier:listfile; 54, keyword_argument; 54, 55; 54, 56; 55, identifier:keypos; 56, identifier:column; 57, keyword_argument; 57, 58; 57, 59; 58, identifier:valuepos; 59, None; 60, expression_statement; 60, 61; 61, assignment; 61, 62; 61, 63; 62, identifier:threaded; 63, list:[]; 64, expression_statement; 64, 65; 65, assignment; 65, 66; 65, 67; 66, identifier:imported; 67, call; 67, 68; 67, 69; 68, identifier:set; 69, argument_list; 70, for_statement; 70, 71; 70, 72; 70, 73; 71, identifier:t; 72, identifier:thread; 73, block; 73, 74; 73, 80; 73, 86; 73, 93; 73, 99; 74, expression_statement; 74, 75; 75, assignment; 75, 76; 75, 77; 76, identifier:accn; 77, attribute; 77, 78; 77, 79; 78, identifier:t; 79, identifier:accn; 80, if_statement; 80, 81; 80, 84; 81, comparison_operator:not; 81, 82; 81, 83; 82, identifier:accn; 83, identifier:lt; 84, block; 84, 85; 85, continue_statement; 86, expression_statement; 86, 87; 87, call; 87, 88; 87, 91; 88, attribute; 88, 89; 88, 90; 89, identifier:imported; 90, identifier:add; 91, argument_list; 91, 92; 92, identifier:accn; 93, expression_statement; 93, 94; 94, assignment; 94, 95; 94, 96; 95, identifier:atoms; 96, subscript; 96, 97; 96, 98; 97, identifier:lt; 98, identifier:accn; 99, expression_statement; 99, 100; 100, call; 100, 101; 100, 104; 101, attribute; 101, 102; 101, 103; 102, identifier:threaded; 103, identifier:append; 104, argument_list; 104, 105; 105, identifier:atoms; 106, assert_statement; 106, 107; 107, comparison_operator:==; 107, 108; 107, 112; 108, call; 108, 109; 108, 110; 109, identifier:len; 110, argument_list; 110, 111; 111, identifier:threaded; 112, call; 112, 113; 112, 114; 113, identifier:len; 114, argument_list; 114, 115; 115, identifier:imported; 116, expression_statement; 116, 117; 117, assignment; 117, 118; 117, 119; 118, identifier:total; 119, call; 119, 120; 119, 121; 120, identifier:sum; 121, generator_expression; 121, 122; 121, 123; 122, integer:1; 123, for_in_clause; 123, 124; 123, 125; 124, identifier:x; 125, call; 125, 126; 125, 127; 126, identifier:open; 127, argument_list; 127, 128; 128, identifier:listfile; 129, expression_statement; 129, 130; 130, call; 130, 131; 130, 134; 131, attribute; 131, 132; 131, 133; 132, identifier:logging; 133, identifier:debug; 134, argument_list; 134, 135; 135, call; 135, 136; 135, 139; 136, attribute; 136, 137; 136, 138; 137, string:"Total: {0}, currently threaded: {1}"; 138, identifier:format; 139, argument_list; 139, 140; 139, 141; 140, identifier:total; 141, call; 141, 142; 141, 143; 142, identifier:len; 143, argument_list; 143, 144; 144, identifier:threaded; 145, expression_statement; 145, 146; 146, assignment; 146, 147; 146, 148; 147, identifier:fp; 148, call; 148, 149; 148, 150; 149, identifier:open; 150, argument_list; 150, 151; 151, identifier:listfile; 152, for_statement; 152, 153; 152, 154; 152, 155; 153, identifier:row; 154, identifier:fp; 155, block; 155, 156; 155, 164; 155, 170; 155, 176; 156, expression_statement; 156, 157; 157, assignment; 157, 158; 157, 159; 158, identifier:atoms; 159, call; 159, 160; 159, 163; 160, attribute; 160, 161; 160, 162; 161, identifier:row; 162, identifier:split; 163, argument_list; 164, expression_statement; 164, 165; 165, assignment; 165, 166; 165, 167; 166, identifier:accn; 167, subscript; 167, 168; 167, 169; 168, identifier:atoms; 169, integer:0; 170, if_statement; 170, 171; 170, 174; 171, comparison_operator:in; 171, 172; 171, 173; 172, identifier:accn; 173, identifier:imported; 174, block; 174, 175; 175, continue_statement; 176, expression_statement; 176, 177; 177, call; 177, 178; 177, 179; 178, identifier:insert_into_threaded; 179, argument_list; 179, 180; 179, 181; 179, 182; 180, identifier:atoms; 181, identifier:threaded; 182, identifier:threadorder; 183, for_statement; 183, 184; 183, 185; 183, 186; 184, identifier:atoms; 185, identifier:threaded; 186, block; 186, 187; 187, expression_statement; 187, 188; 188, call; 188, 189; 188, 190; 189, identifier:print; 190, argument_list; 190, 191; 190, 197; 191, call; 191, 192; 191, 195; 192, attribute; 192, 193; 192, 194; 193, string:"\t"; 194, identifier:join; 195, argument_list; 195, 196; 196, identifier:atoms; 197, keyword_argument; 197, 198; 197, 199; 198, identifier:file; 199, identifier:fw; 200, expression_statement; 200, 201; 201, call; 201, 202; 201, 205; 202, attribute; 202, 203; 202, 204; 203, identifier:fw; 204, identifier:close; 205, argument_list; 206, expression_statement; 206, 207; 207, call; 207, 208; 207, 211; 208, attribute; 208, 209; 208, 210; 209, identifier:logging; 210, identifier:debug; 211, argument_list; 211, 212; 212, call; 212, 213; 212, 216; 213, attribute; 213, 214; 213, 215; 214, string:"File `{0}` sorted to `{1}`."; 215, identifier:format; 216, argument_list; 216, 217; 216, 218; 217, identifier:outfile; 218, attribute; 218, 219; 218, 220; 219, identifier:thread; 220, identifier:filename
def sort_layout(thread, listfile, column=0): """ Sort the syntelog table according to chromomomal positions. First orient the contents against threadbed, then for contents not in threadbed, insert to the nearest neighbor. """ from jcvi.formats.base import DictFile outfile = listfile.rsplit(".", 1)[0] + ".sorted.list" threadorder = thread.order fw = open(outfile, "w") lt = DictFile(listfile, keypos=column, valuepos=None) threaded = [] imported = set() for t in thread: accn = t.accn if accn not in lt: continue imported.add(accn) atoms = lt[accn] threaded.append(atoms) assert len(threaded) == len(imported) total = sum(1 for x in open(listfile)) logging.debug("Total: {0}, currently threaded: {1}".format(total, len(threaded))) fp = open(listfile) for row in fp: atoms = row.split() accn = atoms[0] if accn in imported: continue insert_into_threaded(atoms, threaded, threadorder) for atoms in threaded: print("\t".join(atoms), file=fw) fw.close() logging.debug("File `{0}` sorted to `{1}`.".format(outfile, thread.filename))
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:fromgroups; 3, parameters; 3, 4; 4, identifier:args; 5, block; 5, 6; 5, 8; 5, 15; 5, 24; 5, 35; 5, 55; 5, 61; 5, 69; 5, 80; 5, 87; 5, 104; 6, expression_statement; 6, 7; 7, comment; 8, import_from_statement; 8, 9; 8, 13; 9, dotted_name; 9, 10; 9, 11; 9, 12; 10, identifier:jcvi; 11, identifier:formats; 12, identifier:bed; 13, dotted_name; 13, 14; 14, identifier:Bed; 15, expression_statement; 15, 16; 16, assignment; 16, 17; 16, 18; 17, identifier:p; 18, call; 18, 19; 18, 20; 19, identifier:OptionParser; 20, argument_list; 20, 21; 21, attribute; 21, 22; 21, 23; 22, identifier:fromgroups; 23, identifier:__doc__; 24, expression_statement; 24, 25; 25, assignment; 25, 26; 25, 29; 26, pattern_list; 26, 27; 26, 28; 27, identifier:opts; 28, identifier:args; 29, call; 29, 30; 29, 33; 30, attribute; 30, 31; 30, 32; 31, identifier:p; 32, identifier:parse_args; 33, argument_list; 33, 34; 34, identifier:args; 35, if_statement; 35, 36; 35, 42; 36, comparison_operator:<; 36, 37; 36, 41; 37, call; 37, 38; 37, 39; 38, identifier:len; 39, argument_list; 39, 40; 40, identifier:args; 41, integer:2; 42, block; 42, 43; 43, expression_statement; 43, 44; 44, call; 44, 45; 44, 48; 45, attribute; 45, 46; 45, 47; 46, identifier:sys; 47, identifier:exit; 48, argument_list; 48, 49; 49, not_operator; 49, 50; 50, call; 50, 51; 50, 54; 51, attribute; 51, 52; 51, 53; 52, identifier:p; 53, identifier:print_help; 54, argument_list; 55, expression_statement; 55, 56; 56, assignment; 56, 57; 56, 58; 57, identifier:groupsfile; 58, subscript; 58, 59; 58, 60; 59, identifier:args; 60, integer:0; 61, expression_statement; 61, 62; 62, assignment; 62, 63; 62, 64; 63, identifier:bedfiles; 64, subscript; 64, 65; 64, 66; 65, identifier:args; 66, slice; 66, 67; 66, 68; 67, integer:1; 68, colon; 69, expression_statement; 69, 70; 70, assignment; 70, 71; 70, 72; 71, identifier:beds; 72, list_comprehension; 72, 73; 72, 77; 73, call; 73, 74; 73, 75; 74, identifier:Bed; 75, argument_list; 75, 76; 76, identifier:x; 77, for_in_clause; 77, 78; 77, 79; 78, identifier:x; 79, identifier:bedfiles; 80, expression_statement; 80, 81; 81, assignment; 81, 82; 81, 83; 82, identifier:fp; 83, call; 83, 84; 83, 85; 84, identifier:open; 85, argument_list; 85, 86; 86, identifier:groupsfile; 87, expression_statement; 87, 88; 88, assignment; 88, 89; 88, 90; 89, identifier:groups; 90, list_comprehension; 90, 91; 90, 101; 91, call; 91, 92; 91, 99; 92, attribute; 92, 93; 92, 98; 93, call; 93, 94; 93, 97; 94, attribute; 94, 95; 94, 96; 95, identifier:row; 96, identifier:strip; 97, argument_list; 98, identifier:split; 99, argument_list; 99, 100; 100, string:","; 101, for_in_clause; 101, 102; 101, 103; 102, identifier:row; 103, identifier:fp; 104, for_statement; 104, 105; 104, 108; 104, 115; 105, pattern_list; 105, 106; 105, 107; 106, identifier:b1; 107, identifier:b2; 108, call; 108, 109; 108, 110; 109, identifier:product; 110, argument_list; 110, 111; 110, 112; 111, identifier:beds; 112, keyword_argument; 112, 113; 112, 114; 113, identifier:repeat; 114, integer:2; 115, block; 115, 116; 116, expression_statement; 116, 117; 117, call; 117, 118; 117, 119; 118, identifier:extract_pairs; 119, argument_list; 119, 120; 119, 121; 119, 122; 120, identifier:b1; 121, identifier:b2; 122, identifier:groups
def fromgroups(args): """ %prog fromgroups groupsfile a.bed b.bed ... Flatten the gene familes into pairs, the groupsfile is a file with each line containing the members, separated by comma. The commands also require several bed files in order to sort the pairs into different piles (e.g. pairs of species in comparison. """ from jcvi.formats.bed import Bed p = OptionParser(fromgroups.__doc__) opts, args = p.parse_args(args) if len(args) < 2: sys.exit(not p.print_help()) groupsfile = args[0] bedfiles = args[1:] beds = [Bed(x) for x in bedfiles] fp = open(groupsfile) groups = [row.strip().split(",") for row in fp] for b1, b2 in product(beds, repeat=2): extract_pairs(b1, b2, groups)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:merge_paths; 3, parameters; 3, 4; 3, 5; 4, identifier:paths; 5, default_parameter; 5, 6; 5, 7; 6, identifier:weights; 7, None; 8, block; 8, 9; 8, 11; 8, 21; 8, 28; 9, expression_statement; 9, 10; 10, comment; 11, expression_statement; 11, 12; 12, assignment; 12, 13; 12, 14; 13, identifier:G; 14, call; 14, 15; 14, 16; 15, identifier:make_paths; 16, argument_list; 16, 17; 16, 18; 17, identifier:paths; 18, keyword_argument; 18, 19; 18, 20; 19, identifier:weights; 20, identifier:weights; 21, expression_statement; 21, 22; 22, assignment; 22, 23; 22, 24; 23, identifier:G; 24, call; 24, 25; 24, 26; 25, identifier:reduce_paths; 26, argument_list; 26, 27; 27, identifier:G; 28, return_statement; 28, 29; 29, identifier:G
def merge_paths(paths, weights=None): """ Zip together sorted lists. >>> paths = [[1, 2, 3], [1, 3, 4], [2, 4, 5]] >>> G = merge_paths(paths) >>> nx.topological_sort(G) [1, 2, 3, 4, 5] >>> paths = [[1, 2, 3, 4], [1, 2, 3, 2, 4]] >>> G = merge_paths(paths, weights=(1, 2)) >>> nx.topological_sort(G) [1, 2, 3, 4] """ G = make_paths(paths, weights=weights) G = reduce_paths(G) return G
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:build; 3, parameters; 3, 4; 4, identifier:args; 5, block; 5, 6; 5, 8; 5, 15; 5, 22; 5, 29; 5, 38; 5, 54; 5, 65; 5, 85; 5, 92; 5, 108; 5, 117; 5, 125; 5, 134; 5, 140; 5, 149; 5, 155; 5, 165; 5, 170; 5, 176; 6, expression_statement; 6, 7; 7, comment; 8, import_from_statement; 8, 9; 8, 13; 9, dotted_name; 9, 10; 9, 11; 9, 12; 10, identifier:jcvi; 11, identifier:apps; 12, identifier:cdhit; 13, dotted_name; 13, 14; 14, identifier:deduplicate; 15, import_from_statement; 15, 16; 15, 20; 16, dotted_name; 16, 17; 16, 18; 16, 19; 17, identifier:jcvi; 18, identifier:apps; 19, identifier:vecscreen; 20, dotted_name; 20, 21; 21, identifier:mask; 22, import_from_statement; 22, 23; 22, 27; 23, dotted_name; 23, 24; 23, 25; 23, 26; 24, identifier:jcvi; 25, identifier:formats; 26, identifier:fasta; 27, dotted_name; 27, 28; 28, identifier:sort; 29, expression_statement; 29, 30; 30, assignment; 30, 31; 30, 32; 31, identifier:p; 32, call; 32, 33; 32, 34; 33, identifier:OptionParser; 34, argument_list; 34, 35; 35, attribute; 35, 36; 35, 37; 36, identifier:build; 37, identifier:__doc__; 38, expression_statement; 38, 39; 39, call; 39, 40; 39, 43; 40, attribute; 40, 41; 40, 42; 41, identifier:p; 42, identifier:add_option; 43, argument_list; 43, 44; 43, 45; 43, 48; 43, 51; 44, string:"--nodedup"; 45, keyword_argument; 45, 46; 45, 47; 46, identifier:default; 47, False; 48, keyword_argument; 48, 49; 48, 50; 49, identifier:action; 50, string:"store_true"; 51, keyword_argument; 51, 52; 51, 53; 52, identifier:help; 53, string:"Do not deduplicate [default: deduplicate]"; 54, expression_statement; 54, 55; 55, assignment; 55, 56; 55, 59; 56, pattern_list; 56, 57; 56, 58; 57, identifier:opts; 58, identifier:args; 59, call; 59, 60; 59, 63; 60, attribute; 60, 61; 60, 62; 61, identifier:p; 62, identifier:parse_args; 63, argument_list; 63, 64; 64, identifier:args; 65, if_statement; 65, 66; 65, 72; 66, comparison_operator:!=; 66, 67; 66, 71; 67, call; 67, 68; 67, 69; 68, identifier:len; 69, argument_list; 69, 70; 70, identifier:args; 71, integer:3; 72, block; 72, 73; 73, expression_statement; 73, 74; 74, call; 74, 75; 74, 78; 75, attribute; 75, 76; 75, 77; 76, identifier:sys; 77, identifier:exit; 78, argument_list; 78, 79; 79, not_operator; 79, 80; 80, call; 80, 81; 80, 84; 81, attribute; 81, 82; 81, 83; 82, identifier:p; 83, identifier:print_help; 84, argument_list; 85, expression_statement; 85, 86; 86, assignment; 86, 87; 86, 91; 87, pattern_list; 87, 88; 87, 89; 87, 90; 88, identifier:fastafile; 89, identifier:bacteria; 90, identifier:pf; 91, identifier:args; 92, expression_statement; 92, 93; 93, assignment; 93, 94; 93, 95; 94, identifier:dd; 95, conditional_expression:if; 95, 96; 95, 102; 95, 103; 95, 107; 96, call; 96, 97; 96, 98; 97, identifier:deduplicate; 98, argument_list; 98, 99; 99, list:[fastafile, "--pctid=100"]; 99, 100; 99, 101; 100, identifier:fastafile; 101, string:"--pctid=100"; 102, line_continuation:\; 103, not_operator; 103, 104; 104, attribute; 104, 105; 104, 106; 105, identifier:opts; 106, identifier:nodedup; 107, identifier:fastafile; 108, expression_statement; 108, 109; 109, assignment; 109, 110; 109, 111; 110, identifier:screenfasta; 111, call; 111, 112; 111, 113; 112, identifier:screen; 113, argument_list; 113, 114; 114, list:[dd, bacteria]; 114, 115; 114, 116; 115, identifier:dd; 116, identifier:bacteria; 117, expression_statement; 117, 118; 118, assignment; 118, 119; 118, 120; 119, identifier:tidyfasta; 120, call; 120, 121; 120, 122; 121, identifier:mask; 122, argument_list; 122, 123; 123, list:[screenfasta]; 123, 124; 124, identifier:screenfasta; 125, expression_statement; 125, 126; 126, assignment; 126, 127; 126, 128; 127, identifier:sortedfasta; 128, call; 128, 129; 128, 130; 129, identifier:sort; 130, argument_list; 130, 131; 131, list:[tidyfasta, "--sizes"]; 131, 132; 131, 133; 132, identifier:tidyfasta; 133, string:"--sizes"; 134, expression_statement; 134, 135; 135, assignment; 135, 136; 135, 137; 136, identifier:scaffoldfasta; 137, binary_operator:+; 137, 138; 137, 139; 138, identifier:pf; 139, string:".assembly.fasta"; 140, expression_statement; 140, 141; 141, call; 141, 142; 141, 143; 142, identifier:format; 143, argument_list; 143, 144; 144, list:[sortedfasta, scaffoldfasta, "--prefix=scaffold_", "--sequential"]; 144, 145; 144, 146; 144, 147; 144, 148; 145, identifier:sortedfasta; 146, identifier:scaffoldfasta; 147, string:"--prefix=scaffold_"; 148, string:"--sequential"; 149, expression_statement; 149, 150; 150, assignment; 150, 151; 150, 152; 151, identifier:gapsplitfasta; 152, binary_operator:+; 152, 153; 152, 154; 153, identifier:pf; 154, string:".gapSplit.fasta"; 155, expression_statement; 155, 156; 156, assignment; 156, 157; 156, 158; 157, identifier:cmd; 158, call; 158, 159; 158, 162; 159, attribute; 159, 160; 159, 161; 160, string:"gapSplit -minGap=10 {0} {1}"; 161, identifier:format; 162, argument_list; 162, 163; 162, 164; 163, identifier:scaffoldfasta; 164, identifier:gapsplitfasta; 165, expression_statement; 165, 166; 166, call; 166, 167; 166, 168; 167, identifier:sh; 168, argument_list; 168, 169; 169, identifier:cmd; 170, expression_statement; 170, 171; 171, assignment; 171, 172; 171, 173; 172, identifier:contigsfasta; 173, binary_operator:+; 173, 174; 173, 175; 174, identifier:pf; 175, string:".contigs.fasta"; 176, expression_statement; 176, 177; 177, call; 177, 178; 177, 179; 178, identifier:format; 179, argument_list; 179, 180; 180, list:[gapsplitfasta, contigsfasta, "--prefix=contig_", "--sequential"]; 180, 181; 180, 182; 180, 183; 180, 184; 181, identifier:gapsplitfasta; 182, identifier:contigsfasta; 183, string:"--prefix=contig_"; 184, string:"--sequential"
def build(args): """ %prog build current.fasta Bacteria_Virus.fasta prefix Build assembly files after a set of clean-ups: 1. Use cdhit (100%) to remove duplicate scaffolds 2. Screen against the bacteria and virus database (remove scaffolds 95% id, 50% cov) 3. Mask matches to UniVec_Core 4. Sort by decreasing scaffold sizes 5. Rename the scaffolds sequentially 6. Build the contigs by splitting scaffolds at gaps 7. Rename the contigs sequentially """ from jcvi.apps.cdhit import deduplicate from jcvi.apps.vecscreen import mask from jcvi.formats.fasta import sort p = OptionParser(build.__doc__) p.add_option("--nodedup", default=False, action="store_true", help="Do not deduplicate [default: deduplicate]") opts, args = p.parse_args(args) if len(args) != 3: sys.exit(not p.print_help()) fastafile, bacteria, pf = args dd = deduplicate([fastafile, "--pctid=100"]) \ if not opts.nodedup else fastafile screenfasta = screen([dd, bacteria]) tidyfasta = mask([screenfasta]) sortedfasta = sort([tidyfasta, "--sizes"]) scaffoldfasta = pf + ".assembly.fasta" format([sortedfasta, scaffoldfasta, "--prefix=scaffold_", "--sequential"]) gapsplitfasta = pf + ".gapSplit.fasta" cmd = "gapSplit -minGap=10 {0} {1}".format(scaffoldfasta, gapsplitfasta) sh(cmd) contigsfasta = pf + ".contigs.fasta" format([gapsplitfasta, contigsfasta, "--prefix=contig_", "--sequential"])
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:sort; 3, parameters; 3, 4; 4, identifier:args; 5, block; 5, 6; 5, 8; 5, 13; 6, expression_statement; 6, 7; 7, comment; 8, import_statement; 8, 9; 9, dotted_name; 9, 10; 9, 11; 9, 12; 10, identifier:jcvi; 11, identifier:formats; 12, identifier:blast; 13, return_statement; 13, 14; 14, call; 14, 15; 14, 22; 15, attribute; 15, 16; 15, 21; 16, attribute; 16, 17; 16, 20; 17, attribute; 17, 18; 17, 19; 18, identifier:jcvi; 19, identifier:formats; 20, identifier:blast; 21, identifier:sort; 22, argument_list; 22, 23; 23, binary_operator:+; 23, 24; 23, 25; 24, identifier:args; 25, list:["--coords"]; 25, 26; 26, string:"--coords"
def sort(args): """ %prog sort coordsfile Sort coordsfile based on query or ref. """ import jcvi.formats.blast return jcvi.formats.blast.sort(args + ["--coords"])
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:coverage; 3, parameters; 3, 4; 4, identifier:args; 5, block; 5, 6; 5, 8; 5, 17; 5, 36; 5, 52; 5, 58; 5, 69; 5, 89; 5, 95; 5, 101; 5, 129; 5, 141; 5, 150; 5, 160; 5, 217; 5, 223; 5, 238; 5, 245; 5, 255; 5, 285; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:p; 11, call; 11, 12; 11, 13; 12, identifier:OptionParser; 13, argument_list; 13, 14; 14, attribute; 14, 15; 14, 16; 15, identifier:coverage; 16, identifier:__doc__; 17, expression_statement; 17, 18; 18, call; 18, 19; 18, 22; 19, attribute; 19, 20; 19, 21; 20, identifier:p; 21, identifier:add_option; 22, argument_list; 22, 23; 22, 24; 22, 27; 22, 33; 23, string:"--format"; 24, keyword_argument; 24, 25; 24, 26; 25, identifier:default; 26, string:"bigwig"; 27, keyword_argument; 27, 28; 27, 29; 28, identifier:choices; 29, tuple; 29, 30; 29, 31; 29, 32; 30, string:"bedgraph"; 31, string:"bigwig"; 32, string:"coverage"; 33, keyword_argument; 33, 34; 33, 35; 34, identifier:help; 35, string:"Output format"; 36, expression_statement; 36, 37; 37, call; 37, 38; 37, 41; 38, attribute; 38, 39; 38, 40; 39, identifier:p; 40, identifier:add_option; 41, argument_list; 41, 42; 41, 43; 41, 46; 41, 49; 42, string:"--nosort"; 43, keyword_argument; 43, 44; 43, 45; 44, identifier:default; 45, False; 46, keyword_argument; 46, 47; 46, 48; 47, identifier:action; 48, string:"store_true"; 49, keyword_argument; 49, 50; 49, 51; 50, identifier:help; 51, string:"Do not sort BAM"; 52, expression_statement; 52, 53; 53, call; 53, 54; 53, 57; 54, attribute; 54, 55; 54, 56; 55, identifier:p; 56, identifier:set_outfile; 57, argument_list; 58, expression_statement; 58, 59; 59, assignment; 59, 60; 59, 63; 60, pattern_list; 60, 61; 60, 62; 61, identifier:opts; 62, identifier:args; 63, call; 63, 64; 63, 67; 64, attribute; 64, 65; 64, 66; 65, identifier:p; 66, identifier:parse_args; 67, argument_list; 67, 68; 68, identifier:args; 69, if_statement; 69, 70; 69, 76; 70, comparison_operator:!=; 70, 71; 70, 75; 71, call; 71, 72; 71, 73; 72, identifier:len; 73, argument_list; 73, 74; 74, identifier:args; 75, integer:2; 76, block; 76, 77; 77, expression_statement; 77, 78; 78, call; 78, 79; 78, 82; 79, attribute; 79, 80; 79, 81; 80, identifier:sys; 81, identifier:exit; 82, argument_list; 82, 83; 83, not_operator; 83, 84; 84, call; 84, 85; 84, 88; 85, attribute; 85, 86; 85, 87; 86, identifier:p; 87, identifier:print_help; 88, argument_list; 89, expression_statement; 89, 90; 90, assignment; 90, 91; 90, 94; 91, pattern_list; 91, 92; 91, 93; 92, identifier:fastafile; 93, identifier:bamfile; 94, identifier:args; 95, expression_statement; 95, 96; 96, assignment; 96, 97; 96, 98; 97, identifier:format; 98, attribute; 98, 99; 98, 100; 99, identifier:opts; 100, identifier:format; 101, if_statement; 101, 102; 101, 105; 101, 113; 102, attribute; 102, 103; 102, 104; 103, identifier:opts; 104, identifier:nosort; 105, block; 105, 106; 106, expression_statement; 106, 107; 107, call; 107, 108; 107, 111; 108, attribute; 108, 109; 108, 110; 109, identifier:logging; 110, identifier:debug; 111, argument_list; 111, 112; 112, string:"BAM sorting skipped"; 113, else_clause; 113, 114; 114, block; 114, 115; 115, expression_statement; 115, 116; 116, assignment; 116, 117; 116, 118; 117, identifier:bamfile; 118, call; 118, 119; 118, 120; 119, identifier:index; 120, argument_list; 120, 121; 121, list:[bamfile, "--fasta={0}".format(fastafile)]; 121, 122; 121, 123; 122, identifier:bamfile; 123, call; 123, 124; 123, 127; 124, attribute; 124, 125; 124, 126; 125, string:"--fasta={0}"; 126, identifier:format; 127, argument_list; 127, 128; 128, identifier:fastafile; 129, expression_statement; 129, 130; 130, assignment; 130, 131; 130, 132; 131, identifier:pf; 132, subscript; 132, 133; 132, 140; 133, call; 133, 134; 133, 137; 134, attribute; 134, 135; 134, 136; 135, identifier:bamfile; 136, identifier:rsplit; 137, argument_list; 137, 138; 137, 139; 138, string:"."; 139, integer:2; 140, integer:0; 141, expression_statement; 141, 142; 142, assignment; 142, 143; 142, 144; 143, identifier:sizesfile; 144, attribute; 144, 145; 144, 149; 145, call; 145, 146; 145, 147; 146, identifier:Sizes; 147, argument_list; 147, 148; 148, identifier:fastafile; 149, identifier:filename; 150, expression_statement; 150, 151; 151, assignment; 151, 152; 151, 153; 152, identifier:cmd; 153, call; 153, 154; 153, 157; 154, attribute; 154, 155; 154, 156; 155, string:"genomeCoverageBed -ibam {0} -g {1}"; 156, identifier:format; 157, argument_list; 157, 158; 157, 159; 158, identifier:bamfile; 159, identifier:sizesfile; 160, if_statement; 160, 161; 160, 166; 161, comparison_operator:in; 161, 162; 161, 163; 162, identifier:format; 163, tuple; 163, 164; 163, 165; 164, string:"bedgraph"; 165, string:"bigwig"; 166, block; 166, 167; 166, 171; 166, 177; 166, 185; 166, 192; 166, 198; 166, 210; 166, 215; 167, expression_statement; 167, 168; 168, augmented_assignment:+=; 168, 169; 168, 170; 169, identifier:cmd; 170, string:" -bg"; 171, expression_statement; 171, 172; 172, assignment; 172, 173; 172, 174; 173, identifier:bedgraphfile; 174, binary_operator:+; 174, 175; 174, 176; 175, identifier:pf; 176, string:".bedgraph"; 177, expression_statement; 177, 178; 178, call; 178, 179; 178, 180; 179, identifier:sh; 180, argument_list; 180, 181; 180, 182; 181, identifier:cmd; 182, keyword_argument; 182, 183; 182, 184; 183, identifier:outfile; 184, identifier:bedgraphfile; 185, if_statement; 185, 186; 185, 189; 186, comparison_operator:==; 186, 187; 186, 188; 187, identifier:format; 188, string:"bedgraph"; 189, block; 189, 190; 190, return_statement; 190, 191; 191, identifier:bedgraphfile; 192, expression_statement; 192, 193; 193, assignment; 193, 194; 193, 195; 194, identifier:bigwigfile; 195, binary_operator:+; 195, 196; 195, 197; 196, identifier:pf; 197, string:".bigwig"; 198, expression_statement; 198, 199; 199, assignment; 199, 200; 199, 201; 200, identifier:cmd; 201, call; 201, 202; 201, 206; 202, attribute; 202, 203; 202, 204; 202, 205; 203, string:"bedGraphToBigWig {0} {1} {2}"; 204, line_continuation:\; 205, identifier:format; 206, argument_list; 206, 207; 206, 208; 206, 209; 207, identifier:bedgraphfile; 208, identifier:sizesfile; 209, identifier:bigwigfile; 210, expression_statement; 210, 211; 211, call; 211, 212; 211, 213; 212, identifier:sh; 213, argument_list; 213, 214; 214, identifier:cmd; 215, return_statement; 215, 216; 216, identifier:bigwigfile; 217, expression_statement; 217, 218; 218, assignment; 218, 219; 218, 220; 219, identifier:coveragefile; 220, binary_operator:+; 220, 221; 220, 222; 221, identifier:pf; 222, string:".coverage"; 223, if_statement; 223, 224; 223, 229; 224, call; 224, 225; 224, 226; 225, identifier:need_update; 226, argument_list; 226, 227; 226, 228; 227, identifier:fastafile; 228, identifier:coveragefile; 229, block; 229, 230; 230, expression_statement; 230, 231; 231, call; 231, 232; 231, 233; 232, identifier:sh; 233, argument_list; 233, 234; 233, 235; 234, identifier:cmd; 235, keyword_argument; 235, 236; 235, 237; 236, identifier:outfile; 237, identifier:coveragefile; 238, expression_statement; 238, 239; 239, assignment; 239, 240; 239, 241; 240, identifier:gcf; 241, call; 241, 242; 241, 243; 242, identifier:GenomeCoverageFile; 243, argument_list; 243, 244; 244, identifier:coveragefile; 245, expression_statement; 245, 246; 246, assignment; 246, 247; 246, 248; 247, identifier:fw; 248, call; 248, 249; 248, 250; 249, identifier:must_open; 250, argument_list; 250, 251; 250, 254; 251, attribute; 251, 252; 251, 253; 252, identifier:opts; 253, identifier:outfile; 254, string:"w"; 255, for_statement; 255, 256; 255, 259; 255, 264; 256, pattern_list; 256, 257; 256, 258; 257, identifier:seqid; 258, identifier:cov; 259, call; 259, 260; 259, 263; 260, attribute; 260, 261; 260, 262; 261, identifier:gcf; 262, identifier:iter_coverage_seqid; 263, argument_list; 264, block; 264, 265; 265, expression_statement; 265, 266; 266, call; 266, 267; 266, 268; 267, identifier:print; 268, argument_list; 268, 269; 268, 282; 269, call; 269, 270; 269, 273; 270, attribute; 270, 271; 270, 272; 271, string:"\t"; 272, identifier:join; 273, argument_list; 273, 274; 274, tuple; 274, 275; 274, 276; 275, identifier:seqid; 276, call; 276, 277; 276, 280; 277, attribute; 277, 278; 277, 279; 278, string:"{0:.1f}"; 279, identifier:format; 280, argument_list; 280, 281; 281, identifier:cov; 282, keyword_argument; 282, 283; 282, 284; 283, identifier:file; 284, identifier:fw; 285, expression_statement; 285, 286; 286, call; 286, 287; 286, 290; 287, attribute; 287, 288; 287, 289; 288, identifier:fw; 289, identifier:close; 290, argument_list
def coverage(args): """ %prog coverage fastafile bamfile Calculate coverage for BAM file. BAM file will be sorted unless with --nosort. """ p = OptionParser(coverage.__doc__) p.add_option("--format", default="bigwig", choices=("bedgraph", "bigwig", "coverage"), help="Output format") p.add_option("--nosort", default=False, action="store_true", help="Do not sort BAM") p.set_outfile() opts, args = p.parse_args(args) if len(args) != 2: sys.exit(not p.print_help()) fastafile, bamfile = args format = opts.format if opts.nosort: logging.debug("BAM sorting skipped") else: bamfile = index([bamfile, "--fasta={0}".format(fastafile)]) pf = bamfile.rsplit(".", 2)[0] sizesfile = Sizes(fastafile).filename cmd = "genomeCoverageBed -ibam {0} -g {1}".format(bamfile, sizesfile) if format in ("bedgraph", "bigwig"): cmd += " -bg" bedgraphfile = pf + ".bedgraph" sh(cmd, outfile=bedgraphfile) if format == "bedgraph": return bedgraphfile bigwigfile = pf + ".bigwig" cmd = "bedGraphToBigWig {0} {1} {2}".\ format(bedgraphfile, sizesfile, bigwigfile) sh(cmd) return bigwigfile coveragefile = pf + ".coverage" if need_update(fastafile, coveragefile): sh(cmd, outfile=coveragefile) gcf = GenomeCoverageFile(coveragefile) fw = must_open(opts.outfile, "w") for seqid, cov in gcf.iter_coverage_seqid(): print("\t".join((seqid, "{0:.1f}".format(cov))), file=fw) fw.close()
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:_number_finder; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:s; 5, identifier:regex; 6, identifier:numconv; 7, block; 7, 8; 7, 10; 7, 11; 7, 20; 7, 33; 7, 34; 7, 41; 7, 68; 7, 69; 7, 70; 8, expression_statement; 8, 9; 9, comment; 10, comment; 11, expression_statement; 11, 12; 12, assignment; 12, 13; 12, 14; 13, identifier:s; 14, call; 14, 15; 14, 18; 15, attribute; 15, 16; 15, 17; 16, identifier:regex; 17, identifier:split; 18, argument_list; 18, 19; 19, identifier:s; 20, if_statement; 20, 21; 20, 27; 21, comparison_operator:==; 21, 22; 21, 26; 22, call; 22, 23; 22, 24; 23, identifier:len; 24, argument_list; 24, 25; 25, identifier:s; 26, integer:1; 27, block; 27, 28; 28, return_statement; 28, 29; 29, call; 29, 30; 29, 31; 30, identifier:tuple; 31, argument_list; 31, 32; 32, identifier:s; 33, comment; 34, expression_statement; 34, 35; 35, assignment; 35, 36; 35, 37; 36, identifier:s; 37, call; 37, 38; 37, 39; 38, identifier:remove_empty; 39, argument_list; 39, 40; 40, identifier:s; 41, for_statement; 41, 42; 41, 43; 41, 50; 42, identifier:i; 43, call; 43, 44; 43, 45; 44, identifier:range; 45, argument_list; 45, 46; 46, call; 46, 47; 46, 48; 47, identifier:len; 48, argument_list; 48, 49; 49, identifier:s; 50, block; 50, 51; 51, try_statement; 51, 52; 51, 64; 52, block; 52, 53; 53, expression_statement; 53, 54; 54, assignment; 54, 55; 54, 58; 55, subscript; 55, 56; 55, 57; 56, identifier:s; 57, identifier:i; 58, call; 58, 59; 58, 60; 59, identifier:numconv; 60, argument_list; 60, 61; 61, subscript; 61, 62; 61, 63; 62, identifier:s; 63, identifier:i; 64, except_clause; 64, 65; 64, 66; 65, identifier:ValueError; 66, block; 66, 67; 67, pass_statement; 68, comment; 69, comment; 70, if_statement; 70, 71; 70, 81; 70, 87; 71, not_operator; 71, 72; 72, call; 72, 73; 72, 74; 73, identifier:isinstance; 74, argument_list; 74, 75; 74, 78; 75, subscript; 75, 76; 75, 77; 76, identifier:s; 77, integer:0; 78, attribute; 78, 79; 78, 80; 79, identifier:six; 80, identifier:string_types; 81, block; 81, 82; 82, return_statement; 82, 83; 83, binary_operator:+; 83, 84; 83, 86; 84, list:['']; 84, 85; 85, string:''; 86, identifier:s; 87, else_clause; 87, 88; 88, block; 88, 89; 89, return_statement; 89, 90; 90, identifier:s
def _number_finder(s, regex, numconv): """Helper to split numbers""" # Split. If there are no splits, return now s = regex.split(s) if len(s) == 1: return tuple(s) # Now convert the numbers to numbers, and leave strings as strings s = remove_empty(s) for i in range(len(s)): try: s[i] = numconv(s[i]) except ValueError: pass # If the list begins with a number, lead with an empty string. # This is used to get around the "unorderable types" issue. if not isinstance(s[0], six.string_types): return [''] + s else: return s
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 20; 2, function_name:index_natsorted; 3, parameters; 3, 4; 3, 5; 3, 11; 3, 14; 3, 17; 4, identifier:seq; 5, default_parameter; 5, 6; 5, 7; 6, identifier:key; 7, lambda; 7, 8; 7, 10; 8, lambda_parameters; 8, 9; 9, identifier:x; 10, identifier:x; 11, default_parameter; 11, 12; 11, 13; 12, identifier:number_type; 13, identifier:float; 14, default_parameter; 14, 15; 14, 16; 15, identifier:signed; 16, True; 17, default_parameter; 17, 18; 17, 19; 18, identifier:exp; 19, True; 20, block; 20, 21; 20, 23; 20, 28; 20, 35; 20, 36; 20, 61; 20, 88; 21, expression_statement; 21, 22; 22, comment; 23, import_from_statement; 23, 24; 23, 26; 24, dotted_name; 24, 25; 25, identifier:operator; 26, dotted_name; 26, 27; 27, identifier:itemgetter; 28, expression_statement; 28, 29; 29, assignment; 29, 30; 29, 31; 30, identifier:item1; 31, call; 31, 32; 31, 33; 32, identifier:itemgetter; 33, argument_list; 33, 34; 34, integer:1; 35, comment; 36, expression_statement; 36, 37; 37, assignment; 37, 38; 37, 39; 38, identifier:index_seq_pair; 39, list_comprehension; 39, 40; 39, 46; 40, list:[x, key(y)]; 40, 41; 40, 42; 41, identifier:x; 42, call; 42, 43; 42, 44; 43, identifier:key; 44, argument_list; 44, 45; 45, identifier:y; 46, for_in_clause; 46, 47; 46, 50; 47, pattern_list; 47, 48; 47, 49; 48, identifier:x; 49, identifier:y; 50, call; 50, 51; 50, 52; 51, identifier:zip; 52, argument_list; 52, 53; 52, 60; 53, call; 53, 54; 53, 55; 54, identifier:range; 55, argument_list; 55, 56; 56, call; 56, 57; 56, 58; 57, identifier:len; 58, argument_list; 58, 59; 59, identifier:seq; 60, identifier:seq; 61, expression_statement; 61, 62; 62, call; 62, 63; 62, 66; 63, attribute; 63, 64; 63, 65; 64, identifier:index_seq_pair; 65, identifier:sort; 66, argument_list; 66, 67; 67, keyword_argument; 67, 68; 67, 69; 68, identifier:key; 69, lambda; 69, 70; 69, 72; 70, lambda_parameters; 70, 71; 71, identifier:x; 72, call; 72, 73; 72, 74; 73, identifier:natsort_key; 74, argument_list; 74, 75; 74, 79; 74, 82; 74, 85; 75, call; 75, 76; 75, 77; 76, identifier:item1; 77, argument_list; 77, 78; 78, identifier:x; 79, keyword_argument; 79, 80; 79, 81; 80, identifier:number_type; 81, identifier:number_type; 82, keyword_argument; 82, 83; 82, 84; 83, identifier:signed; 84, identifier:signed; 85, keyword_argument; 85, 86; 85, 87; 86, identifier:exp; 87, identifier:exp; 88, return_statement; 88, 89; 89, list_comprehension; 89, 90; 89, 93; 90, subscript; 90, 91; 90, 92; 91, identifier:x; 92, integer:0; 93, for_in_clause; 93, 94; 93, 95; 94, identifier:x; 95, identifier:index_seq_pair
def index_natsorted(seq, key=lambda x: x, number_type=float, signed=True, exp=True): """\ Sorts a sequence naturally, but returns a list of sorted the indeces and not the sorted list. >>> a = ['num3', 'num5', 'num2'] >>> b = ['foo', 'bar', 'baz'] >>> index = index_natsorted(a) >>> index [2, 0, 1] >>> # Sort both lists by the sort order of a >>> [a[i] for i in index] ['num2', 'num3', 'num5'] >>> [b[i] for i in index] ['baz', 'foo', 'bar'] >>> c = [('a', 'num3'), ('b', 'num5'), ('c', 'num2')] >>> from operator import itemgetter >>> index_natsorted(c, key=itemgetter(1)) [2, 0, 1] """ from operator import itemgetter item1 = itemgetter(1) # Pair the index and sequence together, then sort by index_seq_pair = [[x, key(y)] for x, y in zip(range(len(seq)), seq)] index_seq_pair.sort(key=lambda x: natsort_key(item1(x), number_type=number_type, signed=signed, exp=exp)) return [x[0] for x in index_seq_pair]
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:sort; 3, parameters; 3, 4; 4, identifier:args; 5, block; 5, 6; 5, 8; 5, 17; 5, 37; 5, 56; 5, 72; 5, 81; 5, 87; 5, 98; 5, 118; 5, 123; 5, 129; 5, 139; 5, 145; 5, 184; 5, 194; 5, 198; 5, 214; 5, 223; 5, 234; 5, 248; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:p; 11, call; 11, 12; 11, 13; 12, identifier:OptionParser; 13, argument_list; 13, 14; 14, attribute; 14, 15; 14, 16; 15, identifier:sort; 16, identifier:__doc__; 17, expression_statement; 17, 18; 18, call; 18, 19; 18, 22; 19, attribute; 19, 20; 19, 21; 20, identifier:p; 21, identifier:add_option; 22, argument_list; 22, 23; 22, 24; 22, 25; 22, 28; 22, 31; 22, 34; 23, string:"-i"; 24, string:"--inplace"; 25, keyword_argument; 25, 26; 25, 27; 26, identifier:dest; 27, string:"inplace"; 28, keyword_argument; 28, 29; 28, 30; 29, identifier:default; 30, False; 31, keyword_argument; 31, 32; 31, 33; 32, identifier:action; 33, string:"store_true"; 34, keyword_argument; 34, 35; 34, 36; 35, identifier:help; 36, string:"Sort bed file in place [default: %default]"; 37, expression_statement; 37, 38; 38, call; 38, 39; 38, 42; 39, attribute; 39, 40; 39, 41; 40, identifier:p; 41, identifier:add_option; 42, argument_list; 42, 43; 42, 44; 42, 47; 42, 50; 42, 53; 43, string:"-u"; 44, keyword_argument; 44, 45; 44, 46; 45, identifier:dest; 46, string:"unique"; 47, keyword_argument; 47, 48; 47, 49; 48, identifier:default; 49, False; 50, keyword_argument; 50, 51; 50, 52; 51, identifier:action; 52, string:"store_true"; 53, keyword_argument; 53, 54; 53, 55; 54, identifier:help; 55, string:"Uniqify the bed file"; 56, expression_statement; 56, 57; 57, call; 57, 58; 57, 61; 58, attribute; 58, 59; 58, 60; 59, identifier:p; 60, identifier:add_option; 61, argument_list; 61, 62; 61, 63; 61, 66; 61, 69; 62, string:"--accn"; 63, keyword_argument; 63, 64; 63, 65; 64, identifier:default; 65, False; 66, keyword_argument; 66, 67; 66, 68; 67, identifier:action; 68, string:"store_true"; 69, keyword_argument; 69, 70; 69, 71; 70, identifier:help; 71, string:"Sort based on the accessions [default: %default]"; 72, expression_statement; 72, 73; 73, call; 73, 74; 73, 77; 74, attribute; 74, 75; 74, 76; 75, identifier:p; 76, identifier:set_outfile; 77, argument_list; 77, 78; 78, keyword_argument; 78, 79; 78, 80; 79, identifier:outfile; 80, None; 81, expression_statement; 81, 82; 82, call; 82, 83; 82, 86; 83, attribute; 83, 84; 83, 85; 84, identifier:p; 85, identifier:set_tmpdir; 86, argument_list; 87, expression_statement; 87, 88; 88, assignment; 88, 89; 88, 92; 89, pattern_list; 89, 90; 89, 91; 90, identifier:opts; 91, identifier:args; 92, call; 92, 93; 92, 96; 93, attribute; 93, 94; 93, 95; 94, identifier:p; 95, identifier:parse_args; 96, argument_list; 96, 97; 97, identifier:args; 98, if_statement; 98, 99; 98, 105; 99, comparison_operator:!=; 99, 100; 99, 104; 100, call; 100, 101; 100, 102; 101, identifier:len; 102, argument_list; 102, 103; 103, identifier:args; 104, integer:1; 105, block; 105, 106; 106, expression_statement; 106, 107; 107, call; 107, 108; 107, 111; 108, attribute; 108, 109; 108, 110; 109, identifier:sys; 110, identifier:exit; 111, argument_list; 111, 112; 112, not_operator; 112, 113; 113, call; 113, 114; 113, 117; 114, attribute; 114, 115; 114, 116; 115, identifier:p; 116, identifier:print_help; 117, argument_list; 118, expression_statement; 118, 119; 119, assignment; 119, 120; 119, 122; 120, pattern_list; 120, 121; 121, identifier:bedfile; 122, identifier:args; 123, expression_statement; 123, 124; 124, assignment; 124, 125; 124, 126; 125, identifier:inplace; 126, attribute; 126, 127; 126, 128; 127, identifier:opts; 128, identifier:inplace; 129, if_statement; 129, 130; 129, 136; 130, boolean_operator:and; 130, 131; 130, 133; 131, not_operator; 131, 132; 132, identifier:inplace; 133, comparison_operator:in; 133, 134; 133, 135; 134, string:".sorted."; 135, identifier:bedfile; 136, block; 136, 137; 137, return_statement; 137, 138; 138, identifier:bedfile; 139, expression_statement; 139, 140; 140, assignment; 140, 141; 140, 142; 141, identifier:sortedbed; 142, attribute; 142, 143; 142, 144; 143, identifier:opts; 144, identifier:outfile; 145, if_statement; 145, 146; 145, 147; 145, 152; 146, identifier:inplace; 147, block; 147, 148; 148, expression_statement; 148, 149; 149, assignment; 149, 150; 149, 151; 150, identifier:sortedbed; 151, identifier:bedfile; 152, elif_clause; 152, 153; 152, 158; 153, comparison_operator:is; 153, 154; 153, 157; 154, attribute; 154, 155; 154, 156; 155, identifier:opts; 156, identifier:outfile; 157, None; 158, block; 158, 159; 158, 176; 159, expression_statement; 159, 160; 160, assignment; 160, 161; 160, 164; 161, pattern_list; 161, 162; 161, 163; 162, identifier:pf; 163, identifier:sf; 164, call; 164, 165; 164, 173; 165, attribute; 165, 166; 165, 172; 166, call; 166, 167; 166, 170; 167, attribute; 167, 168; 167, 169; 168, identifier:op; 169, identifier:basename; 170, argument_list; 170, 171; 171, identifier:bedfile; 172, identifier:rsplit; 173, argument_list; 173, 174; 173, 175; 174, string:"."; 175, integer:1; 176, expression_statement; 176, 177; 177, assignment; 177, 178; 177, 179; 178, identifier:sortedbed; 179, binary_operator:+; 179, 180; 179, 183; 180, binary_operator:+; 180, 181; 180, 182; 181, identifier:pf; 182, string:".sorted."; 183, identifier:sf; 184, expression_statement; 184, 185; 185, assignment; 185, 186; 185, 187; 186, identifier:sortopt; 187, conditional_expression:if; 187, 188; 187, 189; 187, 193; 188, string:"-k1,1 -k2,2n -k3,3n -k4,4"; 189, not_operator; 189, 190; 190, attribute; 190, 191; 190, 192; 191, identifier:opts; 192, identifier:accn; 193, string:"-k4,4 -k1,1 -k2,2n -k3,3n"; 194, expression_statement; 194, 195; 195, assignment; 195, 196; 195, 197; 196, identifier:cmd; 197, string:"sort"; 198, if_statement; 198, 199; 198, 202; 199, attribute; 199, 200; 199, 201; 200, identifier:opts; 201, identifier:tmpdir; 202, block; 202, 203; 203, expression_statement; 203, 204; 204, augmented_assignment:+=; 204, 205; 204, 206; 205, identifier:cmd; 206, call; 206, 207; 206, 210; 207, attribute; 207, 208; 207, 209; 208, string:" -T {0}"; 209, identifier:format; 210, argument_list; 210, 211; 211, attribute; 211, 212; 211, 213; 212, identifier:opts; 213, identifier:tmpdir; 214, if_statement; 214, 215; 214, 218; 215, attribute; 215, 216; 215, 217; 216, identifier:opts; 217, identifier:unique; 218, block; 218, 219; 219, expression_statement; 219, 220; 220, augmented_assignment:+=; 220, 221; 220, 222; 221, identifier:cmd; 222, string:" -u"; 223, expression_statement; 223, 224; 224, augmented_assignment:+=; 224, 225; 224, 226; 225, identifier:cmd; 226, call; 226, 227; 226, 230; 227, attribute; 227, 228; 227, 229; 228, string:" {0} {1} -o {2}"; 229, identifier:format; 230, argument_list; 230, 231; 230, 232; 230, 233; 231, identifier:sortopt; 232, identifier:bedfile; 233, identifier:sortedbed; 234, if_statement; 234, 235; 234, 242; 235, boolean_operator:or; 235, 236; 235, 237; 236, identifier:inplace; 237, call; 237, 238; 237, 239; 238, identifier:need_update; 239, argument_list; 239, 240; 239, 241; 240, identifier:bedfile; 241, identifier:sortedbed; 242, block; 242, 243; 243, expression_statement; 243, 244; 244, call; 244, 245; 244, 246; 245, identifier:sh; 246, argument_list; 246, 247; 247, identifier:cmd; 248, return_statement; 248, 249; 249, identifier:sortedbed
def sort(args): """ %prog sort bedfile Sort bed file to have ascending order of seqid, then start. It uses the `sort` command. """ p = OptionParser(sort.__doc__) p.add_option("-i", "--inplace", dest="inplace", default=False, action="store_true", help="Sort bed file in place [default: %default]") p.add_option("-u", dest="unique", default=False, action="store_true", help="Uniqify the bed file") p.add_option("--accn", default=False, action="store_true", help="Sort based on the accessions [default: %default]") p.set_outfile(outfile=None) p.set_tmpdir() opts, args = p.parse_args(args) if len(args) != 1: sys.exit(not p.print_help()) bedfile, = args inplace = opts.inplace if not inplace and ".sorted." in bedfile: return bedfile sortedbed = opts.outfile if inplace: sortedbed = bedfile elif opts.outfile is None: pf, sf = op.basename(bedfile).rsplit(".", 1) sortedbed = pf + ".sorted." + sf sortopt = "-k1,1 -k2,2n -k3,3n -k4,4" if not opts.accn else \ "-k4,4 -k1,1 -k2,2n -k3,3n" cmd = "sort" if opts.tmpdir: cmd += " -T {0}".format(opts.tmpdir) if opts.unique: cmd += " -u" cmd += " {0} {1} -o {2}".format(sortopt, bedfile, sortedbed) if inplace or need_update(bedfile, sortedbed): sh(cmd) return sortedbed
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:by_image_seq; 3, parameters; 3, 4; 3, 5; 4, identifier:blocks; 5, identifier:image_seq; 6, block; 6, 7; 6, 9; 7, expression_statement; 7, 8; 8, comment; 9, return_statement; 9, 10; 10, call; 10, 11; 10, 12; 11, identifier:list; 12, argument_list; 12, 13; 13, call; 13, 14; 13, 15; 14, identifier:filter; 15, argument_list; 15, 16; 15, 28; 16, lambda; 16, 17; 16, 19; 17, lambda_parameters; 17, 18; 18, identifier:block; 19, comparison_operator:==; 19, 20; 19, 27; 20, attribute; 20, 21; 20, 26; 21, attribute; 21, 22; 21, 25; 22, subscript; 22, 23; 22, 24; 23, identifier:blocks; 24, identifier:block; 25, identifier:ec_hdr; 26, identifier:image_seq; 27, identifier:image_seq; 28, identifier:blocks
def by_image_seq(blocks, image_seq): """Filter blocks to return only those associated with the provided image_seq number. Argument: List:blocks -- List of block objects to sort. Int:image_seq -- image_seq number found in ec_hdr. Returns: List -- List of block indexes matching image_seq number. """ return list(filter(lambda block: blocks[block].ec_hdr.image_seq == image_seq, blocks))
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:by_vol_id; 3, parameters; 3, 4; 3, 5; 4, identifier:blocks; 5, default_parameter; 5, 6; 5, 7; 6, identifier:slist; 7, None; 8, block; 8, 9; 8, 11; 8, 15; 8, 16; 8, 17; 8, 80; 9, expression_statement; 9, 10; 10, comment; 11, expression_statement; 11, 12; 12, assignment; 12, 13; 12, 14; 13, identifier:vol_blocks; 14, dictionary; 15, comment; 16, comment; 17, for_statement; 17, 18; 17, 19; 17, 20; 18, identifier:i; 19, identifier:blocks; 20, block; 20, 21; 20, 38; 20, 61; 21, if_statement; 21, 22; 21, 27; 21, 29; 22, boolean_operator:and; 22, 23; 22, 24; 23, identifier:slist; 24, comparison_operator:not; 24, 25; 24, 26; 25, identifier:i; 26, identifier:slist; 27, block; 27, 28; 28, continue_statement; 29, elif_clause; 29, 30; 29, 36; 30, not_operator; 30, 31; 31, attribute; 31, 32; 31, 35; 32, subscript; 32, 33; 32, 34; 33, identifier:blocks; 34, identifier:i; 35, identifier:is_valid; 36, block; 36, 37; 37, continue_statement; 38, if_statement; 38, 39; 38, 48; 39, comparison_operator:not; 39, 40; 39, 47; 40, attribute; 40, 41; 40, 46; 41, attribute; 41, 42; 41, 45; 42, subscript; 42, 43; 42, 44; 43, identifier:blocks; 44, identifier:i; 45, identifier:vid_hdr; 46, identifier:vol_id; 47, identifier:vol_blocks; 48, block; 48, 49; 49, expression_statement; 49, 50; 50, assignment; 50, 51; 50, 60; 51, subscript; 51, 52; 51, 53; 52, identifier:vol_blocks; 53, attribute; 53, 54; 53, 59; 54, attribute; 54, 55; 54, 58; 55, subscript; 55, 56; 55, 57; 56, identifier:blocks; 57, identifier:i; 58, identifier:vid_hdr; 59, identifier:vol_id; 60, list:[]; 61, expression_statement; 61, 62; 62, call; 62, 63; 62, 74; 63, attribute; 63, 64; 63, 73; 64, subscript; 64, 65; 64, 66; 65, identifier:vol_blocks; 66, attribute; 66, 67; 66, 72; 67, attribute; 67, 68; 67, 71; 68, subscript; 68, 69; 68, 70; 69, identifier:blocks; 70, identifier:i; 71, identifier:vid_hdr; 72, identifier:vol_id; 73, identifier:append; 74, argument_list; 74, 75; 75, attribute; 75, 76; 75, 79; 76, subscript; 76, 77; 76, 78; 77, identifier:blocks; 78, identifier:i; 79, identifier:peb_num; 80, return_statement; 80, 81; 81, identifier:vol_blocks
def by_vol_id(blocks, slist=None): """Sort blocks by volume id Arguments: Obj:blocks -- List of block objects. List:slist -- (optional) List of block indexes. Return: Dict -- blocks grouped in lists with dict key as volume id. """ vol_blocks = {} # sort block by volume # not reliable with multiple partitions (fifo) for i in blocks: if slist and i not in slist: continue elif not blocks[i].is_valid: continue if blocks[i].vid_hdr.vol_id not in vol_blocks: vol_blocks[blocks[i].vid_hdr.vol_id] = [] vol_blocks[blocks[i].vid_hdr.vol_id].append(blocks[i].peb_num) return vol_blocks
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:by_type; 3, parameters; 3, 4; 3, 5; 4, identifier:blocks; 5, default_parameter; 5, 6; 5, 7; 6, identifier:slist; 7, None; 8, block; 8, 9; 8, 11; 8, 15; 8, 19; 8, 23; 8, 27; 8, 102; 9, expression_statement; 9, 10; 10, comment; 11, expression_statement; 11, 12; 12, assignment; 12, 13; 12, 14; 13, identifier:layout; 14, list:[]; 15, expression_statement; 15, 16; 16, assignment; 16, 17; 16, 18; 17, identifier:data; 18, list:[]; 19, expression_statement; 19, 20; 20, assignment; 20, 21; 20, 22; 21, identifier:int_vol; 22, list:[]; 23, expression_statement; 23, 24; 24, assignment; 24, 25; 24, 26; 25, identifier:unknown; 26, list:[]; 27, for_statement; 27, 28; 27, 29; 27, 30; 28, identifier:i; 29, identifier:blocks; 30, block; 30, 31; 30, 39; 31, if_statement; 31, 32; 31, 37; 32, boolean_operator:and; 32, 33; 32, 34; 33, identifier:slist; 34, comparison_operator:not; 34, 35; 34, 36; 35, identifier:i; 36, identifier:slist; 37, block; 37, 38; 38, continue_statement; 39, if_statement; 39, 40; 39, 51; 39, 59; 39, 79; 39, 93; 40, boolean_operator:and; 40, 41; 40, 46; 41, attribute; 41, 42; 41, 45; 42, subscript; 42, 43; 42, 44; 43, identifier:blocks; 44, identifier:i; 45, identifier:is_vtbl; 46, attribute; 46, 47; 46, 50; 47, subscript; 47, 48; 47, 49; 48, identifier:blocks; 49, identifier:i; 50, identifier:is_valid; 51, block; 51, 52; 52, expression_statement; 52, 53; 53, call; 53, 54; 53, 57; 54, attribute; 54, 55; 54, 56; 55, identifier:layout; 56, identifier:append; 57, argument_list; 57, 58; 58, identifier:i; 59, elif_clause; 59, 60; 59, 71; 60, boolean_operator:and; 60, 61; 60, 66; 61, attribute; 61, 62; 61, 65; 62, subscript; 62, 63; 62, 64; 63, identifier:blocks; 64, identifier:i; 65, identifier:is_internal_vol; 66, attribute; 66, 67; 66, 70; 67, subscript; 67, 68; 67, 69; 68, identifier:blocks; 69, identifier:i; 70, identifier:is_valid; 71, block; 71, 72; 72, expression_statement; 72, 73; 73, call; 73, 74; 73, 77; 74, attribute; 74, 75; 74, 76; 75, identifier:int_vol; 76, identifier:append; 77, argument_list; 77, 78; 78, identifier:i; 79, elif_clause; 79, 80; 79, 85; 80, attribute; 80, 81; 80, 84; 81, subscript; 81, 82; 81, 83; 82, identifier:blocks; 83, identifier:i; 84, identifier:is_valid; 85, block; 85, 86; 86, expression_statement; 86, 87; 87, call; 87, 88; 87, 91; 88, attribute; 88, 89; 88, 90; 89, identifier:data; 90, identifier:append; 91, argument_list; 91, 92; 92, identifier:i; 93, else_clause; 93, 94; 94, block; 94, 95; 95, expression_statement; 95, 96; 96, call; 96, 97; 96, 100; 97, attribute; 97, 98; 97, 99; 98, identifier:unknown; 99, identifier:append; 100, argument_list; 100, 101; 101, identifier:i; 102, return_statement; 102, 103; 103, expression_list; 103, 104; 103, 105; 103, 106; 103, 107; 104, identifier:layout; 105, identifier:data; 106, identifier:int_vol; 107, identifier:unknown
def by_type(blocks, slist=None): """Sort blocks into layout, internal volume, data or unknown Arguments: Obj:blocks -- List of block objects. List:slist -- (optional) List of block indexes. Returns: List:layout -- List of block indexes of blocks containing the volume table records. List:data -- List of block indexes containing filesystem data. List:int_vol -- List of block indexes containing volume ids greater than UBI_INTERNAL_VOL_START that are not layout volumes. List:unknown -- List of block indexes of blocks that failed validation of crc in ed_hdr or vid_hdr. """ layout = [] data = [] int_vol = [] unknown = [] for i in blocks: if slist and i not in slist: continue if blocks[i].is_vtbl and blocks[i].is_valid: layout.append(i) elif blocks[i].is_internal_vol and blocks[i].is_valid: int_vol.append(i) elif blocks[i].is_valid: data.append(i) else: unknown.append(i) return layout, data, int_vol, unknown
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:group_pairs; 3, parameters; 3, 4; 3, 5; 4, identifier:blocks; 5, identifier:layout_blocks_list; 6, block; 6, 7; 6, 9; 6, 13; 6, 50; 6, 65; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:image_dict; 12, dictionary; 13, for_statement; 13, 14; 13, 15; 13, 16; 14, identifier:block_id; 15, identifier:layout_blocks_list; 16, block; 16, 17; 16, 27; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 20; 19, identifier:image_seq; 20, attribute; 20, 21; 20, 26; 21, attribute; 21, 22; 21, 25; 22, subscript; 22, 23; 22, 24; 23, identifier:blocks; 24, identifier:block_id; 25, identifier:ec_hdr; 26, identifier:image_seq; 27, if_statement; 27, 28; 27, 31; 27, 39; 28, comparison_operator:not; 28, 29; 28, 30; 29, identifier:image_seq; 30, identifier:image_dict; 31, block; 31, 32; 32, expression_statement; 32, 33; 33, assignment; 33, 34; 33, 37; 34, subscript; 34, 35; 34, 36; 35, identifier:image_dict; 36, identifier:image_seq; 37, list:[block_id]; 37, 38; 38, identifier:block_id; 39, else_clause; 39, 40; 40, block; 40, 41; 41, expression_statement; 41, 42; 42, call; 42, 43; 42, 48; 43, attribute; 43, 44; 43, 47; 44, subscript; 44, 45; 44, 46; 45, identifier:image_dict; 46, identifier:image_seq; 47, identifier:append; 48, argument_list; 48, 49; 49, identifier:block_id; 50, expression_statement; 50, 51; 51, call; 51, 52; 51, 53; 52, identifier:log; 53, argument_list; 53, 54; 53, 55; 54, identifier:group_pairs; 55, binary_operator:%; 55, 56; 55, 57; 56, string:'Layout blocks found at PEBs: %s'; 57, call; 57, 58; 57, 59; 58, identifier:list; 59, argument_list; 59, 60; 60, call; 60, 61; 60, 64; 61, attribute; 61, 62; 61, 63; 62, identifier:image_dict; 63, identifier:values; 64, argument_list; 65, return_statement; 65, 66; 66, call; 66, 67; 66, 68; 67, identifier:list; 68, argument_list; 68, 69; 69, call; 69, 70; 69, 73; 70, attribute; 70, 71; 70, 72; 71, identifier:image_dict; 72, identifier:values; 73, argument_list
def group_pairs(blocks, layout_blocks_list): """Sort a list of layout blocks into pairs Arguments: List:blocks -- List of block objects List:layout_blocks -- List of layout block indexes Returns: List -- Layout block pair indexes grouped in a list """ image_dict={} for block_id in layout_blocks_list: image_seq=blocks[block_id].ec_hdr.image_seq if image_seq not in image_dict: image_dict[image_seq]=[block_id] else: image_dict[image_seq].append(block_id) log(group_pairs, 'Layout blocks found at PEBs: %s' % list(image_dict.values())) return list(image_dict.values())
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:read_cBpack; 3, parameters; 3, 4; 4, identifier:filename; 5, block; 5, 6; 5, 8; 5, 34; 5, 40; 5, 74; 6, expression_statement; 6, 7; 7, comment; 8, with_statement; 8, 9; 8, 21; 9, with_clause; 9, 10; 10, with_item; 10, 11; 11, as_pattern; 11, 12; 11, 19; 12, call; 12, 13; 12, 16; 13, attribute; 13, 14; 13, 15; 14, identifier:gzip; 15, identifier:open; 16, argument_list; 16, 17; 16, 18; 17, identifier:filename; 18, string:'rb'; 19, as_pattern_target; 19, 20; 20, identifier:infile; 21, block; 21, 22; 22, expression_statement; 22, 23; 23, assignment; 23, 24; 23, 25; 24, identifier:data; 25, call; 25, 26; 25, 29; 26, attribute; 26, 27; 26, 28; 27, identifier:msgpack; 28, identifier:load; 29, argument_list; 29, 30; 29, 31; 30, identifier:infile; 31, keyword_argument; 31, 32; 31, 33; 32, identifier:raw; 33, False; 34, expression_statement; 34, 35; 35, assignment; 35, 36; 35, 37; 36, identifier:header; 37, subscript; 37, 38; 37, 39; 38, identifier:data; 39, integer:0; 40, if_statement; 40, 41; 40, 66; 41, parenthesized_expression; 41, 42; 42, boolean_operator:or; 42, 43; 42, 58; 43, boolean_operator:or; 43, 44; 43, 50; 44, not_operator; 44, 45; 45, call; 45, 46; 45, 47; 46, identifier:isinstance; 47, argument_list; 47, 48; 47, 49; 48, identifier:header; 49, identifier:dict; 50, comparison_operator:!=; 50, 51; 50, 57; 51, call; 51, 52; 51, 55; 52, attribute; 52, 53; 52, 54; 53, identifier:header; 54, identifier:get; 55, argument_list; 55, 56; 56, string:'format'; 57, string:'cB'; 58, comparison_operator:!=; 58, 59; 58, 65; 59, call; 59, 60; 59, 63; 60, attribute; 60, 61; 60, 62; 61, identifier:header; 62, identifier:get; 63, argument_list; 63, 64; 64, string:'version'; 65, integer:1; 66, block; 66, 67; 67, raise_statement; 67, 68; 68, call; 68, 69; 68, 70; 69, identifier:ValueError; 70, argument_list; 70, 71; 71, binary_operator:%; 71, 72; 71, 73; 72, string:"Unexpected header: %r"; 73, identifier:header; 74, return_statement; 74, 75; 75, subscript; 75, 76; 75, 77; 76, identifier:data; 77, slice; 77, 78; 77, 79; 78, integer:1; 79, colon
def read_cBpack(filename): """ Read a file from an idiosyncratic format that we use for storing approximate word frequencies, called "cBpack". The cBpack format is as follows: - The file on disk is a gzipped file in msgpack format, which decodes to a list whose first element is a header, and whose remaining elements are lists of words. - The header is a dictionary with 'format' and 'version' keys that make sure that we're reading the right thing. - Each inner list of words corresponds to a particular word frequency, rounded to the nearest centibel -- that is, one tenth of a decibel, or a factor of 10 ** .01. 0 cB represents a word that occurs with probability 1, so it is the only word in the data (this of course doesn't happen). -200 cB represents a word that occurs once per 100 tokens, -300 cB represents a word that occurs once per 1000 tokens, and so on. - The index of each list within the overall list (without the header) is the negative of its frequency in centibels. - Each inner list is sorted in alphabetical order. As an example, consider a corpus consisting only of the words "red fish blue fish". The word "fish" occurs as 50% of tokens (-30 cB), while "red" and "blue" occur as 25% of tokens (-60 cB). The cBpack file of their word frequencies would decode to this: [ {'format': 'cB', 'version': 1}, [], [], [], ... # 30 empty lists ['fish'], [], [], [], ... # 29 more empty lists ['blue', 'red'] ] """ with gzip.open(filename, 'rb') as infile: data = msgpack.load(infile, raw=False) header = data[0] if ( not isinstance(header, dict) or header.get('format') != 'cB' or header.get('version') != 1 ): raise ValueError("Unexpected header: %r" % header) return data[1:]
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 10; 2, function_name:addDiscreteOutcomeConstantMean; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 4, identifier:distribution; 5, identifier:x; 6, identifier:p; 7, default_parameter; 7, 8; 7, 9; 8, identifier:sort; 9, False; 10, block; 10, 11; 10, 13; 10, 37; 10, 54; 10, 78; 11, expression_statement; 11, 12; 12, string:''' Adds a discrete outcome of x with probability p to an existing distribution, holding constant the relative probabilities of other outcomes and overall mean. Parameters ---------- distribution : [np.array] Two element list containing a list of probabilities and a list of outcomes. x : float The new value to be added to the distribution. p : float The probability of the discrete outcome x occuring. sort: bool Whether or not to sort X before returning it Returns ------- X : np.array Discrete points for discrete probability mass function. pmf : np.array Probability associated with each point in X. Written by Matthew N. White Latest update: 08 December 2015 by David Low '''; 13, expression_statement; 13, 14; 14, assignment; 14, 15; 14, 16; 15, identifier:X; 16, call; 16, 17; 16, 20; 17, attribute; 17, 18; 17, 19; 18, identifier:np; 19, identifier:append; 20, argument_list; 20, 21; 20, 22; 21, identifier:x; 22, binary_operator:/; 22, 23; 22, 33; 23, binary_operator:*; 23, 24; 23, 27; 24, subscript; 24, 25; 24, 26; 25, identifier:distribution; 26, integer:1; 27, parenthesized_expression; 27, 28; 28, binary_operator:-; 28, 29; 28, 30; 29, integer:1; 30, binary_operator:*; 30, 31; 30, 32; 31, identifier:p; 32, identifier:x; 33, parenthesized_expression; 33, 34; 34, binary_operator:-; 34, 35; 34, 36; 35, integer:1; 36, identifier:p; 37, expression_statement; 37, 38; 38, assignment; 38, 39; 38, 40; 39, identifier:pmf; 40, call; 40, 41; 40, 44; 41, attribute; 41, 42; 41, 43; 42, identifier:np; 43, identifier:append; 44, argument_list; 44, 45; 44, 46; 45, identifier:p; 46, binary_operator:*; 46, 47; 46, 50; 47, subscript; 47, 48; 47, 49; 48, identifier:distribution; 49, integer:0; 50, parenthesized_expression; 50, 51; 51, binary_operator:-; 51, 52; 51, 53; 52, integer:1; 53, identifier:p; 54, if_statement; 54, 55; 54, 56; 55, identifier:sort; 56, block; 56, 57; 56, 66; 56, 72; 57, expression_statement; 57, 58; 58, assignment; 58, 59; 58, 60; 59, identifier:indices; 60, call; 60, 61; 60, 64; 61, attribute; 61, 62; 61, 63; 62, identifier:np; 63, identifier:argsort; 64, argument_list; 64, 65; 65, identifier:X; 66, expression_statement; 66, 67; 67, assignment; 67, 68; 67, 69; 68, identifier:X; 69, subscript; 69, 70; 69, 71; 70, identifier:X; 71, identifier:indices; 72, expression_statement; 72, 73; 73, assignment; 73, 74; 73, 75; 74, identifier:pmf; 75, subscript; 75, 76; 75, 77; 76, identifier:pmf; 77, identifier:indices; 78, return_statement; 78, 79; 79, parenthesized_expression; 79, 80; 80, list:[pmf,X]; 80, 81; 80, 82; 81, identifier:pmf; 82, identifier:X
def addDiscreteOutcomeConstantMean(distribution, x, p, sort = False): ''' Adds a discrete outcome of x with probability p to an existing distribution, holding constant the relative probabilities of other outcomes and overall mean. Parameters ---------- distribution : [np.array] Two element list containing a list of probabilities and a list of outcomes. x : float The new value to be added to the distribution. p : float The probability of the discrete outcome x occuring. sort: bool Whether or not to sort X before returning it Returns ------- X : np.array Discrete points for discrete probability mass function. pmf : np.array Probability associated with each point in X. Written by Matthew N. White Latest update: 08 December 2015 by David Low ''' X = np.append(x,distribution[1]*(1-p*x)/(1-p)) pmf = np.append(p,distribution[0]*(1-p)) if sort: indices = np.argsort(X) X = X[indices] pmf = pmf[indices] return([pmf,X])
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 10; 2, function_name:runStickyEregressionsInStata; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 3, 9; 4, identifier:infile_name; 5, identifier:interval_size; 6, identifier:meas_err; 7, identifier:sticky; 8, identifier:all_specs; 9, identifier:stata_exe; 10, block; 10, 11; 10, 13; 10, 17; 10, 32; 10, 45; 10, 58; 10, 59; 10, 76; 10, 77; 10, 89; 10, 99; 10, 114; 10, 115; 10, 160; 11, expression_statement; 11, 12; 12, string:''' Runs regressions for the main tables of the StickyC paper in Stata and produces a LaTeX table with results for one "panel". Running in Stata allows production of the KP-statistic, for which there is currently no command in statsmodels.api. Parameters ---------- infile_name : str Name of tab-delimited text file with simulation data. Assumed to be in the results directory, and was almost surely generated by makeStickyEdataFile unless we resort to fabricating simulated data. THAT'S A JOKE, FUTURE REFEREES. interval_size : int Number of periods in each regression sample (or interval). meas_err : bool Indicator for whether to add measurement error to DeltaLogC. sticky : bool Indicator for whether these results used sticky expectations. all_specs : bool Indicator for whether this panel should include all specifications or just the OLS on lagged consumption growth. stata_exe : str Absolute location where the Stata executable can be found on the computer running this code. Usually set at the top of StickyEparams.py. Returns ------- panel_text : str String with one panel's worth of LaTeX input. '''; 13, expression_statement; 13, 14; 14, assignment; 14, 15; 14, 16; 15, identifier:dofile; 16, string:"StickyETimeSeries.do"; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 20; 19, identifier:infile_name_full; 20, call; 20, 21; 20, 26; 21, attribute; 21, 22; 21, 25; 22, attribute; 22, 23; 22, 24; 23, identifier:os; 24, identifier:path; 25, identifier:abspath; 26, argument_list; 26, 27; 27, binary_operator:+; 27, 28; 27, 31; 28, binary_operator:+; 28, 29; 28, 30; 29, identifier:results_dir; 30, identifier:infile_name; 31, string:".txt"; 32, expression_statement; 32, 33; 33, assignment; 33, 34; 33, 35; 34, identifier:temp_name_full; 35, call; 35, 36; 35, 41; 36, attribute; 36, 37; 36, 40; 37, attribute; 37, 38; 37, 39; 38, identifier:os; 39, identifier:path; 40, identifier:abspath; 41, argument_list; 41, 42; 42, binary_operator:+; 42, 43; 42, 44; 43, identifier:results_dir; 44, string:"temp.txt"; 45, if_statement; 45, 46; 45, 47; 45, 52; 46, identifier:meas_err; 47, block; 47, 48; 48, expression_statement; 48, 49; 49, assignment; 49, 50; 49, 51; 50, identifier:meas_err_stata; 51, integer:1; 52, else_clause; 52, 53; 53, block; 53, 54; 54, expression_statement; 54, 55; 55, assignment; 55, 56; 55, 57; 56, identifier:meas_err_stata; 57, integer:0; 58, comment; 59, expression_statement; 59, 60; 60, assignment; 60, 61; 60, 62; 61, identifier:cmd; 62, list:[stata_exe, "do", dofile, infile_name_full, temp_name_full, str(interval_size), str(meas_err_stata)]; 62, 63; 62, 64; 62, 65; 62, 66; 62, 67; 62, 68; 62, 72; 63, identifier:stata_exe; 64, string:"do"; 65, identifier:dofile; 66, identifier:infile_name_full; 67, identifier:temp_name_full; 68, call; 68, 69; 68, 70; 69, identifier:str; 70, argument_list; 70, 71; 71, identifier:interval_size; 72, call; 72, 73; 72, 74; 73, identifier:str; 74, argument_list; 74, 75; 75, identifier:meas_err_stata; 76, comment; 77, expression_statement; 77, 78; 78, assignment; 78, 79; 78, 80; 79, identifier:stata_status; 80, call; 80, 81; 80, 84; 81, attribute; 81, 82; 81, 83; 82, identifier:subprocess; 83, identifier:call; 84, argument_list; 84, 85; 84, 86; 85, identifier:cmd; 86, keyword_argument; 86, 87; 86, 88; 87, identifier:shell; 88, string:'true'; 89, if_statement; 89, 90; 89, 93; 90, comparison_operator:!=; 90, 91; 90, 92; 91, identifier:stata_status; 92, integer:0; 93, block; 93, 94; 94, raise_statement; 94, 95; 95, call; 95, 96; 95, 97; 96, identifier:ValueError; 97, argument_list; 97, 98; 98, string:'Stata code could not run. Check the stata_exe in StickyEparams.py'; 99, expression_statement; 99, 100; 100, assignment; 100, 101; 100, 102; 101, identifier:stata_output; 102, call; 102, 103; 102, 106; 103, attribute; 103, 104; 103, 105; 104, identifier:pd; 105, identifier:read_csv; 106, argument_list; 106, 107; 106, 108; 106, 111; 107, identifier:temp_name_full; 108, keyword_argument; 108, 109; 108, 110; 109, identifier:sep; 110, string:','; 111, keyword_argument; 111, 112; 111, 113; 112, identifier:header; 113, integer:0; 114, comment; 115, expression_statement; 115, 116; 116, assignment; 116, 117; 116, 118; 117, identifier:panel_text; 118, call; 118, 119; 118, 120; 119, identifier:makeResultsPanel; 120, argument_list; 120, 121; 120, 126; 120, 131; 120, 136; 120, 141; 120, 146; 120, 151; 120, 154; 120, 157; 121, keyword_argument; 121, 122; 121, 123; 122, identifier:Coeffs; 123, attribute; 123, 124; 123, 125; 124, identifier:stata_output; 125, identifier:CoeffsArray; 126, keyword_argument; 126, 127; 126, 128; 127, identifier:StdErrs; 128, attribute; 128, 129; 128, 130; 129, identifier:stata_output; 130, identifier:StdErrArray; 131, keyword_argument; 131, 132; 131, 133; 132, identifier:Rsq; 133, attribute; 133, 134; 133, 135; 134, identifier:stata_output; 135, identifier:RsqArray; 136, keyword_argument; 136, 137; 136, 138; 137, identifier:Pvals; 138, attribute; 138, 139; 138, 140; 139, identifier:stata_output; 140, identifier:PvalArray; 141, keyword_argument; 141, 142; 141, 143; 142, identifier:OID; 143, attribute; 143, 144; 143, 145; 144, identifier:stata_output; 145, identifier:OIDarray; 146, keyword_argument; 146, 147; 146, 148; 147, identifier:Counts; 148, attribute; 148, 149; 148, 150; 149, identifier:stata_output; 150, identifier:ExtraInfo; 151, keyword_argument; 151, 152; 151, 153; 152, identifier:meas_err; 153, identifier:meas_err; 154, keyword_argument; 154, 155; 154, 156; 155, identifier:sticky; 156, identifier:sticky; 157, keyword_argument; 157, 158; 157, 159; 158, identifier:all_specs; 159, identifier:all_specs; 160, return_statement; 160, 161; 161, identifier:panel_text
def runStickyEregressionsInStata(infile_name,interval_size,meas_err,sticky,all_specs,stata_exe): ''' Runs regressions for the main tables of the StickyC paper in Stata and produces a LaTeX table with results for one "panel". Running in Stata allows production of the KP-statistic, for which there is currently no command in statsmodels.api. Parameters ---------- infile_name : str Name of tab-delimited text file with simulation data. Assumed to be in the results directory, and was almost surely generated by makeStickyEdataFile unless we resort to fabricating simulated data. THAT'S A JOKE, FUTURE REFEREES. interval_size : int Number of periods in each regression sample (or interval). meas_err : bool Indicator for whether to add measurement error to DeltaLogC. sticky : bool Indicator for whether these results used sticky expectations. all_specs : bool Indicator for whether this panel should include all specifications or just the OLS on lagged consumption growth. stata_exe : str Absolute location where the Stata executable can be found on the computer running this code. Usually set at the top of StickyEparams.py. Returns ------- panel_text : str String with one panel's worth of LaTeX input. ''' dofile = "StickyETimeSeries.do" infile_name_full = os.path.abspath(results_dir + infile_name + ".txt") temp_name_full = os.path.abspath(results_dir + "temp.txt") if meas_err: meas_err_stata = 1 else: meas_err_stata = 0 # Define the command to run the Stata do file cmd = [stata_exe, "do", dofile, infile_name_full, temp_name_full, str(interval_size), str(meas_err_stata)] # Run Stata do-file stata_status = subprocess.call(cmd,shell = 'true') if stata_status!=0: raise ValueError('Stata code could not run. Check the stata_exe in StickyEparams.py') stata_output = pd.read_csv(temp_name_full, sep=',',header=0) # Make results table and return it panel_text = makeResultsPanel(Coeffs=stata_output.CoeffsArray, StdErrs=stata_output.StdErrArray, Rsq=stata_output.RsqArray, Pvals=stata_output.PvalArray, OID=stata_output.OIDarray, Counts=stata_output.ExtraInfo, meas_err=meas_err, sticky=sticky, all_specs=all_specs) return panel_text
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:capability_functions; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:fn; 6, block; 6, 7; 6, 9; 6, 20; 6, 21; 6, 25; 6, 69; 6, 70; 6, 84; 6, 95; 6, 96; 7, expression_statement; 7, 8; 8, comment; 9, if_statement; 9, 10; 9, 11; 10, identifier:_debug; 11, block; 11, 12; 12, expression_statement; 12, 13; 13, call; 13, 14; 13, 17; 14, attribute; 14, 15; 14, 16; 15, identifier:Collector; 16, identifier:_debug; 17, argument_list; 17, 18; 17, 19; 18, string:"capability_functions %r"; 19, identifier:fn; 20, comment; 21, expression_statement; 21, 22; 22, assignment; 22, 23; 22, 24; 23, identifier:fns; 24, list:[]; 25, for_statement; 25, 26; 25, 27; 25, 30; 26, identifier:cls; 27, attribute; 27, 28; 27, 29; 28, identifier:self; 29, identifier:capabilities; 30, block; 30, 31; 30, 40; 30, 52; 31, expression_statement; 31, 32; 32, assignment; 32, 33; 32, 34; 33, identifier:xfn; 34, call; 34, 35; 34, 36; 35, identifier:getattr; 36, argument_list; 36, 37; 36, 38; 36, 39; 37, identifier:cls; 38, identifier:fn; 39, None; 40, if_statement; 40, 41; 40, 42; 41, identifier:_debug; 42, block; 42, 43; 43, expression_statement; 43, 44; 44, call; 44, 45; 44, 48; 45, attribute; 45, 46; 45, 47; 46, identifier:Collector; 47, identifier:_debug; 48, argument_list; 48, 49; 48, 50; 48, 51; 49, string:" - cls, xfn: %r, %r"; 50, identifier:cls; 51, identifier:xfn; 52, if_statement; 52, 53; 52, 54; 53, identifier:xfn; 54, block; 54, 55; 55, expression_statement; 55, 56; 56, call; 56, 57; 56, 60; 57, attribute; 57, 58; 57, 59; 58, identifier:fns; 59, identifier:append; 60, argument_list; 60, 61; 61, tuple; 61, 62; 61, 68; 62, call; 62, 63; 62, 64; 63, identifier:getattr; 64, argument_list; 64, 65; 64, 66; 64, 67; 65, identifier:cls; 66, string:'_zindex'; 67, None; 68, identifier:xfn; 69, comment; 70, expression_statement; 70, 71; 71, call; 71, 72; 71, 75; 72, attribute; 72, 73; 72, 74; 73, identifier:fns; 74, identifier:sort; 75, argument_list; 75, 76; 76, keyword_argument; 76, 77; 76, 78; 77, identifier:key; 78, lambda; 78, 79; 78, 81; 79, lambda_parameters; 79, 80; 80, identifier:v; 81, subscript; 81, 82; 81, 83; 82, identifier:v; 83, integer:0; 84, if_statement; 84, 85; 84, 86; 85, identifier:_debug; 86, block; 86, 87; 87, expression_statement; 87, 88; 88, call; 88, 89; 88, 92; 89, attribute; 89, 90; 89, 91; 90, identifier:Collector; 91, identifier:_debug; 92, argument_list; 92, 93; 92, 94; 93, string:" - fns: %r"; 94, identifier:fns; 95, comment; 96, for_statement; 96, 97; 96, 100; 96, 101; 97, pattern_list; 97, 98; 97, 99; 98, identifier:xindx; 99, identifier:xfn; 100, identifier:fns; 101, block; 101, 102; 101, 113; 102, if_statement; 102, 103; 102, 104; 103, identifier:_debug; 104, block; 104, 105; 105, expression_statement; 105, 106; 106, call; 106, 107; 106, 110; 107, attribute; 107, 108; 107, 109; 108, identifier:Collector; 109, identifier:_debug; 110, argument_list; 110, 111; 110, 112; 111, string:" - yield xfn: %r"; 112, identifier:xfn; 113, expression_statement; 113, 114; 114, yield; 114, 115; 115, identifier:xfn
def capability_functions(self, fn): """This generator yields functions that match the requested capability sorted by z-index.""" if _debug: Collector._debug("capability_functions %r", fn) # build a list of functions to call fns = [] for cls in self.capabilities: xfn = getattr(cls, fn, None) if _debug: Collector._debug(" - cls, xfn: %r, %r", cls, xfn) if xfn: fns.append( (getattr(cls, '_zindex', None), xfn) ) # sort them by z-index fns.sort(key=lambda v: v[0]) if _debug: Collector._debug(" - fns: %r", fns) # now yield them in order for xindx, xfn in fns: if _debug: Collector._debug(" - yield xfn: %r", xfn) yield xfn
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:add; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, list_splat_pattern; 5, 6; 6, identifier:items; 7, block; 7, 8; 7, 10; 7, 37; 8, expression_statement; 8, 9; 9, comment; 10, for_statement; 10, 11; 10, 12; 10, 13; 11, identifier:item; 12, identifier:items; 13, block; 13, 14; 13, 23; 13, 29; 14, expression_statement; 14, 15; 15, call; 15, 16; 15, 21; 16, attribute; 16, 17; 16, 20; 17, attribute; 17, 18; 17, 19; 18, identifier:self; 19, identifier:unsorted; 20, identifier:append; 21, argument_list; 21, 22; 22, identifier:item; 23, expression_statement; 23, 24; 24, assignment; 24, 25; 24, 26; 25, identifier:key; 26, subscript; 26, 27; 26, 28; 27, identifier:item; 28, integer:0; 29, expression_statement; 29, 30; 30, assignment; 30, 31; 30, 36; 31, subscript; 31, 32; 31, 35; 32, attribute; 32, 33; 32, 34; 33, identifier:self; 34, identifier:index; 35, identifier:key; 36, identifier:item; 37, return_statement; 37, 38; 38, identifier:self
def add(self, *items): """ Add items to be sorted. @param items: One or more items to be added. @type items: I{item} @return: self @rtype: L{DepList} """ for item in items: self.unsorted.append(item) key = item[0] self.index[key] = item return self
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:sort; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 16; 5, 24; 5, 131; 5, 139; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 13; 10, attribute; 10, 11; 10, 12; 11, identifier:self; 12, identifier:sorted; 13, call; 13, 14; 13, 15; 14, identifier:list; 15, argument_list; 16, expression_statement; 16, 17; 17, assignment; 17, 18; 17, 21; 18, attribute; 18, 19; 18, 20; 19, identifier:self; 20, identifier:pushed; 21, call; 21, 22; 21, 23; 22, identifier:set; 23, argument_list; 24, for_statement; 24, 25; 24, 26; 24, 29; 25, identifier:item; 26, attribute; 26, 27; 26, 28; 27, identifier:self; 28, identifier:unsorted; 29, block; 29, 30; 29, 34; 29, 41; 29, 118; 30, expression_statement; 30, 31; 31, assignment; 31, 32; 31, 33; 32, identifier:popped; 33, list:[]; 34, expression_statement; 34, 35; 35, call; 35, 36; 35, 39; 36, attribute; 36, 37; 36, 38; 37, identifier:self; 38, identifier:push; 39, argument_list; 39, 40; 40, identifier:item; 41, while_statement; 41, 42; 41, 48; 42, call; 42, 43; 42, 44; 43, identifier:len; 44, argument_list; 44, 45; 45, attribute; 45, 46; 45, 47; 46, identifier:self; 47, identifier:stack; 48, block; 48, 49; 49, try_statement; 49, 50; 49, 103; 50, block; 50, 51; 50, 59; 50, 68; 50, 79; 50, 96; 51, expression_statement; 51, 52; 52, assignment; 52, 53; 52, 54; 53, identifier:top; 54, call; 54, 55; 54, 58; 55, attribute; 55, 56; 55, 57; 56, identifier:self; 57, identifier:top; 58, argument_list; 59, expression_statement; 59, 60; 60, assignment; 60, 61; 60, 62; 61, identifier:ref; 62, call; 62, 63; 62, 64; 63, identifier:next; 64, argument_list; 64, 65; 65, subscript; 65, 66; 65, 67; 66, identifier:top; 67, integer:1; 68, expression_statement; 68, 69; 69, assignment; 69, 70; 69, 71; 70, identifier:refd; 71, call; 71, 72; 71, 77; 72, attribute; 72, 73; 72, 76; 73, attribute; 73, 74; 73, 75; 74, identifier:self; 75, identifier:index; 76, identifier:get; 77, argument_list; 77, 78; 78, identifier:ref; 79, if_statement; 79, 80; 79, 83; 80, comparison_operator:is; 80, 81; 80, 82; 81, identifier:refd; 82, None; 83, block; 83, 84; 83, 95; 84, expression_statement; 84, 85; 85, call; 85, 86; 85, 89; 86, attribute; 86, 87; 86, 88; 87, identifier:log; 88, identifier:debug; 89, argument_list; 89, 90; 89, 91; 90, string:'"%s" not found, skipped'; 91, call; 91, 92; 91, 93; 92, identifier:Repr; 93, argument_list; 93, 94; 94, identifier:ref; 95, continue_statement; 96, expression_statement; 96, 97; 97, call; 97, 98; 97, 101; 98, attribute; 98, 99; 98, 100; 99, identifier:self; 100, identifier:push; 101, argument_list; 101, 102; 102, identifier:refd; 103, except_clause; 103, 104; 103, 105; 104, identifier:StopIteration; 105, block; 105, 106; 105, 117; 106, expression_statement; 106, 107; 107, call; 107, 108; 107, 111; 108, attribute; 108, 109; 108, 110; 109, identifier:popped; 110, identifier:append; 111, argument_list; 111, 112; 112, call; 112, 113; 112, 116; 113, attribute; 113, 114; 113, 115; 114, identifier:self; 115, identifier:pop; 116, argument_list; 117, continue_statement; 118, for_statement; 118, 119; 118, 120; 118, 121; 119, identifier:p; 120, identifier:popped; 121, block; 121, 122; 122, expression_statement; 122, 123; 123, call; 123, 124; 123, 129; 124, attribute; 124, 125; 124, 128; 125, attribute; 125, 126; 125, 127; 126, identifier:self; 127, identifier:sorted; 128, identifier:append; 129, argument_list; 129, 130; 130, identifier:p; 131, expression_statement; 131, 132; 132, assignment; 132, 133; 132, 136; 133, attribute; 133, 134; 133, 135; 134, identifier:self; 135, identifier:unsorted; 136, attribute; 136, 137; 136, 138; 137, identifier:self; 138, identifier:sorted; 139, return_statement; 139, 140; 140, attribute; 140, 141; 140, 142; 141, identifier:self; 142, identifier:sorted
def sort(self): """ Sort the list based on dependancies. @return: The sorted items. @rtype: list """ self.sorted = list() self.pushed = set() for item in self.unsorted: popped = [] self.push(item) while len(self.stack): try: top = self.top() ref = next(top[1]) refd = self.index.get(ref) if refd is None: log.debug('"%s" not found, skipped', Repr(ref)) continue self.push(refd) except StopIteration: popped.append(self.pop()) continue for p in popped: self.sorted.append(p) self.unsorted = self.sorted return self.sorted
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:push; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:item; 6, block; 6, 7; 6, 9; 6, 17; 6, 28; 6, 37; 7, expression_statement; 7, 8; 8, comment; 9, if_statement; 9, 10; 9, 15; 10, comparison_operator:in; 10, 11; 10, 12; 11, identifier:item; 12, attribute; 12, 13; 12, 14; 13, identifier:self; 14, identifier:pushed; 15, block; 15, 16; 16, return_statement; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 20; 19, identifier:frame; 20, tuple; 20, 21; 20, 22; 21, identifier:item; 22, call; 22, 23; 22, 24; 23, identifier:iter; 24, argument_list; 24, 25; 25, subscript; 25, 26; 25, 27; 26, identifier:item; 27, integer:1; 28, expression_statement; 28, 29; 29, call; 29, 30; 29, 35; 30, attribute; 30, 31; 30, 34; 31, attribute; 31, 32; 31, 33; 32, identifier:self; 33, identifier:stack; 34, identifier:append; 35, argument_list; 35, 36; 36, identifier:frame; 37, expression_statement; 37, 38; 38, call; 38, 39; 38, 44; 39, attribute; 39, 40; 39, 43; 40, attribute; 40, 41; 40, 42; 41, identifier:self; 42, identifier:pushed; 43, identifier:add; 44, argument_list; 44, 45; 45, identifier:item
def push(self, item): """ Push and item onto the sorting stack. @param item: An item to push. @type item: I{item} @return: The number of items pushed. @rtype: int """ if item in self.pushed: return frame = (item, iter(item[1])) self.stack.append(frame) self.pushed.add(item)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:sort; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:content; 6, block; 6, 7; 6, 9; 6, 15; 6, 41; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:v; 12, attribute; 12, 13; 12, 14; 13, identifier:content; 14, identifier:value; 15, if_statement; 15, 16; 15, 21; 16, call; 16, 17; 16, 18; 17, identifier:isinstance; 18, argument_list; 18, 19; 18, 20; 19, identifier:v; 20, identifier:Object; 21, block; 21, 22; 21, 28; 22, expression_statement; 22, 23; 23, assignment; 23, 24; 23, 25; 24, identifier:md; 25, attribute; 25, 26; 25, 27; 26, identifier:v; 27, identifier:__metadata__; 28, expression_statement; 28, 29; 29, assignment; 29, 30; 29, 33; 30, attribute; 30, 31; 30, 32; 31, identifier:md; 32, identifier:ordering; 33, call; 33, 34; 33, 37; 34, attribute; 34, 35; 34, 36; 35, identifier:self; 36, identifier:ordering; 37, argument_list; 37, 38; 38, attribute; 38, 39; 38, 40; 39, identifier:content; 40, identifier:real; 41, return_statement; 41, 42; 42, identifier:self
def sort(self, content): """ Sort suds object attributes based on ordering defined in the XSD type information. @param content: The content to sort. @type content: L{Object} @return: self @rtype: L{Typed} """ v = content.value if isinstance(v, Object): md = v.__metadata__ md.ordering = self.ordering(content.real) return self
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:alerts; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:alert_level; 7, string:'High'; 8, block; 8, 9; 8, 11; 8, 23; 8, 31; 8, 67; 9, expression_statement; 9, 10; 10, comment; 11, expression_statement; 11, 12; 12, assignment; 12, 13; 12, 14; 13, identifier:alerts; 14, call; 14, 15; 14, 22; 15, attribute; 15, 16; 15, 21; 16, attribute; 16, 17; 16, 20; 17, attribute; 17, 18; 17, 19; 18, identifier:self; 19, identifier:zap; 20, identifier:core; 21, identifier:alerts; 22, argument_list; 23, expression_statement; 23, 24; 24, assignment; 24, 25; 24, 26; 25, identifier:alert_level_value; 26, subscript; 26, 27; 26, 30; 27, attribute; 27, 28; 27, 29; 28, identifier:self; 29, identifier:alert_levels; 30, identifier:alert_level; 31, expression_statement; 31, 32; 32, assignment; 32, 33; 32, 34; 33, identifier:alerts; 34, call; 34, 35; 34, 36; 35, identifier:sorted; 36, argument_list; 36, 37; 36, 52; 36, 64; 37, generator_expression; 37, 38; 37, 39; 37, 42; 38, identifier:a; 39, for_in_clause; 39, 40; 39, 41; 40, identifier:a; 41, identifier:alerts; 42, if_clause; 42, 43; 43, comparison_operator:>=; 43, 44; 43, 51; 44, subscript; 44, 45; 44, 48; 45, attribute; 45, 46; 45, 47; 46, identifier:self; 47, identifier:alert_levels; 48, subscript; 48, 49; 48, 50; 49, identifier:a; 50, string:'risk'; 51, identifier:alert_level_value; 52, keyword_argument; 52, 53; 52, 54; 53, identifier:key; 54, lambda; 54, 55; 54, 57; 55, lambda_parameters; 55, 56; 56, identifier:k; 57, subscript; 57, 58; 57, 61; 58, attribute; 58, 59; 58, 60; 59, identifier:self; 60, identifier:alert_levels; 61, subscript; 61, 62; 61, 63; 62, identifier:k; 63, string:'risk'; 64, keyword_argument; 64, 65; 64, 66; 65, identifier:reverse; 66, True; 67, return_statement; 67, 68; 68, identifier:alerts
def alerts(self, alert_level='High'): """Get a filtered list of alerts at the given alert level, and sorted by alert level.""" alerts = self.zap.core.alerts() alert_level_value = self.alert_levels[alert_level] alerts = sorted((a for a in alerts if self.alert_levels[a['risk']] >= alert_level_value), key=lambda k: self.alert_levels[k['risk']], reverse=True) return alerts
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:assortativity_bin; 3, parameters; 3, 4; 3, 5; 4, identifier:CIJ; 5, default_parameter; 5, 6; 5, 7; 6, identifier:flag; 7, integer:0; 8, block; 8, 9; 8, 11; 8, 170; 8, 171; 8, 184; 8, 205; 8, 225; 8, 237; 9, expression_statement; 9, 10; 10, string:''' The assortativity coefficient is a correlation coefficient between the degrees of all nodes on two opposite ends of a link. A positive assortativity coefficient indicates that nodes tend to link to other nodes with the same or similar degree. Parameters ---------- CIJ : NxN np.ndarray binary directed/undirected connection matrix flag : int 0 : undirected graph; degree/degree correlation 1 : directed graph; out-degree/in-degree correlation 2 : directed graph; in-degree/out-degree correlation 3 : directed graph; out-degree/out-degree correlation 4 : directed graph; in-degree/in-degreen correlation Returns ------- r : float assortativity coefficient Notes ----- The function accepts weighted networks, but all connection weights are ignored. The main diagonal should be empty. For flag 1 the function computes the directed assortativity described in Rubinov and Sporns (2010) NeuroImage. '''; 11, if_statement; 11, 12; 11, 15; 11, 16; 11, 62; 12, comparison_operator:==; 12, 13; 12, 14; 13, identifier:flag; 14, integer:0; 15, comment; 16, block; 16, 17; 16, 24; 16, 43; 16, 50; 16, 56; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 20; 19, identifier:deg; 20, call; 20, 21; 20, 22; 21, identifier:degrees_und; 22, argument_list; 22, 23; 23, identifier:CIJ; 24, expression_statement; 24, 25; 25, assignment; 25, 26; 25, 29; 26, pattern_list; 26, 27; 26, 28; 27, identifier:i; 28, identifier:j; 29, call; 29, 30; 29, 33; 30, attribute; 30, 31; 30, 32; 31, identifier:np; 32, identifier:where; 33, argument_list; 33, 34; 34, comparison_operator:>; 34, 35; 34, 42; 35, call; 35, 36; 35, 39; 36, attribute; 36, 37; 36, 38; 37, identifier:np; 38, identifier:triu; 39, argument_list; 39, 40; 39, 41; 40, identifier:CIJ; 41, integer:1; 42, integer:0; 43, expression_statement; 43, 44; 44, assignment; 44, 45; 44, 46; 45, identifier:K; 46, call; 46, 47; 46, 48; 47, identifier:len; 48, argument_list; 48, 49; 49, identifier:i; 50, expression_statement; 50, 51; 51, assignment; 51, 52; 51, 53; 52, identifier:degi; 53, subscript; 53, 54; 53, 55; 54, identifier:deg; 55, identifier:i; 56, expression_statement; 56, 57; 57, assignment; 57, 58; 57, 59; 58, identifier:degj; 59, subscript; 59, 60; 59, 61; 60, identifier:deg; 61, identifier:j; 62, else_clause; 62, 63; 62, 64; 63, comment; 64, block; 64, 65; 64, 75; 64, 88; 64, 95; 65, expression_statement; 65, 66; 66, assignment; 66, 67; 66, 71; 67, pattern_list; 67, 68; 67, 69; 67, 70; 68, identifier:id; 69, identifier:od; 70, identifier:deg; 71, call; 71, 72; 71, 73; 72, identifier:degrees_dir; 73, argument_list; 73, 74; 74, identifier:CIJ; 75, expression_statement; 75, 76; 76, assignment; 76, 77; 76, 80; 77, pattern_list; 77, 78; 77, 79; 78, identifier:i; 79, identifier:j; 80, call; 80, 81; 80, 84; 81, attribute; 81, 82; 81, 83; 82, identifier:np; 83, identifier:where; 84, argument_list; 84, 85; 85, comparison_operator:>; 85, 86; 85, 87; 86, identifier:CIJ; 87, integer:0; 88, expression_statement; 88, 89; 89, assignment; 89, 90; 89, 91; 90, identifier:K; 91, call; 91, 92; 91, 93; 92, identifier:len; 93, argument_list; 93, 94; 94, identifier:i; 95, if_statement; 95, 96; 95, 99; 95, 112; 95, 129; 95, 146; 95, 163; 96, comparison_operator:==; 96, 97; 96, 98; 97, identifier:flag; 98, integer:1; 99, block; 99, 100; 99, 106; 100, expression_statement; 100, 101; 101, assignment; 101, 102; 101, 103; 102, identifier:degi; 103, subscript; 103, 104; 103, 105; 104, identifier:od; 105, identifier:i; 106, expression_statement; 106, 107; 107, assignment; 107, 108; 107, 109; 108, identifier:degj; 109, subscript; 109, 110; 109, 111; 110, identifier:id; 111, identifier:j; 112, elif_clause; 112, 113; 112, 116; 113, comparison_operator:==; 113, 114; 113, 115; 114, identifier:flag; 115, integer:2; 116, block; 116, 117; 116, 123; 117, expression_statement; 117, 118; 118, assignment; 118, 119; 118, 120; 119, identifier:degi; 120, subscript; 120, 121; 120, 122; 121, identifier:id; 122, identifier:i; 123, expression_statement; 123, 124; 124, assignment; 124, 125; 124, 126; 125, identifier:degj; 126, subscript; 126, 127; 126, 128; 127, identifier:od; 128, identifier:j; 129, elif_clause; 129, 130; 129, 133; 130, comparison_operator:==; 130, 131; 130, 132; 131, identifier:flag; 132, integer:3; 133, block; 133, 134; 133, 140; 134, expression_statement; 134, 135; 135, assignment; 135, 136; 135, 137; 136, identifier:degi; 137, subscript; 137, 138; 137, 139; 138, identifier:od; 139, identifier:i; 140, expression_statement; 140, 141; 141, assignment; 141, 142; 141, 143; 142, identifier:degj; 143, subscript; 143, 144; 143, 145; 144, identifier:od; 145, identifier:j; 146, elif_clause; 146, 147; 146, 150; 147, comparison_operator:==; 147, 148; 147, 149; 148, identifier:flag; 149, integer:4; 150, block; 150, 151; 150, 157; 151, expression_statement; 151, 152; 152, assignment; 152, 153; 152, 154; 153, identifier:degi; 154, subscript; 154, 155; 154, 156; 155, identifier:id; 156, identifier:i; 157, expression_statement; 157, 158; 158, assignment; 158, 159; 158, 160; 159, identifier:degj; 160, subscript; 160, 161; 160, 162; 161, identifier:id; 162, identifier:j; 163, else_clause; 163, 164; 164, block; 164, 165; 165, raise_statement; 165, 166; 166, call; 166, 167; 166, 168; 167, identifier:ValueError; 168, argument_list; 168, 169; 169, string:'Flag must be 0-4'; 170, comment; 171, expression_statement; 171, 172; 172, assignment; 172, 173; 172, 174; 173, identifier:term1; 174, binary_operator:/; 174, 175; 174, 183; 175, call; 175, 176; 175, 179; 176, attribute; 176, 177; 176, 178; 177, identifier:np; 178, identifier:sum; 179, argument_list; 179, 180; 180, binary_operator:*; 180, 181; 180, 182; 181, identifier:degi; 182, identifier:degj; 183, identifier:K; 184, expression_statement; 184, 185; 185, assignment; 185, 186; 185, 187; 186, identifier:term2; 187, call; 187, 188; 187, 191; 188, attribute; 188, 189; 188, 190; 189, identifier:np; 190, identifier:square; 191, argument_list; 191, 192; 192, binary_operator:/; 192, 193; 192, 204; 193, call; 193, 194; 193, 197; 194, attribute; 194, 195; 194, 196; 195, identifier:np; 196, identifier:sum; 197, argument_list; 197, 198; 198, binary_operator:*; 198, 199; 198, 200; 199, float:.5; 200, parenthesized_expression; 200, 201; 201, binary_operator:+; 201, 202; 201, 203; 202, identifier:degi; 203, identifier:degj; 204, identifier:K; 205, expression_statement; 205, 206; 206, assignment; 206, 207; 206, 208; 207, identifier:term3; 208, binary_operator:/; 208, 209; 208, 224; 209, call; 209, 210; 209, 213; 210, attribute; 210, 211; 210, 212; 211, identifier:np; 212, identifier:sum; 213, argument_list; 213, 214; 214, binary_operator:*; 214, 215; 214, 216; 215, float:.5; 216, parenthesized_expression; 216, 217; 217, binary_operator:+; 217, 218; 217, 221; 218, binary_operator:*; 218, 219; 218, 220; 219, identifier:degi; 220, identifier:degi; 221, binary_operator:*; 221, 222; 221, 223; 222, identifier:degj; 223, identifier:degj; 224, identifier:K; 225, expression_statement; 225, 226; 226, assignment; 226, 227; 226, 228; 227, identifier:r; 228, binary_operator:/; 228, 229; 228, 233; 229, parenthesized_expression; 229, 230; 230, binary_operator:-; 230, 231; 230, 232; 231, identifier:term1; 232, identifier:term2; 233, parenthesized_expression; 233, 234; 234, binary_operator:-; 234, 235; 234, 236; 235, identifier:term3; 236, identifier:term2; 237, return_statement; 237, 238; 238, identifier:r
def assortativity_bin(CIJ, flag=0): ''' The assortativity coefficient is a correlation coefficient between the degrees of all nodes on two opposite ends of a link. A positive assortativity coefficient indicates that nodes tend to link to other nodes with the same or similar degree. Parameters ---------- CIJ : NxN np.ndarray binary directed/undirected connection matrix flag : int 0 : undirected graph; degree/degree correlation 1 : directed graph; out-degree/in-degree correlation 2 : directed graph; in-degree/out-degree correlation 3 : directed graph; out-degree/out-degree correlation 4 : directed graph; in-degree/in-degreen correlation Returns ------- r : float assortativity coefficient Notes ----- The function accepts weighted networks, but all connection weights are ignored. The main diagonal should be empty. For flag 1 the function computes the directed assortativity described in Rubinov and Sporns (2010) NeuroImage. ''' if flag == 0: # undirected version deg = degrees_und(CIJ) i, j = np.where(np.triu(CIJ, 1) > 0) K = len(i) degi = deg[i] degj = deg[j] else: # directed version id, od, deg = degrees_dir(CIJ) i, j = np.where(CIJ > 0) K = len(i) if flag == 1: degi = od[i] degj = id[j] elif flag == 2: degi = id[i] degj = od[j] elif flag == 3: degi = od[i] degj = od[j] elif flag == 4: degi = id[i] degj = id[j] else: raise ValueError('Flag must be 0-4') # compute assortativity term1 = np.sum(degi * degj) / K term2 = np.square(np.sum(.5 * (degi + degj)) / K) term3 = np.sum(.5 * (degi * degi + degj * degj)) / K r = (term1 - term2) / (term3 - term2) return r
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:sort_coords; 3, parameters; 3, 4; 4, identifier:coord; 5, block; 5, 6; 5, 8; 5, 11; 5, 29; 5, 40; 6, expression_statement; 6, 7; 7, comment; 8, import_statement; 8, 9; 9, dotted_name; 9, 10; 10, identifier:iris; 11, expression_statement; 11, 12; 12, assignment; 12, 13; 12, 14; 13, identifier:order; 14, dictionary; 14, 15; 14, 19; 14, 23; 14, 26; 15, pair; 15, 16; 15, 17; 16, string:'T'; 17, unary_operator:-; 17, 18; 18, integer:2; 19, pair; 19, 20; 19, 21; 20, string:'Z'; 21, unary_operator:-; 21, 22; 22, integer:1; 23, pair; 23, 24; 23, 25; 24, string:'X'; 25, integer:1; 26, pair; 26, 27; 26, 28; 27, string:'Y'; 28, integer:2; 29, expression_statement; 29, 30; 30, assignment; 30, 31; 30, 32; 31, identifier:axis; 32, call; 32, 33; 32, 38; 33, attribute; 33, 34; 33, 37; 34, attribute; 34, 35; 34, 36; 35, identifier:iris; 36, identifier:util; 37, identifier:guess_coord_axis; 38, argument_list; 38, 39; 39, identifier:coord; 40, return_statement; 40, 41; 41, tuple; 41, 42; 41, 49; 42, call; 42, 43; 42, 46; 43, attribute; 43, 44; 43, 45; 44, identifier:order; 45, identifier:get; 46, argument_list; 46, 47; 46, 48; 47, identifier:axis; 48, integer:0; 49, boolean_operator:and; 49, 50; 49, 51; 50, identifier:coord; 51, call; 51, 52; 51, 55; 52, attribute; 52, 53; 52, 54; 53, identifier:coord; 54, identifier:name; 55, argument_list
def sort_coords(coord): """ Sorts a list of DimCoords trying to ensure that dates and pressure levels appear first and the longitude and latitude appear last in the correct order. """ import iris order = {'T': -2, 'Z': -1, 'X': 1, 'Y': 2} axis = iris.util.guess_coord_axis(coord) return (order.get(axis, 0), coord and coord.name())
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:separate_groups; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:groups; 5, identifier:key; 6, identifier:total; 7, block; 7, 8; 7, 10; 7, 23; 7, 35; 7, 36; 7, 44; 7, 45; 7, 46; 7, 61; 7, 76; 7, 82; 7, 88; 8, expression_statement; 8, 9; 9, comment; 10, expression_statement; 10, 11; 11, assignment; 11, 12; 11, 15; 12, pattern_list; 12, 13; 12, 14; 13, identifier:optimum; 14, identifier:extra; 15, call; 15, 16; 15, 17; 16, identifier:compute_optimum; 17, argument_list; 17, 18; 17, 22; 18, call; 18, 19; 18, 20; 19, identifier:len; 20, argument_list; 20, 21; 21, identifier:groups; 22, identifier:total; 23, expression_statement; 23, 24; 24, assignment; 24, 25; 24, 29; 25, pattern_list; 25, 26; 25, 27; 25, 28; 26, identifier:over_loaded; 27, identifier:under_loaded; 28, identifier:optimal; 29, call; 29, 30; 29, 31; 30, identifier:_smart_separate_groups; 31, argument_list; 31, 32; 31, 33; 31, 34; 32, identifier:groups; 33, identifier:key; 34, identifier:total; 35, comment; 36, if_statement; 36, 37; 36, 39; 37, not_operator; 37, 38; 38, identifier:extra; 39, block; 39, 40; 40, return_statement; 40, 41; 41, expression_list; 41, 42; 41, 43; 42, identifier:over_loaded; 43, identifier:under_loaded; 44, comment; 45, comment; 46, expression_statement; 46, 47; 47, assignment; 47, 48; 47, 49; 48, identifier:potential_under_loaded; 49, list_comprehension; 49, 50; 49, 51; 49, 54; 50, identifier:group; 51, for_in_clause; 51, 52; 51, 53; 52, identifier:group; 53, identifier:optimal; 54, if_clause; 54, 55; 55, comparison_operator:==; 55, 56; 55, 60; 56, call; 56, 57; 56, 58; 57, identifier:key; 58, argument_list; 58, 59; 59, identifier:group; 60, identifier:optimum; 61, expression_statement; 61, 62; 62, assignment; 62, 63; 62, 64; 63, identifier:potential_over_loaded; 64, list_comprehension; 64, 65; 64, 66; 64, 69; 65, identifier:group; 66, for_in_clause; 66, 67; 66, 68; 67, identifier:group; 68, identifier:optimal; 69, if_clause; 69, 70; 70, comparison_operator:>; 70, 71; 70, 75; 71, call; 71, 72; 71, 73; 72, identifier:key; 73, argument_list; 73, 74; 74, identifier:group; 75, identifier:optimum; 76, expression_statement; 76, 77; 77, assignment; 77, 78; 77, 79; 78, identifier:revised_under_loaded; 79, binary_operator:+; 79, 80; 79, 81; 80, identifier:under_loaded; 81, identifier:potential_under_loaded; 82, expression_statement; 82, 83; 83, assignment; 83, 84; 83, 85; 84, identifier:revised_over_loaded; 85, binary_operator:+; 85, 86; 85, 87; 86, identifier:over_loaded; 87, identifier:potential_over_loaded; 88, return_statement; 88, 89; 89, tuple; 89, 90; 89, 100; 90, call; 90, 91; 90, 92; 91, identifier:sorted; 92, argument_list; 92, 93; 92, 94; 92, 97; 93, identifier:revised_over_loaded; 94, keyword_argument; 94, 95; 94, 96; 95, identifier:key; 96, identifier:key; 97, keyword_argument; 97, 98; 97, 99; 98, identifier:reverse; 99, True; 100, call; 100, 101; 100, 102; 101, identifier:sorted; 102, argument_list; 102, 103; 102, 104; 103, identifier:revised_under_loaded; 104, keyword_argument; 104, 105; 104, 106; 105, identifier:key; 106, identifier:key
def separate_groups(groups, key, total): """Separate the group into overloaded and under-loaded groups. The revised over-loaded groups increases the choice space for future selection of most suitable group based on search criteria. For example: Given the groups (a:4, b:4, c:3, d:2) where the number represents the number of elements for each group. smart_separate_groups sets 'a' and 'c' as optimal, 'b' as over-loaded and 'd' as under-loaded. separate-groups combines 'a' with 'b' as over-loaded, allowing to select between these two groups to transfer the element to 'd'. :param groups: list of groups :param key: function to retrieve element count from group :param total: total number of elements to distribute :returns: sorted lists of over loaded (descending) and under loaded (ascending) group """ optimum, extra = compute_optimum(len(groups), total) over_loaded, under_loaded, optimal = _smart_separate_groups(groups, key, total) # If every group is optimal return if not extra: return over_loaded, under_loaded # Some groups in optimal may have a number of elements that is optimum + 1. # In this case they should be considered over_loaded. potential_under_loaded = [ group for group in optimal if key(group) == optimum ] potential_over_loaded = [ group for group in optimal if key(group) > optimum ] revised_under_loaded = under_loaded + potential_under_loaded revised_over_loaded = over_loaded + potential_over_loaded return ( sorted(revised_over_loaded, key=key, reverse=True), sorted(revised_under_loaded, key=key), )
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 17; 2, function_name:zrange; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 3, 11; 3, 14; 4, identifier:self; 5, identifier:name; 6, identifier:start; 7, identifier:end; 8, default_parameter; 8, 9; 8, 10; 9, identifier:desc; 10, False; 11, default_parameter; 11, 12; 11, 13; 12, identifier:withscores; 13, False; 14, default_parameter; 14, 15; 14, 16; 15, identifier:score_cast_func; 16, identifier:float; 17, block; 17, 18; 17, 20; 17, 35; 17, 43; 17, 56; 17, 66; 18, expression_statement; 18, 19; 19, comment; 20, if_statement; 20, 21; 20, 22; 21, identifier:desc; 22, block; 22, 23; 23, return_statement; 23, 24; 24, await; 24, 25; 25, call; 25, 26; 25, 29; 26, attribute; 26, 27; 26, 28; 27, identifier:self; 28, identifier:zrevrange; 29, argument_list; 29, 30; 29, 31; 29, 32; 29, 33; 29, 34; 30, identifier:name; 31, identifier:start; 32, identifier:end; 33, identifier:withscores; 34, identifier:score_cast_func; 35, expression_statement; 35, 36; 36, assignment; 36, 37; 36, 38; 37, identifier:pieces; 38, list:['ZRANGE', name, start, end]; 38, 39; 38, 40; 38, 41; 38, 42; 39, string:'ZRANGE'; 40, identifier:name; 41, identifier:start; 42, identifier:end; 43, if_statement; 43, 44; 43, 45; 44, identifier:withscores; 45, block; 45, 46; 46, expression_statement; 46, 47; 47, call; 47, 48; 47, 51; 48, attribute; 48, 49; 48, 50; 49, identifier:pieces; 50, identifier:append; 51, argument_list; 51, 52; 52, call; 52, 53; 52, 54; 53, identifier:b; 54, argument_list; 54, 55; 55, string:'WITHSCORES'; 56, expression_statement; 56, 57; 57, assignment; 57, 58; 57, 59; 58, identifier:options; 59, dictionary; 59, 60; 59, 63; 60, pair; 60, 61; 60, 62; 61, string:'withscores'; 62, identifier:withscores; 63, pair; 63, 64; 63, 65; 64, string:'score_cast_func'; 65, identifier:score_cast_func; 66, return_statement; 66, 67; 67, await; 67, 68; 68, call; 68, 69; 68, 72; 69, attribute; 69, 70; 69, 71; 70, identifier:self; 71, identifier:execute_command; 72, argument_list; 72, 73; 72, 75; 73, list_splat; 73, 74; 74, identifier:pieces; 75, dictionary_splat; 75, 76; 76, identifier:options
async def zrange(self, name, start, end, desc=False, withscores=False, score_cast_func=float): """ Return a range of values from sorted set ``name`` between ``start`` and ``end`` sorted in ascending order. ``start`` and ``end`` can be negative, indicating the end of the range. ``desc`` a boolean indicating whether to sort the results descendingly ``withscores`` indicates to return the scores along with the values. The return type is a list of (value, score) pairs ``score_cast_func`` a callable used to cast the score return value """ if desc: return await self.zrevrange(name, start, end, withscores, score_cast_func) pieces = ['ZRANGE', name, start, end] if withscores: pieces.append(b('WITHSCORES')) options = { 'withscores': withscores, 'score_cast_func': score_cast_func } return await self.execute_command(*pieces, **options)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:zremrangebyscore; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 4, identifier:self; 5, identifier:name; 6, identifier:min; 7, identifier:max; 8, block; 8, 9; 8, 11; 9, expression_statement; 9, 10; 10, comment; 11, return_statement; 11, 12; 12, await; 12, 13; 13, call; 13, 14; 13, 17; 14, attribute; 14, 15; 14, 16; 15, identifier:self; 16, identifier:execute_command; 17, argument_list; 17, 18; 17, 19; 17, 20; 17, 21; 18, string:'ZREMRANGEBYSCORE'; 19, identifier:name; 20, identifier:min; 21, identifier:max
async def zremrangebyscore(self, name, min, max): """ Remove all elements in the sorted set ``name`` with scores between ``min`` and ``max``. Returns the number of elements removed. """ return await self.execute_command('ZREMRANGEBYSCORE', name, min, max)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 33; 2, function_name:georadius; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 3, 9; 3, 12; 3, 15; 3, 18; 3, 21; 3, 24; 3, 27; 3, 30; 4, identifier:self; 5, identifier:name; 6, identifier:longitude; 7, identifier:latitude; 8, identifier:radius; 9, default_parameter; 9, 10; 9, 11; 10, identifier:unit; 11, None; 12, default_parameter; 12, 13; 12, 14; 13, identifier:withdist; 14, False; 15, default_parameter; 15, 16; 15, 17; 16, identifier:withcoord; 17, False; 18, default_parameter; 18, 19; 18, 20; 19, identifier:withhash; 20, False; 21, default_parameter; 21, 22; 21, 23; 22, identifier:count; 23, None; 24, default_parameter; 24, 25; 24, 26; 25, identifier:sort; 26, None; 27, default_parameter; 27, 28; 27, 29; 28, identifier:store; 29, None; 30, default_parameter; 30, 31; 30, 32; 31, identifier:store_dist; 32, None; 33, block; 33, 34; 33, 36; 34, expression_statement; 34, 35; 35, comment; 36, return_statement; 36, 37; 37, await; 37, 38; 38, call; 38, 39; 38, 42; 39, attribute; 39, 40; 39, 41; 40, identifier:self; 41, identifier:_georadiusgeneric; 42, argument_list; 42, 43; 42, 44; 42, 45; 42, 46; 42, 47; 42, 48; 42, 51; 42, 54; 42, 57; 42, 60; 42, 63; 42, 66; 42, 69; 43, string:'GEORADIUS'; 44, identifier:name; 45, identifier:longitude; 46, identifier:latitude; 47, identifier:radius; 48, keyword_argument; 48, 49; 48, 50; 49, identifier:unit; 50, identifier:unit; 51, keyword_argument; 51, 52; 51, 53; 52, identifier:withdist; 53, identifier:withdist; 54, keyword_argument; 54, 55; 54, 56; 55, identifier:withcoord; 56, identifier:withcoord; 57, keyword_argument; 57, 58; 57, 59; 58, identifier:withhash; 59, identifier:withhash; 60, keyword_argument; 60, 61; 60, 62; 61, identifier:count; 62, identifier:count; 63, keyword_argument; 63, 64; 63, 65; 64, identifier:sort; 65, identifier:sort; 66, keyword_argument; 66, 67; 66, 68; 67, identifier:store; 68, identifier:store; 69, keyword_argument; 69, 70; 69, 71; 70, identifier:store_dist; 71, identifier:store_dist
async def georadius(self, name, longitude, latitude, radius, unit=None, withdist=False, withcoord=False, withhash=False, count=None, sort=None, store=None, store_dist=None): """ Return the members of the specified key identified by the ``name`` argument which are within the borders of the area specified with the ``latitude`` and ``longitude`` location and the maximum distance from the center specified by the ``radius`` value. The units must be one of the following : m, km mi, ft. By default ``withdist`` indicates to return the distances of each place. ``withcoord`` indicates to return the latitude and longitude of each place. ``withhash`` indicates to return the geohash string of each place. ``count`` indicates to return the number of elements up to N. ``sort`` indicates to return the places in a sorted way, ASC for nearest to fairest and DESC for fairest to nearest. ``store`` indicates to save the places names in a sorted set named with a specific key, each element of the destination sorted set is populated with the score got from the original geo sorted set. ``store_dist`` indicates to save the places names in a sorted set named with a specific key, instead of ``store`` the sorted set destination score is set with the distance. """ return await self._georadiusgeneric('GEORADIUS', name, longitude, latitude, radius, unit=unit, withdist=withdist, withcoord=withcoord, withhash=withhash, count=count, sort=sort, store=store, store_dist=store_dist)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 32; 2, function_name:georadiusbymember; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 3, 11; 3, 14; 3, 17; 3, 20; 3, 23; 3, 26; 3, 29; 4, identifier:self; 5, identifier:name; 6, identifier:member; 7, identifier:radius; 8, default_parameter; 8, 9; 8, 10; 9, identifier:unit; 10, None; 11, default_parameter; 11, 12; 11, 13; 12, identifier:withdist; 13, False; 14, default_parameter; 14, 15; 14, 16; 15, identifier:withcoord; 16, False; 17, default_parameter; 17, 18; 17, 19; 18, identifier:withhash; 19, False; 20, default_parameter; 20, 21; 20, 22; 21, identifier:count; 22, None; 23, default_parameter; 23, 24; 23, 25; 24, identifier:sort; 25, None; 26, default_parameter; 26, 27; 26, 28; 27, identifier:store; 28, None; 29, default_parameter; 29, 30; 29, 31; 30, identifier:store_dist; 31, None; 32, block; 32, 33; 32, 35; 33, expression_statement; 33, 34; 34, comment; 35, return_statement; 35, 36; 36, await; 36, 37; 37, call; 37, 38; 37, 41; 38, attribute; 38, 39; 38, 40; 39, identifier:self; 40, identifier:_georadiusgeneric; 41, argument_list; 41, 42; 41, 43; 41, 44; 41, 45; 41, 46; 41, 49; 41, 52; 41, 55; 41, 58; 41, 61; 41, 64; 41, 67; 42, string:'GEORADIUSBYMEMBER'; 43, identifier:name; 44, identifier:member; 45, identifier:radius; 46, keyword_argument; 46, 47; 46, 48; 47, identifier:unit; 48, identifier:unit; 49, keyword_argument; 49, 50; 49, 51; 50, identifier:withdist; 51, identifier:withdist; 52, keyword_argument; 52, 53; 52, 54; 53, identifier:withcoord; 54, identifier:withcoord; 55, keyword_argument; 55, 56; 55, 57; 56, identifier:withhash; 57, identifier:withhash; 58, keyword_argument; 58, 59; 58, 60; 59, identifier:count; 60, identifier:count; 61, keyword_argument; 61, 62; 61, 63; 62, identifier:sort; 63, identifier:sort; 64, keyword_argument; 64, 65; 64, 66; 65, identifier:store; 66, identifier:store; 67, keyword_argument; 67, 68; 67, 69; 68, identifier:store_dist; 69, identifier:store_dist
async def georadiusbymember(self, name, member, radius, unit=None, withdist=False, withcoord=False, withhash=False, count=None, sort=None, store=None, store_dist=None): """ This command is exactly like ``georadius`` with the sole difference that instead of taking, as the center of the area to query, a longitude and latitude value, it takes the name of a member already existing inside the geospatial index represented by the sorted set. """ return await self._georadiusgeneric('GEORADIUSBYMEMBER', name, member, radius, unit=unit, withdist=withdist, withcoord=withcoord, withhash=withhash, count=count, sort=sort, store=store, store_dist=store_dist)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 32; 2, function_name:search; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 14; 3, 17; 3, 20; 3, 23; 3, 26; 3, 29; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:page; 7, integer:0; 8, default_parameter; 8, 9; 8, 10; 9, identifier:per_page; 10, integer:50; 11, default_parameter; 11, 12; 11, 13; 12, identifier:sort; 13, None; 14, default_parameter; 14, 15; 14, 16; 15, identifier:q; 16, None; 17, default_parameter; 17, 18; 17, 19; 18, identifier:include_totals; 19, True; 20, default_parameter; 20, 21; 20, 22; 21, identifier:fields; 22, None; 23, default_parameter; 23, 24; 23, 25; 24, identifier:from_param; 25, None; 26, default_parameter; 26, 27; 26, 28; 27, identifier:take; 28, None; 29, default_parameter; 29, 30; 29, 31; 30, identifier:include_fields; 31, True; 32, block; 32, 33; 32, 35; 32, 89; 33, expression_statement; 33, 34; 34, comment; 35, expression_statement; 35, 36; 36, assignment; 36, 37; 36, 38; 37, identifier:params; 38, dictionary; 38, 39; 38, 42; 38, 45; 38, 55; 38, 58; 38, 70; 38, 80; 38, 83; 38, 86; 39, pair; 39, 40; 39, 41; 40, string:'per_page'; 41, identifier:per_page; 42, pair; 42, 43; 42, 44; 43, string:'page'; 44, identifier:page; 45, pair; 45, 46; 45, 47; 46, string:'include_totals'; 47, call; 47, 48; 47, 54; 48, attribute; 48, 49; 48, 53; 49, call; 49, 50; 49, 51; 50, identifier:str; 51, argument_list; 51, 52; 52, identifier:include_totals; 53, identifier:lower; 54, argument_list; 55, pair; 55, 56; 55, 57; 56, string:'sort'; 57, identifier:sort; 58, pair; 58, 59; 58, 60; 59, string:'fields'; 60, boolean_operator:or; 60, 61; 60, 69; 61, boolean_operator:and; 61, 62; 61, 63; 62, identifier:fields; 63, call; 63, 64; 63, 67; 64, attribute; 64, 65; 64, 66; 65, string:','; 66, identifier:join; 67, argument_list; 67, 68; 68, identifier:fields; 69, None; 70, pair; 70, 71; 70, 72; 71, string:'include_fields'; 72, call; 72, 73; 72, 79; 73, attribute; 73, 74; 73, 78; 74, call; 74, 75; 74, 76; 75, identifier:str; 76, argument_list; 76, 77; 77, identifier:include_fields; 78, identifier:lower; 79, argument_list; 80, pair; 80, 81; 80, 82; 81, string:'q'; 82, identifier:q; 83, pair; 83, 84; 83, 85; 84, string:'from'; 85, identifier:from_param; 86, pair; 86, 87; 86, 88; 87, string:'take'; 88, identifier:take; 89, return_statement; 89, 90; 90, call; 90, 91; 90, 96; 91, attribute; 91, 92; 91, 95; 92, attribute; 92, 93; 92, 94; 93, identifier:self; 94, identifier:client; 95, identifier:get; 96, argument_list; 96, 97; 96, 102; 97, call; 97, 98; 97, 101; 98, attribute; 98, 99; 98, 100; 99, identifier:self; 100, identifier:_url; 101, argument_list; 102, keyword_argument; 102, 103; 102, 104; 103, identifier:params; 104, identifier:params
def search(self, page=0, per_page=50, sort=None, q=None, include_totals=True, fields=None, from_param=None, take=None, include_fields=True): """Search log events. Args: page (int, optional): The result's page number (zero based). per_page (int, optional): The amount of entries per page. sort (str, optional): The field to use for sorting. 1 == ascending and -1 == descending. (e.g: date:1) q (str, optional): Query in Lucene query string syntax. fields (list of str, optional): A list of fields to include or exclude from the result (depending on include_fields). Empty to retrieve all fields. include_fields (bool, optional): True if the fields specified are to be included in the result, False otherwise. include_totals (bool, optional): True if the query summary is to be included in the result, False otherwise. from_param (str, optional): Log Event Id to start retrieving logs. You can limit the amount of logs using the take parameter take (int, optional): The total amount of entries to retrieve when using the from parameter. https://auth0.com/docs/api/management/v2#!/Logs/get_logs """ params = { 'per_page': per_page, 'page': page, 'include_totals': str(include_totals).lower(), 'sort': sort, 'fields': fields and ','.join(fields) or None, 'include_fields': str(include_fields).lower(), 'q': q, 'from': from_param, 'take': take } return self.client.get(self._url(), params=params)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 32; 2, function_name:list; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 14; 3, 17; 3, 20; 3, 23; 3, 26; 3, 29; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:page; 7, integer:0; 8, default_parameter; 8, 9; 8, 10; 9, identifier:per_page; 10, integer:25; 11, default_parameter; 11, 12; 11, 13; 12, identifier:sort; 13, None; 14, default_parameter; 14, 15; 14, 16; 15, identifier:connection; 16, None; 17, default_parameter; 17, 18; 17, 19; 18, identifier:q; 19, None; 20, default_parameter; 20, 21; 20, 22; 21, identifier:search_engine; 22, None; 23, default_parameter; 23, 24; 23, 25; 24, identifier:include_totals; 25, True; 26, default_parameter; 26, 27; 26, 28; 27, identifier:fields; 28, None; 29, default_parameter; 29, 30; 29, 31; 30, identifier:include_fields; 31, True; 32, block; 32, 33; 32, 35; 32, 89; 33, expression_statement; 33, 34; 34, comment; 35, expression_statement; 35, 36; 36, assignment; 36, 37; 36, 38; 37, identifier:params; 38, dictionary; 38, 39; 38, 42; 38, 45; 38, 55; 38, 58; 38, 61; 38, 73; 38, 83; 38, 86; 39, pair; 39, 40; 39, 41; 40, string:'per_page'; 41, identifier:per_page; 42, pair; 42, 43; 42, 44; 43, string:'page'; 44, identifier:page; 45, pair; 45, 46; 45, 47; 46, string:'include_totals'; 47, call; 47, 48; 47, 54; 48, attribute; 48, 49; 48, 53; 49, call; 49, 50; 49, 51; 50, identifier:str; 51, argument_list; 51, 52; 52, identifier:include_totals; 53, identifier:lower; 54, argument_list; 55, pair; 55, 56; 55, 57; 56, string:'sort'; 57, identifier:sort; 58, pair; 58, 59; 58, 60; 59, string:'connection'; 60, identifier:connection; 61, pair; 61, 62; 61, 63; 62, string:'fields'; 63, boolean_operator:or; 63, 64; 63, 72; 64, boolean_operator:and; 64, 65; 64, 66; 65, identifier:fields; 66, call; 66, 67; 66, 70; 67, attribute; 67, 68; 67, 69; 68, string:','; 69, identifier:join; 70, argument_list; 70, 71; 71, identifier:fields; 72, None; 73, pair; 73, 74; 73, 75; 74, string:'include_fields'; 75, call; 75, 76; 75, 82; 76, attribute; 76, 77; 76, 81; 77, call; 77, 78; 77, 79; 78, identifier:str; 79, argument_list; 79, 80; 80, identifier:include_fields; 81, identifier:lower; 82, argument_list; 83, pair; 83, 84; 83, 85; 84, string:'q'; 85, identifier:q; 86, pair; 86, 87; 86, 88; 87, string:'search_engine'; 88, identifier:search_engine; 89, return_statement; 89, 90; 90, call; 90, 91; 90, 96; 91, attribute; 91, 92; 91, 95; 92, attribute; 92, 93; 92, 94; 93, identifier:self; 94, identifier:client; 95, identifier:get; 96, argument_list; 96, 97; 96, 102; 97, call; 97, 98; 97, 101; 98, attribute; 98, 99; 98, 100; 99, identifier:self; 100, identifier:_url; 101, argument_list; 102, keyword_argument; 102, 103; 102, 104; 103, identifier:params; 104, identifier:params
def list(self, page=0, per_page=25, sort=None, connection=None, q=None, search_engine=None, include_totals=True, fields=None, include_fields=True): """List or search users. Args: page (int, optional): The result's page number (zero based). per_page (int, optional): The amount of entries per page. sort (str, optional): The field to use for sorting. 1 == ascending and -1 == descending. (e.g: email:1) connection (str, optional): Connection filter. q (str, optional): Query in Lucene query string syntax. Only fields in app_metadata, user_metadata or the normalized user profile are searchable. search_engine (str, optional): The version of the search_engine to use when querying for users. Will default to the latest version available. See: https://auth0.com/docs/users/search fields (list of str, optional): A list of fields to include or exclude from the result (depending on include_fields). Empty to retrieve all fields. include_fields (bool, optional): True if the fields specified are to be include in the result, False otherwise. See: https://auth0.com/docs/api/management/v2#!/Users/get_users """ params = { 'per_page': per_page, 'page': page, 'include_totals': str(include_totals).lower(), 'sort': sort, 'connection': connection, 'fields': fields and ','.join(fields) or None, 'include_fields': str(include_fields).lower(), 'q': q, 'search_engine': search_engine } return self.client.get(self._url(), params=params)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 18; 2, function_name:get_log_events; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 3, 12; 3, 15; 4, identifier:self; 5, identifier:user_id; 6, default_parameter; 6, 7; 6, 8; 7, identifier:page; 8, integer:0; 9, default_parameter; 9, 10; 9, 11; 10, identifier:per_page; 11, integer:50; 12, default_parameter; 12, 13; 12, 14; 13, identifier:sort; 14, None; 15, default_parameter; 15, 16; 15, 17; 16, identifier:include_totals; 17, False; 18, block; 18, 19; 18, 21; 18, 44; 18, 58; 19, expression_statement; 19, 20; 20, comment; 21, expression_statement; 21, 22; 22, assignment; 22, 23; 22, 24; 23, identifier:params; 24, dictionary; 24, 25; 24, 28; 24, 31; 24, 41; 25, pair; 25, 26; 25, 27; 26, string:'per_page'; 27, identifier:per_page; 28, pair; 28, 29; 28, 30; 29, string:'page'; 30, identifier:page; 31, pair; 31, 32; 31, 33; 32, string:'include_totals'; 33, call; 33, 34; 33, 40; 34, attribute; 34, 35; 34, 39; 35, call; 35, 36; 35, 37; 36, identifier:str; 37, argument_list; 37, 38; 38, identifier:include_totals; 39, identifier:lower; 40, argument_list; 41, pair; 41, 42; 41, 43; 42, string:'sort'; 43, identifier:sort; 44, expression_statement; 44, 45; 45, assignment; 45, 46; 45, 47; 46, identifier:url; 47, call; 47, 48; 47, 51; 48, attribute; 48, 49; 48, 50; 49, identifier:self; 50, identifier:_url; 51, argument_list; 51, 52; 52, call; 52, 53; 52, 56; 53, attribute; 53, 54; 53, 55; 54, string:'{}/logs'; 55, identifier:format; 56, argument_list; 56, 57; 57, identifier:user_id; 58, return_statement; 58, 59; 59, call; 59, 60; 59, 65; 60, attribute; 60, 61; 60, 64; 61, attribute; 61, 62; 61, 63; 62, identifier:self; 63, identifier:client; 64, identifier:get; 65, argument_list; 65, 66; 65, 67; 66, identifier:url; 67, keyword_argument; 67, 68; 67, 69; 68, identifier:params; 69, identifier:params
def get_log_events(self, user_id, page=0, per_page=50, sort=None, include_totals=False): """Retrieve every log event for a specific user id Args: user_id (str): The user_id of the logs to retrieve page (int, optional): The result's page number (zero based). per_page (int, optional): The amount of entries per page. Default: 50. Max value: 100 sort (str, optional): The field to use for sorting. Use field:order where order is 1 for ascending and -1 for descending. For example date:-1 include_totals (bool, optional): True if the query summary is to be included in the result, False otherwise. See: https://auth0.com/docs/api/management/v2#!/Users/get_logs_by_user """ params = { 'per_page': per_page, 'page': page, 'include_totals': str(include_totals).lower(), 'sort': sort } url = self._url('{}/logs'.format(user_id)) return self.client.get(url, params=params)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 14; 2, function_name:get_activities; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:before; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:after; 10, None; 11, default_parameter; 11, 12; 11, 13; 12, identifier:limit; 13, None; 14, block; 14, 15; 14, 17; 14, 29; 14, 41; 14, 53; 14, 69; 15, expression_statement; 15, 16; 16, comment; 17, if_statement; 17, 18; 17, 19; 18, identifier:before; 19, block; 19, 20; 20, expression_statement; 20, 21; 21, assignment; 21, 22; 21, 23; 22, identifier:before; 23, call; 23, 24; 23, 27; 24, attribute; 24, 25; 24, 26; 25, identifier:self; 26, identifier:_utc_datetime_to_epoch; 27, argument_list; 27, 28; 28, identifier:before; 29, if_statement; 29, 30; 29, 31; 30, identifier:after; 31, block; 31, 32; 32, expression_statement; 32, 33; 33, assignment; 33, 34; 33, 35; 34, identifier:after; 35, call; 35, 36; 35, 39; 36, attribute; 36, 37; 36, 38; 37, identifier:self; 38, identifier:_utc_datetime_to_epoch; 39, argument_list; 39, 40; 40, identifier:after; 41, expression_statement; 41, 42; 42, assignment; 42, 43; 42, 44; 43, identifier:params; 44, call; 44, 45; 44, 46; 45, identifier:dict; 46, argument_list; 46, 47; 46, 50; 47, keyword_argument; 47, 48; 47, 49; 48, identifier:before; 49, identifier:before; 50, keyword_argument; 50, 51; 50, 52; 51, identifier:after; 52, identifier:after; 53, expression_statement; 53, 54; 54, assignment; 54, 55; 54, 56; 55, identifier:result_fetcher; 56, call; 56, 57; 56, 60; 57, attribute; 57, 58; 57, 59; 58, identifier:functools; 59, identifier:partial; 60, argument_list; 60, 61; 60, 66; 60, 67; 61, attribute; 61, 62; 61, 65; 62, attribute; 62, 63; 62, 64; 63, identifier:self; 64, identifier:protocol; 65, identifier:get; 66, string:'/athlete/activities'; 67, dictionary_splat; 67, 68; 68, identifier:params; 69, return_statement; 69, 70; 70, call; 70, 71; 70, 72; 71, identifier:BatchedResultsIterator; 72, argument_list; 72, 73; 72, 78; 72, 81; 72, 84; 73, keyword_argument; 73, 74; 73, 75; 74, identifier:entity; 75, attribute; 75, 76; 75, 77; 76, identifier:model; 77, identifier:Activity; 78, keyword_argument; 78, 79; 78, 80; 79, identifier:bind_client; 80, identifier:self; 81, keyword_argument; 81, 82; 81, 83; 82, identifier:result_fetcher; 83, identifier:result_fetcher; 84, keyword_argument; 84, 85; 84, 86; 85, identifier:limit; 86, identifier:limit
def get_activities(self, before=None, after=None, limit=None): """ Get activities for authenticated user sorted by newest first. http://strava.github.io/api/v3/activities/ :param before: Result will start with activities whose start date is before specified date. (UTC) :type before: datetime.datetime or str or None :param after: Result will start with activities whose start date is after specified value. (UTC) :type after: datetime.datetime or str or None :param limit: How many maximum activities to return. :type limit: int or None :return: An iterator of :class:`stravalib.model.Activity` objects. :rtype: :class:`BatchedResultsIterator` """ if before: before = self._utc_datetime_to_epoch(before) if after: after = self._utc_datetime_to_epoch(after) params = dict(before=before, after=after) result_fetcher = functools.partial(self.protocol.get, '/athlete/activities', **params) return BatchedResultsIterator(entity=model.Activity, bind_client=self, result_fetcher=result_fetcher, limit=limit)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 33; 2, function_name:get_segment_leaderboard; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 3, 12; 3, 15; 3, 18; 3, 21; 3, 24; 3, 27; 3, 30; 4, identifier:self; 5, identifier:segment_id; 6, default_parameter; 6, 7; 6, 8; 7, identifier:gender; 8, None; 9, default_parameter; 9, 10; 9, 11; 10, identifier:age_group; 11, None; 12, default_parameter; 12, 13; 12, 14; 13, identifier:weight_class; 14, None; 15, default_parameter; 15, 16; 15, 17; 16, identifier:following; 17, None; 18, default_parameter; 18, 19; 18, 20; 19, identifier:club_id; 20, None; 21, default_parameter; 21, 22; 21, 23; 22, identifier:timeframe; 23, None; 24, default_parameter; 24, 25; 24, 26; 25, identifier:top_results_limit; 26, None; 27, default_parameter; 27, 28; 27, 29; 28, identifier:page; 29, None; 30, default_parameter; 30, 31; 30, 32; 31, identifier:context_entries; 32, None; 33, block; 33, 34; 33, 36; 33, 40; 33, 72; 33, 82; 33, 110; 33, 126; 33, 154; 33, 168; 33, 179; 33, 215; 33, 226; 33, 237; 33, 248; 34, expression_statement; 34, 35; 35, comment; 36, expression_statement; 36, 37; 37, assignment; 37, 38; 37, 39; 38, identifier:params; 39, dictionary; 40, if_statement; 40, 41; 40, 44; 41, comparison_operator:is; 41, 42; 41, 43; 42, identifier:gender; 43, None; 44, block; 44, 45; 44, 66; 45, if_statement; 45, 46; 45, 55; 46, comparison_operator:not; 46, 47; 46, 52; 47, call; 47, 48; 47, 51; 48, attribute; 48, 49; 48, 50; 49, identifier:gender; 50, identifier:upper; 51, argument_list; 52, tuple; 52, 53; 52, 54; 53, string:'M'; 54, string:'F'; 55, block; 55, 56; 56, raise_statement; 56, 57; 57, call; 57, 58; 57, 59; 58, identifier:ValueError; 59, argument_list; 59, 60; 60, call; 60, 61; 60, 64; 61, attribute; 61, 62; 61, 63; 62, string:"Invalid gender: {0}. Possible values: 'M' or 'F'"; 63, identifier:format; 64, argument_list; 64, 65; 65, identifier:gender; 66, expression_statement; 66, 67; 67, assignment; 67, 68; 67, 71; 68, subscript; 68, 69; 68, 70; 69, identifier:params; 70, string:'gender'; 71, identifier:gender; 72, expression_statement; 72, 73; 73, assignment; 73, 74; 73, 75; 74, identifier:valid_age_groups; 75, tuple; 75, 76; 75, 77; 75, 78; 75, 79; 75, 80; 75, 81; 76, string:'0_24'; 77, string:'25_34'; 78, string:'35_44'; 79, string:'45_54'; 80, string:'55_64'; 81, string:'65_plus'; 82, if_statement; 82, 83; 82, 86; 83, comparison_operator:is; 83, 84; 83, 85; 84, identifier:age_group; 85, None; 86, block; 86, 87; 86, 104; 87, if_statement; 87, 88; 87, 92; 88, not_operator; 88, 89; 89, comparison_operator:in; 89, 90; 89, 91; 90, identifier:age_group; 91, identifier:valid_age_groups; 92, block; 92, 93; 93, raise_statement; 93, 94; 94, call; 94, 95; 94, 96; 95, identifier:ValueError; 96, argument_list; 96, 97; 97, call; 97, 98; 97, 101; 98, attribute; 98, 99; 98, 100; 99, string:"Invalid age group: {0}. Possible values: {1!r}"; 100, identifier:format; 101, argument_list; 101, 102; 101, 103; 102, identifier:age_group; 103, identifier:valid_age_groups; 104, expression_statement; 104, 105; 105, assignment; 105, 106; 105, 109; 106, subscript; 106, 107; 106, 108; 107, identifier:params; 108, string:'age_group'; 109, identifier:age_group; 110, expression_statement; 110, 111; 111, assignment; 111, 112; 111, 113; 112, identifier:valid_weight_classes; 113, tuple; 113, 114; 113, 115; 113, 116; 113, 117; 113, 118; 113, 119; 113, 120; 113, 121; 113, 122; 113, 123; 113, 124; 113, 125; 114, string:'0_124'; 115, string:'125_149'; 116, string:'150_164'; 117, string:'165_179'; 118, string:'180_199'; 119, string:'200_plus'; 120, string:'0_54'; 121, string:'55_64'; 122, string:'65_74'; 123, string:'75_84'; 124, string:'85_94'; 125, string:'95_plus'; 126, if_statement; 126, 127; 126, 130; 127, comparison_operator:is; 127, 128; 127, 129; 128, identifier:weight_class; 129, None; 130, block; 130, 131; 130, 148; 131, if_statement; 131, 132; 131, 136; 132, not_operator; 132, 133; 133, comparison_operator:in; 133, 134; 133, 135; 134, identifier:weight_class; 135, identifier:valid_weight_classes; 136, block; 136, 137; 137, raise_statement; 137, 138; 138, call; 138, 139; 138, 140; 139, identifier:ValueError; 140, argument_list; 140, 141; 141, call; 141, 142; 141, 145; 142, attribute; 142, 143; 142, 144; 143, string:"Invalid weight class: {0}. Possible values: {1!r}"; 144, identifier:format; 145, argument_list; 145, 146; 145, 147; 146, identifier:weight_class; 147, identifier:valid_weight_classes; 148, expression_statement; 148, 149; 149, assignment; 149, 150; 149, 153; 150, subscript; 150, 151; 150, 152; 151, identifier:params; 152, string:'weight_class'; 153, identifier:weight_class; 154, if_statement; 154, 155; 154, 158; 155, comparison_operator:is; 155, 156; 155, 157; 156, identifier:following; 157, None; 158, block; 158, 159; 159, expression_statement; 159, 160; 160, assignment; 160, 161; 160, 164; 161, subscript; 161, 162; 161, 163; 162, identifier:params; 163, string:'following'; 164, call; 164, 165; 164, 166; 165, identifier:int; 166, argument_list; 166, 167; 167, identifier:following; 168, if_statement; 168, 169; 168, 172; 169, comparison_operator:is; 169, 170; 169, 171; 170, identifier:club_id; 171, None; 172, block; 172, 173; 173, expression_statement; 173, 174; 174, assignment; 174, 175; 174, 178; 175, subscript; 175, 176; 175, 177; 176, identifier:params; 177, string:'club_id'; 178, identifier:club_id; 179, if_statement; 179, 180; 179, 183; 180, comparison_operator:is; 180, 181; 180, 182; 181, identifier:timeframe; 182, None; 183, block; 183, 184; 183, 192; 183, 209; 184, expression_statement; 184, 185; 185, assignment; 185, 186; 185, 187; 186, identifier:valid_timeframes; 187, expression_list; 187, 188; 187, 189; 187, 190; 187, 191; 188, string:'this_year'; 189, string:'this_month'; 190, string:'this_week'; 191, string:'today'; 192, if_statement; 192, 193; 192, 197; 193, not_operator; 193, 194; 194, comparison_operator:in; 194, 195; 194, 196; 195, identifier:timeframe; 196, identifier:valid_timeframes; 197, block; 197, 198; 198, raise_statement; 198, 199; 199, call; 199, 200; 199, 201; 200, identifier:ValueError; 201, argument_list; 201, 202; 202, call; 202, 203; 202, 206; 203, attribute; 203, 204; 203, 205; 204, string:"Invalid timeframe: {0}. Possible values: {1!r}"; 205, identifier:format; 206, argument_list; 206, 207; 206, 208; 207, identifier:timeframe; 208, identifier:valid_timeframes; 209, expression_statement; 209, 210; 210, assignment; 210, 211; 210, 214; 211, subscript; 211, 212; 211, 213; 212, identifier:params; 213, string:'date_range'; 214, identifier:timeframe; 215, if_statement; 215, 216; 215, 219; 216, comparison_operator:is; 216, 217; 216, 218; 217, identifier:top_results_limit; 218, None; 219, block; 219, 220; 220, expression_statement; 220, 221; 221, assignment; 221, 222; 221, 225; 222, subscript; 222, 223; 222, 224; 223, identifier:params; 224, string:'per_page'; 225, identifier:top_results_limit; 226, if_statement; 226, 227; 226, 230; 227, comparison_operator:is; 227, 228; 227, 229; 228, identifier:page; 229, None; 230, block; 230, 231; 231, expression_statement; 231, 232; 232, assignment; 232, 233; 232, 236; 233, subscript; 233, 234; 233, 235; 234, identifier:params; 235, string:'page'; 236, identifier:page; 237, if_statement; 237, 238; 237, 241; 238, comparison_operator:is; 238, 239; 238, 240; 239, identifier:context_entries; 240, None; 241, block; 241, 242; 242, expression_statement; 242, 243; 243, assignment; 243, 244; 243, 247; 244, subscript; 244, 245; 244, 246; 245, identifier:params; 246, string:'context_entries'; 247, identifier:context_entries; 248, return_statement; 248, 249; 249, call; 249, 250; 249, 255; 250, attribute; 250, 251; 250, 254; 251, attribute; 251, 252; 251, 253; 252, identifier:model; 253, identifier:SegmentLeaderboard; 254, identifier:deserialize; 255, argument_list; 255, 256; 255, 269; 256, call; 256, 257; 256, 262; 257, attribute; 257, 258; 257, 261; 258, attribute; 258, 259; 258, 260; 259, identifier:self; 260, identifier:protocol; 261, identifier:get; 262, argument_list; 262, 263; 262, 264; 262, 267; 263, string:'/segments/{id}/leaderboard'; 264, keyword_argument; 264, 265; 264, 266; 265, identifier:id; 266, identifier:segment_id; 267, dictionary_splat; 267, 268; 268, identifier:params; 269, keyword_argument; 269, 270; 269, 271; 270, identifier:bind_client; 271, identifier:self
def get_segment_leaderboard(self, segment_id, gender=None, age_group=None, weight_class=None, following=None, club_id=None, timeframe=None, top_results_limit=None, page=None, context_entries = None): """ Gets the leaderboard for a segment. http://strava.github.io/api/v3/segments/#leaderboard Note that by default Strava will return the top 10 results, and if the current user has ridden that segment, the current user's result along with the two results above in rank and the two results below will be included. The top X results can be configured by setting the top_results_limit parameter; however, the other 5 results will be included if the current user has ridden that segment. (i.e. if you specify top_results_limit=15, you will get a total of 20 entries back.) :param segment_id: ID of the segment. :type segment_id: int :param gender: (optional) 'M' or 'F' :type gender: str :param age_group: (optional) '0_24', '25_34', '35_44', '45_54', '55_64', '65_plus' :type age_group: str :param weight_class: (optional) pounds '0_124', '125_149', '150_164', '165_179', '180_199', '200_plus' or kilograms '0_54', '55_64', '65_74', '75_84', '85_94', '95_plus' :type weight_class: str :param following: (optional) Limit to athletes current user is following. :type following: bool :param club_id: (optional) limit to specific club :type club_id: int :param timeframe: (optional) 'this_year', 'this_month', 'this_week', 'today' :type timeframe: str :param top_results_limit: (optional, strava default is 10 + 5 from end) How many of leading leaderboard entries to display. See description for why this is a little confusing. :type top_results_limit: int :param page: (optional, strava default is 1) Page number of leaderboard to return, sorted by highest ranking leaders :type page: int :param context_entries: (optional, strava default is 2, max is 15) number of entries surrounding requesting athlete to return :type context_entries: int :return: The SegmentLeaderboard for the specified page (default: 1) :rtype: :class:`stravalib.model.SegmentLeaderboard` """ params = {} if gender is not None: if gender.upper() not in ('M', 'F'): raise ValueError("Invalid gender: {0}. Possible values: 'M' or 'F'".format(gender)) params['gender'] = gender valid_age_groups = ('0_24', '25_34', '35_44', '45_54', '55_64', '65_plus') if age_group is not None: if not age_group in valid_age_groups: raise ValueError("Invalid age group: {0}. Possible values: {1!r}".format(age_group, valid_age_groups)) params['age_group'] = age_group valid_weight_classes = ('0_124', '125_149', '150_164', '165_179', '180_199', '200_plus', '0_54', '55_64', '65_74', '75_84', '85_94', '95_plus') if weight_class is not None: if not weight_class in valid_weight_classes: raise ValueError("Invalid weight class: {0}. Possible values: {1!r}".format(weight_class, valid_weight_classes)) params['weight_class'] = weight_class if following is not None: params['following'] = int(following) if club_id is not None: params['club_id'] = club_id if timeframe is not None: valid_timeframes = 'this_year', 'this_month', 'this_week', 'today' if not timeframe in valid_timeframes: raise ValueError("Invalid timeframe: {0}. Possible values: {1!r}".format(timeframe, valid_timeframes)) params['date_range'] = timeframe if top_results_limit is not None: params['per_page'] = top_results_limit if page is not None: params['page'] = page if context_entries is not None: params['context_entries'] = context_entries return model.SegmentLeaderboard.deserialize(self.protocol.get('/segments/{id}/leaderboard', id=segment_id, **params), bind_client=self)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 18; 2, function_name:get_segment_efforts; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 3, 12; 3, 15; 4, identifier:self; 5, identifier:segment_id; 6, default_parameter; 6, 7; 6, 8; 7, identifier:athlete_id; 8, None; 9, default_parameter; 9, 10; 9, 11; 10, identifier:start_date_local; 11, None; 12, default_parameter; 12, 13; 12, 14; 13, identifier:end_date_local; 14, None; 15, default_parameter; 15, 16; 15, 17; 16, identifier:limit; 17, None; 18, block; 18, 19; 18, 21; 18, 28; 18, 39; 18, 73; 18, 107; 18, 118; 18, 134; 19, expression_statement; 19, 20; 20, comment; 21, expression_statement; 21, 22; 22, assignment; 22, 23; 22, 24; 23, identifier:params; 24, dictionary; 24, 25; 25, pair; 25, 26; 25, 27; 26, string:"segment_id"; 27, identifier:segment_id; 28, if_statement; 28, 29; 28, 32; 29, comparison_operator:is; 29, 30; 29, 31; 30, identifier:athlete_id; 31, None; 32, block; 32, 33; 33, expression_statement; 33, 34; 34, assignment; 34, 35; 34, 38; 35, subscript; 35, 36; 35, 37; 36, identifier:params; 37, string:'athlete_id'; 38, identifier:athlete_id; 39, if_statement; 39, 40; 39, 41; 40, identifier:start_date_local; 41, block; 41, 42; 41, 62; 42, if_statement; 42, 43; 42, 50; 43, call; 43, 44; 43, 45; 44, identifier:isinstance; 45, argument_list; 45, 46; 45, 47; 46, identifier:start_date_local; 47, attribute; 47, 48; 47, 49; 48, identifier:six; 49, identifier:string_types; 50, block; 50, 51; 51, expression_statement; 51, 52; 52, assignment; 52, 53; 52, 54; 53, identifier:start_date_local; 54, attribute; 54, 55; 54, 61; 55, call; 55, 56; 55, 59; 56, attribute; 56, 57; 56, 58; 57, identifier:arrow; 58, identifier:get; 59, argument_list; 59, 60; 60, identifier:start_date_local; 61, identifier:naive; 62, expression_statement; 62, 63; 63, assignment; 63, 64; 63, 67; 64, subscript; 64, 65; 64, 66; 65, identifier:params; 66, string:"start_date_local"; 67, call; 67, 68; 67, 71; 68, attribute; 68, 69; 68, 70; 69, identifier:start_date_local; 70, identifier:strftime; 71, argument_list; 71, 72; 72, string:"%Y-%m-%dT%H:%M:%SZ"; 73, if_statement; 73, 74; 73, 75; 74, identifier:end_date_local; 75, block; 75, 76; 75, 96; 76, if_statement; 76, 77; 76, 84; 77, call; 77, 78; 77, 79; 78, identifier:isinstance; 79, argument_list; 79, 80; 79, 81; 80, identifier:end_date_local; 81, attribute; 81, 82; 81, 83; 82, identifier:six; 83, identifier:string_types; 84, block; 84, 85; 85, expression_statement; 85, 86; 86, assignment; 86, 87; 86, 88; 87, identifier:end_date_local; 88, attribute; 88, 89; 88, 95; 89, call; 89, 90; 89, 93; 90, attribute; 90, 91; 90, 92; 91, identifier:arrow; 92, identifier:get; 93, argument_list; 93, 94; 94, identifier:end_date_local; 95, identifier:naive; 96, expression_statement; 96, 97; 97, assignment; 97, 98; 97, 101; 98, subscript; 98, 99; 98, 100; 99, identifier:params; 100, string:"end_date_local"; 101, call; 101, 102; 101, 105; 102, attribute; 102, 103; 102, 104; 103, identifier:end_date_local; 104, identifier:strftime; 105, argument_list; 105, 106; 106, string:"%Y-%m-%dT%H:%M:%SZ"; 107, if_statement; 107, 108; 107, 111; 108, comparison_operator:is; 108, 109; 108, 110; 109, identifier:limit; 110, None; 111, block; 111, 112; 112, expression_statement; 112, 113; 113, assignment; 113, 114; 113, 117; 114, subscript; 114, 115; 114, 116; 115, identifier:params; 116, string:"limit"; 117, identifier:limit; 118, expression_statement; 118, 119; 119, assignment; 119, 120; 119, 121; 120, identifier:result_fetcher; 121, call; 121, 122; 121, 125; 122, attribute; 122, 123; 122, 124; 123, identifier:functools; 124, identifier:partial; 125, argument_list; 125, 126; 125, 131; 125, 132; 126, attribute; 126, 127; 126, 130; 127, attribute; 127, 128; 127, 129; 128, identifier:self; 129, identifier:protocol; 130, identifier:get; 131, string:'/segments/{segment_id}/all_efforts'; 132, dictionary_splat; 132, 133; 133, identifier:params; 134, return_statement; 134, 135; 135, call; 135, 136; 135, 137; 136, identifier:BatchedResultsIterator; 137, argument_list; 137, 138; 137, 143; 137, 146; 137, 149; 138, keyword_argument; 138, 139; 138, 140; 139, identifier:entity; 140, attribute; 140, 141; 140, 142; 141, identifier:model; 142, identifier:BaseEffort; 143, keyword_argument; 143, 144; 143, 145; 144, identifier:bind_client; 145, identifier:self; 146, keyword_argument; 146, 147; 146, 148; 147, identifier:result_fetcher; 148, identifier:result_fetcher; 149, keyword_argument; 149, 150; 149, 151; 150, identifier:limit; 151, identifier:limit
def get_segment_efforts(self, segment_id, athlete_id=None, start_date_local=None, end_date_local=None, limit=None): """ Gets all efforts on a particular segment sorted by start_date_local Returns an array of segment effort summary representations sorted by start_date_local ascending or by elapsed_time if an athlete_id is provided. If no filtering parameters is provided all efforts for the segment will be returned. Date range filtering is accomplished using an inclusive start and end time, thus start_date_local and end_date_local must be sent together. For open ended ranges pick dates significantly in the past or future. The filtering is done over local time for the segment, so there is no need for timezone conversion. For example, all efforts on Jan. 1st, 2014 for a segment in San Francisco, CA can be fetched using 2014-01-01T00:00:00Z and 2014-01-01T23:59:59Z. http://strava.github.io/api/v3/segments/#all_efforts :param segment_id: ID of the segment. :type segment_id: param :int athlete_id: (optional) ID of athlete. :type athlete_id: int :param start_date_local: (optional) efforts before this date will be excluded. Either as ISO8601 or datetime object :type start_date_local: datetime.datetime or str :param end_date_local: (optional) efforts after this date will be excluded. Either as ISO8601 or datetime object :type end_date_local: datetime.datetime or str :param limit: (optional), limit number of efforts. :type limit: int :return: An iterator of :class:`stravalib.model.SegmentEffort` efforts on a segment. :rtype: :class:`BatchedResultsIterator` """ params = {"segment_id": segment_id} if athlete_id is not None: params['athlete_id'] = athlete_id if start_date_local: if isinstance(start_date_local, six.string_types): start_date_local = arrow.get(start_date_local).naive params["start_date_local"] = start_date_local.strftime("%Y-%m-%dT%H:%M:%SZ") if end_date_local: if isinstance(end_date_local, six.string_types): end_date_local = arrow.get(end_date_local).naive params["end_date_local"] = end_date_local.strftime("%Y-%m-%dT%H:%M:%SZ") if limit is not None: params["limit"] = limit result_fetcher = functools.partial(self.protocol.get, '/segments/{segment_id}/all_efforts', **params) return BatchedResultsIterator(entity=model.BaseEffort, bind_client=self, result_fetcher=result_fetcher, limit=limit)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 16; 2, function_name:range; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 10; 3, 13; 4, identifier:self; 5, identifier:low; 6, identifier:high; 7, default_parameter; 7, 8; 7, 9; 8, identifier:with_scores; 9, False; 10, default_parameter; 10, 11; 10, 12; 11, identifier:desc; 12, False; 13, default_parameter; 13, 14; 13, 15; 14, identifier:reverse; 15, False; 16, block; 16, 17; 16, 19; 17, expression_statement; 17, 18; 18, comment; 19, if_statement; 19, 20; 19, 21; 19, 36; 20, identifier:reverse; 21, block; 21, 22; 22, return_statement; 22, 23; 23, call; 23, 24; 23, 29; 24, attribute; 24, 25; 24, 28; 25, attribute; 25, 26; 25, 27; 26, identifier:self; 27, identifier:database; 28, identifier:zrevrange; 29, argument_list; 29, 30; 29, 33; 29, 34; 29, 35; 30, attribute; 30, 31; 30, 32; 31, identifier:self; 32, identifier:key; 33, identifier:low; 34, identifier:high; 35, identifier:with_scores; 36, else_clause; 36, 37; 37, block; 37, 38; 38, return_statement; 38, 39; 39, call; 39, 40; 39, 45; 40, attribute; 40, 41; 40, 44; 41, attribute; 41, 42; 41, 43; 42, identifier:self; 43, identifier:database; 44, identifier:zrange; 45, argument_list; 45, 46; 45, 49; 45, 50; 45, 51; 45, 52; 46, attribute; 46, 47; 46, 48; 47, identifier:self; 48, identifier:key; 49, identifier:low; 50, identifier:high; 51, identifier:desc; 52, identifier:with_scores
def range(self, low, high, with_scores=False, desc=False, reverse=False): """ Return a range of items between ``low`` and ``high``. By default scores will not be included, but this can be controlled via the ``with_scores`` parameter. :param low: Lower bound. :param high: Upper bound. :param bool with_scores: Whether the range should include the scores along with the items. :param bool desc: Whether to sort the results descendingly. :param bool reverse: Whether to select the range in reverse. """ if reverse: return self.database.zrevrange(self.key, low, high, with_scores) else: return self.database.zrange(self.key, low, high, desc, with_scores)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:sfiles_to_event; 3, parameters; 3, 4; 4, identifier:sfile_list; 5, block; 5, 6; 5, 8; 5, 12; 5, 31; 5, 45; 5, 55; 5, 61; 5, 89; 5, 90; 5, 95; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:event_list; 11, list:[]; 12, expression_statement; 12, 13; 13, assignment; 13, 14; 13, 15; 14, identifier:sort_list; 15, list_comprehension; 15, 16; 15, 28; 16, tuple; 16, 17; 16, 27; 17, attribute; 17, 18; 17, 26; 18, subscript; 18, 19; 18, 25; 19, attribute; 19, 20; 19, 24; 20, call; 20, 21; 20, 22; 21, identifier:readheader; 22, argument_list; 22, 23; 23, identifier:sfile; 24, identifier:origins; 25, integer:0; 26, identifier:time; 27, identifier:sfile; 28, for_in_clause; 28, 29; 28, 30; 29, identifier:sfile; 30, identifier:sfile_list; 31, expression_statement; 31, 32; 32, call; 32, 33; 32, 36; 33, attribute; 33, 34; 33, 35; 34, identifier:sort_list; 35, identifier:sort; 36, argument_list; 36, 37; 37, keyword_argument; 37, 38; 37, 39; 38, identifier:key; 39, lambda; 39, 40; 39, 42; 40, lambda_parameters; 40, 41; 41, identifier:tup; 42, subscript; 42, 43; 42, 44; 43, identifier:tup; 44, integer:0; 45, expression_statement; 45, 46; 46, assignment; 46, 47; 46, 48; 47, identifier:sfile_list; 48, list_comprehension; 48, 49; 48, 52; 49, subscript; 49, 50; 49, 51; 50, identifier:sfile; 51, integer:1; 52, for_in_clause; 52, 53; 52, 54; 53, identifier:sfile; 54, identifier:sort_list; 55, expression_statement; 55, 56; 56, assignment; 56, 57; 56, 58; 57, identifier:catalog; 58, call; 58, 59; 58, 60; 59, identifier:Catalog; 60, argument_list; 61, for_statement; 61, 62; 61, 65; 61, 69; 62, pattern_list; 62, 63; 62, 64; 63, identifier:i; 64, identifier:sfile; 65, call; 65, 66; 65, 67; 66, identifier:enumerate; 67, argument_list; 67, 68; 68, identifier:sfile_list; 69, block; 69, 70; 69, 79; 70, expression_statement; 70, 71; 71, call; 71, 72; 71, 75; 72, attribute; 72, 73; 72, 74; 73, identifier:event_list; 74, identifier:append; 75, argument_list; 75, 76; 76, tuple; 76, 77; 76, 78; 77, identifier:i; 78, identifier:sfile; 79, expression_statement; 79, 80; 80, call; 80, 81; 80, 84; 81, attribute; 81, 82; 81, 83; 82, identifier:catalog; 83, identifier:append; 84, argument_list; 84, 85; 85, call; 85, 86; 85, 87; 86, identifier:readheader; 87, argument_list; 87, 88; 88, identifier:sfile; 89, comment; 90, expression_statement; 90, 91; 91, call; 91, 92; 91, 93; 92, identifier:write_event; 93, argument_list; 93, 94; 94, identifier:catalog; 95, return_statement; 95, 96; 96, identifier:event_list
def sfiles_to_event(sfile_list): """ Write an event.dat file from a list of Seisan events :type sfile_list: list :param sfile_list: List of s-files to sort and put into the database :returns: List of tuples of event ID (int) and Sfile name """ event_list = [] sort_list = [(readheader(sfile).origins[0].time, sfile) for sfile in sfile_list] sort_list.sort(key=lambda tup: tup[0]) sfile_list = [sfile[1] for sfile in sort_list] catalog = Catalog() for i, sfile in enumerate(sfile_list): event_list.append((i, sfile)) catalog.append(readheader(sfile)) # Hand off to sister function write_event(catalog) return event_list
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:sort; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 26; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, call; 9, 10; 9, 15; 10, attribute; 10, 11; 10, 14; 11, attribute; 11, 12; 11, 13; 12, identifier:self; 13, identifier:families; 14, identifier:sort; 15, argument_list; 15, 16; 16, keyword_argument; 16, 17; 16, 18; 17, identifier:key; 18, lambda; 18, 19; 18, 21; 19, lambda_parameters; 19, 20; 20, identifier:x; 21, attribute; 21, 22; 21, 25; 22, attribute; 22, 23; 22, 24; 23, identifier:x; 24, identifier:template; 25, identifier:name; 26, return_statement; 26, 27; 27, identifier:self
def sort(self): """ Sort the families by template name. .. rubric:: Example >>> party = Party(families=[Family(template=Template(name='b')), ... Family(template=Template(name='a'))]) >>> party[0] Family of 0 detections from template b >>> party.sort()[0] Family of 0 detections from template a """ self.families.sort(key=lambda x: x.template.name) return self
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 12; 2, function_name:decluster; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 4, identifier:self; 5, identifier:trig_int; 6, default_parameter; 6, 7; 6, 8; 7, identifier:timing; 8, string:'detect'; 9, default_parameter; 9, 10; 9, 11; 10, identifier:metric; 11, string:'avg_cor'; 12, block; 12, 13; 12, 15; 12, 19; 12, 34; 12, 159; 12, 174; 12, 189; 12, 212; 12, 213; 12, 232; 12, 233; 12, 237; 12, 287; 12, 288; 12, 304; 12, 308; 12, 359; 12, 365; 13, expression_statement; 13, 14; 14, comment; 15, expression_statement; 15, 16; 16, assignment; 16, 17; 16, 18; 17, identifier:all_detections; 18, list:[]; 19, for_statement; 19, 20; 19, 21; 19, 24; 20, identifier:fam; 21, attribute; 21, 22; 21, 23; 22, identifier:self; 23, identifier:families; 24, block; 24, 25; 25, expression_statement; 25, 26; 26, call; 26, 27; 26, 30; 27, attribute; 27, 28; 27, 29; 28, identifier:all_detections; 29, identifier:extend; 30, argument_list; 30, 31; 31, attribute; 31, 32; 31, 33; 32, identifier:fam; 33, identifier:detections; 34, if_statement; 34, 35; 34, 38; 34, 88; 34, 152; 35, comparison_operator:==; 35, 36; 35, 37; 36, identifier:timing; 37, string:'detect'; 38, block; 38, 39; 39, if_statement; 39, 40; 39, 43; 39, 62; 39, 81; 40, comparison_operator:==; 40, 41; 40, 42; 41, identifier:metric; 42, string:'avg_cor'; 43, block; 43, 44; 44, expression_statement; 44, 45; 45, assignment; 45, 46; 45, 47; 46, identifier:detect_info; 47, list_comprehension; 47, 48; 47, 59; 48, tuple; 48, 49; 48, 52; 49, attribute; 49, 50; 49, 51; 50, identifier:d; 51, identifier:detect_time; 52, binary_operator:/; 52, 53; 52, 56; 53, attribute; 53, 54; 53, 55; 54, identifier:d; 55, identifier:detect_val; 56, attribute; 56, 57; 56, 58; 57, identifier:d; 58, identifier:no_chans; 59, for_in_clause; 59, 60; 59, 61; 60, identifier:d; 61, identifier:all_detections; 62, elif_clause; 62, 63; 62, 66; 63, comparison_operator:==; 63, 64; 63, 65; 64, identifier:metric; 65, string:'cor_sum'; 66, block; 66, 67; 67, expression_statement; 67, 68; 68, assignment; 68, 69; 68, 70; 69, identifier:detect_info; 70, list_comprehension; 70, 71; 70, 78; 71, tuple; 71, 72; 71, 75; 72, attribute; 72, 73; 72, 74; 73, identifier:d; 74, identifier:detect_time; 75, attribute; 75, 76; 75, 77; 76, identifier:d; 77, identifier:detect_val; 78, for_in_clause; 78, 79; 78, 80; 79, identifier:d; 80, identifier:all_detections; 81, else_clause; 81, 82; 82, block; 82, 83; 83, raise_statement; 83, 84; 84, call; 84, 85; 84, 86; 85, identifier:MatchFilterError; 86, argument_list; 86, 87; 87, string:'metric is not cor_sum or avg_cor'; 88, elif_clause; 88, 89; 88, 92; 89, comparison_operator:==; 89, 90; 89, 91; 90, identifier:timing; 91, string:'origin'; 92, block; 92, 93; 93, if_statement; 93, 94; 93, 97; 93, 121; 93, 145; 94, comparison_operator:==; 94, 95; 94, 96; 95, identifier:metric; 96, string:'avg_cor'; 97, block; 97, 98; 98, expression_statement; 98, 99; 99, assignment; 99, 100; 99, 101; 100, identifier:detect_info; 101, list_comprehension; 101, 102; 101, 118; 102, tuple; 102, 103; 102, 111; 103, attribute; 103, 104; 103, 110; 104, call; 104, 105; 104, 106; 105, identifier:_get_origin; 106, argument_list; 106, 107; 107, attribute; 107, 108; 107, 109; 108, identifier:d; 109, identifier:event; 110, identifier:time; 111, binary_operator:/; 111, 112; 111, 115; 112, attribute; 112, 113; 112, 114; 113, identifier:d; 114, identifier:detect_val; 115, attribute; 115, 116; 115, 117; 116, identifier:d; 117, identifier:no_chans; 118, for_in_clause; 118, 119; 118, 120; 119, identifier:d; 120, identifier:all_detections; 121, elif_clause; 121, 122; 121, 125; 122, comparison_operator:==; 122, 123; 122, 124; 123, identifier:metric; 124, string:'cor_sum'; 125, block; 125, 126; 126, expression_statement; 126, 127; 127, assignment; 127, 128; 127, 129; 128, identifier:detect_info; 129, list_comprehension; 129, 130; 129, 142; 130, tuple; 130, 131; 130, 139; 131, attribute; 131, 132; 131, 138; 132, call; 132, 133; 132, 134; 133, identifier:_get_origin; 134, argument_list; 134, 135; 135, attribute; 135, 136; 135, 137; 136, identifier:d; 137, identifier:event; 138, identifier:time; 139, attribute; 139, 140; 139, 141; 140, identifier:d; 141, identifier:detect_val; 142, for_in_clause; 142, 143; 142, 144; 143, identifier:d; 144, identifier:all_detections; 145, else_clause; 145, 146; 146, block; 146, 147; 147, raise_statement; 147, 148; 148, call; 148, 149; 148, 150; 149, identifier:MatchFilterError; 150, argument_list; 150, 151; 151, string:'metric is not cor_sum or avg_cor'; 152, else_clause; 152, 153; 153, block; 153, 154; 154, raise_statement; 154, 155; 155, call; 155, 156; 155, 157; 156, identifier:MatchFilterError; 157, argument_list; 157, 158; 158, string:'timing is not detect or origin'; 159, expression_statement; 159, 160; 160, assignment; 160, 161; 160, 162; 161, identifier:min_det; 162, subscript; 162, 163; 162, 173; 163, call; 163, 164; 163, 165; 164, identifier:sorted; 165, argument_list; 165, 166; 166, list_comprehension; 166, 167; 166, 170; 167, subscript; 167, 168; 167, 169; 168, identifier:d; 169, integer:0; 170, for_in_clause; 170, 171; 170, 172; 171, identifier:d; 172, identifier:detect_info; 173, integer:0; 174, expression_statement; 174, 175; 175, assignment; 175, 176; 175, 177; 176, identifier:detect_vals; 177, call; 177, 178; 177, 181; 178, attribute; 178, 179; 178, 180; 179, identifier:np; 180, identifier:array; 181, argument_list; 181, 182; 182, list_comprehension; 182, 183; 182, 186; 183, subscript; 183, 184; 183, 185; 184, identifier:d; 185, integer:1; 186, for_in_clause; 186, 187; 186, 188; 187, identifier:d; 188, identifier:detect_info; 189, expression_statement; 189, 190; 190, assignment; 190, 191; 190, 192; 191, identifier:detect_times; 192, call; 192, 193; 192, 196; 193, attribute; 193, 194; 193, 195; 194, identifier:np; 195, identifier:array; 196, argument_list; 196, 197; 197, list_comprehension; 197, 198; 197, 209; 198, call; 198, 199; 198, 200; 199, identifier:_total_microsec; 200, argument_list; 200, 201; 200, 206; 201, attribute; 201, 202; 201, 205; 202, subscript; 202, 203; 202, 204; 203, identifier:d; 204, integer:0; 205, identifier:datetime; 206, attribute; 206, 207; 206, 208; 207, identifier:min_det; 208, identifier:datetime; 209, for_in_clause; 209, 210; 209, 211; 210, identifier:d; 211, identifier:detect_info; 212, comment; 213, expression_statement; 213, 214; 214, assignment; 214, 215; 214, 216; 215, identifier:peaks_out; 216, call; 216, 217; 216, 218; 217, identifier:decluster; 218, argument_list; 218, 219; 218, 222; 218, 225; 219, keyword_argument; 219, 220; 219, 221; 220, identifier:peaks; 221, identifier:detect_vals; 222, keyword_argument; 222, 223; 222, 224; 223, identifier:index; 224, identifier:detect_times; 225, keyword_argument; 225, 226; 225, 227; 226, identifier:trig_int; 227, binary_operator:*; 227, 228; 227, 229; 228, identifier:trig_int; 229, binary_operator:**; 229, 230; 229, 231; 230, integer:10; 231, integer:6; 232, comment; 233, expression_statement; 233, 234; 234, assignment; 234, 235; 234, 236; 235, identifier:declustered_detections; 236, list:[]; 237, for_statement; 237, 238; 237, 239; 237, 240; 238, identifier:ind; 239, identifier:peaks_out; 240, block; 240, 241; 240, 257; 240, 278; 241, expression_statement; 241, 242; 242, assignment; 242, 243; 242, 244; 243, identifier:matching_time_indeces; 244, subscript; 244, 245; 244, 256; 245, call; 245, 246; 245, 249; 246, attribute; 246, 247; 246, 248; 247, identifier:np; 248, identifier:where; 249, argument_list; 249, 250; 250, comparison_operator:==; 250, 251; 250, 252; 251, identifier:detect_times; 252, subscript; 252, 253; 252, 254; 253, identifier:ind; 254, unary_operator:-; 254, 255; 255, integer:1; 256, integer:0; 257, expression_statement; 257, 258; 258, assignment; 258, 259; 258, 260; 259, identifier:matches; 260, subscript; 260, 261; 260, 262; 261, identifier:matching_time_indeces; 262, subscript; 262, 263; 262, 277; 263, subscript; 263, 264; 263, 276; 264, call; 264, 265; 264, 268; 265, attribute; 265, 266; 265, 267; 266, identifier:np; 267, identifier:where; 268, argument_list; 268, 269; 269, comparison_operator:==; 269, 270; 269, 273; 270, subscript; 270, 271; 270, 272; 271, identifier:detect_vals; 272, identifier:matching_time_indeces; 273, subscript; 273, 274; 273, 275; 274, identifier:ind; 275, integer:0; 276, integer:0; 277, integer:0; 278, expression_statement; 278, 279; 279, call; 279, 280; 279, 283; 280, attribute; 280, 281; 280, 282; 281, identifier:declustered_detections; 282, identifier:append; 283, argument_list; 283, 284; 284, subscript; 284, 285; 284, 286; 285, identifier:all_detections; 286, identifier:matches; 287, comment; 288, expression_statement; 288, 289; 289, assignment; 289, 290; 289, 291; 290, identifier:template_names; 291, call; 291, 292; 291, 293; 292, identifier:list; 293, argument_list; 293, 294; 294, call; 294, 295; 294, 296; 295, identifier:set; 296, argument_list; 296, 297; 297, list_comprehension; 297, 298; 297, 301; 298, attribute; 298, 299; 298, 300; 299, identifier:d; 300, identifier:template_name; 301, for_in_clause; 301, 302; 301, 303; 302, identifier:d; 303, identifier:declustered_detections; 304, expression_statement; 304, 305; 305, assignment; 305, 306; 305, 307; 306, identifier:new_families; 307, list:[]; 308, for_statement; 308, 309; 308, 310; 308, 311; 309, identifier:template_name; 310, identifier:template_names; 311, block; 311, 312; 311, 334; 312, expression_statement; 312, 313; 313, assignment; 313, 314; 313, 315; 314, identifier:template; 315, subscript; 315, 316; 315, 333; 316, list_comprehension; 316, 317; 316, 320; 316, 325; 317, attribute; 317, 318; 317, 319; 318, identifier:fam; 319, identifier:template; 320, for_in_clause; 320, 321; 320, 322; 321, identifier:fam; 322, attribute; 322, 323; 322, 324; 323, identifier:self; 324, identifier:families; 325, if_clause; 325, 326; 326, comparison_operator:==; 326, 327; 326, 332; 327, attribute; 327, 328; 327, 331; 328, attribute; 328, 329; 328, 330; 329, identifier:fam; 330, identifier:template; 331, identifier:name; 332, identifier:template_name; 333, integer:0; 334, expression_statement; 334, 335; 335, call; 335, 336; 335, 339; 336, attribute; 336, 337; 336, 338; 337, identifier:new_families; 338, identifier:append; 339, argument_list; 339, 340; 340, call; 340, 341; 340, 342; 341, identifier:Family; 342, argument_list; 342, 343; 342, 346; 343, keyword_argument; 343, 344; 343, 345; 344, identifier:template; 345, identifier:template; 346, keyword_argument; 346, 347; 346, 348; 347, identifier:detections; 348, list_comprehension; 348, 349; 348, 350; 348, 353; 349, identifier:d; 350, for_in_clause; 350, 351; 350, 352; 351, identifier:d; 352, identifier:declustered_detections; 353, if_clause; 353, 354; 354, comparison_operator:==; 354, 355; 354, 358; 355, attribute; 355, 356; 355, 357; 356, identifier:d; 357, identifier:template_name; 358, identifier:template_name; 359, expression_statement; 359, 360; 360, assignment; 360, 361; 360, 364; 361, attribute; 361, 362; 361, 363; 362, identifier:self; 363, identifier:families; 364, identifier:new_families; 365, return_statement; 365, 366; 366, identifier:self
def decluster(self, trig_int, timing='detect', metric='avg_cor'): """ De-cluster a Party of detections by enforcing a detection separation. De-clustering occurs between events detected by different (or the same) templates. If multiple detections occur within trig_int then the preferred detection will be determined by the metric argument. This can be either the average single-station correlation coefficient which is calculated as Detection.detect_val / Detection.no_chans, or the raw cross channel correlation sum which is simply Detection.detect_val. :type trig_int: float :param trig_int: Minimum detection separation in seconds. :type metric: str :param metric: What metric to sort peaks by. Either 'avg_cor' which takes the single station average correlation or 'cor_sum' which takes the total correlation sum across all channels. :type timing: str :param timing: Either 'detect' or 'origin' to decluster based on either the detection time or the origin time. .. Warning:: Works in place on object, if you need to keep the original safe then run this on a copy of the object! .. rubric:: Example >>> party = Party().read() >>> len(party) 4 >>> declustered = party.decluster(20) >>> len(party) 3 """ all_detections = [] for fam in self.families: all_detections.extend(fam.detections) if timing == 'detect': if metric == 'avg_cor': detect_info = [(d.detect_time, d.detect_val / d.no_chans) for d in all_detections] elif metric == 'cor_sum': detect_info = [(d.detect_time, d.detect_val) for d in all_detections] else: raise MatchFilterError('metric is not cor_sum or avg_cor') elif timing == 'origin': if metric == 'avg_cor': detect_info = [(_get_origin(d.event).time, d.detect_val / d.no_chans) for d in all_detections] elif metric == 'cor_sum': detect_info = [(_get_origin(d.event).time, d.detect_val) for d in all_detections] else: raise MatchFilterError('metric is not cor_sum or avg_cor') else: raise MatchFilterError('timing is not detect or origin') min_det = sorted([d[0] for d in detect_info])[0] detect_vals = np.array([d[1] for d in detect_info]) detect_times = np.array([ _total_microsec(d[0].datetime, min_det.datetime) for d in detect_info]) # Trig_int must be converted from seconds to micro-seconds peaks_out = decluster( peaks=detect_vals, index=detect_times, trig_int=trig_int * 10 ** 6) # Need to match both the time and the detection value declustered_detections = [] for ind in peaks_out: matching_time_indeces = np.where(detect_times == ind[-1])[0] matches = matching_time_indeces[ np.where(detect_vals[matching_time_indeces] == ind[0])[0][0]] declustered_detections.append(all_detections[matches]) # Convert this list into families template_names = list(set([d.template_name for d in declustered_detections])) new_families = [] for template_name in template_names: template = [fam.template for fam in self.families if fam.template.name == template_name][0] new_families.append(Family( template=template, detections=[d for d in declustered_detections if d.template_name == template_name])) self.families = new_families return self
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:sort; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 27; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 13; 10, attribute; 10, 11; 10, 12; 11, identifier:self; 12, identifier:detections; 13, call; 13, 14; 13, 15; 14, identifier:sorted; 15, argument_list; 15, 16; 15, 19; 16, attribute; 16, 17; 16, 18; 17, identifier:self; 18, identifier:detections; 19, keyword_argument; 19, 20; 19, 21; 20, identifier:key; 21, lambda; 21, 22; 21, 24; 22, lambda_parameters; 22, 23; 23, identifier:d; 24, attribute; 24, 25; 24, 26; 25, identifier:d; 26, identifier:detect_time; 27, return_statement; 27, 28; 28, identifier:self
def sort(self): """Sort by detection time. .. rubric:: Example >>> family = Family( ... template=Template(name='a'), detections=[ ... Detection(template_name='a', detect_time=UTCDateTime(0) + 200, ... no_chans=8, detect_val=4.2, threshold=1.2, ... typeofdet='corr', threshold_type='MAD', ... threshold_input=8.0), ... Detection(template_name='a', detect_time=UTCDateTime(0), ... no_chans=8, detect_val=4.5, threshold=1.2, ... typeofdet='corr', threshold_type='MAD', ... threshold_input=8.0), ... Detection(template_name='a', detect_time=UTCDateTime(0) + 10, ... no_chans=8, detect_val=4.5, threshold=1.2, ... typeofdet='corr', threshold_type='MAD', ... threshold_input=8.0)]) >>> family[0].detect_time UTCDateTime(1970, 1, 1, 0, 3, 20) >>> family.sort()[0].detect_time UTCDateTime(1970, 1, 1, 0, 0) """ self.detections = sorted(self.detections, key=lambda d: d.detect_time) return self
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:sort; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 27; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 13; 10, attribute; 10, 11; 10, 12; 11, identifier:self; 12, identifier:templates; 13, call; 13, 14; 13, 15; 14, identifier:sorted; 15, argument_list; 15, 16; 15, 19; 16, attribute; 16, 17; 16, 18; 17, identifier:self; 18, identifier:templates; 19, keyword_argument; 19, 20; 19, 21; 20, identifier:key; 21, lambda; 21, 22; 21, 24; 22, lambda_parameters; 22, 23; 23, identifier:x; 24, attribute; 24, 25; 24, 26; 25, identifier:x; 26, identifier:name; 27, return_statement; 27, 28; 28, identifier:self
def sort(self): """ Sort the tribe, sorts by template name. .. rubric:: Example >>> tribe = Tribe(templates=[Template(name='c'), Template(name='b'), ... Template(name='a')]) >>> tribe.sort() Tribe of 3 templates >>> tribe[0] # doctest: +NORMALIZE_WHITESPACE Template a: 0 channels; lowcut: None Hz; highcut: None Hz; sampling rate None Hz; filter order: None; process length: None s """ self.templates = sorted(self.templates, key=lambda x: x.name) return self
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 23; 2, function_name:filter_picks; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 14; 3, 17; 3, 20; 4, identifier:catalog; 5, default_parameter; 5, 6; 5, 7; 6, identifier:stations; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:channels; 10, None; 11, default_parameter; 11, 12; 11, 13; 12, identifier:networks; 13, None; 14, default_parameter; 14, 15; 14, 16; 15, identifier:locations; 16, None; 17, default_parameter; 17, 18; 17, 19; 18, identifier:top_n_picks; 19, None; 20, default_parameter; 20, 21; 20, 22; 21, identifier:evaluation_mode; 22, string:'all'; 23, block; 23, 24; 23, 26; 23, 27; 23, 35; 23, 73; 23, 111; 23, 149; 23, 187; 23, 255; 23, 426; 23, 427; 23, 433; 23, 454; 24, expression_statement; 24, 25; 25, comment; 26, comment; 27, expression_statement; 27, 28; 28, assignment; 28, 29; 28, 30; 29, identifier:filtered_catalog; 30, call; 30, 31; 30, 34; 31, attribute; 31, 32; 31, 33; 32, identifier:catalog; 33, identifier:copy; 34, argument_list; 35, if_statement; 35, 36; 35, 37; 36, identifier:stations; 37, block; 37, 38; 38, for_statement; 38, 39; 38, 40; 38, 41; 39, identifier:event; 40, identifier:filtered_catalog; 41, block; 41, 42; 41, 53; 42, if_statement; 42, 43; 42, 51; 43, comparison_operator:==; 43, 44; 43, 50; 44, call; 44, 45; 44, 46; 45, identifier:len; 46, argument_list; 46, 47; 47, attribute; 47, 48; 47, 49; 48, identifier:event; 49, identifier:picks; 50, integer:0; 51, block; 51, 52; 52, continue_statement; 53, expression_statement; 53, 54; 54, assignment; 54, 55; 54, 58; 55, attribute; 55, 56; 55, 57; 56, identifier:event; 57, identifier:picks; 58, list_comprehension; 58, 59; 58, 60; 58, 65; 59, identifier:pick; 60, for_in_clause; 60, 61; 60, 62; 61, identifier:pick; 62, attribute; 62, 63; 62, 64; 63, identifier:event; 64, identifier:picks; 65, if_clause; 65, 66; 66, comparison_operator:in; 66, 67; 66, 72; 67, attribute; 67, 68; 67, 71; 68, attribute; 68, 69; 68, 70; 69, identifier:pick; 70, identifier:waveform_id; 71, identifier:station_code; 72, identifier:stations; 73, if_statement; 73, 74; 73, 75; 74, identifier:channels; 75, block; 75, 76; 76, for_statement; 76, 77; 76, 78; 76, 79; 77, identifier:event; 78, identifier:filtered_catalog; 79, block; 79, 80; 79, 91; 80, if_statement; 80, 81; 80, 89; 81, comparison_operator:==; 81, 82; 81, 88; 82, call; 82, 83; 82, 84; 83, identifier:len; 84, argument_list; 84, 85; 85, attribute; 85, 86; 85, 87; 86, identifier:event; 87, identifier:picks; 88, integer:0; 89, block; 89, 90; 90, continue_statement; 91, expression_statement; 91, 92; 92, assignment; 92, 93; 92, 96; 93, attribute; 93, 94; 93, 95; 94, identifier:event; 95, identifier:picks; 96, list_comprehension; 96, 97; 96, 98; 96, 103; 97, identifier:pick; 98, for_in_clause; 98, 99; 98, 100; 99, identifier:pick; 100, attribute; 100, 101; 100, 102; 101, identifier:event; 102, identifier:picks; 103, if_clause; 103, 104; 104, comparison_operator:in; 104, 105; 104, 110; 105, attribute; 105, 106; 105, 109; 106, attribute; 106, 107; 106, 108; 107, identifier:pick; 108, identifier:waveform_id; 109, identifier:channel_code; 110, identifier:channels; 111, if_statement; 111, 112; 111, 113; 112, identifier:networks; 113, block; 113, 114; 114, for_statement; 114, 115; 114, 116; 114, 117; 115, identifier:event; 116, identifier:filtered_catalog; 117, block; 117, 118; 117, 129; 118, if_statement; 118, 119; 118, 127; 119, comparison_operator:==; 119, 120; 119, 126; 120, call; 120, 121; 120, 122; 121, identifier:len; 122, argument_list; 122, 123; 123, attribute; 123, 124; 123, 125; 124, identifier:event; 125, identifier:picks; 126, integer:0; 127, block; 127, 128; 128, continue_statement; 129, expression_statement; 129, 130; 130, assignment; 130, 131; 130, 134; 131, attribute; 131, 132; 131, 133; 132, identifier:event; 133, identifier:picks; 134, list_comprehension; 134, 135; 134, 136; 134, 141; 135, identifier:pick; 136, for_in_clause; 136, 137; 136, 138; 137, identifier:pick; 138, attribute; 138, 139; 138, 140; 139, identifier:event; 140, identifier:picks; 141, if_clause; 141, 142; 142, comparison_operator:in; 142, 143; 142, 148; 143, attribute; 143, 144; 143, 147; 144, attribute; 144, 145; 144, 146; 145, identifier:pick; 146, identifier:waveform_id; 147, identifier:network_code; 148, identifier:networks; 149, if_statement; 149, 150; 149, 151; 150, identifier:locations; 151, block; 151, 152; 152, for_statement; 152, 153; 152, 154; 152, 155; 153, identifier:event; 154, identifier:filtered_catalog; 155, block; 155, 156; 155, 167; 156, if_statement; 156, 157; 156, 165; 157, comparison_operator:==; 157, 158; 157, 164; 158, call; 158, 159; 158, 160; 159, identifier:len; 160, argument_list; 160, 161; 161, attribute; 161, 162; 161, 163; 162, identifier:event; 163, identifier:picks; 164, integer:0; 165, block; 165, 166; 166, continue_statement; 167, expression_statement; 167, 168; 168, assignment; 168, 169; 168, 172; 169, attribute; 169, 170; 169, 171; 170, identifier:event; 171, identifier:picks; 172, list_comprehension; 172, 173; 172, 174; 172, 179; 173, identifier:pick; 174, for_in_clause; 174, 175; 174, 176; 175, identifier:pick; 176, attribute; 176, 177; 176, 178; 177, identifier:event; 178, identifier:picks; 179, if_clause; 179, 180; 180, comparison_operator:in; 180, 181; 180, 186; 181, attribute; 181, 182; 181, 185; 182, attribute; 182, 183; 182, 184; 183, identifier:pick; 184, identifier:waveform_id; 185, identifier:location_code; 186, identifier:locations; 187, if_statement; 187, 188; 187, 191; 187, 214; 187, 241; 188, comparison_operator:==; 188, 189; 188, 190; 189, identifier:evaluation_mode; 190, string:'manual'; 191, block; 191, 192; 192, for_statement; 192, 193; 192, 194; 192, 195; 193, identifier:event; 194, identifier:filtered_catalog; 195, block; 195, 196; 196, expression_statement; 196, 197; 197, assignment; 197, 198; 197, 201; 198, attribute; 198, 199; 198, 200; 199, identifier:event; 200, identifier:picks; 201, list_comprehension; 201, 202; 201, 203; 201, 208; 202, identifier:pick; 203, for_in_clause; 203, 204; 203, 205; 204, identifier:pick; 205, attribute; 205, 206; 205, 207; 206, identifier:event; 207, identifier:picks; 208, if_clause; 208, 209; 209, comparison_operator:==; 209, 210; 209, 213; 210, attribute; 210, 211; 210, 212; 211, identifier:pick; 212, identifier:evaluation_mode; 213, string:'manual'; 214, elif_clause; 214, 215; 214, 218; 215, comparison_operator:==; 215, 216; 215, 217; 216, identifier:evaluation_mode; 217, string:'automatic'; 218, block; 218, 219; 219, for_statement; 219, 220; 219, 221; 219, 222; 220, identifier:event; 221, identifier:filtered_catalog; 222, block; 222, 223; 223, expression_statement; 223, 224; 224, assignment; 224, 225; 224, 228; 225, attribute; 225, 226; 225, 227; 226, identifier:event; 227, identifier:picks; 228, list_comprehension; 228, 229; 228, 230; 228, 235; 229, identifier:pick; 230, for_in_clause; 230, 231; 230, 232; 231, identifier:pick; 232, attribute; 232, 233; 232, 234; 233, identifier:event; 234, identifier:picks; 235, if_clause; 235, 236; 236, comparison_operator:==; 236, 237; 236, 240; 237, attribute; 237, 238; 237, 239; 238, identifier:pick; 239, identifier:evaluation_mode; 240, string:'automatic'; 241, elif_clause; 241, 242; 241, 245; 242, comparison_operator:!=; 242, 243; 242, 244; 243, identifier:evaluation_mode; 244, string:'all'; 245, block; 245, 246; 246, expression_statement; 246, 247; 247, call; 247, 248; 247, 251; 248, attribute; 248, 249; 248, 250; 249, identifier:warnings; 250, identifier:warn; 251, argument_list; 251, 252; 252, binary_operator:%; 252, 253; 252, 254; 253, string:'Unrecognised evaluation_mode: %s, using all picks'; 254, identifier:evaluation_mode; 255, if_statement; 255, 256; 255, 257; 256, identifier:top_n_picks; 257, block; 257, 258; 257, 262; 257, 286; 257, 297; 257, 301; 257, 302; 257, 385; 258, expression_statement; 258, 259; 259, assignment; 259, 260; 259, 261; 260, identifier:all_picks; 261, list:[]; 262, for_statement; 262, 263; 262, 264; 262, 265; 263, identifier:event; 264, identifier:filtered_catalog; 265, block; 265, 266; 266, expression_statement; 266, 267; 267, augmented_assignment:+=; 267, 268; 267, 269; 268, identifier:all_picks; 269, list_comprehension; 269, 270; 269, 281; 270, tuple; 270, 271; 270, 276; 271, attribute; 271, 272; 271, 275; 272, attribute; 272, 273; 272, 274; 273, identifier:pick; 274, identifier:waveform_id; 275, identifier:station_code; 276, attribute; 276, 277; 276, 280; 277, attribute; 277, 278; 277, 279; 278, identifier:pick; 279, identifier:waveform_id; 280, identifier:channel_code; 281, for_in_clause; 281, 282; 281, 283; 282, identifier:pick; 283, attribute; 283, 284; 283, 285; 284, identifier:event; 285, identifier:picks; 286, expression_statement; 286, 287; 287, assignment; 287, 288; 287, 289; 288, identifier:counted; 289, call; 289, 290; 289, 296; 290, attribute; 290, 291; 290, 295; 291, call; 291, 292; 291, 293; 292, identifier:Counter; 293, argument_list; 293, 294; 294, identifier:all_picks; 295, identifier:most_common; 296, argument_list; 297, expression_statement; 297, 298; 298, assignment; 298, 299; 298, 300; 299, identifier:all_picks; 300, list:[]; 301, comment; 302, for_statement; 302, 303; 302, 304; 302, 312; 303, identifier:i; 304, call; 304, 305; 304, 306; 305, identifier:range; 306, argument_list; 306, 307; 307, subscript; 307, 308; 307, 311; 308, subscript; 308, 309; 308, 310; 309, identifier:counted; 310, integer:0; 311, integer:1; 312, block; 312, 313; 312, 335; 312, 336; 312, 351; 312, 367; 313, expression_statement; 313, 314; 314, assignment; 314, 315; 314, 316; 315, identifier:highest; 316, list_comprehension; 316, 317; 316, 320; 316, 323; 317, subscript; 317, 318; 317, 319; 318, identifier:item; 319, integer:0; 320, for_in_clause; 320, 321; 320, 322; 321, identifier:item; 322, identifier:counted; 323, if_clause; 323, 324; 324, comparison_operator:>=; 324, 325; 324, 328; 325, subscript; 325, 326; 325, 327; 326, identifier:item; 327, integer:1; 328, binary_operator:-; 328, 329; 328, 334; 329, subscript; 329, 330; 329, 333; 330, subscript; 330, 331; 330, 332; 331, identifier:counted; 332, integer:0; 333, integer:1; 334, identifier:i; 335, comment; 336, expression_statement; 336, 337; 337, assignment; 337, 338; 337, 339; 338, identifier:highest; 339, call; 339, 340; 339, 341; 340, identifier:sorted; 341, argument_list; 341, 342; 341, 343; 342, identifier:highest; 343, keyword_argument; 343, 344; 343, 345; 344, identifier:key; 345, lambda; 345, 346; 345, 348; 346, lambda_parameters; 346, 347; 347, identifier:tup; 348, subscript; 348, 349; 348, 350; 349, identifier:tup; 350, integer:0; 351, for_statement; 351, 352; 351, 353; 351, 354; 352, identifier:stachan; 353, identifier:highest; 354, block; 354, 355; 355, if_statement; 355, 356; 355, 359; 356, comparison_operator:not; 356, 357; 356, 358; 357, identifier:stachan; 358, identifier:all_picks; 359, block; 359, 360; 360, expression_statement; 360, 361; 361, call; 361, 362; 361, 365; 362, attribute; 362, 363; 362, 364; 363, identifier:all_picks; 364, identifier:append; 365, argument_list; 365, 366; 366, identifier:stachan; 367, if_statement; 367, 368; 367, 374; 368, comparison_operator:>; 368, 369; 368, 373; 369, call; 369, 370; 369, 371; 370, identifier:len; 371, argument_list; 371, 372; 372, identifier:all_picks; 373, identifier:top_n_picks; 374, block; 374, 375; 374, 384; 375, expression_statement; 375, 376; 376, assignment; 376, 377; 376, 378; 377, identifier:all_picks; 378, subscript; 378, 379; 378, 380; 379, identifier:all_picks; 380, slice; 380, 381; 380, 382; 380, 383; 381, integer:0; 382, colon; 383, identifier:top_n_picks; 384, break_statement; 385, for_statement; 385, 386; 385, 387; 385, 388; 386, identifier:event; 387, identifier:filtered_catalog; 388, block; 388, 389; 388, 400; 389, if_statement; 389, 390; 389, 398; 390, comparison_operator:==; 390, 391; 390, 397; 391, call; 391, 392; 391, 393; 392, identifier:len; 393, argument_list; 393, 394; 394, attribute; 394, 395; 394, 396; 395, identifier:event; 396, identifier:picks; 397, integer:0; 398, block; 398, 399; 399, continue_statement; 400, expression_statement; 400, 401; 401, assignment; 401, 402; 401, 405; 402, attribute; 402, 403; 402, 404; 403, identifier:event; 404, identifier:picks; 405, list_comprehension; 405, 406; 405, 407; 405, 412; 406, identifier:pick; 407, for_in_clause; 407, 408; 407, 409; 408, identifier:pick; 409, attribute; 409, 410; 409, 411; 410, identifier:event; 411, identifier:picks; 412, if_clause; 412, 413; 413, comparison_operator:in; 413, 414; 413, 425; 414, tuple; 414, 415; 414, 420; 415, attribute; 415, 416; 415, 419; 416, attribute; 416, 417; 416, 418; 417, identifier:pick; 418, identifier:waveform_id; 419, identifier:station_code; 420, attribute; 420, 421; 420, 424; 421, attribute; 421, 422; 421, 423; 422, identifier:pick; 423, identifier:waveform_id; 424, identifier:channel_code; 425, identifier:all_picks; 426, comment; 427, expression_statement; 427, 428; 428, assignment; 428, 429; 428, 430; 429, identifier:tmp_catalog; 430, call; 430, 431; 430, 432; 431, identifier:Catalog; 432, argument_list; 433, for_statement; 433, 434; 433, 435; 433, 436; 434, identifier:event; 435, identifier:filtered_catalog; 436, block; 436, 437; 437, if_statement; 437, 438; 437, 446; 438, comparison_operator:>; 438, 439; 438, 445; 439, call; 439, 440; 439, 441; 440, identifier:len; 441, argument_list; 441, 442; 442, attribute; 442, 443; 442, 444; 443, identifier:event; 444, identifier:picks; 445, integer:0; 446, block; 446, 447; 447, expression_statement; 447, 448; 448, call; 448, 449; 448, 452; 449, attribute; 449, 450; 449, 451; 450, identifier:tmp_catalog; 451, identifier:append; 452, argument_list; 452, 453; 453, identifier:event; 454, return_statement; 454, 455; 455, identifier:tmp_catalog
def filter_picks(catalog, stations=None, channels=None, networks=None, locations=None, top_n_picks=None, evaluation_mode='all'): """ Filter events in the catalog based on a number of parameters. :param catalog: Catalog to filter. :type catalog: obspy.core.event.Catalog :param stations: List for stations to keep picks from. :type stations: list :param channels: List of channels to keep picks from. :type channels: list :param networks: List of networks to keep picks from. :type networks: list :param locations: List of location codes to use :type locations: list :param top_n_picks: Filter only the top N most used station-channel pairs. :type top_n_picks: int :param evaluation_mode: To select only manual or automatic picks, or use all (default). :type evaluation_mode: str :return: Filtered Catalog - if events are left with no picks, they are removed from the catalog. :rtype: obspy.core.event.Catalog .. note:: Will filter first by station, then by channel, then by network, if using top_n_picks, this will be done last, after the other filters have been applied. .. note:: Doesn't work in place on the catalog, your input catalog will be safe unless you overwrite it. .. note:: Doesn't expand wildcard characters. .. rubric:: Example >>> from obspy.clients.fdsn import Client >>> from eqcorrscan.utils.catalog_utils import filter_picks >>> from obspy import UTCDateTime >>> client = Client('NCEDC') >>> t1 = UTCDateTime(2004, 9, 28) >>> t2 = t1 + 86400 >>> catalog = client.get_events(starttime=t1, endtime=t2, minmagnitude=3, ... minlatitude=35.7, maxlatitude=36.1, ... minlongitude=-120.6, maxlongitude=-120.2, ... includearrivals=True) >>> print(len(catalog)) 12 >>> filtered_catalog = filter_picks(catalog, stations=['BMS', 'BAP', ... 'PAG', 'PAN', ... 'PBI', 'PKY', ... 'YEG', 'WOF']) >>> print(len(filtered_catalog)) 12 >>> stations = [] >>> for event in filtered_catalog: ... for pick in event.picks: ... stations.append(pick.waveform_id.station_code) >>> print(sorted(list(set(stations)))) ['BAP', 'BMS', 'PAG', 'PAN', 'PBI', 'PKY', 'WOF', 'YEG'] """ # Don't work in place on the catalog filtered_catalog = catalog.copy() if stations: for event in filtered_catalog: if len(event.picks) == 0: continue event.picks = [pick for pick in event.picks if pick.waveform_id.station_code in stations] if channels: for event in filtered_catalog: if len(event.picks) == 0: continue event.picks = [pick for pick in event.picks if pick.waveform_id.channel_code in channels] if networks: for event in filtered_catalog: if len(event.picks) == 0: continue event.picks = [pick for pick in event.picks if pick.waveform_id.network_code in networks] if locations: for event in filtered_catalog: if len(event.picks) == 0: continue event.picks = [pick for pick in event.picks if pick.waveform_id.location_code in locations] if evaluation_mode == 'manual': for event in filtered_catalog: event.picks = [pick for pick in event.picks if pick.evaluation_mode == 'manual'] elif evaluation_mode == 'automatic': for event in filtered_catalog: event.picks = [pick for pick in event.picks if pick.evaluation_mode == 'automatic'] elif evaluation_mode != 'all': warnings.warn('Unrecognised evaluation_mode: %s, using all picks' % evaluation_mode) if top_n_picks: all_picks = [] for event in filtered_catalog: all_picks += [(pick.waveform_id.station_code, pick.waveform_id.channel_code) for pick in event.picks] counted = Counter(all_picks).most_common() all_picks = [] # Hack around sorting the counter object: Py 2 does it differently to 3 for i in range(counted[0][1]): highest = [item[0] for item in counted if item[1] >= counted[0][1] - i] # Sort them by alphabetical order in station highest = sorted(highest, key=lambda tup: tup[0]) for stachan in highest: if stachan not in all_picks: all_picks.append(stachan) if len(all_picks) > top_n_picks: all_picks = all_picks[0:top_n_picks] break for event in filtered_catalog: if len(event.picks) == 0: continue event.picks = [pick for pick in event.picks if (pick.waveform_id.station_code, pick.waveform_id.channel_code) in all_picks] # Remove events without picks tmp_catalog = Catalog() for event in filtered_catalog: if len(event.picks) > 0: tmp_catalog.append(event) return tmp_catalog