Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Drop Python 2 support #172

Merged
merged 4 commits into from Jul 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 0 additions & 1 deletion docs/requirements.txt
Expand Up @@ -2,5 +2,4 @@ contextlib2
multipledispatch
numpy>=1.7
opt_einsum>=2.3.2
six>=1.10.0
unification
2 changes: 0 additions & 2 deletions examples/discrete_hmm.py
@@ -1,5 +1,3 @@
from __future__ import absolute_import, division, print_function

import argparse
from collections import OrderedDict

Expand Down
2 changes: 0 additions & 2 deletions examples/kalman_filter.py
@@ -1,5 +1,3 @@
from __future__ import absolute_import, division, print_function

import argparse

import torch
Expand Down
2 changes: 0 additions & 2 deletions examples/minipyro.py
@@ -1,5 +1,3 @@
from __future__ import absolute_import, division, print_function

import argparse

import torch
Expand Down
2 changes: 0 additions & 2 deletions examples/pcfg.py
@@ -1,5 +1,3 @@
from __future__ import absolute_import, division, print_function

import argparse
import math
from collections import OrderedDict
Expand Down
2 changes: 0 additions & 2 deletions examples/slds.py
@@ -1,5 +1,3 @@
from __future__ import absolute_import, division, print_function

import argparse

import torch
Expand Down
2 changes: 0 additions & 2 deletions examples/ss_vae_delayed.py
@@ -1,5 +1,3 @@
from __future__ import absolute_import, division, print_function

import argparse
from collections import OrderedDict

Expand Down
2 changes: 0 additions & 2 deletions examples/vae.py
@@ -1,5 +1,3 @@
from __future__ import absolute_import, division, print_function

import argparse
import os
from collections import OrderedDict
Expand Down
2 changes: 0 additions & 2 deletions funsor/__init__.py
@@ -1,5 +1,3 @@
from __future__ import absolute_import, division, print_function

from funsor.domains import Domain, bint, find_domain, reals
from funsor.integrate import Integrate
from funsor.interpreter import reinterpret
Expand Down
2 changes: 0 additions & 2 deletions funsor/adjoint.py
@@ -1,5 +1,3 @@
from __future__ import absolute_import, division, print_function

from collections import defaultdict

import torch
Expand Down
2 changes: 0 additions & 2 deletions funsor/affine.py
@@ -1,5 +1,3 @@
from __future__ import absolute_import, division, print_function

from collections import OrderedDict

import funsor.ops as ops
Expand Down
2 changes: 0 additions & 2 deletions funsor/contract.py
@@ -1,5 +1,3 @@
from __future__ import absolute_import, division, print_function

import functools
from collections import OrderedDict

Expand Down
7 changes: 1 addition & 6 deletions funsor/delta.py
@@ -1,9 +1,5 @@
from __future__ import absolute_import, division, print_function

from collections import OrderedDict

from six import add_metaclass

import funsor.ops as ops
import funsor.terms
from funsor.domains import Domain, reals
Expand Down Expand Up @@ -38,8 +34,7 @@ def __call__(cls, name, point, log_density=0):
return super(DeltaMeta, cls).__call__(name, point, log_density)


@add_metaclass(DeltaMeta)
class Delta(Funsor):
class Delta(Funsor, metaclass=DeltaMeta):
"""
Normalized delta distribution binding a single variable.

Expand Down
6 changes: 1 addition & 5 deletions funsor/distributions.py
@@ -1,12 +1,9 @@
from __future__ import absolute_import, division, print_function

import math
from collections import OrderedDict

import pyro.distributions as dist
import torch
from pyro.distributions.util import broadcast_shape
from six import add_metaclass

import funsor.delta
import funsor.ops as ops
Expand Down Expand Up @@ -55,8 +52,7 @@ def __call__(cls, *args, **kwargs):
return super(DistributionMeta, cls).__call__(*args)


@add_metaclass(DistributionMeta)
class Distribution(Funsor):
class Distribution(Funsor, metaclass=DistributionMeta):
"""
Funsor backed by a PyTorch distribution object.
"""
Expand Down
17 changes: 7 additions & 10 deletions funsor/domains.py
@@ -1,15 +1,12 @@
from __future__ import absolute_import, division, print_function

import operator
from collections import namedtuple
from functools import reduce

import torch
from pyro.distributions.util import broadcast_shape
from six import integer_types
from six.moves import reduce

import funsor.ops as ops
from funsor.util import lazy_property
import torch


class Domain(namedtuple('Domain', ['shape', 'dtype'])):
Expand All @@ -21,8 +18,8 @@ def __new__(cls, shape, dtype):
assert isinstance(shape, tuple)
if torch._C._get_tracing_state():
shape = tuple(map(int, shape))
assert all(isinstance(size, integer_types) for size in shape), shape
if isinstance(dtype, integer_types):
assert all(isinstance(size, int) for size in shape), shape
if isinstance(dtype, int):
assert not shape
elif isinstance(dtype, str):
assert dtype == 'real'
Expand All @@ -32,7 +29,7 @@ def __new__(cls, shape, dtype):

def __repr__(self):
shape = tuple(self.shape)
if isinstance(self.dtype, integer_types):
if isinstance(self.dtype, int):
if not shape:
return 'bint({})'.format(self.dtype)
return 'bint({}, {})'.format(self.dtype, shape)
Expand All @@ -41,7 +38,7 @@ def __repr__(self):
return 'reals{}'.format(shape)

def __iter__(self):
if isinstance(self.dtype, integer_types) and not self.shape:
if isinstance(self.dtype, int) and not self.shape:
from funsor.terms import Number
return (Number(i, self.dtype) for i in range(self.dtype))
raise NotImplementedError
Expand Down Expand Up @@ -69,7 +66,7 @@ def bint(size):
"""
if torch._C._get_tracing_state():
size = int(size)
assert isinstance(size, integer_types) and size >= 0
assert isinstance(size, int) and size >= 0
return Domain((), size)


