Theory and step-by-step instruction of scipy.ndimage.convolve - python

Good day to all.
Help me please to understand theory of function scipy.ndimage.convolve for 1D arrays. I know the formula from http://lagrange.univ-lyon1.fr/docs/scipy/0.17.1/generated/scipy.ndimage.convolve.html
C_i = \sum_j{I_{i+j-k} W_j},
but i can't understand, how can I get results manually.
For example: test_1 = scipy.ndimage.convolve([1, 2, 3], [1, 2, 3, 4, 5])
result is [24 24 30]
Or test_2 = scipy.ndimage.convolve([1, 2, 3], [3, 4, 5])
result is [15 22 31]
If I write here all attempts that I have made, it will take a lot of space.
Give me please step by step instructions on what to do with these examples manually.

Two tricky things going on here
1) the ndimage has this flag called "mode" which is set to "reflect" by default
2) two is that convolutions internally reverse one of the inputs
try comparing this piece of code
scipy.ndimage.convolve([1, 2, 3][::-1], [1, 2, 3, 4, 5],mode='constant')
to your by hand solution. (get rid of the "[::-1]" if you've already accounted for the reversal)

Related

multiple number of inputs to a function , specifically itertools.product()

i am trying to write a code that needs itertools.product to generate the output from a multiple number of lists , or even variable numbers , for example my inputs will be read from a list as :
a=[[[0,1],[2,3]],[[4,5],[6,7]]]
for this example it would be easy to write down :
itertools.product(a[0],a[1])
but as the list may increase in size and code may get pretty large and messy i can not use a code like this :
itertools.product(a[0],a[1],a[2],...)
what is a clean and efficient alternative way to do this?
i know it is not a function specific question , but a more general question about python itself and functions but i couldn't find any help about the existing functions, if anyone would point me in the right direction i'll be grateful.
thanks in advance
Use * to unpack the input list
>>> list(itertools.product(*a))
[([0, 1], [4, 5]), ([0, 1], [6, 7]), ([2, 3], [4, 5]), ([2, 3], [6, 7])]

Python convert image array(20,20) to array(400)

Using Python 2.7 and Numpy.
I have a B/W image stored in array(20,20) and I would like to convert it to an array(400). How can this be done in Python if I have many images, that is array(x,20,20)?
Thanks a lot!
EDIT: Thanks a lot. I got the problem wrong at the beginning, thus I was not able to figure out this simple piece of code.
I think numpy.flatten() is what you are looking for
>>> a = np.array([[1,2], [3,4]])
>>> a.flatten()
array([1, 2, 3, 4])
>>> a.flatten('F')
array([1, 3, 2, 4])
https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.ndarray.flatten.html
You can use np.ravel to get a 1D view of the array if possible; otherwise a copy is returned. See the linked documentation for the definition of 'if possible'.
a = np.array([[1, 2, 3], [4, 5, 6]])
print(a.ravel())
# [1 2 3 4 5 6]

simplification of algorithm that uses if statements to incorporate efficient iteration

I have the following code that has a small error but otherwise performs what I believe is an insertion sort. I have two questions
1. Why is the index error occurring on running this?
2. How can I, for teaching and learning purposes, simplify the entire code, to incorporate the use of for loops. I would like someone to explain the places in which they are repetition and how they correspond to a solution that uses loops.
*I realise that I need to use a nested loop using i and j, but can't quite work out how. I have added the outer level loop that provides i for the length of the loop.
Code:
def main():
#test with 6125
#test with 0142
#test with 4312
#test with 5432
list=[4,2,6,1]
print(list)
for i in range(len(list)):
#checking first element
if list[i+1]<list[i]:
temp1=list[i]
list[i]=list[i+1]
list[i+1]=temp1
print("first iteration:",list)
#Checking second element
if list[i+2]<list[i]:
temp1=list[i]
list[i]=list[i+2]
list[i+2]=temp1
print("second iteration:",list)
if list[i+2]>list[i]:
if list[i+2]<list[i+1]:
temp2=list[i+1]
list[i+1]=list[i+2]
list[i+2]=temp2
print("second iteration else #1:",list)
#Checking Third element
if list[i+3]<list[i]:
temp1=list[i]
temp2=list[i+1]
temp3=list[i+2]
list[i]=list[i+3]
list[i+1]=temp1
list[i+2]=temp2
list[i+3]=temp3
print("third iteration:",list)
elif list[i+3]>list[i]:
if list[i+3]<list[i+1]:
temp1=list[i+1]
temp2=list[i+2]
temp3=list[i+3]
list[i+1]=temp2
list[i+2]=temp3
list[i+3]=temp1
print("third iteration else #1:",list)
elif list[i+3]>list[i+1]:
if list[i+3]<list[i+2]:
temp3=list[i+2]
list[i+2]=list[i+3]
list[i+3]=temp3
print("third iteration else #2!",list)
main()
Output #1:
#Test with [4, 2, 6, 1]
('first iteration:', [2, 4, 6, 1])
('third iteration:', [1, 2, 4, 6])
**IndexError:** list index out of range on line 54 in main.py
Output Test #2
#Test with [5, 4, 3, 2]
('first iteration:', [4, 5, 3, 2])
('second iteration:', [3, 5, 4, 2])
('second iteration else #1:', [3, 4, 5, 2])
('third iteration:', [2, 3, 4, 5])
A trinket to the code can be found here (for online fixes)
https://trinket.io/python/fb5116c1ed
Note: I realise python can perform this without using the temporary variables, but I wish to implement the solution using them. Please, in any answers, use the existing and my originally provided code to break down.
Your outermost for loop is confusing, and causes the problem. You explicitly iterate through the four elements of the list, making all of the necessary checks. However, you then increment i, and try to repeat the process for elements 1 through 4 -- and eventually 2 through 5 and 3 through 6.
You don't have any elements past the 3rd, and that's your error. I suspect that you simply need to remove that loop.
I disabled the loop, keeping i at 0, and the program runs properly as far as you've written it.

Lists in Python vs vectors and matrices in Matlab

Coming from Matlab I am unable to even think of singular datapoints / variables. Anything I deal with is a matrix / array. After one week of searching and insuccesful trial and error I realise, that I ABSOLUTELY do NOT get the concept of dealing with matrices in (plain) Python.
I created
In[]: A = [[1,2,3], [9,8,7], [5,5,5]]
In[]: A
Out[]: [[1, 2, 3], [9, 8, 7], [5, 5, 5]]
Trying to extract the vectors in the matrix along the two dimensions:
In[]: A[:][1]
Out[]: [9, 8, 7]
In[]: A[1][:]
Out[]: [9, 8, 7]
'surprisingly' gives the same! No way to get a specific column (of course, except with one by one iteration).
Consequently, I am unable to manage merging matrix A with another vector, i.e. extending A with another column. Matlab style approach obviously is odd:
In[]: B = A, [4,6,8]
In[]: B
Out[]: ([[1, 2, 3], [9, 8, 7], [5, 5, 5]], [4, 6, 8])
Results in something nested, not an extension of A.
Same for
B = [A, [4,6,8]]
Ok, more Python-like:
A.append([11,12,13])
This easily adds a row. But is there a similar way to add a column??
(The frustrating thing is that Python doc gives all kinds of fancy examples but apparently these focus on demonstrating 'pythonic' solutions for one-dimensional lists.)
Coming from MATLAB myself, I understand your point.
The problem is that Python lists are not designed to serve as matrices. When indexing a list, you always work on the top level list elements, e.g. A[:][1] returns all the ([:]) three list elements, namely [1, 2, 3], [9, 8, 7] and [5, 5, 5]. Then you select the second ([1]) element from those, i.e. [9, 8, 7]. A[1][:] does the same, just the other way round.
This being said, you can still use nested lists for simple indexing tasks, as A[1][1] gives the expected result (8). However, if you are planing to migrate your whole MATLAB code to Python or work on non-trivial matrix problems, you should definitely consider using NumPy. There is even a NumPy guide for former MATLAB users.

Accessing Lower Triangle of a Numpy Matrix?

Okay, so basically lets say i have a matrix:
matrix([[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4]])
Is it possible to get the area below the diagonal easily when working with numpy matrixs? I looked around and could not find anything. I can do the standard, for loop way however wouldnt that somehow invalidate the performance gave by numpy?
I am working on calculating statististics ofcomparing model output results with actual results. The data i currently have been given, results in around a 10,000 x 10,000 matrix. I am mainly trying to just sum those elements.
Is there an easy way to do this?
You could use tril and triu. See them here:
http://docs.scipy.org/doc/numpy/reference/routines.array-creation.html
def tri_flat(array):
R = array.shape[0]
mask = np.asarray(np.invert(np.tri(R,R,dtype=bool)),dtype=float)
x,y = mask.nonzero()
return array[x,y]
I was looking for a convenience function myself, but this'll have to do... not sure it gets much easier. But if it does, I'd be interested to hear it. Every time you avoid a for-loop, an angel gets its wings.
-ejh
Quick NB: This avoids the diagonal... if you want it, and your matrix is symmetric, just omit the inversion (elem-wise NOT). Otherwise, you'll need a transpose in there.

Categories

Resources