Putting things together: Using numpy's loadtxt and nanmean to assemble a cube.
Last updated: Aug24,2020

Yesterday saw some revelations in using numpy to do image processing tasks. Namely, I fianlly attained some understanding of how to build and apply masks, Then the np.nanmean function can be used to derive the mean values along different axes of the masked arrays. Today I need to practice using different axes, ad know what I am doing. Here are some basic goals for today:

  1. How do I read 1d, 2d and 3d arrays from a test file with np.loadtxt?
  2. How to use np.nanmean on different araay axes?

Below I show a code that does a lot of useful stuff. Basically I read in a six "spectra". Each "spectrum consistes of 15 numbers. I build a 2x3x15 cube and then show what happens when I collapse each axis. I show the routine first in blue, and then the sample run.

 
 

This code:   /home/sco/NumPy/puttin_001.py    

 

#!/usr/bin/env python

def anykey(): 
 rr = raw_input("\nEnter any key to continue (E to end) = ")
 if ( rr == "E" ):
  print "I am ending the run. \n"
  quit() 
 return rr

def getfilename(): 
 rr = raw_input("\nEnter name of text file to input (t1.txt) = ")
 return rr

#===========================================================================
# Here is the start of my "main" program 
# This early stuff, including making the function above, are from reading 
# articles in the very useful:   https://stackabuse.com/
#===========================================================================

import numpy as np

print "\nOutput from my test code:   /home/sco/NumPy/puttin_001.py\n"
print "To run this code:     % python /home/sco/NumPy/puttin_001.py \n"

#--------------------------------------------------------------------------
# Just for completeness, I use the text load method of 2darray_ex2.py
# To find out more I can use "% ipython" then  
# In [1]: import numpy as np
#    ...: help(np.loadtxt)         
#--------------------------------------------------------------------------

tfile1 = getfilename() 
#print tfile1 
#print type(tfile1)
#print 't1.txt'
#print type('t1.txt')
print "\nnumpy array will be loaded from the file:  %s\n" % ( tfile1 ) 

b = np.loadtxt( tfile1, dtype=float)
print "np.loadtxt has run. \n"
ss = anykey()
if ( ss == "E" ):
 print "I am ending the run. \n"
 quit() 

print "\nHere is my loaded numpy array (named b):\n",
print b
print "Type of b         = %s \n" % ( type(b) ),
print "b.dtype = ", b.dtype
print "b.shape = ", b.shape
print "b.ndim = ", b.ndim
print "b.size = ", b.size
ss = anykey()
if ( ss == "E" ):
 print "I am ending the run. \n"
 quit() 

# Get the 2d array dimensions as integers from the tuple from b.shape 
nax1,nax2 = b.shape
print "\nnax1,nax2 of the b (as integers) = %d %d \n" % (nax1,nax2) 
ss = anykey()

#----------------------------------------------------------------------------------------------------
# Determine the shape of the new 3D array 
print "\nWe want to keep the coloumns (lines in the file) as the x (spectral) dimesion. \n"
print "Hence, we want the last axis of the new array to have the value = %d\n" % (nax2) 
newdims = raw_input("\nEnter 3d array shape (2 2 10): ") 
# get the 3 new axes as integers 
a = newdims.split() 
nindex1 = int(a[0]) 
nindex2 = int(a[1]) 
nindex3 = int(a[2]) 
print "\nThe number of elements for indces(1,2,3) = %d, %d, %d \n" % ( nindex1, nindex2, nindex3 ) 
#----------------------------------------------------------------------------------------------------

# Is this possible?
n2d = nindex1*nindex2
modulo_valu = nax1 % n2d
print "nax1, n2d, modulo_valu = %d %d %d \n" % (nax1, n2d, modulo_valu) 
if ( modulo_valu != 0 ):
 print "Sorry, the modulus is not zero. \n"
 quit() 

B = b.reshape(nindex1,nindex2,nindex3)
print "\nHere is my reshaped array (named B):\n",
print B
print "Type of B         = %s \n" % ( type(B) ),
print "B.dtype = ", B.dtype
print "B.shape = ", B.shape
print "B.ndim = ", B.ndim
print "B.size = ", B.size

ss=anykey() 
print "\n*** Now I will cllapse each axis and see what happens. ***\n"

# Collapse along the 0 axis 
Cax0 = np.nanmean(B, axis=0)  
print "I do:   Cax0 = np.nanmean(B, axis=0)\n" 
print "type(Cax0)  = ",type(Cax0)
print "Cax0.shape  = ", Cax0.shape
print "Here is the Cax0 array: \n",
print Cax0 

