numpy package not defined when importing function from another .py file - python

In my master file I have:
import matplotlib.pyplot as plt
import seaborn
import numpy as np
import time
import sys
sys.path.append("C:/.../python check/createsplit")
import createsplit
data='MJexample'
X,Y,N,Ntr=create_training_data(data)
where I am calling create_training_data function from createsplit.py file which is:
import numpy as np
import scipy.io
def create_training_data(data_type):
"""
creates training data
"""
if data_type=='MJexample':
N=300
Ntr = 150
X=np.linspace(0,1,N)
X = np.array([X,X*X,np.linspace(5,10,N),np.sin(X),np.cos(X),np.sin(X)*np.cos(X)]).T
fac=40
Y=np.array([np.sin(fac*x)*np.cos(fac*x**2) for x in X[:,0]])[:,None]
_X=X
_Y=Y
return _X,_Y,N,Ntr
However running my original file results in error: NameError: global name 'np' is not defined for some reason I do not understand. I assume I am importing the functions in a wrong way but I don't really understand what would be wrong.

I think this issue raises just because of a wrong call of the function. Try
X, Y, N, Ntr = createsplit.create_training_data(data)
instead, and it should work.

Related

Edit file traffic.conf with NM directory

can someone help me?
This is my code:
import traffic
from traffic.core import Traffic
import pickle
import pandas as pd
import numpy as np
import os
from numpy import sqrt, argmax
from matplotlib import pyplot
import matplotlib.pyplot as plt
from traffic.data import SO6
import math
from geographiclib.geodesic import Geodesic
from traffic.data import nm_airspaces
#BBDD INITIAL
geod = Geodesic.WGS84
bbdd_initial=SO6.from_file('initial.so6')
bdx_so6 = bbdd_initial.inside_bbox(nm_airspaces['LSAZM456'])*
And, when I run it, it gives me an error and I don't know how to fix it:
*RuntimeError Traceback (most recent call last)
<ipython-input-66-f276e3cdb85c> in <module>
bbdd_inicial=SO6.from_file('initial.so6')
bdx_so6 = bbdd_inicial.inside_bbox(nm_airspaces['LSAZM456'])
~\anaconda3\envs\AISAenv\lib\site-packages\traffic\data\eurocontrol\ddr\airspaces.py in
__getitem__(self, name)
if not self.initialized:
self.init_cache()
list_names = self.airspaces.get(name, None)
~\anaconda3\envs\AISAenv\lib\site-packages\traffic\data\eurocontrol\ddr\airspaces.py in
init_cache(self)
if self.nm_path is None:
raise RuntimeError(msg)
are_file = next(self.nm_path.glob("Sectors_*.are"), None)
RuntimeError: Edit file C:\Users\Usuario\AppData\Local\traffic\traffic\traffic.conf with NM
directory
I am trying to obtain the trajectory which is within a sector. If that doesn´t have solution, is there any way to get that?

error : 'module' object is not callable when using logmmse

I am trying to reduce noise in my audio_file and want to have an output file which doesn't contain noise, and I use the logmmse library:
I use this code:
import wavio
import numpy as np
from logmmse import logmmse_from_file
import logmmse
r = wavio.read('03-01-02-02-01-01-01(read).wav')
y,sr = librosa.load('03-01-02-02-01-01-01(read).wav')
#print(y)
import numpy as np
A = np.asarray(y)
but I have this error:
TypeError: 'module' object is not callable!
can you help me please?
#print(A)
logmmse(A, r.rate, output_file = 'log.wav')
As the error states, you are trying to call the module itself. I suppose what you're trying to do is use the logmmse function inside the logmmse module, so you should do:
logmmse.logmmse(A, r.rate, output_file = 'log.wav')

Python call a imported function from a different .py file, but the imported function cannot see the other imported moduel in main file

I have a function defined in a different file nhpp_next_arrival.py, which contains a function. The important thing to notice is that I am using numpy package.
def nhpp_next_arrival(t,Lambda, h_lam):
# current starting time t
# rate handle h_lam
# Lambda a majorizing function/constant
U = np.random.uniform()
V = np.random.uniform()
t = t - np.log(U)/Lambda
while V > h_lam(t)/Lambda:
t = t - np.log(U)/Lambda
U = np.random.uniform()
V = np.random.uniform()
return t
I imported this function in a different file as the following
import numpy as np
from nhpp import *
#Call nhpp_next_arrival
t_arrival = nhpp_next_arrival(t=t, Lambda=max_arrival, h_lam=h_arr_total)
Then I got the following error message.
U = np.random.uniform() NameError: name 'np' is not defined
Thank you!
You might be confused with a C #include (or something similar).
Your code is using numpy.random at nhpp_next_arrival.py, so you should have at its top
import numpy as np
Even though you imported it before the import to this file, when the interpreter sees
from nhpp import *
it doesn't import the packages into the namespace of that module.

Importing a Function in Python: ImportError "moduleX has no attribute Y"

I'm attempting to import a function defined in another file into the one I am working in.
The function I'm trying to import is in a file called ParallelEqns.py and looks like:
import sys
import numpy as np
import scipy as sp
import sympy as sym
import matplotlib.pyplot as plt
import os
def ParDeriv(x,p):
derivative = []
for k in range(nS):
test = x[(k-1)%nS]*(x[(k+1)%nS] - x[(k-2)%nS]) - x[(k)%nS] + p
if k == 0:
derivative = test
else:
derivative = np.vstack([derivative, test])
return derivative
The file I'm working in looks like:
import sys
import numpy as np
import scipy as sp
import sympy as sym
import matplotlib.pyplot as plt
import os
from ParallelEqns import ParDeriv
That gives me an error of "cannot import name 'ParDeriv'"
If I change the file to:
import sys
import numpy as np
import scipy as sp
import sympy as sym
import matplotlib.pyplot as plt
import os
import ParallelEqns
ParDeriv = ParallelEqns.ParDeriv
I get an error that says "module 'ParallelEqns' has no attribute 'ParDeriv'"
I've checked that both files are in the same directory. I'm not sure what I'm doing wrong here
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Edit: I've answered my own question by closing everything down and restarting python. It looks like I needed to restart python after creating the ParallelEqns.py file for it to correctly import
It turns out I just needed to restart python as I had created the file that I was trying to import after starting up python. Once I did that it worked out

Error in importing a python file

I've written a python file and am trying to import it but it's not recognized.
The file was saved as gentleboost_c_class.c in C:\User\apps\My documents.
I tried to import it like this:
import gentleboost_c_class as gbc
But I get this error:
NameError: name 'gentleboost_c_class' is not defined
gentleboost_c_class.py begins like this:
from sklearn.externals.six.moves import zip
import numpy as np
import statsmodels.api as sm
class GentleBoostC:
.....
It compiles fine.
Both files are in the same folder.
What am I doing wrong?
You're getting a NameError, not an ImportError.
So it seems to me that you import your module as gbc, but later try to refer to it as gentleboost_c_class.
If you import the module with
import gentleboost_c_class as gbc
that means it will be available under the global name gbc, but not as gentleboost_c_class.

Categories

Resources