Python 2.7: Adding all elements of multiple arrays together - python

I'm reading image files from a list with a variable number of objects.
How can I add the arrays from each file to each other?
Here's an example adding only two arrays:
imageArray= [sum(x,y) for x,y in zip(io.imread(list[1]),io.imread(list[2]))]
I want to extend this to be able to add a variable number of arrays.
I have tried the following, without avail:
for x in filelist:
imageArray = [sum(y) for y in itertools.izip(io.imread(x))]
Which yields the error:
TypeError: Invalid dimensions for image data
Any help would be much appreciated!
Edit
I have been able to read all the images into array using part of unutbu's answer below:
im = map(SNIO.imread,filelist)
From here, I wish to add all the resulting arrays together (element wise). The correct solution would reproduce the result from the following code:
imageArray = [x+y+z for x,y,z in zip(im[0],im[1],im[2])]
which works fine, but, as stated in the original question, I wish to do this with any number of arrays rather than specifying them as in the previous example.

Is io.imread the same as scipy.ndimage.io.imread?
If so, then to add the arrays element-wise, use np.add.reduce:
import numpy as np
import scipy.ndimage.io as SNIO
files = ...
arrs = map(SNIO.imread, files)
result = np.add.reduce(arrs)
This will be far faster than calling Python's sum function for each location in the arrays.

Related

(Python3.x)Splitting arrays and saving them into new arrays

I'm writing a Python script intended to split a big array of numbers into equal sub-arrays. For that purpose, I use Numpy's split method as follows:
test=numpy.array_split(raw,nslices)
where raw is the complete array containing all the values, which are float64-type by the way.
nslices is the number of sub-arrays I want to create from the raw array.
In the script, nslices may vary depending of the size of the raw array, so I would like to "automatically" save each created sub-arrays in a particular array as : resultsarray(i)in a similar way that it can be made in MATLAB/Octave.
I tried to use afor in range loop in Python but I am only able to save the last sub-array in a variable.
What is the correct way to save the sub-array for each each incrementation from 1 to nslices?
Here, the complete code as is it now (I am a Python beginner, please bother the low-level of the script).
import numpy as np
file = open("results.txt", "r")
raw = np.loadtxt(fname=file, delimiter="/n", dtype='float64')
nslices = 3
rawslice = np.array_split(raw,nslices)
for i in range(0,len(rawslice)):
resultsarray=(rawslice[i])
print(rawslice[i])
Thank you very much for your help solving this problem!
First - you screwed up delimiter :)
It should be backslash+n \n instead of /n.
Second - as Serge already mentioned in comment you can just access to split parts by index (resultarray[0] to [2]). But if you really wanted to assign each part to a separate variable you can do this in fommowing way:
result_1_of_3, result_2_of_3, result_3_of_3 = rawslice
print(result_1_of_3, result_2_of_3, result_3_of_3)
But probably it isn't the way you should go.

How to print the full NumPy array without wrapping (in Jupyter Notebook)

This question is different from this one: How to print the full NumPy array, without truncation?
In that question, the user wanted to know how to print the full array without truncation. I can print the array without truncation just fine. My problem is that only a small portion of the screen width is used. When trying to inspect large adjacency matrices, it's impossible to inspect them when the rows unnecessarily wrap.
I'm asking this question here because it always takes me hours to find the solution, and I want to disambiguate it from that answer post above.
For example:
import networkx as nx
import numpy as np
np.set_printoptions(threshold=np.inf)
graph = nx.gnm_random_graph(20, 20, 1)
nx.to_numpy_matrix(graph)
This output displays as:
Just gonna post NaN's comment as the answer:
Use np.set_printoptions(linewidth=n) where n has to do with the number of characters (not array elements) per line. So in your case n=100 should do the trick.

In Python, is there a way to save an index subset of an array to use again later?

My code currently has an array, lets say for example:
arr = np.ones((512, 512)).
There is an area of the array that interests me. I usually access it like this:
arr[50:200,150:350] #do stuff here.
I was wondering, is there some way to make a variable that holds [50:200,150:350]? This way, if I need to slightly change my mask, I can do it once, on the top of the file, instead of everywhere it is accessed.
I tried mask = [50:200,150:350], arr[mask] but Python syntax won't allow that.
Thanks for the help!
Apparently numpy extends slicing and allows multiple slice() objects, one per dimension.
import numpy
o = numpy.ones((32, 32))
print(o[3:5,3:5])
foo = slice(3,5), slice(3,5)
print(o[foo])
Both incantations produce same result :)

Deleting rows of data for multiple variables

I have over 500 files that I cleaned up using a pandas data frame, and read in later as a matrix. I now want to delete missing rows of data from multiple variables for the entirety of my files. Each variable is pretty lengthy for its shape, for example, tc and wspd have the shape (84479, 558) and pressure has the shape (558,). I have tried the following example before and has worked in the past for single dimensional arrays with the same shape, but will no longer work with a two dimensional array.
bad=[]
for i in range(len(p)):
if p[i]==-9999 or tc[i]==-9999:
bad.append(i)
p=numpy.delete(p, bad)
tc=numpy.delete(tc, bad)
I tried using the following code instead but with no success (unfortunately).
import numpy as n
import pandas as pd
wspd=pd.read_pickle('/home/wspd').as_matrix()
tc=pd.read_pickle('/home/tc').as_matrix()
press=n.load('/home/file1.npz')
p=press['press']
names=press['names']
length=n.arange(0,84479)
for i in range(len(names[0])): #using the first one as a trial to run faster
print i #used later to see how far we have come in the 558 files
bad=[]
for j in range(len(length)):
if (wspd[j,i]==n.nan or tc[j,i]==n.nan):
bad.append(j)
print bad
From there I plan on deleting missing data as I had done previously except indexing which dimension I am deleting from within my first forloop.
new_tc=n.delete(tc[j,:], bad)
Unfortunately, this has not worked. I have also tried masking the array which also has not worked.
The reason I need to delete the data is my next library does not understand nan values, it requires strictly integers, floats, etc.
I am open to new methods for removing rows of data if anyone has any guidance. I greatly appreciate it.
I would load your 2 dimensional arrays as pandas DataFrames and then use the dropna function to drop any rows that contain a null value
wspd = pd.read_pickle('/home/wspd').dropna()
tc = pd.read_pickle('/home/tc').dropna()
The documentation for pandas.DataFrame.dropna is here

Python SciPy FFT function - Input?

I am currently writing some code which is supposed to perform FFT on a set of data. I have a python list of points and I can easily create a time list. When I run fft(datalist), I get the 'TypeError: 'numpy.ndarray' object is not callable' error. I think (but please correct me) the issue is that the list is one dimension and they have no attachment to time at all by using that one line of code above. My question is, do I have to input a two dimensional array with time and data points? or am I completely wrong and have to rethink?
Thanks, Mike
Edit - forgot to add some code. The t=time. Could it be because the number of entries in the array isnt equal to 2^n where N is an integer?
sample_rate=10.00
t=r_[0:191.6:1/sample_rate]
S = fft([mylist])
print S
The Numpy and SciPy fft functions are looking to have numpy arrays as input, not native python lists. Also they work just fine with lengths that are not powers of two. You probably just need to cast your list as an array before passing it to the fft.
From your example code above try:
from numpy.fftpack import fft
from numpy import array
""" However you generate your list goes here """
S = fft(array([mylist]))

Categories