sequence
stringlengths
546
16.2k
code
stringlengths
108
19.3k
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:values; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 22; 6, expression_statement; 6, 7; 7, comment; 8, function_definition; 8, 9; 8, 10; 8, 12; 9, function_name:key; 10, parameters; 10, 11; 11, identifier:th; 12, block; 12, 13; 13, return_statement; 13, 14; 14, call; 14, 15; 14, 16; 15, identifier:len; 16, argument_list; 16, 17; 17, attribute; 17, 18; 17, 21; 18, attribute; 18, 19; 18, 20; 19, identifier:th; 20, identifier:__class__; 21, identifier:__mro__; 22, return_statement; 22, 23; 23, call; 23, 24; 23, 25; 24, identifier:sorted; 25, argument_list; 25, 26; 25, 32; 25, 35; 26, call; 26, 27; 26, 30; 27, attribute; 27, 28; 27, 29; 28, identifier:dict; 29, identifier:values; 30, argument_list; 30, 31; 31, identifier:self; 32, keyword_argument; 32, 33; 32, 34; 33, identifier:key; 34, identifier:key; 35, keyword_argument; 35, 36; 35, 37; 36, identifier:reverse; 37, True
def values(self): """ Return a list themeables sorted in reverse based on the their depth in the inheritance hierarchy. The sorting is key applying and merging the themeables so that they do not clash i.e :class:`axis_line` applied before :class:`axis_line_x`. """ def key(th): return len(th.__class__.__mro__) return sorted(dict.values(self), key=key, reverse=True)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:topological_sort; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 14; 5, 18; 5, 28; 5, 44; 5, 50; 5, 68; 5, 72; 5, 119; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:graph; 11, attribute; 11, 12; 11, 13; 12, identifier:self; 13, identifier:graph; 14, expression_statement; 14, 15; 15, assignment; 15, 16; 15, 17; 16, identifier:in_degree; 17, dictionary; 18, for_statement; 18, 19; 18, 20; 18, 21; 19, identifier:u; 20, identifier:graph; 21, block; 21, 22; 22, expression_statement; 22, 23; 23, assignment; 23, 24; 23, 27; 24, subscript; 24, 25; 24, 26; 25, identifier:in_degree; 26, identifier:u; 27, integer:0; 28, for_statement; 28, 29; 28, 30; 28, 31; 29, identifier:u; 30, identifier:graph; 31, block; 31, 32; 32, for_statement; 32, 33; 32, 34; 32, 37; 33, identifier:v; 34, subscript; 34, 35; 34, 36; 35, identifier:graph; 36, identifier:u; 37, block; 37, 38; 38, expression_statement; 38, 39; 39, augmented_assignment:+=; 39, 40; 39, 43; 40, subscript; 40, 41; 40, 42; 41, identifier:in_degree; 42, identifier:v; 43, integer:1; 44, expression_statement; 44, 45; 45, assignment; 45, 46; 45, 47; 46, identifier:queue; 47, call; 47, 48; 47, 49; 48, identifier:deque; 49, argument_list; 50, for_statement; 50, 51; 50, 52; 50, 53; 51, identifier:u; 52, identifier:in_degree; 53, block; 53, 54; 54, if_statement; 54, 55; 54, 60; 55, comparison_operator:==; 55, 56; 55, 59; 56, subscript; 56, 57; 56, 58; 57, identifier:in_degree; 58, identifier:u; 59, integer:0; 60, block; 60, 61; 61, expression_statement; 61, 62; 62, call; 62, 63; 62, 66; 63, attribute; 63, 64; 63, 65; 64, identifier:queue; 65, identifier:appendleft; 66, argument_list; 66, 67; 67, identifier:u; 68, expression_statement; 68, 69; 69, assignment; 69, 70; 69, 71; 70, identifier:sorted_graph; 71, list:[]; 72, while_statement; 72, 73; 72, 74; 73, identifier:queue; 74, block; 74, 75; 74, 83; 74, 90; 75, expression_statement; 75, 76; 76, assignment; 76, 77; 76, 78; 77, identifier:u; 78, call; 78, 79; 78, 82; 79, attribute; 79, 80; 79, 81; 80, identifier:queue; 81, identifier:pop; 82, argument_list; 83, expression_statement; 83, 84; 84, call; 84, 85; 84, 88; 85, attribute; 85, 86; 85, 87; 86, identifier:sorted_graph; 87, identifier:append; 88, argument_list; 88, 89; 89, identifier:u; 90, for_statement; 90, 91; 90, 92; 90, 98; 91, identifier:v; 92, call; 92, 93; 92, 94; 93, identifier:sorted; 94, argument_list; 94, 95; 95, subscript; 95, 96; 95, 97; 96, identifier:graph; 97, identifier:u; 98, block; 98, 99; 98, 105; 99, expression_statement; 99, 100; 100, augmented_assignment:-=; 100, 101; 100, 104; 101, subscript; 101, 102; 101, 103; 102, identifier:in_degree; 103, identifier:v; 104, integer:1; 105, if_statement; 105, 106; 105, 111; 106, comparison_operator:==; 106, 107; 106, 110; 107, subscript; 107, 108; 107, 109; 108, identifier:in_degree; 109, identifier:v; 110, integer:0; 111, block; 111, 112; 112, expression_statement; 112, 113; 113, call; 113, 114; 113, 117; 114, attribute; 114, 115; 114, 116; 115, identifier:queue; 116, identifier:appendleft; 117, argument_list; 117, 118; 118, identifier:v; 119, if_statement; 119, 120; 119, 129; 119, 132; 120, comparison_operator:==; 120, 121; 120, 125; 121, call; 121, 122; 121, 123; 122, identifier:len; 123, argument_list; 123, 124; 124, identifier:sorted_graph; 125, call; 125, 126; 125, 127; 126, identifier:len; 127, argument_list; 127, 128; 128, identifier:graph; 129, block; 129, 130; 130, return_statement; 130, 131; 131, identifier:sorted_graph; 132, else_clause; 132, 133; 133, block; 133, 134; 134, raise_statement; 134, 135; 135, call; 135, 136; 135, 137; 136, identifier:ValueError; 137, argument_list; 137, 138; 138, string:'graph is not acyclic'
def topological_sort(self): """ Returns a topological ordering of the DAG. Returns: list: A list of topologically sorted nodes in the graph. Raises: ValueError: Raised if the graph is not acyclic. """ graph = self.graph in_degree = {} for u in graph: in_degree[u] = 0 for u in graph: for v in graph[u]: in_degree[v] += 1 queue = deque() for u in in_degree: if in_degree[u] == 0: queue.appendleft(u) sorted_graph = [] while queue: u = queue.pop() sorted_graph.append(u) for v in sorted(graph[u]): in_degree[v] -= 1 if in_degree[v] == 0: queue.appendleft(v) if len(sorted_graph) == len(graph): return sorted_graph else: raise ValueError('graph is not acyclic')
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:_music_lib_search; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 4, identifier:self; 5, identifier:search; 6, identifier:start; 7, identifier:max_items; 8, block; 8, 9; 8, 11; 8, 40; 8, 41; 8, 45; 8, 66; 9, expression_statement; 9, 10; 10, comment; 11, expression_statement; 11, 12; 12, assignment; 12, 13; 12, 14; 13, identifier:response; 14, call; 14, 15; 14, 20; 15, attribute; 15, 16; 15, 19; 16, attribute; 16, 17; 16, 18; 17, identifier:self; 18, identifier:contentDirectory; 19, identifier:Browse; 20, argument_list; 20, 21; 21, list:[ ('ObjectID', search), ('BrowseFlag', 'BrowseDirectChildren'), ('Filter', '*'), ('StartingIndex', start), ('RequestedCount', max_items), ('SortCriteria', '') ]; 21, 22; 21, 25; 21, 28; 21, 31; 21, 34; 21, 37; 22, tuple; 22, 23; 22, 24; 23, string:'ObjectID'; 24, identifier:search; 25, tuple; 25, 26; 25, 27; 26, string:'BrowseFlag'; 27, string:'BrowseDirectChildren'; 28, tuple; 28, 29; 28, 30; 29, string:'Filter'; 30, string:'*'; 31, tuple; 31, 32; 31, 33; 32, string:'StartingIndex'; 33, identifier:start; 34, tuple; 34, 35; 34, 36; 35, string:'RequestedCount'; 36, identifier:max_items; 37, tuple; 37, 38; 37, 39; 38, string:'SortCriteria'; 39, string:''; 40, comment; 41, expression_statement; 41, 42; 42, assignment; 42, 43; 42, 44; 43, identifier:metadata; 44, dictionary; 45, for_statement; 45, 46; 45, 47; 45, 51; 46, identifier:tag; 47, list:['NumberReturned', 'TotalMatches', 'UpdateID']; 47, 48; 47, 49; 47, 50; 48, string:'NumberReturned'; 49, string:'TotalMatches'; 50, string:'UpdateID'; 51, block; 51, 52; 52, expression_statement; 52, 53; 53, assignment; 53, 54; 53, 60; 54, subscript; 54, 55; 54, 56; 55, identifier:metadata; 56, call; 56, 57; 56, 58; 57, identifier:camel_to_underscore; 58, argument_list; 58, 59; 59, identifier:tag; 60, call; 60, 61; 60, 62; 61, identifier:int; 62, argument_list; 62, 63; 63, subscript; 63, 64; 63, 65; 64, identifier:response; 65, identifier:tag; 66, return_statement; 66, 67; 67, expression_list; 67, 68; 67, 69; 68, identifier:response; 69, identifier:metadata
def _music_lib_search(self, search, start, max_items): """Perform a music library search and extract search numbers. You can get an overview of all the relevant search prefixes (like 'A:') and their meaning with the request: .. code :: response = device.contentDirectory.Browse([ ('ObjectID', '0'), ('BrowseFlag', 'BrowseDirectChildren'), ('Filter', '*'), ('StartingIndex', 0), ('RequestedCount', 100), ('SortCriteria', '') ]) Args: search (str): The ID to search. start (int): The index of the forst item to return. max_items (int): The maximum number of items to return. Returns: tuple: (response, metadata) where response is the returned metadata and metadata is a dict with the 'number_returned', 'total_matches' and 'update_id' integers """ response = self.contentDirectory.Browse([ ('ObjectID', search), ('BrowseFlag', 'BrowseDirectChildren'), ('Filter', '*'), ('StartingIndex', start), ('RequestedCount', max_items), ('SortCriteria', '') ]) # Get result information metadata = {} for tag in ['NumberReturned', 'TotalMatches', 'UpdateID']: metadata[camel_to_underscore(tag)] = int(response[tag]) return response, metadata
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:unique; 3, parameters; 3, 4; 4, identifier:seq; 5, block; 5, 6; 5, 8; 5, 12; 5, 28; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:cleaned; 11, list:[]; 12, for_statement; 12, 13; 12, 14; 12, 15; 13, identifier:each; 14, identifier:seq; 15, block; 15, 16; 16, if_statement; 16, 17; 16, 20; 17, comparison_operator:not; 17, 18; 17, 19; 18, identifier:each; 19, identifier:cleaned; 20, block; 20, 21; 21, expression_statement; 21, 22; 22, call; 22, 23; 22, 26; 23, attribute; 23, 24; 23, 25; 24, identifier:cleaned; 25, identifier:append; 26, argument_list; 26, 27; 27, identifier:each; 28, return_statement; 28, 29; 29, identifier:cleaned
def unique(seq): """Return the unique elements of a collection even if those elements are unhashable and unsortable, like dicts and sets""" cleaned = [] for each in seq: if each not in cleaned: cleaned.append(each) return cleaned
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:build_schema_info; 3, parameters; 3, 4; 4, identifier:connection_alias; 5, block; 5, 6; 5, 8; 5, 15; 5, 19; 5, 129; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:connection; 11, call; 11, 12; 11, 13; 12, identifier:get_valid_connection; 13, argument_list; 13, 14; 14, identifier:connection_alias; 15, expression_statement; 15, 16; 16, assignment; 16, 17; 16, 18; 17, identifier:ret; 18, list:[]; 19, with_statement; 19, 20; 19, 30; 20, with_clause; 20, 21; 21, with_item; 21, 22; 22, as_pattern; 22, 23; 22, 28; 23, call; 23, 24; 23, 27; 24, attribute; 24, 25; 24, 26; 25, identifier:connection; 26, identifier:cursor; 27, argument_list; 28, as_pattern_target; 28, 29; 29, identifier:cursor; 30, block; 30, 31; 30, 47; 31, expression_statement; 31, 32; 32, assignment; 32, 33; 32, 34; 33, identifier:tables_to_introspect; 34, call; 34, 35; 34, 40; 35, attribute; 35, 36; 35, 39; 36, attribute; 36, 37; 36, 38; 37, identifier:connection; 38, identifier:introspection; 39, identifier:table_names; 40, argument_list; 40, 41; 40, 42; 41, identifier:cursor; 42, keyword_argument; 42, 43; 42, 44; 43, identifier:include_views; 44, call; 44, 45; 44, 46; 45, identifier:_include_views; 46, argument_list; 47, for_statement; 47, 48; 47, 49; 47, 50; 48, identifier:table_name; 49, identifier:tables_to_introspect; 50, block; 50, 51; 50, 59; 50, 63; 50, 75; 50, 120; 51, if_statement; 51, 52; 51, 57; 52, not_operator; 52, 53; 53, call; 53, 54; 53, 55; 54, identifier:_include_table; 55, argument_list; 55, 56; 56, identifier:table_name; 57, block; 57, 58; 58, continue_statement; 59, expression_statement; 59, 60; 60, assignment; 60, 61; 60, 62; 61, identifier:td; 62, list:[]; 63, expression_statement; 63, 64; 64, assignment; 64, 65; 64, 66; 65, identifier:table_description; 66, call; 66, 67; 66, 72; 67, attribute; 67, 68; 67, 71; 68, attribute; 68, 69; 68, 70; 69, identifier:connection; 70, identifier:introspection; 71, identifier:get_table_description; 72, argument_list; 72, 73; 72, 74; 73, identifier:cursor; 74, identifier:table_name; 75, for_statement; 75, 76; 75, 77; 75, 78; 76, identifier:row; 77, identifier:table_description; 78, block; 78, 79; 78, 85; 78, 111; 79, expression_statement; 79, 80; 80, assignment; 80, 81; 80, 82; 81, identifier:column_name; 82, subscript; 82, 83; 82, 84; 83, identifier:row; 84, integer:0; 85, try_statement; 85, 86; 85, 101; 86, block; 86, 87; 87, expression_statement; 87, 88; 88, assignment; 88, 89; 88, 90; 89, identifier:field_type; 90, call; 90, 91; 90, 96; 91, attribute; 91, 92; 91, 95; 92, attribute; 92, 93; 92, 94; 93, identifier:connection; 94, identifier:introspection; 95, identifier:get_field_type; 96, argument_list; 96, 97; 96, 100; 97, subscript; 97, 98; 97, 99; 98, identifier:row; 99, integer:1; 100, identifier:row; 101, except_clause; 101, 102; 101, 106; 102, as_pattern; 102, 103; 102, 104; 103, identifier:KeyError; 104, as_pattern_target; 104, 105; 105, identifier:e; 106, block; 106, 107; 107, expression_statement; 107, 108; 108, assignment; 108, 109; 108, 110; 109, identifier:field_type; 110, string:'Unknown'; 111, expression_statement; 111, 112; 112, call; 112, 113; 112, 116; 113, attribute; 113, 114; 113, 115; 114, identifier:td; 115, identifier:append; 116, argument_list; 116, 117; 117, tuple; 117, 118; 117, 119; 118, identifier:column_name; 119, identifier:field_type; 120, expression_statement; 120, 121; 121, call; 121, 122; 121, 125; 122, attribute; 122, 123; 122, 124; 123, identifier:ret; 124, identifier:append; 125, argument_list; 125, 126; 126, tuple; 126, 127; 126, 128; 127, identifier:table_name; 128, identifier:td; 129, return_statement; 129, 130; 130, identifier:ret
def build_schema_info(connection_alias): """ Construct schema information via engine-specific queries of the tables in the DB. :return: Schema information of the following form, sorted by db_table_name. [ ("db_table_name", [ ("db_column_name", "DbFieldType"), (...), ] ) ] """ connection = get_valid_connection(connection_alias) ret = [] with connection.cursor() as cursor: tables_to_introspect = connection.introspection.table_names(cursor, include_views=_include_views()) for table_name in tables_to_introspect: if not _include_table(table_name): continue td = [] table_description = connection.introspection.get_table_description(cursor, table_name) for row in table_description: column_name = row[0] try: field_type = connection.introspection.get_field_type(row[1], row) except KeyError as e: field_type = 'Unknown' td.append((column_name, field_type)) ret.append((table_name, td)) return ret
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 15; 2, function_name:fetchThreads; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 3, 12; 4, identifier:self; 5, identifier:thread_location; 6, default_parameter; 6, 7; 6, 8; 7, identifier:before; 8, None; 9, default_parameter; 9, 10; 9, 11; 10, identifier:after; 11, None; 12, default_parameter; 12, 13; 12, 14; 13, identifier:limit; 14, None; 15, block; 15, 16; 15, 18; 15, 22; 15, 26; 15, 112; 15, 113; 15, 161; 15, 177; 16, expression_statement; 16, 17; 17, comment; 18, expression_statement; 18, 19; 19, assignment; 19, 20; 19, 21; 20, identifier:threads; 21, list:[]; 22, expression_statement; 22, 23; 23, assignment; 23, 24; 23, 25; 24, identifier:last_thread_timestamp; 25, None; 26, while_statement; 26, 27; 26, 28; 26, 29; 27, True; 28, comment; 29, block; 29, 30; 29, 41; 29, 42; 29, 56; 29, 76; 29, 85; 29, 86; 30, if_statement; 30, 31; 30, 39; 31, boolean_operator:and; 31, 32; 31, 33; 32, identifier:limit; 33, comparison_operator:>=; 33, 34; 33, 38; 34, call; 34, 35; 34, 36; 35, identifier:len; 36, argument_list; 36, 37; 37, identifier:threads; 38, identifier:limit; 39, block; 39, 40; 40, break_statement; 41, comment; 42, expression_statement; 42, 43; 43, assignment; 43, 44; 43, 45; 44, identifier:candidates; 45, call; 45, 46; 45, 49; 46, attribute; 46, 47; 46, 48; 47, identifier:self; 48, identifier:fetchThreadList; 49, argument_list; 49, 50; 49, 53; 50, keyword_argument; 50, 51; 50, 52; 51, identifier:before; 52, identifier:last_thread_timestamp; 53, keyword_argument; 53, 54; 53, 55; 54, identifier:thread_location; 55, identifier:thread_location; 56, if_statement; 56, 57; 56, 63; 56, 72; 57, comparison_operator:>; 57, 58; 57, 62; 58, call; 58, 59; 58, 60; 59, identifier:len; 60, argument_list; 60, 61; 61, identifier:candidates; 62, integer:1; 63, block; 63, 64; 64, expression_statement; 64, 65; 65, augmented_assignment:+=; 65, 66; 65, 67; 66, identifier:threads; 67, subscript; 67, 68; 67, 69; 68, identifier:candidates; 69, slice; 69, 70; 69, 71; 70, integer:1; 71, colon; 72, else_clause; 72, 73; 72, 74; 73, comment; 74, block; 74, 75; 75, break_statement; 76, expression_statement; 76, 77; 77, assignment; 77, 78; 77, 79; 78, identifier:last_thread_timestamp; 79, attribute; 79, 80; 79, 84; 80, subscript; 80, 81; 80, 82; 81, identifier:threads; 82, unary_operator:-; 82, 83; 83, integer:1; 84, identifier:last_message_timestamp; 85, comment; 86, if_statement; 86, 87; 86, 110; 87, boolean_operator:or; 87, 88; 87, 99; 88, parenthesized_expression; 88, 89; 89, boolean_operator:and; 89, 90; 89, 93; 90, comparison_operator:is; 90, 91; 90, 92; 91, identifier:before; 92, None; 93, comparison_operator:>; 93, 94; 93, 98; 94, call; 94, 95; 94, 96; 95, identifier:int; 96, argument_list; 96, 97; 97, identifier:last_thread_timestamp; 98, identifier:before; 99, parenthesized_expression; 99, 100; 100, boolean_operator:and; 100, 101; 100, 104; 101, comparison_operator:is; 101, 102; 101, 103; 102, identifier:after; 103, None; 104, comparison_operator:<; 104, 105; 104, 109; 105, call; 105, 106; 105, 107; 106, identifier:int; 107, argument_list; 107, 108; 108, identifier:last_thread_timestamp; 109, identifier:after; 110, block; 110, 111; 111, break_statement; 112, comment; 113, if_statement; 113, 114; 113, 121; 114, boolean_operator:or; 114, 115; 114, 118; 115, comparison_operator:is; 115, 116; 115, 117; 116, identifier:before; 117, None; 118, comparison_operator:is; 118, 119; 118, 120; 119, identifier:after; 120, None; 121, block; 121, 122; 122, for_statement; 122, 123; 122, 124; 122, 125; 123, identifier:t; 124, identifier:threads; 125, block; 125, 126; 125, 135; 126, expression_statement; 126, 127; 127, assignment; 127, 128; 127, 129; 128, identifier:last_message_timestamp; 129, call; 129, 130; 129, 131; 130, identifier:int; 131, argument_list; 131, 132; 132, attribute; 132, 133; 132, 134; 133, identifier:t; 134, identifier:last_message_timestamp; 135, if_statement; 135, 136; 135, 153; 136, boolean_operator:or; 136, 137; 136, 145; 137, parenthesized_expression; 137, 138; 138, boolean_operator:and; 138, 139; 138, 142; 139, comparison_operator:is; 139, 140; 139, 141; 140, identifier:before; 141, None; 142, comparison_operator:>; 142, 143; 142, 144; 143, identifier:last_message_timestamp; 144, identifier:before; 145, parenthesized_expression; 145, 146; 146, boolean_operator:and; 146, 147; 146, 150; 147, comparison_operator:is; 147, 148; 147, 149; 148, identifier:after; 149, None; 150, comparison_operator:<; 150, 151; 150, 152; 151, identifier:last_message_timestamp; 152, identifier:after; 153, block; 153, 154; 154, expression_statement; 154, 155; 155, call; 155, 156; 155, 159; 156, attribute; 156, 157; 156, 158; 157, identifier:threads; 158, identifier:remove; 159, argument_list; 159, 160; 160, identifier:t; 161, if_statement; 161, 162; 161, 170; 162, boolean_operator:and; 162, 163; 162, 164; 163, identifier:limit; 164, comparison_operator:>; 164, 165; 164, 169; 165, call; 165, 166; 165, 167; 166, identifier:len; 167, argument_list; 167, 168; 168, identifier:threads; 169, identifier:limit; 170, block; 170, 171; 171, return_statement; 171, 172; 172, subscript; 172, 173; 172, 174; 173, identifier:threads; 174, slice; 174, 175; 174, 176; 175, colon; 176, identifier:limit; 177, return_statement; 177, 178; 178, identifier:threads
def fetchThreads(self, thread_location, before=None, after=None, limit=None): """ Get all threads in thread_location. Threads will be sorted from newest to oldest. :param thread_location: models.ThreadLocation: INBOX, PENDING, ARCHIVED or OTHER :param before: Fetch only thread before this epoch (in ms) (default all threads) :param after: Fetch only thread after this epoch (in ms) (default all threads) :param limit: The max. amount of threads to fetch (default all threads) :return: :class:`models.Thread` objects :rtype: list :raises: FBchatException if request failed """ threads = [] last_thread_timestamp = None while True: # break if limit is exceeded if limit and len(threads) >= limit: break # fetchThreadList returns at max 20 threads before last_thread_timestamp (included) candidates = self.fetchThreadList( before=last_thread_timestamp, thread_location=thread_location ) if len(candidates) > 1: threads += candidates[1:] else: # End of threads break last_thread_timestamp = threads[-1].last_message_timestamp # FB returns a sorted list of threads if (before is not None and int(last_thread_timestamp) > before) or ( after is not None and int(last_thread_timestamp) < after ): break # Return only threads between before and after (if set) if before is not None or after is not None: for t in threads: last_message_timestamp = int(t.last_message_timestamp) if (before is not None and last_message_timestamp > before) or ( after is not None and last_message_timestamp < after ): threads.remove(t) if limit and len(threads) > limit: return threads[:limit] return threads
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:_get_key_value; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:self; 5, identifier:key; 6, default_parameter; 6, 7; 6, 8; 7, identifier:is_hll; 8, False; 9, block; 9, 10; 9, 12; 10, expression_statement; 10, 11; 11, string:''' Returns the proper key value for the stats @param key: the redis key @param is_hll: the key is a HyperLogLog, else is a sorted set '''; 12, if_statement; 12, 13; 12, 14; 12, 15; 12, 26; 13, identifier:is_hll; 14, comment; 15, block; 15, 16; 16, return_statement; 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:redis_conn; 22, identifier:execute_command; 23, argument_list; 23, 24; 23, 25; 24, string:"PFCOUNT"; 25, identifier:key; 26, else_clause; 26, 27; 26, 28; 27, comment; 28, block; 28, 29; 29, return_statement; 29, 30; 30, call; 30, 31; 30, 36; 31, attribute; 31, 32; 31, 35; 32, attribute; 32, 33; 32, 34; 33, identifier:self; 34, identifier:redis_conn; 35, identifier:zcard; 36, argument_list; 36, 37; 37, identifier:key
def _get_key_value(self, key, is_hll=False): ''' Returns the proper key value for the stats @param key: the redis key @param is_hll: the key is a HyperLogLog, else is a sorted set ''' if is_hll: # get hll value return self.redis_conn.execute_command("PFCOUNT", key) else: # get zcard value return self.redis_conn.zcard(key)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:_get_bin; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:key; 6, block; 6, 7; 6, 9; 6, 10; 6, 14; 6, 15; 6, 65; 7, expression_statement; 7, 8; 8, string:''' Returns a binned dictionary based on redis zscore @return: The sorted dict '''; 9, comment; 10, expression_statement; 10, 11; 11, assignment; 11, 12; 11, 13; 12, identifier:sortedDict; 13, dictionary; 14, comment; 15, for_statement; 15, 16; 15, 17; 15, 25; 16, identifier:item; 17, call; 17, 18; 17, 23; 18, attribute; 18, 19; 18, 22; 19, attribute; 19, 20; 19, 21; 20, identifier:self; 21, identifier:redis_conn; 22, identifier:zscan_iter; 23, argument_list; 23, 24; 24, identifier:key; 25, block; 25, 26; 25, 37; 25, 38; 25, 45; 25, 56; 26, expression_statement; 26, 27; 27, assignment; 27, 28; 27, 29; 28, identifier:my_item; 29, call; 29, 30; 29, 33; 30, attribute; 30, 31; 30, 32; 31, identifier:ujson; 32, identifier:loads; 33, argument_list; 33, 34; 34, subscript; 34, 35; 34, 36; 35, identifier:item; 36, integer:0; 37, comment; 38, expression_statement; 38, 39; 39, assignment; 39, 40; 39, 41; 40, identifier:my_score; 41, unary_operator:-; 41, 42; 42, subscript; 42, 43; 42, 44; 43, identifier:item; 44, integer:1; 45, if_statement; 45, 46; 45, 49; 46, comparison_operator:not; 46, 47; 46, 48; 47, identifier:my_score; 48, identifier:sortedDict; 49, block; 49, 50; 50, expression_statement; 50, 51; 51, assignment; 51, 52; 51, 55; 52, subscript; 52, 53; 52, 54; 53, identifier:sortedDict; 54, identifier:my_score; 55, list:[]; 56, expression_statement; 56, 57; 57, call; 57, 58; 57, 63; 58, attribute; 58, 59; 58, 62; 59, subscript; 59, 60; 59, 61; 60, identifier:sortedDict; 61, identifier:my_score; 62, identifier:append; 63, argument_list; 63, 64; 64, identifier:my_item; 65, return_statement; 65, 66; 66, identifier:sortedDict
def _get_bin(self, key): ''' Returns a binned dictionary based on redis zscore @return: The sorted dict ''' # keys based on score sortedDict = {} # this doesnt return them in order, need to bin first for item in self.redis_conn.zscan_iter(key): my_item = ujson.loads(item[0]) # score is negated in redis my_score = -item[1] if my_score not in sortedDict: sortedDict[my_score] = [] sortedDict[my_score].append(my_item) return sortedDict
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:_is_viable_phone_number; 3, parameters; 3, 4; 4, identifier:number; 5, block; 5, 6; 5, 8; 5, 18; 5, 26; 6, expression_statement; 6, 7; 7, comment; 8, if_statement; 8, 9; 8, 15; 9, comparison_operator:<; 9, 10; 9, 14; 10, call; 10, 11; 10, 12; 11, identifier:len; 12, argument_list; 12, 13; 13, identifier:number; 14, identifier:_MIN_LENGTH_FOR_NSN; 15, block; 15, 16; 16, return_statement; 16, 17; 17, False; 18, expression_statement; 18, 19; 19, assignment; 19, 20; 19, 21; 20, identifier:match; 21, call; 21, 22; 21, 23; 22, identifier:fullmatch; 23, argument_list; 23, 24; 23, 25; 24, identifier:_VALID_PHONE_NUMBER_PATTERN; 25, identifier:number; 26, return_statement; 26, 27; 27, call; 27, 28; 27, 29; 28, identifier:bool; 29, argument_list; 29, 30; 30, identifier:match
def _is_viable_phone_number(number): """Checks to see if a string could possibly be a phone number. At the moment, checks to see that the string begins with at least 2 digits, ignoring any punctuation commonly found in phone numbers. This method does not require the number to be normalized in advance - but does assume that leading non-number symbols have been removed, such as by the method _extract_possible_number. Arguments: number -- string to be checked for viability as a phone number Returns True if the number could be a phone number of some sort, otherwise False """ if len(number) < _MIN_LENGTH_FOR_NSN: return False match = fullmatch(_VALID_PHONE_NUMBER_PATTERN, number) return bool(match)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:__encoded_params_for_signature; 3, parameters; 3, 4; 3, 5; 4, identifier:cls; 5, identifier:params; 6, block; 6, 7; 6, 9; 6, 113; 7, expression_statement; 7, 8; 8, comment; 9, function_definition; 9, 10; 9, 11; 9, 13; 10, function_name:encoded_pairs; 11, parameters; 11, 12; 12, identifier:params; 13, block; 13, 14; 14, for_statement; 14, 15; 14, 18; 14, 24; 15, pattern_list; 15, 16; 15, 17; 16, identifier:k; 17, identifier:v; 18, call; 18, 19; 18, 22; 19, attribute; 19, 20; 19, 21; 20, identifier:six; 21, identifier:iteritems; 22, argument_list; 22, 23; 23, identifier:params; 24, block; 24, 25; 24, 31; 24, 65; 24, 66; 24, 85; 24, 98; 25, if_statement; 25, 26; 25, 29; 26, comparison_operator:==; 26, 27; 26, 28; 27, identifier:k; 28, string:'hmac'; 29, block; 29, 30; 30, continue_statement; 31, if_statement; 31, 32; 31, 38; 31, 39; 32, call; 32, 33; 32, 36; 33, attribute; 33, 34; 33, 35; 34, identifier:k; 35, identifier:endswith; 36, argument_list; 36, 37; 37, string:'[]'; 38, comment; 39, block; 39, 40; 39, 49; 40, expression_statement; 40, 41; 41, assignment; 41, 42; 41, 43; 42, identifier:k; 43, call; 43, 44; 43, 47; 44, attribute; 44, 45; 44, 46; 45, identifier:k; 46, identifier:rstrip; 47, argument_list; 47, 48; 48, string:'[]'; 49, expression_statement; 49, 50; 50, assignment; 50, 51; 50, 52; 51, identifier:v; 52, call; 52, 53; 52, 56; 53, attribute; 53, 54; 53, 55; 54, identifier:json; 55, identifier:dumps; 56, argument_list; 56, 57; 57, call; 57, 58; 57, 59; 58, identifier:list; 59, argument_list; 59, 60; 60, call; 60, 61; 60, 62; 61, identifier:map; 62, argument_list; 62, 63; 62, 64; 63, identifier:str; 64, identifier:v; 65, comment; 66, expression_statement; 66, 67; 67, assignment; 67, 68; 67, 69; 68, identifier:k; 69, call; 69, 70; 69, 82; 70, attribute; 70, 71; 70, 81; 71, call; 71, 72; 71, 78; 72, attribute; 72, 73; 72, 77; 73, call; 73, 74; 73, 75; 74, identifier:str; 75, argument_list; 75, 76; 76, identifier:k; 77, identifier:replace; 78, argument_list; 78, 79; 78, 80; 79, string:"%"; 80, string:"%25"; 81, identifier:replace; 82, argument_list; 82, 83; 82, 84; 83, string:"="; 84, string:"%3D"; 85, expression_statement; 85, 86; 86, assignment; 86, 87; 86, 88; 87, identifier:v; 88, call; 88, 89; 88, 95; 89, attribute; 89, 90; 89, 94; 90, call; 90, 91; 90, 92; 91, identifier:str; 92, argument_list; 92, 93; 93, identifier:v; 94, identifier:replace; 95, argument_list; 95, 96; 95, 97; 96, string:"%"; 97, string:"%25"; 98, expression_statement; 98, 99; 99, yield; 99, 100; 100, call; 100, 101; 100, 110; 101, attribute; 101, 102; 101, 109; 102, call; 102, 103; 102, 106; 103, attribute; 103, 104; 103, 105; 104, string:'{0}={1}'; 105, identifier:format; 106, argument_list; 106, 107; 106, 108; 107, identifier:k; 108, identifier:v; 109, identifier:replace; 110, argument_list; 110, 111; 110, 112; 111, string:"&"; 112, string:"%26"; 113, return_statement; 113, 114; 114, call; 114, 115; 114, 118; 115, attribute; 115, 116; 115, 117; 116, string:"&"; 117, identifier:join; 118, argument_list; 118, 119; 119, call; 119, 120; 119, 121; 120, identifier:sorted; 121, argument_list; 121, 122; 122, call; 122, 123; 122, 124; 123, identifier:encoded_pairs; 124, argument_list; 124, 125; 125, identifier:params
def __encoded_params_for_signature(cls, params): """ Sort and combine query parameters into a single string, excluding those that should be removed and joining with '&' """ def encoded_pairs(params): for k, v in six.iteritems(params): if k == 'hmac': continue if k.endswith('[]'): #foo[]=1&foo[]=2 has to be transformed as foo=["1", "2"] note the whitespace after comma k = k.rstrip('[]') v = json.dumps(list(map(str, v))) # escape delimiters to avoid tampering k = str(k).replace("%", "%25").replace("=", "%3D") v = str(v).replace("%", "%25") yield '{0}={1}'.format(k, v).replace("&", "%26") return "&".join(sorted(encoded_pairs(params)))
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:group_keys; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 6, expression_statement; 6, 7; 7, comment; 8, for_statement; 8, 9; 8, 10; 8, 22; 9, identifier:key; 10, call; 10, 11; 10, 12; 11, identifier:sorted; 12, argument_list; 12, 13; 13, call; 13, 14; 13, 15; 14, identifier:listdir; 15, argument_list; 15, 16; 15, 19; 16, attribute; 16, 17; 16, 18; 17, identifier:self; 18, identifier:_store; 19, attribute; 19, 20; 19, 21; 20, identifier:self; 21, identifier:_path; 22, block; 22, 23; 22, 31; 23, expression_statement; 23, 24; 24, assignment; 24, 25; 24, 26; 25, identifier:path; 26, binary_operator:+; 26, 27; 26, 30; 27, attribute; 27, 28; 27, 29; 28, identifier:self; 29, identifier:_key_prefix; 30, identifier:key; 31, if_statement; 31, 32; 31, 39; 32, call; 32, 33; 32, 34; 33, identifier:contains_group; 34, argument_list; 34, 35; 34, 38; 35, attribute; 35, 36; 35, 37; 36, identifier:self; 37, identifier:_store; 38, identifier:path; 39, block; 39, 40; 40, expression_statement; 40, 41; 41, yield; 41, 42; 42, identifier:key
def group_keys(self): """Return an iterator over member names for groups only. Examples -------- >>> import zarr >>> g1 = zarr.group() >>> g2 = g1.create_group('foo') >>> g3 = g1.create_group('bar') >>> d1 = g1.create_dataset('baz', shape=100, chunks=10) >>> d2 = g1.create_dataset('quux', shape=200, chunks=20) >>> sorted(g1.group_keys()) ['bar', 'foo'] """ for key in sorted(listdir(self._store, self._path)): path = self._key_prefix + key if contains_group(self._store, path): yield key
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:array_keys; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 6, expression_statement; 6, 7; 7, comment; 8, for_statement; 8, 9; 8, 10; 8, 22; 9, identifier:key; 10, call; 10, 11; 10, 12; 11, identifier:sorted; 12, argument_list; 12, 13; 13, call; 13, 14; 13, 15; 14, identifier:listdir; 15, argument_list; 15, 16; 15, 19; 16, attribute; 16, 17; 16, 18; 17, identifier:self; 18, identifier:_store; 19, attribute; 19, 20; 19, 21; 20, identifier:self; 21, identifier:_path; 22, block; 22, 23; 22, 31; 23, expression_statement; 23, 24; 24, assignment; 24, 25; 24, 26; 25, identifier:path; 26, binary_operator:+; 26, 27; 26, 30; 27, attribute; 27, 28; 27, 29; 28, identifier:self; 29, identifier:_key_prefix; 30, identifier:key; 31, if_statement; 31, 32; 31, 39; 32, call; 32, 33; 32, 34; 33, identifier:contains_array; 34, argument_list; 34, 35; 34, 38; 35, attribute; 35, 36; 35, 37; 36, identifier:self; 37, identifier:_store; 38, identifier:path; 39, block; 39, 40; 40, expression_statement; 40, 41; 41, yield; 41, 42; 42, identifier:key
def array_keys(self): """Return an iterator over member names for arrays only. Examples -------- >>> import zarr >>> g1 = zarr.group() >>> g2 = g1.create_group('foo') >>> g3 = g1.create_group('bar') >>> d1 = g1.create_dataset('baz', shape=100, chunks=10) >>> d2 = g1.create_dataset('quux', shape=200, chunks=20) >>> sorted(g1.array_keys()) ['baz', 'quux'] """ for key in sorted(listdir(self._store, self._path)): path = self._key_prefix + key if contains_array(self._store, path): yield key
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 36; 2, function_name:init_array; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 3, 12; 3, 15; 3, 18; 3, 21; 3, 24; 3, 27; 3, 30; 3, 33; 4, identifier:store; 5, identifier:shape; 6, default_parameter; 6, 7; 6, 8; 7, identifier:chunks; 8, True; 9, default_parameter; 9, 10; 9, 11; 10, identifier:dtype; 11, None; 12, default_parameter; 12, 13; 12, 14; 13, identifier:compressor; 14, string:'default'; 15, default_parameter; 15, 16; 15, 17; 16, identifier:fill_value; 17, None; 18, default_parameter; 18, 19; 18, 20; 19, identifier:order; 20, string:'C'; 21, default_parameter; 21, 22; 21, 23; 22, identifier:overwrite; 23, False; 24, default_parameter; 24, 25; 24, 26; 25, identifier:path; 26, None; 27, default_parameter; 27, 28; 27, 29; 28, identifier:chunk_store; 29, None; 30, default_parameter; 30, 31; 30, 32; 31, identifier:filters; 32, None; 33, default_parameter; 33, 34; 33, 35; 34, identifier:object_codec; 35, None; 36, block; 36, 37; 36, 39; 36, 40; 36, 47; 36, 48; 36, 62; 37, expression_statement; 37, 38; 38, comment; 39, comment; 40, expression_statement; 40, 41; 41, assignment; 41, 42; 41, 43; 42, identifier:path; 43, call; 43, 44; 43, 45; 44, identifier:normalize_storage_path; 45, argument_list; 45, 46; 46, identifier:path; 47, comment; 48, expression_statement; 48, 49; 49, call; 49, 50; 49, 51; 50, identifier:_require_parent_group; 51, argument_list; 51, 52; 51, 53; 51, 56; 51, 59; 52, identifier:path; 53, keyword_argument; 53, 54; 53, 55; 54, identifier:store; 55, identifier:store; 56, keyword_argument; 56, 57; 56, 58; 57, identifier:chunk_store; 58, identifier:chunk_store; 59, keyword_argument; 59, 60; 59, 61; 60, identifier:overwrite; 61, identifier:overwrite; 62, expression_statement; 62, 63; 63, call; 63, 64; 63, 65; 64, identifier:_init_array_metadata; 65, argument_list; 65, 66; 65, 67; 65, 70; 65, 73; 65, 76; 65, 79; 65, 82; 65, 85; 65, 88; 65, 91; 65, 94; 65, 97; 66, identifier:store; 67, keyword_argument; 67, 68; 67, 69; 68, identifier:shape; 69, identifier:shape; 70, keyword_argument; 70, 71; 70, 72; 71, identifier:chunks; 72, identifier:chunks; 73, keyword_argument; 73, 74; 73, 75; 74, identifier:dtype; 75, identifier:dtype; 76, keyword_argument; 76, 77; 76, 78; 77, identifier:compressor; 78, identifier:compressor; 79, keyword_argument; 79, 80; 79, 81; 80, identifier:fill_value; 81, identifier:fill_value; 82, keyword_argument; 82, 83; 82, 84; 83, identifier:order; 84, identifier:order; 85, keyword_argument; 85, 86; 85, 87; 86, identifier:overwrite; 87, identifier:overwrite; 88, keyword_argument; 88, 89; 88, 90; 89, identifier:path; 90, identifier:path; 91, keyword_argument; 91, 92; 91, 93; 92, identifier:chunk_store; 93, identifier:chunk_store; 94, keyword_argument; 94, 95; 94, 96; 95, identifier:filters; 96, identifier:filters; 97, keyword_argument; 97, 98; 97, 99; 98, identifier:object_codec; 99, identifier:object_codec
def init_array(store, shape, chunks=True, dtype=None, compressor='default', fill_value=None, order='C', overwrite=False, path=None, chunk_store=None, filters=None, object_codec=None): """Initialize an array store with the given configuration. Note that this is a low-level function and there should be no need to call this directly from user code. Parameters ---------- store : MutableMapping A mapping that supports string keys and bytes-like values. shape : int or tuple of ints Array shape. chunks : int or tuple of ints, optional Chunk shape. If True, will be guessed from `shape` and `dtype`. If False, will be set to `shape`, i.e., single chunk for the whole array. dtype : string or dtype, optional NumPy dtype. compressor : Codec, optional Primary compressor. fill_value : object Default value to use for uninitialized portions of the array. order : {'C', 'F'}, optional Memory layout to be used within each chunk. overwrite : bool, optional If True, erase all data in `store` prior to initialisation. path : string, optional Path under which array is stored. chunk_store : MutableMapping, optional Separate storage for chunks. If not provided, `store` will be used for storage of both chunks and metadata. filters : sequence, optional Sequence of filters to use to encode chunk data prior to compression. object_codec : Codec, optional A codec to encode object arrays, only needed if dtype=object. Examples -------- Initialize an array store:: >>> from zarr.storage import init_array >>> store = dict() >>> init_array(store, shape=(10000, 10000), chunks=(1000, 1000)) >>> sorted(store.keys()) ['.zarray'] Array metadata is stored as JSON:: >>> print(store['.zarray'].decode()) { "chunks": [ 1000, 1000 ], "compressor": { "blocksize": 0, "clevel": 5, "cname": "lz4", "id": "blosc", "shuffle": 1 }, "dtype": "<f8", "fill_value": null, "filters": null, "order": "C", "shape": [ 10000, 10000 ], "zarr_format": 2 } Initialize an array using a storage path:: >>> store = dict() >>> init_array(store, shape=100000000, chunks=1000000, dtype='i1', path='foo') >>> sorted(store.keys()) ['.zgroup', 'foo/.zarray'] >>> print(store['foo/.zarray'].decode()) { "chunks": [ 1000000 ], "compressor": { "blocksize": 0, "clevel": 5, "cname": "lz4", "id": "blosc", "shuffle": 1 }, "dtype": "|i1", "fill_value": null, "filters": null, "order": "C", "shape": [ 100000000 ], "zarr_format": 2 } Notes ----- The initialisation process involves normalising all array metadata, encoding as JSON and storing under the '.zarray' key. """ # normalize path path = normalize_storage_path(path) # ensure parent group initialized _require_parent_group(path, store=store, chunk_store=chunk_store, overwrite=overwrite) _init_array_metadata(store, shape=shape, chunks=chunks, dtype=dtype, compressor=compressor, fill_value=fill_value, order=order, overwrite=overwrite, path=path, chunk_store=chunk_store, filters=filters, object_codec=object_codec)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 26; 2, function_name:view; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 14; 3, 17; 3, 20; 3, 23; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:shape; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:chunks; 10, None; 11, default_parameter; 11, 12; 11, 13; 12, identifier:dtype; 13, None; 14, default_parameter; 14, 15; 14, 16; 15, identifier:fill_value; 16, None; 17, default_parameter; 17, 18; 17, 19; 18, identifier:filters; 19, None; 20, default_parameter; 20, 21; 20, 22; 21, identifier:read_only; 22, None; 23, default_parameter; 23, 24; 23, 25; 24, identifier:synchronizer; 25, None; 26, block; 26, 27; 26, 29; 26, 35; 26, 41; 26, 47; 26, 58; 26, 69; 26, 93; 26, 99; 26, 100; 26, 128; 26, 154; 26, 176; 26, 187; 26, 198; 27, expression_statement; 27, 28; 28, comment; 29, expression_statement; 29, 30; 30, assignment; 30, 31; 30, 32; 31, identifier:store; 32, attribute; 32, 33; 32, 34; 33, identifier:self; 34, identifier:_store; 35, expression_statement; 35, 36; 36, assignment; 36, 37; 36, 38; 37, identifier:chunk_store; 38, attribute; 38, 39; 38, 40; 39, identifier:self; 40, identifier:_chunk_store; 41, expression_statement; 41, 42; 42, assignment; 42, 43; 42, 44; 43, identifier:path; 44, attribute; 44, 45; 44, 46; 45, identifier:self; 46, identifier:_path; 47, if_statement; 47, 48; 47, 51; 48, comparison_operator:is; 48, 49; 48, 50; 49, identifier:read_only; 50, None; 51, block; 51, 52; 52, expression_statement; 52, 53; 53, assignment; 53, 54; 53, 55; 54, identifier:read_only; 55, attribute; 55, 56; 55, 57; 56, identifier:self; 57, identifier:_read_only; 58, if_statement; 58, 59; 58, 62; 59, comparison_operator:is; 59, 60; 59, 61; 60, identifier:synchronizer; 61, None; 62, block; 62, 63; 63, expression_statement; 63, 64; 64, assignment; 64, 65; 64, 66; 65, identifier:synchronizer; 66, attribute; 66, 67; 66, 68; 67, identifier:self; 68, identifier:_synchronizer; 69, expression_statement; 69, 70; 70, assignment; 70, 71; 70, 72; 71, identifier:a; 72, call; 72, 73; 72, 74; 73, identifier:Array; 74, argument_list; 74, 75; 74, 78; 74, 81; 74, 84; 74, 87; 74, 90; 75, keyword_argument; 75, 76; 75, 77; 76, identifier:store; 77, identifier:store; 78, keyword_argument; 78, 79; 78, 80; 79, identifier:path; 80, identifier:path; 81, keyword_argument; 81, 82; 81, 83; 82, identifier:chunk_store; 83, identifier:chunk_store; 84, keyword_argument; 84, 85; 84, 86; 85, identifier:read_only; 86, identifier:read_only; 87, keyword_argument; 87, 88; 87, 89; 88, identifier:synchronizer; 89, identifier:synchronizer; 90, keyword_argument; 90, 91; 90, 92; 91, identifier:cache_metadata; 92, True; 93, expression_statement; 93, 94; 94, assignment; 94, 95; 94, 98; 95, attribute; 95, 96; 95, 97; 96, identifier:a; 97, identifier:_is_view; 98, True; 99, comment; 100, if_statement; 100, 101; 100, 104; 100, 111; 101, comparison_operator:is; 101, 102; 101, 103; 102, identifier:dtype; 103, None; 104, block; 104, 105; 105, expression_statement; 105, 106; 106, assignment; 106, 107; 106, 108; 107, identifier:dtype; 108, attribute; 108, 109; 108, 110; 109, identifier:self; 110, identifier:_dtype; 111, else_clause; 111, 112; 112, block; 112, 113; 112, 122; 113, expression_statement; 113, 114; 114, assignment; 114, 115; 114, 116; 115, identifier:dtype; 116, call; 116, 117; 116, 120; 117, attribute; 117, 118; 117, 119; 118, identifier:np; 119, identifier:dtype; 120, argument_list; 120, 121; 121, identifier:dtype; 122, expression_statement; 122, 123; 123, assignment; 123, 124; 123, 127; 124, attribute; 124, 125; 124, 126; 125, identifier:a; 126, identifier:_dtype; 127, identifier:dtype; 128, if_statement; 128, 129; 128, 132; 128, 139; 129, comparison_operator:is; 129, 130; 129, 131; 130, identifier:shape; 131, None; 132, block; 132, 133; 133, expression_statement; 133, 134; 134, assignment; 134, 135; 134, 136; 135, identifier:shape; 136, attribute; 136, 137; 136, 138; 137, identifier:self; 138, identifier:_shape; 139, else_clause; 139, 140; 140, block; 140, 141; 140, 148; 141, expression_statement; 141, 142; 142, assignment; 142, 143; 142, 144; 143, identifier:shape; 144, call; 144, 145; 144, 146; 145, identifier:normalize_shape; 146, argument_list; 146, 147; 147, identifier:shape; 148, expression_statement; 148, 149; 149, assignment; 149, 150; 149, 153; 150, attribute; 150, 151; 150, 152; 151, identifier:a; 152, identifier:_shape; 153, identifier:shape; 154, if_statement; 154, 155; 154, 158; 155, comparison_operator:is; 155, 156; 155, 157; 156, identifier:chunks; 157, None; 158, block; 158, 159; 158, 170; 159, expression_statement; 159, 160; 160, assignment; 160, 161; 160, 162; 161, identifier:chunks; 162, call; 162, 163; 162, 164; 163, identifier:normalize_chunks; 164, argument_list; 164, 165; 164, 166; 164, 167; 165, identifier:chunks; 166, identifier:shape; 167, attribute; 167, 168; 167, 169; 168, identifier:dtype; 169, identifier:itemsize; 170, expression_statement; 170, 171; 171, assignment; 171, 172; 171, 175; 172, attribute; 172, 173; 172, 174; 173, identifier:a; 174, identifier:_chunks; 175, identifier:chunks; 176, if_statement; 176, 177; 176, 180; 177, comparison_operator:is; 177, 178; 177, 179; 178, identifier:fill_value; 179, None; 180, block; 180, 181; 181, expression_statement; 181, 182; 182, assignment; 182, 183; 182, 186; 183, attribute; 183, 184; 183, 185; 184, identifier:a; 185, identifier:_fill_value; 186, identifier:fill_value; 187, if_statement; 187, 188; 187, 191; 188, comparison_operator:is; 188, 189; 188, 190; 189, identifier:filters; 190, None; 191, block; 191, 192; 192, expression_statement; 192, 193; 193, assignment; 193, 194; 193, 197; 194, attribute; 194, 195; 194, 196; 195, identifier:a; 196, identifier:_filters; 197, identifier:filters; 198, return_statement; 198, 199; 199, identifier:a
def view(self, shape=None, chunks=None, dtype=None, fill_value=None, filters=None, read_only=None, synchronizer=None): """Return an array sharing the same data. Parameters ---------- shape : int or tuple of ints Array shape. chunks : int or tuple of ints, optional Chunk shape. dtype : string or dtype, optional NumPy dtype. fill_value : object Default value to use for uninitialized portions of the array. filters : sequence, optional Sequence of filters to use to encode chunk data prior to compression. read_only : bool, optional True if array should be protected against modification. synchronizer : object, optional Array synchronizer. Notes ----- WARNING: This is an experimental feature and should be used with care. There are plenty of ways to generate errors and/or cause data corruption. Examples -------- Bypass filters: >>> import zarr >>> import numpy as np >>> np.random.seed(42) >>> labels = ['female', 'male'] >>> data = np.random.choice(labels, size=10000) >>> filters = [zarr.Categorize(labels=labels, ... dtype=data.dtype, ... astype='u1')] >>> a = zarr.array(data, chunks=1000, filters=filters) >>> a[:] array(['female', 'male', 'female', ..., 'male', 'male', 'female'], dtype='<U6') >>> v = a.view(dtype='u1', filters=[]) >>> v.is_view True >>> v[:] array([1, 2, 1, ..., 2, 2, 1], dtype=uint8) Views can be used to modify data: >>> x = v[:] >>> x.sort() >>> v[:] = x >>> v[:] array([1, 1, 1, ..., 2, 2, 2], dtype=uint8) >>> a[:] array(['female', 'female', 'female', ..., 'male', 'male', 'male'], dtype='<U6') View as a different dtype with the same item size: >>> data = np.random.randint(0, 2, size=10000, dtype='u1') >>> a = zarr.array(data, chunks=1000) >>> a[:] array([0, 0, 1, ..., 1, 0, 0], dtype=uint8) >>> v = a.view(dtype=bool) >>> v[:] array([False, False, True, ..., True, False, False]) >>> np.all(a[:].view(dtype=bool) == v[:]) True An array can be viewed with a dtype with a different item size, however some care is needed to adjust the shape and chunk shape so that chunk data is interpreted correctly: >>> data = np.arange(10000, dtype='u2') >>> a = zarr.array(data, chunks=1000) >>> a[:10] array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], dtype=uint16) >>> v = a.view(dtype='u1', shape=20000, chunks=2000) >>> v[:10] array([0, 0, 1, 0, 2, 0, 3, 0, 4, 0], dtype=uint8) >>> np.all(a[:].view('u1') == v[:]) True Change fill value for uninitialized chunks: >>> a = zarr.full(10000, chunks=1000, fill_value=-1, dtype='i1') >>> a[:] array([-1, -1, -1, ..., -1, -1, -1], dtype=int8) >>> v = a.view(fill_value=42) >>> v[:] array([42, 42, 42, ..., 42, 42, 42], dtype=int8) Note that resizing or appending to views is not permitted: >>> a = zarr.empty(10000) >>> v = a.view() >>> try: ... v.resize(20000) ... except PermissionError as e: ... print(e) operation not permitted for views """ store = self._store chunk_store = self._chunk_store path = self._path if read_only is None: read_only = self._read_only if synchronizer is None: synchronizer = self._synchronizer a = Array(store=store, path=path, chunk_store=chunk_store, read_only=read_only, synchronizer=synchronizer, cache_metadata=True) a._is_view = True # allow override of some properties if dtype is None: dtype = self._dtype else: dtype = np.dtype(dtype) a._dtype = dtype if shape is None: shape = self._shape else: shape = normalize_shape(shape) a._shape = shape if chunks is not None: chunks = normalize_chunks(chunks, shape, dtype.itemsize) a._chunks = chunks if fill_value is not None: a._fill_value = fill_value if filters is not None: a._filters = filters return a
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:image_field_data; 3, parameters; 3, 4; 3, 5; 4, identifier:request; 5, default_parameter; 5, 6; 5, 7; 6, identifier:include_empty_option; 7, False; 8, block; 8, 9; 8, 11; 8, 39; 8, 53; 8, 63; 8, 95; 8, 107; 9, expression_statement; 9, 10; 10, comment; 11, try_statement; 11, 12; 11, 25; 12, block; 12, 13; 13, expression_statement; 13, 14; 14, assignment; 14, 15; 14, 16; 15, identifier:images; 16, call; 16, 17; 16, 18; 17, identifier:get_available_images; 18, argument_list; 18, 19; 18, 20; 19, identifier:request; 20, attribute; 20, 21; 20, 24; 21, attribute; 21, 22; 21, 23; 22, identifier:request; 23, identifier:user; 24, identifier:project_id; 25, except_clause; 25, 26; 25, 27; 26, identifier:Exception; 27, block; 27, 28; 28, expression_statement; 28, 29; 29, call; 29, 30; 29, 33; 30, attribute; 30, 31; 30, 32; 31, identifier:exceptions; 32, identifier:handle; 33, argument_list; 33, 34; 33, 35; 34, identifier:request; 35, call; 35, 36; 35, 37; 36, identifier:_; 37, argument_list; 37, 38; 38, string:'Unable to retrieve images'; 39, expression_statement; 39, 40; 40, call; 40, 41; 40, 44; 41, attribute; 41, 42; 41, 43; 42, identifier:images; 43, identifier:sort; 44, argument_list; 44, 45; 45, keyword_argument; 45, 46; 45, 47; 46, identifier:key; 47, lambda; 47, 48; 47, 50; 48, lambda_parameters; 48, 49; 49, identifier:c; 50, attribute; 50, 51; 50, 52; 51, identifier:c; 52, identifier:name; 53, expression_statement; 53, 54; 54, assignment; 54, 55; 54, 56; 55, identifier:images_list; 56, list:[('', _('Select Image'))]; 56, 57; 57, tuple; 57, 58; 57, 59; 58, string:''; 59, call; 59, 60; 59, 61; 60, identifier:_; 61, argument_list; 61, 62; 62, string:'Select Image'; 63, for_statement; 63, 64; 63, 65; 63, 66; 64, identifier:image; 65, identifier:images; 66, block; 66, 67; 66, 84; 67, expression_statement; 67, 68; 68, assignment; 68, 69; 68, 70; 69, identifier:image_label; 70, call; 70, 71; 70, 74; 71, attribute; 71, 72; 71, 73; 72, string:u"{} ({})"; 73, identifier:format; 74, argument_list; 74, 75; 74, 78; 75, attribute; 75, 76; 75, 77; 76, identifier:image; 77, identifier:name; 78, call; 78, 79; 78, 80; 79, identifier:filesizeformat; 80, argument_list; 80, 81; 81, attribute; 81, 82; 81, 83; 82, identifier:image; 83, identifier:size; 84, expression_statement; 84, 85; 85, call; 85, 86; 85, 89; 86, attribute; 86, 87; 86, 88; 87, identifier:images_list; 88, identifier:append; 89, argument_list; 89, 90; 90, tuple; 90, 91; 90, 94; 91, attribute; 91, 92; 91, 93; 92, identifier:image; 93, identifier:id; 94, identifier:image_label; 95, if_statement; 95, 96; 95, 98; 96, not_operator; 96, 97; 97, identifier:images; 98, block; 98, 99; 99, return_statement; 99, 100; 100, list:[("", _("No images available")), ]; 100, 101; 101, tuple; 101, 102; 101, 103; 102, string:""; 103, call; 103, 104; 103, 105; 104, identifier:_; 105, argument_list; 105, 106; 106, string:"No images available"; 107, return_statement; 107, 108; 108, identifier:images_list
def image_field_data(request, include_empty_option=False): """Returns a list of tuples of all images. Generates a sorted list of images available. And returns a list of (id, name) tuples. :param request: django http request object :param include_empty_option: flag to include a empty tuple in the front of the list :return: list of (id, name) tuples """ try: images = get_available_images(request, request.user.project_id) except Exception: exceptions.handle(request, _('Unable to retrieve images')) images.sort(key=lambda c: c.name) images_list = [('', _('Select Image'))] for image in images: image_label = u"{} ({})".format(image.name, filesizeformat(image.size)) images_list.append((image.id, image_label)) if not images: return [("", _("No images available")), ] return images_list
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:sort_flavor_list; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:request; 5, identifier:flavors; 6, default_parameter; 6, 7; 6, 8; 7, identifier:with_menu_label; 8, True; 9, block; 9, 10; 9, 12; 9, 45; 10, expression_statement; 10, 11; 11, comment; 12, function_definition; 12, 13; 12, 14; 12, 17; 13, function_name:get_key; 14, parameters; 14, 15; 14, 16; 15, identifier:flavor; 16, identifier:sort_key; 17, block; 17, 18; 18, try_statement; 18, 19; 18, 26; 19, block; 19, 20; 20, return_statement; 20, 21; 21, call; 21, 22; 21, 23; 22, identifier:getattr; 23, argument_list; 23, 24; 23, 25; 24, identifier:flavor; 25, identifier:sort_key; 26, except_clause; 26, 27; 26, 28; 27, identifier:AttributeError; 28, block; 28, 29; 28, 39; 29, expression_statement; 29, 30; 30, call; 30, 31; 30, 34; 31, attribute; 31, 32; 31, 33; 32, identifier:LOG; 33, identifier:warning; 34, argument_list; 34, 35; 34, 38; 35, concatenated_string; 35, 36; 35, 37; 36, string:'Could not find sort key "%s". Using the default '; 37, string:'"ram" instead.'; 38, identifier:sort_key; 39, return_statement; 39, 40; 40, call; 40, 41; 40, 42; 41, identifier:getattr; 42, argument_list; 42, 43; 42, 44; 43, identifier:flavor; 44, string:'ram'; 45, try_statement; 45, 46; 45, 145; 46, block; 46, 47; 46, 56; 46, 66; 46, 76; 46, 100; 46, 143; 47, expression_statement; 47, 48; 48, assignment; 48, 49; 48, 50; 49, identifier:flavor_sort; 50, call; 50, 51; 50, 52; 51, identifier:getattr; 52, argument_list; 52, 53; 52, 54; 52, 55; 53, identifier:settings; 54, string:'CREATE_INSTANCE_FLAVOR_SORT'; 55, dictionary; 56, expression_statement; 56, 57; 57, assignment; 57, 58; 57, 59; 58, identifier:sort_key; 59, call; 59, 60; 59, 63; 60, attribute; 60, 61; 60, 62; 61, identifier:flavor_sort; 62, identifier:get; 63, argument_list; 63, 64; 63, 65; 64, string:'key'; 65, string:'ram'; 66, expression_statement; 66, 67; 67, assignment; 67, 68; 67, 69; 68, identifier:rev; 69, call; 69, 70; 69, 73; 70, attribute; 70, 71; 70, 72; 71, identifier:flavor_sort; 72, identifier:get; 73, argument_list; 73, 74; 73, 75; 74, string:'reverse'; 75, False; 76, if_statement; 76, 77; 76, 82; 76, 94; 77, not_operator; 77, 78; 78, call; 78, 79; 78, 80; 79, identifier:callable; 80, argument_list; 80, 81; 81, identifier:sort_key; 82, block; 82, 83; 83, function_definition; 83, 84; 83, 85; 83, 87; 84, function_name:key; 85, parameters; 85, 86; 86, identifier:flavor; 87, block; 87, 88; 88, return_statement; 88, 89; 89, call; 89, 90; 89, 91; 90, identifier:get_key; 91, argument_list; 91, 92; 91, 93; 92, identifier:flavor; 93, identifier:sort_key; 94, else_clause; 94, 95; 95, block; 95, 96; 96, expression_statement; 96, 97; 97, assignment; 97, 98; 97, 99; 98, identifier:key; 99, identifier:sort_key; 100, if_statement; 100, 101; 100, 102; 100, 128; 101, identifier:with_menu_label; 102, block; 102, 103; 103, expression_statement; 103, 104; 104, assignment; 104, 105; 104, 106; 105, identifier:flavor_list; 106, list_comprehension; 106, 107; 106, 116; 107, tuple; 107, 108; 107, 111; 108, attribute; 108, 109; 108, 110; 109, identifier:flavor; 110, identifier:id; 111, binary_operator:%; 111, 112; 111, 113; 112, string:'%s'; 113, attribute; 113, 114; 113, 115; 114, identifier:flavor; 115, identifier:name; 116, for_in_clause; 116, 117; 116, 118; 117, identifier:flavor; 118, call; 118, 119; 118, 120; 119, identifier:sorted; 120, argument_list; 120, 121; 120, 122; 120, 125; 121, identifier:flavors; 122, keyword_argument; 122, 123; 122, 124; 123, identifier:key; 124, identifier:key; 125, keyword_argument; 125, 126; 125, 127; 126, identifier:reverse; 127, identifier:rev; 128, else_clause; 128, 129; 129, block; 129, 130; 130, expression_statement; 130, 131; 131, assignment; 131, 132; 131, 133; 132, identifier:flavor_list; 133, call; 133, 134; 133, 135; 134, identifier:sorted; 135, argument_list; 135, 136; 135, 137; 135, 140; 136, identifier:flavors; 137, keyword_argument; 137, 138; 137, 139; 138, identifier:key; 139, identifier:key; 140, keyword_argument; 140, 141; 140, 142; 141, identifier:reverse; 142, identifier:rev; 143, return_statement; 143, 144; 144, identifier:flavor_list; 145, except_clause; 145, 146; 145, 147; 146, identifier:Exception; 147, block; 147, 148; 147, 159; 148, expression_statement; 148, 149; 149, call; 149, 150; 149, 153; 150, attribute; 150, 151; 150, 152; 151, identifier:exceptions; 152, identifier:handle; 153, argument_list; 153, 154; 153, 155; 154, identifier:request; 155, call; 155, 156; 155, 157; 156, identifier:_; 157, argument_list; 157, 158; 158, string:'Unable to sort instance flavors.'; 159, return_statement; 159, 160; 160, list:[]
def sort_flavor_list(request, flavors, with_menu_label=True): """Utility method to sort a list of flavors. By default, returns the available flavors, sorted by RAM usage (ascending). Override these behaviours with a ``CREATE_INSTANCE_FLAVOR_SORT`` dict in ``local_settings.py``. """ def get_key(flavor, sort_key): try: return getattr(flavor, sort_key) except AttributeError: LOG.warning('Could not find sort key "%s". Using the default ' '"ram" instead.', sort_key) return getattr(flavor, 'ram') try: flavor_sort = getattr(settings, 'CREATE_INSTANCE_FLAVOR_SORT', {}) sort_key = flavor_sort.get('key', 'ram') rev = flavor_sort.get('reverse', False) if not callable(sort_key): def key(flavor): return get_key(flavor, sort_key) else: key = sort_key if with_menu_label: flavor_list = [(flavor.id, '%s' % flavor.name) for flavor in sorted(flavors, key=key, reverse=rev)] else: flavor_list = sorted(flavors, key=key, reverse=rev) return flavor_list except Exception: exceptions.handle(request, _('Unable to sort instance flavors.')) return []
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 25; 2, function_name:image_list_detailed; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 14; 3, 17; 3, 20; 3, 23; 4, identifier:request; 5, default_parameter; 5, 6; 5, 7; 6, identifier:marker; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:sort_dir; 10, string:'desc'; 11, default_parameter; 11, 12; 11, 13; 12, identifier:sort_key; 13, string:'created_at'; 14, default_parameter; 14, 15; 14, 16; 15, identifier:filters; 16, None; 17, default_parameter; 17, 18; 17, 19; 18, identifier:paginate; 19, False; 20, default_parameter; 20, 21; 20, 22; 21, identifier:reversed_order; 22, False; 23, dictionary_splat_pattern; 23, 24; 24, identifier:kwargs; 25, block; 25, 26; 25, 28; 25, 37; 25, 46; 25, 61; 25, 68; 25, 77; 25, 86; 25, 92; 25, 115; 25, 136; 25, 140; 25, 144; 25, 256; 25, 257; 25, 261; 25, 275; 26, expression_statement; 26, 27; 27, comment; 28, expression_statement; 28, 29; 29, assignment; 29, 30; 29, 31; 30, identifier:limit; 31, call; 31, 32; 31, 33; 32, identifier:getattr; 33, argument_list; 33, 34; 33, 35; 33, 36; 34, identifier:settings; 35, string:'API_RESULT_LIMIT'; 36, integer:1000; 37, expression_statement; 37, 38; 38, assignment; 38, 39; 38, 40; 39, identifier:page_size; 40, call; 40, 41; 40, 44; 41, attribute; 41, 42; 41, 43; 42, identifier:utils; 43, identifier:get_page_size; 44, argument_list; 44, 45; 45, identifier:request; 46, if_statement; 46, 47; 46, 48; 46, 55; 47, identifier:paginate; 48, block; 48, 49; 49, expression_statement; 49, 50; 50, assignment; 50, 51; 50, 52; 51, identifier:request_size; 52, binary_operator:+; 52, 53; 52, 54; 53, identifier:page_size; 54, integer:1; 55, else_clause; 55, 56; 56, block; 56, 57; 57, expression_statement; 57, 58; 58, assignment; 58, 59; 58, 60; 59, identifier:request_size; 60, identifier:limit; 61, expression_statement; 61, 62; 62, call; 62, 63; 62, 64; 63, identifier:_normalize_list_input; 64, argument_list; 64, 65; 64, 66; 65, identifier:filters; 66, dictionary_splat; 66, 67; 67, identifier:kwargs; 68, expression_statement; 68, 69; 69, assignment; 69, 70; 69, 71; 70, identifier:kwargs; 71, dictionary; 71, 72; 72, pair; 72, 73; 72, 74; 73, string:'filters'; 74, boolean_operator:or; 74, 75; 74, 76; 75, identifier:filters; 76, dictionary; 77, if_statement; 77, 78; 77, 79; 78, identifier:marker; 79, block; 79, 80; 80, expression_statement; 80, 81; 81, assignment; 81, 82; 81, 85; 82, subscript; 82, 83; 82, 84; 83, identifier:kwargs; 84, string:'marker'; 85, identifier:marker; 86, expression_statement; 86, 87; 87, assignment; 87, 88; 87, 91; 88, subscript; 88, 89; 88, 90; 89, identifier:kwargs; 90, string:'sort_key'; 91, identifier:sort_key; 92, if_statement; 92, 93; 92, 95; 92, 102; 93, not_operator; 93, 94; 94, identifier:reversed_order; 95, block; 95, 96; 96, expression_statement; 96, 97; 97, assignment; 97, 98; 97, 101; 98, subscript; 98, 99; 98, 100; 99, identifier:kwargs; 100, string:'sort_dir'; 101, identifier:sort_dir; 102, else_clause; 102, 103; 103, block; 103, 104; 104, expression_statement; 104, 105; 105, assignment; 105, 106; 105, 109; 106, subscript; 106, 107; 106, 108; 107, identifier:kwargs; 108, string:'sort_dir'; 109, conditional_expression:if; 109, 110; 109, 111; 109, 114; 110, string:'desc'; 111, comparison_operator:==; 111, 112; 111, 113; 112, identifier:sort_dir; 113, string:'asc'; 114, string:'asc'; 115, expression_statement; 115, 116; 116, assignment; 116, 117; 116, 118; 117, identifier:images_iter; 118, call; 118, 119; 118, 127; 119, attribute; 119, 120; 119, 126; 120, attribute; 120, 121; 120, 125; 121, call; 121, 122; 121, 123; 122, identifier:glanceclient; 123, argument_list; 123, 124; 124, identifier:request; 125, identifier:images; 126, identifier:list; 127, argument_list; 127, 128; 127, 131; 127, 134; 128, keyword_argument; 128, 129; 128, 130; 129, identifier:page_size; 130, identifier:request_size; 131, keyword_argument; 131, 132; 131, 133; 132, identifier:limit; 133, identifier:limit; 134, dictionary_splat; 134, 135; 135, identifier:kwargs; 136, expression_statement; 136, 137; 137, assignment; 137, 138; 137, 139; 138, identifier:has_prev_data; 139, False; 140, expression_statement; 140, 141; 141, assignment; 141, 142; 141, 143; 142, identifier:has_more_data; 143, False; 144, if_statement; 144, 145; 144, 146; 144, 247; 145, identifier:paginate; 146, block; 146, 147; 146, 160; 146, 161; 146, 213; 146, 214; 147, expression_statement; 147, 148; 148, assignment; 148, 149; 148, 150; 149, identifier:images; 150, call; 150, 151; 150, 152; 151, identifier:list; 152, argument_list; 152, 153; 153, call; 153, 154; 153, 157; 154, attribute; 154, 155; 154, 156; 155, identifier:itertools; 156, identifier:islice; 157, argument_list; 157, 158; 157, 159; 158, identifier:images_iter; 159, identifier:request_size; 160, comment; 161, if_statement; 161, 162; 161, 168; 161, 191; 161, 192; 161, 203; 161, 204; 162, comparison_operator:>; 162, 163; 162, 167; 163, call; 163, 164; 163, 165; 164, identifier:len; 165, argument_list; 165, 166; 166, identifier:images; 167, identifier:page_size; 168, block; 168, 169; 168, 177; 168, 181; 168, 182; 169, expression_statement; 169, 170; 170, call; 170, 171; 170, 174; 171, attribute; 171, 172; 171, 173; 172, identifier:images; 173, identifier:pop; 174, argument_list; 174, 175; 175, unary_operator:-; 175, 176; 176, integer:1; 177, expression_statement; 177, 178; 178, assignment; 178, 179; 178, 180; 179, identifier:has_more_data; 180, True; 181, comment; 182, if_statement; 182, 183; 182, 186; 183, comparison_operator:is; 183, 184; 183, 185; 184, identifier:marker; 185, None; 186, block; 186, 187; 187, expression_statement; 187, 188; 188, assignment; 188, 189; 188, 190; 189, identifier:has_prev_data; 190, True; 191, comment; 192, elif_clause; 192, 193; 192, 198; 193, boolean_operator:and; 193, 194; 193, 195; 194, identifier:reversed_order; 195, comparison_operator:is; 195, 196; 195, 197; 196, identifier:marker; 197, None; 198, block; 198, 199; 199, expression_statement; 199, 200; 200, assignment; 200, 201; 200, 202; 201, identifier:has_more_data; 202, True; 203, comment; 204, elif_clause; 204, 205; 204, 208; 205, comparison_operator:is; 205, 206; 205, 207; 206, identifier:marker; 207, None; 208, block; 208, 209; 209, expression_statement; 209, 210; 210, assignment; 210, 211; 210, 212; 211, identifier:has_prev_data; 212, True; 213, comment; 214, if_statement; 214, 215; 214, 216; 215, identifier:reversed_order; 216, block; 216, 217; 217, expression_statement; 217, 218; 218, assignment; 218, 219; 218, 220; 219, identifier:images; 220, call; 220, 221; 220, 222; 221, identifier:sorted; 222, argument_list; 222, 223; 222, 224; 222, 241; 223, identifier:images; 224, keyword_argument; 224, 225; 224, 226; 225, identifier:key; 226, lambda; 226, 227; 226, 229; 227, lambda_parameters; 227, 228; 228, identifier:image; 229, call; 229, 230; 229, 240; 230, attribute; 230, 231; 230, 239; 231, parenthesized_expression; 231, 232; 232, boolean_operator:or; 232, 233; 232, 238; 233, call; 233, 234; 233, 235; 234, identifier:getattr; 235, argument_list; 235, 236; 235, 237; 236, identifier:image; 237, identifier:sort_key; 238, string:''; 239, identifier:lower; 240, argument_list; 241, keyword_argument; 241, 242; 241, 243; 242, identifier:reverse; 243, parenthesized_expression; 243, 244; 244, comparison_operator:==; 244, 245; 244, 246; 245, identifier:sort_dir; 246, string:'desc'; 247, else_clause; 247, 248; 248, block; 248, 249; 249, expression_statement; 249, 250; 250, assignment; 250, 251; 250, 252; 251, identifier:images; 252, call; 252, 253; 252, 254; 253, identifier:list; 254, argument_list; 254, 255; 255, identifier:images_iter; 256, comment; 257, expression_statement; 257, 258; 258, assignment; 258, 259; 258, 260; 259, identifier:wrapped_images; 260, list:[]; 261, for_statement; 261, 262; 261, 263; 261, 264; 262, identifier:image; 263, identifier:images; 264, block; 264, 265; 265, expression_statement; 265, 266; 266, call; 266, 267; 266, 270; 267, attribute; 267, 268; 267, 269; 268, identifier:wrapped_images; 269, identifier:append; 270, argument_list; 270, 271; 271, call; 271, 272; 271, 273; 272, identifier:Image; 273, argument_list; 273, 274; 274, identifier:image; 275, return_statement; 275, 276; 276, expression_list; 276, 277; 276, 278; 276, 279; 277, identifier:wrapped_images; 278, identifier:has_more_data; 279, identifier:has_prev_data
def image_list_detailed(request, marker=None, sort_dir='desc', sort_key='created_at', filters=None, paginate=False, reversed_order=False, **kwargs): """Thin layer above glanceclient, for handling pagination issues. It provides iterating both forward and backward on top of ascetic OpenStack pagination API - which natively supports only iterating forward through the entries. Thus in order to retrieve list of objects at previous page, a request with the reverse entries order had to be made to Glance, using the first object id on current page as the marker - restoring the original items ordering before sending them back to the UI. :param request: The request object coming from browser to be passed further into Glance service. :param marker: The id of an object which defines a starting point of a query sent to Glance service. :param sort_dir: The direction by which the resulting image list throughout all pages (if pagination is enabled) will be sorted. Could be either 'asc' (ascending) or 'desc' (descending), defaults to 'desc'. :param sort_key: The name of key by by which the resulting image list throughout all pages (if pagination is enabled) will be sorted. Defaults to 'created_at'. :param filters: A dictionary of filters passed as is to Glance service. :param paginate: Whether the pagination is enabled. If it is, then the number of entries on a single page of images table is limited to the specific number stored in browser cookies. :param reversed_order: Set this flag to True when it's necessary to get a reversed list of images from Glance (used for navigating the images list back in UI). """ limit = getattr(settings, 'API_RESULT_LIMIT', 1000) page_size = utils.get_page_size(request) if paginate: request_size = page_size + 1 else: request_size = limit _normalize_list_input(filters, **kwargs) kwargs = {'filters': filters or {}} if marker: kwargs['marker'] = marker kwargs['sort_key'] = sort_key if not reversed_order: kwargs['sort_dir'] = sort_dir else: kwargs['sort_dir'] = 'desc' if sort_dir == 'asc' else 'asc' images_iter = glanceclient(request).images.list(page_size=request_size, limit=limit, **kwargs) has_prev_data = False has_more_data = False if paginate: images = list(itertools.islice(images_iter, request_size)) # first and middle page condition if len(images) > page_size: images.pop(-1) has_more_data = True # middle page condition if marker is not None: has_prev_data = True # first page condition when reached via prev back elif reversed_order and marker is not None: has_more_data = True # last page condition elif marker is not None: has_prev_data = True # restore the original ordering here if reversed_order: images = sorted(images, key=lambda image: (getattr(image, sort_key) or '').lower(), reverse=(sort_dir == 'desc')) else: images = list(images_iter) # TODO(jpichon): Do it better wrapped_images = [] for image in images: wrapped_images.append(Image(image)) return wrapped_images, has_more_data, has_prev_data
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 20; 2, function_name:metadefs_namespace_list; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 14; 3, 17; 4, identifier:request; 5, default_parameter; 5, 6; 5, 7; 6, identifier:filters; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:sort_dir; 10, string:'asc'; 11, default_parameter; 11, 12; 11, 13; 12, identifier:sort_key; 13, string:'namespace'; 14, default_parameter; 14, 15; 14, 16; 15, identifier:marker; 16, None; 17, default_parameter; 17, 18; 17, 19; 18, identifier:paginate; 19, False; 20, block; 20, 21; 20, 23; 20, 24; 20, 25; 20, 37; 20, 46; 20, 55; 20, 64; 20, 79; 20, 86; 20, 95; 20, 101; 20, 107; 20, 129; 20, 130; 20, 131; 20, 140; 20, 149; 20, 163; 20, 167; 20, 171; 20, 251; 20, 262; 21, expression_statement; 21, 22; 22, comment; 23, comment; 24, comment; 25, if_statement; 25, 26; 25, 31; 26, comparison_operator:<; 26, 27; 26, 30; 27, call; 27, 28; 27, 29; 28, identifier:get_version; 29, argument_list; 30, integer:2; 31, block; 31, 32; 32, return_statement; 32, 33; 33, expression_list; 33, 34; 33, 35; 33, 36; 34, list:[]; 35, False; 36, False; 37, if_statement; 37, 38; 37, 41; 38, comparison_operator:is; 38, 39; 38, 40; 39, identifier:filters; 40, None; 41, block; 41, 42; 42, expression_statement; 42, 43; 43, assignment; 43, 44; 43, 45; 44, identifier:filters; 45, dictionary; 46, expression_statement; 46, 47; 47, assignment; 47, 48; 47, 49; 48, identifier:limit; 49, call; 49, 50; 49, 51; 50, identifier:getattr; 51, argument_list; 51, 52; 51, 53; 51, 54; 52, identifier:settings; 53, string:'API_RESULT_LIMIT'; 54, integer:1000; 55, expression_statement; 55, 56; 56, assignment; 56, 57; 56, 58; 57, identifier:page_size; 58, call; 58, 59; 58, 62; 59, attribute; 59, 60; 59, 61; 60, identifier:utils; 61, identifier:get_page_size; 62, argument_list; 62, 63; 63, identifier:request; 64, if_statement; 64, 65; 64, 66; 64, 73; 65, identifier:paginate; 66, block; 66, 67; 67, expression_statement; 67, 68; 68, assignment; 68, 69; 68, 70; 69, identifier:request_size; 70, binary_operator:+; 70, 71; 70, 72; 71, identifier:page_size; 72, integer:1; 73, else_clause; 73, 74; 74, block; 74, 75; 75, expression_statement; 75, 76; 76, assignment; 76, 77; 76, 78; 77, identifier:request_size; 78, identifier:limit; 79, expression_statement; 79, 80; 80, assignment; 80, 81; 80, 82; 81, identifier:kwargs; 82, dictionary; 82, 83; 83, pair; 83, 84; 83, 85; 84, string:'filters'; 85, identifier:filters; 86, if_statement; 86, 87; 86, 88; 87, identifier:marker; 88, block; 88, 89; 89, expression_statement; 89, 90; 90, assignment; 90, 91; 90, 94; 91, subscript; 91, 92; 91, 93; 92, identifier:kwargs; 93, string:'marker'; 94, identifier:marker; 95, expression_statement; 95, 96; 96, assignment; 96, 97; 96, 100; 97, subscript; 97, 98; 97, 99; 98, identifier:kwargs; 99, string:'sort_dir'; 100, identifier:sort_dir; 101, expression_statement; 101, 102; 102, assignment; 102, 103; 102, 106; 103, subscript; 103, 104; 103, 105; 104, identifier:kwargs; 105, string:'sort_key'; 106, identifier:sort_key; 107, expression_statement; 107, 108; 108, assignment; 108, 109; 108, 110; 109, identifier:namespaces_iter; 110, call; 110, 111; 110, 120; 111, attribute; 111, 112; 111, 119; 112, attribute; 112, 113; 112, 118; 113, call; 113, 114; 113, 115; 114, identifier:glanceclient; 115, argument_list; 115, 116; 115, 117; 116, identifier:request; 117, string:'2'; 118, identifier:metadefs_namespace; 119, identifier:list; 120, argument_list; 120, 121; 120, 124; 120, 127; 121, keyword_argument; 121, 122; 121, 123; 122, identifier:page_size; 123, identifier:request_size; 124, keyword_argument; 124, 125; 124, 126; 125, identifier:limit; 126, identifier:limit; 127, dictionary_splat; 127, 128; 128, identifier:kwargs; 129, comment; 130, comment; 131, expression_statement; 131, 132; 132, assignment; 132, 133; 132, 134; 133, identifier:resource_types; 134, call; 134, 135; 134, 138; 135, attribute; 135, 136; 135, 137; 136, identifier:filters; 137, identifier:get; 138, argument_list; 138, 139; 139, string:'resource_types'; 140, expression_statement; 140, 141; 141, assignment; 141, 142; 141, 143; 142, identifier:properties_target; 143, call; 143, 144; 143, 147; 144, attribute; 144, 145; 144, 146; 145, identifier:filters; 146, identifier:get; 147, argument_list; 147, 148; 148, string:'properties_target'; 149, if_statement; 149, 150; 149, 153; 150, boolean_operator:and; 150, 151; 150, 152; 151, identifier:resource_types; 152, identifier:properties_target; 153, block; 153, 154; 154, expression_statement; 154, 155; 155, assignment; 155, 156; 155, 157; 156, identifier:namespaces_iter; 157, call; 157, 158; 157, 159; 158, identifier:filter_properties_target; 159, argument_list; 159, 160; 159, 161; 159, 162; 160, identifier:namespaces_iter; 161, identifier:resource_types; 162, identifier:properties_target; 163, expression_statement; 163, 164; 164, assignment; 164, 165; 164, 166; 165, identifier:has_prev_data; 166, False; 167, expression_statement; 167, 168; 168, assignment; 168, 169; 168, 170; 169, identifier:has_more_data; 170, False; 171, if_statement; 171, 172; 171, 173; 171, 242; 172, identifier:paginate; 173, block; 173, 174; 173, 187; 173, 188; 174, expression_statement; 174, 175; 175, assignment; 175, 176; 175, 177; 176, identifier:namespaces; 177, call; 177, 178; 177, 179; 178, identifier:list; 179, argument_list; 179, 180; 180, call; 180, 181; 180, 184; 181, attribute; 181, 182; 181, 183; 182, identifier:itertools; 183, identifier:islice; 184, argument_list; 184, 185; 184, 186; 185, identifier:namespaces_iter; 186, identifier:request_size; 187, comment; 188, if_statement; 188, 189; 188, 195; 188, 218; 188, 219; 188, 232; 188, 233; 189, comparison_operator:>; 189, 190; 189, 194; 190, call; 190, 191; 190, 192; 191, identifier:len; 192, argument_list; 192, 193; 193, identifier:namespaces; 194, identifier:page_size; 195, block; 195, 196; 195, 204; 195, 208; 195, 209; 196, expression_statement; 196, 197; 197, call; 197, 198; 197, 201; 198, attribute; 198, 199; 198, 200; 199, identifier:namespaces; 200, identifier:pop; 201, argument_list; 201, 202; 202, unary_operator:-; 202, 203; 203, integer:1; 204, expression_statement; 204, 205; 205, assignment; 205, 206; 205, 207; 206, identifier:has_more_data; 207, True; 208, comment; 209, if_statement; 209, 210; 209, 213; 210, comparison_operator:is; 210, 211; 210, 212; 211, identifier:marker; 212, None; 213, block; 213, 214; 214, expression_statement; 214, 215; 215, assignment; 215, 216; 215, 217; 216, identifier:has_prev_data; 217, True; 218, comment; 219, elif_clause; 219, 220; 219, 227; 220, boolean_operator:and; 220, 221; 220, 224; 221, comparison_operator:==; 221, 222; 221, 223; 222, identifier:sort_dir; 223, string:'desc'; 224, comparison_operator:is; 224, 225; 224, 226; 225, identifier:marker; 226, None; 227, block; 227, 228; 228, expression_statement; 228, 229; 229, assignment; 229, 230; 229, 231; 230, identifier:has_more_data; 231, True; 232, comment; 233, elif_clause; 233, 234; 233, 237; 234, comparison_operator:is; 234, 235; 234, 236; 235, identifier:marker; 236, None; 237, block; 237, 238; 238, expression_statement; 238, 239; 239, assignment; 239, 240; 239, 241; 240, identifier:has_prev_data; 241, True; 242, else_clause; 242, 243; 243, block; 243, 244; 244, expression_statement; 244, 245; 245, assignment; 245, 246; 245, 247; 246, identifier:namespaces; 247, call; 247, 248; 247, 249; 248, identifier:list; 249, argument_list; 249, 250; 250, identifier:namespaces_iter; 251, expression_statement; 251, 252; 252, assignment; 252, 253; 252, 254; 253, identifier:namespaces; 254, list_comprehension; 254, 255; 254, 259; 255, call; 255, 256; 255, 257; 256, identifier:Namespace; 257, argument_list; 257, 258; 258, identifier:namespace; 259, for_in_clause; 259, 260; 259, 261; 260, identifier:namespace; 261, identifier:namespaces; 262, return_statement; 262, 263; 263, expression_list; 263, 264; 263, 265; 263, 266; 264, identifier:namespaces; 265, identifier:has_more_data; 266, identifier:has_prev_data
def metadefs_namespace_list(request, filters=None, sort_dir='asc', sort_key='namespace', marker=None, paginate=False): """Retrieve a listing of Namespaces :param paginate: If true will perform pagination based on settings. :param marker: Specifies the namespace of the last-seen namespace. The typical pattern of limit and marker is to make an initial limited request and then to use the last namespace from the response as the marker parameter in a subsequent limited request. With paginate, limit is automatically set. :param sort_dir: The sort direction ('asc' or 'desc'). :param sort_key: The field to sort on (for example, 'created_at'). Default is namespace. The way base namespaces are loaded into glance typically at first deployment is done in a single transaction giving them a potentially unpredictable sort result when using create_at. :param filters: specifies addition fields to filter on such as resource_types. :returns A tuple of three values: 1) Current page results 2) A boolean of whether or not there are previous page(s). 3) A boolean of whether or not there are more page(s). """ # Listing namespaces requires the v2 API. If not supported we return an # empty array so callers don't need to worry about version checking. if get_version() < 2: return [], False, False if filters is None: filters = {} limit = getattr(settings, 'API_RESULT_LIMIT', 1000) page_size = utils.get_page_size(request) if paginate: request_size = page_size + 1 else: request_size = limit kwargs = {'filters': filters} if marker: kwargs['marker'] = marker kwargs['sort_dir'] = sort_dir kwargs['sort_key'] = sort_key namespaces_iter = glanceclient(request, '2').metadefs_namespace.list( page_size=request_size, limit=limit, **kwargs) # Filter the namespaces based on the provided properties_target since this # is not supported by the metadata namespaces API. resource_types = filters.get('resource_types') properties_target = filters.get('properties_target') if resource_types and properties_target: namespaces_iter = filter_properties_target(namespaces_iter, resource_types, properties_target) has_prev_data = False has_more_data = False if paginate: namespaces = list(itertools.islice(namespaces_iter, request_size)) # first and middle page condition if len(namespaces) > page_size: namespaces.pop(-1) has_more_data = True # middle page condition if marker is not None: has_prev_data = True # first page condition when reached via prev back elif sort_dir == 'desc' and marker is not None: has_more_data = True # last page condition elif marker is not None: has_prev_data = True else: namespaces = list(namespaces_iter) namespaces = [Namespace(namespace) for namespace in namespaces] return namespaces, has_more_data, has_prev_data
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:sort_js_files; 3, parameters; 3, 4; 4, identifier:js_files; 5, block; 5, 6; 5, 8; 5, 23; 5, 38; 5, 53; 5, 86; 5, 92; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:modules; 11, list_comprehension; 11, 12; 11, 13; 11, 16; 12, identifier:f; 13, for_in_clause; 13, 14; 13, 15; 14, identifier:f; 15, identifier:js_files; 16, if_clause; 16, 17; 17, call; 17, 18; 17, 21; 18, attribute; 18, 19; 18, 20; 19, identifier:f; 20, identifier:endswith; 21, argument_list; 21, 22; 22, identifier:MODULE_EXT; 23, expression_statement; 23, 24; 24, assignment; 24, 25; 24, 26; 25, identifier:mocks; 26, list_comprehension; 26, 27; 26, 28; 26, 31; 27, identifier:f; 28, for_in_clause; 28, 29; 28, 30; 29, identifier:f; 30, identifier:js_files; 31, if_clause; 31, 32; 32, call; 32, 33; 32, 36; 33, attribute; 33, 34; 33, 35; 34, identifier:f; 35, identifier:endswith; 36, argument_list; 36, 37; 37, identifier:MOCK_EXT; 38, expression_statement; 38, 39; 39, assignment; 39, 40; 39, 41; 40, identifier:specs; 41, list_comprehension; 41, 42; 41, 43; 41, 46; 42, identifier:f; 43, for_in_clause; 43, 44; 43, 45; 44, identifier:f; 45, identifier:js_files; 46, if_clause; 46, 47; 47, call; 47, 48; 47, 51; 48, attribute; 48, 49; 48, 50; 49, identifier:f; 50, identifier:endswith; 51, argument_list; 51, 52; 52, identifier:SPEC_EXT; 53, expression_statement; 53, 54; 54, assignment; 54, 55; 54, 56; 55, identifier:other_sources; 56, list_comprehension; 56, 57; 56, 58; 56, 61; 57, identifier:f; 58, for_in_clause; 58, 59; 58, 60; 59, identifier:f; 60, identifier:js_files; 61, if_clause; 61, 62; 62, parenthesized_expression; 62, 63; 63, boolean_operator:and; 63, 64; 63, 79; 64, boolean_operator:and; 64, 65; 64, 72; 65, not_operator; 65, 66; 66, call; 66, 67; 66, 70; 67, attribute; 67, 68; 67, 69; 68, identifier:f; 69, identifier:endswith; 70, argument_list; 70, 71; 71, identifier:MODULE_EXT; 72, not_operator; 72, 73; 73, call; 73, 74; 73, 77; 74, attribute; 74, 75; 74, 76; 75, identifier:f; 76, identifier:endswith; 77, argument_list; 77, 78; 78, identifier:MOCK_EXT; 79, not_operator; 79, 80; 80, call; 80, 81; 80, 84; 81, attribute; 81, 82; 81, 83; 82, identifier:f; 83, identifier:endswith; 84, argument_list; 84, 85; 85, identifier:SPEC_EXT; 86, expression_statement; 86, 87; 87, assignment; 87, 88; 87, 89; 88, identifier:sources; 89, binary_operator:+; 89, 90; 89, 91; 90, identifier:modules; 91, identifier:other_sources; 92, return_statement; 92, 93; 93, expression_list; 93, 94; 93, 95; 93, 96; 94, identifier:sources; 95, identifier:mocks; 96, identifier:specs
def sort_js_files(js_files): """Sorts JavaScript files in `js_files`. It sorts JavaScript files in a given `js_files` into source files, mock files and spec files based on file extension. Output: * sources: source files for production. The order of source files is significant and should be listed in the below order: - First, all the that defines the other application's angular module. Those files have extension of `.module.js`. The order among them is not significant. - Followed by all other source code files. The order among them is not significant. * mocks: mock files provide mock data/services for tests. They have extension of `.mock.js`. The order among them is not significant. * specs: spec files for testing. They have extension of `.spec.js`. The order among them is not significant. """ modules = [f for f in js_files if f.endswith(MODULE_EXT)] mocks = [f for f in js_files if f.endswith(MOCK_EXT)] specs = [f for f in js_files if f.endswith(SPEC_EXT)] other_sources = [f for f in js_files if (not f.endswith(MODULE_EXT) and not f.endswith(MOCK_EXT) and not f.endswith(SPEC_EXT))] sources = modules + other_sources return sources, mocks, specs
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 10; 2, function_name:_slice_mostly_sorted; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 4, identifier:array; 5, identifier:keep; 6, identifier:rest; 7, default_parameter; 7, 8; 7, 9; 8, identifier:ind; 9, None; 10, block; 10, 11; 10, 13; 10, 30; 10, 48; 10, 52; 10, 73; 10, 83; 10, 100; 10, 135; 10, 165; 10, 182; 11, expression_statement; 11, 12; 12, comment; 13, if_statement; 13, 14; 13, 17; 14, comparison_operator:is; 14, 15; 14, 16; 15, identifier:ind; 16, None; 17, block; 17, 18; 18, expression_statement; 18, 19; 19, assignment; 19, 20; 19, 21; 20, identifier:ind; 21, call; 21, 22; 21, 25; 22, attribute; 22, 23; 22, 24; 23, identifier:np; 24, identifier:arange; 25, argument_list; 25, 26; 26, call; 26, 27; 26, 28; 27, identifier:len; 28, argument_list; 28, 29; 29, identifier:array; 30, expression_statement; 30, 31; 31, assignment; 31, 32; 31, 33; 32, identifier:idx; 33, call; 33, 34; 33, 37; 34, attribute; 34, 35; 34, 36; 35, identifier:np; 36, identifier:argsort; 37, argument_list; 37, 38; 38, call; 38, 39; 38, 42; 39, attribute; 39, 40; 39, 41; 40, identifier:np; 41, identifier:concatenate; 42, argument_list; 42, 43; 43, list:[keep, ind[rest]]; 43, 44; 43, 45; 44, identifier:keep; 45, subscript; 45, 46; 45, 47; 46, identifier:ind; 47, identifier:rest; 48, expression_statement; 48, 49; 49, assignment; 49, 50; 49, 51; 50, identifier:slices; 51, list:[]; 52, if_statement; 52, 53; 52, 58; 52, 59; 53, comparison_operator:>; 53, 54; 53, 57; 54, subscript; 54, 55; 54, 56; 55, identifier:keep; 56, integer:0; 57, integer:0; 58, comment; 59, block; 59, 60; 60, expression_statement; 60, 61; 61, call; 61, 62; 61, 65; 62, attribute; 62, 63; 62, 64; 63, identifier:slices; 64, identifier:append; 65, argument_list; 65, 66; 66, call; 66, 67; 66, 68; 67, identifier:slice; 68, argument_list; 68, 69; 68, 70; 69, None; 70, subscript; 70, 71; 70, 72; 71, identifier:keep; 72, integer:0; 73, expression_statement; 73, 74; 74, call; 74, 75; 74, 78; 75, attribute; 75, 76; 75, 77; 76, identifier:slices; 77, identifier:append; 78, argument_list; 78, 79; 79, list:[keep[0]]; 79, 80; 80, subscript; 80, 81; 80, 82; 81, identifier:keep; 82, integer:0; 83, expression_statement; 83, 84; 84, assignment; 84, 85; 84, 86; 85, identifier:windows; 86, call; 86, 87; 86, 88; 87, identifier:zip; 88, argument_list; 88, 89; 88, 95; 89, subscript; 89, 90; 89, 91; 90, identifier:keep; 91, slice; 91, 92; 91, 93; 92, colon; 93, unary_operator:-; 93, 94; 94, integer:1; 95, subscript; 95, 96; 95, 97; 96, identifier:keep; 97, slice; 97, 98; 97, 99; 98, integer:1; 99, colon; 100, for_statement; 100, 101; 100, 104; 100, 105; 101, pattern_list; 101, 102; 101, 103; 102, identifier:l; 103, identifier:r; 104, identifier:windows; 105, block; 105, 106; 105, 127; 106, if_statement; 106, 107; 106, 112; 106, 113; 107, comparison_operator:>; 107, 108; 107, 109; 108, identifier:r; 109, binary_operator:+; 109, 110; 109, 111; 110, identifier:l; 111, integer:1; 112, comment; 113, block; 113, 114; 114, expression_statement; 114, 115; 115, call; 115, 116; 115, 119; 116, attribute; 116, 117; 116, 118; 117, identifier:slices; 118, identifier:append; 119, argument_list; 119, 120; 120, call; 120, 121; 120, 122; 121, identifier:slice; 122, argument_list; 122, 123; 122, 126; 123, binary_operator:+; 123, 124; 123, 125; 124, identifier:l; 125, integer:1; 126, identifier:r; 127, expression_statement; 127, 128; 128, call; 128, 129; 128, 132; 129, attribute; 129, 130; 129, 131; 130, identifier:slices; 131, identifier:append; 132, argument_list; 132, 133; 133, list:[r]; 133, 134; 134, identifier:r; 135, if_statement; 135, 136; 135, 147; 135, 148; 136, comparison_operator:<; 136, 137; 136, 141; 137, subscript; 137, 138; 137, 139; 138, identifier:keep; 139, unary_operator:-; 139, 140; 140, integer:1; 141, binary_operator:-; 141, 142; 141, 146; 142, call; 142, 143; 142, 144; 143, identifier:len; 144, argument_list; 144, 145; 145, identifier:array; 146, integer:1; 147, comment; 148, block; 148, 149; 149, expression_statement; 149, 150; 150, call; 150, 151; 150, 154; 151, attribute; 151, 152; 151, 153; 152, identifier:slices; 153, identifier:append; 154, argument_list; 154, 155; 155, call; 155, 156; 155, 157; 156, identifier:slice; 157, argument_list; 157, 158; 157, 164; 158, binary_operator:+; 158, 159; 158, 163; 159, subscript; 159, 160; 159, 161; 160, identifier:keep; 161, unary_operator:-; 161, 162; 162, integer:1; 163, integer:1; 164, None; 165, expression_statement; 165, 166; 166, assignment; 166, 167; 166, 168; 167, identifier:result; 168, call; 168, 169; 168, 172; 169, attribute; 169, 170; 169, 171; 170, identifier:da; 171, identifier:concatenate; 172, argument_list; 172, 173; 173, list_comprehension; 173, 174; 173, 179; 174, subscript; 174, 175; 174, 176; 175, identifier:array; 176, subscript; 176, 177; 176, 178; 177, identifier:idx; 178, identifier:slice_; 179, for_in_clause; 179, 180; 179, 181; 180, identifier:slice_; 181, identifier:slices; 182, return_statement; 182, 183; 183, identifier:result
def _slice_mostly_sorted(array, keep, rest, ind=None): """Slice dask array `array` that is almost entirely sorted already. We perform approximately `2 * len(keep)` slices on `array`. This is OK, since `keep` is small. Individually, each of these slices is entirely sorted. Parameters ---------- array : dask.array.Array keep : ndarray[Int] This must be sorted. rest : ndarray[Bool] ind : ndarray[Int], optional Returns ------- sliced : dask.array.Array """ if ind is None: ind = np.arange(len(array)) idx = np.argsort(np.concatenate([keep, ind[rest]])) slices = [] if keep[0] > 0: # avoid creating empty slices slices.append(slice(None, keep[0])) slices.append([keep[0]]) windows = zip(keep[:-1], keep[1:]) for l, r in windows: if r > l + 1: # avoid creating empty slices slices.append(slice(l + 1, r)) slices.append([r]) if keep[-1] < len(array) - 1: # avoid creating empty slices slices.append(slice(keep[-1] + 1, None)) result = da.concatenate([array[idx[slice_]] for slice_ in slices]) return result
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 12; 1, 24; 2, function_name:sort_servers_closest; 3, parameters; 3, 4; 4, typed_parameter; 4, 5; 4, 6; 5, identifier:servers; 6, type; 6, 7; 7, generic_type; 7, 8; 7, 9; 8, identifier:Sequence; 9, type_parameter; 9, 10; 10, type; 10, 11; 11, identifier:str; 12, type; 12, 13; 13, generic_type; 13, 14; 13, 15; 14, identifier:Sequence; 15, type_parameter; 15, 16; 16, type; 16, 17; 17, generic_type; 17, 18; 17, 19; 18, identifier:Tuple; 19, type_parameter; 19, 20; 19, 22; 20, type; 20, 21; 21, identifier:str; 22, type; 22, 23; 23, identifier:float; 24, block; 24, 25; 24, 27; 24, 52; 24, 76; 24, 77; 24, 87; 24, 88; 24, 127; 24, 137; 25, expression_statement; 25, 26; 26, comment; 27, if_statement; 27, 28; 27, 46; 28, not_operator; 28, 29; 29, call; 29, 30; 29, 42; 30, attribute; 30, 31; 30, 41; 31, set_comprehension; 31, 32; 31, 38; 32, attribute; 32, 33; 32, 37; 33, call; 33, 34; 33, 35; 34, identifier:urlparse; 35, argument_list; 35, 36; 36, identifier:url; 37, identifier:scheme; 38, for_in_clause; 38, 39; 38, 40; 39, identifier:url; 40, identifier:servers; 41, identifier:issubset; 42, argument_list; 42, 43; 43, set; 43, 44; 43, 45; 44, string:'http'; 45, string:'https'; 46, block; 46, 47; 47, raise_statement; 47, 48; 48, call; 48, 49; 48, 50; 49, identifier:TransportError; 50, argument_list; 50, 51; 51, string:'Invalid server urls'; 52, expression_statement; 52, 53; 53, assignment; 53, 54; 53, 55; 54, identifier:get_rtt_jobs; 55, call; 55, 56; 55, 57; 56, identifier:set; 57, generator_expression; 57, 58; 57, 73; 58, call; 58, 59; 58, 62; 59, attribute; 59, 60; 59, 61; 60, identifier:gevent; 61, identifier:spawn; 62, argument_list; 62, 63; 62, 72; 63, lambda; 63, 64; 63, 66; 64, lambda_parameters; 64, 65; 65, identifier:url; 66, tuple; 66, 67; 66, 68; 67, identifier:url; 68, call; 68, 69; 68, 70; 69, identifier:get_http_rtt; 70, argument_list; 70, 71; 71, identifier:url; 72, identifier:server_url; 73, for_in_clause; 73, 74; 73, 75; 74, identifier:server_url; 75, identifier:servers; 76, comment; 77, expression_statement; 77, 78; 78, call; 78, 79; 78, 82; 79, attribute; 79, 80; 79, 81; 80, identifier:gevent; 81, identifier:joinall; 82, argument_list; 82, 83; 82, 84; 83, identifier:get_rtt_jobs; 84, keyword_argument; 84, 85; 84, 86; 85, identifier:raise_error; 86, False; 87, comment; 88, expression_statement; 88, 89; 89, assignment; 89, 90; 89, 91; 89, 103; 90, identifier:sorted_servers; 91, type; 91, 92; 92, generic_type; 92, 93; 92, 94; 93, identifier:List; 94, type_parameter; 94, 95; 95, type; 95, 96; 96, generic_type; 96, 97; 96, 98; 97, identifier:Tuple; 98, type_parameter; 98, 99; 98, 101; 99, type; 99, 100; 100, identifier:str; 101, type; 101, 102; 102, identifier:float; 103, call; 103, 104; 103, 105; 104, identifier:sorted; 105, argument_list; 105, 106; 105, 121; 106, generator_expression; 106, 107; 106, 110; 106, 113; 107, attribute; 107, 108; 107, 109; 108, identifier:job; 109, identifier:value; 110, for_in_clause; 110, 111; 110, 112; 111, identifier:job; 112, identifier:get_rtt_jobs; 113, if_clause; 113, 114; 114, comparison_operator:is; 114, 115; 114, 120; 115, subscript; 115, 116; 115, 119; 116, attribute; 116, 117; 116, 118; 117, identifier:job; 118, identifier:value; 119, integer:1; 120, None; 121, keyword_argument; 121, 122; 121, 123; 122, identifier:key; 123, call; 123, 124; 123, 125; 124, identifier:itemgetter; 125, argument_list; 125, 126; 126, integer:1; 127, expression_statement; 127, 128; 128, call; 128, 129; 128, 132; 129, attribute; 129, 130; 129, 131; 130, identifier:log; 131, identifier:debug; 132, argument_list; 132, 133; 132, 134; 133, string:'Matrix homeserver RTT times'; 134, keyword_argument; 134, 135; 134, 136; 135, identifier:rtt_times; 136, identifier:sorted_servers; 137, return_statement; 137, 138; 138, identifier:sorted_servers
def sort_servers_closest(servers: Sequence[str]) -> Sequence[Tuple[str, float]]: """Sorts a list of servers by http round-trip time Params: servers: sequence of http server urls Returns: sequence of pairs of url,rtt in seconds, sorted by rtt, excluding failed servers (possibly empty) """ if not {urlparse(url).scheme for url in servers}.issubset({'http', 'https'}): raise TransportError('Invalid server urls') get_rtt_jobs = set( gevent.spawn(lambda url: (url, get_http_rtt(url)), server_url) for server_url in servers ) # these tasks should never raise, returns None on errors gevent.joinall(get_rtt_jobs, raise_error=False) # block and wait tasks sorted_servers: List[Tuple[str, float]] = sorted( (job.value for job in get_rtt_jobs if job.value[1] is not None), key=itemgetter(1), ) log.debug('Matrix homeserver RTT times', rtt_times=sorted_servers) return sorted_servers
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:get_runs_by_id; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:config_id; 6, block; 6, 7; 6, 9; 6, 17; 6, 21; 6, 110; 6, 124; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:d; 12, subscript; 12, 13; 12, 16; 13, attribute; 13, 14; 13, 15; 14, identifier:self; 15, identifier:data; 16, identifier:config_id; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 20; 19, identifier:runs; 20, list:[]; 21, for_statement; 21, 22; 21, 23; 21, 30; 22, identifier:b; 23, call; 23, 24; 23, 29; 24, attribute; 24, 25; 24, 28; 25, attribute; 25, 26; 25, 27; 26, identifier:d; 27, identifier:results; 28, identifier:keys; 29, argument_list; 30, block; 30, 31; 31, try_statement; 31, 32; 31, 107; 32, block; 32, 33; 32, 45; 32, 100; 33, expression_statement; 33, 34; 34, assignment; 34, 35; 34, 36; 35, identifier:err_logs; 36, call; 36, 37; 36, 42; 37, attribute; 37, 38; 37, 41; 38, attribute; 38, 39; 38, 40; 39, identifier:d; 40, identifier:exceptions; 41, identifier:get; 42, argument_list; 42, 43; 42, 44; 43, identifier:b; 44, None; 45, if_statement; 45, 46; 45, 53; 45, 70; 46, comparison_operator:is; 46, 47; 46, 52; 47, subscript; 47, 48; 47, 51; 48, attribute; 48, 49; 48, 50; 49, identifier:d; 50, identifier:results; 51, identifier:b; 52, None; 53, block; 53, 54; 54, expression_statement; 54, 55; 55, assignment; 55, 56; 55, 57; 56, identifier:r; 57, call; 57, 58; 57, 59; 58, identifier:Run; 59, argument_list; 59, 60; 59, 61; 59, 62; 59, 63; 59, 64; 59, 69; 60, identifier:config_id; 61, identifier:b; 62, None; 63, None; 64, subscript; 64, 65; 64, 68; 65, attribute; 65, 66; 65, 67; 66, identifier:d; 67, identifier:time_stamps; 68, identifier:b; 69, identifier:err_logs; 70, else_clause; 70, 71; 71, block; 71, 72; 72, expression_statement; 72, 73; 73, assignment; 73, 74; 73, 75; 74, identifier:r; 75, call; 75, 76; 75, 77; 76, identifier:Run; 77, argument_list; 77, 78; 77, 79; 77, 80; 77, 87; 77, 94; 77, 99; 78, identifier:config_id; 79, identifier:b; 80, subscript; 80, 81; 80, 86; 81, subscript; 81, 82; 81, 85; 82, attribute; 82, 83; 82, 84; 83, identifier:d; 84, identifier:results; 85, identifier:b; 86, string:'loss'; 87, subscript; 87, 88; 87, 93; 88, subscript; 88, 89; 88, 92; 89, attribute; 89, 90; 89, 91; 90, identifier:d; 91, identifier:results; 92, identifier:b; 93, string:'info'; 94, subscript; 94, 95; 94, 98; 95, attribute; 95, 96; 95, 97; 96, identifier:d; 97, identifier:time_stamps; 98, identifier:b; 99, identifier:err_logs; 100, expression_statement; 100, 101; 101, call; 101, 102; 101, 105; 102, attribute; 102, 103; 102, 104; 103, identifier:runs; 104, identifier:append; 105, argument_list; 105, 106; 106, identifier:r; 107, except_clause; 107, 108; 108, block; 108, 109; 109, raise_statement; 110, expression_statement; 110, 111; 111, call; 111, 112; 111, 115; 112, attribute; 112, 113; 112, 114; 113, identifier:runs; 114, identifier:sort; 115, argument_list; 115, 116; 116, keyword_argument; 116, 117; 116, 118; 117, identifier:key; 118, lambda; 118, 119; 118, 121; 119, lambda_parameters; 119, 120; 120, identifier:r; 121, attribute; 121, 122; 121, 123; 122, identifier:r; 123, identifier:budget; 124, return_statement; 124, 125; 125, parenthesized_expression; 125, 126; 126, identifier:runs
def get_runs_by_id(self, config_id): """ returns a list of runs for a given config id The runs are sorted by ascending budget, so '-1' will give the longest run for this config. """ d = self.data[config_id] runs = [] for b in d.results.keys(): try: err_logs = d.exceptions.get(b, None) if d.results[b] is None: r = Run(config_id, b, None, None , d.time_stamps[b], err_logs) else: r = Run(config_id, b, d.results[b]['loss'], d.results[b]['info'] , d.time_stamps[b], err_logs) runs.append(r) except: raise runs.sort(key=lambda r: r.budget) return(runs)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 1, 15; 2, function_name:spell; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, typed_parameter; 5, 6; 5, 7; 6, identifier:word; 7, type; 7, 8; 8, identifier:str; 9, type; 9, 10; 10, generic_type; 10, 11; 10, 12; 11, identifier:List; 12, type_parameter; 12, 13; 13, type; 13, 14; 14, identifier:str; 15, block; 15, 16; 15, 18; 15, 24; 15, 58; 15, 72; 16, expression_statement; 16, 17; 17, comment; 18, if_statement; 18, 19; 18, 21; 19, not_operator; 19, 20; 20, identifier:word; 21, block; 21, 22; 22, return_statement; 22, 23; 23, string:""; 24, expression_statement; 24, 25; 25, assignment; 25, 26; 25, 27; 26, identifier:candidates; 27, parenthesized_expression; 27, 28; 28, boolean_operator:or; 28, 29; 28, 56; 29, boolean_operator:or; 29, 30; 29, 47; 30, boolean_operator:or; 30, 31; 30, 38; 31, call; 31, 32; 31, 35; 32, attribute; 32, 33; 32, 34; 33, identifier:self; 34, identifier:known; 35, argument_list; 35, 36; 36, list:[word]; 36, 37; 37, identifier:word; 38, call; 38, 39; 38, 42; 39, attribute; 39, 40; 39, 41; 40, identifier:self; 41, identifier:known; 42, argument_list; 42, 43; 43, call; 43, 44; 43, 45; 44, identifier:_edits1; 45, argument_list; 45, 46; 46, identifier:word; 47, call; 47, 48; 47, 51; 48, attribute; 48, 49; 48, 50; 49, identifier:self; 50, identifier:known; 51, argument_list; 51, 52; 52, call; 52, 53; 52, 54; 53, identifier:_edits2; 54, argument_list; 54, 55; 55, identifier:word; 56, list:[word]; 56, 57; 57, identifier:word; 58, expression_statement; 58, 59; 59, call; 59, 60; 59, 63; 60, attribute; 60, 61; 60, 62; 61, identifier:candidates; 62, identifier:sort; 63, argument_list; 63, 64; 63, 69; 64, keyword_argument; 64, 65; 64, 66; 65, identifier:key; 66, attribute; 66, 67; 66, 68; 67, identifier:self; 68, identifier:freq; 69, keyword_argument; 69, 70; 69, 71; 70, identifier:reverse; 71, True; 72, return_statement; 72, 73; 73, identifier:candidates
def spell(self, word: str) -> List[str]: """ Return a list of possible words, according to edit distance of 1 and 2, sorted by frequency of word occurrance in the spelling dictionary :param str word: A word to check its spelling """ if not word: return "" candidates = ( self.known([word]) or self.known(_edits1(word)) or self.known(_edits2(word)) or [word] ) candidates.sort(key=self.freq, reverse=True) return candidates
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 17; 1, 19; 2, function_name:rank; 3, parameters; 3, 4; 3, 12; 4, typed_parameter; 4, 5; 4, 6; 5, identifier:words; 6, type; 6, 7; 7, generic_type; 7, 8; 7, 9; 8, identifier:List; 9, type_parameter; 9, 10; 10, type; 10, 11; 11, identifier:str; 12, typed_default_parameter; 12, 13; 12, 14; 12, 16; 13, identifier:exclude_stopwords; 14, type; 14, 15; 15, identifier:bool; 16, False; 17, type; 17, 18; 18, identifier:Counter; 19, block; 19, 20; 19, 22; 19, 28; 19, 43; 20, expression_statement; 20, 21; 21, comment; 22, if_statement; 22, 23; 22, 25; 23, not_operator; 23, 24; 24, identifier:words; 25, block; 25, 26; 26, return_statement; 26, 27; 27, None; 28, if_statement; 28, 29; 28, 30; 29, identifier:exclude_stopwords; 30, block; 30, 31; 31, expression_statement; 31, 32; 32, assignment; 32, 33; 32, 34; 33, identifier:words; 34, list_comprehension; 34, 35; 34, 36; 34, 39; 35, identifier:word; 36, for_in_clause; 36, 37; 36, 38; 37, identifier:word; 38, identifier:words; 39, if_clause; 39, 40; 40, comparison_operator:not; 40, 41; 40, 42; 41, identifier:word; 42, identifier:_STOPWORDS; 43, return_statement; 43, 44; 44, call; 44, 45; 44, 46; 45, identifier:Counter; 46, argument_list; 46, 47; 47, identifier:words
def rank(words: List[str], exclude_stopwords: bool = False) -> Counter: """ Sort words by frequency :param list words: a list of words :param bool exclude_stopwords: exclude stopwords :return: Counter """ if not words: return None if exclude_stopwords: words = [word for word in words if word not in _STOPWORDS] return Counter(words)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 16; 2, function_name:iter; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 14; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:offset; 7, integer:0; 8, default_parameter; 8, 9; 8, 10; 9, identifier:count; 10, None; 11, default_parameter; 11, 12; 11, 13; 12, identifier:pagesize; 13, None; 14, dictionary_splat_pattern; 14, 15; 15, identifier:kwargs; 16, block; 16, 17; 16, 19; 16, 27; 16, 38; 16, 42; 17, expression_statement; 17, 18; 18, comment; 19, assert_statement; 19, 20; 20, boolean_operator:or; 20, 21; 20, 24; 21, comparison_operator:is; 21, 22; 21, 23; 22, identifier:pagesize; 23, None; 24, comparison_operator:>; 24, 25; 24, 26; 25, identifier:pagesize; 26, integer:0; 27, if_statement; 27, 28; 27, 31; 28, comparison_operator:is; 28, 29; 28, 30; 29, identifier:count; 30, None; 31, block; 31, 32; 32, expression_statement; 32, 33; 33, assignment; 33, 34; 33, 35; 34, identifier:count; 35, attribute; 35, 36; 35, 37; 36, identifier:self; 37, identifier:null_count; 38, expression_statement; 38, 39; 39, assignment; 39, 40; 39, 41; 40, identifier:fetched; 41, integer:0; 42, while_statement; 42, 43; 42, 52; 43, boolean_operator:or; 43, 44; 43, 49; 44, comparison_operator:==; 44, 45; 44, 46; 45, identifier:count; 46, attribute; 46, 47; 46, 48; 47, identifier:self; 48, identifier:null_count; 49, comparison_operator:<; 49, 50; 49, 51; 50, identifier:fetched; 51, identifier:count; 52, block; 52, 53; 52, 71; 52, 80; 52, 87; 52, 91; 52, 98; 52, 108; 52, 112; 53, expression_statement; 53, 54; 54, assignment; 54, 55; 54, 56; 55, identifier:response; 56, call; 56, 57; 56, 60; 57, attribute; 57, 58; 57, 59; 58, identifier:self; 59, identifier:get; 60, argument_list; 60, 61; 60, 66; 60, 69; 61, keyword_argument; 61, 62; 61, 63; 62, identifier:count; 63, boolean_operator:or; 63, 64; 63, 65; 64, identifier:pagesize; 65, identifier:count; 66, keyword_argument; 66, 67; 66, 68; 67, identifier:offset; 68, identifier:offset; 69, dictionary_splat; 69, 70; 70, identifier:kwargs; 71, expression_statement; 71, 72; 72, assignment; 72, 73; 72, 74; 73, identifier:items; 74, call; 74, 75; 74, 78; 75, attribute; 75, 76; 75, 77; 76, identifier:self; 77, identifier:_load_list; 78, argument_list; 78, 79; 79, identifier:response; 80, expression_statement; 80, 81; 81, assignment; 81, 82; 81, 83; 82, identifier:N; 83, call; 83, 84; 83, 85; 84, identifier:len; 85, argument_list; 85, 86; 86, identifier:items; 87, expression_statement; 87, 88; 88, augmented_assignment:+=; 88, 89; 88, 90; 89, identifier:fetched; 90, identifier:N; 91, for_statement; 91, 92; 91, 93; 91, 94; 92, identifier:item; 93, identifier:items; 94, block; 94, 95; 95, expression_statement; 95, 96; 96, yield; 96, 97; 97, identifier:item; 98, if_statement; 98, 99; 98, 106; 99, boolean_operator:or; 99, 100; 99, 103; 100, comparison_operator:is; 100, 101; 100, 102; 101, identifier:pagesize; 102, None; 103, comparison_operator:<; 103, 104; 103, 105; 104, identifier:N; 105, identifier:pagesize; 106, block; 106, 107; 107, break_statement; 108, expression_statement; 108, 109; 109, augmented_assignment:+=; 109, 110; 109, 111; 110, identifier:offset; 111, identifier:N; 112, expression_statement; 112, 113; 113, call; 113, 114; 113, 117; 114, attribute; 114, 115; 114, 116; 115, identifier:logging; 116, identifier:debug; 117, argument_list; 117, 118; 117, 119; 117, 120; 117, 121; 117, 122; 117, 123; 118, string:"pagesize=%d, fetched=%d, offset=%d, N=%d, kwargs=%s"; 119, identifier:pagesize; 120, identifier:fetched; 121, identifier:offset; 122, identifier:N; 123, identifier:kwargs
def iter(self, offset=0, count=None, pagesize=None, **kwargs): """Iterates over the collection. This method is equivalent to the :meth:`list` method, but it returns an iterator and can load a certain number of entities at a time from the server. :param offset: The index of the first entity to return (optional). :type offset: ``integer`` :param count: The maximum number of entities to return (optional). :type count: ``integer`` :param pagesize: The number of entities to load (optional). :type pagesize: ``integer`` :param kwargs: Additional arguments (optional): - "search" (``string``): The search query to filter responses. - "sort_dir" (``string``): The direction to sort returned items: "asc" or "desc". - "sort_key" (``string``): The field to use for sorting (optional). - "sort_mode" (``string``): The collating sequence for sorting returned items: "auto", "alpha", "alpha_case", or "num". :type kwargs: ``dict`` **Example**:: import splunklib.client as client s = client.connect(...) for saved_search in s.saved_searches.iter(pagesize=10): # Loads 10 saved searches at a time from the # server. ... """ assert pagesize is None or pagesize > 0 if count is None: count = self.null_count fetched = 0 while count == self.null_count or fetched < count: response = self.get(count=pagesize or count, offset=offset, **kwargs) items = self._load_list(response) N = len(items) fetched += N for item in items: yield item if pagesize is None or N < pagesize: break offset += N logging.debug("pagesize=%d, fetched=%d, offset=%d, N=%d, kwargs=%s", pagesize, fetched, offset, N, kwargs)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 10; 2, function_name:list; 3, parameters; 3, 4; 3, 5; 3, 8; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:count; 7, None; 8, dictionary_splat_pattern; 8, 9; 9, identifier:kwargs; 10, block; 10, 11; 10, 13; 10, 14; 10, 15; 11, expression_statement; 11, 12; 12, comment; 13, comment; 14, comment; 15, return_statement; 15, 16; 16, call; 16, 17; 16, 18; 17, identifier:list; 18, argument_list; 18, 19; 19, call; 19, 20; 19, 23; 20, attribute; 20, 21; 20, 22; 21, identifier:self; 22, identifier:iter; 23, argument_list; 23, 24; 23, 27; 24, keyword_argument; 24, 25; 24, 26; 25, identifier:count; 26, identifier:count; 27, dictionary_splat; 27, 28; 28, identifier:kwargs
def list(self, count=None, **kwargs): """Retrieves a list of entities in this collection. The entire collection is loaded at once and is returned as a list. This function makes a single roundtrip to the server, plus at most two more if the ``autologin`` field of :func:`connect` is set to ``True``. There is no caching--every call makes at least one round trip. :param count: The maximum number of entities to return (optional). :type count: ``integer`` :param kwargs: Additional arguments (optional): - "offset" (``integer``): The offset of the first item to return. - "search" (``string``): The search query to filter responses. - "sort_dir" (``string``): The direction to sort returned items: "asc" or "desc". - "sort_key" (``string``): The field to use for sorting (optional). - "sort_mode" (``string``): The collating sequence for sorting returned items: "auto", "alpha", "alpha_case", or "num". :type kwargs: ``dict`` :return: A ``list`` of entities. """ # response = self.get(count=count, **kwargs) # return self._load_list(response) return list(self.iter(count=count, **kwargs))
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:query; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, dictionary_splat_pattern; 5, 6; 6, identifier:query; 7, block; 7, 8; 7, 10; 8, expression_statement; 8, 9; 9, comment; 10, return_statement; 10, 11; 11, call; 11, 12; 11, 15; 12, attribute; 12, 13; 12, 14; 13, identifier:json; 14, identifier:loads; 15, argument_list; 15, 16; 16, call; 16, 17; 16, 33; 17, attribute; 17, 18; 17, 32; 18, call; 18, 19; 18, 31; 19, attribute; 19, 20; 19, 30; 20, attribute; 20, 21; 20, 29; 21, call; 21, 22; 21, 25; 22, attribute; 22, 23; 22, 24; 23, identifier:self; 24, identifier:_get; 25, argument_list; 25, 26; 25, 27; 26, string:''; 27, dictionary_splat; 27, 28; 28, identifier:query; 29, identifier:body; 30, identifier:read; 31, argument_list; 32, identifier:decode; 33, argument_list; 33, 34; 34, string:'utf-8'
def query(self, **query): """ Gets the results of query, with optional parameters sort, limit, skip, and fields. :param query: Optional parameters. Valid options are sort, limit, skip, and fields :type query: ``dict`` :return: Array of documents retrieved by query. :rtype: ``array`` """ return json.loads(self._get('', **query).body.read().decode('utf-8'))
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 28; 2, function_name:query; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 14; 3, 17; 3, 20; 3, 23; 3, 26; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:area; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:date; 10, None; 11, default_parameter; 11, 12; 11, 13; 12, identifier:raw; 13, None; 14, default_parameter; 14, 15; 14, 16; 15, identifier:area_relation; 16, string:'Intersects'; 17, default_parameter; 17, 18; 17, 19; 18, identifier:order_by; 19, None; 20, default_parameter; 20, 21; 20, 22; 21, identifier:limit; 22, None; 23, default_parameter; 23, 24; 23, 25; 24, identifier:offset; 25, integer:0; 26, dictionary_splat_pattern; 26, 27; 27, identifier:keywords; 28, block; 28, 29; 28, 31; 28, 45; 28, 58; 28, 65; 28, 79; 28, 89; 29, expression_statement; 29, 30; 30, comment; 31, expression_statement; 31, 32; 32, assignment; 32, 33; 32, 34; 33, identifier:query; 34, call; 34, 35; 34, 38; 35, attribute; 35, 36; 35, 37; 36, identifier:self; 37, identifier:format_query; 38, argument_list; 38, 39; 38, 40; 38, 41; 38, 42; 38, 43; 39, identifier:area; 40, identifier:date; 41, identifier:raw; 42, identifier:area_relation; 43, dictionary_splat; 43, 44; 44, identifier:keywords; 45, expression_statement; 45, 46; 46, call; 46, 47; 46, 52; 47, attribute; 47, 48; 47, 51; 48, attribute; 48, 49; 48, 50; 49, identifier:self; 50, identifier:logger; 51, identifier:debug; 52, argument_list; 52, 53; 52, 54; 52, 55; 52, 56; 52, 57; 53, string:"Running query: order_by=%s, limit=%s, offset=%s, query=%s"; 54, identifier:order_by; 55, identifier:limit; 56, identifier:offset; 57, identifier:query; 58, expression_statement; 58, 59; 59, assignment; 59, 60; 59, 61; 60, identifier:formatted_order_by; 61, call; 61, 62; 61, 63; 62, identifier:_format_order_by; 63, argument_list; 63, 64; 64, identifier:order_by; 65, expression_statement; 65, 66; 66, assignment; 66, 67; 66, 70; 67, pattern_list; 67, 68; 67, 69; 68, identifier:response; 69, identifier:count; 70, call; 70, 71; 70, 74; 71, attribute; 71, 72; 71, 73; 72, identifier:self; 73, identifier:_load_query; 74, argument_list; 74, 75; 74, 76; 74, 77; 74, 78; 75, identifier:query; 76, identifier:formatted_order_by; 77, identifier:limit; 78, identifier:offset; 79, expression_statement; 79, 80; 80, call; 80, 81; 80, 86; 81, attribute; 81, 82; 81, 85; 82, attribute; 82, 83; 82, 84; 83, identifier:self; 84, identifier:logger; 85, identifier:info; 86, argument_list; 86, 87; 86, 88; 87, string:"Found %s products"; 88, identifier:count; 89, return_statement; 89, 90; 90, call; 90, 91; 90, 92; 91, identifier:_parse_opensearch_response; 92, argument_list; 92, 93; 93, identifier:response
def query(self, area=None, date=None, raw=None, area_relation='Intersects', order_by=None, limit=None, offset=0, **keywords): """Query the OpenSearch API with the coordinates of an area, a date interval and any other search keywords accepted by the API. Parameters ---------- area : str, optional The area of interest formatted as a Well-Known Text string. date : tuple of (str or datetime) or str, optional A time interval filter based on the Sensing Start Time of the products. Expects a tuple of (start, end), e.g. ("NOW-1DAY", "NOW"). The timestamps can be either a Python datetime or a string in one of the following formats: - yyyyMMdd - yyyy-MM-ddThh:mm:ss.SSSZ (ISO-8601) - yyyy-MM-ddThh:mm:ssZ - NOW - NOW-<n>DAY(S) (or HOUR(S), MONTH(S), etc.) - NOW+<n>DAY(S) - yyyy-MM-ddThh:mm:ssZ-<n>DAY(S) - NOW/DAY (or HOUR, MONTH etc.) - rounds the value to the given unit Alternatively, an already fully formatted string such as "[NOW-1DAY TO NOW]" can be used as well. raw : str, optional Additional query text that will be appended to the query. area_relation : {'Intersects', 'Contains', 'IsWithin'}, optional What relation to use for testing the AOI. Case insensitive. - Intersects: true if the AOI and the footprint intersect (default) - Contains: true if the AOI is inside the footprint - IsWithin: true if the footprint is inside the AOI order_by: str, optional A comma-separated list of fields to order by (on server side). Prefix the field name by '+' or '-' to sort in ascending or descending order, respectively. Ascending order is used if prefix is omitted. Example: "cloudcoverpercentage, -beginposition". limit: int, optional Maximum number of products returned. Defaults to no limit. offset: int, optional The number of results to skip. Defaults to 0. **keywords Additional keywords can be used to specify other query parameters, e.g. `relativeorbitnumber=70`. See https://scihub.copernicus.eu/twiki/do/view/SciHubUserGuide/3FullTextSearch for a full list. Range values can be passed as two-element tuples, e.g. `cloudcoverpercentage=(0, 30)`. `None` can be used in range values for one-sided ranges, e.g. `orbitnumber=(16302, None)`. Ranges with no bounds (`orbitnumber=(None, None)`) will not be included in the query. The time interval formats accepted by the `date` parameter can also be used with any other parameters that expect time intervals (that is: 'beginposition', 'endposition', 'date', 'creationdate', and 'ingestiondate'). Returns ------- dict[string, dict] Products returned by the query as a dictionary with the product ID as the key and the product's attributes (a dictionary) as the value. """ query = self.format_query(area, date, raw, area_relation, **keywords) self.logger.debug("Running query: order_by=%s, limit=%s, offset=%s, query=%s", order_by, limit, offset, query) formatted_order_by = _format_order_by(order_by) response, count = self._load_query(query, formatted_order_by, limit, offset) self.logger.info("Found %s products", count) return _parse_opensearch_response(response)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:get_suggested_type_names; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:schema; 5, identifier:output_type; 6, identifier:field_name; 7, block; 7, 8; 7, 10; 7, 134; 7, 135; 8, expression_statement; 8, 9; 9, comment; 10, if_statement; 10, 11; 10, 18; 11, call; 11, 12; 11, 13; 12, identifier:isinstance; 13, argument_list; 13, 14; 13, 15; 14, identifier:output_type; 15, tuple; 15, 16; 15, 17; 16, identifier:GraphQLInterfaceType; 17, identifier:GraphQLUnionType; 18, block; 18, 19; 18, 23; 18, 29; 18, 98; 18, 99; 18, 124; 18, 125; 18, 132; 19, expression_statement; 19, 20; 20, assignment; 20, 21; 20, 22; 21, identifier:suggested_object_types; 22, list:[]; 23, expression_statement; 23, 24; 24, assignment; 24, 25; 24, 26; 25, identifier:interface_usage_count; 26, call; 26, 27; 26, 28; 27, identifier:OrderedDict; 28, argument_list; 29, for_statement; 29, 30; 29, 31; 29, 37; 30, identifier:possible_type; 31, call; 31, 32; 31, 35; 32, attribute; 32, 33; 32, 34; 33, identifier:schema; 34, identifier:get_possible_types; 35, argument_list; 35, 36; 36, identifier:output_type; 37, block; 37, 38; 37, 50; 37, 51; 37, 60; 38, if_statement; 38, 39; 38, 48; 39, not_operator; 39, 40; 40, call; 40, 41; 40, 46; 41, attribute; 41, 42; 41, 45; 42, attribute; 42, 43; 42, 44; 43, identifier:possible_type; 44, identifier:fields; 45, identifier:get; 46, argument_list; 46, 47; 47, identifier:field_name; 48, block; 48, 49; 49, return_statement; 50, comment; 51, expression_statement; 51, 52; 52, call; 52, 53; 52, 56; 53, attribute; 53, 54; 53, 55; 54, identifier:suggested_object_types; 55, identifier:append; 56, argument_list; 56, 57; 57, attribute; 57, 58; 57, 59; 58, identifier:possible_type; 59, identifier:name; 60, for_statement; 60, 61; 60, 62; 60, 65; 61, identifier:possible_interface; 62, attribute; 62, 63; 62, 64; 63, identifier:possible_type; 64, identifier:interfaces; 65, block; 65, 66; 65, 78; 65, 79; 66, if_statement; 66, 67; 66, 76; 67, not_operator; 67, 68; 68, call; 68, 69; 68, 74; 69, attribute; 69, 70; 69, 73; 70, attribute; 70, 71; 70, 72; 71, identifier:possible_interface; 72, identifier:fields; 73, identifier:get; 74, argument_list; 74, 75; 75, identifier:field_name; 76, block; 76, 77; 77, continue_statement; 78, comment; 79, expression_statement; 79, 80; 80, assignment; 80, 81; 80, 86; 81, subscript; 81, 82; 81, 83; 82, identifier:interface_usage_count; 83, attribute; 83, 84; 83, 85; 84, identifier:possible_interface; 85, identifier:name; 86, parenthesized_expression; 86, 87; 87, binary_operator:+; 87, 88; 87, 97; 88, call; 88, 89; 88, 92; 89, attribute; 89, 90; 89, 91; 90, identifier:interface_usage_count; 91, identifier:get; 92, argument_list; 92, 93; 92, 96; 93, attribute; 93, 94; 93, 95; 94, identifier:possible_interface; 95, identifier:name; 96, integer:0; 97, integer:1; 98, comment; 99, expression_statement; 99, 100; 100, assignment; 100, 101; 100, 102; 101, identifier:suggested_interface_types; 102, call; 102, 103; 102, 104; 103, identifier:sorted; 104, argument_list; 104, 105; 104, 113; 104, 121; 105, call; 105, 106; 105, 107; 106, identifier:list; 107, argument_list; 107, 108; 108, call; 108, 109; 108, 112; 109, attribute; 109, 110; 109, 111; 110, identifier:interface_usage_count; 111, identifier:keys; 112, argument_list; 113, keyword_argument; 113, 114; 113, 115; 114, identifier:key; 115, lambda; 115, 116; 115, 118; 116, lambda_parameters; 116, 117; 117, identifier:k; 118, subscript; 118, 119; 118, 120; 119, identifier:interface_usage_count; 120, identifier:k; 121, keyword_argument; 121, 122; 121, 123; 122, identifier:reverse; 123, True; 124, comment; 125, expression_statement; 125, 126; 126, call; 126, 127; 126, 130; 127, attribute; 127, 128; 127, 129; 128, identifier:suggested_interface_types; 129, identifier:extend; 130, argument_list; 130, 131; 131, identifier:suggested_object_types; 132, return_statement; 132, 133; 133, identifier:suggested_interface_types; 134, comment; 135, return_statement; 135, 136; 136, list:[]
def get_suggested_type_names(schema, output_type, field_name): """Go through all of the implementations of type, as well as the interfaces that they implement. If any of those types include the provided field, suggest them, sorted by how often the type is referenced, starting with Interfaces.""" if isinstance(output_type, (GraphQLInterfaceType, GraphQLUnionType)): suggested_object_types = [] interface_usage_count = OrderedDict() for possible_type in schema.get_possible_types(output_type): if not possible_type.fields.get(field_name): return # This object type defines this field. suggested_object_types.append(possible_type.name) for possible_interface in possible_type.interfaces: if not possible_interface.fields.get(field_name): continue # This interface type defines this field. interface_usage_count[possible_interface.name] = ( interface_usage_count.get(possible_interface.name, 0) + 1 ) # Suggest interface types based on how common they are. suggested_interface_types = sorted( list(interface_usage_count.keys()), key=lambda k: interface_usage_count[k], reverse=True, ) # Suggest both interface and object types. suggested_interface_types.extend(suggested_object_types) return suggested_interface_types # Otherwise, must be an Object type, which does not have possible fields. return []
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:suggestion_list; 3, parameters; 3, 4; 3, 5; 4, identifier:inp; 5, identifier:options; 6, block; 6, 7; 6, 9; 6, 15; 6, 24; 6, 61; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:options_by_distance; 12, call; 12, 13; 12, 14; 13, identifier:OrderedDict; 14, argument_list; 15, expression_statement; 15, 16; 16, assignment; 16, 17; 16, 18; 17, identifier:input_threshold; 18, binary_operator:/; 18, 19; 18, 23; 19, call; 19, 20; 19, 21; 20, identifier:len; 21, argument_list; 21, 22; 22, identifier:inp; 23, integer:2; 24, for_statement; 24, 25; 24, 26; 24, 27; 25, identifier:option; 26, identifier:options; 27, block; 27, 28; 27, 36; 27, 50; 28, expression_statement; 28, 29; 29, assignment; 29, 30; 29, 31; 30, identifier:distance; 31, call; 31, 32; 31, 33; 32, identifier:lexical_distance; 33, argument_list; 33, 34; 33, 35; 34, identifier:inp; 35, identifier:option; 36, expression_statement; 36, 37; 37, assignment; 37, 38; 37, 39; 38, identifier:threshold; 39, call; 39, 40; 39, 41; 40, identifier:max; 41, argument_list; 41, 42; 41, 43; 41, 49; 42, identifier:input_threshold; 43, binary_operator:/; 43, 44; 43, 48; 44, call; 44, 45; 44, 46; 45, identifier:len; 46, argument_list; 46, 47; 47, identifier:option; 48, integer:2; 49, integer:1; 50, if_statement; 50, 51; 50, 54; 51, comparison_operator:<=; 51, 52; 51, 53; 52, identifier:distance; 53, identifier:threshold; 54, block; 54, 55; 55, expression_statement; 55, 56; 56, assignment; 56, 57; 56, 60; 57, subscript; 57, 58; 57, 59; 58, identifier:options_by_distance; 59, identifier:option; 60, identifier:distance; 61, return_statement; 61, 62; 62, call; 62, 63; 62, 64; 63, identifier:sorted; 64, argument_list; 64, 65; 64, 73; 65, call; 65, 66; 65, 67; 66, identifier:list; 67, argument_list; 67, 68; 68, call; 68, 69; 68, 72; 69, attribute; 69, 70; 69, 71; 70, identifier:options_by_distance; 71, identifier:keys; 72, argument_list; 73, keyword_argument; 73, 74; 73, 75; 74, identifier:key; 75, lambda; 75, 76; 75, 78; 76, lambda_parameters; 76, 77; 77, identifier:k; 78, subscript; 78, 79; 78, 80; 79, identifier:options_by_distance; 80, identifier:k
def suggestion_list(inp, options): """ Given an invalid input string and a list of valid options, returns a filtered list of valid options sorted based on their similarity with the input. """ options_by_distance = OrderedDict() input_threshold = len(inp) / 2 for option in options: distance = lexical_distance(inp, option) threshold = max(input_threshold, len(option) / 2, 1) if distance <= threshold: options_by_distance[option] = distance return sorted( list(options_by_distance.keys()), key=lambda k: options_by_distance[k] )
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:lexical_distance; 3, parameters; 3, 4; 3, 5; 4, identifier:a; 5, identifier:b; 6, block; 6, 7; 6, 9; 6, 28; 6, 37; 6, 82; 6, 223; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:d; 12, boolean_operator:or; 12, 13; 12, 27; 13, list_comprehension; 13, 14; 13, 16; 14, list:[i]; 14, 15; 15, identifier:i; 16, for_in_clause; 16, 17; 16, 18; 17, identifier:i; 18, call; 18, 19; 18, 20; 19, identifier:range; 20, argument_list; 20, 21; 21, binary_operator:+; 21, 22; 21, 26; 22, call; 22, 23; 22, 24; 23, identifier:len; 24, argument_list; 24, 25; 25, identifier:a; 26, integer:1; 27, list:[]; 28, expression_statement; 28, 29; 29, assignment; 29, 30; 29, 31; 30, identifier:d_len; 31, boolean_operator:or; 31, 32; 31, 36; 32, call; 32, 33; 32, 34; 33, identifier:len; 34, argument_list; 34, 35; 35, identifier:d; 36, integer:1; 37, for_statement; 37, 38; 37, 39; 37, 43; 38, identifier:i; 39, call; 39, 40; 39, 41; 40, identifier:range; 41, argument_list; 41, 42; 42, identifier:d_len; 43, block; 43, 44; 44, for_statement; 44, 45; 44, 46; 44, 56; 45, identifier:j; 46, call; 46, 47; 46, 48; 47, identifier:range; 48, argument_list; 48, 49; 48, 50; 49, integer:1; 50, binary_operator:+; 50, 51; 50, 55; 51, call; 51, 52; 51, 53; 52, identifier:len; 53, argument_list; 53, 54; 54, identifier:b; 55, integer:1; 56, block; 56, 57; 57, if_statement; 57, 58; 57, 61; 57, 71; 58, comparison_operator:==; 58, 59; 58, 60; 59, identifier:i; 60, integer:0; 61, block; 61, 62; 62, expression_statement; 62, 63; 63, call; 63, 64; 63, 69; 64, attribute; 64, 65; 64, 68; 65, subscript; 65, 66; 65, 67; 66, identifier:d; 67, identifier:i; 68, identifier:append; 69, argument_list; 69, 70; 70, identifier:j; 71, else_clause; 71, 72; 72, block; 72, 73; 73, expression_statement; 73, 74; 74, call; 74, 75; 74, 80; 75, attribute; 75, 76; 75, 79; 76, subscript; 76, 77; 76, 78; 77, identifier:d; 78, identifier:i; 79, identifier:append; 80, argument_list; 80, 81; 81, integer:0; 82, for_statement; 82, 83; 82, 84; 82, 94; 83, identifier:i; 84, call; 84, 85; 84, 86; 85, identifier:range; 86, argument_list; 86, 87; 86, 88; 87, integer:1; 88, binary_operator:+; 88, 89; 88, 93; 89, call; 89, 90; 89, 91; 90, identifier:len; 91, argument_list; 91, 92; 92, identifier:a; 93, integer:1; 94, block; 94, 95; 95, for_statement; 95, 96; 95, 97; 95, 107; 96, identifier:j; 97, call; 97, 98; 97, 99; 98, identifier:range; 99, argument_list; 99, 100; 99, 101; 100, integer:1; 101, binary_operator:+; 101, 102; 101, 106; 102, call; 102, 103; 102, 104; 103, identifier:len; 104, argument_list; 104, 105; 105, identifier:b; 106, integer:1; 107, block; 107, 108; 107, 125; 107, 164; 108, expression_statement; 108, 109; 109, assignment; 109, 110; 109, 111; 110, identifier:cost; 111, conditional_expression:if; 111, 112; 111, 113; 111, 124; 112, integer:0; 113, comparison_operator:==; 113, 114; 113, 119; 114, subscript; 114, 115; 114, 116; 115, identifier:a; 116, binary_operator:-; 116, 117; 116, 118; 117, identifier:i; 118, integer:1; 119, subscript; 119, 120; 119, 121; 120, identifier:b; 121, binary_operator:-; 121, 122; 121, 123; 122, identifier:j; 123, integer:1; 124, integer:1; 125, expression_statement; 125, 126; 126, assignment; 126, 127; 126, 132; 127, subscript; 127, 128; 127, 131; 128, subscript; 128, 129; 128, 130; 129, identifier:d; 130, identifier:i; 131, identifier:j; 132, call; 132, 133; 132, 134; 133, identifier:min; 134, argument_list; 134, 135; 134, 144; 134, 153; 135, binary_operator:+; 135, 136; 135, 143; 136, subscript; 136, 137; 136, 142; 137, subscript; 137, 138; 137, 139; 138, identifier:d; 139, binary_operator:-; 139, 140; 139, 141; 140, identifier:i; 141, integer:1; 142, identifier:j; 143, integer:1; 144, binary_operator:+; 144, 145; 144, 152; 145, subscript; 145, 146; 145, 149; 146, subscript; 146, 147; 146, 148; 147, identifier:d; 148, identifier:i; 149, binary_operator:-; 149, 150; 149, 151; 150, identifier:j; 151, integer:1; 152, integer:1; 153, binary_operator:+; 153, 154; 153, 163; 154, subscript; 154, 155; 154, 160; 155, subscript; 155, 156; 155, 157; 156, identifier:d; 157, binary_operator:-; 157, 158; 157, 159; 158, identifier:i; 159, integer:1; 160, binary_operator:-; 160, 161; 160, 162; 161, identifier:j; 162, integer:1; 163, identifier:cost; 164, if_statement; 164, 165; 164, 196; 165, boolean_operator:and; 165, 166; 165, 185; 166, boolean_operator:and; 166, 167; 166, 174; 167, boolean_operator:and; 167, 168; 167, 171; 168, comparison_operator:>; 168, 169; 168, 170; 169, identifier:i; 170, integer:1; 171, comparison_operator:<; 171, 172; 171, 173; 172, identifier:j; 173, integer:1; 174, comparison_operator:==; 174, 175; 174, 180; 175, subscript; 175, 176; 175, 177; 176, identifier:a; 177, binary_operator:-; 177, 178; 177, 179; 178, identifier:i; 179, integer:1; 180, subscript; 180, 181; 180, 182; 181, identifier:b; 182, binary_operator:-; 182, 183; 182, 184; 183, identifier:j; 184, integer:2; 185, comparison_operator:==; 185, 186; 185, 191; 186, subscript; 186, 187; 186, 188; 187, identifier:a; 188, binary_operator:-; 188, 189; 188, 190; 189, identifier:i; 190, integer:2; 191, subscript; 191, 192; 191, 193; 192, identifier:b; 193, binary_operator:-; 193, 194; 193, 195; 194, identifier:j; 195, integer:1; 196, block; 196, 197; 197, expression_statement; 197, 198; 198, assignment; 198, 199; 198, 204; 199, subscript; 199, 200; 199, 203; 200, subscript; 200, 201; 200, 202; 201, identifier:d; 202, identifier:i; 203, identifier:j; 204, call; 204, 205; 204, 206; 205, identifier:min; 206, argument_list; 206, 207; 206, 212; 207, subscript; 207, 208; 207, 211; 208, subscript; 208, 209; 208, 210; 209, identifier:d; 210, identifier:i; 211, identifier:j; 212, binary_operator:+; 212, 213; 212, 222; 213, subscript; 213, 214; 213, 219; 214, subscript; 214, 215; 214, 216; 215, identifier:d; 216, binary_operator:-; 216, 217; 216, 218; 217, identifier:i; 218, integer:2; 219, binary_operator:-; 219, 220; 219, 221; 220, identifier:j; 221, integer:2; 222, identifier:cost; 223, return_statement; 223, 224; 224, subscript; 224, 225; 224, 231; 225, subscript; 225, 226; 225, 227; 226, identifier:d; 227, call; 227, 228; 227, 229; 228, identifier:len; 229, argument_list; 229, 230; 230, identifier:a; 231, call; 231, 232; 231, 233; 232, identifier:len; 233, argument_list; 233, 234; 234, identifier:b
def lexical_distance(a, b): """ Computes the lexical distance between strings A and B. The "distance" between two strings is given by counting the minimum number of edits needed to transform string A into string B. An edit can be an insertion, deletion, or substitution of a single character, or a swap of two adjacent characters. This distance can be useful for detecting typos in input or sorting @returns distance in number of edits """ d = [[i] for i in range(len(a) + 1)] or [] d_len = len(d) or 1 for i in range(d_len): for j in range(1, len(b) + 1): if i == 0: d[i].append(j) else: d[i].append(0) for i in range(1, len(a) + 1): for j in range(1, len(b) + 1): cost = 0 if a[i - 1] == b[j - 1] else 1 d[i][j] = min(d[i - 1][j] + 1, d[i][j - 1] + 1, d[i - 1][j - 1] + cost) if i > 1 and j < 1 and a[i - 1] == b[j - 2] and a[i - 2] == b[j - 1]: d[i][j] = min(d[i][j], d[i - 2][j - 2] + cost) return d[len(a)][len(b)]
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:arrange; 3, parameters; 3, 4; 3, 5; 3, 7; 4, identifier:df; 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, 23; 9, 58; 9, 77; 9, 94; 10, expression_statement; 10, 11; 11, comment; 12, expression_statement; 12, 13; 13, assignment; 13, 14; 13, 15; 14, identifier:flat_args; 15, list_comprehension; 15, 16; 15, 17; 16, identifier:a; 17, for_in_clause; 17, 18; 17, 19; 18, identifier:a; 19, call; 19, 20; 19, 21; 20, identifier:flatten; 21, argument_list; 21, 22; 22, identifier:args; 23, expression_statement; 23, 24; 24, assignment; 24, 25; 24, 26; 25, identifier:series; 26, list_comprehension; 26, 27; 26, 55; 27, conditional_expression:if; 27, 28; 27, 31; 27, 36; 28, subscript; 28, 29; 28, 30; 29, identifier:df; 30, identifier:arg; 31, call; 31, 32; 31, 33; 32, identifier:isinstance; 33, argument_list; 33, 34; 33, 35; 34, identifier:arg; 35, identifier:str; 36, conditional_expression:if; 36, 37; 36, 44; 36, 49; 37, subscript; 37, 38; 37, 41; 37, 43; 38, attribute; 38, 39; 38, 40; 39, identifier:df; 40, identifier:iloc; 41, slice; 41, 42; 42, colon; 43, identifier:arg; 44, call; 44, 45; 44, 46; 45, identifier:isinstance; 46, argument_list; 46, 47; 46, 48; 47, identifier:arg; 48, identifier:int; 49, call; 49, 50; 49, 53; 50, attribute; 50, 51; 50, 52; 51, identifier:pd; 52, identifier:Series; 53, argument_list; 53, 54; 54, identifier:arg; 55, for_in_clause; 55, 56; 55, 57; 56, identifier:arg; 57, identifier:flat_args; 58, expression_statement; 58, 59; 59, assignment; 59, 60; 59, 61; 60, identifier:sorter; 61, call; 61, 62; 61, 73; 62, attribute; 62, 63; 62, 72; 63, call; 63, 64; 63, 67; 64, attribute; 64, 65; 64, 66; 65, identifier:pd; 66, identifier:concat; 67, argument_list; 67, 68; 67, 69; 68, identifier:series; 69, keyword_argument; 69, 70; 69, 71; 70, identifier:axis; 71, integer:1; 72, identifier:reset_index; 73, argument_list; 73, 74; 74, keyword_argument; 74, 75; 74, 76; 75, identifier:drop; 76, True; 77, expression_statement; 77, 78; 78, assignment; 78, 79; 78, 80; 79, identifier:sorter; 80, call; 80, 81; 80, 84; 81, attribute; 81, 82; 81, 83; 82, identifier:sorter; 83, identifier:sort_values; 84, argument_list; 84, 85; 84, 92; 85, call; 85, 86; 85, 91; 86, attribute; 86, 87; 86, 90; 87, attribute; 87, 88; 87, 89; 88, identifier:sorter; 89, identifier:columns; 90, identifier:tolist; 91, argument_list; 92, dictionary_splat; 92, 93; 93, identifier:kwargs; 94, return_statement; 94, 95; 95, subscript; 95, 96; 95, 99; 95, 102; 96, attribute; 96, 97; 96, 98; 97, identifier:df; 98, identifier:iloc; 99, attribute; 99, 100; 99, 101; 100, identifier:sorter; 101, identifier:index; 102, slice; 102, 103; 103, colon
def arrange(df, *args, **kwargs): """Calls `pandas.DataFrame.sort_values` to sort a DataFrame according to criteria. See: http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.sort_values.html For a list of specific keyword arguments for sort_values (which will be the same in arrange). Args: *args: Symbolic, string, integer or lists of those types indicating columns to sort the DataFrame by. Kwargs: **kwargs: Any keyword arguments will be passed through to the pandas `DataFrame.sort_values` function. """ flat_args = [a for a in flatten(args)] series = [df[arg] if isinstance(arg, str) else df.iloc[:, arg] if isinstance(arg, int) else pd.Series(arg) for arg in flat_args] sorter = pd.concat(series, axis=1).reset_index(drop=True) sorter = sorter.sort_values(sorter.columns.tolist(), **kwargs) return df.iloc[sorter.index, :]
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:csort; 3, parameters; 3, 4; 3, 5; 4, identifier:objs; 5, identifier:key; 6, block; 6, 7; 6, 9; 6, 26; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:idxs; 12, call; 12, 13; 12, 14; 13, identifier:dict; 14, generator_expression; 14, 15; 14, 18; 15, tuple; 15, 16; 15, 17; 16, identifier:obj; 17, identifier:i; 18, for_in_clause; 18, 19; 18, 22; 19, tuple_pattern; 19, 20; 19, 21; 20, identifier:i; 21, identifier:obj; 22, call; 22, 23; 22, 24; 23, identifier:enumerate; 24, argument_list; 24, 25; 25, identifier:objs; 26, return_statement; 26, 27; 27, call; 27, 28; 27, 29; 28, identifier:sorted; 29, argument_list; 29, 30; 29, 31; 30, identifier:objs; 31, keyword_argument; 31, 32; 31, 33; 32, identifier:key; 33, lambda; 33, 34; 33, 36; 34, lambda_parameters; 34, 35; 35, identifier:obj; 36, tuple; 36, 37; 36, 41; 37, call; 37, 38; 37, 39; 38, identifier:key; 39, argument_list; 39, 40; 40, identifier:obj; 41, subscript; 41, 42; 41, 43; 42, identifier:idxs; 43, identifier:obj
def csort(objs, key): """Order-preserving sorting function.""" idxs = dict((obj, i) for (i, obj) in enumerate(objs)) return sorted(objs, key=lambda obj: (key(obj), idxs[obj]))
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:fast_combine_pairs; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 4, identifier:files; 5, identifier:force_single; 6, identifier:full_name; 7, identifier:separators; 8, block; 8, 9; 8, 11; 8, 18; 8, 28; 8, 42; 8, 53; 8, 60; 8, 61; 8, 88; 8, 89; 8, 107; 8, 108; 9, expression_statement; 9, 10; 10, comment; 11, expression_statement; 11, 12; 12, assignment; 12, 13; 12, 14; 13, identifier:files; 14, call; 14, 15; 14, 16; 15, identifier:sort_filenames; 16, argument_list; 16, 17; 17, identifier:files; 18, expression_statement; 18, 19; 19, assignment; 19, 20; 19, 21; 20, identifier:chunks; 21, call; 21, 22; 21, 25; 22, attribute; 22, 23; 22, 24; 23, identifier:tz; 24, identifier:sliding_window; 25, argument_list; 25, 26; 25, 27; 26, integer:10; 27, identifier:files; 28, expression_statement; 28, 29; 29, assignment; 29, 30; 29, 31; 30, identifier:pairs; 31, list_comprehension; 31, 32; 31, 39; 32, call; 32, 33; 32, 34; 33, identifier:combine_pairs; 34, argument_list; 34, 35; 34, 36; 34, 37; 34, 38; 35, identifier:chunk; 36, identifier:force_single; 37, identifier:full_name; 38, identifier:separators; 39, for_in_clause; 39, 40; 39, 41; 40, identifier:chunk; 41, identifier:chunks; 42, expression_statement; 42, 43; 43, assignment; 43, 44; 43, 45; 44, identifier:pairs; 45, list_comprehension; 45, 46; 45, 47; 45, 50; 46, identifier:y; 47, for_in_clause; 47, 48; 47, 49; 48, identifier:x; 49, identifier:pairs; 50, for_in_clause; 50, 51; 50, 52; 51, identifier:y; 52, identifier:x; 53, expression_statement; 53, 54; 54, assignment; 54, 55; 54, 56; 55, identifier:longest; 56, call; 56, 57; 56, 58; 57, identifier:defaultdict; 58, argument_list; 58, 59; 59, identifier:list; 60, comment; 61, for_statement; 61, 62; 61, 63; 61, 64; 62, identifier:pair; 63, identifier:pairs; 64, block; 64, 65; 65, for_statement; 65, 66; 65, 67; 65, 68; 66, identifier:file; 67, identifier:pair; 68, block; 68, 69; 69, if_statement; 69, 70; 69, 81; 70, comparison_operator:<; 70, 71; 70, 77; 71, call; 71, 72; 71, 73; 72, identifier:len; 73, argument_list; 73, 74; 74, subscript; 74, 75; 74, 76; 75, identifier:longest; 76, identifier:file; 77, call; 77, 78; 77, 79; 78, identifier:len; 79, argument_list; 79, 80; 80, identifier:pair; 81, block; 81, 82; 82, expression_statement; 82, 83; 83, assignment; 83, 84; 83, 87; 84, subscript; 84, 85; 84, 86; 85, identifier:longest; 86, identifier:file; 87, identifier:pair; 88, comment; 89, expression_statement; 89, 90; 90, assignment; 90, 91; 90, 92; 91, identifier:longest; 92, set_comprehension; 92, 93; 92, 100; 93, call; 93, 94; 93, 95; 94, identifier:tuple; 95, argument_list; 95, 96; 96, call; 96, 97; 96, 98; 97, identifier:sort_filenames; 98, argument_list; 98, 99; 99, identifier:x; 100, for_in_clause; 100, 101; 100, 102; 101, identifier:x; 102, call; 102, 103; 102, 106; 103, attribute; 103, 104; 103, 105; 104, identifier:longest; 105, identifier:values; 106, argument_list; 107, comment; 108, return_statement; 108, 109; 109, list_comprehension; 109, 110; 109, 117; 110, call; 110, 111; 110, 112; 111, identifier:sort_filenames; 112, argument_list; 112, 113; 113, call; 113, 114; 113, 115; 114, identifier:list; 115, argument_list; 115, 116; 116, identifier:x; 117, for_in_clause; 117, 118; 117, 119; 118, identifier:x; 119, identifier:longest
def fast_combine_pairs(files, force_single, full_name, separators): """ assume files that need to be paired are within 10 entries of each other, once the list is sorted """ files = sort_filenames(files) chunks = tz.sliding_window(10, files) pairs = [combine_pairs(chunk, force_single, full_name, separators) for chunk in chunks] pairs = [y for x in pairs for y in x] longest = defaultdict(list) # for each file, save the longest pair it is in for pair in pairs: for file in pair: if len(longest[file]) < len(pair): longest[file] = pair # keep only unique pairs longest = {tuple(sort_filenames(x)) for x in longest.values()} # ensure filenames are R1 followed by R2 return [sort_filenames(list(x)) for x in longest]
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:align_bam; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 4, identifier:in_bam; 5, identifier:ref_file; 6, identifier:names; 7, identifier:align_dir; 8, identifier:data; 9, block; 9, 10; 9, 12; 9, 18; 9, 37; 9, 47; 9, 57; 9, 67; 9, 79; 9, 80; 9, 101; 9, 222; 10, expression_statement; 10, 11; 11, comment; 12, expression_statement; 12, 13; 13, assignment; 13, 14; 13, 15; 14, identifier:config; 15, subscript; 15, 16; 15, 17; 16, identifier:data; 17, string:"config"; 18, expression_statement; 18, 19; 19, assignment; 19, 20; 19, 21; 20, identifier:out_file; 21, call; 21, 22; 21, 27; 22, attribute; 22, 23; 22, 26; 23, attribute; 23, 24; 23, 25; 24, identifier:os; 25, identifier:path; 26, identifier:join; 27, argument_list; 27, 28; 27, 29; 28, identifier:align_dir; 29, call; 29, 30; 29, 33; 30, attribute; 30, 31; 30, 32; 31, string:"{0}-sort.bam"; 32, identifier:format; 33, argument_list; 33, 34; 34, subscript; 34, 35; 34, 36; 35, identifier:names; 36, string:"lane"; 37, expression_statement; 37, 38; 38, assignment; 38, 39; 38, 40; 39, identifier:samtools; 40, call; 40, 41; 40, 44; 41, attribute; 41, 42; 41, 43; 42, identifier:config_utils; 43, identifier:get_program; 44, argument_list; 44, 45; 44, 46; 45, string:"samtools"; 46, identifier:config; 47, expression_statement; 47, 48; 48, assignment; 48, 49; 48, 50; 49, identifier:bedtools; 50, call; 50, 51; 50, 54; 51, attribute; 51, 52; 51, 53; 52, identifier:config_utils; 53, identifier:get_program; 54, argument_list; 54, 55; 54, 56; 55, string:"bedtools"; 56, identifier:config; 57, expression_statement; 57, 58; 58, assignment; 58, 59; 58, 60; 59, identifier:resources; 60, call; 60, 61; 60, 64; 61, attribute; 61, 62; 61, 63; 62, identifier:config_utils; 63, identifier:get_resources; 64, argument_list; 64, 65; 64, 66; 65, string:"samtools"; 66, identifier:config; 67, expression_statement; 67, 68; 68, assignment; 68, 69; 68, 70; 69, identifier:num_cores; 70, call; 70, 71; 70, 76; 71, attribute; 71, 72; 71, 75; 72, subscript; 72, 73; 72, 74; 73, identifier:config; 74, string:"algorithm"; 75, identifier:get; 76, argument_list; 76, 77; 76, 78; 77, string:"num_cores"; 78, integer:1; 79, comment; 80, expression_statement; 80, 81; 81, assignment; 81, 82; 81, 83; 82, identifier:max_mem; 83, call; 83, 84; 83, 100; 84, attribute; 84, 85; 84, 99; 85, call; 85, 86; 85, 89; 86, attribute; 86, 87; 86, 88; 87, identifier:config_utils; 88, identifier:adjust_memory; 89, argument_list; 89, 90; 89, 97; 89, 98; 90, call; 90, 91; 90, 94; 91, attribute; 91, 92; 91, 93; 92, identifier:resources; 93, identifier:get; 94, argument_list; 94, 95; 94, 96; 95, string:"memory"; 96, string:"1G"; 97, integer:3; 98, string:"decrease"; 99, identifier:upper; 100, argument_list; 101, if_statement; 101, 102; 101, 109; 102, not_operator; 102, 103; 103, call; 103, 104; 103, 107; 104, attribute; 104, 105; 104, 106; 105, identifier:utils; 106, identifier:file_exists; 107, argument_list; 107, 108; 108, identifier:out_file; 109, block; 109, 110; 110, with_statement; 110, 111; 110, 120; 111, with_clause; 111, 112; 112, with_item; 112, 113; 113, as_pattern; 113, 114; 113, 118; 114, call; 114, 115; 114, 116; 115, identifier:tx_tmpdir; 116, argument_list; 116, 117; 117, identifier:data; 118, as_pattern_target; 118, 119; 119, identifier:work_dir; 120, block; 120, 121; 121, with_statement; 121, 122; 121, 142; 122, with_clause; 122, 123; 123, with_item; 123, 124; 124, as_pattern; 124, 125; 124, 138; 125, call; 125, 126; 125, 129; 126, attribute; 126, 127; 126, 128; 127, identifier:postalign; 128, identifier:tobam_cl; 129, argument_list; 129, 130; 129, 131; 129, 132; 130, identifier:data; 131, identifier:out_file; 132, call; 132, 133; 132, 136; 133, attribute; 133, 134; 133, 135; 134, identifier:bam; 135, identifier:is_paired; 136, argument_list; 136, 137; 137, identifier:in_bam; 138, as_pattern_target; 138, 139; 139, tuple; 139, 140; 139, 141; 140, identifier:tobam_cl; 141, identifier:tx_out_file; 142, block; 142, 143; 142, 153; 142, 166; 142, 172; 142, 181; 142, 195; 143, expression_statement; 143, 144; 144, assignment; 144, 145; 144, 146; 145, identifier:bwa_cmd; 146, call; 146, 147; 146, 148; 147, identifier:_get_bwa_mem_cmd; 148, argument_list; 148, 149; 148, 150; 148, 151; 148, 152; 149, identifier:data; 150, identifier:out_file; 151, identifier:ref_file; 152, string:"-"; 153, expression_statement; 153, 154; 154, assignment; 154, 155; 154, 156; 155, identifier:tx_out_prefix; 156, subscript; 156, 157; 156, 165; 157, call; 157, 158; 157, 163; 158, attribute; 158, 159; 158, 162; 159, attribute; 159, 160; 159, 161; 160, identifier:os; 161, identifier:path; 162, identifier:splitext; 163, argument_list; 163, 164; 164, identifier:tx_out_file; 165, integer:0; 166, expression_statement; 166, 167; 167, assignment; 167, 168; 167, 169; 168, identifier:prefix1; 169, binary_operator:%; 169, 170; 169, 171; 170, string:"%s-in1"; 171, identifier:tx_out_prefix; 172, expression_statement; 172, 173; 173, assignment; 173, 174; 173, 175; 174, identifier:cmd; 175, parenthesized_expression; 175, 176; 176, concatenated_string; 176, 177; 176, 178; 176, 179; 176, 180; 177, string:"unset JAVA_HOME && "; 178, string:"{samtools} sort -n -o -l 1 -@ {num_cores} -m {max_mem} {in_bam} {prefix1} "; 179, string:"| {bedtools} bamtofastq -i /dev/stdin -fq /dev/stdout -fq2 /dev/stdout "; 180, string:"| {bwa_cmd} | "; 181, expression_statement; 181, 182; 182, assignment; 182, 183; 182, 184; 183, identifier:cmd; 184, binary_operator:+; 184, 185; 184, 194; 185, call; 185, 186; 185, 189; 186, attribute; 186, 187; 186, 188; 187, identifier:cmd; 188, identifier:format; 189, argument_list; 189, 190; 190, dictionary_splat; 190, 191; 191, call; 191, 192; 191, 193; 192, identifier:locals; 193, argument_list; 194, identifier:tobam_cl; 195, expression_statement; 195, 196; 196, call; 196, 197; 196, 200; 197, attribute; 197, 198; 197, 199; 198, identifier:do; 199, identifier:run; 200, argument_list; 200, 201; 200, 202; 200, 207; 200, 208; 201, identifier:cmd; 202, binary_operator:%; 202, 203; 202, 204; 203, string:"bwa mem alignment from BAM: %s"; 204, subscript; 204, 205; 204, 206; 205, identifier:names; 206, string:"sample"; 207, None; 208, list:[do.file_nonempty(tx_out_file), do.file_reasonable_size(tx_out_file, in_bam)]; 208, 209; 208, 215; 209, call; 209, 210; 209, 213; 210, attribute; 210, 211; 210, 212; 211, identifier:do; 212, identifier:file_nonempty; 213, argument_list; 213, 214; 214, identifier:tx_out_file; 215, call; 215, 216; 215, 219; 216, attribute; 216, 217; 216, 218; 217, identifier:do; 218, identifier:file_reasonable_size; 219, argument_list; 219, 220; 219, 221; 220, identifier:tx_out_file; 221, identifier:in_bam; 222, return_statement; 222, 223; 223, identifier:out_file
def align_bam(in_bam, ref_file, names, align_dir, data): """Perform direct alignment of an input BAM file with BWA using pipes. This avoids disk IO by piping between processes: - samtools sort of input BAM to queryname - bedtools conversion to interleaved FASTQ - bwa-mem alignment - samtools conversion to BAM - samtools sort to coordinate """ config = data["config"] out_file = os.path.join(align_dir, "{0}-sort.bam".format(names["lane"])) samtools = config_utils.get_program("samtools", config) bedtools = config_utils.get_program("bedtools", config) resources = config_utils.get_resources("samtools", config) num_cores = config["algorithm"].get("num_cores", 1) # adjust memory for samtools since used for input and output max_mem = config_utils.adjust_memory(resources.get("memory", "1G"), 3, "decrease").upper() if not utils.file_exists(out_file): with tx_tmpdir(data) as work_dir: with postalign.tobam_cl(data, out_file, bam.is_paired(in_bam)) as (tobam_cl, tx_out_file): bwa_cmd = _get_bwa_mem_cmd(data, out_file, ref_file, "-") tx_out_prefix = os.path.splitext(tx_out_file)[0] prefix1 = "%s-in1" % tx_out_prefix cmd = ("unset JAVA_HOME && " "{samtools} sort -n -o -l 1 -@ {num_cores} -m {max_mem} {in_bam} {prefix1} " "| {bedtools} bamtofastq -i /dev/stdin -fq /dev/stdout -fq2 /dev/stdout " "| {bwa_cmd} | ") cmd = cmd.format(**locals()) + tobam_cl do.run(cmd, "bwa mem alignment from BAM: %s" % names["sample"], None, [do.file_nonempty(tx_out_file), do.file_reasonable_size(tx_out_file, in_bam)]) return out_file
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:_combine_regions; 3, parameters; 3, 4; 3, 5; 4, identifier:all_regions; 5, identifier:ref_regions; 6, block; 6, 7; 6, 9; 6, 13; 6, 30; 6, 49; 6, 53; 6, 83; 6, 92; 6, 108; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:chrom_order; 12, dictionary; 13, for_statement; 13, 14; 13, 17; 13, 21; 14, pattern_list; 14, 15; 14, 16; 15, identifier:i; 16, identifier:x; 17, call; 17, 18; 17, 19; 18, identifier:enumerate; 19, argument_list; 19, 20; 20, identifier:ref_regions; 21, block; 21, 22; 22, expression_statement; 22, 23; 23, assignment; 23, 24; 23, 29; 24, subscript; 24, 25; 24, 26; 25, identifier:chrom_order; 26, attribute; 26, 27; 26, 28; 27, identifier:x; 28, identifier:chrom; 29, identifier:i; 30, function_definition; 30, 31; 30, 32; 30, 34; 31, function_name:wchrom_key; 32, parameters; 32, 33; 33, identifier:x; 34, block; 34, 35; 34, 42; 35, expression_statement; 35, 36; 36, assignment; 36, 37; 36, 41; 37, pattern_list; 37, 38; 37, 39; 37, 40; 38, identifier:chrom; 39, identifier:start; 40, identifier:end; 41, identifier:x; 42, return_statement; 42, 43; 43, tuple; 43, 44; 43, 47; 43, 48; 44, subscript; 44, 45; 44, 46; 45, identifier:chrom_order; 46, identifier:chrom; 47, identifier:start; 48, identifier:end; 49, expression_statement; 49, 50; 50, assignment; 50, 51; 50, 52; 51, identifier:all_intervals; 52, list:[]; 53, for_statement; 53, 54; 53, 55; 53, 56; 54, identifier:region_group; 55, identifier:all_regions; 56, block; 56, 57; 57, for_statement; 57, 58; 57, 59; 57, 60; 58, identifier:region; 59, identifier:region_group; 60, block; 60, 61; 61, expression_statement; 61, 62; 62, call; 62, 63; 62, 66; 63, attribute; 63, 64; 63, 65; 64, identifier:all_intervals; 65, identifier:append; 66, argument_list; 66, 67; 67, tuple; 67, 68; 67, 71; 67, 77; 68, attribute; 68, 69; 68, 70; 69, identifier:region; 70, identifier:chrom; 71, call; 71, 72; 71, 73; 72, identifier:int; 73, argument_list; 73, 74; 74, attribute; 74, 75; 74, 76; 75, identifier:region; 76, identifier:start; 77, call; 77, 78; 77, 79; 78, identifier:int; 79, argument_list; 79, 80; 80, attribute; 80, 81; 80, 82; 81, identifier:region; 82, identifier:stop; 83, expression_statement; 83, 84; 84, call; 84, 85; 84, 88; 85, attribute; 85, 86; 85, 87; 86, identifier:all_intervals; 87, identifier:sort; 88, argument_list; 88, 89; 89, keyword_argument; 89, 90; 89, 91; 90, identifier:key; 91, identifier:wchrom_key; 92, expression_statement; 92, 93; 93, assignment; 93, 94; 93, 95; 94, identifier:bed_lines; 95, list_comprehension; 95, 96; 95, 102; 96, binary_operator:%; 96, 97; 96, 98; 97, string:"%s\t%s\t%s"; 98, tuple; 98, 99; 98, 100; 98, 101; 99, identifier:c; 100, identifier:s; 101, identifier:e; 102, for_in_clause; 102, 103; 102, 107; 103, tuple_pattern; 103, 104; 103, 105; 103, 106; 104, identifier:c; 105, identifier:s; 106, identifier:e; 107, identifier:all_intervals; 108, return_statement; 108, 109; 109, call; 109, 110; 109, 113; 110, attribute; 110, 111; 110, 112; 111, identifier:pybedtools; 112, identifier:BedTool; 113, argument_list; 113, 114; 113, 120; 114, call; 114, 115; 114, 118; 115, attribute; 115, 116; 115, 117; 116, string:"\n"; 117, identifier:join; 118, argument_list; 118, 119; 119, identifier:bed_lines; 120, keyword_argument; 120, 121; 120, 122; 121, identifier:from_string; 122, True
def _combine_regions(all_regions, ref_regions): """Combine multiple BEDtools regions of regions into sorted final BEDtool. """ chrom_order = {} for i, x in enumerate(ref_regions): chrom_order[x.chrom] = i def wchrom_key(x): chrom, start, end = x return (chrom_order[chrom], start, end) all_intervals = [] for region_group in all_regions: for region in region_group: all_intervals.append((region.chrom, int(region.start), int(region.stop))) all_intervals.sort(key=wchrom_key) bed_lines = ["%s\t%s\t%s" % (c, s, e) for (c, s, e) in all_intervals] return pybedtools.BedTool("\n".join(bed_lines), from_string=True)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 11; 2, function_name:_add_meta; 3, parameters; 3, 4; 3, 5; 3, 8; 4, identifier:xs; 5, default_parameter; 5, 6; 5, 7; 6, identifier:sample; 7, None; 8, default_parameter; 8, 9; 8, 10; 9, identifier:config; 10, None; 11, block; 11, 12; 11, 14; 11, 18; 11, 155; 12, expression_statement; 12, 13; 13, comment; 14, expression_statement; 14, 15; 15, assignment; 15, 16; 15, 17; 16, identifier:out; 17, list:[]; 18, for_statement; 18, 19; 18, 20; 18, 21; 19, identifier:x; 20, identifier:xs; 21, block; 21, 22; 21, 53; 21, 66; 21, 102; 21, 148; 22, if_statement; 22, 23; 22, 45; 23, boolean_operator:or; 23, 24; 23, 34; 24, not_operator; 24, 25; 25, call; 25, 26; 25, 27; 26, identifier:isinstance; 27, argument_list; 27, 28; 27, 31; 28, subscript; 28, 29; 28, 30; 29, identifier:x; 30, string:"path"; 31, attribute; 31, 32; 31, 33; 32, identifier:six; 33, identifier:string_types; 34, not_operator; 34, 35; 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:exists; 41, argument_list; 41, 42; 42, subscript; 42, 43; 42, 44; 43, identifier:x; 44, string:"path"; 45, block; 45, 46; 46, raise_statement; 46, 47; 47, call; 47, 48; 47, 49; 48, identifier:ValueError; 49, argument_list; 49, 50; 50, binary_operator:%; 50, 51; 50, 52; 51, string:"Unexpected path for upload: %s"; 52, identifier:x; 53, expression_statement; 53, 54; 54, assignment; 54, 55; 54, 58; 55, subscript; 55, 56; 55, 57; 56, identifier:x; 57, string:"mtime"; 58, call; 58, 59; 58, 62; 59, attribute; 59, 60; 59, 61; 60, identifier:shared; 61, identifier:get_file_timestamp; 62, argument_list; 62, 63; 63, subscript; 63, 64; 63, 65; 64, identifier:x; 65, string:"path"; 66, if_statement; 66, 67; 66, 68; 67, identifier:sample; 68, block; 68, 69; 68, 78; 69, expression_statement; 69, 70; 70, assignment; 70, 71; 70, 72; 71, identifier:sample_name; 72, call; 72, 73; 72, 76; 73, attribute; 73, 74; 73, 75; 74, identifier:dd; 75, identifier:get_sample_name; 76, argument_list; 76, 77; 77, identifier:sample; 78, if_statement; 78, 79; 78, 82; 78, 89; 79, comparison_operator:not; 79, 80; 79, 81; 80, string:"sample"; 81, identifier:x; 82, block; 82, 83; 83, expression_statement; 83, 84; 84, assignment; 84, 85; 84, 88; 85, subscript; 85, 86; 85, 87; 86, identifier:x; 87, string:"sample"; 88, identifier:sample_name; 89, elif_clause; 89, 90; 89, 95; 90, comparison_operator:!=; 90, 91; 90, 94; 91, subscript; 91, 92; 91, 93; 92, identifier:x; 93, string:"sample"; 94, identifier:sample_name; 95, block; 95, 96; 96, expression_statement; 96, 97; 97, assignment; 97, 98; 97, 101; 98, subscript; 98, 99; 98, 100; 99, identifier:x; 100, string:"run"; 101, identifier:sample_name; 102, if_statement; 102, 103; 102, 104; 103, identifier:config; 104, block; 104, 105; 104, 116; 104, 138; 105, expression_statement; 105, 106; 106, assignment; 106, 107; 106, 108; 107, identifier:fc_name; 108, boolean_operator:or; 108, 109; 108, 115; 109, call; 109, 110; 109, 113; 110, attribute; 110, 111; 110, 112; 111, identifier:config; 112, identifier:get; 113, argument_list; 113, 114; 114, string:"fc_name"; 115, string:"project"; 116, expression_statement; 116, 117; 117, assignment; 117, 118; 117, 119; 118, identifier:fc_date; 119, boolean_operator:or; 119, 120; 119, 126; 120, call; 120, 121; 120, 124; 121, attribute; 121, 122; 121, 123; 122, identifier:config; 123, identifier:get; 124, argument_list; 124, 125; 125, string:"fc_date"; 126, call; 126, 127; 126, 136; 127, attribute; 127, 128; 127, 135; 128, call; 128, 129; 128, 134; 129, attribute; 129, 130; 129, 133; 130, attribute; 130, 131; 130, 132; 131, identifier:datetime; 132, identifier:datetime; 133, identifier:now; 134, argument_list; 135, identifier:strftime; 136, argument_list; 136, 137; 137, string:"%Y-%m-%d"; 138, expression_statement; 138, 139; 139, assignment; 139, 140; 139, 143; 140, subscript; 140, 141; 140, 142; 141, identifier:x; 142, string:"run"; 143, binary_operator:%; 143, 144; 143, 145; 144, string:"%s_%s"; 145, tuple; 145, 146; 145, 147; 146, identifier:fc_date; 147, identifier:fc_name; 148, expression_statement; 148, 149; 149, call; 149, 150; 149, 153; 150, attribute; 150, 151; 150, 152; 151, identifier:out; 152, identifier:append; 153, argument_list; 153, 154; 154, identifier:x; 155, return_statement; 155, 156; 156, identifier:out
def _add_meta(xs, sample=None, config=None): """Add top level information about the sample or flowcell to output. Sorts outputs into sample names (sample input) and project (config input). """ out = [] for x in xs: if not isinstance(x["path"], six.string_types) or not os.path.exists(x["path"]): raise ValueError("Unexpected path for upload: %s" % x) x["mtime"] = shared.get_file_timestamp(x["path"]) if sample: sample_name = dd.get_sample_name(sample) if "sample" not in x: x["sample"] = sample_name elif x["sample"] != sample_name: x["run"] = sample_name if config: fc_name = config.get("fc_name") or "project" fc_date = config.get("fc_date") or datetime.datetime.now().strftime("%Y-%m-%d") x["run"] = "%s_%s" % (fc_date, fc_name) out.append(x) return out
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 12; 2, function_name:report; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 3, 9; 3, 10; 3, 11; 4, identifier:self; 5, identifier:align_bam; 6, identifier:ref_file; 7, identifier:is_paired; 8, identifier:bait_file; 9, identifier:target_file; 10, identifier:variant_region_file; 11, identifier:config; 12, block; 12, 13; 12, 15; 12, 24; 12, 34; 12, 35; 12, 36; 12, 40; 12, 50; 12, 64; 12, 148; 12, 157; 12, 172; 12, 176; 12, 197; 12, 218; 13, expression_statement; 13, 14; 14, comment; 15, expression_statement; 15, 16; 16, assignment; 16, 17; 16, 18; 17, identifier:dup_metrics; 18, call; 18, 19; 18, 22; 19, attribute; 19, 20; 19, 21; 20, identifier:self; 21, identifier:_get_current_dup_metrics; 22, argument_list; 22, 23; 23, identifier:align_bam; 24, expression_statement; 24, 25; 25, assignment; 25, 26; 25, 27; 26, identifier:align_metrics; 27, call; 27, 28; 27, 31; 28, attribute; 28, 29; 28, 30; 29, identifier:self; 30, identifier:_collect_align_metrics; 31, argument_list; 31, 32; 31, 33; 32, identifier:align_bam; 33, identifier:ref_file; 34, comment; 35, comment; 36, expression_statement; 36, 37; 37, assignment; 37, 38; 37, 39; 38, identifier:gc_graph; 39, None; 40, expression_statement; 40, 41; 41, assignment; 41, 42; 41, 46; 42, pattern_list; 42, 43; 42, 44; 42, 45; 43, identifier:insert_graph; 44, identifier:insert_metrics; 45, identifier:hybrid_metrics; 46, tuple; 46, 47; 46, 48; 46, 49; 47, None; 48, None; 49, None; 50, if_statement; 50, 51; 50, 52; 51, identifier:is_paired; 52, block; 52, 53; 53, expression_statement; 53, 54; 54, assignment; 54, 55; 54, 58; 55, pattern_list; 55, 56; 55, 57; 56, identifier:insert_graph; 57, identifier:insert_metrics; 58, call; 58, 59; 58, 62; 59, attribute; 59, 60; 59, 61; 60, identifier:self; 61, identifier:_insert_sizes; 62, argument_list; 62, 63; 63, identifier:align_bam; 64, if_statement; 64, 65; 64, 68; 64, 104; 65, boolean_operator:and; 65, 66; 65, 67; 66, identifier:bait_file; 67, identifier:target_file; 68, block; 68, 69; 68, 81; 68, 93; 69, assert_statement; 69, 70; 69, 78; 70, call; 70, 71; 70, 76; 71, attribute; 71, 72; 71, 75; 72, attribute; 72, 73; 72, 74; 73, identifier:os; 74, identifier:path; 75, identifier:exists; 76, argument_list; 76, 77; 77, identifier:bait_file; 78, tuple; 78, 79; 78, 80; 79, identifier:bait_file; 80, string:"does not exist!"; 81, assert_statement; 81, 82; 81, 90; 82, call; 82, 83; 82, 88; 83, attribute; 83, 84; 83, 87; 84, attribute; 84, 85; 84, 86; 85, identifier:os; 86, identifier:path; 87, identifier:exists; 88, argument_list; 88, 89; 89, identifier:target_file; 90, tuple; 90, 91; 90, 92; 91, identifier:target_file; 92, string:"does not exist!"; 93, expression_statement; 93, 94; 94, assignment; 94, 95; 94, 96; 95, identifier:hybrid_metrics; 96, call; 96, 97; 96, 100; 97, attribute; 97, 98; 97, 99; 98, identifier:self; 99, identifier:_hybrid_select_metrics; 100, argument_list; 100, 101; 100, 102; 100, 103; 101, identifier:align_bam; 102, identifier:bait_file; 103, identifier:target_file; 104, elif_clause; 104, 105; 104, 124; 105, parenthesized_expression; 105, 106; 106, boolean_operator:and; 106, 107; 106, 108; 107, identifier:variant_region_file; 108, comparison_operator:in; 108, 109; 108, 122; 109, call; 109, 110; 109, 121; 110, attribute; 110, 111; 110, 120; 111, call; 111, 112; 111, 117; 112, attribute; 112, 113; 112, 116; 113, subscript; 113, 114; 113, 115; 114, identifier:config; 115, string:"algorithm"; 116, identifier:get; 117, argument_list; 117, 118; 117, 119; 118, string:"coverage_interval"; 119, string:""; 120, identifier:lower; 121, argument_list; 122, list:["exome"]; 122, 123; 123, string:"exome"; 124, block; 124, 125; 124, 137; 125, assert_statement; 125, 126; 125, 134; 126, call; 126, 127; 126, 132; 127, attribute; 127, 128; 127, 131; 128, attribute; 128, 129; 128, 130; 129, identifier:os; 130, identifier:path; 131, identifier:exists; 132, argument_list; 132, 133; 133, identifier:variant_region_file; 134, tuple; 134, 135; 134, 136; 135, identifier:variant_region_file; 136, string:"does not exist"; 137, expression_statement; 137, 138; 138, assignment; 138, 139; 138, 140; 139, identifier:hybrid_metrics; 140, call; 140, 141; 140, 144; 141, attribute; 141, 142; 141, 143; 142, identifier:self; 143, identifier:_hybrid_select_metrics; 144, argument_list; 144, 145; 144, 146; 144, 147; 145, identifier:align_bam; 146, identifier:variant_region_file; 147, identifier:variant_region_file; 148, expression_statement; 148, 149; 149, assignment; 149, 150; 149, 151; 150, identifier:vrn_vals; 151, call; 151, 152; 151, 155; 152, attribute; 152, 153; 152, 154; 153, identifier:self; 154, identifier:_variant_eval_metrics; 155, argument_list; 155, 156; 156, identifier:align_bam; 157, expression_statement; 157, 158; 158, assignment; 158, 159; 158, 160; 159, identifier:summary_info; 160, call; 160, 161; 160, 166; 161, attribute; 161, 162; 161, 165; 162, attribute; 162, 163; 162, 164; 163, identifier:self; 164, identifier:_parser; 165, identifier:get_summary_metrics; 166, argument_list; 166, 167; 166, 168; 166, 169; 166, 170; 166, 171; 167, identifier:align_metrics; 168, identifier:dup_metrics; 169, identifier:insert_metrics; 170, identifier:hybrid_metrics; 171, identifier:vrn_vals; 172, expression_statement; 172, 173; 173, assignment; 173, 174; 173, 175; 174, identifier:graphs; 175, list:[]; 176, if_statement; 176, 177; 176, 187; 177, boolean_operator:and; 177, 178; 177, 179; 178, identifier:gc_graph; 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:exists; 185, argument_list; 185, 186; 186, identifier:gc_graph; 187, block; 187, 188; 188, expression_statement; 188, 189; 189, call; 189, 190; 189, 193; 190, attribute; 190, 191; 190, 192; 191, identifier:graphs; 192, identifier:append; 193, argument_list; 193, 194; 194, tuple; 194, 195; 194, 196; 195, identifier:gc_graph; 196, string:"Distribution of GC content across reads"; 197, if_statement; 197, 198; 197, 208; 198, boolean_operator:and; 198, 199; 198, 200; 199, identifier:insert_graph; 200, call; 200, 201; 200, 206; 201, attribute; 201, 202; 201, 205; 202, attribute; 202, 203; 202, 204; 203, identifier:os; 204, identifier:path; 205, identifier:exists; 206, argument_list; 206, 207; 207, identifier:insert_graph; 208, block; 208, 209; 209, expression_statement; 209, 210; 210, call; 210, 211; 210, 214; 211, attribute; 211, 212; 211, 213; 212, identifier:graphs; 213, identifier:append; 214, argument_list; 214, 215; 215, tuple; 215, 216; 215, 217; 216, identifier:insert_graph; 217, string:"Distribution of paired end insert sizes"; 218, return_statement; 218, 219; 219, expression_list; 219, 220; 219, 221; 220, identifier:summary_info; 221, identifier:graphs
def report(self, align_bam, ref_file, is_paired, bait_file, target_file, variant_region_file, config): """Produce report metrics using Picard with sorted aligned BAM file. """ dup_metrics = self._get_current_dup_metrics(align_bam) align_metrics = self._collect_align_metrics(align_bam, ref_file) # Prefer the GC metrics in FastQC instead of Picard # gc_graph, gc_metrics = self._gc_bias(align_bam, ref_file) gc_graph = None insert_graph, insert_metrics, hybrid_metrics = (None, None, None) if is_paired: insert_graph, insert_metrics = self._insert_sizes(align_bam) if bait_file and target_file: assert os.path.exists(bait_file), (bait_file, "does not exist!") assert os.path.exists(target_file), (target_file, "does not exist!") hybrid_metrics = self._hybrid_select_metrics(align_bam, bait_file, target_file) elif (variant_region_file and config["algorithm"].get("coverage_interval", "").lower() in ["exome"]): assert os.path.exists(variant_region_file), (variant_region_file, "does not exist") hybrid_metrics = self._hybrid_select_metrics( align_bam, variant_region_file, variant_region_file) vrn_vals = self._variant_eval_metrics(align_bam) summary_info = self._parser.get_summary_metrics(align_metrics, dup_metrics, insert_metrics, hybrid_metrics, vrn_vals) graphs = [] if gc_graph and os.path.exists(gc_graph): graphs.append((gc_graph, "Distribution of GC content across reads")) if insert_graph and os.path.exists(insert_graph): graphs.append((insert_graph, "Distribution of paired end insert sizes")) return summary_info, graphs
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 14; 2, function_name:report; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 3, 11; 4, identifier:self; 5, identifier:align_bam; 6, identifier:ref_file; 7, identifier:gtf_file; 8, default_parameter; 8, 9; 8, 10; 9, identifier:is_paired; 10, False; 11, default_parameter; 11, 12; 11, 13; 12, identifier:rrna_file; 13, string:"null"; 14, block; 14, 15; 14, 17; 14, 18; 14, 27; 14, 37; 14, 45; 14, 59; 14, 70; 14, 88; 14, 92; 14, 109; 15, expression_statement; 15, 16; 16, comment; 17, comment; 18, expression_statement; 18, 19; 19, assignment; 19, 20; 19, 21; 20, identifier:dup_metrics; 21, call; 21, 22; 21, 25; 22, attribute; 22, 23; 22, 24; 23, identifier:self; 24, identifier:_get_current_dup_metrics; 25, argument_list; 25, 26; 26, identifier:align_bam; 27, expression_statement; 27, 28; 28, assignment; 28, 29; 28, 30; 29, identifier:align_metrics; 30, call; 30, 31; 30, 34; 31, attribute; 31, 32; 31, 33; 32, identifier:self; 33, identifier:_collect_align_metrics; 34, argument_list; 34, 35; 34, 36; 35, identifier:align_bam; 36, identifier:ref_file; 37, expression_statement; 37, 38; 38, assignment; 38, 39; 38, 42; 39, pattern_list; 39, 40; 39, 41; 40, identifier:insert_graph; 41, identifier:insert_metrics; 42, tuple; 42, 43; 42, 44; 43, None; 44, None; 45, if_statement; 45, 46; 45, 47; 46, identifier:is_paired; 47, block; 47, 48; 48, expression_statement; 48, 49; 49, assignment; 49, 50; 49, 53; 50, pattern_list; 50, 51; 50, 52; 51, identifier:insert_graph; 52, identifier:insert_metrics; 53, call; 53, 54; 53, 57; 54, attribute; 54, 55; 54, 56; 55, identifier:self; 56, identifier:_insert_sizes; 57, argument_list; 57, 58; 58, identifier:align_bam; 59, expression_statement; 59, 60; 60, assignment; 60, 61; 60, 62; 61, identifier:rnaseq_metrics; 62, call; 62, 63; 62, 66; 63, attribute; 63, 64; 63, 65; 64, identifier:self; 65, identifier:_rnaseq_metrics; 66, argument_list; 66, 67; 66, 68; 66, 69; 67, identifier:align_bam; 68, identifier:gtf_file; 69, identifier:rrna_file; 70, expression_statement; 70, 71; 71, assignment; 71, 72; 71, 73; 72, identifier:summary_info; 73, call; 73, 74; 73, 79; 74, attribute; 74, 75; 74, 78; 75, attribute; 75, 76; 75, 77; 76, identifier:self; 77, identifier:_parser; 78, identifier:get_summary_metrics; 79, argument_list; 79, 80; 79, 81; 79, 82; 79, 85; 80, identifier:align_metrics; 81, identifier:dup_metrics; 82, keyword_argument; 82, 83; 82, 84; 83, identifier:insert_metrics; 84, identifier:insert_metrics; 85, keyword_argument; 85, 86; 85, 87; 86, identifier:rnaseq_metrics; 87, identifier:rnaseq_metrics; 88, expression_statement; 88, 89; 89, assignment; 89, 90; 89, 91; 90, identifier:graphs; 91, list:[]; 92, if_statement; 92, 93; 92, 99; 93, boolean_operator:and; 93, 94; 93, 95; 94, identifier:insert_graph; 95, call; 95, 96; 95, 97; 96, identifier:file_exists; 97, argument_list; 97, 98; 98, identifier:insert_graph; 99, block; 99, 100; 100, expression_statement; 100, 101; 101, call; 101, 102; 101, 105; 102, attribute; 102, 103; 102, 104; 103, identifier:graphs; 104, identifier:append; 105, argument_list; 105, 106; 106, tuple; 106, 107; 106, 108; 107, identifier:insert_graph; 108, string:"Distribution of paired end insert sizes"; 109, return_statement; 109, 110; 110, expression_list; 110, 111; 110, 112; 111, identifier:summary_info; 112, identifier:graphs
def report(self, align_bam, ref_file, gtf_file, is_paired=False, rrna_file="null"): """Produce report metrics for a RNASeq experiment using Picard with a sorted aligned BAM file. """ # collect duplication metrics dup_metrics = self._get_current_dup_metrics(align_bam) align_metrics = self._collect_align_metrics(align_bam, ref_file) insert_graph, insert_metrics = (None, None) if is_paired: insert_graph, insert_metrics = self._insert_sizes(align_bam) rnaseq_metrics = self._rnaseq_metrics(align_bam, gtf_file, rrna_file) summary_info = self._parser.get_summary_metrics(align_metrics, dup_metrics, insert_metrics=insert_metrics, rnaseq_metrics=rnaseq_metrics) graphs = [] if insert_graph and file_exists(insert_graph): graphs.append((insert_graph, "Distribution of paired end insert sizes")) return summary_info, graphs
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:apply_recal; 3, parameters; 3, 4; 4, identifier:data; 5, block; 5, 6; 5, 8; 5, 24; 5, 30; 5, 143; 5, 144; 5, 166; 5, 203; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:orig_bam; 11, boolean_operator:or; 11, 12; 11, 18; 12, call; 12, 13; 12, 16; 13, attribute; 13, 14; 13, 15; 14, identifier:dd; 15, identifier:get_align_bam; 16, argument_list; 16, 17; 17, identifier:data; 18, call; 18, 19; 18, 22; 19, attribute; 19, 20; 19, 21; 20, identifier:dd; 21, identifier:get_work_bam; 22, argument_list; 22, 23; 23, identifier:data; 24, expression_statement; 24, 25; 25, assignment; 25, 26; 25, 27; 26, identifier:had_work_bam; 27, comparison_operator:in; 27, 28; 27, 29; 28, string:"work_bam"; 29, identifier:data; 30, if_statement; 30, 31; 30, 41; 30, 76; 30, 122; 31, comparison_operator:in; 31, 32; 31, 38; 32, call; 32, 33; 32, 36; 33, attribute; 33, 34; 33, 35; 34, identifier:dd; 35, identifier:get_recalibrate; 36, argument_list; 36, 37; 37, identifier:data; 38, list:[True, "gatk"]; 38, 39; 38, 40; 39, True; 40, string:"gatk"; 41, block; 41, 42; 42, if_statement; 42, 43; 42, 49; 43, call; 43, 44; 43, 47; 44, attribute; 44, 45; 44, 46; 45, identifier:data; 46, identifier:get; 47, argument_list; 47, 48; 48, string:"prep_recal"; 49, block; 49, 50; 49, 67; 50, expression_statement; 50, 51; 51, call; 51, 52; 51, 55; 52, attribute; 52, 53; 52, 54; 53, identifier:logger; 54, identifier:info; 55, argument_list; 55, 56; 56, binary_operator:%; 56, 57; 56, 58; 57, string:"Applying BQSR recalibration with GATK: %s "; 58, call; 58, 59; 58, 60; 59, identifier:str; 60, argument_list; 60, 61; 61, call; 61, 62; 61, 65; 62, attribute; 62, 63; 62, 64; 63, identifier:dd; 64, identifier:get_sample_name; 65, argument_list; 65, 66; 66, identifier:data; 67, expression_statement; 67, 68; 68, assignment; 68, 69; 68, 72; 69, subscript; 69, 70; 69, 71; 70, identifier:data; 71, string:"work_bam"; 72, call; 72, 73; 72, 74; 73, identifier:_gatk_apply_bqsr; 74, argument_list; 74, 75; 75, identifier:data; 76, elif_clause; 76, 77; 76, 85; 77, comparison_operator:==; 77, 78; 77, 84; 78, call; 78, 79; 78, 82; 79, attribute; 79, 80; 79, 81; 80, identifier:dd; 81, identifier:get_recalibrate; 82, argument_list; 82, 83; 83, identifier:data; 84, string:"sentieon"; 85, block; 85, 86; 86, if_statement; 86, 87; 86, 93; 87, call; 87, 88; 87, 91; 88, attribute; 88, 89; 88, 90; 89, identifier:data; 90, identifier:get; 91, argument_list; 91, 92; 92, string:"prep_recal"; 93, block; 93, 94; 93, 111; 94, expression_statement; 94, 95; 95, call; 95, 96; 95, 99; 96, attribute; 96, 97; 96, 98; 97, identifier:logger; 98, identifier:info; 99, argument_list; 99, 100; 100, binary_operator:%; 100, 101; 100, 102; 101, string:"Applying BQSR recalibration with sentieon: %s "; 102, call; 102, 103; 102, 104; 103, identifier:str; 104, argument_list; 104, 105; 105, call; 105, 106; 105, 109; 106, attribute; 106, 107; 106, 108; 107, identifier:dd; 108, identifier:get_sample_name; 109, argument_list; 109, 110; 110, identifier:data; 111, expression_statement; 111, 112; 112, assignment; 112, 113; 112, 116; 113, subscript; 113, 114; 113, 115; 114, identifier:data; 115, string:"work_bam"; 116, call; 116, 117; 116, 120; 117, attribute; 117, 118; 117, 119; 118, identifier:sentieon; 119, identifier:apply_bqsr; 120, argument_list; 120, 121; 121, identifier:data; 122, elif_clause; 122, 123; 122, 129; 123, call; 123, 124; 123, 127; 124, attribute; 124, 125; 124, 126; 125, identifier:dd; 126, identifier:get_recalibrate; 127, argument_list; 127, 128; 128, identifier:data; 129, block; 129, 130; 130, raise_statement; 130, 131; 131, call; 131, 132; 131, 133; 132, identifier:NotImplementedError; 133, argument_list; 133, 134; 134, binary_operator:%; 134, 135; 134, 136; 135, string:"Unsupported recalibration type: %s"; 136, parenthesized_expression; 136, 137; 137, call; 137, 138; 137, 141; 138, attribute; 138, 139; 138, 140; 139, identifier:dd; 140, identifier:get_recalibrate; 141, argument_list; 141, 142; 142, identifier:data; 143, comment; 144, if_statement; 144, 145; 144, 154; 145, boolean_operator:and; 145, 146; 145, 148; 146, not_operator; 146, 147; 147, identifier:had_work_bam; 148, call; 148, 149; 148, 152; 149, attribute; 149, 150; 149, 151; 150, identifier:dd; 151, identifier:get_work_bam; 152, argument_list; 152, 153; 153, identifier:data; 154, block; 154, 155; 155, expression_statement; 155, 156; 156, assignment; 156, 157; 156, 160; 157, subscript; 157, 158; 157, 159; 158, identifier:data; 159, string:"align_bam"; 160, call; 160, 161; 160, 164; 161, attribute; 161, 162; 161, 163; 162, identifier:dd; 163, identifier:get_work_bam; 164, argument_list; 164, 165; 165, identifier:data; 166, if_statement; 166, 167; 166, 184; 167, boolean_operator:and; 167, 168; 167, 176; 168, comparison_operator:!=; 168, 169; 168, 170; 169, identifier:orig_bam; 170, call; 170, 171; 170, 174; 171, attribute; 171, 172; 171, 173; 172, identifier:dd; 173, identifier:get_work_bam; 174, argument_list; 174, 175; 175, identifier:data; 176, comparison_operator:!=; 176, 177; 176, 178; 177, identifier:orig_bam; 178, call; 178, 179; 178, 182; 179, attribute; 179, 180; 179, 181; 180, identifier:dd; 181, identifier:get_align_bam; 182, argument_list; 182, 183; 183, identifier:data; 184, block; 184, 185; 185, expression_statement; 185, 186; 186, call; 186, 187; 186, 190; 187, attribute; 187, 188; 187, 189; 188, identifier:utils; 189, identifier:save_diskspace; 190, argument_list; 190, 191; 190, 192; 190, 200; 191, identifier:orig_bam; 192, binary_operator:%; 192, 193; 192, 194; 193, string:"BAM recalibrated to %s"; 194, call; 194, 195; 194, 198; 195, attribute; 195, 196; 195, 197; 196, identifier:dd; 197, identifier:get_work_bam; 198, argument_list; 198, 199; 199, identifier:data; 200, subscript; 200, 201; 200, 202; 201, identifier:data; 202, string:"config"; 203, return_statement; 203, 204; 204, identifier:data
def apply_recal(data): """Apply recalibration tables to the sorted aligned BAM, producing recalibrated BAM. """ orig_bam = dd.get_align_bam(data) or dd.get_work_bam(data) had_work_bam = "work_bam" in data if dd.get_recalibrate(data) in [True, "gatk"]: if data.get("prep_recal"): logger.info("Applying BQSR recalibration with GATK: %s " % str(dd.get_sample_name(data))) data["work_bam"] = _gatk_apply_bqsr(data) elif dd.get_recalibrate(data) == "sentieon": if data.get("prep_recal"): logger.info("Applying BQSR recalibration with sentieon: %s " % str(dd.get_sample_name(data))) data["work_bam"] = sentieon.apply_bqsr(data) elif dd.get_recalibrate(data): raise NotImplementedError("Unsupported recalibration type: %s" % (dd.get_recalibrate(data))) # CWL does not have work/alignment BAM separation if not had_work_bam and dd.get_work_bam(data): data["align_bam"] = dd.get_work_bam(data) if orig_bam != dd.get_work_bam(data) and orig_bam != dd.get_align_bam(data): utils.save_diskspace(orig_bam, "BAM recalibrated to %s" % dd.get_work_bam(data), data["config"]) return data
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:_prep_callable_bed; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 4, identifier:in_file; 5, identifier:work_dir; 6, identifier:stats; 7, identifier:data; 8, block; 8, 9; 8, 11; 8, 39; 8, 49; 8, 108; 9, expression_statement; 9, 10; 10, comment; 11, expression_statement; 11, 12; 12, assignment; 12, 13; 12, 14; 13, identifier:out_file; 14, call; 14, 15; 14, 20; 15, attribute; 15, 16; 15, 19; 16, attribute; 16, 17; 16, 18; 17, identifier:os; 18, identifier:path; 19, identifier:join; 20, argument_list; 20, 21; 20, 22; 21, identifier:work_dir; 22, binary_operator:%; 22, 23; 22, 24; 23, string:"%s-merge.bed.gz"; 24, subscript; 24, 25; 24, 38; 25, call; 25, 26; 25, 29; 26, attribute; 26, 27; 26, 28; 27, identifier:utils; 28, identifier:splitext_plus; 29, argument_list; 29, 30; 30, call; 30, 31; 30, 36; 31, attribute; 31, 32; 31, 35; 32, attribute; 32, 33; 32, 34; 33, identifier:os; 34, identifier:path; 35, identifier:basename; 36, argument_list; 36, 37; 37, identifier:in_file; 38, integer:0; 39, expression_statement; 39, 40; 40, assignment; 40, 41; 40, 42; 41, identifier:gsort; 42, call; 42, 43; 42, 46; 43, attribute; 43, 44; 43, 45; 44, identifier:config_utils; 45, identifier:get_program; 46, argument_list; 46, 47; 46, 48; 47, string:"gsort"; 48, identifier:data; 49, if_statement; 49, 50; 49, 58; 50, not_operator; 50, 51; 51, call; 51, 52; 51, 55; 52, attribute; 52, 53; 52, 54; 53, identifier:utils; 54, identifier:file_uptodate; 55, argument_list; 55, 56; 55, 57; 56, identifier:out_file; 57, identifier:in_file; 58, block; 58, 59; 59, with_statement; 59, 60; 59, 70; 60, with_clause; 60, 61; 61, with_item; 61, 62; 62, as_pattern; 62, 63; 62, 68; 63, call; 63, 64; 63, 65; 64, identifier:file_transaction; 65, argument_list; 65, 66; 65, 67; 66, identifier:data; 67, identifier:out_file; 68, as_pattern_target; 68, 69; 69, identifier:tx_out_file; 70, block; 70, 71; 70, 85; 70, 92; 71, expression_statement; 71, 72; 72, assignment; 72, 73; 72, 74; 73, identifier:fai_file; 74, call; 74, 75; 74, 78; 75, attribute; 75, 76; 75, 77; 76, identifier:ref; 77, identifier:fasta_idx; 78, argument_list; 78, 79; 79, call; 79, 80; 79, 83; 80, attribute; 80, 81; 80, 82; 81, identifier:dd; 82, identifier:get_ref_file; 83, argument_list; 83, 84; 84, identifier:data; 85, expression_statement; 85, 86; 86, assignment; 86, 87; 86, 88; 87, identifier:cmd; 88, parenthesized_expression; 88, 89; 89, concatenated_string; 89, 90; 89, 91; 90, string:"{gsort} {in_file} {fai_file} | bedtools merge -i - -d {stats[merge_size]} | "; 91, string:"bgzip -c > {tx_out_file}"; 92, expression_statement; 92, 93; 93, call; 93, 94; 93, 97; 94, attribute; 94, 95; 94, 96; 95, identifier:do; 96, identifier:run; 97, argument_list; 97, 98; 97, 107; 98, call; 98, 99; 98, 102; 99, attribute; 99, 100; 99, 101; 100, identifier:cmd; 101, identifier:format; 102, argument_list; 102, 103; 103, dictionary_splat; 103, 104; 104, call; 104, 105; 104, 106; 105, identifier:locals; 106, argument_list; 107, string:"Prepare SV callable BED regions"; 108, return_statement; 108, 109; 109, call; 109, 110; 109, 113; 110, attribute; 110, 111; 110, 112; 111, identifier:vcfutils; 112, identifier:bgzip_and_index; 113, argument_list; 113, 114; 113, 115; 114, identifier:out_file; 115, subscript; 115, 116; 115, 117; 116, identifier:data; 117, string:"config"
def _prep_callable_bed(in_file, work_dir, stats, data): """Sort and merge callable BED regions to prevent SV double counting """ out_file = os.path.join(work_dir, "%s-merge.bed.gz" % utils.splitext_plus(os.path.basename(in_file))[0]) gsort = config_utils.get_program("gsort", data) if not utils.file_uptodate(out_file, in_file): with file_transaction(data, out_file) as tx_out_file: fai_file = ref.fasta_idx(dd.get_ref_file(data)) cmd = ("{gsort} {in_file} {fai_file} | bedtools merge -i - -d {stats[merge_size]} | " "bgzip -c > {tx_out_file}") do.run(cmd.format(**locals()), "Prepare SV callable BED regions") return vcfutils.bgzip_and_index(out_file, data["config"])
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:fake_index; 3, parameters; 3, 4; 3, 5; 4, identifier:in_bam; 5, identifier:data; 6, block; 6, 7; 6, 9; 6, 15; 6, 55; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:index_file; 12, binary_operator:%; 12, 13; 12, 14; 13, string:"%s.bai"; 14, identifier:in_bam; 15, if_statement; 15, 16; 15, 23; 16, not_operator; 16, 17; 17, call; 17, 18; 17, 21; 18, attribute; 18, 19; 18, 20; 19, identifier:utils; 20, identifier:file_exists; 21, argument_list; 21, 22; 22, identifier:index_file; 23, block; 23, 24; 24, with_statement; 24, 25; 24, 35; 25, with_clause; 25, 26; 26, with_item; 26, 27; 27, as_pattern; 27, 28; 27, 33; 28, call; 28, 29; 28, 30; 29, identifier:file_transaction; 30, argument_list; 30, 31; 30, 32; 31, identifier:data; 32, identifier:index_file; 33, as_pattern_target; 33, 34; 34, identifier:tx_out_file; 35, block; 35, 36; 36, with_statement; 36, 37; 36, 47; 37, with_clause; 37, 38; 38, with_item; 38, 39; 39, as_pattern; 39, 40; 39, 45; 40, call; 40, 41; 40, 42; 41, identifier:open; 42, argument_list; 42, 43; 42, 44; 43, identifier:tx_out_file; 44, string:"w"; 45, as_pattern_target; 45, 46; 46, identifier:out_handle; 47, block; 47, 48; 48, expression_statement; 48, 49; 49, call; 49, 50; 49, 53; 50, attribute; 50, 51; 50, 52; 51, identifier:out_handle; 52, identifier:write; 53, argument_list; 53, 54; 54, string:"name sorted -- no index"; 55, return_statement; 55, 56; 56, identifier:index_file
def fake_index(in_bam, data): """Create a fake index file for namesorted BAMs. bais require by CWL for consistency. """ index_file = "%s.bai" % in_bam if not utils.file_exists(index_file): with file_transaction(data, index_file) as tx_out_file: with open(tx_out_file, "w") as out_handle: out_handle.write("name sorted -- no index") return index_file
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 12; 2, function_name:sort; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 4, identifier:in_bam; 5, identifier:config; 6, default_parameter; 6, 7; 6, 8; 7, identifier:order; 8, string:"coordinate"; 9, default_parameter; 9, 10; 9, 11; 10, identifier:out_dir; 11, None; 12, block; 12, 13; 12, 15; 12, 23; 12, 33; 12, 42; 12, 48; 12, 207; 13, expression_statement; 13, 14; 14, comment; 15, assert_statement; 15, 16; 15, 20; 16, call; 16, 17; 16, 18; 17, identifier:is_bam; 18, argument_list; 18, 19; 19, identifier:in_bam; 20, binary_operator:%; 20, 21; 20, 22; 21, string:"%s in not a BAM file"; 22, identifier:in_bam; 23, if_statement; 23, 24; 23, 30; 24, call; 24, 25; 24, 26; 25, identifier:bam_already_sorted; 26, argument_list; 26, 27; 26, 28; 26, 29; 27, identifier:in_bam; 28, identifier:config; 29, identifier:order; 30, block; 30, 31; 31, return_statement; 31, 32; 32, identifier:in_bam; 33, expression_statement; 33, 34; 34, assignment; 34, 35; 34, 36; 35, identifier:sort_stem; 36, call; 36, 37; 36, 38; 37, identifier:_get_sort_stem; 38, argument_list; 38, 39; 38, 40; 38, 41; 39, identifier:in_bam; 40, identifier:order; 41, identifier:out_dir; 42, expression_statement; 42, 43; 43, assignment; 43, 44; 43, 45; 44, identifier:sort_file; 45, binary_operator:+; 45, 46; 45, 47; 46, identifier:sort_stem; 47, string:".bam"; 48, if_statement; 48, 49; 48, 56; 49, not_operator; 49, 50; 50, call; 50, 51; 50, 54; 51, attribute; 51, 52; 51, 53; 52, identifier:utils; 53, identifier:file_exists; 54, argument_list; 54, 55; 55, identifier:sort_file; 56, block; 56, 57; 56, 67; 56, 79; 57, expression_statement; 57, 58; 58, assignment; 58, 59; 58, 60; 59, identifier:samtools; 60, call; 60, 61; 60, 64; 61, attribute; 61, 62; 61, 63; 62, identifier:config_utils; 63, identifier:get_program; 64, argument_list; 64, 65; 64, 66; 65, string:"samtools"; 66, identifier:config; 67, expression_statement; 67, 68; 68, assignment; 68, 69; 68, 70; 69, identifier:cores; 70, call; 70, 71; 70, 76; 71, attribute; 71, 72; 71, 75; 72, subscript; 72, 73; 72, 74; 73, identifier:config; 74, string:"algorithm"; 75, identifier:get; 76, argument_list; 76, 77; 76, 78; 77, string:"num_cores"; 78, integer:1; 79, with_statement; 79, 80; 79, 90; 80, with_clause; 80, 81; 81, with_item; 81, 82; 82, as_pattern; 82, 83; 82, 88; 83, call; 83, 84; 83, 85; 84, identifier:file_transaction; 85, argument_list; 85, 86; 85, 87; 86, identifier:config; 87, identifier:sort_file; 88, as_pattern_target; 88, 89; 89, identifier:tx_sort_file; 90, block; 90, 91; 90, 104; 90, 120; 90, 129; 90, 139; 90, 140; 90, 141; 90, 165; 90, 172; 91, expression_statement; 91, 92; 92, assignment; 92, 93; 92, 94; 93, identifier:tx_sort_stem; 94, subscript; 94, 95; 94, 103; 95, call; 95, 96; 95, 101; 96, attribute; 96, 97; 96, 100; 97, attribute; 97, 98; 97, 99; 98, identifier:os; 99, identifier:path; 100, identifier:splitext; 101, argument_list; 101, 102; 102, identifier:tx_sort_file; 103, integer:0; 104, expression_statement; 104, 105; 105, assignment; 105, 106; 105, 107; 106, identifier:tx_dir; 107, call; 107, 108; 107, 111; 108, attribute; 108, 109; 108, 110; 109, identifier:utils; 110, identifier:safe_makedir; 111, argument_list; 111, 112; 112, call; 112, 113; 112, 118; 113, attribute; 113, 114; 113, 117; 114, attribute; 114, 115; 114, 116; 115, identifier:os; 116, identifier:path; 117, identifier:dirname; 118, argument_list; 118, 119; 119, identifier:tx_sort_file; 120, expression_statement; 120, 121; 121, assignment; 121, 122; 121, 123; 122, identifier:order_flag; 123, conditional_expression:if; 123, 124; 123, 125; 123, 128; 124, string:"-n"; 125, comparison_operator:==; 125, 126; 125, 127; 126, identifier:order; 127, string:"queryname"; 128, string:""; 129, expression_statement; 129, 130; 130, assignment; 130, 131; 130, 132; 131, identifier:resources; 132, call; 132, 133; 132, 136; 133, attribute; 133, 134; 133, 135; 134, identifier:config_utils; 135, identifier:get_resources; 136, argument_list; 136, 137; 136, 138; 137, string:"samtools"; 138, identifier:config; 139, comment; 140, comment; 141, expression_statement; 141, 142; 142, assignment; 142, 143; 142, 144; 143, identifier:mem; 144, call; 144, 145; 144, 164; 145, attribute; 145, 146; 145, 163; 146, call; 146, 147; 146, 150; 147, attribute; 147, 148; 147, 149; 148, identifier:config_utils; 149, identifier:adjust_memory; 150, argument_list; 150, 151; 150, 158; 150, 159; 150, 160; 151, call; 151, 152; 151, 155; 152, attribute; 152, 153; 152, 154; 153, identifier:resources; 154, identifier:get; 155, argument_list; 155, 156; 155, 157; 156, string:"memory"; 157, string:"2G"; 158, float:1.25; 159, string:"decrease"; 160, keyword_argument; 160, 161; 160, 162; 161, identifier:out_modifier; 162, string:"M"; 163, identifier:upper; 164, argument_list; 165, expression_statement; 165, 166; 166, assignment; 166, 167; 166, 168; 167, identifier:cmd; 168, parenthesized_expression; 168, 169; 169, concatenated_string; 169, 170; 169, 171; 170, string:"{samtools} sort -@ {cores} -m {mem} -O BAM {order_flag} "; 171, string:"-T {tx_sort_stem}-sort -o {tx_sort_file} {in_bam}"; 172, expression_statement; 172, 173; 173, call; 173, 174; 173, 177; 174, attribute; 174, 175; 174, 176; 175, identifier:do; 176, identifier:run; 177, argument_list; 177, 178; 177, 187; 178, call; 178, 179; 178, 182; 179, attribute; 179, 180; 179, 181; 180, identifier:cmd; 181, identifier:format; 182, argument_list; 182, 183; 183, dictionary_splat; 183, 184; 184, call; 184, 185; 184, 186; 185, identifier:locals; 186, argument_list; 187, binary_operator:%; 187, 188; 187, 189; 188, string:"Sort BAM file %s: %s to %s"; 189, tuple; 189, 190; 189, 191; 189, 199; 190, identifier:order; 191, call; 191, 192; 191, 197; 192, attribute; 192, 193; 192, 196; 193, attribute; 193, 194; 193, 195; 194, identifier:os; 195, identifier:path; 196, identifier:basename; 197, argument_list; 197, 198; 198, identifier:in_bam; 199, call; 199, 200; 199, 205; 200, attribute; 200, 201; 200, 204; 201, attribute; 201, 202; 201, 203; 202, identifier:os; 203, identifier:path; 204, identifier:basename; 205, argument_list; 205, 206; 206, identifier:sort_file; 207, return_statement; 207, 208; 208, identifier:sort_file
def sort(in_bam, config, order="coordinate", out_dir=None): """Sort a BAM file, skipping if already present. """ assert is_bam(in_bam), "%s in not a BAM file" % in_bam if bam_already_sorted(in_bam, config, order): return in_bam sort_stem = _get_sort_stem(in_bam, order, out_dir) sort_file = sort_stem + ".bam" if not utils.file_exists(sort_file): samtools = config_utils.get_program("samtools", config) cores = config["algorithm"].get("num_cores", 1) with file_transaction(config, sort_file) as tx_sort_file: tx_sort_stem = os.path.splitext(tx_sort_file)[0] tx_dir = utils.safe_makedir(os.path.dirname(tx_sort_file)) order_flag = "-n" if order == "queryname" else "" resources = config_utils.get_resources("samtools", config) # Slightly decrease memory and allow more accurate representation # in Mb to ensure fits within systems like SLURM mem = config_utils.adjust_memory(resources.get("memory", "2G"), 1.25, "decrease", out_modifier="M").upper() cmd = ("{samtools} sort -@ {cores} -m {mem} -O BAM {order_flag} " "-T {tx_sort_stem}-sort -o {tx_sort_file} {in_bam}") do.run(cmd.format(**locals()), "Sort BAM file %s: %s to %s" % (order, os.path.basename(in_bam), os.path.basename(sort_file))) return sort_file
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:tobam_cl; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:data; 5, identifier:out_file; 6, default_parameter; 6, 7; 6, 8; 7, identifier:is_paired; 8, False; 9, block; 9, 10; 9, 12; 9, 19; 9, 28; 10, expression_statement; 10, 11; 11, comment; 12, expression_statement; 12, 13; 13, assignment; 13, 14; 13, 15; 14, identifier:do_dedup; 15, call; 15, 16; 15, 17; 16, identifier:_check_dedup; 17, argument_list; 17, 18; 18, identifier:data; 19, expression_statement; 19, 20; 20, assignment; 20, 21; 20, 22; 21, identifier:umi_consensus; 22, call; 22, 23; 22, 26; 23, attribute; 23, 24; 23, 25; 24, identifier:dd; 25, identifier:get_umi_consensus; 26, argument_list; 26, 27; 27, identifier:data; 28, with_statement; 28, 29; 28, 39; 29, with_clause; 29, 30; 30, with_item; 30, 31; 31, as_pattern; 31, 32; 31, 37; 32, call; 32, 33; 32, 34; 33, identifier:file_transaction; 34, argument_list; 34, 35; 34, 36; 35, identifier:data; 36, identifier:out_file; 37, as_pattern_target; 37, 38; 38, identifier:tx_out_file; 39, block; 39, 40; 40, if_statement; 40, 41; 40, 43; 40, 53; 40, 66; 40, 150; 41, not_operator; 41, 42; 42, identifier:do_dedup; 43, block; 43, 44; 44, expression_statement; 44, 45; 45, yield; 45, 46; 46, tuple; 46, 47; 46, 52; 47, call; 47, 48; 47, 49; 48, identifier:sam_to_sortbam_cl; 49, argument_list; 49, 50; 49, 51; 50, identifier:data; 51, identifier:tx_out_file; 52, identifier:tx_out_file; 53, elif_clause; 53, 54; 53, 55; 54, identifier:umi_consensus; 55, block; 55, 56; 56, expression_statement; 56, 57; 57, yield; 57, 58; 58, tuple; 58, 59; 58, 65; 59, call; 59, 60; 59, 61; 60, identifier:_sam_to_grouped_umi_cl; 61, argument_list; 61, 62; 61, 63; 61, 64; 62, identifier:data; 63, identifier:umi_consensus; 64, identifier:tx_out_file; 65, identifier:tx_out_file; 66, elif_clause; 66, 67; 66, 84; 67, boolean_operator:and; 67, 68; 67, 74; 68, boolean_operator:and; 68, 69; 68, 70; 69, identifier:is_paired; 70, call; 70, 71; 70, 72; 71, identifier:_need_sr_disc_reads; 72, argument_list; 72, 73; 73, identifier:data; 74, not_operator; 74, 75; 75, call; 75, 76; 75, 77; 76, identifier:_too_many_contigs; 77, argument_list; 77, 78; 78, call; 78, 79; 78, 82; 79, attribute; 79, 80; 79, 81; 80, identifier:dd; 81, identifier:get_ref_file; 82, argument_list; 82, 83; 83, identifier:data; 84, block; 84, 85; 84, 100; 84, 115; 85, expression_statement; 85, 86; 86, assignment; 86, 87; 86, 88; 87, identifier:sr_file; 88, binary_operator:%; 88, 89; 88, 90; 89, string:"%s-sr.bam"; 90, subscript; 90, 91; 90, 99; 91, call; 91, 92; 91, 97; 92, attribute; 92, 93; 92, 96; 93, attribute; 93, 94; 93, 95; 94, identifier:os; 95, identifier:path; 96, identifier:splitext; 97, argument_list; 97, 98; 98, identifier:out_file; 99, integer:0; 100, expression_statement; 100, 101; 101, assignment; 101, 102; 101, 103; 102, identifier:disc_file; 103, binary_operator:%; 103, 104; 103, 105; 104, string:"%s-disc.bam"; 105, subscript; 105, 106; 105, 114; 106, call; 106, 107; 106, 112; 107, attribute; 107, 108; 107, 111; 108, attribute; 108, 109; 108, 110; 109, identifier:os; 110, identifier:path; 111, identifier:splitext; 112, argument_list; 112, 113; 113, identifier:out_file; 114, integer:0; 115, with_statement; 115, 116; 115, 126; 116, with_clause; 116, 117; 117, with_item; 117, 118; 118, as_pattern; 118, 119; 118, 124; 119, call; 119, 120; 119, 121; 120, identifier:file_transaction; 121, argument_list; 121, 122; 121, 123; 122, identifier:data; 123, identifier:sr_file; 124, as_pattern_target; 124, 125; 125, identifier:tx_sr_file; 126, block; 126, 127; 127, with_statement; 127, 128; 127, 138; 128, with_clause; 128, 129; 129, with_item; 129, 130; 130, as_pattern; 130, 131; 130, 136; 131, call; 131, 132; 131, 133; 132, identifier:file_transaction; 133, argument_list; 133, 134; 133, 135; 134, identifier:data; 135, identifier:disc_file; 136, as_pattern_target; 136, 137; 137, identifier:tx_disc_file; 138, block; 138, 139; 139, expression_statement; 139, 140; 140, yield; 140, 141; 141, tuple; 141, 142; 141, 149; 142, call; 142, 143; 142, 144; 143, identifier:samblaster_dedup_sort; 144, argument_list; 144, 145; 144, 146; 144, 147; 144, 148; 145, identifier:data; 146, identifier:tx_out_file; 147, identifier:tx_sr_file; 148, identifier:tx_disc_file; 149, identifier:tx_out_file; 150, else_clause; 150, 151; 151, block; 151, 152; 152, expression_statement; 152, 153; 153, yield; 153, 154; 154, tuple; 154, 155; 154, 160; 155, call; 155, 156; 155, 157; 156, identifier:_biobambam_dedup_sort; 157, argument_list; 157, 158; 157, 159; 158, identifier:data; 159, identifier:tx_out_file; 160, identifier:tx_out_file
def tobam_cl(data, out_file, is_paired=False): """Prepare command line for producing de-duplicated sorted output. - If no deduplication, sort and prepare a BAM file. - If paired, then use samblaster and prepare discordant outputs. - If unpaired, use biobambam's bammarkduplicates """ do_dedup = _check_dedup(data) umi_consensus = dd.get_umi_consensus(data) with file_transaction(data, out_file) as tx_out_file: if not do_dedup: yield (sam_to_sortbam_cl(data, tx_out_file), tx_out_file) elif umi_consensus: yield (_sam_to_grouped_umi_cl(data, umi_consensus, tx_out_file), tx_out_file) elif is_paired and _need_sr_disc_reads(data) and not _too_many_contigs(dd.get_ref_file(data)): sr_file = "%s-sr.bam" % os.path.splitext(out_file)[0] disc_file = "%s-disc.bam" % os.path.splitext(out_file)[0] with file_transaction(data, sr_file) as tx_sr_file: with file_transaction(data, disc_file) as tx_disc_file: yield (samblaster_dedup_sort(data, tx_out_file, tx_sr_file, tx_disc_file), tx_out_file) else: yield (_biobambam_dedup_sort(data, tx_out_file), tx_out_file)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:sam_to_sortbam_cl; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:data; 5, identifier:tx_out_file; 6, default_parameter; 6, 7; 6, 8; 7, identifier:name_sort; 8, False; 9, block; 9, 10; 9, 12; 9, 24; 9, 36; 9, 49; 9, 56; 10, expression_statement; 10, 11; 11, comment; 12, expression_statement; 12, 13; 13, assignment; 13, 14; 13, 15; 14, identifier:samtools; 15, call; 15, 16; 15, 19; 16, attribute; 16, 17; 16, 18; 17, identifier:config_utils; 18, identifier:get_program; 19, argument_list; 19, 20; 19, 21; 20, string:"samtools"; 21, subscript; 21, 22; 21, 23; 22, identifier:data; 23, string:"config"; 24, expression_statement; 24, 25; 25, assignment; 25, 26; 25, 29; 26, pattern_list; 26, 27; 26, 28; 27, identifier:cores; 28, identifier:mem; 29, call; 29, 30; 29, 31; 30, identifier:_get_cores_memory; 31, argument_list; 31, 32; 31, 33; 32, identifier:data; 33, keyword_argument; 33, 34; 33, 35; 34, identifier:downscale; 35, integer:2; 36, expression_statement; 36, 37; 37, assignment; 37, 38; 37, 39; 38, identifier:tmp_file; 39, binary_operator:%; 39, 40; 39, 41; 40, string:"%s-sorttmp"; 41, subscript; 41, 42; 41, 48; 42, call; 42, 43; 42, 46; 43, attribute; 43, 44; 43, 45; 44, identifier:utils; 45, identifier:splitext_plus; 46, argument_list; 46, 47; 47, identifier:tx_out_file; 48, integer:0; 49, expression_statement; 49, 50; 50, assignment; 50, 51; 50, 52; 51, identifier:sort_flag; 52, conditional_expression:if; 52, 53; 52, 54; 52, 55; 53, string:"-n"; 54, identifier:name_sort; 55, string:""; 56, return_statement; 56, 57; 57, parenthesized_expression; 57, 58; 58, call; 58, 59; 58, 64; 59, attribute; 59, 60; 59, 63; 60, concatenated_string; 60, 61; 60, 62; 61, string:"{samtools} sort -@ {cores} -m {mem} {sort_flag} "; 62, string:"-T {tmp_file} -o {tx_out_file} /dev/stdin"; 63, identifier:format; 64, argument_list; 64, 65; 65, dictionary_splat; 65, 66; 66, call; 66, 67; 66, 68; 67, identifier:locals; 68, argument_list
def sam_to_sortbam_cl(data, tx_out_file, name_sort=False): """Convert to sorted BAM output. Set name_sort to True to sort reads by queryname """ samtools = config_utils.get_program("samtools", data["config"]) cores, mem = _get_cores_memory(data, downscale=2) tmp_file = "%s-sorttmp" % utils.splitext_plus(tx_out_file)[0] sort_flag = "-n" if name_sort else "" return ("{samtools} sort -@ {cores} -m {mem} {sort_flag} " "-T {tmp_file} -o {tx_out_file} /dev/stdin".format(**locals()))
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:samblaster_dedup_sort; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 4, identifier:data; 5, identifier:tx_out_file; 6, identifier:tx_sr_file; 7, identifier:tx_disc_file; 8, block; 8, 9; 8, 11; 8, 23; 8, 35; 8, 48; 8, 53; 8, 54; 8, 66; 8, 67; 8, 85; 8, 104; 8, 152; 8, 153; 8, 157; 8, 169; 8, 189; 8, 209; 8, 210; 8, 217; 9, expression_statement; 9, 10; 10, comment; 11, expression_statement; 11, 12; 12, assignment; 12, 13; 12, 14; 13, identifier:samblaster; 14, call; 14, 15; 14, 18; 15, attribute; 15, 16; 15, 17; 16, identifier:config_utils; 17, identifier:get_program; 18, argument_list; 18, 19; 18, 20; 19, string:"samblaster"; 20, subscript; 20, 21; 20, 22; 21, identifier:data; 22, string:"config"; 23, expression_statement; 23, 24; 24, assignment; 24, 25; 24, 26; 25, identifier:samtools; 26, call; 26, 27; 26, 30; 27, attribute; 27, 28; 27, 29; 28, identifier:config_utils; 29, identifier:get_program; 30, argument_list; 30, 31; 30, 32; 31, string:"samtools"; 32, subscript; 32, 33; 32, 34; 33, identifier:data; 34, string:"config"; 35, expression_statement; 35, 36; 36, assignment; 36, 37; 36, 38; 37, identifier:tmp_prefix; 38, binary_operator:%; 38, 39; 38, 40; 39, string:"%s-sorttmp"; 40, subscript; 40, 41; 40, 47; 41, call; 41, 42; 41, 45; 42, attribute; 42, 43; 42, 44; 43, identifier:utils; 44, identifier:splitext_plus; 45, argument_list; 45, 46; 46, identifier:tx_out_file; 47, integer:0; 48, expression_statement; 48, 49; 49, assignment; 49, 50; 49, 51; 50, identifier:tobam_cmd; 51, parenthesized_expression; 51, 52; 52, string:"{samtools} sort {sort_opt} -@ {cores} -m {mem} -T {tmp_prefix}-{dext} {out_file} -"; 53, comment; 54, expression_statement; 54, 55; 55, assignment; 55, 56; 55, 59; 56, pattern_list; 56, 57; 56, 58; 57, identifier:cores; 58, identifier:mem; 59, call; 59, 60; 59, 61; 60, identifier:_get_cores_memory; 61, argument_list; 61, 62; 61, 63; 62, identifier:data; 63, keyword_argument; 63, 64; 63, 65; 64, identifier:downscale; 65, integer:2; 66, comment; 67, expression_statement; 67, 68; 68, assignment; 68, 69; 68, 70; 69, identifier:ds_cmd; 70, conditional_expression:if; 70, 71; 70, 72; 70, 78; 71, None; 72, call; 72, 73; 72, 76; 73, attribute; 73, 74; 73, 75; 74, identifier:data; 75, identifier:get; 76, argument_list; 76, 77; 77, string:"align_split"; 78, call; 78, 79; 78, 82; 79, attribute; 79, 80; 79, 81; 80, identifier:bam; 81, identifier:get_maxcov_downsample_cl; 82, argument_list; 82, 83; 82, 84; 83, identifier:data; 84, string:"samtools"; 85, expression_statement; 85, 86; 86, assignment; 86, 87; 86, 88; 87, identifier:sort_opt; 88, conditional_expression:if; 88, 89; 88, 90; 88, 103; 89, string:"-n"; 90, boolean_operator:and; 90, 91; 90, 97; 91, call; 91, 92; 91, 95; 92, attribute; 92, 93; 92, 94; 93, identifier:data; 94, identifier:get; 95, argument_list; 95, 96; 96, string:"align_split"; 97, call; 97, 98; 97, 101; 98, attribute; 98, 99; 98, 100; 99, identifier:dd; 100, identifier:get_mark_duplicates; 101, argument_list; 101, 102; 102, identifier:data; 103, string:""; 104, if_statement; 104, 105; 104, 106; 104, 130; 105, identifier:ds_cmd; 106, block; 106, 107; 107, expression_statement; 107, 108; 108, assignment; 108, 109; 108, 110; 109, identifier:dedup_cmd; 110, binary_operator:%; 110, 111; 110, 112; 111, string:"%s %s > %s"; 112, tuple; 112, 113; 112, 128; 112, 129; 113, call; 113, 114; 113, 117; 114, attribute; 114, 115; 114, 116; 115, identifier:tobam_cmd; 116, identifier:format; 117, argument_list; 117, 118; 117, 121; 117, 124; 118, keyword_argument; 118, 119; 118, 120; 119, identifier:out_file; 120, string:""; 121, keyword_argument; 121, 122; 121, 123; 122, identifier:dext; 123, string:"full"; 124, dictionary_splat; 124, 125; 125, call; 125, 126; 125, 127; 126, identifier:locals; 127, argument_list; 128, identifier:ds_cmd; 129, identifier:tx_out_file; 130, else_clause; 130, 131; 131, block; 131, 132; 132, expression_statement; 132, 133; 133, assignment; 133, 134; 133, 135; 134, identifier:dedup_cmd; 135, call; 135, 136; 135, 139; 136, attribute; 136, 137; 136, 138; 137, identifier:tobam_cmd; 138, identifier:format; 139, argument_list; 139, 140; 139, 145; 139, 148; 140, keyword_argument; 140, 141; 140, 142; 141, identifier:out_file; 142, binary_operator:%; 142, 143; 142, 144; 143, string:"-o %s"; 144, identifier:tx_out_file; 145, keyword_argument; 145, 146; 145, 147; 146, identifier:dext; 147, string:"full"; 148, dictionary_splat; 148, 149; 149, call; 149, 150; 149, 151; 150, identifier:locals; 151, argument_list; 152, comment; 153, expression_statement; 153, 154; 154, assignment; 154, 155; 154, 156; 155, identifier:sort_opt; 156, string:""; 157, expression_statement; 157, 158; 158, assignment; 158, 159; 158, 162; 159, pattern_list; 159, 160; 159, 161; 160, identifier:cores; 161, identifier:mem; 162, call; 162, 163; 162, 164; 163, identifier:_get_cores_memory; 164, argument_list; 164, 165; 164, 166; 165, identifier:data; 166, keyword_argument; 166, 167; 166, 168; 167, identifier:downscale; 168, integer:4; 169, expression_statement; 169, 170; 170, assignment; 170, 171; 170, 172; 171, identifier:splitter_cmd; 172, call; 172, 173; 172, 176; 173, attribute; 173, 174; 173, 175; 174, identifier:tobam_cmd; 175, identifier:format; 176, argument_list; 176, 177; 176, 182; 176, 185; 177, keyword_argument; 177, 178; 177, 179; 178, identifier:out_file; 179, binary_operator:%; 179, 180; 179, 181; 180, string:"-o %s"; 181, identifier:tx_sr_file; 182, keyword_argument; 182, 183; 182, 184; 183, identifier:dext; 184, string:"spl"; 185, dictionary_splat; 185, 186; 186, call; 186, 187; 186, 188; 187, identifier:locals; 188, argument_list; 189, expression_statement; 189, 190; 190, assignment; 190, 191; 190, 192; 191, identifier:discordant_cmd; 192, call; 192, 193; 192, 196; 193, attribute; 193, 194; 193, 195; 194, identifier:tobam_cmd; 195, identifier:format; 196, argument_list; 196, 197; 196, 202; 196, 205; 197, keyword_argument; 197, 198; 197, 199; 198, identifier:out_file; 199, binary_operator:%; 199, 200; 199, 201; 200, string:"-o %s"; 201, identifier:tx_disc_file; 202, keyword_argument; 202, 203; 202, 204; 203, identifier:dext; 204, string:"disc"; 205, dictionary_splat; 205, 206; 206, call; 206, 207; 206, 208; 207, identifier:locals; 208, argument_list; 209, comment; 210, expression_statement; 210, 211; 211, assignment; 211, 212; 211, 213; 212, identifier:cmd; 213, parenthesized_expression; 213, 214; 214, concatenated_string; 214, 215; 214, 216; 215, string:"{samblaster} --addMateTags -M --splitterFile >({splitter_cmd}) --discordantFile >({discordant_cmd}) "; 216, string:"| {dedup_cmd}"; 217, return_statement; 217, 218; 218, call; 218, 219; 218, 222; 219, attribute; 219, 220; 219, 221; 220, identifier:cmd; 221, identifier:format; 222, argument_list; 222, 223; 223, dictionary_splat; 223, 224; 224, call; 224, 225; 224, 226; 225, identifier:locals; 226, argument_list
def samblaster_dedup_sort(data, tx_out_file, tx_sr_file, tx_disc_file): """Deduplicate and sort with samblaster, produces split read and discordant pair files. """ samblaster = config_utils.get_program("samblaster", data["config"]) samtools = config_utils.get_program("samtools", data["config"]) tmp_prefix = "%s-sorttmp" % utils.splitext_plus(tx_out_file)[0] tobam_cmd = ("{samtools} sort {sort_opt} -@ {cores} -m {mem} -T {tmp_prefix}-{dext} {out_file} -") # full BAM -- associate more memory and cores cores, mem = _get_cores_memory(data, downscale=2) # Potentially downsample to maximum coverage here if not splitting and whole genome sample ds_cmd = None if data.get("align_split") else bam.get_maxcov_downsample_cl(data, "samtools") sort_opt = "-n" if data.get("align_split") and dd.get_mark_duplicates(data) else "" if ds_cmd: dedup_cmd = "%s %s > %s" % (tobam_cmd.format(out_file="", dext="full", **locals()), ds_cmd, tx_out_file) else: dedup_cmd = tobam_cmd.format(out_file="-o %s" % tx_out_file, dext="full", **locals()) # split and discordant BAMs -- give less memory/cores since smaller files sort_opt = "" cores, mem = _get_cores_memory(data, downscale=4) splitter_cmd = tobam_cmd.format(out_file="-o %s" % tx_sr_file, dext="spl", **locals()) discordant_cmd = tobam_cmd.format(out_file="-o %s" % tx_disc_file, dext="disc", **locals()) # samblaster 0.1.22 and better require the -M flag for compatibility with bwa-mem cmd = ("{samblaster} --addMateTags -M --splitterFile >({splitter_cmd}) --discordantFile >({discordant_cmd}) " "| {dedup_cmd}") return cmd.format(**locals())
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:_biobambam_dedup_sort; 3, parameters; 3, 4; 3, 5; 4, identifier:data; 5, identifier:tx_out_file; 6, block; 6, 7; 6, 9; 6, 21; 6, 33; 6, 46; 6, 127; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:samtools; 12, call; 12, 13; 12, 16; 13, attribute; 13, 14; 13, 15; 14, identifier:config_utils; 15, identifier:get_program; 16, argument_list; 16, 17; 16, 18; 17, string:"samtools"; 18, subscript; 18, 19; 18, 20; 19, identifier:data; 20, string:"config"; 21, expression_statement; 21, 22; 22, assignment; 22, 23; 22, 26; 23, pattern_list; 23, 24; 23, 25; 24, identifier:cores; 25, identifier:mem; 26, call; 26, 27; 26, 28; 27, identifier:_get_cores_memory; 28, argument_list; 28, 29; 28, 30; 29, identifier:data; 30, keyword_argument; 30, 31; 30, 32; 31, identifier:downscale; 32, integer:2; 33, expression_statement; 33, 34; 34, assignment; 34, 35; 34, 36; 35, identifier:tmp_file; 36, binary_operator:%; 36, 37; 36, 38; 37, string:"%s-sorttmp"; 38, subscript; 38, 39; 38, 45; 39, call; 39, 40; 39, 43; 40, attribute; 40, 41; 40, 42; 41, identifier:utils; 42, identifier:splitext_plus; 43, argument_list; 43, 44; 44, identifier:tx_out_file; 45, integer:0; 46, if_statement; 46, 47; 46, 53; 46, 77; 47, call; 47, 48; 47, 51; 48, attribute; 48, 49; 48, 50; 49, identifier:data; 50, identifier:get; 51, argument_list; 51, 52; 52, string:"align_split"; 53, block; 53, 54; 53, 71; 54, expression_statement; 54, 55; 55, assignment; 55, 56; 55, 57; 56, identifier:sort_opt; 57, conditional_expression:if; 57, 58; 57, 59; 57, 70; 58, string:"-n"; 59, boolean_operator:and; 59, 60; 59, 66; 60, call; 60, 61; 60, 64; 61, attribute; 61, 62; 61, 63; 62, identifier:data; 63, identifier:get; 64, argument_list; 64, 65; 65, string:"align_split"; 66, call; 66, 67; 66, 68; 67, identifier:_check_dedup; 68, argument_list; 68, 69; 69, identifier:data; 70, string:""; 71, expression_statement; 71, 72; 72, assignment; 72, 73; 72, 74; 73, identifier:cmd; 74, binary_operator:%; 74, 75; 74, 76; 75, string:"{samtools} sort %s -@ {cores} -m {mem} -O bam -T {tmp_file}-namesort -o {tx_out_file} -"; 76, identifier:sort_opt; 77, else_clause; 77, 78; 77, 79; 78, comment; 79, block; 79, 80; 79, 98; 79, 108; 79, 118; 80, expression_statement; 80, 81; 81, assignment; 81, 82; 81, 83; 82, identifier:cores; 83, call; 83, 84; 83, 85; 84, identifier:max; 85, argument_list; 85, 86; 85, 87; 86, integer:1; 87, call; 87, 88; 87, 89; 88, identifier:int; 89, argument_list; 89, 90; 90, call; 90, 91; 90, 94; 91, attribute; 91, 92; 91, 93; 92, identifier:math; 93, identifier:ceil; 94, argument_list; 94, 95; 95, binary_operator:*; 95, 96; 95, 97; 96, identifier:cores; 97, float:0.75; 98, expression_statement; 98, 99; 99, assignment; 99, 100; 99, 101; 100, identifier:ds_cmd; 101, call; 101, 102; 101, 105; 102, attribute; 102, 103; 102, 104; 103, identifier:bam; 104, identifier:get_maxcov_downsample_cl; 105, argument_list; 105, 106; 105, 107; 106, identifier:data; 107, string:"bamsormadup"; 108, expression_statement; 108, 109; 109, assignment; 109, 110; 109, 111; 110, identifier:bamsormadup; 111, call; 111, 112; 111, 115; 112, attribute; 112, 113; 112, 114; 113, identifier:config_utils; 114, identifier:get_program; 115, argument_list; 115, 116; 115, 117; 116, string:"bamsormadup"; 117, identifier:data; 118, expression_statement; 118, 119; 119, assignment; 119, 120; 119, 121; 120, identifier:cmd; 121, parenthesized_expression; 121, 122; 122, binary_operator:%; 122, 123; 122, 126; 123, concatenated_string; 123, 124; 123, 125; 124, string:"{bamsormadup} inputformat=sam threads={cores} tmpfile={tmp_file}-markdup "; 125, string:"SO=coordinate %s > {tx_out_file}"; 126, identifier:ds_cmd; 127, return_statement; 127, 128; 128, call; 128, 129; 128, 132; 129, attribute; 129, 130; 129, 131; 130, identifier:cmd; 131, identifier:format; 132, argument_list; 132, 133; 133, dictionary_splat; 133, 134; 134, call; 134, 135; 134, 136; 135, identifier:locals; 136, argument_list
def _biobambam_dedup_sort(data, tx_out_file): """Perform streaming deduplication and sorting with biobambam's bamsormadup """ samtools = config_utils.get_program("samtools", data["config"]) cores, mem = _get_cores_memory(data, downscale=2) tmp_file = "%s-sorttmp" % utils.splitext_plus(tx_out_file)[0] if data.get("align_split"): sort_opt = "-n" if data.get("align_split") and _check_dedup(data) else "" cmd = "{samtools} sort %s -@ {cores} -m {mem} -O bam -T {tmp_file}-namesort -o {tx_out_file} -" % sort_opt else: # scale core usage to avoid memory issues with larger WGS samples cores = max(1, int(math.ceil(cores * 0.75))) ds_cmd = bam.get_maxcov_downsample_cl(data, "bamsormadup") bamsormadup = config_utils.get_program("bamsormadup", data) cmd = ("{bamsormadup} inputformat=sam threads={cores} tmpfile={tmp_file}-markdup " "SO=coordinate %s > {tx_out_file}" % ds_cmd) return cmd.format(**locals())
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:_prepare_bam_file; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:bam_file; 5, identifier:tmp_dir; 6, identifier:config; 7, block; 7, 8; 7, 10; 7, 18; 7, 32; 8, expression_statement; 8, 9; 9, comment; 10, expression_statement; 10, 11; 11, assignment; 11, 12; 11, 13; 12, identifier:sort_mode; 13, call; 13, 14; 13, 15; 14, identifier:_get_sort_order; 15, argument_list; 15, 16; 15, 17; 16, identifier:bam_file; 17, identifier:config; 18, if_statement; 18, 19; 18, 22; 19, comparison_operator:!=; 19, 20; 19, 21; 20, identifier:sort_mode; 21, string:"queryname"; 22, block; 22, 23; 23, expression_statement; 23, 24; 24, assignment; 24, 25; 24, 26; 25, identifier:bam_file; 26, call; 26, 27; 26, 28; 27, identifier:sort; 28, argument_list; 28, 29; 28, 30; 28, 31; 29, identifier:bam_file; 30, identifier:config; 31, string:"queryname"; 32, return_statement; 32, 33; 33, identifier:bam_file
def _prepare_bam_file(bam_file, tmp_dir, config): """ Pipe sort by name cmd in case sort by coordinates """ sort_mode = _get_sort_order(bam_file, config) if sort_mode != "queryname": bam_file = sort(bam_file, config, "queryname") return bam_file
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:_concat_records; 3, parameters; 3, 4; 3, 5; 4, identifier:items_by_key; 5, identifier:input_order; 6, block; 6, 7; 6, 9; 6, 13; 6, 35; 6, 44; 6, 53; 6, 101; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:all_records; 12, list:[]; 13, for_statement; 13, 14; 13, 17; 13, 22; 14, tuple_pattern; 14, 15; 14, 16; 15, identifier:k; 16, identifier:t; 17, call; 17, 18; 17, 21; 18, attribute; 18, 19; 18, 20; 19, identifier:input_order; 20, identifier:items; 21, argument_list; 22, block; 22, 23; 23, if_statement; 23, 24; 23, 27; 24, comparison_operator:==; 24, 25; 24, 26; 25, identifier:t; 26, string:"record"; 27, block; 27, 28; 28, expression_statement; 28, 29; 29, call; 29, 30; 29, 33; 30, attribute; 30, 31; 30, 32; 31, identifier:all_records; 32, identifier:append; 33, argument_list; 33, 34; 34, identifier:k; 35, expression_statement; 35, 36; 36, assignment; 36, 37; 36, 38; 37, identifier:out_items_by_key; 38, call; 38, 39; 38, 42; 39, attribute; 39, 40; 39, 41; 40, identifier:utils; 41, identifier:deepish_copy; 42, argument_list; 42, 43; 43, identifier:items_by_key; 44, expression_statement; 44, 45; 45, assignment; 45, 46; 45, 47; 46, identifier:out_input_order; 47, call; 47, 48; 47, 51; 48, attribute; 48, 49; 48, 50; 49, identifier:utils; 50, identifier:deepish_copy; 51, argument_list; 51, 52; 52, identifier:input_order; 53, if_statement; 53, 54; 53, 60; 54, comparison_operator:>; 54, 55; 54, 59; 55, call; 55, 56; 55, 57; 56, identifier:len; 57, argument_list; 57, 58; 58, identifier:all_records; 59, integer:1; 60, block; 60, 61; 60, 67; 60, 73; 60, 95; 61, expression_statement; 61, 62; 62, assignment; 62, 63; 62, 64; 63, identifier:final_k; 64, subscript; 64, 65; 64, 66; 65, identifier:all_records; 66, integer:0; 67, expression_statement; 67, 68; 68, assignment; 68, 69; 68, 70; 69, identifier:final_v; 70, subscript; 70, 71; 70, 72; 71, identifier:items_by_key; 72, identifier:final_k; 73, for_statement; 73, 74; 73, 75; 73, 80; 74, identifier:k; 75, subscript; 75, 76; 75, 77; 76, identifier:all_records; 77, slice; 77, 78; 77, 79; 78, integer:1; 79, colon; 80, block; 80, 81; 80, 87; 80, 91; 81, expression_statement; 81, 82; 82, augmented_assignment:+=; 82, 83; 82, 84; 83, identifier:final_v; 84, subscript; 84, 85; 84, 86; 85, identifier:items_by_key; 86, identifier:k; 87, delete_statement; 87, 88; 88, subscript; 88, 89; 88, 90; 89, identifier:out_items_by_key; 90, identifier:k; 91, delete_statement; 91, 92; 92, subscript; 92, 93; 92, 94; 93, identifier:out_input_order; 94, identifier:k; 95, expression_statement; 95, 96; 96, assignment; 96, 97; 96, 100; 97, subscript; 97, 98; 97, 99; 98, identifier:out_items_by_key; 99, identifier:final_k; 100, identifier:final_v; 101, return_statement; 101, 102; 102, expression_list; 102, 103; 102, 104; 103, identifier:out_items_by_key; 104, identifier:out_input_order
def _concat_records(items_by_key, input_order): """Concatenate records into a single key to avoid merging. Handles heterogeneous records that will then be sorted out in the processing fuction. """ all_records = [] for (k, t) in input_order.items(): if t == "record": all_records.append(k) out_items_by_key = utils.deepish_copy(items_by_key) out_input_order = utils.deepish_copy(input_order) if len(all_records) > 1: final_k = all_records[0] final_v = items_by_key[final_k] for k in all_records[1:]: final_v += items_by_key[k] del out_items_by_key[k] del out_input_order[k] out_items_by_key[final_k] = final_v return out_items_by_key, out_input_order
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:_combine_files; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:tsv_files; 5, identifier:work_dir; 6, identifier:data; 7, block; 7, 8; 7, 10; 7, 30; 7, 39; 7, 54; 7, 124; 8, expression_statement; 8, 9; 9, comment; 10, expression_statement; 10, 11; 11, assignment; 11, 12; 11, 13; 12, identifier:header; 13, call; 13, 14; 13, 17; 14, attribute; 14, 15; 14, 16; 15, string:"\t"; 16, identifier:join; 17, argument_list; 17, 18; 18, list:["caller", "sample", "chrom", "start", "end", "svtype", "lof", "annotation", "split_read_support", "paired_support_PE", "paired_support_PR"]; 18, 19; 18, 20; 18, 21; 18, 22; 18, 23; 18, 24; 18, 25; 18, 26; 18, 27; 18, 28; 18, 29; 19, string:"caller"; 20, string:"sample"; 21, string:"chrom"; 22, string:"start"; 23, string:"end"; 24, string:"svtype"; 25, string:"lof"; 26, string:"annotation"; 27, string:"split_read_support"; 28, string:"paired_support_PE"; 29, string:"paired_support_PR"; 30, expression_statement; 30, 31; 31, assignment; 31, 32; 31, 33; 32, identifier:sample; 33, call; 33, 34; 33, 37; 34, attribute; 34, 35; 34, 36; 35, identifier:dd; 36, identifier:get_sample_name; 37, argument_list; 37, 38; 38, identifier:data; 39, expression_statement; 39, 40; 40, assignment; 40, 41; 40, 42; 41, identifier:out_file; 42, call; 42, 43; 42, 48; 43, attribute; 43, 44; 43, 47; 44, attribute; 44, 45; 44, 46; 45, identifier:os; 46, identifier:path; 47, identifier:join; 48, argument_list; 48, 49; 48, 50; 49, identifier:work_dir; 50, binary_operator:%; 50, 51; 50, 52; 51, string:"%s-prioritize.tsv"; 52, parenthesized_expression; 52, 53; 53, identifier:sample; 54, if_statement; 54, 55; 54, 62; 55, not_operator; 55, 56; 56, call; 56, 57; 56, 60; 57, attribute; 57, 58; 57, 59; 58, identifier:utils; 59, identifier:file_exists; 60, argument_list; 60, 61; 61, identifier:out_file; 62, block; 62, 63; 63, with_statement; 63, 64; 63, 74; 64, with_clause; 64, 65; 65, with_item; 65, 66; 66, as_pattern; 66, 67; 66, 72; 67, call; 67, 68; 67, 69; 68, identifier:file_transaction; 69, argument_list; 69, 70; 69, 71; 70, identifier:data; 71, identifier:out_file; 72, as_pattern_target; 72, 73; 73, identifier:tx_out_file; 74, block; 74, 75; 74, 86; 74, 95; 74, 104; 74, 108; 75, expression_statement; 75, 76; 76, assignment; 76, 77; 76, 78; 77, identifier:tmpdir; 78, call; 78, 79; 78, 84; 79, attribute; 79, 80; 79, 83; 80, attribute; 80, 81; 80, 82; 81, identifier:os; 82, identifier:path; 83, identifier:dirname; 84, argument_list; 84, 85; 85, identifier:tx_out_file; 86, expression_statement; 86, 87; 87, assignment; 87, 88; 87, 89; 88, identifier:input_files; 89, call; 89, 90; 89, 93; 90, attribute; 90, 91; 90, 92; 91, string:" "; 92, identifier:join; 93, argument_list; 93, 94; 94, identifier:tsv_files; 95, expression_statement; 95, 96; 96, assignment; 96, 97; 96, 98; 97, identifier:sort_cmd; 98, call; 98, 99; 98, 102; 99, attribute; 99, 100; 99, 101; 100, identifier:bedutils; 101, identifier:get_sort_cmd; 102, argument_list; 102, 103; 103, identifier:tmpdir; 104, expression_statement; 104, 105; 105, assignment; 105, 106; 105, 107; 106, identifier:cmd; 107, string:"{{ echo '{header}'; cat {input_files} | {sort_cmd} -k3,3 -k4,4n; }} > {tx_out_file}"; 108, expression_statement; 108, 109; 109, call; 109, 110; 109, 113; 110, attribute; 110, 111; 110, 112; 111, identifier:do; 112, identifier:run; 113, argument_list; 113, 114; 113, 123; 114, call; 114, 115; 114, 118; 115, attribute; 115, 116; 115, 117; 116, identifier:cmd; 117, identifier:format; 118, argument_list; 118, 119; 119, dictionary_splat; 119, 120; 120, call; 120, 121; 120, 122; 121, identifier:locals; 122, argument_list; 123, string:"Combine prioritized from multiple callers"; 124, return_statement; 124, 125; 125, identifier:out_file
def _combine_files(tsv_files, work_dir, data): """Combine multiple priority tsv files into a final sorted output. """ header = "\t".join(["caller", "sample", "chrom", "start", "end", "svtype", "lof", "annotation", "split_read_support", "paired_support_PE", "paired_support_PR"]) sample = dd.get_sample_name(data) out_file = os.path.join(work_dir, "%s-prioritize.tsv" % (sample)) if not utils.file_exists(out_file): with file_transaction(data, out_file) as tx_out_file: tmpdir = os.path.dirname(tx_out_file) input_files = " ".join(tsv_files) sort_cmd = bedutils.get_sort_cmd(tmpdir) cmd = "{{ echo '{header}'; cat {input_files} | {sort_cmd} -k3,3 -k4,4n; }} > {tx_out_file}" do.run(cmd.format(**locals()), "Combine prioritized from multiple callers") return out_file
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:sort_filenames; 3, parameters; 3, 4; 4, identifier:filenames; 5, block; 5, 6; 5, 8; 5, 23; 5, 47; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:basenames; 11, list_comprehension; 11, 12; 11, 20; 12, call; 12, 13; 12, 18; 13, attribute; 13, 14; 13, 17; 14, attribute; 14, 15; 14, 16; 15, identifier:os; 16, identifier:path; 17, identifier:basename; 18, argument_list; 18, 19; 19, identifier:x; 20, for_in_clause; 20, 21; 20, 22; 21, identifier:x; 22, identifier:filenames; 23, expression_statement; 23, 24; 24, assignment; 24, 25; 24, 26; 25, identifier:indexes; 26, list_comprehension; 26, 27; 26, 30; 27, subscript; 27, 28; 27, 29; 28, identifier:i; 29, integer:0; 30, for_in_clause; 30, 31; 30, 32; 31, identifier:i; 32, call; 32, 33; 32, 34; 33, identifier:sorted; 34, argument_list; 34, 35; 34, 39; 35, call; 35, 36; 35, 37; 36, identifier:enumerate; 37, argument_list; 37, 38; 38, identifier:basenames; 39, keyword_argument; 39, 40; 39, 41; 40, identifier:key; 41, lambda; 41, 42; 41, 44; 42, lambda_parameters; 42, 43; 43, identifier:x; 44, subscript; 44, 45; 44, 46; 45, identifier:x; 46, integer:1; 47, return_statement; 47, 48; 48, list_comprehension; 48, 49; 48, 52; 49, subscript; 49, 50; 49, 51; 50, identifier:filenames; 51, identifier:x; 52, for_in_clause; 52, 53; 52, 54; 53, identifier:x; 54, identifier:indexes
def sort_filenames(filenames): """ sort a list of files by filename only, ignoring the directory names """ basenames = [os.path.basename(x) for x in filenames] indexes = [i[0] for i in sorted(enumerate(basenames), key=lambda x:x[1])] return [filenames[x] for x in indexes]
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:sort_csv; 3, parameters; 3, 4; 4, identifier:in_file; 5, block; 5, 6; 5, 8; 5, 14; 5, 75; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:out_file; 11, binary_operator:%; 11, 12; 11, 13; 12, string:"%s.sort"; 13, identifier:in_file; 14, if_statement; 14, 15; 14, 36; 15, not_operator; 15, 16; 16, parenthesized_expression; 16, 17; 17, boolean_operator:and; 17, 18; 17, 26; 18, call; 18, 19; 18, 24; 19, attribute; 19, 20; 19, 23; 20, attribute; 20, 21; 20, 22; 21, identifier:os; 22, identifier:path; 23, identifier:exists; 24, argument_list; 24, 25; 25, identifier:out_file; 26, comparison_operator:>; 26, 27; 26, 35; 27, call; 27, 28; 27, 33; 28, attribute; 28, 29; 28, 32; 29, attribute; 29, 30; 29, 31; 30, identifier:os; 31, identifier:path; 32, identifier:getsize; 33, argument_list; 33, 34; 34, identifier:out_file; 35, integer:0; 36, block; 36, 37; 36, 45; 37, expression_statement; 37, 38; 38, assignment; 38, 39; 38, 40; 39, identifier:cl; 40, list:["sort", "-k", "1,1", in_file]; 40, 41; 40, 42; 40, 43; 40, 44; 41, string:"sort"; 42, string:"-k"; 43, string:"1,1"; 44, identifier:in_file; 45, with_statement; 45, 46; 45, 56; 46, with_clause; 46, 47; 47, with_item; 47, 48; 48, as_pattern; 48, 49; 48, 54; 49, call; 49, 50; 49, 51; 50, identifier:open; 51, argument_list; 51, 52; 51, 53; 52, identifier:out_file; 53, string:"w"; 54, as_pattern_target; 54, 55; 55, identifier:out_handle; 56, block; 56, 57; 56, 69; 57, expression_statement; 57, 58; 58, assignment; 58, 59; 58, 60; 59, identifier:child; 60, call; 60, 61; 60, 64; 61, attribute; 61, 62; 61, 63; 62, identifier:subprocess; 63, identifier:Popen; 64, argument_list; 64, 65; 64, 66; 65, identifier:cl; 66, keyword_argument; 66, 67; 66, 68; 67, identifier:stdout; 68, identifier:out_handle; 69, expression_statement; 69, 70; 70, call; 70, 71; 70, 74; 71, attribute; 71, 72; 71, 73; 72, identifier:child; 73, identifier:wait; 74, argument_list; 75, return_statement; 75, 76; 76, identifier:out_file
def sort_csv(in_file): """Sort a CSV file by read name, allowing direct comparison. """ out_file = "%s.sort" % in_file if not (os.path.exists(out_file) and os.path.getsize(out_file) > 0): cl = ["sort", "-k", "1,1", in_file] with open(out_file, "w") as out_handle: child = subprocess.Popen(cl, stdout=out_handle) child.wait() return out_file
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:hydra_to_vcf_writer; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 4, identifier:hydra_file; 5, identifier:genome_2bit; 6, identifier:options; 7, identifier:out_handle; 8, block; 8, 9; 8, 11; 8, 16; 8, 28; 8, 41; 9, expression_statement; 9, 10; 10, comment; 11, expression_statement; 11, 12; 12, call; 12, 13; 12, 14; 13, identifier:_write_vcf_header; 14, argument_list; 14, 15; 15, identifier:out_handle; 16, expression_statement; 16, 17; 17, assignment; 17, 18; 17, 19; 18, identifier:brends; 19, call; 19, 20; 19, 21; 20, identifier:list; 21, argument_list; 21, 22; 22, call; 22, 23; 22, 24; 23, identifier:_get_vcf_breakends; 24, argument_list; 24, 25; 24, 26; 24, 27; 25, identifier:hydra_file; 26, identifier:genome_2bit; 27, identifier:options; 28, expression_statement; 28, 29; 29, call; 29, 30; 29, 33; 30, attribute; 30, 31; 30, 32; 31, identifier:brends; 32, identifier:sort; 33, argument_list; 33, 34; 34, keyword_argument; 34, 35; 34, 36; 35, identifier:key; 36, call; 36, 37; 36, 38; 37, identifier:attrgetter; 38, argument_list; 38, 39; 38, 40; 39, string:"chrom"; 40, string:"pos"; 41, for_statement; 41, 42; 41, 43; 41, 44; 42, identifier:brend; 43, identifier:brends; 44, block; 44, 45; 45, expression_statement; 45, 46; 46, call; 46, 47; 46, 48; 47, identifier:_write_vcf_breakend; 48, argument_list; 48, 49; 48, 50; 49, identifier:brend; 50, identifier:out_handle
def hydra_to_vcf_writer(hydra_file, genome_2bit, options, out_handle): """Write hydra output as sorted VCF file. Requires loading the hydra file into memory to perform sorting on output VCF. Could generalize this to no sorting or by-chromosome approach if this proves too memory intensive. """ _write_vcf_header(out_handle) brends = list(_get_vcf_breakends(hydra_file, genome_2bit, options)) brends.sort(key=attrgetter("chrom", "pos")) for brend in brends: _write_vcf_breakend(brend, out_handle)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:_split_by_ready_regions; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:ext; 5, identifier:file_key; 6, identifier:dir_ext_fn; 7, block; 7, 8; 7, 10; 7, 32; 7, 107; 7, 255; 8, expression_statement; 8, 9; 9, comment; 10, function_definition; 10, 11; 10, 12; 10, 14; 11, function_name:_sort_by_size; 12, parameters; 12, 13; 13, identifier:region_w_bams; 14, block; 14, 15; 14, 21; 14, 28; 15, expression_statement; 15, 16; 16, assignment; 16, 17; 16, 20; 17, pattern_list; 17, 18; 17, 19; 18, identifier:region; 19, identifier:_; 20, identifier:region_w_bams; 21, expression_statement; 21, 22; 22, assignment; 22, 23; 22, 27; 23, pattern_list; 23, 24; 23, 25; 23, 26; 24, identifier:_; 25, identifier:start; 26, identifier:end; 27, identifier:region; 28, return_statement; 28, 29; 29, binary_operator:-; 29, 30; 29, 31; 30, identifier:end; 31, identifier:start; 32, function_definition; 32, 33; 32, 34; 32, 36; 33, function_name:_assign_bams_to_regions; 34, parameters; 34, 35; 35, identifier:data; 36, block; 36, 37; 36, 39; 37, expression_statement; 37, 38; 38, comment; 39, for_statement; 39, 40; 39, 43; 39, 49; 40, pattern_list; 40, 41; 40, 42; 41, identifier:i; 42, identifier:region; 43, call; 43, 44; 43, 45; 44, identifier:enumerate; 45, argument_list; 45, 46; 46, subscript; 46, 47; 46, 48; 47, identifier:data; 48, string:"region"; 49, block; 49, 50; 49, 54; 49, 88; 49, 102; 50, expression_statement; 50, 51; 51, assignment; 51, 52; 51, 53; 52, identifier:work_bams; 53, list:[]; 54, for_statement; 54, 55; 54, 56; 54, 59; 55, identifier:xs; 56, subscript; 56, 57; 56, 58; 57, identifier:data; 58, string:"region_bams"; 59, block; 59, 60; 60, if_statement; 60, 61; 60, 67; 60, 77; 61, comparison_operator:==; 61, 62; 61, 66; 62, call; 62, 63; 62, 64; 63, identifier:len; 64, argument_list; 64, 65; 65, identifier:xs; 66, integer:1; 67, block; 67, 68; 68, expression_statement; 68, 69; 69, call; 69, 70; 69, 73; 70, attribute; 70, 71; 70, 72; 71, identifier:work_bams; 72, identifier:append; 73, argument_list; 73, 74; 74, subscript; 74, 75; 74, 76; 75, identifier:xs; 76, integer:0; 77, else_clause; 77, 78; 78, block; 78, 79; 79, expression_statement; 79, 80; 80, call; 80, 81; 80, 84; 81, attribute; 81, 82; 81, 83; 82, identifier:work_bams; 83, identifier:append; 84, argument_list; 84, 85; 85, subscript; 85, 86; 85, 87; 86, identifier:xs; 87, identifier:i; 88, for_statement; 88, 89; 88, 90; 88, 91; 89, identifier:work_bam; 90, identifier:work_bams; 91, block; 91, 92; 92, assert_statement; 92, 93; 92, 101; 93, call; 93, 94; 93, 99; 94, attribute; 94, 95; 94, 98; 95, attribute; 95, 96; 95, 97; 96, identifier:os; 97, identifier:path; 98, identifier:exists; 99, argument_list; 99, 100; 100, identifier:work_bam; 101, identifier:work_bam; 102, expression_statement; 102, 103; 103, yield; 103, 104; 104, expression_list; 104, 105; 104, 106; 105, identifier:region; 106, identifier:work_bams; 107, function_definition; 107, 108; 107, 109; 107, 111; 108, function_name:_do_work; 109, parameters; 109, 110; 110, identifier:data; 111, block; 111, 112; 112, if_statement; 112, 113; 112, 116; 112, 249; 113, comparison_operator:in; 113, 114; 113, 115; 114, string:"region"; 115, identifier:data; 116, block; 116, 117; 116, 132; 116, 151; 116, 167; 116, 177; 116, 181; 116, 245; 117, expression_statement; 117, 118; 118, assignment; 118, 119; 118, 120; 119, identifier:name; 120, conditional_expression:if; 120, 121; 120, 126; 120, 129; 121, subscript; 121, 122; 121, 125; 122, subscript; 122, 123; 122, 124; 123, identifier:data; 124, string:"group"; 125, integer:0; 126, comparison_operator:in; 126, 127; 126, 128; 127, string:"group"; 128, identifier:data; 129, subscript; 129, 130; 129, 131; 130, identifier:data; 131, string:"description"; 132, expression_statement; 132, 133; 133, assignment; 133, 134; 133, 135; 134, identifier:out_dir; 135, call; 135, 136; 135, 141; 136, attribute; 136, 137; 136, 140; 137, attribute; 137, 138; 137, 139; 138, identifier:os; 139, identifier:path; 140, identifier:join; 141, argument_list; 141, 142; 141, 147; 142, subscript; 142, 143; 142, 146; 143, subscript; 143, 144; 143, 145; 144, identifier:data; 145, string:"dirs"; 146, string:"work"; 147, call; 147, 148; 147, 149; 148, identifier:dir_ext_fn; 149, argument_list; 149, 150; 150, identifier:data; 151, expression_statement; 151, 152; 152, assignment; 152, 153; 152, 154; 153, identifier:out_file; 154, call; 154, 155; 154, 160; 155, attribute; 155, 156; 155, 159; 156, attribute; 156, 157; 156, 158; 157, identifier:os; 158, identifier:path; 159, identifier:join; 160, argument_list; 160, 161; 160, 162; 161, identifier:out_dir; 162, binary_operator:%; 162, 163; 162, 164; 163, string:"%s%s"; 164, tuple; 164, 165; 164, 166; 165, identifier:name; 166, identifier:ext; 167, assert_statement; 167, 168; 168, call; 168, 169; 168, 170; 169, identifier:isinstance; 170, argument_list; 170, 171; 170, 174; 171, subscript; 171, 172; 171, 173; 172, identifier:data; 173, string:"region"; 174, tuple; 174, 175; 174, 176; 175, identifier:list; 176, identifier:tuple; 177, expression_statement; 177, 178; 178, assignment; 178, 179; 178, 180; 179, identifier:out_parts; 180, list:[]; 181, for_statement; 181, 182; 181, 185; 181, 198; 182, pattern_list; 182, 183; 182, 184; 183, identifier:r; 184, identifier:work_bams; 185, call; 185, 186; 185, 187; 186, identifier:sorted; 187, argument_list; 187, 188; 187, 192; 187, 195; 188, call; 188, 189; 188, 190; 189, identifier:_assign_bams_to_regions; 190, argument_list; 190, 191; 191, identifier:data; 192, keyword_argument; 192, 193; 192, 194; 193, identifier:key; 194, identifier:_sort_by_size; 195, keyword_argument; 195, 196; 195, 197; 196, identifier:reverse; 197, True; 198, block; 198, 199; 198, 213; 198, 235; 199, expression_statement; 199, 200; 200, assignment; 200, 201; 200, 202; 201, identifier:out_region_dir; 202, call; 202, 203; 202, 208; 203, attribute; 203, 204; 203, 207; 204, attribute; 204, 205; 204, 206; 205, identifier:os; 206, identifier:path; 207, identifier:join; 208, argument_list; 208, 209; 208, 210; 209, identifier:out_dir; 210, subscript; 210, 211; 210, 212; 211, identifier:r; 212, integer:0; 213, expression_statement; 213, 214; 214, assignment; 214, 215; 214, 216; 215, identifier:out_region_file; 216, call; 216, 217; 216, 222; 217, attribute; 217, 218; 217, 221; 218, attribute; 218, 219; 218, 220; 219, identifier:os; 220, identifier:path; 221, identifier:join; 222, argument_list; 222, 223; 222, 224; 223, identifier:out_region_dir; 224, binary_operator:%; 224, 225; 224, 226; 225, string:"%s-%s%s"; 226, tuple; 226, 227; 226, 228; 226, 234; 227, identifier:name; 228, call; 228, 229; 228, 232; 229, attribute; 229, 230; 229, 231; 230, identifier:pregion; 231, identifier:to_safestr; 232, argument_list; 232, 233; 233, identifier:r; 234, identifier:ext; 235, expression_statement; 235, 236; 236, call; 236, 237; 236, 240; 237, attribute; 237, 238; 237, 239; 238, identifier:out_parts; 239, identifier:append; 240, argument_list; 240, 241; 241, tuple; 241, 242; 241, 243; 241, 244; 242, identifier:r; 243, identifier:work_bams; 244, identifier:out_region_file; 245, return_statement; 245, 246; 246, expression_list; 246, 247; 246, 248; 247, identifier:out_file; 248, identifier:out_parts; 249, else_clause; 249, 250; 250, block; 250, 251; 251, return_statement; 251, 252; 252, expression_list; 252, 253; 252, 254; 253, None; 254, list:[]; 255, return_statement; 255, 256; 256, identifier:_do_work
def _split_by_ready_regions(ext, file_key, dir_ext_fn): """Organize splits based on regions generated by parallel_prep_region. Sort splits so largest regions analyzed first, avoiding potentially lagging runs at end. """ def _sort_by_size(region_w_bams): region, _ = region_w_bams _, start, end = region return end - start def _assign_bams_to_regions(data): """Ensure BAMs aligned with input regions, either global or individual. """ for i, region in enumerate(data["region"]): work_bams = [] for xs in data["region_bams"]: if len(xs) == 1: work_bams.append(xs[0]) else: work_bams.append(xs[i]) for work_bam in work_bams: assert os.path.exists(work_bam), work_bam yield region, work_bams def _do_work(data): if "region" in data: name = data["group"][0] if "group" in data else data["description"] out_dir = os.path.join(data["dirs"]["work"], dir_ext_fn(data)) out_file = os.path.join(out_dir, "%s%s" % (name, ext)) assert isinstance(data["region"], (list, tuple)) out_parts = [] for r, work_bams in sorted(_assign_bams_to_regions(data), key=_sort_by_size, reverse=True): out_region_dir = os.path.join(out_dir, r[0]) out_region_file = os.path.join(out_region_dir, "%s-%s%s" % (name, pregion.to_safestr(r), ext)) out_parts.append((r, work_bams, out_region_file)) return out_file, out_parts else: return None, [] return _do_work
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:_combine_variants; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 4, identifier:in_vcfs; 5, identifier:out_file; 6, identifier:ref_file; 7, identifier:config; 8, block; 8, 9; 8, 11; 8, 17; 8, 21; 8, 141; 9, expression_statement; 9, 10; 10, comment; 11, expression_statement; 11, 12; 12, call; 12, 13; 12, 16; 13, attribute; 13, 14; 13, 15; 14, identifier:in_vcfs; 15, identifier:sort; 16, argument_list; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 20; 19, identifier:wrote_header; 20, False; 21, with_statement; 21, 22; 21, 32; 22, with_clause; 22, 23; 23, with_item; 23, 24; 24, as_pattern; 24, 25; 24, 30; 25, call; 25, 26; 25, 27; 26, identifier:open; 27, argument_list; 27, 28; 27, 29; 28, identifier:out_file; 29, string:"w"; 30, as_pattern_target; 30, 31; 31, identifier:out_handle; 32, block; 32, 33; 32, 125; 33, for_statement; 33, 34; 33, 35; 33, 43; 34, identifier:in_vcf; 35, generator_expression; 35, 36; 35, 40; 36, subscript; 36, 37; 36, 38; 37, identifier:x; 38, unary_operator:-; 38, 39; 39, integer:1; 40, for_in_clause; 40, 41; 40, 42; 41, identifier:x; 42, identifier:in_vcfs; 43, block; 43, 44; 44, with_statement; 44, 45; 44, 54; 45, with_clause; 45, 46; 46, with_item; 46, 47; 47, as_pattern; 47, 48; 47, 52; 48, call; 48, 49; 48, 50; 49, identifier:open; 50, argument_list; 50, 51; 51, identifier:in_vcf; 52, as_pattern_target; 52, 53; 53, identifier:in_handle; 54, block; 54, 55; 54, 76; 54, 94; 55, expression_statement; 55, 56; 56, assignment; 56, 57; 56, 58; 57, identifier:header; 58, call; 58, 59; 58, 60; 59, identifier:list; 60, argument_list; 60, 61; 61, call; 61, 62; 61, 65; 62, attribute; 62, 63; 62, 64; 63, identifier:itertools; 64, identifier:takewhile; 65, argument_list; 65, 66; 65, 75; 66, lambda; 66, 67; 66, 69; 67, lambda_parameters; 67, 68; 68, identifier:x; 69, call; 69, 70; 69, 73; 70, attribute; 70, 71; 70, 72; 71, identifier:x; 72, identifier:startswith; 73, argument_list; 73, 74; 74, string:"#"; 75, identifier:in_handle; 76, if_statement; 76, 77; 76, 86; 77, not_operator; 77, 78; 78, call; 78, 79; 78, 84; 79, attribute; 79, 80; 79, 83; 80, subscript; 80, 81; 80, 82; 81, identifier:header; 82, integer:0; 83, identifier:startswith; 84, argument_list; 84, 85; 85, string:"##fileformat=VCFv4"; 86, block; 86, 87; 87, raise_statement; 87, 88; 88, call; 88, 89; 88, 90; 89, identifier:ValueError; 90, argument_list; 90, 91; 91, binary_operator:%; 91, 92; 91, 93; 92, string:"Unexpected VCF file: %s"; 93, identifier:in_vcf; 94, for_statement; 94, 95; 94, 96; 94, 97; 95, identifier:line; 96, identifier:in_handle; 97, block; 97, 98; 97, 118; 98, if_statement; 98, 99; 98, 101; 99, not_operator; 99, 100; 100, identifier:wrote_header; 101, block; 101, 102; 101, 106; 102, expression_statement; 102, 103; 103, assignment; 103, 104; 103, 105; 104, identifier:wrote_header; 105, True; 106, expression_statement; 106, 107; 107, call; 107, 108; 107, 111; 108, attribute; 108, 109; 108, 110; 109, identifier:out_handle; 110, identifier:write; 111, argument_list; 111, 112; 112, call; 112, 113; 112, 116; 113, attribute; 113, 114; 113, 115; 114, string:""; 115, identifier:join; 116, argument_list; 116, 117; 117, identifier:header; 118, expression_statement; 118, 119; 119, call; 119, 120; 119, 123; 120, attribute; 120, 121; 120, 122; 121, identifier:out_handle; 122, identifier:write; 123, argument_list; 123, 124; 124, identifier:line; 125, if_statement; 125, 126; 125, 128; 126, not_operator; 126, 127; 127, identifier:wrote_header; 128, block; 128, 129; 129, expression_statement; 129, 130; 130, call; 130, 131; 130, 134; 131, attribute; 131, 132; 131, 133; 132, identifier:out_handle; 133, identifier:write; 134, argument_list; 134, 135; 135, call; 135, 136; 135, 139; 136, attribute; 136, 137; 136, 138; 137, string:""; 138, identifier:join; 139, argument_list; 139, 140; 140, identifier:header; 141, return_statement; 141, 142; 142, identifier:out_file
def _combine_variants(in_vcfs, out_file, ref_file, config): """Combine variant files, writing the header from the first non-empty input. in_vcfs is a list with each item starting with the chromosome regions, and ending with the input file. We sort by these regions to ensure the output file is in the expected order. """ in_vcfs.sort() wrote_header = False with open(out_file, "w") as out_handle: for in_vcf in (x[-1] for x in in_vcfs): with open(in_vcf) as in_handle: header = list(itertools.takewhile(lambda x: x.startswith("#"), in_handle)) if not header[0].startswith("##fileformat=VCFv4"): raise ValueError("Unexpected VCF file: %s" % in_vcf) for line in in_handle: if not wrote_header: wrote_header = True out_handle.write("".join(header)) out_handle.write(line) if not wrote_header: out_handle.write("".join(header)) return out_file
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 18; 2, function_name:picard_sort; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 3, 12; 3, 15; 4, identifier:picard; 5, identifier:align_bam; 6, default_parameter; 6, 7; 6, 8; 7, identifier:sort_order; 8, string:"coordinate"; 9, default_parameter; 9, 10; 9, 11; 10, identifier:out_file; 11, None; 12, default_parameter; 12, 13; 12, 14; 13, identifier:compression_level; 14, None; 15, default_parameter; 15, 16; 15, 17; 16, identifier:pipe; 17, False; 18, block; 18, 19; 18, 21; 18, 34; 18, 47; 18, 123; 19, expression_statement; 19, 20; 20, comment; 21, expression_statement; 21, 22; 22, assignment; 22, 23; 22, 26; 23, pattern_list; 23, 24; 23, 25; 24, identifier:base; 25, identifier:ext; 26, call; 26, 27; 26, 32; 27, attribute; 27, 28; 27, 31; 28, attribute; 28, 29; 28, 30; 29, identifier:os; 30, identifier:path; 31, identifier:splitext; 32, argument_list; 32, 33; 33, identifier:align_bam; 34, if_statement; 34, 35; 34, 38; 35, comparison_operator:is; 35, 36; 35, 37; 36, identifier:out_file; 37, None; 38, block; 38, 39; 39, expression_statement; 39, 40; 40, assignment; 40, 41; 40, 42; 41, identifier:out_file; 42, binary_operator:%; 42, 43; 42, 44; 43, string:"%s-sort%s"; 44, tuple; 44, 45; 44, 46; 45, identifier:base; 46, identifier:ext; 47, if_statement; 47, 48; 47, 53; 48, not_operator; 48, 49; 49, call; 49, 50; 49, 51; 50, identifier:file_exists; 51, argument_list; 51, 52; 52, identifier:out_file; 53, block; 53, 54; 54, with_statement; 54, 55; 54, 66; 55, with_clause; 55, 56; 56, with_item; 56, 57; 57, as_pattern; 57, 58; 57, 64; 58, call; 58, 59; 58, 60; 59, identifier:tx_tmpdir; 60, argument_list; 60, 61; 61, attribute; 61, 62; 61, 63; 62, identifier:picard; 63, identifier:_config; 64, as_pattern_target; 64, 65; 65, identifier:tmp_dir; 66, block; 66, 67; 67, with_statement; 67, 68; 67, 80; 68, with_clause; 68, 69; 69, with_item; 69, 70; 70, as_pattern; 70, 71; 70, 78; 71, call; 71, 72; 71, 73; 72, identifier:file_transaction; 73, argument_list; 73, 74; 73, 77; 74, attribute; 74, 75; 74, 76; 75, identifier:picard; 76, identifier:_config; 77, identifier:out_file; 78, as_pattern_target; 78, 79; 79, identifier:tx_out_file; 80, block; 80, 81; 80, 100; 80, 112; 81, expression_statement; 81, 82; 82, assignment; 82, 83; 82, 84; 83, identifier:opts; 84, list:[("INPUT", align_bam), ("OUTPUT", out_file if pipe else tx_out_file), ("TMP_DIR", tmp_dir), ("SORT_ORDER", sort_order)]; 84, 85; 84, 88; 84, 94; 84, 97; 85, tuple; 85, 86; 85, 87; 86, string:"INPUT"; 87, identifier:align_bam; 88, tuple; 88, 89; 88, 90; 89, string:"OUTPUT"; 90, conditional_expression:if; 90, 91; 90, 92; 90, 93; 91, identifier:out_file; 92, identifier:pipe; 93, identifier:tx_out_file; 94, tuple; 94, 95; 94, 96; 95, string:"TMP_DIR"; 96, identifier:tmp_dir; 97, tuple; 97, 98; 97, 99; 98, string:"SORT_ORDER"; 99, identifier:sort_order; 100, if_statement; 100, 101; 100, 102; 101, identifier:compression_level; 102, block; 102, 103; 103, expression_statement; 103, 104; 104, call; 104, 105; 104, 108; 105, attribute; 105, 106; 105, 107; 106, identifier:opts; 107, identifier:append; 108, argument_list; 108, 109; 109, tuple; 109, 110; 109, 111; 110, string:"COMPRESSION_LEVEL"; 111, identifier:compression_level; 112, expression_statement; 112, 113; 113, call; 113, 114; 113, 117; 114, attribute; 114, 115; 114, 116; 115, identifier:picard; 116, identifier:run; 117, argument_list; 117, 118; 117, 119; 117, 120; 118, string:"SortSam"; 119, identifier:opts; 120, keyword_argument; 120, 121; 120, 122; 121, identifier:pipe; 122, identifier:pipe; 123, return_statement; 123, 124; 124, identifier:out_file
def picard_sort(picard, align_bam, sort_order="coordinate", out_file=None, compression_level=None, pipe=False): """Sort a BAM file by coordinates. """ base, ext = os.path.splitext(align_bam) if out_file is None: out_file = "%s-sort%s" % (base, ext) if not file_exists(out_file): with tx_tmpdir(picard._config) as tmp_dir: with file_transaction(picard._config, out_file) as tx_out_file: opts = [("INPUT", align_bam), ("OUTPUT", out_file if pipe else tx_out_file), ("TMP_DIR", tmp_dir), ("SORT_ORDER", sort_order)] if compression_level: opts.append(("COMPRESSION_LEVEL", compression_level)) picard.run("SortSam", opts, pipe=pipe) return out_file
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:picard_fix_rgs; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:picard; 5, identifier:in_bam; 6, identifier:names; 7, block; 7, 8; 7, 10; 7, 25; 7, 112; 8, expression_statement; 8, 9; 9, comment; 10, expression_statement; 10, 11; 11, assignment; 11, 12; 11, 13; 12, identifier:out_file; 13, binary_operator:%; 13, 14; 13, 15; 14, string:"%s-fixrgs.bam"; 15, subscript; 15, 16; 15, 24; 16, call; 16, 17; 16, 22; 17, attribute; 17, 18; 17, 21; 18, attribute; 18, 19; 18, 20; 19, identifier:os; 20, identifier:path; 21, identifier:splitext; 22, argument_list; 22, 23; 23, identifier:in_bam; 24, integer:0; 25, if_statement; 25, 26; 25, 31; 26, not_operator; 26, 27; 27, call; 27, 28; 27, 29; 28, identifier:file_exists; 29, argument_list; 29, 30; 30, identifier:out_file; 31, block; 31, 32; 32, with_statement; 32, 33; 32, 44; 33, with_clause; 33, 34; 34, with_item; 34, 35; 35, as_pattern; 35, 36; 35, 42; 36, call; 36, 37; 36, 38; 37, identifier:tx_tmpdir; 38, argument_list; 38, 39; 39, attribute; 39, 40; 39, 41; 40, identifier:picard; 41, identifier:_config; 42, as_pattern_target; 42, 43; 43, identifier:tmp_dir; 44, block; 44, 45; 45, with_statement; 45, 46; 45, 58; 46, with_clause; 46, 47; 47, with_item; 47, 48; 48, as_pattern; 48, 49; 48, 56; 49, call; 49, 50; 49, 51; 50, identifier:file_transaction; 51, argument_list; 51, 52; 51, 55; 52, attribute; 52, 53; 52, 54; 53, identifier:picard; 54, identifier:_config; 55, identifier:out_file; 56, as_pattern_target; 56, 57; 57, identifier:tx_out_file; 58, block; 58, 59; 58, 104; 59, expression_statement; 59, 60; 60, assignment; 60, 61; 60, 62; 61, identifier:opts; 62, list:[("INPUT", in_bam), ("OUTPUT", tx_out_file), ("SORT_ORDER", "coordinate"), ("RGID", names["rg"]), ("RGLB", names.get("lb", "unknown")), ("RGPL", names["pl"]), ("RGPU", names["pu"]), ("RGSM", names["sample"]), ("TMP_DIR", tmp_dir)]; 62, 63; 62, 66; 62, 69; 62, 72; 62, 77; 62, 86; 62, 91; 62, 96; 62, 101; 63, tuple; 63, 64; 63, 65; 64, string:"INPUT"; 65, identifier:in_bam; 66, tuple; 66, 67; 66, 68; 67, string:"OUTPUT"; 68, identifier:tx_out_file; 69, tuple; 69, 70; 69, 71; 70, string:"SORT_ORDER"; 71, string:"coordinate"; 72, tuple; 72, 73; 72, 74; 73, string:"RGID"; 74, subscript; 74, 75; 74, 76; 75, identifier:names; 76, string:"rg"; 77, tuple; 77, 78; 77, 79; 78, string:"RGLB"; 79, call; 79, 80; 79, 83; 80, attribute; 80, 81; 80, 82; 81, identifier:names; 82, identifier:get; 83, argument_list; 83, 84; 83, 85; 84, string:"lb"; 85, string:"unknown"; 86, tuple; 86, 87; 86, 88; 87, string:"RGPL"; 88, subscript; 88, 89; 88, 90; 89, identifier:names; 90, string:"pl"; 91, tuple; 91, 92; 91, 93; 92, string:"RGPU"; 93, subscript; 93, 94; 93, 95; 94, identifier:names; 95, string:"pu"; 96, tuple; 96, 97; 96, 98; 97, string:"RGSM"; 98, subscript; 98, 99; 98, 100; 99, identifier:names; 100, string:"sample"; 101, tuple; 101, 102; 101, 103; 102, string:"TMP_DIR"; 103, identifier:tmp_dir; 104, expression_statement; 104, 105; 105, call; 105, 106; 105, 109; 106, attribute; 106, 107; 106, 108; 107, identifier:picard; 108, identifier:run; 109, argument_list; 109, 110; 109, 111; 110, string:"AddOrReplaceReadGroups"; 111, identifier:opts; 112, return_statement; 112, 113; 113, identifier:out_file
def picard_fix_rgs(picard, in_bam, names): """Add read group information to BAM files and coordinate sort. """ out_file = "%s-fixrgs.bam" % os.path.splitext(in_bam)[0] if not file_exists(out_file): with tx_tmpdir(picard._config) as tmp_dir: with file_transaction(picard._config, out_file) as tx_out_file: opts = [("INPUT", in_bam), ("OUTPUT", tx_out_file), ("SORT_ORDER", "coordinate"), ("RGID", names["rg"]), ("RGLB", names.get("lb", "unknown")), ("RGPL", names["pl"]), ("RGPU", names["pu"]), ("RGSM", names["sample"]), ("TMP_DIR", tmp_dir)] picard.run("AddOrReplaceReadGroups", opts) return out_file
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:_enforce_max_region_size; 3, parameters; 3, 4; 3, 5; 4, identifier:in_file; 5, identifier:data; 6, block; 6, 7; 6, 9; 6, 13; 6, 17; 6, 43; 6, 54; 6, 122; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:max_size; 12, integer:20000; 13, expression_statement; 13, 14; 14, assignment; 14, 15; 14, 16; 15, identifier:overlap_size; 16, integer:250; 17, function_definition; 17, 18; 17, 19; 17, 21; 18, function_name:_has_larger_regions; 19, parameters; 19, 20; 20, identifier:f; 21, block; 21, 22; 22, return_statement; 22, 23; 23, call; 23, 24; 23, 25; 24, identifier:any; 25, generator_expression; 25, 26; 25, 35; 26, comparison_operator:>; 26, 27; 26, 34; 27, binary_operator:-; 27, 28; 27, 31; 28, attribute; 28, 29; 28, 30; 29, identifier:r; 30, identifier:stop; 31, attribute; 31, 32; 31, 33; 32, identifier:r; 33, identifier:start; 34, identifier:max_size; 35, for_in_clause; 35, 36; 35, 37; 36, identifier:r; 37, call; 37, 38; 37, 41; 38, attribute; 38, 39; 38, 40; 39, identifier:pybedtools; 40, identifier:BedTool; 41, argument_list; 41, 42; 42, identifier:f; 43, expression_statement; 43, 44; 44, assignment; 44, 45; 44, 46; 45, identifier:out_file; 46, binary_operator:%; 46, 47; 46, 48; 47, string:"%s-regionlimit%s"; 48, call; 48, 49; 48, 52; 49, attribute; 49, 50; 49, 51; 50, identifier:utils; 51, identifier:splitext_plus; 52, argument_list; 52, 53; 53, identifier:in_file; 54, if_statement; 54, 55; 54, 62; 55, not_operator; 55, 56; 56, call; 56, 57; 56, 60; 57, attribute; 57, 58; 57, 59; 58, identifier:utils; 59, identifier:file_exists; 60, argument_list; 60, 61; 61, identifier:out_file; 62, block; 62, 63; 63, if_statement; 63, 64; 63, 68; 63, 112; 64, call; 64, 65; 64, 66; 65, identifier:_has_larger_regions; 66, argument_list; 66, 67; 67, identifier:in_file; 68, block; 68, 69; 69, with_statement; 69, 70; 69, 80; 70, with_clause; 70, 71; 71, with_item; 71, 72; 72, as_pattern; 72, 73; 72, 78; 73, call; 73, 74; 73, 75; 74, identifier:file_transaction; 75, argument_list; 75, 76; 75, 77; 76, identifier:data; 77, identifier:out_file; 78, as_pattern_target; 78, 79; 79, identifier:tx_out_file; 80, block; 80, 81; 81, expression_statement; 81, 82; 82, call; 82, 83; 82, 110; 83, attribute; 83, 84; 83, 109; 84, call; 84, 85; 84, 92; 85, attribute; 85, 86; 85, 91; 86, call; 86, 87; 86, 90; 87, attribute; 87, 88; 87, 89; 88, identifier:pybedtools; 89, identifier:BedTool; 90, argument_list; 91, identifier:window_maker; 92, argument_list; 92, 93; 92, 96; 92, 101; 93, keyword_argument; 93, 94; 93, 95; 94, identifier:w; 95, identifier:max_size; 96, keyword_argument; 96, 97; 96, 98; 97, identifier:s; 98, binary_operator:-; 98, 99; 98, 100; 99, identifier:max_size; 100, identifier:overlap_size; 101, keyword_argument; 101, 102; 101, 103; 102, identifier:b; 103, call; 103, 104; 103, 107; 104, attribute; 104, 105; 104, 106; 105, identifier:pybedtools; 106, identifier:BedTool; 107, argument_list; 107, 108; 108, identifier:in_file; 109, identifier:saveas; 110, argument_list; 110, 111; 111, identifier:tx_out_file; 112, else_clause; 112, 113; 113, block; 113, 114; 114, expression_statement; 114, 115; 115, call; 115, 116; 115, 119; 116, attribute; 116, 117; 116, 118; 117, identifier:utils; 118, identifier:symlink_plus; 119, argument_list; 119, 120; 119, 121; 120, identifier:in_file; 121, identifier:out_file; 122, return_statement; 122, 123; 123, identifier:out_file
def _enforce_max_region_size(in_file, data): """Ensure we don't have any chunks in the region greater than 20kb. VarDict memory usage depends on size of individual windows in the input file. This breaks regions into 20kb chunks with 250bp overlaps. 20kb gives ~1Gb/core memory usage and the overlaps avoid missing indels spanning a gap. Downstream VarDict merging sorts out any variants across windows. https://github.com/AstraZeneca-NGS/VarDictJava/issues/64 """ max_size = 20000 overlap_size = 250 def _has_larger_regions(f): return any(r.stop - r.start > max_size for r in pybedtools.BedTool(f)) out_file = "%s-regionlimit%s" % utils.splitext_plus(in_file) if not utils.file_exists(out_file): if _has_larger_regions(in_file): with file_transaction(data, out_file) as tx_out_file: pybedtools.BedTool().window_maker(w=max_size, s=max_size - overlap_size, b=pybedtools.BedTool(in_file)).saveas(tx_out_file) else: utils.symlink_plus(in_file, out_file) return out_file
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 10; 2, function_name:_prep_vrn_file; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 3, 9; 4, identifier:in_file; 5, identifier:vcaller; 6, identifier:work_dir; 7, identifier:somatic_info; 8, identifier:ignore_file; 9, identifier:config; 10, block; 10, 11; 10, 13; 10, 43; 10, 73; 10, 304; 11, expression_statement; 11, 12; 12, comment; 13, if_statement; 13, 14; 13, 20; 13, 25; 13, 34; 14, call; 14, 15; 14, 18; 15, attribute; 15, 16; 15, 17; 16, identifier:vcaller; 17, identifier:startswith; 18, argument_list; 18, 19; 19, string:"vardict"; 20, block; 20, 21; 21, expression_statement; 21, 22; 22, assignment; 22, 23; 22, 24; 23, identifier:variant_type; 24, string:"vardict"; 25, elif_clause; 25, 26; 25, 29; 26, comparison_operator:==; 26, 27; 26, 28; 27, identifier:vcaller; 28, string:"mutect"; 29, block; 29, 30; 30, expression_statement; 30, 31; 31, assignment; 31, 32; 31, 33; 32, identifier:variant_type; 33, string:"mutect-smchet"; 34, else_clause; 34, 35; 35, block; 35, 36; 36, raise_statement; 36, 37; 37, call; 37, 38; 37, 39; 38, identifier:ValueError; 39, argument_list; 39, 40; 40, binary_operator:%; 40, 41; 40, 42; 41, string:"Unexpected variant caller for PhyloWGS prep: %s"; 42, identifier:vcaller; 43, expression_statement; 43, 44; 44, assignment; 44, 45; 44, 46; 45, identifier:out_file; 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:work_dir; 54, binary_operator:%; 54, 55; 54, 56; 55, string:"%s-%s-prep.vcf"; 56, tuple; 56, 57; 56, 72; 57, subscript; 57, 58; 57, 71; 58, call; 58, 59; 58, 62; 59, attribute; 59, 60; 59, 61; 60, identifier:utils; 61, identifier:splitext_plus; 62, argument_list; 62, 63; 63, call; 63, 64; 63, 69; 64, attribute; 64, 65; 64, 68; 65, attribute; 65, 66; 65, 67; 66, identifier:os; 67, identifier:path; 68, identifier:basename; 69, argument_list; 69, 70; 70, identifier:in_file; 71, integer:0; 72, identifier:vcaller; 73, if_statement; 73, 74; 73, 82; 74, not_operator; 74, 75; 75, call; 75, 76; 75, 79; 76, attribute; 76, 77; 76, 78; 77, identifier:utils; 78, identifier:file_uptodate; 79, argument_list; 79, 80; 79, 81; 80, identifier:out_file; 81, identifier:in_file; 82, block; 82, 83; 82, 90; 83, expression_statement; 83, 84; 84, assignment; 84, 85; 84, 86; 85, identifier:check_fn; 86, call; 86, 87; 86, 88; 87, identifier:_min_sample_pass; 88, argument_list; 88, 89; 89, identifier:ignore_file; 90, with_statement; 90, 91; 90, 103; 91, with_clause; 91, 92; 92, with_item; 92, 93; 93, as_pattern; 93, 94; 93, 101; 94, call; 94, 95; 94, 96; 95, identifier:file_transaction; 96, argument_list; 96, 97; 96, 100; 97, attribute; 97, 98; 97, 99; 98, identifier:somatic_info; 99, identifier:tumor_data; 100, identifier:out_file; 101, as_pattern_target; 101, 102; 102, identifier:tx_out_file; 103, block; 103, 104; 103, 115; 103, 116; 103, 170; 103, 231; 103, 232; 104, expression_statement; 104, 105; 105, assignment; 105, 106; 105, 107; 106, identifier:tx_out_file_raw; 107, binary_operator:%; 107, 108; 107, 109; 108, string:"%s-raw%s"; 109, call; 109, 110; 109, 113; 110, attribute; 110, 111; 110, 112; 111, identifier:utils; 112, identifier:splitext_plus; 113, argument_list; 113, 114; 114, identifier:tx_out_file; 115, comment; 116, with_statement; 116, 117; 116, 126; 117, with_clause; 117, 118; 118, with_item; 118, 119; 119, as_pattern; 119, 120; 119, 124; 120, call; 120, 121; 120, 122; 121, identifier:VariantFile; 122, argument_list; 122, 123; 123, identifier:in_file; 124, as_pattern_target; 124, 125; 125, identifier:bcf_in; 126, block; 126, 127; 126, 145; 126, 154; 127, expression_statement; 127, 128; 128, assignment; 128, 129; 128, 130; 129, identifier:depths; 130, list_comprehension; 130, 131; 130, 138; 131, call; 131, 132; 131, 133; 132, identifier:_sample_depth; 133, argument_list; 133, 134; 133, 135; 134, identifier:rec; 135, attribute; 135, 136; 135, 137; 136, identifier:somatic_info; 137, identifier:tumor_name; 138, for_in_clause; 138, 139; 138, 140; 139, identifier:rec; 140, call; 140, 141; 140, 142; 141, identifier:filter; 142, argument_list; 142, 143; 142, 144; 143, identifier:check_fn; 144, identifier:bcf_in; 145, expression_statement; 145, 146; 146, call; 146, 147; 146, 150; 147, attribute; 147, 148; 147, 149; 148, identifier:depths; 149, identifier:sort; 150, argument_list; 150, 151; 151, keyword_argument; 151, 152; 151, 153; 152, identifier:reverse; 153, True; 154, expression_statement; 154, 155; 155, assignment; 155, 156; 155, 157; 156, identifier:depth_thresh; 157, conditional_expression:if; 157, 158; 157, 168; 157, 169; 158, subscript; 158, 159; 158, 166; 159, subscript; 159, 160; 159, 161; 160, identifier:depths; 161, slice; 161, 162; 161, 163; 162, colon; 163, subscript; 163, 164; 163, 165; 164, identifier:config; 165, string:"sample_size"; 166, unary_operator:-; 166, 167; 167, integer:1; 168, identifier:depths; 169, integer:0; 170, with_statement; 170, 171; 170, 180; 171, with_clause; 171, 172; 172, with_item; 172, 173; 173, as_pattern; 173, 174; 173, 178; 174, call; 174, 175; 174, 176; 175, identifier:VariantFile; 176, argument_list; 176, 177; 177, identifier:in_file; 178, as_pattern_target; 178, 179; 179, identifier:bcf_in; 180, block; 180, 181; 181, with_statement; 181, 182; 181, 197; 182, with_clause; 182, 183; 183, with_item; 183, 184; 184, as_pattern; 184, 185; 184, 195; 185, call; 185, 186; 185, 187; 186, identifier:VariantFile; 187, argument_list; 187, 188; 187, 189; 187, 190; 188, identifier:tx_out_file_raw; 189, string:"w"; 190, keyword_argument; 190, 191; 190, 192; 191, identifier:header; 192, attribute; 192, 193; 192, 194; 193, identifier:bcf_in; 194, identifier:header; 195, as_pattern_target; 195, 196; 196, identifier:bcf_out; 197, block; 197, 198; 198, for_statement; 198, 199; 198, 200; 198, 201; 199, identifier:rec; 200, identifier:bcf_in; 201, block; 201, 202; 202, if_statement; 202, 203; 202, 223; 203, parenthesized_expression; 203, 204; 204, boolean_operator:and; 204, 205; 204, 209; 205, call; 205, 206; 205, 207; 206, identifier:check_fn; 207, argument_list; 207, 208; 208, identifier:rec; 209, parenthesized_expression; 209, 210; 210, boolean_operator:or; 210, 211; 210, 214; 211, comparison_operator:<; 211, 212; 211, 213; 212, identifier:depth_thresh; 213, integer:5; 214, comparison_operator:>=; 214, 215; 214, 222; 215, call; 215, 216; 215, 217; 216, identifier:_sample_depth; 217, argument_list; 217, 218; 217, 219; 218, identifier:rec; 219, attribute; 219, 220; 219, 221; 220, identifier:somatic_info; 221, identifier:tumor_name; 222, identifier:depth_thresh; 223, block; 223, 224; 224, expression_statement; 224, 225; 225, call; 225, 226; 225, 229; 226, attribute; 226, 227; 226, 228; 227, identifier:bcf_out; 228, identifier:write; 229, argument_list; 229, 230; 230, identifier:rec; 231, comment; 232, with_statement; 232, 233; 232, 242; 233, with_clause; 233, 234; 234, with_item; 234, 235; 235, as_pattern; 235, 236; 235, 240; 236, call; 236, 237; 236, 238; 237, identifier:open; 238, argument_list; 238, 239; 239, identifier:tx_out_file_raw; 240, as_pattern_target; 240, 241; 241, identifier:in_handle; 242, block; 242, 243; 243, with_statement; 243, 244; 243, 254; 244, with_clause; 244, 245; 245, with_item; 245, 246; 246, as_pattern; 246, 247; 246, 252; 247, call; 247, 248; 247, 249; 248, identifier:open; 249, argument_list; 249, 250; 249, 251; 250, identifier:tx_out_file; 251, string:"w"; 252, as_pattern_target; 252, 253; 253, identifier:out_handle; 254, block; 254, 255; 255, for_statement; 255, 256; 255, 257; 255, 258; 256, identifier:line; 257, identifier:in_handle; 258, block; 258, 259; 258, 297; 259, if_statement; 259, 260; 259, 267; 260, not_operator; 260, 261; 261, call; 261, 262; 261, 265; 262, attribute; 262, 263; 262, 264; 263, identifier:line; 264, identifier:startswith; 265, argument_list; 265, 266; 266, string:"#"; 267, block; 267, 268; 267, 277; 267, 288; 268, expression_statement; 268, 269; 269, assignment; 269, 270; 269, 271; 270, identifier:parts; 271, call; 271, 272; 271, 275; 272, attribute; 272, 273; 272, 274; 273, identifier:line; 274, identifier:split; 275, argument_list; 275, 276; 276, string:"\t"; 277, expression_statement; 277, 278; 278, assignment; 278, 279; 278, 282; 279, subscript; 279, 280; 279, 281; 280, identifier:parts; 281, integer:0; 282, call; 282, 283; 282, 284; 283, identifier:_phylowgs_compatible_chroms; 284, argument_list; 284, 285; 285, subscript; 285, 286; 285, 287; 286, identifier:parts; 287, integer:0; 288, expression_statement; 288, 289; 289, assignment; 289, 290; 289, 291; 290, identifier:line; 291, call; 291, 292; 291, 295; 292, attribute; 292, 293; 292, 294; 293, string:"\t"; 294, identifier:join; 295, argument_list; 295, 296; 296, identifier:parts; 297, expression_statement; 297, 298; 298, call; 298, 299; 298, 302; 299, attribute; 299, 300; 299, 301; 300, identifier:out_handle; 301, identifier:write; 302, argument_list; 302, 303; 303, identifier:line; 304, return_statement; 304, 305; 305, expression_list; 305, 306; 305, 307; 306, identifier:variant_type; 307, identifier:out_file
def _prep_vrn_file(in_file, vcaller, work_dir, somatic_info, ignore_file, config): """Create a variant file to feed into the PhyloWGS prep script, limiting records. Sorts by depth, adding top covered samples up to the sample_size supported by PhyloWGS. The logic is that the higher depth samples will have better resolution for frequency differences. More complex implementations could try to subset based on a distribution of frequencies to best sample the potential heterogeneity. Handles MuTect and VarDict as inputs to PhyloWGS. Fixes chromosome naming to use non chr-prefixed contigs, to match _prep_cnv_file. """ if vcaller.startswith("vardict"): variant_type = "vardict" elif vcaller == "mutect": variant_type = "mutect-smchet" else: raise ValueError("Unexpected variant caller for PhyloWGS prep: %s" % vcaller) out_file = os.path.join(work_dir, "%s-%s-prep.vcf" % (utils.splitext_plus(os.path.basename(in_file))[0], vcaller)) if not utils.file_uptodate(out_file, in_file): check_fn = _min_sample_pass(ignore_file) with file_transaction(somatic_info.tumor_data, out_file) as tx_out_file: tx_out_file_raw = "%s-raw%s" % utils.splitext_plus(tx_out_file) # Filter inputs with VariantFile(in_file) as bcf_in: depths = [_sample_depth(rec, somatic_info.tumor_name) for rec in filter(check_fn, bcf_in)] depths.sort(reverse=True) depth_thresh = depths[:config["sample_size"]][-1] if depths else 0 with VariantFile(in_file) as bcf_in: with VariantFile(tx_out_file_raw, "w", header=bcf_in.header) as bcf_out: for rec in bcf_in: if (check_fn(rec) and (depth_thresh < 5 or _sample_depth(rec, somatic_info.tumor_name) >= depth_thresh)): bcf_out.write(rec) # Fix potential chromosome issues with open(tx_out_file_raw) as in_handle: with open(tx_out_file, "w") as out_handle: for line in in_handle: if not line.startswith("#"): parts = line.split("\t") parts[0] = _phylowgs_compatible_chroms(parts[0]) line = "\t".join(parts) out_handle.write(line) return variant_type, out_file
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:concat; 3, parameters; 3, 4; 3, 5; 4, identifier:bed_files; 5, default_parameter; 5, 6; 5, 7; 6, identifier:catted; 7, None; 8, block; 8, 9; 8, 11; 8, 21; 8, 71; 8, 116; 9, expression_statement; 9, 10; 10, comment; 11, expression_statement; 11, 12; 12, assignment; 12, 13; 12, 14; 13, identifier:bed_files; 14, list_comprehension; 14, 15; 14, 16; 14, 19; 15, identifier:x; 16, for_in_clause; 16, 17; 16, 18; 17, identifier:x; 18, identifier:bed_files; 19, if_clause; 19, 20; 20, identifier:x; 21, if_statement; 21, 22; 21, 28; 22, comparison_operator:==; 22, 23; 22, 27; 23, call; 23, 24; 23, 25; 24, identifier:len; 25, argument_list; 25, 26; 26, identifier:bed_files; 27, integer:0; 28, block; 28, 29; 29, if_statement; 29, 30; 29, 31; 29, 32; 29, 67; 30, identifier:catted; 31, comment; 32, block; 32, 33; 32, 41; 33, expression_statement; 33, 34; 34, assignment; 34, 35; 34, 36; 35, identifier:sorted_bed; 36, call; 36, 37; 36, 40; 37, attribute; 37, 38; 37, 39; 38, identifier:catted; 39, identifier:sort; 40, argument_list; 41, if_statement; 41, 42; 41, 51; 41, 63; 42, not_operator; 42, 43; 43, call; 43, 44; 43, 49; 44, attribute; 44, 45; 44, 48; 45, attribute; 45, 46; 45, 47; 46, identifier:sorted_bed; 47, identifier:fn; 48, identifier:endswith; 49, argument_list; 49, 50; 50, string:".bed"; 51, block; 51, 52; 52, return_statement; 52, 53; 53, call; 53, 54; 53, 57; 54, attribute; 54, 55; 54, 56; 55, identifier:sorted_bed; 56, identifier:moveto; 57, argument_list; 57, 58; 58, binary_operator:+; 58, 59; 58, 62; 59, attribute; 59, 60; 59, 61; 60, identifier:sorted_bed; 61, identifier:fn; 62, string:".bed"; 63, else_clause; 63, 64; 64, block; 64, 65; 65, return_statement; 65, 66; 66, identifier:sorted_bed; 67, else_clause; 67, 68; 68, block; 68, 69; 69, return_statement; 69, 70; 70, identifier:catted; 71, if_statement; 71, 72; 71, 74; 71, 95; 72, not_operator; 72, 73; 73, identifier:catted; 74, block; 74, 75; 74, 82; 75, expression_statement; 75, 76; 76, assignment; 76, 77; 76, 78; 77, identifier:bed_files; 78, call; 78, 79; 78, 80; 79, identifier:list; 80, argument_list; 80, 81; 81, identifier:bed_files; 82, expression_statement; 82, 83; 83, assignment; 83, 84; 83, 85; 84, identifier:catted; 85, call; 85, 86; 85, 89; 86, attribute; 86, 87; 86, 88; 87, identifier:bt; 88, identifier:BedTool; 89, argument_list; 89, 90; 90, call; 90, 91; 90, 94; 91, attribute; 91, 92; 91, 93; 92, identifier:bed_files; 93, identifier:pop; 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:catted; 100, call; 100, 101; 100, 104; 101, attribute; 101, 102; 101, 103; 102, identifier:catted; 103, identifier:cat; 104, argument_list; 104, 105; 104, 110; 104, 113; 105, call; 105, 106; 105, 109; 106, attribute; 106, 107; 106, 108; 107, identifier:bed_files; 108, identifier:pop; 109, argument_list; 110, keyword_argument; 110, 111; 110, 112; 111, identifier:postmerge; 112, False; 113, keyword_argument; 113, 114; 113, 115; 114, identifier:force_truncate; 115, False; 116, return_statement; 116, 117; 117, call; 117, 118; 117, 119; 118, identifier:concat; 119, argument_list; 119, 120; 119, 121; 120, identifier:bed_files; 121, identifier:catted
def concat(bed_files, catted=None): """ recursively concat a set of BED files, returning a sorted bedtools object of the result """ bed_files = [x for x in bed_files if x] if len(bed_files) == 0: if catted: # move to a .bed extension for downstream tools if not already sorted_bed = catted.sort() if not sorted_bed.fn.endswith(".bed"): return sorted_bed.moveto(sorted_bed.fn + ".bed") else: return sorted_bed else: return catted if not catted: bed_files = list(bed_files) catted = bt.BedTool(bed_files.pop()) else: catted = catted.cat(bed_files.pop(), postmerge=False, force_truncate=False) return concat(bed_files, catted)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:_get_sv_callers; 3, parameters; 3, 4; 4, identifier:items; 5, block; 5, 6; 5, 8; 5, 12; 5, 35; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:callers; 11, list:[]; 12, for_statement; 12, 13; 12, 14; 12, 15; 13, identifier:data; 14, identifier:items; 15, block; 15, 16; 16, for_statement; 16, 17; 16, 18; 16, 25; 17, identifier:sv; 18, call; 18, 19; 18, 22; 19, attribute; 19, 20; 19, 21; 20, identifier:data; 21, identifier:get; 22, argument_list; 22, 23; 22, 24; 23, string:"sv"; 24, list:[]; 25, block; 25, 26; 26, expression_statement; 26, 27; 27, call; 27, 28; 27, 31; 28, attribute; 28, 29; 28, 30; 29, identifier:callers; 30, identifier:append; 31, argument_list; 31, 32; 32, subscript; 32, 33; 32, 34; 33, identifier:sv; 34, string:"variantcaller"; 35, return_statement; 35, 36; 36, call; 36, 37; 36, 54; 37, attribute; 37, 38; 37, 53; 38, call; 38, 39; 38, 40; 39, identifier:list; 40, argument_list; 40, 41; 41, call; 41, 42; 41, 43; 42, identifier:set; 43, argument_list; 43, 44; 44, list_comprehension; 44, 45; 44, 46; 44, 49; 45, identifier:x; 46, for_in_clause; 46, 47; 46, 48; 47, identifier:x; 48, identifier:callers; 49, if_clause; 49, 50; 50, comparison_operator:!=; 50, 51; 50, 52; 51, identifier:x; 52, string:"sv-ensemble"; 53, identifier:sort; 54, argument_list
def _get_sv_callers(items): """ return a sorted list of all of the structural variant callers run """ callers = [] for data in items: for sv in data.get("sv", []): callers.append(sv["variantcaller"]) return list(set([x for x in callers if x != "sv-ensemble"])).sort()
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:get_sort_cmd; 3, parameters; 3, 4; 4, default_parameter; 4, 5; 4, 6; 5, identifier:tmp_dir; 6, None; 7, block; 7, 8; 7, 10; 7, 26; 7, 39; 7, 66; 8, expression_statement; 8, 9; 9, comment; 10, expression_statement; 10, 11; 11, assignment; 11, 12; 11, 13; 12, identifier:has_versionsort; 13, call; 13, 14; 13, 25; 14, attribute; 14, 15; 14, 24; 15, call; 15, 16; 15, 19; 16, attribute; 16, 17; 16, 18; 17, identifier:subprocess; 18, identifier:check_output; 19, argument_list; 19, 20; 19, 21; 20, string:"sort --help | grep version-sort; exit 0"; 21, keyword_argument; 21, 22; 21, 23; 22, identifier:shell; 23, True; 24, identifier:strip; 25, argument_list; 26, if_statement; 26, 27; 26, 28; 26, 33; 27, identifier:has_versionsort; 28, block; 28, 29; 29, expression_statement; 29, 30; 30, assignment; 30, 31; 30, 32; 31, identifier:cmd; 32, string:"sort -V"; 33, else_clause; 33, 34; 34, block; 34, 35; 35, expression_statement; 35, 36; 36, assignment; 36, 37; 36, 38; 37, identifier:cmd; 38, string:"sort"; 39, if_statement; 39, 40; 39, 59; 40, boolean_operator:and; 40, 41; 40, 51; 41, boolean_operator:and; 41, 42; 41, 43; 42, identifier:tmp_dir; 43, call; 43, 44; 43, 49; 44, attribute; 44, 45; 44, 48; 45, attribute; 45, 46; 45, 47; 46, identifier:os; 47, identifier:path; 48, identifier:exists; 49, argument_list; 49, 50; 50, identifier:tmp_dir; 51, call; 51, 52; 51, 57; 52, attribute; 52, 53; 52, 56; 53, attribute; 53, 54; 53, 55; 54, identifier:os; 55, identifier:path; 56, identifier:isdir; 57, argument_list; 57, 58; 58, identifier:tmp_dir; 59, block; 59, 60; 60, expression_statement; 60, 61; 61, augmented_assignment:+=; 61, 62; 61, 63; 62, identifier:cmd; 63, binary_operator:%; 63, 64; 63, 65; 64, string:" -T %s"; 65, identifier:tmp_dir; 66, return_statement; 66, 67; 67, identifier:cmd
def get_sort_cmd(tmp_dir=None): """Retrieve GNU coreutils sort command, using version-sort if available. Recent versions of sort have alpha-numeric sorting, which provides more natural sorting of chromosomes (chr1, chr2) instead of (chr1, chr10). This also fixes versions of sort, like 8.22 in CentOS 7.1, that have broken sorting without version sorting specified. https://github.com/bcbio/bcbio-nextgen/issues/624 https://github.com/bcbio/bcbio-nextgen/issues/1017 """ has_versionsort = subprocess.check_output("sort --help | grep version-sort; exit 0", shell=True).strip() if has_versionsort: cmd = "sort -V" else: cmd = "sort" if tmp_dir and os.path.exists(tmp_dir) and os.path.isdir(tmp_dir): cmd += " -T %s" % tmp_dir return cmd
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 15; 2, function_name:clean_file; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 3, 12; 4, identifier:in_file; 5, identifier:data; 6, default_parameter; 6, 7; 6, 8; 7, identifier:prefix; 8, string:""; 9, default_parameter; 9, 10; 9, 11; 10, identifier:bedprep_dir; 11, None; 12, default_parameter; 12, 13; 12, 14; 13, identifier:simple; 14, None; 15, block; 15, 16; 15, 18; 15, 19; 15, 20; 15, 27; 16, expression_statement; 16, 17; 17, comment; 18, comment; 19, comment; 20, expression_statement; 20, 21; 21, assignment; 21, 22; 21, 23; 22, identifier:simple; 23, conditional_expression:if; 23, 24; 23, 25; 23, 26; 24, string:"iconv -c -f utf-8 -t ascii | sed 's/ //g' |"; 25, identifier:simple; 26, string:""; 27, if_statement; 27, 28; 27, 29; 28, identifier:in_file; 29, block; 29, 30; 29, 55; 29, 56; 29, 75; 29, 98; 29, 108; 29, 125; 29, 217; 29, 234; 30, if_statement; 30, 31; 30, 33; 31, not_operator; 31, 32; 32, identifier:bedprep_dir; 33, block; 33, 34; 34, expression_statement; 34, 35; 35, assignment; 35, 36; 35, 37; 36, identifier:bedprep_dir; 37, call; 37, 38; 37, 41; 38, attribute; 38, 39; 38, 40; 39, identifier:utils; 40, identifier:safe_makedir; 41, argument_list; 41, 42; 42, call; 42, 43; 42, 48; 43, attribute; 43, 44; 43, 47; 44, attribute; 44, 45; 44, 46; 45, identifier:os; 46, identifier:path; 47, identifier:join; 48, argument_list; 48, 49; 48, 54; 49, subscript; 49, 50; 49, 53; 50, subscript; 50, 51; 50, 52; 51, identifier:data; 52, string:"dirs"; 53, string:"work"; 54, string:"bedprep"; 55, comment; 56, if_statement; 56, 57; 56, 72; 57, boolean_operator:and; 57, 58; 57, 59; 58, identifier:prefix; 59, call; 59, 60; 59, 70; 60, attribute; 60, 61; 60, 69; 61, call; 61, 62; 61, 67; 62, attribute; 62, 63; 62, 66; 63, attribute; 63, 64; 63, 65; 64, identifier:os; 65, identifier:path; 66, identifier:basename; 67, argument_list; 67, 68; 68, identifier:in_file; 69, identifier:startswith; 70, argument_list; 70, 71; 71, identifier:prefix; 72, block; 72, 73; 73, return_statement; 73, 74; 74, identifier:in_file; 75, expression_statement; 75, 76; 76, assignment; 76, 77; 76, 78; 77, identifier:out_file; 78, call; 78, 79; 78, 84; 79, attribute; 79, 80; 79, 83; 80, attribute; 80, 81; 80, 82; 81, identifier:os; 82, identifier:path; 83, identifier:join; 84, argument_list; 84, 85; 84, 86; 85, identifier:bedprep_dir; 86, binary_operator:%; 86, 87; 86, 88; 87, string:"%s%s"; 88, tuple; 88, 89; 88, 90; 89, identifier:prefix; 90, call; 90, 91; 90, 96; 91, attribute; 91, 92; 91, 95; 92, attribute; 92, 93; 92, 94; 93, identifier:os; 94, identifier:path; 95, identifier:basename; 96, argument_list; 96, 97; 97, identifier:in_file; 98, expression_statement; 98, 99; 99, assignment; 99, 100; 99, 101; 100, identifier:out_file; 101, call; 101, 102; 101, 105; 102, attribute; 102, 103; 102, 104; 103, identifier:out_file; 104, identifier:replace; 105, argument_list; 105, 106; 105, 107; 106, string:".interval_list"; 107, string:".bed"; 108, if_statement; 108, 109; 108, 115; 109, call; 109, 110; 109, 113; 110, attribute; 110, 111; 110, 112; 111, identifier:out_file; 112, identifier:endswith; 113, argument_list; 113, 114; 114, string:".gz"; 115, block; 115, 116; 116, expression_statement; 116, 117; 117, assignment; 117, 118; 117, 119; 118, identifier:out_file; 119, subscript; 119, 120; 119, 121; 120, identifier:out_file; 121, slice; 121, 122; 121, 123; 122, colon; 123, unary_operator:-; 123, 124; 124, integer:3; 125, if_statement; 125, 126; 125, 134; 126, not_operator; 126, 127; 127, call; 127, 128; 127, 131; 128, attribute; 128, 129; 128, 130; 129, identifier:utils; 130, identifier:file_uptodate; 131, argument_list; 131, 132; 131, 133; 132, identifier:out_file; 133, identifier:in_file; 134, block; 134, 135; 134, 141; 134, 147; 135, expression_statement; 135, 136; 136, call; 136, 137; 136, 138; 137, identifier:check_bed_contigs; 138, argument_list; 138, 139; 138, 140; 139, identifier:in_file; 140, identifier:data; 141, expression_statement; 141, 142; 142, call; 142, 143; 142, 144; 143, identifier:check_bed_coords; 144, argument_list; 144, 145; 144, 146; 145, identifier:in_file; 146, identifier:data; 147, with_statement; 147, 148; 147, 158; 148, with_clause; 148, 149; 149, with_item; 149, 150; 150, as_pattern; 150, 151; 150, 156; 151, call; 151, 152; 151, 153; 152, identifier:file_transaction; 153, argument_list; 153, 154; 153, 155; 154, identifier:data; 155, identifier:out_file; 156, as_pattern_target; 156, 157; 157, identifier:tx_out_file; 158, block; 158, 159; 158, 165; 158, 177; 158, 191; 158, 200; 159, expression_statement; 159, 160; 160, assignment; 160, 161; 160, 162; 161, identifier:bcbio_py; 162, attribute; 162, 163; 162, 164; 163, identifier:sys; 164, identifier:executable; 165, expression_statement; 165, 166; 166, assignment; 166, 167; 166, 168; 167, identifier:cat_cmd; 168, conditional_expression:if; 168, 169; 168, 170; 168, 176; 169, string:"zcat"; 170, call; 170, 171; 170, 174; 171, attribute; 171, 172; 171, 173; 172, identifier:in_file; 173, identifier:endswith; 174, argument_list; 174, 175; 175, string:".gz"; 176, string:"cat"; 177, expression_statement; 177, 178; 178, assignment; 178, 179; 178, 180; 179, identifier:sort_cmd; 180, call; 180, 181; 180, 182; 181, identifier:get_sort_cmd; 182, argument_list; 182, 183; 183, call; 183, 184; 183, 189; 184, attribute; 184, 185; 184, 188; 185, attribute; 185, 186; 185, 187; 186, identifier:os; 187, identifier:path; 188, identifier:dirname; 189, argument_list; 189, 190; 190, identifier:tx_out_file; 191, expression_statement; 191, 192; 192, assignment; 192, 193; 192, 194; 193, identifier:cmd; 194, parenthesized_expression; 194, 195; 195, concatenated_string; 195, 196; 195, 197; 195, 198; 195, 199; 196, string:"{cat_cmd} {in_file} | grep -v ^track | grep -v ^browser | grep -v ^@ | "; 197, string:"grep -v ^# | {simple} "; 198, string:"{bcbio_py} -c 'from bcbio.variation import bedutils; bedutils.remove_bad()' | "; 199, string:"{sort_cmd} -k1,1 -k2,2n > {tx_out_file}"; 200, expression_statement; 200, 201; 201, call; 201, 202; 201, 205; 202, attribute; 202, 203; 202, 204; 203, identifier:do; 204, identifier:run; 205, argument_list; 205, 206; 205, 215; 205, 216; 206, call; 206, 207; 206, 210; 207, attribute; 207, 208; 207, 209; 208, identifier:cmd; 209, identifier:format; 210, argument_list; 210, 211; 211, dictionary_splat; 211, 212; 212, call; 212, 213; 212, 214; 213, identifier:locals; 214, argument_list; 215, string:"Prepare cleaned BED file"; 216, identifier:data; 217, expression_statement; 217, 218; 218, call; 218, 219; 218, 222; 219, attribute; 219, 220; 219, 221; 220, identifier:vcfutils; 221, identifier:bgzip_and_index; 222, argument_list; 222, 223; 222, 224; 222, 231; 223, identifier:out_file; 224, call; 224, 225; 224, 228; 225, attribute; 225, 226; 225, 227; 226, identifier:data; 227, identifier:get; 228, argument_list; 228, 229; 228, 230; 229, string:"config"; 230, dictionary; 231, keyword_argument; 231, 232; 231, 233; 232, identifier:remove_orig; 233, False; 234, return_statement; 234, 235; 235, identifier:out_file
def clean_file(in_file, data, prefix="", bedprep_dir=None, simple=None): """Prepare a clean sorted input BED file without headers """ # Remove non-ascii characters. Used in coverage analysis, to support JSON code in one column # and be happy with sambamba: simple = "iconv -c -f utf-8 -t ascii | sed 's/ //g' |" if simple else "" if in_file: if not bedprep_dir: bedprep_dir = utils.safe_makedir(os.path.join(data["dirs"]["work"], "bedprep")) # Avoid running multiple times with same prefix if prefix and os.path.basename(in_file).startswith(prefix): return in_file out_file = os.path.join(bedprep_dir, "%s%s" % (prefix, os.path.basename(in_file))) out_file = out_file.replace(".interval_list", ".bed") if out_file.endswith(".gz"): out_file = out_file[:-3] if not utils.file_uptodate(out_file, in_file): check_bed_contigs(in_file, data) check_bed_coords(in_file, data) with file_transaction(data, out_file) as tx_out_file: bcbio_py = sys.executable cat_cmd = "zcat" if in_file.endswith(".gz") else "cat" sort_cmd = get_sort_cmd(os.path.dirname(tx_out_file)) cmd = ("{cat_cmd} {in_file} | grep -v ^track | grep -v ^browser | grep -v ^@ | " "grep -v ^# | {simple} " "{bcbio_py} -c 'from bcbio.variation import bedutils; bedutils.remove_bad()' | " "{sort_cmd} -k1,1 -k2,2n > {tx_out_file}") do.run(cmd.format(**locals()), "Prepare cleaned BED file", data) vcfutils.bgzip_and_index(out_file, data.get("config", {}), remove_orig=False) return out_file
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:_sort_by_region; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 4, identifier:fnames; 5, identifier:regions; 6, identifier:ref_file; 7, identifier:config; 8, block; 8, 9; 8, 11; 8, 15; 8, 38; 8, 42; 8, 55; 8, 62; 8, 176; 8, 182; 9, expression_statement; 9, 10; 10, comment; 11, expression_statement; 11, 12; 12, assignment; 12, 13; 12, 14; 13, identifier:contig_order; 14, dictionary; 15, for_statement; 15, 16; 15, 19; 15, 29; 16, pattern_list; 16, 17; 16, 18; 17, identifier:i; 18, identifier:sq; 19, call; 19, 20; 19, 21; 20, identifier:enumerate; 21, argument_list; 21, 22; 22, call; 22, 23; 22, 26; 23, attribute; 23, 24; 23, 25; 24, identifier:ref; 25, identifier:file_contigs; 26, argument_list; 26, 27; 26, 28; 27, identifier:ref_file; 28, identifier:config; 29, block; 29, 30; 30, expression_statement; 30, 31; 31, assignment; 31, 32; 31, 37; 32, subscript; 32, 33; 32, 34; 33, identifier:contig_order; 34, attribute; 34, 35; 34, 36; 35, identifier:sq; 36, identifier:name; 37, identifier:i; 38, expression_statement; 38, 39; 39, assignment; 39, 40; 39, 41; 40, identifier:sitems; 41, list:[]; 42, assert_statement; 42, 43; 42, 52; 43, comparison_operator:==; 43, 44; 43, 48; 44, call; 44, 45; 44, 46; 45, identifier:len; 46, argument_list; 46, 47; 47, identifier:regions; 48, call; 48, 49; 48, 50; 49, identifier:len; 50, argument_list; 50, 51; 51, identifier:fnames; 52, tuple; 52, 53; 52, 54; 53, identifier:regions; 54, identifier:fnames; 55, expression_statement; 55, 56; 56, assignment; 56, 57; 56, 58; 57, identifier:added_fnames; 58, call; 58, 59; 58, 60; 59, identifier:set; 60, argument_list; 60, 61; 61, list:[]; 62, for_statement; 62, 63; 62, 66; 62, 71; 63, pattern_list; 63, 64; 63, 65; 64, identifier:region; 65, identifier:fname; 66, call; 66, 67; 66, 68; 67, identifier:zip; 68, argument_list; 68, 69; 68, 70; 69, identifier:regions; 70, identifier:fnames; 71, block; 71, 72; 72, if_statement; 72, 73; 72, 76; 73, comparison_operator:not; 73, 74; 73, 75; 74, identifier:fname; 75, identifier:added_fnames; 76, block; 76, 77; 76, 154; 76, 169; 77, if_statement; 77, 78; 77, 85; 77, 93; 77, 140; 78, call; 78, 79; 78, 80; 79, identifier:isinstance; 80, argument_list; 80, 81; 80, 82; 81, identifier:region; 82, tuple; 82, 83; 82, 84; 83, identifier:list; 84, identifier:tuple; 85, block; 85, 86; 86, expression_statement; 86, 87; 87, assignment; 87, 88; 87, 92; 88, pattern_list; 88, 89; 88, 90; 88, 91; 89, identifier:c; 90, identifier:s; 91, identifier:e; 92, identifier:region; 93, elif_clause; 93, 94; 93, 110; 94, boolean_operator:and; 94, 95; 94, 102; 95, call; 95, 96; 95, 97; 96, identifier:isinstance; 97, argument_list; 97, 98; 97, 99; 98, identifier:region; 99, attribute; 99, 100; 99, 101; 100, identifier:six; 101, identifier:string_types; 102, comparison_operator:>=; 102, 103; 102, 109; 103, call; 103, 104; 103, 107; 104, attribute; 104, 105; 104, 106; 105, identifier:region; 106, identifier:find; 107, argument_list; 107, 108; 108, string:":"; 109, integer:0; 110, block; 110, 111; 110, 122; 111, expression_statement; 111, 112; 112, assignment; 112, 113; 112, 116; 113, pattern_list; 113, 114; 113, 115; 114, identifier:c; 115, identifier:coords; 116, call; 116, 117; 116, 120; 117, attribute; 117, 118; 117, 119; 118, identifier:region; 119, identifier:split; 120, argument_list; 120, 121; 121, string:":"; 122, expression_statement; 122, 123; 123, assignment; 123, 124; 123, 127; 124, pattern_list; 124, 125; 124, 126; 125, identifier:s; 126, identifier:e; 127, list_comprehension; 127, 128; 127, 132; 128, call; 128, 129; 128, 130; 129, identifier:int; 130, argument_list; 130, 131; 131, identifier:x; 132, for_in_clause; 132, 133; 132, 134; 133, identifier:x; 134, call; 134, 135; 134, 138; 135, attribute; 135, 136; 135, 137; 136, identifier:coords; 137, identifier:split; 138, argument_list; 138, 139; 139, string:"-"; 140, else_clause; 140, 141; 141, block; 141, 142; 141, 146; 142, expression_statement; 142, 143; 143, assignment; 143, 144; 143, 145; 144, identifier:c; 145, identifier:region; 146, expression_statement; 146, 147; 147, assignment; 147, 148; 147, 151; 148, pattern_list; 148, 149; 148, 150; 149, identifier:s; 150, identifier:e; 151, expression_list; 151, 152; 151, 153; 152, integer:0; 153, integer:0; 154, expression_statement; 154, 155; 155, call; 155, 156; 155, 159; 156, attribute; 156, 157; 156, 158; 157, identifier:sitems; 158, identifier:append; 159, argument_list; 159, 160; 160, tuple; 160, 161; 160, 167; 160, 168; 161, tuple; 161, 162; 161, 165; 161, 166; 162, subscript; 162, 163; 162, 164; 163, identifier:contig_order; 164, identifier:c; 165, identifier:s; 166, identifier:e; 167, identifier:c; 168, identifier:fname; 169, expression_statement; 169, 170; 170, call; 170, 171; 170, 174; 171, attribute; 171, 172; 171, 173; 172, identifier:added_fnames; 173, identifier:add; 174, argument_list; 174, 175; 175, identifier:fname; 176, expression_statement; 176, 177; 177, call; 177, 178; 177, 181; 178, attribute; 178, 179; 178, 180; 179, identifier:sitems; 180, identifier:sort; 181, argument_list; 182, return_statement; 182, 183; 183, list_comprehension; 183, 184; 183, 191; 184, tuple; 184, 185; 184, 188; 185, subscript; 185, 186; 185, 187; 186, identifier:x; 187, integer:1; 188, subscript; 188, 189; 188, 190; 189, identifier:x; 190, integer:2; 191, for_in_clause; 191, 192; 191, 193; 192, identifier:x; 193, identifier:sitems
def _sort_by_region(fnames, regions, ref_file, config): """Sort a set of regionally split files by region for ordered output. """ contig_order = {} for i, sq in enumerate(ref.file_contigs(ref_file, config)): contig_order[sq.name] = i sitems = [] assert len(regions) == len(fnames), (regions, fnames) added_fnames = set([]) for region, fname in zip(regions, fnames): if fname not in added_fnames: if isinstance(region, (list, tuple)): c, s, e = region elif isinstance(region, six.string_types) and region.find(":") >= 0: c, coords = region.split(":") s, e = [int(x) for x in coords.split("-")] else: c = region s, e = 0, 0 sitems.append(((contig_order[c], s, e), c, fname)) added_fnames.add(fname) sitems.sort() return [(x[1], x[2]) for x in sitems]
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:_get_file_list; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 4, identifier:orig_files; 5, identifier:out_file; 6, identifier:regions; 7, identifier:ref_file; 8, identifier:config; 9, block; 9, 10; 9, 12; 9, 22; 9, 48; 9, 105; 9, 120; 9, 133; 9, 158; 10, expression_statement; 10, 11; 11, comment; 12, expression_statement; 12, 13; 13, assignment; 13, 14; 13, 15; 14, identifier:sorted_files; 15, call; 15, 16; 15, 17; 16, identifier:_sort_by_region; 17, argument_list; 17, 18; 17, 19; 17, 20; 17, 21; 18, identifier:orig_files; 19, identifier:regions; 20, identifier:ref_file; 21, identifier:config; 22, expression_statement; 22, 23; 23, assignment; 23, 24; 23, 25; 24, identifier:exist_files; 25, list_comprehension; 25, 26; 25, 29; 25, 34; 26, tuple; 26, 27; 26, 28; 27, identifier:c; 28, identifier:x; 29, for_in_clause; 29, 30; 29, 33; 30, pattern_list; 30, 31; 30, 32; 31, identifier:c; 32, identifier:x; 33, identifier:sorted_files; 34, if_clause; 34, 35; 35, boolean_operator:and; 35, 36; 35, 44; 36, call; 36, 37; 36, 42; 37, attribute; 37, 38; 37, 41; 38, attribute; 38, 39; 38, 40; 39, identifier:os; 40, identifier:path; 41, identifier:exists; 42, argument_list; 42, 43; 43, identifier:x; 44, call; 44, 45; 44, 46; 45, identifier:vcf_has_variants; 46, argument_list; 46, 47; 47, identifier:x; 48, if_statement; 48, 49; 48, 55; 48, 56; 48, 76; 48, 93; 49, comparison_operator:==; 49, 50; 49, 54; 50, call; 50, 51; 50, 52; 51, identifier:len; 52, argument_list; 52, 53; 53, identifier:exist_files; 54, integer:0; 55, comment; 56, block; 56, 57; 57, expression_statement; 57, 58; 58, assignment; 58, 59; 58, 60; 59, identifier:exist_files; 60, list_comprehension; 60, 61; 60, 62; 60, 67; 61, identifier:x; 62, for_in_clause; 62, 63; 62, 66; 63, pattern_list; 63, 64; 63, 65; 64, identifier:c; 65, identifier:x; 66, identifier:sorted_files; 67, if_clause; 67, 68; 68, call; 68, 69; 68, 74; 69, attribute; 69, 70; 69, 73; 70, attribute; 70, 71; 70, 72; 71, identifier:os; 72, identifier:path; 73, identifier:exists; 74, argument_list; 74, 75; 75, identifier:x; 76, elif_clause; 76, 77; 76, 83; 77, comparison_operator:>; 77, 78; 77, 82; 78, call; 78, 79; 78, 80; 79, identifier:len; 80, argument_list; 80, 81; 81, identifier:exist_files; 82, integer:1; 83, block; 83, 84; 84, expression_statement; 84, 85; 85, assignment; 85, 86; 85, 87; 86, identifier:exist_files; 87, call; 87, 88; 87, 89; 88, identifier:_fix_gatk_header; 89, argument_list; 89, 90; 89, 91; 89, 92; 90, identifier:exist_files; 91, identifier:out_file; 92, identifier:config; 93, else_clause; 93, 94; 94, block; 94, 95; 95, expression_statement; 95, 96; 96, assignment; 96, 97; 96, 98; 97, identifier:exist_files; 98, list_comprehension; 98, 99; 98, 100; 99, identifier:x; 100, for_in_clause; 100, 101; 100, 104; 101, pattern_list; 101, 102; 101, 103; 102, identifier:c; 103, identifier:x; 104, identifier:exist_files; 105, expression_statement; 105, 106; 106, assignment; 106, 107; 106, 108; 107, identifier:ready_files; 108, call; 108, 109; 108, 110; 109, identifier:run_multicore; 110, argument_list; 110, 111; 110, 112; 110, 119; 111, identifier:p_bgzip_and_index; 112, list_comprehension; 112, 113; 112, 116; 113, list:[x, config]; 113, 114; 113, 115; 114, identifier:x; 115, identifier:config; 116, for_in_clause; 116, 117; 116, 118; 117, identifier:x; 118, identifier:exist_files; 119, identifier:config; 120, expression_statement; 120, 121; 121, assignment; 121, 122; 121, 123; 122, identifier:input_file_list; 123, binary_operator:%; 123, 124; 123, 125; 124, string:"%s-files.list"; 125, subscript; 125, 126; 125, 132; 126, call; 126, 127; 126, 130; 127, attribute; 127, 128; 127, 129; 128, identifier:utils; 129, identifier:splitext_plus; 130, argument_list; 130, 131; 131, identifier:out_file; 132, integer:0; 133, with_statement; 133, 134; 133, 144; 134, with_clause; 134, 135; 135, with_item; 135, 136; 136, as_pattern; 136, 137; 136, 142; 137, call; 137, 138; 137, 139; 138, identifier:open; 139, argument_list; 139, 140; 139, 141; 140, identifier:input_file_list; 141, string:"w"; 142, as_pattern_target; 142, 143; 143, identifier:out_handle; 144, block; 144, 145; 145, for_statement; 145, 146; 145, 147; 145, 148; 146, identifier:fname; 147, identifier:ready_files; 148, block; 148, 149; 149, expression_statement; 149, 150; 150, call; 150, 151; 150, 154; 151, attribute; 151, 152; 151, 153; 152, identifier:out_handle; 153, identifier:write; 154, argument_list; 154, 155; 155, binary_operator:+; 155, 156; 155, 157; 156, identifier:fname; 157, string:"\n"; 158, return_statement; 158, 159; 159, identifier:input_file_list
def _get_file_list(orig_files, out_file, regions, ref_file, config): """Create file with region sorted list of non-empty VCFs for concatenating. """ sorted_files = _sort_by_region(orig_files, regions, ref_file, config) exist_files = [(c, x) for c, x in sorted_files if os.path.exists(x) and vcf_has_variants(x)] if len(exist_files) == 0: # no non-empty inputs, merge the empty ones exist_files = [x for c, x in sorted_files if os.path.exists(x)] elif len(exist_files) > 1: exist_files = _fix_gatk_header(exist_files, out_file, config) else: exist_files = [x for c, x in exist_files] ready_files = run_multicore(p_bgzip_and_index, [[x, config] for x in exist_files], config) input_file_list = "%s-files.list" % utils.splitext_plus(out_file)[0] with open(input_file_list, "w") as out_handle: for fname in ready_files: out_handle.write(fname + "\n") return input_file_list
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:sort_by_ref; 3, parameters; 3, 4; 3, 5; 4, identifier:vcf_file; 5, identifier:data; 6, block; 6, 7; 6, 9; 6, 22; 6, 153; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:out_file; 12, binary_operator:%; 12, 13; 12, 14; 13, string:"%s-prep.vcf.gz"; 14, subscript; 14, 15; 14, 21; 15, call; 15, 16; 15, 19; 16, attribute; 16, 17; 16, 18; 17, identifier:utils; 18, identifier:splitext_plus; 19, argument_list; 19, 20; 20, identifier:vcf_file; 21, integer:0; 22, if_statement; 22, 23; 22, 31; 23, not_operator; 23, 24; 24, call; 24, 25; 24, 28; 25, attribute; 25, 26; 25, 27; 26, identifier:utils; 27, identifier:file_uptodate; 28, argument_list; 28, 29; 28, 30; 29, identifier:out_file; 30, identifier:vcf_file; 31, block; 31, 32; 32, with_statement; 32, 33; 32, 43; 33, with_clause; 33, 34; 34, with_item; 34, 35; 35, as_pattern; 35, 36; 35, 41; 36, call; 36, 37; 36, 38; 37, identifier:file_transaction; 38, argument_list; 38, 39; 38, 40; 39, identifier:data; 40, identifier:out_file; 41, as_pattern_target; 41, 42; 42, identifier:tx_out_file; 43, block; 43, 44; 43, 57; 43, 101; 43, 113; 43, 120; 44, expression_statement; 44, 45; 45, assignment; 45, 46; 45, 47; 46, identifier:header_file; 47, binary_operator:%; 47, 48; 47, 49; 48, string:"%s-header.txt"; 49, subscript; 49, 50; 49, 56; 50, call; 50, 51; 50, 54; 51, attribute; 51, 52; 51, 53; 52, identifier:utils; 53, identifier:splitext_plus; 54, argument_list; 54, 55; 55, identifier:tx_out_file; 56, integer:0; 57, with_statement; 57, 58; 57, 68; 58, with_clause; 58, 59; 59, with_item; 59, 60; 60, as_pattern; 60, 61; 60, 66; 61, call; 61, 62; 61, 63; 62, identifier:open; 63, argument_list; 63, 64; 63, 65; 64, identifier:header_file; 65, string:"w"; 66, as_pattern_target; 66, 67; 67, identifier:out_handle; 68, block; 68, 69; 69, for_statement; 69, 70; 69, 71; 69, 85; 70, identifier:region; 71, call; 71, 72; 71, 75; 72, attribute; 72, 73; 72, 74; 73, identifier:ref; 74, identifier:file_contigs; 75, argument_list; 75, 76; 75, 82; 76, call; 76, 77; 76, 80; 77, attribute; 77, 78; 77, 79; 78, identifier:dd; 79, identifier:get_ref_file; 80, argument_list; 80, 81; 81, identifier:data; 82, subscript; 82, 83; 82, 84; 83, identifier:data; 84, string:"config"; 85, block; 85, 86; 86, expression_statement; 86, 87; 87, call; 87, 88; 87, 91; 88, attribute; 88, 89; 88, 90; 89, identifier:out_handle; 90, identifier:write; 91, argument_list; 91, 92; 92, binary_operator:%; 92, 93; 92, 94; 93, string:"##contig=<ID=%s,length=%s>\n"; 94, tuple; 94, 95; 94, 98; 95, attribute; 95, 96; 95, 97; 96, identifier:region; 97, identifier:name; 98, attribute; 98, 99; 98, 100; 99, identifier:region; 100, identifier:size; 101, expression_statement; 101, 102; 102, assignment; 102, 103; 102, 104; 103, identifier:cat_cmd; 104, conditional_expression:if; 104, 105; 104, 106; 104, 112; 105, string:"zcat"; 106, call; 106, 107; 106, 110; 107, attribute; 107, 108; 107, 109; 108, identifier:vcf_file; 109, identifier:endswith; 110, argument_list; 110, 111; 111, string:"vcf.gz"; 112, string:"cat"; 113, expression_statement; 113, 114; 114, assignment; 114, 115; 114, 116; 115, identifier:cmd; 116, parenthesized_expression; 116, 117; 117, concatenated_string; 117, 118; 117, 119; 118, string:"{cat_cmd} {vcf_file} | grep -v ^##contig | bcftools annotate -h {header_file} | "; 119, string:"vt sort -m full -o {tx_out_file} -"; 120, with_statement; 120, 121; 120, 136; 121, with_clause; 121, 122; 122, with_item; 122, 123; 123, call; 123, 124; 123, 127; 124, attribute; 124, 125; 124, 126; 125, identifier:utils; 126, identifier:chdir; 127, argument_list; 127, 128; 128, call; 128, 129; 128, 134; 129, attribute; 129, 130; 129, 133; 130, attribute; 130, 131; 130, 132; 131, identifier:os; 132, identifier:path; 133, identifier:dirname; 134, argument_list; 134, 135; 135, identifier:tx_out_file; 136, block; 136, 137; 137, expression_statement; 137, 138; 138, call; 138, 139; 138, 142; 139, attribute; 139, 140; 139, 141; 140, identifier:do; 141, identifier:run; 142, argument_list; 142, 143; 142, 152; 143, call; 143, 144; 143, 147; 144, attribute; 144, 145; 144, 146; 145, identifier:cmd; 146, identifier:format; 147, argument_list; 147, 148; 148, dictionary_splat; 148, 149; 149, call; 149, 150; 149, 151; 150, identifier:locals; 151, argument_list; 152, string:"Sort VCF by reference"; 153, return_statement; 153, 154; 154, call; 154, 155; 154, 156; 155, identifier:bgzip_and_index; 156, argument_list; 156, 157; 156, 158; 157, identifier:out_file; 158, subscript; 158, 159; 158, 160; 159, identifier:data; 160, string:"config"
def sort_by_ref(vcf_file, data): """Sort a VCF file by genome reference and position, adding contig information. """ out_file = "%s-prep.vcf.gz" % utils.splitext_plus(vcf_file)[0] if not utils.file_uptodate(out_file, vcf_file): with file_transaction(data, out_file) as tx_out_file: header_file = "%s-header.txt" % utils.splitext_plus(tx_out_file)[0] with open(header_file, "w") as out_handle: for region in ref.file_contigs(dd.get_ref_file(data), data["config"]): out_handle.write("##contig=<ID=%s,length=%s>\n" % (region.name, region.size)) cat_cmd = "zcat" if vcf_file.endswith("vcf.gz") else "cat" cmd = ("{cat_cmd} {vcf_file} | grep -v ^##contig | bcftools annotate -h {header_file} | " "vt sort -m full -o {tx_out_file} -") with utils.chdir(os.path.dirname(tx_out_file)): do.run(cmd.format(**locals()), "Sort VCF by reference") return bgzip_and_index(out_file, data["config"])
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:align_to_sort_bam; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 4, identifier:fastq1; 5, identifier:fastq2; 6, identifier:aligner; 7, identifier:data; 8, block; 8, 9; 8, 11; 8, 17; 8, 30; 8, 49; 8, 57; 8, 74; 8, 87; 8, 124; 8, 215; 9, expression_statement; 9, 10; 10, comment; 11, expression_statement; 11, 12; 12, assignment; 12, 13; 12, 14; 13, identifier:names; 14, subscript; 14, 15; 14, 16; 15, identifier:data; 16, string:"rgnames"; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 20; 19, identifier:align_dir_parts; 20, list:[data["dirs"]["work"], "align", names["sample"]]; 20, 21; 20, 26; 20, 27; 21, subscript; 21, 22; 21, 25; 22, subscript; 22, 23; 22, 24; 23, identifier:data; 24, string:"dirs"; 25, string:"work"; 26, string:"align"; 27, subscript; 27, 28; 27, 29; 28, identifier:names; 29, string:"sample"; 30, if_statement; 30, 31; 30, 37; 31, call; 31, 32; 31, 35; 32, attribute; 32, 33; 32, 34; 33, identifier:data; 34, identifier:get; 35, argument_list; 35, 36; 36, string:"disambiguate"; 37, block; 37, 38; 38, expression_statement; 38, 39; 39, call; 39, 40; 39, 43; 40, attribute; 40, 41; 40, 42; 41, identifier:align_dir_parts; 42, identifier:append; 43, argument_list; 43, 44; 44, subscript; 44, 45; 44, 48; 45, subscript; 45, 46; 45, 47; 46, identifier:data; 47, string:"disambiguate"; 48, string:"genome_build"; 49, expression_statement; 49, 50; 50, assignment; 50, 51; 50, 52; 51, identifier:aligner_index; 52, call; 52, 53; 52, 54; 53, identifier:_get_aligner_index; 54, argument_list; 54, 55; 54, 56; 55, identifier:aligner; 56, identifier:data; 57, expression_statement; 57, 58; 58, assignment; 58, 59; 58, 60; 59, identifier:align_dir; 60, call; 60, 61; 60, 64; 61, attribute; 61, 62; 61, 63; 62, identifier:utils; 63, identifier:safe_makedir; 64, argument_list; 64, 65; 65, call; 65, 66; 65, 71; 66, attribute; 66, 67; 66, 70; 67, attribute; 67, 68; 67, 69; 68, identifier:os; 69, identifier:path; 70, identifier:join; 71, argument_list; 71, 72; 72, list_splat; 72, 73; 73, identifier:align_dir_parts; 74, expression_statement; 74, 75; 75, assignment; 75, 76; 75, 77; 76, identifier:ref_file; 77, call; 77, 78; 77, 81; 78, attribute; 78, 79; 78, 80; 79, identifier:tz; 80, identifier:get_in; 81, argument_list; 81, 82; 81, 86; 82, tuple; 82, 83; 82, 84; 82, 85; 83, string:"reference"; 84, string:"fasta"; 85, string:"base"; 86, identifier:data; 87, if_statement; 87, 88; 87, 94; 87, 108; 88, call; 88, 89; 88, 92; 89, attribute; 89, 90; 89, 91; 90, identifier:fastq1; 91, identifier:endswith; 92, argument_list; 92, 93; 93, string:".bam"; 94, block; 94, 95; 95, expression_statement; 95, 96; 96, assignment; 96, 97; 96, 98; 97, identifier:data; 98, call; 98, 99; 98, 100; 99, identifier:_align_from_bam; 100, argument_list; 100, 101; 100, 102; 100, 103; 100, 104; 100, 105; 100, 106; 100, 107; 101, identifier:fastq1; 102, identifier:aligner; 103, identifier:aligner_index; 104, identifier:ref_file; 105, identifier:names; 106, identifier:align_dir; 107, identifier:data; 108, else_clause; 108, 109; 109, block; 109, 110; 110, expression_statement; 110, 111; 111, assignment; 111, 112; 111, 113; 112, identifier:data; 113, call; 113, 114; 113, 115; 114, identifier:_align_from_fastq; 115, argument_list; 115, 116; 115, 117; 115, 118; 115, 119; 115, 120; 115, 121; 115, 122; 115, 123; 116, identifier:fastq1; 117, identifier:fastq2; 118, identifier:aligner; 119, identifier:aligner_index; 120, identifier:ref_file; 121, identifier:names; 122, identifier:align_dir; 123, identifier:data; 124, if_statement; 124, 125; 124, 137; 125, boolean_operator:and; 125, 126; 125, 129; 126, subscript; 126, 127; 126, 128; 127, identifier:data; 128, string:"work_bam"; 129, call; 129, 130; 129, 133; 130, attribute; 130, 131; 130, 132; 131, identifier:utils; 132, identifier:file_exists; 133, argument_list; 133, 134; 134, subscript; 134, 135; 134, 136; 135, identifier:data; 136, string:"work_bam"; 137, block; 137, 138; 137, 179; 138, if_statement; 138, 139; 138, 152; 138, 153; 138, 154; 138, 165; 139, boolean_operator:and; 139, 140; 139, 146; 140, call; 140, 141; 140, 144; 141, attribute; 141, 142; 141, 143; 142, identifier:data; 143, identifier:get; 144, argument_list; 144, 145; 145, string:"align_split"; 146, call; 146, 147; 146, 150; 147, attribute; 147, 148; 147, 149; 148, identifier:dd; 149, identifier:get_mark_duplicates; 150, argument_list; 150, 151; 151, identifier:data; 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:bam; 159, identifier:fake_index; 160, argument_list; 160, 161; 160, 164; 161, subscript; 161, 162; 161, 163; 162, identifier:data; 163, string:"work_bam"; 164, identifier:data; 165, else_clause; 165, 166; 166, block; 166, 167; 167, expression_statement; 167, 168; 168, call; 168, 169; 168, 172; 169, attribute; 169, 170; 169, 171; 170, identifier:bam; 171, identifier:index; 172, argument_list; 172, 173; 172, 176; 173, subscript; 173, 174; 173, 175; 174, identifier:data; 175, string:"work_bam"; 176, subscript; 176, 177; 176, 178; 177, identifier:data; 178, string:"config"; 179, for_statement; 179, 180; 179, 181; 179, 184; 180, identifier:extra; 181, list:["-sr", "-disc"]; 181, 182; 181, 183; 182, string:"-sr"; 183, string:"-disc"; 184, block; 184, 185; 184, 197; 185, expression_statement; 185, 186; 186, assignment; 186, 187; 186, 188; 187, identifier:extra_bam; 188, call; 188, 189; 188, 192; 189, attribute; 189, 190; 189, 191; 190, identifier:utils; 191, identifier:append_stem; 192, argument_list; 192, 193; 192, 196; 193, subscript; 193, 194; 193, 195; 194, identifier:data; 195, string:'work_bam'; 196, identifier:extra; 197, if_statement; 197, 198; 197, 204; 198, call; 198, 199; 198, 202; 199, attribute; 199, 200; 199, 201; 200, identifier:utils; 201, identifier:file_exists; 202, argument_list; 202, 203; 203, identifier:extra_bam; 204, block; 204, 205; 205, expression_statement; 205, 206; 206, call; 206, 207; 206, 210; 207, attribute; 207, 208; 207, 209; 208, identifier:bam; 209, identifier:index; 210, argument_list; 210, 211; 210, 212; 211, identifier:extra_bam; 212, subscript; 212, 213; 212, 214; 213, identifier:data; 214, string:"config"; 215, return_statement; 215, 216; 216, identifier:data
def align_to_sort_bam(fastq1, fastq2, aligner, data): """Align to the named genome build, returning a sorted BAM file. """ names = data["rgnames"] align_dir_parts = [data["dirs"]["work"], "align", names["sample"]] if data.get("disambiguate"): align_dir_parts.append(data["disambiguate"]["genome_build"]) aligner_index = _get_aligner_index(aligner, data) align_dir = utils.safe_makedir(os.path.join(*align_dir_parts)) ref_file = tz.get_in(("reference", "fasta", "base"), data) if fastq1.endswith(".bam"): data = _align_from_bam(fastq1, aligner, aligner_index, ref_file, names, align_dir, data) else: data = _align_from_fastq(fastq1, fastq2, aligner, aligner_index, ref_file, names, align_dir, data) if data["work_bam"] and utils.file_exists(data["work_bam"]): if data.get("align_split") and dd.get_mark_duplicates(data): # If merging later with with bamsormadup need query sorted inputs # but CWL requires a bai file. Create a fake one to make it happy. bam.fake_index(data["work_bam"], data) else: bam.index(data["work_bam"], data["config"]) for extra in ["-sr", "-disc"]: extra_bam = utils.append_stem(data['work_bam'], extra) if utils.file_exists(extra_bam): bam.index(extra_bam, data["config"]) return data
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 12; 2, function_name:_align_from_fastq; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 3, 9; 3, 10; 3, 11; 4, identifier:fastq1; 5, identifier:fastq2; 6, identifier:aligner; 7, identifier:align_ref; 8, identifier:sam_ref; 9, identifier:names; 10, identifier:align_dir; 11, identifier:data; 12, block; 12, 13; 12, 15; 12, 21; 12, 29; 12, 41; 12, 42; 13, expression_statement; 13, 14; 14, comment; 15, expression_statement; 15, 16; 16, assignment; 16, 17; 16, 18; 17, identifier:config; 18, subscript; 18, 19; 18, 20; 19, identifier:data; 20, string:"config"; 21, expression_statement; 21, 22; 22, assignment; 22, 23; 22, 24; 23, identifier:align_fn; 24, attribute; 24, 25; 24, 28; 25, subscript; 25, 26; 25, 27; 26, identifier:TOOLS; 27, identifier:aligner; 28, identifier:align_fn; 29, expression_statement; 29, 30; 30, assignment; 30, 31; 30, 32; 31, identifier:out; 32, call; 32, 33; 32, 34; 33, identifier:align_fn; 34, argument_list; 34, 35; 34, 36; 34, 37; 34, 38; 34, 39; 34, 40; 35, identifier:fastq1; 36, identifier:fastq2; 37, identifier:align_ref; 38, identifier:names; 39, identifier:align_dir; 40, identifier:data; 41, comment; 42, if_statement; 42, 43; 42, 48; 42, 71; 42, 72; 43, call; 43, 44; 43, 45; 44, identifier:isinstance; 45, argument_list; 45, 46; 45, 47; 46, identifier:out; 47, identifier:dict; 48, block; 48, 49; 48, 69; 49, assert_statement; 49, 50; 49, 56; 50, call; 50, 51; 50, 54; 51, attribute; 51, 52; 51, 53; 52, identifier:out; 53, identifier:get; 54, argument_list; 54, 55; 55, string:"work_bam"; 56, tuple; 56, 57; 56, 63; 57, call; 57, 58; 57, 61; 58, attribute; 58, 59; 58, 60; 59, identifier:dd; 60, identifier:get_sample_name; 61, argument_list; 61, 62; 62, identifier:data; 63, call; 63, 64; 63, 67; 64, attribute; 64, 65; 64, 66; 65, identifier:out; 66, identifier:get; 67, argument_list; 67, 68; 68, string:"work_bam"; 69, return_statement; 69, 70; 70, identifier:out; 71, comment; 72, else_clause; 72, 73; 73, block; 73, 74; 73, 84; 73, 96; 74, expression_statement; 74, 75; 75, assignment; 75, 76; 75, 77; 76, identifier:work_bam; 77, call; 77, 78; 77, 81; 78, attribute; 78, 79; 78, 80; 79, identifier:bam; 80, identifier:sam_to_bam; 81, argument_list; 81, 82; 81, 83; 82, identifier:out; 83, identifier:config; 84, expression_statement; 84, 85; 85, assignment; 85, 86; 85, 89; 86, subscript; 86, 87; 86, 88; 87, identifier:data; 88, string:"work_bam"; 89, call; 89, 90; 89, 93; 90, attribute; 90, 91; 90, 92; 91, identifier:bam; 92, identifier:sort; 93, argument_list; 93, 94; 93, 95; 94, identifier:work_bam; 95, identifier:config; 96, return_statement; 96, 97; 97, identifier:data
def _align_from_fastq(fastq1, fastq2, aligner, align_ref, sam_ref, names, align_dir, data): """Align from fastq inputs, producing sorted BAM output. """ config = data["config"] align_fn = TOOLS[aligner].align_fn out = align_fn(fastq1, fastq2, align_ref, names, align_dir, data) # handle align functions that update the main data dictionary in place if isinstance(out, dict): assert out.get("work_bam"), (dd.get_sample_name(data), out.get("work_bam")) return out # handle output of raw SAM files that need to be converted to BAM else: work_bam = bam.sam_to_bam(out, config) data["work_bam"] = bam.sort(work_bam, config) return data
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 2, function_name:picard_prep; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 8; 4, identifier:in_bam; 5, identifier:names; 6, identifier:ref_file; 7, identifier:dirs; 8, identifier:data; 9, block; 9, 10; 9, 12; 9, 24; 9, 46; 9, 54; 9, 84; 9, 112; 9, 124; 9, 135; 10, expression_statement; 10, 11; 11, comment; 12, expression_statement; 12, 13; 13, assignment; 13, 14; 13, 15; 14, identifier:runner; 15, call; 15, 16; 15, 19; 16, attribute; 16, 17; 16, 18; 17, identifier:broad; 18, identifier:runner_from_path; 19, argument_list; 19, 20; 19, 21; 20, string:"picard"; 21, subscript; 21, 22; 21, 23; 22, identifier:data; 23, string:"config"; 24, expression_statement; 24, 25; 25, assignment; 25, 26; 25, 27; 26, identifier:work_dir; 27, call; 27, 28; 27, 31; 28, attribute; 28, 29; 28, 30; 29, identifier:utils; 30, identifier:safe_makedir; 31, argument_list; 31, 32; 32, call; 32, 33; 32, 38; 33, attribute; 33, 34; 33, 37; 34, attribute; 34, 35; 34, 36; 35, identifier:os; 36, identifier:path; 37, identifier:join; 38, argument_list; 38, 39; 38, 42; 38, 43; 39, subscript; 39, 40; 39, 41; 40, identifier:dirs; 41, string:"work"; 42, string:"bamclean"; 43, subscript; 43, 44; 43, 45; 44, identifier:names; 45, string:"sample"; 46, expression_statement; 46, 47; 47, call; 47, 48; 47, 51; 48, attribute; 48, 49; 48, 50; 49, identifier:runner; 50, identifier:run_fn; 51, argument_list; 51, 52; 51, 53; 52, string:"picard_index_ref"; 53, identifier:ref_file; 54, expression_statement; 54, 55; 55, assignment; 55, 56; 55, 57; 56, identifier:reorder_bam; 57, call; 57, 58; 57, 63; 58, attribute; 58, 59; 58, 62; 59, attribute; 59, 60; 59, 61; 60, identifier:os; 61, identifier:path; 62, identifier:join; 63, argument_list; 63, 64; 63, 65; 64, identifier:work_dir; 65, binary_operator:%; 65, 66; 65, 67; 66, string:"%s-reorder.bam"; 67, subscript; 67, 68; 67, 83; 68, call; 68, 69; 68, 74; 69, attribute; 69, 70; 69, 73; 70, attribute; 70, 71; 70, 72; 71, identifier:os; 72, identifier:path; 73, identifier:splitext; 74, argument_list; 74, 75; 75, call; 75, 76; 75, 81; 76, attribute; 76, 77; 76, 80; 77, attribute; 77, 78; 77, 79; 78, identifier:os; 79, identifier:path; 80, identifier:basename; 81, argument_list; 81, 82; 82, identifier:in_bam; 83, integer:0; 84, if_statement; 84, 85; 84, 92; 85, not_operator; 85, 86; 86, call; 86, 87; 86, 90; 87, attribute; 87, 88; 87, 89; 88, identifier:utils; 89, identifier:file_exists; 90, argument_list; 90, 91; 91, identifier:reorder_bam; 92, block; 92, 93; 93, expression_statement; 93, 94; 94, assignment; 94, 95; 94, 96; 95, identifier:reorder_bam; 96, call; 96, 97; 96, 102; 97, attribute; 97, 98; 97, 101; 98, attribute; 98, 99; 98, 100; 99, identifier:os; 100, identifier:path; 101, identifier:join; 102, argument_list; 102, 103; 102, 104; 103, identifier:work_dir; 104, binary_operator:%; 104, 105; 104, 106; 105, string:"%s-reorder.bam"; 106, call; 106, 107; 106, 110; 107, attribute; 107, 108; 107, 109; 108, identifier:dd; 109, identifier:get_sample_name; 110, argument_list; 110, 111; 111, identifier:data; 112, expression_statement; 112, 113; 113, assignment; 113, 114; 113, 115; 114, identifier:reorder_bam; 115, call; 115, 116; 115, 119; 116, attribute; 116, 117; 116, 118; 117, identifier:runner; 118, identifier:run_fn; 119, argument_list; 119, 120; 119, 121; 119, 122; 119, 123; 120, string:"picard_reorder"; 121, identifier:in_bam; 122, identifier:ref_file; 123, identifier:reorder_bam; 124, expression_statement; 124, 125; 125, assignment; 125, 126; 125, 127; 126, identifier:rg_bam; 127, call; 127, 128; 127, 131; 128, attribute; 128, 129; 128, 130; 129, identifier:runner; 130, identifier:run_fn; 131, argument_list; 131, 132; 131, 133; 131, 134; 132, string:"picard_fix_rgs"; 133, identifier:reorder_bam; 134, identifier:names; 135, return_statement; 135, 136; 136, call; 136, 137; 136, 138; 137, identifier:_filter_bad_reads; 138, argument_list; 138, 139; 138, 140; 138, 141; 139, identifier:rg_bam; 140, identifier:ref_file; 141, identifier:data
def picard_prep(in_bam, names, ref_file, dirs, data): """Prepare input BAM using Picard and GATK cleaning tools. - ReorderSam to reorder file to reference - AddOrReplaceReadGroups to add read group information and coordinate sort - PrintReads to filters to remove problem records: - filterMBQ to remove reads with mismatching bases and base qualities """ runner = broad.runner_from_path("picard", data["config"]) work_dir = utils.safe_makedir(os.path.join(dirs["work"], "bamclean", names["sample"])) runner.run_fn("picard_index_ref", ref_file) reorder_bam = os.path.join(work_dir, "%s-reorder.bam" % os.path.splitext(os.path.basename(in_bam))[0]) if not utils.file_exists(reorder_bam): reorder_bam = os.path.join(work_dir, "%s-reorder.bam" % dd.get_sample_name(data)) reorder_bam = runner.run_fn("picard_reorder", in_bam, ref_file, reorder_bam) rg_bam = runner.run_fn("picard_fix_rgs", reorder_bam, names) return _filter_bad_reads(rg_bam, ref_file, data)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:iterate_flattened_separately; 3, parameters; 3, 4; 3, 5; 4, identifier:dictionary; 5, default_parameter; 5, 6; 5, 7; 6, identifier:manually_sorted_keys; 7, None; 8, block; 8, 9; 8, 11; 8, 20; 8, 36; 8, 67; 8, 81; 8, 110; 9, expression_statement; 9, 10; 10, comment; 11, if_statement; 11, 12; 11, 15; 12, comparison_operator:is; 12, 13; 12, 14; 13, identifier:manually_sorted_keys; 14, None; 15, block; 15, 16; 16, expression_statement; 16, 17; 17, assignment; 17, 18; 17, 19; 18, identifier:manually_sorted_keys; 19, list:[]; 20, for_statement; 20, 21; 20, 22; 20, 23; 21, identifier:key; 22, identifier:manually_sorted_keys; 23, block; 23, 24; 24, if_statement; 24, 25; 24, 28; 25, comparison_operator:in; 25, 26; 25, 27; 26, identifier:key; 27, identifier:dictionary; 28, block; 28, 29; 29, expression_statement; 29, 30; 30, yield; 30, 31; 31, expression_list; 31, 32; 31, 33; 32, identifier:key; 33, subscript; 33, 34; 33, 35; 34, identifier:dictionary; 35, identifier:key; 36, expression_statement; 36, 37; 37, assignment; 37, 38; 37, 39; 38, identifier:single_line_keys; 39, list_comprehension; 39, 40; 39, 41; 39, 48; 40, identifier:key; 41, for_in_clause; 41, 42; 41, 43; 42, identifier:key; 43, call; 43, 44; 43, 47; 44, attribute; 44, 45; 44, 46; 45, identifier:dictionary; 46, identifier:keys; 47, argument_list; 48, if_clause; 48, 49; 49, boolean_operator:and; 49, 50; 49, 53; 50, comparison_operator:not; 50, 51; 50, 52; 51, identifier:key; 52, identifier:manually_sorted_keys; 53, parenthesized_expression; 53, 54; 54, boolean_operator:or; 54, 55; 54, 59; 55, not_operator; 55, 56; 56, subscript; 56, 57; 56, 58; 57, identifier:dictionary; 58, identifier:key; 59, not_operator; 59, 60; 60, call; 60, 61; 60, 62; 61, identifier:isinstance; 62, argument_list; 62, 63; 62, 66; 63, subscript; 63, 64; 63, 65; 64, identifier:dictionary; 65, identifier:key; 66, identifier:dict; 67, for_statement; 67, 68; 67, 69; 67, 73; 68, identifier:key; 69, call; 69, 70; 69, 71; 70, identifier:sorted; 71, argument_list; 71, 72; 72, identifier:single_line_keys; 73, block; 73, 74; 74, expression_statement; 74, 75; 75, yield; 75, 76; 76, expression_list; 76, 77; 76, 78; 77, identifier:key; 78, subscript; 78, 79; 78, 80; 79, identifier:dictionary; 80, identifier:key; 81, expression_statement; 81, 82; 82, assignment; 82, 83; 82, 84; 83, identifier:multi_line_keys; 84, list_comprehension; 84, 85; 84, 86; 84, 93; 85, identifier:key; 86, for_in_clause; 86, 87; 86, 88; 87, identifier:key; 88, call; 88, 89; 88, 92; 89, attribute; 89, 90; 89, 91; 90, identifier:dictionary; 91, identifier:keys; 92, argument_list; 93, if_clause; 93, 94; 94, boolean_operator:and; 94, 95; 94, 98; 95, comparison_operator:not; 95, 96; 95, 97; 96, identifier:key; 97, identifier:manually_sorted_keys; 98, parenthesized_expression; 98, 99; 99, boolean_operator:and; 99, 100; 99, 103; 100, subscript; 100, 101; 100, 102; 101, identifier:dictionary; 102, identifier:key; 103, call; 103, 104; 103, 105; 104, identifier:isinstance; 105, argument_list; 105, 106; 105, 109; 106, subscript; 106, 107; 106, 108; 107, identifier:dictionary; 108, identifier:key; 109, identifier:dict; 110, for_statement; 110, 111; 110, 112; 110, 116; 111, identifier:key; 112, call; 112, 113; 112, 114; 113, identifier:sorted; 114, argument_list; 114, 115; 115, identifier:multi_line_keys; 116, block; 116, 117; 116, 122; 117, expression_statement; 117, 118; 118, yield; 118, 119; 119, expression_list; 119, 120; 119, 121; 120, identifier:key; 121, identifier:PATHCHANGE; 122, for_statement; 122, 123; 122, 126; 122, 133; 123, pattern_list; 123, 124; 123, 125; 124, identifier:k; 125, identifier:val; 126, call; 126, 127; 126, 128; 127, identifier:iterate_flattened_separately; 128, argument_list; 128, 129; 128, 132; 129, subscript; 129, 130; 129, 131; 130, identifier:dictionary; 131, identifier:key; 132, identifier:manually_sorted_keys; 133, block; 133, 134; 134, expression_statement; 134, 135; 135, yield; 135, 136; 136, expression_list; 136, 137; 136, 142; 137, call; 137, 138; 137, 139; 138, identifier:join_paths; 139, argument_list; 139, 140; 139, 141; 140, identifier:key; 141, identifier:k; 142, identifier:val
def iterate_flattened_separately(dictionary, manually_sorted_keys=None): """ Recursively iterate over the items of a dictionary in a special order. First iterate over manually sorted keys and then over all items that are non-dictionary values (sorted by keys), then over the rest (sorted by keys), providing full dotted paths for every leaf. """ if manually_sorted_keys is None: manually_sorted_keys = [] for key in manually_sorted_keys: if key in dictionary: yield key, dictionary[key] single_line_keys = [key for key in dictionary.keys() if key not in manually_sorted_keys and (not dictionary[key] or not isinstance(dictionary[key], dict))] for key in sorted(single_line_keys): yield key, dictionary[key] multi_line_keys = [key for key in dictionary.keys() if key not in manually_sorted_keys and (dictionary[key] and isinstance(dictionary[key], dict))] for key in sorted(multi_line_keys): yield key, PATHCHANGE for k, val in iterate_flattened_separately(dictionary[key], manually_sorted_keys): yield join_paths(key, k), val
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:gather_command_line_options; 3, parameters; 3, 4; 4, default_parameter; 4, 5; 4, 6; 5, identifier:filter_disabled; 6, None; 7, block; 7, 8; 7, 10; 7, 24; 7, 42; 8, expression_statement; 8, 9; 9, comment; 10, if_statement; 10, 11; 10, 14; 11, comparison_operator:is; 11, 12; 11, 13; 12, identifier:filter_disabled; 13, None; 14, block; 14, 15; 15, expression_statement; 15, 16; 16, assignment; 16, 17; 16, 18; 17, identifier:filter_disabled; 18, not_operator; 18, 19; 19, attribute; 19, 20; 19, 23; 20, attribute; 20, 21; 20, 22; 21, identifier:SETTINGS; 22, identifier:COMMAND_LINE; 23, identifier:SHOW_DISABLED_OPTIONS; 24, expression_statement; 24, 25; 25, assignment; 25, 26; 25, 27; 26, identifier:options; 27, list_comprehension; 27, 28; 27, 29; 27, 35; 28, identifier:opt; 29, for_in_clause; 29, 30; 29, 31; 30, identifier:opt; 31, call; 31, 32; 31, 33; 32, identifier:get_inheritors; 33, argument_list; 33, 34; 34, identifier:CommandLineOption; 35, if_clause; 35, 36; 36, boolean_operator:or; 36, 37; 36, 39; 37, not_operator; 37, 38; 38, identifier:filter_disabled; 39, attribute; 39, 40; 39, 41; 40, identifier:opt; 41, identifier:_enabled; 42, return_statement; 42, 43; 43, call; 43, 44; 43, 45; 44, identifier:sorted; 45, argument_list; 45, 46; 45, 47; 46, identifier:options; 47, keyword_argument; 47, 48; 47, 49; 48, identifier:key; 49, lambda; 49, 50; 49, 52; 50, lambda_parameters; 50, 51; 51, identifier:opt; 52, attribute; 52, 53; 52, 54; 53, identifier:opt; 54, identifier:__name__
def gather_command_line_options(filter_disabled=None): """Get a sorted list of all CommandLineOption subclasses.""" if filter_disabled is None: filter_disabled = not SETTINGS.COMMAND_LINE.SHOW_DISABLED_OPTIONS options = [opt for opt in get_inheritors(CommandLineOption) if not filter_disabled or opt._enabled] return sorted(options, key=lambda opt: opt.__name__)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 37; 2, function_name:bar; 3, parameters; 3, 4; 3, 5; 3, 10; 3, 13; 3, 16; 3, 19; 3, 22; 3, 25; 3, 28; 3, 31; 3, 34; 4, identifier:df; 5, default_parameter; 5, 6; 5, 7; 6, identifier:figsize; 7, tuple; 7, 8; 7, 9; 8, integer:24; 9, integer:10; 10, default_parameter; 10, 11; 10, 12; 11, identifier:fontsize; 12, integer:16; 13, default_parameter; 13, 14; 13, 15; 14, identifier:labels; 15, None; 16, default_parameter; 16, 17; 16, 18; 17, identifier:log; 18, False; 19, default_parameter; 19, 20; 19, 21; 20, identifier:color; 21, string:'dimgray'; 22, default_parameter; 22, 23; 22, 24; 23, identifier:inline; 24, False; 25, default_parameter; 25, 26; 25, 27; 26, identifier:filter; 27, None; 28, default_parameter; 28, 29; 28, 30; 29, identifier:n; 30, integer:0; 31, default_parameter; 31, 32; 31, 33; 32, identifier:p; 33, integer:0; 34, default_parameter; 34, 35; 34, 36; 35, identifier:sort; 36, None; 37, block; 37, 38; 37, 40; 37, 57; 37, 73; 37, 83; 37, 92; 37, 119; 37, 127; 37, 132; 37, 133; 37, 294; 37, 295; 37, 303; 37, 310; 37, 321; 37, 332; 37, 350; 37, 357; 37, 423; 38, expression_statement; 38, 39; 39, comment; 40, expression_statement; 40, 41; 41, assignment; 41, 42; 41, 43; 42, identifier:nullity_counts; 43, binary_operator:-; 43, 44; 43, 48; 44, call; 44, 45; 44, 46; 45, identifier:len; 46, argument_list; 46, 47; 47, identifier:df; 48, call; 48, 49; 48, 56; 49, attribute; 49, 50; 49, 55; 50, call; 50, 51; 50, 54; 51, attribute; 51, 52; 51, 53; 52, identifier:df; 53, identifier:isnull; 54, argument_list; 55, identifier:sum; 56, argument_list; 57, expression_statement; 57, 58; 58, assignment; 58, 59; 58, 60; 59, identifier:df; 60, call; 60, 61; 60, 62; 61, identifier:nullity_filter; 62, argument_list; 62, 63; 62, 64; 62, 67; 62, 70; 63, identifier:df; 64, keyword_argument; 64, 65; 64, 66; 65, identifier:filter; 66, identifier:filter; 67, keyword_argument; 67, 68; 67, 69; 68, identifier:n; 69, identifier:n; 70, keyword_argument; 70, 71; 70, 72; 71, identifier:p; 72, identifier:p; 73, expression_statement; 73, 74; 74, assignment; 74, 75; 74, 76; 75, identifier:df; 76, call; 76, 77; 76, 78; 77, identifier:nullity_sort; 78, argument_list; 78, 79; 78, 80; 79, identifier:df; 80, keyword_argument; 80, 81; 80, 82; 81, identifier:sort; 82, identifier:sort; 83, expression_statement; 83, 84; 84, call; 84, 85; 84, 88; 85, attribute; 85, 86; 85, 87; 86, identifier:plt; 87, identifier:figure; 88, argument_list; 88, 89; 89, keyword_argument; 89, 90; 89, 91; 90, identifier:figsize; 91, identifier:figsize; 92, expression_statement; 92, 93; 93, call; 93, 94; 93, 103; 94, attribute; 94, 95; 94, 102; 95, parenthesized_expression; 95, 96; 96, binary_operator:/; 96, 97; 96, 98; 97, identifier:nullity_counts; 98, call; 98, 99; 98, 100; 99, identifier:len; 100, argument_list; 100, 101; 101, identifier:df; 102, identifier:plot; 103, argument_list; 103, 104; 103, 107; 103, 110; 103, 113; 103, 116; 104, keyword_argument; 104, 105; 104, 106; 105, identifier:kind; 106, string:'bar'; 107, keyword_argument; 107, 108; 107, 109; 108, identifier:figsize; 109, identifier:figsize; 110, keyword_argument; 110, 111; 110, 112; 111, identifier:fontsize; 112, identifier:fontsize; 113, keyword_argument; 113, 114; 113, 115; 114, identifier:log; 115, identifier:log; 116, keyword_argument; 116, 117; 116, 118; 117, identifier:color; 118, identifier:color; 119, expression_statement; 119, 120; 120, assignment; 120, 121; 120, 122; 121, identifier:ax1; 122, call; 122, 123; 122, 126; 123, attribute; 123, 124; 123, 125; 124, identifier:plt; 125, identifier:gca; 126, argument_list; 127, expression_statement; 127, 128; 128, assignment; 128, 129; 128, 130; 129, identifier:axes; 130, list:[ax1]; 130, 131; 131, identifier:ax1; 132, comment; 133, if_statement; 133, 134; 133, 149; 133, 285; 134, boolean_operator:or; 134, 135; 134, 136; 135, identifier:labels; 136, parenthesized_expression; 136, 137; 137, boolean_operator:and; 137, 138; 137, 141; 138, comparison_operator:is; 138, 139; 138, 140; 139, identifier:labels; 140, None; 141, comparison_operator:<=; 141, 142; 141, 148; 142, call; 142, 143; 142, 144; 143, identifier:len; 144, argument_list; 144, 145; 145, attribute; 145, 146; 145, 147; 146, identifier:df; 147, identifier:columns; 148, integer:50; 149, block; 149, 150; 149, 170; 149, 171; 149, 179; 149, 186; 150, expression_statement; 150, 151; 151, call; 151, 152; 151, 155; 152, attribute; 152, 153; 152, 154; 153, identifier:ax1; 154, identifier:set_xticklabels; 155, argument_list; 155, 156; 155, 161; 155, 164; 155, 167; 156, call; 156, 157; 156, 160; 157, attribute; 157, 158; 157, 159; 158, identifier:ax1; 159, identifier:get_xticklabels; 160, argument_list; 161, keyword_argument; 161, 162; 161, 163; 162, identifier:rotation; 163, integer:45; 164, keyword_argument; 164, 165; 164, 166; 165, identifier:ha; 166, string:'right'; 167, keyword_argument; 167, 168; 167, 169; 168, identifier:fontsize; 169, identifier:fontsize; 170, comment; 171, expression_statement; 171, 172; 172, assignment; 172, 173; 172, 174; 173, identifier:ax2; 174, call; 174, 175; 174, 178; 175, attribute; 175, 176; 175, 177; 176, identifier:ax1; 177, identifier:twinx; 178, argument_list; 179, expression_statement; 179, 180; 180, call; 180, 181; 180, 184; 181, attribute; 181, 182; 181, 183; 182, identifier:axes; 183, identifier:append; 184, argument_list; 184, 185; 185, identifier:ax2; 186, if_statement; 186, 187; 186, 189; 186, 236; 187, not_operator; 187, 188; 188, identifier:log; 189, block; 189, 190; 189, 199; 189, 210; 190, expression_statement; 190, 191; 191, call; 191, 192; 191, 195; 192, attribute; 192, 193; 192, 194; 193, identifier:ax1; 194, identifier:set_ylim; 195, argument_list; 195, 196; 196, list:[0, 1]; 196, 197; 196, 198; 197, integer:0; 198, integer:1; 199, expression_statement; 199, 200; 200, call; 200, 201; 200, 204; 201, attribute; 201, 202; 201, 203; 202, identifier:ax2; 203, identifier:set_yticks; 204, argument_list; 204, 205; 205, call; 205, 206; 205, 209; 206, attribute; 206, 207; 206, 208; 207, identifier:ax1; 208, identifier:get_yticks; 209, argument_list; 210, expression_statement; 210, 211; 211, call; 211, 212; 211, 215; 212, attribute; 212, 213; 212, 214; 213, identifier:ax2; 214, identifier:set_yticklabels; 215, argument_list; 215, 216; 215, 233; 216, list_comprehension; 216, 217; 216, 226; 217, call; 217, 218; 217, 219; 218, identifier:int; 219, argument_list; 219, 220; 220, binary_operator:*; 220, 221; 220, 222; 221, identifier:n; 222, call; 222, 223; 222, 224; 223, identifier:len; 224, argument_list; 224, 225; 225, identifier:df; 226, for_in_clause; 226, 227; 226, 228; 227, identifier:n; 228, call; 228, 229; 228, 232; 229, attribute; 229, 230; 229, 231; 230, identifier:ax1; 231, identifier:get_yticks; 232, argument_list; 233, keyword_argument; 233, 234; 233, 235; 234, identifier:fontsize; 235, identifier:fontsize; 236, else_clause; 236, 237; 236, 238; 236, 239; 236, 240; 237, comment; 238, comment; 239, comment; 240, block; 240, 241; 240, 248; 240, 259; 241, expression_statement; 241, 242; 242, call; 242, 243; 242, 246; 243, attribute; 243, 244; 243, 245; 244, identifier:ax2; 245, identifier:set_yscale; 246, argument_list; 246, 247; 247, string:'log'; 248, expression_statement; 248, 249; 249, call; 249, 250; 249, 253; 250, attribute; 250, 251; 250, 252; 251, identifier:ax2; 252, identifier:set_ylim; 253, argument_list; 253, 254; 254, call; 254, 255; 254, 258; 255, attribute; 255, 256; 255, 257; 256, identifier:ax1; 257, identifier:get_ylim; 258, argument_list; 259, expression_statement; 259, 260; 260, call; 260, 261; 260, 264; 261, attribute; 261, 262; 261, 263; 262, identifier:ax2; 263, identifier:set_yticklabels; 264, argument_list; 264, 265; 264, 282; 265, list_comprehension; 265, 266; 265, 275; 266, call; 266, 267; 266, 268; 267, identifier:int; 268, argument_list; 268, 269; 269, binary_operator:*; 269, 270; 269, 271; 270, identifier:n; 271, call; 271, 272; 271, 273; 272, identifier:len; 273, argument_list; 273, 274; 274, identifier:df; 275, for_in_clause; 275, 276; 275, 277; 276, identifier:n; 277, call; 277, 278; 277, 281; 278, attribute; 278, 279; 278, 280; 279, identifier:ax1; 280, identifier:get_yticks; 281, argument_list; 282, keyword_argument; 282, 283; 282, 284; 283, identifier:fontsize; 284, identifier:fontsize; 285, else_clause; 285, 286; 286, block; 286, 287; 287, expression_statement; 287, 288; 288, call; 288, 289; 288, 292; 289, attribute; 289, 290; 289, 291; 290, identifier:ax1; 291, identifier:set_xticks; 292, argument_list; 292, 293; 293, list:[]; 294, comment; 295, expression_statement; 295, 296; 296, assignment; 296, 297; 296, 298; 297, identifier:ax3; 298, call; 298, 299; 298, 302; 299, attribute; 299, 300; 299, 301; 300, identifier:ax1; 301, identifier:twiny; 302, argument_list; 303, expression_statement; 303, 304; 304, call; 304, 305; 304, 308; 305, attribute; 305, 306; 305, 307; 306, identifier:axes; 307, identifier:append; 308, argument_list; 308, 309; 309, identifier:ax3; 310, expression_statement; 310, 311; 311, call; 311, 312; 311, 315; 312, attribute; 312, 313; 312, 314; 313, identifier:ax3; 314, identifier:set_xticks; 315, argument_list; 315, 316; 316, call; 316, 317; 316, 320; 317, attribute; 317, 318; 317, 319; 318, identifier:ax1; 319, identifier:get_xticks; 320, argument_list; 321, expression_statement; 321, 322; 322, call; 322, 323; 322, 326; 323, attribute; 323, 324; 323, 325; 324, identifier:ax3; 325, identifier:set_xlim; 326, argument_list; 326, 327; 327, call; 327, 328; 327, 331; 328, attribute; 328, 329; 328, 330; 329, identifier:ax1; 330, identifier:get_xlim; 331, argument_list; 332, expression_statement; 332, 333; 333, call; 333, 334; 333, 337; 334, attribute; 334, 335; 334, 336; 335, identifier:ax3; 336, identifier:set_xticklabels; 337, argument_list; 337, 338; 337, 341; 337, 344; 337, 347; 338, attribute; 338, 339; 338, 340; 339, identifier:nullity_counts; 340, identifier:values; 341, keyword_argument; 341, 342; 341, 343; 342, identifier:fontsize; 343, identifier:fontsize; 344, keyword_argument; 344, 345; 344, 346; 345, identifier:rotation; 346, integer:45; 347, keyword_argument; 347, 348; 347, 349; 348, identifier:ha; 349, string:'left'; 350, expression_statement; 350, 351; 351, call; 351, 352; 351, 355; 352, attribute; 352, 353; 352, 354; 353, identifier:ax3; 354, identifier:grid; 355, argument_list; 355, 356; 356, False; 357, for_statement; 357, 358; 357, 359; 357, 360; 358, identifier:ax; 359, identifier:axes; 360, block; 360, 361; 360, 372; 360, 383; 360, 394; 360, 405; 360, 414; 361, expression_statement; 361, 362; 362, call; 362, 363; 362, 370; 363, attribute; 363, 364; 363, 369; 364, subscript; 364, 365; 364, 368; 365, attribute; 365, 366; 365, 367; 366, identifier:ax; 367, identifier:spines; 368, string:'top'; 369, identifier:set_visible; 370, argument_list; 370, 371; 371, False; 372, expression_statement; 372, 373; 373, call; 373, 374; 373, 381; 374, attribute; 374, 375; 374, 380; 375, subscript; 375, 376; 375, 379; 376, attribute; 376, 377; 376, 378; 377, identifier:ax; 378, identifier:spines; 379, string:'right'; 380, identifier:set_visible; 381, argument_list; 381, 382; 382, False; 383, expression_statement; 383, 384; 384, call; 384, 385; 384, 392; 385, attribute; 385, 386; 385, 391; 386, subscript; 386, 387; 386, 390; 387, attribute; 387, 388; 387, 389; 388, identifier:ax; 389, identifier:spines; 390, string:'bottom'; 391, identifier:set_visible; 392, argument_list; 392, 393; 393, False; 394, expression_statement; 394, 395; 395, call; 395, 396; 395, 403; 396, attribute; 396, 397; 396, 402; 397, subscript; 397, 398; 397, 401; 398, attribute; 398, 399; 398, 400; 399, identifier:ax; 400, identifier:spines; 401, string:'left'; 402, identifier:set_visible; 403, argument_list; 403, 404; 404, False; 405, expression_statement; 405, 406; 406, call; 406, 407; 406, 412; 407, attribute; 407, 408; 407, 411; 408, attribute; 408, 409; 408, 410; 409, identifier:ax; 410, identifier:xaxis; 411, identifier:set_ticks_position; 412, argument_list; 412, 413; 413, string:'none'; 414, expression_statement; 414, 415; 415, call; 415, 416; 415, 421; 416, attribute; 416, 417; 416, 420; 417, attribute; 417, 418; 417, 419; 418, identifier:ax; 419, identifier:yaxis; 420, identifier:set_ticks_position; 421, argument_list; 421, 422; 422, string:'none'; 423, if_statement; 423, 424; 423, 425; 423, 432; 424, identifier:inline; 425, block; 425, 426; 426, expression_statement; 426, 427; 427, call; 427, 428; 427, 431; 428, attribute; 428, 429; 428, 430; 429, identifier:plt; 430, identifier:show; 431, argument_list; 432, else_clause; 432, 433; 433, block; 433, 434; 434, return_statement; 434, 435; 435, identifier:ax1
def bar(df, figsize=(24, 10), fontsize=16, labels=None, log=False, color='dimgray', inline=False, filter=None, n=0, p=0, sort=None): """ A bar chart visualization of the nullity of the given DataFrame. :param df: The input DataFrame. :param log: Whether or not to display a logorithmic plot. Defaults to False (linear). :param filter: The filter to apply to the heatmap. Should be one of "top", "bottom", or None (default). :param n: The cap on the number of columns to include in the filtered DataFrame. :param p: The cap on the percentage fill of the columns in the filtered DataFrame. :param sort: The sort to apply to the heatmap. Should be one of "ascending", "descending", or None (default). :param figsize: The size of the figure to display. :param fontsize: The figure's font size. This default to 16. :param labels: Whether or not to display the column names. Would need to be turned off on particularly large displays. Defaults to True. :param color: The color of the filled columns. Default to the RGB multiple `(0.25, 0.25, 0.25)`. :return: If `inline` is False, the underlying `matplotlib.figure` object. Else, nothing. """ nullity_counts = len(df) - df.isnull().sum() df = nullity_filter(df, filter=filter, n=n, p=p) df = nullity_sort(df, sort=sort) plt.figure(figsize=figsize) (nullity_counts / len(df)).plot(kind='bar', figsize=figsize, fontsize=fontsize, log=log, color=color) ax1 = plt.gca() axes = [ax1] # Start appending elements, starting with a modified bottom x axis. if labels or (labels is None and len(df.columns) <= 50): ax1.set_xticklabels(ax1.get_xticklabels(), rotation=45, ha='right', fontsize=fontsize) # Create the numerical ticks. ax2 = ax1.twinx() axes.append(ax2) if not log: ax1.set_ylim([0, 1]) ax2.set_yticks(ax1.get_yticks()) ax2.set_yticklabels([int(n*len(df)) for n in ax1.get_yticks()], fontsize=fontsize) else: # For some reason when a logarithmic plot is specified `ax1` always contains two more ticks than actually # appears in the plot. The fix is to ignore the first and last entries. Also note that when a log scale # is used, we have to make it match the `ax1` layout ourselves. ax2.set_yscale('log') ax2.set_ylim(ax1.get_ylim()) ax2.set_yticklabels([int(n*len(df)) for n in ax1.get_yticks()], fontsize=fontsize) else: ax1.set_xticks([]) # Create the third axis, which displays columnar totals above the rest of the plot. ax3 = ax1.twiny() axes.append(ax3) ax3.set_xticks(ax1.get_xticks()) ax3.set_xlim(ax1.get_xlim()) ax3.set_xticklabels(nullity_counts.values, fontsize=fontsize, rotation=45, ha='left') ax3.grid(False) for ax in axes: ax.spines['top'].set_visible(False) ax.spines['right'].set_visible(False) ax.spines['bottom'].set_visible(False) ax.spines['left'].set_visible(False) ax.xaxis.set_ticks_position('none') ax.yaxis.set_ticks_position('none') if inline: plt.show() else: return ax1
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 44; 2, function_name:heatmap; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 14; 3, 17; 3, 20; 3, 25; 3, 28; 3, 31; 3, 34; 3, 38; 3, 41; 4, identifier:df; 5, default_parameter; 5, 6; 5, 7; 6, identifier:inline; 7, False; 8, default_parameter; 8, 9; 8, 10; 9, identifier:filter; 10, None; 11, default_parameter; 11, 12; 11, 13; 12, identifier:n; 13, integer:0; 14, default_parameter; 14, 15; 14, 16; 15, identifier:p; 16, integer:0; 17, default_parameter; 17, 18; 17, 19; 18, identifier:sort; 19, None; 20, default_parameter; 20, 21; 20, 22; 21, identifier:figsize; 22, tuple; 22, 23; 22, 24; 23, integer:20; 24, integer:12; 25, default_parameter; 25, 26; 25, 27; 26, identifier:fontsize; 27, integer:16; 28, default_parameter; 28, 29; 28, 30; 29, identifier:labels; 30, True; 31, default_parameter; 31, 32; 31, 33; 32, identifier:cmap; 33, string:'RdBu'; 34, default_parameter; 34, 35; 34, 36; 35, identifier:vmin; 36, unary_operator:-; 36, 37; 37, integer:1; 38, default_parameter; 38, 39; 38, 40; 39, identifier:vmax; 40, integer:1; 41, default_parameter; 41, 42; 41, 43; 42, identifier:cbar; 43, True; 44, block; 44, 45; 44, 47; 44, 48; 44, 64; 44, 74; 44, 83; 44, 93; 44, 104; 44, 105; 44, 140; 44, 141; 44, 153; 44, 162; 44, 173; 44, 239; 44, 240; 44, 248; 44, 270; 44, 289; 44, 308; 44, 317; 44, 326; 44, 335; 44, 432; 45, expression_statement; 45, 46; 46, comment; 47, comment; 48, expression_statement; 48, 49; 49, assignment; 49, 50; 49, 51; 50, identifier:df; 51, call; 51, 52; 51, 53; 52, identifier:nullity_filter; 53, argument_list; 53, 54; 53, 55; 53, 58; 53, 61; 54, identifier:df; 55, keyword_argument; 55, 56; 55, 57; 56, identifier:filter; 57, identifier:filter; 58, keyword_argument; 58, 59; 58, 60; 59, identifier:n; 60, identifier:n; 61, keyword_argument; 61, 62; 61, 63; 62, identifier:p; 63, identifier:p; 64, expression_statement; 64, 65; 65, assignment; 65, 66; 65, 67; 66, identifier:df; 67, call; 67, 68; 67, 69; 68, identifier:nullity_sort; 69, argument_list; 69, 70; 69, 71; 70, identifier:df; 71, keyword_argument; 71, 72; 71, 73; 72, identifier:sort; 73, identifier:sort; 74, expression_statement; 74, 75; 75, call; 75, 76; 75, 79; 76, attribute; 76, 77; 76, 78; 77, identifier:plt; 78, identifier:figure; 79, argument_list; 79, 80; 80, keyword_argument; 80, 81; 80, 82; 81, identifier:figsize; 82, identifier:figsize; 83, expression_statement; 83, 84; 84, assignment; 84, 85; 84, 86; 85, identifier:gs; 86, call; 86, 87; 86, 90; 87, attribute; 87, 88; 87, 89; 88, identifier:gridspec; 89, identifier:GridSpec; 90, argument_list; 90, 91; 90, 92; 91, integer:1; 92, integer:1; 93, expression_statement; 93, 94; 94, assignment; 94, 95; 94, 96; 95, identifier:ax0; 96, call; 96, 97; 96, 100; 97, attribute; 97, 98; 97, 99; 98, identifier:plt; 99, identifier:subplot; 100, argument_list; 100, 101; 101, subscript; 101, 102; 101, 103; 102, identifier:gs; 103, integer:0; 104, comment; 105, expression_statement; 105, 106; 106, assignment; 106, 107; 106, 108; 107, identifier:df; 108, subscript; 108, 109; 108, 112; 108, 114; 109, attribute; 109, 110; 109, 111; 110, identifier:df; 111, identifier:iloc; 112, slice; 112, 113; 113, colon; 114, list_comprehension; 114, 115; 114, 116; 114, 136; 115, identifier:i; 116, for_in_clause; 116, 117; 116, 120; 117, pattern_list; 117, 118; 117, 119; 118, identifier:i; 119, identifier:n; 120, call; 120, 121; 120, 122; 121, identifier:enumerate; 122, argument_list; 122, 123; 123, call; 123, 124; 123, 127; 124, attribute; 124, 125; 124, 126; 125, identifier:np; 126, identifier:var; 127, argument_list; 127, 128; 127, 133; 128, call; 128, 129; 128, 132; 129, attribute; 129, 130; 129, 131; 130, identifier:df; 131, identifier:isnull; 132, argument_list; 133, keyword_argument; 133, 134; 133, 135; 134, identifier:axis; 135, string:'rows'; 136, if_clause; 136, 137; 137, comparison_operator:>; 137, 138; 137, 139; 138, identifier:n; 139, integer:0; 140, comment; 141, expression_statement; 141, 142; 142, assignment; 142, 143; 142, 144; 143, identifier:corr_mat; 144, call; 144, 145; 144, 152; 145, attribute; 145, 146; 145, 151; 146, call; 146, 147; 146, 150; 147, attribute; 147, 148; 147, 149; 148, identifier:df; 149, identifier:isnull; 150, argument_list; 151, identifier:corr; 152, argument_list; 153, expression_statement; 153, 154; 154, assignment; 154, 155; 154, 156; 155, identifier:mask; 156, call; 156, 157; 156, 160; 157, attribute; 157, 158; 157, 159; 158, identifier:np; 159, identifier:zeros_like; 160, argument_list; 160, 161; 161, identifier:corr_mat; 162, expression_statement; 162, 163; 163, assignment; 163, 164; 163, 172; 164, subscript; 164, 165; 164, 166; 165, identifier:mask; 166, call; 166, 167; 166, 170; 167, attribute; 167, 168; 167, 169; 168, identifier:np; 169, identifier:triu_indices_from; 170, argument_list; 170, 171; 171, identifier:mask; 172, True; 173, if_statement; 173, 174; 173, 175; 173, 212; 174, identifier:labels; 175, block; 175, 176; 176, expression_statement; 176, 177; 177, call; 177, 178; 177, 181; 178, attribute; 178, 179; 178, 180; 179, identifier:sns; 180, identifier:heatmap; 181, argument_list; 181, 182; 181, 183; 181, 186; 181, 189; 181, 192; 181, 195; 181, 198; 181, 206; 181, 209; 182, identifier:corr_mat; 183, keyword_argument; 183, 184; 183, 185; 184, identifier:mask; 185, identifier:mask; 186, keyword_argument; 186, 187; 186, 188; 187, identifier:cmap; 188, identifier:cmap; 189, keyword_argument; 189, 190; 189, 191; 190, identifier:ax; 191, identifier:ax0; 192, keyword_argument; 192, 193; 192, 194; 193, identifier:cbar; 194, identifier:cbar; 195, keyword_argument; 195, 196; 195, 197; 196, identifier:annot; 197, True; 198, keyword_argument; 198, 199; 198, 200; 199, identifier:annot_kws; 200, dictionary; 200, 201; 201, pair; 201, 202; 201, 203; 202, string:'size'; 203, binary_operator:-; 203, 204; 203, 205; 204, identifier:fontsize; 205, integer:2; 206, keyword_argument; 206, 207; 206, 208; 207, identifier:vmin; 208, identifier:vmin; 209, keyword_argument; 209, 210; 209, 211; 210, identifier:vmax; 211, identifier:vmax; 212, else_clause; 212, 213; 213, block; 213, 214; 214, expression_statement; 214, 215; 215, call; 215, 216; 215, 219; 216, attribute; 216, 217; 216, 218; 217, identifier:sns; 218, identifier:heatmap; 219, argument_list; 219, 220; 219, 221; 219, 224; 219, 227; 219, 230; 219, 233; 219, 236; 220, identifier:corr_mat; 221, keyword_argument; 221, 222; 221, 223; 222, identifier:mask; 223, identifier:mask; 224, keyword_argument; 224, 225; 224, 226; 225, identifier:cmap; 226, identifier:cmap; 227, keyword_argument; 227, 228; 227, 229; 228, identifier:ax; 229, identifier:ax0; 230, keyword_argument; 230, 231; 230, 232; 231, identifier:cbar; 232, identifier:cbar; 233, keyword_argument; 233, 234; 233, 235; 234, identifier:vmin; 235, identifier:vmin; 236, keyword_argument; 236, 237; 236, 238; 237, identifier:vmax; 238, identifier:vmax; 239, comment; 240, expression_statement; 240, 241; 241, call; 241, 242; 241, 247; 242, attribute; 242, 243; 242, 246; 243, attribute; 243, 244; 243, 245; 244, identifier:ax0; 245, identifier:xaxis; 246, identifier:tick_bottom; 247, argument_list; 248, expression_statement; 248, 249; 249, call; 249, 250; 249, 253; 250, attribute; 250, 251; 250, 252; 251, identifier:ax0; 252, identifier:set_xticklabels; 253, argument_list; 253, 254; 253, 261; 253, 264; 253, 267; 254, call; 254, 255; 254, 260; 255, attribute; 255, 256; 255, 259; 256, attribute; 256, 257; 256, 258; 257, identifier:ax0; 258, identifier:xaxis; 259, identifier:get_majorticklabels; 260, argument_list; 261, keyword_argument; 261, 262; 261, 263; 262, identifier:rotation; 263, integer:45; 264, keyword_argument; 264, 265; 264, 266; 265, identifier:ha; 266, string:'right'; 267, keyword_argument; 267, 268; 267, 269; 268, identifier:fontsize; 269, identifier:fontsize; 270, expression_statement; 270, 271; 271, call; 271, 272; 271, 275; 272, attribute; 272, 273; 272, 274; 273, identifier:ax0; 274, identifier:set_yticklabels; 275, argument_list; 275, 276; 275, 283; 275, 286; 276, call; 276, 277; 276, 282; 277, attribute; 277, 278; 277, 281; 278, attribute; 278, 279; 278, 280; 279, identifier:ax0; 280, identifier:yaxis; 281, identifier:get_majorticklabels; 282, argument_list; 283, keyword_argument; 283, 284; 283, 285; 284, identifier:fontsize; 285, identifier:fontsize; 286, keyword_argument; 286, 287; 286, 288; 287, identifier:rotation; 288, integer:0; 289, expression_statement; 289, 290; 290, call; 290, 291; 290, 294; 291, attribute; 291, 292; 291, 293; 292, identifier:ax0; 293, identifier:set_yticklabels; 294, argument_list; 294, 295; 294, 302; 294, 305; 295, call; 295, 296; 295, 301; 296, attribute; 296, 297; 296, 300; 297, attribute; 297, 298; 297, 299; 298, identifier:ax0; 299, identifier:yaxis; 300, identifier:get_majorticklabels; 301, argument_list; 302, keyword_argument; 302, 303; 302, 304; 303, identifier:rotation; 304, integer:0; 305, keyword_argument; 305, 306; 305, 307; 306, identifier:fontsize; 307, identifier:fontsize; 308, expression_statement; 308, 309; 309, call; 309, 310; 309, 315; 310, attribute; 310, 311; 310, 314; 311, attribute; 311, 312; 311, 313; 312, identifier:ax0; 313, identifier:xaxis; 314, identifier:set_ticks_position; 315, argument_list; 315, 316; 316, string:'none'; 317, expression_statement; 317, 318; 318, call; 318, 319; 318, 324; 319, attribute; 319, 320; 319, 323; 320, attribute; 320, 321; 320, 322; 321, identifier:ax0; 322, identifier:yaxis; 323, identifier:set_ticks_position; 324, argument_list; 324, 325; 325, string:'none'; 326, expression_statement; 326, 327; 327, call; 327, 328; 327, 333; 328, attribute; 328, 329; 328, 332; 329, attribute; 329, 330; 329, 331; 330, identifier:ax0; 331, identifier:patch; 332, identifier:set_visible; 333, argument_list; 333, 334; 334, False; 335, for_statement; 335, 336; 335, 337; 335, 340; 336, identifier:text; 337, attribute; 337, 338; 337, 339; 338, identifier:ax0; 339, identifier:texts; 340, block; 340, 341; 340, 352; 341, expression_statement; 341, 342; 342, assignment; 342, 343; 342, 344; 343, identifier:t; 344, call; 344, 345; 344, 346; 345, identifier:float; 346, argument_list; 346, 347; 347, call; 347, 348; 347, 351; 348, attribute; 348, 349; 348, 350; 349, identifier:text; 350, identifier:get_text; 351, argument_list; 352, if_statement; 352, 353; 352, 357; 352, 365; 352, 380; 352, 392; 352, 405; 352, 419; 353, comparison_operator:<=; 353, 354; 353, 355; 353, 356; 354, float:0.95; 355, identifier:t; 356, integer:1; 357, block; 357, 358; 358, expression_statement; 358, 359; 359, call; 359, 360; 359, 363; 360, attribute; 360, 361; 360, 362; 361, identifier:text; 362, identifier:set_text; 363, argument_list; 363, 364; 364, string:'<1'; 365, elif_clause; 365, 366; 365, 372; 366, comparison_operator:<; 366, 367; 366, 369; 366, 370; 367, unary_operator:-; 367, 368; 368, integer:1; 369, identifier:t; 370, unary_operator:-; 370, 371; 371, float:0.95; 372, block; 372, 373; 373, expression_statement; 373, 374; 374, call; 374, 375; 374, 378; 375, attribute; 375, 376; 375, 377; 376, identifier:text; 377, identifier:set_text; 378, argument_list; 378, 379; 379, string:'>-1'; 380, elif_clause; 380, 381; 380, 384; 381, comparison_operator:==; 381, 382; 381, 383; 382, identifier:t; 383, integer:1; 384, block; 384, 385; 385, expression_statement; 385, 386; 386, call; 386, 387; 386, 390; 387, attribute; 387, 388; 387, 389; 388, identifier:text; 389, identifier:set_text; 390, argument_list; 390, 391; 391, string:'1'; 392, elif_clause; 392, 393; 392, 397; 393, comparison_operator:==; 393, 394; 393, 395; 394, identifier:t; 395, unary_operator:-; 395, 396; 396, integer:1; 397, block; 397, 398; 398, expression_statement; 398, 399; 399, call; 399, 400; 399, 403; 400, attribute; 400, 401; 400, 402; 401, identifier:text; 402, identifier:set_text; 403, argument_list; 403, 404; 404, string:'-1'; 405, elif_clause; 405, 406; 405, 411; 406, comparison_operator:<; 406, 407; 406, 409; 406, 410; 407, unary_operator:-; 407, 408; 408, float:0.05; 409, identifier:t; 410, float:0.05; 411, block; 411, 412; 412, expression_statement; 412, 413; 413, call; 413, 414; 413, 417; 414, attribute; 414, 415; 414, 416; 415, identifier:text; 416, identifier:set_text; 417, argument_list; 417, 418; 418, string:''; 419, else_clause; 419, 420; 420, block; 420, 421; 421, expression_statement; 421, 422; 422, call; 422, 423; 422, 426; 423, attribute; 423, 424; 423, 425; 424, identifier:text; 425, identifier:set_text; 426, argument_list; 426, 427; 427, call; 427, 428; 427, 429; 428, identifier:round; 429, argument_list; 429, 430; 429, 431; 430, identifier:t; 431, integer:1; 432, if_statement; 432, 433; 432, 434; 432, 441; 433, identifier:inline; 434, block; 434, 435; 435, expression_statement; 435, 436; 436, call; 436, 437; 436, 440; 437, attribute; 437, 438; 437, 439; 438, identifier:plt; 439, identifier:show; 440, argument_list; 441, else_clause; 441, 442; 442, block; 442, 443; 443, return_statement; 443, 444; 444, identifier:ax0
def heatmap(df, inline=False, filter=None, n=0, p=0, sort=None, figsize=(20, 12), fontsize=16, labels=True, cmap='RdBu', vmin=-1, vmax=1, cbar=True ): """ Presents a `seaborn` heatmap visualization of nullity correlation in the given DataFrame. Note that this visualization has no special support for large datasets. For those, try the dendrogram instead. :param df: The DataFrame whose completeness is being heatmapped. :param filter: The filter to apply to the heatmap. Should be one of "top", "bottom", or None (default). See `nullity_filter()` for more information. :param n: The cap on the number of columns to include in the filtered DataFrame. See `nullity_filter()` for more information. :param p: The cap on the percentage fill of the columns in the filtered DataFrame. See `nullity_filter()` for more information. :param sort: The sort to apply to the heatmap. Should be one of "ascending", "descending", or None. See `nullity_sort()` for more information. :param figsize: The size of the figure to display. This is a `matplotlib` parameter which defaults to (20, 12). :param fontsize: The figure's font size. :param labels: Whether or not to label each matrix entry with its correlation (default is True). :param cmap: What `matplotlib` colormap to use. Defaults to `RdBu`. :param vmin: The normalized colormap threshold. Defaults to -1, e.g. the bottom of the color scale. :param vmax: The normalized colormap threshold. Defaults to 1, e.g. the bottom of the color scale. :param inline: Whether or not the figure is inline. If it's not then instead of getting plotted, this method will return its figure. :return: If `inline` is False, the underlying `matplotlib.figure` object. Else, nothing. """ # Apply filters and sorts, set up the figure. df = nullity_filter(df, filter=filter, n=n, p=p) df = nullity_sort(df, sort=sort) plt.figure(figsize=figsize) gs = gridspec.GridSpec(1, 1) ax0 = plt.subplot(gs[0]) # Remove completely filled or completely empty variables. df = df.iloc[:,[i for i, n in enumerate(np.var(df.isnull(), axis='rows')) if n > 0]] # Create and mask the correlation matrix. Construct the base heatmap. corr_mat = df.isnull().corr() mask = np.zeros_like(corr_mat) mask[np.triu_indices_from(mask)] = True if labels: sns.heatmap(corr_mat, mask=mask, cmap=cmap, ax=ax0, cbar=cbar, annot=True, annot_kws={'size': fontsize - 2}, vmin=vmin, vmax=vmax) else: sns.heatmap(corr_mat, mask=mask, cmap=cmap, ax=ax0, cbar=cbar, vmin=vmin, vmax=vmax) # Apply visual corrections and modifications. ax0.xaxis.tick_bottom() ax0.set_xticklabels(ax0.xaxis.get_majorticklabels(), rotation=45, ha='right', fontsize=fontsize) ax0.set_yticklabels(ax0.yaxis.get_majorticklabels(), fontsize=fontsize, rotation=0) ax0.set_yticklabels(ax0.yaxis.get_majorticklabels(), rotation=0, fontsize=fontsize) ax0.xaxis.set_ticks_position('none') ax0.yaxis.set_ticks_position('none') ax0.patch.set_visible(False) for text in ax0.texts: t = float(text.get_text()) if 0.95 <= t < 1: text.set_text('<1') elif -1 < t <= -0.95: text.set_text('>-1') elif t == 1: text.set_text('1') elif t == -1: text.set_text('-1') elif -0.05 < t < 0.05: text.set_text('') else: text.set_text(round(t, 1)) if inline: plt.show() else: return ax0
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 32; 2, function_name:dendrogram; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 14; 3, 17; 3, 20; 3, 23; 3, 26; 3, 29; 4, identifier:df; 5, default_parameter; 5, 6; 5, 7; 6, identifier:method; 7, string:'average'; 8, default_parameter; 8, 9; 8, 10; 9, identifier:filter; 10, None; 11, default_parameter; 11, 12; 11, 13; 12, identifier:n; 13, integer:0; 14, default_parameter; 14, 15; 14, 16; 15, identifier:p; 16, integer:0; 17, default_parameter; 17, 18; 17, 19; 18, identifier:sort; 19, None; 20, default_parameter; 20, 21; 20, 22; 21, identifier:orientation; 22, None; 23, default_parameter; 23, 24; 23, 25; 24, identifier:figsize; 25, None; 26, default_parameter; 26, 27; 26, 28; 27, identifier:fontsize; 28, integer:16; 29, default_parameter; 29, 30; 29, 31; 30, identifier:inline; 31, False; 32, block; 32, 33; 32, 35; 32, 83; 32, 92; 32, 102; 32, 113; 32, 129; 32, 139; 32, 140; 32, 160; 32, 170; 32, 194; 32, 228; 32, 229; 32, 236; 32, 245; 32, 258; 32, 267; 32, 276; 32, 287; 32, 298; 32, 309; 32, 320; 32, 329; 32, 330; 32, 378; 32, 427; 33, expression_statement; 33, 34; 34, comment; 35, if_statement; 35, 36; 35, 38; 36, not_operator; 36, 37; 37, identifier:figsize; 38, block; 38, 39; 39, if_statement; 39, 40; 39, 56; 39, 63; 40, boolean_operator:or; 40, 41; 40, 53; 41, boolean_operator:or; 41, 42; 41, 50; 42, comparison_operator:<=; 42, 43; 42, 49; 43, call; 43, 44; 43, 45; 44, identifier:len; 45, argument_list; 45, 46; 46, attribute; 46, 47; 46, 48; 47, identifier:df; 48, identifier:columns; 49, integer:50; 50, comparison_operator:==; 50, 51; 50, 52; 51, identifier:orientation; 52, string:'top'; 53, comparison_operator:==; 53, 54; 53, 55; 54, identifier:orientation; 55, string:'bottom'; 56, block; 56, 57; 57, expression_statement; 57, 58; 58, assignment; 58, 59; 58, 60; 59, identifier:figsize; 60, tuple; 60, 61; 60, 62; 61, integer:25; 62, integer:10; 63, else_clause; 63, 64; 64, block; 64, 65; 65, expression_statement; 65, 66; 66, assignment; 66, 67; 66, 68; 67, identifier:figsize; 68, tuple; 68, 69; 68, 70; 69, integer:25; 70, binary_operator:*; 70, 71; 70, 82; 71, parenthesized_expression; 71, 72; 72, binary_operator:-; 72, 73; 72, 81; 73, binary_operator:+; 73, 74; 73, 75; 74, integer:25; 75, call; 75, 76; 75, 77; 76, identifier:len; 77, argument_list; 77, 78; 78, attribute; 78, 79; 78, 80; 79, identifier:df; 80, identifier:columns; 81, integer:50; 82, float:0.5; 83, expression_statement; 83, 84; 84, call; 84, 85; 84, 88; 85, attribute; 85, 86; 85, 87; 86, identifier:plt; 87, identifier:figure; 88, argument_list; 88, 89; 89, keyword_argument; 89, 90; 89, 91; 90, identifier:figsize; 91, identifier:figsize; 92, expression_statement; 92, 93; 93, assignment; 93, 94; 93, 95; 94, identifier:gs; 95, call; 95, 96; 95, 99; 96, attribute; 96, 97; 96, 98; 97, identifier:gridspec; 98, identifier:GridSpec; 99, argument_list; 99, 100; 99, 101; 100, integer:1; 101, integer:1; 102, expression_statement; 102, 103; 103, assignment; 103, 104; 103, 105; 104, identifier:ax0; 105, call; 105, 106; 105, 109; 106, attribute; 106, 107; 106, 108; 107, identifier:plt; 108, identifier:subplot; 109, argument_list; 109, 110; 110, subscript; 110, 111; 110, 112; 111, identifier:gs; 112, integer:0; 113, expression_statement; 113, 114; 114, assignment; 114, 115; 114, 116; 115, identifier:df; 116, call; 116, 117; 116, 118; 117, identifier:nullity_filter; 118, argument_list; 118, 119; 118, 120; 118, 123; 118, 126; 119, identifier:df; 120, keyword_argument; 120, 121; 120, 122; 121, identifier:filter; 122, identifier:filter; 123, keyword_argument; 123, 124; 123, 125; 124, identifier:n; 125, identifier:n; 126, keyword_argument; 126, 127; 126, 128; 127, identifier:p; 128, identifier:p; 129, expression_statement; 129, 130; 130, assignment; 130, 131; 130, 132; 131, identifier:df; 132, call; 132, 133; 132, 134; 133, identifier:nullity_sort; 134, argument_list; 134, 135; 134, 136; 135, identifier:df; 136, keyword_argument; 136, 137; 136, 138; 137, identifier:sort; 138, identifier:sort; 139, comment; 140, expression_statement; 140, 141; 141, assignment; 141, 142; 141, 143; 142, identifier:x; 143, call; 143, 144; 143, 147; 144, attribute; 144, 145; 144, 146; 145, identifier:np; 146, identifier:transpose; 147, argument_list; 147, 148; 148, attribute; 148, 149; 148, 159; 149, call; 149, 150; 149, 157; 150, attribute; 150, 151; 150, 156; 151, call; 151, 152; 151, 155; 152, attribute; 152, 153; 152, 154; 153, identifier:df; 154, identifier:isnull; 155, argument_list; 156, identifier:astype; 157, argument_list; 157, 158; 158, identifier:int; 159, identifier:values; 160, expression_statement; 160, 161; 161, assignment; 161, 162; 161, 163; 162, identifier:z; 163, call; 163, 164; 163, 167; 164, attribute; 164, 165; 164, 166; 165, identifier:hierarchy; 166, identifier:linkage; 167, argument_list; 167, 168; 167, 169; 168, identifier:x; 169, identifier:method; 170, if_statement; 170, 171; 170, 173; 171, not_operator; 171, 172; 172, identifier:orientation; 173, block; 173, 174; 174, if_statement; 174, 175; 174, 183; 174, 188; 175, comparison_operator:>; 175, 176; 175, 182; 176, call; 176, 177; 176, 178; 177, identifier:len; 178, argument_list; 178, 179; 179, attribute; 179, 180; 179, 181; 180, identifier:df; 181, identifier:columns; 182, integer:50; 183, block; 183, 184; 184, expression_statement; 184, 185; 185, assignment; 185, 186; 185, 187; 186, identifier:orientation; 187, string:'left'; 188, else_clause; 188, 189; 189, block; 189, 190; 190, expression_statement; 190, 191; 191, assignment; 191, 192; 191, 193; 192, identifier:orientation; 193, string:'bottom'; 194, expression_statement; 194, 195; 195, call; 195, 196; 195, 199; 196, attribute; 196, 197; 196, 198; 197, identifier:hierarchy; 198, identifier:dendrogram; 199, argument_list; 199, 200; 199, 201; 199, 204; 199, 213; 199, 216; 199, 222; 199, 225; 200, identifier:z; 201, keyword_argument; 201, 202; 201, 203; 202, identifier:orientation; 203, identifier:orientation; 204, keyword_argument; 204, 205; 204, 206; 205, identifier:labels; 206, call; 206, 207; 206, 212; 207, attribute; 207, 208; 207, 211; 208, attribute; 208, 209; 208, 210; 209, identifier:df; 210, identifier:columns; 211, identifier:tolist; 212, argument_list; 213, keyword_argument; 213, 214; 213, 215; 214, identifier:distance_sort; 215, string:'descending'; 216, keyword_argument; 216, 217; 216, 218; 217, identifier:link_color_func; 218, lambda; 218, 219; 218, 221; 219, lambda_parameters; 219, 220; 220, identifier:c; 221, string:'black'; 222, keyword_argument; 222, 223; 222, 224; 223, identifier:leaf_font_size; 224, identifier:fontsize; 225, keyword_argument; 225, 226; 225, 227; 226, identifier:ax; 227, identifier:ax0; 228, comment; 229, expression_statement; 229, 230; 230, call; 230, 231; 230, 234; 231, attribute; 231, 232; 231, 233; 232, identifier:ax0; 233, identifier:set_aspect; 234, argument_list; 234, 235; 235, string:'auto'; 236, expression_statement; 236, 237; 237, call; 237, 238; 237, 241; 238, attribute; 238, 239; 238, 240; 239, identifier:ax0; 240, identifier:grid; 241, argument_list; 241, 242; 242, keyword_argument; 242, 243; 242, 244; 243, identifier:b; 244, False; 245, if_statement; 245, 246; 245, 249; 246, comparison_operator:==; 246, 247; 246, 248; 247, identifier:orientation; 248, string:'bottom'; 249, block; 249, 250; 250, expression_statement; 250, 251; 251, call; 251, 252; 251, 257; 252, attribute; 252, 253; 252, 256; 253, attribute; 253, 254; 253, 255; 254, identifier:ax0; 255, identifier:xaxis; 256, identifier:tick_top; 257, argument_list; 258, expression_statement; 258, 259; 259, call; 259, 260; 259, 265; 260, attribute; 260, 261; 260, 264; 261, attribute; 261, 262; 261, 263; 262, identifier:ax0; 263, identifier:xaxis; 264, identifier:set_ticks_position; 265, argument_list; 265, 266; 266, string:'none'; 267, expression_statement; 267, 268; 268, call; 268, 269; 268, 274; 269, attribute; 269, 270; 269, 273; 270, attribute; 270, 271; 270, 272; 271, identifier:ax0; 272, identifier:yaxis; 273, identifier:set_ticks_position; 274, argument_list; 274, 275; 275, string:'none'; 276, expression_statement; 276, 277; 277, call; 277, 278; 277, 285; 278, attribute; 278, 279; 278, 284; 279, subscript; 279, 280; 279, 283; 280, attribute; 280, 281; 280, 282; 281, identifier:ax0; 282, identifier:spines; 283, string:'top'; 284, identifier:set_visible; 285, argument_list; 285, 286; 286, False; 287, expression_statement; 287, 288; 288, call; 288, 289; 288, 296; 289, attribute; 289, 290; 289, 295; 290, subscript; 290, 291; 290, 294; 291, attribute; 291, 292; 291, 293; 292, identifier:ax0; 293, identifier:spines; 294, string:'right'; 295, identifier:set_visible; 296, argument_list; 296, 297; 297, False; 298, expression_statement; 298, 299; 299, call; 299, 300; 299, 307; 300, attribute; 300, 301; 300, 306; 301, subscript; 301, 302; 301, 305; 302, attribute; 302, 303; 302, 304; 303, identifier:ax0; 304, identifier:spines; 305, string:'bottom'; 306, identifier:set_visible; 307, argument_list; 307, 308; 308, False; 309, expression_statement; 309, 310; 310, call; 310, 311; 310, 318; 311, attribute; 311, 312; 311, 317; 312, subscript; 312, 313; 312, 316; 313, attribute; 313, 314; 313, 315; 314, identifier:ax0; 315, identifier:spines; 316, string:'left'; 317, identifier:set_visible; 318, argument_list; 318, 319; 319, False; 320, expression_statement; 320, 321; 321, call; 321, 322; 321, 327; 322, attribute; 322, 323; 322, 326; 323, attribute; 323, 324; 323, 325; 324, identifier:ax0; 325, identifier:patch; 326, identifier:set_visible; 327, argument_list; 327, 328; 328, False; 329, comment; 330, if_statement; 330, 331; 330, 334; 330, 354; 331, comparison_operator:==; 331, 332; 331, 333; 332, identifier:orientation; 333, string:'bottom'; 334, block; 334, 335; 335, expression_statement; 335, 336; 336, call; 336, 337; 336, 340; 337, attribute; 337, 338; 337, 339; 338, identifier:ax0; 339, identifier:set_xticklabels; 340, argument_list; 340, 341; 340, 348; 340, 351; 341, call; 341, 342; 341, 347; 342, attribute; 342, 343; 342, 346; 343, attribute; 343, 344; 343, 345; 344, identifier:ax0; 345, identifier:xaxis; 346, identifier:get_majorticklabels; 347, argument_list; 348, keyword_argument; 348, 349; 348, 350; 349, identifier:rotation; 350, integer:45; 351, keyword_argument; 351, 352; 351, 353; 352, identifier:ha; 353, string:'left'; 354, elif_clause; 354, 355; 354, 358; 355, comparison_operator:==; 355, 356; 355, 357; 356, identifier:orientation; 357, string:'top'; 358, block; 358, 359; 359, expression_statement; 359, 360; 360, call; 360, 361; 360, 364; 361, attribute; 361, 362; 361, 363; 362, identifier:ax0; 363, identifier:set_xticklabels; 364, argument_list; 364, 365; 364, 372; 364, 375; 365, call; 365, 366; 365, 371; 366, attribute; 366, 367; 366, 370; 367, attribute; 367, 368; 367, 369; 368, identifier:ax0; 369, identifier:xaxis; 370, identifier:get_majorticklabels; 371, argument_list; 372, keyword_argument; 372, 373; 372, 374; 373, identifier:rotation; 374, integer:45; 375, keyword_argument; 375, 376; 375, 377; 376, identifier:ha; 377, string:'right'; 378, if_statement; 378, 379; 378, 386; 378, 406; 379, boolean_operator:or; 379, 380; 379, 383; 380, comparison_operator:==; 380, 381; 380, 382; 381, identifier:orientation; 382, string:'bottom'; 383, comparison_operator:==; 383, 384; 383, 385; 384, identifier:orientation; 385, string:'top'; 386, block; 386, 387; 387, expression_statement; 387, 388; 388, call; 388, 389; 388, 392; 389, attribute; 389, 390; 389, 391; 390, identifier:ax0; 391, identifier:tick_params; 392, argument_list; 392, 393; 392, 396; 393, keyword_argument; 393, 394; 393, 395; 394, identifier:axis; 395, string:'y'; 396, keyword_argument; 396, 397; 396, 398; 397, identifier:labelsize; 398, call; 398, 399; 398, 400; 399, identifier:int; 400, argument_list; 400, 401; 401, binary_operator:*; 401, 402; 401, 405; 402, binary_operator:/; 402, 403; 402, 404; 403, identifier:fontsize; 404, integer:16; 405, integer:20; 406, else_clause; 406, 407; 407, block; 407, 408; 408, expression_statement; 408, 409; 409, call; 409, 410; 409, 413; 410, attribute; 410, 411; 410, 412; 411, identifier:ax0; 412, identifier:tick_params; 413, argument_list; 413, 414; 413, 417; 414, keyword_argument; 414, 415; 414, 416; 415, identifier:axis; 416, string:'x'; 417, keyword_argument; 417, 418; 417, 419; 418, identifier:labelsize; 419, call; 419, 420; 419, 421; 420, identifier:int; 421, argument_list; 421, 422; 422, binary_operator:*; 422, 423; 422, 426; 423, binary_operator:/; 423, 424; 423, 425; 424, identifier:fontsize; 425, integer:16; 426, integer:20; 427, if_statement; 427, 428; 427, 429; 427, 436; 428, identifier:inline; 429, block; 429, 430; 430, expression_statement; 430, 431; 431, call; 431, 432; 431, 435; 432, attribute; 432, 433; 432, 434; 433, identifier:plt; 434, identifier:show; 435, argument_list; 436, else_clause; 436, 437; 437, block; 437, 438; 438, return_statement; 438, 439; 439, identifier:ax0
def dendrogram(df, method='average', filter=None, n=0, p=0, sort=None, orientation=None, figsize=None, fontsize=16, inline=False ): """ Fits a `scipy` hierarchical clustering algorithm to the given DataFrame's variables and visualizes the results as a `scipy` dendrogram. The default vertical display will fit up to 50 columns. If more than 50 columns are specified and orientation is left unspecified the dendrogram will automatically swap to a horizontal display to fit the additional variables. :param df: The DataFrame whose completeness is being dendrogrammed. :param method: The distance measure being used for clustering. This is a parameter that is passed to `scipy.hierarchy`. :param filter: The filter to apply to the heatmap. Should be one of "top", "bottom", or None (default). :param n: The cap on the number of columns to include in the filtered DataFrame. :param p: The cap on the percentage fill of the columns in the filtered DataFrame. :param sort: The sort to apply to the heatmap. Should be one of "ascending", "descending", or None. :param figsize: The size of the figure to display. This is a `matplotlib` parameter which defaults to `(25, 10)`. :param fontsize: The figure's font size. :param orientation: The way the dendrogram is oriented. Defaults to top-down if there are less than or equal to 50 columns and left-right if there are more. :param inline: Whether or not the figure is inline. If it's not then instead of getting plotted, this method will return its figure. :return: If `inline` is False, the underlying `matplotlib.figure` object. Else, nothing. """ if not figsize: if len(df.columns) <= 50 or orientation == 'top' or orientation == 'bottom': figsize = (25, 10) else: figsize = (25, (25 + len(df.columns) - 50)*0.5) plt.figure(figsize=figsize) gs = gridspec.GridSpec(1, 1) ax0 = plt.subplot(gs[0]) df = nullity_filter(df, filter=filter, n=n, p=p) df = nullity_sort(df, sort=sort) # Link the hierarchical output matrix, figure out orientation, construct base dendrogram. x = np.transpose(df.isnull().astype(int).values) z = hierarchy.linkage(x, method) if not orientation: if len(df.columns) > 50: orientation = 'left' else: orientation = 'bottom' hierarchy.dendrogram(z, orientation=orientation, labels=df.columns.tolist(), distance_sort='descending', link_color_func=lambda c: 'black', leaf_font_size=fontsize, ax=ax0 ) # Remove extraneous default visual elements. ax0.set_aspect('auto') ax0.grid(b=False) if orientation == 'bottom': ax0.xaxis.tick_top() ax0.xaxis.set_ticks_position('none') ax0.yaxis.set_ticks_position('none') ax0.spines['top'].set_visible(False) ax0.spines['right'].set_visible(False) ax0.spines['bottom'].set_visible(False) ax0.spines['left'].set_visible(False) ax0.patch.set_visible(False) # Set up the categorical axis labels and draw. if orientation == 'bottom': ax0.set_xticklabels(ax0.xaxis.get_majorticklabels(), rotation=45, ha='left') elif orientation == 'top': ax0.set_xticklabels(ax0.xaxis.get_majorticklabels(), rotation=45, ha='right') if orientation == 'bottom' or orientation == 'top': ax0.tick_params(axis='y', labelsize=int(fontsize / 16 * 20)) else: ax0.tick_params(axis='x', labelsize=int(fontsize / 16 * 20)) if inline: plt.show() else: return ax0
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:nullity_sort; 3, parameters; 3, 4; 3, 5; 4, identifier:df; 5, default_parameter; 5, 6; 5, 7; 6, identifier:sort; 7, None; 8, block; 8, 9; 8, 11; 9, expression_statement; 9, 10; 10, comment; 11, if_statement; 11, 12; 11, 15; 11, 38; 11, 70; 12, comparison_operator:==; 12, 13; 12, 14; 13, identifier:sort; 14, string:'ascending'; 15, block; 15, 16; 16, return_statement; 16, 17; 17, subscript; 17, 18; 17, 21; 17, 36; 18, attribute; 18, 19; 18, 20; 19, identifier:df; 20, identifier:iloc; 21, call; 21, 22; 21, 25; 22, attribute; 22, 23; 22, 24; 23, identifier:np; 24, identifier:argsort; 25, argument_list; 25, 26; 26, attribute; 26, 27; 26, 35; 27, call; 27, 28; 27, 31; 28, attribute; 28, 29; 28, 30; 29, identifier:df; 30, identifier:count; 31, argument_list; 31, 32; 32, keyword_argument; 32, 33; 32, 34; 33, identifier:axis; 34, string:'columns'; 35, identifier:values; 36, slice; 36, 37; 37, colon; 38, elif_clause; 38, 39; 38, 42; 39, comparison_operator:==; 39, 40; 39, 41; 40, identifier:sort; 41, string:'descending'; 42, block; 42, 43; 43, return_statement; 43, 44; 44, subscript; 44, 45; 44, 48; 44, 68; 45, attribute; 45, 46; 45, 47; 46, identifier:df; 47, identifier:iloc; 48, call; 48, 49; 48, 52; 49, attribute; 49, 50; 49, 51; 50, identifier:np; 51, identifier:flipud; 52, argument_list; 52, 53; 53, call; 53, 54; 53, 57; 54, attribute; 54, 55; 54, 56; 55, identifier:np; 56, identifier:argsort; 57, argument_list; 57, 58; 58, attribute; 58, 59; 58, 67; 59, call; 59, 60; 59, 63; 60, attribute; 60, 61; 60, 62; 61, identifier:df; 62, identifier:count; 63, argument_list; 63, 64; 64, keyword_argument; 64, 65; 64, 66; 65, identifier:axis; 66, string:'columns'; 67, identifier:values; 68, slice; 68, 69; 69, colon; 70, else_clause; 70, 71; 71, block; 71, 72; 72, return_statement; 72, 73; 73, identifier:df
def nullity_sort(df, sort=None): """ Sorts a DataFrame according to its nullity, in either ascending or descending order. :param df: The DataFrame object being sorted. :param sort: The sorting method: either "ascending", "descending", or None (default). :return: The nullity-sorted DataFrame. """ if sort == 'ascending': return df.iloc[np.argsort(df.count(axis='columns').values), :] elif sort == 'descending': return df.iloc[np.flipud(np.argsort(df.count(axis='columns').values)), :] else: return df
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 20; 2, function_name:sortino_ratio; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 14; 3, 17; 4, identifier:returns; 5, default_parameter; 5, 6; 5, 7; 6, identifier:required_return; 7, integer:0; 8, default_parameter; 8, 9; 8, 10; 9, identifier:period; 10, identifier:DAILY; 11, default_parameter; 11, 12; 11, 13; 12, identifier:annualization; 13, None; 14, default_parameter; 14, 15; 14, 16; 15, identifier:out; 16, None; 17, default_parameter; 17, 18; 17, 19; 18, identifier:_downside_risk; 19, None; 20, block; 20, 21; 20, 23; 20, 29; 20, 47; 20, 55; 20, 84; 20, 97; 20, 105; 20, 117; 20, 133; 20, 144; 20, 173; 21, expression_statement; 21, 22; 22, comment; 23, expression_statement; 23, 24; 24, assignment; 24, 25; 24, 26; 25, identifier:allocated_output; 26, comparison_operator:is; 26, 27; 26, 28; 27, identifier:out; 28, None; 29, if_statement; 29, 30; 29, 31; 30, identifier:allocated_output; 31, block; 31, 32; 32, expression_statement; 32, 33; 33, assignment; 33, 34; 33, 35; 34, identifier:out; 35, call; 35, 36; 35, 39; 36, attribute; 36, 37; 36, 38; 37, identifier:np; 38, identifier:empty; 39, argument_list; 39, 40; 40, subscript; 40, 41; 40, 44; 41, attribute; 41, 42; 41, 43; 42, identifier:returns; 43, identifier:shape; 44, slice; 44, 45; 44, 46; 45, integer:1; 46, colon; 47, expression_statement; 47, 48; 48, assignment; 48, 49; 48, 50; 49, identifier:return_1d; 50, comparison_operator:==; 50, 51; 50, 54; 51, attribute; 51, 52; 51, 53; 52, identifier:returns; 53, identifier:ndim; 54, integer:1; 55, if_statement; 55, 56; 55, 62; 56, comparison_operator:<; 56, 57; 56, 61; 57, call; 57, 58; 57, 59; 58, identifier:len; 59, argument_list; 59, 60; 60, identifier:returns; 61, integer:2; 62, block; 62, 63; 62, 71; 62, 82; 63, expression_statement; 63, 64; 64, assignment; 64, 65; 64, 68; 65, subscript; 65, 66; 65, 67; 66, identifier:out; 67, tuple; 68, attribute; 68, 69; 68, 70; 69, identifier:np; 70, identifier:nan; 71, if_statement; 71, 72; 71, 73; 72, identifier:return_1d; 73, block; 73, 74; 74, expression_statement; 74, 75; 75, assignment; 75, 76; 75, 77; 76, identifier:out; 77, call; 77, 78; 77, 81; 78, attribute; 78, 79; 78, 80; 79, identifier:out; 80, identifier:item; 81, argument_list; 82, return_statement; 82, 83; 83, identifier:out; 84, expression_statement; 84, 85; 85, assignment; 85, 86; 85, 87; 86, identifier:adj_returns; 87, call; 87, 88; 87, 91; 88, attribute; 88, 89; 88, 90; 89, identifier:np; 90, identifier:asanyarray; 91, argument_list; 91, 92; 92, call; 92, 93; 92, 94; 93, identifier:_adjust_returns; 94, argument_list; 94, 95; 94, 96; 95, identifier:returns; 96, identifier:required_return; 97, expression_statement; 97, 98; 98, assignment; 98, 99; 98, 100; 99, identifier:ann_factor; 100, call; 100, 101; 100, 102; 101, identifier:annualization_factor; 102, argument_list; 102, 103; 102, 104; 103, identifier:period; 104, identifier:annualization; 105, expression_statement; 105, 106; 106, assignment; 106, 107; 106, 108; 107, identifier:average_annual_return; 108, binary_operator:*; 108, 109; 108, 116; 109, call; 109, 110; 109, 111; 110, identifier:nanmean; 111, argument_list; 111, 112; 111, 113; 112, identifier:adj_returns; 113, keyword_argument; 113, 114; 113, 115; 114, identifier:axis; 115, integer:0; 116, identifier:ann_factor; 117, expression_statement; 117, 118; 118, assignment; 118, 119; 118, 120; 119, identifier:annualized_downside_risk; 120, parenthesized_expression; 120, 121; 121, conditional_expression:if; 121, 122; 121, 123; 121, 126; 122, identifier:_downside_risk; 123, comparison_operator:is; 123, 124; 123, 125; 124, identifier:_downside_risk; 125, None; 126, call; 126, 127; 126, 128; 127, identifier:downside_risk; 128, argument_list; 128, 129; 128, 130; 128, 131; 128, 132; 129, identifier:returns; 130, identifier:required_return; 131, identifier:period; 132, identifier:annualization; 133, expression_statement; 133, 134; 134, call; 134, 135; 134, 138; 135, attribute; 135, 136; 135, 137; 136, identifier:np; 137, identifier:divide; 138, argument_list; 138, 139; 138, 140; 138, 141; 139, identifier:average_annual_return; 140, identifier:annualized_downside_risk; 141, keyword_argument; 141, 142; 141, 143; 142, identifier:out; 143, identifier:out; 144, if_statement; 144, 145; 144, 146; 144, 155; 145, identifier:return_1d; 146, block; 146, 147; 147, expression_statement; 147, 148; 148, assignment; 148, 149; 148, 150; 149, identifier:out; 150, call; 150, 151; 150, 154; 151, attribute; 151, 152; 151, 153; 152, identifier:out; 153, identifier:item; 154, argument_list; 155, elif_clause; 155, 156; 155, 163; 156, call; 156, 157; 156, 158; 157, identifier:isinstance; 158, argument_list; 158, 159; 158, 160; 159, identifier:returns; 160, attribute; 160, 161; 160, 162; 161, identifier:pd; 162, identifier:DataFrame; 163, block; 163, 164; 164, expression_statement; 164, 165; 165, assignment; 165, 166; 165, 167; 166, identifier:out; 167, call; 167, 168; 167, 171; 168, attribute; 168, 169; 168, 170; 169, identifier:pd; 170, identifier:Series; 171, argument_list; 171, 172; 172, identifier:out; 173, return_statement; 173, 174; 174, identifier:out
def sortino_ratio(returns, required_return=0, period=DAILY, annualization=None, out=None, _downside_risk=None): """ Determines the Sortino ratio of a strategy. Parameters ---------- returns : pd.Series or np.ndarray or pd.DataFrame Daily returns of the strategy, noncumulative. - See full explanation in :func:`~empyrical.stats.cum_returns`. required_return: float / series minimum acceptable return period : str, optional Defines the periodicity of the 'returns' data for purposes of annualizing. Value ignored if `annualization` parameter is specified. Defaults are:: 'monthly':12 'weekly': 52 'daily': 252 annualization : int, optional Used to suppress default values available in `period` to convert returns into annual returns. Value should be the annual frequency of `returns`. _downside_risk : float, optional The downside risk of the given inputs, if known. Will be calculated if not provided. out : array-like, optional Array to use as output buffer. If not passed, a new array will be created. Returns ------- sortino_ratio : float or pd.Series depends on input type series ==> float DataFrame ==> pd.Series Note ----- See `<https://www.sunrisecapital.com/wp-content/uploads/2014/06/Futures_ Mag_Sortino_0213.pdf>`__ for more details. """ allocated_output = out is None if allocated_output: out = np.empty(returns.shape[1:]) return_1d = returns.ndim == 1 if len(returns) < 2: out[()] = np.nan if return_1d: out = out.item() return out adj_returns = np.asanyarray(_adjust_returns(returns, required_return)) ann_factor = annualization_factor(period, annualization) average_annual_return = nanmean(adj_returns, axis=0) * ann_factor annualized_downside_risk = ( _downside_risk if _downside_risk is not None else downside_risk(returns, required_return, period, annualization) ) np.divide(average_annual_return, annualized_downside_risk, out=out) if return_1d: out = out.item() elif isinstance(returns, pd.DataFrame): out = pd.Series(out) return out
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 17; 2, function_name:downside_risk; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 14; 4, identifier:returns; 5, default_parameter; 5, 6; 5, 7; 6, identifier:required_return; 7, integer:0; 8, default_parameter; 8, 9; 8, 10; 9, identifier:period; 10, identifier:DAILY; 11, default_parameter; 11, 12; 11, 13; 12, identifier:annualization; 13, None; 14, default_parameter; 14, 15; 14, 16; 15, identifier:out; 16, None; 17, block; 17, 18; 17, 20; 17, 26; 17, 44; 17, 52; 17, 81; 17, 89; 17, 116; 17, 126; 17, 137; 17, 147; 17, 163; 17, 197; 18, expression_statement; 18, 19; 19, comment; 20, expression_statement; 20, 21; 21, assignment; 21, 22; 21, 23; 22, identifier:allocated_output; 23, comparison_operator:is; 23, 24; 23, 25; 24, identifier:out; 25, None; 26, if_statement; 26, 27; 26, 28; 27, identifier:allocated_output; 28, block; 28, 29; 29, expression_statement; 29, 30; 30, assignment; 30, 31; 30, 32; 31, identifier:out; 32, call; 32, 33; 32, 36; 33, attribute; 33, 34; 33, 35; 34, identifier:np; 35, identifier:empty; 36, argument_list; 36, 37; 37, subscript; 37, 38; 37, 41; 38, attribute; 38, 39; 38, 40; 39, identifier:returns; 40, identifier:shape; 41, slice; 41, 42; 41, 43; 42, integer:1; 43, colon; 44, expression_statement; 44, 45; 45, assignment; 45, 46; 45, 47; 46, identifier:returns_1d; 47, comparison_operator:==; 47, 48; 47, 51; 48, attribute; 48, 49; 48, 50; 49, identifier:returns; 50, identifier:ndim; 51, integer:1; 52, if_statement; 52, 53; 52, 59; 53, comparison_operator:<; 53, 54; 53, 58; 54, call; 54, 55; 54, 56; 55, identifier:len; 56, argument_list; 56, 57; 57, identifier:returns; 58, integer:1; 59, block; 59, 60; 59, 68; 59, 79; 60, expression_statement; 60, 61; 61, assignment; 61, 62; 61, 65; 62, subscript; 62, 63; 62, 64; 63, identifier:out; 64, tuple; 65, attribute; 65, 66; 65, 67; 66, identifier:np; 67, identifier:nan; 68, if_statement; 68, 69; 68, 70; 69, identifier:returns_1d; 70, block; 70, 71; 71, expression_statement; 71, 72; 72, assignment; 72, 73; 72, 74; 73, identifier:out; 74, call; 74, 75; 74, 78; 75, attribute; 75, 76; 75, 77; 76, identifier:out; 77, identifier:item; 78, argument_list; 79, return_statement; 79, 80; 80, identifier:out; 81, expression_statement; 81, 82; 82, assignment; 82, 83; 82, 84; 83, identifier:ann_factor; 84, call; 84, 85; 84, 86; 85, identifier:annualization_factor; 86, argument_list; 86, 87; 86, 88; 87, identifier:period; 88, identifier:annualization; 89, expression_statement; 89, 90; 90, assignment; 90, 91; 90, 92; 91, identifier:downside_diff; 92, call; 92, 93; 92, 96; 93, attribute; 93, 94; 93, 95; 94, identifier:np; 95, identifier:clip; 96, argument_list; 96, 97; 96, 112; 96, 115; 97, call; 97, 98; 97, 99; 98, identifier:_adjust_returns; 99, argument_list; 99, 100; 99, 106; 100, call; 100, 101; 100, 104; 101, attribute; 101, 102; 101, 103; 102, identifier:np; 103, identifier:asanyarray; 104, argument_list; 104, 105; 105, identifier:returns; 106, call; 106, 107; 106, 110; 107, attribute; 107, 108; 107, 109; 108, identifier:np; 109, identifier:asanyarray; 110, argument_list; 110, 111; 111, identifier:required_return; 112, attribute; 112, 113; 112, 114; 113, identifier:np; 114, identifier:NINF; 115, integer:0; 116, expression_statement; 116, 117; 117, call; 117, 118; 117, 121; 118, attribute; 118, 119; 118, 120; 119, identifier:np; 120, identifier:square; 121, argument_list; 121, 122; 121, 123; 122, identifier:downside_diff; 123, keyword_argument; 123, 124; 123, 125; 124, identifier:out; 125, identifier:downside_diff; 126, expression_statement; 126, 127; 127, call; 127, 128; 127, 129; 128, identifier:nanmean; 129, argument_list; 129, 130; 129, 131; 129, 134; 130, identifier:downside_diff; 131, keyword_argument; 131, 132; 131, 133; 132, identifier:axis; 133, integer:0; 134, keyword_argument; 134, 135; 134, 136; 135, identifier:out; 136, identifier:out; 137, expression_statement; 137, 138; 138, call; 138, 139; 138, 142; 139, attribute; 139, 140; 139, 141; 140, identifier:np; 141, identifier:sqrt; 142, argument_list; 142, 143; 142, 144; 143, identifier:out; 144, keyword_argument; 144, 145; 144, 146; 145, identifier:out; 146, identifier:out; 147, expression_statement; 147, 148; 148, call; 148, 149; 148, 152; 149, attribute; 149, 150; 149, 151; 150, identifier:np; 151, identifier:multiply; 152, argument_list; 152, 153; 152, 154; 152, 160; 153, identifier:out; 154, call; 154, 155; 154, 158; 155, attribute; 155, 156; 155, 157; 156, identifier:np; 157, identifier:sqrt; 158, argument_list; 158, 159; 159, identifier:ann_factor; 160, keyword_argument; 160, 161; 160, 162; 161, identifier:out; 162, identifier:out; 163, if_statement; 163, 164; 163, 165; 163, 174; 164, identifier:returns_1d; 165, block; 165, 166; 166, expression_statement; 166, 167; 167, assignment; 167, 168; 167, 169; 168, identifier:out; 169, call; 169, 170; 169, 173; 170, attribute; 170, 171; 170, 172; 171, identifier:out; 172, identifier:item; 173, argument_list; 174, elif_clause; 174, 175; 174, 182; 175, call; 175, 176; 175, 177; 176, identifier:isinstance; 177, argument_list; 177, 178; 177, 179; 178, identifier:returns; 179, attribute; 179, 180; 179, 181; 180, identifier:pd; 181, identifier:DataFrame; 182, block; 182, 183; 183, expression_statement; 183, 184; 184, assignment; 184, 185; 184, 186; 185, identifier:out; 186, call; 186, 187; 186, 190; 187, attribute; 187, 188; 187, 189; 188, identifier:pd; 189, identifier:Series; 190, argument_list; 190, 191; 190, 192; 191, identifier:out; 192, keyword_argument; 192, 193; 192, 194; 193, identifier:index; 194, attribute; 194, 195; 194, 196; 195, identifier:returns; 196, identifier:columns; 197, return_statement; 197, 198; 198, identifier:out
def downside_risk(returns, required_return=0, period=DAILY, annualization=None, out=None): """ Determines the downside deviation below a threshold Parameters ---------- returns : pd.Series or np.ndarray or pd.DataFrame Daily returns of the strategy, noncumulative. - See full explanation in :func:`~empyrical.stats.cum_returns`. required_return: float / series minimum acceptable return period : str, optional Defines the periodicity of the 'returns' data for purposes of annualizing. Value ignored if `annualization` parameter is specified. Defaults are:: 'monthly':12 'weekly': 52 'daily': 252 annualization : int, optional Used to suppress default values available in `period` to convert returns into annual returns. Value should be the annual frequency of `returns`. out : array-like, optional Array to use as output buffer. If not passed, a new array will be created. Returns ------- downside_deviation : float or pd.Series depends on input type series ==> float DataFrame ==> pd.Series Note ----- See `<https://www.sunrisecapital.com/wp-content/uploads/2014/06/Futures_ Mag_Sortino_0213.pdf>`__ for more details, specifically why using the standard deviation of the negative returns is not correct. """ allocated_output = out is None if allocated_output: out = np.empty(returns.shape[1:]) returns_1d = returns.ndim == 1 if len(returns) < 1: out[()] = np.nan if returns_1d: out = out.item() return out ann_factor = annualization_factor(period, annualization) downside_diff = np.clip( _adjust_returns( np.asanyarray(returns), np.asanyarray(required_return), ), np.NINF, 0, ) np.square(downside_diff, out=downside_diff) nanmean(downside_diff, axis=0, out=out) np.sqrt(out, out=out) np.multiply(out, np.sqrt(ann_factor), out=out) if returns_1d: out = out.item() elif isinstance(returns, pd.DataFrame): out = pd.Series(out, index=returns.columns) return out
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:GetLoadingOrder; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 12; 5, 41; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:result; 11, dictionary; 12, for_statement; 12, 13; 12, 16; 12, 23; 13, pattern_list; 13, 14; 13, 15; 14, identifier:filename; 15, identifier:mapping; 16, call; 16, 17; 16, 22; 17, attribute; 17, 18; 17, 21; 18, attribute; 18, 19; 18, 20; 19, identifier:self; 20, identifier:_file_mapping; 21, identifier:iteritems; 22, argument_list; 23, block; 23, 24; 23, 30; 24, expression_statement; 24, 25; 25, assignment; 25, 26; 25, 27; 26, identifier:loading_order; 27, subscript; 27, 28; 27, 29; 28, identifier:mapping; 29, string:'loading_order'; 30, if_statement; 30, 31; 30, 34; 31, comparison_operator:is; 31, 32; 31, 33; 32, identifier:loading_order; 33, None; 34, block; 34, 35; 35, expression_statement; 35, 36; 36, assignment; 36, 37; 36, 40; 37, subscript; 37, 38; 37, 39; 38, identifier:result; 39, identifier:loading_order; 40, identifier:filename; 41, return_statement; 41, 42; 42, call; 42, 43; 42, 44; 43, identifier:list; 44, generator_expression; 44, 45; 44, 48; 45, subscript; 45, 46; 45, 47; 46, identifier:result; 47, identifier:key; 48, for_in_clause; 48, 49; 48, 50; 49, identifier:key; 50, call; 50, 51; 50, 52; 51, identifier:sorted; 52, argument_list; 52, 53; 53, identifier:result
def GetLoadingOrder(self): """Returns a list of filenames sorted by loading order. Only includes files that Loader's standardized loading knows how to load""" result = {} for filename, mapping in self._file_mapping.iteritems(): loading_order = mapping['loading_order'] if loading_order is not None: result[loading_order] = filename return list(result[key] for key in sorted(result))
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:GetOrderKey; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 13; 5, 22; 5, 33; 5, 37; 5, 53; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:context_attributes; 11, list:['_type']; 11, 12; 12, string:'_type'; 13, expression_statement; 13, 14; 14, call; 14, 15; 14, 18; 15, attribute; 15, 16; 15, 17; 16, identifier:context_attributes; 17, identifier:extend; 18, argument_list; 18, 19; 19, attribute; 19, 20; 19, 21; 20, identifier:ExceptionWithContext; 21, identifier:CONTEXT_PARTS; 22, expression_statement; 22, 23; 23, call; 23, 24; 23, 27; 24, attribute; 24, 25; 24, 26; 25, identifier:context_attributes; 26, identifier:extend; 27, argument_list; 27, 28; 28, call; 28, 29; 28, 32; 29, attribute; 29, 30; 29, 31; 30, identifier:self; 31, identifier:_GetExtraOrderAttributes; 32, argument_list; 33, expression_statement; 33, 34; 34, assignment; 34, 35; 34, 36; 35, identifier:tokens; 36, list:[]; 37, for_statement; 37, 38; 37, 39; 37, 40; 38, identifier:context_attribute; 39, identifier:context_attributes; 40, block; 40, 41; 41, expression_statement; 41, 42; 42, call; 42, 43; 42, 46; 43, attribute; 43, 44; 43, 45; 44, identifier:tokens; 45, identifier:append; 46, argument_list; 46, 47; 47, call; 47, 48; 47, 49; 48, identifier:getattr; 49, argument_list; 49, 50; 49, 51; 49, 52; 50, identifier:self; 51, identifier:context_attribute; 52, None; 53, return_statement; 53, 54; 54, identifier:tokens
def GetOrderKey(self): """Return a tuple that can be used to sort problems into a consistent order. Returns: A list of values. """ context_attributes = ['_type'] context_attributes.extend(ExceptionWithContext.CONTEXT_PARTS) context_attributes.extend(self._GetExtraOrderAttributes()) tokens = [] for context_attribute in context_attributes: tokens.append(getattr(self, context_attribute, None)) return tokens
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 7; 2, function_name:AddShapePointObjectUnsorted; 3, parameters; 3, 4; 3, 5; 3, 6; 4, identifier:self; 5, identifier:shapepoint; 6, identifier:problems; 7, block; 7, 8; 7, 10; 7, 72; 7, 102; 7, 201; 7, 218; 7, 230; 7, 242; 8, expression_statement; 8, 9; 9, comment; 10, if_statement; 10, 11; 10, 31; 10, 41; 10, 56; 11, parenthesized_expression; 11, 12; 12, boolean_operator:or; 12, 13; 12, 21; 13, comparison_operator:==; 13, 14; 13, 20; 14, call; 14, 15; 14, 16; 15, identifier:len; 16, argument_list; 16, 17; 17, attribute; 17, 18; 17, 19; 18, identifier:self; 19, identifier:sequence; 20, integer:0; 21, comparison_operator:>=; 21, 22; 21, 25; 22, attribute; 22, 23; 22, 24; 23, identifier:shapepoint; 24, identifier:shape_pt_sequence; 25, subscript; 25, 26; 25, 29; 26, attribute; 26, 27; 26, 28; 27, identifier:self; 28, identifier:sequence; 29, unary_operator:-; 29, 30; 30, integer:1; 31, block; 31, 32; 32, expression_statement; 32, 33; 33, assignment; 33, 34; 33, 35; 34, identifier:index; 35, call; 35, 36; 35, 37; 36, identifier:len; 37, argument_list; 37, 38; 38, attribute; 38, 39; 38, 40; 39, identifier:self; 40, identifier:sequence; 41, elif_clause; 41, 42; 41, 51; 42, comparison_operator:<=; 42, 43; 42, 46; 43, attribute; 43, 44; 43, 45; 44, identifier:shapepoint; 45, identifier:shape_pt_sequence; 46, subscript; 46, 47; 46, 50; 47, attribute; 47, 48; 47, 49; 48, identifier:self; 49, identifier:sequence; 50, integer:0; 51, block; 51, 52; 52, expression_statement; 52, 53; 53, assignment; 53, 54; 53, 55; 54, identifier:index; 55, integer:0; 56, else_clause; 56, 57; 57, block; 57, 58; 58, expression_statement; 58, 59; 59, assignment; 59, 60; 59, 61; 60, identifier:index; 61, call; 61, 62; 61, 65; 62, attribute; 62, 63; 62, 64; 63, identifier:bisect; 64, identifier:bisect; 65, argument_list; 65, 66; 65, 69; 66, attribute; 66, 67; 66, 68; 67, identifier:self; 68, identifier:sequence; 69, attribute; 69, 70; 69, 71; 70, identifier:shapepoint; 71, identifier:shape_pt_sequence; 72, if_statement; 72, 73; 72, 80; 73, comparison_operator:in; 73, 74; 73, 77; 74, attribute; 74, 75; 74, 76; 75, identifier:shapepoint; 76, identifier:shape_pt_sequence; 77, attribute; 77, 78; 77, 79; 78, identifier:self; 79, identifier:sequence; 80, block; 80, 81; 81, expression_statement; 81, 82; 82, call; 82, 83; 82, 86; 83, attribute; 83, 84; 83, 85; 84, identifier:problems; 85, identifier:InvalidValue; 86, argument_list; 86, 87; 86, 88; 86, 91; 87, string:'shape_pt_sequence'; 88, attribute; 88, 89; 88, 90; 89, identifier:shapepoint; 90, identifier:shape_pt_sequence; 91, binary_operator:%; 91, 92; 91, 95; 92, concatenated_string; 92, 93; 92, 94; 93, string:'The sequence number %d occurs more than once in '; 94, string:'shape %s.'; 95, tuple; 95, 96; 95, 99; 96, attribute; 96, 97; 96, 98; 97, identifier:shapepoint; 98, identifier:shape_pt_sequence; 99, attribute; 99, 100; 99, 101; 100, identifier:self; 101, identifier:shape_id; 102, if_statement; 102, 103; 102, 117; 103, boolean_operator:and; 103, 104; 103, 109; 104, comparison_operator:is; 104, 105; 104, 108; 105, attribute; 105, 106; 105, 107; 106, identifier:shapepoint; 107, identifier:shape_dist_traveled; 108, None; 109, comparison_operator:>; 109, 110; 109, 116; 110, call; 110, 111; 110, 112; 111, identifier:len; 112, argument_list; 112, 113; 113, attribute; 113, 114; 113, 115; 114, identifier:self; 115, identifier:sequence; 116, integer:0; 117, block; 117, 118; 117, 160; 118, if_statement; 118, 119; 118, 138; 119, parenthesized_expression; 119, 120; 120, boolean_operator:and; 120, 121; 120, 129; 121, comparison_operator:!=; 121, 122; 121, 123; 122, identifier:index; 123, call; 123, 124; 123, 125; 124, identifier:len; 125, argument_list; 125, 126; 126, attribute; 126, 127; 126, 128; 127, identifier:self; 128, identifier:sequence; 129, comparison_operator:>; 129, 130; 129, 133; 130, attribute; 130, 131; 130, 132; 131, identifier:shapepoint; 132, identifier:shape_dist_traveled; 133, subscript; 133, 134; 133, 137; 134, attribute; 134, 135; 134, 136; 135, identifier:self; 136, identifier:distance; 137, identifier:index; 138, block; 138, 139; 139, expression_statement; 139, 140; 140, call; 140, 141; 140, 144; 141, attribute; 141, 142; 141, 143; 142, identifier:problems; 143, identifier:InvalidValue; 144, argument_list; 144, 145; 144, 146; 144, 149; 145, string:'shape_dist_traveled'; 146, attribute; 146, 147; 146, 148; 147, identifier:shapepoint; 148, identifier:shape_dist_traveled; 149, binary_operator:%; 149, 150; 149, 155; 150, concatenated_string; 150, 151; 150, 152; 150, 153; 150, 154; 151, string:'Each subsequent point in a shape should have '; 152, string:'a distance value that shouldn\'t be larger '; 153, string:'than the next ones. In this case, the next '; 154, string:'distance was %f.'; 155, subscript; 155, 156; 155, 159; 156, attribute; 156, 157; 156, 158; 157, identifier:self; 158, identifier:distance; 159, identifier:index; 160, if_statement; 160, 161; 160, 177; 161, parenthesized_expression; 161, 162; 162, boolean_operator:and; 162, 163; 162, 166; 163, comparison_operator:>; 163, 164; 163, 165; 164, identifier:index; 165, integer:0; 166, comparison_operator:<; 166, 167; 166, 170; 167, attribute; 167, 168; 167, 169; 168, identifier:shapepoint; 169, identifier:shape_dist_traveled; 170, subscript; 170, 171; 170, 174; 171, attribute; 171, 172; 171, 173; 172, identifier:self; 173, identifier:distance; 174, binary_operator:-; 174, 175; 174, 176; 175, identifier:index; 176, integer:1; 177, block; 177, 178; 178, expression_statement; 178, 179; 179, call; 179, 180; 179, 183; 180, attribute; 180, 181; 180, 182; 181, identifier:problems; 182, identifier:InvalidValue; 183, argument_list; 183, 184; 183, 185; 183, 188; 184, string:'shape_dist_traveled'; 185, attribute; 185, 186; 185, 187; 186, identifier:shapepoint; 187, identifier:shape_dist_traveled; 188, binary_operator:%; 188, 189; 188, 194; 189, concatenated_string; 189, 190; 189, 191; 189, 192; 189, 193; 190, string:'Each subsequent point in a shape should have '; 191, string:'a distance value that\'s at least as large as '; 192, string:'the previous ones. In this case, the previous '; 193, string:'distance was %f.'; 194, subscript; 194, 195; 194, 198; 195, attribute; 195, 196; 195, 197; 196, identifier:self; 197, identifier:distance; 198, binary_operator:-; 198, 199; 198, 200; 199, identifier:index; 200, integer:1; 201, if_statement; 201, 202; 201, 209; 202, comparison_operator:>; 202, 203; 202, 206; 203, attribute; 203, 204; 203, 205; 204, identifier:shapepoint; 205, identifier:shape_dist_traveled; 206, attribute; 206, 207; 206, 208; 207, identifier:self; 208, identifier:max_distance; 209, block; 209, 210; 210, expression_statement; 210, 211; 211, assignment; 211, 212; 211, 215; 212, attribute; 212, 213; 212, 214; 213, identifier:self; 214, identifier:max_distance; 215, attribute; 215, 216; 215, 217; 216, identifier:shapepoint; 217, identifier:shape_dist_traveled; 218, expression_statement; 218, 219; 219, call; 219, 220; 219, 225; 220, attribute; 220, 221; 220, 224; 221, attribute; 221, 222; 221, 223; 222, identifier:self; 223, identifier:sequence; 224, identifier:insert; 225, argument_list; 225, 226; 225, 227; 226, identifier:index; 227, attribute; 227, 228; 227, 229; 228, identifier:shapepoint; 229, identifier:shape_pt_sequence; 230, expression_statement; 230, 231; 231, call; 231, 232; 231, 237; 232, attribute; 232, 233; 232, 236; 233, attribute; 233, 234; 233, 235; 234, identifier:self; 235, identifier:distance; 236, identifier:insert; 237, argument_list; 237, 238; 237, 239; 238, identifier:index; 239, attribute; 239, 240; 239, 241; 240, identifier:shapepoint; 241, identifier:shape_dist_traveled; 242, expression_statement; 242, 243; 243, call; 243, 244; 243, 249; 244, attribute; 244, 245; 244, 248; 245, attribute; 245, 246; 245, 247; 246, identifier:self; 247, identifier:points; 248, identifier:insert; 249, argument_list; 249, 250; 249, 251; 250, identifier:index; 251, tuple; 251, 252; 251, 255; 251, 258; 252, attribute; 252, 253; 252, 254; 253, identifier:shapepoint; 254, identifier:shape_pt_lat; 255, attribute; 255, 256; 255, 257; 256, identifier:shapepoint; 257, identifier:shape_pt_lon; 258, attribute; 258, 259; 258, 260; 259, identifier:shapepoint; 260, identifier:shape_dist_traveled
def AddShapePointObjectUnsorted(self, shapepoint, problems): """Insert a point into a correct position by sequence. """ if (len(self.sequence) == 0 or shapepoint.shape_pt_sequence >= self.sequence[-1]): index = len(self.sequence) elif shapepoint.shape_pt_sequence <= self.sequence[0]: index = 0 else: index = bisect.bisect(self.sequence, shapepoint.shape_pt_sequence) if shapepoint.shape_pt_sequence in self.sequence: problems.InvalidValue('shape_pt_sequence', shapepoint.shape_pt_sequence, 'The sequence number %d occurs more than once in ' 'shape %s.' % (shapepoint.shape_pt_sequence, self.shape_id)) if shapepoint.shape_dist_traveled is not None and len(self.sequence) > 0: if (index != len(self.sequence) and shapepoint.shape_dist_traveled > self.distance[index]): problems.InvalidValue('shape_dist_traveled', shapepoint.shape_dist_traveled, 'Each subsequent point in a shape should have ' 'a distance value that shouldn\'t be larger ' 'than the next ones. In this case, the next ' 'distance was %f.' % self.distance[index]) if (index > 0 and shapepoint.shape_dist_traveled < self.distance[index - 1]): problems.InvalidValue('shape_dist_traveled', shapepoint.shape_dist_traveled, 'Each subsequent point in a shape should have ' 'a distance value that\'s at least as large as ' 'the previous ones. In this case, the previous ' 'distance was %f.' % self.distance[index - 1]) if shapepoint.shape_dist_traveled > self.max_distance: self.max_distance = shapepoint.shape_dist_traveled self.sequence.insert(index, shapepoint.shape_pt_sequence) self.distance.insert(index, shapepoint.shape_dist_traveled) self.points.insert(index, (shapepoint.shape_pt_lat, shapepoint.shape_pt_lon, shapepoint.shape_dist_traveled))
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:GetStopTimes; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, default_parameter; 5, 6; 5, 7; 6, identifier:problems; 7, None; 8, block; 8, 9; 8, 11; 8, 12; 8, 13; 8, 25; 8, 41; 8, 45; 8, 55; 8, 68; 8, 144; 9, expression_statement; 9, 10; 10, comment; 11, comment; 12, comment; 13, expression_statement; 13, 14; 14, assignment; 14, 15; 14, 16; 15, identifier:cursor; 16, call; 16, 17; 16, 24; 17, attribute; 17, 18; 17, 23; 18, attribute; 18, 19; 18, 22; 19, attribute; 19, 20; 19, 21; 20, identifier:self; 21, identifier:_schedule; 22, identifier:_connection; 23, identifier:cursor; 24, argument_list; 25, expression_statement; 25, 26; 26, call; 26, 27; 26, 30; 27, attribute; 27, 28; 27, 29; 28, identifier:cursor; 29, identifier:execute; 30, argument_list; 30, 31; 30, 37; 31, concatenated_string; 31, 32; 31, 33; 31, 34; 31, 35; 31, 36; 32, string:'SELECT arrival_secs,departure_secs,stop_headsign,pickup_type,'; 33, string:'drop_off_type,shape_dist_traveled,stop_id,stop_sequence,timepoint '; 34, string:'FROM stop_times '; 35, string:'WHERE trip_id=? '; 36, string:'ORDER BY stop_sequence'; 37, tuple; 37, 38; 38, attribute; 38, 39; 38, 40; 39, identifier:self; 40, identifier:trip_id; 41, expression_statement; 41, 42; 42, assignment; 42, 43; 42, 44; 43, identifier:stop_times; 44, list:[]; 45, expression_statement; 45, 46; 46, assignment; 46, 47; 46, 48; 47, identifier:stoptime_class; 48, attribute; 48, 49; 48, 54; 49, call; 49, 50; 49, 53; 50, attribute; 50, 51; 50, 52; 51, identifier:self; 52, identifier:GetGtfsFactory; 53, argument_list; 54, identifier:StopTime; 55, if_statement; 55, 56; 55, 59; 55, 60; 55, 61; 56, comparison_operator:is; 56, 57; 56, 58; 57, identifier:problems; 58, None; 59, comment; 60, comment; 61, block; 61, 62; 62, expression_statement; 62, 63; 63, assignment; 63, 64; 63, 65; 64, identifier:problems; 65, attribute; 65, 66; 65, 67; 66, identifier:problems_module; 67, identifier:default_problem_reporter; 68, for_statement; 68, 69; 68, 70; 68, 75; 69, identifier:row; 70, call; 70, 71; 70, 74; 71, attribute; 71, 72; 71, 73; 72, identifier:cursor; 73, identifier:fetchall; 74, argument_list; 75, block; 75, 76; 75, 89; 76, expression_statement; 76, 77; 77, assignment; 77, 78; 77, 79; 78, identifier:stop; 79, call; 79, 80; 79, 85; 80, attribute; 80, 81; 80, 84; 81, attribute; 81, 82; 81, 83; 82, identifier:self; 83, identifier:_schedule; 84, identifier:GetStop; 85, argument_list; 85, 86; 86, subscript; 86, 87; 86, 88; 87, identifier:row; 88, integer:6; 89, expression_statement; 89, 90; 90, call; 90, 91; 90, 94; 91, attribute; 91, 92; 91, 93; 92, identifier:stop_times; 93, identifier:append; 94, argument_list; 94, 95; 95, call; 95, 96; 95, 97; 96, identifier:stoptime_class; 97, argument_list; 97, 98; 97, 101; 97, 104; 97, 109; 97, 114; 97, 119; 97, 124; 97, 129; 97, 134; 97, 139; 98, keyword_argument; 98, 99; 98, 100; 99, identifier:problems; 100, identifier:problems; 101, keyword_argument; 101, 102; 101, 103; 102, identifier:stop; 103, identifier:stop; 104, keyword_argument; 104, 105; 104, 106; 105, identifier:arrival_secs; 106, subscript; 106, 107; 106, 108; 107, identifier:row; 108, integer:0; 109, keyword_argument; 109, 110; 109, 111; 110, identifier:departure_secs; 111, subscript; 111, 112; 111, 113; 112, identifier:row; 113, integer:1; 114, keyword_argument; 114, 115; 114, 116; 115, identifier:stop_headsign; 116, subscript; 116, 117; 116, 118; 117, identifier:row; 118, integer:2; 119, keyword_argument; 119, 120; 119, 121; 120, identifier:pickup_type; 121, subscript; 121, 122; 121, 123; 122, identifier:row; 123, integer:3; 124, keyword_argument; 124, 125; 124, 126; 125, identifier:drop_off_type; 126, subscript; 126, 127; 126, 128; 127, identifier:row; 128, integer:4; 129, keyword_argument; 129, 130; 129, 131; 130, identifier:shape_dist_traveled; 131, subscript; 131, 132; 131, 133; 132, identifier:row; 133, integer:5; 134, keyword_argument; 134, 135; 134, 136; 135, identifier:stop_sequence; 136, subscript; 136, 137; 136, 138; 137, identifier:row; 138, integer:7; 139, keyword_argument; 139, 140; 139, 141; 140, identifier:timepoint; 141, subscript; 141, 142; 141, 143; 142, identifier:row; 143, integer:8; 144, return_statement; 144, 145; 145, identifier:stop_times
def GetStopTimes(self, problems=None): """Return a sorted list of StopTime objects for this trip.""" # In theory problems=None should be safe because data from database has been # validated. See comment in _LoadStopTimes for why this isn't always true. cursor = self._schedule._connection.cursor() cursor.execute( 'SELECT arrival_secs,departure_secs,stop_headsign,pickup_type,' 'drop_off_type,shape_dist_traveled,stop_id,stop_sequence,timepoint ' 'FROM stop_times ' 'WHERE trip_id=? ' 'ORDER BY stop_sequence', (self.trip_id,)) stop_times = [] stoptime_class = self.GetGtfsFactory().StopTime if problems is None: # TODO: delete this branch when StopTime.__init__ doesn't need a # ProblemReporter problems = problems_module.default_problem_reporter for row in cursor.fetchall(): stop = self._schedule.GetStop(row[6]) stop_times.append(stoptime_class(problems=problems, stop=stop, arrival_secs=row[0], departure_secs=row[1], stop_headsign=row[2], pickup_type=row[3], drop_off_type=row[4], shape_dist_traveled=row[5], stop_sequence=row[7], timepoint=row[8])) return stop_times
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:GetFrequencyStartTimes; 3, parameters; 3, 4; 4, identifier:self; 5, block; 5, 6; 5, 8; 5, 12; 5, 13; 5, 55; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:start_times; 11, list:[]; 12, comment; 13, for_statement; 13, 14; 13, 15; 13, 20; 14, identifier:freq_tuple; 15, call; 15, 16; 15, 19; 16, attribute; 16, 17; 16, 18; 17, identifier:self; 18, identifier:GetFrequencyTuples; 19, argument_list; 20, block; 20, 21; 20, 33; 20, 34; 20, 38; 21, expression_statement; 21, 22; 22, assignment; 22, 23; 22, 27; 23, tuple_pattern; 23, 24; 23, 25; 23, 26; 24, identifier:start_secs; 25, identifier:end_secs; 26, identifier:headway_secs; 27, subscript; 27, 28; 27, 29; 28, identifier:freq_tuple; 29, slice; 29, 30; 29, 31; 29, 32; 30, integer:0; 31, colon; 32, integer:3; 33, comment; 34, expression_statement; 34, 35; 35, assignment; 35, 36; 35, 37; 36, identifier:run_secs; 37, identifier:start_secs; 38, while_statement; 38, 39; 38, 42; 39, comparison_operator:<; 39, 40; 39, 41; 40, identifier:run_secs; 41, identifier:end_secs; 42, block; 42, 43; 42, 50; 42, 51; 43, expression_statement; 43, 44; 44, call; 44, 45; 44, 48; 45, attribute; 45, 46; 45, 47; 46, identifier:start_times; 47, identifier:append; 48, argument_list; 48, 49; 49, identifier:run_secs; 50, comment; 51, expression_statement; 51, 52; 52, augmented_assignment:+=; 52, 53; 52, 54; 53, identifier:run_secs; 54, identifier:headway_secs; 55, return_statement; 55, 56; 56, identifier:start_times
def GetFrequencyStartTimes(self): """Return a list of start time for each headway-based run. Returns: a sorted list of seconds since midnight, the start time of each run. If this trip doesn't have headways returns an empty list.""" start_times = [] # for each headway period of the trip for freq_tuple in self.GetFrequencyTuples(): (start_secs, end_secs, headway_secs) = freq_tuple[0:3] # reset run secs to the start of the timeframe run_secs = start_secs while run_secs < end_secs: start_times.append(run_secs) # increment current run secs by headway secs run_secs += headway_secs return start_times
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:create; 3, parameters; 3, 4; 3, 5; 4, identifier:filename; 5, identifier:spec; 6, block; 6, 7; 6, 9; 6, 14; 6, 52; 6, 65; 6, 76; 6, 93; 6, 106; 6, 115; 6, 149; 6, 165; 6, 194; 6, 231; 6, 237; 6, 283; 6, 304; 6, 341; 6, 384; 7, expression_statement; 7, 8; 8, comment; 9, import_from_statement; 9, 10; 9, 12; 10, relative_import; 10, 11; 11, import_prefix; 12, dotted_name; 12, 13; 13, identifier:_segyio; 14, if_statement; 14, 15; 14, 20; 14, 27; 15, not_operator; 15, 16; 16, call; 16, 17; 16, 18; 17, identifier:structured; 18, argument_list; 18, 19; 19, identifier:spec; 20, block; 20, 21; 21, expression_statement; 21, 22; 22, assignment; 22, 23; 22, 24; 23, identifier:tracecount; 24, attribute; 24, 25; 24, 26; 25, identifier:spec; 26, identifier:tracecount; 27, else_clause; 27, 28; 28, block; 28, 29; 29, expression_statement; 29, 30; 30, assignment; 30, 31; 30, 32; 31, identifier:tracecount; 32, binary_operator:*; 32, 33; 32, 46; 33, binary_operator:*; 33, 34; 33, 40; 34, call; 34, 35; 34, 36; 35, identifier:len; 36, argument_list; 36, 37; 37, attribute; 37, 38; 37, 39; 38, identifier:spec; 39, identifier:ilines; 40, call; 40, 41; 40, 42; 41, identifier:len; 42, argument_list; 42, 43; 43, attribute; 43, 44; 43, 45; 44, identifier:spec; 45, identifier:xlines; 46, call; 46, 47; 46, 48; 47, identifier:len; 48, argument_list; 48, 49; 49, attribute; 49, 50; 49, 51; 50, identifier:spec; 51, identifier:offsets; 52, expression_statement; 52, 53; 53, assignment; 53, 54; 53, 55; 54, identifier:ext_headers; 55, conditional_expression:if; 55, 56; 55, 59; 55, 64; 56, attribute; 56, 57; 56, 58; 57, identifier:spec; 58, identifier:ext_headers; 59, call; 59, 60; 59, 61; 60, identifier:hasattr; 61, argument_list; 61, 62; 61, 63; 62, identifier:spec; 63, string:'ext_headers'; 64, integer:0; 65, expression_statement; 65, 66; 66, assignment; 66, 67; 66, 68; 67, identifier:samples; 68, call; 68, 69; 68, 72; 69, attribute; 69, 70; 69, 71; 70, identifier:numpy; 71, identifier:asarray; 72, argument_list; 72, 73; 73, attribute; 73, 74; 73, 75; 74, identifier:spec; 75, identifier:samples; 76, expression_statement; 76, 77; 77, assignment; 77, 78; 77, 79; 78, identifier:endians; 79, dictionary; 79, 80; 79, 83; 79, 84; 79, 87; 79, 90; 80, pair; 80, 81; 80, 82; 81, string:'lsb'; 82, integer:256; 83, comment; 84, pair; 84, 85; 84, 86; 85, string:'little'; 86, integer:256; 87, pair; 87, 88; 87, 89; 88, string:'msb'; 89, integer:0; 90, pair; 90, 91; 90, 92; 91, string:'big'; 92, integer:0; 93, expression_statement; 93, 94; 94, assignment; 94, 95; 94, 96; 95, identifier:endian; 96, conditional_expression:if; 96, 97; 96, 100; 96, 105; 97, attribute; 97, 98; 97, 99; 98, identifier:spec; 99, identifier:endian; 100, call; 100, 101; 100, 102; 101, identifier:hasattr; 102, argument_list; 102, 103; 102, 104; 103, identifier:spec; 104, string:'endian'; 105, string:'big'; 106, if_statement; 106, 107; 106, 110; 107, comparison_operator:is; 107, 108; 107, 109; 108, identifier:endian; 109, None; 110, block; 110, 111; 111, expression_statement; 111, 112; 112, assignment; 112, 113; 112, 114; 113, identifier:endian; 114, string:'big'; 115, if_statement; 115, 116; 115, 119; 116, comparison_operator:not; 116, 117; 116, 118; 117, identifier:endian; 118, identifier:endians; 119, block; 119, 120; 119, 124; 119, 137; 120, expression_statement; 120, 121; 121, assignment; 121, 122; 121, 123; 122, identifier:problem; 123, string:'unknown endianness {}, expected one of: '; 124, expression_statement; 124, 125; 125, assignment; 125, 126; 125, 127; 126, identifier:opts; 127, call; 127, 128; 127, 131; 128, attribute; 128, 129; 128, 130; 129, string:' '; 130, identifier:join; 131, argument_list; 131, 132; 132, call; 132, 133; 132, 136; 133, attribute; 133, 134; 133, 135; 134, identifier:endians; 135, identifier:keys; 136, argument_list; 137, raise_statement; 137, 138; 138, call; 138, 139; 138, 140; 139, identifier:ValueError; 140, argument_list; 140, 141; 141, binary_operator:+; 141, 142; 141, 148; 142, call; 142, 143; 142, 146; 143, attribute; 143, 144; 143, 145; 144, identifier:problem; 145, identifier:format; 146, argument_list; 146, 147; 147, identifier:endian; 148, identifier:opts; 149, expression_statement; 149, 150; 150, assignment; 150, 151; 150, 152; 151, identifier:fd; 152, call; 152, 153; 152, 156; 153, attribute; 153, 154; 153, 155; 154, identifier:_segyio; 155, identifier:segyiofd; 156, argument_list; 156, 157; 156, 161; 156, 162; 157, call; 157, 158; 157, 159; 158, identifier:str; 159, argument_list; 159, 160; 160, identifier:filename; 161, string:'w+'; 162, subscript; 162, 163; 162, 164; 163, identifier:endians; 164, identifier:endian; 165, expression_statement; 165, 166; 166, call; 166, 167; 166, 170; 167, attribute; 167, 168; 167, 169; 168, identifier:fd; 169, identifier:segymake; 170, argument_list; 170, 171; 170, 177; 170, 180; 170, 188; 171, keyword_argument; 171, 172; 171, 173; 172, identifier:samples; 173, call; 173, 174; 173, 175; 174, identifier:len; 175, argument_list; 175, 176; 176, identifier:samples; 177, keyword_argument; 177, 178; 177, 179; 178, identifier:tracecount; 179, identifier:tracecount; 180, keyword_argument; 180, 181; 180, 182; 181, identifier:format; 182, call; 182, 183; 182, 184; 183, identifier:int; 184, argument_list; 184, 185; 185, attribute; 185, 186; 185, 187; 186, identifier:spec; 187, identifier:format; 188, keyword_argument; 188, 189; 188, 190; 189, identifier:ext_headers; 190, call; 190, 191; 190, 192; 191, identifier:int; 192, argument_list; 192, 193; 193, identifier:ext_headers; 194, expression_statement; 194, 195; 195, assignment; 195, 196; 195, 197; 196, identifier:f; 197, call; 197, 198; 197, 201; 198, attribute; 198, 199; 198, 200; 199, identifier:segyio; 200, identifier:SegyFile; 201, argument_list; 201, 202; 201, 203; 201, 209; 201, 212; 201, 220; 201, 228; 202, identifier:fd; 203, keyword_argument; 203, 204; 203, 205; 204, identifier:filename; 205, call; 205, 206; 205, 207; 206, identifier:str; 207, argument_list; 207, 208; 208, identifier:filename; 209, keyword_argument; 209, 210; 209, 211; 210, identifier:mode; 211, string:'w+'; 212, keyword_argument; 212, 213; 212, 214; 213, identifier:iline; 214, call; 214, 215; 214, 216; 215, identifier:int; 216, argument_list; 216, 217; 217, attribute; 217, 218; 217, 219; 218, identifier:spec; 219, identifier:iline; 220, keyword_argument; 220, 221; 220, 222; 221, identifier:xline; 222, call; 222, 223; 222, 224; 223, identifier:int; 224, argument_list; 224, 225; 225, attribute; 225, 226; 225, 227; 226, identifier:spec; 227, identifier:xline; 228, keyword_argument; 228, 229; 228, 230; 229, identifier:endian; 230, identifier:endian; 231, expression_statement; 231, 232; 232, assignment; 232, 233; 232, 236; 233, attribute; 233, 234; 233, 235; 234, identifier:f; 235, identifier:_samples; 236, identifier:samples; 237, if_statement; 237, 238; 237, 242; 238, call; 238, 239; 238, 240; 239, identifier:structured; 240, argument_list; 240, 241; 241, identifier:spec; 242, block; 242, 243; 242, 256; 242, 267; 243, expression_statement; 243, 244; 244, assignment; 244, 245; 244, 246; 245, identifier:sorting; 246, conditional_expression:if; 246, 247; 246, 250; 246, 255; 247, attribute; 247, 248; 247, 249; 248, identifier:spec; 249, identifier:sorting; 250, call; 250, 251; 250, 252; 251, identifier:hasattr; 252, argument_list; 252, 253; 252, 254; 253, identifier:spec; 254, string:'sorting'; 255, None; 256, if_statement; 256, 257; 256, 260; 257, comparison_operator:is; 257, 258; 257, 259; 258, identifier:sorting; 259, None; 260, block; 260, 261; 261, expression_statement; 261, 262; 262, assignment; 262, 263; 262, 264; 263, identifier:sorting; 264, attribute; 264, 265; 264, 266; 265, identifier:TraceSortingFormat; 266, identifier:INLINE_SORTING; 267, expression_statement; 267, 268; 268, call; 268, 269; 268, 272; 269, attribute; 269, 270; 269, 271; 270, identifier:f; 271, identifier:interpret; 272, argument_list; 272, 273; 272, 276; 272, 279; 272, 282; 273, attribute; 273, 274; 273, 275; 274, identifier:spec; 275, identifier:ilines; 276, attribute; 276, 277; 276, 278; 277, identifier:spec; 278, identifier:xlines; 279, attribute; 279, 280; 279, 281; 280, identifier:spec; 281, identifier:offsets; 282, identifier:sorting; 283, expression_statement; 283, 284; 284, assignment; 284, 285; 284, 290; 285, subscript; 285, 286; 285, 289; 286, attribute; 286, 287; 286, 288; 287, identifier:f; 288, identifier:text; 289, integer:0; 290, call; 290, 291; 290, 292; 291, identifier:default_text_header; 292, argument_list; 292, 293; 292, 296; 292, 299; 293, attribute; 293, 294; 293, 295; 294, identifier:f; 295, identifier:_il; 296, attribute; 296, 297; 296, 298; 297, identifier:f; 298, identifier:_xl; 299, attribute; 299, 300; 299, 303; 300, attribute; 300, 301; 300, 302; 301, identifier:segyio; 302, identifier:TraceField; 303, identifier:offset; 304, if_statement; 304, 305; 304, 311; 304, 323; 305, comparison_operator:==; 305, 306; 305, 310; 306, call; 306, 307; 306, 308; 307, identifier:len; 308, argument_list; 308, 309; 309, identifier:samples; 310, integer:1; 311, block; 311, 312; 312, expression_statement; 312, 313; 313, assignment; 313, 314; 313, 315; 314, identifier:interval; 315, call; 315, 316; 315, 317; 316, identifier:int; 317, argument_list; 317, 318; 318, binary_operator:*; 318, 319; 318, 322; 319, subscript; 319, 320; 319, 321; 320, identifier:samples; 321, integer:0; 322, integer:1000; 323, else_clause; 323, 324; 324, block; 324, 325; 325, expression_statement; 325, 326; 326, assignment; 326, 327; 326, 328; 327, identifier:interval; 328, call; 328, 329; 328, 330; 329, identifier:int; 330, argument_list; 330, 331; 331, binary_operator:*; 331, 332; 331, 340; 332, parenthesized_expression; 332, 333; 333, binary_operator:-; 333, 334; 333, 337; 334, subscript; 334, 335; 334, 336; 335, identifier:samples; 336, integer:1; 337, subscript; 337, 338; 337, 339; 338, identifier:samples; 339, integer:0; 340, integer:1000; 341, expression_statement; 341, 342; 342, call; 342, 343; 342, 348; 343, attribute; 343, 344; 343, 347; 344, attribute; 344, 345; 344, 346; 345, identifier:f; 346, identifier:bin; 347, identifier:update; 348, argument_list; 348, 349; 348, 352; 348, 355; 348, 358; 348, 361; 348, 367; 348, 373; 348, 381; 349, keyword_argument; 349, 350; 349, 351; 350, identifier:ntrpr; 351, identifier:tracecount; 352, keyword_argument; 352, 353; 352, 354; 353, identifier:nart; 354, identifier:tracecount; 355, keyword_argument; 355, 356; 355, 357; 356, identifier:hdt; 357, identifier:interval; 358, keyword_argument; 358, 359; 358, 360; 359, identifier:dto; 360, identifier:interval; 361, keyword_argument; 361, 362; 361, 363; 362, identifier:hns; 363, call; 363, 364; 363, 365; 364, identifier:len; 365, argument_list; 365, 366; 366, identifier:samples; 367, keyword_argument; 367, 368; 367, 369; 368, identifier:nso; 369, call; 369, 370; 369, 371; 370, identifier:len; 371, argument_list; 371, 372; 372, identifier:samples; 373, keyword_argument; 373, 374; 373, 375; 374, identifier:format; 375, call; 375, 376; 375, 377; 376, identifier:int; 377, argument_list; 377, 378; 378, attribute; 378, 379; 378, 380; 379, identifier:spec; 380, identifier:format; 381, keyword_argument; 381, 382; 381, 383; 382, identifier:exth; 383, identifier:ext_headers; 384, return_statement; 384, 385; 385, identifier:f
def create(filename, spec): """Create a new segy file. Create a new segy file with the geometry and properties given by `spec`. This enables creating SEGY files from your data. The created file supports all segyio modes, but has an emphasis on writing. The spec must be complete, otherwise an exception will be raised. A default, empty spec can be created with ``segyio.spec()``. Very little data is written to the file, so just calling create is not sufficient to re-read the file with segyio. Rather, every trace header and trace must be written to the file to be considered complete. Create should be used together with python's ``with`` statement. This ensure the data is written. Please refer to the examples. The ``segyio.spec()`` function will default sorting, offsets and everything in the mandatory group, except format and samples, and requires the caller to fill in *all* the fields in either of the exclusive groups. If any field is missing from the first exclusive group, and the tracecount is set, the resulting file will be considered unstructured. If the tracecount is set, and all fields of the first exclusive group are specified, the file is considered structured and the tracecount is inferred from the xlines/ilines/offsets. The offsets are defaulted to ``[1]`` by ``segyio.spec()``. Parameters ---------- filename : str Path to file to create spec : segyio.spec Structure of the segy file Returns ------- file : segyio.SegyFile An open segyio file handle, similar to that returned by `segyio.open` See also -------- segyio.spec : template for the `spec` argument Notes ----- .. versionadded:: 1.1 .. versionchanged:: 1.4 Support for creating unstructured files .. versionchanged:: 1.8 Support for creating lsb files The ``spec`` is any object that has the following attributes Mandatory:: iline : int or segyio.BinField xline : int or segyio.BinField samples : array of int format : { 1, 5 } 1 = IBM float, 5 = IEEE float Exclusive:: ilines : array_like of int xlines : array_like of int offsets : array_like of int sorting : int or segyio.TraceSortingFormat OR tracecount : int Optional:: ext_headers : int endian : str { 'big', 'msb', 'little', 'lsb' } defaults to 'big' Examples -------- Create a file: >>> spec = segyio.spec() >>> spec.ilines = [1, 2, 3, 4] >>> spec.xlines = [11, 12, 13] >>> spec.samples = list(range(50)) >>> spec.sorting = 2 >>> spec.format = 1 >>> with segyio.create(path, spec) as f: ... ## fill the file with data ... pass ... Copy a file, but shorten all traces by 50 samples: >>> with segyio.open(srcpath) as src: ... spec = segyio.spec() ... spec.sorting = src.sorting ... spec.format = src.format ... spec.samples = src.samples[:len(src.samples) - 50] ... spec.ilines = src.ilines ... spec.xline = src.xlines ... with segyio.create(dstpath, spec) as dst: ... dst.text[0] = src.text[0] ... dst.bin = src.bin ... dst.header = src.header ... dst.trace = src.trace Copy a file, but shift samples time by 50: >>> with segyio.open(srcpath) as src: ... delrt = 50 ... spec = segyio.spec() ... spec.samples = src.samples + delrt ... spec.ilines = src.ilines ... spec.xline = src.xlines ... with segyio.create(dstpath, spec) as dst: ... dst.text[0] = src.text[0] ... dst.bin = src.bin ... dst.header = src.header ... dst.header = { TraceField.DelayRecordingTime: delrt } ... dst.trace = src.trace Copy a file, but shorten all traces by 50 samples (since v1.4): >>> with segyio.open(srcpath) as src: ... spec = segyio.tools.metadata(src) ... spec.samples = spec.samples[:len(spec.samples) - 50] ... with segyio.create(dstpath, spec) as dst: ... dst.text[0] = src.text[0] ... dst.bin = src.bin ... dst.header = src.header ... dst.trace = src.trace """ from . import _segyio if not structured(spec): tracecount = spec.tracecount else: tracecount = len(spec.ilines) * len(spec.xlines) * len(spec.offsets) ext_headers = spec.ext_headers if hasattr(spec, 'ext_headers') else 0 samples = numpy.asarray(spec.samples) endians = { 'lsb': 256, # (1 << 8) 'little': 256, 'msb': 0, 'big': 0, } endian = spec.endian if hasattr(spec, 'endian') else 'big' if endian is None: endian = 'big' if endian not in endians: problem = 'unknown endianness {}, expected one of: ' opts = ' '.join(endians.keys()) raise ValueError(problem.format(endian) + opts) fd = _segyio.segyiofd(str(filename), 'w+', endians[endian]) fd.segymake( samples = len(samples), tracecount = tracecount, format = int(spec.format), ext_headers = int(ext_headers), ) f = segyio.SegyFile(fd, filename = str(filename), mode = 'w+', iline = int(spec.iline), xline = int(spec.xline), endian = endian, ) f._samples = samples if structured(spec): sorting = spec.sorting if hasattr(spec, 'sorting') else None if sorting is None: sorting = TraceSortingFormat.INLINE_SORTING f.interpret(spec.ilines, spec.xlines, spec.offsets, sorting) f.text[0] = default_text_header(f._il, f._xl, segyio.TraceField.offset) if len(samples) == 1: interval = int(samples[0] * 1000) else: interval = int((samples[1] - samples[0]) * 1000) f.bin.update( ntrpr = tracecount, nart = tracecount, hdt = interval, dto = interval, hns = len(samples), nso = len(samples), format = int(spec.format), exth = ext_headers, ) return f
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 8; 2, function_name:rotation; 3, parameters; 3, 4; 3, 5; 4, identifier:f; 5, default_parameter; 5, 6; 5, 7; 6, identifier:line; 7, string:'fast'; 8, block; 8, 9; 8, 11; 8, 21; 8, 45; 8, 88; 8, 94; 8, 113; 8, 133; 8, 171; 9, expression_statement; 9, 10; 10, comment; 11, if_statement; 11, 12; 11, 15; 12, attribute; 12, 13; 12, 14; 13, identifier:f; 14, identifier:unstructured; 15, block; 15, 16; 16, raise_statement; 16, 17; 17, call; 17, 18; 17, 19; 18, identifier:ValueError; 19, argument_list; 19, 20; 20, string:"Rotation requires a structured file"; 21, expression_statement; 21, 22; 22, assignment; 22, 23; 22, 24; 23, identifier:lines; 24, dictionary; 24, 25; 24, 30; 24, 35; 24, 40; 25, pair; 25, 26; 25, 27; 26, string:'fast'; 27, attribute; 27, 28; 27, 29; 28, identifier:f; 29, identifier:fast; 30, pair; 30, 31; 30, 32; 31, string:'slow'; 32, attribute; 32, 33; 32, 34; 33, identifier:f; 34, identifier:slow; 35, pair; 35, 36; 35, 37; 36, string:'iline'; 37, attribute; 37, 38; 37, 39; 38, identifier:f; 39, identifier:iline; 40, pair; 40, 41; 40, 42; 41, string:'xline'; 42, attribute; 42, 43; 42, 44; 43, identifier:f; 44, identifier:xline; 45, if_statement; 45, 46; 45, 49; 46, comparison_operator:not; 46, 47; 46, 48; 47, identifier:line; 48, identifier:lines; 49, block; 49, 50; 49, 59; 49, 77; 50, expression_statement; 50, 51; 51, assignment; 51, 52; 51, 53; 52, identifier:error; 53, call; 53, 54; 53, 57; 54, attribute; 54, 55; 54, 56; 55, string:"Unknown line {}"; 56, identifier:format; 57, argument_list; 57, 58; 58, identifier:line; 59, expression_statement; 59, 60; 60, assignment; 60, 61; 60, 62; 61, identifier:solution; 62, call; 62, 63; 62, 66; 63, attribute; 63, 64; 63, 65; 64, string:"Must be any of: {}"; 65, identifier:format; 66, argument_list; 66, 67; 67, call; 67, 68; 67, 71; 68, attribute; 68, 69; 68, 70; 69, string:' '; 70, identifier:join; 71, argument_list; 71, 72; 72, call; 72, 73; 72, 76; 73, attribute; 73, 74; 73, 75; 74, identifier:lines; 75, identifier:keys; 76, argument_list; 77, raise_statement; 77, 78; 78, call; 78, 79; 78, 80; 79, identifier:ValueError; 80, argument_list; 80, 81; 81, call; 81, 82; 81, 85; 82, attribute; 82, 83; 82, 84; 83, string:'{} {}'; 84, identifier:format; 85, argument_list; 85, 86; 85, 87; 86, identifier:error; 87, identifier:solution; 88, expression_statement; 88, 89; 89, assignment; 89, 90; 89, 91; 90, identifier:l; 91, subscript; 91, 92; 91, 93; 92, identifier:lines; 93, identifier:line; 94, expression_statement; 94, 95; 95, assignment; 95, 96; 95, 97; 96, identifier:origin; 97, subscript; 97, 98; 97, 103; 97, 108; 98, subscript; 98, 99; 98, 102; 99, attribute; 99, 100; 99, 101; 100, identifier:f; 101, identifier:header; 102, integer:0; 103, attribute; 103, 104; 103, 107; 104, attribute; 104, 105; 104, 106; 105, identifier:segyio; 106, identifier:su; 107, identifier:cdpx; 108, attribute; 108, 109; 108, 112; 109, attribute; 109, 110; 109, 111; 110, identifier:segyio; 111, identifier:su; 112, identifier:cdpy; 113, expression_statement; 113, 114; 114, assignment; 114, 115; 114, 118; 115, pattern_list; 115, 116; 115, 117; 116, identifier:cdpx; 117, identifier:cdpy; 118, expression_list; 118, 119; 118, 126; 119, subscript; 119, 120; 119, 121; 120, identifier:origin; 121, attribute; 121, 122; 121, 125; 122, attribute; 122, 123; 122, 124; 123, identifier:segyio; 124, identifier:su; 125, identifier:cdpx; 126, subscript; 126, 127; 126, 128; 127, identifier:origin; 128, attribute; 128, 129; 128, 132; 129, attribute; 129, 130; 129, 131; 130, identifier:segyio; 131, identifier:su; 132, identifier:cdpy; 133, expression_statement; 133, 134; 134, assignment; 134, 135; 134, 136; 135, identifier:rot; 136, call; 136, 137; 136, 142; 137, attribute; 137, 138; 137, 141; 138, attribute; 138, 139; 138, 140; 139, identifier:f; 140, identifier:xfd; 141, identifier:rotation; 142, argument_list; 142, 143; 142, 147; 142, 150; 142, 156; 143, call; 143, 144; 143, 145; 144, identifier:len; 145, argument_list; 145, 146; 146, identifier:l; 147, attribute; 147, 148; 147, 149; 148, identifier:l; 149, identifier:stride; 150, call; 150, 151; 150, 152; 151, identifier:len; 152, argument_list; 152, 153; 153, attribute; 153, 154; 153, 155; 154, identifier:f; 155, identifier:offsets; 156, call; 156, 157; 156, 160; 157, attribute; 157, 158; 157, 159; 158, identifier:np; 159, identifier:fromiter; 160, argument_list; 160, 161; 160, 166; 161, call; 161, 162; 161, 165; 162, attribute; 162, 163; 162, 164; 163, identifier:l; 164, identifier:keys; 165, argument_list; 166, keyword_argument; 166, 167; 166, 168; 167, identifier:dtype; 168, attribute; 168, 169; 168, 170; 169, identifier:np; 170, identifier:intc; 171, return_statement; 171, 172; 172, expression_list; 172, 173; 172, 174; 172, 175; 173, identifier:rot; 174, identifier:cdpx; 175, identifier:cdpy
def rotation(f, line = 'fast'): """ Find rotation of the survey Find the clock-wise rotation and origin of `line` as ``(rot, cdpx, cdpy)`` The clock-wise rotation is defined as the angle in radians between line given by the first and last trace of the first line and the axis that gives increasing CDP-Y, in the direction that gives increasing CDP-X. By default, the first line is the 'fast' direction, which is inlines if the file is inline sorted, and crossline if it's crossline sorted. Parameters ---------- f : SegyFile line : { 'fast', 'slow', 'iline', 'xline' } Returns ------- rotation : float cdpx : int cdpy : int Notes ----- .. versionadded:: 1.2 """ if f.unstructured: raise ValueError("Rotation requires a structured file") lines = { 'fast': f.fast, 'slow': f.slow, 'iline': f.iline, 'xline': f.xline, } if line not in lines: error = "Unknown line {}".format(line) solution = "Must be any of: {}".format(' '.join(lines.keys())) raise ValueError('{} {}'.format(error, solution)) l = lines[line] origin = f.header[0][segyio.su.cdpx, segyio.su.cdpy] cdpx, cdpy = origin[segyio.su.cdpx], origin[segyio.su.cdpy] rot = f.xfd.rotation( len(l), l.stride, len(f.offsets), np.fromiter(l.keys(), dtype = np.intc) ) return rot, cdpx, cdpy
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 23; 2, function_name:from_array3D; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 3, 12; 3, 17; 3, 20; 4, identifier:filename; 5, identifier:data; 6, default_parameter; 6, 7; 6, 8; 7, identifier:iline; 8, integer:189; 9, default_parameter; 9, 10; 9, 11; 10, identifier:xline; 11, integer:193; 12, default_parameter; 12, 13; 12, 14; 13, identifier:format; 14, attribute; 14, 15; 14, 16; 15, identifier:SegySampleFormat; 16, identifier:IBM_FLOAT_4_BYTE; 17, default_parameter; 17, 18; 17, 19; 18, identifier:dt; 19, integer:4000; 20, default_parameter; 20, 21; 20, 22; 21, identifier:delrt; 22, integer:0; 23, block; 23, 24; 23, 26; 23, 35; 23, 44; 23, 63; 24, expression_statement; 24, 25; 25, comment; 26, expression_statement; 26, 27; 27, assignment; 27, 28; 27, 29; 28, identifier:data; 29, call; 29, 30; 29, 33; 30, attribute; 30, 31; 30, 32; 31, identifier:np; 32, identifier:asarray; 33, argument_list; 33, 34; 34, identifier:data; 35, expression_statement; 35, 36; 36, assignment; 36, 37; 36, 38; 37, identifier:dimensions; 38, call; 38, 39; 38, 40; 39, identifier:len; 40, argument_list; 40, 41; 41, attribute; 41, 42; 41, 43; 42, identifier:data; 43, identifier:shape; 44, if_statement; 44, 45; 44, 48; 45, comparison_operator:!=; 45, 46; 45, 47; 46, identifier:dimensions; 47, integer:3; 48, block; 48, 49; 48, 58; 49, expression_statement; 49, 50; 50, assignment; 50, 51; 50, 52; 51, identifier:problem; 52, call; 52, 53; 52, 56; 53, attribute; 53, 54; 53, 55; 54, string:"Expected 3 dimensions, {} was given"; 55, identifier:format; 56, argument_list; 56, 57; 57, identifier:dimensions; 58, raise_statement; 58, 59; 59, call; 59, 60; 59, 61; 60, identifier:ValueError; 61, argument_list; 61, 62; 62, identifier:problem; 63, expression_statement; 63, 64; 64, call; 64, 65; 64, 66; 65, identifier:from_array; 66, argument_list; 66, 67; 66, 68; 66, 69; 66, 72; 66, 75; 66, 78; 66, 81; 67, identifier:filename; 68, identifier:data; 69, keyword_argument; 69, 70; 69, 71; 70, identifier:iline; 71, identifier:iline; 72, keyword_argument; 72, 73; 72, 74; 73, identifier:xline; 74, identifier:xline; 75, keyword_argument; 75, 76; 75, 77; 76, identifier:format; 77, identifier:format; 78, keyword_argument; 78, 79; 78, 80; 79, identifier:dt; 80, identifier:dt; 81, keyword_argument; 81, 82; 81, 83; 82, identifier:delrt; 83, identifier:delrt
def from_array3D(filename, data, iline=189, xline=193, format=SegySampleFormat.IBM_FLOAT_4_BYTE, dt=4000, delrt=0): """ Create a new SEGY file from a 3D array Create an structured SEGY file with defaulted headers from a 3-dimensional array. The file is inline-sorted. ilines, xlines and samples are inferred from the array. Structure-defining fields in the binary header and in the traceheaders are set accordingly. Such fields include, but are not limited to iline, xline and offset. The file also contains a defaulted textual header. The 3-dimensional array is interpreted as:: xl0 xl1 xl2 ----------------- / | tr0 | tr1 | tr2 | il0 ----------------- | / | tr3 | tr4 | tr5 | il1 ----------------- | / | tr6 | tr7 | tr8 | il2 ----------------- | / / / / n-samples ------------------ ilines = [1, len(axis(0) + 1] xlines = [1, len(axis(1) + 1] samples = [0, len(axis(2)] Parameters ---------- filename : string-like Path to new file data : 3-dimensional array-like iline : int or segyio.TraceField Inline number field in the trace headers. Defaults to 189 as per the SEG-Y rev1 specification xline : int or segyio.TraceField Crossline number field in the trace headers. Defaults to 193 as per the SEG-Y rev1 specification format : int or segyio.SegySampleFormat Sample format field in the trace header. Defaults to IBM float 4 byte dt : int-like sample interval delrt : int-like Notes ----- .. versionadded:: 1.8 Examples -------- Create a file from a 3D array, open it and read an iline: >>> segyio.tools.from_array3D(path, array3d) >>> segyio.open(path, mode) as f: ... iline = f.iline[0] ... """ data = np.asarray(data) dimensions = len(data.shape) if dimensions != 3: problem = "Expected 3 dimensions, {} was given".format(dimensions) raise ValueError(problem) from_array(filename, data, iline=iline, xline=xline, format=format, dt=dt, delrt=delrt)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 23; 2, function_name:open; 3, parameters; 3, 4; 3, 5; 3, 8; 3, 11; 3, 14; 3, 17; 3, 20; 4, identifier:filename; 5, default_parameter; 5, 6; 5, 7; 6, identifier:mode; 7, string:"r"; 8, default_parameter; 8, 9; 8, 10; 9, identifier:iline; 10, integer:189; 11, default_parameter; 11, 12; 11, 13; 12, identifier:xline; 13, integer:193; 14, default_parameter; 14, 15; 14, 16; 15, identifier:strict; 16, True; 17, default_parameter; 17, 18; 17, 19; 18, identifier:ignore_geometry; 19, False; 20, default_parameter; 20, 21; 20, 22; 21, identifier:endian; 22, string:'big'; 23, block; 23, 24; 23, 26; 23, 51; 23, 68; 23, 102; 23, 107; 23, 123; 23, 129; 23, 137; 23, 164; 23, 227; 23, 232; 24, expression_statement; 24, 25; 25, comment; 26, if_statement; 26, 27; 26, 30; 27, comparison_operator:in; 27, 28; 27, 29; 28, string:'w'; 29, identifier:mode; 30, block; 30, 31; 30, 35; 30, 39; 31, expression_statement; 31, 32; 32, assignment; 32, 33; 32, 34; 33, identifier:problem; 34, string:'w in mode would truncate the file'; 35, expression_statement; 35, 36; 36, assignment; 36, 37; 36, 38; 37, identifier:solution; 38, string:'use r+ to open in read-write'; 39, raise_statement; 39, 40; 40, call; 40, 41; 40, 42; 41, identifier:ValueError; 42, argument_list; 42, 43; 43, call; 43, 44; 43, 47; 44, attribute; 44, 45; 44, 46; 45, string:', '; 46, identifier:join; 47, argument_list; 47, 48; 48, tuple; 48, 49; 48, 50; 49, identifier:problem; 50, identifier:solution; 51, expression_statement; 51, 52; 52, assignment; 52, 53; 52, 54; 53, identifier:endians; 54, dictionary; 54, 55; 54, 58; 54, 59; 54, 62; 54, 65; 55, pair; 55, 56; 55, 57; 56, string:'little'; 57, integer:256; 58, comment; 59, pair; 59, 60; 59, 61; 60, string:'lsb'; 61, integer:256; 62, pair; 62, 63; 62, 64; 63, string:'big'; 64, integer:0; 65, pair; 65, 66; 65, 67; 66, string:'msb'; 67, integer:0; 68, if_statement; 68, 69; 68, 72; 69, comparison_operator:not; 69, 70; 69, 71; 70, identifier:endian; 71, identifier:endians; 72, block; 72, 73; 72, 77; 72, 90; 73, expression_statement; 73, 74; 74, assignment; 74, 75; 74, 76; 75, identifier:problem; 76, string:'unknown endianness {}, expected one of: '; 77, expression_statement; 77, 78; 78, assignment; 78, 79; 78, 80; 79, identifier:opts; 80, call; 80, 81; 80, 84; 81, attribute; 81, 82; 81, 83; 82, string:' '; 83, identifier:join; 84, argument_list; 84, 85; 85, call; 85, 86; 85, 89; 86, attribute; 86, 87; 86, 88; 87, identifier:endians; 88, identifier:keys; 89, argument_list; 90, raise_statement; 90, 91; 91, call; 91, 92; 91, 93; 92, identifier:ValueError; 93, argument_list; 93, 94; 94, binary_operator:+; 94, 95; 94, 101; 95, call; 95, 96; 95, 99; 96, attribute; 96, 97; 96, 98; 97, identifier:problem; 98, identifier:format; 99, argument_list; 99, 100; 100, identifier:endian; 101, identifier:opts; 102, import_from_statement; 102, 103; 102, 105; 103, relative_import; 103, 104; 104, import_prefix; 105, dotted_name; 105, 106; 106, identifier:_segyio; 107, expression_statement; 107, 108; 108, assignment; 108, 109; 108, 110; 109, identifier:fd; 110, call; 110, 111; 110, 114; 111, attribute; 111, 112; 111, 113; 112, identifier:_segyio; 113, identifier:segyiofd; 114, argument_list; 114, 115; 114, 119; 114, 120; 115, call; 115, 116; 115, 117; 116, identifier:str; 117, argument_list; 117, 118; 118, identifier:filename; 119, identifier:mode; 120, subscript; 120, 121; 120, 122; 121, identifier:endians; 122, identifier:endian; 123, expression_statement; 123, 124; 124, call; 124, 125; 124, 128; 125, attribute; 125, 126; 125, 127; 126, identifier:fd; 127, identifier:segyopen; 128, argument_list; 129, expression_statement; 129, 130; 130, assignment; 130, 131; 130, 132; 131, identifier:metrics; 132, call; 132, 133; 132, 136; 133, attribute; 133, 134; 133, 135; 134, identifier:fd; 135, identifier:metrics; 136, argument_list; 137, expression_statement; 137, 138; 138, assignment; 138, 139; 138, 140; 139, identifier:f; 140, call; 140, 141; 140, 144; 141, attribute; 141, 142; 141, 143; 142, identifier:segyio; 143, identifier:SegyFile; 144, argument_list; 144, 145; 144, 146; 144, 152; 144, 155; 144, 158; 144, 161; 145, identifier:fd; 146, keyword_argument; 146, 147; 146, 148; 147, identifier:filename; 148, call; 148, 149; 148, 150; 149, identifier:str; 150, argument_list; 150, 151; 151, identifier:filename; 152, keyword_argument; 152, 153; 152, 154; 153, identifier:mode; 154, identifier:mode; 155, keyword_argument; 155, 156; 155, 157; 156, identifier:iline; 157, identifier:iline; 158, keyword_argument; 158, 159; 158, 160; 159, identifier:xline; 160, identifier:xline; 161, keyword_argument; 161, 162; 161, 163; 162, identifier:endian; 163, identifier:endian; 164, try_statement; 164, 165; 164, 218; 165, block; 165, 166; 165, 182; 165, 196; 165, 202; 166, expression_statement; 166, 167; 167, assignment; 167, 168; 167, 169; 168, identifier:dt; 169, binary_operator:/; 169, 170; 169, 181; 170, call; 170, 171; 170, 176; 171, attribute; 171, 172; 171, 175; 172, attribute; 172, 173; 172, 174; 173, identifier:segyio; 174, identifier:tools; 175, identifier:dt; 176, argument_list; 176, 177; 176, 178; 177, identifier:f; 178, keyword_argument; 178, 179; 178, 180; 179, identifier:fallback_dt; 180, float:4000.0; 181, float:1000.0; 182, expression_statement; 182, 183; 183, assignment; 183, 184; 183, 185; 184, identifier:t0; 185, subscript; 185, 186; 185, 191; 186, subscript; 186, 187; 186, 190; 187, attribute; 187, 188; 187, 189; 188, identifier:f; 189, identifier:header; 190, integer:0; 191, attribute; 191, 192; 191, 195; 192, attribute; 192, 193; 192, 194; 193, identifier:segyio; 194, identifier:TraceField; 195, identifier:DelayRecordingTime; 196, expression_statement; 196, 197; 197, assignment; 197, 198; 197, 199; 198, identifier:samples; 199, subscript; 199, 200; 199, 201; 200, identifier:metrics; 201, string:'samplecount'; 202, expression_statement; 202, 203; 203, assignment; 203, 204; 203, 207; 204, attribute; 204, 205; 204, 206; 205, identifier:f; 206, identifier:_samples; 207, binary_operator:+; 207, 208; 207, 217; 208, parenthesized_expression; 208, 209; 209, binary_operator:*; 209, 210; 209, 216; 210, call; 210, 211; 210, 214; 211, attribute; 211, 212; 211, 213; 212, identifier:numpy; 213, identifier:arange; 214, argument_list; 214, 215; 215, identifier:samples; 216, identifier:dt; 217, identifier:t0; 218, except_clause; 218, 219; 219, block; 219, 220; 219, 226; 220, expression_statement; 220, 221; 221, call; 221, 222; 221, 225; 222, attribute; 222, 223; 222, 224; 223, identifier:f; 224, identifier:close; 225, argument_list; 226, raise_statement; 227, if_statement; 227, 228; 227, 229; 228, identifier:ignore_geometry; 229, block; 229, 230; 230, return_statement; 230, 231; 231, identifier:f; 232, return_statement; 232, 233; 233, call; 233, 234; 233, 235; 234, identifier:infer_geometry; 235, argument_list; 235, 236; 235, 237; 235, 238; 235, 239; 235, 240; 236, identifier:f; 237, identifier:metrics; 238, identifier:iline; 239, identifier:xline; 240, identifier:strict
def open(filename, mode="r", iline = 189, xline = 193, strict = True, ignore_geometry = False, endian = 'big'): """Open a segy file. Opens a segy file and tries to figure out its sorting, inline numbers, crossline numbers, and offsets, and enables reading and writing to this file in a simple manner. For reading, the access mode `r` is preferred. All write operations will raise an exception. For writing, the mode `r+` is preferred (as `rw` would truncate the file). Any mode with `w` will raise an error. The modes used are standard C file modes; please refer to that documentation for a complete reference. Open should be used together with python's ``with`` statement. Please refer to the examples. When the ``with`` statement is used the file will automatically be closed when the routine completes or an exception is raised. By default, segyio tries to open in ``strict`` mode. This means the file will be assumed to represent a geometry with consistent inline, crosslines and offsets. If strict is False, segyio will still try to establish a geometry, but it won't abort if it fails. When in non-strict mode is opened, geometry-dependent modes such as iline will raise an error. If ``ignore_geometry=True``, segyio will *not* try to build iline/xline or other geometry related structures, which leads to faster opens. This is essentially the same as using ``strict=False`` on a file that has no geometry. Parameters ---------- filename : str Path to file to open mode : {'r', 'r+'} File access mode, read-only ('r', default) or read-write ('r+') iline : int or segyio.TraceField Inline number field in the trace headers. Defaults to 189 as per the SEG-Y rev1 specification xline : int or segyio.TraceField Crossline number field in the trace headers. Defaults to 193 as per the SEG-Y rev1 specification strict : bool, optional Abort if a geometry cannot be inferred. Defaults to True. ignore_geometry : bool, optional Opt out on building geometry information, useful for e.g. shot organised files. Defaults to False. endian : {'big', 'msb', 'little', 'lsb'} File endianness, big/msb (default) or little/lsb Returns ------- file : segyio.SegyFile An open segyio file handle Raises ------ ValueError If the mode string contains 'w', as it would truncate the file Notes ----- .. versionadded:: 1.1 .. versionchanged:: 1.8 endian argument When a file is opened non-strict, only raw traces access is allowed, and using modes such as ``iline`` raise an error. Examples -------- Open a file in read-only mode: >>> with segyio.open(path, "r") as f: ... print(f.ilines) ... [1, 2, 3, 4, 5] Open a file in read-write mode: >>> with segyio.open(path, "r+") as f: ... f.trace = np.arange(100) Open two files at once: >>> with segyio.open(path) as src, segyio.open(path, "r+") as dst: ... dst.trace = src.trace # copy all traces from src to dst Open a file little-endian file: >>> with segyio.open(path, endian = 'little') as f: ... f.trace[0] """ if 'w' in mode: problem = 'w in mode would truncate the file' solution = 'use r+ to open in read-write' raise ValueError(', '.join((problem, solution))) endians = { 'little': 256, # (1 << 8) 'lsb': 256, 'big': 0, 'msb': 0, } if endian not in endians: problem = 'unknown endianness {}, expected one of: ' opts = ' '.join(endians.keys()) raise ValueError(problem.format(endian) + opts) from . import _segyio fd = _segyio.segyiofd(str(filename), mode, endians[endian]) fd.segyopen() metrics = fd.metrics() f = segyio.SegyFile(fd, filename = str(filename), mode = mode, iline = iline, xline = xline, endian = endian, ) try: dt = segyio.tools.dt(f, fallback_dt = 4000.0) / 1000.0 t0 = f.header[0][segyio.TraceField.DelayRecordingTime] samples = metrics['samplecount'] f._samples = (numpy.arange(samples) * dt) + t0 except: f.close() raise if ignore_geometry: return f return infer_geometry(f, metrics, iline, xline, strict)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:calc_min_interval; 3, parameters; 3, 4; 3, 5; 4, identifier:x; 5, identifier:alpha; 6, block; 6, 7; 6, 9; 6, 16; 6, 22; 6, 36; 6, 42; 6, 56; 6, 73; 6, 82; 6, 88; 6, 96; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:n; 12, call; 12, 13; 12, 14; 13, identifier:len; 14, argument_list; 14, 15; 15, identifier:x; 16, expression_statement; 16, 17; 17, assignment; 17, 18; 17, 19; 18, identifier:cred_mass; 19, binary_operator:-; 19, 20; 19, 21; 20, float:1.0; 21, identifier:alpha; 22, expression_statement; 22, 23; 23, assignment; 23, 24; 23, 25; 24, identifier:interval_idx_inc; 25, call; 25, 26; 25, 27; 26, identifier:int; 27, argument_list; 27, 28; 28, call; 28, 29; 28, 32; 29, attribute; 29, 30; 29, 31; 30, identifier:np; 31, identifier:floor; 32, argument_list; 32, 33; 33, binary_operator:*; 33, 34; 33, 35; 34, identifier:cred_mass; 35, identifier:n; 36, expression_statement; 36, 37; 37, assignment; 37, 38; 37, 39; 38, identifier:n_intervals; 39, binary_operator:-; 39, 40; 39, 41; 40, identifier:n; 41, identifier:interval_idx_inc; 42, expression_statement; 42, 43; 43, assignment; 43, 44; 43, 45; 44, identifier:interval_width; 45, binary_operator:-; 45, 46; 45, 51; 46, subscript; 46, 47; 46, 48; 47, identifier:x; 48, slice; 48, 49; 48, 50; 49, identifier:interval_idx_inc; 50, colon; 51, subscript; 51, 52; 51, 53; 52, identifier:x; 53, slice; 53, 54; 53, 55; 54, colon; 55, identifier:n_intervals; 56, if_statement; 56, 57; 56, 63; 57, comparison_operator:==; 57, 58; 57, 62; 58, call; 58, 59; 58, 60; 59, identifier:len; 60, argument_list; 60, 61; 61, identifier:interval_width; 62, integer:0; 63, block; 63, 64; 63, 69; 64, expression_statement; 64, 65; 65, call; 65, 66; 65, 67; 66, identifier:print_; 67, argument_list; 67, 68; 68, string:'Too few elements for interval calculation'; 69, return_statement; 69, 70; 70, list:[None, None]; 70, 71; 70, 72; 71, None; 72, None; 73, expression_statement; 73, 74; 74, assignment; 74, 75; 74, 76; 75, identifier:min_idx; 76, call; 76, 77; 76, 80; 77, attribute; 77, 78; 77, 79; 78, identifier:np; 79, identifier:argmin; 80, argument_list; 80, 81; 81, identifier:interval_width; 82, expression_statement; 82, 83; 83, assignment; 83, 84; 83, 85; 84, identifier:hdi_min; 85, subscript; 85, 86; 85, 87; 86, identifier:x; 87, identifier:min_idx; 88, expression_statement; 88, 89; 89, assignment; 89, 90; 89, 91; 90, identifier:hdi_max; 91, subscript; 91, 92; 91, 93; 92, identifier:x; 93, binary_operator:+; 93, 94; 93, 95; 94, identifier:min_idx; 95, identifier:interval_idx_inc; 96, return_statement; 96, 97; 97, list:[hdi_min, hdi_max]; 97, 98; 97, 99; 98, identifier:hdi_min; 99, identifier:hdi_max
def calc_min_interval(x, alpha): """Internal method to determine the minimum interval of a given width Assumes that x is sorted numpy array. """ n = len(x) cred_mass = 1.0 - alpha interval_idx_inc = int(np.floor(cred_mass * n)) n_intervals = n - interval_idx_inc interval_width = x[interval_idx_inc:] - x[:n_intervals] if len(interval_width) == 0: print_('Too few elements for interval calculation') return [None, None] min_idx = np.argmin(interval_width) hdi_min = x[min_idx] hdi_max = x[min_idx + interval_idx_inc] return [hdi_min, hdi_max]
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:_compute_gas_price; 3, parameters; 3, 4; 3, 5; 4, identifier:probabilities; 5, identifier:desired_probability; 6, block; 6, 7; 6, 9; 6, 15; 6, 22; 6, 50; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:first; 12, subscript; 12, 13; 12, 14; 13, identifier:probabilities; 14, integer:0; 15, expression_statement; 15, 16; 16, assignment; 16, 17; 16, 18; 17, identifier:last; 18, subscript; 18, 19; 18, 20; 19, identifier:probabilities; 20, unary_operator:-; 20, 21; 21, integer:1; 22, if_statement; 22, 23; 22, 28; 22, 36; 23, comparison_operator:>=; 23, 24; 23, 25; 24, identifier:desired_probability; 25, attribute; 25, 26; 25, 27; 26, identifier:first; 27, identifier:prob; 28, block; 28, 29; 29, return_statement; 29, 30; 30, call; 30, 31; 30, 32; 31, identifier:int; 32, argument_list; 32, 33; 33, attribute; 33, 34; 33, 35; 34, identifier:first; 35, identifier:gas_price; 36, elif_clause; 36, 37; 36, 42; 37, comparison_operator:<=; 37, 38; 37, 39; 38, identifier:desired_probability; 39, attribute; 39, 40; 39, 41; 40, identifier:last; 41, identifier:prob; 42, block; 42, 43; 43, return_statement; 43, 44; 44, call; 44, 45; 44, 46; 45, identifier:int; 46, argument_list; 46, 47; 47, attribute; 47, 48; 47, 49; 48, identifier:last; 49, identifier:gas_price; 50, for_statement; 50, 51; 50, 54; 50, 59; 50, 137; 51, pattern_list; 51, 52; 51, 53; 52, identifier:left; 53, identifier:right; 54, call; 54, 55; 54, 56; 55, identifier:sliding_window; 56, argument_list; 56, 57; 56, 58; 57, integer:2; 58, identifier:probabilities; 59, block; 59, 60; 59, 83; 59, 91; 59, 101; 59, 107; 59, 117; 59, 135; 60, if_statement; 60, 61; 60, 66; 60, 68; 61, comparison_operator:<; 61, 62; 61, 63; 62, identifier:desired_probability; 63, attribute; 63, 64; 63, 65; 64, identifier:right; 65, identifier:prob; 66, block; 66, 67; 67, continue_statement; 68, elif_clause; 68, 69; 68, 74; 68, 75; 68, 76; 68, 77; 69, comparison_operator:>; 69, 70; 69, 71; 70, identifier:desired_probability; 71, attribute; 71, 72; 71, 73; 72, identifier:left; 73, identifier:prob; 74, comment; 75, comment; 76, comment; 77, block; 77, 78; 78, raise_statement; 78, 79; 79, call; 79, 80; 79, 81; 80, identifier:Exception; 81, argument_list; 81, 82; 82, string:'Invariant'; 83, expression_statement; 83, 84; 84, assignment; 84, 85; 84, 86; 85, identifier:adj_prob; 86, binary_operator:-; 86, 87; 86, 88; 87, identifier:desired_probability; 88, attribute; 88, 89; 88, 90; 89, identifier:right; 90, identifier:prob; 91, expression_statement; 91, 92; 92, assignment; 92, 93; 92, 94; 93, identifier:window_size; 94, binary_operator:-; 94, 95; 94, 98; 95, attribute; 95, 96; 95, 97; 96, identifier:left; 97, identifier:prob; 98, attribute; 98, 99; 98, 100; 99, identifier:right; 100, identifier:prob; 101, expression_statement; 101, 102; 102, assignment; 102, 103; 102, 104; 103, identifier:position; 104, binary_operator:/; 104, 105; 104, 106; 105, identifier:adj_prob; 106, identifier:window_size; 107, expression_statement; 107, 108; 108, assignment; 108, 109; 108, 110; 109, identifier:gas_window_size; 110, binary_operator:-; 110, 111; 110, 114; 111, attribute; 111, 112; 111, 113; 112, identifier:left; 113, identifier:gas_price; 114, attribute; 114, 115; 114, 116; 115, identifier:right; 116, identifier:gas_price; 117, expression_statement; 117, 118; 118, assignment; 118, 119; 118, 120; 119, identifier:gas_price; 120, call; 120, 121; 120, 122; 121, identifier:int; 122, argument_list; 122, 123; 123, call; 123, 124; 123, 127; 124, attribute; 124, 125; 124, 126; 125, identifier:math; 126, identifier:ceil; 127, argument_list; 127, 128; 128, binary_operator:+; 128, 129; 128, 132; 129, attribute; 129, 130; 129, 131; 130, identifier:right; 131, identifier:gas_price; 132, binary_operator:*; 132, 133; 132, 134; 133, identifier:gas_window_size; 134, identifier:position; 135, return_statement; 135, 136; 136, identifier:gas_price; 137, else_clause; 137, 138; 137, 139; 137, 140; 137, 141; 137, 142; 137, 143; 137, 144; 137, 145; 137, 146; 138, comment; 139, comment; 140, comment; 141, comment; 142, comment; 143, comment; 144, comment; 145, comment; 146, block; 146, 147; 147, raise_statement; 147, 148; 148, call; 148, 149; 148, 150; 149, identifier:Exception; 150, argument_list; 150, 151; 151, string:'Invariant'
def _compute_gas_price(probabilities, desired_probability): """ Given a sorted range of ``Probability`` named-tuples returns a gas price computed based on where the ``desired_probability`` would fall within the range. :param probabilities: An iterable of `Probability` named-tuples sorted in reverse order. :param desired_probability: An floating point representation of the desired probability. (e.g. ``85% -> 0.85``) """ first = probabilities[0] last = probabilities[-1] if desired_probability >= first.prob: return int(first.gas_price) elif desired_probability <= last.prob: return int(last.gas_price) for left, right in sliding_window(2, probabilities): if desired_probability < right.prob: continue elif desired_probability > left.prob: # This code block should never be reachable as it would indicate # that we already passed by the probability window in which our # `desired_probability` is located. raise Exception('Invariant') adj_prob = desired_probability - right.prob window_size = left.prob - right.prob position = adj_prob / window_size gas_window_size = left.gas_price - right.gas_price gas_price = int(math.ceil(right.gas_price + gas_window_size * position)) return gas_price else: # The initial `if/else` clause in this function handles the case where # the `desired_probability` is either above or below the min/max # probability found in the `probabilities`. # # With these two cases handled, the only way this code block should be # reachable would be if the `probabilities` were not sorted correctly. # Otherwise, the `desired_probability` **must** fall between two of the # values in the `probabilities``. raise Exception('Invariant')
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 12; 2, function_name:generate_X_grid; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 4, identifier:self; 5, identifier:term; 6, default_parameter; 6, 7; 6, 8; 7, identifier:n; 8, integer:100; 9, default_parameter; 9, 10; 9, 11; 10, identifier:meshgrid; 11, False; 12, block; 12, 13; 12, 15; 12, 26; 12, 27; 12, 41; 12, 42; 13, expression_statement; 13, 14; 14, comment; 15, if_statement; 15, 16; 15, 20; 16, not_operator; 16, 17; 17, attribute; 17, 18; 17, 19; 18, identifier:self; 19, identifier:_is_fitted; 20, block; 20, 21; 21, raise_statement; 21, 22; 22, call; 22, 23; 22, 24; 23, identifier:AttributeError; 24, argument_list; 24, 25; 25, string:'GAM has not been fitted. Call fit first.'; 26, comment; 27, if_statement; 27, 28; 27, 35; 28, attribute; 28, 29; 28, 34; 29, subscript; 29, 30; 29, 33; 30, attribute; 30, 31; 30, 32; 31, identifier:self; 32, identifier:terms; 33, identifier:term; 34, identifier:isintercept; 35, block; 35, 36; 36, raise_statement; 36, 37; 37, call; 37, 38; 37, 39; 38, identifier:ValueError; 39, argument_list; 39, 40; 40, string:'cannot create grid for intercept term'; 41, comment; 42, if_statement; 42, 43; 42, 50; 42, 120; 42, 121; 42, 227; 42, 228; 43, attribute; 43, 44; 43, 49; 44, subscript; 44, 45; 44, 48; 45, attribute; 45, 46; 45, 47; 46, identifier:self; 47, identifier:terms; 48, identifier:term; 49, identifier:istensor; 50, block; 50, 51; 50, 55; 50, 87; 50, 100; 51, expression_statement; 51, 52; 52, assignment; 52, 53; 52, 54; 53, identifier:Xs; 54, list:[]; 55, for_statement; 55, 56; 55, 57; 55, 62; 56, identifier:term_; 57, subscript; 57, 58; 57, 61; 58, attribute; 58, 59; 58, 60; 59, identifier:self; 60, identifier:terms; 61, identifier:term; 62, block; 62, 63; 63, expression_statement; 63, 64; 64, call; 64, 65; 64, 68; 65, attribute; 65, 66; 65, 67; 66, identifier:Xs; 67, identifier:append; 68, argument_list; 68, 69; 69, call; 69, 70; 69, 73; 70, attribute; 70, 71; 70, 72; 71, identifier:np; 72, identifier:linspace; 73, argument_list; 73, 74; 73, 79; 73, 84; 74, subscript; 74, 75; 74, 78; 75, attribute; 75, 76; 75, 77; 76, identifier:term_; 77, identifier:edge_knots_; 78, integer:0; 79, subscript; 79, 80; 79, 83; 80, attribute; 80, 81; 80, 82; 81, identifier:term_; 82, identifier:edge_knots_; 83, integer:1; 84, keyword_argument; 84, 85; 84, 86; 85, identifier:num; 86, identifier:n; 87, expression_statement; 87, 88; 88, assignment; 88, 89; 88, 90; 89, identifier:Xs; 90, call; 90, 91; 90, 94; 91, attribute; 91, 92; 91, 93; 92, identifier:np; 93, identifier:meshgrid; 94, argument_list; 94, 95; 94, 97; 95, list_splat; 95, 96; 96, identifier:Xs; 97, keyword_argument; 97, 98; 97, 99; 98, identifier:indexing; 99, string:'ij'; 100, if_statement; 100, 101; 100, 102; 100, 108; 101, identifier:meshgrid; 102, block; 102, 103; 103, return_statement; 103, 104; 104, call; 104, 105; 104, 106; 105, identifier:tuple; 106, argument_list; 106, 107; 107, identifier:Xs; 108, else_clause; 108, 109; 109, block; 109, 110; 110, return_statement; 110, 111; 111, call; 111, 112; 111, 115; 112, attribute; 112, 113; 112, 114; 113, identifier:self; 114, identifier:_flatten_mesh; 115, argument_list; 115, 116; 115, 117; 116, identifier:Xs; 117, keyword_argument; 117, 118; 117, 119; 118, identifier:term; 119, identifier:term; 120, comment; 121, elif_clause; 121, 122; 121, 131; 122, call; 122, 123; 122, 124; 123, identifier:hasattr; 124, argument_list; 124, 125; 124, 130; 125, subscript; 125, 126; 125, 129; 126, attribute; 126, 127; 126, 128; 127, identifier:self; 128, identifier:terms; 129, identifier:term; 130, string:'edge_knots_'; 131, block; 131, 132; 131, 161; 131, 167; 131, 168; 131, 183; 131, 197; 131, 225; 132, expression_statement; 132, 133; 133, assignment; 133, 134; 133, 135; 134, identifier:x; 135, call; 135, 136; 135, 139; 136, attribute; 136, 137; 136, 138; 137, identifier:np; 138, identifier:linspace; 139, argument_list; 139, 140; 139, 149; 139, 158; 140, subscript; 140, 141; 140, 148; 141, attribute; 141, 142; 141, 147; 142, subscript; 142, 143; 142, 146; 143, attribute; 143, 144; 143, 145; 144, identifier:self; 145, identifier:terms; 146, identifier:term; 147, identifier:edge_knots_; 148, integer:0; 149, subscript; 149, 150; 149, 157; 150, attribute; 150, 151; 150, 156; 151, subscript; 151, 152; 151, 155; 152, attribute; 152, 153; 152, 154; 153, identifier:self; 154, identifier:terms; 155, identifier:term; 156, identifier:edge_knots_; 157, integer:1; 158, keyword_argument; 158, 159; 158, 160; 159, identifier:num; 160, identifier:n; 161, if_statement; 161, 162; 161, 163; 162, identifier:meshgrid; 163, block; 163, 164; 164, return_statement; 164, 165; 165, tuple; 165, 166; 166, identifier:x; 167, comment; 168, expression_statement; 168, 169; 169, assignment; 169, 170; 169, 171; 170, identifier:X; 171, call; 171, 172; 171, 175; 172, attribute; 172, 173; 172, 174; 173, identifier:np; 174, identifier:zeros; 175, argument_list; 175, 176; 176, tuple; 176, 177; 176, 178; 177, identifier:n; 178, subscript; 178, 179; 178, 182; 179, attribute; 179, 180; 179, 181; 180, identifier:self; 181, identifier:statistics_; 182, string:'m_features'; 183, expression_statement; 183, 184; 184, assignment; 184, 185; 184, 196; 185, subscript; 185, 186; 185, 187; 185, 189; 186, identifier:X; 187, slice; 187, 188; 188, colon; 189, attribute; 189, 190; 189, 195; 190, subscript; 190, 191; 190, 194; 191, attribute; 191, 192; 191, 193; 192, identifier:self; 193, identifier:terms; 194, identifier:term; 195, identifier:feature; 196, identifier:x; 197, if_statement; 197, 198; 197, 210; 198, comparison_operator:is; 198, 199; 198, 209; 199, call; 199, 200; 199, 201; 200, identifier:getattr; 201, argument_list; 201, 202; 201, 207; 201, 208; 202, subscript; 202, 203; 202, 206; 203, attribute; 203, 204; 203, 205; 204, identifier:self; 205, identifier:terms; 206, identifier:term; 207, string:'by'; 208, None; 209, None; 210, block; 210, 211; 211, expression_statement; 211, 212; 212, assignment; 212, 213; 212, 224; 213, subscript; 213, 214; 213, 215; 213, 217; 214, identifier:X; 215, slice; 215, 216; 216, colon; 217, attribute; 217, 218; 217, 223; 218, subscript; 218, 219; 218, 222; 219, attribute; 219, 220; 219, 221; 220, identifier:self; 221, identifier:terms; 222, identifier:term; 223, identifier:by; 224, float:1.; 225, return_statement; 225, 226; 226, identifier:X; 227, comment; 228, else_clause; 228, 229; 229, block; 229, 230; 230, raise_statement; 230, 231; 231, call; 231, 232; 231, 233; 232, identifier:TypeError; 233, argument_list; 233, 234; 234, call; 234, 235; 234, 238; 235, attribute; 235, 236; 235, 237; 236, string:'Unexpected term type: {}'; 237, identifier:format; 238, argument_list; 238, 239; 239, subscript; 239, 240; 239, 243; 240, attribute; 240, 241; 240, 242; 241, identifier:self; 242, identifier:terms; 243, identifier:term
def generate_X_grid(self, term, n=100, meshgrid=False): """create a nice grid of X data array is sorted by feature and uniformly spaced, so the marginal and joint distributions are likely wrong if term is >= 0, we generate n samples per feature, which results in n^deg samples, where deg is the degree of the interaction of the term Parameters ---------- term : int, Which term to process. n : int, optional number of data points to create meshgrid : bool, optional Whether to return a meshgrid (useful for 3d plotting) or a feature matrix (useful for inference like partial predictions) Returns ------- if meshgrid is False: np.array of shape (n, n_features) where m is the number of (sub)terms in the requested (tensor)term. else: tuple of len m, where m is the number of (sub)terms in the requested (tensor)term. each element in the tuple contains a np.ndarray of size (n)^m Raises ------ ValueError : If the term requested is an intercept since it does not make sense to process the intercept term. """ if not self._is_fitted: raise AttributeError('GAM has not been fitted. Call fit first.') # cant do Intercept if self.terms[term].isintercept: raise ValueError('cannot create grid for intercept term') # process each subterm in a TensorTerm if self.terms[term].istensor: Xs = [] for term_ in self.terms[term]: Xs.append(np.linspace(term_.edge_knots_[0], term_.edge_knots_[1], num=n)) Xs = np.meshgrid(*Xs, indexing='ij') if meshgrid: return tuple(Xs) else: return self._flatten_mesh(Xs, term=term) # all other Terms elif hasattr(self.terms[term], 'edge_knots_'): x = np.linspace(self.terms[term].edge_knots_[0], self.terms[term].edge_knots_[1], num=n) if meshgrid: return (x,) # fill in feature matrix with only relevant features for this term X = np.zeros((n, self.statistics_['m_features'])) X[:, self.terms[term].feature] = x if getattr(self.terms[term], 'by', None) is not None: X[:, self.terms[term].by] = 1. return X # dont know what to do here else: raise TypeError('Unexpected term type: {}'.format(self.terms[term]))
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:build_from_info; 3, parameters; 3, 4; 3, 5; 4, identifier:cls; 5, identifier:info; 6, block; 6, 7; 6, 9; 6, 13; 6, 31; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:terms; 12, list:[]; 13, for_statement; 13, 14; 13, 15; 13, 18; 14, identifier:term_info; 15, subscript; 15, 16; 15, 17; 16, identifier:info; 17, string:'terms'; 18, block; 18, 19; 19, expression_statement; 19, 20; 20, call; 20, 21; 20, 24; 21, attribute; 21, 22; 21, 23; 22, identifier:terms; 23, identifier:append; 24, argument_list; 24, 25; 25, call; 25, 26; 25, 29; 26, attribute; 26, 27; 26, 28; 27, identifier:SplineTerm; 28, identifier:build_from_info; 29, argument_list; 29, 30; 30, identifier:term_info; 31, return_statement; 31, 32; 32, call; 32, 33; 32, 34; 33, identifier:cls; 34, argument_list; 34, 35; 35, list_splat; 35, 36; 36, identifier:terms
def build_from_info(cls, info): """build a TensorTerm instance from a dict Parameters ---------- cls : class info : dict contains all information needed to build the term Return ------ TensorTerm instance """ terms = [] for term_info in info['terms']: terms.append(SplineTerm.build_from_info(term_info)) return cls(*terms)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 10; 2, function_name:sort_kate_imports; 3, parameters; 3, 4; 3, 7; 4, default_parameter; 4, 5; 4, 6; 5, identifier:add_imports; 6, tuple; 7, default_parameter; 7, 8; 7, 9; 8, identifier:remove_imports; 9, tuple; 10, block; 10, 11; 10, 13; 10, 21; 10, 29; 10, 37; 10, 45; 10, 92; 10, 101; 10, 116; 10, 180; 11, expression_statement; 11, 12; 12, comment; 13, expression_statement; 13, 14; 14, assignment; 14, 15; 14, 16; 15, identifier:document; 16, call; 16, 17; 16, 20; 17, attribute; 17, 18; 17, 19; 18, identifier:kate; 19, identifier:activeDocument; 20, argument_list; 21, expression_statement; 21, 22; 22, assignment; 22, 23; 22, 24; 23, identifier:view; 24, call; 24, 25; 24, 28; 25, attribute; 25, 26; 25, 27; 26, identifier:document; 27, identifier:activeView; 28, argument_list; 29, expression_statement; 29, 30; 30, assignment; 30, 31; 30, 32; 31, identifier:position; 32, call; 32, 33; 32, 36; 33, attribute; 33, 34; 33, 35; 34, identifier:view; 35, identifier:cursorPosition; 36, argument_list; 37, expression_statement; 37, 38; 38, assignment; 38, 39; 38, 40; 39, identifier:selection; 40, call; 40, 41; 40, 44; 41, attribute; 41, 42; 41, 43; 42, identifier:view; 43, identifier:selectionRange; 44, argument_list; 45, expression_statement; 45, 46; 46, assignment; 46, 47; 46, 48; 47, identifier:sorter; 48, call; 48, 49; 48, 50; 49, identifier:SortImports; 50, argument_list; 50, 51; 50, 58; 50, 61; 50, 64; 51, keyword_argument; 51, 52; 51, 53; 52, identifier:file_contents; 53, call; 53, 54; 53, 57; 54, attribute; 54, 55; 54, 56; 55, identifier:document; 56, identifier:text; 57, argument_list; 58, keyword_argument; 58, 59; 58, 60; 59, identifier:add_imports; 60, identifier:add_imports; 61, keyword_argument; 61, 62; 61, 63; 62, identifier:remove_imports; 63, identifier:remove_imports; 64, keyword_argument; 64, 65; 64, 66; 65, identifier:settings_path; 66, call; 66, 67; 66, 72; 67, attribute; 67, 68; 67, 71; 68, attribute; 68, 69; 68, 70; 69, identifier:os; 70, identifier:path; 71, identifier:dirname; 72, argument_list; 72, 73; 73, call; 73, 74; 73, 79; 74, attribute; 74, 75; 74, 78; 75, attribute; 75, 76; 75, 77; 76, identifier:os; 77, identifier:path; 78, identifier:abspath; 79, argument_list; 79, 80; 80, call; 80, 81; 80, 82; 81, identifier:str; 82, argument_list; 82, 83; 83, call; 83, 84; 83, 91; 84, attribute; 84, 85; 84, 90; 85, call; 85, 86; 85, 89; 86, attribute; 86, 87; 86, 88; 87, identifier:document; 88, identifier:url; 89, argument_list; 90, identifier:path; 91, argument_list; 92, expression_statement; 92, 93; 93, call; 93, 94; 93, 97; 94, attribute; 94, 95; 94, 96; 95, identifier:document; 96, identifier:setText; 97, argument_list; 97, 98; 98, attribute; 98, 99; 98, 100; 99, identifier:sorter; 100, identifier:output; 101, expression_statement; 101, 102; 102, call; 102, 103; 102, 106; 103, attribute; 103, 104; 103, 105; 104, identifier:position; 105, identifier:setLine; 106, argument_list; 106, 107; 107, binary_operator:+; 107, 108; 107, 113; 108, call; 108, 109; 108, 112; 109, attribute; 109, 110; 109, 111; 110, identifier:position; 111, identifier:line; 112, argument_list; 113, attribute; 113, 114; 113, 115; 114, identifier:sorter; 115, identifier:length_change; 116, if_statement; 116, 117; 116, 118; 117, identifier:selection; 118, block; 118, 119; 118, 127; 118, 142; 118, 150; 118, 165; 118, 173; 119, expression_statement; 119, 120; 120, assignment; 120, 121; 120, 122; 121, identifier:start; 122, call; 122, 123; 122, 126; 123, attribute; 123, 124; 123, 125; 124, identifier:selection; 125, identifier:start; 126, argument_list; 127, expression_statement; 127, 128; 128, call; 128, 129; 128, 132; 129, attribute; 129, 130; 129, 131; 130, identifier:start; 131, identifier:setLine; 132, argument_list; 132, 133; 133, binary_operator:+; 133, 134; 133, 139; 134, call; 134, 135; 134, 138; 135, attribute; 135, 136; 135, 137; 136, identifier:start; 137, identifier:line; 138, argument_list; 139, attribute; 139, 140; 139, 141; 140, identifier:sorter; 141, identifier:length_change; 142, expression_statement; 142, 143; 143, assignment; 143, 144; 143, 145; 144, identifier:end; 145, call; 145, 146; 145, 149; 146, attribute; 146, 147; 146, 148; 147, identifier:selection; 148, identifier:end; 149, argument_list; 150, expression_statement; 150, 151; 151, call; 151, 152; 151, 155; 152, attribute; 152, 153; 152, 154; 153, identifier:end; 154, identifier:setLine; 155, argument_list; 155, 156; 156, binary_operator:+; 156, 157; 156, 162; 157, call; 157, 158; 157, 161; 158, attribute; 158, 159; 158, 160; 159, identifier:end; 160, identifier:line; 161, argument_list; 162, attribute; 162, 163; 162, 164; 163, identifier:sorter; 164, identifier:length_change; 165, expression_statement; 165, 166; 166, call; 166, 167; 166, 170; 167, attribute; 167, 168; 167, 169; 168, identifier:selection; 169, identifier:setRange; 170, argument_list; 170, 171; 170, 172; 171, identifier:start; 172, identifier:end; 173, expression_statement; 173, 174; 174, call; 174, 175; 174, 178; 175, attribute; 175, 176; 175, 177; 176, identifier:view; 177, identifier:setSelection; 178, argument_list; 178, 179; 179, identifier:selection; 180, expression_statement; 180, 181; 181, call; 181, 182; 181, 185; 182, attribute; 182, 183; 182, 184; 183, identifier:view; 184, identifier:setCursorPosition; 185, argument_list; 185, 186; 186, identifier:position
def sort_kate_imports(add_imports=(), remove_imports=()): """Sorts imports within Kate while maintaining cursor position and selection, even if length of file changes.""" document = kate.activeDocument() view = document.activeView() position = view.cursorPosition() selection = view.selectionRange() sorter = SortImports(file_contents=document.text(), add_imports=add_imports, remove_imports=remove_imports, settings_path=os.path.dirname(os.path.abspath(str(document.url().path())))) document.setText(sorter.output) position.setLine(position.line() + sorter.length_change) if selection: start = selection.start() start.setLine(start.line() + sorter.length_change) end = selection.end() end.setLine(end.line() + sorter.length_change) selection.setRange(start, end) view.setSelection(selection) view.setCursorPosition(position)
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 1, 7; 2, function_name:_get_line; 3, parameters; 3, 4; 4, identifier:self; 5, type; 5, 6; 6, identifier:str; 7, block; 7, 8; 7, 10; 7, 20; 7, 26; 8, expression_statement; 8, 9; 9, comment; 10, expression_statement; 10, 11; 11, assignment; 11, 12; 11, 13; 12, identifier:line; 13, subscript; 13, 14; 13, 17; 14, attribute; 14, 15; 14, 16; 15, identifier:self; 16, identifier:in_lines; 17, attribute; 17, 18; 17, 19; 18, identifier:self; 19, identifier:index; 20, expression_statement; 20, 21; 21, augmented_assignment:+=; 21, 22; 21, 25; 22, attribute; 22, 23; 22, 24; 23, identifier:self; 24, identifier:index; 25, integer:1; 26, return_statement; 26, 27; 27, identifier:line
def _get_line(self) -> str: """Returns the current line from the file while incrementing the index.""" line = self.in_lines[self.index] self.index += 1 return line
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 22; 1, 24; 2, function_name:_add_comments; 3, parameters; 3, 4; 3, 5; 3, 17; 4, identifier:self; 5, typed_parameter; 5, 6; 5, 7; 6, identifier:comments; 7, type; 7, 8; 8, generic_type; 8, 9; 8, 10; 9, identifier:Optional; 10, type_parameter; 10, 11; 11, type; 11, 12; 12, generic_type; 12, 13; 12, 14; 13, identifier:Sequence; 14, type_parameter; 14, 15; 15, type; 15, 16; 16, identifier:str; 17, typed_default_parameter; 17, 18; 17, 19; 17, 21; 18, identifier:original_string; 19, type; 19, 20; 20, identifier:str; 21, string:""; 22, type; 22, 23; 23, identifier:str; 24, block; 24, 25; 24, 27; 24, 43; 25, expression_statement; 25, 26; 26, comment; 27, if_statement; 27, 28; 27, 33; 28, subscript; 28, 29; 28, 32; 29, attribute; 29, 30; 29, 31; 30, identifier:self; 31, identifier:config; 32, string:'ignore_comments'; 33, block; 33, 34; 34, return_statement; 34, 35; 35, subscript; 35, 36; 35, 42; 36, call; 36, 37; 36, 40; 37, attribute; 37, 38; 37, 39; 38, identifier:self; 39, identifier:_strip_comments; 40, argument_list; 40, 41; 41, identifier:original_string; 42, integer:0; 43, if_statement; 43, 44; 43, 46; 43, 49; 44, not_operator; 44, 45; 45, identifier:comments; 46, block; 46, 47; 47, return_statement; 47, 48; 48, identifier:original_string; 49, else_clause; 49, 50; 50, block; 50, 51; 51, return_statement; 51, 52; 52, call; 52, 53; 52, 56; 53, attribute; 53, 54; 53, 55; 54, string:"{0}{1} {2}"; 55, identifier:format; 56, argument_list; 56, 57; 56, 65; 56, 70; 57, subscript; 57, 58; 57, 64; 58, call; 58, 59; 58, 62; 59, attribute; 59, 60; 59, 61; 60, identifier:self; 61, identifier:_strip_comments; 62, argument_list; 62, 63; 63, identifier:original_string; 64, integer:0; 65, subscript; 65, 66; 65, 69; 66, attribute; 66, 67; 66, 68; 67, identifier:self; 68, identifier:config; 69, string:'comment_prefix'; 70, call; 70, 71; 70, 74; 71, attribute; 71, 72; 71, 73; 72, string:"; "; 73, identifier:join; 74, argument_list; 74, 75; 75, identifier:comments
def _add_comments( self, comments: Optional[Sequence[str]], original_string: str = "" ) -> str: """ Returns a string with comments added if ignore_comments is not set. """ if self.config['ignore_comments']: return self._strip_comments(original_string)[0] if not comments: return original_string else: return "{0}{1} {2}".format(self._strip_comments(original_string)[0], self.config['comment_prefix'], "; ".join(comments))
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 9; 1, 11; 2, function_name:_wrap; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, typed_parameter; 5, 6; 5, 7; 6, identifier:line; 7, type; 7, 8; 8, identifier:str; 9, type; 9, 10; 10, identifier:str; 11, block; 11, 12; 11, 14; 11, 22; 11, 394; 12, expression_statement; 12, 13; 13, comment; 14, expression_statement; 14, 15; 15, assignment; 15, 16; 15, 17; 16, identifier:wrap_mode; 17, subscript; 17, 18; 17, 21; 18, attribute; 18, 19; 18, 20; 19, identifier:self; 20, identifier:config; 21, string:'multi_line_output'; 22, if_statement; 22, 23; 22, 39; 22, 357; 23, boolean_operator:and; 23, 24; 23, 34; 24, comparison_operator:>; 24, 25; 24, 29; 25, call; 25, 26; 25, 27; 26, identifier:len; 27, argument_list; 27, 28; 28, identifier:line; 29, subscript; 29, 30; 29, 33; 30, attribute; 30, 31; 30, 32; 31, identifier:self; 32, identifier:config; 33, string:'line_length'; 34, comparison_operator:!=; 34, 35; 34, 36; 35, identifier:wrap_mode; 36, attribute; 36, 37; 36, 38; 37, identifier:WrapModes; 38, identifier:NOQA; 39, block; 39, 40; 39, 44; 39, 48; 39, 65; 40, expression_statement; 40, 41; 41, assignment; 41, 42; 41, 43; 42, identifier:line_without_comment; 43, identifier:line; 44, expression_statement; 44, 45; 45, assignment; 45, 46; 45, 47; 46, identifier:comment; 47, None; 48, if_statement; 48, 49; 48, 52; 49, comparison_operator:in; 49, 50; 49, 51; 50, string:'#'; 51, identifier:line; 52, block; 52, 53; 53, expression_statement; 53, 54; 54, assignment; 54, 55; 54, 58; 55, pattern_list; 55, 56; 55, 57; 56, identifier:line_without_comment; 57, identifier:comment; 58, call; 58, 59; 58, 62; 59, attribute; 59, 60; 59, 61; 60, identifier:line; 61, identifier:split; 62, argument_list; 62, 63; 62, 64; 63, string:'#'; 64, integer:1; 65, for_statement; 65, 66; 65, 67; 65, 71; 66, identifier:splitter; 67, tuple; 67, 68; 67, 69; 67, 70; 68, string:"import "; 69, string:"."; 70, string:"as "; 71, block; 71, 72; 71, 85; 72, expression_statement; 72, 73; 73, assignment; 73, 74; 73, 75; 74, identifier:exp; 75, binary_operator:+; 75, 76; 75, 84; 76, binary_operator:+; 76, 77; 76, 78; 77, string:r"\b"; 78, call; 78, 79; 78, 82; 79, attribute; 79, 80; 79, 81; 80, identifier:re; 81, identifier:escape; 82, argument_list; 82, 83; 83, identifier:splitter; 84, string:r"\b"; 85, if_statement; 85, 86; 85, 105; 86, boolean_operator:and; 86, 87; 86, 94; 87, call; 87, 88; 87, 91; 88, attribute; 88, 89; 88, 90; 89, identifier:re; 90, identifier:search; 91, argument_list; 91, 92; 91, 93; 92, identifier:exp; 93, identifier:line_without_comment; 94, not_operator; 94, 95; 95, call; 95, 96; 95, 103; 96, attribute; 96, 97; 96, 102; 97, call; 97, 98; 97, 101; 98, attribute; 98, 99; 98, 100; 99, identifier:line_without_comment; 100, identifier:strip; 101, argument_list; 102, identifier:startswith; 103, argument_list; 103, 104; 104, identifier:splitter; 105, block; 105, 106; 105, 116; 105, 135; 105, 139; 105, 183; 105, 195; 105, 219; 105, 345; 106, expression_statement; 106, 107; 107, assignment; 107, 108; 107, 109; 108, identifier:line_parts; 109, call; 109, 110; 109, 113; 110, attribute; 110, 111; 110, 112; 111, identifier:re; 112, identifier:split; 113, argument_list; 113, 114; 113, 115; 114, identifier:exp; 115, identifier:line_without_comment; 116, if_statement; 116, 117; 116, 118; 117, identifier:comment; 118, block; 118, 119; 119, expression_statement; 119, 120; 120, assignment; 120, 121; 120, 125; 121, subscript; 121, 122; 121, 123; 122, identifier:line_parts; 123, unary_operator:-; 123, 124; 124, integer:1; 125, call; 125, 126; 125, 129; 126, attribute; 126, 127; 126, 128; 127, string:'{0}#{1}'; 128, identifier:format; 129, argument_list; 129, 130; 129, 134; 130, subscript; 130, 131; 130, 132; 131, identifier:line_parts; 132, unary_operator:-; 132, 133; 133, integer:1; 134, identifier:comment; 135, expression_statement; 135, 136; 136, assignment; 136, 137; 136, 138; 137, identifier:next_line; 138, list:[]; 139, while_statement; 139, 140; 139, 162; 140, boolean_operator:and; 140, 141; 140, 161; 141, comparison_operator:>; 141, 142; 141, 149; 142, parenthesized_expression; 142, 143; 143, binary_operator:+; 143, 144; 143, 148; 144, call; 144, 145; 144, 146; 145, identifier:len; 146, argument_list; 146, 147; 147, identifier:line; 148, integer:2; 149, parenthesized_expression; 149, 150; 150, boolean_operator:or; 150, 151; 150, 156; 151, subscript; 151, 152; 151, 155; 152, attribute; 152, 153; 152, 154; 153, identifier:self; 154, identifier:config; 155, string:'wrap_length'; 156, subscript; 156, 157; 156, 160; 157, attribute; 157, 158; 157, 159; 158, identifier:self; 159, identifier:config; 160, string:'line_length'; 161, identifier:line_parts; 162, block; 162, 163; 162, 174; 163, expression_statement; 163, 164; 164, call; 164, 165; 164, 168; 165, attribute; 165, 166; 165, 167; 166, identifier:next_line; 167, identifier:append; 168, argument_list; 168, 169; 169, call; 169, 170; 169, 173; 170, attribute; 170, 171; 170, 172; 171, identifier:line_parts; 172, identifier:pop; 173, argument_list; 174, expression_statement; 174, 175; 175, assignment; 175, 176; 175, 177; 176, identifier:line; 177, call; 177, 178; 177, 181; 178, attribute; 178, 179; 178, 180; 179, identifier:splitter; 180, identifier:join; 181, argument_list; 181, 182; 182, identifier:line_parts; 183, if_statement; 183, 184; 183, 186; 184, not_operator; 184, 185; 185, identifier:line; 186, block; 186, 187; 187, expression_statement; 187, 188; 188, assignment; 188, 189; 188, 190; 189, identifier:line; 190, call; 190, 191; 190, 194; 191, attribute; 191, 192; 191, 193; 192, identifier:next_line; 193, identifier:pop; 194, argument_list; 195, expression_statement; 195, 196; 196, assignment; 196, 197; 196, 198; 197, identifier:cont_line; 198, call; 198, 199; 198, 202; 199, attribute; 199, 200; 199, 201; 200, identifier:self; 201, identifier:_wrap; 202, argument_list; 202, 203; 203, binary_operator:+; 203, 204; 203, 209; 204, subscript; 204, 205; 204, 208; 205, attribute; 205, 206; 205, 207; 206, identifier:self; 207, identifier:config; 208, string:'indent'; 209, call; 209, 210; 209, 218; 210, attribute; 210, 211; 210, 217; 211, call; 211, 212; 211, 215; 212, attribute; 212, 213; 212, 214; 213, identifier:splitter; 214, identifier:join; 215, argument_list; 215, 216; 216, identifier:next_line; 217, identifier:lstrip; 218, argument_list; 219, if_statement; 219, 220; 219, 225; 220, subscript; 220, 221; 220, 224; 221, attribute; 221, 222; 221, 223; 222, identifier:self; 223, identifier:config; 224, string:'use_parentheses'; 225, block; 225, 226; 225, 262; 225, 273; 225, 336; 226, expression_statement; 226, 227; 227, assignment; 227, 228; 227, 229; 228, identifier:output; 229, call; 229, 230; 229, 233; 230, attribute; 230, 231; 230, 232; 231, string:"{0}{1}({2}{3}{4}{5})"; 232, identifier:format; 233, argument_list; 233, 234; 233, 235; 233, 236; 233, 239; 233, 240; 233, 248; 234, identifier:line; 235, identifier:splitter; 236, attribute; 236, 237; 236, 238; 237, identifier:self; 238, identifier:line_separator; 239, identifier:cont_line; 240, conditional_expression:if; 240, 241; 240, 242; 240, 247; 241, string:","; 242, subscript; 242, 243; 242, 246; 243, attribute; 243, 244; 243, 245; 244, identifier:self; 245, identifier:config; 246, string:'include_trailing_comma'; 247, string:""; 248, conditional_expression:if; 248, 249; 248, 252; 248, 261; 249, attribute; 249, 250; 249, 251; 250, identifier:self; 251, identifier:line_separator; 252, comparison_operator:in; 252, 253; 252, 254; 253, identifier:wrap_mode; 254, set; 254, 255; 254, 258; 255, attribute; 255, 256; 255, 257; 256, identifier:WrapModes; 257, identifier:VERTICAL_HANGING_INDENT; 258, attribute; 258, 259; 258, 260; 259, identifier:WrapModes; 260, identifier:VERTICAL_GRID_GROUPED; 261, string:""; 262, expression_statement; 262, 263; 263, assignment; 263, 264; 263, 265; 264, identifier:lines; 265, call; 265, 266; 265, 269; 266, attribute; 266, 267; 266, 268; 267, identifier:output; 268, identifier:split; 269, argument_list; 269, 270; 270, attribute; 270, 271; 270, 272; 271, identifier:self; 272, identifier:line_separator; 273, if_statement; 273, 274; 273, 294; 274, boolean_operator:and; 274, 275; 274, 285; 275, comparison_operator:in; 275, 276; 275, 281; 276, subscript; 276, 277; 276, 280; 277, attribute; 277, 278; 277, 279; 278, identifier:self; 279, identifier:config; 280, string:'comment_prefix'; 281, subscript; 281, 282; 281, 283; 282, identifier:lines; 283, unary_operator:-; 283, 284; 284, integer:1; 285, call; 285, 286; 285, 292; 286, attribute; 286, 287; 286, 291; 287, subscript; 287, 288; 287, 289; 288, identifier:lines; 289, unary_operator:-; 289, 290; 290, integer:1; 291, identifier:endswith; 292, argument_list; 292, 293; 293, string:')'; 294, block; 294, 295; 294, 314; 295, expression_statement; 295, 296; 296, assignment; 296, 297; 296, 300; 297, pattern_list; 297, 298; 297, 299; 298, identifier:line; 299, identifier:comment; 300, call; 300, 301; 300, 307; 301, attribute; 301, 302; 301, 306; 302, subscript; 302, 303; 302, 304; 303, identifier:lines; 304, unary_operator:-; 304, 305; 305, integer:1; 306, identifier:split; 307, argument_list; 307, 308; 307, 313; 308, subscript; 308, 309; 308, 312; 309, attribute; 309, 310; 309, 311; 310, identifier:self; 311, identifier:config; 312, string:'comment_prefix'; 313, integer:1; 314, expression_statement; 314, 315; 315, assignment; 315, 316; 315, 320; 316, subscript; 316, 317; 316, 318; 317, identifier:lines; 318, unary_operator:-; 318, 319; 319, integer:1; 320, binary_operator:+; 320, 321; 320, 330; 321, binary_operator:+; 321, 322; 321, 325; 322, binary_operator:+; 322, 323; 322, 324; 323, identifier:line; 324, string:')'; 325, subscript; 325, 326; 325, 329; 326, attribute; 326, 327; 326, 328; 327, identifier:self; 328, identifier:config; 329, string:'comment_prefix'; 330, subscript; 330, 331; 330, 332; 331, identifier:comment; 332, slice; 332, 333; 332, 334; 333, colon; 334, unary_operator:-; 334, 335; 335, integer:1; 336, return_statement; 336, 337; 337, call; 337, 338; 337, 343; 338, attribute; 338, 339; 338, 342; 339, attribute; 339, 340; 339, 341; 340, identifier:self; 341, identifier:line_separator; 342, identifier:join; 343, argument_list; 343, 344; 344, identifier:lines; 345, return_statement; 345, 346; 346, call; 346, 347; 346, 350; 347, attribute; 347, 348; 347, 349; 348, string:"{0}{1}\\{2}{3}"; 349, identifier:format; 350, argument_list; 350, 351; 350, 352; 350, 353; 350, 356; 351, identifier:line; 352, identifier:splitter; 353, attribute; 353, 354; 353, 355; 354, identifier:self; 355, identifier:line_separator; 356, identifier:cont_line; 357, elif_clause; 357, 358; 357, 376; 358, boolean_operator:and; 358, 359; 358, 369; 359, comparison_operator:>; 359, 360; 359, 364; 360, call; 360, 361; 360, 362; 361, identifier:len; 362, argument_list; 362, 363; 363, identifier:line; 364, subscript; 364, 365; 364, 368; 365, attribute; 365, 366; 365, 367; 366, identifier:self; 367, identifier:config; 368, string:'line_length'; 369, comparison_operator:==; 369, 370; 369, 371; 370, identifier:wrap_mode; 371, attribute; 371, 372; 371, 375; 372, attribute; 372, 373; 372, 374; 373, identifier:settings; 374, identifier:WrapModes; 375, identifier:NOQA; 376, block; 376, 377; 377, if_statement; 377, 378; 377, 381; 378, comparison_operator:not; 378, 379; 378, 380; 379, string:"# NOQA"; 380, identifier:line; 381, block; 381, 382; 382, return_statement; 382, 383; 383, call; 383, 384; 383, 387; 384, attribute; 384, 385; 384, 386; 385, string:"{0}{1} NOQA"; 386, identifier:format; 387, argument_list; 387, 388; 387, 389; 388, identifier:line; 389, subscript; 389, 390; 389, 393; 390, attribute; 390, 391; 390, 392; 391, identifier:self; 392, identifier:config; 393, string:'comment_prefix'; 394, return_statement; 394, 395; 395, identifier:line
def _wrap(self, line: str) -> str: """ Returns an import wrapped to the specified line-length, if possible. """ wrap_mode = self.config['multi_line_output'] if len(line) > self.config['line_length'] and wrap_mode != WrapModes.NOQA: line_without_comment = line comment = None if '#' in line: line_without_comment, comment = line.split('#', 1) for splitter in ("import ", ".", "as "): exp = r"\b" + re.escape(splitter) + r"\b" if re.search(exp, line_without_comment) and not line_without_comment.strip().startswith(splitter): line_parts = re.split(exp, line_without_comment) if comment: line_parts[-1] = '{0}#{1}'.format(line_parts[-1], comment) next_line = [] while (len(line) + 2) > (self.config['wrap_length'] or self.config['line_length']) and line_parts: next_line.append(line_parts.pop()) line = splitter.join(line_parts) if not line: line = next_line.pop() cont_line = self._wrap(self.config['indent'] + splitter.join(next_line).lstrip()) if self.config['use_parentheses']: output = "{0}{1}({2}{3}{4}{5})".format( line, splitter, self.line_separator, cont_line, "," if self.config['include_trailing_comma'] else "", self.line_separator if wrap_mode in {WrapModes.VERTICAL_HANGING_INDENT, WrapModes.VERTICAL_GRID_GROUPED} else "") lines = output.split(self.line_separator) if self.config['comment_prefix'] in lines[-1] and lines[-1].endswith(')'): line, comment = lines[-1].split(self.config['comment_prefix'], 1) lines[-1] = line + ')' + self.config['comment_prefix'] + comment[:-1] return self.line_separator.join(lines) return "{0}{1}\\{2}{3}".format(line, splitter, self.line_separator, cont_line) elif len(line) > self.config['line_length'] and wrap_mode == settings.WrapModes.NOQA: if "# NOQA" not in line: return "{0}{1} NOQA".format(line, self.config['comment_prefix']) return line
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:get_formatter; 3, parameters; 3, 4; 4, identifier:name; 5, block; 5, 6; 5, 8; 6, expression_statement; 6, 7; 7, comment; 8, for_statement; 8, 9; 8, 10; 8, 14; 9, identifier:k; 10, call; 10, 11; 10, 12; 11, identifier:sorted; 12, argument_list; 12, 13; 13, identifier:_FORMATTERS; 14, block; 14, 15; 15, if_statement; 15, 16; 15, 22; 16, call; 16, 17; 16, 20; 17, attribute; 17, 18; 17, 19; 18, identifier:k; 19, identifier:startswith; 20, argument_list; 20, 21; 21, identifier:name; 22, block; 22, 23; 23, return_statement; 23, 24; 24, subscript; 24, 25; 24, 26; 25, identifier:_FORMATTERS; 26, identifier:k
def get_formatter(name): """ Looks up a formatter class given a prefix to it. The names are sorted, and the first matching class is returned. """ for k in sorted(_FORMATTERS): if k.startswith(name): return _FORMATTERS[k]
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 5; 2, function_name:call_once; 3, parameters; 3, 4; 4, identifier:func; 5, block; 5, 6; 5, 8; 5, 17; 5, 37; 5, 76; 5, 82; 5, 91; 5, 103; 6, expression_statement; 6, 7; 7, comment; 8, expression_statement; 8, 9; 9, assignment; 9, 10; 9, 11; 10, identifier:argspec; 11, call; 11, 12; 11, 15; 12, attribute; 12, 13; 12, 14; 13, identifier:inspect; 14, identifier:getargspec; 15, argument_list; 15, 16; 16, identifier:func; 17, if_statement; 17, 18; 17, 29; 18, boolean_operator:or; 18, 19; 18, 26; 19, boolean_operator:or; 19, 20; 19, 23; 20, attribute; 20, 21; 20, 22; 21, identifier:argspec; 22, identifier:args; 23, attribute; 23, 24; 23, 25; 24, identifier:argspec; 25, identifier:varargs; 26, attribute; 26, 27; 26, 28; 27, identifier:argspec; 28, identifier:keywords; 29, block; 29, 30; 30, raise_statement; 30, 31; 31, call; 31, 32; 31, 33; 32, identifier:ValueError; 33, argument_list; 33, 34; 33, 35; 33, 36; 34, string:'Can only decorate functions with no args'; 35, identifier:func; 36, identifier:argspec; 37, decorated_definition; 37, 38; 37, 45; 38, decorator; 38, 39; 39, call; 39, 40; 39, 43; 40, attribute; 40, 41; 40, 42; 41, identifier:functools; 42, identifier:wraps; 43, argument_list; 43, 44; 44, identifier:func; 45, function_definition; 45, 46; 45, 47; 45, 48; 45, 49; 46, function_name:_wrapper; 47, parameters; 48, comment; 49, block; 49, 50; 49, 72; 50, if_statement; 50, 51; 50, 57; 51, not_operator; 51, 52; 52, call; 52, 53; 52, 56; 53, attribute; 53, 54; 53, 55; 54, identifier:_wrapper; 55, identifier:HasRun; 56, argument_list; 57, block; 57, 58; 57, 64; 58, expression_statement; 58, 59; 59, call; 59, 60; 59, 63; 60, attribute; 60, 61; 60, 62; 61, identifier:_wrapper; 62, identifier:MarkAsRun; 63, argument_list; 64, expression_statement; 64, 65; 65, assignment; 65, 66; 65, 69; 66, attribute; 66, 67; 66, 68; 67, identifier:_wrapper; 68, identifier:return_value; 69, call; 69, 70; 69, 71; 70, identifier:func; 71, argument_list; 72, return_statement; 72, 73; 73, attribute; 73, 74; 73, 75; 74, identifier:_wrapper; 75, identifier:return_value; 76, expression_statement; 76, 77; 77, assignment; 77, 78; 77, 81; 78, attribute; 78, 79; 78, 80; 79, identifier:_wrapper; 80, identifier:has_run; 81, False; 82, expression_statement; 82, 83; 83, assignment; 83, 84; 83, 87; 84, attribute; 84, 85; 84, 86; 85, identifier:_wrapper; 86, identifier:HasRun; 87, lambda; 87, 88; 88, attribute; 88, 89; 88, 90; 89, identifier:_wrapper; 90, identifier:has_run; 91, expression_statement; 91, 92; 92, assignment; 92, 93; 92, 96; 93, attribute; 93, 94; 93, 95; 94, identifier:_wrapper; 95, identifier:MarkAsRun; 96, lambda; 96, 97; 97, call; 97, 98; 97, 99; 98, identifier:setattr; 99, argument_list; 99, 100; 99, 101; 99, 102; 100, identifier:_wrapper; 101, string:'has_run'; 102, True; 103, return_statement; 103, 104; 104, identifier:_wrapper
def call_once(func): """Decorate a function to only allow it to be called once. Note that it doesn't make sense to only call a function once if it takes arguments (use @functools.lru_cache for that sort of thing), so this only works on callables that take no args. """ argspec = inspect.getargspec(func) if argspec.args or argspec.varargs or argspec.keywords: raise ValueError('Can only decorate functions with no args', func, argspec) @functools.wraps(func) def _wrapper(): # If we haven't been called yet, actually invoke func and save the result. if not _wrapper.HasRun(): _wrapper.MarkAsRun() _wrapper.return_value = func() return _wrapper.return_value _wrapper.has_run = False _wrapper.HasRun = lambda: _wrapper.has_run _wrapper.MarkAsRun = lambda: setattr(_wrapper, 'has_run', True) return _wrapper
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 19; 2, function_name:paginate; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 7; 3, 10; 3, 13; 3, 16; 4, identifier:self; 5, identifier:model_class; 6, identifier:order_by; 7, default_parameter; 7, 8; 7, 9; 8, identifier:page_num; 9, integer:1; 10, default_parameter; 10, 11; 10, 12; 11, identifier:page_size; 12, integer:100; 13, default_parameter; 13, 14; 13, 15; 14, identifier:conditions; 15, None; 16, default_parameter; 16, 17; 16, 18; 17, identifier:settings; 18, None; 19, block; 19, 20; 19, 22; 19, 32; 19, 47; 19, 73; 19, 82; 19, 86; 19, 95; 19, 101; 19, 109; 19, 119; 20, expression_statement; 20, 21; 21, string:''' Selects records and returns a single page of model instances. - `model_class`: the model class matching the query's table, or `None` for getting back instances of an ad-hoc model. - `order_by`: columns to use for sorting the query (contents of the ORDER BY clause). - `page_num`: the page number (1-based), or -1 to get the last page. - `page_size`: number of records to return per page. - `conditions`: optional SQL conditions (contents of the WHERE clause). - `settings`: query settings to send as HTTP GET parameters The result is a namedtuple containing `objects` (list), `number_of_objects`, `pages_total`, `number` (of the current page), and `page_size`. '''; 22, expression_statement; 22, 23; 23, assignment; 23, 24; 23, 25; 24, identifier:count; 25, call; 25, 26; 25, 29; 26, attribute; 26, 27; 26, 28; 27, identifier:self; 28, identifier:count; 29, argument_list; 29, 30; 29, 31; 30, identifier:model_class; 31, identifier:conditions; 32, expression_statement; 32, 33; 33, assignment; 33, 34; 33, 35; 34, identifier:pages_total; 35, call; 35, 36; 35, 37; 36, identifier:int; 37, argument_list; 37, 38; 38, call; 38, 39; 38, 40; 39, identifier:ceil; 40, argument_list; 40, 41; 41, binary_operator:/; 41, 42; 41, 43; 42, identifier:count; 43, call; 43, 44; 43, 45; 44, identifier:float; 45, argument_list; 45, 46; 46, identifier:page_size; 47, if_statement; 47, 48; 47, 52; 47, 61; 48, comparison_operator:==; 48, 49; 48, 50; 49, identifier:page_num; 50, unary_operator:-; 50, 51; 51, integer:1; 52, block; 52, 53; 53, expression_statement; 53, 54; 54, assignment; 54, 55; 54, 56; 55, identifier:page_num; 56, call; 56, 57; 56, 58; 57, identifier:max; 58, argument_list; 58, 59; 58, 60; 59, identifier:pages_total; 60, integer:1; 61, elif_clause; 61, 62; 61, 65; 62, comparison_operator:<; 62, 63; 62, 64; 63, identifier:page_num; 64, integer:1; 65, block; 65, 66; 66, raise_statement; 66, 67; 67, call; 67, 68; 67, 69; 68, identifier:ValueError; 69, argument_list; 69, 70; 70, binary_operator:%; 70, 71; 70, 72; 71, string:'Invalid page number: %d'; 72, identifier:page_num; 73, expression_statement; 73, 74; 74, assignment; 74, 75; 74, 76; 75, identifier:offset; 76, binary_operator:*; 76, 77; 76, 81; 77, parenthesized_expression; 77, 78; 78, binary_operator:-; 78, 79; 78, 80; 79, identifier:page_num; 80, integer:1; 81, identifier:page_size; 82, expression_statement; 82, 83; 83, assignment; 83, 84; 83, 85; 84, identifier:query; 85, string:'SELECT * FROM $table'; 86, if_statement; 86, 87; 86, 88; 87, identifier:conditions; 88, block; 88, 89; 89, expression_statement; 89, 90; 90, augmented_assignment:+=; 90, 91; 90, 92; 91, identifier:query; 92, binary_operator:+; 92, 93; 92, 94; 93, string:' WHERE '; 94, identifier:conditions; 95, expression_statement; 95, 96; 96, augmented_assignment:+=; 96, 97; 96, 98; 97, identifier:query; 98, binary_operator:%; 98, 99; 98, 100; 99, string:' ORDER BY %s'; 100, identifier:order_by; 101, expression_statement; 101, 102; 102, augmented_assignment:+=; 102, 103; 102, 104; 103, identifier:query; 104, binary_operator:%; 104, 105; 104, 106; 105, string:' LIMIT %d, %d'; 106, tuple; 106, 107; 106, 108; 107, identifier:offset; 108, identifier:page_size; 109, expression_statement; 109, 110; 110, assignment; 110, 111; 110, 112; 111, identifier:query; 112, call; 112, 113; 112, 116; 113, attribute; 113, 114; 113, 115; 114, identifier:self; 115, identifier:_substitute; 116, argument_list; 116, 117; 116, 118; 117, identifier:query; 118, identifier:model_class; 119, return_statement; 119, 120; 120, call; 120, 121; 120, 122; 121, identifier:Page; 122, argument_list; 122, 123; 122, 139; 122, 142; 122, 145; 122, 148; 123, keyword_argument; 123, 124; 123, 125; 124, identifier:objects; 125, conditional_expression:if; 125, 126; 125, 137; 125, 138; 126, call; 126, 127; 126, 128; 127, identifier:list; 128, argument_list; 128, 129; 129, call; 129, 130; 129, 133; 130, attribute; 130, 131; 130, 132; 131, identifier:self; 132, identifier:select; 133, argument_list; 133, 134; 133, 135; 133, 136; 134, identifier:query; 135, identifier:model_class; 136, identifier:settings; 137, identifier:count; 138, list:[]; 139, keyword_argument; 139, 140; 139, 141; 140, identifier:number_of_objects; 141, identifier:count; 142, keyword_argument; 142, 143; 142, 144; 143, identifier:pages_total; 144, identifier:pages_total; 145, keyword_argument; 145, 146; 145, 147; 146, identifier:number; 147, identifier:page_num; 148, keyword_argument; 148, 149; 148, 150; 149, identifier:page_size; 150, identifier:page_size
def paginate(self, model_class, order_by, page_num=1, page_size=100, conditions=None, settings=None): ''' Selects records and returns a single page of model instances. - `model_class`: the model class matching the query's table, or `None` for getting back instances of an ad-hoc model. - `order_by`: columns to use for sorting the query (contents of the ORDER BY clause). - `page_num`: the page number (1-based), or -1 to get the last page. - `page_size`: number of records to return per page. - `conditions`: optional SQL conditions (contents of the WHERE clause). - `settings`: query settings to send as HTTP GET parameters The result is a namedtuple containing `objects` (list), `number_of_objects`, `pages_total`, `number` (of the current page), and `page_size`. ''' count = self.count(model_class, conditions) pages_total = int(ceil(count / float(page_size))) if page_num == -1: page_num = max(pages_total, 1) elif page_num < 1: raise ValueError('Invalid page number: %d' % page_num) offset = (page_num - 1) * page_size query = 'SELECT * FROM $table' if conditions: query += ' WHERE ' + conditions query += ' ORDER BY %s' % order_by query += ' LIMIT %d, %d' % (offset, page_size) query = self._substitute(query, model_class) return Page( objects=list(self.select(query, model_class, settings)) if count else [], number_of_objects=count, pages_total=pages_total, number=page_num, page_size=page_size )
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 6; 2, function_name:compute_memory_contents_under_schedule; 3, parameters; 3, 4; 3, 5; 4, identifier:self; 5, identifier:schedule; 6, block; 6, 7; 6, 9; 6, 17; 6, 23; 6, 27; 6, 121; 7, expression_statement; 7, 8; 8, comment; 9, expression_statement; 9, 10; 10, assignment; 10, 11; 10, 12; 11, identifier:out_degree; 12, call; 12, 13; 12, 16; 13, attribute; 13, 14; 13, 15; 14, identifier:self; 15, identifier:_compute_initial_out_degree; 16, argument_list; 17, expression_statement; 17, 18; 18, assignment; 18, 19; 18, 20; 19, identifier:curr_memory_contents; 20, call; 20, 21; 20, 22; 21, identifier:set; 22, argument_list; 23, expression_statement; 23, 24; 24, assignment; 24, 25; 24, 26; 25, identifier:memory_contents_for_each_operation; 26, list:[]; 27, for_statement; 27, 28; 27, 29; 27, 30; 28, identifier:operation_id; 29, identifier:schedule; 30, block; 30, 31; 30, 41; 30, 42; 30, 58; 30, 68; 30, 69; 30, 92; 31, expression_statement; 31, 32; 32, assignment; 32, 33; 32, 34; 33, identifier:operation_name; 34, attribute; 34, 35; 34, 40; 35, subscript; 35, 36; 35, 39; 36, attribute; 36, 37; 36, 38; 37, identifier:self; 38, identifier:_operations; 39, identifier:operation_id; 40, identifier:name; 41, comment; 42, for_statement; 42, 43; 42, 44; 42, 50; 43, identifier:output_name; 44, call; 44, 45; 44, 48; 45, attribute; 45, 46; 45, 47; 46, identifier:self; 47, identifier:get_operation_output_names; 48, argument_list; 48, 49; 49, identifier:operation_name; 50, block; 50, 51; 51, expression_statement; 51, 52; 52, call; 52, 53; 52, 56; 53, attribute; 53, 54; 53, 55; 54, identifier:curr_memory_contents; 55, identifier:add; 56, argument_list; 56, 57; 57, identifier:output_name; 58, expression_statement; 58, 59; 59, call; 59, 60; 59, 63; 60, attribute; 60, 61; 60, 62; 61, identifier:memory_contents_for_each_operation; 62, identifier:append; 63, argument_list; 63, 64; 64, call; 64, 65; 64, 66; 65, identifier:frozenset; 66, argument_list; 66, 67; 67, identifier:curr_memory_contents; 68, comment; 69, for_statement; 69, 70; 69, 71; 69, 77; 70, identifier:output_name; 71, call; 71, 72; 71, 75; 72, attribute; 72, 73; 72, 74; 73, identifier:self; 74, identifier:get_operation_output_names; 75, argument_list; 75, 76; 76, identifier:operation_name; 77, block; 77, 78; 78, if_statement; 78, 79; 78, 84; 79, comparison_operator:==; 79, 80; 79, 83; 80, subscript; 80, 81; 80, 82; 81, identifier:out_degree; 82, identifier:output_name; 83, integer:0; 84, block; 84, 85; 85, expression_statement; 85, 86; 86, call; 86, 87; 86, 90; 87, attribute; 87, 88; 87, 89; 88, identifier:curr_memory_contents; 89, identifier:remove; 90, argument_list; 90, 91; 91, identifier:output_name; 92, for_statement; 92, 93; 92, 94; 92, 100; 93, identifier:input_name; 94, call; 94, 95; 94, 98; 95, attribute; 95, 96; 95, 97; 96, identifier:self; 97, identifier:get_operation_input_names; 98, argument_list; 98, 99; 99, identifier:operation_name; 100, block; 100, 101; 100, 107; 101, expression_statement; 101, 102; 102, augmented_assignment:-=; 102, 103; 102, 106; 103, subscript; 103, 104; 103, 105; 104, identifier:out_degree; 105, identifier:input_name; 106, integer:1; 107, if_statement; 107, 108; 107, 113; 108, comparison_operator:==; 108, 109; 108, 112; 109, subscript; 109, 110; 109, 111; 110, identifier:out_degree; 111, identifier:input_name; 112, integer:0; 113, block; 113, 114; 114, expression_statement; 114, 115; 115, call; 115, 116; 115, 119; 116, attribute; 116, 117; 116, 118; 117, identifier:curr_memory_contents; 118, identifier:remove; 119, argument_list; 119, 120; 120, identifier:input_name; 121, return_statement; 121, 122; 122, identifier:memory_contents_for_each_operation
def compute_memory_contents_under_schedule(self, schedule): """The in-memory tensors present when executing each operation in schedule. Simulates running operations in the order given by a schedule. Keeps track of the tensors in memory at every point in time, and outputs a list (one entry for each point in time) of all sets of all memory contents (i.e. a frozenset of strings) ever seen in this execution. It is assumed (but not checked) that schedule is a valid topological sort of the operations in this graph. Args: schedule: A list of integer ids; the order to run operations in. Returns: a list of frozenset of strings, where the ith entry describes the tensors in memory when executing operation i (where schedule[i] is an index into get_all_operation_names()). """ out_degree = self._compute_initial_out_degree() curr_memory_contents = set() memory_contents_for_each_operation = [] for operation_id in schedule: operation_name = self._operations[operation_id].name # Allocate new memory to perform the computation at this node. for output_name in self.get_operation_output_names(operation_name): curr_memory_contents.add(output_name) memory_contents_for_each_operation.append(frozenset(curr_memory_contents)) # Free any tensors which are no longer needed. for output_name in self.get_operation_output_names(operation_name): if out_degree[output_name] == 0: curr_memory_contents.remove(output_name) for input_name in self.get_operation_input_names(operation_name): out_degree[input_name] -= 1 if out_degree[input_name] == 0: curr_memory_contents.remove(input_name) return memory_contents_for_each_operation
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 16; 1, 18; 2, function_name:sort; 3, parameters; 3, 4; 3, 5; 3, 9; 3, 14; 4, identifier:self; 5, typed_parameter; 5, 6; 5, 7; 6, identifier:by; 7, type; 7, 8; 8, identifier:str; 9, typed_default_parameter; 9, 10; 9, 11; 9, 13; 10, identifier:out; 11, type; 11, 12; 12, identifier:object; 13, string:''; 14, dictionary_splat_pattern; 14, 15; 15, identifier:kwargs; 16, type; 16, 17; 17, string:'SASdata'; 18, block; 18, 19; 18, 21; 18, 25; 18, 29; 18, 119; 18, 130; 18, 149; 18, 155; 18, 159; 18, 163; 18, 179; 18, 187; 18, 194; 18, 254; 19, expression_statement; 19, 20; 20, comment; 21, expression_statement; 21, 22; 22, assignment; 22, 23; 22, 24; 23, identifier:outstr; 24, string:''; 25, expression_statement; 25, 26; 26, assignment; 26, 27; 26, 28; 27, identifier:options; 28, string:''; 29, if_statement; 29, 30; 29, 31; 30, identifier:out; 31, block; 31, 32; 32, if_statement; 32, 33; 32, 38; 32, 93; 33, call; 33, 34; 33, 35; 34, identifier:isinstance; 35, argument_list; 35, 36; 35, 37; 36, identifier:out; 37, identifier:str; 38, block; 38, 39; 38, 48; 39, expression_statement; 39, 40; 40, assignment; 40, 41; 40, 42; 41, identifier:fn; 42, call; 42, 43; 42, 46; 43, attribute; 43, 44; 43, 45; 44, identifier:out; 45, identifier:partition; 46, argument_list; 46, 47; 47, string:'.'; 48, if_statement; 48, 49; 48, 54; 48, 75; 49, comparison_operator:==; 49, 50; 49, 53; 50, subscript; 50, 51; 50, 52; 51, identifier:fn; 52, integer:1; 53, string:'.'; 54, block; 54, 55; 54, 61; 54, 67; 55, expression_statement; 55, 56; 56, assignment; 56, 57; 56, 58; 57, identifier:libref; 58, subscript; 58, 59; 58, 60; 59, identifier:fn; 60, integer:0; 61, expression_statement; 61, 62; 62, assignment; 62, 63; 62, 64; 63, identifier:table; 64, subscript; 64, 65; 64, 66; 65, identifier:fn; 66, integer:2; 67, expression_statement; 67, 68; 68, assignment; 68, 69; 68, 70; 69, identifier:outstr; 70, binary_operator:%; 70, 71; 70, 72; 71, string:"out=%s.%s"; 72, tuple; 72, 73; 72, 74; 73, identifier:libref; 74, identifier:table; 75, else_clause; 75, 76; 76, block; 76, 77; 76, 81; 76, 87; 77, expression_statement; 77, 78; 78, assignment; 78, 79; 78, 80; 79, identifier:libref; 80, string:''; 81, expression_statement; 81, 82; 82, assignment; 82, 83; 82, 84; 83, identifier:table; 84, subscript; 84, 85; 84, 86; 85, identifier:fn; 86, integer:0; 87, expression_statement; 87, 88; 88, assignment; 88, 89; 88, 90; 89, identifier:outstr; 90, binary_operator:+; 90, 91; 90, 92; 91, string:"out="; 92, identifier:table; 93, else_clause; 93, 94; 94, block; 94, 95; 94, 101; 94, 107; 95, expression_statement; 95, 96; 96, assignment; 96, 97; 96, 98; 97, identifier:libref; 98, attribute; 98, 99; 98, 100; 99, identifier:out; 100, identifier:libref; 101, expression_statement; 101, 102; 102, assignment; 102, 103; 102, 104; 103, identifier:table; 104, attribute; 104, 105; 104, 106; 105, identifier:out; 106, identifier:table; 107, expression_statement; 107, 108; 108, assignment; 108, 109; 108, 110; 109, identifier:outstr; 110, binary_operator:%; 110, 111; 110, 112; 111, string:"out=%s.%s"; 112, tuple; 112, 113; 112, 116; 113, attribute; 113, 114; 113, 115; 114, identifier:out; 115, identifier:libref; 116, attribute; 116, 117; 116, 118; 117, identifier:out; 118, identifier:table; 119, if_statement; 119, 120; 119, 123; 120, comparison_operator:in; 120, 121; 120, 122; 121, string:'options'; 122, identifier:kwargs; 123, block; 123, 124; 124, expression_statement; 124, 125; 125, assignment; 125, 126; 125, 127; 126, identifier:options; 127, subscript; 127, 128; 127, 129; 128, identifier:kwargs; 129, string:'options'; 130, expression_statement; 130, 131; 131, assignment; 131, 132; 131, 133; 132, identifier:code; 133, binary_operator:%; 133, 134; 133, 135; 134, string:"proc sort data=%s.%s%s %s %s ;\n"; 135, tuple; 135, 136; 135, 139; 135, 142; 135, 147; 135, 148; 136, attribute; 136, 137; 136, 138; 137, identifier:self; 138, identifier:libref; 139, attribute; 139, 140; 139, 141; 140, identifier:self; 141, identifier:table; 142, call; 142, 143; 142, 146; 143, attribute; 143, 144; 143, 145; 144, identifier:self; 145, identifier:_dsopts; 146, argument_list; 147, identifier:outstr; 148, identifier:options; 149, expression_statement; 149, 150; 150, augmented_assignment:+=; 150, 151; 150, 152; 151, identifier:code; 152, binary_operator:%; 152, 153; 152, 154; 153, string:"by %s;"; 154, identifier:by; 155, expression_statement; 155, 156; 156, augmented_assignment:+=; 156, 157; 156, 158; 157, identifier:code; 158, string:"run\n;"; 159, expression_statement; 159, 160; 160, assignment; 160, 161; 160, 162; 161, identifier:runcode; 162, True; 163, if_statement; 163, 164; 163, 169; 164, attribute; 164, 165; 164, 168; 165, attribute; 165, 166; 165, 167; 166, identifier:self; 167, identifier:sas; 168, identifier:nosub; 169, block; 169, 170; 169, 175; 170, expression_statement; 170, 171; 171, call; 171, 172; 171, 173; 172, identifier:print; 173, argument_list; 173, 174; 174, identifier:code; 175, expression_statement; 175, 176; 176, assignment; 176, 177; 176, 178; 177, identifier:runcode; 178, False; 179, expression_statement; 179, 180; 180, assignment; 180, 181; 180, 182; 181, identifier:ll; 182, call; 182, 183; 182, 186; 183, attribute; 183, 184; 183, 185; 184, identifier:self; 185, identifier:_is_valid; 186, argument_list; 187, if_statement; 187, 188; 187, 189; 188, identifier:ll; 189, block; 189, 190; 190, expression_statement; 190, 191; 191, assignment; 191, 192; 191, 193; 192, identifier:runcode; 193, False; 194, if_statement; 194, 195; 194, 196; 195, identifier:runcode; 196, block; 196, 197; 196, 209; 196, 213; 196, 238; 197, expression_statement; 197, 198; 198, assignment; 198, 199; 198, 200; 199, identifier:ll; 200, call; 200, 201; 200, 206; 201, attribute; 201, 202; 201, 205; 202, attribute; 202, 203; 202, 204; 203, identifier:self; 204, identifier:sas; 205, identifier:submit; 206, argument_list; 206, 207; 206, 208; 207, identifier:code; 208, string:"text"; 209, expression_statement; 209, 210; 210, assignment; 210, 211; 210, 212; 211, identifier:elog; 212, list:[]; 213, for_statement; 213, 214; 213, 215; 213, 222; 214, identifier:line; 215, call; 215, 216; 215, 221; 216, attribute; 216, 217; 216, 220; 217, subscript; 217, 218; 217, 219; 218, identifier:ll; 219, string:'LOG'; 220, identifier:splitlines; 221, argument_list; 222, block; 222, 223; 223, if_statement; 223, 224; 223, 230; 224, call; 224, 225; 224, 228; 225, attribute; 225, 226; 225, 227; 226, identifier:line; 227, identifier:startswith; 228, argument_list; 228, 229; 229, string:'ERROR'; 230, block; 230, 231; 231, expression_statement; 231, 232; 232, call; 232, 233; 232, 236; 233, attribute; 233, 234; 233, 235; 234, identifier:elog; 235, identifier:append; 236, argument_list; 236, 237; 237, identifier:line; 238, if_statement; 238, 239; 238, 243; 239, call; 239, 240; 239, 241; 240, identifier:len; 241, argument_list; 241, 242; 242, identifier:elog; 243, block; 243, 244; 244, raise_statement; 244, 245; 245, call; 245, 246; 245, 247; 246, identifier:RuntimeError; 247, argument_list; 247, 248; 248, call; 248, 249; 248, 252; 249, attribute; 249, 250; 249, 251; 250, string:"\n"; 251, identifier:join; 252, argument_list; 252, 253; 253, identifier:elog; 254, if_statement; 254, 255; 254, 256; 254, 282; 255, identifier:out; 256, block; 256, 257; 257, if_statement; 257, 258; 257, 264; 257, 267; 258, not_operator; 258, 259; 259, call; 259, 260; 259, 261; 260, identifier:isinstance; 261, argument_list; 261, 262; 261, 263; 262, identifier:out; 263, identifier:str; 264, block; 264, 265; 265, return_statement; 265, 266; 266, identifier:out; 267, else_clause; 267, 268; 268, block; 268, 269; 269, return_statement; 269, 270; 270, call; 270, 271; 270, 276; 271, attribute; 271, 272; 271, 275; 272, attribute; 272, 273; 272, 274; 273, identifier:self; 274, identifier:sas; 275, identifier:sasdata; 276, argument_list; 276, 277; 276, 278; 276, 279; 277, identifier:table; 278, identifier:libref; 279, attribute; 279, 280; 279, 281; 280, identifier:self; 281, identifier:results; 282, else_clause; 282, 283; 283, block; 283, 284; 284, return_statement; 284, 285; 285, identifier:self
def sort(self, by: str, out: object = '', **kwargs) -> 'SASdata': """ Sort the SAS Data Set :param by: REQUIRED variable to sort by (BY <DESCENDING> variable-1 <<DESCENDING> variable-2 ...>;) :param out: OPTIONAL takes either a string 'libref.table' or 'table' which will go to WORK or USER if assigned or a sas data object'' will sort in place if allowed :param kwargs: :return: SASdata object if out= not specified, or a new SASdata object for out= when specified :Example: #. wkcars.sort('type') #. wkcars2 = sas.sasdata('cars2') #. wkcars.sort('cylinders', wkcars2) #. cars2=cars.sort('DESCENDING origin', out='foobar') #. cars.sort('type').head() #. stat_results = stat.reg(model='horsepower = Cylinders EngineSize', by='type', data=wkcars.sort('type')) #. stat_results2 = stat.reg(model='horsepower = Cylinders EngineSize', by='type', data=wkcars.sort('type','work.cars')) """ outstr = '' options = '' if out: if isinstance(out, str): fn = out.partition('.') if fn[1] == '.': libref = fn[0] table = fn[2] outstr = "out=%s.%s" % (libref, table) else: libref = '' table = fn[0] outstr = "out=" + table else: libref = out.libref table = out.table outstr = "out=%s.%s" % (out.libref, out.table) if 'options' in kwargs: options = kwargs['options'] code = "proc sort data=%s.%s%s %s %s ;\n" % (self.libref, self.table, self._dsopts(), outstr, options) code += "by %s;" % by code += "run\n;" runcode = True if self.sas.nosub: print(code) runcode = False ll = self._is_valid() if ll: runcode = False if runcode: ll = self.sas.submit(code, "text") elog = [] for line in ll['LOG'].splitlines(): if line.startswith('ERROR'): elog.append(line) if len(elog): raise RuntimeError("\n".join(elog)) if out: if not isinstance(out, str): return out else: return self.sas.sasdata(table, libref, self.results) else: return self
0, module; 0, 1; 1, function_definition; 1, 2; 1, 3; 1, 30; 2, function_name:find_items; 3, parameters; 3, 4; 3, 5; 3, 6; 3, 9; 3, 12; 3, 15; 3, 18; 3, 21; 3, 24; 3, 27; 4, identifier:self; 5, identifier:q; 6, default_parameter; 6, 7; 6, 8; 7, identifier:shape; 8, identifier:ID_ONLY; 9, default_parameter; 9, 10; 9, 11; 10, identifier:depth; 11, identifier:SHALLOW; 12, default_parameter; 12, 13; 12, 14; 13, identifier:additional_fields; 14, None; 15, default_parameter; 15, 16; 15, 17; 16, identifier:order_fields; 17, None; 18, default_parameter; 18, 19; 18, 20; 19, identifier:calendar_view; 20, None; 21, default_parameter; 21, 22; 21, 23; 22, identifier:page_size; 23, None; 24, default_parameter; 24, 25; 24, 26; 25, identifier:max_items; 26, None; 27, default_parameter; 27, 28; 27, 29; 28, identifier:offset; 29, integer:0; 30, block; 30, 31; 30, 33; 30, 47; 30, 61; 30, 75; 30, 113; 30, 132; 30, 133; 30, 197; 30, 219; 30, 274; 31, expression_statement; 31, 32; 32, comment; 33, if_statement; 33, 34; 33, 37; 34, comparison_operator:not; 34, 35; 34, 36; 35, identifier:shape; 36, identifier:SHAPE_CHOICES; 37, block; 37, 38; 38, raise_statement; 38, 39; 39, call; 39, 40; 39, 41; 40, identifier:ValueError; 41, argument_list; 41, 42; 42, binary_operator:%; 42, 43; 42, 44; 43, string:"'shape' %s must be one of %s"; 44, tuple; 44, 45; 44, 46; 45, identifier:shape; 46, identifier:SHAPE_CHOICES; 47, if_statement; 47, 48; 47, 51; 48, comparison_operator:not; 48, 49; 48, 50; 49, identifier:depth; 50, identifier:ITEM_TRAVERSAL_CHOICES; 51, block; 51, 52; 52, raise_statement; 52, 53; 53, call; 53, 54; 53, 55; 54, identifier:ValueError; 55, argument_list; 55, 56; 56, binary_operator:%; 56, 57; 56, 58; 57, string:"'depth' %s must be one of %s"; 58, tuple; 58, 59; 58, 60; 59, identifier:depth; 60, identifier:ITEM_TRAVERSAL_CHOICES; 61, if_statement; 61, 62; 61, 66; 62, not_operator; 62, 63; 63, attribute; 63, 64; 63, 65; 64, identifier:self; 65, identifier:folders; 66, block; 66, 67; 66, 74; 67, expression_statement; 67, 68; 68, call; 68, 69; 68, 72; 69, attribute; 69, 70; 69, 71; 70, identifier:log; 71, identifier:debug; 72, argument_list; 72, 73; 73, string:'Folder list is empty'; 74, return_statement; 75, if_statement; 75, 76; 75, 77; 76, identifier:additional_fields; 77, block; 77, 78; 77, 91; 78, for_statement; 78, 79; 78, 80; 78, 81; 79, identifier:f; 80, identifier:additional_fields; 81, block; 81, 82; 82, expression_statement; 82, 83; 83, call; 83, 84; 83, 87; 84, attribute; 84, 85; 84, 86; 85, identifier:self; 86, identifier:validate_item_field; 87, argument_list; 87, 88; 88, keyword_argument; 88, 89; 88, 90; 89, identifier:field; 90, identifier:f; 91, for_statement; 91, 92; 91, 93; 91, 94; 92, identifier:f; 93, identifier:additional_fields; 94, block; 94, 95; 95, if_statement; 95, 96; 95, 101; 96, attribute; 96, 97; 96, 100; 97, attribute; 97, 98; 97, 99; 98, identifier:f; 99, identifier:field; 100, identifier:is_complex; 101, block; 101, 102; 102, raise_statement; 102, 103; 103, call; 103, 104; 103, 105; 104, identifier:ValueError; 105, argument_list; 105, 106; 106, binary_operator:%; 106, 107; 106, 108; 107, string:"find_items() does not support field '%s'. Use fetch() instead"; 108, attribute; 108, 109; 108, 112; 109, attribute; 109, 110; 109, 111; 110, identifier:f; 111, identifier:field; 112, identifier:name; 113, if_statement; 113, 114; 113, 124; 114, boolean_operator:and; 114, 115; 114, 118; 115, comparison_operator:is; 115, 116; 115, 117; 116, identifier:calendar_view; 117, None; 118, not_operator; 118, 119; 119, call; 119, 120; 119, 121; 120, identifier:isinstance; 121, argument_list; 121, 122; 121, 123; 122, identifier:calendar_view; 123, identifier:CalendarView; 124, block; 124, 125; 125, raise_statement; 125, 126; 126, call; 126, 127; 126, 128; 127, identifier:ValueError; 128, argument_list; 128, 129; 129, binary_operator:%; 129, 130; 129, 131; 130, string:"'calendar_view' %s must be a CalendarView instance"; 131, identifier:calendar_view; 132, comment; 133, if_statement; 133, 134; 133, 139; 133, 148; 133, 174; 134, call; 134, 135; 134, 138; 135, attribute; 135, 136; 135, 137; 136, identifier:q; 137, identifier:is_empty; 138, argument_list; 139, block; 139, 140; 139, 144; 140, expression_statement; 140, 141; 141, assignment; 141, 142; 141, 143; 142, identifier:restriction; 143, None; 144, expression_statement; 144, 145; 145, assignment; 145, 146; 145, 147; 146, identifier:query_string; 147, None; 148, elif_clause; 148, 149; 148, 152; 149, attribute; 149, 150; 149, 151; 150, identifier:q; 151, identifier:query_string; 152, block; 152, 153; 152, 157; 153, expression_statement; 153, 154; 154, assignment; 154, 155; 154, 156; 155, identifier:restriction; 156, None; 157, expression_statement; 157, 158; 158, assignment; 158, 159; 158, 160; 159, identifier:query_string; 160, call; 160, 161; 160, 162; 161, identifier:Restriction; 162, argument_list; 162, 163; 162, 164; 162, 169; 163, identifier:q; 164, keyword_argument; 164, 165; 164, 166; 165, identifier:folders; 166, attribute; 166, 167; 166, 168; 167, identifier:self; 168, identifier:folders; 169, keyword_argument; 169, 170; 169, 171; 170, identifier:applies_to; 171, attribute; 171, 172; 171, 173; 172, identifier:Restriction; 173, identifier:ITEMS; 174, else_clause; 174, 175; 175, block; 175, 176; 175, 193; 176, expression_statement; 176, 177; 177, assignment; 177, 178; 177, 179; 178, identifier:restriction; 179, call; 179, 180; 179, 181; 180, identifier:Restriction; 181, argument_list; 181, 182; 181, 183; 181, 188; 182, identifier:q; 183, keyword_argument; 183, 184; 183, 185; 184, identifier:folders; 185, attribute; 185, 186; 185, 187; 186, identifier:self; 187, identifier:folders; 188, keyword_argument; 188, 189; 188, 190; 189, identifier:applies_to; 190, attribute; 190, 191; 190, 192; 191, identifier:Restriction; 192, identifier:ITEMS; 193, expression_statement; 193, 194; 194, assignment; 194, 195; 194, 196; 195, identifier:query_string; 196, None; 197, expression_statement; 197, 198; 198, call; 198, 199; 198, 202; 199, attribute; 199, 200; 199, 201; 200, identifier:log; 201, identifier:debug; 202, argument_list; 202, 203; 202, 204; 202, 207; 202, 210; 202, 211; 202, 212; 202, 213; 203, string:'Finding %s items in folders %s (shape: %s, depth: %s, additional_fields: %s, restriction: %s)'; 204, attribute; 204, 205; 204, 206; 205, identifier:self; 206, identifier:folders; 207, attribute; 207, 208; 207, 209; 208, identifier:self; 209, identifier:account; 210, identifier:shape; 211, identifier:depth; 212, identifier:additional_fields; 213, conditional_expression:if; 213, 214; 213, 217; 213, 218; 214, attribute; 214, 215; 214, 216; 215, identifier:restriction; 216, identifier:q; 217, identifier:restriction; 218, None; 219, expression_statement; 219, 220; 220, assignment; 220, 221; 220, 222; 221, identifier:items; 222, call; 222, 223; 222, 241; 223, attribute; 223, 224; 223, 240; 224, call; 224, 225; 224, 226; 225, identifier:FindItem; 226, argument_list; 226, 227; 226, 232; 226, 237; 227, keyword_argument; 227, 228; 227, 229; 228, identifier:account; 229, attribute; 229, 230; 229, 231; 230, identifier:self; 231, identifier:account; 232, keyword_argument; 232, 233; 232, 234; 233, identifier:folders; 234, attribute; 234, 235; 234, 236; 235, identifier:self; 236, identifier:folders; 237, keyword_argument; 237, 238; 237, 239; 238, identifier:chunk_size; 239, identifier:page_size; 240, identifier:call; 241, argument_list; 241, 242; 241, 245; 241, 248; 241, 251; 241, 254; 241, 257; 241, 260; 241, 263; 241, 271; 242, keyword_argument; 242, 243; 242, 244; 243, identifier:additional_fields; 244, identifier:additional_fields; 245, keyword_argument; 245, 246; 245, 247; 246, identifier:restriction; 247, identifier:restriction; 248, keyword_argument; 248, 249; 248, 250; 249, identifier:order_fields; 250, identifier:order_fields; 251, keyword_argument; 251, 252; 251, 253; 252, identifier:shape; 253, identifier:shape; 254, keyword_argument; 254, 255; 254, 256; 255, identifier:query_string; 256, identifier:query_string; 257, keyword_argument; 257, 258; 257, 259; 258, identifier:depth; 259, identifier:depth; 260, keyword_argument; 260, 261; 260, 262; 261, identifier:calendar_view; 262, identifier:calendar_view; 263, keyword_argument; 263, 264; 263, 265; 264, identifier:max_items; 265, conditional_expression:if; 265, 266; 265, 269; 265, 270; 266, attribute; 266, 267; 266, 268; 267, identifier:calendar_view; 268, identifier:max_items; 269, identifier:calendar_view; 270, identifier:max_items; 271, keyword_argument; 271, 272; 271, 273; 272, identifier:offset; 273, identifier:offset; 274, if_statement; 274, 275; 274, 282; 274, 302; 275, boolean_operator:and; 275, 276; 275, 279; 276, comparison_operator:==; 276, 277; 276, 278; 277, identifier:shape; 278, identifier:ID_ONLY; 279, comparison_operator:is; 279, 280; 279, 281; 280, identifier:additional_fields; 281, None; 282, block; 282, 283; 283, for_statement; 283, 284; 283, 285; 283, 286; 284, identifier:i; 285, identifier:items; 286, block; 286, 287; 287, expression_statement; 287, 288; 288, yield; 288, 289; 289, conditional_expression:if; 289, 290; 289, 291; 289, 296; 290, identifier:i; 291, call; 291, 292; 291, 293; 292, identifier:isinstance; 293, argument_list; 293, 294; 293, 295; 294, identifier:i; 295, identifier:Exception; 296, call; 296, 297; 296, 300; 297, attribute; 297, 298; 297, 299; 298, identifier:Item; 299, identifier:id_from_xml; 300, argument_list; 300, 301; 301, identifier:i; 302, else_clause; 302, 303; 303, block; 303, 304; 304, for_statement; 304, 305; 304, 306; 304, 307; 305, identifier:i; 306, identifier:items; 307, block; 307, 308; 308, if_statement; 308, 309; 308, 314; 308, 318; 309, call; 309, 310; 309, 311; 310, identifier:isinstance; 311, argument_list; 311, 312; 311, 313; 312, identifier:i; 313, identifier:Exception; 314, block; 314, 315; 315, expression_statement; 315, 316; 316, yield; 316, 317; 317, identifier:i; 318, else_clause; 318, 319; 319, block; 319, 320; 320, expression_statement; 320, 321; 321, yield; 321, 322; 322, call; 322, 323; 322, 333; 323, attribute; 323, 324; 323, 332; 324, call; 324, 325; 324, 328; 325, attribute; 325, 326; 325, 327; 326, identifier:Folder; 327, identifier:item_model_from_tag; 328, argument_list; 328, 329; 329, attribute; 329, 330; 329, 331; 330, identifier:i; 331, identifier:tag; 332, identifier:from_xml; 333, argument_list; 333, 334; 333, 337; 334, keyword_argument; 334, 335; 334, 336; 335, identifier:elem; 336, identifier:i; 337, keyword_argument; 337, 338; 337, 339; 338, identifier:account; 339, attribute; 339, 340; 339, 341; 340, identifier:self; 341, identifier:account
def find_items(self, q, shape=ID_ONLY, depth=SHALLOW, additional_fields=None, order_fields=None, calendar_view=None, page_size=None, max_items=None, offset=0): """ Private method to call the FindItem service :param q: a Q instance containing any restrictions :param shape: controls whether to return (id, chanegkey) tuples or Item objects. If additional_fields is non-null, we always return Item objects. :param depth: controls the whether to return soft-deleted items or not. :param additional_fields: the extra properties we want on the return objects. Default is no properties. Be aware that complex fields can only be fetched with fetch() (i.e. the GetItem service). :param order_fields: the SortOrder fields, if any :param calendar_view: a CalendarView instance, if any :param page_size: the requested number of items per page :param max_items: the max number of items to return :param offset: the offset relative to the first item in the item collection :return: a generator for the returned item IDs or items """ if shape not in SHAPE_CHOICES: raise ValueError("'shape' %s must be one of %s" % (shape, SHAPE_CHOICES)) if depth not in ITEM_TRAVERSAL_CHOICES: raise ValueError("'depth' %s must be one of %s" % (depth, ITEM_TRAVERSAL_CHOICES)) if not self.folders: log.debug('Folder list is empty') return if additional_fields: for f in additional_fields: self.validate_item_field(field=f) for f in additional_fields: if f.field.is_complex: raise ValueError("find_items() does not support field '%s'. Use fetch() instead" % f.field.name) if calendar_view is not None and not isinstance(calendar_view, CalendarView): raise ValueError("'calendar_view' %s must be a CalendarView instance" % calendar_view) # Build up any restrictions if q.is_empty(): restriction = None query_string = None elif q.query_string: restriction = None query_string = Restriction(q, folders=self.folders, applies_to=Restriction.ITEMS) else: restriction = Restriction(q, folders=self.folders, applies_to=Restriction.ITEMS) query_string = None log.debug( 'Finding %s items in folders %s (shape: %s, depth: %s, additional_fields: %s, restriction: %s)', self.folders, self.account, shape, depth, additional_fields, restriction.q if restriction else None, ) items = FindItem(account=self.account, folders=self.folders, chunk_size=page_size).call( additional_fields=additional_fields, restriction=restriction, order_fields=order_fields, shape=shape, query_string=query_string, depth=depth, calendar_view=calendar_view, max_items=calendar_view.max_items if calendar_view else max_items, offset=offset, ) if shape == ID_ONLY and additional_fields is None: for i in items: yield i if isinstance(i, Exception) else Item.id_from_xml(i) else: for i in items: if isinstance(i, Exception): yield i else: yield Folder.item_model_from_tag(i.tag).from_xml(elem=i, account=self.account)