Skip to content

Commit

Permalink
parse char tensors
Browse files Browse the repository at this point in the history
  • Loading branch information
dragazo committed May 9, 2024
1 parent a6fc238 commit 59754b5
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 8 deletions.
21 changes: 14 additions & 7 deletions src/procedures/matlab/matlab.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
*
* For more information, check out https://www.mathworks.com/products/matlab.html.
*
* @service
* @alpha
* @service
*/

const logger = require("../utils/logger")("matlab");
Expand All @@ -20,11 +20,6 @@ const request = axios.create({
const MATLAB = {};
MATLAB.serviceName = "MATLAB";

function reversed(arr) {
const cpy = [...arr];
return cpy.reverse();
}

async function requestWithRetry(url, body, numRetries = 0) {
try {
return await request.post(url, body, {
Expand All @@ -42,6 +37,8 @@ async function requestWithRetry(url, body, numRetries = 0) {
* Evaluate a MATLAB function with the given arguments and number of return
* values.
*
* For a list of all MATLAB functions, see the `Reference Manual <https://www.mathworks.com/help/matlab/referencelist.html?type=function>`__.
*
* @param {String} fn Name of the function to call
* @param {Array<Any>=} args arguments to pass to the function
* @param {BoundedInteger<1>=} numReturnValues Number of return values expected.
Expand Down Expand Up @@ -150,7 +147,13 @@ MATLAB._parseResultData = (result) => {
if (!Array.isArray(result.mwdata) || result.mwdata.length !== 1 || typeof(result.mwdata[0]) !== 'string') {
throw Error('error parsing character string result');
}
data = data[0];
function rejoin(x) {
if (x.length !== 0 && !Array.isArray(x[0])) {
return x.join('');
}
return x.map((y) => rejoin(y));
}
return MATLAB._squeeze(rejoin(MATLAB._unflatten([...data[0]], size)));
}

return MATLAB._squeeze(MATLAB._unflatten(data, size));
Expand Down Expand Up @@ -188,6 +191,10 @@ MATLAB._colcat = (cols) => {
};

MATLAB._unflatten = (data, shape) => {
if (!Array.isArray(data)) {
throw Error('internal usage error');
}

if (shape.length <= 1) {
return data;
}
Expand Down
36 changes: 35 additions & 1 deletion test/procedures/matlab/matlab.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,35 @@ describe(utils.suiteName(__filename), function () {
const expected = [[1,1,1],[1,1,1],[1,1,1]];
assert.deepEqual(result, expected);
});
it("should parse in column major order - 1", function() {
it("should parse in column major order - 2", function() {
const result = MATLAB._parseResult(
{"results":[{"mwdata":[1,1,1,0,1,1,0,0,1],"mwsize":[3,3],"mwtype":"double"}],"isError":false,"uuid":"","messageFaults":[]}
);
const expected = [[1,0,0],[1,1,0],[1,1,1]];
assert.deepEqual(result, expected);
});
it("should parse in column major order - 3", function() {
const result = MATLAB._parseResult(
{"results":[{"mwdata":[1,0,0,0,1,0,0,0,1,0,0,0],"mwsize":[3,4],"mwtype":"double"}],"isError":false,"uuid":"","messageFaults":[]}
);
const expected = [[1,0,0,0],[0,1,0,0],[0,0,1,0]];
assert.deepEqual(result, expected);
});
it("should parse in column major order - 4", function() {
const result = MATLAB._parseResult(
{"results":[{"mwdata":[140,320,146,335],"mwsize":[2,2],"mwtype":"double"}],"isError":false,"uuid":"","messageFaults":[]}
);
const expected = [[140,146],[320,335]];
assert.deepEqual(result, expected);
});

it("should parse character tensors", function() {
const result = MATLAB._parseResult(
{"results":[{"mwdata":["00110101"],"mwsize":[4,2],"mwtype":"char"}],"isError":false,"uuid":"","messageFaults":[]}
);
const expected = ["00","01","10","11"];
assert.deepEqual(result, expected);
});
});

describe("_parseArgument", function () {
Expand Down Expand Up @@ -287,6 +309,18 @@ describe(utils.suiteName(__filename), function () {
const reconstructed = MATLAB._unflatten(flat, shape);
assert.deepEqual(input, reconstructed);
});

it("should reconstruct a character tensor", function () {
const example = ["0","0","1","1","0","1","0","1"];
const actual = MATLAB._unflatten(example, [4, 2]);
const expected = [
["0", "0"],
["0", "1"],
["1", "0"],
["1", "1"],
];
assert.deepEqual(actual, expected);
});
});

function range(end) {
Expand Down

0 comments on commit 59754b5

Please sign in to comment.