google / tangent

Source-to-Source Debuggable Derivatives in Pure Python

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ExtSlice Support

stefdoerr opened this issue · comments

I am playing around a bit with tangent after your helpful reply in reddit ;)

So I was trying to reshape an array. First I had:

  File "<stdin>", line 2
    r2 = np.sum(wrapped ** 2, axis=1)[:, np.newaxis]
                                   ^
TangentParseError: Extended Slices are not supported

Then I tried with reshape
r2 = np.sum(wrapped ** 2, axis=1).reshape(-1, 1)
and it gave: AttributeError: 'NoneType' object has no attribute 'reshape'

Then I tried

r2 = np.sum(wrapped ** 2, axis=1)
r2 = r2.reshape(-1, 1)

which gave AttributeError: module 'builtins' has no attribute 'r2'

and there I finally got the hint and did:

r2 = np.sum(wrapped ** 2, axis=1)
r2 = np.reshape(r2, (-1, 1))

Which worked. I guess it would be nice for new users to add a line to the docs not to use the array methods and instead use directly numpy functions.

Good point. We don't currently have the machinery to detect that r2 is an object with methods. I'll add a line to the docs soon making this clear.

We should support ExtSlice, though, that should have worked off the bat.

Requires supporting it in our ANF transform, probably minimal change:

tangent/tangent/anf.py

Lines 109 to 123 in 01f861a

if isinstance(node.slice, gast.Index):
node.slice.value = self.trivialize(node.slice.value)
elif isinstance(node.slice, gast.Slice):
name = self.namer.name(node.slice)
target = gast.Name(id=name, ctx=gast.Store(), annotation=None)
stmt = gast.Assign(targets=[target], value=None)
self.prepend(stmt)
stmt.value = gast.Call(
func=gast.Name(id='slice', ctx=gast.Load(), annotation=None),
args=[
self.trivialize(arg) if arg else
gast.Name(id='None', ctx=gast.Load(), annotation=None)
for arg in [node.slice.lower, node.slice.upper,
node.slice.step]],
keywords=[])

Should be fixed in 0ef0225.