Skip to content

Commit

Permalink
adjust use of expression parameter in eval_ methods
Browse files Browse the repository at this point in the history
  • Loading branch information
mmatera committed Jan 17, 2025
1 parent 93df099 commit 89ad93f
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 62 deletions.
36 changes: 18 additions & 18 deletions pymathics/graph/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -817,13 +817,13 @@ class _PatternList(_NetworkXBuiltin):
def eval(
self, graph, expression: Expression, evaluation: Evaluation, options: dict
):
"%(name)s[graph_, OptionsPattern[%(name)s]]"
"expression: %(name)s[graph_, OptionsPattern[%(name)s]]"
graph = self._build_graph(graph, evaluation, options, expression)
if graph:
return ListExpression(*(from_python(q) for q in self._items(graph)))

def eval_patt(self, graph, patt, expression, evaluation, options):
"%(name)s[graph_, patt_, OptionsPattern[%(name)s]]"
"expression: %(name)s[graph_, patt_, OptionsPattern[%(name)s]]"
graph = self._build_graph(graph, evaluation, options, expression)
if graph:
return Expression(SymbolCases, ListExpression(*self._items(graph)), patt)
Expand Down Expand Up @@ -875,7 +875,7 @@ def _retrieve(self, graph, what, neighbors, expression, evaluation):
self._not_a_vertex(expression, 2, evaluation)

def eval(self, graph, what, expression, evaluation, options):
"%(name)s[graph_, what_, OptionsPattern[%(name)s]]"
"expression: %(name)s[graph_, what_, OptionsPattern[%(name)s]]"
graph = self._build_graph(graph, evaluation, options, expression)
if graph:
G = graph.G.to_undirected() # FIXME inefficient
Expand All @@ -884,7 +884,7 @@ def eval(self, graph, what, expression, evaluation, options):
)

def eval_d(self, graph, what, d, expression, evaluation, options):
"%(name)s[graph_, what_, d_, OptionsPattern[%(name)s]]"
"expression: %(name)s[graph_, what_, d_, OptionsPattern[%(name)s]]"
py_d = d.to_mpmath()
if py_d is None:
return
Expand Down Expand Up @@ -967,13 +967,13 @@ class EdgeConnectivity(_NetworkXBuiltin):
summary_text = "edge connectivity of a graph"

def eval(self, graph, expression, evaluation, options):
"%(name)s[graph_, OptionsPattern[%(name)s]]"
"expression: %(name)s[graph_, OptionsPattern[%(name)s]]"
graph = self._build_graph(graph, evaluation, options, expression)
if graph and not graph.empty():
return Integer(nx.edge_connectivity(graph.G))

def eval_st(self, graph, s, t, expression, evaluation, options):
"%(name)s[graph_, s_, t_, OptionsPattern[%(name)s]]"
"expression: %(name)s[graph_, s_, t_, OptionsPattern[%(name)s]]"
graph = self._build_graph(graph, evaluation, options, expression)
if graph and not graph.empty():
return Integer(nx.edge_connectivity(graph.G, s, t))
Expand All @@ -995,7 +995,7 @@ class EdgeIndex(_NetworkXBuiltin):
summary_text = "find the position of an edge"

def eval(self, graph, v, expression, evaluation, options):
"%(name)s[graph_, v_, OptionsPattern[%(name)s]]"
"expression: %(name)s[graph_, v_, OptionsPattern[%(name)s]]"
graph = self._build_graph(graph, evaluation, options, expression)
if graph:
# FIXME: check if directionality must be considered or not.
Expand Down Expand Up @@ -1040,7 +1040,7 @@ class EdgeRules(_NetworkXBuiltin):
summary_text = "list the edge as rules"

def eval(self, graph, expression, evaluation, options):
"%(name)s[graph_, OptionsPattern[%(name)s]]"
"expression: %(name)s[graph_, OptionsPattern[%(name)s]]"
graph = self._build_graph(graph, evaluation, options, expression)
if graph:

Expand Down Expand Up @@ -1096,7 +1096,7 @@ class FindShortestPath(_NetworkXBuiltin):
def eval_s_t(
self, graph, s, t, expression: Expression, evaluation: Evaluation, options: dict
):
"FindShortestPath[graph_, s_, t_, OptionsPattern[FindShortestPath]]"
"expression: FindShortestPath[graph_, s_, t_, OptionsPattern[FindShortestPath]]"
graph = self._build_graph(graph, evaluation, options, expression)
if not graph:
return
Expand Down Expand Up @@ -1153,7 +1153,7 @@ class FindVertexCut(_NetworkXBuiltin):
summary_text = "find the vertex cuts"

