Accesing a matrix within a .mat file with python - python

I'm translating matlab code to python. I have a few matrices within a .mat file called 'AK_1'. I only want to access the data in one of these matrices. The matlab code accesses it this way where .response1 is the desired matrix:
numtrials1 = subject_data1.response1(1,:);
I tried loading all the data into a dict so I could then loop through it to the desired matrice with this code but that did not produce a workable result.
subject_data1_dict = {}
subject_data1 = scipy.io.loadmat('./MAT_Data_Full_AAAD_V2/AK_1.mat', subject_data1_dict)
How can I access only the matrix 'response1' within the file AK_1.mat?

Say you have a myfile.mat with the following struct S:
S =
response1: [5x5 double]
response2: [5x5 double]
response3: [5x5 double]
And you want to access response1 from python. Then:
>>> from scipy.io import loadmat
>>> D = loadmat("myfile.mat", variable_names = ("S",) )
>>> D["S"]["response1"] # returns matlab's S.response1
If you wanted to select more variables contained in the file than just S, you just add them in the tuple, i.e. variable_names=("S","otherVar")
Obviously, if all you're interested in is the response1 array, you can bypass collecting the dictionary altogether, i.e.:
>>> response1 = loadmat("myfile.mat", variable_names = ("S",) )["S"]["response1"]
>>> response1
array([[ array([[ 9, 1, 2, 2, 7],
[10, 3, 10, 5, 1],
[ 2, 6, 10, 10, 9],
[10, 10, 5, 8, 10],
[ 7, 10, 9, 10, 7]], dtype=uint8)]], dtype=object)

create and save a structure containing 3 matrices in matlab:
a=1:5
b.aa=a
b.bb=a
b.cc=a
save(struct.mat,'b')
load .mat file in python
from scipy.io import loadmat
matfile = loadmat('d:/struct.mat')
you can now access for example b.aa and b.bb via:
matfile[('b')][0][0][0]
matfile[('b')][0][0][1]
Is that what you wanted?

Related

Numpy: How to subtract every other element in array

I have the following numpy array
u = np.array([a1,b1,a2,b2...,an,bn])
where I would like to subtract the a and b elements from each other and end up with a numpy array:
u_result = np.array([(a2-a1),(b2-b1),(a3-a2),(b3-b2),....,(an-a_(n-1)),(an-a_(n-1))])
How can I do this without too much array splitting and for loops? I'm using this in a larger loop so ideally, I would like to do this efficiently (and learn something new)
(I hope the indexing of the resulting array is clear)
Or simply, perform a substraction :
u = np.array([3, 2, 5, 3, 7, 8, 12, 28])
u[2:] - u[:-2]
Output:
array([ 2, 1, 2, 5, 5, 20])
you can use ravel torearrange as your original vector.
Short answer:
u_r = np.ravel([np.diff(u[::2]),
np.diff(u[1::2])], 'F')
Here a long and moore detailed explanation:
separate a from b in u this can be achieved indexing
differentiate a and b you can use np.diff for easiness of code.
ravel again the differentiated values.
#------- Create u---------------
import numpy as np
a_aux = np.array([50,49,47,43,39,34,28])
b_aux = np.array([1,2,3,4,5,6,7])
u = np.ravel([a_aux,b_aux],'F')
print(u)
#-------------------------------
#1)
# get a as elements with index 0, 2, 4 ....
a = u[::2]
b = u[1::2] #get b as 1,3,5,....
#2)
#differentiate
ad = np.diff(a)
bd = np.diff(b)
#3)
#ravel putting one of everyone
u_result = np.ravel([ad,bd],'F')
print(u_result)
You can try in this way. Firstly, split all a and b elements using array[::2], array[1::2]. Finally, subtract from b to a (np.array(array[1::2] - array[::2])).
import numpy as np
array = np.array([7,8,9,6,5,2])
u_result = np.array(array[1::2] - array[::2] )
print(u_result)
Looks like you need to use np.roll:
shift = 2
u = np.array([1, 11, 2, 12, 3, 13, 4, 14])
shifted_u = np.roll(u, -shift)
(shifted_u - u)[:-shift]
Returns:
array([1, 1, 1, 1, 1, 1])

Replace entry in specific numpy array stored in dictionary

I have a dictionary containing a variable number of numpy arrays (all same length), each array is stored in its respective key.
For each index I want to replace the value in one of the arrays by a newly calculated value. (This is a very simplyfied version what I'm actually doing.)
The problem is that when I try this as shown below, the value at the current index of every array in the dictionary is replaced, not just the one I specify.
Sorry if the formatting of the example code is confusing, it's my first question here (Don't quite get how to show the line example_dict["key1"][idx] = idx+10 properly indented in the next line of the for loop...).
>>> import numpy as np
>>> example_dict = dict.fromkeys(["key1", "key2"], np.array(range(10)))
>>> example_dict["key1"]
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> example_dict["key2"]
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> for idx in range(10):
example_dict["key1"][idx] = idx+10
>>> example_dict["key1"]
array([10, 11, 12, 13, 14, 15, 16, 17, 18, 19])
>>> example_dict["key2"]
array([10, 11, 12, 13, 14, 15, 16, 17, 18, 19])
I expected the loop to only access the array in example_dict["key1"], but somehow the same operation is applied to the array stored in example_dict["key2"] as well.
>>> hex(id(example_dict["key1"]))
'0x26a543ea990'
>>> hex(id(example_dict["key2"]))
'0x26a543ea990'
example_dict["key1"] and example_dict["key2"] are pointing at the same address. To fix this, you can use a dict comprehension.
import numpy
keys = ["key1", "key2"]
example_dict = {key: numpy.array(range(10)) for key in keys}