Expand Down
7 changes: 2 additions & 5 deletions funsor/einsum.py
@@ -1,10 +1,7 @@
from __future__ import absolute_import, division, print_function

from collections import OrderedDict
from functools import reduce

import torch
from six import integer_types
from six.moves import reduce

import funsor.ops as ops
from funsor.contract import Contract
Expand All @@ -16,7 +13,7 @@


def _make_base_lhs(prod_op, arg, reduced_vars, normalized=False):
if not all(isinstance(d.dtype, integer_types) for d in arg.inputs.values()):
if not all(isinstance(d.dtype, int) for d in arg.inputs.values()):
raise NotImplementedError("TODO implement continuous base lhss")

if prod_op not in (ops.add, ops.mul):
Expand Down
12 changes: 4 additions & 8 deletions funsor/gaussian.py
@@ -1,13 +1,10 @@
from __future__ import absolute_import, division, print_function

import math
import warnings
from collections import OrderedDict, defaultdict
from functools import reduce

import torch
from pyro.distributions.util import broadcast_shape
from six import add_metaclass, integer_types
from six.moves import reduce

import funsor.ops as ops
from funsor.delta import Delta
Expand Down Expand Up @@ -140,7 +137,7 @@ def _parse_slices(index, value):
for pos, i in reversed(list(enumerate(index))):
if isinstance(i, slice):
start_stops.append((i.start, i.stop))
elif isinstance(i, integer_types):
elif isinstance(i, int):
start_stops.append((i, i + 1))
value = value.unsqueeze(pos - len(index))
else:
Expand Down Expand Up @@ -288,8 +285,7 @@ def __call__(cls, loc, precision, inputs):
return super(GaussianMeta, cls).__call__(loc, precision, inputs)


@add_metaclass(GaussianMeta)
class Gaussian(Funsor):
class Gaussian(Funsor, metaclass=GaussianMeta):
"""
Funsor representing a batched joint Gaussian distribution as a log-density
function.
Expand All @@ -316,7 +312,7 @@ def __init__(self, loc, precision, inputs):

# Compute total shape of all bint inputs.
batch_shape = tuple(d.dtype for d in inputs.values()
if isinstance(d.dtype, integer_types))
if isinstance(d.dtype, int))
if not torch._C._get_tracing_state():
assert _issubshape(loc.shape, batch_shape + (dim,))
assert _issubshape(precision.shape, batch_shape + (dim, dim))
Expand Down
2 changes: 0 additions & 2 deletions funsor/integrate.py
@@ -1,5 +1,3 @@
from __future__ import absolute_import, division, print_function

import functools
from collections import OrderedDict

Expand Down
4 changes: 1 addition & 3 deletions funsor/interpreter.py
@@ -1,11 +1,10 @@
from __future__ import absolute_import, division, print_function

import functools
import inspect
import os
import re
import types
from collections import OrderedDict
from functools import singledispatch

import numpy
import torch
Expand All @@ -14,7 +13,6 @@
from funsor.domains import Domain
from funsor.ops import Op
from funsor.registry import KeyedRegistry
from funsor.six import singledispatch

_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
_DEBUG = int(os.environ.get("FUNSOR_DEBUG", 0))
Expand Down
9 changes: 2 additions & 7 deletions funsor/joint.py
@@ -1,11 +1,7 @@
from __future__ import absolute_import, division, print_function

import functools
import math
from collections import OrderedDict

from six import add_metaclass
from six.moves import reduce
from functools import reduce

import funsor.interpreter as interpreter
import funsor.ops as ops
Expand Down Expand Up @@ -43,8 +39,7 @@ def __call__(cls, deltas=(), discrete=0, gaussian=0):
return super(JointMeta, cls).__call__(deltas, discrete, gaussian)


@add_metaclass(JointMeta)
class Joint(Funsor):
class Joint(Funsor, metaclass=JointMeta):
"""
Normal form for a joint log probability density funsor.

