Even more Python script support

This commit is contained in:
Ted Nyman
2013-07-07 14:52:46 -07:00
parent 72b70a11bc
commit 53340ddd4c
2 changed files with 534 additions and 43 deletions

View File

@@ -383,8 +383,8 @@
".gemrc"
]
},
"tokens_total": 360152,
"languages_total": 427,
"tokens_total": 361619,
"languages_total": 428,
"tokens": {
"ABAP": {
"*/**": 1,
@@ -32448,16 +32448,16 @@
"L": 2
},
"Python": {
"from": 33,
"from": 34,
"__future__": 2,
"import": 42,
"import": 47,
"unicode_literals": 1,
"copy": 1,
"sys": 2,
"functools": 1,
"update_wrapper": 2,
"future_builtins": 1,
"zip": 3,
"zip": 8,
"django.db.models.manager": 1,
"#": 13,
"Imported": 1,
@@ -32468,13 +32468,13 @@
"django.conf": 1,
"settings": 1,
"django.core.exceptions": 1,
"(": 508,
"(": 719,
"ObjectDoesNotExist": 2,
"MultipleObjectsReturned": 2,
"FieldError": 4,
"ValidationError": 8,
"NON_FIELD_ERRORS": 3,
")": 519,
")": 730,
"django.core": 1,
"validators": 1,
"django.db.models.fields": 1,
@@ -32504,7 +32504,7 @@
"get_model": 3,
"django.utils.translation": 1,
"ugettext_lazy": 1,
"as": 8,
"as": 11,
"_": 5,
"django.utils.functional": 1,
"curry": 6,
@@ -32516,8 +32516,8 @@
"capfirst": 6,
"class": 14,
"ModelBase": 4,
"type": 4,
"def": 63,
"type": 6,
"def": 68,
"__new__": 2,
"cls": 32,
"name": 39,
@@ -32527,20 +32527,20 @@
"super": 2,
".__new__": 1,
"parents": 8,
"[": 66,
"b": 9,
"for": 52,
"in": 72,
"if": 144,
"[": 152,
"b": 11,
"for": 59,
"in": 79,
"if": 145,
"isinstance": 11,
"]": 66,
"]": 152,
"not": 64,
"return": 53,
"return": 57,
"module": 6,
"attrs.pop": 2,
"new_class": 9,
"{": 24,
"}": 24,
"{": 25,
"}": 25,
"attr_meta": 5,
"None": 86,
"abstract": 3,
@@ -32555,13 +32555,13 @@
"new_class.__module__": 1,
"kwargs": 9,
"model_module.__name__.split": 1,
"-": 4,
"-": 30,
"new_class.add_to_class": 7,
"**kwargs": 9,
"subclass_exception": 3,
"tuple": 3,
"x.DoesNotExist": 1,
"x": 4,
"x": 22,
"hasattr": 11,
"and": 35,
"x._meta.abstract": 2,
@@ -32587,7 +32587,7 @@
"attrs.items": 1,
"new_fields": 2,
"new_class._meta.local_fields": 3,
"+": 15,
"+": 37,
"new_class._meta.local_many_to_many": 2,
"new_class._meta.virtual_fields": 1,
"field_names": 5,
@@ -32620,7 +32620,7 @@
"attr_name": 3,
"base._meta.module_name": 1,
"auto_created": 1,
"True": 15,
"True": 20,
"parent_link": 1,
"new_class._meta.parents": 1,
"copy.deepcopy": 2,
@@ -32703,7 +32703,7 @@
"KeyError": 3,
"field.get_default": 3,
"field.null": 1,
"prop": 4,
"prop": 5,
"kwargs.keys": 2,
"property": 2,
"AttributeError": 1,
@@ -32712,7 +32712,7 @@
"signals.post_init.send": 1,
"instance": 5,
"__repr__": 2,
"u": 3,
"u": 9,
"unicode": 8,
"UnicodeEncodeError": 1,
"UnicodeDecodeError": 1,
@@ -32728,7 +32728,7 @@
"__hash__": 1,
"hash": 1,
"__reduce__": 1,
"data": 9,
"data": 22,
"self.__dict__": 1,
"defers": 2,
"self._deferred": 1,
@@ -32787,10 +32787,10 @@
"._update": 1,
"meta.order_with_respect_to": 2,
"order_value": 2,
"*": 9,
"*": 33,
".count": 1,
"self._order": 1,
"fields": 10,
"fields": 12,
"update_pk": 3,
"bool": 2,
"meta.has_auto_field": 1,
@@ -32815,7 +32815,7 @@
"strings_only": 1,
"_get_next_or_previous_by_FIELD": 1,
"self.pk": 6,
"op": 5,
"op": 6,
"order": 5,
"param": 3,
"q": 4,
@@ -32878,7 +32878,7 @@
"lookup_kwargs": 8,
"self._meta.get_field": 1,
"lookup_value": 3,
"str": 1,
"str": 2,
"lookup_kwargs.keys": 1,
"model_class._default_manager.filter": 2,
"*lookup_kwargs": 2,
@@ -32911,7 +32911,7 @@
"lambda": 1,
"full_clean": 1,
"self.clean_fields": 1,
"e": 7,
"e": 13,
"e.update_error_dict": 3,
"self.clean": 1,
"errors.keys": 1,
@@ -32992,7 +32992,7 @@
"view.methods": 1,
"cls.methods": 1,
"MethodViewType": 2,
"d": 4,
"d": 5,
"rv": 2,
"type.__new__": 1,
"rv.methods": 2,
@@ -33026,14 +33026,14 @@
"index": 1,
"number": 1,
"cpp_type": 1,
"label": 1,
"label": 18,
"has_default_value": 1,
"default_value": 1,
"message_type": 1,
"enum_type": 1,
"is_extension": 1,
"extension_scope": 1,
"options": 2,
"options": 3,
"extensions": 1,
"nested_types": 1,
"enum_types": 1,
@@ -33045,10 +33045,10 @@
"Person": 1,
"_message.Message": 1,
"_reflection.GeneratedProtocolMessageType": 1,
"SHEBANG#!python": 3,
"print": 6,
"SHEBANG#!python": 4,
"print": 39,
"os": 1,
"main": 2,
"main": 4,
"usage": 3,
"string": 1,
"command": 4,
@@ -33057,7 +33057,7 @@
"sys.exit": 1,
"printDelimiter": 4,
"get": 1,
"a": 1,
"a": 2,
"list": 1,
"git": 1,
"directories": 1,
@@ -33076,7 +33076,175 @@
"os.walk": 1,
".next": 1,
"os.path.isdir": 1,
"__name__": 1,
"__name__": 2,
"argparse": 1,
"matplotlib.pyplot": 1,
"pl": 1,
"numpy": 1,
"np": 1,
"scipy.optimize": 1,
"prettytable": 1,
"PrettyTable": 6,
"__docformat__": 1,
"S": 4,
"phif": 7,
"U": 10,
"/": 23,
"_parse_args": 2,
"V": 12,
"np.genfromtxt": 8,
"delimiter": 8,
"t": 8,
"U_err": 7,
"offset": 13,
"np.mean": 1,
"np.linspace": 9,
"min": 10,
"max": 11,
"y": 10,
"np.ones": 11,
"x.size": 2,
"pl.plot": 9,
"**6": 6,
".format": 11,
"pl.errorbar": 8,
"yerr": 8,
"linestyle": 8,
"marker": 4,
"pl.grid": 5,
"pl.legend": 5,
"loc": 5,
"pl.title": 5,
"pl.xlabel": 5,
"ur": 11,
"pl.ylabel": 5,
"pl.savefig": 5,
"pl.clf": 5,
"glanz": 13,
"matt": 13,
"schwarz": 13,
"weiss": 13,
"T0": 1,
"T0_err": 2,
"glanz_phi": 4,
"matt_phi": 4,
"schwarz_phi": 4,
"weiss_phi": 4,
"T_err": 7,
"sigma": 4,
"boltzmann": 12,
"T": 6,
"epsilon": 7,
"T**4": 1,
"glanz_popt": 3,
"glanz_pconv": 1,
"op.curve_fit": 6,
"matt_popt": 3,
"matt_pconv": 1,
"schwarz_popt": 3,
"schwarz_pconv": 1,
"weiss_popt": 3,
"weiss_pconv": 1,
"glanz_x": 3,
"glanz_y": 2,
"*glanz_popt": 1,
"color": 8,
"matt_x": 3,
"matt_y": 2,
"*matt_popt": 1,
"schwarz_x": 3,
"schwarz_y": 2,
"*schwarz_popt": 1,
"weiss_x": 3,
"weiss_y": 2,
"*weiss_popt": 1,
"np.sqrt": 17,
"glanz_pconv.diagonal": 2,
"matt_pconv.diagonal": 2,
"schwarz_pconv.diagonal": 2,
"weiss_pconv.diagonal": 2,
"xerr": 6,
"U_err/S": 4,
"header": 5,
"glanz_table": 2,
"row": 10,
".size": 4,
"*T_err": 4,
"glanz_phi.size": 1,
"*U_err/S": 4,
"glanz_table.add_row": 1,
"matt_table": 2,
"matt_phi.size": 1,
"matt_table.add_row": 1,
"schwarz_table": 2,
"schwarz_phi.size": 1,
"schwarz_table.add_row": 1,
"weiss_table": 2,
"weiss_phi.size": 1,
"weiss_table.add_row": 1,
"T0**4": 1,
"phi": 5,
"c": 3,
"a*x": 1,
"dx": 6,
"d**": 2,
"dy": 4,
"dx_err": 3,
"np.abs": 1,
"dy_err": 2,
"popt": 5,
"pconv": 2,
"*popt": 2,
"pconv.diagonal": 3,
"table": 2,
"table.align": 1,
"dy.size": 1,
"*dy_err": 1,
"table.add_row": 1,
"U1": 3,
"I1": 3,
"U2": 2,
"I_err": 2,
"p": 1,
"R": 1,
"R_err": 2,
"/I1": 1,
"**2": 2,
"U1/I1**2": 1,
"phi_err": 3,
"alpha": 2,
"beta": 1,
"R0": 6,
"R0_err": 2,
"alpha*R0": 2,
"*np.sqrt": 6,
"*beta*R": 5,
"alpha**2*R0": 5,
"*beta*R0": 7,
"*beta*R0*T0": 2,
"epsilon_err": 2,
"f1": 1,
"f2": 1,
"f3": 1,
"alpha**2": 1,
"*beta": 1,
"*beta*T0": 1,
"*beta*R0**2": 1,
"f1**2": 1,
"f2**2": 1,
"f3**2": 1,
"parser": 1,
"argparse.ArgumentParser": 1,
"description": 1,
"#parser.add_argument": 3,
"metavar": 1,
"nargs": 1,
"help": 2,
"dest": 1,
"default": 1,
"action": 1,
"version": 6,
"parser.parse_args": 1,
"absolute_import": 1,
"division": 1,
"with_statement": 1,
@@ -33150,7 +33318,6 @@
"start_line": 1,
"method": 5,
"uri": 5,
"version": 5,
"start_line.split": 1,
"version.startswith": 1,
"headers": 5,
@@ -38865,7 +39032,7 @@
"PowerShell": 12,
"Processing": 74,
"Prolog": 4040,
"Python": 4248,
"Python": 5715,
"R": 14,
"Racket": 360,
"Ragel in Ruby Host": 593,
@@ -38966,7 +39133,7 @@
"PowerShell": 2,
"Processing": 1,
"Prolog": 6,
"Python": 6,
"Python": 7,
"R": 1,
"Racket": 3,
"Ragel in Ruby Host": 3,
@@ -38999,5 +39166,5 @@
"Xtend": 2,
"YAML": 1
},
"md5": "06f04376114dadff4923b1138204a240"
"md5": "0c2809e4b07cb4ec80e99a245694470e"
}

