From da2b2f855b2002358b94e6533fdf19f5d2dad6dc Mon Sep 17 00:00:00 2001 From: rasmusvt Date: Tue, 22 Mar 2022 19:57:21 +0100 Subject: [PATCH 01/12] Change plotting method to allow for multiple diffractograms --- beamtime/plotting.py | 11 ++++++ beamtime/xrd/io.py | 77 ++++++++++++++++++++++----------------- beamtime/xrd/plot.py | 87 +++++++++++++++++++++++++++++++++++--------- 3 files changed, 124 insertions(+), 51 deletions(-) diff --git a/beamtime/plotting.py b/beamtime/plotting.py index 3430a2a..dd9a481 100644 --- a/beamtime/plotting.py +++ b/beamtime/plotting.py @@ -313,6 +313,17 @@ def update_widgets(options): if widget['state'] != options['x_vals']: for arg in widget[f'{options["x_vals"]}_default']: + + # If new min value is larger than previous max, or new max value is smaller than previous min, set the opposite first + if arg == 'min': + if widget[f'{options["x_vals"]}_default']['min'] > getattr(widget['w'], 'max'): + setattr(widget['w'], 'max', widget[f'{options["x_vals"]}_default']['max']) + + elif arg == 'max': + if widget[f'{options["x_vals"]}_default']['max'] < getattr(widget['w'], 'min'): + setattr(widget['w'], 'min', widget[f'{options["x_vals"]}_default']['min']) + + setattr(widget['w'], arg, widget[f'{options["x_vals"]}_default'][arg]) widget['state'] = options['x_vals'] diff --git a/beamtime/xrd/io.py b/beamtime/xrd/io.py index 6b0bb7a..3e29b80 100644 --- a/beamtime/xrd/io.py +++ b/beamtime/xrd/io.py @@ -27,7 +27,7 @@ def get_image_headers(path): return image.header -def integrate_1d(data, options={}): +def integrate_1d(data, options={}, index=0): ''' Integrates an image file to a 1D diffractogram. Required content of data: @@ -51,11 +51,14 @@ def integrate_1d(data, options={}): 'overwrite': False} options = aux.update_options(options=options, required_options=required_options, default_options=default_options) + + if not isinstance(data['path'], list): + data['path'] = [data['path']] # Get image array from filename if not passed if 'image' not in data.keys(): - data['image'] = get_image_array(data['path']) + data['image'] = get_image_array(data['path'][index]) # Instanciate the azimuthal integrator from pyFAI from the calibrant (.poni-file) ai = pyFAI.load(data['calibrant']) @@ -70,18 +73,18 @@ def integrate_1d(data, options={}): res = ai.integrate1d(data['image'], data['nbins'], unit=options['unit'], filename=filename) - data['path'] = filename - diffractogram = read_xy(data=data, options=options) + data['path'][index] = filename + diffractogram, wavelength = read_xy(data=data, options=options) if not options['save']: os.remove(filename) shutil.rmtree('tmp') - return diffractogram + return diffractogram, wavelength -def make_filename(data, options): +def make_filename(options, path=None): # Define save location for integrated diffractogram data if not options['save']: @@ -93,7 +96,7 @@ def make_filename(data, options): # Case 1: No filename is given. if not options['save_filename']: # If a path is given instead of an image array, the path is taken as the trunk of the savename - if data['path']: + if path: # Make filename by joining the save_folder, the filename (with extension deleted) and adding the save_extension filename = os.path.join(options['save_folder'], os.path.split(data['path'])[-1].split('.')[0] + options['save_extension']) else: @@ -176,12 +179,12 @@ def view_integrator(calibrant): -def read_brml(data, options={}): +def read_brml(data, options={}, index=0): required_options = ['extract_folder', 'save_folder'] default_options = { - 'extract_folder': 'tmp', + 'extract_folder': 'tmp/', 'save_folder': None } @@ -194,7 +197,7 @@ def read_brml(data, options={}): # Extract the RawData0.xml file from the brml-file - with zipfile.ZipFile(data['path'], 'r') as brml: + with zipfile.ZipFile(data['path'][index], 'r') as brml: for info in brml.infolist(): if "RawData" in info.filename: brml.extract(info.filename, options['extract_folder']) @@ -233,9 +236,10 @@ def read_brml(data, options={}): diffractogram.append({'2th': twotheta, 'I': intensity}) - if 'wavelength' not in data.keys(): - for chain in root.findall('./FixedInformation/Instrument/PrimaryTracks/TrackInfoData/MountedOptics/InfoData/Tube/WaveLengthAlpha1'): - data['wavelength'] = float(chain.attrib['Value']) + #if 'wavelength' not in data.keys(): + # Find wavelength + for chain in root.findall('./FixedInformation/Instrument/PrimaryTracks/TrackInfoData/MountedOptics/InfoData/Tube/WaveLengthAlpha1'): + wavelength = float(chain.attrib['Value']) diffractogram = pd.DataFrame(diffractogram) @@ -249,15 +253,16 @@ def read_brml(data, options={}): - return diffractogram + return diffractogram, wavelength -def read_xy(data, options={}): +def read_xy(data, options={}, index=0): - if 'wavelength' not in data.keys(): - find_wavelength_from_xy(data=data) + #if 'wavelength' not in data.keys(): + # Get wavelength from scan + wavelength = find_wavelength_from_xy(path=data['path'][index]) - with open(data['path'], 'r') as f: + with open(data['path'][index], 'r') as f: position = 0 current_line = f.readline() @@ -276,37 +281,37 @@ def read_xy(data, options={}): diffractogram.columns = ['2th', 'I', 'sigma'] - return diffractogram + return diffractogram, wavelength -def read_data(data, options={}): +def read_data(data, options={}, index=0): beamline_extensions = ['mar3450', 'edf', 'cbf'] - file_extension = data['path'].split('.')[-1] + file_extension = data['path'][index].split('.')[-1] if file_extension in beamline_extensions: - diffractogram = integrate_1d(data=data, options=options) + diffractogram, wavelength = integrate_1d(data=data, options=options, index=index) elif file_extension == 'brml': - diffractogram = read_brml(data=data, options=options) + diffractogram, wavelength = read_brml(data=data, options=options, index=index) elif file_extension in['xy', 'xye']: - diffractogram = read_xy(data=data, options=options) + diffractogram, wavelength = read_xy(data=data, options=options, index=index) - diffractogram = translate_wavelengths(data=diffractogram, wavelength=data['wavelength']) + diffractogram = translate_wavelengths(data=diffractogram, wavelength=wavelength) - return diffractogram + return diffractogram, wavelength def load_reflection_table(data, options={}): - required_options = ['wavelength', 'to_wavelength'] + required_options = ['ref_wavelength', 'to_wavelength'] default_options = { - 'wavelength': 1.54059, + 'ref_wavelength': 1.54059, 'to_wavelength': None } @@ -333,7 +338,7 @@ def load_reflection_table(data, options={}): # Set the new modified headers as the headers of reflections.columns = headers - reflections = translate_wavelengths(data=reflections, wavelength=options['wavelength'], to_wavelength=options['to_wavelength']) + reflections = translate_wavelengths(data=reflections, wavelength=options['ref_wavelength'], to_wavelength=options['to_wavelength']) #print(reflections) @@ -378,6 +383,7 @@ def translate_wavelengths(data, wavelength, to_wavelength=None): if to_wavelength: + if to_wavelength > cuka: max_2th = 2*np.arcsin(cuka/to_wavelength) * 180/np.pi @@ -395,19 +401,24 @@ def translate_wavelengths(data, wavelength, to_wavelength=None): -def find_wavelength_from_xy(data): +def find_wavelength_from_xy(path): wavelength_dict = {'Cu': 1.54059, 'Mo': 0.71073} - with open(data['path'], 'r') as f: + with open(path, 'r') as f: lines = f.readlines() for line in lines: + # For .xy-files output from EVA if 'Anode' in line: anode = line.split()[8].strip('"') - data['wavelength'] = wavelength_dict[anode] + wavelength = wavelength_dict[anode] + # For .xy-files output from pyFAI integration elif 'Wavelength' in line: - data['wavelength'] = float(line.split()[2])*10**10 + wavelength = float(line.split()[2])*10**10 + + + return wavelength diff --git a/beamtime/xrd/plot.py b/beamtime/xrd/plot.py index daa7eaf..cec2d1e 100644 --- a/beamtime/xrd/plot.py +++ b/beamtime/xrd/plot.py @@ -43,12 +43,25 @@ def plot_diffractogram(data, options={}): options = aux.update_options(options=options, required_options=required_options, default_options=default_options) + # Convert data['path'] to list to allow iteration over this to accommodate both single and multiple diffractograms + if not isinstance(data['path'], list): + data['path'] = [data['path']] + + if not 'diffractogram' in data.keys(): - diffractogram = xrd.io.read_data(data=data, options=options) - data['diffractogram'] = diffractogram + # Initialise empty list for diffractograms and wavelengths + data['diffractogram'] = [None for _ in range(len(data['path']))] + data['wavelength'] = [None for _ in range(len(data['path']))] + + for index in range(len(data['path'])): + diffractogram, wavelength = xrd.io.read_data(data=data, options=options, index=index) + data['diffractogram'][index] = diffractogram + data['wavelength'][index] = wavelength else: - diffractogram = data['diffractogram'] + if not isinstance(data['diffractogram'], list): + data['diffractogram'] = [data['diffractogram']] + data['wavelength'] = [data['wavelength']] # Sets the xlim if this has not bee specified if not options['xlim']: @@ -95,11 +108,12 @@ def plot_diffractogram(data, options={}): colours = btp.generate_colours(options['palettes']) - if options['line']: - diffractogram.plot(x=options['x_vals'], y=options['y_vals'], ax=ax, c=next(colours), zorder=1) + for diffractogram in data['diffractogram']: + if options['line']: + diffractogram.plot(x=options['x_vals'], y=options['y_vals'], ax=ax, c=next(colours), zorder=1) - if options['scatter']: - ax.scatter(x=diffractogram[options['x_vals']], y = diffractogram[options['y_vals']], c=[(1,1,1,0)], edgecolors=[next(colours)], linewidths=plt.rcParams['lines.markeredgewidth'], zorder=2) #, edgecolors=np.array([next(colours)])) + if options['scatter']: + ax.scatter(x=diffractogram[options['x_vals']], y = diffractogram[options['y_vals']], c=[(1,1,1,0)], edgecolors=[next(colours)], linewidths=plt.rcParams['lines.markeredgewidth'], zorder=2) #, edgecolors=np.array([next(colours)])) @@ -110,7 +124,7 @@ def plot_diffractogram(data, options={}): # Make the reflection plots if options['reflections_plot'] and options['reflections_data']: options['xlim'] = ax.get_xlim() - options['to_wavelength'] = data['wavelength'] + options['to_wavelength'] = data['wavelength'][0] for reference, axis in zip(options['reflections_data'], ref_axes): plot_reflection_table(data=reference, ax=axis, options=options) @@ -118,7 +132,7 @@ def plot_diffractogram(data, options={}): # Print the reflection indices if options['reflections_indices'] and options['reflections_data']: options['xlim'] = ax.get_xlim() - options['to_wavelength'] = data['wavelength'] + options['to_wavelength'] = data['wavelength'][0] for reference in options['reflections_data']: plot_reflection_indices(data=reference, ax=indices_ax, options=options) @@ -149,17 +163,54 @@ def determine_grid_layout(options): def plot_diffractogram_interactive(data, options): + + minmax = {'2th': [None, None], '2th_cuka': [None, None], '2th_moka': [None, None], 'd': [None, None], '1/d': [None, None], 'q': [None, None], 'q2': [None, None], 'q4': [None, None]} + + for index, diffractogram in enumerate(data['diffractogram']): + if not minmax['2th'][0] or diffractogram['2th'].min() < minmax['2th'][0]: + minmax['2th'][0] = diffractogram['2th'].min() + min_index = index + + if not minmax['2th'][1] or diffractogram['2th'].max() > minmax['2th'][1]: + minmax['2th'][1] = diffractogram['2th'].max() + max_index = index + + minmax['2th_cuka'][0], minmax['2th_cuka'][1] = data['diffractogram'][min_index]['2th_cuka'].min(), data['diffractogram'][max_index]['2th_cuka'].max() + minmax['2th_moka'][0], minmax['2th_moka'][1] = data['diffractogram'][min_index]['2th_moka'].min(), data['diffractogram'][max_index]['2th_moka'].max() + minmax['d'][0], minmax['d'][1] = data['diffractogram'][max_index]['d'].min(), data['diffractogram'][min_index]['d'].max() + minmax['1/d'][0], minmax['1/d'][1] = data['diffractogram'][min_index]['1/d'].min(), data['diffractogram'][max_index]['1/d'].max() + minmax['q'][0], minmax['q'][1] = data['diffractogram'][min_index]['q'].min(), data['diffractogram'][max_index]['q'].max() + minmax['q2'][0], minmax['q2'][1] = data['diffractogram'][min_index]['q2'].min(), data['diffractogram'][max_index]['q2'].max() + minmax['q4'][0], minmax['q4'][1] = data['diffractogram'][min_index]['q4'].min(), data['diffractogram'][max_index]['q4'].max() + + + + # options['widgets'] = { + # 'xlim': { + # 'w': widgets.FloatRangeSlider(value=[data['diffractogram']['2th'].min(), data['diffractogram']['2th'].max()], min=data['diffractogram']['2th'].min(), max=data['diffractogram']['2th'].max(), step=0.5, layout=widgets.Layout(width='95%')), + # '2th_default': {'min': data['diffractogram']['2th'].min(), 'max': data['diffractogram']['2th'].max(), 'value': [data['diffractogram']['2th'].min(), data['diffractogram']['2th'].max()], 'step': 0.5}, + # '2th_cuka_default': {'min': data['diffractogram']['2th_cuka'].min(), 'max': data['diffractogram']['2th_cuka'].max(), 'value': [data['diffractogram']['2th_cuka'].min(), data['diffractogram']['2th_cuka'].max()], 'step': 0.5}, + # '2th_moka_default': {'min': data['diffractogram']['2th_moka'].min(), 'max': data['diffractogram']['2th_moka'].max(), 'value': [data['diffractogram']['2th_moka'].min(), data['diffractogram']['2th_moka'].max()], 'step': 0.5}, + # 'd_default': {'min': data['diffractogram']['d'].min(), 'max': data['diffractogram']['d'].max(), 'value': [data['diffractogram']['d'].min(), data['diffractogram']['d'].max()], 'step': 0.5}, + # '1/d_default': {'min': data['diffractogram']['1/d'].min(), 'max': data['diffractogram']['1/d'].max(), 'value': [data['diffractogram']['1/d'].min(), data['diffractogram']['1/d'].max()], 'step': 0.5}, + # 'q_default': {'min': data['diffractogram']['q'].min(), 'max': data['diffractogram']['q'].max(), 'value': [data['diffractogram']['q'].min(), data['diffractogram']['q'].max()], 'step': 0.5}, + # 'q2_default': {'min': data['diffractogram']['q2'].min(), 'max': data['diffractogram']['q2'].max(), 'value': [data['diffractogram']['q2'].min(), data['diffractogram']['q2'].max()], 'step': 0.5}, + # 'q4_default': {'min': data['diffractogram']['q4'].min(), 'max': data['diffractogram']['q4'].max(), 'value': [data['diffractogram']['q4'].min(), data['diffractogram']['q4'].max()], 'step': 0.5}, + # 'state': '2th' + # } + # } + options['widgets'] = { 'xlim': { - 'w': widgets.FloatRangeSlider(value=[data['diffractogram']['2th'].min(), data['diffractogram']['2th'].max()], min=data['diffractogram']['2th'].min(), max=data['diffractogram']['2th'].max(), step=0.5, layout=widgets.Layout(width='95%')), - '2th_default': {'min': data['diffractogram']['2th'].min(), 'max': data['diffractogram']['2th'].max(), 'value': [data['diffractogram']['2th'].min(), data['diffractogram']['2th'].max()], 'step': 0.5}, - '2th_cuka_default': {'min': data['diffractogram']['2th_cuka'].min(), 'max': data['diffractogram']['2th_cuka'].max(), 'value': [data['diffractogram']['2th_cuka'].min(), data['diffractogram']['2th_cuka'].max()], 'step': 0.5}, - '2th_moka_default': {'min': data['diffractogram']['2th_moka'].min(), 'max': data['diffractogram']['2th_moka'].max(), 'value': [data['diffractogram']['2th_moka'].min(), data['diffractogram']['2th_moka'].max()], 'step': 0.5}, - 'd_default': {'min': data['diffractogram']['d'].min(), 'max': data['diffractogram']['d'].max(), 'value': [data['diffractogram']['d'].min(), data['diffractogram']['d'].max()], 'step': 0.5}, - '1/d_default': {'min': data['diffractogram']['1/d'].min(), 'max': data['diffractogram']['1/d'].max(), 'value': [data['diffractogram']['1/d'].min(), data['diffractogram']['1/d'].max()], 'step': 0.5}, - 'q_default': {'min': data['diffractogram']['q'].min(), 'max': data['diffractogram']['q'].max(), 'value': [data['diffractogram']['q'].min(), data['diffractogram']['q'].max()], 'step': 0.5}, - 'q2_default': {'min': data['diffractogram']['q2'].min(), 'max': data['diffractogram']['q2'].max(), 'value': [data['diffractogram']['q2'].min(), data['diffractogram']['q2'].max()], 'step': 0.5}, - 'q4_default': {'min': data['diffractogram']['q4'].min(), 'max': data['diffractogram']['q4'].max(), 'value': [data['diffractogram']['q4'].min(), data['diffractogram']['q4'].max()], 'step': 0.5}, + 'w': widgets.FloatRangeSlider(value=[minmax['2th'][0], minmax['2th'][1]], min=minmax['2th'][0], max=minmax['2th'][1], step=0.5, layout=widgets.Layout(width='95%')), + '2th_default': {'min': minmax['2th'][0], 'max': minmax['2th'][1], 'value': [minmax['2th'][0], minmax['2th'][1]], 'step': 0.5}, + '2th_cuka_default': {'min': minmax['2th_cuka'][0], 'max': minmax['2th_cuka'][1], 'value': [minmax['2th_cuka'][0], minmax['2th_cuka'][1]], 'step': 0.5}, + '2th_moka_default': {'min': minmax['2th_moka'][0], 'max': minmax['2th_moka'][1], 'value': [minmax['2th_moka'][0], minmax['2th_moka'][1]], 'step': 0.5}, + 'd_default': {'min': minmax['d'][0], 'max': minmax['d'][1], 'value': [minmax['d'][0], minmax['d'][1]], 'step': 0.5}, + '1/d_default': {'min': minmax['1/d'][0], 'max': minmax['1/d'][1], 'value': [minmax['1/d'][0], minmax['1/d'][1]], 'step': 0.5}, + 'q_default': {'min': minmax['q'][0], 'max': minmax['q'][1], 'value': [minmax['q'][0], minmax['q'][1]], 'step': 0.5}, + 'q2_default': {'min': minmax['q2'][0], 'max': minmax['q2'][1], 'value': [minmax['q2'][0], minmax['q2'][1]], 'step': 0.5}, + 'q4_default': {'min': minmax['q4'][0], 'max': minmax['q4'][1], 'value': [minmax['q4'][0], minmax['q4'][1]], 'step': 0.5}, 'state': '2th' } } From f26153757a9527449d6e082d72bcd25141328af4 Mon Sep 17 00:00:00 2001 From: rasmusvt Date: Tue, 22 Mar 2022 20:03:23 +0100 Subject: [PATCH 02/12] testing this branching stuff --- beamtime/test.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 beamtime/test.txt diff --git a/beamtime/test.txt b/beamtime/test.txt new file mode 100644 index 0000000..30a1328 --- /dev/null +++ b/beamtime/test.txt @@ -0,0 +1 @@ +something something test From b2ee68859a93b41a3d0b85f1bc203896ccadaeb2 Mon Sep 17 00:00:00 2001 From: rasmusvt Date: Tue, 22 Mar 2022 20:08:11 +0100 Subject: [PATCH 03/12] removing test-file --- beamtime/test.txt | 1 - 1 file changed, 1 deletion(-) delete mode 100644 beamtime/test.txt diff --git a/beamtime/test.txt b/beamtime/test.txt deleted file mode 100644 index 30a1328..0000000 --- a/beamtime/test.txt +++ /dev/null @@ -1 +0,0 @@ -something something test From 86a2ae2379dfcc71e5761a215b4ba2179a7b96e6 Mon Sep 17 00:00:00 2001 From: rasmusvt Date: Tue, 22 Mar 2022 20:25:53 +0100 Subject: [PATCH 04/12] Fix bug to iterate correctly through filenames --- beamtime/xrd/io.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/beamtime/xrd/io.py b/beamtime/xrd/io.py index 3e29b80..16991a2 100644 --- a/beamtime/xrd/io.py +++ b/beamtime/xrd/io.py @@ -40,10 +40,11 @@ def integrate_1d(data, options={}, index=0): df: DataFrame contianing 1D diffractogram if option 'return' is True ''' - required_options = ['unit', 'save', 'save_filename', 'save_extension', 'save_folder', 'overwrite'] + required_options = ['unit', 'nbins', 'save', 'save_filename', 'save_extension', 'save_folder', 'overwrite'] default_options = { 'unit': '2th_deg', + 'nbins': 3000, 'save': False, 'save_filename': None, 'save_extension': '_integrated.xy', @@ -64,17 +65,17 @@ def integrate_1d(data, options={}, index=0): ai = pyFAI.load(data['calibrant']) # Determine filename - filename = make_filename(data=data, options=options) + filename = make_filename(options=options, path=data['path'][index]) # Make save_folder if this does not exist already if not os.path.isdir(options['save_folder']): os.makedirs(options['save_folder']) - res = ai.integrate1d(data['image'], data['nbins'], unit=options['unit'], filename=filename) + res = ai.integrate1d(data['image'], options['nbins'], unit=options['unit'], filename=filename) data['path'][index] = filename - diffractogram, wavelength = read_xy(data=data, options=options) + diffractogram, wavelength = read_xy(data=data, options=options, index=index) if not options['save']: os.remove(filename) @@ -98,7 +99,7 @@ def make_filename(options, path=None): # If a path is given instead of an image array, the path is taken as the trunk of the savename if path: # Make filename by joining the save_folder, the filename (with extension deleted) and adding the save_extension - filename = os.path.join(options['save_folder'], os.path.split(data['path'])[-1].split('.')[0] + options['save_extension']) + filename = os.path.join(options['save_folder'], os.path.split(path)[-1].split('.')[0] + options['save_extension']) else: # Make filename just "integrated.dat" in the save_folder filename = os.path.join(options['save_folder'], 'integrated.xy') @@ -403,6 +404,8 @@ def translate_wavelengths(data, wavelength, to_wavelength=None): def find_wavelength_from_xy(path): + print(path) + wavelength_dict = {'Cu': 1.54059, 'Mo': 0.71073} From d31adb9585e95400191d79ca0b2fa5ef441fe969 Mon Sep 17 00:00:00 2001 From: rasmusvt Date: Tue, 22 Mar 2022 20:26:38 +0100 Subject: [PATCH 05/12] Add feature list --- beamtime/feature_list.txt | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 beamtime/feature_list.txt diff --git a/beamtime/feature_list.txt b/beamtime/feature_list.txt new file mode 100644 index 0000000..f4a0efd --- /dev/null +++ b/beamtime/feature_list.txt @@ -0,0 +1,2 @@ +- Must allow for automatic normalisation between different diffractograms, must only happen upon reading data +- From 06753ab6b2d0f685e21929f63dbb762b2008690a Mon Sep 17 00:00:00 2001 From: rasmusvt Date: Wed, 23 Mar 2022 14:20:19 +0100 Subject: [PATCH 06/12] Move minmax-determiniation to own function --- beamtime/xrd/plot.py | 61 +++++++++++++++++++------------------------- 1 file changed, 26 insertions(+), 35 deletions(-) diff --git a/beamtime/xrd/plot.py b/beamtime/xrd/plot.py index cec2d1e..173da03 100644 --- a/beamtime/xrd/plot.py +++ b/beamtime/xrd/plot.py @@ -166,43 +166,12 @@ def plot_diffractogram_interactive(data, options): minmax = {'2th': [None, None], '2th_cuka': [None, None], '2th_moka': [None, None], 'd': [None, None], '1/d': [None, None], 'q': [None, None], 'q2': [None, None], 'q4': [None, None]} - for index, diffractogram in enumerate(data['diffractogram']): - if not minmax['2th'][0] or diffractogram['2th'].min() < minmax['2th'][0]: - minmax['2th'][0] = diffractogram['2th'].min() - min_index = index - - if not minmax['2th'][1] or diffractogram['2th'].max() > minmax['2th'][1]: - minmax['2th'][1] = diffractogram['2th'].max() - max_index = index - - minmax['2th_cuka'][0], minmax['2th_cuka'][1] = data['diffractogram'][min_index]['2th_cuka'].min(), data['diffractogram'][max_index]['2th_cuka'].max() - minmax['2th_moka'][0], minmax['2th_moka'][1] = data['diffractogram'][min_index]['2th_moka'].min(), data['diffractogram'][max_index]['2th_moka'].max() - minmax['d'][0], minmax['d'][1] = data['diffractogram'][max_index]['d'].min(), data['diffractogram'][min_index]['d'].max() - minmax['1/d'][0], minmax['1/d'][1] = data['diffractogram'][min_index]['1/d'].min(), data['diffractogram'][max_index]['1/d'].max() - minmax['q'][0], minmax['q'][1] = data['diffractogram'][min_index]['q'].min(), data['diffractogram'][max_index]['q'].max() - minmax['q2'][0], minmax['q2'][1] = data['diffractogram'][min_index]['q2'].min(), data['diffractogram'][max_index]['q2'].max() - minmax['q4'][0], minmax['q4'][1] = data['diffractogram'][min_index]['q4'].min(), data['diffractogram'][max_index]['q4'].max() - - - - # options['widgets'] = { - # 'xlim': { - # 'w': widgets.FloatRangeSlider(value=[data['diffractogram']['2th'].min(), data['diffractogram']['2th'].max()], min=data['diffractogram']['2th'].min(), max=data['diffractogram']['2th'].max(), step=0.5, layout=widgets.Layout(width='95%')), - # '2th_default': {'min': data['diffractogram']['2th'].min(), 'max': data['diffractogram']['2th'].max(), 'value': [data['diffractogram']['2th'].min(), data['diffractogram']['2th'].max()], 'step': 0.5}, - # '2th_cuka_default': {'min': data['diffractogram']['2th_cuka'].min(), 'max': data['diffractogram']['2th_cuka'].max(), 'value': [data['diffractogram']['2th_cuka'].min(), data['diffractogram']['2th_cuka'].max()], 'step': 0.5}, - # '2th_moka_default': {'min': data['diffractogram']['2th_moka'].min(), 'max': data['diffractogram']['2th_moka'].max(), 'value': [data['diffractogram']['2th_moka'].min(), data['diffractogram']['2th_moka'].max()], 'step': 0.5}, - # 'd_default': {'min': data['diffractogram']['d'].min(), 'max': data['diffractogram']['d'].max(), 'value': [data['diffractogram']['d'].min(), data['diffractogram']['d'].max()], 'step': 0.5}, - # '1/d_default': {'min': data['diffractogram']['1/d'].min(), 'max': data['diffractogram']['1/d'].max(), 'value': [data['diffractogram']['1/d'].min(), data['diffractogram']['1/d'].max()], 'step': 0.5}, - # 'q_default': {'min': data['diffractogram']['q'].min(), 'max': data['diffractogram']['q'].max(), 'value': [data['diffractogram']['q'].min(), data['diffractogram']['q'].max()], 'step': 0.5}, - # 'q2_default': {'min': data['diffractogram']['q2'].min(), 'max': data['diffractogram']['q2'].max(), 'value': [data['diffractogram']['q2'].min(), data['diffractogram']['q2'].max()], 'step': 0.5}, - # 'q4_default': {'min': data['diffractogram']['q4'].min(), 'max': data['diffractogram']['q4'].max(), 'value': [data['diffractogram']['q4'].min(), data['diffractogram']['q4'].max()], 'step': 0.5}, - # 'state': '2th' - # } - # } + update_minmax(minmax, data) options['widgets'] = { 'xlim': { 'w': widgets.FloatRangeSlider(value=[minmax['2th'][0], minmax['2th'][1]], min=minmax['2th'][0], max=minmax['2th'][1], step=0.5, layout=widgets.Layout(width='95%')), + 'state': '2th', '2th_default': {'min': minmax['2th'][0], 'max': minmax['2th'][1], 'value': [minmax['2th'][0], minmax['2th'][1]], 'step': 0.5}, '2th_cuka_default': {'min': minmax['2th_cuka'][0], 'max': minmax['2th_cuka'][1], 'value': [minmax['2th_cuka'][0], minmax['2th_cuka'][1]], 'step': 0.5}, '2th_moka_default': {'min': minmax['2th_moka'][0], 'max': minmax['2th_moka'][1], 'value': [minmax['2th_moka'][0], minmax['2th_moka'][1]], 'step': 0.5}, @@ -210,8 +179,7 @@ def plot_diffractogram_interactive(data, options): '1/d_default': {'min': minmax['1/d'][0], 'max': minmax['1/d'][1], 'value': [minmax['1/d'][0], minmax['1/d'][1]], 'step': 0.5}, 'q_default': {'min': minmax['q'][0], 'max': minmax['q'][1], 'value': [minmax['q'][0], minmax['q'][1]], 'step': 0.5}, 'q2_default': {'min': minmax['q2'][0], 'max': minmax['q2'][1], 'value': [minmax['q2'][0], minmax['q2'][1]], 'step': 0.5}, - 'q4_default': {'min': minmax['q4'][0], 'max': minmax['q4'][1], 'value': [minmax['q4'][0], minmax['q4'][1]], 'step': 0.5}, - 'state': '2th' + 'q4_default': {'min': minmax['q4'][0], 'max': minmax['q4'][1], 'value': [minmax['q4'][0], minmax['q4'][1]], 'step': 0.5} } } @@ -235,6 +203,29 @@ def plot_diffractogram_interactive(data, options): display(w) +def update_minmax(minmax, data): + ''' Finds minimum and maximum values of each column and updates the minmax dictionary to contain the correct values. + + Input: + minmax (dict): contains ''' + + for index, diffractogram in enumerate(data['diffractogram']): + if not minmax['2th'][0] or diffractogram['2th'].min() < minmax['2th'][0]: + minmax['2th'][0] = diffractogram['2th'].min() + min_index = index + + if not minmax['2th'][1] or diffractogram['2th'].max() > minmax['2th'][1]: + minmax['2th'][1] = diffractogram['2th'].max() + max_index = index + + minmax['2th_cuka'][0], minmax['2th_cuka'][1] = data['diffractogram'][min_index]['2th_cuka'].min(), data['diffractogram'][max_index]['2th_cuka'].max() + minmax['2th_moka'][0], minmax['2th_moka'][1] = data['diffractogram'][min_index]['2th_moka'].min(), data['diffractogram'][max_index]['2th_moka'].max() + minmax['d'][0], minmax['d'][1] = data['diffractogram'][max_index]['d'].min(), data['diffractogram'][min_index]['d'].max() # swapped, intended + minmax['1/d'][0], minmax['1/d'][1] = data['diffractogram'][min_index]['1/d'].min(), data['diffractogram'][max_index]['1/d'].max() + minmax['q'][0], minmax['q'][1] = data['diffractogram'][min_index]['q'].min(), data['diffractogram'][max_index]['q'].max() + minmax['q2'][0], minmax['q2'][1] = data['diffractogram'][min_index]['q2'].min(), data['diffractogram'][max_index]['q2'].max() + minmax['q4'][0], minmax['q4'][1] = data['diffractogram'][min_index]['q4'].min(), data['diffractogram'][max_index]['q4'].max() + def update_widgets(options): for widget in options['widgets'].values(): From 0fb8883d198ac6d57ac9a6dde3f2a6ed32084731 Mon Sep 17 00:00:00 2001 From: rasmusvt Date: Wed, 23 Mar 2022 14:26:43 +0100 Subject: [PATCH 07/12] Add normalisation of diffractograms (default: True= --- beamtime/xrd/io.py | 4 ++++ beamtime/xrd/plot.py | 6 +++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/beamtime/xrd/io.py b/beamtime/xrd/io.py index 16991a2..fc05654 100644 --- a/beamtime/xrd/io.py +++ b/beamtime/xrd/io.py @@ -300,6 +300,10 @@ def read_data(data, options={}, index=0): diffractogram, wavelength = read_xy(data=data, options=options, index=index) + if options['normalise']: + diffractogram['I'] = diffractogram['I'] / diffractogram['I'].max() + + diffractogram = translate_wavelengths(data=diffractogram, wavelength=wavelength) return diffractogram, wavelength diff --git a/beamtime/xrd/plot.py b/beamtime/xrd/plot.py index 173da03..d196f2c 100644 --- a/beamtime/xrd/plot.py +++ b/beamtime/xrd/plot.py @@ -19,7 +19,7 @@ def plot_diffractogram(data, options={}): 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', + required_options = ['x_vals', 'y_vals', 'ylabel', 'xlabel', 'xunit', 'yunit', 'line', 'scatter', 'xlim', 'ylim', 'normalise', 'reflections_plot', 'reflections_indices', 'reflections_data', 'plot_kind', 'palettes', 'interactive', 'rc_params', 'format_params'] default_options = { @@ -28,6 +28,7 @@ def plot_diffractogram(data, options={}): 'ylabel': 'Intensity', 'xlabel': '2theta', 'xunit': 'deg', 'yunit': 'a.u.', 'xlim': None, 'ylim': None, + 'normalise': True, 'line': True, # whether or not to plot diffractogram as a line plot 'scatter': False, # whether or not to plot individual data points 'reflections_plot': False, # whether to plot reflections as a plot @@ -55,9 +56,12 @@ def plot_diffractogram(data, options={}): for index in range(len(data['path'])): diffractogram, wavelength = xrd.io.read_data(data=data, options=options, index=index) + data['diffractogram'][index] = diffractogram data['wavelength'][index] = wavelength + + else: if not isinstance(data['diffractogram'], list): data['diffractogram'] = [data['diffractogram']] From 2424d89156adc085a4fb5c2a82ee632469ccbedf Mon Sep 17 00:00:00 2001 From: rasmusvt Date: Wed, 23 Mar 2022 15:31:47 +0100 Subject: [PATCH 08/12] Add offset of x- and y-values for stacked diffractograms --- beamtime/xrd/io.py | 50 +++++++++++++++++++++++++++++++++++++------- beamtime/xrd/plot.py | 14 ++++++++----- 2 files changed, 52 insertions(+), 12 deletions(-) diff --git a/beamtime/xrd/io.py b/beamtime/xrd/io.py index fc05654..263eaae 100644 --- a/beamtime/xrd/io.py +++ b/beamtime/xrd/io.py @@ -45,6 +45,7 @@ def integrate_1d(data, options={}, index=0): default_options = { 'unit': '2th_deg', 'nbins': 3000, + 'extract_folder': 'tmp', 'save': False, 'save_filename': None, 'save_extension': '_integrated.xy', @@ -68,8 +69,8 @@ def integrate_1d(data, options={}, index=0): filename = make_filename(options=options, path=data['path'][index]) # Make save_folder if this does not exist already - if not os.path.isdir(options['save_folder']): - os.makedirs(options['save_folder']) + if not os.path.isdir(options['extract_folder']): + os.makedirs(options['extract_folder']) 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']: os.remove(filename) - shutil.rmtree('tmp') + shutil.rmtree(f'tmp') + + + # Reset this option + options['save_folder'] = None return diffractogram, wavelength @@ -89,8 +94,7 @@ def make_filename(options, path=None): # Define save location for integrated diffractogram data if not options['save']: - options['save_folder'] = 'tmp' - filename = os.path.join(options['save_folder'], 'tmp_diff.dat') + filename = os.path.join(options['extract_folder'], 'tmp_diff.dat') elif options['save']: @@ -183,9 +187,12 @@ def view_integrator(calibrant): 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'] default_options = { - 'extract_folder': 'tmp/', + 'extract_folder': 'tmp', 'save_folder': None } @@ -304,12 +311,41 @@ def read_data(data, options={}, index=0): diffractogram['I'] = diffractogram['I'] / diffractogram['I'].max() + if options['offset']: + diffractogram = apply_offset(diffractogram, wavelength, index, options) + + diffractogram = translate_wavelengths(data=diffractogram, wavelength=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={}): diff --git a/beamtime/xrd/plot.py b/beamtime/xrd/plot.py index d196f2c..17ad70b 100644 --- a/beamtime/xrd/plot.py +++ b/beamtime/xrd/plot.py @@ -19,7 +19,7 @@ def plot_diffractogram(data, options={}): 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', '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'] default_options = { @@ -29,6 +29,9 @@ def plot_diffractogram(data, options={}): 'xunit': 'deg', 'yunit': 'a.u.', 'xlim': None, 'ylim': None, 'normalise': True, + 'offset': True, + 'offset_x': 0, + 'offset_y': 1, 'line': True, # whether or not to plot diffractogram as a line plot 'scatter': False, # whether or not to plot individual data points 'reflections_plot': False, # whether to plot reflections as a plot @@ -49,6 +52,8 @@ def plot_diffractogram(data, options={}): 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(): # Initialise empty list for diffractograms and wavelengths 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()] - # 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']: options['interactive'] = False options['interactive_session_active'] = True @@ -91,7 +96,6 @@ def plot_diffractogram(data, options={}): # Prepare plot, and read and process data - 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']: options['xlim'] = ax.get_xlim() 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): 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']: options['xlim'] = ax.get_xlim() options['to_wavelength'] = data['wavelength'][0] From 606bfc180d13e5a5df3436b3dc08c5368ac6b286 Mon Sep 17 00:00:00 2001 From: rasmusvt Date: Wed, 23 Mar 2022 16:03:34 +0100 Subject: [PATCH 09/12] Add first stab at ylim widget, should be improved --- beamtime/xrd/io.py | 6 +++--- beamtime/xrd/plot.py | 21 ++++++++++++++++++++- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/beamtime/xrd/io.py b/beamtime/xrd/io.py index 263eaae..612cc16 100644 --- a/beamtime/xrd/io.py +++ b/beamtime/xrd/io.py @@ -388,6 +388,8 @@ def load_reflection_table(data, options={}): def translate_wavelengths(data, wavelength, to_wavelength=None): + # FIXME Somewhere here there is an invalid arcsin-argument. Not sure where. + pd.options.mode.chained_assignment = None # Translate to CuKalpha @@ -424,9 +426,7 @@ def translate_wavelengths(data, wavelength, to_wavelength=None): if to_wavelength: - - - if to_wavelength > cuka: + if to_wavelength >= cuka: max_2th = 2*np.arcsin(cuka/to_wavelength) * 180/np.pi else: max_2th = data['2th_cuka'].max() diff --git a/beamtime/xrd/plot.py b/beamtime/xrd/plot.py index 17ad70b..770b5a8 100644 --- a/beamtime/xrd/plot.py +++ b/beamtime/xrd/plot.py @@ -176,6 +176,24 @@ def plot_diffractogram_interactive(data, options): update_minmax(minmax, data) + ymin, ymax = None, None + for index, diffractogram in enumerate(data['diffractogram']): + if not ymin or (ymin > (diffractogram['I'].min())): #+index*options['offset_y'])): + ymin = diffractogram['I'].min()#+index*options['offset_y'] + + if not ymax or (ymax < (diffractogram['I'].max())):#+index*options['offset_y'])): + ymax = diffractogram['I'].max()#+index*options['offset_y'] + print(ymax) + + + ymin_start = ymin - 0.1*ymax + ymax_start = ymax+0.2*ymax + ymin = ymin - 5*ymax + ymax = ymax*5 + + + + options['widgets'] = { 'xlim': { 'w': widgets.FloatRangeSlider(value=[minmax['2th'][0], minmax['2th'][1]], min=minmax['2th'][0], max=minmax['2th'][1], step=0.5, layout=widgets.Layout(width='95%')), @@ -199,7 +217,8 @@ def plot_diffractogram_interactive(data, options): reflections_plot=widgets.ToggleButton(value=True), reflections_indices=widgets.ToggleButton(value=False), x_vals=widgets.Dropdown(options=['2th', 'd', '1/d', 'q', 'q2', 'q4', '2th_cuka', '2th_moka'], value='2th', description='X-values'), - xlim=options['widgets']['xlim']['w']) + xlim=options['widgets']['xlim']['w'], + ylim=widgets.FloatRangeSlider(value=[ymin_start, ymax_start], min=ymin, max=ymax, step=0.5, layout=widgets.Layout(width='95%'))) else: w = widgets.interactive(btp.ipywidgets_update, func=widgets.fixed(plot_diffractogram), data=widgets.fixed(data), options=widgets.fixed(options), From 0fbfd20a74cebea5d14daaf207ff7ba51edae2b0 Mon Sep 17 00:00:00 2001 From: rasmusvt Date: Wed, 30 Mar 2022 16:11:34 +0200 Subject: [PATCH 10/12] Enable reading of CoupledTwoTheta-scans --- beamtime/xrd/io.py | 64 +++++++++++++++++++++++++++++++++++----------- 1 file changed, 49 insertions(+), 15 deletions(-) diff --git a/beamtime/xrd/io.py b/beamtime/xrd/io.py index 612cc16..5aa2fb1 100644 --- a/beamtime/xrd/io.py +++ b/beamtime/xrd/io.py @@ -224,24 +224,56 @@ def read_brml(data, options={}, index=0): for chain in root.findall('./DataRoutes/DataRoute'): - for scantype in chain.findall('ScanInformation/ScanMode'): - if scantype.text == 'StillScan': - if chain.get('Description') == 'Originally measured data.': - for scandata in chain.findall('Datum'): - scandata = scandata.text.split(',') - scandata = [float(i) for i in scandata] - twotheta, intensity = float(scandata[2]), float(scandata[3]) + # Get the scan type to be able to handle different data formats + scantype = chain.findall('ScanInformation')[0].get('VisibleName') - - else: - if chain.get('Description') == 'Originally measured data.': - for scandata in chain.findall('Datum'): + # Check if the chain is the right one to extract the data from + if chain.get('Description') == 'Originally measured data.': + + + if scantype == 'TwoTheta': + for scandata in chain.findall('Datum'): + scandata = scandata.text.split(',') + twotheta, intensity = float(scandata[2]), float(scandata[3]) + + if twotheta > 0: + diffractogram.append({'2th': twotheta, 'I': intensity}) + + elif scantype == 'Coupled TwoTheta/Theta': + for scandata in chain.findall('Datum'): + scandata = scandata.text.split(',') + twotheta, intensity = float(scandata[2]), float(scandata[4]) + + if twotheta > 0: + diffractogram.append({'2th': twotheta, 'I': intensity}) + + elif scantype == 'Still (Eiger2R_500K (1D mode))': + + start = float(chain.findall('ScanInformation/ScaleAxes/ScaleAxisInfo/Start')[0].text) + stop = float(chain.findall('ScanInformation/ScaleAxes/ScaleAxisInfo/Stop')[0].text) + increment = float(chain.findall('ScanInformation/ScaleAxes/ScaleAxisInfo/Increment')[0].text) + + + + for scandata in chain.findall('Datum'): scandata = scandata.text.split(',') - twotheta, intensity = float(scandata[2]), float(scandata[3]) - - if twotheta > 0: - diffractogram.append({'2th': twotheta, 'I': intensity}) + raw = [float(i) for i in scandata] + + intensity = [] + for r in raw: + if r > 600: + intensity.append(r) + + intensity = np.array(intensity) + + + + + twotheta = np.linspace(start, stop, len(intensity)) + + diffractogram = {'2th': twotheta, 'I': intensity} + #if 'wavelength' not in data.keys(): @@ -249,7 +281,9 @@ def read_brml(data, options={}, index=0): for chain in root.findall('./FixedInformation/Instrument/PrimaryTracks/TrackInfoData/MountedOptics/InfoData/Tube/WaveLengthAlpha1'): wavelength = float(chain.attrib['Value']) + diffractogram = pd.DataFrame(diffractogram) + From 223be18c3e451b8be2a962bba126f44c9eab41db Mon Sep 17 00:00:00 2001 From: rasmusvt Date: Wed, 30 Mar 2022 17:40:06 +0200 Subject: [PATCH 11/12] Change default behaviour with 10+ plots --- beamtime/plotting.py | 20 ++++++++++++-------- beamtime/xrd/io.py | 6 ++---- beamtime/xrd/plot.py | 13 ++++++++++--- 3 files changed, 24 insertions(+), 15 deletions(-) diff --git a/beamtime/plotting.py b/beamtime/plotting.py index dd9a481..4e058ac 100644 --- a/beamtime/plotting.py +++ b/beamtime/plotting.py @@ -377,16 +377,20 @@ def update_rc_params(rc_params): plt.rcParams.update({key: rc_params[key]}) -def generate_colours(palettes): +def generate_colours(palettes, kind=None): - # Creates a list of all the colours that is passed in the colour_cycles argument. Then makes cyclic iterables of these. - colour_collection = [] - for palette in palettes: - mod = importlib.import_module("palettable.colorbrewer.%s" % palette[0]) - colour = getattr(mod, palette[1]).mpl_colors - colour_collection = colour_collection + colour + if kind == 'single': + colour_cycle = itertools.cycle(palettes) - colour_cycle = itertools.cycle(colour_collection) + else: + # Creates a list of all the colours that is passed in the colour_cycles argument. Then makes cyclic iterables of these. + colour_collection = [] + for palette in palettes: + mod = importlib.import_module("palettable.colorbrewer.%s" % palette[0]) + colour = getattr(mod, palette[1]).mpl_colors + colour_collection = colour_collection + colour + + colour_cycle = itertools.cycle(colour_collection) return colour_cycle \ No newline at end of file diff --git a/beamtime/xrd/io.py b/beamtime/xrd/io.py index 5aa2fb1..14c0f98 100644 --- a/beamtime/xrd/io.py +++ b/beamtime/xrd/io.py @@ -252,7 +252,7 @@ def read_brml(data, options={}, index=0): start = float(chain.findall('ScanInformation/ScaleAxes/ScaleAxisInfo/Start')[0].text) stop = float(chain.findall('ScanInformation/ScaleAxes/ScaleAxisInfo/Stop')[0].text) - increment = float(chain.findall('ScanInformation/ScaleAxes/ScaleAxisInfo/Increment')[0].text) + @@ -262,7 +262,7 @@ def read_brml(data, options={}, index=0): intensity = [] for r in raw: - if r > 600: + if r > 601: intensity.append(r) intensity = np.array(intensity) @@ -478,9 +478,7 @@ def translate_wavelengths(data, wavelength, to_wavelength=None): def find_wavelength_from_xy(path): - print(path) - wavelength_dict = {'Cu': 1.54059, 'Mo': 0.71073} with open(path, 'r') as f: diff --git a/beamtime/xrd/plot.py b/beamtime/xrd/plot.py index 770b5a8..1a9429c 100644 --- a/beamtime/xrd/plot.py +++ b/beamtime/xrd/plot.py @@ -20,7 +20,7 @@ def plot_diffractogram(data, options={}): # Update options 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', 'interactive_session_active'] default_options = { 'x_vals': '2th', @@ -45,6 +45,10 @@ def plot_diffractogram(data, options={}): 'format_params': {}, } + if 'offset_y' not in options.keys(): + if len(data['path']) > 10: + default_options['offset_y'] = 0.05 + options = aux.update_options(options=options, required_options=required_options, default_options=default_options) # Convert data['path'] to list to allow iteration over this to accommodate both single and multiple diffractograms @@ -62,6 +66,7 @@ def plot_diffractogram(data, options={}): for index in range(len(data['path'])): diffractogram, wavelength = xrd.io.read_data(data=data, options=options, index=index) + data['diffractogram'][index] = diffractogram data['wavelength'][index] = wavelength @@ -113,7 +118,10 @@ def plot_diffractogram(data, options={}): ax = ax[-1] - colours = btp.generate_colours(options['palettes']) + if len(data['path']) < 10: + colours = btp.generate_colours(options['palettes']) + else: + colours = btp.generate_colours(['black'], kind='single') for diffractogram in data['diffractogram']: @@ -183,7 +191,6 @@ def plot_diffractogram_interactive(data, options): if not ymax or (ymax < (diffractogram['I'].max())):#+index*options['offset_y'])): ymax = diffractogram['I'].max()#+index*options['offset_y'] - print(ymax) ymin_start = ymin - 0.1*ymax From 3e4c0a9fc2bbc95b8b6525902727dd30863c30b7 Mon Sep 17 00:00:00 2001 From: rasmusvt Date: Wed, 30 Mar 2022 17:50:41 +0200 Subject: [PATCH 12/12] Add slider for offset_y (not working yet) --- beamtime/xrd/plot.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/beamtime/xrd/plot.py b/beamtime/xrd/plot.py index 1a9429c..78c3906 100644 --- a/beamtime/xrd/plot.py +++ b/beamtime/xrd/plot.py @@ -66,7 +66,6 @@ def plot_diffractogram(data, options={}): for index in range(len(data['path'])): diffractogram, wavelength = xrd.io.read_data(data=data, options=options, index=index) - data['diffractogram'][index] = diffractogram data['wavelength'][index] = wavelength @@ -157,6 +156,8 @@ def plot_diffractogram(data, options={}): if options['interactive_session_active']: btp.update_widgets(options=options) + xrd.io.up + return diffractogram, fig, ax @@ -225,7 +226,9 @@ def plot_diffractogram_interactive(data, options): reflections_indices=widgets.ToggleButton(value=False), x_vals=widgets.Dropdown(options=['2th', 'd', '1/d', 'q', 'q2', 'q4', '2th_cuka', '2th_moka'], value='2th', description='X-values'), xlim=options['widgets']['xlim']['w'], - ylim=widgets.FloatRangeSlider(value=[ymin_start, ymax_start], min=ymin, max=ymax, step=0.5, layout=widgets.Layout(width='95%'))) + ylim=widgets.FloatRangeSlider(value=[ymin_start, ymax_start], min=ymin, max=ymax, step=0.5, layout=widgets.Layout(width='95%')), + offset_y=widgets.FloatSlider(value=options['offset_y'], min=-5, max=5) + ) else: w = widgets.interactive(btp.ipywidgets_update, func=widgets.fixed(plot_diffractogram), data=widgets.fixed(data), options=widgets.fixed(options),