### Install pyRDF2Vec from Source Source: https://pyrdf2vec.readthedocs.io/en/latest/readme.html Clone the pyRDF2Vec repository from GitHub and install it locally. This method is useful for development or when you need the latest unreleased version. ```bash git clone https://github.com/IBCNServices/pyRDF2Vec.git pip install . ``` -------------------------------- ### Example Changelog Entry: New Feature Source: https://pyrdf2vec.readthedocs.io/en/latest/contributing.html An example of a changelog entry for a new feature, highlighting the addition of 'wallkers.Foo'. ```rst Added ``wallkers.Foo``. This walker is a new walker. ``` -------------------------------- ### Install pyRDF2Vec Dependencies with Poetry Source: https://pyrdf2vec.readthedocs.io/en/latest/contributing.html Install the project's development dependencies using Poetry. This command should be run after installing Poetry itself. ```bash poetry install ``` -------------------------------- ### Activate Poetry Virtual Environment Source: https://pyrdf2vec.readthedocs.io/en/latest/contributing.html After installing dependencies with Poetry, activate the virtual environment to work within it. ```bash poetry shell ``` -------------------------------- ### Example Changelog Entry: Improved Functionality Source: https://pyrdf2vec.readthedocs.io/en/latest/contributing.html An example of a changelog entry for improved functionality, noting that 'fit_transform()' now handles larger Knowledge Graph Embeddings (KGE). ```rst ``fit_transform()`` now can deal with bigger Knowledge Graph Embeddings (KGE). ``` -------------------------------- ### Install Poetry Package Manager Source: https://pyrdf2vec.readthedocs.io/en/latest/contributing.html Install the Poetry dependency management tool using pip. This is a prerequisite for managing pyRDF2Vec dependencies. ```bash pip install poetry ``` -------------------------------- ### Install pyRDF2Vec via pip Source: https://pyrdf2vec.readthedocs.io/en/latest/readme.html Install the pyRDF2Vec library using pip, the standard Python package installer. This is the most common method for adding the library to your project. ```bash pip install pyrdf2vec ``` -------------------------------- ### Preview Changelog Entry with towncrier Source: https://pyrdf2vec.readthedocs.io/en/latest/contributing.html Run this command to preview how your changelog entry will appear in the final release notes. Ensure you have tox installed. ```bash tox -e changelog ``` -------------------------------- ### Define Walking Strategy with Sampling Source: https://pyrdf2vec.readthedocs.io/en/latest/readme.html Configure walking strategies with sampling to manage walk extraction from large knowledge graphs. This example sets up a RandomWalker with a depth of 4, 10 walks per entity, and PageRank sampling. ```python from pyrdf2vec.samplers import PageRankSampler from pyrdf2vec.walkers import RandomWalker walkers = [RandomWalker(4, 10, PageRankSampler())] ``` -------------------------------- ### Run pyRDF2Vec with Docker Compose Source: https://pyrdf2vec.readthedocs.io/en/latest/contributing.html Use Docker Compose to build and run the pyRDF2Vec development environment. This avoids local dependency installation. ```bash docker-compose up --build -d ``` -------------------------------- ### Install pyRDF2Vec via Poetry Source: https://pyrdf2vec.readthedocs.io/en/latest/readme.html Add pyRDF2Vec as a dependency to your project managed by Poetry. This ensures proper dependency management within your Python environment. ```bash poetry add pyrdf2vec ``` -------------------------------- ### Generate Documentation Locally Source: https://pyrdf2vec.readthedocs.io/en/latest/contributing.html Build the project's documentation locally using tox. ```bash tox -e docs ``` -------------------------------- ### Create KG from Scratch Source: https://pyrdf2vec.readthedocs.io/en/latest/readme.html Construct a knowledge graph from scratch by adding walks. This is useful for custom or small datasets. Ensure to import Vertex and KG classes. ```python from pyrdf2vec.graphs import KG, Vertex GRAPH = [ ["Alice", "knows", "Bob"], ["Alice", "knows", "Dean"], ["Dean", "loves", "Alice"], ] URL = "http://pyRDF2Vec" CUSTOM_KG = KG() for row in GRAPH: subj = Vertex(f"{URL}#{row[0]}") obj = Vertex((f"{URL}#{row[2]}")) pred = Vertex((f"{URL}#{row[1]}"), predicate=True, vprev=subj, vnext=obj) CUSTOM_KG.add_walk(subj, pred, obj) ``` -------------------------------- ### Check Code Style and Documentation Source: https://pyrdf2vec.readthedocs.io/en/latest/contributing.html Ensure code style and documentation correctness using tox. ```bash tox -e lint,docs ``` -------------------------------- ### Import New Sampler in __init__.py Source: https://pyrdf2vec.readthedocs.io/en/latest/contributing.html When adding a new sampling strategy, import it in the samplers' __init__.py file and include it in the __all__ list. ```python from .sampler import Sampler # ... from .foo import FooSampler # ... from .uniform import UniformSampler __all__ = [ # ... "FooSampler", # ... "Sampler", "UniformSampler", ] ``` -------------------------------- ### get_neighbors Source: https://pyrdf2vec.readthedocs.io/en/latest/api/pyrdf2vec.graphs.html Gets the children or parent neighbors of a given vertex. ```APIDOC ## get_neighbors ### Description Gets the children or parents neighbors of a vertex. ### Parameters - **vertex** (Vertex): The vertex. - **is_reverse** (bool, optional): If True, this function gets the parent nodes of a vertex. Otherwise, get the child nodes for this vertex. Defaults to False. ### Returns - **Set[Vertex]**: The children or parents neighbors of a vertex. ``` -------------------------------- ### Run Lint and Docs Check Source: https://pyrdf2vec.readthedocs.io/en/latest/contributing.html Ensure code style and documentation are correct by running tox with lint and docs environments. ```bash tox -e lint,docs ``` -------------------------------- ### PageRankSampler.get_weight Method Source: https://pyrdf2vec.readthedocs.io/en/latest/api/pyrdf2vec.samplers.pagerank.html Gets the weight of a hop in the Knowledge Graph. ```APIDOC def get_weight(hop): """Gets the weight of a hop in the Knowledge Graph. Args: hop (Tuple[Any, Any]): The hop of a vertex in a (predicate, object) form to get the weight. Returns: float: The weight of a given hop. Raises: ValueError: If there is an attempt to access the weight of a hop without the sampling strategy having been trained. """ pass ``` -------------------------------- ### RDF2VecTransformer.get_walks Source: https://pyrdf2vec.readthedocs.io/en/latest/api/pyrdf2vec.rdf2vec.html Gets the walks of an entity based on a Knowledge Graph and a list of walkers. ```APIDOC ## get_walks ### Description Gets the walks of an entity based on a Knowledge Graph and a list of walkers. ### Parameters - **kg** (KG) - The Knowledge Graph. - **entities** (List[str]) - The entities including test entities to create the embeddings. Since RDF2Vec is unsupervised, there is no label leakage. ### Returns - **List[List[Tuple[str, ...]]])** - The walks for the given entities. ### Raises - **ValueError** - If the provided entities aren’t in the Knowledge Graph. ``` -------------------------------- ### WideSampler.get_weight Method Source: https://pyrdf2vec.readthedocs.io/en/latest/api/pyrdf2vec.samplers.wide.html Gets the weight of a hop in the Knowledge Graph based on the fitted sampling strategy. ```APIDOC ## get_weight ### Description Gets the weight of a hop in the Knowledge Graph. ### Parameters - **hop** (Tuple[Any, Any]) - The hop of a vertex in a (predicate, object) form to get the weight. ### Return Type float ### Returns The weight of a given hop. ### Raises - **ValueError** - If there is an attempt to access the weight of a hop without the sampling strategy having been trained. ``` -------------------------------- ### Initialize KG from SPARQL Endpoint Source: https://pyrdf2vec.readthedocs.io/en/latest/readme.html Use this method to load a knowledge graph from a SPARQL endpoint. Specify the endpoint URL, predicates to skip, and predicate chains for fetching literals. ```python from pyrdf2vec.graphs import KG # Defined the DBpedia endpoint server, as well as a set of predicates to # exclude from this KG and a list of predicate chains to fetch the literals. KG( "https://dbpedia.org/sparql", skip_predicates={"www.w3.org/1999/02/22-rdf-syntax-ns#type"}, literals=[ [ "http://dbpedia.org/ontology/wikiPageWikiLink", "http://www.w3.org/2004/02/skos/core#prefLabel", ], ["http://dbpedia.org/ontology/humanDevelopmentIndex"], ], ), ``` -------------------------------- ### Import New Walker in __init__.py Source: https://pyrdf2vec.readthedocs.io/en/latest/contributing.html Import your custom walker class and add it to the __all__ list in pyrdf2vec/walkers/__init__.py. ```python from .walker import Walker # ... from .foo import FooWalker # ... from .walklet import WalkletWalker from .weisfeiler_lehman import WLWalker __all__ = [ # ... "FooWalker", # ... "Walker", "WalkletWalker", "WLWalker", ] ``` -------------------------------- ### Initialize KG from RDFLib File Source: https://pyrdf2vec.readthedocs.io/en/latest/readme.html Load a knowledge graph from a file using RDFLib. This method requires specifying the file path, predicates to exclude, and predicate chains for literals. ```python from pyrdf2vec.graphs import KG # Defined the MUTAG KG, as well as a set of predicates to exclude from # this KG and a list of predicate chains to get the literals. KG( "samples/mutag/mutag.owl", skip_predicates={"http://dl-learner.org/carcinogenesis#isMutagenic"}, literals=[ [ "http://dl-learner.org/carcinogenesis#hasBond", "http://dl-learner.org/carcinogenesis#inBond", ], [ "http://dl-learner.org/carcinogenesis#hasAtom", "http://dl-learner.org/carcinogenesis#charge", ], ], ), ``` -------------------------------- ### Check Documentation Code Style Source: https://pyrdf2vec.readthedocs.io/en/latest/contributing.html Verify the code style of the documentation using tox. ```bash tox -e lint ``` -------------------------------- ### Run Unit Tests with Tox Source: https://pyrdf2vec.readthedocs.io/en/latest/contributing.html Execute unit tests and code style checks using the tox command. Ensure all tests pass and code style is maintained before submitting changes. ```bash tox ``` -------------------------------- ### KG Class Initialization Source: https://pyrdf2vec.readthedocs.io/en/latest/api/pyrdf2vec.graphs.html Initializes a Knowledge Graph object. It can load data from a given location, with options to skip predicates, include literals, specify if the graph is remote, and configure various parameters for data fetching and verification. ```APIDOC ## KG Class ### Description Represents a Knowledge Graph. ### Parameters - **location** (str, optional): The location of the file to load. Defaults to None. - **skip_predicates** (set, optional): The label predicates to skip from the KG. Defaults to set. - **literals** (list, optional): The predicate chains to get the literals. Defaults to []. - **is_remote** (bool, optional): True if the Knowledge Graph is in remote, False otherwise. Defaults to False. - **fmt** (str, optional): The format of the file. Defaults to None. - **mul_req** (bool, optional): True to allow bundling of SPARQL queries, False otherwise. Defaults to False. - **skip_verify** (bool, optional): To skip or not the verification of existing entities in a Knowledge Graph. Defaults to False. - **cache** (object, optional): The policy and size cache to use. Defaults to TTLCache(maxsize=1024, ttl=1200). ### Attributes - **entity_hops**: Caches the results of asynchronous requests. Defaults to {}. - **entities**: Stores the entities. Defaults to set. - **vertices**: Stores the vertices. Defaults to set. - **inv_transition_matrix**: Contains the parents of vertices. Defaults to defaultdict. - **transition_matrix**: Contains the children of vertices. Defaults to defaultdict. - **cache**: The policy and size cache to use. Defaults to TTLCache(maxsize=1024, ttl=1200). - **connector**: The connector to use. Defaults to SPARQLConnector. - **fmt**: The format of the file. Defaults to None. - **literals**: The predicate chains to get the literals. Defaults to []. - **location**: The location of the file to load. Defaults to None. - **mul_req**: True to allow bundling of SPARQL queries, False otherwise. Defaults to False. - **skip_predicates**: The label predicates to skip from the KG. Defaults to set. - **skip_verify**: To skip or not the verification of existing entities in a Knowledge Graph. Defaults to False. ``` -------------------------------- ### KG Class Initialization Source: https://pyrdf2vec.readthedocs.io/en/latest/api/pyrdf2vec.graphs.kg.html Initializes a Knowledge Graph object. It can be configured with various parameters to control how the graph is loaded and processed, including remote access, predicate skipping, literal extraction, and caching. ```APIDOC ## KG Class ### Description Represents a Knowledge Graph. ### Parameters - **location** (`str`, optional): The location of the file to load. Defaults to None. - **skip_predicates** (`Set[str]`, optional): The label predicates to skip from the KG. Defaults to set. - **literals** (`List[str]`, optional): The predicate chains to get the literals. Defaults to []. - **is_remote** (`bool`, optional): True if the Knowledge Graph is in remote, False otherwise. Defaults to False. - **fmt** (`str`, optional): The format of the file. It should be used only if the format can not be determined from source. Defaults to None. - **mul_req** (`bool`, optional): True to allow bundling of SPARQL queries, False otherwise. This attribute accelerates the extraction of walks for remote Knowledge Graphs. Beware that this may violate the policy of some SPARQL endpoint server. Defaults to False. - **skip_verify** (`bool`, optional): To skip or not the verification of existing entities in a Knowledge Graph. Its deactivation can improve HTTP latency for KG remotes. Defaults to False. - **cache** (`Cache`, optional): The policy and size cache to use. Defaults to TTLCache(maxsize=1024, ttl=1200). ``` -------------------------------- ### Implement Custom Sampler Class Source: https://pyrdf2vec.readthedocs.io/en/latest/contributing.html Extend the Sampler class to create a new sampling strategy. Implement the fit and get_weight methods. ```python import attr from pyrdf2vec.graph import KG from pyrdf2vec.samplers import Sampler from pyrdf2vec.typings import Hop @attr.s class FooSampler(Sampler): """Defines the Foo sampling strategy.""" def fit(self, kg: KG) -> None: """Since the weights are uniform, this function does nothing. Args: kg: The Knowledge Graph. """ # TODO: to be implemented def get_weight(self, hop: Hop) -> int: """Gets the weight of a hop in the Knowledge Graph. Args: hop: The hop (pred, obj) to get the weight. Returns: The weight for a given hop. """ # TODO: to be implemented ``` -------------------------------- ### Import New Embedder in __init__.py Source: https://pyrdf2vec.readthedocs.io/en/latest/contributing.html Import your custom embedder class and add it to the __all__ list in pyrdf2vec/embedders/__init__.py. ```python from .embedder import Embedder from .foo import FooEmbedder from .word2vec import Word2Vec __all__ = [ "Embedder", "FooEmbedder", "Word2Vec", ] ``` -------------------------------- ### Implement Custom Connector Class Source: https://pyrdf2vec.readthedocs.io/en/latest/contributing.html Extend the Connector class to create a new connector. Implement the fetch method and configure HTTP adapter with retries. ```python import attr from requests.adapters import HTTPAdapter from requests.packages.urllib3.util import Retry from pyrdf2vec.connectors import Connector @attr.s class FooConnector(Connector): """Represents a Foo connector.""" def __attrs_post_init__(self): adapter = HTTPAdapter( Retry( total=3, status_forcelist=[429, 500, 502, 503, 504], method_whitelist=["HEAD", "GET", "OPTIONS"], ) ) self._session.mount("http", adapter) self._session.mount("https", adapter) def fetch(self, query: str) -> None: """Fetchs the result of a query. Args: query: The query to fetch the result. Returns: The generated dictionary from the ['results']['bindings'] json """ # TODO: to be implemented ``` -------------------------------- ### Run Pytest for Walkers Source: https://pyrdf2vec.readthedocs.io/en/latest/contributing.html Execute unit tests for your new walking strategy by running pytest on the corresponding test file. ```bash pytest tests/walkers/foo.py ``` -------------------------------- ### Run Sampler Unit Tests Source: https://pyrdf2vec.readthedocs.io/en/latest/contributing.html Execute unit tests for a custom sampling technique using pytest. ```bash pytest tests/samplers/foo.py ``` -------------------------------- ### build_dictionary Method Source: https://pyrdf2vec.readthedocs.io/en/latest/api/pyrdf2vec.walkers.halk.html Builds a dictionary where predicates are mapped with the identifiers of the walks they appear in. This is a utility method for analyzing walk content. ```APIDOC build_dictionary(walks) Builds a dictionary of predicates mapped with the walk(s) identifiers to which it appears. Parameters: walks (List[Tuple[str, ...]]): The walks to build the dictionary. Returns: DefaultDict[str, Set[int]]: The dictionary of predicate names. ``` -------------------------------- ### Implement Walker Class Source: https://pyrdf2vec.readthedocs.io/en/latest/contributing.html Extend the Walker class and implement the _extract method for your new walking strategy. ```python from hashlib import md5 from typing import List, Set import attr from pyrdf2vec.graphs import KG, Vertex from pyrdf2vec.typings import EntityWalks, SWalk, Walk from pyrdf2vec.walkers import Walker @attr.s class FooWalker(Walker): """Defines the foo walking strategy.""" def _extract(self, kg: KG, instance: Vertex) -> EntityWalks: """Extracts walks rooted at the provided entities.""" canonical_walks: Set[SWalk] = set() # TODO: to be implemented return {instance.name: list(canonical_walks)} ``` -------------------------------- ### RDF2VecTransformer.load Source: https://pyrdf2vec.readthedocs.io/en/latest/api/pyrdf2vec.rdf2vec.html Loads a RDF2VecTransformer object from a binary file. ```APIDOC ## load ### Description Loads a RDF2VecTransformer object. ### Parameters - **filename** (str) - The binary file to load the RDF2VecTransformer object. Defaults to 'transformer_data'. ### Returns - **RDF2VecTransformer** - The loaded RDF2VecTransformer. ``` -------------------------------- ### WideSampler Class Source: https://pyrdf2vec.readthedocs.io/en/latest/api/pyrdf2vec.samplers.wide.html Initializes the WideSampler. This strategy gives priority to walks containing edges with the highest degree of predicates and objects. ```APIDOC ## WideSampler ### Description Wide sampling node-centric sampling strategy which gives priority to walks containing edges with the highest degree of predicates and objects. The degree of a predicate and an object being defined by the number of predicates and objects present in its neighborhood, but also by their number of occurrence in a Knowledge Graph. ### Parameters - **inverse** (bool) - Optional - True if the inverse algorithm must be used, False otherwise. Defaults to False. - **split** (bool) - Optional - True if the split algorithm must be used, False otherwise. Defaults to False. ### Attributes - **is_support_remote** (bool) - True if the sampling strategy can be used with a remote Knowledge Graph, False Otherwise. Defaults to False. - **random_state** (Any) - The random state to use to keep random determinism with the sampling strategy. Defaults to None. - **vertices_deg** (dict) - The degree of the vertices. Defaults to {}. - **visited** (set) - Tags vertices that appear at the max depth or of which all their children are tagged. Defaults to set. - **inverse** (bool) - True if the inverse algorithm must be used, False otherwise. Defaults to False. - **split** (bool) - True if the split algorithm must be used, False otherwise. Defaults to False. ``` -------------------------------- ### SamplerNotSupported Exception Source: https://pyrdf2vec.readthedocs.io/en/latest/api/pyrdf2vec.samplers.sampler.html Custom exception raised when a sampling strategy is not supported for a remote Knowledge Graph. ```APIDOC exception SamplerNotSupported Bases: Exception Base exception class for the lack of support of a sampling strategy for the extraction of walks via a SPARQL endpoint server. ``` -------------------------------- ### load Method Source: https://pyrdf2vec.readthedocs.io/en/latest/api/pyrdf2vec.html Loads a previously saved RDF2VecTransformer object from a file. ```APIDOC ## Static Method load Loads a RDF2VecTransformer object. ### Parameters * **filename** (str) – Optional - The binary file to load the RDF2VecTransformer object. Defaults to 'transformer_data'. ### Return Type `RDF2VecTransformer` ### Returns The loaded RDF2VecTransformer. ``` -------------------------------- ### PageRankSampler Class Source: https://pyrdf2vec.readthedocs.io/en/latest/api/pyrdf2vec.samplers.pagerank.html Initializes the PageRankSampler. This strategy assigns higher weights to frequent objects using PageRank to prioritize walks. ```APIDOC class PageRankSampler(Sampler): """PageRank node-centric sampling strategy which prioritizes walks containing the most frequent objects. This frequency being defined by assigning a higher weight to the most frequent objects using the PageRank ranking.""" def __init__(self, inverse=False, split=False, *, alpha=0.85): """Initializes the PageRankSampler. Args: inverse (bool): True if the inverse algorithm must be used, False otherwise. Defaults to False. split (bool): True if the split algorithm must be used, False otherwise. Defaults to False. alpha (float): The damping for PageRank. Defaults to 0.85. """ pass ``` -------------------------------- ### basic_split Method Source: https://pyrdf2vec.readthedocs.io/en/latest/api/pyrdf2vec.walkers.split.html The basic_split method splits vertices of random walks for an entity. It processes each vertex (except the root node) by splitting it according to symbols and capitalization, and removing any duplication. ```APIDOC ## def basic_split(_walks_) ### Description Splits vertices of random walks for an entity based. To achieve this, each vertex (except the root node) is split according to symbols and capitalization by removing any duplication. ### Parameters * **walks** (List[Tuple[Any, ...]]) - The random extracted walks. ### Returns Set[Tuple[str, ...]] - The list of tuples that contains split walks. ``` -------------------------------- ### Run Connector Unit Tests Source: https://pyrdf2vec.readthedocs.io/en/latest/contributing.html Execute unit tests for a custom connector using pytest. ```bash pytest tests/connectors/foo.py ``` -------------------------------- ### extract Method Source: https://pyrdf2vec.readthedocs.io/en/latest/api/pyrdf2vec.walkers.community.html Fits the provided sampling strategy and then calls the private _extract method that is implemented for each of the walking strategies. ```APIDOC ## Method: extract ### Description Fits the provided sampling strategy and then calls the private _extract method that is implemented for each of the walking strategies. ### Parameters * **kg** (KG) - The Knowledge Graph. The graph from which the neighborhoods are extracted for the provided entities. * **entities** (List[str]) - The entities to be extracted from the Knowledge Graph. * **verbose** (int, optional) - The verbosity level. 0: does not display anything; 1: display of the progress of extraction and training of walks; 2: debugging. Defaults to 0. ### Returns List[List[Tuple[str, ...]]] - The 2D matrix with its number of rows equal to the number of provided entities; number of column equal to the embedding size. ``` -------------------------------- ### UniformSampler Source: https://pyrdf2vec.readthedocs.io/en/latest/api/pyrdf2vec.samplers.html The UniformSampler assigns a uniform weight to each edge in a Knowledge Graph, prioritizing walks with strongly connected entities. It supports remote Knowledge Graphs and allows for deterministic random state. ```APIDOC ## Class UniformSampler ### Description Uniform sampling strategy that assigns a uniform weight to each edge in a Knowledge Graph, in order to prioritizes walks with strongly connected entities. ### Parameters - **_is_support_remote** (bool) - True if the sampling strategy can be used with a remote Knowledge Graph, False Otherwise Defaults to True. - **_random_state** (Any) - The random state to use to keep random determinism with the sampling strategy. Defaults to None. - **_vertices_deg** (Dict) - The degree of the vertices. Defaults to {}. - **_visited** (Set) - Tags vertices that appear at the max depth or of which all their children are tagged. Defaults to set. - **inverse** (bool) - True if the inverse algorithm must be used, False otherwise. Defaults to False. - **split** (bool) - True if the split algorithm must be used, False otherwise. Defaults to False. ### Methods #### fit(_kg_) Fits the sampling strategy. Since the weights are uniform, this function does nothing. Parameters: - **kg** (KG) – The Knowledge Graph. Return type: `None` #### get_weight(_hop_) Gets the weight of a hop in the Knowledge Graph. Parameters: - **hop** (Tuple[Any, Any]) – The hop of a vertex in a (predicate, object) form to get the weight. Return type: `int` Returns: The weight of a given hop. ``` -------------------------------- ### UniformSampler Class Source: https://pyrdf2vec.readthedocs.io/en/latest/api/pyrdf2vec.samplers.uniform.html The UniformSampler class implements a uniform sampling strategy. It assigns equal weight to each edge, prioritizing walks with strongly connected entities. It includes parameters for handling remote Knowledge Graphs, random state, and inverse/split algorithms. ```APIDOC class UniformSampler: """Uniform sampling strategy that assigns a uniform weight to each edge in a Knowledge Graph, in order to prioritizes walks with strongly connected entities.""" is_support_remote: bool True if the sampling strategy can be used with a remote Knowledge Graph, False Otherwise Defaults to True. random_state: Any The random state to use to keep random determinism with the sampling strategy. Defaults to None. vertices_deg: dict The degree of the vertices. Defaults to {}. visited: set Tags vertices that appear at the max depth or of which all their children are tagged. Defaults to set. inverse: bool True if the inverse algorithm must be used, False otherwise. Defaults to False. split: bool True if the split algorithm must be used, False otherwise. Defaults to False. def fit(kg): """Since the weights are uniform, this function does nothing.""" Parameters: kg (KG): The Knowledge Graph. Return type: None def get_weight(hop): """Gets the weight of a hop in the Knowledge Graph.""" Parameters: hop (Tuple[Any, Any]): The hop of a vertex in a (predicate, object) form to get the weight. Return type: int Returns: The weight of a given hop. ``` -------------------------------- ### is_exist Source: https://pyrdf2vec.readthedocs.io/en/latest/api/pyrdf2vec.graphs.html Checks if all provided entities exist within the Knowledge Graph. ```APIDOC ## is_exist ### Description Checks that all provided entities exists in the Knowledge Graph. ### Parameters - **entities** (List[str]): The entities to check the existence. ### Returns - **bool**: True if all the entities exists, False otherwise. ``` -------------------------------- ### PageRankSampler.fit Method Source: https://pyrdf2vec.readthedocs.io/en/latest/api/pyrdf2vec.samplers.pagerank.html Fits the sampling strategy by running PageRank on a provided Knowledge Graph according to the specified damping. ```APIDOC def fit(kg): """Fits the sampling strategy by running PageRank on a provided KG according to the specified damping. Args: kg (KG): The Knowledge Graph. Returns: None """ pass ``` -------------------------------- ### WalkletWalker Class Source: https://pyrdf2vec.readthedocs.io/en/latest/api/pyrdf2vec.walkers.walklet.html Initializes the WalkletWalker. This class implements a walking strategy that transforms randomly extracted walks into walklets, which are walks of size one or two including the root node and potentially another vertex (predicate or object). ```APIDOC ## Class: WalkletWalker ### Description Initializes the WalkletWalker. This class implements a walking strategy that transforms randomly extracted walks into walklets, which are walks of size one or two including the root node and potentially another vertex (predicate or object). ### Parameters - **max_depth** (int) - The maximum depth of one walk. - **max_walks** (int, optional) - The maximum number of walks per entity. Defaults to None. - **sampler** (object, optional) - The sampling strategy. Defaults to UniformSampler. - **n_jobs** (int, optional) - Number of parallel jobs to run. Defaults to None. - **with_reverse** (bool, optional) - True to extract parents and children hops from an entity, creating (max_walks * max_walks) walks of 2 * depth, allowing also to centralize this entity in the walks. False otherwise. Defaults to False. - **random_state** (int, optional) - The random state to use to keep random determinism with the walking strategy. Defaults to None. - **md5_bytes** (int, optional) - The number of bytes to use for MD5 hashing. Defaults to 8. ### Attributes - **is_support_remote** (bool) - True if the walking strategy can be used with a remote Knowledge Graph, False Otherwise. Defaults to True. - **kg** (object) - The global KG used later on for the worker process. Defaults to None. - **max_depth** (int) - The maximum depth of one walk. - **max_walks** (int, optional) - The maximum number of walks per entity. Defaults to None. - **random_state** (int, optional) - The random state to use to keep random determinism with the walking strategy. Defaults to None. - **sampler** (object) - The sampling strategy. Defaults to UniformSampler. - **with_reverse** (bool, optional) - True to extract parents and children hops from an entity, creating (max_walks * max_walks) walks of 2 * depth, allowing also to centralize this entity in the walks. False otherwise. Defaults to False. ``` -------------------------------- ### RandomWalker Class Source: https://pyrdf2vec.readthedocs.io/en/latest/api/pyrdf2vec.walkers.random.html The RandomWalker class implements a random walking strategy. It uses DFS if max_walks is specified, otherwise BFS. It can be configured with various parameters to control walk generation. ```APIDOC ## class RandomWalker(_max_depth_, _max_walks =None_, _sampler =NOTHING_, _n_jobs =None_, _*_ , _with_reverse =False_, _random_state =None_, _md5_bytes =8_) Bases: `pyrdf2vec.walkers.walker.Walker` Random walking strategy which extracts walks from a root node using the Depth First Search (DFS) algorithm if a maximum number of walks is specified, otherwise the Breadth First Search (BFS) algorithm is used. ### Attributes * `is_support_remote` (bool): True if the walking strategy can be used with a remote Knowledge Graph, False Otherwise. Defaults to True. * `kg` (KG): The global KG used later on for the worker process. Defaults to None. * `max_depth` (int): The maximum depth of one walk. * `max_walks` (int): The maximum number of walks per entity. Defaults to None. * `md5_bytes` (int): The number of bytes to keep after hashing objects in MD5. Hasher allows to reduce the memory occupied by a long text. If md5_bytes is None, no hash is applied. Defaults to 8. * `random_state` (int): The random state to use to keep random determinism with the walking strategy. Defaults to None. * `sampler` (Sampler): The sampling strategy. Defaults to UniformSampler. * `with_reverse` (bool): True to extracts parents and children hops from an entity, creating (max_walks * max_walks) walks of 2 * depth, allowing also to centralize this entity in the walks. False otherwise. Defaults to False. ``` -------------------------------- ### build_dictionary Source: https://pyrdf2vec.readthedocs.io/en/latest/api/pyrdf2vec.walkers.html Builds a dictionary mapping predicates to the walk identifiers they appear in. ```APIDOC ## Function pyrdf2vec.walkers.build_dictionary(_walks_) Builds a dictionary of predicates mapped with the walk(s) identifiers to which it appears. Parameters: * **walks** (`List[Tuple[str, ...]]`): The walks to build the dictionary. Returns: * `DefaultDict[str, Set[int]]`: The dictionary of predicate names. ``` -------------------------------- ### Sampler Source: https://pyrdf2vec.readthedocs.io/en/latest/api/pyrdf2vec.samplers.html Base class for sampling strategies in pyrdf2vec. It defines the common interface and properties for all sampling methods. ```APIDOC ## Sampler ### Description Base class of the sampling strategies. ### Parameters - **inverse** (bool) - Optional - True if the inverse algorithm must be used, False otherwise. Defaults to False. - **split** (bool) - Optional - True if the split algorithm must be used, False otherwise. Defaults to False. ### Methods #### fit(kg) Fits the sampling strategy. - **kg** (KG) - The Knowledge Graph. Raises: SamplerNotSupported: If there is an attempt to use an invalid sampling strategy to a remote Knowledge Graph. #### get_weight(hop) Gets the weight of a hop in the Knowledge Graph. - **hop** (Tuple[Any, Any]) - The hop of a vertex in a (predicate, object) form to get the weight. Returns: The weight of a given hop. Raises: NotImplementedError: If this method is called, without having provided an implementation. #### get_weights(hops) Gets the weights of the provided hops. - **hops** (List[Tuple[Any, Any]]) - The hops to get the weights. Returns: Optional[List[float]]: The weights to the edge of the Knowledge Graph. #### sample_hop(kg, walk, is_last_hop, is_reverse=False) Samples an unvisited random hop in the (predicate, object) form, according to the weight of hops for a given walk. - **kg** (KG) - The Knowledge Graph. - **walk** (Tuple[Any, ...]) - The walk with one or several vertices. - **is_last_hop** (bool) - True if the next hop to be visited is the last one for the desired depth, False otherwise. - **is_reverse** (bool) - Optional - True to get the parent neighbors instead of the child neighbors, False otherwise. Defaults to False. Returns: Optional[Tuple[Any, Any]]: An unvisited hop in the (predicate, object) form. ### Properties #### random_state Gets the random state. Returns: Optional[int]: The random state. #### visited Gets the tagged vertices that appear at the max depth or of which all their children are tagged. Returns: Set[Tuple[Tuple[Any, Any], int]]: The tagged vertices. ``` -------------------------------- ### fit_transform Method Source: https://pyrdf2vec.readthedocs.io/en/latest/api/pyrdf2vec.html Creates a model and generates embeddings and literals for the provided entities. ```APIDOC ## Method fit_transform Creates a model and generates embeddings and literals for the provided entities. ### Parameters * **kg** (KG) – Required - The Knowledge Graph. * **entities** (List[str]) – Required - The entities including test entities to create the embeddings. Since RDF2Vec is unsupervised, there is no label leakage. * **is_update** (bool) – Optional - True if the new corpus should be added to old model’s corpus, False otherwise. Defaults to False. ### Return Type `Tuple[List[str], List[List[Union[float, str, Tuple[Union[float, str], ...]]]]]` ### Returns The embeddings and the literals of the provided entities. ``` -------------------------------- ### get_walks Method Source: https://pyrdf2vec.readthedocs.io/en/latest/api/pyrdf2vec.html Retrieves the walks for a given entity based on a Knowledge Graph and a list of walkers. ```APIDOC ## Method get_walks Gets the walks of an entity based on a Knowledge Graph and a list of walkers. ### Parameters * **kg** (KG) – Required - The Knowledge Graph. * **entities** (List[str]) – Required - The entities including test entities to create the embeddings. Since RDF2Vec is unsupervised, there is no label leakage. ### Return Type `List[List[Tuple[str, ...]]]` ### Returns The walks for the given entities. ### Raises **ValueError** – If the provided entities aren’t in the Knowledge Graph. ``` -------------------------------- ### WLWalker Class Source: https://pyrdf2vec.readthedocs.io/en/latest/api/pyrdf2vec.walkers.weisfeiler_lehman.html The WLWalker class implements the Weisfeiler-Lehman walking strategy. It relabels nodes in the extracted random walks to capture more information about entity representations. ```APIDOC ## Class WLWalker ### Description Implements the Weisfeiler-Lehman walking strategy which relabels the nodes of the extracted random walks, providing additional information about the entity representations only when a maximum number of walks is not specified. ### Parameters - **max_depth** (int) - The maximum depth of one walk. - **max_walks** (int, optional) - The maximum number of walks per entity. Defaults to None. - **sampler** (Sampler, optional) - The sampling strategy. Defaults to UniformSampler. - **n_jobs** (int, optional) - Number of parallel jobs to run. - **with_reverse** (bool, optional) - Whether to consider reverse edges. Defaults to False. - **random_state** (int, optional) - The random state to use for reproducibility. Defaults to None. - **md5_bytes** (int, optional) - The number of bytes to keep after hashing objects in MD5. Defaults to 8. - **wl_iterations** (int, optional) - The number of Weisfeiler-Lehman iterations. Defaults to 4. ### Attributes - **inv_label_map** (defaultdict) - Stores the mapping of the inverse labels. - **is_support_remote** (bool) - True if the walking strategy can be used with a remote Knowledge Graph, False Otherwise. Defaults to False. - **label_map** (defaultdict) - Stores the mapping of the inverse labels. - **kg** (KG) - The global KG used later on for the worker process. Defaults to None. - **max_depth** (int) - The maximum depth of one walk. - **max_walks** (int) - The maximum number of walks per entity. Defaults to None. - **md5_bytes** (int) - The number of bytes to keep after hashing objects in MD5. Defaults to 8. - **random_state** (int) - The random state to use to keep random determinism with the walking strategy. Defaults to None. - **sampler** (Sampler) - The sampling strategy. Defaults to UniformSampler. - **wl_iterations** (int) - The Weisfeiler Lehman’s iteration. Defaults to 4. ## Method extract ### Description Fits the provided sampling strategy and then calls the private _extract method that is implemented for each of the walking strategies. ### Parameters - **kg** (KG) - The Knowledge Graph. - **entities** (List[str]) - The entities to be extracted from the Knowledge Graph. - **verbose** (int, optional) - The verbosity level. 0: does not display anything; 1: display of the progress of extraction and training of walks; 2: debugging. Defaults to 0. ### Returns - List[List[Tuple[str, ...]]] - The 2D matrix with its number of rows equal to the number of provided entities; number of column equal to the embedding size. ``` -------------------------------- ### SplitWalker Class Source: https://pyrdf2vec.readthedocs.io/en/latest/api/pyrdf2vec.walkers.split.html The SplitWalker class implements a splitting walking strategy. It splits each vertex (except the root node) present in the randomly extracted walks. It inherits from RandomWalker and offers various parameters to control the walking process, including depth, number of walks, sampling, and parallel processing. ```APIDOC ## class pyrdf2vec.walkers.split.SplitWalker ### Description Splitting walking strategy which splits each vertex (except the root node) present in the randomly extracted walks. ### Parameters * **_max_depth_** (int) - The maximum depth of one walk. * **_max_walks_** (int, optional) - The maximum number of walks per entity. Defaults to None. * **_sampler_** (pyrdf2vec.samplers.Sampler, optional) - The sampling strategy. Defaults to UniformSampler. * **_n_jobs_** (int, optional) - Number of parallel jobs to run. Defaults to None. * **_with_reverse_** (bool, optional) - True to extracts parents and children hops from an entity, creating (max_walks * max_walks) walks of 2 * depth, allowing also to centralize this entity in the walks. False otherwise. Defaults to False. * **_random_state_** (int, optional) - The random state to use to keep random determinism with the walking strategy. Defaults to None. * **_md5_bytes_** (int, optional) - The number of bytes to keep after hashing objects in MD5. Hasher allows to reduce the memory occupied by a long text. If md5_bytes is None, no hash is applied. Defaults to 8. * **_func_split_** (callable, optional) - The function to call for the splitting of vertices. In case of reimplementation, it is important to respect the signature imposed by basic_split function. Defaults to func_split. ``` -------------------------------- ### RandomWalker Source: https://pyrdf2vec.readthedocs.io/en/latest/api/pyrdf2vec.walkers.html The RandomWalker class implements a random walking strategy. It uses Depth First Search (DFS) or Breadth First Search (BFS) to extract walks from a root node. ```APIDOC ## Class pyrdf2vec.walkers.RandomWalker Bases: `pyrdf2vec.walkers.walker.Walker` Random walking strategy which extracts walks from a root node using the Depth First Search (DFS) algorithm if a maximum number of walks is specified, otherwise the Breadth First Search (BFS) algorithm is used. ### Attributes * `_is_support_remote` (bool): True if the walking strategy can be used with a remote Knowledge Graph, False Otherwise. Defaults to True. * `kg` (KG): The global KG used later on for the worker process. Defaults to None. * `max_depth` (int): The maximum depth of one walk. * `max_walks` (int, optional): The maximum number of walks per entity. Defaults to None. * `md5_bytes` (int, optional): The number of bytes to keep after hashing objects in MD5. Hasher allows to reduce the memory occupied by a long text. If md5_bytes is None, no hash is applied. Defaults to 8. * `random_state` (int, optional): The random state to use to keep random determinism with the walking strategy. Defaults to None. * `sampler` (Sampler): The sampling strategy. Defaults to UniformSampler. * `with_reverse` (bool, optional): True to extracts parents and children hops from an entity, creating (max_walks * max_walks) walks of 2 * depth, allowing also to centralize this entity in the walks. False otherwise. Defaults to False. ### Methods #### extract_walks(_kg_, _entity_) Extracts random walks for an entity based on Knowledge Graph using the Depth First Search (DFS) algorithm if a maximum number of walks is specified, otherwise the Breadth First Search (BFS) algorithm is used. Parameters: * **kg** (KG): The Knowledge Graph. * **entity** (Vertex): The root node to extract walks. Returns: * `List[Tuple[Any, ...]]`: The list of unique walks for the provided entity. ``` -------------------------------- ### WalkerNotSupported Exception Source: https://pyrdf2vec.readthedocs.io/en/latest/api/pyrdf2vec.walkers.walker.html Custom exception raised when a walking strategy is not supported for a remote Knowledge Graph. ```APIDOC ## exception pyrdf2vec.walkers.walker.WalkerNotSupported ### Description Base exception class for the lack of support of a walking strategy for the extraction of walks via a SPARQL endpoint server. ```