def eval(self, graph, expression, evaluation, options):
"FindVertexCut[graph_, OptionsPattern[%(name)s]]"
"expression: FindVertexCut[graph_, OptionsPattern[%(name)s]]"
graph = self._build_graph(graph, evaluation, options, expression)
if graph:
if graph.empty() or not is_connected(graph.G):
Expand All @@ -1164,7 +1164,7 @@ def eval(self, graph, expression, evaluation, options):
)

def eval_st(self, graph, s, t, expression, evaluation, options):
"FindVertexCut[graph_, s_, t_, OptionsPattern[%(name)s]]"
"expression: FindVertexCut[graph_, s_, t_, OptionsPattern[%(name)s]]"
graph = self._build_graph(graph, evaluation, options, expression)
if not graph:
return
Expand Down Expand Up @@ -1248,7 +1248,7 @@ class HighlightGraph(_NetworkXBuiltin):
summary_text = "highlight elements in a graph"

def eval(self, graph, what, expression, evaluation, options):
"HighlightGraph[graph_, what_List, OptionsPattern[%(name)s]]"
"expression: HighlightGraph[graph_, what_List, OptionsPattern[%(name)s]]"
default_highlight = [Expression(SymbolRGBColor, Integer1, Integer0, Integer0)]

def parse(item):
Expand Down Expand Up @@ -1355,7 +1355,7 @@ class VertexAdd(_NetworkXBuiltin):
summary_text = "add a vertex"

def eval(self, graph: Expression, what, expression, evaluation, options):
"VertexAdd[graph_, what_, OptionsPattern[VertexAdd]]"
"expression: VertexAdd[graph_, what_, OptionsPattern[VertexAdd]]"
mathics_graph = self._build_graph(graph, evaluation, options, expression)
if mathics_graph:
if what.get_head_name() == "System`List":
Expand Down Expand Up @@ -1399,7 +1399,7 @@ class VertexConnectivity(_NetworkXBuiltin):
summary_text = "vertex connectivity"

def eval(self, graph, expression, evaluation, options):
"%(name)s[graph_, OptionsPattern[%(name)s]]"
"expression: %(name)s[graph_, OptionsPattern[%(name)s]]"
graph = self._build_graph(graph, evaluation, options, expression)
if graph and not graph.empty():
if not is_connected(graph.G):
Expand All @@ -1408,7 +1408,7 @@ def eval(self, graph, expression, evaluation, options):
return Integer(nx.node_connectivity(graph.G))

def eval_st(self, graph, s, t, expression, evaluation, options):
"%(name)s[graph_, s_, t_, OptionsPattern[%(name)s]]"
"expression: %(name)s[graph_, s_, t_, OptionsPattern[%(name)s]]"
graph = self._build_graph(graph, evaluation, options, expression)
if graph and not graph.empty():
if not is_connected(graph.G):
Expand Down Expand Up @@ -1440,7 +1440,7 @@ class VertexDelete(_NetworkXBuiltin):
summary_text = "remove a vertex"

def eval(self, graph, what, expression, evaluation, options) -> Optional[Graph]:
"VertexDelete[graph_, what_, OptionsPattern[VertexDelete]]"
"expression: VertexDelete[graph_, what_, OptionsPattern[VertexDelete]]"
graph = self._build_graph(graph, evaluation, options, expression)
if graph:
head_name = what.get_head_name()
Expand Down Expand Up @@ -1475,7 +1475,7 @@ class VertexIndex(_NetworkXBuiltin):
summary_text = "find the position of a vertex"

def eval(self, graph, v, expression, evaluation, options):
"%(name)s[graph_, v_, OptionsPattern[%(name)s]]"
"expression: %(name)s[graph_, v_, OptionsPattern[%(name)s]]"
graph = self._build_graph(graph, evaluation, options, expression)
if graph:
try:
Expand Down Expand Up @@ -1579,7 +1579,7 @@ class EdgeDelete(_NetworkXBuiltin):
summary_text = "remove an edge"

