From 3c2cc8f656a9fd7f21a5c6245540662e234b3ccc Mon Sep 17 00:00:00 2001 From: Lukas Welzel Date: Sat, 13 Sep 2025 16:18:14 +0200 Subject: [PATCH 1/2] adds util functions to read `.mat` files and save them as `.pt` (torch) files --- README.md | 7 +++++ utils.py | 88 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+) create mode 100644 utils.py diff --git a/README.md b/README.md index 29709c2..af36449 100644 --- a/README.md +++ b/README.md @@ -6,10 +6,17 @@ The matrices enable efficient extension of non-periodic functions to periodic on ## Example Usage +### Computing and saving FC Gram matrices to '.mat' files ```matlab FCGram_Matrices(4, 25, 12, 25, 20, 2, 256) ``` +### Writing FC Gram matrices in '.mat' files to '.pt' files. +This requires having scipy and pytorch installed. +```bash +python utils.py +``` + ## Parameters | Parameter | Type | Description | Typical Values | diff --git a/utils.py b/utils.py new file mode 100644 index 0000000..b161550 --- /dev/null +++ b/utils.py @@ -0,0 +1,88 @@ +from pathlib import Path +from typing import Tuple + +import scipy.io +import torch +from numpy import ndarray as Arr + +def _read_mat_file(file_path: Path) -> Tuple[Arr, Arr]: + """ + Read a MATLAB .mat file and extract the `ArQr` and `AlQl` arrays. + + Args: + file_path (Path): Path to the .mat file. + + Returns: + Tuple[Arr, Arr]: A tuple containing the arrays (ArQr, AlQl). + + Raises: + AssertionError: If the file is not a `.mat` file. + KeyError: If `ArQr` or `AlQl` keys are missing in the file. + """ + file_path = Path(file_path) + assert file_path.suffix == ".mat", "File must be a .mat file" + mat = scipy.io.loadmat(file_path) + ArQr, AlQl = mat["ArQr"], mat["AlQl"] + return ArQr, AlQl + +def read_mat_file(d: int, C: int = 25) -> Tuple[Arr, Arr]: + """ + Load a .mat file for given `d` and `C` values from the default directory. + + Args: + d (int): Dimension index for the dataset. + C (int, optional): Constant index (default is 25). + + Returns: + Tuple[Arr, Arr]: Arrays (ArQr, AlQl) loaded from the file. + """ + file_pattern = f"FCGram_data_d{d}_C{C}.mat" + file_path = Path("./FCGram_matrices") / file_pattern + return _read_mat_file(file_path) + +def torch_read_mat_file(d: int, C: int = 25) -> Tuple[torch.Tensor, torch.Tensor]: + """ + Load a .mat file and convert its arrays into PyTorch tensors. + + Args: + d (int): Dimension index for the dataset. + C (int, optional): Constant index (default is 25). + + Returns: + Tuple[torch.Tensor, torch.Tensor]: Tensors (ArQr, AlQl). + """ + ArQr, AlQl = read_mat_file(d, C) + return torch.tensor(ArQr), torch.tensor(AlQl) + +def convert_all_mat_to_torch(src_dir: Path, dst_dir: Path) -> None: + """ + Convert all `.mat` files in a directory to `.pt` PyTorch tensor files. + + Args: + src_dir (Path): Source directory containing .mat files. + dst_dir (Path): Destination directory to save .pt files. + + Notes: + - Tensors are saved with dtype `torch.float64` on CPU. + - Destination directory will be created if it does not exist. + """ + src_dir = Path(src_dir) + dst_dir = Path(dst_dir) + dst_dir.mkdir(parents=True, exist_ok=True) + + for mat_file in src_dir.glob("FCGram_data_d*_C*.mat"): + ArQr, AlQl = _read_mat_file(mat_file) + ArQr_torch = torch.tensor(ArQr, dtype=torch.float64, device="cpu") + AlQl_torch = torch.tensor(AlQl, dtype=torch.float64, device="cpu") + + dst_file = dst_dir / mat_file.name + torch.save( + { + "ArQr": ArQr_torch, + "AlQl": AlQl_torch + }, + dst_file.with_suffix(".pt") + ) + +if __name__ == "__main__": + convert_all_mat_to_torch("./FCGram_matrices", "./FCGram_matrices_torch") \ No newline at end of file From 92c183904c202c5fab8120458286902c3f3f732e Mon Sep 17 00:00:00 2001 From: Lukas Welzel Date: Sun, 14 Sep 2025 11:16:08 +0200 Subject: [PATCH 2/2] add .npz saving --- README.md | 2 +- utils.py | 29 ++++++++++++++++++++++++++++- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index af36449..51ed2a5 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ The matrices enable efficient extension of non-periodic functions to periodic on FCGram_Matrices(4, 25, 12, 25, 20, 2, 256) ``` -### Writing FC Gram matrices in '.mat' files to '.pt' files. +### Writing FC Gram matrices in '.mat' files to '.pt' and '.npz' files. This requires having scipy and pytorch installed. ```bash python utils.py diff --git a/utils.py b/utils.py index b161550..89bbf49 100644 --- a/utils.py +++ b/utils.py @@ -3,6 +3,7 @@ import scipy.io import torch +import numpy as np from numpy import ndarray as Arr def _read_mat_file(file_path: Path) -> Tuple[Arr, Arr]: @@ -84,5 +85,31 @@ def convert_all_mat_to_torch(src_dir: Path, dst_dir: Path) -> None: dst_file.with_suffix(".pt") ) +def convert_all_mat_to_numpy(src_dir: Path, dst_dir: Path) -> None: + """ + Convert all `.mat` files in a directory to `.npz` NumPy files. + + Args: + src_dir (Path): Source directory containing .mat files. + dst_dir (Path): Destination directory to save .npz files. + + Notes: + - Destination directory will be created if it does not exist. + """ + src_dir = Path(src_dir) + dst_dir = Path(dst_dir) + dst_dir.mkdir(parents=True, exist_ok=True) + + for mat_file in src_dir.glob("FCGram_data_d*_C*.mat"): + ArQr, AlQl = _read_mat_file(mat_file) + + dst_file = dst_dir / mat_file.name + np.savez( + dst_file.with_suffix(".npz"), + ArQr=ArQr, + AlQl=AlQl + ) + if __name__ == "__main__": - convert_all_mat_to_torch("./FCGram_matrices", "./FCGram_matrices_torch") \ No newline at end of file + convert_all_mat_to_torch("./FCGram_matrices", "./FCGram_matrices_torch") + convert_all_mat_to_numpy("./FCGram_matrices", "./FCGram_matrices_numpy") \ No newline at end of file