Re: [Chimera-users] Sum of volume data values
Hi Eran, Ok, here is code to read an MRC file and print the sum of map values without using Chimera. You need Python, the Numeric Python module which handles multidimensional arrays and the VolumeData module from Chimera. Numeric is obtained from sourceforge: http://sourceforge.net/project/showfiles.php?group_id=1369&package_id=1351 The VolumeData module is in your Chimera distribution. chimera/share/VolumeData Here's an example running the script where I've set PYTHONPATH to have access to the VolumeData module: % env PYTHONPATH=/usr/local/chimera/share python2.4 volumesum2.py Sum of values of groel.mrc = 21720.8984375 Send future questions to chimera-users@cgl.ucsf.edu so others who have similar problems can benefit. I get that email just as fast. Tom ------- file volumesum2.py follows: # Script to print the sum of volume data values. # Does not need Chimera, just the VolumeData module and Numeric and Python. path = 'groel.mrc' # Read the MRC volume data and get the 3-D Numeric array. from VolumeData.mrc.mrc_format import MRC_Data data = MRC_Data(path) m = data.matrix() # Compute the sum of the elements of the volume matrix. import Numeric sum = Numeric.sum(m.flat) # Print the sum print 'Sum of values of', path, ' = ', sum -------
Date: Wed, 12 Apr 2006 14:46:14 -0700 From: Eran Shacham <eshacham@burnham.org> To: Thomas Goddard <goddard@cgl.ucsf.edu> Subject: Re: Sum of volume data values
Hey Tom,
Thanks for your reply. However, I would like to do that from within a script I already wrote. i.e I would like to integrate a read_in method for an MRC file, access it values and then calculate the sum. I would like to avoid doing that by opening my file in Chimera at this stage of the program. Can it be done by importing some of your code?
Thanks, Eran.
Hey Tom, Sure, from now on I will send my questions to chimera-users email address. Sorry about that. The Numeric flat() method doesn't work for me. Error is: ValueError: flattened indexing only available for contiguous array So, instead I just wrote a loop to 'flatten' the array down: for i in m: for x in i: for j in x: for u in j: sum = sum + u Does that seem fine, or is .flat() doing something else? Cheers, Eran. Thomas Goddard wrote:
Hi Eran,
Ok, here is code to read an MRC file and print the sum of map values without using Chimera. You need Python, the Numeric Python module which handles multidimensional arrays and the VolumeData module from Chimera. Numeric is obtained from sourceforge:
http://sourceforge.net/project/showfiles.php?group_id=1369&package_id=1351
The VolumeData module is in your Chimera distribution.
chimera/share/VolumeData
Here's an example running the script where I've set PYTHONPATH to have access to the VolumeData module:
% env PYTHONPATH=/usr/local/chimera/share python2.4 volumesum2.py Sum of values of groel.mrc = 21720.8984375
Send future questions to chimera-users@cgl.ucsf.edu so others who have similar problems can benefit. I get that email just as fast.
Tom
------- file volumesum2.py follows:
# Script to print the sum of volume data values. # Does not need Chimera, just the VolumeData module and Numeric and Python.
path = 'groel.mrc'
# Read the MRC volume data and get the 3-D Numeric array. from VolumeData.mrc.mrc_format import MRC_Data data = MRC_Data(path) m = data.matrix()
# Compute the sum of the elements of the volume matrix. import Numeric sum = Numeric.sum(m.flat)
# Print the sum print 'Sum of values of', path, ' = ', sum
-------
Date: Wed, 12 Apr 2006 14:46:14 -0700 From: Eran Shacham <eshacham@burnham.org> To: Thomas Goddard <goddard@cgl.ucsf.edu> Subject: Re: Sum of volume data values
Hey Tom,
Thanks for your reply. However, I would like to do that from within a script I already wrote. i.e I would like to integrate a read_in method for an MRC file, access it values and then calculate the sum. I would like to avoid doing that by opening my file in Chimera at this stage of the program. Can it be done by importing some of your code?
Thanks, Eran.
Hi Eran, The m.flat only works for "contiguous arrays". Contiguous means you the values are all layed out in memory in order with no gaps. You get a non-contiguous array if you take an array subblock m2 = m[50:100,:,:], or if you change the step size m2 = m[::2,:,:], or if you permute the order of the axes m2 = Numeric.transpose(m). The only case you can get from the MRC_Data.matrix() call is the permuting of axes. The MRC format supports permutations of data axes, so depending on your file the m.flat will fail. The solution is to use import Numeric m1d = Numeric.ravel(m) sum = Numeric.sum(m1d) instead of import Numeric sum = Numeric.sum(m.flat) The Numeric.ravel() call makes a 1D array, making a copy if the passed in array is non-contiguous. This will certainly be much faster than your nested for loops for large arrays. Tom
participants (2)
-
Eran Shacham
-
Thomas Goddard