How can I use numpy functions within NumbaPro functions? - python

I am new to CUDA, I am trying to use NumbaPro's cuda extension to compute some exponentials, however I cannot seem to use numpy within the cuda complied functions? I know i am doing something silly, but I cannot figure it out.
import numpy as np
from pylab import imshow, show, plot
from timeit import default_timer as timer
from numbapro import cuda
from numba import *
import math
def Deng_test(xx,Expre):
ExTest = (np.abs(np.sum(Expre)))**2
return ExTest
Deng_test_gpu = cuda.jit(argtypes=(double,complex128), restype=float64, device=True, inline=True)(Deng_test)

Related

DeprecationWarning rasied in Spyder for every Scipy function used

I'm coding in Spyder and the code runs, but every line that uses sp.___ raises a DeprecationWarning, e.g. DeprecationWarning: scipy.array is deprecated and will be removed in SciPy 2.0.0, use numpy.array instead.
Why is Spyder doing this and how do I allow me to use scipy without raising this error? Failing that, what can I do to suppress the error from popping up each time?
The code is like this:
import matplotlib.pyplot as plt,scipy as sp
import scipy.optimize as op
a=9.3779
x_in=sp.array([.095,.065,.09,.108,.125,.115,.040,.055,.055])
x=(x_in+14)
y_in=sp.array([.2,.6,.5,.4,.1,.3,-0.2,-0.4,0])
y=y_in+45
ax.plot(x_in,y_in,'ro')
plt.show()
This raises the error:
C:\Users\Shiva Pingle\Desktop\python\others\peaks.py:38: DeprecationWarning: scipy.array is deprecated and will be removed in SciPy 2.0.0, use numpy.array instead
x_in=sp.array([.095,.065,.09,.108,.125,.115,.040,.055,.055])
C:\Users\Shiva Pingle\Desktop\python\others\peaks.py:40: DeprecationWarning: scipy.array is deprecated and will be removed in SciPy 2.0.0, use numpy.array instead
y_in=sp.array([.2,.6,.5,.4,.1,.3,-0.2,-0.4,0])
Your solution in the comments will make you ignore all the deprecation warnings. This is not suggested.
You could instead import numpy as np and use the np.array().
Corrected code:
import matplotlib.pyplot as plt,scipy as sp
import scipy.optimize as op
import numpy as np # Added import of numpy
a=9.3779
x_in=np.array([.095,.065,.09,.108,.125,.115,.040,.055,.055]) # Changed sp to np
x=(x_in+14)
y_in=np.array([.2,.6,.5,.4,.1,.3,-0.2,-0.4,0]) # Changed sp to np
y=y_in+45
plt.plot(x_in,y_in,'ro') # Also changed the ax to plt
plt.show()

Pandas/Numpy Errors in Eclipse

I am working in python and eclipse for the first time and i am having troubles. This is the code I have now
from pandas import pandas as pd
from numpy import numpy as np
update = pd.read_csv("PPList.txt")
My error says that the read_csv is an undefined from import
My previous searches indicates that i need to change from numpy import numpy as np to just import numpy *
When I do that, the * has an error saying that it's expecting a comma or colon.
The imports should be
import pandas as pd
import numpy as np
Or
from pandas import *
from numpy import *

How to import single function from packages with the same name

What should I do about this? It seems python is calling the first function.
from numpy.random import multivariate_normal
from scipy.stats import multivariate_normal
The usual convention is:
import numpy as np
np.random.multivariate_normal
Then there won't be such collisions.

scipy equivalent for MATLAB spy

I have been porting code for an isomap algorithm from MATLAB to Python. I am trying to visualize the sparsity pattern using the spy function.
MATLAB command:
spy(sparse(A));
drawnow;
Python command:
matplotlib.pyplot.spy(scipy.sparse.csr_matrix(A))
plt.show()
I am not able to reproduce the MATLAB result in Python using the above command. Using the command with only A in non-sparse format gives quite similar result to MATLAB. But it's taking quite long (A being 2000-by-2000). What would be the MATLAB equivalent of a sparse function for scipy?
Maybe it's your version of matplotlib that makes trouble, as for me scipy.sparse and matplotlib.pylab work well together.
See sample code below that produces the 'spy' plot attached.
import matplotlib.pylab as plt
import scipy.sparse as sps
A = sps.rand(10000,10000, density=0.00001)
M = sps.csr_matrix(A)
plt.spy(M)
plt.show()
# Returns here '1.3.0'
matplotlib.__version__
This gives this plot:
I just released betterspy, which arguably does a better job here. Install with
pip install betterspy
and run with
import betterspy
from scipy import sparse
A = sparse.rand(20, 20, density=0.1)
betterspy.show(A)
betterspy.write_png("out.png", A)
With smaller markers:
import matplotlib.pylab as pl
import scipy.sparse as sps
import scipy.io
import sys
A=scipy.io.mmread(sys.argv[1])
pl.spy(A,precision=0.01, markersize=1)
pl.show()

