%matplotlib inline
import pandas as pd
measurement = pd.Series.from_csv('measurement.csv', header=0)
measurement.plot()
reference = pd.Series.from_csv('reference.csv')
reference.plot()
measurement.plot()
norm_measurement = measurement / reference
norm_measurement.plot()
from uncertainties import ufloat
import pint
u = pint.UnitRegistry()
l = ufloat(2, 0.2, tag='length') * u.m
t = ufloat(7.8, 0.002, tag='time') * u.s
v = l/t
v.to(u.km/u.hour)
v.magnitude.error_components()
l - l
l + 2 *u.inch
l - 2 * u.s
import module / from module import thing
# example
import numpy as np
print(np .pi)
from numpy import pi
print(pi)
# strings
s = "hi\nthere"
print(s)
# double or single quotes
s = 'hi\nthere'
print(s)
# triple quotes for easy multilines
s = """hi
there"""
print(s)
# integers, floats, complex numbers
i = 2
f = 3.6
c = 3.6 + 5.2j
i, f, c
# lists
l = ['a', 2.3, 5]
l[1]
# dictionaries (maps)
d = {"a": 2.5,
"b": 3.0,
"c": 15}
d["d"]
d["d"] = 2
d["d"]
# pandas DataFrame
import pandas as pd
df = pd.read_csv('./example.dat', header=201, sep='\t', index_col=0)
df
df.loc[0.5:1.2]
df.iloc[1:3]
df.plot(y="Keysight1", logy=True)
# pandas Series
ser = df.iloc[:-3]
ser
pd.Series([1.2, 2.2, 3.6], index=[3300, 3301, 3302])
import pandas as pd
measurement = pd.Series.from_csv('measurement.csv', header=0)
reference = pd.Series.from_csv('reference.csv')
norm_measurement = measurement / reference
measurement.plot()
norm_measurement.plot()
l = [2, 4, 6, 8]
l/2
import numpy as np
a = np.array(l)
a/2
3%2
list
vs. np.array
: better math, same dtypenp.array
vs pd.Series
/pd.DataFrame
: smart indexing, plotting, other convenience=> if in doubt, use pd.Series
/pd.DataFrame
def sq_half(x):
return x**2 / 2
sq_half(4)
def rms(x, y=2):
"""root mean square of x and y"""
return np.sqrt((x**2 + y**2) / 2), x, y
a, b, c = rms(3)
import numpy as np
class Point:
"""A 2d point"""
def __init__(self, x, y):
self.x = x
self.y = y
def distance(self, other):
"""Euclidean distance to the other point."""
return np.sqrt( (self.x - other.x)**2 + (self.y - other.y)**2 )
p = Point(2, 3)
q = Point(3, 7)
p.distance(q)
from uncertainties import ufloat
import pint
u = pint.UnitRegistry()
l = ufloat(2, 0.2, tag='length') * u.m
t = ufloat(7.8, 0.002, tag='time') * u.s
v = l/t
type(v)
type(v.magnitude)
v.magnitude.error_components()
for x in iterable:
do_stuff
l = ["a", "b", 15]
for x in l:
print(x)
# to count (like loop in C/C++)
for i in range(5):
print(i)
range(100000)
# conditionals
if thing:
do_stuff
a = 2
if a < 0:
a = a * -1
elif a < 2:
print('haha')
print(a)
# equivalent
a = abs(a)
thing?
rms??
ipython
: interactive terminaljupyter
: interactive notebooks in the browser (and slide shows)pycharm
: IDE with debugger, syntax checks etc.conda
: binary package manager for python: stable, easy installationmost useful usually:
os.path
logging
subprocess
worth a look if you search something
import subprocess
subprocess.run(['ls', '-la'])
!ls -la
numpy
¶import numpy as np
scipy
¶sp.special.erf
)import scipy as sp
pandas
¶import pandas as pd
numba: http://numba.pydata.org/ make specific, math-heavy functions go fast
caveat: conversion needs some work, not all functions from numpy are available in numba