From 7964dc85db49e85c7209f5aecf06f32e424361a9 Mon Sep 17 00:00:00 2001 From: Matthew Evans Date: Fri, 13 Jun 2025 15:49:23 +0100 Subject: [PATCH] Add mode to attempt to read files with unknown columns and only warn --- galvani/BioLogic.py | 77 +++++++++++++++++++++++++++++++++++++++------ 1 file changed, 68 insertions(+), 9 deletions(-) diff --git a/galvani/BioLogic.py b/galvani/BioLogic.py index 7c9d3d6..aa72045 100644 --- a/galvani/BioLogic.py +++ b/galvani/BioLogic.py @@ -13,9 +13,12 @@ from os import SEEK_SET import time from datetime import date, datetime, timedelta from collections import defaultdict, OrderedDict +import warnings import numpy as np +UNKNOWN_COLUMN_TYPE_HIERARCHY = ("', with an attempt to read + it with a few different dtypes. + + """ self.loop_index = None if isinstance(file_or_path, str): mpr_file = open(file_or_path, "rb") @@ -596,8 +622,41 @@ class MPRfile: assert not any(remaining_headers) - self.dtype, self.flags_dict = VMPdata_dtype_from_colIDs(column_types) - self.data = np.frombuffer(main_data, dtype=self.dtype) + dtypes, self.flags_dict = VMPdata_dtype_from_colIDs( + column_types, error_on_unknown_column=error_on_unknown_column + ) + + unknown_cols = [] + # Iteratively work through the unknown columns and try to read them + if not error_on_unknown_column: + for col, _ in dtypes: + if col.startswith("unknown_colID"): + unknown_cols.append(col) + + if unknown_cols: + # create a list of all possible combinations of dtypes + # for the unknown columns + from itertools import product + perms = product(UNKNOWN_COLUMN_TYPE_HIERARCHY, repeat=len(unknown_cols)) + for perm in perms: + for unknown_col_ind, c in enumerate(unknown_cols): + for ind, (col, _) in enumerate(dtypes): + if c == col: + dtypes[ind] = (col, perm[unknown_col_ind]) + + try: + self.dtype = np.dtype(dtypes) + self.data = np.frombuffer(main_data, dtype=self.dtype) + break + except ValueError: + continue + else: + raise RuntimeError("Unable to read data for unknown columns %s with any of the common dtypes %s", unknown_cols, UNKNOWN_COLUMN_TYPE_HIERARCHY) + + else: + self.dtype = np.dtype(dtypes) + self.data = np.frombuffer(main_data, dtype=self.dtype) + assert self.data.shape[0] == n_data_points # No idea what these 'column types' mean or even if they are actually