Translate relfections to heatmap x-coords

This commit is contained in:
rasmusvt 2022-04-06 13:42:34 +02:00
parent dd7f2d9dea
commit 6372603324
2 changed files with 49 additions and 30 deletions

View file

@ -400,7 +400,7 @@ def revert_offset(diffractogram,which=None):
return diffractogram return diffractogram
def load_reflection_table(data, options={}): def load_reflection_table(data: dict, reflections_params: dict, options={}):
required_options = ['ref_wavelength', 'to_wavelength'] required_options = ['ref_wavelength', 'to_wavelength']
@ -413,12 +413,12 @@ def load_reflection_table(data, options={}):
# VESTA outputs the file with a header that has a space between the parameter and units - so there is some extra code to rectify the issue # VESTA outputs the file with a header that has a space between the parameter and units - so there is some extra code to rectify the issue
# that ensues from this formatting # that ensues from this formatting
reflections = pd.read_csv(data['path'], delim_whitespace=True) reflections = pd.read_csv(reflections_params['path'], delim_whitespace=True)
# Remove the extra column that appears from the headers issue # Remove the extra column that appears from the headers issue
reflections.drop(reflections.columns[-1], axis=1, inplace=True) reflections.drop(reflections.columns[-1], axis=1, inplace=True)
with open(data['path'], 'r') as f: with open(reflections_params['path'], 'r') as f:
line = f.readline() line = f.readline()
headers = line.split() headers = line.split()
@ -434,13 +434,28 @@ def load_reflection_table(data, options={}):
reflections = translate_wavelengths(data=reflections, wavelength=options['ref_wavelength'], to_wavelength=options['to_wavelength']) reflections = translate_wavelengths(data=reflections, wavelength=options['ref_wavelength'], to_wavelength=options['to_wavelength'])
#print(reflections) if 'heatmap' in data.keys():
start_2th, stop_2th = data['diffractogram'][0]['2th'].min(), data['diffractogram'][0]['2th'].max()
len_2th = stop_2th - start_2th
#print(start_2th, stop_2th, len_2th)
start_heatmap, stop_heatmap = 0, data['heatmap'].shape[1]
len_heatmap = stop_heatmap - start_heatmap
#print(start_heatmap, stop_heatmap, len_heatmap)
scale = len_heatmap/len_2th
#print(scale)
#print(stop_2th * scale)
reflections['heatmap'] = (reflections['2th']-start_2th) * scale
return reflections return reflections
def translate_wavelengths(data, wavelength, to_wavelength=None): def translate_wavelengths(data: pd.DataFrame, wavelength: float, to_wavelength=None) -> pd.DataFrame:
# FIXME Somewhere here there is an invalid arcsin-argument. Not sure where. # FIXME Somewhere here there is an invalid arcsin-argument. Not sure where.
pd.options.mode.chained_assignment = None pd.options.mode.chained_assignment = None

View file