Save list values in csv file with Python

I have a Python simple list like this one:
a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
I want to save it in a csv file that looks like this:
1,2,3,4,5,6,7,8
9,10,11,12,13,14,15,16
How can I do that? I tried:
np.savetxt('fname.csv', bbox_form, fmt='%d')
But I don't know how to write a new line only after 8 values.
col_num=8
row_num=len(a)/col_num
b=np.reshape(a, [row_num,col_num])
d=[','.join(map(str,c)) for c in b]
np.savetxt('fname.csv', d, fmt='%s')
This should work
Numpy will save each "row" in your array to a line. If you want to save it to multiple lines, then you'll need to reshape your array.
Have a look here:
https://docs.scipy.org/doc/numpy/reference/generated/numpy.reshape.html

How to print numpy objects without line breaks

I am logging input arguments to a function using
logging.debug('Input to this function = %s',
inspect.getargvalues(inspect.currentframe())[3])
But I do not want the line breaks inserted within numpy objects. numpy.set_printoptions(linewidth=np.nan) removes some, but line breaks are still inserted in 2D objects such as
array([[ 0.84148239, 0.71467895, 0.00946744, 0.3471317 ],
[ 0.68041249, 0.20310698, 0.89486761, 0.97799646],
[ 0.22328803, 0.32401271, 0.96479887, 0.43404245]])
I want it to be like this:
array([[ 0.84148239, 0.71467895, 0.00946744, 0.3471317 ], [ 0.68041249, 0.20310698, 0.89486761, 0.97799646], [ 0.22328803, 0.32401271, 0.96479887, 0.43404245]])
How can I do this? Thanks.
Given an array x, you can print it without line breaks with,
import numpy as np
x_str = np.array_repr(x).replace('\n', '')
print(x_str)
or alternatively using the function np.array2string instead of np.array_repr.
I'm not sure if there is an easy way to remove newlines from the string representation or numpy arrays. However, it is always possible to remove them after the conversion took place,
input_args = inspect.getargvalues(inspect.currentframe())[3]
logging.debug('Input to this function = %s', repr(input_args).replace('\n', ''))
import numpy as np
np.set_printoptions(threshold=np.inf)
np.set_printoptions(linewidth=np.inf)
# Testing:
big_arr = np.ones([30,70])
print(big_arr)
Simple solution
import numpy as np
value = np.array([[1, 2, 3, 4, 5, 6, 7, 8], [1, 2, 3, 4, 5, 6, 7, 8], [1, 2, 3, 7, 8]])
value_str = str(value).replace('\n', '')
print("prints: " + value_str)
# prints: [[1, 2, 3, 4, 5, 6, 7, 8], [1, 2, 3, 4, 5, 6, 7, 8], [1, 2, 3, 7, 8]]
This solution also works:
myArray = np.array([[ 0.84148239, 0.71467895, 0.00946744, 0.3471317 ],
[ 0.68041249, 0.20310698, 0.89486761, 0.97799646],
[ 0.22328803, 0.32401271, 0.96479887, 0.43404245]])
print(np.array2string(myArray).replace('\n','').replace(' ',', '))
Output:
[[0.84148239, 0.71467895, 0.00946744, 0.3471317, ], [0.68041249, 0.20310698, 0.89486761, 0.97799646], [0.22328803, 0.32401271, 0.96479887, 0.43404245]]

Python dictionary hold array ranges?

I am tryng to make a dict that could hold some array sniplets
like [127:130, 122:124] but dict = {1:[127:130, 122:124], 2:[127:129, 122:123]} doesn't work.
Is there a way to do this? It doesn't need to be dicts, but I want a bunch of these areas to be callable.
So I have 256x256 arrays and I want to select small areas in them for some calculations:
fft[127:130, 122:124]
Would be great if the whole part between brackets could be in a dict
You could use the slice function. It returns a slice object that can be stored in a dictionary. eg:
slice_1 = slice(127, 130)
slice_2 = slice(122, 124)
slice_a = slice(127, 129)
slice_b = slice(122, 123)
d = {1:[slice_1, slice_2],
2:[slice_a, slice_b]
}
x = fft[d[1]] # Same as fft[127:130, 122:124]
y = fft[d[2]] # Same as fft[127:129, 122:123]
Slicing numpy arrays returns a view, and not a copy, maybe this is what you are looking for?
import numpy
a = numpy.arange(10)
b = a[3:6] # array([3, 4, 5])
a[4] = 0
#b is now array([ 3, 0, 5])
b[1] = 1
#a is now array([0, 1, 2, 3, 1, 5, 6, 7, 8, 9])

Categories