class_docstr: A data generator for link prediction with Heterogeneous HinSAGE models
  
  At minimum, supply the StellarGraph, the batch size, and the number of
  node samples for each layer of the GraphSAGE model.
  
  The supplied graph should be a StellarGraph object with node features for all node types.
  
  Use the :meth:`flow` method supplying the nodes and (optionally) targets
  to get an object that can be used as a Keras data generator.
  
  The generator should be given the ``(src,dst)`` node types using
  
  * It's possible to do link prediction on a graph where that link type is completely removed from the graph
    (e.g., "same_as" links in ER)
  
  .. seealso::
  
     Model using this generator: :class:`.HinSAGE`.
  
     Example using this generator: `link prediction <https://stellargraph.readthedocs.io/en/stable/demos/link-prediction/hinsage-link-prediction.html>`__.
  
     Related functionality:
  
     - :class:`.UnsupervisedSampler` for unsupervised training using random walks
     - :class:`.HinSAGENodeGenerator` for node classification and related tasks
     - :class:`.GraphSAGELinkGenerator` for homogeneous graphs
     - :class:`.DirectedGraphSAGELinkGenerator` for directed homogeneous graphs
  
  Args:
      g (StellarGraph): A machine-learning ready graph.
      batch_size (int): Size of batch of links to return.
      num_samples (list): List of number of neighbour node samples per GraphSAGE layer (hop) to take.
      head_node_types (list, optional): List of the types (str) of the two head nodes forming the
          node pair. This does not need to be specified if ``G`` has only one node type.
      seed (int or str, optional): Random seed for the sampling methods.
  
  Example::
  
      G_generator = HinSAGELinkGenerator(G, 50, [10,10])
      data_gen = G_generator.flow(edge_ids)
class_docstr_tok: ["A","data","generator","for","link","prediction","with","Heterogeneous","HinSAGE","models","At","minimum",",","supply","the","StellarGraph",",","the","batch","size",",","and","the","number","of","node","samples","for","each","layer","of","the","GraphSAGE","model",".","The","supplied","graph","should","be","a","StellarGraph","object","with","node","features","for","all","node","types",".","Use","the",":","meth",":","`","flow","`","method","supplying","the","nodes","and","(","optionally",")","targets","to","get","an","object","that","can","be","used","as","a","Keras","data","generator",".","The","generator","should","be","given","the","``","(","src",",","dst",")","``","node","types","using","*","It","'s","possible","to","do","link","prediction","on","a","graph","where","that","link","type","is","completely","removed","from","the","graph","(","e.g.",",","``","same_as","''","links","in","ER",")","..","seealso",":",":","Model","using","this","generator",":",":","class",":","`",".HinSAGE","`",".","Example","using","this","generator",":","`","link","prediction","<","https",":","\/\/stellargraph.readthedocs.io\/en\/stable\/demos\/link-prediction\/hinsage-link-prediction.html",">","`","__",".","Related","functionality",":","-",":","class",":","`",".UnsupervisedSampler","`","for","unsupervised","training","using","random","walks","-",":","class",":","`",".HinSAGENodeGenerator","`","for","node","classification","and","related","tasks","-",":","class",":","`",".GraphSAGELinkGenerator","`","for","homogeneous","graphs","-",":","class",":","`",".DirectedGraphSAGELinkGenerator","`","for","directed","homogeneous","graphs","Args",":","g","(","StellarGraph",")",":","A","machine-learning","ready","graph",".","batch_size","(","int",")",":","Size","of","batch","of","links","to","return",".","num_samples","(","list",")",":","List","of","number","of","neighbour","node","samples","per","GraphSAGE","layer","(","hop",")","to","take",".","head_node_types","(","list",",","optional",")",":","List","of","the","types","(","str",")","of","the","two","head","nodes","forming","the","node","pair",".","This","does","not","need","to","be","specified","if","``","G","``","has","only","one","node","type",".","seed","(","int","or","str",",","optional",")",":","Random","seed","for","the","sampling","methods",".","Example",":",":","G_generator","=","HinSAGELinkGenerator","(","G",",","50",",","[","10,10","]",")","data_gen","=","G_generator.flow","(","edge_ids",")"]
docstr: Sample neighbours recursively from the head nodes, collect the features of the
  sampled nodes, and return these as a list of feature arrays for the GraphSAGE
  algorithm.
  
  Args:
      head_links (list): An iterable of edges to perform sampling for.
      batch_num (int): Batch number
  
  Returns:
      A list of the same length as `num_samples` of collected features from
      the sampled nodes of shape: ``(len(head_nodes), num_sampled_at_layer, feature_size)``
      where ``num_sampled_at_layer`` is the cumulative product of `num_samples`
      for that layer.
code: def sample_features(self, head_links, batch_num):
      """
      Sample neighbours recursively from the head nodes, collect the features of the
      sampled nodes, and return these as a list of feature arrays for the GraphSAGE
      algorithm.
  
      Args:
          head_links (list): An iterable of edges to perform sampling for.
          batch_num (int): Batch number
  
      Returns:
          A list of the same length as `num_samples` of collected features from
          the sampled nodes of shape: ``(len(head_nodes), num_sampled_at_layer, feature_size)``
          where ``num_sampled_at_layer`` is the cumulative product of `num_samples`
          for that layer.
      """
      nodes_by_type = []
      for ii in range(2):
          # Extract head nodes from edges: each edge is a tuple of 2 nodes, so we are extracting 2 head nodes per edge
          head_nodes = [e[ii] for e in head_links]
  
          # Get sampled nodes for the subgraphs starting from the (src, dst) head nodes
          # nodes_samples is list of two lists: [[samples for src], [samples for dst]]
          node_samples = self.sampler.run(
              nodes=head_nodes, n=1, n_size=self.num_samples
          )
  
          # Reshape node samples to the required format for the HinSAGE model
          # This requires grouping the sampled nodes by edge type and in order
          nodes_by_type.append(
              [
                  (
                      nt,
                      reduce(
                          operator.concat,
                          (
                              samples[ks]
                              for samples in node_samples
                              for ks in indices
                          ),
                          [],
                      ),
                  )
                  for nt, indices in self._sampling_schema[ii]
              ]
          )
  
      # Interlace the two lists, nodes_by_type[0] (for src head nodes) and nodes_by_type[1] (for dst head nodes)
      nodes_by_type = [
          tuple(
              (ab[0][0], reduce(operator.concat, (ab[0][1], ab[1][1])))
          )
          for ab in zip(nodes_by_type[0], nodes_by_type[1])
      ]
  
      batch_feats = self._get_features(
          nodes_by_type, len(head_links), use_ilocs=True
      )
  
      return batch_feats
  
imports: import random
  import operator
  import numpy
  import itertools
  import operator
  import collections
  import abc
  import warnings
  from functools import reduce
  from tensorflow import keras
  from ..core.graph import StellarGraph, GraphSchema
  from ..data import SampledBreadthFirstWalk, SampledHeterogeneousBreadthFirstWalk, UniformRandomWalk, UnsupervisedSampler, DirectedBreadthFirstNeighbours
  from ..core.utils import is_real_iterable
  from .None import LinkSequence, OnDemandLinkSequence
  from ..random import SeededPerBatch
  from .base import Generator
label_desc: Use image node_id 3 for calling the HinSAGELinkGenerator obj's underlying member method code with example usage: obj.sample_features(head_links, batch_num) and returns: batch_feats