Using matlab matrix as input for a python code - python

Is it possible to use a matrix generated with matlab and saved in a binary file as input of a python script?

Of course it's possible. bits are bits, you just need to know how to interpret them :). Fortunately for you, it looks like someone has already done the hard work of figuring out the matlab file format and has written a reader for it ... Have a look at the scipy.io module. Specifically, the loadmat function might be useful.
scipy isn't in the python standard library, but generally speaking, if you're going to be trying to use python to replicate something done in matlab, you'll probably want to have it and it's sibling/child package numpy installed.

Related

Is there a way to pass arbitrary precision numbers between fortran and python?

I am currently writing a program that is heavy on finite differencing. The problem is that to get the accuracy I'd need, I need to use arbitrary precision arithmetic. I have a purely Python script that does the job, but it's rather slow so I'm thinking of passing the task of calculating the finite differences to a compiled Fortran code.
The current implementation I'm working on is that I use SymPy and mpmath to generate multiple precision initial data in Python, save it on some text file that then gets read by F2PY'd Fortran code, who then does most of the work in arbitrary precision. Fortran saves the data in some other file, and Python reads it for some post-processing.
It would be useful to me if I can just freely pass arbitrary precision numbers between Fortran and Python (say, for example, mpath's mpf data type and mpfun's mp_real), instead of going through the trouble of reading and writing multiple precision data. If this is possible with other languages instead, like C, I'd be interested to know also. Or maybe there's a better way to do this?
Look at cython. You can compile with gfortran and gcc, import with an equivalent C header and from there either use cython or the python C api to do just about anything. You can splice fortran into C pretty easily and C into python. There's a very good guide available at http://www.yolinux.com/TUTORIALS/LinuxTutorialMixingFortranAndC.html as well as many questions and answers on the stack about specific items.

Python's equivalent of "EQUIVALENCE" concept in Fortran?

I am interested in rewriting an old Fortran code to Python. The code was for solving any general field variable, call it F (velocity, temperature, pressure, etc). But to solve each variable we have to define EQUIVALENCE of that variable to F.
For example, something like this:
EQUIVALENCE (F(1,1,1),TP(1,1)),(FOLD(1,1,1),TPOLD(1,1))
Is there a Python version of the above concept?
To my knowledge, there is no way to manipulate the memory usage in python.
You can perhaps simply use a list.
F=[]
and
FOLD=[]
When you do
F=FOLD
F and FOLD will point to the same data.
I would suggest to use numpy and scipy to create solvers and use python concepts to make it efficient instead of trying to mimic fortran concepts. Especially very old ones.

Python plyfile vs pymesh

I need to read, manipulate and write PLY files in Python. PLY is a format for storing 3D objects. Through a simple search I've found two relevant libraries, PyMesh and plyfile. Has anyone had any experience with either of them, and does anyone have any recommendations? plyfile seems to have been dormant for a year now, judging by Github.
I know this question instigates opinion-based answers but I don't really know where else to ask this question.
As of (2020 January).
None, use open3d. It's the easiest and reads .ply files directly into numpy.
import numpy as np
import open3d as o3d
# Read .ply file
input_file = "input.ply"
pcd = o3d.io.read_point_cloud(input_file) # Read the point cloud
# Visualize the point cloud within open3d
o3d.visualization.draw_geometries([pcd])
# Convert open3d format to numpy array
# Here, you have the point cloud in numpy format.
point_cloud_in_numpy = np.asarray(pcd.points)
References:
http://www.open3d.org/docs/release/tutorial/Basic/visualization.html
http://www.open3d.org/docs/release/tutorial/Basic/working_with_numpy.html
I have succesfully used plyfile while working with pointclouds.
It's true that the poject had not presented any activity from a long time, but It meets its purpose.
And is not like the fact of parsing a ply file were something that allows you to recreate yourself by adding new features.
On the other hand PyMesh offers you many other features besides parsing ply files.
So maybe the question is:
Do you want to just 'read, manipulate and write PLY files' or are you looking for a library that provides more extra features?
What made me choose plyfile was that I'm able to incorporate it to my project by just copying 1 source file. Also I wasn't interested in any of the other features that PyMesh offers.
Update
I ended writing my own functions to read/write ply files (supporting ascii and binary) because I found the plyfile source code a little messy.
If anyone is interested, here is a link to the file:
ply reader/writer
I've just updated meshio to support PLY as well, next to about 20 other formats. Install with
pip install meshio
and use either on the command line
meshio convert in.ply out.vtk
or from within Python like
import meshio
mesh = meshio.read("in.ply")
# mesh.points, mesh.cells, ...
I rolled my own ascii ply writer (because it's so simple, I didn't want to take a dependency). Later, I was lazy and took a dependency on plyfile for loading binary .ply files coming from other places. Nothing has caught on fire yet.
A thing to mention, for better or worse, the .ply format is extensible. We shoehorned custom data into it, and that was easy since we also wrote our own writer.

Matlab output format that Python can consume

I am pretty new to Matlab but relatively familiar with Python. I am now using an existing Matlab code but I want the the program to generate output that Python can consume. The standard output format, as far as I know is .mat, which is more like a binary format.
Another function I considered is the built-in csvwrite but the problem with that is the variable I want to output is more like a dictionary and it can have several level subfield (e.g., feature.subfeature.subsubfeature = [1, 2, 3]). Another possibility is to output json format but it seems there is no built-in method for output json. There are some toolbox I can use but I don't have sudo permission on the machine that I am using.
Any suggestion on what's a better way to output a format that python can consume? Thanks.
The solution with the least effort on MATLAB side would be to use SciPy, which read/write mat-files:
SciPy
The mat-format is binary, but (surprisingly) open and document by Mathworks.

Are there any matrix math modules compatible with Python 3.x?

When I began this project, I thought it would be easy to get libraries for common stuff like matrix math, so I chose to work in Python 3.1- it being the most recent, updated version of the language. Unfortunately, NumPy is only compatible with 2.5 and 2.6 and seems to be the only game in town! Even other stuff that I turned up like gameobjects seemed to be based on NumPy and therefore incompatible with 3.x as well.
Does anyone out there know of a matrix library that is compatible with 3? I need to be able to do the following: matrix add, subtract, multiply, scalar multiply, inverse, transpose, and determinant. I've been looking all day and all roads seem to lead back to NumPy. I even tried this module: http://www.nightmare.com/squirl/python-ext/misc/matrix.py but it too is for 2.x. Even after converting it using the 2to3 tool, I can't get the yarn module that it refers to (and is probably itself 2.x).
Any help is hugely appreciated.
Given that a large portion of those interested in this sort of development are involved in NumPy, and given their schedule for migrating I think the answer is "no, there is nothing yet".
I would advise treating Python 3.x as "still experimental" and start with Python 2.6 instead. Make some small effort to write your code in a such a way that it won't be too hard to migrate in, say, a year or two, when the Python 3.x series really stabilizes, but don't jump there just yet. Other more general questions have answers that may help you decide.
EDIT: PyEuclid has supports matrices, vectors up to 4 dimensions and is designed for geometric operations.
Otherwise, the answer is probably not what you want, but:
use python 2.x instead, do use numpy (which is really good), until numpy supports python 3.x
implement your own Matrix class, since you don't require too much functionality and it's a good exercise to implement. Should be less work than looking a day on the internet.

Categories