PACS System 0.1.0
PACS DICOM system library
Loading...
Searching...
No Matches
ai_result_handler.h
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2021-2025, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
19#pragma once
20
24
25#include <kcenon/common/patterns/result.h>
26
27#include <chrono>
28#include <cstdint>
29#include <functional>
30#include <memory>
31#include <optional>
32#include <string>
33#include <string_view>
34#include <vector>
35
37
39template <typename T>
41
43using VoidResult = kcenon::common::VoidResult;
44
45// ─────────────────────────────────────────────────────
46// Enumerations
47// ─────────────────────────────────────────────────────
48
57
69
70// ─────────────────────────────────────────────────────
71// Data Structures
72// ─────────────────────────────────────────────────────
73
79 std::string sop_instance_uid;
80
83
85 std::string sop_class_uid;
86
89
91 std::string source_study_uid;
92
94 std::string algorithm_name;
95
97 std::string algorithm_version;
98
100 std::chrono::system_clock::time_point received_at;
101
103 std::optional<std::string> description;
104};
105
112
114 std::optional<std::string> series_instance_uid;
115
117 std::vector<std::string> sop_instance_uids;
118};
119
125 std::string finding_type;
126
128 std::string location;
129
131 std::optional<double> confidence;
132
134 std::optional<std::string> measurement;
135
137 std::optional<std::string> referenced_sop_instance_uid;
138};
139
146
148 std::string segment_label;
149
151 std::optional<std::string> description;
152
154 std::string algorithm_type;
155
157 std::optional<std::tuple<uint8_t, uint8_t, uint8_t>> recommended_display_color;
158};
159
166
168 std::optional<std::string> error_message;
169
171 std::vector<std::string> missing_tags;
172
174 std::vector<std::string> invalid_references;
175};
176
183
186
189
191 std::vector<std::string> accepted_sr_templates;
192
194 uint16_t max_segments = 256;
195};
196
197// ─────────────────────────────────────────────────────
198// Callback Types
199// ─────────────────────────────────────────────────────
200
206using ai_result_received_callback = std::function<void(const ai_result_info& info)>;
207
215using pre_store_validator = std::function<bool(
216 const core::dicom_dataset& dataset,
217 ai_result_type type)>;
218
219// ─────────────────────────────────────────────────────
220// Main Class
221// ─────────────────────────────────────────────────────
222
260public:
268 [[nodiscard]] static auto create(
269 std::shared_ptr<storage::storage_interface> storage,
270 std::shared_ptr<storage::index_database> database)
271 -> std::unique_ptr<ai_result_handler>;
272
277
278 // Non-copyable, movable
282 auto operator=(ai_result_handler&&) noexcept -> ai_result_handler&;
283
284 // ========================================================================
285 // Configuration
286 // ========================================================================
287
293 void configure(const ai_handler_config& config);
294
300 [[nodiscard]] auto get_config() const -> ai_handler_config;
301
308
315
316 // ========================================================================
317 // Structured Report (SR) Operations
318 // ========================================================================
319
331 [[nodiscard]] auto receive_structured_report(const core::dicom_dataset& sr)
332 -> VoidResult;
333
340 [[nodiscard]] auto validate_sr_template(const core::dicom_dataset& sr)
342
349 [[nodiscard]] auto get_cad_findings(std::string_view sr_sop_instance_uid)
350 -> Result<std::vector<cad_finding>>;
351
352 // ========================================================================
353 // Segmentation (SEG) Operations
354 // ========================================================================
355
367 [[nodiscard]] auto receive_segmentation(const core::dicom_dataset& seg)
368 -> VoidResult;
369
376 [[nodiscard]] auto validate_segmentation(const core::dicom_dataset& seg)
378
385 [[nodiscard]] auto get_segment_info(std::string_view seg_sop_instance_uid)
386 -> Result<std::vector<segment_info>>;
387
388 // ========================================================================
389 // Presentation State (PR) Operations
390 // ========================================================================
391
402 [[nodiscard]] auto receive_presentation_state(const core::dicom_dataset& pr)
403 -> VoidResult;
404
411 [[nodiscard]] auto validate_presentation_state(const core::dicom_dataset& pr)
413
414 // ========================================================================
415 // Source Linking Operations
416 // ========================================================================
417
428 [[nodiscard]] auto link_to_source(
429 std::string_view result_uid,
430 std::string_view source_study_uid) -> VoidResult;
431
439 [[nodiscard]] auto link_to_source(
440 std::string_view result_uid,
441 const source_reference& references) -> VoidResult;
442
449 [[nodiscard]] auto get_source_reference(std::string_view result_uid)
451
452 // ========================================================================
453 // Query Operations
454 // ========================================================================
455
462 [[nodiscard]] auto find_ai_results_for_study(std::string_view study_instance_uid)
463 -> Result<std::vector<ai_result_info>>;
464
472 [[nodiscard]] auto find_ai_results_by_type(
473 std::string_view study_instance_uid,
474 ai_result_type type) -> Result<std::vector<ai_result_info>>;
475
482 [[nodiscard]] auto get_ai_result_info(std::string_view sop_instance_uid)
483 -> std::optional<ai_result_info>;
484
491 [[nodiscard]] auto exists(std::string_view sop_instance_uid) const -> bool;
492
493 // ========================================================================
494 // Removal Operations
495 // ========================================================================
496
503 [[nodiscard]] auto remove(std::string_view sop_instance_uid) -> VoidResult;
504
511 [[nodiscard]] auto remove_ai_results_for_study(std::string_view study_instance_uid)
512 -> Result<std::size_t>;
513
514protected:
519 std::shared_ptr<storage::storage_interface> storage,
520 std::shared_ptr<storage::index_database> database);
521
522private:
524 class impl;
525 std::unique_ptr<impl> pimpl_;
526};
527
528} // namespace kcenon::pacs::ai
Simple result type for error handling.
auto operator=(const ai_result_handler &) -> ai_result_handler &=delete
void set_pre_store_validator(pre_store_validator validator)
Set custom pre-storage validator.
auto receive_presentation_state(const core::dicom_dataset &pr) -> VoidResult
Receive and store an AI-generated Presentation State.
ai_result_handler(ai_result_handler &&) noexcept
auto get_source_reference(std::string_view result_uid) -> Result< source_reference >
Get source references for an AI result.
auto remove(std::string_view sop_instance_uid) -> VoidResult
Remove an AI result and its source links.
auto receive_structured_report(const core::dicom_dataset &sr) -> VoidResult
Receive and store an AI-generated Structured Report.
auto exists(std::string_view sop_instance_uid) const -> bool
Check if an AI result exists.
auto find_ai_results_by_type(std::string_view study_instance_uid, ai_result_type type) -> Result< std::vector< ai_result_info > >
Find AI results by type.
auto receive_segmentation(const core::dicom_dataset &seg) -> VoidResult
Receive and store an AI-generated Segmentation object.
ai_result_handler(const ai_result_handler &)=delete
auto remove_ai_results_for_study(std::string_view study_instance_uid) -> Result< std::size_t >
Remove all AI results linked to a study.
static auto create(std::shared_ptr< storage::storage_interface > storage, std::shared_ptr< storage::index_database > database) -> std::unique_ptr< ai_result_handler >
Create an AI result handler.
auto find_ai_results_for_study(std::string_view study_instance_uid) -> Result< std::vector< ai_result_info > >
Find all AI results linked to a study.
auto get_ai_result_info(std::string_view sop_instance_uid) -> std::optional< ai_result_info >
Get AI result information by SOP Instance UID.
void set_received_callback(ai_result_received_callback callback)
Set callback for AI result reception notifications.
auto validate_segmentation(const core::dicom_dataset &seg) -> validation_result
Validate segmentation data integrity.
auto get_cad_findings(std::string_view sr_sop_instance_uid) -> Result< std::vector< cad_finding > >
Extract CAD findings from a Structured Report.
void configure(const ai_handler_config &config)
Configure handler behavior.
auto link_to_source(std::string_view result_uid, std::string_view source_study_uid) -> VoidResult
Link an AI result to its source study.
auto validate_presentation_state(const core::dicom_dataset &pr) -> validation_result
Validate Presentation State.
auto get_segment_info(std::string_view seg_sop_instance_uid) -> Result< std::vector< segment_info > >
Get segment information from a stored Segmentation.
virtual ~ai_result_handler()
Destructor.
auto get_config() const -> ai_handler_config
Get current configuration.
auto validate_sr_template(const core::dicom_dataset &sr) -> validation_result
Validate SR template conformance.
DICOM Dataset - ordered collection of Data Elements.
PACS index database for metadata storage and retrieval.
ai_result_type
Types of AI-generated DICOM objects.
@ structured_report
DICOM SR (Structured Report)
@ segmentation
DICOM SEG (Segmentation)
@ presentation_state
DICOM PR (Presentation State)
validation_status
Validation status for AI results.
@ invalid_template
SR template conformance failed.
@ unknown_error
Unexpected validation error.
@ invalid_segment_data
Segmentation data is malformed.
@ valid
All validations passed.
@ invalid_reference
Referenced source images not found.
@ missing_required_tags
Required DICOM tags are missing.
kcenon::common::VoidResult VoidResult
Result type alias for void operations.
std::function< void(const ai_result_info &info)> ai_result_received_callback
Callback for notification when AI result is received.
std::function< bool( const core::dicom_dataset &dataset, ai_result_type type)> pre_store_validator
Callback for pre-storage validation.
Abstract storage interface for DICOM persistent storage backends.
Configuration for AI result handler.
bool validate_sr_templates
Whether to validate SR template conformance.
std::vector< std::string > accepted_sr_templates
Accepted SR template identifiers (empty = accept all)
bool auto_link_to_source
Whether to auto-link results to source studies.
uint16_t max_segments
Maximum segment count for SEG objects (0 = unlimited)
bool validate_source_references
Whether to validate source references exist in storage.
Information about an AI result stored in the system.
std::string series_instance_uid
Series Instance UID.
ai_result_type type
Type of AI result.
std::string algorithm_name
AI model/algorithm identifier.
std::string algorithm_version
Algorithm version.
std::string sop_class_uid
SOP Class UID.
std::string source_study_uid
Study Instance UID of the source study.
std::string sop_instance_uid
SOP Instance UID of the AI result.
std::optional< std::string > description
Optional description.
std::chrono::system_clock::time_point received_at
Timestamp when the result was received.
CAD finding extracted from Structured Report.
std::optional< double > confidence
Confidence score (0.0 to 1.0)
std::string location
Location/site description.
std::optional< std::string > measurement
Additional measurement or annotation data.
std::optional< std::string > referenced_sop_instance_uid
Reference to specific image where finding was detected.
std::string finding_type
Finding type/category.
Segment information from Segmentation object.
std::string algorithm_type
Algorithm type that created this segment.
std::string segment_label
Segment label.
std::optional< std::tuple< uint8_t, uint8_t, uint8_t > > recommended_display_color
RGB color for visualization (optional)
uint16_t segment_number
Segment number (1-based)
std::optional< std::string > description
Segment description.
Source reference linking AI result to original images.
std::vector< std::string > sop_instance_uids
SOP Instance UIDs (optional, for instance-level reference)
std::string study_instance_uid
Study Instance UID.
std::optional< std::string > series_instance_uid
Series Instance UID (optional, for series-level reference)
Validation result containing status and details.
std::vector< std::string > missing_tags
List of missing required tags (if applicable)
validation_status status
Overall validation status.
std::optional< std::string > error_message
Detailed error message if validation failed.
std::vector< std::string > invalid_references
List of invalid references (if applicable)