<jupyter_start><jupyter_text>Add title hereThis is a sample file of interactive stopped-flow data analysis. You do NOT need to understand python language to use this program. By replacing file names and options with your own, you can easily produce figures and interactively adjust plotting optinos. It is strongly recommended to keep this file for reference, and make edits on a duplication of this file.   import libraries and define functionsPress Ctrl+Enter to run sections<jupyter_code># import mpld3
# mpld3.enable_notebook()
%matplotlib widget
from sf_utils import *
from uv_utils import *<jupyter_output><empty_output><jupyter_text>compare multiple inputs on selected linesIn many cases, the same trace may be repeated for several times. This<jupyter_code>rcParams['figure.figsize'] = [6, 4.5]
csvfiles = [
    'average-sample-10s-1.csv',
    'average-sample-10s-2.csv',
    'average-sample-10s-3.csv',
]


sfData = SFData.quickload_csv(csvfiles)

sfData.plot_selected_kinetics()
display(widgets.HBox([sfData.add_logx_button(), sfData.plot_scan_wavelength_button()]))
sfData.plot_interactive_buttons_for_kinetics()<jupyter_output><empty_output><jupyter_text>save averaged files<jupyter_code>csvfiles = [
    'average-sample-10s-1.csv',
    'average-sample-10s-2.csv',
    'average-sample-10s-3.csv',
]

save_average(csvfiles)<jupyter_output>averaged file name is: average-sample-10s-ave.csv<jupyter_text>plot full spectra with more options  plot with table<jupyter_code>df = pd.DataFrame(
    columns=['csvfile', 'legend', 'shift', 'scale', 'color', 'timepoint'],
    data=[
        ['average-sample-10s-ave.csv', '0.003s', 0, 1, 'black', 0.003],
        ['average-sample-10s-ave.csv', '0.01s', 0, 1, 'red', 0.01],
        ['average-sample-10s-ave.csv', '0.1s', 0, 1, 'blue', 0.1],
        ['average-sample-10s-ave.csv', '1s', 0, 1, 'orange', 1],
        ['average-sample-10s-ave.csv', '10s', 0, 1, 'green', 10],
    ]
)

base = pd.DataFrame(
    columns=['csvfile', 'timepoint'],
    data=[
        ['average-sample-10s-ave.csv', 0.002],
    ]
)

# plot_from_df(df, valuetype='timepoint') # no subtraction
plot_from_df(df, valuetype='timepoint', base=base) # subtract spectra at certain timepoint<jupyter_output><empty_output><jupyter_text>plot with variables (lower level API)<jupyter_code>rcParams['figure.figsize'] = [6, 4.5]
fig = plt.figure()
axis = fig.gca()

csvfiles = [
    'average-sample-10s-ave.csv',
    'average-sample-10s-ave.csv',
    'average-sample-10s-ave.csv',
    'average-sample-10s-ave.csv',
    'average-sample-10s-ave.csv',
]

dfs = list(map(load_data, csvfiles))
df_base = dfs[0].iloc[1,:]
dfs = [df - df_base for df in dfs]

timepoints = [
    [0.003],
    [0.01],
    [0.1],
    [1],
    [10]
]

legends = [
    ['0.003s'],
    ['0.01s'],
    ['0.1s'],
    ['1s'],
    ['10s']
]

shifts = [
    [0],
    [0],
    [0],
    [0],
    [0]
]

scales = [
    [1],
    [1],
    [1],
    [1],
    [1],
]

colors = [
    ['black'],
    ['red'],
    ['blue'],
    ['orange'],
    ['green']
]

sfData = SFData(
    axis=axis,
    dfs=dfs,
    colors=colors,
    legends=legends,
    scales=scales,
    shifts=shifts,
    xlabel='Time (s)',
    ylabel='Abs',
)

sfData.plot_selected_spectra(timepoints)
# display(widgets.HBox([sfData.plot_scan_timepoint_button()]))
sfData.plot_interactive_buttons_for_spectra()<jupyter_output><empty_output><jupyter_text>plot kinetic curves with more options  plot with table<jupyter_code>df = pd.DataFrame(
    columns=['csvfile', 'legend', 'shift', 'scale', 'color', 'wavelength'],
    data=[
        ['average-sample-10s-ave.csv', '350nm', 0, 1, 'black', 350],
        ['average-sample-10s-ave.csv', '400nm', 0, 1, 'red', 400],
        ['average-sample-10s-ave.csv', '450nm', 0, 1, 'blue', 450],
    ]
)

base = pd.DataFrame(
    columns=['csvfile', 'timepoint'],
    data=[
        ['average-sample-10s-ave.csv', 0.002],
    ]
)

# plot_from_df(df, valuetype='wavelength')   
plot_from_df(df, valuetype='wavelength', base=base)<jupyter_output><empty_output><jupyter_text>plot with variables (lower level API)<jupyter_code>rcParams['figure.figsize'] = [6, 4.5]
fig = plt.figure()
axis = fig.gca()

