Aaron Williams | 1 Jul 2004 05:47
Picon
Favicon

Help with FFT code

Hi

I am a new user of python and scipy. I am trying to write a code that Fast Fourier Transforms 2-D arrays saved as binary FORTRAN files. I would like for my code to also create an output file containing the certain statistics about the imported data set.
 
First I am having a problem calling my program from the command line. When I run the code with Python shell IDLE nothing happens.
 
Second I was wondering how to create an output file in my code.
 
 Here is an example of my code.
#################################
# FFT converter
#!/usr/bin/python
from scipy import *
io.array_import
#open Fortran Binary array
x = raw_input("enter in file path, i.e. 'path' else enter 'No'")
if x == No:
print "no data file enter end of program"
else:
fid=io.fopen(x,'r','n')
z1=fid.fort_read(256,dtype='f')
#data array is brought in as 1D array needs to be 2D
zcl=reshape(z1,(256,256))
zcl2=fftpack.fft2(zcl)
zcl3=fftpack.fftshift(zcl2)
zcl4=zcl3*conjugate(zlc3)
#power spectrum
zcl5=zcl4.real
#xplt.imagesc(zcl5)
#xplt.imagesc(zcl)
#Ask for the name of plot
q=raw_input("Enter name of transformed File")
xplt.imagesc(log(zcl5))
xplt.eps(q)
s=raw_input("Enter Name of Original")
xplt.imagesc(zcl)
xplt.eps(s)

#####################################

I have been able to enter the italicized parts into the command individually, but this not efficient for as many transforms I need to do.
 
Cheers,

Do you Yahoo!?
New and Improved Yahoo! Mail - Send 10MB messages!
_______________________________________________
SciPy-user mailing list
SciPy-user <at> scipy.net
http://www.scipy.net/mailman/listinfo/scipy-user
Travis Oliphant | 1 Jul 2004 07:30
Favicon

Re: Help with FFT code

Aaron Williams wrote:

> Hi
>
> I am a new user of python and scipy. I am trying to write a code that 
> Fast Fourier Transforms 2-D arrays saved as binary FORTRAN files. 
> I would like for my code to also create an output file containing the 
> certain statistics about the imported data set.

What format would you like this output file to be in?   Raw binary, text 
file, importable Python module, matlab .mat file?  There are lots of 
options here.  The answer usually depends on what you are going to do 
with the saved data.

If you will be sticking with using the data in Python later I would 
recommend  io.save(name, dict)

>  
> First I am having a problem calling my program from the command line. 
> When I run the code with Python shell IDLE nothing happens.

Probably due to the raw_input command.  This usually reads from standard 
input. 

Just run your program from the prompt, for example:

python myprogram.py

> Second I was wondering how to create an output file in my code.
>  
>  Here is an example of my code.
> #################################
> # FFT converter
> #!/usr/bin/python
> /from scipy import *
> /

> /io.array_import
> /

(not sure what you are trying to do here?)

> #open Fortran Binary array
> x = raw_input("enter in file path, i.e. 'path' else enter 'No'")
> if x == No:

should have quotes around No 
   if x=="No":

> print "no data file enter end of program"
> else:
> /fid=io.fopen(x,'r','n')
> /

default is 'n'  so fid=io.fopen(x,'r')  should do

> /z1=fid.fort_read(256,dtype='f')
> /#data array is brought in as 1D array needs to be 2D
> /zcl=reshape(z1,(256,256))
> zcl2=fftpack.fft2(zcl)
> zcl3=fftpack.fftshift(zcl2)
> zcl4=zcl3*conjugate(zlc3)
> /#power spectrum
> /zcl5=zcl4.real/

fft2 and fftshift can be accessed from scipy name space

zcl3 = fftshift(fft2(zcl))
zcl5 = real(zcl3*conj(zcl3))

> #xplt.imagesc(zcl5)
> #xplt.imagesc(zcl)

should work

> #Ask for the name of plot
> q=raw_input("Enter name of transformed File")
> /xplt.imagesc(log(zcl5))/
> xplt.eps(q)
> s=raw_input("Enter Name of Original")
> /xplt.imagesc(zcl)/
> xplt.eps(s)
>
> #####################################
>
> I have been able to enter the italicized parts into the command 
> individually, but this not efficient for as many transforms I need to do.

Again, it looks like the trouble might be the way raw_input is 
interacting with IDLE.

To save data for later use in Python use

io.save("mydata", {'varname1':vardata1, 'vardata2':vardata2}) 

Later

import mydata

