Skip to content

Commit

Permalink
Refs #76. slight refactor: added shutils.exec_redirect_to_stdout and …
Browse files Browse the repository at this point in the history
…use it in mpi filter and mpi reconstruction
  • Loading branch information
yxqd committed Jan 22, 2018
1 parent 85e7676 commit 68a2379
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 23 deletions.
13 changes: 2 additions & 11 deletions python/imars3d/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,8 @@ def _(*args, **kwds):
for k,v in kwds.items():
logger.info(" - %s: %s" % (k,v))
continue
import subprocess as sp, shlex, sys
args = shlex.split(cmd)
# sp.check_call(args, shell=False)
# manually pipe the output from the subprocess to sys.stdout so that
# jupyter gets it inside the web page instead of the stdout of the terminal
# from which jupyter is launched
p = sp.Popen(args, shell=False, stdout=sp.PIPE, stderr=sp.STDOUT)
for c in iter(lambda: p.stdout.read(1), ''):
sys.stdout.write(c)
p.communicate(); r = p.poll()
if r: raise RuntimeError("Cmd %r failed" % cmd)
from .shutils import exec_redirect_to_stdout
exec_redirect_to_stdout(cmd)
logger.info("done.")
return
return _
22 changes: 12 additions & 10 deletions python/imars3d/jnbui/tomoreconui.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ def nextStep(self):
"""
Removes the current panel. Then, creates a ct object,
and saves it into context's ct member.
Fianlly, creates an ImgSliderPanel and displays it.
Fianlly, creates an MainUIPanel and displays it.
"""
self.remove()
from imars3d.CT import CT
Expand All @@ -186,7 +186,7 @@ def nextStep(self):
outdir=context.config.outdir, ob_files=context.config.ob_files,
df_files=context.config.df_files)
context.ct = ct
img_slide = ImgSliderPanel(context)
img_slide = MainUIPanel(context)
img_slide.show()


Expand Down Expand Up @@ -342,7 +342,7 @@ def nextStep(self):
Then, after printing the configuration, creates a ct object,
and stores the result in context.ct.
Finally, removes the current panel,
and replaces it with an ImgSliderPanel.
and replaces it with an MainUIPanel.
"""
# save path of current imars3d config
import imars3d; orig_imars3d_config = os.path.abspath(imars3d.conf_path)
Expand Down Expand Up @@ -377,11 +377,11 @@ def nextStep(self):
context.ct = ct
# new interface
self.remove()
imgslide = ImgSliderPanel(context)
imgslide = MainUIPanel(context)
imgslide.show()


class ImgSliderPanel(base.Panel):
class MainUIPanel(base.Panel):
"""Creates a tab interface where each tab displays
an ImageSlider widget with either the CT, DF, or OB
images displayed. In each tab, there is also
Expand All @@ -390,9 +390,11 @@ class ImgSliderPanel(base.Panel):
a new tab is added that displays
an ImageSlider of the reconstructed images."""

layout = ipyw.Layout(border="1px solid lightgray", padding='4px')

def __init__(self, context):
"""
Create ImgSliderPanel instance.
Create MainUIPanel instance.
Parameters
----------
Expand All @@ -407,9 +409,9 @@ def __init__(self, context):
with wait_alert("Preprocessing. Please wait..."):
self.ppd = ct.preprocess()
# create interface
explanation = ipyw.Label(
"Select a Region of Interest from the CT Images, and make sure the OB and DF images are reasonable",
layout = ipyw.Layout(height='35px', padding='4px', width='500px')
explanation = ipyw.HTML(
"<p>Select a Region of Interest from the CT Images, and make sure the OB and DF images are reasonable</p>",
layout = ipyw.Layout(padding='4px', width='500px')
)
# image sliders
self.ct_slider = ImgSlider.ImageSlider(self.ppd, self.width, self.height)
Expand All @@ -432,7 +434,7 @@ def __init__(self, context):
self.recon_button = ipyw.Button(description="Reconstruct", layout=self.button_layout)
self.recon_button.on_click(self.onRecon)
#
self.panel = ipyw.VBox(children=[explanation, self.tabs, self.recon_button])
self.panel = ipyw.VBox(children=[explanation, self.tabs, self.recon_button], layout=self.layout)
return

def onRecon(self, event):
Expand Down
4 changes: 2 additions & 2 deletions python/imars3d/recon/mpi.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ def recon(sinograms, theta, recon_series, nodes=None, **kwds):
nodes = min(nodes, imars3d.configuration['parallelization']['max_nodes'])
# shell cmd
cmd = 'mpirun -np %(nodes)s python %(pyfile)s' % locals()
if os.system(cmd):
raise RuntimeError("%s failed" % cmd)
from ..shutils import exec_redirect_to_stdout
exec_redirect_to_stdout(cmd)
return


Expand Down
15 changes: 15 additions & 0 deletions python/imars3d/shutils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@

def exec_redirect_to_stdout(cmd, shell=False):
"execute a command in a subproces and redirect outputs (including errors) to sys.stdout"
import subprocess as sp, shlex, sys
args = shlex.split(cmd)
# sp.check_call(args, shell=shell)
# manually pipe the output from the subprocess to sys.stdout so that
# jupyter gets it inside the web page instead of the stdout of the terminal
# from which jupyter is launched
p = sp.Popen(args, shell=shell, stdout=sp.PIPE, stderr=sp.STDOUT)
for c in iter(lambda: p.stdout.read(1), ''):
sys.stdout.write(c)
p.communicate(); r = p.poll()
if r: raise RuntimeError("Cmd %r failed" % cmd)
return


def exec_withlog(cmd, logfile):
import subprocess as sp, shlex
Expand Down

0 comments on commit 68a2379

Please sign in to comment.