Using matlab code in Octave - Bayes Net Toolbox - matlab

I am trying to run Kevin Murphy's Bayes Net Toolbox in Octave and encountering some problems. It doesn't help that I'm a novice at Bayesian networks, Matlab and Octave.
This toolbox was originally written for Matlab. There is a large test file called test_BNT.m which runs through all the functionality in the toolbox. Most of the error messages relate to the difference between & and && in Matlab and Octave. This is easy to fix. However, I've now come across a new problem and I don't know what to do about it.
For instance, the qmr1.m script creates an instance of the pearl_inf_engine class, sets some of the member member variables and passes the instance of the class to another function. Later on, the member variables are accessed again in a different script (parallel_protocol.m). But when this happens, the following message appears:
error: invalid index for class
error: evaluating argument list element number 1
It seems that from one script to another, it has forgotten that the class has any member variables and gives the invalid index message when you try to access them.
Is this a common error with an easy solution? Is something wrong with the path or working directory? Maybe someone else has already converted the BNT to octave and knows what to do?
Edit
I was able to get past this error message. The trick was to read the installation instructions (haha) and run addpath(genpathKPM(<BNT base directory)). genpathKPM.m is a script includes in BNT which adds all the required directories to the path.
After doing this, run test_BNT.m and change & to && and | to || at each line where it gives a warning. This will clear up most of the errors.
However, I'm still unable to run mpe1.m, mp2.m, mildew1.m and some others. The new error message I'm stuck on is:
error: invalid empty index list
error: called from:
error: C:\FullBNT-1.0.7\bnt\BNT\inference\static\#var_elim_inf_engine\find_mpe
.m at line 63, column 5
on this line of code:
eval(['sCPT.T(', sargs, num2str(jj), ')=0;']);
If I can get all the scripts to work, I'll post an answer here with the steps I took to do it.
Edit 2
I was able to get past the problem in the previous edit. Replace
eval(['sCPT.T(', sargs, num2str(jj), ')=0;']);
with
eval(['sCPT.T(', sargs, sprintf('%d',jj), ')=0;']);
The next problem is identical. Just replace num2str in the same way.
This file was apparently contributed by a user of BNT, and not written by the original author. Using eval kind of a hack, I think. A better fix would be to just rewrite the code so it doesn't use eval at all.
There is one more error in draw_graph.m, which was apparently also an outside contribution to the project. I just commented out the call to that function since I'm not interested in drawing graphs right now. After doing this, and continuing to fix shortcircuit operators, all of the tests in test_BNT.m will run.
Still, I won't create an answer for this until I can get draw_graph.m to run, too.

As a significant amount of time has passed, and the answer to the core problem was provided in the question, I will post it here so it will not stay listed as unanswered:
tl;dr: Change a few operators, solve the remaining bugs specified below, and everything works except the drawing of graphs.
Edit
I was able to get past this error message. The trick was to read the
installation instructions (haha) and run addpath(genpathKPM(<BNT base
directory)). genpathKPM.m is a script includes in BNT which adds
all the required directories to the path.
After doing this, run test_BNT.m and change & to && and | to
|| at each line where it gives a warning. This will clear up most of
the errors.
However, I'm still unable to run mpe1.m, mp2.m, mildew1.m and
some others. The new error message I'm stuck on is:
error: invalid empty index list
error: called from:
error: C:\FullBNT-1.0.7\bnt\BNT\inference\static\#var_elim_inf_engine\find_mpe
.m at line 63, column 5
on this line of code:
eval(['sCPT.T(', sargs, num2str(jj), ')=0;']);
If I can get all the scripts to work, I'll post an answer here with
the steps I took to do it.
Edit 2
I was able to get past the problem in the previous edit. Replace
eval(['sCPT.T(', sargs, num2str(jj), ')=0;']); with
eval(['sCPT.T(', sargs, sprintf('%d',jj), ')=0;']);
The next problem is identical. Just replace num2str in the same way.
This file was apparently contributed by a user of BNT, and not written
by the original author. Using eval kind of a hack, I think. A better
fix would be to just rewrite the code so it doesn't use eval at all.
There is one more error in draw_graph.m, which was apparently also
an outside contribution to the project. I just commented out the call
to that function since I'm not interested in drawing graphs right now.
After doing this, and continuing to fix shortcircuit operators, all of
the tests in test_BNT.m will run.

