nodes: 0, module; 1, function_definition; 2, function_name:split_traversal; 3, parameters; 4, block; 5, identifier:traversal; 6, identifier:edges; 7, default_parameter; 8, expression_statement; 9, expression_statement; 10, comment:# hash edge rows for contains checks; 11, if_statement; 12, comment:# turn the (n,) traversal into (n-1,2) edges; 13, expression_statement; 14, comment:# hash each edge so we can compare to edge set; 15, expression_statement; 16, comment:# check if each edge is contained in edge set; 17, expression_statement; 18, comment:# exit early if every edge of traversal exists; 19, if_statement; 20, comment:# close traversals if necessary; 21, for_statement; 22, expression_statement; 23, return_statement; 24, identifier:edges_hash; 25, None; 26, comment:"""
      Given a traversal as a list of nodes, split the traversal
      if a sequential index pair is not in the given edges.
  
      Parameters
      --------------
      edges : (n, 2) int
         Graph edge indexes
      traversal : (m,) int
         Traversal through edges
      edge_hash : (n,)
         Edges sorted on axis=1 and
         passed to grouping.hashable_rows
  
      Returns
      ---------------
      split : sequence of (p,) int
      """; 27, assignment; 28, comparison_operator:edges_hash is None; 29, block; 30, assignment; 31, assignment; 32, assignment; 33, call; 34, comment:# just reshape one traversal; 35, block; 36, else_clause; 37, pattern_list; 38, call; 39, comment:# make sure elements of sequence are numpy arrays; 40, block; 41, assignment; 42, identifier:result; 43, identifier:traversal; 44, call; 45, identifier:edges_hash; 46, None; 47, expression_statement; 48, identifier:trav_edge; 49, call; 50, identifier:trav_hash; 51, call; 52, identifier:contained; 53, call; 54, attribute; 55, argument_list; 56, expression_statement; 57, comment:# find contiguous groups of contained edges; 58, block; 59, identifier:i; 60, identifier:t; 61, identifier:enumerate; 62, argument_list; 63, expression_statement; 64, comment:# don't close if its a single edge; 65, if_statement; 66, comment:# make sure it's not already closed; 67, expression_statement; 68, if_statement; 69, expression_statement; 70, comment:# if we need the edge add it; 71, if_statement; 72, identifier:result; 73, call; 74, attribute; 75, argument_list; 76, assignment; 77, attribute; 78, argument_list; 79, attribute; 80, argument_list; 81, attribute; 82, argument_list; 83, identifier:contained; 84, identifier:all; 85, assignment; 86, expression_statement; 87, comment:# turn edges back in to sequence of traversals; 88, expression_statement; 89, identifier:split; 90, assignment; 91, comparison_operator:len(t) <= 2; 92, block; 93, assignment; 94, comparison_operator:edge.ptp() == 0; 95, block; 96, assignment; 97, comparison_operator:close in edges_hash; 98, block; 99, attribute; 100, argument_list; 101, identifier:np; 102, identifier:asanyarray; 103, identifier:traversal; 104, keyword_argument; 105, identifier:edges_hash; 106, call; 107, identifier:np; 108, identifier:column_stack; 109, tuple; 110, identifier:grouping; 111, identifier:hashable_rows; 112, call; 113, identifier:np; 114, identifier:in1d; 115, identifier:trav_hash; 116, identifier:edges_hash; 117, identifier:split; 118, list; 119, assignment; 120, assignment; 121, subscript; 122, call; 123, call; 124, integer:2; 125, continue_statement; 126, identifier:edge; 127, call; 128, call; 129, integer:0; 130, continue_statement; 131, identifier:close; 132, subscript; 133, identifier:close; 134, identifier:edges_hash; 135, expression_statement; 136, identifier:np; 137, identifier:array; 138, identifier:split; 139, identifier:dtype; 140, attribute; 141, attribute; 142, argument_list; 143, subscript; 144, subscript; 145, attribute; 146, argument_list; 147, identifier:traversal; 148, identifier:blocks; 149, call; 150, identifier:split; 151, list_comprehension; 152, identifier:split; 153, identifier:i; 154, attribute; 155, argument_list; 156, identifier:len; 157, argument_list; 158, attribute; 159, argument_list; 160, attribute; 161, argument_list; 162, call; 163, integer:0; 164, assignment; 165, identifier:np; 166, identifier:int64; 167, identifier:grouping; 168, identifier:hashable_rows; 169, call; 170, identifier:traversal; 171, slice; 172, identifier:traversal; 173, slice; 174, identifier:np; 175, identifier:sort; 176, identifier:trav_edge; 177, keyword_argument; 178, attribute; 179, argument_list; 180, call; 181, for_in_clause; 182, identifier:np; 183, identifier:asanyarray; 184, subscript; 185, keyword_argument; 186, identifier:t; 187, identifier:np; 188, identifier:sort; 189, list; 190, identifier:edge; 191, identifier:ptp; 192, attribute; 193, argument_list; 194, subscript; 195, call; 196, attribute; 197, argument_list; 198, unary_operator; 199, integer:1; 200, identifier:axis; 201, integer:1; 202, identifier:grouping; 203, identifier:blocks; 204, identifier:contained; 205, keyword_argument; 206, keyword_argument; 207, attribute; 208, argument_list; 209, identifier:b; 210, identifier:blocks; 211, identifier:split; 212, identifier:i; 213, identifier:dtype; 214, attribute; 215, subscript; 216, subscript; 217, identifier:grouping; 218, identifier:hashable_rows; 219, call; 220, identifier:split; 221, identifier:i; 222, attribute; 223, argument_list; 224, identifier:np; 225, identifier:sort; 226, identifier:edges; 227, keyword_argument; 228, integer:1; 229, identifier:min_len; 230, integer:1; 231, identifier:only_nonzero; 232, True; 233, identifier:np; 234, identifier:append; 235, subscript; 236, subscript; 237, identifier:np; 238, identifier:int64; 239, identifier:t; 240, integer:0; 241, identifier:t; 242, unary_operator; 243, attribute; 244, argument_list; 245, call; 246, identifier:astype; 247, attribute; 248, identifier:axis; 249, integer:1; 250, subscript; 251, slice; 252, integer:0; 253, subscript; 254, integer:1; 255, integer:1; 256, identifier:edge; 257, identifier:reshape; 258, tuple; 259, attribute; 260, argument_list; 261, identifier:np; 262, identifier:int64; 263, identifier:trav_edge; 264, identifier:b; 265, identifier:trav_edge; 266, subscript; 267, integer:1; 268, integer:2; 269, identifier:np; 270, identifier:append; 271, identifier:t; 272, subscript; 273, identifier:b; 274, unary_operator; 275, identifier:t; 276, integer:0; 277, integer:1
code: def split_traversal(traversal,
                      edges,
                      edges_hash=None):
      """
      Given a traversal as a list of nodes, split the traversal
      if a sequential index pair is not in the given edges.
  
      Parameters
      --------------
      edges : (n, 2) int
         Graph edge indexes
      traversal : (m,) int
         Traversal through edges
      edge_hash : (n,)
         Edges sorted on axis=1 and
         passed to grouping.hashable_rows
  
      Returns
      ---------------
      split : sequence of (p,) int
      """
      traversal = np.asanyarray(traversal,
                                dtype=np.int64)
  
      # hash edge rows for contains checks
      if edges_hash is None:
          edges_hash = grouping.hashable_rows(
              np.sort(edges, axis=1))
  
      # turn the (n,) traversal into (n-1,2) edges
      trav_edge = np.column_stack((traversal[:-1],
                                   traversal[1:]))
      # hash each edge so we can compare to edge set
      trav_hash = grouping.hashable_rows(
          np.sort(trav_edge, axis=1))
      # check if each edge is contained in edge set
      contained = np.in1d(trav_hash, edges_hash)
  
      # exit early if every edge of traversal exists
      if contained.all():
          # just reshape one traversal
          split = [traversal]
      else:
          # find contiguous groups of contained edges
          blocks = grouping.blocks(contained,
                                   min_len=1,
                                   only_nonzero=True)
  
          # turn edges back in to sequence of traversals
          split = [np.append(trav_edge[b][:, 0],
                             trav_edge[b[-1]][1])
                   for b in blocks]
  
      # close traversals if necessary
      for i, t in enumerate(split):
          # make sure elements of sequence are numpy arrays
          split[i] = np.asanyarray(split[i], dtype=np.int64)
          # don't close if its a single edge
          if len(t) <= 2:
              continue
          # make sure it's not already closed
          edge = np.sort([t[0], t[-1]])
          if edge.ptp() == 0:
              continue
          close = grouping.hashable_rows(edge.reshape((1, 2)))[0]
          # if we need the edge add it
          if close in edges_hash:
              split[i] = np.append(t, t[0]).astype(np.int64)
      result = np.array(split)
  
      return result