Add offset of x- and y-values for stacked diffractograms

This commit is contained in:
rasmusvt 2022-03-23 15:31:47 +01:00
parent 0fb8883d19
commit 2424d89156
2 changed files with 52 additions and 12 deletions

View file

@ -45,6 +45,7 @@ def integrate_1d(data, options={}, index=0):
default_options = { default_options = {
'unit': '2th_deg', 'unit': '2th_deg',
'nbins': 3000, 'nbins': 3000,
'extract_folder': 'tmp',
'save': False, 'save': False,
'save_filename': None, 'save_filename': None,
'save_extension': '_integrated.xy', 'save_extension': '_integrated.xy',
@ -68,8 +69,8 @@ def integrate_1d(data, options={}, index=0):
filename = make_filename(options=options, path=data['path'][index]) filename = make_filename(options=options, path=data['path'][index])
# Make save_folder if this does not exist already # Make save_folder if this does not exist already
if not os.path.isdir(options['save_folder']): if not os.path.isdir(options['extract_folder']):
os.makedirs(options['save_folder']) os.makedirs(options['extract_folder'])
res = ai.integrate1d(data['image'], options['nbins'], unit=options['unit'], filename=filename) res = ai.integrate1d(data['image'], options['nbins'], unit=options['unit'], filename=filename)
@ -79,7 +80,11 @@ def integrate_1d(data, options={}, index=0):
if not options['save']: if not options['save']:
os.remove(filename) os.remove(filename)
shutil.rmtree('tmp') shutil.rmtree(f'tmp')
# Reset this option
options['save_folder'] = None
return diffractogram, wavelength return diffractogram, wavelength
@ -89,8 +94,7 @@ def make_filename(options, path=None):
# Define save location for integrated diffractogram data # Define save location for integrated diffractogram data
if not options['save']: if not options['save']:
options['save_folder'] = 'tmp' filename = os.path.join(options['extract_folder'], 'tmp_diff.dat')
filename = os.path.join(options['save_folder'], 'tmp_diff.dat')
elif options['save']: elif options['save']:
@ -183,9 +187,12 @@ def view_integrator(calibrant):
def read_brml(data, options={}, index=0): def read_brml(data, options={}, index=0):
# FIXME: Can't read RECX1-data, apparently must be formatted differently from RECX2. Check the RawData-files and compare between the two files.
required_options = ['extract_folder', 'save_folder'] required_options = ['extract_folder', 'save_folder']
default_options = { default_options = {
'extract_folder': 'tmp/', 'extract_folder': 'tmp',
'save_folder': None 'save_folder': None
} }
@ -304,12 +311,41 @@ def read_data(data, options={}, index=0):
diffractogram['I'] = diffractogram['I'] / diffractogram['I'].max() diffractogram['I'] = diffractogram['I'] / diffractogram['I'].max()
if options['offset']:
diffractogram = apply_offset(diffractogram, wavelength, index, options)
diffractogram = translate_wavelengths(data=diffractogram, wavelength=wavelength) diffractogram = translate_wavelengths(data=diffractogram, wavelength=wavelength)
return diffractogram, wavelength return diffractogram, wavelength
def apply_offset(diffractogram, wavelength, index, options):
#Apply offset along y-axis
diffractogram['I_org'] = diffractogram['I'] # make copy of original intensities
diffractogram['I'] = diffractogram['I'] + index*options['offset_y']
# Apply offset along x-axis
relative_shift = (wavelength / 1.54059)*options['offset_x'] # Adjusts the offset-factor to account for wavelength, so that offset_x given is given in 2th_cuka-units
diffractogram['2th_org'] = diffractogram['2th']
diffractogram['2th'] = diffractogram['2th'] + index*relative_shift
return diffractogram
def revert_offset(diffractogram,which=None):
if which == 'both':
diffractogram['2th'] = diffractogram['2th_org']
diffractogram['I'] = diffractogram['I_org']
if which == 'y':
diffractogram['I'] = diffractogram['I_org']
if which == 'x':
diffractogram['2th'] = diffractogram['2th_org']
return diffractogram
def load_reflection_table(data, options={}): def load_reflection_table(data, options={}):

View file

@ -19,7 +19,7 @@ def plot_diffractogram(data, options={}):
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 # Update options
required_options = ['x_vals', 'y_vals', 'ylabel', 'xlabel', 'xunit', 'yunit', 'line', 'scatter', 'xlim', 'ylim', 'normalise', required_options = ['x_vals', 'y_vals', 'ylabel', 'xlabel', 'xunit', 'yunit', 'line', 'scatter', 'xlim', 'ylim', 'normalise', 'offset', 'offset_x', 'offset_y',
'reflections_plot', 'reflections_indices', 'reflections_data', 'plot_kind', 'palettes', 'interactive', 'rc_params', 'format_params'] 'reflections_plot', 'reflections_indices', 'reflections_data', 'plot_kind', 'palettes', 'interactive', 'rc_params', 'format_params']
default_options = { default_options = {
@ -29,6 +29,9 @@ def plot_diffractogram(data, options={}):
'xunit': 'deg', 'yunit': 'a.u.', 'xunit': 'deg', 'yunit': 'a.u.',
'xlim': None, 'ylim': None, 'xlim': None, 'ylim': None,
'normalise': True, 'normalise': True,
'offset': True,
'offset_x': 0,
'offset_y': 1,
'line': True, # whether or not to plot diffractogram as a line plot 'line': True, # whether or not to plot diffractogram as a line plot
'scatter': False, # whether or not to plot individual data points 'scatter': False, # whether or not to plot individual data points
'reflections_plot': False, # whether to plot reflections as a plot 'reflections_plot': False, # whether to plot reflections as a plot
@ -49,6 +52,8 @@ def plot_diffractogram(data, options={}):
data['path'] = [data['path']] data['path'] = [data['path']]
# Check if there is some data stored already, load in data if not. This speeds up replotting in interactive mode.
if not 'diffractogram' in data.keys(): if not 'diffractogram' in data.keys():
# Initialise empty list for diffractograms and wavelengths # Initialise empty list for diffractograms and wavelengths
data['diffractogram'] = [None for _ in range(len(data['path']))] data['diffractogram'] = [None for _ in range(len(data['path']))]
@ -72,7 +77,7 @@ def plot_diffractogram(data, options={}):
options['xlim'] = [diffractogram[options['x_vals']].min(), diffractogram[options['x_vals']].max()] options['xlim'] = [diffractogram[options['x_vals']].min(), diffractogram[options['x_vals']].max()]
# Start inteactive session with ipywidgets # Start inteactive session with ipywidgets. Disables options['interactive'] in order for the interactive loop to not start another interactive session
if options['interactive']: if options['interactive']:
options['interactive'] = False options['interactive'] = False
options['interactive_session_active'] = True options['interactive_session_active'] = True
@ -91,7 +96,6 @@ def plot_diffractogram(data, options={}):
# Prepare plot, and read and process data # Prepare plot, and read and process data
fig, ax = btp.prepare_plot(options=options) fig, ax = btp.prepare_plot(options=options)
@ -125,7 +129,7 @@ def plot_diffractogram(data, options={}):
# Make the reflection plots # Make the reflection plots. By default, the wavelength of the first diffractogram will be used for these.
if options['reflections_plot'] and options['reflections_data']: if options['reflections_plot'] and options['reflections_data']:
options['xlim'] = ax.get_xlim() options['xlim'] = ax.get_xlim()
options['to_wavelength'] = data['wavelength'][0] options['to_wavelength'] = data['wavelength'][0]
@ -133,7 +137,7 @@ def plot_diffractogram(data, options={}):
for reference, axis in zip(options['reflections_data'], ref_axes): for reference, axis in zip(options['reflections_data'], ref_axes):
plot_reflection_table(data=reference, ax=axis, options=options) plot_reflection_table(data=reference, ax=axis, options=options)
# Print the reflection indices # Print the reflection indices. By default, the wavelength of the first diffractogram will be used for this.
if options['reflections_indices'] and options['reflections_data']: if options['reflections_indices'] and options['reflections_data']:
options['xlim'] = ax.get_xlim() options['xlim'] = ax.get_xlim()
options['to_wavelength'] = data['wavelength'][0] options['to_wavelength'] = data['wavelength'][0]