Files

469 lines
24 KiB
HTML

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1">
<meta name="generator" content="pdoc3 0.11.1">
<title>defs.TOC API documentation</title>
<meta name="description" content="This submodule contains functionality to calibrate TOC measurement data and analyse them.">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/10up-sanitize.css/13.0.0/sanitize.min.css" integrity="sha512-y1dtMcuvtTMJc1yPgEqF0ZjQbhnc/bFhyvIyVNb9Zk5mIGtqVaAB1Ttl28su8AvFMOY0EwRbAe+HCLqj6W7/KA==" crossorigin>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/10up-sanitize.css/13.0.0/typography.min.css" integrity="sha512-Y1DYSb995BAfxobCkKepB1BqJJTPrOp3zPL74AWFugHHmmdcvO+C48WLrUOlhGMc0QG7AE3f7gmvvcrmX2fDoA==" crossorigin>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/default.min.css" crossorigin>
<style>:root{--highlight-color:#fe9}.flex{display:flex !important}body{line-height:1.5em}#content{padding:20px}#sidebar{padding:1.5em;overflow:hidden}#sidebar > *:last-child{margin-bottom:2cm}.http-server-breadcrumbs{font-size:130%;margin:0 0 15px 0}#footer{font-size:.75em;padding:5px 30px;border-top:1px solid #ddd;text-align:right}#footer p{margin:0 0 0 1em;display:inline-block}#footer p:last-child{margin-right:30px}h1,h2,h3,h4,h5{font-weight:300}h1{font-size:2.5em;line-height:1.1em}h2{font-size:1.75em;margin:2em 0 .50em 0}h3{font-size:1.4em;margin:1.6em 0 .7em 0}h4{margin:0;font-size:105%}h1:target,h2:target,h3:target,h4:target,h5:target,h6:target{background:var(--highlight-color);padding:.2em 0}a{color:#058;text-decoration:none;transition:color .2s ease-in-out}a:visited{color:#503}a:hover{color:#b62}.title code{font-weight:bold}h2[id^="header-"]{margin-top:2em}.ident{color:#900;font-weight:bold}pre code{font-size:.8em;line-height:1.4em;padding:1em;display:block}code{background:#f3f3f3;font-family:"DejaVu Sans Mono",monospace;padding:1px 4px;overflow-wrap:break-word}h1 code{background:transparent}pre{border-top:1px solid #ccc;border-bottom:1px solid #ccc;margin:1em 0}#http-server-module-list{display:flex;flex-flow:column}#http-server-module-list div{display:flex}#http-server-module-list dt{min-width:10%}#http-server-module-list p{margin-top:0}.toc ul,#index{list-style-type:none;margin:0;padding:0}#index code{background:transparent}#index h3{border-bottom:1px solid #ddd}#index ul{padding:0}#index h4{margin-top:.6em;font-weight:bold}@media (min-width:200ex){#index .two-column{column-count:2}}@media (min-width:300ex){#index .two-column{column-count:3}}dl{margin-bottom:2em}dl dl:last-child{margin-bottom:4em}dd{margin:0 0 1em 3em}#header-classes + dl > dd{margin-bottom:3em}dd dd{margin-left:2em}dd p{margin:10px 0}.name{background:#eee;font-size:.85em;padding:5px 10px;display:inline-block;min-width:40%}.name:hover{background:#e0e0e0}dt:target .name{background:var(--highlight-color)}.name > span:first-child{white-space:nowrap}.name.class > span:nth-child(2){margin-left:.4em}.inherited{color:#999;border-left:5px solid #eee;padding-left:1em}.inheritance em{font-style:normal;font-weight:bold}.desc h2{font-weight:400;font-size:1.25em}.desc h3{font-size:1em}.desc dt code{background:inherit}.source summary,.git-link-div{color:#666;text-align:right;font-weight:400;font-size:.8em;text-transform:uppercase}.source summary > *{white-space:nowrap;cursor:pointer}.git-link{color:inherit;margin-left:1em}.source pre{max-height:500px;overflow:auto;margin:0}.source pre code{font-size:12px;overflow:visible}.hlist{list-style:none}.hlist li{display:inline}.hlist li:after{content:',\2002'}.hlist li:last-child:after{content:none}.hlist .hlist{display:inline;padding-left:1em}img{max-width:100%}td{padding:0 .5em}.admonition{padding:.1em 1em;margin-bottom:1em}.admonition-title{font-weight:bold}.admonition.note,.admonition.info,.admonition.important{background:#aef}.admonition.todo,.admonition.versionadded,.admonition.tip,.admonition.hint{background:#dfd}.admonition.warning,.admonition.versionchanged,.admonition.deprecated{background:#fd4}.admonition.error,.admonition.danger,.admonition.caution{background:lightpink}</style>
<style media="screen and (min-width: 700px)">@media screen and (min-width:700px){#sidebar{width:30%;height:100vh;overflow:auto;position:sticky;top:0}#content{width:70%;max-width:100ch;padding:3em 4em;border-left:1px solid #ddd}pre code{font-size:1em}.name{font-size:1em}main{display:flex;flex-direction:row-reverse;justify-content:flex-end}.toc ul ul,#index ul ul{padding-left:1em}.toc > ul > li{margin-top:.5em}}</style>
<style media="print">@media print{#sidebar h1{page-break-before:always}.source{display:none}}@media print{*{background:transparent !important;color:#000 !important;box-shadow:none !important;text-shadow:none !important}a[href]:after{content:" (" attr(href) ")";font-size:90%}a[href][title]:after{content:none}abbr[title]:after{content:" (" attr(title) ")"}.ir a:after,a[href^="javascript:"]:after,a[href^="#"]:after{content:""}pre,blockquote{border:1px solid #999;page-break-inside:avoid}thead{display:table-header-group}tr,img{page-break-inside:avoid}img{max-width:100% !important}@page{margin:0.5cm}p,h2,h3{orphans:3;widows:3}h1,h2,h3,h4,h5,h6{page-break-after:avoid}}</style>
<script type="text/x-mathjax-config">MathJax.Hub.Config({ tex2jax: { inlineMath: [ ['$','$'], ["\\(","\\)"] ], processEscapes: true } });</script>
<script async src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/latest.js?config=TeX-AMS_CHTML" integrity="sha256-kZafAc6mZvK3W3v1pHOcUix30OHQN6pU/NO2oFkqZVw=" crossorigin></script>
<script defer src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js" integrity="sha512-D9gUyxqja7hBtkWpPWGt9wfbfaMGVt9gnyCvYa+jojwwPHLCzUm5i8rpk7vD7wNee9bA35eYIjobYPaQuKS1MQ==" crossorigin></script>
<script>window.addEventListener('DOMContentLoaded', () => {
hljs.configure({languages: ['bash', 'css', 'diff', 'graphql', 'ini', 'javascript', 'json', 'plaintext', 'python', 'python-repl', 'rust', 'shell', 'sql', 'typescript', 'xml', 'yaml']});
hljs.highlightAll();
})</script>
</head>
<body>
<main>
<article id="content">
<header>
<h1 class="title">Module <code>defs.TOC</code></h1>
</header>
<section id="section-intro">
<p>This submodule contains functionality to calibrate TOC measurement data and analyse them.</p>
</section>
<section>
</section>
<section>
</section>
<section>
</section>
<section>
<h2 class="section-title" id="header-classes">Classes</h2>
<dl>
<dt id="defs.TOC.Calibration"><code class="flex name class">
<span>class <span class="ident">Calibration</span></span>
<span>(</span><span>cal_file: pathlib.Path)</span>
</code></dt>
<dd>
<div class="desc"></div>
<details class="source">
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">class Calibration():
def __init__(self, cal_file: Path):
self.metadata = CalibrationMetadata()
self.curve = CalibrationCurve()
self.cal_file = cal_file
self.calibrant_data = None
self.__results_index = None
self.excluded_points = None
self.__read_calibration_file()
self.mask = None
def __read_calibration_file(self):
conctr = []
diltns = []
area_1 = []
area_2 = []
area_3 = []
results_flag = False
with open(self.cal_file, &#39;r&#39;, errors = &#39;ignore&#39;) as file:
results_index = None
for index, line in enumerate(file.readlines()):
# Reading in the metadata
if index == 2:
self.metadata.standard = float(line.split()[1])
if index == 3:
self.metadata.unit = line.split()[1]
if index == 4:
self.metadata.prepared = line.split()[1]
if index == 5:
self.metadata.measured = line.split()[1]
# Reading in the calibration data
if index &gt;= 10 and results_flag == False:
if line.startswith(&#39;CALIBRATION RESULTS&#39;):
results_flag = True
continue
elif not results_flag:
if line == &#39;\n&#39;:
continue
items = line.split()
conctr.append(float(items[0]))
diltns.append(float(items[1]))
area_1.append(float(items[2]))
area_2.append(float(items[3]))
area_3.append(float(items[4]))
if results_flag == True:
if line.startswith(&#39;-&#39;):
if results_index is None:
self.__results_index = index + 1
continue
else:
continue
items = line.split()
if index == self.__results_index:
self.curve.exists = True
self.curve.slope = float(items[1])
elif index == self.__results_index + 1:
self.curve.intercept = float(items[1])
elif index == self.__results_index + 2:
self.curve.R2_value = float(items[1])
elif index == self.__results_index + 3:
if items[1] == &#39;None&#39;:
continue
else:
del items[0]
self.curve.excluded = [int(items[i].replace(&#39;,&#39;, &#39;&#39;)) for i in range(len(items))]
elif index == self.__results_index + 4:
self.curve.lower_bound = float(items[1])
elif index == self.__results_index + 5:
self.curve.upper_bound = float(items[1])
elif index == self.__results_index + 6:
self.curve.computed_on = pd.to_datetime(items[1] + &#39; &#39; + items[2])
self.calibrant_data = pd.DataFrame({&#39;concentration&#39;: conctr,
&#39;dilution&#39;: diltns,
&#39;area_1&#39;: area_1,
&#39;area_2&#39;: area_2,
&#39;area_3&#39;: area_3,
&#39;area_average&#39;: [(a1 + a2 + a3) / 3 for a1, a2, a3 in zip(area_1, area_2, area_3)]})
def produce_statistics():
# TODO
pass
def calibrate(self, excluded_points = [], write_to_new_file = False):
if excluded_points:
self.curve.excluded = excluded_points
self.mask = [False if i + 1 in self.curve.excluded else True for i in range(len(self.calibrant_data))]
else:
self.curve.excluded = None
self.mask = [True for i in range(len(self.calibrant_data))]
self.curve.calibrate(x_data=self.calibrant_data[&#39;area_average&#39;].values[self.mask], y_data=self.calibrant_data[&#39;concentration&#39;].values[self.mask])
with open(self.cal_file, &#39;r&#39;, errors = &#39;ignore&#39;) as file:
lines = file.readlines()
if self.__results_index is None:
lines.append(&#39;\nCALIBRATION RESULTS\n&#39;)
lines.append(&#39;----------------------------------------------------------------------\n&#39;)
lines.append(&#39;Slope\t\t&#39; + f&#34;{self.curve.slope:2.4f}\n&#34;)
lines.append(&#39;Intercept\t&#39; + f&#34;{self.curve.intercept:2.4f}\n&#34;)
lines.append(&#39;R2\t\t&#39; + f&#34;{self.curve.R2_value:1.4f}\n&#34;)
if self.curve.excluded is None:
lines.append(&#39;EXCLUDED\t&#39; + f&#34;None\n&#34;)
else:
lines.append(&#39;EXCLUDED\t&#39; + &#39;, &#39;.join([str(n) for n in self.curve.excluded]) + &#39;\n&#39;)
lines.append(&#39;FROM\t\t&#39; + f&#34;{self.curve.lower_bound:2.4f}\n&#34;)
lines.append(&#39;TO\t\t&#39; + f&#34;{self.curve.upper_bound:2.4f}\n&#34;)
lines.append(&#39;COMPUTED\t&#39; + f&#34;{self.curve.computed_on:%d.%m.%Y %H:%M:%S}&#34;)
else:
lines[self.__results_index] = &#39;Slope\t\t&#39; + f&#34;{self.curve.slope:2.4f}\n&#34;
lines[self.__results_index + 1] =&#39;Intercept\t&#39; + f&#34;{self.curve.intercept:2.4f}\n&#34;
lines[self.__results_index + 2] =&#39;R2\t\t&#39; + f&#34;{self.curve.R2_value:1.4f}\n&#34;
if self.curve.excluded is None:
lines[self.__results_index + 3] = &#39;EXCLUDED\t&#39; + f&#34;None\n&#34;
else:
lines[self.__results_index + 3] = &#39;EXCLUDED\t&#39; + &#39;, &#39;.join([str(n) for n in self.curve.excluded]) + &#39;\n&#39;
lines[self.__results_index + 4] =&#39;FROM\t\t&#39; + f&#34;{self.curve.lower_bound:2.4f}\n&#34;
lines[self.__results_index + 5] =&#39;TO\t\t&#39; + f&#34;{self.curve.upper_bound:2.4f}\n&#34;
lines[self.__results_index + 6] =&#39;COMPUTED\t&#39; + f&#34;{self.curve.computed_on:%d.%m.%Y %H:%M:%S}&#34;
if write_to_new_file == False:
with open(self.cal_file, &#39;w&#39;, errors = &#39;ignore&#39;) as file:
file.writelines(lines)
elif write_to_new_file == True:
directory_path = self.cal_file.parents[0]
new_file = self.cal_file.stem + &#39;_&#39; + self.curve.computed_on.strftime(&#39;%d_%m_%Y_%H_%M_%S&#39;) + &#39;.cal&#39;
new_path = PurePath(directory_path, new_file)
with open(new_path, &#39;w&#39;, errors = &#39;ignore&#39;) as file:
file.writelines(lines)
def compute(self, sample_areas):
return self.curve.compute(sample_areas=sample_areas)</code></pre>
</details>
<h3>Methods</h3>
<dl>
<dt id="defs.TOC.Calibration.calibrate"><code class="name flex">
<span>def <span class="ident">calibrate</span></span>(<span>self, excluded_points=[], write_to_new_file=False)</span>
</code></dt>
<dd>
<div class="desc"></div>
</dd>
<dt id="defs.TOC.Calibration.compute"><code class="name flex">
<span>def <span class="ident">compute</span></span>(<span>self, sample_areas)</span>
</code></dt>
<dd>
<div class="desc"></div>
</dd>
<dt id="defs.TOC.Calibration.produce_statistics"><code class="name flex">
<span>def <span class="ident">produce_statistics</span></span>(<span>)</span>
</code></dt>
<dd>
<div class="desc"></div>
</dd>
</dl>
</dd>
<dt id="defs.TOC.CalibrationCurve"><code class="flex name class">
<span>class <span class="ident">CalibrationCurve</span></span>
</code></dt>
<dd>
<div class="desc"><p>An object representing a calibration curve for TOC, IC or NPOC Measurements.</p>
<h2 id="description">Description</h2>
<p>This object instanciates a class that relates the area $A$ measured in the TOC Analyser to the concentration $c$ used in the calibration solutions. The calibration curve is affine, $c=aA+b$ where $a$ and $b$ denote slope and intercept, respectively, of the affine map. This object can be used to represent linear calibration curves for TC (total carbon)-, IC (inorganic carbon)- and NPOC (non-purgeable organic carbon) measurements. </p>
<h2 id="attributes">Attributes</h2>
<dl>
<dt><strong><code>exists</code></strong> :&ensp;<code>Bool</code></dt>
<dd>This value is set to <code>True</code> if a calibration curve is already available, else it is set to <code>False</code>.</dd>
<dt><strong><code>slope</code></strong> :&ensp;<code>float</code></dt>
<dd>Value of the slope for the calibration of concentration (dependent variable) with measured area (independent variable).</dd>
<dt><strong><code>intercep</code></strong> :&ensp;<code>float</code></dt>
<dd>Value of the intercep for the calibration of concentration (dependent variable) with measured area (independent variable).</dd>
<dt><strong><code>R2_value</code></strong> :&ensp;<code>float</code></dt>
<dd>The coefficient of determination of the calibration curve. For a good calibration curve, this should be above 0.995.\</dd>
<dt><strong><code>excluded</code></strong> :&ensp;<code>list</code></dt>
<dd>A list of integers denoting the samples in the calibrant data that are excluded.</dd>
<dt><strong><code>lower_bound</code></strong> :&ensp;<code>float</code></dt>
<dd>The lower bound value for which this calibration curve can be used.</dd>
<dt><strong><code>upper_bound</code></strong> :&ensp;<code>float</code></dt>
<dd>The upper bound value for which this calibration curve can be used.</dd>
<dt><strong><code>computed_on</code></strong> :&ensp;<code>pd.Timestamp</code></dt>
<dd>A timestamp indicating the time at which the calibration curve was computed.</dd>
</dl></div>
<details class="source">
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">class CalibrationCurve():
r&#34;&#34;&#34;An object representing a calibration curve for TOC, IC or NPOC Measurements.
Description
-----------
This object instanciates a class that relates the area $A$ measured in the TOC Analyser to the concentration $c$ used in the calibration solutions. The calibration curve is affine, $c=aA+b$ where $a$ and $b$ denote slope and intercept, respectively, of the affine map. This object can be used to represent linear calibration curves for TC (total carbon)-, IC (inorganic carbon)- and NPOC (non-purgeable organic carbon) measurements.
Attributes
----------
exists: Bool
This value is set to `True` if a calibration curve is already available, else it is set to `False`.
slope: float
Value of the slope for the calibration of concentration (dependent variable) with measured area (independent variable).
intercep: float
Value of the intercep for the calibration of concentration (dependent variable) with measured area (independent variable).
R2_value: float
The coefficient of determination of the calibration curve. For a good calibration curve, this should be above 0.995.\
excluded: list
A list of integers denoting the samples in the calibrant data that are excluded.
lower_bound: float
The lower bound value for which this calibration curve can be used.
upper_bound: float
The upper bound value for which this calibration curve can be used.
computed_on: pd.Timestamp
A timestamp indicating the time at which the calibration curve was computed.
&#34;&#34;&#34;
def __init__(self):
# initialises all member variables
self.exists = False
self.slope = None
self.intercept = None
self.R2_value = None
self.excluded = None
self.lower_bound = None
self.upper_bound = None
self.computed_on = None
def __linear_function(self, x_values, a, b):
r&#34;&#34;&#34;Computes the value of the calibration curve for the passed parameters.
Description
-----------
Computes the affine map, $c=aA+b$, given data $A$ and parameters $a$ (slope) and $b$ (intercept).
Parameters
----------
x_values: array-like
An array-like (array or list) object of length $n$ containing the data for the independent variable that are to be mapped.
a: float
The slope of the calibration curve.
b: float
The intercept of the calibration curve.
Returns
-------
y_values: np.ndarray
An array (of size $n$) containing the image under the affine map of `x_values`.
&#34;&#34;&#34;
if not isinstance(x_values, np.ndarray):
x_values = np.array(x_values)
return a * x_values + b
def __optimise(self, x_data, y_data, p0):
return curve_fit(self.__linear_function, x_data, y_data, p0)
def __check_exist(self):
if self.exists == True:
return True
else:
return False
def __check(self):
if self.__check_exist():
warnings.warn(&#34;Calibration alreadyt exists, will be overwritten...&#34;)
else:
return
def calibrate(self, x_data, y_data):
&#34;&#34;&#34;The docstring for this method
Description
-----------
Bla Bla Bla
Parameters
----------
x_data: np.ndarray
Bla bla bla
y_data: np.ndarray
Bla Bla Bla
&#34;&#34;&#34;
self.__check()
parameters, _ = self.__optimise(x_data=x_data, y_data=y_data, p0=[10, 0.1])
self.exists = True
self.slope = parameters[0]
self.intercept = parameters[1]
self.R2_value = compute_R2(y_true = y_data, y_est = self.__linear_function(x_values=x_data, a = parameters[0], b=parameters[1]))
self.lower_bound = np.min(y_data)
self.upper_bound = np.max(y_data)
self.computed_on = pd.to_datetime(datetime.datetime.now())
def compute(self, sample_areas):
return self.__linear_function(x_values=sample_areas, a = self.slope, b = self.intercept)</code></pre>
</details>
<h3>Methods</h3>
<dl>
<dt id="defs.TOC.CalibrationCurve.calibrate"><code class="name flex">
<span>def <span class="ident">calibrate</span></span>(<span>self, x_data, y_data)</span>
</code></dt>
<dd>
<div class="desc"><p>The docstring for this method</p>
<h2 id="description">Description</h2>
<p>Bla Bla Bla</p>
<h2 id="parameters">Parameters</h2>
<dl>
<dt><strong><code>x_data</code></strong> :&ensp;<code>np.ndarray</code></dt>
<dd>Bla bla bla</dd>
<dt><strong><code>y_data</code></strong> :&ensp;<code>np.ndarray</code></dt>
<dd>Bla Bla Bla</dd>
</dl></div>
</dd>
<dt id="defs.TOC.CalibrationCurve.compute"><code class="name flex">
<span>def <span class="ident">compute</span></span>(<span>self, sample_areas)</span>
</code></dt>
<dd>
<div class="desc"></div>
</dd>
</dl>
</dd>
<dt id="defs.TOC.CalibrationMetadata"><code class="flex name class">
<span>class <span class="ident">CalibrationMetadata</span></span>
</code></dt>
<dd>
<div class="desc"></div>
<details class="source">
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">class CalibrationMetadata():
def __init__(self):
self.standard = None
self.unit = None
self.prepared = None
self.measured = None</code></pre>
</details>
</dd>
</dl>
</section>
</article>
<nav id="sidebar">
<div class="toc">
<ul></ul>
</div>
<ul id="index">
<li><h3>Super-module</h3>
<ul>
<li><code><a title="defs" href="index.html">defs</a></code></li>
</ul>
</li>
<li><h3><a href="#header-classes">Classes</a></h3>
<ul>
<li>
<h4><code><a title="defs.TOC.Calibration" href="#defs.TOC.Calibration">Calibration</a></code></h4>
<ul class="">
<li><code><a title="defs.TOC.Calibration.calibrate" href="#defs.TOC.Calibration.calibrate">calibrate</a></code></li>
<li><code><a title="defs.TOC.Calibration.compute" href="#defs.TOC.Calibration.compute">compute</a></code></li>
<li><code><a title="defs.TOC.Calibration.produce_statistics" href="#defs.TOC.Calibration.produce_statistics">produce_statistics</a></code></li>
</ul>
</li>
<li>
<h4><code><a title="defs.TOC.CalibrationCurve" href="#defs.TOC.CalibrationCurve">CalibrationCurve</a></code></h4>
<ul class="">
<li><code><a title="defs.TOC.CalibrationCurve.calibrate" href="#defs.TOC.CalibrationCurve.calibrate">calibrate</a></code></li>
<li><code><a title="defs.TOC.CalibrationCurve.compute" href="#defs.TOC.CalibrationCurve.compute">compute</a></code></li>
</ul>
</li>
<li>
<h4><code><a title="defs.TOC.CalibrationMetadata" href="#defs.TOC.CalibrationMetadata">CalibrationMetadata</a></code></h4>
</li>
</ul>
</li>
</ul>
</nav>
</main>
<footer id="footer">
<p>Generated by <a href="https://pdoc3.github.io/pdoc" title="pdoc: Python API documentation generator"><cite>pdoc</cite> 0.11.1</a>.</p>
</footer>
</body>
</html>