Hard time finding Python-Numpy deg2rad function - python

Title says it all, I somehow can not find that function. Obviously it's inside the Numpy package (numpy.core.umath.deg2rad) and I've tried importing it but to no avail. Anyone care to chime in?
import numpy as np - np.deg2rad doesn't even show up
from numpy import* - umath.deg2rad shows up, but it raises an error, ''name 'umath' is not defined''

from numpy.core.umath import deg2rad
# then
deg2rad(...)
Or
import numpy as np
np.core.umath.deg2rad(...)

Related

Why couldn't I import np.typing.NDArray, but now I can?

I'm running into a weird situation with Python imports.
Does someone know how this works?
I have:
import numpy as np
values: Union[Sequence[int], np.typing.NDArray]
probs: Union[Sequence[float], np.typing.NDArray]
​Now that fails because np.typing can't be imported this way. I guess since that is not defined in the init file?
Ok, so now I replace this with:
import numpy as np
import numpy.typing as npt
values: Union[Sequence[int], npt.NDArray]
probs: Union[Sequence[float], np.typing.NDArray]
​and now it works - but why doesn't it break on the 'probs' line? There, I still have the same statement that was giving me an error before. What changed to make this work?
Context: Numpy 1.21.3, Python 3.7
Note: I know I can simply replace both statements, but I was surprised by why this doesn't give an error and wanted to know how this worked.
After you run import
import numpy.typing as npt
interpreter "find out" addition parts of np.
You can check this with:
import numpy as np
len(dir(np)) # 602 (in my case)
import numpy.typing as npt
len(dir(np)) # 604

Calling librosa.grifflim returns an attribute error

In the following code I'm getting errors when trying to call librosa.grifflim, telling me the attribute does not exist.
import os
from matplotlib import pyplot as plt
import librosa
import librosa.display
import IPython.display as ipd
import numpy as np
import cv2
S = cv2.imread('spectrograms/CantinaBand60.wav10.jpg')
D = librosa.amplitude_to_db(np.abs(S), ref=np.max)
signal = librosa.griffinlim(D)
sf.write('test.wav', signal, 352000)
I've upgraded librosa, and I still encounter the error. The documentation page for this function no longer seems to exist either. I've also tried import just that module using librosa.griffinlim but it continues to tell me this module doesn't exist. Was this function removed during a recent version? If so, is there another function I can use to apply the griffin lim algorithm?
librosa.griffinlim was introduced in librosa 0.7.0. So you need to have that version or later. You can check this using the following code.
import librosa; print(librosa.__version__)

Dill installed - throwing error that part of the module is missing

I'm writing code in a Jupyter Notebook that involves cleaning and analyzing a large amount of consumer data. I'm trying to use dill to save the dataframes with thousands of rows so I don't have to run the code every time I want to make an adjustment, so dill seems like the perfect package to do so... Except I'm getting this error when attempting to pickle the notebook:
AttributeError: module 'dill' has no attribute 'dump_session'
Let me know if the program code is necessary - I don't think it should make a difference. The imports are:
import numpy as np
import pandas as pd
import dill
import scipy
from matplotlib import pyplot as plt
from __future__ import division
from collections import OrderedDict
from sklearn.cluster import KMeans
pd.options.display.max_columns = None
and when I run this code I get the error from above:
dill.dump_session('recengine.db')
Is there another package that's interfering with dill's use of pickle vs. cpickle?

Python import statement with argument [duplicate]

This question already has answers here:
Python import functions from module twice with different internal imports
(3 answers)
Closed 4 years ago.
I am using numpy in one of my libraries. No surprise there.
One user would essentially like a copy of my project where I don't use the default numpy, but the one bundled with autograd. For instance, let's say I have a dumb function:
import numpy
def doSomething(x):
return numpy.sin(x)
They would like a copy of the library where all of these import numpy are replaced by from autograd import numpy:
from autograd import numpy
def doSomething(x):
return numpy.sin(x)
This would allow them to easily compute gradients and jacobians of my functions.
I would like to know what the easiest way to handle this is without copying the whole codebase and replacing all of these lines.
Options I am aware of:
I could make a copy of the codebase (lib and lib_autograd) where the first uses import numpy, and the second uses from autograd import numpy. This is bad because then I have to maintain two codebases.
I could automatically import from autograd if it is available:
try:
from autograd import numpy
except ImportError:
import numpy
The reason I do not want to do this is that many people have highly optimized numpy installs, whereas autograd might not. So I want to give the user an option which version to import. Forcing the user to use the autograd version if they have it seems bad since it would not be apparent to the user what is going on, and would require the user to uninstall autograd if they want to use the library with their default numpy installation.
So what are my options?
Ideally there would be a way of doing something like passing a parameter to the import statement (I do realize that you can't do this):
useAutograd = False
from lib(useAutograd) import doSomething
You can have 'conditional' import with:
try:
from autograd import numpy
except ImportError:
import numpy
One of other options is to have environment variable that switches whether you want to use numpy from autograd or regular one, because here you either use autograd.numpy (if it exists) or numpy. You don't have an option to use numpy if there is autograd module/package.
To elaborate on giving user an option to switch, here is one possibility:
import os
if os.environ.get('AUTOGRADNUMPY'):
try:
from autograd import numpy
except ImportError:
import numpy
else:
import numpy
Having environment variable AUTOGRADNUMPY set to True (or anything else that is not empty string) when you want to load numpy from autograd package. If it is not set or doesn't exist, regular numpy is imported.
All of this stands if user has at least numpy installed.
This might help:
try:
from autograd import numpy as np
except ImportError:
import numpy as np
...
...
np.sum(..)

pylab.imshow() not working

I've seen many times online that people use
import pylab
pylab.imshow(some_arg)
or
from pylab import *
imshow(some_arg)
but it gives the following error on my computer
NameError: name 'imshow' is not defined
however if I directly use matplotlib, it works fine
import matplotlib.pyplot as plt
plt.imshow(some_arg) # works fine
I've tried search about how pylab works, but there doesn't seem to be much information.
What should be the problem in my case?
Thank you.

Categories