class AnalysisResult
A dataclass that encapsulates the results from statistical analysis operations, including metadata, file paths, and timestamps.
/tf/active/vicechatdev/vice_ai/smartstat_models.py
190 - 207
simple
Purpose
AnalysisResult serves as a structured container for storing and managing statistical analysis outputs. It captures various types of analysis results (summary statistics, test results, plots, tables, interpretations) along with associated metadata, file references, and tracking information. This class is designed to provide a standardized format for analysis results that can be easily serialized, stored, and retrieved in a statistical analysis workflow.
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()
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
bases |
- | - |
Parameter Details
result_id: Unique identifier for this specific analysis result. Should be a string that uniquely identifies this result instance, typically a UUID or similar unique string.
session_id: Identifier linking this result to a specific analysis session. Used to group related analysis results together within a session context.
step_id: Identifier for the specific analysis step that produced this result. Allows tracking which step in an analysis pipeline generated this output.
result_type: String indicating the type of result stored. Valid values include: 'summary_stats' (summary statistics), 'test_result' (statistical test outcomes), 'plot' (visualization data), 'table' (tabular data), 'interpretation' (textual analysis interpretation).
result_data: Dictionary containing the actual analysis result data. Structure varies based on result_type. Can contain any JSON-serializable data relevant to the analysis output.
file_paths: Optional list of file paths associated with this result (e.g., saved plots, exported tables). Defaults to empty list if not provided.
metadata: Optional dictionary for storing additional metadata about the result (e.g., analysis parameters, timestamps, user info). Defaults to empty dictionary if not provided.
created_at: Timestamp indicating when this result was created. Automatically set to current datetime if not provided during instantiation.
Return Value
Instantiation returns an AnalysisResult object with all specified attributes initialized. The __post_init__ method ensures that optional attributes (file_paths, metadata, created_at) are properly initialized with default values if not provided. The object can be used to access and manipulate analysis result data throughout its lifecycle.
Class Interface
Methods
__post_init__(self) -> None
Purpose: Initializes default values for optional attributes after the dataclass __init__ method runs. Ensures file_paths, metadata, and created_at have proper default values.
Returns: None. This method modifies the instance in-place by setting default values for optional attributes.
Attributes
| Name | Type | Description | Scope |
|---|---|---|---|
result_id |
str | Unique identifier for this analysis result instance | instance |
session_id |
str | Identifier linking this result to a specific analysis session | instance |
step_id |
str | Identifier for the analysis step that produced 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, exported tables). Defaults to empty list. | instance |
metadata |
Dict[str, Any] | Dictionary for storing additional metadata about the result. Defaults to empty dictionary. | instance |
created_at |
datetime | Timestamp indicating when this 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
@dataclass
class AnalysisResult:
result_id: str
session_id: str
step_id: str
result_type: str
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()
# Create a summary statistics result
result = AnalysisResult(
result_id="res_001",
session_id="session_123",
step_id="step_001",
result_type="summary_stats",
result_data={
"mean": 45.2,
"median": 43.0,
"std_dev": 12.5
},
metadata={"variable": "age", "n_samples": 100}
)
# Access attributes
print(result.result_type) # 'summary_stats'
print(result.result_data["mean"]) # 45.2
print(result.created_at) # Current timestamp
# Add file paths
result.file_paths.append("/path/to/plot.png")
# Update metadata
result.metadata["analyst"] = "John Doe"
Best Practices
- Always provide unique result_id values to avoid conflicts when storing multiple results
- Use consistent result_type values across your application to enable proper filtering and retrieval
- The result_data dictionary should contain JSON-serializable data for easy persistence
- Leverage the metadata dictionary to store contextual information that doesn't fit in the main result_data
- File paths should be absolute or relative to a known base directory for reliable file access
- The __post_init__ method automatically initializes optional fields, so you can safely omit them during instantiation
- Consider using dataclasses.asdict() to convert instances to dictionaries for serialization
- The created_at timestamp is automatically set, but can be overridden if you need to preserve historical timestamps
- Group related results using the same session_id for easier batch operations
- Use step_id to maintain the order and relationship of results in multi-step analysis pipelines
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
class AnalysisResult_v1 98.7% similar
-
class AnalysisResult_v1 94.0% similar
-
class AnalysisRequest 69.6% similar
-
class DataSection 67.1% similar
-
class AnalysisStep 66.8% similar