Struggling to import modules from scipy - python

I'm having some trouble using the mannwhitneyu test provided by scipy. I think my problem is with importing. Here is my script:
SNPs=[]
Nonsense=[]
with open("final_positions_SNP_removed_duplicates_truepos_truetrans_normalized.txt") as fd:
fd.readline()
for line in fd:
SNPs.append(line.split()[8])
with open("final_positions_nonsense_truepos_truetrans_normalized.txt") as fd:
fd.readline()
for line in fd:
Nonsense.append(line.split()[8])
from scipy import stats
from scipy.stats import mannwhitneyu
u_value, p_value = scipy.stats.mannwhitneyu(SNPs, Nonsense)
I keep getting an error that states: NameError: name 'scipy' is not defined I think I need to import scipy, but how?

from scipy.stats import mannwhitneyu
u_value, p_value = scipy.stats.mannwhitneyu(SNPs, Nonsense)
You have imported mannwhitneyu, so you should use
u_value, p_value = mannwhitneyu(SNPs, Nonsense)
The reason you are getting NameError: name 'scipy' is not defined is that you did not import scipy, you only imported modules from scipy. to import scipy itself you would have to do:
import scipy

Related

Jupyter imported module not using library

I made an external module model.py and I want to import it within my Jupyter notebook.
# model.py
import numpy as np
import pandas as pd
from scipy.stats import chi2
from sklearn.covariance import EllipticEnvelope
from sklearn.base import BaseEstimator
from scipy.stats import combine_pvalues
# ...
def predict(self, xtest):
return np.where(self.predict_proba(xtest, False) < self.critical_value, 1., -1.)
When I try to call the predict method of my model class I get the error:
NameError: name 'np' is not defined.
I have the numpy library installed and I am strugggling to understand why it is not able to use it.
Any ideas?

package import is not importing its dependencies

I just wrote a small package containing a class to use on jupyter labs at work, however when I try to import and use the class which is dependent on numpy and matplotlib, it says that np is not defined.
Even though it is imported into the file I am importing the class into, and I imported it into the class file itself that I am importing, and the __init__.py file for the package. I just need the class to load some data into a np.array([]) via np.genfromtxt().
I have made one package before and didn't have any issues but I'm new so am obviously missing something.
Here's how I get the error:
pip install git+https://github.com/Elliot-Wadge/pl_class#egg=pl_class
import numpy as np
import matplotlib.pyplot as plt
import glob
from pl_data import pl_data as pl
PL_data = pl.PL_data
flist = glob.glob("data/*")
dictionary = PL_data(flist, skip_header = 4, unpack = True, delimiter = '\t')
error message
class file

Print version of a module without importing the entire package

Is it possible to check the version of a package if only a module is imported?
When a package is imported like...
import pandas as pd
I use:
print('pandas : version {}'.format(pd.__version__))
to print the version number.
How do I check the version number if only a module is imported, like
import matplotlib.pyplot as plt
or
from sklearn.metrics import confusion_matrix
Any suggestions?
I usually do this:
import matplotlib.pyplot as plt
import sys
print (sys.modules[plt.__package__].__version__)
if you import just a function:
from sklearn.metrics import confusion_matrix as function
import sys
try:module_name = function.__module__[:function.__module__.index(".")]
except:module_name = function.__module__
print (sys.modules[module_name].__version__)
and if this doesn't work you could just import pip and for loop all the modules.

Easy Install Astropysics gives Errors when transforming Coordinates, coordsys

