Skip to content

Commit

Permalink
use sage Feature rather than require_package
Browse files Browse the repository at this point in the history
  • Loading branch information
videlec committed Nov 25, 2023
1 parent 894f8a5 commit 3f3dcbf
Show file tree
Hide file tree
Showing 12 changed files with 129 additions and 161 deletions.
11 changes: 3 additions & 8 deletions veerer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,6 @@
from .linear_family import VeeringTriangulationLinearFamily
from .automaton import FlipGraph, CoreAutomaton, ReducedCoreAutomaton, GeometricAutomaton, GeometricAutomatonSubspace
from .flip_sequence import VeeringFlipSequence

from .env import sage
if sage is not None:
from .flat_structure import FlatVeeringTriangulation
from .layout import FlatVeeringTriangulationLayout
from .measured_train_track import MeasuredTrainTrack

del sage
from .flat_structure import FlatVeeringTriangulation
from .layout import FlatVeeringTriangulationLayout
from .measured_train_track import MeasuredTrainTrack
42 changes: 20 additions & 22 deletions veerer/automaton.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@
from .linear_family import VeeringTriangulationLinearFamily
from .constants import RED, BLUE, PURPLE, PROPERTIES_COLOURS, colour_to_char, colour_to_string
from .permutation import perm_invert

from . import env
from .env import sage, require_package
from .env import CHECK


class Automaton(object):
Expand Down Expand Up @@ -517,7 +515,7 @@ def set_seed(self, state):
state = self._seed_setup(state)

# TODO: check to be removed
if env.CHECK:
if CHECK:
state._check()

if self._verbosity:
Expand Down Expand Up @@ -604,7 +602,7 @@ def run(self, max_size=None):
new_flip = flip_branch[-1].pop()

# TODO: check to be removed
if env.CHECK:
if CHECK:
T._check()

if self._verbosity >= 2:
Expand Down Expand Up @@ -670,7 +668,7 @@ def run(self, max_size=None):
self._flip_back(flip_back_data)

# TODO: check to be removed
if env.CHECK:
if CHECK:
T._check()
assert T == state_branch[-1], (T, state_branch[-1])

Expand Down Expand Up @@ -726,12 +724,12 @@ def _forward_flips(self, state):

def _flip(self, flip_data):
e = flip_data
self._state.flip(e, check=env.CHECK)
self._state.flip(e, check=CHECK)
return True, e

def _flip_back(self, flip_back_data):
e = flip_back_data
self._state.flip_back(e, check=env.CHECK)
self._state.flip_back(e, check=CHECK)


class CoreAutomaton(Automaton):
Expand All @@ -757,13 +755,13 @@ def _forward_flips(self, state):
def _flip(self, flip_data):
e, col = flip_data
old_col = self._state.colour(e)
self._state.flip(e, col, check=env.CHECK)
self._state.flip(e, col, check=CHECK)
flip_back_data = (e, old_col)
return self._state.edge_has_curve(e), flip_back_data

def _flip_back(self, flip_back_data):
e, old_col = flip_back_data
self._state.flip_back(e, old_col, check=env.CHECK)
self._state.flip_back(e, old_col, check=CHECK)


