From 02100624a89f3acdf5757da69051e0e69ce27843 Mon Sep 17 00:00:00 2001 From: domfournier Date: Thu, 5 Jun 2025 16:42:13 -0700 Subject: [PATCH 1/4] Handling of either int or string channels --- simpeg/directives/_save_geoh5.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/simpeg/directives/_save_geoh5.py b/simpeg/directives/_save_geoh5.py index 0096bc6b7d..7faad2504a 100644 --- a/simpeg/directives/_save_geoh5.py +++ b/simpeg/directives/_save_geoh5.py @@ -67,7 +67,7 @@ def endIter(self): self.write(self.opt.iter) def get_names( - self, component: str, channel: str, iteration: int + self, component: str, channel: str | int | None, iteration: int ) -> tuple[str, str]: """ Format the data and property_group name. @@ -77,7 +77,9 @@ def get_names( base_name += f"_{component}" channel_name = base_name - if channel: + if isinstance(channel, int): + channel_name += f"_[{channel}]" + elif isinstance(channel, str): channel_name += f"_{channel}" if self.label is not None: @@ -280,7 +282,7 @@ def write(self, iteration: int, values: list[np.ndarray] = None): # flake8: noq self.data_type[component][channel] = data.entity_type type_name = f"{self._attribute_type}_{component}" if channel: - type_name += f"_{channel}" + type_name += f"_[{ii}]" data.entity_type.name = type_name else: data.entity_type = w_s.find_type( @@ -490,7 +492,7 @@ def __init__( super().__init__(h5_object, **kwargs) def get_names( - self, component: str, channel: str, iteration: int + self, component: str, channel: int | None, iteration: int ) -> tuple[str, str]: """ Format the data and property_group name. @@ -545,7 +547,7 @@ def write(self, iteration: int, values: list[np.ndarray] | None = None): """ petro_model = self.get_values(values) petro_model = self.apply_transformations(petro_model).flatten() - channel_name, base_name = self.get_names("petrophysics", "", iteration) + channel_name, _ = self.get_names("petrophysics", "", iteration) with fetch_active_workspace(self._geoh5, mode="r+") as w_s: h5_object = w_s.get_entity(self.h5_object)[0] data = h5_object.add_data( From 644fa647a127530cb4887b535bcd75bac2255907 Mon Sep 17 00:00:00 2001 From: domfournier Date: Fri, 6 Jun 2025 09:36:16 -0700 Subject: [PATCH 2/4] Fix extra underscore with empty channels --- simpeg/directives/_save_geoh5.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/simpeg/directives/_save_geoh5.py b/simpeg/directives/_save_geoh5.py index 7faad2504a..80988191c3 100644 --- a/simpeg/directives/_save_geoh5.py +++ b/simpeg/directives/_save_geoh5.py @@ -79,7 +79,7 @@ def get_names( channel_name = base_name if isinstance(channel, int): channel_name += f"_[{channel}]" - elif isinstance(channel, str): + elif isinstance(channel, str) and len(component) > 1: channel_name += f"_{channel}" if self.label is not None: From aa02b4718fdf2805eecb1c048e6d635c6c595a60 Mon Sep 17 00:00:00 2001 From: domfournier Date: Fri, 6 Jun 2025 15:12:37 -0700 Subject: [PATCH 3/4] Fix handling of int for channels --- simpeg/directives/_save_geoh5.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/simpeg/directives/_save_geoh5.py b/simpeg/directives/_save_geoh5.py index 80988191c3..3db9d37cd6 100644 --- a/simpeg/directives/_save_geoh5.py +++ b/simpeg/directives/_save_geoh5.py @@ -77,7 +77,7 @@ def get_names( base_name += f"_{component}" channel_name = base_name - if isinstance(channel, int): + if isinstance(channel, np.integer): channel_name += f"_[{channel}]" elif isinstance(channel, str) and len(component) > 1: channel_name += f"_{channel}" @@ -281,8 +281,11 @@ def write(self, iteration: int, values: list[np.ndarray] = None): # flake8: noq if channel not in self.data_type[component].keys(): self.data_type[component][channel] = data.entity_type type_name = f"{self._attribute_type}_{component}" - if channel: + if isinstance(channel, int): type_name += f"_[{ii}]" + else: + type_name += f"_{channel}" + data.entity_type.name = type_name else: data.entity_type = w_s.find_type( From b1aa1cc58b4189706f2b00bb500ec9fcb0908945 Mon Sep 17 00:00:00 2001 From: domfournier Date: Sat, 7 Jun 2025 21:19:25 -0700 Subject: [PATCH 4/4] Better handling of labels --- simpeg/directives/_save_geoh5.py | 33 ++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/simpeg/directives/_save_geoh5.py b/simpeg/directives/_save_geoh5.py index 3db9d37cd6..4cd3cb8aa2 100644 --- a/simpeg/directives/_save_geoh5.py +++ b/simpeg/directives/_save_geoh5.py @@ -67,7 +67,7 @@ def endIter(self): self.write(self.opt.iter) def get_names( - self, component: str, channel: str | int | None, iteration: int + self, component: str, channel: str, iteration: int ) -> tuple[str, str]: """ Format the data and property_group name. @@ -77,9 +77,7 @@ def get_names( base_name += f"_{component}" channel_name = base_name - if isinstance(channel, np.integer): - channel_name += f"_[{channel}]" - elif isinstance(channel, str) and len(component) > 1: + if len(channel) > 0: channel_name += f"_{channel}" if self.label is not None: @@ -88,6 +86,17 @@ def get_names( return channel_name, base_name + @staticmethod + def _channel_label(channel: int, label: str | float | None) -> str: + """ + Format the channel label. + """ + if isinstance(label, str) and len(label) > 1: + return label + elif isinstance(label, float): + return f"[{channel}]" + return "" + @abstractmethod def write(self, iteration: int, values: list[np.ndarray] = None): # flake8: noqa """ @@ -265,8 +274,9 @@ def write(self, iteration: int, values: list[np.ndarray] = None): # flake8: noq if self.sorting is not None: values = values[self.sorting] + label = self._channel_label(ii, channel) channel_name, base_name = self.get_names( - component, channel, iteration + component, label, iteration ) data = h5_object.add_data( @@ -280,12 +290,7 @@ def write(self, iteration: int, values: list[np.ndarray] = None): # flake8: noq # Re-assign the data type if channel not in self.data_type[component].keys(): self.data_type[component][channel] = data.entity_type - type_name = f"{self._attribute_type}_{component}" - if isinstance(channel, int): - type_name += f"_[{ii}]" - else: - type_name += f"_{channel}" - + type_name = f"{self._attribute_type}_{component}" + f"_{label}" data.entity_type.name = type_name else: data.entity_type = w_s.find_type( @@ -448,10 +453,10 @@ def write(self, iteration: int, **_): for component in self.components: properties = [] - for channel in self.channels: - + for ii, channel in enumerate(self.channels): + label = self._channel_label(ii, channel) channel_name, base_name = self.get_names( - component, channel, iteration + component, label, iteration ) children = [ child