From 16965036f38fb133a28592ba72a5d3a416e57386 Mon Sep 17 00:00:00 2001 From: fourndo Date: Tue, 13 Jun 2023 14:55:52 -0700 Subject: [PATCH 1/4] Add out file to saved files --- SimPEG/directives/directives.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/SimPEG/directives/directives.py b/SimPEG/directives/directives.py index 7c4fbb61b6..3430f7e4c0 100644 --- a/SimPEG/directives/directives.py +++ b/SimPEG/directives/directives.py @@ -3008,14 +3008,26 @@ def save_log(self, iteration: int): with Workspace(self._h5_file) as w_s: h5_object = w_s.get_entity(self.h5_object)[0] - child_names = [k.name for k in h5_object.parent.children] + child_names = {k.name: k for k in h5_object.parent.children} if "SimPEG.out" in child_names: - file_entity = h5_object.parent.children[child_names.index("SimPEG.out")] + file_entity = child_names["SimPEG.out"] else: file_entity = h5_object.parent.add_file(filepath) file_entity.values = raw_file + log_file = os.path.join(dirpath, "SimPEG.log") + if os.path.isfile(log_file): + with open(log_file, "rb") as f: + raw_file = f.read() + + if "SimPEG.log" in child_names: + file_entity = child_names["SimPEG.log"] + else: + file_entity = h5_object.parent.add_file(log_file) + + file_entity.values = raw_file + @property def joint_index(self): """ From 59326dac48b77d88ca187b7850d2b01d29654f48 Mon Sep 17 00:00:00 2001 From: fourndo Date: Wed, 14 Jun 2023 10:30:01 -0700 Subject: [PATCH 2/4] Split the write and save_log. Simplify log and out handling --- SimPEG/directives/directives.py | 42 ++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 19 deletions(-) diff --git a/SimPEG/directives/directives.py b/SimPEG/directives/directives.py index 3430f7e4c0..d864082f08 100644 --- a/SimPEG/directives/directives.py +++ b/SimPEG/directives/directives.py @@ -1,3 +1,4 @@ +from pathlib import Path import numpy as np import matplotlib.pyplot as plt import warnings @@ -2885,13 +2886,16 @@ def initialize(self): self.save_components(0) if self.save_objective_function: - self.save_log(0) + self.write_update(0) + self.save_log() def endIter(self): self.save_components(self.opt.iter) if self.save_objective_function: - self.save_log(self.opt.iter) + self.write_update(self.opt.iter) + self.save_log() + def stack_channels(self, dpred: list): """ @@ -2984,11 +2988,10 @@ def save_components(self, iteration: int, values: list[np.ndarray] = None): data, base_name ) - def save_log(self, iteration: int): + def write_update(self, iteration: int): """ - Save iteration metrics to comments. + Write update to file. """ - dirpath = os.path.dirname(self._h5_file) filepath = os.path.join(dirpath, "SimPEG.out") @@ -3003,31 +3006,32 @@ def save_log(self, iteration: int): f"{self.invProb.phi_m:.3e} {date_time}\n" ) - with open(filepath, "rb") as f: - raw_file = f.read() + def save_log(self): + """ + Save iteration metrics to comments. + """ + dirpath = Path(self._h5_file).parent with Workspace(self._h5_file) as w_s: h5_object = w_s.get_entity(self.h5_object)[0] - child_names = {k.name: k for k in h5_object.parent.children} - if "SimPEG.out" in child_names: - file_entity = child_names["SimPEG.out"] - else: - file_entity = h5_object.parent.add_file(filepath) - file_entity.values = raw_file + for file in ["SimPEG.out", "SimPEG.log"]: + filepath = dirpath / file - log_file = os.path.join(dirpath, "SimPEG.log") - if os.path.isfile(log_file): - with open(log_file, "rb") as f: + if not os.path.isfile(filepath): + continue + + with open(filepath, "rb") as f: raw_file = f.read() - if "SimPEG.log" in child_names: - file_entity = child_names["SimPEG.log"] + if h5_object.parent.get_entity(file): + file_entity = h5_object.parent.get_entity(file)[0] else: - file_entity = h5_object.parent.add_file(log_file) + file_entity = h5_object.parent.add_file(filepath) file_entity.values = raw_file + @property def joint_index(self): """ From cc9cd4625996f06f9440be7fd155862f3a429dfa Mon Sep 17 00:00:00 2001 From: domfournier Date: Thu, 15 Jun 2023 09:03:52 -0700 Subject: [PATCH 3/4] Update SimPEG/directives/directives.py Co-authored-by: benk-mira <81254271+benk-mira@users.noreply.github.com> --- SimPEG/directives/directives.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SimPEG/directives/directives.py b/SimPEG/directives/directives.py index d864082f08..9707465e1a 100644 --- a/SimPEG/directives/directives.py +++ b/SimPEG/directives/directives.py @@ -3018,7 +3018,7 @@ def save_log(self): for file in ["SimPEG.out", "SimPEG.log"]: filepath = dirpath / file - if not os.path.isfile(filepath): + if not filepath.is_file(): continue with open(filepath, "rb") as f: From b4c2ab11c4bd3ad2064fce1d662c72413afcec02 Mon Sep 17 00:00:00 2001 From: fourndo Date: Thu, 15 Jun 2023 09:07:44 -0700 Subject: [PATCH 4/4] Use pathlib --- SimPEG/directives/directives.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/SimPEG/directives/directives.py b/SimPEG/directives/directives.py index 9707465e1a..abcf154678 100644 --- a/SimPEG/directives/directives.py +++ b/SimPEG/directives/directives.py @@ -2992,8 +2992,8 @@ def write_update(self, iteration: int): """ Write update to file. """ - dirpath = os.path.dirname(self._h5_file) - filepath = os.path.join(dirpath, "SimPEG.out") + dirpath = Path(self._h5_file).parent + filepath = dirpath / "SimPEG.out" if iteration == 0: with open(filepath, 'w') as f: