Coder Social home page Coder Social logo

awesomebytes / libermate Goto Github PK

View Code? Open in Web Editor NEW
175.0 22.0 84.0 169 KB

DEPRECATED use https://github.com/victorlei/smop instead LiberMate - A MATLAB to Python (SciPy/NumPy) Translator

License: GNU General Public License v3.0

Python 88.66% M 0.01% MATLAB 10.95% CLIPS 0.37%

libermate's Introduction

DEPRECATED

Use https://github.com/victorlei/smop instead.

libermate

LiberMate - A MATLAB to Python (SciPy/NumPy) Translator

All credit goes to Eric C. Schug for this incredible work.

Check notes_on_using_libermate.txt if you use it and it doesn't work properly (it's normal!) I give many hints there on how to correct problems I had while translating https://github.com/awesomebytes/parametric_modeling

You may want to try the new: https://github.com/victorlei/smop It's a work in progress, which means, someone is working on it! This one is just as it is.

=========

(Almost) Original README from http://sourceforge.net/p/libermate/code/HEAD/tree/

=== LiberMate ===

LiberMate - A MATLAB to Python (SciPy/NumPy) Translator

git clone https://github.com/awesomebytes/libermate.git

===Setup===

Libermate needs pyclips http://pyclips.sourceforge.net/web/ --> http://sourceforge.net/projects/pyclips/files/pyclips/pyclips-1.0/

Download pyclips-1.0.7.348.tar.gz

Extract and install (there is a README if you have doubts)

tar -zxvf pyclips-1.0.7.348.tar.gz
cd pyclibs
sudo python setup.py build
sudo python setup.py install

===Run===

Change directory to the libermate directorie e.g.

$ cd libermate

To get command line help use

$ python libermate.py -h

To convert a single test file type

$ python libermate.py Tests/colon.m

and will print the following output

Opening File Tests/colon.m
Starting Parser
Parser Complete
Starting Translator
Translation Complete
writing to file Tests/colon.py

To convert all test files type

