Skip to content

Commit

Permalink
Fix formatting issues
Browse files Browse the repository at this point in the history
  • Loading branch information
LukasZahradnik committed Jul 4, 2022
1 parent 7b46d4c commit e2e1e63
Show file tree
Hide file tree
Showing 22 changed files with 65 additions and 45 deletions.
1 change: 0 additions & 1 deletion neuralogic/core/builder/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from neuralogic.core.builder.builder import Builder
from neuralogic.core.builder.dataset_builder import DatasetBuilder
from neuralogic.core.builder.components import Neuron, Weight, Sample, BuiltDataset

2 changes: 1 addition & 1 deletion neuralogic/core/constructs/relation.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def __init__(self, weight, predicate: Predicate, fixed=False, terms=None, negate

def fixed(self) -> "WeightedRelation":
if self.is_fixed:
raise Exception(f"Weighted relation is already fixed")
raise Exception(f"Weighted relation {self} is already fixed")
return WeightedRelation(self.weight, self.predicate, True, self.terms, self.negated)

def __invert__(self) -> "WeightedRelation":
Expand Down
2 changes: 1 addition & 1 deletion neuralogic/dataset/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def __init__(
self.replace_empty_column = replace_empty_column

if len(term_columns) == 0:
raise NotImplementedError(f"Cannot create DBSource with zero terms")
raise NotImplementedError("Cannot create DBSource with zero terms")

def to_csv(self, cursor) -> CSVFile:
source = io.StringIO()
Expand Down
5 changes: 4 additions & 1 deletion neuralogic/dataset/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@


class FileDataset(BaseDataset):
r"""``FileDataset`` represents samples stored in files in the `NeuraLogic <https://github.com/GustikS/NeuraLogic>`_ (logic) format.
r"""
``FileDataset`` represents samples stored in files in the `NeuraLogic <https://github.com/GustikS/NeuraLogic>`_
(logic) format.
Parameters
----------
Expand All @@ -16,6 +18,7 @@ class FileDataset(BaseDataset):
Path to the queries file. Default: ``None``
"""

def __init__(
self,
examples_file: Optional[str] = None,
Expand Down
9 changes: 6 additions & 3 deletions neuralogic/dataset/logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@

class Dataset(BaseDataset):
r"""
Dataset encapsulating (learning) samples in the form of logic format, allowing users to fully take advantage of the PyNeuraLogic library.
Dataset encapsulating (learning) samples in the form of logic format, allowing users to fully take advantage of the
PyNeuraLogic library.
One learning sample consists of:
* Example: A list of logic facts and rules representing some instance (e.g., a graph)
Expand Down Expand Up @@ -51,7 +52,8 @@ class Dataset(BaseDataset):
dataset.add_query([first_query_0, first_query_1])
dataset.add_query(second_query)
# Learning samples: [first_example, first_query_0], [first_example, first_query_1], [second_example, second_query]
# Learning samples:
# [first_example, first_query_0], [first_example, first_query_1], [second_example, second_query]
Parameters
----------
Expand All @@ -62,10 +64,11 @@ class Dataset(BaseDataset):
List of queries. Default: ``None``
"""

def __init__(
self,
examples: Optional[List[List[DatasetEntries]]] = None,
queries: Optional[List[Union[List[DatasetEntries], DatasetEntries]]] = None
queries: Optional[List[Union[List[DatasetEntries], DatasetEntries]]] = None,
):
self.examples: List[List[DatasetEntries]] = examples if examples is not None else []
self.queries: List[Union[List[DatasetEntries], DatasetEntries]] = queries if queries is not None else []
Expand Down
2 changes: 1 addition & 1 deletion neuralogic/db/pg/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def get_rule_aggregation_function(
inner_select.append(f"SELECT {selects} FROM {function_name}")

if is_fact:
select = [f"SUM(out.value) as value"]
select = ["SUM(out.value) as value"]
else:
select = [f"{FUNCTION_MAP[activation]}(SUM(out.value)) as value"]

Expand Down
2 changes: 1 addition & 1 deletion neuralogic/inference/evaluation_inference_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def query(self, query: BaseRelation, examples: Optional[List[Union[BaseRelation,
try:
built_dataset = self.model.build_dataset(self.dataset)
results = self.model(built_dataset.samples, train=False)
except Exception as e:
except Exception:
self.dataset.examples = global_examples
return {}

Expand Down
2 changes: 1 addition & 1 deletion neuralogic/inference/inference_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def query(self, query: BaseRelation, examples: Optional[List[Union[BaseRelation,
lifted_example = gs.query.evidence
template = gs.template

ground_template = self.grounder.groundRulesAndFacts(lifted_example, template)
self.grounder.groundRulesAndFacts(lifted_example, template)

clause = self.java_factory.atom_to_clause(query)
horn_clause = self.horn_clause(clause)
Expand Down
4 changes: 1 addition & 3 deletions neuralogic/nn/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from typing import Optional

from neuralogic.core.enums import Backend


Expand All @@ -20,7 +18,7 @@ def get_neuralogic_layer(backend: Backend = Backend.JAVA):


def get_evaluator(
template: "Template",
template,
settings=None,
backend: Backend = Backend.JAVA,
):
Expand Down
2 changes: 1 addition & 1 deletion neuralogic/nn/module/general/linear.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from neuralogic.core.constructs.metadata import Metadata
from neuralogic.core.enums import Activation
from neuralogic.core.constructs.factories import R, V
from neuralogic.core.constructs.factories import R
from neuralogic.nn.module.module import Module


Expand Down
2 changes: 1 addition & 1 deletion neuralogic/nn/module/general/pooling.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from neuralogic.core.constructs.metadata import Metadata
from neuralogic.core.enums import Activation, Aggregation
from neuralogic.core.constructs.factories import R, V
from neuralogic.core.constructs.factories import R
from neuralogic.nn.module.module import Module


Expand Down
2 changes: 0 additions & 2 deletions neuralogic/nn/module/general/rnn.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from typing import Union

from neuralogic.core.constructs.metadata import Metadata
from neuralogic.core.enums import Activation
from neuralogic.core.constructs.factories import R, V
Expand Down
1 change: 0 additions & 1 deletion neuralogic/nn/module/general/rvnn.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from neuralogic.core.enums import Activation, Aggregation
from neuralogic.core.constructs.factories import R, V
from neuralogic.nn.module.module import Module
from neuralogic.nn.module.general.rnn import RNNCell


class RvNN(Module):
Expand Down
15 changes: 10 additions & 5 deletions neuralogic/nn/module/gnn/appnp.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,32 @@

class APPNPConv(Module):
r"""
Approximate Personalized Propagation of Neural Predictions layer from `"Predict then Propagate: Graph Neural Networks meet Personalized PageRank" <https://arxiv.org/abs/1810.05997>`_.
Approximate Personalized Propagation of Neural Predictions layer from
`"Predict then Propagate: Graph Neural Networks meet Personalized PageRank" <https://arxiv.org/abs/1810.05997>`_.
Which can be expressed as:
.. math::
\mathbf{x}^{0}_i = \mathbf{x}_i
.. math::
\mathbf{x}^{k}_i = \alpha \cdot \mathbf{x}^0_i + (1 - \alpha) \cdot {agg}_{j \in \mathcal{N}(i)}(\mathbf{x}^{k - 1}_j)
\mathbf{x}^{k}_i = \alpha \cdot \mathbf{x}^0_i + (1 - \alpha) \cdot
{agg}_{j \in \mathcal{N}(i)}(\mathbf{x}^{k - 1}_j)
.. math::
\mathbf{x}^{\prime}_i = act(\mathbf{x}^{K}_i)
Where *act* is an activation function and *agg* aggregation function.
The first part of the second equation that is ":math:`\alpha \cdot \mathbf{x}^0_i`" is expressed in the logic form as:
The first part of the second equation that is ":math:`\alpha \cdot \mathbf{x}^0_i`" is expressed
in the logic form as:
.. code-block:: logtalk
R.<output_name>__<k>(V.I) <= R.<feature_name>(V.I)[<alpha>].fixed()
The second part of the second equation that is ":math:`(1 - \alpha) \cdot {agg}_{j \in \mathcal{N}(i)}(\mathbf{x}^{k - 1}_j)`" is expressed as:
The second part of the second equation that is
":math:`(1 - \alpha) \cdot {agg}_{j \in \mathcal{N}(i)}(\mathbf{x}^{k - 1}_j)`" is expressed as:
.. code-block:: logtalk
Expand All @@ -36,7 +40,8 @@ class APPNPConv(Module):
Examples
--------
The whole computation of this module (parametrized as :code:`APPNPConv("h1", "h0", "_edge", 3, 0.1, Activation.SIGMOID)`) is as follows:
The whole computation of this module
(parametrized as :code:`APPNPConv("h1", "h0", "_edge", 3, 0.1, Activation.SIGMOID)`) is as follows:
.. code:: logtalk
Expand Down
3 changes: 2 additions & 1 deletion neuralogic/nn/module/gnn/gcn.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

class GCNConv(Module):
r"""
Graph Convolutional layer from `"Semi-supervised Classification with Graph Convolutional Networks" <https://arxiv.org/abs/1609.02907>`_.
Graph Convolutional layer from
`"Semi-supervised Classification with Graph Convolutional Networks" <https://arxiv.org/abs/1609.02907>`_.
Which can be expressed as:
.. math::
Expand Down
4 changes: 2 additions & 2 deletions neuralogic/nn/module/gnn/gsage.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ class SAGEConv(Module):
\mathbf{x}^{\prime}_i = act(\mathbf{W}_1 \mathbf{x}_i + \mathbf{W}_2 \cdot
{agg}_{j \in \mathcal{N}(i)}(\mathbf{x}_j)))
Where *act* is an activation function, *agg* aggregation function and *W*'s are learnable parameters. This equation is
translated into the logic form as:
Where *act* is an activation function, *agg* aggregation function and *W*'s are learnable parameters.
This equation is translated into the logic form as:
.. code:: logtalk
Expand Down
7 changes: 4 additions & 3 deletions neuralogic/nn/module/gnn/res_gated.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ class ResGatedGraphConv(Module):
\mathbf{\eta}_{i,j} = gating\_act(\mathbf{W}_3 \mathbf{x}_i + \mathbf{W}_4 \mathbf{x}_j)
Where *act* is an activation function, *agg* aggregation function, *gating_act* is a gating activation function and :math:`W_n` are learnable parameters. This equation is
translated into the logic form as:
Where *act* is an activation function, *agg* aggregation function, *gating_act* is a gating activation function and
:math:`W_n` are learnable parameters. This equation is translated into the logic form as:
.. code:: logtalk
Expand All @@ -36,7 +36,8 @@ class ResGatedGraphConv(Module):
Examples
--------
The whole computation of this module (parametrized as :code:`ResGatedGraphConv(1, 2, "h1", "h0", "_edge")`) is as follows:
The whole computation of this module (parametrized as :code:`ResGatedGraphConv(1, 2, "h1", "h0", "_edge")`)
is as follows:
.. code:: logtalk
Expand Down
22 changes: 14 additions & 8 deletions neuralogic/nn/module/gnn/rgcn.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,26 @@

class RGCNConv(Module):
r"""
Relational Graph Convolutional layer from `Modeling Relational Data with Graph Convolutional Networks <https://arxiv.org/abs/1703.06103>`_.
Relational Graph Convolutional layer from
`Modeling Relational Data with Graph Convolutional Networks <https://arxiv.org/abs/1703.06103>`_.
Which can be expressed as:
.. math::
\mathbf{x}^{\prime}_i = act(\mathbf{W_0} \cdot \mathbf{x}_i + \sum_{r \in \mathcal{R}}
{agg}_{j \in \mathcal{N}_r(i)}(\mathbf{W_r} \cdot \mathbf{x}_j))
Where *act* is an activation function, *agg* aggregation function (by default average), :math:`W_0` is a learnable root parameter
and :math:`W_r` is a learnable parameter for each relation.
Where *act* is an activation function, *agg* aggregation function (by default average), :math:`W_0` is a
learnable root parameter and :math:`W_r` is a learnable parameter for each relation.
The first part of the equation that is ":math:`\mathbf{W_0} \cdot \mathbf{x}_i`" can be expressed in the logic form as:
The first part of the equation that is ":math:`\mathbf{W_0} \cdot \mathbf{x}_i`" can be expressed
in the logic form as:
.. code-block:: logtalk
R.<output_name>(V.I) <= R.<feature_name>(V.I)[<W0>]
Another part of the equation that is ":math:`{agg}_{j \in \mathcal{N}_r(i)}(\mathbf{W_r} \cdot \mathbf{x}_j)`" can be expressed as:
Another part of the equation that is ":math:`{agg}_{j \in \mathcal{N}_r(i)}(\mathbf{W_r} \cdot \mathbf{x}_j)`"
can be expressed as:
.. code-block:: logtalk
Expand All @@ -36,12 +39,14 @@ class RGCNConv(Module):
R.<output_name>(V.I) <= (R.<feature_name>(V.J)[<Wr>], R.<relation>(V.J, V.I))
The outer summation, together with summing it with the first part, is handled by aggregation of all rules with the same head (and substitution).
The outer summation, together with summing it with the first part, is handled by aggregation of all rules with the
same head (and substitution).
Examples
--------
The whole computation of this module (parametrized as :code:`RGCNConv(1, 2, "h1", "h0", "_edge", ["sibling", "parent"])`) is as follows:
The whole computation of this module
(parametrized as :code:`RGCNConv(1, 2, "h1", "h0", "_edge", ["sibling", "parent"])`) is as follows:
.. code:: logtalk
Expand Down Expand Up @@ -75,7 +80,8 @@ class RGCNConv(Module):
feature_name : str
Feature predicate name to get features from.
edge_name : Optional[str]
Edge predicate name to use for neighborhood relations. When :code:`None`, elements from :code:`relations` are used instead.
Edge predicate name to use for neighborhood relations. When :code:`None`, elements from :code:`relations`
are used instead.
relations : List[str]
List of relations' names
activation : Activation
Expand Down
3 changes: 2 additions & 1 deletion neuralogic/nn/module/gnn/sg.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ class SGConv(Module):
.. math::
\mathbf{x}^{\prime}_i = act(\mathbf{W} \cdot {agg}_{j \in \mathcal{N}^k(i)}(\mathbf{x}_j))
Where *act* is an activation function, *agg* aggregation function, *W* is a learnable parameter and :math:`\mathcal{N}^k(i)` denotes nodes that are *k* hops away from the node *i*.
Where *act* is an activation function, *agg* aggregation function, *W* is a learnable parameter
and :math:`\mathcal{N}^k(i)` denotes nodes that are *k* hops away from the node *i*.
This equation is translated into the logic form as:
.. code:: logtalk
Expand Down
8 changes: 5 additions & 3 deletions neuralogic/nn/module/gnn/tag.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@

class TAGConv(Module):
r"""
Topology Adaptive Graph Convolutional layer from `"Topology Adaptive Graph Convolutional Networks" <https://arxiv.org/abs/1710.10370>`_.
Topology Adaptive Graph Convolutional layer from
`"Topology Adaptive Graph Convolutional Networks" <https://arxiv.org/abs/1710.10370>`_.
Which can be expressed as:
.. math::
\mathbf{x}^{\prime}_i = act(\sum_{k=0}^K \mathbf{W}_k \cdot {agg}_{j \in \mathcal{N}^k(i)}(\mathbf{x}_j))
Where *act* is an activation function, *agg* aggregation function, *Wk* are learnable parameters and :math:`\mathcal{N}^k(i)` denotes nodes that are *k* hops away from the node *i*.
This equation is translated into the logic form as:
Where *act* is an activation function, *agg* aggregation function, *Wk* are learnable parameters and
:math:`\mathcal{N}^k(i)` denotes nodes that are *k* hops away from the node *i*. This equation is translated into
the logic form as:
This equation is translated into the logic form as:
Expand Down
4 changes: 2 additions & 2 deletions neuralogic/nn/module/meta/meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ class MetaConv(Module):
\sum_{k \in \mathcal{K}}
(\mathbf{W_k} \cdot \mathbf{x}_j))
Where *act* is an activation function, *agg* aggregation function (by default average), :math:`W_0` is a learnable root parameter
and :math:`W_k` is a learnable parameter for each role.
Where *act* is an activation function, *agg* aggregation function (by default average), :math:`W_0` is a learnable
root parameter and :math:`W_k` is a learnable parameter for each role.
Parameters
----------
Expand Down
8 changes: 6 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,12 @@
long_description = DESCRIPTION

about: Dict = {}
with open(os.path.join(here, NAME, "__version__.py")) as f: # type: ignore
exec(f.read(), about)

try:
with open(os.path.join(here, NAME, "__version__.py")) as f: # type: ignore
exec(f.read(), about)
except Exception:
about["__version__"] = "0.0.0" # development

setup(
name=NAME,
Expand Down

0 comments on commit e2e1e63

Please sign in to comment.