Skip to content

Commit

Permalink
Get ready for release 2.3.0
Browse files Browse the repository at this point in the history
Fix lint errors
  • Loading branch information
rocky committed Jun 27, 2021
1 parent f1fc830 commit 437146f
Show file tree
Hide file tree
Showing 13 changed files with 163 additions and 43 deletions.
28 changes: 28 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# THis is an EditorConfig file
# https://EditorConfig.org

root = true

[*]
end_of_line = lf
insert_final_newline = true
charset = utf-8
indent_style = tab
indent_size = 4
insert_final_newline = true

[*.yml]
indent_style = space
indent_size = 2
end_of_line = lf
insert_final_newline = true

[*.py]
indent_style = space
indent_size = 4
end_of_line = lf
insert_final_newline = true

# Tab indentation (no size specified)
[Makefile]
indent_style = tab
12 changes: 12 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.0.0
hooks:
- id: debug-statements
- id: end-of-file-fixer
- repo: https://github.com/psf/black
rev: 20.8b1
hooks:
- id: black
language_version: python3
exclude: 'mathicsscript/version.py'
9 changes: 9 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
2.3.0
-----

Small API changes to track Mathics 3.0.0.

Blacken everything



2.2.0
-----

Expand Down
6 changes: 3 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ Example Session
::

$ mathicsscript
Mathics 1.1.1
Mathics 3.0.1
on CPython 3.6.12 (default, Oct 24 2020, 10:34:18)
using SymPy 1.8.dev, mpmath 1.1.0, cython 0.29.21
using SymPy 1.8, mpmath 1.2.1, cython 0.29.22

Copyright (C) 2011-2020 The Mathics Team.
Copyright (C) 2011-2021 The Mathics Team.
This program comes with ABSOLUTELY NO WARRANTY.
This is free software, and you are welcome to redistribute it
under certain conditions.
Expand Down
18 changes: 12 additions & 6 deletions pymathics/graph/__init__.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
"""Pymathics Graph - Working with Graphs (Vertices and Edgies)
"""Pymathics Graph - Working with Graphs (Vertices and Edges)
This module provides functions and variables for workting with
graphs.
"""
Example:
In[1]:= LoadModule["pymathics.graph"]
Out[1]= pymathics.graph
In[2]:= BinomialTree[3]
In[3]:= BinomialTree[6]
In[4]:= CompleteKaryTree[3, VertexLabels->True]
"""

from pymathics.graph.version import __version__
from pymathics.graph.__main__ import *
from pymathics.graph.tree import *
from pymathics.graph.generators import *
from pymathics.graph.algorithms import *
from pymathics.graph.__main__ import * # noqa
from pymathics.graph.tree import * # qoqa
from pymathics.graph.generators import * # noqa
from pymathics.graph.algorithms import * # noqa