Related

SPM12 debugging on Matlab

I have a problem using SPM on Matlab. I have an m-file that I need to debug and I have not written.
This code is old and probably the error is hopefully given by the difference in syntax of the newer versions.
The error pops out using this function spm_get_files, originally present in the code. When changing this function to spm_get (I found that these two functions are supposedly equivalent) I get the following error:
Error using spm_get (line 1726)
Illegal Action string
Error in suj6 (line 46)
Fr3 = spm_get('/home/***/folder','a3*093.img');
where '/home/***/folder','a3*093.img' is the directory of the input files I want to analyze. These are fMRI scans.
My Matlab version is 9(R2016a) and the SPM is SPM12. (The code is old and was originally written in SPM99)
Anyone can help me out?
Thank you!
spm_get_files is basically just this one line of code:
varargout = {spm_get('Files',varargin{:})};
Clearly if you would like to switch back to using spm_get, you need to explicitly add 'Files' as the first argument.

Matlab: Error in copied and renamed function [duplicate]

I using the function dtw in the latest MATLAB release, and would like to tweak a few parts of it. To get started, I typed:
edit dtw
I saved the resulting code to file called dtw_Copy.m, and changed the name of the function to dtw_Copy as well. Going through the code line by line with a set of input parameters x and y, around line 90:
[metric, varargin] = getmutexclopt({'absolute','euclidean','squared','symmkl'},'euclidean',varargin);
I receive an error message:
Undefined function 'getmutexclopt' for input arguments of type 'cell'.
I also get this error message if I do not go through the code line by line, and simply type dtw_Current(x,y), after again testing a set of input parameters x and y.
Upon running:
help 'getmutexclopt'
it is indicated that getmutexclopt is not found. I tried also:
edit 'getmutexclopt'
But am told that currentDirectory/getmutexcloptm.m does not exist.
I tried:
which getmutexclopt
And am told that getmutexclopt is not found.
Searching online, I found a resource that seemed straight-forward in trouble-shooting this error. The resource recommends to ensure the toolbox is installed. I am unsure which toolbox supports the function getmutexclopt, and so I type the function name into the website. This results in a message that: "Your search - getmutexclopt - did not match any documents."
The resource also recommends verifying the path used to access the function. I followed the instructions to do so, and when I typed:
which -all getmutexclopt
I receive:
currentDirectory\matlab\toolbox\signal\signal\private\getmutexclopt.m % Private to signal
This seems to indicate that the function is in the signal toolbox, which is private? Is there a possibility to still run dtw_Current(x,y) and/or to run its contents line by line?
Yes, this issue is because the function getmutexclopt is a private function. You'll need to make a copy of that function if you hope to safely call it from your copy of dtw. It appears to be a basic function (type edit private/getmutexclopt.m in your Command Window) so you may be able to add it as a sub-function to your dtw_Copy/dtw_Current.
See also this question – adding private functions to the search path is not allowed.

Undefined function in MATLAB

I using the function dtw in the latest MATLAB release, and would like to tweak a few parts of it. To get started, I typed:
edit dtw
I saved the resulting code to file called dtw_Copy.m, and changed the name of the function to dtw_Copy as well. Going through the code line by line with a set of input parameters x and y, around line 90:
[metric, varargin] = getmutexclopt({'absolute','euclidean','squared','symmkl'},'euclidean',varargin);
I receive an error message:
Undefined function 'getmutexclopt' for input arguments of type 'cell'.
I also get this error message if I do not go through the code line by line, and simply type dtw_Current(x,y), after again testing a set of input parameters x and y.
Upon running:
help 'getmutexclopt'
it is indicated that getmutexclopt is not found. I tried also:
edit 'getmutexclopt'
But am told that currentDirectory/getmutexcloptm.m does not exist.
I tried:
which getmutexclopt
And am told that getmutexclopt is not found.
Searching online, I found a resource that seemed straight-forward in trouble-shooting this error. The resource recommends to ensure the toolbox is installed. I am unsure which toolbox supports the function getmutexclopt, and so I type the function name into the website. This results in a message that: "Your search - getmutexclopt - did not match any documents."
The resource also recommends verifying the path used to access the function. I followed the instructions to do so, and when I typed:
which -all getmutexclopt
I receive:
currentDirectory\matlab\toolbox\signal\signal\private\getmutexclopt.m % Private to signal
This seems to indicate that the function is in the signal toolbox, which is private? Is there a possibility to still run dtw_Current(x,y) and/or to run its contents line by line?
Yes, this issue is because the function getmutexclopt is a private function. You'll need to make a copy of that function if you hope to safely call it from your copy of dtw. It appears to be a basic function (type edit private/getmutexclopt.m in your Command Window) so you may be able to add it as a sub-function to your dtw_Copy/dtw_Current.
See also this question – adding private functions to the search path is not allowed.

