Reading MATLAB data file (.mat) in python - python-3.x

I have an array of complex numbers in Matlab and I want to import that data in Python. I have tried all methods including Scipy module and h5py etc. Can anyone tell me any other possible way?
My Matlab version is 2017b. and python version is 2.7.

In MATLAB, save your data with the '-v7' option:
myMat = complex(rand(4), rand(4));
save('myfile', 'myMat', '-v7')
In Python, load the .mat file with scipy.io.loadmat. The result is a Python dict:
>>> d = scipy.io.loadmat('myfile.mat')
>>> m = d['myMat']
>>> m[0,0]
'(0.421761282626275+0.27692298496088996j)'
and so on.

Related

Python vs BigQuery FarmHash Sometimes Do Not Equal

In BigQuery when i run
select farm_fingerprint('6823339101') as f
resuls in
-889610237538610470
In Python
#pip install pyfarmhash
import farmhash
print(farmhash.hash64('6823339101'))
results in
17557133836170941146
BigQuery & Python do agree on most inputs, but there are specific ones like the one above where there is a mismatch for the same input
'6823339101'
How can I get bigquery & python to agree 100% of the time?
Links to bigquery & python hash documentation
https://pypi.org/project/pyfarmhash/
https://cloud.google.com/bigquery/docs/reference/standard-sql/hash_functions
As mentioned in the comments, the function is returning an unsigned int.
So we need to convert that as follows;
import numpy as np
np.uint64(farmhash.fingerprint64(x)).astype('int64')
Relevant issues: https://github.com/lovell/farmhash/issues/26#issuecomment-524581600
Results:
>>> import farmhash
>>> import numpy as np
>>> np.uint64(farmhash.fingerprint64('6823339101')).astype('int64')
-889610237538610470
Quickly scanning over the documentation that you have linked and pyfarmhash source:
The docs for farm_fingerprint read:
Computes the fingerprint of the STRING or BYTES input using the Fingerprint64 function
But in your python code, you are using the hash64 function, which according to the pyfarmhash source code uses a different function from the farmhash library than fingerprint does
Solution:
Use the same function farm_fingerprint is using
import farmhash
print(farmhash.fingerprint64('6823339101'))

Calculating Log base c in python3

Is there a way to calculate log base c in python?
c is a variable and may change due to some dependencies.
I am new to programming and also python3.
There is already a built in function in the math module in python that does this.
from math import log
def logOc(c, num):
return log(num,c)
print(log(3,3**24))
You can read more about log and the python math module here
Yes, you can simply use math's function log():
import math
c = 100
val = math.log(10000,c) #Where the first value is the number and the second the base.
print(val)
Example:
print(val)
2.0

python 3 numpy save multiple arrays

I have 3 arrays and one list:
array1.shape = (1000,5,5,7)
array2.shape = (1000,)
array3.shape = (1000,)
len(list1) = (1000)
I want to save all of these to a numpy file. When I used to run in Python 2.7, the command I used to use was:
np.save(filename,[array1, array2, array3, list1])
And everything worked great, including loading all of the data with np.load. However, when I try this command in Python 3.6 I get an error:
could not broadcast input array from shape (1000,5,5,7) into shape (1000)
How am I able to save the 3 arrays as well as the list into a single numpy array in Python 3.6?

Iterate through each integer python separated by a blankspace/endline

I am a Python 2.7 user who recently switched to python3. While reading integers separated by a blackspace/endline I used nex = iter(map(int,stdin.read().split())).next, where nex() acts as a function to input integers (Suppose for inputting an integral value in x -> x=nex(). But in python3 this doesn't seem to work. Someone please propose a workaround for using the same in Python3.
.next() method is called .__next__() in Python 3. You could use next() function to write single-source Python 2/3 compatible code:
from functools import partial
nex = partial(next, iter(iterable))
print(nex())

RPy2 Convert Dataframe to SpatialGridDataFrame

how can a Dataframe be converted to a SpatialGridDataFrame using the R maptools library? I am new to Rpy2, so this might be a very basic question.
The R Code is:
coordinates(dataf)=~X+Y
In Python:
import rpy2
import rpy2.robjects as robjects
r = robjects.r
# Create a Test Dataframe
d = {'TEST': robjects.IntVector((221,412,332)), 'X': robjects.IntVector(('25', '31', '44')), 'Y': robjects.IntVector(('25', '35', '14'))}
dataf = robjects.r['data.frame'](**d)
r.library('maptools')
# Then i could not manage to write the above mentioned R-Code using the Rpy2 documentation
Apart this particular question i would be pleased to get some feedback on a more general idea: My final goal would be to make regression-kriging with spatial data using the gstat library. The R-script is working fine, but i would like to call my Script from Python/Arcgis. What do you think about this task, is this possible via rpy2?
Thanks a lot!
Richard
In some cases, Rpy2 is still unable to dynamically (and automagically) generate smart bindings.
An analysis of the R code will help:
coordinates(dataf)=~X+Y
This can be more explicitly written as:
dataf <- "coordinates<-"(dataf, formula("~X+Y"))
That last expression makes the Python/rpy2 straigtforward:
from rpy2.robjects.packages import importr
sp = importr('sp') # "coordinates<-()" is there
from rpy2.robjects import baseenv, Formula
maptools_set = baseenv.get('coordinates<-')
dataf = maptools_set(dataf, Formula(' ~ X + Y'))
To be (wisely) explicit about where "coordinates<-" is coming from, use:
maptools_set = getattr(sp, 'coordinates<-')

Resources