How do I tell which module a function comes from? - python

I'm trying to read the help on what various things do as I'm reading through code. I'm getting a bit lost in how to determine which module a function comes from. Here is my current example:
import quandl
import numpy as np
import matplotlib.pyplot as plt
amzn = quandl.get("WIKI/AMZN", start_date="2018-01-01", end_date="2019-01-01")
amzn_daily_close = amzn[['Adj. Close']]
amzn_daily_log_returns = np.log(amzn_daily_close.pct_change()+1)
monthly = amzn.resample('BM').apply(lambda x: x[-1])
So given this block of code, I can do help (quandl.get) to see information about that and help (np.log) to see what that does. But when I get to amzn.resample, where is that resample coming from? What should I be entering to see some help information on the resample stuff?

Look at the docstring of quandl.get method to get the help message about the return object. This will contain a statement as returns x-object. Googling about x-object will give you more info on this.
Alternatively, you can do this. To identify what is the object you can do the below.
amzn_type = type(amzn)
This gives the monthly object type. Googling for this type value will give you more insights about that object.Example -
a = 10
print(type(a))
The above code returns <class 'int'> output. Googling about int class in python3 will be helpful.

Inspection
You can 'inspect' the method to find the implementation:
import inspect
print(inspect.getfile(amzn.resample))
# /opt/miniconda/envs/stackoverflow/lib/python3.6/site-packages/pandas/core/generic.py
IDE
Or you can use a good IDE (e.g. PyCharm or IntelliJ) which supports you with some neat features:

Generally, these modules should be documented somewhere. They are usually "packaged" and made available on Python Package Index (pypi). You can search there for your package name and find the quandl page. That may have a link to the projects home page with more documentation.

Related

Imorting zero_gradients from torch.autograd.gradcheck

I want to replicate the code here, and I get the following error while running in Google Colab?
ImportError: cannot import name 'zero_gradients' from
'torch.autograd.gradcheck'
(/usr/local/lib/python3.7/dist-packages/torch/autograd/gradcheck.py)
Can someone help me with how to solve this?
This seems like it's using a very old version of PyTorch, the function itself is not available anymore. However, if you look at this commit, you will see the implementation of zero_gradients. What it does is simply zero out the gradient of the input:
def zero_gradients(i):
for t in iter_gradients(i):
t.zero_()
Then zero_gradients(x) should be the same as x.zero_grad(), which is the current API, assuming x is a nn.Module!
Or it's just:
if x.grad is not None:
x.grad.zero_()

How to define and use a python function across chunks in R Markdown?

Though I feel very stupid asking this question, I have not found a way to resolve this.
I am trying to define a simple python function in a code chunk in R Markdown.
def show(T):
print("shape of T:", T.shape)
print(T)
Then, I am trying to use it from another pyhton chunk in the same R Markdown.
import numpy as np
T = np.array([[...],[...], ...])
show(T)
But this throws an error saying that
NameError: name 'show' is not defined
I am using RStudio 1.2.907. Is there a way to resolve this?

Using functions from modules in pandas dataframe

I have following dataframe:
Open High Low Close
Date
2000-02-01 6841.12 7052.22 6841.12 7050.46
2000-02-02 7063.57 7172.05 7038.71 7171.95
2000-02-03 7175.87 7354.56 7134.42 7354.26
I want to use functions from a different module. Say I call the module TA.py. In this module I enter following function:
def pan1(val):
return val > val.shift(1)
Now, I would like to apply this function for creating a new column in the dataframe:
import TA as TA
df['signal'] = np.where(TA.pan1(df.High),'Yes', 'No')
I receive the error message that module 'TA' has no attribute 'pan1'.
How can I reconstruct things that this is working (by keeping a module soultion)? If I use the same function in the same script (without the module) it is working.
EDIT: as explained in the comments, there are other functions in TA.py that I can import without problem. But this function pan1 seems to be wrong or not usable in the current set up. So, any hint what I should do different is welcome.
I post this as a possible solution although it should be more of a comment. I had the same issue with exactly the same error messages. The problem was banal: I forgot to save the module (after I added the "problematic" function). So I saved the module, I imported it again and everything worked.

How to use img2pdf as module

I'm building a tiny Python 2.7.9 script that takes a list of JPEG images as an input and outputs a PDF file. I'm googling for hours but can't find a solution:
I tried pypdf, but it says I don't have PIL installed although i have.
I tried Reportlab, but the page size is bigger than the image itself and I couldn't find a way to fix it. I also find it hard...
I tried img2pdf, but couldn't understand how to use it
I'm really tired of all these libraries, and I'm looking for a good solution.
Good means:
As pythonic as possible
fit page size to image size
Support big amount of images (maybe even 500)
Easy enough
If you can help with with the modules I already tried/code examples it would be awesome.
Also, if you have experience with a module I would be glad if you could share it.
Edit:
As suggested in the comments, I decided to give another chance to img2pdf. I'm using the following code from the official GitHub README:
import img2pdf
pdf_bytes = img2pdf('test.jpg', dpi=150)
But it throws an exception:
`TypeError: 'module' object is not callable
Do someone knows how to use img2pdf as a module to several images?`
Nothing in the GitHub code suggests to me that this module is callable. It's possible that's a mistake in the readme; they have meant for the example to use the convert function. Try this instead:
import img2pdf
pdf_bytes = img2pdf.convert(('test.jpg',), dpi=150, x=None, y=None)
In the source code, this function requires positional arguments x and y:
def convert(images, dpi, x, y, title=None, author=None, creator=None, producer=None,
creationdate=None, moddate=None, subject=None, keywords=None,
colorspace=None, verbose=False):
But as long as you provide the dpi argument, it's set up with a default case that makes it look (to me, at least) like both x and y were intended to be optional arguments:
if not x and not y:
pdf_x, pdf_y = 72.0*width/ndpi[0], 72.0*height/ndpi[1]
The package you got on PIP isn't necessarily identical to the code on GitHub, though. You could try it without supplying x and y as arguments to convert as well—or even better, find the source file on your own machine and check it out yourself.

Accessing Math functions from rootpy

I'm currently trying to access Math functions such as DeltaR from rootpy, but I'm not sure how this is done. I've seen nothing in the documentation or in any examples. The C++ equivalent would be something like:
double dR = ROOT::Math::VectorUtil::DeltaR((jets)[i],(partons)[i]);
But I'm unable to find a rootpy or even pyroot equivalent that'll work. If I try in pyroot with
import ROOT as r
r.Math.VectorUtil.DeltaR(jets[i],partons[i])
I get the error:
AttributeError: type object 'ROOT::Math' has no attribute 'VectorUtil'
When it quite clearly should, unless I don't understand correctly what it means by 'Attribute'. Anyway, I don't want to ask pyroot questions here :) I just put this down to a quirk in the way that pyroot handles such things, which is why I thought I'd give rootpy a try. I'm not sure if this is possible however.
Cheers,
Joseph
The functions from ROOT::Math::VectorUtil are in libGenVector which is loaded automatically in neither CINT nor PyROOT. Manually loading it (like you probably do in your root_logon.C) makes the functions available, e.g.
import ROOT as r
r.gSystem.Load('libGenVector')
# ...
r.Math.VectorUtil.DeltaR(jets[i],partons[i])
If jets and partons are TLorentzVectors then you should be able to do:
from ROOT import *
dR = jet.DeltaR(parton)

Categories