From f2902aed3a97261eaa5e4161c85549ddb8ce3a09 Mon Sep 17 00:00:00 2001 From: rasmusvt Date: Tue, 15 Mar 2022 21:17:15 +0100 Subject: [PATCH] Speed up replotting and add xlim slider --- beamtime/plotting.py | 14 ++++---- beamtime/xrd/plot.py | 80 +++++++++++++++++++++++++------------------- 2 files changed, 53 insertions(+), 41 deletions(-) diff --git a/beamtime/plotting.py b/beamtime/plotting.py index f1c4bbe..c78a989 100644 --- a/beamtime/plotting.py +++ b/beamtime/plotting.py @@ -57,15 +57,15 @@ def prepare_plot(options={}): update_rc_params(rc_params) if not format_params['width']: - width = determine_width(format_params=format_params) + format_params['width'] = determine_width(format_params=format_params) if not format_params['height']: - height = determine_height(format_params=format_params, width=width) + format_params['height'] = determine_height(format_params=format_params, width=format_params['width']) - width, height = scale_figure(format_params=format_params, width=width, height=height) + format_params['width'], format_params['height'] = scale_figure(format_params=format_params, width=format_params['width'], height=format_params['height']) if format_params['nrows'] == 1 and format_params['ncols'] == 1: - fig, ax = plt.subplots(figsize=(width, height), dpi=format_params['dpi']) + fig, ax = plt.subplots(figsize=(format_params['width'], format_params['height']), dpi=format_params['dpi']) return fig, ax @@ -76,7 +76,7 @@ def prepare_plot(options={}): if not format_params['grid_ratio_width']: format_params['grid-ratio_width'] = [1 for i in range(format_params['ncols'])] - fig, axes = plt.subplots(nrows=format_params['nrows'], ncols=format_params['ncols'], figsize=(width,height), + fig, axes = plt.subplots(nrows=format_params['nrows'], ncols=format_params['ncols'], figsize=(format_params['width'],format_params['height']), gridspec_kw={'height_ratios': format_params['grid_ratio_height'], 'width_ratios': format_params['grid_ratio_width']}, facecolor='w', dpi=format_params['dpi']) @@ -287,7 +287,7 @@ def adjust_plot(fig, ax, options): -def ipywidgets_update(func, plot_data, options={}, **kwargs): +def ipywidgets_update(func, data, options={}, **kwargs): ''' A general ipywidgets update function that can be passed to ipywidgets.interactive. To use this, you can run: import ipywidgets as widgets @@ -303,7 +303,7 @@ def ipywidgets_update(func, plot_data, options={}, **kwargs): options[key] = kwargs[key] # Call the function with the plot_data and options-dictionaries - func(plot_data=plot_data, options=options) + func(data=data, options=options) diff --git a/beamtime/xrd/plot.py b/beamtime/xrd/plot.py index 25a91cf..7c89c43 100644 --- a/beamtime/xrd/plot.py +++ b/beamtime/xrd/plot.py @@ -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')