build_module_demoΒΆ

# -*-Python-*-
# Created by bgrierson at 15 Nov 2016  06:14

# ----
# Build Module Skeletons
# ----
# Parent module
PARENT = OMFITmodule(None)
root['PARENT'] = PARENT

# Child modules
SIN = OMFITmodule(None)
root['PARENT']['SIN'] = SIN

COS = OMFITmodule(None)
root['PARENT']['COS'] = COS

# ----
# Build SIN Module
# ----
SIN['SCRIPTS']['run'] = OMFITpythonTask(
    "run_sin.py",
    fromString='''
x = linspace(root['INPUTS']['xmin']*2*np.pi,root['INPUTS']['xmax']*2*np.pi,root['INPUTS']['nx'])
y = root['INPUTS']['a'] * np.sin(x)
root['OUTPUTS']['x'] = x
root['OUTPUTS']['y'] = y
''',
)

SIN['SCRIPTS']['reset'] = OMFITpythonTask(
    "reset_sin.py",
    fromString='''
root['INPUTS'].clear()
root['OUTPUTS'].clear()
''',
)

SIN['PLOTS']['show'] = OMFITpythonPlot(
    "show_sin.py",
    fromString='''
ax = gca()
ax.plot(root['OUTPUTS']['x'],root['OUTPUTS']['y'],marker='o',label='SIN')
ax.set_xlabel('x'); ax.set_ylabel('y')
ax.legend()
''',
)

SIN['GUIS']['gui'] = OMFITpythonGUI(
    "gui_sin.py",
    fromString='''
OMFITx.TitleGUI('SIN')
OMFITx.Entry("root['INPUTS']['nx']",'nx',default=101)
OMFITx.Entry("root['INPUTS']['xmin']",'xmin/2pi',default=0.0)
OMFITx.Entry("root['INPUTS']['xmax']",'xmax/2pi',default=1.0)
OMFITx.Entry("root['INPUTS']['a']",'amp',default=1.0)
OMFITx.Button('Run SIN',"root['SCRIPTS']['run']")
OMFITx.Button('Plot SIN',root['PLOTS']['show'].plotFigure,state=['disabled','normal']['y' in root['OUTPUTS']])
OMFITx.Button('Reset SIN',root['SCRIPTS']['reset'])
''',
)

# Settings
SIN['SETTINGS']['MODULE']['defaultGUI'] = "root['GUIS']['gui']"

# ----
# Build COS Module
# ----
COS['SCRIPTS']['run'] = OMFITpythonTask(
    "run_cos.py",
    fromString='''
x = linspace(root['INPUTS']['xmin']*2*np.pi,root['INPUTS']['xmax']*2*np.pi,root['INPUTS']['nx'])
y = root['INPUTS']['a'] * np.cos(x)
root['OUTPUTS']['x'] = x
root['OUTPUTS']['y'] = y
''',
)

COS['SCRIPTS']['reset'] = OMFITpythonTask(
    "reset_cos.py",
    fromString='''
root['INPUTS'].clear()
root['OUTPUTS'].clear()
''',
)

COS['PLOTS']['show'] = OMFITpythonPlot(
    "show_cos.py",
    fromString='''
ax = gca()
ax.plot(root['OUTPUTS']['x'],root['OUTPUTS']['y'],marker='o',label='COS')
ax.set_xlabel('x'); ax.set_ylabel('y')
ax.legend()
''',
)

COS['GUIS']['gui'] = OMFITpythonGUI(
    "gui_cos.py",
    fromString='''
OMFITx.TitleGUI('COS')
OMFITx.Entry("root['INPUTS']['nx']",'nx',default=101)
OMFITx.Entry("root['INPUTS']['xmin']",'xmin/2pi',default=0.0)
OMFITx.Entry("root['INPUTS']['xmax']",'xmax/2pi',default=1.0)
OMFITx.Entry("root['INPUTS']['a']",'amp',default=1.0)
OMFITx.Button('Run COS',"root['SCRIPTS']['run']")
OMFITx.Button('Plot COS',root['PLOTS']['show'].plotFigure,state=['disabled','normal']['y' in root['OUTPUTS']])
OMFITx.Button('Reset COS',root['SCRIPTS']['reset'])
''',
)

# Settings
COS['SETTINGS']['MODULE']['defaultGUI'] = "root['GUIS']['gui']"
COS['SETTINGS']['DEPENDENCIES']['SIN'] = "OMFITmodules[-2]['SIN']"

# ----
# Build PARENT Module
# ----
PARENT['SCRIPTS']['run'] = OMFITpythonTask(
    "run.py",
    fromString='''
y = root['INPUTS']['a']*(SIN['OUTPUTS']['y'] + COS['OUTPUTS']['y'])
root['OUTPUTS']['x'] = copy.copy(SIN['OUTPUTS']['x'])
root['OUTPUTS']['y'] = y
runid = root['SETTINGS']['EXPERIMENT']['runid']
root['RUN_DB'].setdefault(runid,OMFITtree())
root['RUN_DB'][runid]['INPUTS'] = copy.deepcopy(root['INPUTS'])
root['RUN_DB'][runid]['OUTPUTS'] = copy.deepcopy(root['OUTPUTS'])
''',
)

PARENT['SCRIPTS']['reset'] = OMFITpythonTask(
    "reset.py",
    fromString='''
SIN['SCRIPTS']['reset'].run()
COS['SCRIPTS']['reset'].run()
root['INPUTS'].clear()
root['OUTPUTS'].clear()
''',
)

PARENT['PLOTS']['show'] = OMFITpythonPlot(
    "show.py",
    fromString='''
ax = gca()
ax.plot(SIN['OUTPUTS']['x'],SIN['OUTPUTS']['y'],marker='o',label='SIN')
ax.plot(COS['OUTPUTS']['x'],COS['OUTPUTS']['y'],marker='o',label='COS')
ax.plot(root['OUTPUTS']['x'],root['OUTPUTS']['y'],marker='o',label='PARENT')
ax.set_xlabel('x'); ax.set_ylabel('y')
ax.legend()
''',
)

PARENT['PLOTS']['compare'] = OMFITpythonPlot(
    "compare.py",
    fromString='''
ax = gca()
for r in root['RUN_DB']:
    ax.plot(root['RUN_DB'][r]['OUTPUTS']['x'],root['RUN_DB'][r]['OUTPUTS']['y'],label=r)
ax.set_xlabel('x'); ax.set_ylabel('y')
ax.legend()
''',
)

PARENT['GUIS']['gui'] = OMFITpythonGUI(
    "gui.py",
    fromString='''
def updateFiles(location=None):
    runid = str(root['SETTINGS']['EXPERIMENT']['runid'])
    if len(root['RUN_DB']) and runid in root['RUN_DB']:
        printi('Loading cached simulation for runid `{}`'.format(runid))
        base = root['RUN_DB'][runid]
        root['OUTPUTS'] = copy.deepcopy(base['OUTPUTS'])
        root['INPUTS'] = copy.deepcopy(base['INPUTS'])
    else:
        printi('New runid; Execution begins with current settings')


OMFITx.TitleGUI('Parent')
OMFITx.Separator()
OMFITx.Label("1. Run SIN, plot")
OMFITx.Label("2. Run COS, plot")
OMFITx.Label("3. Run PARENT, plot")
OMFITx.Label("4. Change runID, amp and run PARENT")
OMFITx.Label("5. Compare RUN_DB")
OMFITx.Separator()

OMFITx.ComboBox("root['SETTINGS']['EXPERIMENT']['runid']", root['RUN_DB'].keys(), 'Simulation runid',
                postcommand=updateFiles, updateGUI=True, state='normal')

OMFITx.Entry("root['INPUTS']['a']", 'amp for SIN+COS', default=1.0)

OMFITx.Tab('SIN')
OMFITx.CompoundGUI(SIN['GUIS']['gui'])
OMFITx.Tab('COS')
OMFITx.CompoundGUI(COS['GUIS']['gui'])

OMFITx.Tab("")
OMFITx.Button('Run Parent', "root['SCRIPTS']['run']",
              state=['disabled', 'normal']['y' in SIN['OUTPUTS'] and 'y' in COS['OUTPUTS']])
OMFITx.Button('Plot Parent', root['PLOTS']['show'].plotFigure,
              state=['disabled', 'normal']['y' in SIN['OUTPUTS'] and 'y' in COS['OUTPUTS']])
OMFITx.Button('Reset All', "root['SCRIPTS']['reset']")

OMFITx.Separator()
OMFITx.Button('Compare RUN_DB', root['PLOTS']['compare'].plotFigure)
OMFITx.Button('Clear RUN_DB', lambda: root['RUN_DB'].clear())
''',
)

PARENT['RUN_DB'] = OMFITtree()

# Settings
PARENT['SETTINGS']['EXPERIMENT']['runID'] = 'sim1'
PARENT['SETTINGS']['MODULE']['defaultGUI'] = "root['GUIS']['gui']"
PARENT['SETTINGS']['DEPENDENCIES']['SIN'] = "root['SIN']"
PARENT['SETTINGS']['DEPENDENCIES']['COS'] = "root['COS']"