will give you mydata.varname1 and mydata.varname2

Or you can use

io.write_arrray to write a text file

or

fid.fwrite    for low-level binary data writing (not usually recommended).

-Travis O.
Karthikesh Raju | 1 Jul 2004 07:54
Picon
Picon

Re: Help with FFT code

Hi All,

Regarding the saving and loading data part of this question, i was faced
with the dilema of shifting data between python and matlab. i have written
a small module dataloader.py (the latest version of which is at

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/286171
)

dumpData takes a dictionary of variables and makes separate dat files in
ascii
format and saves them in these files. When all the keys are traversed, it
compresses all the files into an archive.

loadData does the reverse, it loads the data from a file to a dictionary.

i also have the equivalent matlab routines

loadData.m - when paseed a tar.gz file, it extracts the variable name and
loads it in the workspace

dumpData.m - when passed with variables in the workspace, it writes them
in ascii files and compresess them.

The both work quite well.

Side note: please let me know of methods to improve the python module.

Warm regards
karthik

-----------------------------------------------------------------------
Karthikesh Raju,		    email: karthik <at> james.hut.fi
Researcher,			    http://www.cis.hut.fi/karthik
Helsinki University of Technology,  Tel: +358-9-451 5389
Laboratory of Comp. & Info. Sc.,    Fax: +358-9-451 3277
Department of Computer Sc.,
P.O Box 5400, FIN 02015 HUT,
Espoo, FINLAND
-----------------------------------------------------------------------

On Wed, 30 Jun 2004, Travis Oliphant wrote:

> Aaron Williams wrote:
>
> > Hi
> >
> > I am a new user of python and scipy. I am trying to write a code that
> > Fast Fourier Transforms 2-D arrays saved as binary FORTRAN files.
> > I would like for my code to also create an output file containing the
> > certain statistics about the imported data set.
>
> What format would you like this output file to be in?   Raw binary, text
> file, importable Python module, matlab .mat file?  There are lots of
> options here.  The answer usually depends on what you are going to do
> with the saved data.
>
> If you will be sticking with using the data in Python later I would
> recommend  io.save(name, dict)
>
>
> >
> > First I am having a problem calling my program from the command line.
> > When I run the code with Python shell IDLE nothing happens.
>
> Probably due to the raw_input command.  This usually reads from standard
> input.
>
> Just run your program from the prompt, for example:
>
> python myprogram.py
>
>
> > Second I was wondering how to create an output file in my code.
> >
> >  Here is an example of my code.
> > #################################
> > # FFT converter
> > #!/usr/bin/python
> > /from scipy import *
> > /
>
> > /io.array_import
> > /
>
> (not sure what you are trying to do here?)
>
> > #open Fortran Binary array
> > x = raw_input("enter in file path, i.e. 'path' else enter 'No'")
> > if x == No:
>
> should have quotes around No
>    if x=="No":
>
> > print "no data file enter end of program"
> > else:
> > /fid=io.fopen(x,'r','n')
> > /
>
> default is 'n'  so fid=io.fopen(x,'r')  should do
>
>
> > /z1=fid.fort_read(256,dtype='f')
> > /#data array is brought in as 1D array needs to be 2D
> > /zcl=reshape(z1,(256,256))
> > zcl2=fftpack.fft2(zcl)
> > zcl3=fftpack.fftshift(zcl2)
> > zcl4=zcl3*conjugate(zlc3)
> > /#power spectrum
> > /zcl5=zcl4.real/
>
> fft2 and fftshift can be accessed from scipy name space
>
> zcl3 = fftshift(fft2(zcl))
> zcl5 = real(zcl3*conj(zcl3))
>
> > #xplt.imagesc(zcl5)
> > #xplt.imagesc(zcl)
>
> should work
>
> > #Ask for the name of plot
> > q=raw_input("Enter name of transformed File")
> > /xplt.imagesc(log(zcl5))/
> > xplt.eps(q)
> > s=raw_input("Enter Name of Original")
> > /xplt.imagesc(zcl)/
> > xplt.eps(s)
> >
> > #####################################
> >
> > I have been able to enter the italicized parts into the command
> > individually, but this not efficient for as many transforms I need to do.
>
>
> Again, it looks like the trouble might be the way raw_input is
> interacting with IDLE.
>
> To save data for later use in Python use
>
> io.save("mydata", {'varname1':vardata1, 'vardata2':vardata2})
>
> Later
>
> import mydata
>
> will give you mydata.varname1 and mydata.varname2
>
> Or you can use
>
> io.write_arrray to write a text file
>
> or
>
> fid.fwrite    for low-level binary data writing (not usually recommended).
>
> -Travis O.
>
> _______________________________________________
> SciPy-user mailing list
> SciPy-user <at> scipy.net
> http://www.scipy.net/mailman/listinfo/scipy-user
>
>
Nils Wagner | 5 Jul 2004 10:47
Picon

