Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Views: 129
Kernel: Python 2 (SageMath)
from IPython.core.display import HTML def css_styling(): styles = open("custom.css", "r").read() return HTML(styles) css_styling()

AnalyzeFourPointCond

Notebook for analyzing four-point conductance measurements. This notebook only plots the temperature versus time.

Import and set up the notebook.

%matplotlib inline import numpy as np import matplotlib.pyplot as plt import scipy.odr as odr import glob, re

Get Data File

dataFiles = glob.glob('*.csv') for (i,file) in enumerate(dataFiles): print "%d, %s" % (i, file) iFile = int(raw_input("Enter data file number: ")) dataFile = dataFiles[iFile] fpData = open(dataFile, 'r') # Dump the file comments for line in fpData.readlines(): if line[0] == '#' and "Temp" not in line: print line.strip() fpData.seek(0)
0, readArduino-201804171428.csv 1, test.csv 2, readArduino-201804191455.csv 3, readArduino-201804121033-Original.csv 4, readArduino-201804121033-Fixed.csv
Enter data file number:
# readArduino.py # Tue Apr 17 14:28:30 2018 # wired R

Read Data

fpData.seek(0,0) temperature = [] resistance = [] resUncert = [] done = False iLine = 0 nLines = 0 nTemps = 0 while not done: try: line = fpData.readline().strip() if not line: done = True continue nLines += 1 iLine += 1 # Check for beginning of a temperature point if "#Therm" in line: nTemps += 1 line = fpData.readline().strip() nLines += 1 I = np.zeros((16,), dtype=float) V = np.zeros((16,), dtype=float) fields = line.split(',') temperature.append(float(fields[0])) I[0] = float(fields[1]) V[0] = float(fields[2]) for i in range(1,16): line = fpData.readline().strip() nLines += 1 fields = line.split(',') I[i] = float(fields[0]) V[i] = float(fields[1]) z, cov = np.polyfit(I, V, 1, cov=True) resistance.append(1e3 * z[0]) resUncert.append(1e3 * np.sqrt(cov[0,0])) except: print """ ERROR! ERROR! ERROR! ERROR! on line """, nLines break temperature = np.array(temperature) resistance = np.array(resistance) resUncert = np.array(resUncert) print "There are {} lines in the file".format(nLines) print "There are {} data points".format(nTemps) print "Last temperature is {}".format(temperature[-1]) print "The last I data are {}".format(I) print "The last V data are {}".format(V) print "The last temperature is {} C".format(temperature[-1]) print "The last resistance is {:.2f} +/- {:.2f} Ohms".format(resistance[-1], resUncert[-1])
ERROR! ERROR! ERROR! ERROR! on line 13 There are 13 lines in the file There are 1 data points Last temperature is 24.77 The last I data are [ 0. 0.34 0.69 1.03 1.37 1.71 2.06 2.4 0. 0. 0. 0. 0. 0. 0. 0. ] The last V data are [-0.83 -0.8 -0.68 -0.64 -0.49 -0.38 -0.3 -0.15 0. 0. 0. 0. 0. 0. 0. 0. ] The last temperature is 24.77 C
--------------------------------------------------------------------------- IndexError Traceback (most recent call last) <ipython-input-10-c46b9bf1ccfa> in <module>() 53 print "The last V data are {}".format(V) 54 print "The last temperature is {} C".format(temperature[-1]) ---> 55 print "The last resistance is {:.2f} +/- {:.2f} Ohms".format(resistance[-1], resUncert[-1]) IndexError: index -1 is out of bounds for axis 0 with size 0

Plotting Data Nicely

The cool down happens very fast so the thremistor is cooling much faster than the resistor. So I want to separate the data into two batches, the cool down data, then the data we care about when the temperature slowly went back up.

First I will plot the temperature versus time data. Then work on an algorithm to find when the temperature starts up.

def moving_average(a, n=3) : ret = np.cumsum(a, dtype=float) ret[n:] = ret[n:] - ret[:-n] return ret[n - 1:] / n # Smooth the temperature tMA = moving_average(temperature, 10) minTemp = np.min(tMA) cool0 = 0 # step through and wait for t to get with in 2.0 of min temp for (i,t) in enumerate(tMA): if t - minTemp < 1.0: break cool1 = i # Now step form there to where temp starts up for (i,t) in enumerate(tMA[cool1:]): if t - minTemp > 1.5: break cool2 = i + cool1 print cool0, cool1, cool2 fig = plt.figure() ax = fig.add_subplot(111) ax.plot(range(cool2), tMA[:cool2]+273.15, 'r.') ax.plot(range(cool2,len(tMA)), tMA[cool2:]+273.15, 'b.') ax.set_ylabel("Temperture (K)") ax.set_xlabel("Time (s)") ax.set_title("R vs T, 330 $\Omega$ Carbon ")
0 28 48
<matplotlib.text.Text at 0x7f0d280be7d0>
Image in a Jupyter notebook
fig = plt.figure() ax = fig.add_subplot(111) ax.plot(temperature[:cool2]+273.15, resistance[:cool2], 'r.', label="Cooling") ax.plot(temperature[cool2:]+273.15, resistance[cool2:], 'b.', label="Warming") ax.set_xlabel("Temperture (K)") ax.set_ylabel("Resistance ($\Omega$)") ax.set_title("R vs T, 330 $\Omega$ Carbon ") ax.legend(loc=3)
<matplotlib.legend.Legend at 0x7f0d285d3210>
Image in a Jupyter notebook
print len(temperature) tMA = moving_average(temperature, 10) print len(tMA)
3090 3081