Issue

matx::io::write_mat saves only one tensor per .mat file. We want to save more than one tensor per file.

Elaborate

Imagine a .mat file as a container for variables. The MatX program that I wrote has two tensors named tensorA and tensorB, and I want both of them in a specific .mat file called experiment.mat.

However, after we save tensorA, when we try to save tensorB to the same file, tensorB overwrites tensorA. We want to save both of them together and avoid the overwrite.

Solution

We updated the API (matx::io::write_mat) in our proposed change. It now lets us collect both tensors and then write the file once.

Elaborate

OLD

In the old API, matx::io::write_mat(tensor, filename, name) accepted one tensor and one variable name. Each call converted the tensor to a NumPy array, created a fresh one-entry pybind11::dict, and passed it to scipy.io.savemat. However, every time we called matx::io::write_mat(), it rewrote the file.

An example of how users used to use it (both calls target the same file):

matx::io::write_mat(input, "experiment.mat", "input");
matx::io::write_mat(result, "experiment.mat", "result");

NEW

We created a new API call, matx::io::add_mat_variable(dict, name, tensor), that converts the tensor to a NumPy array and inserts it into a dictionary. You can call it repeatedly to collect multiple tensors in memory. As a result, we are essentially storing the tensors in a dictionary.

An example of how users would use it:

pybind11::dict variables;

matx::io::add_mat_variable(variables, "input", input);
matx::io::add_mat_variable(variables, "result", result);
matx::io::write_mat(variables, "experiment.mat");

Now experiment.mat contains both input and result as separate named variables.

View the pull request on GitHub ↗