Re: function for sqrt of matrix

Travis E. Oliphant wrote:
> Sudheer Phani wrote:
> 
>> Hello
>>
>>    I am looking for a function to calculate the square root (sqrt) of
>> matrix M. For e.g. x = sqrtm(M) such that M is square matrix and dot(x,x)
>> = M.
>>
> In SciPy:
> 
> It would be good to have a specific sqrtm function.
> 
> But for now you can do:
> 
> x = linalg.funm(M, sqrt)
> 
> 
> or
> 
> def sqrtm(M):
>     return linalg.funm(M,sqrt)
> 
> x = sqrtm(M)
> 
> 
> -Travis O.
> 
> _______________________________________________
> SciPy-user mailing list
> SciPy-user <at> scipy.net
> http://www.scipy.net/mailman/listinfo/scipy-user
> 

Very useful algorithms for sqrtm are available
via http://www.ma.man.ac.uk/~higham/pap-mf.html

Nils
Nils Wagner | 8 Jul 2004 14:07
Picon

Severe problems with linalg.eigvals

Dear experts,

I am going to solve nonlinear eigenvalue problems by a brute force 
approach (see the attachement). However I get into difficulties using 
linalg.eigvals

Traceback (most recent call last):
   File "brute.py", line 39, in ?
     w = linalg.eigvals(A,B)
   File "/usr/lib/python2.3/site-packages/scipy/linalg/decomp.py", line 
161, in eigvals
     return eig(a,b=b,left=0,right=0,overwrite_a=overwrite_a)
   File "/usr/lib/python2.3/site-packages/scipy/linalg/decomp.py", line 
109, in eig
     return _geneig(a1,b,left,right,overwrite_a,overwrite_b)
   File "/usr/lib/python2.3/site-packages/scipy/linalg/decomp.py", line 
60, in _geneig
     if info>0: raise LinAlgError,"generalized eig algorithm did not 
converge"
scipy.linalg.basic.LinAlgError: generalized eig algorithm did not converge

How can I get rid of this problem ?

Any pointer would be appreciated.

Thanks in advance

                             Nils
Attachment (brute.py): text/x-python, 804 bytes
_______________________________________________
SciPy-user mailing list
SciPy-user <at> scipy.net
http://www.scipy.net/mailman/listinfo/scipy-user
Bob.Cowdery | 9 Jul 2004 14:44

Problem with Weave

I am about to convert some python code to 'C' so am going through the Weave documentation. I can get most things to work except returning values from the 'C' code. I would guess the correct libraries are not getting into the compile but I don't know how to fix it. I can compile and run any 'C' code fine until I use any of the Py:: classes.
 
As an example if I type:
 
>>> import weave 
>>> a=1
>>> a=weave.inline("return_val = Py::new_reference_to(Py::Int(a+1));",['a'], verbose=2)
 
this gives me:
 
file changed
running build_ext
building 'sc_5b09eaf68ff529a1fbaedc892ca5a4531' extension
C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\bin\cl.exe /c /nologo /Ox /MD /W3 /GX -IC:\Python22\lib\site-packages\weave -IC:\Python22\lib\site-packages\weave\scxx -IC:\Python22\include /TpC:\DOCUME~1\cowderyb\LOCALS~1\Temp\CowderyB\python22_compiled\sc_5b09eaf68ff529a1fbaedc892ca5a4531.cpp /FoC:\DOCUME~1\cowderyb\LOCALS~1\Temp\CowderyB\python22_intermediate\compiler_ba943cc959c75c0df25fd4c114ec382f\Release\sc_5b09eaf68ff529a1fbaedc892ca5a4531.obj
Traceback (most recent call last):
  File "<input>", line 1, in ?
  File "C:\Python22\lib\site-packages\weave\inline_tools.py", line 335, in inline
    auto_downcast = auto_downcast,
  File "C:\Python22\lib\site-packages\weave\inline_tools.py", line 439, in compile_function
    verbose=verbose, **kw)
  File "C:\Python22\lib\site-packages\weave\ext_tools.py", line 340, in compile
    verbose = verbose, **kw)
  File "C:\Python22\lib\site-packages\weave\build_tools.py", line 272, in build_extension
    setup(name = module_name, ext_modules = [ext],verbose=verb)
  File "C:\Python22\lib\site-packages\scipy_distutils\core.py", line 42, in setup
    return old_setup(**new_attr)
  File "C:\Python22\lib\distutils\core.py", line 157, in setup
    raise SystemExit, "error: " + str(msg)