csvfiles = [
    'average-sample-10s-ave.csv',
    'average-sample-10s-ave.csv',
    'average-sample-10s-ave.csv',
]

dfs = list(map(load_data, csvfiles))

df_base = dfs[0].iloc[1,:]
dfs = [df - df_base for df in dfs]

wavelengths = [
    [350],
    [400],
    [450],
]

legends = [
    ['350 nm'],
    ['400 nm'],
    ['450 nm'],
]

shifts = [
    [0],
    [0],
    [0],
]

scales = [
    [1],
    [1],
    [1],
]

colors = [
    ['black'],
    ['red'],
    ['blue'],
]

sfData = SFData(
    axis=axis,
    dfs=dfs,
    colors=colors,
    legends=legends,
    scales=scales,
    shifts=shifts,
    xlabel='Time (s)',
    ylabel='Abs',
)

sfData.plot_selected_kinetics(wavelengths)
display(sfData.add_logx_button())
# display(widgets.HBoxsfDataData.add_logx_button(), sfData.plot_scan_wavelength_button()]))
sfData.plot_interactive_buttons_for_kinetics()<jupyter_output><empty_output><jupyter_text>overview - kinetic curve and full spectraWarning: this section is slow<jupyter_code>rcParams['figure.figsize'] = [9, 4.5]
csvfile = 'average-sample-10s-ave.csv'
df = load_data(csvfile)
(row, col) = (1, 2)
fig, axs = plt.subplots(row, col, sharex=False, sharey=False)
axis1 = axs[0]  # first axis
axis2 = axs[1]  # second axis

plot_all_kinetic(df, axis1)
plot_all_spectra(df, axis2)<jupyter_output><empty_output><jupyter_text>overview - difference spectraWarning: this section is slow<jupyter_code>rcParams['figure.figsize'] = [9, 4.5]
df = load_data(csvfile)
baseCurve = df.iloc[1,:] # select the second time point as baseline
diffDf = df - baseCurve
df1 = df - baseCurve
(row, col) = (1, 2)
fig, axs = plt.subplots(row, col, sharex=False, sharey=False)
axis1 = axs[0]  # first axis
axis2 = axs[1]  # second axis

plot_all_kinetic(df1, axis1)
plot_all_spectra(df1, axis2)<jupyter_output><empty_output><jupyter_text>export kintek input files<jupyter_code>csvfile = 'kintek-sample-1-10s.csv'
wavelengths = [440]
kintekFileName = 'kintek-sample-1-10s-data.txt'
export_kintek(csvfile, wavelengths, kintekFileName)

csvfile = 'kintek-sample-2-10s.csv'
wavelengths = [440]
kintekFileName = 'kintek-sample-2-10s-data.txt'
export_kintek(csvfile, wavelengths, kintekFileName)<jupyter_output>kintek input file saved as kintek-sample-1-10s-data.txt
kintek input file saved as kintek-sample-2-10s-data.txt<jupyter_text>plot original kinetic and kintek simulation<jupyter_code>rcParams['figure.figsize'] = [6, 4.5]
rcParams.update({'xtick.labelsize': 14})
rcParams.update({'ytick.labelsize': 14})
rcParams.update({'axes.labelsize':16})
rcParams.update({'legend.frameon': False})
rcParams.update({'legend.fontsize': 14})

simfiles = [
    'kintek-sample-1-10s-data.txt',
    'kintek-sample-2-10s-data.txt',
    'kintek-sample-1-10s-sim.txt',
    'kintek-sample-2-10s-sim.txt',
]

dfs = list(map(read_kintek_simulation, simfiles))

df = pd.concat(dfs, axis=1)
df = df[df.index > 0.002] # filter the value range to plot
df = df[df.index < 0.8] # filter the value range to plot

aPlot = AdjustablePlot.quickload_df(df)
aPlot.colors = ['red', '#0080ff', 'black', 'black']
aPlot.shifts = [0.007, 0, 0.007, 0]
aPlot.legends = ['1', '2', '1-sim', '2-sim']
aPlot.plot()

aPlot.axis.set_xscale('log')
aPlot.axis.set_xlim([0.001, 1])
# aPlot.axis.set_ylim([-0.019, 0.029])

for i in range(2):
    line = aPlot.axis.lines[i]
    line.set_marker('.')
    line.set_linewidth(0)
    line.set_markersize(5)

aPlot.axis.lines[-1].set_linestyle('dashed')
aPlot.plot_interactive_buttons()
_ = aPlot.axis.legend().set_draggable(True)
_ = aPlot.axis.set_xlabel('Time (s)')
_ = aPlot.axis.set_ylabel('ΔAbs')<jupyter_output><empty_output><jupyter_text>plot UV-Vis titration data  plot titration<jupyter_code>rcParams['figure.figsize'] = [6, 4.5]

