<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Chapter 4 :: Coding For Chemists</title>
    <link>https://codingforchemistsbook.com/book_material/chapter-4/index.html</link>
    <description>Data Chapter 4 usesthe same data as for Chapter 3&#xA;Alternatively, individual files can be found in the Data section.&#xA;Code from chapter from rdkit import Chem from rdkit.Chem import Draw # Define the SMILES for acetate acetate_smiles = &#34;CC(=O)[O-]&#34; # Convert the SMILES to an RDKit molecule object acetate_mol = Chem.MolFromSmiles(acetate_smiles) # Display the structure Draw.ShowMol(acetate_mol) def velocity_at_time(time): &#39;&#39;&#39; A function to find the velocity of a dropped object at a given time. Required Params: time (float): the time that has elapsed since the drop Returns: v (float): the velocity at that time &#39;&#39;&#39; v = 9.8 * time**2 return v v1 = velocity_at_time(2) print(v1) print(velocity_at_time(3)) &#39;&#39;&#39; A program to plot multiple uv-vis spectra from .csv files from a titration experiment Requires: .csv files with col 1 as wavelength and col 2 as intensity Written by: Ben Lear and Chris Johnson (authors@codechembook.com) v1.1.0 - 250201 - added structure drawings showing protonation reaction v1.0.0 - 250131 - initial version &#39;&#39;&#39; import numpy as np from plotly.subplots import make_subplots from codechembook.quickTools import quickOpenFilenames # First, we need the names of the files that we want to plot # Ask the user to select the files and then sort the resulting list data_files = quickOpenFilenames(filetypes = &#39;CSV files, *.csv&#39;) sorted_data_files = sorted(data_files) # Next, we will process one file at a time and add it to the plot titration_series = make_subplots() # start a blank plotly figure object # Read the data in one file and add it as a scatter trace to the figure object for file in sorted_data_files: # loop through the data files one at a time # Read the data and store it in temporary x and y variables x_data, y_data = np.genfromtxt(file, delimiter = &#39;,&#39;, skip_header = 1, unpack = True) # Add data as scatter trace with formatted lines and exclude from legend titration_series.add_scatter(x = x_data, y = y_data, line = dict(color = &#39;gray&#39;, width = 1, dash = &#39;dot&#39;), name = file.stem + &#39; eqs&#39;, showlegend=False) # Adjust the appearance of only the first and last traces to highlight titration_series.update_traces(selector = 0, # specify the initial trace line = dict(color = &#39;darkcyan&#39;, width = 2, dash = &#39;solid&#39;), showlegend = True, name = &#39;initial&#39;) titration_series.update_traces(selector = -1, # specify the final trace line = dict(color = &#39;darkred&#39;, width = 2, dash = &#39;solid&#39;), showlegend = True, name = &#39;final&#39;) # Move the initial trace to the end of the data, so that it is drawn on top titration_series.data = titration_series.data[1:] + titration_series.data[:1] # Format the plot area and then show it and then save it titration_series.update_layout(template = &#39;simple_white&#39;) titration_series.update_xaxes(title = &#39;wavelength /nm&#39;, range = [270, 1100]) titration_series.update_yaxes(title = &#39;absorbance&#39;, range = [0, 4.5]) from rdkit import Chem from rdkit.Chem import Draw from rdkit.Chem.Draw.MolDrawing import DrawingOptions import base64 from codechembook.symbols.typesettingHTML import textsup def make_mol_uri(smiles, color, bonds_wide, bonds_tall): &#39;&#39;&#39; Function to generate an svg of molecular drawing using rdkit and then return it in a format that is appropriate for inclusion in a Plotly figure object. REQUIRED PARAMETERS smiles (string): Valid chemical smiles string. color (tuple): Color designated in the rgb format. bonds_wide (int): Specifies how many bonds wide the structure is bonds_tall (int): Specifies how many bonds tall the structure is RETURNS (bytes): Uri of the image for use in Plotly. &#39;&#39;&#39; # Create an RDKit molecule object from a SMILES string mol = Chem.MolFromSmiles(smiles) # Create a dictionary that defines the color of all atoms (here, the same) for key in range(1, 119): # 119 is exclusive, so it goes up to 118 DrawingOptions.elemDict[key] = color # Set options for drawing drawer = Draw.MolDraw2DSVG(50*bonds_wide, 50*bonds_tall) # create a canvas to draw on drawer.drawOptions().updateAtomPalette(DrawingOptions.elemDict) # the colors of atoms drawer.drawOptions().setBackgroundColour((0, 0, 0, 0)) # the canvas color # Now that the options are set, we can process the molecule drawing drawer.DrawMolecule(mol) # create the drawing instructions drawer.FinishDrawing() # use the drawing instructions to make the drawing # Generate the SVG instructions then convert to a Base64-encoded data URI with a Plotly-required preamble svg = drawer.GetDrawingText().replace(&#39;svg:&#39;, &#39;&#39;) svg_data_uri = f&#34;data:image/svg+xml;base64,{base64.b64encode(svg.encode(&#39;utf-8&#39;)).decode(&#39;utf-8&#39;)}&#34; return svg_data_uri # return the binary code for the image # Define the SMILES string for the base and acid species base_smiles = &#39;C1(N=CC=N2)=C2O[Ni]3(O1)OC4=NC=CN=C4O3&#39; acid_smiles = &#39;[H][N+]1=C2O[Ni]3(OC2=NC=C1)OC4=NC=CN=C4O3&#39; # Define colors for the base and acid species base_color = (0, 139/255, 139/255) # dark cyan acid_color = (139/255, 0, 0) # dark red # Loop through both molecules and create their respective images for species, color, position in zip( [base_smiles, acid_smiles], # the SMILES strings [base_color, acid_color], # the colors [580, 870]): # the positions of the images on the plot area # Add image to plot titration_series.add_layout_image( dict(source=make_mol_uri(species, color, bonds_wide = 8, bonds_tall = 3), xref=&#39;x&#39;, yref=&#39;y&#39;, # references for coordinate origin x=position, y=2.45, # x- and y-coordinate position of the image xanchor=&#39;center&#39;, yanchor=&#39;top&#39;,# alignment of image wrt x,y coords sizex=200, sizey=1)) # width and height of the image # Add an arrow to denote the reaction titration_series.add_annotation( dict(ax = 680, ay = 2, # arrow start coordinates x = 770, y = 2, # arrow end coordinates axref = &#39;x&#39;, ayref = &#39;y&#39;, xref = &#39;x&#39;, yref = &#39;y&#39;, # references for coordinates showarrow = True, arrowhead = 1, arrowwidth = 1.5, arrowcolor = &#39;grey&#39;)) # arrow format # Add H+ label titration_series.add_annotation( dict(text = &#39;H&#39; + textsup(&#39;+&#39;), font = dict(color = &#39;grey&#39;), xref = &#39;x&#39;, yref = &#39;y&#39;, # references for coordinate origin x = (770+680)/2, y = 2, # x- and y-coordinate position of the image xanchor = &#39;center&#39;, yanchor = &#39;bottom&#39;, # location of text box wrt position showarrow = False)) # we want no arrow associated with this annotation # eliminate the legend entries for all traces titration_series.update_traces(showlegend = False) # Now output the plot titration_series.show(&#39;png+browser&#39;) titration_series.write_image(&#39;titration.png&#39;, width = 6*300, height = 4*300) Solutions to Exercises Targeted exercises Accessing items from dictionaries using keys Exercise 0 Starting with the dictionary that you made in Exercise 22 of Chapter 3, print the following properties for the given solvent:</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-4/index.xml" rel="self" type="application/rss+xml" />
  </channel>
</rss>