class ReducedCoreAutomaton(Automaton):
Expand All @@ -784,7 +782,7 @@ def _forward_flips(self, state):
"""
ffe = state.purple_edges()
# TODO: check to be removed
if env.CHECK:
if CHECK:
assert ffe == state.forward_flippable_edges()
return [(x, col) for x in ffe for col in (BLUE, RED)]

Expand All @@ -793,10 +791,10 @@ def _flip(self, flip_data):
e, col = flip_data

# TODO: check to be removed
if env.CHECK:
if CHECK:
assert T.is_forward_flippable(e)
old_col = self._state.colour(e)
self._state.flip(e, col, reduced=False, check=env.CHECK)
self._state.flip(e, col, reduced=False, check=CHECK)

if not self._state.edge_has_curve(e):
return False, (e, old_col, ())
Expand All @@ -821,7 +819,7 @@ def _flip(self, flip_data):
T._colouring[T._ep[d]] = PURPLE

# assertions to be removed
if env.CHECK:
if CHECK:
assert not T.is_forward_flippable(e)
assert not T.is_forward_flippable(a)
assert not T.is_forward_flippable(c)
Expand All @@ -837,7 +835,7 @@ def _flip(self, flip_data):
T._colouring[T._ep[c]] = PURPLE

# assertions to be removed
if env.CHECK:
if CHECK:
assert not T.is_forward_flippable(e)
assert not T.is_forward_flippable(b)
assert not T.is_forward_flippable(d)
Expand All @@ -849,7 +847,7 @@ def _flip_back(self, flip_back_data):
for ee, ccol in recolorings:
self._state._colouring[ee] = ccol
self._state._colouring[self._state._ep[ee]] = ccol
self._state.flip_back(e, old_col, check=env.CHECK)
self._state.flip_back(e, old_col, check=CHECK)


class GeometricAutomaton(Automaton):
Expand Down Expand Up @@ -900,15 +898,15 @@ def _flip(self, flip_data):
assert all(self._state.colour(e) == self._state.colour(edges[0]) for e in edges)
flip_back_data = (edges, self._state.colour(edges[0]))
for e in edges:
self._state.flip(e, col, check=env.CHECK)
if env.CHECK and not self._state.is_geometric(backend=self._backend):
self._state.flip(e, col, check=CHECK)
if CHECK and not self._state.is_geometric(backend=self._backend):
raise RuntimeError('that was indeed possible!')
return True, flip_back_data

def _flip_back(self, flip_back_data):
edges, old_col = flip_back_data
for e in edges:
self._state.flip_back(e, old_col, check=env.CHECK)
self._state.flip_back(e, old_col, check=CHECK)


class GeometricAutomatonSubspace(Automaton):
Expand Down Expand Up @@ -970,8 +968,8 @@ def _flip(self, flip_data):
assert all(self._state.colour(e) == self._state.colour(edges[0]) for e in edges)
flip_back_data = (edges, self._state.colour(edges[0]))
for e in edges:
self._state.flip(e, col, check=env.CHECK)
if env.CHECK:
self._state.flip(e, col, check=CHECK)
if CHECK:
self._state._check(RuntimeError)
if not self._state.is_geometric(backend=self._backend):
raise RuntimeError
Expand All @@ -980,4 +978,4 @@ def _flip(self, flip_data):
def _flip_back(self, flip_back_data):
edges, old_col = flip_back_data
for e in edges:
self._state.flip_back(e, old_col, check=env.CHECK)
self._state.flip_back(e, old_col, check=CHECK)
82 changes: 18 additions & 64 deletions veerer/env.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
r"""
Various environment modules.
TESTS::
sage: from veerer.env import sage, flipper, surface_dynamics, ppl # random output due to deprecation warnings from realalg
Tests for optional packages used by veerer.
"""
# ****************************************************************************
# This file is part of veerer
Expand All @@ -28,61 +23,20 @@

CHECK = False

import sage.all
import sage
import sage.misc.prandom as random
from sage.structure.richcmp import op_LT, op_LE, op_EQ, op_NE, op_GT, op_GE, rich_to_bool


try:
import surface_dynamics
except ImportError:
surface_dynamics = None


try:
import flipper
except ImportError:
flipper = None

try:
import curver
except ImportError:
curver = None

try:
import ppl
except ImportError:
ppl = None


try:
import PyNormaliz
except ImportError:
PyNormaliz = None

error_msg = {
'curver': 'the function {} can only be called when the package curver is installed.',

'surface_dynamics': 'the function {} only works when the package surface_dynamics is installed. See https://pypi.org/project/surface_dynamics/ for instructions.',

'flipper': 'the function {} only works when the package flipper is installed. See https://pypi.org/project/flipper/ for instructions',

'ppl': 'the function {} only works when the package pplpy is installed. See https://pypi.org/project/pplpy/ for instructions.',

'PyNormaliz': 'the function {} only works when the package PyNormaliz is installed.'
}

missing_mods = {
'curver': curver is None,
'flipper': flipper is None,
'ppl': ppl is None,
'surface_dynamics': surface_dynamics is None,
'PyNormaliz': PyNormaliz is None
}

# TODO: use the traceback to find out who called this function!
# https://docs.python.org/2/library/traceback.html#traceback-examples
def require_package(mod_name, caller):
if missing_mods[mod_name]:
raise ValueError(error_msg[mod_name].format(caller))
from sage.features import PythonModule

pyflatsurf_feature = PythonModule(
"pyflatsurf", url="https://github.com/flatsurf/flatsurf/#install-with-conda"
)
surface_dynamics_feature = PythonModule(
"surface-dynamics", url="https://flatsurf.github.io/surface-dynamics/#installation"
)
flipper_feature = PythonModule(
"flipper", url="https://flipper.readthedocs.io/en/latest/user/install.html"
)
curver_feature = PythonModule(
"curver", url="https://curver.readthedocs.io/en/master/user/install.html"
)
pynormaliz_feature = PythonModule(
"PyNormaliz", url="https://github.com/Normaliz/PyNormaliz"
)
21 changes: 21 additions & 0 deletions veerer/features.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
r"""
Tests for optional packages used by veerer.
"""

from sage.features import PythonModule

flatsurf_feature = PythonModule(
"flatsurf", url="https://flatsurf.github.io/sage-flatsurf/#installation"
)
surface_dynamics_feature = PythonModule(
"surface_dynamics", url="https://flatsurf.github.io/surface-dynamics/#installation"
)
flipper_feature = PythonModule(
"veerer", url="https://flatsurf.github.io/veerer/installation.html"
)
curver_feature = PythonModule(
"curver", url="https://curver.readthedocs.io/en/master/user/install.html"
)
pynormaliz_feature = PythonModule(
"PyNormaliz", url="https://github.com/Normaliz/PyNormaliz"
)
2 changes: 1 addition & 1 deletion veerer/flip_sequence.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
from .constants import colour_from_char, colour_to_char, RED, BLUE, PURPLE, GREEN, HORIZONTAL, VERTICAL
from .permutation import perm_init, perm_check, perm_id, perm_is_one, perm_preimage, perm_invert, perm_cycle_string, perm_compose, perm_pow, perm_conjugate
from .veering_triangulation import VeeringTriangulation
from .env import require_package, sage, ppl


def flip_sequence_to_string(sequence):
return " ".join("%d%s" % (e, colour_to_char(col)) for e,col in sequence)
Expand Down
34 changes: 11 additions & 23 deletions veerer/linear_family.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,33 +28,21 @@
import numbers
from random import choice, shuffle

from .env import sage, ppl
from sage.structure.element import get_coercion_model, Matrix
from sage.rings.integer_ring import ZZ
from sage.rings.rational_field import QQ
from sage.matrix.constructor import matrix
from sage.geometry.polyhedron.constructor import Polyhedron
from sage.arith.misc import gcd
from sage.categories.number_fields import NumberFields

from .constants import VERTICAL, HORIZONTAL, BLUE, RED
from .permutation import perm_cycle_string, perm_cycles, perm_check, perm_conjugate, perm_on_list
from .polyhedron import LinearExpressions, ConstraintSystem
from .veering_triangulation import VeeringTriangulation


if sage is not None:
from sage.structure.element import get_coercion_model
from sage.rings.integer_ring import ZZ
from sage.rings.rational_field import QQ
from sage.matrix.constructor import matrix
from sage.geometry.polyhedron.constructor import Polyhedron
from sage.arith.misc import gcd
from sage.categories.number_fields import NumberFields

cm = get_coercion_model()
_NumberFields = NumberFields()
else:
matrix = None
Polyhedron = None
gcd = None
ZZ = None
QQ = None

cm = None
_NumberFields = None
cm = get_coercion_model()
_NumberFields = NumberFields()


def subspace_are_equal(subspace1, subspace2, check=True):
Expand Down Expand Up @@ -210,7 +198,7 @@ def __init__(self, *args, mutable=False, check=True):
t, colouring, subspace = args
VeeringTriangulation.__init__(self, t, colouring, mutable=mutable, check=False)

if not isinstance(subspace, sage.structure.element.Matrix):
if not isinstance(subspace, Matrix):
subspace = matrix(subspace)

self._subspace = subspace
Expand Down
Loading

0 comments on commit 3f3dcbf

Please sign in to comment.