提交 0de42ea0 authored 作者: Pascal Lamblin's avatar Pascal Lamblin

More PEP8 / Pyflakes

上级 2b1796cc
import os, sys
import os
import sys
from theano.compat import PY3
from theano.gof.compilelock import get_lock, release_lock
......@@ -13,11 +14,13 @@ if os.path.exists(os.path.join(config.compiledir, 'cutils_ext.so')):
def compile_cutils():
"""Do just the compilation of cutils_ext"""
types = ['npy_' + t for t in ['int8', 'int16', 'int32', 'int64', 'int128',
'int256', 'uint8', 'uint16', 'uint32', 'uint64', 'uint128', 'uint256',
'float16', 'float32', 'float64', 'float80', 'float96', 'float128',
'float256']]
types = ['npy_'+t for t in ['int8', 'int16', 'int32', 'int64', 'int128', 'int256', 'uint8', 'uint16', 'uint32', 'uint64', 'uint128', 'uint256', 'float16', 'float32', 'float64', 'float80', 'float96', 'float128', 'float256'] ]
complex_types = ['npy_'+t for t in ['complex32', 'complex64', 'complex128', 'complex160', 'complex192', 'complex512'] ]
complex_types = ['npy_' + t for t in ['complex32', 'complex64',
'complex128', 'complex160', 'complex192', 'complex512']]
inplace_map_template = """
#if defined(%(typen)s)
......@@ -40,16 +43,20 @@ def compile_cutils():
((%(type)s*)mit->dataptr)[0].imag = ((%(type)s*)mit->dataptr)[0].imag + ((%(type)s*)it->dataptr)[0].imag;
"""
fns = ''.join([inplace_map_template % {'type' : t, 'typen' : t.upper(), 'op' : floatadd % {'type' : t} } for t in types] +
[inplace_map_template % {'type' : t, 'typen' : t.upper(), 'op' : complexadd % {'type' : t} } for t in complex_types])
fns = ''.join([inplace_map_template % {'type': t, 'typen': t.upper(),
'op': floatadd % {'type': t}}
for t in types] +
[inplace_map_template % {'type': t, 'typen': t.upper(),
'op': complexadd % {'type': t}}
for t in complex_types])
fn_array = ("inplace_map_binop addition_funcs[] = {" +
''.join(["""
#if defined(%(typen)s)
%(type)s_inplace_add,
#endif
""" % {'type' : t, 'typen' : t.upper()} for t in types+complex_types]) +
""" % {'type': t, 'typen': t.upper()}
for t in types + complex_types]) +
"""NULL};
""")
......@@ -58,11 +65,10 @@ def compile_cutils():
#if defined(%(typen)s)
%(typen)s,
#endif
""" % {'type' : t, 'typen' : t.upper()} for t in types+complex_types]) +
""" % {'type': t, 'typen': t.upper()}
for t in types + complex_types]) +
"-1000};")
code = ("""
#include <Python.h>
#include "numpy/arrayobject.h"
......@@ -121,7 +127,6 @@ map_increment(PyArrayMapIterObject *mit, PyObject *op, inplace_map_binop add_inp
PyArray_BroadcastToShape((PyObject*)arr, mit->dimensions, mit->nd);
if (it == NULL) {
Py_DECREF(arr);
return -1;
}
......@@ -210,10 +215,9 @@ fail:
{NULL, NULL, 0, NULL} /* Sentinel */
};""")
if PY3:
# This is not the most efficient code, but it is written this way to highlight
# the changes needed to make 2.x code compile under python 3.
# This is not the most efficient code, but it is written this way to
# highlight the changes needed to make 2.x code compile under python 3.
code = code.replace("<Python.h>", '"numpy/npy_3kcompat.h"', 1)
code = code.replace("PyCObject", "NpyCapsule")
code += """
......@@ -242,7 +246,6 @@ fail:
} //extern C
"""
import cmodule
loc = os.path.join(config.compiledir, 'cutils_ext')
if not os.path.exists(loc):
......
......@@ -23,7 +23,8 @@ import numpy
import theano
from theano.compat import PY3
from theano import gof
from theano.gof import Op, utils, Variable, Constant, Type, Apply, FunctionGraph
from theano.gof import (Op, utils, Variable, Constant, Type, Apply,
FunctionGraph)
from theano.gof.python25 import partial, all, any
from theano.configparser import config
......@@ -2680,7 +2681,7 @@ class Composite(ScalarOp):
except AttributeError:
if 0:
l = []
for n in fgraph.toposort():
for n in self.fgraph.toposort():
if hasattr(n.op, "name") and n.op.name is not None:
v = n.op.name
if v.startswith("Composite"):
......
......@@ -24,7 +24,7 @@ from theano import compile, printing
from theano.printing import pprint, min_informative_str
from theano.tensor.utils import hash_from_ndarray
import theano.gof.cutils #needed to import cutils_ext
import theano.gof.cutils # needed to import cutils_ext
try:
from cutils_ext.cutils_ext import inplace_increment
except ImportError:
......@@ -4439,7 +4439,8 @@ class Subtensor(Op):
slice_c = None
return slice(slice_a, slice_b, slice_c)
# There is a bug in numpy that results in isinstance(x, int) returning False for numpy integers.
# There is a bug in numpy that results in isinstance(x, int) returning
# False for numpy integers.
# See <http://projects.scipy.org/numpy/ticket/2235>.
elif isinstance(entry, (numpy.integer, int)):
return entry
......@@ -7198,19 +7199,21 @@ def as_index_variable(idx):
raise TypeError('index must be integers')
return idx
def as_int_none_variable(x):
if x is None:
return NoneConst
x = as_tensor_variable(x, ndim = 0)
x = as_tensor_variable(x, ndim=0)
if x.type.dtype[:3] not in ('int', 'uin'):
raise TypeError('index must be integers')
return x
class MakeSlice(Op):
def make_node(self, slc):
return Apply(self,
map(as_int_none_variable,[slc.start, slc.stop, slc.step]),
map(as_int_none_variable,
[slc.start, slc.stop, slc.step]),
[slicetype()])
def perform(self, node, inp, out_):
......@@ -7227,7 +7230,7 @@ class MakeSlice(Op):
return hash(type(self))
def grad(self, inputs, grads):
return [DiconnectedType()() for i in inputs]
return [DisconnectedType()() for i in inputs]
make_slice = MakeSlice()
......@@ -7244,7 +7247,6 @@ class SliceType(gof.Type):
return "slice"
class NoneTypeT(gof.Type):
def filter(self, x, strict=False, allow_downcast=None):
......@@ -7257,13 +7259,16 @@ class NoneTypeT(gof.Type):
return "None"
slicetype = SliceType()
NoneConst = Constant(NoneTypeT(), None, name = 'None')
NoneConst = Constant(NoneTypeT(), None, name='None')
def adv_index_broadcastable_pattern(a, idx):
"""
This function is only used to determine the broardcast pattern for AdvancedSubtensor output variable.
This function is only used to determine the broadcast pattern for
AdvancedSubtensor output variable.
For this, we make a fake ndarray and a fake idx and call use ask numpy the output. From this, we find the output broadcast pattern.
For this, we make a fake ndarray and a fake idx and call use ask numpy
the output. From this, we find the output broadcast pattern.
"""
def replace_slice(v):
......@@ -7278,9 +7283,9 @@ def adv_index_broadcastable_pattern(a, idx):
if NoneConst.equals(v):
return None
if isinstance(v.type, SliceType):
return slice(None,None)
return slice(None, None)
return numpy.zeros( (2,)* v.ndim, int)
return numpy.zeros((2,) * v.ndim, int)
newidx = tuple(map(replace_slice, idx))
......@@ -7289,6 +7294,7 @@ def adv_index_broadcastable_pattern(a, idx):
retshape = numpy.empty(fakeshape)[newidx].shape
return tuple([dim == 1 for dim in retshape])
class AdvancedSubtensor(Op):
"""Return a subtensor copy, using advanced indexing.
"""
......@@ -7309,13 +7315,11 @@ class AdvancedSubtensor(Op):
x = as_tensor_variable(x)
index = tuple(map(as_index_variable, index))
bcast = adv_index_broadcastable_pattern(x, index)
return gof.Apply(self,
(x,) + index,
[tensor(dtype = x.type.dtype,
broadcastable = adv_index_broadcastable_pattern(x, index) )])
[tensor(dtype=x.type.dtype,
broadcastable=bcast)])
def R_op(self, inputs, eval_points):
if eval_points[0] is None:
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论