View File

@@ -0,0 +1,324 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright © 2013 Martin Ueding <dev@martin-ueding.de>
import argparse
import matplotlib.pyplot as pl
import numpy as np
import scipy.optimize as op
from prettytable import PrettyTable
__docformat__ = "restructuredtext en"
# Sensitivität der Thermosäule
S = 30e-6
def phif(U):
return U / S
def main():
options = _parse_args()
V = 1000
data = np.genfromtxt("a-leer.csv", delimiter="\t")
t = data[:,0]
U = data[:,1] / V / 1000
U_err = 0.7e-3 / V
offset = np.mean(U[-3:])
x = np.linspace(min(t), max(t))
y = np.ones(x.size) * offset
pl.plot(x, y * 10**6, label="Offset")
print "Offset: {:.3g} V".format(offset)
pl.errorbar(t, U * 10**6, yerr=U_err * 10**6, linestyle="none", marker="+",
label="Messdaten")
pl.grid(True)
pl.legend(loc="best")
pl.title(u"Bestimmung des Offsets")
pl.xlabel(ur"Zeit $t / \mathrm{s}$")
pl.ylabel(ur"Thermospannung $U / \mathrm{\mu V}$")
pl.savefig("Plot_a-leer.pdf")
pl.clf()
V = 100
data = np.genfromtxt("a-Lampe.csv", delimiter="\t")
t = data[:,0]
U = data[:,1] / V / 1000 - offset
U_err = 0.7e-3 / V
x = np.linspace(min(t), max(t))
y = np.ones(x.size) * max(U) * 0.9
pl.plot(x, y * 10**6, label=ur"$90\%$")
pl.errorbar(t, U * 10**6, yerr=U_err * 10**6, linestyle="none", marker="+",
label="Messdaten")
pl.grid(True)
pl.legend(loc="best")
pl.title(u"Bestimmung der Ansprechzeit")
pl.xlabel(ur"Zeit $t / \mathrm{s}$")
pl.ylabel(ur"Thermospannung $U / \mathrm{\mu V}$")
pl.savefig("Plot_a-Lampe.pdf")
pl.clf()
# Lesliewürfel
print """
Lesliewürfel
============
"""
glanz = np.genfromtxt("b-glanz.csv", delimiter="\t")
matt = np.genfromtxt("b-matt.csv", delimiter="\t")
schwarz = np.genfromtxt("b-schwarz.csv", delimiter="\t")
weiss = np.genfromtxt("b-weiss.csv", delimiter="\t")
T0 = 19.0 + 273.15
T0_err = 1.0
glanz[:,0] += 273.15
matt[:,0] += 273.15
schwarz[:,0] += 273.15
weiss[:,0] += 273.15
glanz[:,1] /= 1000 * V
matt[:,1] /= 1000 * V
schwarz[:,1] /= 1000 * V
weiss[:,1] /= 1000 * V
glanz[:,1] -= offset
matt[:,1] -= offset
schwarz[:,1] -= offset
weiss[:,1] -= offset
glanz_phi = phif(glanz[:,1])
matt_phi = phif(matt[:,1])
schwarz_phi = phif(schwarz[:,1])
weiss_phi = phif(weiss[:,1])
T_err = 0.3
sigma = 5.670373e-8
def boltzmann(T, epsilon, offset):
return epsilon * sigma * T**4 + offset
glanz_popt, glanz_pconv = op.curve_fit(boltzmann, glanz[:,0], glanz_phi)
matt_popt, matt_pconv = op.curve_fit(boltzmann, matt[:,0], matt_phi)
schwarz_popt, schwarz_pconv = op.curve_fit(boltzmann, schwarz[:,0], schwarz_phi)
weiss_popt, weiss_pconv = op.curve_fit(boltzmann, weiss[:,0], weiss_phi)
glanz_x = np.linspace(min(glanz[:,0]), max(glanz[:,0]))
glanz_y = boltzmann(glanz_x, *glanz_popt)
pl.plot(glanz_x, glanz_y, label="Fit glanz", color="gold")
matt_x = np.linspace(min(matt[:,0]), max(matt[:,0]))
matt_y = boltzmann(matt_x, *matt_popt)
pl.plot(matt_x, matt_y, label="Fit matt", color="yellow")
schwarz_x = np.linspace(min(schwarz[:,0]), max(schwarz[:,0]))
schwarz_y = boltzmann(schwarz_x, *schwarz_popt)
pl.plot(schwarz_x, schwarz_y, label="Fit schwarz", color="black")
weiss_x = np.linspace(min(weiss[:,0]), max(weiss[:,0]))
weiss_y = boltzmann(weiss_x, *weiss_popt)
pl.plot(weiss_x, weiss_y, label="Fit weiss", color="gray")
print "glanz ε = {:.3g} ± {:.3g}".format(glanz_popt[0], np.sqrt(glanz_pconv.diagonal()[0]))
print "glanz offset = {:.3g} ± {:.3g}".format(glanz_popt[1], np.sqrt(glanz_pconv.diagonal()[1]))
print "matt ε = {:.3g} ± {:.3g}".format(matt_popt[0], np.sqrt(matt_pconv.diagonal()[0]))
print "matt offset = {:.3g} ± {:.3g}".format(matt_popt[1], np.sqrt(matt_pconv.diagonal()[1]))
print "schwarz ε = {:.3g} ± {:.3g}".format(schwarz_popt[0], np.sqrt(schwarz_pconv.diagonal()[0]))
print "schwarz offset = {:.3g} ± {:.3g}".format(schwarz_popt[1], np.sqrt(schwarz_pconv.diagonal()[1]))
print "weiss ε = {:.3g} ± {:.3g}".format(weiss_popt[0], np.sqrt(weiss_pconv.diagonal()[0]))
print "weiss offset = {:.3g} ± {:.3g}".format(weiss_popt[1], np.sqrt(weiss_pconv.diagonal()[1]))
pl.errorbar(glanz[:,0], glanz_phi, xerr=T_err, yerr=U_err/S,
label="glanz", color="gold", linestyle="none")
pl.errorbar(matt[:,0], matt_phi, xerr=T_err, yerr=U_err/S,
label="matt", color="yellow", linestyle="none")
pl.errorbar(schwarz[:,0], schwarz_phi, xerr=T_err, yerr=U_err/S,
label="schwarz", color="black", linestyle="none")
pl.errorbar(weiss[:,0], weiss_phi, xerr=T_err, yerr=U_err/S,
label="weiss", color="gray", linestyle="none")
header = ["T / K", "Phi/F in W/m^2", "Fehler T", "Fehler Phi/F"]
print """
Tabellen für den Lesliewürfel-Plot
----------------------------------
"""
print "Glanz"
glanz_table = PrettyTable(header)
for row in zip(glanz[:,0], glanz_phi, np.ones(glanz[:,0].size)*T_err, np.ones(glanz_phi.size)*U_err/S):
glanz_table.add_row(row)
print glanz_table
print
print "Matt"
matt_table = PrettyTable(header)
for row in zip(matt[:,0], matt_phi, np.ones(matt[:,0].size)*T_err, np.ones(matt_phi.size)*U_err/S):
matt_table.add_row(row)
print matt_table
print
print "Schwarz"
schwarz_table = PrettyTable(header)
for row in zip(schwarz[:,0], schwarz_phi, np.ones(schwarz[:,0].size)*T_err, np.ones(schwarz_phi.size)*U_err/S):
schwarz_table.add_row(row)
print schwarz_table
print
print "Weiß"
weiss_table = PrettyTable(header)
for row in zip(weiss[:,0], weiss_phi, np.ones(weiss[:,0].size)*T_err, np.ones(weiss_phi.size)*U_err/S):
weiss_table.add_row(row)
print weiss_table
print
epsilon = 0.1
x = np.linspace(min([min(x) for x in [glanz[:,0], matt[:,0], schwarz[:,0],
weiss[:,0]]]),
max([max(x) for x in [glanz[:,0], matt[:,0], schwarz[:,0],
weiss[:,0]]]),
100)
offset = - epsilon * sigma * T0**4
print "ideal offset = {:.3g}".format(offset)
y = boltzmann(x, epsilon, offset)
pl.plot(x, y, label=ur"$\epsilon = 0.1$")
pl.grid(True)
pl.title(u"Lesliewürfel")
pl.xlabel(ur"Temperatur $T / \mathrm{K}$")
pl.ylabel(ur"Strahlungsfluss $\frac{\Phi}{F} / \mathrm{\frac{W}{m^2}}$")
pl.legend(loc="best", prop={"size": 12})
pl.savefig("Plot_b.pdf")
pl.clf()
# Aufgabe c
print """
Aufgabe c
=========
"""
data = np.genfromtxt("c-erste.csv", delimiter="\t")
d = data[:,0] / 100
U = data[:,1] / V
phi = phif(U)
def c(x, a, b):
return a*x + b
dx = d**(-2)
dy = phi
dx_err = np.abs(-2 * d**(-3)) * 0.001
dy_err = 0.001 / S
popt, pconv = op.curve_fit(c, dx, dy)
x = np.linspace(min(dx), max(dx))
y = c(x, *popt)
pl.plot(x, y, label="Fit")
print "Fitparameter"
print "a", popt[0], "±", np.sqrt(pconv.diagonal()[0])
print "b", popt[1], "±", np.sqrt(pconv.diagonal()[1])
pl.errorbar(dx, dy, xerr=dx_err, yerr=dy_err, linestyle="none",
marker="+", label="Messdaten")
pl.grid(True)
pl.title(u"Halogenlampe bei verschiedenen Abständen")
pl.xlabel(ur"Abstand $d^{-2} / \mathrm{m^{-2}}$")
pl.ylabel(ur"Strahlungsfluss $\frac{\Phi}{F} / \mathrm{\frac{W}{m^2}}$")
pl.legend(loc="best")
pl.savefig("Plot_c-erste.pdf")
pl.clf()
print
print "Tabelle für Aufgabe c"
fields = ["d^-2 in m^-2", "Phi/F in W/m^2", "Fehler d^-2", "Fehler Phi/F"]
table = PrettyTable(fields)
table.align = "l"
for row in zip(dx, dy, dx_err, np.ones(dy.size)*dy_err):
table.add_row(row)
print table
print
data = np.genfromtxt("c-zweite.csv", delimiter="\t")
U1 = data[:,0]
I1 = data[:,1]
U2 = data[:,2] / V
U_err = 0.001
I_err = 0.01
p = U1 * I1
R = U1 / I1
R_err = np.sqrt(
(1/I1 * U_err)**2
+ (U1/I1**2 * I_err)**2
)
phi = phif(U2)
phi_err = U_err / S
alpha = 4.82e-3
beta = 6.76e-7
R0 = 0.35
R0_err = 0.05
T = (-alpha*R0 + np.sqrt(R0)*np.sqrt(4*beta*R + alpha**2*R0 - 4*beta*R0) +
2*beta*R0*T0)/(2*beta*R0)
popt, pconv = op.curve_fit(boltzmann, T, phi, sigma=phi_err)
x = np.linspace(min(T), max(T))
y = boltzmann(x, *popt)
pl.plot(x, y, label="Fit")
epsilon = popt[0]
epsilon_err = np.sqrt(pconv.diagonal()[0])
print "ε = {:.3g} ± {:.3g}".format(epsilon, epsilon_err)
f1 = (1/(np.sqrt(R0)*np.sqrt(4*beta*R + alpha**2*R0 - 4*beta*R0))) * R_err
f2 = T0_err
f3 = ((-alpha + ((alpha**2 - 4*beta)*np.sqrt(R0))/( 2*np.sqrt(4*beta*R + alpha**2*R0 - 4*beta*R0)) + np.sqrt( 4*beta*R + alpha**2*R0 - 4*beta*R0)/(2*np.sqrt(R0)) + 2*beta*T0)/( 2*beta*R0) - (-alpha*R0 + np.sqrt(R0)*np.sqrt(4*beta*R + alpha**2*R0 - 4*beta*R0) + 2*beta*R0*T0)/( 2*beta*R0**2)) * R0_err
T_err = np.sqrt(f1**2 + f2**2 + f3**2)
pl.errorbar(T, phi, xerr=T_err, yerr=phi_err, label="Messdaten",
linestyle="none", marker="+")
pl.grid(True)
pl.legend(loc="best")
pl.title(u"Halogenlampe bei verschiedenen Leistungen")
pl.xlabel(u"Temperatur $T / \mathrm{K}$")
pl.ylabel(ur"Strahlungsfluss $\frac{\Phi}{F} / \mathrm{\frac{W}{m^2}}$")
pl.savefig("Plot_c-zweite.pdf")
pl.clf()
def _parse_args():
"""
Parses the command line arguments.
:return: Namespace with arguments.
:rtype: Namespace
"""
parser = argparse.ArgumentParser(description="")
#parser.add_argument("args", metavar="N", type=str, nargs="*", help="Positional arguments.")
#parser.add_argument("", dest="", type="", default=, help=)
#parser.add_argument("--version", action="version", version="<the version>")
return parser.parse_args()
if __name__ == "__main__":
main()