首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MATLAB脚本要运行在瓶后端:Matlabto.py转换器?

MATLAB脚本要运行在瓶后端:Matlabto.py转换器?
EN

Stack Overflow用户
提问于 2019-05-08 13:29:43
回答 1查看 529关注 0票数 0

我有一个Flask后端,它接收来自前端的xml数据,然后在后端解析这些数据,以提供一些预先提供的MATLAB脚本。我的问题是建筑和功能两个方面:

(1)我想知道如何将多个相互依赖的MATLAB脚本连接到一个烧瓶服务器上,以便这些脚本在发送到服务器的请求时进行操作(无论是matlab和python之间的某种中间件,还是matlab自定义util)。

(2)以及如何动态更改我的主要MATLAB脚本导入它应该分析的文件的目录,因为它是传入的请求,

Stack推荐了各种各样的东西,其中最常见的是SMOP库,用于将MATLAB转换为python,这似乎并不适用于给定复杂性的脚本:大约有5个.m文件值得依赖,而且并不是所有需要的类都被实现了。

我复制的Matlab应用程序编译器也没有"python包“选项。

这里是MATLAB的一个示例,代码:它是带有子目录的包的一个文件(您可以看到从相对路径导入的依赖项)。它接受一个.m5文件作为输入。如何在服务器上实现这样的东西呢?

代码语言:javascript
复制
% calculate the VCG ECG CAD Score form the 12 lead ECG
%
% input path to file
% output size 1 by 1

function [Score] = Calculate_VCG_CAD_Score_From12LeadECGTemplate(TwelveLeadECGFilePath, VCG_transform)

% testing
Testing = 1;

% addpaths
root = 'M:\Some Dingy Ass School in Europe\Datenbank';
addpath([root,'\Software\Matlab']);
addpath([root,'\Software\Matlab\Transforms']);
addpath([root,'\Cleverly Editted Out Projectname\ProgrammCode\Gui']);

% warnings
war1 = 'no file name given';
war2 = 'Undefined transform. Averdson is used';

if nargin < 1
    warning(war1)
    TwelveLeadECGFilePath = [root,'\Cleverly Editted Out Projectname\Daten\ECG\CS200Ex_BaselVIII_2703_3212\MPS_2703_2703_20140228_082204000.xml']
    warning(war2)
    VCG_transform = 'Averdson';
end
if nargin == 1
    warning(war2)
    VCG_transform = 'Averdson';
end
if nargin > 2
    warning('too many inputs will be ignored');
end

% select transform
switch(VCG_transform)
    case {'Averdson'} 
        transform = 'Averdson';
    otherwise
        warning(war2)
        transform = 'Averdson';
end

cebrisDataReader(TwelveLeadECGFilePath);

% read in ECG file (XML, Templates)
% evtl. Funktion machen
%xDoc = xmlread(TwelveLeadECGFilePath);
%xRoot = xDoc.getDocumentElement;
%schema = char(xRoot.getAttribute('xsi:noNamespaceSchemaLocation'))
%allListitems = xDoc.getElementsByTagName('listitem')

% test case
TwelveLeadECGTemplateRest = [1 2 3 4 5 6 7 8 9 10 11 12; 13 14 15 16 17 18 19 20 21 22 23 24; 25 26 27 28 29 30 31 32 33 34 35 36];
TwelveLeadECGTemplateRest = [1 2 3 4 5 6 7 6 5 4 3 2; 
                             2 4 6 8 10 12 14 12 10 8 6 4; 
                             3 6 8 10 12 14 16 14 12 10 8 6;
                             2 4 6 8 10 12 14 12 10 8 6 4; 
                             1 2 3 4 5 6 7 6 5 4 3 2];

% Fall 1 keine CAD
TwelveLeadECGTemplateMaxLoad = TwelveLeadECGTemplateRest;
figure(2);clf
subplot(2,1,1); hold on
plot(TwelveLeadECGTemplateRest(:,1),'b-');
plot(TwelveLeadECGTemplateRest(:,2),'g-');
plot(TwelveLeadECGTemplateRest(:,3),'r-');
subplot(2,1,2); hold on
plot(TwelveLeadECGTemplateMaxLoad(:,1),'b-');
plot(TwelveLeadECGTemplateMaxLoad(:,2),'g-');
plot(TwelveLeadECGTemplateMaxLoad(:,3),'r-');
%figure(3);clf
%plot3(TwelveLeadECGTemplateRest(1,:),TwelveLeadECGTemplateRest(2,:),TwelveLeadECGTemplateRest(3,:),'b-');
%hold on
%plot3(TwelveLeadECGTemplateMaxLoad(1,:),TwelveLeadECGTemplateMaxLoad(2,:),TwelveLeadECGTemplateMaxLoad(3,:),'r-');