def eval(self, graph, what, expression, evaluation, options) -> Optional[Graph]:
"EdgeDelete[graph_, what_, OptionsPattern[EdgeDelete]]"
"expression: EdgeDelete[graph_, what_, OptionsPattern[EdgeDelete]]"
graph = self._build_graph(graph, evaluation, options, expression)
if graph:
head_name = what.get_head_name()
Expand Down
20 changes: 10 additions & 10 deletions pymathics/graph/centralities.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class BetweennessCentrality(_Centrality):
summary_text = "get Betweenness centrality"

def eval(self, graph, expression, evaluation, options):
"%(name)s[graph_, OptionsPattern[%(name)s]]"
"expression: %(name)s[graph_, OptionsPattern[%(name)s]]"
graph = self._build_graph(graph, evaluation, options, expression)
if graph:
weight = graph.update_weights(evaluation)
Expand Down Expand Up @@ -143,7 +143,7 @@ class ClosenessCentrality(_Centrality):
summary_text = "get the closeness centrality"

def eval(self, graph, expression, evaluation, options):
"%(name)s[graph_, OptionsPattern[%(name)s]]"
"expression: %(name)s[graph_, OptionsPattern[%(name)s]]"
graph = self._build_graph(graph, evaluation, options, expression)
if graph:
weight = graph.update_weights(evaluation)
Expand Down Expand Up @@ -196,19 +196,19 @@ def _from_dict(self, graph, centrality):
)

def eval(self, graph, expression, evaluation, options):
"Pymathics`DegreeCentrality[graph_, OptionsPattern[]]"
"expression: Pymathics`DegreeCentrality[graph_, OptionsPattern[]]"
graph = self._build_graph(graph, evaluation, options, expression)
if graph:
return self._from_dict(graph, nx.degree_centrality(graph.G))

def eval_in(self, graph, expression, evaluation, options):
'%(name)s[graph_, "In", OptionsPattern[]]'
'expression: %(name)s[graph_, "In", OptionsPattern[]]'
graph = self._build_graph(graph, evaluation, options, expression)
if graph:
return self._from_dict(graph, nx.in_degree_centrality(graph.G))

def eval_out(self, graph, expression, evaluation, options):
'%(name)s[graph_, "Out", OptionsPattern[]]'
'expression: %(name)s[graph_, "Out", OptionsPattern[]]'
graph = self._build_graph(graph, evaluation, options, expression)
if graph:
return self._from_dict(graph, nx.out_degree_centrality(graph.G))
Expand Down Expand Up @@ -263,13 +263,13 @@ def _centrality(self, g, weight):
return nx.eigenvector_centrality(g, max_iter=10000, tol=1.0e-7, weight=weight)

def eval(self, graph, expression, evaluation, options):
"%(name)s[graph_, OptionsPattern[%(name)s]]"
"expression: %(name)s[graph_, OptionsPattern[%(name)s]]"
graph = self._build_graph(graph, evaluation, options, expression)
if graph:
return self._compute(graph, evaluation)

def eval_in_out(self, graph, dir, expression, evaluation, options):
"%(name)s[graph_, dir_String, OptionsPattern[%(name)s]]"
"expression: %(name)s[graph_, dir_String, OptionsPattern[%(name)s]]"
py_dir = dir.get_string_value()
if py_dir not in ("In", "Out"):
return
Expand Down Expand Up @@ -299,7 +299,7 @@ class HITSCentrality(_Centrality):
summary_text = "get HITS centrality"

def eval(self, graph, expression, evaluation, options):
"%(name)s[graph_, OptionsPattern[%(name)s]]"
"expression: %(name)s[graph_, OptionsPattern[%(name)s]]"
graph = self._build_graph(graph, evaluation, options, expression)
if graph:
G, _ = graph.coalesced_graph(evaluation) # FIXME warn if weight > 1
Expand Down Expand Up @@ -364,7 +364,7 @@ def _centrality(self, g, weight, alpha, beta):
)

def eval(self, graph, alpha, beta, expression, evaluation, options):
"Pymathics`KatzCentrality[Pymathics`graph_, alpha_, beta_, OptionsPattern[%(name)s]]"
"expression: Pymathics`KatzCentrality[Pymathics`graph_, alpha_, beta_, OptionsPattern[%(name)s]]"
graph = self._build_graph(graph, evaluation, options, expression)
if graph:
try:
Expand Down Expand Up @@ -407,7 +407,7 @@ class PageRankCentrality(_Centrality):
summary_text = "get the page rank centralities"

