Re: [Tutor] plotting several datasets and calling data from afar

2012-03-27 Thread Evert Rol
See below, but not all the way. Interspersed in the code.

snipped some text; to keep it digestible

 I am trying to set up a code to do some plotting and before I get too 
  far I wanted to ask some structure questions.  Basically I want to tell 
  python to read 2 datasets, plot them on the same scale on the same x-y axis 
  , read a third dataset and match the name from the first dataset, then 
  label certain values from the third... complicating matters is that all 
  these data are part of much, much larger sets in seperate files, the paths 
  look like:
  pathway1/namered.dat
  pathway2/nameblue.dat
  matchingfile.txt
 
  so I do fopen on the matchingfile, read it with asciitable, and then I have 
  a column in that file called 'name' and a column called 'M', I sort the 
  file, return a subset that is interesting, and get name1, name2, etc for 
  every subset.  I want to make a plot that looks like:
 
  plot pathway1/namered.dat and pathway2/nameblue.dat with label 'M' for 
  every value in the subset name1, each row[i] I need to assign to a seperate 
  window so that I get a multiplot with a shared x-axis, and stacking my 
  plots up the y-axis.  I do have multiplot working and I know how to plot 
  'M' for each subset.
 
  The conceptual trouble has come in, how do I match 'name' variable of my 
  subset 'name1' with the plot I want to do for pathway1/namered.dat and 
  pathway2/nameblue.dat... the key feature that is the same is the 'name' 
  variable, but in one instance I have to match the 'name'+'red.dat' and in 
  the other the 'name'+'blue.dat'
 
 It's not 100% clear to me what you precisely want to do, but here are a few 
 possibilites:
 
 - use a dictionary. Assign each dataset to a dictionary with the name as the 
 key (or the name + color). Eg, dataset['name1red'], dataset['name1blue'], 
 dataset['name2red'] etc. Each value in this dictionary is a dataset read by 
 asciitable.
  the (big) disadvantage is that you would read every single *.dat file 
 beforehand

The dictionaries look a bit like the right idea, but in the end I was able 
 to manipulate my input table so they aren't quite necessary.  The code I have 
 now, which partially works, is as follows:
 
 #!/usr/bin/python
 

snipped lots of imports

 #File from Read_All
 x=open('LowZ_joinAll')
 
 dat=asciitable.read(x,Reader=asciitable.NoHeader, 
 fill_values=['--','-999.99'])
 #gives dat file where filenames are first two columns
 
 ###
 bluefilename1=dat['col1']
 filename1=dat['col2']  
 
 #other stuff I need
 
 #Ra/Dec in decimal radians  
 Radeg1=dat[ 'col6']*180./math.pi   #ra-drad  
 Decdeg1=dat['col7']*180./math.pi#dec-drad 
 Vmag=dat['col8']
 Mag=dat['col15'] 
 EW1=dat['col16']  
 EW2=dat['col17']  
 EW3=dat['col18']  
 #Horizontal Branch Estimate
 VHB=18.0
 EWn = (0.5*abs(EW1)) + abs(EW2) + (0.6*abs(EW3))
 # NEED ABS VALUE FOR FORMULA
 FEHn = -2.66 + 0.42*(EWn + 0.64*(Vmag - VHB))   
 EW1_G=dat['col23']
 EW2_G=dat['col24']
 EW3_G=dat['col25']
 EWg = (0.5*abs(EW1_G)) + abs(EW2_G) + (0.6*abs(EW3_G))
 FEHg = -2.66 + 0.42*(EWg + 0.64*(Vmag - VHB))
 #use 0.15-0.2 dex as standard error -- R. Ibata
 FEHerror=0.2  
 #corrected velocity  
 Vhel=dat['col37']  
 V_err=dat['col38']  
 m_H=dat['col74']
 alpha_Fe=dat['col75'] 
 microturb=dat['col76'] 
 Vrot=dat['col77']
 Chisq=dat['col78'] 
 RVorig=dat['col79']  
 RVcorr=dat['col80'] 
 Heliocentric_RV=RVorig+RVcorr
 
 #now if I want to make plots I have to access paths
 
 #example, trying with one element
 path1r='../Core2dfdr-red-sorted/'+filename1[1]
 path1b='../Core2dfdr-blue/'+bluefilename1[1]
 
 #and use a title in the plot 
 title1='Data Fe/Hn='+str(FEHn[1])+' Fe/Hg='+str(FEHg[1])
 
 for i in xrange(len(Radeg1)):
 if i=5:
 #subset1
 print 'filename= ',filename1[i],'  ',bluefilename1[i],'  FEHn= 
 ',FEHn[i],'  FEHg= ',FEHg[i]
 #multiplot1
 fig1, (ax1, ax2, ax3, ax4, ax5) = plt.subplots(5, sharex=True)
 ax1.plot(path1r,'--ro')
 ax1.plot(path1b,'--bo')

