array cannot be safely cast to required type

496 views
Skip to first unread message

Flavio Coelho

unread,
Apr 9, 2009, 6:34:15 AM4/9/09
to sage-support
Hi I having trouble with running code within sage which runs perfectly
in straight Python:

import scipy.stats as stats
import numpy

def lhsFromSample(sample,siz=100):
"""
Latin Hypercube Sample from a set of values

:Parameters:
- `sample`: list, tuple of array
- `siz`: Number or shape tuple for the output sample
"""
if not isinstance(sample, (list,tuple,numpy.ndarray)):
raise TypeError('sample is not a list, tuple or numpy vector')
n = siz
if isinstance(siz,(tuple,list)):
n=numpy.product(siz)
perc = numpy.arange(0,100.,100./n)
numpy.random.shuffle(perc)
smp = [stats.uniform(i,100./n).rvs() for i in perc]
v = numpy.array([stats.scoreatpercentile(sample,p) for p in smp])
if isinstance(siz,(tuple,list)):
v.shape = siz
return v

def lhsFromDensity(kde,siz=100):
'''
LHS sampling from a variables Kernel density estimate.

:Parameters:
- `kde`: scipy.stats.kde.gaussian_kde object
- `siz`: Number or shape tuple for the output sample
'''
if not isinstance(kde,scipy.stats.kde.gaussian_kde):
raise TypeError("kde is not a density object")
if isinstance(siz,(tuple,list)):
n=numpy.product(siz)
s = kde.resample(n)
v = lhsFromSample(s,n)
if isinstance(siz,(tuple,list)):
v.shape = siz
return v


def lhs(dist, parms, siz=100):
'''
Latin Hypercube sampling of any distrbution.
dist is is a scipy.stats random number generator
such as stats.norm, stats.beta, etc
parms is a tuple with the parameters needed for
the specified distribution.

:Parameters:
- `dist`: random number generator from scipy.stats module.
- `parms`: tuple of parameters as required for dist.
- `siz` :number or shape tuple for the output sample
'''
if not isinstance(dist, (stats.rv_discrete,stats.rv_continuous)):
raise TypeError('dist is not a scipy.stats distribution
object')
n=siz
if isinstance(siz,(tuple,list)):
n=numpy.product(siz)

perc = numpy.arange(0,1.,1./n)
numpy.random.shuffle(perc)
smp = [stats.uniform(i,1./n).rvs() for i in perc]
v = dist(*parms).ppf(smp)

if isinstance(siz,(tuple,list)):
v.shape = siz
return v

lhs(stats.uniform,(1,30),10)


If I run this code in Sage 3.4 it gives the following error and
traceback:



Traceback (click to the left for traceback)
...
TypeError: array cannot be safely cast to required type

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/fccoelho/.sage/sage_notebook/worksheets/admin/2/code/
4.py", line 79, in <module>
lhs(stats.uniform,
(_sage_const_1 ,_sage_const_30 ),_sage_const_10 )
File "/home/fccoelho/Downloads/sage-3.4-linux-Ubuntu_8.10-i686-Linux/
local/lib/python2.5/site-packages/SQLAlchemy-0.4.6-py2.5.egg/", line
1, in <module>

File "/home/fccoelho/.sage/sage_notebook/worksheets/admin/2/code/
4.py", line 73, in lhs
v = dist(*parms).ppf(smp)
File "/home/fccoelho/Downloads/sage-3.4-linux-Ubuntu_8.10-i686-Linux/
local/lib/python2.5/site-packages/scipy/stats/distributions.py", line
112, in ppf
return self.dist.ppf(q,*self.args,**self.kwds)
File "/home/fccoelho/Downloads/sage-3.4-linux-Ubuntu_8.10-i686-Linux/
local/lib/python2.5/site-packages/scipy/stats/distributions.py", line
563, in ppf
place(output,cond,self._ppf(*goodargs)*scale + loc)
File "/home/fccoelho/Downloads/sage-3.4-linux-Ubuntu_8.10-i686-Linux/
local/lib/python2.5/site-packages/numpy/lib/function_base.py", line
1357, in place
return _insert(arr, mask, vals)
TypeError: array cannot be safely cast to required type


can anyone tell me if this is a Sage "bug" or "feature" ;-)

Flávio

Robert Bradshaw

unread,
Apr 9, 2009, 6:45:09 AM4/9/09
to sage-s...@googlegroups.com
On Apr 9, 2009, at 3:34 AM, Flavio Coelho wrote:

> Hi I having trouble with running code within sage which runs perfectly
> in straight Python:

...

> TypeError: array cannot be safely cast to required type
>

> can anyone tell me if this is a Sage "bug" or "feature" ;-)

My guess is that it's a NumPy bug--NumPy doesn't play well with Sage
types (including floating point numbers). One reason we do this is so
that

sage: 1/3

actually gives the rational number 1/3, rather than 0 (or some
floating point approximation). Sage code is run through a preparser
that wraps literals in Sage types.

- Robert

Flavio Coelho

unread,
Apr 9, 2009, 6:58:32 AM4/9/09
to sage-support
I had already bumped against this type compatibility
problem, which I solved by turning off sage's preparser:

preparser(False)

However, this error won't go away when the preparser is turned off.

Flávio

Flavio Coelho

unread,
Apr 16, 2009, 4:58:11 AM4/16/09
to sage-support
I wonder if it would be possible, when in python mode: "%python", Sage
would revert to use standard python (and numpy) types.

I can now replicate my previous bug report with just a line of python
(excluding the import):

%python
from scipy import stats
stats.randint(0,23).ppf(3)

I don't see why sage types should get in the way here, since we are
only dealing with integers and in "python mode" (what is this supposed
to mean if not a pure python code block?)

Flávio


On 9 abr, 11:45, Robert Bradshaw <rober...@math.washington.edu> wrote:

Robert Bradshaw

unread,
Apr 16, 2009, 3:18:36 PM4/16/09
to sage-s...@googlegroups.com
On Apr 16, 2009, at 1:58 AM, Flavio Coelho wrote:

>
> I wonder if it would be possible, when in python mode: "%python", Sage
> would revert to use standard python (and numpy) types.
>
> I can now replicate my previous bug report with just a line of python
> (excluding the import):
>
> %python
> from scipy import stats
> stats.randint(0,23).ppf(3)
>
> I don't see why sage types should get in the way here, since we are
> only dealing with integers and in "python mode" (what is this supposed
> to mean if not a pure python code block?)

Hmm. I think this means we should bug the scipy folks about it,
sounds like a bug.

- Robert

Reply all
Reply to author
Forward
0 new messages