<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Chapter 15 :: Coding For Chemists</title>
    <link>https://codingforchemistsbook.com/book_material/chapter-15/index.html</link>
    <description>Data Download Data for Chapter 15&#xA;Alternatively, individual files can be found in the Data section.&#xA;Code from chapter &#39;&#39;&#39; Fit data in an excel file and update the file with the results Requires: existing titration data excel file Written by: Ben Lear and Chris Johnson (authors@codechembook.com) v1.0.0 - 250508 - initial version &#39;&#39;&#39; import numpy as np from lmfit import Model from openpyxl import load_workbook from openpyxl.chart import Reference, Series from openpyxl.styles import PatternFill, Border, Side, Font from codechembook.quickTools import importFromPy, quickOpenFilename, quickSaveFilename, quickPopupMessage importFromPy(&#39;TitrationModel.py&#39;, &#39;pH_response&#39;) # Read the excel file and go to the right worksheet quickPopupMessage(message = &#39;Select the Excel workbook containing the data.&#39;) filename = quickOpenFilename(filetypes = &#39;Excel Workbooks, *.xlsx&#39;) wb = load_workbook(filename) # Load the workbook into Python ws = wb[&#39;data&#39;] # Select a worksheet # Get the data from the columns of the worksheet row by row exp_eqs, exp_pHs = [], [] for eq, pH in zip(ws[&#39;A&#39;], ws[&#39;B&#39;]): try: exp_eqs.append(float(eq.value)) exp_pHs.append(float(pH.value)) except: pass # Set up a new model with the pH_response function and &#39;eqs&#39; as the indpedent variable pH_model = Model(pH_response, independent_vars=[&#39;eqs&#39;]) # Set up the fit parameter with the average pH as the initial guess pH_params = pH_model.make_params() pH_params.add(&#39;pKa&#39;, value = np.mean(exp_pHs)) # specifications for the parameter # Fit the model to the data pH_fit = pH_model.fit(data = exp_pHs, eqs = exp_eqs, params = pH_params, base_i = exp_eqs[0]) # Specify the colors and borders to use in the worksheet font_color = Font(color=&#39;FFFFFF&#39;) pink_fill = PatternFill(start_color=&#39;FF9194&#39;, end_color=&#39;FF9194&#39;, fill_type=&#39;solid&#39;) cell_border = Border(left=Side(style=&#39;thin&#39;), right=Side(style=&#39;thin&#39;), top=Side(style=&#39;thin&#39;), bottom=Side(style=&#39;thin&#39;)) # Label the header cells and format ws[&#39;C1&#39;] = &#39;fit&#39; # column heading ws[&#39;C1&#39;].fill = pink_fill ws[&#39;C1&#39;].border = cell_border ws[&#39;D1&#39;] = &#39;parameter&#39; ws[&#39;E1&#39;] = &#39;value&#39; ws[&#39;F1&#39;] = &#39;uncertainty&#39; # Loop through each value in the best fitting line and add it to the corresponding cell for i, value in enumerate(pH_fit.best_fit): ws[f&#39;C{i+2}&#39;] = round(value, 3) # set the value ws[f&#39;C{i+2}&#39;].number_format = &#39;0.000&#39; # ensure we have three decimal places showing ws[f&#39;C{i+2}&#39;].fill = pink_fill ws[f&#39;C{i+2}&#39;].border = cell_border # Add the best fitting parameters to the corresponding cells and format ws[&#39;E2&#39;] = f&#39;{pH_fit.params[&#34;pKa&#34;].value:.3f}&#39; ws[&#39;F2&#39;] = f&#39;{pH_fit.params[&#34;pKa&#34;].stderr:.3f}&#39; ws[&#39;F2&#39;].font = font_color ws[&#39;F2&#39;].fill = PatternFill(start_color=&#39;51154A&#39;, end_color=&#39;51154A&#39;, fill_type=&#39;solid&#39;) # Get a variable for the chart in the worksheet chart = ws._charts[0] # Create reference to the data to be plotted in the chart x_values = Reference(ws, min_col=1, min_row=2, max_col=1, max_row=len(pH_fit.best_fit)+1) y_values = Reference(ws, min_col=3, min_row=2, max_col=3, max_row=len(pH_fit.best_fit)+1) # Create a data series for the data fit_data = Series(y_values, x_values, title_from_data=False) # Set the data series line color and width fit_data.graphicalProperties.line.solidFill = &#39;FF9194&#39; # Red color fit_data.graphicalProperties.line.width = 28575 # Width in EMUs (1 pt = 12700 EMUs) # Add the fit line to the chart chart.series.append(fit_data) # Save the changes back to the same file quickPopupMessage(message = &#39;Choose a filename to save the new Excel workbook.&#39;) save_filename = quickSaveFilename(filetypes = &#39;Excel Workbooks, *.xlsx&#39;) wb.save(save_filename) Solutions to Exercises Targeted exercises Reading and writing excel files using openpyxl Exercise 1 Go get the ‘Stress-Strain.xlsm’ file from our website (website).</description>
    <generator>Hugo</generator>
    <language>en-us</language>
    <managingEditor>authors@codingforchemistsbook.com (Benjamin Lear and Christopher Johnson)</managingEditor>
    <webMaster>authors@codingforchemistsbook.com (Benjamin Lear and Christopher Johnson)</webMaster>
    <atom:link href="https://codingforchemistsbook.com/book_material/chapter-15/index.xml" rel="self" type="application/rss+xml" />
  </channel>
</rss>