Expand Down
2 changes: 0 additions & 2 deletions funsor/minipyro.py
Expand Up @@ -11,8 +11,6 @@
An accompanying example that makes use of this implementation can be
found at examples/minipyro.py.
"""
from __future__ import absolute_import, division, print_function

import functools
import warnings
import weakref
Expand Down
2 changes: 0 additions & 2 deletions funsor/montecarlo.py
@@ -1,5 +1,3 @@
from __future__ import absolute_import, division, print_function

from collections import OrderedDict

from contextlib2 import contextmanager
Expand Down
12 changes: 4 additions & 8 deletions funsor/numpy.py
@@ -1,10 +1,7 @@
from __future__ import absolute_import, division, print_function

from collections import OrderedDict

import numpy as np
from multipledispatch import dispatch
from six import add_metaclass, integer_types

import funsor.ops as ops
from funsor.domains import Domain, bint, find_domain
Expand All @@ -24,7 +21,7 @@ def align_array(new_inputs, x):
"""
assert isinstance(new_inputs, OrderedDict)
assert isinstance(x, (Number, Array))
assert all(isinstance(d.dtype, integer_types) for d in x.inputs.values())
assert all(isinstance(d.dtype, int) for d in x.inputs.values())

data = x.data
if isinstance(x, Number):
Expand Down Expand Up @@ -77,8 +74,7 @@ def __call__(cls, data, inputs=None, dtype="real"):
return super(ArrayMeta, cls).__call__(data, inputs, dtype)


@add_metaclass(ArrayMeta)
class Array(Funsor):
class Array(Funsor, metaclass=ArrayMeta):
"""
Funsor backed by a numpy ndarray.

Expand All @@ -88,7 +84,7 @@ class Array(Funsor):
def __init__(self, data, inputs=None, dtype="real"):
assert isinstance(data, np.ndarray) or np.isscalar(data)
assert isinstance(inputs, tuple)
assert all(isinstance(d.dtype, integer_types) for k, d in inputs)
assert all(isinstance(d.dtype, int) for k, d in inputs)
inputs = OrderedDict(inputs)
output = Domain(data.shape[len(inputs):], dtype)
fresh = frozenset(inputs.keys())
Expand Down Expand Up @@ -282,7 +278,7 @@ def materialize(x):
return x
subs = []
for name, domain in x.inputs.items():
if not isinstance(domain.dtype, integer_types):
if not isinstance(domain.dtype, int):
raise ValueError('materialize() requires integer free variables but found '
'"{}" of domain {}'.format(name, domain))
assert not domain.shape
Expand Down
6 changes: 1 addition & 5 deletions funsor/ops.py
@@ -1,11 +1,8 @@
from __future__ import absolute_import, division, print_function

import operator
from numbers import Number

import numpy as np
from multipledispatch import Dispatcher
from six import add_metaclass

_builtin_abs = abs
_builtin_max = max
Expand Down Expand Up @@ -84,8 +81,7 @@ def __call__(cls, offset):
return instance


@add_metaclass(GetitemMeta)
class GetitemOp(Op):
class GetitemOp(Op, metaclass=GetitemMeta):
"""
Op encoding an index into one dime, e.g. ``x[:,:,:,y]`` for offset of 3.
"""
Expand Down
6 changes: 2 additions & 4 deletions funsor/optimizer.py
@@ -1,9 +1,7 @@
from __future__ import absolute_import, division, print_function

import collections
from functools import reduce

from opt_einsum.paths import greedy
from six.moves import reduce

import funsor.ops as ops
from funsor.contract import Contract, contractor
Expand All @@ -21,7 +19,7 @@
class Finitary(Funsor):
"""
Lazy finitary operation. Used internally in the optimizer.
Finitary(op, operands) == six.moves.reduce(op, operands)
Finitary(op, operands) == functools.reduce(op, operands)
"""
def __init__(self, op, operands):
assert callable(op)
Expand Down
2 changes: 0 additions & 2 deletions funsor/pattern.py
@@ -1,5 +1,3 @@
from __future__ import absolute_import, division, print_function

import functools

import unification.match
Expand Down