@ -80,6 +80,8 @@ def plot_diffractogram(data, options={}):
# Generate heatmap data # Generate heatmap data
data['heatmap'], data['heatmap_xticks'], data['heatmap_xticklabels'] = generate_heatmap(data=data, options=options) data['heatmap'], data['heatmap_xticks'], data['heatmap_xticklabels'] = generate_heatmap(data=data, options=options)
options['heatmap_loaded'] = True
if options['heatmap']: if options['heatmap']:
options['xlim'] = options['heatmap_xlim'] options['xlim'] = options['heatmap_xlim']
@ -163,16 +165,16 @@ def plot_diffractogram(data, options={}):
options['xlim'] = ax.get_xlim() options['xlim'] = ax.get_xlim()
options['to_wavelength'] = data['wavelength'][0] options['to_wavelength'] = data['wavelength'][0]
for reference, axis in zip(options['reflections_data'], ref_axes): for reflections_params, axis in zip(options['reflections_data'], ref_axes):
plot_reflection_table(data=reference, ax=axis, options=options) plot_reflection_table(data=data, reflections_params=reflections_params, ax=axis, options=options)
# Print the reflection indices. By default, the wavelength of the first diffractogram will be used for this. # 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]
for reference in options['reflections_data']: for reflections_params in options['reflections_data']:
plot_reflection_indices(data=reference, ax=indices_ax, options=options) plot_reflection_indices(data=data, reflections_params=reflections_params, ax=indices_ax, options=options)
if options['interactive_session_active']: if options['interactive_session_active']:
@ -533,7 +535,7 @@ def update_widgets(data, options):
def plot_reflection_indices(data, ax, options={}): def plot_reflection_indices(data, reflections_params, ax, options={}):
''' Print reflection indices from output generated by VESTA. ''' Print reflection indices from output generated by VESTA.
Required contents of data: Required contents of data:
@ -547,20 +549,21 @@ def plot_reflection_indices(data, ax, options={}):
'hide_indices': False 'hide_indices': False
} }
data = aux.update_options(options=data, required_options=required_options, default_options=default_options) reflections_params = aux.update_options(options=reflections_params, required_options=required_options, default_options=default_options)
if not data['hide_indices']: if not reflections_params['hide_indices']:
reflection_table = xrd.io.load_reflection_table(data=data, options=options) reflection_table = xrd.io.load_reflection_table(data=data, reflections_params=reflections_params, options=options)
if data['reflection_indices'] > 0: if reflections_params['reflection_indices'] > 0:
# Get the data['reflection_indices'] number of highest reflections within the subrange options['xlim'] # Get the data['reflection_indices'] number of highest reflections within the subrange options['xlim']
reflection_indices = reflection_table.loc[(reflection_table[options['x_vals']] > options['xlim'][0]) & (reflection_table[options['x_vals']] < options['xlim'][1])].nlargest(options['reflection_indices'], 'I') x_vals = 'heatmap' if options['heatmap'] else options['x_vals']
reflection_indices = reflection_table.loc[(reflection_table[x_vals] > options['xlim'][0]) & (reflection_table[x_vals] < options['xlim'][1])].nlargest(options['reflection_indices'], 'I')
# Plot the indices # Plot the indices
for i in range(data['reflection_indices']): for i in range(reflections_params['reflection_indices']):
if reflection_indices.shape[0] > i: if reflection_indices.shape[0] > i:
ax.text(s=f'({reflection_indices["h"].iloc[i]} {reflection_indices["k"].iloc[i]} {reflection_indices["l"].iloc[i]})', x=reflection_indices[options['x_vals']].iloc[i], y=0, fontsize=2.5, rotation=90, va='bottom', ha='center', c=data['text_colour']) ax.text(s=f'({reflection_indices["h"].iloc[i]} {reflection_indices["k"].iloc[i]} {reflection_indices["l"].iloc[i]})', x=reflection_indices[x_vals].iloc[i], y=0, fontsize=2.5, rotation=90, va='bottom', ha='center', c=reflections_params['text_colour'])
if options['xlim']: if options['xlim']:
@ -571,7 +574,7 @@ def plot_reflection_indices(data, ax, options={}):
return return
def plot_reflection_table(data, ax=None, options={}): def plot_reflection_table(data, reflections_params, ax=None, options={}):
''' Plots a reflection table from output generated by VESTA. ''' Plots a reflection table from output generated by VESTA.
Required contents of data: Required contents of data:
@ -590,15 +593,15 @@ def plot_reflection_table(data, ax=None, options={}):
} }
if 'colour' in data.keys(): if 'colour' in data.keys():
options['reflections_colour'] = data['colour'] options['reflections_colour'] = reflections_params['colour']
if 'min_alpha' in data.keys(): if 'min_alpha' in reflections_params.keys():
options['min_alpha'] = data['min_alpha'] options['min_alpha'] = reflections_params['min_alpha']
if 'reflection_indices' in data.keys(): if 'reflection_indices' in reflections_params.keys():
options['reflection_indices'] = data['reflection_indices'] options['reflection_indices'] = reflections_params['reflection_indices']
if 'label' in data.keys(): if 'label' in reflections_params.keys():
options['label'] = data['label'] options['label'] = reflections_params['label']
if 'wavelength' in data.keys(): if 'wavelength' in reflections_params.keys():
options['wavelength'] = data['wavelength'] options['wavelength'] = reflections_params['wavelength']
options = aux.update_options(options=options, required_options=required_options, default_options=default_options) options = aux.update_options(options=options, required_options=required_options, default_options=default_options)
@ -606,9 +609,10 @@ def plot_reflection_table(data, ax=None, options={}):
if not ax: if not ax:
_, ax = btp.prepare_plot(options) _, ax = btp.prepare_plot(options)
reflection_table = xrd.io.load_reflection_table(data=data, options=options) x_vals = 'heatmap' if options['heatmap'] else options['x_vals']
reflections, intensities = reflection_table[options['x_vals']], reflection_table['I'] reflection_table = xrd.io.load_reflection_table(data=data, reflections_params=reflections_params, options=options)
reflections, intensities = reflection_table[x_vals], reflection_table['I']
@ -638,7 +642,7 @@ def plot_reflection_table(data, ax=None, options={}):
xlim_range = ax.get_xlim()[1] - ax.get_xlim()[0] xlim_range = ax.get_xlim()[1] - ax.get_xlim()[0]
ylim_avg = (ax.get_ylim()[0]+ax.get_ylim()[1])/2 ylim_avg = (ax.get_ylim()[0]+ax.get_ylim()[1])/2
ax.text(s=data['label'], x=(ax.get_xlim()[0]-0.01*xlim_range), y=ylim_avg, ha = 'right', va = 'center') ax.text(s=reflections_params['label'], x=(ax.get_xlim()[0]-0.01*xlim_range), y=ylim_avg, ha = 'right', va = 'center')