I have some code that I wrote and imported to a new computer, I tried to do easy_install on astropysics and networkx but it seems that my previous (working) code has trouble with the easy_install version of astropysics. I've not seen this one before so I thought maybe someone here would have an idea. My relevant code is:
import os, sys
import numpy as np
import matplotlib.pyplot as plt
import asciitable
from scipy import stats
import astropysics
import astropysics.obstools
import astropysics.coords
import math
import pylab as P
import random
from random import randint
from pylab import *
from astropysics.coords import ICRSCoordinates,GalacticCoordinates
from scipy.optimize import curve_fit
f=open(sys.argv[1])
y= asciitable.read(f,Reader=asciitable.CommentedHeader,delimiter=' ')
f.close()
vhc = y['Vhelavg']
verr = y['Verravg']
Radeg=y['Radeg']
Decdeg=y['Decdeg']
Rcoeff=y['Rcoeffavg']
logg=y['loggavg']
vhcgood= (vhc != -99.9) & (vhc >= -400) & (vhc <= 400) & (logg<=2.3)
vhcg=vhc[vhcgood]
l=[]
b=[]
Radg=Radeg[vhcgood]
Decdg=Decdeg[vhcgood]
for i in xrange(len(Radg)):
gcoords=ICRSCoordinates(Radg[i],Decdg[i]).convert(GalacticCoordinates)
l.append(gcoords.l.radians)
b.append(gcoords.b.radians)
The Error is:
Traceback (most recent call last):
File "gauss01_15.py", line 46, in <module>
gcoords=ICRSCoordinates(Radg[i],Decdg[i]).convert(GalacticCoordinates)
File "/Library/Python/2.7/site-packages/Astropysics-1.0-py2.7.egg/astropysics/coords/coordsys.py", line 1895, in convert
res = EpochalLatLongCoordinates.convert(self,tosys,optimize)
File "/Library/Python/2.7/site-packages/Astropysics-1.0-py2.7.egg/astropysics/coords/coordsys.py", line 1781, in convert
res = LatLongCoordinates.convert(self,tosys,optimize)
File "/Library/Python/2.7/site-packages/Astropysics-1.0-py2.7.egg/astropysics/coords/coordsys.py", line 1728, in convert
return CoordinateSystem.convert(self,tosys)
File "/Library/Python/2.7/site-packages/Astropysics-1.0-py2.7.egg/astropysics/coords/coordsys.py", line 1098, in convert
convpath = CoordinateSystem.getTransformPath(self.__class__,tosys)
File "/Library/Python/2.7/site-packages/Astropysics-1.0-py2.7.egg/astropysics/coords/coordsys.py", line 1038, in getTransformPath
path = nx.shortest_path(g,fromsys,tosys,weighted=True)
TypeError: shortest_path() got an unexpected keyword argument 'weighted'
I've never gotten this before and never had any issues with the simple python coordinate transform in astropysics. Any ideas are appreciated.
You probably need to upgrade your astropysics version. There was a change in the networkx (nx) dependency of astropysics, that has been taken care of in more recent version of astropysics. (The keyword argument weighted changed to weight, and apparently you have incompatible versions of astropysics and networkx to handle this change.)
The relevant fix is show in these lines in astropysics.
You probably want to upgrade the networkx package as well, if that doesn't happen automatically for you.
easy_install --upgrade astropysics
easy_install --upgrade networkx
hopefully does it for you.

Trouble to impliment scipy interpolation

I am trying to use the class shown here to interpolate some data.
I am having trouble getting this class to work. A minimal example is:
#!/usr/bin/env python
import sys
from mayavi.mlab import *
from mayavi import mlab
from numpy import *
import numpy as np
from mayavi.scripts import mayavi2
from mayavi.sources.vtk_file_reader import VTKFileReader
from mayavi.modules.outline import Outline
from mayavi.modules.grid_plane import GridPlane
from mayavi.modules.contour_grid_plane import ContourGridPlane
from mayavi.modules.iso_surface import IsoSurface
from mayavi.modules.scalar_cut_plane import ScalarCutPlane
import scipy
from scipy import interpolate
from scipy.interpolate import griddata
#from scipy.interpolate import RegularGridInterpolator
print 'Argument List:', str(sys.argv)
if (len(sys.argv)==5):
NX = int(sys.argv[1])
NY = int(sys.argv[2])
NZ = int(sys.argv[3])
fname = sys.argv[4]
else:
print "Error in parsing the command line arguments"
print "Command line arguments are: N_X N_Y N_Z filename "
sys.exit()
clf()
figure(bgcolor=(1, 1, 1))
len(sys.argv)
data = genfromtxt(fname)
x,y,z = mgrid[ 0:NX:1, 0:NY:1, 0:NZ:1 ]
#print(x)
index = 0
V=0*x
for k in range(NZ):
for j in range(NY):
for i in range(NX):
V[i][j][k] = 1000*data[index,5]
scipy.interpolate.RegularGridInterpolator(V, V, method='linear', bounds_error=True, fill_value=nan) #ERROR
The only bits really of interest to my current problem are the last line and all the import commands at the beginning
My error is:
Traceback (most recent call last):
File "contour-3d.py", line 70, in <module>
scipy.interpolate.RegularGridInterpolator(W, W, method='linear', bounds_error=True, fill_value=nan)
AttributeError: 'module' object has no attribute 'RegularGridInterpolator'
I'm not too familiar with python, so this may be a very basic error.
From the docs:
New in version 0.14.
It's likely you have a previous version of SciPy. If you are using Ubuntu, try running pip:
pip install --user --upgrade scipy
You might need some additional dependencies:
sudo apt-get install gcc gfortran python-dev libblas-dev liblapack-dev cython
For more information see http://www.scipy.org/install.html and http://www.scipy.org/scipylib/building/
scipy.interpolate.RegularGridInterpolator is a new function in scipy 0.14.x. You are likely to be using a older version. Updating scipy should solve the problem.

Categories