qc#
Quality-control checks and trend tracking for routine measurements.
Define a QC configuration listing the metrics to watch (SNR, linewidth, peak
position, …) with pass/fail limits, run measure on a reference
spectrum to record pass/fail, then review history, trends, statistics, and
control charts over time. Useful for daily instrument checks.
add_metric
#
add_metric(
config_name: str,
metric_type: str,
name: str | None = None,
unit: str = "",
lower_limit: float | None = None,
upper_limit: float | None = None,
target_value: float | None = None,
peak_index: int | None = None,
region_start_hz: float | None = None,
region_end_hz: float | None = None,
) -> bool
Add a metric to an existing configuration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config_name
|
str
|
Name of the configuration to modify. |
required |
metric_type
|
str
|
Type of metric ("snr", "linewidth_hz", etc.). |
required |
name
|
str | None
|
Display name (defaults to metric_type if not provided). |
None
|
unit
|
str
|
Unit string. |
''
|
lower_limit
|
float | None
|
Minimum acceptable value. |
None
|
upper_limit
|
float | None
|
Maximum acceptable value. |
None
|
target_value
|
float | None
|
Expected nominal value. |
None
|
peak_index
|
int | None
|
For peak-specific metrics. |
None
|
region_start_hz
|
float | None
|
Start of integration region (for integral). |
None
|
region_end_hz
|
float | None
|
End of integration region (for integral). |
None
|
Returns:
| Type | Description |
|---|---|
bool
|
True if successful, False if configuration not found. |
clear_history
#
Clear QC history.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config_name
|
str | None
|
Clear only for this config (None for all). |
None
|
Returns:
| Type | Description |
|---|---|
int
|
Number of records deleted. |
create_configuration
#
create_configuration(
name: str,
reference_sample: str = "",
metrics: list[dict[str, Any]] | None = None,
sequence_preset: str | None = None,
enabled: bool = True,
) -> dict[str, Any]
Create a new QC configuration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Configuration name (e.g., "daily_snr_check"). |
required |
reference_sample
|
str
|
Reference sample description (e.g., "sucrose_standard"). |
''
|
metrics
|
list[dict[str, Any]] | None
|
List of metric definitions, each containing: - type: Metric type ("snr", "linewidth_hz", "peak_position_hz", "peak_position_ppm", "integral", "baseline_rms", "custom") - name: Display name for the metric - unit: Unit string (optional) - lower_limit: Fail if value below this (optional) - upper_limit: Fail if value above this (optional) - target_value: Expected nominal value (optional) - peak_index: For peak-specific metrics (optional) - region_start_hz, region_end_hz: For integral metrics (optional) |
None
|
sequence_preset
|
str | None
|
Optional sequence preset name for QC acquisition. |
None
|
enabled
|
bool
|
Whether the configuration is active. |
True
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
A summary of the created configuration with keys: |
dict[str, Any]
|
|
dict[str, Any]
|
|
delete_configuration
#
Delete a QC configuration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Configuration name to delete. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if deleted, False if not found. |
generate_chart
#
generate_chart(
config_name: str,
metric_name: str,
days: int = 30,
width: int = 800,
height: int = 400,
scale: float = 2.0,
title: str | None = None,
) -> str | None
Generate a QC control chart image as base64-encoded PNG.
Use this to create custom charts for reports with specific parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config_name
|
str
|
QC configuration name. |
required |
metric_name
|
str
|
Metric to chart. |
required |
days
|
int
|
Number of days of trend data to include. |
30
|
width
|
int
|
Image width in pixels. |
800
|
height
|
int
|
Image height in pixels. |
400
|
scale
|
float
|
Scale factor for high-DPI output. |
2.0
|
title
|
str | None
|
Custom chart title (auto-generated if None). |
None
|
Returns:
| Type | Description |
|---|---|
str | None
|
Base64-encoded PNG image string, or None if no data/error. |
generate_chart_html
#
generate_chart_html(
config_name: str,
metric_name: str,
days: int = 30,
width: int = 800,
height: int = 400,
scale: float = 2.0,
title: str | None = None,
) -> str
Generate a QC control chart as an HTML img tag.
Like generate_chart() but returns complete HTML that can be directly embedded in report templates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config_name
|
str
|
QC configuration name. |
required |
metric_name
|
str
|
Metric to chart. |
required |
days
|
int
|
Number of days of trend data to include. |
30
|
width
|
int
|
Image width in pixels. |
800
|
height
|
int
|
Image height in pixels. |
400
|
scale
|
float
|
Scale factor for high-DPI output. |
2.0
|
title
|
str | None
|
Custom chart title (auto-generated if None). |
None
|
Returns:
| Type | Description |
|---|---|
str
|
HTML img tag with embedded base64 image, or error message. |
get_configuration
#
Get a QC configuration by name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Configuration name. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any] | None
|
The configuration as a dictionary, or |
dict[str, Any] | None
|
|
dict[str, Any] | None
|
|
dict[str, Any] | None
|
|
dict[str, Any] | None
|
|
get_history
#
Get QC measurement history.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config_name
|
str | None
|
Filter by configuration (None for all). |
None
|
limit
|
int
|
Maximum records to return. |
100
|
Returns:
| Name | Type | Description |
|---|---|---|
list[dict[str, Any]]
|
A list of past measurement records, newest first. Each record has |
|
keys |
list[dict[str, Any]]
|
|
list[dict[str, Any]]
|
|
get_statistics
#
Get statistics for a specific metric.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config_name
|
str
|
QC configuration name. |
required |
metric_name
|
str
|
Metric to analyze. |
required |
days
|
int
|
Number of days of history. |
30
|
Returns:
| Type | Description |
|---|---|
dict[str, Any] | None
|
Statistics dictionary or None if no data: |
dict[str, Any] | None
|
|
dict[str, Any] | None
|
|
dict[str, Any] | None
|
|
dict[str, Any] | None
|
|
dict[str, Any] | None
|
|
dict[str, Any] | None
|
|
dict[str, Any] | None
|
|
dict[str, Any] | None
|
|
get_trend
#
Get trend data for a specific metric.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config_name
|
str
|
QC configuration name. |
required |
metric_name
|
str
|
Metric to get trend for. |
required |
days
|
int
|
Number of days of history. |
30
|
Returns:
| Type | Description |
|---|---|
list[dict[str, Any]]
|
List of {timestamp, value, passed} dicts. |
list_configurations
#
List all QC configuration names.
Returns:
| Type | Description |
|---|---|
list[str]
|
List of configuration names. |
measure
#
measure(
config_name: str,
spectrum: Spectrum1D | None = None,
scan_id: str | None = None,
notes: str = "",
dry_run: bool = False,
) -> dict[str, Any]
Run QC measurement using current or provided spectrum.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config_name
|
str
|
Name of QC configuration to use. |
required |
spectrum
|
Spectrum1D | None
|
Spectrum to analyze (uses current spectrum if None). |
None
|
scan_id
|
str | None
|
Optional scan ID to associate with measurement. |
None
|
notes
|
str
|
Optional notes. |
''
|
dry_run
|
bool
|
If True, compute metrics but don't save to history. Use this to preview results before committing. |
False
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary with measurement results: |
dict[str, Any]
|
|
dict[str, Any]
|
|
dict[str, Any]
|
|
dict[str, Any]
|
|
dict[str, Any]
|
|
Example
# Preview first
preview = qc.measure("daily_snr", dry_run=True)
print(f"Preview: {'PASS' if preview['overall_pass'] else 'FAIL'}")
# Then record if satisfied
result = qc.measure("daily_snr")
if result["overall_pass"]:
print("QC PASSED!")
else:
for m in result["measurements"]:
if not m["passed"]:
print(f"FAILED: {m['name']} = {m['value']}")
set_report_options
#
set_report_options(
config_name: str | None = None,
days: int = 30,
width: int = 800,
height: int = 400,
scale: float = 2.0,
include_charts: bool = True,
) -> None
Set options for QC data in auto-generated reports.
These options affect how QC data appears in reports when using the {{qc}} template variable. Call before generating a report.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config_name
|
str | None
|
Only include this config in reports (None = all). |
None
|
days
|
int
|
Days of trend data to include. |
30
|
width
|
int
|
Chart width in pixels. |
800
|
height
|
int
|
Chart height in pixels. |
400
|
scale
|
float
|
Chart scale factor. |
2.0
|
include_charts
|
bool
|
Whether to generate chart images. |
True
|