google / tangent

Source-to-Source Debuggable Derivatives in Pure Python

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Automatically detect use of array methods (e.g. myArray.shape is illegal)

stefdoerr opened this issue · comments

I am trying to concatenate/combine two matrices
I tried np.stack np.hstack np.vstack np.concatenate yet none of these seems supported.

feats = np.concatenate((1 / r, coornorm), axis=1)
feats = np.hstack((1 / r, coornorm))
feats = np.stack((1 / r, coornorm), axis=1)

So in the end I tried assigning them to a preallocated array:

    feats = np.zeros((coornorm.shape[0], 4))
    feats[:, 0] = 1 / r
    feats[:, 1:] = coornorm

but it doesn't support extended slicing.
So I flipped the array around to be able to assign with a 0-dimension index

    feats = np.zeros((4, coornorm.shape[0]))
    feats[0] = 1 / r
    feats[1:] = coornorm

Here it fails at the last command with ValueError: Failed to process assignment to: ['coornorm_shape']. Error: Unknown node type: Attribute

Currently I'm out of ideas on how to combine two matrices :) Any suggestions?

Oh wait, I got it. I needed to use np.shape(coornorm)[0]. That worked. I guess thus this becomes a duplicate of my other issue :) I need to change my habits a bit of using array methods, sorry.

Changed the issue names around a little bit, because they exposed two features that we should implement.

I think the easiest way to detect this is using active variable analysis. If a variable shows up as active during dataflow analysis, it should not be referenced in the func field of a Call node or the value field of an Attribute node..