Skip to content

Commit

Permalink
Changes so that array conversion happens correctly. Fixed speed of fr…
Browse files Browse the repository at this point in the history
…obenius norm. Added flatten to imread
  • Loading branch information
teoliphant committed Sep 30, 2004
1 parent 7ea23f3 commit 471f247
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 23 deletions.
8 changes: 4 additions & 4 deletions Lib/linalg/basic.py
Expand Up @@ -11,7 +11,7 @@
from lapack import get_lapack_funcs
from flinalg import get_flinalg_funcs
from scipy_base import asarray,zeros,sum,NewAxis,greater_equal,subtract,arange,\
conjugate,ravel,r_,mgrid,take,ones,dot,transpose,diag,sqrt
conjugate,ravel,r_,mgrid,take,ones,dot,transpose,diag,sqrt,add
import Matrix
import scipy_base
from scipy_base import asarray_chkfinite
Expand Down Expand Up @@ -270,8 +270,8 @@ def norm(x, ord=2):
elif ord == -Inf:
return scipy_base.amin(scipy_base.sum(abs(x),axis=1))
elif ord in ['fro','f']:
X = scipy_base.mat(x)
return sqrt(sum(diag(X.H * X)))
val = real((conjugate(x)*x).flat)
return sqrt(add.reduce(val))
else:
raise ValueError, "Invalid norm order for matrices."
else:
Expand Down Expand Up @@ -442,7 +442,7 @@ def toeplitz(c,r=None):
return c
if r is None:
r = c
r[0] = conjugate(r[0])
r[0] = conjugoate(r[0])
c = conjugate(c)
r,c = map(asarray_chkfinite,(r,c))
r,c = map(ravel,(r,c))
Expand Down
27 changes: 21 additions & 6 deletions Lib/pilutil.py
Expand Up @@ -28,12 +28,18 @@ def bytescale(data, cmin=None, cmax=None, high=255, low=0):
bytedata = ((data*1.0-cmin)*scale + 0.4999).astype(_UInt8)
return bytedata + cast[_UInt8](low)

def imread(name,flatten=0):
"""Read an image file from a filename.
def imread(name):
"""Read an image file.
Optional arguments:
- flatten (0): if true, the image is flattened by calling convert('F') on
the resulting image object. This flattens the color layers into a single
grayscale layer.
"""

im = Image.open(name)
return fromimage(im)
return fromimage(im,flatten=flatten)

def imsave(name, arr):
"""Save an array to an image file.
Expand All @@ -42,11 +48,20 @@ def imsave(name, arr):
im.save(name)
return

