<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Check format from purity collection
Step1:  Try to add drift velocity correction function
Step2:  Systematic error on drift velocity estimates is around 0.2%, see 
Step3:  Insert in runs db
<ASSISTANT_TASK:>
Python Code:
purity_coll = client['run']['purity']
purity_coll.count()
d = purity_coll.find_one()
for d in purity_coll.find(sort=(('calculation_time', -1), )):
    print(str(d['calculation_time']), parse_expr(d['function']))
Explanation: Check format from purity collection
End of explanation
import numpy as np
data = np.array([
    4,  0.9948,       # Julien
    5,  1.1224,       # Julien
    6,  1.2137,       # Julien
    7,  1.2833,       # Julien
    8,  1.3323,       # Julien
    9,  1.371,        # Jelle
    12, 1.4401,       # Jelle & Julien (same value, well Julien had 1.4402 and I had 1.440...)
    13, 1.456,
    15, 1.482
])
cathode_kv, vdrift_kmpers = data.reshape(-1, 2).T
import matplotlib
import matplotlib.pyplot as plt
%matplotlib inline
vs = np.linspace(0, 20, 100)
deg = len(cathode_kv) - 4
coeffs = np.polyfit(cathode_kv, vdrift_kmpers, deg=deg)
plt.plot(vs, np.polyval(coeffs, vs), label='%dth degree polynomial fit' % deg)
plt.scatter(cathode_kv, vdrift_kmpers, marker='x', c='k', label='Datapoints')
plt.ylim(0.5, 1.7)
plt.legend(loc='lower right')
plt.xlabel("Cathode voltage (kV)")
plt.ylabel("Drift velocity (km/sec)")
plt.plot(cathode_kv, 100 * (np.polyval(coeffs, cathode_kv) - vdrift_kmpers)/vdrift_kmpers, 
         linestyle='', marker='x')
plt.ylabel('Relative fit error (%)')
plt.xlabel("Cathode voltage kV")
Explanation: Try to add drift velocity correction function
End of explanation
v = sympy.Symbol('v')
sp_poly = sympy.Poly.from_list(coeffs.tolist(), gens=v)
# Sanity check:
sp_poly.subs(dict(v=7.5))
# Try converting to and from serialization format, repeat sanity check
serialized_fit = sympy.srepr(sp_poly)
parse_expr(serialized_fit).subs(dict(v=7.5))
Explanation: Systematic error on drift velocity estimates is around 0.2%, see : https://xecluster.lngs.infn.it/dokuwiki/doku.php?id=xenon:xenon1t:aalbers:drift_and_diffusion#results
so if the fitter error is much less than this, we're ok.
Make sympy function
End of explanation
drift_coll = client['run']['drift_velocity']
from datetime import datetime
# Uncomment this, adjust versions and comments to do actual insertion
# drift_coll.insert_one(dict(
#     version='1.0.0',
#     calculation_time=datetime.now(),
#     function=serialized_fit,
#     comment="5th degree polynomial fit to Julien&Jelle's Drift velocity results. Jelle, 1 April 2017."
# ))
Explanation: Insert in runs db
End of explanation
<END_TASK>