<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Chapter 14 :: Coding For Chemists</title>
    <link>https://codingforchemistsbook.com/book_material/chapter-14/index.html</link>
    <description>Data Chapter 14 usesthe same data as for Chapter 3.&#xA;Alternatively, individual files can be found in the Data section.&#xA;Code from chapter &#39;&#39;&#39; A script to produce .csv files with data from my titration paper Requires: DecomposeSpectrum.py Written by: Ben Lear and Chris Johnson (authors@codechembook.com) v1.0.0 - 250502 - initial version &#39;&#39;&#39; from codechembook.quickTools import importFromPy, quickSaveFilename, quickPopupMessage importFromPy(&#39;DecomposeSpectrum.py&#39;, &#39;trimmed_wavelength&#39;, &#39;trimmed_absorbance&#39;, &#39;result&#39;) # Gather the fit parameters and spectral data into a dictionary spectral_data = dict(wavelength = trimmed_wavelength, absorption = trimmed_absorbance) # Load the individual components of the fit into the dictionary comps = result.eval_components(x = trimmed_wavelength) for c in comps: spectral_data[f&#39;{c[:-1]}&#39;] = comps[c] # Get the path for the file that we want to save quickPopupMessage(&#39;Choose a CSV file to save the data.&#39;) file = quickSaveFilename(filetypes=&#39;CSV files, *.csv&#39;, initialpath=&#39;./report.csv&#39;, title=&#39;Save your file&#39;) # Write the spectral data and fit parameters to a CSV file with open(file, &#39;w&#39;, encoding=&#39;utf-8-sig&#39;) as f: # open a file to write, in write mode # Write the header line, starting with the data line = &#39;wavelength, exp, fit, gaussian1, gaussian2, gaussian3, &#39; # Now add the headers for the columns with gaussian fits for j in range(3): line = line + f&#39;g{j+1} amplitude, g{j+1} amplitude uncertainty, g{j+1} center, g{j+1} center uncertainty, g{j+1} sigma, g{j+1} sigma uncertainty, &#39; # Now add the linear part line = line + &#39;slope, slope uncertainty, intercept, intercept uncertainty\n&#39; f.write(line) # Construct the complex CSV file. We need to write out N_data_data_points rows # Some rows at the top will have extra columns for the fit data for i in range(len(trimmed_wavelength)): # loop through the rows we need line = &#39;&#39; # start with a blank line on each iteration for data in spectral_data: # iterate through keys in the spectral data dictionary line = line + f&#39;{spectral_data[data][i]:E}, &#39; # add data values to line in engineering format # Treat rows 1-3 differently to print the best fitting parameters for the gaussian components if i == 0: # we can add in parameter value information for j in range(3): line = line + f&#39;{result.params[f&#34;g{j+1}_amplitude&#34;].value}, &#39; line = line + f&#39;{result.params[f&#34;g{j+1}_amplitude&#34;].stderr}, &#39; line = line + f&#39;{result.params[f&#34;g{j+1}_center&#34;].value}, &#39; line = line + f&#39;{result.params[f&#34;g{j+1}_center&#34;].stderr}, &#39; line = line + f&#39;{result.params[f&#34;g{j+1}_sigma&#34;].value}, &#39; line = line + f&#39;{result.params[f&#34;g{j+1}_sigma&#34;].stderr}, &#39; line = line + f&#39;{result.params[&#34;lin_slope&#34;].value:E}, &#39; line = line + f&#39;{result.params[&#34;lin_slope&#34;].stderr:E},&#39; line = line + f&#39;{result.params[&#34;lin_intercept&#34;].value:E}, &#39; line = line + f&#39;{result.params[&#34;lin_intercept&#34;].stderr:E}&#39; line = line + &#39;\n&#39; # add a newline character to the end of our line f.write(line) # write the line &#39;&#39;&#39; Produce a Word document with a table for the titration paper Requires: DecomposeSpectrum.py Written by: Ben Lear and Chris Johnson (authors@codechembook.com) v1.0.0 - 250502 - initial version &#39;&#39;&#39; from docx import Document from docx.oxml import OxmlElement, parse_xml from docx.oxml.ns import qn, nsdecls from docx.enum.text import WD_PARAGRAPH_ALIGNMENT from codechembook.quickTools import importFromPy, quickSaveFilename, quickPopupMessage importFromPy(&#39;DecomposeSpectrum.py&#39;, &#39;result&#39;) # Start a new Word document doc = Document() # Add a section title - we have to handle the subscript as a separate paragraph title = doc.add_heading(level=1) # Use the preset heading font title_run = title.add_run(&#39;Composition of the Ni(pz)&#39;) # Text for the start of the heading; pz = pyrazine subscript_run = title.add_run(&#39;2&#39;) # Text that is supposed to be subscripted subscript_run.font.subscript = True # Change the text formatting to subscript title.add_run(&#39; MLCT band&#39;) # Change back to normal font and keep going # Add the introductory paragraph with subscript paragraph = doc.add_paragraph(&#39;Parameters extracted from the spectral decomposition of the MLCT band for Ni(pz)&#39;) paragraph_run = paragraph.add_run(&#39;2&#39;) paragraph_run.font.subscript = True paragraph.add_run(&#39; into Gaussians contributions. &#39;) # Create a table parameters = [&#39;amplitude&#39;, &#39;center&#39;, &#39;sigma&#39;] ncomponents = 3 # the number of gaussians ncols = len(parameters)*2 # each gaussian has three parameters, each with uncertainty table = doc.add_table(rows=ncomponents + 1, cols=ncols + 1) # create a blank table table.style = &#39;Table Grid&#39; # style the table # Add header row with the names of the fit parameters hdr_cells = table.rows[0].cells hdr_cells[0].text = &#39;Component&#39; for i, name in enumerate(parameters): hdr_cells[2*i+1].text = f&#39;{name}&#39; hdr_cells[2*i+2].text = f&#39;{name} uncertainty&#39; # Make text in header cells bold and shaded gray, and with a light gray backgound for cell in hdr_cells: # Get the instructions for Word to apply the text shading and giving it to the cell shading_elm = parse_xml(r&#39;&lt;w:shd {} w:fill=&#34;D9D9D9&#34;/&gt;&#39;.format(nsdecls(&#39;w&#39;))) # must create each time cell._tc.get_or_add_tcPr().append(shading_elm) # Loop through each run in each paragraph in the cell for paragraph in cell.paragraphs: for run in paragraph.runs: run.font.bold = True shading_elm = OxmlElement(&#39;w:shd&#39;) # create a shading element shading_elm.set(qn(&#39;w:fill&#39;), &#39;C0C0C0&#39;) # set the text color run._element.append(shading_elm) # add it back in # Loop through each gaussian componenet and add the parameter values to the table for i, model in enumerate([&#39;g1&#39;, &#39;g2&#39;, &#39;g3&#39;]): data_cells = table.rows[i+1].cells # get a collection of new cells data_cells[0].text = f&#39;{model}&#39; # set the row label # Loop through the parameters and print their values in the corresponding cell for j, parameter in enumerate(parameters): data_cells[2*j+1].text = f&#39;{result.params[f&#34;{model}_{parameter}&#34;].value:.2f}&#39; data_cells[2*j+2].text = f&#39;{result.params[f&#34;{model}_{parameter}&#34;].stderr:.2f}&#39; data_cells[2*j+1].paragraphs[0].alignment = WD_PARAGRAPH_ALIGNMENT.RIGHT data_cells[2*j+2].paragraphs[0].alignment = WD_PARAGRAPH_ALIGNMENT.RIGHT # Open a file dialog to pick the file to save to quickPopupMessage(message = &#39;Choose a filename for the Word document.&#39;) file = quickSaveFilename(title = &#39;Please choose a filename to save as.&#39;, filetypes = &#39;Word Documents, *.docx&#39;) # Save the document doc.save(file) Solutions to Exercises Targeted exercises Writing text files with arbitrary formatting using with open Exercise 0 Produce a narrative fit report, that explains the model, the fit approach, and the results for the linear fit produced by the final code in Chapter 5. You can append the following to the code for Chapter 5.</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-14/index.xml" rel="self" type="application/rss+xml" />
  </channel>
</rss>