### ===================================================================================================================
### Load model from MYVIIA database for A1 analysis
### ===================================================================================================================
# Copyright ©VIIA 2024
### ===================================================================================================================
### 1. Import modules
### ===================================================================================================================
# General imports
from __future__ import annotations
import json
from typing import TYPE_CHECKING, Dict, Any
# References for functions and classes in the rhdhv_fem package
from rhdhv_fem.tools.fem_model_summary import fem_create_model_summary_data
# References for functions and classes in the viiaPackage
if TYPE_CHECKING:
from viiapackage.viiaStatus import ViiaProject
from viiapackage.database import myviia_get_a1_results, myviia_get_eng_a1_with_model
from viiapackage.database.myviia import MYVIIAEngA1
### ====================================================================================================================
### 2. Collect from MYVIIA and load the A1 model
### ====================================================================================================================
[docs]def viia_read_myviia_a1_model(project: ViiaProject) -> Dict[str, Any]:
"""
This function will collect the model from MYVIIA that was uploaded for the A1 analysis. The function will raise an
error when there is already a model present, or if something went wrong in the process to collect or read.
Input:
- project (obj): VIIA project object containing collections of fem objects and project variables.
Output:
- The model is read from MYVIIA and loaded into project.
- Returns model summary of the loaded json.
"""
# Collect the results of the A1 analyses on MYVIIA
a1_data = myviia_get_a1_results(object_deel_id=project.object_part['id'], token=project.token)
a1_data = [MYVIIAEngA1.from_myviia(item) for item in a1_data]
a1_data = [item for item in a1_data if item.eng_a1_model_id is not None]
# Create the model if it is available
if not a1_data:
raise ValueError(f"ERROR: The A1 model for {project.name} is not present on MYVIIA. Model could not be loaded.")
# Sort the list first on date and then ID
a1_data.sort(key=lambda x: (x.date, x.id))
eng_a1 = None
if a1_data and a1_data[-1].eng_a1_model_id:
eng_a1 = myviia_get_eng_a1_with_model(eng_a1_id=a1_data[-1].eng_a1_model_id, token=project.token)
# Create the model if it is available
if not eng_a1:
raise ValueError(
f"ERROR: There was an issue downloading the A1 model for {project.name} from MYVIIA. Model could not be "
f"loaded.")
# Check if there is already a model
if len(project.collections.shapes) > 0:
raise ValueError(
"ERROR: Loading a model from MYVIIA cannot be executed if there is already a model present.")
model_json = json.dumps(eng_a1['eng_a1_model']['model'])
project.read_dump(json_string=model_json)
# Return summary of the model
return fem_create_model_summary_data(project=project)
### ===================================================================================================================
### 3. End of script
### ===================================================================================================================