Exemplo n.º 1
0
    def __init__(self, generator, ids, targets=None, shuffle=True):
        # Check that ids is an iterable
        if not is_real_iterable(ids):
            raise TypeError(
                "IDs must be an iterable or numpy array of graph node IDs")

        # Check targets is iterable & has the correct length
        if targets is not None:
            if not is_real_iterable(targets):
                raise TypeError(
                    "Targets must be None or an iterable or numpy array ")
            if len(ids) != len(targets):
                raise ValueError(
                    "The length of the targets must be the same as the length of the ids"
                )
            self.targets = np.asanyarray(targets)
        else:
            self.targets = None

        # Ensure number of labels matches number of ids
        if targets is not None and len(ids) != len(targets):
            raise ValueError(
                "Length of link ids must match length of link targets")

        if (isinstance(generator, GraphSAGELinkGenerator)
                or isinstance(generator, HinSAGELinkGenerator)
                or isinstance(generator, Attri2VecLinkGenerator)):
            self.generator = generator
        else:
            raise TypeError(
                "({}) GraphSAGELinkGenerator, HinSAGELinkGenerator or Attri2VecLinkGenerator is required."
                .format(type(self).__name__))

        self.ids = list(ids)
        self.data_size = len(self.ids)
        self.shuffle = shuffle

        # Shuffle the IDs to begin
        self.on_epoch_end()

        if isinstance(self.generator, GraphSAGELinkGenerator) or isinstance(
                self.generator, HinSAGELinkGenerator):
            # Get head node types from all src, dst nodes extracted from all links,
            # and make sure there's only one pair of node types:
            self.head_node_types = self._infer_head_node_types(
                generator.schema)

            self._sampling_schema = generator.schema.sampling_layout(
                self.head_node_types, generator.num_samples)

            self.type_adjacency_list = generator.schema.type_adjacency_list(
                self.head_node_types, len(generator.num_samples))
    def __init__(
        self,
        G,
        nodes=None,
        length=2,
        number_of_walks=1,
        seed=None,
        walker=None,
    ):
        if not isinstance(G, StellarGraph):
            raise ValueError(
                "({}) Graph must be a StellarGraph or StellarDigraph object.".
                format(type(self).__name__))
        else:
            self.graph = G

        # Instantiate the walker class used to generate random walks in the graph
        if walker is not None:
            _warn_if_ignored(length, 2, "length")
            _warn_if_ignored(number_of_walks, 1, "number_of_walks")
            _warn_if_ignored(seed, None, "seed")
            self.walker = walker
        else:
            self.walker = UniformRandomWalk(G,
                                            n=number_of_walks,
                                            length=length,
                                            seed=seed)

        # Define the root nodes for the walks
        # if no root nodes are provided for sampling defaulting to using all nodes as root nodes.
        if nodes is None:
            self.nodes = list(G.nodes())
        elif is_real_iterable(
                nodes):  # check whether the nodes provided are valid.
            self.nodes = list(nodes)
        else:
            raise ValueError(
                "nodes parameter should be an iterableof node IDs.")

        # Require walks of at lease length two because to create a sample pair we need at least two nodes.
        if length < 2:
            raise ValueError(
                "({}) For generating (target,context) samples, walk length has to be at least 2"
                .format(type(self).__name__))
        else:
            self.length = length

        if number_of_walks < 1:
            raise ValueError(
                "({}) At least 1 walk from each head node has to be done".
                format(type(self).__name__))
        else:
            self.number_of_walks = number_of_walks

        # Setup an interal random state with the given seed
        _, self.np_random = random_state(seed)
Exemplo n.º 3
0
    def __init__(self, G, nodes=None, length=2, number_of_walks=1, seed=None):
        if not isinstance(G, StellarGraphBase):
            raise ValueError(
                "({}) Graph must be a StellarGraph object.".format(
                    type(self).__name__))
        else:
            self.graph = G

        # Instantiate the walker class used to generate random walks in the graph
        self.walker = UniformRandomWalk(G, seed=seed)

        # This code will enable alternative walker classes
        # TODO: Enable this code, but figure out how to pass required options to run
        # if walker is not None:
        #     if not isinstance(
        #         walker, UniformRandomWalk
        #     ):  # only work with Uniform Random Walker at the moment
        #         raise TypeError(
        #             "({}) Only Uniform Random Walks are possible".format(
        #                 type(self).__name__
        #             )
        #         )
        #     else:
        #         self.walker = walker(G, seed=seed)
        # else:
        #         self.walker = UniformRandomWalk(G, seed=seed)

        # Define the root nodes for the walks
        # if no root nodes are provided for sampling defaulting to using all nodes as root nodes.
        if nodes is None:
            self.nodes = list(G.nodes())
        elif is_real_iterable(
                nodes):  # check whether the nodes provided are valid.
            self.nodes = list(nodes)
        else:
            raise ValueError(
                "nodes parameter should be an iterableof node IDs.")

        # Require walks of at lease length two because to create a sample pair we need at least two nodes.
        if length < 2:
            raise ValueError(
                "({}) For generating (target,context) samples, walk length has to be at least 2"
                .format(type(self).__name__))
        else:
            self.length = length

        if number_of_walks < 1:
            raise ValueError(
                "({}) At least 1 walk from each head node has to be done".
                format(type(self).__name__))
        else:
            self.number_of_walks = number_of_walks

        # Setup an interal random state with the given seed
        self.random = random.Random(seed)
 def _check_nodes(self, nodes):
     if nodes is None:
         self._raise_error("A list of root node IDs was not provided.")
     if not is_real_iterable(nodes):
         self._raise_error(
             "Nodes parameter should be an iterable of node IDs.")
     if (
             len(nodes) == 0
     ):  # this is not an error but maybe a warning should be printed to inform the caller
         warnings.warn(
             "No root node IDs given. An empty list will be returned as a result.",
             RuntimeWarning,
             stacklevel=3,
         )