% calculate VCG from 12 leadd ECG
if Testing
    TwelveLeadECGTemplateRest = [1 1 1 1 1 1 1 1 1 1 1 1; 
                                 2 2 2 2 2 2 2 2 2 2 2 2; 
                                 3 3 3 3 3 3 3 3 3 3 3 3;
                                 4 4 4 4 4 4 4 4 4 4 4 4; 
                                 5 5 5 5 5 5 5 5 5 5 5 5];
end
[VCG_rest] = TwelveLeadECGToVCG(TwelveLeadECGTemplateRest, transform);
[VCG_MaxLoad] = TwelveLeadECGToVCG(TwelveLeadECGTemplateMaxLoad, transform);
if Testing
    VCG_rest
end

% calculate area from 
if Testing
    VCG_rest = [0 0 1 2 3 2 1;
                0 1 2 2 0 2 -1;
                0 1 2 3 4 2 1];
    VCG_rest = VCG_rest'
    VCG_MaxLoad = [0 0.5 1 2 3 2 1;
                   0 0.5 1 2 0 2 -1;
                   0 0.5 1 3 4 2 1];
    VCG_MaxLoad = VCG_MaxLoad'
end
VCGAreaRest = CalculateVCGScore(VCG_rest);
VCGAreaMaxLoad = CalculateVCGScore(VCG_MaxLoad);
if Testing
    VCGAreaRest
    VCGAreaMaxLoad
end

figure(4);clf
subplot(2,1,1)
hold on
plot(VCGAreaRest,'b-');
plot(VCGAreaMaxLoad,'r-');
subplot(2,1,2)
plot(VCGAreaRest-VCGAreaMaxLoad,'k-');

figure(5);clf
plot3(VCG_rest(1,:),VCG_rest(2,:),VCG_rest(3,:),'b-');
hold on
plot3(VCG_MaxLoad(1,:),VCG_MaxLoad(2,:),VCG_MaxLoad(3,:),'r-');

end

% locally defined functions

function [VCGarea] = CalculateVCGScore(VCGTemplate)

    LengthOfTemplate = size(VCGTemplate,1);
    area = 0;
    VCGarea = [];
    for n = 2: LengthOfTemplate
        % cumulative
        area = area + 0.5*norm(cross(VCGTemplate(n,:), VCGTemplate(n-1,:))); 
        % partly areas
        area = 0.5*norm(cross(VCGTemplate(n,:), VCGTemplate(n-1,:))); 

        % store
        VCGarea(n-1,:) = area;
    end
    % close loop (first and last element should be null as (0,0,0) for n=0
    VCGarea(LengthOfTemplate,:) = norm(cross(VCGTemplate(LengthOfTemplate,:), VCGTemplate(1,:)));
end

后端服务器是标准的RESTful,我想它在这一点上可以说是一个标准的API,但是我只是在上周才了解到这个概念,所以我不确定。光秃秃的骨头现在看起来就像这样:

代码语言:javascript
复制
from flask_restful import Resource, Api
from flask_cors import CORS
from flask import jsonify
import requests, os, json, xmltodict
app = Flask(__name__)
api = Api(app)
CORS(app)
#VCG scripts are under Software/Matlab/Transfroms

@app.route('/api/upload', methods = ['POST'])
def upload_file():
    file = request.files['file']    
    contents = xmltodict.parse(file)
    # xmltodict is XML to JSON parser
    print(">> The XML contents. <<<\n")
    return jsonify(contents)

if __name__ == '__main__':
    app.run(debug=False)

# ----> Matlab to Python Scripts
# ---->Upload File parser

我很高兴听到任何和所有的建议!提前谢谢。

EN

回答 1

Stack Overflow用户

发布于 2019-05-08 15:04:37

为什么不直接在MATLAB中处理XML数据呢?xml2struct是一种将XML文件转换为MATLAB结构的令人敬畏的/快速的MEX函数。我在自己的机器上安装了一个在文件交换中的非MEX版本,运行速度比C++ MEX慢130倍。

或者,如果我读错了,而您需要在MATLAB中读取JSON而不是XML (当您的过程读取XML并输出JSON时,我阅读了您的文章),尝试jsonlab。它的输出有点不同(单元格/数组而不是struct),但仍然很容易使用。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56041989

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档