I am trying to run from Python a script in Matlab that run a Simulink mode, save a variable as Power.mat and read this variable in Python. I am using Python 2.7 on Windows.
I've tried to use the library hdf5storage to read the file:
import hdf5storage
x=hdf5storage.loadmat('Power.mat','r')
but I get the error attached.
Which could be the problem?
I have also tried with the library h5py but I get the same error.
The file .mat seems not to be corrupted since I open it without any problem in Matlab.
Thanks!
You can use scipy.io to exchange data between Python and Matlab. There are functions named savemat and loadmat for this purpose.
Something like this should work:
import scipy.io
mat = scipy.io.loadmat('Power.mat')
For reference, http://docs.scipy.org/doc/scipy/reference/generated/scipy.io.loadmat.html
Try this code :
import h5py
Data = h5py.File('File.mat')
Related
I am trying to import a 3D file created by TexGen in python. I would like to have a dask array at the end for further analysis. My file is in ".igs" format. I can open it with FreeCAD and export it in a variety of formats. I just don't know which format to use and which library in python is more helpful.
I found this question (Import CAD object in Python and store as array) and tried installing FreeCAD package in python but I was unsuccessful. I get a conflict error (incompatible packages) which I can't solve.
I found this question (3D CAD to OpenGL) and installed PyOpenGL but I don't know how to use OpenGL to import STL file.
Any suggestions?
You can have a look at the opencascade python module
OpenCascade is the library FreeCAD is based on, it can also import/export .stp (and I guess .igs) files and transform it into a mesh.
I often use pickle files to store my dataset. Currently I encounter a trouble. In my local desktop, the python version is python 3.8. However, In the remote server, the python version is python 3.7. I need to work in the remote server. So
I want to know how to read the pickle files created by the python 3.8?
import pandas as pd
df=pd.read_pickle('FUND_AREACLASS.pkl')
The report error is here:
File "C:\ProgramData\Anaconda3_new\lib\site-packages\pandas\io\pickle.py", line 181, in read_pickle
return pickle.load(f)
ValueError: unsupported pickle protocol: 5
Can anybody help me figure it out? Thanks in advance.
I'm new to python and I'm having an issue importing a module that imports numpy,PIL and os packages. I'll try and be as clear as possible with my problem
So I have a module lets call it preprocessing.py in which I've written a class to process an image imported from PIL using Image and converting it to a numpy array so the structure looks like the following (note method1 converts a jpg to numpy array)
----- preprocessing.py
import numpy as np
import os
from PIL import Image
Class process_object:
method1
Now I want to use this module as follows I want to import process_object from preprocessing.py and use method1 to process an image again imported using Image in PIL. So my script computation.py looks like the following
---computation.py
import os
import numpy as np
from PIL import Image
a = process_image(input)
a.method1()
However, when I do this I get the following error message
ImportError: No module named numpy
Could someone explain to me what is going on and how to fix it? I'd really appreciate an explanation which allows me to understand what is going on under the hood, so I can avoid situations like this. I really appreciate any help! Thanks!!
Check in which version of Python pip is installing numpy. It could be that when pip installs it, it's pointing to a different Python version on your system.
For problems like these, I would recommend using:
https://github.com/pyenv/pyenv-virtualenv
Will handle Python versions for you, so that you can differentiate which packages are being installed.
I will also recommend using PyCharm's Community Edition.
https://www.jetbrains.com/pycharm/download
Excellent tool and lets you create your own environment.
Hope this helps.
https://sourceforge.net/projects/numpy/files//NumPy/1.5.0/NOTES.txt/view. This is the support for numpy in Python 3.0. You probably need a newer version of numpy. You can also use:
pip install numpy
or
pip3 install numpy
I'm trying to build a python script that is suppose to feed into another Matlab program. the script uses (among other things) numpy and pandas.
Here's the matlab code when I try to load the script:
path='C:\XXXXXX\Local\Continuum\anaconda3\python.exe';
pyversion(path)
algo=py.importlib.import_module('Algo_Pres');
When I try to load the script into matlab, I get an import error that seems to originate from python:
I understand the error as: pandas is missing a numpy dependency.
And yet when I turn back to python and run the script in python it works smoothly...
Where do you think the problem comes from?
PS: I checked my Library using conda list in the Prompt.
For some reason numpy is listed in the anaconda channel, whereas anything else is listed without any channel. Do you think it could be linked?
Problem
I have a file created with h5py in python 2.7.
These steps lead to a corruption:
I download a fresh copy of it from a collaborator using scp. It is whole and 286MB.
I check that it is readable by opening it with hdfview. This shows all the datasets and groups properly.
I exit hdfview.
Repeat steps 2 and 3 to ensure hdfview is not corrupting the file.
I open ipython 3.6 and,
import h5py
f = h5py.File(filename,'r')
g = f['/sol000']#one group that should be there
I get KeyError: "Unable to open object (Object 'sol000' doesn't exist)"
I f.close() and exit ipython. I again open it with hdfview and the entire structure is gone. The file is now 4KB.
I am able to open the file in python 2 hdf5 and access all the datasets, but must use python 3 for my code.
Systems
File created on Fedora 24 64-bit, python 2.7, hdf5 2.7.0
System trying to read it on Fedora 25 64-bit python 3.6, h5py 2.7.0
Minimal code showing should work
On system 1:
import h5py
import numpy as np
f = h5py.File("file.hdf5","w")
f.create_dataset("/sol000/data",(100,100),dtype=float)
f["/sol000/data"] = np.zeros([100,100],dtype=float)
f.close()
On system 2: Do steps 1-4.
import h5py
f = h5py.File("file.hdf5","r")
f.visit(lambda *x:print(x))
#(sol000/data,)
f.close()
The solution was to enforce libver=earliest. I.e. the following code worked to open the file:
import h5py
f = f.File("file.hdf5","r",libver="earliest")
I've discovered a possible inconsistency in h5py documentation.
It claims that
The “earliest” option means that HDF5 will make a best effort to be
backwards compatible.
The default is “earliest”.
This can't be true if it only works when I explicitly set it.
My collaborator, it turns out, created the corruptable file with an older version of hdf5 C library.