Package: arraydebug
ArrayDebugArrayDebug generates human-friendly debug information for array-like
objects.Free software: MIT licenseSupport python 3.6+Installation$pipinstallarraydebugGet StartedAll you need is toimport arraydebugafternumpy, torch, pandas, etc.importnumpyasnpimporttorchimportpandasaspd...importarraydebug# import at lastThen you will get information of the array-like object shown in the debugger, like:<Tensor: shape=(6, 4), dtype=float32, device='cpu', requires_grad=True>It works with all debuggers which rely onreprfunction to display variable information.VSCodeIPythonpdbetc.VSCodeIPythonIn[1]:importtorchIn[2]:importarraydebugIn[3]:torch.ones(6,4,requires_grad=True)Out[3]:<Tensor:shape=(6,4),dtype=float32,device='cpu',requires_grad=True>tensor([[1.,1.,1.,1.],[1.,1.,1.,1.],[1.,1.,1.,1.],[1.,1.,1.,1.],[1.,1.,1.,1.],[1.,1.,1.,1.]],requires_grad=True)pdb$python-mpdbexamples/example.py
...
...
...(Pdb)tensor
<Tensor:shape=(6,4),dtype=float32,device='cpu',requires_grad=True>
tensor([[0.0833,0.2675,0.4372,0.5344],[0.9977,0.6844,0.1404,0.2646],[0.7211,0.7529,0.1239,0.2511],[0.1717,0.6611,0.6598,0.9705],[0.5230,0.3439,0.0459,0.9937],[0.8603,0.6598,0.0652,0.1235]],requires_grad=True)UsageImportarraydebugafternumpy, torch, pandas, etc.importtorchimportarraydebug# import at lastThen you will get information of the array-like object shown in the debugger, like:<Tensor: shape=(6, 4), dtype=float32, device='cpu', requires_grad=True>ArrayDebug searches imported packages for array_like objects, and provides debug information for them. So, it important to importarraydebugafter them.How does it work?Behind the hood, this is achieved by modifying behavior ofrepr(). So, all debuggers that relies onrepr()will display the debug information.>>>importtorch>>>importarraydebug>>>tensor=torch.ones(3,requires_grad=True)>>>print(repr(tensor))<Tensor:shape=(3,),dtype=float32,device='cpu',requires_grad=True>tensor([1.,1.,1.],requires_grad=True)Enable and disableTo recover the vanillarepr(), you may disable ArrayDebug bydisable(),>>>arraydebug.disable()>>>print(repr(tensor))tensor([1.,1.,1.],requires_grad=True)or enable ArrayDebug again byenable().>>>arraydebug.enable()>>>print(repr(tensor))<Tensor:shape=(3,),dtype=float32,device='cpu',requires_grad=True>tensor([1.,1.,1.],requires_grad=True)This is also useful when you import some modules after ArrayDebug, and want to enable ArrayDebug for them:>>>importarraydebug>>>importtorch>>>print(repr(torch.ones(3,requires_grad=True)))<Tensor:shape=(3,),dtype=float32,device='cpu',requires_grad=True>tensor([1.,1.,1.],requires_grad=True)CustomizeYou can register your own debug info byregister_repr().>>>classA:...def__init__(self,x):...self.x=x...>>>info_fn=lambdaa:f'<class A object with x={a.x}>'>>>arraydebug.register_repr(A,info_fn)>>>print(repr(A(5)))<classAobjectwithx=5>CreditsThis package was created withCookiecutterand theaudreyr/cookiecutter-pypackageproject template.History0.1.2 (2022-03-29)Allowsarray_like.__array__()to throw exceptions.Now,array_like_info(obj)degrades to vanillarepr(obj)ifobj.__array__()throws exception. Before, it did nothing, but let caller to handle the exception.renamearraydebug.recover_repr()toarraydebug.disable().renamearraydebug.inject_repr()toarraydebug.enable().enable()also refresharraydebug.repr_fn_tablenow.0.1.0 (2022-03-27)First release on PyPI.Support numpy.ndarray, torch.Tensor.Support objects with__array__method.Setup github CI workflow.