Here's where you go wrong.
You're feeding plot() a path, not data. plot() just takes an x and y array of 
(float) values (and more options if wanted). Not like gnuplot for example, 
which can deal with a filename as input.
This is also the ValueError you get below: plot() sees a string (the filename), 
which it tries to interpret as an array of y values (it assumes x is [0, 1, 2, 
…] if it's missing), and that fails as it tries to convert the string to floats.

So, you'll first have to read the file, extract the data, then plot those data.
pyfits is the module you want to use for reading your data.

Hope that gets you further,

  Evert



 plt.show()
 #subset2
 #if i5 and i=10:
 #print 'filename= ',filename1[i],'  FEHn= ',FEHn[i],'  FEHg= ',FEHg[i]
 
 .
 this is where it breaks down.
The path name it cannot follow, and the title is not fully a string.  
 Since 

Re: [Tutor] (no subject)

2012-03-27 Thread Kushal Kumaran
On Tue, Mar 27, 2012 at 6:38 AM, thao nguyen thaonphu...@gmail.com wrote:
 Dear Support Team,

 I have built a function (enclosed here) to merge many files (in this example
 is 2 files: a1.txt and a2.txt) lines by lines. The output file is called
 final_file. However, i could not have it run successfully.

 Content of a1.txt:
 1
 3
 5


 Content of a2.txt:
 2
 4
 6


 Content of final_file.txt will be like:
 1
 2
 3
 4
 5
 6


 In Python, i called just written module:

 import argument
 reload(argument)
 argument.test(2,C:/a1.txt,C:/a2.txt)

 and get the error as below:
     ValueError: I/O operation on closed file
  File c:\append.py, line 5, in module
  argument.test(2,C:/a1.txt,C:/a2.txt)
  File c:\argument.py, line 28, in test
     for line_data in f:

 Could you please advise the resolution for this?


To start with, the error mentions the argument.py file, so you should
post that file as well.

You don't need to use the reload function in append.py.  Is there a
reason why it is being called?

-- 
regards,
kushal
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] (no subject)

2012-03-27 Thread Christian Witts

On 2012/03/27 03:08 AM, thao nguyen wrote:


Dear Support Team,

I have built a function (enclosed here) to merge many files (in this 
example is 2 files: a1.txt and a2.txt) lines by lines. The output 
file is called final_file. However, i could not have it run 
successfully.


Content of a1.txt:
1
3
5


Content of a2.txt:
2
4
6


Content of final_file.txt will be like:
1
2
3
4
5
6


In Python, i called just written module:

import argument
reload(argument)
argument.test(2,C:/a1.txt,C:/a2.txt)

and get the error as below:
ValueError: I/O operation on closed file
 File c:\append.py, line 5, in module
 argument.test(2,C:/a1.txt,C:/a2.txt)
 File c:\argument.py, line 28, in test
for line_data in f:

Could you please advise the resolution for this?


Thank you



___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Your Exception states what the problem is, which is operating on a 
closed file, and your traceback indicates it is when you're iterating 
over it.
As the error occurs in your argument.py file, you should post the 
relevant portions of that code too.


You could also do `cat file1 file2 filen  final_file` in a *nix prompt 
if that is your use-case.

--

Christian Witts
Python Developer
//
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Permissions Error

2012-03-27 Thread Tim Golden

On 27/03/2012 05:00, Michael Lewis wrote:

Traceback (most recent call last):
   File C:\Python27\Utilities\copyfiles.py, line 47, in module
 copyfiles(srcdir, dstdir)
   File C:\Python27\Utilities\copyfiles.py, line 42, in copyfiles
 shutil.copy(srcfile, dstfile)
   File C:\Python27\lib\shutil.py, line 116, in copy
 copyfile(src, dst)
   File C:\Python27\lib\shutil.py, line 81, in copyfile
 with open(src, 'rb') as fsrc:
IOError: [Errno 13] Permission denied: 'C:\\Users\\Chief
Ninja\\Pictures\\testdir'


It's not 100% clear what's going on, but it looks as though
you've passed along paths whicih result in shutil trying
to open a *directory* for reading -- which it won't be able
to do. (It is, in fact, possible to get a file handle to a directory
on Windows but you don't want to do that and shutil doesn't
know how).



I've noticed that the code runs if I use shutil.copyfiles instead of
shutil.copy. Do you know why?


Assuming my premise above, it would be because it special-cases
directories passed as parameter. (I haven't actually looked at
the code to check).

With this kind of problem the most helpful thing you can do for
yourself -- and for us if you can't resolve it yourself -- is
to pull yourself out of the depths of your copy/copyfiles
architecture and to try to do a simple:

open (c:/path/to/file.txt, rb)

If that succeeds then obviuosly there's no permissions issue as
such. If it fails (with an access error) then we're onto something.
If it succeeds then, probably your more complex copyfiles code
is doing something you don't think it's doing. Start chucking
out print () statements or logging or something so you know
*exactly* where the paths are pointing to which you're passing
into the shutil or other stdlib functions.

Hopefully all this will make it clearer what's going on. Full marks
for posting your code -- that does help. But you'll do better if
you post a *minimal* code example, and preferably a self-complete
one.

TJG
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] I/O operation on closed file?

2012-03-27 Thread bob gailer

in addition to posting the relevant code:
please in future use a meaningful subject which I have provided this time.
remember to reply-all so a copy goes to the list.

On 3/26/2012 9:08 PM, thao nguyen wrote:


Dear Support Team,

I have built a function (enclosed here) to merge many files (in this 
example is 2 files: a1.txt and a2.txt) lines by lines. The output 
file is called final_file. However, i could not have it run 
successfully.


Content of a1.txt:
1
3
5


Content of a2.txt:
2
4
6


Content of final_file.txt will be like:
1
2
3
4
5
6


In Python, i called just written module:

import argument
reload(argument)
argument.test(2,C:/a1.txt,C:/a2.txt)

and get the error as below:
ValueError: I/O operation on closed file
 File c:\append.py, line 5, in module
 argument.test(2,C:/a1.txt,C:/a2.txt)
 File c:\argument.py, line 28, in test
for line_data in f:

Could you please advise the resolution for this?


What does  I/O operation on closed file suggest to you?

--
Bob Gailer
919-636-4239
Chapel Hill NC

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor