Skip to content

Commit

Permalink
Fixed issues with 'user_ns'
Browse files Browse the repository at this point in the history
  • Loading branch information
dmgav committed Nov 19, 2020
1 parent 9797662 commit f0583ed
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 18 deletions.
30 changes: 21 additions & 9 deletions startup/BMM/xspress3_1element.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,14 @@
import itertools, os, json

import matplotlib.pyplot as plt
from IPython import get_ipython
user_ns = get_ipython().user_ns

try:
from bluesky_queueserver.manager.profile_tools import set_user_ns
except ModuleNotFoundError:
from ._set_user_ns import set_user_ns

# from IPython import get_ipython
# user_ns = get_ipython().user_ns

from BMM.db import file_resource
from BMM.functions import error_msg, warning_msg, go_msg, url_msg, bold_msg, verbosebold_msg, list_msg, disconnected_msg, info_msg, whisper
Expand Down Expand Up @@ -60,7 +66,11 @@ def restart(self):
def set_rois(self):
'''Read ROI values from a JSON serialization on disk and set all 16 ROIs for channel8.
'''
startup_dir = get_ipython().profile_dir.startup_dir
# startup_dir = get_ipython().profile_dir.startup_dir

# Find path based on the location of the current file instead of using ipython
startup_dir = os.path.split(os.path.split(__file__)[0])[0]

with open(os.path.join(startup_dir, 'rois.json'), 'r') as fl:
js = fl.read()
allrois = json.loads(js)
Expand All @@ -76,8 +86,8 @@ def set_rois(self):
self.set_roi_channel(channel=8, index=i+1, name=f'{el.capitalize()}8', low=allrois[el][edge]['low'], high=allrois[el][edge]['high'])


def measure_roi(self):
@set_user_ns
def measure_roi(self, *, user_ns):
'''Hint the ROI currently in use for XAS
'''
BMMuser = user_ns['BMMuser']
Expand All @@ -90,8 +100,8 @@ def measure_roi(self):
else:
this.value.kind = 'omitted'


def plot(self, uid=None, add=False):
@set_user_ns
def plot(self, uid=None, add=False, *, user_ns):
'''Make a plot appropriate for the 4-element detector.
Parameters
Expand Down Expand Up @@ -123,7 +133,8 @@ def plot(self, uid=None, add=False):
plt.plot(e, s1, label='channel 8')
plt.legend()

def table(self):
@set_user_ns
def table(self, *, user_ns):
'''Pretty print a table of values for each ROI.
'''
BMMuser = user_ns['BMMuser']
Expand All @@ -146,7 +157,8 @@ def table(self):
print(f" {int(getattr(getattr(self, f'channel{c}').rois, f'roi{r:02}').value.get()):7} ", end='')
print('')

def to_xdi(self, filename=None):
@set_user_ns
def to_xdi(self, filename=None, *, user_ns):
'''Write an XDI-style file with bin energy in the first column and the
waveform of the measurement channel in the second column.
Expand Down
32 changes: 23 additions & 9 deletions startup/BMM/xspress3_4element.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,14 @@
import itertools, os, json

import matplotlib.pyplot as plt
from IPython import get_ipython
user_ns = get_ipython().user_ns

try:
from bluesky_queueserver.manager.profile_tools import set_user_ns
except ModuleNotFoundError:
from ._set_user_ns import set_user_ns

# from IPython import get_ipython
# user_ns = get_ipython().user_ns

from BMM.db import file_resource
from BMM.functions import error_msg, warning_msg, go_msg, url_msg, bold_msg, verbosebold_msg, list_msg, disconnected_msg, info_msg, whisper
Expand Down Expand Up @@ -83,7 +89,11 @@ def restart(self):
def set_rois(self):
'''Read ROI values from a JSON serialization on disk and set all 16 ROIs for channels 1-4.
'''
startup_dir = get_ipython().profile_dir.startup_dir
# startup_dir = get_ipython().profile_dir.startup_dir

# Find path based on the location of the current file instead of using ipython
startup_dir = os.path.split(os.path.split(__file__)[0])[0]

with open(os.path.join(startup_dir, 'rois.json'), 'r') as fl:
js = fl.read()
allrois = json.loads(js)
Expand All @@ -99,8 +109,9 @@ def set_rois(self):
edge = 'l3'
for ch in range(1,5):
self.set_roi_channel(channel=ch, index=i+1, name=f'{el.capitalize()}{ch}', low=allrois[el][edge]['low'], high=allrois[el][edge]['high'])

def measure_roi(self):

@set_user_ns
def measure_roi(self, *, user_ns):
'''Hint the ROI currently in use for XAS
'''
BMMuser = user_ns['BMMuser']
Expand All @@ -115,7 +126,8 @@ def measure_roi(self):
else:
this.value.kind = 'omitted'

def plot(self, uid=None, add=False, only=None):
@set_user_ns
def plot(self, uid=None, add=False, only=None, *, user_ns):
'''Make a plot appropriate for the 4-element detector.
The default is to overplot the four channels.
Expand Down Expand Up @@ -169,8 +181,9 @@ def plot(self, uid=None, add=False, only=None):
plt.plot(e, s3, label='channel 3')
plt.plot(e, s4, label='channel 4')
plt.legend()

def table(self):

@set_user_ns
def table(self, *, user_ns):
'''Pretty print a table of values for each ROI and for all four channels.
'''
BMMuser = user_ns['BMMuser']
Expand All @@ -194,7 +207,8 @@ def table(self):
print('')


def to_xdi(self, filename=None):
@set_user_ns
def to_xdi(self, filename=None, *, user_ns):
'''Write an XDI-style file with bin energy in the first column and the
waveform of each of the 4 channels in the other columns.
Expand Down

0 comments on commit f0583ed

Please sign in to comment.