Skip to content

Commit 09d5143

Browse files
committed
Scrape info from log file
1 parent 30e3478 commit 09d5143

2 files changed

Lines changed: 19 additions & 8 deletions

File tree

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ exclude_also = [
136136
directory = "coverage_html_report"
137137

138138
[tool.black]
139-
required-version = '24.3.0'
139+
required-version = '25.1.0'
140140
target-version = ['py38', 'py39', 'py310', 'py311']
141141

142142
[tool.flake8]

simpeg/directives/_save_geoh5.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import re
12
from abc import ABC, abstractmethod
23
from datetime import datetime
34
from pathlib import Path
@@ -12,6 +13,7 @@
1213
from geoh5py.objects import ObjectBase
1314
from geoh5py.ui_json.utils import fetch_active_workspace
1415

16+
1517
def compute_JtJdiags(data_misfit, m):
1618
if hasattr(data_misfit, "getJtJdiag"):
1719
return data_misfit.getJtJdiag(m)
@@ -26,6 +28,7 @@ def compute_JtJdiags(data_misfit, m):
2628

2729
return np.asarray(jtj_diag)
2830

31+
2932
class BaseSaveGeoH5(InversionDirective, ABC):
3033
"""
3134
Base class for saving inversion results to a geoh5 file
@@ -364,13 +367,21 @@ def write(self, iteration: int, **_):
364367
if iteration == 0:
365368
with open(filepath, "w", encoding="utf-8") as f:
366369
f.write("iteration beta phi_d phi_m time\n")
367-
368-
with open(filepath, "a", encoding="utf-8") as f:
369-
date_time = datetime.now().strftime("%b-%d-%Y:%H:%M:%S")
370-
f.write(
371-
f"{iteration} {self.invProb.beta:.3e} {self.invProb.phi_d:.3e} "
372-
f"{self.invProb.phi_m:.3e} {date_time}\n"
373-
)
370+
log = []
371+
with open(dirpath / "SimPEG.log", "r", encoding="utf-8") as file:
372+
iter = 0
373+
for line in file:
374+
val = re.findall(
375+
"[+\-]?(?:0|[1-9]\d*)(?:\.\d*)?(?:[eE][+\-]?\d+)", line
376+
)
377+
if len(val) == 5:
378+
log.append(val[:-2])
379+
iter += 1
380+
381+
if len(log) > 0:
382+
with open(filepath, "a", encoding="utf-8") as file:
383+
date_time = datetime.now().strftime("%b-%d-%Y:%H:%M:%S")
384+
file.write(f"{iter-1} " + " ".join(log[-1]) + f" {date_time}\n")
374385

375386
self.save_log()
376387

0 commit comments

Comments
 (0)