Skip to content

Commit

Permalink
run in parallel and with iteration progress
Browse files Browse the repository at this point in the history
  • Loading branch information
brisvag committed Aug 2, 2023
1 parent e08fe2d commit c3b2afc
Showing 1 changed file with 29 additions and 13 deletions.
42 changes: 29 additions & 13 deletions stemia/imod/find_NAD_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,48 @@ def cli(input, k_values, iterations, std):
Test a range of k and iteration values for nad_eed_3d
"""
from pathlib import Path
import re
from functools import partial

import mrcfile
from io import StringIO
from rich.progress import track
from rich.progress import Progress
from rich import print
import sh

inp = Path(input)
if std is None:
with mrcfile.open(input, header_only=True) as mrc:
std = mrc.header.rms.item()

stdout = StringIO()

ks = [float(k) for k in k_values.split(',')]
for k in track(ks, description='Denoising...'):
k = k * std
out = inp.with_stem(inp.stem + f'-{k:.5f}_i').with_suffix('')
max_it = max(float(it) for it in iterations.split(','))

it_n = re.compile(r'iteration number:\s+\d+')

with Progress() as progress:
def _process_output(task, line):
if it_n.match(line):
progress.update(task, advance=1)

try:
sh.nad_eed_3d(
procs = []
for k_ in ks:
k = k_ * std
out = inp.with_stem(inp.stem + f'-{k:.5f}_i').with_suffix('')

task = progress.add_task(f'Iterating with k={k:.5f} ({k_} * std)...', total=max_it)

proc = sh.nad_eed_3d(
'-k', k,
'-i', iterations,
'-e', 'mrc',
str(inp),
str(out),
_out=stdout,
_err=stdout,
_out=partial(_process_output, task),
_err=print,
_bg=True,
)
except:
print(stdout.getvalue())

procs.append(proc)

for proc in procs:
proc.wait()

0 comments on commit c3b2afc

Please sign in to comment.