Find missing but required files for running a script

While deploying packages of Matlab code using Matlab 2015 I encountered the problem of gathering all required files from my repository to run a certain file or set of files. Matlab has a method for simplifying this process, matlab.codetools.requiredFilesAndProducts.
However, sometimes some files are missing in the repository (either because I got a package from someone else who was not that careful or because it was not checked in in the repository).
When running the code one would get of course an error:
Undefined function or variable 'XXX'.
However, this may take long to fix (running takes long, you would have to repeat for every missing file). Therefore I thought to use the function above. Unfortunately, it only lists existing files in the output (I tested this). Functions that are called from your code, but that are not present in the current path are omitted by matlab.codetools.requiredFilesAndProducts.
My problem: I would like to get a list of all files that are required by running a certain file but are not present in the current path so that I can find them and add them to my collection.
I know that this must be an iterative process because the missing files could themselves call other missing files and I know that there would be false positives, some of these items could be unknown variables instead, and I know that the missing files would only have a name, no path (of course).
My question: What is the easiest way to find a list of potentially missing files of my code in one go?
Please note that function depfun has been removed in recent versions of Matlab.

Matlab function signature changes

Let us say that I have a Matlab function and I change its signature (i.e. add parameter). As Matlab does not 'compile' is there an easy way to determine which other functions do not use the right signature (i.e. submits the additional parameter). I do not want to determine this at runtime (i.e. get an error message) or have to do text searches. Hope this makes sense. Any feedback would be very much appreciated. Many thanks.
If I understand you correctly, you want to change a function's signature and find all functions/scripts/classes that call it in the "old" way, and change it to the "new" way.
You also indicated you don't want to do it at runtime, or do text searches, but there is no way to detect "incorrect" calls at "parse-time", so I'm afraid these demands leave no option at all to detect old function calls...
What I would do in that case is temporarily add a few lines to the new function:
function myFunc(param1, param2, newParam) % <-- the NEW signature
if nargin == 2
clc, error('old call detected.'); end
and then run the main script/function/whatever in which this function resides. You'll get one error for each time something calls the function incorrectly, along with the error stack in the Matlab command window.
It is then a matter of clicking on the link in the bottom of the error stack, correct the function call, and repeat from the top until no more errors occur.
Don't forget to remove these lines when you're done, or better, replace the word error with warning just to capture anything that was missed.
Better yet: if you're on linux, a text search would be a matter of
$ grep -l 'myFunc(.*,.*); *.m'
which will list all the files having the "incorrect" call. That's not too difficult I'd say...You can probably do a similar thing with the standard windows search, but I can't test that right now.
This is more or less what the dependency report was invented for. Using that tool, you can find what functions/scripts call your altered function. Then it is just a question of manually inspecting every occurrence.
However, I'd advise to make your changes to the function signature such that backwards compatibility is maintained. You can do so by specifying default values for new parameters and/or issuing a warning in those scenarios. That way, your code will run, and you will get run-time hints of deprecated code (which is more or less a necessary evil in interpreted/dynamic languages).
For many dynamic languages (and MATLAB specifically) it is generally impossible to fully inspect the code without the interpreter executing the code. Just imagine the following piece of code:
x = magic(10);
In general, you'd say that the magic function is called. However, magic could map to a totally different function. This could be done in ways that are invisible to a static analysis tool (such as the dependency report): e.g. eval('magic = 1:100;');.
The only way is to go through your whole code base, either inspecting every occurrence manually (which can be found easily with a text search) or by running a test that fully covers your code base.
edit:
There is however a way to access intermediate outputs of the MATLAB parser. This can be accessed using the undocumented and unsupported mtree function (which can be called like this: t = mtree(file, '-file'); for every file in your code base). Using the resulting structure you might be able to find calls with a certain amount of parameters.