pylab.X vs pylab.plt.X

I noticed that there some names are duplicated in pylab. Say I import pylab. Here are some examples:
E.g. 1 :
pylab.ion()
pylab.plt.ion()
E.g. 2
pylab.figure(1)
pylab.plot.figure(1)
Is there any difference between them? Why have the two of them?
You can always check:
>>> pylab.ion is pylab.plt.ion
True
So, they are the same function.
Some names are duplicated, probably historical reasons and to enable backward compatibility...
There is an unwritten convention to import matplotlib as:
import matplotlib.pyplot as plt
if you want just the plotting functionality.
Importing pylab creates a Matlab like environment with a lot of functionalities from NumPy. (so this is also a reason for name duplication)
If you read the source (the entirety of pylab.py excluding the docstring is below)
from __future__ import print_function
import sys, warnings
from matplotlib.cbook import flatten, is_string_like, exception_to_str, \
silent_list, iterable, dedent
import matplotlib as mpl
# make mpl.finance module available for backwards compatability, in case folks
# using pylab interface depended on not having to import it
import matplotlib.finance
from matplotlib.dates import date2num, num2date,\
datestr2num, strpdate2num, drange,\
epoch2num, num2epoch, mx2num,\
DateFormatter, IndexDateFormatter, DateLocator,\
RRuleLocator, YearLocator, MonthLocator, WeekdayLocator,\
DayLocator, HourLocator, MinuteLocator, SecondLocator,\
rrule, MO, TU, WE, TH, FR, SA, SU, YEARLY, MONTHLY,\
WEEKLY, DAILY, HOURLY, MINUTELY, SECONDLY, relativedelta
import matplotlib.dates # Do we need this at all?
# bring all the symbols in so folks can import them from
# pylab in one fell swoop
## We are still importing too many things from mlab; more cleanup is needed.
from matplotlib.mlab import griddata, stineman_interp, slopes, \
inside_poly, poly_below, poly_between, \
is_closed_polygon, path_length, distances_along_curve, vector_lengths
from matplotlib.mlab import window_hanning, window_none, detrend, demean, \
detrend_mean, detrend_none, detrend_linear, entropy, normpdf, levypdf, \
find, longest_contiguous_ones, longest_ones, prepca, \
prctile, prctile_rank, \
center_matrix, rk4, bivariate_normal, get_xyz_where, \
get_sparse_matrix, dist, \
dist_point_to_segment, segments_intersect, fftsurr, movavg, \
exp_safe, \
amap, rms_flat, l1norm, l2norm, norm_flat, frange, identity, \
base_repr, binary_repr, log2, ispower2, \
rec_append_fields, rec_drop_fields, rec_join, csv2rec, rec2csv, isvector
import matplotlib.mlab as mlab
import matplotlib.cbook as cbook
from numpy import *
from numpy.fft import *
from numpy.random import *
from numpy.linalg import *
from matplotlib.pyplot import *
# provide the recommended module abbrevs in the pylab namespace
import matplotlib.pyplot as plt
import numpy as np
import numpy.ma as ma
# don't let numpy's datetime hide stdlib
import datetime
if sys.version_info > (2, 6, 0):
bytes = __builtins__['bytes']
you see that both every thing from pyplot is imported (from matplotlb.pyplot import * and pyplot) and pyplot is imported (import pyplot as plt). You are not seeing two functions, you are seeing the same function/module imported multiple times.
As to why, why not? pylab is designed for interactive use. It is convenient to have all the function directly in your name space and it is quite handy to have plt in the name space as well for prototyping code.

Categories

Resources