Source code for viiapackage.tools.viia_create_server_result_json

### ===================================================================================================================
###   Import results from a folder and output to a result json (to be used after running on the server)
### ===================================================================================================================
# Copyright ©VIIA 2025

### ===================================================================================================================
###   1. Import modules
### ===================================================================================================================

# General imports
from __future__ import annotations
from typing import Union, Tuple
from pathlib import Path


### ===================================================================================================================
###   3. Function to collect results and output to tb file
### ===================================================================================================================

[docs]def viia_create_server_result_json( viia_object_number: str, folder_path: Union[str, Path], output_folder_path: Union[str, Path] = None)\ -> Path: """ This function loads in a model json present in an analysis folder, collects and reads the correct tb files and generates a result json with the processed results. Input: - viia_object_number (str): The object number used in the VIIA project, for example '1485V'. - folder_path (str or Path): The path object of the folder containing the analysis files as string or as instance of Path. - output_folder_path (str or Path): The path object of the folder where the output should be stored, as string or as instance of Path. By default, folder_path is used. Output: - If possible, a result json is generated and the corresponding file path is returned """ from viiapackage.viiaStatus import viia_create_project # Create a path object from the input string (or leaves the input as a Path object) if isinstance(folder_path, str): folder_path = Path(folder_path) # If output_folder is specified, create a path object from the input string (or leaves the input as a Path object) if output_folder_path is None: output_folder_path = folder_path elif isinstance(output_folder_path, str): output_folder_path = Path(output_folder_path) # Create test project, to process the model json and the tb files in project = viia_create_project(project_name=f'test-{viia_object_number}_results') project.current_analysis_folder = folder_path # Read the model json file from the analysis folder (reads the first json found that fits viia file convention) json_files = [file for file in folder_path.iterdir() if file.suffix == '.json' and viia_object_number in file.name and 'result' not in file.name] if len(json_files) == 1: json_file = json_files[0] project.viia_read_dump(json_file) elif len(json_files) == 0: raise FileNotFoundError( f'Model json for object {viia_object_number} could not be found in folder {folder_path}') else: raise ValueError( f'Multiple model jsons for object {viia_object_number} are found: ' f'{[json_file.name for json_file in json_files]} in folder {folder_path}, correct json could not be ' f'determined.') # Collect all unique tb file names and resp. analysis names by looping through all output blocks in all analyses # Only collects file names from output blocks with a tabulated output device tb_file_infos = list(set((project.current_analysis_folder / (o_b.output_filename + '.tb'), analysis) for analysis in project.collections.analyses for o_b in analysis.get_all_output_blocks() if o_b.output_device == 'tabulated')) # Load in tb files one by one for tb_file_path, analysis in tb_file_infos: if tb_file_path.exists(): project.read_diana_tbfile(file=tb_file_path, analysis=analysis) else: raise FileNotFoundError( f'tb file {tb_file_path.name} for analysis {analysis} could not be found in folder {folder_path}') # Generate a result json (and a new model json that corresponds to it) _, result_json, _ = project.viia_write_dump( filename=json_file.name, folder=output_folder_path, create_summary=False, result_json_only=True) if not result_json.exists(): raise FileNotFoundError( f'result json {result_json.name} was not generated in folder {output_folder_path}') return result_json
### =================================================================================================================== ### 4. End of script ### ===================================================================================================================