$ python libermate.py Tests/*.m

To output Abstract Syntax Tree from colon.m

$ python libermate.py --astdump Tests/colon.m

Opening File Tests/colon.m
Starting Parser
Parser Complete
writing to file Tests/colon.ast
Starting Translator
Translation Complete
writing to file Tests/colon.py

This will create a file called Tests/colon.ast which contains the AST for the parsed file. The AST file can be useful to track where the Parser may have had problems.

LiberMate will attempt to convert MATLAB source files to python. The MATLAB files must end with .m and the translated python code will be written to python files, of the same name but ending with .py.

===Known Issues===

  • LiberMate does not handle command style MATLAB expressions such as

    grid on

    instead use function expressions such as

    grid('on')

  • LiberMate does not map all MATLAB functions to SciPy/NumPy equivalents. It does map several common functions.

  • LiberMate does not completely support freeform matrix expressions (e.g.

$ ./libermate.py Tests/matrix_tests.m

Opening File Tests/matrix_tests.m
Starting Parser
syntax error: unexpected symbol at line 10 (column 4): "
 "
syntax error: unexpected symbol at line 11 (column 3): " "
Parser Complete
Starting Translator
error: unexpected end of subtree
Translation Complete
writing to file Tests/matrix_tests.py

The output shows two errors on lines 10 and 11 that are cause by an initial newline and final newline in matrix expression for variable d.

  • numpy does not automatically grow arrays
  • python scalars behave differently from matlab arrays
  • plot title,ylabel do not take extra arguments
  • missing waitbar function

TODO

  • replace a.copy() with copy(a)

libermate's People

Contributors

awesomebytes avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

libermate's Issues

PyPA recommendations

In order for your package to be useful for others you should really refactor your project structure to be in line with the PyPA recommendations so it can be installed using pip:

  • Put everything in a libermate subdirectory
  • Create a setup.py file that declares all dependencies.
  • Gitignore pyc files

.m script matlab file is not converting to .py successfully.

My .m matlab script file is not able to convert successfully.
My .m matlab script file is give below ๐Ÿ‘Ž

%%% Plotting output obtained from East Coast UNSWAN operational model.
%% Plotting magnitude of Hs with peak wave direction
clear all;clc;
xyzfile = 'PUNSWANIObathy.xyz';
dx = 0.7 ; % Distance between two consecutive wave direction arrows
xmin = 76;           % Boundary coordinates of basemap
xmax = 90;
ymin = 4;
ymax = 24;
cmax = 7;
%% Writing .mat output on basemap
load('EC_20180321.mat');
% Creating date string from start and end dates
stdate = '21-03-2018 00:00:00';
enddate = '24-03-2018 00:00:00';
stdate_num = datenum(stdate,'dd-mm-yyyy HH:MM:SS');
enddate_num = datenum(enddate,'dd-mm-yyyy HH:MM:SS');
basename = 'fort';                              % basename of element file
param = 'Hsig'; % parameter name should be same as that in the .mat file
hr_step=datenum(0,0,0,3,0,0); % Time interval(in hours)
date_array=stdate_num:hr_step:enddate_num; 
x=datestr(date_array,'dd-mm-yyyy HH:MM:SS'); % display in date format
siz_x = size(x);
siz_x = siz_x(1,1);
%% Importing element information
elefile=[basename '.ele'];
fid = fopen(elefile);                    % load TRIANGLE element based connectivity file
[nelem] = fscanf(fid,'%i',[1 3]);        % get number of triangles
ncol = 4+nelem(3);                       % specify number of columns in elefile
tri = fscanf(fid,'%i',[ncol nelem(1)])'; % get connectivity table
fclose(fid);
for i = 1%:siz_x
    date = x(i,:);
    day = date(1:2);
    month = date(4:5);
    year = date(7:10);
    hr = date(12:13);
    min = date(15:16);
    sec  = date(18:19);
    varname = strcat(param,'_',year,month,day,'_',hr,min,sec);
    z = eval([varname]);
    openfig('basemapstates_25.fig');
    % Converting time from GMT to IST
    t = datenum(date,'dd-mm-yyyy HH:MM:SS');
    t = addtodate(t,330,'minute');
    date_IST = datestr(t,'dd-mmm-yyyy HH:MM:SS');
    date_IST1 = date_IST(1:11);
    time_IST = date_IST(13:17);
    ax1_a = gca ;
    xtik = xmin:1:xmax;
    ytik = ymin:1:ymax;
    set(ax1_a,'Position',[0.2 0.15 0.72 0.72],'Xtick',xtik,'Ytick',ytik,'TickDir','out','FontName','Calibri','FontWeight','bold','Fontsize',8,'YAxisLocation','left','XAxisLocation','bottom');
    degreetick 'x'
    degreetick 'y'
    ax1_b = copyobj(gca,gcf);
    set(ax1_b,'YAxisLocation','right','XAxisLocation','top');
    degreetick 'x'
    degreetick 'y'
    linkaxes([ax1_a,ax1_b]);

    ax1 = gca;
    xlim([76 90]);
    ylim([4 24]);
    ax2 = axes ;
    %% Setting axes properties
    set(ax2,'ActivePositionProperty','outerposition','BusyAction','queue','Clipping','on','Color','none','DataAspectRatio',[1 1 1],'DataAspectRatioMode','manual','FontAngle','normal','FontName','Helvetica','FontSize',10,'FontUnits','points','FontWeight','normal','HandleVisibility','on','HitTest','on','Interruptible','on','Layer','bottom','LineStyleOrder','-','LineWidth',0.5000,'NextPlot','add','OuterPosition',[0 0 1 1],'PlotBoxAspectRatio',[1 1.3437 1.2500],'PlotBoxAspectRatioMode','auto','Position',[0.1300 0.1100 0.7750 0.8150],'Projection','orthographic','PlotBoxAspectRatio',[1 1.3437 1.2500],'PlotBoxAspectRatioMode','auto','Position',[0.1300 0.1100 0.7750 0.8150],'Projection','orthographic','Selected','off','SelectionHighlight','on','SortMethod','depth','Tag','','TickDir','in','TickDirMode','auto','TickLength',[0.0100 0.0250],'UIContextMenu',[],'Units','normalized','UserData',[],'View',[0 90],'Visible','off','XAxisLocation','bottom','XColor',[0.1500 0.1500 0.1500],'XDir','normal','XGrid','on','XLimMode','manual','XMinorGrid','off','XScale','linear','XTick',[],'YAxisLocation','left','YColor',[0.1500 0.1500 0.1500],'YDir','normal','YGrid','on','YMinorGrid','off','YMinorTick','off','YScale','linear','YTick',[],'YTickLabelMode','auto','ZColor',[0.1500 0.1500 0.1500],'ZDir','normal','ZDir','normal','ZGrid','on','ZMinorGrid','off','ZMinorTick','off','ZScale','linear');
    trisurf(tri(:,2:4),Xp,Yp,z,'EdgeColor','none');
    caxis([0 cmax]);    
    shading interp;
    cmap1 = jet;
    cmap = [cmap1(10,:);cmap1(15,:);cmap1(27,:);cmap1(35,:);cmap1(39,:);cmap1(43,:);cmap1(47,:);cmap1(51,:);cmap1(55,:);cmap1(57,:);cmap1(59,:);cmap1(62,:);cmap1(63,:);cmap1(64,:)];
    %cmap = cmap(7:20,:)
    view(0,90); 
    daspect([1 1 1])
    colormap(cmap);
    linkaxes([ax1,ax2])
    % Setting colorbar properties
    hcb = colorbar;
    ctick_vect = 0:0.5:cmax;
    set(hcb,'location','southoutside','Xtick',ctick_vect,'FontName','Calibri','FontWeight','bold','Fontsize',8);
    cpos = get(hcb,'Position');
    cpos(4) = 0.3*cpos(4);
    cpos(2) = 0.75*cpos(2);
    cpos(3) = 1*cpos(3);
    cpos(1) = 1.13*cpos(1);
    set(hcb,'Position',cpos);  
    set(gcf,'units','centimeters','outerposition',[0 0 30 30]);
    hold on
        
    %% Plotting directions
    var_dirname = strcat('PkDir','_',year,month,day,'_',hr,min,sec);
    d = eval([var_dirname]);
    d1 = degunitcircle2degN(d);
    u = arrayfun(@cosd,d1);
    v = arrayfun(@sind,d1);
    w = zeros(size(u));
    % Making structured from unstructured dataset to get the uniform density
    % of arrows everywhere
    [X,Y,Z,U,V,W] = unstrc2strcbathy(Xp,Yp,z,u,v,dx,xyzfile);
    idx = ~isnan(U) & ~isnan(V);
    Z = cmax*ones(size(U));
    arr = quiver3(X(idx),Y(idx),Z(idx),U(idx),V(idx),W(idx),'color','k','MaxHeadSize',0.8,'AutoScale','on','AutoScaleFactor',0.45,'LineWidth',1.3);
    ax3 = gca;
    set(ax3,'Position',[0.2 0.15 0.72 0.72]);
    title2 = ['Experimental Forecast for',' ',time_IST,' ','IST',' ',date_IST1];    

    annotation(gcf,'textbox',[0.41 0.895 0.3 0.0565552686204641],...
    'String','Significant Wave Height (m)',...
    'HorizontalAlignment','center','FontWeight','bold','FontName','Calibri','FontSize',11,...
    'LineStyle','none');
    annotation(gcf,'textbox',[0.26 0.87 0.6 0.0565552686204641],...
    'String',title2,...
    'HorizontalAlignment','center','FontWeight','bold','FontName','Calibri','FontSize',10,...
    'LineStyle','none','color',[0 0.5 0]);
    % Create textbox
    annotation(gcf,'textbox',[0.31 0.029 0.5 0.0565552686204641],...
    'String','Colour scale indicates magnitude of Hs',...
    'HorizontalAlignment','center','FontWeight','bold','FontName','Calibri',...
    'FontSize',9,'LineStyle','none');
    annotation(gcf,'textbox',[0.31 0.013 0.5 0.0565552686204641],...
    'String','Direction of arrows represent peak wave direction',...
    'HorizontalAlignment','center','FontWeight','bold','FontName','Calibri',...
    'FontSize',9,'LineStyle','none');
    num = num2str(i);
    image_name = strcat(param,'-',day,'-',month,'-',year,'--',hr,min);
    drawnow
    export_fig(image_name,'-jpg','-r200');
    hold off
    close all
end

can you able to convert this .m file to .py file .
kindly reply or tell me how to convert this .m script file to .py ,.
thanks

Cannot translate the code if it has a function call in it

This if main.m, and it calls another file addTwo.m

main.m

a = 20;
b = 30;

c = addTwo(a,b);
disp(c);

addTwo.m
function addition = addTwo(a,b) addition = a+b;

If I am trying to convert addTwo.m it converts perfectly but when I try to convert main.m, it fails
The out put it generated is :

main.py

import numpy as np
import scipy
import matcompat

try:
     import matplotlib.pylab as plt
except ImportError:
     pass

am I doing something wrong? because I have seen that in your Test/ folder the file cheby.m is being called by cheb3plot.m and your code works perfectly fine for this.

syntax error: <AST>:745:1:

Good morning,
I have a regular error in the execution of libermate. When I run from the terminal this error appears for many file "syntax error::...:.." :

Opening File Function:Molecular ion implantation/Main_molecular.m
Starting Parser
syntax error: unexpected symbol at line 426 (column 60): ";"
syntax error: unexpected symbol at line 426 (column 128): "]"
syntax error: :745:1:
Parser Complete
Starting Translator
Translation Complete
writing to file Function:Molecular ion implantation/Main_molecular.py
TimNemo:libermate Tim$ python libermate.py Function:Molecular\ ion\ implantation/Main_molecular.m
<function rule_callback at 0x101be52a8>

My version of python is Python 2.7.15 :: Anaconda, Inc. I searched answers on internet, but I didn't find much.
Could you help me please ? Thanks :D

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.