inline code
fragments#Do some sums
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
#Define a variable
frequency=1
#Define a list of numbers
# in the range 0..1 with step size 0.2
timestamps=np.arange(0.0,1.0,0.02)
timestamps
array([ 0. , 0.02, 0.04, 0.06, 0.08, 0.1 , 0.12, 0.14, 0.16, 0.18, 0.2 , 0.22, 0.24, 0.26, 0.28, 0.3 , 0.32, 0.34, 0.36, 0.38, 0.4 , 0.42, 0.44, 0.46, 0.48, 0.5 , 0.52, 0.54, 0.56, 0.58, 0.6 , 0.62, 0.64, 0.66, 0.68, 0.7 , 0.72, 0.74, 0.76, 0.78, 0.8 , 0.82, 0.84, 0.86, 0.88, 0.9 , 0.92, 0.94, 0.96, 0.98])
plt.plot(timestamps,np.sin(2*np.pi*timestamps*frequency));
frequency=5
from ipywidgets import interact
def pltsin(f):
plt.plot(timestamps,np.sin(2*np.pi*timestamps*f))
interact(pltsin,f=(0,10,1));
A Jupyter Widget
import folium
mymap = folium.Map(location=[52.0236, -0.7088], zoom_start=15,
width=600, height=300, tiles='Stamen Terrain')
#Add two markers to the map
#folium.Marker([52.0239, -0.7072], popup='OU Library').add_to(mymap)
#folium.Marker([52.0242, -0.7112], popup='Jennie Lee Building').add_to(mymap)
mymap
import requests
postcode = 'MK7 6AA'
r=requests.get('https://api.postcodes.io/postcodes/{PC}'.format(PC=postcode)).json()['result']
lat=r['latitude']; lon=r['longitude']; pc=r['parliamentary_constituency']
mymap = folium.Map(location=[lat, lon], zoom_start=15, width=600, height=300)
#Create a popup message
popupstr = 'Location of {PC} in the {pc} constituency'.format(PC=postcode,pc=pc)
#Display a marker for the location
folium.Marker([lat, lon], popup=popupstr).add_to(mymap)
mymap
import MDAnalysis as mda
import nglview as nv
from nglview.datafiles import PDB, XTC
import warnings
warnings.filterwarnings("ignore")
u = mda.Universe(PDB, XTC)
protein = u.select_atoms('protein')
nv.show_mdanalysis(protein)
/usr/local/lib/python3.5/dist-packages/MDAnalysis/__init__.py:245: UserWarning: ##### MDAnalysis on python 3 is highly experimental! It is mostly non functional and dramatically untested. Use at your own risks!!! ''')
A Jupyter Widget
rdkit
¶Note this also shows how to access a second, python2, environment from the notebook.
%%python2
from rdkit import Chem
from rdkit.Chem import Draw
m = Chem.MolFromSmiles('COc1ccc2c(c1)[nH]c(n2)[S@@](=O)Cc1ncc(c(c1C)OC)C')
Draw.MolToFile(m,'cdk2_mol1.o.png')
from IPython.display import Image
Image('cdk2_mol1.o.png')
dot
language¶# http://www.graphviz.org/content/fsm
from graphviz import Digraph
f = Digraph('finite_state_machine')
f.attr(rankdir='LR', size='8,5')
f.attr('node', shape='doublecircle')
f.node('LR_0') ;f.node('LR_3');
f.attr('node', shape='circle')
f.edge('LR_0', 'LR_2', label='SS(B)'); f.edge('LR_0', 'LR_1', label='SS(S)')
f.edge('LR_1', 'LR_3', label='S($end)'); f.edge('LR_2', 'LR_2', label='SS(b)')
f
For music, we need to set up some mess - though there are ways round this...
from music21 import *
environment.UserSettings()['lilypondPath']='/usr/bin/lilypond'
environment.set('pdfPath', '/usr/bin/musescore')
environment.set('graphicsPath', '/usr/bin/musescore')
environment.set("musescoreDirectPNGPath", "/usr/bin/musescore")
environment.set("musicxmlPath", "/usr/bin/musescore")
from IPython.display import Image
c = chord.Chord("C4 E4 G4")
c.isConsonant()
c.show('lily.png')
Image(filename=c.write('lily.png'))
myBach = corpus.parse('bach/bwv57.8')
alto = myBach.parts['Alto']
alto.show('lily.png')
Image(filename=alto.write('lily.png'))
import SchemDraw as schem
import SchemDraw.elements as e
d = schem.Drawing()
V1 = d.add(e.SOURCE_V, label='5V')
d.add(e.LINE, d='right', l=d.unit*.75)
S1 = d.add(e.SWITCH_SPDT2_CLOSE, d='up', anchor='b', rgtlabel='$t=0$')
d.add(e.LINE, d='right', xy=S1.c, l=d.unit*.75)
d.add(e.RES, d='down', label='$100\Omega$', botlabel=['+','$v_o$','-'])
d.add(e.LINE, to=V1.start)
d.add(e.CAP, xy=S1.a, d='down', toy=V1.start, label='1$\mu$F')
d.add(e.DOT)
d.draw(showplot=False)
drawingspec = '''
import math; import graph; import geometry;
size(12cm); xaxis("$x$"); yaxis("$y$");
real theta1={theta1}; real theta2={theta2};
real L1=0.45; real L2=0.48;
pair l1p = (L1*sin(theta1), L1*cos(theta1));
pair l2p = l1p + (L2*sin(theta1+theta2), L2*cos(theta1+theta2));
draw((0,0)--l1p--l2p); draw(l1p--l1p*1.5, dashed); dot(l2p); draw((0,0)--l2p, dashed);
label("$J1(0,0)$", (0,0), NW); label("$J2(u,v)$", l1p, NW); label("$H(x,y)$", l2p, NE);
draw("$L1$",(0,0)-0.02*I*l1p--l1p-0.02*I*l1p, red, Arrows, Bars, PenMargins);
draw("$L2$", l1p-0.02*I*l2p--l2p-0.02*I*l2p, red, Arrows, Bars, PenMargins);
draw("$\\theta_1$", arc((0,1),(0,0),l1p,0.1), blue, PenMargins);
draw("$\\theta_2$", arc(l1p,0.1,degrees(l1p),degrees(l2p-l1p),CW), blue, PenMargins);
draw("$\\alpha$", arc((1,0),(0,0),l2p,0.2), blue, PenMargins);
'''
import subprocess # to run asymptote
import shlex
f=open('/tmp/pa10-firstjoint.asy', 'w')
theta1='-pi/2' ; theta2='pi/3'
f.write(drawingspec.format(theta1=theta1,theta2=theta2)); f.close()
subprocess.call(shlex.split('asy /tmp/pa10-firstjoint.asy -f png -o pa10-firstjoint.png'))
Image('pa10-firstjoint.png')
#Import sympy packages
import math
from sympy import *
from sympy import init_printing
init_printing()
b,c,B,C=symbols(['b','c','B','C'])
#A simple example of the sine rule
exp=Eq(b/sin(B),c/sin(C))
exp
sympy
Symbolic Maths Packages Lets You Solve Equations¶#We can solve for b:
distance_b = solve(exp)[0][b]
Eq(b, distance_b)
#Then we can substitute in values
Eq(Eq(b,distance_b), distance_b.subs({c:27404,B:math.radians(45),C:math.radians(60)}))
try.jupyter.org
notebooks.azure.com
(not OU account?)Anaconda
, one of the many Docker containers, or the OU TM351 VM