def eval_alpha_beta(self, graph, alpha, expression, evaluation, options):
"%(name)s[graph_, alpha_, OptionsPattern[%(name)s]]"
"expression: %(name)s[graph_, alpha_, OptionsPattern[%(name)s]]"
graph = self._build_graph(graph, evaluation, options, expression)
if graph:
py_alpha = float(alpha.to_mpmath())
Expand Down
4 changes: 2 additions & 2 deletions pymathics/graph/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class ConnectedComponents(_NetworkXBuiltin):
def eval(
self, graph, expression, evaluation: Evaluation, options: dict
) -> Optional[ListExpression]:
"ConnectedComponents[graph_, OptionsPattern[%(name)s]]"
"expression: ConnectedComponents[graph_, OptionsPattern[%(name)s]]"
graph = self._build_graph(graph, evaluation, options, expression)
if graph:
connect_fn = (
Expand Down Expand Up @@ -123,7 +123,7 @@ class WeaklyConnectedComponents(_NetworkXBuiltin):
summary_text = "list the weakly connected components"

def eval(self, graph, expression, evaluation: Evaluation, options):
"WeaklyConnectedComponents[graph_, OptionsPattern[WeaklyConnectedComponents]]"
"expression: WeaklyConnectedComponents[graph_, OptionsPattern[WeaklyConnectedComponents]]"
graph = self._build_graph(graph, evaluation, options, expression)
if graph:
components = nx.connected_components(graph.G.to_undirected())
Expand Down
2 changes: 1 addition & 1 deletion pymathics/graph/curated.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class GraphData(_NetworkXBuiltin):
def eval(
self, name, expression, evaluation: Evaluation, options: dict
) -> Optional[Graph]:
"GraphData[name_String, OptionsPattern[GraphData]]"
"expression: GraphData[name_String, OptionsPattern[GraphData]]"
py_name = name.get_string_value()
fn, layout = WL_TO_NETWORKX_FN.get(py_name, (None, None))
if not fn:
Expand Down
8 changes: 4 additions & 4 deletions pymathics/graph/measures_and_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ class _PatternCount(_NetworkXBuiltin):
no_doc = True

def eval(self, graph, expression, evaluation, options) -> Optional[Integer]:
"%(name)s[graph_, OptionsPattern[%(name)s]]"
"expression: %(name)s[graph_, OptionsPattern[%(name)s]]"
graph = self._build_graph(graph, evaluation, options, expression)
if graph:
return Integer(len(self._items(graph)))

def eval_patt(
self, graph, patt, expression, evaluation, options
) -> Optional[Expression]:
"%(name)s[graph_, patt_, OptionsPattern[%(name)s]]"
"expression: %(name)s[graph_, patt_, OptionsPattern[%(name)s]]"
graph = self._build_graph(graph, evaluation, options, expression)
if graph:
return Expression(
Expand Down Expand Up @@ -132,7 +132,7 @@ class GraphDistance(_NetworkXBuiltin):
def eval_s(
self, graph, s, expression, evaluation: Evaluation, options: dict
) -> Optional[ListExpression]:
"GraphDistance[graph_, s_, OptionsPattern[GraphDistance]]"
"expression: GraphDistance[graph_, s_, OptionsPattern[GraphDistance]]"
graph = self._build_graph(graph, evaluation, options, expression)
if graph:
weight = graph.update_weights(evaluation)
Expand All @@ -141,7 +141,7 @@ def eval_s(
return to_mathics_list(*[d.get(v, inf) for v in graph.vertices])

def eval_s_t(self, graph, s, t, expression, evaluation: Evaluation, options: dict):
"GraphDistance[graph_, s_, t_, OptionsPattern[GraphDistance]]"
"expression: GraphDistance[graph_, s_, t_, OptionsPattern[GraphDistance]]"
graph = self._build_graph(graph, evaluation, options, expression)
if not graph:
return
Expand Down
2 changes: 1 addition & 1 deletion pymathics/graph/operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class FindSpanningTree(_NetworkXBuiltin):
summary_text = "find a spanning tree"

def eval(self, graph, expression, evaluation: Evaluation, options: dict):
"%(name)s[graph_, OptionsPattern[%(name)s]]"
"expression: %(name)s[graph_, OptionsPattern[%(name)s]]"
graph = self._build_graph(graph, evaluation, options, expression)
if graph:
graph.update_weights(evaluation)
Expand Down
Loading

0 comments on commit 89ad93f

Please sign in to comment.