Skip to content

Commit

Permalink
code linting with black
Browse files Browse the repository at this point in the history
  • Loading branch information
Guenter Quast committed Aug 30, 2023
1 parent 71057b8 commit be01e6a
Show file tree
Hide file tree
Showing 26 changed files with 1,848 additions and 1,523 deletions.
273 changes: 157 additions & 116 deletions ipynb_examples/Beispiel_Diodenkennlinie.ipynb
Expand Up @@ -51,123 +51,164 @@
"import numpy as np, matplotlib.pyplot as plt\n",
"from PhyPraKit import xyFit, k2Fit\n",
"\n",
"\n",
"# define the model function to fit\n",
"def Shockley(U, I_s = 0.5, U0 = 0.03):\n",
" \"\"\"Parametrisierung einer Diodenkennlinie\n",
"\n",
" U0 sollte während der Anpassung auf einen solchen Wert beschränkt \n",
" werden, dass U/U0<150 bleibt, um Überscheitungen des mit 64 Bit \n",
" Genauigkeit darstellbaren Zahlenraums zu verhindern\n",
"\n",
" Args:\n",
" - U: Spannung (V)\n",
" - I_s: Sperrstrom (nA)\n",
" - U0: Temperaturspannung (V) * Emissionskoeffizient\n",
" \n",
" Returns:\n",
" - float I: Strom (mA)\n",
" \"\"\"\n",
" return 1e-6 * I_s * np.exp( (U/U0) - 1.)\n",
"\n",
"\n",
"if __name__ == \"__main__\": # -------------------------------------- \n",
"\n",
"#\n",
"# Anpassung der Shockley-Gleichung an eine Diodenkennlinie \n",
"#\n",
" \n",
"\n",
"# Schockleygleichung als Fitfunktion setzen\n",
" model = Shockley \n",
"\n",
"# Messdaten:\n",
"# - Spannung im Messbereich 2V\n",
" data_x = [0.450, 0.470, 0.490, 0.510, 0.530, \n",
" 0.550, 0.560, 0.570, 0.580, 0.590, 0.600, 0.610, 0.620, 0.630,\n",
" 0.640, 0.645, 0.650, 0.655, 0.660, 0.665 ]\n",
"# - Strommessungen: 2 im Bereich 200µA, 12 mit 20mA und 6 mit 200mA\n",
" data_y = [0.056, 0.198, 0.284, 0.404, 0.739, 1.739, 1.962,\n",
" 2.849, 3.265, 5.706, 6.474, 7.866, 11.44, 18.98,\n",
" 23.35, 27.96, 38.61, 46.73, 49.78, 57.75]\n",
" \n",
"# Komponenten der Messunsicherheit\n",
"# - Genauigkeit Spannungsmessung: 4000 Counts, +/-(0.5% + 3 digits)\n",
"# - Messbereich 2V\n",
" crel_U = 0.005\n",
" Udigits = 3\n",
" Urange = 2\n",
" Ucounts = 4000\n",
"# - Genauigkeit Strommessung: 2000 Counts, +/-(1.0% + 3 digits) \n",
"# - Messbereiche 200µA, 20mA und 200mA \n",
" crel_I = 0.010\n",
" Idigits = 3\n",
" Icounts = 2000\n",
" Irange1 = 0.2\n",
" Irange2 = 20\n",
" Irange3 = 200\n",
"# - Rauschanteil (aus Fluktuationen der letzen Stelle)\n",
"# - delta U = 0.005 V\n",
" deltaU = 0.005\n",
"# - delta I = 0.025 mA\n",
" deltaI = 0.025\n",
"\n",
"# - Anzeigegenauigkeit der Spannung (V) \n",
" sx = Udigits * Urange / Ucounts\n",
" sabsx = np.sqrt(deltaU**2 + sx**2) # Rauschanteil addieren\n",
"# - korrelierte Kalibrationsunsicherheit \n",
" crelx = crel_U\n",
" \n",
"# - Anzeigegenauigkeit des Stroms (mA), 3 Messbereiche\n",
" sy = np.asarray( 2 * [Idigits * Irange1 / Icounts] + \\\n",
" 12 * [Idigits * Irange2 / Icounts] + \\\n",
" 6 * [Idigits * Irange3 / Icounts]) \n",
" sabsy = np.sqrt(deltaI**2 + sy**2) # Rauschanteil addieren\n",
"# - korrelierte Kalibrationsunsicherheit \n",
" crely = crel_I\n",
" \n",
"# Anpassung ausführen (mit Fit-Funktionen aus Paket PhyPraKit)\n",
" thisFit = xyFit # Alternativen: xyFit oder k2fit\n",
"## thisFit = k2Fit # Alternativen: xyFit oder k2fit\n",
" fitResult = thisFit(model,\n",
" # - data and uncertainties\n",
" data_x, data_y, # data x and y coordinates\n",
" sx=sabsx, # indep x\n",
" sy=sabsy, # indel y\n",
" xrelcor=crelx, # correlated rel. x\n",
" yrelcor=crely, # correlated rel. y\n",
" ref_to_model=True, # reference of rel. uncert. to model\n",
" # - fit control\n",
" p0=(0.2, 0.05), # initial guess for parameter values \n",
" limits=('U0', 0.005, None), # parameter limits\n",
"# - output options\n",
" plot=True, # plot data and model\n",
" plot_cor=False, # plot profiles likelihood and contours\n",
" showplots = False, # plt.show() in user code \n",
" quiet=False, # suppress informative printout\n",
" axis_labels=['U (V)', '$I_D$ (mA) \\ Shockley-Gl.'], \n",
" data_legend = 'Messwerte mit Unsicherheiten', \n",
" model_legend = 'Shockley-Gleichung'\n",
" )\n",
"\n",
"# adjust to different output formats of k2Fit and xyFit \n",
" if type(fitResult) is type({}):\n",
" pvals, puncs, cor, chi2, pnams = fitResult.values()\n",
" else:\n",
" pvals, puncs, cor, chi2 = fitResult\n",
" \n",
"# Ausgabe der Ergebnisse in Textform:\n",
" print('\\n*==* Fit Result:')\n",
" print(\" chi2: {:.3g}\".format(chi2))\n",
"## print(\" parameter names: \", pnams )\n",
" print(\" parameter values: \", pvals )\n",
" print(\" parameter uncertainties: \", puncs )\n",
" np.set_printoptions(precision=3)\n",
" print(\" correlations : \\n\", cor )\n",
"\n",
"# Anpassung der Optionen für grafische Darstellung\n",
" plt.figure(num=1) # activate first figure ...\n",
" plt.ylim(-1., 60.) # ... set y-limit ...\n",
" plt.show() # .. and show all figures\n"
"def Shockley(U, I_s=0.5, U0=0.03):\n",
" \"\"\"Parametrisierung einer Diodenkennlinie\n",
"\n",
" U0 sollte während der Anpassung auf einen solchen Wert beschränkt\n",
" werden, dass U/U0<150 bleibt, um Überscheitungen des mit 64 Bit\n",
" Genauigkeit darstellbaren Zahlenraums zu verhindern\n",
"\n",
" Args:\n",
" - U: Spannung (V)\n",
" - I_s: Sperrstrom (nA)\n",
" - U0: Temperaturspannung (V) * Emissionskoeffizient\n",
"\n",
" Returns:\n",
" - float I: Strom (mA)\n",
" \"\"\"\n",
" return 1e-6 * I_s * np.exp((U / U0) - 1.0)\n",
"\n",
"\n",
"if __name__ == \"__main__\": # --------------------------------------\n",
" #\n",
" # Anpassung der Shockley-Gleichung an eine Diodenkennlinie\n",
" #\n",
"\n",
" # Schockleygleichung als Fitfunktion setzen\n",
" model = Shockley\n",
"\n",
" # Messdaten:\n",
" # - Spannung im Messbereich 2V\n",
" data_x = [\n",
" 0.450,\n",
" 0.470,\n",
" 0.490,\n",
" 0.510,\n",
" 0.530,\n",
" 0.550,\n",
" 0.560,\n",
" 0.570,\n",
" 0.580,\n",
" 0.590,\n",
" 0.600,\n",
" 0.610,\n",
" 0.620,\n",
" 0.630,\n",
" 0.640,\n",
" 0.645,\n",
" 0.650,\n",
" 0.655,\n",
" 0.660,\n",
" 0.665,\n",
" ]\n",
" # - Strommessungen: 2 im Bereich 200µA, 12 mit 20mA und 6 mit 200mA\n",
" data_y = [\n",
" 0.056,\n",
" 0.198,\n",
" 0.284,\n",
" 0.404,\n",
" 0.739,\n",
" 1.739,\n",
" 1.962,\n",
" 2.849,\n",
" 3.265,\n",
" 5.706,\n",
" 6.474,\n",
" 7.866,\n",
" 11.44,\n",
" 18.98,\n",
" 23.35,\n",
" 27.96,\n",
" 38.61,\n",
" 46.73,\n",
" 49.78,\n",
" 57.75,\n",
" ]\n",
"\n",
" # Komponenten der Messunsicherheit\n",
" # - Genauigkeit Spannungsmessung: 4000 Counts, +/-(0.5% + 3 digits)\n",
" # - Messbereich 2V\n",
" crel_U = 0.005\n",
" Udigits = 3\n",
" Urange = 2\n",
" Ucounts = 4000\n",
" # - Genauigkeit Strommessung: 2000 Counts, +/-(1.0% + 3 digits)\n",
" # - Messbereiche 200µA, 20mA und 200mA\n",
" crel_I = 0.010\n",
" Idigits = 3\n",
" Icounts = 2000\n",
" Irange1 = 0.2\n",
" Irange2 = 20\n",
" Irange3 = 200\n",
" # - Rauschanteil (aus Fluktuationen der letzen Stelle)\n",
" # - delta U = 0.005 V\n",
" deltaU = 0.005\n",
" # - delta I = 0.025 mA\n",
" deltaI = 0.025\n",
"\n",
" # - Anzeigegenauigkeit der Spannung (V)\n",
" sx = Udigits * Urange / Ucounts\n",
" sabsx = np.sqrt(deltaU**2 + sx**2) # Rauschanteil addieren\n",
" # - korrelierte Kalibrationsunsicherheit\n",
" crelx = crel_U\n",
"\n",
" # - Anzeigegenauigkeit des Stroms (mA), 3 Messbereiche\n",
" sy = np.asarray(\n",
" 2 * [Idigits * Irange1 / Icounts]\n",
" + 12 * [Idigits * Irange2 / Icounts]\n",
" + 6 * [Idigits * Irange3 / Icounts]\n",
" )\n",
" sabsy = np.sqrt(deltaI**2 + sy**2) # Rauschanteil addieren\n",
" # - korrelierte Kalibrationsunsicherheit\n",
" crely = crel_I\n",
"\n",
" # Anpassung ausführen (mit Fit-Funktionen aus Paket PhyPraKit)\n",
" thisFit = xyFit # Alternativen: xyFit oder k2fit\n",
" ## thisFit = k2Fit # Alternativen: xyFit oder k2fit\n",
" fitResult = thisFit(\n",
" model,\n",
" # - data and uncertainties\n",
" data_x,\n",
" data_y, # data x and y coordinates\n",
" sx=sabsx, # indep x\n",
" sy=sabsy, # indel y\n",
" xrelcor=crelx, # correlated rel. x\n",
" yrelcor=crely, # correlated rel. y\n",
" ref_to_model=True, # reference of rel. uncert. to model\n",
" # - fit control\n",
" p0=(0.2, 0.05), # initial guess for parameter values\n",
" limits=(\"U0\", 0.005, None), # parameter limits\n",
" # - output options\n",
" plot=True, # plot data and model\n",
" plot_cor=False, # plot profiles likelihood and contours\n",
" showplots=False, # plt.show() in user code\n",
" quiet=False, # suppress informative printout\n",
" axis_labels=[\"U (V)\", \"$I_D$ (mA) \\ Shockley-Gl.\"],\n",
" data_legend=\"Messwerte mit Unsicherheiten\",\n",
" model_legend=\"Shockley-Gleichung\",\n",
" )\n",
"\n",
" # adjust to different output formats of k2Fit and xyFit\n",
" if type(fitResult) is type({}):\n",
" pvals, puncs, cor, chi2, pnams = fitResult.values()\n",
" else:\n",
" pvals, puncs, cor, chi2 = fitResult\n",
"\n",
" # Ausgabe der Ergebnisse in Textform:\n",
" print(\"\\n*==* Fit Result:\")\n",
" print(\" chi2: {:.3g}\".format(chi2))\n",
" ## print(\" parameter names: \", pnams )\n",
" print(\" parameter values: \", pvals)\n",
" print(\" parameter uncertainties: \", puncs)\n",
" np.set_printoptions(precision=3)\n",
" print(\" correlations : \\n\", cor)\n",
"\n",
" # Anpassung der Optionen für grafische Darstellung\n",
" plt.figure(num=1) # activate first figure ...\n",
" plt.ylim(-1.0, 60.0) # ... set y-limit ...\n",
" plt.show() # .. and show all figures"
]
}
],
Expand Down

0 comments on commit be01e6a

Please sign in to comment.