thzdaq-analysis 0.0.6b4__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- thzdaq_analysis-0.0.6b4/.gitignore +25 -0
- thzdaq_analysis-0.0.6b4/LICENSE +21 -0
- thzdaq_analysis-0.0.6b4/PKG-INFO +29 -0
- thzdaq_analysis-0.0.6b4/README.md +7 -0
- thzdaq_analysis-0.0.6b4/pyproject.toml +42 -0
- thzdaq_analysis-0.0.6b4/src/thzdaq_analysis/__init__.py +2 -0
- thzdaq_analysis-0.0.6b4/src/thzdaq_analysis/decibels.py +13 -0
- thzdaq_analysis-0.0.6b4/src/thzdaq_analysis/iv.py +489 -0
- thzdaq_analysis-0.0.6b4/src/thzdaq_analysis/noise.py +39 -0
- thzdaq_analysis-0.0.6b4/src/thzdaq_analysis/shot_noise.py +54 -0
- thzdaq_analysis-0.0.6b4/src/thzdaq_analysis/two_sb.py +47 -0
- thzdaq_analysis-0.0.6b4/tests/test_decibels.py +12 -0
- thzdaq_analysis-0.0.6b4/tests/test_iv.py +147 -0
- thzdaq_analysis-0.0.6b4/tests/test_noise_and_2sb.py +27 -0
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
.idea
|
|
2
|
+
venv
|
|
3
|
+
.venv
|
|
4
|
+
.uv-cache
|
|
5
|
+
build
|
|
6
|
+
dist
|
|
7
|
+
*__pycache__
|
|
8
|
+
*.log
|
|
9
|
+
*.DS_Store
|
|
10
|
+
*.spec
|
|
11
|
+
!ThzDAQ.spec
|
|
12
|
+
*.ipynb
|
|
13
|
+
.env
|
|
14
|
+
/data/
|
|
15
|
+
# What a daemon run from sources keeps between runs
|
|
16
|
+
/daemon.json
|
|
17
|
+
/daemon-sim.json
|
|
18
|
+
# Window layout and perspectives of a local run
|
|
19
|
+
/settings.ini
|
|
20
|
+
# What a daemon run from sources keeps of instruments (a grid's angle)
|
|
21
|
+
/state.json
|
|
22
|
+
/state-sim.json
|
|
23
|
+
# The program's own measurements and the profiles' previous versions
|
|
24
|
+
/measurements/
|
|
25
|
+
stations/.history/
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 yarvod
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: thzdaq-analysis
|
|
3
|
+
Version: 0.0.6b4
|
|
4
|
+
Summary: ThzDAQ analysis: I-V curves, noise temperature, shot noise, 2SB sideband ratios
|
|
5
|
+
Project-URL: Homepage, https://github.com/yarvod/ThzDAQ
|
|
6
|
+
Project-URL: Documentation, https://github.com/yarvod/ThzDAQ/blob/master/docs/daemon.md
|
|
7
|
+
Project-URL: Issues, https://github.com/yarvod/ThzDAQ/issues
|
|
8
|
+
Project-URL: Releases, https://github.com/yarvod/ThzDAQ/releases
|
|
9
|
+
Author: yarvod
|
|
10
|
+
License-Expression: MIT
|
|
11
|
+
License-File: LICENSE
|
|
12
|
+
Classifier: Development Status :: 3 - Alpha
|
|
13
|
+
Classifier: Intended Audience :: Science/Research
|
|
14
|
+
Classifier: Operating System :: OS Independent
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
18
|
+
Classifier: Topic :: Scientific/Engineering :: Physics
|
|
19
|
+
Requires-Python: >=3.11
|
|
20
|
+
Requires-Dist: numpy>=1.26
|
|
21
|
+
Description-Content-Type: text/markdown
|
|
22
|
+
|
|
23
|
+
# thzdaq-analysis
|
|
24
|
+
|
|
25
|
+
Calculations on measured data of a SIS mixer bench: I-V curve analysis (Rn, Rj, Q, the
|
|
26
|
+
gap), noise temperature from hot/cold powers, shot noise, 2SB sideband rejection. Arrays
|
|
27
|
+
and numbers in, results out: no files, no instruments, no daemon; it depends only on
|
|
28
|
+
`numpy`. The bench daemon runs it as operations (`analyze_iv`); scripts and notebooks
|
|
29
|
+
may import it directly (`pip install thzdaq[analysis]`).
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
# thzdaq-analysis
|
|
2
|
+
|
|
3
|
+
Calculations on measured data of a SIS mixer bench: I-V curve analysis (Rn, Rj, Q, the
|
|
4
|
+
gap), noise temperature from hot/cold powers, shot noise, 2SB sideband rejection. Arrays
|
|
5
|
+
and numbers in, results out: no files, no instruments, no daemon; it depends only on
|
|
6
|
+
`numpy`. The bench daemon runs it as operations (`analyze_iv`); scripts and notebooks
|
|
7
|
+
may import it directly (`pip install thzdaq[analysis]`).
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "thzdaq-analysis"
|
|
3
|
+
version = "0.0.6b4"
|
|
4
|
+
description = "ThzDAQ analysis: I-V curves, noise temperature, shot noise, 2SB sideband ratios"
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
requires-python = ">=3.11"
|
|
7
|
+
license = "MIT"
|
|
8
|
+
license-files = ["LICENSE"]
|
|
9
|
+
authors = [{ name = "yarvod" }]
|
|
10
|
+
classifiers = [
|
|
11
|
+
"Development Status :: 3 - Alpha",
|
|
12
|
+
"Intended Audience :: Science/Research",
|
|
13
|
+
"Operating System :: OS Independent",
|
|
14
|
+
"Programming Language :: Python :: 3",
|
|
15
|
+
"Programming Language :: Python :: 3 :: Only",
|
|
16
|
+
"Programming Language :: Python :: 3.11",
|
|
17
|
+
"Topic :: Scientific/Engineering :: Physics",
|
|
18
|
+
]
|
|
19
|
+
dependencies = [
|
|
20
|
+
"numpy>=1.26",
|
|
21
|
+
]
|
|
22
|
+
|
|
23
|
+
[project.urls]
|
|
24
|
+
Homepage = "https://github.com/yarvod/ThzDAQ"
|
|
25
|
+
Documentation = "https://github.com/yarvod/ThzDAQ/blob/master/docs/daemon.md"
|
|
26
|
+
Issues = "https://github.com/yarvod/ThzDAQ/issues"
|
|
27
|
+
Releases = "https://github.com/yarvod/ThzDAQ/releases"
|
|
28
|
+
|
|
29
|
+
[dependency-groups]
|
|
30
|
+
dev = [
|
|
31
|
+
"pytest>=8.2",
|
|
32
|
+
]
|
|
33
|
+
|
|
34
|
+
[build-system]
|
|
35
|
+
requires = ["hatchling>=1.27"]
|
|
36
|
+
build-backend = "hatchling.build"
|
|
37
|
+
|
|
38
|
+
[tool.hatch.build.targets.wheel]
|
|
39
|
+
packages = ["src/thzdaq_analysis"]
|
|
40
|
+
|
|
41
|
+
[tool.pytest.ini_options]
|
|
42
|
+
testpaths = ["tests"]
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"""Power ratios in decibels."""
|
|
2
|
+
|
|
3
|
+
import math
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def db_to_ratio(db: float) -> float:
|
|
7
|
+
return 10 ** (db / 10)
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def ratio_to_db(ratio: float) -> float:
|
|
11
|
+
if ratio <= 0:
|
|
12
|
+
raise ValueError("Ratio must be positive to convert to dB")
|
|
13
|
+
return 10 * math.log10(ratio)
|
|
@@ -0,0 +1,489 @@
|
|
|
1
|
+
"""I-V curve analysis: Rn fit, Rj supporting tangent, Q and the gap (mV / µA in, as plotted).
|
|
2
|
+
|
|
3
|
+
Moved from the legacy utils/iv_analysis.py without changes in behaviour.
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
from dataclasses import dataclass, field
|
|
7
|
+
from typing import Optional, Protocol, Sequence
|
|
8
|
+
|
|
9
|
+
import numpy as np
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
@dataclass(frozen=True)
|
|
13
|
+
class IVAnalysisConfig:
|
|
14
|
+
rn_min_voltage_mv: float = 4.0
|
|
15
|
+
rn_max_voltage_mv: Optional[float] = None
|
|
16
|
+
rj_min_voltage_mv: float = 0.0
|
|
17
|
+
rj_max_voltage_mv: float = 2.8
|
|
18
|
+
|
|
19
|
+
def __post_init__(self):
|
|
20
|
+
if (
|
|
21
|
+
self.rn_max_voltage_mv is not None
|
|
22
|
+
and self.rn_max_voltage_mv <= self.rn_min_voltage_mv
|
|
23
|
+
):
|
|
24
|
+
raise ValueError("Rn maximum voltage must be greater than its minimum")
|
|
25
|
+
if self.rj_max_voltage_mv <= max(0.0, self.rj_min_voltage_mv):
|
|
26
|
+
raise ValueError(
|
|
27
|
+
"Rj maximum voltage must be positive and greater than its minimum"
|
|
28
|
+
)
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
@dataclass(frozen=True)
|
|
32
|
+
class IVGapConfig:
|
|
33
|
+
gap_min_voltage_mv: float = 2.2
|
|
34
|
+
gap_max_voltage_mv: float = 3.0
|
|
35
|
+
gap_slope_factor: float = 2.0
|
|
36
|
+
smoothing_window: int = 31
|
|
37
|
+
smoothing_degree: int = 3
|
|
38
|
+
interpolation_points: int = 1001
|
|
39
|
+
|
|
40
|
+
def __post_init__(self):
|
|
41
|
+
if self.gap_max_voltage_mv <= max(0.0, self.gap_min_voltage_mv):
|
|
42
|
+
raise ValueError(
|
|
43
|
+
"Gap maximum voltage must be positive and greater than its minimum"
|
|
44
|
+
)
|
|
45
|
+
if self.gap_slope_factor <= 1.0:
|
|
46
|
+
raise ValueError("Gap slope factor must be greater than one")
|
|
47
|
+
if self.smoothing_window < 3 or self.smoothing_window % 2 == 0:
|
|
48
|
+
raise ValueError("Smoothing window must be an odd integer of at least 3")
|
|
49
|
+
if self.smoothing_degree < 1:
|
|
50
|
+
raise ValueError("Smoothing degree must be positive")
|
|
51
|
+
if self.smoothing_degree >= self.smoothing_window:
|
|
52
|
+
raise ValueError("Smoothing degree must be smaller than its window")
|
|
53
|
+
if self.interpolation_points < self.smoothing_window:
|
|
54
|
+
raise ValueError(
|
|
55
|
+
"Interpolation point count must not be smaller than smoothing window"
|
|
56
|
+
)
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
@dataclass(frozen=True)
|
|
60
|
+
class IVAnalysisParameters:
|
|
61
|
+
resistance: IVAnalysisConfig = field(default_factory=IVAnalysisConfig)
|
|
62
|
+
gap: IVGapConfig = field(default_factory=IVGapConfig)
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
@dataclass(frozen=True)
|
|
66
|
+
class LinearFit:
|
|
67
|
+
resistance_ohm: float
|
|
68
|
+
slope_ua_per_mv: float
|
|
69
|
+
intercept_ua: float
|
|
70
|
+
r_squared: float
|
|
71
|
+
reference_voltage_mv: float
|
|
72
|
+
reference_current_ua: float
|
|
73
|
+
point_count: int
|
|
74
|
+
line_voltage_range_mv: tuple[float, float]
|
|
75
|
+
|
|
76
|
+
def current_ua(self, voltage_mv):
|
|
77
|
+
return self.slope_ua_per_mv * voltage_mv + self.intercept_ua
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
@dataclass(frozen=True)
|
|
81
|
+
class SupportingTangent:
|
|
82
|
+
resistance_ohm: float
|
|
83
|
+
slope_ua_per_mv: float
|
|
84
|
+
touch_voltage_mv: float
|
|
85
|
+
touch_current_ua: float
|
|
86
|
+
minimum_clearance_ua: float
|
|
87
|
+
point_count: int
|
|
88
|
+
line_voltage_range_mv: tuple[float, float]
|
|
89
|
+
|
|
90
|
+
def current_ua(self, voltage_mv):
|
|
91
|
+
return self.slope_ua_per_mv * voltage_mv
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
@dataclass(frozen=True)
|
|
95
|
+
class GapFeatures:
|
|
96
|
+
voltage_mv: float
|
|
97
|
+
current_step_ua: float
|
|
98
|
+
lower_voltage_mv: float
|
|
99
|
+
lower_current_ua: float
|
|
100
|
+
upper_voltage_mv: float
|
|
101
|
+
upper_current_ua: float
|
|
102
|
+
upper_method: str
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
@dataclass(frozen=True)
|
|
106
|
+
class IVResistanceAnalysis:
|
|
107
|
+
rn_fit: LinearFit
|
|
108
|
+
rj_tangent: SupportingTangent
|
|
109
|
+
q: float
|
|
110
|
+
gap: Optional[GapFeatures] = None
|
|
111
|
+
|
|
112
|
+
@property
|
|
113
|
+
def rn_ohm(self) -> float:
|
|
114
|
+
return self.rn_fit.resistance_ohm
|
|
115
|
+
|
|
116
|
+
@property
|
|
117
|
+
def rj_ohm(self) -> float:
|
|
118
|
+
return self.rj_tangent.resistance_ohm
|
|
119
|
+
|
|
120
|
+
@property
|
|
121
|
+
def vgap_mv(self) -> Optional[float]:
|
|
122
|
+
return None if self.gap is None else self.gap.voltage_mv
|
|
123
|
+
|
|
124
|
+
@property
|
|
125
|
+
def igap_ua(self) -> Optional[float]:
|
|
126
|
+
return None if self.gap is None else self.gap.current_step_ua
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
class IVAnalyzer(Protocol):
|
|
130
|
+
def analyze(
|
|
131
|
+
self,
|
|
132
|
+
voltage_mv: Sequence[float],
|
|
133
|
+
current_ua: Sequence[float],
|
|
134
|
+
) -> IVResistanceAnalysis:
|
|
135
|
+
...
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
class IVGapAnalyzer(Protocol):
|
|
139
|
+
def analyze(
|
|
140
|
+
self,
|
|
141
|
+
voltage_mv: Sequence[float],
|
|
142
|
+
current_ua: Sequence[float],
|
|
143
|
+
rn_fit: LinearFit,
|
|
144
|
+
) -> Optional[GapFeatures]:
|
|
145
|
+
...
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
class IVAnalyzerFactory(Protocol):
|
|
149
|
+
def create(self, parameters: IVAnalysisParameters) -> IVAnalyzer:
|
|
150
|
+
...
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
class NumpyIVResistanceAnalyzer:
|
|
154
|
+
"""Calculate Rn, the lower supporting-tangent Rj, and Q for one I-V curve.
|
|
155
|
+
|
|
156
|
+
Input voltage is in mV and current is in µA. The analyzer has no Qt or
|
|
157
|
+
plotting dependencies, so its range policy can be replaced through config
|
|
158
|
+
or the whole implementation can be injected through ``IVAnalyzer``.
|
|
159
|
+
"""
|
|
160
|
+
|
|
161
|
+
def __init__(
|
|
162
|
+
self,
|
|
163
|
+
config: Optional[IVAnalysisConfig] = None,
|
|
164
|
+
gap_analyzer: Optional[IVGapAnalyzer] = None,
|
|
165
|
+
):
|
|
166
|
+
self.config = config or IVAnalysisConfig()
|
|
167
|
+
self.gap_analyzer = gap_analyzer or NumpyIVGapAnalyzer()
|
|
168
|
+
|
|
169
|
+
def analyze(
|
|
170
|
+
self,
|
|
171
|
+
voltage_mv: Sequence[float],
|
|
172
|
+
current_ua: Sequence[float],
|
|
173
|
+
) -> IVResistanceAnalysis:
|
|
174
|
+
voltage = np.asarray(voltage_mv, dtype=float)
|
|
175
|
+
current = np.asarray(current_ua, dtype=float)
|
|
176
|
+
if voltage.ndim != 1 or current.ndim != 1:
|
|
177
|
+
raise ValueError("Voltage and current must be one-dimensional arrays")
|
|
178
|
+
if voltage.shape != current.shape:
|
|
179
|
+
raise ValueError("Voltage and current arrays must have the same shape")
|
|
180
|
+
|
|
181
|
+
finite = np.isfinite(voltage) & np.isfinite(current)
|
|
182
|
+
if np.count_nonzero(finite) < 2:
|
|
183
|
+
raise ValueError("The I-V curve must contain at least two finite points")
|
|
184
|
+
|
|
185
|
+
rn_fit = self._calculate_rn(voltage, current, finite)
|
|
186
|
+
rj_tangent = self._calculate_rj(voltage, current, finite)
|
|
187
|
+
return IVResistanceAnalysis(
|
|
188
|
+
rn_fit=rn_fit,
|
|
189
|
+
rj_tangent=rj_tangent,
|
|
190
|
+
q=rj_tangent.resistance_ohm / rn_fit.resistance_ohm,
|
|
191
|
+
gap=self.gap_analyzer.analyze(
|
|
192
|
+
voltage[finite],
|
|
193
|
+
current[finite],
|
|
194
|
+
rn_fit,
|
|
195
|
+
),
|
|
196
|
+
)
|
|
197
|
+
|
|
198
|
+
def _calculate_rn(self, voltage, current, finite) -> LinearFit:
|
|
199
|
+
config = self.config
|
|
200
|
+
rn_mask = finite & (voltage >= config.rn_min_voltage_mv)
|
|
201
|
+
if config.rn_max_voltage_mv is not None:
|
|
202
|
+
rn_mask &= voltage <= config.rn_max_voltage_mv
|
|
203
|
+
if np.count_nonzero(rn_mask) < 2:
|
|
204
|
+
raise ValueError("Not enough points in the Rn fit range")
|
|
205
|
+
|
|
206
|
+
rn_voltage = voltage[rn_mask]
|
|
207
|
+
rn_current = current[rn_mask]
|
|
208
|
+
if np.unique(rn_voltage).size < 2:
|
|
209
|
+
raise ValueError("The Rn fit requires at least two different voltages")
|
|
210
|
+
|
|
211
|
+
slope, intercept = np.polyfit(rn_voltage, rn_current, 1)
|
|
212
|
+
slope = float(slope)
|
|
213
|
+
intercept = float(intercept)
|
|
214
|
+
if slope <= 0 or np.isclose(slope, 0):
|
|
215
|
+
raise ValueError("The Rn fit must have a positive non-zero slope")
|
|
216
|
+
|
|
217
|
+
model_current = slope * rn_voltage + intercept
|
|
218
|
+
residual_sum = float(np.sum((rn_current - model_current) ** 2))
|
|
219
|
+
total_sum = float(np.sum((rn_current - np.mean(rn_current)) ** 2))
|
|
220
|
+
r_squared = 1.0 - residual_sum / total_sum if total_sum > 0 else np.nan
|
|
221
|
+
reference_index = int(np.argmin(np.abs(rn_current - model_current)))
|
|
222
|
+
line_max_voltage = (
|
|
223
|
+
float(np.max(rn_voltage))
|
|
224
|
+
if config.rn_max_voltage_mv is None
|
|
225
|
+
else config.rn_max_voltage_mv
|
|
226
|
+
)
|
|
227
|
+
|
|
228
|
+
return LinearFit(
|
|
229
|
+
resistance_ohm=1000.0 / slope,
|
|
230
|
+
slope_ua_per_mv=slope,
|
|
231
|
+
intercept_ua=intercept,
|
|
232
|
+
r_squared=float(r_squared),
|
|
233
|
+
reference_voltage_mv=float(rn_voltage[reference_index]),
|
|
234
|
+
reference_current_ua=float(rn_current[reference_index]),
|
|
235
|
+
point_count=int(np.count_nonzero(rn_mask)),
|
|
236
|
+
line_voltage_range_mv=(config.rn_min_voltage_mv, line_max_voltage),
|
|
237
|
+
)
|
|
238
|
+
|
|
239
|
+
def _calculate_rj(self, voltage, current, finite) -> SupportingTangent:
|
|
240
|
+
config = self.config
|
|
241
|
+
rj_mask = (
|
|
242
|
+
finite
|
|
243
|
+
& (voltage > max(0.0, config.rj_min_voltage_mv))
|
|
244
|
+
& (voltage <= config.rj_max_voltage_mv)
|
|
245
|
+
& (current > 0.0)
|
|
246
|
+
)
|
|
247
|
+
if np.count_nonzero(rj_mask) < 2:
|
|
248
|
+
raise ValueError("Not enough positive points in the Rj tangent range")
|
|
249
|
+
|
|
250
|
+
indices = np.flatnonzero(rj_mask)
|
|
251
|
+
indices = indices[np.argsort(voltage[indices], kind="stable")]
|
|
252
|
+
slopes = current[indices] / voltage[indices]
|
|
253
|
+
minimum_slope = float(np.min(slopes))
|
|
254
|
+
equal_minimum = np.isclose(
|
|
255
|
+
slopes,
|
|
256
|
+
minimum_slope,
|
|
257
|
+
rtol=1e-12,
|
|
258
|
+
atol=1e-12,
|
|
259
|
+
)
|
|
260
|
+
touch_index = int(indices[np.flatnonzero(equal_minimum)[0]])
|
|
261
|
+
touch_voltage = float(voltage[touch_index])
|
|
262
|
+
touch_current = float(current[touch_index])
|
|
263
|
+
slope = touch_current / touch_voltage
|
|
264
|
+
|
|
265
|
+
clearance = current[indices] - slope * voltage[indices]
|
|
266
|
+
minimum_clearance = float(np.min(clearance))
|
|
267
|
+
clearance_tolerance = 1e-10 * max(
|
|
268
|
+
1.0,
|
|
269
|
+
float(np.max(np.abs(current[indices]))),
|
|
270
|
+
)
|
|
271
|
+
if minimum_clearance < -clearance_tolerance:
|
|
272
|
+
raise RuntimeError("The calculated Rj tangent crosses the I-V curve")
|
|
273
|
+
|
|
274
|
+
return SupportingTangent(
|
|
275
|
+
resistance_ohm=1000.0 / slope,
|
|
276
|
+
slope_ua_per_mv=slope,
|
|
277
|
+
touch_voltage_mv=touch_voltage,
|
|
278
|
+
touch_current_ua=touch_current,
|
|
279
|
+
minimum_clearance_ua=minimum_clearance,
|
|
280
|
+
point_count=len(indices),
|
|
281
|
+
line_voltage_range_mv=(0.0, config.rj_max_voltage_mv),
|
|
282
|
+
)
|
|
283
|
+
|
|
284
|
+
|
|
285
|
+
class NumpyIVGapAnalyzer:
|
|
286
|
+
"""Extract gap features from an I-V curve independently of Qt and plotting."""
|
|
287
|
+
|
|
288
|
+
def __init__(self, config: Optional[IVGapConfig] = None):
|
|
289
|
+
self.config = config or IVGapConfig()
|
|
290
|
+
|
|
291
|
+
def analyze(
|
|
292
|
+
self,
|
|
293
|
+
voltage_mv: Sequence[float],
|
|
294
|
+
current_ua: Sequence[float],
|
|
295
|
+
rn_fit: LinearFit,
|
|
296
|
+
) -> Optional[GapFeatures]:
|
|
297
|
+
config = self.config
|
|
298
|
+
voltage = np.asarray(voltage_mv, dtype=float)
|
|
299
|
+
current = np.asarray(current_ua, dtype=float)
|
|
300
|
+
if voltage.ndim != 1 or current.ndim != 1 or voltage.shape != current.shape:
|
|
301
|
+
return None
|
|
302
|
+
finite = np.isfinite(voltage) & np.isfinite(current)
|
|
303
|
+
positive = finite & (voltage >= 0.0)
|
|
304
|
+
source_voltage, source_current = self._sorted_unique_curve(
|
|
305
|
+
voltage[positive],
|
|
306
|
+
current[positive],
|
|
307
|
+
)
|
|
308
|
+
if (
|
|
309
|
+
source_voltage.size < 4
|
|
310
|
+
or source_voltage[0] > config.gap_min_voltage_mv
|
|
311
|
+
or source_voltage[-1] < config.gap_max_voltage_mv
|
|
312
|
+
):
|
|
313
|
+
return None
|
|
314
|
+
|
|
315
|
+
dense_voltage = np.linspace(
|
|
316
|
+
source_voltage[0],
|
|
317
|
+
source_voltage[-1],
|
|
318
|
+
config.interpolation_points,
|
|
319
|
+
)
|
|
320
|
+
dense_current = np.interp(dense_voltage, source_voltage, source_current)
|
|
321
|
+
smooth_current = self._savitzky_golay(
|
|
322
|
+
dense_current,
|
|
323
|
+
config.smoothing_window,
|
|
324
|
+
config.smoothing_degree,
|
|
325
|
+
)
|
|
326
|
+
conductance = np.gradient(smooth_current, dense_voltage)
|
|
327
|
+
|
|
328
|
+
gap_indices = np.flatnonzero(
|
|
329
|
+
(dense_voltage >= config.gap_min_voltage_mv)
|
|
330
|
+
& (dense_voltage <= config.gap_max_voltage_mv)
|
|
331
|
+
& np.isfinite(conductance)
|
|
332
|
+
)
|
|
333
|
+
if gap_indices.size < 3:
|
|
334
|
+
return None
|
|
335
|
+
|
|
336
|
+
peak_index = int(gap_indices[np.argmax(conductance[gap_indices])])
|
|
337
|
+
target_conductance = config.gap_slope_factor * rn_fit.slope_ua_per_mv
|
|
338
|
+
if conductance[peak_index] < target_conductance:
|
|
339
|
+
return None
|
|
340
|
+
|
|
341
|
+
lower_region = gap_indices[gap_indices <= peak_index]
|
|
342
|
+
upper_region = gap_indices[gap_indices >= peak_index]
|
|
343
|
+
if lower_region.size < 2 or upper_region.size < 2:
|
|
344
|
+
return None
|
|
345
|
+
|
|
346
|
+
lower_voltage = self._last_level_crossing(
|
|
347
|
+
dense_voltage,
|
|
348
|
+
conductance,
|
|
349
|
+
lower_region,
|
|
350
|
+
target_conductance,
|
|
351
|
+
)
|
|
352
|
+
if lower_voltage is None:
|
|
353
|
+
lower_index = int(
|
|
354
|
+
lower_region[
|
|
355
|
+
np.argmin(np.abs(conductance[lower_region] - target_conductance))
|
|
356
|
+
]
|
|
357
|
+
)
|
|
358
|
+
lower_voltage = float(dense_voltage[lower_index])
|
|
359
|
+
lower_current = float(np.interp(lower_voltage, dense_voltage, smooth_current))
|
|
360
|
+
|
|
361
|
+
normal_residual = smooth_current - rn_fit.current_ua(dense_voltage)
|
|
362
|
+
upper_voltage = self._first_zero_crossing(
|
|
363
|
+
dense_voltage,
|
|
364
|
+
normal_residual,
|
|
365
|
+
upper_region,
|
|
366
|
+
)
|
|
367
|
+
upper_method = "normal_fit_intersection"
|
|
368
|
+
if upper_voltage is None:
|
|
369
|
+
upper_voltage = self._first_level_crossing(
|
|
370
|
+
dense_voltage,
|
|
371
|
+
conductance,
|
|
372
|
+
upper_region,
|
|
373
|
+
target_conductance,
|
|
374
|
+
)
|
|
375
|
+
upper_method = "double_normal_slope"
|
|
376
|
+
if upper_voltage is None:
|
|
377
|
+
upper_index = int(
|
|
378
|
+
upper_region[
|
|
379
|
+
np.argmin(np.abs(conductance[upper_region] - target_conductance))
|
|
380
|
+
]
|
|
381
|
+
)
|
|
382
|
+
upper_voltage = float(dense_voltage[upper_index])
|
|
383
|
+
upper_method = "nearest_double_normal_slope"
|
|
384
|
+
|
|
385
|
+
upper_current = float(np.interp(upper_voltage, dense_voltage, smooth_current))
|
|
386
|
+
if upper_voltage <= lower_voltage or upper_current <= lower_current:
|
|
387
|
+
return None
|
|
388
|
+
|
|
389
|
+
center_current = lower_current + (upper_current - lower_current) / 2.0
|
|
390
|
+
center_region = np.flatnonzero(
|
|
391
|
+
(dense_voltage >= lower_voltage) & (dense_voltage <= upper_voltage)
|
|
392
|
+
)
|
|
393
|
+
center_voltage = self._first_level_crossing(
|
|
394
|
+
dense_voltage,
|
|
395
|
+
smooth_current,
|
|
396
|
+
center_region,
|
|
397
|
+
center_current,
|
|
398
|
+
)
|
|
399
|
+
if center_voltage is None:
|
|
400
|
+
center_index = int(
|
|
401
|
+
center_region[
|
|
402
|
+
np.argmin(np.abs(smooth_current[center_region] - center_current))
|
|
403
|
+
]
|
|
404
|
+
)
|
|
405
|
+
center_voltage = float(dense_voltage[center_index])
|
|
406
|
+
|
|
407
|
+
return GapFeatures(
|
|
408
|
+
voltage_mv=center_voltage,
|
|
409
|
+
current_step_ua=upper_current - lower_current,
|
|
410
|
+
lower_voltage_mv=lower_voltage,
|
|
411
|
+
lower_current_ua=lower_current,
|
|
412
|
+
upper_voltage_mv=upper_voltage,
|
|
413
|
+
upper_current_ua=upper_current,
|
|
414
|
+
upper_method=upper_method,
|
|
415
|
+
)
|
|
416
|
+
|
|
417
|
+
@staticmethod
|
|
418
|
+
def _sorted_unique_curve(voltage, current):
|
|
419
|
+
if voltage.size == 0:
|
|
420
|
+
return voltage, current
|
|
421
|
+
order = np.argsort(voltage, kind="stable")
|
|
422
|
+
voltage = voltage[order]
|
|
423
|
+
current = current[order]
|
|
424
|
+
unique_voltage, inverse = np.unique(voltage, return_inverse=True)
|
|
425
|
+
if unique_voltage.size == voltage.size:
|
|
426
|
+
return voltage, current
|
|
427
|
+
current_sum = np.zeros(unique_voltage.size, dtype=float)
|
|
428
|
+
point_count = np.zeros(unique_voltage.size, dtype=int)
|
|
429
|
+
np.add.at(current_sum, inverse, current)
|
|
430
|
+
np.add.at(point_count, inverse, 1)
|
|
431
|
+
return unique_voltage, current_sum / point_count
|
|
432
|
+
|
|
433
|
+
@staticmethod
|
|
434
|
+
def _savitzky_golay(values, requested_window: int, degree: int):
|
|
435
|
+
point_count = len(values)
|
|
436
|
+
window = min(requested_window, point_count)
|
|
437
|
+
if window % 2 == 0:
|
|
438
|
+
window -= 1
|
|
439
|
+
minimum_window = degree + 1
|
|
440
|
+
if minimum_window % 2 == 0:
|
|
441
|
+
minimum_window += 1
|
|
442
|
+
if window < minimum_window:
|
|
443
|
+
return np.asarray(values, dtype=float)
|
|
444
|
+
|
|
445
|
+
half_window = window // 2
|
|
446
|
+
positions = np.arange(-half_window, half_window + 1, dtype=float)
|
|
447
|
+
design = np.vander(positions, degree + 1, increasing=True)
|
|
448
|
+
coefficients = np.linalg.pinv(design)[0]
|
|
449
|
+
padded = np.pad(values, half_window, mode="reflect")
|
|
450
|
+
return np.convolve(padded, coefficients[::-1], mode="valid")
|
|
451
|
+
|
|
452
|
+
@classmethod
|
|
453
|
+
def _first_zero_crossing(cls, x, values, indices) -> Optional[float]:
|
|
454
|
+
return cls._first_level_crossing(x, values, indices, 0.0)
|
|
455
|
+
|
|
456
|
+
@staticmethod
|
|
457
|
+
def _level_crossings(x, values, indices, level: float) -> list[float]:
|
|
458
|
+
crossings = []
|
|
459
|
+
for left, right in zip(indices[:-1], indices[1:]):
|
|
460
|
+
left_delta = values[left] - level
|
|
461
|
+
right_delta = values[right] - level
|
|
462
|
+
if left_delta == 0:
|
|
463
|
+
crossings.append(float(x[left]))
|
|
464
|
+
continue
|
|
465
|
+
if left_delta * right_delta > 0:
|
|
466
|
+
continue
|
|
467
|
+
fraction = -left_delta / (right_delta - left_delta)
|
|
468
|
+
crossings.append(float(x[left] + fraction * (x[right] - x[left])))
|
|
469
|
+
if indices.size and values[indices[-1]] == level:
|
|
470
|
+
crossings.append(float(x[indices[-1]]))
|
|
471
|
+
return crossings
|
|
472
|
+
|
|
473
|
+
@classmethod
|
|
474
|
+
def _first_level_crossing(cls, x, values, indices, level) -> Optional[float]:
|
|
475
|
+
crossings = cls._level_crossings(x, values, indices, level)
|
|
476
|
+
return crossings[0] if crossings else None
|
|
477
|
+
|
|
478
|
+
@classmethod
|
|
479
|
+
def _last_level_crossing(cls, x, values, indices, level) -> Optional[float]:
|
|
480
|
+
crossings = cls._level_crossings(x, values, indices, level)
|
|
481
|
+
return crossings[-1] if crossings else None
|
|
482
|
+
|
|
483
|
+
|
|
484
|
+
class NumpyIVAnalyzerFactory:
|
|
485
|
+
def create(self, parameters: IVAnalysisParameters) -> IVAnalyzer:
|
|
486
|
+
return NumpyIVResistanceAnalyzer(
|
|
487
|
+
config=parameters.resistance,
|
|
488
|
+
gap_analyzer=NumpyIVGapAnalyzer(parameters.gap),
|
|
489
|
+
)
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"""Hot/cold load (Y-factor) noise temperature."""
|
|
2
|
+
|
|
3
|
+
import math
|
|
4
|
+
|
|
5
|
+
from thzdaq_analysis.decibels import db_to_ratio
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def y_factor_db(hot_dbm: float, cold_dbm: float) -> float:
|
|
9
|
+
return hot_dbm - cold_dbm
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def noise_temperature(
|
|
13
|
+
hot_dbm: float, cold_dbm: float, t_hot: float = 300.0, t_cold: float = 77.0
|
|
14
|
+
) -> float:
|
|
15
|
+
"""Receiver noise temperature, K. NaN when Y <= 1 (no usable hot/cold contrast)."""
|
|
16
|
+
y = db_to_ratio(hot_dbm - cold_dbm)
|
|
17
|
+
if y <= 1:
|
|
18
|
+
return math.nan
|
|
19
|
+
return (t_hot - y * t_cold) / (y - 1)
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def match_hot_cold(hot, cold, window: int = 10):
|
|
23
|
+
"""Pair hot and cold readings measured at (nearly) the same bias.
|
|
24
|
+
|
|
25
|
+
hot, cold: sequences of (voltage, power_dbm) in sweep order. Each hot point
|
|
26
|
+
is paired with the cold point of the closest voltage among `window`
|
|
27
|
+
neighbours of the same index. Returns [(voltage, hot_dbm, cold_dbm)].
|
|
28
|
+
"""
|
|
29
|
+
pairs = []
|
|
30
|
+
count = min(len(hot), len(cold))
|
|
31
|
+
left = window // 2
|
|
32
|
+
right = left + window % 2
|
|
33
|
+
for index in range(count):
|
|
34
|
+
low = max(index - left, 0)
|
|
35
|
+
high = min(index + right + 1, len(cold))
|
|
36
|
+
voltage, hot_power = hot[index]
|
|
37
|
+
nearest = min(range(low, high), key=lambda j: abs(cold[j][0] - voltage))
|
|
38
|
+
pairs.append((voltage, hot_power, cold[nearest][1]))
|
|
39
|
+
return pairs
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
"""IF amplifier noise from SIS shot noise at two bias points above the gap."""
|
|
2
|
+
|
|
3
|
+
import math
|
|
4
|
+
from typing import Sequence, Tuple
|
|
5
|
+
|
|
6
|
+
ELEMENTARY_CHARGE = 1.602176634e-19 # C
|
|
7
|
+
BOLTZMANN = 1.380649e-23 # J/K
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def linear_fit(x: Sequence[float], y: Sequence[float]) -> Tuple[float, float]:
|
|
11
|
+
"""Least squares y = slope * x + intercept."""
|
|
12
|
+
n = len(x)
|
|
13
|
+
if n < 2:
|
|
14
|
+
raise ValueError("need at least two points")
|
|
15
|
+
mean_x, mean_y = sum(x) / n, sum(y) / n
|
|
16
|
+
sxx = sum((xi - mean_x) ** 2 for xi in x)
|
|
17
|
+
if sxx == 0:
|
|
18
|
+
raise ValueError("x values are all equal")
|
|
19
|
+
slope = sum((xi - mean_x) * (yi - mean_y) for xi, yi in zip(x, y)) / sxx
|
|
20
|
+
return slope, mean_y - slope * mean_x
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def dynamic_resistance(voltages: Sequence[float], currents: Sequence[float]) -> float:
|
|
24
|
+
"""dV/dI around a bias point, Ohm."""
|
|
25
|
+
slope, _ = linear_fit(voltages, currents)
|
|
26
|
+
return 1 / slope
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def shot_noise_temperature(
|
|
30
|
+
current: float, voltage: float, rd: float, t_sis: float = 4.0
|
|
31
|
+
) -> float:
|
|
32
|
+
"""Equivalent IF noise temperature of the SIS shot noise, K."""
|
|
33
|
+
x = ELEMENTARY_CHARGE * voltage / (BOLTZMANN * t_sis)
|
|
34
|
+
return ELEMENTARY_CHARGE * rd * current / (2 * BOLTZMANN * math.tanh(x))
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def mismatch(z_sis: float, z_load: float = 50.0) -> float:
|
|
38
|
+
"""Power transmission 1 - |Γ|² between the SIS and the IF chain."""
|
|
39
|
+
return 1 - abs((z_sis - z_load) / (z_sis + z_load)) ** 2
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def amplifier_temperature(
|
|
43
|
+
power1_w: float,
|
|
44
|
+
power2_w: float,
|
|
45
|
+
t1: float,
|
|
46
|
+
t2: float,
|
|
47
|
+
m1: float,
|
|
48
|
+
m2: float,
|
|
49
|
+
) -> float:
|
|
50
|
+
"""IF amplifier noise, K, from IF powers (W) at bias points 1 and 2."""
|
|
51
|
+
y = power2_w / power1_w
|
|
52
|
+
if y == 1:
|
|
53
|
+
return math.nan
|
|
54
|
+
return (y * t1 * m1 - m2 * t2) / (1 - y)
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
"""Sideband separating (2SB) mixer calculations, powers in dBm."""
|
|
2
|
+
|
|
3
|
+
from typing import Tuple
|
|
4
|
+
|
|
5
|
+
from thzdaq_analysis.decibels import db_to_ratio, ratio_to_db
|
|
6
|
+
|
|
7
|
+
SIDE_BANDS = ("upper", "lower")
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def test_tone_frequency(
|
|
11
|
+
lo_frequency: float, if_frequency: float, side_band: str
|
|
12
|
+
) -> float:
|
|
13
|
+
if side_band == "upper":
|
|
14
|
+
return lo_frequency + if_frequency
|
|
15
|
+
return lo_frequency - if_frequency
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def power_difference(power_upper: float, power_lower: float, side_band: str) -> float:
|
|
19
|
+
"""Wanted IF channel minus the image one, dB."""
|
|
20
|
+
if side_band == "upper":
|
|
21
|
+
return power_upper - power_lower
|
|
22
|
+
return power_lower - power_upper
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def sideband_rejection(
|
|
26
|
+
p_upper_hot: float,
|
|
27
|
+
p_upper_cold: float,
|
|
28
|
+
p_lower_hot: float,
|
|
29
|
+
p_lower_cold: float,
|
|
30
|
+
usb_power_upper: float,
|
|
31
|
+
usb_power_lower: float,
|
|
32
|
+
lsb_power_upper: float,
|
|
33
|
+
lsb_power_lower: float,
|
|
34
|
+
) -> Tuple[float, float]:
|
|
35
|
+
"""SRR (dB) of the upper and lower channels.
|
|
36
|
+
|
|
37
|
+
usb_* are IF channel powers with the test tone in the upper sideband,
|
|
38
|
+
lsb_* with the tone in the lower one; p_* are hot/cold load powers.
|
|
39
|
+
"""
|
|
40
|
+
m_dsb = (db_to_ratio(p_upper_hot) - db_to_ratio(p_upper_cold)) / (
|
|
41
|
+
db_to_ratio(p_lower_hot) - db_to_ratio(p_lower_cold)
|
|
42
|
+
)
|
|
43
|
+
mu = db_to_ratio(usb_power_upper - usb_power_lower)
|
|
44
|
+
ml = db_to_ratio(lsb_power_lower - lsb_power_upper)
|
|
45
|
+
r1 = mu * (ml * m_dsb - 1) / (mu - m_dsb)
|
|
46
|
+
r2 = ml * (mu - m_dsb) / (ml * m_dsb - 1)
|
|
47
|
+
return ratio_to_db(abs(r1)), ratio_to_db(abs(r2))
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import pytest
|
|
2
|
+
from thzdaq_analysis.decibels import db_to_ratio, ratio_to_db
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
def test_db_roundtrip():
|
|
6
|
+
assert db_to_ratio(3) == pytest.approx(1.995, rel=1e-3)
|
|
7
|
+
assert ratio_to_db(db_to_ratio(2)) == pytest.approx(2)
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def test_non_positive_ratio_rejected():
|
|
11
|
+
with pytest.raises(ValueError):
|
|
12
|
+
ratio_to_db(0)
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import unittest
|
|
2
|
+
|
|
3
|
+
import numpy as np
|
|
4
|
+
from thzdaq_analysis.iv import (
|
|
5
|
+
IVAnalysisConfig,
|
|
6
|
+
IVGapConfig,
|
|
7
|
+
NumpyIVGapAnalyzer,
|
|
8
|
+
NumpyIVResistanceAnalyzer,
|
|
9
|
+
)
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class IVResistanceAnalyzerTest(unittest.TestCase):
|
|
13
|
+
@staticmethod
|
|
14
|
+
def _gap_curve(overshoot_ua=0.0):
|
|
15
|
+
voltage_mv = np.linspace(0.1, 6.0, 300)
|
|
16
|
+
subgap_current_ua = 20.0 * voltage_mv
|
|
17
|
+
normal_current_ua = 100.0 * voltage_mv - 180.0
|
|
18
|
+
transition = 1.0 / (1.0 + np.exp(-(voltage_mv - 2.58) / 0.045))
|
|
19
|
+
current_ua = (
|
|
20
|
+
subgap_current_ua + (normal_current_ua - subgap_current_ua) * transition
|
|
21
|
+
)
|
|
22
|
+
current_ua += overshoot_ua * np.exp(-(((voltage_mv - 2.72) / 0.07) ** 2))
|
|
23
|
+
return voltage_mv, current_ua
|
|
24
|
+
|
|
25
|
+
def test_calculates_rn_supporting_tangent_and_q(self):
|
|
26
|
+
voltage_mv = [-2.0, 0.0, 2.5, 1.0, 6.0, 2.0, 4.0, 0.5, 5.0, np.nan]
|
|
27
|
+
current_ua = [
|
|
28
|
+
-100.0,
|
|
29
|
+
0.0,
|
|
30
|
+
140.0,
|
|
31
|
+
60.0,
|
|
32
|
+
300.0,
|
|
33
|
+
100.0,
|
|
34
|
+
100.0,
|
|
35
|
+
40.0,
|
|
36
|
+
200.0,
|
|
37
|
+
np.nan,
|
|
38
|
+
]
|
|
39
|
+
|
|
40
|
+
result = NumpyIVResistanceAnalyzer().analyze(voltage_mv, current_ua)
|
|
41
|
+
|
|
42
|
+
self.assertAlmostEqual(result.rn_ohm, 10.0)
|
|
43
|
+
self.assertAlmostEqual(result.rj_ohm, 20.0)
|
|
44
|
+
self.assertAlmostEqual(result.q, 2.0)
|
|
45
|
+
self.assertAlmostEqual(result.rn_fit.slope_ua_per_mv, 100.0)
|
|
46
|
+
self.assertAlmostEqual(result.rn_fit.intercept_ua, -300.0)
|
|
47
|
+
self.assertAlmostEqual(result.rn_fit.r_squared, 1.0)
|
|
48
|
+
self.assertEqual(result.rn_fit.point_count, 3)
|
|
49
|
+
self.assertAlmostEqual(result.rj_tangent.touch_voltage_mv, 2.0)
|
|
50
|
+
self.assertAlmostEqual(result.rj_tangent.touch_current_ua, 100.0)
|
|
51
|
+
self.assertGreaterEqual(result.rj_tangent.minimum_clearance_ua, 0.0)
|
|
52
|
+
|
|
53
|
+
def test_equal_supporting_slopes_choose_the_lowest_voltage(self):
|
|
54
|
+
voltage_mv = [2.0, 1.0, 2.5, 4.0, 5.0]
|
|
55
|
+
current_ua = [100.0, 50.0, 140.0, 100.0, 200.0]
|
|
56
|
+
|
|
57
|
+
result = NumpyIVResistanceAnalyzer().analyze(voltage_mv, current_ua)
|
|
58
|
+
|
|
59
|
+
self.assertAlmostEqual(result.rj_tangent.touch_voltage_mv, 1.0)
|
|
60
|
+
self.assertAlmostEqual(result.rj_tangent.touch_current_ua, 50.0)
|
|
61
|
+
|
|
62
|
+
def test_calculates_gap_from_normal_fit_intersection(self):
|
|
63
|
+
voltage_mv, current_ua = self._gap_curve(overshoot_ua=30.0)
|
|
64
|
+
|
|
65
|
+
result = NumpyIVResistanceAnalyzer().analyze(voltage_mv, current_ua)
|
|
66
|
+
|
|
67
|
+
self.assertIsNotNone(result.gap)
|
|
68
|
+
self.assertEqual(result.gap.upper_method, "normal_fit_intersection")
|
|
69
|
+
self.assertGreater(result.vgap_mv, result.gap.lower_voltage_mv)
|
|
70
|
+
self.assertLess(result.vgap_mv, result.gap.upper_voltage_mv)
|
|
71
|
+
self.assertGreater(result.igap_ua, 0.0)
|
|
72
|
+
|
|
73
|
+
def test_uses_double_normal_slope_when_fit_does_not_cross_curve(self):
|
|
74
|
+
voltage_mv, current_ua = self._gap_curve()
|
|
75
|
+
|
|
76
|
+
result = NumpyIVResistanceAnalyzer().analyze(voltage_mv, current_ua)
|
|
77
|
+
|
|
78
|
+
self.assertIsNotNone(result.gap)
|
|
79
|
+
self.assertEqual(result.gap.upper_method, "double_normal_slope")
|
|
80
|
+
self.assertGreater(result.igap_ua, 0.0)
|
|
81
|
+
|
|
82
|
+
def test_leaves_gap_empty_when_curve_has_no_knee(self):
|
|
83
|
+
voltage_mv = np.linspace(0.1, 6.0, 100)
|
|
84
|
+
current_ua = 50.0 * voltage_mv
|
|
85
|
+
|
|
86
|
+
result = NumpyIVResistanceAnalyzer().analyze(voltage_mv, current_ua)
|
|
87
|
+
|
|
88
|
+
self.assertIsNone(result.gap)
|
|
89
|
+
self.assertIsNone(result.vgap_mv)
|
|
90
|
+
self.assertIsNone(result.igap_ua)
|
|
91
|
+
|
|
92
|
+
def test_gap_analyzer_can_be_injected(self):
|
|
93
|
+
class NoGapAnalyzer:
|
|
94
|
+
def __init__(self):
|
|
95
|
+
self.calls = []
|
|
96
|
+
|
|
97
|
+
def analyze(self, voltage_mv, current_ua, rn_fit):
|
|
98
|
+
self.calls.append((voltage_mv, current_ua, rn_fit))
|
|
99
|
+
return None
|
|
100
|
+
|
|
101
|
+
gap_analyzer = NoGapAnalyzer()
|
|
102
|
+
voltage_mv, current_ua = self._gap_curve()
|
|
103
|
+
|
|
104
|
+
result = NumpyIVResistanceAnalyzer(gap_analyzer=gap_analyzer).analyze(
|
|
105
|
+
voltage_mv,
|
|
106
|
+
current_ua,
|
|
107
|
+
)
|
|
108
|
+
|
|
109
|
+
self.assertIsNone(result.gap)
|
|
110
|
+
self.assertEqual(len(gap_analyzer.calls), 1)
|
|
111
|
+
|
|
112
|
+
def test_rejects_incompatible_or_insufficient_data(self):
|
|
113
|
+
analyzer = NumpyIVResistanceAnalyzer()
|
|
114
|
+
|
|
115
|
+
with self.assertRaisesRegex(ValueError, "same shape"):
|
|
116
|
+
analyzer.analyze([1.0, 2.0], [1.0])
|
|
117
|
+
with self.assertRaisesRegex(ValueError, "Rn fit range"):
|
|
118
|
+
analyzer.analyze([0.5, 1.0, 2.0], [30.0, 50.0, 100.0])
|
|
119
|
+
|
|
120
|
+
def test_validates_analysis_ranges(self):
|
|
121
|
+
with self.assertRaisesRegex(ValueError, "Rn maximum"):
|
|
122
|
+
NumpyIVResistanceAnalyzer(
|
|
123
|
+
IVAnalysisConfig(
|
|
124
|
+
rn_min_voltage_mv=4.0,
|
|
125
|
+
rn_max_voltage_mv=3.0,
|
|
126
|
+
)
|
|
127
|
+
)
|
|
128
|
+
with self.assertRaisesRegex(ValueError, "Rj maximum"):
|
|
129
|
+
NumpyIVResistanceAnalyzer(
|
|
130
|
+
IVAnalysisConfig(
|
|
131
|
+
rj_min_voltage_mv=1.0,
|
|
132
|
+
rj_max_voltage_mv=1.0,
|
|
133
|
+
)
|
|
134
|
+
)
|
|
135
|
+
with self.assertRaisesRegex(ValueError, "Gap maximum"):
|
|
136
|
+
NumpyIVGapAnalyzer(
|
|
137
|
+
IVGapConfig(
|
|
138
|
+
gap_min_voltage_mv=3.0,
|
|
139
|
+
gap_max_voltage_mv=2.2,
|
|
140
|
+
)
|
|
141
|
+
)
|
|
142
|
+
with self.assertRaisesRegex(ValueError, "Smoothing window"):
|
|
143
|
+
NumpyIVGapAnalyzer(IVGapConfig(smoothing_window=30))
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
if __name__ == "__main__":
|
|
147
|
+
unittest.main()
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import pytest
|
|
2
|
+
from thzdaq_analysis.noise import match_hot_cold
|
|
3
|
+
from thzdaq_analysis.shot_noise import dynamic_resistance, linear_fit
|
|
4
|
+
from thzdaq_analysis.two_sb import sideband_rejection
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def test_match_hot_cold_pairs_nearest_voltage():
|
|
8
|
+
hot = [(1.0, -30), (2.0, -29), (3.0, -28)]
|
|
9
|
+
cold = [(1.1, -32), (1.9, -31), (3.05, -30)]
|
|
10
|
+
assert match_hot_cold(hot, cold, window=2) == [
|
|
11
|
+
(1.0, -30, -32),
|
|
12
|
+
(2.0, -29, -31),
|
|
13
|
+
(3.0, -28, -30),
|
|
14
|
+
]
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def test_linear_fit_and_resistance():
|
|
18
|
+
assert linear_fit([0, 1, 2], [1, 3, 5]) == pytest.approx((2, 1))
|
|
19
|
+
assert dynamic_resistance([0, 1e-3], [0, 50e-6]) == pytest.approx(20)
|
|
20
|
+
with pytest.raises(ValueError):
|
|
21
|
+
linear_fit([1, 1], [1, 2])
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def test_srr_of_ideal_mixer_matches_tone_ratio():
|
|
25
|
+
# Equal channel gains: SRR equals the tone ratio
|
|
26
|
+
upper, lower = sideband_rejection(-30, -32, -30, -32, -20, -40, -40, -20)
|
|
27
|
+
assert upper == pytest.approx(20) and lower == pytest.approx(20)
|