pymathics_version_data = {
"author": "The Mathics Team",
Expand Down
7 changes: 3 additions & 4 deletions pymathics/graph/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@
# uses networkx

from mathics.builtin.base import Builtin, AtomBuiltin
from mathics.builtin.graphics import GraphicsBox
from mathics.builtin.box.graphics import GraphicsBox
from mathics.core.expression import (
Atom,
Expression,
Integer,
Real,
String,
Symbol,
)
from mathics.builtin.patterns import Matcher
Expand Down Expand Up @@ -730,7 +729,7 @@ def full_new_edge_properties(new_edge_style):
attr_dict = attr_dict or empty_dict
G.add_edge(u, v, **attr_dict)

edge_collection = _EdgeCollection(
_EdgeCollection(
edges,
edge_properties,
n_directed=len(directed_edges),
Expand Down Expand Up @@ -1804,7 +1803,7 @@ def apply_s_t(self, graph, s, t, expression, evaluation, options):
def _convert_networkx_graph(G, options):
mapping = dict((v, Integer(i)) for i, v in enumerate(G.nodes))
G = nx.relabel_nodes(G, mapping)
edges = [Expression("System`UndirectedEdge", u, v) for u, v in G.edges]
[Expression("System`UndirectedEdge", u, v) for u, v in G.edges]
return Graph(
G,
**options,
Expand Down
26 changes: 18 additions & 8 deletions pymathics/graph/algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
# SymbolFalse = Symbol("System`False")
# SymbolTrue = Symbol("System`True")


class ConnectedComponents(_NetworkXBuiltin):
"""
>> g = Graph[{1 -> 2, 2 -> 3, 3 <-> 4}]; ConnectedComponents[g]
Expand All @@ -38,10 +39,12 @@ def apply(self, graph, expression, evaluation, options):
"ConnectedComponents[graph_, OptionsPattern[%(name)s]]"
graph = self._build_graph(graph, evaluation, options, expression)
if graph:
connect_fn = nx.strongly_connected_components if graph.G.is_directed() else nx.connected_components
components = [
Expression("List", *c) for c in connect_fn(graph.G)
]
connect_fn = (
nx.strongly_connected_components
if graph.G.is_directed()
else nx.connected_components
)
components = [Expression("List", *c) for c in connect_fn(graph.G)]
return Expression("List", *components)


Expand All @@ -63,6 +66,7 @@ def apply(self, graph, expression, evaluation, options):
# # int_path = map(Integer, path)
# return Expression("List", *path)


class GraphDistance(_NetworkXBuiltin):
"""
<dl>
Expand Down Expand Up @@ -110,9 +114,7 @@ def apply_s(self, graph, s, expression, evaluation, options):
weight = graph.update_weights(evaluation)
d = nx.shortest_path_length(graph.G, source=s, weight=weight)
inf = Expression("DirectedInfinity", 1)
return Expression(
"List", *[d.get(v, inf) for v in graph.vertices]
)
return Expression("List", *[d.get(v, inf) for v in graph.vertices])

def apply_s_t(self, graph, s, t, expression, evaluation, options):
"%(name)s[graph_, s_, t_, OptionsPattern[%(name)s]]"
Expand All @@ -133,6 +135,7 @@ def apply_s_t(self, graph, s, t, expression, evaluation, options):
except nx.exception.NetworkXNoPath:
return Expression("DirectedInfinity", 1)


class FindSpanningTree(_NetworkXBuiltin):
"""
<dl>
Expand All @@ -144,14 +147,18 @@ class FindSpanningTree(_NetworkXBuiltin):
"""

options = DEFAULT_GRAPH_OPTIONS

def apply(self, graph, expression, evaluation, options):
"%(name)s[graph_, OptionsPattern[%(name)s]]"
graph = self._build_graph(graph, evaluation, options, expression)
if graph:
weight = graph.update_weights(evaluation)
edge_type = "DirectedEdge" if graph.G.is_directed() else "UndirectedEdge"
# FIXME: put in edge to Graph conversion function?
edges = [Expression("UndirectedEdge", u, v) for u, v in nx.minimum_spanning_edges(graph.G, data=False)]
edges = [
Expression("UndirectedEdge", u, v)
for u, v in nx.minimum_spanning_edges(graph.G, data=False)
]
g = _create_graph(edges, [None] * len(edges), options)
if not hasattr(g.G, "graph_layout"):
if hasattr(graph.G, "graph_layout"):
Expand All @@ -160,6 +167,7 @@ def apply(self, graph, expression, evaluation, options):
g.G.graph_layout = "tree"
return g


class PlanarGraphQ(_NetworkXBuiltin):
"""
<dl>
Expand All @@ -174,6 +182,7 @@ class PlanarGraphQ(_NetworkXBuiltin):
"""

options = DEFAULT_GRAPH_OPTIONS

def apply(self, graph, expression, evaluation, options):
"%(name)s[graph_, OptionsPattern[%(name)s]]"
graph = self._build_graph(graph, evaluation, options, expression)
Expand All @@ -182,6 +191,7 @@ def apply(self, graph, expression, evaluation, options):
is_planar, _ = nx.check_planarity(graph.G)
return Symbol("System`True") if is_planar else Symbol("System`False")


class WeaklyConnectedComponents(_NetworkXBuiltin):
"""
>> g = Graph[{1 -> 2, 2 -> 3, 3 <-> 4}]; WeaklyConnectedComponents[g]
Expand Down
36 changes: 21 additions & 15 deletions pymathics/graph/generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@
"""

from mathics.builtin.numbers.randomnumbers import RandomEnv
from mathics.core.expression import String

from pymathics.graph.__main__ import (
Graph,
WL_MARKER_TO_NETWORKX,
_NetworkXBuiltin,
_convert_networkx_graph,
_graph_from_list,
Expand All @@ -32,6 +30,7 @@ def graph_helper(
options: dict,
can_digraph: bool,
graph_layout: str,
evaluation,
root: Optional[int] = None,
*args,
**kwargs
Expand Down Expand Up @@ -94,7 +93,7 @@ def apply(self, r, h, expression, evaluation, options):
return None

args = (py_r, py_h)
g = graph_helper(nx.balanced_tree, options, True, "tree", 0, *args)
g = graph_helper(nx.balanced_tree, options, True, "tree", evaluation, 0, *args)
if not g:
return None
g.G.r = r
Expand Down Expand Up @@ -133,7 +132,7 @@ def apply(self, m1, m2, expression, evaluation, options):
return

args = (py_m1, py_m2)
g = graph_helper(nx.barbell_graph, options, False, "spring", None, *args)
g = graph_helper(nx.barbell_graph, options, False, "spring", evaluation, None, *args)
if not g:
return None

Expand Down Expand Up @@ -205,7 +204,7 @@ def apply(self, n, expression, evaluation, options):
return

args = (py_n,)
g = graph_helper(binomial_tree, options, True, "tree", 0, *args)
g = graph_helper(binomial_tree, options, True, "tree", evaluation, 0, *args)
if not g:
return None
g.G.n = n
Expand All @@ -220,7 +219,7 @@ def complete_graph_apply(self, n, expression, evaluation, options):
return

args = (py_n,)
g = graph_helper(nx.complete_graph, options, False, "circular", None, *args)
g = graph_helper(nx.complete_graph, options, False, "circular", evaluation, None, *args)
if not g:
return None

Expand Down Expand Up @@ -333,7 +332,7 @@ def f_r_t_apply(self, r, n, expression, evaluation, options):
return

args = (py_r, py_n)
g = graph_helper(nx.full_rary_tree, options, True, "tree", 0, *args)
g = graph_helper(nx.full_rary_tree, options, True, "tree", evaluation, 0, *args)
if not g:
return None

Expand Down Expand Up @@ -395,7 +394,7 @@ def apply(self, n, expression, evaluation, options):
return

args = (py_n,)
g = graph_helper(nx.graph_atlas, options, False, "spring", None, *args)
g = graph_helper(nx.graph_atlas, options, False, "spring", evaluation, None, *args)
if not g:
return None
g.n = n
Expand All @@ -417,7 +416,7 @@ def hkn_harary_apply(self, k, n, expression, evaluation, options):
from pymathics.graph.harary import hkn_harary_graph

args = (py_k, py_n)
g = graph_helper(hkn_harary_graph, options, False, "circular", None, *args)
g = graph_helper(hkn_harary_graph, options, False, "circular", evaluation, None, *args)
if not g:
return None
g.k = py_k
Expand Down Expand Up @@ -490,7 +489,7 @@ def apply(self, n, m, expression, evaluation, options):
from pymathics.graph.harary import hnm_harary_graph

args = (py_n, py_m)
g = graph_helper(hmn_harary_graph, options, False, "circular", None, *args)
g = graph_helper(hnm_harary_graph, options, False, "circular", evaluation, None, *args)
if not g:
return None
g.n = py_n
Expand Down Expand Up @@ -557,12 +556,13 @@ def apply(self, n, expression, evaluation, options):
return

args = (py_n,)
g = graph_helper(nx.ladder_graph, options, False, "spring", 0, *args)
g = graph_helper(nx.ladder_graph, options, False, "spring", evaluation, 0, *args)
if not g:
return None
g.G.n = n
return g


class PathGraph(_NetworkXBuiltin):
"""
<dl>
Expand All @@ -582,7 +582,9 @@ def edges():
yield Expression("UndirectedEdge", u, v)

g = _graph_from_list(edges(), options)
g.G.graph_layout = options["System`GraphLayout"].get_string_value() or "spiral_equidistant"
g.G.graph_layout = (
options["System`GraphLayout"].get_string_value() or "spiral_equidistant"
)
return g


Expand Down Expand Up @@ -645,7 +647,7 @@ def apply(self, n, expression, evaluation, options):
return

args = (py_n,)
g = graph_helper(nx.random_tree, options, False, "tree", 0, *args)
g = graph_helper(nx.random_tree, options, False, "tree", evaluation, 0, *args)
if not g:
return None
g.G.n = n
Expand Down Expand Up @@ -676,12 +678,13 @@ def apply(self, n, expression, evaluation, options):
return

args = (py_n,)
g = graph_helper(nx.star_graph, options, False, "spring", 0, *args)
g = graph_helper(nx.star_graph, options, False, "spring", evaluation, 0, *args)
if not g:
return None
g.G.n = n
return g


WL_TO_NETWORKX_FN = {
"DodecahedralGraph": (nx.dodecahedral_graph, None),
"DiamondGraph": (nx.diamond_graph, "spring"),
Expand All @@ -690,6 +693,7 @@ def apply(self, n, expression, evaluation, options):
"PetersenGraph": (nx.petersen_graph, None),
}


class GraphData(_NetworkXBuiltin):
"""
<dl>
Expand All @@ -699,6 +703,7 @@ class GraphData(_NetworkXBuiltin):
>> GraphData["PappusGraph"]
"""

def apply(self, name, expression, evaluation, options):
"%(name)s[name_String, OptionsPattern[%(name)s]]"
py_name = name.get_string_value()
Expand All @@ -710,11 +715,12 @@ def apply(self, name, expression, evaluation, options):
# These graphs require parameters
return
import inspect

fn = dict(inspect.getmembers(nx, inspect.isfunction)).get(py_name, None)
# parameters = inspect.signature(nx.diamond_graph).parameters.values()
# if len([p for p in list(parameters) if p.kind in [inspect.Parameter.POSITIONAL_ONLY, inspect.Parameter.POSITIONAL_OR_KEYWORD]]) != 0:
# return
if fn:
g = graph_helper(fn, options, False, layout)
g = graph_helper(fn, options, False, evaluation, layout)
g.G.name = py_name
return g
Loading

0 comments on commit 437146f

Please sign in to comment.