MATLAB .NET assembly (Undefined function or variable 'feedforwardnet') - c#

I'm trying to integrate MATLAB 2010b with Visual Studio 2008 Professional.
I have the following MATLAB method.
function varargout = perform( func, varargin )
%% // Set default values
workspaceDirectory = ['Results/MatlabWorkspace_' datestr(now, 'yyyy-mm-dd_HH-MM-SS')];
clear args
args = struct('workspacePath', [ workspaceDirectory '/workspace.mat'], ...
'testArray', [], ...
'k', 50, ...
'rate', 0.0001, ...
'trainingDataPath', 'P2AT_LaserImageVectorList.csv', ...
'network', 'feedforwardnet', ...
'initialWeights', [], ...
'divideFcn', 'dividerand', ...
'trainRatio', 70/100, ...
'testRatio', 15/100, ...
'valRatio', 15/100, ...
'trainInd', [], ...
'testInd', [], ...
'valInd', [], ...
'trainFcn', 'trainlm', ...
'performFcn', 'mse', ...
'biasConnect', [0; 0; 0], ...
'layerSize', [9; 4; 1], ...
'layerTransferFcn', ['tansig '; 'tansig '; 'purelin'], ...
'max_fail', 10, ...
'mu_dec', 0.04, ...
'useInitialWeights', false, ...
'saveResults', true);
% // Returns a modified properties structure
args = getopt(args,varargin);
args.layerTransferFcn = cellstr(args.layerTransferFcn);
if args.saveResults && ~strcmpi(func,'test')
if (exist(workspaceDirectory, 'dir') ~= 7)
mkdir(workspaceDirectory);
end
end
if (strcmpi(func,'test'))
try
load(args.workspacePath, '-regexp', '.*');
catch err
Warning(err.message);
varargin{1,1} = null;
return;
end
data_inputAngle = args.testArray(2501);
data_inputPCA = args.testArray(1:2500);
if size(data_inputPCA,1) == 1
data_inputPCA = data_inputPCA';
end
switch(featureExtractionMethod)
case {'gha','apex'}
% // [W, errvals] = gha(data_inputPCA, k, varargin{1,3});
data_PCs = W' * data_inputPCA;
data_inputNN = [data_PCs; data_inputAngle];
case 'nnmf'
% // [W,H,D] = nnmf(data_inputPCA',k);
data_PCs = H * data_inputPCA;
data_inputNN = [data_PCs; data_inputAngle];
case 'pcaProcess'
otherwise
warning = 'ERROR: no feature extraction method has been defined.';
Warning('ERROR: no feature extraction method has been defined.');
varargout{1,1} = null;
return;
end
% // Just to test to see if it recognizes 'feedforwardnet'.
testnet = feedforwardnet; % // <------------------------------- LINE 81
% // Saving all the workspace variables to see if they are all correctly processed.
save('all');
varargout{1,1} = net(data_inputNN); %// <------------------------- LINE 86
end
end
And this is how I create my DLL file to import in Visual Studio:
%%// Determine path names
workdir = pwd();
outdir = fullfile(workdir, 'Output');
dnetdir = fullfile(workdir, 'dotnet');
%%// Determine file names
mfile = fullfile(workdir, 'perform.m');
dnetdll = fullfile(dnetdir, 'dotnet.dll');
%%// Build .NET Assembly
eval(['mcc -N -d ''' dnetdir ''' -W ''dotnet:dotnet,' ...
'dotnetclass,0.0,private'' -T link:lib ''' mfile '''']);
So everything works perfectly fine when I use MATLAB Engine's COM interface to run the routine inside MATLAB from C#:
/*
* This function calls the routine inside
* MATLAB using the MATLAB Engine's COM interface
*/
static private float MatlabTestDebug(float[] testData, Double targetAngle)
{
Array X = new double[testData.Length + 1];
testData.CopyTo(X, 0);
X.SetValue((double)targetAngle, testData.Length);
Array zerosX = new double[X.GetLength(0)];
MLApp.MLAppClass matlab = new MLApp.MLAppClass();
matlab.PutFullMatrix("testArray", "base", X, zerosX);
matlab.PutWorkspaceData("workspacePath", "base", "workspace.mat");
// Using Engine Interface, execute the ML command
// contained in quotes.
matlab.Execute("cd 'c:\\Users\\H\\Documents\\Project\\Source Code\\MatlabFiles';");
matlab.Execute("open perform.m");
matlab.Execute("dbstop in perform.m");
matlab.Execute("result = perform('test', 'workspacePath', 'workspace.mat', 'testArray', testArray);");
matlab.Execute("com.mathworks.mlservices.MLEditorServices.closeAll");
return (double)matlab.GetVariable("result", "base");
}
But when I use the .NET assembly, it's not recognizing 'feedforwardnet'. I used to get an error on line 86 (net(data_inputNN)). So I added a line to test to see if it at least recognizes 'feedforwardnet', but it didn't.
Note: I'm loading some variables from a file including "net" which is a neural network (load(args.workspacePath, '-regexp', '.*');)
Also in the MATLAB method if I load a saved "network" from file and then save it to see how it processes the network, it will save it as a "struct" instead of a "network".
I'm assuming it's loading it as a struct to begin with.
I also had this problem inside MATLAB 2009b itself. That's the reason I'm using MATLAB 2010b now, because apparently MATLAB 2009b didn't have this particular neural networks toolbox.
Following is the C# code to use the .NET assembly.
/*
* Calls the method from inside a .NET assembly created with MATLAB
* using Builder for .NET.
*/
private float MatlabTest(float[] testData, Double targetAngle)
{
if (testData != null)
{
dotnetclass AClass = new dotnetclass();
Array X = new double[testData.Length + 1];
testData.CopyTo(X, 0);
X.SetValue((double)targetAngle, testData.Length);
MWNumericArray XNumericArray = new MWNumericArray(X);
MWArray[] RetVal = AClass.perform(1, "test",
"workspacePath", "workspace.mat",
"testArray", XNumericArray);
Array result = ((MWNumericArray)RetVal[0]).ToVector(MWArrayComponent.Real);
return (float)result.GetValue(0);
}
else
{
return 0f;
}
}
I'm getting this error in Visual Studio:
... MWMCR::EvaluateFunction error ...
Undefined function or variable 'feedforwardnet'.
Error in => perform.m at line 81.
NOTE: version of my compiler and softwares:
Compiler: Microsoft Visual C++ 2008 SP1 in C:\Program Files (x86)\Microsoft Visual Studio 9.0
MATLAB: R2010b (64-bit)
Visual Studio: MVS 2008 professional (.NET Framework 3.5 SP1)
Microsoft Windows SDK 6.1
Recent Updates:
I've added the path of the neural network toolbox in mcc.
eval(['mcc -N -p ''C:\Program Files\MATLAB\R2010b\toolbox\nnet'' -d ''' dnetdir ''' -W ''dotnet:dotnet,' ...
'dotnetclass,0.0,private'' -T link:lib -v ''' mfile '''']);
Now I get these messages in mccExcludeFiles.log:
C:\Program Files\MATLAB\R2010b\toolbox\nnet\nnet\#network\network.m
called by C:\Program Files\MATLAB\R2010b\toolbox\nnet\nnet\nnnetwork\cascadeforwardnet.m
(because of toolbox compilability rules)
C:\Program Files\MATLAB\R2010b\toolbox\nnet\nnet\#network\network.m
called by C:\Program Files\MATLAB\R2010b\toolbox\nnet\nnet\nnnetwork\feedforwardnet.m
(because of toolbox compilability rules)

The only answer I could come up with (which not a solution to the problem) was from Creating standalone application that contains neural network toolbox functions, stating that:
You will not be able to compile any
function which trains the network
(like ADAPT). Though the link does not
explicitly list these funcions (like
ADAPT), they fall under the 'All other
command line functionality'.
However, you can deploy a M function
code which uses a pre-trained
network. I believe the SIM function
will deploy fine.
The workflow I see is:
In MATLAB, train you network using test input/output
Save the network (MAT file?)
Create a deployable function which then uses the pretrained network for
new data. The network itself would not
change/adapt/train in this function
Compile and deploy the above function

Related

Jenkins to only build Projects that Changed not whole Solution

We have a large repo that contains many C# projects but we would only like to build the projects where a change was committed.
So if a Change was committed to Project A, then it will only build Project A and not all the projects in the solution.
Is this possible using Jenkins?
Thanks in advance.
Yes, it is possible. But, you'll have to configure all the projects as separate jobs in Jenkins.
We did exactly this in a solution with over 165 projects grouped into 30 individual services and web applications. However instead of building a specific project with msbuild we created individual solutions based on the respective service and web application domain. The result were solutions like JenkinsBuild_Project1.sln and JenkinsBuild_Project2.sln.
https://stackoverflow.com/a/19534376/3850405
The code below can be modified to use msbuild instead with minor changes.
Jenkinsfile:
#!groovy
import groovy.json.*
pipeline {
agent {
node {
label 'msbuild'
}
}
environment {
OCTOPUS_PUBLISH_KEY = credentials('OctoPackPublishApiKey')
//Global variable where we save what to build
BUILD_LIST =''
}
stages {
stage('What to build?') {
steps {
script {
GIT_DIFF = bat (
script: 'git diff --name-status HEAD..HEAD~1',
returnStdout: true
).trim()
echo "From batscript: ${GIT_DIFF}"
//Environment variable values must either be single quoted, double quoted, or function calls and therefore a json string is used to save the array
//https://stackoverflow.com/a/53745878/3850405
BUILD_LIST = new JsonBuilder(whatToBuild("${GIT_DIFF}")).toPrettyString()
echo BUILD_LIST
}
}
}
stage('Prepare') {
steps {
script {
def buildList = new JsonSlurper().parseText(BUILD_LIST)
for (i = 0; i < buildList.size(); i++){
bat "C:\\Program\\NuGet\\nuget_4.4.1.exe restore Source\\${buildList[i]}\\${buildList[i]}.sln"
}
}
}
}
stage('Build') {
steps {
script {
def buildList = new JsonSlurper().parseText(BUILD_LIST)
for (i = 0; i < buildList.size(); i++){
bat "\"${tool 'Build Tools 2019'}\" /t:Clean;Build /p:Configuration=JenkinsBuild JenkinsBuild\\JenkinsBuild_${buildList[i]}.sln"
}
}
}
}
stage('Publish') {
when {
anyOf {
branch 'master';
}
}
steps {
script {
def buildList = new JsonSlurper().parseText(BUILD_LIST)
for (i = 0; i < buildList.size(); i++){
bat """\"${tool 'Build Tools 2019'}\" ^
/t:Build ^
/p:Configuration=JenkinsBuild ^
/p:RunOctoPack=true ^
/p:OctoPackEnforceAddingFiles=true ^
/p:AllowedReferenceRelatedFileExtensions=none ^
/p:OctoPackNuGetProperties=env=${env.BRANCH_NAME};change=${env.CHANGE_ID} ^
/p:OctoPackPackageVersion=2.1.${BUILD_NUMBER}-${env.BRANCH_NAME}1 ^
/p:OctoPackPublishPackageToHttp=https://OurOctopusServer:1337/nuget/packages ^
/p:OctoPackPublishApiKey=${env.OCTOPUS_PUBLISH_KEY} ^
JenkinsBuild\\JenkinsBuild_${buildList[i]}.sln"""
}
}
}
}
}
post {
always {
//Clean and notify
}
}
}
Groovy script that takes a file list from git command git diff --name-status HEAD..HEAD~1 and filters out unique project values like Project1 and Project2 from paths like below.
/Source/Project1/Project1.Data/Properties/AssemblyInfo.cs
/Source/Project2/Project2.Clients.WinService/Services/Project2.Designer.cs
whatToBuild.groovy:
#!/usr/bin/env groovy
def call(String fileList) {
println "Printing filelist in whatToBuild()"
println fileList
def lines = fileList.tokenize('\n')
println lines
def list = []
lines.eachWithIndex{ value, key ->
println "Printing value"
println value
if (value.contains("Source")) {
def result = value =~ /Source\/([A-Z]*)\//
println "Printing result"
println result[0][1]
list.add(result[0][1])
}
}
def listUnique = list as Set
println listUnique
return listUnique
}

When I run a python program using Process.Start the log in my python program does not work?

I'm running a python program from c# using process.start() on a raspberry pi zero w. I've tested it the program is running when called but it will not log to file. But if I just run the program myself python Relays.py 0 0 0 0 0 the python log works. Does anyone know what is causing this problem?
Below are the python code and C# function:
void update()
{
String acommand = status();
Console.WriteLine("LOOKSLIKEWEMADEIT " + acommand);
String location = "/home/pi/Debug/Relays.py " + Convert.ToString(acommand);
//run python
ProcessStartInfo info = new ProcessStartInfo("python", location);
Process.Start(info);
Console.WriteLine("YOUHAVEPASSEd");
}
import RPi.GPIO as G
import time
import sys
import socket
import threading
import logging
G.setwarnings(False)
G.setmode(G.BOARD)
relays = (24,26,32,36,38)
for r in relays:
G.setup(r,G.OUT)
logging.basicConfig(filename='Relaytrigger.log',level=logging.DEBUG,format='%(asctime)s %(message)s$
logging.info('\r\n')
logging.info(sys.argv)
logging.info('\r\n')
#######################heat
if sys.argv[1] == '1':
heater = True
G.output(relays[0],1)
logging.info('heaton\r\n')
else:
heater = False
G.output(relays[0],0)
logging.info('heatoff\r\n')
##########################main
if sys.argv[2] == '1':
mainpump = True
G.output(relays[1],1)
logging.info('mainon\r\n')
else:
mainpump = False
G.output(relays[1],0)
logging.info('mainoff\r\n')
#########################aux
if sys.argv[3] == '1':
auxilarypump = True
G.output(relays[2],1)
logging.info('auxon\r\n')
else:
auxilarypump = False
G.output(relays[2],0)
logging.info('auxoff\r\n')
#########################auxset
if sys.argv[4] == '1':
auxsetting = True
G.output(relays[3],1)
logging.info('mainhigh\r\n')
else:
auxsetting = False
G.output(relays[3],0)
logging.info('heatlow\r\n')
########################light
if sys.argv[5] == '1':
lighting = True
G.output(relays[4],1)
logging.info('lighton\r\n')
else:
lighting = False
G.output(relays[4],0)
logging.info('lightoff\r\n')
I seems to me like problems with your CWD (current working directory).
Depending on from where an python application is started "relative paths" may differ.
Solution #1 : "The python-side solution":
Use absolute paths! Relative paths are more likely to be unsafe.
Solution #2 : "The C#-side solution":
Use the ProcessStartInfo-Property "WorkingDirectory" to define your CWD.
Your CWD is usualy the directory containing your .py file.
Link to ProcessStartInfo-Class
HF

Integrating python code in MVC application in asp.Net

I'm developing an MVC application in asp.net and have a python file 'clientcheck.py' by which the client sends an image/video to the server (also written in Python). My question is, how do I integrate the python code in the MVC application. I'm running Visual Studio 2012.
Also, the python code when running separately works completely fine. I have also installed IronPython.
Would really appreciate the help. I'm new here so please don't mind if my style of question asking is not up to the mark.
Code
import socket,os
import sys
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect(("192.168.1.21", 5050))
k = ' '
size = 1024
while(1):
print ("Do you want to Send a \n1.Image\n2.Video\n")
k = raw_input()
#k=k.encode('utf-8')
client_socket.send(k)
print (k)
k = int (k)
# if(k == 1):
# print ("Enter file name\n")
# data = input()
# data=data.encode('utf-8')
# fp = open(data,'r')
# print ("Opening file - ",data)
# fp = open(data,'r')
# strng = fp.read()
# size = os.path.getsize(data)
# size = str(size)
# client_socket.send(size)
# print ("")
# client_socket.send (strng)
# client_socket.close()
# print ("Data sent successfully")
if (k==1 or k==2):
print ("Enter file name of the image with extentsion (example: filename.jpg,filename.png or if a video file then filename.mpg etc) - ")
fname = raw_input()
fname1 = 'Received_'+fname
fname = """C:/Users/Mehmoo/Desktop/Python/"""+fname
print ("Opening file - ",fname)
#fname1=fname1.encode('utf-8')
client_socket.send(fname1)
img = open(fname,'rb')
while True:
strng = img.read(1024)
print('Sending data...')
if not strng:
break
client_socket.send(strng)
img.close()
print ("Data sent successfully")
exit()

Set Script Task code dynamically in SSIS 2012

In my application, a script task is created dynamically.
In SQL Server 2008's implementation of SSIS, the following method worked fine.
private void SetSourceCode(ScriptTask scriptTask, string code, string codeName)
{
string fileName = "ScriptMain.vb";
string language = "VisualBasic";
string proj = ".vbproj";
scriptTask.ScriptLanguage = VSTAScriptLanguages.GetDisplayName(language);
scriptTask.ScriptingEngine.InitNewScript(language,
scriptTask.ScriptProjectName, proj);
scriptTask.ScriptingEngine.ShowDesigner(false);
scriptTask.ScriptingEngine.AddCodeFile(fileName, code);
if (!scriptTask.ScriptingEngine.Build())
throw new Exception("Failed to build vb script code: " + codeName);
scriptTask.ScriptingEngine.SaveScriptToStorage();
if (!scriptTask.ScriptingEngine.CloseIDE(false))
{
throw new Exception("Unable to close Scripting engine.");
}
}
How do I migrate this code to SQL Server 2012, because following methods are removed from SQL Server 2012 dll-s (assemblies):
InitNewScript
AddProjectReference
AddCodeFile
SaveScriptToStorage
CloseIDE
Build
ShowDesigner
Generally, how do I dynamically set source code for script task in SQL Server 2012?
As you've noticed, the VSTA helper methods you could use in 2008 were moved/removed in 2012. It is still possible to do, but the code has changed.
The easiest thing to do is load an existing project using VstaHelper.LoadProjectFromFolder().
If you want to dynamically add script files, see the snippet below. There are two main things you need to keep in mind:
The ScriptingEngine and VstaHelper classes represent VSTA itself. This is where you’d create the project, and add new files. You cannot remove or replace an existing file directly here. When you call SaveProjecToStorage(), it's like closing the VSTA window … it saves the project and compiled binary to the ScriptTask.
ScriptTask.ScriptStorage allows you to directly manipulate the source file contents. From here, you can modify the content of a file.
The following code snippet should help you get started.
static void Main(string[] args)
{
// 1. Create new package, and add a script task
var pkg = new Package();
var exec = pkg.Executables.Add("STOCK:ScriptTask");
var th = (TaskHost)exec;
th.Name = "Script Task";
th.Description = "This is a Script Task";
var task = (ScriptTask)th.InnerObject;
// 2. Set the script language - "CSharp" or "VisualBasic"
task.ScriptLanguage = VSTAScriptLanguages.GetDisplayName("CSharp");
// 3. Set any variables used by the script
//task.ReadWriteVariables = "User::Var1, User::Var2";
// 4. Create a new project from the template located in the default path
task.ScriptingEngine.VstaHelper.LoadNewProject(task.ProjectTemplatePath, null, "MyScriptProject");
// 5. Initialize the designer project, add a new code file, and build
//task.ScriptingEngine.VstaHelper.Initalize("", true);
//task.ScriptingEngine.VstaHelper.AddFileToProject("XX.cs", "FileContents");
//task.ScriptingEngine.VstaHelper.Build("");
// 6. Persist the VSTA project + binary to the task
if (!task.ScriptingEngine.SaveProjectToStorage())
{
throw new Exception("Save failed");
}
// 7. Use the following code to replace the ScriptMain contents
var contents = File.ReadAllText("path to file");
var scriptFile =
task.ScriptStorage.ScriptFiles["ScriptMain.cs"] =
new VSTAScriptProjectStorage.VSTAScriptFile(VSTAScriptProjectStorage.Encoding.UTF8, contents);
// 8. Reload the script project, build and save
task.ScriptingEngine.LoadProjectFromStorage();
task.ScriptingEngine.VstaHelper.Build("");
// 9. Persist the VSTA project + binary to the task
if (!task.ScriptingEngine.SaveProjectToStorage())
{
throw new Exception("Save failed");
}
// 10. Cleanup
task.ScriptingEngine.DisposeVstaHelper();
// 11. Save
string xml;
pkg.SaveToXML(out xml, null);
File.WriteAllText(#"c:\temp\package.dtsx", xml);
}

How can I programmatically open and save a PowerPoint presentation as HTML/JPEG in C# or Perl?

I am looking for a code snippet that does just this, preferably in C# or even Perl.
I hope this not a big task ;)
The following will open C:\presentation1.ppt and save the slides as C:\Presentation1\slide1.jpg etc.
If you need to get the interop assembly, it is available under 'Tools' in the Office install program, or you can download it from here (office 2003). You should be able to find the links for other versions from there if you have a newer version of office.
using Microsoft.Office.Core;
using PowerPoint = Microsoft.Office.Interop.PowerPoint;
namespace PPInterop
{
class Program
{
static void Main(string[] args)
{
var app = new PowerPoint.Application();
var pres = app.Presentations;
var file = pres.Open(#"C:\Presentation1.ppt", MsoTriState.msoTrue, MsoTriState.msoTrue, MsoTriState.msoFalse);
file.SaveCopyAs(#"C:\presentation1.jpg", Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsJPG, MsoTriState.msoTrue);
}
}
}
Edit:
Sinan's version using export looks to be a bit better option since you can specify an output resolution. For C#, change the last line above to:
file.Export(#"C:\presentation1.jpg", "JPG", 1024, 768);
As Kev points out, don't use this on a web server. However, the following Perl script is perfectly fine for offline file conversion etc:
#!/usr/bin/perl
use strict;
use warnings;
use Win32::OLE;
use Win32::OLE::Const 'Microsoft PowerPoint';
$Win32::OLE::Warn = 3;
use File::Basename;
use File::Spec::Functions qw( catfile );
my $EXPORT_DIR = catfile $ENV{TEMP}, 'ppt';
my ($ppt) = #ARGV;
defined $ppt or do {
my $progname = fileparse $0;
warn "Usage: $progname output_filename\n";
exit 1;
};
my $app = get_powerpoint();
$app->{Visible} = 1;
my $presentation = $app->Presentations->Open($ppt);
die "Could not open '$ppt'\n" unless $presentation;
$presentation->Export(
catfile( $EXPORT_DIR, basename $ppt ),
'JPG',
1024,
768,
);
sub get_powerpoint {
my $app;
eval { $app = Win32::OLE->GetActiveObject('PowerPoint.Application') };
die "$#\n" if $#;
unless(defined $app) {
$app = Win32::OLE->new('PowerPoint.Application',
sub { $_[0]->Quit }
) or die sprintf(
"Cannot start PowerPoint: '%s'\n", Win32::OLE->LastError
);
}
return $app;
}

Categories