Speed up replotting and add xlim slider

This commit is contained in:
rasmusvt 2022-03-15 21:17:15 +01:00
parent ebb98debff
commit f2902aed3a
2 changed files with 53 additions and 41 deletions

View file

@ -12,11 +12,11 @@ import beamtime.auxillary as aux
import beamtime.plotting as btp
def plot_diffractogram(plot_data, options={}):
def plot_diffractogram(data, options={}):
''' Plots a diffractogram.
Input:
plot_data (dict): Must include path = string to diffractogram data, and plot_kind = (recx, beamline, image)'''
data (dict): Must include path = string to diffractogram data, and plot_kind = (recx, beamline, image)'''
# Update options
required_options = ['x_vals', 'y_vals', 'ylabel', 'xlabel', 'xunit', 'yunit', 'line', 'scatter', 'xlim', 'ylim',
@ -43,11 +43,22 @@ def plot_diffractogram(plot_data, options={}):
options = aux.update_options(options=options, required_options=required_options, default_options=default_options)
if not 'diffractogram' in data.keys():
diffractogram = xrd.io.read_data(data=data)
data['diffractogram'] = diffractogram
else:
diffractogram = data['diffractogram']
if not options['xlim']:
options['xlim'] = [diffractogram[options['x_vals']].min(), diffractogram[options['x_vals']].max()]
# Start inteactive session with ipywidgets
if options['interactive']:
options['interactive'] = False
options['interactive_session_active'] = True
plot_diffractogram_interactive(plot_data=plot_data, options=options)
plot_diffractogram_interactive(data=data, options=options)
return
@ -63,7 +74,7 @@ def plot_diffractogram(plot_data, options={}):
# Prepare plot, and read and process data
fig, ax = btp.prepare_plot(options=options)
# Assign the correct axes
if options['reflections_plot'] or options['reflections_indices']:
@ -82,7 +93,7 @@ def plot_diffractogram(plot_data, options={}):
colours = btp.generate_colours(options['palettes'])
diffractogram = xrd.io.read_data(data=plot_data)
if options['line']:
@ -93,8 +104,7 @@ def plot_diffractogram(plot_data, options={}):
if not options['xlim']:
options['xlim'] = [diffractogram[options['x_vals']].min(), diffractogram[options['x_vals']].max()]
fig, ax = btp.adjust_plot(fig=fig, ax=ax, options=options)
@ -105,13 +115,13 @@ def plot_diffractogram(plot_data, options={}):
options['xlim'] = ax.get_xlim()
for reference, axis in zip(options['reflections_data'], ref_axes):
plot_reflection_table(plot_data=reference, ax=axis, options=options)
plot_reflection_table(data=reference, ax=axis, options=options)
# Print the reflection indices
if options['reflections_indices'] and options['reflections_data']:
options['xlim'] = ax.get_xlim()
for reference in options['reflections_data']:
plot_reflection_indices(plot_data=reference, ax=indices_ax, options=options)
plot_reflection_indices(data=reference, ax=indices_ax, options=options)
return diffractogram, fig, ax
@ -133,28 +143,30 @@ def determine_grid_layout(options):
def plot_diffractogram_interactive(plot_data, options):
def plot_diffractogram_interactive(data, options):
if options['reflections_data']:
w = widgets.interactive(btp.ipywidgets_update, func=widgets.fixed(plot_diffractogram), plot_data=widgets.fixed(plot_data), options=widgets.fixed(options),
w = widgets.interactive(btp.ipywidgets_update, func=widgets.fixed(plot_diffractogram), data=widgets.fixed(data), options=widgets.fixed(options),
scatter=widgets.ToggleButton(value=False),
line=widgets.ToggleButton(value=True),
reflections_plot=widgets.ToggleButton(value=True),
reflections_indices=widgets.ToggleButton(value=False))
reflections_indices=widgets.ToggleButton(value=False),
xlim=widgets.IntRangeSlider(value=options['xlim'], min=options['xlim'][0], max=options['xlim'][1], step=1, description='xlim', layout=widgets.Layout(width='1000px')))
else:
w = widgets.interactive(btp.ipywidgets_update, func=widgets.fixed(plot_diffractogram), plot_data=widgets.fixed(plot_data), options=widgets.fixed(options),
w = widgets.interactive(btp.ipywidgets_update, func=widgets.fixed(plot_diffractogram), data=widgets.fixed(data), options=widgets.fixed(options),
scatter=widgets.ToggleButton(value=False),
line=widgets.ToggleButton(value=True))
line=widgets.ToggleButton(value=True),
xlim=widgets.IntRangeSlider(value=options['xlim'], min=options['xlim'][0], max=options['xlim'][1], step=1, description='xlim'))
display(w)
def plot_reflection_indices(plot_data, ax, options={}):
def plot_reflection_indices(data, ax, options={}):
''' Print reflection indices from output generated by VESTA.
Required contents of plot_data:
Required contents of data:
path (str): relative path to reflection table file'''
required_options = ['reflection_indices', 'text_colour', 'hide_indices']
@ -165,17 +177,17 @@ def plot_reflection_indices(plot_data, ax, options={}):
'hide_indices': False
}
plot_data = update_options(options=plot_data, required_options=required_options, default_options=default_options)
data = update_options(options=data, required_options=required_options, default_options=default_options)
if not plot_data['hide_indices']:
reflection_table = xrd.io.load_reflection_table(plot_data['path'])
if not data['hide_indices']:
reflection_table = xrd.io.load_reflection_table(data['path'])
if plot_data['reflection_indices'] > 0:
if data['reflection_indices'] > 0:
reflection_indices = reflection_table.nlargest(options['reflection_indices'], 'I')
for i in range(plot_data['reflection_indices']):
ax.text(s=f'({reflection_indices["h"].iloc[i]} {reflection_indices["k"].iloc[i]} {reflection_indices["l"].iloc[i]})', x=reflection_indices['2th'].iloc[i], y=0, fontsize=2.5, rotation=90, va='bottom', ha='center', c=plot_data['text_colour'])
for i in range(data['reflection_indices']):
ax.text(s=f'({reflection_indices["h"].iloc[i]} {reflection_indices["k"].iloc[i]} {reflection_indices["l"].iloc[i]})', x=reflection_indices['2th'].iloc[i], y=0, fontsize=2.5, rotation=90, va='bottom', ha='center', c=data['text_colour'])
if options['xlim']:
@ -186,10 +198,10 @@ def plot_reflection_indices(plot_data, ax, options={}):
return
def plot_reflection_table(plot_data, ax=None, options={}):
def plot_reflection_table(data, ax=None, options={}):
''' Plots a reflection table from output generated by VESTA.
Required contents of plot_data:
Required contents of data:
path (str): relative path to reflection table file'''
required_options = ['reflection_indices', 'reflections_colour', 'min_alpha', 'format_params', 'rc_params', 'label']
@ -203,14 +215,14 @@ def plot_reflection_table(plot_data, ax=None, options={}):
'label': None
}
if 'colour' in plot_data.keys():
options['reflections_colour'] = plot_data['colour']
if 'min_alpha' in plot_data.keys():
options['min_alpha'] = plot_data['min_alpha']
if 'reflection_indices' in plot_data.keys():
options['reflection_indices'] = plot_data['reflection_indices']
if 'label' in plot_data.keys():
options['label'] = plot_data['label']
if 'colour' in data.keys():
options['reflections_colour'] = data['colour']
if 'min_alpha' in data.keys():
options['min_alpha'] = data['min_alpha']
if 'reflection_indices' in data.keys():
options['reflection_indices'] = data['reflection_indices']
if 'label' in data.keys():
options['label'] = data['label']
options = aux.update_options(options=options, required_options=required_options, default_options=default_options)
@ -219,7 +231,7 @@ def plot_reflection_table(plot_data, ax=None, options={}):
if not ax:
_, ax = btp.prepare_plot(options)
reflection_table = xrd.io.load_reflection_table(plot_data['path'])
reflection_table = xrd.io.load_reflection_table(data['path'])
reflections, intensities = reflection_table['2th'], reflection_table['I']
@ -251,7 +263,7 @@ def plot_reflection_table(plot_data, ax=None, options={}):
xlim_range = ax.get_xlim()[1] - ax.get_xlim()[0]
ylim_avg = (ax.get_ylim()[0]+ax.get_ylim()[1])/2
ax.text(s=plot_data['label'], x=(ax.get_xlim()[0]-0.01*xlim_range), y=ylim_avg, ha = 'right', va = 'center')
ax.text(s=data['label'], x=(ax.get_xlim()[0]-0.01*xlim_range), y=ylim_avg, ha = 'right', va = 'center')