# Collapse along the 1 axis 
Cax1 = np.nanmean(B, axis=1)  
print "I do:   Cax1 = np.nanmean(B, axis=1)\n" 
print "type(Cax1)  = ",type(Cax1)
print "Cax1.shape  = ", Cax1.shape
print "Here is the Cax1 array: \n",
print Cax1 

# Collapse along the 2 axis 
Cax2 = np.nanmean(B, axis=2)  
print "I do:   Cax2 = np.nanmean(B, axis=2)\n" 
print "type(Cax2)  = ",type(Cax2)
print "Cax2.shape  = ", Cax2.shape
print "Here is the Cax2 array: \n",
print Cax2 
 

Here is a sample run: 
% cat t3.txt 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15  
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15  
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15  
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15  
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15  
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15  
  
% python puttin_001.py 

Output from my test code:   /home/sco/NumPy/puttin_001.py

To run this code:     % python /home/sco/NumPy/puttin_001.py 


Enter name of text file to input (t1.txt) = t3.txt 

numpy array will be loaded from the file:  t3.txt

np.loadtxt has run. 


Enter any key to continue (E to end) = a 

Here is my loaded numpy array (named b):
[[ 1.  2.  3.  4.  5.  6.  7.  8.  9. 10. 11. 12. 13. 14. 15.]
 [ 1.  2.  3.  4.  5.  6.  7.  8.  9. 10. 11. 12. 13. 14. 15.]
 [ 1.  2.  3.  4.  5.  6.  7.  8.  9. 10. 11. 12. 13. 14. 15.]
 [ 1.  2.  3.  4.  5.  6.  7.  8.  9. 10. 11. 12. 13. 14. 15.]
 [ 1.  2.  3.  4.  5.  6.  7.  8.  9. 10. 11. 12. 13. 14. 15.]
 [ 1.  2.  3.  4.  5.  6.  7.  8.  9. 10. 11. 12. 13. 14. 15.]]
Type of b         =  
b.dtype =  float64
b.shape =  (6, 15)
b.ndim =  2
b.size =  90

Enter any key to continue (E to end) = a 

nax1,nax2 of the b (as integers) = 6 15 


Enter any key to continue (E to end) = a  

We want to keep the coloumns (lines in the file) as the x (spectral) dimesion. 

Hence, we want the last axis of the new array to have the value = 15


Enter 3d array shape (2 2 10): 2 3 15 

The number of elements for indces(1,2,3) = 2, 3, 15 

nax1, n2d, modulo_valu = 6 6 0 


Here is my reshaped array (named B):
[[[ 1.  2.  3.  4.  5.  6.  7.  8.  9. 10. 11. 12. 13. 14. 15.]
  [ 1.  2.  3.  4.  5.  6.  7.  8.  9. 10. 11. 12. 13. 14. 15.]
  [ 1.  2.  3.  4.  5.  6.  7.  8.  9. 10. 11. 12. 13. 14. 15.]]

 [[ 1.  2.  3.  4.  5.  6.  7.  8.  9. 10. 11. 12. 13. 14. 15.]
  [ 1.  2.  3.  4.  5.  6.  7.  8.  9. 10. 11. 12. 13. 14. 15.]
  [ 1.  2.  3.  4.  5.  6.  7.  8.  9. 10. 11. 12. 13. 14. 15.]]]
Type of B         =  
B.dtype =  float64
B.shape =  (2, 3, 15)
B.ndim =  3
B.size =  90

Enter any key to continue (E to end) = any string I want can go here   

*** Now I will cllapse each axis and see what happens. ***

I do:   Cax0 = np.nanmean(B, axis=0)

type(Cax0)  =  
Cax0.shape  =  (3, 15)
Here is the Cax0 array: 
[[ 1.  2.  3.  4.  5.  6.  7.  8.  9. 10. 11. 12. 13. 14. 15.]
 [ 1.  2.  3.  4.  5.  6.  7.  8.  9. 10. 11. 12. 13. 14. 15.]
 [ 1.  2.  3.  4.  5.  6.  7.  8.  9. 10. 11. 12. 13. 14. 15.]]
I do:   Cax1 = np.nanmean(B, axis=1)

type(Cax1)  =  
Cax1.shape  =  (2, 15)
Here is the Cax1 array: 
[[ 1.  2.  3.  4.  5.  6.  7.  8.  9. 10. 11. 12. 13. 14. 15.]
 [ 1.  2.  3.  4.  5.  6.  7.  8.  9. 10. 11. 12. 13. 14. 15.]]
I do:   Cax2 = np.nanmean(B, axis=2)

type(Cax2)  =  
Cax2.shape  =  (2, 3)
Here is the Cax2 array: 
[[8. 8. 8.]
 [8. 8. 8.]]

 



Back to calling page