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.
Related
I am trying to execute this code in Visual Studio Code, the code works, my problem is related to the import of numpy; the other imports are working.
import codecs
from operator import le
import string
from struct import unpack
import paho.mqtt.client as mqtt
import struct
import numpy as np
def on_connect1(client1, userdata1, flags1, rc1):
client1.subscribe("MYTOPIC")
def on_message1(client1, userdata1, msg1):
#print(msg1.topic+" "+ "TERMORESISTENZA: "+str(msg1.payload))
Byte_Order = '<' # little-endian
Format_Characters = 'f' # float (4 bytes)
data_format = Byte_Order + Format_Characters
r = np.array(list(struct.iter_unpack(data_format, msg1.payload)), dtype=float)
print(r)
When I run the code it returns me this error:
ModuleNotFoundError: No module named 'numpy'
Any suggestions?
I used the Windows Command Prompt to install numpy with the command:
pip install numpy
and solved the problem
When I run the code:
import sys
import math
import numpy as np
import matplotlib.pyplot as plt
from typing import List, Tuple
import os
import scipy.io, scipy.signal
import colorednoise as cn
def generate_pink_noise(singal_length):
beta = 1
samples = singal_length
noise = cn.powerlaw_psd_gaussian(beta, samples)
return noise
I get:
Traceback (most recent call last):
File "...MEA_foward_model.py", line 11, in <module>
import colorednoise as cn
ModuleNotFoundError: No module named 'colorednoise'
Note the 'test' environment I'm using shown here when I get the error: environment.
However the same code runs without error in Jupiter notebook:
import colorednoise as cn
signal_length =10
beta = 1 # the exponent: 0=white noite; 1=pink noise; 2=red noise (also "brownian noise")
samples = signal_length # number of samples to generate (time series extension)
noise = cn.powerlaw_psd_gaussian(beta, samples)
This image shows (see top right corner) shows that the same environment is used
jupyter notebook output. What is causing this dissonance between the two different behaviours?
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.
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
I wrote a module (processing_0) in which I import all packages and modules required for my project.
import networkx as nx
import matplotlib.pyplot as plt
import numpy as np
import collections
import la
import csv
import fishery
import re
from collections import OrderedDict
import processing_1
import processing_2
import processing_3
from processing_1 import readingraph, readinpathgraph, preparefisher, inEEG
from processing_2 import pathwayprofile
from processing_3 import checkkin
from fishery import fisher
The modules that I wrote (processing_1/2/3) all require access to networkx (nx).
As part of the master module, I have a the startup function:
def startup():
EEG = readingraph("/.../file1")
EET = readingraph("/.../file2")
EEL = readingraph("/.../file3")
return EEG, EET, EEL
However, after importing processing_0 and trying to run startup() that uses readingraph from processing_1, I keep getting the error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "processing_0.py", line 31, in startup
EEG = readingraph("/.../file1")
File "processing_1.py", line 4, in process
graph = nx.read_adjlist(filename)
NameError: global name 'nx' is not defined
Is there any way to globally import networkx as nx and make it accessible to all imported modules?
if you are using linux ubuntu, do these followings in order.
sudo apt-get update
sudo apt-get install python-networkx
go to pycharm env and > file>setting> interpreter and structure to
configure your python env and add packages, there is all packages
that are in env, then click on + to add newpackage
type networkx in search text box, and then select it from package
list and click on install
after finish it , click ok and close windows
In every file that you use networkx you need to import it. So just repeat the line
import networkx as nx
inside the file processing_1.py