class AnalysisResult_v1
A dataclass that encapsulates the results from statistical analysis operations, including metadata, file paths, and timestamps.
/tf/active/vicechatdev/vice_ai/models.py
1844 - 1874
simple
Purpose
AnalysisResult serves as a structured container for storing and serializing statistical analysis results. It tracks the result type (summary statistics, test results, plots, tables, or interpretations), associated data, file paths for generated artifacts, and metadata. The class provides automatic initialization of optional fields and serialization capabilities for persistence or transmission. It's designed to be part of a larger statistical analysis workflow where results need to be tracked, stored, and retrieved with proper identification through session and step IDs.
Source Code
class AnalysisResult:
"""Results from statistical analysis"""
result_id: str
session_id: str
step_id: str
result_type: str # "summary_stats", "test_result", "plot", "table", "interpretation"
result_data: Dict[str, Any]
file_paths: List[str] = None
metadata: Dict[str, Any] = None
created_at: datetime = None
def __post_init__(self):
if self.file_paths is None:
self.file_paths = []
if self.metadata is None:
self.metadata = {}
if self.created_at is None:
self.created_at = datetime.now()
def to_dict(self) -> Dict[str, Any]:
"""Convert to dictionary with proper serialization"""
return {
'result_id': self.result_id,
'session_id': self.session_id,
'step_id': self.step_id,
'result_type': self.result_type,
'result_data': self.result_data,
'file_paths': self.file_paths,
'metadata': self.metadata,
'created_at': self.created_at.isoformat() if self.created_at else None
}
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
bases |
- | - |
Parameter Details
result_id: Unique identifier for this specific analysis result. Should be a unique string (e.g., UUID) to distinguish this result from others.
session_id: Identifier linking this result to a specific analysis session. Used to group related results together.
step_id: Identifier for the specific analysis step that produced this result. Allows tracking of results within a multi-step analysis workflow.
result_type: Category of the result. Must be one of: 'summary_stats', 'test_result', 'plot', 'table', or 'interpretation'. Determines how the result should be interpreted and displayed.
result_data: Dictionary containing the actual analysis result data. Structure varies based on result_type. Can contain statistical values, test outcomes, plot specifications, or other analysis outputs.
file_paths: Optional list of file system paths to files associated with this result (e.g., saved plots, exported tables). Defaults to empty list if not provided.
metadata: Optional dictionary for storing additional contextual information about the result (e.g., analysis parameters, data source info). Defaults to empty dict if not provided.
created_at: Timestamp when the result was created. Automatically set to current datetime if not provided during instantiation.
Return Value
Instantiation returns an AnalysisResult object with all fields initialized. The to_dict() method returns a dictionary representation with all fields serialized, where the created_at datetime is converted to ISO format string for JSON compatibility. Returns None for created_at if it was somehow not set.
Class Interface
Methods
__post_init__(self) -> None
Purpose: Automatically called after dataclass initialization to set default values for optional fields
Returns: None. Modifies instance attributes in place.
to_dict(self) -> Dict[str, Any]
Purpose: Converts the AnalysisResult instance to a dictionary with proper serialization of datetime objects
Returns: Dictionary containing all instance attributes with created_at converted to ISO format string. Keys are: result_id, session_id, step_id, result_type, result_data, file_paths, metadata, created_at
Attributes
| Name | Type | Description | Scope |
|---|---|---|---|
result_id |
str | Unique identifier for this analysis result | instance |
session_id |
str | Identifier for the analysis session this result belongs to | instance |
step_id |
str | Identifier for the specific analysis step that generated this result | instance |
result_type |
str | Type of result: 'summary_stats', 'test_result', 'plot', 'table', or 'interpretation' | instance |
result_data |
Dict[str, Any] | Dictionary containing the actual analysis result data, structure varies by result_type | instance |
file_paths |
List[str] | List of file paths associated with this result (e.g., saved plots or tables). Defaults to empty list. | instance |
metadata |
Dict[str, Any] | Additional metadata about the result. Defaults to empty dictionary. | instance |
created_at |
datetime | Timestamp when the result was created. Automatically set to current time if not provided. | instance |
Dependencies
datetimetypingdataclasses
Required Imports
from datetime import datetime
from typing import List, Dict, Any
from dataclasses import dataclass
Usage Example
from datetime import datetime
from typing import List, Dict, Any
from dataclasses import dataclass
import uuid
# Create an analysis result for summary statistics
result = AnalysisResult(
result_id=str(uuid.uuid4()),
session_id='session_123',
step_id='step_001',
result_type='summary_stats',
result_data={'mean': 45.2, 'std': 12.3, 'count': 100},
file_paths=['/path/to/output.csv'],
metadata={'dataset': 'experiment_1', 'variable': 'temperature'}
)
# Access attributes
print(result.result_type) # 'summary_stats'
print(result.result_data['mean']) # 45.2
# Convert to dictionary for serialization
result_dict = result.to_dict()
print(result_dict['created_at']) # ISO format timestamp
# Create with minimal required fields (optional fields auto-initialized)
minimal_result = AnalysisResult(
result_id=str(uuid.uuid4()),
session_id='session_456',
step_id='step_002',
result_type='test_result',
result_data={'p_value': 0.03, 'statistic': 2.45}
)
print(minimal_result.file_paths) # []
print(minimal_result.metadata) # {}
Best Practices
- Always provide unique result_id values (use uuid.uuid4() to generate) to avoid conflicts when storing multiple results
- Use consistent result_type values from the documented set: 'summary_stats', 'test_result', 'plot', 'table', 'interpretation'
- Structure result_data appropriately for the result_type to maintain consistency across the application
- Store absolute file paths in file_paths to avoid path resolution issues
- Use metadata dictionary to store any additional context that doesn't fit in the main fields
- The __post_init__ method automatically initializes optional fields, so you can omit them during instantiation
- Use to_dict() method when serializing to JSON or storing in databases, as it handles datetime conversion
- This is an immutable-style dataclass - consider creating new instances rather than modifying existing ones for better traceability
- Ensure session_id and step_id are consistent across related results for proper grouping and retrieval
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
class AnalysisResult 98.7% similar
-
class AnalysisRequest 69.7% similar
-
class DataSection 68.8% similar
-
class AnalysisStep 68.6% similar