ModelOutput#
- class nasem_dairy.model_output.ModelOutput.ModelOutput(locals_input, config_path='./model_output_structure.json', report_config_path='./report_structure.json')[source]#
Bases:
objectHandles the organization, retrieval, and reporting of NASEM model outputs.
The ModelOutput class loads output structures from JSON configuration files, categorizes model outputs into logical groups, and provides various methods for retrieving, searching, displaying, and exporting model results.
- Parameters:
locals_input (dict) – Dictionary of local input data containing all model output variables.
config_path (str, optional) – Path to the JSON file containing the model output structure, by default “./model_output_structure.json”
report_config_path (str, optional) – Path to the JSON file containing the report structure, by default “./report_structure.json”
- categories#
List of category names that organize the model outputs.
- Type:
List[str]
- skip_attrs#
List of internal attributes to skip when processing categories.
- Type:
List[str]
- locals_input#
Original input dictionary (cleared after processing).
- Type:
dict
- dev_out#
Dictionary containing development/internal variables filtered from input.
- Type:
dict
- categories_structure#
Structure loaded from the model output configuration file.
- Type:
dict
- report_structure#
Structure loaded from the report configuration file.
- Type:
dict
Examples
Create a ModelOutput instance from NASEM model results:
>>> # Assuming you have model output data in locals_dict >>> output = ModelOutput(locals_dict) >>> >>> # Access a specific category >>> production_data = output.Production >>> >>> # Search for variables containing "Milk" >>> milk_vars = output.search("Milk") >>> >>> # Get a specific value >>> milk_production = output.get_value("Mlk_Prod_comp") >>> >>> # Generate a report >>> summary_report = output.get_report("summary")
- export_to_JSON(file_path)[source]#
Export the entire ModelOutput instance to a JSON file.
Saves all model outputs to a JSON file using a custom encoder that handles NumPy arrays, pandas DataFrames, and other specialized data types.
- Parameters:
file_path (str) – The file path where the JSON file will be saved.
- export_to_dict()[source]#
Export all model outputs to a flat dictionary.
Extracts all values from the ModelOutput instance and organizes them into a single-level dictionary with variable names as keys.
- Returns:
Dictionary containing all model output variables as key-value pairs. Nested structures are flattened using the final variable names as keys.
- Return type:
Dict[str, Any]
- export_variable_names()[source]#
Extract a list of all variable names in the model output.
Returns all variable names, including DataFrame column names. If a value is a DataFrame, the DataFrame name is replaced with its individual column names.
- Returns:
Unique list of all variable names including DataFrame columns.
- Return type:
List[str]
- get_report(report_name)[source]#
Generate a formatted report based on predefined report structure.
Creates a structured report using the report configuration JSON file, organizing related variables into a readable table format with descriptions.
- Parameters:
report_name (str) – The name of the report to generate. Must match a report defined in the report structure configuration file.
- Returns:
Formatted report as a DataFrame with appropriate columns, totals, and footnotes as specified in the report configuration.
- Return type:
pd.DataFrame
- Raises:
ValueError – If the report name is not found in the report structure configuration.
- get_value(name)[source]#
Retrieve a value, dictionary, or dataframe by name.
Searches through all categories in the ModelOutput instance to find a specific value, dictionary, or dataframe by its exact name.
- Parameters:
name (str) – The exact name of the variable, dictionary, or dataframe to retrieve.
- Returns:
The object with the given name, or None if not found. Can be a scalar value, dictionary, DataFrame, or other data structure.
- Return type:
Union[str, int, float, dict, pd.DataFrame, None]
- search(search_string, dictionaries_to_search=None, case_sensitive=False)[source]#
Search for variables containing a specific string pattern.
Performs a pattern search across all or specified categories and returns matching results in a structured DataFrame with location information.
- Parameters:
search_string (str) – The string pattern to search for in variable names.
dictionaries_to_search (Union[None, List[str]], optional) – List of category names to search within. If None, searches all categories, by default None
case_sensitive (bool, optional) – Whether the search should be case-sensitive, by default False
- Returns:
DataFrame containing search results with columns: - ‘Name’: Variable name - ‘Value’: Variable value or type description - ‘Category’: Top-level category name - ‘Level 1’, ‘Level 2’, etc.: Nested location information
- Return type:
pd.DataFrame
- to_response_variables()[source]#
Convert model outputs to response variables for database storage.
Extracts only the variables specified in the response variables configuration, making it suitable for database storage or analysis workflows.
- Returns:
Dictionary containing only the specified response variables with their values. Variables not found in the model output will have None values.
- Return type:
Dict[str, Any]
Notes
This method uses the RESPONSE_VARIABLE_NAMES configuration to determine which variables to include in the output.