CompileError: error: command '"C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\bin\cl.exe"' failed with exit status 2
 
Any help appreciated.
 
Thanks
Bob
 
 
Bob Cowdery
CGI Senior Technical Architect
+44(0)1438 791517
Mobile: +44(0)7771 532138
 
 
 

*** Confidentiality Notice *** Proprietary/Confidential
Information belonging to CGI Group Inc. and its affiliates
may be contained in this message. If you are not a recipient
indicated or intended in this message (or responsible for
delivery of this message to such person), or you think for
any reason that this message may have been addressed to you
in error, you may not use or copy or deliver this message
to anyone else.  In such case, you should destroy this
message and are asked to notify the sender by reply email.

_______________________________________________
SciPy-user mailing list
SciPy-user <at> scipy.net
http://www.scipy.net/mailman/listinfo/scipy-user
Randy Heiland | 9 Jul 2004 16:19
Picon
Favicon

SciPy 2004 program

Hello,

When might the program/talks for this conference be updated so that a
person can determine whether or not to register by next week's deadline?

Thanks, Randy Heiland
Matthew Hellman | 6 Jul 2004 20:16
Picon
Favicon

floating characters

Hi,
  I was wondering if anyone had a method for converting a stringType to
an assignableType using characters.  For instance, if I read in
characters and then wanted to assign mathematical equations to them, 'r'
would be converted to r.  Essentially, I'd want to create an array of
equations as:

array(r, l, t, rl)

not array('r', 'l', 't', 'rl')

after reading in 'r','l','t','rl' where r,l,t,rl have all been assigned
different equations that can be evaluted for different values at
different times.

Thanks,
Matt
Eric Jonas | 9 Jul 2004 21:54
Picon

Reading in data as arrays, quickly and easily?

Hello! I'm trying to read in large chunks of binary data as arrays, but
the file formats are complex enough that there is lots of junk that
needs to be skipped over. I have a functioning datafile object in python
with a read(N) method that returns the next N data points in the file,
doing the various raw manipulations, endian conversions, and the like
internally. 

The problem is that it's really really slow. I've spent most of today
playing with boost.python, weave, raw numeric and numarray integration,
and the like. And yet I still can't figure out what I should -really- be
using.

Does anyone have any suggestions for approaches that have worked for
them? Ideally I'd love to read in my data and perform really basic
preprocessing in c++ and then return the resulting arrays to manipulate
using python. I'd rather not spend the weekend reinventing the wheel...

Thanks!
			...Eric
Sebastian Haase | 9 Jul 2004 22:17
Picon

Re: Reading in data as arrays, quickly and easily?

Hi Eric,
I assume you talk about Numeric, but in case you are open for numarray I use 
numarray's memmap quite successfully on files even larger than 1 GB (Linux; I 
think the effective limit for Windows might be lower ). It works for all 
datatypes and for byteswapped data too. You can skip any amount of bytes by 
having your mem-"slice" start at any offset you want. I actually  map the 
first part into a record-array so that I can read the parts of the 
"header"-information I'm interested in.

Regards,
Sebastian Haase

On Friday 09 July 2004 12:54 pm, Eric Jonas wrote:
> Hello! I'm trying to read in large chunks of binary data as arrays, but
> the file formats are complex enough that there is lots of junk that
> needs to be skipped over. I have a functioning datafile object in python
> with a read(N) method that returns the next N data points in the file,
> doing the various raw manipulations, endian conversions, and the like
> internally.
>
> The problem is that it's really really slow. I've spent most of today
> playing with boost.python, weave, raw numeric and numarray integration,
> and the like. And yet I still can't figure out what I should -really- be
> using.
>
> Does anyone have any suggestions for approaches that have worked for
> them? Ideally I'd love to read in my data and perform really basic
> preprocessing in c++ and then return the resulting arrays to manipulate
> using python. I'd rather not spend the weekend reinventing the wheel...
>
> Thanks!
> 			...Eric
>
> _______________________________________________
> SciPy-user mailing list
> SciPy-user <at> scipy.net
> http://www.scipy.net/mailman/listinfo/scipy-user

Gmane