uv_filenames = [
    'titration_and_UV/2.0.CSV', 
    'titration_and_UV/2.1.CSV',
    'titration_and_UV/2.2.CSV',
    'titration_and_UV/2.3.CSV',
    'titration_and_UV/2.4.CSV',
    'titration_and_UV/2.5.CSV',
    'titration_and_UV/2.6.CSV',
    'titration_and_UV/2.7.CSV',
    'titration_and_UV/2.8.CSV',
    'titration_and_UV/2.9.CSV',
]
df = read_multiple_uv_to_df(uv_filenames)
aPlot = AdjustablePlot.quickload_df(df)

aPlot.colors = color_range('red', 'black', len(uv_filenames))
# calculate shift on each spectra to remove baseline floating issue
# aPlot.shifts = shift_to_align_wavelength(df, wavelength=1000)
aPlot.legends =  ['%5.1f eq aKG' % (0.5*i) for i in range(len(uv_filenames))]


aPlot.plot()
aPlot.plot_interactive_buttons()

aPlot.axis.set_xlim([320, 1100])
aPlot.axis.set_ylim([-0.2, 1.2])
aPlot.axis.set_title('Titration: PIsnB + 4Fe + 4TyrNC + n*0.5aKG')
aPlot.axis.set_xlabel('wavelength (nm)')
aPlot.axis.set_ylabel('Abs')<jupyter_output><empty_output><jupyter_text>subtraction<jupyter_code># subtract base
base = aPlot.df.iloc[:,[0]]
aPlot.df = aPlot.df - base.values

# plot in a new figure
aPlot.axis = plt.figure().gca()
aPlot.plot()
aPlot.plot_interactive_buttons()

aPlot.axis.set_xlim([350, 1100])
aPlot.axis.set_ylim([-0.02, 0.15])
aPlot.axis.set_title('Titration: PIsnB + 4Fe + 4TyrNC + n*0.5aKG')
aPlot.axis.set_xlabel('wavelength (nm)')
aPlot.axis.set_ylabel('ΔAbs')<jupyter_output><empty_output><jupyter_text>remove baseline shift<jupyter_code># subtract base
base = aPlot.df.iloc[:,[0]]
aPlot.df = aPlot.df - base.values

# remove baseline shift
aPlot.shifts = shift_to_align_wavelength(aPlot.df, wavelength=800)

# plot in a new figure
aPlot.axis = plt.figure().gca()
aPlot.plot()
aPlot.plot_interactive_buttons()

aPlot.axis.set_xlim([350, 1100])
aPlot.axis.set_ylim([-0.02, 0.12])
aPlot.axis.set_title('Titration: PIsnB + 4Fe + 4TyrNC + n*0.5aKG')
aPlot.axis.set_xlabel('wavelength (nm)')
aPlot.axis.set_ylabel('ΔAbs')<jupyter_output><empty_output><jupyter_text>plot trend at certain x value<jupyter_code>df_trace512 = aPlot.df.iloc[[get_index_of_closest_x_value(aPlot.df, 512)], :].transpose()
df_trace512.index =  [0.5*i for i in range(len(uv_filenames))]
trace512Plot = AdjustablePlot.quickload_df(df_trace512)
trace512Plot.plot()
trace512Plot.plot_interactive_buttons()


trace512Plot.axis.lines[0].set_marker('o')

trace512Plot.axis.legend().set_draggable(True)
trace512Plot.axis.set_xlabel('equivalent of aKG')
trace512Plot.axis.set_ylabel('ΔAbs at 512 nm')<jupyter_output><empty_output><jupyter_text>Appendix: more options<jupyter_code># global settings
# two semantics are equivalent
# more options can be found at https://matplotlib.org/users/customizing.html
# set figure size
rcParams['figure.figsize'] = [9, 4.5]
# set tick pointing inwards or outwards
rcParams['xtick.direction'] = 'in'
rcParams['ytick.direction'] = 'in'
# set visibility of minor ticks
rcParams['xtick.minor.visible'] = True
rcParams['ytick.minor.visible'] = True
# set ticks on top and right axes
rcParams['xtick.top'] = True
rcParams['ytick.right'] = True
# set better layout for multiple plots in a figure
# https://matplotlib.org/3.1.1/tutorials/intermediate/tight_layout_guide.html
rcParams.update({'figure.autolayout': True})
# set x and y label size
rcParams.update({'axes.labelsize': 20})
# set tick label size
rcParams.update({'xtick.labelsize': 12})
rcParams.update({'ytick.labelsize': 12})
# turn on/off frame of legend box
rcParams.update({'legend.frameon': False})
# set legend fontsize
rcParams.update({'legend.fontsize': 10})<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16342378501003402, 'fraction_numerical': 0.039263589564610415, 'mean_word_length': 4.762694821518351, 'pattern_counts': {'":': 0, '<': 64, '<?xml version=': 0, '>': 64, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '16', 'dir': 'jupyter-structured-clean-dedup', 'id': '11514', 'license': 'MIT', 'n_tokens_mistral': 4905, 'n_tokens_neox': 4488, 'n_words': 815, 'path': 'stoppedflow-analysis.ipynb', 'repo_name': 'YijieTang/stoppedflow-data-processor', 'size': '11461'}