def fromimage(im):
"""Takes a PIL image and returns a copy of the image in a Numeric container.
If the image is RGB returns a 3-dimensional array: arr[:,:,n] is each channel
def fromimage(im, flatten=0):
"""Takes a PIL image and returns a copy of the image in a Numeric container.
If the image is RGB returns a 3-dimensional array: arr[:,:,n] is each channel
Optional arguments:
- flatten (0): if true, the image is flattened by calling convert('F') on
the image object before extracting the numerical data. This flattens the
color layers into a single grayscale layer. Note that the supplied image
object is NOT modified.
"""
assert Image.isImageType(im), "Not a PIL image."
if flatten:
im = im.convert('F')
mode = im.mode
adjust = 0
if mode == '1':
Expand Down
2 changes: 1 addition & 1 deletion Lib/xplt/eztest.py
Expand Up @@ -133,7 +133,7 @@ def demo ():
y = zeros ( (10, 10), Float)
vx = zeros ( (10, 10), Float)
vy = zeros ( (10, 10), Float)
ireg = zeros ( (10, 10), Int32)
ireg = zeros ( (10, 10), Int)
for i in range (10) :
for j in range (10) :
vy [i, j] = sin (i)
Expand Down
20 changes: 10 additions & 10 deletions Lib/xplt/pygist/gistCmodule.c
Expand Up @@ -2231,7 +2231,7 @@ static PyObject *contour (PyObject * self, PyObject * args, PyObject * kd)
/* create three arrays and their data, make sure DECREF'able */
npt = (int) nparts;
NEW_MEM (np, npt, long, PyObject *);
RET_ARR ( anp, 1, &npt, PyArray_INT, (char *) np, PyObject *);
RET_ARR ( anp, 1, &npt, PyArray_LONG, (char *) np, PyObject *);
SET_OWN (anp);
NEW_MEM (xcp, ntotal, double, PyObject *);
RET_ARR ( axcp, 1, &ntotal, PyArray_DOUBLE, (char *) xcp, PyObject *);
Expand Down Expand Up @@ -6042,7 +6042,7 @@ static int set_def_reg (int nr, int nc)

ne = nr * nc;
newlen = ne + nc + 1;
TRY (ra1 = (PyArrayObject *) PyArray_FromDims (1, &newlen, PyArray_INT), 0);
TRY (ra1 = (PyArrayObject *) PyArray_FromDims (1, &newlen, PyArray_LONG), 0);
p1 = (int *) A_DATA (ra1);

/* Fill in the data part of the new region array. */
Expand Down Expand Up @@ -6126,7 +6126,7 @@ static int set_reg (PyObject *op)
int i, ok, nr, nc, ne, newlen, *p2, *p1;
PyArrayObject *ra2, *ra1;

ok = (isARRAY(op) && (A_NDIM(op) == 2) && ((A_TYPE(op) == PyArray_INT)
ok = (isARRAY(op) && (A_NDIM(op) == 2) && ((A_TYPE(op) == PyArray_LONG)
|| (A_TYPE(op) == PyArray_LONG)));
if (!ok) {
return (int) ERRSS ("(ireg) must be a 2-D int array");
Expand All @@ -6143,9 +6143,9 @@ static int set_reg (PyObject *op)

ne = nr * nc;
newlen = ne + nc + 1;
NEW_ARR (ra1, 1, &newlen, PyArray_INT, int);
NEW_ARR (ra1, 1, &newlen, PyArray_LONG, int);
p1 = (int *) A_DATA (ra1);
GET_ARR (ra2, op, PyArray_INT, 2, int);
GET_ARR (ra2, op, PyArray_LONG, 2, int);
p2 = (int *) A_DATA (ra2);

/* Fill in the data part of the new region array. */
Expand Down Expand Up @@ -6770,7 +6770,7 @@ static PyObject * slice2 (PyObject * self, PyObject * args)
GET_ARR (aplane, oplane, PyArray_DOUBLE, 1, PyObject *);
}
/* convert arguments to arrays */
GET_ARR (anverts, onverts, PyArray_INT, 1, PyObject *);
GET_ARR (anverts, onverts, PyArray_LONG, 1, PyObject *);
GET_ARR (axyzverts, oxyzverts, PyArray_DOUBLE, 2, PyObject *);
if (isARRAY (ovalues)) {
if (A_TYPE (ovalues) == PyArray_DOUBLE) {
Expand Down Expand Up @@ -7182,7 +7182,7 @@ static PyObject * slice2 (PyObject * self, PyObject * args)
/* done if no partially clipped polys */
if (list1_length == 0 && listc_length == 0) {
if (rnverts) {
RET_ARR (ornverts, 1, & (rnverts->size), PyArray_INT, (char *) rnvertsd,
RET_ARR (ornverts, 1, & (rnverts->size), PyArray_LONG, (char *) rnvertsd,
PyObject *);
SET_OWN (ornverts);
}
Expand Down Expand Up @@ -7256,7 +7256,7 @@ static PyObject * slice2 (PyObject * self, PyObject * args)
}
else {
/* Build the rest */
RET_ARR (ornvertb, 1, & (rnvertb->size), PyArray_INT, (char *) rnvertbd,
RET_ARR (ornvertb, 1, & (rnvertb->size), PyArray_LONG, (char *) rnvertbd,
PyObject *);
SET_OWN (ornvertb);
xdims [0] = rxyzvertb->size / 3;
Expand Down Expand Up @@ -7503,7 +7503,7 @@ static PyObject * slice2 (PyObject * self, PyObject * args)
freeArray (keep, 0);

/* All done, set up return values. */
RET_ARR (ornverts, 1, & (rnverts->size), PyArray_INT, (char *) rnvertsd,
RET_ARR (ornverts, 1, & (rnverts->size), PyArray_LONG, (char *) rnvertsd,
PyObject *);
SET_OWN (ornverts);
xdims [0] = rxyzverts->size / 3;
Expand Down Expand Up @@ -7550,7 +7550,7 @@ static PyObject * slice2 (PyObject * self, PyObject * args)
}
else {
/* Build the rest */
RET_ARR (ornvertb, 1, & (rnvertb->size), PyArray_INT, (char *) rnvertbd,
RET_ARR (ornvertb, 1, & (rnvertb->size), PyArray_LONG, (char *) rnvertbd,
PyObject *);
SET_OWN (ornvertb);
xdims [0] = rxyzvertb->size / 3;
Expand Down
4 changes: 2 additions & 2 deletions Lib/xplt/quadmesh.py
Expand Up @@ -224,13 +224,13 @@ def __init__ ( self , * kwds , ** keywords ) :
"shape of ireg must be nx by ny."
else :
if self.dimsofx == 2:
self.ireg = array (self.x).astype (Int32)
self.ireg = array (self.x).astype (Int)
else :
if self.nx is None :
self.nx = 50
if self.ny is None :
self.ny = 50
self.ireg = array ( (self.nx, self.ny), Int32)
self.ireg = array ( (self.nx, self.ny), Int)
self.ireg [0:self.nx, 0] = 0
self.ireg [0, 0:self.ny] = 0
self.ireg [1:self.nx, 1:self.ny] = 1
Expand Down

0 comments on commit 471f247

Please sign in to comment.